Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trivial parsec example produces a type error

Tags:

haskell

parsec

I'm trying to get this trivial parsec code to compile

import Text.Parsec
simple = letter

but I keep getting this error

No instance for (Stream s0 m0 Char)
  arising from a use of `letter'
Possible fix: add an instance declaration for (Stream s0 m0 Char)
In the expression: letter
In an equation for `simple': simple = letter
like image 758
Peter Avatar asked Jul 17 '11 10:07

Peter


People also ask

What are compiler errors in Java?

Compile Time Errors are sometimes also referred to as Syntax errors. These kind of errors are easy to spot and rectify because the java compiler finds them for you. The compiler will tell you which piece of code in the program got in trouble and its best guess as to what you did wrong.

Why can’t I connect to Parsec?

The Parsec web app only works in recent versions of Chrome and Edge, please use one of these browsers or install the Parsec app on your client to continue. Check if the machine is online, get a new link if applicable, or try to restart Parsec on both ends.

How does the compiler know what I did wrong in programming?

The compiler will tell you which piece of code in the program got in trouble and its best guess as to what you did wrong. Usually, the compiler indicates the exact line where the error is, or sometimes the line just before it, however, if the problem is with incorrectly nested braces, the actual error may be at the beginning of the block.

What is a type 2 error in statistics?

Type II Error In statistical hypothesis testing, a type II error is a situation wherein a hypothesis test fails to reject the null hypothesis that is false. In other Conditional Probability Conditional probability is the probability of an event occurring given that another event has already occurred.


Video Answer


1 Answers

I think you have ran against the monomorphism restriction. This restriction means: If a variable is declared with no explicit arguments, its type has to be monomorphic. This forces the typechecker to pick a particular instance of Stream, but it can't decide.

There are two ways to fight it:

  1. Give simple an explicit signature:

    simple :: Stream s m Char => ParsecT s u m Char
    simple = letter
    
  2. Disable the monorphism restriction:

    {-# LANGUAGE NoMonomorphismRestriction #-}
    import Text.Parsec
    simple = letter
    

See What is the monomorphism restriction? for more information on the monomorphism restriction.

like image 99
fuz Avatar answered Sep 22 '22 17:09

fuz