lambeq.rewrite
- class lambeq.rewrite.CoordinationRewriteRule(words: Optional[Container[str]] = None)[source]
Bases:
lambeq.rewrite.base.RewriteRule
A rewrite rule for coordination.
This rule matches the word ‘and’ with codomain
a.r @ a @ a.l
for pregroup typea
, and replaces the word, based on [Kar2016], with a layer of interleaving spiders.- __call__(box: discopy.rigid.Box) Optional[discopy.rigid.Diagram]
Apply the rewrite rule to a box.
- Parameters
- box
discopy.rigid.Box
The candidate box to be tested against this rewrite rule.
- box
- Returns
discopy.rigid.Diagram
, optionalThe rewritten diagram, or
None
if rule does not apply.
Notes
The default implementation uses the
matches()
andrewrite()
methods, but derived classes may choose to not use them, since the defaultRewriter
implementation does not call those methods directly, only this one.
- __init__(words: Optional[Container[str]] = None) None [source]
Instantiate a CoordinationRewriteRule.
- Parameters
- wordscontainer of str, optional
A list of words to be rewritten by this rule. If a box does not have one of these words, it will not be rewritten, even if the codomain matches. If omitted, the rewrite applies only to the word “and”.
- matches(box: discopy.rigid.Box) bool [source]
Check if the given box should be rewritten.
- rewrite(box: discopy.rigid.Box) discopy.rigid.Diagram [source]
Rewrite the given box.
- class lambeq.rewrite.CurryRewriteRule[source]
Bases:
lambeq.rewrite.base.RewriteRule
A rewrite rule using map-state duality.
- __call__(box: discopy.rigid.Box) Optional[discopy.rigid.Diagram]
Apply the rewrite rule to a box.
- Parameters
- box
discopy.rigid.Box
The candidate box to be tested against this rewrite rule.
- box
- Returns
discopy.rigid.Diagram
, optionalThe rewritten diagram, or
None
if rule does not apply.
Notes
The default implementation uses the
matches()
andrewrite()
methods, but derived classes may choose to not use them, since the defaultRewriter
implementation does not call those methods directly, only this one.
- __init__() None [source]
Instantiate a CurryRewriteRule.
This rule uses the map-state duality by iteratively uncurrying on both sides of each box. When used in conjunction with
normal_form()
, this removes cups from the diagram in exchange for depth. Diagrams with less cups become circuits with less post-selection, which results in faster QML experiments.
- matches(box: discopy.rigid.Box) bool [source]
Check if the given box should be rewritten.
- rewrite(box: discopy.rigid.Box) discopy.rigid.Diagram [source]
Rewrite the given box.
- class lambeq.rewrite.RewriteRule[source]
Bases:
abc.ABC
Base class for rewrite rules.
- __call__(box: discopy.rigid.Box) Optional[discopy.rigid.Diagram] [source]
Apply the rewrite rule to a box.
- Parameters
- box
discopy.rigid.Box
The candidate box to be tested against this rewrite rule.
- box
- Returns
discopy.rigid.Diagram
, optionalThe rewritten diagram, or
None
if rule does not apply.
Notes
The default implementation uses the
matches()
andrewrite()
methods, but derived classes may choose to not use them, since the defaultRewriter
implementation does not call those methods directly, only this one.
- abstract matches(box: discopy.rigid.Box) bool [source]
Check if the given box should be rewritten.
- abstract rewrite(box: discopy.rigid.Box) discopy.rigid.Diagram [source]
Rewrite the given box.
- class lambeq.rewrite.Rewriter(rules: Optional[Iterable[Union[str, RewriteRule]]] = None)[source]
Bases:
object
Class that rewrites diagrams.
Comes with a set of default rules.
- __call__(diagram: discopy.rigid.Diagram) discopy.rigid.Diagram [source]
Apply the rewrite rules to the given diagram.
- __init__(rules: Optional[Iterable[Union[str, RewriteRule]]] = None) None [source]
Initialise a rewriter.
- Parameters
- rulesiterable of str or RewriteRule, optional
A list of rewrite rules to use.
RewriteRule
instances are used directly, str objects are used as names of the default rules. SeeRewriter.available_rules()
for the list of rule names. If omitted, all the default rules are used.
- add_rules(*rules: Union[str, lambeq.rewrite.base.RewriteRule]) None [source]
Add rules to this rewriter.
- class lambeq.rewrite.SimpleRewriteRule(cod: Ty, template: Diagram, words: Optional[Container[str]] = None, case_sensitive: bool = False)[source]
Bases:
lambeq.rewrite.base.RewriteRule
A simple rewrite rule.
This rule matches each box against a required codomain and, if provided, a set of words. If they match, the word box is rewritten into a set template.
- __call__(box: discopy.rigid.Box) Optional[discopy.rigid.Diagram]
Apply the rewrite rule to a box.
- Parameters
- box
discopy.rigid.Box
The candidate box to be tested against this rewrite rule.
- box
- Returns
discopy.rigid.Diagram
, optionalThe rewritten diagram, or
None
if rule does not apply.
Notes
The default implementation uses the
matches()
andrewrite()
methods, but derived classes may choose to not use them, since the defaultRewriter
implementation does not call those methods directly, only this one.
- __init__(cod: Ty, template: Diagram, words: Optional[Container[str]] = None, case_sensitive: bool = False) None [source]
Instantiate a simple rewrite rule.
- Parameters
- cod
discopy.rigid.Ty
The type that the codomain of each box is matched against.
- template
discopy.rigid.Diagram
The diagram that a matching box is replaced with. A special placeholder box is replaced by the word in the matched box, and can be created using
SimpleRewriteRule.placeholder()
.- wordscontainer of str, optional
If provided, this is a list of words that are rewritten by this rule. If a box does not have one of these words, it is not rewritten, even if the codomain matches. If omitted, all words are permitted.
- case_sensitivebool, default: False
This indicates whether the list of words specified above are compared case-sensitively. The default is
False
.
- cod
- matches(box: discopy.rigid.Box) bool [source]
Check if the given box should be rewritten.
- classmethod placeholder(cod: discopy.rigid.Ty) discopy.grammar.pregroup.Word [source]
Helper function to generate the placeholder for a template.
- Parameters
- cod
discopy.rigid.Ty
The codomain of the placeholder, and hence the word in the resulting rewritten diagram.
- cod
- Returns
discopy.rigid.Box
A placeholder box with the given codomain.
- rewrite(box: discopy.rigid.Box) discopy.rigid.Diagram [source]
Rewrite the given box.