private bool OnlyRefersToConstants(Expr e)
        {
            VariableCollector vc = new VariableCollector();

            vc.Visit(e);
            return(vc.usedVars.OfType <Constant>().Count() == vc.usedVars.Count());
        }
        private IEnumerable <VariableDescriptor> GetReferencedVariables(Absy node, string proc)
        {
            var VarCollector = new VariableCollector();

            VarCollector.Visit(node);
            return(VarCollector.usedVars.Where(Item => VariableRelevantToAnalysis(Item, proc)).
                   Select(Item => MakeDescriptor(proc, Item)));
        }
        public override Expr VisitIdentifierExpr(IdentifierExpr node)
        {
            VariableCollector vc = new VariableCollector();

            vc.Visit(node);
            var existConsts = vc.usedVars.Where(x => QKeyValue.FindBoolAttribute(x.Attributes, "existential"));

            existConsts.Iter(x => usedExistentialConstants.Add(x));
            return(base.VisitIdentifierExpr(node));
        }
        private void MergeIgnoredAnnotations()
        {
            var IgnoredAnnotationsToVariables = new Dictionary <string, HashSet <Variable> >();

            foreach (var c in AllAnnotationIdentifiers())
            {
                IgnoredAnnotationsToVariables[c] = new HashSet <Variable>();
            }

            foreach (var ci in AnnotationInstances())
            {
                if (!IgnoredAnnotationsToVariables.ContainsKey(ci.AnnotationIdentifier))
                {
                    continue;
                }

                VariableCollector vc = new VariableCollector();
                vc.Visit(ci.Expr);
                if (vc.usedVars.Select(Item => varDepAnalyser.VariableRelevantToAnalysis(Item, ci.Proc)).Count() != 0)
                {
                    continue;
                }

                foreach (var v in vc.usedVars)
                {
                    if (varDepAnalyser.Ignoring(v, ci.Proc))
                    {
                        IgnoredAnnotationsToVariables[ci.AnnotationIdentifier].Add(v);
                    }
                }
            }

            foreach (var c in IgnoredAnnotationsToVariables.Keys)
            {
                foreach (var d in IgnoredAnnotationsToVariables.Keys)
                {
                    if (c == d)
                    {
                        continue;
                    }

                    if (IgnoredAnnotationsToVariables[c].Equals(IgnoredAnnotationsToVariables[d]))
                    {
                        AnnotationDependences.AddEdge(c, d);
                    }
                }
            }
        }
Exemple #5
0
        public Tuple <string, IEnumerable <LemmaDecl> > GenerateTactic(Expr e)
        {
            var varCollector = new VariableCollector();

            varCollector.Visit(e);

            var lookupTyThms = new List <string>();
            var funMemThms   = new List <string>();

            foreach (var v in varCollector.usedVars)
            {
                try
                {
                    lookupTyThms.Add(programAccessor.LookupVarTyLemma(v));
                }
                catch
                {
                    //variable not found, possible if, for example, v is bound
                }
            }

            var usedFuncs = functionCollector.UsedFunctions(e);

            foreach (var f in usedFuncs)
            {
                funMemThms.Add(programAccessor.MembershipLemma(f));
            }

            var hintLemmas = equalityHintGenerator.GetHints(e);

            var hintsML    = MLUtil.IsaToMLThms(hintLemmas.Select(lemma => lemma.Name));
            var lookupTyML = MLUtil.IsaToMLThms(lookupTyThms);
            var funMemML   = MLUtil.IsaToMLThms(funMemThms);

            var args = new List <string>
            {
                MLUtil.ContextAntiquotation(),
                hintsML,
                lookupTyML,
                funMemML
            };

            var tactic = ProofUtil.Apply(ProofUtil.MLTactic("typing_tac " + string.Join(" ", args), 1));

            return(Tuple.Create(tactic, hintLemmas));
        }
Exemple #6
0
        private HashSet <VariableDescriptor> UsedVars(Expr e)
        {
            VariableCollector vc = new VariableCollector();

            vc.Visit(e);
            HashSet <VariableDescriptor> result = new HashSet <VariableDescriptor>();

            foreach (var v in vc.usedVars)
            {
                var vd = MakeDescriptor(v);
                if (vd != null)
                {
                    result.Add(vd);
                }
            }
            return(result);
        }
