|
|
1.1 ! root 1: \chapter{Derived forms} ! 2: \label{derived} ! 3: ML is equipped with a number of {\em derived forms}, which in no way ! 4: add to the power of the language, as each is expressible in terms of ! 5: the more primitive constructs. ! 6: ! 7: The $n$-tuple type ! 8: \verb"("$ty_1$\verb"*"$ty_2$\verb"*"~$\cdots$~\verb"*"$ty_n$\verb")" ! 9: for $n\geq 2$ is an abbreviation for the record type with numeric labels ! 10: \verb"{1:"$ty_1$\verb",2:"$ty_2$\verb","~$\cdots$~\verb","$n$\verb":"$ty_n$\verb"}". ! 11: Similarly the $n$-tuple expression ! 12: \verb"("$exp_1$\verb","$exp_2$\verb","~$\cdots$~\verb","$exp_n$\verb")" ! 13: is an abbreviation for the record expression ! 14: verb"{1="$exp_1$\verb",2="$exp_2$\verb","~$\cdots$~\verb","$n$\verb"="$exp_n$\verb"}", ! 15: and the $n$-tuple pattern ! 16: \verb"("$pat_1$\verb","$pat_2$\verb","~$\cdots$~\verb","$pat_n$\verb")" ! 17: is an abbrevation for the record pattern ! 18: verb"{1="$pat_1$\verb",2="$pat_2$\verb","~$\cdots$~\verb","$n$\verb"="$pat_n$\verb"}". ! 19: ! 20: The ``empty record'' \verb"{}" can also be written as \verb"()". This ! 21: value is conventionally returned from functions that have side-effects ! 22: but don't return an interesting value. The type of empty records ! 23: is named \verb"unit" (because the type only contains one value). ! 24: ! 25: The expression \verb"#"{\it lab} for any label (symbolic or numeric) ! 26: is a selector function that extracts the named field from a record. ! 27: ! 28: The \verb"case"~{\it exp}~\verb"of"~{\it match} expression is completely ! 29: equivalent to a \verb"(fn"~{\it match}\verb")" expression applied to ! 30: the {\it exp}. ! 31: ! 32: The \verb"if"-\verb"then"-\verb"else" expression has conventional semantics: ! 33: it evaluates a boolean condition, then evaluates either the \verb"then" ! 34: expression or the \verb"else" expression; this behavior can be defined ! 35: in terms of a \verb"case" with patterns \verb"true" and \verb"false". ! 36: ! 37: The \verb"orelse" and \verb"andalso" boolean operators evaluate their ! 38: right-hand expressions only if the left-hand expressions are insufficient ! 39: to determine the answer (i.e. \verb"false" for \verb"orelse" and \verb"true" ! 40: for \verb"andalso"). Their syntactic precedence is weaker than all other ! 41: infix operators, and \verb"orelse" binds weaker than \verb"andalso". ! 42: ! 43: Several expressions may be separated by semicolons, and the whole enclosed ! 44: in parentheses; all the expressions will be evaluated and the result ! 45: of the whole will is the result of the last expression. When such an ! 46: {\it expression sequence} is the entire body of a \verb"let" expression, ! 47: the parentheses may be omitted. ! 48: ! 49: The expression \verb"while"~$exp_1$~\verb"do"~$exp_2$ repeatedly evaluates ! 50: $exp_1$ followed by $exp_2$ until $exp_1$ evaluates to \verb"false" ! 51: (just as in Pascal); the \verb"while" expression can be expressed ! 52: in terms of a recursive function. ! 53: ! 54: The expression ! 55: \verb"["$exp_1$\verb","$exp_2$\verb","$\cdots$\verb","$exp_n$\verb"]" is ! 56: an abbreviation for the list ! 57: $exp_1$\verb"::"$exp_2$\verb"::"$\cdots$\verb"::"$exp_n$\verb"::nil", ! 58: and similarly the pattern ! 59: \verb"["$pat_1$\verb","$pat_2$\verb","$\cdots$\verb","$pat_n$\verb"]" is ! 60: an abbreviation for the list pattern ! 61: $pat_1$\verb"::"$pat_2$\verb"::"$\cdots$\verb"::"$pat_n$\verb"::nil". ! 62: In both patterns and expressions, \verb"[]" is an abbrevation for \verb"nil". ! 63: ! 64: In a record pattern (but not in a record expression), an element ! 65: $id$\verb"="$id$, where the pattern is a variable with the same identifier ! 66: as the label, can be abbreviated as just $id$. Similarly, ! 67: $id$\verb"=~"$id$~verb"as"~{\it pat} can be abbreviated ! 68: as "$id$~verb"as"~{\it pat}, ! 69: $id$\verb"=~"$id$~verb":"~{\it ty} can be abbreviated ! 70: "$id$~verb":"~{\it ty}. ! 71: ! 72: Recursive clausal function definitions can be defined conviently with ! 73: the \verb"fun" keyword. The declaration ! 74: \begin{verbatim} ! 75: fun f pat1a pat2a pat3a = exp1 ! 76: | f pat1b pat2b pat3b = exp2 ! 77: | f pat1c pat2c pat3c = exp2 ! 78: \end{verbatim} ! 79: is an abbreviation for the curried function ! 80: \begin{verbatim} ! 81: val rec f = fn pat1 => fn pat2 => fn pat3 => ! 82: case (pat1,pat2,pat3) ! 83: of (pat1a,pat2a,pat3a) => exp1 ! 84: | (pat1b,pat2b,pat3b) => exp1 ! 85: | (pat1c,pat2c,pat3c) => exp1 ! 86: \end{verbatim} ! 87: The patterns must all be atomic patterns (including, of course, ! 88: arbitrary patterns enclosed in parentheses) to avoid syntactic ambiguity. ! 89: ! 90: A special form of \verb"fun" declaration is permitted for infixed ! 91: function identifiers, of which an example is shown here: ! 92: \begin{verbatim} ! 93: fun a + b = b - ~a ! 94: \end{verbatim} ! 95: ! 96: The derived forms are summarized in the tables below. ! 97: ! 98: \section{Expressions and patterns} ! 99: \begin{tabular}{@{}l l} ! 100: {\bf Derived Form}&{\bf Equivalent Form} \\ \hline ! 101: \multicolumn{2}{l}{\bf Types:} \\ ! 102: ${\rm ty}_1$ \verb"*" \rep{2} \verb"*" ${\rm ty}_n$ & ! 103: \verb"{" 1 : ${\rm ty}_1$ , \rep{2} , $n$ : ${\rm ty}_n$ \verb"}" \\ \hline ! 104: \multicolumn{2}{l}{\bf Expressions:} \\ ! 105: \verb"()" & \verb"{ }"\\ \xskip ! 106: \verb"(" ${\rm exp}_1$ \verb"," \rep{2} \verb"," ${\rm exp}_n$ \verb")" & ! 107: \verb"{" 1 \verb"=" ${\rm exp}_1$ , \rep{2} , ! 108: $n$ \verb"=" ${\rm exp}_n$ \verb"}" \\ \xskip ! 109: \verb"case" exp \verb"of" match & ( \verb"fn" match ) ( exp ) \\ \xskip ! 110: \verb"#" lab & \verb"fn {" lab \verb"= x , ...} => x" \\ \xskip ! 111: \verb"if" exp \verb"then" ${\rm exp}_1$ \verb"else" ${\rm exp}_2$ & ! 112: \verb"case" exp \verb"of true =>" ${\rm exp}_1$ \\ ! 113: & \ \ \ \ \ \ \ \ \ \ \verb"| false =>" ${\rm exp}_2$ \\ \xskip ! 114: ${\rm exp}_1$ \verb"orelse" ${\rm exp}_2$ & ! 115: \verb"if" ${\rm exp}_1$ \verb"then true else" ${\rm exp}_2$ \\ \xskip ! 116: ${\rm exp}_1$ \verb"andalso" ${\rm exp}_2$ & ! 117: \verb"if" ${\rm exp}_1$ \verb"then" ${\rm exp}_2$ \verb"else false" \\ \xskip ! 118: ( ${\rm exp}_1$ ; \rep{1} ; ${\rm exp}_n$ ; exp) & ! 119: \verb"case" ${\rm exp}_1$ \verb"of _ =>" \underline{\ \ \ } \verb"=>" \\ ! 120: & \ \ \ \verb"case" ${\rm exp}_n$ \verb"of _ =>" exp \\ \xskip ! 121: \verb"let" dec \verb"in" ${\rm exp}_1$ ; \rep{1} ; ${\rm exp}_n$ \verb"end" ! 122: & ! 123: \verb"let" dec \verb"in" ( ${\rm exp}_1$ ; \rep{1} ; ${\rm exp}_n$) \verb"end" ! 124: \\ \xskip ! 125: \verb"while" ${\rm exp}_1$ \verb"do" ${\rm exp}_2$ & ! 126: \parbox[t]{2.5in}{\begin{raggedright} ! 127: \verb"let val rec f = fn () =>" \\ ! 128: \ \ \verb"if" ${\rm exp}_1$ \verb"then (" ${\rm exp}_2$ \verb"; f()) else ()" ! 129: \\ ! 130: \verb" in f() end" ! 131: \end{raggedright}} \\ \xskip ! 132: \verb"[" ${\rm exp}_1$ , \rep{0} , ${\rm exp}_n$ \verb"]" & ! 133: ${\rm exp}_1$ \verb"::" \rep{0} \verb"::" ${\rm exp}_n$ \verb":: nil" \\ ! 134: \hline ! 135: \pagebreak[1] ! 136: {\bf Derived Form}&{\bf Equivalent Form} \\ \hline ! 137: \multicolumn{2}{l}{\bf Patterns:} \\ ! 138: \verb"()" & \verb"{ }" {\it (no space between ``\/\verb"()"'')} \\ \xskip ! 139: \verb"(" ${\rm pat}_1$ \verb"," \rep{2} \verb"," ${\rm pat}_n$ \verb")" & ! 140: \verb"{" 1 \verb"=" ${\rm pat}_1$ , \rep{2} , ! 141: $n$ \verb"=" ${\rm pat}_n$ \verb"}" \\ \xskip ! 142: ! 143: \verb"[" ${\rm pat}_1$ , \rep{0} , ${\rm pat}_n$ \verb"]" & ! 144: ${\rm pat}_1$ \verb"::" \rep{0} \verb"::" ${\rm pat}_n$ \verb":: nil" \\ \xskip ! 145: \verb"{" \underline{\ \ \ } , id , \underline{\ \ \ } \verb"}" & ! 146: \verb"{" \underline{\ \ \ } , id \verb"=" id , \underline{\ \ \ } \verb"}" \\ \xskip ! 147: \verb"{" \underline{\ \ \ } , id \verb"as" pat, \underline{\ \ \ } \verb"}" & ! 148: \verb"{" \underline{\ \ \ } , id \verb"=" id \verb"as" pat, \underline{\ \ \ } \verb"}" \\ \xskip ! 149: \verb"{" \underline{\ \ \ } , id : ty , \underline{\ \ \ } \verb"}" & ! 150: \verb"{" \underline{\ \ \ } , id \verb"=" id : ty , \underline{\ \ \ } \verb"}" \\ ! 151: \hline ! 152: \end{tabular} ! 153: ! 154: Each derived form is identical semantically to its ``equivalent ! 155: form.'' The type-checking of each derived form is also defined by ! 156: that of its equivalent form. The precedence among all primitive and ! 157: derived forms is shown in Appendix~\ref{grammar}. ! 158: ! 159: The derived type ${\rm ty}_1$ \verb"*" \rep{2} \verb"*" ${\rm ty}_n$ ! 160: is called an (n--)tuple type, and the values of this type are called ! 161: (n--)tuples. ! 162: ! 163: The final derived pattern allows a label and its associated value to ! 164: be elided in a record pattern, when they are the same identifier. ! 165: ! 166: \section{Bindings and declarations} ! 167: ! 168: A syntax class {\bf fb} of function bindings is used as a convient ! 169: form of value binding for (possibly recursive) function declarations. ! 170: The equivalent form of each function binding is an ordinary value ! 171: binding. These new function bindings must be declared by \verb"fun", ! 172: not by \verb"val"; however, functions may still be declared using ! 173: \verb"val" or \verb"val rec" along with \verb"fn" expressions. ! 174: ! 175: \begin{tabular}{@{}l l} ! 176: \multicolumn{1}{c}{\bf Derived Form}& ! 177: \multicolumn{1}{c}{\bf Equivalent Form} \\ \hline ! 178: \multicolumn{2}{l}{\bf Function bindings {\rm fb}:} \\ ! 179: & id = \verb"fn" $x_1$ \verb"=>" \rep{1} \verb"=> fn" $x_n$ \verb"=>" \\ ! 180: & \ \ \verb"case (" $x_1,$ \underline{\ \ \ } $, x_n$ \verb")" \\ ! 181: \ id ${\rm apat}_{11}$ \rep{1} ${\rm apat}_{1n}$ cst = ${\rm exp}_1$ & ! 182: \ \ \verb"of" ( ${\rm apat}_{11}$ , \rep{1} , ${\rm apat}_{1n}$ \verb"=>" ${\rm exp}_1$ cst \\ ! 183: \verb"|" \underline{\ \ \ } & \ \ \verb"|" \underline{\ \ \ } \\ ! 184: \verb"|" id ${\rm apat}_{m1}$ \rep{1} ${\rm apat}_{mn}$ cst = ${\rm exp}_m$ & ! 185: \ \ \ \verb"|" ( ${\rm apat}_{m1}$ , \rep{1} , ${\rm apat}_{mn}$ \verb"=>" ${\rm exp}_m$ cst \\ ! 186: ! 187: & \\ ! 188: ${\rm fb}_1$ and \rep{1} and ${\rm fb}_n$ & ! 189: ${\rm vb}_1$ and \rep{1} and ${\rm vb}_n$ \\ ! 190: &{\it (where ${\rm vb}_i$ is the equivalent of\/ ${\rm fb}_i$) } \\ ! 191: \hline ! 192: \multicolumn{2}{l}{\bf Declarations:} \\ ! 193: \verb"fun" fb & \verb"val rec" vb \\ ! 194: &{\it (where \/{\rm vb} is the equivalent of\/ {\rm fb}) } \\ ! 195: & \\ ! 196: exp & \verb"val it =" exp {\it (only at top level)}\\ ! 197: \hline ! 198: \end{tabular} ! 199: In the table above, ``cst'' stands for an optional type constraint---a colon ! 200: followed by a type expression. ! 201: The last derived declaration (using ``it'') is only allowed at ! 202: top-level, for treating top-level expressions as degenerate ! 203: declarations; ``it'' is just a normal value variable.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.