0 | module Data.Container.Base.Extension.Instances
5 | import Data.Container.Base.Object.Definition
6 | import Data.Container.Base.Morphism.Definition
7 | import Data.Container.Base.Extension.Definition
8 | import Data.Container.Base.Product.Definition
10 | import Data.Container.Base.Object.Instances
11 | import Data.Container.Base.TreeUtils
14 | import Data.Fin.Split
19 | %hide Data.Vect.fromList
20 | %hide Prelude.toList
24 | namespace ExtensionsOfMainExamples
27 | Scalar' : Type -> Type
28 | Scalar' = Ext Scalar
32 | Pair' : Type -> Type
37 | Either' : Type -> Type
38 | Either' = Ext Either
42 | Maybe' : Type -> Type
47 | List' : Type -> Type
52 | Vect' : (n : Nat) -> Type -> Type
53 | Vect' n = Ext (Vect n)
57 | Stream' : Type -> Type
58 | Stream' = Ext Stream
62 | BinTree' : Type -> Type
63 | BinTree' = Ext BinTree
67 | BinTreeNode' : Type -> Type
68 | BinTreeNode' = Ext BinTreeNode
72 | BinTreeLeaf' : Type -> Type
73 | BinTreeLeaf' = Ext BinTreeLeaf
88 | composeExtensions : List Cont -> Type -> Type
89 | composeExtensions = foldr (\c, f => (Ext c) . f) (Ext Scalar)
92 | fixShape : {c : Cont} ->
94 | fixShape e = At c (shapeExt e)
98 | Pick : Cont -> Type -> Cont
99 | Pick c a = Const2 (Ext c a) a
101 | namespace ComposeExtensionsVect
103 | composeExtensions : Vect n Cont -> Type -> Type
104 | composeExtensions = foldr @{straightforward} (\c, f => (Ext c) . f) (Ext Scalar)
107 | [fe] {shape : List Cont} -> Functor (composeExtensions shape) where
108 | map {shape = []} f = map f
109 | map {shape = (s :: ss)} f = (map @{fe} f <$>)
116 | positionsCont : {0 c : Cont} -> {sh : c.Shp} -> Ext c (c.Pos sh)
117 | positionsCont = sh <| id
125 | fromList : List a -> List' a
126 | fromList [] = (0 <| absurd)
127 | fromList (x :: xs) = let (l <| c) = fromList xs
128 | in (S l <| cons x c)
131 | toList : List' a -> List a
132 | toList (0 <| _) = []
133 | toList l@((S k) <| ind) = head ind :: toList
134 | (assert_smaller l (k <| tail ind))
138 | fromVect : Vect n a -> Vect' n a
139 | fromVect v = () <| \i => index i v
142 | toVect : {n : Nat} -> Vect' n a -> Vect n a
143 | toVect (_ <| index) = Vect.Fin.tabulate index
145 | namespace BinTreeSame
147 | fromBinTreeSame : BinTreeSame a -> BinTree' a
148 | fromBinTreeSame (Leaf x) = LeafS <| \_ => x
149 | fromBinTreeSame (Node x lt rt) =
150 | let (fblt, fbrt) = (fromBinTreeSame lt, fromBinTreeSame rt)
151 | in NodeS (shapeExt fblt) (shapeExt fbrt) <| \case
153 | GoLeft posL => index fblt posL
154 | GoRight posR => index fbrt posR
157 | toBinTreeSame : BinTree' a -> BinTreeSame a
158 | toBinTreeSame (LeafS <| index) = Leaf (index AtLeaf)
159 | toBinTreeSame n@(NodeS lt rt <| index) =
160 | Node (index AtNode)
161 | (toBinTreeSame $
assert_smaller n (lt <| index . GoLeft))
162 | (toBinTreeSame $
assert_smaller n (rt <| index . GoRight))
164 | namespace BinTreeNode
166 | fromTreeHelper : BinTreePosNode LeafS -> a
167 | fromTreeHelper AtNode
impossible
168 | fromTreeHelper (GoLeft x
) impossible
169 | fromTreeHelper (GoRight x
) impossible
172 | fromBinTreeNode : BinTreeNode a -> BinTreeNode' a
173 | fromBinTreeNode (Leaf ()) = LeafS <| fromTreeHelper
174 | fromBinTreeNode (Node node leftTree rightTree)
175 | = let (fblt, fbrt) = (fromBinTreeNode leftTree, fromBinTreeNode rightTree)
176 | in (NodeS (shapeExt fblt) (shapeExt fbrt) <| \case
178 | GoLeft posL => index fblt posL
179 | GoRight posR => index fbrt posR)
182 | toBinTreeNode : BinTreeNode' a -> BinTreeNode a
183 | toBinTreeNode (LeafS <| index) = Leaf ()
184 | toBinTreeNode n@(NodeS lt rt <| index) =
185 | Node (index AtNode)
186 | (toBinTreeNode $
assert_smaller n (lt <| index . GoLeft))
187 | (toBinTreeNode $
assert_smaller n (rt <| index . GoRight))
189 | namespace BinTreeLeaf
191 | fromBinTreeLeaf : BinTreeLeaf a -> BinTreeLeaf' a
192 | fromBinTreeLeaf (Leaf leaf) = LeafS <| \_ => leaf
193 | fromBinTreeLeaf (Node node lt rt) =
194 | let (fblt, fbrt) = (fromBinTreeLeaf lt, fromBinTreeLeaf rt)
195 | in NodeS (shapeExt fblt) (shapeExt fbrt) <| \case
196 | GoLeft posL => index fblt posL
197 | GoRight posR => index fbrt posR
200 | toBinTreeLeaf : BinTreeLeaf' a -> BinTreeLeaf a
201 | toBinTreeLeaf (LeafS <| content) = Leaf (content AtLeaf)
202 | toBinTreeLeaf n@(NodeS l r <| content) =
203 | Node' (toBinTreeLeaf $
assert_smaller n (l <| content . GoLeft))
204 | (toBinTreeLeaf $
assert_smaller n (r <| content . GoRight))
210 | namespace VectInstances
212 | {n : Nat} -> Eq x => Eq (Vect' n x) where
213 | v == v' = (toVect v) == (toVect v')
223 | take : {n : Nat} ->
224 | (s : Fin (S n)) -> Vect' n a -> Vect' (finToNat s) a
225 | take s = fromVect . takeFin s . toVect
228 | (++) : {n : Nat} -> Vect' n a -> Vect' m a -> Vect' (n + m) a
229 | (++) v1 v2 = () <| \i => case splitSum i of
230 | Left i1 => index v1 i1
231 | Right i2 => index v2 i2
238 | namespace ListInstances
241 | Eq a => Eq (List' a) where
242 | l == l' = assert_total ((toList l) == (toList l'))
264 | namespace BinTreeInstances
267 | Eq a => Eq (BinTree' a) where
268 | t == t' = assert_total (toBinTreeSame t == toBinTreeSame t')
276 | namespace BinTreeLeafInstances
279 | Eq a => Eq (BinTreeLeaf' a) where
280 | t == t' = assert_total (toBinTreeLeaf t == toBinTreeLeaf t')
288 | namespace BinTreeNodeInstances
291 | Eq a => Eq (BinTreeNode' a) where
292 | t == t' = assert_total (toBinTreeNode t == toBinTreeNode t')