protected override Dictionary <VCExprVar, Polarity> VisitAfterBinding(VCExprQuantifier node, Polarity arg)
        {
            var result = node.Body.Accept(this, arg);

            foreach (var x in node.BoundVars)
            {
                result.Remove(x);
            }
            return(result);
        }
Example #2
0
        public override bool Visit(VCExprQuantifier node, bool arg)
        {
            Contract.Requires(node != null);
            foreach (VCExprVar v in node.BoundVars)
            {
                Contract.Assert(v != null);
                RegisterType(v.Type);
            }

            return(base.Visit(node, arg));
        }
Example #3
0
        /////////////////////////////////////////////////////////////////////////////////////

        public bool Visit(VCExprQuantifier node, LineariserOptions options)
        {
            //Contract.Requires(node != null);
            //Contract.Requires(options != null);
            AssertAsFormula(node.Quan.ToString(), options);
            Contract.Assert(node.TypeParameters.Count == 0);

            Namer.PushScope(); try {
                string kind = node.Quan == Quantifier.ALL ? "!" : "?";
                wr.Write("{0} [", kind);

                for (int i = 0; i < node.BoundVars.Count; i++)
                {
                    VCExprVar var = node.BoundVars[i];
                    Contract.Assert(var != null);
                    // ensure that the variable name starts with ?
                    string printedName = Namer.GetLocalName(var, "V" + MakeIdPrintable(var.Name));
                    Contract.Assert(printedName != null);
                    Contract.Assert(printedName[0] == 'V');
                    if (i > 0)
                    {
                        wr.Write(",");
                    }
                    wr.Write("{0}", printedName);
                }

                wr.Write("] : (");

                /*      if (options.QuantifierIds) {
                 * // only needed for Z3
                 * VCQuantifierInfos! infos = node.Infos;
                 * if (infos.qid != null) {
                 *  wr.Write("(QID ");
                 *  wr.Write(infos.qid);
                 *  wr.Write(") ");
                 * }
                 * if (0 <= infos.uniqueId) {
                 *  wr.Write("(SKOLEMID ");
                 *  wr.Write(infos.uniqueId);
                 *  wr.Write(") ");
                 * }
                 * } */

                Linearise(node.Body, options);

                // WriteTriggers(node.Triggers, options);
                wr.Write(")");

                return(true);
            } finally {
                Namer.PopScope();
            }
        }
        private void InstantiateQuantifier(VCExprQuantifier quantifierExpr)
        {
            var boundVariableToLabels = quantifierExpr.Info.instantiationLabels;
            var boundVariableToExprs  = boundVariableToLabels.Keys.ToDictionary(
                boundVariable => boundVariable,
                boundVariable =>
                boundVariableToLabels[boundVariable]
                .SelectMany(label =>
                            accLabelToInstances.ContainsKey(label) ? accLabelToInstances[label] : new HashSet <VCExpr>()).ToHashSet());

            ConstructInstances(quantifierExpr, boundVariableToExprs, 0, new List <VCExpr>());
        }
        public override Dictionary <VCExprVar, Polarity> Visit(VCExprQuantifier node, Polarity arg)
        {
            var result = base.Visit(node, arg);

            if (arg != Polarity.Unknown && !result.Keys.Intersect(BoundTermVars).Any())
            {
                if ((arg == Polarity.Positive) == (node.Quan == Quantifier.EX))
                {
                    quantifiers.Add(node);
                }
            }
            return(result);
        }
 private VCExpr AugmentWithInstances(VCExprQuantifier quantifierExpr)
 {
     if (quantifierExpr.Quan == Quantifier.ALL)
     {
         return(vcExprGen.And(quantifierExpr,
                              vcExprGen.NAry(VCExpressionGenerator.AndOp,
                                             quantifierInstantiationInfo[quantifierExpr].instances.Values.ToList())));
     }
     else
     {
         return(vcExprGen.Or(quantifierExpr,
                             vcExprGen.NAry(VCExpressionGenerator.OrOp,
                                            quantifierInstantiationInfo[quantifierExpr].instances.Values.ToList())));
     }
 }
        private void ConstructInstances(VCExprQuantifier quantifierExpr,
                                        Dictionary <VCExprVar, HashSet <VCExpr> > boundVariableToExprs, int n, List <VCExpr> instance)
        {
            if (quantifierExpr.BoundVars.Count == n)
            {
                InstantiateQuantifierAtInstance(quantifierExpr, instance);
                return;
            }
            var boundVariable = quantifierExpr.BoundVars[n];

            foreach (var expr in boundVariableToExprs[boundVariable])
            {
                instance.Add(expr);
                ConstructInstances(quantifierExpr, boundVariableToExprs, n + 1, instance);
                instance.RemoveAt(n);
            }
        }
        public VCExpr BindQuantifier(VCExprQuantifier node)
        {
            if (node.TypeParameters.Count > 0)
            {
                return(node);
            }
            var boundVariableToLabels = node.Info.instantiationLabels;

            if (boundVariableToLabels.Count < node.BoundVars.Count ||
                boundVariableToLabels.Any(kv => kv.Value.Count == 0))
            {
                return(node);
            }
            var v = new VCExprVar($"{quantifierBindingNamePrefix}{quantifierBinding.Count}", Type.Bool);

            quantifierBinding[v] = node;
            quantifierInstantiationInfo[node] = new QuantifierInstantiationInfo(boundVariableToLabels);
            return(v);
        }
        private void InstantiateQuantifierAtInstance(VCExprQuantifier quantifierExpr, List <VCExpr> instance)
        {
            var quantifierInstantiationInfo = this.quantifierInstantiationInfo[quantifierExpr];

            if (quantifierInstantiationInfo.instances.ContainsKey(instance))
            {
                return;
            }
            var subst = new VCExprSubstitution(
                Enumerable.Range(0, quantifierExpr.BoundVars.Count).ToDictionary(
                    x => quantifierExpr.BoundVars[x],
                    x => instance[x]), new Dictionary <TypeVariable, Type>());
            var substituter   = new SubstitutingVCExprVisitor(vcExprGen);
            var instantiation = substituter.Mutate(quantifierExpr.Body, subst);

            quantifierInstantiationInfo.instances[new List <VCExpr>(instance)] =
                Skolemizer.Skolemize(this, quantifierExpr.Quan == Quantifier.ALL ? Polarity.Positive : Polarity.Negative,
                                     instantiation);
        }
        private VCExpr PerformSkolemization(VCExprQuantifier node, bool arg)
        {
            var oldToNew = node.BoundVars.ToDictionary(v => v, v => (VCExpr)qiEngine.FreshSkolemConstant(v));

            foreach (var x in node.BoundVars)
            {
                bound.Add(x, oldToNew[x]);
            }
            var retExpr = (VCExprQuantifier)base.Visit(node, arg);

            retExpr.Info.instantiationExprs.Iter(kv =>
            {
                kv.Value.Iter(expr => { qiEngine.AddTerm(kv.Key, expr.Accept(this, arg)); });
            });
            foreach (var x in node.BoundVars)
            {
                bound.Remove(x);
            }
            return(retExpr.Body);
        }
