| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
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:
- https://www.sciencedirect.com/science/article/pii/S0747717121000079
- PDF: https://www3.risc.jku.at/publications/download/risc_6260/variadic-equational-matching-jsc-final-with-mma-versions.pdf
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
IntegerorString, leave it unchanged. Howl: done. - Evaluate the head
hof the expression. Howl: done. - Evaluate each element
e_iof the expression in turn. Ifhis a symbol with attributesHoldFirst,HoldRest,HoldAll, orHoldAllComplete, then skip evaluation of certain elements. Howl:HoldAllCompletenot implemented. - Unless
hhas attributesSequenceHoldorHoldAllComplete, flatten out allSequenceobjects that appear among thee_i. Howl:SequenceHoldandHoldAllCompletenot implemented. - Unless
hhas attributeHoldAllComplete, strip the outermostUnevaluatedwrappers that appear among thee_i. Howl: not implemented. - If
hhas attributeFlat, flatten out all nested expressions with headh. Howl: done. - If
hhas attributeListable, thread through anye_ithat are lists. Howl:Listablenot implemented. - If
hhas attributeOrderless, sort thee_i. Howl: done. - Unless
hhas attributeHoldAllComplete, use any user-defined upvalues of symbolsfappearing in expressions of the formh[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
hin expressions of the formh[...]orh[...][...], including repeated curried application such ash[...][...][...]. 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[...]orh[...][...]. 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
- newtype SubstitutionSet = MkSubstitutionSet (Map Symbol Expr)
- data Substitution = MkSubstitution Symbol Expr
- data MatchingEq
- singletonSubstitutionSet :: Symbol -> Expr -> SubstitutionSet
- emptySubstitutionSet :: SubstitutionSet
- insertSubstitution :: Substitution -> SubstitutionSet -> Maybe SubstitutionSet
- insertSubstitutions :: Foldable f => f Substitution -> SubstitutionSet -> Maybe SubstitutionSet
- lookupBinding :: Symbol -> SubstitutionSet -> Maybe Expr
- removeBindings :: Seq Symbol -> SubstitutionSet -> SubstitutionSet
- applySubstitutions :: SubstitutionSet -> Expr -> Expr
- tryApplyRule :: Rule -> Expr -> Eval (Maybe Expr)
- solveMatchMaybe :: MatchingEq -> Eval (Maybe SubstitutionSet)
- eval :: Expr -> Eval Expr
Documentation
newtype SubstitutionSet #
A set of substitutions, with at most one substitution for each symbol.
Constructors
| MkSubstitutionSet (Map Symbol Expr) |
Instances
| Show SubstitutionSet # | |
Defined in Howl.Eval Methods showsPrec :: Int -> SubstitutionSet -> ShowS # show :: SubstitutionSet -> String # showList :: [SubstitutionSet] -> ShowS # | |
| Eq SubstitutionSet # | |
Defined in Howl.Eval Methods (==) :: SubstitutionSet -> SubstitutionSet -> Bool # (/=) :: SubstitutionSet -> SubstitutionSet -> Bool # | |
| Ord SubstitutionSet # | |
Defined in Howl.Eval Methods compare :: SubstitutionSet -> SubstitutionSet -> Ordering # (<) :: SubstitutionSet -> SubstitutionSet -> Bool # (<=) :: SubstitutionSet -> SubstitutionSet -> Bool # (>) :: SubstitutionSet -> SubstitutionSet -> Bool # (>=) :: SubstitutionSet -> SubstitutionSet -> Bool # max :: SubstitutionSet -> SubstitutionSet -> SubstitutionSet # min :: SubstitutionSet -> SubstitutionSet -> SubstitutionSet # | |
data Substitution #
A binding of a pattern variable to an expression.
Constructors
| MkSubstitution Symbol Expr |
Instances
| Show Substitution # | |
Defined in Howl.Eval Methods showsPrec :: Int -> Substitution -> ShowS # show :: Substitution -> String # showList :: [Substitution] -> ShowS # | |
| Eq Substitution # | |
Defined in Howl.Eval | |
| Ord Substitution # | |
Defined in Howl.Eval Methods compare :: Substitution -> Substitution -> Ordering # (<) :: Substitution -> Substitution -> Bool # (<=) :: Substitution -> Substitution -> Bool # (>) :: Substitution -> Substitution -> Bool # (>=) :: Substitution -> Substitution -> Bool # max :: Substitution -> Substitution -> Substitution # min :: Substitution -> Substitution -> Substitution # | |
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.
Instances
| Show MatchingEq # | |
Defined in Howl.Eval Methods showsPrec :: Int -> MatchingEq -> ShowS # show :: MatchingEq -> String # showList :: [MatchingEq] -> ShowS # | |
| Eq MatchingEq # | |
Defined in Howl.Eval | |
| Ord MatchingEq # | |
Defined in Howl.Eval Methods compare :: MatchingEq -> MatchingEq -> Ordering # (<) :: MatchingEq -> MatchingEq -> Bool # (<=) :: MatchingEq -> MatchingEq -> Bool # (>) :: MatchingEq -> MatchingEq -> Bool # (>=) :: MatchingEq -> MatchingEq -> Bool # max :: MatchingEq -> MatchingEq -> MatchingEq # min :: MatchingEq -> MatchingEq -> MatchingEq # | |
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.