Update facts after "x += y".

This commit is contained in:
Nigel Tao committed 2017-06-02 10:28:46 +10:00
1 parent a0a9975815
commit 51da37c661
3 files changed
+43 -14

No files matched your search

+13 -12
View File
@@ -24,13 +24,14 @@ func (z *facts) appendFact(x *a.Expr) {
*z = append(*z, x)
}
// drop drops any facts that mention x. They are replaced with a nil *a.Expr,
// and then the slice is compacted to remove all nils.
func (z *facts) drop(x *a.Expr) {
// update applies f to each fact, replacing the slice element with the result
// of the function call. The slice is then compacted to remove all nils.
func (z *facts) update(f func(*a.Expr) *a.Expr) {
i := 0
for _, f := range *z {
if !f.Mentions(x) {
(*z)[i] = f
for _, x := range *z {
x = f(x)
if x != nil {
(*z)[i] = x
i++
}
}
@@ -123,12 +124,12 @@ func argValue(m *t.IDMap, args []*a.Node, name string) *a.Expr {
}
// parseBinaryOp parses n as "lhs op rhs".
func parseBinaryOp(n *a.Expr) (op t.Key, lhs *a.Expr, rhs *a.Expr) {
func parseBinaryOp(n *a.Expr) (op t.ID, lhs *a.Expr, rhs *a.Expr) {
if !n.ID0().IsBinaryOp() {
return 0, nil, nil
}
op = n.ID0().Key()
if op == t.KeyAs {
op = n.ID0()
if op.Key() == t.KeyAs {
return 0, nil, nil
}
return op, n.LHS().Expr(), n.RHS().Expr()
@@ -219,11 +220,11 @@ var reasons = [...]struct {
}{
{`"a < (b + c): a < c; 0 <= b"`, func(q *checker, n *a.Assert) error {
op, a, bc := parseBinaryOp(n.Condition())
if op != t.KeyXBinaryLessThan {
if op.Key() != t.KeyXBinaryLessThan {
return errFailed
}
op, b, c := parseBinaryOp(bc)
if op != t.KeyXBinaryPlus {
if op.Key() != t.KeyXBinaryPlus {
return errFailed
}
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
@@ -240,7 +241,7 @@ var reasons = [...]struct {
return errFailed
}
op, a, b := parseBinaryOp(n.Condition())
if op != t.KeyXBinaryLessThan {
if op.Key() != t.KeyXBinaryLessThan {
return errFailed
}
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
+29 -2
View File
@@ -220,13 +220,40 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
// TODO: check lhs and rhs are pure expressions.
if op == t.IDEq {
// Drop any facts involving lhs.
q.facts.drop(lhs)
q.facts.update(func(x *a.Expr) *a.Expr {
if x.Mentions(lhs) {
return nil
}
return x
})
o := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryEqEq, 0, lhs.Node(), nil, rhs.Node(), nil)
o.SetMType(lhs.MType())
q.facts.appendFact(o)
} else {
// TODO: update any facts involving lhs.
// Update any facts involving lhs.
q.facts.update(func(x *a.Expr) *a.Expr {
xOp, xLHS, xRHS := parseBinaryOp(x)
if xOp == 0 || !xLHS.Eq(lhs) {
if x.Mentions(lhs) {
return nil
}
return x
}
if xRHS.Mentions(lhs) {
return nil
}
switch op.Key() {
case t.KeyPlusEq:
// TODO: fold constants, so that ((x + 1) + 1) becomes (x + 2)?
oRHS := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryPlus, 0, xRHS.Node(), nil, rhs.Node(), nil)
oRHS.SetMType(xRHS.MType())
o := a.NewExpr(a.FlagsTypeChecked, xOp, 0, xLHS.Node(), nil, oRHS.Node(), nil)
o.SetMType(x.MType())
return o
}
return nil
})
}
return nil
}
+1
View File
@@ -22,6 +22,7 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)()
{
assert n_bits < (width + 8) via "a < (b + c): a < c; 0 <= b"()
while n_bits < width,
inv n_bits < (width + 8),
post n_bits >= width,
{
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)