Semantyka i weryfikacja programów/Ćwiczenia 2: Różnice pomiędzy wersjami

Z Studia Informatyczne
Przejdź do nawigacjiPrzejdź do wyszukiwania
m Zastępowanie tekstu - "\Longrightarrow\" na "\Longrightarrow"
Linia 61: Linia 61:


<math>
<math>
e, s \,\Longrightarrow\, e', s
e, s \,\Longrightarrow, e', s
</math>
</math>


Linia 67: Linia 67:


<math>
<math>
b, s \,\Longrightarrow\, b', s
b, s \,\Longrightarrow, b', s
</math>
</math>


Linia 74: Linia 74:


<math>
<math>
e, s \,\Longrightarrow\, n
e, s \,\Longrightarrow, n
\quad \quad
\quad \quad
\mbox{ oraz }
\mbox{ oraz }
\quad \quad
\quad \quad
b, s \,\Longrightarrow\, l
b, s \,\Longrightarrow, l
</math>
</math>
gdzie <math>n \in</math> jest liczbą całkowitą, <math>n \in \mathbf{Num}</math>, a <math>l \in \mathbf{Bool} = \{ \mathbf{true}, \mathbf{false} \}</math>.
gdzie <math>n \in</math> jest liczbą całkowitą, <math>n \in \mathbf{Num}</math>, a <math>l \in \mathbf{Bool} = \{ \mathbf{true}, \mathbf{false} \}</math>.
Linia 94: Linia 94:


<math>
<math>
l, s \,\Longrightarrow\, l
l, s \,\Longrightarrow, l
\quad \quad
\quad \quad
n, s \,\Longrightarrow\, n.
n, s \,\Longrightarrow, n.
</math>
</math>


Linia 106: Linia 106:


