Exemplo n.º 1
0
        private IEnumerable <Solution> SuchThat(Statement st, Solution solution)
        {
            TacticVarDeclStmt tvds = st as TacticVarDeclStmt;

            // statement must be object level, thus include as is
            if (tvds == null)
            {
                yield return(AddNewStatement(st, st));

                yield break;
            }

            AssignSuchThatStmt suchThat = tvds.Update as AssignSuchThatStmt;

            Contract.Assert(suchThat != null, Error.MkErr(st, 5, typeof(AssignSuchThatStmt), tvds.Update.GetType()));

            BinaryExpr bexp = suchThat.Expr as BinaryExpr;

            Contract.Assert(bexp != null, Error.MkErr(st, 5, typeof(BinaryExpr), suchThat.Expr.GetType()));

            // this will cause issues when multiple variables are used
            // as the variables are updated one at a time
            foreach (var local in tvds.Locals)
            {
                foreach (var item in ResolveExpression(bexp, local))
                {
                    yield return(AddNewLocal(local, item));
                }
            }
        }
Exemplo n.º 2
0
        private static IEnumerable <ProofState> RegisterVariable(TacticVarDeclStmt declaration, ProofState state)
        {
            if (declaration.Update == null)
            {
                yield break;
            }
            var rhs = declaration.Update as UpdateStmt;

            if (rhs == null)
            {
                // check if rhs is SuchThatStmt
                if (declaration.Update is AssignSuchThatStmt)
                {
                    foreach (var item in declaration.Locals)
                    {
                        state.AddLocal(item, null);
                    }
                    foreach (var item in EvaluateSuchThatStmt(declaration.Update as AssignSuchThatStmt, state))
                    {
                        yield return(item.Copy());
                    }
                }
                else
                {
                    foreach (var item in declaration.Locals)
                    {
                        state.AddLocal(item, null);
                    }
                }
            }
            else
            {
                foreach (var item in rhs.Rhss)
                {
                    int index = rhs.Rhss.IndexOf(item);
                    Contract.Assert(declaration.Locals.ElementAtOrDefault(index) != null, "register var err");
                    var exprRhs = item as ExprRhs;
                    if (exprRhs?.Expr is ApplySuffix)
                    {
                        var aps = (ApplySuffix)exprRhs.Expr;
                        foreach (var result in EvaluateTacnyExpression(state, aps))
                        {
                            state.AddLocal(declaration.Locals[index], result);
                        }
                    }
                    else if (exprRhs?.Expr is Dafny.LiteralExpr)
                    {
                        state.AddLocal(declaration.Locals[index], (Dafny.LiteralExpr)exprRhs?.Expr);
                    }
                    else
                    {
                        state.AddLocal(declaration.Locals[index], exprRhs?.Expr);
                    }
                }
            }
            yield return(state.Copy());
        }
Exemplo n.º 3
0
        public static IEnumerable <ProofState> RegisterVariable(TacticVarDeclStmt declaration, ProofState state)
        {
            var rhs = declaration.Update as UpdateStmt;

            if (rhs != null)
            {
                foreach (var item in rhs.Rhss)
                {
                    int index = rhs.Rhss.IndexOf(item);
                    Contract.Assert(declaration.Locals.ElementAtOrDefault(index) != null, "register var err");
                    var exprRhs = item as ExprRhs;
                    var res     = EvalExpr.EvalTacticExpression(state, exprRhs?.Expr);
                    if (res != null)
                    {
                        state.AddTacnyVar(declaration.Locals[index], res);
                        yield return(state);
                    }
                }
            }
            else
            {
                var stmt = declaration.Update as AssignSuchThatStmt;
                if (stmt != null)
                {
                    foreach (var item in declaration.Locals)
                    {
                        state.AddTacnyVar(item, null);
                    }
                    foreach (var item in EvalSuchThatStmt(stmt, state))
                    {
                        yield return(item);
                    }
                }
                else
                {
                    foreach (var item in declaration.Locals)
                    {
                        if (state.ContainTVal(item.Name))
                        {
                            state.ReportTacticError(item.Tok, item.Name + " has already been defined in the current scope.");
                            yield break;
                        }
                        else
                        {
                            state.AddTacnyVar(item, null);
                        }
                    }

                    yield return(state);
                }
            }
        }
