Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a Show instance for undefined

Can anything be done to define a Show instance for an undefined value? Maybe some GHC extensions exist? I want something like this:

> print (1,undefined)

(1,"undefined")
like image 570
user1374768 Avatar asked Jun 14 '26 14:06

user1374768


2 Answers

According to the Haskell 2010 report, chapter 9, evaluating undefined should always cause an error:

-- It is expected that compilers will recognize this and insert error
-- messages that are more appropriate to the context in which undefined
-- appears.
undefined :: a
undefined = error "Prelude.undefined"

Since printing a value includes evaluating it, this will always give an error.

like image 117
evilcandybag Avatar answered Jun 16 '26 08:06

evilcandybag


The bottom value (of which undefined is one flavor) is a value that is never constructed and hence can't be observed. This implies that you can't print it either. This value can't be compared to null from other languages, which usually can be observed and even checked against.

It is useful to think of undefined as well as error "blah" and all other bottoms as equivalent to results of infinite loops. The result of an infinite loop is never constructed and hence can't be observed.

like image 37
ertes Avatar answered Jun 16 '26 09:06

ertes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!