public bool Visit(VCExprLet node, TextWriter wr)
        {
            //Contract.Requires(wr != null);
            //Contract.Requires(node != null);
            wr.Write("(Let ");

            string /*!*/
                sep = "";

            foreach (VCExprLetBinding /*!*/ b in node)
            {
                Contract.Assert(b != null);
                wr.Write(sep);
                sep = ", ";
                Print(b.V, wr);
                wr.Write(" = ");
                Print(b.E, wr);
            }

            wr.Write(" ");

            Print(node.Body, wr);
            wr.Write(")");
            return(true);
        }
Exemple #2
0
        /////////////////////////////////////////////////////////////////////////////////////

        public bool Visit(VCExprLet node, LineariserOptions options)
        {
            Namer.PushScope();
            try
            {
                foreach (VCExprLetBinding b in node)
                {
                    wr.Write("(let (");
                    Contract.Assert(b != null);
                    wr.Write("({0} ", Namer.GetQuotedName(b.V, b.V.Name));
                    Linearise(b.E, options);
                    wr.Write("))\n");
                }

                Linearise(node.Body, options);
                foreach (VCExprLetBinding b in node)
                {
                    wr.Write(")");
                }
                return(true);
            }
            finally
            {
                Namer.PopScope();
            }
        }
Exemple #3
0
        /// <summary>
        ///     Returns a mapping from blocks to corresponding verification conditions
        ///     Assumes that the expression defines a separate let binding for each block, where the name of the variable is given
        ///     by
        ///     "blockName_correct"
        /// </summary>
        public static IDictionary <Block, VCExpr> BlockToVCMapping(VCExprLet letExpr, CFGRepr cfg)
        {
            var blockToVC = new Dictionary <Block, VCExpr>();

            foreach (var binding in letExpr)
            {
                var predictionSuccess = PredictBlockName(binding.V.Name, out var predictedBlockName);
                if (predictionSuccess)
                {
                    try
                    {
                        var block = cfg.GetBlocksForwards().Single(b => b.Label.Equals(predictedBlockName));
                        blockToVC.Add(block, binding.E);
                    }
                    catch (Exception e)
                    {
                        throw new ProofGenUnexpectedStateException(typeof(VCBlockExtractor), e.Message);
                    }
                }
                else
                {
                    throw new ProofGenUnexpectedStateException(typeof(VCBlockExtractor),
                                                               "let binding does not correspond to block");
                }
            }

            if (blockToVC.Count != cfg.NumOfBlocks())
            {
                throw new ProofGenUnexpectedStateException(typeof(VCBlockExtractor),
                                                           "could not find let binding for all blocks");
            }

            return(blockToVC);
        }
Exemple #4
0
        public Term Visit(VCExprLet node, LineariserOptions options)
        {
            foreach (VCExprLetBinding b in node)
            {
                Term defAst = Linearise(b.E, options);
                letBindings.Add(b.V, defAst);
            }
            Term letAst = Linearise(node.Body, options);

            foreach (VCExprLetBinding b in node)
            {
                letBindings.Remove(b.V);
            }
            return(letAst);
        }
        protected override Dictionary <VCExprVar, Polarity> VisitAfterBinding(VCExprLet node, Polarity arg)
        {
            var result = node.Body.Accept(this, arg);

            for (int i = node.Length - 1; i >= 0; i--)
            {
                if (result.ContainsKey(node[i].V))
                {
                    result = Join(result, node[i].E.Accept(this, result[node[i].V]));
                }
            }
            foreach (var x in node.BoundVars)
            {
                result.Remove(x);
            }
            return(result);
        }
        /////////////////////////////////////////////////////////////////////////////////////

        public bool Visit(VCExprLet node, LineariserOptions options)
        {
            //Contract.Requires(options != null);
            //Contract.Requires(node != null);
            Namer.PushScope();
            try {
                wr.Write("(LET (");

                LineariserOptions optionsWithVars = options.AddLetVariables(node.BoundVars);
                Contract.Assert(optionsWithVars != null);

                string s = "(";
                foreach (VCExprLetBinding b in node)
                {
                    Contract.Assert(b != null);
                    wr.Write(s);
                    string printedName = Namer.GetLocalName(b.V, b.V.Name);

                    bool formula = b.V.Type.IsBool;
                    if (formula)
                    {
                        wr.Write("FORMULA ");
                    }
                    else
                    {
                        wr.Write("TERM ");
                    }
                    WriteId(printedName);
                    wr.Write(" ");
                    Linearise(b.E, optionsWithVars.SetAsTerm(!formula));
                    wr.Write(")");
                    s = " (";
                }
                wr.Write(") ");
                Linearise(node.Body, optionsWithVars);
                wr.Write(")");

                return(true);
            } finally {
                Namer.PopScope();
            }
        }
