Skip to content

Commit 32fc557

Browse files
committed
remove spec def: compile-time callability is inferred. A top-level val body is the only compile-time body; error[spec-handle] becomes error[comptime-handle]; spec tests move to top-level val bodies (neg cval_*), spec_gone checks the parse error
1 parent 05b9c1d commit 32fc557

35 files changed

Lines changed: 110 additions & 148 deletions

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,18 @@ extern def rt_handle {f: File} <at(f, Opened)> (): OpenFile
269269
Generic parameters translate cleanly to `void*` in standard C output.
270270

271271
### 3. Formal Contracts & Proof Hints
272-
srs supports formal specification primitives (`requires`, `ensures`, `invariant`, `decreases`, `proof by induction`, `proof using`) and `spec def` constructs. These facilitate transpilation to Lean 4 for interactive theorem proving.
272+
srs supports formal specification primitives (`requires`, `ensures`, `invariant`, `decreases`, `proof by induction`, `proof using`). A contract names ordinary defs. These facilitate transpilation to Lean 4 for interactive theorem proving.
273273

274274
```scala
275-
spec def size(xs: List): I64
275+
def size(xs: List): I64
276276
ensures result >= 0
277277
proof by induction xs
278278
= xs match
279279
case Nil => 0
280280
case Cons(_, t) => 1 + size(t)
281281
```
282282

283-
**Tests:** [`tests/s2/prog/contracts.srs`](./tests/s2/prog/contracts.srs) (contracts parse, check, and erase in C), [`tests/s2/neg/spec_impure.srs`](./tests/s2/neg/spec_impure.srs) (a spec def calls impure code, rejected).
283+
**Tests:** [`tests/s2/prog/contracts.srs`](./tests/s2/prog/contracts.srs) (contracts parse, check, and erase in C), [`tests/s2/neg/cval_impure.srs`](./tests/s2/neg/cval_impure.srs) (a compile-time body calls impure code, rejected).
284284

285285
---
286286

‎bootstrap/s2c-seed.c‎

Lines changed: 23 additions & 24 deletions
Large diffs are not rendered by default.

‎s2/ast.srs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ enum Ast.Clause:
109109
enum Ast.Body:
110110
case Ast.Def(cs: Coll.List[Ast.Clause], e: Ast.Expr)
111111
case Ast.Extern
112-
// a spec def: a compile-time body, no binder groups (vic 2026-11-05), no Region
113-
case Ast.Spec(cs: Coll.List[Ast.Clause], e: Ast.Expr)
114112

115113
enum Ast.Fn:
116114
case Ast.Fn(line: I64, name: Sym.Sym, groups: Coll.List[Ast.Group], params: Coll.List[Ast.Binder], ret: Ast.Ty, exit: Coll.List[Ast.Atom], body: Ast.Body)
@@ -564,7 +562,6 @@ def Ast.fn(f: Ast.Fn, ind: I64): Unit =
564562
val Ast.Fn(_, _, _, _, _, _, body) = f
565563
body match
566564
case Ast.Extern => Console.print("extern ")
567-
case Ast.Spec(_, _) => Console.print("spec ")
568565
case Ast.Def(_, _) => ()
569566
Ast.fnSig(f, ind)
570567

@@ -598,7 +595,6 @@ def Ast.fnSig(f: Ast.Fn, ind: I64): Unit =
598595
body match
599596
case Ast.Extern => ()
600597
case Ast.Def(cs, e) => Ast.withClauses(cs, e, ind)
601-
case Ast.Spec(cs, e) => Ast.withClauses(cs, e, ind)
602598

603599
// = body; with clauses: each on its own line one step in, then = at the start of a line (as tests/s2/prog/contracts.srs)
604600
def Ast.withClauses(cs: Coll.List[Ast.Clause], e: Ast.Expr, ind: I64): Unit = cs match

