Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 111 additions & 15 deletions s2/ast.srs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ enum Ast.Caps:
enum Ast.Ty:
case Ast.Ty(line: I64, name: Sym.Sym, args: Coll.List[Ast.Ty], caps: Ast.Caps, vals: Coll.List[Ast.Expr])

enum Ast.Q:
case Ast.Forall
case Ast.Exists

enum Ast.LoopCond:
case Ast.LInvariant
case Ast.LDecreases

enum Ast.Expr:
case Ast.Int(line: I64, v: I64)
case Ast.Bool(line: I64, v: Bool)
Expand All @@ -48,6 +56,8 @@ enum Ast.Expr:
case Ast.Block(line: I64, stmts: Coll.List[Ast.Stmt], last: Ast.Expr)
case Ast.While(line: I64, c: Ast.Expr, body: Ast.Expr)
case Ast.Assign(line: I64, x: Sym.Sym, e: Ast.Expr)
// forall x: T => e, exists x: T => e (contracts only)
case Ast.Quant(line: I64, q: Ast.Q, x: Sym.Sym, t: Ast.Ty, e: Ast.Expr)

enum Ast.Pat:
case Ast.PCase(c: Sym.Sym, binds: Coll.List[Sym.Sym])
Expand All @@ -61,6 +71,8 @@ enum Ast.Stmt:
case Ast.Var(line: I64, x: Sym.Sym, ann: Coll.Option[Ast.Ty], e: Ast.Expr)
case Ast.Handle(line: I64, x: Sym.Sym, h: Sym.Sym, args: Coll.List[Ast.Expr])
case Ast.Do(e: Ast.Expr)
// a loop invariant or measure in a block (s1/parse.srs:494)
case Ast.Check(line: I64, k: Ast.LoopCond, e: Ast.Expr)

// <> atoms. a name is a variable or a constant by its position (s1/facts.srs:1-3: lowercase in a state position is a
// variable, but `knock` in next(s, knock, s2) is an op): the checker decides, not the parser
Expand All @@ -80,11 +92,25 @@ enum Ast.Group:
case Ast.Evidence(bs: Coll.List[Ast.Binder])
case Ast.Facts(atoms: Coll.List[Ast.Atom])

// a contract clause between a signature and its body (C13, s1/parse.srs:147-184), erased in C
enum Ast.Cond:
case Ast.Requires
case Ast.Ensures
case Ast.Returns
case Ast.Decreases
case Ast.Invariant

enum Ast.Clause:
case Ast.CondC(line: I64, k: Ast.Cond, e: Ast.Expr)
case Ast.ProofUsing(line: I64, xs: Coll.List[Sym.Sym])
case Ast.ProofInduction(line: I64, x: Sym.Sym)
case Ast.ProofLean(line: I64, s: Str)

enum Ast.Body:
case Ast.Def(e: Ast.Expr)
case Ast.Def(cs: Coll.List[Ast.Clause], e: Ast.Expr)
case Ast.Extern
// a spec def: a compile-time body, no binder groups (vic 2026-11-05), no Region
case Ast.Spec(e: Ast.Expr)
case Ast.Spec(cs: Coll.List[Ast.Clause], e: Ast.Expr)

enum Ast.Fn:
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)
Expand Down Expand Up @@ -265,6 +291,7 @@ def Ast.bindOf {r: Region} (e: Ast.Expr): Ast.Bind = e match
case Ast.Block(_, _, _) => Ast.Loose
case Ast.While(_, _, _) => Ast.Loose
case Ast.Assign(_, _, _) => Ast.Loose
case Ast.Quant(_, _, _, _, _) => Ast.Loose
case _ => Ast.Atomic

def Ast.fits {r: Region} (e: Ast.Expr, n: Ast.Need): Bool = n match
Expand Down Expand Up @@ -346,7 +373,7 @@ def Ast.expr {r: Region} (e: Ast.Expr, ind: I64): Unit = e match
Ast.expr(c, ind)
rt_print(" then")
Ast.body(t, ind)
Ast.elseBody(x, ind)
Ast.elseBody(t, x, ind)
case Ast.Match(_, s, arms) =>
Ast.operand(s, Ast.AtLeast(Ast.Atomic), ind)
rt_print(" match")
Expand All @@ -358,8 +385,21 @@ def Ast.expr {r: Region} (e: Ast.Expr, ind: I64): Unit = e match
case Ast.While(_, c, b) =>
rt_print("while ")
Ast.expr(c, ind)
rt_print(" do")
// a match condition ends with its arms: do starts the next line (as tests/contracts.srs)
c match
case Ast.Match(_, _, _) =>
Ast.line(ind)
rt_print("do")
case _ => rt_print(" do")
Ast.body(b, ind)
case Ast.Quant(_, q, x, t, b) =>
rt_print(Ast.qText(q))
rt_print(" ")
Ast.sym(x)
rt_print(": ")
Ast.ty(t)
rt_print(" => ")
Ast.expr(b, ind)
case Ast.Assign(_, x, v) =>
Ast.sym(x)
rt_print(" = ")
Expand All @@ -370,15 +410,24 @@ def Ast.unText(op: Ast.UnOp): Str = op match
case Ast.Not => "!"

