Ur (programming language)

{{Infobox programming language

| name = Ur, Ur/Web

| logo =

| logo caption =

| paradigms = functional, reactive

| family = ML, Haskell

| designer = Adam Chlipala

| developers =

| released = {{Start date and age|2014|12}}[https://www.reddit.com/r/programming/comments/2qbdm0/urweb_is_out_of_beta/ UrWeb is out of beta]

| latest release version = 20200209

| latest release date = {{Start date and age|2020|02|09}}

| typing = static, row

| platform = x86-64

| operating system = POSIX

| license = MIT

| website = {{URL|impredicative.com/ur}}

| file ext = .ur, .urs, .urp

| file format =

| implementations =

| dialects =

| influenced by = ML, Haskell{{cite web

|url=http://impredicative.com/ur/

|title=The Ur Programming Language Family

|publisher=Impredicative.com/ur

|access-date=3 April 2016}}

| influenced =

}}

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{{cite web

|last1=Chlipala

|first1=Adam

|date=January 2015

|url=https://dspace.mit.edu/handle/1721.1/92321

|title=Ur/Web: A Simple Model for Programming the Web

|publisher=MIT / Association for Computing Machinery (ACM)

|access-date= 5 January 2015}} 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,{{Cite web |last1=Chlipala |first1=Adam |date=2006 |url=https://laconic.sourceforge.net/ |title=The Laconic programming language family |website=SourceForge}} in 2006.{{Cite web |last1=Chlipala |first1=Adam |date=2006 |url=http://adam.chlipala.net/papers/LaconicTR/ |title=Scrap Your Web Application Boilerplate, or Metaprogramming with Row Types |website=Adam.Chlipala.net}}

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

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.

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 (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.

{{Quote box|align=right|width=50%|quote=

Ur/Web not only makes web applications easier to write, it also makes them more secure.

"Let's say you want to have a calendar widget on your web page, and you're going to use a library that provides the calendar widget, and on the same page there's also an advertisement box that's based on code that's provided by the ad network," Chlipala said.

"What you don't want is for the ad network to be able to change how the calendar works or the author of the calendar code to be able to interfere with delivering the ads."|source={{cite news

|last1=Hardesty |first1=Larry

|date=December 23, 2014

|url=https://news.mit.edu/2014/new-programming-language-coordinates-web-page-components-1223

|title=Taking the grunt work out of Web development

|publisher=Massachusetts Institute of Technology: MIT News

|access-date=29 December 2016}}

}}

Example program

This is a demo program showing client, server and database code with Ajax communication, from the web demos,[http://impredicative.com/ur/demo/ Ur language demo programs] with extra comments to outline each of the components:

Interface file (ML-like signature) with {{Not a typo|.urs}} 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 : { Id : int, A : string }

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 {[expression]} *)

dml (INSERT INTO t (Id, A) VALUES ({[id]}, {[s]}))

fun del id =

dml (DELETE FROM t WHERE t.Id = {[id]})

fun lookup id =

(* haskell style monadic code *)

ro <- oneOrNoRows (SELECT t.A FROM t WHERE t.Id = {[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

Libraries

  • [https://github.com/urweb/urweb/blob/master/lib/ur/basis.urs The predefined API]
  • [https://github.com/urweb/urweb/tree/master/lib/ur The standard library]
  • [https://github.com/urweb/urweb/tree/master/tests Per feature tests]
  • [http://www.impredicative.com/wiki/index.php/Libraries_and_FFI_bindings Ur wiki - Libraries and FFI bindings]

Special features and problems

  • Record updating

datatype mystruc k v = Empty | Node of {Key: k, Value: v}

fun setKey [k][v] (* type polymorphism *)

(_: ord k) (* implicit instance of class ord *)

(callerErrNote: string) (k1: k) (my: mystruc k v) : mystruc k v =

if k1 < kmin

then error setKey: illegal k1 {[callerErrNote]}

else case my of

Node r => Node (r -- #Key ++ {Key = k1})

| _ => error setKey: not a Node {[callerErrNote]}

corresponding signature (kind annotations (:::) implicit; (::) explicit):

con mystruc :: Type -> Type -> Type (* two param. type constructor *)

val setKey : k ::: Type -> v ::: Type -> ord k -> string -> k -> mystruc k v -> mystruc k v

  • Record fields ellipsis

case my of

Node {Key = k, ...} => doWhatever k

| _ => ....

  • Error "Substitution in constructor is blocked by a too-deep unification variable"[http://www.impredicative.com/pipermail/ur/2011-January/000498.html Unexpected type error: "Substitution in constructor is blocked by a too-deep unification variable"]

This error happens with types of arity > 0 in nested case or let clauses and disappears by type annotating the variables in the nested clauses.

See also

  • Opa, a language for combined frontend-backend development

References

{{Reflist}}