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

Howl.Eval

Description

Pattern matching and evaluation.

This module implements the matcher used by rewrite rules together with the main expression evaluator.

Pattern Matching

The pattern matching algorithm implemented here is described in:

Specifically, Howl implements algorithm M_{Mma}, which is an incomplete, strict variant of the algorithm described in that paper.

Evaluation

The Wolfram Language evaluation sequence is described here: https://reference.wolfram.com/language/tutorial/Evaluation.html

The list below summarizes the intended evaluation order together with the current Howl status.

  • If the expression is a raw object such as Integer or String, leave it unchanged. Howl: done.
  • Evaluate the head h of the expression. Howl: done.
  • Evaluate each element e_i of the expression in turn. If h is a symbol with attributes HoldFirst, HoldRest, HoldAll, or HoldAllComplete, then skip evaluation of certain elements. Howl: HoldAllComplete not implemented.
  • Unless h has attributes SequenceHold or HoldAllComplete, flatten out all Sequence objects that appear among the e_i. Howl: SequenceHold and HoldAllComplete not implemented.
  • Unless h has attribute HoldAllComplete, strip the outermost Unevaluated wrappers that appear among the e_i. Howl: not implemented.
  • If h has attribute Flat, flatten out all nested expressions with head h. Howl: done.
  • If h has attribute Listable, thread through any e_i that are lists. Howl: Listable not implemented.
  • If h has attribute Orderless, sort the e_i. Howl: done.
  • Unless h has attribute HoldAllComplete, use any user-defined upvalues of symbols f appearing in expressions of the form h[f[e1,...], ...]. Howl does not look for upvalues inside the head. For example, the following is not supported:
g /: f[g[x_]][1] := foo;

because g appears inside the head of the expression rather than in the top-level argument sequence.

  • Use any builtin upvalues. Howl lets the user choose the ordering and gives no special privilege to builtin versus user-defined upvalues.
  • Use any user-defined downvalues associated to h in expressions of the form h[...] or h[...][...], including repeated curried application such as h[...][...][...]. Wolfram Language distinguishes between downvalues and subvalues; Howl currently does not and instead associates downvalues to the root symbol.
  • Use any builtin transformation rules for h[...] or h[...][...]. Howl again does not distinguish between builtin and user-defined downvalues.

Conceptually, we would like to try every rule in the Context when evaluating expressions. In practice this would perform poorly for large contexts, so the upvalue/downvalue machinery serves as a set of heuristics for pre-selecting rules that may have a chance of matching.

Synopsis

Documentation

data Substitution #

A binding of a pattern variable to an expression.

Constructors

MkSubstitution Symbol Expr 

data MatchingEq #

A matching equation produced by the matcher.

A SingleEq matches one pattern against one expression, while a SeqEq matches a sequence of patterns against a sequence of expressions under the given application type.

Constructors

SingleEq !Pat !Expr 
SeqEq !AppType !(Seq Pat) !(Seq Expr) 

Instances

Instances details
Show MatchingEq # 
Instance details

Defined in Howl.Eval

Eq MatchingEq # 
Instance details

Defined in Howl.Eval

Ord MatchingEq # 
Instance details

Defined in Howl.Eval

singletonSubstitutionSet :: Symbol -> Expr -> SubstitutionSet #

A substitution set containing a single binding.

emptySubstitutionSet :: SubstitutionSet #

The empty substitution set.

insertSubstitution :: Substitution -> SubstitutionSet -> Maybe SubstitutionSet #

Attempt to insert a Substitution. If an old binding is already present and agrees with the new one, or if there was no old binding, then return the result. Otherwise there is a conflict -- return Nothing.

insertSubstitutions :: Foldable f => f Substitution -> SubstitutionSet -> Maybe SubstitutionSet #

Attempt to insert the given substitutions.

lookupBinding :: Symbol -> SubstitutionSet -> Maybe Expr #

Look up the binding corresponding to a symbol in the given substitution set.

removeBindings :: Seq Symbol -> SubstitutionSet -> SubstitutionSet #

Remove the given bindings from a substitution set.

applySubstitutions :: SubstitutionSet -> Expr -> Expr #

Replace the symbols in the given expression with their corresponding bindings in the given substitution set.

tryApplyRule :: Rule -> Expr -> Eval (Maybe Expr) #

Try to apply a rule to the given expression. If the rule matches, return 'Just result' otherwise return nothing.

solveMatchMaybe :: MatchingEq -> Eval (Maybe SubstitutionSet) #

Solve a matching equation, returning a consistent substitution set if the match succeeds.

eval :: Expr -> Eval Expr #

Evaluate an expression using the current context.