neurolang.expression_pattern_matching#

Module implementing expression pattern matching.

Functions

add_entry_point_match(pattern[, guard])

Decorate by adding patterns to a PatternMatcher class.

add_match(pattern[, guard])

Decorate by adding patterns to a PatternMatcher class.

signature(cls)

Classes

Pattern(pattern, guard, action)

PatternMatcher(*args, **kwargs)

Class for expression pattern matching.

PatternMatchingMetaClass(name, bases, classdict)

Exceptions

NeuroLangPatternMatchingNoMatch

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 patterns and calls the action specified by the first satisfied triplet.

pattern_match(pattern, expression)#

Return True if pattern matches expression.

Patterns are of the following form:

  • ... matches everything.

  • Expression class matches if expression is instance of the given class for any type.

  • Expression [T] matches if expression is instance of the given class with expression.type a subtype of T.

  • Expression [T](Pattern_0, Pattern_1, ....., Pattern_N) matches if expression is instance of the given class with expression.type a subtype of T and recursively matches the construction parameters of the instance on the types given as Pattern_0, Pattern_1, etc.

  • (Pattern_0, ....., Pattern_N) matches when expression is a tuple of length N + 1 where expression[0] matches Pattern_0, expression[1] matches Pattern_1, etc.

  • instance an instance of a python class not subclassing Expression matches when instance == 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. See pattern_match() for more details.

  • guard: is a function mapping an Expression instance to a boolean or None.

  • action: is the method receiving the matching expression instance to be executed upon pattern and match being True.

type#

alias of Any

neurolang.expression_pattern_matching.add_match(pattern, guard=None)#

Decorate by adding patterns to a PatternMatcher class.

Should be used as @add_match(PATTERN, GUARD) to turn the decorated method, receiving an instance of Expression into a matching case. See PatternMatcher.pattern_match() for more details on patterns.