comparison doc/manual.tex @ 529:4df69124e9c5

Shorthands
author Adam Chlipala <adamc@hcoop.net>
date Thu, 27 Nov 2008 16:55:30 -0500
parents 9e2abd85529b
children c2f9f94ea709
comparison
equal deleted inserted replaced
528:9e2abd85529b 529:4df69124e9c5
13 \title{The Ur/Web Manual} 13 \title{The Ur/Web Manual}
14 \author{Adam Chlipala} 14 \author{Adam Chlipala}
15 15
16 \maketitle 16 \maketitle
17 17
18 \section{Syntax} 18 \section{Ur Syntax}
19
20 In this section, we describe the syntax of Ur, deferring to a later section discussion of most of the syntax specific to SQL and XML. The sole exceptions are the declaration forms for tables, sequences, and cookies.
19 21
20 \subsection{Lexical Conventions} 22 \subsection{Lexical Conventions}
21 23
22 We give the Ur language definition in \LaTeX $\;$ math mode, since that is prettier than monospaced ASCII. The corresponding ASCII syntax can be read off directly. Here is the key for mapping math symbols to ASCII character sequences. 24 We give the Ur language definition in \LaTeX $\;$ math mode, since that is prettier than monospaced ASCII. The corresponding ASCII syntax can be read off directly. Here is the key for mapping math symbols to ASCII character sequences.
23 25
26 \textbf{\LaTeX} & \textbf{ASCII} \\ 28 \textbf{\LaTeX} & \textbf{ASCII} \\
27 $\to$ & \cd{->} \\ 29 $\to$ & \cd{->} \\
28 $\times$ & \cd{*} \\ 30 $\times$ & \cd{*} \\
29 $\lambda$ & \cd{fn} \\ 31 $\lambda$ & \cd{fn} \\
30 $\Rightarrow$ & \cd{=>} \\ 32 $\Rightarrow$ & \cd{=>} \\
31 & \cd{---} \\ 33 $\neq$ & \cd{<>} \\
34 $\leq$ & \cd{<=} \\
35 $\geq$ & \cd{>=} \\
32 \\ 36 \\
33 $x$ & Normal textual identifier, not beginning with an uppercase letter \\ 37 $x$ & Normal textual identifier, not beginning with an uppercase letter \\
34 $X$ & Normal textual identifier, beginning with an uppercase letter \\ 38 $X$ & Normal textual identifier, beginning with an uppercase letter \\
35 \end{tabular} 39 \end{tabular}
36 \end{center} 40 \end{center}
49 &&& \mt{Unit} & \textrm{the trivial constructor} \\ 53 &&& \mt{Unit} & \textrm{the trivial constructor} \\
50 &&& \mt{Name} & \textrm{field names} \\ 54 &&& \mt{Name} & \textrm{field names} \\
51 &&& \kappa \to \kappa & \textrm{type-level functions} \\ 55 &&& \kappa \to \kappa & \textrm{type-level functions} \\
52 &&& \{\kappa\} & \textrm{type-level records} \\ 56 &&& \{\kappa\} & \textrm{type-level records} \\
53 &&& (\kappa\times^+) & \textrm{type-level tuples} \\ 57 &&& (\kappa\times^+) & \textrm{type-level tuples} \\
54 &&& \_ & \textrm{wildcard} \\ 58 &&& \_\_ & \textrm{wildcard} \\
55 &&& (\kappa) & \textrm{explicit precedence} \\ 59 &&& (\kappa) & \textrm{explicit precedence} \\
56 \end{array}$$ 60 \end{array}$$
57 61
58 Ur supports several different notions of functions that take types as arguments. These arguments can be either implicit, causing them to be inferred at use sites; or explicit, forcing them to be specified manually at use sites. There is a common explicitness annotation convention applied at the definitions of and in the types of such functions. 62 Ur supports several different notions of functions that take types as arguments. These arguments can be either implicit, causing them to be inferred at use sites; or explicit, forcing them to be specified manually at use sites. There is a common explicitness annotation convention applied at the definitions of and in the types of such functions.
59 $$\begin{array}{rrcll} 63 $$\begin{array}{rrcll}
83 &&& (c^+) & \textrm{type-level tuple} \\ 87 &&& (c^+) & \textrm{type-level tuple} \\
84 &&& c.n & \textrm{type-level tuple projection ($n \in \mathbb N^+$)} \\ 88 &&& c.n & \textrm{type-level tuple projection ($n \in \mathbb N^+$)} \\
85 \\ 89 \\
86 &&& \lambda [c \sim c] \Rightarrow c & \textrm{guarded constructor} \\ 90 &&& \lambda [c \sim c] \Rightarrow c & \textrm{guarded constructor} \\
87 \\ 91 \\
88 &&& \_ & \textrm{wildcard} \\ 92 &&& \_ :: \kappa & \textrm{wildcard} \\
89 &&& (c) & \textrm{explicit precedence} \\ 93 &&& (c) & \textrm{explicit precedence} \\
90 \end{array}$$ 94 \end{array}$$
91 95
92 Modules of the module system are described by \emph{signatures}. 96 Modules of the module system are described by \emph{signatures}.
93 $$\begin{array}{rrcll} 97 $$\begin{array}{rrcll}
94 \textrm{Signatures} & S &::=& \mt{sig} \; s^* \; \mt{end} & \textrm{constant} \\ 98 \textrm{Signatures} & S &::=& \mt{sig} \; s^* \; \mt{end} & \textrm{constant} \\
95 &&& X & \textrm{variable} \\ 99 &&& X & \textrm{variable} \\
96 &&& \mt{functor}(X : S) : S & \textrm{functor} \\ 100 &&& \mt{functor}(X : S) : S & \textrm{functor} \\
97 &&& S \; \mt{where} \; x = c & \textrm{concretizing an abstract constructor} \\ 101 &&& S \; \mt{where} \; \mt{con} \; x = c & \textrm{concretizing an abstract constructor} \\
98 &&& M.X & \textrm{projection from a module} \\ 102 &&& M.X & \textrm{projection from a module} \\
99 \\ 103 \\
100 \textrm{Signature items} & s &::=& \mt{con} \; x :: \kappa & \textrm{abstract constructor} \\ 104 \textrm{Signature items} & s &::=& \mt{con} \; x :: \kappa & \textrm{abstract constructor} \\
101 &&& \mt{con} \; x :: \kappa = c & \textrm{concrete constructor} \\ 105 &&& \mt{con} \; x :: \kappa = c & \textrm{concrete constructor} \\
102 &&& \mt{datatype} \; x \; x^* = dc\mid^+ & \textrm{algebraic datatype definition} \\ 106 &&& \mt{datatype} \; x \; x^* = dc\mid^+ & \textrm{algebraic datatype definition} \\
103 &&& \mt{datatype} \; x = M.x & \textrm{algebraic datatype import} \\ 107 &&& \mt{datatype} \; x = \mt{datatype} \; M.x & \textrm{algebraic datatype import} \\
104 &&& \mt{val} \; x : \tau & \textrm{value} \\ 108 &&& \mt{val} \; x : \tau & \textrm{value} \\
105 &&& \mt{structure} \; X : S & \textrm{sub-module} \\ 109 &&& \mt{structure} \; X : S & \textrm{sub-module} \\
106 &&& \mt{signature} \; X = S & \textrm{sub-signature} \\ 110 &&& \mt{signature} \; X = S & \textrm{sub-signature} \\
107 &&& \mt{include} \; S & \textrm{signature inclusion} \\ 111 &&& \mt{include} \; S & \textrm{signature inclusion} \\
108 &&& \mt{constraint} \; c \sim c & \textrm{record disjointness constraint} \\ 112 &&& \mt{constraint} \; c \sim c & \textrm{record disjointness constraint} \\
122 &&& \hat{X} \; p & \textrm{unary constructor} \\ 126 &&& \hat{X} \; p & \textrm{unary constructor} \\
123 &&& \{(x = p,)^*\} & \textrm{rigid record pattern} \\ 127 &&& \{(x = p,)^*\} & \textrm{rigid record pattern} \\
124 &&& \{(x = p,)^+, \ldots\} & \textrm{flexible record pattern} \\ 128 &&& \{(x = p,)^+, \ldots\} & \textrm{flexible record pattern} \\
125 &&& (p) & \textrm{explicit precedence} \\ 129 &&& (p) & \textrm{explicit precedence} \\
126 \\ 130 \\
127 \textrm{Qualified capitalized variable} & \hat{X} &::=& X & \textrm{not from a module} \\ 131 \textrm{Qualified capitalized variables} & \hat{X} &::=& X & \textrm{not from a module} \\
128 &&& M.X & \textrm{projection from a module} \\ 132 &&& M.X & \textrm{projection from a module} \\
129 \end{array}$$ 133 \end{array}$$
130 134
131 \emph{Expressions} are the main run-time entities, corresponding to both ``expressions'' and ``statements'' in mainstream imperative languages. 135 \emph{Expressions} are the main run-time entities, corresponding to both ``expressions'' and ``statements'' in mainstream imperative languages.
132 $$\begin{array}{rrcll} 136 $$\begin{array}{rrcll}
133 \textrm{Expressions} & e &::=& e : \tau & \textrm{type annotation} \\ 137 \textrm{Expressions} & e &::=& e : \tau & \textrm{type annotation} \\
134 &&& x & \textrm{variable} \\ 138 &&& \hat{x} & \textrm{variable} \\
139 &&& \hat{X} & \textrm{datatype constructor} \\
135 &&& \ell & \textrm{constant} \\ 140 &&& \ell & \textrm{constant} \\
136 \\ 141 \\
137 &&& e \; e & \textrm{function application} \\ 142 &&& e \; e & \textrm{function application} \\
138 &&& \lambda x : \tau \Rightarrow e & \textrm{function abstraction} \\ 143 &&& \lambda x : \tau \Rightarrow e & \textrm{function abstraction} \\
139 &&& e [c] & \textrm{polymorphic function application} \\ 144 &&& e [c] & \textrm{polymorphic function application} \\
155 &&& \_ & \textrm{wildcard} \\ 160 &&& \_ & \textrm{wildcard} \\
156 &&& (e) & \textrm{explicit precedence} \\ 161 &&& (e) & \textrm{explicit precedence} \\
157 \\ 162 \\
158 \textrm{Local declarations} & ed &::=& \cd{val} \; x : \tau = e & \textrm{non-recursive value} \\ 163 \textrm{Local declarations} & ed &::=& \cd{val} \; x : \tau = e & \textrm{non-recursive value} \\
159 &&& \cd{val} \; \cd{rec} \; (x : \tau = e \; \cd{and})^+ & \textrm{mutually-recursive values} \\ 164 &&& \cd{val} \; \cd{rec} \; (x : \tau = e \; \cd{and})^+ & \textrm{mutually-recursive values} \\
165 \\
166 \textrm{Qualified uncapitalized variables} & \hat{x} &::=& x & \textrm{not from a module} \\
167 &&& M.x & \textrm{projection from a module} \\
160 \end{array}$$ 168 \end{array}$$
161 169
162 \emph{Declarations} primarily bring new symbols into context. 170 \emph{Declarations} primarily bring new symbols into context.
163 $$\begin{array}{rrcll} 171 $$\begin{array}{rrcll}
164 \textrm{Declarations} & d &::=& \mt{con} \; x :: \kappa = c & \textrm{constructor synonym} \\ 172 \textrm{Declarations} & d &::=& \mt{con} \; x :: \kappa = c & \textrm{constructor synonym} \\
165 &&& \mt{datatype} \; x \; x^* = dc\mid^+ & \textrm{algebraic datatype definition} \\ 173 &&& \mt{datatype} \; x \; x^* = dc\mid^+ & \textrm{algebraic datatype definition} \\
166 &&& \mt{datatype} \; x = M.x & \textrm{algebraic datatype import} \\ 174 &&& \mt{datatype} \; x = \mt{datatype} \; M.x & \textrm{algebraic datatype import} \\
167 &&& \mt{val} \; x : \tau = e & \textrm{value} \\ 175 &&& \mt{val} \; x : \tau = e & \textrm{value} \\
168 &&& \mt{val} \; \cd{rec} \; (x : \tau = e \; \mt{and})^+ & \textrm{mutually-recursive values} \\ 176 &&& \mt{val} \; \cd{rec} \; (x : \tau = e \; \mt{and})^+ & \textrm{mutually-recursive values} \\
169 &&& \mt{structure} \; X : S = M & \textrm{module definition} \\ 177 &&& \mt{structure} \; X : S = M & \textrm{module definition} \\
170 &&& \mt{signature} \; X = S & \textrm{signature definition} \\ 178 &&& \mt{signature} \; X = S & \textrm{signature definition} \\
171 &&& \mt{open} \; M & \textrm{module inclusion} \\ 179 &&& \mt{open} \; M & \textrm{module inclusion} \\
172 &&& \mt{constraint} \; c \sim c & \textrm{record disjointness constraint} \\ 180 &&& \mt{constraint} \; c \sim c & \textrm{record disjointness constraint} \\
173 &&& \mt{open} \; \mt{constraints} \; M & \textrm{inclusion of just the constraints from a module} \\ 181 &&& \mt{open} \; \mt{constraints} \; M & \textrm{inclusion of just the constraints from a module} \\
174 &&& \mt{table} \; x : c & \textrm{SQL table} \\ 182 &&& \mt{table} \; x : c & \textrm{SQL table} \\
175 &&& \mt{sequence} \; x & \textrm{SQL sequence} \\ 183 &&& \mt{sequence} \; x & \textrm{SQL sequence} \\
176 &&& \mt{class} \; x = c & \textrm{concrete type class} \\ 184 &&& \mt{class} \; x = c & \textrm{concrete type class} \\
177 &&& \mt{cookie} \; x : c & \textrm{HTTP cookie} \\ 185 &&& \mt{cookie} \; x : \tau & \textrm{HTTP cookie} \\
178 \\ 186 \\
179 \textrm{Modules} & M &::=& \mt{struct} \; d^* \; \mt{end} & \mt{constant} \\ 187 \textrm{Modules} & M &::=& \mt{struct} \; d^* \; \mt{end} & \textrm{constant} \\
180 &&& X & \mt{variable} \\ 188 &&& X & \textrm{variable} \\
181 &&& M.X & \mt{projection} \\ 189 &&& M.X & \textrm{projection} \\
182 &&& M(M) & \mt{functor application} \\ 190 &&& M(M) & \textrm{functor application} \\
183 &&& \mt{functor}(X : S) : S = M & \mt{functor abstraction} \\ 191 &&& \mt{functor}(X : S) : S = M & \textrm{functor abstraction} \\
184 \end{array}$$ 192 \end{array}$$
185 193
186 There are two kinds of Ur files. A file named $M\texttt{.ur}$ is an \emph{implementation file}, and it should contain a sequence of declarations $d^*$. A file named $M\texttt{.urs}$ is an \emph{interface file}; it must always have a matching $M\texttt{.ur}$ and should contain a sequence of signature items $s^*$. When both files are present, the overall effect is the same as a monolithic declaration $\mt{structure} \; M : \mt{sig} \; s^* \; \mt{end} = \mt{struct} \; d^* \; \mt{end}$. When no interface file is included, the overall effect is similar, with a signature for module $M$ being inferred rather than just checked against an interface. 194 There are two kinds of Ur files. A file named $M\texttt{.ur}$ is an \emph{implementation file}, and it should contain a sequence of declarations $d^*$. A file named $M\texttt{.urs}$ is an \emph{interface file}; it must always have a matching $M\texttt{.ur}$ and should contain a sequence of signature items $s^*$. When both files are present, the overall effect is the same as a monolithic declaration $\mt{structure} \; M : \mt{sig} \; s^* \; \mt{end} = \mt{struct} \; d^* \; \mt{end}$. When no interface file is included, the overall effect is similar, with a signature for module $M$ being inferred rather than just checked against an interface.
187 195
196 \subsection{Shorthands}
197
198 There are a variety of derived syntactic forms that elaborate into the core syntax from the last subsection. We will present the additional forms roughly following the order in which we presented the constructs that they elaborate into.
199
200 In many contexts where record fields are expected, like in a projection $e.c$, a constant field may be written as simply $X$, rather than $\#X$.
201
202 A record type may be written $\{(c = c,)^*\}$, which elaborates to $\$[(c = c,)^*]$.
203
204 A tuple type $(\tau_1, \ldots, \tau_n)$ expands to a record type $\{1 = \tau_1, \ldots, n = \tau_n\}$, with natural numbers as field names. A tuple pattern $(p_1, \ldots, p_n)$ expands to a rigid record pattern $\{1 = p_1, \ldots, n = p_n\}$. Positive natural numbers may be used in most places where field names would be allowed.
205
206 In general, several adjacent $\lambda$ forms may be combined into one, and kind and type annotations may be omitted, in which case they are implicitly included as wildcards. More formally, for constructor-level abstractions, we can define a new non-terminal $b ::= x \mid (x :: \kappa) \mid [c \sim c]$ and allow composite abstractions of the form $\lambda b^+ \Rightarrow c$, elaborating into the obvious sequence of one core $\lambda$ per element of $b^+$.
207
208 For any signature item or declaration that defines some entity to be equal to $A$ with classification annotation $B$ (e.g., $\mt{val} \; x : B = A$), $B$ and the preceding colon (or similar punctuation) may be omitted, in which case it is filled in as a wildcard.
209
210 A signature item or declaration $\mt{type} \; x$ or $\mt{type} \; x = \tau$ is elaborated into $\mt{con} \; x :: \mt{Type}$ or $\mt{con} \; x :: \mt{Type} = \tau$, respectively.
211
212 A signature item or declaration $\mt{class} \; x = \lambda y :: \mt{Type} \Rightarrow c$ may be abbreviated $\mt{class} \; x \; y = c$.
213
214 Handling of implicit and explicit constructor arguments may be tweaked with some prefixes to variable references. An expression $@x$ is a version of $x$ where all implicit constructor arguments have been made explicit. An expression $@@x$ achieves the same effect, additionally halting automatic resolution of type class instances. The same syntax works for variables projected out of modules and for capitalized variables (datatype constructors).
215
216 At the expression level, an analogue is available of the composite $\lambda$ form for constructors. We define the language of binders as $b ::= x \mid (x : \tau) \mid (x \; ? \; \kappa) \mid [c \sim c]$. A lone variable $x$ as a binder stands for an expression variable of unspecified type.
217
218 A $\mt{val}$ or $\mt{val} \; \mt{rec}$ declaration may include expression binders before the equal sign, following the binder grammar from the last paragraph. Such declarations are elaborated into versions that add additional $\lambda$s to the fronts of the righthand sides, as appropriate. The keyword $\mt{fun}$ is a synonym for $\mt{val} \; \mt{rec}$.
219
220 A signature item $\mt{functor} \; X_1 \; (X_2 : S_1) : S_2$ is elaborated into $\mt{structure} \; X_1 : \mt{functor}(X_2 : S_1) : S_2$. A declaration $\mt{functor} \; X_1 \; (X_2 : S_1) : S_2 = M$ is elaborated into $\mt{structure} \; X_1 : \mt{functor}(X_2 : S_1) : S_2 = \mt{functor}(X_2 : S_1) : S_2 = M$.
221
222 A declaration $\mt{table} \; x : \{(c = c,)^*\}$ is elaborated into $\mt{table} \; x : [(c = c,)^*]$
223
224 The syntax $\mt{where} \; \mt{type}$ is an alternate form of $\mt{where} \; \mt{con}$.
225
226 The syntax $\mt{if} \; e \; \mt{then} \; e_1 \; \mt{else} \; e_2$ expands to $\mt{case} \; e \; \mt{of} \; \mt{Basis}.\mt{True} \Rightarrow e_1 \mid \mt{Basis}.\mt{False} \Rightarrow e_2$.
227
228 There are infix operator syntaxes for a number of functions defined in the $\mt{Basis}$ module. There is $=$ for $\mt{eq}$, $\neq$ for $\mt{neq}$, $-$ for $\mt{neg}$ (as a prefix operator) and $\mt{minus}$, $+$ for $\mt{plus}$, $\times$ for $\mt{times}$, $/$ for $\mt{div}$, $\%$ for $\mt{mod}$, $<$ for $\mt{lt}$, $\leq$ for $\mt{le}$, $>$ for $\mt{gt}$, and $\geq$ for $\mt{ge}$.
229
230 A signature item $\mt{table} \; x : c$ is shorthand for $\mt{val} \; x : \mt{Basis}.\mt{sql\_table} \; c$. $\mt{sequence} \; x$ is short for $\mt{val} \; x : \mt{Basis}.\mt{sql\_sequence}$, and $\mt{cookie} \; x : \tau$ is shorthand for $\mt{val} \; x : \mt{Basis}.\mt{http\_cookie} \; \tau$.
231
188 \end{document} 232 \end{document}