neurolang.datalog.expression_processing module

Utilities to process intermediate representations of Datalog programs.

class neurolang.datalog.expression_processing.CollapseConjunctiveAntecedents(*args, **kwargs)

Bases: CollapseConjunctions

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

conjunction

implication_with_collapsable_conjunctive_antecedent

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

process_expression

process_iterable_argument

walk

implication_with_collapsable_conjunctive_antecedent(implication)
type = typing.Any
class neurolang.datalog.expression_processing.EqualitySymbolLeftHandSideNormaliseMixin(*args, **kwargs)

Bases: PatternWalker

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

reverse_equality

sort_antecedent_equalities

walk

static reverse_equality(equality)
sort_antecedent_equalities(implication)
type = typing.Any
class neurolang.datalog.expression_processing.FlattenQueryInNonRecursiveUCQ(program)

Bases: PatternWalker

Flatten a query defined by a non-recursive union of conjunctive queries (UCQ)

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

builtin_application

conjunction

negation

negation_function_application

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

query_predicate

walk

builtin_application(fa)
conjunction(expression)
negation(expression)
negation_function_application(expression)
query_predicate(qpred)
type = typing.Any
class neurolang.datalog.expression_processing.FreshenFreeVariables(*args, **kwargs)

Bases: PatternWalker

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

implication_with_free_variables

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

walk

implication_with_free_variables(implication)
type = typing.Any
class neurolang.datalog.expression_processing.HeadConstantToBodyEquality(*args, **kwargs)

Bases: PatternWalker

Transform rules whose head (consequent) predicate contains constant terms into an equivalent rule where the head predicate only contains variable terms and the body (antecedent) of the rule contains variable-to-constant equalities. A fresh variable is generated for each constant term in the head predicate.

Examples

The rule Q(x, 2) :- P(x, y) is transformed into the rule Q(x, _f_) :- P(x, y), _f_ = 2, where _f_ is a fresh variable.

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

implication_with_constant_term_in_head

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

walk

implication_with_constant_term_in_head(implication)
type = typing.Any
class neurolang.datalog.expression_processing.HeadRepeatedVariableToBodyEquality(*args, **kwargs)

Bases: PatternWalker

Transform rules whose head (consequent) predicate contains repeated variables (symbols) to an equivalent rule without repeated variables and corresponding variable equalities. New fresh variables are generated to replace variable repetitions.

Examples

The rule Q(x, x) :- P(x, y) is transformed into the rule Q(x, _f_) :- P(x, y), _f_ = x, where _f_ is a fresh variable.

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

implication_with_repeated_variable_in_head

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

walk

implication_with_repeated_variable_in_head(implication)
type = typing.Any
class neurolang.datalog.expression_processing.RemoveDuplicatedAntecedentPredicates(*args, **kwargs)

Bases: PatternWalker

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

implication_with_duplicated_antecedent_predicate

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

walk

implication_with_duplicated_antecedent_predicate(implication)
type = typing.Any
class neurolang.datalog.expression_processing.TranslateToDatalogSemantics(*args, **kwargs)

Bases: TranslateToLogic, ExpressionWalker

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

build_conjunction

build_conjunction_from_expression_block

build_disjunction

build_negation

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

process_expression

process_iterable_argument

translate_logic_operator

translate_true_implication

walk

type = typing.Any
class neurolang.datalog.expression_processing.UnifyVariableEqualities(*args, **kwargs)

Bases: UnifyVariableEqualitiesMixin, ExpressionWalker

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

add_equality_with_constant

add_equality_with_symbol

build_substitutions_from_equalities

extract_and_unify_var_eqs_in_implication

extract_variable_equalities

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

process_expression

process_iterable_argument

walk

type = typing.Any
class neurolang.datalog.expression_processing.UnifyVariableEqualitiesMixin(*args, **kwargs)

Bases: PatternWalker

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

add_equality_with_constant

add_equality_with_symbol

build_substitutions_from_equalities

extract_and_unify_var_eqs_in_implication

extract_variable_equalities

pattern_match_expression

pattern_match_expression_parameters

pattern_match_expression_tuple

pattern_match_tuple

walk

static add_equality_with_constant(eq_sets, symb, const)
static add_equality_with_symbol(eq_sets, first, second)
static build_substitutions_from_equalities(eq_sets)
extract_and_unify_var_eqs_in_implication(implication)
static extract_variable_equalities(expression)
type = typing.Any
neurolang.datalog.expression_processing.all_body_preds_in_set(implication, predicate_set)

Checks wether all predicates in the antecedent are in the functor_set or are the consequent functor.

Parameters:
implication :

Implication

predicate_set :

set or functors of the consequent

Returns:
bool

True is all predicates in the antecedent are in the prediacte_set

neurolang.datalog.expression_processing.any_body_preds_in_set(implication, predicate_set)

Checks wether any predicates in the antecedent are in the predicate_set.

Parameters:
implication :

Implication

predicate_set :

set of functors

Returns:
bool

True is any predicates in the antecedent are in the predicate_set

