how to convert z3 expression to infix expression? - z3

I want to translate boolExpression in Z3 into infix representation. For example, there is a z3 expression (>= t 3), I want to get the infix string "t>=3", is any existing Z3 api to implement it in C# ?

No, the official API does not have support for displaying expressions in infix notation. This functionality can be implemented on top of the API for traversing expressions. The Z3 Python API implements an infix printer. Actually, it implements two: one for Python-like syntax, and one for HTML math-like syntax. The source code of these printers is included in the Z3 distribution. The code is written in python, but can be easily converted into any programming language. The code is located at python\z3printer.py.

Related

Microsoft Z3 - How to use tactic combinators in the C# API

I am currently going through following documents:
https://rise4fun.com/z3/tutorial/strategies
http://z3prover.github.io/api/html/namespace_microsoft_1_1_z3.html
In one of our academic research project, we are using Z3 for problem-solving. It is written using Z3 C# API. We want to use the concept of tactics, goals, and sub-goals. We want to give tactics using tactic combinators (aka tacticals). However, in the C# API, I could not find any way to use combinators like (then ..) (or-else ...).
Is there any API function I can use to create such combinators?
The way a single tactic can be used is as follows:
Tactic t = Context.MkTactic("simplify");
Context.MkSolver(Tactic)
The tactic combinator constructors are on the Context, e.g. AndThen.

Z3 Java API: FuncDecl and Expr

I have an existing smt2 file, and used z3 java api parsed this and solved the problem. But I have a question about how to cast a FuncDecl to an Expr because I want to build some simple formula by using z3 java apis. Since my original formulas is purely written in smt2 text file and everything gets parsed into a single BoolExpr. Now, I successfully extracted the consts, and need to manipulate them with new formula. How can i do it? Basically, what I am looking for is how to build an Expr from FuncDecl Or is there a way I can cast it to Expr? Is there any official java api document available? I know there is an example of using z3 java api, but it's pretty painful to look for a specific api description in such a large example.
FuncDecl's are function declarations, they can not be cast directly to expressions, but the function they represent can be applied (to some arguments) to yield an expression. This is what FuncDecl.apply(...) does. Constants are of course a special case, where the function doesn't take any arguments.

Parse Math Expression in PHP

I'm currently trying to parse math expression into expression tree.
But I'm stuck on the stage where I need to implement functions and negates. I don't understand logic to do it using Shunting-Yard algorithm.
What I currently want to do is to support
Negates, like -(x+5)
Function calls, like min(x,y)
Power just after function name, like cos^2(x)
Implicit multiplication, like 2x is same as 2*x
Scientific notation
Constants e and pi
Can somebody tell me hints how to implement this?
An working, PSR-0 compatible implementation of the shunting yard algorithm can be found here: https://github.com/andig/php-shunting-yard/tree/dev.
It supports constants, custom functions etc.

Code quotations and Expression trees

I wonder if there is any difference in how the two features are implemented under the hood? I.e. Aren't just code quotations built on top of the old good expression trees?
Thanks.
The two types are quite similar, but they are represented differently.
Quotations are designed in a more functional way. For example foo a b would be represented as a series of applications App(App(foo, a), b)
Quotations can represent some constructs that are available only in F# and using expression trees would hide them. For example there is Expr.LetRecursive for let rec declarations
Quotations were first introduced in .NET 3.0. Back then expression trees could only represent C# expressions, so it wasn't possible to easily capture all F# constructs (quotations can capture any F# expression including imperative ones).
Quotations are also designed to be easily processible using recursion. The ExprShape module contains patterns that allow you to handle all possible quotations with just 4 cases (which is a lot easier than implementing visitor pattern with tens of methods in C#).
When you have an F# quotation, you can translate it to C# expression tree using FSharp.Quotations.Evaluator. This is quite useful if you're using some .NET API that expects expression trees from F#. As far as I know, there is no translation the other way round.

Python3 parser generator

I'm looking for a parser generator for a reasonably complex language (similar in complexity to Python itself) which works with Python3. If it can generate an AST automatically, this would be a bonus, but I'm fine if it just calls rules while parsing. I have no special requirements, nor does it have to be very efficient/fast.
LEPL isn't exactly a parser generator - it's better! The parsers are defined in Python code and constructed at runtime (hence some inefficiency, but much easier to use). It uses operator overloading to construct a quite readable DSL. Things like c = a & b | b & c for the BNF c := a b | b c..
You can pass the results of a (sub-)parser to an abritary callable, and this is very usable for AST generation (also useful for converting e.g. number literals to Python-level number objects). It's a recursive descent parser, so you better avoid left recursion in the grammar (there are memoization objets that can make left recursion work, but "Lepl's support for them has historically been unreliable (buggy)").
ANTLR can generate a lexer and/or parser in Python. You can also use it to create AST's and iterator-like structures to walk the AST (called tree grammars).
See ANTLR get and split lexer content for an ANTLR demo that produces an AST with the Python target.

Resources