next up previous contents
Next: What You Should Up: Operators Previous: How to Change

A More Complex Example

We now try to represent data structures that look like:

 
if a and b or c then d

As we already have a representation for ``a and b or c'', this reduces to representing

 
if a then b

We will make ``then'' an infix operator of arity 2. Because both subtrees might contain and/2 we will need to make then/2 of higher precedence than and/2 ---say,1050 and not associative. Hence:

 
op(1050,xfx,then).

This means that ``if'' must be a prefix operator. As we do not wish expressions of the form

 
if if a

we must make if/1 of higher precedence than then/2 (say, 1075) and if/1 must be non associative:
 
op(1075,fx,if).

We can now represent

 
if a and b or c then d

as the tree

or, as the Prolog term

 
if(then(and(a,or(b,c)),d))

This Prolog term is difficult to read but unambiguous while the representation using operators is easy to read but depends heavily on your understanding the precedences and associativities involved. All right if you wrote the code but the code is harder for someone else to read.



Paul Brna
Mon May 24 20:14:48 BST 1999