How can I find the Haskell source code for the sqrt function?

I am starting to learn Haskell and need to learn how to look things up. Since I am studying a function that uses sqrt, I want to see how that was made in Haskell. This is as much an exercise in using reference material as it is in seeing how the sqrt function works under the hood in Haskell. Any advice would be appreciated.

By entering `:i sqrt` using ghci, we can see that sqrt is

-- Defined in ‘GHC.Float’

A quick google shows that the source code repo is on https://gitlab.haskell.org/ghc/ghc.

Entering `sqrt` in the search bar for the repository yields several pages of interesting results (including tests). From what I see, using `sqrt` includes calling the corresponding `sqrt` operation on a CPU level (check out the x86 related code as one example).

Of course, GHC is not the only implementation of Haskell, but at least within these realms, both terms are most often used as synonyms.

Edit: OP found the implementation detail with this approach in https://gitlab.haskell.org/ghc/ghc/-/blob/master/libraries/base/GHC/Float.hs, where `sqrt` is defined as follows:

``````sqrt x              =  x ** 0.5
``````
• Thank you @Matthias Sieber From your instructions, I was delighted to find the following in the source code. sqrt x = x ** 0.5 I found that I could substitute x ** 0.5 for sqrt which tells me a lot about Haskell. Much thanks for your help Commented May 16, 2021 at 0:47

API docs for the core libraries are maintained at haskell.org as well. There's an index link in the upper right where you can look up specific functions and then, on each module's documentation page, there are links to source code. For sqrt:

Also, bookmark this, the top-level of the latest API docs:

• Great resource @dino. Much thanks Commented Aug 14, 2021 at 21:02

The simplest and the most effective way to learn Haskell is to use online playgrounds.

Here's how a square root integer calculation may look like in Haskell:

``````squareRoot :: Int -> Int
squareRoot n = try n where
try i | i * i > n = try (i - 1)
| i * i <= n = i

main = do
print (squareRoot 749)
``````