Exemple #7
0
        public override Expr VisitNAryExpr(NAryExpr node)
        {
            if (node.Fun is FunctionCall)
            {
                var collector = new VariableCollector();
                collector.Visit(node);

                if (existentialExpr != null && existentialExpr.Dummies.Intersect(collector.usedVars).Any())
                {
                    functionsUsed.Add(Tuple.Create((node.Fun as FunctionCall).Func, existentialExpr));
                }
                else
                {
                    functionsUsed.Add(Tuple.Create <Function, ExistsExpr>((node.Fun as FunctionCall).Func, null));
                }
            }

            return(base.VisitNAryExpr(node));
        }
    private static void PropagateCall(CallCmd cc, HashSet <Variable> liveVarsAfter, Program program)
    {
        // globals U in-params U (after - out-params)
        cc.Outs.Where(ie => ie != null).Iter(ie => liveVarsAfter.Remove(ie.Decl));
        if (program != null)
        {
            program.TopLevelDeclarations
            .OfType <GlobalVariable>()
            .Iter(v => liveVarsAfter.Add(v));
        }

        VariableCollector /*!*/ collector = new VariableCollector();

        cc.Ins.Where(e => e != null).Iter(e => collector.Visit(e));
        if (program == null)
        {
            liveVarsAfter.UnionWith(collector.usedVars.Where(v => v is LocalVariable || v is Formal));
        }
        else
        {
            liveVarsAfter.UnionWith(collector.usedVars);
        }
    }
        private static void UpdateLiveSet(Cmd cmd, HashSet <Variable> liveVars)
        {
            if (cmd is AssignCmd assignCmd)
            {
                foreach (var lhs in assignCmd.Lhss)
                {
                    liveVars.Remove(lhs.DeepAssignedVariable);
                }

                foreach (var rhs in assignCmd.Rhss)
                {
                    var collector = new VariableCollector();
                    collector.Visit(rhs);
                    //we neglect that the corresponding lhs may be dead
                    liveVars.UnionWith(collector.usedVars);
                }
            }
            else if (cmd is HavocCmd havocCmd)
            {
                foreach (var ie in havocCmd.Vars)
                {
                    liveVars.Remove(ie.Decl);
                }
            }
            else if (cmd is PredicateCmd predicateCmd)
            {
                //live set is not cleared when reaching "assert false" or "assume false"
                var collector = new VariableCollector();
                collector.Visit(predicateCmd.Expr);
                liveVars.UnionWith(collector.usedVars);
            }
            else
            {
                throw new ProofGenUnexpectedStateException(
                          "Unsupported command in program right before passification.");
            }
        }
