/// <summary> /// Project the parameters of the calling method/function /// </summary> /// <param name="expression"></param> /// <param name="proofState"></param> /// <returns></returns> public override Expression Generate(Expression expression, ProofState proofState) { var vars = proofState.GetAllDafnyVars().Values.ToList().Where(IsParam); var ret = new List <Expression>(); foreach (var x in vars) { ret.Add(new TacticLiteralExpr(x.Variable.Name)); } return(GenerateEAtomExprAsSeq(ret)); }
public override IEnumerable <object> Generate(Expression expression, ProofState proofState) { var vars = proofState.GetAllDafnyVars().Values.ToList(); yield return(vars.Select(x => x.Variable).ToList()); }
/// <summary> /// Project the parameters of the calling method/function /// </summary> /// <param name="expression"></param> /// <param name="proofState"></param> /// <returns></returns> public override IEnumerable<object> Generate(Expression expression, ProofState proofState) { var vars = proofState.GetAllDafnyVars().Values.ToList().Where(IsParam); yield return vars.Select(x => x.Variable).ToList(); }
public override IEnumerable <ProofState> Generate(Statement statement, ProofState state) { List <List <IVariable> > args = new List <List <IVariable> >(); List <IVariable> mdIns = new List <IVariable>(); List <Expression> callArguments; bool branchGenerated = false; state.NeedVerify = true; InitArgs(state, statement, out callArguments); /********************** * init lemmas **********************/ //check the number of arguments if (callArguments == null || callArguments.Count != 2) { state.ReportTacticError(statement.Tok, " The number of arguments for explore is not correct, expect 2."); yield break; } // get the first arg: a sequence of lemmas var members0 = EvalExpr.EvalTacticExpression(state, callArguments[0]) as SeqDisplayExpr; if (members0 == null) { state.ReportTacticError(statement.Tok, Printer.ExprToString(callArguments[0]) + " is not a sequence."); yield break; } List <MemberDecl> members = new List <MemberDecl>(); foreach (var mem in members0.Elements) { if (!(mem is TacticLiteralExpr)) { state.ReportTacticError(statement.Tok, "In " + Printer.ExprToString(callArguments[0]) + Printer.ExprToString(mem) + " is not a lemma."); yield break; } var key = (string)(mem as TacticLiteralExpr).Value; if (state.Members.ContainsKey(key)) { members.Add(state.Members[key]); } else { state.ReportTacticError(statement.Tok, "In " + Printer.ExprToString(callArguments[0]) + ", " + key + " is not a lemma."); yield break; } } if (members.Count == 0) { branchGenerated = true; yield return(state); yield break; } foreach (var member in members) { mdIns.Clear(); args.Clear(); var md = (MemberDecl)member; // take the membed decl parameters var method = md as Method; if (method != null) { mdIns.AddRange(method.Ins); } else if (md is Function) { mdIns.AddRange(((Function)md).Formals); } else { state.ReportTacticError(statement.Tok, Printer.ExprToString(callArguments[0]) + " is neither a Method or a Function"); yield break; } /********************** * init args for lemmas **********************/ var ovars = EvalExpr.EvalTacticExpression(state, callArguments[1]) as SeqDisplayExpr; if (ovars == null) { state.ReportTacticError(statement.Tok, Printer.ExprToString(callArguments[1]) + " is not a sequence."); yield break; } List <IVariable> vars = new List <IVariable>(); foreach (var var in ovars.Elements) { string key; if (var is TacticLiteralExpr) { key = (string)(var as TacticLiteralExpr).Value; } else if (var is NameSegment) { key = (var as NameSegment).Name; } else { state.ReportTacticError(statement.Tok, "In " + Printer.ExprToString(callArguments[1]) + ", " + Printer.ExprToString(var) + " is not a dafny variable."); yield break; } if (state.GetAllDafnyVars().ContainsKey(key)) { vars.Add(state.GetAllDafnyVars()[key].Variable); } else { state.ReportTacticError(statement.Tok, "In " + Printer.ExprToString(callArguments[1]) + ", " + key + " is not in scope."); yield break; } } //for the case of no args, just add an empty list if (mdIns.Count == 0) { args.Add(new List <IVariable>()); } //if any of the arguements is not valid, set it to false. bool flag = true; for (int i = 0; i < mdIns.Count; i++) { var item = mdIns[i]; args.Add(new List <IVariable>()); foreach (var arg in vars) { // get variable type Type type = state.GetDafnyVarType(arg.Name); if (type != null) { if (type is UserDefinedType && item.Type is UserDefinedType) { var udt1 = type as UserDefinedType; var udt2 = (UserDefinedType)item.Type; if (udt1.Name == udt2.Name) { args[i].Add(arg); } } else { // if variable type and current argument types match, or the type is yet to be inferred if (item.Type.ToString() == type.ToString() || type is InferredTypeProxy) { args[i].Add(arg); } } } else { args[i].Add(arg); } } /** * if no type correct variables have been added we can return * because we won't be able to generate valid calls */ if (args[i].Count == 0) { flag = false; } } // permute lemma call for the current lemma if (flag) { foreach (var result in PermuteArguments(args, 0, new List <NameSegment>())) { // create new fresh list of items to remove multiple references to the same object List <Expression> newList = result.Cast <Expression>().ToList().Copy(); ApplySuffix aps = new ApplySuffix(callArguments[0].tok, new NameSegment(callArguments[0].tok, md.Name, null), newList); var newState = state.Copy(); UpdateStmt us = new UpdateStmt(aps.tok, aps.tok, new List <Expression>(), new List <AssignmentRhs> { new ExprRhs(aps) }); newState.AddStatement(us); branchGenerated = true; yield return(newState); } } } // for the case when no lemma call is generated. if (!branchGenerated) { yield return(state); } }
public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state) { Contract.Assume(statement != null); Contract.Assume(statement is TacticForallStmt); _stmt = statement as TacticForallStmt; Contract.Assert(_stmt != null); // do basic simplification // maybe do a check and throw an error instead? // fixme: error returns null! //var e = (ForallExpr) SimpTacticExpr.SimpTacExpr(state0, _stmt.Spec); var e = (ForallExpr)SimpExpr.SimpTacticExpr(state, _stmt.Spec); // var e = _stmt.Spec as ForallExpr; // to rename expressions RenameVar rn = new RenameVar(); // to rename in the body of statement RenameVar rnBody = new RenameVar(); List <String> usedVars = state.GetAllDafnyVars().Keys.ToList(); usedVars.AddRange(state.GetAllTVars().Keys.ToList()); //List<String> tmp = new List<string>(); AllVars.DeclaredVars(_stmt.Body.Body[0], ref usedVars); if (_stmt.Attributes != null && _stmt.Attributes.Name.Equals("vars")) { var attrs = _stmt.Attributes.Args; for (int i = 0; i < attrs.Count; i++) { // todo: should really report an errors if first condition does not hold var segment = attrs[i] as NameSegment; if (segment != null && i < e.BoundVars.Count) { NameSegment ns = segment; String fv; if (GenFreeVar(ns.Name, usedVars, out fv)) { rnBody.AddRename(ns.Name, fv); } rn.AddRename(e.BoundVars[i].Name, fv); } // else we should have an error _vars = new List <BoundVar>(); foreach (BoundVar bv in e.BoundVars) { _vars.Add(rn.CloneBoundVar(bv)); } } } else { _vars = e.BoundVars; } foreach (var tmp in _vars) { state.AddDafnyVar(tmp.Name, new ProofState.VariableData { Variable = tmp, Type = tmp.Type }); } // we could even break _ens into a set of all conjunctions? // what about forall x (forall y) x var expr = e.Term as BinaryExpr; if (expr != null && (expr.Op.Equals(BinaryExpr.Opcode.Imp))) { var be = expr; _range = rn.CloneExpr(be.E0); var t = new MaybeFreeExpression(rn.CloneExpr(be.E1)); var l = new List <MaybeFreeExpression> { t }; _ens = l; } else { _range = new LiteralExpr(_stmt.Tok, true); var t = new MaybeFreeExpression(rn.CloneExpr(e.Term)); var l = new List <MaybeFreeExpression> { t }; _ens = l; } // Note that we do not need to rename variables in the body (unless the variables in vars is changed) InitBasicFrameCtrl(new List <Statement>(), state.IsCurFramePartial(), null, VerifyN); state.AddNewFrame(this); var bodyFrame = new DefaultTacticFrameCtrl(); var newBody = rnBody.CloneBlockStmt(_stmt.Body); bodyFrame.InitBasicFrameCtrl(newBody.Body, state.IsCurFramePartial(), null, VerifyN); bodyFrame.IsPartial = IsPartial; state.AddNewFrame(bodyFrame); yield return(state); }