Home > Software design >  Parameterized Types in Haskell
Parameterized Types in Haskell

Time:11-04

Why do types in Haskell have to be explicitly parameterized in the type constructor parameter?

For example:

data Maybe a = Nothing | Just a  

Here a has to be specified with the type. Why can't it be specified only in the constructor?

data Maybe = Nothing | Just a 

Why did they make this choice from a design point of view? Is one better than the other?

I do understand that first is more strongly typed than the second, but there isn't even an option for the second one.

Edit :

Example function


data Maybe = Just a | Nothing

div :: (Int -> Int -> Maybe)
div a b
  | b == 0    = Nothing
  | otherwise = Just (a / b)

CodePudding user response:

It would probably clear things up to use GADT notation, since the standard notation kind of mangles together the type- and value-level languages.

The standard Maybe type looks thus as a GADT:

{-# LANGUAGE GADTs #-}

data Maybe a where
  Nothing :: Maybe a
  Just :: a -> Maybe a

The “un-parameterised” version is also possible:

data EMaybe where
  ENothing :: EMaybe
  EJust :: a -> EMaybe

(as Joseph Sible commented, this is called an existential type). And now you can define

foo :: Maybe Int
foo = Just 37

foo' :: EMaybe
foo' = EJust 37

Great, so why don't we just use EMaybe always?

Well, the problem is when you want to use such a value. With Maybe it's fine, you have full control of the contained type:

bhrar :: Maybe Int -> String
bhrar Nothing = "No number            
  • Related