// `else` on the line of a one-line then, on its own line after a block then
def Ast.elseBody {r: Region} (x: Ast.Expr, ind: I64): Unit = x match
case Ast.Block(_, _, _) =>
// else starts a new line when either branch takes more than one line (a block, a match, a multi-line if or while):
// after one, a same-line else would read as part of its last line
def Ast.elseBody {r: Region} (t: Ast.Expr, x: Ast.Expr, ind: I64): Unit =
if Ast.multiLine(t) || Ast.multiLine(x) then
Ast.line(ind)
rt_print("else")
Ast.body(x, ind)
case _ =>
else
rt_print(" else")
Ast.body(x, ind)

def Ast.multiLine(e: Ast.Expr): Bool = e match
case Ast.Block(_, _, _) => true
case Ast.Match(_, _, _) => true
case Ast.While(_, _, _) => true
case Ast.If(_, _, t, x) => Ast.multiLine(t) || Ast.multiLine(x)
case _ => false

def Ast.arms {r: Region} (xs: Coll.List[Ast.Arm], ind: I64): Unit = xs match
case Coll.Nil => ()
case Coll.Cons(a, t) =>
Expand Down Expand Up @@ -425,6 +474,18 @@ def Ast.stmt {r: Region} (s: Ast.Stmt, ind: I64): Unit = s match
Ast.args(xs, ind)
rt_print(")")
case Ast.Do(e) => Ast.expr(e, ind)
case Ast.Check(_, k, e) =>
rt_print(Ast.loopText(k))
rt_print(" ")
Ast.expr(e, ind)

def Ast.qText(q: Ast.Q): Str = q match
case Ast.Forall => "forall"
case Ast.Exists => "exists"

def Ast.loopText(k: Ast.LoopCond): Str = k match
case Ast.LInvariant => "invariant"
case Ast.LDecreases => "decreases"

def Ast.binding {r: Region} (x: Sym.Sym, ann: Coll.Option[Ast.Ty], e: Ast.Expr, ind: I64): Unit =
Ast.sym(x)
Expand Down Expand Up @@ -501,8 +562,8 @@ def Ast.fn {r: Region} (f: Ast.Fn, ind: I64): Unit =
val Ast.Fn(_, _, _, _, _, _, body) = f
body match
case Ast.Extern => rt_print("extern ")
case Ast.Spec(_) => rt_print("spec ")
case Ast.Def(_) => ()
case Ast.Spec(_, _) => rt_print("spec ")
case Ast.Def(_, _) => ()
Ast.fnSig(f, ind)

// a trait method signature: a def without body and without extern
Expand Down Expand Up @@ -534,12 +595,47 @@ def Ast.fnSig {r: Region} (f: Ast.Fn, ind: I64): Unit =
rt_print(">")
body match
case Ast.Extern => ()
case Ast.Def(e) =>
rt_print(" =")
Ast.body(e, ind)
case Ast.Spec(e) =>
rt_print(" =")
Ast.body(e, ind)
case Ast.Def(cs, e) => Ast.withClauses(cs, e, ind)
case Ast.Spec(cs, e) => Ast.withClauses(cs, e, ind)

// = body; with clauses: each on its own line one step in, then = at the start of a line (as tests/contracts.srs)
def Ast.withClauses {r: Region} (cs: Coll.List[Ast.Clause], e: Ast.Expr, ind: I64): Unit = cs match
case Coll.Nil =>
rt_print(" =")
Ast.body(e, ind)
case Coll.Cons(_, _) =>
Ast.clauses(cs, ind + Ast.step())
Ast.line(ind)
rt_print("=")
Ast.body(e, ind)

def Ast.condText(k: Ast.Cond): Str = k match
case Ast.Requires => "requires"
case Ast.Ensures => "ensures"
case Ast.Returns => "returns"
case Ast.Decreases => "decreases"
case Ast.Invariant => "invariant"

