0 | ||| Logic for constructing specialisation tasks. Not part of the specialiser proper (yet).
  1 | module Deriving.SpecialiseData.TaskFormation
  2 |
  3 | import public Language.Reflection
  4 | import public Language.Reflection.Syntax
  5 | import public Language.Reflection.Compat.TypeInfo
  6 | import public Language.Reflection.Logging -- workaround for idris-lang/Idris2#2439
  7 |
  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
 15 |     Just _ => pure v
 16 |     Nothing => pure `(?)
 17 | allQImpl _ _ = pure `(?)
 18 |
 19 | ||| Replace every non-function sub-expression with a question mark
 20 | |||
 21 | ||| (x -> (y -> z) -> q) becomes (? -> (? -> ?) -> ?)
 22 | allQuestions : NamesInfoInTypes => TTImp -> TTImp
 23 | allQuestions t = runIdentity $ mapMTTImp' allQImpl t
 24 |
 25 | ||| Information about a generator's argument extracted from GenSignature
 26 | public export
 27 | record GenArg where
 28 |   constructor MkGenArg
 29 |   ||| Formal argument beloging to a type constructor
 30 |   arg : Arg
 31 |   ||| Value of the actual/given argument (if present)
 32 |   given : Maybe TTImp
 33 |
 34 | export
 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})"
 38 |
 39 | unGA : List GenArg -> (List Arg, List (Maybe TTImp))
 40 | unGA [] = ([], [])
 41 | unGA (x :: xs) = let (ys, zs) = unGA xs in (x.arg :: ys, x.given :: zs)
 42 |
 43 | (.isGenerated) : GenArg -> Bool
 44 | (.isGenerated) = isNothing . given
 45 |
 46 | (.isGiven) : GenArg -> Bool
 47 | (.isGiven) = isJust . given
 48 |
 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])
 53 |
 54 | public export
 55 | data ArgDecision = Passthrough | SpecLit TTImp | SpecRec Name (List GenArg)
 56 |
 57 | isPassthrough : ArgDecision -> Bool
 58 | isPassthrough Passthrough = True
 59 | isPassthrough _ = False
 60 |
 61 | export
 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")
 75 |         _ => do
 76 |           let givens = map (uncurry MkGenArg) $ zip tyInfo.args $ popArgVals tyInfo.args (mkAllApps appTerms)
 77 |           (SpecRec tyName givens, "Given a type invocation")
 78 |       Nothing =>
 79 |         ( Passthrough
 80 |         , if (snd (unPi ga.arg.type) == `(Type))
 81 |             then "Given a non-global type expr"
 82 |             else "Given a non-type expr")
 83 |
 84 | processArg :
 85 |   MonadLog m =>
 86 |   NamesInfoInTypes =>
 87 |   (GenArg -> (ArgDecision, String)) ->
 88 |   Name ->
 89 |   Nat ->
 90 |   GenArg ->
 91 |   m (TTImp, List GenArg)
 92 |
 93 | processArgs :
 94 |   MonadLog m =>
 95 |   NamesInfoInTypes =>
 96 |   (GenArg -> (ArgDecision, String)) ->
 97 |   Name ->
 98 |   Nat ->
 99 |   List GenArg ->
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')
106 |
107 | processArg dec tyName argIdx ga =
108 |   case dec ga of
109 |     (Passthrough, s) =>
110 |       logValue DetailedDebug "specialiseData.taskFormation" [tyName, ga]
111 |         "\{s}, passing through" $ singleArg argIdx ga
112 |     (SpecLit x, s) =>
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
119 |
120 | export
121 | argsToSpecTask :
122 |   MonadLog m =>
123 |   NamesInfoInTypes =>
124 |   (GenArg -> (ArgDecision, String)) ->
125 |   Name ->
126 |   List GenArg ->
127 |   m (TTImp, List Arg, List $ Maybe TTImp)
128 | argsToSpecTask dec tyName ga = bimap (reAppAny $ IVar EmptyFC tyName) unGA <$> processArgs dec tyName 0 ga
129 |
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
137 |
138 | export
139 | exprToSpecTask :
140 |   MonadLog m =>
141 |   NamesInfoInTypes =>
142 |   (GenArg -> (ArgDecision, String)) ->
143 |   TTImp ->
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}"
150 |               Nothing
151 |   let (Just tyInfo) = lookupType tyName
152 |     | _ => logValue DetailedDebug "specialiseData.taskFormation" []
153 |               "\{tyName} is not a global type"
154 |               Nothing
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!"
160 |                 Nothing
161 |   Just <$> argsToSpecTask dec tyName genArgs
162 |