0 | module Data.Container.Base.Monoid.Instances
2 | import Data.Container.Base.Object.Definition
3 | import Data.Container.Base.Extension.Definition
4 | import Data.Container.Base.Morphism.Definition
5 | import Data.Container.Base.Product.Definition
6 | import Data.Container.Base.Properties.Definition
8 | import Data.Container.Base.Object.Instances
9 | import Data.Container.Base.Extension.Instances
10 | import Data.Container.Base.Morphism.Instances
11 | import Data.Container.Base.Monoid.Definition
12 | import Data.Container.Base.Properties.Instances
14 | import Data.Container.Base.TreeUtils
16 | import Data.Fin.Split
18 | import Data.Functor.Algebra
23 | TensorMonoid Maybe where
24 | tensorN = toState True
25 | tensorM = !% \(b1, b2) => (
b1 && b2 ** \bb => case b1 of
26 | True => ((), if b2 then bb else absurd bb)
32 | TensorMonoid Either where
33 | tensorN = toState True
34 | tensorM = !% \(b1, b2) => (
b1 && b2 ** \() => ((), ()))
40 | TensorMonoid List where
42 | tensorM = !% \(n, m) => (
n * m ** splitFinProd DefaultLayoutOrder)
45 | SeqMonoid List where
46 | seqM = !% \(n <| contentM) => (
sum contentM ** splitFinProdDep contentM)
66 | IsNaperian c => TensorMonoid c where
67 | tensorN @{(MkIsNaperian _)} = toState ()
68 | tensorM @{(MkIsNaperian _)} = !% \((), ()) => (
() ** \i => (i, i))
74 | IsNaperian c => SeqMonoid c where
75 | seqM @{MkIsNaperian pos} = compToTensor {d=c} %>> tensorM
79 | diagonalAroundMiddle : IsNaperian c =>
80 | (f : c >@ c =%> c) ->
81 | c >@ d >@ c =%> c >@ d
85 | join : SeqMonoid c =>
86 | Tensor [c, c] =%> Tensor [c]
87 | join = (id >@ rightUnit)
92 | cojoin : SeqComonoid c =>
93 | Tensor [c] =%> Tensor [c, c]
96 | %>> (id >@ rightUnitInv)
99 | diagonal : IsNaperian c =>
100 | Tensor [c, c] =%> Tensor [c]
104 | codiagonal : TensorMonoid c =>
105 | Tensor [c] =%> Tensor [c, c]
106 | codiagonal = ?cojoinn
109 | namespace BinTreeUtils
111 | pairBTreeShapes : BinTreeShape -> BinTreeShape -> BinTreeShape
112 | pairBTreeShapes LeafS LeafS
114 | pairBTreeShapes LeafS (NodeS ltb rtb)
115 | = NodeS (pairBTreeShapes LeafS ltb) (pairBTreeShapes LeafS rtb)
116 | pairBTreeShapes (NodeS lta rta) LeafS
117 | = NodeS (pairBTreeShapes lta LeafS) (pairBTreeShapes rta LeafS)
118 | pairBTreeShapes (NodeS lta rta) (NodeS ltb rtb)
119 | = NodeS (pairBTreeShapes lta ltb) (pairBTreeShapes rta rtb)
124 | pairBTreePos : {sh1, sh2 : BinTreeShape} ->
125 | BinTreePos (pairBTreeShapes sh1 sh2) -> (BinTreePos sh1, BinTreePos sh2)
126 | pairBTreePos {sh1 = LeafS, sh2 = LeafS} AtLeaf
128 | pairBTreePos {sh1 = LeafS, sh2 = (NodeS ltb rtb)} p
129 | = (AtLeaf, case p of
131 | GoLeft posL => GoLeft $
snd (pairBTreePos posL)
132 | GoRight posR => GoRight $
snd (pairBTreePos posR))
133 | pairBTreePos {sh1 = (NodeS lta rta), sh2 = LeafS} p
136 | GoLeft posL => GoLeft $
fst (pairBTreePos posL)
137 | GoRight posR => GoRight $
fst (pairBTreePos posR), AtLeaf)
138 | pairBTreePos {sh1 = (NodeS lta rta), sh2 = (NodeS ltb rtb)} p
140 | AtNode => (AtNode, AtNode)
141 | GoLeft posL => let (pl, pr) = pairBTreePos posL
142 | in (GoLeft $
pl, GoLeft $
pr)
143 | GoRight posR => let (pl, pr) = pairBTreePos posR
144 | in (GoRight $
pl, GoRight $
pr)
147 | pairBTreeLeafPos : {sh1, sh2 : BinTreeShape} ->
148 | BinTreePosLeaf (pairBTreeShapes sh1 sh2) ->
149 | (BinTreePosLeaf sh1, BinTreePosLeaf sh2)
150 | pairBTreeLeafPos {sh1 = LeafS, sh2 = LeafS} AtLeaf
152 | pairBTreeLeafPos {sh1 = LeafS, sh2 = (NodeS ltb rtb)} p
153 | = (AtLeaf, case p of
154 | GoLeft posL => GoLeft $
snd (pairBTreeLeafPos posL)
155 | GoRight posR => GoRight $
snd (pairBTreeLeafPos posR))
156 | pairBTreeLeafPos {sh1 = (NodeS lta rta), sh2 = LeafS} p
158 | GoLeft posL => GoLeft $
fst (pairBTreeLeafPos posL)
159 | GoRight posR => GoRight $
fst (pairBTreeLeafPos posR), AtLeaf)
160 | pairBTreeLeafPos {sh1 = (NodeS lta rta), sh2 = (NodeS ltb rtb)} p
162 | GoLeft posL => let (pl, pr) = pairBTreeLeafPos posL
163 | in (GoLeft $
pl, GoLeft $
pr)
164 | GoRight posR => let (pl, pr) = pairBTreeLeafPos posR
165 | in (GoRight $
pl, GoRight $
pr)
168 | TensorMonoid BinTree where
169 | tensorN = toState LeafS
170 | tensorM = !% \(sh1, sh2) => (
pairBTreeShapes sh1 sh2 ** pairBTreePos)
173 | TensorMonoid BinTreeLeaf where
174 | tensorN = toState LeafS
175 | tensorM = !% \(sh1, sh2) => (
pairBTreeShapes sh1 sh2 ** pairBTreeLeafPos)
180 | reduce : {c : Cont} -> Algebra (Ext c) a =>
181 | Ext c a -> Ext Scalar a
182 | reduce x = () <| \() => reduce x
185 | dotWith : {cont : Cont} -> TensorMonoid cont => Algebra (Ext cont) c =>
187 | Ext cont a -> Ext cont b -> Ext Scalar c
188 | dotWith f ea eb = reduce $
uncurry f <$> liftA2Ext ea eb