Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Logic Programming Assignment In this assignment, you are asked to write simple l

ID: 3738476 • Letter: L

Question

Logic Programming Assignment

In this assignment, you are asked to write simple logic programs.

2. A library has a searchable database of books. Each book has a unique classification code. For instance, the book with classification code QA76.642.C39 is entitled Parallel Algorithms; it is published by the CRC Press in 2008 and authored by Henri Casanova, Arnaud Legrand and Yves Robert. Assume that the database contains the following relations each of which is represented as a collection of facts

. ? title(Classifier, Title) says that the title of the book with classification code Classifier is Title;

? publishedBy(Classifier,Publisher) says that the book with classification code Classifier is published by Publisher;

? author(Person, Classifier) says that Person is an author of the book with classification code Classifier;

? publishedIn(Classifier,Year) says that the book with classification code Classifier is published in year Year.

For instance, the above mentioned book is represented as the following facts where a person is represented by a term of the form person(Firstname,Lastname). title("QA76.642.C39", "Parallel Algorithms"). publishedBy("QA76.642.C39","CRC press"). author(person(henri,casanova),"QA76.642.C39"). author(person(arnaud,legrand),"QA76.642.C39"). author(person(yves,robert),"QA76.642.C39"). publishedIn("QA76.642.C39",2008). Build a database containing at least 5 books and write and test the following predicates:

a) Write a predicate multi_authored(Title) that is true if and only if a book named Title has multiple authors;

b) Write a predicate author_title(Person, Title) that is true if and only if Person is an author of a book named Title;

c) Write a predicate author_year(Person, Year) that is true if and only if Person published books in year Year;

d) Write a predicate new_book(Title) that is true if and only if there is a book named Title that is published in 2017 or later.

e) Write a predicate coauthor(Person1, Person2) that is true if and only if Person1 and Person2 co-authored a book.

Explanation / Answer

--a) Write a predicate multi_authored(Title) that is true if and only if a book named Title has multiple authors;
select count(*)
from author A
where A.classifier = (select classifier from title where title='Parallel Algorithms2')
having count(*) > 1 ;

--b) Write a predicate author_title(Person, Title) that is true if and only if Person is an author of a book named Title;
select *
from author A
where A.classifier = (select classifier from title where title='Parallel Algorithms')
AND A.firstname = 'henri2'
AND A.lastname = 'casanova2';

--c) Write a predicate author_year(Person, Year) that is true if and only if Person published books in year Year;
select *
from author A
where A.classifier IN (select classifier from publishedin where yaer=2008)
AND A.firstname = 'henri2'
AND A.lastname = 'casanova2';

--d) Write a predicate new_book(Title) that is true if and only if there is a book named Title that is published in 2017 or later.
select *
from title A
where A.classifier IN (select classifier from publishedin where yaer >= 2017)
AND A.title='Parallel Algorithms3';

---e) Write a predicate coauthor(Person1, Person2) that is true if and only if Person1 and Person2 co-authored a book.
select * from (select *
from author A
where A.classifier = (select classifier from title where title='Parallel Algorithms1')
AND A.firstname = 'henri2'
AND A.lastname = 'casanova2'
) Q1
join
(select *
from author B
where B.classifier = (select classifier from title where title='Parallel Algorithms1')
AND B.firstname = 'arnaud2'
AND B.lastname = 'legrand2'
) Q2 on Q1.CLASSIFIER = Q2.CLASSIFIER;

/*-- Book 1
insert into title values ('QA76.642.C39', 'Parallel Algorithms');
insert into publishedBy values ('QA76.642.C39', 'CRC press');
insert into author values ( 'QA76.642.C39', 'henri', 'casanova');
insert into author values ( 'QA76.642.C39', 'arnaud', 'legrand');
insert into author values ('yves', 'robert', 'QA76.642.C39');
insert into publishedIn values ('QA76.642.C39', 2008);

-- Book 2
insert into title values ('QA76.642.C40', 'Mathematics');
insert into publishedBy values ('QA76.642.C40', 'CRC press');
insert into author values ('QA76.642.C40','henri1','casanova1');
insert into publishedIn values ('QA76.642.C40', 2009);

-- Book 3
insert into title values ('QA76.642.C41', 'Parallel Algorithms1');
insert into publishedBy values ('QA76.642.C41', 'CRC press1');
insert into author values ('QA76.642.C41','henri2','casanova2');
insert into author values ('QA76.642.C41','arnaud2','legrand2');
insert into publishedIn values ('QA76.642.C41', 2008);

-- Book 4
insert into title values ('QA76.642.C42', 'Parallel Algorithms2');
insert into publishedBy values ('QA76.642.C42', 'CRC press');
insert into author values ('QA76.642.C42','henri2','casanova2');
insert into author values ('QA76.642.C42','yves2','robert2');
insert into publishedIn values ('QA76.642.C42', 2010);

-- Book 5
insert into title values ('QA76.642.C43', 'Parallel Algorithms3');
insert into publishedBy values ('QA76.642.C43', 'CRC press');
insert into author values ('QA76.642.C43','henri','casanova');
insert into author values ('QA76.642.C43','arnaud','legrand');
insert into author values ('QA76.642.C43','yves','robert');
insert into publishedIn values ('QA76.642.C43', 2018);

*/
/*CREATE TABLE TITLE
(
CLASSIFIER VARCHAR2(20)
, TITLE VARCHAR2(100)
);

CREATE TABLE publishedBy
(
CLASSIFIER VARCHAR2(20)
, publisher VARCHAR2(100)
);

CREATE TABLE author
(
CLASSIFIER VARCHAR2(20)
, Firstname VARCHAR2(100)
, Lastname VARCHAR2(100)
);


CREATE TABLE publishedIn
(
CLASSIFIER VARCHAR2(20)
, yaer int
);

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote