tssm / up-to-date-real-world-haskell

I'm trying to update the Real World Haskell book
Other
792 stars 49 forks source link

[SOLVED] Chapter 6 is mentioning an error which no longer occurs #37

Open theqp opened 3 years ago

theqp commented 3 years ago

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>
ehamberg commented 3 years ago

@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
  |          ^^^^