Parsing expressions with combinators I'm very new to Haskell's ReadP library, and the entire concept of parser combinators, so I was wondering whether there are better ways to do some things in this program:

import Text.ParserCombinators.ReadP
import Control.Applicative
import Data.List

data Operator = Add | Subtract

instance Show Operator where
    show Add = "+"
    show Subtract = "-"

instance Read Operator where
    readsPrec _ "+" = [(Add, "")]
    readsPrec _ "-" = [(Subtract, "")]
    readsPrec _ _ = []

data Expression = Number Int
                | Infix { left :: Expression, op :: Operator, right :: Expression }

instance Show Expression where
    show (Number x) = show x
    show (Infix left op right) = "(" ++ (show left) ++ " " ++ (show op) ++ " " ++ (show right) ++ ")"

digit :: ReadP Char
digit = satisfy $ \char -&gt; char &gt;= '0' &amp;&amp; char &lt;= '9'

number :: ReadP Expression
number = fmap (Number . read) (many1 digit)

operator :: ReadP Operator
operator = fmap read (string "+" &lt;|&gt; string "-")

expression :: ReadP Expression
expression = do
    skipSpaces
    left &lt;- number
    skipSpaces
    op &lt;- Control.Applicative.optional operator
    case op of
        Nothing -&gt; return left
        Just op -&gt; do
            skipSpaces
            right &lt;- expression
            return (Infix left op right)

parseExpression :: String -&gt; Maybe Expression
parseExpression input = case readP_to_S expression input of
    [] -&gt; Nothing
    xs -&gt; (Just . fst . last) xs


The main area where I'm looking for improvements is the expression function, and specifically the things which seem improvable are the repeated calls to skipSpaces and the case expression to check whether an operator was parsed, but of course if you notice anything else that would be helpful too!
First, a standard approach for dealing with the skipSpaces issue is to define a higher-order parser combinator, traditionally called lexeme:

lexeme :: ReadP a -&gt; ReadP a
lexeme p = p &lt;* skipSpaces


Here, lexeme takes a space-naive parser p, and converts it into a new parser that parses whatever p was planning to parse, and then reads and discards any trailing spaces.  You use lexeme in the definitions of any of your parsers that would reasonably be assumed to read a complete "lexeme" and ignore any trailing space.  For example, number should be a lexeme parser:

number :: ReadP Expression
number = lexeme $ fmap (Number . read) (many1 digit)


So should operator, though obviously not digit!  expression won't need to use lexeme, because we'll arrange to end it with a lexeme parser.

It's also helpful to define a symbol parser which is essentially string that ignores trailing spaces:

symbol :: String -&gt; ReadP String
symbol = lexeme . string


Consistently used, lexeme (and symbol) will deal with all unwanted spaces, other than any leading spaces at the very start of your parse.  If you have a non-recursive "top level" grammar production, like say a parser for program :: ReadP Program, then you would probably deal with them there.  In your example, you don't have such a production (e.g., expression is recursive), so you'd stick an extra skipSpaces in parseExpression.  This is also a good place to put eof to make sure there isn't any trailing material that you aren't parsing:

parseExpression :: String -&gt; Maybe Expression
parseExpression input = case readP_to_S (skipSpaces *&gt; expression &lt;* eof) input of
    [] -&gt; Nothing
    xs -&gt; (Just . fst . last) xs


Second, your use of a Read instance for parsing your operators is very unusual.  It would be more standard to make it a helper in the operator parser, writing something like:

operator :: ReadP Operator
operator = readSymbol &lt;$&gt; (symbol "+" &lt;|&gt; symbol "-")
  where readSymbol "+" = Add
        readSymbol "-" = Subtract


(though an even more standard version is given below).

Third, in expression, you can avoid the case construct by using alternation (&lt;|&gt;) like so:

expression' :: ReadP Expression
expression' = do
    left &lt;- number
    (do op &lt;- operator
        right &lt;- expression
        return (Infix left op right)
     &lt;|&gt; return left)


