[Haskell-beginners] Would you shed light on "undefined values" please?
austin seipp
as at hacks.yi.org
Fri Jun 24 01:09:03 CEST 2011
Hi Roger,
As Bird says himself in your second quotation, every datatype
declaration introduces an extra anonymous value, the undefined value
of the datatype."
In haskell, this is true. This special 'bottom' value is actually
represented by the value 'undefined'. You can see this definition in
the haskell libraries:
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:undefined
It's rather unimportant that the documentation says 'undefined is a
special case of error' - the interesting part about undefined *is its
type*
undefined :: a
which is rather strange. This would seem to indicate that undefined
can in fact be used in any place to represent any concrete type.
That's correct. In fact, this type merely means that undefined is an
inhabitant of *every* possible type. That is, all of the following
values typecheck, because 'undefined' inhabits all types:
v1 = undefined :: Int
v2 = undefined :: String
v3 = undefined :: Bool
v4 = undefined :: MyBool -- your version of Bool
All types in haskell have 'bottom' as one of their members. But since
haskell is lazy, what happens if you try to evaluate an 'undefined'?
You get an error and the program terminates prematurely. But every
type in haskell has at least a single inhabitant: bottom. This is
true, even with 'empty' data types:
data Foo -- no constructors
Indeed, the type 'Foo' has no values/constructors that inhabit it! So
there is no explicit value/constructor you could use to construct a
value of type 'Foo.' But despite the lack of constructors, there is
still a way to construct a value of type 'Foo' - bottom:
v5 = undefined :: Foo
As a theoretical concept bottom is, I believe, tied a bit more into
denotational semantics and in particular, domain theory (someone
please correct me if I am wrong.) You can find a good bit of stuff
about the formal semantics of programming languages - including domain
theory - in a book like one by Glenn Winksel, "The Formal Semantics of
Programming Languages" and many others. There are also various
writings that discuss such topics with specific relation to Haskell,
such as the links Arlen Cuss gave.
On Thu, Jun 23, 2011 at 5:35 PM, Costello, Roger L. <costello at mitre.org> wrote:
> Hi Folks,
>
> In this book [1] the author defines the term "bottom":
>
> In order that we can say that, without exception, every
> syntactically well-formed expression denotes a value,
> it is convenient to introduce a special symbol (upside
> down T), pronounced 'bottom', to stand for the undefined
> value of a particular type. In particular, the value of
> infinity is the undefined value (bottom) of type Integer,
> and 1/0 is the undefined value (bottom) of type Float.
> Hence we can assert that 1/0 = bottom.
>
> He defines infinity as this:
>
> infinity :: Integer
> infinity = infinity + 1
>
> The author says this when discussing the Bool datatype:
>
> It follows that there are not two but three Boolean
> values, namely False, True, and bottom. In fact, every
> datatype declaration introduces an extra anonymous
> value, the undefined value of the datatype.
>
> What is the undefined value (bottom) of type Bool?
>
> What is the undefined value (bottom) of type String?
>
> If I create my own datatype:
>
> data MyBool = F | T
>
> What is the undefined value (bottom) of type MyBool?
>
> I am not clear why "bottom" is an important concept. Would you explain please?
>
> /Roger
>
> [1] Introduction to Functional Programming using Haskell by Richard Bird
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Regards,
Austin
More information about the Beginners
mailing list