I recently generalized a type class away from a constraint MonadError GenError m
to a more flexible constraint of MonadError e m, CanContainGenError e
. This is useful for using the relevant m开发者_高级运维onad transformer with a stack that already has an ErrorT SomeError m
- I can just add GenError
as an element of a new constructor in the SomeError
data type.
I found myself surprised to be writing a custom CanContainGenError
class hard-coded to GenError
. Isn't there a common ContainedType
class or some such? (I almost called it "subtype", heh)
Anything like the below CanContainType
or ContainsType
classes I just made up?
class CanContainType cont orig where
toCont :: orig -> cont
fromCont :: cont -> Maybe orig
class ContainsType orig sub where
toContainer :: orig -> cont
fromContainer :: cont -> orig
Where an example instantiation is:
-- edit fixed example instance to reflect what I want, sorry for the misleading code
data IntOrFloatOrDouble= I Int | F Float | D Double
instance CanContainType IntOrFloatOrDouble Int where
toCont = I
fromCont (I a) = Just a
fromCont _ = Nothing
Now that I've typed this out I'm realizing there probably isn't an established one because my requirements mandate MPTCs. Still, I'm interested in any thoughts.
Sometimes things like this come up in EDSL construction, for lifting instances from Haskell to the EDSL. See e.g. http://www.galois.com/~dons/tmp/Type.hs
data IntegralType a where
TypeInt :: IntegralDict Int -> IntegralType Int
TypeInt8 :: IntegralDict Int8 -> IntegralType Int8
TypeInt16 :: IntegralDict Int16 -> IntegralType Int16
TypeInt32 :: IntegralDict Int32 -> IntegralType Int32
TypeInt64 :: IntegralDict Int64 -> IntegralType Int64
...
There's nothing standard, obviously.
精彩评论