deeplog.formula.lowering.compiler
The compiler: formulas in, one module out.
Classes
Compiles formulas to one module, with the domains, algebras and builders to use. |
Module Contents
- class Compiler(
- variables: Mapping[Symbol, Domain] | None = None,
- reification: AlgebraicStructure = BOOLEAN,
- structures: Mapping[str, AlgebraicStructure] | None = None,
- aggregation_builders: Mapping[str, AggregationBuilder] | None = None,
- atom_builders: Mapping[tuple[str, int, str], AtomBuilder] | None = None,
- transformation_builders: Mapping[tuple[str, str], TransformationBuilder] | None = None,
Compiles formulas to one module, with the domains, algebras and builders to use.
variablesdeclares the domain each binder ranges over; a binder it does not declare is a reification variable, ranging over the values ofreification.structuresnames the algebras a formula may use beside the registered ones, and wins over them.atom_builders,aggregation_buildersandtransformation_buildersadd builders, keyed by predicate, operation and pair of algebras, and each wins over the default registered for its key. An aggregation whose operation has no builder is computed byreduction().The value test
=/2is the compiler’s own: it reads a variable’s value as its position in the variable’s domain (domain()), which only the compiler knows.An atom no builder computes is an input of the compiled module, named by the atom. Where it reads variables an aggregation binds, it is its ground atoms instead, an input each, named by substituting those variables’ values, and read at the values the aggregation gives them; such a variable must range over named values.
Every argument of an atom has a sort, the domain its predicate declares for it (
domains_of()). A variable ranges over its domain invariables, or else over the named or tensor domain of the arguments it is that each of the others includes; each must include the variable’s domain, and a variable over part of a named one reaches its predicate as a position in it. An argument of sort the values takes a variable over values without names. A variable that no argument gives a domain ranges over its declared one, or, if it is bound and only=tests it, over the values ofreification. A variable an aggregation binds is that aggregation’s own, so two aggregations binding one name may give it different domains; a free variable is one variable in every formula compiled together (Sorts).Configure the compiler with the domains, algebras and builders to use.
- reification
- compile(
- formulas: Mapping[str | Symbol, FormulaNode],
- /,
- compile(
- *formulas: FormulaNode,
Compile
formulasto one module with a column per formula, in order.Given as one mapping, each formula’s column is named by its key, a symbol or the text of one; given as formulas, the
i-th column is named@i. A name is labelled with the algebra of its column, asrain_implies_wet _ probability, and equal formulas compile once, whatever their names.Each formula is constructed bottom-up by folding it through a
CircuitFactory, which rewrites every region a circuit can hold into a lump, and the results are lowered together, top-down (Lowering). Equal subformulas of all the formulas are first made one object, and construction runs under one memo, so a subformula is built once however many formulas write it, their shared atoms are one circuit leaf, and counts over one boolean circuit take one knowledge compilation. The atoms whose builder’s module reads none of their variables are computed before any aggregation, by one module per predicate, so a network fed no variable runs once per input, however many aggregations read it and however deep.- Raises:
TypeError – If a mapping is given beside formulas.
ValueError – If two formulas are given one name, or a name is labelled with an algebra its formula’s value is not in.
- domain(binder: Symbol) Domain[source]
The domain
binderranges over.A binder the compiler declares in its
variablesuses that domain. Any other binder is a reification variable, and inherits the values of the compiler’sreificationalgebra as its domain.- Raises:
ValueError – If the binder is undeclared and the reification algebra declares no enumerable values.
- algebra(name: str) AlgebraicStructure[source]
The algebra named
name: the compiler’s own, else the registered one.- Raises:
ValueError – If
nameis neither the compiler’s own nor registered.
- atom_builder( ) AtomBuilder | None[source]
The builder of the
functor/arityatoms instructure, orNone.
- declaring(
- variables: Mapping[Symbol, Domain] | None = None,
- atom_builders: Mapping[tuple[str, int, str], AtomBuilder] | None = None,
A copy of this compiler that also declares
variablesandatom_builders.For a front end that introduces binders and predicates of its own, whose caller cannot know them. Its builders shadow the compiler’s defaults, such as
p/2in probability for a program’s ownp/1extended with a binder. This compiler is left as it is.- Raises:
ValueError – If a binder is already declared with another domain, or this compiler was given a builder for one of the keys.