How to turn these expressions into Reverse Polish Notation? - odoo-8

I am working with Odoo 8 and I want to set a couple of domains. I thought I understood reverse Polish notation but domains are not working so I guess I am wrong.
The domains I want to achieve using reverse Polish notation are:
A and B and C and (D or (E and F)): I tried to implement it with the expression A B C or D E F, but it did not work.
A and B and (C or D or (E and F)): I tried to implement it with the expression A B or or C D E F, but it did work neither.
Note: I am not writing ANDs in domains (if you do not specify anything
in Odoo, it is supposed to use &).
My domains are wrong because I always get no record.
Can anyone help me, please?

I have the answer. Despite if you do not specify anything, Odoo takes an AND by default, you must write ANDs when there are expressions which must be executed before other and Odoo does not know which are because you have a complex and long expression.
In my cases, E and F must be executed before any other expression, so we cannot leave them without AND, so:
A and B and C and (D or (E and F)): The solution is A B C or D and E F.
A and B and (C or D or (E and F)): The solution is A B or or C D and E F.
In an XML domain in Odoo, these domains will be:
[A, B, C, '|', D, '&', E, F].
[A, B, '|', '|', C, D, '&', E, F].
Where each letter will be an expression like ('field', 'operator', 'value').
Note: ampersands must be escaped.
EDIT
I have answered a question here:
I don´t understand Normal Polish Notation (NPN or PN). How to build a complex domain in Odoo?
In which I explain with details a great method to resolve complex domains. I hope it helps to someone.

Related

Prolog pythagorean triplet

I'm trying to solve project Euler problem 9 using prolog. I'm a 100% n00b with prolog and I'm just trying to understand the basics right now.
I'm trying to use findall to get all triplets that would add up to 1000, but I can't really figure out the syntax.
What I'm hoping for is something like:
pythag_trip(A, B, C, D) :- D is (A * A) + (B * B) + (C * C).
one_thou_pythag(A, B, C) :- pythag_trip(A, B, C, 1000).
product_trip(A, B, C, D) :- D is A * B * C.
findall([A, B, C], one_thou_pythag(A, B, C) , Bag)).
writeln(Bag).
I know that doesn't work because it's saying Bag is not instantiated. But there are still some basics that I don't understand about the language, too.
1: can I even do this? With multiple moving pieces at once? Can I find all triplets satisfying a condition? Do I need to go down a completely different like like using clpfd?
2: What is supposed to be going in that last argument where I put Bag?
3: Is it possible to create data types? I was thinking it might be good to create a triplet set type and an operation to get the pythagorean triplet sum of them if I have to find some way to generate all the possibilities on my own
Basically those questions and then, I could use some pointing in the right direction if anyone has tips
Sorry but I don't answer your questions. It seems to me that you're trying not a prolog-like approach.
You should try to solve it logically.
So do this problem from the top to bottom.
We want to have 3 numbers that sum to 1000.
between(1,1000,A), between(A,1000,B), between(B,1000,C), C is 1000-A-B.
In that case, we will have them sorted and we won't take permutations.
So let's go a step further. We want them to be pythagorem triplet.
between(1,1000,A), between(A,1000,B), between(B,1000,C), C is 1000-A-B, is_triplet(A,B,C).
But we don't have is_triplet/3 predicate, so let's create it
is_triplet(A,B,C) :- D is C*C - A*A -B*B, D=0.
And that's actually it.
So let's sum it up
is_triple(A, B, C) :- D is C*C - A*A - B*B, D = 0.
triplet(A,B,C) :- between(1,1000,A), between(A,1000,B), C is 1000-A-B, between(B,1000,C), C is 1000-A-B, is_triple(A,B,C).
When you call triplet(A,B,C) you should get an answer.
Notice one thing, that at the end I've swapped C is 1000-A-B with between(B,1000,C). It makes the program much faster, try to think why.

NOR-conjunction within OR-conjunctions

Let's save I have the logical expression
a OR b NOR c OR d
Is this interpreted as (a OR b) NOR (c OR d) or as a OR (b NOR c) OR d ?
It is a specific programming problem I face, thus the question on stackoverflow.
As far as I know, it would be interpreted as:
A or b or (Not c) or d

Wolfram Alpha and Logic - NOR transformation

