3 ms·
Have I gone mad? (EDIT: it appears I have.) ``` GHCi, version 8.6.5: http://www.haskell.org/ghc/ http://www.haskell.org/ghc/ :? for help Prelude> 1 / 2 0.5
by Smaug123 6y ago
Have I gone mad? (EDIT: it appears I have.)
```
GHCi, version 8.6.5: http://www.haskell.org/ghc/ http://www.haskell.org/ghc/ :? for help
Prelude> 1 / 2
0.5
```
This is because 1 and 2 are polymorphic and get instantiated to floats here. Witchcraft.
- MaxBarraclough 6y agoI think you're still right though. I know Haskell has an unusual approach to numbers, but it looks like the resultant type is indeed different from the type of the operands. I got this from typeOf (/): Note: there are several potential instances: instance RealFloat a => Fractional (Complex a) -- Defined in ‘Data.Complex’ instance HasResolution a => Fractional (Data.Fixed.Fixed a) -- Defined in ‘Data.Fixed’ instance Integral a => Fractional (Ratio a) -- Defined in ‘GHC.Real’ ...plus two others
- nybble41 6y agoThere are many instances for Fractional, but the resultant type of the (/) operator is always the same as the operands: GHCI> :i (/) class Num a => Fractional a where (/) :: a -> a -> a ... -- Defined in ‘GHC.Real’ infixl 7 / The key is that integer types aren't Fractional, so the Haskell 2010 defaulting rules[1] cause the literals to be interpreted as Double. With an appropriate default declaration GHC could have picked Ratio Integer, Complex Double, etc. instead, but without one Integer and Double are the only options and Integer wouldn't satisfy the constraints. [1] https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-750004.3 https://www.haskell.org/onlinereport/haskell2010/haskellch4....