Ur (programming language) explained

Ur, Ur/Web
Paradigms:functional, reactive
Family:ML, Haskell
Designer:Adam Chlipala
Developer:-->
Released:[1]
Latest Release Version:20200209
Typing:static, row
Platform:x86-64
Operating System:POSIX
License:MIT
File Ext:.ur, .urs, .urp
File Formats:-->
Influenced By:ML, Haskell[2]

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[3] 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.[2]

Ur has its start and roots in a superseded progenitor language named Laconic/Web,[4] in 2006.[5]

Function

Ur supports a powerful kind of metaprogramming based on row data types.[2]

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:[2]

This type safety is just the foundation of the Ur/Web methodology. It is also possible to use metaprogramming to build significant application pieces by analysis of type structure.[2]

The Ur/Web compiler also produces very efficient object code that does not use garbage collection.[2]

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 (corresponds to Haskell's input/output (IO)) and its marshalling and decoding is encapsulated in the rpc function.

The browser client side includes functional reactive programming facilities using the (source a) 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,[6] with extra comments to outline each of the components:

Interface file (ML-like signature) with extension:(* the environment monad is called transaction, corresponds to Haskell's IO monad *)val main : unit -> transaction page

Implementation file (.ur extension):datatype list t = Nil | Cons of t * list t

table t : PRIMARY KEY Id

(* server side database access, called through AJAX XmlHttpRequest encapsulated as rpc function (remote procedure call) *)fun add id s = (* sql dml template with *) dml (INSERT INTO t (Id, A) VALUES)

fun del id = dml (DELETE FROM t WHERE t.Id =)

fun lookup id = (* haskell style monadic code *) ro <- oneOrNoRows (SELECT t.A FROM t WHERE t.Id =); case ro of None => return None (* return is the monad lifting function *) | Some r => return (Some r.T.A)

(* check called by client side onClick event handler, so it will be compiled to JavaScript as page embedded client script *)fun check ls = case ls of Nil => return | Cons (id, ls') => ao <- rpc (lookup id); (* Ajax call to server side *) alert (case ao of None => "Nada" | Some a => a); check ls'

fun main = idAdd <- source ""; aAdd <- source "";

idDel <- source "";

(* generates web page with JavaScript inclusions *) return