From 47da01233bd036b1f82f854dc088807f8c67b677 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 1 Jun 2017 11:37:29 +1000 Subject: [PATCH] Use "inv etc" not "assert etc" for invariants. --- lang/ast/ast.go | 4 ++-- lang/check/bounds.go | 20 ++++++++++---------- lang/check/check_test.go | 2 ++ lang/parse/parse.go | 15 +++++++++------ lang/token/list.go | 11 +++++++---- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 65735d2f..49b0268f 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -219,8 +219,8 @@ func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mh } } -// Assert is "assert RHS via ID1(args)", "pre etc" or "post etc": -// - ID0: +// Assert is "assert RHS via ID1(args)", "pre etc", "inv etc" or "post etc": +// - ID0: // - ID1: reason // - RHS: // - List0: reason arguments diff --git a/lang/check/bounds.go b/lang/check/bounds.go index b6de58ca..1ad93b77 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -263,7 +263,7 @@ func (q *checker) bcheckWhile(n *a.While) error { return err } - // Check the pre and assert conditions on entry. + // Check the pre and inv conditions on entry. for _, o := range n.Asserts() { if o.Assert().Keyword().Key() == t.KeyPost { continue @@ -271,11 +271,11 @@ func (q *checker) bcheckWhile(n *a.While) error { // TODO } - // Check the post conditions on exit, assuming only the pre and assert - // conditions and the inverted while condition. + // Check the post conditions on exit, assuming only the pre and inv + // (invariant) conditions and the inverted while condition. // - // We don't need to check the assert conditions, even though we add them to - // the facts after the while loop, since we have already proven each assert + // We don't need to check the inv conditions, even though we add them to + // the facts after the while loop, since we have already proven each inv // condition on entry, and below, proven them on each explicit continue and // on the implicit continue after the body. if cv := n.Condition().ConstValue(); cv != nil && cv.Cmp(one) == 0 { @@ -308,7 +308,7 @@ func (q *checker) bcheckWhile(n *a.While) error { // We effectively have a "while false { etc }" loop. There's no need to // check the body. } else { - // Assume the pre and assert conditions, and the while condition. Check + // Assume the pre and inv conditions, and the while condition. Check // the body. q.facts = q.facts[:0] for _, o := range n.Asserts() { @@ -323,7 +323,7 @@ func (q *checker) bcheckWhile(n *a.While) error { return err } } - // Check the pre and assert conditions on the implicit continue after the + // Check the pre and inv conditions on the implicit continue after the // body. for _, o := range n.Asserts() { if o.Assert().Keyword().Key() == t.KeyPost { @@ -332,12 +332,12 @@ func (q *checker) bcheckWhile(n *a.While) error { // TODO } - // TODO: check the pre and assert conditions for each continue. + // TODO: check the pre and inv conditions for each continue. - // TODO: check the assert and post conditions for each break. + // TODO: check the inv and post conditions for each break. } - // Assume the assert and post conditions. + // Assume the inv and post conditions. q.facts = q.facts[:0] for _, o := range n.Asserts() { if o.Assert().Keyword().Key() == t.KeyPre { diff --git a/lang/check/check_test.go b/lang/check/check_test.go index a2712cfa..8b52c029 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -71,6 +71,8 @@ func TestCheck(t *testing.T) { assert true while p == q, + pre true, + inv true, post p != q, { } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index b715b0d9..b551faa9 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -395,25 +395,28 @@ func (p *parser) parseBlock() ([]*a.Node, error) { } func (p *parser) assertsSorted(asserts []*a.Node) error { - seenAssert, seenPost := false, false + seenInv, seenPost := false, false for _, a := range asserts { switch a.Assert().Keyword().Key() { + case t.KeyAssert: + return fmt.Errorf(`parse: assertion chain cannot contain "assert", `+ + `only "pre", "inv" and "post" at %s:%d`, p.filename, p.line()) case t.KeyPre: - if seenPost || seenAssert { + if seenPost || seenInv { break } continue - case t.KeyAssert: + case t.KeyInv: if seenPost { break } - seenAssert = true + seenInv = true continue default: seenPost = true continue } - return fmt.Errorf(`parse: assertion chain not in "pre", "assert", "post" order at %s:%d`, + return fmt.Errorf(`parse: assertion chain not in "pre", "inv", "post" order at %s:%d`, p.filename, p.line()) } return nil @@ -421,7 +424,7 @@ func (p *parser) assertsSorted(asserts []*a.Node) error { func (p *parser) parseAssertNode() (*a.Node, error) { switch x := p.peek1(); x.Key() { - case t.KeyAssert, t.KeyPre, t.KeyPost: + case t.KeyAssert, t.KeyPre, t.KeyInv, t.KeyPost: p.src = p.src[1:] condition, err := p.parseExpr() if err != nil { diff --git a/lang/token/list.go b/lang/token/list.go index 579d40f1..cc6103a2 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -195,6 +195,7 @@ const ( KeyVar = Key(IDVar >> KeyShift) KeyIdeal = Key(IDIdeal >> KeyShift) KeyPre = Key(IDPre >> KeyShift) + KeyInv = Key(IDInv >> KeyShift) KeyPost = Key(IDPost >> KeyShift) KeyVia = Key(IDVia >> KeyShift) KeyPub = Key(IDPub >> KeyShift) @@ -323,10 +324,11 @@ const ( IDVar = ID(0x6B<