[Haskell-cafe] Defining types (newbie)

John Fouhy john at fouhy.net
Tue Mar 13 23:12:04 EDT 2007


Hi all,

I studied Haskell a few years ago at uni, but I haven't used it since,
so I'm a bit rusty.  But I've been inspired by this paper:
http://citeseer.ist.psu.edu/585421.html which talks about building a
combinator language for financial contracts, and which uses Haskell as
the implementation language.

...anyway.  Say I have a data type:

data Thing = Thing { field_one :: String, field_two :: String,
field_three :: Integer }

I'd like to build a datatype that represents comparison rules on
Things.  A comparison rule would be something like "thing1.field_two
== thing2.field_two" or "thing1.field_three > thing2.field_three".

If I was writing python, I might do this as:

class ThingCompare:
    def __init__(self, op, field, thing1, thing2):
        self.op = op
        self.field = field

I could evaluate instances of ThingCompare as:

def evalThingCompare(tc, thing1, thing2):
    return tc.op(getattr(thing1, tc.field), getattr(thing2, tc.field))

In Haskell, I envisage writing something like:

data ThingCompare = TC Op Field

So that if I had:

eq x y = x == y

then I could create a ThingCompare by saying:

tc = eq field_two

And I could evaluate my ThingCompares by defining:

evaltc :: ThingCompare -> Thing -> Thing -> Bool
evaltc (TC o f) t1 t2 = o (f t1) (f t2)

But I can't figure out what types "Op" and "Field" should be.  Any
suggestions?  Am I on the wrong track? (have I been corrupted by
python's dynamic typing? :-) )

-- 
John.


More information about the Haskell-Cafe mailing list