Ur (programming language)
Ur, also called Ur/Web, is a multi-paradigm, high-level, pure, strict, functional programming language. It is a dialect of the language ML, designed for web development, created by Adam Chlipala at the Massachusetts Institute of Technology that one program can emit code for a server, web browser client, and SQL specific to a given database backend. The full implementation is free and open-source software released under an MIT License.
Ur has its start and roots in a superseded progenitor language named Laconic/Web, in 2006.
Function
Ur supports a powerful kind of metaprogramming based on row data types.Ur/Web is Ur plus a special standard library and associated rules for parsing and optimizing. Ur/Web supports construction of dynamic web pages and applications backed by SQL databases. The signature of the standard library is such that well-typed Ur/Web programs "don't go wrong" in a very broad sense. They do not crash during particular page generations, and may not:
- Suffer from any kinds of code injection attacks
- Return invalid HTML
- Contain dead intra-application links
- Have mismatches between HTML forms and the fields expected by their handlers
- Include client-side code that makes incorrect assumptions about the "Ajax"-style services that the remote web server provides
- Attempt invalid SQL queries
- Use improper marshaling or unmarshaling in communication with SQL databases or between web browsers and web servers
The Ur/Web compiler also produces very efficient object code that does not use garbage collection.
SQL syntax templates embedded in the language facilitate the handling of tables.
Although the syntax is based on Standard ML the language includes concepts from Haskell with added type manipulation.
Ajax call/response is serialized through a monad called transaction and its marshalling and decoding is encapsulated in the rpc function.
The browser client side includes functional reactive programming facilities using the
type and a signal monad.Example program
This is a demo program showing client, server and database code with Ajax communication, from the web demos, with extra comments to outline each of the components:Interface file with extension:
val main : unit -> transaction page
Implementation file :
datatype list t = Nil | Cons of t * list t
table t :
PRIMARY KEY Id
fun add id s =
dml VALUES )
fun del id =
dml
fun lookup id =
ro <- oneOrNoRows ;
case ro of
None => return None
| Some r => return
fun check ls =
case ls of
Nil => return
| Cons =>
ao <- rpc ;
alert ;
check ls'
fun main =
idAdd <- source "";
aAdd <- source "";
idDel <- source "";
return
Project file, must contain an optional directive list followed by a listing of project modules:
# hash prefixed line comments
rewrite url Module1/main # set root URL to Module1/main function
exe myexename
database dbname=test # database attrib. and parameters
sql noisy.sql
$/list # stdlib modules prefixed with "$/"
module2 # if used by module1 it must precede it
module1 # main module
- server side, page retrieving functions with no side effects are accessible through a URL as ; they should have type .
- To export a page which may cause side effects, accessible only via HTTP POST, include one argument of the page handler of type Basis.postBody.
urweb module1 # looks for module1.urp
Execute as a web server :
./module1.exe -p 8081 ''# -h : RTS options help''
Libraries
Special features and problems
- Record updating
datatype mystruc k v = Empty | Node of
fun setKey
: mystruc k v =
if k1 < kmin
then error
else case my of
Node r => Node
| _ => error
corresponding signature implicit; :
con mystruc :: Type -> Type -> Type
val setKey : k ::: Type -> v ::: Type -> ord k -> string -> k -> mystruc k v -> mystruc k v
- Record fields ellipsis
case my of
Node => doWhatever k
| _ =>....
- Error "Substitution in constructor is blocked by a too-deep unification variable"
> 0 in nested case or let clauses and disappears by type annotating the variables in the nested clauses.