{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Haskell code for interacting with a Futhark server program.  This
-- module presents a low-level interface.  See
-- <https://futhark.readthedocs.io/en/latest/server-protocol.html the
-- documentation of the server protocol> for the meaning of the
-- commands.  See also "Futhark.Server.Values" for higher-level
-- functions for loading data into a server.
--
-- Error messages produced by the server will be returned as a
-- 'CmdFailure'.  However, certain errors (such as if the server
-- process terminates unexpectedly, or temporary files cannot be
-- created) will result in an IO exception.
--
-- Many of the functions here are documented only as the server
-- protocol command they correspond to.  See the protocol
-- documentation for details.
module Futhark.Server
  ( -- * Server creation
    Server,
    ServerCfg (..),
    newServerCfg,
    withServer,

    -- * Commands
    Cmd,
    CmdFailure (..),
    VarName,
    TypeName,
    EntryName,
    InputType (..),
    OutputType (..),

    -- ** Main commands
    cmdRestore,
    cmdStore,
    cmdCall,
    cmdFree,
    cmdRename,
    cmdInputs,
    cmdOutputs,
    cmdClear,

    -- ** Interrogation
    cmdTypes,
    cmdEntryPoints,

    -- ** Records
    cmdNew,
    cmdProject,
    cmdFields,

    -- ** Auxiliary
    cmdReport,
    cmdPauseProfiling,
    cmdUnpauseProfiling,
    cmdSetTuningParam,
    cmdTuningParams,
    cmdTuningParamClass,

    -- * Utility
    cmdMaybe,
    cmdEither,

    -- * Raw
    startServer,
    stopServer,
    sendCommand,
  )
where

import Control.Exception
import Control.Monad
import Control.Monad.Except
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import System.Directory (removeFile)
import System.Exit
import System.IO hiding (stdin, stdout)
import System.IO.Temp (getCanonicalTemporaryDirectory)
import qualified System.Process as P

-- | The name of a command.
type Cmd = Text

-- | A handle to a running server.
data Server = Server
  { Server -> Handle
serverStdin :: Handle,
    Server -> Handle
serverStdout :: Handle,
    Server -> [Char]
serverErrLog :: FilePath,
    Server -> ProcessHandle
serverProc :: P.ProcessHandle,
    Server -> Text -> Text -> IO ()
serverOnLine :: Cmd -> Text -> IO (),
    Server -> Bool
serverDebug :: Bool
  }

-- | Configuration of the server.  Use 'newServerCfg' to conveniently
-- create a sensible default configuration.
data ServerCfg = ServerCfg
  { -- | Path to the server executable.
    ServerCfg -> [Char]
cfgProg :: FilePath,
    -- | Command line options to pass to the
    -- server executable.
    ServerCfg -> [[Char]]
cfgProgOpts :: [String],
    -- | If true, print a running log of server communication to stderr.
    ServerCfg -> Bool
cfgDebug :: Bool,
    -- | A function that is invoked on every line of input sent by the
    -- server, except the @%%% OK@ and @%%% FAILURE@ prompts.  This
    -- can be used to e.g. print or gather logging messages as they
    -- arrive, instead of waiting for the command to finish.  The name
    -- of the command leading to the message is also provided.  The
    -- default function does nothing.
    ServerCfg -> Text -> Text -> IO ()
cfgOnLine :: Cmd -> Text -> IO ()
  }

-- | Create a server config with the given 'cfgProg' and 'cfgProgOpts'.
newServerCfg :: FilePath -> [String] -> ServerCfg
newServerCfg :: [Char] -> [[Char]] -> ServerCfg
newServerCfg [Char]
prog [[Char]]
opts =
  ServerCfg
    { cfgProg :: [Char]
cfgProg = [Char]
prog,
      cfgProgOpts :: [[Char]]
cfgProgOpts = [[Char]]
opts,
      cfgDebug :: Bool
cfgDebug = Bool
False,
      cfgOnLine :: Text -> Text -> IO ()
cfgOnLine = \Text
_ Text
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    }

-- | Start up a server.  Make sure that 'stopServer' is eventually
-- called on the server.  If this does not happen, then temporary
-- files may be left on the file system.  You almost certainly wish to
-- use 'bracket' or similar to avoid this.  Calls 'error' if startup
-- fails.
startServer :: ServerCfg -> IO Server
startServer :: ServerCfg -> IO Server
startServer (ServerCfg [Char]
prog [[Char]]
options Bool
debug Text -> Text -> IO ()
on_line_f) = do
  [Char]
tmpdir <- IO [Char]
getCanonicalTemporaryDirectory
  ([Char]
err_log_f, Handle
err_log_h) <- [Char] -> [Char] -> IO ([Char], Handle)
openTempFile [Char]
tmpdir [Char]
"futhark-server-stderr.log"
  (Just Handle
stdin, Just Handle
stdout, Maybe Handle
Nothing, ProcessHandle
phandle) <-
    CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
P.createProcess
      ( ([Char] -> [[Char]] -> CreateProcess
P.proc [Char]
prog [[Char]]
options)
          { std_err :: StdStream
P.std_err = Handle -> StdStream
P.UseHandle Handle
err_log_h,
            std_in :: StdStream
P.std_in = StdStream
P.CreatePipe,
            std_out :: StdStream
P.std_out = StdStream
P.CreatePipe
          }
      )

  Maybe ExitCode
code <- ProcessHandle -> IO (Maybe ExitCode)
P.getProcessExitCode ProcessHandle
phandle
  case Maybe ExitCode
code of
    Just (ExitFailure Int
e) ->
      forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Cannot start " forall a. [a] -> [a] -> [a]
++ [Char]
prog forall a. [a] -> [a] -> [a]
++ [Char]
": error " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
e
    Maybe ExitCode
_ -> do
      let server :: Server
server =
            Server
              { serverStdin :: Handle
serverStdin = Handle
stdin,
                serverStdout :: Handle
serverStdout = Handle
stdout,
                serverProc :: ProcessHandle
serverProc = ProcessHandle
phandle,
                serverDebug :: Bool
serverDebug = Bool
debug,
                serverErrLog :: [Char]
serverErrLog = [Char]
err_log_f,
                serverOnLine :: Text -> Text -> IO ()
serverOnLine = Text -> Text -> IO ()
on_line_f
              }
      forall (f :: * -> *) a. Functor f => f a -> f ()
void (Text -> Server -> IO [Text]
responseLines Text
"startup" Server
server) forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` forall a. Server -> IOError -> IO a
onStartupError Server
server
      forall (f :: * -> *) a. Applicative f => a -> f a
pure Server
server
  where
    onStartupError :: Server -> IOError -> IO a
    onStartupError :: forall a. Server -> IOError -> IO a
onStartupError Server
s IOError
_ = do
      ExitCode
code <- ProcessHandle -> IO ExitCode
P.waitForProcess forall a b. (a -> b) -> a -> b
$ Server -> ProcessHandle
serverProc Server
s
      [Char]
stderr_s <- [Char] -> IO [Char]
readFile forall a b. (a -> b) -> a -> b
$ Server -> [Char]
serverErrLog Server
s
      [Char] -> IO ()
removeFile forall a b. (a -> b) -> a -> b
$ Server -> [Char]
serverErrLog Server
s
      forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
        [Char]
"Command failed with "
          forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show ExitCode
code
          forall a. [a] -> [a] -> [a]
++ [Char]
":\n"
          forall a. [a] -> [a] -> [a]
++ [[Char]] -> [Char]
unwords ([Char]
prog forall a. a -> [a] -> [a]
: [[Char]]
options)
          forall a. [a] -> [a] -> [a]
++ [Char]
"\nStderr:\n"
          forall a. [a] -> [a] -> [a]
++ [Char]
stderr_s

-- | Shut down a server.  It may not be used again.  Calls 'error' if
-- the server process terminates with a failing exit code
-- (i.e. anything but 'ExitSuccess').
stopServer :: Server -> IO ()
stopServer :: Server -> IO ()
stopServer Server
s = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. IO a -> IO b -> IO a
finally ([Char] -> IO ()
removeFile (Server -> [Char]
serverErrLog Server
s)) forall a b. (a -> b) -> a -> b
$ do
  Handle -> IO ()
hClose forall a b. (a -> b) -> a -> b
$ Server -> Handle
serverStdin Server
s
  ExitCode
code <- ProcessHandle -> IO ExitCode
P.waitForProcess forall a b. (a -> b) -> a -> b
$ Server -> ProcessHandle
serverProc Server
s
  case ExitCode
code of
    ExitCode
ExitSuccess -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    ExitFailure Int
_ -> do
      [Char]
stderr_s <- [Char] -> IO [Char]
readFile forall a b. (a -> b) -> a -> b
$ Server -> [Char]
serverErrLog Server
s
      forall a. HasCallStack => [Char] -> a
error [Char]
stderr_s

-- | Start a server, execute an action, then shut down the server.
-- The 'Server' may not be returned from the action.
withServer :: ServerCfg -> (Server -> IO a) -> IO a
withServer :: forall a. ServerCfg -> (Server -> IO a) -> IO a
withServer ServerCfg
cfg Server -> IO a
m = forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
  Server
server <- ServerCfg -> IO Server
startServer ServerCfg
cfg
  a
x <- forall a. IO a -> IO a
restore (Server -> IO a
m Server
server) forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` forall {b}. Server -> SomeException -> IO b
mException Server
server
  Server -> IO ()
stopServer Server
server
  forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
  where
    mException :: Server -> SomeException -> IO b
mException Server
server SomeException
e = do
      -- Anything that goes wrong here is probably less interesting
      -- than the original exception.
      Server -> IO ()
stopServer Server
server forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` forall a. SomeException -> SomeException -> IO a
stopServerException SomeException
e
      forall a e. Exception e => e -> a
throw SomeException
e
    stopServerException :: SomeException -> SomeException -> IO a
    stopServerException :: forall a. SomeException -> SomeException -> IO a
stopServerException SomeException
e SomeException
_ = forall a e. Exception e => e -> a
throw SomeException
e

-- Read lines of response until the next %%% OK (which is what
-- indicates that the server is ready for new instructions).
responseLines :: Cmd -> Server -> IO [Text]
responseLines :: Text -> Server -> IO [Text]
responseLines Text
cmd Server
s = do
  Text
l <- Handle -> IO Text
T.hGetLine forall a b. (a -> b) -> a -> b
$ Server -> Handle
serverStdout Server
s
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Server -> Bool
serverDebug Server
s) forall a b. (a -> b) -> a -> b
$
    Handle -> Text -> IO ()
T.hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$
      Text
"<<< " forall a. Semigroup a => a -> a -> a
<> Text
l
  case Text
l of
    Text
"%%% OK" -> forall (f :: * -> *) a. Applicative f => a -> f a
pure []
    Text
_ -> do
      Server -> Text -> Text -> IO ()
serverOnLine Server
s Text
cmd Text
l
      (Text
l forall a. a -> [a] -> [a]
:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Server -> IO [Text]
responseLines Text
cmd Server
s

-- | The command failed, and this is why.  The first 'Text' is any
-- output before the failure indicator, and the second Text is the
-- output after the indicator.
data CmdFailure = CmdFailure {CmdFailure -> [Text]
failureLog :: [Text], CmdFailure -> [Text]
failureMsg :: [Text]}
  deriving (CmdFailure -> CmdFailure -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CmdFailure -> CmdFailure -> Bool
$c/= :: CmdFailure -> CmdFailure -> Bool
== :: CmdFailure -> CmdFailure -> Bool
$c== :: CmdFailure -> CmdFailure -> Bool
Eq, Eq CmdFailure
CmdFailure -> CmdFailure -> Bool
CmdFailure -> CmdFailure -> Ordering
CmdFailure -> CmdFailure -> CmdFailure
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CmdFailure -> CmdFailure -> CmdFailure
$cmin :: CmdFailure -> CmdFailure -> CmdFailure
max :: CmdFailure -> CmdFailure -> CmdFailure
$cmax :: CmdFailure -> CmdFailure -> CmdFailure
>= :: CmdFailure -> CmdFailure -> Bool
$c>= :: CmdFailure -> CmdFailure -> Bool
> :: CmdFailure -> CmdFailure -> Bool
$c> :: CmdFailure -> CmdFailure -> Bool
<= :: CmdFailure -> CmdFailure -> Bool
$c<= :: CmdFailure -> CmdFailure -> Bool
< :: CmdFailure -> CmdFailure -> Bool
$c< :: CmdFailure -> CmdFailure -> Bool
compare :: CmdFailure -> CmdFailure -> Ordering
$ccompare :: CmdFailure -> CmdFailure -> Ordering
Ord, Int -> CmdFailure -> ShowS
[CmdFailure] -> ShowS
CmdFailure -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [CmdFailure] -> ShowS
$cshowList :: [CmdFailure] -> ShowS
show :: CmdFailure -> [Char]
$cshow :: CmdFailure -> [Char]
showsPrec :: Int -> CmdFailure -> ShowS
$cshowsPrec :: Int -> CmdFailure -> ShowS
Show)

-- Figure out whether the response is a failure, and if so, return the
-- failure message.
checkForFailure :: [Text] -> Either CmdFailure [Text]
checkForFailure :: [Text] -> Either CmdFailure [Text]
checkForFailure [] = forall a b. b -> Either a b
Right []
checkForFailure (Text
"%%% FAILURE" : [Text]
ls) = forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ [Text] -> [Text] -> CmdFailure
CmdFailure forall a. Monoid a => a
mempty [Text]
ls
checkForFailure (Text
l : [Text]
ls) =
  case [Text] -> Either CmdFailure [Text]
checkForFailure [Text]
ls of
    Left (CmdFailure [Text]
xs [Text]
ys) -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ [Text] -> [Text] -> CmdFailure
CmdFailure (Text
l forall a. a -> [a] -> [a]
: [Text]
xs) [Text]
ys
    Right [Text]
ls' -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Text
l forall a. a -> [a] -> [a]
: [Text]
ls'

-- Words with spaces in them must be quoted.
quoteWord :: Text -> Text
quoteWord :: Text -> Text
quoteWord Text
t
  | Just Char
_ <- (Char -> Bool) -> Text -> Maybe Char
T.find (forall a. Eq a => a -> a -> Bool
== Char
' ') Text
t =
      Text
"\"" forall a. Semigroup a => a -> a -> a
<> Text
t forall a. Semigroup a => a -> a -> a
<> Text
"\""
  | Bool
otherwise = Text
t

-- | Send an arbitrary command to the server.  This is only useful
-- when the server protocol has been extended without this module
-- having been similarly extended.  Be careful not to send invalid
-- commands.
sendCommand :: Server -> Cmd -> [Text] -> IO (Either CmdFailure [Text])
sendCommand :: Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
cmd [Text]
args = do
  let cmd_and_args' :: Text
cmd_and_args' = [Text] -> Text
T.unwords forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
quoteWord forall a b. (a -> b) -> a -> b
$ Text
cmd forall a. a -> [a] -> [a]
: [Text]
args

  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Server -> Bool
serverDebug Server
s) forall a b. (a -> b) -> a -> b
$
    Handle -> Text -> IO ()
T.hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$
      Text
">>> " forall a. Semigroup a => a -> a -> a
<> Text
cmd_and_args'

  Handle -> Text -> IO ()
T.hPutStrLn (Server -> Handle
serverStdin Server
s) Text
cmd_and_args'
  Handle -> IO ()
hFlush forall a b. (a -> b) -> a -> b
$ Server -> Handle
serverStdin Server
s
  [Text] -> Either CmdFailure [Text]
checkForFailure forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Server -> IO [Text]
responseLines Text
cmd Server
s forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` forall a. IOError -> IO a
onError
  where
    onError :: IOError -> IO a
    onError :: forall a. IOError -> IO a
onError IOError
e = do
      Maybe ExitCode
code <- ProcessHandle -> IO (Maybe ExitCode)
P.getProcessExitCode forall a b. (a -> b) -> a -> b
$ Server -> ProcessHandle
serverProc Server
s
      let code_msg :: [Char]
code_msg =
            case Maybe ExitCode
code of
              Just (ExitFailure Int
x) ->
                [Char]
"\nServer process exited unexpectedly with exit code: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
x
              Maybe ExitCode
_ -> forall a. Monoid a => a
mempty
      [Char]
stderr_s <- [Char] -> IO [Char]
readFile forall a b. (a -> b) -> a -> b
$ Server -> [Char]
serverErrLog Server
s
      forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$
        [Char]
"After sending command "
          forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Text
cmd
          forall a. [a] -> [a] -> [a]
++ [Char]
" to server process:"
          forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show IOError
e
          forall a. [a] -> [a] -> [a]
++ [Char]
code_msg
          forall a. [a] -> [a] -> [a]
++ [Char]
"\nServer stderr:\n"
          forall a. [a] -> [a] -> [a]
++ [Char]
stderr_s

-- | The name of a server-side variable.
type VarName = Text

-- | The name of a server-side type.
type TypeName = Text

-- | The name of an entry point.
type EntryName = Text

-- | The type of an input of an entry point.  If 'inputConsumed', then
-- the value passed in a 'cmdCall' must not be used again (nor any of
-- its aliases).
data InputType = InputType
  { InputType -> Bool
inputConsumed :: Bool,
    InputType -> Text
inputType :: TypeName
  }

-- | The type of an output of an entry point.  If 'outputUnique', then
-- the value returned does not alias any of the inputs.  See the
-- Futhark language manual itself for more details - the implications
-- are quite subtle (but you can ignore them unless you manually use
-- type annotations to make some entry point parameters unique).
data OutputType = OutputType
  { OutputType -> Bool
outputUnique :: Bool,
    OutputType -> Text
outputType :: TypeName
  }

inOutType :: (Bool -> TypeName -> a) -> Text -> a
inOutType :: forall a. (Bool -> Text -> a) -> Text -> a
inOutType Bool -> Text -> a
f Text
t =
  case Text -> Maybe (Char, Text)
T.uncons Text
t of
    Just (Char
'*', Text
t') -> Bool -> Text -> a
f Bool
True Text
t'
    Maybe (Char, Text)
_ -> Bool -> Text -> a
f Bool
False Text
t

helpCmd :: Server -> Cmd -> [Text] -> IO (Maybe CmdFailure)
helpCmd :: Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
cmd [Text]
args =
  forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> Maybe a
Just (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
cmd [Text]
args

-- | @restore filename var0 type0 var1 type1...@.
cmdRestore :: Server -> FilePath -> [(VarName, TypeName)] -> IO (Maybe CmdFailure)
cmdRestore :: Server -> [Char] -> [(Text, Text)] -> IO (Maybe CmdFailure)
cmdRestore Server
s [Char]
fname [(Text, Text)]
vars = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"restore" forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack [Char]
fname forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall {a}. (a, a) -> [a]
f [(Text, Text)]
vars
  where
    f :: (a, a) -> [a]
f (a
v, a
t) = [a
v, a
t]

-- | @store filename vars...@.
cmdStore :: Server -> FilePath -> [VarName] -> IO (Maybe CmdFailure)
cmdStore :: Server -> [Char] -> [Text] -> IO (Maybe CmdFailure)
cmdStore Server
s [Char]
fname [Text]
vars = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"store" forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack [Char]
fname forall a. a -> [a] -> [a]
: [Text]
vars

-- | @call entrypoint outs... ins...@.
cmdCall :: Server -> EntryName -> [VarName] -> [VarName] -> IO (Either CmdFailure [Text])
cmdCall :: Server -> Text -> [Text] -> [Text] -> IO (Either CmdFailure [Text])
cmdCall Server
s Text
entry [Text]
outs [Text]
ins =
  Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"call" forall a b. (a -> b) -> a -> b
$ Text
entry forall a. a -> [a] -> [a]
: [Text]
outs forall a. [a] -> [a] -> [a]
++ [Text]
ins

-- | @free vars...@.
cmdFree :: Server -> [VarName] -> IO (Maybe CmdFailure)
cmdFree :: Server -> [Text] -> IO (Maybe CmdFailure)
cmdFree Server
s = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"free"

-- | @rename oldname newname@.
cmdRename :: Server -> VarName -> VarName -> IO (Maybe CmdFailure)
cmdRename :: Server -> Text -> Text -> IO (Maybe CmdFailure)
cmdRename Server
s Text
oldname Text
newname = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"rename" [Text
oldname, Text
newname]

-- | @inputs entryname@, with uniqueness represented as True.
cmdInputs :: Server -> EntryName -> IO (Either CmdFailure [InputType])
cmdInputs :: Server -> Text -> IO (Either CmdFailure [InputType])
cmdInputs Server
s Text
entry =
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. (a -> b) -> [a] -> [b]
map (forall a. (Bool -> Text -> a) -> Text -> a
inOutType Bool -> Text -> InputType
InputType)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"inputs" [Text
entry]

-- | @outputs entryname@, with uniqueness represented as True.
cmdOutputs :: Server -> EntryName -> IO (Either CmdFailure [OutputType])
cmdOutputs :: Server -> Text -> IO (Either CmdFailure [OutputType])
cmdOutputs Server
s Text
entry =
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. (a -> b) -> [a] -> [b]
map (forall a. (Bool -> Text -> a) -> Text -> a
inOutType Bool -> Text -> OutputType
OutputType)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"outputs" [Text
entry]

-- | @clear@
cmdClear :: Server -> IO (Maybe CmdFailure)
cmdClear :: Server -> IO (Maybe CmdFailure)
cmdClear Server
s = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"clear" []

-- | @report@
cmdReport :: Server -> IO (Either CmdFailure [Text])
cmdReport :: Server -> IO (Either CmdFailure [Text])
cmdReport Server
s = Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"report" []

-- | @pause_profiling@
cmdPauseProfiling :: Server -> IO (Maybe CmdFailure)
cmdPauseProfiling :: Server -> IO (Maybe CmdFailure)
cmdPauseProfiling Server
s = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"pause_profiling" []

-- | @unpause_profiling@
cmdUnpauseProfiling :: Server -> IO (Maybe CmdFailure)
cmdUnpauseProfiling :: Server -> IO (Maybe CmdFailure)
cmdUnpauseProfiling Server
s = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"unpause_profiling" []

-- | @set_tuning_param param value@
cmdSetTuningParam :: Server -> Text -> Text -> IO (Either CmdFailure [Text])
cmdSetTuningParam :: Server -> Text -> Text -> IO (Either CmdFailure [Text])
cmdSetTuningParam Server
s Text
param Text
value = Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"set_tuning_param" [Text
param, Text
value]

-- | @tuning_params@
cmdTuningParams :: Server -> Text -> IO (Either CmdFailure [Text])
cmdTuningParams :: Server -> Text -> IO (Either CmdFailure [Text])
cmdTuningParams Server
s Text
entry = Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"tuning_params" [Text
entry]

-- | @tuning_param_class param@
cmdTuningParamClass :: Server -> Text -> IO (Either CmdFailure Text)
cmdTuningParamClass :: Server -> Text -> IO (Either CmdFailure Text)
cmdTuningParamClass Server
s Text
param = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [a] -> a
head forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"tuning_param_class" [Text
param]

-- | @types@
cmdTypes :: Server -> IO (Either CmdFailure [Text])
cmdTypes :: Server -> IO (Either CmdFailure [Text])
cmdTypes Server
s = Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"types" []

-- | @entry_points@
cmdEntryPoints :: Server -> IO (Either CmdFailure [Text])
cmdEntryPoints :: Server -> IO (Either CmdFailure [Text])
cmdEntryPoints Server
s = Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"entry_points" []

-- | @fields type@
cmdFields :: Server -> Text -> IO (Either CmdFailure [Text])
cmdFields :: Server -> Text -> IO (Either CmdFailure [Text])
cmdFields Server
s Text
t = Server -> Text -> [Text] -> IO (Either CmdFailure [Text])
sendCommand Server
s Text
"fields" [Text
t]

-- | @new var0 type var1...@
cmdNew :: Server -> Text -> Text -> [Text] -> IO (Maybe CmdFailure)
cmdNew :: Server -> Text -> Text -> [Text] -> IO (Maybe CmdFailure)
cmdNew Server
s Text
var0 Text
t [Text]
vars = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"new" forall a b. (a -> b) -> a -> b
$ Text
var0 forall a. a -> [a] -> [a]
: Text
t forall a. a -> [a] -> [a]
: [Text]
vars

-- | @project to from field@
cmdProject :: Server -> Text -> Text -> Text -> IO (Maybe CmdFailure)
cmdProject :: Server -> Text -> Text -> Text -> IO (Maybe CmdFailure)
cmdProject Server
s Text
to Text
from Text
field = Server -> Text -> [Text] -> IO (Maybe CmdFailure)
helpCmd Server
s Text
"project" [Text
to, Text
from, Text
field]

-- | Turn a 'Maybe'-producing command into a monadic action.
cmdMaybe :: (MonadError Text m, MonadIO m) => IO (Maybe CmdFailure) -> m ()
cmdMaybe :: forall (m :: * -> *).
(MonadError Text m, MonadIO m) =>
IO (Maybe CmdFailure) -> m ()
cmdMaybe = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
T.unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. CmdFailure -> [Text]
failureMsg) forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

-- | Turn an 'Either'-producing command into a monadic action.
cmdEither :: (MonadError Text m, MonadIO m) => IO (Either CmdFailure a) -> m a
cmdEither :: forall (m :: * -> *) a.
(MonadError Text m, MonadIO m) =>
IO (Either CmdFailure a) -> m a
cmdEither = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
T.unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. CmdFailure -> [Text]
failureMsg) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO