Download APM 2001/2002 : Answers / Marking Scheme 1. (a) (i) Yes. P

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Algorithm characterizations wikipedia , lookup

Transcript
APM 2001/2002 : Answers / Marking Scheme
1.
(a)
(i)
Yes. P = description(csf, cm075).
[2]
(ii)
Yes. X = apm, Y = cm033.
[2]
(iii) No. cm026 \= cm117.
[2]
(iv) No. staff \= room.
[2]
(v)
No. Different arities.
[2]
(b)
son(sean, Who)
|
(1)| X1 = sean
| Y1 = Who
parent(Who, sean), male(sean)
|
C1
|
+------------------+------------------+
|
|
|
(2)| james
= Who (3)| james = Who
(9)| thomas
= Who
| thomas \= sean
| sean = sean
| elizabeth \= sean
|
|
|
no
male(sean)
no
|
Back to C1
C2
FAIL
|
+------------------+------------------+
|
|
|
(4)| james \= sean (5)| thomas \= sean
|
|
|
[I]
no
no
Back to C2
Back to C2
[I]
|
+------------------+------------------+
|
|
|
(6)| sean = sean
(7)| william = sean(8)| david \= sean
|
|
|
yes
no
no
Who = james
Back to C2
Back to C2
;
Back to C1
Back to C2
[10]
(c)
male_heir(A, B) :- son(A,B).
% male_heir 1
male_heir(A, B) :- son(Z, B),
male_heir(A, Z). % male_heir 2
[5]
2.
(a)
•
•
•
(b)
program(input, output)
The program is a relation which holds between the input and the output.
The world view of a relational language is based upon the theory of
mathematical logic - HORN CLAUSE MODEL.
No notion of a state which can be modified.
There are no statements.
No notion of memory so no { imperative style } variables.
[5]
path(H, T, C) :move(H, T, C).
% path 1
path(H, T, C) :move(H, I, Ca),
path(I, T, Cb),
C is Ca + Cb.
% path 2
(c) Abbreviations : lon = london, new = newcastle, car =
carlisle, bir = birmingham.
path(lon, car, Cost)
|
C1
|
+--------------------+--------------------+
|
|
(1)| H1 = lon
[I]
| T1 = car
| C1 = Cost
move(lon, car, Cost)
|
C2
|
+--------------------+--------------------+
|
|
|
(2)| lon = lon
(3)| new \= lon
(4)| lon = lon
| new \= car
| car = car
| bir \= car
| 160 = Cost
| 60
= Cost
| 40
= Cost
no
no
no
Back to C2
Back to C2
Back to C2
2
[I]
|
(5)| H5 = lon
| T5 = car
| C5 = Cost
move(lon, I5, Ca5), path(I5, car, Cb5),
|
Cost is Ca5 + Cb5
C3
|
(6)| lon = lon
| new = I5
| 160 = Ca5
path(new, car, Cb5), Cost is 160 + Cb5
|
C4
|
(7)| H7 = new
| T7 = car
| C7 = Ca5
move(new, car, Cb5), Cost is 160 + Cb5
|
C5
|
+---------+----------+
|
|
(8)| lon \= new
(9)| new = new
| new \= car
| car = car
| 160 = Cb5
| 60
= Cb5
no
Cost is 160 + 60
Back to C5
|
| Cost = 220
yes
Cost = 220
[10]
3.
(a)
insert(X, Xs, [X | Xs]).
insert(Xa, [Xb | Xs], [Xb | Zs]) :insert(Xa, Xs, Zs).
[5]
(b)
permute([], []).
permute([X | Xs], Zs) :permute(Xs, Ys),
insert(X, Ys, Zs).
[5]
3
(c)
findall(L, permute([1,2,3,4,5], L), Ls)
[5]
(d)
minus(L, [], L).
minus(L, [X | Xs], Ys) :delete(L, X, Ys1),
minus(Ys1, Xs, Ys).
delete([], _, []).
delete([X | Xs], X, Ys) :delete(Xs, X, Ys).
delete([Y | Ys], X, [Y | L]) :delete(Ys, X, L),
X \= Y.
[10]
4.
(a) What are the essential differences between the Von Neumann machine,
Lambda calculus and Predicate calculus models of computation?
•
•
•
Von Neumann model is based on computation by changing state.
Lambda calculus model is based on computation by function application.
Predicate calculus model is based on computation by deduction from facts
and rules.
Why are there no programming languages based on the Turing machine model?
•
I don’t know. The best I can come up with is that (a) the universe of the
Turing machine model does not correspond to the universe of the
problems we want to solve; and (b) it's hard to build a Turing machine in
silicon. Of course, (a) more or less also applies to the Von Neumann
model IMHO.
Why are most programming languages within the imperative paradigm?
•
Because (a) it's easy to build a Von Neumann machine in silicon (or in the
early days, using valves and relays); and (b) at a superficial level most
problems seem to fit with the Von Neumann model because the real
universe is imperative.
[5]
(b)
What is Turing machine equivalence?
4
•
•
Also known as Church’s thesis after Alonzo Church who was an
American mathematician and famous researcher in the field of
computability (the study of what can be computed).
Informally, Church’s thesis can be expressed as follows :
If you can write a program to solve problem X in a language from
language paradigm P then you can also write a program to solve problem
X in languages from all the other language paradigms.
Why is it important?
•
•
•
All the language paradigms have equivalent power. This is a fairly
stunning concept : anything you can do in C++ you can do in Haskell or
PROLOG (or any other programming language).
Of course, this begs the question of why we don’t just stick to using the
imperative paradigm since imperative languages are the most efficient on
Von Neumann machines. However, remember Baruch’s observation. Just
because you can solve a problem in a particular paradigm doesn’t mean
that that’s the best way to do it.
Note that Church’s thesis is so called because it has never been formally
proven - if it had been it would be called Church’s theory.
Proving Church’s thesis would win you the Turing award but
unfortunately it’s impossible to do it. Essentially the argument is that there
may be a mathematical model of computation (i.e. programming
paradigm) which no-one has yet thought of which is more powerful that a
Turing machine. It is possible that neural nets are just that paradigm but
no one has yet produced formal model of how a neural net works - so far
as I know - so the question is still open.
How about trying to disprove it? Obviously all this requires is to invent a
mathematical model of computation more powerful than a Turing
machine (see above). A nice trick if you can do it (and well worth a
Turing award) but no-one’s managed it yet. There are good (but totally
informal) arguments which suggest that this is impossible.
An alternative method would be to write a program in (for example)
PROLOG which solves a problem which cannot be solved by a program
written in (for example) C or Haskell. This would also win you the Turing
award but is obviously impossible since it is possible to write an
interpreter for any (Turing machine equivalent language) in another
(Turing machine equivalent) language.
[7]
(c) What are the major differences between natural (i.e. human) languages
and programming languages?
•
•
•
•
Natural languages evolved. Programming languages were defined.
All languages have a universe of discourse : the concepts they can
express.
Natural language : universe of discourse is determined largely by the
universe speakers inhabit.
Programming language : universe of discourse is determined by the
language.
5
•
•
•
•
•
•
Soundbyte : for natural languages, universe defines language; for
programming languages, language defines universe.
What are the basic requirements for a programming language?
Programming language must be universal, natural, implementable and
efficient.
Universal : can express the solution to any problem which can be solved
by a computer (Turing machine equivalence).
Natural : suited to the problem you are trying to solve. Basically, it must
have the same universe as the problem.
Implementable : must be possible to run it on a computer. Specification
languages (e.g. Z, VDL, CSP etc.) are not executable and are therefore not
useful programming languages.
Efficient : must run sufficiently quickly to be practicable and must not use
too much memory.
Explain the meaning of the terms idiom and paradigm when applied to natural
and programming languages. Give examples of idioms and paradigms in both
types of language.
•
•
•
•
•
•
•
•
Idioms are commonly used expressions (ways of saying things). Overused
idioms tend to be called clichés.
Natural language idioms are standard ways of expressing common ideas;
archetypal idioms are figures of speech and proverbs. For example, in
English (the only natural language I know well enough to comment on)
the following are uses of idioms :
“I think the functional paradigm is superior to the other paradigms. On the
other hand, functional languages are poor for interactive, state-based
programming.”
“I want to be tough on C++ and tough on the causes of C++.”
“He’s a few sandwiches short of a picnic.”
“The trouble with bureaucrats is that they know the name of everything
and the value of nothing.”
“I’m a free thinker, you’re eccentric, he’s as mad as a hatter.”
Programming language idioms are standard ways of writing code to solve
common problems. For example, in imperative languages the standard
array processing idiom is :
for (i=0; i < n ; i++) { ... }
In functional languages the standard list processing idiom is
f []
= []
f (x : xs) = g x : f xs
In relational languages the standard transitive relation processing idiom is
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
A good way of determining how well you know a particular language is to
see how many idioms you can write down. Just in passing, the test of
6
•
•
•
fluency in a programming language is whether you can write a compiler
for that language in that language. For example, can you write a C++
compiler in C++? If you can’t then you are not fluent in C++.
In general, a language has an idiom for every common programming
problem. For example, there is a standard way to read a set of numbers
into an array in C. There is also a standard way to do this in Haskell or
PROLOG. Of course, the idiom is very different in each language since
the languages come from different paradigms. The most obvious symptom
of the difference between the paradigms is that idioms in imperative
languages tend to be iterative while idioms in functional and relational
languages tend to be recursive. For example, the obvious “quick and
dirty” sorting idiom for an imperative language is Bubblesort, for a
functional or relational language its Quicksort.
Paradigm is a pretentious word for type. When applied to languages it
refers to groups of languages with more or less identical views of their
universe of discourse.
Paradigms in natural languages are the major human language groupings
(e.g. Indo-European, Polynesian, Native American, Oriental etc.).
Paradigms in programming languages are the major programming
language groupings (i.e. imperative, relational and functional).
[8]
(d) ML is an impure functional language; PROLOG is an impure relational
language. Why are they impure?
•
•
ML has the ref construct which allows assignment statements.
PROLOG has assert and retract (self-modifying code); nonbacktracking I/O statements and assignment statements.
Are there any impure imperative languages? If not, why not?
•
•
Yes. ML and PROLOG are both impure imperative languages.
It’s simply a matter of terminology. Most languages are imperative and
most impure functional and relational languages are impure because they
include imperative features. The phrases ‘impure relational language’ and
‘impure functional language’ convey more information than the phrase
‘impure imperative language’.
[5]
7