public override BinderExpr VisitBinderExpr(BinderExpr node)
        {
            var oldToNew = node.Dummies.ToDictionary(x => x, x => new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, prefix + x.Name, x.TypedIdent.Type), x.Attributes));

            foreach (var x in node.Dummies)
            {
                bound.Add(x, Expr.Ident(oldToNew[x]));
            }

            BinderExpr expr = base.VisitBinderExpr(node);

            expr.Dummies = node.Dummies.Select(x => oldToNew[x]).ToList <Variable>();

            // We process triggers of quantifer expressions here, because otherwise the
            // substitutions for bound variables have to be leaked outside this procedure.
            if (node is QuantifierExpr quantifierExpr)
            {
                if (quantifierExpr.Triggers != null)
                {
                    ((QuantifierExpr)expr).Triggers = this.VisitTrigger(quantifierExpr.Triggers);
                }
            }

            foreach (var x in node.Dummies)
            {
                bound.Remove(x);
            }

            return(expr);
        }
Esempio n. 2
0
        bool IEqualityComparer <Expr> .Equals(Expr x, Expr y)
        {
            var nx = deBruijn.Rename(x);
            var ny = deBruijn.Rename(y);

            return(BinderExpr.EqualWithAttributesAndTriggers(nx, ny));
        }
Esempio n. 3
0
 public override BinderExpr VisitBinderExpr(BinderExpr node) {
   base.VisitBinderExpr(node);
   // visit attributes, even though StandardVisitor does not do that (but maybe it should?)
   if (node.Attributes != null) {
     this.Visit(node.Attributes);
   }
   return node;
 }
Esempio n. 4
0
 public override BinderExpr VisitBinderExpr(BinderExpr node)
 {
     if (node.TypeParameters.Count > 0)
     {
         isMonomorphizable = false;
     }
     return(base.VisitBinderExpr(node));
 }
Esempio n. 5
0
 public virtual BinderExpr VisitBinderExpr(BinderExpr node)
 {
     Contract.Requires(node != null);
     Contract.Ensures(Contract.Result <BinderExpr>() != null);
     node.Body    = this.VisitExpr(node.Body);
     node.Dummies = this.VisitVariableSeq(node.Dummies);
     //node.Type = this.VisitType(node.Type);
     return(node);
 }
Esempio n. 6
0
            public override BinderExpr VisitBinderExpr(BinderExpr node)
            {
                var subst    = new Dictionary <Variable, Expr>();
                var newBound = new List <Variable>();

                foreach (var bv in node.Dummies)
                {
                    var bvNew = boundVars[boundVarCount++, bv.TypedIdent.Type];
                    newBound.Add(bvNew);
                    subst[bv] = new IdentifierExpr(Token.NoToken, bvNew);
                }
                node.Dummies = this.VisitVariableSeq(newBound);
                node.Body    = this.VisitExpr(Substituter.Apply(Substituter.SubstitutionFromHashtable(subst), node.Body));
                return(node);
            }
Esempio n. 7
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (!(obj is BinderExpr) ||
                this.Kind != ((BinderExpr)obj).Kind)
            {
                return(false);
            }

            BinderExpr other = (BinderExpr)obj;

            // Note, we consider quantifiers equal modulo the Triggers.
            return(object.Equals(this.TypeParameters, other.TypeParameters) &&
                   object.Equals(this.Dummies, other.Dummies) &&
                   object.Equals(this.Body, other.Body));
        }
Esempio n. 8
0
 public override BinderExpr VisitBinderExpr(BinderExpr node) {
   //Contract.Requires(node != null);
   Contract.Ensures(Contract.Result<BinderExpr>() != null);
   return base.VisitBinderExpr((BinderExpr)node.Clone());
 }
Esempio n. 9
0
 public override BinderExpr VisitBinderExpr(BinderExpr node)
 {
     // don't go into binder expression, since the terms in there may have more bound variables
     return(node);
 }
Esempio n. 10
0
 public override BinderExpr VisitBinderExpr(BinderExpr node)
 {
     if (node.TypeParameters.Count > 0)
       isMonomorphic = false;
       return base.VisitBinderExpr(node);
 }
Esempio n. 11
0
 public override BinderExpr VisitBinderExpr(BinderExpr node)
 {
     Contract.Ensures(Contract.Result<BinderExpr>() == node);
     this.VisitExpr(node.Body);
     this.VisitVariableSeq(node.Dummies);
     // this.VisitType(node.Type);
     return node;
 }
Esempio n. 12
0
 public override BinderExpr VisitBinderExpr(BinderExpr node)
 {
     Debug.Assert(false);
      	            return base.VisitBinderExpr(node);
 }
Esempio n. 13
0
 public override BinderExpr VisitBinderExpr(BinderExpr node) {
   // don't go into binder expression, since the terms in there may have more bound variables
   return node;
 }
