dm.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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{Small-step Semantic}
  39. \subsection{Domains}
  40. The $State$ domain of evaluation is defined as:
  41. \[State = (Var \rightharpoonup Value) \cup \bot\]
  42. Where $Var$ is the set of variables, and $Value$ the set of possible values.
  43. We use $\bot$ to represent the error state.
  44. In our model, a record is represented as a partial application $Field \rightharpoonup Value$.
  45. This means that a record field can contains a record itself.
  46. To do so, we define the set of value as:
  47. \begin{align*}
  48. Value &= \bigcup_{i\in\mathbb{N}} Value_i \\
  49. Value_n &= \mathbb{N} \cup (Field \rightharpoonup Value_{n+1}) \\
  50. \end{align*}
  51. Because the variables are updated by copying, the codomain of the partial application is merely $Value$.
  52. There is no references system.
  53. \subsection{Denotational Semantic for expressions}
  54. We define $\mathcal{A}$ the denotational semantics of arithmetic expressions as:
  55. \[ \mathcal{A}|[ \bullet |] : AExpr \rightarrow State \rightarrow (Value \cup \bot) \]
  56. where
  57. \begin{align*}
  58. \mathcal{A} |[ n |] \sigma &= \mathcal{N} |[n|] \\
  59. \mathcal{A} |[ x |] \sigma &= \sigma x \\
  60. \mathcal{A} |[ e_1\ o\ e_2 |] \sigma &= \mathcal{A} |[ e_1 |] \sigma\ \bar{o}\ \mathcal{A} |[ e_2 |] \sigma, o \in \{+, -, \times\} \\
  61. \mathcal{A} |[ e.f |] \sigma &= \mathcal{A}|[e|] \sigma f\\
  62. \mathcal{A} |[ e_1{f \mapsto e_2} |] \sigma f &= \mathcal{A}|[ e_1 |] \sigma [f \mapsto \mathcal{A}|[ e_2 |] \sigma ]\\
  63. \mathcal{A} |[ \{f_1 = e_1 ; \dots ; f_n = e_n\} |] \sigma f_i &= \mathcal{A}|[ e_i |] \sigma \\
  64. \end{align*}
  65. For the undefined cases, for example when we try to add two records, we also note $\mathcal{A}|[e|] \sigma = \bot$.
  66. In the rest of this homwork, we suppose $\mathcal{B}|[ \bullet |]$ the denotational semantic for booleans expressions already defined.
  67. \subsection{Structural Operational Semantic for commands}
  68. We define $->$ the structural operational semantics for commands as:
  69. \[ ->\ \subseteq (Statement \times State) \times ((Statement \times State) \cup State) \]
  70. \subsubsection{Regular execution}
  71. For all $\sigma \neq \bot$:
  72. \[
  73. \inference{}{(\<skip>, \sigma) -> \sigma}
  74. \]
  75. \[
  76. \inference{}{(x := a, \sigma) -> \sigma[x \mapsto \mathcal{A}|[a|] \sigma]}{\text{if }\mathcal{A}|[a|] \sigma \neq \bot}
  77. \]
  78. \[
  79. \inference{(S_1, \sigma) -> (S_1', \sigma')}{(S_1 ; S_2, \sigma) -> (S_1' ; S_2, \sigma')}
  80. \]
  81. \[
  82. \inference{(S_1, \sigma) -> \sigma'}{(S_1 ; S_2, \sigma) -> (S_2, \sigma')}
  83. \]
  84. \[
  85. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> (S ; \<while>\ b\ \<do>\ S, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  86. \]
  87. \[
  88. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  89. \]
  90. \[
  91. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_1, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{tt}}
  92. \]
  93. \[
  94. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> (S_2, \sigma)}{\text{if } \mathcal{B}|[b|] \sigma = \text{ff}}
  95. \]
  96. \subsubsection{Error handling}
  97. An error can occurs when one tries to add (or compare) records.
  98. In that case, according to our definition of arithmetic expressions, $\mathcal{A}|[e|] \sigma$ (or $\mathcal{B}|[b|]$) will returns $\bot$.
  99. From this point, the execution of the program makes no sense, and we will return the error state.
  100. \[
  101. \inference{}{(x := a, \sigma) -> \bot}{\text{if }\mathcal{A}|[a|] \sigma = \bot}
  102. \]
  103. \[
  104. \inference{}{(\<while>\ b\ \<do>\ S, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  105. \]
  106. \[
  107. \inference{}{(\<if>\ b\ \<then>\ S_1\ \<else>\ S_2, \sigma) -> \bot}{\text{if } \mathcal{B}|[b|] \sigma = \bot}
  108. \]
  109. To ensure that the error can make it through the end, we also add the error propagation rule:
  110. \[
  111. \inference{}{(S, \bot) -> \bot}
  112. \]
  113. Finally, note that according to the exercise statement, we suppose that all variables are already defined in $\sigma$,
  114. so that no error can occur because of an undefined variable.
  115. \section{Type system}
  116. We suppose now that every variable in the program is declared with a type satisfying the following syntax:
  117. \[
  118. t\quad::=\quad\text{int }|\ \{f_1 : t_1;\ \dots\ ; f_n : t_n\}
  119. \]
  120. \subsection{Definition}
  121. We note $\Gamma \vdash S$ the type judgment for our language, defined by the following typing rules:
  122. \[
  123. \inference{}{\Gamma \vdash n : int}
  124. \]
  125. \[
  126. \inference{(x : T) \in \Gamma}{\Gamma \vdash x : T}
  127. \]
  128. \[
  129. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1\ o\ a_2 : int}{o \in \{+, -, \times\}}
  130. \]
  131. Records
  132. \[
  133. \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\}}
  134. \]
  135. Field assignment
  136. \[
  137. \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\}}
  138. \]
  139. \[
  140. \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\}}
  141. \]
  142. Field lookup
  143. \[
  144. \inference{\Gamma \vdash e : \{f : T\}}{\Gamma \vdash e.f : T}
  145. \]
  146. As is, the last rule can seems too restrictive.
  147. Indeed, an expression $e$ does not have to match \emph{exactly} the type $\{f : T\}$ to provide the field $f$.
  148. It can contains some other fields.
  149. We want the type $\{f : T\}$ to express \enquote{any record with a field $f$ of type $T$}.
  150. To do this, we introduce the following sub-typing relation:
  151. \[
  152. \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\}}
  153. \]
  154. % FIXME on avait noté la relation <: dans l'autre sens
  155. Note that in order to assure the reflexivity of our relation,
  156. a type $S$ is a sub-type of $T$ if all the fields of $T$ are included in $S$.
  157. We also introduce a new subsumption typing rule:
  158. \[
  159. \inference{\Gamma \vdash e : S & S <: T}{\Gamma \vdash e : T}
  160. \]
  161. Assignment
  162. \[
  163. \inference{\Gamma \vdash x : T & \Gamma \vdash a : T}{\Gamma \vdash x := a}
  164. \]
  165. Sequence
  166. \[
  167. \inference{\Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash (S_1 ; S_2)}
  168. \]
  169. While loop
  170. \[
  171. \inference{\Gamma \vdash b & \Gamma \vdash S}{\Gamma \vdash \<while>\ b\ \<do>\ S}
  172. \]
  173. Condition
  174. \[
  175. \inference{\Gamma \vdash b & \Gamma \vdash S_1 & \Gamma \vdash S_2}{\Gamma \vdash \<if>\ b\ \<then>\ S_1\ \<else>\ S_2}
  176. \]
  177. 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:
  178. \[
  179. \inference{}{\Gamma \vdash \<true>}
  180. \]
  181. \[
  182. \inference{}{\Gamma \vdash \<false>}
  183. \]
  184. \[
  185. \inference{\Gamma \vdash b}{\Gamma \vdash \<not>\ b}
  186. \]
  187. \[
  188. \inference{\Gamma \vdash b_1 & \Gamma \vdash b_2}{\Gamma \vdash b_1\ o\ b_2}{o \in \{\<and>, \<or>\}}
  189. \]
  190. \[
  191. \inference{\Gamma \vdash a_1 : int & \Gamma \vdash a_2 : int}{\Gamma \vdash a_1 \le a_2}
  192. \]
  193. \[
  194. \inference{\Gamma \vdash a_1 : T & \Gamma \vdash a_2 : T}{\Gamma \vdash a_1 = a_2}
  195. \]
  196. Note: for the equality test, the rule works for \<int> as well as for records.
  197. % Domains
  198. \[
  199. n \in Num
  200. \]
  201. \[
  202. x \in Var
  203. \]
  204. \[
  205. e,e_1,e_2,a,a_1,a_2 \in AExpr
  206. \]
  207. \[
  208. b,b_1,b_2 \in BExpr
  209. \]
  210. \[
  211. f,f_1,\dots,f_n,f_i,g_1,\dots,g_k, \in Field % Field n'est pas défini…
  212. \]
  213. \[
  214. S,S_1,S_2 \in Statement
  215. \]
  216. \subsection{Correctness}
  217. \begin{definition}[Well typed value]
  218. A value $v \in Value$ is well typed regarding to a type $T$ (noted $v |- T$) when :
  219. \begin{align*}
  220. v |- int &=> v \in \mathbb{N} \\
  221. 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(t_i) |- T_i
  222. \end{align*}
  223. \end{definition}
  224. \begin{definition}[Well typed state]
  225. A state $\sigma \in State$, $\sigma \neq \bot$ is well typed with regards to a type system $\Gamma$ (noted $\sigma |- \Gamma$) when
  226. \[\forall x \in Var,\ \Gamma |- x : T => x |- T\]
  227. \end{definition}
  228. \begin{lemma}[Well typed expressions cannot go wrong]
  229. $\forall e \in AExpr, \sigma \in State, \Gamma$ such as $\sigma |- \Gamma$ :
  230. \[\Gamma |- e : T => \mathcal{A}|[e|] \sigma |- T\]
  231. In particular, $\mathcal{A}|[e|] \neq \bot$.
  232. \end{lemma}
  233. \begin{proof}
  234. We demonstrate this property by induction on the structure of the expression $e$.
  235. First let assume that $\Gamma |- e : T$.
  236. We can eliminates all trivial cases in contradiction with this assumption (for example when $e = 1 + 1$ and $T = \{\}$).
  237. Here are the other base cases:
  238. \begin{itemize}
  239. \item $e = n, T = int$.
  240. $\mathcal{A}|[e|] = \mathcal{N}|[n|]$.
  241. $\mathcal{N}|[n|] \in \mathbb{N}$ so we have $\mathcal{A}|[e|] \in \mathbb{N}$ which means that $\mathcal{A}|[e|] \sigma |- T$.
  242. \item $e = n, T = int$.
  243. \item $e = x$.
  244. \item $e = e_1 o e_2, T = int$.
  245. \item $e = e_1.f$.
  246. \item $e = e_1[f \mapsto e_2]$.
  247. \item $e = \{f_1 = e_1; \dots; f_n = e_n\}$.
  248. \end{itemize}
  249. \end{proof}
  250. \begin{lemma}[Well typed programs cannot go wrong]
  251. \[\forall \Gamma P,\quad \Gamma \vdash P \; => \;\forall \sigma,\; (P, \sigma) \not\reduce \bot\]
  252. \end{lemma}
  253. \begin{proof}
  254. By induction on the path of $\reduce$, and by induction on the structure/type of the statement at each step of the path.
  255. \end{proof}
  256. \section{Static analysis}
  257. We propose a constraint based static analysis to determine at each instruction which field is defined in each variables.
  258. Our static analysis is incarnated by the function $RF$ (\enquote{Reachable Fields}).
  259. \[RF : Lab -> Var -> \mathcal{P}(Field)\]
  260. where $Lab$ is the set of labels.
  261. One unique label is assigned to each instruction.
  262. For the sake of simplicity, we note $[I]^l$ the instruction of label $l$.
  263. We also define an $init$ function returning the first label of a statement where:
  264. \begin{align*}
  265. &init([\<skip>]^l) = l\\
  266. &init([x := a]^l) = l\\
  267. &init(S_1;S_2) = init(S_1)\\
  268. &init(\<while>\ [b]^l\ \<do>\ S) = l \\
  269. &init(\<if>\ [b]^l\ \<then>\ S_1\ \<else>\ S_2) = l
  270. \end{align*}
  271. \subsection{Definition}
  272. % TODO : définir l'opérateur \vdash
  273. \[
  274. \inference{RF(l) \subseteq RF (init(S)) & RF \vdash (S, l) & RF(l) \subseteq RF(l')}{RF \vdash (\<while>\ [b]^l\ \<do>\ S, l')}
  275. \]
  276. \[
  277. \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')}
  278. \]
  279. \[
  280. \inference{RF(l) \subseteq RF(l')}{RF \vdash ([skip]^l, l')}
  281. \]
  282. \[
  283. \inference{RF \vdash (S_1, init(S_2)) & RF \vdash (S_2, l')}{RF \vdash (S_1 ; S_2, l')}
  284. \]
  285. \[
  286. \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')}
  287. \]
  288. \[
  289. % TODO : il faudrait évaluer statiquement e_1 pour être moins pessimiste
  290. \inference{RF(l)[x \mapsto \{f\}] \subseteq RF(l')}{RF \vdash ([x := e_1[f \mapsto e_2]]^l, l')}
  291. \]
  292. % autres affectations
  293. \[
  294. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := n]^l, l')}
  295. \]
  296. \[
  297. \inference{RF(l)[x \mapsto RF(l)(y)] \subseteq RF(l')}{RF \vdash ([x := y]^l, l')}
  298. \]
  299. \[
  300. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := e_1\ o\ e_2]^l, l')}
  301. \]
  302. \[
  303. \inference{RF(l)[x \mapsto \emptyset] \subseteq RF(l')}{RF \vdash ([x := e.f]^l, l')}
  304. \]
  305. \begin{landscape}
  306. \subsection{Example}
  307. Let $S_1 = [x := \{f_1 = 0; f_2 = 42\}]^1$,
  308. $S_3 = [y := \{f_1 = x.f_1 + 1\}]^3$,
  309. $S_4 = [x := y]^4$,
  310. and $S = \<while>\ ([x.f_1 \le 100]^2)\ \<do>\ (S_3 ; S_4)$.
  311. We perform the static analysis for the program $S_1 ; S$:
  312. {
  313. \footnotesize\[
  314. \inference
  315. {
  316. \inference
  317. {
  318. RF(1)[x \mapsto \{f_1, f_2\}] \subseteq RF(2)
  319. }
  320. {
  321. RF \vdash (S_1, 2)
  322. } &
  323. \inference
  324. {
  325. RF(2) \subseteq RF(3) &
  326. \inference
  327. {
  328. \inference
  329. {
  330. RF(3)[y \mapsto \{f_1\}] \subseteq RF(4)
  331. }
  332. {
  333. RF \vdash (S_3, 4)
  334. } &
  335. \inference
  336. {
  337. RF(4)[x \mapsto RF(4)(y)] \subseteq RF(2)
  338. }
  339. {
  340. RF \vdash (S_4, 2)
  341. }
  342. }
  343. {
  344. RF \vdash (S_3 ; S_4, 2)
  345. } &
  346. RF(2) \subseteq RF(l')
  347. }
  348. {
  349. RF \vdash (S, l')
  350. }
  351. }
  352. {
  353. RF \vdash (S_1; S, l')
  354. }
  355. \]
  356. }
  357. So the equation system is:
  358. \begin{align*}
  359. RF(1)[x \mapsto \{f_1, f_2\}] &\subseteq RF(2) \\
  360. RF(2) &\subseteq RF(3) \\
  361. RF(3)[y \mapsto \{f_1\}] &\subseteq RF(4) \\
  362. RF(4)[x \mapsto RF(4)(y)] &\subseteq RF(2) \\
  363. RF(2) &\subseteq RF(l')
  364. \end{align*}
  365. \end{landscape}
  366. \subsection{Termination}
  367. In each premise of each rule, the size of the statement is \emph{strictly} decreasing,
  368. assuring the termination of the analysis.
  369. \subsection{Safety}
  370. This is a pessimistic static analysis.
  371. \end{document}