z1naOK9nu8iY5A
z1naOK9nu8iY5A

Reputation: 893

Type checking and the Haskell hint interpreter library

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.

Upvotes: 0

Views: 196

Answers (1)

robx
robx

Reputation: 2329

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.

Upvotes: 1

Related Questions