The acronym Prolog stands for Programming in LogicFirst Prolog system was introduced in 1973 by Alain ColmerauerPurpose: Natural Language Understanding, Translation.
In conventional programming:  The programmer instructs machine how to solve a problem by performing a given sequence of actions.  The machine carries out the instructions in a specified order.In logic programming:  The programmer defines what the problem using the language of logic  The system applied logical deduction to find answers to the problem.“Conventional programming languages are fat and flabby” — John Backus
A Prolog program is a description of a world written in Prolog notation.Example: Ann likes every toy she plays with. Doll is a toy. Snoopy is a toy. Ann plays with Snoopy. Sue likes everything Ann likesEach line of the program is a “clause.” First and the last clauses are “rules.” The other three are “facts.”

likes(ann, X) :-  toy(X),plays(ann, X).



plays(ann, snoopy).

likes(sue, Y) :-  likes(ann, Y).

X and Y are variables (begin with capital letter) ann, doll, snoopy, sue are constants (begin with lower case) likes, toy, plays are predicates “:-“ and “,” are logical symbols (read “if” and “and”)A term is a variable or a constant or an expression of the form f(T1, T2, … ,Tn) where f is a function symbol and T1, … Tn are terms (n > 0). The symbol f is called the “functor”, n is the arity, T1, T2, … Tn are the arguments of the term. Remember: functors are not predicate symbols.

ann. % These are terms snoopy. % because they are constants 4. square(4).% This is a term, because % square is a function symbol, % and 4 is a constant plus(1, square(4)). % This is also a term

An atomic formula is an expression of the form p(T1, T2, …, Tn), where p is a predicate symbol and T1, T2, … Tn are terms. If n == 0, that is there are no arguments, parentheses can be omitted. They contain predicates, constants, and variables and have a truth-valuelikes(ann,X). % likes is the predicate. toy(X). plays(ann,snoopy). isprime(plus(1,square(4)))

The last one is an atomic formula that happens to be true. It shows that atomic formulas can contain “function symbols.” A clause is formula of the form A :- B1, B2, … ,Bm.

where m >= 0 and A and Bi are atomic formulas. (It is read: A if B1 and B2 … and Bm) The Atomic formula A is called the head of the clause and B1, B2, … Bm is called the body of the clause.If m >0, then the clause is called a RULE. If m = 0, the implies symbol :- can be omitted. The clause looks like A. and now it is called a FACT. A Prolog program is a finite set of clauses.

Queries Prolog works by trying to satisfy a goal. The Prolog interpreter contains a main loop where the user types a goal (also called a query) and Prolog tries to satisfy it in any and all possible ways. 

This process is repeated indefinitely. This stops only when you type quit. (i.e. the word quit followed by a period) as the goal.

In Prolog notation ?- likes(sue, X). is a query. It asks, “What does sue like?” (Strictly speaking, it asks “find an X for which likes(sue, X) is true).

Prolog figures out the answer as follows: I Know: Rule 1:sue likes X if ann likes X. Rule 2:ann likes X if X is a toy and ann plays with X. Fact 1: snoopy is a toy. Fact 2: ann plays with snoopy. Therefore, a new fact is deduced: ann likes snoopy. (Because of Rule2, Fact 1, Fact 2). Therefore, another new fact is deduced: sue likes snoopy. Prolog will answer: X = snoopy

A “consult” loads the facts and rules from the specified file into the Prolog database. For example, if try.pro contained: toy(doll). toy(snoopy). plays(ann, snoopy).

Here is the familiar Factorial function: factorial(0,1). factorial(N,F) :- N > 0, N1 is N – 1, factorial(N1, F1), F is N * F1.

?- factorial(7, F). F = 5040

First build a table of (state, capital) pairs capital(newyork, albany). capital(pennsylvania, harrisburg). capital(maryland, annapolis).

findCapital :- write(‘Enter the State: ‘), read(State), searchCapital(State).

searchCapital(State) :- capital(State, City), write(‘The Capital is: ‘), write(City), nl.

searchCapital(State) :- write(‘I do not know that state.’),nl, write(‘Can you tell me the capital?’),nl, write(‘I will remember next time.’), read(Capital), assertz(capital(State, Capital)), write(‘Thank you!’), nl.

Prolog has the following predicates to handle input/output using files: see, seeing, seen and tell, telling, told.

see(F) This opens a file for reading. Examples: see(‘proj1.txt’). or F = ‘proj1.txt’, see(F). seen This closes the current input stream. seeing(F) This instantiates F to the current input stream.

tell(F) Open file for writing told Close the current output stream. telling(F) This instantiates F to the current output stream