Suggested answers

advertisement
Suggested answers
This document contains suggested answers to selected questions from the exam in TDDC65
Artificial Intelligence and Lisp from 2009-04-17.
Question 1: Lisp programming (9p)
1a (2p) What will the Lisp interpreter return if we enter the following four expressions
(exactly as stated) with labels 1-4 in the example?
CL-USER(1): (setq animals (list 'dog (cons 'cat '(mouse))))
(DOG (CAT MOUSE))
CL-USER(2): (cons 'bob (car (second animals)))
(BOB . CAT)
CL-USER(3): (funcall #'length (append animals '()))
2
CL-USER(4): (list '(* 5 7) (* 5 7))
((* 5 7) 35)
1b (3p) Write a Lisp function sum-intervals that takes a list of numeric intervals. The
function should calculate the sum of all those intervals. Assume that each interval is a list of
two numbers where the first one is smaller than the second one.
(defun sum-intervals (pair-list)
(if (endp pair-list)
0
(+ (- (second (first pair-list))
(first (first pair-list)))
1
(sum-intervals (rest pair-list)))))
1c (4p) Write a more generic Lisp function do-intervals that can process the same kind of
numeric intervals as in the previous question. However, what the function should do with the
intervals is to be specified by the arguments to do-intervals. The second argument should
be a function that takes an interval and the current value. The third argument should be the
starting value. First of all, write the generic function do-intervals. Secondly, replace #’+ in
the call above with a more suitable function or lambda expression that works.
(defun do-intervals (pair-list fn start)
(if (endp pair-list)
start
(funcall fn (first pair-list)
(do-intervals (rest pair-list) fn start))))
CL-USER(12): (do-intervals '((2 4) (7 10))
#'(lambda (pair sum)
(+ (1+ (- (second pair) (first pair))) sum))
0)
7
Question 2: Logic (12p)
2a (2p) Prove that the following formula is valid using a truth table:
P ∧ (¬Q ∨ R) ⇒ ¬(¬P ∧ Q) ∨ R
P
0
0
0
0
1
1
1
1
Q
0
0
1
1
0
0
1
1
R
0
1
0
1
0
1
0
1
¬Q ∨ R
1
1
0
1
1
1
0
1
P ∧ (¬Q ∨ R)
0
0
0
0
1
1
0
1
(¬P ∧ Q)
0
0
1
1
0
0
0
0
¬(¬P ∧ Q) ∨ R
1
1
0
1
1
1
1
1
LHS ⇒ RHS
1
1
1
1
1
1
1
1
2b (2p) Prove that the same formula is valid by rewriting it to a known tautology (i.e. a much
simpler formula that is trivially always true).
P ∧ (¬Q ∨ R) ⇒ ¬(¬P ∧ Q) ∨ R
¬ (P ∧ (¬Q ∨ R)) ∨ ¬(¬P ∧ Q) ∨ R
implication elimination
¬P ∨ ¬ (¬Q ∨ R) ∨ ¬¬P ∨ ¬Q ∨ R
De Morgan
¬P ∨ ¬ (¬Q ∨ R) ∨ P ∨ ¬Q ∨ R
double negation
At this point we can observe that the formula is equivalent to ¬P ∨ P ∨ A where A is the
rest of the formula, but since the first part is a trivial tautology the formula is always
true, i.e. valid.
2c (2p) The four sentences can be formalized like this:
¬C ⇒ T ∨ I
G ∧ ¬C ⇒ ¬I
C⇒G
T
2d (3p) In order to prove that the conclusion T follows from the three first sentences, we
negate the conclusion, convert all formulas to CNF and apply the resolution rule until we get a
contradiction.
Formula in CNF
C∨T∨I
¬G ∨ C ∨ ¬I
¬C ∨ G
¬T
Original formula
¬C ⇒ T ∨ I
G ∧ ¬C ⇒ ¬I
C⇒G
¬T
Here is the resolution proof:
1.
2.
3.
4.
5.
6.
7.
C∨T∨I
¬G ∨ C ∨ ¬I
¬C ∨ G
¬T
C ∨ T ∨ ¬G
T
⊥
1+2
3+5
4+6
2e (3p) See the start of chapter 7 in the course book. The Wumpus world might be a good
example from the book to illustrate the merits of predicate logic.
Download