Skip to main content

Extend the language without editing it

This context, the type that stands for one interpreter and holds its choices, evaluates a larger language, with subtraction and negation added to addition and multiplication, and no existing provider is edited to do it. It is the last example in expression, an interpreter from the cgp-examples repository, built with Context-Generic Programming (CGP), which enables pluggable trait implementations that a new language reuses at compile time. It is the pay-off of the design the earlier examples set up: the language grows by adding types and providers next to the old ones.

tip

New to CGP?​

add_mult introduces the interpreter and its wiring, which this page extends. For CGP itself, the Hello World tutorial introduces contexts and wiring, and Extensible variants explains how one provider can serve several enums.

The problem​

The task is to add subtraction and negation to the language, and to evaluate the larger language, without editing the base language's types or any of its existing providers. This is the half of the expression problem that a plain Rust enum makes hardest: adding a new kind of expression.

Without CGP​

With an enum and a match, adding Minus and Negate means adding two variants to Expr and an arm to every function that matches on it, which only the crate that owns Expr can do. The usual workaround for a downstream crate is a new enum that wraps the old one, with a variant for the old language and one for each new operator. It breaks as soon as the operators mix: the old Plus holds operands of the old Expr, so a sum whose operand is a negation cannot be written, and making it possible means copying every old operator into the new enum along with the code that handles it. This page adds the operators so that every existing provider works for the new language unchanged.

Run it​

From the root of the cgp-examples repository:

cargo test -p cgp-example-expression add_mult_neg::

The context's test passes:

test contexts::add_mult_neg::test::test_add_mult_neg ... ok

It evaluates 2 + 3 to 5, 2 * 3 to 6, 2 - 3 to -1, and -2 * (3 + 4) to -14.

A new enum, from old parts and new ones​

The context's module, add_mult_neg.rs, defines the larger language as a new enum:

pub type Value = i64;

#[derive(Debug, CgpData)]
pub enum MathPlusExpr {
Plus(Plus<MathPlusExpr>),
Times(Times<MathPlusExpr>),
Literal(Literal<Value>),
Negate(Negate<MathPlusExpr>),
Minus(Minus<MathPlusExpr>),
}

Plus, Times, and Literal are the same operator structs the base language uses, here instantiated at MathPlusExpr and with i64 literals, since the language now has negative numbers. Negate and Minus are two more operator structs. The base language's MathExpr is left as it was; the new enum sits beside it.

The new operators get providers of their own​

Each new operator needs an evaluation provider. EvalNegate, from the crate's providers/eval/negate.rs, negates its one operand:

#[cgp_impl(new EvalNegate)]
#[uses(CanComputeRef<Code, MathExpr, Output = Output>)]
impl<Code, MathExpr, Output> ComputerRef<Code, Negate<MathExpr>>
where
Output: Neg<Output = Output>,
{
type Output = Output;

fn compute_ref(
&self,
code: PhantomData<Code>,
Negate(expr): &Negate<MathExpr>,
) -> Self::Output {
-self.compute_ref(code, expr)
}
}

EvalSubtract does the same for Minus with - between its two operands. Like every provider in the crate, each asks the context to evaluate its operands and knows nothing about the enum they sit in. They could be written in another crate from the one that defines the base language.

The context mixes old providers and new​

InterpreterPlus is a new context, with a table that wires the old providers for the old operators and the new providers for the new ones:

pub struct InterpreterPlus;

delegate_components! {
InterpreterPlus {
open ComputerRefComponent;

@ComputerRefComponent.Eval.MathPlusExpr: DispatchEval,
@ComputerRefComponent.Eval.Plus<MathPlusExpr>: EvalAdd,
@ComputerRefComponent.Eval.Times<MathPlusExpr>: EvalMultiply,
@ComputerRefComponent.Eval.Literal<Value>: EvalLiteral,
@ComputerRefComponent.Eval.Minus<MathPlusExpr>: EvalSubtract,
@ComputerRefComponent.Eval.Negate<MathPlusExpr>: EvalNegate,
}
}

EvalAdd, EvalMultiply, and EvalLiteral are the providers add_mult wires, unchanged, now at a different enum and a different number type. The table is keyed on the Eval marker and the input, as in add_mult_code, and the context has its own DispatchEval wrapper for MathPlusExpr, for the reason add_mult gives.

Conversion is left unwired​

InterpreterPlus wires evaluation only. There is no ToLisp entry, and no abstract Lisp type, and the context compiles and runs regardless: CGP checks wiring where it is used, so an operation that nothing calls needs no providers. The context's check_components! block lists evaluation for all six input types, and nothing for conversion.

Try a change​

Ask for conversion anyway. Add ToLisp to the module's dsl import, and add an entry to its check block:

(ToLisp, MathPlusExpr),

Run cargo cgp check, and the check reports that conversion was never wired:

error[E0277]: [CGP-E001] missing wiring for `@ComputerRefComponent.ToLisp.MathPlusExpr` on `InterpreterPlus`
= note: root cause: [CGP-E113] missing wiring for `@ComputerRefComponent.ToLisp.MathPlusExpr` on `InterpreterPlus`

Supporting it would be a second group of ToLisp entries beside the Eval ones, with a provider for each new operator, an entry binding the context's Lisp type, and a DispatchToLisp wrapper, as add_mult has. cargo cgp check leads with the root cause for the classes it recognizes, and the tool does not yet reshape every class.

The pattern​

This context shows the expression problem solved: a language extended with new variants while every existing operation's providers stay as they were. The old operators' providers serve the new enum because they never named an enum; the new operators get providers of their own; and a new context wires the two together. Adding a new operation instead would be the same move in the other direction, a new set of providers and a new group of entries. Extensible variants explains the representation that allows it.

The cost is that the extension is a new language, not a change to the old one. MathPlusExpr is a separate enum and InterpreterPlus a separate context, and code that handles MathExpr does not accept MathPlusExpr until it is wired for it. The flexibility is in the providers, which are reused, rather than in the enums, which stay ordinary closed Rust enums.

Where to go next​


An AI agent wrote this page using the CGP knowledge base. Its content was verified against the project's source. See How AI is used in this project.