Transformation of Applicative Specifications into Imperative ...

Transformation of Applicative Specifications into Imperative ... Transformation of Applicative Specifications into Imperative ...

26.09.2013 Views

CHAPTER 6. TRANSFORMATIONS A case expression is transformed by transforming the constituent value expressions and patterns. The patterns are transformed as follows: Value Literal Pattern A value literal pattern is transformed into itself. Name Pattern A name pattern is transformed into itself unless if it is of the type of interest. In that case the specification cannot be transformed as a pattern cannot consist of a variable. Wildcard Pattern A wildcard pattern is transformed into itself. Product Pattern A product pattern is of the form: (pattern1, pattern2, ..., patternn) A product pattern is transformed by transforming the constituent patterns. Record Pattern A record pattern is of the form: id(pattern1, pattern2, ..., patternn) A record pattern is transformed by transforming the constituent patterns. List Pattern A list pattern is of one of the forms: 〈pattern1, pattern2, ..., patternn〉 〈pattern1, pattern2, ..., patternn〉 patternn+1 A list pattern is transformed by transforming the constituent patterns. Introducing Assignment Expressions When transforming an applicative specification into the imperative counterpart it is necessary to establish assignment expressions when the value of the variable of the type of interest must be changed. This can happen in two situations. When dealing with a generator of the form: 50

6.4. TRANSFORMING EXPLICIT FUNCTION DEFINITIONS id : type_exprarg → type_exprres id(id1, ..., idn) ≡ value_expr the variable has to be assigned the proper value. There are two cases to consider. The type expression type_exprres of the result of the function is: 1. T. Transformation: The variable must be assigned the transformed value_expr, see function gen1 in Example 6.12. 2. a product in which T occurs. Transformation: The variable must be assigned the proper part of the product expression of value_expr, see function gen2 in Example 6.12. Example 6.12 – Assignment expressions in generators type T = Int value gen1 : Int → T gen1(i) ≡ i, ✄ gen2 : Int × Bool → T × Bool gen2(i, b) ≡ (i, b) type T = Int variable t : T value gen1 : Int → write t Unit gen1(i) ≡ t := i, gen2 : Int × Bool → write t Bool gen2(i, b) ≡ let (pa_0, pa_1) = (t := i, b) in (pa_1) end When dealing with implicit generators, that is when a value of the type of interest is expected, assignment expressions have to be established. This 51

6.4. TRANSFORMING EXPLICIT FUNCTION DEFINITIONS<br />

id : type_exprarg → type_exprres<br />

id(id1, ..., idn) ≡ value_expr<br />

the variable has to be assigned the proper value. There are two cases to<br />

consider. The type expression type_exprres <strong>of</strong> the result <strong>of</strong> the function is:<br />

1. T.<br />

<strong>Transformation</strong>: The variable must be assigned the transformed<br />

value_expr, see function gen1 in Example 6.12.<br />

2. a product in which T occurs.<br />

<strong>Transformation</strong>: The variable must be assigned the proper part <strong>of</strong><br />

the product expression <strong>of</strong> value_expr, see function gen2 in Example<br />

6.12.<br />

Example 6.12 – Assignment expressions in generators<br />

type<br />

T = Int<br />

value<br />

gen1 : Int → T<br />

gen1(i) ≡ i,<br />

✄<br />

gen2 : Int × Bool → T × Bool<br />

gen2(i, b) ≡ (i, b)<br />

type<br />

T = Int<br />

variable<br />

t : T<br />

value<br />

gen1 : Int → write t Unit<br />

gen1(i) ≡ t := i,<br />

gen2 : Int × Bool → write t Bool<br />

gen2(i, b) ≡<br />

let (pa_0, pa_1) = (t := i, b) in (pa_1) end<br />

When dealing with implicit generators, that is when a value <strong>of</strong> the type<br />

<strong>of</strong> interest is expected, assignment expressions have to be established. This<br />

51

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

Saved successfully!

Ooh no, something went wrong!