0 | ||| Types and functions reused inside different specialiser modules
 1 | module Deriving.SpecialiseData.Helpers
 2 |
 3 | import Language.Reflection.Compat
 4 | import Language.Reflection.Expr
 5 |
 6 | export
 7 | prependS : String -> Name -> Name
 8 | prependS s n = UN $ Basic $ s ++ show n
 9 |
10 | ||| Given a sequence of arguments, return list of argument name-BindVar pairs
11 | export
12 | argsToBindMap : Foldable f => f Arg -> List (Name, TTImp)
13 | argsToBindMap = foldMap $ toList . map (\y => (y, bindVar y)) . name
14 |
15 | ||| Specialistaion-related constructor argument metadata
16 | public export
17 | record ArgMeta where
18 |   constructor MkAMeta
19 |   ||| The argument's type can be substituted by specialised type invocation
20 |   isRecursiveArg : Bool
21 |   ||| The argument's type contains a potential mutual recursion
22 |   hasMutualRecursion: Bool
23 |
24 | ||| Specialisation-related constructor metadata
25 | public export
26 | record ConMeta where
27 |   constructor MkCMeta
28 |   ||| Metadata for each argument
29 |   argMeta : List ArgMeta
30 |   ||| Replacements to transform original argument's type to specialised type
31 |   mToPRenames : SortedMap Name TTImp
32 |   ||| Replacement to transform specialised type to original argument's type
33 |   pToMRenames : SortedMap Name TTImp
34 |
35 | export
36 | hasRecursiveArgs : ConMeta -> Bool
37 | hasRecursiveArgs = any isRecursiveArg . argMeta
38 |
39 | export
40 | countRecursiveArgs : ConMeta -> Nat
41 | countRecursiveArgs = count isRecursiveArg  . argMeta
42 |
43 | export
44 | recursiveArgNames : Con -> ConMeta -> List Name
45 | recursiveArgNames con meta = do
46 |   let recursiveArgPairs = List.filter (isRecursiveArg . snd) $ zip con.args meta.argMeta
47 |   fromMaybe "" . name . fst <$> recursiveArgPairs
48 |
49 | ||| Specialisation-related type metadata
50 | public export
51 | record TypeMeta where
52 |   constructor MkTyMeta
53 |   ||| Specialisation-related metadata for each constructor
54 |   conMeta : List ConMeta
55 |
56 | ||| Generate a constructor binding where only recursive arguments are bound.
57 | ||| Said arguments are also aliased via `alias` function.
58 | export
59 | bindConRecArgsAliased : Con -> ConMeta -> (Name -> Name) -> TTImp
60 | bindConRecArgsAliased con meta alias =
61 |   reAppAny (var con.name) $ processArg <$> zip con.args meta.argMeta
62 |   where
63 |   maybeBind : Arg -> ArgMeta -> TTImp
64 |   maybeBind a am = if isRecursiveArg am then bindVar $ alias $ fromMaybe "" a.name else `(_)
65 |
66 |   processArg : (Arg, ArgMeta) -> AnyApp
67 |   processArg (a@(MkArg _ ExplicitArg _ _), am) = PosApp $ maybeBind a am
68 |   processArg (a, am) = NamedApp (fromMaybe "" a.name) $ maybeBind a am
69 |
70 | ||| Make an argument omega implicit if it is explicit
71 | export
72 | hideExplicitArg : Arg -> Arg
73 | --                           v Using $= here doesn't compile
74 | hideExplicitArg a = { piInfo := if a.piInfo == ExplicitArg then ImplicitArg else a.piInfo } a
75 |
76 | ||| Make a type argument zero-count
77 | export
78 | makeTypeArgM0 : Arg -> Arg
79 | makeTypeArgM0 a = { count := if a.type == `(Type) then M0 else a.count } a
80 |