comparison doc/manual.tex @ 659:d3e1b4f337e4

Functional-reactive subsection
author Adam Chlipala <adamc@hcoop.net>
date Thu, 12 Mar 2009 12:10:02 -0400
parents 81c5c2674215
children 9abeb533f6a7
comparison
equal deleted inserted replaced
658:81c5c2674215 659:d3e1b4f337e4
1446 One last useful function is for aborting any page generation, returning some XML as an error message. This function takes the place of some uses of a general exception mechanism. 1446 One last useful function is for aborting any page generation, returning some XML as an error message. This function takes the place of some uses of a general exception mechanism.
1447 $$\begin{array}{l} 1447 $$\begin{array}{l}
1448 \mt{val} \; \mt{error} : \mt{t} ::: \mt{Type} \to \mt{xml} \; [\mt{Body}] \; [] \; [] \to \mt{t} 1448 \mt{val} \; \mt{error} : \mt{t} ::: \mt{Type} \to \mt{xml} \; [\mt{Body}] \; [] \; [] \to \mt{t}
1449 \end{array}$$ 1449 \end{array}$$
1450 1450
1451
1452 \subsection{Functional-Reactive Client-Side Programming}
1453
1454 Ur/Web supports running code on web browsers, via automatic compilation to JavaScript. Most approaches to this kind of 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.
1455
1456 $$\begin{array}{l}
1457 \mt{con} \; \mt{source} :: \mt{Type} \to \mt{Type} \\
1458 \mt{val} \; \mt{source} : \mt{t} ::: \mt{Type} \to \mt{t} \to \mt{transaction} \; (\mt{source} \; \mt{t}) \\
1459 \mt{val} \; \mt{set} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{t} \to \mt{transaction} \; \mt{unit} \\
1460 \mt{val} \; \mt{get} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{transaction} \; \mt{t}
1461 \end{array}$$
1462
1463 Pure functions over sources are represented in a monad of \emph{signals}.
1464
1465 $$\begin{array}{l}
1466 \mt{con} \; \mt{signal} :: \mt{Type} \to \mt{Type} \\
1467 \mt{val} \; \mt{signal\_monad} : \mt{monad} \; \mt{signal} \\
1468 \mt{val} \; \mt{signal} : \mt{t} ::: \mt{Type} \to \mt{source} \; \mt{t} \to \mt{signal} \; \mt{t}
1469 \end{array}$$
1470
1471 A reactive portion of an HTML page is injected with a $\mt{dyn}$ tag, which has a signal-valued attribute $\mt{Signal}$.
1472
1473 $$\begin{array}{l}
1474 \mt{val} \; \mt{dyn} : \mt{ctx} ::: \{\mt{Unit}\} \to \mt{use} ::: \{\mt{Type}\} \to \mt{bind} ::: \{\mt{Type}\} \to \mt{unit} \\
1475 \hspace{.1in} \to \mt{tag} \; [\mt{Signal} = \mt{signal} \; (\mt{xml} \; \mt{ctx} \; \mt{use} \; \mt{bind})] \; \mt{ctx} \; [] \; \mt{use} \; \mt{bind}
1476 \end{array}$$
1477
1478 Transactions can be run on the client by including them in attributes like the $\mt{OnClick}$ attribute of $\mt{button}$, and GUI widgets like $\mt{ctextbox}$ have $\mt{Source}$ attributes that can be used to connect them to sources, so that their values can be read by code running because of, e.g., an $\mt{OnClick}$ event.
1451 1479
1452 \section{Ur/Web Syntax Extensions} 1480 \section{Ur/Web Syntax Extensions}
1453 1481
1454 Ur/Web features some syntactic shorthands for building values using the functions from the last section. This section sketches the grammar of those extensions. We write spans of syntax inside brackets to indicate that they are optional. 1482 Ur/Web features some syntactic shorthands for building values using the functions from the last section. This section sketches the grammar of those extensions. We write spans of syntax inside brackets to indicate that they are optional.
1455 1483