Example #11
0
        private VCExpr HandleQuantifier(VCExprQuantifier node, List <VCExprVar /*!*/> /*!*/ newBoundVars, VariableBindings bindings)
        {
            Contract.Requires(bindings != null);
            Contract.Requires(node != null);
            Contract.Requires(cce.NonNullElements(newBoundVars));
            Contract.Ensures(Contract.Result <VCExpr>() != null);
            List <VCTrigger /*!*/> /*!*/ newTriggers = MutateTriggers(node.Triggers, bindings);

            Contract.Assert(cce.NonNullElements(newTriggers));
            VCExpr /*!*/ newBody = Mutate(node.Body, bindings);

            Contract.Assert(newBody != null);
            newBody = AxBuilder.Cast(newBody, Type.Bool);

            if (newBoundVars.Count == 0) // might happen that no bound variables are left
            {
                return(newBody);
            }
            return(Gen.Quantify(node.Quan, new List <TypeVariable /*!*/>(), newBoundVars,
                                newTriggers, node.Infos, newBody));
        }
Example #12
0
        ////////////////////////////////////////////////////////////////////////////

        public override VCExpr Visit(VCExprQuantifier node, VariableBindings oldBindings)
        {
            Contract.Requires(oldBindings != null);
            Contract.Requires(node != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);
            VariableBindings /*!*/
                bindings = oldBindings.Clone();

            // bound term variables are replaced with bound term variables
            // typed in a simpler way
            List <VCExprVar /*!*/> /*!*/
            newBoundVars =
                BoundVarsAfterErasure(node.BoundVars, bindings);

            // type variables are replaced with ordinary quantified variables
            GenBoundVarsForTypeParams(node.TypeParameters, newBoundVars, bindings);
            VCExpr /*!*/
                newNode = HandleQuantifier(node, newBoundVars, bindings);

            Contract.Assert(newNode != null);

            if (!(newNode is VCExprQuantifier) || !IsUniversalQuantifier(node))
            {
                return(newNode);
            }

            VariableBindings /*!*/
                bindings2;

            if (!RedoQuantifier(node, (VCExprQuantifier)newNode, node.BoundVars, oldBindings,
                                out bindings2, out newBoundVars))
            {
                return(newNode);
            }

            GenBoundVarsForTypeParams(node.TypeParameters, newBoundVars, bindings2);
            return(HandleQuantifier(node, newBoundVars, bindings2));
        }
