Esempio n. 1
0
        public Expr DisjointnessExpr(string domainName, HashSet <Variable> scope)
        {
            LinearDomain  domain    = linearDomains[domainName];
            BoundVariable partition = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("partition_{0}", domainName), new MapType(Token.NoToken, new List <TypeVariable>(), new List <Type> {
                domain.elementType
            }, Microsoft.Boogie.Type.Int)));
            Expr disjointExpr = Expr.True;
            int  count        = 0;

            foreach (Variable v in scope)
            {
                IdentifierExpr ie = new IdentifierExpr(Token.NoToken, v);
                Expr           e  = new NAryExpr(Token.NoToken, new FunctionCall(domain.mapConstInt), new List <Expr> {
                    new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(count++))
                });
                e = new NAryExpr(Token.NoToken, new FunctionCall(domain.mapEqInt), new List <Expr> {
                    new IdentifierExpr(Token.NoToken, partition), e
                });
                e = new NAryExpr(Token.NoToken, new FunctionCall(domain.mapImpBool), new List <Expr> {
                    v.TypedIdent.Type is MapType ? ie : Singleton(ie, domainName), e
                });
                e = Expr.Binary(BinaryOperator.Opcode.Eq, e, new NAryExpr(Token.NoToken, new FunctionCall(domain.mapConstBool), new List <Expr> {
                    Expr.True
                }));
                disjointExpr = Expr.Binary(BinaryOperator.Opcode.And, e, disjointExpr);
            }
            var expr = new ExistsExpr(Token.NoToken, new List <Variable> {
                partition
            }, disjointExpr);

            expr.Resolve(new ResolutionContext(null));
            expr.Typecheck(new TypecheckingContext(null));
            return(expr);
        }
Esempio n. 2
0
        // Parse an sequence of add/subtract operators
        Expr ParseMultiplyDivide()
        {
            // Parse the left hand side
            var lhs = ParsePowers();

            while (true)
            {
                // Work out the operator
                BinaryOp op;
                switch (_tokenizer.Current.Token)
                {
                case Token.Multiply:
                    op = BinaryOp.Multiply;
                    break;

                case Token.Divide:
                    op = BinaryOp.Divide;
                    break;

                default:
                    // Binary operator not found.
                    return(lhs);
                }

                // Skip the operator
                _tokenizer.MoveNext();

                // Parse the right hand side of the expression
                var rhs = ParsePowers();

                // Create a binary node and use it as the left-hand side from now on
                switch (op)
                {
                case BinaryOp.Multiply:
                case BinaryOp.Divide:
                    lhs = Expr.Binary(op, lhs, rhs);
                    break;

                default:
                    throw new NotSupportedException();
                }
            }
        }
Esempio n. 3
0
        private async Task <Expr> ParseEquation(Expr e)
        {
            var pExpr = e;

            do
            {
                var pExpr2 = await ParseExpr();

                var pExprTmp = Expr.Binary(ETypeExpr.Equal, pExpr, pExpr2);

                pExpr = pExprTmp;

                if (mTokenizer.EOF || mTokenizer.EOL)
                {
                    break;
                }
            } while (mTokenizer.Token == ETokenType.Equal);

            return(pExpr);
        }
