git.iko.soy/type-level-sets
on master help sign in
added EQ instances
commit3cca261595ab1b25ef427ad456199bb6e68da08a authordominic <dom.orchard@gmail.com> dateSun 4 Sep 2016, 21:27 (10 years ago) parent850220a
4 files changed+33−18
src/Data/Type/Map.hs+7−1 src/Data/Type/Set.hs+4 stack.yaml+1−1 type-level-sets.cabal+21−16
src/Data/Type/Map.hs+7−1
@@ -98,6 +98,12 @@ instance Show' (Map '[]) where
9898instance (KnownSymbol k, Show v, Show' (Map s)) => Show' (Map ((k :-> v) ': s)) where
9999 show' (Ext k v s) = ", " ++ show k ++ " :-> " ++ show v ++ (show' s)
100100
101+instance Eq (Map '[]) where
102+ Empty == Empty = True
103+
104+instance (KnownSymbol k, Eq (Var k), Eq v, Eq (Map s)) => Eq (Map ((k :-> v) ': s)) where
105+ (Ext k v m) == (Ext k' v' m') = k == k' && v == v' && m == m'
106+
101107{-| Union of two finite maps -}
102108union :: (Unionable s t) => Map s -> Map t -> Map (Union s t)
103109union s t = nub (quicksort (append s t))
@@ -151,7 +157,7 @@ instance Nubable '[] where
151157instance Nubable '[e] where
152158 nub (Ext k v Empty) = Ext k v Empty
153159
154−instance {-# OVERLAPABLE #-}
160+instance {-# OVERLAPPABLE #-}
155161 (Nub (e ': f ': s) ~ (e ': Nub (f ': s)),
156162 Nubable (f ': s)) => Nubable (e ': f ': s) where
157163 nub (Ext k v (Ext k' v' s)) = Ext k v (nub (Ext k' v' s))
src/Data/Type/Set.hs+4
@@ -34,6 +34,10 @@ instance Show' (Set '[]) where
3434instance (Show' (Set s), Show e) => Show' (Set (e ': s)) where
3535 show' (Ext e s) = ", " ++ show e ++ (show' s)
3636
37+instance (Eq e, Eq (Set s)) => Eq (Set (e ': s)) where
38+ (Ext e m) == (Ext e' m') = e == e' && m == m'
39+
40+
3741{-| At the type level, normalise the list form to the set form -}
3842type AsSet s = Nub (Sort s)
3943
stack.yaml+1−1
@@ -1,7 +1,7 @@
11# For more information, see: https://github.com/commercialhaskell/stack/wiki/stack.yaml
22
33# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
4−resolver: lts-3.5
4+resolver: nightly-2016-07-18
55
66# Local packages, usually specified by relative directory name
77packages:
type-level-sets.cabal+21−16
@@ -1,5 +1,5 @@
11name: type-level-sets
2−version: 0.7
2+version: 0.8.0.0
33synopsis: Type-level sets and finite maps (with value-level counterparts)
44description:
55 This package provides type-level sets (no duplicates, sorted to provide a normal form) via 'Set' and type-level
@@ -8,32 +8,37 @@ description:
88 Described in the paper \"Embedding effect systems in Haskell\" by Dominic Orchard
99 and Tomas Petricek <http://www.cl.cam.ac.uk/~dao29/publ/haskell14-effects.pdf> (Haskell Symposium, 2014). This version now uses Quicksort to normalise the representation.
1010 .
11− Here is a brief example for finite maps.:
11+ Here is a brief example for finite maps:
1212 .
1313 >
1414 > import Data.Type.Map
1515 >
16− > foo :: Map '["x" :-> Int, "z" :-> Int, "w" :-> Int]
17− > foo = Ext ((Var :: (Var "x")) :-> 2) $
18− > Ext ((Var :: (Var "z")) :-> 4) $
19− > Ext ((Var :: (Var "w")) :-> 5) $
20− > Empty
16+ > -- Specify how to combine duplicate key-value pairs for Int values
17+ > type instance Combine Int Int = Int
18+ > instance Combinable Int Int where
19+ > combine x y = x + y
20+ >
21+ > foo :: Map '["x" :-> Int, "z" :-> Bool, "w" :-> Int]
22+ > foo = Ext (Var :: (Var "x")) 2
23+ > $ Ext (Var :: (Var "z")) True
24+ > $ Ext (Var :: (Var "w")) 5
25+ > $ Empty
2126 >
2227 > bar :: Map '["y" :-> Int, "w" :-> Int]
23− > bar = Ext ((Var :: (Var "y")) :-> 3) $
24− > Ext ((Var :: (Var "w")) :-> 1) $
25− > Empty
28+ > bar = Ext (Var :: (Var "y")) 3
29+ > $ Ext (Var :: (Var "w")) 1
30+ > $ Empty
2631 >
27− > -- foobar :: Map '["w" :-> Int, "x" :-> Int, "y" :-> Int, "z" :-> Int]
32+ > -- foobar :: Map '["w" :-> Int, "x" :-> Int, "y" :-> Int, "z" :-> Bool]
2833 > foobar = foo `union` bar
2934 .
30− The 'Set' type for 'foobar' here shows the normalised form (sorted with no duplicates).
35+ The 'Map' type for 'foobar' here shows the normalised form (sorted with no duplicates).
3136 The type signatures is commented out as it can be infered. Running the example we get:
3237 .
3338 > >>> foobar
34− > [(Var :-> 1), (Var :-> 2), (Var :-> 3), (Var :-> 4)]
39+ > {w :-> 6, x :-> 2, y :-> 3, z :-> True}
3540 .
36− Thus, we see that the first value paired with the \"w\" variable is dropped.
41+ Thus, we see that the values for \"w\" are added together.
3742 For sets, here is an example:
3843 .
3944 > import GHC.TypeLits
@@ -64,9 +69,9 @@ maintainer: Dominic Orchard
6469stability: experimental
6570build-type: Simple
6671cabal-version: >= 1.6
67−tested-with: GHC == 7.10.3
72+tested-with: GHC >= 8.0.1
6873
69−extra-source-files: example.hs
74+extra-source-files: example.hs, example2.hs
7075
7176
7277source-repository head