1 | module Deriving.SpecialiseData.Codegen.Cast
3 | import Deriving.SpecialiseData.Common
4 | import Deriving.SpecialiseData.Helpers
5 | import Deriving.SpecialiseData.Task
6 | import Deriving.SpecialiseData.TaskFormation
7 | import Deriving.SpecialiseData.Unification
8 | import Deriving.SpecialiseData.Utils.Quantifiers
9 | import Deriving.SpecialiseData.Utils.TypeInfo
10 | import Language.Reflection.Compat
11 | import Language.Reflection.Expr
12 | import Language.Reflection.Expr.VarSubst
18 | transMachineVars : TTImp -> TTImp
19 | transMachineVars $
IVar fc n@(MN ns nn) = IVar fc $
fromString "MS^\{show ns}^\{show nn}"
20 | transMachineVars $
IBindVar fc n@(MN ns nn) = IBindVar fc $
fromString "MS^\{show ns}^\{show nn}"
21 | transMachineVars t = t
23 | parameters (t : SpecTask)
27 | UnificationResult ->
29 | (0 _ : AllTyArgsNamed mt) =>
31 | (0 _ : ConArgsNamed pCon) =>
33 | (0 _ : ConArgsNamed mCon) =>
37 | mkMToPImplClause ur _ con mcon meta _ =
38 | mapClause transMachineVars $
41 | (substituteVariables
42 | (fromList $
argsToBindMap mcon.args) <$> ur.fullResult)
43 | .= (substituteVariables meta.mToPRenames $
con.apply var ur.fullResult)
50 | (0 _ : AllTyArgsNamed mt) =>
53 | mkMToPImplDecls urs mt meta = do
54 | let clauses = map2UConsN t mkMToPImplClause urs mt meta
55 | [ def "mToPImpl" clauses
60 | mkMToPSig : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => TTImp
61 | mkMToPSig mt = forallMTArgs t `(Cast
~(t.specInvocation) ~(t.fullInvocation))
65 | mkMToPDecls : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => List Decl
67 | [ def "mToP" [ (var "mToP") .= `(MkCast mToPImpl
)]
75 | tupleOfN : Nat -> TTImp -> TTImp
76 | tupleOfN 0 _ = `(MkUnit
)
78 | tupleOfN (S n) t = `(MkPair
~t ~(tupleOfN n t))
81 | tupleOf : List TTImp -> TTImp
82 | tupleOf [] = `(MkUnit
)
84 | tupleOf (x :: xs) = `(MkPair
~x ~(tupleOf xs))
86 | parameters (t : SpecTask)
88 | recCastInj : Name -> Name -> TTImp
89 | recCastInj p1 p2 = `(~(var $
inGenNS t $
"castInjImpl") $ trans
~(var p1) $ sym
~(var p2))
92 | mkArgWithClause : Name -> TTImp -> Clause -> Clause
93 | mkArgWithClause argName existingLhs inner = do
94 | let mToPImpl = var $
inGenNS t "mToPImpl"
95 | let lhsArg = var $
fromString "lhs^\{argName}"
96 | let rhsArg = var $
fromString "rhs^\{argName}"
97 | let p1 = Just (MW, fromString "\{argName}^p1")
98 | let p2 = Just (MW, fromString "\{argName}^p2")
99 | withClause existingLhs MW `(~mToPImpl ~lhsArg) p1 [] [
100 | withClause `(~existingLhs | _) MW `(~mToPImpl ~rhsArg) p2 [] [inner]
104 | withManyUnders : Nat -> TTImp -> TTImp
105 | withManyUnders 0 x = x
106 | withManyUnders (S n) x = withManyUnders n `(~x | _)
109 | mkFinalClause : (con : Con) -> (0 _ : ConArgsNamed con) => ConMeta -> Clause
110 | mkFinalClause con meta = do
111 | let emptyCon = con.apply (\_ => `(_)) empty
112 | let recArgAmount = countRecursiveArgs meta
113 | let initialLhs = withManyUnders (2 * recArgAmount) $
114 | var "castInjImpl" .! ("castInj^x", emptyCon) .! ("castInj^y", emptyCon) .$ var "Refl"
115 | let recNames = recursiveArgNames con meta
116 | let recFns = (\n => recCastInj (fromString "\{n}^p1") (fromString "\{n}^p2")) <$> recNames
117 | withClause initialLhs MW (tupleOf recFns) Nothing [] [
118 | `(~initialLhs | ~(tupleOfN recArgAmount `(Refl
))) .= `(Refl
)
122 | mkInitialLhs : Con -> ConMeta -> TTImp
123 | mkInitialLhs con meta = do
124 | let lhsCon = bindConRecArgsAliased con meta $
prependS "lhs^"
125 | let rhsCon = bindConRecArgsAliased con meta $
prependS "rhs^"
126 | var "castInjImpl" .! ("castInj^x", lhsCon) .! ("castInj^y", rhsCon) .$ bindVar "prf"
129 | mkRecArgClauses : List Name -> TTImp -> Clause -> Clause
130 | mkRecArgClauses [] exLhs inner = inner
131 | mkRecArgClauses (x :: xs) exLhs inner = mkArgWithClause x exLhs $
mkRecArgClauses xs `(~exLhs | _ | _) inner
135 | UnificationResult ->
137 | (0 _ : AllTyArgsNamed mt) =>
139 | (0 cn : ConArgsNamed con) =>
141 | (0 mcn : ConArgsNamed mcon) =>
145 | mkCastInjClause ur mt _ con meta n = do
146 | if not (hasRecursiveArgs meta)
148 | let emptyCon = con.apply (\_ => `(_)) empty
149 | (var "castInjImpl") .! ("castInj^x", emptyCon) .! ("castInj^y", emptyCon) .$ `(Refl
) .= `(Refl
)
151 | let finalClause = mkFinalClause con meta
152 | let recNames = recursiveArgNames con meta
153 | let initLhs = mkInitialLhs con meta
154 | mkRecArgClauses recNames initLhs finalClause
161 | (0 mtp : AllTyArgsNamed mt) =>
164 | mkCastInjDecls ur ti meta = do
165 | let xVar = "castInj^x"
166 | let yVar = "castInj^y"
167 | let mToPVar = var $
inGenNS t "mToP"
168 | let mToPImplVar = applyMTArgs t $
var $
inGenNS t "mToPImpl"
169 | let arg1 = MkArg MW ImplicitArg (Just xVar) $
171 | let arg2 = MkArg MW ImplicitArg (Just yVar) $
174 | `((~(mToPImplVar .$ var xVar)
176 | ~(mToPImplVar .$ var yVar)) ->
177 | ~(var xVar) ~=~
~(var yVar))
178 | let castInjImplClauses = map2UConsN t mkCastInjClause ur ti meta
179 | [ claim M0 Public [] "castInjImpl" $
forallMTArgs t $
pi arg1 $
pi arg2 $
eqs
180 | , def "castInjImpl" castInjImplClauses
181 | , claim M0 Public [Hint False] "castInj" $
forallMTArgs t $
182 | `(Injective
~(mToPImplVar))
183 | , def "castInj" $
singleton $
184 | `(castInj
) .= `(MkInjective castInjImpl
)
192 | parameters (t : SpecTask)
198 | (0 _ : AllTyArgsNamed mt) =>
200 | mkPToMImplSig _ mt =
201 | forallMTArgs t $
arg t.fullInvocation .-> t.specInvocation
206 | UnificationResult ->
208 | (0 _ : AllTyArgsNamed mt) =>
210 | (0 _ : ConArgsNamed pCon) =>
212 | (0 _ : ConArgsNamed mCon) =>
216 | mkPToMImplClause ur _ con mcon meta _ =
217 | mapClause transMachineVars $
218 | var "pToMImpl" .$ con.apply bindVar
219 | (substituteVariables
220 | (fromList $
argsToBindMap $
con.args) <$> ur.fullResult)
221 | .= (substituteVariables meta.pToMRenames $
mcon.apply var ur.fullResult)
228 | (0 _ : AllTyArgsNamed mt) =>
231 | mkPToMImplDecls urs mt meta = do
232 | let clauses = map2UConsN t mkPToMImplClause urs mt meta
233 | [ def "pToMImpl" clauses
238 | mkPToMSig : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => TTImp
240 | forallMTArgs t $
`(Cast
~(t.fullInvocation) ~(t.specInvocation))
244 | mkPToMDecls : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => List Decl
246 | [ def "pToM" [ (var "pToM") .= `(MkCast pToMImpl
)]