JLNN Compiler¶
JLNN Symbolic Compiler
This module provides the infrastructure to compile symbolic logical formulas into neural computational graphs using Flax NNX and JAX. It leverages the Lark parser to transform strings into a tree of NNX Modules.
- class jlnn.symbolic.compiler.BinaryGateNode(*args: Any, **kwargs: Any)[source]¶
Bases:
NodeRepresents a gate with exactly 2 inputs, specifically designed for asymmetric operations like Implication (A -> B).
- class jlnn.symbolic.compiler.JLNNCompiler(rngs: Rngs)[source]¶
Bases:
TransformerLark Transformer that converts a CST (Concrete Syntax Tree) into an NNX model tree.
Note
The grammar rules use
and,or, andnot. Since these are reserved keywords in Python, we define them asand_,or_,not_and alias them dynamically after class definition.- and(children: List[Node]) NAryGateNode¶
Constructs a WeightedAnd gate node.
- and_(children: List[Node]) NAryGateNode[source]¶
Constructs a WeightedAnd gate node.
- implication(children: List[Node]) BinaryGateNode[source]¶
Constructs a WeightedImplication gate node (A -> B).
- not(children: List[Node]) UnaryGateNode¶
Constructs a WeightedNot gate node.
- not_(children: List[Node]) UnaryGateNode[source]¶
Constructs a WeightedNot gate node.
- or(children: List[Node]) NAryGateNode¶
Constructs a WeightedOr gate node.
- or_(children: List[Node]) NAryGateNode[source]¶
Constructs a WeightedOr gate node.
- variable(tokens: List[Token]) PredicateNode[source]¶
Transforms a variable token into a PredicateNode, ensuring weight sharing if name repeats.
- class jlnn.symbolic.compiler.LNNFormula(*args: Any, **kwargs: Any)[source]¶
Bases:
ModuleThe main LNN Formula wrapper. Compiles a logical string formula into a differentiable NNX module.
- root¶
The root node of the neural computational graph.
- predicates¶
An nnx.Dict containing all trainable predicate groundings.
Example
>>> rngs = nnx.Rngs(0) >>> model = LNNFormula("A & B -> C", rngs) >>> inputs = {"A": jnp.array([[0.9]]), "B": jnp.array([[0.8]]), "C": jnp.array([[0.1]])} >>> output = model(inputs) # Returns truth interval [Lower, Upper]
- class jlnn.symbolic.compiler.NAryGateNode(*args: Any, **kwargs: Any)[source]¶
Bases:
NodeRepresents a logic gate with N inputs, such as weighted AND, OR or XOR.
- class jlnn.symbolic.compiler.Node(*args: Any, **kwargs: Any)[source]¶
Bases:
ModuleAbstract base class for all nodes in the JLNN computational graph.
Each node represents a logical operation or a predicate and must implement a forward pass that operates on JAX arrays.
- class jlnn.symbolic.compiler.PredicateNode(*args: Any, **kwargs: Any)[source]¶
Bases:
NodeRepresents a leaf node (variable) mapping to a LearnedPredicate.
This node acts as the grounding layer where raw numeric data is transformed into a fuzzy truth value.
- class jlnn.symbolic.compiler.UnaryGateNode(*args: Any, **kwargs: Any)[source]¶
Bases:
NodeRepresents a gate with a single input, such as NOT.
The compiler recursively traverses the syntax tree from the parser and creates corresponding NNX modules for each node. The result is a hierarchical model that can be trained directly.
Key Components:¶
Node: Abstract base for all graph nodes. Ensures recursive
forwardcalls.PredicateNode: Represents input variables (leaves of the graph).
NAryGateNode: Zapouzdřuje n-ární hradla (AND, OR, XOR).
Main class that unifies the process from a string formula to an executable model.