codesnippets:typedefaulting
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:typedefaulting [2021/03/17 08:07] – created f2b216 | codesnippets: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 | ||
| + | </ | ||
| + | |||
| + | Output: | ||
| + | |||
| + | * < | ||
| + | -4 | ||
| + | </ | ||
| + | |||
| + | ...or this: | ||
| + | |||
| + | * <code Haskell> | ||
| + | module Main where | ||
| + | |||
| + | main :: IO () | ||
| + | main = do | ||
| + | print $ sin 3 | ||
| + | </ | ||
| + | |||
| + | Output: | ||
| + | |||
| + | * < | ||
| + | 0.1411200080598672 | ||
| + | </ | ||
| + | |||
| + | Despite, lentgthy discussions here: [[https:// | ||
| + | |||
| + | And despite this: [[https:// | ||
| + | |||
| + | And despite this: [[https:// | ||
| + | |||
| + | ...which states: " | ||
| + | |||
| + | * <code Haskell> | ||
| default Num Integer | default Num Integer | ||
| default Real Integer | default Real Integer | ||
| Line 12: | Line 52: | ||
| </ | </ | ||
| + | Latest reserach in GHC.Num | ||
| + | |||
| + | * <code Haskell> | ||
| + | module GHC.Num (module GHC.Num, module GHC.Integer, | ||
| + | |||
| + | #include " | ||
| + | |||
| + | import GHC.Base | ||
| + | import GHC.Integer | ||
| + | import GHC.Natural | ||
| + | |||
| + | infixl 7 * | ||
| + | infixl 6 +, - | ||
| + | |||
| + | default () -- Double isn't available yet, | ||
| + | -- and we shouldn' | ||
| + | </ | ||
| + | |||
| + | Even more puzzling, this here does not compile: | ||
| + | |||
| + | * <code Haskell> | ||
| + | module Main where | ||
| + | |||
| + | default () | ||
| + | |||
| + | main :: IO () | ||
| + | main = do | ||
| + | print $ sin 3 | ||
| + | </ | ||
| + | |||
| + | Compiler error: | ||
| + | |||
| + | * < | ||
| + | app\Main.hs: | ||
| + | * 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 ' | ||
| + | In the expression: do print $ sin 3 | ||
| + | In an equation for `main': | ||
| + | | | ||
| + | 7 | print $ sin 3 | ||
| + | | | ||
| + | </ | ||
| + | |||
| + | It will work again, like this: | ||
| + | |||
| + | * <code Haskell> | ||
| + | module Main where | ||
| + | |||
| + | default (Float) | ||
| + | |||
| + | main :: IO () | ||
| + | main = do | ||
| + | print $ sin 3 | ||
| + | </ | ||
| + | |||
| + | Output: | ||
| + | |||
| + | * < | ||
| + | 0.14112 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/typedefaulting.1615964831.txt.gz · Last modified: (external edit)
