@@ -1,12 +1,13 @@
1{-# LANGUAGE GADTs, DataKinds, KindSignatures, TypeOperators, TypeFamilies,
1{-# LANGUAGE GADTs, DataKinds, KindSignatures, TypeOperators, TypeFamilies,
2 MultiParamTypeClasses, FlexibleInstances, PolyKinds,
2 MultiParamTypeClasses, FlexibleInstances, PolyKinds,
3 FlexibleContexts, UndecidableInstances, ConstraintKinds,
3 FlexibleContexts, UndecidableInstances, ConstraintKinds,
4− ScopedTypeVariables #-}
4+ ScopedTypeVariables, TypeInType #-}
6module Data.Type.Set (Set(..), Union, Unionable, union, quicksort, append,
6module Data.Type.Set (Set(..), Union, Unionable, union, quicksort, append,
7 Sort, Sortable, (:++), Split(..), Cmp, Filter, Flag(..),
7 Sort, Sortable, (:++), Split(..), Cmp, Filter, Flag(..),
8 Nub, Nubable(..), AsSet, asSet, IsSet, Subset(..),
8 Nub, Nubable(..), AsSet, asSet, IsSet, Subset(..),
9− Delete(..), Proxy(..)) where
9+ Delete(..), Proxy(..), remove, Remove, (:\),
10+ Member(..), NonMember, MemberP(..)) where
11import GHC.TypeLits
12import GHC.TypeLits
12import Data.Type.Bool
13import Data.Type.Bool
@@ -15,7 +16,7 @@ import Data.Type.Equality
15data Proxy (p :: k) = Proxy
16data Proxy (p :: k) = Proxy
17-- Value-level 'Set' representation, essentially a list
18-- Value-level 'Set' representation, essentially a list
18−data Set (n :: [*]) where
19+data Set (n :: [k]) where
19 {--| Construct an empty set -}
20 {--| Construct an empty set -}
20 Empty :: Set '[]
21 Empty :: Set '[]
21 {--| Extend a set with an element -}
22 {--| Extend a set with an element -}
@@ -73,6 +74,25 @@ append :: Set s -> Set t -> Set (s :++ t)
73append Empty x = x
74append Empty x = x
74append (Ext e xs) ys = Ext e (append xs ys)
75append (Ext e xs) ys = Ext e (append xs ys)
77+{-| Delete elements from a set -}
78+type family (m :: [k]) :\ (x :: k) :: [k] where
80+ (x ': xs) :\ x = xs :\ x
81+ (y ': xs) :\ x = y ': (xs :\ x)
83+class Remove s t where
84+ remove :: Set s -> Proxy t -> Set (s :\ t)
86+instance Remove '[] t where
87+ remove Empty Proxy = Empty
89+instance {-# OVERLAPS #-} Remove xs x => Remove (x ': xs) x where
90+ remove (Ext _ xs) x@Proxy = remove xs x
92+instance {-# OVERLAPPABLE #-} (((y : xs) :\ x) ~ (y : (xs :\ x)), Remove xs x)
93+ => Remove (y ': xs) x where
94+ remove (Ext y xs) (x@Proxy) = Ext y (remove xs x)
76{-| Splitting a union a set, given the sets we want to split it into -}
96{-| Splitting a union a set, given the sets we want to split it into -}
77class Split s t st where
97class Split s t st where
78 -- where st ~ Union s t
98 -- where st ~ Union s t
@@ -93,8 +113,6 @@ instance {-# OVERLAPS #-} (Split s t st) => Split s (x ': t) (x ': st) where
93 split (Ext x st) = let (s, t) = split st
113 split (Ext x st) = let (s, t) = split st
94 in (s, Ext x t)
114 in (s, Ext x t)
98{-| Remove duplicates from a sorted list -}
116{-| Remove duplicates from a sorted list -}
99type family Nub t where
117type family Nub t where
100 Nub '[] = '[]
118 Nub '[] = '[]
@@ -130,10 +148,10 @@ class Subset s t where
130instance Subset '[] '[] where
148instance Subset '[] '[] where
131 subset xs = Empty
149 subset xs = Empty
133−instance Subset s t => Subset s (x ': t) where
151+instance {-# OVERLAPPABLE #-} Subset s t => Subset s (x ': t) where
134 subset (Ext _ xs) = subset xs
152 subset (Ext _ xs) = subset xs
136−instance Subset s t => Subset (x ': s) (x ': t) where
154+instance {-# OVERLAPS #-} Subset s t => Subset (x ': s) (x ': t) where
137 subset (Ext x xs) = Ext x (subset xs)
155 subset (Ext x xs) = Ext x (subset xs)
@@ -198,3 +216,21 @@ instance Conder False where
198{-| Open-family for the ordering operation in the sort -}
216{-| Open-family for the ordering operation in the sort -}
200type family Cmp (a :: k) (b :: k) :: Ordering
218type family Cmp (a :: k) (b :: k) :: Ordering
220+{-| Membership of an element in a set, with an acommanying
221+ value-level function that returns a bool -}
222+class Member a s where
223+ member :: Proxy a -> Set s -> Bool
225+instance {-# OVERLAPS #-} Member a (a ': s) where
226+ member _ (Ext x _) = True
228+instance {-# OVERLAPPABLE #-} Member a s => Member a (b ': s) where
229+ member a (Ext _ xs) = member a xs
231+type family MemberP a s :: Bool where
232+ MemberP a '[] = False
233+ MemberP a (a ': s) = True
234+ MemberP a (b ': s) = MemberP a s
236+type NonMember a s = MemberP a s ~ False