forked from i-loder-matthew/RadicalLF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeUtils.hs
65 lines (51 loc) · 1.97 KB
/
TreeUtils.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module TreeUtils where
import QRT
import Data.Maybe
-- Zippers for Type Structures
data TSCxt = Top | L TypeS TSCxt | R TypeS TSCxt | Abs Int TSCxt
deriving (Eq, Show)
data TSLoc = Loc TypeS TSCxt
deriving (Eq, Show)
toZip :: TypeS -> TSLoc
toZip ts = Loc ts Top
toTree :: TSLoc -> TypeS
toTree (Loc ts Top) = ts
toTree loc = (toTree . fromJust . parent) loc
parent :: TSLoc -> Maybe TSLoc
parent (Loc _ Top) = Nothing
parent (Loc ts cxt) = case cxt of
L right parent -> Just (Loc (Node ts right) parent)
R left parent -> Just (Loc (Node left ts) parent)
Abs ix parent -> Just (Loc (Lambda ix ts) parent)
sister :: TSLoc -> Maybe TSLoc
sister (Loc ts cxt) = case cxt of
L right parent -> Just (Loc right (R ts cxt))
R left parent -> Just (Loc left (L ts cxt))
_ -> Nothing
downRight :: TSLoc -> Maybe TSLoc
downRight (Loc (Leaf _) _) = Nothing
downRight (Loc (Tr _) _) = Nothing
downRight (Loc (Lambda _ _) _) = Nothing
downRight (Loc (Node l r) cxt) = Just (Loc r (R l cxt))
downLeft :: TSLoc -> Maybe TSLoc
downLeft (Loc (Leaf _) _) = Nothing
downLeft (Loc (Tr _) _) = Nothing
downLeft (Loc (Lambda _ _) _) = Nothing
downLeft (Loc (Node l r) cxt) = Just (Loc l (L r cxt))
downLambda :: TSLoc -> Maybe TSLoc
downLambda (Loc (Leaf _) _) = Nothing
downLambda (Loc (Tr _) _) = Nothing
downLambda (Loc (Node {}) _) = Nothing
downLambda (Loc (Lambda ix r) cxt) = Just (Loc r (Abs ix cxt))
-- downLambda _ = Nothing
-- find list of all places where condition is true
find :: TSLoc -> (TSLoc -> Bool) -> [TSLoc]
find loc test = case loc of
Loc (Leaf _) _ -> [loc | test loc]
Loc (Tr _) _ -> [loc | test loc]
Loc (Lambda _ r) _ -> [loc | test loc] ++ find (fromJust (downLambda loc)) test
Loc (Node {}) _ -> [loc | test loc] ++ find (fromJust (downRight loc)) test ++ find (fromJust (downLeft loc)) test
replaceSubTree :: TSLoc -> TypeS -> TSLoc
replaceSubTree (Loc ts1 cxt) ts2 = Loc ts2 cxt
getSubTreeAt :: TSLoc -> TypeS
getSubTreeAt (Loc ts1 cxt) = ts1