Introduction to PROLOG Programming LAB (2) PDF
Document Details
Tags
Summary
This document provides a concise overview of PROLOG programming constructs, including facts, rules, and queries. It also covers fundamental concepts like constants, variables, and arithmetic operators. The material is presented in a structured format, suitable for introductory level computer science courses.
Full Transcript
Introduction to PROLOG programming LAB (2) Description Representing Facts, Rules, and Queries. Constants & Variable. Solving different problems through using the representation of facts and rules. Tests within clauses. Operators in PROLOG (arithmetic operators , comparison). R...
Introduction to PROLOG programming LAB (2) Description Representing Facts, Rules, and Queries. Constants & Variable. Solving different problems through using the representation of facts and rules. Tests within clauses. Operators in PROLOG (arithmetic operators , comparison). Representing Facts, Rules, and Queries. When we write in PROLOG, we are typically constructing one of three things: 1. A FACT - something that we are asserting to be true. 2. A RULE - something that allows us to infer new knowledge from existing facts and rules. 3. A QUERY - a question to be answered based on existing facts and rules. Constants & Variables Constants – Constants are names that begin with lower case letters. – Names of relationships are constants Variables – Variables take the place of constants in facts. – Variables begin with upper case letters Solving different problems using the representation of facts and rules. Example (1) : We have the following facts: 1) Hind likes mango. % هند تحب المانجو 2) Fatima is a girl. 3) Rose is red. 4) Mohammed likes fish. 5) Gamal owns gold.. Solving different problems using the representation of facts and rules. % Program % likes (hind, mango). girl(fatima). red(rose). likes (mohammed, fish). owns (gamal, gold). Solving different problems using the representation of facts and rules. we need to convert or represents the following facts into predicates: % Program % elephant is bigger than horse. horse is bigger than donkey. donkey is bigger than dog. donkey is bigger than monkey. Hint : X is bigger than Y if X is bigger than Z and Z is bigger than Y. Solving different problems using the representation of facts and rules. use the following facts and rules to build a relations for countries if we have the statements: Facts: – Sudan is a north African country. – Egypt is a north African country. – USA is a north American country. – KSA is a Gulf country. – Qatar is a Gulf country. – France is European country. – Pakistan is Asian country. Solving different problems using the representation of facts and rules. – India is Asian country. – Canada is a north American country. – The roots of the North African are Arab. – The roots of the Gulf are Arab. – The roots of the American are Sam. Rules: – Peoples of the origin(roots) from north African countries are Arab and Muslims. – People of the origin(roots) from American countries are Sam and non- Muslims. Solving different problems using the representation of facts and rules. Write predicates one converts centigrade temperatures to Fahrenheit, the other checks if a temperature is below freezing? %PROGRAM c_to_f(C,F) :- F is C * 9 / 5 + 32. freezing(F) :- F =< 32. Tests within clauses When we ask PROLOG a question we are asking for the interpreter to prove that the statement is true. ?- 5 < 7. true = the statement can be proven. ?- 15 < 7. false = the proof failed because either – the statement does not hold, or – the program is broken. Error = there is a problem with the question or program. *nothing* = the program is in an infinite loop. Arithmetic operators Operator Example Meaning + X+Y the sum of X and Y - X-Y the difference of X and Y * X*Y the product of X and Y / X/Y the quotient of X and Y ^ or ** X^Y X to the power of Y mod X mod Y X modulus Y Arithmetic operators %PROGRAM calc :- X is 100 + 200,write('100 + 200 is'), write(X),nl, Y is 400 - 150,write('400 - 150 is'),write(Y),nl, Z is 10 * 300,write('10 * 300 is'),write(Z),nl, A is 100 / 30,write('100 / 30 is'),write(A),nl, B is 100 // 30,write('100 // 30 is'),write(B),nl, C is 100 ** 2,write('100 ** 2 is'),write(C),nl, D is 100 mod 30,write('100 mod 30 is'),write(D),nl. Comparison operators Operator Meaning X>Y X is greater than Y X= Y X is greater than or equal to Y X =< Y X is less than or equal to Y X =:= Y the X and Y values are equal X =\= Y the X and Y values are not equal Comparison operators Examples ?- 1+2=:=2+1. true ?- 1+2=2+1. false. ?- 1+A=B+2. A = 2, B=1 ?- 10=\=100. true Comparison operators Examples Write a PROLOG program to check whether a year is a leap year or not? % PROGRAM leap_check(Year) :- Year mod 4 = 0, Year mod 100 = 0, Year mod 400 = 0.