Clauses with bodies are called rules. An example of a rule is: animal X :- cat X. If we add that rule and ask what things are animals? For this reason, a comparatively small set of library predicates suffices for many Prolog programs. These predicates are not given a relational meaning and are only useful for the side-effects they exhibit on the system.
|Published (Last):||5 November 2005|
|PDF File Size:||17.3 Mb|
|ePub File Size:||11.32 Mb|
|Price:||Free* [*Free Regsitration Required]|
Clauses with bodies are called rules. An example of a rule is: animal X :- cat X. If we add that rule and ask what things are animals? For this reason, a comparatively small set of library predicates suffices for many Prolog programs.
These predicates are not given a relational meaning and are only useful for the side-effects they exhibit on the system. Logically, the Prolog engine tries to find a resolution refutation of the negated query. The resolution method used by Prolog is called SLD resolution. If the negated query can be refuted, it follows that the query, with the appropriate variable bindings in place, is a logical consequence of the program.
In that case, all generated variable bindings are reported to the user, and the query is said to have succeeded. In that case, the system creates a choice-point, unifies the goal with the clause head of the first alternative, and continues with the goals of that first alternative.
If any goal fails in the course of executing the program, all variable bindings that were made since the most recent choice-point was created are undone, and execution continues with the next alternative of that choice-point. This execution strategy is called chronological backtracking. This results in the following query being evaluated as true:? Yes This is obtained as follows: Initially, the only matching clause-head for the query sibling sally, erica is the first one, so proving the query is equivalent to proving the body of that clause with the appropriate variable bindings in place, i.
The next goal to be proved is the leftmost one of this conjunction, i. Two clause heads match this goal. Again, this can be proved by the corresponding fact. Since all goals could be proved, the query succeeds. Since the query contained no variables, no bindings are reported to the user. A query with variables, like:? Notice that with the code as stated above, the query? One would insert additional goals to describe the relevant restrictions, if desired.
If a proof for that goal can be found, the original goal i. If no proof can be found, the original goal succeeds. This kind of negation is sound if its argument is "ground" i. Soundness is lost if the argument contains variables and the proof procedure is complete. In particular, the query? Programming in Prolog[ edit ] In Prolog, loading code is referred to as consulting. Prolog can be used interactively by entering queries at the Prolog prompt? If there is no solution, Prolog writes no. If a solution exists then it is printed.
If there are multiple solutions to the query, then these can be requested by entering a semi-colon ;. There are guidelines on good programming practice to improve code efficiency, readability and maintainability. An example of a query:? Hello World! Design patterns[ edit ] A design pattern is a general reusable solution to a commonly occurring problem in software design.
In Prolog, design patterns go under various names: skeletons and techniques,   cliches,  program schemata,  and logic description schemata. This can be used for list comprehension.
This can be used to enumerate perfect numbers, and also to check whether a number is perfect. Modules[ edit ] For programming in the large , Prolog provides a module system. The module system is standardised by ISO. Most notably, the rewriting equips the predicate with two additional arguments, which can be used to implicitly thread state around,[ clarification needed ] analogous to monads in other languages. DCGs are often used to write parsers or list generators, as they also provide a convenient interface to difference lists.
Meta-interpreters and reflection[ edit ] Prolog is a homoiconic language and provides many facilities for reflection. Its implicit execution strategy makes it possible to write a concise meta-circular evaluator also called meta-interpreter for pure Prolog code: solve true.
For example, Sterling and Shapiro present a meta-interpreter that performs reasoning with uncertainty, reproduced here with slight modifications:  solve true, 1 :-!. This interpreter uses a table of built-in Prolog predicates of the form  builtin A is B. Given those, it can be called as solve Goal, Certainty to execute Goal and obtain a measure of certainty about the result.
Turing completeness[ edit ] Pure Prolog is based on a subset of first-order predicate logic , Horn clauses , which is Turing-complete. A simple example Turing machine is specified by the facts: rule q0, 1, q0, 1, right.
This machine performs incrementation by one of a number in unary encoding: It loops over any number of "1" cells and appends an additional "1" at the end. Example query and result:?
Prolog Programming for Artificial Intelligence