neurolang.frontend.neurolang module¶
Neurolang Datalog grammar definition and translation to intermediate representation ============================================== 1- defines the Neurolang Datalog syntax 2- code written using this grammar can be parsed into an Abstract Syntax Tree (AST) 3- the obtained AST can ba walked through to translate it to the intermediate representation used in the backend
- class neurolang.frontend.neurolang.ExistentialPredicate(head, body)¶
Bases:
Quantifier
Methods
__call__
(*args, **kwargs)Call self as a function.
apply
(*args)Builds a new expression using a tuple of its parameters
unapply
()Returns a tuple of parameters used to build the expression.
cast
change_type
get_wrapped_attribute
- type = typing.Any¶
- exception neurolang.frontend.neurolang.NeuroLangException¶
Bases:
Exception
Base class for NeuroLang Exceptions
- class neurolang.frontend.neurolang.NeuroLangIntermediateRepresentation(type_name_map: Iterable | Mapping | None = None)¶
Bases:
ASTWalker
Abstract Syntax Tree walker class implementing translation from an ASTNode to the corresponding Neurolang Intermediate Representation Expression (ir.Expression)
Methods
evaluate
(ast)Converts input to intermediate representation: - if input is an ASTNode, calls -if it exists- the class method corresponding to the node type (methods are declared in child class) - if input is a list of ASTNode, recursively calls itself on every node - else, simply passes ast through
and_test
assignment
comparison
dotted_identifier
function_application
integer
negated_argument
point_float
power
predicate
product
projection
query
statement
string
sum
tuple
value
- and_test(ast)¶
- assignment(ast)¶
- comparison(ast)¶
- dotted_identifier(ast)¶
- function_application(ast)¶
- integer(ast)¶
- negated_argument(ast)¶
- point_float(ast)¶
- power(ast)¶
- predicate(ast)¶
- product(ast)¶
- projection(ast)¶
- query(ast)¶
- statement(ast)¶
- string(ast)¶
- sum(ast)¶
- tuple(ast)¶
- value(ast)¶
- class neurolang.frontend.neurolang.NeuroLangIntermediateRepresentationCompiler(functions=None, type_name_map=None, types=None, symbols=None)¶
Bases:
ExpressionBasicEvaluator
- Attributes:
patterns
Property holding an iterator of triplets
(pattern, guard, action)
.
Methods
match
(expression)Find the action for a given expression by going through the
patterns
.pattern_match
(pattern, expression)Return
True
ifpattern
matchesexpression
.compile
eval_lambda
evaluate_function
evaluate_function_infer_type
evaluate_projection
get_intermediate_representation
pattern_match_expression
pattern_match_expression_parameters
pattern_match_expression_tuple
pattern_match_tuple
process_expression
process_iterable_argument
walk
- compile(ast, **kwargs)¶
- get_intermediate_representation(ast, **kwargs)¶
- type = typing.Any¶
- class neurolang.frontend.neurolang.PatternMatcher(*args, **kwargs)¶
Bases:
object
Class for expression pattern matching.
- Attributes:
patterns
Property holding an iterator of triplets
(pattern, guard, action)
.
Methods
match
(expression)Find the action for a given expression by going through the
patterns
.pattern_match
(pattern, expression)Return
True
ifpattern
matchesexpression
.pattern_match_expression
pattern_match_expression_parameters
pattern_match_expression_tuple
pattern_match_tuple
- 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
ifpattern
matchesexpression
.Patterns are of the following form:
...
matches everything.Expression
class matches ifexpression
is instance of the given class for anytype
.Expression
[T]
matches ifexpression
is instance of the given class withexpression.type
a subtype ofT
.Expression
[T](Pattern_0, Pattern_1, ....., Pattern_N)
matches ifexpression
is instance of the given class withexpression.type
a subtype ofT
and 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 atuple
of length N + 1 whereexpression[0]
matchesPattern_0
,expression[1]
matchesPattern_1
, etc.instance
an instance of a python class not subclassingExpression
matches wheninstance == expression
- pattern_match_expression(pattern, expression)¶
- pattern_match_expression_parameters(pattern, expression)¶
- pattern_match_expression_tuple(expression, pattern)¶
- pattern_match_tuple(pattern, 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 matchingexpression
instance to be executed upon pattern and match beingTrue
.
- type = typing.Any¶
- neurolang.frontend.neurolang.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. SeePatternMatcher.pattern_match()
for more details on patterns.