Logic programming is the declaration of a series of facts and rules of deduction. The execution of a program consists in proving a theorem. The program answers whether or not the theorem can be proved from the statements made beforehand.
Logic programming is different from procedural programming (C, JAVA, etc.), the calculations are done in the form of logical proofs. We speak of declarative programming: the program describes a situation corresponding to a problem to be solved. Declarative programming is not incompatible with object structuring.
The formulas expressed in Prolog are restricted to Horn clauses and first order logic. A Horn clause is a clause with at most one positive literal. These will not be explained but implicit in the explanations of the Prolog language.
Rules, facts and goals
is written in Prolog: grandchild_of (X, Z): - child_of (X, Y), child_of (Y, Z)
A program is therefore a set of facts and rules, all in the form of Horn clauses. The Prolog interpreter uses this knowledge base to meet goals.
Write a Prolog program
A program is a list of facts and rules. We group together the rules which have the same leading predicate and the facts which relate to the same predicate. The order in which the rules are declared can have an impact on the goals.
A constant ('roger') covers the whole program while a variable only exists in the clause where it is found. So the variable X of nephew (X, Y) is not the same as that of grand_child_ of (X, Y). On the other hand, it is the same as in the conjunction which is associated with it.
Once the programs loaded and thus the base of facts and rules constituted, it is possible to ask questions. This is called a demonstration or a question resolution. A question arises by replacing in a rule or making a constant or variable by a '?'. To find out all the rules of a type, write the name of the rule containing variables followed by a '?' : human (H)? is used to know all the humans of the base.