Commit d48a9533 authored by Giuseppe Castagna's avatar Giuseppe Castagna
Browse files

Patched fixpoint combinator

parent 0af273e2
......@@ -148,7 +148,7 @@ $(\Int\to\textsf{Empty})\land(\neg\Int\to{}2)$\newline
atom null
type Object = Null | { prototype = Object ..}
type ObjectWithPropertyL = { l = Any ..}
| { prototype = ObjectWithPropertyL ..}
| { prototype = ObjectWithPropertyL ..}
let has_property_l = fun (o:Object) ->
if o is ObjectWithPropertyL then true else false
......@@ -158,7 +158,7 @@ let has_own_property_l = fun (o:Object) ->
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 if o is Null then null
else self (o.prototype)
\end{lstlisting} &\vfill\medskip\smallskip
$(\Keyw{ObjectWithPropertyL}\to\True)$ $\land$
......
\noindent In our language, a fixpoint combinator can be defined as follows
\begin{alltt}\color{darkblue}
type X = X -> \textit{S} -> \textit{T}
let z = fun (((\textit{S} -> \textit{T}) -> \textit{S} -> \textit{T} ) -> (\textit{S} -> \textit{T})) f ->
let delta = fun ( X -> (\textit{S} -> \textit{T}) ) x ->
f ( fun (\textit{S} -> \textit{T}) v -> ( x x v ))
in delta delta
\end{alltt}
which applied to any function \code{f:(\textit{S}$\to$\textit{T})$\to$\textit{S}$\to$\textit{T}}
returns a function \code{(z\,f):\textit{S}$\to$\textit{T}} such that
for every non
diverging expression \code{$e$} of type \code{\textit{S}}, the
expression \code{(z\,f)$e$} (which is of type \code{\textit{T}}) reduces to \code{f((z\,f)$e$)}.
It is then clear that definition of \code{get\_property\_l} in Code 11
of Table~\ref{tab:implem}, is nothing but syntactic sugar for
\begin{alltt}\color{darkblue}
let get_property_l =
let aux = fun (self:Object->Any) -> fun (o:Object)->
if has_own_property_l o is True then o.l
else if o is Null then null
else self (o.prototype)
in z aux
\end{alltt}
where \code{\textit{S}} is \code{Object} and \code{\textit{T}} is \code{Any}.
......@@ -261,6 +261,10 @@ The authors thank Paul-André Melliès for his help on type ranking.
\newpage
\section{Fix-point combinator}
\label{sec:fixpoint}
\input{fixpoint}
\iflongversion%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
......
......@@ -187,26 +187,30 @@ This is precisely reflected by the case $\Int\to\Empty$ in the result.
Indeed our {\tt example10} function can be applied to an integer, but
at runtime the application of {\tt f\,x} will diverge.
Code~11 simulates the behaviour of JavaScript property resolution, by looking
Code~11 simulates the behavior of JavaScript property resolution, by looking
for a property \texttt{l} either in the object \texttt{o} itself or in the
chained list of its \texttt{prototype} objects. In that example, we first model
prototype chaining by defining a type \texttt{Object} that can be either the
prototype-chaining by defining a type \texttt{Object} that can be either the
atom \texttt{Null} or any record with a \texttt{prototype} field which contains
(recursively) an \texttt{Object}. To ease the reading, we defined a recursive
type \texttt{ObjectWithPropertyL} which is either a record with a field
\texttt{l} or a record with a prototype of type \texttt{ObjectWithPropertyL}. We
can then define two predicate function \texttt{has\_property\_l} and
\texttt{has\_own\_property\_l} that tests whether an object has a property
through its prototype or directly. Lastly we can define a function
can then define two predicate functions \texttt{has\_property\_l} and
\texttt{has\_own\_property\_l} that test whether an object has a property
through its prototype or directly. Lastly, we can define a function
\texttt{get\_property\_l} which directly access the field if it is present, or
recursively search for it through the prototype chain (in our syntax, the
paremeter \texttt{self} allows one to refer to the function itself). Of
recursively search for it through the prototype chain, where in our syntax, the
explicitly-typed parameter \texttt{self} allows one to refer to the function itself. Of
particular interest is the type deduced for the two predicate functions. Indeed,
we can see that \texttt{has\_own\_property\_l} is given an overloaded type whose
first argument is in each case a recursive record type that describe precisely
whether \texttt{l} is present at some point in the list or not (recall that
in a record type such as $\orecord{ l=?\Empty }$, indicate that field \texttt{l}
is absent for sure).
is absent for sure). As an aside notice that even if our language does
not explicitly include recursive expression, it is quite easy to
define a well-typed fix-point combinator (for the reader convinience
we show its definition both in \Appendix\ref{sec:fixpoint} and in our
on-line prototype).
Finally, Code~12 implements the typical type-switching pattern used in
JavaScript. While languages such as Scheme and Racket provides specific
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment