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: How to avoid evaluating the second (undefined) argument
of a Boolean AND operation? (Felipe Almeida Lessa)
2. Re: How to avoid evaluating the second (undefined) argument
of a Boolean AND operation? (Brandon Allbery)
3. Re: How to avoid evaluating the second (undefined) argument
of a Boolean AND operation? (Jack Henahan)
----------------------------------------------------------------------
Message: 1
Date: Wed, 22 Jun 2011 20:21:55 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] How to avoid evaluating the second
(undefined) argument of a Boolean AND operation?
To: Jack Henahan <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Wed, Jun 22, 2011 at 8:07 PM, Jack Henahan <[email protected]> wrote:
> Use
>
> ? ?myAND F _ = F
>
> instead of using x.
Actually that doesn't change anything and both definitions are the
same thing. The problem here is that "type checking is not lazy" =).
The second argument of myAND in "myAND F (1/0)" isn't being evaluated
at all; actually your code isn't running at all. What is being
reported is a type error, not a runtime error. Try one of these
instead:
myAND F undefined
myAND F (error "foo")
myAND F (let x = x in x)
myAND F (unsafePerformIO $ launchMissiles)
Cheers! =)
--
Felipe.
------------------------------
Message: 2
Date: Wed, 22 Jun 2011 19:23:20 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] How to avoid evaluating the second
(undefined) argument of a Boolean AND operation?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Wed, Jun 22, 2011 at 19:03, Costello, Roger L. <[email protected]>wrote:
> myAND :: MyBool -> MyBool -> MyBool
> myAND F x = F
> myAND T x = x
>
> If the first argument is F then return F. I assumed that the second
> argument would not even bother being evaluated.
>
> I figured that I could provide an undefined value for the second argument:
>
> myAND F (1 / 0)
>
> However, that doesn't work. I get this error message:
>
There's no evaluation here. Haskell does no implicit coercions for you
(with a limited exception involving the definition of numeric literals) so
you are passing a Fractional a => a (1/0) when a MyBool is expected, and
MyBool isn't a Fractional. (No coercions means, in this case, that a number
*cannot* be used as a boolean, neither a real one nor your alternative
formulation, without some kind of explicit coercion.)
How you fix this depends on what you're trying to accomplish. If you really
want to use a division by zero there, you need to use an Integral a => a and
coerce it manually:
> myAND F (toEnum (1 `div` 0))
or, if you insist on Fractional,
> myAND F (toEnum (fromRational (1 / 0)))
...and both of these require that you declare MyBool as
> data MyBool = F | T deriving Enum
so that you can use toEnum.
If you're just trying to prove the point about lazy evaluation, leave the
numbers out of it:
> myAND F undefined
("undefined"'s type is "a", that is, any type; from this you can infer that
it can never produce an actual value, it can only raise an exception,
because there is no value that inhabits all possible types.)
Alternately you can say
> myAND F (error "wait, what?")
which lets you control what message is printed if for some reason the second
parameter is evaluated.
--
brandon s allbery [email protected]
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110622/78902e3c/attachment-0001.htm>
------------------------------
Message: 3
Date: Wed, 22 Jun 2011 19:26:35 -0400
From: Jack Henahan <[email protected]>
Subject: Re: [Haskell-beginners] How to avoid evaluating the second
(undefined) argument of a Boolean AND operation?
To: Felipe Almeida Lessa <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
You're right, of course. I totally spaced out while reading the error message.
That'll teach me to reply to lists while on the phone. :/
On Jun 22, 2011, at 7:21 PM, Felipe Almeida Lessa wrote:
> On Wed, Jun 22, 2011 at 8:07 PM, Jack Henahan <[email protected]> wrote:
>> Use
>>
>> myAND F _ = F
>>
>> instead of using x.
>
> Actually that doesn't change anything and both definitions are the
> same thing. The problem here is that "type checking is not lazy" =).
> The second argument of myAND in "myAND F (1/0)" isn't being evaluated
> at all; actually your code isn't running at all. What is being
> reported is a type error, not a runtime error. Try one of these
> instead:
>
> myAND F undefined
> myAND F (error "foo")
> myAND F (let x = x in x)
> myAND F (unsafePerformIO $ launchMissiles)
>
> Cheers! =)
>
> --
> Felipe.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 398E692F.asc
Type: application/x-apple-msg-attachment
Size: 24295 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110622/d6c16cef/attachment.bin>
-------------- next part --------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 881 bytes
Desc: This is a digitally signed message part
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110622/d6c16cef/attachment.pgp>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 56
*****************************************