Prolog (1)
Logic programming is a type of declarative programming. Different from procedural programming languages, such as C/C++ and Java, the focus of declarative programming is to describe a situation (a set of knowledge), not to describe a solution (a sequence of instructions). Especially, logic programming uses a language that is similar to logic, writes a program as a list of facts and rules, and treats the execution of a program as an inference process, from given facts and rules to the desired goal.
In the simplest situation, a fact is a predicate name followed by an argument list and a dot sign, ".". It represents a relation among the arguments, or a property of a (single) argument.
Prolog uses the convention that a constant is an identifier starting with a lower-case letter, while a variable is an identifier starting with a upper-case letter. A constant stands for a specific entity, and different constants stand for different entities. On the other hand, a variable can stand for any entity, and different variables can stand for the same entity. The predicate name must be a constant, while each argument can either be a constant or a variable.
For example, "George is funny" can be represented as a Prolog fact:
funny(george)."George likes swimming" can be represented as:
likes(george, swimming).Things to be noticed:
likes(susie, swimming) :- likes(george, swimming).Things to be noticed:
likes(mary, X) :- likes(susie, X), likes(george, X).
likes(george, swimming). likes(susie, swimming) :- likes(george, swimming). likes(mary, X) :- likes(susie, X), likes(george, X).A Prolog program (sometimes also called a "database") can be used to answer queries, or to achieve goals.
Prolog is an interactive language. The interpreter accepts queries at the prompt "?-", and a query is just like a fact, except that its truth will be judged by the program. To do that, the Prolog interpreter tries to "match" the query with sentences in the program one by one (from top to bottom), and answers "Yes" when a match is found, otherwise answer "No".
For example, after the above program is loaded, we will have:
?- likes(george, swimming). Yes ?- likes(jack, biking). No ?- dislikes(jack, biking). NoPlease note that in Prolog there is no "I don't know", and "No" is the answer as far as "Yes" cannot be obtained.
When a query meets a rule, and the "head" (the conclusion part, or the left-hand side) of the rule matches the query, then the interpreter treats the conditions of the rule as derived sub-queries, and recursively check for them one by one (from left to right). If the answer for them are all "Yes", the original query gets an "Yes", otherwise it gets a "No".
?- likes(susie, swimming). YesWhen there are variables in the sentences to be matched, the interpreter tries to "unify" them, by substituting a variable by a constant (or another variable). If a query contains variable(s) and the unifications are successful, then the relevant substitutions are reported as the answer (and the "Yes" is no longer explicitly reported).
?- likes(mary, swimming). Yes ?- likes(susie, X). X = swimming ?- likes(X, swimming). X = georgeFor queries with multiple answers, type a semicolen ";" after an answer will make the interpreter continues to look for the next answer.
?- likes(X, swimming). X = george ; X = susie ; X = mary ; NoThe last "No" actually means "No more".