This would be the standard approach for non-parallel parser libraries (e.g., Parsec or Megaparsec).  For ReadP, it's better to replace the (&lt;|&gt;) operator with the ReadP-specific (&lt;++) operator to avoid also following the unwanted second parse in parallel.  Beware that (&lt;++) has higher precedence than (&lt;|&gt;), so some extra parentheses might be needed if it's being used in combination with other operators, as in the examples below.

Fourth, you've probably noticed my use of the applicative operators &lt;* and *&gt; and the alias &lt;$&gt; for fmap in the code above.  It is very common to use these -- plus the additional applicative operator &lt;*&gt; and sometimes the operators &lt;**&gt; or &lt;$ -- in parsers.  Once you get used to them, they tend to lead to less cluttered code.

For example, a more standard way of writing expression would be:

expression' :: ReadP Expression
expression' =     Infix &lt;$&gt; number &lt;*&gt; operator &lt;*&gt; expression
              &lt;|&gt; number


or the slightly more efficient solution:

expression :: ReadP Expression
expression = do
  left &lt;- number
  (Infix left &lt;$&gt; operator &lt;*&gt; expression) &lt;++ return left


Note that, in the context of parsers, an expression like f &lt;$&gt; p &lt;*&gt; q means "try to run the parser p, and then the parser q; assuming they both succeed, pass their return values to f".  In other words, that Infix expression is essentially:

Infix left op right


where op is the return value from the parser operator and right is the return value from the parser expression.

Similarly, the standard way of writing operator is actually:

operator :: ReadP Operator
operator = Add &lt;$ symbol "+" &lt;|&gt; Subtract &lt;$ symbol "-"


This one requires an additional word of explanation.  The operator &lt;$ is kind of an odd duck.  It's type signature is:

(&lt;$) :: a -&gt; f b -&gt; f a


but in the context of parsers specifically, the meaning of x &lt;$ p is "try to run the parser p; if it succeeds, ignore its return value and return x".  Basically, it's used to replace the return value of a parser that's used only for its success or failure and not its return value.

Note that these versions of expression, like your original version, treat the operators as right associative.  This may be a problem if you're trying to parse "1-2-3" as equivalent to "(1-2)-3" instead of "1-(2-3)".

A few additional minor points:


isDigit is a more readable name for \c -&gt; c &gt;= '0' &amp;&amp; c &lt;= '9'
munch1 is more efficient than many1 (satisfy xxx), so I'd redefine number to use it
for testing, it's probably a good idea to have a parseExpressions function that looks at all the parses
for production, it's probably a good idea to check for ambiguous parses and do something about it, rather than (fairly arbitrarily) selecting the last parse in the list


With all of these suggestions implemented, the final version would look something like:

{-# OPTIONS_GHC -Wall #-}

import Data.Char (isDigit)
import Control.Applicative
import Text.ParserCombinators.ReadP (eof, munch1, ReadP, readP_to_S,
                                     skipSpaces, string, (&lt;++))

data Operator = Add | Subtract

data Expression = Number Int
                | Infix { left :: Expression, op :: Operator, right :: Expression }

lexeme :: ReadP a -&gt; ReadP a
lexeme p = p &lt;* skipSpaces

symbol :: String -&gt; ReadP String
symbol = lexeme . string

number :: ReadP Expression
number = lexeme $ Number . read &lt;$&gt; munch1 isDigit

operator :: ReadP Operator
operator = Add &lt;$ symbol "+" &lt;|&gt; Subtract &lt;$ symbol "-"

expression :: ReadP Expression
expression = do
  x &lt;- number
  (Infix x &lt;$&gt; operator &lt;*&gt; expression) &lt;++ return x

top :: ReadP Expression
top = skipSpaces *&gt; expression &lt;* eof

parseExpressions :: String -&gt; [(Expression, String)]
parseExpressions = readP_to_S top

parseExpression :: String -&gt; Maybe Expression
parseExpression input = case parseExpressions input of
    [] -&gt; Nothing
    [(x,"")] -&gt; Just x
    _ -&gt; error "ambiguous parse"