Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Prolog
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Execution === Execution of a Prolog program is initiated by the user's posting of a single goal, called the query. Logically, the Prolog engine tries to find a [[resolution (logic)|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. Operationally, Prolog's execution strategy can be thought of as a generalization of function calls in other languages, one difference being that multiple clause heads can match a given call. In that case, the system creates a choice-point, [[unification (computer science)|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]]. For example, given the family relation program defined above, the following query will be evaluated to true: <syntaxhighlight lang="prolog"> ?- sibling(sally, erica). Yes </syntaxhighlight> This is obtained as follows: Initially, the only matching clause-head for the query <code>sibling(sally, erica)</code> 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.e., the conjunction <code>(parent_child(Z,sally), parent_child(Z,erica))</code>. The next goal to be proved is the leftmost one of this conjunction, i.e., <code>parent_child(Z, sally)</code>. Two clause heads match this goal. The system creates a choice-point and tries the first alternative, whose body is <code>father_child(Z, sally)</code>. This goal can be proved using the fact <code>father_child(tom, sally)</code>, so the binding <code>Z = tom</code> is generated, and the next goal to be proved is the second part of the above conjunction: <code>parent_child(tom, erica)</code>. 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: <syntaxhighlight lang="prolog">?- father_child(Father, Child).</syntaxhighlight> enumerates all valid answers on backtracking. Notice that with the code as stated above, the query <code>?- sibling(sally, sally).</code> also succeeds. One would insert additional goals to describe the relevant restrictions, if desired.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)