
On 14/03/07, Bryan O'Sullivan
This wants to be a bit more concrete:
data ThingCompare = TC (a -> a -> Bool) (Thing -> a)
so that you could then have something to execute your comparison thingy:
runTC :: ThingCompare -> Thing -> Thing -> Bool runTC (TC compare extract) a b = compare (extract a) (extract b)
...ok, I'm stuck again. I'm still paralleling "How to write a financial contract", so I'd now like to combine ThingCompare. This is my code: ----------- data Thing = Thing { field_one :: String, field_two :: String, field_three :: Integer } t1 :: Thing t1 = Thing { field_one = "foo", field_two = "bar", field_three = 17 } t2 :: Thing t2 = Thing { field_one = "foo", field_two = "baz", field_three = 13 } type BooleanOp a = a -> a -> Bool type Field a = Thing -> a data ThingCompare a = TC (BooleanOp a) (Field a) | And (ThingCompare a) (ThingCompare a) | Or (ThingCompare a) (ThingCompare a) and :: ThingCompare a -> ThingCompare a -> ThingCompare a and tc1 tc2 = And tc1 tc2 or :: ThingCompare a -> ThingCompare a -> ThingCompare a or tc1 tc2 = Or tc1 tc2 runTC :: ThingCompare a -> Thing -> Thing -> Bool runTC (TC compare extract) x y = compare (extract x) (extract y) runTC (And tc1 tc2) x y = (runTC tc1 x y) && (runTC tc2 x y) runTC (Or tc1 tc2) x y = (runTC tc1 x y) || (runTC tc2 x y) tcEqOne = TC (==) field_one tcEqTwo = TC (==) field_two tcGtThree = TC (>) field_three ---------- So I can happily write, for example, tcEqBoth = and tcEqOne tcEqTwo runTC tcEqBoth t1 t2 and hugs will tell me "False". But if I want to combine tcEqOne and tcGtThree I run into type problems, because one of them uses Strings and the other Integers. I want to break the type dependence between the arguments of "And"; can I do this? If I type: data ThingCompare a = TC (BooleanOp a) (Field a) | And (ThingCompare b) (ThingCompare c) | Or (ThingCompare b) (ThingCompare c) hugs complains that it doesn't know what "b" and "c" are. -- John.