Personal tools

GroteTrap

From HaskellWiki

Jump to: navigation, search

GroteTrap allows you to quickly define expression languages. It is available for download on Hackage.

An example:

data Logic
  = Var String
  | Or [Logic]
  | And [Logic]
  | Impl Logic Logic
  | Not Logic
  deriving (Show, Eq)
 
logicLanguage :: Language Logic
logicLanguage = language
  { variable  = Var
  , operators =
      [ Unary    Not  Prefix 0 "!"
      , Nary     And  True   1 "&&"
      , Nary     Or   True   2 "||"
      , Binary   Impl InfixR 3 "->"
      ]
  }

With this, we can do:

> readExpression logicLanguage "p && q"
And [Var "p",Var "q"]

Categories:Libraries