Exemplo n.º 1
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   Contract.Assert(ExtractGuard(st) != null, Error.MkErr(st, 2));
   /**
    * Check if the loop guard can be resolved localy
    */
   return IsResolvable() ? ExecuteLoop(st as WhileStmt) : InsertLoop(st as WhileStmt);
 }
Exemplo n.º 2
0
    public IEnumerable<Solution> AddInvar(TacticInvariantStmt st, Solution solution) {
      MaybeFreeExpression invariant = null;


      foreach (var item in ResolveExpression(st.Expr)) {
        if (item is UpdateStmt) {   
          var us = item as UpdateStmt;
          var aps = ((ExprRhs)us.Rhss[0]).Expr as ApplySuffix;
          if (aps != null)
            invariant = new MaybeFreeExpression(aps);
        } else {
          invariant = item as MaybeFreeExpression ?? new MaybeFreeExpression(item as Expression);
        }
        WhileStmt nws = null;

        var ws = DynamicContext.whileStmt;
        Contract.Assert(ws != null, Error.MkErr(st, 11));
        // if we already added new invariants to the statement, use the updated statement instead
        nws = GetUpdated(ws) as WhileStmt;

        var invarArr = nws?.Invariants.ToArray() ?? ws.Invariants.ToArray();

        var invar = new List<MaybeFreeExpression>(invarArr) {invariant};
        //invar.Insert(0, invariant);
        nws = new WhileStmt(ws.Tok, ws.EndTok, ws.Guard, invar, ws.Decreases, ws.Mod, ws.Body);
        yield return AddNewStatement(ws, nws);
      }
    }
Exemplo n.º 3
0
    /// <summary>
    /// Merge two lists together
    /// </summary>
    /// <param name="st"></param>
    /// <param name="solution_list"></param>
    /// <returns></returns>
    public IEnumerable<Solution> Resolve(Statement st, Solution solution) {

      IVariable lv = null;
      List<Expression> call_arguments;
      IList result = null;
      InitArgs(st, out lv, out call_arguments);
      Contract.Assert(lv != null, Error.MkErr(st, 8));
      Contract.Assert(tcce.OfSize(call_arguments, 2), Error.MkErr(st, 0, 2, call_arguments.Count));

      foreach (var arg1 in ResolveExpression(call_arguments[0])) {
        foreach (var arg2 in ResolveExpression(call_arguments[1])) {
          // valdiate the argument types
          Type type1 = arg1.GetType().GetGenericArguments().Single();
          Type type2 = arg2.GetType().GetGenericArguments().Single();
          Contract.Assert(type1.Equals(type2), Error.MkErr(st, 1, type1));

          if (!(arg1 is IEnumerable) || !(arg2 is IEnumerable))
            Contract.Assert(false, Error.MkErr(st, 1, typeof(IEnumerable)));

          dynamic darg1 = arg1;
          dynamic darg2 = arg2;

          result = MergeLists(darg1, darg2, type1);
          yield return AddNewLocal(lv, result);
        }
      }
    }
Exemplo n.º 4
0
 /// <summary>
 /// Identity expression
 /// </summary>
 /// <param name="st"></param>
 /// <param name="solution"></param>
 /// <returns></returns>
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   List<Expression> args = null;
   IVariable lv = null;
   InitArgs(st, out lv, out args);
   LiteralExpr lit = new LiteralExpr(st.Tok, true);
   var ac = Copy();
   ac.DynamicContext.AddLocal(lv, lit);
   yield return new Solution(ac);
 }
Exemplo n.º 5
0
    public IEnumerable<Solution> Resolve(Statement st, Solution solution) {

      var temp = st as TacticInvariantStmt;

      if(temp != null && temp.IsObjectLevel) {
        return AddInvar(temp, solution);
      }
      throw new NotImplementedException();
    }
Exemplo n.º 6
0
    private Solution Params(Statement st, Solution solution) {
      IVariable lv = null;
      List<Expression> call_arguments; // we don't care about this
      List<IVariable> input = new List<IVariable>();

      InitArgs(st, out lv, out call_arguments);
      Contract.Assert(lv != null, Error.MkErr(st, 8));
      Contract.Assert(tcce.OfSize(call_arguments, 0), Error.MkErr(st, 0, 0, call_arguments.Count));

      Method source = DynamicContext.md as Method;
      Contract.Assert(source != null, Error.MkErr(st, 4));

      input.AddRange(source.Ins);
      //input.AddRange(source.Outs);
      AddLocal(lv, input);
      return new Solution(Copy());
    }
