===== p1.pl =====
/* From: http://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpages/ 
   1. Prolog consists of rules and facts 
   2. A program is executed by providing a query that prolog will try to 
      prove against the set of rules and facts 
   3. swipl to execute prolog interpreter
   4. ?-  prompt
   5. halt. to exit (notice period (full stop)).
   6. To load to database ['file.pl']
   7. If fact does not exist in database you can use assert
      (assert(like(x,y))
   8. facts 
      a. Begin with lowercase and end with full stop (.)
      b. Can have letters, numbers, _ 
*/


cold.
testudo_rocks.


===== p2.pl =====
/*
 1. facts with arguments: relation(arg1, arg2, ..., argn).  
 2. relation names must start with lowercase.
 */

likes(minion,bananas).
eats(alf,cats).
eats(mary,cheese).
eats(mary,bread).
age(peter,34).

===== p3.pl =====
/*
 1. Given likes(minion,bananas) how can we ask what minion likes?
    Would likes(minion,food) work?  No.  
 2. We need variables. Variables start with a uppercase.  
 3. Unification - Process of matching items with variables.
 4. Examples: Y, ThE_Value
 5. Let's try: likes(minion,X). likes(X,bananas). likes(X,Y).
 */

likes(minion,bananas).

===== p4.pl =====
/* 
 * Example about classes info.
 * Let's try: course(cmsc70,Teacher,Time). course(X,Y,am). 
 */

course(cmsc80, john, am).
course(cmsc70, mary, pm).
course(cmsc70, kelly, pm).

===== p5.pl =====
/*
 * 1. So far: facts and how to query them.
 * 2. Rules -> allow us to make conditional statements.
      Format: Head :- Body
 * 3. Rules have clauses that provide different choices about
      how to perform inference.
   4. Below rule can be read as: "For a given X, X is mortal if X is human" or 
      "To prove goal that X is mortal, prove subgoal that X is human"
   5. Let's try: mortal(napoleon). mortal(X).
   6. trace.  Let's you step through goal's execution
   7. notrace. to turn it off
 */

mortal(X) :- human(X).
human(napoleon).

===== p5_1_father_mother.pl =====
/* Notice that pressing tap will give you multiple */
/* answers.  Try son(X,Y).                         */

female(alice).
male(bob).
male(charlie).
father(bob, charlie).
mother(alice, charlie).
% "X is a son of Y"
son(X, Y) :- father(Y, X), male(X).
son(X, Y) :- mother(Y, X), male(X).


===== p6.pl =====
/*
 * We can have alternatives by provided several clauses to a predicate.
 * enjoyable(bike_ride). processed by first checking whether bike_ride is
 * free a query that fails. Therefore first clause of enjoyable will fail.
 * We try second clause.   
 */

enjoyable(X) :- free(X), onweekends(X).
enjoyable(X) :- lowcost(X), onweekends(X).
enjoyable(playing_tetris).

free(park).
onweekends(park).
onweekends(bike_ride).
lowcost(bike_ride).

===== p7.pl =====
/*
 * Backtracking: If a goal fails, prolog goes back to its last choice
 * and looks for an alternative. In example below, we backtrack after
 * has_money(mary) fails.
 */

travel(X) :- on_vacation(X), has_money(X).

on_vacation(mary).
on_vacation(peter).
has_money(peter).

    


===== p7_1.pl =====
/* Recursion #1 */

is_bigger(X,Y) :- bigger(X,Y).
is_bigger(X,Y) :- bigger(X,Z), is_bigger(Z,Y).

bigger(horse,duck).
bigger(duck,fly).

===== p7_2.pl =====
/* Recursion #2 */

gossip_reach(boss).

gossip_reach(Person):- 
       tells(Person,NextPerson),
       gossip_reach(NextPerson).


tells(rose,peter).
tells(peter,john).
tells(john,boss).
tells(tom,sarah).
tells(sarah,kyle).

===== p8.pl =====
/*
 * Lists: start and end with square bracket and items separated by commas.
 * To split list use |: 
 * Example: [cat,dog,bird] = [H|T].
 * Let's try:  data([cat,dog,bird], X, Y). data([cat], X, Y).  data([], X, Y). 
 */

data([H|T], H, T).



===== p9.pl =====
/*
 * Another tracing example.
 * Let's try: my_last(X, [1,2,3]).
 */

my_last(X, [X]).
my_last(X, [_|T]) :- my_last(X, T).

