Lecture 12

Today:

    1. Prolog.

    2. Reading questions from Lecture 11.

    3. Homework 6 due.

Next time:

    1. Quiz.

    2. Reading questions below.

    3. Start Homework 7.

Prolog

1) Install gprolog: sudo apt-get install gprolog

a) If you use emacs, you might want to sudo apt-get install prolog-el and then add these lines to ~/.emacs

(autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)

(setq auto-mode-alist (append '(("\\.pl$" . prolog-mode))

auto-mode-alist))

For windows:

2) Read this page http://www.dcs.warwick.ac.uk/~mju/CS205/prolog1.html

3) And then download harry1.pl.

% Here are the facts:

parents(lily_evans, james_potter, harry_potter).

parents(petunia_evans, vernon_dursley, dudley_dursley).

parents(molly_prewett, arthur_weasley, ronald_weasley).

parents(molly_prewett, arthur_weasley, ginevra_weasley).

parents(cedrella_black, septimus_weasley, arthur_weasley).

In the usual example, you specify two parents with two different rules, but (a) that's too much typing, and (b) I wanted to experiment:

% And here's the first set of rules:

mother(X,Y) :- parents(X,_,Y).

father(X,Y) :- parents(_,X,Y).

The underscore is an anonymous variable, loosly translated "anyone"

A comma between subgoals is AND.

A semi-colon between subgoals is OR.

parent(X,Y) :- mother(X,Y); father(X,Y).

You can have more than one anonymous variable; they don't have to match.

mother(X) :- parents(X,_,_).

father(X) :- parents(_,X,_).

You can combine rules and facts

male(X) :- is_father(X).

male(harry_potter).

male(dudley_dursley).

female(X) :- is_mother(X).

female(ginevra_weasley).

4) Start gprolog and "consult" this file:

$ gprolog

GNU Prolog 1.2.18

By Daniel Diaz

Copyright (C) 1999-2004 Daniel Diaz

| ?- [harry1].

compiling harry1.pl for byte code...

harry1.pl compiled, 24 lines read - 2569 bytes written, 18 ms

yes

| ?-

5) Try out the following queries:

a) True/False

| ?- female(lily_evans).

yes

b) One reply.

| ?- mother(lily_evans, X).

X = harry_potter

yes

c) Multiple replies.

| ?- parents(molly_prewett, arthur_weasley, X).

X = ronald_weasley ? a

X = ginevra_weasley

yes

After the first reply, you can press return to quit or 'a' to get 'all' replies.

d) No replies

| ?- parent(harry_potter, X).

no

6) Answer the following questions by formulating appropriate queries:

a) Is Harry Potter male?

b) Is Harry Potter a father?

c) Who are the children of Petunia Evans?

d) Who is Harry Potter's father?

e) Who are Molly Prewett's children?

7) Add a rule to define grand_parent(X,Y) and use it to find the grandparents of Ronald Weasley.

8) Add a rule to define ancestor(X,Y) and use it to find the ancestors of Harry Potter.

Reading questions

Sipser, pages 137-147

1) What are the differences between a Turing machine and a finite automaton?

2) If you were using a Python dictionary to represent the transition function, delta, for a Turing machine, what type would the keys and values be?

3) What is a configuration of a Turing machine?

4) What does it mean to say that one configuration yields another?

5) What is the difference between Turing-recognizable and Turing-decidable?

6) At the bottom of page 147, Sipser claims that languages A, B, C, and E are decidable. What is the basis of this claim?

7) What happened to language D?

8) Do exercise 3.1 on page 159, at least until you get sick of it.