예제 #1
0
        public void MSO_Succ_Z3A()
        {
            var z3Context = new Context();
            var S         = z3Context.IntSort;
            var solver    = new Z3BoolAlg(z3Context, S);
            Func <int, BoolExpr> IsPos = (i => z3Context.MkEq(solver.x, z3Context.MkInt(i)));
            var x      = new Variable("x", true);
            var y      = new Variable("y", true);
            var x_is_0 = new MSOPredicate <BoolExpr>(IsPos(0), x);
            var y_is_1 = new MSOPredicate <BoolExpr>(IsPos(1), y);
            //every 0 is immediately followed by a 1
            MSOFormula <BoolExpr> phi = Forall(x, Implies(x_is_0, Exists(y, And(Succ(x, y), y_is_1))));

            //var ca = new CartesianAlgebraBDD<BoolExpr>(solver);
            var aut = phi.GetAutomaton(solver);
            //aut.ShowGraph();
            //Automaton<BoolExpr> aut1 = Automaton<BoolExpr>.ProjectSecond<BDD>(aut);

            var expected_automaton = Automaton <BoolExpr> .Create(solver, 0, new int[] { 0 },
                                                                  new Move <BoolExpr>[] {
                Move <BoolExpr> .Create(0, 0, z3Context.MkNot(IsPos(0))),
                Move <BoolExpr> .Create(0, 1, IsPos(0)),
                Move <BoolExpr> .Create(1, 0, IsPos(1))
            });

            bool equiv = aut.IsEquivalentWith(expected_automaton);
            //Assert.IsTrue(equiv, "the automata must be equivalent");
        }
예제 #2
0
        public void TestWS1S_GetAutomatonBDD_eq_GetAutomaton()
        {
            var solver = new CharSetSolver(BitWidth.BV7);
            //var nrOfLabelBits = (int)BitWidth.BV7;
            var isDigit  = solver.MkCharSetFromRegexCharClass(@"\d");
            var isLetter = solver.MkCharSetFromRegexCharClass(@"(c|C)");
            var x        = new Variable("x", false);
            var y        = new Variable("y", false);
            var z        = new Variable("z", false);
            var X        = new Variable("X", false);
            //there are at least two distinct positions x and y
            var xy = new MSOAnd <BDD>(new MSONot <BDD>(new MSOEq <BDD>(x, y)), new MSOAnd <BDD>(new MSOIsSingleton <BDD>(x), new MSOIsSingleton <BDD>(y)));
            //there is a set X containing x and y and all positions z in X have characters that satisfy isWordLetter
            var x_sub_X    = new MSOSubset <BDD>(x, X);
            var y_sub_X    = new MSOSubset <BDD>(y, X);
            var z_sub_X    = new MSOSubset <BDD>(z, X);
            var isletter_z = new MSOPredicate <BDD>(isLetter, z);
            var psi        = new MSOExists <BDD>(X, (x_sub_X & y_sub_X & ~(new MSOExists <BDD>(z, ~((~((new MSOIsSingleton <BDD>(z)) & z_sub_X)) | isletter_z)))));

            var atLeast2w   = xy & psi;
            var atLeast2wEE = new MSOExists <BDD>(x, (new MSOExists <BDD>(y, atLeast2w)));
            var autBDD      = atLeast2w.GetAutomaton(solver);
            var ca          = new CartesianAlgebraBDD <BDD>(solver);
            var autPROD     = atLeast2w.GetAutomaton(ca);
            //autBDD.ShowGraph("autBDD");
            //autPROD.ShowGraph("autPROD");
            var aut_atLeast2wEE1 = BasicAutomata.Restrict(atLeast2wEE.GetAutomaton(ca));
            var aut_atLeast2wEE2 = atLeast2wEE.GetAutomaton(solver);

            //aut_atLeast2wEE1.ShowGraph("aut_atLeast2wEE1");
            //aut_atLeast2wEE2.ShowGraph("aut_atLeast2wEE2");
            Assert.IsTrue(aut_atLeast2wEE1.IsEquivalentWith(aut_atLeast2wEE2));
        }
예제 #3
0
 public void TestWS1S_Label()
 {
     var solver  = new CharSetSolver(BitWidth.BV7);
     var x       = new Variable("X", false);
     var pred    = new MSOPredicate <BDD>(solver.MkCharConstraint('c'), x);
     var ca      = new CartesianAlgebraBDD <BDD>(solver);
     var lab     = pred & new MSOIsSingleton <BDD>(x);
     var lab_aut = lab.GetAutomaton(ca);
     //lab_aut.ShowGraph("lab_aut");
 }
