neurolang.expression_pattern_matching#
Module implementing expression pattern matching.
Functions
|
Decorate by adding patterns to a |
|
Decorate by adding patterns to a |
|
Classes
|
|
|
Class for expression pattern matching. |
|
Exceptions
|
- class neurolang.expression_pattern_matching.PatternMatcher(*args, **kwargs)#
Class for expression pattern matching.
- match(expression)#
Find the action for a given expression by going through the
patterns.Goes through the triplets in in
patternsand calls the action specified by the first satisfied triplet.
- pattern_match(pattern, expression)#
Return
Trueifpatternmatchesexpression.Patterns are of the following form:
...matches everything.Expressionclass matches ifexpressionis instance of the given class for anytype.Expression[T]matches ifexpressionis instance of the given class withexpression.typea subtype ofT.Expression[T](Pattern_0, Pattern_1, ....., Pattern_N)matches ifexpressionis instance of the given class withexpression.typea subtype ofTand recursively matches the construction parameters of the instance on the types given asPattern_0,Pattern_1, etc.(Pattern_0, ....., Pattern_N)matches when expression is atupleof length N + 1 whereexpression[0]matchesPattern_0,expression[1]matchesPattern_1, etc.instancean instance of a python class not subclassingExpressionmatches wheninstance == expression
- property patterns#
Property holding an iterator of triplets
(pattern, guard, action).pattern: is an Expression class, or instance where construction parameters and the type can be replaced by an ellipsis … to signal a wildcard. Seepattern_match()for more details.guard: is a function mapping an Expression instance to a boolean orNone.action: is the method receiving the matchingexpressioninstance to be executed upon pattern and match beingTrue.
- neurolang.expression_pattern_matching.add_match(pattern, guard=None)#
Decorate by adding patterns to a
PatternMatcherclass.Should be used as @add_match(PATTERN, GUARD) to turn the decorated method, receiving an instance of
Expressioninto a matching case. SeePatternMatcher.pattern_match()for more details on patterns.