up-to-date-real-world-haskell
up-to-date-real-world-haskell copied to clipboard
[SOLVED] Chapter 6 is mentioning an error which no longer occurs
trafficstars
In "Chapter 6: Using Type Classes" at "The dreaded monomorphism restriction" it states that the following code results in an error.
myShow = show
But in ghci I get no error
GHCi, version 8.10.2: https://www.haskell.org/ghc/ :? for help
Prelude> myShow = show
Prelude>
@theqp I don't remember the details, but the monomorphism restriction is switched off in GHCi when interactively entering code. You should still get the error if you create a file and load it – like what's done in the book – instead of using GHCi interactively:
$ echo "myShow = show" > Monomorphism.hs
$ ghci
GHCi, version 8.10.1: https://www.haskell.org/ghc/ :? for help
Prelude> :load Monomorphism
[1 of 1] Compiling Main ( Monomorphism.hs, interpreted )
Monomorphism.hs:1:10: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘show’
prevents the constraint ‘(Show a0)’ from being solved.
Relevant bindings include
myShow :: a0 -> String (bound at Monomorphism.hs:1:1)
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 12 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show
In an equation for ‘myShow’: myShow = show
|
1 | myShow = show
| ^^^^