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

type

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 if pattern matches expression.

type

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 if pattern matches expression.

type

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 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

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. 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 = 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. See PatternMatcher.pattern_match() for more details on patterns.

neurolang.frontend.neurolang.parser(code: str, **kwargs)

Parses Datalog code into an Abstract Syntax Tree (AST)

Parameters:
codestr

code written in Datalog, as described by it’s EBNF syntax

**kwargs

completed and passed to the lark parser

Returns:
AST

Abstract Syntax Tree resulting from code parsing