@@ -110,7 +110,8 @@ type family IsSubList a b :: Constraint where
110
110
IsSubList '[] b = ()
111
111
IsSubList (x ': xs ) y = Elem x y `And ` IsSubList xs y
112
112
113
- -- | Check that an eleme is an element of a list:
113
+ #if !MIN_VERSION_base(4,9,0)
114
+ -- | Check that a value is an element of a list:
114
115
--
115
116
-- >>> ok (Proxy :: Proxy (Elem Bool '[Int, Bool]))
116
117
-- OK
@@ -121,13 +122,31 @@ type family IsSubList a b :: Constraint where
121
122
-- arising from a use of ‘ok’
122
123
-- ...
123
124
type Elem e es = ElemGo e es es
125
+ #else
126
+ -- | Check that a value is an element of a list:
127
+ --
128
+ -- >>> ok (Proxy :: Proxy (Elem Bool '[Int, Bool]))
129
+ -- OK
130
+ --
131
+ -- >>> ok (Proxy :: Proxy (Elem String '[Int, Bool]))
132
+ -- ...
133
+ -- ... [Char] expected in list '[Int, Bool]
134
+ -- ...
135
+ type Elem e es = ElemGo e es es
136
+ #endif
124
137
125
138
-- 'orig' is used to store original list for better error messages
126
139
type family ElemGo e es orig :: Constraint where
127
140
ElemGo x (x ': xs ) orig = ()
128
141
ElemGo y (x ': xs ) orig = ElemGo y xs orig
142
+ #if MIN_VERSION_base(4,9,0)
143
+ -- Note [Custom Errors]
144
+ ElemGo x '[] orig = TypeError ('ShowType x
145
+ ':<>: 'Text " expected in list "
146
+ ':<>: 'ShowType orig)
147
+ #else
129
148
ElemGo x '[] orig = ElemNotFoundIn x orig
130
-
149
+ #endif
131
150
132
151
-- ** Logic
133
152
@@ -144,30 +163,16 @@ type family And (a :: Constraint) (b :: Constraint) :: Constraint where
144
163
145
164
-- * Custom type errors
146
165
147
- #if MIN_VERSION_base(4,9,0)
148
- -- GHC >= 8
149
-
150
-
151
- type ElemNotFoundIn val list = TypeError
152
- (ShowType val :<>: Text " expected in list " :<>: ShowList list )
153
-
154
-
155
- -- Utilities
156
-
157
- type family ShowListGo ls :: ErrorMessage where
158
- ShowListGo '[] = Text " "
159
- ShowListGo (x ': xs ) = ShowType x :<>: Text " , " :<>: ShowListGo xs
160
-
161
- type ShowList ls = Text " [" :<>: ShowListGo ls :<>: Text " ]"
162
-
163
-
164
- #else
165
-
166
- -- GHC < 8
166
+ #if !MIN_VERSION_base(4,9,0)
167
167
class ElemNotFoundIn val list
168
-
169
168
#endif
170
169
170
+ {- Note [Custom Errors]
171
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172
+ We might try to factor these our more cleanly, but the type synonyms and type
173
+ families are not evaluated (see https://ghc.haskell.org/trac/ghc/ticket/12048).
174
+ -}
175
+
171
176
-- $setup
172
177
-- >>> import Data.Proxy
173
178
-- >>> data OK = OK deriving (Show)
0 commit comments