‎s2/eff.srs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def Eff.decls(cx: Eff.Cx, ds: Coll.List[Ast.Decl], w: Eff.W): Coll.List[D.Fact]
216216
Eff.decls(cx, rest, Eff.hfns(cx, hn, hfs, Eff.W(n, fs, Coll.Nil, Coll.sEmpty())))
217217
case _ => Eff.decls(cx, rest, w)
218218

219-
// decl(F, P) for each {} binder of a protocol; the body of a run def (a spec def is pure: it has no row)
219+
// decl(F, P) for each {} binder of a protocol; the body of a run def
220220
// n: the subject, the def itself or its handler
221221
def Eff.fn(cx: Eff.Cx, n: Sym.Sym, f: Ast.Fn, w: Eff.W): Eff.W =
222222
val Ast.Fn(_, _, gs, ps, _, _, body) = f

‎s2/elab.srs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ enum El.Param:
4343
enum El.Fn:
4444
case El.Fn(line: I64, name: Sym.Sym, tparams: Coll.List[Sym.Sym], caps: Coll.List[Sym.Sym], params: Coll.List[El.Param], ret: K.Term, body: El.Body)
4545

46-
// a def has C, an extern is a C symbol of the runtime, a spec body is compile time only (no C)
46+
// a def has C, an extern is a C symbol of the runtime
4747
enum El.Body:
4848
case El.Def(e: El.Expr)
4949
case El.Extern
50-
case El.Spec(e: El.Expr)
5150

5251
// handler name for protocol proto: its fns (init, then the ops)
5352
enum El.Handler:
@@ -58,7 +57,7 @@ enum El.Handler:
5857
enum El.Given:
5958
case El.Given(tparams: Coll.List[Sym.Sym], ev: Coll.List[Sym.Sym], head: K.Term, fns: Coll.List[El.Fn])
6059

61-
// where a constructor allocates: in the Region capability region, or nowhere at run time (a spec body: compile time only)
60+
// where a constructor allocates: in the Region capability region, or nowhere at run time (a top-level val: compile time only)
6261
enum El.Alloc:
6362
case El.In(region: Sym.Sym)
6463
case El.Comptime

