Exercises on Concepts of Programming Languages - Christian

Exercises on Concepts of Programming Languages. Christian Rinderknecht. 9 February 2006. 1 Calculator. Assume the following system of inference rules:.
29KB taille 7 téléchargements 319 vues
Exercises on Concepts of Programming Languages Christian Rinderknecht 9 February 2006

1

Calculator

Assume the following system of inference rules: e1 → e′1 e1 × e2 → e′1 × e2 e1 → e′1 e1 + e2 → e′1 + e2 e1 → e′1 e1 − e2 → e′1 − e2 e1 → e′1 e1 /e2 → e′1 /e2

e2 → e′2

⟨Mult1 ⟩

e1 × e2 → e1 × e′2 e2 → e′2

⟨Add1 ⟩

e1 + e2 → e1 + e′2 e2 → e′2

⟨Sub1 ⟩

e1 − e2 → e1 − e′2 e2 → e′2

⟨Div1 ⟩

e1 /e2 → e1 /e′2

⟨Mult2 ⟩

⟨Add2 ⟩

⟨Sub2 ⟩

⟨Div2 ⟩

We also assume that we have an infinity of rules for multiplying, adding, subtracting and dividing (integer division) numbers. 1. Is this system deterministic? 2. Reduce the following expressions in all the possible ways and give at each rewrite step the corresponding substitution: (a) (1 + 2) × (3 + (4 × 5)) (b) ((1 + 2)/0) × (3 + 4) (c) (1 + (2 × 3))/((4 + 5) − 9)

1

Assume now the following variant and answer the previous questions again. e1 → e′1 e1 × e2 → e′1 × e2 e → e′ e + v → e′ + v

e1 − e2 → e′1 − e2 e/v → e′ /v

v × e → v × e′ e2 → e′2

⟨Add1 ⟩

e1 → e′1 e → e′

e → e′

⟨Mult1 ⟩

e1 + e2 → e1 + e′2 e → e′

⟨Sub1 ⟩

v − e → v − e′ e2 → e′2

⟨Div1 ⟩

e1 /e2 → e1 /e′2

⟨Mult2 ⟩

⟨Add2 ⟩

⟨Sub2 ⟩

⟨Div2 ⟩

Assume now that we have a different rule ⟨Div1 ⟩ and new rules for handling the division by zero: v ̸= 0

e → e′

e/v → e′ /v

e/0 → NaN ⟨DivZero⟩

⟨Div1 ⟩

NaN × e → NaN ⟨Mult-Err1 ⟩

e × NaN → NaN ⟨Mult-Err2 ⟩

NaN + e → NaN ⟨Add-Err1 ⟩

e + NaN → NaN ⟨Add-Err2 ⟩

NaN − e → NaN ⟨Sub-Err1 ⟩

e − NaN → NaN

NaN/e → NaN ⟨Div-Err1 ⟩

⟨Sub-Err2 ⟩

e/NaN → NaN ⟨Div-Err2 ⟩

Answer the same questions again. Is it better to have the following rule? e1 → e′1 e1 /e2 → e′1 /e2

2

⟨Div1 ⟩

2

Boolean expressions

Consider the system of inference rules true ∧ e → e

false ∧ e → false ⟨∧False ⟩

⟨∧True ⟩

e1 → e′1 e1 ∧ e2 → e′1 ∧ e2

¬false → true ⟨Not-False⟩

⟨∧⟩

¬true → false ⟨Not-True⟩

e → e′ ¬e → ¬e′

⟨Not⟩

e1 ∨ e2 → ¬(¬e1 ∧ ¬e2 ) ⟨Or⟩ 1. Is this system deterministic? 2. Reduce the following expressions in several ways if possible, and give at each rewrite step the corresponding substitution: (a) (true ∧ (false ∨ true)) ∧ ¬(true ∨ (true ∧ true)) (b) ¬((true ∧ true) ∨ ¬(false ∨ ¬true))

3

Arithmetic

Let us model the integers. The number 0 is noted Zero. If an integer is noted n, then Succ(n) denotes the next integer and Pred(n) the previous. For example Mathematical notation New notation Zero 0 Succ(Zero) 1 Succ(Succ(Zero)) 2 ... ... Pred(Zero) −1 Pred(Pred(Zero)) −2 ... ... Let us define now a function IsZero which returns the boolean true if the argument is Zero and false if the argument is not Zero: IsZero(Zero) → true IsZero(Succ(n)) → false IsZero(Pred(n)) → false

3

But this definition is broken, because it implies, for instance IsZero(Pred(Succ(Zero))) → false What solution do you propose to fix it? Answer: Succ(Pred(n)) → n Pred(Succ(n)) → n

4

Stacks IsEmpty(Push(x, s)) → false ⟨IsNotEmpty⟩ IsEmpty(Empty) → true ⟨IsEmpty⟩ Length(Empty) → 0

⟨ZeroLength⟩

Length(Push(x, s)) → 1 + Length(s) ⟨Length⟩ Rev(Empty) → Empty

⟨Rev-E⟩

Rev(Push(x, s)) → Append(Rev(s), Push(x, Empty))

4

⟨Rev-P⟩