Reputation: 34097
I have the following statement:
Prelude> :t error "Hello"
error "Hello" :: a
that I would like to know, what is the type of a
?
Upvotes: 5
Views: 178
Reputation: 2044
a
is the type; that is, error "Hello"
can be any type, and GHCi can't tell you what specific type it should be without more context.
Values of type a
(with no constraints, not e.g. Eq a => a
) are called "bottom values," and they arise as exceptional states or certain non-terminating expressions. Perhaps the best-known bottom value is Prelude.undefined
:
undefined :: a
Trying to examine this value will throw
*** Exception: Prelude.undefined
Likewise, your error "Hello"
will throw
*** Exception: Hello
(Thus undefined
is just error "Prelude.undefined"
.) An example of a nonterminating expression with type a
is:
let q = q in q
:t q
q :: a
And attempting to evaluate that expression will enter an infinite loop.
Upvotes: 6
Reputation: 531693
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.
Upvotes: 7