<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
Ketil,<BR> <BR>Thanks for the response. It seems that defining them as a pair only postphones the error.<BR>GHC will give an error when you extract the components of the pair, no matter whether you add<BR>the "NoMonomorphismRestriction" flag or not.<BR> <BR>--{-# LANGUAGE NoMonomorphismRestriction #-}<BR>p :: (Show a, Ord b) => (a -> String, b -> b -> Bool)<br>p = (id . show, flip (<))<BR>f1 = fst p<br>f2 = snd p<BR>-----------------------<BR>Without NoMonomorphismRestriction, I got:<BR><br>D:\work\test1.hs:6:10:<br> Ambiguous type variable `a0' in the constraint:<br> (Show a0) arising from a use of `p'<br> Possible cause: the monomorphism restriction applied to the following:<br> f1 :: a0 -> String (bound at D:\work\hsOcaml\test1.hs:6:1)<br> Probable fix: give these definition(s) an explicit type signature<br> or use -XNoMonomorphismRestriction<br> In the first argument of `fst', namely `p'<br> In the expression: fst p<br> In an equation for `f1': f1 = fst p<BR>D:\work\hsOcaml\test1.hs:6:10:<br> Ambiguous type variable `b0' in the constraint:<br> (Ord b0) arising from a use of `p'<br> Probable fix: add a type signature that fixes these type variable(s)<br>...... <BR>Failed, modules loaded: none.<BR> <BR>------------------<BR>With NoMonomorphismRestriction, I got:<BR> <BR><br>D:\work\test1.hs:6:10:<br> Ambiguous type variable `b0' in the constraint:<br> (Ord b0) arising from a use of `p'<br> Probable fix: add a type signature that fixes these type variable(s)<br> In the first argument of `fst', namely `p'<br> In the expression: fst p<br> In an equation for `f1': f1 = fst p<BR>D:\work\test1.hs:7:10:<br> Ambiguous type variable `a0' in the constraint:<br> (Show a0) arising from a use of `p'<br> Probable fix: add a type signature that fixes these type variable(s)<br> In the first argument of `snd', namely `p'<br> In the expression: snd p<br> In an equation for `f2': f2 = snd p<br>Failed, modules loaded: none.<BR> <BR> <BR>Thanks,<BR><br> Ting<BR> <BR><div><div id="SkyDrivePlaceholder"></div>> From: ketil@malde.org<br>> To: tinlyx@hotmail.com<br>> CC: haskell-cafe@haskell.org<br>> Subject: Re: [Haskell-cafe] a code that cannot compile with or without NoMonomorphismRestriction<br>> Date: Thu, 29 Mar 2012 12:27:04 +0200<br>> <br>> Ting Lei <tinlyx@hotmail.com> writes:<br>> <br>> > (f1, f2) =<br>> > let commond_definitions = undefined in<br>> > let f1 = id.show <br>> > f2 x = (< x) <br>> > in<br>> > (f1, f2)<br>> <br>> I think the type signatures should be:<br>> <br>> f1 :: Show a => a -> String<br>> <br>> and <br>> <br>> f2 :: Ord b => b -> b -> Bool <br>> <br>> When I define these separately, this works:<br>> <br>> f1 :: Show a => a -> String<br>> f1 = id . show<br>> <br>> f2 :: Ord b => b -> b -> Bool <br>> f2 = flip (<)<br>> <br>> <br>> But when I define them as a pair<br>> <br>> f1 :: Show a => a -> String<br>> f2 :: Ord b => b -> b -> Bool <br>> (f1,f2) = (id . show, flip (<))<br>> <br>> I get an error message:<br>> <br>> Line 9: 1 error(s), 0 warning(s)<br>> <br>> Couldn't match expected type `forall a. Show a => a -> String'<br>> with actual type `a -> String'<br>> When checking that `f1'<br>> has the specified type `forall a1. Show a1 => a1 -> String'<br>> <br>> Defining the pair at once works:<br>> <br>> p :: (Show a, Ord b) => (a -> String, b -> b -> Bool)<br>> p = (id . show, flip (<))<br>> <br>> I guess that didn't help a lot, somebody with deeper GHC-fu than me will<br>> have to step in.<br>> <br>> -k<br>> -- <br>> If I haven't seen further, it is by standing in the footprints of giants<br></div>                                            </div></body>
</html>