Example #13
0
        public override VCExpr Visit(VCExprQuantifier node, FlattenerState state)
        {
            Contract.Requires(node != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);
            if (state.InTerm)
            {
                return(GetVarFor(node));
            }

            // we only flatten within the matrix of the quantified formula,
            // not within the triggers (since SMT-solvers do not seem to
            // appreciate triggers with let-binders)
            VCExpr newBody = Mutate(node.Body, state);

            // Check whether any of the extracted terms contain variables
            // bound by this quantifier. In this case, we have to add
            // let-binders and remove the extracted terms
            bool cont = true;

            while (cont)
            {
                List <VCExprLetBinding /*!*/> /*!*/
                localBindings =
                    RemoveBindingsWithVars(node.BoundVars, node.TypeParameters);
                Contract.Assert(cce.NonNullElements(localBindings));
                if (localBindings.Count > 0)
                {
                    newBody = AddBindings(localBindings, newBody, state);
                }
                else
                {
                    cont = false;
                }
            }

            return(Gen.Quantify(node.Quan, node.TypeParameters, node.BoundVars, node.Triggers, node.Infos, newBody));
        }
        /////////////////////////////////////////////////////////////////////////////////////

        public bool Visit(VCExprQuantifier node, LineariserOptions options)
        {
            //Contract.Requires(options != null);
            //Contract.Requires(node != null);
            AssertAsFormula(node.Quan.ToString(), options);
            Contract.Assert(node.TypeParameters.Count == 0);

            Namer.PushScope();
            try {
                string kind = node.Quan == Quantifier.ALL ? "FORALL" : "EXISTS";
                wr.Write("({0} (", kind);

                for (int i = 0; i < node.BoundVars.Count; i++)
                {
                    VCExprVar var = node.BoundVars[i];
                    Contract.Assert(var != null);
                    string printedName = Namer.GetLocalName(var, var.Name);
                    Contract.Assert(printedName != null);
                    if (i != 0)
                    {
                        wr.Write(" ");
                    }
                    WriteId(printedName);
                    if (options.UseTypes)
                    {
                        wr.Write(" :TYPE {0}", TypeToString(var.Type));
                    }
                }
                wr.Write(") ");

                WriteTriggers(node.Triggers, options);

                if (options.QuantifierIds)
                {
                    // only needed for Z3
                    VCQuantifierInfos infos = node.Infos;
                    Contract.Assert(infos != null);
                    if (infos.qid != null)
                    {
                        wr.Write("(QID ");
                        wr.Write(infos.qid);
                        wr.Write(") ");
                    }
                    if (0 <= infos.uniqueId)
                    {
                        wr.Write("(SKOLEMID ");
                        wr.Write(infos.uniqueId);
                        wr.Write(") ");
                    }
                }

                if (options.UseWeights)
                {
                    int weight = QKeyValue.FindIntAttribute(node.Infos.attributes, "weight", 1);
                    if (weight != 1)
                    {
                        wr.Write("(WEIGHT ");
                        wr.Write(weight);
                        wr.Write(") ");
                    }
                }

                Linearise(node.Body, options);
                wr.Write(")");

                return(true);
            } finally {
                Namer.PopScope();
            }
        }
