(* This transcript has been annotated with comments *) weep% ocaml Objective Caml version 2.04 # 5 ;; - : int = 5 # true ;; - : bool = true # 1 + 3 ;; - : int = 4 # fun (x : int) -> x ;; - : int -> int = # fun x -> x;; - : 'a -> 'a = (* 'a is read as "alpha" 'a -> 'a is FORALL X . X -> X in our notation *) # let i = fun x -> x;; val i : 'a -> 'a = # i 8 ;; (* ML implicitly inserts a type application to specialize i to int -> int *) - : int = 8 # i(8);; - : int = 8 # 8(9);; Characters 0-1: This expression is not a function, it cannot be applied # (i 8) 9;; Characters 1-4: This expression is not a function, it cannot be applied # (1 , 2) ;; - : int * int = 1, 2 # 1 , 3 , 7 ;; - : int * int * int = 1, 3, 7 # 3 , true , i ;; - : int * bool * ('a -> 'a) = 3, true, # fun x -> x ;; - : 'a -> 'a = # function 0 -> 1 | x -> 0 ;; - : int -> int = # n = function 0 -> 1 | x -> 0 ;; Characters 0-1: Unbound value n (* forgot `let' *) # let n = function 0 -> 1 | x -> 0 ;; val n : int -> int = # n 0;; - : int = 1 # n (-354);; - : int = 0 # n -354 ;; Characters 0-1: This expression has type int -> int but is here used with type int (* the expression was parsed as `(n) - (354)' *) # let swap = function (x, y) -> (y, x) ;; val swap : 'a * 'b -> 'b * 'a = # swap (1,2) ;; - : int * int = 2, 1 # let add = function (x, y) -> (y+x) ;; val add : int * int -> int = # add (1, 2);; - : int = 3 # match 1 with 2 -> 9 | 1 -> 78 ;; Characters 0-33: Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: 0 - : int = 78 # match 1 with | 1 -> 78 ;; Characters 0-23: Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: 0 - : int = 78 # match 1 with 1 -> 78 | x -> 17 ;; - : int = 78 # match 1 with x -> 17 | 1 -> 19 ;; Characters 25-26: Warning: this match case is unused. - : int = 17 # match swap(1,2) with (x, y) -> x+y ;; - : int = 3 (* [Emacs garbling elided here] *) # match swap(1,2) with (x, 0) -> 0 | (x,y) -> x + y ;; - : int = 3 # match swap(0,2) with (x, 0) -> 0 | (x,y) -> x + y ;; - : int = 0 # if true then 1 else 2 ;; - : int = 1 # if (7 = 8) then 1 else 2 ;; - : int = 2 (* [bad example elided here] *) # let g = function 0-> 0 | x -> g (x - 1);; Characters 45-46: Unbound value g # let rec g = function 0-> 0 | x -> g (x - 1);; val g : int -> int = # let rec odd = function 0 -> false | x -> even (x -1) and even = function 0 -> true | x -> odd(x-1) ;; val odd : int -> bool = val even : int -> bool = # odd 0;; - : bool = false # odd 987;; - : bool = true # let g2 = ... and f2 = ...;; (* using `and' with `let': the value for `f2' wouldn't be able to see `g2' *) Characters 9-12: Syntax error # 1 ;; - : int = 1 # false ;; - : bool = false # type mytype = Foo | Bar of int ;; type mytype = | Foo | Bar of int # Foo;; - : mytype = Foo # Bar(0) ;; - : mytype = Bar 0 # Bar (785);; - : mytype = Bar 785 # let name = function Foo -> "foo" | Bar(n) -> "bar" ;; val name : mytype -> string = # name Foo;; - : string = "foo" # name Bar(10);; Characters 5-8: The constructor Bar expects 1 argument(s), but is here applied to 0 argument(s) # name (Bar(10));; - : string = "bar" # name (Bar 10);; - : string = "bar" # let name = function Foo -> "foo" | Bar(n) -> n ;; Characters 65-66: This expression has type int but is here used with type string # let name = function Foo -> 0 | Bar(n) -> n ;; val name : mytype -> int = # name Foo ;; - : int = 0 # name (Bar 10);; - : int = 10 # (* ctl-d to exit *)