Haskell type functions?

Viewed 5787

Consider this ADT:

data Property f a = Property String (f a) | Zilch
  deriving Show

What is f here? Is it a function acting on a? Is it a 'type function'? The instructor said that Haskell has a Turing complete type language...so in which case the types can have functions too I assume?

*Main> var = Property "Colors" [1,2,3,4]
*Main> :t var
var :: Num a => Property [] a

How is f behaving like [] here? Since [] is the constructor for the empty list, is f always going to be the outermost empty constructor for the type of a as in the following examples?

*Main> var = Property "Colors" [(1,"Red"),(2,"Blue")]
*Main> :t var
var :: Num t => Property [] (t, [Char])

*Main> var = Property "Colors" (1,"Red")
*Main> :t var
var :: Num t => Property ((,) t) [Char]

The latter one I don't quite get but if someone said Haskell inferred the empty constructor for that tuple, I'm okay buying that. On the other hand,

*Main> var = Property "Colors" 20
*Main> :t var
var :: Num (f a) => Property f a

what is f here? Can't be the identity because id :: a -> a but we need (f a).

I managed to make my ADT a functor with:

instance Functor f => Functor (Property f) where
  fmap fun (Property name a) = Property name (fmap fun a)
  fmap g Zilch               = Zilch 

So something like the following works

*Main> var = Property "Colors" [1,2,3,4]
*Main> fmap (+1) var
Property "Colors" [2,3,4,5]

But what if I gave it to the previous example of a tuple?

I am really looking for explanatory answers (have seen Haskell for barely two months in a summer course), not references to things like FlexibleContexts to allow ... say fmap to work on arbitrary a.

4 Answers
Related