Exemple #10
0
        public AtomicActionInfo(Procedure proc, Ensures ensures, MoverType moverType, int layerNum, int availableUptoLayerNum)
            : base(proc, layerNum, availableUptoLayerNum)
        {
            this.ensures       = ensures;
            this.moverType     = moverType;
            this.gate          = new List <AssertCmd>();
            this.action        = ensures.Condition as CodeExpr;
            this.thisGate      = new List <AssertCmd>();
            this.thisInParams  = new List <Variable>();
            this.thisOutParams = new List <Variable>();
            this.thatGate      = new List <AssertCmd>();
            this.thatInParams  = new List <Variable>();
            this.thatOutParams = new List <Variable>();
            this.hasAssumeCmd  = false;
            this.thisMap       = new Dictionary <Variable, Expr>();
            this.thatMap       = new Dictionary <Variable, Expr>();

            foreach (Block block in this.action.Blocks)
            {
                block.Cmds.ForEach(x => this.hasAssumeCmd = this.hasAssumeCmd || x is AssumeCmd);
            }

            foreach (Block block in this.action.Blocks)
            {
                if (block.TransferCmd is ReturnExprCmd)
                {
                    block.TransferCmd = new ReturnCmd(block.TransferCmd.tok);
                }
            }

            var cmds = this.action.Blocks[0].Cmds;

            for (int i = 0; i < cmds.Count; i++)
            {
                AssertCmd assertCmd = cmds[i] as AssertCmd;
                if (assertCmd == null)
                {
                    break;
                }
                this.gate.Add(assertCmd);
                cmds[i] = new AssumeCmd(assertCmd.tok, Expr.True);
            }

            foreach (Variable x in proc.InParams)
            {
                Variable thisx = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "this_" + x.Name, x.TypedIdent.Type), true, x.Attributes);
                this.thisInParams.Add(thisx);
                this.thisMap[x] = Expr.Ident(thisx);
                Variable thatx = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "that_" + x.Name, x.TypedIdent.Type), true, x.Attributes);
                this.thatInParams.Add(thatx);
                this.thatMap[x] = Expr.Ident(thatx);
            }
            foreach (Variable x in proc.OutParams)
            {
                Variable thisx = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "this_" + x.Name, x.TypedIdent.Type), false, x.Attributes);
                this.thisOutParams.Add(thisx);
                this.thisMap[x] = Expr.Ident(thisx);
                Variable thatx = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "that_" + x.Name, x.TypedIdent.Type), false, x.Attributes);
                this.thatOutParams.Add(thatx);
                this.thatMap[x] = Expr.Ident(thatx);
            }
            List <Variable> thisLocVars = new List <Variable>();
            List <Variable> thatLocVars = new List <Variable>();

            foreach (Variable x in this.action.LocVars)
            {
                Variable thisx = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "this_" + x.Name, x.TypedIdent.Type), false);
                thisMap[x] = Expr.Ident(thisx);
                thisLocVars.Add(thisx);
                Variable thatx = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "that_" + x.Name, x.TypedIdent.Type), false);
                thatMap[x] = Expr.Ident(thatx);
                thatLocVars.Add(thatx);
            }
            Contract.Assume(proc.TypeParameters.Count == 0);
            Substitution thisSubst = Substituter.SubstitutionFromHashtable(this.thisMap);
            Substitution thatSubst = Substituter.SubstitutionFromHashtable(this.thatMap);

            foreach (AssertCmd assertCmd in this.gate)
            {
                this.thisGate.Add((AssertCmd)Substituter.Apply(thisSubst, assertCmd));
                this.thatGate.Add((AssertCmd)Substituter.Apply(thatSubst, assertCmd));
            }
            this.thisAction = new CodeExpr(thisLocVars, SubstituteBlocks(this.action.Blocks, thisSubst, "this_"));
            this.thatAction = new CodeExpr(thatLocVars, SubstituteBlocks(this.action.Blocks, thatSubst, "that_"));

            {
                VariableCollector collector = new VariableCollector();
                collector.Visit(this.action);
                this.actionUsedGlobalVars = new HashSet <Variable>(collector.usedVars.Where(x => x is GlobalVariable));
            }

            List <Variable> modifiedVars = new List <Variable>();

            foreach (Block block in this.action.Blocks)
            {
                block.Cmds.ForEach(cmd => cmd.AddAssignedVariables(modifiedVars));
            }
            this.modifiedGlobalVars = new HashSet <Variable>(modifiedVars.Where(x => x is GlobalVariable));

            {
                VariableCollector collector = new VariableCollector();
                this.gate.ForEach(assertCmd => collector.Visit(assertCmd));
                this.gateUsedGlobalVars = new HashSet <Variable>(collector.usedVars.Where(x => x is GlobalVariable));
            }
        }
 private void MergeIgnoredCandidates()
 {
     var IgnoredCandidatesToVariables = new Dictionary<string, HashSet<Variable>>();
       foreach(var c in candidates) {
     IgnoredCandidatesToVariables[c] = new HashSet<Variable>();
       }
       foreach(var ci in CandidateInstances()) {
     if(!IgnoredCandidatesToVariables.ContainsKey(ci.Candidate)) {
       continue;
     }
     VariableCollector vc = new VariableCollector();
     vc.Visit(ci.Expr);
     if(vc.usedVars.Select(Item => varDepAnalyser.VariableRelevantToAnalysis(Item, ci.Proc)).Count() != 0) {
       continue;
     }
     foreach(var v in vc.usedVars) {
       if(varDepAnalyser.Ignoring(v, ci.Proc)) {
     IgnoredCandidatesToVariables[ci.Candidate].Add(v);
       }
     }
       }
       foreach(var c in IgnoredCandidatesToVariables.Keys) {
     foreach(var d in IgnoredCandidatesToVariables.Keys) {
       if(c == d) {
     continue;
       }
       if(IgnoredCandidatesToVariables[c].Equals(IgnoredCandidatesToVariables[d])) {
     CandidateDependences.AddEdge(c, d);
       }
     }
       }
 }
    // perform in place update of liveSet
    public static void Propagate(Cmd cmd, HashSet <Variable /*!*/> /*!*/ liveSet, bool allGlobalsAreLive)
    {
        Contract.Requires(cmd != null);
        Contract.Requires(cce.NonNullElements(liveSet));
        if (cmd is AssignCmd)
        {
            AssignCmd /*!*/ assignCmd = (AssignCmd)cce.NonNull(cmd);
            // I must first iterate over all the targets and remove the live ones.
            // After the removals are done, I must add the variables referred on
            // the right side of the removed targets

            AssignCmd     simpleAssignCmd = assignCmd.AsSimpleAssignCmd;
            HashSet <int> indexSet        = new HashSet <int>();
            int           index           = 0;
            foreach (AssignLhs /*!*/ lhs in simpleAssignCmd.Lhss)
            {
                Contract.Assert(lhs != null);
                SimpleAssignLhs salhs = lhs as SimpleAssignLhs;
                Contract.Assert(salhs != null);
                Variable var = salhs.DeepAssignedVariable;
                if (var != null && (liveSet.Contains(var) || (allGlobalsAreLive && var is GlobalVariable)))
                {
                    indexSet.Add(index);
                    liveSet.Remove(var);
                }
                index++;
            }
            index = 0;
            foreach (Expr /*!*/ expr in simpleAssignCmd.Rhss)
            {
                Contract.Assert(expr != null);
                if (indexSet.Contains(index))
                {
                    VariableCollector /*!*/ collector = new VariableCollector();
                    collector.Visit(expr);
                    if (allGlobalsAreLive)
                    {
                        liveSet.UnionWith(collector.usedVars.Where(v => v is LocalVariable || v is Formal));
                    }
                    else
                    {
                        liveSet.UnionWith(collector.usedVars);
                    }
                }
                index++;
            }
        }
        else if (cmd is HavocCmd)
        {
            HavocCmd /*!*/ havocCmd = (HavocCmd)cmd;
            foreach (IdentifierExpr /*!*/ expr in havocCmd.Vars)
            {
                Contract.Assert(expr != null);
                if (expr.Decl != null)
                {
                    liveSet.Remove(expr.Decl);
                }
            }
        }
        else if (cmd is PredicateCmd)
        {
            Contract.Assert((cmd is AssertCmd || cmd is AssumeCmd));
            PredicateCmd /*!*/ predicateCmd = (PredicateCmd)cce.NonNull(cmd);
            if (predicateCmd.Expr is LiteralExpr)
            {
                LiteralExpr le = (LiteralExpr)predicateCmd.Expr;
                if (le.IsFalse)
                {
                    liveSet.Clear();
                }
            }
            else
            {
                VariableCollector /*!*/ collector = new VariableCollector();
                collector.Visit(predicateCmd.Expr);
                if (allGlobalsAreLive)
                {
                    liveSet.UnionWith(collector.usedVars.Where(v => v is LocalVariable || v is Formal));
                }
                else
                {
                    liveSet.UnionWith(collector.usedVars);
                }
            }
        }
        else if (cmd is CommentCmd)
        {
            // comments are just for debugging and don't affect verification
        }
        else if (cmd is SugaredCmd)
        {
            SugaredCmd /*!*/ sugCmd = (SugaredCmd)cce.NonNull(cmd);
            Propagate(sugCmd.Desugaring, liveSet, allGlobalsAreLive);
        }
        else if (cmd is StateCmd)
        {
            StateCmd /*!*/   stCmd = (StateCmd)cce.NonNull(cmd);
            List <Cmd> /*!*/ cmds  = cce.NonNull(stCmd.Cmds);
            int len = cmds.Count;
            for (int i = len - 1; i >= 0; i--)
            {
                Propagate(cmds[i], liveSet, allGlobalsAreLive);
            }
            foreach (Variable /*!*/ v in stCmd.Locals)
            {
                Contract.Assert(v != null);
                liveSet.Remove(v);
            }
        }
        else
        {
            {
                Contract.Assert(false);
                throw new cce.UnreachableException();
            }
        }
    }
