private static IEnumerable <ProofState> UpdateLocalValue(UpdateStmt us, ProofState state) { Contract.Requires <ArgumentNullException>(us != null, "stmt"); Contract.Requires <ArgumentNullException>(state != null, "state"); Contract.Requires <ArgumentException>(state.IsLocalAssignment(us), "stmt"); foreach (var item in us.Rhss) { int index = us.Rhss.IndexOf(item); Contract.Assert(us.Lhss.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.UpdateLocal(((NameSegment)us.Lhss[index]).Name, result); } } else if (exprRhs?.Expr is Dafny.LiteralExpr) { state.UpdateLocal(((NameSegment)us.Lhss[index]).Name, (Dafny.LiteralExpr)exprRhs?.Expr); } else { state.UpdateLocal(((NameSegment)us.Lhss[index]).Name, exprRhs?.Expr); } } yield return(state.Copy()); }
public static IEnumerable <ProofState> EvalPredicateStmt(PredicateStmt predicate, ProofState state) { Contract.Requires <ArgumentNullException>(predicate != null, "predicate"); foreach (var result in EvalTacnyExpression(state, predicate.Expr)) { var resultExpression = result is IVariable?Util.VariableToExpression(result as IVariable) : result as Expression; PredicateStmt newPredicate; var tok = predicate.Tok.Copy(); tok.line = TACNY_CODE_TOK_LINE; var endTok = predicate.EndTok.Copy(); endTok.line = TACNY_CODE_TOK_LINE; if (predicate is AssertStmt) { newPredicate = new AssertStmt(tok, endTok, resultExpression, predicate.Attributes); } else { newPredicate = new AssumeStmt(tok, endTok, resultExpression, predicate.Attributes); } var copy = state.Copy(); copy.AddStatement(newPredicate); copy.IfVerify = true; yield return(copy); } }
/// <summary> /// Insert the statement as is into the state /// </summary> /// <param name="stmt"></param> /// <param name="state"></param> /// <returns></returns> private static IEnumerable <ProofState> DefaultAction(Statement stmt, ProofState state) { Contract.Requires <ArgumentNullException>(stmt != null, "stmt"); Contract.Requires <ArgumentNullException>(state != null, "state"); state.AddStatement(stmt); yield return(state.Copy()); }
/* * private static IEnumerable<ProofState> ResolveFlowControlStmt(Statement stmt, ProofState state) { * Language.FlowControlStmt fcs = null; * if(stmt is IfStmt) { * //fcs = new Language.IfStmt(); * //TODO: if statemenet * } else if(stmt is WhileStmt) { * //TODO: while statemenet * } else { * Contract.Assert(false); * return null; * } * return fcs.Generate(stmt, state); * } */ public static IEnumerable <ProofState> EvalTacApp(UpdateStmt stmt, ProofState state) { var state0 = state.Copy(); state0.InitTacFrame(stmt, true); yield return(state0); }
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()); }
public static IEnumerable <ProofState> EvalStep(ProofState state) { Contract.Requires <ArgumentNullException>(state != null, "state"); // one step // call the IEnumerable <ProofState> enumerable = null; var stmt = state.GetStmt(); if (stmt is TacticVarDeclStmt) { enumerable = RegisterVariable(stmt as TacticVarDeclStmt, state); } else if (stmt is UpdateStmt) { var us = stmt as UpdateStmt; if (state.IsLocalAssignment(us)) { enumerable = UpdateLocalValue(us, state); } else if (state.IsArgumentApplication(us)) { //TODO: argument application } else if (state.IsTacticCall(us)) { enumerable = ApplyNestedTactic(state.Copy(), state.DafnyVars(), us); } } else if (stmt is AssignSuchThatStmt) { enumerable = EvaluateSuchThatStmt((AssignSuchThatStmt)stmt, state); } else if (stmt is PredicateStmt) { enumerable = ResolvePredicateStmt((PredicateStmt)stmt, state); } else if (stmt is TStatement || stmt is IfStmt || stmt is WhileStmt) { //TODO: } else { enumerable = DefaultAction(stmt, state); } foreach (var item in enumerable) { yield return(item.Copy()); } }
public static IEnumerable <ProofState> ApplyNestedTactic(ProofState state, Dictionary <IVariable, Type> variables, UpdateStmt tacticApplication) { Contract.Requires <ArgumentNullException>(tcce.NonNull(variables)); Contract.Requires <ArgumentNullException>(tcce.NonNull(tacticApplication)); Contract.Requires <ArgumentNullException>(state != null, "state"); state.InitState(tacticApplication, variables); var search = new BaseSearchStrategy(state.TacticInfo.SearchStrategy, false); foreach (var result in search.Search(state, _errorReporterDelegate)) { var c = state.Copy(); c.AddStatementRange(result.GetGeneratedCode()); yield return(c); } }
private static IEnumerable <ProofState> ResolvePredicateStmt(PredicateStmt predicate, ProofState state) { Contract.Requires <ArgumentNullException>(predicate != null, "predicate"); foreach (var result in EvaluateTacnyExpression(state, predicate.Expr)) { var resultExpression = result is IVariable?Util.VariableToExpression(result as IVariable) : result as Expression; PredicateStmt newPredicate; if (predicate is AssertStmt) { newPredicate = new AssertStmt(predicate.Tok, predicate.EndTok, resultExpression, predicate.Attributes); } else { newPredicate = new AssumeStmt(predicate.Tok, predicate.EndTok, resultExpression, predicate.Attributes); } var copy = state.Copy(); copy.AddStatement(newPredicate); yield return(copy); } }
public static IEnumerable<ProofState> EvalStep(ProofState state) { Contract.Requires<ArgumentNullException>(state != null, "state"); // one step // call the IEnumerable<ProofState> enumerable = null; var stmt = state.GetStmt(); if (stmt is TacticVarDeclStmt) { enumerable = RegisterVariable(stmt as TacticVarDeclStmt, state); } else if (stmt is UpdateStmt) { var us = stmt as UpdateStmt; if (state.IsLocalAssignment(us)) { enumerable = UpdateLocalValue(us, state); } else if (state.IsArgumentApplication(us)) { //TODO: argument application } else if (state.IsTacticCall(us)) { enumerable = ApplyNestedTactic(state.Copy(), state.DafnyVars(), us); } } else if (stmt is AssignSuchThatStmt) { enumerable = EvaluateSuchThatStmt((AssignSuchThatStmt)stmt, state); } else if (stmt is PredicateStmt) { enumerable = ResolvePredicateStmt((PredicateStmt)stmt, state); } else if (stmt is TStatement || stmt is IfStmt || stmt is WhileStmt) { //TODO: } else { enumerable = DefaultAction(stmt, state); } foreach (var item in enumerable) yield return item.Copy(); }
public static IEnumerable<ProofState> ApplyNestedTactic(ProofState state, Dictionary<IVariable, Type> variables, UpdateStmt tacticApplication) { Contract.Requires<ArgumentNullException>(tcce.NonNull(variables)); Contract.Requires<ArgumentNullException>(tcce.NonNull(tacticApplication)); Contract.Requires<ArgumentNullException>(state != null, "state"); state.InitState(tacticApplication, variables); var search = new BaseSearchStrategy(state.TacticInfo.SearchStrategy, false); foreach (var result in search.Search(state, _errorReporterDelegate)) { var c = state.Copy(); c.AddStatementRange(result.GetGeneratedCode()); yield return c; } }
public IEnumerable<ProofState> EvalNext(Statement statement, ProofState state0){ Contract.Requires(statement != null); Contract.Requires(statement is TacnyCasesBlockStmt); var state = state0.Copy(); var stmt = statement as TacnyCasesBlockStmt; var raw = state.GetGeneratedaRawCode(); state.AddNewFrame(stmt.Body.Body, IsPartial); var matchStmt = raw[0][0] as MatchStmt; var idx = GetNthCaseIdx(raw); foreach(var tmp in matchStmt.Cases[idx].CasePatterns) { state.AddDafnyVar(tmp.Var.Name, new ProofState.VariableData { Variable = tmp.Var, Type = tmp.Var.Type }); } //with this flag set to true, dafny will check the case branch before evaluates any tacny code state.IfVerify = true; yield return state; }
public IEnumerable<ProofState> EvalInit(Statement statement, ProofState state0){ Contract.Requires(statement != null); Contract.Requires(statement is TacnyCasesBlockStmt); var state = state0.Copy(); var stmt = statement as TacnyCasesBlockStmt; var p = new Printer(Console.Out); NameSegment caseVar; //get guards Debug.Assert(stmt != null, "stmt != null"); var guard = stmt.Guard as ParensExpression; if(guard == null) caseVar = stmt.Guard as NameSegment; else caseVar = guard.E as NameSegment; //TODO: need to check the datatype pf caseGuard, // also need to consider the case that caseVar is a tac var var srcVar = state.GetTacnyVarValue(caseVar) as NameSegment; var srcVarData = state.GetDafnyVar(srcVar.Name); var datatype = state.GetDafnyVarType(srcVar.Name).AsDatatype; //generate a test program to check which cases need to apply tacny bool[] ctorFlags; int ctor; // current active match case InitCtorFlags(datatype, out ctorFlags); List<Func<int, List<Statement>>> fList = new List<Func<int, List<Statement>>>(); int i; for(i = 0; i < datatype.Ctors.Count; i++) { fList.Add(GenerateAssumeFalseStmtAsStmtList); } //var matchStmt = GenerateMatchStmt(state.TacticApplication.Tok.line, srcVar.Copy(), datatype, fList); var matchStmt = GenerateMatchStmt(Interpreter.TACNY_CODE_TOK_LINE, srcVar.Copy(), datatype, fList); //use a dummystmt to creat a frame for match, note that this stmts is never be evaluated var dummystmt = new List<Statement>(); for(i = 0; i < datatype.Ctors.Count; i++) { dummystmt.Add(stmt); } state.AddNewFrame(dummystmt, IsPartial, Signature); //add raw[0] state.AddStatement(matchStmt); //push a frame for the first case //TODO: add case variable to frame, so that variable () can refer to it state.AddNewFrame(stmt.Body.Body, IsPartial); foreach(var tmp in matchStmt.Cases[0].CasePatterns) { state.AddDafnyVar(tmp.Var.Name, new ProofState.VariableData { Variable = tmp.Var, Type = tmp.Var.Type }); } //with this flag set to true, dafny will check the case brnach before evaluates any tacny code state.IfVerify = true; yield return state; }
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(); }
private static IEnumerable<ProofState> UpdateLocalValue(UpdateStmt us, ProofState state) { Contract.Requires<ArgumentNullException>(us != null, "stmt"); Contract.Requires<ArgumentNullException>(state != null, "state"); Contract.Requires<ArgumentException>(state.IsLocalAssignment(us), "stmt"); foreach (var item in us.Rhss) { int index = us.Rhss.IndexOf(item); Contract.Assert(us.Lhss.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.UpdateLocal(((NameSegment)us.Lhss[index]).Name, result); } } else if (exprRhs?.Expr is Dafny.LiteralExpr) { state.UpdateLocal(((NameSegment)us.Lhss[index]).Name, (Dafny.LiteralExpr)exprRhs?.Expr); } else { state.UpdateLocal(((NameSegment)us.Lhss[index]).Name, exprRhs?.Expr); } } yield return state.Copy(); }
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()); }
private static IEnumerable<ProofState> ResolvePredicateStmt(PredicateStmt predicate, ProofState state) { Contract.Requires<ArgumentNullException>(predicate != null, "predicate"); foreach (var result in EvaluateTacnyExpression(state, predicate.Expr)) { var resultExpression = result is IVariable ? Util.VariableToExpression(result as IVariable) : result as Expression; PredicateStmt newPredicate; if (predicate is AssertStmt) { newPredicate = new AssertStmt(predicate.Tok, predicate.EndTok, resultExpression, predicate.Attributes); } else { newPredicate = new AssumeStmt(predicate.Tok, predicate.EndTok, resultExpression, predicate.Attributes); } var copy = state.Copy(); copy.AddStatement(newPredicate); yield return copy; } }
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(); }
/* private static IEnumerable<ProofState> ResolveFlowControlStmt(Statement stmt, ProofState state) { Language.FlowControlStmt fcs = null; if(stmt is IfStmt) { //fcs = new Language.IfStmt(); //TODO: if statemenet } else if(stmt is WhileStmt) { //TODO: while statemenet } else { Contract.Assert(false); return null; } return fcs.Generate(stmt, state); } */ public static IEnumerable<ProofState> EvalTacApp(UpdateStmt stmt, ProofState state){ var state0 = state.Copy(); state0.InitTacFrame(stmt, true); yield return state0; }
/// <summary> /// Insert the statement as is into the state /// </summary> /// <param name="stmt"></param> /// <param name="state"></param> /// <returns></returns> private static IEnumerable<ProofState> DefaultAction(Statement stmt, ProofState state) { Contract.Requires<ArgumentNullException>(stmt != null, "stmt"); Contract.Requires<ArgumentNullException>(state != null, "state"); state.AddStatement(stmt); yield return state.Copy(); }
public static IEnumerable<ProofState> EvalPredicateStmt(PredicateStmt predicate, ProofState state) { Contract.Requires<ArgumentNullException>(predicate != null, "predicate"); foreach(var result in EvalTacnyExpression(state, predicate.Expr)) { var resultExpression = result is IVariable ? Util.VariableToExpression(result as IVariable) : result as Expression; PredicateStmt newPredicate; var tok = predicate.Tok.Copy(); tok.line = TACNY_CODE_TOK_LINE; var endTok = predicate.EndTok.Copy(); endTok.line = TACNY_CODE_TOK_LINE; if(predicate is AssertStmt) { newPredicate = new AssertStmt(tok, endTok, resultExpression, predicate.Attributes); } else { newPredicate = new AssumeStmt(tok, endTok, resultExpression, predicate.Attributes); } var copy = state.Copy(); copy.AddStatement(newPredicate); copy.IfVerify = true; yield return copy; } }