Exemplo n.º 4
0
    public virtual Statement CloneStmt(Statement stmt) {
      if (stmt == null) {
        return null;
      }

      Statement r;
      if (stmt is AssertStmt) {
        var s = (AssertStmt)stmt;
        r = new AssertStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null);

      } else if (stmt is AssumeStmt) {
        var s = (AssumeStmt)stmt;
        r = new AssumeStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null);
      } else if (stmt is TacticInvariantStmt) {
        var s = (TacticInvariantStmt)stmt;
        r = new TacticInvariantStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null, s.IsObjectLevel);
      } else if (stmt is TacticAssertStmt) {
        var s = (TacticAssertStmt)stmt;
        r = new TacticAssertStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null, s.IsObjectLevel);
      } else if (stmt is PrintStmt) {
        var s = (PrintStmt)stmt;
        r = new PrintStmt(Tok(s.Tok), Tok(s.EndTok), s.Args.ConvertAll(CloneExpr));
      } else if (stmt is BreakStmt) {
        var s = (BreakStmt)stmt;
        if (s.TargetLabel != null) {
          r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.TargetLabel);
        } else {
          r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.BreakCount);
        }

      } else if (stmt is ReturnStmt) {
        var s = (ReturnStmt)stmt;
        r = new ReturnStmt(Tok(s.Tok), Tok(s.EndTok), s.rhss == null ? null : s.rhss.ConvertAll(CloneRHS));

      } else if (stmt is YieldStmt) {
        var s = (YieldStmt)stmt;
        r = new YieldStmt(Tok(s.Tok), Tok(s.EndTok), s.rhss == null ? null : s.rhss.ConvertAll(CloneRHS));

      } else if (stmt is AssignStmt) {
        var s = (AssignStmt)stmt;
        r = new AssignStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Lhs), CloneRHS(s.Rhs));

      } else if (stmt is BlockStmt) {
        r = CloneBlockStmt((BlockStmt)stmt);

      } else if (stmt is IfStmt) {
        var s = (IfStmt)stmt;
        r = new IfStmt(Tok(s.Tok), Tok(s.EndTok), s.IsExistentialGuard, CloneExpr(s.Guard), CloneBlockStmt(s.Thn), CloneStmt(s.Els));

      } else if (stmt is AlternativeStmt) {
        var s = (AlternativeStmt)stmt;
        r = new AlternativeStmt(Tok(s.Tok), Tok(s.EndTok), s.Alternatives.ConvertAll(CloneGuardedAlternative));

      } else if (stmt is WhileStmt) {
        var s = (WhileStmt)stmt;
        r = new WhileStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Guard), s.Invariants.ConvertAll(CloneMayBeFreeExpr), CloneSpecExpr(s.Decreases), CloneSpecFrameExpr(s.Mod), CloneBlockStmt(s.Body));
        ((WhileStmt)r).TacAps = s.TacAps;
      } else if (stmt is AlternativeLoopStmt) {
        var s = (AlternativeLoopStmt)stmt;
        r = new AlternativeLoopStmt(Tok(s.Tok), Tok(s.EndTok), s.Invariants.ConvertAll(CloneMayBeFreeExpr), CloneSpecExpr(s.Decreases), CloneSpecFrameExpr(s.Mod), s.Alternatives.ConvertAll(CloneGuardedAlternative));

      } else if (stmt is ForallStmt) {
        var s = (ForallStmt)stmt;
        r = new ForallStmt(Tok(s.Tok), Tok(s.EndTok), s.BoundVars.ConvertAll(CloneBoundVar), null, CloneExpr(s.Range), s.Ens.ConvertAll(CloneMayBeFreeExpr), CloneStmt(s.Body));
        if (s.ForallExpressions != null) {
          ((ForallStmt)r).ForallExpressions = s.ForallExpressions.ConvertAll(CloneExpr);
        }
      } else if (stmt is CalcStmt) {
        var s = (CalcStmt)stmt;
        // calc statements have the unusual property that the last line is duplicated.  If that is the case (which
        // we expect it to be here), we share the clone of that line as well.
        var lineCount = s.Lines.Count;
        var lines = new List<Expression>(lineCount);
        for (int i = 0; i < lineCount; i++) {
          lines.Add(i == lineCount - 1 && 2 <= lineCount && s.Lines[i] == s.Lines[i - 1] ? lines[i - 1] : CloneExpr(s.Lines[i]));
        }
        Contract.Assert(lines.Count == lineCount);
        r = new CalcStmt(Tok(s.Tok), Tok(s.EndTok), CloneCalcOp(s.Op), lines, s.Hints.ConvertAll(CloneBlockStmt), s.StepOps.ConvertAll(CloneCalcOp), CloneCalcOp(s.ResultOp), CloneAttributes(s.Attributes));

      } else if (stmt is MatchStmt) {
        var s = (MatchStmt)stmt;
        r = new MatchStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Source),
        s.Cases.ConvertAll(CloneMatchCaseStmt), s.UsesOptionalBraces);

      } else if (stmt is AssignSuchThatStmt) {
        var s = (AssignSuchThatStmt)stmt;
        r = new AssignSuchThatStmt(Tok(s.Tok), Tok(s.EndTok), s.Lhss.ConvertAll(CloneExpr), CloneExpr(s.Expr), s.AssumeToken == null ? null : Tok(s.AssumeToken), null);

      } else if (stmt is UpdateStmt) {
        var s = (UpdateStmt)stmt;
        r = new UpdateStmt(Tok(s.Tok), Tok(s.EndTok), s.Lhss.ConvertAll(CloneExpr), s.Rhss.ConvertAll(CloneRHS), s.CanMutateKnownState);

      } else if (stmt is VarDeclStmt) {
        var s = (VarDeclStmt)stmt;
        var lhss = s.Locals.ConvertAll(c => new LocalVariable(Tok(c.Tok), Tok(c.EndTok), c.Name, CloneType(c.OptionalType), c.IsGhost));
        r = new VarDeclStmt(Tok(s.Tok), Tok(s.EndTok), lhss, (ConcreteUpdateStatement)CloneStmt(s.Update));
      } else if (stmt is LetStmt) {
        var s = (LetStmt)stmt;
        r = new LetStmt(Tok(s.Tok), Tok(s.EndTok), s.LHSs.ConvertAll(CloneCasePattern), s.RHSs.ConvertAll(CloneExpr));
      } else if (stmt is ModifyStmt) {
        var s = (ModifyStmt)stmt;
        var mod = CloneSpecFrameExpr(s.Mod);
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new ModifyStmt(Tok(s.Tok), Tok(s.EndTok), mod.Expressions, mod.Attributes, body);
      } else if (stmt is TacnyCasesBlockStmt) {
        var s = (TacnyCasesBlockStmt)stmt;
        var guard = CloneExpr(s.Guard);
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new TacnyCasesBlockStmt(Tok(s.Tok), Tok(s.EndTok), guard, body);
      } else if (stmt is TacnyChangedBlockStmt) {
        var s = (TacnyChangedBlockStmt)stmt;
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new TacnyChangedBlockStmt(Tok(s.Tok), Tok(s.EndTok), body);
      } else if (stmt is TacnySolvedBlockStmt) {
        var s = (TacnySolvedBlockStmt)stmt;
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new TacnySolvedBlockStmt(Tok(s.Tok), Tok(s.EndTok), body);
      } else if (stmt is TacnyTryCatchBlockStmt) {
        var s = (TacnyTryCatchBlockStmt)stmt;
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        var c = s.Ctch == null ? null : CloneBlockStmt(s.Ctch);
        r = new TacnyTryCatchBlockStmt(Tok(s.Tok), Tok(s.EndTok), body, c);
      } else if (stmt is TacticVarDeclStmt) {
        var s = (TacticVarDeclStmt)stmt;
        var lhss = s.Locals.ConvertAll(c => new LocalVariable(Tok(c.Tok), Tok(c.EndTok), c.Name, CloneType(c.OptionalType), c.IsGhost));
        r = new TacticVarDeclStmt(Tok(s.Tok), Tok(s.EndTok), lhss, (ConcreteUpdateStatement)CloneStmt(s.Update));
      } else {
        Contract.Assert(false); throw new cce.UnreachableException();  // unexpected statement
      }

      // add labels to the cloned statement
      AddStmtLabels(r, stmt.Labels);
      r.Attributes = CloneAttributes(stmt.Attributes);

      return r;
    }
