I came across the following code in GHC.Prim:
...
negateFloat# :: Float# -> Float#
negateFloat# = let x = x in x
-- |Truncates a @Float#@ value to the nearest @Int#@.
-- Results are undefined if the truncation if truncation yields
-- a value outside the range of @Int#@.
float2Int# :: Float# -> Int#
float2Int# = let x = x in x
expFloat# :: Float# -> Float#
expFloat# = let x = x in x
logFloat# :: Float# -> Float#
logFloat# = let x = x in x
...
In particular:
float2Int#
shouldn't even type checklet x = x
I know the extension is called "MagicHash", but it can't be that magical. What gives?
(I guess I should add the disclaimer that I have no idea what -XMagicHash
actually does, I just assumed it allowed the #
syntax, and nothing more.)
The clue is at the top of the module:
{-
This is a generated file (generated by genprimopcode).
It is not code to actually be used. Its only purpose is to be
consumed by haddock.
-}
Incidentally, let x = x in x
is equivalent to undefined
, and is valid for any data type; x
isn't constrained by being defined in any way other than self-referentially, so can be any type.
These functions are "primitive" meaning operations so basic they're not defined in Haskell code, and quite possibly just translated directly to machine instructions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With