15.08.2013 Views

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the axiom says that any object that is not represented by a constructor expression is not a member<br />

of N1 and L[N].<br />

Operations on Lists: Append<br />

The append function @: L[N] × L[N] → L[N] concatenates lists<br />

Defining equations: nil@l = l and cons(n, l)@r = cons(n, l@r)<br />

Example 119 [3, 2, 1]@[1, 2] = [3, 2, 1, 1, 2] and []@[1, 2, 3] = [1, 2, 3] = [1, 2, 3]@[]<br />

Lemma 120 For all l, r ∈ L[N], there is exactly one s ∈ L[N] with s = l@r.<br />

Proof: by induction on l. (what does this mean?)<br />

P.1 we have two cases<br />

P.1.1 base case: l = nil:<br />

must have s = r.<br />

P.1.2 step case: l = cons(n, k) for some list k:<br />

P.1.2.1 Assume that here is a unique s ′ with s ′ = k@r,<br />

P.1.2.2 then s = cons(n, k)@r = cons(n, k@r) = cons(n, s ′ ).<br />

Corollary 121 Append is a function (see, this just worked fine!)<br />

c○: Michael Kohlhase 79<br />

You should have noticed that this proof looks exactly like the one for addition. In fact, wherever<br />

we have used an axiom P i there, we have used an axiom LPi here. It seems that we can do<br />

anything we could for unary natural numbers for lists now, in particular, programming by recursive<br />

equations.<br />

Operations on Lists: more examples<br />

Definition 122 λ(nil) = o and λ(cons(n, l)) = s(λ(l))<br />

Definition 123 ρ(nil) = nil and ρ(cons(n, l)) = ρ(l)@cons(n, nil).<br />

c○: Michael Kohlhase 80<br />

Now, we have seen that “inductively defined sets” are a basis for computation, we will turn to the<br />

programming language see them at work in concrete setting.<br />

2.3.3 Inductively Defined Sets in SML<br />

We are about to introduce one of the most powerful aspects of SML, its ability to define data<br />

types. After all, we have claimed that types in SML are first-class objects, so we have to have a<br />

means of constructing them.<br />

We have seen above, that the main feature of an inductively defined set is that it has Peano<br />

Axioms that enable us to use it for computation. Note that specifying them, we only need to<br />

know the constructors (and their types). Therefore the datatype constructor in SML only needs<br />

to specify this information as well. Moreover, note that if we have a set of constructors of an<br />

inductively defined set — e.g. zero : mynat and suc : mynat -> mynat for the set mynat, then<br />

their codomain type is always the same: mynat. Therefore, we can condense the syntax even<br />

further by leaving that implicit.<br />

44

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!