diff --git a/src/content/1.1/code/ocaml/snippet01.ml b/src/content/1.1/code/ocaml/snippet01.ml index 4343c96c..6652d9e0 100644 --- a/src/content/1.1/code/ocaml/snippet01.ml +++ b/src/content/1.1/code/ocaml/snippet01.ml @@ -1,6 +1 @@ -module type Polymorphic_Function_F = sig - type a - type b - - val f : a -> b -end +val f : a -> b diff --git a/src/content/1.1/code/ocaml/snippet02.ml b/src/content/1.1/code/ocaml/snippet02.ml index 33b36136..15342350 100644 --- a/src/content/1.1/code/ocaml/snippet02.ml +++ b/src/content/1.1/code/ocaml/snippet02.ml @@ -1,6 +1 @@ -module type Polymorphic_Function_G = sig - type b - type c - - val g : b -> c -end +val g : b -> c diff --git a/src/content/1.1/code/ocaml/snippet03.ml b/src/content/1.1/code/ocaml/snippet03.ml index 9ec1d227..c0896afa 100644 --- a/src/content/1.1/code/ocaml/snippet03.ml +++ b/src/content/1.1/code/ocaml/snippet03.ml @@ -1,9 +1,3 @@ -module Compose_Example - (F : Polymorphic_Function_F) - (G : Polymorphic_Function_G with type b = F.b) = -struct - (** OCaml doesn't have a compose operator. So, creating one. **) - let ( >> ) g f x = g (f x) - - let compose : 'a -> 'c = G.g >> F.f -end +(** OCaml doesn't have a compose operator. So, creating one. **) +let ( % ) g f x = g (f x) +g % f diff --git a/src/content/1.1/code/ocaml/snippet04.ml b/src/content/1.1/code/ocaml/snippet04.ml index a74aa394..407999af 100644 --- a/src/content/1.1/code/ocaml/snippet04.ml +++ b/src/content/1.1/code/ocaml/snippet04.ml @@ -1,9 +1,5 @@ -module Compose_Three_GF = functor(F:Polymorphic_Function_F)(G:Polymorphic_Function_G with type b = F.b)(H:Polymorphic_Function_H with type c = G.c) -> struct - let compose : 'a -> 'd = H.h >> (G.g >> F.f) -end +val f : a -> b +val g : b -> c +val h : c -> d -module Compose_Three_HG = functor(F:Polymorphic_Function_F)(G:Polymorphic_Function_G with type b = F.b)(H:Polymorphic_Function_H with type c = G.c) -> struct - let compose : 'a -> 'd = (H.h >> G.g) >> F.f -end - -Compose_Three_GF.compose = Compose_Three_HG.compose +h % (g % f) = (h % g) % f = h % g % f diff --git a/src/content/1.1/code/ocaml/snippet06.ml b/src/content/1.1/code/ocaml/snippet06.ml index 33456482..dc8642b1 100644 --- a/src/content/1.1/code/ocaml/snippet06.ml +++ b/src/content/1.1/code/ocaml/snippet06.ml @@ -1,2 +1,2 @@ -f >> id -id >> f +f % id = f +id % f = f diff --git a/src/content/1.2/code/ocaml/snippet01.ml b/src/content/1.2/code/ocaml/snippet01.ml index 6aedf3e2..48451390 100644 --- a/src/content/1.2/code/ocaml/snippet01.ml +++ b/src/content/1.2/code/ocaml/snippet01.ml @@ -1,3 +1 @@ -module type Chapter2_DeclareVariable = sig - val x : int -end +val x : int diff --git a/src/content/1.2/code/ocaml/snippet010.ml b/src/content/1.2/code/ocaml/snippet010.ml deleted file mode 100644 index 848f09b7..00000000 --- a/src/content/1.2/code/ocaml/snippet010.ml +++ /dev/null @@ -1 +0,0 @@ -let unit _ = () diff --git a/src/content/1.2/code/ocaml/snippet011.ml b/src/content/1.2/code/ocaml/snippet011.ml deleted file mode 100644 index 640d192e..00000000 --- a/src/content/1.2/code/ocaml/snippet011.ml +++ /dev/null @@ -1,3 +0,0 @@ -type bool = - | false - | true diff --git a/src/content/1.2/code/ocaml/snippet02.ml b/src/content/1.2/code/ocaml/snippet02.ml index d463de59..55e026c8 100644 --- a/src/content/1.2/code/ocaml/snippet02.ml +++ b/src/content/1.2/code/ocaml/snippet02.ml @@ -1,3 +1 @@ -module type Chapter2_DeclareFunction = sig - val f : bool -> bool -end +val f : bool -> bool diff --git a/src/content/1.2/code/ocaml/snippet03.ml b/src/content/1.2/code/ocaml/snippet03.ml index 2a140d02..6bb7863a 100644 --- a/src/content/1.2/code/ocaml/snippet03.ml +++ b/src/content/1.2/code/ocaml/snippet03.ml @@ -1,3 +1,2 @@ -module Chapter2_Bottom : Chapter2_DeclareFunction = struct - let f (b : bool) : bool = failwith "Not Implemented" -end +(* val f : bool -> bool *) +let f _x = failwith "Not Implemented" diff --git a/src/content/1.2/code/ocaml/snippet04.ml b/src/content/1.2/code/ocaml/snippet04.ml index 36a1faf9..876c69ba 100644 --- a/src/content/1.2/code/ocaml/snippet04.ml +++ b/src/content/1.2/code/ocaml/snippet04.ml @@ -1,3 +1,2 @@ -module Chapter2_Bottom : Chapter2_DeclareFunction = struct - let f : bool -> bool = fun _ -> failwith "Not implemented" -end +(* val f : bool -> bool *) +let f = failwith "Not Implemented" diff --git a/src/content/1.2/code/ocaml/snippet05.ml b/src/content/1.2/code/ocaml/snippet05.ml index 17852e43..01a12899 100644 --- a/src/content/1.2/code/ocaml/snippet05.ml +++ b/src/content/1.2/code/ocaml/snippet05.ml @@ -1 +1,14 @@ -let fact n = List.fold (List.range 1 n) ~init:1 ~f:( * ) +(* OCaml doesn't ship with a lazy sequence range syntax nor a product + function; defining our own. *) + +let rec range seq n1 n2 = + if n2 < n1 then seq + else n2 |> pred |> range (fun () -> Seq.Cons (n2, seq)) n1 +let range = range Seq.empty + +let rec product result seq = match seq () with + | Seq.Nil -> result + | Seq.Cons (n, seq) -> product (result * n) seq +let product = product 1 + +let fact n = product (range 1 n) diff --git a/src/content/1.2/code/ocaml/snippet06.ml b/src/content/1.2/code/ocaml/snippet06.ml index 49d3ba47..7a6528e3 100644 --- a/src/content/1.2/code/ocaml/snippet06.ml +++ b/src/content/1.2/code/ocaml/snippet06.ml @@ -1,3 +1,2 @@ type void - -let rec absurd (x : void) = absurd x +val absurd : void -> 'a diff --git a/src/content/1.2/code/ocaml/snippet07.ml b/src/content/1.2/code/ocaml/snippet07.ml index 225aa9b7..ca2b6b03 100644 --- a/src/content/1.2/code/ocaml/snippet07.ml +++ b/src/content/1.2/code/ocaml/snippet07.ml @@ -1 +1,2 @@ -let f44 () : int = 44 +(* val f44 : unit -> int *) +let f44 () = 44 diff --git a/src/content/1.2/code/ocaml/snippet08.ml b/src/content/1.2/code/ocaml/snippet08.ml index b887dd9c..cc4a78c8 100644 --- a/src/content/1.2/code/ocaml/snippet08.ml +++ b/src/content/1.2/code/ocaml/snippet08.ml @@ -1 +1,2 @@ -let f_int (x : int) = () +(* val f_int : int -> unit *) +let f_int x = () diff --git a/src/content/1.2/code/ocaml/snippet09.ml b/src/content/1.2/code/ocaml/snippet09.ml index f134290c..3db9b669 100644 --- a/src/content/1.2/code/ocaml/snippet09.ml +++ b/src/content/1.2/code/ocaml/snippet09.ml @@ -1 +1,2 @@ -let f_int (_ : int) = () +(* val f_int :: int -> unit *) +let f_int _ = () diff --git a/src/content/1.2/code/ocaml/snippet10.ml b/src/content/1.2/code/ocaml/snippet10.ml index 848f09b7..752e8a4e 100644 --- a/src/content/1.2/code/ocaml/snippet10.ml +++ b/src/content/1.2/code/ocaml/snippet10.ml @@ -1 +1,2 @@ +(* val unit : 'a -> unit *) let unit _ = () diff --git a/src/content/1.3/code/ocaml/snippet01.ml b/src/content/1.3/code/ocaml/snippet01.ml index 4f8b6bd0..9260f1dc 100644 --- a/src/content/1.3/code/ocaml/snippet01.ml +++ b/src/content/1.3/code/ocaml/snippet01.ml @@ -1,6 +1,6 @@ -module type Monoid = sig - type a +module type MONOID = sig + type t - val mempty : a - val mappend : a -> a -> a + val mempty : t + val mappend : t -> t -> t end diff --git a/src/content/1.3/code/ocaml/snippet02.ml b/src/content/1.3/code/ocaml/snippet02.ml index 3fdc5834..8a092162 100644 --- a/src/content/1.3/code/ocaml/snippet02.ml +++ b/src/content/1.3/code/ocaml/snippet02.ml @@ -1,6 +1,8 @@ -module StringMonoid : Monoid = struct - type a = string +(* In OCaml, any module that defines a module type's members, + automatically conforms to that module type. It doesn't need to + explicitly declare that it conforms. *) - let mempty = "" - let mappend = ( ^ ) -end +type t = string + +let mempty = "" +let mappend = ( ^ ) diff --git a/src/content/1.4/code/ocaml/snippet03.ml b/src/content/1.4/code/ocaml/snippet03.ml index b5533bd7..28053a47 100644 --- a/src/content/1.4/code/ocaml/snippet03.ml +++ b/src/content/1.4/code/ocaml/snippet03.ml @@ -1,7 +1 @@ -module type Kleisli = sig - type a - type b - type c - - val ( >=> ) : (a -> b writer) -> (b -> c writer) -> a -> c writer -end +val ( >=> ) : ('a -> 'b writer) -> ('b -> 'c writer) -> 'a -> 'c writer diff --git a/src/content/1.4/code/ocaml/snippet04.ml b/src/content/1.4/code/ocaml/snippet04.ml index d7c311f4..f91d7e6c 100644 --- a/src/content/1.4/code/ocaml/snippet04.ml +++ b/src/content/1.4/code/ocaml/snippet04.ml @@ -1 +1,4 @@ -let pure x = x, "" +let ( >=> ) m1 m2 = fun x -> + let y, s1 = m1 x in + let z, s2 = m2 y in + z, s1 ^ s2 diff --git a/src/content/1.4/code/ocaml/snippet05.ml b/src/content/1.4/code/ocaml/snippet05.ml index 380e46a5..a654e3ff 100644 --- a/src/content/1.4/code/ocaml/snippet05.ml +++ b/src/content/1.4/code/ocaml/snippet05.ml @@ -1,3 +1,2 @@ -let up_case : string -> string writer = - fun s -> String.uppercase s, "up_case " -;; +(* val return : 'a -> 'a writer *) +let return x = x, "" diff --git a/src/content/1.4/code/ocaml/snippet06.ml b/src/content/1.4/code/ocaml/snippet06.ml index ed0f0d29..f233ecfd 100644 --- a/src/content/1.4/code/ocaml/snippet06.ml +++ b/src/content/1.4/code/ocaml/snippet06.ml @@ -1,3 +1,6 @@ -let to_words : string -> string list writer = - fun s -> String.split s ~on:' ', "to_words " -;; +(* val up_case : string -> string writer + Note: OCaml strings are raw bytestrings, not UTF-8 encoded. *) +let up_case s = String.uppercase_ascii s, "up_case " + +(* val to_words : string -> string list writer *) +let to_words s = String.split_on_char ' ' s, "to_words " diff --git a/src/content/1.4/code/ocaml/snippet07.ml b/src/content/1.4/code/ocaml/snippet07.ml index 67a030ea..f4ee66ba 100644 --- a/src/content/1.4/code/ocaml/snippet07.ml +++ b/src/content/1.4/code/ocaml/snippet07.ml @@ -1,10 +1,2 @@ -module KleisiExample - (K : Kleisli - with type a = string - and type b = string - and type c = string list) = -struct - let up_case_and_to_words : string -> string list writer = - K.( >=> ) up_case to_words - ;; -end +(* val process : string -> string list writer *) +let process = up_case >=> to_words