def Ast.clauses {r: Region} (cs: Coll.List[Ast.Clause], ind: I64): Unit = cs match
case Coll.Nil => ()
case Coll.Cons(c, rest) =>
Ast.line(ind)
c match
case Ast.CondC(_, k, x) =>
rt_print(Ast.condText(k))
rt_print(" ")
Ast.expr(x, ind)
case Ast.ProofUsing(_, xs) =>
rt_print("proof using ")
Ast.syms(xs, ", ")
case Ast.ProofInduction(_, x) =>
rt_print("proof by induction ")
Ast.sym(x)
case Ast.ProofLean(_, s) =>
rt_print("proof lean \"")
rt_print(s)
rt_print("\"")
Ast.clauses(rest, ind)

def Ast.fns {r: Region} (xs: Coll.List[Ast.Fn], ind: I64): Unit = xs match
case Coll.Nil => ()
Expand Down
5 changes: 3 additions & 2 deletions s2/flow.srs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ def Flow.stmt {r: Region} (cx: Flow.Cx, st: Flow.St, p: I64, s: Ast.Stmt): Flow.
case Ast.Val(_, x, _, e) => Flow.bindVal(cx, st, p, x, e)
case Ast.Var(_, x, _, e) => Flow.bindVal(cx, st, p, x, e)
case Ast.Do(e) => Flow.expr(cx, st, p, e)
case Ast.Check(_, _, e) => Flow.expr(cx, st, p, e)
case Ast.Handle(line, x, h, xs) =>
val Flow.At(st1, pa) = Flow.exprs(cx, st, p, xs)
Flow.protoOf(Flow.cxHs(cx), h) match
Expand Down Expand Up @@ -989,8 +990,8 @@ def Flow.facts {r: Region} (cx: Flow.Cx, f: Ast.Fn): Coll.List[D.Fact] =
val st1 = Flow.worlds(ps, st0, Coll.rev(caps), bs, atoms, exit, as, 0, Flow.Entry)
val walked: Flow.At = body match
case Ast.Extern => Flow.At(st1, 0)
case Ast.Def(e) => Flow.expr(cx, st1, 0, e)
case Ast.Spec(e) => Flow.expr(cx, st1, 0, e)
case Ast.Def(_, e) => Flow.expr(cx, st1, 0, e)
case Ast.Spec(_, e) => Flow.expr(cx, st1, 0, e)
val Flow.At(st2, e) = walked
val st3 = Flow.worlds(ps, st2, Coll.rev(caps), bs, atoms, exit, as, 0, Flow.Exit(e))
val st4 = as match
Expand Down
20 changes: 18 additions & 2 deletions s2/names.srs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ def Names.fn {r: Region} (f: Ast.Fn, outer: Coll.List[Sym.Sym], st: Names.St): N
val locals = Names.binderNames(params, Names.evidenceNames(gs, Coll.Nil))
body match
case Ast.Extern => st1
case Ast.Def(e) => Names.expr(e, locals, st1)
case Ast.Spec(e) => Names.expr(e, locals, st1)
case Ast.Def(cs, e) => Names.expr(e, locals, Names.clauses(cs, locals, st1))
case Ast.Spec(cs, e) => Names.expr(e, locals, Names.clauses(cs, locals, st1))

