[Haskell-cafe] Small question

David Menendez zednenem at psualum.com
Sat Aug 11 00:06:23 EDT 2007

On 8/10/07, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
> My program needs to make decisions based on a pair of boolean values.
> Encoding both values as a single algebraic data type means I have to
> keep "taking it apart" so I can work with it. I'm not sure how much time
> this wastes...

Incidentally, there is an argument that many (perhaps most) use of
Bool should instead be custom datatypes. That is, instead of:

    type FooBar = (Bool, Bool)

one should instead do something like

    data Foo = Foo | AntiFoo
    data Bar = Baz | Bo
    type FooBar = (Foo, Bar)

which makes it clearer what's going on and harder to confuse the two booleans.

Of course, now you have to replace

    \(foo, bar) -> if foo then ... else ...


    \(foo, bar) -> if foo == Foo then ... else ...


    \(foo, bar) -> case foo of { Foo -> ...; Bar -> ... }


Actually, that raises an interesting question. Is there a performance
difference between "if foo == Foo ..." and "case Foo of ..."? I think
JHC's case-hoisting should be able to transform the former into the
latter, but does it?

More information about the Haskell-Cafe mailing list