adam@1494: (* Introduction *) adam@1494: adam@1497: (* begin hide *) adam@1497: val show_string = mkShow (fn s => "\"" ^ s ^ "\"") adam@1497: (* end *) adam@1495: adam@1497: (* This tutorial by Adam Chlipala is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License. *) adam@1493: adam@1497: (* This is a tutorial for the Ur/Web programming language. The official project web site is your starting point for information, like a reference manual and where to download the latest code release. In this tutorial, we'll just focus on introducing the language features. *) adam@1497: adam@1497: (* Ur/Web contains a web-indendent core language called Ur, which will be the subject of the first few chapters of the tutorial. 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. *) adam@1497: adam@1497: (* * Basics *) adam@1497: adam@1497: (* 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!) *) adam@1493: adam@1493: (* begin eval *) adam@1497: 1 + 1 adam@1493: (* end *) adam@1493: adam@1497: (* begin eval *) adam@1497: 1.2 + 3.4 adam@1497: (* end *) adam@1496: adam@1497: (* begin eval *) adam@1497: "Hello " ^ "world!" adam@1497: (* end *) adam@1497: adam@1497: (* begin eval *) adam@1497: 1 + 1 < 6 adam@1497: (* end *) adam@1497: adam@1497: (* begin eval *) adam@1497: 0.0 < -3.2 adam@1497: (* end *) adam@1497: adam@1497: (* begin eval *) adam@1497: "Hello" = "Goodbye" || (1 * 2 <> 8 && True <> False) adam@1497: (* end *) adam@1497: adam@1497: (* We also have function definitions with type inference for parameter and return types. *) adam@1497: adam@1497: fun double n = 2 * n adam@1497: adam@1497: (* begin eval *) adam@1497: double 8 adam@1497: (* end *) adam@1497: adam@1497: fun fact n = if n = 0 then 1 else n * fact (n - 1) adam@1497: adam@1497: (* begin eval *) adam@1497: fact 5 adam@1497: (* end *) adam@1497: adam@1497: (* Of course we have anonymous functions, too. *) adam@1497: adam@1497: val inc = fn x => x + 1 adam@1497: adam@1497: (* begin eval *) adam@1497: inc 3 adam@1497: (* end *) adam@1497: adam@1497: (* 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. *) adam@1497: adam@1497: fun id [a] (x : a) : a = x adam@1497: adam@1497: (* begin eval *) adam@1497: id "hi" adam@1497: (* end *) adam@1497: adam@1497: fun compose [a] [b] [c] (f : b -> c) (g : a -> b) (x : a) : c = f (g x) adam@1497: adam@1497: (* begin eval *) adam@1497: compose inc inc 3 adam@1497: (* end *) adam@1498: adam@1499: (* 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 families appear before their arguments, as in Haskell. *) adam@1498: adam@1498: fun predecessor (n : int) : option int = if n >= 1 then Some (n - 1) else None adam@1498: adam@1498: (* begin hide *) adam@1498: fun show_option [t] (_ : show t) : show (option t) = adam@1498: mkShow (fn x => case x of adam@1498: None => "None" adam@1498: | Some x => "Some(" ^ show x ^ ")") adam@1498: (* end *) adam@1498: adam@1498: (* begin eval *) adam@1498: predecessor 6 adam@1498: (* end *) adam@1498: adam@1498: (* begin eval *) adam@1498: predecessor 0 adam@1498: (* end *) adam@1499: adam@1499: (* Naturally, there are lists, too! *) adam@1499: adam@1499: val numbers : list int = 1 :: 2 :: 3 :: [] adam@1499: val strings : list string = "a" :: "bc" :: [] adam@1499: adam@1499: fun length [a] (ls : list a) : int = adam@1499: case ls of adam@1499: [] => 0 adam@1499: | _ :: ls' => 1 + length ls' adam@1499: adam@1499: (* begin eval *) adam@1499: length numbers adam@1499: (* end *) adam@1499: adam@1499: (* begin eval *) adam@1499: length strings adam@1499: (* end *)