$20
Unzip the directory A3_Problem2.zip and refer to the Tiny OOPL program given in tinyoopl.txt – a portion of this file is shown on the next page. It gives the outline of an object-oriented program consisting of classes, fields, and methods, but without the bodies of methods. The constructs of a TINY OOPL program can be encoded in three relations:
db_class(C1, C2) – meaning, class C1 extends class C2
db_field(C, F:T) – meaning, class C declares field F with type T
(where : is an infix binary constructor)
db_method(C, M:T1->T2) – class C declares method M with type T1->T2, where T2 is void when M is a void method (and -> is an infix binary constructor)
The file database.pl shows instances of these relations for the program in tinyoopl.txt, and a sample from this file reproduced on the next page. Using these relations, define the following Prolog predicates:
a. subclass(C1,C2): Given C2 as input, return in C1 every subclass of C2, one-by-one upon backtracking. And, given C1 as input, return in C2 every superclass of C1, one-byone upon backtracking. Note: the terms superclass and subclass refer not only to the immediate super/subclass, but also the super/subclasses that are obtained transitively.
b. recursive(C): Given a class C as input, return true/false indicating whether C is recursive, i.e., whether C or one of its subclasses declares a field of type C.
c. over_ridden(B,C,M,T): A method M of type T in class C is said to be over-ridden relative a class B if either B or some superclass B2 of B (where B2 is a subclass of C) also defines method M of type T.
d. inherits(C,L): Given a class C as input, return in L the list of all C2:M:T where M:T is a declared (but not an over-ridden) method of some superclass class C2 of C, and T is M’s declared type. The predicate should fail if C has no such methods.
e. cycle(C): Return true/false indicating whether there is a cycle through some method of class C. We say there is a cycle through a method of class C if the method has a parameter with type (class) C or the method has a parameter some other type (class) C2 that declares a method with a parameter of type (class) C; or, transitively, a class Ck that declares a method with a parameter of type (class) C.
Enter your definitions into the file analyzer.pl. Load into SWI Prolog the file problem2.pl – which includes database.pl and analyzer.pl. Proceed as follows.
% prolog problem2.pl % this might vary with platform: Mac, Linux, Windows
?- analyze.
… prints out banner message …
?- subclass(c, a).
?- why(subclass(c, a)). % ‘why’ provides an explanation … ?- cycle(a).
?- why(cycle(a)). % ‘why’ provides an explanation
Note: The why(G) predicate is in the included file explain.pl. Sample test queries and their output are given in the file transcript.