예제 #4
0
 public void BooleanAlgebraZ3_test1()
 {
     var ctx    = new Context();
     var sort   = ctx.IntSort;
     var solver = new Z3BoolAlg(ctx, sort);
     var alg    = new BDDAlgebra <BoolExpr>(solver);
     var x      = new Variable("x", true);
     var pred   = new MSOPredicate <BoolExpr>(ctx.MkEq(solver.x, ctx.MkNumeral(42, sort)), x);
     MSOFormula <BoolExpr> phi = new MSOExists <BoolExpr>(x, pred);
     var pred_aut = pred.GetAutomaton(alg);
     //pred_aut.ShowGraph("pred_aut");
 }
예제 #5
0
 public void TestWS1S_NotLabel()
 {
     var solver = new CharSetSolver(BitWidth.BV7);
     //var x1 = new Variable("x1", false);
     var x    = new Variable("x", false);
     var pred = new MSOPredicate <BDD>(solver.MkCharConstraint('c'), x);
     var fo_x = new MSOIsSingleton <BDD>(x);
     var ca   = new CartesianAlgebraBDD <BDD>(solver);
     var lab  = new MSOAnd <BDD>(pred, fo_x);
     MSOFormula <BDD> not_lab = new MSONot <BDD>(lab);
     var not_lab_actual       = new MSOAnd <BDD>(not_lab, fo_x);
     var aut_not_lab          = not_lab_actual.GetAutomaton(ca);
     var aut_not_lab_prelim   = not_lab.GetAutomaton(ca);
     var c_aut_lab            = lab.GetAutomaton(ca).Complement().Minimize();
     //c_aut_lab.ShowGraph("c_aut_lab");
     //aut_not_lab.ShowGraph("aut_not_lab");
     //aut_not_lab_prelim.ShowGraph("aut_not_lab_prelim");
     //TBD: equivalence
 }
예제 #6
0
        public void TestMSO_Pred()
        {
            var solver = new CharSetSolver(BitWidth.BV16);
            var x = new Variable("x", true);
            var pred = new MSOPredicate<BDD>(solver.MkCharConstraint( 'c'), x);
            MSOFormula<BDD> phi = new MSOExists<BDD>(x, pred);

            var ca = new CartesianAlgebraBDD<BDD>(solver);
            var pred_aut = pred.GetAutomaton(ca);
            //pred_aut.ShowGraph("pred_aut");

            var aut = phi.GetAutomaton(solver);
            for (int i = 0; i < 10; i++)
            {
                var s = solver.GenerateMember(aut);
                Assert.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(s, "c"), "regex mismatch");
            }
            var aut2 = solver.RegexConverter.Convert("c", System.Text.RegularExpressions.RegexOptions.Singleline);
            //aut2.ShowGraph("aut2");
            //aut.ShowGraph("aut");
            Assert.IsTrue(aut2.IsEquivalentWith(aut), "automata not equialent");
        }
예제 #7
0
        public void MSO_Succ_Z3A()
        {
            var z3Context = new Context();
            var S         = z3Context.IntSort;
            var solver    = new Z3BoolAlg(z3Context, S);
            Func <int, BoolExpr> IsInt = (i => z3Context.MkEq(solver.x, z3Context.MkInt(i)));
            var x_is_0 = new MSOPredicate <BoolExpr>(IsInt(0), "x");
            var y_is_1 = new MSOPredicate <BoolExpr>(IsInt(1), "y");
            //every 0 is immediately followed by a 1
            MSOFormula <BoolExpr> phi = Forall("x", Implies(x_is_0, Exists("y", And(Succ("x", "y"), y_is_1))));

            var aut = phi.GetAutomaton(solver);

            var expected_automaton = Automaton <BoolExpr> .Create(solver, 0, new int[] { 0 },
                                                                  new Move <BoolExpr>[] {
                Move <BoolExpr> .Create(0, 0, z3Context.MkNot(IsInt(0))),
                Move <BoolExpr> .Create(0, 1, IsInt(0)),
                Move <BoolExpr> .Create(1, 0, IsInt(1))
            });

            bool equiv = aut.IsEquivalentWith(expected_automaton, solver);

            Assert.IsTrue(equiv, "the automata must be equivalent");
        }
