5 | module Data.Graph.Types
7 | import Data.AssocList
9 | import Data.Prim.Bits32
17 | export infixl 4 :>
, <>
33 | 0 prf : node2 > node1
36 | mkEdge : Node -> Node -> Maybe Edge
37 | mkEdge k j = case comp k j of
38 | LT p _ _ => Just $
MkEdge k j p
39 | GT _ _ p => Just $
MkEdge j k p
42 | public export %inline
43 | (<>) : (i,j : Node) -> (0 prf : j > i) => Edge
44 | i <> j = MkEdge i j prf
48 | MkEdge m1 n1 _ == MkEdge m2 n2 _ = m1 == m2 && n1 == n2
52 | compare (MkEdge m1 n1 _) (MkEdge m2 n2 _) =
53 | compare m1 m2 <+> compare n1 n2
57 | showPrec p (MkEdge n1 n2 _) =
58 | showCon p "\{show n1} <> \{show n2}" ""
66 | record LNode n where
72 | Eq a => Eq (LNode a) where
73 | MkLNode n1 l1 == MkLNode n2 l2 = n1 == n2 && l1 == l2
76 | Show n => Show (LNode n) where
77 | showPrec p (MkLNode k l) =
78 | showCon p "\{show k} :> \{show l}" ""
81 | public export %inline
82 | (:>) : Node -> n -> LNode n
85 | public export %inline
87 | map f ln = { label $= f } ln
89 | public export %inline
90 | Foldable LNode where
91 | foldl f a n = f a n.label
92 | foldr f a n = f n.label a
93 | foldMap f n = f n.label
95 | toList n = [n.label]
96 | foldlM f a n = f a n.label
98 | public export %inline
99 | Traversable LNode where
100 | traverse f (MkLNode n l) = MkLNode n <$> f l
104 | record LEdge e where
105 | constructor MkLEdge
110 | Eq a => Eq (LEdge a) where
111 | MkLEdge e1 l1 == MkLEdge e2 l2 = e1 == e2 && l1 == l2
114 | Ord a => Ord (LEdge a) where
115 | compare (MkLEdge e1 l1) (MkLEdge e2 l2) =
116 | compare e1 e2 <+> compare l1 l2
119 | Show e => Show (LEdge e) where
120 | showPrec p (MkLEdge ed l) =
121 | showCon p "\{show ed} :> \{show l}" ""
124 | public export %inline
125 | (:>) : Edge -> e -> LEdge e
128 | public export %inline
129 | Functor LEdge where
130 | map f le = { label $= f } le
132 | public export %inline
133 | Foldable LEdge where
134 | foldl f a n = f a n.label
135 | foldr f a n = f n.label a
136 | foldMap f n = f n.label
138 | toList n = [n.label]
139 | foldlM f a n = f a n.label
141 | public export %inline
142 | Traversable LEdge where
143 | traverse f (MkLEdge n l) = MkLEdge n <$> f l
158 | record Adj e n where
161 | neighbours : AssocList e
164 | Eq e => Eq n => Eq (Adj e n) where
165 | MkAdj l1 ns1 == MkAdj l2 ns2 = l1 == l2 && ns1 == ns2
168 | Show e => Show n => Show (Adj e n) where
169 | showPrec p (MkAdj lbl ns) = showCon p "MkAdj" $
showArg lbl ++ showArg ns
172 | Functor (Adj e) where
173 | map f adj = { label $= f } adj
176 | Foldable (Adj e) where
177 | foldl f a n = f a n.label
178 | foldr f a n = f n.label a
179 | foldMap f n = f n.label
181 | toList n = [n.label]
182 | foldlM f a n = f a n.label
184 | public export %inline
185 | Traversable (Adj e) where
186 | traverse f (MkAdj n l) = (`MkAdj` l) <$> f n
188 | namespace AdjFunctor
189 | [AdjEdge] Functor (`Adj` n) where
190 | map f = {neighbours $= map f}
192 | namespace AdjFoldable
193 | [AdjEdge] Foldable (`Adj` n) where
194 | foldl f a = foldl f a . neighbours
195 | foldr f a = foldr f a . neighbours
196 | foldMap f = foldMap f . neighbours
197 | null = null . neighbours
198 | toList = toList . neighbours
199 | foldlM f a = foldlM f a . neighbours
202 | Bifunctor Adj where
203 | bimap f g (MkAdj l es) = MkAdj (g l) (map f es)
204 | mapFst f (MkAdj l es) = MkAdj l (map f es)
205 | mapSnd g (MkAdj l es) = MkAdj (g l) es
208 | Bifoldable Adj where
209 | bifoldr f g acc (MkAdj l es) = foldr f (g l acc) es
210 | bifoldl f g acc (MkAdj l es) = g (foldl f acc es) l
214 | Bitraversable Adj where
215 | bitraverse f g (MkAdj l es) = [| MkAdj (g l) (traverse f es) |]
222 | record Context e n where
223 | constructor MkContext
226 | neighbours : AssocList e
229 | Eq e => Eq n => Eq (Context e n) where
230 | MkContext n1 l1 ns1 == MkContext n2 l2 ns2 =
231 | n1 == n2 && l1 == l2 && ns1 == ns2
234 | Show e => Show n => Show (Context e n) where
235 | showPrec p (MkContext n lbl ns) =
236 | showCon p "MkContext" $
showArg n ++ showArg lbl ++ showArg ns
239 | Functor (Context e) where
240 | map f c = { label $= f } c
243 | Foldable (Context e) where
244 | foldl f a n = f a n.label
245 | foldr f a n = f n.label a
246 | foldMap f n = f n.label
248 | toList n = [n.label]
249 | foldlM f a n = f a n.label
251 | namespace ContextFunctor
252 | [CtxtEdge] Functor (`Context` n) where
253 | map f = {neighbours $= map f}
255 | namespace ContextFoldable
256 | [CtxtEdge] Foldable (`Context` n) where
257 | foldl f a = foldl f a . neighbours
258 | foldr f a = foldr f a . neighbours
259 | foldMap f = foldMap f . neighbours
260 | null = null . neighbours
261 | toList = toList . neighbours
262 | foldlM f a = foldlM f a . neighbours
264 | public export %inline
265 | Traversable (Context e) where
266 | traverse f (MkContext n l es) =
267 | (\l2 => MkContext n l2 es) <$> f l
270 | Bifunctor Context where
271 | bimap f g (MkContext n l es) = MkContext n (g l) (map f es)
272 | mapFst f (MkContext n l es) = MkContext n l (map f es)
273 | mapSnd g (MkContext n l es) = MkContext n (g l) es
276 | Bifoldable Context where
277 | bifoldr f g acc (MkContext _ l es) = foldr f (g l acc) es
278 | bifoldl f g acc (MkContext _ l es) = g (foldl f acc es) l
282 | Bitraversable Context where
283 | bitraverse f g (MkContext n l es) =
284 | [| MkContext (pure n) (g l) (traverse f es) |]
292 | GraphRep : (e : Type) -> (n : Type) -> Type
293 | GraphRep e n = BitMap (Adj e n)
296 | record Graph e n where
297 | constructor MkGraph
298 | graph : GraphRep e n
301 | Eq e => Eq n => Eq (Graph e n) where
302 | MkGraph g1 == MkGraph g2 = g1 == g2
305 | Functor (Graph e) where
306 | map f g = { graph $= map (map f) } g
309 | Foldable (Graph e) where
310 | foldl f a = foldl (\a',adj => f a' adj.label) a . graph
311 | foldr f a = foldr (f . label) a . graph
312 | foldMap f = foldMap (f . label) . graph
313 | toList = map label . toList . graph
314 | null g = isEmpty $
graph g
316 | namespace GraphFunctor
318 | [OnEdge] Functor (`Graph` n) where
319 | map f g = { graph $= map {neighbours $= map f} } g
321 | namespace GraphFoldable
323 | [OnEdge] Foldable (`Graph` n) where
324 | foldr f a = foldr (\v,x => foldr f x v.neighbours) a . graph
325 | foldl f a = foldl (\x,v => foldl f x v.neighbours) a . graph
326 | foldMap f = foldMap (\v => foldMap f v.neighbours) . graph
327 | toList g = toList g.graph >>= toList . neighbours
328 | null g = all (null . neighbours) g.graph
330 | public export %inline
331 | Traversable (Graph e) where
332 | traverse f (MkGraph g) = MkGraph <$> traverse (traverse f) g
335 | Bifunctor Graph where
336 | bimap f g (MkGraph m) = MkGraph $
bimap f g <$> m
337 | mapFst f (MkGraph m) = MkGraph $
mapFst f <$> m
338 | mapSnd g (MkGraph m) = MkGraph $
mapSnd g <$> m
341 | Bifoldable Graph where
343 | foldr (flip $
bifoldr f g) acc . graph
345 | foldl (bifoldl f g) acc . graph
349 | Bitraversable Graph where
350 | bitraverse f g (MkGraph m) =
351 | [| MkGraph (traverse (bitraverse f g) m) |]
358 | data Decomp : (e : Type) -> (n : Type) -> Type where
359 | Split : (ctxt : Context e n) -> (gr : Graph e n) -> Decomp e n