MathematicaInterpreter.hs revision f474203c4cef7d85cb078f15ce5c2cea71e9a030
7db058a7846888b8823fca9e8135f395265ef1d8nilgun{-# LANGUAGE FlexibleContexts, TypeSynonymInstances #-}
fd9abdda70912b99b24e3bf1a38f26fde908a74cndModule : $Header$
fd9abdda70912b99b24e3bf1a38f26fde908a74cndDescription : Mathematica instance for the AssignmentStore class
fd9abdda70912b99b24e3bf1a38f26fde908a74cndCopyright : (c) Ewaryst Schulz, DFKI Bremen 2010
7db058a7846888b8823fca9e8135f395265ef1d8nilgunLicense : GPLv2 or higher, see LICENSE.txt
7db058a7846888b8823fca9e8135f395265ef1d8nilgunMaintainer : Ewaryst.Schulz@dfki.de
7db058a7846888b8823fca9e8135f395265ef1d8nilgunStability : experimental
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletcPortability : non-portable (via imports)
7db058a7846888b8823fca9e8135f395265ef1d8nilgunMathematica as AssignmentStore based on the Mathlink interface
7db058a7846888b8823fca9e8135f395265ef1d8nilgunimport Common.ProverTools (missingExecutableInPath)
7db058a7846888b8823fca9e8135f395265ef1d8nilgunimport Common.Utils (getEnvDef, trimLeft)
7db058a7846888b8823fca9e8135f395265ef1d8nilgunimport CSL.Parse_AS_Basic (parseExpression)
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung-- the process communication interface
7db058a7846888b8823fca9e8135f395265ef1d8nilgunimport qualified Interfaces.Process as PC
7db058a7846888b8823fca9e8135f395265ef1d8nilgun--import Control.Monad.Trans (MonadTrans (..), MonadIO (..))
7db058a7846888b8823fca9e8135f395265ef1d8nilgunimport Control.Monad.Error (ErrorT(..), MonadError (..))
7db058a7846888b8823fca9e8135f395265ef1d8nilgunimport Data.List hiding (lookup)
50cb7e2b30597f481fee57bac945190f06ebcc58jortonimport qualified Data.Set as Set
e10f97d0097170c9843f6cf335dfeef0b44cd83crbowenimport System.Exit (ExitCode)
c9f4eb2763c1d6ba9a3d26828e1729e476d0bb1epctonyimport Prelude hiding (lookup)
7db058a7846888b8823fca9e8135f395265ef1d8nilgun-- ----------------------------------------------------------------------
7db058a7846888b8823fca9e8135f395265ef1d8nilgun-- ----------------------------------------------------------------------
7db058a7846888b8823fca9e8135f395265ef1d8nilgun-- ----------------------------------------------------------------------
7db058a7846888b8823fca9e8135f395265ef1d8nilgun-- * Mathematica Types and Instances
7db058a7846888b8823fca9e8135f395265ef1d8nilgun-- ----------------------------------------------------------------------
50cb7e2b30597f481fee57bac945190f06ebcc58jorton-- | MathematicaInterpreter with Translator based on the Mathlink interface
e10f97d0097170c9843f6cf335dfeef0b44cd83crbowentype MathState = ASState ConnectInfo
7db058a7846888b8823fca9e8135f395265ef1d8nilgunupdateCS :: PC.CommandState -> ConnectInfo -> ConnectInfo
7db058a7846888b8823fca9e8135f395265ef1d8nilgunupdateCS cs (_, dt) = (cs, dt)
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeriupdateDT :: PC.DTime -> ConnectInfo -> ConnectInfo
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggeriupdateDT dt (cs, _) = (cs, dt)
c44eeebd065e2c8cd028016b45c58afb480aaf8fdruggerigetChannelTimeout :: MathState -> PC.DTime
7db058a7846888b8823fca9e8135f395265ef1d8nilgungetChannelTimeout = snd . getConnectInfo
7db058a7846888b8823fca9e8135f395265ef1d8nilgunsetChannelTimeout :: PC.DTime -> MathState -> MathState
7db058a7846888b8823fca9e8135f395265ef1d8nilgunsetChannelTimeout dt = fmap (updateDT dt)
7db058a7846888b8823fca9e8135f395265ef1d8nilgungetMI :: MathState -> PC.CommandState
7db058a7846888b8823fca9e8135f395265ef1d8nilgungetMI = fst . getConnectInfo
4aa603e6448b99f9371397d439795c91a93637eandsetMI :: PC.CommandState -> MathState -> MathState
17ade6df5ec233536985eb1c130a906c725dd614humbedoohsetMI cs = fmap (updateCS cs)
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar-- Mathematica interface, built on CommandState
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coartype MathematicaIO = ErrorT ASError (IOS MathState)
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coarinstance AssignmentStore MathematicaIO where
4aa603e6448b99f9371397d439795c91a93637eand assign = mathematicaAssign (evalMathematicaString True) mathematicaTransS mathematicaTransVarE
17ade6df5ec233536985eb1c130a906c725dd614humbedooh mathematicaAssigns (evalMathematicaString False []) mathematicaTransS mathematicaTransVarE
4aa603e6448b99f9371397d439795c91a93637eand lookup = mathematicaLookup (evalMathematicaString True []) mathematicaTransS
17ade6df5ec233536985eb1c130a906c725dd614humbedooh eval = mathematicaEval (evalMathematicaString True []) mathematicaTransE
17ade6df5ec233536985eb1c130a906c725dd614humbedooh check = mathematicaCheck (evalMathematicaString True []) mathematicaTransE
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar names = get >>= return . SMem . getBMap
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar evalRaw s = get >>= liftIO . flip (mathematicaDirect True) s
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar getUndefinedConstants e = do
4aa603e6448b99f9371397d439795c91a93637eand adg <- gets depGraph
e487d6c09669296f94a5190cc34586a98e624a00nd let g = isNothing . depGraphLookup adg
7db058a7846888b8823fca9e8135f395265ef1d8nilgun return $ Set.filter g $ Set.map SimpleConstant $ setOfUserDefined e
7db058a7846888b8823fca9e8135f395265ef1d8nilgun genNewKey = do
50cb7e2b30597f481fee57bac945190f06ebcc58jorton let (bm, i) = genKey $ getBMap mit
7153ff43420a22c7f6213937b6b210f25d02c464rbowen put mit { getBMap = bm }
7db058a7846888b8823fca9e8135f395265ef1d8nilgun getDepGraph = gets depGraph
7db058a7846888b8823fca9e8135f395265ef1d8nilgun updateConstant n def =
50cb7e2b30597f481fee57bac945190f06ebcc58jorton let f gr = updateGraph gr n
7db058a7846888b8823fca9e8135f395265ef1d8nilgun $ DepGraphAnno { annoDef = def
c9f4eb2763c1d6ba9a3d26828e1729e476d0bb1epctony , annoVal = () }
7db058a7846888b8823fca9e8135f395265ef1d8nilgun mf mit = mit { depGraph = f $ depGraph mit }
c9f4eb2763c1d6ba9a3d26828e1729e476d0bb1epctony in modify mf
50cb7e2b30597f481fee57bac945190f06ebcc58jortoninstance VCGenerator MathematicaIO where
7db058a7846888b8823fca9e8135f395265ef1d8nilgun addVC ea e = do
50cb7e2b30597f481fee57bac945190f06ebcc58jorton $ (text "Verification condition for" <+> pretty ea <> text ":")
7db058a7846888b8823fca9e8135f395265ef1d8nilgun $++$ printExpForVC e
7db058a7846888b8823fca9e8135f395265ef1d8nilgun --s = show $ printExpForVC e <> text ";"
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar -- s = (++ "\n\n;\n\n") $ showRaw $ text "VC for" <+> pretty ea <> text ":" $++$ pretty e
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- vcHdl = stdout
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar vcHdl <- liftM (fromMaybe stdout) $ gets vericondOut
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar liftIO $ hPutStrLn vcHdl s where
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarinstance StepDebugger MathematicaIO where
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar setDebugMode b = modify mf where mf mit = mit { debugMode = b }
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar getDebugMode = gets debugMode
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarinstance SymbolicEvaluator MathematicaIO where
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar setSymbolicMode b = modify mf where mf mit = mit { symbolicMode = b }
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar getSymbolicMode = gets symbolicMode
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarinstance MessagePrinter MathematicaIO where
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar printMessage = liftIO . putStrLn
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- ----------------------------------------------------------------------
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- * Mathematica Transformation Instances
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- ----------------------------------------------------------------------
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- TODO: Review the vargen facility and the cache-stuff in Transformation
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- | Variable generator instance for internal variables on the Hets-side,
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- in contrast to the newvar generation in lookupOrInsert of the BMap, which
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar-- generates variables for the Mathematica-side. We use nevertheless the same counter.
1f1b6bf13313fdd14a45e52e553d3ff28689b717coarinstance VarGen MathematicaIO where
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar genVar = do
7db058a7846888b8823fca9e8135f395265ef1d8nilgun let i = newkey $ getBMap s
d3bd91523e4565551991605fb157fea59c3610e2gryzor put $ s { getBMap = (getBMap s) { newkey = i + 1 } }
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung return $ "?" ++ show i
727872d18412fc021f03969b8641810d8896820bhumbedooh-- ----------------------------------------------------------------------
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh-- * Mathematica syntax functions
0d0ba3a410038e179b695446bb149cce6264e0abnd-- ----------------------------------------------------------------------
727872d18412fc021f03969b8641810d8896820bhumbedoohprintExp :: EXPRESSION -> String
0d0ba3a410038e179b695446bb149cce6264e0abndprintExp e = show $ runReader (printExpression e) mathematicaOpInfoNameMap
0d0ba3a410038e179b695446bb149cce6264e0abnd--printExp = exportExp
0d0ba3a410038e179b695446bb149cce6264e0abnd--printExp = show . pretty
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh-- :: ExpressionPrinter m => EXPRESSION -> m Doc
727872d18412fc021f03969b8641810d8896820bhumbedoohmathematicaOpInfoMap :: OpInfoMap
0d0ba3a410038e179b695446bb149cce6264e0abndmathematicaOpInfoMap = operatorInfoMap
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedoohmathematicaOpInfoNameMap :: OpInfoNameMap
205f749042ed530040a4f0080dbcb47ceae8a374rjungmathematicaOpInfoNameMap = operatorInfoNameMap
0d0ba3a410038e179b695446bb149cce6264e0abndprintAssignment :: String -> [String] -> EXPRESSION -> String
7fec19672a491661b2fe4b29f685bc7f4efa64d4ndprintAssignment n [] e = concat [n, ":= ", printExp e, ":", n, ";"]
7fec19672a491661b2fe4b29f685bc7f4efa64d4ndprintAssignment n l e = concat [ n, ":= proc", args, printExp e
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd , " end proc:", n, args, ";"]
7db058a7846888b8823fca9e8135f395265ef1d8nilgun where args = concat [ "(", intercalate ", " l, ") " ]
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