[ARCHIVED] Experimental Rust DSL for composing runtime actions with type-level context.
README.md

type_context #

type_context is an experimental Rust DSL for composing runtime actions with type-level context.

The library is not intended for production use. It depends on nightly Rust features and exists mainly as a small experiment with macro_rules!, associated types, const generics, and type-level state.

This repository is archived and not actively maintained.

What this is #

ctx! builds an Action. An action can:

  • return runtime values with pure
  • bind the result of another action
  • store, read, and remove values from a type-level context
  • branch on type-level conditions
  • perform effects and handle them with typed handlers

In short, this is a DSL for writing runtime actions that carry type-level state.

Example #

use type_context::action::Action;
use type_context::ctx;

struct Id<const N: usize>;
type Var1 = (Id<1>, u32);
type Var2 = (Id<2>, u32);
type Var3 = (Id<3>, u32);

fn main() {
    let action = ctx! {
        set Var1 = 6;
        set Var2 = 7;
        set Var3 = a * b where a <- get Var1, b <- get Var2;
        get Var3
    };

    assert_eq!(action.run(), 42);
}

Var1, Var2, and Var3 are types used as variable names. Their values are stored in the context, so later actions can read them through get.

Syntax #

Block syntax #

Syntax Meaning
ACTION Return the action result.
ACTION; ... Run an action, then continue with the next action.
name <- ACTION; ... Run an action and bind its result to name.
_ <- ACTION; ... Run an action and ignore its result.
new_name <- move old_name; ... Move a runtime value out of the context.
let name = EXPR; ... Bind a runtime value.
const NAME: TYPE = EXPR; ... Declare a constant inside the block.
type Name = TYPE; ... Declare a type alias inside the block.

Action syntax #

Syntax Meaning
pure EXPR Return EXPR as an action result.
get VAR Read VAR from the type-level context. Missing variables fail at compile time.
set VAR = EXPR Store EXPR as VAR in the type-level context.
set VAR = EXPR where ... Store a value computed from outer parameters or existing context values.
unset VAR Remove VAR from the type-level context.
if [COND] { ... } else { ... } Branch using a type-level condition.
perform EFFECT Perform an effect that can be handled by a typed handler.
panic "MESSAGE" Trigger a compile-time panic when the action is instantiated.

Variables #

Variables are represented by types that implement Variable. Tuples already implement it, so a common pattern is to use (Name, Type):

use type_context::action::Action;
use type_context::ctx;

struct Count;
type CountVar = (Count, usize);

fn count() -> impl Action<Output = usize> {
    ctx! {
        set CountVar = 40usize;
        set CountVar = value + 2 where value <- get CountVar;
        get CountVar
    }
}

Effects #

The library also has a small typed effect system. An effect declares its output type, and a handler decides how to resume or replace the continuation.

use type_context::action::Action;
use type_context::context::ConstContext;
use type_context::ctx;
use type_context::effect::handler::TailResumptiveHandler;
use type_context::effect::Effect;

struct AddTen(usize);

impl Effect for AddTen {
    type Output = usize;
}

impl TailResumptiveHandler for AddTen {
    type Effect = AddTen;
    type Context<Ctx: ConstContext> = Ctx;

    fn handle<Ctx: ConstContext>(AddTen(value): AddTen) -> usize {
        value + 10
    }
}

fn main() {
    let action = ctx! {
        value <- perform AddTen(32);
        pure value
    };

    assert_eq!(action.handle::<AddTen>().run(), 42);
}

Limitations #

  • This requires nightly Rust.
  • This is an experiment, not a production-ready crate.
  • The macro syntax is intentionally narrow and can produce hard-to-read compiler errors.
  • Runtime bindings cannot be mutable.
  • Runtime bindings cannot borrow from other runtime bindings inside the ctx! block.

Running tests #

cargo test

License #

CC0-1.0