CMSC 631 Fall 2006: Derivations & Semantics for Extended Language Homework, Assignments of Computer Science

A homework assignment for the computer science course cmsc 631, offered in the fall 2006 semester. The assignment includes three problems related to derivations and semantics for a programming language with extended features, such as boolean expressions, lists, and pattern matching. Students are asked to establish the legality of certain evaluations, prove a theorem about the language, and define small and big-step semantics for the extended language.

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-x1z-2
koofers-user-x1z-2 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 631, Fall 2006
Homework 2
Due Wednesday, Sept. 27, in class
1. For the language of commands that we saw in class (Figure 2), show derivations that establish the
following as legal evaluations:
(a) ((1 2) + (3 x), s)(8, s) and likewise ((1 2) + (3 x), s)(8, s) where s(x) = 2.
(b) (x:= 1; while xdo (x:= x+ (1)), s)s0where s0=s[x7→ 0] and likewise for .
(Recall that is the small-step relation, is the big-step relation. Also, the syntax s[x7→ i] defines
a new map s0such that s0(x) = iand s0(y) = s(y) for all y6=x.)
2. For the language of arithmetic expressions that we discussed in class (Figure 1), prove that for all eand
swhere dom(s)vars (e), either eis an integer ior else there exists an e0such that (e,s)(e0, s).
We define vars(e) as follows:
vars(x) = {x}
vars(i) =
vars(e1+e2) = vars (e1)vars(e2)
vars(e1e2) = vars(e1)vars (e2)
You should prove this by induction on the height of the derivation (e, s)(e0, s).
3. Suppose we modify the language of commands and expressions in two ways. First, we add a new
class of boolean expressions and modify the conditional operators if and while to use boolean guards.
Second, we add lists of integers as a legal data structure in our language, along with a pattern matching
operation for accessing the elements of a list. The new language is shown in Figure 3. Define small
and big-step semantics for this new language. Here are a few more details about the language:
Boolean expressions These are largely straightforward; you should follow the same ideas as arith-
metic expressions in defining their semantics. (Note that is boolean “or”, is boolean “and”, and
! is boolean “not”.) You should assume that two expressions are equal if they are syntactically the
same. That is, v1=v2if v1has exactly the same syntactic structure as v2. Note that you only define
equality in terms of values, which are either integers ior integer lists l. In all cases, expressions will
evaluate to values.
Lists A list lis a series of comma-separated integers, concluded by a nil. For example, (1,nil ) is a
list, and (1,(2,(3,nil))) is a list. We’ve extended expressions ewith the empty list nil, and the form
e1, e2which will evaluate to a list assuming that e1evaluates to an integer and e2evaluates to a list.
To access the elements of a list, we use the case expression to do pattern matching. In the command
case eof nil c1or (x:: y)c2, if eevaluates to nil, then the whole command reduces to c1. If e
evaluates to some list (i, l), then the whole thing evaluates to c2, and in the current store is modified
to map xto iand yto l.
pf2

Partial preview of the text

Download CMSC 631 Fall 2006: Derivations & Semantics for Extended Language Homework and more Assignments Computer Science in PDF only on Docsity!

CMSC 631, Fall 2006

Homework 2

Due Wednesday, Sept. 27, in class

  1. For the language of commands that we saw in class (Figure 2), show derivations that establish the following as legal evaluations:

(a) ((1 ∗ 2) + (3 ∗ x), s) →∗^ (8, s) and likewise ((1 ∗ 2) + (3 ∗ x), s) ⇓ (8, s) where s(x) = 2. (b) (x := 1; while x do (x := x + (−1)), s) →∗^ s′^ where s′^ = s[x 7 → 0] and likewise for ⇓.

(Recall that → is the small-step relation, ⇓ is the big-step relation. Also, the syntax s[x 7 → i] defines a new map s′^ such that s′(x) = i and s′(y) = s(y) for all y 6 = x.)

  1. For the language of arithmetic expressions that we discussed in class (Figure 1), prove that for all e and s where dom(s) ⊇ vars(e), either e is an integer i or else there exists an e′^ such that (e, s) → (e′, s). We define vars(e) as follows:

vars(x) = {x} vars(i) = ∅ vars(e 1 + e 2 ) = vars(e 1 ) ∪ vars(e 2 ) vars(e 1 ∗ e 2 ) = vars(e 1 ) ∪ vars(e 2 )

You should prove this by induction on the height of the derivation (e, s) → (e′, s).

  1. Suppose we modify the language of commands and expressions in two ways. First, we add a new class of boolean expressions and modify the conditional operators if and while to use boolean guards. Second, we add lists of integers as a legal data structure in our language, along with a pattern matching operation for accessing the elements of a list. The new language is shown in Figure 3. Define small and big-step semantics for this new language. Here are a few more details about the language: Boolean expressions These are largely straightforward; you should follow the same ideas as arith- metic expressions in defining their semantics. (Note that ∨ is boolean “or”, ∧ is boolean “and”, and ! is boolean “not”.) You should assume that two expressions are equal if they are syntactically the same. That is, v 1 = v 2 if v 1 has exactly the same syntactic structure as v 2. Note that you only define equality in terms of values, which are either integers i or integer lists l. In all cases, expressions will evaluate to values. Lists A list l is a series of comma-separated integers, concluded by a nil. For example, (1, nil ) is a list, and (1, (2, (3, nil ))) is a list. We’ve extended expressions e with the empty list nil, and the form e 1 , e 2 which will evaluate to a list assuming that e 1 evaluates to an integer and e 2 evaluates to a list. To access the elements of a list, we use the case expression to do pattern matching. In the command case e of nil ⇒ c 1 or (x :: y) ⇒ c 2 , if e evaluates to nil, then the whole command reduces to c 1. If e evaluates to some list (i, l), then the whole thing evaluates to c 2 , and in the current store is modified to map x to i and y to l.
  1. Can you prove the theorem in problem (2), above, for the new language defined in problem (3)? That is: for all c and s where dom(s) ⊇ vars(c), there exists an e′^ and s′^ such that either (c, s) → s′^ or (c, s) → (c′, s′). (Here, vars is extended to commands in the natural way.) If you can prove this, sketch the proof (you don’t need to go into all the details). Otherwise, explain why it cannot be proved.

variables x, y, z ∈ V integers i, j, k ∈ Z expressions e ::= x | i | e 1 + e 2 | e 1 ∗ e 2

Figure 1: Language of arithmetic expressions

commands c, d ::= skip | x := e | if e then c 1 else c 2 | while e do c

Figure 2: Language of commands (includes language of expressions)

variables x, y, z ∈ V integers i, j, k ∈ Z lists l, m ::= nil | i, l bool .expressions a, b ::= true | false | b 1 ∧ b 2 | b 1 ∨ b 2 | !b | e 1 = e 2 expressions e ::= x | i | e 1 + e 2 | e 1 ∗ e 2 | e 1 , e 2 | nil values v ::= i | l commands c, d ::= skip | x := e | if b then c 1 else c 2 | while b do c | c 1 ; c 2 | case e of nil ⇒ c 1 or (x :: y) ⇒ c 2

Figure 3: Extended language of commands