Sem 7 >
AI
WAP - More Mathematical Functions.
Maths.pl natural_number(N) :- integer(N),N>0. add(M,N,Sum) :- Sum is M+N. sub(M,N,Diff) :- Diff is M-N. mul(M,N,Prod) :- Prod is M^N. div(M,N,Sol,Rem) :- Sol is M/N, Rem is mod(M,N). exp(M,N,Exp) :- Exp is M**N. factorial(0,1). factorial(N,F) :- N>0, M is N-1, factorial(M,Fm), F is N*Fm. Queries: 11 ?- factorial(3,F). F = 6 . 12 ?- | . ERROR: Stream user_input:5:57 Syntax error: Unexpected end of clause 12 ?- natural_number(5). true. 13 ?- natural_number(-1). false. 14 ?- factorial(5). ERROR: Undefined procedure: factorial/1 ERROR: However, there are definitions for: ERROR: factorial/2 false. 15 ?- factorial(5,F). F = 120 . 16 ?- div(7,3,Sol,Rem). Sol = 2.3333333333333335, Rem = 1. Now, LCM, GCD: gcd(A,B,GCD):- A=B,GCD=A. gcd(A,B,GCD):- A<B,NB is B-A, gcd(A,NB,GCD). gcd(A,B,GCD):- A>B, NA is A-B,gcd(NA,B,GCD). lcm(X,Y,LCM):-gcd(X,Y,GCD), LCM is X*Y//GCD. Lab work: 1. arith 2. facotorial 3. gcd/lcm 4. rerlation (family) 5. health |
0 Intro
To transform an English sentence to Predicate Logic, we remove unnecessary terms.
This leaves only the relationship and the entities involved, known as arguments.
Predicate Logic
Ex: A pie is good = good(pie)
The relation is ‘good’, the relation’s argument is ‘pie’.
Prolog Rules To infer facts from other facts, Prolog uses Rules. Ex: Bill likes cars if they are red = likes(bill, cars):- red(cars).
By the way, in prolog, ‘:-’ is pronounced ‘if’.
Prolog Queries • We may want to ask, “What does ali like?”
•In Prolog syntax, we ask: likes(ali,What). Note: capital W on what
Examples: a_kind_of(aa,ship).
a_kind_of(bb,ship).
part_of(aa,jordanian_navy).
part_of(bb,jordanian_navy).
part_of(jordanian_navy,jordanian_government). a_kind_of(jordanian_government,government).
color(ship,red).
a_kind_of(aa,ship). a_kind_of(bb,ship).
part_of(aa,jordanian_navy).
part_of(bb,jordanian_navy).
part_of(jordanian_navy,jordanian_government). a_kind_of(jordanian_government,government).
color(ship,red).
Parts of a Prolog Program All programs written in Prolog contain at least 4 parts:
Domains: The section of code where we define the legal values for any type that is not defined as a standard type. Predicates: The predicate section is where we define predicates to be used in the CLAUSES section and define the domains for their arguments. Symbolic name of a relation
We found it best to think of predicate declarations as function prototypes.
Ex: age(string, integer)
Clauses: Clauses are the heart of the program.
A clause is an instance of a predicate, followed by a period.
Clauses are of two types:
Goals: Part of program where queries are made.
Can be singular or compound.
Each part of a compound goal is known as a subgoal.
To satisfy a compound goal (or query) each subgoal must itself be satisfied by the system •Ex: What are the things do Bill and Cindy both like (in common)?
•In Prolog:
likes(bill, What), likes(cindy, What). Attached a ppt with more information on the AI page.
|
WAP to perform simple arithmetic operations.
COMMANDS ON SWI-Prolog Shell // Note the variables should not be in small case. It needs to have at least one Upper Case character. 1 ?- a is 2. false. 2 ?- A is 2. A = 2. 3 ?- number is 2. false. 4 ?- Number is 2. Number = 2.
1 ?- A IS 7. ERROR: Syntax error: Operator expected ERROR: A ERROR: ** here ** ERROR: IS 7 . 1 ?- A is 7. A = 7.
1 ?- X is 7. Y is 3. Z is X+Y. X = 7. Y = 3. ERROR: is/2: Arguments are not sufficiently instantiated 4 ?- X is 7, Y is 3, Z is X+Y. X = 7, Y = 3, Z = 10.
1 ?- X is 7, Y = 10, C is X + Y. X = 7, Y = 10, C = 17. 2 ?- X is 7, Y = 10, C = X + Y. X = 7, Y = 10, C = 7+10. |
Lisp and Prolog 1
female(mary). father(X,Y):- male(X), parent(X,Y). SAMPLE PROLOG FILE: familytree.pl
parent(pet,mike). parent(pet,july). parent(pet,amenda). parent(mary,mike). parent(mary,july). parent(mary,amenda). female(mary). female(july). female(amenda). male(mike). male(pet). sibling(mike,july). sibling(mike,amenda). sibling(amenda,mike). sibling(july,mike). sibling(july,amenda). sibling(amenda,july). father(X,Y):-male(X),parent(X,Y). mother(X,Y):-female(X),parent(X,Y). brother(X,Y):-male(X),sibling(X,Y),X\=Y. sister(X,Y):-female(X),sibling(X,Y),X\=Y. SAMPLE QUERIES: |
1-5 of 5