Using Parsec for simple arithmetic language I've been reading Types and Programming Languages and I wanted to try to implement the first language in Haskell to understand it properly

I have barely written any Haskell before and not used Parsec so I would grateful for any feedback on this code

Here are some specific points I am unsure about


Is my main function sensible?
Can the eval function be expressed any better?
I'm unhappy with functionParser and ifParser. How can I code these better. In particular can the ifParser be coded in an applicative style?


If there's anything else you consider odd don't hesitate to mention it

import Control.Monad  
import System.Environment   
import Text.ParserCombinators.Parsec

data Term = TmTrue
          | TmFalse
          | TmIf Term Term Term
          | TmZero
          | TmSucc Term
          | TmPred Term
          | TmIsZero Term
          | TmError
    deriving Show

main :: IO[()]
main = do 
    args &lt;- getArgs
    forM args (\arg -&gt; case parseArith arg of
                Left e -&gt; print e 
                Right term -&gt; print $ eval term)

isNumerical :: Term -&gt; Bool
isNumerical term = case term of
    TmZero -&gt; True
    TmSucc subterm -&gt; isNumerical subterm
    TmPred subterm -&gt; isNumerical subterm
    _ -&gt; False

eval :: Term -&gt; Term
eval term = case term of 
    TmTrue -&gt; TmTrue
    TmFalse -&gt; TmFalse
    TmZero -&gt; TmZero
    TmIf term1 term2 term3 -&gt; case eval term1 of
        TmTrue -&gt; eval term2
        TmFalse -&gt; eval term3
        _ -&gt; TmError
    TmIsZero subterm -&gt; case eval subterm of
        TmZero -&gt; TmTrue
        t2 | isNumerical t2 -&gt; TmFalse
        _ -&gt; TmError
    TmPred TmZero -&gt; TmZero
    TmPred (TmSucc subterm) -&gt; eval subterm
    TmSucc subterm -&gt; case eval subterm of
        t2 | isNumerical t2 -&gt; TmSucc t2
        _ -&gt; TmError
    _ -&gt; TmError

parseArith :: String -&gt; Either ParseError Term
parseArith input = parse arithParser "Failed to parse arithmetic expression" input

arithParser :: GenParser Char st Term
arithParser = try( ifParser ) 
          &lt;|&gt; try( succParser )
          &lt;|&gt; try( predParser )
          &lt;|&gt; try( isZeroParser ) 
          &lt;|&gt; try( trueParser )
          &lt;|&gt; try( falseParser )
          &lt;|&gt; try( zeroParser )

trueParser :: GenParser Char st Term
trueParser = string "true" &gt;&gt; return TmTrue

falseParser :: GenParser Char st Term
falseParser = string "false" &gt;&gt; return TmFalse

zeroParser :: GenParser Char st Term
zeroParser = char '0' &gt;&gt; return TmZero

functionParser :: String -&gt; (Term -&gt; Term) -&gt; GenParser Char st Term
functionParser name funcTerm = do
    string $ name ++ "(" 
    term &lt;- arithParser
    char ')'
    return $ funcTerm term

succParser :: GenParser Char st Term
succParser = functionParser "succ" TmSucc

predParser :: GenParser Char st Term
predParser = functionParser "pred" TmPred

isZeroParser :: GenParser Char st Term
isZeroParser = functionParser "iszero" TmIsZero

ifParser :: GenParser Char st Term
ifParser = do
    string "if"
    spaces
    term1 &lt;- arithParser
    spaces
    string "then"
    spaces
    term2 &lt;- arithParser
    spaces
    string "else"
    spaces
    term3 &lt;- arithParser
    return $ TmIf term1 term2 term3
Where you used case you could have used pattern matching. This yields more idiomatic code in many cases.

isNumerical :: Term -&gt; Bool
isNumerical TmZero = True
isNumerical (TmSucc subterm) = isNumerical subterm
isNumerical (TmPred subterm) = isNumerical subterm
isNumerical _ = False

eval :: Term -&gt; Term
eval TmTrue  = TmTrue
eval TmFalse = TmFalse
eval TmZero  = TmZero
eval (TmIf term1 term2 term3) = evalIf (eval term1) term2 term3
eval (TmIsZero subterm) = evalIsZero $ eval subterm
eval (TmPred subterm) = evalPred $ eval subterm
eval (TmSucc subterm) = evalSucc $ eval subterm
eval TmError = TmError

evalIf :: Term -&gt; Term -&gt; Term -&gt; Term
evalIf TmTrue a  _ = eval a
evalIf TmFalse _ b = eval b
evalIf _ _ _  = TmError

evalIsZero :: Term -&gt; Term
evalIsZero TmZero = TmTrue
evalIsZero term
    | isNumerical term = TmFalse
    | otherwise        = TmError

evalPred :: Term -&gt; Term
evalPred TmZero = TmZero
evalPred t@(TmSucc subterm) = t
evalPred _ = TmError

evalSucc :: Term -&gt; Term
evalSucc term
    | isNumerical term = TmSucc term
    | otherwise        = TmError


Note the complex terms are farmed off to their own functions. This makes testing easier. Especially as the runtime gets more complex.

You asked about main's type. If you use forM_ you can define it as main :: IO ().

As for Applicative:

functionParser :: String -&gt; (Term -&gt; Term) -&gt; GenParser Char st Term
functionParser name funcTerm = funcTerm &lt;$&gt; (string (name ++ "(")
                                             *&gt; arithParser
                                             &lt;* char ')')
ifParser :: GenParser Char st Term
ifParser = TmIf &lt;$&gt; (string "if" *&gt; spaces *&gt; arithParser)
                &lt;*&gt; (spaces *&gt; string "then" *&gt; spaces *&gt; arithParser)
                &lt;*&gt; (spaces *&gt; string "else" *&gt; spaces *&gt; arithParser)


Also, the last 'try' should not be used. 'try' means to attempt a parse and do not consume the input if it failed. The last parse action is the end of the parse so there is no need to leave the input in the parser.