1 | module Deriving.SpecialiseData.TaskFormation
3 | import public Language.Reflection
4 | import public Language.Reflection.Syntax
5 | import public Language.Reflection.Compat.TypeInfo
6 | import public Language.Reflection.Logging
8 | allQImpl : Monad m => NamesInfoInTypes => TTImp -> TTImp -> m TTImp
9 | allQImpl (IPi {}) r = pure r
10 | allQImpl (IApp {}) (IApp _ (Implicit {}) _) = pure `(?
)
11 | allQImpl (IApp {}) r@(IApp {}) = pure r
12 | allQImpl (IApp {}) _ = pure `(?
)
13 | allQImpl v@(IVar _ n) _ =
14 | case lookupType n of
16 | Nothing => pure `(?
)
17 | allQImpl _ _ = pure `(?
)
22 | allQuestions : NamesInfoInTypes => TTImp -> TTImp
23 | allQuestions t = runIdentity $
mapMTTImp' allQImpl t
28 | constructor MkGenArg
35 | LogPosition GenArg where
36 | logPosition (MkGenArg a Nothing) = "\{fromMaybe "<unnamed arg>" a.name}"
37 | logPosition (MkGenArg a $
Just t) = "(\{fromMaybe "<unnamed arg>" a.name} := \{show t})"
39 | unGA : List GenArg -> (List Arg, List (Maybe TTImp))
41 | unGA (x :: xs) = let (ys, zs) = unGA xs in (x.arg :: ys, x.given :: zs)
43 | (.isGenerated) : GenArg -> Bool
44 | (.isGenerated) = isNothing . given
46 | (.isGiven) : GenArg -> Bool
47 | (.isGiven) = isJust . given
49 | singleArg : NamesInfoInTypes => Nat -> GenArg -> (TTImp, List GenArg)
50 | singleArg n (MkGenArg a v) = do
51 | let n : Name = fromString "lam^\{show n}"
52 | (IVar EmptyFC n, [MkGenArg (MkArg a.count a.piInfo (Just n) $
allQuestions a.type) v])
55 | data ArgDecision = Passthrough | SpecLit TTImp | SpecRec Name (List GenArg)
57 | isPassthrough : ArgDecision -> Bool
58 | isPassthrough Passthrough = True
59 | isPassthrough _ = False
62 | specDecideArg : NamesInfoInTypes => GenArg -> (ArgDecision, String)
63 | specDecideArg ga with (ga.given)
64 | specDecideArg ga | Nothing = (Passthrough, "No given value")
65 | specDecideArg ga | Just x = do
66 | let (appLhs, appTerms) = unAppAny x
67 | let IVar _ tyName = appLhs
68 | | IPrimVal _ (PrT _) => (SpecLit x, "Given a primitive type invocation")
69 | | IPi _ _ _ _ _ _ => (SpecLit x, "Given a function (pi) type")
70 | | IType _ => (SpecLit x, "Given a type of types")
71 | | _ => (Passthrough, "Given value head is not a variable")
72 | case lookupType tyName of
73 | Just tyInfo => case appTerms of
74 | [] => (SpecLit x, "Given a type invocation w/o arguments")
76 | let givens = map (uncurry MkGenArg) $
zip tyInfo.args $
popArgVals tyInfo.args (mkAllApps appTerms)
77 | (SpecRec tyName givens, "Given a type invocation")
80 | , if (snd (unPi ga.arg.type) == `(Type))
81 | then "Given a non-global type expr"
82 | else "Given a non-type expr")
87 | (GenArg -> (ArgDecision, String)) ->
91 | m (TTImp, List GenArg)
96 | (GenArg -> (ArgDecision, String)) ->
100 | m (List AnyApp, List GenArg)
101 | processArgs dec tyName k [] = pure ([], [])
102 | processArgs dec tyName k (x :: xs) = do
103 | (aT, l) <- assert_total $
processArg dec tyName k x
104 | (recAA, l') <- processArgs dec tyName (k + length l) xs
105 | pure (appArg x.arg aT :: recAA, l ++ l')
107 | processArg dec tyName argIdx ga =
109 | (Passthrough, s) =>
110 | logValue DetailedDebug "specialiseData.taskFormation" [tyName, ga]
111 | "\{s}, passing through" $
singleArg argIdx ga
113 | logValue DetailedDebug "specialiseData.taskFormation" [tyName, ga]
114 | "\{s}, specialising" (x, [])
115 | (SpecRec n givens, s) => do
116 | logPoint DetailedDebug "specialiseData.taskFormation" [tyName, ga]
117 | "\{s}, traversing arguments: \{show $ map (fromMaybe "" . name . arg) givens}"
118 | map (mapFst $
reAppAny (IVar EmptyFC n)) $
processArgs dec n argIdx $
takeWhile (.isGiven) givens
123 | NamesInfoInTypes =>
124 | (GenArg -> (ArgDecision, String)) ->
127 | m (TTImp, List Arg, List $
Maybe TTImp)
128 | argsToSpecTask dec tyName ga = bimap (reAppAny $
IVar EmptyFC tyName) unGA <$> processArgs dec tyName 0 ga
130 | allAppsToGenArgs : List Arg -> AllApps -> List GenArg
131 | allAppsToGenArgs [] aa = []
132 | allAppsToGenArgs (x :: xs) aa = do
133 | let pav = popArgVal x aa
134 | let mr = fst <$> pav
135 | let aa = fromMaybe aa $
snd <$> pav
136 | MkGenArg x mr :: allAppsToGenArgs xs aa
141 | NamesInfoInTypes =>
142 | (GenArg -> (ArgDecision, String)) ->
144 | m $
Maybe (TTImp, List Arg, List $
Maybe TTImp)
145 | exprToSpecTask dec expr = do
146 | let (appHead, appTerms) = unAppAny expr
147 | let (IVar _ tyName) = appHead
148 | | _ => logValue DetailedDebug "specialiseData.taskFormation" []
149 | "Head of expression is not a variable: \{show appHead}"
151 | let (Just tyInfo) = lookupType tyName
152 | | _ => logValue DetailedDebug "specialiseData.taskFormation" []
153 | "\{tyName} is not a global type"
155 | let allApps = mkAllApps appTerms
156 | let genArgs = allAppsToGenArgs tyInfo.args allApps
157 | let False = all isPassthrough $
fst . dec <$> genArgs
158 | | True => logValue DetailedDebug "specialiseData.taskFormation" []
159 | "No non-passthrough arguments!"
161 | Just <$> argsToSpecTask dec tyName genArgs