CSCI 162 Spring 2018: Prolog Whale Geneology Lab




IO in Prolog

Type this in Prolog:

?- write(hello).

Now try this:
?- write(hello)

Now try this:
?- write(Hello).

Now try this:
?- write(hello world).

Now try this:
?- write('Hello').

Now try this:
?- write("hello world").
?- halt.


Whale geneology

For this lab, you will use the data in family.pl to test your prolog programs. You can get it using the following command:
cp ~gpruesse/public_html/teaching/162/labs/prologfamily/family.pl .
The data is for whale families; we will assume that all whales can be categorized as male or female. You will be able to load the file's facts and predicates into Prolog by typing the name of the file (minus the .pl extension) in square brackets on the Prolog query line, as follows:
?- [family].

  1. Who are chester's parents? Use "listing" to get all the facts and predicates that are loaded into prolog, and make the appropriate query. Do spot or ned or stumpy have any children?

    You are to write the following predicates (structures) into your family.pl file. Add the new structures to the end of the file. At the end of the file as it is given to you is an example structure, coparents, which you can use as a model for your own structures.

  2. sibling(X,Y) % X and Y are siblings, either full- or half-siblings.
    Ensure that you do not find that ned is his own sibling, by requiring that X does not equal Y: X\==Y

  3. fullsib(X,Y) % X and Y have two distinct parents in common. You can use \== to determine that two atoms are not the same; see the example "coparents" provided in the file.

  4. brother(X,B)   
    
    /* Can be used to answer the following questions: * if YOU instantiate X, and let Prolog instantiate B, * then you can find the first (second, third...) brother of your X. * If you instantiate B, and let Prolog instantiate X, * then you can find all those entities who have B as a brother. * If you don't instantiate either X or B, * then you can find out (one or more) pairs of entities where * the second is a brother of the first. */

  5. sister(X,S)

  6. grandparent(X,G).

  7. cousin(X,C).  % Neither self nor sibling is a cousin.
    
    You can use the operator `not(P)', which will be satisified only if P is not, or the test X==Y (for equality) or X\==Y (for not equal).
    male(X):- not female(X).
    or
    male(X):- not(female(X)).
    

  8. ancestor(X,Y).  
    /* An ancestor of X is Y.
    * I am my own ancestor, and every ancestor of my parent is 
    * also my ancestor; nobody else is my ancestor.
    */
    
    For ancestor, you will need multiple clauses -- they are "OR"ed together, or you can regard them as "If" conditions. For example, one way to write a recursive fibonacci calculator is the following:
    fib(1,1). 
    fib(2,1). %the second fibonacci number is 1.
    fib(N,Fn):- N1 is N-1, fib(N1,Fn1), 
                N2 is N1-1, fib(N2,Fn2),
                Fn =  Fn1+Fn2.
    

  9. Construct a structure that is satisfied if a person has three distinct grandchildren. Use `;' to determine all the whales in the database who have three distinct grandchildren. Show the instructor in the lab the execution in Prolog that names them all.

  10. Suppose whales live in a matrilineal society, where the "family name" of the whale is the same as the family name of the mother. When will two whales have the same last name? Build a structure:
    fname(X,Y)  % X and Y have the same family name.
    
    

    [Hint: you may want to define predicates
    hasmother(X), nomother(X), topmother(X,G).]