Some very basic concepts and codes for prolog language!
Prolog, derived from the French
for Programming in Logic, is a Declarative programming language, especially seen in the field of Artificial Intelligence(AI). The language is declarative, in the sense that it handles the working of logic all by itself for finding the answers to logical questions/queries, once it is given some facts and rules determining the working of the logic.
Prolog scripts/queries basically are consisted of the following three entities:
-
Atoms and numbers: Atoms are entities which are named such that they begin with a small letter only. They can also be represented by names surrounded by single quotes
' '
. Numbers represent any real floating point number.% Examples of atoms % tony % child % 'Joker' % Notice the atom surrounded by ' ' % doesHomeWork % goesShopping % 'likesToEat' % 'action(percepts)'
-
Variables: Variables are entities capable of binding to (say, storing) any atom or number. They must be named so that the names begin with either a capital letter or the underscore
_
character.% Examples of variables % _ % This is the 'anonymous variable' % X % Y % Tony % _child % _80days % Lives9
-
Complex Terms: Complex Terms can be thought of as a function which defines the relationship between one or more number of entities. The name of the complex term is known as functor. The number of arguments the functor takes is known as the arity of the functor/complex term. As such, the complex terms can also be represented as
<name of the functor>/<arity of the functor>
.The functors with different arities are treated as distinct functors.
% Example of complex terms % sour(grapes) % represented as sour/1 % likes(charlie, chocolate) % represented as likes/2 % makes(wonka, chocolate) % represented as makes/2 % makes(elon, cars, cybertruk) % represented as makes/3 % runs(deer, cheetah, human) % represented as runs/3
In a prolog script, every statement either consists of a fact, or a rule, terminated by .
.
% the statement below denotes a fact.
man(socrates). % Read as socrates is a man.
% the statement below denotes a rule.
mortal(X) :- man(X). % Read as 'X' is mortal if 'X' is a man.
When querying the above script, the following output could be obtained
?- mortal(socrates).
true.
The prolog interactive replies to queries by either yes/true
, no/false
, or a list of matching values/atoms.
- Logic
- Variables
- Facts and Rules
- Conjunctions(AND)
- Disjunctions(OR)
- Anonymous variables
- Transitive Logic Example
-
Navigate to
/codes
directory. -
Launch prolog console.
-
Type the name of the file you want to run queries on without the extension in
[ ]
.1 ?- [logic0]. true.
-
If the console returns
true
, you are all set!