Example #15
0
        /////////////////////////////////////////////////////////////////////////////////////

        public bool Visit(VCExprQuantifier node, LineariserOptions options)
        {
            Contract.Assert(node.TypeParameters.Count == 0);

            UnderQuantifier++;
            Namer.PushScope(); try {
                string kind = node.Quan == Quantifier.ALL ? "forall" : "exists";
                wr.Write("({0} (", kind);

                for (int i = 0; i < node.BoundVars.Count; i++)
                {
                    VCExprVar var = node.BoundVars[i];
                    Contract.Assert(var != null);
                    string printedName = Namer.GetQuotedLocalName(var, var.Name);
                    Contract.Assert(printedName != null);
                    wr.Write("({0} {1}) ", printedName, TypeToString(var.Type));
                }

                wr.Write(") ");

                VCQuantifierInfos infos = node.Infos;
                var weight = QKeyValue.FindIntAttribute(infos.attributes, "weight", 1);
                if (!ProverOptions.UseWeights)
                {
                    weight = 1;
                }
                var hasAttrs = node.Triggers.Count > 0 || infos.qid != null || weight != 1 || infos.uniqueId != -1;

                if (hasAttrs)
                {
                    wr.Write("(! ");
                }

                Linearise(node.Body, options);

                if (hasAttrs)
                {
                    wr.Write("\n");
                    if (infos.qid != null)
                    {
                        wr.Write(" :qid {0}\n", SMTLibNamer.QuoteId(infos.qid));
                    }
                    if (weight != 1)
                    {
                        wr.Write(" :weight {0}\n", weight);
                    }
                    if (infos.uniqueId != -1)
                    {
                        wr.Write(" :skolemid |{0}|\n", infos.uniqueId);
                    }
                    WriteTriggers(node.Triggers, options);

                    wr.Write(")");
                }

                wr.Write(")");

                return(true);
            } finally {
                UnderQuantifier--;
                Namer.PopScope();
            }
        }
 public Term Visit(VCExprQuantifier node, bool arg)
 {
     throw new ProofGenUnexpectedStateException(GetType(),
                                                "only expect variables and function operations in extractors");
 }
