[Haskell-cafe] combinators for a simple grammar

Rahul Kapoor rk at trie.org
Mon Aug 6 22:33:01 EDT 2007

I am having problems coming up with nice combinators for a simple
DSEL. The abstract syntax is simply given by the types:

data SearchCondition = SearchCondition BoolTerm | OpOr SearchCondition  BoolTerm

data BoolTerm = BoolTerm BoolFactor | OpAnd BoolTerm BoolFactor

data BoolFactor = Constant Bool

My current combinators are

(.||.) :: SearchCondition -> BoolTerm -> SearchCondition
(.||.) sc term = OpOr sc term

(.&&.) :: BoolTerm -> BoolFactor -> BoolTerm
(.&&.) term fact = OpAnd term fact

which allow you to write expression of the form

factTrue = Constant True
termTrue = BoolTerm factTrue
scTrue = SearchCondition termTrue

sc = scTrue .||. termTrue .||. termTrue .&&. factTrue

I am wondering it it is possible to define combinators to hide the
structure of the grammar from the user to some extent, so that the
user can simply write things like

sc = True .||.True .||. True .&&. False


sc = const True .||. const True .||. const True .&&. const  False

That is the user does not have to worry about the distinction between
terms and factors (which exists solely for precedence in this case).

Or is it a better idea to just remove the precedence rules from the
types and move it the part of the code that evaluates the expressions?


More information about the Haskell-Cafe mailing list