neurolang.datalog.expression_processing.conjunct_formulas(f1, f2)

Conjunct two logical formulas.

neurolang.datalog.expression_processing.conjunct_if_needed(formulas)

Only conjunct the given list of formulas if there is more than one. If an empty list of formulas is passed, Constant[bool](True) is returned instead.

neurolang.datalog.expression_processing.dependency_matrix(datalog, rules=None, instance=None)

Produces the dependecy matrix for a datalog’s instance intensional database (IDB).

Parameters:
datalogDatalogProgram

datalog instance containing the EDB and IDB.

rulesNone or Union of rules

an optional subset of rules from the datalog program’s IDB.

instance: Instance

an optional instance, meant to represent the current DatalogProgram resolution, and whose’s symbols won’t raise a SymbolNotFoundError if found as a dependency in the given rules.

Returns:
idb_symbols: list

A list of IDB symbols in the dependency matrix.

dependency_matrix: ndarray

The dependency matrix where row is the origin symbol and column is the dependency. It is the adjacency matrix of the graph where each node is a predicate of the IDB.

Raises:
SymbolNotFoundError

If there is a predicate in the antecedent of a rule which is not a constant or an extensional/intensional predicate.

neurolang.datalog.expression_processing.enforce_conjunctive_antecedent(implication)
neurolang.datalog.expression_processing.extract_logic_atoms(expression)

Extract atoms from expression knowing that it’s in Datalog format

Parameters:
expressionExpression

expression to extract atoms from

Returns:
OrderedSet

set of all predicates in the atoms in lexicographical order.

neurolang.datalog.expression_processing.extract_logic_free_variables(expression)

Extract variables from expression assuming it’s in datalog format.

Parameters:
expressionExpression
Returns:
OrderedSet

set of all free variables in the expression.

neurolang.datalog.expression_processing.extract_logic_predicates(expression)

Extract predicates from expression knowing that it’s in Datalog format

Parameters:
expressionExpression

expression to extract predicates from

Returns:
OrderedSet

set of all predicates in the expression in lexicographical order.

neurolang.datalog.expression_processing.flatten_query(query, program)

Construct the conjunction corresponding to a query on a program.

TODO: currently this only handles programs without conjunctions.

Parameters:
querypredicate or conjunction of predicates

The query for which the conjunction is constructed.

programa program with an intensional database

Program with logical rules that will be used to construct the conjunction corresponding to the given query.

Returns:
disjunction or conjunction of predicates
neurolang.datalog.expression_processing.implication_has_existential_variable_in_antecedent(implication)

Whether an implication has at least one existential variable in its antecedent.

neurolang.datalog.expression_processing.is_aggregation_predicate(predicate)
neurolang.datalog.expression_processing.is_aggregation_rule(rule)
neurolang.datalog.expression_processing.is_conjunctive_expression(expression)
neurolang.datalog.expression_processing.is_conjunctive_expression_with_nested_predicates(expression)
neurolang.datalog.expression_processing.is_ground_predicate(predicate)

Whether all the predicate’s terms are all constant.

neurolang.datalog.expression_processing.is_linear_rule(rule)

Check if a rule is linear

Parameters:
ruleImplication

rule to analyse

Returns:
bool

True if the rule is linear

neurolang.datalog.expression_processing.is_rule_with_builtin(rule, known_builtins=None)
neurolang.datalog.expression_processing.is_symb_to_const_equality(formula)
neurolang.datalog.expression_processing.is_to_be_sorted_equality(formula)
neurolang.datalog.expression_processing.is_var_equality_to_var_or_const(formula)
neurolang.datalog.expression_processing.maybe_deconjunct_single_pred(expression)

Remove the conjunction from single-conjunct conjunctions.

The conjunction remains unchanged if it has multiple conjuncts or if it already is a single predicate.

neurolang.datalog.expression_processing.maybe_disjunct(formulas: Iterable[Expression]) Expression | Disjunction
neurolang.datalog.expression_processing.program_has_loops(program_representation)
neurolang.datalog.expression_processing.reachable_code(query, datalog)

Produces the code reachable by a query

Parameters:
queryImplication

Rule to figure out the reachable program from

datalogDatalogProgram

datalog instance containing the EDB and IDB.

Returns:
ExpressionBlock

Code needed to solve the query.

neurolang.datalog.expression_processing.remove_conjunction_duplicates(conjunction)
neurolang.datalog.expression_processing.stratify(union, datalog_instance)
Given an expression block containing Implication instances

and a datalog instance, return the stratification of the formulas in the block as a list of lists..

Parameters:
unionUnion

union of implications to be stratified.

datalog_instanceDatalogProgram

Datalog instance containing the EDB and IDB databases

Returns:
list of lists of Implications, boolean

Strata and wether it was stratisfiable. If it was not, all non-stratified predicates will be in the last strata.

neurolang.datalog.expression_processing.stratify_obtain_facts_stratum(to_process, seen)
neurolang.datalog.expression_processing.stratify_obtain_new_stratum(to_process, seen)