0 | module Data.CT.DependentPara.Instances
3 | import Data.CT.Category.Definition
4 | import Data.CT.Functor.Definition
5 | import Data.CT.DependentAction.Definition
6 | import Data.CT.DependentPara.Definition
7 | import Data.CT.Category.Instances
8 | import Data.CT.Functor.Instances
9 | import Data.CT.DependentAction.Instances
11 | import Data.Container.Base
12 | import Data.Container.Additive
36 | public export infixr 1 -\->
37 | public export infixr 1 -\-->
40 | public export infixr 1 =\\=>
45 | public export infixr 10 \>>
48 | namespace ParametricFunctions
51 | Para : (a, b : Type) -> Type
52 | Para = DepParaMor PairType
58 | (-\-->) : (a, b : Type) -> Type
59 | a -\--> b = Para a b
64 | DPara : (a, b : Type) -> Type
65 | DPara = DepParaMor DPairType
70 | (-\->) : (a, b : Type) -> Type
71 | a -\-> b = DPara a b
74 | trivialParam : (a -> b) -> a -\-> b
75 | trivialParam f = MkPara
81 | id = trivialParam id
84 | composePara : a -\-> b -> b -\-> c -> a -\-> c
85 | composePara (MkPara p f) (MkPara q g) = MkPara
86 | (\x => DPair (p x) (\p' => q (f (
x ** p')
)) )
87 | (\(
x ** (
p' ** q'))
=> g (
f (
x ** p')
** q')
)
90 | composeParallel : a -\-> b -> c -\-> d -> (a, c) -\-> (b, d)
91 | composeParallel (MkPara p f) (MkPara q g) = MkPara
92 | (\(x, y) => (p x, q y))
93 | (\((x, y) ** (px, qy)) => (f (
x ** px)
, g (
y ** qy)
))
96 | (\>>) : a -\-> b -> b -\-> c -> a -\-> c
100 | reparam : (pf : a -\-> b) ->
102 | (r : (x : a) -> q x -> pf.Param x) ->
104 | reparam (MkPara p f) r = MkPara q (\(
x ** qq)
=> f (
x ** (r x qq))
)
107 | Param : DPara a b -> a -> Type
108 | Param = DepParaMor.Param
111 | Run : (pf : DPara a b) -> (x : a) -> Param pf x -> b
112 | Run pf = DPair.curry (DepParaMor.Run pf)
115 | data IsNotDependent : DPara a b -> Type where
116 | MkNonDep : (p : Type) -> (f : DPair a (const p) -> b) ->
117 | IsNotDependent (MkPara (\_ => p) f)
120 | GetNonDep : (pf : DPara a b) ->
121 | IsNotDependent pf => (p : Type ** DPair a (const p) -> b)
122 | GetNonDep _ @{MkNonDep p f} = (
p ** f)
126 | GetParam : (pf : DPara a b) ->
127 | IsNotDependent pf => Type
128 | GetParam _ @{MkNonDep p f} = p
131 | composeNTimes : Nat -> a -\-> a -> a -\-> a
132 | composeNTimes 0 f = id
133 | composeNTimes 1 f = f
134 | composeNTimes (S k) f = composePara f (composeNTimes k f)
137 | binaryOpToPara : {p : Type} -> (f : (a, p) -> b) -> a -\-> b
138 | binaryOpToPara f = MkPara
140 | (\(
x ** p')
=> f (x, p'))
144 | namespace ParametricLenses
154 | record ParaAddLens (a, b : AddCont) where
157 | Run : a >*< Param =%+> b
164 | (=\\=>) : (a, b : AddCont) -> Type
165 | a =\\=> b = ParaAddLens a b
170 | toDepPara : ParaAddLens a b -> DepParaMor PairAddCont a b
171 | toDepPara (MkPara p f) = MkPara p f
174 | fromDepPara : DepParaMor PairAddCont a b -> ParaAddLens a b
175 | fromDepPara (MkPara p f) = MkPara p f
179 | trivialParam : a =%+> b -> a =\\=> b
180 | trivialParam f = MkPara
183 | let (
y ** ky)
= (%!+) f x
184 | in (
y ** \y' => (ky y', ()))
)
187 | binaryOpToPara : {p : AddCont} ->
188 | (a >*< p) =%+> b -> a =\\=> b
189 | binaryOpToPara f = MkPara p f
193 | id = trivialParam id
196 | toHomRepresentation : (f : ParaAddLens a b) ->
197 | (Param f) =%+> InternalLensAdditive a b
198 | toHomRepresentation (MkPara pType f) = !%+ \p =>
199 | (
!%+ \a => (
f.fwd (a, p) ** \b' => fst (f.bwd (a, p) b'))
**
200 | \l => foldr (\(
a ** b')
=> pType.Plus p (snd (f.bwd (a, p) b'))) (pType.Zero p) l)
203 | composePara : a =\\=> b -> b =\\=> c -> a =\\=> c
204 | composePara f g = MkPara
205 | (Param f >*< Param g)
206 | (assocR %+>> (Run f >*< id) %+>> Run g)
209 | composeParallel : a =\\=> b -> c =\\=> d -> (a >*< c) =\\=> (b >*< d)
210 | composeParallel f g = MkPara
211 | (Param f >*< Param g)
212 | (swapMiddle %+>> (Run f >*< Run g))
216 | postcomposeLens : a =\\=> b -> b =%+> c -> a =\\=> c
217 | postcomposeLens f g = MkPara (Param f) (Run f %+>> g)
220 | reparam : {q : AddCont} ->
224 | reparam f r = MkPara q $
(id >*< r) %+>> Run f
227 | namespace DependentParametricLenses
232 | record DParaAddLens (a, b : AddCont) where
234 | Param : a.Shp -> AddCont
235 | Run : DPair a Param =%+> b
239 | toDepPara : DParaAddLens a b -> DepParaMor DPairAddCont a b
240 | toDepPara (MkPara p f) = MkPara p f
243 | fromDepPara : DepParaMor DPairAddCont a b -> DParaAddLens a b
244 | fromDepPara (MkPara p f) = MkPara p f