IHaskell/main/Main.hs

452 lines
17 KiB
Haskell
Raw Normal View History

2015-05-25 21:25:34 +02:00
{-# LANGUAGE CPP, ScopedTypeVariables, QuasiQuotes #-}
2015-03-19 18:30:22 -07:00
-- | Description : Argument parsing and basic messaging loop, using Haskell
-- Chans to communicate with the ZeroMQ sockets.
2015-03-19 18:30:22 -07:00
module Main (main) where
2014-01-25 17:31:55 -08:00
import IHaskellPrelude
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Char8 as CBS
2014-01-25 17:31:55 -08:00
-- Standard library imports.
import Control.Concurrent (threadDelay)
import Control.Concurrent.Chan
import Data.Aeson
import System.Directory
import System.Exit (exitSuccess)
import System.Environment (getArgs)
import System.Posix.Signals
import qualified Data.Map as Map
import Data.String.Here (hereFile)
import qualified Data.Text.Encoding as E
2014-01-25 17:31:55 -08:00
-- IHaskell imports.
import IHaskell.Convert (convert)
import IHaskell.Eval.Completion (complete)
import IHaskell.Eval.Inspect (inspect)
import IHaskell.Eval.Evaluate
import IHaskell.Display
import IHaskell.Eval.Info
import IHaskell.Flags
import IHaskell.IPython
import IHaskell.Types
import IHaskell.IPython.ZeroMQ
import IHaskell.IPython.Types
import qualified IHaskell.IPython.Message.UUID as UUID
import qualified IHaskell.IPython.Stdin as Stdin
2014-01-25 17:31:55 -08:00
-- GHC API imports.
import GHC hiding (extensions, language)
-- | Compute the GHC API version number using the dist/build/autogen/cabal_macros.h
ghcVersionInts :: [Int]
ghcVersionInts = map (fromJust . readMay) . words . map dotToSpace $ VERSION_ghc
where
dotToSpace '.' = ' '
dotToSpace x = x
ihaskellCSS :: String
ihaskellCSS = [hereFile|html/custom.css|]
consoleBanner :: Text
consoleBanner =
"Welcome to IHaskell! Run `IHaskell --help` for more information.\n" <>
"Enter `:help` to learn more about IHaskell built-ins."
2013-12-28 23:21:04 -05:00
main :: IO ()
main = do
args <- parseFlags <$> getArgs
2014-01-08 16:27:36 -05:00
case args of
Left errorMessage -> hPutStrLn stderr errorMessage
Right args -> ihaskell args
2013-12-28 23:21:04 -05:00
ihaskell :: Args -> IO ()
ihaskell (Args (ShowHelp help) _) = putStrLn help
2014-03-09 17:57:08 -07:00
ihaskell (Args ConvertLhs args) = showingHelp ConvertLhs args $ convert args
ihaskell (Args InstallKernelSpec args) = showingHelp InstallKernelSpec args $ do
let kernelSpecOpts = parseKernelArgs args
replaceIPythonKernelspec kernelSpecOpts
ihaskell (Args (Kernel (Just filename)) args) = do
let kernelSpecOpts = parseKernelArgs args
runKernel kernelSpecOpts filename
2013-12-28 23:21:04 -05:00
showingHelp :: IHaskellMode -> [Argument] -> IO () -> IO ()
showingHelp mode flags act =
2015-03-19 18:30:22 -07:00
case find (== Help) flags of
2013-12-28 23:21:04 -05:00
Just _ ->
putStrLn $ help mode
2013-12-28 23:21:04 -05:00
Nothing ->
act
2013-12-28 23:21:04 -05:00
-- | Parse initialization information from the flags.
parseKernelArgs :: [Argument] -> KernelSpecOptions
parseKernelArgs = foldl' addFlag defaultKernelSpecOptions
where
addFlag kernelSpecOpts (ConfFile filename) =
kernelSpecOpts { kernelSpecConfFile = return (Just filename) }
addFlag kernelSpecOpts KernelDebug =
kernelSpecOpts { kernelSpecDebug = True }
addFlag kernelSpecOpts (GhcLibDir libdir) =
kernelSpecOpts { kernelSpecGhcLibdir = libdir }
addFlag kernelSpecOpts flag = error $ "Unknown flag" ++ show flag
2013-10-20 20:16:34 -07:00
-- | Run the IHaskell language kernel.
runKernel :: KernelSpecOptions -- ^ Various options from when the kernel was installed.
-> String -- ^ File with kernel profile JSON (ports, etc).
2013-12-28 23:21:04 -05:00
-> IO ()
runKernel kernelOpts profileSrc = do
let debug = kernelSpecDebug kernelOpts
libdir = kernelSpecGhcLibdir kernelOpts
2013-08-26 14:47:27 -07:00
-- Parse the profile file.
Just profile <- liftM decode $ LBS.readFile profileSrc
2014-01-05 23:01:38 -05:00
-- Necessary for `getLine` and their ilk to work.
dir <- getIHaskellDir
Stdin.recordKernelProfile dir profile
2014-01-05 23:01:38 -05:00
2013-08-26 14:47:27 -07:00
-- Serve on all sockets and ports defined in the profile.
interface <- serveProfile profile debug
2014-01-01 21:36:11 -05:00
-- Create initial state in the directory the kernel *should* be in.
2013-08-26 14:47:27 -07:00
state <- initialKernelState
2014-01-07 22:48:01 -05:00
modifyMVar_ state $ \kernelState -> return $
kernelState { kernelDebug = debug }
2013-08-26 14:47:27 -07:00
-- Receive and reply to all messages on the shell socket.
interpret libdir True $ do
2015-03-19 18:30:22 -07:00
-- Ignore Ctrl-C the first time. This has to go inside the `interpret`, because GHC API resets the
-- signal handlers for some reason (completely unknown to me).
liftIO ignoreCtrlC
2015-03-19 18:30:22 -07:00
-- Initialize the context by evaluating everything we got from the command line flags.
let noPublish _ = return ()
2015-06-02 11:42:48 +05:30
noWidget s _ = return s
evaluator line = void $ do
-- Create a new state each time.
stateVar <- liftIO initialKernelState
state <- liftIO $ takeMVar stateVar
2015-06-02 11:42:48 +05:30
evaluate state line noPublish noWidget
confFile <- liftIO $ kernelSpecConfFile kernelOpts
case confFile of
Just filename -> liftIO (readFile filename) >>= evaluator
2015-03-19 18:30:22 -07:00
Nothing -> return ()
2013-12-28 23:21:04 -05:00
forever $ do
-- Read the request from the request channel.
request <- liftIO $ readChan $ shellRequestChannel interface
-- Create a header for the reply.
replyHeader <- createReplyHeader (header request)
2015-03-19 18:30:22 -07:00
-- We handle comm messages and normal ones separately. The normal ones are a standard
-- request/response style, while comms can be anything, and don't necessarily require a response.
2014-03-16 16:37:32 -07:00
if isCommMessage request
then liftIO $ do
oldState <- takeMVar state
let replier = writeChan (iopubChannel interface)
newState <- handleComm replier oldState request replyHeader
putMVar state newState
writeChan (shellReplyChannel interface) SendNothing
else do
-- Create the reply, possibly modifying kernel state.
oldState <- liftIO $ takeMVar state
(newState, reply) <- replyTo interface request replyHeader oldState
liftIO $ putMVar state newState
-- Write the reply to the reply channel.
liftIO $ writeChan (shellReplyChannel interface) reply
where
ignoreCtrlC =
installHandler keyboardSignal (CatchOnce $ putStrLn "Press Ctrl-C again to quit kernel.")
Nothing
isCommMessage req = msgType (header req) `elem` [CommDataMessage, CommCloseMessage]
2014-03-16 16:37:32 -07:00
2013-08-26 14:47:27 -07:00
-- Initial kernel state.
initialKernelState :: IO (MVar KernelState)
initialKernelState = newMVar defaultKernelState
2013-10-10 22:52:32 -07:00
-- | Duplicate a message header, giving it a new UUID and message type.
dupHeader :: MessageHeader -> MessageType -> IO MessageHeader
2013-10-10 22:52:32 -07:00
dupHeader header messageType = do
2013-10-12 22:31:47 -07:00
uuid <- liftIO UUID.random
2013-10-10 22:52:32 -07:00
return header { messageId = uuid, msgType = messageType }
2013-08-26 14:47:27 -07:00
-- | Create a new message header, given a parent message header.
2013-10-20 12:22:27 -07:00
createReplyHeader :: MessageHeader -> Interpreter MessageHeader
2013-08-26 14:47:27 -07:00
createReplyHeader parent = do
-- Generate a new message UUID.
2013-10-20 12:22:27 -07:00
newMessageId <- liftIO UUID.random
let repType = fromMaybe err (replyType $ msgType parent)
err = error $ "No reply for message " ++ show (msgType parent)
2015-03-19 18:30:22 -07:00
return
MessageHeader
{ identifiers = identifiers parent
, parentHeader = Just parent
, metadata = Map.fromList []
, messageId = newMessageId
, sessionId = sessionId parent
, username = username parent
, msgType = repType
}
-- | Compute a reply to a message.
2013-10-12 22:31:47 -07:00
replyTo :: ZeroMQInterface -> Message -> MessageHeader -> KernelState -> Interpreter (KernelState, Message)
2015-03-19 18:30:22 -07:00
-- Reply to kernel info requests with a kernel info reply. No computation needs to be done, as a
-- kernel info reply is a static object (all info is hard coded into the representation of that
-- message type).
2014-01-05 23:01:38 -05:00
replyTo _ KernelInfoRequest{} replyHeader state =
2015-03-19 18:30:22 -07:00
return
(state, KernelInfoReply
{ header = replyHeader
, language = "haskell"
, versionList = ghcVersionInts
})
-- Reply to a shutdown request by exiting the main thread. Before shutdown, reply to the request to
-- let the frontend know shutdown is happening.
replyTo interface ShutdownRequest { restartPending = restartPending } replyHeader _ = liftIO $ do
writeChan (shellReplyChannel interface) $ ShutdownReply replyHeader restartPending
exitSuccess
2013-11-09 17:01:18 -08:00
-- Reply to an execution request. The reply itself does not require computation, but this causes
-- messages to be sent to the IOPub socket with the output of the code in the execution request.
replyTo interface req@ExecuteRequest { getCode = code } replyHeader state = do
-- Convenience function to send a message to the IOPub socket.
let send msg = liftIO $ writeChan (iopubChannel interface) msg
2013-10-07 21:32:51 -07:00
2014-01-05 23:01:38 -05:00
-- Log things so that we can use stdin.
dir <- liftIO getIHaskellDir
liftIO $ Stdin.recordParentHeader dir $ header req
2014-01-05 23:01:38 -05:00
-- Notify the frontend that the kernel is busy computing. All the headers are copies of the reply
-- header with a different message type, because this preserves the session ID, parent header, and
-- other important information.
busyHeader <- liftIO $ dupHeader replyHeader StatusMessage
2013-10-10 22:52:32 -07:00
send $ PublishStatus busyHeader Busy
-- Construct a function for publishing output as this is going. This function accepts a boolean
-- indicating whether this is the final output and the thing to display. Store the final outputs in
-- a list so that when we receive an updated non-final output, we can clear the entire output and
-- re-display with the updated output.
displayed <- liftIO $ newMVar []
updateNeeded <- liftIO $ newMVar False
pagerOutput <- liftIO $ newMVar []
let clearOutput = do
header <- dupHeader replyHeader ClearOutputMessage
send $ ClearOutput header True
sendOutput (ManyDisplay manyOuts) = mapM_ sendOutput manyOuts
sendOutput (Display outs) = do
header <- dupHeader replyHeader DisplayDataMessage
send $ PublishDisplayData header "haskell" $ map (convertSvgToHtml . prependCss) outs
2014-02-28 23:21:13 -08:00
convertSvgToHtml (DisplayData MimeSvg svg) = html $ makeSvgImg $ base64 $ E.encodeUtf8 svg
2014-02-28 23:21:13 -08:00
convertSvgToHtml x = x
makeSvgImg :: Base64 -> String
2015-05-25 23:42:34 +02:00
makeSvgImg base64data = T.unpack $ "<img src=\"data:image/svg+xml;base64," <>
base64data <>
"\"/>"
prependCss (DisplayData MimeHtml html) =
DisplayData MimeHtml $ mconcat ["<style>", T.pack ihaskellCSS, "</style>", html]
prependCss x = x
2015-06-02 11:42:48 +05:30
-- Publish outputs, ignore any CommMsgs
publish :: EvaluationResult -> IO ()
publish result = do
let final =
case result of
IntermediateResult{} -> False
FinalResult{} -> True
outs = outputs result
-- If necessary, clear all previous output and redraw.
clear <- readMVar updateNeeded
when clear $ do
clearOutput
disps <- readMVar displayed
mapM_ sendOutput $ reverse disps
-- Draw this message.
sendOutput outs
-- If this is the final message, add it to the list of completed messages. If it isn't, make sure we
-- clear it later by marking update needed as true.
modifyMVar_ updateNeeded (const $ return $ not final)
when final $ do
modifyMVar_ displayed (return . (outs :))
-- If this has some pager output, store it for later.
let pager = pagerOut result
unless (null pager) $
if usePager state
then modifyMVar_ pagerOutput (return . (++ pager))
else sendOutput $ Display pager
2015-06-02 11:42:48 +05:30
handleMessage :: KernelState -> WidgetMsg -> IO KernelState
handleMessage state (Open widget initVal stateVal) = do
2015-06-02 11:42:48 +05:30
-- Check whether the widget is already present in the state
let oldComms = openComms state
uuid = getCommUUID widget
present = isJust $ Map.lookup uuid oldComms
newComms = Map.insert uuid widget $ openComms state
newState = state { openComms = newComms }
target = targetName widget
communicate val = do
2015-06-02 11:42:48 +05:30
head <- dupHeader replyHeader CommDataMessage
writeChan (iopubChannel interface) $ CommData head uuid val
2015-06-02 11:42:48 +05:30
if present
then return state
else do -- Send the comm open
header <- dupHeader replyHeader CommOpenMessage
send $ CommOpen header target uuid initVal
-- Initial state update
communicate . toJSON $ UpdateState stateVal
2015-06-02 11:42:48 +05:30
-- Send anything else the widget requires.
open widget communicate
-- Store the widget in the kernelState
return newState
handleMessage state (Close widget value) = do
let oldComms = openComms state
present = isJust $ Map.lookup (getCommUUID widget) oldComms
target = targetName widget
uuid = getCommUUID widget
newComms = Map.delete uuid $ openComms state
newState = state { openComms = newComms }
if present
then do header <- dupHeader replyHeader CommCloseMessage
send $ CommClose header uuid value
return newState
else return state
handleMessage state (View widget) = do
let oldComms = openComms state
uuid = getCommUUID widget
present = isJust $ Map.lookup (getCommUUID widget) oldComms
when present $ do
header <- dupHeader replyHeader CommDataMessage
send . CommData header uuid $ toJSON DisplayWidget
return state
-- Assume that a state update means that it is time the stored widget also gets updated.
-- Thus replace the stored widget with the copy passed in the CommMsg.
handleMessage state (Update widget value) = do
let oldComms = openComms state
present = isJust $ Map.lookup (getCommUUID widget) oldComms
target = targetName widget
uuid = getCommUUID widget
newComms = Map.insert uuid widget $ openComms state
newState = state { openComms = newComms }
if present
then do header <- dupHeader replyHeader CommDataMessage
send . CommData header uuid . toJSON $ UpdateState value
return newState
else return state
widgetHandler :: KernelState -> [WidgetMsg] -> IO KernelState
widgetHandler state [] = return state
widgetHandler state (x:xs) = do
newState <- handleMessage state x
widgetHandler newState xs
let execCount = getExecutionCounter state
-- Let all frontends know the execution count and code that's about to run
inputHeader <- liftIO $ dupHeader replyHeader InputMessage
send $ PublishInput inputHeader (T.unpack code) execCount
-- Run code and publish to the frontend as we go.
2015-06-02 11:42:48 +05:30
updatedState <- evaluate state (T.unpack code) publish widgetHandler
2013-10-20 12:22:27 -07:00
-- Notify the frontend that we're done computing.
idleHeader <- liftIO $ dupHeader replyHeader StatusMessage
2013-10-20 12:22:27 -07:00
send $ PublishStatus idleHeader Idle
-- Take pager output if we're using the pager.
pager <- if usePager state
then liftIO $ readMVar pagerOutput
else return []
return
(updatedState, ExecuteReply
{ header = replyHeader
, pagerOutput = pager
, executionCounter = execCount
, status = Ok
})
replyTo _ req@CompleteRequest{} replyHeader state = do
2015-03-06 10:58:38 -08:00
let code = getCode req
pos = getCursorPos req
(matchedText, completions) <- complete (T.unpack code) pos
2015-03-06 10:58:38 -08:00
let start = pos - length matchedText
end = pos
reply = CompleteReply replyHeader (map T.pack completions) start end Map.empty True
2015-03-19 18:30:22 -07:00
return (state, reply)
replyTo _ req@InspectRequest{} replyHeader state = do
result <- inspect (T.unpack $ inspectCode req) (inspectCursorPos req)
let reply =
case result of
Just (Display datas) -> InspectReply
{ header = replyHeader
, inspectStatus = True
, inspectData = datas
}
_ -> InspectReply { header = replyHeader, inspectStatus = False, inspectData = [] }
2014-01-25 17:31:55 -08:00
return (state, reply)
2014-03-16 16:37:32 -07:00
-- TODO: Implement history_reply.
replyTo _ HistoryRequest{} replyHeader state = do
let reply = HistoryReply
{ header = replyHeader
-- FIXME
, historyReply = []
}
return (state, reply)
2014-03-16 16:37:32 -07:00
handleComm :: (Message -> IO ()) -> KernelState -> Message -> MessageHeader -> IO KernelState
handleComm replier kernelState req replyHeader = do
let widgets = openComms kernelState
uuid = commUuid req
dat = commData req
communicate value = do
head <- dupHeader replyHeader CommDataMessage
replier $ CommData head uuid value
case Map.lookup uuid widgets of
2014-03-16 16:37:32 -07:00
Nothing -> fail $ "no widget with uuid " ++ show uuid
Just (Widget widget) ->
case msgType $ header req of
CommDataMessage -> do
comm widget dat communicate
return kernelState
CommCloseMessage -> do
close widget dat
return kernelState { openComms = Map.delete uuid widgets }