Creating a constraint Perhaps where Eq a

How can I limit, perhaps, where is Eq a? He must be good * β†’ Constraint

What I tried:

class (a ~ Maybe b, Eq b) => K a where instance (a ~ Maybe b, Eq b) => K a where 

Mistake:

 Not in scope: type variable 'b' 

Usage example:

 data Test c = forall a. (ca) => Test a r :: Test K -> Maybe Bool r (Test o) = (==) <$> o <*> o -- I need GHC to infer that o is Maybe Eq 

Cases that work:

 pp :: Test ((~) String) -> String pp (Test o) = o ++ "X" -- GHC infers that o is a string hh :: Test Eq -> Bool hh (Test o) = o == o -- GHC infers that o is Eq 

The general answer is here: Is there a general way to apply restrictions on the type of application?

+6
haskell
source share
2 answers

The following compilation on my machine. I don’t know how reasonable this is.

 {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE ConstraintKinds #-} class (Eq (UnMaybe a), a ~ Maybe (UnMaybe a)) => EqMaybe a where type UnMaybe a instance Eq a => EqMaybe (Maybe a) where type UnMaybe (Maybe a) = a data Test c = forall a. ca => Test a r :: Test EqMaybe -> Maybe Bool r (Test o) = (==) <$> o <*> o f :: Test Eq -> Test EqMaybe f (Test o) = Test (Just o) 
+8
source share

This is a known issue . The GHC is suffocating because you entered a new variable of type b not mentioned anywhere in the instance head. One way is to use type families and constraint types.

 {-# LANGUAGE ConstraintKinds, TypeFamilies, UndecideableInstances, UndecideableSuperclasses, FlexibleInstances #-} import GHC.Exts (Constraint) import GHC.Prim (Any) type family MaybeEq x :: Constraint where MaybeEq (Maybe a) = Eq a MaybeEq _ = Any -- A good "unsatisfiable" constraint class MaybeEq a => K a where instance MaybeEq a => K a where 
+6
source share

All Articles