dm.tex 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. \usepackage{csquotes}
  15. %% Pour écrire du français
  16. % \usepackage[frenchb]{babel}
  17. %% Pour composer des mathématiques
  18. \usepackage{mathtools}
  19. \usepackage{amsfonts}
  20. \usepackage{amssymb}
  21. \usepackage{amsthm}
  22. \usepackage{stmaryrd}
  23. \usepackage{semantic}
  24. %% Commandes du paquet semantic : langage While
  25. \reservestyle{\command}{\textbf}
  26. \command{skip,while,do,if,then,else,not,and,or,true,false,int}
  27. %% -> Utiliser \<begin> Pour styliser le mot-clé
  28. \newcommand{\reduce}{\rightarrow^{*}}
  29. \begin{document}
  30. \author{Timoth\'ee Haudebourg \and Thibaut Marty}
  31. \title{Homework PAS/SDL}
  32. \date{November 18, 2016}
  33. \maketitle
  34. \section{Small-step Semantic}
  35. \subsection{Domains}
  36. The $State$ domain of evaluation is defined as:
  37. \[State = (Var \rightharpoonup Value) \cup \bot\]
  38. Where $Var$ is the set of variables, and $Value$ the set of possible values.
  39. We use $\bot$ to represent the error state.
  40. In our model, a record is represented as a partial application $Field \rightharpoonup Value$.
  41. This means that a record field can contains a record itself.
  42. To do so, we define the set of value as:
  43. \begin{align*}
  44. Value &= \bigcup_{i\in\mathbb{N}} Value_i \\
  45. Value_n &= \mathbb{N} \cup (Field \rightharpoonup Value_{n+1}) \\
  46. \end{align*}
  47. \subsection{Denotational Semantic for expressions}
  48. We define $\mathcal{A}$ the denotational semantics of arithmetic expressions as:
  49. \[ \mathcal{A}|[ \bullet |] : AExpr \rightarrow State \rightarrow (Value \cup \bot) \]
  50. where
  51. \begin{align*}
  52. \mathcal{A} |[ n |] \sigma &= \mathcal{N} |[n|] \\
  53. \mathcal{A} |[ x |] \sigma &= \sigma x \\
  54. \mathcal{A} |[ e_1\ o\ e_2 |] \sigma &= \mathcal{A} |[ e_1 |] \sigma\ \bar{o}\ \mathcal{A} |[ e_2 |] \sigma, o \in \{+, -, \times\} \\
  55. \mathcal{A} |[ e.f |] \sigma &= \mathcal{A}|[e|] \sigma f\\
  56. \mathcal{A} |[ e_1{f \mapsto e_2} |] \sigma f &= \mathcal{A}|[ e_1 |] \sigma [f \mapsto \mathcal{A}|[ e_2 |] \sigma ]\\
  57. \mathcal{A} |[ \{f_1 = e_1 ; \dots ; f_n = e_n\} |] \sigma f_i &= \mathcal{A}|[ e_i |] \sigma \\
  58. \end{align*}
  59. For the undefined cases, for example when we try to add two records, we also note $\mathcal{A}|[e|] \sigma = \bot$.
  60. In the rest of this homwork, we suppose $\mathcal{B}|[ \bullet |]$ the denotational semantic for booleans expressions already defined.
  61. % TODO fin de la question : explications
  62. \subsection{Structural Operational Semantic for commands}
  63. We define $->$ the structural operational semantics for commands as:
  64. \[ -> : (Statement \times State) \times ((Statement \times State) \cup State) \]
  65. \subsubsection{Regular execution}
  66. For all $\sigma \neq \bot$:
  67. \[
  68. \inference{}{(\<skip>, \sigma) -> \sigma}
  69. \]
  70. \[
  71. \inference{}{(x := a, \sigma) -> \sigma[x \mapsto \mathcal{A}|[a|] \sigma]}{\text{if }\mathcal{A}|[a|] \sigma \neq \bot}
  72. \]
  73. \[
  74. \inference{(S_1, \sigma) -> (S_1', \sigma')}{(S_1 ; S_2, \sigma) -> (S_1' ; S_2, \sigma')}
  75. \]
  76. \[
  77. \inference{(S_1, \sigma) -> \sigma'}{(S_1 ; S_2, \sigma) -> (S_2, \sigma')}
  78. \]
  79. \[
  80. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> (S ; \<while>\ b\ \<do>\ S, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  81. \]
  82. \[
  83. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  84. \]
  85. \[
  86. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_1, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  87. \]
  88. \[
  89. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_2, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  90. \]
  91. \subsubsection{Error handling}
  92. An error can occurs when one tries to add (or compare) records.
  93. In that case, according to our definition of arithmetic expressions, $\mathcal{A}|[e|] \sigma$ (or $\mathcal{B}|[b|]$) will returns $\bot$.
  94. From this point, the execution of the program makes no sense, and we will return the error state.
  95. \[
  96. \inference{}{(x := a, \sigma) -> \bot}{\text{if }\mathcal{A}|[a|] \sigma = \bot}
  97. \]
  98. \[
  99. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  100. \]
  101. \[
  102. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  103. \]
  104. To ensure that the error can make it through the end, we also add the error propagation rule:
  105. \[
  106. \inference{}{(S, \bot) -> \bot}
  107. \]
  108. Finally, note that according to the exercise statement, we suppose that all variables are already defined in $\sigma$,
  109. so that no error can occur because of an undefined variable.
  110. \section{Type system}
  111. We suppose now that every variable in the program is declared with a type satisfying the following syntax:
  112. \[
  113. t\quad::=\quad\text{int }|\ \{f_1 : t_1;\ \dots\ ; f_n : t_n\}
  114. \]
  115. \subsection{Definition}
  116. We note $\Gamma \vdash S$ the type judgment for our language, defined by the following typing rules:
  117. \[
  118. \inference{}{\Gamma \vdash n : int}
  119. \]
  120. \[
  121. \inference{(x : T) \in \Gamma}{\Gamma \vdash x : T}
  122. \]
  123. \[
  124. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1\ o\ a_2 : int}{o \in \{+, -, \times\}}
  125. \]
  126. Records
  127. \[
  128. \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\}}
  129. \]
  130. Field assignment
  131. \[
  132. \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] : \{f : T; f_1 : T_1; \dots; f_n : T_n\}}
  133. \]
  134. \[
  135. \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] : \{f_1 : T_1; \dots; f_i : T; \dots; f_n : T_n\}}
  136. \]
  137. Field lookup
  138. \[
  139. \inference{\Gamma \vdash e : \{f : T\}}{\Gamma \vdash e.f : T}
  140. \]
  141. As is, the last rule can seems too restrictive.
  142. Indeed, an expression $e$ does not have to match \emph{exactly} the type $\{f : T\}$ to provide the field $f$.
  143. It can contains some other fields.
  144. We want the type $\{f : T\}$ to express \enquote{any record with a field $f$ of type $T$}.
  145. To do this, we introduce the following sub-typing relation:
  146. \[
  147. \inference{}{\{f_1 : T_1; \dots ; f_n : T_n; g_1 : S_1; \dots ; g_k : S_k\} <: \{f_1 : T_1; \dots ; f_n : T_n\}}
  148. \]
  149. % FIXME on avait noté la relation <: dans l'autre sens
  150. Note that in order to assure the reflexivity of our relation,
  151. a type $S$ is a sub-type of $T$ if all the fields of $T$ are included in $S$.
  152. We also introduce a new subsumption typing rule:
  153. \[
  154. \inference{\Gamma \vdash e : S & S <: T}{\Gamma \vdash e : T}
  155. \]
  156. Assignment
  157. \[
  158. \inference{\Gamma \vdash x : T & \Gamma \vdash a : T}{\Gamma \vdash x := a}
  159. \]
  160. Sequence
  161. \[
  162. \inference{\Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash (S_1 ; S_2)}
  163. \]
  164. While loop
  165. \[
  166. \inference{\Gamma \vdash b & \Gamma \vdash S}{\Gamma \vdash \<while>\ b\ \<do>\ S}
  167. \]
  168. Condition
  169. \[
  170. \inference{\Gamma \vdash b & \Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash \<if>\ b\ \<then>\ S_1\ \<else>\ S_2}
  171. \]
  172. Booleans
  173. \[
  174. \inference{}{\Gamma \vdash \<true>}
  175. \]
  176. \[
  177. \inference{}{\Gamma \vdash \<false>}
  178. \]
  179. \[
  180. \inference{\Gamma \vdash b}{\Gamma \vdash \<not>\ b}
  181. \]
  182. \[
  183. \inference{\Gamma \vdash b_1 & \Gamma \vdash b_2}{\Gamma \vdash b_1\ o\ b_2}{o \in \{\<and>, \<or>\}}
  184. \]
  185. \[
  186. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1 \le a_2}
  187. \]
  188. \[
  189. \inference{\Gamma \vdash a_1 : T & \Gamma \vdash a_2 : T}{\Gamma \vdash a_1 = a_2}
  190. \]
  191. Note: for the equality test, the rule works for \<int> as well as for records.
  192. % Domains
  193. \[
  194. n \in Num
  195. \]
  196. \[
  197. x \in Var
  198. \]
  199. \[
  200. e,e_1,e_2,a,a_1,a_2 \in AExpr
  201. \]
  202. \[
  203. b,b_1,b_2 \in BExpr
  204. \]
  205. \[
  206. f,f_1,\dots,f_n,f_i,g_1,\dots,g_k, \in Field % Field n'est pas défini…
  207. \]
  208. \[
  209. S,S_1,S_2 \in Statement
  210. \]
  211. \subsection{Correctness}
  212. \[\forall \Gamma P,\quad \Gamma \vdash P \; => \;\forall \sigma,\; (P, \sigma) \not\reduce \bot\]
  213. \begin{proof}
  214. By induction on the path of $\reduce$, and by induction on the structure/type of the statement at each step of the path.
  215. \end{proof}
  216. \section{Static analysis}
  217. We propose a constraint based static analysis to determine at each instruction which field is defined in each variables.
  218. Our static analysis is incarnated by the function $RF$ (\enquote{Reachable Fileds}).
  219. \[RF : Lab -> Var -> \mathcal{P}(Field)\]
  220. where $Lab$ is the set of labels.
  221. One unique label is assigned to each instruction.
  222. For the sake of simplicity, we note $[I]^l$ the instruction of label $l$.
  223. We also define an $init$ function returning the first label of a statement where:
  224. \begin{align*}
  225. &init([\<skip>]^l) = l\\
  226. &init([x := a]^l) = l\\
  227. &init(S_1;S_2) = init(S_1)\\
  228. &init(\<while>\ [b]^l\ \<do>\ S) = l \\
  229. &init(\<if>\ [b]^l\ \<then>\ S_1\ \<else>\ S_2) = l
  230. \end{align*}
  231. \subsection{Definition}
  232. \[
  233. \inference{RF(l) \subseteq RF (init(S)) & RF \vdash (S, l) & RF(l) \subseteq RF(l')}{RF \vdash (\<while>\ [b]^l\ \<do>\ S, l')}
  234. \]
  235. \[
  236. \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')}
  237. \]
  238. \[
  239. \inference{RF(l) \subseteq RF(l')}{RF \vdash ([skip]^l, l')}
  240. \]
  241. \[
  242. \inference{RF \vdash (S_1, init(S_2)) & RF \vdash (S_2, l')}{RF \vdash (S_1 ; S_2, l')}
  243. \]
  244. \[
  245. \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')}
  246. \]
  247. \[
  248. % TODO : il faudrait évaluer statiquement e_1 pour être moins pessimiste
  249. \inference{RF(l)[x \mapsto \{f\}] \subseteq RF(l')}{RF \vdash ([x := e_1[f \mapsto e_2]]^l, l')}
  250. \]
  251. % autres affectations
  252. \[
  253. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := n]^l, l')}
  254. \]
  255. \[
  256. \inference{RF(l)[x \mapsto RF(l)(y)] \subseteq RF(l')}{RF \vdash ([x := y]^l, l')}
  257. \]
  258. \[
  259. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := e_1\ o\ e_2]^l, l')}
  260. \]
  261. \[
  262. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := e.f]^l, l')}
  263. \]
  264. \subsection{Example}
  265. Example.
  266. \subsection{Termination}
  267. In each premise of each rule, the size of the statement is \emph{strictly} decreasing,
  268. assuring the termination of the analysis.
  269. \subsection{Safety}
  270. This is a pessimistic static analysis.
  271. \end{document}