[Haskell-beginners] How to avoid evaluating the second (undefined) argument of a Boolean AND operation?

Costello, Roger L. costello at mitre.org
Thu Jun 23 01:03:17 CEST 2011


Hi Folks,

Here is my own version of the Bool datatype, and my own version of the Boolean AND:

data MyBool = F | T

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:

    No instance for (Fractional MyBool)
      arising from a use of `/' 
    Possible fix: add an instance declaration for (Fractional MyBool)
    In the second argument of `myAND', namely `(1 / 0)'
    In the expression: myAND F (1 / 0)
    In the definition of `t4': t4 = myAND F (1 / 0)

Why does it evaluate the second argument when the answer is already known from the first argument? 

How can I design it so that if the answer is known from the first argument, then an undefined second argument doesn't produce an error?

/Roger



More information about the Beginners mailing list