Prolog programming Introduction to Prolog CS 370 CS

  • Slides: 11
Download presentation
Prolog programming Introduction to Prolog CS 370 – CS 461 (part 2)

Prolog programming Introduction to Prolog CS 370 – CS 461 (part 2)

Data Objects • The arguments of relation can be: data objects ü Constant objects

Data Objects • The arguments of relation can be: data objects ü Constant objects (such as bob and 12). ü General objects (such as X and Y). ü Structure objects ( such as date and time). simple objects constants atoms variables numbers structures

Atoms § Characters: • Upper-case letter A, B, …, Z • Lower-case letter a,

Atoms § Characters: • 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: o Strings of letters, digits and the underscore character, ’_’, starting with a lower case letter, such as : anna, x 25, x_35 AB, x___y, miss_Jones. o Strings of special characters such as: <--->, ===>, …, : : =, . : . , (except : - ). o Strings of characters enclosed in single quotes, such as: ‘Tom’, ‘South_America’, ‘Sarah Jones’.

Numbers and Variables Ø Number o Numbers used in Prolog include integer numbers and

Numbers and Variables Ø Number o Numbers used in Prolog include integer numbers and real numbers. • Integer numbers: 1313, 0, -97 • Real numbers: 3. 14, -0. 0035, 100. 2 o In symbolic computation, integers are often used. Ø Variables are start with an upper-case letter or an underscore character. • Examples: X, Result, _x 23, _23

Questions in prolog. • Another question can be asked in Prolog: pam tom Ø

Questions in prolog. • Another question can be asked in Prolog: pam tom Ø Who is Liz’s parent? ? - parent(X, liz). The answer here will not be true/false, rather, prolog will find the value of X. bob ann liz pat So the answer is : X= tom. jim

Class exercise(1) • Who are bob’s children? tom pam liz bob pat ann jim

Class exercise(1) • Who are bob’s children? tom pam liz bob pat ann jim

Class exercise(2) • Who is parent of whom? tom pam liz bob pat ann

Class exercise(2) • Who is parent of whom? tom pam liz bob pat ann jim

Questions in prolog. • In prolog, we can ask more complicated questions such as:

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). X Parent Y The answer will be: Y = pat, X = bob. Parent jim grandparent

Class exercise(3) • Who are Tom’s grandchildren?

Class exercise(3) • Who are Tom’s grandchildren?

Some important points • A prolog program consists of clauses, each clause ends with

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 • Formulate the following questions and find Prolog answers : Ø Who is

Homework • 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?