ihaskell-0.11.0.0: A Haskell backend kernel for the Jupyter project.
Safe HaskellSafe-Inferred
LanguageHaskell2010

IHaskell.Eval.Evaluate

Description

This module exports all functions used for evaluation of IHaskell input.

Synopsis

Documentation

interpret :: String -> Bool -> Bool -> (Bool -> Interpreter a) -> IO a Source #

Run an interpreting action. This is effectively runGhc with initialization and importing. The allowedStdin argument indicates whether stdin is handled specially, which cannot be done in a testing environment. The needsSupportLibraries argument indicates whether we want support libraries to be imported, which is not the case during testing. The argument passed to the action indicates whether the IHaskell library is available.

testInterpret :: Interpreter a -> IO a Source #

Interpreting function for testing.

testEvaluate :: String -> IO () Source #

Evaluation function for testing.

evaluate Source #

Arguments

:: KernelState

The kernel state.

-> String

Haskell code or other interpreter commands.

-> Publisher

Function used to publish data outputs.

-> (KernelState -> [WidgetMsg] -> IO KernelState)

Function to handle widget messages

-> Interpreter (KernelState, ErrorOccurred) 

Evaluate some IPython input code.

liftIO :: MonadIO m => IO a -> m a #

Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

Example

Expand
import Control.Monad.Trans.State -- from the "transformers" library

printState :: Show s => StateT s IO ()
printState = do
  state <- get
  liftIO $ print state

Had we omitted liftIO, we would have ended up with this error:

• Couldn't match type ‘IO’ with ‘StateT s IO’
 Expected type: StateT s IO ()
   Actual type: IO ()

The important part here is the mismatch between StateT s IO () and IO ().

Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

> evalStateT printState "hello"
"hello"

> evalStateT printState 3
3