Question

How can I get this Haskell code to type check?

aesonLensInterpreter :: String -> String -> Interpreter (Maybe Value)
aesonLensInterpreter input expr = do
      setImportsQ [("Prelude", Nothing), ("Data.Map", Just "M"), ("Control.Lens", Nothing), ("Data.Aeson.Lens", Nothing), ("Data.Aeson", Nothing)]
      set [languageExtensions := [OverloadedStrings]]

      let interpExpr = "(" ++ (show input) ++ " :: String)" ++ expr
      v <- interpret interpExpr (as :: ???)
      return $ Just (toJSON v)

Basically I want the user to input any possible Lens expression and get the result as a ToJSON instance. Since Lens expressions can both return Values or plain Haskell types I'm confused.

Était-ce utile?

La solution

To answer the immediate question, replace ??? by any type that's an instance of both Typeable and ToJSON. The code will typecheck if you use String, for instance.

Regarding your goal, if the expressions you want to interpret can actually have different types it looks like this approach won't work directly. Since you're interpreting already, I suggest doing the JSON conversion within the interpreter. Something along the lines of prepending interpExpr with toJSON $ and replacing ??? by Value.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top