
Andrew Hunter wrote:
Several times now I've had to define an EDSL for working with (vaguely) numeric expressions. For stuff like 2*X+Y, this is easy, looking pretty much like:
data Expr = Const Integer | Plus Expr Expr | Times Expr Expr
instance Num Expr where fromInterger = Const (+) = Plus (*) = Times
Does anyone know of a good solution, here? Are there good substitutions for all the six operators that are important (<,>,>=,<=,==,/=), that are close enough to be pretty-looking but not used for other important modules?
If you're just wanting to build Exprs, then the canonical solution is to use ':' as in (:>), (:>=), (:==), (:/=), (:<=), (:<). The colon is considered a "capital symbol" and so it's what you use as the first letter of symbolic constructors. For symmetry, many folks will ad another colon at the end as well.
data Expr = Const Integer | Expr :+: Expr | Expr :*: Expr | Expr :>: Expr | ...
-- Live well, ~wren