1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | -- my Interpreter file module HReal.Server.Interpreter where import HReal.Core.World import HReal.Core.History import Language.Haskell.Interpreter as I type Modification = History -> Either Constraint World interpret :: String -> IO (Either InterpreterError Modification) interpret source = runInterpreter $ do setImports [ "Prelude" , "Data.Int" , "Data.Map" , "Data.Set" , "HReal.Core.Prelude" ] I.interpret source (as :: Modification) -- HReal.Core.Prelude file module HReal.Core.Prelude ( module HReal.Core.World , module HReal.Core.History , module HReal.Core.Vertex ) where import HReal.Core.World import HReal.Core.History import HReal.Core.Vertex |
1 2 | *Main> do; Left c <- I.interpret "undefined"; print c WontCompile [GhcError {errMsg = "Not in scope: type constructor or class `HReal.Core.World.World'"},GhcError {errMsg = "Not in scope:\n type constructor or class `HReal.Core.World.Constraint'"},GhcError {errMsg = "Not in scope: type constructor or class `HReal.Core.World.World'"}] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -- A.hs module A (module B, b) where import B b :: B b = 0 -- B.hs module B where type B = Int -- Main.hs module Main where import A import Language.Haskell.Interpreter main :: IO () main = (print =<<) . runInterpreter $ do loadModules [ "A" ] -- this is important for success setImports [ "Prelude", "A" ] interpret "b" (as :: B) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | module Main where import Language.Haskell.Interpreter import Language.Haskell.Interpreter.Unsafe import Data.Typeable as T import Data.Typeable.Internal import GHC.Fingerprint.Type main = failure >> success test t = (print =<<) . runInterpreter $ do setImports [ "Prelude" ] interpret "1/5" t failure = test (as :: Rational) success = test (as :: Q) newtype Q = Q Rational instance Typeable Q where typeOf _ = TypeRep (Fingerprint 0 0) (T.mkTyCon "Rational") [] instance Show Q where show (Q a) = show a showsPrec n (Q a) = showsPrec n a {- Left (WontCompile [GhcError {errMsg = "Not in scope: type constructor or class `Ratio'"}]) Right (1 % 5) -} |