dm.tex 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. \documentclass[11pt]{article}
  2. \usepackage{fullpage}
  3. %% Corrige quelques erreurs de LaTeX2ε
  4. \usepackage{fixltx2e}
  5. \usepackage{xspace}
  6. \usepackage{microtype}
  7. %% Pour ne pas commettre les erreurs fréquentes décrites dans l2tabu
  8. \usepackage[l2tabu,abort]{nag}
  9. %% Saisie en UTF-8
  10. \usepackage[utf8]{inputenc}
  11. %% Fonte : cf. http://www.tug.dk/FontCatalogue/
  12. \usepackage[T1]{fontenc}
  13. \usepackage{lmodern}
  14. %% Pour écrire du français
  15. % \usepackage[frenchb]{babel}
  16. %% Pour composer des mathématiques
  17. \usepackage{mathtools}
  18. \usepackage{amsfonts}
  19. \usepackage{amssymb}
  20. \usepackage{amsthm}
  21. \usepackage{stmaryrd}
  22. \usepackage{semantic}
  23. %% Commandes du paquet semantic : langage While
  24. \reservestyle{\command}{\textbf}
  25. \command{skip,while,do,if,then,else,not,and,or,true,false,int}
  26. %% -> Utiliser \<begin> Pour styliser le mot-clé
  27. \newcommand{\reduce}{\rightarrow^{*}}
  28. \begin{document}
  29. \author{Timoth\'ee Haudebourg \and Thibaut Marty}
  30. \title{Homework PAS/SDL}
  31. \date{November 18, 2016}
  32. \maketitle
  33. \section{Exercise 1}
  34. \subsection{Domains}
  35. The $State$ domain of evaluation is defined as:
  36. \[State = (Var \rightharpoonup Value) \cup \bot\]
  37. Where $Var$ is the set of variables, and $Value$ the set of possible values.
  38. We use $\bot$ to represent the error state.
  39. In our model, a record is represented as a partial application $Field \rightharpoonup Value$.
  40. This means that a record field can contains a record itself.
  41. To do so, we define the set of value as:
  42. \begin{align*}
  43. Value &= \bigcup_{i\in\mathbb{N}} Value_i \\
  44. Value_n &= \mathbb{N} \cup (Field \rightharpoonup Value_{n+1}) \\
  45. \end{align*}
  46. \subsection{Denotational Semantic for expressions}
  47. We define $\mathcal{A}$ the denotational semantics of arithmetic expressions as:
  48. \[ \mathcal{A}|[ \bullet |] : AExpr \rightarrow State \rightarrow (Value \cup \bot) \]
  49. where
  50. \begin{align*}
  51. \mathcal{A} |[ n |] \sigma &= \mathcal{N} |[n|] \\
  52. \mathcal{A} |[ x |] \sigma &= \sigma x \\
  53. \mathcal{A} |[ e_1\ o\ e_2 |] \sigma &= \mathcal{A} |[ e_1 |] \sigma\ \bar{o}\ \mathcal{A} |[ e_2 |] \sigma \\
  54. \mathcal{A} |[ e.f |] \sigma &= \mathcal{A}|[e|] \sigma f\\
  55. \mathcal{A} |[ e_1{f \mapsto e_2} |] \sigma f &= \mathcal{A}|[ e_1 |] \sigma [f \mapsto \mathcal{A}|[ e_2 |] \sigma ]\\
  56. \mathcal{A} |[ \{f_1 = e_1 ; \dots ; f_n = e_n\} |] \sigma f_i &= \mathcal{A}|[ e_i |] \sigma \\
  57. \end{align*}
  58. For the undefined cases, for example when we try to add two records, we also note $\mathcal{A}|[e|] \sigma = \bot$.
  59. In the rest of this homwork, we suppose $\mathcal{B}|[ \bullet |]$ the denotational semantic for booleans expressions already defined.
  60. % TODO fin de la question : explications
  61. \subsection{Structural Operational Semantic for commands}
  62. We define $->$ the structural operational semantics for commands as:
  63. \[ -> : (Statement \times State) \times ((Statement \times State) \cup State) \]
  64. \subsubsection{Regular execution}
  65. For all $\sigma \neq \bot$:
  66. \[
  67. \inference{}{(skip, \sigma) -> \sigma}
  68. \]
  69. \[
  70. \inference{}{(x := a, \sigma) -> \sigma[x \mapsto \mathcal{A}|[a|] \sigma]}{\text{if }\mathcal{A}|[a|] \sigma \neq \bot}
  71. \]
  72. \[
  73. \inference{(S_1, \sigma) -> (S_1', \sigma')}{(S_1 ; S_2, \sigma) -> (S_1' ; S_2, \sigma')}
  74. \]
  75. \[
  76. \inference{(S_1, \sigma) -> \sigma'}{(S_1 ; S_2, \sigma) -> (S_2, \sigma')}
  77. \]
  78. \[
  79. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> (S ; \<while>\ b\ \<do>\ S, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  80. \]
  81. \[
  82. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  83. \]
  84. \[
  85. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_1, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  86. \]
  87. \[
  88. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_2, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  89. \]
  90. \subsubsection{Error handling}
  91. An error can occurs when one tries to add (or compare) records.
  92. In that case, according to our definition of arithmetic expressions, $\mathcal{A}|[e|] \sigma$ (or $\mathcal{B}|[b|]$) will returns $\bot$.
  93. From this point, the execution of the program makes no sense, and we will return the error state.
  94. \[
  95. \inference{}{(x := a, \sigma) -> \bot}{\text{if }\mathcal{A}|[a|] \sigma = \bot}
  96. \]
  97. \[
  98. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  99. \]
  100. \[
  101. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  102. \]
  103. To ensure that the error can make it through the end, we also add the error propagation rule:
  104. \[
  105. \inference{}{(S, \bot) -> \bot}
  106. \]
  107. \section{Type system}
  108. We suppose now that every variable in the program is declared with a type satisfying the following syntax:
  109. \[
  110. t\quad::=\quad\text{int }|\ \{f_1 : t_1;\ \dots\ ; f_n : t_n\}
  111. \]
  112. We note $\Gamma \vdash S$ the type judgment for our language, defined by the following typing rules:
  113. \[
  114. \inference{}{\Gamma \vdash n : int}
  115. \]
  116. \[
  117. \inference{(x : T) \in \Gamma}{\Gamma \vdash x : T}
  118. \]
  119. \[
  120. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1\ o\ a_2 : int}
  121. \]
  122. \[
  123. \inference{\Gamma \vdash e_1 : T_1 & \dots & \Gamma \vdash e_n : T_n}{\Gamma \vdash \{f_1 = e_1 ; \dots ; f_n = e_n\} : \{f_1 : T_1 ; \dots f_n : T_n\}}
  124. \]
  125. \[
  126. \inference{\Gamma \vdash \{f : T\}}{\Gamma \vdash e.f : T}
  127. \]
  128. \[
  129. \inference{\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] : \{f : T ; f_1 : T_1 ; \dots ; f_n : T_n\}}
  130. \]
  131. % TODO : règle sub-typing
  132. % TODO : booléens… (autre photo)
  133. \section{Static analysis}
  134. $RF : Lab -> Var -> \mathcal{P}(Field)$
  135. % TODO : définir init
  136. \[
  137. \inference{RF(l) \subseteq RF (init(S)) & RF \vdash (S, l) & RF(l) \subseteq RF(l')}{RF \vdash (\<while>\ [b]^l\ \<do>\ S, l')}
  138. \]
  139. \[
  140. \inference{RF(l) \subseteq RF(init(S_1)) & RF \vdash (S_1, l') \\ RF(l) \subseteq RF(init(S_2)) & RF \vdash (S_2, l')}{RF \vdash (\<if>\ [b]^l\ \<then>\ S_1\ \<else>\ S_2, l')}
  141. \]
  142. \[
  143. \inference{RF(l) \subseteq RF(l')}{RF \vdash ([skip]^l, l')}
  144. \]
  145. \[
  146. \inference{RF \vdash (S_1, init(S_2)) & RF \vdash (S_2, l')}{RF \vdash (S_1 ; S_2, l')}
  147. \]
  148. \[
  149. \inference{RF(l)[x \mapsto \{f_1, \dots, f_n\}] \subseteq RF(l')}{RF \vdash ([x := \{f_1 = e_1 ; \dots ; f_n = e_n\}]^l, l')}
  150. \]
  151. \[
  152. % TODO : il faudrait évaluer statiquement e_1 pour être moins pessimiste
  153. \inference{RF(l)[x \mapsto \{f\}] \subseteq RF(l')}{RF \vdash ([x := e_1[f \mapsto e_2]]^l, l')}
  154. \]
  155. % autres affectations
  156. \[
  157. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := n]^l, l')}
  158. \]
  159. \[
  160. \inference{RF(l)[x \mapsto RF(l)(y)] \subseteq RF(l')}{RF \vdash ([x := y]^l, l')}
  161. \]
  162. \[
  163. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := e_1\ o\ e_2]^l, l')}
  164. \]
  165. \[
  166. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := e.f]^l, l')}
  167. \]
  168. \end{document}