10 Prolog
Logic Programming¶
Logic program is expressed as a set of atomic sentences(facts) and Horn clauses (rules)
We can ask questions in the form of conjectures (like facts, but with variables) to find out what values will make the conjecture true
Statement | Type of Statement |
---|---|
Ramu is a boy | Fact |
Ramuβs father is Kicha | Relationship |
Ramu eats chocolate if chocolates are available and chocolates have nuts | Horn Clause (Rule) |
LIPS: Logical Inferences per second
Prolog¶
Consists of a database of predicates composed of facts and rules, involving constants and variables
Properties¶
- There is no structure imposed on a Prolog program, there is no main procedure, and there is no nesting of definitions.
- Terminator is
.
- Assignment is performed using
is
- Program is executed by asking a question using
?-
, called a query - All facts and rules are global in scope and the scope of a variable is the fact or rule in which it appears
- Facts, rules, and queries are called clauses.
Naming Rule | Example | |
---|---|---|
Constant (Atoms) | Number String starting with lowercase letter | tom bill a1 x 217 -33.74 |
Variable | String starting with uppercase letter String starting with _ | Thahir _thahir X |
Fact | True about some constant |
Predicate | Function result, which can be true/false |
Relationship¶
Rules¶
Symbol | Meaning |
---|---|
:- | if |
, | and |
; | or |
grandmother(x, z) :- mother(x, y), parent(y, z)
grandmother(x, z) :- mother(x, y), ( father(y, z); mother(y, z) )
Query¶
Queries in Prolog are entered by the user following the ?- prompt
?- grandmother(habeeb_fathima, thahir) % will give Yes
?- grandmother(What, thahir) % will give habeeb_fathima
Datatypes¶
Complex term¶
No of arguments in a complex term is called arity
Other¶
% list
[dog, cat, mouse]
% records/tuples are represented as patterns; elements are accessed by pattern matching
Example of a Knowledge Base¶
loves(vincent, mia).
loves(marcellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
jealous(X, Y) :- loves(X, Z), loves(Y, Z).
?- loves(X, mia) % gives us who loves mia
?- jealous(X, Y) % gives us which pairs are jealous
Database¶
part(p001,nut).
part(p002,bolt).
supplier(s001,abc).
supplier(s002,def).
part_supplier_cost(p001,s001,0.10).
part_supplier_cost(p002,s001,0.15).
list(Pkey):-
part(Pkey,Pname),
part_supplier_cost(Pkey,Skey,Cost),
supplier(Skey,Sname),
write(Pkey),write(' '),
write(Pname),write(' '),
write(Skey),write(' '),
write(Sname),write(' '),
write(Cost),nl.
Type Predicates¶
=..
is used to compose and decompose terms
Arithmetic Expression¶
Unification¶
Two terms unify,
- if they are the same term or
- if they contain variables that can be uniformly instantiated with terms in such a way that the resulting terms are equal.
Example¶
Unify? | |
---|---|
mia and mia | β |
42 and 42 | β |
woman(mia) and woman(mia) | β |
vincent and mia | β |
woman(mia) and woman(jody) | β |
Comparison Operators¶
Symbol | Operation |
---|---|
A =:= B | A==B (value) |
A =\+= B | A!=B (value) |
A < B | numeric |
A @< B | String comparison |
A =< B | numeric |
A @=< B | String comparison |
A > B | numeric |
A @> B | String comparison |
A >= B | numeric |
A @>= B | String comparison |
Value of functor with argument (term) is always more than any numeric and character (or string)
Logical Operators¶
a :- b. % a if b
a :- b,c. % a if b and c.
a :- b;c. % a if b or c.
a :- not b. % a if b fails
a :- b -> c;d. % a if (if b then c else d)
Meaning | |
---|---|
a :- b. | a if b |
a :- b,c. | a if b and c |
a :- b;c. | a if b or c |
a :- not b. | a if b fails |
a :- b -> c;d. | a if (if b then c else d) |
Functions¶
Functions are implemented using relations