Ur (programming language)

From Infogalactic: the planetary knowledge core
(Redirected from Ur/Web)
Jump to: navigation, search
The Ur Programming Language Family
Paradigms functional, reactive
Family ML
Designed by Adam Chlipala
First appeared December 2014 [1]
Stable release 20160306 release / March 6, 2016; 8 years ago (2016-03-06)
Platform POSIX
License open source
Filename extensions .ur, .urs, .urp
Website impredicative.com/ur
Influenced by
Haskell, ML[2]

Ur also called Ur/Web is an open source functional programming language specific for web development, created by Adam Chlipala at the Massachusetts Institute of Technology[3] that from a single program produces server code, browser client code and SQL code specific for the chosen database backend.

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

Ur/Web is Ur plus a special standard library and associated rules for parsing and optimization. Ur/Web supports construction of dynamic web 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. Not only do they not crash during particular page generations, but they also may not:[2]

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

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

The implementation of all this is open source.[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 additional type manipulation.

Ajax call/response is serialized through a monad called transaction (corresponds to Haskell's 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.

<templatestyles src="Template:Quote_box/styles.css" />

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

[4]

Example program

This is a demo program showing client, server and database code with Ajax communication, from the web demos,[5] with extra comments to outline each of the components:

Interface file (ML-like signature) with .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 <xml><body>
      <button value="Check values of 1, 2, and 3" 
              onclick={fn _ => let val mylist = 1 :: 2 :: 3 :: []
                               in 
                                  check mylist
                               end   
                               }/><br/>
      <br/>
      <button value="Add" 
              onclick={fn _ => id <- get idAdd; 
                               a <- get aAdd; 
                               rpc (add (readError id) a)  (* Ajax call to server side *)
                               }/>
      <ctextbox source={idAdd}/>
      <ctextbox source={aAdd}/><br/>
      <br/>
      <button value="Delete" 
              onclick={fn _ => id <- get idDel; 
                               rpc (del (readError id))    (* Ajax call to server side *)
                               }/>
      <ctextbox source={idDel}/>
    </body></xml>

Project file (.urp extension), must contain an optional directive list followed by a listing of project modules:[6]

# 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 (http GET method) are accessible through a URL as /ModulePath/functionName, they should have type (unit -> transaction page).
  • 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.[7]

Compile:

urweb module1   # looks for module1.urp

Execute as a web server (other modes are CGI, FastCGI, ...):

./module1.exe -p 8081   # -h : RTS options help

Libraries

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 <xml>setKey: illegal k1 {[callerErrNote]}</xml>
   else case my of
             Node r => Node (r -- #Key ++ {Key = k1})
             | _ => error <xml>setKey: not a Node {[callerErrNote]}</xml>

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"[8]

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.

References

  1. UrWeb is out of beta
  2. 2.0 2.1 2.2 2.3 2.4 2.5 Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Ur language demo programs
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. The Ur/Web Manual - The Structure of Web Applications
  8. Unexpected type error: "Substitution in constructor is blocked by a too-deep unification variable"

External links