This version of Learn Prolog Now! embeds SWI SH , SWI-Prolog for SHaring. The current version rewrites the Learn Prolog Now! HTML on the fly, recognising source code and example queries. It is not yet good at recognising the relations between source code fragments and queries. Also Learn Prolog Now! needs some updating to be more compatible with SWI-Prolog. All sources are on GitHub:
LearnPrologNow LPN SWISH Proxy SWISH

### 3.4 Practical Session

By now, you should feel more at home with writing and running basic Prolog programs. In this practical session we first suggest two series of keyboard exercises which will help you get familiar with recursive definitions in Prolog, and then give you some programming problems to solve.

First the keyboard exercises. As recursive programming is so fundamental to Prolog, it is important that you have a firm grasp of what it involves. In particular, it is important that you understand the process of variable instantiation when recursive definitions are used, and that you understand why the order of goals in rules can make the difference between termination and non-termination. So:

1. Load descend1.pl , turn on trace , and pose the query descend(anne,emily) . Count how many steps it takes Prolog to work out the answer (that is, how many times do you have to hit the return key). Now turn trace off and pose the query descend(X,Y) . How many answers are there?
2. Load descend2.pl . This is the variant of descend1.pl with the rule order reversed. Repeat the traces you have carried out for descend1.pl , and compare the results.
3. Load descend3.pl . This is the variant of descend2.pl in which the goal order within the recursive rule is switched, resulting in a left recursive rule. Because of this, even for such simple queries as descend(anne,bridget) , Prolog will not terminate. Step through an example, using trace , to confirm this.
4. Load descend4.pl . This is the variant of descend3.pl obtained by switching the rule order. So descend4.pl also contains a left recursive rule, and does not terminate on all input. But it does terminate on some input where descend3.pl does not. Which extra solutions does it find?

As we said in the text, goal ordering, not rule ordering is what is truly procedurally significant. But with non-terminating programs, rule ordering changes can have unexpected effects. Recall the successor program discussed in the text (let’s call it numeral1.pl ):

```numeral(0).
numeral(succ(X)) :- numeral(X).```

Let’s swap the order of the two clauses, and call the result numeral2.pl :

```numeral(succ(X)) :- numeral(X).
numeral(0).```

Clearly the declarative, or logical, content of this program is exactly the same as the earlier version. But what are the procedural differences, if any?

1. Create a file containing numeral2.pl , load it, and investigate what happens if we pose queries about specific numerals. For example, suppose we ask:
`?- numeral(succ(succ(succ(0)))).`

Do numeral1.pl and numeral2.pl behave in the same way on such input?

2. Second, look at what happens if we try to generate numerals, that is, suppose we pose the query
`?- numeral(X).`

Do the programs display identical behaviour?

Here are some programs for you to try your hand at.

1. Imagine that the following knowledge base describes a maze. The facts determine which points are connected, that is, from which points you can get to which other points in one step. Furthermore, imagine that all paths are one-way streets, so that you can only walk them in one direction. So, you can get from point 1 to point 2, but not the other way round.
```connected(1,2).
connected(3,4).
connected(5,6).
connected(7,8).
connected(9,10).
connected(12,13).
connected(13,14).
connected(15,16).
connected(17,18).
connected(19,20).
connected(4,1).
connected(6,3).
connected(4,7).
connected(6,11).
connected(14,9).
connected(11,15).
connected(16,12).
connected(14,17).
connected(16,19).```

Write a predicate path/2 that tells you from which points in the maze you can get to which other points when chaining together connections given in the above knowledge base. Can you get from point 5 to point 10? Which other point can you get to when starting at point 1? And which points can be reached from point 13?

2. We are given the following knowledge base of travel information:
```byCar(auckland,hamilton).
byCar(hamilton,raglan).
byCar(valmont,saarbruecken).
byCar(valmont,metz).

byTrain(metz,frankfurt).
byTrain(saarbruecken,frankfurt).
byTrain(metz,paris).
byTrain(saarbruecken,paris).

byPlane(frankfurt,bangkok).
byPlane(frankfurt,singapore).
byPlane(paris,losAngeles).
byPlane(bangkok,auckland).
byPlane(singapore,auckland).
byPlane(losAngeles,auckland).```

Write a predicate travel/2 which determines whether it is possible to travel from one place to another by chaining together car, train, and plane journeys. For example, your program should answer yes to the query travel(valmont,raglan) .

3. So, by using travel/2 to query the above database, you can find out that it is possible to go from Valmont to Raglan. If you are planning such a voyage, that’s already something useful to know, but you would probably prefer to have the precise route from Valmont to Raglan. Write a predicate travel/3 which tells you which route to take when travelling from one place to another. For example, the program should respond
```X = go(valmont,metz,
go(metz,paris,
go(paris,losAngeles)))```

to the query travel(valmont,losAngeles,X) .

4. Extend the predicate travel/3 so that it not only tells you the route to take to get from one place to another, but also how you have to travel. That is, the new program should let us know, for each stage of the voyage, whether we need to travel by car, train, or plane. © 2006-2012 Patrick Blackburn, Johan Bos, Kristina Striegnitz
{"ft_lang_label":"__label__en","ft_lang_prob":0.85263616,"math_prob":0.9172973,"size":5281,"snap":"2019-51-2020-05","text_gpt3_token_len":1268,"char_repetition_ratio":0.13397764,"word_repetition_ratio":0.017412934,"special_character_ratio":0.23442529,"punctuation_ratio":0.18239564,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9607444,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-28T14:52:41Z\",\"WARC-Record-ID\":\"<urn:uuid:9dbbb46e-b0de-430b-a55e-5e59b0e50ff8>\",\"Content-Length\":\"14666\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ec3fd5b1-f296-44f5-9c61-517150af1656>\",\"WARC-Concurrent-To\":\"<urn:uuid:c77aba11-0720-47f4-be69-cdfe9ad41e71>\",\"WARC-IP-Address\":\"130.37.193.11\",\"WARC-Target-URI\":\"http://lpn.swi-prolog.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse12\",\"WARC-Payload-Digest\":\"sha1:ONXRLF22V3BR2CLYVKOJKVX4A6MCHDA3\",\"WARC-Block-Digest\":\"sha1:V3SZ4ARFGYQW7LVST3YOLSLKQPUAS2MR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251778272.69_warc_CC-MAIN-20200128122813-20200128152813-00508.warc.gz\"}"}