codesnippets:typedefaulting

Type defaulting

Stil not clear how this works:

Output:

  • -4

…or this:

  • module Main where
     
    main :: IO ()
    main = do
        print $ sin 3

Output:

  • 0.1411200080598672

Despite, lentgthy discussions here: https://www.reddit.com/r/haskell/comments/1r3w3w/implicit_type_conversion_for_value_constants/

And despite this: https://www.haskell.org/onlinereport/decls.html#sect4.3.4

And despite this: https://kseo.github.io/posts/2017-01-04-type-defaulting-in-haskell.html

…which states: “Haskell default rule can be summarized as:”

Latest reserach in GHC.Num shows:

  • module GHC.Num (module GHC.Num, module GHC.Integer, module GHC.Natural) where
     
    #include "MachDeps.h"
     
    import GHC.Base
    import GHC.Integer
    import GHC.Natural
     
    infixl 7  *
    infixl 6  +, -
     
    default ()              -- Double isn't available yet,
                            -- and we shouldn't be using defaults anyway

Even more puzzling, this here does not compile:

  • module Main where
     
    default ()
     
    main :: IO ()
    main = do
        print $ sin 3

Compiler error:

  • app\Main.hs:7:5: error:
        * Ambiguous type variable `a0' arising from a use of `print'
          prevents the constraint `(Show a0)' from being solved.
          Probable fix: use a type annotation to specify what `a0' should be.
          These potential instances exist:
            instance Show Ordering -- Defined in `GHC.Show'
            instance Show Integer -- Defined in `GHC.Show'
            instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
            ...plus 22 others
            ...plus 28 instances involving out-of-scope types
            (use -fprint-potential-instances to see them all)
        * In a stmt of a 'do' block: print $ sin 3
          In the expression: do print $ sin 3
          In an equation for `main': main = do print $ sin 3
      |
    7 |     print $ sin 3
      |     ^^^^^^^^^^^^^

It will work again, like this:

  • module Main where
     
    default (Float)
     
    main :: IO ()
    main = do
        print $ sin 3

Output:

  • 0.14112

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
codesnippets/typedefaulting.txt · Last modified: by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki