
Hi, Why does this not function? Prelude> sequence [print 'a', print 2] 'a' 2 [(),()] *Prelude> let myprint = print* *Prelude> sequence [myprint 'a', myprint 2]* <interactive>:1:18: Couldn't match expected type `()' against inferred type `Char' In the first argument of `myprint', namely 'a' In the expression: myprint 'a' In the first argument of `sequence', namely `[myprint 'a', myprint 2]' Can providing some type annotations (interactively in ghci or in some .hs file) help solve the problem? Best Regards, CS

Cetin Sert wrote:
Hi,
Why does this not function?
Prelude> sequence [print 'a', print 2] 'a' 2 [(),()] *Prelude> let myprint = print* *Prelude> sequence [myprint 'a', myprint 2]*
<interactive>:1:18: Couldn't match expected type `()' against inferred type `Char' In the first argument of `myprint', namely 'a' In the expression: myprint 'a' In the first argument of `sequence', namely `[myprint 'a', myprint 2]'
The problem is the monomorphism restriction: ~> ghci GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :t print print :: (Show a) => a -> IO () Prelude> let myprint=print Prelude> :t myprint myprint :: () -> IO () Prelude> :q Leaving GHCi. ~> ghci -fno-monomorphism-restriction GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> let myprint=print Prelude> :t myprint myprint :: (Show a) => a -> IO ()
Can providing some type annotations (interactively in ghci or in some .hs file) help solve the problem?
Yes. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

The other simple option is to eta-expand:
let myprint x = print x will get the correct type,
The monomorphism restriction is to stop things that look like *values*
whose computation results are memoized, from turning into *functions*
which need a dictionary context and get recomputed every time they are
accessed. In your declaration, "myprint" looks like a value, so it
should only get computed once. In order to do this it needs to be
monomorphic, because otherwise it could have a different value
depending on where it was used.
-- ryan
On Wed, Dec 10, 2008 at 10:55 PM, Janis Voigtlaender
Cetin Sert wrote:
Hi,
Why does this not function?
Prelude> sequence [print 'a', print 2] 'a' 2 [(),()] *Prelude> let myprint = print* *Prelude> sequence [myprint 'a', myprint 2]*
<interactive>:1:18: Couldn't match expected type `()' against inferred type `Char' In the first argument of `myprint', namely 'a' In the expression: myprint 'a' In the first argument of `sequence', namely `[myprint 'a', myprint 2]'
The problem is the monomorphism restriction:
~> ghci GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :t print print :: (Show a) => a -> IO () Prelude> let myprint=print Prelude> :t myprint myprint :: () -> IO () Prelude> :q Leaving GHCi. ~> ghci -fno-monomorphism-restriction GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> let myprint=print Prelude> :t myprint myprint :: (Show a) => a -> IO ()
Can providing some type annotations (interactively in ghci or in some .hs file) help solve the problem?
Yes.
-- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

You can disable the monomorphism restriction in your .ghci so it needn't trouble your interactive sessions further. My .ghci follows my signature. -- _jsn :set -XOverlappingInstances :set -XNoMonomorphismRestriction :set -XUnicodeSyntax :set -XArrows :set -Wall :set -fno-warn-name-shadowing :set -fwarn-unused-imports :set -fglasgow-exts :set +t
participants (4)
-
Cetin Sert
-
Janis Voigtlaender
-
Jason Dusek
-
Ryan Ingram