Exemplo n.º 5
0
 private static IEnumerable<ProofState> RegisterVariable(TacticVarDeclStmt declaration, ProofState state) {
   if (declaration.Update == null) yield break;
   var rhs = declaration.Update as UpdateStmt;
   if (rhs == null) {
     // check if rhs is SuchThatStmt
     if (declaration.Update is AssignSuchThatStmt) {
       foreach (var item in declaration.Locals)
         state.AddLocal(item, null);
       foreach (var item in EvaluateSuchThatStmt(declaration.Update as AssignSuchThatStmt, state)) {
         yield return item.Copy();
       }
     } else {
       foreach (var item in declaration.Locals)
         state.AddLocal(item, null);
     }
   } else {
     foreach (var item in rhs.Rhss) {
       int index = rhs.Rhss.IndexOf(item);
       Contract.Assert(declaration.Locals.ElementAtOrDefault(index) != null, "register var err");
       var exprRhs = item as ExprRhs;
       if (exprRhs?.Expr is ApplySuffix) {
         var aps = (ApplySuffix)exprRhs.Expr;
         foreach (var result in EvaluateTacnyExpression(state, aps)) {
           state.AddLocal(declaration.Locals[index], result);
         }
       } else if (exprRhs?.Expr is Dafny.LiteralExpr) {
         state.AddLocal(declaration.Locals[index], (Dafny.LiteralExpr)exprRhs?.Expr);
       } else {
         state.AddLocal(declaration.Locals[index], exprRhs?.Expr);
       }
     }
   }
   yield return state.Copy();
 }
