10 | module Generics.Meta
19 | data ArgName : Type where
22 | NamedArg : (index : Nat) -> (name : String) -> ArgName
25 | UnnamedArg : (index : Nat) -> ArgName
29 | getName : ArgName -> Maybe String
30 | getName (NamedArg _ name) = Just name
31 | getName (UnnamedArg _) = Nothing
39 | record ConInfo_ (k : Type) (ks : List k) where
40 | constructor MkConInfo
49 | args : NP_ k (K ArgName) ks
53 | ConInfo : {k : Type} -> (ks : List k) -> Type
54 | ConInfo = ConInfo_ k
59 | argNames : ConInfo ks -> Maybe (NP (K String) ks)
60 | argNames = htraverse getName . args
65 | isOperator : String -> Bool
66 | isOperator = all (not . isAlphaNum) . unpack
70 | wrapOperator : String -> String
71 | wrapOperator n = if isOperator n then "(" ++ n ++ ")" else n
79 | record TypeInfo' (k : Type) (kss : List $
List k) where
80 | constructor MkTypeInfo
83 | typeNS : List String
89 | constructors : NP_ (List k) (ConInfo_ k) kss
93 | TypeInfo : {k : Type} -> (kss : List $
List k) -> Type
94 | TypeInfo = TypeInfo' k
105 | interface Generic t code => Meta t code | t where
107 | meta : TypeInfo' Type code
112 | metaFor : (0 t : Type) -> Meta t code => TypeInfo code
113 | metaFor t = meta {t = t}
120 | showC : NP (Show . f) ks => (p : Prec) -> ConInfo ks -> NP f ks -> String
123 | showC _ info [] = info.conName
127 | showC p info args =
128 | let con := wrapOperator info.conName
129 | in maybe (showOther con) (showRecord con) (argNames info)
132 | showNamed : Show a => String -> a -> String
133 | showNamed name v = name ++ " = " ++ show v
135 | showRecord : (con : String) -> NP (K String) ks -> String
136 | showRecord con names =
137 | let applied := hcliftA2 (Show . f) showNamed names args
138 | inner := intersperse ", " (collapseNP applied)
139 | in showCon p con (" { " ++ concat inner ++ " }")
141 | showOther : (con : String) -> String
143 | let argStr := hconcat $
hcmap (Show . f) showArg args
144 | in showCon p con argStr
147 | {auto all : POP (Show . f) kss}
152 | showSOP {all = MkPOP _} p (MkTypeInfo _ _ cons) =
153 | hconcat . hcliftA2 (NP $
Show . f) (showC p) cons . unSOP
164 | genShowPrec : Meta t code => POP Show code => Prec -> t -> String
165 | genShowPrec p = showSOP p (metaFor t) . from