Class FormulaFactory

Namespace
LogicalOptimizer
Assembly
LogicalOptimizer.Core.dll

LogicNG-style formula construction: n-ary And/Or with automatic flattening, duplicate-operand removal, constant folding, complement folding (x and !x collapse the connective), canonical operand ordering and structural interning — equal formulas built through the factory are the SAME instance, so reference equality works as structural equality and repeated subformulas share memory. Because operands are sorted with a stable canonical key at construction time, structural (order-sensitive) equality is effectively commutative for factory-built trees. The intern table is a ConcurrentDictionary<TKey, TValue>, so a factory instance may be shared across threads.

public sealed class FormulaFactory
Inheritance
FormulaFactory
Inherited Members

Properties

False

Constant false (interned).

public AstNode False { get; }

Property Value

AstNode

True

Constant true (interned).

public AstNode True { get; }

Property Value

AstNode

Methods

And(params AstNode[])

N-ary conjunction: nested ANDs are flattened, duplicates removed, constants folded (0 annihilates, 1 disappears), complementary operands collapse to 0, and the surviving operands are sorted into canonical order.

public AstNode And(params AstNode[] operands)

Parameters

operands AstNode[]

Returns

AstNode

And(IEnumerable<AstNode>)

N-ary conjunction: nested ANDs are flattened, duplicates removed, constants folded (0 annihilates, 1 disappears), complementary operands collapse to 0, and the surviving operands are sorted into canonical order.

public AstNode And(IEnumerable<AstNode> operands)

Parameters

operands IEnumerable<AstNode>

Returns

AstNode

Equivalence(AstNode, AstNode)

Equivalence a ↔ b constructed as (a & b) | (!a & !b).

public AstNode Equivalence(AstNode left, AstNode right)

Parameters

left AstNode
right AstNode

Returns

AstNode

Implication(AstNode, AstNode)

Implication a → b constructed as !a | b.

public AstNode Implication(AstNode left, AstNode right)

Parameters

left AstNode
right AstNode

Returns

AstNode

Import(AstNode)

Rebuild an existing tree through the factory: the result carries every construction-time canonicalization and is interned. Derived operators (Xor/Eqv/Imp/Nand/Nor) are decomposed into And/Or/Not.

public AstNode Import(AstNode node)

Parameters

node AstNode

Returns

AstNode

Not(AstNode)

Negation with double-negation and constant folding.

public AstNode Not(AstNode operand)

Parameters

operand AstNode

Returns

AstNode

Or(params AstNode[])

N-ary disjunction: nested ORs are flattened, duplicates removed, constants folded (1 annihilates, 0 disappears), complementary operands collapse to 1, and the surviving operands are sorted into canonical order.

public AstNode Or(params AstNode[] operands)

Parameters

operands AstNode[]

Returns

AstNode

Or(IEnumerable<AstNode>)

N-ary disjunction: nested ORs are flattened, duplicates removed, constants folded (1 annihilates, 0 disappears), complementary operands collapse to 1, and the surviving operands are sorted into canonical order.

public AstNode Or(IEnumerable<AstNode> operands)

Parameters

operands IEnumerable<AstNode>

Returns

AstNode

Parse(string)

Parse text through the standard grammar, building canonical nodes via this factory. Throws FormulaParseException (a subclass of ArgumentException) on invalid input; use TryParse(string, out AstNode?, out ParseDiagnostic?) to avoid the exception.

public AstNode Parse(string expression)

Parameters

expression string

Returns

AstNode

TryParse(string, out AstNode?, out ParseDiagnostic?)

Attempts to parse expression. On success returns true with the canonical tree in formula; on invalid input returns false with a structured diagnostic (position, length, expected tokens, machine-readable code and a caret snippet) instead of throwing. A null expression is still a contract violation and throws ArgumentNullException.

public bool TryParse(string expression, out AstNode? formula, out ParseDiagnostic? diagnostic)

Parameters

expression string
formula AstNode
diagnostic ParseDiagnostic

Returns

bool

Variable(string)

Interned variable.

public AstNode Variable(string name)

Parameters

name string

Returns

AstNode

Xor(AstNode, AstNode)

Exclusive or constructed as (a & !b) | (!a & b).

public AstNode Xor(AstNode left, AstNode right)

Parameters

left AstNode
right AstNode

Returns

AstNode