Prolog is a logic programming language used for artificial intelligence and computational linguistics. It is declarative and logic is expressed through relations represented as facts and rules. A Prolog program consists of clauses that define relations, either facts with no body or rules with a head and body. Execution is initiated by a query, which tries to find a resolution refutation by using SLD resolution. Examples demonstrate writing to output, relations between family members, and an implementation of quicksort.
Introduction Prolog isa general purpose logic programming language associated with artificial intelligence and computational linguistics.Prolog is declarative, The program logic is expressed in terms of relations, represented as facts and rules and a computation is initiated by running a query over these relations.The language was first conceived by a group around Alain Colmeraurer in Marseille, France.
4.
Data typesProlog’s singledata type is the term.Terms are either atoms, numbers, variables and compound terms.An atom is a general purpose name with no inherit meaning. Ex: x,blue etc.Numbers can be floats or integers.Variables are denoted by a string consisting of letters, numbers and underscore characters, and beginning with an upper case characters and underscore.A compound term is composed of an atom called “functor” and a number of “arguments”, which are again terms.
5.
Compound terms areordinarily written as a functor followed by a comma-separated list of argument terms, which is contained in the parentheses. A List is an ordered collection of terms. It is denoted by square brackets with the terms separated by brackets with the terms separated by commas or in the case of an empty list, [] ex: [1,2,3] or [red, green, blue] etc.Strings are a sequence of characters surrounded by quotes.Ex: “ to be, or not to be”
6.
Rules and FactsPrologprograms describe relations, defined by means of clauses.There are two types of clauses:Facts
7.
RolesA role isof the form Head :- Body and is read as head is true if body is true.A rule’s body consists of calls to predicates, which are called the rule’s goals.Classes with empty bodies are called facts.Ex: cat(tom).
8.
EvaluationExecution of aProlog program is initiated by the user's posting of a single goal, called the query.Logically, the Prolog engine tries to find a resolution refutation of the negated query. The resolution method used by Prolog is called SLD resolution.
9.
Ex: mother_child(trude, sally). father_child(tom, sally). father_child(tom, erica). father_child(mike, tom). sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). parent_child(X, Y) :- father_child(X, Y). parent_child(X, Y) :- mother_child(X, Y).Results in the following query being evaluated as true:?- sibling(sally, erica). Yes
10.
Loops and RecursionIterativealgorithms can be implemented by means of recursive predicatesNegation:The built-in Prolog predicate \+/1 provides negation as failure, which allows for non-monotonic reasoning.legal(X) :- \+ illegal(X). Prolog attempts to prove the illegal(X).
11.
If a prooffor that goal can be found, the original goal (i.e., \+ illegal(X)) fails.
12.
If no proofcan be found, the original goal succeeds.Examples:An example of a query:?- write('Hello world!'), nl. Hello world! true. ?-An example Quick Sort sorting algorithm, relating a list to its sorted version:partition([], _, [], []). partition([X|Xs], Pivot, Smalls, Bigs) :- ( X @< Pivot -> Smalls = [X|Rest], partition(Xs, Pivot, Rest, Bigs) ; Bigs = [X|Rest], partition(Xs, Pivot, Smalls, Rest) ). quicksort([]) --> []. quicksort([X|Xs]) --> { partition(Xs, X, Smaller, Bigger) }, quicksort(Smaller), [X], quicksort(Bigger).
13.
Visit more selfhelp tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net