
On Wed, Jun 22, 2011 at 19:03, Costello, Roger L.
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 allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms