Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the implementation code for "and"?

Tags:

haskell

I have found the definition for and, on the internet but I couldnt find the actual implementation of and. I did search for some sort of prelude haskell file on my computer but it didn't return anything which could be opened in a text editor.

like image 211
Lethi Avatar asked Sep 22 '12 14:09

Lethi


1 Answers

You can use Hoogle to search for Haskell functions, like so:

http://www.haskell.org/hoogle/?hoogle=and

The function links take you to the library module where the function was defined, and in this case the link for and takes us to here:

http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:and

You then click on the Source link to the right of the function name and it takes you to the source of that function. For the case of and it links us to here:

http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-List.html#and

and                     :: [Bool] -> Bool
or                      :: [Bool] -> Bool
#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
#endif

You'll see that and actually has two definitions. One is the Standard Prelude definition, which is only enabled if you compile with the USE_REPORT_PRELUDE flag, and the other is the definition the Prelude is usually compiled with, which is typically more optimized.

like image 187
Gabriella Gonzalez Avatar answered Sep 22 '22 14:09

Gabriella Gonzalez