Esempio n. 4
0
        static void TestBinaryFunctions()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Test Binary Functions:");
            Console.ForegroundColor = ConsoleColor.Gray;
            VariableExpr a = "a", b = "b";
            double       x = 5, y = 2;
            string       fstr = $"f({x},{y})", fpstr = $"f'({x},{y})";

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write($"{"f",-12} ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write($"{"f'",-28} ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write($"{fstr,-22} ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write($"{fpstr,-28}");
            Console.WriteLine();
            foreach (var op in Enum.GetValues(typeof(BinaryOp)) as BinaryOp[])
            {
                if (op == BinaryOp.Undefined)
                {
                    continue;
                }
                var f_expr = Expr.Binary(op, a, b);
                var dfda   = f_expr.Partial(a);
                var f      = f_expr[a, b];
                var fp     = dfda[a, b];
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write($"{f_expr,-12} ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write($"{dfda,-28:g4} ");
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write($"{f(x,y),-22:g4} ");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write($"{fp(x,y),-28:g4}");
                Console.WriteLine();
            }
            Console.WriteLine();
        }
Esempio n. 5
0
 private void MergeCells(Cell l, Cell r)
 {
     l.Expr   = Expr.Binary(l.TypeOp, l.Expr, r.Expr);
     l.TypeOp = r.TypeOp;
 }
Esempio n. 6
0
        public LinearDomain(Program program, string domainName, Type elementType)
        {
            this.elementType = elementType;
            this.axioms      = new List <Axiom>();

            MapType mapTypeBool = new MapType(Token.NoToken, new List <TypeVariable>(), new List <Type> {
                this.elementType
            }, Type.Bool);
            MapType mapTypeInt = new MapType(Token.NoToken, new List <TypeVariable>(), new List <Type> {
                this.elementType
            }, Type.Int);

            this.mapOrBool = new Function(Token.NoToken, "linear_" + domainName + "_MapOr",
                                          new List <Variable> {
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "a", mapTypeBool), true),
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "b", mapTypeBool), true)
            },
                                          new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "c", mapTypeBool), false));
            if (CommandLineOptions.Clo.UseArrayTheory)
            {
                this.mapOrBool.AddAttribute("builtin", "MapOr");
            }
            else
            {
                BoundVariable  a           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "a", mapTypeBool));
                IdentifierExpr aie         = new IdentifierExpr(Token.NoToken, a);
                BoundVariable  b           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "b", mapTypeBool));
                IdentifierExpr bie         = new IdentifierExpr(Token.NoToken, b);
                BoundVariable  x           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", elementType));
                IdentifierExpr xie         = new IdentifierExpr(Token.NoToken, x);
                var            mapApplTerm = new NAryExpr(Token.NoToken, new FunctionCall(mapOrBool), new List <Expr> {
                    aie, bie
                });
                var lhsTerm = new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    mapApplTerm, xie
                });
                var rhsTerm = Expr.Binary(BinaryOperator.Opcode.Or,
                                          new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    aie, xie
                }),
                                          new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    bie, xie
                }));
                var axiomExpr = new ForallExpr(Token.NoToken, new List <TypeVariable>(), new List <Variable> {
                    a, b
                }, null,
                                               new Trigger(Token.NoToken, true, new List <Expr> {
                    mapApplTerm
                }),
                                               new ForallExpr(Token.NoToken, new List <Variable> {
                    x
                }, Expr.Binary(BinaryOperator.Opcode.Eq, lhsTerm, rhsTerm)));
                axiomExpr.Typecheck(new TypecheckingContext(null));
                axioms.Add(new Axiom(Token.NoToken, axiomExpr));
            }

            this.mapImpBool = new Function(Token.NoToken, "linear_" + domainName + "_MapImp",
                                           new List <Variable> {
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "a", mapTypeBool), true),
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "b", mapTypeBool), true)
            },
                                           new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "c", mapTypeBool), false));
            if (CommandLineOptions.Clo.UseArrayTheory)
            {
                this.mapImpBool.AddAttribute("builtin", "MapImp");
            }
            else
            {
                BoundVariable  a           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "a", mapTypeBool));
                IdentifierExpr aie         = new IdentifierExpr(Token.NoToken, a);
                BoundVariable  b           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "b", mapTypeBool));
                IdentifierExpr bie         = new IdentifierExpr(Token.NoToken, b);
                BoundVariable  x           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", elementType));
                IdentifierExpr xie         = new IdentifierExpr(Token.NoToken, x);
                var            mapApplTerm = new NAryExpr(Token.NoToken, new FunctionCall(mapImpBool), new List <Expr> {
                    aie, bie
                });
                var lhsTerm = new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    mapApplTerm, xie
                });
                var rhsTerm = Expr.Binary(BinaryOperator.Opcode.Imp,
                                          new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    aie, xie
                }),
                                          new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    bie, xie
                }));
                var axiomExpr = new ForallExpr(Token.NoToken, new List <TypeVariable>(), new List <Variable> {
                    a, b
                }, null,
                                               new Trigger(Token.NoToken, true, new List <Expr> {
                    mapApplTerm
                }),
                                               new ForallExpr(Token.NoToken, new List <Variable> {
                    x
                }, Expr.Binary(BinaryOperator.Opcode.Eq, lhsTerm, rhsTerm)));
                axiomExpr.Typecheck(new TypecheckingContext(null));
                axioms.Add(new Axiom(Token.NoToken, axiomExpr));
            }

            this.mapConstBool = new Function(Token.NoToken, "linear_" + domainName + "_MapConstBool",
                                             new List <Variable> {
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "b", Type.Bool), true)
            },
                                             new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "c", mapTypeBool), false));
            if (CommandLineOptions.Clo.UseArrayTheory)
            {
                this.mapConstBool.AddAttribute("builtin", "MapConst");
            }
            else
            {
                BoundVariable  x        = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", elementType));
                IdentifierExpr xie      = new IdentifierExpr(Token.NoToken, x);
                var            trueTerm = new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1),
                                                       new List <Expr> {
                    new NAryExpr(Token.NoToken, new FunctionCall(mapConstBool), new List <Expr> {
                        Expr.True
                    }), xie
                });
                var trueAxiomExpr = new ForallExpr(Token.NoToken, new List <Variable> {
                    x
                }, trueTerm);
                trueAxiomExpr.Typecheck(new TypecheckingContext(null));
                axioms.Add(new Axiom(Token.NoToken, trueAxiomExpr));
                var falseTerm = new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1),
                                             new List <Expr> {
                    new NAryExpr(Token.NoToken, new FunctionCall(mapConstBool), new List <Expr> {
                        Expr.False
                    }), xie
                });
                var falseAxiomExpr = new ForallExpr(Token.NoToken, new List <Variable> {
                    x
                }, Expr.Unary(Token.NoToken, UnaryOperator.Opcode.Not, falseTerm));
                falseAxiomExpr.Typecheck(new TypecheckingContext(null));
                axioms.Add(new Axiom(Token.NoToken, falseAxiomExpr));
            }

            this.mapEqInt = new Function(Token.NoToken, "linear_" + domainName + "_MapEq",
                                         new List <Variable> {
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "a", mapTypeInt), true),
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "b", mapTypeInt), true)
            },
                                         new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "c", mapTypeBool), false));
            if (CommandLineOptions.Clo.UseArrayTheory)
            {
                this.mapEqInt.AddAttribute("builtin", "MapEq");
            }
            else
            {
                BoundVariable  a           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "a", mapTypeInt));
                IdentifierExpr aie         = new IdentifierExpr(Token.NoToken, a);
                BoundVariable  b           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "b", mapTypeInt));
                IdentifierExpr bie         = new IdentifierExpr(Token.NoToken, b);
                BoundVariable  x           = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", elementType));
                IdentifierExpr xie         = new IdentifierExpr(Token.NoToken, x);
                var            mapApplTerm = new NAryExpr(Token.NoToken, new FunctionCall(mapEqInt), new List <Expr> {
                    aie, bie
                });
                var lhsTerm = new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    mapApplTerm, xie
                });
                var rhsTerm = Expr.Binary(BinaryOperator.Opcode.Eq,
                                          new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    aie, xie
                }),
                                          new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    bie, xie
                }));
                var axiomExpr = new ForallExpr(Token.NoToken, new List <TypeVariable>(), new List <Variable> {
                    a, b
                }, null,
                                               new Trigger(Token.NoToken, true, new List <Expr> {
                    mapApplTerm
                }),
                                               new ForallExpr(Token.NoToken, new List <Variable> {
                    x
                }, Expr.Binary(BinaryOperator.Opcode.Eq, lhsTerm, rhsTerm)));
                axiomExpr.Typecheck(new TypecheckingContext(null));
                axioms.Add(new Axiom(Token.NoToken, axiomExpr));
            }

            this.mapConstInt = new Function(Token.NoToken, "linear_" + domainName + "_MapConstInt",
                                            new List <Variable> {
                new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "b", Type.Int), true)
            },
                                            new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "c", mapTypeInt), false));
            if (CommandLineOptions.Clo.UseArrayTheory)
            {
                this.mapConstInt.AddAttribute("builtin", "MapConst");
            }
            else
            {
                BoundVariable  a       = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "a", Type.Int));
                IdentifierExpr aie     = new IdentifierExpr(Token.NoToken, a);
                BoundVariable  x       = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", elementType));
                IdentifierExpr xie     = new IdentifierExpr(Token.NoToken, x);
                var            lhsTerm = new NAryExpr(Token.NoToken, new MapSelect(Token.NoToken, 1), new List <Expr> {
                    new NAryExpr(Token.NoToken, new FunctionCall(mapConstInt), new List <Expr> {
                        aie
                    }), xie
                });
                var axiomExpr = new ForallExpr(Token.NoToken, new List <Variable> {
                    a, x
                }, Expr.Binary(BinaryOperator.Opcode.Eq, lhsTerm, aie));
                axiomExpr.Typecheck(new TypecheckingContext(null));
                axioms.Add(new Axiom(Token.NoToken, axiomExpr));
            }

            foreach (var axiom in axioms)
            {
                axiom.Expr.Resolve(new ResolutionContext(null));
                axiom.Expr.Typecheck(new TypecheckingContext(null));
            }
        }
 public Expr MakeModPow2(Expr lhs, Expr rhs)
 {
     return(Expr.Binary(BinaryOperator.Opcode.Mod, lhs, rhs));
 }
