MathematicaInterpreter.hs revision f474203c4cef7d85cb078f15ce5c2cea71e9a030
26f3a893a0221238d498d03cc19c11c0373f61d6rbb{-# LANGUAGE FlexibleContexts, TypeSynonymInstances #-}
db479b48bd4d75423ed4a45e15b75089d1a8ad72fieldingModule : $Header$
db479b48bd4d75423ed4a45e15b75089d1a8ad72fieldingDescription : Mathematica instance for the AssignmentStore class
db479b48bd4d75423ed4a45e15b75089d1a8ad72fieldingCopyright : (c) Ewaryst Schulz, DFKI Bremen 2010
db479b48bd4d75423ed4a45e15b75089d1a8ad72fieldingLicense : GPLv2 or higher, see LICENSE.txt
db479b48bd4d75423ed4a45e15b75089d1a8ad72fieldingMaintainer : Ewaryst.Schulz@dfki.de
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcndStability : experimental
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcndPortability : non-portable (via imports)
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcndMathematica as AssignmentStore based on the Mathlink interface
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcndimport Common.ProverTools (missingExecutableInPath)
26f3a893a0221238d498d03cc19c11c0373f61d6rbbimport Common.Utils (getEnvDef, trimLeft)
d8d240df2f2b23455be6b01343daedebaa6c4f96trawickimport CSL.Parse_AS_Basic (parseExpression)
26f3a893a0221238d498d03cc19c11c0373f61d6rbb-- the process communication interface
26f3a893a0221238d498d03cc19c11c0373f61d6rbbimport qualified Interfaces.Process as PC
26f3a893a0221238d498d03cc19c11c0373f61d6rbb--import Control.Monad.Trans (MonadTrans (..), MonadIO (..))
26f3a893a0221238d498d03cc19c11c0373f61d6rbbimport Control.Monad.Error (ErrorT(..), MonadError (..))
d472f67198d6b15dd1270136f180cca9c9263243trawickimport Data.List hiding (lookup)
26f3a893a0221238d498d03cc19c11c0373f61d6rbbimport qualified Data.Set as Set
26f3a893a0221238d498d03cc19c11c0373f61d6rbbimport System.Exit (ExitCode)
87f0329e30de94828e08d53a99ea23cda86a9fccjerenkrantzimport Prelude hiding (lookup)
0967fa2b2d694e2ac6d9c4001eb4aa71a1046b15trawick-- ----------------------------------------------------------------------
d472f67198d6b15dd1270136f180cca9c9263243trawick-- ----------------------------------------------------------------------
26f3a893a0221238d498d03cc19c11c0373f61d6rbb-- ----------------------------------------------------------------------
26f3a893a0221238d498d03cc19c11c0373f61d6rbb-- * Mathematica Types and Instances
26f3a893a0221238d498d03cc19c11c0373f61d6rbb-- ----------------------------------------------------------------------
67d00e5bf4687af8e8ce82addb5befe94d2e9f6faaron-- | MathematicaInterpreter with Translator based on the Mathlink interface
67d00e5bf4687af8e8ce82addb5befe94d2e9f6faarontype MathState = ASState ConnectInfo
75293e34b98e5497b69f1a3c9f057b88d2d5eaabbrianpupdateCS :: PC.CommandState -> ConnectInfo -> ConnectInfo
26f3a893a0221238d498d03cc19c11c0373f61d6rbbupdateCS cs (_, dt) = (cs, dt)
26f3a893a0221238d498d03cc19c11c0373f61d6rbbupdateDT :: PC.DTime -> ConnectInfo -> ConnectInfo
67d00e5bf4687af8e8ce82addb5befe94d2e9f6faaronupdateDT dt (cs, _) = (cs, dt)
ba2bab42e97405dc41c0f8fe3416f7f9a79ed7a9brianpgetChannelTimeout :: MathState -> PC.DTime
ba2bab42e97405dc41c0f8fe3416f7f9a79ed7a9brianpgetChannelTimeout = snd . getConnectInfo
26f3a893a0221238d498d03cc19c11c0373f61d6rbbsetChannelTimeout :: PC.DTime -> MathState -> MathState
26f3a893a0221238d498d03cc19c11c0373f61d6rbbsetChannelTimeout dt = fmap (updateDT dt)
26f3a893a0221238d498d03cc19c11c0373f61d6rbbgetMI :: MathState -> PC.CommandState
26f3a893a0221238d498d03cc19c11c0373f61d6rbbgetMI = fst . getConnectInfo
8afc9ec2b5229abf02ddb7abf0b6923109b46119rbowensetMI :: PC.CommandState -> MathState -> MathState
d8d240df2f2b23455be6b01343daedebaa6c4f96trawicksetMI cs = fmap (updateCS cs)
26f3a893a0221238d498d03cc19c11c0373f61d6rbb-- Mathematica interface, built on CommandState
d8d240df2f2b23455be6b01343daedebaa6c4f96trawicktype MathematicaIO = ErrorT ASError (IOS MathState)
945a9b081610f2b57759231e4cfad7aed62c9326sliveinstance AssignmentStore MathematicaIO where
945a9b081610f2b57759231e4cfad7aed62c9326slive assign = mathematicaAssign (evalMathematicaString True) mathematicaTransS mathematicaTransVarE
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick mathematicaAssigns (evalMathematicaString False []) mathematicaTransS mathematicaTransVarE
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick lookup = mathematicaLookup (evalMathematicaString True []) mathematicaTransS
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick eval = mathematicaEval (evalMathematicaString True []) mathematicaTransE
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick check = mathematicaCheck (evalMathematicaString True []) mathematicaTransE
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick names = get >>= return . SMem . getBMap
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick evalRaw s = get >>= liftIO . flip (mathematicaDirect True) s
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick getUndefinedConstants e = do
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick adg <- gets depGraph
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick let g = isNothing . depGraphLookup adg
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick return $ Set.filter g $ Set.map SimpleConstant $ setOfUserDefined e
292cb7b720095e7bb434d79ae53b02d332aeb99acovener genNewKey = do
d8d240df2f2b23455be6b01343daedebaa6c4f96trawick let (bm, i) = genKey $ getBMap mit
26f3a893a0221238d498d03cc19c11c0373f61d6rbb put mit { getBMap = bm }
wrapCommand :: IOS PC.CommandState a -> IOS MathState a
(res, _) <- runIOS (getMI mit) $ PC.call (getChannelTimeout mit) s
res <- lift $ wrapCommand $ PC.call (getChannelTimeout mit) s
-- when b $ liftIO $ putStrLn $ show $ maybeToList $ parseExpression mathematicaOpInfoMap $ trimLeft
-- when b $ liftIO $ putStrLn $ show $ map trans $ maybeToList $ parseExpression mathematicaOpInfoMap $ trimLeft
-> PC.DTime -- ^ timeout for response
cs <- PC.start (mathematicacmd ++ " -q") v
(_, cs') <- runIOS cs $ PC.call 0.5
fmap fst $ runIOS (getMI rit) (PC.call 0.5 $ "with(" ++ s ++ ");")
(ec, _) <- runIOS (getMI mit) $ PC.close $ Just "quit;"
-> PC.DTime -- ^ timeout for response
runIOS (getMI mit) $ PC.call 0.3 debugFun