Пример #1
0
        // Find tactic application and resolve it
        private void SearchBlockStmt(BlockStmt body)
        {
            Contract.Requires(tcce.NonNull(body));

            // TODO Make sure this is an OK Thing to be doing
            // Currently, if the proof list is not reset, then because it is static across
            //   calls, it will start to build up old information, and the program will go
            //   into a loop trying to figure out new fresh names for 'existing' methods.
            BaseSearchStrategy.ResetProofList();
            _frame.Push(new Dictionary <IVariable, Type>());
            foreach (var stmt in body.Body)
            {
                if (stmt is VarDeclStmt)
                {
                    var vds = stmt as VarDeclStmt;
                    // register local variable declarations
                    foreach (var local in vds.Locals)
                    {
                        try {
                            _frame.Peek().Add(local, local.Type);
                        } catch (Exception e) {
                            //TODO: some error handling when target is not resolved
                            Console.Out.WriteLine(e.Message);
                        }
                    }
                }
                else if (stmt is IfStmt)
                {
                    var ifStmt = stmt as IfStmt;
                    SearchIfStmt(ifStmt);
                }
                else if (stmt is WhileStmt)
                {
                    var whileStmt = stmt as WhileStmt;
                    SearchBlockStmt(whileStmt.Body);
                }
                else if (stmt is UpdateStmt)
                {
                    var us = stmt as UpdateStmt;
                    if (_state.IsTacticCall(us))
                    {
                        var list   = StackToDict(_frame);
                        var result = ApplyTactic(_state, list, us);
                        if (result != null)
                        {
                            _resultList.Add(us.Copy(), result.GetGeneratedCode().Copy());
                        }
                        else
                        {
                            //TODO what should go here?
                        }
                    }
                }
                else if (stmt is BlockStmt)
                {
                    //TODO:
                }
            }
            _frame.Pop();
        }
Пример #2
0
        // Find tactic application and resolve it
        private void SearchBlockStmt(BlockStmt body)
        {
            Contract.Requires(tcce.NonNull(body));

            // BaseSearchStrategy.ResetProofList();
            _frame.Push(new Dictionary <IVariable, Type>());
            foreach (var stmt in body.Body)
            {
                if (stmt is VarDeclStmt)
                {
                    var vds = stmt as VarDeclStmt;
                    // register local variable declarations
                    foreach (var local in vds.Locals)
                    {
                        try {
                            _frame.Peek().Add(local, local.Type);
                        } catch (Exception e) {
                            //TODO: some error handling when target is not resolved
                            Console.Out.WriteLine(e.Message);
                        }
                    }
                }
                else if (stmt is IfStmt)
                {
                    var ifStmt = stmt as IfStmt;
                    SearchIfStmt(ifStmt);
                }
                else if (stmt is WhileStmt)
                {
                    var whileStmt = stmt as WhileStmt;
                    SearchBlockStmt(whileStmt.Body);
                }
                else if (stmt is UpdateStmt)
                {
                    var us = stmt as UpdateStmt;
                    if (_state.IsTacticCall(us))
                    {
                        var list   = StackToDict(_frame);
                        var result = EvalTactic(_state, list, us);
                        if (result != null)
                        {
                            _resultList.Add(us.Copy(), result.GetGeneratedCode().Copy());
                        }
                        else// when no results, just return a empty stmt list
                        {
                            _resultList.Add(us.Copy(), new List <Statement>());
                        }
                    }
                }
                else if (stmt is BlockStmt)
                {
                    //TODO:
                }
            }
            _frame.Pop();
        }
Пример #3
0
        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());
            }
        }
