0 | module Data.Container.Base.TreeUtils
2 | import Decidable.Equality
3 | import Language.Reflection
4 | import Derive.Prelude
7 | import Data.Container.Base.Extension.Definition
9 | import Data.Container.SubTerm
11 | %language ElabReflection
32 | namespace BinaryTrees
35 | data BinTreeShape : Type where
36 | LeafS : BinTreeShape
37 | NodeS : BinTreeShape -> BinTreeShape -> BinTreeShape
39 | %runElab derive "BinTreeShape" [Eq, Show]
40 | %name BinTreeShape
b, b1, b2, b3, b4, b5
43 | numLeaves : BinTreeShape -> Nat
45 | numLeaves (NodeS lt rt) = numLeaves lt + numLeaves rt
48 | numNodes : BinTreeShape -> Nat
50 | numNodes (NodeS lt rt) = numNodes lt + (1 + numNodes rt)
53 | numNodesAndLeaves : BinTreeShape -> Nat
54 | numNodesAndLeaves LeafS = 1
55 | numNodesAndLeaves (NodeS lt rt)
56 | = numNodesAndLeaves lt + (1 + numNodesAndLeaves rt)
58 | namespace NodesAndLeaves
61 | data BinTreePos : (b : BinTreeShape) -> Type where
62 | AtLeaf : BinTreePos LeafS
63 | AtNode : {l, r : BinTreeShape} -> BinTreePos (NodeS l r)
64 | GoLeft : {l, r : BinTreeShape} -> BinTreePos l -> BinTreePos (NodeS l r)
65 | GoRight : {l, r : BinTreeShape} -> BinTreePos r -> BinTreePos (NodeS l r)
67 | %runElab deriveIndexed "BinTreePos" [Eq, Show]
70 | DecEq (BinTreePos b) where
71 | decEq AtLeaf AtLeaf = Yes Refl
72 | decEq AtNode AtNode = Yes Refl
73 | decEq (GoLeft p) (GoLeft q) = case decEq p q of
74 | Yes Refl => Yes Refl
75 | No contra => No (\Refl => contra Refl)
76 | decEq (GoRight p) (GoRight q) = case decEq p q of
77 | Yes Refl => Yes Refl
78 | No contra => No (\Refl => contra Refl)
79 | decEq AtNode (GoLeft _) = No (\case Refl
impossible)
80 | decEq AtNode (GoRight _) = No (\case Refl
impossible)
81 | decEq (GoLeft _) AtNode = No (\case Refl
impossible)
82 | decEq (GoLeft _) (GoRight _) = No (\case Refl
impossible)
83 | decEq (GoRight _) AtNode = No (\case Refl
impossible)
84 | decEq (GoRight _) (GoLeft _) = No (\case Refl
impossible)
89 | MOrd (BinTreePos b) where
90 | mcompare AtLeaf AtLeaf = Just EQ
91 | mcompare AtNode AtNode = Just EQ
92 | mcompare (GoLeft b1) (GoLeft b2) = mcompare b1 b2
93 | mcompare (GoRight b1) (GoRight b2) = mcompare b1 b2
94 | mcompare AtNode (GoLeft _) = Just LT
95 | mcompare AtNode (GoRight _) = Just LT
96 | mcompare (GoLeft _) AtNode = Just GT
97 | mcompare (GoRight _) AtNode = Just GT
98 | mcompare (GoLeft _) (GoRight _) = Nothing
99 | mcompare (GoRight _) (GoLeft _) = Nothing
106 | data BinTreePosNode : (b : BinTreeShape) -> Type where
107 | AtNode : {l, r : BinTreeShape} -> BinTreePosNode (NodeS l r)
108 | GoLeft : {l, r : BinTreeShape} -> BinTreePosNode l -> BinTreePosNode (NodeS l r)
109 | GoRight : {l, r : BinTreeShape} -> BinTreePosNode r -> BinTreePosNode (NodeS l r)
111 | %runElab deriveIndexed "BinTreePosNode" [Eq, Show]
114 | DecEq (BinTreePosNode b) where
115 | decEq AtNode AtNode = Yes Refl
116 | decEq (GoLeft p) (GoLeft q) = case decEq p q of
117 | Yes Refl => Yes Refl
118 | No contra => No (\Refl => contra Refl)
119 | decEq (GoRight p) (GoRight q) = case decEq p q of
120 | Yes Refl => Yes Refl
121 | No contra => No (\Refl => contra Refl)
122 | decEq AtNode (GoLeft _) = No (\case Refl
impossible)
123 | decEq AtNode (GoRight _) = No (\case Refl
impossible)
124 | decEq (GoLeft _) AtNode = No (\case Refl
impossible)
125 | decEq (GoLeft _) (GoRight _) = No (\case Refl
impossible)
126 | decEq (GoRight _) AtNode = No (\case Refl
impossible)
127 | decEq (GoRight _) (GoLeft _) = No (\case Refl
impossible)
130 | {b : BinTreeShape} -> Finite (BinTreePosNode b) where
131 | values {b = LeafS} = []
132 | values {b = (NodeS l r)} =
133 | let tl = GoLeft <$> values {a=BinTreePosNode l}
134 | tr = GoRight <$> values {a=BinTreePosNode r}
135 | in tl ++ [AtNode] ++ tr
138 | MOrd (BinTreePosNode b) where
139 | mcompare AtNode AtNode = Just EQ
140 | mcompare (GoLeft b1) (GoLeft b2) = mcompare b1 b2
141 | mcompare (GoRight b1) (GoRight b2) = mcompare b1 b2
142 | mcompare AtNode (GoLeft _) = Just LT
143 | mcompare AtNode (GoRight _) = Just LT
144 | mcompare (GoLeft _) AtNode = Just GT
145 | mcompare (GoRight _) AtNode = Just GT
146 | mcompare (GoLeft _) (GoRight _) = Nothing
147 | mcompare (GoRight _) (GoLeft _) = Nothing
152 | data BinTreePosLeaf : (b : BinTreeShape) -> Type where
153 | AtLeaf : BinTreePosLeaf LeafS
154 | GoLeft : {l, r : BinTreeShape} -> BinTreePosLeaf l -> BinTreePosLeaf (NodeS l r)
155 | GoRight : {l, r : BinTreeShape} -> BinTreePosLeaf r -> BinTreePosLeaf (NodeS l r)
157 | %runElab deriveIndexed "BinTreePosLeaf" [Eq, Show]
160 | DecEq (BinTreePosLeaf b) where
161 | decEq AtLeaf AtLeaf = Yes Refl
162 | decEq (GoLeft p) (GoLeft q) = case decEq p q of
163 | Yes Refl => Yes Refl
164 | No contra => No (\Refl => contra Refl)
165 | decEq (GoRight p) (GoRight q) = case decEq p q of
166 | Yes Refl => Yes Refl
167 | No contra => No (\Refl => contra Refl)
168 | decEq (GoLeft _) (GoRight _) = No (\case Refl
impossible)
169 | decEq (GoRight _) (GoLeft _) = No (\case Refl
impossible)
173 | {b : BinTreeShape} -> Finite (BinTreePosLeaf b) where
174 | values {b = LeafS} = [AtLeaf]
175 | values {b = (NodeS l r)} =
176 | let tl = GoLeft <$> values {a=BinTreePosLeaf l}
177 | tr = GoRight <$> values {a=BinTreePosLeaf r}
181 | MOrd (BinTreePosLeaf b) where
182 | mcompare AtLeaf AtLeaf = Just EQ
183 | mcompare (GoLeft b1) (GoLeft b2) = mcompare b1 b2
184 | mcompare (GoRight b1) (GoRight b2) = mcompare b1 b2
185 | mcompare (GoLeft _) (GoRight _) = Nothing
186 | mcompare (GoRight _) (GoLeft _) = Nothing