// ── calculator ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: calculator.es topic: choice status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo // ═════════════════════════════════════════════════════════════════════════════ // A tiny arithmetic calculator, end to end. // // It turns a string like "1 + 2 * 3" into a number — and gets 7, not 9, because it // respects operator precedence (multiplication binds tighter than addition). It is // written the way a real E# program is: a recursive-descent parser that builds an // abstract syntax tree (AST), then a recursive evaluator that walks it. // // If this is the first E# you have ever read, here is the whole tour in one file: // // • `ref choice` — a tagged union (a "sum type"). The AST is one of three shapes: // a number, an addition, or a multiplication. Because it is a // *ref* choice it is a reference type, so a variant may hold // another `Expr` and the tree can nest to any depth. // • `data` + `*P` — a small mutable parser-state struct. `data` is a value type // (copying it copies the bits), so to let every parse step share // and advance ONE cursor we put it on the heap with `new`, which // hands back a pointer, written `*P`. // • `new` — the heap-allocation operator: `new P { ... }` allocates a value // `data` on the heap and yields a `*P`. It is the only way to mint // a fresh pointer. // • `Result` — error handling without exceptions. A parse step returns either // `ok(tree)` or `error("message")`. The `?` after a call means // "if that was an error, stop and return the same error from here", // so the happy path stays flat and readable. // • `match` — exhaustive, binding pattern dispatch over a choice's variants. // ═════════════════════════════════════════════════════════════════════════════ // The AST. Each variant carries its own payload. `add` and `mul` hold two more // `Expr`s — legal because `ref choice` is a reference type, so the tree nests freely. ref choice Expr { num(value: int) // a literal integer, e.g. 42 add(left: Expr, right: Expr) // left + right mul(left: Expr, right: Expr) // left * right } // The parser's mutable state: the input text plus a cursor into it. It is a value // type (`data`), but we will hand around a *pointer* (`*P`) so every parse step // advances the same shared `pos` instead of mutating a private copy. data P { var src: string var pos: int } // --- small cursor helpers ----------------------------------------------------- // Each takes `*P` (a pointer). A pointer parameter is NOT promoted to a method, so // these stay plain free functions you call as `atEnd(p)` — only a direct `data` // receiver (e.g. `func f(v: Vec)`) would become `v.f()`. // True once the cursor has consumed the whole string. func atEnd(p: *P) -> bool { return p.pos >= p.src.Length } // Advance past spaces so the grammar can ignore whitespace. func skipWs(p: *P) { while !atEnd(p) { if p.src[p.pos] == ' ' { p.pos += 1 } else { return } } } // --- the grammar, highest-precedence first ------------------------------------ // // expr = term ('+' term)* (addition — lowest precedence) // term = factor ('*' factor)* (multiplication — binds tighter) // factor = number (a run of digits) // // Each rule returns `Result`: the parsed subtree, or an error message. // factor = number func parseFactor(p: *P) -> Result { skipWs(p) if atEnd(p) { return error("expected a number") } if !char.IsDigit(p.src[p.pos]) { return error("expected a digit") } let start = p.pos while !atEnd(p) && char.IsDigit(p.src[p.pos]) { p.pos += 1 } let text = p.src.Substring(start, p.pos - start) return ok(Expr.num(int.Parse(text))) } // term = factor ('*' factor)* — left-associative: fold each '*' into the tree. func parseTerm(p: *P) -> Result { var left = parseFactor(p)? // `?` unwraps ok, or returns the error from here while true { skipWs(p) if atEnd(p) || p.src[p.pos] != '*' { return ok(left) } p.pos += 1 let right = parseFactor(p)? left = Expr.mul(left, right) } return ok(left) } // expr = term ('+' term)* — same shape, one precedence level down. func parseExpr(p: *P) -> Result { var left = parseTerm(p)? while true { skipWs(p) if atEnd(p) || p.src[p.pos] != '+' { return ok(left) } p.pos += 1 let right = parseTerm(p)? left = Expr.add(left, right) } return ok(left) } // Entry point: put the cursor on the heap with `new`, parse a whole expression, and // confirm nothing is left over. func parse(src: string) -> Result { var p: *P = new P { src: src, pos: 0 } // `new` → heap value `data`, yields *P let tree = parseExpr(p)? skipWs(p) if !atEnd(p) { return error("trailing characters") } return ok(tree) } // --- evaluation --------------------------------------------------------------- // Walk the tree to an int. `match` dispatches on the variant and binds a "case view" // — `.add(n)` gives an `n` whose members are that variant's payloads, reached by their // declared names (`n.left`, `n.right`, `n.value`). Recursion follows the tree's shape. func eval(e: Expr) -> int { // `Expr` is a choice → not promoted → a free function match e { .num(n) { return n.value } .add(n) { return eval(n.left) + eval(n.right) } .mul(n) { return eval(n.left) * eval(n.right) } } return 0 } // Parse, then evaluate. Returns the answer, or -1 if the input did not parse — so the // precedence is visible in the result: 1 + 2 * 3 == 7, never 9. func calc(src: string) -> int { let r = parse(src) if r.IsError { return -1 } return eval(r.Value) } // The entry point is a `main` method on a `ref data Program` (the OO application shape, // the class-style alternative to a bare top-level `func main`). That method IS the entry // point; the compiler constructs the program (`Program()`) and calls `.main()`. ref data Program { func main() -> int { return calc("1 + 2 * 3") // 7 } } // ── FeatureMixingTests__Axis5_NestedChoiceInChoiceInResult_FullMatch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FeatureMixingTests.cs::Axis5_NestedChoiceInChoiceInResult_FullMatch topic: choice status: verified // verified behavior: Test.describe(...) == "err:nope" namespace Test choice Token { word(text: string) number(value: int) } choice Lex { one(t: Token) pair(a: Token, b: Token) } func parse(input: string) -> Result { if input == "fail" { return error("nope") } if input == "num" { return ok(Lex.one(Token.number(7))) } return ok(Lex.pair(Token.word("hi"), Token.number(9))) } func describe(input: string) -> string { let r = parse(input) match (r: Result) { .ok(lex) { match (lex: Lex) { .one(o) { match (o.t: Token) { .word(w) { return "one-word:{w.text}" } .number(n) { return "one-num:{n.value}" } } } .pair(p) { var first = "" match (p.a: Token) { .word(w) { first = "w:{w.text}" } .number(n) { first = "n:{n.value}" } } var second = "" match (p.b: Token) { .word(w) { second = "w:{w.text}" } .number(n) { second = "n:{n.value}" } } return "pair:{first},{second}" } } } .err(e) { return "err:{e}" } } return "?" } // ── FeatureMixingTests__Axis5_ResultOfChoice_PropagateThroughQuestion ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FeatureMixingTests.cs::Axis5_ResultOfChoice_PropagateThroughQuestion topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Reply { accepted(id: int) rejected(reason: string) } func fetch() -> Result = ok(Reply.accepted(7)) func process() -> Result { let r = fetch()? match (r: Reply) { .accepted(a) { return ok(a.id * 2) } .rejected(_) { return error("nope") } } return error("unreachable") } // ── FeatureMixingTests__MiniProgram_CliArgRouter ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FeatureMixingTests.cs::MiniProgram_CliArgRouter topic: choice status: verified // verified behavior: Test.run3(...) == "?nope" namespace Test choice Cmd { help add(a: int, b: int) greet(name: string) unknown(name: string) } func parseCmd(args: List) -> Cmd { if args.Count == 0 { return Cmd.help() } let head = args[0] if head == "help" { return Cmd.help() } if head == "add" { if args.Count < 3 { return Cmd.unknown("add: need 2 args") } return Cmd.add(int.Parse(args[1]), int.Parse(args[2])) } if head == "greet" { let n = args.Count >= 2 ? args[1] : "world" return Cmd.greet(n) } return Cmd.unknown(head) } func dispatch(args: List) -> string { let c = parseCmd(args) match (c: Cmd) { .help { return "usage: help|add|greet" } .add(a, b) { return "sum={a + b}" } .greet(name) { return "hi {name}" } .unknown(n) { return "?{n}" } } return "?" } func run1() -> string { var args = List() args.Add("add") args.Add("3") args.Add("4") return dispatch(args) } func run2() -> string { var args = List() args.Add("greet") return dispatch(args) } func run3() -> string { var args = List() args.Add("nope") return dispatch(args) } // ── FeatureMixingTests__MiniProgram_ConnectionLifecycleStateMachine ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FeatureMixingTests.cs::MiniProgram_ConnectionLifecycleStateMachine topic: choice status: verified // verified behavior: Test.run(...) == "connected:99|failed:timeout" namespace Test choice ConnState { disconnected connecting connected(sessionId: int) failed(reason: string) } data Client { var state: ConnState name: string } func startConnect(c: *Client) -> Result { match (c.state: ConnState) { .disconnected { c.state = ConnState.connecting() return ok(1) } .connecting { return error("already connecting") } .connected(sid) { return error("already connected") } .failed(reason) { return error("in failed: {reason}") } } return error("unreachable") } func markConnected(c: *Client, sid: int) { c.state = ConnState.connected(sid) } func markFailed(c: *Client, reason: string) { c.state = ConnState.failed(reason) } func describe(c: *Client) -> string { match (c.state: ConnState) { .disconnected { return "disconnected" } .connecting { return "connecting" } .connected(sid) { return "connected:{sid}" } .failed(reason) { return "failed:{reason}" } } return "?" } func run() -> string { var c: *Client = new Client { state: ConnState.disconnected(), name: "node-1" } let r1 = startConnect(c) markConnected(c, 99) let mid = describe(c) markFailed(c, "timeout") let end = describe(c) return "{mid}|{end}" } // ── FeatureMixingTests__MiniProgram_TinyJsonParser_NumbersAndStrings ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FeatureMixingTests.cs::MiniProgram_TinyJsonParser_NumbersAndStrings topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Token { integer(value: int) str(text: string) eof } data Lex { var src: string var pos: int } func skipSpace(l: *Lex) { while l.pos < l.src.Length { let c = l.src[l.pos] if c == ' ' || c == '\t' || c == '\n' { l.pos += 1 } else { return } } } func nextToken(l: *Lex) -> Result { skipSpace(l) if l.pos >= l.src.Length { return ok(Token.eof()) } let c = l.src[l.pos] if c == '"' { l.pos += 1 let start = l.pos while l.pos < l.src.Length && l.src[l.pos] != '"' { l.pos += 1 } if l.pos >= l.src.Length { return error("unterminated string") } let text = l.src.Substring(start, l.pos - start) l.pos += 1 return ok(Token.str(text)) } if char.IsDigit(c) { let start = l.pos while l.pos < l.src.Length && char.IsDigit(l.src[l.pos]) { l.pos += 1 } let text = l.src.Substring(start, l.pos - start) return ok(Token.integer(int.Parse(text))) } return error("unexpected char") } func sumIntegersUntilEof(input: string) -> Result { var l: *Lex = new Lex { src: input, pos: 0 } var total = 0 while true { let tok = nextToken(l)? match (tok: Token) { .integer(i) { total += i.value } .str(_) { } .eof { return ok(total) } } } return ok(total) } // ── FluentChainTests__ChainedCall_WithIfGuardedMutation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FluentChainTests.cs::ChainedCall_WithIfGuardedMutation topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Command { forward(steps: int) } ref data Turtle { var y: int var facing: int init() { self.y = 0 self.facing = 0 } } func apply(t: Turtle, cmd: Command) -> Turtle { match cmd { .forward(steps) { if t.facing == 0 { t.y += steps } } } return t } func go() -> int { let t = Turtle().apply(Command.forward(5)).apply(Command.forward(3)) return t.y } // ── FluentChainTests__ChainedCall_WithNestedEnumMatch_UpdatesField ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FluentChainTests.cs::ChainedCall_WithNestedEnumMatch_UpdatesField topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test enum Turn { left right } choice Command { turn(direction: Turn) } ref data Turtle { var facing: int init() { self.facing = 0 } } func apply(t: Turtle, cmd: Command) -> Turtle { match cmd { .turn(direction) { match (direction: Turn) { .left { t.facing = (t.facing + 3) % 4 } .right { t.facing = (t.facing + 1) % 4 } } } } return t } func go() -> int { let t = Turtle().apply(Command.turn(Turn.right())) return t.facing } // ── FluentChainTests__Turtle_FullDrive_FluentChain ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: FluentChainTests.cs::Turtle_FullDrive_FluentChain topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test enum Turn { left right } choice Command { forward(steps: int) turn(direction: Turn) } ref data Turtle { var x: int var y: int var facing: int init() { self.x = 0 self.y = 0 self.facing = 0 } } func apply(t: Turtle, cmd: Command) -> Turtle { match cmd { .forward(steps) { if t.facing == 0 { t.y += steps } else if t.facing == 1 { t.x += steps } else if t.facing == 2 { t.y -= steps } else { t.x -= steps } } .turn(direction) { match (direction: Turn) { .left { t.facing = (t.facing + 3) % 4 } .right { t.facing = (t.facing + 1) % 4 } } } } return t } func go() -> int { let t = Turtle() .apply(Command.forward(5)) .apply(Command.turn(Turn.right())) .apply(Command.forward(3)) .apply(Command.turn(Turn.right())) .apply(Command.forward(2)) return t.x * 100 + t.y } // ── ForwardReferenceTests__Choice_Payload_Of_LaterChoice ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ForwardReferenceTests.cs::Choice_Payload_Of_LaterChoice topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Outer { wraps(inner: Inner) none } choice Inner { has(n: int) nothing } func go() -> int { let o = Outer.wraps(Inner.has(11)) match o { .wraps(c) { match c.inner { .has(x) { return x } .nothing { return 0 } } } .none { return 0 } } } // ── ForwardReferenceTests__Choice_Payload_Of_LaterData ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ForwardReferenceTests.cs::Choice_Payload_Of_LaterData topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Box { full(p: Item) empty } data Item { weight: int } func go() -> int { let b = Box.full(Item { weight: 9 }) match b { .full(c) { return c.p.weight } .empty { return 0 } } } // ── ForwardReferenceTests__Data_And_RefChoice_MutualCycle ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ForwardReferenceTests.cs::Data_And_RefChoice_MutualCycle topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test data Field { key: string, value: J } ref choice J { jnull jobj(fields: List) } func go() -> int { let fs = List() fs.Add(Field { key: "a", value: J.jnull() }) fs.Add(Field { key: "b", value: J.jnull() }) let j = J.jobj(fs) match j { .jnull { return 0 } .jobj(c) { return c.fields.Count } } } // ── ForwardReferenceTests__Data_Field_Of_LaterChoice ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ForwardReferenceTests.cs::Data_Field_Of_LaterChoice topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test data Wrap { c: Pick } choice Pick { a(n: int) b(n: int) } func go() -> int { let w = Wrap { c: Pick.a(5) } match w.c { .a(x) { return x } .b(x) { return x } } } // ── ForwardReferenceTests__Data_Field_Of_LaterEnum ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ForwardReferenceTests.cs::Data_Field_Of_LaterEnum topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test data Row { d: Dir } enum Dir { north south east } func go() -> int { let r = Row { d: Dir.east() } match r.d { .north { return 0 } .south { return 1 } .east { return 2 } } } // ── ForwardReferenceTests__EndToEnd_ForwardDeclared_JsonLike ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ForwardReferenceTests.cs::EndToEnd_ForwardDeclared_JsonLike topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test using "System.Text" func render(v: Val) -> string { match v { .num(c) { return "{c.value}" } .arr(c) { let sb = StringBuilder() sb.Append("[") var i = 0 while i < c.items.Count { if i > 0 { sb.Append(",") } sb.Append(render(c.items[i])) i += 1 } sb.Append("]") return sb.ToString() } } } ref choice Val { num(value: int) arr(items: List) } func go() -> string { let xs = List() xs.Add(Val.num(1)) xs.Add(Val.num(2)) xs.Add(Val.num(3)) return render(Val.arr(xs)) } // ── ForwardReferenceTests__RefChoice_Payload_Of_LaterData ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ForwardReferenceTests.cs::RefChoice_Payload_Of_LaterData topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Node { leaf branch(items: List) } data Leaf { v: int } func go() -> int { let xs = List() xs.Add(Leaf { v: 1 }) xs.Add(Leaf { v: 2 }) xs.Add(Leaf { v: 3 }) let n = Node.branch(xs) match n { .leaf { return 0 } .branch(c) { return c.items.Count } } } // ── ILEmitterTests__Choice_FactoryMethods_CreateValidStructs ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::Choice_FactoryMethods_CreateValidStructs topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Option { some(value: int) none } func makeNone() -> Option { return Option.none() } func makeSome(v: int) -> Option { return Option.some(v) } // ── ILEmitterTests__Choice_TagEnum_HasCorrectValues ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::Choice_TagEnum_HasCorrectValues topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Color { red green blue } func makeGreen() -> Color { return Color.green() } // ── ILEmitterTests__MultiFile_ChoiceInFileA_MatchedInFileB_Works ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::MultiFile_ChoiceInFileA_MatchedInFileB_Works topic: choice status: verified // verified behavior: Test.goSquare(...) == 36 namespace Test choice Shape { circle(r: int) square(side: int) } // ── ILEmitterTests__MultiPayload_Choice_FieldsAndFactory ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::MultiPayload_Choice_FieldsAndFactory topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Msg { text(from: string, body: string) ping } func makeText(f: string, b: string) -> Msg { return Msg.text(f, b) } func makePing() -> Msg { return Msg.ping() } // ── ILEmitterTests__MultiPayload_ValueChoice_Destructuring_IL_Runtime ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::MultiPayload_ValueChoice_Destructuring_IL_Runtime topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice LogEntry { message(level: int, text: string) timestamp(epoch: int) } func getLevel() -> int { let entry = LogEntry.message(3, "hello") match (entry: LogEntry) { .message(lvl, txt) { return lvl } .timestamp(t) { return 0 } } return -1 } // ── ILEmitterTests__RefChoice_DotCase_EmitsNewobj ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::RefChoice_DotCase_EmitsNewobj topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Shape { circle(radius: float) point } func makeCircle() -> Shape { return .circle(3.14) } func makePoint() -> Shape { return .point() } // ── ILEmitterTests__RefChoice_IL_EmitsSealedClassHierarchy ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::RefChoice_IL_EmitsSealedClassHierarchy topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Shape { circle(radius: float) rect(width: float) point } // ── ILEmitterTests__RefChoice_Match_EmitsIsinst ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::RefChoice_Match_EmitsIsinst topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Shape { circle(radius: float) point } func describe(s: Shape) -> int { match (s: Shape) { .circle(c) { return 1 } .point { return 0 } } return 0 } // ── ILEmitterTests__RefChoice_Match_Runtime ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::RefChoice_Match_Runtime topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Expr { literal(value: int) neg(inner: Expr) } func eval(e: Expr) -> int { match (e: Expr) { .literal(lit) { return lit.value } .neg(n) { return 0 - eval(n.inner) } default { return 0 } } return 0 } // ── ILEmitterTests__ValueChoice_Match_Destructuring_IL_Runtime ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::ValueChoice_Match_Destructuring_IL_Runtime topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Result { ok(value: int) err(message: string) } func check() -> int { let r = Result.ok(42) match (r: Result) { .ok(v) { return v } .err(msg) { return 0 } } return -1 } // ── ILEmitterTests_CaseView__Enum_NoBindingsStillWorks ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::Enum_NoBindingsStillWorks topic: choice status: verified // verified behavior: Test.go(...) == "N" enum Dir { north south } func go() -> string { let d = Dir.north() match (d: Dir) { .north { return "N" } .south { return "S" } } return "?" } // ── ILEmitterTests_CaseView__ThreeLevel_OnePath ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::ThreeLevel_OnePath topic: choice status: verified // verified behavior: Test.go(...) == "one-num:7" choice Lex { one(t: Token) pair(a: Token, b: Token) } // ── ILEmitterTests_Coverage_Adversarial__DefiniteReturn_ExhaustiveBool_NoTrailingReturn_Clean ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Adversarial.cs::DefiniteReturn_ExhaustiveBool_NoTrailingReturn_Clean topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func label(b: bool) -> string { match b { true { return "on" } false { return "off" } } } func go() -> string = label(true) // ── ILEmitterTests_Coverage_Adversarial__DefiniteReturn_ExhaustiveChoice_NoTrailingReturn_Clean ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Adversarial.cs::DefiniteReturn_ExhaustiveChoice_NoTrailingReturn_Clean topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Dir { north, south } func name(d: Dir) -> string { match d { .north { return "N" } .south { return "S" } } } func go() -> string = name(Dir.south()) // ── ILEmitterTests_Coverage_Adversarial__DefiniteReturn_ExhaustiveEnum_NoTrailingReturn_Clean ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Adversarial.cs::DefiniteReturn_ExhaustiveEnum_NoTrailingReturn_Clean topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test enum Color { red, green, blue } func code(c: Color) -> int { match (c: Color) { .red { return 1 } .green { return 2 } .blue { return 3 } } } func go() -> int = code(Color.green()) // ── ILEmitterTests_Coverage_Adversarial__Generics_OptionPayloadMatch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Adversarial.cs::Generics_OptionPayloadMatch topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Option { some(value: T) none } func unwrapOr(o: Option, fallback: int) -> int { match o { .some(v) { return v } .none { return fallback } } return fallback } func go() -> int { let a = Option.some(99) let b = Option.none() return unwrapOr(a, -1) + unwrapOr(b, 5) } // ── ILEmitterTests_Coverage_Adversarial__Interp_NestedMatchInHole ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Adversarial.cs::Interp_NestedMatchInHole topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func go() -> string { let n = 2 return "v={match n { 1 { "one" } 2 { "two" } default { "?" } }}" } // ── ILEmitterTests_Coverage_Adversarial__Ns_SnakeCaseTypeName_IsError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Adversarial.cs::Ns_SnakeCaseTypeName_IsError topic: choice status: verified // verified behavior: reports diagnostic ES2160 namespace Test choice my_choice { a b } // ── ILEmitterTests_Coverage_Choice__Enum_ExplicitAndAutoValues ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::Enum_ExplicitAndAutoValues topic: choice status: verified // verified behavior: Test.go(...) == 21 namespace Test enum Level { a b = 10 c } func rank(l: Level) -> int { match (l: Level) { .a { return 0 } .b { return 10 } .c { return 11 } } return -1 } func go() -> int { return rank(Level.a()) + rank(Level.b()) + rank(Level.c()) } // ── ILEmitterTests_Coverage_Choice__Match_AsExpression ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::Match_AsExpression topic: choice status: verified // verified behavior: Test.go(...) == 40 namespace Test func go() -> int { let status = 404 let label = match status { 200 { 1 } 404 { 4 } default { 0 } } return label * 10 } // ── ILEmitterTests_Coverage_Choice__Match_LiteralInt ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::Match_LiteralInt topic: choice status: verified // verified behavior: Test.go(...) == 2 namespace Test func classify(n: int) -> int { match n { 200 { return 1 } 404 { return 2 } 500 { return 3 } default { return 0 } } } func go() -> int { return classify(404) + classify(999) } // ── ILEmitterTests_Coverage_Choice__Match_LiteralString ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::Match_LiteralString topic: choice status: verified // verified behavior: Test.go(...) == 8 namespace Test func perm(name: string) -> int { match name { "admin" { return 7 } "guest" { return 1 } default { return 0 } } } func go() -> int { return perm("admin") + perm("guest") + perm("other") } // ── ILEmitterTests_Coverage_Choice__Match_OnReceiverInferred ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::Match_OnReceiverInferred topic: choice status: verified // verified behavior: Test.go(...) == 1 namespace Test choice State { on off } data Switch { var s: State } func describe(sw: Switch) -> int { match sw.s { .on { return 1 } .off { return 0 } } return -1 } func go() -> int { let sw = Switch { s: State.on() } return sw.describe() } // ── ILEmitterTests_Coverage_Choice__RefChoice_PositionalMultiPayloadBinding ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::RefChoice_PositionalMultiPayloadBinding topic: choice status: verified // verified behavior: Test.go(...) == 23 namespace Test ref choice Expr { lit(value: int) add(left: Expr, right: Expr) mul(left: Expr, right: Expr) } func eval(e: Expr) -> int { match (e: Expr) { .lit(v) { return v.value } .add(l, r) { return eval(l) + eval(r) } .mul(l, r) { return eval(l) * eval(r) } } return 0 } func go() -> int { let tree = Expr.add(Expr.lit(3), Expr.mul(Expr.lit(4), Expr.lit(5))) return eval(tree) } // ── ILEmitterTests_Coverage_Choice__RefChoice_RecursiveEval ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::RefChoice_RecursiveEval topic: choice status: verified // verified behavior: Test.go(...) == 23 namespace Test ref choice Expr { lit(value: int) add(left: Expr, right: Expr) mul(left: Expr, right: Expr) } func eval(e: Expr) -> int { match (e: Expr) { .lit(v) { return v.value } .add(a) { return eval(a.left) + eval(a.right) } .mul(m) { return eval(m.left) * eval(m.right) } } return 0 } func go() -> int { let tree = Expr.add(Expr.lit(3), Expr.mul(Expr.lit(4), Expr.lit(5))) return eval(tree) } // ── ILEmitterTests_Coverage_Choice__ValueChoice_MatchZeroPayload ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::ValueChoice_MatchZeroPayload topic: choice status: verified // verified behavior: Test.go(...) == 2 namespace Test choice Color { red green blue } func code(c: Color) -> int { match (c: Color) { .red { return 1 } .green { return 2 } .blue { return 3 } } return 0 } func go() -> int { return code(Color.green()) } // ── ILEmitterTests_Coverage_Choice__ValueChoice_MultiPayload ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::ValueChoice_MultiPayload topic: choice status: verified // verified behavior: Test.go(...) == 207 namespace Test choice Entry { log(level: int, code: int) blank } func score(e: Entry) -> int { match (e: Entry) { .log(lvl, code) { return lvl * 100 + code } .blank { return 0 } } return -1 } func go() -> int { return score(Entry.log(2, 7)) } // ── ILEmitterTests_Coverage_Choice__ValueChoice_SinglePayload ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::ValueChoice_SinglePayload topic: choice status: verified // verified behavior: Test.go(...) == 25 namespace Test choice Shape { circle(radius: int) square(side: int) } func area(s: Shape) -> int { match (s: Shape) { .circle(r) { return 3 * r * r } .square(side) { return side * side } } return 0 } func go() -> int { return area(Shape.square(5)) } // ── ILEmitterTests_Coverage_ControlFlow__Added_Match_Default ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Added_Match_Default topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Sel { a, b, c } func pick(s: Sel) -> int { match s { .a { return 1 } default { return 99 } } return 0 } func go() -> int { return pick(Sel.c()) } // ── ILEmitterTests_Coverage_ControlFlow__Added_Match_OnChoice ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Added_Match_OnChoice topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Shape { circle(r: int), square(s: int) } func area(sh: Shape) -> int { match sh { .circle(r) { return r } .square(s) { return s } } return 0 } func go() -> int { return area(Shape.square(5)) } // ── ILEmitterTests_Coverage_ControlFlow__Added_Match_PointerMutateInArm ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Added_Match_PointerMutateInArm topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test data Box { n: int } choice Sel { a, b } func go() -> int { let s = Sel.a() var box = new Box { n: 0 } match s { .a { box.n = 7 } .b { box.n = 9 } } return box.n } // ── ILEmitterTests_Coverage_ControlFlow__Match_AsExpression_ExpressionBodiedFunc ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_AsExpression_ExpressionBodiedFunc topic: choice status: verified // verified behavior: Test.go(...) == "two" namespace Test func name(n: int) -> string = match n { 1 { "one" } 2 { "two" } default { "many" } } func go() -> string { return name(2) } // ── ILEmitterTests_Coverage_ControlFlow__Match_AsExpression_InInterpolation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_AsExpression_InInterpolation topic: choice status: verified // verified behavior: Test.go(...) == "code=ok" namespace Test func go() -> string { let status = 200 return "code={match status { 200 { "ok" } default { "?" } }}" } // ── ILEmitterTests_Coverage_ControlFlow__Match_AsExpression_InLet ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_AsExpression_InLet topic: choice status: verified // verified behavior: Test.go(...) == "not found" namespace Test func go() -> string { let status = 404 let label = match status { 200 { "ok" } 404 { "not found" } default { "other" } } return label } // ── ILEmitterTests_Coverage_ControlFlow__Match_BoolLiteral ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_BoolLiteral topic: choice status: verified // verified behavior: Test.go(...) == "on" namespace Test func label(flag: bool) -> string { match flag { true { return "on" } false { return "off" } } } func go() -> string { return label(true) } // ── ILEmitterTests_Coverage_ControlFlow__Match_Choice_AsExpression ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Choice_AsExpression topic: choice status: verified // verified behavior: Test.go(...) == "sum=7" namespace Test choice Cmd { add(a: int, b: int) nop } func go() -> string { let c = Cmd.add(3, 4) return match c { .add(a, b) { "sum={a + b}" } .nop { "noop" } } } // ── ILEmitterTests_Coverage_ControlFlow__Match_Choice_CaseViewProjection ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Choice_CaseViewProjection topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Pair { one(a: int) two(a: int, b: int) } func go() -> int { let p = Pair.two(3, 4) match p { .one(o) { return o.a } .two(t) { return t.a + t.b } } return -1 } // ── ILEmitterTests_Coverage_ControlFlow__Match_Choice_GenericOption ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Choice_GenericOption topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Option { some(value: T) none } func unwrapOr(o: Option, fallback: int) -> int { match o { .some(v) { return v } .none { return fallback } } return fallback } func go() -> int { let o = Option.some(99) return unwrapOr(o, -1) } // ── ILEmitterTests_Coverage_ControlFlow__Match_Choice_GenericOption_NoneFallback ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Choice_GenericOption_NoneFallback topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Option { some(value: T) none } func unwrapOr(o: Option, fallback: int) -> int { match o { .some(v) { return v } .none { return fallback } } return fallback } func go() -> int { let o = Option.none() return unwrapOr(o, -1) } // ── ILEmitterTests_Coverage_ControlFlow__Match_Choice_MultiPayloadDestructure ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Choice_MultiPayloadDestructure topic: choice status: verified // verified behavior: Test.go(...) == "[3] hi" namespace Test choice LogEntry { message(level: int, text: string) blank } func go() -> string { let e = LogEntry.message(3, "hi") match e { .message(lvl, txt) { return "[{lvl}] {txt}" } .blank { return "" } } return "?" } // ── ILEmitterTests_Coverage_ControlFlow__Match_Choice_SinglePayloadBinding ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Choice_SinglePayloadBinding topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Box { empty full(value: int) } func go() -> int { let b = Box.full(42) match b { .empty { return 0 } .full(v) { return v } } return -1 } // ── ILEmitterTests_Coverage_ControlFlow__Match_Choice_ZeroPayload ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Choice_ZeroPayload topic: choice status: verified // verified behavior: Test.go(...) == "a" namespace Test choice Cmd { help quit } func go() -> string { let c = Cmd.help() match c { .help { return "a" } .quit { return "b" } } return "?" } // ── ILEmitterTests_Coverage_ControlFlow__Match_Enum_AsExpression ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Enum_AsExpression topic: choice status: verified // verified behavior: Test.go(...) == "N" namespace Test enum Dir { north, south } func go() -> string { let d = Dir.north() let name = match (d: Dir) { .north { "N" } .south { "S" } default { "?" } } return name } // ── ILEmitterTests_Coverage_ControlFlow__Match_Enum_BasicDispatch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Enum_BasicDispatch topic: choice status: verified // verified behavior: Test.go(...) == "S" namespace Test enum Direction { north, south, east, west } func glyph(d: Direction) -> string { match (d: Direction) { .north { return "N" } .south { return "S" } .east { return "E" } .west { return "W" } } return "?" } func go() -> string { return glyph(Direction.south()) } // ── ILEmitterTests_Coverage_ControlFlow__Match_Enum_ExplicitValues ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Enum_ExplicitValues topic: choice status: verified // verified behavior: Test.go(...) == "warn" namespace Test enum Code { ok = 0, warn = 10, fail = 20 } func name(c: Code) -> string { match (c: Code) { .ok { return "ok" } .warn { return "warn" } .fail { return "fail" } default { return "?" } } } func go() -> string { return name(Code.warn()) } // ── ILEmitterTests_Coverage_ControlFlow__Match_Enum_WithDefaultArm ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Enum_WithDefaultArm topic: choice status: verified // verified behavior: Test.go(...) == "other" namespace Test enum Size { small, medium, large } func label(s: Size) -> string { match (s: Size) { .small { return "S" } default { return "other" } } } func go() -> string { return label(Size.large()) } // ── ILEmitterTests_Coverage_ControlFlow__Match_IntLiteral ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_IntLiteral topic: choice status: verified // verified behavior: Test.go(...) == "not found" namespace Test func describe(code: int) -> string { match code { 200 { return "ok" } 404 { return "not found" } 500 { return "server error" } default { return "unknown" } } } func go() -> string { return describe(404) } // ── ILEmitterTests_Coverage_ControlFlow__Match_IntLiteral_DefaultArm ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_IntLiteral_DefaultArm topic: choice status: verified // verified behavior: Test.go(...) == "unknown" namespace Test func describe(code: int) -> string { match code { 200 { return "ok" } default { return "unknown" } } } func go() -> string { return describe(301) } // ── ILEmitterTests_Coverage_ControlFlow__Match_NegativeIntLiteral ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_NegativeIntLiteral topic: choice status: verified // verified behavior: Test.go(...) == "neg" namespace Test func sign(n: int) -> string { match n { -1 { return "neg" } 0 { return "zero" } 1 { return "pos" } default { return "?" } } } func go() -> string { return sign(-1) } // ── ILEmitterTests_Coverage_ControlFlow__Match_Nested ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_Nested topic: choice status: verified // verified behavior: Test.go(...) == "a-inner" namespace Test func go() -> string { let outer = "a" let inner = 1 match outer { "a" { match inner { 1 { return "a-inner" } default { return "a-other" } } } default { return "other" } } return "?" } // ── ILEmitterTests_Coverage_ControlFlow__Match_OnLoopIndex_Categorizes ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_OnLoopIndex_Categorizes topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func go() -> int { var evens = 0 for i in 0..8 { match i % 2 { 0 { evens += 1 } default { } } } return evens } // ── ILEmitterTests_Coverage_ControlFlow__Match_RefChoice_CompositeLiteralConstruction ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_RefChoice_CompositeLiteralConstruction topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Expr { literal(value: int) add(left: Expr, right: Expr) } func eval(e: Expr) -> int { match e { .literal(lit) { return lit.value } .add(a) { return eval(a.left) + eval(a.right) } } return 0 } func go() -> int { let sum = Expr_add { left: Expr_literal { value: 3 } right: Expr_literal { value: 4 } } return eval(sum) } // ── ILEmitterTests_Coverage_ControlFlow__Match_RefChoice_RecursiveEval ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_RefChoice_RecursiveEval topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Expr { literal(value: int) add(left: Expr, right: Expr) mul(left: Expr, right: Expr) } func eval(e: Expr) -> int { match e { .literal(lit) { return lit.value } .add(a) { return eval(a.left) + eval(a.right) } .mul(m) { return eval(m.left) * eval(m.right) } } return 0 } func go() -> int { let tree = Expr.add(Expr.literal(2), Expr.mul(Expr.literal(3), Expr.literal(4))) return eval(tree) } // ── ILEmitterTests_Coverage_ControlFlow__Match_StringLiteral ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Match_StringLiteral topic: choice status: verified // verified behavior: Test.go(...) == "admin-perms" namespace Test func perms(role: string) -> string { match role { "admin" { return "admin-perms" } "guest" { return "guest-perms" } default { return "none" } } } func go() -> string { return perms("admin") } // ── ILEmitterTests_Coverage_ControlFlow__Nested_LoopWithMatchClassifier ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_ControlFlow.cs::Nested_LoopWithMatchClassifier topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func classify(n: int) -> int { return match n % 3 { 0 { 0 } 1 { 1 } default { 2 } } } func go() -> int { var twos = 0 for i in 0..9 { if classify(i) == 2 { twos += 1 } } return twos } // ── ILEmitterTests_Coverage_Errors__OptionInt_NoneFallsBack ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::OptionInt_NoneFallsBack topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice OptionInt { some(value: int) none } func get(present: bool) -> OptionInt { if present { return OptionInt.some(42) } return OptionInt.none() } func go() -> int { let o = get(false) match o { .some(v) { return v } .none { return -1 } } return -1 } // ── ILEmitterTests_Coverage_Errors__OptionInt_SomeUnwraps ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::OptionInt_SomeUnwraps topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice OptionInt { some(value: int) none } func get(present: bool) -> OptionInt { if present { return OptionInt.some(42) } return OptionInt.none() } func go() -> int { let o = get(true) match o { .some(v) { return v } .none { return -1 } } return -1 } // ── ILEmitterTests_Coverage_Errors__Result_ChoiceErrorVariant ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_ChoiceErrorVariant topic: choice status: verified // verified behavior: Test.go(...) == "notFound" namespace Test choice DbError { notFound timeout connectionFailed(message: string) } func lookup(id: int) -> Result { if id == 0 { return error(DbError.notFound()) } return ok(id) } func go() -> string { let r = lookup(0) if r.IsError { match r.Error { .notFound { return "notFound" } .timeout { return "timeout" } .connectionFailed(c) { return "conn" } } } return "ok" } // ── ILEmitterTests_Coverage_Errors__Result_ChoiceErrorWithPayload ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_ChoiceErrorWithPayload topic: choice status: verified // verified behavior: Test.go(...) == "conn:down" namespace Test choice DbError { notFound connectionFailed(message: string) } func lookup(id: int) -> Result { return error(DbError.connectionFailed("down")) } func go() -> string { let r = lookup(1) if r.IsError { match r.Error { .notFound { return "notFound" } .connectionFailed(c) { return "conn:{c.message}" } } } return "ok" } // ── ILEmitterTests_Coverage_Errors__Result_DotCaseShorthandError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_DotCaseShorthandError topic: choice status: verified // verified behavior: Test.go(...) == "notFound" namespace Test choice DbError { notFound timeout } func lookup(id: int) -> Result { if id == 0 { return error(.notFound) } return ok(id) } func go() -> string { let r = lookup(0) if r.IsError { match r.Error { .notFound { return "notFound" } .timeout { return "timeout" } } } return "ok" } // ── ILEmitterTests_Coverage_Misc__Enum_MatchWithDefault ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Misc.cs::Enum_MatchWithDefault topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test enum Dir { north, south, east, west } func go() -> int { let d = Dir.north() match (d: Dir) { .north { return 1 } default { return 0 } } return -1 } // ── ILEmitterTests_Integration__CrossNamespace_ChoiceConstructedAndMatched ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Integration.cs::CrossNamespace_ChoiceConstructedAndMatched topic: choice status: verified // verified behavior: Test.go(...) == "active" namespace Model choice Status { active suspended(reason: string) } // ── ILEmitterTests_Interpolation__InterpolationInsideMatchArm ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Interpolation.cs::InterpolationInsideMatchArm topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) func go() -> string { let n = 2 return match n { 1 { "one:1" } 2 { "two:{n}" } default { "x" } } } // ── ILEmitterTests2__Choice_Bare_Variants_Tag_Discrimination_Pin ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Choice_Bare_Variants_Tag_Discrimination_Pin topic: choice status: verified // verified behavior: Test.test(...) == "busy" namespace Test choice Status { idle busy done } func describe(s: Status) -> string { match (s: Status) { .idle { return "idle" } .busy { return "busy" } .done { return "done" } default { return "unknown" } } return "unknown" } func test() -> string { return describe(Status.busy()) } // ── ILEmitterTests2__Choice_Many_Bare_Variants_Pin ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Choice_Many_Bare_Variants_Pin topic: choice status: verified // verified behavior: Test.test(...) == "west" namespace Test choice Direction { north south east west } func opposite(d: Direction) -> string { match (d: Direction) { .north { return "south" } .south { return "north" } .east { return "west" } .west { return "east" } default { return "?" } } return "?" } func test() -> string { return opposite(Direction.east()) } // ── ILEmitterTests2__Enum_ExplicitValues_MatchExpression ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Enum_ExplicitValues_MatchExpression topic: choice status: verified // verified behavior: Test.test(...) == "H" namespace Test enum Priority { low = 1 medium = 5 high = 10 } func label(p: Priority) -> string { match (p: Priority) { .low { return "L" } .medium { return "M" } .high { return "H" } default { return "?" } } } func test() -> string { return label(Priority.high()) } // ── ILEmitterTests2__Enum_Match_BasicDispatch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Enum_Match_BasicDispatch topic: choice status: verified // verified behavior: Test.test(...) == "g" namespace Test enum Color { red green blue } func describe(c: Color) -> string { match (c: Color) { .red { return "r" } .green { return "g" } .blue { return "b" } default { return "?" } } } func test() -> string { return describe(Color.green()) } // ── ILEmitterTests2__Enum_Match_ExplicitValues_Dispatch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Enum_Match_ExplicitValues_Dispatch topic: choice status: verified // verified behavior: Test.test(...) == "warn" namespace Test enum Code { ok = 0 warn = 10 fail = 20 } func name(c: Code) -> string { match (c: Code) { .ok { return "ok" } .warn { return "warn" } .fail { return "fail" } default { return "?" } } } func test() -> string { return name(Code.warn()) } // ── ILEmitterTests2__Enum_Match_WithDefault ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Enum_Match_WithDefault topic: choice status: verified // verified behavior: Test.test(...) == "other" namespace Test enum Size { small medium large } func label(s: Size) -> string { match (s: Size) { .small { return "S" } default { return "other" } } } func test() -> string { return label(Size.large()) } // ── ILEmitterTests2__Enum_StoredInLocal_ThenPassedToFunc ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Enum_StoredInLocal_ThenPassedToFunc topic: choice status: verified // verified behavior: Test.test(...) == "S" namespace Test enum Dir { north south } func label(d: Dir) -> string { match (d: Dir) { .north { return "N" } .south { return "S" } default { return "?" } } } func test() -> string { let d = Dir.south() return label(d) } // ── ILEmitterTests2__Literal_Match_Bool ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Literal_Match_Bool topic: choice status: verified // verified behavior: Test.test(...) == "no" namespace Test func label(b: bool) -> string { match b { true { return "yes" } false { return "no" } default { return "?" } } } func test() -> string { return label(false) } // ── ILEmitterTests2__Literal_Match_Int ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Literal_Match_Int topic: choice status: verified // verified behavior: Test.test(...) == "two" namespace Test func describe(n: int) -> string { match n { 1 { return "one" } 2 { return "two" } 3 { return "three" } default { return "other" } } } func test() -> string { return describe(2) } // ── ILEmitterTests2__Literal_Match_Int_Default ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Literal_Match_Int_Default topic: choice status: verified // verified behavior: Test.test(...) == "other" namespace Test func describe(n: int) -> string { match n { 1 { return "one" } default { return "other" } } } func test() -> string { return describe(99) } // ── ILEmitterTests2__Literal_Match_NegativeInt ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Literal_Match_NegativeInt topic: choice status: verified // verified behavior: Test.test(...) == "neg" namespace Test func sign(n: int) -> string { match n { -1 { return "neg" } 0 { return "zero" } 1 { return "pos" } default { return "other" } } } func test() -> string { return sign(-1) } // ── ILEmitterTests2__Literal_Match_String ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Literal_Match_String topic: choice status: verified // verified behavior: Test.test(...) == "hola" namespace Test func greet(lang: string) -> string { match lang { "en" { return "hello" } "es" { return "hola" } "fr" { return "bonjour" } default { return "hi" } } } func test() -> string { return greet("es") } // ── ILEmitterTests2__Match_Expression_Choice ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_Choice topic: choice status: verified // verified behavior: Test.test(...) == 42 namespace Test choice Option { some(value: int) none } func unwrap(o: Option) -> int { let v = match o { .some(x) { x } .none { 0 } } return v } func test() -> int { return unwrap(Option.some(42)) } // ── ILEmitterTests2__Match_Expression_ChoiceString ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_ChoiceString topic: choice status: verified // verified behavior: Test.test(...) == "ok" namespace Test choice Result { ok(value: int) err(message: string) } func unwrap(r: Result) -> string = match r { .ok(v) { "ok" } .err(msg) { "err" } default { "?" } } func test() -> string { return unwrap(Result.ok(42)) } // ── ILEmitterTests2__Match_Expression_Enum ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_Enum topic: choice status: verified // verified behavior: Test.test(...) == "N" namespace Test enum Dir { north south } func test() -> string { let d = Dir.north() let name = match (d: Dir) { .north { "N" } .south { "S" } default { "?" } } return name } // ── ILEmitterTests2__Match_Expression_ExpressionBody ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_ExpressionBody topic: choice status: verified // verified behavior: Test.test(...) == "two" namespace Test func label(n: int) -> string = match n { 1 { "one" } 2 { "two" } default { "?" } } func test() -> string { return label(2) } // ── ILEmitterTests2__Match_Expression_InlineLetAssignment ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_InlineLetAssignment topic: choice status: verified // verified behavior: Test.test(...) == "seven" namespace Test func test() -> string { let x = 7 let tag = match x { 1 { "one" } 7 { "seven" } default { "other" } } return tag } // ── ILEmitterTests2__Match_Expression_IntLiteral ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_IntLiteral topic: choice status: verified // verified behavior: Test.test(...) == "two" namespace Test func test() -> string { let x = match 2 { 1 { "one" } 2 { "two" } default { "other" } } return x } // ── ILEmitterTests2__Match_Expression_NegativeLiteral ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_NegativeLiteral topic: choice status: verified // verified behavior: Test.test(...) == "neg" namespace Test func sign(n: int) -> string = match n { -1 { "neg" } 0 { "zero" } 1 { "pos" } default { "other" } } func test() -> string { return sign(-1) } // ── ILEmitterTests2__Match_Expression_StringLiteral ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_Expression_StringLiteral topic: choice status: verified // verified behavior: Test.test(...) == "hola" namespace Test func greet(lang: string) -> string = match lang { "en" { "hello" } "es" { "hola" } default { "hi" } } func test() -> string { return greet("es") } // ── ILEmitterTests2__Match_ExpressionBody_ResultInInterpolation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Match_ExpressionBody_ResultInInterpolation topic: choice status: verified // verified behavior: Test.test(...) == "level: medium" namespace Test func levelName(n: int) -> string = match n { 1 { "low" } 2 { "medium" } default { "?" } } func test() -> string { let name = levelName(2) return "level: {name}" } // ── ILEmitterTests2__Sample_MatchPatterns_Full ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests2.cs::Sample_MatchPatterns_Full topic: choice status: verified // verified behavior: Test.test(...) == "medium,hola,CRIT,ok,seven,negative" namespace Test func levelName(n: int) -> string = match n { 1 { "low" } 2 { "medium" } 3 { "high" } default { "unknown" } } func greet(lang: string) -> string = match lang { "en" { "hello" } "es" { "hola" } "fr" { "bonjour" } default { "hi" } } enum Priority { low = 1 medium = 5 high = 10 critical = 100 } func priorityLabel(p: Priority) -> string { match (p: Priority) { .low { return "LOW" } .medium { return "MED" } .high { return "HIGH" } .critical { return "CRIT" } default { return "?" } } } choice Result { ok(value: int) err(message: string) } func unwrap(r: Result) -> string = match r { .ok(v) { "ok" } .err(msg) { "err" } default { "?" } } enum Color { red green = 10 blue } func test() -> string { let a = levelName(2) let b = greet("es") let c = priorityLabel(Priority.critical()) let d = unwrap(Result.ok(42)) let x = 7 let tag = match x { 1 { "one" } 7 { "seven" } default { "other" } } let n = -1 let sign = match n { -1 { "negative" } 0 { "zero" } 1 { "positive" } default { "other" } } return "{a},{b},{c},{d},{tag},{sign}" } // ── ILEmitterTests3__Choice_Bare_Variants_Default_Arm ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Choice_Bare_Variants_Default_Arm topic: choice status: verified // verified behavior: Test.test(...) == "yes" namespace Test choice Light { off, on } func test() -> string { let l = Light.on() match (l: Light) { .on { return "yes" } default { return "no" } } return "unreachable" } // ── ILEmitterTests3__Choice_Helper_Constructs_Variant ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Choice_Helper_Constructs_Variant topic: choice status: verified // verified behavior: Test.test(...) == 1 namespace Test choice Status { idle, busy } func make_busy() -> Status = Status.busy() func test() -> int { let s = make_busy() match (s: Status) { .busy { return 1 } default { return 0 } } return -1 } // ── ILEmitterTests3__Match_Bool_Patterns ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Match_Bool_Patterns topic: choice status: verified // verified behavior: Test.state(...) == "off" namespace Test func state(b: bool) -> string = match b { true { "on" } false { "off" } } // ── ILEmitterTests3__Match_Choice_Multiple_Variants_With_Payloads ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Match_Choice_Multiple_Variants_With_Payloads topic: choice status: verified // verified behavior: Test.test(...) == "num=42|word=foo" namespace Test choice Token { plus minus number(value: int) word(name: string) } func describe(t: Token) -> string = match (t: Token) { .plus { "+" } .minus { "-" } .number(n) { "num=" + n.ToString() } .word(w) { "word=" + w } default { "?" } } func test() -> string { return describe(Token.number(42)) + "|" + describe(Token.word("foo")) } // ── ILEmitterTests3__Match_Choice_With_Payload_Destructures ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Match_Choice_With_Payload_Destructures topic: choice status: verified // verified behavior: Test.test(...) == 42 namespace Test choice Maybe { none some(value: int) } func unwrap(m: Maybe, def: int) -> int = match (m: Maybe) { .none { def } .some(v) { v } default { def } } func test() -> int = unwrap(Maybe.some(42), 0) // ── ILEmitterTests3__Match_Expression_Returns_Value ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Match_Expression_Returns_Value topic: choice status: verified // verified behavior: Test.describe(...) == "fail" namespace Test enum Code { ok, warn, fail } func describe(c: Code) -> string = match (c: Code) { .ok { "ok" } .warn { "warn" } .fail { "fail" } default { "?" } } // ── ILEmitterTests3__Match_Literal_Int_Patterns ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Match_Literal_Int_Patterns topic: choice status: verified // verified behavior: Test.http(...) == "unknown" namespace Test func http(code: int) -> string = match code { 200 { "ok" } 404 { "not found" } 500 { "server error" } default { "unknown" } } // ── ILEmitterTests3__Match_Literal_String_Patterns ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Match_Literal_String_Patterns topic: choice status: verified // verified behavior: Test.role(...) == 0 namespace Test func role(name: string) -> int = match name { "admin" { 100 } "guest" { 1 } default { 0 } } // ── IndexDiagnosticTests__IndexingChoice_Errors ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: IndexDiagnosticTests.cs::IndexingChoice_Errors topic: choice status: verified // verified behavior: reports diagnostic ES2145 namespace Test choice C { a b(n: int) } func f(c: C) -> int { return c[0] } // ── json ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: json.es topic: choice status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo using "System.Text" // StringBuilder // ═════════════════════════════════════════════════════════════════════════════ // A JSON value, modeled and serialized. The shape of JSON is a recursive sum type — // a value is null, a bool, a number, a string, an array OF values, or an object whose // fields hold values — so it is a `ref choice`: an identity-carrying tagged union whose // cases can recurse through each other (an array holds a `List`, and so on). // // `stringify` is the whole point: one `match` over the six cases, each arm building its // own text, the array/object arms recursing back into `stringify`. This is the canonical // "interpreter over an AST" shape — `match` for dispatch, recursion for structure, a // `StringBuilder` for the O(n) join, string interpolation for the leaves. // ═════════════════════════════════════════════════════════════════════════════ // One key/value entry of an object. A plain value `data` — copied by value, no identity. data Field { key: string value: Json } // The recursive value type. `ref choice` = abstract base + one sealed subclass per case; // the cases reference `Json` (and `List`) freely because each case is a real class. ref choice Json { jnull jbool(value: bool) jnum(value: double) jstr(value: string) jarr(items: List) jobj(fields: List) } // Serialize any value to its JSON text. Exhaustive `match` — every case is handled, so // the compiler needs no trailing fallback. Each single-payload case binds its payload // *transparently*: in `.jbool(b)` the name `b` IS the bool, in `.jarr(items)` `items` IS // the `List` — the case view unwraps to the payload value (and `b.value` / // `items.Count` still work). The array/object arms walk their collection and recurse. // // The transparent binding is pure SUGAR. A single-payload arm `.jbool(b) { … b … }` is // exactly the case view `.jbool(c) { … c.value … }` with the projection elided — both // lower to the identical IL (cast to the variant subclass, then load the one payload // field). So `.jbool(b)` and `.jbool(c)` + `c.value` are interchangeable; the bare name // is just the common case spelled shorter. (A multi-payload case has no single value to // unwrap to, so it keeps the explicit view: `.jobj(c)` then `c.fields`.) func stringify(j: Json) -> string { match j { .jnull { return "null" } .jbool(b) { return b ? "true" : "false" } .jnum(n) { return "{n}" } .jstr(s) { return "\"{s}\"" } .jarr(items) { let sb = StringBuilder() sb.Append("[") var i = 0 while i < items.Count { if i > 0 { sb.Append(",") } sb.Append(stringify(items[i])) // recurse into each element i += 1 } sb.Append("]") return sb.ToString() } .jobj(fields) { let sb = StringBuilder() sb.Append("{") var i = 0 while i < fields.Count { let f = fields[i] if i > 0 { sb.Append(",") } sb.Append("\"{f.key}\":") sb.Append(stringify(f.value)) // recurse into each value i += 1 } sb.Append("}") return sb.ToString() } } } // Build a small document and render it: {"name":"ada","tags":["math","engine"],"active":true} func main() -> string { let tags = List() tags.Add(Json.jstr("math")) tags.Add(Json.jstr("engine")) let fields = List() fields.Add(Field { key: "name", value: Json.jstr("ada") }) fields.Add(Field { key: "tags", value: Json.jarr(tags) }) fields.Add(Field { key: "active", value: Json.jbool(true) }) return stringify(Json.jobj(fields)) } // ── promotion_and_match ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: promotion_and_match.es topic: choice status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo // ═════════════════════════════════════════════════════════════════════════════ // E#'s defining move: behavior lives in FREE FUNCTIONS, but a free function whose // first parameter is a value `data` is PROMOTED to a method on that type. You write // plain procedural code; you get to call it object-style — including chaining. // // func add(v: Vec, o: Vec) -> Vec is the method Vec.add → v.add(o) // // The free-call spelling `add(v, o)` is then a hard error (the compiler points you at // `v.add(o)`). The method travels with its receiver type, so it is reachable wherever // `Vec` is, regardless of which file declared the function. // // This file pairs that with the other half of E#'s type story — a `choice` (a closed // set of shapes) consumed by `match` (exhaustive pattern dispatch). A `choice` is NOT // a `data`, so functions over it are ordinary free functions, never promoted. // ═════════════════════════════════════════════════════════════════════════════ // A 2-D vector. A `data` is value-semantic: copying it copies the fields, and a // function that returns a new `Vec` never disturbs its input. data Vec { x: int y: int } // Each of these has `Vec` as its first parameter, so each is promoted onto `Vec`. // Call them `a.add(b)`, `a.scaled(2)`, `a.dot(b)` — never `add(a, b)`. func add(v: Vec, o: Vec) -> Vec { return Vec { x: v.x + o.x, y: v.y + o.y } } func scaled(v: Vec, k: int) -> Vec { return Vec { x: v.x * k, y: v.y * k } } func dot(v: Vec, o: Vec) -> int { return v.x * o.x + v.y * o.y } // Because `add` and `scaled` return a `Vec`, promoted calls CHAIN: each result is a // fresh value you can call the next method on. No mutation, no aliasing — just values // flowing through transformations. func combine(a: Vec, b: Vec) -> Vec { return a.add(b).scaled(2) } // The other half of the type story: a sum type. Each variant may carry payload fields. choice Shape { point // no payload segment(length: int) // one payload box(w: int, h: int) // two payloads } // `Shape` is a `choice`, not a `data`, so this is a plain free function — call it // `area(s)`. `match` must cover every variant (the compiler warns otherwise); each // arm binds that variant's payloads positionally. func area(s: Shape) -> int { match s { .point { return 0 } .segment(len) { return 0 } // a 1-D segment has no area .box(w, h) { return w * h } } return 0 } func main() -> int { let a = Vec { x: 1, y: 2 } let b = Vec { x: 3, y: 4 } let c = a.combine(b) // (a + b) then *2 → Vec { x: 8, y: 12 } — promoted, so `a.combine(b)` let d = a.dot(b) // 1*3 + 2*4 = 11 // Construct a choice value with its factory, then fold it with `match`. let boxArea = area(Shape.box(3, 5)) // 15 return c.x + c.y + d + boxArea // 8 + 12 + 11 + 15 = 46 } // ── RefChoiceCaseViewTests__MultiPayload_CaseView_StillWorks ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: RefChoiceCaseViewTests.cs::MultiPayload_CaseView_StillWorks topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice E { lit(value: int) add(left: E, right: E) } func eval(e: E) -> int { match e { .lit(v) { return v } .add(n) { return eval(n.left) + eval(n.right) } } } func go() -> int = eval(E.add(E.lit(5), E.lit(7))) // ── RefChoiceCaseViewTests__MultiPayload_Positional_StillWorks ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: RefChoiceCaseViewTests.cs::MultiPayload_Positional_StillWorks topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice E { lit(value: int) add(left: E, right: E) } func eval(e: E) -> int { match e { .lit(v) { return v } .add(l, r) { return eval(l) + eval(r) } } } func go() -> int = eval(E.add(E.lit(8), E.lit(12))) // ── RefChoiceCaseViewTests__SinglePayload_BareName_IsPayloadValue ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: RefChoiceCaseViewTests.cs::SinglePayload_BareName_IsPayloadValue topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice E { lit(value: int) neg(inner: E) } func eval(e: E) -> int { match e { .lit(v) { return v } .neg(inner) { return 0 - eval(inner) } } } func go() -> int = eval(E.lit(42)) // ── RefChoiceCaseViewTests__SinglePayload_BoolTransparent ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: RefChoiceCaseViewTests.cs::SinglePayload_BoolTransparent topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Flag { on(value: bool) off } func read(f: Flag) -> bool { match f { .on(v) { return v } .off { return false } } } func go() -> bool = read(Flag.on(true)) // ── RefChoiceCaseViewTests__SinglePayload_DotField_StillResolves ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: RefChoiceCaseViewTests.cs::SinglePayload_DotField_StillResolves topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Box { full(value: int) empty } func read(b: Box) -> int { match b { .full(c) { return c.value } // c.value still works (aliases the bare binding) .empty { return 0 } } } func go() -> int = read(Box.full(9)) // ── RefChoiceCaseViewTests__SinglePayload_ListTransparent_Indexing ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: RefChoiceCaseViewTests.cs::SinglePayload_ListTransparent_Indexing topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Seq { arr(items: List) empty } func total(s: Seq) -> int { match s { .arr(items) { var sum = 0 var i = 0 while i < items.Count { sum += items[i] i += 1 } return sum } .empty { return 0 } } } func go() -> int { let xs = List() xs.Add(10) xs.Add(20) return total(Seq.arr(xs)) } // ── RefChoiceCaseViewTests__SinglePayload_Recursion_ThroughBareName ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: RefChoiceCaseViewTests.cs::SinglePayload_Recursion_ThroughBareName topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice E { lit(value: int) neg(inner: E) } func eval(e: E) -> int { match e { .lit(v) { return v } .neg(inner) { return 0 - eval(inner) } } } func go() -> int = eval(E.neg(E.lit(7))) // ── TranspilerTests__ExhaustiveMatch_No_Warning_When_Complete ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::ExhaustiveMatch_No_Warning_When_Complete topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Light { red yellow green } func check(l: Light) -> int { match (l: Light) { .red { return 1 } .yellow { return 2 } .green { return 3 } } return 0 } // ── TranspilerTests__ExhaustiveMatch_No_Warning_With_Default ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::ExhaustiveMatch_No_Warning_With_Default topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Light { red yellow green } func check(l: Light) -> int { match (l: Light) { .red { return 1 } default { return 0 } } return 0 } // ── TranspilerTests__ExhaustiveMatch_Warns_Missing_Cases ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::ExhaustiveMatch_Warns_Missing_Cases topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Light { red yellow green } func check(l: Light) -> int { match (l: Light) { .red { return 1 } .yellow { return 2 } } return 0 } // ── TranspilerTests__Transpiles_Generic_Choice ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_Generic_Choice topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Opt choice Option { some(value: T) none } func unwrap(opt: Option, fallback: T) -> T { match (opt: Option) { .some(value) { return value } default { return fallback } } } // ── TranspilerTests__Transpiles_Match_On_Choice_Annotated ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_Match_On_Choice_Annotated topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Conn pub choice ConnectionState { disconnected connected failed(reason: string) } pub func describe(state: ConnectionState) -> string { match (state: ConnectionState) { .disconnected { return "off" } .connected { return "on" } .failed(reason) { return reason } default { return "?" } } } // ── TranspilerTests__Transpiles_Match_On_MemberAccess_Inferred ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_Match_On_MemberAccess_Inferred topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace SM pub choice Status { on, off } pub data Device { status: Status } pub func check(d: Device) -> string { match d.status { .on { return "on" } .off { return "off" } default { return "?" } } } // ── TranspilerTests__Transpiles_Match_Without_Annotation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_Match_Without_Annotation topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Conn pub choice ConnectionState { disconnected connected } pub func describe(state: ConnectionState) -> string { match state { .disconnected { return "off" } .connected { return "on" } default { return "?" } } } // ── TranspilerTests__Transpiles_MultiPayload_Destructuring ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_MultiPayload_Destructuring topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Action { popout(containerId: uint, slotIndex: int) close } func handle(a: Action) -> int { match (a: Action) { .popout(cid, slot) { return 1 } .close { return 0 } } return 0 } // ── TranspilerTests__Transpiles_MultiPayload_RefChoice ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_MultiPayload_RefChoice topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Action { popout(containerId: uint, slotIndex: int) close } // ── TranspilerTests__Transpiles_MultiPayload_ValueChoice ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_MultiPayload_ValueChoice topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice Msg { text(from: string, body: string) ping } // ── TranspilerTests__Transpiles_RefChoice_DotCase ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_RefChoice_DotCase topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Option { some(value: int) none } func make() -> Option { return .some(42) } // ── TranspilerTests__Transpiles_RefChoice_Match ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_RefChoice_Match topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Shape { circle(radius: float) square(side: float) } func describe(s: Shape) -> string { match (s: Shape) { .circle(c) { return "circle" } .square(sq) { return "square" } } return "unknown" } // ── TranspilerTests__Transpiles_RefChoice_SealedHierarchy ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_RefChoice_SealedHierarchy topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test ref choice Expr { literal(value: int) negate(inner: Expr) unit } // ── TranspilerTests__Transpiles_SinglePayload_Destructuring_BackwardCompat ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_SinglePayload_Destructuring_BackwardCompat topic: choice status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test choice State { running(progress: int) stopped } func check(s: State) -> int { match (s: State) { .running(p) { return p } .stopped { return 0 } } return 0 } // ── turtle ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: turtle.es topic: choice status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo // ═════════════════════════════════════════════════════════════════════════════ // A turtle you drive with a CHAIN of commands — the fluent-builder pattern, and the // reason it works. // // The turtle is a `ref data`: it has identity and evolving state (position + heading). // Each command is applied by `apply`, which mutates the turtle and then **returns the // turtle itself**. Because a `ref data` is a reference, returning it hands back the SAME // object — so `apply` can be chained, and you never re-name the receiver: // // Turtle().apply(...).apply(...).apply(...) // // That return-`self` move is the whole trick behind fluent APIs (`StringBuilder`, // LINQ, query builders). Contrast it with a method that returns a `Result` or a fresh // value: those don't chain, because the next call would land on the wrong type. // // The commands themselves show the other half of the language: a `choice` whose // `forward` case carries a step count and whose `turn` case carries an `enum` // direction, dispatched with `match`. One `apply`, two payload shapes, no `if`-ladder. // ═════════════════════════════════════════════════════════════════════════════ // Which way to pivot. A plain `enum` — a closed set of named constants. enum Turn { left right } // What the turtle can be told to do. A `choice`: `forward` carries how far, `turn` // carries which way (the `Turn` enum). `match` in `apply` handles both. choice Command { forward(steps: int) turn(direction: Turn) } // The turtle. `ref data` = identity + mutable state; heading is 0=N, 1=E, 2=S, 3=W, // so a right turn is +1 (mod 4) and a left turn is +3 (mod 4). ref data Turtle { var x: int var y: int var facing: int init() { self.x = 0 self.y = 0 self.facing = 0 // start facing North } } // Apply one command and RETURN THE TURTLE — that return-self is what makes `apply` // chainable. `match` dispatches the command; `forward` walks in the current heading, // `turn` rotates. The nested `match (direction: Turn)` folds the enum to a delta. func apply(t: Turtle, cmd: Command) -> Turtle { match cmd { .forward(steps) { if t.facing == 0 { t.y += steps } // North else if t.facing == 1 { t.x += steps } // East else if t.facing == 2 { t.y -= steps } // South else { t.x -= steps } // West } .turn(direction) { match (direction: Turn) { .left { t.facing = (t.facing + 3) % 4 } .right { t.facing = (t.facing + 1) % 4 } } } } return t // the SAME turtle — the chain continues on it } // Drive the turtle with a single fluent chain — no intermediate `t` rebinding, just // commands flowing through the one object: // // start (0,0) facing N // forward 5 → (0,5) turn right → facing E // forward 3 → (3,5) turn right → facing S // forward 2 → (3,3) // // Final position (3, 3) → encoded as x*100 + y = 303. func main() -> int { let t = Turtle() .apply(Command.forward(5)) .apply(Command.turn(Turn.right())) .apply(Command.forward(3)) .apply(Command.turn(Turn.right())) .apply(Command.forward(2)) return t.x * 100 + t.y // 303 } // ── vending ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: vending.es topic: choice status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo // ═════════════════════════════════════════════════════════════════════════════ // A vending machine — a small stateful protocol, and the one place E# reaches for // the object world on purpose. // // The machine HAS IDENTITY: there is one of it, money accumulates in it, stock // depletes from it, and every command acts on that same evolving thing. That is the // definition of `ref data` — a class with reference semantics — as opposed to the // value `data` used elsewhere in this corpus. Commands arrive as a `choice` (insert a // coin, buy, cancel) and are dispatched with `match`. Coins are an `enum` with // explicit denominations, decoded by a second `match`. Anything the user can get // wrong (too little credit, sold out) is a `Result`, so the caller always gets a // clear outcome instead of a silent failure. // ═════════════════════════════════════════════════════════════════════════════ // Coin denominations, in cents. An `enum` emits as a real CLR enum; the explicit // values are the actual coin worth. enum Coin { nickel = 5 dime = 10 quarter = 25 } // Decode a coin to its cent value. `match` over an enum needs the type named so the // `.nickel` shorthand resolves to `Coin`. func cents(c: Coin) -> int { match (c: Coin) { .nickel { return 5 } .dime { return 10 } .quarter { return 25 } } return 0 } // The three things a customer can do. A `choice`: `insert` carries which coin; the // others carry nothing. choice Command { insert(coin: Coin) buy cancel } // The machine itself — a `ref data`, because it has identity and evolving state. // `var` fields mutate in place; `price` is fixed at construction. ref data Machine { var credit: int var stock: int let price: int init(stock: int, price: int) { self.credit = 0 self.stock = stock self.price = price } } // Apply one command to the machine, mutating it and reporting the outcome. Promoted // onto `Machine` (first parameter), so the caller writes `machine.apply(cmd)`. Each // arm reads/writes the shared state through the reference — the changes persist // across calls because a `ref data` is one object, not a copy. func apply(m: Machine, cmd: Command) -> Result { match cmd { .insert(coin) { m.credit += cents(coin) // coin IS the Coin (transparent view) return ok("credit: {m.credit}c") } .buy { if m.stock == 0 { return error("sold out") } if m.credit < m.price { return error("need {m.price - m.credit}c more") } let change = m.credit - m.price m.credit = 0 // bank the price, return the rest m.stock -= 1 return ok("dispensed — change {change}c, {m.stock} left") } .cancel { let refund = m.credit m.credit = 0 return ok("refunded {refund}c") } } } // A short session against ONE machine: the mutations accumulate because `m` is a // single identity-bearing object, not re-copied each call. Two quarters + a dime is // 60c against a 50c price → dispensed with 10c change. // The application is itself a `ref data` — `Program` OWNS the machine and drives the // session in `main`. A `main` method on a `ref data Program` IS the program's entry // point (the class-style alternative to a bare top-level `func main`); the compiler // constructs the program (`Program()`) and calls `.main()`, so no launcher is needed. ref data Program { func main() -> string { let m = Machine(3, 50) // 3 items, 50c each m.apply(Command.insert(Coin.quarter())) // credit 25 m.apply(Command.insert(Coin.quarter())) // credit 50 m.apply(Command.insert(Coin.dime())) // credit 60 let result = m.apply(Command.buy()) // dispense, 10c change, 2 left return result.IsOk ? result.Value : "error: {result.Error}" } } // ── vm ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: vm.es topic: choice status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo // ═════════════════════════════════════════════════════════════════════════════ // A tiny stack machine — the smallest thing that is honestly a "virtual machine". // // A program is a list of instructions; execution is a loop that walks them, each // one pushing to or popping from an operand stack. This is how real bytecode VMs // (the CLR's own, the JVM, CPython, Lua) work underneath — just with more opcodes. // // The instruction set is a `choice` (a value tagged union): some variants carry a // payload (`push` carries the integer to push), most carry none (`add`, `dup`, …). // Executing one instruction is a `match` over the variant — the canonical use of a // sum type. The operand stack is a `List` used as a LIFO: `Add` is push, // `xs[xs.Count - 1]` is peek, `RemoveAt(xs.Count - 1)` is pop — a `List` from the // BCL, called directly. Anything that can fail (popping an empty stack, dividing by // zero, leaving the wrong number of results) is a `Result`, threaded with `?`. // ═════════════════════════════════════════════════════════════════════════════ // The instruction set. `push` is the only variant with a payload. choice Op { push(value: int) add sub mul div dup // duplicate the top of the stack swap // exchange the top two } // Pop the top of the stack, or fail on underflow. Takes `*List`... actually a // `List` is already a reference type, so it is passed and mutated directly — no // pointer needed. Returns the popped value (or an error), leaving the list shorter. func pop(stack: List) -> Result { if stack.Count == 0 { return error("stack underflow") } let top = stack[stack.Count - 1] stack.RemoveAt(stack.Count - 1) return ok(top) } // Execute one instruction against the stack. `match` covers every variant, so the // dispatch is exhaustive; each arm either pushes, or pops its operands (with `?` // propagating an underflow) and pushes the result. Single-payload `push` binds its // value transparently — `n` IS the int. func step(stack: List, op: Op) -> Result { match op { .push(n) { stack.Add(n) } .add { let b = pop(stack)? let a = pop(stack)? stack.Add(a + b) } .sub { let b = pop(stack)? let a = pop(stack)? stack.Add(a - b) } .mul { let b = pop(stack)? let a = pop(stack)? stack.Add(a * b) } .div { let b = pop(stack)? let a = pop(stack)? if b == 0 { return error("division by zero") } stack.Add(a / b) } .dup { let top = pop(stack)? stack.Add(top) stack.Add(top) } .swap { let b = pop(stack)? let a = pop(stack)? stack.Add(b) stack.Add(a) } } return ok(0) // step's value is unused; it reports success or the error } // Run a whole program: step through every instruction, then require exactly one value // left on the stack — that's the result. Too many or too few is a program error, not a // silent answer. func run(program: List) -> Result { let stack = List() var i = 0 while i < program.Count { step(stack, program[i])? // propagate the first runtime error i += 1 } if stack.Count != 1 { return error("program left {stack.Count} values on the stack, expected 1") } return ok(stack[0]) } // Build and run: (2 + 3) * (10 - 6) == 5 * 4 == 20 // push 2, push 3, add → stack [5] // push 10, push 6, sub → stack [5, 4] // mul → stack [20] // The entry point lives on a `ref data Program` — the OO "an object IS the application" // shape, the class-style alternative to a bare top-level `func main`. A `main` method on // a `ref data Program` IS the program's entry point; the compiler constructs the program // (`Program()`) and calls `.main()`, so no separate launcher function is needed. ref data Program { func main() -> int { let program = List() program.Add(Op.push(2)) program.Add(Op.push(3)) program.Add(Op.add()) program.Add(Op.push(10)) program.Add(Op.push(6)) program.Add(Op.sub()) program.Add(Op.mul()) let r = run(program) return r.IsOk ? r.Value : -1 // 20 } } // ── ILEmitterTests_CaseView__MixedArms_PositionalAndView ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::MixedArms_PositionalAndView topic: choice status: unverified // verified behavior: Test.go(...) == 2 func go() -> int { let p = Pair2.single(2) match (p: Pair2) { .single(s) { return s.x } .couple(a, b) { return a + b } } return -1 } // ── ILEmitterTests_CaseView__NestedView_OneThenInnerMatch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::NestedView_OneThenInnerMatch topic: choice status: unverified // verified behavior: Test.go(...) == "num:7" func desc(tok: Token) -> string { match (tok: Token) { .word(w) { return "w:{w.text}" } .number(n) { return "num:{n.value}" } } return "?" } func go() -> string { let lx = Lex.one(Token.number(7)) match (lx: Lex) { .one(o) { return desc(o.t) } .pair(p) { return "pair" } } return "?" } // ── ILEmitterTests_CaseView__NestedView_PairProjectsTwoTokens ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::NestedView_PairProjectsTwoTokens topic: choice status: unverified // verified behavior: Test.go(...) == "hi/9" func desc(tok: Token) -> string { match (tok: Token) { .word(w) { return w.text } .number(n) { let v = n.value return "{v}" } } return "?" } func go() -> string { let lx = Lex.pair(Token.word("hi"), Token.number(9)) match (lx: Lex) { .one(o) { return "one" } .pair(p) { return "{desc(p.a)}/{desc(p.b)}" } } return "?" } // ── ILEmitterTests_CaseView__NestedView_SameNameShadowing ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::NestedView_SameNameShadowing topic: choice status: unverified // verified behavior: Test.go(...) == 3 func go() -> int { let outer = Pair2.couple(1, 2) match (outer: Pair2) { .single(c) { return c.x } .couple(c) { let inner = Pair2.single(3) match (inner: Pair2) { .single(c) { return c.x } .couple(c) { return c.a } } return -2 } } return -1 } // ── ILEmitterTests_CaseView__Positional_SinglePayload_UsedBare ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::Positional_SinglePayload_UsedBare topic: choice status: unverified // verified behavior: Test.go(...) == 7 func go() -> int { let t = Token.number(7) match (t: Token) { .word(w) { return 0 } .number(n) { return n } } return -1 } // ── ILEmitterTests_CaseView__Positional_ThreePayloads ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::Positional_ThreePayloads topic: choice status: unverified // verified behavior: Test.go(...) == 6 func go() -> int { let t = Triple.only(1, 2, 3) match (t: Triple) { .only(a, b, c) { return a + b + c } } return -1 } // ── ILEmitterTests_CaseView__Positional_TwoPayloads ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::Positional_TwoPayloads topic: choice status: unverified // verified behavior: Test.go(...) == 30 func go() -> int { let p = Pair2.couple(10, 20) match (p: Pair2) { .single(x) { return x } .couple(a, b) { return a + b } } return -1 } // ── ILEmitterTests_CaseView__ResultOfChoice_ViewProjection ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::ResultOfChoice_ViewProjection topic: choice status: unverified // verified behavior: Test.go(...) == 14 func fetch() -> Result = ok(Reply.accepted(7)) func go() -> int { let r = fetch()? match (r: Reply) { .accepted(a) { return a.id * 2 } .rejected(x) { return 0 } } return -1 } // ── ILEmitterTests_CaseView__SinglePayloadView_InInterpolation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::SinglePayloadView_InInterpolation topic: choice status: unverified // verified behavior: Test.go(...) == "v=42" func go() -> string { let p = Pair2.single(42) match (p: Pair2) { .single(s) { return "v={s.x}" } .couple(c) { return "?" } } return "?" } // ── ILEmitterTests_CaseView__View_InMatchExpression ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_InMatchExpression topic: choice status: unverified // verified behavior: Test.go(...) == 11 func go() -> int { let p = Pair2.couple(5, 6) return match (p: Pair2) { .single(s) { s.x } .couple(c) { c.a + c.b } } } // ── ILEmitterTests_CaseView__View_MultiPayload_BothFields ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_MultiPayload_BothFields topic: choice status: unverified // verified behavior: Test.go(...) == 30 func go() -> int { let p = Pair2.couple(10, 20) match (p: Pair2) { .single(x) { return x } .couple(c) { return c.a + c.b } } return -1 } // ── ILEmitterTests_CaseView__View_MultiPayload_FieldA ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_MultiPayload_FieldA topic: choice status: unverified // verified behavior: Test.go(...) == 10 func go() -> int { let p = Pair2.couple(10, 20) match (p: Pair2) { .single(x) { return x } .couple(c) { return c.a } } return -1 } // ── ILEmitterTests_CaseView__View_MultiPayload_FieldB ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_MultiPayload_FieldB topic: choice status: unverified // verified behavior: Test.go(...) == 20 func go() -> int { let p = Pair2.couple(10, 20) match (p: Pair2) { .single(x) { return x } .couple(c) { return c.b } } return -1 } // ── ILEmitterTests_CaseView__View_MultiPayload_FieldOrderIndependent ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_MultiPayload_FieldOrderIndependent topic: choice status: unverified // verified behavior: Test.go(...) == 7 func go() -> int { let p = Pair2.couple(3, 4) match (p: Pair2) { .single(x) { return x } .couple(c) { let b = c.b let a = c.a return a + b } } return -1 } // ── ILEmitterTests_CaseView__View_SinglePayload_ArithmeticOnProjection ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_SinglePayload_ArithmeticOnProjection topic: choice status: unverified // verified behavior: Test.go(...) == 100 func go() -> int { let p = Pair2.single(10) match (p: Pair2) { .single(s) { return s.x * s.x } .couple(c) { return 0 } } return -1 } // ── ILEmitterTests_CaseView__View_SinglePayload_BareUse ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_SinglePayload_BareUse topic: choice status: unverified // verified behavior: Test.go(...) == 9 func go() -> int { let p = Pair2.single(9) match (p: Pair2) { .single(s) { return s } .couple(c) { return 0 } } return -1 } // ── ILEmitterTests_CaseView__View_SinglePayload_FieldAccess ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_SinglePayload_FieldAccess topic: choice status: unverified // verified behavior: Test.go(...) == 9 func go() -> int { let p = Pair2.single(9) match (p: Pair2) { .single(s) { return s.x } .couple(c) { return 0 } } return -1 } // ── ILEmitterTests_CaseView__View_StringPayloadProjection ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_StringPayloadProjection topic: choice status: unverified // verified behavior: Test.go(...) == "hello" func go() -> string { let t = Token.word("hello") match (t: Token) { .word(w) { return w.text } .number(n) { return "n" } } return "?" } // ── ILEmitterTests_CaseView__View_ThreePayloads ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::View_ThreePayloads topic: choice status: unverified // verified behavior: Test.go(...) == 6 func go() -> int { let t = Triple.only(1, 2, 3) match (t: Triple) { .only(v) { return v.a + v.b + v.c } } return -1 } // ── ILEmitterTests_CaseView__ViewProjection_InInterpolation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_CaseView.cs::ViewProjection_InInterpolation topic: choice status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> string { let p = Pair2.couple(5, 6) match (p: Pair2) { .single(s) { return "{s.x}" } .couple(c) { return "x={c.a},y={c.b}" } } return "?" } // ── ILEmitterTests_GenericDerive__GenericEquality_ReturnedFromMatch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_GenericDerive.cs::GenericEquality_ReturnedFromMatch topic: choice status: unverified // verified behavior: Test.go(...) == true func go() -> bool { let a = Pair { first: 2, second: 3 } let b = Pair { first: 2, second: 3 } let eq = a.Equals(b) return match eq { true { true } false { false } } } // ── ILEmitterTests_Integration__Json_ParsesScalarString ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Integration.cs::Json_ParsesScalarString topic: choice status: unverified // verified behavior: Test.go(...) == "hello" func go() -> string { let r = parse("\"hello\"") if r.IsError { return "ERR" } match r.Value { .jstr(node) { return node.s } default { return "?" } } return "?" } // ── TranspilerTests__Transpiles_Data_Choice_And_Functions ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_Data_Choice_And_Functions topic: choice status: unverified // compiles cleanly (no auto-run claim was extracted) namespace Auth pub data LoginRequest { email: string password: string } pub choice AuthError { invalidCredentials accountLocked(untilUtc: DateTimeOffset) } pub func makeRequest(email: string, password: string) -> LoginRequest { let req = LoginRequest { email: email password: password } return req } pub func login(req: LoginRequest) -> Result { if req.password == "secret" { return ok(req) } return error(AuthError.invalidCredentials()) }