1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE NamedFieldPuns #-}
3 {-# OPTIONS_GHC -fno-warn-tabs #-}
5 module Data.TreeMap.Strict.Zipper where
7 import Control.Monad (Monad(..), (>=>))
8 import Control.Applicative (Applicative(..), Alternative(..))
9 import Data.Bool (Bool)
10 import Data.Data (Data)
12 import Data.Function (($), (.))
13 import Data.Functor ((<$>))
15 import qualified Data.List as List
16 import Data.List.NonEmpty (NonEmpty(..))
17 import qualified Data.Map.Strict as Map
18 import Data.Maybe (Maybe(..), maybe, maybeToList)
19 import Data.Ord (Ord(..))
20 import Data.Tuple (fst)
21 import Data.Typeable (Typeable)
22 import Text.Show (Show(..))
24 import Data.TreeMap.Strict (TreeMap(..))
25 import qualified Data.TreeMap.Strict as TreeMap
31 { zipper_path :: [Zipper_Step k a]
32 , zipper_curr :: TreeMap k a
33 } deriving (Data, Eq, Show, Typeable)
35 zipper :: TreeMap k a -> Zipper k a
38 zipper_root :: Ord k => Zipper k a -> TreeMap k a
39 zipper_root = zipper_curr . List.last . zipper_ancestor_or_self
41 path_of_zipper :: Zipper k x -> [k]
43 fst . zipper_step_self <$>
44 List.reverse (zipper_path z)
46 -- * Type 'Zipper_Step'
50 { zipper_step_prec :: TreeMap k a
51 , zipper_step_self :: (k, TreeMap.Node k a)
52 , zipper_step_foll :: TreeMap k a
53 } deriving (Data, Eq, Show, Typeable)
57 -- | Collect all 'Zipper's along a given axis,
58 -- including the first 'Zipper'.
59 zipper_collect :: (z -> Maybe z) -> z -> [z]
60 zipper_collect f z = z : maybe [] (zipper_collect f) (f z)
62 -- | Collect all 'Zipper's along a given axis,
63 -- excluding the first 'Zipper'.
64 zipper_collect_without_self :: (z -> Maybe z) -> z -> [z]
65 zipper_collect_without_self f z = maybe [] (zipper_collect f) (f z)
69 zipper_self :: Zipper k a -> TreeMap.Node k a
73 Zipper_Step{zipper_step_self=(_, nod)}
75 _ -> TreeMap.node_empty
79 zipper_child :: Ord k => Zipper k a -> [Zipper k a]
81 maybeToList (zipper_child_first z)
82 >>= zipper_collect zipper_foll
85 :: (Ord k, Alternative f)
86 => k -> Zipper k a -> f (Zipper k a)
87 zipper_child_lookup k (Zipper path (TreeMap m)) =
88 case Map.splitLookup k m of
89 (_, Nothing, _) -> empty
92 { zipper_path = Zipper_Step (TreeMap ps) (k, s) (TreeMap fs) : path
93 , zipper_curr = TreeMap.node_descendants s
96 zipper_child_first :: Alternative f => Zipper k a -> f (Zipper k a)
97 zipper_child_first (Zipper path (TreeMap m)) =
98 case Map.minViewWithKey m of
100 Just ((k', s'), fs') ->
102 { zipper_path = Zipper_Step TreeMap.empty (k', s') (TreeMap fs') : path
103 , zipper_curr = TreeMap.node_descendants s'
106 zipper_child_last :: Alternative f => Zipper k a -> f (Zipper k a)
107 zipper_child_last (Zipper path (TreeMap m)) =
108 case Map.maxViewWithKey m of
110 Just ((k', s'), ps') ->
112 { zipper_path = Zipper_Step (TreeMap ps') (k', s') TreeMap.empty : path
113 , zipper_curr = TreeMap.node_descendants s'
118 zipper_ancestor :: Ord k => Zipper k a -> [Zipper k a]
119 zipper_ancestor = zipper_collect_without_self zipper_parent
121 zipper_ancestor_or_self :: Ord k => Zipper k a -> [Zipper k a]
122 zipper_ancestor_or_self = zipper_collect zipper_parent
124 -- ** Axis descendant
126 zipper_descendant_or_self :: Ord k => Zipper k a -> [Zipper k a]
127 zipper_descendant_or_self =
130 collect_child acc z =
133 (zipper_child_first z)
141 zipper_descendant_or_self_reverse :: Ord k => Zipper k a -> [Zipper k a]
142 zipper_descendant_or_self_reverse z =
144 zipper_descendant_or_self_reverse
145 (List.reverse $ zipper_child z)
147 zipper_descendant :: Ord k => Zipper k a -> [Zipper k a]
148 zipper_descendant = List.tail . zipper_descendant_or_self
150 zipper_descendant_lookup
151 :: (Ord k, Alternative f, Monad f)
152 => TreeMap.Path k -> Zipper k a -> f (Zipper k a)
153 zipper_descendant_lookup (k:|ks) =
155 [] -> zipper_child_lookup k
156 k':ks' -> zipper_child_lookup k >=> zipper_descendant_lookup (k':|ks')
160 zipper_prec :: (Ord k, Alternative f) => Zipper k a -> f (Zipper k a)
161 zipper_prec (Zipper path _curr) =
164 Zipper_Step (TreeMap ps) (k, s) (TreeMap fs):steps ->
165 case Map.maxViewWithKey ps of
167 Just ((k', s'), ps') ->
169 { zipper_path = Zipper_Step (TreeMap ps')
171 (TreeMap $ Map.insert k s fs)
173 , zipper_curr = TreeMap.node_descendants s'
176 zipper_preceding :: Ord k => Zipper k a -> [Zipper k a]
178 zipper_ancestor_or_self >=>
179 zipper_preceding_sibling >=>
180 zipper_descendant_or_self_reverse
182 zipper_preceding_sibling :: Ord k => Zipper k a -> [Zipper k a]
183 zipper_preceding_sibling = zipper_collect_without_self zipper_prec
187 zipper_foll :: (Ord k, Alternative f) => Zipper k a -> f (Zipper k a)
188 zipper_foll (Zipper path _curr) =
191 Zipper_Step (TreeMap ps) (k, s) (TreeMap fs):steps ->
192 case Map.minViewWithKey fs of
194 Just ((k', s'), fs') ->
196 { zipper_path = Zipper_Step (TreeMap $ Map.insert k s ps)
200 , zipper_curr = TreeMap.node_descendants s'
203 zipper_following :: Ord k => Zipper k a -> [Zipper k a]
205 zipper_ancestor_or_self >=>
206 zipper_following_sibling >=>
207 zipper_descendant_or_self
209 zipper_following_sibling :: Ord k => Zipper k a -> [Zipper k a]
210 zipper_following_sibling = zipper_collect_without_self zipper_foll
214 zipper_parent :: (Ord k, Alternative f) => Zipper k a -> f (Zipper k a)
215 zipper_parent (Zipper path curr) =
218 Zipper_Step (TreeMap ps) (k, s) (TreeMap fs):steps ->
219 let nod = TreeMap.node (TreeMap.node_value s) curr in
221 { zipper_path = steps
222 , zipper_curr = TreeMap $ Map.union ps $ Map.insert k nod fs
228 :: (Zipper k a -> [Zipper k a])
229 -> (Zipper k a -> Bool)
230 -> (Zipper k a -> [Zipper k a])
231 zipper_filter axis p z = List.filter p (axis z)
232 infixl 5 `zipper_filter`
234 zipper_at :: Alternative f
235 => (Zipper k a -> [Zipper k a]) -> Int
236 -> (Zipper k a -> f (Zipper k a))
237 zipper_at axis n z = case List.drop n (axis z) of {[] -> empty; a:_ -> pure a}
241 :: (Zipper k a -> [Zipper k a])
242 -> Zipper k a -> Bool
243 zipper_null axis = List.null . axis