Exemple #13
0
        public AtomicActionInfo(Procedure proc, Ensures ensures, MoverType moverType, int layerNum, int availableUptoLayerNum)
            : base(proc, layerNum, availableUptoLayerNum)
        {
            CodeExpr codeExpr = ensures.Condition as CodeExpr;

            this.ensures       = ensures;
            this.moverType     = moverType;
            this.thisGate      = new List <AssertCmd>();
            this.thisAction    = codeExpr;
            this.thisInParams  = new List <Variable>();
            this.thisOutParams = new List <Variable>();
            this.thatGate      = new List <AssertCmd>();
            this.thatInParams  = new List <Variable>();
            this.thatOutParams = new List <Variable>();
            this.hasAssumeCmd  = false;

            foreach (Block block in codeExpr.Blocks)
            {
                block.Cmds.ForEach(x => this.hasAssumeCmd = this.hasAssumeCmd || x is AssumeCmd);
            }

            var cmds = thisAction.Blocks[0].Cmds;

            for (int i = 0; i < cmds.Count; i++)
            {
                AssertCmd assertCmd = cmds[i] as AssertCmd;
                if (assertCmd == null)
                {
                    break;
                }
                thisGate.Add(assertCmd);
                cmds[i] = new AssumeCmd(assertCmd.tok, Expr.True);
            }

            Dictionary <Variable, Expr> map = new Dictionary <Variable, Expr>();

            foreach (Variable x in proc.InParams)
            {
                this.thisInParams.Add(x);
                Variable y = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "that_" + x.Name, x.TypedIdent.Type), true, x.Attributes);
                this.thatInParams.Add(y);
                map[x] = Expr.Ident(y);
            }
            foreach (Variable x in proc.OutParams)
            {
                this.thisOutParams.Add(x);
                Variable y = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "that_" + x.Name, x.TypedIdent.Type), false, x.Attributes);
                this.thatOutParams.Add(y);
                map[x] = Expr.Ident(y);
            }
            List <Variable> thatLocVars = new List <Variable>();

            foreach (Variable x in thisAction.LocVars)
            {
                Variable y = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "that_" + x.Name, x.TypedIdent.Type), false);
                map[x] = Expr.Ident(y);
                thatLocVars.Add(y);
            }
            Contract.Assume(proc.TypeParameters.Count == 0);
            Substitution subst = Substituter.SubstitutionFromHashtable(map);

            foreach (AssertCmd assertCmd in thisGate)
            {
                thatGate.Add((AssertCmd)Substituter.Apply(subst, assertCmd));
            }
            Dictionary <Block, Block> blockMap = new Dictionary <Block, Block>();
            List <Block> thatBlocks            = new List <Block>();

            foreach (Block block in thisAction.Blocks)
            {
                List <Cmd> otherCmds = new List <Cmd>();
                foreach (Cmd cmd in block.Cmds)
                {
                    otherCmds.Add(Substituter.Apply(subst, cmd));
                }
                Block thatBlock = new Block();
                thatBlock.Cmds  = otherCmds;
                thatBlock.Label = "that_" + block.Label;
                block.Label     = "this_" + block.Label;
                thatBlocks.Add(thatBlock);
                blockMap[block] = thatBlock;
                if (block.TransferCmd is GotoCmd)
                {
                    GotoCmd gotoCmd = block.TransferCmd as GotoCmd;
                    for (int i = 0; i < gotoCmd.labelNames.Count; i++)
                    {
                        gotoCmd.labelNames[i] = "this_" + gotoCmd.labelNames[i];
                    }
                }
            }
            foreach (Block block in thisAction.Blocks)
            {
                if (block.TransferCmd is ReturnExprCmd)
                {
                    block.TransferCmd           = new ReturnCmd(block.TransferCmd.tok);
                    blockMap[block].TransferCmd = new ReturnCmd(block.TransferCmd.tok);
                    continue;
                }
                List <Block>  thatGotoCmdLabelTargets = new List <Block>();
                List <string> thatGotoCmdLabelNames   = new List <string>();
                GotoCmd       gotoCmd = block.TransferCmd as GotoCmd;
                foreach (Block target in gotoCmd.labelTargets)
                {
                    thatGotoCmdLabelTargets.Add(blockMap[target]);
                    thatGotoCmdLabelNames.Add(blockMap[target].Label);
                }
                blockMap[block].TransferCmd = new GotoCmd(block.TransferCmd.tok, thatGotoCmdLabelNames, thatGotoCmdLabelTargets);
            }
            this.thatAction = new CodeExpr(thatLocVars, thatBlocks);

            {
                VariableCollector collector = new VariableCollector();
                collector.Visit(codeExpr);
                this.actionUsedGlobalVars = new HashSet <Variable>(collector.usedVars.Where(x => x is GlobalVariable));
            }

            List <Variable> modifiedVars = new List <Variable>();

            foreach (Block block in codeExpr.Blocks)
            {
                block.Cmds.ForEach(cmd => cmd.AddAssignedVariables(modifiedVars));
            }
            this.modifiedGlobalVars = new HashSet <Variable>(modifiedVars.Where(x => x is GlobalVariable));

            {
                VariableCollector collector = new VariableCollector();
                this.thisGate.ForEach(assertCmd => collector.Visit(assertCmd));
                this.gateUsedGlobalVars = new HashSet <Variable>(collector.usedVars.Where(x => x is GlobalVariable));
            }
        }
 private bool OnlyRefersToConstants(Expr e) {
   VariableCollector vc = new VariableCollector();
   vc.Visit(e);
   return vc.usedVars.OfType<Constant>().Count() == vc.usedVars.Count();
 }