diff --git a/collects/typed-scheme/typed-scheme.scrbl b/collects/typed-scheme/typed-scheme.scrbl index d5d667e7..95baadbf 100644 --- a/collects/typed-scheme/typed-scheme.scrbl +++ b/collects/typed-scheme/typed-scheme.scrbl @@ -41,7 +41,8 @@ easy to start using Typed Scheme. The following program defines the Fibonacci function in PLT Scheme: -@schememod[scheme +@schememod[ +scheme (define (fib n) (cond [(= 0 n) 1] [(= 1 n) 1] @@ -50,7 +51,8 @@ The following program defines the Fibonacci function in PLT Scheme: This program defines the same program using Typed Scheme. -@schememod[typed-scheme +@schememod[ +typed-scheme (: fib (Number -> Number)) (define (fib n) (cond [(= 0 n) 1] @@ -76,7 +78,8 @@ PLT Scheme program to transform it into a Typed Scheme program. Other typed binding forms are also available. For example, we could have rewritten our fibonacci program as follows: -@schememod[typed-scheme +@schememod[ +typed-scheme (: fib (Number -> Number)) (define (fib n) (let ([base? (or (= 0 n) (= 1 n))]) @@ -91,7 +94,8 @@ annotations are required. Typed Scheme infers the type of We can also define mutually-recursive functions: -@schememod[typed-scheme +@schememod[ +typed-scheme (: my-odd? (Number -> Boolean)) (define (my-odd? n) (if (= 0 n) #f @@ -116,7 +120,8 @@ to PLT Scheme structures. The following program defines a date structure and a function that formats a date as a string, using PLT Scheme's built-in @scheme[format] function. -@schememod[typed-scheme +@schememod[ +typed-scheme (define-struct: Date ([day : Number] [month : String] [year : Number])) (: format-date (Date -> String)) @@ -141,7 +146,8 @@ we would have with @scheme[define-struct]. Many data structures involve multiple variants. In Typed Scheme, we represent these using @italic{union types}, written @scheme[(U t1 t2 ...)]. -@schememod[typed-scheme +@schememod[ +typed-scheme (define-type-alias Tree (U leaf node)) (define-struct: leaf ([val : Number])) (define-struct: node ([left : Tree] [right : Tree])) @@ -195,7 +201,8 @@ Virtually every Scheme program uses lists and sexpressions. Fortunately, Typed Scheme can handle these as well. A simple list processing program can be written like this: -@schememod[typed-scheme +@schememod[ +typed-scheme (: sum-list ((Listof Number) -> Number)) (define (sum-list l) (cond [(null? l) 0] @@ -212,7 +219,8 @@ want. We can define our own type constructors as well. For example, here is an analog of the @tt{Maybe} type constructor from Haskell: -@schememod[typed-scheme +@schememod[ +typed-scheme (define-struct: Nothing ()) (define-struct: (a) Just ([v : a])) @@ -258,7 +266,8 @@ Sometimes functions over polymorphic data structures only concern themselves with the form of the structure. For example, one might write a function that takes the length of a list of numbers: -@schememod[typed-scheme +@schememod[ +typed-scheme (: list-number-length ((Listof Number) -> Integer)) (define (list-number-length l) (if (null? l) @@ -267,7 +276,8 @@ write a function that takes the length of a list of numbers: and also a function that takes the length of a list of strings: -@schememod[typed-scheme +@schememod[ +typed-scheme (: list-string-length ((Listof String) -> Integer)) (define (list-string-length l) (if (null? l) @@ -281,7 +291,8 @@ definition. We can abstract over the type of the element as follows: -@schememod[typed-scheme +@schememod[ +typed-scheme (: list-length (All (A) ((Listof A) -> Integer))) (define (list-length l) (if (null? l) @@ -301,7 +312,8 @@ Typed Scheme can handle some uses of rest arguments. In Scheme, one can write a function that takes an arbitrary number of arguments as follows: -@schememod[scheme +@schememod[ +scheme (define (sum . xs) (if (null? xs) 0 @@ -318,7 +330,8 @@ to the rest parameter. So the examples above evaluate to We can define such functions in Typed Scheme as well: -@schememod[typed-scheme +@schememod[ +typed-scheme (: sum (Number * -> Number)) (define (sum . xs) (if (null? xs) @@ -333,7 +346,8 @@ of the rest parameter is used at the same type. However, the rest argument may be used as a heterogeneous list. Take this (simplified) definition of the Scheme function @scheme[map]: -@schememod[scheme +@schememod[ +scheme (define (map f as . bss) (if (or (null? as) (ormap null? bss)) @@ -358,7 +372,8 @@ The example uses of @scheme[map] evaluate to @schemeresult[(list 2 3 4 5)], In Typed Scheme, we can define @scheme[map] as follows: -@schememod[typed-scheme +@schememod[ +typed-scheme (: map (All (C A B ...) ((A B ... B -> C) (Listof A) (Listof B) ... B