dm.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. Some programs of a language can be incorrect, that is programs which their execution will make no sense.
  40. Ensuring that a program does exactly what we want it to do is a hard task.
  41. Since Rice and his famous theorem, we know that automatically checking that a program respects its specification is impossible (in general).
  42. There exists some techniques, not to ensure the validity of a program, but at least ensure that a program does not contains a certain type of nonsense.
  43. Using a type system, and type checking, is one way to ensure that no operation in the program is performed when it has no sense.
  44. Data-flow analysis for reachability is one other way to ensure that all used objects are defined.
  45. However theses analysis are doomed to be imperfect.
  46. That means they will either reject or accept programs even if they are respectively correct or not.
  47. Such an analysis will always made some assumption that will not be true for every execution.
  48. In this work, we first define a semantic for a small imperative language, \emph{While}.
  49. Then, we define a type system and a static analysis for this language.
  50. We will see that both of them have different flows. Each complement the other, and none of them are perfect.
  51. \section{Small-step Semantics}
  52. \subsection{Domains}
  53. The $State$ domain of evaluation is defined as:
  54. \[State = (Var \rightharpoonup Value) \cup \bot\]
  55. Where $Var$ is the set of variables, and $Value$ the set of possible values.
  56. We use $\bot$ to represent the error state.
  57. In our model, a record is represented as a partial application $Field \rightharpoonup Value$.
  58. This means that a record field can contains a record itself.
  59. To do so, we define the set of value as:
  60. \begin{align*}
  61. Value &= \bigcup_{i\in\mathbb{N}} Value_i \\
  62. Value_n &= \mathbb{N} \cup (Field \rightharpoonup Value_{n+1}) \\
  63. \end{align*}
  64. Because the variables are updated by copying, the codomain of the partial application is merely $Value$.
  65. There is no references system.
  66. \subsection{Denotational Semantic for expressions}
  67. We define $\mathcal{A}$ the denotational semantics of arithmetic expressions as:
  68. \[ \mathcal{A}|[ \bullet |] : AExpr \rightarrow State \rightarrow (Value \cup \bot) \]
  69. where
  70. \begin{align*}
  71. \mathcal{A} |[ n |] \sigma &= \mathcal{N} |[n|] \\
  72. \mathcal{A} |[ x |] \sigma &= \sigma x \\
  73. \mathcal{A} |[ e_1\ o\ e_2 |] \sigma &= \mathcal{A} |[ e_1 |] \sigma\ \bar{o}\ \mathcal{A} |[ e_2 |] \sigma, o \in \{+, -, \times\} \\
  74. \mathcal{A} |[ e.f |] \sigma &= \mathcal{A}|[e|] \sigma f\\
  75. \mathcal{A} |[ e_1{f \mapsto e_2} |] \sigma f &= \mathcal{A}|[ e_1 |] \sigma [f \mapsto \mathcal{A}|[ e_2 |] \sigma ]\\
  76. \mathcal{A} |[ \{f_1 = e_1 ; \dots ; f_n = e_n\} |] \sigma f_i &= \mathcal{A}|[ e_i |] \sigma \\
  77. \end{align*}
  78. For the undefined cases, for example when we try to add two records, we also note $\mathcal{A}|[e|] \sigma = \bot$.
  79. In the rest of this work, we suppose $\mathcal{B}|[ \bullet |]$ the denotational semantic for booleans expressions already defined.
  80. \subsection{Structural Operational Semantic for commands}
  81. We define $->$ the structural operational semantics for commands as:
  82. \[ ->\ \subseteq (Statement \times State) \times ((Statement \times State) \cup State) \]
  83. \subsubsection{Regular execution}
  84. For all $\sigma \neq \bot$:
  85. \[
  86. \inference{}{(\<skip>, \sigma) -> \sigma}
  87. \]
  88. \[
  89. \inference{}{(x := a, \sigma) -> \sigma[x \mapsto \mathcal{A}|[a|] \sigma]}{\text{if }\mathcal{A}|[a|] \sigma \neq \bot}
  90. \]
  91. \[
  92. \inference{(S_1, \sigma) -> (S_1', \sigma')}{(S_1 ; S_2, \sigma) -> (S_1' ; S_2, \sigma')}
  93. \]
  94. \[
  95. \inference{(S_1, \sigma) -> \sigma'}{(S_1 ; S_2, \sigma) -> (S_2, \sigma')}
  96. \]
  97. \[
  98. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> (S ; \<while>\ b\ \<do>\ S, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  99. \]
  100. \[
  101. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  102. \]
  103. \[
  104. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_1, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  105. \]
  106. \[
  107. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_2, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  108. \]
  109. \subsubsection{Error handling}
  110. An error can occurs when one tries to add (or compare) records.
  111. In that case, according to our definition of arithmetic expressions, $\mathcal{A}|[e|] \sigma$ (or $\mathcal{B}|[b|]$) will returns $\bot$.
  112. From this point, the execution of the program makes no sense, and we will return the error state.
  113. \[
  114. \inference{}{(x := a, \sigma) -> \bot}{\text{if }\mathcal{A}|[a|] \sigma = \bot}
  115. \]
  116. \[
  117. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  118. \]
  119. \[
  120. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  121. \]
  122. To ensure that the error can make it through the end, we also add the error propagation rule:
  123. \[
  124. \inference{}{(S, \bot) -> \bot}
  125. \]
  126. Finally, note that according to the exercise statement, we suppose that all variables are already defined in $\sigma$,
  127. so that no error can occur because of an undefined variable.
  128. \section{Type system}
  129. We suppose now that every variable in the program is declared with a type satisfying the following syntax:
  130. \[
  131. t\quad::=\quad\text{int }|\ \{f_1 : t_1;\ \dots\ ; f_n : t_n\}
  132. \]
  133. \subsection{Definition}
  134. We note $\Gamma \vdash S$ the type judgment for our language, defined by the following typing rules:
  135. \[
  136. \inference{}{\Gamma \vdash n : int}
  137. \]
  138. \[
  139. \inference{(x : T) \in \Gamma}{\Gamma \vdash x : T}
  140. \]
  141. \[
  142. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1\ o\ a_2 : int}{o \in \{+, -, \times\}}
  143. \]
  144. Records
  145. \[
  146. \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\}}
  147. \]
  148. Field assignment
  149. \[
  150. \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\}}
  151. \]
  152. \[
  153. \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\}}
  154. \]
  155. Field lookup
  156. \[
  157. \inference{\Gamma \vdash e : \{f : T\}}{\Gamma \vdash e.f : T}
  158. \]
  159. As is, the last rule can seem too restrictive.
  160. Indeed, an expression $e$ does not have to match \emph{exactly} the type $\{f : T\}$ to provide the field $f$.
  161. It can contain some other fields.
  162. We want the type $\{f : T\}$ to express \enquote{any record with a field $f$ of type $T$}.
  163. To do this, we introduce the following sub-typing relation:
  164. \[
  165. \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\}}
  166. \]
  167. Note that in order to assure the reflexivity of our relation,
  168. a type $S$ is a sub-type of $T$ if all the fields of $T$ are included in $S$.
  169. We also introduce a new subsumption typing rule:
  170. \[
  171. \inference{\Gamma \vdash e : S & S <: T}{\Gamma \vdash e : T}
  172. \]
  173. Assignment
  174. \[
  175. \inference{\Gamma \vdash x : T & \Gamma \vdash a : T}{\Gamma \vdash x := a}
  176. \]
  177. Sequence
  178. \[
  179. \inference{\Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash (S_1 ; S_2)}
  180. \]
  181. While loop
  182. \[
  183. \inference{\Gamma \vdash b & \Gamma \vdash S}{\Gamma \vdash \<while>\ b\ \<do>\ S}
  184. \]
  185. Condition
  186. \[
  187. \inference{\Gamma \vdash b & \Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash \<if>\ b\ \<then>\ S_1\ \<else>\ S_2}
  188. \]
  189. As the boolean expressions can contain expressions which can contain variables, they are not necessarily well typed. Hence we need to define typing rules on booleans:
  190. \[
  191. \inference{}{\Gamma \vdash \<true>}
  192. \]
  193. \[
  194. \inference{}{\Gamma \vdash \<false>}
  195. \]
  196. \[
  197. \inference{\Gamma \vdash b}{\Gamma \vdash \<not>\ b}
  198. \]
  199. \[
  200. \inference{\Gamma \vdash b_1 & \Gamma \vdash b_2}{\Gamma \vdash b_1\ o\ b_2}{o \in \{\<and>, \<or>\}}
  201. \]
  202. \[
  203. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1 \le a_2}
  204. \]
  205. \[
  206. \inference{\Gamma \vdash a_1 : T & \Gamma \vdash a_2 : T}{\Gamma \vdash a_1 = a_2}
  207. \]
  208. Note: for the equality test, the rule works for \<int> as well as for records.
  209. % Domains
  210. \[
  211. n \in Num
  212. \]
  213. \[
  214. x \in Var
  215. \]
  216. \[
  217. e,e_1,e_2,a,a_1,a_2 \in AExpr
  218. \]
  219. \[
  220. b,b_1,b_2 \in BExpr
  221. \]
  222. \[
  223. f,f_1,\dots,f_n,f_i,g_1,\dots,g_k, \in Field % Field n'est pas défini…
  224. \]
  225. \[
  226. S,S_1,S_2 \in Statement
  227. \]
  228. \input{sections/correctness.tex}
  229. \section{Static analysis}
  230. We propose a constraint based static analysis to determine at each instruction which field is defined in each variables.
  231. Our static analysis is incarnated by the function $RF$ (\enquote{Reachable Fields}).
  232. \[RF : Lab -> Var -> \mathcal{P}(Field)\]
  233. where $Lab$ is the set of labels.
  234. One unique label is assigned to each instruction.
  235. For the sake of simplicity, we note $[I]^l$ the instruction of label $l$.
  236. \subsection{Definition}
  237. Here is the definition of the static analysis.
  238. We suppose that we dispose of the control flow graph $flow(P)$ of the program.
  239. \[RF(l) = \bigcap_{(l', l) \in flow(P)} RF_{out}(l')\]
  240. Where $RF_{out}$ is defined by:
  241. \begin{align*}
  242. RF_{out}([\<skip>]^l) &= RF(l) \\
  243. RF_{out}([b]^l) &= RF(l)\quad(b \in BExp)\\
  244. RF_{out}([x := e]^l) &= RF(l)[x \mapsto \mathcal{A}^\#|[e|] RF(l)]
  245. \end{align*}
  246. Where $\mathcal{A}^\#$ is defined by:
  247. \[\mathcal{A}^\#|[\bullet|] : AExpr -> (Var -> \mathcal{P}(Field)) -> \mathcal{P}(Field)\]
  248. \begin{align*}
  249. \mathcal{A}^\#|[n|] \sigma &= \emptyset \\
  250. \mathcal{A}^\#|[x|] \sigma &= \sigma x \\
  251. \mathcal{A}^\#|[\{f_1 = e_1; \dots; f_n = e_n\}|] \sigma &= \{f_1; \dots; f_n\} \\
  252. \mathcal{A}^\#|[e_1[f \mapsto e_2]|] \sigma &= \mathcal{A}^\#|[e_1|] \sigma \cup \{f\} \\
  253. \mathcal{A}^\#|[e.f|] \sigma &= \emptyset
  254. \end{align*}
  255. \subsection{Example}
  256. Let $S_1 = [x := \{f_1 = 0; f_2 = 42\}]^1$,
  257. $S_3 = [y := \{f_1 = x.f_1 + 1\}]^3$,
  258. $S_4 = [x := y]^4$, and
  259. \[S = \<while>\ ([x.f_1 \le 100]^2)\ \<do>\ (S_3 ; S_4)\].
  260. We perform the static analysis for the program $[S_1 ; S]^5$:
  261. Let's begin with the initial state:
  262. \begin{align*}
  263. RF(1) &= \emptyset \\
  264. RF(2) &= RF_{out}(1) \cap RF(4)_{out} \\
  265. RF(3) &= RF(2)_{out} \\
  266. RF(4) &= RF(3)_{out} \\
  267. RF(5) &= RF(2)_{out}
  268. \end{align*}
  269. \begin{align*}
  270. RF_{out}(1) &= RF(1)[x \mapsto \{f_1, f_2\}] \\
  271. RF_{out}(2) &= RF(2) \\
  272. RF_{out}(3) &= RF(3)[y \mapsto \{f_1\}] \\
  273. RF_{out}(4) &= RF(4)[x \mapsto \{f_1\}]
  274. \end{align*}
  275. Now we can solve those equations and found:
  276. \begin{align*}
  277. RF(1) &= \emptyset \\
  278. RF(2) &= RF(1)[x \mapsto \{f_1, f_2\}] \cap RF(4)[x \mapsto \{f_1\}] = \{x \mapsto \{f_1\}\} \\
  279. RF(3) &= RF(2) = \{x \mapsto \{f_1\}\} \\
  280. RF(4) &= RF(3)[y \mapsto \{f_1\}] = \{x \mapsto \{f_1\}, y \mapsto \{f_1\}\} \\
  281. RF(5) &= RF(2) = \{x \mapsto \{f_1\}\}
  282. \end{align*}
  283. \subsection{Termination}
  284. 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)$.
  285. At each step we can:
  286. \begin{itemize}
  287. \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 contain $RF(l)$ (because $(l, l) \not\in flow(P)$).
  288. \item Because a program contains a finite number of instructions, it exists a time when no $RF(l)$ is left. The system is solved.
  289. \end{itemize}
  290. \subsection{Safety}
  291. 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).
  292. \section{Discussion on the type system and static analysis}
  293. The absence of undefined field reading is performed by two different ways by the type system and the static analysis.
  294. On one hand the type system focuses on the expressions of a program, i.\,e. a statement is well typed if its expressions are well typed.
  295. On the other hand the static analysis focuses one the commands.
  296. Therefore, they are complementary.
  297. The static analysis of the program $x := \{f = 1\} ; y := 3 + x$ works because there is no undefined field reading.
  298. The variable $x$ contains one field $f$, and no other fields are read.
  299. However, the type system rejects this program.
  300. Indeed, the expression $3 + x$ is not well typed as the type of $x$ is $\{f:int\}$, not $int$.
  301. 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\}$.
  302. However, its static analysis fails.
  303. The static analysis will take the worst case of the two branches of the \<if> statement, that is the \<else> branch when $x$ does not get the extra field $f$.
  304. Thus, the last statement, $y := x.f$, tries to reach the undefined (according to the analysis) field $f$.
  305. \subsection{Extension of the static analysis}
  306. A better solution of the two previous approaches is the union of them.
  307. In other words, we could extend the static analysis by using the result of the type checking.
  308. In this way, the two previous examples will be rightly rejected by the new analysis.
  309. \end{document}