comparison doc/manual.tex @ 784:990f80c1cec1

Revising manual through end of Section 6
author Adam Chlipala <adamc@hcoop.net>
date Tue, 05 May 2009 11:59:50 -0400
parents ec0a0dd0ca12
children fe63a08cbb91
comparison
equal deleted inserted replaced
783:ec0a0dd0ca12 784:990f80c1cec1
164 \end{verbatim} 164 \end{verbatim}
165 165
166 166
167 \section{Ur Syntax} 167 \section{Ur Syntax}
168 168
169 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. 169 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 relations, cookies, and styles.
170 170
171 \subsection{Lexical Conventions} 171 \subsection{Lexical Conventions}
172 172
173 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. 173 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.
174 174
341 &&& \mt{signature} \; X = S & \textrm{signature definition} \\ 341 &&& \mt{signature} \; X = S & \textrm{signature definition} \\
342 &&& \mt{open} \; M & \textrm{module inclusion} \\ 342 &&& \mt{open} \; M & \textrm{module inclusion} \\
343 &&& \mt{constraint} \; c \sim c & \textrm{record disjointness constraint} \\ 343 &&& \mt{constraint} \; c \sim c & \textrm{record disjointness constraint} \\
344 &&& \mt{open} \; \mt{constraints} \; M & \textrm{inclusion of just the constraints from a module} \\ 344 &&& \mt{open} \; \mt{constraints} \; M & \textrm{inclusion of just the constraints from a module} \\
345 &&& \mt{table} \; x : c & \textrm{SQL table} \\ 345 &&& \mt{table} \; x : c & \textrm{SQL table} \\
346 &&& \mt{view} \; x : c & \textrm{SQL view} \\
346 &&& \mt{sequence} \; x & \textrm{SQL sequence} \\ 347 &&& \mt{sequence} \; x & \textrm{SQL sequence} \\
347 &&& \mt{cookie} \; x : \tau & \textrm{HTTP cookie} \\ 348 &&& \mt{cookie} \; x : \tau & \textrm{HTTP cookie} \\
349 &&& \mt{style} \; x : \tau & \textrm{CSS class} \\
348 &&& \mt{class} \; x :: \kappa = c & \textrm{concrete constructor class} \\ 350 &&& \mt{class} \; x :: \kappa = c & \textrm{concrete constructor class} \\
349 \\ 351 \\
350 \textrm{Modules} & M &::=& \mt{struct} \; d^* \; \mt{end} & \textrm{constant} \\ 352 \textrm{Modules} & M &::=& \mt{struct} \; d^* \; \mt{end} & \textrm{constant} \\
351 &&& X & \textrm{variable} \\ 353 &&& X & \textrm{variable} \\
352 &&& M.X & \textrm{projection} \\ 354 &&& M.X & \textrm{projection} \\
354 &&& \mt{functor}(X : S) : S = M & \textrm{functor abstraction} \\ 356 &&& \mt{functor}(X : S) : S = M & \textrm{functor abstraction} \\
355 \end{array}$$ 357 \end{array}$$
356 358
357 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. 359 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.
358 360
361 We omit some extra possibilities in $\mt{table}$ syntax, deferring them to Section \ref{tables}.
362
359 \subsection{Shorthands} 363 \subsection{Shorthands}
360 364
361 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. 365 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.
362 366
363 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$. 367 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$.
390 394
391 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$. 395 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$.
392 396
393 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}$. 397 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}$.
394 398
395 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$. 399 A signature item $\mt{table} \; x : c$ is shorthand for $\mt{val} \; x : \mt{Basis}.\mt{sql\_table} \; c \; []$. $\mt{view} \; x : c$ is shorthand for $\mt{val} \; x : \mt{Basis}.\mt{sql\_view} \; c$, $\mt{sequence} \; x$ is short for $\mt{val} \; x : \mt{Basis}.\mt{sql\_sequence}$. $\mt{cookie} \; x : \tau$ is shorthand for $\mt{val} \; x : \mt{Basis}.\mt{http\_cookie} \; \tau$, and $\mt{style} \; x$ is shorthand for $\mt{val} \; x : \mt{Basis}.\mt{css\_class}$.
396 400
397 401
398 \section{Static Semantics} 402 \section{Static Semantics}
399 403
400 In this section, we give a declarative presentation of Ur's typing rules and related judgments. Inference is the subject of the next section; here, we assume that an oracle has filled in all wildcards with concrete values. 404 In this section, we give a declarative presentation of Ur's typing rules and related judgments. Inference is the subject of the next section; here, we assume that an oracle has filled in all wildcards with concrete values.
788 } 792 }
789 \quad \infer{\Gamma \vdash \mt{open} \; \mt{constraints} \; M \leadsto \Gamma, \mathcal O_c(M, \overline{s})}{ 793 \quad \infer{\Gamma \vdash \mt{open} \; \mt{constraints} \; M \leadsto \Gamma, \mathcal O_c(M, \overline{s})}{
790 \Gamma \vdash M : \mt{sig} \; \overline{s} \; \mt{end} 794 \Gamma \vdash M : \mt{sig} \; \overline{s} \; \mt{end}
791 }$$ 795 }$$
792 796
793 $$\infer{\Gamma \vdash \mt{table} \; x : c \leadsto \Gamma, x : \mt{Basis}.\mt{sql\_table} \; c}{ 797 $$\infer{\Gamma \vdash \mt{table} \; x : c \leadsto \Gamma, x : \mt{Basis}.\mt{sql\_table} \; c \; []}{
794 \Gamma \vdash c :: \{\mt{Type}\} 798 \Gamma \vdash c :: \{\mt{Type}\}
795 } 799 }
796 \quad \infer{\Gamma \vdash \mt{sequence} \; x \leadsto \Gamma, x : \mt{Basis}.\mt{sql\_sequence}}{}$$ 800 \quad \infer{\Gamma \vdash \mt{view} \; x : c \leadsto \Gamma, x : \mt{Basis}.\mt{sql\_view} \; c}{
801 \Gamma \vdash c :: \{\mt{Type}\}
802 }$$
803
804 $$\infer{\Gamma \vdash \mt{sequence} \; x \leadsto \Gamma, x : \mt{Basis}.\mt{sql\_sequence}}{}$$
797 805
798 $$\infer{\Gamma \vdash \mt{cookie} \; x : \tau \leadsto \Gamma, x : \mt{Basis}.\mt{http\_cookie} \; \tau}{ 806 $$\infer{\Gamma \vdash \mt{cookie} \; x : \tau \leadsto \Gamma, x : \mt{Basis}.\mt{http\_cookie} \; \tau}{
799 \Gamma \vdash \tau :: \mt{Type} 807 \Gamma \vdash \tau :: \mt{Type}
800 }$$ 808 }
801 809 \quad \infer{\Gamma \vdash \mt{style} \; x \leadsto \Gamma, x : \mt{Basis}.\mt{css\_class}}{}$$
802 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leadsto \Gamma, x :: \kappa \to \mt{Type} = c}{ 810
803 \Gamma \vdash c :: \kappa \to \mt{Type} 811 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leadsto \Gamma, x :: \kappa = c}{
812 \Gamma \vdash c :: \kappa
804 }$$ 813 }$$
805 814
806 $$\infer{\overline{y}; x; \Gamma \vdash \cdot \leadsto \Gamma}{} 815 $$\infer{\overline{y}; x; \Gamma \vdash \cdot \leadsto \Gamma}{}
807 \quad \infer{\overline{y}; x; \Gamma \vdash X \mid \overline{dc} \leadsto \Gamma', X : \overline{y ::: \mt{Type}} \to x \; \overline{y}}{ 816 \quad \infer{\overline{y}; x; \Gamma \vdash X \mid \overline{dc} \leadsto \Gamma', X : \overline{y ::: \mt{Type}} \to x \; \overline{y}}{
808 \overline{y}; x; \Gamma \vdash \overline{dc} \leadsto \Gamma' 817 \overline{y}; x; \Gamma \vdash \overline{dc} \leadsto \Gamma'
854 $$\infer{\Gamma \vdash \mt{constraint} \; c_1 \sim c_2 \leadsto \Gamma, c_1 \sim c_2}{ 863 $$\infer{\Gamma \vdash \mt{constraint} \; c_1 \sim c_2 \leadsto \Gamma, c_1 \sim c_2}{
855 \Gamma \vdash c_1 :: \{\kappa\} 864 \Gamma \vdash c_1 :: \{\kappa\}
856 & \Gamma \vdash c_2 :: \{\kappa\} 865 & \Gamma \vdash c_2 :: \{\kappa\}
857 }$$ 866 }$$
858 867
859 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leadsto \Gamma, x :: \kappa \to \mt{Type} = c}{ 868 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leadsto \Gamma, x :: \kappa = c}{
860 \Gamma \vdash c :: \kappa \to \mt{Type} 869 \Gamma \vdash c :: \kappa
861 } 870 }
862 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa \leadsto \Gamma, x :: \kappa \to \mt{Type}}{}$$ 871 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa \leadsto \Gamma, x :: \kappa}{}$$
863 872
864 \subsection{Signature Compatibility} 873 \subsection{Signature Compatibility}
865 874
866 To simplify the judgments in this section, we assume that all signatures are alpha-varied as necessary to avoid including multiple bindings for the same identifier. This is in addition to the usual alpha-variation of locally-bound variables. 875 To simplify the judgments in this section, we assume that all signatures are alpha-varied as necessary to avoid including multiple bindings for the same identifier. This is in addition to the usual alpha-variation of locally-bound variables.
867 876
917 $$\infer{\Gamma \vdash \mt{datatype} \; x = \mt{datatype} \; M.z \leq \mt{con} \; x :: \mt{Type}^{\mt{len}(y)} \to \mt{Type}}{ 926 $$\infer{\Gamma \vdash \mt{datatype} \; x = \mt{datatype} \; M.z \leq \mt{con} \; x :: \mt{Type}^{\mt{len}(y)} \to \mt{Type}}{
918 \Gamma \vdash M : \mt{sig} \; \overline{s} \; \mt{end} 927 \Gamma \vdash M : \mt{sig} \; \overline{s} \; \mt{end}
919 & \mt{proj}(M, \overline{s}, \mt{datatype} \; z) = (\overline{y}, \overline{dc}) 928 & \mt{proj}(M, \overline{s}, \mt{datatype} \; z) = (\overline{y}, \overline{dc})
920 }$$ 929 }$$
921 930
922 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa \leq \mt{con} \; x :: \kappa \to \mt{Type}}{} 931 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa \leq \mt{con} \; x :: \kappa}{}
923 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leq \mt{con} \; x :: \kappa \to \mt{Type}}{}$$ 932 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leq \mt{con} \; x :: \kappa}{}$$
924 933
925 $$\infer{\Gamma \vdash \mt{con} \; x :: \kappa = c_1 \leq \mt{con} \; x :: \mt{\kappa} = c_2}{ 934 $$\infer{\Gamma \vdash \mt{con} \; x :: \kappa = c_1 \leq \mt{con} \; x :: \mt{\kappa} = c_2}{
926 \Gamma \vdash c_1 \equiv c_2 935 \Gamma \vdash c_1 \equiv c_2
927 } 936 }
928 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c_1 \leq \mt{con} \; x :: \kappa \to \mt{Type} = c_2}{ 937 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c_1 \leq \mt{con} \; x :: \kappa = c_2}{
929 \Gamma \vdash c_1 \equiv c_2 938 \Gamma \vdash c_1 \equiv c_2
930 }$$ 939 }$$
931 940
932 $$\infer{\Gamma \vdash \mt{datatype} \; x \; \overline{y} = \overline{dc} \leq \mt{datatype} \; x \; \overline{y} = \overline{dc'}}{ 941 $$\infer{\Gamma \vdash \mt{datatype} \; x \; \overline{y} = \overline{dc} \leq \mt{datatype} \; x \; \overline{y} = \overline{dc'}}{
933 \Gamma, \overline{y :: \mt{Type}} \vdash \overline{dc} \leq \overline{dc'} 942 \Gamma, \overline{y :: \mt{Type}} \vdash \overline{dc} \leq \overline{dc'}
1017 \mt{sigOf}(\mt{signature} \; X = S) &=& \mt{signature} \; X = S \\ 1026 \mt{sigOf}(\mt{signature} \; X = S) &=& \mt{signature} \; X = S \\
1018 \mt{sigOf}(\mt{open} \; M) &=& \mt{include} \; S \textrm{ (where $\Gamma \vdash M : S$)} \\ 1027 \mt{sigOf}(\mt{open} \; M) &=& \mt{include} \; S \textrm{ (where $\Gamma \vdash M : S$)} \\
1019 \mt{sigOf}(\mt{constraint} \; c_1 \sim c_2) &=& \mt{constraint} \; c_1 \sim c_2 \\ 1028 \mt{sigOf}(\mt{constraint} \; c_1 \sim c_2) &=& \mt{constraint} \; c_1 \sim c_2 \\
1020 \mt{sigOf}(\mt{open} \; \mt{constraints} \; M) &=& \cdot \\ 1029 \mt{sigOf}(\mt{open} \; \mt{constraints} \; M) &=& \cdot \\
1021 \mt{sigOf}(\mt{table} \; x : c) &=& \mt{table} \; x : c \\ 1030 \mt{sigOf}(\mt{table} \; x : c) &=& \mt{table} \; x : c \\
1031 \mt{sigOf}(\mt{view} \; x : c) &=& \mt{view} \; x : c \\
1022 \mt{sigOf}(\mt{sequence} \; x) &=& \mt{sequence} \; x \\ 1032 \mt{sigOf}(\mt{sequence} \; x) &=& \mt{sequence} \; x \\
1023 \mt{sigOf}(\mt{cookie} \; x : \tau) &=& \mt{cookie} \; x : \tau \\ 1033 \mt{sigOf}(\mt{cookie} \; x : \tau) &=& \mt{cookie} \; x : \tau \\
1034 \mt{sigOf}(\mt{style} \; x) &=& \mt{style} \; x \\
1024 \mt{sigOf}(\mt{class} \; x :: \kappa = c) &=& \mt{class} \; x :: \kappa = c \\ 1035 \mt{sigOf}(\mt{class} \; x :: \kappa = c) &=& \mt{class} \; x :: \kappa = c \\
1025 \end{eqnarray*} 1036 \end{eqnarray*}
1026 \begin{eqnarray*} 1037 \begin{eqnarray*}
1027 \mt{selfify}(M, \cdot) &=& \cdot \\ 1038 \mt{selfify}(M, \cdot) &=& \cdot \\
1028 \mt{selfify}(M, s \; \overline{s'}) &=& \mt{selfify}(M, s) \; \mt{selfify}(M, \overline{s'}) \\ 1039 \mt{selfify}(M, s \; \overline{s'}) &=& \mt{selfify}(M, s) \; \mt{selfify}(M, \overline{s'}) \\
1096 1107
1097 The type inference engine tries to take advantage of the algebraic rules governing type-level records, as shown in Section \ref{definitional}. When two constructors of record kind are unified, they are reduced to normal forms, with like terms crossed off from each normal form until, hopefully, nothing remains. This cannot be complete, with the inclusion of unification variables. The type-checker can help you understand what goes wrong when the process fails, as it outputs the unmatched remainders of the two normal forms. 1108 The type inference engine tries to take advantage of the algebraic rules governing type-level records, as shown in Section \ref{definitional}. When two constructors of record kind are unified, they are reduced to normal forms, with like terms crossed off from each normal form until, hopefully, nothing remains. This cannot be complete, with the inclusion of unification variables. The type-checker can help you understand what goes wrong when the process fails, as it outputs the unmatched remainders of the two normal forms.
1098 1109
1099 \subsection{\label{typeclasses}Constructor Classes} 1110 \subsection{\label{typeclasses}Constructor Classes}
1100 1111
1101 Ur includes a constructor class facility inspired by Haskell's. The current version is very rudimentary, only supporting instances for particular constructors built up from abstract constructors and datatypes and type-level application. 1112 Ur includes a constructor class facility inspired by Haskell's. The current version is experimental, with very general Prolog-like facilities that can lead to compile-time non-termination.
1102 1113
1103 Constructor classes are integrated with the module system. A constructor class of kind $\kappa$ is just a constructor of kind $\kappa \to \mt{Type}$. By marking such a constructor $c$ as a constructor class, the programmer instructs the type inference engine to, in each scope, record all values of types $c \; c'$ as \emph{instances}. Any function argument whose type is of such a form is treated as implicit, to be determined by examining the current instance database. 1114 Constructor classes are integrated with the module system. A constructor class of kind $\kappa$ is just a constructor of kind $\kappa$. By marking such a constructor $c$ as a constructor class, the programmer instructs the type inference engine to, in each scope, record all values of types $c \; c_1 \; \ldots \; c_n$ as \emph{instances}. Any function argument whose type is of such a form is treated as implicit, to be determined by examining the current instance database.
1104 1115
1105 The ``dictionary encoding'' often used in Haskell implementations is made explicit in Ur. Constructor class instances are just properly-typed values, and they can also be considered as ``proofs'' of membership in the class. In some cases, it is useful to pass these proofs around explicitly. An underscore written where a proof is expected will also be inferred, if possible, from the current instance database. 1116 The ``dictionary encoding'' often used in Haskell implementations is made explicit in Ur. Constructor class instances are just properly-typed values, and they can also be considered as ``proofs'' of membership in the class. In some cases, it is useful to pass these proofs around explicitly. An underscore written where a proof is expected will also be inferred, if possible, from the current instance database.
1106 1117
1107 Just as for constructors, constructors classes may be exported from modules, and they may be exported as concrete or abstract. Concrete constructor classes have their ``real'' definitions exposed, so that client code may add new instances freely. Abstract constructor classes are useful as ``predicates'' that can be used to enforce invariants, as we will see in some definitions of SQL syntax in the Ur/Web standard library. 1118 Just as for constructors, constructors classes may be exported from modules, and they may be exported as concrete or abstract. Concrete constructor classes have their ``real'' definitions exposed, so that client code may add new instances freely. Abstract constructor classes are useful as ``predicates'' that can be used to enforce invariants, as we will see in some definitions of SQL syntax in the Ur/Web standard library.
1108 1119
1192 1203
1193 The fundamental unit of interest in the embedding of SQL is tables, described by a type family and creatable only via the $\mt{table}$ declaration form. 1204 The fundamental unit of interest in the embedding of SQL is tables, described by a type family and creatable only via the $\mt{table}$ declaration form.
1194 $$\begin{array}{l} 1205 $$\begin{array}{l}
1195 \mt{con} \; \mt{sql\_table} :: \{\mt{Type}\} \to \mt{Type} 1206 \mt{con} \; \mt{sql\_table} :: \{\mt{Type}\} \to \mt{Type}
1196 \end{array}$$ 1207 \end{array}$$
1208
1209 \subsubsection{\label{tables}Tables}
1197 1210
1198 \subsubsection{Queries} 1211 \subsubsection{Queries}
1199 1212
1200 A final query is constructed via the $\mt{sql\_query}$ function. Constructor arguments respectively specify the table fields we select (as records mapping tables to the subsets of their fields that we choose) and the (always named) extra expressions that we select. 1213 A final query is constructed via the $\mt{sql\_query}$ function. Constructor arguments respectively specify the table fields we select (as records mapping tables to the subsets of their fields that we choose) and the (always named) extra expressions that we select.
1201 $$\begin{array}{l} 1214 $$\begin{array}{l}