Search code examples
haskellargumentsfunctornewtype

Functor type variables for Flip data type


I have following type definition:

newtype Flip f a b = 
  Flip (f b a) deriving (Eq, Show)

Does the Flip data constructor has one or three arguments?

Consinder following implementation:

data K a b = K a

newtype Flip f a b = 
  Flip (f b a) deriving (Eq, Show)

instance Functor (Flip K a) where
  fmap f (Flip (K b)) = Flip (K (f b))  

What is the type of (Flip K a)?


Solution

  • The Flip data constructor has one argument. That argument has type f b a.

    That thus means that f itself is a higher order type argument with type f :: * -> * -> *. A more rigorous newtype statement would be:

    newtype Flip (f :: * -> * -> *) a b = Flip (f b a)
    

    You can thus for instance instantiate a Flip Either Int Bool, since Either is a type that requires two additional type parameters, and then construct a Flip (Right 1) :: Flip Either Int Bool.

    What is the type of (Flip K a)?

    Flip K a is not a fully applied type. In pseudo-code, it has type b -> Flip K a b. Once the b has been resolved (Functor works with higher order types), we know that the only argument of Flip will have a K b constructor. So for instance Flip (K 1) is a Flip K a Int type.