howl-0.1.0.0: A small Wolfram Language interpreter and symbolic rewriting library
Safe HaskellNone
LanguageGHC2021

Howl.Eval.Context

Description

Core evaluation context types and operations.

This module defines the evaluator context, rules, declarations, attributes, and lower-level operations for running and extending Howl evaluation.

Synopsis

Documentation

data Rule #

A rewrite rule used by the evaluator.

Constructors

PatRule Pat Expr

A rule with a compiled pattern and right-hand side expression.

BuiltinRule (Expr -> Eval (Maybe Expr))

A builtin rule implemented by a Haskell function.

Instances

Instances details
Show Rule # 
Instance details

Defined in Howl.Eval.Context

Methods

showsPrec :: Int -> Rule -> ShowS #

show :: Rule -> String #

showList :: [Rule] -> ShowS #

PPrint Rule # 
Instance details

Defined in Howl.Eval.Context

Methods

pPrint :: Rule -> String #

data Context #

The mutable state used when evaluating expressions.

Instances

Instances details
MonadReader Context Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

ask :: Eval Context #

local :: (Context -> Context) -> Eval a -> Eval a #

reader :: (Context -> a) -> Eval a #

data Attributes #

TODO: Add Protected, NumericFunction, OneIdentity | The attributes associated with a symbol.

Constructors

MkAttributes 

Instances

Instances details
Show Attributes # 
Instance details

Defined in Howl.Eval.Context

Eq Attributes # 
Instance details

Defined in Howl.Eval.Context

Ord Attributes # 
Instance details

Defined in Howl.Eval.Context

data HoldType #

The hold behavior of a symbol during evaluation.

Constructors

HoldFirst 
HoldRest 
HoldAll 

Instances

Instances details
Show HoldType # 
Instance details

Defined in Howl.Eval.Context

Eq HoldType # 
Instance details

Defined in Howl.Eval.Context

Ord HoldType # 
Instance details

Defined in Howl.Eval.Context

emptyAttributes :: Attributes #

The default empty set of symbol attributes.

data SymbolRecord #

Information associated with a symbol in the evaluation context.

Constructors

MkSymbolRecord 

Fields

Instances

Instances details
Show SymbolRecord # 
Instance details

Defined in Howl.Eval.Context

data DownValues #

For cases when a pattern matches a unique expression, we store that expression in the uniqueMatches Map for fast lookup. In the evaluator, we try these lookups first before moving through the sequentialRules. | The down-values associated with a symbol.

Constructors

MkDownValues 

Instances

Instances details
Show DownValues # 
Instance details

Defined in Howl.Eval.Context

newtype Eval a #

The monad in which Howl evaluation runs.

An Eval computation has access to the current evaluation context, including symbol definitions, attributes, line-number state, and output/error handlers.

Constructors

Eval (ReaderT Context IO a) 

Instances

Instances details
MonadIO Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

liftIO :: IO a -> Eval a #

MonadCatch Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

catch :: (HasCallStack, Exception e) => Eval a -> (e -> Eval a) -> Eval a #

MonadMask Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

mask :: HasCallStack => ((forall a. Eval a -> Eval a) -> Eval b) -> Eval b #

uninterruptibleMask :: HasCallStack => ((forall a. Eval a -> Eval a) -> Eval b) -> Eval b #

generalBracket :: HasCallStack => Eval a -> (a -> ExitCase b -> Eval c) -> (a -> Eval b) -> Eval (b, c) #

MonadThrow Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

throwM :: (HasCallStack, Exception e) => e -> Eval a #

Applicative Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

pure :: a -> Eval a #

(<*>) :: Eval (a -> b) -> Eval a -> Eval b #

liftA2 :: (a -> b -> c) -> Eval a -> Eval b -> Eval c #

(*>) :: Eval a -> Eval b -> Eval b #

(<*) :: Eval a -> Eval b -> Eval a #

Functor Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

fmap :: (a -> b) -> Eval a -> Eval b #

(<$) :: a -> Eval b -> Eval a #

Monad Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

(>>=) :: Eval a -> (a -> Eval b) -> Eval b #

(>>) :: Eval a -> Eval b -> Eval b #

return :: a -> Eval a #

MonadFail Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

fail :: String -> Eval a #

MonadReader Context Eval # 
Instance details

Defined in Howl.Eval.Context

Methods

ask :: Eval Context #

local :: (Context -> Context) -> Eval a -> Eval a #

reader :: (Context -> a) -> Eval a #

ToExpr a => ToBuiltin (Eval (Maybe a)) #

A monadic builtin that may fail to match its arguments, signified by Nothing.

Instance details

