Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of a?

Tags:

haskell

I have the following statement:

Prelude> :t error "Hello"
error "Hello" :: a

that I would like to know, what is the type of a?

like image 217
softshipper Avatar asked Dec 06 '22 08:12

softshipper


1 Answers

a is a type variable, one that could be unified with any concrete type you like. Want to add error "Hello" to an Integer? OK.

> :t error "Hello" + (3 :: Integer)
error "Hello" + (3 :: Integer) :: Integer

Want to prepend a Maybe Char value? No problem.

> :t Just 'c' : error "Hello"
Just 'c' : error "Hello" :: [Maybe Char]

Whatever you want a to be, error will tell you it can return a value of that type.


Of course, this is a moot point, because error will never actually return.

> error "Hello" + (3 :: Integer)
*** Exception: Hello
CallStack (from HasCallStack):
  error, called at <interactive>:3:1 in interactive:Ghci1

> Just 'c' : error "Hello"
[Just 'c'*** Exception: Hello
CallStack (from HasCallStack):
  error, called at <interactive>:4:12 in interactive:Ghci1

Especially in the last one: ghci starts to output a value of type [Maybe Char], and succeeds at outputting the first element, because (:) is non-strict in its second element. Not until an attempt is actually made to get the second value does error "Hello" get evaluated and its "bluff" is called. Rather than being able to match it against either [] or (:), a runtime exception occurs.

like image 133
chepner Avatar answered Jan 11 '23 05:01

chepner