Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Would you shed light on "undefined values" please?
(Arlen Cuss)
2. Re: Getting Stuff out of IO Handles (Kyle Murphy)
3. Re: Would you shed light on "undefined values" please?
(austin seipp)
4. Re: Would you shed light on "undefined values" please?
(Brandon Allbery)
5. Re: Getting Stuff out of IO Handles (Jared Hance)
6. Re: Getting Stuff out of IO Handles (Kyle Murphy)
----------------------------------------------------------------------
Message: 1
Date: Fri, 24 Jun 2011 08:59:38 +1000
From: Arlen Cuss <[email protected]>
Subject: Re: [Haskell-beginners] Would you shed light on "undefined
values" please?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Howdy,
> I am not clear why "bottom" is an important concept. Would you explain please?
I've been getting into this myself lately. Here's what I've been reading:
http://en.wikibooks.org/wiki/Haskell/Denotational_semantics
http://blog.ezyang.com/2010/12/hussling-haskell-types-into-hasse-diagrams/
Let us know if you find anything else!
Arlen
------------------------------
Message: 2
Date: Thu, 23 Jun 2011 19:05:37 -0400
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] Getting Stuff out of IO Handles
To: Raphael P?bst <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
The short answer is no. The longer answer depends on what you mean by "get
out". You can take the result of an IO action and pass it to a pure function
like so:
do
result <- someIOAction
return $ somePureFunction result
or alternately:
someIOAction >>= return . somePureFunction
If you want to know if you can do something like:
someFunction :: Int -> Int
someFunction n = escapeIO $ someIOAction n
then the answer is you shouldn't. It is technically possible, by using a
function that every experienced haskell developer will tell you to never
ever ever ever ever ever ever ever (get the picture) use, which is
unsafePerformIO, but if you use unsafePerformIO it's likely that your code
won't do what you actually expect it to do, even if the type signatures all
check correctly. The IO Monad serves a very important purpose and implies
certain things about a computation, likewise the absence of the IO Monad
implies certain guarantees, and when you break those guarantees bad things
happen.
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Thu, Jun 23, 2011 at 11:23, Raphael P?bst
<[email protected]>wrote:
> Hey everyone!
> This is probably a stupid question, but is there a way to get stuff
> out of a IO handle?
> If I do something with an IO handle, reading in data from a file for
> example and then do something with the data, is there a way to get the
> results out of the handle, comparable to the return in a do block?
> Or do all operations on the data have to happen inside the handle?
> Thanks
>
> Raf
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110623/66be794d/attachment-0001.htm>
------------------------------
Message: 3
Date: Thu, 23 Jun 2011 18:09:03 -0500
From: austin seipp <[email protected]>
Subject: Re: [Haskell-beginners] Would you shed light on "undefined
values" please?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
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. <[email protected]> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Regards,
Austin
------------------------------
Message: 4
Date: Thu, 23 Jun 2011 19:27:18 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Would you shed light on "undefined
values" please?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, Jun 23, 2011 at 18:35, Costello, Roger L. <[email protected]> wrote:
> ? ?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?
> bottomString = bottomString ++ bottomString
is one way to formulate it. More generally
> undefined :: a
> undefined = let x = x in x -- a computation that never produces a value
The reason _|_ is significant is that a type that admits it as a value
can be computed lazily. If your type does not allow for _|_ then it
is strict; anything that touches it in any way when it is invalid (an
infinite loop, exception, or other effect that is not producing an
actual value; this is the effective definition of "bottom") will fail
immediately. A "lifted" type (one which allows _|_) will only loop /
throw an exception / dump core / set the building on fire / whatever
when something actually demands that value.
Some practical uses of this:
- since _|_ inhabits every lifted type, the definition of "undefined"
that I provided above can be used as a stub during development, and
will always typecheck;
- similarly, the computation
> error :: String -> a
(whose implementation can't be expressed within the language) throws a
runtime exception, and can be used anywhere that a lifted type is
permitted (anywhere, in standard Haskell).
- "undefined" in particular is useful as a placeholder when only the
type is significant. See "asTypeOf" as an example of this. (It could
be argued that this case really wants "_" to function as a
pseudo-value instead of just as a pattern, but that has its own
problems.)
> If I create my own datatype:
>
> data MyBool = F | T
>
> What is the undefined value (bottom) of type MyBool?
_|_ is not, in general, expressible within Haskell itself except in
terms of infinite loops or runtime exceptions; it's not like SQL's
NULL, it's a "not-value". In this case, the easy way to generate a
bottom for your type is
> myBoolBottom :: MyBool
> myBoolBottom = let x = x in x
(Remember that "let" is recursive in Haskell; the above expression
will try to compute "x", which will try to compute "x", which will ...
in reality, the GHC runtime will notice that the computation for "x"
immediately re-enters itself and will compute 'error "<<loop>>"' in
its place, raising a runtime exception.)
GHC and several other Haskell compilers support unlifted types, which
are strict. Every program uses one, in fact; in GHC, IO is an
unlifted tuple. The primary explicit use of them is UArray/STArray,
which are the equivalent of arrays in strict languages.
--
brandon s allbery ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [email protected]
wandering unix systems administrator (available) ? ? (412) 475-9364 vm/sms
------------------------------
Message: 5
Date: Thu, 23 Jun 2011 19:46:59 -0400
From: Jared Hance <[email protected]>
Subject: Re: [Haskell-beginners] Getting Stuff out of IO Handles
To: [email protected]
Message-ID: <20110623234658.GA2436@gentoo>
Content-Type: text/plain; charset=us-ascii
> If you want to know if you can do something like:
>
> someFunction :: Int -> Int
> someFunction n = escapeIO $ someIOAction n
>
> then the answer is you shouldn't. It is technically possible, by using a
> function that every experienced haskell developer will tell you to never
> ever ever ever ever ever ever ever (get the picture) use, which is
> unsafePerformIO, but if you use unsafePerformIO it's likely that your code
> won't do what you actually expect it to do, even if the type signatures all
> check correctly. The IO Monad serves a very important purpose and implies
> certain things about a computation, likewise the absence of the IO Monad
> implies certain guarantees, and when you break those guarantees bad things
> happen.
I'd like to clarify a bit here. The unsafePerformIO function has a use,
and should be used, but only in the right circumstances. The problem is
that often a function is referentially transparent, but haskell cannot
prove that it is because it uses the IO Monad.
The circumstance in which this crops up is usually when you use the FFI.
The function you call in C might return a pointer to something, rather
than the data itself. The pointer will usually be different every time,
so the result isn't referentially transparent. However, if you extract
the data from the pointer, that data _is_ referentially transparent and
is, as such, a good use case for unsafePerformIO.
So I would say an experienced programmer would say to never use it if
you don't know what you are doing, but if you do, use it when you need
it.
------------------------------
Message: 6
Date: Thu, 23 Jun 2011 20:37:40 -0400
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] Getting Stuff out of IO Handles
To: Jared Hance <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
I had started to say something about the circumstances under which you
actually can use unsafePerformIO, but it sort of bogged down the reply and
muddled the point I was trying to make. You seem to have done a better job
explaining the circumstances under which you can actually use unsafePerfomIO
safely than I was able to at the time.
-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.
On Thu, Jun 23, 2011 at 19:46, Jared Hance <[email protected]> wrote:
> > If you want to know if you can do something like:
> >
> > someFunction :: Int -> Int
> > someFunction n = escapeIO $ someIOAction n
> >
> > then the answer is you shouldn't. It is technically possible, by using a
> > function that every experienced haskell developer will tell you to never
> > ever ever ever ever ever ever ever (get the picture) use, which is
> > unsafePerformIO, but if you use unsafePerformIO it's likely that your
> code
> > won't do what you actually expect it to do, even if the type signatures
> all
> > check correctly. The IO Monad serves a very important purpose and implies
> > certain things about a computation, likewise the absence of the IO Monad
> > implies certain guarantees, and when you break those guarantees bad
> things
> > happen.
>
> I'd like to clarify a bit here. The unsafePerformIO function has a use,
> and should be used, but only in the right circumstances. The problem is
> that often a function is referentially transparent, but haskell cannot
> prove that it is because it uses the IO Monad.
>
> The circumstance in which this crops up is usually when you use the FFI.
> The function you call in C might return a pointer to something, rather
> than the data itself. The pointer will usually be different every time,
> so the result isn't referentially transparent. However, if you extract
> the data from the pointer, that data _is_ referentially transparent and
> is, as such, a good use case for unsafePerformIO.
>
> So I would say an experienced programmer would say to never use it if
> you don't know what you are doing, but if you do, use it when you need
> it.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110623/ea7bcd57/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 59
*****************************************