0 | ||| Logic for generating cast functions and proofs over them
  1 | module Deriving.SpecialiseData.Codegen.Cast
  2 |
  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
 13 |
 14 | ------------------------------------
 15 | --- SPEC TO POLY CAST DERIVATION ---
 16 | ------------------------------------
 17 |
 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
 22 |
 23 | parameters (t : SpecTask)
 24 |   ||| Generate specialised to polymorphic type conversion function clause
 25 |   ||| for given constructor
 26 |   mkMToPImplClause :
 27 |     UnificationResult ->
 28 |     (mt : TypeInfo) ->
 29 |     (0 _ : AllTyArgsNamed mt) =>
 30 |     (pCon : Con) ->
 31 |     (0 _ : ConArgsNamed pCon) =>
 32 |     (mCon : Con) ->
 33 |     (0 _ : ConArgsNamed mCon) =>
 34 |     ConMeta ->
 35 |     Nat ->
 36 |     Clause
 37 |   mkMToPImplClause ur _ con mcon meta _ =
 38 |     mapClause transMachineVars $
 39 |       var "mToPImpl" .$
 40 |         mcon.apply bindVar
 41 |           (substituteVariables
 42 |             (fromList $ argsToBindMap mcon.args) <$> ur.fullResult)
 43 |       .= (substituteVariables meta.mToPRenames $ con.apply var ur.fullResult)
 44 |
 45 |   ||| Generate specialised to polymorphic type conversion function declarations
 46 |   export
 47 |   mkMToPImplDecls :
 48 |     UniResults ->
 49 |     (mt : TypeInfo) ->
 50 |     (0 _ : AllTyArgsNamed mt) =>
 51 |     TypeMeta ->
 52 |     List Decl
 53 |   mkMToPImplDecls urs mt meta = do
 54 |     let clauses = map2UConsN t mkMToPImplClause urs mt meta
 55 |     [ def "mToPImpl" clauses
 56 |     ]
 57 |
 58 |   ||| Generate specialised to polymorphic cast signature
 59 |   export
 60 |   mkMToPSig : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => TTImp
 61 |   mkMToPSig mt = forallMTArgs t `(Cast ~(t.specInvocation) ~(t.fullInvocation))
 62 |
 63 |   ||| Generate specialised to polymorphic cast declarations
 64 |   export
 65 |   mkMToPDecls : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => List Decl
 66 |   mkMToPDecls mt =
 67 |     [ def "mToP" [ (var "mToP") .= `(MkCast mToPImpl)]
 68 |     ]
 69 |
 70 |   -----------------------------------
 71 |   --- CAST INJECTIVITY DERIVATION ---
 72 |   -----------------------------------
 73 |
 74 | ||| A tuple value of multiple repeating expressions
 75 | tupleOfN : Nat -> TTImp -> TTImp
 76 | tupleOfN 0 _ = `(MkUnit)
 77 | tupleOfN 1 t = t
 78 | tupleOfN (S n) t = `(MkPair ~t ~(tupleOfN n t))
 79 |
 80 | ||| Assemble a TTImp of a tuple from a list of `TTImp`s
 81 | tupleOf : List TTImp -> TTImp
 82 | tupleOf [] = `(MkUnit)
 83 | tupleOf [x] = x
 84 | tupleOf (x :: xs) = `(MkPair ~x ~(tupleOf xs))
 85 |
 86 | parameters (t : SpecTask)
 87 |   ||| Emit a recursive call to castInjImpl constructing the proof from given names
 88 |   recCastInj : Name -> Name -> TTImp
 89 |   recCastInj p1 p2 = `(~(var $ inGenNS t $ "castInjImpl") $ trans ~(var p1) $ sym ~(var p2))
 90 |
 91 |   ||| Generate a with-clause corresponding to a single recursive argument
 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]
101 |     ]
102 |
103 |   ||| Wrap a term into a number of `IAppWith`s with underscores
104 |   withManyUnders : Nat -> TTImp -> TTImp
105 |   withManyUnders 0 x = x
106 |   withManyUnders (S n) x = withManyUnders n `(~x | _)
107 |
108 |   ||| Generate a final with-clause that matches all equality proofs to `Refl`s
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)
119 |     ]
120 |
121 |   ||| Generate a left-hand-side for recursive argument with-clauses
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"
127 |
128 |   ||| Wrap a clause in with-clauses for all given names
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
132 |
133 |   ||| Derive a single cast injectivity clause
134 |   mkCastInjClause :
135 |     UnificationResult ->
136 |     (mt : TypeInfo) ->
137 |     (0 _ : AllTyArgsNamed mt) =>
138 |     (con : Con) ->
139 |     (0 cn : ConArgsNamed con) =>
140 |     (mcon : Con) ->
141 |     (0 mcn : ConArgsNamed mcon) =>
142 |     ConMeta ->
143 |     Nat ->
144 |     Clause
145 |   mkCastInjClause ur mt _ con meta n = do
146 |     if not (hasRecursiveArgs meta)
147 |       then do
148 |         let emptyCon = con.apply (\_ => `(_)) empty
149 |         (var "castInjImpl") .! ("castInj^x", emptyCon) .! ("castInj^y", emptyCon) .$ `(Refl) .= `(Refl)
150 |       else do
151 |         let finalClause = mkFinalClause con meta
152 |         let recNames = recursiveArgNames con meta
153 |         let initLhs = mkInitialLhs con meta
154 |         mkRecArgClauses recNames initLhs finalClause
155 |
156 |   ||| Derive cast injectivity proof
157 |   export
158 |   mkCastInjDecls :
159 |     UniResults ->
160 |     (mt : TypeInfo) ->
161 |     (0 mtp : AllTyArgsNamed mt) =>
162 |     TypeMeta ->
163 |     List Decl
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) $
170 |                 ti.apply var empty
171 |     let arg2 = MkArg MW ImplicitArg (Just yVar) $
172 |                 ti.apply var empty
173 |     let eqs =
174 |       `((~(mToPImplVar .$ var xVar)
175 |           ~=~
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)
185 |     ]
186 |
187 |
188 | ------------------------------------
189 | --- POLY TO SPEC CAST DERIVATION ---
190 | ------------------------------------
191 |
192 | parameters (t : SpecTask)
193 |   ||| Generate specialised to polymorphic type conversion function signature
194 |   export
195 |   mkPToMImplSig :
196 |     UniResults ->
197 |     (mt : TypeInfo) ->
198 |     (0 _ : AllTyArgsNamed mt) =>
199 |     TTImp
200 |   mkPToMImplSig _ mt =
201 |     forallMTArgs t $ arg t.fullInvocation .-> t.specInvocation
202 |
203 |   ||| Generate specialised to polymorphic type conversion function clause
204 |   ||| for given constructor
205 |   mkPToMImplClause :
206 |     UnificationResult ->
207 |     (mt : TypeInfo) ->
208 |     (0 _ : AllTyArgsNamed mt) =>
209 |     (pCon : Con) ->
210 |     (0 _ : ConArgsNamed pCon) =>
211 |     (mCon : Con) ->
212 |     (0 _ : ConArgsNamed mCon) =>
213 |     ConMeta ->
214 |     Nat ->
215 |     Clause
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)
222 |
223 |   ||| Generate specialised to polymorphic type conversion function declarations
224 |   export
225 |   mkPToMImplDecls :
226 |     UniResults ->
227 |     (mt : TypeInfo) ->
228 |     (0 _ : AllTyArgsNamed mt) =>
229 |     TypeMeta ->
230 |     List Decl
231 |   mkPToMImplDecls urs mt meta = do
232 |     let clauses = map2UConsN t mkPToMImplClause urs mt meta
233 |     [ def "pToMImpl" clauses
234 |     ]
235 |
236 |   ||| Generate specialised to polymorphic cast signature
237 |   export
238 |   mkPToMSig : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => TTImp
239 |   mkPToMSig mt = do
240 |     forallMTArgs t $ `(Cast ~(t.fullInvocation) ~(t.specInvocation))
241 |
242 |   ||| Generate specialised to polymorphic cast declarations
243 |   export
244 |   mkPToMDecls : (mt : TypeInfo) -> (0 _ : AllTyArgsNamed mt) => List Decl
245 |   mkPToMDecls mt =
246 |     [ def "pToM" [ (var "pToM") .= `(MkCast pToMImpl)]
247 |     ]
248 |