Simply Logical lab exercises chapter 1

Log on to a Unix server and start up SICStus Prolog with the command sicstus. SICStus starts up as follows:
   SICStus 3.8.4 (sparc-solaris-5.7): Mon Jun 12 18:41:59 MET DST 2000
   Licensed to acrc.bristol.ac.uk
   | ?-
?- is the Prolog prompt at which you can type a query, ended by a period. Prolog programs should be stored in a file and then read into the Prolog interpreter, by means of the query ?-consult(file). (which can be abbreviated ?-[file].). The default extension of a SICStus Prolog file is .pl, so the query ?-consult(file). will either load file or file.pl. If your filename contains non-alphanumeric characters you should use single quotes (e.g. ?-consult('file.txt').). The Prolog file to be used for this chapter's lab exercises is labs1.pl. Copy it to your own directory, then load it into Prolog:
   | ?- [labs1].
   {consulting /home/public/www/HTML/Teaching/Resources/COMS30106/labs/labs1.pl...}
   {Warning: [L] - singleton variables in nearby/2 in lines 15-18}
   {Warning: [L] - singleton variables in reachable/3 in lines 19-22}
   {Warning: [L] - singleton variables in reachable/3 in lines 22-23}
   {/home/public/www/HTML/Teaching/Resources/COMS30106/labs/labs1.pl consulted, 10 msec 2448 bytes}
Don't worry about the warnings, they concern don't-care variables that occur only once in a clause (sometimes they indicate mis-spelled variable names, however).


  1. The predicate reachable returns a list of intermediate stations in its third argument (see p.14 of the book). Extend it with a fourth argument returning the list of lines taken on this route, e.g.
       | ?- reachable(bond_street,piccadilly_circus,Stations,Lines).
       
       Stations = [oxford_circus],
       Lines = [central,bakerloo] ? ;
       
       Stations = [green_park],
       Lines = [jubilee,piccadilly] ? ;
       
       Stations = [green_park,oxford_circus],
       Lines = [jubilee,victoria,bakerloo] ? ;
       
       no
    
    Note that a Prolog program is correct only if all the answers it returns are correct, not only the first one! As shown above you can ask for the next answer by pressing ; followed by return.

    NB. Don't worry about the duplicate lines that sometimes show up in the list of lines -- we will see later how to deal with them.


  2. Translate the following questions into Prolog: and record the answers.


  3. The predicate nearby should be symmetric but isn't -- i.e., we have nearby(bond_street,tottenham_court_road) but not nearby(tottenham_court_road,bond_street). The same holds for the predicate connected. Fix this, without duplicating the information in the current definition of connected.

    This introduces another problem with nearby -- which one?


  4. Write a predicate stations that lists the stations on a particular line:
       | ?- stations(Line,Stations).
       
       Line = victoria,
       Stations = [green_park,oxford_circus] ? ;
       
       Line = central,
       Stations = [bond_street,oxford_circus,tottenham_court_road] ? ;
       
       Line = jubilee,
       Stations = [bond_street,green_park,charing_cross] ? ;
       
       Line = piccadilly,
       Stations = [green_park,piccadilly_circus,leicester_square] ? ;
       
       Line = bakerloo,
       Stations = [oxford_circus,piccadilly_circus,charing_cross] ? ;
       
       Line = northern,
       Stations = [tottenham_court_road,leicester_square,charing_cross] ? ;
       
       no
    
    NB. You'll have to treat the Victoria line separately, as it has only 2 stations.

    Can you do something similar for finding all lines a particular station is on? Why not?


  5. Change the representation of stations and lines as follows:
       line(central,[bond_street,oxford_circus,tottenham_court_road]).
       line(jubilee,[bond_street,green_park,charing_cross]).
       line(piccadilly,[green_park,piccadilly_circus,leicester_square]).
       line(victoria,[green_park,oxford_circus]).
       line(bakerloo,[oxford_circus,piccadilly_circus,charing_cross]).
       line(northern,[tottenham_court_road,leicester_square,charing_cross]).
    
    and rewrite the definition of connected using this representation.


Back / Peter Flach