Exemple #7
0
        public override VCExpr Visit(VCExprLet node, FlattenerState state)
        {
            Contract.Requires(node != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);
            if (state.InTerm)
            {
                return(GetVarFor(node));
            }

            VCExprLet prelimRes = (VCExprLet)cce.NonNull(base.Visit(node, state));

            List <VCExprLetBinding /*!*/> /*!*/
            allBindings = new List <VCExprLetBinding /*!*/>();

            allBindings.AddRange(prelimRes);

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

            while (cont)
            {
                List <VCExprLetBinding /*!*/> /*!*/
                localBindings =
                    RemoveBindingsWithVars(prelimRes.BoundVars, new List <TypeVariable /*!*/>());
                if (localBindings.Count > 0)
                {
                    allBindings.AddRange(FlattenBindings(localBindings, state));
                }
                else
                {
                    cont = false;
                }
            }

            return(Gen.Let(allBindings, prelimRes.Body));
        }
 public Term Visit(VCExprLet node, bool arg)
 {
     throw new ProofGenUnexpectedStateException(GetType(),
                                                "only expect variables and function operations in extractors");
 }
        public override VCExpr Visit(VCExprLet node, bool arg)
        {
            Contract.Requires(node != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);
            IDictionary <VCExprVar /*!*/, Binding /*!*/> boundVars =
                new Dictionary <VCExprVar /*!*/, Binding /*!*/>();

            // recurse and collect the free variables in bound terms and formulae
            foreach (VCExprLetBinding /*!*/ binding in node)
            {
                Contract.Assert(binding != null);
                VCExpr  /*!*/
                    newE = Mutate(binding.E, arg);
                Binding /*!*/
                    b = new Binding(binding.V, newE, FreeVarsIn(newE));
                boundVars.Add(b.V, b);
            }

            // generate the occurrence edges
            foreach (KeyValuePair <VCExprVar /*!*/, Binding /*!*/> pair in boundVars)
            {
                Contract.Assert(cce.NonNullElements(pair));
                Binding /*!*/
                    b = pair.Value;
                Contract.Assert(b != null);
                foreach (VCExprVar /*!*/ v in b.FreeVars)
                {
                    Contract.Assert(v != null);
                    Binding b2;
                    if (boundVars.TryGetValue(v, out b2))
                    {
                        cce.NonNull(b2).Occurrences.Add(b);
                        b.InvOccurrencesNum = b.InvOccurrencesNum + 1;
                    }
                }
            }

            // topological sort
            Stack <Binding /*!*/> rootBindings = new Stack <Binding /*!*/>();

            foreach (KeyValuePair <VCExprVar /*!*/, Binding /*!*/> pair in boundVars)
            {
                Contract.Assert(cce.NonNullElements(pair));
                if (pair.Value.InvOccurrencesNum == 0)
                {
                    rootBindings.Push(pair.Value);
                }
            }

            List <Binding /*!*/> /*!*/
            sortedBindings = new List <Binding /*!*/>();

            while (rootBindings.Count > 0)
            {
                Binding /*!*/
                    b = rootBindings.Pop();
                Contract.Assert(b != null);
                sortedBindings.Add(b);
                foreach (Binding /*!*/ b2 in b.Occurrences)
                {
                    Contract.Assert(b2 != null);
                    b2.InvOccurrencesNum = b2.InvOccurrencesNum - 1;
                    if (b2.InvOccurrencesNum == 0)
                    {
                        rootBindings.Push(b2);
                    }
                }
            }

            if (boundVars.Any(pair => pair.Value.InvOccurrencesNum > 0))
            {
                System.Diagnostics.Debug.Fail("Cyclic let-bindings");
            }

            Contract.Assert(node.Length == sortedBindings.Count);

            // check which of the bindings can be dropped
            VCExpr newBody = Mutate(node.Body, arg);

            Contract.Assert(newBody != null);

            IDictionary <VCExprVar /*!*/, VCExprVar /*!*/> /*!*/
            usedVars =
                new Dictionary <VCExprVar /*!*/, VCExprVar /*!*/>();

            foreach (VCExprVar /*!*/ v in FreeVarsIn(newBody))
            {
                Contract.Assert(v != null);
                if (!usedVars.ContainsKey(v))
                {
                    usedVars.Add(v, v);
                }
            }

            for (int i = sortedBindings.Count - 1; i >= 0; --i)
            {
                if (usedVars.ContainsKey(sortedBindings[i].V))
                {
                    foreach (VCExprVar /*!*/ v in sortedBindings[i].FreeVars)
                    {
                        Contract.Assert(v != null);
                        if (!usedVars.ContainsKey(v))
                        {
                            usedVars.Add(v, v);
                        }
                    }
                }
                else
                {
                    sortedBindings.RemoveAt(i);
                }
            }

            // assemble the resulting let-expression
            List <VCExprLetBinding /*!*/> /*!*/
            newBindings = new List <VCExprLetBinding /*!*/>();

            foreach (Binding b in sortedBindings)
            {
                newBindings.Add(Gen.LetBinding(b.V, b.E));
            }

            return(Gen.Let(newBindings, newBody));
        }
