correctness.tex 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. \subsection{Correctness}
  2. \begin{definition}[Well typed value]
  3. \label{wt-value}
  4. A value $v \in Value$ is well typed regarding to a type $T$ (noted $v |- T$) when:
  5. \begin{align*}
  6. v |- int &<=> v \in \mathbb{N} \\
  7. v |- \{f_1 : T_1, \dots, f_n : T_n\} &<=> v \in Field \rightharpoonup Value\text{ and }\forall f_i, f_i \in dom(v) \land v(f_i) |- T_i
  8. \end{align*}
  9. \end{definition}
  10. \begin{definition}[Well typed state]
  11. \label{wt-state}
  12. A state $\sigma \in State$, $\sigma \neq \bot$ is well typed with regards to a type system $\Gamma$ (noted $\sigma |- \Gamma$) when
  13. \[\forall x \in Var,\ \Gamma |- x : T => \sigma x |- T\]
  14. \end{definition}
  15. \begin{lemma}[Well typed expressions cannot go wrong]
  16. $\forall e \in AExpr, \sigma \in State, \Gamma$ such as $\sigma |- \Gamma$:
  17. \[\Gamma |- e : T => \mathcal{A}|[e|] \sigma |- T\]
  18. In particular, $\mathcal{A}|[e|] \sigma \neq \bot$.
  19. \end{lemma}
  20. \begin{proof}
  21. We demonstrate this property by induction on the structure of the expression $e$.
  22. First let assume that $\Gamma |- e : T$.
  23. We can eliminate all trivial cases in contradiction with this assumption (for example when $e = 1 + 1$ and $T = \{\}$).
  24. Here are the other base cases:
  25. \begin{itemize}
  26. \item $e = n, T = int$.
  27. $\mathcal{A}|[e|] \sigma = \mathcal{N}|[n|] \sigma$.
  28. $\mathcal{N}|[n|] \sigma \in \mathbb{N}$ so we have $\mathcal{A}|[e|] \sigma \in \mathbb{N}$ which means that $\mathcal{A}|[e|] \sigma |- T$.
  29. \item $e = x$.
  30. By definition $\mathcal{A}|[x|]\sigma = \sigma x$.
  31. $\sigma$ is well typed, so by definition \ref{wt-state}, $\sigma x |- T$.
  32. So we have $\mathcal{A}|[e|] \sigma |- T$.
  33. \end{itemize}
  34. Now the inductive cases:
  35. \begin{itemize}
  36. \item $e = e_1\ o\ e_2, T = int$.
  37. By definition $\mathcal{A}|[e|]\sigma = \mathcal{A}|[e_1|]\sigma\ o\ \mathcal{A}|[e_2|]\sigma$.
  38. We also know that $\Gamma |- e_1 : int$ and $\Gamma |- e_2 : int$ (by definition of $|-$).
  39. So by our hypothesis of induction, we have $\mathcal{A}|[e_1|]\sigma |- int$ and $\mathcal{A}|[e_2|]\sigma |- int$.
  40. This means that both $\mathcal{A}|[e_1|]\sigma$ and $\mathcal{A}|[e_2|]\sigma$ are in $\mathbb{N}$,
  41. $\mathcal{A}|[e_1|]\sigma\ o\ \mathcal{A}|[e_2|]\sigma$ is defined and returns a value in $\mathbb{N}$.
  42. So we have $\mathcal{A}|[e|] \sigma |- T$.
  43. \item $e = e_1.f$.
  44. By definition, $\mathcal{A} |[ e |] \sigma = \mathcal{A}|[e_1|] \sigma f$.
  45. But is it defined?
  46. By definition of $|-$,
  47. \[
  48. \inference{\Gamma \vdash e_1 : \{f : T\}}{\Gamma \vdash e_1.f : T}
  49. \]
  50. By our hypothesis of induction, we have $\mathcal{A}|[e_1|]\sigma |- T$.
  51. So by definition \ref{wt-state} we have:
  52. \begin{itemize}
  53. \item $\mathcal{A}|[e_1|]\sigma \in Field \rightharpoonup Value$
  54. \item $f \in dom(\mathcal{A}|[e_1|]\sigma)$
  55. \item $\mathcal{A}|[e_1|]\sigma f |- T$
  56. \end{itemize}
  57. This means that $\mathcal{A} |[ e |] \sigma = \mathcal{A}|[e_1|] \sigma f$ is defined,
  58. and $\mathcal{A}|[e|] \sigma |- T$.
  59. \item $e = e_1[f \mapsto e_2]$.
  60. By definition, $\mathcal{A} |[ e |] \sigma = \mathcal{A}|[ e_1 |] \sigma [f \mapsto \mathcal{A}|[ e_2 |] \sigma ]$.
  61. We need to ensure that it is defined, and well typed.
  62. By definition of $|-$,
  63. \[
  64. \inference[append ]{\Gamma \vdash e_2 : T' & \Gamma \vdash e_1 : \{f_1 : T_1; \dots; f_n : T_n\}}{\Gamma \vdash e_1[f \mapsto e_2] : T = \{f : T'; f_1 : T_1; \dots; f_n : T_n\}}
  65. \]
  66. or
  67. \[
  68. \inference[override ]{\Gamma \vdash e_2 : T' & \Gamma \vdash e_1 : \{f_1 : T_1; \dots; f_i : T_i; \dots; f_n : T_n\}}{\Gamma \vdash e_1[f_i \mapsto e_2] : T = \{f_1 : T_1; \dots; f_i : T'; \dots; f_n : T_n\}}
  69. \]
  70. Let consider the first case (the other case works the same).
  71. By our hypothesis of induction, we have $\mathcal{A}|[e_1|]\sigma |- \{f_1 : T_1; \dots; f_n : T_n\}$.
  72. So by definition \ref{wt-state} we have:
  73. \begin{itemize}
  74. \item $\mathcal{A}|[e_1|]\sigma \in Field \rightharpoonup Value$
  75. \item $f \in dom(\mathcal{A}|[e_1|]\sigma)$
  76. %\item $\mathcal{A}|[e_1|]\sigma f |- T$
  77. \end{itemize}
  78. We also have (by induction again), $\mathcal{A}|[e_2|]\sigma |- T'$.
  79. So $\mathcal{A} |[ e |] \sigma = \mathcal{A}|[ e_1 |] \sigma [f \mapsto \mathcal{A}|[ e_2 |]]$ is defined,
  80. and moreover:
  81. \begin{itemize}
  82. \item $\mathcal{A}|[e|] \sigma \in Field \rightharpoonup Value$
  83. \item $\forall f_i, 1 \leq i \leq n, f_i \in dom(\mathcal{A}|[e|]\sigma)$ (because it is in $dom(\mathcal{A}|[e_1|]\sigma)$)
  84. \item $f \in dom(\mathcal{A}|[e|]\sigma)$ (because it has just been added), and finally,
  85. \item $\mathcal{A}|[e|]\sigma f |- T'$.
  86. \end{itemize}
  87. This matches the definition of $\mathcal{A}|[e|] \sigma |- T = \{f : T'; f_1 : T_1; \dots; f_n : T_n\}$.
  88. \item $e = \{f_1 = e_1; \dots; f_n = e_n\}$.
  89. By definition,
  90. \[\mathcal{A} |[e|] \sigma f_i = \mathcal{A}|[e_i|] \sigma\]
  91. This means that, $\forall f_i, 1 \leq i \leq n$:
  92. \begin{itemize}
  93. \item $\mathcal{A}|[e|] \sigma \in Field \rightharpoonup Value$
  94. \item $f_i \in dom(\mathcal{A}|[e|]\sigma)$
  95. \item $\mathcal{A}|[e|]\sigma f_i = \mathcal{A}|[e_i|]\sigma |- T_i$ (by induction).
  96. \end{itemize}
  97. This matches the definition for $\mathcal{A}|[e|] \sigma |- T$.
  98. \end{itemize}
  99. \end{proof}
  100. \begin{lemma}[Well typed programs cannot go wrong]
  101. \label{wt-state-dgw}
  102. \[\forall \Gamma P,\quad \Gamma \vdash P \; => \;\forall \sigma |- \Gamma,\; ((P, \sigma) \reduce \sigma' => \sigma' |- \Gamma) \land ((P, \sigma) \reduce (S, \sigma') => \sigma' |- \Gamma)\]
  103. In particular this also means that:
  104. \[\forall \Gamma P,\quad \Gamma \vdash P \; => \;\forall \sigma |- \Gamma,\; (P, \sigma) \not\reduce \bot \land (P, \sigma) \not\reduce (S, \bot)\]
  105. \end{lemma}
  106. \begin{proof}
  107. This proof can be done by induction on the length of the execution path of the program $P$.
  108. Let's consider $\Gamma |- P$ and a state $\sigma |- \Gamma$.
  109. \begin{itemize}
  110. \item Let's prove that if $(P, \sigma) \rightarrow^0 \sigma' or (P, \sigma) \rightarrow^0 (S, \sigma')$ then $\sigma' |- \Gamma$.
  111. Because $\sigma = (S, \sigma')$ is impossible, we are in the case where $\sigma = \sigma'$.
  112. By hypothesis, $\sigma |- \Gamma$, so $\sigma' |- \Gamma$.
  113. \item We name $s$ the intermediate state such as $(P, \sigma) \rightarrow^k s$ for any $k$.
  114. We have two cases:
  115. \begin{itemize}
  116. \item $s = \sigma'' \in State$
  117. There is no transition from a terminal state, so this is trivially true.
  118. \item $s = (S, \sigma'')$
  119. By induction we know that $\sigma'' |- \Gamma$.
  120. We can prove by induction on the structure of $S$ that
  121. $(S, \sigma'') \rightarrow \sigma' => \sigma' |- \Gamma$ and
  122. $(S, \sigma'') \rightarrow (S', \sigma') => \sigma' |- \Gamma$.
  123. Sadely, We don't have time to terminate the proof...
  124. \end{itemize}
  125. \end{itemize}
  126. \end{proof}