From 4593948fd27e6cdc4c24a0b008ce1beac4df2560 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Fri, 15 Jul 2011 18:45:03 -0400 Subject: Tutorial tweaks --- doc/intro.ur | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 6 deletions(-) (limited to 'doc/intro.ur') diff --git a/doc/intro.ur b/doc/intro.ur index 1bdc6de1..d474a1e1 100644 --- a/doc/intro.ur +++ b/doc/intro.ur @@ -1,15 +1,75 @@ (* Introduction *) -(* This tutorial is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License. *) +(* begin hide *) +val show_string = mkShow (fn s => "\"" ^ s ^ "\"") +(* end *) + +(* This tutorial by Adam Chlipala is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License. *) + +(* 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. *) + +(* 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. *) + +(* * 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!) *) + +(* begin eval *) +1 + 1 +(* end *) + +(* begin eval *) +1.2 + 3.4 +(* end *) + +(* begin eval *) +"Hello " ^ "world!" +(* end *) + +(* begin eval *) +1 + 1 < 6 +(* end *) + +(* begin eval *) +0.0 < -3.2 +(* end *) + +(* begin eval *) +"Hello" = "Goodbye" || (1 * 2 <> 8 && True <> False) +(* end *) + +(* We also have function definitions with type inference for parameter and return types. *) + +fun double n = 2 * n + +(* begin eval *) +double 8 +(* end *) + +fun fact n = if n = 0 then 1 else n * fact (n - 1) -(* Test evaluation.... *) +(* begin eval *) +fact 5 +(* end *) + +(* Of course we have anonymous functions, too. *) + +val inc = fn x => x + 1 + +(* begin eval *) +inc 3 +(* end *) + +(* 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. *) -fun f [a] (x : a) : a = x +fun id [a] (x : a) : a = x (* begin eval *) -f 6 +id "hi" (* end *) -(** Section *) +fun compose [a] [b] [c] (f : b -> c) (g : a -> b) (x : a) : c = f (g x) -val y = 9 +(* begin eval *) +compose inc inc 3 +(* end *) -- cgit v1.2.3