User Tools

Site Tools


codesnippets:typedefaulting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
codesnippets:typedefaulting [2021/03/17 08:07] – created f2b216codesnippets:typedefaulting [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Type defaulting ====== ====== Type defaulting ======
  
-<code Haskell>+Stil not clear how this works: 
 + 
 +  * <code Haskell> 
 +module Main where 
 + 
 +main :: IO () 
 +main = do 
 +    print $ negate 4 
 +</code> 
 + 
 +Output: 
 + 
 +  * <code> 
 +-4 
 +</code> 
 + 
 +...or this: 
 + 
 +  * <code Haskell> 
 +module Main where 
 + 
 +main :: IO () 
 +main = do 
 +    print $ sin 3 
 +</code> 
 + 
 +Output: 
 + 
 +  * <code> 
 +0.1411200080598672 
 +</code> 
 + 
 +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:" 
 + 
 +  * <code Haskell>
 default Num Integer default Num Integer
 default Real Integer default Real Integer
Line 12: Line 52:
 </code> </code>
  
 +Latest reserach in GHC.Num  shows:
 +
 +  * <code Haskell>
 +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
 +</code>
 +
 +Even more puzzling, this here does not compile:
 +
 +  * <code Haskell>
 +module Main where
 +
 +default ()
 +
 +main :: IO ()
 +main = do
 +    print $ sin 3
 +</code>
 +
 +Compiler error:
 +
 +  * <code>
 +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
 +  |     ^^^^^^^^^^^^^
 +</code>
 +
 +It will work again, like this:
 +
 +  * <code Haskell>
 +module Main where
 +
 +default (Float)
 +
 +main :: IO ()
 +main = do
 +    print $ sin 3
 +</code>
 +
 +Output:
 +
 +  * <code>
 +0.14112
 +</code>
 +
 +
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/typedefaulting.1615964831.txt.gz · Last modified: (external edit)

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