Why is this specification only compiled with "-XNoMonomorphismRestriction"?

I get some strange errors in the form of a stitch of the form "There is no instance for (Test a0) associated with an expression type signature." Here is the simplest version of the offensive code that I could come up with:

class Test a where
  test :: a

foo = test

Adding a type does not help:

foo = test :: Test a => a

However, adding an instance of Test makes it compiled:

instance Test Int where
  test = 0

This is not entirely acceptable as I want my instances to be declared elsewhere.

Finally, passing -XNoMonomorphismRestrictionin ghc (i) also allows compilation. While this is good enough, I don’t understand what this extension does, why it is necessary or what shortcomings may be hidden.

+4
source share
2

( , , ), , :

foo :: Test a => a
foo = test

test, test foo.

+10

, , . , , , , ( , ).

"" ( ). ; -XNoMonomorphismRestriction ? /, , , , MR .

+1

All Articles