2 | import Decidable.Equality
4 | import public Data.List.Elem
5 | import public Data.SOP
16 | interface Generic (0 t : Type) (0 code : List $
List Type) | t where
17 | constructor MkGeneric
19 | from : (v : t) -> SOP I code
23 | to : (v : SOP I code) -> t
26 | fromToId : (v : t) -> to (from v) = v
29 | toFromId : (v : SOP I code) -> from (to v) = v
33 | {auto _ : Generic t code}
36 | -> (from x = from y)
38 | fromInjective x y prf = rewrite sym $
fromToId y in lemma2
41 | lemma1 : to {t = t} (from x) = to {t = t} (from y)
42 | lemma1 = cong to prf
44 | lemma2 : x = to {t = t} (from y)
45 | lemma2 = rewrite sym $
fromToId x in lemma1
48 | 0 Code : (t : Type) -> Generic t code => List $
List Type
57 | -> {auto _ : Generic t code}
58 | -> {auto prf : Elem ts code}
60 | genExtract ts v = extractSOP ts $
from v
68 | -> {auto _ : Generic t code}
69 | -> {auto prf : Elem [t'] code}
71 | genExtract1 t' v = hd <$> genExtract [t'] v
76 | valuesNP : Generic t code => (et : EnumType code) =>
77 | NP_ (List Type) (K t) code
78 | valuesNP = hmap (to . MkSOP) (run et)
81 | run : EnumType kss -> NP_ (List k) (K (NS_ (List k) (NP f) kss)) kss
83 | run (ES x) = Z [] :: mapNP (\ns => S ns) (run x)
87 | public export %inline
88 | values : Generic t code => (et : EnumType code) => List t
89 | values = collapseNP valuesNP
93 | public export %inline
96 | -> {auto _ : Generic t code}
97 | -> {auto et : EnumType code}
98 | -> NP_ (List Type) (K t) code
99 | valuesForNP _ = valuesNP
103 | public export %inline
104 | valuesFor : (0 t : Type) -> Generic t code => (et : EnumType code) => List t
105 | valuesFor _ = values
114 | genEq : Generic t code => POP Eq code => t -> t -> Bool
115 | genEq x y = from x == from y
120 | genCompare : Generic t code => POP Ord code => t -> t -> Ordering
121 | genCompare x y = compare (from x) (from y)
127 | {auto _ : Generic t code}
128 | -> {auto _ : POP DecEq code}
133 | case decEq (from x) (from y) of
134 | (Yes prf) => Yes $
fromInjective x y prf
135 | (No contra) => No $
\h => contra (cong from h)
141 | {auto _ : Generic t [ts]}
142 | -> {auto _ : POP Semigroup [ts]}
144 | genAppend x y = to $
from x <+> from y
149 | genNeutral : Generic t [ts] => POP Monoid [ts] => t
150 | genNeutral = to neutral
161 | Generic () [[]] where
162 | from () = MkSOP $
Z []
164 | to (MkSOP $
Z []) = ()
165 | to (MkSOP $
S _) impossible
169 | toFromId (MkSOP $
Z []) = Refl
172 | Generic (a,b) [[a,b]] where
173 | from (a,b) = MkSOP $
Z [a,b]
175 | to (MkSOP $
Z [a,b]) = (a,b)
176 | to (MkSOP $
S _) impossible
178 | fromToId (a,b) = Refl
180 | toFromId (MkSOP $
Z [a,b]) = Refl
183 | Generic (LPair a b) [[a,b]] where
184 | from (a # b) = MkSOP $
Z [a,b]
186 | to (MkSOP $
Z [a,b]) = (a # b)
187 | to (MkSOP $
S _) impossible
189 | fromToId (a # b) = Refl
191 | toFromId (MkSOP $
Z [a, b]) = Refl
194 | Generic Void [] where
197 | to (MkSOP v) impossible
199 | fromToId v impossible
201 | toFromId (MkSOP v) impossible
204 | Generic (Stream a) [[a, Inf (Stream a)]] where
205 | from (h :: t) = MkSOP $
Z [h,t]
207 | to (MkSOP $
Z [h,t]) = h :: t
209 | fromToId (h :: t) = Refl
211 | toFromId (MkSOP $
Z [h,t]) = Refl