Example #17
0
        public bool Visit(VCExprQuantifier node, TextWriter wr)
        {
            //Contract.Requires(wr != null);
            //Contract.Requires(node != null);
            string /*!*/ quan = node.Quan == Quantifier.ALL ? "Forall" : "Exists";

            Contract.Assert(quan != null);

            wr.Write("({0} ", quan);

            if (node.TypeParameters.Count > 0)
            {
                wr.Write("<");
                string /*!*/ sep = "";
                foreach (TypeVariable /*!*/ v in node.TypeParameters)
                {
                    Contract.Assert(v != null);
                    wr.Write(sep);
                    sep = ", ";
                    wr.Write("{0}", v.Name);
                }
                wr.Write("> ");
            }

            if (node.BoundVars.Count > 0)
            {
                string /*!*/ sep = "";
                foreach (VCExprVar /*!*/ v in node.BoundVars)
                {
                    Contract.Assert(v != null);
                    wr.Write(sep);
                    sep = ", ";
                    Print(v, wr);
                }
                wr.Write(" ");
            }

            wr.Write(":: ");

            if (node.Triggers.Count > 0)
            {
                wr.Write("{0} ", "{");
                string /*!*/ sep = "";
                foreach (VCTrigger /*!*/ t in node.Triggers)
                {
                    Contract.Assert(t != null);
                    wr.Write(sep);
                    sep = ", ";
                    string /*!*/ sep2 = "";
                    foreach (VCExpr /*!*/ e in t.Exprs)
                    {
                        Contract.Assert(e != null);
                        wr.Write(sep2);
                        sep2 = "+";
                        Print(e, wr);
                    }
                }
                wr.Write(" {0} ", "}");
            }

            Print(node.Body, wr);
            wr.Write(")");
            return(true);
        }
    private VCExpr HandleQuantifier(VCExprQuantifier node, List<VCExprVar/*!*/>/*!*/ newBoundVars, VariableBindings bindings){
Contract.Requires(bindings != null);
Contract.Requires(node != null);
Contract.Requires(cce.NonNullElements(newBoundVars));
Contract.Ensures(Contract.Result<VCExpr>() != null);
      List<VCTrigger/*!*/>/*!*/ newTriggers = MutateTriggers(node.Triggers, bindings);
      Contract.Assert(cce.NonNullElements(newTriggers));
      VCExpr/*!*/ newBody = Mutate(node.Body, bindings);
      Contract.Assert(newBody != null);
      newBody = AxBuilder.Cast(newBody, Type.Bool);

      if (newBoundVars.Count == 0)  // might happen that no bound variables are left
        return newBody;
      return Gen.Quantify(node.Quan, new List<TypeVariable/*!*/>(), newBoundVars,
                          newTriggers, node.Infos, newBody);
    }
    ////////////////////////////////////////////////////////////////////////////

    public override VCExpr Visit(VCExprQuantifier node, VariableBindings oldBindings) {
      Contract.Requires(oldBindings != null);
      Contract.Requires(node != null);
      Contract.Ensures(Contract.Result<VCExpr>() != null);
      VariableBindings/*!*/ bindings = oldBindings.Clone();

      // bound term variables are replaced with bound term variables
      // typed in a simpler way
      List<VCExprVar/*!*/>/*!*/ newBoundVars =
        BoundVarsAfterErasure(node.BoundVars, bindings);

      // type variables are replaced with ordinary quantified variables
      GenBoundVarsForTypeParams(node.TypeParameters, newBoundVars, bindings);
      VCExpr/*!*/ newNode = HandleQuantifier(node, newBoundVars, bindings);
      Contract.Assert(newNode != null);

      if (!(newNode is VCExprQuantifier) || !IsUniversalQuantifier(node))
        return newNode;

      VariableBindings/*!*/ bindings2;
      if (!RedoQuantifier(node, (VCExprQuantifier)newNode, node.BoundVars, oldBindings,
                          out bindings2, out newBoundVars))
        return newNode;

      GenBoundVarsForTypeParams(node.TypeParameters, newBoundVars, bindings2);
      return HandleQuantifier(node, newBoundVars, bindings2);
    }
        private VCExpr HandleQuantifier(VCExprQuantifier node, List<VCExprVar/*!*/>/*!*/ occurringVars/*!*/, List<VCExprVar/*!*/>/*!*/ newBoundVars, VariableBindings bindings)
        {
            Contract.Requires(bindings != null);
              Contract.Requires(node != null);
              Contract.Requires(cce.NonNullElements(occurringVars/*!*/));
              Contract.Requires(cce.NonNullElements(newBoundVars));
              Contract.Ensures(Contract.Result<VCExpr>() != null);
              List<VCExprLetBinding/*!*/>/*!*/ typeVarBindings =
            AxBuilderPremisses.GenTypeParamBindings(node.TypeParameters, occurringVars, bindings, true);
              Contract.Assert(typeVarBindings != null);
              // Check whether some of the type parameters could not be
              // determined from the bound variable types. In this case, we
              // quantify explicitly over these variables
              if (typeVarBindings.Count < node.TypeParameters.Count) {
            foreach (TypeVariable/*!*/ var in node.TypeParameters) {
              Contract.Assert(var != null);
              if (typeVarBindings.All(b => !b.V.Equals(var)))
            newBoundVars.Add((VCExprVar)bindings.TypeVariableBindings[var]);
            }
              }

              // the lists of old and new bound variables for which type
              // antecedents are to be generated
              List<VCExprVar/*!*/>/*!*/ varsWithTypeSpecs = new List<VCExprVar/*!*/>();
              List<VCExprVar/*!*/>/*!*/ newVarsWithTypeSpecs = new List<VCExprVar/*!*/>();
              if (!IsUniversalQuantifier(node) ||
              CommandLineOptions.Clo.TypeEncodingMethod
                == CommandLineOptions.TypeEncoding.Predicates) {
            foreach (VCExprVar/*!*/ oldVar in occurringVars) {
              Contract.Assert(oldVar != null);
              varsWithTypeSpecs.Add(oldVar);
              newVarsWithTypeSpecs.Add(bindings.VCExprVarBindings[oldVar]);
            }
              } // else, no type antecedents are created for any variables

              List<VCTrigger/*!*/>/*!*/ furtherTriggers;
              VCExpr/*!*/ typePremisses =
            GenTypePremisses(varsWithTypeSpecs, newVarsWithTypeSpecs,
                         bindings.TypeVariableBindings,
                         typeVarBindings, out furtherTriggers);

              Contract.Assert(cce.NonNullElements(furtherTriggers));
              Contract.Assert(typePremisses != null);
              List<VCTrigger/*!*/>/*!*/ newTriggers = MutateTriggers(node.Triggers, bindings);
              Contract.Assert(cce.NonNullElements(newTriggers));
              newTriggers.AddRange(furtherTriggers);
              newTriggers = AddLets2Triggers(newTriggers, typeVarBindings);

              VCExpr/*!*/ newBody = Mutate(node.Body, bindings);
              Contract.Assert(newBody != null);

              // assemble the new quantified formula

              if (CommandLineOptions.Clo.TypeEncodingMethod
                == CommandLineOptions.TypeEncoding.None) {
            typePremisses = VCExpressionGenerator.True;
              }

              VCExpr/*!*/ bodyWithPremisses =
            AxBuilderPremisses.AddTypePremisses(typeVarBindings, typePremisses,
                                            node.Quan == Quantifier.ALL,
                                            AxBuilder.Cast(newBody, Type.Bool));
              Contract.Assert(bodyWithPremisses != null);
              if (newBoundVars.Count == 0)  // might happen that no bound variables are left
            return bodyWithPremisses;

              foreach (VCExprVar/*!*/ v in newBoundVars) {
            Contract.Assert(v != null);
            if (v.Type == AxBuilderPremisses.U) {
              newTriggers.Add(Gen.Trigger(false, AxBuilderPremisses.Cast(v, Type.Int)));
              newTriggers.Add(Gen.Trigger(false, AxBuilderPremisses.Cast(v, Type.Bool)));
            }
              }

              return Gen.Quantify(node.Quan, new List<TypeVariable/*!*/>(), newBoundVars,
                          newTriggers, node.Infos, bodyWithPremisses);
        }
        ////////////////////////////////////////////////////////////////////////////
        public override VCExpr Visit(VCExprQuantifier node, VariableBindings oldBindings)
        {
            Contract.Requires(oldBindings != null);
              Contract.Requires(node != null);
              Contract.Ensures(Contract.Result<VCExpr>() != null);
              VariableBindings bindings = oldBindings.Clone();

              // determine the bound vars that actually occur in the body or
              // in any of the triggers (if some variables do not occur, we
              // need to take special care of type parameters that only occur
              // in the types of such variables)
              FreeVariableCollector coll = new FreeVariableCollector();
              coll.Collect(node.Body);
              foreach (VCTrigger trigger in node.Triggers) {
            if (trigger.Pos)
              foreach (VCExpr/*!*/ e in trigger.Exprs) {
            Contract.Assert(e != null);

            coll.Collect(e);
              }
              }

              List<VCExprVar/*!*/> occurringVars = new List<VCExprVar/*!*/>(node.BoundVars.Count);
              foreach (VCExprVar var in node.BoundVars)
            if (coll.FreeTermVars.ContainsKey(var))
              occurringVars.Add(var);

              occurringVars.TrimExcess();

              // bound term variables are replaced with bound term variables typed in
              // a simpler way
              List<VCExprVar/*!*/>/*!*/ newBoundVars =
            BoundVarsAfterErasure(occurringVars, bindings);
              Contract.Assert(cce.NonNullElements(newBoundVars));
              VCExpr/*!*/ newNode = HandleQuantifier(node, occurringVars,
                                         newBoundVars, bindings);
              Contract.Assert(newNode != null);

              if (!(newNode is VCExprQuantifier) || !IsUniversalQuantifier(node))
            return newNode;

              VariableBindings bindings2;
              if (!RedoQuantifier(node, (VCExprQuantifier)newNode, occurringVars, oldBindings,
                          out bindings2, out newBoundVars))
            return newNode;

              return HandleQuantifier(node, occurringVars,
                              newBoundVars, bindings2);
        }