예제 #8
0
        public static void Run()
        {
            var sw = new Stopwatch();

            //ex x1 x2... a(x1) /\ a(x2).../\ x1<x2...
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"..\mso-minterm-p1.csv"))
            {
                file.WriteLine("k, old, cartesian, trie, minterm");
                for (int size = 2; size < kminterm; size++)
                {
                    var solver           = new CharSetSolver(BitWidth.BV64);
                    MSOFormula <BDD> phi = new MSOTrue <BDD>();

                    //x1<x2 /\...
                    for (int k = 1; k < size; k++)
                    {
                        var leq = new MSOSuccN <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true), 1);
                        phi = new MSOAnd <BDD>(phi, leq);
                    }

                    //ai(xi) /\ ...
                    for (int k = 0; k < size; k++)
                    {
                        var axk = new MSOPredicate <BDD>(solver.MkBitTrue(k), new Variable("x" + k, true));
                        phi = new MSOAnd <BDD>(phi, axk);
                    }
                    phi = new MSOAnd <BDD>(new MSOeqN <BDD>(new Variable("x" + 0, true), 0), phi);
                    for (int k = size - 1; k >= 0; k--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                    }

                    //Old
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(solver);
                    }

                    var told = sw.ElapsedMilliseconds;

                    //BDD
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(new CartesianAlgebraBDD <BDD>(solver));
                    }
                    sw.Stop();

                    var t1 = sw.ElapsedMilliseconds;

                    //Trie
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(new BDDAlgebra <BDD>(solver), false);
                    }

                    var t2 = sw.ElapsedMilliseconds;

                    //Tminterm
                    solver = new CharSetSolver(BitWidth.BV64);
                    BDD[] predicates = new BDD[size];
                    solver.GenerateMinterms();
                    for (int k = 0; k < size; k++)
                    {
                        predicates[k] = solver.MkBitTrue(k);
                    }

                    var t3 = 60000L * numTests;
                    if (size <= maxmint)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            var mint = solver.GenerateMinterms(predicates).ToList();
                        }
                        sw.Stop();
                        t3 = sw.ElapsedMilliseconds;
                    }

                    file.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                    Console.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                }
            }

            //ex x1 x2... a(x1) /\ a(x2)...
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"..\mso-minterm-p2.csv"))
            {
                file.WriteLine("k, old, cartesian, trie, minterm");
                for (int size = 2; size < 10; size++)
                {
                    // Tsolve force
                    var solver = new CharSetSolver();

                    MSOFormula <BDD> phi = new MSOTrue <BDD>();

                    for (int k = 0; k < size; k++)
                    {
                        var axk = new MSOPredicate <BDD>(solver.MkBitTrue(k), new Variable("x" + k, true));
                        phi = new MSOAnd <BDD>(phi, axk);
                    }
                    for (int k = size - 1; k >= 0; k--)
                    {
                        phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                    }

                    //Old
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        var aut = phi.GetAutomaton(solver);
                    }

                    var told = sw.ElapsedMilliseconds;

                    //Cartesian
                    var t1 = 60000L * numTests;
                    if (size <= 7)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            phi.GetAutomaton(new CartesianAlgebraBDD <BDD>(solver));
                        }
                        sw.Stop();

                        t1 = sw.ElapsedMilliseconds;
                    }


                    //Trie
                    var t2 = 60000L * numTests;
                    sw.Restart();
                    for (int t = 0; t < numTests; t++)
                    {
                        phi.GetAutomaton(new BDDAlgebra <BDD>(solver), false);
                    }
                    sw.Stop();

                    t2 = sw.ElapsedMilliseconds;

                    //Tminterm
                    solver = new CharSetSolver(BitWidth.BV64);
                    BDD[] predicates = new BDD[size];
                    solver.GenerateMinterms();
                    for (int k = 0; k < size; k++)
                    {
                        predicates[k] = solver.MkBitTrue(k);
                    }

                    var t3 = 60000L * numTests;
                    if (size <= maxmint)
                    {
                        sw.Restart();
                        for (int t = 0; t < numTests; t++)
                        {
                            var mint = solver.GenerateMinterms(predicates).ToList();
                        }
                        sw.Stop();
                        t3 = sw.ElapsedMilliseconds;
                    }

                    file.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                    Console.WriteLine(size + ", " + (double)told / numTests + ", " + (double)t1 / numTests + ", " + (double)t2 / numTests + ", " + (double)t3 / numTests);
                }
            }
        }