<math>
<math>
\frac{b_1, s \,\Longrightarrow\, b'_1, s}
\frac{b_1, s \,\Longrightarrow, b'_1, s}
{b_1 \land b_2, s \,\Longrightarrow\, b'_1 \land b_2, s}
{b_1 \land b_2, s \,\Longrightarrow, b'_1 \land b_2, s}
\quad \quad
\quad \quad
\frac{b_2, s \,\Longrightarrow\, b'_2, s}
\frac{b_2, s \,\Longrightarrow, b'_2, s}
{l_1 \land b_2, s \,\Longrightarrow\, l_1 \land b_2, s}
{l_1 \land b_2, s \,\Longrightarrow, l_1 \land b_2, s}
\quad \quad
\quad \quad
l_1 \land l_2, s \,\Longrightarrow\, l, s   
l_1 \land l_2, s \,\Longrightarrow, l, s   
\mbox{ o ile } l = l_1 \land l_2  
\mbox{ o ile } l = l_1 \land l_2  
</math>
</math>
Linia 120: Linia 120:


<math>
<math>
\frac{b_1, s \,\Longrightarrow\, b'_1, s}
\frac{b_1, s \,\Longrightarrow, b'_1, s}
{b_1 \land b_2, s \,\Longrightarrow\, b'_1 \land b_2, s}
{b_1 \land b_2, s \,\Longrightarrow, b'_1 \land b_2, s}
\quad \quad
\quad \quad
\mathbf{false} \land b_2, s \,\Longrightarrow\, \mathbf{false}, s
\mathbf{false} \land b_2, s \,\Longrightarrow, \mathbf{false}, s
\quad \quad
\quad \quad
\mathbf{true}  \land b_2, s \,\Longrightarrow\, b_2, s.
\mathbf{true}  \land b_2, s \,\Longrightarrow, b_2, s.
</math>
</math>


Linia 131: Linia 131:


<math>
<math>
\frac{b_2, s \,\Longrightarrow\, b'_2, s}
\frac{b_2, s \,\Longrightarrow, b'_2, s}
{b_1 \land b_2, s \,\Longrightarrow\, b_1 \land b'_2, s}
{b_1 \land b_2, s \,\Longrightarrow, b_1 \land b'_2, s}
\quad \quad
\quad \quad
b_1 \land \mathbf{false}, s \,\Longrightarrow\, \mathbf{false}, s
b_1 \land \mathbf{false}, s \,\Longrightarrow, \mathbf{false}, s
\quad \quad
\quad \quad
b_1 \land \mathbf{true}, s \,\Longrightarrow\, b_1, s.
b_1 \land \mathbf{true}, s \,\Longrightarrow, b_1, s.
</math>
</math>


Linia 146: Linia 146:


<math>
<math>
\neg \mathbf{true}, s \,\Longrightarrow\, \mathbf{false}, s
\neg \mathbf{true}, s \,\Longrightarrow, \mathbf{false}, s
\quad \quad \quad
\quad \quad \quad
\neg \mathbf{false}, s \,\Longrightarrow\, \mathbf{true}, s  
\neg \mathbf{false}, s \,\Longrightarrow, \mathbf{true}, s  
\quad \quad \quad
\quad \quad \quad
\frac{b, s \,\Longrightarrow\, b', s}
\frac{b, s \,\Longrightarrow, b', s}
     {\neg b, s \,\Longrightarrow\, \neg b', s}
     {\neg b, s \,\Longrightarrow, \neg b', s}
</math>
</math>


Linia 157: Linia 157:


<math>
<math>
\frac{e_1, s \,\Longrightarrow\, e'_1, s}
\frac{e_1, s \,\Longrightarrow, e'_1, s}
{e_1 \leq e_2, s \,\Longrightarrow\, e'_1 \leq e_2, s}
{e_1 \leq e_2, s \,\Longrightarrow, e'_1 \leq e_2, s}
\quad \quad
\quad \quad
\frac{e_2, s \,\Longrightarrow\, e'_2, s}
\frac{e_2, s \,\Longrightarrow, e'_2, s}
{e_1 \leq e_2, s \,\Longrightarrow\, e_1 \leq e'_2, s}
{e_1 \leq e_2, s \,\Longrightarrow, e_1 \leq e'_2, s}
</math>
</math>


<math>
<math>
n_1 \leq n_2, s \,\Longrightarrow\, \mathbf{true}, s  \quad \mbox{ o ile } n_1 \leq n_2
n_1 \leq n_2, s \,\Longrightarrow, \mathbf{true}, s  \quad \mbox{ o ile } n_1 \leq n_2
\quad \quad
\quad \quad
n_1 \leq n_2, s \,\Longrightarrow\, \mathbf{false}, s  \quad  \mbox{ o ile } n_1 > n_2.
n_1 \leq n_2, s \,\Longrightarrow, \mathbf{false}, s  \quad  \mbox{ o ile } n_1 > n_2.
</math>
</math>


Linia 177: Linia 177:


<math>
<math>
\frac{e, s \,\Longrightarrow\, e', s}
\frac{e, s \,\Longrightarrow, e', s}
     {x := e, s \,\Longrightarrow\, x := e', s}
     {x := e, s \,\Longrightarrow, x := e', s}
\quad \quad
\quad \quad
{x := n, s \,\Longrightarrow\, s[x \mapsto n]}
{x := n, s \,\Longrightarrow, s[x \mapsto n]}
</math>
</math>


Linia 187: Linia 187:


<math>
<math>
\frac{b, s \,\Longrightarrow\, b', s}
\frac{b, s \,\Longrightarrow, b', s}
{\mathbf{if}\, b \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s \,\Longrightarrow\,  
{\mathbf{if}\, b \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s \,\Longrightarrow,  
\mathbf{if}\, b' \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s}
\mathbf{if}\, b' \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s}
\quad \quad
\quad \quad
\frac{b, s \,\Longrightarrow\, b', s}
\frac{b, s \,\Longrightarrow, b', s}
{\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow\, \mathbf{while}\, b' \,\mathbf{do}\, I, s}
{\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow, \mathbf{while}\, b' \,\mathbf{do}\, I, s}
</math>
</math>


Linia 199: Linia 199:


<math>
<math>
\mathbf{if}\, \mathbf{true} \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s \,\Longrightarrow\, I_1, s
\mathbf{if}\, \mathbf{true} \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s \,\Longrightarrow, I_1, s
\quad \quad
\quad \quad
\mathbf{if}\, \mathbf{false} \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s \,\Longrightarrow\, I_2, s
\mathbf{if}\, \mathbf{false} \,\mathbf{then}\, I_1 \,\mathbf{else}\, I_2, s \,\Longrightarrow, I_2, s
</math>
</math>


Linia 207: Linia 207:


<math>
<math>
\mathbf{while}\, \mathbf{true} \,\mathbf{do}\, I, s \,\Longrightarrow\,  
\mathbf{while}\, \mathbf{true} \,\mathbf{do}\, I, s \,\Longrightarrow,  
I;\, \mathbf{while}\, ? \,\mathbf{do}\, I, s
I;\, \mathbf{while}\, ? \,\mathbf{do}\, I, s
</math>
</math>


ale nie wiemy już, jaki był dozór pętli (widzimy tylko wynik obliczenia tego dozoru w stanie s, czyli <math>\mathbf{true}</math>).
ale nie wiemy już, jaki był dozór pętli (widzimy tylko wynik obliczenia tego dozoru w stanie s, czyli <math>\mathbf{true}</math>).
Możemy odwołać się do tranzytywnego domknięcia relacji <math>\,\Longrightarrow\,</math> (czyli w zasadzie do semantyki dużych kroków):
Możemy odwołać się do tranzytywnego domknięcia relacji <math>\,\Longrightarrow,</math> (czyli w zasadzie do semantyki dużych kroków):


<math>
<math>
\frac{b, s \,\Longrightarrow\,^{*} \mathbf{true}}
\frac{b, s \,\Longrightarrow,^{*} \mathbf{true}}
{\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow\,  
{\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow,  
  I;\, \mathbf{while}\, b \,\mathbf{do}\, I, s}
  I;\, \mathbf{while}\, b \,\mathbf{do}\, I, s}
\quad \quad
\quad \quad
\frac{b, s \,\Longrightarrow\,^{*} \mathbf{false}}
\frac{b, s \,\Longrightarrow,^{*} \mathbf{false}}
{\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow\, s}
{\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow, s}
</math>
</math>


Linia 228: Linia 228:


<math>
<math>
\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow\, \mathbf{if}\, b \,\mathbf{then}\, (I; \mathbf{while}\, b \,\mathbf{do}\, I) \,\mathbf{else}\, \mathbf{skip}, s.
\mathbf{while}\, b \,\mathbf{do}\, I, s \,\Longrightarrow, \mathbf{if}\, b \,\mathbf{then}\, (I; \mathbf{while}\, b \,\mathbf{do}\, I) \,\mathbf{else}\, \mathbf{skip}, s.
</math>
</math>


Linia 269: Linia 269:


<math>
<math>
\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow\, I; \mathbf{if}\, b \,\mathbf{then}\, (\mathbf{repeat}\, I \,\mathbf{until}\, b) \,\mathbf{else}\, \mathbf{skip}, s.
\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow, I; \mathbf{if}\, b \,\mathbf{then}\, (\mathbf{repeat}\, I \,\mathbf{until}\, b) \,\mathbf{else}\, \mathbf{skip}, s.
</math>
</math>


Po drugie, spróbujmy odwołać się do <math>\,\Longrightarrow\,^{*}</math> dla dozoru pętli <math>b</math>:
Po drugie, spróbujmy odwołać się do <math>\,\Longrightarrow,^{*}</math> dla dozoru pętli <math>b</math>:


<math>
<math>
\frac{I, s \,\Longrightarrow\, I', s'}
\frac{I, s \,\Longrightarrow, I', s'}
     {\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow\, \mathbf{repeat}\, I' \,\mathbf{until}\, b, s'}
     {\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow, \mathbf{repeat}\, I' \,\mathbf{until}\, b, s'}
</math>
</math>


<math>
<math>
\frac{I, s \,\Longrightarrow\, s' \quad b, s \,\Longrightarrow\,^{*} \mathbf{false}}
\frac{I, s \,\Longrightarrow, s' \quad b, s \,\Longrightarrow,^{*} \mathbf{false}}
     {\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow\, s'}
     {\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow, s'}
\quad \quad
\quad \quad
\frac{I, s \,\Longrightarrow\, s' \quad b, s \,\Longrightarrow\,^{*} \mathbf{true}}
\frac{I, s \,\Longrightarrow, s' \quad b, s \,\Longrightarrow,^{*} \mathbf{true}}
     {\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow\, \mathbf{repeat}\, ? \,\mathbf{until}\, b, s'}
     {\mathbf{repeat}\, I \,\mathbf{until}\, b, s \,\Longrightarrow, \mathbf{repeat}\, ? \,\mathbf{until}\, b, s'}
</math>
</math>


Linia 298: Linia 298:


<math>
<math>
\frac{e, s \,\Longrightarrow\, e', s}
\frac{e, s \,\Longrightarrow, e', s}
     {\,\mathbf{do}\, e \,\mathbf{times}\, I, s \,\Longrightarrow\, D e' \,\mathbf{times}\, I, s}
     {\,\mathbf{do}\, e \,\mathbf{times}\, I, s \,\Longrightarrow, D e' \,\mathbf{times}\, I, s}
</math>
</math>


Linia 311: Linia 311:


<math>
<math>
\,\mathbf{do}\, n \,\mathbf{times}\, I, s \,\Longrightarrow\, I; \,\mathbf{do}\, n{-}1 \,\mathbf{times}\, I, s  
\,\mathbf{do}\, n \,\mathbf{times}\, I, s \,\Longrightarrow, I; \,\mathbf{do}\, n{-}1 \,\mathbf{times}\, I, s  
\quad \mbox{ o ile } n > 0
\quad \mbox{ o ile } n > 0
</math>
</math>


<math>
<math>
\,\mathbf{do}\, n \,\mathbf{times}\, I, s \,\Longrightarrow\, s  
\,\mathbf{do}\, n \,\mathbf{times}\, I, s \,\Longrightarrow, s  
\quad \mbox{ o ile } n = 0.
\quad \mbox{ o ile } n = 0.
</math>
</math>
Linia 330: Linia 330:


<math>
<math>
\frac{e_1, s \,\Longrightarrow\, e'_1, s}
\frac{e_1, s \,\Longrightarrow, e'_1, s}
     {\mathbf{for}\, x = e_1 \,\mathbf{to}\, e_2 \,\mathbf{do}\, I,s \,\Longrightarrow\, \mathbf{for}\, x = e'_1 \,\mathbf{to}\, e_2 \,\mathbf{do}\, I, s}
     {\mathbf{for}\, x = e_1 \,\mathbf{to}\, e_2 \,\mathbf{do}\, I,s \,\Longrightarrow, \mathbf{for}\, x = e'_1 \,\mathbf{to}\, e_2 \,\mathbf{do}\, I, s}
</math>
</math>


<math>
<math>
\frac{e_2, s \,\Longrightarrow\, e'_2, s}
\frac{e_2, s \,\Longrightarrow, e'_2, s}
     {\mathbf{for}\, x = n \,\mathbf{to}\, e_2 \,\mathbf{do}\, I,s \,\Longrightarrow\, \mathbf{for}\, x = n \,\mathbf{to}\, e'_2 \,\mathbf{do}\, I, s}
     {\mathbf{for}\, x = n \,\mathbf{to}\, e_2 \,\mathbf{do}\, I,s \,\Longrightarrow, \mathbf{for}\, x = n \,\mathbf{to}\, e'_2 \,\mathbf{do}\, I, s}
</math>
</math>


Linia 348: Linia 348:


<math>
<math>
\mathbf{for}\, x = n_1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s \,\Longrightarrow\, I; \mathbf{for}\, x = n_1+1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s[x \mapsto n_1]
\mathbf{for}\, x = n_1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s \,\Longrightarrow, I; \mathbf{for}\, x = n_1+1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s[x \mapsto n_1]
\quad \mbox{ o ile } n_1 \leq n_2
\quad \mbox{ o ile } n_1 \leq n_2
</math>
</math>


<math>
<math>
\mathbf{for}\, x = n_1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s \,\Longrightarrow\, s
\mathbf{for}\, x = n_1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s \,\Longrightarrow, s
\quad \mbox{ o ile } n_1 > n_2
\quad \mbox{ o ile } n_1 > n_2
</math>
</math>
Linia 362: Linia 362:
'''Pytanie:''' oto inna wersja jednej z powyższych reguł:  
'''Pytanie:''' oto inna wersja jednej z powyższych reguł:  
<math>
<math>
\mathbf{for}\, x = n_1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s \,\Longrightarrow\, I; \mathbf{for}\, x = n_1+1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s[x \mapsto s(x)+1]
\mathbf{for}\, x = n_1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s \,\Longrightarrow, I; \mathbf{for}\, x = n_1+1 \,\mathbf{to}\, n_2 \,\mathbf{do}\, I, s[x \mapsto s(x)+1]
\quad \mbox{ o ile } s(x) \leq n_2.
\quad \mbox{ o ile } s(x) \leq n_2.
</math>
</math>
Linia 376: Linia 376:


<math>
<math>
\,\mathbf{do}\, I \,\mathbf{while}\, b, s \,\Longrightarrow\, \mathbf{repeat}\, I \,\mathbf{until}\, \neg b, s.
\,\mathbf{do}\, I \,\mathbf{while}\, b, s \,\Longrightarrow, \mathbf{repeat}\, I \,\mathbf{until}\, \neg b, s.
</math>
</math>


Linia 409: Linia 409:


<math>
<math>
e_1 0 + e_2 0 \,\Longrightarrow\, (e_1 + e_2) 0
e_1 0 + e_2 0 \,\Longrightarrow, (e_1 + e_2) 0
</math>
</math>


<math>
<math>
e_1 0 + e_2 1 \,\Longrightarrow\, (e_1 + e_2) 1
e_1 0 + e_2 1 \,\Longrightarrow, (e_1 + e_2) 1
</math>
</math>


<math>
<math>
e_1 1 + e_2 0 \,\Longrightarrow\, (e_1 + e_2) 1
e_1 1 + e_2 0 \,\Longrightarrow, (e_1 + e_2) 1
</math>
</math>


Linia 423: Linia 423:


<math>
<math>
e_1 1 + e_2 1 \,\Longrightarrow\, ?
e_1 1 + e_2 1 \,\Longrightarrow, ?
</math>
</math>


Linia 429: Linia 429:


<math>
<math>
e_1 1 + e_2 1 \,\Longrightarrow\, ((e_1 + e_2) + \epsilon 1) 0
e_1 1 + e_2 1 \,\Longrightarrow, ((e_1 + e_2) + \epsilon 1) 0
</math>
</math>


Linia 456: Linia 456:


<math>
<math>
\epsilon + \epsilon \,\Longrightarrow\, \epsilon.
\epsilon + \epsilon \,\Longrightarrow, \epsilon.
</math>
</math>


Linia 462: Linia 462:


<math>
<math>
\epsilon + e 0 \,\Longrightarrow\, e 0
\epsilon + e 0 \,\Longrightarrow, e 0
\quad \quad
\quad \quad
\epsilon + e 1 \,\Longrightarrow\, e 1
\epsilon + e 1 \,\Longrightarrow, e 1
</math>
</math>


Linia 470: Linia 470:


<math>
<math>
e 0 + \epsilon \,\Longrightarrow\, e 0
e 0 + \epsilon \,\Longrightarrow, e 0
\quad \quad
\quad \quad
e 1 + \epsilon \,\Longrightarrow\, e 1.
e 1 + \epsilon \,\Longrightarrow, e 1.
</math>
</math>


Linia 478: Linia 478:


<math>
<math>
e_1 + e_2 \,\Longrightarrow\, e_2 + e_1
e_1 + e_2 \,\Longrightarrow, e_2 + e_1
</math>
</math>


Linia 486: Linia 486:


<math>
<math>
\frac{e_1 \,\Longrightarrow\, e'_1}
\frac{e_1 \,\Longrightarrow, e'_1}
     {e_1 + e_2 \,\Longrightarrow\, e'_1 + e_2}
     {e_1 + e_2 \,\Longrightarrow, e'_1 + e_2}
\quad \quad
\quad \quad
\frac{e_2 \,\Longrightarrow\, e'_2}
\frac{e_2 \,\Longrightarrow, e'_2}
     {e_1 + e_2 \,\Longrightarrow\, e_1 + e'_2}
     {e_1 + e_2 \,\Longrightarrow, e_1 + e'_2}
\quad \quad
\quad \quad
\frac{e \,\Longrightarrow\, e'}
\frac{e \,\Longrightarrow, e'}
     {e 0 \,\Longrightarrow\, e' 0}
     {e 0 \,\Longrightarrow, e' 0}
\quad \quad
\quad \quad
\frac{e \,\Longrightarrow\, e'}
\frac{e \,\Longrightarrow, e'}
     {e 1 \,\Longrightarrow\, e' 1}
     {e 1 \,\Longrightarrow, e' 1}
\quad \quad
\quad \quad
</math>
</math>
Linia 555: Linia 555:


<math>
<math>
p + e 0 \,\Longrightarrow\, e 0
p + e 0 \,\Longrightarrow, e 0
\quad \quad
\quad \quad
p + e 1 \,\Longrightarrow\, e 1
p + e 1 \,\Longrightarrow, e 1
\quad \quad
\quad \quad
e 0 + p \,\Longrightarrow\, e 0
e 0 + p \,\Longrightarrow, e 0
\quad \quad
\quad \quad
e 1 + p \,\Longrightarrow\, e 1.
e 1 + p \,\Longrightarrow, e 1.
</math>
</math>


Linia 568: Linia 568:


<math>
<math>
\epsilon + e 0 \,\Longrightarrow\, e 0
\epsilon + e 0 \,\Longrightarrow, e 0
\quad \quad
\quad \quad
\epsilon + e 1 \,\Longrightarrow\, e 1
\epsilon + e 1 \,\Longrightarrow, e 1
\quad \quad
\quad \quad
e 0 + \epsilon \,\Longrightarrow\, e 0
e 0 + \epsilon \,\Longrightarrow, e 0
\quad \quad
\quad \quad
e 1 + \epsilon \,\Longrightarrow\, e 1.
e 1 + \epsilon \,\Longrightarrow, e 1.
</math>
</math>


Linia 583: Linia 583:


<math>
<math>
p + p \,\Longrightarrow\, p.
p + p \,\Longrightarrow, p.
</math>
</math>


Ale co należy zrobić, gdy tylko jeden ze składników odnotował przepełnienie? <math>p + \epsilon \,\Longrightarrow\, ?</math>
Ale co należy zrobić, gdy tylko jeden ze składników odnotował przepełnienie? <math>p + \epsilon \,\Longrightarrow, ?</math>
Oczywiście, w tej sytuacji żadnego przepełnienia nie ma, ponieważ drugi składnik ma wystarczająco dużo cyfr, by je przesłonić.
Oczywiście, w tej sytuacji żadnego przepełnienia nie ma, ponieważ drugi składnik ma wystarczająco dużo cyfr, by je przesłonić.
Oto odpowiednie reguły:
Oto odpowiednie reguły:


<math>
<math>
p + \epsilon \,\Longrightarrow\, \epsilon
p + \epsilon \,\Longrightarrow, \epsilon
\quad \quad
\quad \quad
\epsilon + p \,\Longrightarrow\, \epsilon.
\epsilon + p \,\Longrightarrow, \epsilon.
</math>
</math>


Linia 603: Linia 603:


<math>
<math>
e_1 1 + e_2 1 \,\Longrightarrow\, ((e_1 + e_2) + p 1) 0.
e_1 1 + e_2 1 \,\Longrightarrow, ((e_1 + e_2) + p 1) 0.
</math>
</math>



Wersja z 14:16, 9 cze 2020

Zawartość

Ćwiczymy dalej semantykę małych kroków. Uzupełnimy semantykę języka Tiny o semantykę operacyjną wyrażeń boolowskich i arytmetycznych. Następnie rozszerzymy nasz język o róznorodne konstrukcje iteracji. Na koniec zdefiniujemy operacje arytmetyczne liczb binarnych.


Rozszerzenia semantyki języka Tiny

Ćwiczenie 1

Semantyka języka Tiny z wykładu używała funkcji semantycznych B,E:StateState dla określenia znaczenia wyrażeń boolowskich i arytmetycznych. Zdefiniuj znaczenie wyrażeń za pomocą semantyki operacyjnej, w stylu małych kroków.


Rozwiązanie


Ćwiczenie 2

Rozszerzmy język Tiny o następujące dobrze znane konstrukcje iteracji:

I::=𝐫𝐞𝐩𝐞𝐚𝐭I𝐮𝐧𝐭𝐢𝐥b|𝐟𝐨𝐫x:=e1𝐭𝐨e2𝐝𝐨I|𝐝𝐨e𝐭𝐢𝐦𝐞𝐬I|𝐝𝐨I𝐰𝐡𝐢𝐥𝐞b

Napisz semantykę małych kroków dla powyższych konstrukcji.


Rozwiązanie

Kalkulator binarny

Ćwiczenie 3

Rozważmy następujący język wyrażeń (liczby binarne z dodawaniem):

e::=ϵ|e0|e1|e1+e2

ϵ oznacza słowo puste, czyli np. ϵ1011 oznacza binarną liczbę 1011. Napisz semantykę operacyjną obliczającą wartość wyrażeń.


Rozwiązanie


Ćwiczenie 4

Rozszerzmy składnię o jeden symbol p oznaczający "przepełnienie":

e::=ϵ|p|e0|e1|e1+e2.

Na przykład p101 oznacza tę samą liczbę, co ϵ101, ale z dodatkową informacją, że podczas jej obliczania nastąpiło "przepełnienie". Rozumiemy przez to sytuację, gdy wynik ma więcej cyfr niż każdy z argumentów. Cyfry zero z lewej strony (najbardziej znaczące) również uważamy za pełnoprawne cyfry, nie należy ich usuwać ani dodawać nowych.

Napisz semantykę operacyjną obliczającą wartość wyrażenia wraz z informacja o ewentualnym przepełnieniu. Wynik powinien byc poprawny przynajmniej dla wyrażeń e w składni ograniczonej:

e::=b|e1+e2

b::=ϵ|b0|b1.

reprezentujących sumę liczb binarnych.


Rozwiązanie

Zadania domowe

Ćwiczenie 1

Podaj przykład wyrażenia boolowskiego, które nie policzy się ani przy użyciu strategii lewo-, ani prawostronnej, a policzy się przy strategii równoległej.


Ćwiczenie 2

Zmodyfikuj semantykę wyrażeń następująco: dla każdego podwyrażenia niedeterministycznie wybierana jest strategia lewo- albo prawostronna, ale niedozwolony jest "przeplot".


Ćwiczenie 3

Rozważ inną semantykę pętli 𝐟𝐨𝐫x=e1𝐭𝐨e2𝐝𝐨I, w której wyrażenie e2 jest obliczane na nowo przed każdą iteracją pętli.


Ćwiczenie 4

Dodaj do wyrażeń binarnych operację odejmowania.


Ćwiczenie 5

Zaproponuj semantykę przypisania równoległego w języku TINY:

x1:=e1||||xn:=en

polegającego na obliczeniu najpierw wartości wyrażeń e1,,en, a następnie na podstawieniu tych wartości na zmienne x1,,xn.