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

Denis Moskvin dmoskvin at gmail.com
Fri Mar 30 13:44:43 CEST 2012


f :: (Show a, Ord b) => (a -> String, b -> b -> Bool)
f = let commond_definitions = undefined in
    let f1 = id.show
        f2 x = (< x)
    in
      (f1, f2)

> From: Ting Lei <tinlyx at hotmail.com>
> To: <haskell-cafe at haskell.org>
> Cc:
> Date: Wed, 28 Mar 2012 23:42:30 -0700
> Subject: [Haskell-cafe] a code that cannot compile with or without
> NoMonomorphismRestriction
> 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

Denis Moskvin



More information about the Haskell-Cafe mailing list