code_table.tex 6.26 KB
Newer Older
Giuseppe Castagna's avatar
Giuseppe Castagna committed
1
\lstset{language=[Objective]Caml,columns=fixed,basicstyle=\linespread{0.43}\ttfamily\scriptsize,aboveskip=-0.5em,belowskip=-1em,xleftmargin=-0.5em}
Kim Nguyễn's avatar
Kim Nguyễn committed
2
\begin{table}
3
   {\scriptsize
4
  \begin{tabular}{|@{\,}c@{\,}|p{0.54\textwidth}@{}|@{\,}p{0.41\textwidth}@{\,}|}
Kim Nguyễn's avatar
Kim Nguyễn committed
5
\hline
6
    & Code & Inferred type\\
7
%    \hline
Kim Nguyễn's avatar
Kim Nguyễn committed
8
9
10
11
12
    \hline
    1 &
          \begin{lstlisting}
let basic_inf = fun (y : Int | Bool) ->
  if y is Int then incr y else lnot y\end{lstlisting}
Giuseppe Castagna's avatar
Giuseppe Castagna committed
13
         &\vfill
Kim Nguyễn's avatar
Kim Nguyễn committed
14
15
16
17
18
19
20
           $(\Int\to\Int)\land(\Bool\to\Bool)$
    \\\hline
    2 &
\begin{lstlisting}
let any_inf = fun (x : Any) ->
  if x is Int then incr x else
  if x is Bool then lnot x else x
Giuseppe Castagna's avatar
Giuseppe Castagna committed
21
\end{lstlisting} &\vfill
Giuseppe Castagna's avatar
Giuseppe Castagna committed
22
 $(\Int\to\Int)\land(\lnot\Int\to\lnot\Int)\;\land$\newline
Kim Nguyễn's avatar
Kim Nguyễn committed
23
24
25
26
27
28
29
30
31
32
33
34
35
 $(\Bool\to\Bool)\land(\lnot(\Int\vee\Bool)\to\lnot(\Int\vee\Bool))$\\
    \hline

    3 &\begin{lstlisting}
let is_int = fun (x : Any) ->
 if x is Int then true else false

let is_bool = fun (x : Any) ->
 if x is Bool then true else false

let is_char = fun (x : Any) ->
 if x is Char then true else false
\end{lstlisting}
36
37
38
39
40
41
&\smallskip
  $(\Int\to\Keyw{True})\land(\lnot\Int\to\Keyw{False})$\newline
  ~\newline\smallskip
  $(\Bool\to\Keyw{True})\land(\lnot\Bool\to\Keyw{False})$\newline
  ~\newline
  $(\Char\to\Keyw{True})\land(\lnot\Char\to\Keyw{False})$
Kim Nguyễn's avatar
Kim Nguyễn committed
42
43
44
45
46
\\\hline
    4 &
          \begin{lstlisting}
let not_ = fun (x : Any) ->
   if x is True then false else true
Giuseppe Castagna's avatar
Giuseppe Castagna committed
47
48
 \end{lstlisting} &\vfill
 $(\Keyw{True}\to\Keyw{False})\land(\lnot\Keyw{True}\to\Keyw{True})$\\\hline
Kim Nguyễn's avatar
Kim Nguyễn committed
49
50
    5 &
          \begin{lstlisting}
51
52
53
let or_ = fun (x : Any) -> fun (y: Any) ->
 if x is True then true
 else if y is True then true else false
Kim Nguyễn's avatar
Kim Nguyễn committed
54
\end{lstlisting}
Giuseppe Castagna's avatar
Giuseppe Castagna committed
55
 &\smallskip\vfill
Giuseppe Castagna's avatar
Giuseppe Castagna committed
56
   $(\True\to\textsf{Any}\to\True)\land(\textsf{Any}\to\True\to\True)\;\land$\newline
Kim Nguyễn's avatar
Kim Nguyễn committed
57
58
59
60
61
62
63
64
   $(\lnot\True\to\lnot\True\to\False)$
\\\hline
    6 &
          \begin{lstlisting}
let and_ = fun (x : Any) -> fun (y : Any) ->
  if not_ (or_ (not_ x) (not_ y)) is True
  then true else false
\end{lstlisting}
Giuseppe Castagna's avatar
Giuseppe Castagna committed
65
 &\vfill
Kim Nguyễn's avatar
Kim Nguyễn committed
66
   $(\True\to((\lnot\True\to\False)\land(\True\to\True))$\newline
Giuseppe Castagna's avatar
Giuseppe Castagna committed
67
   $ \land(\lnot\True\to\textsf{Any}\to\False)$
Kim Nguyễn's avatar
Kim Nguyễn committed
68
69
70
71
72
73
74
75
76
77
78
\\\hline
    7 &
\begin{lstlisting}
let f = fun (x : Any) -> fun (y : Any) ->
  if and_ (is_int x) (is_bool y) is True
  then 1 else
     if or_ (is_char x) (is_int y) is True
     then 2 else 3
\end{lstlisting}&
$(\Int \to (\Int \to 2) \land (\lnot\Int \to 1 \lor 3) \land (\Bool \to 1) \land$\newline
 \hspace*{1cm}$(\lnot(\Bool \lor \Int) \to 3) \land
79
          (\lnot\Bool \to 2\lor3))$~~$\land$\newline
Kim Nguyễn's avatar
Kim Nguyễn committed
80
81
$(\Char \to (\Int \to 2) \land (\lnot\Int \to 2) \land (\Bool \to 2) \land$\newline
\hspace*{1cm}$(\lnot(\Bool \lor \Int) \to 2) \land
82
          (\lnot\Bool \to 2))$~~$\land$\newline
Kim Nguyễn's avatar
Kim Nguyễn committed
83
84
85
86
$(\lnot(\Int \lor \Char) \to (\Int \to 2) \land
          (\lnot\Int \to 3) \land$\newline
\hspace*{0.2cm}$(\Bool \to 3) \land(\lnot(\Bool \lor \Int) \to 3)
          \land (\lnot\Bool \to 2
87
88
                  \lor 3))$\newline
                  $\land \ldots$ (two other redundant cases omitted)
Kim Nguyễn's avatar
Kim Nguyễn committed
89
90
91
92
93
94
95
96
%                   \newline
% $(\lnot\Char \to (\Int \to 2) \land (\lnot\Int \to 1 \lor 3) \land (\Bool \to 1 \lor 3) \land$\newline
% \hspace*{1cm}$(\lnot(\Bool \lor \Int) \to 3) \land
%           (\lnot\Bool \to 2\lor 3)) \land$\newline
%           $(\lnot\Int \to (\Int \to 2) \land (\lnot\Int \to 2
%           \lor 3) \land (\Bool \to 2\lor 3) \land$\newline
%           \hspace*{1cm}$(\lnot(\Bool \lor \Int) \to 2
%           \lor 3) \land (\lnot\Bool \to 2\lor 3))$
97
\\
Kim Nguyễn's avatar
Kim Nguyễn committed
98
99
100
101
102
& \begin{lstlisting}
let test_1 = f 3 true
let test_2 = f (42,42) 42
let test_3 = f nil nil
\end{lstlisting}&
Giuseppe Castagna's avatar
Giuseppe Castagna committed
103
104
105
                  $\p 1$\newline
                  $\p 2$\newline
                  $\p 3$
106
107
108
    \\\hline
    8 &
\begin{lstlisting}
Mickael Laurent's avatar
Mickael Laurent committed
109
atom nil
110
type Document = { nodeType=9 ..}
111
112
and Element = { nodeType=1, childNodes=NodeList ..}
and Text = { nodeType=3,
Mickael Laurent's avatar
Mickael Laurent committed
113
              isElementContentWhiteSpace=Bool ..}
114
115
and Node = Document | Element | Text
and NodeList = Nil | (Node, NodeList)
116
117
118

let is_empty_node = fun (x : Node) ->
  if x.nodeType is 9 then false
Mickael Laurent's avatar
Mickael Laurent committed
119
  else if x is { nodeType=3 ..} then
120
    x.isElementContentWhiteSpace
Mickael Laurent's avatar
Mickael Laurent committed
121
  else if x.childNodes is Nil then true else false
122
\end{lstlisting} &\vspace{12mm}
Giuseppe Castagna's avatar
Giuseppe Castagna committed
123
124
$(\Keyw{Document}\to\False)~\land$\newline
$(\orecord{ \texttt{nodeType}\,{=}\,1, \texttt{childNodes}\,{=}\,\Keyw{Nil} }\to\True) ~\land$\newline
125
126
127
128
$(\orecord{ \texttt{nodeType}\,{=}\,1,
                   \texttt{childNodes}\,{=}\,(\Keyw{Node},\Keyw{NodeList})
                   }$\newline
                   \hspace*{4.5cm}$\to\False) ~\land$\newline
Giuseppe Castagna's avatar
Giuseppe Castagna committed
129
$(\Keyw{Text}\to\Bool)~\land$\newline (omitted redundant arrows)
130
131
132
133
134
    \\\hline
    9 & \begin{lstlisting}
let xor_ = fun (x : Any) -> fun (y : Any) ->
 if and_ (or_ x y) (not_ (and_ x y)) is True
 then true else false
Giuseppe Castagna's avatar
Giuseppe Castagna committed
135
\end{lstlisting} &\vfill
136
137
138
$\True\to((\True\to\False)\land(\lnot\True\to\True))~\land$\newline
$(\lnot\True\to((\True\to\True) \land (\lnot\True\to\False))$
    \\\hline
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
139
 10 & \begin{lstlisting}
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
140
(* f, g have type: (Int->Int) & (Any->Bool) *)
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
141

Kim Nguyễn's avatar
.    
Kim Nguyễn committed
142
let example10 = fun (x : Any) ->
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
143
  if (f x, g x) is (Int, Bool) then 1 else 2
Giuseppe Castagna's avatar
Giuseppe Castagna committed
144
\end{lstlisting} &\vfill\medskip\smallskip
Giuseppe Castagna's avatar
empty    
Giuseppe Castagna committed
145
$(\Int\to\textsf{Empty})\land(\neg\Int\to{}2)$\newline
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
146
\texttt{Warning: line 4, 39-40: unreachable expression}
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
147
    \\\hline
Mickael Laurent's avatar
Mickael Laurent committed
148
  11 & \begin{lstlisting}
149
150
atom null
type Object = Null | { prototype = Object ..}
Mickael Laurent's avatar
Mickael Laurent committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
type ObjectWithPropertyL = { l = Any ..}
  | { prototype = ObjectWithPropertyL ..}

let has_property_l = fun (o:Object) ->
    if o is ObjectWithPropertyL then true else false

let has_own_property_l = fun (o:Object) ->
    if o is { l=Any ..} then true else false

let get_property_l = fun (self:Object->Any) o ->
    if has_own_property_l o is True then o.l
    else if o is Nil then nil
    else self (o.prototype)
\end{lstlisting} &\vfill\medskip\smallskip
$(\Keyw{ObjectWithPropertyL}\to\True)$ $\land$
$(\Keyw{X1}\to\False) \texttt{ where}$\newline
$\Keyw{X1}\,=\,(\Keyw{Nil}\,|\,\orecord{\texttt{l}\,{=}\,?\Keyw{Empty},\, \texttt{prototype}\,{=}\,\Keyw{X1}})$\newline
~\newline
$(\orecord{\Keyw{l}\,=\,\Keyw{Any}, \texttt{prototype}\,=\,\Keyw{Object}}\to\True)$ $\land$\newline
$((\Keyw{Nil}\,|\,\orecord{\texttt{l}\,=\,?\Keyw{Empty},\, \texttt{prototype}\,=\,\Keyw{Object}})\to\False)$\newline
\newline
~\newline
$\Keyw{Object}\to\Keyw{Any}$
    \\\hline
Kim Nguyễn's avatar
Kim Nguyễn committed
175
176
  \end{tabular}
}
177
178
\caption{Types inferred by the implementation}
\ifsubmission%
Kim Nguyễn's avatar
Kim Nguyễn committed
179
\svvspace{-10mm}
180
\fi%
Kim Nguyễn's avatar
Kim Nguyễn committed
181
\label{tab:implem}
Giuseppe Castagna's avatar
Giuseppe Castagna committed
182
\end{table}