0 | ||| Specialiser-specific extension functions for TypeInfo, Con, and Arg
 1 | module Deriving.SpecialiseData.Utils.TypeInfo
 2 |
 3 | import Language.Reflection.Compat
 4 | import Language.Reflection.Expr
 5 |
 6 | ||| Generate an AnyApp for given Arg, with the argument value either
 7 | ||| retrieved from the map if present or generated with `fallback`
 8 | (.appWith) :
 9 |   (arg : Arg) ->
10 |   (0 _ : IsNamedArg arg) =>
11 |   (fallback : Name -> TTImp) ->
12 |   (argValues : SortedMap Name TTImp) ->
13 |   AnyApp
14 | (.appWith) arg@(MkArg _ _ (Just n) _) f argVals =
15 |   appArg arg $ fromMaybe (f n) $ lookup n argVals
16 |
17 | ||| Generate a List AnyApp for given argument List,
18 | ||| with arguments retrieved from the map if present or generated with `fallback`
19 | export
20 | (.appsWith) :
21 |   (args : List Arg) ->
22 |   (0 _ : All IsNamedArg args) =>
23 |   (fallback : Name -> TTImp) ->
24 |   (argValues : SortedMap Name TTImp) ->
25 |   List AnyApp
26 | (.appsWith) [] _ _ = []
27 | (.appsWith) (x :: xs) @{_ :: _} f argVals =
28 |   x.appWith f argVals :: xs.appsWith f argVals
29 |
30 | namespace TypeInfoInvoke
31 |   ||| Returns a full application of the given type constructor
32 |   ||| with argument values sourced from `argValues`
33 |   ||| or generated with `fallback` if not present
34 |   export
35 |   (.apply) :
36 |     (ti : TypeInfo) ->
37 |     (0 tiN : AllTyArgsNamed ti) =>
38 |     (fallback : Name -> TTImp) ->
39 |     (argValues : SortedMap Name TTImp) ->
40 |     TTImp
41 |   (.apply) t f vals = reAppAny (var t.name) $ t.args.appsWith @{tiN.tyArgsNamed} f vals
42 |
43 | namespace ConInvoke
44 |   ||| Returns a full application of the given constructor
45 |   ||| with argument values sourced from `argValues`
46 |   ||| or generated with `fallback` if not present
47 |   export
48 |   (.apply) :
49 |     (con : Con) ->
50 |     (0 _ : ConArgsNamed con) =>
51 |     (fallback : Name -> TTImp) ->
52 |     (argValues : SortedMap Name TTImp) ->
53 |     TTImp
54 |   (.apply) con f vals = reAppAny (var con.name) $ con.args.appsWith f vals @{conArgsNamed}
55 |