Prolog Program You are running a computerized dating service

Prolog Program:

You are running a computerized dating service and maintain a database consisting of the following facts that use the predicate person (name, gender, height, age, education (hs,college,masters,phd)):

person(lisa, female, 180, 30, phd).

person(jenny, female, 167, 25, hs).

person(bob, male, 180, 40, phd).

person(charles, male, 190, 30, masters).

person(arnold, male, 177, 29, hs).

You know from experience that a woman will only date a man if

a. He is at least as tall as she is,

b. His educational level is at least as high as hers,

c. He is not younger, and no more than 10 years older than her.

Write a recursive rule edu_le(A,B) in Prolog that succeeds if the educational level A is

less than or equal to B. For example, the following queries should work in this way:

?- edu_le(hs,hs).

yes

?- edu_le(hs,college).

yes

?- edu_le(hs,phd).

yes

?- edu_le(phd,masters).

no

Then, write a Prolog predicate dateable(Female,Male) which encodes the dating rules above:

(Include a printout that shows your query and the program’s responses (you may simply

copy this from SWI-Prolog’s main window).

?- dateable(lisa,charles).

no

?- dateable(lisa, bob).

yes

?- dateable(jenny,arnold).

yes

Solution

/* Assignment : ass5.pl Author : Kyle Reese Almryde Due Date : April 1st, 2013 @ 12:00 pm Instructor : Christian Collberg */ /*======================= 2. Magic Squares =======================*/ % Fact, a board is a 3x3 grid board(A,B,C, % First row D,E,F, % Second row G,H,I). % Third row % Takes a board (a 3 × 3 grid) % The board is magic if all rows, all columns, % and the diagonals sum to 15: magic(board(A,B,C,D,E,F,G,H,I)) :- 15 is A+B+C, % First row 15 is D+E+F, % Second row 15 is G+H+I, % Third row 15 is A+D+G, % First column 15 is B+E+H, % Second column 15 is C+F+I, % Third column 15 is A+E+I, % First diagonal 15 is G+E+C. % Second diagonal /*===================== 3. Prolog Arithmetic =====================*/ % abs(X,Y) % takes a number X as input and computes the % absolute value |X| as output %% | ?- abs(0,0). %% yes %% | ?- abs(-1,1). %% yes %% | ?- abs(-1,Y). %% Y = 1 %% | ?- abs(5,Y). %% Y = 5 abs(X,Y) :- X >= 0 -> Y is X. abs(X,Y) :- X < 0 ->Y is -X. % seq(First, Last, N) % Instantiates N to each integer value from First % through Last, inclusive. seq(First, Last, N) :- First =< Last -> N is First. seq(First, Last, N) :- First < Last, F1 is First + 1, seq(F1, Last, N). /*=========================== 4. Chess ===========================*/ % withinBoard(Row,Col) % Determine whether a given position is within the bounds of a 8x8 chess % board withinBoard(Row,Col) :- Row < 9, Row > 0, Col < 9, Col > 0. % move(Row,Col) % Gives the positions where the knight can move, relative to the current % position move(Row,Col) :- withinBoard(Row,Col). % knight(Row,Col,Row1,Col2) % Given a position (Row,Col) on a chess board, will produce all the legal % positions (clockwise around) where a knight might move from that position knight(Row,Col,Row1,Col1) :- Row1 is Row + 2 , Col1 is Col - 1 , move(Row1,Col1). knight(Row,Col,Row1,Col1) :- Row1 is Row + 2 , Col1 is Col + 1 , move(Row1,Col1). knight(Row,Col,Row1,Col1) :- Row1 is Row + 1 , Col1 is Col + 2 , move(Row1,Col1). knight(Row,Col,Row1,Col1) :- Row1 is Row - 1 , Col1 is Col + 2 , move(Row1,Col1). knight(Row,Col,Row1,Col1) :- Row1 is Row - 2 , Col1 is Col + 1 , move(Row1,Col1). knight(Row,Col,Row1,Col1) :- Row1 is Row - 2 , Col1 is Col - 1 , move(Row1,Col1). knight(Row,Col,Row1,Col1) :- Row1 is Row - 1 , Col1 is Col - 2 , move(Row1,Col1). knight(Row,Col,Row1,Col1) :- Row1 is Row + 1 , Col1 is Col - 2 , move(Row1,Col1). /*========================= 5. Peano Axiom (Rerun!) =========================*/ % sum(A,B,C). % Representing integers using the Peano Axioms. sum(zero,A,A). % The sum of 0 and any integer A is A sum(succ(A),B,succ(C)):- % The sum the successor of A and B is C sum(A,B,C). /*========================= 6. Parts List =========================*/ % A database of facts about parts and their corresponding objects % Facts are represented as has(object,part,# of parts) has(bicycle,wheel,2). has(bicycle,handlebar,1). has(bicycle,brake,2). has(wheel,hub,1). has(wheel,spoke,32). has(bicycle,frame,1). has(car,steering_wheel,1). has(car,stereo,1). has(car,tires,4). % partof(X,Y) % Determine if the object X has the part Y, based on the provided facts % above. partof(X,Y) :- has(X,Y,_). partof(X,Y) :- has(X,Z,_), has(Z,Y,_). /*======================= 7. Dating Database =======================*/ %% Facts about people % person(name, gender, height(cm), age, education (hs,college,masters,phd)) person(lisa, female, 180, 30, phd). person(jenny, female, 167, 25, hs). person(bob, male, 180, 40, phd). person(charles, male, 190, 30, masters). person(arnold, male, 177, 29, hs). %% Facts about education edu_less(hs,college). edu_less(college,masters). edu_less(masters,phd). %% Queries % edu_le(A,B) % A recursive rule that determines if A is less than or equal to B edu_le(A,B) :- edu_less(A,B). edu_le(A,B) :- edu_less(A,C), edu_less(C,D), edu_less(D,B). edu_le(A,B) :- A = B. % dateable(Female,Male) % A couple is considered \'Dateable\' if they meet the following conditions % 1. he is at least as tall as she is, % 2. his educational level is at least as high as hers, % 3. he is not younger, and no more than 10 years older than her. % For the purposes of this assignment we assume that women will date only men and men will date only % women. dateable(Female,Male) :- person(Female,X,Xheight,Xage,Xeducation), person(Male,Y,Yheight,Yage,Yeducation), Xheight =< Yheight, edu_le(Xeducation,Yeducation), Xage10 is Xage + 10, Yage >= Xage, Yage =< Xage10.
Prolog Program: You are running a computerized dating service and maintain a database consisting of the following facts that use the predicate person (name, gen
Prolog Program: You are running a computerized dating service and maintain a database consisting of the following facts that use the predicate person (name, gen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site