2 | import Data.List.Elem
3 | import Data.SOP.Interfaces
5 | import Data.SOP.Utils
7 | import Decidable.Equality
42 | data NS_ : (k : Type) -> (f : k -> Type) -> (ks : List k) -> Type where
43 | Z : (v : f t) -> NS_ k f (t :: ks)
44 | S : NS_ k f ks -> NS_ k f (t :: ks)
50 | NS : {k : Type} -> (f : k -> Type) -> (ks : List k) -> Type
56 | toI : NS_ k f ks -> NS I (map f ks)
58 | toI (S x) = S $
toI x
66 | mapNS : (fun : forall a . f a -> g a) -> NS f ks -> NS g ks
67 | mapNS fun (Z v) = Z $
fun v
68 | mapNS fun (S x) = S $
mapNS fun x
72 | foldlNS : (fun : acc -> el -> acc) -> acc -> NS (K el) ks -> acc
73 | foldlNS fun acc (Z v) = fun acc v
74 | foldlNS fun acc (S x) = foldlNS fun acc x
77 | public export %inline
78 | foldrNS : (fun : el -> Lazy acc -> acc) -> Lazy acc -> NS (K el) ks -> acc
79 | foldrNS fun acc (Z v) = fun v acc
80 | foldrNS fun acc (S x) = foldrNS fun acc x
84 | sequenceNS : Applicative g => NS (\a => g (f a)) ks -> g (NS f ks)
85 | sequenceNS (Z v) = map Z v
86 | sequenceNS (S x) = S <$> sequenceNS x
90 | hapNS : NP (\a => f a -> g a) ks -> NS f ks -> NS g ks
91 | hapNS (fun :: _) (Z v) = Z $
fun v
92 | hapNS (_ :: funs) (S y) = S $
hapNS funs y
96 | collapseNS : NS (K a) ks -> a
97 | collapseNS (Z v) = v
98 | collapseNS (S x) = collapseNS x
107 | 0 Injection : (f : k -> Type) -> (ks : List k) -> (v : k) -> Type
108 | Injection f ks v = f v -> K (NS f ks) v
113 | injectionsNP : NP g ks -> NP (Injection f ks) ks
114 | injectionsNP [] = []
115 | injectionsNP (_ :: t) = Z :: mapNP (S .) (injectionsNP t)
120 | injections : {ks : _} -> NP (Injection f ks) ks
121 | injections = injectionsNP (pureNP ())
132 | apInjsNP_ : NP f ks -> NP (K $
NS f ks) ks
134 | apInjsNP_ (v :: vs) = Z v :: mapNP S (apInjsNP_ vs)
138 | apInjsNP : NP f ks -> List (NS f ks)
139 | apInjsNP = collapseNP . apInjsNP_
147 | inject : {0 t : k} -> (v : f t) -> {auto prf : Elem t ks} -> NS f ks
148 | inject v {prf = Here} = Z v
149 | inject v {prf = There _} = S $
inject v
153 | extract : (0 t : k) -> NS f ks -> {auto prf : Elem t ks} -> Maybe (f t)
154 | extract _ (Z v) {prf = Here} = Just v
155 | extract _ (S _) {prf = Here} = Nothing
156 | extract _ (Z _) {prf = There y} = Nothing
157 | extract t (S x) {prf = There y} = extract t x
162 | toNP : {ks : _} -> Alternative g => NS f ks -> NP (g . f) ks
163 | toNP {ks = _ :: _} (Z v) = pure v :: hpure empty
164 | toNP {ks = _ :: _} (S x) = empty :: toNP x
172 | expand : NS f ks -> {auto prf: Sublist ks ks'} -> NS f ks'
173 | expand (Z v) {prf = SLSame y} = Z v
174 | expand (S x) {prf = SLSame y} = S $
expand x
175 | expand v {prf = SLDiff y} = S $
expand v
176 | expand _ {prf = SLNil} impossible
181 | narrow : NS f ks -> {auto prf: Sublist ks' ks} -> Maybe (NS f ks')
182 | narrow _ {prf = SLNil} = Nothing
183 | narrow (Z v) {prf = SLSame x} = Just $
Z v
184 | narrow (Z v) {prf = SLDiff x} = Nothing
185 | narrow (S x) {prf = SLSame y} = S <$> narrow x
186 | narrow (S x) {prf = SLDiff y} = narrow x
192 | public export %inline
193 | HFunctor k (List k) (NS_ k) where hmap = mapNS
195 | public export %inline
196 | HAp k (List k) (NP_ k) (NS_ k) where hap = hapNS
198 | public export %inline
199 | HFold k (List k) (NS_ k) where
204 | HSequence k (List k) (NS_ k) where hsequence = sequenceNS
207 | HCollapse k (List k) (NS_ k) I where hcollapse = collapseNS
210 | (all : NP (Eq . f) ks) => Eq (NS_ k f ks) where
211 | (==) {all = _::_} (Z v) (Z w) = v == w
212 | (==) {all = _::_} (S x) (S y) = x == y
213 | (==) {all = _::_} _ _ = False
216 | (all : NP (Ord . f) ks) => Ord (NS_ k f ks) where
217 | compare {all = _::_} (Z v) (Z w) = compare v w
218 | compare {all = _::_} (S x) (S y) = compare x y
219 | compare {all = _::_} (Z _) (S _) = LT
220 | compare {all = _::_} (S _) (Z _) = GT
226 | (all : NP (Semigroup . f) [k']) =>
227 | Semigroup (NS_ k f [k']) where
228 | (<+>) {all = _ :: _} (Z l) (Z r) = Z $
l <+> r
229 | (<+>) {all = _ :: _} (S _) _ impossible
230 | (<+>) {all = _ :: _} _ (S _) impossible
233 | (all : NP (Show . f) ks) => Show (NS_ k f ks) where
234 | showPrec {all = _::_} p (Z v) = showCon p "Z" (showArg v)
235 | showPrec {all = _::_} p (S x) = showCon p "S" (showPrec App x)
241 | (all : NP (Monoid . f) [k']) => Monoid (NS_ k f [k']) where
242 | neutral {all = _ :: _} = Z neutral
245 | Uninhabited (Data.SOP.NS.Z x = Data.SOP.NS.S y) where
246 | uninhabited Refl
impossible
249 | Uninhabited (Data.SOP.NS.S x = Data.SOP.NS.Z y) where
250 | uninhabited Refl
impossible
253 | zInjective : Data.SOP.NS.Z x = Data.SOP.NS.Z y -> x = y
254 | zInjective Refl = Refl
257 | sInjective : Data.SOP.NS.S x = Data.SOP.NS.S y -> x = y
258 | sInjective Refl = Refl
261 | (all : NP (DecEq . f) ks) => DecEq (NS_ k f ks) where
262 | decEq {all = _::_} (Z x) (Z y) with (decEq x y)
263 | decEq {all = _::_} (Z x) (Z x) | Yes Refl = Yes Refl
264 | decEq {all = _::_} (Z x) (Z y) | No contra = No (contra . zInjective)
265 | decEq {all = _::_} (Z x) (S y) = No absurd
266 | decEq {all = _::_} (S x) (Z y) = No absurd
267 | decEq {all = _::_} (S x) (S y) with (decEq x y)
268 | decEq {all = _::_} (S x) (S x) | Yes Refl = Yes Refl
269 | decEq {all = _::_} (S x) (S y) | No contra = No (contra . sInjective)
275 | Ex1 : let T = NS I [Char,Bool] in (T,T)
276 | Ex1 = (Z 'x', S $
Z False)