Page 3 of 3
Logic rules OK!
What all this means is that logical reasoning, first order predicate calculus to be precise, can swallow expert systems whole and still have some power left over.
For example, the IF..THEN rule can be thought of and written as logical implication. Instead of:
Rule 1: IF everything else working THEN electricity not cut off
we could write:
everything else working > the electricity isn't cut off
(where > is the symbol for 'implies'). Written in this form the sort of reasoning used by expert systems can be seen to be a simple application of the chain rule i.e.:
from A > B and B > C deduce A > C.
As Prolog is the language of logic it is very easy to use it to implement an expert system.
If you want to try out the following examples then download and install SWIProlog which is free, easy to use and standard. See Database the Prolog way for more details and an example.
In Prolog IF..THEN type rules are written the other way around. That is, the Prolog predicate B : A. has to be read as B is true if A is true and so it is equivalent to A>B. Indeed some versions of Prolog allow you to write B : A. as B IF A.
You can now write:
electricity_on : everything_working
as the Prolog equivalent of rule 1.
You can even go one step further and define a predicate that will ask the user a question when a fact isn't known. For example, the three electricity fault finding rules are equivalent to the following Prolog program:
electric_on : everything_working. fuse_ok : everything_working. bulb_blown : electric_on, fuse_ok.
and to ask a question to discover if everything is working all we need is:
everything_working : ask("is everything working"). ask(X) : Y, write(X),read(Y),Y='Y'.
where the predicate ask(X) will ask the question X and return true if the user presses the Y key and false otherwise.
If you type the above program in, compile it and switch to the interactive console and type:
bulb_blown.
You will be asked the same question twice to which you have to answer either Y. or N.  notice that the input takes the form of a Prolog clause and so ends with a dot.
If everything is working and as long as you answer Y then this limited expert will conclude that the bulb_blown clause is indeed true. You are asked twice if everything is working  this primitive implementation doesn't have any memory of your first answer because it uses nothing but backward chaining.
It is not difficult to alter the program so that facts are only asked for once but it is more interesting to look at the ways in which logic and Prolog in particular can extend the limited reasoning used by expert systems.
X the unknown variable
First order predicate calculus, and Prolog in particular, provides much more thinking power than the classical expert system.
Part of this power comes from the more flexible reasoning method but much of this is due to the ability to use variables. This allows you to write general rules that capture knowledge that would otherwise have to be written in terms of many specialised rules.
Even the simple facts can be written in a more flexible way using predicates with variables. For example, if you are producing an expert system that deals with electricity it would be worth introducing a predicate fuse(Device) which was true if the fuse for Device was working. You could also introduce a predicate same_circuit(X,Y) which was true if devices X and Y were on the same electric circuit. It is clear that if fuse(X) is true then fuse(Y) is true if same_circuit(X,Y) or in Prolog:
fuse(Y) : fuse(X),same_circuit(X,Y).
This simple piece of Prolog corresponds to the obvious rule that if you know the fuse is OK for one device then you know that the fuse is OK for all other devices on the same circuit. Simple, yes but many expert systems have no way of including such a general idea. To deal with variables, general rules, relationships between entities and many other desirable concepts you NEED the power of first order predicate calculus and you need proof by unification and resolution as provided by Prolog.
Experts have variables too
If you are planning to buy, or have bought, an expert system shell then there there is no point in asking does it have variables  they all do. In classical expert systems variables are used to hold values corresponding to facts.
For example, you might write a rule something like:
IF fuse1=OK AND electricity=ON THEN fault=bulb blown
where fuse1, electricity and fault are all variables that can take on values such as NOTOK, OK, ON, OFF etc.
Although this looks like the same sort of use that Prolog makes of variables it isn't. In this case the variables are simply being used to hold the state of knowledge about some simple fact. In this case there is no way that you can write a rule that doesn't compare each variable to a constant. This precludes writing general rules like:
fuse(Y) : fuse(X),same_circuit(X,Y).
as in Prolog.
Expert Shell or Prolog?
The main point of this discussion is that Prolog, and logic in general, is much more powerful than the basic sort of expert system using IF..THEN rules and backward chaining. If you want to get involved in reasoning with facts then you really do have to go to the extra trouble of learning Prolog and developing a program. It is worth saying that while Prolog isn't an impossibly difficult language, people vary in their ability to absorb it and use it naturally. That is you might spend a lot of time learning Prolog never to get very far. If you are planning an expert system project it is worth considering a simple expert system shell because most of the really cost effective applications are simple enough not to need firstorder predicate calculus.
Expert system shells are much easier to use than Prolog and if you do find that you need something a little more advanced you can always learn Prolog and transfer the knowledge base to a suitable program.
If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter.
<ASIN:0201403757>
<ASIN:0262514451>
<ASIN:0136070477>