// the types of the {} binders (a {} binder names a trait or a protocol)
def Names.groupTys {r: Region} (gs: Coll.List[Ast.Group], tps: Coll.List[Sym.Sym], st: Names.St): Names.St = gs match
Expand All @@ -256,6 +256,20 @@ def Names.evidenceNames {r: Region} (gs: Coll.List[Ast.Group], acc: Coll.List[Sy
case Ast.Types(_) => Names.evidenceNames(rest, acc)
case Ast.Facts(_) => Names.evidenceNames(rest, acc)

// contract clauses: their expressions see the params and result (s1 binds result in an ensures)
def Names.clauses {r: Region} (cs: Coll.List[Ast.Clause], ls: Coll.List[Sym.Sym], st: Names.St): Names.St = cs match
case Coll.Nil => st
case Coll.Cons(c, rest) => c match
case Ast.CondC(_, _, e) => Names.clauses(rest, ls, Names.expr(e, Coll.Cons(Sym.of("result"), ls), st))
case Ast.ProofUsing(line, xs) => Names.clauses(rest, ls, Names.useAll(xs, line, st))
case Ast.ProofInduction(_, _) => Names.clauses(rest, ls, st)
case Ast.ProofLean(_, _) => Names.clauses(rest, ls, st)

// proof using f, g: each is a use of a def
def Names.useAll {r: Region} (xs: Coll.List[Sym.Sym], line: I64, st: Names.St): Names.St = xs match
case Coll.Nil => st
case Coll.Cons(x, rest) => Names.useAll(rest, line, Names.add(st, Names.fUseCall(x, line)))

// a body: a call or a name that is not local is a use of the call namespace (a def, a case, a nullary case)
def Names.expr {r: Region} (e: Ast.Expr, ls: Coll.List[Sym.Sym], st: Names.St): Names.St = e match
case Ast.Name(line, x) => if Names.member(x, ls) then st else Names.add(st, Names.fUseCall(x, line))
Expand All @@ -266,6 +280,7 @@ def Names.expr {r: Region} (e: Ast.Expr, ls: Coll.List[Sym.Sym], st: Names.St):
case Ast.If(_, c, t, x) => Names.expr(x, ls, Names.expr(t, ls, Names.expr(c, ls, st)))
case Ast.While(_, c, b) => Names.expr(b, ls, Names.expr(c, ls, st))
case Ast.Assign(_, _, x) => Names.expr(x, ls, st)
case Ast.Quant(_, _, x, t, b) => Names.expr(b, Coll.Cons(x, ls), Names.ty(t, Coll.Nil, st))
case Ast.Match(_, s, arms) => Names.arms(arms, ls, Names.expr(s, ls, st))
case Ast.Block(_, ss, last) => Names.block(ss, last, ls, st)
case _ => st
Expand Down Expand Up @@ -296,6 +311,7 @@ def Names.block {r: Region} (ss: Coll.List[Ast.Stmt], last: Ast.Expr, ls: Coll.L
case Ast.Var(_, x, ann, e) => Names.block(rest, last, Coll.Cons(x, ls), Names.annTy(ann, Names.expr(e, ls, st)))
case Ast.Handle(line, x, h, xs) => Names.block(rest, last, Coll.Cons(x, ls), Names.exprs(xs, ls, Names.add(st, Names.fTyRef(h, line))))
case Ast.Do(e) => Names.block(rest, last, ls, Names.expr(e, ls, st))
case Ast.Check(_, _, e) => Names.block(rest, last, ls, Names.expr(e, ls, st))

def Names.annTy {r: Region} (ann: Coll.Option[Ast.Ty], st: Names.St): Names.St = ann match
case Coll.None => st
Expand Down
70 changes: 67 additions & 3 deletions s2/parse.srs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,59 @@ def Parse.fnBody {r: Region} (ts: Coll.List[Lex.Tok], f: Ast.Fn, k: Parse.FnKind
case Coll.Nil => Parse.bodyOf(ts, f, true)
case Coll.Cons(_, _) => Parse.Err(line, "a spec def without binder groups")

// [NL IND clauses DED] = rhs (s1/parse.srs:88-89: the clauses sit between the signature and =)
def Parse.bodyOf {r: Region} (ts: Coll.List[Lex.Tok], f: Ast.Fn, spec: Bool): Parse.R[Ast.Fn] =
val Ast.Fn(line, name, gs, ps, ret, exit, _) = f
Parse.op(ts, "=") match
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)
rc match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t1) => Parse.rhs(t1) match
case Parse.Ok(cs, t0) => Parse.op(t0, "=") match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(e, t2) => Parse.Ok(Ast.Fn(line, name, gs, ps, ret, exit, if spec then Ast.Spec(e) else Ast.Def(e)), t2)
case Parse.Ok(_, t1) => Parse.rhs(t1) match
case Parse.Err(l, w) => Parse.Err(l, w)
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)

// clauses until DED, one per line
def Parse.clauses {r: Region} (ts: Coll.List[Lex.Tok], acc: Coll.List[Ast.Clause]): Parse.R[Coll.List[Ast.Clause]] =
if Parse.isDedent(ts) then Parse.Ok(Coll.rev(acc), Parse.next(ts))
else Parse.clause(ts) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(c, t1) => Parse.clauses(Parse.endLine(t1), Coll.Cons(c, acc))

// requires|ensures|returns|decreases|invariant expr | proof using f, ... | proof by induction x | proof lean "..."
def Parse.clause {r: Region} (ts: Coll.List[Lex.Tok]): Parse.R[Ast.Clause] =
val line = Parse.line(ts)
if Parse.isKw(ts, "proof") then Parse.proof(Parse.next(ts), line)
else Parse.condOf(Parse.peek(ts)) match
case Coll.None => Parse.Err(line, "requires, ensures, returns, decreases, invariant or proof")
case Coll.Some(k) => Parse.expr(Parse.next(ts)) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(e, t1) => Parse.Ok(Ast.CondC(line, k, e), t1)

def Parse.condOf {r: Region} (t: Lex.Tok): Coll.Option[Ast.Cond] =
if Parse.kwIs(t, "requires") then Coll.Some(Ast.Requires)
else if Parse.kwIs(t, "ensures") then Coll.Some(Ast.Ensures)
else if Parse.kwIs(t, "returns") then Coll.Some(Ast.Returns)
else if Parse.kwIs(t, "decreases") then Coll.Some(Ast.Decreases)
else if Parse.kwIs(t, "invariant") then Coll.Some(Ast.Invariant)
else Coll.None

def Parse.proof {r: Region} (ts: Coll.List[Lex.Tok], line: I64): Parse.R[Ast.Clause] =
if Parse.isKw(ts, "using") then
Parse.nameLine(Parse.next(ts), Coll.Nil) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(xs, t1) => Parse.Ok(Ast.ProofUsing(line, xs), t1)
else if Parse.isKw(ts, "by") then
Parse.kw(Parse.next(ts), "induction") match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t1) => Parse.ident(t1) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(x, t2) => Parse.Ok(Ast.ProofInduction(line, x), t2)
else Parse.kw(ts, "lean") match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t1) => Parse.peek(t1) match
case Lex.TStr(s, _) => Parse.Ok(Ast.ProofLean(line, s), Parse.next(t1))
case _ => Parse.Err(Parse.line(t1), "a string of Lean tactics")

