Friday, July 17, 2009

Boolean logic in emacs lisp

The field of logic is very ancient, probably as ancient as man himself. However, the earliest known work is that of Aristotle. Boolean logic is a branch of logic and was first defined in the mid-19th century by George Boole. It has many applications in computer science and here I will try and give a quick and simple outline of what a boolean expression is and how it is used in writing programs in emacs lisp.

True or False?

In emacs lisp, t denotes True and nil denotes False. Thus, t and nil are the simplest boolean expressions evaluating to themselves. Emacs lisp also provides us with functions that evaluate to t or nil such as <, >, =, equal and so on. Check the code below to get a feel for these expressions.
Code :
(< 4 5)
(> 5 10)
(equal user-login-name "Barbarossa")
(eq "Jack" "Jack")
(eq 'Jack 'Jack)

If you have any doubts you can always refer the documentation for the function by pressing C-h f in emacs and typing in the name of the function.

Boolean functions

Strictly speaking, boolean functions are those functions whose input can consist of only true or false values and the output is also either true or false. However, as we will see shortly this is not the case with emacs lisp. The functions not, or, and are examples of boolean functions.They are the standard boolean functions forming the core of boolean logic and hence they are implemented in most programming languages.

The standard not function just flips the input value. If given true it returns false and if given false it returns true. However, the emacs lisp interpreter treats any string, number, list or any other valid data type as true. Thus, the not function returns nil for every valid type other than nil.If given nil as input it returns t.

Code :
(not t)
(not nil)
(not "cool")
(not 6)
(not (+ 1 2))
(not '(4 3 2 1))
(not (not nil))

The standard 'or' function would take in a series of t or nil values and return t if any of its inputs were t and it would return nil if all its inputs were nil. However, the emacs lisp interpreter treats any symbol with a valid data type as true and hence the input may consist of any symbol of any valid data type. The answer returned by the or function is the first non-nil argument encountered in order or nil if all its arguments are nil. Thus the or function performs a kind of short-circuit evaluation.

Similarly the standard 'and' function would take in a series of t or nil values and return the value t if and only if all its inputs were true. However, the short-circuit and of emacs lisp is such that if all its input arguments are non-nil it returns the last input argument. However, if even one nil argument is supplied to it it returns nil.

Thus, we find that we can conveniently evaluate commands one after another using the or and and functions (see the last program in the code below and see the documentation for print).

Code :
(or t t t)
(or "hello" t nil)
(or t "hello" nil)
(not (or t "hello" nil))
(and "hello" nil t)
(and "hello" 4 "bye")
(and (or (< 4 3) (> 5 4)) (message "Hello %s" user-login-name))
(and (or (< 4 3) (> 0 4)) (message "Hello %s" user-login-name))
(and (or (print "Hello world")) (print "Good job!") nil)


Post a Comment