Exemplo n.º 6
0
	void TacticVarDeclStmt(out Statement/*!*/ s, IToken x) {
		IToken assignTok = null;
		bool isGhost = false;
		LocalVariable d;
		AssignmentRhs r;
		List<LocalVariable> lhss = new List<LocalVariable>();
		List<AssignmentRhs> rhss = new List<AssignmentRhs>();
		IToken suchThatAssume = null;
		Expression suchThat = null;
		Attributes attrs = null;
		IToken endTok;
		Expression or = null;
		
		while (la.kind == 46) {
			Attribute(ref attrs);
		}
		LocalIdentTypeOptional(out d, isGhost);
		lhss.Add(d); d.Attributes = attrs; attrs = null; 
		while (la.kind == 22) {
			Get();
			while (la.kind == 46) {
				Attribute(ref attrs);
			}
			LocalIdentTypeOptional(out d, isGhost);
			lhss.Add(d); d.Attributes = attrs; attrs = null; 
		}
		if (la.kind == 25 || la.kind == 46 || la.kind == 104) {
			if (la.kind == 104) {
				Get();
				assignTok = t; 
				Rhs(out r);
				rhss.Add(r); 
				while (la.kind == 22) {
					Get();
					Rhs(out r);
					rhss.Add(r); 
				}
				if (la.kind == 105) {
					Get();
					x = t; 
					Expression(out or, false, true);
				}
			} else {
				while (la.kind == 46) {
					Attribute(ref attrs);
				}
				Expect(25);
				assignTok = t; 
				if (la.kind == _assume) {
					Expect(31);
					suchThatAssume = t; 
				}
				Expression(out suchThat, false, true);
			}
		}
		while (!(la.kind == 0 || la.kind == 28)) {SynErr(216); Get();}
		Expect(28);
		endTok = t; 
		ConcreteUpdateStatement update;
		if (suchThat != null) {
		 var ies = new List<Expression>();
		 foreach (var lhs in lhss) {
		   ies.Add(new IdentifierExpr(lhs.Tok, lhs.Name));
		 }
		 update = new AssignSuchThatStmt(assignTok, endTok, ies, suchThat, suchThatAssume, attrs);
		} else if (rhss.Count == 0) {
		 update = null;
		} else {
		 var ies = new List<Expression>();
		 foreach (var lhs in lhss) {
		   ies.Add(new AutoGhostIdentifierExpr(lhs.Tok, lhs.Name));
		 }
		 update = new UpdateStmt(assignTok, endTok, ies, rhss);
		}
		s = new TacticVarDeclStmt(x, endTok, lhss, update);
		
	}