i have some difficulties interpret some WolframAlpha logic.
I have this logical expression: !(a || b || c)
WA says, that it's minimal NOR-Form ist a NOR b NOR c.
But if you type it in, the truth tables are different.
However if you search for this (!a nor b) nor c
you'll get the correct answer.
Is this an WolframAlpha bug or do I just misinterpret the result?
Here are the links:
!(a || b || c) http://www.wolframalpha.com/input/?i=!%28a+||+b+||+c%29
a NOR b NOR c http://www.wolframalpha.com/input/?i=a+nor+b+nor+c
Thanks!
If we consider NOR as a binary operator, then we need to treat an expression like a NOR b NOR c as either (a NOR b) NOR c or a NOR (b NOR c). Either way, it's not the same as !(a || b || c). This is in fact how WA treats the formula when you ask it about a NOR b NOR c.
However, suppose we consider NOR as a “variable” arity operator, that takes any number of arguments. Thus we treat a NOR b NOR c as NOR(a, b, c), where the NOR function returns true if and only if all of its arguments are false. Then a NOR b NOR c is the same as !(a || b || c). This seems to be what WA thinks when you ask it about !(a || b || c).
It does seem like a bug in Wolfram Alpha that it uses different definitions of a NOR b NOR c in these two cases.

complex logical operation

Given this logical operation :
(A AND B) OR (C AND D)
Is there a way to write a similar expression without using any parentheses and giving the same result ? Usage of logical operators AND, OR, NOT are allowed.
Yes:
A and B or C and D
In most programming languages, and is taken to have higher precedence than or (this stems from the equivalence of and and or to * and +, respectively).
Of course, if your original expression had been:
(A or B) and (C or D)
you couldn't simply remove the parentheses. In this instance, you'd have to "multiply out" the factors:
A and C or B and C or A and D or B and D
How about A AND B OR C AND D? It's the same because AND takes precedence over OR.
Just don't put any parentheses, it is the same...
It can be written in two ways
A & B | C & D
Type as it is mentioned in question just remove the parenthesis it will show the same result.
We can use & for AND to multiply and | for OR to divide. Also simply you can write them without any parenthesis

Parsing expressions with an undefined number of arguments

I'm trying to parse a string in a self-made language into a sort of tree, e.g.:
# a * b1 b2 -> c * d1 d2 -> e # f1 f2 * g
should result in:
# a
* b1 b2
-> c
* d1 d2
-> e
# f1 f2
* g
#, * and -> are symbols. a, b1, etc. are texts.
Since the moment I know only rpn method to evaluate expressions, and my current solution is as follows. If I allow only a single text token after each symbol I can easily convert expression first into RPN notation (b = b1 b2; d = d1 d2; f = f1 f2) and parse it from here:
a b c -> * d e -> * # f g * #
However, merging text tokens and whatever else comes seems to be problematic. My idea was to create marker tokens (M), so RPN looks like:
a M b2 b1 M c -> * M d2 d1 M e -> * # f2 f1 M g * #
which is also parseable and seems to solve the problem.
That said:
Does anyone have experience with something like that and can say it is or it is not a viable solution for the future?
Are there better methods for parsing expressions with undefined arity of operators?
Can you point me at some good resources?
Note. Yes, I know this example very much resembles Lisp prefix notation and maybe the way to go would be to add some brackets, but I don't have any experience here. However, the source text must not contain any artificial brackets and also I'm not sure what to do about potential infix mixins like # a * b -> [if value1 = value2] c -> d.
Thanks for any help.
EDIT: It seems that what I'm looking for are sources on postfix notation with a variable number of arguments.
I couldn't fully understand your question, but it seems what you want is a grammar definition and a parser generator. I suggest you take a look at ANTLR, it should be pretty straightforward with it to define a grammar for either your original syntax or the RPN.
Edit: (After exercising self-criticism, and making some effort to understand the question details.) Actually, the language grammar is unclear from your example. However, it seems to me, that the advantages of the prefix/postfix notations (i.e. that you need neither parentheses nor a precedence-aware parser) stem from the fact that you know the number of arguments every time you encounter an operator, therefore you know exactly how many elements to read (for prefix notation) or to pop from the stack (for postfix notation). OTOH, I beleive that having operators which can have variable number of arguments makes prefix/postfix notations not simply difficult to parse but outright ambiguous. Take the following expression for example:
# a * b c d
Which of the following three is the canonical form?
(a, *(b, c, d))
(a, *(b, c), d)
(a, *(b), c, d)
Without knowing more about the operators, it is impossible to tell. Of course you could define some sort of greedyness of the operators, e.g. * is greedier than #, so it gobbles up all the arguments. But this would beat the purpose of a prefix notation, because you simply wouldn't be able to write down the second variant from the above three; not without additinonal syntactic elements.
Now that I think of it, it is probably not by sheer chance that none of the programming languages I know support operators with a variable number of arguments, only functions/procedures.

Resources