[Haskell-cafe] a code that cannot compile with or without NoMonomorphismRestriction

Haisheng Wu freizl at gmail.com
Thu Mar 29 18:07:12 CEST 2012


I think the error message tell you how to fix:
   use -XNoMonomorphismRestriction
One approach is add following line into top of your hs file and it works for me.
   {-# LANGUAGE NoMonomorphismRestriction #-}

Regarding the deeper reason, I think you would be able to find via GHC
user guide and google.

-Haisheng


On Thu, Mar 29, 2012 at 2:42 PM, Ting Lei <tinlyx at hotmail.com> wrote:
> Hi
>
> I have met a piece of code that cannot be compiled whether I add or remove
> the NoMonomorphismRestriction flag (as of GHC 7.0.4, Haskell platform
> 2011.4.0.0).
> I have extracted a minimal example below:
>
>
>
> {-# LANGUAGE NoMonomorphismRestriction #-}
> (f1, f2) =
>     let commond_definitions = undefined in
>     let f1 = id.show
>         f2 x = (< x)
>     in
>       (f1, f2)
>
> I needed this format because there are many shared definitions in
> common_definitions for f1 and f2, and I want to keep them local.
>
> If I compile them with NoMonomorphismRestriction, I get:
>
> D:\work\test.hs:7:8:
>     Ambiguous type variable `a0' in the constraint:
>       (Show a0) arising from a use of `f1'
>     Possible cause: the monomorphism restriction applied to the following:
>       f1 :: a0 -> String (bound at D:\work\hsOcaml\test.hs:2:2)
>     Probable fix: give these definition(s) an explicit type signature
>     In the expression: f1
>     In the expression: (f1, f2)
>     In the expression:
>       let
>         f1 = id . show
>         f2 x = (< x)
>       in (f1, f2)
> D:\work\test.hs:7:12:
>     Ambiguous type variable `a1' in the constraint:
>       (Ord a1) arising from a use of `f2'
>     Possible cause: the monomorphism restriction applied to the following:
>       f2 :: a1 -> a1 -> Bool (bound at D:\work\hsOcaml\test.hs:2:6)
>     Probable fix: give these definition(s) an explicit type signature
>     In the expression: f2
>     In the expression: (f1, f2)
>     In the expression:
>       let
>         f1 = id . show
>         f2 x = (< x)
>       in (f1, f2)
> Failed, modules loaded: none.
>
> If I comment out
> -- {-# LANGUAGE NoMonomorphismRestriction #-}
> I get:
>
> D:\work\hsOcaml\test.hs:4:17:
>     Ambiguous type variable `a0' in the constraint:
>       (Show a0) arising from a use of `show'
>     Possible cause: the monomorphism restriction applied to the following:
>       f1 :: a0 -> String (bound at D:\work\hsOcaml\test.hs:2:2)
>     Probable fix: give these definition(s) an explicit type signature
>                   or use -XNoMonomorphismRestriction
>     In the second argument of `(.)', namely `show'
>     In the expression: id . show
>     In an equation for `f1': f1 = id . show
> D:\work\hsOcaml\test.hs:7:12:
>     Ambiguous type variable `a1' in the constraint:
>       (Ord a1) arising from a use of `f2'
>     Possible cause: the monomorphism restriction applied to the following:
>       f2 :: a1 -> a1 -> Bool (bound at D:\work\hsOcaml\test.hs:2:6)
>     Probable fix: give these definition(s) an explicit type signature
>                   or use -XNoMonomorphismRestriction
>     In the expression: f2
>     In the expression: (f1, f2)
>     In the expression:
>       let
>         f1 = id . show
>         f2 x = (< x)
>       in (f1, f2)
> Failed, modules loaded: none.
>
> Can anyone show me why this does not work and how to fix it (e.g. by adding
> type signature as the error message suggested)?
> I tried to add type signature by couldn't figure out the right way of doing
> it.
>
> Thanks in advance!
>
> Ting
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list