Пример #4
0
    public static IEnumerable<object> EvaluateTacnyExpression(ProofState state, Expression expr) {
      Contract.Requires<ArgumentNullException>(state != null, "state");
      Contract.Requires<ArgumentNullException>(expr != null, "expr");
      if (expr is NameSegment) {
        var ns = (NameSegment)expr;
        if (state.HasLocalValue(ns.Name)) {
          yield return state.GetLocalValue(ns.Name);
        } else {
          yield return ns;
        }
      } else if (expr is ApplySuffix) {
        var aps = (ApplySuffix)expr;
        if (state.IsTacticCall(aps)) {
          var us = new UpdateStmt(aps.tok, aps.tok, new List<Expression>() { aps.Lhs },
            new List<AssignmentRhs>() { new ExprRhs(aps) });
          foreach (var item in ApplyNestedTactic(state, state.DafnyVars(), us).Select(x => x.GetGeneratedCode())) {
            yield return item;
          }
        }
        // rewrite the expression
        if (aps.Lhs is ExprDotName) {
          foreach (var item in EvaluateTacnyExpression(state, aps.Lhs)) {
            if (item is Expression) {
              yield return new ApplySuffix(aps.tok, (Expression)item, aps.Args);
            } else {
              Contract.Assert(false, "Unexpected ExprNotName case");
            }
          }
        } else {
          // try to evaluate as tacny expression
          string sig = Util.GetSignature(aps);
          // using reflection find all classes that extend EAtomic
          var types =
            Assembly.GetAssembly(typeof(EAtomic.EAtomic)).GetTypes().Where(t => t.IsSubclassOf(typeof(EAtomic.EAtomic)));
          foreach (var type in types) {
            var resolverInstance = Activator.CreateInstance(type) as EAtomic.EAtomic;
            if (sig == resolverInstance?.Signature) {
              //TODO: validate input countx
              foreach (var item in resolverInstance?.Generate(aps, state))
                yield return item;
            }
            // if we reached this point, rewrite  the apply suffix
            foreach (var item in EvaluateTacnyExpression(state, aps.Lhs)) {
              if (!(item is NameSegment)) {
                //TODO: warning
              } else {
                var argList = new List<Expression>();
                foreach (var arg in aps.Args) {
                  foreach (var result in EvaluateTacnyExpression(state, arg)) {
                    if (result is Expression)
                      argList.Add(result as Expression);
                    else
                      argList.Add(Util.VariableToExpression(result as IVariable));
                    break;
                  }
                }
                yield return new ApplySuffix(aps.tok, aps.Lhs, argList);
              }
            }

          }
        }
      } else if (expr is ExprDotName) {
        var edn = (ExprDotName)expr;
        var ns = edn.Lhs as NameSegment;
        if (ns != null && state.ContainsVariable(ns)) {
          var newLhs = state.GetLocalValue(ns);
          var lhs = newLhs as Expression;
          if (lhs != null)
            yield return new ExprDotName(edn.tok, lhs, edn.SuffixName, edn.OptTypeArguments);
        }
        yield return edn;
      } else if (expr is UnaryOpExpr) {
        var op = (UnaryOpExpr)expr;
        foreach (var result in EvaluateTacnyExpression(state, op.E)) {
          switch (op.Op) {
            case UnaryOpExpr.Opcode.Cardinality:
              if (!(result is IEnumerable)) {
                var resultExp = result is IVariable
                  ? Util.VariableToExpression(result as IVariable)
                  : result as Expression;
                yield return new UnaryOpExpr(op.tok, op.Op, resultExp);
              } else {
                var enumerator = result as IList;
                if (enumerator != null)
                  yield return new Dafny.LiteralExpr(op.tok, enumerator.Count);
              }
              yield break;
            case UnaryOpExpr.Opcode.Not:
              if (result is Dafny.LiteralExpr) {
                var lit = (Dafny.LiteralExpr)result;
                if (lit.Value is bool) {
                  // inverse the bool value
                  yield return new Dafny.LiteralExpr(op.tok, !(bool)lit.Value);
                } else {
                  Contract.Assert(false);
                  //TODO: error message
                }
              } else {
                var resultExp = result is IVariable ? Util.VariableToExpression(result as IVariable) : result as Expression;
                yield return new UnaryOpExpr(op.tok, op.Op, resultExp);
              }
              yield break;
            default:
              Contract.Assert(false, "Unsupported Unary Operator");
              yield break;
          }
        }
      } else if (expr is DisplayExpression) {
        var dexpr = (DisplayExpression)expr;
        if (dexpr.Elements.Count == 0) {
          yield return dexpr.Copy();
        } else {
          foreach (var item in ResolveDisplayExpression(state, dexpr)) {
            yield return item;
          }

        }
      } else { yield return expr; }
    }
Пример #5
0
    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();
    }
