Evaluate the semantics of these combinations of keystrokes
10 – 5 +/- M+ 6 × MR M+ =
using the denotational definition of the calculator language. (25%)
Ans>
meaning[[10 – 5 +/- M+ 6 × MR M+ =]] = d
where perform[[10 – 5 +/- M+ 6 × MR M+ =]](0, nop, 0, 0) = (a, op, d, m)
The evaluation proceeds as follows:
perform[[10 – 5 +/- M+ 6 × MR M+ =]]( 0, nop, 0, 0 )
= ( perform[[6 × MR M+ =]] º evaluate[[10 – 5 +/- M+]] )( 0, nop, 0, 0 )
= perform[[6 × MR M+ =]]( calculate[[M+]] º evaluate[[10 – 5 +/-]] )( 0, nop, 0, 0 )
= perform[[6 × MR M+ =]]( calculate[[M+]]( calculate[[+/-]] º
evaluate[[10 – 5]] ) )( 0, nop, 0, 0 )
= perform[[6 × MR M+ =]]( calculate[[M+]]( calculate[[+/-]](
evaluate[[5]] º compute[[–]] º evaluate[[10]] ) ) )( 0, nop, 0, 0 )
= perform[[6 × MR M+ =]]( calculate[[M+]]( calculate[[+/-]](
evaluate[[5]]( compute[[–]]( evaluate[[10]] ( 0, nop, 0, 0 ) ) ) ) ) )
= perform[[6 × MR M+ =]]( calculate[[M+]]( calculate[[+/-]](
evaluate[[5]]( compute[[–]]( 0, nop, 10, 0 ) ) ) ) )
= perform[[6 × MR M+ =]]( calculate[[M+]]( calculate[[+/-]](
evaluate[[5]]( 10, minus, 10, 0 ) ) ) )
= perform[[6 × MR M+ =]]( calculate[[M+]]( calculate[[+/-]]( 10, minus, 5, 0 ) ) )
= perform[[6 × MR M+ =]]( calculate[[M+]]( 10, minus, -5, 0 ) )
= perform[[6 × MR M+ =]]( 10, nop, 15, 15 )
= evaluate[[6 × MR M+ =]]( 10, nop, 15, 15 )
= ( calculate[[=]] º evaluate[[6 × MR M+]] )( 10, nop, 15, 15 )
= calculate[[=]]( calculate[[M+]] º evaluate[[6 × MR]] )( 10, nop, 15, 15 )
= calculate[[=]]( calculate[[M+]]( evaluate[[MR]] º
compute[[×]] º evaluate[[6]] ) )( 10, nop, 15, 15 )
= calculate[[=]]( calculate[[M+]]( evaluate[[MR]](
compute[[×]]( evaluate[[6]]( 10, nop, 15, 15 ) ) ) ) )
= calculate[[=]]( calculate[[M+]]( evaluate[[MR]](
compute[[×]]( 10, nop, 6, 15 ) ) ) )
= calculate[[=]]( calculate[[M+]]( evaluate[[MR]]( 6, times, 6, 15 ) ) )
= calculate[[=]]( calculate[[M+]]( 6, times, 15, 15 ) )
= calculate[[=]]( 6, nop, 90, 105 )
= ( 6, nop, 90, 105 )
Therefore meaning[[10 – 5 +/- M+ 6 × MR M+ =]] = 90
.
Use the operational semantics of the sample language to reduce the following program to its environment: (30%)
x := 0 – 4;
if x then x := x else x := 0 – x fi;
while x do x := x – 2 od
Ans>
Let
S0 = x ':=' '0' '–' '4'
,
S1 = 'if' x 'then' x':='x 'else' x':='0'–'x 'fi'
, and
S2 = 'while' x 'do' x ':=' x '–' '2' 'od'
.
<S0; S1; S2 | Env0>
<x ':=' '0' '–' '4' | Env0>
⇒ <x ':=' 0 '–' '4' | Env0>
⇒ <x ':=' 0 '–' 4 | Env0>
⇒ <x ':=' –4 | Env0>
⇒ Env0 & {x = –4} = {x = –4}
<S0; S1; S2 | Env0>
⇒ <S1; S2 | {x=–4}>
<'if' x 'then' x ':=' x 'else' x ':=' '0' '–' x 'fi' | {x = –4}>
⇒ <'if' –4 'then' x ':=' x 'else' x ':=' '0' '–' x 'fi' | {x = –4}>
⇒ <x ':=' '0' '–' x | {x = –4}>
⇒ <x ':=' 0 '–' x | {x = –4}>
⇒ <x ':=' 0 '–' –4 | {x = –4}>
⇒ <x ':=' 4 | {x = –4}>
⇒ {x = 4}
<S1; S2 | {x = –4}>
⇒ <S2 | {x = 4}>
<'while' x 'do' x ':=' x '–' '2' 'od' | {x=4}>
<x ':=' x '–' '2' | {x = 4}>
⇒ <x ':=' 4 '–' '2' | {x = 4}>
⇒ <x ':=' 4 '–' 2 | {x = 4}>
⇒ <x ':=' 2 | {x = 4}>
⇒ {x = 2}
<'while' x 'do' x ':=' x '–' '2' 'od' | {x=4}>
⇒ <x ':=' x '–' '2'; 'while' x 'do' x ':=' x '–' '2' 'od' | {x=4}>
⇒ <'while' x 'do' x ':=' x '–' '2' 'od' | {x=2}>
<x ':=' x '–' '2' | {x = 2}>
⇒ <x ':=' 2 '–' '2' | {x = 2}>
⇒ <x ':=' 2 '–' 2 | {x = 2}>
⇒ <x ':=' 0 | {x = 2}>
⇒ {x = 0}
<'while' x 'do' x ':=' x '–' '2' 'od' | {x=2}>
⇒ <x ':=' x '–' '2'; 'while' x 'do' x ':=' x '–' '2' 'od' | {x=2}>
⇒ <'while' x 'do' x ':=' x '–' '2' 'od' | {x=0}>
⇒ {x=0}
The final environment is {x=0}
.