[Haskell-cafe] Expression problem in the database?

Manuel Gómez targen at gmail.com
Mon Jul 22 21:00:49 CEST 2013

Hi café,

I don’t know whether this is a good forum to ask about this —perhaps
Stack Overflow is better suited—, but since Haskell and related
languages are so finely fit for good solutions to the expression
problem, I figure this list may have a few helpful pointers regarding
this problem.

I’m facing a situation that requires saving boolean expressions of
more or less arbitrary structure (encoding some business rules) into a
SQL database.  The expressions can include terms that refer to some
other objects in the database.  I realize this is somewhat vague, but
that’s intentional: I’m really searching for ideas and experience with
similar problems, not for a specific solution to my specific

Though I’m uncertain of their relative merits, I’ve had a few ideas myself:

*   I could sacrifice relational integrity and store the expression
serialized, perhaps as an AST represented in JSON or somesuch —
although the rest of the data model is a rather traditional,
normalized relational schema, so this is undesirable in my situation
if only for consistency.

*   I could directly model some form of the expression’s AST in the
relational schema, and then endure the usual pain of representing sum
types as relations.  I imagine this to be quite unpleasant, although I
haven’t tried it.

*   As these are boolean logic predicates, the expressions could be
transformed into some normal form that could be more easily
represented with relations.

I’ll be mostly (but not exclusively) using this database from an
application written in Haskell (Yesod using Esqueleto to talk to
PostgreSQL), so some native Haskell solution could work almost as well
as an SQL solution — but some legacy systems do need to interact with
the database, so I’d prefer a solution in the database as long as it’s
not horrible to work with in the application.

I’ll be grateful for any ideas or pointers — my Googling for the
expression problem has not proved fruitful.  Thanks!

More information about the Haskell-Cafe mailing list