Semantyka i weryfikacja programów/Ćwiczenia 1: Różnice pomiędzy wersjami
m Zastępowanie tekstu - "\Longrightarrow\" na "\Longrightarrow" |
|||
Linia 50: | Linia 50: | ||
<math> | <math> | ||
e, s \,\Longrightarrow | e, s \,\Longrightarrow, e', s | ||
</math> | </math> | ||
Linia 59: | Linia 59: | ||
<math> | <math> | ||
e, s \,\Longrightarrow | e, s \,\Longrightarrow, n | ||
</math> | </math> | ||
Linia 80: | Linia 80: | ||
<math> | <math> | ||
n, s \,\Longrightarrow | n, s \,\Longrightarrow, n | ||
</math> | </math> | ||
Linia 88: | Linia 88: | ||
<math> | <math> | ||
x, s \,\Longrightarrow | x, s \,\Longrightarrow, n, s \quad \mbox{ o ile } s(x) = n. | ||
</math> | </math> | ||
Linia 95: | Linia 95: | ||
<math> | <math> | ||
e_1 + e_2, s \,\Longrightarrow | e_1 + e_2, s \,\Longrightarrow, e'_1 + e_2, s | ||
\quad \mbox{ o ile } \quad | \quad \mbox{ o ile } \quad | ||
e_1, s \,\Longrightarrow | e_1, s \,\Longrightarrow, e'_1, s. | ||
</math> | </math> | ||
Linia 103: | Linia 103: | ||
<math> | <math> | ||
\frac{e_1, s \,\Longrightarrow | \frac{e_1, s \,\Longrightarrow, e'_1, s} | ||
{e_1 + e_2, s \,\Longrightarrow | {e_1 + e_2, s \,\Longrightarrow, e'_1 + e_2, s} | ||
</math> | </math> | ||
Linia 111: | Linia 111: | ||
<math> | <math> | ||
\frac{e_2, s \,\Longrightarrow | \frac{e_2, s \,\Longrightarrow, e'_2, s} | ||
{n + e_2, s \,\Longrightarrow | {n + e_2, s \,\Longrightarrow, n + e'_2, s}. | ||
</math> | </math> | ||
Linia 118: | Linia 118: | ||
<math> | <math> | ||
n_1 + n_2, s \,\Longrightarrow | n_1 + n_2, s \,\Longrightarrow, n, s \quad \mbox{ o ile } n = n_1 + n_2. | ||
</math> | </math> | ||
Linia 125: | Linia 125: | ||
<math> | <math> | ||
\mathrm{add}(n_1, n_2), s \,\Longrightarrow | \mathrm{add}(n_1, n_2), s \,\Longrightarrow, n, s \quad \mbox{ o ile } n = n_1 + n_2. | ||
</math> | </math> | ||
Linia 131: | Linia 131: | ||
<math> | <math> | ||
\frac{e_2, s \,\Longrightarrow | \frac{e_2, s \,\Longrightarrow, e'_2, s} | ||
{e_1 + e_2, s \,\Longrightarrow | {e_1 + e_2, s \,\Longrightarrow, e_1 + e'_2} | ||
\quad \quad | \quad \quad | ||
\frac{e_1, s \,\Longrightarrow | \frac{e_1, s \,\Longrightarrow, e'_1, s} | ||
{e_1 + n, s \,\Longrightarrow | {e_1 + n, s \,\Longrightarrow, e'_1 + n, s}. | ||
</math> | </math> | ||
Linia 141: | Linia 141: | ||
<math> | <math> | ||
\frac{e_1, s \,\Longrightarrow | \frac{e_1, s \,\Longrightarrow, e'_1, s} | ||
{e_1 + e_2, s \,\Longrightarrow | {e_1 + e_2, s \,\Longrightarrow, e'_1 + e_2, s} | ||
\quad \quad | \quad \quad | ||
\frac{e_2, s \,\Longrightarrow | \frac{e_2, s \,\Longrightarrow, e'_2, s} | ||
{e_1 + e_2, s \,\Longrightarrow | {e_1 + e_2, s \,\Longrightarrow, e_1 + e'_2} | ||
\quad \quad | \quad \quad | ||
n_1 + n_2, s \,\Longrightarrow | n_1 + n_2, s \,\Longrightarrow, n, s \quad \mbox{ o ile } n = n_1 + n_2. | ||
</math> | </math> | ||
Linia 155: | Linia 155: | ||
<math> | <math> | ||
e_1 + e_2, s \,\Longrightarrow | e_1 + e_2, s \,\Longrightarrow, e'_1 + e_2, s | ||
\quad \quad \quad | \quad \quad \quad | ||
e_1 + e_2, s \,\Longrightarrow | e_1 + e_2, s \,\Longrightarrow, e_1 + e'_2, s. | ||
</math> | </math> | ||
Linia 165: | Linia 165: | ||
<math> | <math> | ||
\frac{e_1, s \,\Longrightarrow | \frac{e_1, s \,\Longrightarrow, e'_1, s} | ||
{\mathbf{if}\, e_1 \,\mathbf{then}\, e_2 \,\mathbf{else}\, e_3, s \,\Longrightarrow | {\mathbf{if}\, e_1 \,\mathbf{then}\, e_2 \,\mathbf{else}\, e_3, s \,\Longrightarrow, \mathbf{if}\, e'_1 \,\mathbf{then}\, e_2 \,\mathbf{else}\, e_3, s} | ||
</math> | </math> | ||
<math> | <math> | ||
\mathbf{if}\, n \,\mathbf{then}\, e_2 \,\mathbf{else}\, e_3, s \,\Longrightarrow | \mathbf{if}\, n \,\mathbf{then}\, e_2 \,\mathbf{else}\, e_3, s \,\Longrightarrow, e_2, s \quad \mbox{ o ile } n \neq 0 | ||
</math> | </math> | ||
<math> | <math> | ||
\mathbf{if}\, n \,\mathbf{then}\, e_2 \,\mathbf{else}\, e_3, s \,\Longrightarrow | \mathbf{if}\, n \,\mathbf{then}\, e_2 \,\mathbf{else}\, e_3, s \,\Longrightarrow, e_3, s \quad \mbox{ o ile } n = 0 | ||
</math> | </math> | ||
Linia 249: | Linia 249: | ||
<math> | <math> | ||
e, s \,\Longrightarrow | e, s \,\Longrightarrow, e', s'. | ||
</math> | </math> | ||
Linia 259: | Linia 259: | ||
<math> | <math> | ||
x, s \,\Longrightarrow | x, s \,\Longrightarrow, n, s \quad \mbox{ o ile } s(x) \mbox{ jest określone i } s(x) = n | ||
</math> | </math> | ||
Linia 266: | Linia 266: | ||
<math> | <math> | ||
\mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow | \mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow, e_2, s[x \mapsto n]. | ||
</math> | </math> | ||
Linia 285: | Linia 285: | ||
<math> | <math> | ||
\frac{e_1, s \,\Longrightarrow | \frac{e_1, s \,\Longrightarrow, e'_1, s'} | ||
{\mathbf{let}\, x = e_1 \,\mathbf{in}\, e_2, s \,\Longrightarrow | {\mathbf{let}\, x = e_1 \,\mathbf{in}\, e_2, s \,\Longrightarrow, \mathbf{let}\, x = e'_1 \,\mathbf{in}\, e_2, s'} | ||
</math> | </math> | ||
Linia 304: | Linia 304: | ||
<math> | <math> | ||
\mathbf{let}\, x = (\mathbf{let}\, z = 4 \,\mathbf{in}\, z+z+z) \,\mathbf{in}\, z, \emptyset \,\Longrightarrow | \mathbf{let}\, x = (\mathbf{let}\, z = 4 \,\mathbf{in}\, z+z+z) \,\mathbf{in}\, z, \emptyset \,\Longrightarrow, | ||
\mathbf{let}\, x = z+z+z \,\mathbf{in}\, z, \emptyset[z \mapsto 4] \,\Longrightarrow | \mathbf{let}\, x = z+z+z \,\mathbf{in}\, z, \emptyset[z \mapsto 4] \,\Longrightarrow, \quad \ldots \quad \,\Longrightarrow, | ||
\mathbf{let}\, x = 12 \,\mathbf{in}\, z, \emptyset[z \mapsto 4] \,\Longrightarrow | \mathbf{let}\, x = 12 \,\mathbf{in}\, z, \emptyset[z \mapsto 4] \,\Longrightarrow, | ||
12, \emptyset[z \mapsto 4] \,\Longrightarrow | 12, \emptyset[z \mapsto 4] \,\Longrightarrow, | ||
12 ! | 12 ! | ||
</math> | </math> | ||
Linia 322: | Linia 322: | ||
<math> | <math> | ||
\mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow | \mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow, e_2, s[x \mapsto n]. | ||
</math> | </math> | ||
Linia 328: | Linia 328: | ||
<math> | <math> | ||
\mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow | \mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow, e_2 \,\mathbf{then}\, \mbox{"przywróć wartość zmiennej x"}, s[x \mapsto n]. | ||
</math> | </math> | ||
Linia 344: | Linia 344: | ||
<math> | <math> | ||
\mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow | \mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow, e_2 \,\mathbf{then}\, x := n', s[x \mapsto n] \quad | ||
\mbox{ o ile } s(x) = n'. | \mbox{ o ile } s(x) = n'. | ||
</math> | </math> | ||
Linia 362: | Linia 362: | ||
<math> | <math> | ||
\mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow | \mathbf{let}\, x = n \,\mathbf{in}\, e_2, s \,\Longrightarrow, e_2 \,\mathbf{then}\, x := \bot, s[x \mapsto n] \quad | ||
\mbox{ o ile } s(x) \, \mbox{ jest nieokreślone}. | \mbox{ o ile } s(x) \, \mbox{ jest nieokreślone}. | ||
</math> | </math> | ||
Linia 370: | Linia 370: | ||
<math> | <math> | ||
\frac{e, s \,\Longrightarrow | \frac{e, s \,\Longrightarrow, e', s'} | ||
{e \,\mathbf{then}\, x := n, s \,\Longrightarrow | {e \,\mathbf{then}\, x := n, s \,\Longrightarrow, e' \,\mathbf{then}\, x:= n, s'} | ||
</math> | </math> | ||
<math> | <math> | ||
n' \,\mathbf{then}\, x := n, s \,\Longrightarrow | n' \,\mathbf{then}\, x := n, s \,\Longrightarrow, n', s[x \mapsto n] | ||
</math> | </math> | ||
<math> | <math> | ||
n' \,\mathbf{then}\, x := \bot, s \,\Longrightarrow | n' \,\mathbf{then}\, x := \bot, s \,\Longrightarrow, n', s' \quad \mbox{ o ile } s(x) | ||
\mbox{ jest określone i } s' = s \setminus \{ (x, s(x)) \} | \mbox{ jest określone i } s' = s \setminus \{ (x, s(x)) \} | ||
</math> | </math> | ||
Linia 417: | Linia 417: | ||
<math> | <math> | ||
e, s \,\Longrightarrow | e, s \,\Longrightarrow, e', s' | ||
</math> | </math> | ||
Linia 424: | Linia 424: | ||
<math> | <math> | ||
e, s \,\Longrightarrow | e, s \,\Longrightarrow, e', s ? | ||
</math> | </math> | ||
Linia 430: | Linia 430: | ||
<math> | <math> | ||
\frac{e_1, s \,\Longrightarrow | \frac{e_1, s \,\Longrightarrow, e'_1, s} | ||
{\mathbf{let}\, x = e_1 \,\mathbf{in}\, e_2, s \,\Longrightarrow | {\mathbf{let}\, x = e_1 \,\mathbf{in}\, e_2, s \,\Longrightarrow, \mathbf{let}\, x = e'_1 \,\mathbf{in}\, e_2, s} | ||
</math> | </math> | ||
Linia 439: | Linia 439: | ||
<math> | <math> | ||
\frac{e, s[x \mapsto n] \,\Longrightarrow | \frac{e, s[x \mapsto n] \,\Longrightarrow, e', s[x \mapsto n] } | ||
{\mathbf{let}\, x = n \,\mathbf{in}\, e, s \,\Longrightarrow | {\mathbf{let}\, x = n \,\mathbf{in}\, e, s \,\Longrightarrow, \mathbf{let}\, x = n \,\mathbf{in}\, e', s}. | ||
</math> | </math> | ||
Linia 450: | Linia 450: | ||
<math> | <math> | ||
\mathbf{let}\, x = n \,\mathbf{in}\, n', s \,\Longrightarrow | \mathbf{let}\, x = n \,\mathbf{in}\, n', s \,\Longrightarrow, n', s | ||
</math> | </math> | ||
Wersja z 14:16, 9 cze 2020
Zawartość
Tematem tych zajęć jest semantyka operacyjna wyrażeń (małe kroki).
Semantyka operacyjna wyrażeń
Ćwiczenie 1
Rozważmy bardzo prosty język wyrażeń, którego składnia opisana jest następującą gramatyką:
Wynikiem wyrażenienia warunkowego jest wartość wyrażenia , o ile wyrażenie oblicza się do wartości różnej od zera; w przeciwnym przypadku wynikiem jest wartość wyrażenia .
Zaproponuj semantykę operacyjną (małe kroki) dla tego języka.
Rozwiązanie
Ćwiczenie 2
Rozszerzmy język wyrażeń z poprzedniego zadania o jedną konstrukcję
Wyrażenie zawiera w sobie deklarację , która stanowi mechanizm wiązania identyfikatorów w naszym języku. Deklaracja wprowadza nową zmienną oraz przypisuje jej wartość. Wartość wyrażenia obliczamy następująco: najpierw oblicza się wartość , podstawia ją za zmienną , a następnie oblicza wyrażenie . Zakresem zmiennej jest wyrażenie , czyli wewnątrz można odwoływać się (wielokrotnie) do zmiennej ; Ogólniej, odwołania do zmiennej w wyrażeniu odnoszą się do "najbliższej" (najbardziej zagnieżdzonej) deklaracji tej zmiennej. Taki mechanizm wiązania identyfikatorów nazywamy wiązaniem statycznym. Przyjmujemy zwykłe (statyczne) reguły przesłaniania zmiennych, np. jeśli w występuje podwyrażenie , to deklaracja "przesłania" deklarację w wyrażeniu .
Zakładamy, że na początku wartości wszystkich zmiennych są nieokreślone, czyli zmienne są niezainicjowane, a odwołanie do niezainicjowanej zmiennej jest uważane za niepoprawne.
{{przyklad|||
Parser nie mógł rozpoznać (błąd składni): {\displaystyle \mathbf{let}\, z = 5 \,\mathbf{in}\, x+z \quad \quad \mapsto \quad \quad \mbox{brak wyniku; odwołanie do niezainicjowanej zmiennej}\, x }
Rozwiązanie
Zadania domowe
Ćwiczenie 1
Zapisz wariant 2 semantyki z poprzedniego zadania.
Ćwiczenie 2
Dotychczas wystąpienie błędu podczas obliczania wyrażenia, np. odwołanie do niezainicjowanej zmiennej, powodowało, że wyrażenie nie posiadało wartości (nie było ciągu tranzycji prowadzących do konfiguracji końcowej). Zmodyfikuj którąś z semantyk z poprzednich zadań tak, aby błąd był komunikowany jako jedna z konfiguracji końcowych. To znaczy: jeśli obliczenie wyrażenia w stanie jest niemożliwe bo wystąpił błąd, to
Ćwiczenie 3
Rozważ rozszerzenie języka wyrażeń o wyrażenia boolowskie:
Zaproponuj semantykę małych kroków dla tego języka. Rozważ różne strategie obliczania wyrażeń boolowskich, oraz podejście leniwe. Na przykład w strategii lewostronnej dla , gdy zostało obliczone do , w podejściu leniwym nie ma wogóle potrzeby obliczania .