java.lang.Object
java.lang.Record
io.github.vecbricks.jegg.Rewrite<L,D>
- Type Parameters:
L- the languageD- the analysis fact- Record Components:
name- the rule's name, astoString()shows it; not nulllhs- the left-hand pattern or multi-pattern to search for; not nullrhs- what to add for each match; not null. A pattern applier's variables are checked againstlhs's on constructioncondition- whether a match may be applied, read at apply time; not null
public record Rewrite<L extends Language<L>, D extends @Nullable Object>(String name, Searcher<L extends Language<L>> lhs, Applier<L extends Language<L>, D extends @Nullable Object> rhs, Condition<L extends Language<L>, D extends @Nullable Object> condition)
extends Record
A named rewrite: a left-hand
Searcher (a pattern, or several joined in a
MultiPattern), a right-hand Applier and a Condition.
Searching finds the matches; applying one adds the right-hand side and unions it with the
matched class, or, for a multi-pattern right-hand side, with the classes its clause variables
name. The runner does both for every rule each iteration, reading all matches
before writing any, which is the equality-saturation loop of the paper's Figure 5b.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapply(EGraph<L, D> graph, Matcher.Match match) Applies one match if its condition holds: the right-hand side'sApplier.applyTo(EGraph, Matcher.Match)adds its classes and unions them with the matched class, or, for a multi-pattern, does its own unions.Returns the value of theconditionrecord component.A rewrite from a pattern or multi-pattern to a computed right-hand side, unconditional.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.lhs()Returns the value of thelhsrecord component.multi(String name, MultiPattern<L> lhs, MultiPattern<L> rhs) A rewrite from a multi-pattern to a multi-pattern, unconditional: egg'smulti_rewrite!.name()Returns the value of thenamerecord component.A rewrite from one pattern to another, unconditional.rhs()Returns the value of therhsrecord component.The matches of the left-hand side, in the matcher's order.The firstlimitmatches, a prefix of all of them (Searcher.search(EGraph, int)); a pattern's search stops within the node that reached the limit, a multi-pattern's within the class of its first clause.toString()Returns a string representation of this record class.This rewrite under a further condition.
-
Constructor Details
-
Rewrite
Checks that no component is null and that a pattern right-hand side uses only variables the left-hand side binds.- Throws:
IllegalArgumentException- if the right-hand side has a subterm or payload variable the left-hand side does not bind (or, for a multi-pattern right-hand side, no earlier clause binds), or if the left-hand side is a multi-pattern whose first clause is a bare variable; egg refuses that one only when it searches
-
-
Method Details
-
of
public static <L extends Language<L>, D extends @Nullable Object> Rewrite<L,D> of(String name, Pattern<L> lhs, Pattern<L> rhs) A rewrite from one pattern to another, unconditional.- Type Parameters:
L- the languageD- the analysis fact- Parameters:
name- the rule's namelhs- the pattern to search forrhs- the pattern to instantiate for each match, over variableslhsbinds- Returns:
- the rewrite, whose condition always holds
-
multi
public static <L extends Language<L>, D extends @Nullable Object> Rewrite<L,D> multi(String name, MultiPattern<L> lhs, MultiPattern<L> rhs) A rewrite from a multi-pattern to a multi-pattern, unconditional: egg'smulti_rewrite!. Each match applies the right-hand clauses in order.- Type Parameters:
L- the languageD- the analysis fact- Parameters:
name- the rule's namelhs- the clauses to join for a match; the first is not a bare variablerhs- the clauses to apply for each match, over variableslhsbinds or an earlier right-hand clause does- Returns:
- the rewrite, whose condition always holds
-
dynamic
public static <L extends Language<L>, D extends @Nullable Object> Rewrite<L,D> dynamic(String name, Searcher<L> lhs, Applier<L, D> rhs) A rewrite from a pattern or multi-pattern to a computed right-hand side, unconditional.- Type Parameters:
L- the languageD- the analysis fact- Parameters:
name- the rule's namelhs- the pattern or multi-pattern to search forrhs- computes the classes to union with each match; a function's variables are not checked, a pattern's or a multi-pattern's (Applier.multi(MultiPattern)) are- Returns:
- the rewrite, whose condition always holds
-
when
-
search
The matches of the left-hand side, in the matcher's order. The condition is not read here but inapply(EGraph, Matcher.Match), as egg'sConditionalApplierreads it.- Parameters:
graph- the graph to search; not changed- Returns:
- a fresh list of matches, empty if there are none
-
search
The firstlimitmatches, a prefix of all of them (Searcher.search(EGraph, int)); a pattern's search stops within the node that reached the limit, a multi-pattern's within the class of its first clause.- Parameters:
graph- the graph to search; not changedlimit- the most matches to return; must be positive- Returns:
- a fresh list of at most
limitmatches, in the matcher's order
-
apply
Applies one match if its condition holds: the right-hand side'sApplier.applyTo(EGraph, Matcher.Match)adds its classes and unions them with the matched class, or, for a multi-pattern, does its own unions. Returns what that did, or empty if the condition did not hold and nothing was applied.The condition is read here, at apply time, not at search: the runner applies a match only after the matches before it in the iteration, whose merges may have made the condition false since the search.
- Parameters:
graph- the graph to write to; the match must come from a search of itmatch- a match of this rewrite's left-hand side: the matched class and the substitution for the variables- Returns:
- the unions that changed the graph and the number egg counts as applied, or empty if the condition did not hold
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
name
-
lhs
-
rhs
-
condition
-