Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized Types in Haskell

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)

like image 558
Caelan Miron Avatar asked Dec 04 '22 16:12

Caelan Miron


1 Answers

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 ๐Ÿ˜ž"
bhrar (Just i)
 | i<0        = "Negative ๐Ÿ˜–"
 | otherwise  = replicate i '๐Ÿ˜Œ'

But what can you do with a value of type EMaybe? Not much, it turns out, because EJust contains a value of some unknown type. So whatever you try to use the value for, will be a type error, because the compiler has no way to confirm it's actually the right type.

bhrar :: EMaybe -> String
bhrar' (EJust i) = replicate i '๐Ÿ˜Œ'
  =====> Error couldn't match expected type Int with a
like image 85
leftaroundabout Avatar answered Dec 31 '22 14:12

leftaroundabout