Reduce_Interface.hs revision 3d3889e0cefcdce9b3f43c53aaa201943ac2e895
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{-# LANGUAGE FlexibleInstances #-}
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterModule : $Header$
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterDescription : interface to Reduce CAS
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterCopyright : (c) Dominik Dietrich, DFKI Bremen, 2010
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterLicense : GPLv2 or higher, see LICENSE.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterMaintainer : Dominik.Dietrich@dfki.de
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterStability : provisional
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterPortability : non-portable (uses type-expression in class instances)
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterInterface for Reduce CAS system.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport Common.ProverTools (missingExecutableInPath)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport Common.Utils (getEnvDef)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport Control.Monad (replicateM_)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport Data.Time (midnight)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport Data.Maybe (maybeToList)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport Data.List (intercalate)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport qualified Data.Map as Map
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster{- ----------------------------------------------------------------------
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterConnection handling
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster---------------------------------------------------------------------- -}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster-- | A session is a process connection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterclass Session a where
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster outp :: a -> Handle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster inp :: a -> Handle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err :: a -> Maybe Handle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster err = const Nothing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster proch :: a -> Maybe ProcessHandle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster proch = const Nothing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster-- | The simplest session
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterinstance Session (Handle, Handle) where
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster-- | Better use this session to properly close the connection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterinstance Session (Handle, Handle, ProcessHandle) where
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster inp (x, _, _) = x
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster outp (_, x, _) = x
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster proch (_, _, x) = Just x
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster-- | Left String is success, Right String is failure
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterlookupRedShellCmd :: IO (Either String String)
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterlookupRedShellCmd = do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster reducecmd <- getEnvDef "HETS_REDUCE" "redcsl"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster -- check that prog exists
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster noProg <- missingExecutableInPath reducecmd
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster let f = if noProg then Right else Left
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return $ f reducecmd
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster-- | connects to the CAS, prepares the streams and sets initial options
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterconnectCAS :: String -> IO (Handle, Handle, Handle, ProcessHandle)
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterconnectCAS reducecmd = do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster putStrLn "succeeded"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (inpt, out, errh, pid) <- runInteractiveCommand $ reducecmd ++ " -w"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hSetBuffering out NoBuffering
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hSetBuffering inpt LineBuffering
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hPutStrLn inpt "off nat;"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hPutStrLn inpt "load redlog;"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hPutStrLn inpt "rlset reals;"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster -- read 7 lines
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster replicateM_ 7 $ hGetLine out
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster putStrLn "done"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (inpt, out, errh, pid)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster-- | closes the connection to the CAS
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterdisconnectCAS :: Session a => a -> IO ()
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterdisconnectCAS s = do
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hPutStrLn (inp s) "quit;"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster case proch s of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Nothing -> return ()
-- | declares an operator, such that it can used infix/prefix in CAS