‎s2/eval.srs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def Ev.call(cx: Ev.Cx, f: Sym.Sym, env: Coll.List[Ev.Bind], fuel: I64, line: I64
131131
val env3 = Ev.bindAll(ps, vs, Coll.Nil)
132132
body match
133133
case Ast.Def(_, b) => Ev.result(Ev.expr(cx, g, env3, fuel2 - 1, b), env2)
134-
case Ast.Spec(_, b) => Ev.result(Ev.expr(cx, g, env3, fuel2 - 1, b), env2)
135134
case Ast.Extern => Ev.extern(line, g, vs, env2, fuel2)
136135

137136
// the callee's value with the caller's locals

‎s2/flow.srs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ def Flow.step(st: Flow.St, p: I64, c: Flow.Cap, op: Sym.Sym, line: I64): Flow.At
432432
Flow.At(Flow.add(st3, Flow.fLine(s, line)), q)
433433

434434
// a constructor call (written with args: a nullary case takes no ()) is a request on Region: an alloc step on the
435-
// innermost Region in scope. none here: the def's implicit Region (its row, s2/eff.srs), owned by a caller. not yet: a
436-
// spec def allocates nothing (the s2 Ast has no spec marker)
435+
// innermost Region in scope. none here: the def's implicit Region (its row, s2/eff.srs), owned by a caller
437436
def Flow.alloc(st: Flow.St, p: I64, line: I64): Flow.At = Flow.request(st, p, Coll.Some(Eff.Req(Sym.of("Region"), Sym.of("alloc"))), line)
438437

439438
// a request P.op(...): a step of the innermost capability of P in scope (none: the implicit one, not tracked here)
@@ -1019,7 +1018,6 @@ def Flow.facts(cx: Flow.Cx, f: Ast.Fn): Coll.List[D.Fact] =
10191018
val walked: Flow.At = body match
10201019
case Ast.Extern => Flow.At(st1, 0)
10211020
case Ast.Def(_, e) => Flow.expr(cx, st1, 0, e)
1022-
case Ast.Spec(_, e) => Flow.expr(cx, st1, 0, e)
10231021
val Flow.At(st2, e) = walked
10241022
val st3 = Flow.worlds(ps, st2, Coll.rev(caps), bs, atoms, exit, as, 0, Flow.Exit(e))
10251023
val st4 = as match

‎s2/hash.srs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ def Hs.fn(cx: Hs.Cx, f: Ast.Fn): Hs.Dg =
213213
body match
214214
case Ast.Extern => Hs.add(g3, 70)
215215
case Ast.Def(cs, e) => Hs.clauses(cx, env2, Hs.expr(cx, env2, Hs.add(g3, 71), e), cs)
216-
case Ast.Spec(cs, e) => Hs.clauses(cx, env2, Hs.expr(cx, env2, Hs.add(g3, 72), e), cs)
217216

218217
// an enum: its groups, then each case's fields (the field and case names are metadata: positions only)
219218
def Hs.enum_(cx: Hs.Cx, e: Ast.Enum): Hs.Dg =
@@ -251,7 +250,6 @@ def Hs.refs(d: Hs.D, acc: Coll.List[Sym.Sym]): Coll.List[Sym.Sym] = d match
251250
val Ast.Fn(_, _, _, _, _, _, body) = f
252251
body match
253252
case Ast.Def(_, e) => Hs.eRefs(e, acc)
254-
case Ast.Spec(_, e) => Hs.eRefs(e, acc)
255253
case Ast.Extern => acc
256254
case Hs.DE(e) =>
257255
val Ast.Enum(_, _, _, cases) = e

‎s2/names.srs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ def Names.fn(f: Ast.Fn, outer: Coll.List[Sym.Sym], st: Names.St): Names.St =
242242
body match
243243
case Ast.Extern => st1
244244
case Ast.Def(cs, e) => Names.expr(e, locals, tps, Names.clauses(cs, locals, tps, st1))
245-
case Ast.Spec(cs, e) => Names.expr(e, locals, tps, Names.clauses(cs, locals, tps, st1))
246245

247246
// the types of the {} binders (a {} binder names a trait or a protocol)
248247
def Names.groupTys(gs: Coll.List[Ast.Group], tps: Coll.List[Sym.Sym], st: Names.St): Names.St = gs match

‎s2/parse.srs‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def Parse.decls(ts: Coll.List[Lex.Tok], acc: Coll.List[Ast.Decl]): Parse.R[Coll.
9494

9595
def Parse.decl(ts: Coll.List[Lex.Tok]): Parse.R[Ast.Decl] =
9696
if Parse.isKw(ts, "def") then Parse.wrapFn(Parse.fn(Parse.next(ts), Parse.line(ts), Parse.FDef))
97-
else if Parse.isKw(ts, "extern") || Parse.isKw(ts, "spec") then
97+
else if Parse.isKw(ts, "extern") then
9898
Parse.kw(Parse.next(ts), "def") match
9999
case Parse.Err(l, w) => Parse.Err(l, w)
100-
case Parse.Ok(_, t1) => Parse.wrapFn(Parse.fn(t1, Parse.line(ts), if Parse.isKw(ts, "spec") then Parse.FSpec else Parse.FExtern))
100+
case Parse.Ok(_, t1) => Parse.wrapFn(Parse.fn(t1, Parse.line(ts), Parse.FExtern))
101101
else if Parse.isKw(ts, "enum") then Parse.enumDecl(Parse.next(ts), Parse.line(ts))
102102
else if Parse.isKw(ts, "protocol") then Parse.proto(Parse.next(ts), Parse.line(ts))
103103
else if Parse.isKw(ts, "handler") then Parse.handler(Parse.next(ts), Parse.line(ts))
@@ -234,11 +234,10 @@ def Parse.wrapFn(rf: Parse.R[Ast.Fn]): Parse.R[Ast.Decl] = rf match
234234
case Parse.Err(l, w) => Parse.Err(l, w)
235235
case Parse.Ok(f, t1) => Parse.Ok(Ast.DFn(f), t1)
236236

237-
// which def: a def with a body, an extern def (or a trait method: no body), a spec def (a body, no binder groups)
237+
// which def: a def with a body, an extern def (or a trait method: no body)
238238
enum Parse.FnKind:
239239
case Parse.FDef
240240
case Parse.FExtern
241-
case Parse.FSpec
242241

243242
// def name groups (params) groups : ty [<atoms>] [= rhs]
244243
def Parse.fn(ts: Coll.List[Lex.Tok], line: I64, k: Parse.FnKind): Parse.R[Ast.Fn] = Parse.ident(ts) match
@@ -257,18 +256,13 @@ def Parse.fn(ts: Coll.List[Lex.Tok], line: I64, k: Parse.FnKind): Parse.R[Ast.Fn
257256
case Parse.Err(l, w) => Parse.Err(l, w)
258257
case Parse.Ok(exit, t7) => Parse.fnBody(t7, Ast.Fn(line, name, gs, ps, ret, exit, Ast.Extern), k)
259258

260-
// the body of a def by its kind; a spec def with binder groups is an error (vic 2026-11-05)
261-
def Parse.fnBody(ts: Coll.List[Lex.Tok], f: Ast.Fn, k: Parse.FnKind): Parse.R[Ast.Fn] =
262-
val Ast.Fn(line, name, gs, ps, ret, exit, _) = f
263-
k match
264-
case Parse.FExtern => Parse.Ok(f, ts)
265-
case Parse.FDef => Parse.bodyOf(ts, f, false)
266-
case Parse.FSpec => gs match
267-
case Coll.Nil => Parse.bodyOf(ts, f, true)
268-
case Coll.Cons(_, _) => Parse.Err(line, "a spec def without binder groups")
259+
// the body of a def by its kind
260+
def Parse.fnBody(ts: Coll.List[Lex.Tok], f: Ast.Fn, k: Parse.FnKind): Parse.R[Ast.Fn] = k match
261+
case Parse.FExtern => Parse.Ok(f, ts)
262+
case Parse.FDef => Parse.bodyOf(ts, f)
269263

270264
// [NL IND clauses DED] = rhs (s1/parse.srs:88-89: the clauses sit between the signature and =)
271-
def Parse.bodyOf(ts: Coll.List[Lex.Tok], f: Ast.Fn, spec: Bool): Parse.R[Ast.Fn] =
265+
def Parse.bodyOf(ts: Coll.List[Lex.Tok], f: Ast.Fn): Parse.R[Ast.Fn] =
272266
val Ast.Fn(line, name, gs, ps, ret, exit, _) = f
273267
val rc: Parse.R[Coll.List[Ast.Clause]] = if Parse.isNl(ts) && Parse.isIndent(Parse.next(ts)) then Parse.clauses(Parse.next(Parse.next(ts)), Coll.Nil) else Parse.Ok(Coll.Nil, ts)
274268
rc match
@@ -277,7 +271,7 @@ def Parse.bodyOf(ts: Coll.List[Lex.Tok], f: Ast.Fn, spec: Bool): Parse.R[Ast.Fn]
277271
case Parse.Err(l, w) => Parse.Err(l, w)
278272
case Parse.Ok(_, t1) => Parse.rhs(t1) match
279273
case Parse.Err(l, w) => Parse.Err(l, w)
280-
case Parse.Ok(e, t2) => Parse.Ok(Ast.Fn(line, name, gs, ps, ret, exit, if spec then Ast.Spec(cs, e) else Ast.Def(cs, e)), t2)
274+
case Parse.Ok(e, t2) => Parse.Ok(Ast.Fn(line, name, gs, ps, ret, exit, Ast.Def(cs, e)), t2)
281275

282276
// clauses until DED, one per line
283277
def Parse.clauses(ts: Coll.List[Lex.Tok], acc: Coll.List[Ast.Clause]): Parse.R[Coll.List[Ast.Clause]] =

0 commit comments

Comments
 (0)