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 \>>
47 | public export infixr 10 &>>
50 | namespace ParametricFunctions
53 | Para : (a, b : Type) -> Type
54 | Para = DepParaMor PairType
60 | (-\-->) : (a, b : Type) -> Type
61 | a -\--> b = Para a b
66 | DPara : (a, b : Type) -> Type
67 | DPara = DepParaMor DPairType
72 | (-\->) : (a, b : Type) -> Type
73 | a -\-> b = DPara a b
76 | trivialParam : (a -> b) -> a -\-> b
77 | trivialParam f = MkPara
83 | id = trivialParam id
86 | composePara : a -\-> b -> b -\-> c -> a -\-> c
87 | composePara (MkPara p f) (MkPara q g) = MkPara
88 | (\x => DPair (p x) (\p' => q (f (
x ** p')
)) )
89 | (\(
x ** (
p' ** q'))
=> g (
f (
x ** p')
** q')
)
92 | composeParallel : a -\-> b -> c -\-> d -> (a, c) -\-> (b, d)
93 | composeParallel (MkPara p f) (MkPara q g) = MkPara
94 | (\(x, y) => (p x, q y))
95 | (\((x, y) ** (px, qy)) => (f (
x ** px)
, g (
y ** qy)
))
98 | (\>>) : a -\-> b -> b -\-> c -> a -\-> c
102 | reparam : (pf : a -\-> b) ->
104 | (r : (x : a) -> q x -> pf.Param x) ->
106 | reparam (MkPara p f) r = MkPara q (\(
x ** qq)
=> f (
x ** (r x qq))
)
109 | Param : DPara a b -> a -> Type
110 | Param = DepParaMor.Param
113 | Run : (pf : DPara a b) -> (x : a) -> Param pf x -> b
114 | Run pf = DPair.curry (DepParaMor.Run pf)
117 | data IsNotDependent : DPara a b -> Type where
118 | MkNonDep : (p : Type) -> (f : DPair a (const p) -> b) ->
119 | IsNotDependent (MkPara (\_ => p) f)
122 | GetNonDep : (pf : DPara a b) ->
123 | IsNotDependent pf => (p : Type ** DPair a (const p) -> b)
124 | GetNonDep _ @{MkNonDep p f} = (
p ** f)
128 | GetParam : (pf : DPara a b) ->
129 | IsNotDependent pf => Type
130 | GetParam _ @{MkNonDep p f} = p
133 | composeNTimes : Nat -> a -\-> a -> a -\-> a
134 | composeNTimes 0 f = id
135 | composeNTimes 1 f = f
136 | composeNTimes (S k) f = composePara f (composeNTimes k f)
139 | binaryOpToPara : {p : Type} -> (f : (a, p) -> b) -> a -\-> b
140 | binaryOpToPara f = MkPara
142 | (\(
x ** p')
=> f (x, p'))
146 | namespace ParametricLenses
156 | record ParaAddLens (a, b : AddCont) where
159 | Run : (a >*< Param) =%+> b
166 | (=\\=>) : (a, b : AddCont) -> Type
167 | a =\\=> b = ParaAddLens a b
172 | toDepPara : ParaAddLens a b -> DepParaMor PairAddCont a b
173 | toDepPara (MkPara p f) = MkPara p f
176 | fromDepPara : DepParaMor PairAddCont a b -> ParaAddLens a b
177 | fromDepPara (MkPara p f) = MkPara p f
181 | trivialParam : a =%+> b -> a =\\=> b
182 | trivialParam f = MkPara
185 | let (
y ** ky)
= (%!+) f x
186 | in (
y ** \y' => (ky y', ()))
)
189 | binaryOpToPara : {p : AddCont} ->
190 | (a >*< p) =%+> b -> a =\\=> b
191 | binaryOpToPara f = MkPara p f
195 | id = trivialParam id
198 | toHomRepresentation : (f : ParaAddLens a b) ->
199 | (Param f) =%+> InternalLensAdditive a b
200 | toHomRepresentation (MkPara pType f) = !%+ \p =>
201 | (
!%+ \a => (
f.fwd (a, p) ** \b' => fst (f.bwd (a, p) b'))
**
202 | \l => foldr (\(
a ** b')
=> pType.Plus p (snd (f.bwd (a, p) b'))) (pType.Zero p) l)
205 | composePara : a =\\=> b -> b =\\=> c -> a =\\=> c
206 | composePara f g = MkPara
207 | (Param f >*< Param g)
208 | (assocR %+>> (Run f >*< id) %+>> Run g)
211 | composeParallel : a =\\=> b -> c =\\=> d -> (a >*< c) =\\=> (b >*< d)
212 | composeParallel f g = MkPara
213 | (Param f >*< Param g)
214 | (swapMiddle %+>> (Run f >*< Run g))
218 | postcomposeLens : a =\\=> b -> b =%+> c -> a =\\=> c
219 | postcomposeLens f g = MkPara (Param f) (Run f %+>> g)
222 | namespace DependentParametricLenses
227 | record DParaAddLens (a, b : AddCont) where
229 | Param : a.Shp -> AddCont
230 | Run : DPair a Param =%+> b
234 | toDepPara : DParaAddLens a b -> DepParaMor DPairAddCont a b
235 | toDepPara (MkPara p f) = MkPara p f
238 | fromDepPara : DepParaMor DPairAddCont a b -> DParaAddLens a b
239 | fromDepPara (MkPara p f) = MkPara p f