[Haskell-cafe] and from standard Prelude
Oleg Lobachev
lobachev at Mathematik.Uni-Marburg.de
Wed Aug 18 05:05:33 EDT 2010
Hello all,
the and function, and :: [Bool] -> Bool is defined in two different ways in the latest Prelude.
I would expect it to be
> and = foldr (&&) True
However, there is a further recursive definition, and it is the one used! See http://haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/GHC-List.html#and
> #ifdef USE_REPORT_PRELUDE
> and = foldr (&&) True
> or = foldr (||) False
> #else
> and [] = True
> and (x:xs) = x && and xs
> or [] = False
> or (x:xs) = x || or xs
>
> {-# RULES
>
> "and/build" forall (g::forall b.(Bool->b->b)->b->b) .
>
> and (build g) = g (&&) True
>
> "or/build" forall (g::forall b.(Bool->b->b)->b->b) .
>
> or (build g) = g (||) False
>
> #-}
> #endif
Is there any reason for that besides some clever optimizations? I am particularly interested in the behavior of and for infinite lists and these two versions are equivalent in this aspect.
Greetings,
Oleg
More information about the Haskell-Cafe
mailing list