Пример #6
0
    public static IEnumerable<ProofState> EvalStep(ProofState state) {
      Contract.Requires<ArgumentNullException>(state != null, "state");

      IEnumerable<ProofState> enumerable = null;
      var stmt = state.GetStmt();

      //if the current frame is a control flow, e.g. match, no need to get stmt
      if(FlowControlMng.IsFlowControlFrame(state)) {
        enumerable = FlowControlMng.EvalNextControlFlow(stmt, state);
      } else {
        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 = EvalTacApp(us, state);
          } else {
            // apply atomic
            string sig = Util.GetSignature(us);
            //Firstly, check if this is a projection function
            var types =
              Assembly.GetAssembly(typeof(Atomic.Atomic))
                .GetTypes()
                .Where(t => t.IsSubclassOf(typeof(Atomic.Atomic)));
            foreach(var fType in types) {
              var porjInst = Activator.CreateInstance(fType) as Atomic.Atomic;
              if(sig == porjInst?.Signature) {
                //TODO: validate input countx
                enumerable = porjInst?.Generate(us, state);
              }
            }
          }
        } else if(stmt is AssignSuchThatStmt) {
          enumerable = EvalSuchThatStmt((AssignSuchThatStmt)stmt, state);
        } else if(stmt is PredicateStmt) {
          enumerable = EvalPredicateStmt((PredicateStmt)stmt, state);
        } else if(FlowControlMng.IsFlowControl(stmt)) {
          if(stmt is TacnyCasesBlockStmt) {
            enumerable = new Match().EvalInit(stmt, state);
          }
          //TODO: to implement if and while control flow
        } else {
          enumerable = DefaultAction(stmt, state);
        }
      }
      foreach(var item in enumerable)
        yield return item.Copy();
    }
Пример #7
0
        public static IEnumerable <object> EvaluateTacnyExpression(ProofState state, Expression expr)
        {
            Contract.Requires <ArgumentNullException>(state != null, "state");
            Contract.Requires <ArgumentNullException>(expr != null, "expr");
            if (expr is NameSegment)
            {
                var ns = (NameSegment)expr;
                if (state.HasLocalValue(ns.Name))
                {
                    yield return(state.GetLocalValue(ns.Name));
                }
                else
                {
                    yield return(ns);
                }
            }
            else if (expr is ApplySuffix)
            {
                var aps = (ApplySuffix)expr;
                if (state.IsTacticCall(aps))
                {
                    var us = new UpdateStmt(aps.tok, aps.tok, new List <Expression>()
                    {
                        aps.Lhs
                    },
                                            new List <AssignmentRhs>()
                    {
                        new ExprRhs(aps)
                    });
                    foreach (var item in ApplyNestedTactic(state, state.DafnyVars(), us).Select(x => x.GetGeneratedCode()))
                    {
                        yield return(item);
                    }
                }
                // rewrite the expression
                if (aps.Lhs is ExprDotName)
                {
                    foreach (var item in EvaluateTacnyExpression(state, aps.Lhs))
                    {
                        if (item is Expression)
                        {
                            yield return(new ApplySuffix(aps.tok, (Expression)item, aps.Args));
                        }
                        else
                        {
                            Contract.Assert(false, "Unexpected ExprNotName case");
                        }
                    }
                }
                else
                {
                    // try to evaluate as tacny expression
                    string sig = Util.GetSignature(aps);
                    // using reflection find all classes that extend EAtomic
                    var types =
                        Assembly.GetAssembly(typeof(EAtomic.EAtomic)).GetTypes().Where(t => t.IsSubclassOf(typeof(EAtomic.EAtomic)));
                    foreach (var type in types)
                    {
                        var resolverInstance = Activator.CreateInstance(type) as EAtomic.EAtomic;
                        if (sig == resolverInstance?.Signature)
                        {
                            //TODO: validate input countx
                            foreach (var item in resolverInstance?.Generate(aps, state))
                            {
                                yield return(item);
                            }
                        }
                        // if we reached this point, rewrite  the apply suffix
                        foreach (var item in EvaluateTacnyExpression(state, aps.Lhs))
                        {
                            if (!(item is NameSegment))
                            {
                                //TODO: warning
                            }
                            else
                            {
                                var argList = new List <Expression>();
                                foreach (var arg in aps.Args)
                                {
                                    foreach (var result in EvaluateTacnyExpression(state, arg))
                                    {
                                        if (result is Expression)
                                        {
                                            argList.Add(result as Expression);
                                        }
                                        else
                                        {
                                            argList.Add(Util.VariableToExpression(result as IVariable));
                                        }
                                        break;
                                    }
                                }
                                yield return(new ApplySuffix(aps.tok, aps.Lhs, argList));
                            }
                        }
                    }
                }
            }
            else if (expr is ExprDotName)
            {
                var edn = (ExprDotName)expr;
                var ns  = edn.Lhs as NameSegment;
                if (ns != null && state.ContainsVariable(ns))
                {
                    var newLhs = state.GetLocalValue(ns);
                    var lhs    = newLhs as Expression;
                    if (lhs != null)
                    {
                        yield return(new ExprDotName(edn.tok, lhs, edn.SuffixName, edn.OptTypeArguments));
                    }
                }
                yield return(edn);
            }
            else if (expr is UnaryOpExpr)
            {
                var op = (UnaryOpExpr)expr;
                foreach (var result in EvaluateTacnyExpression(state, op.E))
                {
                    switch (op.Op)
                    {
                    case UnaryOpExpr.Opcode.Cardinality:
                        if (!(result is IEnumerable))
                        {
                            var resultExp = result is IVariable
                  ? Util.VariableToExpression(result as IVariable)
                  : result as Expression;

                            yield return(new UnaryOpExpr(op.tok, op.Op, resultExp));
                        }
                        else
                        {
                            var enumerator = result as IList;
                            if (enumerator != null)
                            {
                                yield return(new Dafny.LiteralExpr(op.tok, enumerator.Count));
                            }
                        }
                        yield break;

                    case UnaryOpExpr.Opcode.Not:
                        if (result is Dafny.LiteralExpr)
                        {
                            var lit = (Dafny.LiteralExpr)result;
                            if (lit.Value is bool)
                            {
                                // inverse the bool value
                                yield return(new Dafny.LiteralExpr(op.tok, !(bool)lit.Value));
                            }
                            else
                            {
                                Contract.Assert(false);
                                //TODO: error message
                            }
                        }
                        else
                        {
                            var resultExp = result is IVariable?Util.VariableToExpression(result as IVariable) : result as Expression;

                            yield return(new UnaryOpExpr(op.tok, op.Op, resultExp));
                        }
                        yield break;

                    default:
                        Contract.Assert(false, "Unsupported Unary Operator");
                        yield break;
                    }
                }
            }
            else if (expr is DisplayExpression)
            {
                var dexpr = (DisplayExpression)expr;
                if (dexpr.Elements.Count == 0)
                {
                    yield return(dexpr.Copy());
                }
                else
                {
                    foreach (var item in ResolveDisplayExpression(state, dexpr))
                    {
                        yield return(item);
                    }
                }
            }
            else
            {
                yield return(expr);
            }
        }
