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, with mixed server-side and client-side applications generated from source code in one language.

Ur inherits its foundation from ML and Haskell, then going further to add fancier stuff. This first chapter of the tutorial reviews the key ML and Haskell features, giving their syntax in Ur. I do assume reading familiarity with ML and Haskell and won't dwell too much on explaining the imported features.

For information on compiling applications (and for some full example applications), see the intro page of the online demo, with further detail available in the reference manual.

Basics

Let's start with features shared with both ML and Haskell. First, we have the basic numeric, string, and Boolean stuff. (In the following examples, == is used to indicate the result of evaluating an expression. It's not valid Ur syntax!)

Then there's parametric polymorphism. Unlike in ML and Haskell, polymorphic functions in Ur/Web often require full type annotations. That is because more advanced features (which we'll get to in the next chapter) make Ur type inference undecidable.

The option type family is like ML's option or Haskell's Maybe. We also have a case expression form lifted directly from ML. Note that, while Ur follows most syntactic conventions of ML, one key difference is that type family names appear before their arguments, as in Haskell.

And lists make a good setting for demonstrating higher-order functions and local functions. (This example also introduces one idiosyncrasy of Ur, which is that map is a keyword, so we name our "map" function mp.)

We also have anonymous record types, as in Standard ML. The next chapter will show that there is quite a lot more going on here with records than in SML or OCaml, but we'll stick to the basics in this chapter. We will add one tantalizing hint of what's to come by demonstrating the record concatention operator ++ and the record field removal operator --.

We may package not just abstract types, but also abstract type families. Here we see our first use of the con keyword, which stands for constructor. Constructors are a generalization of types to include other "compile-time things"; for instance, basic type families, which are assigned the kindType -> Type. Kinds are to constructors as types are to normal values. We also see how to write the type of a polymorphic function, using the ::: syntax for type variable binding. This ::: differs from the :: used with the con keyword because it marks a type parameter as implicit, so that it need not be supplied explicitly at call sites. Such an option is the only one available in ML and Haskell, but, in the next chapter, we'll meet cases where it is appropriate to use explicit constructor parameters.

One can even use the open command to import a module's namespace wholesale, though this can make it harder for someone reading code to tell which identifiers come from which modules.

openIT
lookup (insert (insert empty 0 "A") 1 "B") 2
== None

Ur adopts OCaml's approach to splitting projects across source files. When a project contains files foo.ur and foo.urs, these are taken as defining a module named Foo whose signature is drawn from foo.urs and whose implementation is drawn from foo.ur. If foo.ur exists without foo.urs, then module Foo is defined without an explicit signature, so that it is assigned its principal signature, which exposes all typing details without abstraction.

Borrowed from Haskell

Ur includes a take on type classes. For instance, here is a generic "max" function that relies on a type class ord. Notice that the type class membership witness is treated like an ordinary function parameter, though we don't assign it a name here, because type inference figures out where it should be used. The more advanced examples of the next chapter will include cases where we manipulate type class witnesses explicitly.

That example had a mix of instances defined with a class and instances defined outside its module. Its possible to create closed type classes simply by omitting from the module an instance creation function like mkDouble. This way, only the instances you decide on may be allowed, which enables you to enforce program-wide invariants over instances.

Like Haskell, Ur supports the more general notion of constructor classes, whose instances may be parameterized over constructors with kinds beside Type. Also like in Haskell, the flagship constructor class is monad. Ur/Web's counterpart of Haskell's IO monad is transaction, which indicates the tight coupling with transactional execution in server-side code. Just as in Haskell, transaction must be used to create side-effecting actions, since Ur is purely functional (but has eager evaluation). Here is a quick example transaction, showcasing Ur's variation on Haskell do notation.