Exemplo n.º 7
0
 public static IEnumerable<ProofState> RegisterVariable(TacticVarDeclStmt declaration, ProofState state) {
   if(declaration.Update == null)
     yield break;
   var rhs = declaration.Update as UpdateStmt;
   if(rhs == null) {
     // check if rhs is SuchThatStmt
     if(declaration.Update is AssignSuchThatStmt) {
       foreach(var item in declaration.Locals)
         state.AddTacnyVar(item, null);
       foreach(var item in EvalSuchThatStmt(declaration.Update as AssignSuchThatStmt, state)) {
         yield return item.Copy();
       }
     } else {
       foreach(var item in declaration.Locals)
         state.AddTacnyVar(item, null);
     }
   } else {
     foreach(var item in rhs.Rhss) {
       int index = rhs.Rhss.IndexOf(item);
       Contract.Assert(declaration.Locals.ElementAtOrDefault(index) != null, "register var err");
       var exprRhs = item as ExprRhs;
       if(exprRhs?.Expr is ApplySuffix) {
         var aps = (ApplySuffix)exprRhs.Expr;
         foreach(var result in EvalTacnyExpression(state, aps)) {
           state.AddTacnyVar(declaration.Locals[index], result);
         }
       } else if(exprRhs?.Expr is Dafny.LiteralExpr) {
         state.AddTacnyVar(declaration.Locals[index], (Dafny.LiteralExpr)exprRhs?.Expr);
       } else if(exprRhs?.Expr is Dafny.NameSegment) {
         var name = ((Dafny.NameSegment)exprRhs.Expr).Name;
         if(state.ContainTacnyVal(name))
           // in the case that referring to an exisiting tvar, dereference it
           state.AddTacnyVar(declaration.Locals[index], state.GetTacnyVarValue(name));
       } else {
         state.AddTacnyVar(declaration.Locals[index], exprRhs?.Expr);
       }
     }
   }
   yield return state.Copy();
 }
Exemplo n.º 8
0
        public static IEnumerable <ProofState> RegisterVariable(TacticVarDeclStmt declaration, ProofState state)
        {
            if (declaration.Update == null)
            {
                yield break;
            }
            var rhs = declaration.Update as UpdateStmt;

            if (rhs == null)
            {
                // check if rhs is SuchThatStmt
                if (declaration.Update is AssignSuchThatStmt)
                {
                    foreach (var item in declaration.Locals)
                    {
                        state.AddTacnyVar(item, null);
                    }
                    foreach (var item in EvalSuchThatStmt(declaration.Update as AssignSuchThatStmt, state))
                    {
                        yield return(item.Copy());
                    }
                }
                else
                {
                    foreach (var item in declaration.Locals)
                    {
                        state.AddTacnyVar(item, null);
                    }
                }
            }
            else
            {
                foreach (var item in rhs.Rhss)
                {
                    int index = rhs.Rhss.IndexOf(item);
                    Contract.Assert(declaration.Locals.ElementAtOrDefault(index) != null, "register var err");
                    var exprRhs = item as ExprRhs;
                    if (exprRhs?.Expr is ApplySuffix)
                    {
                        var aps = (ApplySuffix)exprRhs.Expr;
                        foreach (var result in EvalTacnyExpression(state, aps))
                        {
                            state.AddTacnyVar(declaration.Locals[index], result);
                        }
                    }
                    else if (exprRhs?.Expr is Dafny.LiteralExpr)
                    {
                        state.AddTacnyVar(declaration.Locals[index], (Dafny.LiteralExpr)exprRhs?.Expr);
                    }
                    else if (exprRhs?.Expr is Dafny.NameSegment)
                    {
                        var name = ((Dafny.NameSegment)exprRhs.Expr).Name;
                        if (state.ContainTacnyVal(name))
                        {
                            // in the case that referring to an exisiting tvar, dereference it
                            state.AddTacnyVar(declaration.Locals[index], state.GetTacnyVarValue(name));
                        }
                    }
                    else
                    {
                        state.AddTacnyVar(declaration.Locals[index], exprRhs?.Expr);
                    }
                }
            }
            yield return(state.Copy());
        }