Search code examples
haskellnewtype

Haskell newtype with parentheses


I'm trying to understand the explaination in Monads made difficult and I have a hard time figuring out the following newtype definition:

newtype (FComp g f) x = FComp { unCompose :: g (f x) }

instance (Functor b c f, Functor a b g) => Functor a c (FComp g f) where
  fmap f (FComp xs) = FComp $ fmap (fmap f) xs

I have nowhere seen an explaination of what newtype means with an expression in parentheses in place of the type declaration. I therefore cannot figure out what the definition of the fmap function means. I also don't understand why the unCompose field accessor is defined but never used. I feel like I am missing some basic semantics of newtype.


Solution

  • You could write this:

    newtype (FComp g f) x = FComp { unCompose :: g (f x) }
    

    like so:

    newtype FComp g f x = FComp (g (f x))
    unCompose (FComp it) = it
    

    This is so because type application has the same syntactic properties as ordinary applications, i.e.:

    a b c  = (a b) c
    

    holds for values a,b,c and for types a,b,c.