Class type data types

After reading the article Cancel Type Classes , I repeated some of the ideas presented. At the same time, I came across something really strange: Type Type - Type can be used as a type restriction! My question is: why?

My code is:

{-# LANGUAGE Rank2Types #-} data IFunctor f = IFunctor { _fmap :: forall a b. (a -> b) -> fa -> fb } -- this type checks... _fmap2 :: IFunctor f => (a -> b) -> f (fa) -> f (fb) _fmap2 = \inst -> _fmap inst . _fmap inst 

In GHCi, the following happens:

 >>> :t _fmap2 :: IFunctor f => (a -> b) -> f (fa) -> f (fb) _fmap2 :: IFunctor f => (a -> b) -> f (fa) -> f (fb) :: IFunctor f -> (a -> b) -> f (fa) -> f (fb) 
+6
source share
1 answer

This does not work on GHC 7.8.2. It gives the error Expected a constraint, but 'IFunctor f' has kind '*' .

There was an error in older versions of GHC in which they allowed the use of => as -> in certain situations. This is probably due to the fact that the internal restrictions of the class of the class are passed as arguments in the form of method dictionaries.

+1
source

Source: https://habr.com/ru/post/971167/


All Articles