✓
Passing This code compiles and runs correctly.
Code
// TEST: Multiple exec calls in a single transaction
//
// exec() consumes [!started|!active] and produces <active!>
// This means you can chain multiple execs - each one consumes
// the previous active state and produces a new one.
//
// EXPECTED: Compiles and runs successfully
import app/db
app/db:connect(host: "localhost")
| ok c |> app/db:begin(conn: c)
| ok t |> app/db:exec(tx: t, sql: "INSERT INTO users VALUES (1, 'alice')")
| ok r1 |> app/db:exec(tx: r1, sql: "INSERT INTO users VALUES (2, 'bob')")
| ok r2 |> app/db:exec(tx: r2, sql: "INSERT INTO users VALUES (3, 'charlie')")
| ok r3 |> app/db:commit(tx: r3)
| ok conn |> app/db:close(conn)
| ok |> _
| err _ |> _
| err _ |> _
| err _ |> _
| err _ |> _
| err _ |> _
| err _ |> _
| err _ |> _
Actual
Executing: INSERT INTO users VALUES (1, 'alice')
Executing: INSERT INTO users VALUES (2, 'bob')
Executing: INSERT INTO users VALUES (3, 'charlie')
COMMIT
Connection closed
Expected output
Executing: INSERT INTO users VALUES (1, 'alice')
Executing: INSERT INTO users VALUES (2, 'bob')
Executing: INSERT INTO users VALUES (3, 'charlie')
COMMIT
Connection closed
Flows
flow ~connect click a branch to expand · @labels scroll to their anchor
connect (host: "localhost")
Imported Files
// Database module demonstrating phantom obligation semantics
//
// State machine:
// connect() → Connection<connected!> -- must use connection
// begin() → Transaction<started!> -- consumes conn, must exec
// exec() → Transaction<active!> -- now can commit/rollback
// commit() → Connection<active!> -- returns conn, can reuse or close
// rollback() → Connection<active!> -- returns conn, can reuse or close
// close() → void -- only accepts <!active>, not <!connected>
//
// Key insight: close() requires <!active>, which only comes from commit/rollback.
// This FORCES you to use the connection meaningfully before closing.
const std = @import("std");
const Connection = struct { handle: i32 };
const Transaction = struct { conn_handle: i32 };
// connect: creates connection with <connected!> obligation
~pub tor connect { host: string }
| ok *Connection<connected!>
| err string
~proc connect|zig {
const c = std.heap.page_allocator.create(Connection) catch unreachable;
c.* = Connection{ .handle = 42 };
return .{ .ok = c };
}
// begin: consumes <!connected> OR <!active>, produces Transaction<started!>
~pub tor begin { conn: *Connection<!connected|!active> }
| ok *Transaction<started!>
| err string
~proc begin|zig {
const tx = std.heap.page_allocator.create(Transaction) catch unreachable;
tx.* = Transaction{ .conn_handle = conn.handle };
return .{ .ok = tx };
}
// exec: consumes <!started> OR <!active>, produces Transaction<active!>
~pub tor exec { tx: *Transaction<!started|!active>, sql: string }
| ok *Transaction<active!>
| err string
~proc exec|zig {
std.debug.print("Executing: {s}\n", .{sql});
return .{ .ok = tx };
}
// commit: consumes <!active>, returns Connection<active!>
~pub tor commit { tx: *Transaction<!active> }
| ok *Connection<active!>
| err string
~proc commit|zig {
std.debug.print("COMMIT\n", .{});
const c = std.heap.page_allocator.create(Connection) catch unreachable;
c.* = Connection{ .handle = tx.conn_handle };
std.heap.page_allocator.destroy(tx);
return .{ .ok = c };
}
// rollback: consumes <!active>, returns Connection<active!>
~pub tor rollback { tx: *Transaction<!active> }
| ok *Connection<active!>
| err string
~proc rollback|zig {
std.debug.print("ROLLBACK\n", .{});
const c = std.heap.page_allocator.create(Connection) catch unreachable;
c.* = Connection{ .handle = tx.conn_handle };
std.heap.page_allocator.destroy(tx);
return .{ .ok = c };
}
// close: ONLY accepts <!active> - forces meaningful use before close
~pub tor close { conn: *Connection<!active> }
| ok
| err string
~proc close|zig {
std.debug.print("Connection closed\n", .{});
std.heap.page_allocator.destroy(conn);
return .{ .ok = .{} };
}
Test Configuration
MUST_RUN