A restricted form of predicate logic wffs, called Horn clauses, can be used as a programming language to solve computing problems as well as database queries. A Horn clause is one of the following forms:
Example: the following are some facts:
eat(bear,fish). eat(bear,fox). eat(fox,rabbit). eat(deer,grass). animal(bear). animal(fish). animal(fox). animal(rabbit). animal(deer). plant(grass).
Example: the following are some rules:
prey(X) :- eat(Y,X), animal(X). hunted(X) :- prey(X). inFoodChain(X,Y) :- eat(X,Y). inFoodChain(X,Y) :- eat(X,Z), inFoodChain(Z,Y).The rules are interpreted as follows:
(∀ X)(∀ Y)(eat(X,Y) ∧ animal(X) → prey(X))
(∀ X)(prey(X) → hunted(X))
(∀ X)(∀ Y)(eat(X,Y) → inFoodChain(X,Y))
(∀ X)(∀ Y)(eat(X,Z) ∧ inFoodChain(Z,Y) → inFoodChain(X,Y))
i.e. all variables are universally quantified within each rule at the outermost level and the rule itself is treated as an implication.
Example: the following are some queries:
:- animal(bear). :- animal(X). :- eat(bear,X). :- eat(X,Y), plant(Y). :- prey(X). :- inFoodChain(bear,Y).
[~][5:32pm] pl Welcome to SWI-Prolog (Multi-threaded, Version 5.4.7) Copyright (c) 1990-2003 University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- ['foodChain.pl']. % foodChain.pl compiled 0.00 sec, 2,388 bytes Yes ?- animal(bear). Yes ?- animal(X). X = bear ; X = fish ; X = fox ; X = rabbit ; X = deer ; No ?- eat(bear,X). X = fish ; X = fox ; No ?- eat(X,Y), plant(Y). X = deer Y = grass ; No ?- prey(X). X = fish ; X = fox ; X = rabbit ; No ?- inFoodChain(bear,Y). Y = fish ; Y = fox ; Y = rabbit ; No ?- halt. [~][5:33pm]The file foodChain.pl contains the following program (facts and rules)
eat(bear,fish). eat(bear,fox). eat(fox,rabbit). eat(deer,grass). animal(bear). animal(fish). animal(fox). animal(rabbit). animal(deer). plant(grass). prey(X) :- eat(_,X), animal(X). hunted(X) :- prey(X). inFoodChain(X,Y) :- eat(X,Y). inFoodChain(X,Y) :- eat(X,Z), inFoodChain(Z,Y).and the program is read by the Prolog interpreter by the command ['foodChain.pl']. Queries are then presented to the system and the responses are shown on the screen. The first answer is shown immediately; to get subsequent answers, the user has to type a semi-colon.