user2649442
user2649442

Reputation:

Type Class Data Types

Having read the article Scrap your type classes, I re-implemented some of the ideas shown. While doing that I came across something really strange: The Type Class - Type can be used as a type constraint! My question: Why is that?

My Code:

{-# LANGUAGE Rank2Types #-}

data IFunctor f = IFunctor { 
    _fmap :: forall a b. (a -> b) -> f a -> f b 
}

-- this type checks...
_fmap2 :: IFunctor f => (a -> b) -> f (f a) ->  f (f b)
_fmap2 = \inst -> _fmap inst . _fmap inst

In GHCi the following thing happens:

>>> :t _fmap2 :: IFunctor f => (a -> b) -> f (f a) -> f (f b)
_fmap2 :: IFunctor f => (a -> b) -> f (f a) -> f (f b)
       :: IFunctor f -> (a -> b) -> f (f a) -> f (f b)

Upvotes: 6

Views: 161

Answers (1)

David Young
David Young

Reputation: 10793

This doesn't work on GHC 7.8.2. It gives the error Expected a constraint, but ‘IFunctor f’ has kind ‘*’.

Older versions of GHC had a bug where they allowed => to be used like -> in certain situations. This is likely because internally type class constraints are passed as arguments in the form of method dictionaries.

Upvotes: 1

Related Questions