Exemplo n.º 7
0
    private Solution GetVariables(Statement st, Solution solution) {
      IVariable lv = null;
      List<Expression> call_arguments;
      List<IVariable> locals = new List<IVariable>();

      InitArgs(st, out lv, out call_arguments);
      Contract.Assert(lv != null, Error.MkErr(st, 8));
      Contract.Assert(tcce.OfSize(call_arguments, 0), Error.MkErr(st, 0, 0, call_arguments.Count));

      Method source = DynamicContext.md as Method;
      Contract.Assert(source != null, Error.MkErr(st, 4));

      locals.AddRange(StaticContext.staticVariables.Values.ToList());

      AddLocal(lv, locals);
      return new Solution(Copy());
    }
Exemplo n.º 8
0
    public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
      IVariable lv = null;
      List<Expression> callArgs = null;
      InitArgs(st, out lv, out callArgs);
      Contract.Assert(tcce.OfSize(callArgs, 2), Error.MkErr(st, 0, 2, callArgs.Count));

      foreach (var ns in ResolveExpression(callArgs[0])) {
        var toRemove = ns as NameSegment;
        foreach (var list in ResolveExpression(callArgs[1])) {
          var source = list as List<IVariable>;
          var target = source.FirstOrDefault(x => x.Name == toRemove.Name);
          if (target != null)
            source.Remove(target);

          yield return AddNewLocal(lv, source);
        }
      }
    }
Exemplo n.º 9
0
    private IEnumerable<Solution> FreshName(Statement st, Solution solution) {
      IVariable lv = null;
      List<Expression> callArguments = null;

      InitArgs(st, out lv, out callArguments);
      Contract.Assert(tcce.OfSize(callArguments, 1), Error.MkErr(st, 0, 1, callArguments.Count));

      var nameLiteralExpr = callArguments[0] as StringLiteralExpr;
      Contract.Assert(nameLiteralExpr != null, Error.MkErr(st, 1, "string"));
      var count = StaticContext.program.Members.Keys.Count(x => x == nameLiteralExpr.AsStringLiteral());
      if (count == 0) {
        yield return AddNewLocal(lv, Util.Copy.CopyStringLiteralExpr(nameLiteralExpr));
      } else {
        var nameValue = nameLiteralExpr.AsStringLiteral();
        var freshNameValue = string.Format("{0}{1}{2}", nameValue, SUFFIX, count);
        yield return AddNewLocal(lv, new StringLiteralExpr(nameLiteralExpr.tok, freshNameValue, nameLiteralExpr.IsVerbatim));
      }
    }
Exemplo n.º 10
0
 private Solution GetMember(Statement st, Solution solution) {
   IVariable lv = null;
   List<Expression> call_arguments;
   InitArgs(st, out lv, out call_arguments);
   Contract.Assert(lv != null, Error.MkErr(st, 8));
   Contract.Assert(call_arguments.Count <= 1, Error.MkErr(st, 0, 1, call_arguments.Count));
   MemberDecl memberDecl = null;
   if (call_arguments.Count == 1) {
     var memberName = call_arguments[0] as StringLiteralExpr;
     try {
       memberDecl = StaticContext.program.Members[memberName.AsStringLiteral()];
     } catch (KeyNotFoundException e) {
       Console.Out.WriteLine(e.Message);
       Contract.Assert(false, Error.MkErr(st, 20, memberName.AsStringLiteral()));
     }
   } else {
     memberDecl = StaticContext.md;
   }
   return AddNewLocal(lv, memberDecl);
 }
Exemplo n.º 11
0
    private Solution Precond(Statement st, Solution solution) {
      IVariable lv = null;
      List<Expression> call_arguments; // we don't care about this
      List<Expression> requirements = new List<Expression>();
      InitArgs(st, out lv, out call_arguments);
      Contract.Assert(lv != null, Error.MkErr(st, 8));

      Contract.Assert(call_arguments.Count <= 1, Error.MkErr(st, 0, 0, call_arguments.Count));
      MemberDecl memberDecl = null;
      if (call_arguments.Count > 0) {
        foreach (var member in ResolveExpression(call_arguments[0])) {
          memberDecl = member as MemberDecl;
          if (memberDecl == null)
            Contract.Assert(false, Error.MkErr(st, 1, "Function, [Ghost] Method, Declaration"));
          break;
        }
      } else {
        memberDecl = StaticContext.md;
      }

      Function fun = memberDecl as Function;
      if (fun != null) {
        foreach (var req in fun.Req)
          requirements.Add(Util.Copy.CopyExpression(req));
      } else {

        Method method = memberDecl as Method;
        if (method != null) {
          foreach (var req in method.Req)
            requirements.Add(Util.Copy.CopyExpression(req.E));
        } else {
          Contract.Assert(false, Error.MkErr(st, 1, "Function, [Ghost] Method, Declaration"));
        }
      }

      var ac = Copy();
      ac.AddLocal(lv, requirements);
      return new Solution(ac);
    }
