comparison doc/manual.tex @ 1403:860c245a7c4d

More detail on sources and signals
author Adam Chlipala <adam@chlipala.net>
date Tue, 18 Jan 2011 13:32:48 -0500
parents c57414e39321
children 3dab4696d116
comparison
equal deleted inserted replaced
1402:d6ea63612909 1403:860c245a7c4d
1921 1921
1922 \subsubsection{Functional-Reactive Page Generation} 1922 \subsubsection{Functional-Reactive Page Generation}
1923 1923
1924 Most approaches to ``AJAX''-style coding involve imperative manipulation of the DOM tree representing an HTML document's structure. Ur/Web follows the \emph{functional-reactive} approach instead. Programs may allocate mutable \emph{sources} of arbitrary types, and an HTML page is effectively a pure function over the latest values of the sources. The page is not mutated directly, but rather it changes automatically as the sources are mutated. 1924 Most approaches to ``AJAX''-style coding involve imperative manipulation of the DOM tree representing an HTML document's structure. Ur/Web follows the \emph{functional-reactive} approach instead. Programs may allocate mutable \emph{sources} of arbitrary types, and an HTML page is effectively a pure function over the latest values of the sources. The page is not mutated directly, but rather it changes automatically as the sources are mutated.
1925 1925
1926 More operationally, you can think of a source as a mutable cell with facilities for subscription to change notifications. That level of detail is hidden behind a monadic facility to be described below. First, there are three primitive operations for working with sources just as if they were ML \cd{ref} cells, corresponding to ML's \cd{ref}, \cd{:=}, and \cd{!} operations.
1927
1926 $$\begin{array}{l} 1928 $$\begin{array}{l}
1927 \mt{con} \; \mt{source} :: \mt{Type} \to \mt{Type} \\ 1929 \mt{con} \; \mt{source} :: \mt{Type} \to \mt{Type} \\
1928 \mt{val} \; \mt{source} : \mt{t} ::: \mt{Type} \to \mt{t} \to \mt{transaction} \; (\mt{source} \; \mt{t}) \\ 1930 \mt{val} \; \mt{source} : \mt{t} ::: \mt{Type} \to \mt{t} \to \mt{transaction} \; (\mt{source} \; \mt{t}) \\
1929 \mt{val} \; \mt{set} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{t} \to \mt{transaction} \; \mt{unit} \\ 1931 \mt{val} \; \mt{set} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{t} \to \mt{transaction} \; \mt{unit} \\
1930 \mt{val} \; \mt{get} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{transaction} \; \mt{t} 1932 \mt{val} \; \mt{get} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{transaction} \; \mt{t}
1931 \end{array}$$ 1933 \end{array}$$
1932 1934
1933 Only source creation and setting are supported server-side, as a convenience to help in setting up a page, where you may wish to allocate many sources that will be referenced through the page. All server-side storage of values inside sources uses string serializations of values, while client-side storage uses normal JavaScript values. 1935 Only source creation and setting are supported server-side, as a convenience to help in setting up a page, where you may wish to allocate many sources that will be referenced through the page. All server-side storage of values inside sources uses string serializations of values, while client-side storage uses normal JavaScript values.
1934 1936
1935 Pure functions over arbitrary numbers of sources are represented in a monad of \emph{signals}, which may only be used in client-side code. 1937 Pure functions over arbitrary numbers of sources are represented in a monad of \emph{signals}, which may only be used in client-side code. This is presented to the programmer in the form of a monad $\mt{signal}$, each of whose values represents (conceptually) some pure function over all sources that may be allocated in the course of program execution. A monad operation $\mt{signal}$ denotes the identity function over a particular source. By using $\mt{signal}$ on a source, you implicitly subscribe to change notifications for that source. That is, your signal will automatically be recomputed as that source changes. The usual monad operators make it possible to build up complex signals that depend on multiple sources; automatic updating upon source-value changes still happens automatically.
1936 1938
1937 $$\begin{array}{l} 1939 $$\begin{array}{l}
1938 \mt{con} \; \mt{signal} :: \mt{Type} \to \mt{Type} \\ 1940 \mt{con} \; \mt{signal} :: \mt{Type} \to \mt{Type} \\
1939 \mt{val} \; \mt{signal\_monad} : \mt{monad} \; \mt{signal} \\ 1941 \mt{val} \; \mt{signal\_monad} : \mt{monad} \; \mt{signal} \\
1940 \mt{val} \; \mt{signal} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{signal} \; \mt{t} 1942 \mt{val} \; \mt{signal} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{signal} \; \mt{t}