// binder groups in written order: [binders] {binders} <atoms>; acc holds them reversed
def Parse.groups {r: Region} (ts: Coll.List[Lex.Tok], acc: Coll.List[Ast.Group]): Parse.R[Coll.List[Ast.Group]] =
Expand Down Expand Up @@ -547,6 +593,10 @@ def Parse.patVal {r: Region} (ts: Coll.List[Lex.Tok], vline: I64, line: I64, acc
def Parse.stmt {r: Region} (ts: Coll.List[Lex.Tok]): Parse.R[Ast.Stmt] =
if Parse.isKw(ts, "val") || Parse.isKw(ts, "var") then Parse.valStmt(Parse.next(ts), Parse.line(ts), Parse.isKw(ts, "var"))
else if Parse.isKw(ts, "handle") then Parse.handleStmt(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "invariant") || Parse.isKw(ts, "decreases") then
Parse.expr(Parse.next(ts)) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(e, t1) => Parse.Ok(Ast.Check(Parse.line(ts), if Parse.isKw(ts, "invariant") then Ast.LInvariant else Ast.LDecreases, e), t1)
else Parse.expr(ts) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(e, t1) => Parse.Ok(Ast.Do(e), t1)
Expand Down Expand Up @@ -583,6 +633,7 @@ def Parse.handleStmt {r: Region} (ts: Coll.List[Lex.Tok], line: I64): Parse.R[As
def Parse.expr {r: Region} (ts: Coll.List[Lex.Tok]): Parse.R[Ast.Expr] =
if Parse.isKw(ts, "if") then Parse.ifExpr(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "while") then Parse.whileExpr(Parse.next(ts), Parse.line(ts))
else if Parse.isKw(ts, "forall") || Parse.isKw(ts, "exists") then Parse.quant(Parse.next(ts), Parse.line(ts), if Parse.isKw(ts, "forall") then Ast.Forall else Ast.Exists)
else Parse.peek(ts) match
case Lex.TIdent(x, line) =>
if Parse.opIs(Parse.second(ts), "=") then
Expand All @@ -592,6 +643,19 @@ def Parse.expr {r: Region} (ts: Coll.List[Lex.Tok]): Parse.R[Ast.Expr] =
else Parse.matchOf(Parse.binary(ts, 0))
case _ => Parse.matchOf(Parse.binary(ts, 0))

// forall x: ty => expr, exists x: ty => expr (s1/parse.srs:544-548)
def Parse.quant {r: Region} (ts: Coll.List[Lex.Tok], line: I64, q: Ast.Q): Parse.R[Ast.Expr] = Parse.ident(ts) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(x, t1) => Parse.op(t1, ":") match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t2) => Parse.ty(t2) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(t, t3) => Parse.op(t3, "=>") match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(_, t4) => Parse.expr(t4) match
case Parse.Err(l, w) => Parse.Err(l, w)
case Parse.Ok(b, t5) => Parse.Ok(Ast.Quant(line, q, x, t, b), t5)

// e match NL IND (case pat => rhs)* DED: match is postfix on a whole binary expression
def Parse.matchOf {r: Region} (re: Parse.R[Ast.Expr]): Parse.R[Ast.Expr] = re match
case Parse.Err(l, w) => Parse.Err(l, w)
Expand Down
Loading
Loading