@@ -1,32 +1,27 @@
1−{-# LANGUAGE GADTs, DataKinds, KindSignatures, TypeOperators, TypeFamilies,
1+{-# LANGUAGE GADTs, DataKinds, KindSignatures, TypeOperators, TypeFamilies,
2− MultiParamTypeClasses, FlexibleInstances, PolyKinds,
2+ MultiParamTypeClasses, FlexibleInstances, PolyKinds, FlexibleContexts,
3− FlexibleContexts, UndecidableInstances, ConstraintKinds,
3+ UndecidableInstances, ConstraintKinds, OverlappingInstances, ScopedTypeVariables #-}
4− ScopedTypeVariables, TypeInType #-}
6−module Data.Type.Set (Set(..), Union, Unionable, union, quicksort, append,
5+module Data.Type.Set (Set(..), Union, Unionable, union, quicksort, append,
7− Sort, Sortable, (:++), Split(..), Cmp, Filter, Flag(..),
6+ Sort, Sortable, Append(..), Split(..), Cmp,
8 Nub, Nubable(..), AsSet, asSet, IsSet, Subset(..),
7 Nub, Nubable(..), AsSet, asSet, IsSet, Subset(..),
9− Delete(..), Proxy(..), remove, Remove, (:\),
8+ (:->)(..), Var(..)) where
10− Member(..), NonMember, MemberP(..)) where
12import GHC.TypeLits
10import GHC.TypeLits
13import Data.Type.Bool
11import Data.Type.Bool
14import Data.Type.Equality
12import Data.Type.Equality
16−data Proxy (p :: k) = Proxy
15+{-| Core Set definition, in terms of lists -}
18−-- Value-level 'Set' representation, essentially a list
19−data Set (n :: [k]) where
16+data Set (n :: [*]) where
20− {--| Construct an empty set -}
21 Empty :: Set '[]
17 Empty :: Set '[]
22− {--| Extend a set with an element -}
23 Ext :: e -> Set s -> Set (e ': s)
18 Ext :: e -> Set s -> Set (e ': s)
25instance Show (Set '[]) where
20instance Show (Set '[]) where
26 show Empty = "{}"
21 show Empty = "{}"
28instance (Show e, Show' (Set s)) => Show (Set (e ': s)) where
23instance (Show e, Show' (Set s)) => Show (Set (e ': s)) where
29− show (Ext e s) = "{" ++ show e ++ (show' s) ++ "}"
24+ show (Ext e s) = "{" ++ show e ++ (show' s) ++ "}"
31class Show' t where
26class Show' t where
32 show' :: t -> String
27 show' :: t -> String
@@ -35,10 +30,6 @@ instance Show' (Set '[]) where
35instance (Show' (Set s), Show e) => Show' (Set (e ': s)) where
30instance (Show' (Set s), Show e) => Show' (Set (e ': s)) where
36 show' (Ext e s) = ", " ++ show e ++ (show' s)
31 show' (Ext e s) = ", " ++ show e ++ (show' s)
38−instance (Eq e, Eq (Set s)) => Eq (Set (e ': s)) where
39− (Ext e m) == (Ext e' m') = e == e' && m == m'
42{-| At the type level, normalise the list form to the set form -}
33{-| At the type level, normalise the list form to the set form -}
43type AsSet s = Nub (Sort s)
34type AsSet s = Nub (Sort s)
@@ -50,68 +41,54 @@ asSet x = nub (quicksort x)
50type IsSet s = (s ~ Nub (Sort s))
41type IsSet s = (s ~ Nub (Sort s))
52{-| Useful properties to be able to refer to someties -}
43{-| Useful properties to be able to refer to someties -}
53−type SetProperties f = (Union f '[] ~ f, Split f '[] f,
44+type SetProperties f = (Union f '[] ~ f, Split f '[] f,
54− Union '[] f ~ f, Split '[] f f,
45+ Union '[] f ~ f, Split '[] f f,
55 Union f f ~ f, Split f f f,
46 Union f f ~ f, Split f f f,
56 Unionable f '[], Unionable '[] f)
47 Unionable f '[], Unionable '[] f)
58{-- Union --}
49{-- Union --}
60{-| Union of sets -}
51{-| Union of sets -}
61−type Union s t = Nub (Sort (s :++ t))
52+type Union s t = Nub (Sort (Append s t))
63union :: (Unionable s t) => Set s -> Set t -> Set (Union s t)
54union :: (Unionable s t) => Set s -> Set t -> Set (Union s t)
64union s t = nub (quicksort (append s t))
55union s t = nub (quicksort (append s t))
66−type Unionable s t = (Sortable (s :++ t), Nubable (Sort (s :++ t)))
57+type Unionable s t = (Sortable (Append s t), Nubable (Sort (Append s t)))
68{-| List append (essentially set disjoint union) -}
59{-| List append (essentially set disjoint union) -}
69−type family (:++) (x :: [k]) (y :: [k]) :: [k] where
60+type family Append (s :: [k]) (t :: [k]) :: [k] where
70− '[] :++ xs = xs
61+ Append '[] t = t
71− (x ': xs) :++ ys = x ': (xs :++ ys)
62+ Append (x ': xs) ys = x ': (Append xs ys)
73−append :: Set s -> Set t -> Set (s :++ t)
64+append :: Set s -> Set t -> Set (Append s t)
74append Empty x = x
65append Empty x = x
75append (Ext e xs) ys = Ext e (append xs ys)
66append (Ext e xs) ys = Ext e (append xs ys)
77−{-| Delete elements from a set -}
68+{-| Useful alias for append -}
78−type family (m :: [k]) :\ (x :: k) :: [k] where
69+type (s :: [k]) :++ (t :: [k]) = Append s t
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)
96{-| Splitting a union a set, given the sets we want to split it into -}
71{-| Splitting a union a set, given the sets we want to split it into -}
97class Split s t st where
72class Split s t st where
98 -- where st ~ Union s t
73 -- where st ~ Union s t
99− split :: Set st -> (Set s, Set t)
74+ split :: Set st -> (Set s, Set t)
101instance Split '[] '[] '[] where
76instance Split '[] '[] '[] where
102 split Empty = (Empty, Empty)
77 split Empty = (Empty, Empty)
104−instance {-# OVERLAPPABLE #-} Split s t st => Split (x ': s) (x ': t) (x ': st) where
79+instance Split s t st => Split (x ': s) (x ': t) (x ': st) where
105 split (Ext x st) = let (s, t) = split st
80 split (Ext x st) = let (s, t) = split st
106 in (Ext x s, Ext x t)
81 in (Ext x s, Ext x t)
108−instance {-# OVERLAPS #-} Split s t st => Split (x ': s) t (x ': st) where
83+instance Split s t st => Split (x ': s) t (x ': st) where
109 split (Ext x st) = let (s, t) = split st
84 split (Ext x st) = let (s, t) = split st
110− in (Ext x s, t)
85+ in (Ext x s, t)
112−instance {-# OVERLAPS #-} (Split s t st) => Split s (x ': t) (x ': st) where
87+instance (Split s t st) => Split s (x ': t) (x ': st) where
113 split (Ext x st) = let (s, t) = split st
88 split (Ext x st) = let (s, t) = split st
114− in (s, Ext x t)
89+ in (s, Ext x t)
116{-| Remove duplicates from a sorted list -}
93{-| Remove duplicates from a sorted list -}
117type family Nub t where
94type family Nub t where
@@ -121,7 +98,7 @@ type family Nub t where
121 Nub (e ': f ': s) = e ': Nub (f ': s)
98 Nub (e ': f ': s) = e ': Nub (f ': s)
123{-| Value-level counterpart to the type-level 'Nub'
100{-| Value-level counterpart to the type-level 'Nub'
124− Note: the value-level case for equal types is not define here,
101+ Note: the value-level case for equal types is not define here,
125 but should be given per-application, e.g., custom 'merging' behaviour may be required -}
102 but should be given per-application, e.g., custom 'merging' behaviour may be required -}
127class Nubable t where
104class Nubable t where
@@ -136,7 +113,7 @@ instance Nubable '[e] where
136instance Nubable (e ': s) => Nubable (e ': e ': s) where
113instance Nubable (e ': s) => Nubable (e ': e ': s) where
137 nub (Ext _ (Ext e s)) = nub (Ext e s)
114 nub (Ext _ (Ext e s)) = nub (Ext e s)
139−instance {-# OVERLAPS #-} (Nub (e ': f ': s) ~ (e ': Nub (f ': s)),
116+instance (Nub (e ': f ': s) ~ (e ': Nub (f ': s)),
140 Nubable (f ': s)) => Nubable (e ': f ': s) where
117 Nubable (f ': s)) => Nubable (e ': f ': s) where
141 nub (Ext e (Ext f s)) = Ext e (nub (Ext f s))
118 nub (Ext e (Ext f s)) = Ext e (nub (Ext f s))
@@ -145,13 +122,13 @@ instance {-# OVERLAPS #-} (Nub (e ': f ': s) ~ (e ': Nub (f ': s)),
145class Subset s t where
122class Subset s t where
146 subset :: Set t -> Set s
123 subset :: Set t -> Set s
148−instance Subset '[] '[] where
125+instance Subset '[] '[] where
149 subset xs = Empty
126 subset xs = Empty
151−instance {-# OVERLAPPABLE #-} Subset s t => Subset s (x ': t) where
128+instance Subset '[] (x ': t) where
152− subset (Ext _ xs) = subset xs
129+ subset xs = Empty
154−instance {-# OVERLAPS #-} Subset s t => Subset (x ': s) (x ': t) where
131+instance Subset s t => Subset (x ': s) (x ': t) where
155 subset (Ext x xs) = Ext x (subset xs)
132 subset (Ext x xs) = Ext x (subset xs)
@@ -164,17 +141,8 @@ data Flag = FMin | FMax
165type family Filter (f :: Flag) (p :: k) (xs :: [k]) :: [k] where
142type family Filter (f :: Flag) (p :: k) (xs :: [k]) :: [k] where
166 Filter f p '[] = '[]
143 Filter f p '[] = '[]
167− Filter FMin p (x ': xs) = If (Cmp x p == LT) (x ': (Filter FMin p xs)) (Filter FMin p xs)
144+ Filter FMin p (x ': xs) = If (Cmp x p == LT) (x ': (Filter FMin p xs)) (Filter FMin p xs)
168− Filter FMax p (x ': xs) = If (Cmp x p == GT || Cmp x p == EQ) (x ': (Filter FMax p xs)) (Filter FMax p xs)
145+ Filter FMax p (x ': xs) = If (Cmp x p == GT || Cmp x p == EQ) (x ': (Filter FMax p xs)) (Filter FMax p xs)
170−type family DeleteFromList (e :: elem) (list :: [elem]) where
171− DeleteFromList elem '[] = '[]
172− DeleteFromList elem (x ': xs) = If (Cmp elem x == EQ)
174− (x ': DeleteFromList elem xs)
176−type family Delete elem set where
177− Delete elem (Set xs) = Set (DeleteFromList elem xs)
179{-| Value-level quick sort that respects the type-level ordering -}
147{-| Value-level quick sort that respects the type-level ordering -}
180class Sortable xs where
148class Sortable xs where
@@ -198,11 +166,11 @@ instance FilterV f p '[] where
199instance (Conder ((Cmp x p) == LT), FilterV FMin p xs) => FilterV FMin p (x ': xs) where
167instance (Conder ((Cmp x p) == LT), FilterV FMin p xs) => FilterV FMin p (x ': xs) where
200 filterV f@Proxy p (Ext x xs) = cond (Proxy::(Proxy ((Cmp x p) == LT)))
168 filterV f@Proxy p (Ext x xs) = cond (Proxy::(Proxy ((Cmp x p) == LT)))
201− (Ext x (filterV f p xs)) (filterV f p xs)
169+ (Ext x (filterV f p xs)) (filterV f p xs)
203instance (Conder (((Cmp x p) == GT) || ((Cmp x p) == EQ)), FilterV FMax p xs) => FilterV FMax p (x ': xs) where
171instance (Conder (((Cmp x p) == GT) || ((Cmp x p) == EQ)), FilterV FMax p xs) => FilterV FMax p (x ': xs) where
204 filterV f@Proxy p (Ext x xs) = cond (Proxy::(Proxy (((Cmp x p) == GT) || ((Cmp x p) == EQ))))
172 filterV f@Proxy p (Ext x xs) = cond (Proxy::(Proxy (((Cmp x p) == GT) || ((Cmp x p) == EQ))))
205− (Ext x (filterV f p xs)) (filterV f p xs)
173+ (Ext x (filterV f p xs)) (filterV f p xs)
207class Conder g where
175class Conder g where
208 cond :: Proxy g -> Set s -> Set t -> Set (If g s t)
176 cond :: Proxy g -> Set s -> Set t -> Set (If g s t)
@@ -217,20 +185,30 @@ instance Conder False where
218type family Cmp (a :: k) (b :: k) :: Ordering
186type family Cmp (a :: k) (b :: k) :: Ordering
220−{-| Membership of an element in a set, with an acommanying
188+{-| Pair a symbol (represetning a variable) with a type -}
221− value-level function that returns a bool -}
189+infixl 2 :->
222−class Member a s where
190+data (k :: Symbol) :-> (v :: *) = (Var k) :-> v
223− member :: Proxy a -> Set s -> Bool
191+
224−
192+data Var (k :: Symbol) where Var :: Var k
225−instance {-# OVERLAPS #-} Member a (a ': s) where
193+ {-| Some special defaults for some common names -}
226− member _ (Ext x _) = True
194+ X :: Var "x"
228−instance {-# OVERLAPPABLE #-} Member a s => Member a (b ': s) where
196+ Z :: Var "z"
229− member a (Ext _ xs) = member a xs
197+
231−type family MemberP a s :: Bool where
199+instance (Show (Var k), Show v) => Show (k :-> v) where
232− MemberP a '[] = False
200+ show (k :-> v) = "(" ++ show k ++ " :-> " ++ show v ++ ")"
233− MemberP a (a ': s) = True
201+instance Show (Var "x") where
234− MemberP a (b ': s) = MemberP a s
202+ show X = "x"
235−
203+ show Var = "Var"
236−type NonMember a s = MemberP a s ~ False
204+instance Show (Var "y") where
207+instance Show (Var "z") where
210+instance Show (Var v) where
213+{-| Symbol comparison -}
214+type instance Cmp (v :-> a) (u :-> b) = CmpSymbol v u