Annotation of researchv10no/cmd/sml/doc/refman/reference.tex, revision 1.1.1.1

1.1       root        1: \chapter{Reference values}
                      2: \label{reference}
                      3: References are cells whose contents may be changed after creation by
                      4: assignment.  The \verb"ref" ``datatype'' constructor, and its
                      5: corresponding value constructor, are almost as if defined by the declaration
                      6: \begin{verbatim}
                      7: datatype 'a ref = ref of 'a
                      8: \end{verbatim}
                      9: Thus, a reference whose initial contents are the string \verb|"abc"|
                     10: may be created by \verb|val r = ref "abc"|.  Subsequently, the
                     11: contents of \verb"r" may be altered by assignment: \verb|r := "def"|.
                     12: The contents of a reference may be examined by using the \verb"ref"
                     13: constructor in a pattern:
                     14: \begin{verbatim}
                     15: let val (ref s) = r
                     16:  in print s
                     17: end
                     18: \end{verbatim}
                     19: The function \verb"!" is defined to take the contents of a reference;
                     20: that is,
                     21: \begin{verbatim}
                     22: fun ! (ref x) = x
                     23: \end{verbatim}
                     24: 
                     25: References are not fully polymorphic; see Chapter~\ref{reftype}.
                     26: 
                     27: Formally, we say that phrases in ML are evaluated in the presence of
                     28: an {\em environment} $E$ and a {\em store} $S$.  The effect on $E$ of
                     29: evaluating declarations, expressions, etc.  is described in
                     30: Chapter~\ref{eval}.  Here we summarize the effect on $S$.
                     31: 
                     32: The store $S$ maps reference values to their contents.  Evaluation of
                     33: an expression in the store $S$ yields, depending on the form of the
                     34: expression,
                     35: \begin{description}
                     36: \item[\verb"ref" exp\hfill] exp is evaluated in $S$,
                     37: producing a value $v$ and a store $S'$; the 
                     38: reference value $r$ is returned with the store $S'+\{r \mapsto v\}$.
                     39: 
                     40: \item[${\rm exp}_1$~${\rm exp}_2$\hfill]  ${\rm exp}_1$ is evaluated in $S$
                     41: yielding the function $f_1$ and store $S'$;
                     42: ${\rm exp}_2$ is evaluated in $S'$ yielding $v_2$ and $S''$;
                     43: finally the body of $f_1$ is evaluated with its variable bound to $v_2$,
                     44: in the store $S''$, yielding the result $v$ and the store $S'''$.
                     45: 
                     46: \item[\verb"op := "$({\rm exp}_1,{\rm exp}_2)$\hfill]  The expression 
                     47: $({\rm exp}_1,{\rm exp}_2)$ is evaluated in $S$, yielding the pair
                     48: $(r,v)$ and the store $S'$; then the unit value \{\} is returned with
                     49: the store $S'+\{r \mapsto v\}$.
                     50: 
                     51: \item[\protect\verb"\{" ${\rm lab}_1$ \protect\verb"=" ${\rm exp}_1$ , \underline{\ \ \ } , ${\rm lab}_n$ \protect\verb"=" ${\rm exp}_n$ \protect\verb"\}" \hfill]  
                     52: ${\rm exp}_1$ is evaluated in $S$, yielding $v_1$ and the store
                     53: $S_1$; then each ${\rm exp}_i$ is evaluated in $S_{i-1}$, yielding $v_i$ and the store $S_i$; then the record
                     54: $\{ {\rm lab}_1 = v_1 , ... , {\rm lab}_n = v_n \}$ is returned with
                     55: the store $S_n$.  Note that the expressions are evaluated in the
                     56: sequence they are written, not in alphabetical order of the labels.
                     57: 
                     58: \item[\protect\verb"raise" exp\hfill]  exp is evaluated in $S$, returning $v$
                     59: and $S'$; then the exception-packet $(v,S')$ is raised.
                     60: 
                     61: \item[exp \verb"handle" match\hfill]  exp is evaluated; if exp returns a
                     62: value $v$ with state $S'$, then $v$ is returned with $S'$.  
                     63: If exp raises an exception-packet $(e,S'')$ then
                     64: the match is applied to $e$ in the state $S''$.
                     65: If the match fails, then $(e,S'')$ is raised
                     66: (as the value of the \verb"handle" expression).  If the match
                     67: succeeds, then the resulting value is returned.
                     68: \end{description}
                     69: 
                     70: Matching a pattern to a value has no effect on the store.  Evaluating
                     71: a value binding has an effect on the store just from the evaluation of
                     72: the constituent expressions.  Evaluation of type, datatype, or exception
                     73: bindings has no effect on the store.
                     74: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.