예제 #9
0
        public static void RunPOPLTests()
        {
            //// all x1...xn. xi<xi+1
            List <Pair <MSOFormula <BDD>, CharSetSolver> > phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();

            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver();
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, leq);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }

                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"..\popl14-1.csv"), phis);


            // all x1...xn. xi<xi+1 and a(xi)
            phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();
            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver(BitWidth.BV64);
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, leq);
                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate <BDD>(
                        solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, axk);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }
                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }
            RunTest(new StreamWriter(@"..\popl14-2.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi)) and ex y. c(y)
            phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();
            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver(BitWidth.BV64);
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, leq);
                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate <BDD>(solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd <BDD>(phi, axk);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }

                var exycy = new MSOExists <BDD>(new Variable("y", true), new MSOPredicate <BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd <BDD>(phi, exycy);

                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"..\popl14-3.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi) \/ c(xi))

            phis = new List <Pair <MSOFormula <BDD>, CharSetSolver> >();
            for (int to = 2; to < kpopl; to++)
            {
                var solver           = new CharSetSolver(BitWidth.BV64);
                MSOFormula <BDD> phi = new MSOTrue <BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq   = new MSOLt <BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    var axk   = new MSOPredicate <BDD>(solver.MkCharConstraint('a', false), new Variable("x" + (k - 1), true));
                    var cxk   = new MSOPredicate <BDD>(solver.MkCharConstraint('c', false), new Variable("x" + (k - 1), true));
                    var inter = new MSOOr <BDD>(new MSOAnd <BDD>(leq, axk), cxk);
                    phi = new MSOAnd <BDD>(phi, inter);
                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists <BDD>(new Variable("x" + k, true), phi);
                }

                MSOFormula <BDD> exycy = new MSOExists <BDD>(new Variable("y", true), new MSOPredicate <BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd <BDD>(phi, exycy);

                phis.Add(new Pair <MSOFormula <BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"..\popl14-4.csv"), phis, 10, 10, 11);
        }
예제 #10
0
        public static void RunPOPLTests()
        {
            //// all x1...xn. xi<xi+1
            List<Pair<MSOFormula<BDD>, CharSetSolver>> phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();

            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver();
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, leq);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }

                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"popl14-1.csv"), phis);

            // all x1...xn. xi<xi+1 and a(xi)
            phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();
            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver(BitWidth.BV64);
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, leq);

                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate<BDD>(
                        solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, axk);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }
                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }
            RunTest(new StreamWriter(@"popl14-2.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi)) and ex y. c(y)
            phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();
            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver(BitWidth.BV64);
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, leq);

                }
                for (int k = 0; k < to; k++)
                {
                    var axk = new MSOPredicate<BDD>(solver.MkCharConstraint('a', false), new Variable("x" + k, true));
                    phi = new MSOAnd<BDD>(phi, axk);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }

                var exycy = new MSOExists<BDD>(new Variable("y", true), new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd<BDD>(phi, exycy);

                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"popl14-3.csv"), phis);

            // all x1...xn. (xi<xi+1 and a(xi) \/ c(xi))

            phis = new List<Pair<MSOFormula<BDD>, CharSetSolver>>();
            for (int to = 2; to < kpopl; to++)
            {
                var solver = new CharSetSolver(BitWidth.BV64);
                MSOFormula<BDD> phi = new MSOTrue<BDD>();

                for (int k = 1; k < to; k++)
                {
                    var leq = new MSOLt<BDD>(new Variable("x" + (k - 1), true), new Variable("x" + k, true));
                    var axk = new MSOPredicate<BDD>(solver.MkCharConstraint('a', false), new Variable("x" + (k - 1), true));
                    var cxk = new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("x" + (k - 1), true));
                    var inter = new MSOOr<BDD>(new MSOAnd<BDD>(leq, axk), cxk);
                    phi = new MSOAnd<BDD>(phi, inter);

                }
                for (int k = to - 1; k >= 0; k--)
                {
                    phi = new MSOExists<BDD>(new Variable("x" + k, true), phi);
                }

                MSOFormula<BDD> exycy = new MSOExists<BDD>(new Variable("y", true), new MSOPredicate<BDD>(solver.MkCharConstraint('c', false), new Variable("y", true)));
                phi = new MSOAnd<BDD>(phi, exycy);

                phis.Add(new Pair<MSOFormula<BDD>, CharSetSolver>(phi, solver));
            }

            RunTest(new StreamWriter(@"popl14-4.csv"), phis, 11, 11, 11);
        }