small expression evaluator

Hello, on my journey through Haskell I've hit a problem where I don't know any further and would greatly appreciate your help. I've got this code so far: data Value a e = VConst a | VFunc (e -> a) evalV :: Value a e -> e -> a evalV (VConst x) = const x evalV (VFunc f) = f liftV :: (a -> a -> t) -> (Value a e) -> (Value a e) -> e -> t liftV f x y e = (evalV x e) `f` (evalV y e) (.==.), (./=.) :: (Eq a) => (Value a e) -> (Value a e) -> e -> Bool (.==.) = liftV (==) (./=.) = liftV (/=) (.&&.), (.||.) .... some more operators This allows me to write client code like this: data Person = Person { personName :: String , personAge :: Int } deriving (Show) exampleExpr :: Bool exampleExpr = (VConst 99) .==. (VFunc personAge) $ Person "pete" 99 I was wondering, whether it'd be possible to enable defining expression without the Value data constructors, i.e. 99 .==. personAge $ Person "pete" 99 I've tried using a type class to have a function for lifting different types into Values. class ToValue a e | a -> e where valueLift :: a -> Value a e then I tried defining an instance for functions: instance ToValue ((->) Person a) Person where valueLift f = undefined in ghci:
:t valueLift personName valueLift personName :: Value (Person -> String) Person
and suddenly I realized this will not work because I would need the type `Value String Person'. Now, I'm stuck and don't know which path to take in order to come closer to being able writing "99 .==. personAge" or "personAge .==. 99". Maybe I'm approaching it in a completely wrong way or it's simply not possible. Do you have any ideas, hints? Many thanks in advance for any kind of feedback, pete.

On Tue, 22 Mar 2011 08:56:45 +0100, Petr Novotnik
data Person = Person { personName :: String , personAge :: Int } deriving (Show)
exampleExpr :: Bool exampleExpr = (VConst 99) .==. (VFunc personAge) $ Person "pete" 99
I was wondering, whether it'd be possible to enable defining expression without the Value data constructors, i.e.
99 .==. personAge $ Person "pete" 99
You can write: 99 == personAge (Person "pete" 99) Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

On Tue, 22 Mar 2011 10:14:57 +0100, Henk-Jan van Tuyl
On Tue, 22 Mar 2011 08:56:45 +0100, Petr Novotnik
wrote: data Person = Person { personName :: String , personAge :: Int } deriving (Show)
exampleExpr :: Bool exampleExpr = (VConst 99) .==. (VFunc personAge) $ Person "pete" 99
I was wondering, whether it'd be possible to enable defining expression without the Value data constructors, i.e.
99 .==. personAge $ Person "pete" 99
You can write: 99 == personAge (Person "pete" 99)
Or you could write: c .==. f = \x -> c == f x test = 99 .==. personAge $ Person "pete" 99 The .==. operator is not symmetrical in this case, of course Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

Hello Henk-Jan, many thanks for your answer. Yes, I could do what you propose. But I still wonder if it is possible or not to the "lifting" as I mentioned earlier. Again thanks for answering, pete. On 03/22/2011 01:43 PM, Henk-Jan van Tuyl wrote:
On Tue, 22 Mar 2011 10:14:57 +0100, Henk-Jan van Tuyl
wrote: On Tue, 22 Mar 2011 08:56:45 +0100, Petr Novotnik
wrote: data Person = Person { personName :: String , personAge :: Int } deriving (Show)
exampleExpr :: Bool exampleExpr = (VConst 99) .==. (VFunc personAge) $ Person "pete" 99
I was wondering, whether it'd be possible to enable defining expression without the Value data constructors, i.e.
99 .==. personAge $ Person "pete" 99
You can write: 99 == personAge (Person "pete" 99)
Or you could write: c .==. f = \x -> c == f x
test = 99 .==. personAge $ Person "pete" 99
The .==. operator is not symmetrical in this case, of course
Regards, Henk-Jan van Tuyl
participants (2)
-
Henk-Jan van Tuyl
-
Petr Novotnik