// ── ILEmitterTests__Result_OkAndError_RoundTrips ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::Result_OkAndError_RoundTrips topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func getOk() -> Result { return ok(42) } func getErr() -> Result { return error("bad") } // ── ILEmitterTests__TryUnwrap_PropagatesError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::TryUnwrap_PropagatesError topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func inner() -> Result { return error("fail") } func outer() -> Result { let x = inner()? return ok(x + 1) } // ── ILEmitterTests__TryUnwrap_UnwrapsOk ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests.cs::TryUnwrap_UnwrapsOk topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func inner() -> Result { return ok(10) } func outer() -> Result { let x = inner()? return ok(x + 1) } // ── ILEmitterTests_Coverage_Choice__Result_ErrorShortCircuits ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::Result_ErrorShortCircuits topic: result status: verified // verified behavior: Test.go(...) == 99 namespace Test func half(n: int) -> Result { if n % 2 != 0 { return error("odd") } return ok(n / 2) } func chain(n: int) -> Result { let a = half(n)? let b = half(a)? return ok(b) } func go() -> int { let r = chain(6) return r.IsError ? 99 : r.UnwrapOr(0) } // ── ILEmitterTests_Coverage_Choice__Result_OkPropagation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Choice.cs::Result_OkPropagation topic: result status: verified // verified behavior: Test.go(...) == 2 namespace Test func half(n: int) -> Result { if n % 2 != 0 { return error("odd") } return ok(n / 2) } func chain(n: int) -> Result { let a = half(n)? let b = half(a)? return ok(b) } func go() -> int { let r = chain(8) return r.UnwrapOr(0 - 1) } // ── ILEmitterTests_Coverage_Errors__Combinator_Unwrap_Ok ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Combinator_Unwrap_Ok topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func f() -> Result { return ok(7) } func go() -> int { return f().Unwrap() } // ── ILEmitterTests_Coverage_Errors__Combinator_UnwrapOr_Error ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Combinator_UnwrapOr_Error topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func f() -> Result { return error("bad") } func go() -> int { return f().UnwrapOr(-1) } // ── ILEmitterTests_Coverage_Errors__Combinator_UnwrapOr_Ok ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Combinator_UnwrapOr_Ok topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func f() -> Result { return ok(42) } func go() -> int { return f().UnwrapOr(-1) } // ── ILEmitterTests_Coverage_Errors__Pipeline_MiddleStageFails ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Pipeline_MiddleStageFails topic: result status: verified // verified behavior: Test.go(...) == "non-positive" namespace Test func parse(s: string) -> Result { if s == "" { return error("empty") } return ok(int.Parse(s)) } func positive(n: int) -> Result { if n <= 0 { return error("non-positive") } return ok(n) } func run(s: string) -> Result { let a = parse(s)? let b = positive(a)? return ok(b) } func go() -> string { let r = run("0") return r.IsError ? r.Error : "ok" } // ── ILEmitterTests_Coverage_Errors__Pipeline_ThreeStageSuccess ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Pipeline_ThreeStageSuccess topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func parse(s: string) -> Result { if s == "" { return error("empty") } return ok(int.Parse(s)) } func positive(n: int) -> Result { if n <= 0 { return error("non-positive") } return ok(n) } func doubleIt(n: int) -> Result { return ok(n * 2) } func run(s: string) -> Result { let a = parse(s)? let b = positive(a)? let c = doubleIt(b)? return ok(c + 0) } func go() -> int { let r = run("13") return r.IsOk ? r.Value : -1 } // ── ILEmitterTests_Coverage_Errors__Propagation_ChainsThroughSuccess ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_ChainsThroughSuccess topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func parse(s: string) -> Result { if s == "" { return error("empty") } return ok(int.Parse(s)) } func chain(s: string) -> Result { let n = parse(s)? return ok(n + 1) } func go() -> int { let r = chain("42") return r.IsOk ? r.Value : -1 } // ── ILEmitterTests_Coverage_Errors__Propagation_SecondStageErrorWins ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_SecondStageErrorWins topic: result status: verified // verified behavior: Test.go(...) == "zero" namespace Test func step1(n: int) -> Result { return ok(n) } func step2(n: int) -> Result { if n == 0 { return error("zero") } return ok(n + 10) } func pipeline(n: int) -> Result { let a = step1(n)? let b = step2(a)? return ok(b) } func go() -> string { let r = pipeline(0) return r.IsError ? r.Error : "ok" } // ── ILEmitterTests_Coverage_Errors__Propagation_ShortCircuitsOnError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_ShortCircuitsOnError topic: result status: verified // verified behavior: Test.go(...) == "empty" namespace Test func parse(s: string) -> Result { if s == "" { return error("empty") } return ok(int.Parse(s)) } func chain(s: string) -> Result { let n = parse(s)? return ok(n + 1) } func go() -> string { let r = chain("") return r.IsError ? r.Error : "ok" } // ── ILEmitterTests_Coverage_Errors__Propagation_TwoStageChain ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Propagation_TwoStageChain topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func step1(n: int) -> Result { if n == 0 { return error("zero") } return ok(n * 2) } func step2(n: int) -> Result { return ok(n + 10) } func pipeline(n: int) -> Result { let a = step1(n)? let b = step2(a)? return ok(b) } func go() -> int { let r = pipeline(5) return r.IsOk ? r.Value : -1 } // ── ILEmitterTests_Coverage_Errors__Result_AccumulateOverList ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_AccumulateOverList topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func parse(s: string) -> Result { return ok(int.Parse(s)) } func sumAll(items: List) -> Result { var total = 0 for s in items { let n = parse(s)? total += n } return ok(total) } func go() -> int { let xs = ["1", "2", "3"] let r = sumAll(xs) return r.IsOk ? r.Value : -1 } // ── ILEmitterTests_Coverage_Errors__Result_BranchOnInvalid ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_BranchOnInvalid topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func validate(n: int) -> Result { if n < 0 { return error("negative") } return ok(n) } func go() -> int { let r = validate(-5) if r.IsOk { return r.Value } return -1 } // ── ILEmitterTests_Coverage_Errors__Result_BranchOnValidation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_BranchOnValidation topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func validate(n: int) -> Result { if n < 0 { return error("negative") } return ok(n) } func go() -> int { let r = validate(7) if r.IsOk { return r.Value } return -1 } // ── ILEmitterTests_Coverage_Errors__Result_DataPayloadThroughPropagation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_DataPayloadThroughPropagation topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test data User { id: int, score: int } func load(id: int) -> Result { if id == 0 { return error("missing") } return ok(User { id: id, score: 7 }) } func scoreOf(id: int) -> Result { let u = load(id)? return ok(u.score) } func go() -> int { let r = scoreOf(5) return r.IsOk ? r.Value : -1 } // ── ILEmitterTests_Coverage_Errors__Result_EarlyErrorStopsAccumulation ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_EarlyErrorStopsAccumulation topic: result status: verified // verified behavior: Test.go(...) == "bad" namespace Test func parse(s: string) -> Result { if s == "x" { return error("bad") } return ok(int.Parse(s)) } func sumAll(items: List) -> Result { var total = 0 for s in items { let n = parse(s)? total += n } return ok(total) } func go() -> string { let xs = ["1", "x", "3"] let r = sumAll(xs) return r.IsError ? r.Error : "ok" } // ── ILEmitterTests_Coverage_Errors__Result_ErrorCarriesError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_ErrorCarriesError topic: result status: verified // verified behavior: Test.go(...) == "bad" namespace Test func parse(s: string) -> Result { return error("bad") } func go() -> string { let r = parse("x") return r.IsError ? r.Error : "ok" } // ── ILEmitterTests_Coverage_Errors__Result_IsErrorTrueOnError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_IsErrorTrueOnError topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func f() -> Result { return error("e") } func go() -> bool { return f().IsError } // ── ILEmitterTests_Coverage_Errors__Result_IsOkTrueOnOk ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_IsOkTrueOnOk topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func f() -> Result { return ok(1) } func go() -> bool { return f().IsOk } // ── ILEmitterTests_Coverage_Errors__Result_MapErrorMessageManually ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_MapErrorMessageManually topic: result status: verified // verified behavior: Test.go(...) == "wrapped:empty" namespace Test func parse(s: string) -> Result { if s == "" { return error("empty") } return ok(int.Parse(s)) } func wrap(s: string) -> Result { let r = parse(s) if r.IsError { return error("wrapped:{r.Error}") } return ok(r.Value) } func go() -> string { let r = wrap("") return r.IsError ? r.Error : "ok" } // ── ILEmitterTests_Coverage_Errors__Result_OkCarriesDataValue ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_OkCarriesDataValue topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test data User { id: int, age: int } func load(id: int) -> Result { if id == 0 { return error("missing") } return ok(User { id: id, age: 30 }) } func go() -> int { let r = load(1) if r.IsOk { return r.Value.age } return -1 } // ── ILEmitterTests_Coverage_Errors__Result_OkCarriesValue ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_OkCarriesValue topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func parse(s: string) -> Result { return ok(42) } func go() -> int { let r = parse("x") return r.IsOk ? r.Value : -1 } // ── ILEmitterTests_Coverage_Errors__Result_StoredAndReread ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_StoredAndReread topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func produce(n: int) -> Result { return ok(n + 3) } func go() -> int { let r = produce(30) let s = produce(r.Value) return s.IsOk ? r.Value : -1 } // ── ILEmitterTests_Coverage_Errors__Result_VoidLikeOkUnit ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests_Coverage_Errors.cs::Result_VoidLikeOkUnit topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Test func validate(n: int) -> Result { if n > 0 { return ok(n) } return error("non-positive") } func go() -> int { let r = validate(5) return r.IsOk ? 1 : 0 } // ── ILEmitterTests3__Result_Err_Construction ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Result_Err_Construction topic: result status: verified // verified behavior: Test.test(...) == true namespace Test func produce(n: int) -> Result { if n < 0 { return error("negative") } return ok(n) } func test() -> bool { let r = produce(-1) return r.IsError } // ── ILEmitterTests3__Result_IsOk_Branch ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Result_IsOk_Branch topic: result status: verified // verified behavior: Test.test(...) == -1 namespace Test func produce(b: bool) -> Result { if b { return ok(7) } return error("bad") } func test(b: bool) -> int { let r = produce(b) if r.IsOk { return r.Value } return -1 } // ── ILEmitterTests3__Result_Map_Chain_Pin ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Result_Map_Chain_Pin topic: result status: verified // verified behavior: Test.test(...) == 42 namespace Test func parse(s: string) -> Result { if s == "" { return error("empty") } return ok(42) } func test() -> int { let r = parse("hello") return r.Value } // ── ILEmitterTests3__Result_Ok_Construction_And_Unwrap ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ILEmitterTests3.cs::Result_Ok_Construction_And_Unwrap topic: result status: verified // verified behavior: Test.test(...) == 42 namespace Test func produce(n: int) -> Result = ok(n) func test() -> int { let r = produce(42) return r.Value } // ── money ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: money.es topic: result status: verified // hand-authored, idiomatic E# — verified through the E# compiler namespace Demo // ═════════════════════════════════════════════════════════════════════════════ // Money done right: a fixed-point amount in integer cents, never a float. // // Money is the textbook case for value semantics. An amount has no identity — two // $5.00 bills are interchangeable — and you never want one assignment to mutate // another ledger entry by surprise. So `Money` is a `data`: copied on assignment, // equal by its field, no shared mutation. And it's stored as `cents: int`, because // binary floating point cannot represent 0.10 exactly and money math must be exact. // // Every operation is a free function whose first parameter is `Money`, so by // promotion it reads as a method on the value — `a.plus(b)`, `price.times(3)`, // `total.format()` — and because each returns a fresh `Money` (or a `Result`), the // calls chain without ever mutating the input. Division can fail (zero divisor), so // it returns `Result` and the `?` operator threads the failure. // ═════════════════════════════════════════════════════════════════════════════ // A value amount. One field, integer cents. No constructor — value `data` is built // with a composite literal; a factory gives a readable surface. data Money { cents: int } // Factories. `dollars(5)` → $5.00; `of(12, 34)` → $12.34. func dollars(d: int) -> Money = Money { cents: d * 100 } func of(d: int, c: int) -> Money = Money { cents: d * 100 + c } // --- promoted arithmetic (first param `Money` ⇒ method on `Money`) ---------------- // Each returns a new value; the receiver is never touched. func plus(a: Money, b: Money) -> Money = Money { cents: a.cents + b.cents } func minus(a: Money, b: Money) -> Money = Money { cents: a.cents - b.cents } func times(a: Money, factor: int) -> Money = Money { cents: a.cents * factor } func isNegative(a: Money) -> bool = a.cents < 0 // Splitting a bill N ways can fail (N <= 0), so it speaks `Result`. Integer division // floors, so the remainder cents are handed back too — no money is silently lost. func splitEvenly(a: Money, ways: int) -> Result { if ways <= 0 { return error("cannot split into {ways} shares") } return ok(Money { cents: a.cents / ways }) } // Render as "$D.CC", padding the cents and handling a negative amount cleanly. func format(a: Money) -> string { let neg = a.cents < 0 let abs = neg ? 0 - a.cents : a.cents let d = abs / 100 let c = abs % 100 let cc = c < 10 ? "0{c}" : "{c}" let sign = neg ? "-" : "" return "{sign}${d}.{cc}" } // A small end-to-end calculation: three items, a discount, split two ways. // The `?` after splitEvenly unwraps the ok value or returns the error from here. func checkout() -> Result { let apple = of(0, 99) // $0.99 let bread = of(2, 49) // $2.49 let coffee = dollars(8) // $8.00 let subtotal = apple.plus(bread).plus(coffee) // chained, no mutation: $11.48 let discount = of(1, 48) // $1.48 off let total = subtotal.minus(discount) // $10.00 let perPerson = total.splitEvenly(2)? // $5.00, or propagate the error return ok("total {total.format()}, each {perPerson.format()}") } func main() -> string { let r = checkout() return r.IsOk ? r.Value : "error: {r.Error}" // "total $10.00, each $5.00" } // ── TranspilerTests__Transpiles_TryUnwrap_In_Let ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: TranspilerTests.cs::Transpiles_TryUnwrap_In_Let topic: result status: verified // compiles cleanly (no auto-run claim was extracted) namespace Prop pub func inner(x: int) -> Result { return ok(x) } pub func outer(x: int) -> Result { let val = inner(x)? return ok(val) } // ── ErrorPropagationTests__Question_AsBareStatement_Error_Propagates ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_AsBareStatement_Error_Propagates topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> Result { parse(-1)? return ok(99) } // ── ErrorPropagationTests__Question_AsBareStatement_Ok_Continues ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_AsBareStatement_Ok_Continues topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> Result { parse(5)? return ok(99) } // ── ErrorPropagationTests__Question_AsCallArgument ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_AsCallArgument topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func add(a: int, b: int) -> int = a + b func go() -> Result { return ok(add(parse(3)?, 100)) // 6 + 100 } // ── ErrorPropagationTests__Question_ChainedStatements_StopAtFirstError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_ChainedStatements_StopAtFirstError topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> Result { parse(1)? // ok, discarded parse(-9)? // error here — propagates, the rest never runs return ok(0) } // ── ErrorPropagationTests__Question_InLetPosition_Ok ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_InLetPosition_Ok topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> Result { let x = parse(5)? return ok(x + 1) } // ── ErrorPropagationTests__Question_InReturnPosition_Ok ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_InReturnPosition_Ok topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> Result { return ok(parse(5)? + 1) } // ── ErrorPropagationTests__Question_InReturnPosition_PropagatesError ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_InReturnPosition_PropagatesError topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> Result { return ok(parse(-3)? + 1) } // ── ErrorPropagationTests__Question_Nested ── // E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript). // provenance: ErrorPropagationTests.cs::Question_Nested topic: result status: unverified // compiles cleanly (no auto-run claim was extracted) func go() -> Result { return ok(parse(parse(3)?)?) }