Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Logic of "all" + verbs + relative clauses, for a class at Indiana University

Views: 6871
1
module ProofTreeNumbers (PTree(..), lineNumberHelp, near, full_reverse, tree_reverse)
2
where
3
4
5
data PTree a = T a [PTree a]
6
deriving (Show, Eq)
7
get1 (aa,b,c) = aa
8
get2 (aa,b,c) = b
9
get3 (aa,b,c) = c
10
11
12
lineNumberHelp :: [(a, [Int], Int)] -> [(a, [Int], Int)] -> [(a, [Int], Int)]
13
lineNumberHelp firstseq secondseq =
14
let
15
n = if null firstseq then 0 else (get3 $ head firstseq)
16
modify w = map ( \ (x, listofInts, k) -> (x, (map (\ i -> 1 + n+i) listofInts), k)) w
17
in (firstseq ++ modify secondseq)
18
19
------ near of a PTree lists the nodes in depth-first order, along with an extra list for each node.
20
------ for the node n in the tree it lists the addresses of the children of n in the same tree, again in the
21
------ depth-first order of the tree overall. Getting this right was probably the hardest part of this whole exercise.
22
------ Note also that what we want in the end is not the depth-first listing of the PTree but rather the 'bottom-up' listing,
23
------ and these are related by
24
------
25
------ bottom_up t = reverse (depth_first ( tree_reverse t))
26
27
near :: (PTree a) -> [(a, [Int], Int)]
28
near (T x l) = [(x,s, k+1 )] ++ extrastuff
29
where
30
k = sum r
31
--extrastuff :: [(a, [Int], Int)]
32
extrastuff = foldl lineNumberHelp [] (map near l)
33
--q :: [(a, [Int], Int)]
34
q = map head $ map near l
35
r :: [Int]
36
r = map get3 q
37
s = init( scanl (+) 2 r)
38
39
tree_reverse (T n t) = T n (map tree_reverse (reverse t))
40
41
full_reverse :: [(a, [Int], Int)] -> [(Int, a, [Int])]
42
full_reverse h =
43
let
44
n = length h
45
p = [(i, reverse( map (\ x -> n + 1 - x) j )) | (i,j,k) <- h]
46
q = reverse p
47
w = [1..n]
48
mergeMe :: [Int] -> [(a,[Int])] -> [(Int,a,[Int])]
49
mergeMe [] [] = []
50
mergeMe (i:wMore) (pair:pMore) = (i, (fst pair), (snd pair)) : (mergeMe wMore pMore)
51
in mergeMe w q
52
53