Prolog exercises

  1. (30 Aug 2016, deadline 06 Sept)
    Create your family database 'family.pl' and store into it the facts about your family using predicates mother, married, male and female.
    • mother(X,Y) is true if the mother of X is Y
    • married(X,Y) is true if the husband of X is Y
    • male(X) is true is X is male
    • female(X) is true if X is female
    Finish the program of your relatives by adding the rules for finding fathers, sisters, brothers, aunts, uncles, grandfathers and grandmothers:
    • father/2
    • sister/2, brother/2
    • aunt/2, uncle/2
    • grandfather/2, grandmother/2
    Test the programm and store in Moodle: Prolog exercise 1.

  2. (06 Sept, deadline 13 Sept)
    Define predicate type/2 that models typing of words on a shifted keyboard. Either hand can be in the standard position or shifted one position to the left or one position to the right. Positions of both hands are fixed at the beginning of typing. For convenience words are presented as minus-sequences of letters. Give all possible solutions. For example
       ?- type(l-o-g-i-c,X).
       X = l-o-g-i-c ;
       X = k-i-g-u-c ;
       X = ö-p-g-o-c ;
       X = l-o-f-i-x ;
       X = k-i-f-u-x ;
       X = ö-p-f-o-x ;
       X = l-o-h-i-v ;
       X = k-i-h-u-v ;
       X = ö-p-h-o-v ;
       false.
       
    Ideally the program works also backwards.
       ?- type(X,k-i-g-u-c).
       X = l-o-g-i-c ;
       
    Hint: Decide the direction of substraction (xfy – from left to right – or yfx – from right to left) and write the corresponding op-command at the beginning of your program.

  3. (13 Sept, deadline 20 Sept)
    Describe Prolog predicate flower(N) that draws a flower using ASCII symbols. The parameter N scales the drawing. For example
    ?- flower(1).
      ( )
    ( )@( )
      ( )
       |
      \|/
    true .
    ?- flower(2).
       (  )
    (  )@@(  )
    (  )@@(  )
       (  )
        ||
        ||
      \\||//
    true .
         
    Hint: ASCII flower
    gallery.
    Note: Use monotype fonts that have fixed width (system, terminal etc).

  4. (20 Sept, deadline 27 Sept)
    Write program similar_registration_numbers(A,B,Cs,N) that is true if two vehicle registration numbers A and B have N symbols in common. Take into consideration repetions of symbols. Output Cs as a list of coinciding symbols. For example
         ?- similar_registration_numbers('334MMM','333BMW',Cs,N).
         Cs = ['3', '3', 'M'],
         N = 3.
         ?- similar_registration_numbers('333BMW','334MMM',Cs,N).
         Cs = ['3', '3', 'M'],
         N = 3.
         ?- similar_registration_numbers('123ABC','345DEF',Cs,N).
         Cs = ['3'],
         N = 1.
         
    Hint: Use system predicate atom_chars/2 to transforms a word into the list of its characters.

  5. (27 Sept, deadline 04 Oct)
    Write Prolog predicate idcode/2 that uses difference lists to parse Estonian Personal Identification Code. For example
         ?- idcode([3,4,5,0,1,2,3,4,2,1,5],[]).
         true.
         ?- idcode([3,4,5,0,1,2,3,4,2,1,5,5],[]).
         false.
         ?- idcode([3,4,5,0,1,2,3,4,2,1,5,5],X).
         X = [5]. 
         
    Hint: See Lecture 4 slides 25–26.
    Hint:
    Personal Identification Code.

  6. (04 Oct, deadline 11 Oct)
    Describe predicate solve(X) that finds a solution to the following puzzle.

    In a company there are working Max, Nick, Oscar and Peter. There agencies are: engineer, boss, cashier and storeman (but not necessarily in that order). Find each agency, as it is known the following facts.

    1. Although the boss always loses, he never plays chess with anyone but the engineer.
    2. The storeman and the engineer play chess better than the cashier.
    3. Max an Nick live in the same house and often play chess together.
    4. Peter plays chess better than Max.
    5. The cashier lives in the same house with the boss, but no-one more from the company staff lives in this house.
           ?-solve(X).
           
    Hint: See Lecture 5 slides 16–23 (three friends).
    Hint: See Einstein puzzle (zebra puzzle).

  7. (11 Oct, deadline 18 Oct)
    Write a program equation/1 that solves a linear one variable equation. The equation consistst of one variable, two numbers, one equality and one operator. The operator is either addition, subtraction, multiplication or division. For example
           ?- equation(X + 2 = 5).
           X = 3.
           ?- equation(2 - 3.5 = Dif).
           Dif = -1.5.
           
    Hint: See Lecture 3 slides 9–10 (operators: type declarations, example).

  8. (18 Oct, deadline 25 Oct, extended 01 Nov)
    Let the syntax of propositional formulas be as following:
    • propositional variables: a, b, c, d ...
    • connectives: not, and, or, ->, <->
    • parenthesis: (, )
    Construct a program truthtable/3 that calculates a truth table of a propositional formula and saves it in html-format. Its first argument chooses the filling level (main – only main connective, connectives – all connectives, full – connectives and variables). The second parameter is a propositional formula and third parameter is the name of a html-file for saving the table. First and/or third parameter can be dropped (default values are 'main' and 'table.html'). For example
         ?-truthtable(connectives, a and b or not c, 'table.html').
         a b c | a and b or not c
         -------------------------
         t t t |    t     t  f
         t t f |    t     t  t
         ...
         ?-truthtable(a and b or not c).
         a b c | a and b or not c
         -------------------------
         t t t |          t   
         t t f |          t   
         ...
         
    Hint: Define logical connectives using command op/3 at the beginning of program.

    Note: You can improve the solution during one week (add connectives, calling parameters, css-style, modify the basic structures, html-layout etc).

  9. (01 Nov, deadline 08 Nov)
    Compose a program dr_google/0 that mimics the work of a doctor. The program uses symptoms to diagnose some well-known diseases and suggests a doctor who takes care of the disease. For example
         ?- dr_google.
         Dr. Google: Any problems.
         Patient: I have sore throat and cough.
         Dr. Google: Do you have also headache, muscle pains and high fever.
         Patient: Yes.
         Dr. Google: You have flu, please go to your GP and she will assign you a sick note.
         true.
         
    Hint: The program should contain lists of symptoms, diseases and doctors and also relations between the entities. The relations can be built as Prolog rules or tables of dependencies.

    Note: The interaction between the doctor and patient can be conducted in different ways — yes/no questions, multiple choise questions or answers in natural language. Please keep the dialogue as simple as possible.

    Note: If interested you can build a GUI using SWI-Prolog built-in XPCE, html or Javascript.

  10. (08 Nov, deadline 15 Nov)
    Solve a
    LP test (just to compare with the students of IT College).

  11. (15 Nov, deadline 22 Nov)
    Construct a procedure triple/2 that parses a natural language sentence in list form into a subject-predicate-object triple. Subject is a noun that is followed by a verb (i.e. predicate) and object is a noun that follows a verb. For example
         ?- triple([the,cat,chase,the,mouse],X).
         X = [cat,chase,mouse].
         
    Note: Try to write a definite clause grammar (see Lecture 8 slide 13).

    Note: Use WordNet dictionary (wn_s.pl, estwn_s.pl).

         :-[wn_s].
         
    Note: The sentences can be read from a preprocessed text file 'sentences.pl'. Preprocessing translates the sentences into Prolog readable terms sentence/1 and normalizes the words if needed.
         :-[sentences].
         ?- sentence(X), triple(X,Y).
         

  12. (22 Nov, deadline 29 Nov)
    Build a logic_engine/N that checks validity of certain logic arguments. Traditionally an argument consists of two premises and a conclusion (N=3). Its constituents can be presented as lists of words or logic terms (trees). For example, sentence 'All trees are green' may be formalized as a list [all,trees,are,green] or a Prolog term all(trees,green).
    1. Find (choose) some valid arguments and some invalid arguments.
    2. The logic engine gives a conclusion for valid arguments and fails on invalid arguments. For example
         ?- logic_engine([there,are,trees,in,the,park],[all,trees,are,green],X).
         X = [trees,in,the,park,are,green].
         ?- logic_engine([there,are,birds,in,the,park],[all,trees,are,green],X).
         false.
         
  13. (29 Nov, deadline 06 Dec)
    Write a Prolog procedure search/2 that solves the water jar problem in artificial intelligence: There are two jars, one has capacity 4 litres and the other 3 litres. At the beginning both jars are empty. We need 2 litres in the bigger jar.

    Use depth-first and breadth-first search strategies to find all solutions to the problem. For example

         ?- search(depth-first,Path).
         Path = [(2,0),...,(4,3),(4,0),(0,0)] ;
         ...
         ?- search(breadth-first,Path).
         
    Hint: See Lecture 5 slides 8–15 (monkey and banana). Avoid cycles.

  14. (06 Dec, deadline 13 Dec)
    Compose a program cs_thesis(Text,Thesis) that finds from
    Institute's Graduation Theses Registry a Thesis containing a given Text on author or title fields. For example
         ?- cs_thesis('html',Thesis).
         Thesis = ['Tähepõld', 'Tanel', 'Context-Aware Mobile Games Using 
                    Android, Arduino and HTML5', '2012'] ;
         Thesis = ['Tõnisson', 'Kaarel', 'Mechanism for Change Detection 
                    in HTML Web Pages as XML Documents', '2015'] ;
         false.
         ?- cs_thesis('tõn',Thesis).
         Thesis = ['Tasa', 'Tõnis', 'Re-using public RNA-Seq data', '2015'] ;
         Thesis = ['Jaarma', 'Tõnu', 'Review of object oriented parallel 
                    programming framework Charm++', '2012'] .
         
    Hint: Use SWI-Prolog libraries http_open and xpath.

  15. (13 Dec, deadline 20 Dec)
    Realize in Prolog your favorite game. For example mastermind, chess, checkers, minesweeper etc. You are free in formalizing the requirements for the game. Preferable is a game between a human and a computer. Opponents could also be two humans or even two computers.

    Hint: See Sterling & Shapiro "The art of Prolog", Bratko "Prolog programming for AI", Jaak Henno "Loogiline programmeerimine".