문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top