Reputation: 123
I have written this code:
import GHC.Float
next :: GHC.Float -> GHC.Float-> GHC.Float
next n x = (x + n / x) / 2
And I am getting the following error:
numerical.hs:3:9:
Not in scope: type constructor or class `GHC.Float'
numerical.hs:3:22:
Not in scope: type constructor or class `GHC.Float'
numerical.hs:3:34:
Not in scope: type constructor or class `GHC.Float'
The module imports without any problem, so I'm not sure if I'm referring to it with the wrong name or if the standard Float module is the same as the IEEE GHC.Float one and there's no need to explicitly import it.
I tried doing an import GHC.Float as Fl
with no success--got the same type error on Fl
.
I'm just starting Haskell (obviously), so any help is appreciated!
Upvotes: 5
Views: 4240
Reputation: 54058
You don't have to import GHC.Float
manually, you can just write Float
, like so
next :: Float -> Float -> Float
next n x = (x + n / x) / 2
GHC implicitly imports a module called Prelude
in every source file you have. Prelude
includes a lot of handy types, functions, and other things that are used as the "built-ins" of the language. Types like Int
, Float
, Maybe
, IO
, and functions like head
, +
, /
, and more.
You can test to see if a floating point number is an IEEE floating point with the function isIEEE
from the GHC.Float
module:
import GHC.Float
main = do
putStr "1.0 is an IEEE floating point: "
print $ isIEEE (1.0 :: Float)
If you run this, it will print True
I should have also mentioned that the reason why your code didn't compile earlier is because when you import a module with just import
, everything from it comes into scope. You can force it to be qualified by using import qualified
, here's a few examples:
import GHC.Float -- Everything now in scope
import qualified Data.Maybe -- Have to use full name
import qualified Data.List as L -- aliased to L
main = do
-- Don't have to type GHC.Float.isIEEE
print $ isIEEE (1.0 :: Float)
-- Have to use full name
print $ Data.Maybe.isJust $ Nothing
-- Uses aliased name
print $ L.sort [1, 4, 2, 5, 3]
Upvotes: 5