Skip to content
Newer
Older
100644 308 lines (259 sloc) 10.8 KB
00ae7d1 @ekmett added universal quantification of constraints via instances for unexp…
authored Nov 6, 2011
1 {-# LANGUAGE FunctionalDependencies #-}
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
2 {-# LANGUAGE UndecidableInstances #-}
00ae7d1 @ekmett added universal quantification of constraints via instances for unexp…
authored Nov 6, 2011
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE FlexibleContexts #-}
7 {-# LANGUAGE ConstraintKinds #-}
8 {-# LANGUAGE EmptyDataDecls #-}
9 {-# LANGUAGE KindSignatures #-}
10 {-# LANGUAGE TypeOperators #-}
c06e884 @ekmett updated to work with ghc 7.4
authored Dec 29, 2011
11 {-# LANGUAGE Trustworthy #-}
00ae7d1 @ekmett added universal quantification of constraints via instances for unexp…
authored Nov 6, 2011
12 {-# LANGUAGE Rank2Types #-}
13 {-# LANGUAGE GADTs #-}
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
14
4dc5350 @ekmett better constraints
authored Nov 2, 2011
15 module Data.Constraint
16 (
c06e884 @ekmett updated to work with ghc 7.4
authored Dec 29, 2011
17 -- * Constraints
18 Constraint
4dc5350 @ekmett better constraints
authored Nov 2, 2011
19 -- * Dictionary
c06e884 @ekmett updated to work with ghc 7.4
authored Dec 29, 2011
20 , Dict(Dict)
4dc5350 @ekmett better constraints
authored Nov 2, 2011
21 -- * Entailment
22 , (:-)(Sub)
23 , (\\)
24 , weaken1, weaken2, contract
25 , (&&&), (***)
26 , trans, refl
27 , top
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
28 -- * Reflection
29 , Class(..)
30 , (:=>)(..)
4dc5350 @ekmett better constraints
authored Nov 2, 2011
31 ) where
32
33 import Control.Monad
34 import Control.Applicative
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
35 import Data.Monoid
36 import Data.Complex
37 import Data.Ratio
c06e884 @ekmett updated to work with ghc 7.4
authored Dec 29, 2011
38 import GHC.Prim (Constraint)
4dc5350 @ekmett better constraints
authored Nov 2, 2011
39
844de69 @ekmett standalone deriving
authored Nov 3, 2011
40 -- | Capture a dictionary for a given constraint
4dc5350 @ekmett better constraints
authored Nov 2, 2011
41 data Dict :: Constraint -> * where
42 Dict :: a => Dict a
43
844de69 @ekmett standalone deriving
authored Nov 3, 2011
44 deriving instance Eq (Dict a)
45 deriving instance Ord (Dict a)
46 deriving instance Show (Dict a)
4dc5350 @ekmett better constraints
authored Nov 2, 2011
47
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
48 infixr 9 :-
2a07dee @ekmett removed more explicit Constraint kinds, updated the type family version
authored Nov 3, 2011
49 newtype a :- b = Sub (a => Dict b)
4dc5350 @ekmett better constraints
authored Nov 2, 2011
50
6add7fd @ekmett removed unnecessary kind annotations
authored Nov 3, 2011
51 instance Eq (a :- b) where
52 _ == _ = True
4dc5350 @ekmett better constraints
authored Nov 2, 2011
53
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
54 instance Ord (a :- b) where
6add7fd @ekmett removed unnecessary kind annotations
authored Nov 3, 2011
55 compare _ _ = EQ
4dc5350 @ekmett better constraints
authored Nov 2, 2011
56
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
57 instance Show (a :- b) where
6add7fd @ekmett removed unnecessary kind annotations
authored Nov 3, 2011
58 showsPrec d _ = showParen (d > 10) $ showString "Sub Dict"
4dc5350 @ekmett better constraints
authored Nov 2, 2011
59
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
60 infixl 1 \\ -- required comment
844de69 @ekmett standalone deriving
authored Nov 3, 2011
61
62 -- | Given that @a :- b@, derive something that needs a context @b@, using the context @a@
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
63 (\\) :: a => (b => r) -> (a :- b) -> r
64 r \\ Sub Dict = r
65
844de69 @ekmett standalone deriving
authored Nov 3, 2011
66 -- | due to the hack for the kind of (,) in the current version of GHC we can't actually
67 -- make instances for (,) :: Constraint -> Constraint -> Constraint
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
68 (***) :: (a :- b) -> (c :- d) -> (a, c) :- (b, d)
69 f *** g = Sub $ Dict \\ f \\ g
4dc5350 @ekmett better constraints
authored Nov 2, 2011
70
844de69 @ekmett standalone deriving
authored Nov 3, 2011
71 -- | Weakening a constraint product
72 weaken1 :: (a, b) :- a
4dc5350 @ekmett better constraints
authored Nov 2, 2011
73 weaken1 = Sub Dict
74
844de69 @ekmett standalone deriving
authored Nov 3, 2011
75 -- | Weakening a constraint product
76 weaken2 :: (a, b) :- b
4dc5350 @ekmett better constraints
authored Nov 2, 2011
77 weaken2 = Sub Dict
78
844de69 @ekmett standalone deriving
authored Nov 3, 2011
79 -- | Contracting a constraint / diagonal morphism
4dc5350 @ekmett better constraints
authored Nov 2, 2011
80 contract :: a :- (a, a)
81 contract = Sub Dict
82
844de69 @ekmett standalone deriving
authored Nov 3, 2011
83 -- | Constraint product
84 --
85 -- > trans weaken1 (f &&& g) = f
86 -- > trans weaken2 (f &&& g) = g
87 (&&&) :: (a :- b) -> (a :- c) -> a :- (b, c)
4dc5350 @ekmett better constraints
authored Nov 2, 2011
88 f &&& g = Sub $ Dict \\ f \\ g
89
844de69 @ekmett standalone deriving
authored Nov 3, 2011
90 -- ?
91 -- / \
92 -- (#) ?? ???
93 -- / \ / \
94 -- # * Constraint
95
96 -- | Transitivity of entailment
97 --
98 -- If we view '(:-)' as a Constraint-indexed category, then this is '(.)'
4dc5350 @ekmett better constraints
authored Nov 2, 2011
99 trans :: (b :- c) -> (a :- b) -> a :- c
100 trans f g = Sub $ Dict \\ f \\ g
101
844de69 @ekmett standalone deriving
authored Nov 3, 2011
102 -- | Reflexivity of entailment
103 --
104 -- If we view '(:-)' as a Constraint-indexed category, then this is 'id'
4dc5350 @ekmett better constraints
authored Nov 2, 2011
105 refl :: a :- a
106 refl = Sub Dict
107
844de69 @ekmett standalone deriving
authored Nov 3, 2011
108 -- | Every constraint implies truth
109 --
110 -- These are the terminal arrows of the category, and () is the terminal object.
4dc5350 @ekmett better constraints
authored Nov 2, 2011
111 top :: a :- ()
112 top = Sub Dict
113
6add7fd @ekmett removed unnecessary kind annotations
authored Nov 3, 2011
114 class Class b h | h -> b where
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
115 cls :: h :- b
4dc5350 @ekmett better constraints
authored Nov 2, 2011
116
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
117 infixr 9 :=>
6add7fd @ekmett removed unnecessary kind annotations
authored Nov 3, 2011
118 class b :=> h | h -> b where
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
119 ins :: b :- h
4dc5350 @ekmett better constraints
authored Nov 2, 2011
120
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
121 instance Class () (Class b a) where cls = Sub Dict
122 instance Class () (b :=> a) where cls = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
123
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
124 instance Class b a => () :=> Class b a where ins = Sub Dict
125 instance (b :=> a) => () :=> b :=> a where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
126
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
127 instance Class () () where cls = Sub Dict
128 instance () :=> () where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
129
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
130 -- Local, Prelude, Applicative, C.M.I and Data.Monoid instances
4dc5350 @ekmett better constraints
authored Nov 2, 2011
131
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
132 -- Eq
133 instance Class () (Eq a) where cls = Sub Dict
134 instance () :=> Eq () where ins = Sub Dict
135 instance () :=> Eq Int where ins = Sub Dict
136 instance () :=> Eq Bool where ins = Sub Dict
137 instance () :=> Eq Integer where ins = Sub Dict
138 instance () :=> Eq Float where ins = Sub Dict
139 instance () :=> Eq Double where ins = Sub Dict
140 instance Eq a :=> Eq [a] where ins = Sub Dict
141 instance Eq a :=> Eq (Maybe a) where ins = Sub Dict
142 instance Eq a :=> Eq (Complex a) where ins = Sub Dict
143 instance Eq a :=> Eq (Ratio a) where ins = Sub Dict
144 instance (Eq a, Eq b) :=> Eq (a, b) where ins = Sub Dict
145 instance (Eq a, Eq b) :=> Eq (Either a b) where ins = Sub Dict
146 instance () :=> Eq (Dict a) where ins = Sub Dict
147 instance () :=> Eq (a :- b) where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
148
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
149 -- Ord
150 instance Class (Eq a) (Ord a) where cls = Sub Dict
151 instance () :=> Ord () where ins = Sub Dict
152 instance () :=> Ord Bool where ins = Sub Dict
153 instance () :=> Ord Int where ins = Sub Dict
154 instance ():=> Ord Integer where ins = Sub Dict
155 instance () :=> Ord Float where ins = Sub Dict
156 instance ():=> Ord Double where ins = Sub Dict
157 instance () :=> Ord Char where ins = Sub Dict
158 instance Ord a :=> Ord (Maybe a) where ins = Sub Dict
159 instance Ord a :=> Ord [a] where ins = Sub Dict
160 instance (Ord a, Ord b) :=> Ord (a, b) where ins = Sub Dict
161 instance (Ord a, Ord b) :=> Ord (Either a b) where ins = Sub Dict
162 instance Integral a :=> Ord (Ratio a) where ins = Sub Dict
163 instance () :=> Ord (Dict a) where ins = Sub Dict
164 instance () :=> Ord (a :- b) where ins = Sub Dict
165
166 instance Class () (Show a) where cls = Sub Dict
167 instance () :=> Show () where ins = Sub Dict
168 instance () :=> Show Bool where ins = Sub Dict
169 instance () :=> Show Ordering where ins = Sub Dict
170 instance () :=> Show Char where ins = Sub Dict
171 instance Show a :=> Show (Complex a) where ins = Sub Dict
172 instance Show a :=> Show [a] where ins = Sub Dict
173 instance Show a :=> Show (Maybe a) where ins = Sub Dict
174 instance (Show a, Show b) :=> Show (a, b) where ins = Sub Dict
175 instance (Show a, Show b) :=> Show (Either a b) where ins = Sub Dict
176 instance (Integral a, Show a) :=> Show (Ratio a) where ins = Sub Dict
177 instance () :=> Show (Dict a) where ins = Sub Dict
178 instance () :=> Show (a :- b) where ins = Sub Dict
179
180 instance Class () (Read a) where cls = Sub Dict
181 instance () :=> Read () where ins = Sub Dict
182 instance () :=> Read Bool where ins = Sub Dict
183 instance () :=> Read Ordering where ins = Sub Dict
184 instance () :=> Read Char where ins = Sub Dict
185 instance Read a :=> Read (Complex a) where ins = Sub Dict
186 instance Read a :=> Read [a] where ins = Sub Dict
187 instance Read a :=> Read (Maybe a) where ins = Sub Dict
188 instance (Read a, Read b) :=> Read (a, b) where ins = Sub Dict
189 instance (Read a, Read b) :=> Read (Either a b) where ins = Sub Dict
190 instance (Integral a, Read a) :=> Read (Ratio a) where ins = Sub Dict
191
192 instance Class () (Enum a) where cls = Sub Dict
193 instance () :=> Enum () where ins = Sub Dict
194 instance () :=> Enum Bool where ins = Sub Dict
195 instance () :=> Enum Ordering where ins = Sub Dict
196 instance () :=> Enum Char where ins = Sub Dict
197 instance () :=> Enum Int where ins = Sub Dict
198 instance () :=> Enum Integer where ins = Sub Dict
199 instance () :=> Enum Float where ins = Sub Dict
200 instance () :=> Enum Double where ins = Sub Dict
201 instance Integral a :=> Enum (Ratio a) where ins = Sub Dict
202
203 instance Class () (Bounded a) where cls = Sub Dict
204 instance () :=> Bounded () where ins = Sub Dict
205 instance () :=> Bounded Ordering where ins = Sub Dict
206 instance () :=> Bounded Bool where ins = Sub Dict
207 instance () :=> Bounded Int where ins = Sub Dict
208 instance () :=> Bounded Char where ins = Sub Dict
209 instance (Bounded a, Bounded b) :=> Bounded (a,b) where ins = Sub Dict
210
211 instance Class () (Num a) where cls = Sub Dict
212 instance () :=> Num Int where ins = Sub Dict
213 instance () :=> Num Integer where ins = Sub Dict
214 instance () :=> Num Float where ins = Sub Dict
215 instance () :=> Num Double where ins = Sub Dict
216 instance RealFloat a :=> Num (Complex a) where ins = Sub Dict
217 instance Integral a :=> Num (Ratio a) where ins = Sub Dict
218
219 instance Class (Num a, Ord a) (Real a) where cls = Sub Dict
220 instance () :=> Real Int where ins = Sub Dict
221 instance () :=> Real Integer where ins = Sub Dict
222 instance () :=> Real Float where ins = Sub Dict
223 instance () :=> Real Double where ins = Sub Dict
224 instance Integral a :=> Real (Ratio a) where ins = Sub Dict
225
226 instance Class (Real a, Enum a) (Integral a) where cls = Sub Dict
227 instance () :=> Integral Int where ins = Sub Dict
228 instance () :=> Integral Integer where ins = Sub Dict
229
230 instance Class (Num a) (Fractional a) where cls = Sub Dict
231 instance () :=> Fractional Float where ins = Sub Dict
232 instance () :=> Fractional Double where ins = Sub Dict
233 instance RealFloat a :=> Fractional (Complex a) where ins = Sub Dict
234 instance Integral a :=> Fractional (Ratio a) where ins = Sub Dict
235
236 instance Class (Fractional a) (Floating a) where cls = Sub Dict
237 instance () :=> Floating Float where ins = Sub Dict
238 instance () :=> Floating Double where ins = Sub Dict
239 instance RealFloat a :=> Floating (Complex a) where ins = Sub Dict
240
241 instance Class (Real a, Fractional a) (RealFrac a) where cls = Sub Dict
242 instance () :=> RealFrac Float where ins = Sub Dict
243 instance () :=> RealFrac Double where ins = Sub Dict
244 instance Integral a :=> RealFrac (Ratio a) where ins = Sub Dict
245
246 instance Class (RealFrac a, Floating a) (RealFloat a) where cls = Sub Dict
247 instance () :=> RealFloat Float where ins = Sub Dict
248 instance () :=> RealFloat Double where ins = Sub Dict
249
250 instance Class () (Monoid a) where cls = Sub Dict
251 instance () :=> Monoid () where ins = Sub Dict
252 instance () :=> Monoid Ordering where ins = Sub Dict
253 instance () :=> Monoid [a] where ins = Sub Dict
254 instance Monoid a :=> Monoid (Maybe a) where ins = Sub Dict
255 instance (Monoid a, Monoid b) :=> Monoid (a, b) where ins = Sub Dict
256
257 instance Class () (Functor f) where cls = Sub Dict
258 instance () :=> Functor [] where ins = Sub Dict
259 instance () :=> Functor Maybe where ins = Sub Dict
260 instance () :=> Functor (Either a) where ins = Sub Dict
261 instance () :=> Functor ((->) a) where ins = Sub Dict
262 instance () :=> Functor ((,) a) where ins = Sub Dict
263 instance () :=> Functor IO where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
264 instance Monad m :=> Functor (WrappedMonad m) where ins = Sub Dict
265
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
266 instance Class (Functor f) (Applicative f) where cls = Sub Dict
267 instance () :=> Applicative [] where ins = Sub Dict
268 instance () :=> Applicative Maybe where ins = Sub Dict
269 instance () :=> Applicative (Either a) where ins = Sub Dict
270 instance () :=> Applicative ((->)a) where ins = Sub Dict
271 instance () :=> Applicative IO where ins = Sub Dict
272 instance Monoid a :=> Applicative ((,)a) where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
273 instance Monad m :=> Applicative (WrappedMonad m) where ins = Sub Dict
274
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
275 instance Class (Applicative f) (Alternative f) where cls = Sub Dict
276 instance () :=> Alternative [] where ins = Sub Dict
277 instance () :=> Alternative Maybe where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
278 instance MonadPlus m :=> Alternative (WrappedMonad m) where ins = Sub Dict
279
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
280 instance Class () (Monad f) where cls = Sub Dict
281 instance () :=> Monad [] where ins = Sub Dict
282 instance () :=> Monad ((->) a) where ins = Sub Dict
283 instance () :=> Monad (Either a) where ins = Sub Dict
284 instance () :=> Monad IO where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
285
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
286 instance Class (Monad f) (MonadPlus f) where cls = Sub Dict
287 instance () :=> MonadPlus [] where ins = Sub Dict
288 instance () :=> MonadPlus Maybe where ins = Sub Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
289
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
290 -- UndecidableInstances
291 instance a :=> Enum (Dict a) where ins = Sub Dict
292 instance a => Enum (Dict a) where
293 toEnum _ = Dict
294 fromEnum Dict = 0
4dc5350 @ekmett better constraints
authored Nov 2, 2011
295
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
296 instance a :=> Bounded (Dict a) where ins = Sub Dict
297 instance a => Bounded (Dict a) where
298 minBound = Dict
299 maxBound = Dict
4dc5350 @ekmett better constraints
authored Nov 2, 2011
300
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
301 instance a :=> Read (Dict a) where ins = Sub Dict
844de69 @ekmett standalone deriving
authored Nov 3, 2011
302 deriving instance a => Read (Dict a)
4dc5350 @ekmett better constraints
authored Nov 2, 2011
303
557a4af @ekmett refactored Constraint.hs to reflect the benefits of the type family v…
authored Nov 2, 2011
304 instance a :=> Monoid (Dict a) where ins = Sub Dict
305 instance a => Monoid (Dict a) where
306 mappend Dict Dict = Dict
307 mempty = Dict
Something went wrong with that request. Please try again.