This module provides the main high-level interface to the library
functionality. It does this through a monadic interface, mainly due to the
fact that several of the components require the use of the IO monad. It is
highly recommended that users of this library use a monad and then implement
CSPMMonad class on their own custom monad. An example of this is shown
by the basic implementation of the
The main library datatype is exported by
provides an AST representation of machine CSP. Most of the pieces of syntax,
like expressions (
Exp), are parametrised by the type of the variables that
it contains. For more information see the comment at the top of the above
The library exports several APIs which, in likely order of usage, are:
- Parses strings or files and produces an AST, parametrised
UnRenamedName, which are simply pieces of text.
- Renames the AST and produces an equivalent AST, but
Name, which uniquely identify the binding instance of each variable (see documentation of
- Type checks an AST, in the process annotating it with types.
- Desugars an AST, remove syntactic sugar and prepares it for evaluation. The AST produced by this phase should not be pretty printed as it parenthesis have been removed, potentially making it not equivalent.
- Evaluates an AST, returning a
Value. Note that the evaluator is lazy, meaning that the resulting Value will be generated as it is consumed, making it suitable for streaming to subsequent compilation phases.
For example, suppose we wish to evaluate the expression
the context of the file
test.csp we could use the following segment of
main :: IO () main = do session <- newCSPMSession (value, resultingSession) <- unCSPM session $ do -- Parse the file, returning something of type PModule. parsedFile <- parseFile "test.csp" -- Rename the file, returning something of type TCModule. renamedFile <- renameFile parsedFile -- Typecheck the file, annotating it with types. typeCheckedFile <- typeCheckFile renamedFile -- Desugar the file, returning the version ready for evaluation. desugaredFile <- desugarFile typeCheckedFile -- Bind the file, making all functions and patterns available. bindFile desugaredFile -- The file is now ready for use, so now we build the expression -- to be evaluated. parsedExpression <- parseExpression "test(1,2,3)" renamedExpression <- renameExpression parsedExpression typeCheckedExpression <- typeCheckExpression renamedExpression desugaredExpression <- desugarExpression typeCheckedExpression -- Evaluate the expression in the current context. value <- evaluateExpression desugaredExpression return value putStrLn (show (prettyPrint value)) return ()
This would pretty print the value of the expression to stdout.
- data CSPMSession
- newCSPMSession :: MonadIO m => m CSPMSession
- class MonadIO m => CSPMMonad m where
- withSession :: CSPMMonad m => (CSPMSession -> m a) -> m a
- type CSPM = StateT CSPMSession IO
- unCSPM :: CSPMSession -> CSPM a -> IO (a, CSPMSession)
- module CSPM.DataStructures.Names
- module CSPM.DataStructures.Syntax
- module CSPM.DataStructures.Types
- module CSPM.Evaluator.Values
- parseStringAsFile :: CSPMMonad m => String -> m [PModule]
- parseFile :: CSPMMonad m => FilePath -> m [PModule]
- parseInteractiveStmt :: CSPMMonad m => String -> m PInteractiveStmt
- parseExpression :: CSPMMonad m => String -> m PExp
- renameFile :: CSPMMonad m => [PModule] -> m [TCModule]
- renameInteractiveStmt :: CSPMMonad m => PInteractiveStmt -> m TCInteractiveStmt
- renameExpression :: CSPMMonad m => PExp -> m TCExp
- typeCheckFile :: CSPMMonad m => [TCModule] -> m [TCModule]
- typeCheckInteractiveStmt :: CSPMMonad m => TCInteractiveStmt -> m TCInteractiveStmt
- typeCheckExpression :: CSPMMonad m => TCExp -> m TCExp
- ensureExpressionIsOfType :: CSPMMonad m => Type -> TCExp -> m TCExp
- dependenciesOfExp :: CSPMMonad m => TCExp -> m [Name]
- typeOfExpression :: CSPMMonad m => TCExp -> m Type
- desugarFile :: CSPMMonad m => [TCModule] -> m [TCModule]
- desugarInteractiveStmt :: CSPMMonad m => TCInteractiveStmt -> m TCInteractiveStmt
- desugarExpression :: CSPMMonad m => TCExp -> m TCExp
- bindFile :: CSPMMonad m => [TCModule] -> m ()
- bindDeclaration :: CSPMMonad m => TCDecl -> m ()
- getBoundNames :: CSPMMonad m => m [Name]
- evaluateExpression :: CSPMMonad m => TCExp -> m Value
- runParserInCurrentState :: CSPMMonad m => FilePath -> ParseMonad a -> m a
- runRenamerInCurrentState :: CSPMMonad m => RenamerMonad a -> m a
- runTypeCheckerInCurrentState :: CSPMMonad m => TypeCheckMonad a -> m (a, [ErrorMessage])
- runEvaluatorInCurrentState :: CSPMMonad m => EvaluationMonad a -> m a
- reportWarnings :: CSPMMonad m => m (a, [ErrorMessage]) -> m a
- getLibCSPMVersion :: Version
CSPMSession represents the internal states of all the various
The CSPMMonad is the main monad in which all functions must be called.
Whilst there is a build in representation (see
CSPM) it is recommended
that you define an instance of
CSPMMonad over whatever monad you use.
Get the current session.
Update the current session.
This is called whenever warnings are emitted.
Executes an operation giving it access to the current
A basic implementation of the monad
CSPM function, returning the result and the resulting session.
Common Data Types
Defines the names that are used by machine CSP.
Defines the abstract syntax for machine CSP.
Defines the types used by the typechecker.
Defines the values produced by the evaluator.
Parses a string, treating it as though it were a file. Throws a
SourceError on any parse error.
Parse a file
fp. Throws a
SourceError on any parse error.
Rename ian interactive statement.
Type Checker API
Type checks a
Type checkes a
PExp, returning the desugared and annotated version.
Names that the given type checked expression depends on.
Gets the type of the expression in the current context.
Desugar a file, preparing it for evaulation.
Desugars an interactive statement.
Binds all the declarations that are in a particular file. Requires the file to be desugared.
Takes a declaration and adds it to the current environment. Requires the declaration to be desugared.
Get a list of currently bound names in the environment.
Evaluates the expression in the current context. Requires the expression to be desugared.
Whilst this module provides many of the commonly used functionality within the CSPM monad, sometimes there are additional functions exported by other modules that are of use. The following functions allow the renamer, typechecker and evaluator to be run in the current state. They also save the resulting state in the current session.
Runs renamer in the current state.
Runs the typechecker in the current state, saving the resulting state and returning any warnings encountered.
Runs the evaluator in the current state, saving the resulting state.
Given a program that can return warnings, runs the program and raises
any warnings found using