Esempio n. 14
0
 public override BinderExpr VisitBinderExpr(BinderExpr node)
 {
     //Contract.Requires(node != null);
     Contract.Ensures(Contract.Result <BinderExpr>() != null);
     return(base.VisitBinderExpr((BinderExpr)node.Clone()));
 }
Esempio n. 15
0
      /// <summary>
      /// Performs lambda lifting (see <see cref="LambdaHelper.ExpandLambdas"/>) by replacing the lambda's
      /// free variables with bound ones.
      /// </summary>
      /// <param name="lambda">A lambda expression
      ///   <code>(lambda x1: T1 ... x_n: T_n :: t)</code>
      /// where <c>t</c> contains the free variables <c>y1</c>, ..., <c>y_m</c>.
      /// </param>
      /// <returns>
      /// <list type="bullet">
      ///   <item>
      ///     A function application <c>f(y1, ..., y_m)</c> where <c>f</c>'s body is defined to be the result of
      ///     replacing the free variables <c>y1</c>, ..., <c>y_m</c> in <c>t</c> with bound variables
      ///     <c>b1</c>, ..., <c>b_m</c>.
      ///   </item>
      ///   <item>
      ///     Adds a definition and axiom for <c>f</c> to <see cref="lambdaFunctions"/> and <see cref="lambdaAxioms"/>.
      ///     Memoizes <c>f</c> as the lifted lambda for <para>lambda</para>.
      ///   </item>
      /// </list>
      /// </returns>
      private Expr LiftLambdaFreeVars(LambdaExpr lambda)
      {
        // We start by getting rid of any use of "old" inside the lambda.  This is done as follows.
        // For each variable "g" occurring inside lambda as "old(... g ...)", create a new name "og".
        // Replace each old occurrence of "g" with "og", removing the enclosing "old" wrappers.
        var oldFinder = new OldFinder();
        oldFinder.Visit(lambda);
        var oldSubst = new Dictionary<Variable, Expr>(); // g -> g0
        var callOldMapping = new Dictionary<Variable, Expr>(); // g0 -> old(g)
        foreach (var v in oldFinder.FreeOldVars)
        {
          var g = v as GlobalVariable;
          if (g != null)
          {
            var g0 = new GlobalVariable(g.tok, new TypedIdent(g.tok, g.TypedIdent.Name + "@old", g.TypedIdent.Type));
            oldSubst.Add(g, new IdentifierExpr(g0.tok, g0));
            callOldMapping.Add(g0, new OldExpr(g0.tok, new IdentifierExpr(g.tok, g)));
          }
        }

        var lambdaBody = Substituter.ApplyReplacingOldExprs(
          Substituter.SubstitutionFromDictionary(new Dictionary<Variable, Expr>()),
          Substituter.SubstitutionFromDictionary(oldSubst),
          lambda.Body);
        var lambdaAttrs = Substituter.ApplyReplacingOldExprs(
          Substituter.SubstitutionFromDictionary(new Dictionary<Variable, Expr>()),
          Substituter.SubstitutionFromDictionary(oldSubst),
          lambda.Attributes);

        if (0 < CommandLineOptions.Clo.VerifySnapshots &&
            QKeyValue.FindStringAttribute(lambdaAttrs, "checksum") == null)
        {
          // Attach a dummy checksum to avoid issues in the dependency analysis.
          var checksumAttr = new QKeyValue(lambda.tok, "checksum", new List<object> {"lambda expression"}, null);
          if (lambdaAttrs == null)
          {
            lambdaAttrs = checksumAttr;
          }
          else
          {
            lambdaAttrs.AddLast(checksumAttr);
          }
        }

        // this is ugly, the output will depend on hashing order
        var subst = new Dictionary<Variable, Expr>();
        var substFnAttrs = new Dictionary<Variable, Expr>();
        var formals = new List<Variable>();
        var callArgs = new List<Expr>();
        var axCallArgs = new List<Expr>();
        var dummies = new List<Variable>(lambda.Dummies);
        var freeTypeVars = new List<TypeVariable>();
        var fnTypeVarActuals = new List<Type /*!*/>();
        var freshTypeVars = new List<TypeVariable>(); // these are only used in the lambda@n function's definition

        // compute the free variables of the lambda expression, but with lambdaBody instead of lambda.Body
        Set freeVars = new Set();
        BinderExpr.ComputeBinderFreeVariables(lambda.TypeParameters, lambda.Dummies, lambdaBody, null, lambdaAttrs,
          freeVars);

        foreach (object o in freeVars)
        {
          // 'o' is either a Variable or a TypeVariable.
          if (o is Variable)
          {
            var v = o as Variable;
            var ti = new TypedIdent(v.TypedIdent.tok, v.TypedIdent.Name, v.TypedIdent.Type);
            var f = new Formal(v.tok, ti, true);
            formals.Add(f);
            substFnAttrs.Add(v, new IdentifierExpr(f.tok, f));
            var b = new BoundVariable(v.tok, ti);
            dummies.Add(b);
            if (callOldMapping.ContainsKey(v))
            {
              callArgs.Add(callOldMapping[v]);
            }
            else
            {
              callArgs.Add(new IdentifierExpr(v.tok, v));
            }

            Expr id = new IdentifierExpr(b.tok, b);
            subst.Add(v, id);
            axCallArgs.Add(id);
          }
          else
          {
            var tv = (TypeVariable) o;
            freeTypeVars.Add(tv);
            fnTypeVarActuals.Add(tv);
            freshTypeVars.Add(new TypeVariable(tv.tok, tv.Name));
          }
        }

        var sw = new System.IO.StringWriter();
        var wr = new TokenTextWriter(sw, true);
        lambda.Emit(wr);
        string lam_str = sw.ToString();

        FunctionCall fcall;
        IToken tok = lambda.tok;
        Formal res = new Formal(tok, new TypedIdent(tok, TypedIdent.NoName, cce.NonNull(lambda.Type)), false);

        if (liftedLambdas.TryGetValue(lambda, out fcall))
        {
          if (CommandLineOptions.Clo.TraceVerify)
          {
            Console.WriteLine("Old lambda: {0}", lam_str);
          }
        }
        else
        {
          if (CommandLineOptions.Clo.TraceVerify)
          {
            Console.WriteLine("New lambda: {0}", lam_str);
          }

          Function fn = new Function(tok, FreshLambdaFunctionName(), freshTypeVars, formals, res,
            "auto-generated lambda function",
            Substituter.Apply(Substituter.SubstitutionFromDictionary(substFnAttrs), lambdaAttrs));
          fn.OriginalLambdaExprAsString = lam_str;

          fcall = new FunctionCall(new IdentifierExpr(tok, fn.Name));
          fcall.Func = fn; // resolve here
          liftedLambdas[lambda] = fcall;

          List<Expr /*!*/> selectArgs = new List<Expr /*!*/>();
          foreach (Variable /*!*/ v in lambda.Dummies)
          {
            Contract.Assert(v != null);
            selectArgs.Add(new IdentifierExpr(v.tok, v));
          }

          NAryExpr axcall = new NAryExpr(tok, fcall, axCallArgs);
          axcall.Type = res.TypedIdent.Type;
          axcall.TypeParameters = SimpleTypeParamInstantiation.From(freeTypeVars, fnTypeVarActuals);
          NAryExpr select = Expr.Select(axcall, selectArgs);
          select.Type = lambdaBody.Type;
          List<Type /*!*/> selectTypeParamActuals = new List<Type /*!*/>();
          List<TypeVariable> forallTypeVariables = new List<TypeVariable>();
          foreach (TypeVariable /*!*/ tp in lambda.TypeParameters)
          {
            Contract.Assert(tp != null);
            selectTypeParamActuals.Add(tp);
            forallTypeVariables.Add(tp);
          }

          forallTypeVariables.AddRange(freeTypeVars);
          select.TypeParameters = SimpleTypeParamInstantiation.From(lambda.TypeParameters, selectTypeParamActuals);

          Expr bb = Substituter.Apply(Substituter.SubstitutionFromDictionary(subst), lambdaBody);
          NAryExpr body = Expr.Eq(select, bb);
          body.Type = Type.Bool;
          body.TypeParameters = SimpleTypeParamInstantiation.EMPTY;
          Trigger trig = new Trigger(select.tok, true, new List<Expr> {select});

          lambdaFunctions.Add(fn);
          lambdaAxioms.Add(new ForallExpr(tok, forallTypeVariables, dummies,
            Substituter.Apply(Substituter.SubstitutionFromDictionary(subst), lambdaAttrs),
            trig, body));
        }

        NAryExpr call = new NAryExpr(tok, fcall, callArgs);
        call.Type = res.TypedIdent.Type;
        call.TypeParameters = SimpleTypeParamInstantiation.From(freeTypeVars, fnTypeVarActuals);

        return call;
      }
Esempio n. 16
0
 public virtual BinderExpr VisitBinderExpr(BinderExpr node) {
   Contract.Requires(node != null);
   Contract.Ensures(Contract.Result<BinderExpr>() != null);
   node.Body = this.VisitExpr(node.Body);
   node.Dummies = this.VisitVariableSeq(node.Dummies);
   //node.Type = this.VisitType(node.Type);
   return node;
 }
Esempio n. 17
0
 public override BinderExpr VisitBinderExpr(BinderExpr node) {
   base.VisitBinderExpr(node);
   // visit attributes, even though StandardVisitor does not do that (but maybe it should?)
   if (node.Attributes != null) {
     this.Visit(node.Attributes);
   }
   return node;
 }