Exemple #10
0
        /////////////////////////////////////////////////////////////////////////////////////

        public bool Visit(VCExprLet node, LineariserOptions options)
        {
            throw new NotImplementedException();
        }
Exemple #11
0
        //unsafe private static string getMemory(object o)
        //{
        //    //GCHandle h = GCHandle.Alloc(o, GCHandleType.Pinned);
        //    TypedReference tr = __makeref(o);
        //    IntPtr addr = **(IntPtr**)(&tr);
        //    return "0x" + addr.ToString("X");
        //}
        public override VCExpr Visit(VCExprLet node, bool arg)
        {
            Contract.Requires(node != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);
            IDictionary <VCExprVar /*!*/, Binding /*!*/> boundVars =
                new Dictionary <VCExprVar /*!*/, Binding /*!*/> ();


            //List<VCExpr> test = new List<VCExpr>();
            // recurse and collect the free variables in bound terms and formulae
            foreach (VCExprLetBinding /*!*/ binding in node)
            {
                Contract.Assert(binding != null);


                //Console.WriteLine("Before mutate, the original expr variables is : {0},  address is {1}", binding.V.Name, getMemory(binding.V));
                //Console.WriteLine("Before mutate, the original expr  is : {0}", binding.E.ToString());
                //Console.WriteLine("Before mutate, the original expr 's free variables are :");
                //List<VCExprVar> oExprs = FreeVarsIn(binding.E);
                //for (int i = 0; i < oExprs.Count; i++)
                //{
                //    //Console.WriteLine(getMemory(oExprs[i]));
                //    Console.WriteLine("Name : {0}, address {1}", oExprs[i].Name, getMemory(oExprs[i]));
                //}
                VCExpr /*!*/     newE  = Mutate(binding.E, arg);
                List <VCExprVar> exprs = FreeVarsIn(newE);
                Binding /*!*/    b     = new Binding(binding.V, newE, exprs);
                //Console.WriteLine("After mutate, the new expr is : {0}", newE.ToString());
                //Console.WriteLine("After mutate, the new exprssion's free variables are :");
                //for (int i = 0; i < exprs.Count; i++) {
                //    Console.WriteLine("Name : {0}, address {1}", exprs[i].Name, getMemory(exprs[i]));
                //}
                //Console.WriteLine("After init new object, the  new expr var is {0}, address is {1}:", b.V.Name, getMemory(b.V));
                //Console.WriteLine("After init new object, the  free variables are :");
                //for (int i = 0; i < b.FreeVars.Count; i++)
                //{
                //    Console.WriteLine("Name : {0}, address {1}", b.FreeVars[i].Name, getMemory(b.FreeVars[i]));
                //}
                //foreach (VCExprVar/*!*/ p in exprs)
                // Contract.Assert(p != null);
                // Console.WriteLine(getMemory(p));
                //}
                //foreach (VCExprVar/*!*/ v in b.FreeVars)
                //{
                //    Contract.Assert(v != null);
                //    Console.WriteLine(getMemory(v));
                //}
                boundVars.Add(b.V, b);
                //test.Add(b.V);
            }

            // generate the occurrence edges
            foreach (KeyValuePair <VCExprVar /*!*/, Binding /*!*/> pair in boundVars)
            {
                Contract.Assert(cce.NonNullElements(pair));
                Binding /*!*/ b = pair.Value;
                Contract.Assert(b != null);
                foreach (VCExprVar /*!*/ v in b.FreeVars)
                {
                    Contract.Assert(v != null);
                    Binding b2;
                    // Console.WriteLine("current is :\n {0}", v.ToString());
                    //Console.WriteLine(getMemory(v));
                    //foreach(VCExpr e in test){
                    //    Console.WriteLine(e.ToString());
                    //    Console.WriteLine(getMemory(e));
                    //    bool equal = v == e;
                    //    if (equal) {
                    //        Console.WriteLine("Correct");
                    //    }
                    //}
                    //bool equal = v ==
                    if (boundVars.TryGetValue(v, out b2))
                    {
                        cce.NonNull(b2).Occurrences.Add(b);
                        b.InvOccurrencesNum = b.InvOccurrencesNum + 1;
                    }
                }
            }

            // topological sort
            Stack <Binding /*!*/> rootBindings = new Stack <Binding /*!*/> ();

            foreach (KeyValuePair <VCExprVar /*!*/, Binding /*!*/> pair in boundVars)
            {
                Contract.Assert(cce.NonNullElements(pair));
                if (pair.Value.InvOccurrencesNum == 0)
                {
                    rootBindings.Push(pair.Value);
                }
            }

            List <Binding /*!*/> /*!*/ sortedBindings = new List <Binding /*!*/> ();

            while (rootBindings.Count > 0)
            {
                Binding /*!*/ b = rootBindings.Pop();
                Contract.Assert(b != null);
                sortedBindings.Add(b);
                foreach (Binding /*!*/ b2 in b.Occurrences)
                {
                    Contract.Assert(b2 != null);
                    b2.InvOccurrencesNum = b2.InvOccurrencesNum - 1;
                    if (b2.InvOccurrencesNum == 0)
                    {
                        rootBindings.Push(b2);
                    }
                }
            }

            if (boundVars.Any(pair => pair.Value.InvOccurrencesNum > 0))
            {
                System.Diagnostics.Debug.Fail("Cyclic let-bindings");
            }

            Contract.Assert(node.Length == sortedBindings.Count);

            // check which of the bindings can be dropped
            VCExpr newBody = Mutate(node.Body, arg);

            Contract.Assert(newBody != null);

            IDictionary <VCExprVar /*!*/, VCExprVar /*!*/> /*!*/ usedVars =
                new Dictionary <VCExprVar /*!*/, VCExprVar /*!*/> ();

            foreach (VCExprVar /*!*/ v in FreeVarsIn(newBody))
            {
                Contract.Assert(v != null);
                if (!usedVars.ContainsKey(v))
                {
                    usedVars.Add(v, v);
                }
            }

            for (int i = sortedBindings.Count - 1; i >= 0; --i)
            {
                if (usedVars.ContainsKey(sortedBindings[i].V))
                {
                    foreach (VCExprVar /*!*/ v in sortedBindings[i].FreeVars)
                    {
                        Contract.Assert(v != null);
                        if (!usedVars.ContainsKey(v))
                        {
                            usedVars.Add(v, v);
                        }
                    }
                }
                else
                {
                    sortedBindings.RemoveAt(i);
                }
            }

            // assemble the resulting let-expression
            List <VCExprLetBinding /*!*/> /*!*/ newBindings = new List <VCExprLetBinding /*!*/>();

            foreach (Binding b in sortedBindings)
            {
                newBindings.Add(Gen.LetBinding(b.V, b.E));
            }

            return(Gen.Let(newBindings, newBody));
        }