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.