What language extensions allow you to write "class A (B c) => D c where ..."? What is the point of declaring this type of class?
I am trying to understand the following class declaration:
class (MonadHold t (PushM t), MonadSample t (PullM t), Functor (Event t), Functor (Behavior t)) => Reflex t where
data Behavior t :: * -> *
which is in the Reflex FRP library.
1) What language extensions allow you to write Functor (Behavior t) => Reflex t? I guess this is a combination of language extensions. I suppose family members are involved, but what else?
In other words, what language extension do I need to enable so that the code containing contains class A (B c) => D c where ...compiles?
2) What is the point class A (B c) => D c where ...?
3) Could you give a super simple example explaining why and when to write class A (B c) => D c where ...?
4) , class A (B c) => D c where ...? , SPJ , , ?
Edit:
:
Haskell 98 ( ) ; , .
, Haskell98 class A b=>C b where , , class A(B c) => D c . , , , - , ? Haskell 98, Haskell 98, , //?
class Functor (Behaviour t) => Reflex t where data Behaviour t :: * -> * , , t, Reflex, , Behaviour t, Functor.
: , :
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
class Functor (F a) => Funky a where
data F a :: * -> *
-
frobulate :: (Funky a) => F a Int -> F a Bool
frobulate = fmap (< 5)
frobulate Funky a, Functor (F a).
:
instance Funky Int where
data F Int a = MkF a
F Int Functor:
No instance for (Functor (F Int))
arising from the superclasses of an instance declaration
In the instance declaration for `Funky Int'
,
instance Functor (F Int) where
fmap f (MkF x) = MkF (f x)
, Functor FlexibleInstances.
. , , , , .
(Haskell 98) class (Eq a) => MyClass a where , a MyClass, a Eq, Eq a ( , a Eq) , , MyClass a ( , a MyClass).
, class ($Predicate) => MyClass classInstance, $Predicate , , Eq a, , a Eq.
FlexibleContexts, - , ATypeClass typeParameter.
ATypeClass SomeComplexType, - Haskell 98, SomeComplexType ATypeClass.
Haskell ( ).
. , .
The second compiler does not compile because it is Smellsnot StringLikeas required in the class declaration IntLike. However, if we remove this restriction (in the third program), the program will compile again.
Here is an example of a toy that compiles:
{-# LANGUAGE FlexibleContexts #-}
class StringLike a where
getString :: a->String
class (StringLike Smells) =>IntLike a where
getInt :: a-> Int
instance IntLike Colors where
getInt x = 0
instance StringLike Colors where
getString x = "Hello"
instance StringLike Smells where
getString x = "Bad"
data Colors = Blue | Red
data Smells = Rose | Trash
However, the following errors:
{-# LANGUAGE FlexibleContexts #-}
class StringLike a where
getString :: a->String
class (StringLike Smells) =>IntLike a where
getInt :: a-> Int
instance IntLike Colors where
getInt x = 0
instance StringLike Colors where
getString x = "Hello"
data Colors = Blue | Red
data Smells = Rose | Trash
with error message:
test.hs:10:10:
No instance for (StringLike Smells)
arising from the superclasses of an instance declaration
In the instance declaration for `IntLike Colors'
Failed, modules loaded: none.
However, the following compilations are again just fine:
{-# LANGUAGE FlexibleContexts #-}
class StringLike a where
getString :: a->String
class (StringLike Smells) =>IntLike a where
getInt :: a-> Int
instance StringLike Colors where
getString x = "Hello"
data Colors = Blue | Red
data Smells = Rose | Trash