Esempio n. 8
0
        // Parse a leaf node
        // (For the moment this is just a number)
        Expr ParseLeaf()
        {
            // Is it a number?
            if (_tokenizer.Current.Token == Token.Number)
            {
                var node = Expr.Number(_tokenizer.Current.Number);
                _tokenizer.MoveNext();
                return(node);
            }
            // Bracket?
            if (_tokenizer.Current.Token == Token.OpenBracket)
            {
                // array
                // Skip '['
                _tokenizer.MoveNext();

                var elements = new List <Expr>();
                while (true)
                {
                    // Parse argument and add to list
                    elements.Add(ParseAddSubtract());

                    // Is there another argument?
                    if (_tokenizer.Current.Token == Token.Comma)
                    {
                        _tokenizer.MoveNext();
                        continue;
                    }

                    // Get out
                    break;
                }

                // Check and skip ')'
                if (_tokenizer.Current.Token != Token.CloseBracket)
                {
                    throw new SyntaxException("Missing close bracket");
                }
                _tokenizer.MoveNext();

                return(Expr.FromArray(elements));
            }

            // Parenthesis?
            if (_tokenizer.Current.Token == Token.OpenParens)
            {
                // Skip '('
                _tokenizer.MoveNext();

                // Parse a top-level expression
                var node = ParseAddSubtract();

                // Check and skip ')'
                if (_tokenizer.Current.Token != Token.CloseParens)
                {
                    throw new SyntaxException("Missing close parenthesis");
                }
                _tokenizer.MoveNext();

                // Return
                return(node);
            }

            // Variable
            if (_tokenizer.Current.Token == Token.Identifier)
            {
                // Capture the name and skip it
                var name = _tokenizer.Current.Identifier;
                _tokenizer.MoveNext();

                // Parens indicate a function call, otherwise just a variable
                if (_tokenizer.Current.Token != Token.OpenParens)
                {
                    return(Expr.VariableOrConst(name));
                }
                else
                {
                    // Function call

                    // Skip parens
                    _tokenizer.MoveNext();

                    // Parse arguments
                    var arguments = new List <Expr>();
                    while (true)
                    {
                        // Parse argument and add to list
                        arguments.Add(ParseAddSubtract());

                        // Is there another argument?
                        if (_tokenizer.Current.Token == Token.Comma)
                        {
                            _tokenizer.MoveNext();
                            continue;
                        }

                        // Get out
                        break;
                    }

                    // Check and skip ')'
                    if (_tokenizer.Current.Token != Token.CloseParens)
                    {
                        throw new SyntaxException("Missing close parenthesis");
                    }
                    _tokenizer.MoveNext();

                    // Create the function call node
                    switch (arguments.Count)
                    {
                    case 1:
                        if (FindOperation(name, out UnaryOp uop))
                        {
                            return(Expr.Unary(uop, arguments[0]));
                        }
                        throw new ArgumentException($"Invalid unary function {name}");

                    case 2:
                        if (FindOperation(name, out BinaryOp bop))
                        {
                            return(Expr.Binary(bop, arguments[0], arguments[1]));
                        }
                        throw new ArgumentException($"Invalid binary function {name}");

                    default:
                        throw new SyntaxException("Invalid number of arguments");
                    }
                }
            }

            // Don't Understand
            throw new SyntaxException($"Unexpected token: {_tokenizer.Current.Token}");
        }