Пример #8
0
        public static IEnumerable <ProofState> EvalStep(ProofState state)
        {
            Contract.Requires <ArgumentNullException>(state != null, "state");

            IEnumerable <ProofState> enumerable = null;
            var stmt = state.GetStmt();

            //if the current frame is a control flow, e.g. match, no need to get stmt
            if (FlowControlMng.IsFlowControlFrame(state))
            {
                enumerable = FlowControlMng.EvalNextControlFlow(stmt, state);
            }
            else
            {
                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 = EvalTacApp(us, state);
                    }
                    else
                    {
                        // apply atomic
                        string sig = Util.GetSignature(us);
                        //Firstly, check if this is a projection function
                        var types =
                            Assembly.GetAssembly(typeof(Atomic.Atomic))
                            .GetTypes()
                            .Where(t => t.IsSubclassOf(typeof(Atomic.Atomic)));
                        foreach (var fType in types)
                        {
                            var porjInst = Activator.CreateInstance(fType) as Atomic.Atomic;
                            if (sig == porjInst?.Signature)
                            {
                                //TODO: validate input countx
                                enumerable = porjInst?.Generate(us, state);
                            }
                        }
                    }
                }
                else if (stmt is AssignSuchThatStmt)
                {
                    enumerable = EvalSuchThatStmt((AssignSuchThatStmt)stmt, state);
                }
                else if (stmt is PredicateStmt)
                {
                    enumerable = EvalPredicateStmt((PredicateStmt)stmt, state);
                }
                else if (FlowControlMng.IsFlowControl(stmt))
                {
                    if (stmt is TacnyCasesBlockStmt)
                    {
                        enumerable = new Match().EvalInit(stmt, state);
                    }
                    //TODO: to implement if and while control flow
                }
                else
                {
                    enumerable = DefaultAction(stmt, state);
                }
            }
            foreach (var item in enumerable)
            {
                yield return(item.Copy());
            }
        }