Concepts

Compiler Contract and Plan IR

The gap between static text and dynamic execution is bridged by the Plan Intermediate Representation.

The Compiler Contract

In many interpreted languages, the AST is executed directly (tree-walking). CNL-PL takes a different approach: it compiles the AST into a flat, optimized IR (Intermediate Representation) known as the Plan.

The Compiler Contract guarantees determinism. It states that for any given AST and any given dictionary state, the compiler will produce exactly the same sequence of Plan instructions. This separation enables the "Write Once, Run Anywhere" philosophy for pragmatics: the same compiled Plan can be sent to a local in-memory engine, a remote SQL backend, or a specialized SMT solver.

The Plan Types

The Plan IR is typed. It does not just say "Execute X"; it specifies what kind of result X produces. This prevents runtime type errors and allows for aggressive optimization.

SetPlan

Produces a Bitset of entities. Used for queries like "Find all users". Operations include Intersection (AND), Union (OR), and Difference (NOT).

BoolPlan

Produces a single Boolean (true/false). Used for "Verify" commands and rule conditions. It efficiently checks if a SetPlan result is empty or not.

RelationPlan

Produces a RelationMatrix (rows of bitsets). Used for complex joins, like "Find users who manage servers that have high load".

NumberPlan

Produces a Numeric value. Used for aggregations ("Count of users") or attribute lookups ("Average salary").

ActionPlan and RulePlan

Beyond simple queries, the system compiles behaviors. An ActionPlan is the compiled form of an Action Block. It contains a Precondition (a BoolPlan tree) that determines if the action is valid, and a set of Effect Deltas that describe how to mutate the KB if the action succeeds.

Similarly, a RulePlan is a specialized structure for deduction. It contains a Body (the "IF" part, compiled to a Plan) and a Head (the "THEN" part). The execution engine runs these RulePlans in a loop (fixpoint iteration) to derive all possible consequences of the current state.