Académique Documents
Professionnel Documents
Culture Documents
247/468
Contents
Arithmetic
Lists
Programs
248/468
Axioms
Thus far, we have used symbols for functions, relations, etc., without
a fixed meaning. An interpretation can specify them arbitrarily.
Often, however, we want to use one or more symbols in a specific
way. The usual approach to this is to use axioms.
Definition: An axiom is a formula that is assumed as a
premise in any proof. An axiom schema is a set of axioms,
defined by a pattern or rule.
Axioms often behave like additional inference rules.
249/468
EQ2: 1 2 1 = 2 [1 / z] [2 / z] is an axiom,
for any choice of formula and variable z.
We shall keep these, and add new ones.
Axiom EQ1
EQ1 + e
e[t]: k
and similarly for other axioms.
250/468
Natural Numbers
Fix the domain as N, the natural numbers. Interpret the constant
symbol 0 as zero and the unary function symbol s as successor.
Thus each number in N has a term: 0, s(0), s(s(0)), s(s(s(0))), . . . .
Zero and successor satisfy the following axioms.
PA1: s() 6= 0.
Zero is not a successor.
PA2: y s() = s(y) = y .
Nothing has two predecessors.
Arithmetic
251/468
Arithmetic
252/468
253/468
Arithmetic
254/468
y(0 + y = y + 0)
y( + y = y + ) y(s() + y = y + s()) .
Arithmetic
255/468
Arithmetic
256/468
Arithmetic
256/468
Lemma, continued
Target y (0 + y = y + 0) (0 + s(y) = s(y) + 0) :
PA4
assumption + EQsubs(s())
PA3 .
= s(y + 0)
= s(y ) + 0
Arithmetic
257/468
0+0=0+0
EQ1 + e
2.
y fresh
3.
0+y=y+0
Assumption
4.
0 + s(y) = s(0 + y)
PA4 + e
5.
s(0 + y) = s(y + 0)
EQsubs(s()): 3
6.
y+0=y
PA3 + e
7.
s(y + 0) = s(y)
EQsubs(s()): 6
8.
s(y) + 0 = s(y)
PA3 + e
9.
0 + s(y) = s(y) + 0
EQtrans(3): 4, 5, 7, 8
10.
0 + y = y + 0 0 + s(y) = s(y) + 0 i: 39
11.
y 0 + y = y + 0
0 + s(y) = s(y) + 0
i: 10
12.
y(0 + y = y + 0)
PA7 + e + e (2):
1, 11
Arithmetic
258/468
= s(z + s())
0
= s(s(z + ))
Arithmetic
PA4
assumption + EQsubs(s())
PA4 + EQsubs(s()) .
259/468
PA4
premise + EQsubs(s())
PA4 + EQsubs(s()) .
= s( + s(z ))
= s(s( + z ))
Arithmetic
260/468
1.
y + y = y +
Premise
2.
s() + 0 = 0 + s()
Lemma, p. 256, + e
3.
+ z = z + , z fresh
e: 1
4.
s() + z = z + s()
Assumption
5.
+ s(z) = s(z) +
e: 1
6.
10.
15.
EQtrans(6): 814
16.
s() + z = z + s()
s() + s(z) = s(z) + s()
z (s() + z = z + s())
(s() + s(z) = s(z) + s())
i: 415
y s() + y = y + s()
PA7 + e + e (2): 2, 17
17.
18.
Arithmetic
i: 16
261/468
0+0 = 0
PA3 + e
Lemma, p. 256
14.
y 0 + y = y + 0
PA7 + e (2): 1, 12
15.
y + y = y +
Assumption
16.
33.
34.
Lemma, p. 259
y + y = y +
y s() + y = y + s()
y + y = y +
i: 1532
PA7 + e + e (2): 14, 33
Arithmetic
262/468
Definability
Let formula have free variables 1 ,. . . ,k .
Given an interpretation I, a formula defines the k-ary relation of
tuples that make true that is, the relation
Arithmetic
263/468
The PA axioms allow one to show that the defined relation has the
usual properties.
y and y z imply z (transitivity).
If y and y then = y.
iff
( y) 6= y .
Arithmetic
264/468
Transitivity of Less-or-Equal
To show: y, y z ` z.
1.
+ = y
Premise
2.
y + = z
Premise
3.
+ = y, fresh
Assumption
4.
y + = z, fresh
Assumption
5.
+ ( + ) = ( + ) +
Associativity of +
6.
( + ) + = y +
EQsubs( + ): 3
7.
+ ( + ) = z
EQtrans(2): 5, 6, 4
8.
+ = z
i: 7
9.
+ = z
e: 2, 48
10.
+ = z
e: 1, 39
Arithmetic
265/468
Defining Functions
To define a k-ary function, use its (k + 1)-ary relation.
Example: Let Rsq (square-of) be defined by 1 1 = 2 .
Then we can get the effect of having the squaring function:
if contains a free variable , but is fresh, then the
formula
(Rsq (t, ) [/ ])
expresses the square of t satisfies .
We must, however, ensure that Rsq really does define a function;
that is, every number has exactly one square:
(y Rsq (, y)) y z (Rsq (, y) Rsq (, z)) y = z .
We leave this proof as an exercise.
Arithmetic
266/468
Lists
Lists
267/468
Lists
The term cons(, b) will denote the list with as its first element
and b as the remainder of the list.
Lists
268/468
Similarly, lists start with the empty list e, and the cons function
generates new lists out of previous ones.
e, cons(e, e), cons(e, cons(e, e)), cons(e, cons(e, cons(e, e))), . . . .
We also get other objects, when the first argument to cons is not e.
Lists
Basic lists
269/468
Whats a List?
A domain of lists must contain the empty list e.
It must also contain cons(e, e), and so on:
cons(e, cons(e, e)),
cons(e, cons(e, cons(e, e))), . . .
We shall regard the above objects as lists whose elements are all e.
In general, if we want a list containing 1 , 2 , . . . , k we use the
object formed as
cons(1 , cons(2 , cons(. . . cons(k , e) . . .))) .
The values can be anything in the domain.
For now, the values on a list will themselves be lists.
Lists
Basic lists
270/468
Examples
For example, the list containing the three objects cons(e, e), e and
cons(cons(e, e), e), in that order, is
cons cons(e, e), cons e, cons(cons(cons(e, e), e), e)
.
Note: , is NOT the same as , !
Lists
Basic lists
271/468
Examplecises
Lists
Basic lists
272/468
Examplecises
The whole term contains that list twice, yielding e , e .
Lists
Basic lists
272/468
Examplecises
The whole term contains that list twice, yielding e , e .
2. What is the explicit term denoted by e, e, e?
Lists
Basic lists
272/468
Examplecises
The whole term contains that list twice, yielding e , e .
2. What is the explicit term denoted by e, e, e?
The list cons(e, cons(e, cons(e, e))) that we saw previously.
Lists
Basic lists
272/468
Examplecises
The whole term contains that list twice, yielding e , e .
2. What is the explicit term denoted by e, e, e?
The list cons(e, cons(e, cons(e, e))) that we saw previously.
3. Which list is longer: e, e, e or e, e, e, e ?
Lists
Basic lists
272/468
Examplecises
The whole term contains that list twice, yielding e , e .
2. What is the explicit term denoted by e, e, e?
The list cons(e, cons(e, cons(e, e))) that we saw previously.
3. Which list is longer: e, e, e or e, e, e, e ?
The first list is longer. It has three items; the second only two.
Lists
Basic lists
272/468
[e/ ] y [cons(y, )/ ]
Lists
Basic lists
273/468
Exercise:
Prove that every non-e object is a cons; that is, show that
`List 6= e y z cons(y, z) = .
Lists
Basic lists
274/468
Relations on Lists
We can define relations and functions on lists.
Example: the function first(), where the first item on list is first().
BUT: Thats not a function! The list e has no first.
Lists
275/468
Relations on Lists
We can define relations and functions on lists.
Example: the function first(), where the first item on list is first().
BUT: Thats not a function! The list e has no first.
Solution: use the relation
Rfirst = cons(, b), a | a and b are lists .
Rfirst (, y) is definable by the formula z = cons(y, z) .
Lists
275/468
Relations on Lists
We can define relations and functions on lists.
Example: the function first(), where the first item on list is first().
BUT: Thats not a function! The list e has no first.
Solution: use the relation
Rfirst = cons(, b), a | a and b are lists .
Rfirst (, y) is definable by the formula z = cons(y, z) .
Similarly, formula z = cons(z, y) defines
Rrest (, y) =
cons(a, b), b | a and b are lists .
Lists
275/468
and
= cons(z, ) .
and
= cons(z, 2 ) .
Lists
276/468
Length of Lists
Lists
277/468
Length of Lists
Lists
EqLen(e, e).
277/468
Length of Lists
EqLen(e, e).
A non-empty list does not have the same length as the empty
list:
6= e (EqLen(, e) EqLen(e, ) .
Lists
277/468
Length of Lists
EqLen(e, e).
A non-empty list does not have the same length as the empty
list:
6= e (EqLen(, e) EqLen(e, ) .
length :
y EqLen(, y) EqLen(cons(, ), cons(, y)) .
Lists
277/468
To show:
Every list has the same length as itself; that is, EqLen(, ).
Lists
278/468
To show:
Every list has the same length as itself; that is, EqLen(, ).
We prove this using induction, via List3.
Basis: `List EqLen(e, e). Given.
Inductive step: Need to prove, for arbitrary (fresh) , that
EqLen(, ) `List y EqLen(cons(y, ), cons(y, )) .
This follows directly from the third formula for EqLen.
Lists
278/468
6= e UnEqLen(e, ) UnEqLen(, e)
,
and
y UnEqLen(, y) UnEqLen(cons(, ), cons(, y)) .
Exercise: Show that `List y UnEqLen(, y) EqLen(, y) .
Lists
279/468
280/468
Introduction
281/468
Introduction
282/468
Introduction
283/468
Analyzing Append
284/468
Analyzing Append
285/468
Append, line 1
The first line:
( cond ( ( equal? x empty) y ) ...)
Corresponding formula:
= e RAppend (, y, y)
Simplified:
Remaining:
RAppend (e, y, y) .
6= e 2 ,
Analyzing Append
286/468
Append, line 2
cond ( #t (cons ( first x ) ( Append (rest x) y ) ) )
Introduce new variables for the anonymous values:
: the value of first x
r : the value of rest x
: the value of (Append ...).
The formula 2 :
Rfirst (, ) Rrest (, r )
Analyzing Append
287/468
Exercise:
Prove that Append is total; that is, prove
{App1, App2} `List y z RAppend (, y, z)
Analyzing Append
288/468
Properties of Append
Exercise:
Show that the function Append is associative; that is, for all x, y
and z, the programs (Append x (Append y z) ) and
(Append (Append x y) z) produce the same result.
Give a formula, using the relation RAppend , that states the
required property.
Show that your formula has a proof from the list axioms and
Analyzing Append
289/468
Analyzing Append
290/468
291/468
292/468
293/468
Remembering Append
We analyzed the program for Append as follows.
We introduced variables for the values used in the program,
294/468
Whats in a Name?
295/468
Whats in a Name?II
296/468
Whats in a Name?III
297/468
298/468
cond, equal?, x, e, y,
299/468
Evaluation of Programs
Evaluation is the process of converting expressions to values.
In Scheme, the basic step of evaluation is a substitution step: a
replacement of one part of the expression by something else.
If no substitution is possible, then evaluation has ended.
We shall describe two predicates, Step and Eval.
Step will describe a single substitution step.
Eval will describe the result of a complete evaluation.
The formulas for Step will take some time to cover, since it has
many cases.
After that, Eval is rather simpler to characterize.
However, its apparent simplicity does mask some subtleties.
Interpreting Scheme Programs
Evaluation: Basics
300/468
Evaluation: Basics
301/468
Simplifying Assumptions
For now, we shall make two assumptions about programs.
All define statements come at the start of the program, with no
Evaluation: Basics
302/468
More-complex steps:
Apply a control element.
Apply a user-defined function.
Take a step in a sub-expression.
Evaluation: Basics
303/468
Built-In Functions
Example:
The formula first for the built-in function first is
Rfirst (, y).
Evaluation: Basics
304/468
Step first, cons (, y) , D, .
Evaluation: Basics
305/468
Evaluation: Basics
306/468
Evaluation: Basics
307/468
Looking Up a Name, I
Evaluation: Basics
308/468
Looking Up a Name, II
If the first pair in the dictionary has something else as its first
6= LookUp(, D, y) LookUp , cons (, , D) , y .
If the dictionary has no first pair (it is the empty list),
Evaluation: Basics
309/468
Evaluation: Basics
310/468
What Is a Value?
Values
311/468
Lists of Values
Values
312/468
Step6: y IsValue , , y .
If and y are values, and is not a function, then cons (, y) is a
value:
Step7:
Values
313/468
314/468
Step Foo, a, b , cons Foo, foodef , z , foodef, a, b .
315/468
Step9: Step cons3(cond, #t, , y), D, .
(cond (#f b) c...) has the same evaluation as (cond c...):
Step10: Step cons3(cond, #f, , y), D, cons (cond, y) .
316/468
cond, Continued
317/468
Applying Functions
The final case of stepping: evaluating an expression which is the
application of a function. Scheme defines application as follows.
To apply a function (lambda (x) y) to a value u, substitute
u for the name x everywhere that x occurs in the
expression y, and evaluate the result.
If Subst(, , y, ) means substituting for name in term y
yields term , then we get
Step12: IsValue()
Subst , , y, Step
, , y , , D,
.
318/468
Applying Functions, II
319/468
Defining Substitution
This leaves us with the substitution itself, which we characterize
inductively. We have base cases
Step14:
Subst (, , e, e) ,
(, , , ) , and
Subst
y 0 (y = cons (name, y 0 )) (y 6= ) Subst (, , y, y) .
Subst (, , y, ) (y 6= name)
Subst (, , z, ) Subst , , cons (y, z) , cons (, )
.
320/468
Full Evaluation
Lastly, we have the cases where no step applies; that is, we have
and D for which
y (Step(, D, y)) .
In this case, evaluation is complete, and is the final result.
Step(, D, y) Eval(y, D, z) Eval(, D, z) .
321/468
Summary of Evaluation
We can write axioms such that the following holds.
322/468