+--- -*-haskell-*-
+---
+--- Haskell implementation of a `same-fringe' solver.
+
+import System.IO
+import System.Environment
+import System.Exit
+import Control.Monad
+
+-----------------------------------------------------------------------------
+--- Parser combinators.
+
+-- A very simple parser monad.
+newtype Parser t a = Parser { runparse :: [t] -> Either String (a, [t]) }
+
+instance Monad (Parser t) where
+ return x = Parser $ \ts -> Right (x, ts)
+ fail err = Parser $ \_ -> Left err
+ (Parser p) >>= f = Parser $ \ts -> case p ts of
+ Right (x, ts) -> runparse (f x) ts
+ Left err -> Left err
+
+-- Access to the token stream.
+peek = Parser $ \ts -> case ts of
+ t:_ -> Right (Just t, ts)
+ _ -> Right (Nothing, ts)
+
+step = Parser $ \ts -> case ts of
+ _:ts -> Right ((), ts)
+ _ -> Left "unexpected end-of-file"