Skip to content

Instantly share code, notes, and snippets.

@beastaugh
Created December 3, 2012 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beastaugh/4191806 to your computer and use it in GitHub Desktop.
Save beastaugh/4191806 to your computer and use it in GitHub Desktop.
New parser for Hatt with support for associativity and operator precedence
module Data.Logic.Propositional.Parser2 where
import Data.Logic.Propositional
import Text.Parsec.Prim ((<|>), (<?>), runPT)
import Text.Parsec.Char (char, letter, spaces, string)
import Text.Parsec.Expr
parseExpr' = runPT expr ()
expr = buildExpressionParser operators term
<?> "compound expression"
term = parens expr
<|> variable
<?> "expression"
variable = do c <- letter
return $ Variable (Var c)
<?> "variable"
parens p = do char '('
spaces
x <- p
spaces
char ')'
return x
<?> "parens"
operators = [ [Prefix (string "~" >> return Negation)]
, [binary "&" Conjunction]
, [binary "|" Disjunction]
, [binary "->" Conditional]
, [binary "<->" Biconditional]
]
binary n c = Infix (spaces >> string n >> spaces >> return c) AssocRight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment