@@ -17,11 +17,12 @@ import Data.Tree (Forest, Tree, mkTree, modifyNodeValue, setNodeValue)
1717-- | tree to reach the level of the current `Node` starting from the tree's root,
1818-- | and the `before` and `after` fields describe its location in the current
1919-- | level.
20−newtype Loc a = Loc { node :: Tree a
21− , before :: Forest a
22− , after :: Forest a
23− , parents :: List (Loc a)
24− }
20+newtype Loc a = Loc
21+ { node :: Tree a
22+ , before :: Forest a
23+ , after :: Forest a
24+ , parents :: List (Loc a)
25+ }
2526
2627derive newtype instance eqLoc :: Eq a => Eq (Loc a)
2728
@@ -32,51 +33,56 @@ next :: forall a. Loc a -> Maybe (Loc a)
3233next (Loc r) =
3334 case r.after of
3435 Nil -> Nothing
35− (c:cs) -> Just $ Loc r { node = c
36− , before = r.node : r.before
37− , after = cs
38− }
36+ (c : cs) -> Just $ Loc r
37+ { node = c
38+ , before = r.node : r.before
39+ , after = cs
40+ }
3941
4042-- -- | Move the cursor to the previous sibling.
4143prev :: forall a. Loc a -> Maybe (Loc a)
4244prev (Loc r) =
4345 case r.before of
4446 Nil -> Nothing
45− (c:cs) -> Just $ Loc r { node = c
46− , before = cs
47− , after = r.node : r.after
48− }
47+ (c : cs) -> Just $ Loc r
48+ { node = c
49+ , before = cs
50+ , after = r.node : r.after
51+ }
4952
5053-- -- | Move the cursor to the first sibling.
5154first :: forall a. Loc a -> Loc a
5255first l@(Loc r) =
5356 case reverse r.before of
5457 Nil -> l
55− c:cs -> Loc r { node = c
56− , before = Nil
57− , after = cs <> r.after
58− }
58+ c : cs -> Loc r
59+ { node = c
60+ , before = Nil
61+ , after = cs <> r.after
62+ }
5963
6064-- -- | Move the cursor to the last sibling.
6165last :: forall a. Loc a -> Loc a
6266last l@(Loc r) =
6367 case reverse r.after of
6468 Nil -> l
65− c:cs -> Loc r { node = c
66− , before = cs <> (r.node : r.before)
67− , after = Nil
68− }
69+ c : cs -> Loc r
70+ { node = c
71+ , before = cs <> (r.node : r.before)
72+ , after = Nil
73+ }
6974
7075-- -- | Move the cursor to the parent `Node`.
7176up :: forall a. Loc a -> Maybe (Loc a)
7277up l@(Loc r) =
7378 case r.parents of
7479 Nil -> Nothing
75− (p:ps) -> Just $ Loc { node: (value p) :< (siblings l)
76− , before: before p
77− , after: after p
78− , parents: ps
79− }
80+ (p : ps) -> Just $ Loc
81+ { node: (value p) :< (siblings l)
82+ , before: before p
83+ , after: after p
84+ , parents: ps
85+ }
8086
8187-- | Move the cursor to the root of the tree.
8288root :: forall a. Loc a -> Loc a
@@ -90,12 +96,13 @@ firstChild :: forall a. Loc a -> Maybe (Loc a)
9096firstChild n =
9197 case children n of
9298 Nil -> Nothing
93− c:cs ->
94− Just $ Loc { node: c
95− , before: Nil
96− , after: cs
97− , parents: n : (parents n)
98− }
99+ c : cs ->
100+ Just $ Loc
101+ { node: c
102+ , before: Nil
103+ , after: cs
104+ , parents: n : (parents n)
105+ }
99106
100107-- | Move the cursor to the first child of the current `Node`.
101108down :: forall a. Loc a -> Maybe (Loc a)
@@ -106,12 +113,13 @@ lastChild :: forall a. Loc a -> Maybe (Loc a)
106113lastChild n =
107114 case reverse (children n) of
108115 Nil -> Nothing
109− c:cs ->
110− Just $ Loc { node: c
111− , before: cs
112− , after: Nil
113− , parents: n : (parents n)
114− }
116+ c : cs ->
117+ Just $ Loc
118+ { node: c
119+ , before: cs
120+ , after: Nil
121+ , parents: n : (parents n)
122+ }
115123
116124-- | Move the cursor to a specific sibling by it's index.
117125siblingAt :: forall a. Int -> Loc a -> Maybe (Loc a)
@@ -119,18 +127,18 @@ siblingAt i l@(Loc r) = do
119127 p@(Loc r') <- up l
120128 c <- (children p) !! i
121129 let before' = reverse $ take i (children p)
122− let after' = drop (i+1) (children p)
123− pure $ Loc { node: c
124− , before: before'
125− , after: after'
126− , parents: r.parents
127− }
130+ let after' = drop (i + 1) (children p)
131+ pure $ Loc
132+ { node: c
133+ , before: before'
134+ , after: after'
135+ , parents: r.parents
136+ }
128137
129138-- | Move the cursor to a specific child of the current `Node` by it's index.
130139childAt :: forall a. Int -> Loc a -> Maybe (Loc a)
131140childAt i p = (firstChild p) >>= (siblingAt i)
132141
133−
134142-- | Retrieve the `Tree` representation, i.e., returns the root `Node` of the
135143-- | current tree.
136144toTree :: forall a. Loc a -> Tree a
@@ -138,11 +146,12 @@ toTree = node <<< root
138146
139147-- | Get a `Loc`ation representation from a given `Tree`.
140148fromTree :: forall a. Tree a -> Loc a
141−fromTree n = Loc { node: n
142− , before: Nil
143− , after: Nil
144− , parents: Nil
145− }
149+fromTree n = Loc
150+ { node: n
151+ , before: Nil
152+ , after: Nil
153+ , parents: Nil
154+ }
146155
147156-- | Set the `Node` at the current position.
148157setNode :: forall a. Tree a -> Loc a -> Loc a
@@ -164,69 +173,75 @@ modifyValue f l = setNode (modifyNodeValue f (node l)) l
164173
165174-- | Insert a node after the current position, and move cursor to the new node.
166175insertAfter :: forall a. Tree a -> Loc a -> Loc a
167−insertAfter n (Loc r) = Loc r { node = n
168− , before = r.node : r.before
169− }
176+insertAfter n (Loc r) = Loc r
177+ { node = n
178+ , before = r.node : r.before
179+ }
170180
171181-- | Insert a node before the current position, and move cursor to the new node.
172182insertBefore :: forall a. Tree a -> Loc a -> Loc a
173−insertBefore n (Loc r) = Loc r { node = n
174− , after = r.node : r.after
175− }
183+insertBefore n (Loc r) = Loc r
184+ { node = n
185+ , after = r.node : r.after
186+ }
176187
177188-- | Insert a node as a child to the current node, and move cursor to the new node.
178189insertChild :: forall a. Tree a -> Loc a -> Loc a
179190insertChild n l =
180191 case down l of
181192 Just c -> insertAfter n c
182− Nothing -> Loc { node: n
183− , after: Nil
184− , before: Nil
185− , parents: l : (parents l)
186− }
193+ Nothing -> Loc
194+ { node: n
195+ , after: Nil
196+ , before: Nil
197+ , parents: l : (parents l)
198+ }
187199
188200-- | Delete the node in the current position.
189201delete :: forall a. Loc a -> Loc a
190202delete l@(Loc r) =
191203 case r.after of
192− c:cs -> Loc r { node = c
193− , after = cs
194− }
204+ c : cs -> Loc r
205+ { node = c
206+ , after = cs
207+ }
195208 Nil ->
196209 case r.before of
197− c:cs -> Loc r { node = c
198− , before = cs
199− }
210+ c : cs -> Loc r
211+ { node = c
212+ , before = cs
213+ }
200214 Nil ->
201215 case r.parents of
202216 Nil -> l
203− c:cs -> Loc { node: mkTree (value c) Nil
204− , before: before c
205− , after: after c
206− , parents: parents c
207− }
217+ c : cs -> Loc
218+ { node: mkTree (value c) Nil
219+ , before: before c
220+ , after: after c
221+ , parents: parents c
222+ }
208223
209224-- Searches
210225
211− -- | Search down and to the right for the first occurence where the given predicate is true and return the Loc
226+-- | Search down and to the right for the first occurence where the given predicate is true and return the Loc
212227findDownWhere :: ∀ a. (a -> Boolean) -> Loc a -> Maybe (Loc a)
213228findDownWhere predicate loc | predicate $ value loc = Just loc
214229findDownWhere predicate loc = lookNext <|> lookDown
215230 where
216− lookNext = next loc >>= findDownWhere predicate
217− lookDown = down loc >>= findDownWhere predicate
231+ lookNext = next loc >>= findDownWhere predicate
232+ lookDown = down loc >>= findDownWhere predicate
218233
219234-- | Search for the first occurence of the value `a` downwards and to the right.
220235findDown :: forall a. Eq a => a -> Loc a -> Maybe (Loc a)
221−findDown a = findDownWhere ( _ == a)
236+findDown a = findDownWhere (_ == a)
222237
223238-- | Search to the left and up for the first occurence where the given predicate is true and return the Loc
224239findUpWhere :: ∀ a. (a -> Boolean) -> Loc a -> Maybe (Loc a)
225240findUpWhere predicate loc | predicate $ value loc = Just loc
226241findUpWhere predicate loc = lookPrev <|> lookUp
227242 where
228− lookPrev = prev loc >>= findUpWhere predicate
229− lookUp = up loc >>= findUpWhere predicate
243+ lookPrev = prev loc >>= findUpWhere predicate
244+ lookUp = up loc >>= findUpWhere predicate
230245
231246-- | Search for the first occurence of the value `a` upwards and to the left,
232247findUp :: forall a. Eq a => a -> Loc a -> Maybe (Loc a)
@@ -246,20 +261,18 @@ findFromRoot a = findFromRootWhere (_ == a)
246261flattenLocDepthFirst :: ∀ a. Loc a -> List (Loc a)
247262flattenLocDepthFirst loc = loc : (go loc)
248263 where
249− go :: Loc a -> List (Loc a)
250− go loc' =
251− let
252− downs = goDir loc' down
253− nexts = goDir loc' next
254− in
255− downs <> nexts
256−
257− goDir :: Loc a -> (Loc a -> Maybe (Loc a)) -> List (Loc a)
258− goDir loc' dirFn = case (dirFn loc') of
259− Just l -> l : go l
260− Nothing -> Nil
261−
262−
264+ go :: Loc a -> List (Loc a)
265+ go loc' =
266+ let
267+ downs = goDir loc' down
268+ nexts = goDir loc' next
269+ in
270+ downs <> nexts
271+
272+ goDir :: Loc a -> (Loc a -> Maybe (Loc a)) -> List (Loc a)
273+ goDir loc' dirFn = case (dirFn loc') of
274+ Just l -> l : go l
275+ Nothing -> Nil
263276
264277-- Setters and Getters
265278node :: forall a. Loc a -> Tree a