1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE NamedFieldPuns #-}
3 {-# OPTIONS_GHC -fno-warn-tabs #-}
4 module Data.TreeSeq.Strict.Zipper where
6 import Control.Applicative (Applicative(..), Alternative(..))
7 import Control.Monad (Monad(..), (>=>))
10 import Data.Function (($), (.))
11 import Data.Functor ((<$>))
13 import Data.Maybe (Maybe(..), maybe)
14 import Data.Monoid (Monoid(..))
15 import Data.Sequence (ViewL(..), ViewR(..), (<|), (|>))
16 import Data.Semigroup (Semigroup(..))
17 import Data.Typeable (Typeable)
18 import Prelude (undefined)
19 import Text.Show (Show(..))
20 import qualified Data.Sequence as Seq
21 import qualified Data.List as List
23 import Data.TreeSeq.Strict (Trees, Tree(..))
25 safeHead :: Alternative f => [a] -> f a
27 safeHead (a:_) = pure a
29 nodesTree :: Tree k a -> Trees k a
30 nodesTree Tree0{} = mempty
31 nodesTree (TreeN _k ts) = ts
33 keyTree :: Tree k a -> k
34 keyTree (TreeN k _) = k
35 keyTree Tree0{} = undefined
40 { zipper_path :: [Zipper_Step k a]
41 , zipper_curr :: Trees k a
42 } deriving (Eq, Show, Typeable)
44 zipper :: Trees k a -> Zipper k a
47 zipper_root :: Zipper k a -> Trees k a
48 zipper_root = zipper_curr . List.last . zipper_ancestor_or_self
50 path_of_zipper :: Zipper k x -> [k]
52 keyTree . zipper_step_self <$>
53 List.reverse (zipper_path z)
55 -- * Type 'Zipper_Step'
58 { zipper_step_prec :: Trees k a
59 , zipper_step_self :: Tree k a
60 , zipper_step_foll :: Trees k a
61 } deriving (Eq, Show, Typeable)
64 -- | Collect all 'Zipper's along a given axis,
65 -- including the first 'Zipper'.
66 zipper_collect :: (z -> Maybe z) -> z -> [z]
67 zipper_collect f z = z : maybe [] (zipper_collect f) (f z)
69 -- | Collect all 'Zipper's along a given axis,
70 -- excluding the first 'Zipper'.
71 zipper_collect_without_self :: (z -> Maybe z) -> z -> [z]
72 zipper_collect_without_self f z = maybe [] (zipper_collect f) (f z)
75 zipper_self :: Zipper k a -> [Tree k a]
76 zipper_self (Zipper (Zipper_Step _ t _ : _) _) = [t]
80 zipper_child :: Zipper k a -> [Zipper k a]
82 zipper_child_first z >>=
83 zipper_collect zipper_foll
85 zipper_child_lookup ::
87 (k -> Bool) -> Zipper k a -> f (Zipper k a)
88 zipper_child_lookup fk z = safeHead $ zipper_childs_lookup fk z
90 zipper_childs_lookup ::
91 (k -> Bool) -> Zipper k a -> [Zipper k a]
92 zipper_childs_lookup fk (Zipper path ts) =
93 (<$> Seq.findIndicesL (\case TreeN k _ -> fk k; Tree0{} -> False) ts) $ \i ->
94 let (ps, ps') = Seq.splitAt i ts in
99 { zipper_path = Zipper_Step ps t fs : path
100 , zipper_curr = nodesTree t
103 zipper_child_first :: Alternative f => Zipper k a -> f (Zipper k a)
104 zipper_child_first (Zipper path trees) =
105 case Seq.viewl trees of
107 t :< ts -> pure $ Zipper
108 { zipper_path = Zipper_Step mempty t ts : path
109 , zipper_curr = nodesTree t
112 zipper_child_last :: Alternative f => Zipper k a -> f (Zipper k a)
113 zipper_child_last (Zipper path trees) =
114 case Seq.viewr trees of
116 ts :> t -> pure $ Zipper
117 { zipper_path = Zipper_Step ts t mempty : path
118 , zipper_curr = nodesTree t
122 zipper_ancestor :: Zipper k a -> [Zipper k a]
123 zipper_ancestor = zipper_collect_without_self zipper_parent
125 zipper_ancestor_or_self :: Zipper k a -> [Zipper k a]
126 zipper_ancestor_or_self = zipper_collect zipper_parent
128 -- ** Axis descendant
129 zipper_descendant_or_self :: Zipper k a -> [Zipper k a]
130 zipper_descendant_or_self =
133 collect_child acc z =
136 (zipper_child_first z)
144 zipper_descendant_or_self_reverse :: Zipper k a -> [Zipper k a]
145 zipper_descendant_or_self_reverse z =
147 zipper_descendant_or_self_reverse
148 (List.reverse $ zipper_child z)
150 zipper_descendant :: Zipper k a -> [Zipper k a]
151 zipper_descendant = List.tail . zipper_descendant_or_self
154 zipper_prec :: Alternative f => Zipper k a -> f (Zipper k a)
155 zipper_prec (Zipper [] _curr) = empty
156 zipper_prec (Zipper (Zipper_Step ps c fs : path) _curr) =
159 ts :> t -> pure Zipper
160 { zipper_path = Zipper_Step ts t (c <| fs) : path
161 , zipper_curr = nodesTree t
164 zipper_preceding :: Zipper k a -> [Zipper k a]
166 zipper_ancestor_or_self >=>
167 zipper_preceding_sibling >=>
168 zipper_descendant_or_self_reverse
170 zipper_preceding_sibling :: Zipper k a -> [Zipper k a]
171 zipper_preceding_sibling = zipper_collect_without_self zipper_prec
174 zipper_foll :: Alternative f => Zipper k a -> f (Zipper k a)
175 zipper_foll (Zipper [] _curr) = empty
176 zipper_foll (Zipper (Zipper_Step ps c fs:path) _curr) =
179 t :< ts -> pure $ Zipper
180 { zipper_path = Zipper_Step (ps |> c) t ts : path
181 , zipper_curr = nodesTree t
184 zipper_following :: Zipper k a -> [Zipper k a]
186 zipper_ancestor_or_self >=>
187 zipper_following_sibling >=>
188 zipper_descendant_or_self
190 zipper_following_sibling :: Zipper k a -> [Zipper k a]
191 zipper_following_sibling = zipper_collect_without_self zipper_foll
194 zipper_parent :: Alternative f => Zipper k a -> f (Zipper k a)
195 zipper_parent (Zipper [] _) = empty
196 zipper_parent (Zipper (Zipper_Step ps c fs : path) curr) =
199 , zipper_curr = (ps |> m) <> fs
203 TreeN k _ -> TreeN k curr
208 (Zipper k a -> [Zipper k a]) ->
209 (Zipper k a -> Bool) ->
210 (Zipper k a -> [Zipper k a])
211 zipper_filter axis p z = List.filter p (axis z)
212 infixl 5 `zipper_filter`
216 (Zipper k a -> [Zipper k a]) -> Int ->
217 (Zipper k a -> f (Zipper k a))
219 case List.drop n (axis z) of
225 (Zipper k a -> [Zipper k a]) ->
227 zipper_null axis = List.null . axis