1 | module Deriving.SpecialiseData.Codegen.Type
3 | import public Data.DPair
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
17 | import public Language.Reflection.Unify
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
33 | getVar : TTImp -> Maybe Name
34 | getVar (IVar _ n) = Just n
38 | record RecursionSearchState where
41 | mToPRenames : SortedMap Name TTImp
43 | pToMRenames : SortedMap Name TTImp
45 | areArgsRecursive : SnocList ArgMeta
47 | argsForUnifier : Subset (Vect (length areArgsRecursive) Arg) (All IsNamedArg)
49 | argsOutput : Subset (SnocList Arg) (All IsNamedArg)
51 | parameters (t : SpecTask)
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)
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
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
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
93 | (insert (argName thisArg) `(~mToPImpl ~(var $
argName thisArg)) cRenames)
94 | (insert (argName thisArg) `(~pToMImpl ~(var $
argName thisArg)) pToMRenames)
95 | (areArgsRec :< MkAMeta True False)
96 | (Element (snoc argsForUni thisArg) (snoc %search thisArgNamed))
97 | (Element (argsOut :< outArg) (%search :< outArgNamed))
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
107 | (areArgsRec :< MkAMeta False hasMutualRecursion)
108 | (Element (snoc argsForUni thisArg) (snoc %search thisArgNamed))
109 | (Element (argsOut :< outArg) (%search :< outArgNamed))
117 | NamesInfoInTypes =>
118 | (params : SpecialisationParams) =>
119 | (newArgs : List Arg) ->
120 | (0 _ : All IsNamedArg newArgs) =>
121 | UnificationResult ->
123 | (0 _ : ConArgsNamed con) =>
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 =
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
138 | { name = inGenNS t $
n
140 | , type = substituteVariables mToPRenames tyRet
141 | } `Element` TheyAreNamed outArgsNamed, conMeta)
146 | Monad m => CanUnify m => MonadLog m =>
147 | SpecialisationParams => NamesInfoInTypes =>
148 | UniResults -> m $
(Subset TypeInfo AllTyArgsNamed, TypeMeta)
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
156 | { name = inGenNS t t.resultName
159 | } `Element` TheyAllAreNamed t.ttArgsNamed consAreNamed, MkTyMeta specMeta)