0 | ||| Logic for generating the specialised type itself
  1 | module Deriving.SpecialiseData.Codegen.Type
  2 |
  3 | import public Data.DPair -- workaround for idris-lang/Idris2#2439
  4 | import Data.Fin
  5 | import Data.SnocList
  6 | import Data.SnocList.Quantifiers
  7 | import Deriving.SpecialiseData.Common
  8 | import Deriving.SpecialiseData.Helpers
  9 | import Deriving.SpecialiseData.Task
 10 | import Deriving.SpecialiseData.TaskFormation
 11 | import Deriving.SpecialiseData.Unification
 12 | import Deriving.SpecialiseData.Utils.Quantifiers
 13 | import Deriving.SpecialiseData.Utils.TypeInfo
 14 | import Language.Reflection.Compat
 15 | import Language.Reflection.Expr
 16 | import public Language.Reflection.Expr.VarSubst -- workaround for idris-lang/Idris2#2439
 17 | import public Language.Reflection.Unify
 18 |
 19 | -----------------------------------
 20 | --- SPECIALISED TYPE GENERATION ---
 21 | -----------------------------------
 22 |
 23 |
 24 | ||| Generate argument of a specified constructor
 25 | mkSpecArg : (ur : UnificationResult) -> Fin (ur.uniDg.freeVars) -> Subset Arg IsNamedArg
 26 | mkSpecArg ur fvId = do
 27 |   let fvData = index fvId ur.uniDg.fvData
 28 |   let fromLambda = finToNat fvId >= ur.task.lfv
 29 |   let rig = if fromLambda then M0 else fvData.rig
 30 |   let piInfo = if fromLambda && (fvData.piInfo == ExplicitArg) then ImplicitArg else fvData.piInfo
 31 |   Element (MkArg rig piInfo (Just fvData.name) fvData.type) ItIsNamed
 32 |
 33 | getVar : TTImp -> Maybe Name
 34 | getVar (IVar _ n) = Just n
 35 | getVar _ = Nothing
 36 |
 37 | ||| Internal state of recursion search algorithm
 38 | record RecursionSearchState where
 39 |   constructor MkRSS
 40 |   ||| Accumulated transformation to cast from argument type to specialised type
 41 |   mToPRenames : SortedMap Name TTImp
 42 |   ||| Accumulated transformation to cast from specialised type to argument type
 43 |   pToMRenames : SortedMap Name TTImp
 44 |   ||| SnocList containing metadata of previous arguments
 45 |   areArgsRecursive : SnocList ArgMeta
 46 |   ||| The pre-baked arguments to run unification with.
 47 |   argsForUnifier : Subset (Vect (length areArgsRecursive) Arg) (All IsNamedArg)
 48 |   ||| Accumulated arguments to be used in specialised constructor
 49 |   argsOutput : Subset (SnocList Arg) (All IsNamedArg)
 50 |
 51 | parameters (t : SpecTask)
 52 |   ||| Check if a given argument is "recursive" (i.e. its type can be replaced with invocation of specialised type)
 53 |   checkArgRecursion :
 54 |     Monad m =>
 55 |     CanUnify m =>
 56 |     MonadLog m =>
 57 |     NamesInfoInTypes =>
 58 |     RecursionSearchState -> Subset Arg IsNamedArg -> m RecursionSearchState
 59 |   checkArgRecursion rss (Element thisArg thisArgNamed) = do
 60 |     let (MkRSS cRenames pToMRenames areArgsRec (Element argsForUni _) (Element argsOut _)) = rss
 61 |     let (aLhs, aa) = unAppAny thisArg.type
 62 |     let hasMutualRecursion = hasNameInsideDeep t.polyTy.name thisArg.type
 63 |     let True = (length aa /= 0) || (isJust $ lookupType =<< getVar aLhs)
 64 |       | False => do
 65 |         let outPiInfo = substituteVariables cRenames <$> thisArg.piInfo
 66 |         let outType = substituteVariables cRenames thisArg.type
 67 |         let Element outArg outArgNamed =
 68 |             Element (MkArg thisArg.count outPiInfo (Just $ argName thisArg) outType) ItIsNamed
 69 |         pure $
 70 |           MkRSS
 71 |             cRenames
 72 |             pToMRenames
 73 |             (areArgsRec :< MkAMeta False hasMutualRecursion)
 74 |             (Element (snoc argsForUni thisArg) (snoc %search thisArgNamed))
 75 |             (Element (argsOut :< outArg) (%search :< outArgNamed))
 76 |     let Element ta _ = fromListAll t.tqArgs @{t.tqArgsNamed}
 77 |     let uniTask = MkUniTask {lfv=_} argsForUni thisArg.type {rfv=_} ta t.fullInvocation
 78 |     ur <- unify uniTask
 79 |     case ur of
 80 |       Success ur => do
 81 |         let typeArgs = t.ttArgs.appsWith @{t.ttArgsNamed} var ur.fullResult
 82 |         logPoint DetailedDebug "specialiseData.fra" [] $ show ur.fullResult
 83 |         let tyRet = reAppAny (var t.resultName) typeArgs
 84 |         logPoint DetailedDebug "specialiseData.fra" [] $ show tyRet
 85 |         let mToPImpl = var $ inGenNS t "mToPImpl"
 86 |         let pToMImpl = var $ inGenNS t "pToMImpl"
 87 |         let outPiInfo = (\x => `(cast ~x)) . substituteVariables cRenames <$> thisArg.piInfo
 88 |         let outType = substituteVariables cRenames tyRet
 89 |         let Element outArg outArgNamed =
 90 |             Element (MkArg thisArg.count outPiInfo (Just $ argName thisArg) outType) ItIsNamed
 91 |         pure $
 92 |           MkRSS
 93 |             (insert (argName thisArg) `(~mToPImpl ~(var $ argName thisArg)) cRenames)
 94 |             (insert (argName thisArg) `(~pToMImpl ~(var $ argName thisArg)) pToMRenames)
 95 |             (areArgsRec :< MkAMeta True False) -- If we found direct recursion, mutual recursion is out by definition
 96 |             (Element (snoc argsForUni thisArg) (snoc %search thisArgNamed))
 97 |             (Element (argsOut :< outArg) (%search :< outArgNamed))
 98 |       _ => do
 99 |         let outPiInfo = substituteVariables cRenames <$> thisArg.piInfo
100 |         let outType = substituteVariables cRenames thisArg.type
101 |         let Element outArg outArgNamed =
102 |             Element (MkArg thisArg.count outPiInfo (Just $ argName thisArg) outType) ItIsNamed
103 |         pure $
104 |           MkRSS
105 |             cRenames
106 |             pToMRenames
107 |             (areArgsRec :< MkAMeta False hasMutualRecursion)
108 |             (Element (snoc argsForUni thisArg) (snoc %search thisArgNamed))
109 |             (Element (argsOut :< outArg) (%search :< outArgNamed))
110 |
111 |   ||| Generate a specialised constructor
112 |   export
113 |   mkSpecCon :
114 |     Monad m =>
115 |     CanUnify m =>
116 |     MonadLog m =>
117 |     NamesInfoInTypes =>
118 |     (params : SpecialisationParams) =>
119 |     (newArgs : List Arg) ->
120 |     (0 _ : All IsNamedArg newArgs) =>
121 |     UnificationResult ->
122 |     (con : Con) ->
123 |     (0 _ : ConArgsNamed con) =>
124 |     Nat ->
125 |     m $ (Subset Con ConArgsNamed, ConMeta)
126 |   mkSpecCon newArgs ur pCon cIdx = do
127 |     let specArgs = mkSpecArg ur <$> ur.order
128 |     let Element args allArgs =
129 |       pullOut specArgs
130 |     let typeArgs = newArgs.appsWith var ur.fullResult
131 |     let tyRet = reAppAny (var t.resultName) typeArgs
132 |     let n = if params.eraseConNames then fromString "\{t.resultName}^Con^\{show cIdx}" else dropNS pCon.name
133 |     rssRhs <- foldlM checkArgRecursion (MkRSS empty empty [<] (Element [] []) (Element [<] [<])) specArgs
134 |     let (MkRSS mToPRenames pToMRenames argsMeta' _ (Element outArgs' outArgsNamed')) = rssRhs
135 |     let Element outArgs outArgsNamed = toListAll outArgs' outArgsNamed'
136 |     let conMeta = MkCMeta (toList argsMeta') mToPRenames pToMRenames
137 |     pure $ (MkCon
138 |       { name = inGenNS t $ n
139 |       , args = outArgs
140 |       , type = substituteVariables mToPRenames tyRet
141 |       } `Element` TheyAreNamed outArgsNamed, conMeta)
142 |
143 |   ||| Generate a specialised type
144 |   export
145 |   mkSpecTy :
146 |     Monad m => CanUnify m => MonadLog m =>
147 |     SpecialisationParams => NamesInfoInTypes =>
148 |     UniResults -> m $ (Subset TypeInfo AllTyArgsNamed, TypeMeta)
149 |   mkSpecTy ur = do
150 |     let 0 _ = t.ttArgsNamed
151 |     let muc = mapUCons t (mkSpecCon t.ttArgs) ur
152 |     specConsMeta <- traverse id muc
153 |     let (specCons, specMeta) = unzip specConsMeta
154 |     let Element cons consAreNamed = pullOut specCons
155 |     pure $ (MkTypeInfo
156 |       { name = inGenNS t t.resultName
157 |       , args = t.ttArgs
158 |       , cons
159 |       } `Element` TheyAllAreNamed t.ttArgsNamed consAreNamed, MkTyMeta specMeta)
160 |
161 |
162 |