dm.tex 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. \documentclass[11pt]{article}
  2. \usepackage{fullpage}
  3. \usepackage[bookmarks,hidelinks]{hyperref}
  4. \usepackage{lscape}
  5. %% Corrige quelques erreurs de LaTeX2ε
  6. \usepackage{fixltx2e}
  7. \usepackage{xspace}
  8. \usepackage{microtype}
  9. %% Pour ne pas commettre les erreurs fréquentes décrites dans l2tabu
  10. \usepackage[l2tabu,abort]{nag}
  11. %% Saisie en UTF-8
  12. \usepackage[utf8]{inputenc}
  13. %% Fonte : cf. http://www.tug.dk/FontCatalogue/
  14. \usepackage[T1]{fontenc}
  15. \usepackage{lmodern}
  16. \usepackage{csquotes}
  17. %% Pour écrire du français
  18. % \usepackage[frenchb]{babel}
  19. %% Pour composer des mathématiques
  20. \usepackage{mathtools}
  21. \usepackage{amsfonts}
  22. \usepackage{amssymb}
  23. \usepackage{amsthm}
  24. \usepackage{stmaryrd}
  25. \usepackage{semantic}
  26. \newtheorem{lemma}{Lemma}
  27. \newtheorem{definition}{Definition}
  28. %% Commandes du paquet semantic : langage While
  29. \reservestyle{\command}{\textbf}
  30. \command{skip,while,do,if,then,else,not,and,or,true,false,int}
  31. %% -> Utiliser \<begin> Pour styliser le mot-clé
  32. \newcommand{\reduce}{\rightarrow^{*}}
  33. \begin{document}
  34. \author{Timoth\'ee Haudebourg \and Thibaut Marty}
  35. \title{Homework PAS/SDL}
  36. \date{November 18, 2016}
  37. \maketitle
  38. \section{Introduction}
  39. % TODO
  40. \section{Small-step Semantic}
  41. \subsection{Domains}
  42. The $State$ domain of evaluation is defined as:
  43. \[State = (Var \rightharpoonup Value) \cup \bot\]
  44. Where $Var$ is the set of variables, and $Value$ the set of possible values.
  45. We use $\bot$ to represent the error state.
  46. In our model, a record is represented as a partial application $Field \rightharpoonup Value$.
  47. This means that a record field can contains a record itself.
  48. To do so, we define the set of value as:
  49. \begin{align*}
  50. Value &= \bigcup_{i\in\mathbb{N}} Value_i \\
  51. Value_n &= \mathbb{N} \cup (Field \rightharpoonup Value_{n+1}) \\
  52. \end{align*}
  53. Because the variables are updated by copying, the codomain of the partial application is merely $Value$.
  54. There is no references system.
  55. \subsection{Denotational Semantic for expressions}
  56. We define $\mathcal{A}$ the denotational semantics of arithmetic expressions as:
  57. \[ \mathcal{A}|[ \bullet |] : AExpr \rightarrow State \rightarrow (Value \cup \bot) \]
  58. where
  59. \begin{align*}
  60. \mathcal{A} |[ n |] \sigma &= \mathcal{N} |[n|] \\
  61. \mathcal{A} |[ x |] \sigma &= \sigma x \\
  62. \mathcal{A} |[ e_1\ o\ e_2 |] \sigma &= \mathcal{A} |[ e_1 |] \sigma\ \bar{o}\ \mathcal{A} |[ e_2 |] \sigma, o \in \{+, -, \times\} \\
  63. \mathcal{A} |[ e.f |] \sigma &= \mathcal{A}|[e|] \sigma f\\
  64. \mathcal{A} |[ e_1{f \mapsto e_2} |] \sigma f &= \mathcal{A}|[ e_1 |] \sigma [f \mapsto \mathcal{A}|[ e_2 |] \sigma ]\\
  65. \mathcal{A} |[ \{f_1 = e_1 ; \dots ; f_n = e_n\} |] \sigma f_i &= \mathcal{A}|[ e_i |] \sigma \\
  66. \end{align*}
  67. For the undefined cases, for example when we try to add two records, we also note $\mathcal{A}|[e|] \sigma = \bot$.
  68. In the rest of this homwork, we suppose $\mathcal{B}|[ \bullet |]$ the denotational semantic for booleans expressions already defined.
  69. \subsection{Structural Operational Semantic for commands}
  70. We define $->$ the structural operational semantics for commands as:
  71. \[ ->\ \subseteq (Statement \times State) \times ((Statement \times State) \cup State) \]
  72. \subsubsection{Regular execution}
  73. For all $\sigma \neq \bot$:
  74. \[
  75. \inference{}{(\<skip>, \sigma) -> \sigma}
  76. \]
  77. \[
  78. \inference{}{(x := a, \sigma) -> \sigma[x \mapsto \mathcal{A}|[a|] \sigma]}{\text{if }\mathcal{A}|[a|] \sigma \neq \bot}
  79. \]
  80. \[
  81. \inference{(S_1, \sigma) -> (S_1', \sigma')}{(S_1 ; S_2, \sigma) -> (S_1' ; S_2, \sigma')}
  82. \]
  83. \[
  84. \inference{(S_1, \sigma) -> \sigma'}{(S_1 ; S_2, \sigma) -> (S_2, \sigma')}
  85. \]
  86. \[
  87. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> (S ; \<while>\ b\ \<do>\ S, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  88. \]
  89. \[
  90. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  91. \]
  92. \[
  93. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_1, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  94. \]
  95. \[
  96. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_2, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  97. \]
  98. \subsubsection{Error handling}
  99. An error can occurs when one tries to add (or compare) records.
  100. In that case, according to our definition of arithmetic expressions, $\mathcal{A}|[e|] \sigma$ (or $\mathcal{B}|[b|]$) will returns $\bot$.
  101. From this point, the execution of the program makes no sense, and we will return the error state.
  102. \[
  103. \inference{}{(x := a, \sigma) -> \bot}{\text{if }\mathcal{A}|[a|] \sigma = \bot}
  104. \]
  105. \[
  106. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  107. \]
  108. \[
  109. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  110. \]
  111. To ensure that the error can make it through the end, we also add the error propagation rule:
  112. \[
  113. \inference{}{(S, \bot) -> \bot}
  114. \]
  115. Finally, note that according to the exercise statement, we suppose that all variables are already defined in $\sigma$,
  116. so that no error can occur because of an undefined variable.
  117. \section{Type system}
  118. We suppose now that every variable in the program is declared with a type satisfying the following syntax:
  119. \[
  120. t\quad::=\quad\text{int }|\ \{f_1 : t_1;\ \dots\ ; f_n : t_n\}
  121. \]
  122. \subsection{Definition}
  123. We note $\Gamma \vdash S$ the type judgment for our language, defined by the following typing rules:
  124. \[
  125. \inference{}{\Gamma \vdash n : int}
  126. \]
  127. \[
  128. \inference{(x : T) \in \Gamma}{\Gamma \vdash x : T}
  129. \]
  130. \[
  131. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1\ o\ a_2 : int}{o \in \{+, -, \times\}}
  132. \]
  133. Records
  134. \[
  135. \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\}}
  136. \]
  137. Field assignment
  138. \[
  139. \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\}}
  140. \]
  141. \[
  142. \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\}}
  143. \]
  144. Field lookup
  145. \[
  146. \inference{\Gamma \vdash e : \{f : T\}}{\Gamma \vdash e.f : T}
  147. \]
  148. As is, the last rule can seems too restrictive.
  149. Indeed, an expression $e$ does not have to match \emph{exactly} the type $\{f : T\}$ to provide the field $f$.
  150. It can contains some other fields.
  151. We want the type $\{f : T\}$ to express \enquote{any record with a field $f$ of type $T$}.
  152. To do this, we introduce the following sub-typing relation:
  153. \[
  154. \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\}}
  155. \]
  156. % FIXME on avait noté la relation <: dans l'autre sens
  157. Note that in order to assure the reflexivity of our relation,
  158. a type $S$ is a sub-type of $T$ if all the fields of $T$ are included in $S$.
  159. We also introduce a new subsumption typing rule:
  160. \[
  161. \inference{\Gamma \vdash e : S & S <: T}{\Gamma \vdash e : T}
  162. \]
  163. Assignment
  164. \[
  165. \inference{\Gamma \vdash x : T & \Gamma \vdash a : T}{\Gamma \vdash x := a}
  166. \]
  167. Sequence
  168. \[
  169. \inference{\Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash (S_1 ; S_2)}
  170. \]
  171. While loop
  172. \[
  173. \inference{\Gamma \vdash b & \Gamma \vdash S}{\Gamma \vdash \<while>\ b\ \<do>\ S}
  174. \]
  175. Condition
  176. \[
  177. \inference{\Gamma \vdash b & \Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash \<if>\ b\ \<then>\ S_1\ \<else>\ S_2}
  178. \]
  179. As the booleans expressions can contain expressions which can contain variables, they are not necessarily well typed. Hence we need to define typing rules on booleans:
  180. \[
  181. \inference{}{\Gamma \vdash \<true>}
  182. \]
  183. \[
  184. \inference{}{\Gamma \vdash \<false>}
  185. \]
  186. \[
  187. \inference{\Gamma \vdash b}{\Gamma \vdash \<not>\ b}
  188. \]
  189. \[
  190. \inference{\Gamma \vdash b_1 & \Gamma \vdash b_2}{\Gamma \vdash b_1\ o\ b_2}{o \in \{\<and>, \<or>\}}
  191. \]
  192. \[
  193. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1 \le a_2}
  194. \]
  195. \[
  196. \inference{\Gamma \vdash a_1 : T & \Gamma \vdash a_2 : T}{\Gamma \vdash a_1 = a_2}
  197. \]
  198. Note: for the equality test, the rule works for \<int> as well as for records.
  199. % Domains
  200. \[
  201. n \in Num
  202. \]
  203. \[
  204. x \in Var
  205. \]
  206. \[
  207. e,e_1,e_2,a,a_1,a_2 \in AExpr
  208. \]
  209. \[
  210. b,b_1,b_2 \in BExpr
  211. \]
  212. \[
  213. f,f_1,\dots,f_n,f_i,g_1,\dots,g_k, \in Field % Field n'est pas défini…
  214. \]
  215. \[
  216. S,S_1,S_2 \in Statement
  217. \]
  218. \input{sections/correctness.tex}
  219. \section{Static analysis}
  220. We propose a constraint based static analysis to determine at each instruction which field is defined in each variables.
  221. Our static analysis is incarnated by the function $RF$ (\enquote{Reachable Fields}).
  222. \[RF : Lab -> Var -> \mathcal{P}(Field)\]
  223. where $Lab$ is the set of labels.
  224. One unique label is assigned to each instruction.
  225. For the sake of simplicity, we note $[I]^l$ the instruction of label $l$.
  226. \subsection{Definition}
  227. Here is the definition of the static analysis.
  228. We suppose that we dispose of the control flow graph $flow(P)$ of the program.
  229. \[RF(l) = \bigcap_{(l', l) \in flow(P)} RF_{out}(l')\]
  230. Where $RF_{out}$ is defined by:
  231. \begin{align*}
  232. RF_{out}([\<skip>]^l) &= RF(l) \\
  233. RF_{out}([b]^l) &= RF(l)\quad(b \in BExp)\\
  234. RF_{out}([x := e]^l) &= RF(l)[x \mapsto \mathcal{A}^\#|[e|] RF(l)]
  235. \end{align*}
  236. Where $\mathcal{A}^\#$ is defined by:
  237. \[\mathcal{A}^\#|[\bullet|] : AExpr -> (Var -> \mathcal{P}(Field)) -> \mathcal{P}(Field)\]
  238. \begin{align*}
  239. \mathcal{A}^\#|[n|] \sigma &= \emptyset \\
  240. \mathcal{A}^\#|[x|] \sigma &= \sigma x \\
  241. \mathcal{A}^\#|[\{f_1 = e_1; \dots; f_n = e_n\}|] \sigma &= \{f_1; \dots; f_n\} \\
  242. \mathcal{A}^\#|[e_1[f \mapsto e_2]|] \sigma &= \mathcal{A}^\#|[e_1|] \sigma \cup \{f\} \\
  243. \mathcal{A}^\#|[e.f|] \sigma &= \emptyset
  244. \end{align*}
  245. \subsection{Example}
  246. Let $S_1 = [x := \{f_1 = 0; f_2 = 42\}]^1$,
  247. $S_3 = [y := \{f_1 = x.f_1 + 1\}]^3$,
  248. $S_4 = [x := y]^4$, and
  249. \[S = \<while>\ ([x.f_1 \le 100]^2)\ \<do>\ (S_3 ; S_4)\].
  250. We perform the static analysis for the program $[S_1 ; S]^5$:
  251. Let's begin with the initial state:
  252. \begin{align*}
  253. RF(1) &= \emptyset \\
  254. RF(2) &= RF_{out}(1) \cap RF(4)_{out} \\
  255. RF(3) &= RF(2)_{out} \\
  256. RF(4) &= RF(3)_{out} \\
  257. RF(5) &= RF(2)_{out}
  258. \end{align*}
  259. \begin{align*}
  260. RF_{out}(1) &= RF(1)[x \mapsto \{f_1, f_2\}] \\
  261. RF_{out}(2) &= RF(2) \\
  262. RF_{out}(3) &= RF(3)[y \mapsto \{f_1\}] \\
  263. RF_{out}(4) &= RF(4)[x \mapsto \{f_1\}]
  264. \end{align*}
  265. Now we can solve those equations and found:
  266. \begin{align*}
  267. RF(1) &= \emptyset \\
  268. RF(2) &= RF(1)[x \mapsto \{f_1, f_2\}] \cap RF(4)[x \mapsto \{f_1\}] = \{x \mapsto \{f_1\}\} \\
  269. RF(3) &= RF(2) = \{x \mapsto \{f_1\}\} \\
  270. RF(4) &= RF(3)[y \mapsto \{f_1\}] = \{x \mapsto \{f_1\}, y \mapsto \{f_1\}\} \\
  271. RF(5) &= RF(2) = \{x \mapsto \{f_1\}\}
  272. \end{align*}
  273. \subsection{Termination}
  274. We can design an algorithm to solve this equation system under the hypothesis that there is no label $l$ such as $(l, l) \in flow(P)$.
  275. At each step we can :
  276. \begin{itemize}
  277. \item Rewrite a $RF(l)$ as $\bigcap_{(l', l) \in flow(P)} RF_{out}(l')$, where $RF_{out}(l')$ can be simplified into a term that does not contains $RF(l)$ (because $(l, l) \not\in flow(P)$).
  278. \item Because a program contains a finite number of instructions, it exists a time when no $RF(l)$ is left. The system is solved.
  279. \end{itemize}
  280. \subsection{Safety}
  281. This is a pessimistic static analysis. Due to the intersection, we know that $RF(l)$ contains no field that is never defined during the execution (...need some material to do the proof).
  282. \section{Discussion of the type system and static analysis}
  283. % The static analysis and the type system are complementary to prove
  284. % is pessimistic by nature, so it will reject some programs which are legals.
  285. The static analysis of the program $x := \{f = 1\} ; y := 3 + x$ works because there is no undefined field reading.
  286. The variable $x$ contains one field $f$, and no other fields are read.
  287. However, the type system rejects this program.
  288. Indeed, the expression $3 + x$ is not well typed as the type of $x$ is $\{f:int\}$, not $int$.
  289. The program $x := \{\}; \<if>\ b\ \<then>\ x.f := 1\ \<else>\ \<skip>; y := x.f$ is well typed because the type $\{\}$ is a sub-type of the type $\{f:int\}$.
  290. However, its static analysis fails.
  291. The static analysis will take the worst case of the two branches of the \<if> statement, that is the \<else> branch when $x$ doesn't get the extra field $f$.
  292. Thus, the last statement, $y := x.f$, tries to reach the undefined (according to the analysis) field $f$.
  293. \subsection{Extension of the static analysis}
  294. \end{document}