I have the following data structures:
data Operator :: * -> * where
StringEquals :: Operator String
StringNotEquals :: Operator String
NumericEquals :: Operator Integer
NumericNotEquals :: Operator Integer
data Variable :: * -> * where
UserName :: Variable String
RequestPath :: Variable String
BodyLength :: Variable Integer
data Value :: * -> * where
StringValue :: String -> Value String
NumericValue :: Integer -> Value Integer
data Condition a = Condition (Operator a) (Variable a) (Value a)
that will be used in functions like these:
extractVariable :: Variable a -> HttpRequest -> a
evaluate :: Operator a -> Value a -> a -> Bool
I'd like to parse Conditions from a JSON file. I can parse the parts individually, wrapped with Some, but not sure how to construct a Condition from them.
The following works and shows what I'd like to do, but it obviously isn't scalable to write:
import Data.Some
parseCondition :: Some Operator -> Some Variable -> Some Value -> Parser (Some Condition)
parseCondition (Some op@StringEquals) (Some var@UserName) (Some val@(StringValue _)) = return $ mkSome $ Condition op var val
parseCondition (Some op@StringNotEquals) (Some var@UserName) (Some val@(StringValue _)) = return $ mkSome $ Condition op var val
parseCondition (Some op@StringEquals) (Some var@RequestPath) (Some val@(StringValue _)) = return $ mkSome $ Condition op var val
parseCondition (Some op@StringNotEquals) (Some var@RequestPath) (Some val@(StringValue _)) = return $ mkSome $ Condition op var val
parseCondition (Some op@NumericEquals) (Some var@BodyLength) (Some val@(NumericValue _)) = return $ mkSome $ Condition op var val
parseCondition _ _ _ = parseFail "incompatible types"
What's the best way to do this in haskell?