Runtime Haskell interpreter
#3Can't interpret values of types that, in the interpreter, are in scope but only qualified
I wrote following code using qualified imports (setImportsQ) but it seems not work. Am I doing something wrong or is it a bug in the library?
Code:
module Main where
import Data.Map (Map)
import qualified Language.Haskell.Interpreter as I
run :: I.Interpreter ()
run = do
I.reset
I.setImportsQ [("Prelude" , Nothing)
,("Data.Map" , Just "Map")]
_ <- I.interpret "Map.empty" (I.as :: Map Int Int)
return ()
main :: IO ()
main = I.runInterpreter run >>= print
Output:
Left (WontCompile [GhcError {errMsg = "Not in scope: type constructor or class `Map'\nPerhaps you meant `Map.Map' (imported from Data.Map)"}])
- description updated
- description updated
Interesting. The problem is that to ensure that the expression to interpret has the right type, we add a type annotation using the
Typeable
instance of the target type.In this case, the actual string becomes something like
"(Map.empty) :: Map Int Int"
and the problem is that"Map Int Int"
is not in scope in the interpreter.Since
base-4.4.0.0
, we can also get the defining module for each type using theTypeable
interface. That means that in principle we could go fromMap Int Int
to, say,"Data.Map.Base.Map GHC.Types.Int GHC.Types.Int"
, and then we would need to enforce behind the scenes that those modules are imported qualified as well every time.- summary changed to "Can't interpret values of types that, in the interpreter, are in scope but only qualified"
- added tag bug
I think I resolve this issue by this patch: http://hub.darcs.net/konn/hint/patch/475c3c169050c7c1594389afada18e4395903ced . I confirmed that this passes unit-tests. Can you merge this change?
hint is now on GitHub, feel free to open this issue over there or open pull requests with patches: