Prolog programming Introduction to Prolog (part2) CS 370 – CS461
Data Objects The arguments of relation can be: Constant objects (such as bob and 12). General objects (such as X and Y). Structure objects ( such as date and time). data objects variables constants structures simple objects numbers atoms
Atoms Characters: Atoms can be constructed in three ways: Upper-case letter A, B,…, Z Lower-case letter a, b,…, z Digits 0,1,2,…,9 Special characters such as +-*/<>=:.&_~ Atoms can be constructed in three ways: Strings of letters, digits and the underscore character,’_’, starting with a lower case letter, such as : anna, x25, x_35AB, x___y, miss_Jones. Strings of special characters such as: <--->, ===>, …,::=,.:., (except :- ). Strings of characters enclosed in single quotes, such as: ‘Tom’, ‘South_America’, ‘Sarah Jones’.
Numbers and Variables Number Numbers used in Prolog include integer numbers and real numbers. Integer numbers: 1313, 0, -97 Real numbers: 3.14, -0.0035, 100.2 In symbolic computation, integers are often used. Variables are start with an upper-case letter or an underscore character. Examples: X, Result, _x23, _23
Questions in prolog. Another question can be asked in Prolog: pam tom bob liz ann pat jim Another question can be asked in Prolog: Who is Liz’s parent? ?- parent(X, liz). The answer here will not be true/false, rather, prolog will find the value of X. So the answer is : X= tom.
Class exercise(1) Who are bob’s children? pam tom bob liz ann pat jim
Class exercise(2) Who is parent of whom? pam tom bob liz ann pat jim
Questions in prolog. In prolog, we can ask more complicated questions such as: Who is a grandparent of jim? ?- parent(Y,jim) , parent( X,Y). The answer will be: Y = pat, X = bob. X Parent Y grandparent Parent jim
Class exercise(3) Who are Tom’s grandchildren?
Some important points A prolog program consists of clauses, each clause ends with a full stop. Questions in prolog consist of one or more goals. ?-parent(X,ann),parent(X,pat) . means the conjunctions of the goals: X is parent of ann , and X is parent of pat.
Homework Who is bob’s father? Who is bob’s sister? Formulate the following questions and find Prolog answers : Who is bob’s father? Who is bob’s sister? Do ann and pat have a common parents?