-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcap2.tex
1246 lines (1131 loc) · 60.3 KB
/
cap2.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\cleardoublepage
\chapter{Semántica operacional}
$\\$
En el anterior capítulo hemos visto cómo dar un valor semántico al lenguaje While mediante el punto de vista de la semántica denotacional. Centrémonos ahora en la semántica operacional. La distinción fundamental que hacíamos de este enfoque es la siguiente:
\begin{itemize}
\item Semántica operacional \textit{natural}, que describe cómo se han obtenido los resultados generales de las ejecuciones.
\item Semántica operacional \textit{estructural}, que describe cómo se ha obtenido cada paso en la ejecución.
\end{itemize}
Para ambos tipos de semántica operacional, el valor semántico de cada expresión será especificado por un \textit{sistema de transiciones}, compuesto de dos configuraciones distintas:
\begin{itemize}
\item[] $\langle S, s\rangle$, que denota que la expresión $S$ se ejecutará desde el estado $s$.
\item[] $s$, que denota un estado terminal. Las \textit{configuraciones terminales} tendrán esta forma.
\end{itemize}
Finalmente, es necesaria una \textit{relación de transición} que describa cómo tiene lugar la ejecución. La diferencia entre las dos semánticas se encuentra principalmente en ésta. De hecho, veremos que ambos tipos de semántica son, en cierto sentido, equivalentes.
\section{Semántica operacional natural}
\subsection{Sistema de transiciones}
La relación de transición $\langle S, s\rangle \rightarrow s'$ se puede leer como que, la ejecución de $S$ desde el estado $s$ terminará y el nuevo estado será $s'$. Está determinada por las siguientes reglas\footnote{Nótese que las variables $S_1, S_2, s, s', s''$, etc. son libres.}:
\begin{sist*}[$\nn{While}_\nn{ns}$]\mbox{}
\begin{itemize}
\item[] $[\text{ass}_{\text{ns}}]$
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\UnaryInfC{ $\langle x:= a, s\rangle \rightarrow s[x\mapsto \fc{A}{a}s]$}
\DisplayProof
\end{center}
\item[] $[\text{skip}_{\text{ns}}]$
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\UnaryInfC{$\la{\n{skip}}{s}\rightarrow s$}
\DisplayProof
\end{center}
\item[][$\text{comp}_{\text{ns}}$]
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S_1}{s} \to s'$}
\AxiomC{$\la{S_2}{s'} \to s''$}
\BinaryInfC{$\la{S_1; S_2}{s} \to s''$}
\DisplayProof
\end{center}
\newpage
\item[][$\text{if}^{\text{tt}}_{\text{ns}}$]
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S_1}{s} \to s'$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then}\ S_1\ \n{else}\ S_2}{s} \to s'$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s = \mathbf{tt}$
\end{center}
\item[] [$\text{if}^{\text{ff}}_{\text{ns}}$]
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S_2}{s} \to s'$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then}\ S_1\ \n{else}\ S_2}{s} \to s'$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s = \mathbf{ff}$
\end{center}
\item[][$\text{while}^{\text{tt}}_{\text{ns}}$]
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S}{s} \to s'$}
\AxiomC{$\la{\n{while}\ b\ \n{do}\ S}{s'} \to s''$}
\BinaryInfC{$\la{\n{while}\ b\ \n{do}\ S}{s} \to s''$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s = \mathbf{tt}$
\end{center}
\item[] [$\text{while}^{\text{ff}}_{\text{ns}}$]
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\UnaryInfC{$\la{\n{while}\ b\ \n{do}\ S}{s} \to s$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s = \mathbf{ff}$
\end{center}
\end{itemize}
\end{sist*}
Aclaremos un poco la terminología:
\begin{definition}
Una \textit{regla} en general tiene la forma general
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S_1}{s_1}\rightarrow s'_1 \dots \la{S_n}{s_n}\rightarrow s'_n $}
\UnaryInfC{$\la{S}{s}\rightarrow s'$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si } \varphi$
\end{center}
donde los términos que aparecen encima y bajo la línea son, respectivamente, las \textit{premisas} y la \textit{conclusión}, y donde $\varphi$ es la \textit{condición}. Cuando empleamos las reglas anteriores para obtener una transición $\la{S}{s}\rightarrow s'$, obtenemos un \textit{árbol de derivación}. Una regla sin premisas se llama \textit{axioma}.
\end{definition}
Consideremos el problema de construir un árbol de derivación para una expresión $S$ y un estado $s$. El método general consiste en partir de la `raíz' y encontrar las `hojas', es decir, el paso inicial consiste en buscar una regla de modo que su conclusión tenga la ejecución $\la{S}{s}$ en su parte izquierda. Los pasos inductivos son:
\begin{itemize}
\item Si la regla encontrada es un axioma, entonces podemos determinar el estado terminal y terminamos.
\item Si la regla encontrada no es un axioma, entonces el siguiente paso consiste en buscar un árbol de derivación para sus premisas.
\end{itemize}
Nótese que, en cada paso, las condiciones para aplicar cada regla tienen que ser verificadas. En el futuro demostraremos algo que parece falso a primera vista: que en el lenguaje While hay a lo sumo un árbol de derivación posible para cada ejecución $\la{S}{s}$.
\begin{definition}
Decimos que una ejecución de la expresión $S$ desde el estado $s$, $\la{S}{s}$, \textit{termina} si existe un estado $s'$ tal que $\la{S}{s}\rightarrow s'$. Si tal estado no existe entonces decimos que la ejecución \textit{cicla}. Para una expresión $S$, decimos que \textit{siempre termina} si $\la{S}{s}$ termina para cada elección de $s$ y que \textit{siempre cicla} si $\la{S}{s}$ cicla para cada elección de $s$.
\end{definition}
\begin{example}
Podemos tratar de determinar si las siguientes expresiones terminan o ciclan siempre:
\begin{enumerate}
\item $\n{while} \, \neg(\n{x}=1) \, \n{do} \, (\n{y} := \n{y} \times \n{x}; \n{x} := \n{x} - 1)$.
\item $\n{while} \, 1 \leq \n{x} \, \n{do} \, (\n{y} := \n{y} \times \n{x}; \n{x} := \n{x} - 1)$.
\item $\n{while true do skip}$.
\end{enumerate}
La primera para si se inicializa $x$ con un valor mayor o igual que $1$ y cicla en caso contrario: nótese que si $\n{x}<1$ decrecerá continuamente, luego es imposible que la condición del bucle llegue a no cumplirse.\\ \\
La segunda para siempre. Es parecida a la anterior salvo por el hecho de que la condición del bucle hace que, para $\n{x}<1$, se pare. \\ \\
La tercera dejaría intacta la configuración inicial. Sin embargo, la ejecución cicla pese a no realizar ninguna acción. Esto es porque la condición del bucle es siempre cierta.
\end{example}
\subsection{Propiedades}
El sistema de transición nos da un entorno en el que estudiar las propiedades de las expresiones. Veamos a continuación una definición precisa de un concepto que introdujimos al final de la introducción:
\begin{definition}
Dos expresiones $S_1, S_2$ se dicen \textit{semánticamente equivalentes} si para cada par $s, s' \in \mathbf{State}$,
$$\la{S_1}{s}\rightarrow s' \text{ si y solo si } \la{S_2}{s}\rightarrow s'.$$
\end{definition}
\begin{lema}\label{lemans}
\normalfont $\n{while } b \n{ do } S$ \textit{es semánticamente equivalente a} $\n{if } b \n{ then } (S; \n{while } b \n{ do } S)\n{ else skip}$.
\end{lema}
\begin{proof}
Dividimos la prueba en dos implicaciones:
\noindent\textit{Parte 1.} Supongamos que se cumple $\la{\n{while } b \n{ do } S}{s}\rightarrow s''$. Entonces existe un árbol de derivación para él, $T$. $T$ puede tener dos formas en función de la regla que hayamos aplicado: o bien hemos aplicado la regla o el axioma [$\text{while}^{\text{ff}}_{\text{ns}}$]. Veamos cada caso:
\\
\noindent$(a)$ Si hemos aplicado la regla [$\text{while}^{\text{tt}}_{\text{ns}}$], $T$ es de la forma:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\dots$}
\UnaryInfC{$\la{S}{s}\rightarrow s'$}
\AxiomC{$\dots$}
\UnaryInfC{$\la{\n{while}\ b\ \n{do}\ S}{s'} \to s''$}
\BinaryInfC{$\la{\n{while } b \n{ do } S}{s}\rightarrow s''$}[$\text{while}^{\text{tt}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
con $\fc{B}{b}s = \mathbf{tt}$. Ahora bien, notemos que:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\dots$}
\UnaryInfC{$\la{S}{s}\rightarrow s'$}
\AxiomC{$\dots$}
\UnaryInfC{$\la{\n{while}\ b\ \n{do}\ S}{s'} \to s''$}
\BinaryInfC{$\la{S;\n{while } b \n{ do } S}{s}\rightarrow s''$}$[\text{comp}_{\text{ns}}]$
\DisplayProof
\quad
\centerAlignProof
\end{center}
Usando que $\fc{B}{b}s = \mathbf{tt}$, podemos aplicar:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S;\n{while } b \n{ do } S}{s}\rightarrow s''$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then}\ (S;\n{while } b \n{ do } S)\ \n{else}\ \n{skip}}{s} \to s''$}[$\text{if}^{\text{ff}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
Y por tanto, $\la{\n{if}\ b\ \n{then}\ (S;\n{while } b \n{ do } S)\ \n{else}\ \n{skip}}{s} \to s''$.
\\
\noindent $(b)$ Si hemos aplicado la regla [$\text{while}^{\text{ff}}_{\text{ns}}$], $T$ es de la forma:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\UnaryInfC{$\la{\n{while}\ b\ \n{do}\ S}{s} \to s$}
\DisplayProof
\quad
\centerAlignProof
\end{center}
es decir, necesariamente $s=s''$ y $\fc{B}{b}s = \mathbf{ff}$. Usando el axioma $[\text{skip}_{\text{ns}}]$, directamente obtenemos que
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\UnaryInfC{$\la{\n{skip}}{s}\rightarrow s''$}$[\text{skip}_{\text{ns}}]$
\DisplayProof
\quad
\centerAlignProof
\end{center}
Pero entonces,
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{\n{while}\ b\ \n{do}\ S}{s} \to s$}
\AxiomC{$\la{\n{skip}}{s}\rightarrow s''$}
\BinaryInfC{$\la{\n{if}\ b\ \n{then}\ (S;\n{while } b \n{ do } S)\ \n{else}\ \n{skip}}{s} \to s''$}[$\text{if}^{\text{ff}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
y por tanto obtenemos el resultado.
\\
\noindent\textit{Parte 2.} Supongamos ahora que se cumple $\la{\n{if}\ b\ \n{then}\ (S;\n{while } b \n{ do } S)\ \n{else}\ \n{skip}}{s} \to s''$. Entonces, tenemos un árbol de derivación $T$ y, de nuevo, podemos distinguir qué forma tendrá según las reglas que hayamos aplicado:
\\
\noindent $(a)$ Si hemos aplicado la regla [$\text{if}^{\text{tt}}_{\text{ns}}$], $T$ es de la forma:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\dots$}
\UnaryInfC{$\la{S; \n{while } b \n{ do } s}{s} \to s''$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then}\ (S;\n{while } b \n{ do } S)\ \n{else}\ \n{skip}}{s} \to s''$}[$\text{if}^{\text{tt}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
y con $\fc{B}{b}s = \mathbf{tt}$. Ahora bien, solo hemos podido obtener la premisa anterior mediante $[\text{comp}_{\text{ns}}]$, por tener una expresión de la forma $S_1; S_2$ en la ejecución. Entonces deducimos que $T$ es:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\dots$}
\UnaryInfC{$\la{S}{s}\rightarrow s'$}
\AxiomC{$\dots$}
\UnaryInfC{$\la{\n{while } b \n{ do } S}{s'}\rightarrow s''$}
\BinaryInfC{$\la{S; \n{while } b \n{ do } s}{s} \to s''$}[$\text{comp}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
Pero entonces notemos que, usando la hipótesis $\fc{B}{b}s = \mathbf{tt}$:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\dots$}
\UnaryInfC{$\la{S}{s}\rightarrow s'$}
\AxiomC{$\dots$}
\UnaryInfC{$\la{\n{while } b \n{ do } S}{s'}\rightarrow s''$}
\BinaryInfC{$\la{\n{while}\ b\ \n{do}\ S}{s} \to s''$}[$\text{while}^{\text{tt}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
y obtenemos el resultado.
\\
\noindent $(b)$ Si hemos usado la regla [$\text{if}^{\text{ff}}_{\text{ns}}$], deducimos que $\fc{B}{b}s = \mathbf{ff}$ y que por tanto tenemos un árbol de derivación para $\la{\n{skip}}{s}\rightarrow s''$ y, por tanto, que $s=s''$. Pero usando [$\text{while}^{\text{ff}}_{\text{ns}}$], tenemos el resultado (el razonamiento ha sido análogo al apartado $(b)$ de la Parte 1).
\end{proof}
\begin{example}
Veamos que $S_1; (S_2; S_3)$ y $(S_1; S_2); S_3$ son semánticamente equivalentes. Si suponemos que $\la{S_1; (S_2; S_3)}{s}\rightarrow s'$, entonces es porque en su árbol de derivación hemos empleado [$\text{comp}_{\text{ns}}$] a las premisas $\la{S_1}{s}\rightarrow s''$ y $\la{S_2;S_3}{s''}\rightarrow s'$. A su vez, la segunda premisa proviene del mismo modo de las premisas $\la{S_2}{s''}\rightarrow t$ y $\la{S_3}{t}\rightarrow s'$. Es decir, tenemos las siguientes hojas:
\begin{itemize}
\item[(a)] $\la{S_1}{s}\rightarrow s''$.
\item[(b)] $\la{S_2}{s''}\rightarrow t$.
\item[(c)] $\la{S_3}{t}\rightarrow s'$.
\end{itemize}
Ahora, combinando (a) y (b) con [$\text{comp}_{\text{ns}}$], obtenemos $\la{S_1;S_2}{s}\rightarrow t$ y, combinando esto con (c) de la misma forma, obtenemos que $\la{(S_1; S_2);S_3}{s}\rightarrow s'$, como queríamos ver. La otra implicación es análoga.
Notemos, por otro lado, que en general $S_1;S_2$ y $S_2; S_1$ no son semánticamente equivalentes: si tratásemos de hacer lo mismo que antes, obtendríamos las hojas $\la{S_1}{s}\rightarrow s''$ y $\la{S_2}{s''}\rightarrow s'$ por un lado y $\la{S_2}{s}\rightarrow s''$ y $\la{S_1}{s''}\rightarrow s'$ por otro, y en general no hay forma de combinar cada par de premisas para obtener la conclusión deseada.
\end{example}
\begin{example}\textbf{}
Podemos expandir el sistema $\text{While}_\text{ns}$ el siguiente modo: añadimos dos reglas que permitan dar una semántica de la expresión $\n{for }x:= a_1 \n{ to } a_2\n{ do }S$, es decir,
\begin{itemize}
\item[]
\begin{center}
\centerAlignProof
[$\text{for}_{\text{ns}}^{\text{tt}}$]
\quad
\centerAlignProof
\AxiomC{$\la{x:= a_1;S}{s}\rightarrow s'$}
\AxiomC{$\la{\n{for } \n{x}:= \n{x}+\n{1 to } a_2 \n{ do }S}{s'}\to s''$}
\BinaryInfC{$\la{\n{for } \n{x}:= a_1\n{ to } a_2 \n{ do }S}{s}\to s''$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{a_1\leq a_2}s = \mathbf{tt}$
\end{center}
\item[]
\begin{center}
\centerAlignProof
[$\text{for}_{\text{ns}}^{\text{ff}}$]
\quad
\centerAlignProof
\AxiomC{}
\UnaryInfC{$\la{\n{for } \n{x}:= a_1\n{ to } a_2 \n{ do }S}{s}\to s[\n{x}\mapsto \fc{A}{a_1}]s$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{a_1\leq a_2}s = \mathbf{ff}$
\end{center}
\end{itemize}
Pero debemos tener un especial cuidado con este tipo de reglas, por ejemplo, podemos descuidar que en $a_1$ aparezca la variable $\n{y}$, a saber, que $a_1$ contenga $\n{y} + 3$, y que por otro lado en $S$ tengamos $\n{y}=5$. Del mismo modo, podríamos tener que la variable $\n{x}$ ya aparece del mismo modo como $\n{x} = 4$, por ejemplo. Si $\n{x}$ apareciera en $a_2$ entonces también tendríamos este problema.
\end{example}
Aunque no lo demostraremos, se puede observar que el sistema $\text{While}_\text{ns}$ es \textit{Turing-completo}, es decir, en él podemos simular cualquier computación posible en una máquina de Turing. Por tanto, se podía pensar que podemos introducir reglas para ciertas expresiones en función de su correlato en $\text{While}_\text{ns}$ (que existe, por lo anterior). Sin embargo, si quisiéramos introducir $\n{for }x:= a_1 \n{ to } a_2\n{ do }S$ como un bucle $\n{while ... do ...}$, acabaríamos teniendo apariciones de $\n{while ... do ...}$ en las reglas asociadas a $\n{for }x:= a_1 \n{ to } a_2\n{ do }S$, lo que difiere de la semántica operacional que hemos visto hasta ahora.
\\
\begin{example}
Podríamos extender el lenguaje While con dos reglas para la expresión $\n{repeat } S \n{ until } b$:
\begin{itemize}
\item[]
\begin{center}
\centerAlignProof
[$\text{repeat}_{\text{ns}}^{\text{tt}}$]
\quad
\centerAlignProof
\AxiomC{$\la{S}{s}\rightarrow s'$}
\UnaryInfC{$\la{\n{repeat } S \n{ until } b}{s}\rightarrow s'$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s' = \mathbf{tt}$
\end{center}
\item[]
\begin{center}
\centerAlignProof
[$\text{repeat}_{\text{ns}}^{\text{ff}}$]
\quad
\centerAlignProof
\AxiomC{$\la{S}{s}\rightarrow s'$}
\AxiomC{$\la{\n{repeat } S \n{ until } b}{s'}\rightarrow s''$}
\BinaryInfC{$\la{\n{repeat } S \n{ until } b}{s}\rightarrow s''$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s' = \mathbf{ff}$
\end{center}
\end{itemize}
\end{example}
\begin{prop}
Son semánticamente equivalentes:
\begin{itemize}
\item $\n{repeat } S \n{ until } b$.
\item $S; \n{if } b \n{ then skip else } (\n{repeat } S \n{ until } b)$.
\end{itemize}
\end{prop}
\begin{proof}
\noindent\textit{Parte 1.} Supongamos que $\la{\n{repeat } S \n{ until } b}{s}\rightarrow s'$. Solo tenemos las siguientes posibilidades:
\\
\noindent\textit{(a)} Si hemos aplicado la regla [$\text{repeat}_{\text{ns}}^{\text{tt}}$], tenemos:
\begin{center}
\centerAlignProof
[$\text{repeat}_{\text{ns}}^{\text{tt}}$]
\quad
\centerAlignProof
\AxiomC{$\la{S}{s}\rightarrow s'$}
\UnaryInfC{$\la{\n{repeat } S \n{ until } b}{s}\rightarrow s'$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s' = \mathbf{tt}$
\end{center}
Ahora bien, por otro lado, podemos aplicar el axioma [$\text{skip}_{\text{ns}}$] para obtener directamente que $\la{\n{skip}}{s'}\rightarrow s'$. Ahora, como $\text{ si }\fc{B}{b}s' = \mathbf{tt}$, podemos aplicar la regla [$\text{if}^{\text{tt}}_{\text{ns}}$]:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{\n{skip}}{s'}\rightarrow s'$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s'} \to s'$}[$\text{if}^{\text{tt}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
Y, entonces,
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S}{s} \to s'$}
\AxiomC{$\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s'} \to s'$}
\BinaryInfC{$\la{S; \n{if } b \n{ then skip else } (\n{repeat } S \n{ until } b)}{s} \to s'$}[$\text{comp}_{\text{ns}}$]
\DisplayProof
\end{center}
Luego obtenemos el resultado.
\\
\noindent\textit{(b)} Si hemos aplicado la regla [$\text{repeat}_{\text{ns}}^{\text{ff}}$], tenemos:
\begin{center}
\centerAlignProof
[$\text{repeat}_{\text{ns}}^{\text{ff}}$]
\quad
\centerAlignProof
\AxiomC{$\la{S}{s}\rightarrow s''$}
\AxiomC{$\la{\n{repeat } S \n{ until } b}{s''}\rightarrow s'$}
\BinaryInfC{$\la{\n{repeat } S \n{ until } b}{s}\rightarrow s'$}
\DisplayProof
\quad
\centerAlignProof
$\text{ si }\fc{B}{b}s' = \mathbf{ff}$
\end{center}
Ahora, usando que $\text{ si }\fc{B}{b}s' = \mathbf{ff}$,
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{\n{repeat } S \n{ until } b}{s''}\rightarrow s'$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s''} \to s'$}[$\text{if}^{\text{ff}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
Pero entonces,
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S}{s} \to s''$}
\AxiomC{$\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s''} \to s'$}
\BinaryInfC{$\la{S; \n{if } b \n{ then skip else } (\n{repeat } S \n{ until } b)}{s} \to s'$}[$\text{comp}_{\text{ns}}$]
\DisplayProof
\end{center}
\noindent\textit{Parte 2.} Supongamos que $\la{S;\n{ if } b \n{ then } \n{skip } \n{else } \n{repeat } S \n{ until } b}{s}\to s'$. La única posibilidad es haber aplicado la regla $[\text{comp}_{\text{ns}}]$
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S}{s} \to s_0$}
\AxiomC{$\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s_0} \to s'$}
\BinaryInfC{$\la{S;\n{if } b \n{ then } \n{skip } \n{else } \n{repeat } S \n{ until } b}{s}\to s'$}
\DisplayProof
\end{center}
para algún $s_0 \in \textbf{State}$. Para la transición $\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s_0} \to s'$ tenemos dos posibilidades
\\ \\
\noindent\textit{(a)} Si $\fc{B}{b}s_0 = \textbf{tt}$ entonces únicamente existe la posibilidad de que se haya derivado de $[\text{if}^\nn{tt}_\nn{ns}]$:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{skip}{s_0} \to s' $}
\UnaryInfC{$\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s_0} \to s'$}
\DisplayProof
\end{center}
y la única forma de que sea cierto $\la{skip}{s_0} \to s'$ es que $s_0 = s'$. Como se verifica $\la{S}{s} \to s_0$ entonces se verifica $\la{S}{s} \to s'$ y se puede aplicar la regla $[\text{repeat}^\nn{tt}_\nn{ns}]$:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S}{s} \to s' $}
\UnaryInfC{$\la{\n{repeat } S \n{ until } b}{s} \to s'$}
\DisplayProof
\end{center}
obteniendo el resultado pues ya se sabe que $\la{S}{s} \to s_0$.
\\ \\
\noindent\textit{(b)} Si $\fc{B}{b}s_0 = \textbf{ff}$ entonces solo cabe la posibilidad de que haya partido de $[\text{if}^\nn{ff}_\nn{ns}]$:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{\n{repeat } S \n{ until } b}{s_0} \to s'$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then skip else}\ (\n{repeat } S \n{ until } b)}{s_0} \to s'$}
\DisplayProof
\end{center}
teniendo así $\la{\n{repeat } S \n{ until } b}{s_0} \to s'$ y entonces se puede deducir
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{S}{s} \to s_0$}
\AxiomC{$\la{\n{repeat } S \n{ until } b}{s_0} \to s'$}
\BinaryInfC{$\la{\n{repeat } S \n{ until } b}{s} \to s'$}
\DisplayProof
\end{center}
mediante $[\text{repeat}^\nn{ff}_\nn{ns}]$ pues $\fc{B}{b}s_0 = \textbf{ff}$. \\
\end{proof}
Para poder demostrar que una propiedad como la anterior se verifica en árboles sencillos y compuestos, emplearemos la demostración por \textit{inducción sobre reglas}, que se compone de dos pasos:
\begin{enumerate}
\item Primero comprobamos que la propiedad se verifica para los axiomas del sistema.
\item Para cada regla, suponiendo que las premisas verifican la propiedad , comprobamos que también se cumple para la conclusión (siempre y cuando se verifiquen las condiciones de la regla).
\end{enumerate}
El siguiente resultado nos dice que, en general, hay \textit{una} manera de deducir una configuración mediante las reglas del sistema de transición $\nn{While}_\nn{ns}$:
\begin{theorem}\label{determinismo}
El sistema de transiciones $\nn{While}_{\nn{ns}}$ es determinista, es decir, para cada $S \in \mathbf{Stm}$, $s, s', s'' \in \mathbf{State}$,
$$\la{S}{s}\rightarrow s' \text{ y } \la{S}{s}\rightarrow s'' \text{ implica que } s'= s''.$$
\end{theorem}
\begin{proof}
Para simplificar la demostración, vamos a definir una propiedad sintáctica de las reglas del sistema $\nn{While}_\nn{ns}$. Decimos que dos reglas son \textit{independientes entre sí} cuando no es posible obtener una mediante la aplicación de la otra. Notemos que este es el caso de nuestro sistema: las reglas $[\nn{while}_\nn{ns}^\nn{tt}]$ y $[\nn{while}_\nn{ns}^\nn{ff}]$ son independientes entre sí porque ambas tienen premisas distintas (suponemos que $\mathbf{tt}$ y $\mathbf{ff}$ son distintos). Entonces, como cada regla es independiente de la otra (y evidentemente, cada regla es determinista), deducimos que, en caso de que tengamos $\la{S}{s}\rightarrow s'$ y $\la{S}{s}\rightarrow s''$, necesariamente tendremos que haber aplicado la misma única regla posible en los dos casos para llegar a las respectivas configuraciones. Es fácil convencerse entonces de que, por inducción sobre las reglas, la propiedad deseada se cumple\footnote{Obviamente, esa demostración no es intercambiable con la demostración formal por inducción estructural. Podría demostrarse el caso general del que hemos hablado, a saber, formalizando lo que significa precisamente la independencia de dos reglas.}.
\end{proof}
\begin{example} Podemos añadir una semántica $\n{forVar}\ x\ \n{do}\ S$ que ejecute la sentencia $S$ siempre que $x$ sea distinto de $0$ y lo incremente en $1$ en cada iteración. Veamos que sería semánticamente equivalente a $\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)$.
\\ \\
Primero, definimos la semantica de $\n{forVar}\ x\ \n{do}\ S$:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\LeftLabel{$[\text{for}^0]$ }
\RightLabel{si $\fc{A}{x}s = 0$}
\UnaryInfC{$\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s$}
\DisplayProof
\end{center}
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\LeftLabel{$[\text{for}^{\ne 0}]$ }
\RightLabel{si $\fc{A}{x}s \ne 0$}
\AxiomC{ $\la{S; x := x+1}{s} \to s'$ }
\AxiomC{$\la{\n{forVar}\ x\ \n{do}\ S}{s'} \to s_1$}
\BinaryInfC{$\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s_1$}
\DisplayProof
\end{center}
\noindent Veamos que $\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s_1$ implica $\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s}\to s_1 $.\\
Para empezar, sabemos que $\fc{A}{x}s = 0$ si y solo si $\fc{B}{\neg (x=0) }s = \mathbf{ff}$, dividimos la demostración en dos pasos:
\begin{enumerate}
\item Si $x=0$ tenemos por $[\text{for}^0]$ que:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s$}
\DisplayProof
\end{center}
Como $\fc{B}{\neg (x=0) }s = \mathbf{ff}$ por la regla [$\text{while}^{\text{ff}}_{\text{ns}}$] sabemos que:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s} \to s$}
\DisplayProof
\quad
\centerAlignProof
\end{center}
\item Si $ x\ne0$, entonces suponemos ciertas las siguientes premisas:
\begin{enumerate}[label=\alph*)]
\item \AxiomC{$\la{S; x := x+1}{s} \to s_2$} \DisplayProof
\item \AxiomC{$\la{\n{forVar}\ x\ \n{do}\ S}{s_2} \to s'$} \DisplayProof
\end{enumerate}
pues la transición $\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s'$ solo puede haber provenido de:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{ $\la{S; x := x+1}{s} \to s_2$ }
\LeftLabel{$[\text{for}^{\ne 0}]$}
\AxiomC{$\la{\n{forVar}\ x\ \n{do}\ S}{s_2} \to s'$}
\BinaryInfC{$\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s'$}
\DisplayProof
\end{center}
Podemos aplicar la hipótesis de inducción sobre $\la{\n{forVar}\ x\ \n{do}\ S}{s_2} \to s'$ y por lo tanto tenemos que $\la{\n{forVar}\ x\ \n{do}\ S}{s_2} \to s'$ implica que $\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s_2} \to s' $, luego podemos costruir el siguiente árbol de derivación:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{ $\la{S; x := x+1}{s} \to s_2$ }
\LeftLabel{ [$\text{while}^{\text{tt}}_{\text{ns}}$]}
\AxiomC{$\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s_2} \to s'$}
\BinaryInfC{$\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s} \to s'$}
\DisplayProof
\end{center}
\end{enumerate}
Supongamos ahora que $\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s}\to s'$. Entonces, distinguimos los siguientes casos:
\begin{enumerate}
\item Si hemos aplicado [$\text{while}^{\text{ff}}_{\text{ns}}$], entonces
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\UnaryInfC{$\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s} \to s'$}[$\text{while}^{\text{ff}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
y además deducimos que $s=s'$ y que $x \neq 0$. Pero entonces tenemos que, directamente:
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{}
\LeftLabel{$[\text{for}^0]$ }
\RightLabel{}
\UnaryInfC{$\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s$}
\DisplayProof
\end{center}
es decir, obtenemos la implicación deseada.
\item Si hemos aplicado [$\text{while}^{\text{tt}}_{\text{ns}}$],
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\AxiomC{$\la{(S; x:=x+1)}{s} \to s'$}
\AxiomC{$\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s'} \to s''$}
\BinaryInfC{$\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s} \to s''$}[$\text{while}^{\text{tt}}_{\text{ns}}$]
\DisplayProof
\quad
\centerAlignProof
\end{center}
y además deducimos que $x \neq 0$. Si aplicamos hipótesis de inducción sobre $\la{\n{while}\ \neg (x=0)\ \n{do}\ (S; x:=x+1)}{s'} \to s''$, obtenemos que $\la{\n{forVar}\ x\ \n{do}\ S}{s'} \to s''$. Pero entonces, juntando las premisas anteriores,
\begin{center}
\centerAlignProof
\quad
\centerAlignProof
\LeftLabel{$[\text{for}^{\ne 0}]$ }
\RightLabel{}
\AxiomC{ $\la{S; x := x+1}{s} \to s'$ }
\AxiomC{$\la{\n{forVar}\ x\ \n{do}\ S}{s'} \to s''$}
\BinaryInfC{$\la{\n{forVar}\ x\ \n{do}\ S}{s} \to s''$}
\DisplayProof
\end{center}
luego obtenemos el resultado.
\end{enumerate}
\end{example}
\subsection{Expresiones}
Finalmente, podemos definir el valor semántico de cada $S \in \mathbf{Stm}$ mediante una aplicación
$\mc{S}_\nn{ns}: \mathbf{Stm} \rightarrow (\mathbf{State}\hookrightarrow\mathbf{State})$, donde
\begin{align*}
\mc{S}_\nn{ns}[[S]]: \mathbf{State} & \hookrightarrow \mathbf{State} \\
s & \mapsto \begin{cases} s' \text{, si } \la{S}{s}\to s' \\
\nn{indefinido} \text{, en otro caso}
\end{cases}
\end{align*}
El determinismo de $\nn{While}_\nn{ns}$ implica que está bien definida. Además, es parcial porque, como vimos, la expresión $\n{while true do skip}$ siempre entra en bucle, es decir, $ \mc{S}_\nn{ns}[[\n{while true do skip}]]s = \mathrm{indefinido}$, para cada $s \in \mathbf{State}$.
\begin{example}
Podemos definir, por ejemplo, una semántica de paso largo para $\mathbf{Aexp}$ mediante la relación de transición $\la{a}{s}\rightarrow_A z$, donde $\la{a}{s}$ significa que $a \in \mathbf{Aexp}$ se evalúa en $s\in \mathbf{State}$ y $z\in \Z$ es un estado final:
\begin{sist*}[$\mathbf{Aexp}_\nn{ns}$]\mbox{}
\begin{itemize}
\item[]
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\RightLabel{$\text{ si } n \in \mathbf{Num}$}
\UnaryInfC{$\la{n}{s}\rightarrow_A \fc{N}{n}$}
\end{prooftree}
\item[]
\begin{prooftree}
\AxiomC{}
\RightLabel{$\text{ si } x \in \mathbf{Var}$}
\UnaryInfC{$\la{x}{s}\rightarrow_A s x$}
\end{prooftree}
\item[]
\begin{prooftree}
\AxiomC{$\la{a_1}{s}\rightarrow_A z_1$}
\AxiomC{$\la{a_2}{s}\rightarrow_A z_2$}
\LeftLabel{}
\RightLabel{}
\BinaryInfC{$\la{a_1 \n{ op } a_2}{s}\rightarrow_A z_1 * z_2$}
\end{prooftree}
$\text{donde } \n{op} \text{ se refiere a } \oplus, \ominus, \otimes \text{ y } * \text{ a } +, -, \times$.
\end{itemize}
\end{sist*}
\end{example}
\begin{prop}
Sean $a \in \mathbf{Aexp}$, $s \in \mathbf{State}$, $z \in \Z$. Entonces $\la{a}{s}\rightarrow_A z$ si y solo si $\fc{A}{a}s =z$.
\end{prop}
\begin{proof}
La demostración es por inducción estructural. Los casos base son:
\begin{itemize}
\item Si $a = n$, entonces $\la{a}{s}\rightarrow_A \fc{N}{n} = \fc{A}{n}s$.
\item Si $a = x$, entonces $\la{a}{s}\rightarrow_A sx = \fc{A}{x}s$.
\end{itemize}
Resumimos los casos inductivos en:
\begin{itemize}
\item Si $a = a_1 \n{ op } a_2$, entonces, empleando la última regla de $\mathbf{Aexp}_\nn{ns}$, tenemos que $\la{a_i}{s}\rightarrow_A z_i$ si y solo si (por hipótesis de inducción) $\fc{A}{a_i}s = z_i$, con $i = 1, 2$. Pero entonces sabemos que $\fc{A}{a_1 \n{ op }a_2}S = \fc{A}{a_1}s * \fc{A}{a_2}s = z_1 * z_2 = z$.
\end{itemize}
\end{proof}
\begin{example}
Siguiendo el ejemplo anterior, podemos definir un sistema de transiciones para expresiones booleanas como sigue. Definimos $\la{b}{s}\rightarrow_B X$, donde $\la{b}{s}$ indica que $b$ se evalúa en el estado $s$ y donde $X\in Bool$.
\begin{sist*}[$\mathbf{Bexp}_\nn{ns}$]\mbox{}
\begin{itemize}
\item[]
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\UnaryInfC{$\la{\n{true}}{s}\rightarrow_B \mathbf{tt}$}
\end{prooftree}
\item[]
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\UnaryInfC{$\la{\n{false}}{s}\rightarrow_B \mathbf{ff}$}
\end{prooftree}
\item[]
\begin{prooftree}
\AxiomC{$\la{a_1}{s}\rightarrow_A z_1$}
\AxiomC{$\la{a_2}{s}\rightarrow_A z_2$}
\LeftLabel{}
\RightLabel{}
\BinaryInfC{$\la{a_1 = a_2}{s}\rightarrow_B X$}
\end{prooftree}
donde $X$ es el booleano correspondiente (véase la definición de $\fc{B}{\cdot}$).
\item[]
\begin{prooftree}
\AxiomC{$\la{a_1}{s}\rightarrow_A z_1$}
\AxiomC{$\la{a_2}{s}\rightarrow_A z_2$}
\LeftLabel{}
\RightLabel{}
\BinaryInfC{$\la{a_1 \leq s a_2}{s}\rightarrow_B X$}
\end{prooftree}
donde $X$ es el booleano correspondiente (de nuevo, empleando la definición de $\fc{B}{\cdot}$).
\item[]\begin{prooftree}
\AxiomC{$\la{a_1}{s}\rightarrow_A z_1$}
\AxiomC{$\la{a_2}{s}\rightarrow_A z_2$}
\LeftLabel{}
\RightLabel{}
\BinaryInfC{$\la{a_1 \leq s a_2}{s}\rightarrow_B X$}
\end{prooftree}
donde $X$ es el booleano correspondiente.
\item[]
\begin{prooftree}
\AxiomC{$\la{b}{s}\rightarrow_B X$}
\LeftLabel{}
\RightLabel{}
\UnaryInfC{$\la{\neg b}{s}\rightarrow_B X'$}
\end{prooftree}
donde $X'$ es el booleano correspondiente (negación de $X$).
\item[]
\begin{prooftree}
\AxiomC{$\la{b_1}{s}\rightarrow_B X$}
\AxiomC{$\la{b_2}{s}\rightarrow_B Y$}
\LeftLabel{}
\RightLabel{}
\BinaryInfC{$\la{b_1 \land b_2}{s}\rightarrow_B Z$}
\end{prooftree}
donde $Z$ es el booleano correspondiente (conjunción de $X$ e $Y$).
\end{itemize}
\end{sist*}
\end{example}
El siguiente resultado es análogo al último que dimos antes:
\begin{prop}
Sean $b \in \mathbf{Bexp}$, $s \in \mathbf{State}$ y $X \in Bool$. Entonces $\la{b}{s}\rightarrow_B X$ si y solo si $\fc{B}{b}s = X$.
\end{prop}
\begin{proof}
Por inducción estructural. Véase la demostración del anterior teorema y la definición de $\fc{B}{\cdot}$. La demostración es análoga.
\end{proof}
\section{Semántica operacional estructural}
\subsection{Sistema de transiciones}
Ahora nos centramos en los pasos concretos de la ejecución de un programa. Para ello, definimos una relación de transición $\la{S}{s} \dto \gamma$ como:
\begin{itemize}
\item Si $\gamma$ es de la forma $\la{S'}{s'}$, entonces la ejecución de $S$ desde $s$ no se completa y sigue en $\la{S'}{s'}$.
\item Si $\gamma$ es de la forma $s'$, entonces la ejecución finaliza en el estado $s'$.
\end{itemize}
La nueva relación de transición queda determinada por el conjunto de reglas:
\begin{sist*}[$\nn{While}_\nn{sos}$]\mbox{}
\begin{itemize}
\item[] $[\text{ass}_{\nn{sos}}]$
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\UnaryInfC{ $\la{x:=a}{s} \dto s[x\mapsto \fc{A}{a}s]$}
\end{prooftree}
\item[]$[\text{skip}_{\nn{sos}}]$
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\UnaryInfC{$\la{\n{skip}}{s} \dto s$}
\end{prooftree}
\item[]$[\text{comp}_{\nn{sos}}^1]$
\begin{prooftree}
\AxiomC{$\la{S_1}{s} \dto \la{S_1'}{s'}$}
\LeftLabel{}
\UnaryInfC{ $\la{S_1; S_2}{s} \dto \la{S_1'; S_2}{s'}$}
\end{prooftree}
\item[]$[\text{comp}_{\nn{sos}}^2]$
\begin{prooftree}
\AxiomC{$\la{S_1}{s} \dto s'$}
\LeftLabel{}
\UnaryInfC{ $\la{S_1; S_2}{s} \dto \la{S_2}{s'}$}
\end{prooftree}
\item[]$[\text{if}^{\nn{tt}}_{\nn{sos}}]$
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\RightLabel{si $\fc{B}{b}s = \textbf{tt}$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then}\ S_1\ \n{else}\ S_2}{s} \dto \la{S_1}{s}$}
\end{prooftree}
\item[]$[\text{if}_{\nn{sos}}^\nn{ff}]$
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\RightLabel{si $\fc{B}{b}s = \textbf{ff}$}
\UnaryInfC{$\la{\n{if}\ b\ \n{then}\ S_1\ \n{else}\ S_2; s}{s} \dto \la{S_2}{s}$}
\end{prooftree}
\item[] $[\nn{while}_\nn{sos}]$
\begin{prooftree}
\AxiomC{}
\LeftLabel{}
\RightLabel{}
\UnaryInfC{$\la{\n{while }b\n{ do }S}{s}\dto \la{\n{if }b\n{ then }(S; \n{while }b\n{ do }S)\n{ else skip}}{s}$}
\end{prooftree}
\end{itemize}
\end{sist*}
Notemos que podríamos haber incluido, por ejemplo, dos reglas para la semántica de $\n{while }b \n{ do }S$:
\begin{prooftree}
\AxiomC{}
\LeftLabel{$[\nn{while}_\nn{sos}^\nn{ff}]$}
\RightLabel{ si $\fc{B}{s}= \mathbf{ff}$}
\UnaryInfC{$\la{\n{while }b\n{ do }S}{s}\dto s$}
\end{prooftree}
y
\begin{prooftree}
\AxiomC{}
\LeftLabel{$[\nn{while}_\nn{sos}^\nn{tt}]$}
\RightLabel{ si $\fc{B}{s}= \mathbf{tt}$}
\UnaryInfC{$\la{\n{while }b\n{ do }S}{s}\dto \la{S;\n{while }b\n{ do }S}{s}$}
\end{prooftree}
\begin{definition}
Se dirá que $\la{S}{s}$ está \textit{bloqueada} si no existe $\gamma$ tal que $\la{S}{s} \dto \gamma$. Una secuencia de derivación es finita cuando llega a un bloqueo o a un estado final:
\[
\gamma_0 \dto \gamma_1 \dto ... \dto \gamma_k
\]
donde $\gamma_0 = \la{S}{s}$, $\gamma_i \dto \gamma_{i+1}$ para $i\in\{0, ..., k-1\}$ y $\gamma_k$ es una configuración bloqueada.
\end{definition}
Normalmente escribiremos $\gamma_0 \dto^i \gamma$ si hay $i$ pasos en la ejecución de $\gamma_0$ a $\gamma$. Si hay finitos pasos, denotamos $\gamma_0 \dto^* \gamma$. $\gamma_0 \dto^i \gamma$ y $\gamma_0 \dto^* \gamma$ no tiene por qué ser secuencias de derivación, solo si $\gamma$ es configuración final o de bloqueo.
\begin{definition}
La ejecución $\la{S}{s}$ de la expresión $S$ en un estado $s$:
\begin{enumerate}
\item \textit{Termina} si existe una única secuencia de derivación finita comenzando en $\la{S}{s}$.
\item \textit{Termina con éxito} si $\la{S_1}{s} \dto ^* s'$ para algún estado $s'$.
\item \textit{Cicla} si existe una secuencia de derivación infinita comenzando en $\la{S}{s}$.
\end{enumerate}
Nótese que estas definiciones son mutuamente excluyentes si y solo si las secuencias de derivacion son únicas. Por comodidad, las definimos de este modo porque, si extendemos el lenguaje, no nos tendremos que preocupar.
\end{definition}
\begin{example}\label{examplerepeat}
Supongamos que queremos extender $\nn{While}_\nn{sos}$ con la expresión $\n{repeat }S \n{ until }b$. Podemos añadir la regla:
\begin{prooftree}
\AxiomC{}
\LeftLabel{$[\nn{repeat}_\nn{sos}]$}
\RightLabel{}
\UnaryInfC{$\la{\n{repeat }S\n{ until }b}{s}\dto \la{S; \n{if }b\n{ then skip else }(\n{repeat }S\n{ until }b)}{s}$}
\end{prooftree}
La idea es que la expresión $\n{repeat }S\n{ until }b$ sea equivalente a $S;\n{ while }\neg b\n{ do } S$. Se definirá posteriormente el concepto de equivalencia semántica y se demostrará este resultado.
\end{example}
\subsection{Propiedades}
El método de demostración principal consiste en hacer \textit{inducción sobre la longitud de las secuencias de derivación} (finitas) que se estudian, es decir, si queremos demostrar una propiedad acerca de nuestro sistema de transiciones:
\begin{itemize}
\item Demostramos que la propiedad se cumple para secuencias de derivación de longitud 0 (en ocasiones nos encontraremos que se cumple la propiedad de forma vacía).
\item Demostramos que si la propiedad se cumple para secuencias de longitud (a lo sumo) $k$, entonces se cumple para secuencias de longitud $k+1$.
\end{itemize}
A modo de ejemplo de este método, veamos el siguiente resultado:
\begin{lema}\label{lemasos1}
Si $\la{S_1;S_2}{s}\dto^k s''$, entonces existen $s'\in \mathbf{State}$, $k_1, k_2 \in \N$ tales que $k = k_1 + k_2$ y $$\la{S_1}{s}\dto^{k_1}s' \quad \text{ y }\quad \la{S_2}{s'}\dto^{k_2}s''.$$
\end{lema}
\begin{proof}
Si $k=0$, entonces $\la{S_1;S_2}{s}\dto^0 s''$ implica (vacuamente) el resultado, porque $\la{S_1;S_2}{s}$ y $s''$ son distintos. Supongamos que el resultado se cumple para longitudes menores o iguales que $k$. Veamos que se sigue para $k+1$. Por tanto, tenemos la premisa $\la{S_1;S_2}{s}\dto^{k+1} s''$, es decir, que existe una configuración $\gamma$ tal que
$$\la{S_1;S_2}{s}\dto \gamma \dto^{k} s''$$
Por tanto, distinguimos dos casos según la regla que hemos aplicado a $\la{S_1;S_2}{s}$ para llegar a $\gamma$:
\begin{itemize}
\item[(a)] Si hemos aplicado [$\nn{comp}^1_\nn{sos}$], tenemos que
\begin{prooftree}
\AxiomC{$\la{S_1}{s} \dto \la{S_1'}{s'}$}
\LeftLabel{$[\text{comp}_{\nn{sos}}^1]$}
\UnaryInfC{ $\la{S_1; S_2}{s} \dto \la{S_1'; S_2}{s'}= \gamma$}
\end{prooftree}
luego $\la{S_1'; S_2}{s'} \dto^k s''$. Entonces, como esta derivación es de longitud $k$, podemos aplicar hipótesis de inducción, esto es, existen $s_0 \in \mathbf{State}$ y $k_1, k_2 \in \N$ con $k = k_1 + k_2$ y
$$\la{S_1'}{s'}\dto^{k_1}s_0 \quad \text{ y }\quad \la{S_2}{s_0}\dto^{k_2}s''.$$
Ahora bien, como tenemos la premisa $\la{S_1}{s} \dto \la{S_1'}{s'}$ y $\la{S_1'}{s'}\dto^{k_1}s_0$, entonces tenemos que $\la{S_1}{s}\dto^{k_1 + 1}s_0$. Por otro lado, también tenemos $\la{S_2}{s_0}\dto^{k_2}s''$ y que $(k_1 + 1) + k_2 = (k_1 + k_2)+1 = k+1$. Es decir, hemos obtenido la conclusión deseada. Por tanto, hemos probado el resultado para este caso.
\item[(b)] Si hemos aplicado [$\nn{comp}^2_\nn{sos}$], tenemos que
\begin{prooftree}
\AxiomC{$\la{S_1}{s} \dto s'$}
\LeftLabel{[$\nn{comp}^2_\nn{sos}$]}
\UnaryInfC{ $\la{S_1; S_2}{s} \dto \la{S_2}{s'}= \gamma$}
\end{prooftree}
Entonces deducimos que $\la{S_2}{s'} \dto^{k}s''$. Simplemente tomando $k_1 := 1$ y $k_2 := k$ vemos que $k_1+k_2 = k+1$ y que tenemos el resultado.
\end{itemize}
\end{proof}
\begin{example}
Por otro lado, $\la{S_1;S_2}{s}\dto^* \la{S_2}{s'}$ no implica necesariamente que $\la{S_1}{s}\dto^* s'$. Por ejemplo, podemos tomar $S_1 := \n{skip}$, $S_2 := \n{while }\neg(\n{x}=1)\n{ do } \n{x}:=\n{x}+1$ y $s\n{x} = 3$, $s'\n{x} = s[\n{x}\mapsto 2]$.
\end{example}
El siguiente lema viene a decir que la ejecución de una expresión es independiente de cualquier enunciado que se ejecute después:
\begin{lema}\label{lemasos2}
Si $\la{S_1}{s}\dto^k s'$, entonces $\la{S_1;S_2}{s}\dto^k \la{S_2}{s'}$.
\end{lema}
\begin{proof}
Por inducción sobre la longitud de las derivaciones. En caso de $k=0$, la premisa es falsa y el resultado se tiene directamente. Supongamos que se cumple el resultado para longitudes $\leq k$ y veámoslo para $k+1$. Nuestra suposición es que $\la{S_1}{s}\dto^{k+1} s'$. Entonces tenemos que hay cierta configuración $\gamma$ con
$$\la{S_1}{s}\dto \gamma \dto^k s'$$
y además, notemos que $\gamma = \la{S}{s''}$ porque $k\leq 1$. Pero entonces, aplicando la hipótesis de inducción a $\la{S}{s''} \dto^k s'$, tenemos que $\la{S;S_2}{s''}\dto^k \la{S_2}{s'}$.
Por otro lado, de $\la{S_1}{s}\dto \la{S}{s''}$ podemos deducir que:
\begin{prooftree}
\AxiomC{$\la{S_1}{s} \dto \la{S}{s''}$}
\LeftLabel{$[\text{comp}_{\nn{sos}}^1]$}
\UnaryInfC{ $\la{S_1; S_2}{s} \dto \la{S; S_2}{s''}$}
\end{prooftree}
Es decir, sabemos que $\la{S_1; S_2}{s} \dto \la{S; S_2}{s''}$ y que $\la{S;S_2}{s''}\dto^k \la{S_2}{s'}$. Basta componer ambas derivaciones para ver que $\la{S_1; S_2}{s}\dto^{k+1} \la{S_2}{s'}$, como queríamos.
\end{proof}
\begin{theorem}\label{teosos}
El sistema de transiciones $\nn{While}_\nn{sos}$ es determinista, es decir, para cualesquiera $S, s, \gamma, \gamma'$ tenemos que
$$\la{S}{s}\dto \gamma \text{ y } \la{S}{s}\dto \gamma' \text{ implica que } \gamma = \gamma'$$
\end{theorem}
\begin{proof}
Véase la demostración del Teorema \ref{determinismo}.