0 | module Data.Container.Base.Concrete.Instances
7 | import Data.Container.Base.Object.Definition
8 | import Data.Container.Base.Extension.Definition
9 | import Data.Container.Base.Concrete.Definition
10 | import Data.Container.Base.Object.Instances
11 | import Data.Container.Base.Extension.Instances
13 | import Data.Container.Base.TreeUtils
16 | import public Data.Tree
20 | %hide Data.Vect.fromList
22 | namespace ConversionFunctions
24 | toScalar : a -> Scalar' a
25 | toScalar a = () <| (\_ => a)
28 | extract : Scalar' a -> a
29 | extract (() <| f) = f ()
32 | fromMaybe : Maybe a -> Maybe' a
33 | fromMaybe Nothing = (False <| absurd)
34 | fromMaybe (Just a) = (True <| \_ => a)
37 | toMaybe : Maybe' a -> Maybe a
38 | toMaybe (False <| absurd) = Nothing
39 | toMaybe (True <| f) = Just (f ())
42 | fromList : List a -> List' a
43 | fromList [] = (0 <| absurd)
44 | fromList (x :: xs) = let (l <| c) = fromList xs
45 | in (S l <| cons x c)
48 | toList : List' a -> List a
49 | toList (0 <| _) = []
50 | toList ((S k) <| ind) = head ind :: toList (k <| tail ind)
53 | fromVect : Vect n a -> Vect' n a
54 | fromVect v = () <| \i => index i v
57 | toVect : {n : Nat} -> Vect' n a -> Vect n a
58 | toVect (_ <| index) = Vect.Fin.tabulate index
61 | fromBinTreeSame : BinTreeSame a -> BinTree' a
62 | fromBinTreeSame (Leaf x) = LeafS <| \_ => x
63 | fromBinTreeSame (Node x lt rt) =
64 | let fblt = fromBinTreeSame lt
65 | fbrt = fromBinTreeSame rt
66 | in NodeS (shapeExt fblt) (shapeExt fbrt) <| \case
68 | GoLeft posL => index fblt posL
69 | GoRight posR => index fbrt posR
72 | toBinTreeSame : BinTree' a -> BinTreeSame a
73 | toBinTreeSame (LeafS <| index) = Leaf (index AtLeaf)
74 | toBinTreeSame (NodeS lt rt <| index) =
76 | (toBinTreeSame (lt <| index . GoLeft))
77 | (toBinTreeSame (rt <| index . GoRight))
81 | fromTreeHelper : BinTreePosNode LeafS -> a
82 | fromTreeHelper AtNode
impossible
83 | fromTreeHelper (GoLeft x
) impossible
84 | fromTreeHelper (GoRight x
) impossible
87 | fromBinTreeNode : BinTreeNode a -> BinTreeNode' a
88 | fromBinTreeNode (Leaf ()) = LeafS <| fromTreeHelper
89 | fromBinTreeNode (Node node leftTree rightTree)
90 | = let fblt = fromBinTreeNode leftTree
91 | fbrt = fromBinTreeNode rightTree
92 | in (NodeS (shapeExt fblt) (shapeExt fbrt) <| \case
94 | GoLeft posL => index fblt posL
95 | GoRight posR => index fbrt posR)
98 | toBinTreeNode : BinTreeNode' a -> BinTreeNode a
99 | toBinTreeNode (LeafS <| index) = Leaf ()
100 | toBinTreeNode (NodeS lt rt <| index) =
101 | Node (index AtNode)
102 | (toBinTreeNode (lt <| index . GoLeft))
103 | (toBinTreeNode (rt <| index . GoRight))
106 | fromBinTreeLeaf : BinTreeLeaf a -> BinTreeLeaf' a
107 | fromBinTreeLeaf (Leaf leaf) = LeafS <| \_ => leaf
108 | fromBinTreeLeaf (Node node lt rt) =
109 | let fblt = fromBinTreeLeaf lt
110 | fbrt = fromBinTreeLeaf rt
111 | in NodeS (shapeExt fblt) (shapeExt fbrt) <| \case
112 | GoLeft posL => index fblt posL
113 | GoRight posR => index fbrt posR
116 | toBinTreeLeaf : BinTreeLeaf' a -> BinTreeLeaf a
117 | toBinTreeLeaf (LeafS <| content) = Leaf (content AtLeaf)
118 | toBinTreeLeaf (NodeS l r <| content) =
119 | Node' (toBinTreeLeaf (l <| content . GoLeft))
120 | (toBinTreeLeaf (r <| content . GoRight))
135 | FromConcrete Scalar where
137 | concreteFunctor = MkFunctor id
138 | fromConcreteTy = pure
139 | toConcreteTy = extract
142 | FromConcrete Maybe where
143 | concreteType = Maybe
144 | concreteFunctor = %search
145 | fromConcreteTy = fromMaybe
146 | toConcreteTy = toMaybe
149 | FromConcrete List where
150 | concreteType = List
151 | concreteFunctor = %search
152 | fromConcreteTy = fromList
153 | toConcreteTy = toList
156 | {n : Nat} -> FromConcrete (Vect n) where
157 | concreteType = Vect n
158 | concreteFunctor = %search
159 | fromConcreteTy = fromVect
160 | toConcreteTy = toVect
163 | FromConcrete BinTree where
164 | concreteType = BinTreeSame
165 | concreteFunctor = %search
166 | fromConcreteTy = fromBinTreeSame
167 | toConcreteTy = toBinTreeSame
170 | FromConcrete BinTreeNode where
171 | concreteType = BinTreeNode
172 | concreteFunctor = %search
173 | fromConcreteTy = fromBinTreeNode
174 | toConcreteTy = toBinTreeNode
177 | FromConcrete BinTreeLeaf where
178 | concreteType = BinTreeLeaf
179 | concreteFunctor = %search
180 | fromConcreteTy = fromBinTreeLeaf
181 | toConcreteTy = toBinTreeLeaf