0

The following code compiles without issue:

import Text.ParserCombinators.Parsec (many, noneOf, GenParser)

notNewline :: GenParser Char st String
notNewline = many (noneOf "\n")

main = print "Hello"

But once I comment the type signature, like so:

import Text.ParserCombinators.Parsec (many, noneOf, GenParser)

--notNewline :: GenParser Char st String
notNewline = many (noneOf "\n")

main = print "Hello"

I get the following error:

No instance for (Text.Parsec.Prim.Stream s0 m0 Char)
  arising from a use of `noneOf'
Possible fix:
  add an instance declaration for
  (Text.Parsec.Prim.Stream s0 m0 Char)
In the first argument of `many', namely `(noneOf "")'
In the expression: many (noneOf "")
In an equation for `notNewline': notNewline = many (noneOf "")

Why do I get an error message from omitting the type signature?

Clinton
  • 22,361
  • 15
  • 67
  • 163
  • Do you have the monomorphism restriction on? Try `{-# LANGUAGE NoMonomorphismRestriction #-}` at the top of your file. – bheklilr Dec 12 '14 at 04:39
  • Beat me to it by 40 seconds. That's the one. An answer explaining it would be appreciated though, I think I roughly understand why. – Clinton Dec 12 '14 at 04:41
  • Mind if I just mark it as a duplicate and link it back to a previous explanation I've written? – bheklilr Dec 12 '14 at 04:41
  • If I can find one that I've written that looks close enough... or written by someone else. This problem shows up often enough that the more questions we have pointing at it the better. – bheklilr Dec 12 '14 at 04:43
  • 3
    [Candidate 1](http://stackoverflow.com/questions/1344414/haskell-type-inference-and-function-composition/1344680). [Candidate 2](http://stackoverflow.com/questions/4179453/why-are-polymorphic-values-not-inferred-in-haskell/4179515). [Candidate 3](http://stackoverflow.com/questions/4999020/specific-type-inference-using-uncurry-function/4999860). – bheklilr Dec 12 '14 at 04:47
  • Mark it as a duplicate, go for it. – Clinton Dec 12 '14 at 04:50

0 Answers0