Defined in Howl.Builtins.ToBuiltin

Methods

toBuiltin :: Eval (Maybe a) -> Seq Expr -> Eval (Maybe Expr) #

ToExpr a => ToBuiltin (Eval a) #

A monadic builtin that always matches its arguments.

Instance details

Defined in Howl.Builtins.ToBuiltin

Methods

toBuiltin :: Eval a -> Seq Expr -> Eval (Maybe Expr) #

(FromExpr a, ToExpr b) => ToBuiltin (Variadic a (Eval (Maybe b))) #

A variadic monadic builtin that may fail to match its arguments, signified by Nothing.

Instance details

Defined in Howl.Builtins.ToBuiltin

Methods

toBuiltin :: Variadic a (Eval (Maybe b)) -> Seq Expr -> Eval (Maybe Expr) #

(FromExpr a, ToExpr b) => ToBuiltin (Variadic a (Eval b)) #

A variadic monadic builtin that always matches its arguments.

Instance details

Defined in Howl.Builtins.ToBuiltin

Methods

toBuiltin :: Variadic a (Eval b) -> Seq Expr -> Eval (Maybe Expr) #

runEvalWithContext :: Context -> Eval a -> IO a #

Run an Eval computation in the given context.

runEvalNewContext :: Eval a -> IO a #

Create a new context and run an Eval computation in it.

getContext :: Eval Context #

Get the current context.

returnIfInCache :: Expr -> Eval Expr -> Eval Expr #

Return the cached value of an expression when available, otherwise run the given computation.

addToEvalCache :: Expr -> Eval () #

Add an expression to the evaluation cache.

emitErrorLine :: Text -> Eval () #

Emit a line of error output using the current error handler.

emitOutputLine :: Text -> Eval () #

Emit a line of standard output using the current output handler.

data Decl #

A declaration that can be added to the evaluation context.

Constructors

OwnValue Symbol Expr

An own-value assignment for a symbol.

DownValue Symbol Rule

A down-value rule for expressions with the given head.

UpValue Symbol Rule

An up-value rule for expressions containing the given symbol.

Instances

Instances details
Show Decl # 
Instance details

Defined in Howl.Eval.Context

Methods

showsPrec :: Int -> Decl -> ShowS #

show :: Decl -> String #

showList :: [Decl] -> ShowS #

newContext :: IO Context #

Create a new empty evaluation context.

getLineNumber :: Eval Int #

Get the current input line number.

incrLineNumber :: Eval () #

Increment the current input line number.

setErrorLineHandler :: Context -> (Text -> IO ()) -> IO () #

Set the error output handler for a Context.

setOutputLineHandler :: Context -> (Text -> IO ()) -> IO () #

Set the standard output handler for a Context.

lookupSymbolRecord :: Symbol -> Eval (Maybe SymbolRecord) #

Look up the record associated with a symbol in the current context.

lookupAttributes :: Symbol -> Eval Attributes #

Look up the attributes of a symbol in the current context.

addDownValue :: Symbol -> Rule -> Eval () #

Add a down-value rule for the given symbol.

addUpValue :: Symbol -> Rule -> Eval () #

Add an up-value rule for the given symbol.

addDecl :: Decl -> Eval () #

Add a declaration to the current context.

modifyAttributes :: Symbol -> (Attributes -> Attributes) -> Eval () #

Modify the attributes of a symbol in the current context.

setAttributes :: Symbol -> Attributes -> Eval () #

Set the attributes of a symbol in the current context.

setFlat :: Attributes -> Attributes #

Set the Flat attribute.

setOrderless :: Attributes -> Attributes #

Set the Orderless attribute.

setNumericFunction :: Attributes -> Attributes #

Set the NumericFunction attribute.

setHoldType :: HoldType -> Attributes -> Attributes #

Set the hold behavior of a symbol.

clear :: Symbol -> Eval () #

Clear the values associated with a symbol, leaving its attributes.

clearAll :: Symbol -> Eval () #

Clear the values and attributes associated with a symbol.

newModuleSymbol :: Symbol -> Eval Symbol #

Create a fresh module-local symbol derived from the given base symbol.

getDefinedSymbols :: Eval [Symbol] #

Get the symbols that currently have records in the context.

compilePat :: Expr -> Eval Pat #

Compile an expression into a pattern, using the current context to determine attributes such as associativity and commutativity.

dummyAddToEvalCache :: EvalCache -> Expr -> Eval () #

A no-op cache insertion hook.

dummyReturnIfInCache :: EvalCache -> Expr -> Eval Expr -> Eval Expr #

A cache lookup hook that always evaluates the given computation.