Page 1 of 1

Optimal boolean expressions evaluation

Posted: Tue Oct 13, 2009 10:02 am
by makia
What is known tactic for creating optimal boolean query expressions, for example if i have this query:
(OR (AND (= A B) (= C D))
(AND (= A B) (= Y P)))

faster evaluation would be:
(AND (= A B)
(OR (= C D) (= Y P)))
, this is not only lisp related but it's natural to solve this in lisp form
Thanks

Re: Optimal boolean expressions evaluation

Posted: Tue Oct 13, 2009 10:08 am
by makia
btw. i posted this here because it's part of something i'm doing in Common Lisp and i'm interested in CL solutions (also it's natural to do this in sexp) .... wiki link can help too :)

Re: Optimal boolean expressions evaluation

Posted: Tue Oct 13, 2009 10:52 am
by ramarren
I don't know that much about prepositional logic, but this seems to be an NP-hard problem. Some relevant Wikipedia links: Circuit minimization (note that digital circuits are equivalent to boolean expressions), Karnaugh maps,Quine–McCluskey algorithm, Espresso heuristic logic minimizer.

I haven't seen any code for doing that in CL, but I haven't looked. It is quite likely that there might be some in some older archives like CMU Common Lisp Repository.

Re: Optimal boolean expressions evaluation

Posted: Tue Oct 13, 2009 2:03 pm
by nuntius
A couple things to watch for:
- Short-circuiting operators (AND fails after first nil, OR passes after first t) may change the optimal result, especially if you can accurately predict truth frequencies.
- If short-circuiting operators protect mutating tests, refactoring may be impossible.

Re: Optimal boolean expressions evaluation

Posted: Tue Oct 13, 2009 9:03 pm
by Kohath
This makes me want to be able to tell if a particular form is a mutating/side-effect one or not. I suppose it would be kinda cool to have a compiler macro for this kind of thing.

Re: Optimal boolean expressions evaluation

Posted: Tue Oct 13, 2009 11:31 pm
by makia
of course there is no mutating tests here, we can simplify this:

Code: Select all

(OR (AND (= A B) (= C D))
    (AND (= A B) (= Y P)))
to

Code: Select all

(OR (AND a b)
    (AND a c))
and this should lead to:

Code: Select all

(AND a (OR b c))

Re: Optimal boolean expressions evaluation

Posted: Wed Oct 14, 2009 6:48 am
by gugamilare
This kind of problem reminds me the type inference made by SBCL.

Internally, SBCL transforms a type specifier (like 'array or 'string) into an object. The function that retrieves such object is sb-kernel:specifier-type. The function that gives the specifier back is sb-kernel:type-specifier. Types like '(and string array) and '(or integer character) are constructed using sb-kernel:type-intersection and sb-kernel:type-union.

I believe this is much similar to what you want to do, since SBCL can simplify some nested type unions / intersections similarly to the simplification of boolean expressions. You may refer to these functions (using M-. using Slime) and see if it helps.

Code: Select all

cl-user> (sb-kernel:type-specifier
          (sb-kernel:type-intersection (sb-kernel:type-union (sb-kernel:specifier-type 'array)
                                                             (sb-kernel:specifier-type 'fixnum))
                                       (sb-kernel:type-union (sb-kernel:specifier-type 'array)
                                                             (sb-kernel:specifier-type 'integer))))
(or array fixnum)
cl-user> (sb-kernel:type-specifier
          (sb-kernel:type-union (sb-kernel:type-intersection (sb-kernel:specifier-type '(integer -2 1))
                                                             (sb-kernel:specifier-type '(integer -1 2)))
                                (sb-kernel:type-intersection (sb-kernel:specifier-type '(integer -2 1))
                                                             (sb-kernel:specifier-type '(integer 0 3)))))
(integer -1 1)
cl-user> (sb-kernel:type-specifier
          (sb-kernel:type-intersection (sb-kernel:type-union (sb-kernel:specifier-type 'character)
                                                             (sb-kernel:specifier-type 'fixnum))
                                       (sb-kernel:type-union (sb-kernel:specifier-type 'character)
                                                             (sb-kernel:specifier-type 'array))))
character
cl-user> (sb-kernel:type-specifier
          (sb-kernel:type-intersection (sb-kernel:type-union (sb-kernel:specifier-type 'character)
                                                             (sb-kernel:specifier-type '(satisfies foo)))
                                       (sb-kernel:type-union (sb-kernel:specifier-type 'character)
                                                             (sb-kernel:specifier-type '(satisfies bar)))))
(or character (and (satisfies bar) (satisfies foo)))
Unfortunately, it seems not to be able to simplify the following expression, which is what you would want it to do:

Code: Select all

cl-user> (sb-kernel:type-specifier
          (sb-kernel:type-union (sb-kernel:type-intersection (sb-kernel:specifier-type 'fixnum)
                                                             (sb-kernel:specifier-type '(satisfies foo)))
                                (sb-kernel:type-intersection (sb-kernel:specifier-type 'fixnum)
                                                             (sb-kernel:specifier-type '(satisfies bar)))))
(or (and fixnum (satisfies foo)) (and fixnum (satisfies bar)))
;;; it would be better if it was (and fixnum (or (satisfies foo) (satisfies bar)))
;;; like in your example