Actually check assertions.

This commit is contained in:
Nigel Tao committed 2017-05-20 12:34:02 +10:00
1 parent 06eaadb1d6
commit 5bc5e43335
3 files changed
+184 -34

No files matched your search

+134 -2
View File
@@ -4,14 +4,46 @@
package check
import (
"errors"
"fmt"
"math/big"
a "github.com/google/puffs/lang/ast"
t "github.com/google/puffs/lang/token"
)
// binds returns fact's lower or upper bound for id.
func binds(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
type facts []*a.Expr
func (z *facts) appendFact(x *a.Expr) {
// TODO: make this faster than O(N) by keeping facts sorted somehow?
for _, f := range *z {
if f.Eq(x) {
return
}
}
*z = append(*z, x)
}
func (z facts) refine(n *a.Expr, min *big.Int, max *big.Int) (*big.Int, *big.Int) {
if n.ID0() != 0 || !n.ID1().IsIdent() {
// TODO.
return min, max
}
for _, f := range z {
f0, f1 := refine(f, n.ID1())
if f0 != nil && min.Cmp(f0) < 0 {
min = f0
}
if f1 != nil && max.Cmp(f1) > 0 {
max = f1
}
}
return min, max
}
// refine returns fact's lower or upper bound for id.
func refine(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
op := fact.ID0()
if !op.IsBinaryOp() || op.Key() == t.KeyAs {
return nil, nil
@@ -62,3 +94,103 @@ func binds(fact *a.Expr, id t.ID) (*big.Int, *big.Int) {
return nil, nil
}
func argValue(m *t.IDMap, args []*a.Node, name string) *a.Expr {
if x := m.ByName(name); x != 0 {
for _, a := range args {
if a.Arg().Name() == x {
return a.Arg().Value()
}
}
}
return nil
}
// parseBinaryOp parses n as "lhs op rhs".
func parseBinaryOp(n *a.Expr) (op t.Key, lhs *a.Expr, rhs *a.Expr) {
if !n.ID0().IsBinaryOp() {
return 0, nil, nil
}
op = n.ID0().Key()
if op == t.KeyAs {
return 0, nil, nil
}
return op, n.LHS().Expr(), n.RHS().Expr()
}
func proveBinaryOpConstValues(op t.Key, lMin *big.Int, lMax *big.Int, rMin *big.Int, rMax *big.Int) (ok bool) {
switch op {
case t.KeyXBinaryNotEq:
return lMax.Cmp(rMin) < 0 || lMin.Cmp(rMax) > 0
case t.KeyXBinaryLessThan:
return lMax.Cmp(rMin) < 0
case t.KeyXBinaryLessEq:
return lMax.Cmp(rMin) <= 0
case t.KeyXBinaryEqEq:
return lMin.Cmp(rMax) == 0 && lMax.Cmp(rMin) == 0
case t.KeyXBinaryGreaterEq:
return lMin.Cmp(rMax) >= 0
case t.KeyXBinaryGreaterThan:
return lMin.Cmp(rMax) > 0
}
return false
}
func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) (ok bool) {
if cv := lhs.ConstValue(); cv != nil {
rMin, rMax, err := q.bcheckExpr(rhs, 0)
if err != nil {
// TODO: should this function return the error?
return false
}
if proveBinaryOpConstValues(op, cv, cv, rMin, rMax) {
return true
}
}
if cv := rhs.ConstValue(); cv != nil {
lMin, lMax, err := q.bcheckExpr(lhs, 0)
if err != nil {
// TODO: should this function return the error?
return false
}
if proveBinaryOpConstValues(op, lMin, lMax, cv, cv) {
return true
}
}
for _, f := range q.facts {
if f.ID0().Key() == op && f.LHS().Expr().Eq(lhs) && f.RHS().Expr().Eq(rhs) {
return true
}
}
return false
}
var errFailed = errors.New("failed")
type reason func(q *checker, n *a.Assert) error
type reasonMap map[t.Key]reason
var reasons = [...]struct {
s string
r reason
}{
{`"a < b: a < c <= b"`, func(q *checker, n *a.Assert) error {
c := argValue(q.idMap, n.Args(), "c")
if c == nil {
return errFailed
}
op, a, b := parseBinaryOp(n.Condition())
if op != t.KeyXBinaryLessThan {
return errFailed
}
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.idMap), c.String(q.idMap))
}
if !q.proveBinaryOp(t.KeyXBinaryLessEq, c, b) {
return fmt.Errorf("cannot prove \"%s <= %s\"", c.String(q.idMap), b.String(q.idMap))
}
return nil
}},
}
+25 -18
View File
@@ -53,8 +53,27 @@ func (c *checker) bcheckStatement(n *a.Node) error {
switch n.Kind() {
case a.KAssert:
// TODO: check the assertion.
c.facts = append(c.facts, n.Assert().Condition())
o := n.Assert()
condition := o.Condition()
err := errFailed
if reasonID := o.Reason(); reasonID != 0 {
if reasonFunc := c.reasonMap[reasonID.Key()]; reasonFunc != nil {
err = reasonFunc(c, o)
} else {
err = fmt.Errorf("no such reason %s", reasonID.String(c.idMap))
}
} else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs {
if c.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr()) {
err = nil
}
}
if err != nil {
if err == errFailed {
return fmt.Errorf("check: cannot prove %q", condition.String(c.idMap))
}
return fmt.Errorf("check: cannot prove %q: %v", condition.String(c.idMap), err)
}
c.facts.appendFact(condition)
case a.KAssign:
o := n.Assign()
@@ -162,9 +181,9 @@ func (c *checker) bcheckWhile(n *a.While) error {
if m.Assert().Keyword().Key() == t.KeyPost {
continue
}
c.facts = append(c.facts, m.Assert().Condition())
c.facts.appendFact(m.Assert().Condition())
}
c.facts = append(c.facts, n.Condition())
c.facts.appendFact(n.Condition())
for _, m := range n.Body() {
if err := c.bcheckStatement(m); err != nil {
return err
@@ -179,7 +198,7 @@ func (c *checker) bcheckWhile(n *a.While) error {
if m.Assert().Keyword().Key() == t.KeyPre {
continue
}
c.facts = append(c.facts, m.Assert().Condition())
c.facts.appendFact(m.Assert().Condition())
}
return nil
}
@@ -199,19 +218,7 @@ func (c *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error
if err != nil {
return nil, nil, err
}
if n.ID0() == 0 && n.ID1().IsIdent() {
for _, f := range c.facts {
f0, f1 := binds(f, n.ID1())
if f0 != nil && n0.Cmp(f0) < 0 {
n0 = f0
}
if f1 != nil && n1.Cmp(f1) > 0 {
n1 = f1
}
}
} else {
// TODO: propagate facts about e.g. "this.foo".
}
n0, n1 = c.facts.refine(n, n0, n1)
return n0, n1, nil
case t.FlagsUnaryOp:
return c.bcheckExprUnaryOp(n, depth)
+25 -14
View File
@@ -82,11 +82,19 @@ func Check(m *t.IDMap, files ...*a.File) (*Checker, error) {
}
}
c := &Checker{
idMap: m,
structs: map[t.ID]Struct{},
funcs: map[t.QID]Func{},
rMap := reasonMap{}
for _, r := range reasons {
if id := m.ByName(r.s); id != 0 {
rMap[id.Key()] = r.r
}
}
c := &Checker{
idMap: m,
reasonMap: rMap,
structs: map[t.ID]Struct{},
funcs: map[t.QID]Func{},
}
for _, phase := range phases {
for _, f := range files {
for _, n := range f.TopLevelDecls() {
@@ -115,9 +123,10 @@ var phases = [...]struct {
}
type Checker struct {
idMap *t.IDMap
structs map[t.ID]Struct
funcs map[t.QID]Func
idMap *t.IDMap
reasonMap reasonMap
structs map[t.ID]Struct
funcs map[t.QID]Func
}
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
@@ -261,9 +270,10 @@ func (c *Checker) checkFuncContract(n *a.Node) error {
func (c *Checker) checkFuncBody(n *a.Node) error {
f := n.Func()
q := &checker{
c: c,
idMap: c.idMap,
f: c.funcs[f.QID()],
c: c,
idMap: c.idMap,
reasonMap: c.reasonMap,
f: c.funcs[f.QID()],
}
// Fill in the TypeMap with all local variables. Note that they have
@@ -329,12 +339,13 @@ func (c *Checker) checkFuncBody(n *a.Node) error {
}
type checker struct {
c *Checker
idMap *t.IDMap
f Func
c *Checker
idMap *t.IDMap
reasonMap reasonMap
f Func
errFilename string
errLine uint32
facts []*a.Expr
facts facts
}