Exemplo n.º 12
0
    private IEnumerable<Solution> ResolveBexp(Statement st, Solution solution) {
      // unwarp binary expr
      var updateStmt = st as UpdateStmt;
      Contract.Assert(updateStmt != null, "updateStmt == null");
      var aps = ((ExprRhs)updateStmt?.Rhss[0])?.Expr as ApplySuffix;
      var bexp = aps?.Lhs as TacnyBinaryExpr;
      if (bexp == null)
        Contract.Assert(false);

      Contract.Assert(bexp != null, "updateStmt == null");
      switch (bexp.Op) {
        case TacnyBinaryExpr.TacnyOpcode.TacnyOr:
          foreach (var result in ResolveExpression(bexp.E0)) {
            var ac = Copy();
            if (result is Expression)
              ac.DynamicContext.generatedExpressions.Add(result as Expression);
            else if (result is IVariable)
              ac.DynamicContext.generatedExpressions.Add(VariableToExpression(result as IVariable));
            else
              Contract.Assert(false, "Sum tin wong");
            yield return new Solution(ac);
          }
          foreach (var result in ResolveExpression(bexp.E1)) {
            var ac = Copy();
            if (result is Expression)
              ac.DynamicContext.generatedExpressions.Add(result as Expression);
            else if (result is IVariable)
              ac.DynamicContext.generatedExpressions.Add(VariableToExpression(result as IVariable));
            else
              Contract.Assert(false, "Sum tin wong");
            yield return new Solution(ac);
          }
          yield break;
        default:
          Contract.Assert(false); // unknown operator
          yield break;
      }
    }
Exemplo n.º 13
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.º 14
0
    public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
      List<Expression> call_arguments = null;
      IVariable lv = null;
      DatatypeDecl datatype = null;

      InitArgs(st, out lv, out call_arguments);
      Contract.Assert(tcce.OfSize(call_arguments, 1), Error.MkErr(st, 0, 1, call_arguments.Count));

      string argType = GetArgumentType(call_arguments[0] as NameSegment);

      foreach (var result in ResolveExpression(call_arguments[0])) {
        if (argType == "Element") {
          var ns = result as NameSegment;
          IVariable originalDecl = StaticContext.GetGlobalVariable(ns.Name);
          Contract.Assert(originalDecl != null, Error.MkErr(st, 9, ((NameSegment)call_arguments[0]).Name));
          var datatypeName = originalDecl.Type.ToString();
          datatype = StaticContext.GetGlobal(datatypeName);
          var lit = IsInductive(datatypeName, datatype);
          yield return AddNewLocal(lv, lit);
        } else {
          var ctor = result as DatatypeCtor;
          Contract.Assert(ctor != null, Error.MkErr(st, 1, "Datatype constructor"));
          var datatypeName = ctor.EnclosingDatatype.Name;

          LiteralExpr lit = new LiteralExpr(st.Tok, false);

          foreach (var formal in ctor.Formals) {
            if (formal.Type.ToString() == datatypeName) {
              lit = new LiteralExpr(st.Tok, true);
              break;
            }
          }
          yield return AddNewLocal(lv, lit);
        }
      }
    }
Exemplo n.º 15
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   yield return Lemmas(st);
 }
Exemplo n.º 16
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   return FreshName(st, solution);
 }
Exemplo n.º 17
0
 public Solution(Atomic state, Solution parent = null)
     : this(state, false, parent) { }
Exemplo n.º 18
0
 public Solution(Atomic state, bool isFinal, Solution parent) {
   State = state;
   IsFinal = isFinal;
   Parent = parent;
 }
Exemplo n.º 19
0
    public IEnumerable<Solution> Resolve(Statement st, Solution solution) {

      yield return (GenerateExpression(st));
    }
Exemplo n.º 20
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   yield return GetVariables(st, solution);
 }
Exemplo n.º 21
0
    public IEnumerable<Solution> Resolve(Statement st, Solution solution) {

      yield return (GetConstructor(st));
    }
Exemplo n.º 22
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   return GetPredicates(st);
 }
Exemplo n.º 23
0
 internal static bool VerifySolution(Solution solution) {
   if (!solution.State.StaticContext.program.HasError()) {
     // return the valid solution and terminate
     return true;
   } // if verifies break else continue to the next solution
   if (solution.State.ResolveAndVerify(solution)) {
     return !solution.State.StaticContext.program.HasError();
   }
   return false;
 }
Exemplo n.º 24
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   yield return Postcond(st);
 }
Exemplo n.º 25
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   return ResolveBexp(st, solution);
 }
Exemplo n.º 26
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   return ReplaceOperator(st);
 }
Exemplo n.º 27
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   yield return Params(st, solution);
 }
Exemplo n.º 28
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   return ReplaceConstant(st);
 }
Exemplo n.º 29
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   throw new NotImplementedException();
 }
Exemplo n.º 30
0
 public IEnumerable<Solution> Resolve(Statement st, Solution solution) {
   Assert(st);
   yield return new Solution(solution.State.Copy());
 }