From 5200e2d296d3953b484992965c5f74f5c840ac90 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:12:49 +0100 Subject: [PATCH 01/27] Update Ch. 1 OCaml snippets --- src/content/1.1/code/ocaml/snippet01.ml | 7 +------ src/content/1.1/code/ocaml/snippet02.ml | 7 +------ src/content/1.1/code/ocaml/snippet03.ml | 14 ++++++-------- src/content/1.1/code/ocaml/snippet04.ml | 15 ++++++--------- src/content/1.1/code/ocaml/snippet05.ml | 3 ++- src/content/1.1/code/ocaml/snippet06.ml | 4 ++-- 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/content/1.1/code/ocaml/snippet01.ml b/src/content/1.1/code/ocaml/snippet01.ml index 4343c96c5..f75bf920f 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 (* In a module signature *) diff --git a/src/content/1.1/code/ocaml/snippet02.ml b/src/content/1.1/code/ocaml/snippet02.ml index 33b361362..e315cebfe 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 (* In a module signature *) diff --git a/src/content/1.1/code/ocaml/snippet03.ml b/src/content/1.1/code/ocaml/snippet03.ml index 9ec1d227b..67bb0bb26 100644 --- a/src/content/1.1/code/ocaml/snippet03.ml +++ b/src/content/1.1/code/ocaml/snippet03.ml @@ -1,9 +1,7 @@ -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) +Fun.compose g f (* Since OCaml 5.2 *) - let compose : 'a -> 'c = G.g >> F.f -end +(* Alternatively, you can also define it as follows: *) +let compose f g x = f (g x) + +(* Or even as an infix operator: *) +let ( % ) = Fun.compose (* Like in the Batteries library *) diff --git a/src/content/1.1/code/ocaml/snippet04.ml b/src/content/1.1/code/ocaml/snippet04.ml index a74aa3949..16743c76e 100644 --- a/src/content/1.1/code/ocaml/snippet04.ml +++ b/src/content/1.1/code/ocaml/snippet04.ml @@ -1,9 +1,6 @@ -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 - -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 +(* Assuming these signatures *) +val f : a -> b +val g : b -> c +val h : c -> d +(* These are equivalent *) +h % (g % f) = (h % g) % f = h % g % f diff --git a/src/content/1.1/code/ocaml/snippet05.ml b/src/content/1.1/code/ocaml/snippet05.ml index f54d47a87..8760cd2c9 100644 --- a/src/content/1.1/code/ocaml/snippet05.ml +++ b/src/content/1.1/code/ocaml/snippet05.ml @@ -1 +1,2 @@ -let id x = x +(* val id : 'a -> 'a *) +let id x = x (* Also defined as Fun.id in the Stdlib *) diff --git a/src/content/1.1/code/ocaml/snippet06.ml b/src/content/1.1/code/ocaml/snippet06.ml index 33456482f..84b2dc323 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 % Fun.id = f +Fun.id % f = f From d26f52f8b6762184248870499029c4230a98af1f Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:13:36 +0100 Subject: [PATCH 02/27] Update Ch. 2 OCaml snippets --- src/content/1.2/code/ocaml/snippet010.ml | 1 - src/content/1.2/code/ocaml/snippet011.ml | 3 --- src/content/1.2/code/ocaml/snippet04.ml | 2 +- src/content/1.2/code/ocaml/snippet05.ml | 2 +- src/content/1.2/code/ocaml/snippet11.ml | 4 +--- 5 files changed, 3 insertions(+), 9 deletions(-) delete mode 100644 src/content/1.2/code/ocaml/snippet010.ml delete mode 100644 src/content/1.2/code/ocaml/snippet011.ml 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 848f09b7c..000000000 --- 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 640d192e3..000000000 --- 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/snippet04.ml b/src/content/1.2/code/ocaml/snippet04.ml index 36a1faf99..b69073a5f 100644 --- a/src/content/1.2/code/ocaml/snippet04.ml +++ b/src/content/1.2/code/ocaml/snippet04.ml @@ -1,3 +1,3 @@ module Chapter2_Bottom : Chapter2_DeclareFunction = struct - let f : bool -> bool = fun _ -> failwith "Not implemented" + let f : bool -> bool = failwith "Not implemented" end diff --git a/src/content/1.2/code/ocaml/snippet05.ml b/src/content/1.2/code/ocaml/snippet05.ml index 17852e432..8562285ee 100644 --- a/src/content/1.2/code/ocaml/snippet05.ml +++ b/src/content/1.2/code/ocaml/snippet05.ml @@ -1 +1 @@ -let fact n = List.fold (List.range 1 n) ~init:1 ~f:( * ) +let fact n = Seq.(fold_left ( * ) 1 (take n (ints 1))) diff --git a/src/content/1.2/code/ocaml/snippet11.ml b/src/content/1.2/code/ocaml/snippet11.ml index 640d192e3..d621b2a19 100644 --- a/src/content/1.2/code/ocaml/snippet11.ml +++ b/src/content/1.2/code/ocaml/snippet11.ml @@ -1,3 +1 @@ -type bool = - | false - | true +type bool = false | true From 5c25300e3b9319927089884126d650a31f5a5829 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:13:59 +0100 Subject: [PATCH 03/27] Update Ch. 3 OCaml snippets --- src/content/1.3/code/ocaml/snippet01.ml | 6 +++--- src/content/1.3/code/ocaml/snippet02.ml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/content/1.3/code/ocaml/snippet01.ml b/src/content/1.3/code/ocaml/snippet01.ml index 4f8b6bd02..cd4412bba 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 + 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 3fdc58340..f61fb1a46 100644 --- a/src/content/1.3/code/ocaml/snippet02.ml +++ b/src/content/1.3/code/ocaml/snippet02.ml @@ -1,5 +1,5 @@ -module StringMonoid : Monoid = struct - type a = string +module StringMonoid : Monoid with type t = string = struct + type t = string let mempty = "" let mappend = ( ^ ) From b29fc3a6cdee34d0a244dc43caca5e38f95f4e4d Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:14:19 +0100 Subject: [PATCH 04/27] Update Ch. 4 OCaml snippets --- src/content/1.4/code/ocaml/snippet03.ml | 9 +++------ src/content/1.4/code/ocaml/snippet04.ml | 6 +++++- src/content/1.4/code/ocaml/snippet05.ml | 4 +--- src/content/1.4/code/ocaml/snippet06.ml | 8 +++++--- src/content/1.4/code/ocaml/snippet07.ml | 12 ++---------- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/content/1.4/code/ocaml/snippet03.ml b/src/content/1.4/code/ocaml/snippet03.ml index b5533bd7a..cd8ecc64d 100644 --- a/src/content/1.4/code/ocaml/snippet03.ml +++ b/src/content/1.4/code/ocaml/snippet03.ml @@ -1,7 +1,4 @@ -module type Kleisli = sig - type a - type b - type c - - val ( >=> ) : (a -> b writer) -> (b -> c writer) -> a -> c writer +module type Fish_sig = sig + val ( >=> ) : ('a -> 'b writer) -> + ('b -> 'c writer) -> 'a -> 'c writer end diff --git a/src/content/1.4/code/ocaml/snippet04.ml b/src/content/1.4/code/ocaml/snippet04.ml index d7c311f4f..519059662 100644 --- a/src/content/1.4/code/ocaml/snippet04.ml +++ b/src/content/1.4/code/ocaml/snippet04.ml @@ -1 +1,5 @@ -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 380e46a53..7a13464db 100644 --- a/src/content/1.4/code/ocaml/snippet05.ml +++ b/src/content/1.4/code/ocaml/snippet05.ml @@ -1,3 +1 @@ -let up_case : string -> string writer = - fun s -> String.uppercase s, "up_case " -;; +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 ed0f0d298..4e888f81f 100644 --- a/src/content/1.4/code/ocaml/snippet06.ml +++ b/src/content/1.4/code/ocaml/snippet06.ml @@ -1,3 +1,5 @@ -let to_words : string -> string list writer = - fun s -> String.split s ~on:' ', "to_words " -;; +let up_case (s : string) : string writer = + (String.uppercase_ascii s, "up_case ") + +let to_words (s : string) : string list writer = + (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 67a030ea3..b409e1040 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 +let process : string -> string list writer = + up_case >=> to_words From b00e4a8ed92557d3dbd1a6d363030563479fe8d1 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:14:39 +0100 Subject: [PATCH 05/27] Update Ch. 5 OCaml snippets --- src/content/1.5/code/ocaml/snippet02.ml | 2 +- src/content/1.5/code/ocaml/snippet05.ml | 4 ++-- src/content/1.5/code/ocaml/snippet10.ml | 6 +----- src/content/1.5/code/ocaml/snippet12.ml | 4 ++-- src/content/1.5/code/ocaml/snippet13.ml | 2 +- src/content/1.5/code/ocaml/snippet14.ml | 4 ++-- src/content/1.5/code/ocaml/snippet16.ml | 4 ++-- src/content/1.5/code/ocaml/snippet22.ml | 4 ++-- src/content/1.5/code/ocaml/snippet23.ml | 4 +--- src/content/1.5/code/ocaml/snippet24.ml | 2 +- src/content/1.5/code/ocaml/snippet25.ml | 5 ++--- src/content/1.5/code/ocaml/snippet26.ml | 1 - src/content/1.5/code/ocaml/snippet27.ml | 4 ++-- src/content/1.5/code/ocaml/snippet28.ml | 4 ++-- 14 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/content/1.5/code/ocaml/snippet02.ml b/src/content/1.5/code/ocaml/snippet02.ml index 638d9ceca..848f09b7c 100644 --- a/src/content/1.5/code/ocaml/snippet02.ml +++ b/src/content/1.5/code/ocaml/snippet02.ml @@ -1 +1 @@ -let unit x = () +let unit _ = () diff --git a/src/content/1.5/code/ocaml/snippet05.ml b/src/content/1.5/code/ocaml/snippet05.ml index 333a2030e..01109aa1e 100644 --- a/src/content/1.5/code/ocaml/snippet05.ml +++ b/src/content/1.5/code/ocaml/snippet05.ml @@ -1,2 +1,2 @@ -compose f g = id -compose g f = id +Fun.compose f g = Fun.id +Fun.compose g f = Fun.id diff --git a/src/content/1.5/code/ocaml/snippet10.ml b/src/content/1.5/code/ocaml/snippet10.ml index d6bcc864e..0886c0fc4 100644 --- a/src/content/1.5/code/ocaml/snippet10.ml +++ b/src/content/1.5/code/ocaml/snippet10.ml @@ -1,8 +1,4 @@ -module Chapter5_Product_Example : - Chapter5_Product - with type a = int - and type b = bool - and type c = int = struct +module Chapter5_Product_Example : Chapter5_Product = struct type a = int type b = bool type c = int diff --git a/src/content/1.5/code/ocaml/snippet12.ml b/src/content/1.5/code/ocaml/snippet12.ml index e6f2e5ac2..9f6be907b 100644 --- a/src/content/1.5/code/ocaml/snippet12.ml +++ b/src/content/1.5/code/ocaml/snippet12.ml @@ -1,2 +1,2 @@ -let p' = compose Chapter5_Product_Example.p m -let q' = compose Chapter5_Product_Example.q m +let p' = Fun.compose Chapter5_Product_Example.p m +let q' = Fun.compose Chapter5_Product_Example.q m diff --git a/src/content/1.5/code/ocaml/snippet13.ml b/src/content/1.5/code/ocaml/snippet13.ml index 0f96438fa..e223cc89e 100644 --- a/src/content/1.5/code/ocaml/snippet13.ml +++ b/src/content/1.5/code/ocaml/snippet13.ml @@ -1 +1 @@ -let m (x : int) = x, true +let m (x : int) : int * bool = x, true diff --git a/src/content/1.5/code/ocaml/snippet14.ml b/src/content/1.5/code/ocaml/snippet14.ml index 0d8b34ff0..a581bf422 100644 --- a/src/content/1.5/code/ocaml/snippet14.ml +++ b/src/content/1.5/code/ocaml/snippet14.ml @@ -1,2 +1,2 @@ -let p x = fst (m x) -let q x = snd (m x) +let p x = fst (m x) (* = x *) +let q x = snd (m x) (* = true *) diff --git a/src/content/1.5/code/ocaml/snippet16.ml b/src/content/1.5/code/ocaml/snippet16.ml index f2729c26a..3d1b90f7f 100644 --- a/src/content/1.5/code/ocaml/snippet16.ml +++ b/src/content/1.5/code/ocaml/snippet16.ml @@ -1,2 +1,2 @@ -fst = compose p m' -snd = compose q m' +let fst = Fun.compose p m' +let snd = Fun.compose q m' diff --git a/src/content/1.5/code/ocaml/snippet22.ml b/src/content/1.5/code/ocaml/snippet22.ml index 927fb6a3f..4591b609d 100644 --- a/src/content/1.5/code/ocaml/snippet22.ml +++ b/src/content/1.5/code/ocaml/snippet22.ml @@ -1,2 +1,2 @@ -i' == compose m i -j' == compose m j +let i' = Fun.compose m i +let j' = Fun.compose m j diff --git a/src/content/1.5/code/ocaml/snippet23.ml b/src/content/1.5/code/ocaml/snippet23.ml index e3f11cba0..2ae0e6bd4 100644 --- a/src/content/1.5/code/ocaml/snippet23.ml +++ b/src/content/1.5/code/ocaml/snippet23.ml @@ -1,3 +1 @@ -type contact = - | PhoneNum of int - | EmailAddr of string +type contact = PhoneNum of int | EmailAddr of string diff --git a/src/content/1.5/code/ocaml/snippet24.ml b/src/content/1.5/code/ocaml/snippet24.ml index d99490ec0..4407bc664 100644 --- a/src/content/1.5/code/ocaml/snippet24.ml +++ b/src/content/1.5/code/ocaml/snippet24.ml @@ -1 +1 @@ -let helpdesk = PhoneNum 2222222 +let helpdesk : contact = PhoneNum 2222222 diff --git a/src/content/1.5/code/ocaml/snippet25.ml b/src/content/1.5/code/ocaml/snippet25.ml index 0c0aff60b..e500249de 100644 --- a/src/content/1.5/code/ocaml/snippet25.ml +++ b/src/content/1.5/code/ocaml/snippet25.ml @@ -1,3 +1,2 @@ -type ('a, 'b) either = - | Left of 'a - | Right of 'b +(* Available in the [Either] module of the Stdlib *) +type ('a, 'b) either = Left of 'a | Right of 'b diff --git a/src/content/1.5/code/ocaml/snippet26.ml b/src/content/1.5/code/ocaml/snippet26.ml index 725e2a29d..7da5a78a8 100644 --- a/src/content/1.5/code/ocaml/snippet26.ml +++ b/src/content/1.5/code/ocaml/snippet26.ml @@ -1,4 +1,3 @@ let factorizer i j = function | Left a -> i a | Right b -> j b -;; diff --git a/src/content/1.5/code/ocaml/snippet27.ml b/src/content/1.5/code/ocaml/snippet27.ml index 5af299b5c..f5d73860b 100644 --- a/src/content/1.5/code/ocaml/snippet27.ml +++ b/src/content/1.5/code/ocaml/snippet27.ml @@ -1,2 +1,2 @@ -p = compose fst m -q = compose snd m +let p = Fun.compose fst m +let q = Fun.compose snd m diff --git a/src/content/1.5/code/ocaml/snippet28.ml b/src/content/1.5/code/ocaml/snippet28.ml index e44ffb71a..4e5b7ddf7 100644 --- a/src/content/1.5/code/ocaml/snippet28.ml +++ b/src/content/1.5/code/ocaml/snippet28.ml @@ -1,2 +1,2 @@ -p () = fst (m ()) -q () = snd (m ()) +let p () = fst (m ()) +let q () = snd (m ()) From a6a55fd2ce4e42fb3c31dfb0306a85b9c8b65936 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:16:21 +0100 Subject: [PATCH 06/27] Update Ch. 6 OCaml snippets --- src/content/1.6/code/ocaml/snippet12.ml | 3 ++- src/content/1.6/code/ocaml/snippet14.ml | 3 +-- src/content/1.6/code/ocaml/snippet16.ml | 1 - src/content/1.6/code/ocaml/snippet17.ml | 1 - src/content/1.6/code/ocaml/snippet19.ml | 3 +-- src/content/1.6/code/ocaml/snippet20.ml | 2 +- src/content/1.6/code/ocaml/snippet21.ml | 5 ++--- src/content/1.6/code/ocaml/snippet23.ml | 2 +- src/content/1.6/code/ocaml/snippet24.ml | 5 +---- src/content/1.6/code/ocaml/snippet25.ml | 4 +--- src/content/1.6/code/ocaml/snippet26.ml | 5 ++--- src/content/1.6/code/ocaml/snippet27.ml | 2 +- src/content/1.6/code/ocaml/snippet28.ml | 2 +- src/content/1.6/code/ocaml/snippet29.ml | 2 +- src/content/1.6/code/ocaml/snippet30.ml | 4 +--- src/content/1.6/code/ocaml/snippet31.ml | 11 +++-------- src/content/1.6/code/ocaml/snippet34.ml | 1 - src/content/1.6/code/ocaml/snippet35.ml | 1 - src/content/1.6/code/ocaml/snippet37.ml | 4 +--- 19 files changed, 20 insertions(+), 41 deletions(-) diff --git a/src/content/1.6/code/ocaml/snippet12.ml b/src/content/1.6/code/ocaml/snippet12.ml index 8da12cccb..1853f9e54 100644 --- a/src/content/1.6/code/ocaml/snippet12.ml +++ b/src/content/1.6/code/ocaml/snippet12.ml @@ -1 +1,2 @@ -let stmt = "This statement is", false +(* You cannot do that in OCaml *) +let stmt = ("This statement is", false) diff --git a/src/content/1.6/code/ocaml/snippet14.ml b/src/content/1.6/code/ocaml/snippet14.ml index 9633b1357..5d9ad5633 100644 --- a/src/content/1.6/code/ocaml/snippet14.ml +++ b/src/content/1.6/code/ocaml/snippet14.ml @@ -1,3 +1,2 @@ let starts_with_symbol (name, symbol, _) = - String.is_prefix name ~prefix:symbol -;; + String.starts_with ~prefix:symbol name diff --git a/src/content/1.6/code/ocaml/snippet16.ml b/src/content/1.6/code/ocaml/snippet16.ml index f6a3f35ca..53a75b9d1 100644 --- a/src/content/1.6/code/ocaml/snippet16.ml +++ b/src/content/1.6/code/ocaml/snippet16.ml @@ -1,3 +1,2 @@ let tuple_to_elem (name, symbol, atomic_number) = { name; symbol; atomic_number } -;; diff --git a/src/content/1.6/code/ocaml/snippet17.ml b/src/content/1.6/code/ocaml/snippet17.ml index 39838dedd..859db466f 100644 --- a/src/content/1.6/code/ocaml/snippet17.ml +++ b/src/content/1.6/code/ocaml/snippet17.ml @@ -1,3 +1,2 @@ let elem_to_tuple { name; symbol; atomic_number } = name, symbol, atomic_number -;; diff --git a/src/content/1.6/code/ocaml/snippet19.ml b/src/content/1.6/code/ocaml/snippet19.ml index 0e34acb2d..dd7c7ba69 100644 --- a/src/content/1.6/code/ocaml/snippet19.ml +++ b/src/content/1.6/code/ocaml/snippet19.ml @@ -1,3 +1,2 @@ let starts_with_symbol { name; symbol; _ } = - String.is_prefix name ~prefix:symbol -;; + String.starts_with ~prefix:symbol name diff --git a/src/content/1.6/code/ocaml/snippet20.ml b/src/content/1.6/code/ocaml/snippet20.ml index 18025f3a5..592959cdb 100644 --- a/src/content/1.6/code/ocaml/snippet20.ml +++ b/src/content/1.6/code/ocaml/snippet20.ml @@ -1,2 +1,2 @@ (* OCaml only allows special characters in the infix operator. - So, the above function name cannot be applied be infix. *) + So, the above function name cannot be made infix. *) diff --git a/src/content/1.6/code/ocaml/snippet21.ml b/src/content/1.6/code/ocaml/snippet21.ml index 0c0aff60b..e500249de 100644 --- a/src/content/1.6/code/ocaml/snippet21.ml +++ b/src/content/1.6/code/ocaml/snippet21.ml @@ -1,3 +1,2 @@ -type ('a, 'b) either = - | Left of 'a - | Right of 'b +(* Available in the [Either] module of the Stdlib *) +type ('a, 'b) either = Left of 'a | Right of 'b diff --git a/src/content/1.6/code/ocaml/snippet23.ml b/src/content/1.6/code/ocaml/snippet23.ml index 36b070f00..4b3a31e83 100644 --- a/src/content/1.6/code/ocaml/snippet23.ml +++ b/src/content/1.6/code/ocaml/snippet23.ml @@ -1 +1 @@ -'a void either +('a, void) Either.t diff --git a/src/content/1.6/code/ocaml/snippet24.ml b/src/content/1.6/code/ocaml/snippet24.ml index 8c0c65f46..1684e4519 100644 --- a/src/content/1.6/code/ocaml/snippet24.ml +++ b/src/content/1.6/code/ocaml/snippet24.ml @@ -1,4 +1 @@ -type color = - | Red - | Green - | Blue +type color = Red | Green | Blue diff --git a/src/content/1.6/code/ocaml/snippet25.ml b/src/content/1.6/code/ocaml/snippet25.ml index 1e1b07644..31950d476 100644 --- a/src/content/1.6/code/ocaml/snippet25.ml +++ b/src/content/1.6/code/ocaml/snippet25.ml @@ -1,3 +1 @@ -type bool = - | True - | False +type bool = True | False diff --git a/src/content/1.6/code/ocaml/snippet26.ml b/src/content/1.6/code/ocaml/snippet26.ml index b285a7dfe..99327aa3d 100644 --- a/src/content/1.6/code/ocaml/snippet26.ml +++ b/src/content/1.6/code/ocaml/snippet26.ml @@ -1,3 +1,2 @@ -type 'a maybe = - | Nothing - | Just of 'a +(* In OCaml, this is called option *) +type 'a option = None | Some of 'a diff --git a/src/content/1.6/code/ocaml/snippet27.ml b/src/content/1.6/code/ocaml/snippet27.ml index 6c77da862..eb2bb7dd0 100644 --- a/src/content/1.6/code/ocaml/snippet27.ml +++ b/src/content/1.6/code/ocaml/snippet27.ml @@ -1 +1 @@ -type nothing_type = Nothing +type none_type = None diff --git a/src/content/1.6/code/ocaml/snippet28.ml b/src/content/1.6/code/ocaml/snippet28.ml index 84fa905c3..0d2daf166 100644 --- a/src/content/1.6/code/ocaml/snippet28.ml +++ b/src/content/1.6/code/ocaml/snippet28.ml @@ -1 +1 @@ -type 'a just_type = Just of 'a +type 'a some_type = Some of 'a diff --git a/src/content/1.6/code/ocaml/snippet29.ml b/src/content/1.6/code/ocaml/snippet29.ml index 765342f51..a31a5879e 100644 --- a/src/content/1.6/code/ocaml/snippet29.ml +++ b/src/content/1.6/code/ocaml/snippet29.ml @@ -1 +1 @@ -type 'a maybe = (unit, 'a) either +type 'a option = (unit, 'a) Either.t diff --git a/src/content/1.6/code/ocaml/snippet30.ml b/src/content/1.6/code/ocaml/snippet30.ml index bf428a2a6..f6496a64a 100644 --- a/src/content/1.6/code/ocaml/snippet30.ml +++ b/src/content/1.6/code/ocaml/snippet30.ml @@ -1,3 +1 @@ -type 'a list = - | Nil - | Cons of 'a * 'a list +type 'a list = Nil | Cons of 'a * 'a list diff --git a/src/content/1.6/code/ocaml/snippet31.ml b/src/content/1.6/code/ocaml/snippet31.ml index f8b5575be..3f3b016ec 100644 --- a/src/content/1.6/code/ocaml/snippet31.ml +++ b/src/content/1.6/code/ocaml/snippet31.ml @@ -1,8 +1,3 @@ -type 'a maybe = - | Nothing - | Just of 'a - -let maybe_tail = function - | Nil -> Nothing - | Cons (_, xs) -> Just xs -;; +let tail_option = function + | Nil -> None + | Cons (_, xs) -> Some xs diff --git a/src/content/1.6/code/ocaml/snippet34.ml b/src/content/1.6/code/ocaml/snippet34.ml index 6799f1660..03b35a596 100644 --- a/src/content/1.6/code/ocaml/snippet34.ml +++ b/src/content/1.6/code/ocaml/snippet34.ml @@ -2,4 +2,3 @@ let prod_to_sum (x, e) = match e with | Left y -> Left (x, y) | Right z -> Right (x, z) -;; diff --git a/src/content/1.6/code/ocaml/snippet35.ml b/src/content/1.6/code/ocaml/snippet35.ml index ab0d54827..9a3f62d6d 100644 --- a/src/content/1.6/code/ocaml/snippet35.ml +++ b/src/content/1.6/code/ocaml/snippet35.ml @@ -1,4 +1,3 @@ let sum_to_prod = function | Left (x, y) -> x, Left y | Right (x, z) -> x, Right z -;; diff --git a/src/content/1.6/code/ocaml/snippet37.ml b/src/content/1.6/code/ocaml/snippet37.ml index bf428a2a6..f6496a64a 100644 --- a/src/content/1.6/code/ocaml/snippet37.ml +++ b/src/content/1.6/code/ocaml/snippet37.ml @@ -1,3 +1 @@ -type 'a list = - | Nil - | Cons of 'a * 'a list +type 'a list = Nil | Cons of 'a * 'a list From c2f09b4221ffe9fb9597a99e04aed436c5df0de0 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:16:42 +0100 Subject: [PATCH 07/27] Update Ch. 7 OCaml snippets --- src/content/1.7/code/ocaml/snippet01.ml | 4 +--- src/content/1.7/code/ocaml/snippet03.ml | 1 - src/content/1.7/code/ocaml/snippet04.ml | 2 +- src/content/1.7/code/ocaml/snippet06.ml | 1 - src/content/1.7/code/ocaml/snippet09.ml | 6 ++---- src/content/1.7/code/ocaml/snippet10.ml | 2 ++ src/content/1.7/code/ocaml/snippet12.ml | 3 +-- src/content/1.7/code/ocaml/snippet14.ml | 1 - src/content/1.7/code/ocaml/snippet15.ml | 4 +--- src/content/1.7/code/ocaml/snippet17.ml | 1 - src/content/1.7/code/ocaml/snippet18.ml | 1 - src/content/1.7/code/ocaml/snippet22.ml | 2 +- src/content/1.7/code/ocaml/snippet23.ml | 2 +- src/content/1.7/code/ocaml/snippet24.ml | 2 +- src/content/1.7/code/ocaml/snippet25.ml | 4 +++- src/content/1.7/code/ocaml/snippet28.ml | 2 +- src/content/1.7/code/ocaml/snippet29.ml | 3 +-- src/content/1.7/code/ocaml/snippet30.ml | 4 +++- src/content/1.7/code/ocaml/snippet31.ml | 4 ++-- 19 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/content/1.7/code/ocaml/snippet01.ml b/src/content/1.7/code/ocaml/snippet01.ml index e30a56a0c..d6813a040 100644 --- a/src/content/1.7/code/ocaml/snippet01.ml +++ b/src/content/1.7/code/ocaml/snippet01.ml @@ -1,3 +1 @@ -type 'a option = - | None - | Some of 'a +type 'a option = None | Some of 'a diff --git a/src/content/1.7/code/ocaml/snippet03.ml b/src/content/1.7/code/ocaml/snippet03.ml index 8103489db..afa714286 100644 --- a/src/content/1.7/code/ocaml/snippet03.ml +++ b/src/content/1.7/code/ocaml/snippet03.ml @@ -1,4 +1,3 @@ let f' f = function | None -> None | Some x -> Some (f x) -;; diff --git a/src/content/1.7/code/ocaml/snippet04.ml b/src/content/1.7/code/ocaml/snippet04.ml index d6e711ee1..82bb813aa 100644 --- a/src/content/1.7/code/ocaml/snippet04.ml +++ b/src/content/1.7/code/ocaml/snippet04.ml @@ -2,5 +2,5 @@ module type Maybe_Functor = sig type a type b - val fmap : (a -> b) -> a option -> b option + val fmap : (a -> b) -> (a option -> b option) end diff --git a/src/content/1.7/code/ocaml/snippet06.ml b/src/content/1.7/code/ocaml/snippet06.ml index 8137ae731..5ea722f76 100644 --- a/src/content/1.7/code/ocaml/snippet06.ml +++ b/src/content/1.7/code/ocaml/snippet06.ml @@ -1,4 +1,3 @@ let fmap f = function | None -> None | Some x -> Some (f x) -;; diff --git a/src/content/1.7/code/ocaml/snippet09.ml b/src/content/1.7/code/ocaml/snippet09.ml index d543d6c0b..5e5383e2e 100644 --- a/src/content/1.7/code/ocaml/snippet09.ml +++ b/src/content/1.7/code/ocaml/snippet09.ml @@ -1,10 +1,8 @@ module Test_Functor_Compose (F : Functor) = struct open F - (* Compose *) - let ( <.> ) f g x = f (g x) + let ( % ) = Fun.compose let test_compose f g x = - assert (fmap (f <.> g) x = fmap f (fmap g x)) - ;; + assert (fmap (f % g) x = fmap f (fmap g x)) end diff --git a/src/content/1.7/code/ocaml/snippet10.ml b/src/content/1.7/code/ocaml/snippet10.ml index d74faf41f..b52712e48 100644 --- a/src/content/1.7/code/ocaml/snippet10.ml +++ b/src/content/1.7/code/ocaml/snippet10.ml @@ -1,3 +1,5 @@ +(* OCaml does not have typeclasses, but we can simulate + them with modules. *) module type Eq = sig type a diff --git a/src/content/1.7/code/ocaml/snippet12.ml b/src/content/1.7/code/ocaml/snippet12.ml index 9e98baf99..e481d4bc0 100644 --- a/src/content/1.7/code/ocaml/snippet12.ml +++ b/src/content/1.7/code/ocaml/snippet12.ml @@ -1,7 +1,6 @@ -module Point_Eq (E : Eq with type a = float) = struct +module Point_Eq (E : Eq with type a = int) = struct type a = point let ( == ) (Pt (p1x, p1y)) (Pt (p2x, p2y)) = E.(p1x == p2x) && E.(p2x == p2y) - ;; end diff --git a/src/content/1.7/code/ocaml/snippet14.ml b/src/content/1.7/code/ocaml/snippet14.ml index dbd72adb1..60cb81d37 100644 --- a/src/content/1.7/code/ocaml/snippet14.ml +++ b/src/content/1.7/code/ocaml/snippet14.ml @@ -4,5 +4,4 @@ module Option_Functor : Functor with type 'a t = 'a option = struct let fmap f = function | None -> None | Some x -> Some (f x) - ;; end diff --git a/src/content/1.7/code/ocaml/snippet15.ml b/src/content/1.7/code/ocaml/snippet15.ml index bf428a2a6..f6496a64a 100644 --- a/src/content/1.7/code/ocaml/snippet15.ml +++ b/src/content/1.7/code/ocaml/snippet15.ml @@ -1,3 +1 @@ -type 'a list = - | Nil - | Cons of 'a * 'a list +type 'a list = Nil | Cons of 'a * 'a list diff --git a/src/content/1.7/code/ocaml/snippet17.ml b/src/content/1.7/code/ocaml/snippet17.ml index 726198743..9dec73be0 100644 --- a/src/content/1.7/code/ocaml/snippet17.ml +++ b/src/content/1.7/code/ocaml/snippet17.ml @@ -1,4 +1,3 @@ let rec fmap f = function | Nil -> Nil | Cons (x, xs) -> Cons (f x, fmap f xs) -;; diff --git a/src/content/1.7/code/ocaml/snippet18.ml b/src/content/1.7/code/ocaml/snippet18.ml index c4169c4c9..466df864e 100644 --- a/src/content/1.7/code/ocaml/snippet18.ml +++ b/src/content/1.7/code/ocaml/snippet18.ml @@ -4,5 +4,4 @@ module List_Functor : Functor with type 'a t = 'a list = struct let rec fmap f = function | Nil -> Nil | Cons (x, xs) -> Cons (f x, fmap f xs) - ;; end diff --git a/src/content/1.7/code/ocaml/snippet22.ml b/src/content/1.7/code/ocaml/snippet22.ml index b0d5cf604..09aa41e35 100644 --- a/src/content/1.7/code/ocaml/snippet22.ml +++ b/src/content/1.7/code/ocaml/snippet22.ml @@ -1,4 +1,4 @@ -module Reader_Functor (T : T) : Functor = struct +module Reader_Functor (T : T) : Functor with type 'a t = T.t -> 'a = struct type 'a t = T.t -> 'a let fmap f ra r = f (ra r) diff --git a/src/content/1.7/code/ocaml/snippet23.ml b/src/content/1.7/code/ocaml/snippet23.ml index f69a4e125..5940a2d0c 100644 --- a/src/content/1.7/code/ocaml/snippet23.ml +++ b/src/content/1.7/code/ocaml/snippet23.ml @@ -1 +1 @@ -let fmap f g = compose f g +let fmap f g = Fun.compose f g diff --git a/src/content/1.7/code/ocaml/snippet24.ml b/src/content/1.7/code/ocaml/snippet24.ml index 9b997d1c9..80c91733b 100644 --- a/src/content/1.7/code/ocaml/snippet24.ml +++ b/src/content/1.7/code/ocaml/snippet24.ml @@ -1 +1 @@ -let fmap : ('a -> 'b) -> ('r -> 'a) -> 'r -> 'b = compose +let fmap = Fun.compose diff --git a/src/content/1.7/code/ocaml/snippet25.ml b/src/content/1.7/code/ocaml/snippet25.ml index 8b1f1ab6c..e329469f8 100644 --- a/src/content/1.7/code/ocaml/snippet25.ml +++ b/src/content/1.7/code/ocaml/snippet25.ml @@ -1 +1,3 @@ -let nats = Caml.Stream.from (fun i -> Some (i + 1)) +(* For lazy/infinite lists like in Haskell, + we can use the [Seq] module. *) +let nats = Seq.ints 1 (* Equivalent to [1..] in Haskell *) diff --git a/src/content/1.7/code/ocaml/snippet28.ml b/src/content/1.7/code/ocaml/snippet28.ml index d3ba51fb8..d12a8eec3 100644 --- a/src/content/1.7/code/ocaml/snippet28.ml +++ b/src/content/1.7/code/ocaml/snippet28.ml @@ -1,5 +1,5 @@ module Const_Functor (T : T) : Functor = struct type 'a t = (T.t, 'a) const - let fmap f (Const c) = Const c (* or even let fmap _ c = c *) + let fmap _ (Const c) = Const c end diff --git a/src/content/1.7/code/ocaml/snippet29.ml b/src/content/1.7/code/ocaml/snippet29.ml index 956a98a86..7796c06a0 100644 --- a/src/content/1.7/code/ocaml/snippet29.ml +++ b/src/content/1.7/code/ocaml/snippet29.ml @@ -1,4 +1,3 @@ -let maybe_tail = function +let tail_option = function | [] -> None | _ :: xs -> Some xs -;; diff --git a/src/content/1.7/code/ocaml/snippet30.ml b/src/content/1.7/code/ocaml/snippet30.ml index 8c4ac188b..60b844a6f 100644 --- a/src/content/1.7/code/ocaml/snippet30.ml +++ b/src/content/1.7/code/ocaml/snippet30.ml @@ -1,3 +1,5 @@ let square x = x * x -let mis = Some (Cons (1, Cons (2, Cons (3, Nil)))) + +let mis : int list option = Some [ 1; 2; 3 ] + let mis2 = Option_Functor.fmap (List_Functor.fmap square) mis diff --git a/src/content/1.7/code/ocaml/snippet31.ml b/src/content/1.7/code/ocaml/snippet31.ml index bf03da028..0bb4b574a 100644 --- a/src/content/1.7/code/ocaml/snippet31.ml +++ b/src/content/1.7/code/ocaml/snippet31.ml @@ -1,4 +1,4 @@ let fmapO = Option_Functor.fmap let fmapL = List_Functor.fmap -let fmapC f l = (compose fmapO fmapL) f l -let mis2 = fmapC square mis + +let mis2 = (fmapO % fmapL) square mis From db3f2cbbee0170a3f159436b5e2dbbab978e22b3 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:17:08 +0100 Subject: [PATCH 08/27] Update Ch. 8 OCaml snippets --- src/content/1.8/code/ocaml/snippet01.ml | 34 +++++++++++-------- src/content/1.8/code/ocaml/snippet02.ml | 6 ++-- src/content/1.8/code/ocaml/snippet04.ml | 11 +++---- src/content/1.8/code/ocaml/snippet06.ml | 2 +- src/content/1.8/code/ocaml/snippet07.ml | 4 +-- src/content/1.8/code/ocaml/snippet08.ml | 7 +--- src/content/1.8/code/ocaml/snippet09.ml | 18 +++-------- src/content/1.8/code/ocaml/snippet10.ml | 14 +++----- src/content/1.8/code/ocaml/snippet11.ml | 2 +- src/content/1.8/code/ocaml/snippet12.ml | 4 +-- src/content/1.8/code/ocaml/snippet13.ml | 6 +++- src/content/1.8/code/ocaml/snippet14.ml | 5 ++- src/content/1.8/code/ocaml/snippet15.ml | 3 +- src/content/1.8/code/ocaml/snippet17.ml | 9 ++---- src/content/1.8/code/ocaml/snippet18.ml | 2 +- src/content/1.8/code/ocaml/snippet19.ml | 10 +++--- src/content/1.8/code/ocaml/snippet22.ml | 4 +-- src/content/1.8/code/ocaml/snippet24.ml | 2 +- src/content/1.8/code/ocaml/snippet26.ml | 4 +-- src/content/1.8/code/ocaml/snippet27.ml | 3 +- src/content/1.8/code/ocaml/snippet28.ml | 5 ++- src/content/1.8/code/ocaml/snippet29.ml | 43 ++++++++++++++----------- src/content/1.8/code/ocaml/snippet30.ml | 18 +++++------ 23 files changed, 103 insertions(+), 113 deletions(-) diff --git a/src/content/1.8/code/ocaml/snippet01.ml b/src/content/1.8/code/ocaml/snippet01.ml index b2ab138a5..f94d213f9 100644 --- a/src/content/1.8/code/ocaml/snippet01.ml +++ b/src/content/1.8/code/ocaml/snippet01.ml @@ -1,29 +1,37 @@ -(** You can represent bifunctor defintion in two forms and implement - just and derive the other from it. *) -module type BifunctorCore = sig +module type Bifunctor = sig type ('a, 'b) t val bimap : ('a -> 'c) -> ('b -> 'd) -> ('a, 'b) t -> ('c, 'd) t + val first : ('a -> 'c) -> ('a, 'b) t -> ('c, 'b) t + val second : ('b -> 'd) -> ('a, 'b) t -> ('a, 'd) t end -module type BifunctorExt = sig +(** You can represent a bifunctor in one of the two forms and + derive one from the other. *) +module type Bimap = sig + type ('a, 'b) t + + val bimap : ('a -> 'c) -> ('b -> 'd) -> ('a, 'b) t -> ('c, 'd) t +end + +module type Fmaps = sig type ('a, 'b) t val first : ('a -> 'c) -> ('a, 'b) t -> ('c, 'b) t val second : ('b -> 'd) -> ('a, 'b) t -> ('a, 'd) t end -module BifunctorCore_Using_Ext (M : BifunctorExt) : BifunctorCore = -struct - type ('a, 'b) t = ('a, 'b) M.t +module Bifunctor_From_Bimap (M : Bimap) : + Bifunctor with type ('a, 'b) t = ('a, 'b) M.t = struct + include M - let bimap g h x = M.first g (M.second h x) + let first g x = M.bimap g Fun.id x + let second h x = M.bimap Fun.id h x end -module BifunctorExt_Using_Core (M : BifunctorCore) : BifunctorExt = -struct - type ('a, 'b) t = ('a, 'b) M.t +module Bifunctor_From_Fmaps (M : Fmaps) : + Bifunctor with type ('a, 'b) t = ('a, 'b) M.t = struct + include M - let first g x = M.bimap g id x - let second h x = M.bimap id h x + let bimap g h x = M.first g (M.second h x) end diff --git a/src/content/1.8/code/ocaml/snippet02.ml b/src/content/1.8/code/ocaml/snippet02.ml index 1329dbb2e..cc953568f 100644 --- a/src/content/1.8/code/ocaml/snippet02.ml +++ b/src/content/1.8/code/ocaml/snippet02.ml @@ -1,5 +1,5 @@ -module Bifunctor_Product : BifunctorCore = struct +module Bifunctor_Product = Bifunctor_From_Bimap (struct type ('a, 'b) t = 'a * 'b - let bimap f g (l, r) = f l, g r -end + let bimap f g (l, r) = (f l, g r) +end) diff --git a/src/content/1.8/code/ocaml/snippet04.ml b/src/content/1.8/code/ocaml/snippet04.ml index 442cf6d8b..0c92a9c87 100644 --- a/src/content/1.8/code/ocaml/snippet04.ml +++ b/src/content/1.8/code/ocaml/snippet04.ml @@ -1,12 +1,9 @@ -type ('a, 'b) either = - | Left of 'a - | Right of 'b +module Bifunctor_Either = Bifunctor_From_Bimap (struct + open Either -module Bifunctor_Either : BifunctorCore = struct - type ('a, 'b) t = ('a, 'b) either + type ('a, 'b) t = ('a, 'b) Either.t let bimap f g = function | Left a -> Left (f a) | Right b -> Right (g b) - ;; -end +end) diff --git a/src/content/1.8/code/ocaml/snippet06.ml b/src/content/1.8/code/ocaml/snippet06.ml index a97203ae4..eddd18848 100644 --- a/src/content/1.8/code/ocaml/snippet06.ml +++ b/src/content/1.8/code/ocaml/snippet06.ml @@ -1,4 +1,4 @@ -module Identity_Functor : Functor = struct +module Identity_Functor : Functor with type 'a t = 'a id = struct type 'a t = 'a id let fmap f (Id a) = Id (f a) diff --git a/src/content/1.8/code/ocaml/snippet07.ml b/src/content/1.8/code/ocaml/snippet07.ml index e30a56a0c..d6813a040 100644 --- a/src/content/1.8/code/ocaml/snippet07.ml +++ b/src/content/1.8/code/ocaml/snippet07.ml @@ -1,3 +1 @@ -type 'a option = - | None - | Some of 'a +type 'a option = None | Some of 'a diff --git a/src/content/1.8/code/ocaml/snippet08.ml b/src/content/1.8/code/ocaml/snippet08.ml index ba64ba6f3..f275da370 100644 --- a/src/content/1.8/code/ocaml/snippet08.ml +++ b/src/content/1.8/code/ocaml/snippet08.ml @@ -1,9 +1,4 @@ (** OCaml doesn't have a built in Const type *) type ('a, 'b) const = Const of 'a -(** OCaml doesn't have a built in either type *) -type ('a, 'b) either = - | Left of 'a - | Right of 'b (** Either type *) - -type 'a option = ((unit, 'a) const, 'a id) either +type 'a option = ((unit, 'a) const, 'a id) Either.t diff --git a/src/content/1.8/code/ocaml/snippet09.ml b/src/content/1.8/code/ocaml/snippet09.ml index e07299686..c84901270 100644 --- a/src/content/1.8/code/ocaml/snippet09.ml +++ b/src/content/1.8/code/ocaml/snippet09.ml @@ -2,16 +2,8 @@ module functors to emulate the behavior higher kinded types. There's less verbose options using type defunctionalization but it's more advanced and obscures the flow of this book *) -module type BiComp = functor - (BF : sig - type ('a, 'b) t - end) - (FU : sig - type 'a t - end) - (GU : sig - type 'b t - end) - -> sig - type ('a, 'b) bicomp = BiComp of ('a FU.t, 'b GU.t) BF.t -end +module type BiCompBifunctor = functor + (BF : Bifunctor) + (FU : Functor) + (GU : Functor) + -> Bifunctor diff --git a/src/content/1.8/code/ocaml/snippet10.ml b/src/content/1.8/code/ocaml/snippet10.ml index 5029cdca4..775692406 100644 --- a/src/content/1.8/code/ocaml/snippet10.ml +++ b/src/content/1.8/code/ocaml/snippet10.ml @@ -1,10 +1,6 @@ -module BiCompBifunctor - (BF : BifunctorCore) - (FU : Functor) - (GU : Functor) : BifunctorCore = struct - type ('a, 'b) t = BiComp of ('a FU.t, 'b GU.t) BF.t +module BiCompBifunctor (BF : Bifunctor) (FU : Functor) (GU : Functor) = +Bifunctor_From_Bimap (struct + type ('a, 'b) t = ('a FU.t, 'b GU.t) BF.t - let bimap f g (BiComp x) = - BiComp (BF.bimap (FU.fmap f) (GU.fmap g) x) - ;; -end + let bimap f g x = BF.bimap (FU.fmap f) (GU.fmap g) x +end) diff --git a/src/content/1.8/code/ocaml/snippet11.ml b/src/content/1.8/code/ocaml/snippet11.ml index cdfa3789e..a90aa60d4 100644 --- a/src/content/1.8/code/ocaml/snippet11.ml +++ b/src/content/1.8/code/ocaml/snippet11.ml @@ -1 +1 @@ -type ('a FU.t, 'b GU.t) BF.t +('a FU.t, 'b GU.t) BF.t diff --git a/src/content/1.8/code/ocaml/snippet12.ml b/src/content/1.8/code/ocaml/snippet12.ml index 820a64372..c1c22323c 100644 --- a/src/content/1.8/code/ocaml/snippet12.ml +++ b/src/content/1.8/code/ocaml/snippet12.ml @@ -1,2 +1,2 @@ -val f1 : a -> a' -val f2 : b -> b' +val f1 : 'a -> 'a_prime +val f2 : 'b -> 'b_prime diff --git a/src/content/1.8/code/ocaml/snippet13.ml b/src/content/1.8/code/ocaml/snippet13.ml index 9b02938ca..e3aadc886 100644 --- a/src/content/1.8/code/ocaml/snippet13.ml +++ b/src/content/1.8/code/ocaml/snippet13.ml @@ -1 +1,5 @@ -val bimap : (a FU.t -> a' FU.t) -> (b GU.t -> b' GU.t) -> (a FU.t, b GU.t) t -> (a' FU.t, b' GU.t) t +(* Here 'a and 'b are (Haskell-like) functorial types due + to the application of the FU and GU OCaml modules *) +val bimap : + ('a -> 'a_prime) -> ('b -> 'b_prime) -> + ('a, 'b) t -> ('a_prime, 'b_prime) t diff --git a/src/content/1.8/code/ocaml/snippet14.ml b/src/content/1.8/code/ocaml/snippet14.ml index 0667b9224..6bc00a4ff 100644 --- a/src/content/1.8/code/ocaml/snippet14.ml +++ b/src/content/1.8/code/ocaml/snippet14.ml @@ -1,6 +1,5 @@ -(** Deriving a functor in OCaml is not available as a language - extension. You could try experimental library like ocsigen - to derive functors.*) +(** Deriving typeclasses is not available as a language feature + in OCaml. You could try ppxs to achieve similar results. *) type 'a tree = | Leaf of 'a | Node of 'a tree * 'a tree diff --git a/src/content/1.8/code/ocaml/snippet15.ml b/src/content/1.8/code/ocaml/snippet15.ml index 3f37dd43a..b564bd13e 100644 --- a/src/content/1.8/code/ocaml/snippet15.ml +++ b/src/content/1.8/code/ocaml/snippet15.ml @@ -1,8 +1,7 @@ -module TreeFunctor : Functor = struct +module TreeFunctor : Functor with type 'a t = 'a tree = struct type 'a t = 'a tree let rec fmap f = function | Leaf a -> Leaf (f a) | Node (l, r) -> Node (fmap f l, fmap f r) - ;; end diff --git a/src/content/1.8/code/ocaml/snippet17.ml b/src/content/1.8/code/ocaml/snippet17.ml index 0c2bae22a..97520d429 100644 --- a/src/content/1.8/code/ocaml/snippet17.ml +++ b/src/content/1.8/code/ocaml/snippet17.ml @@ -1,10 +1,7 @@ module KleisliComposition = struct - let ( >=> ) - : ('a -> 'b writer) -> ('b -> 'c writer) -> 'a -> 'c writer - = - fun m1 m2 x -> + let ( >=> ) (m1 : 'a -> 'b writer) (m2 : 'b -> 'c writer) = + fun x -> let y, s1 = m1 x in let z, s2 = m2 y in - z, StringLabels.concat ~sep:"" [ s1; s2 ] - ;; + (z, s1 ^ s2) end diff --git a/src/content/1.8/code/ocaml/snippet18.ml b/src/content/1.8/code/ocaml/snippet18.ml index cac9283e8..c9045484a 100644 --- a/src/content/1.8/code/ocaml/snippet18.ml +++ b/src/content/1.8/code/ocaml/snippet18.ml @@ -1,3 +1,3 @@ module KleisliIdentity = struct - let return : 'a -> 'a writer = fun a -> a, "" + let return (x : 'a) : 'a writer = (x, "") end diff --git a/src/content/1.8/code/ocaml/snippet19.ml b/src/content/1.8/code/ocaml/snippet19.ml index 593e2aa18..b29792941 100644 --- a/src/content/1.8/code/ocaml/snippet19.ml +++ b/src/content/1.8/code/ocaml/snippet19.ml @@ -1,8 +1,8 @@ -module KleisliFunctor : Functor = struct +module KleisliFunctor : Functor with type 'a t = 'a writer = struct + open KleisliComposition + open KleisliIdentity + type 'a t = 'a writer - let fmap f = - KleisliComposition.( >=> ) id (fun x -> - KleisliIdentity.return (f x)) - ;; + let fmap f = Fun.id >=> fun x -> return (f x) end diff --git a/src/content/1.8/code/ocaml/snippet22.ml b/src/content/1.8/code/ocaml/snippet22.ml index 795b20bbb..30b462dcd 100644 --- a/src/content/1.8/code/ocaml/snippet22.ml +++ b/src/content/1.8/code/ocaml/snippet22.ml @@ -1,7 +1,7 @@ module ReaderFunctor (In : sig type r -end) : Functor = struct +end) : Functor with type 'a t = (In.r, 'a) reader = struct type 'a t = (In.r, 'a) reader - let fmap f g = compose f g + let fmap f g = Fun.compose f g end diff --git a/src/content/1.8/code/ocaml/snippet24.ml b/src/content/1.8/code/ocaml/snippet24.ml index 37207ac38..b654b12f2 100644 --- a/src/content/1.8/code/ocaml/snippet24.ml +++ b/src/content/1.8/code/ocaml/snippet24.ml @@ -1 +1 @@ -val fmap : 'a 'b. ('a -> 'b) -> ('a -> 'r) -> ('b -> 'r) +val fmap : ('a -> 'b) -> ('a -> 'r) -> ('b -> 'r) diff --git a/src/content/1.8/code/ocaml/snippet26.ml b/src/content/1.8/code/ocaml/snippet26.ml index bc942bfff..3e67e6f3a 100644 --- a/src/content/1.8/code/ocaml/snippet26.ml +++ b/src/content/1.8/code/ocaml/snippet26.ml @@ -1,7 +1,7 @@ module OpContravariant (In : sig type r -end) : Contravariant = struct +end) : Contravariant with type 'a t = (In.r, 'a) op = struct type 'a t = (In.r, 'a) op - let contramap f g = compose g f + let contramap f g = Fun.compose g f end diff --git a/src/content/1.8/code/ocaml/snippet27.ml b/src/content/1.8/code/ocaml/snippet27.ml index 6231f96b9..601e2d8e4 100644 --- a/src/content/1.8/code/ocaml/snippet27.ml +++ b/src/content/1.8/code/ocaml/snippet27.ml @@ -1 +1,2 @@ -let flip f b a = f a b +(* Available as Fun.flip in the standard library *) +let flip (f : 'a -> 'b -> 'c) (x : 'b) (y : 'a) : 'c = f y x diff --git a/src/content/1.8/code/ocaml/snippet28.ml b/src/content/1.8/code/ocaml/snippet28.ml index 951687c22..e18a1858e 100644 --- a/src/content/1.8/code/ocaml/snippet28.ml +++ b/src/content/1.8/code/ocaml/snippet28.ml @@ -1,3 +1,2 @@ -let contramap : ('b -> 'a) -> ('r, 'a) op -> ('r, 'b) op = - fun f g -> flip compose f g -;; +let contramap (f : 'b -> 'a) (g : ('r, 'a) op) : ('r, 'b) op = + Fun.flip Fun.compose f g diff --git a/src/content/1.8/code/ocaml/snippet29.ml b/src/content/1.8/code/ocaml/snippet29.ml index 7d368c55c..3c751400f 100644 --- a/src/content/1.8/code/ocaml/snippet29.ml +++ b/src/content/1.8/code/ocaml/snippet29.ml @@ -1,30 +1,37 @@ -(* Profunctor definition *) module type Profunctor = sig - type ('a, 'b) p + type ('a, 'b) t - val dimap : ('a -> 'b) -> ('c -> 'd) -> ('b, 'c) p -> ('a, 'd) p + val dimap : ('a -> 'b) -> ('c -> 'd) -> ('b, 'c) t -> ('a, 'd) t + val lmap : ('a -> 'b) -> ('b, 'c) t -> ('a, 'c) t + val rmap : ('b -> 'c) -> ('a, 'b) t -> ('a, 'c) t end -(* Profunctor alternate definition *) -module type ProfunctorExt = sig - type ('a, 'b) p +(* Like with the bifunctor, we can represent a profunctor in + one of the two forms and derive one from the other. *) +module type Dimap = sig + type ('a, 'b) t - val lmap : ('a -> 'b) -> ('b, 'c) p -> ('a, 'c) p - val rmap : ('b -> 'c) -> ('a, 'b) p -> ('a, 'c) p + val dimap : ('a -> 'b) -> ('c -> 'd) -> ('b, 'c) t -> ('a, 'd) t end -(* Profunctor dimap defined using lmap and rmap *) -module Profunctor_Using_Ext (PF : ProfunctorExt) : Profunctor = struct - type ('a, 'b) p = ('a, 'b) PF.p +module type Maps = sig + type ('a, 'b) t - let dimap f g = compose (PF.lmap f) (PF.rmap g) + val lmap : ('a -> 'b) -> ('b, 'c) t -> ('a, 'c) t + val rmap : ('b -> 'c) -> ('a, 'b) t -> ('a, 'c) t end -(** Profunctor lmap and rmap defined using dimap *) -module ProfunctorExt_Using_Dimap (PF : Profunctor) : ProfunctorExt = -struct - type ('a, 'b) p = ('a, 'b) PF.p +module Profunctor_From_Dimap (M : Dimap) : + Profunctor with type ('a, 'b) t = ('a, 'b) M.t = struct + include M - let lmap f = PF.dimap f id - let rmap g = PF.dimap id g + let lmap f = M.dimap f Fun.id + let rmap g = M.dimap Fun.id g +end + +module Profunctor_From_Maps (M : Maps) : + Profunctor with type ('a, 'b) t = ('a, 'b) M.t = struct + include M + + let dimap f g = Fun.compose (M.lmap f) (M.rmap g) end diff --git a/src/content/1.8/code/ocaml/snippet30.ml b/src/content/1.8/code/ocaml/snippet30.ml index a05762bae..d0c15781d 100644 --- a/src/content/1.8/code/ocaml/snippet30.ml +++ b/src/content/1.8/code/ocaml/snippet30.ml @@ -1,12 +1,10 @@ -module ProfunctorArrow : Profunctor = struct - type ('a, 'b) p = 'a -> 'b +(* Writing the full implementation without deriving any of the functions *) +module ProfunctorArrow : Profunctor +with type ('a, 'b) t = 'a -> 'b = struct + type ('a, 'b) t = 'a -> 'b - let dimap f g p = compose g (compose p f) -end - -module ProfunctorExtArrow : ProfunctorExt = struct - type ('a, 'b) p = 'a -> 'b - - let lmap f p = (flip compose) f p - let rmap = compose + let ( % ) = Fun.compose + let dimap ab cd bc = cd % bc % ab + let lmap f g = g % f + let rmap f g = f % g end From d40a2b351a34495862cd349e15b99e42f9da1999 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:17:39 +0100 Subject: [PATCH 09/27] Update Ch. 9 OCaml snippets --- src/content/1.9/code/ocaml/snippet03.ml | 2 +- src/content/1.9/code/ocaml/snippet04.ml | 2 +- src/content/1.9/code/ocaml/snippet08.ml | 2 +- src/content/1.9/code/ocaml/snippet09.ml | 2 +- src/content/1.9/code/ocaml/snippet10.ml | 2 +- src/content/1.9/code/ocaml/snippet11.ml | 14 ++++++++------ src/content/1.9/code/ocaml/snippet14.ml | 2 +- 7 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/content/1.9/code/ocaml/snippet03.ml b/src/content/1.9/code/ocaml/snippet03.ml index 29e124c5a..178605b82 100644 --- a/src/content/1.9/code/ocaml/snippet03.ml +++ b/src/content/1.9/code/ocaml/snippet03.ml @@ -1 +1 @@ -let catstr s s' = String.concat ~sep:"" [ s; s' ] +let catstr s s' = s ^ s' diff --git a/src/content/1.9/code/ocaml/snippet04.ml b/src/content/1.9/code/ocaml/snippet04.ml index 29e124c5a..491442259 100644 --- a/src/content/1.9/code/ocaml/snippet04.ml +++ b/src/content/1.9/code/ocaml/snippet04.ml @@ -1 +1 @@ -let catstr s s' = String.concat ~sep:"" [ s; s' ] +let catstr' s = fun s' -> s ^ s' diff --git a/src/content/1.9/code/ocaml/snippet08.ml b/src/content/1.9/code/ocaml/snippet08.ml index dbb792053..fd1a1e325 100644 --- a/src/content/1.9/code/ocaml/snippet08.ml +++ b/src/content/1.9/code/ocaml/snippet08.ml @@ -1 +1 @@ -let uncurry f p = f (fst p) (snd p) +let uncurry f (a, b) = f a b diff --git a/src/content/1.9/code/ocaml/snippet09.ml b/src/content/1.9/code/ocaml/snippet09.ml index 0632d1a67..e97fa30b5 100644 --- a/src/content/1.9/code/ocaml/snippet09.ml +++ b/src/content/1.9/code/ocaml/snippet09.ml @@ -1 +1 @@ -let factorizer g a b = g (a, b) +let factorizer g = fun a -> fun b -> g (a, b) diff --git a/src/content/1.9/code/ocaml/snippet10.ml b/src/content/1.9/code/ocaml/snippet10.ml index 9fd6dd077..482b1fcc1 100644 --- a/src/content/1.9/code/ocaml/snippet10.ml +++ b/src/content/1.9/code/ocaml/snippet10.ml @@ -1,3 +1,3 @@ module type Exponential_Of_Sums_Example = sig - val f : (int, float) either -> string + val f : (int, float) Either.t -> string end diff --git a/src/content/1.9/code/ocaml/snippet11.ml b/src/content/1.9/code/ocaml/snippet11.ml index 0de551325..2af2ec833 100644 --- a/src/content/1.9/code/ocaml/snippet11.ml +++ b/src/content/1.9/code/ocaml/snippet11.ml @@ -1,9 +1,11 @@ module Exp_Sum_Impl : Exponential_Of_Sums_Example = struct let f = function - | Left n -> if n < 0 then "Negative int" else "Positive int" - | Right x -> - if Float.compare x 0.4 < 0 - then "Negative double" - else "Positive double" - ;; + | Either.Left n -> + if n < 0 + then "Negative int" + else "Positive int" + | Either.Right x -> + if x < 0.0 + then "Negative double" + else "Positive double" end diff --git a/src/content/1.9/code/ocaml/snippet14.ml b/src/content/1.9/code/ocaml/snippet14.ml index 641b8e93b..1db642276 100644 --- a/src/content/1.9/code/ocaml/snippet14.ml +++ b/src/content/1.9/code/ocaml/snippet14.ml @@ -1 +1 @@ -('a, 'b) either -> 'a +('a, 'b) Either.t -> 'a From 7ab3ab49a539d4dfa00314197075636a6cb9025d Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:18:00 +0100 Subject: [PATCH 10/27] Update Ch. 10 OCaml snippets --- src/content/1.10/code/ocaml/snippet01.ml | 2 +- src/content/1.10/code/ocaml/snippet02.ml | 2 +- src/content/1.10/code/ocaml/snippet03.ml | 2 +- src/content/1.10/code/ocaml/snippet04.ml | 1 - src/content/1.10/code/ocaml/snippet05.ml | 4 +++- src/content/1.10/code/ocaml/snippet06.ml | 6 ++++-- src/content/1.10/code/ocaml/snippet07.ml | 2 +- src/content/1.10/code/ocaml/snippet08.ml | 4 +++- src/content/1.10/code/ocaml/snippet09.ml | 5 ++++- src/content/1.10/code/ocaml/snippet10.ml | 10 ++++++---- src/content/1.10/code/ocaml/snippet11.ml | 18 ++++++++++++++---- src/content/1.10/code/ocaml/snippet12.ml | 8 +++----- src/content/1.10/code/ocaml/snippet13.ml | 4 +--- src/content/1.10/code/ocaml/snippet15.ml | 4 +--- src/content/1.10/code/ocaml/snippet17.ml | 7 ++----- src/content/1.10/code/ocaml/snippet19.ml | 4 +--- src/content/1.10/code/ocaml/snippet20.ml | 4 +--- src/content/1.10/code/ocaml/snippet22.ml | 7 ++----- src/content/1.10/code/ocaml/snippet23.ml | 4 +--- src/content/1.10/code/ocaml/snippet24.ml | 2 +- src/content/1.10/code/ocaml/snippet25.ml | 1 - src/content/1.10/code/ocaml/snippet27.ml | 2 +- 22 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/content/1.10/code/ocaml/snippet01.ml b/src/content/1.10/code/ocaml/snippet01.ml index 49d533497..8c90c2a05 100644 --- a/src/content/1.10/code/ocaml/snippet01.ml +++ b/src/content/1.10/code/ocaml/snippet01.ml @@ -1 +1 @@ -val alpha : 'a . 'a f -> 'a g +val alpha : 'a . 'a F.t -> 'a G.t (* For 2 Functors F and G *) diff --git a/src/content/1.10/code/ocaml/snippet02.ml b/src/content/1.10/code/ocaml/snippet02.ml index cf63465a6..f12e015b4 100644 --- a/src/content/1.10/code/ocaml/snippet02.ml +++ b/src/content/1.10/code/ocaml/snippet02.ml @@ -1 +1 @@ -val alpha : 'a f -> 'a g +val alpha : 'a F.t -> 'a G.t (* For 2 Functors F and G *) diff --git a/src/content/1.10/code/ocaml/snippet03.ml b/src/content/1.10/code/ocaml/snippet03.ml index cf63465a6..35962bb75 100644 --- a/src/content/1.10/code/ocaml/snippet03.ml +++ b/src/content/1.10/code/ocaml/snippet03.ml @@ -1 +1 @@ -val alpha : 'a f -> 'a g +val alpha : 'a F.t -> 'a G.t diff --git a/src/content/1.10/code/ocaml/snippet04.ml b/src/content/1.10/code/ocaml/snippet04.ml index 69990e13b..43f213a34 100644 --- a/src/content/1.10/code/ocaml/snippet04.ml +++ b/src/content/1.10/code/ocaml/snippet04.ml @@ -1,4 +1,3 @@ let safe_head = function | [] -> None | x :: xs -> Some x -;; diff --git a/src/content/1.10/code/ocaml/snippet05.ml b/src/content/1.10/code/ocaml/snippet05.ml index 13f897cf0..b183a6a9a 100644 --- a/src/content/1.10/code/ocaml/snippet05.ml +++ b/src/content/1.10/code/ocaml/snippet05.ml @@ -1 +1,3 @@ -compose (fmap f) safe_head = compose safe_head (fmap f) +(* Given a Functor implementation for Option and List, + the following equality should hold: *) +OptionFunctor.fmap f % safe_head = safe_head % ListFunctor.fmap f diff --git a/src/content/1.10/code/ocaml/snippet06.ml b/src/content/1.10/code/ocaml/snippet06.ml index e6882baee..2b03bb9e5 100644 --- a/src/content/1.10/code/ocaml/snippet06.ml +++ b/src/content/1.10/code/ocaml/snippet06.ml @@ -1,2 +1,4 @@ -(* Starting with empty list *) -let fmap f (safe_head []) = fmap f None = None +(* As a reminder, this is not actual code *) +OptionFunctor.fmap f (safe_head []) + = OptionFunctor.fmap f None + = None diff --git a/src/content/1.10/code/ocaml/snippet07.ml b/src/content/1.10/code/ocaml/snippet07.ml index f4ac25115..f2fb67b83 100644 --- a/src/content/1.10/code/ocaml/snippet07.ml +++ b/src/content/1.10/code/ocaml/snippet07.ml @@ -1 +1 @@ -let safe_head (fmap f []) = safe_head [] = None +safe_head (ListFunctor.fmap f []) = safe_head [] = None diff --git a/src/content/1.10/code/ocaml/snippet08.ml b/src/content/1.10/code/ocaml/snippet08.ml index 0c411cf27..312184f3b 100644 --- a/src/content/1.10/code/ocaml/snippet08.ml +++ b/src/content/1.10/code/ocaml/snippet08.ml @@ -1 +1,3 @@ -let fmap f (safe_head (x :: xs)) = fmap f (Some x)= Some (f x) +OptFunctor.fmap f (safe_head (x :: xs)) + = OptFunctor.fmap f (Some x) + = Some (f x) diff --git a/src/content/1.10/code/ocaml/snippet09.ml b/src/content/1.10/code/ocaml/snippet09.ml index abe3681a0..47d4b1efb 100644 --- a/src/content/1.10/code/ocaml/snippet09.ml +++ b/src/content/1.10/code/ocaml/snippet09.ml @@ -1 +1,4 @@ -let safe_head (fmap f (x :: xs)) = safe_head (f x :: f xs) = Some (f x) + +safe_head (ListFunctor.fmap f (x :: xs)) + = safe_head (f x :: f xs) + = Some (f x) diff --git a/src/content/1.10/code/ocaml/snippet10.ml b/src/content/1.10/code/ocaml/snippet10.ml index fa7be49a3..203e53298 100644 --- a/src/content/1.10/code/ocaml/snippet10.ml +++ b/src/content/1.10/code/ocaml/snippet10.ml @@ -1,4 +1,6 @@ -let rec fmap f = function - | [] -> [] - | x :: xs -> f x :: fmap f xs -;; +module ListFunctor : Functor with type 'a t = 'a list = struct + type 'a t = 'a list + let rec fmap f = function + | [] -> [] + | x :: xs -> f x :: fmap f xs +end diff --git a/src/content/1.10/code/ocaml/snippet11.ml b/src/content/1.10/code/ocaml/snippet11.ml index 9ee8d9229..516ef5d55 100644 --- a/src/content/1.10/code/ocaml/snippet11.ml +++ b/src/content/1.10/code/ocaml/snippet11.ml @@ -1,4 +1,14 @@ -let rec fmap f = function - | None -> None - | Some x -> Some (f x) -;; +module OptionFunctor : Functor with type 'a t = 'a option = struct + type 'a t = 'a option + + let fmap f = function + | None -> None + | Some x -> Some (f x) +end + +(* As a reminder, note that in the OCaml Stdlib, the List + and Option modules are already "Functors" in the sense + that they have an fmap function (called map). Thus, the + following fmaps do the same as the fmaps defined above. *) +let option_fmap f = Option.map f +let list_fmap f = List.map f diff --git a/src/content/1.10/code/ocaml/snippet12.ml b/src/content/1.10/code/ocaml/snippet12.ml index d8f69fc7c..521019d75 100644 --- a/src/content/1.10/code/ocaml/snippet12.ml +++ b/src/content/1.10/code/ocaml/snippet12.ml @@ -1,8 +1,6 @@ -(** OCaml requires mutually recursive functions to be defined together *) +type ('c, 'a) const = Const of 'c + +(* Assuming you've defined un_const before this function *) let rec length : 'a list -> (int, 'a) const = function | [] -> Const 0 | _ :: xs -> Const (1 + un_const (length xs)) - -and un_const : 'c 'a. ('c, 'a) const -> 'c = function - | Const c -> c -;; diff --git a/src/content/1.10/code/ocaml/snippet13.ml b/src/content/1.10/code/ocaml/snippet13.ml index e9414f517..f9facc3f4 100644 --- a/src/content/1.10/code/ocaml/snippet13.ml +++ b/src/content/1.10/code/ocaml/snippet13.ml @@ -1,3 +1 @@ -let un_const : 'c 'a. ('c, 'a) const -> 'c = function - | Const c -> c -;; +let un_const (Const c) = c diff --git a/src/content/1.10/code/ocaml/snippet15.ml b/src/content/1.10/code/ocaml/snippet15.ml index 8e302b76a..c96d3ccd2 100644 --- a/src/content/1.10/code/ocaml/snippet15.ml +++ b/src/content/1.10/code/ocaml/snippet15.ml @@ -1,3 +1 @@ -let scam : 'a. ('int, 'a) const -> 'a option = function - | Const a -> None -;; +let scam (Const x : (int, 'a) const) : 'a option = None diff --git a/src/content/1.10/code/ocaml/snippet17.ml b/src/content/1.10/code/ocaml/snippet17.ml index aaedbf3ab..de2faafd1 100644 --- a/src/content/1.10/code/ocaml/snippet17.ml +++ b/src/content/1.10/code/ocaml/snippet17.ml @@ -1,10 +1,7 @@ module Reader_Functor (T : sig type e -end) : Functor = struct +end) : Functor with type 'a t = (T.e, 'a) reader = struct type 'a t = (T.e, 'a) reader - let fmap : 'a 'b. ('a -> 'b) -> 'a t -> 'b t = - fun f -> function - | Reader r -> Reader (compose f r) - ;; + let fmap f (Reader g) = Reader (fun e -> f (g e)) end diff --git a/src/content/1.10/code/ocaml/snippet19.ml b/src/content/1.10/code/ocaml/snippet19.ml index 84ac342f5..75a33c1f1 100644 --- a/src/content/1.10/code/ocaml/snippet19.ml +++ b/src/content/1.10/code/ocaml/snippet19.ml @@ -1,3 +1 @@ -let dumb : 'a. (unit, 'a) reader -> 'a option = function - | Reader _ -> None -;; +let dumb (Reader _) = None diff --git a/src/content/1.10/code/ocaml/snippet20.ml b/src/content/1.10/code/ocaml/snippet20.ml index c015712bc..b2a73e21e 100644 --- a/src/content/1.10/code/ocaml/snippet20.ml +++ b/src/content/1.10/code/ocaml/snippet20.ml @@ -1,3 +1 @@ -let obvious : 'a. (unit, 'a) reader -> 'a option = function - | Reader f -> Some (f ()) -;; +let obvious (Reader g) = Some (g ()) diff --git a/src/content/1.10/code/ocaml/snippet22.ml b/src/content/1.10/code/ocaml/snippet22.ml index ae6702178..894f655c0 100644 --- a/src/content/1.10/code/ocaml/snippet22.ml +++ b/src/content/1.10/code/ocaml/snippet22.ml @@ -1,10 +1,7 @@ module Op_Contravariant (T : sig type r -end) : Contravariant = struct +end) : Contravariant with type 'a t = (T.r, 'a) op = struct type 'a t = (T.r, 'a) op - let contramap : ('b -> 'a) -> 'a t -> 'b t = - fun f -> function - | Op g -> Op (compose g f) - ;; + let contramap f (Op g) = Op (Fun.compose g f) end diff --git a/src/content/1.10/code/ocaml/snippet23.ml b/src/content/1.10/code/ocaml/snippet23.ml index a93249ec3..169bd6708 100644 --- a/src/content/1.10/code/ocaml/snippet23.ml +++ b/src/content/1.10/code/ocaml/snippet23.ml @@ -1,3 +1 @@ -let pred_to_str = function - | Op f -> Op (fun x -> if f x then "T" else "F") -;; +let pred_to_str (Op f) = Op (fun x -> if f x then "T" else "F") diff --git a/src/content/1.10/code/ocaml/snippet24.ml b/src/content/1.10/code/ocaml/snippet24.ml index f58a24f2e..389cc65bd 100644 --- a/src/content/1.10/code/ocaml/snippet24.ml +++ b/src/content/1.10/code/ocaml/snippet24.ml @@ -1 +1 @@ -compose (contramap f) pred_to_str = compose pred_to_str (contramap f) +contramap f % pred_to_str = pred_to_str % contramap f diff --git a/src/content/1.10/code/ocaml/snippet25.ml b/src/content/1.10/code/ocaml/snippet25.ml index 20e7bc077..27483332f 100644 --- a/src/content/1.10/code/ocaml/snippet25.ml +++ b/src/content/1.10/code/ocaml/snippet25.ml @@ -4,4 +4,3 @@ end) let op_bool_contramap : ('b -> 'a) -> 'a Op_Bool.t -> 'b Op_Bool.t = Op_Bool.contramap -;; diff --git a/src/content/1.10/code/ocaml/snippet27.ml b/src/content/1.10/code/ocaml/snippet27.ml index bcd4326a4..b3b16b5bd 100644 --- a/src/content/1.10/code/ocaml/snippet27.ml +++ b/src/content/1.10/code/ocaml/snippet27.ml @@ -1 +1 @@ -('a -> 'a) -> 'a f +('a -> 'a) -> 'a F.t From c3cf7c2c00821a6ec00a4dac406f27ef723151b5 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:18:41 +0100 Subject: [PATCH 11/27] Update Ch. 11 OCaml snippets --- src/content/2.1/code/ocaml/snippet01.ml | 4 ++-- src/content/2.1/code/ocaml/snippet02.ml | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/content/2.1/code/ocaml/snippet01.ml b/src/content/2.1/code/ocaml/snippet01.ml index 511023769..f98e24cca 100644 --- a/src/content/2.1/code/ocaml/snippet01.ml +++ b/src/content/2.1/code/ocaml/snippet01.ml @@ -1,2 +1,2 @@ -(* Assume g and f are already defined *) -let h = compose g f +(* Assuming g and f are already defined *) +let h = Fun.compose g f diff --git a/src/content/2.1/code/ocaml/snippet02.ml b/src/content/2.1/code/ocaml/snippet02.ml index 54dab9c22..db42fc5f8 100644 --- a/src/content/2.1/code/ocaml/snippet02.ml +++ b/src/content/2.1/code/ocaml/snippet02.ml @@ -1,4 +1,3 @@ let h x = let y = f x in g y -;; From 949a89816c3019295eb32b38c2341bc4d6373e01 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:18:57 +0100 Subject: [PATCH 12/27] Update Ch. 12 OCaml snippets --- src/content/2.2/code/ocaml/snippet01.ml | 4 ++-- src/content/2.2/code/ocaml/snippet02.ml | 6 ++---- src/content/2.2/code/ocaml/snippet05.ml | 5 +++-- src/content/2.2/code/ocaml/snippet06.ml | 3 +-- src/content/2.2/code/ocaml/snippet09.ml | 3 +-- src/content/2.2/code/ocaml/snippet11.ml | 2 +- src/content/2.2/code/ocaml/snippet15.ml | 2 +- src/content/2.2/code/ocaml/snippet17.ml | 10 ++-------- src/content/2.2/code/ocaml/snippet18.ml | 2 +- src/content/2.2/code/ocaml/snippet19.ml | 2 +- 10 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/content/2.2/code/ocaml/snippet01.ml b/src/content/2.2/code/ocaml/snippet01.ml index 223fd022b..a31d3a093 100644 --- a/src/content/2.2/code/ocaml/snippet01.ml +++ b/src/content/2.2/code/ocaml/snippet01.ml @@ -1,2 +1,2 @@ -let p1 = compose p m -let q1 = compose q m +let p' = Fun.compose p m +let q' = Fun.compose q m diff --git a/src/content/2.2/code/ocaml/snippet02.ml b/src/content/2.2/code/ocaml/snippet02.ml index 38d99345f..9a8194dd4 100644 --- a/src/content/2.2/code/ocaml/snippet02.ml +++ b/src/content/2.2/code/ocaml/snippet02.ml @@ -1,4 +1,2 @@ -let contramap : ('c_prime -> 'c) -> ('c -> 'limD) -> 'c_prime -> 'limD - = - fun f u -> compose u f -;; +let contramap (f : 'c_prime -> 'c) (u : 'c -> limD) : 'c_prime -> limD = + Fun.compose u f diff --git a/src/content/2.2/code/ocaml/snippet05.ml b/src/content/2.2/code/ocaml/snippet05.ml index 5680d1e93..adcc2ab51 100644 --- a/src/content/2.2/code/ocaml/snippet05.ml +++ b/src/content/2.2/code/ocaml/snippet05.ml @@ -1,2 +1,3 @@ -q = compose f p -q = compose g p +(* Pseudocode *) +q = Fun.compose f p +q = Fun.compose g p diff --git a/src/content/2.2/code/ocaml/snippet06.ml b/src/content/2.2/code/ocaml/snippet06.ml index ca0277372..fd2dcacde 100644 --- a/src/content/2.2/code/ocaml/snippet06.ml +++ b/src/content/2.2/code/ocaml/snippet06.ml @@ -1,2 +1 @@ -(** Pseudo OCaml expressing function equality **) -compose f p = compose g p +Fun.compose f p = Fun.compose g p diff --git a/src/content/2.2/code/ocaml/snippet09.ml b/src/content/2.2/code/ocaml/snippet09.ml index adf85c5a4..5f8c92e42 100644 --- a/src/content/2.2/code/ocaml/snippet09.ml +++ b/src/content/2.2/code/ocaml/snippet09.ml @@ -1,2 +1 @@ -(** Pseudo OCaml expressing function equality **) -compose f p' = compose g p' +Fun.compose f p' = Fun.compose g p' diff --git a/src/content/2.2/code/ocaml/snippet11.ml b/src/content/2.2/code/ocaml/snippet11.ml index b18ca8f44..2e5ed13ab 100644 --- a/src/content/2.2/code/ocaml/snippet11.ml +++ b/src/content/2.2/code/ocaml/snippet11.ml @@ -1 +1 @@ -let p' = compose p h +let p' = Fun.compose p h diff --git a/src/content/2.2/code/ocaml/snippet15.ml b/src/content/2.2/code/ocaml/snippet15.ml index 928048952..23d547f5a 100644 --- a/src/content/2.2/code/ocaml/snippet15.ml +++ b/src/content/2.2/code/ocaml/snippet15.ml @@ -1 +1 @@ -compose g q = compose f p +Fun.compose g q = Fun.compose f p diff --git a/src/content/2.2/code/ocaml/snippet17.ml b/src/content/2.2/code/ocaml/snippet17.ml index d9f237c05..a120ee603 100644 --- a/src/content/2.2/code/ocaml/snippet17.ml +++ b/src/content/2.2/code/ocaml/snippet17.ml @@ -1,13 +1,7 @@ -module type Contravariant = sig - type 'a t - - val contramap : ('b -> 'a) -> 'a t -> 'b t -end - type 'a tostring = ToString of ('a -> string) -module ToStringInstance : Contravariant = struct +module ToStringInstance : Contravariant with type 'a t = 'a tostring = struct type 'a t = 'a tostring - let contramap f (ToString g) = ToString (compose g f) + let contramap f (ToString g) = ToString (Fun.compose g f) end diff --git a/src/content/2.2/code/ocaml/snippet18.ml b/src/content/2.2/code/ocaml/snippet18.ml index 37f14061c..d3b6cde58 100644 --- a/src/content/2.2/code/ocaml/snippet18.ml +++ b/src/content/2.2/code/ocaml/snippet18.ml @@ -1 +1 @@ -('b 'c either) tostring ~ ('b -> string, 'c -> string) +('b, 'c) Either.t tostring ~ ('b -> string * 'c -> string) diff --git a/src/content/2.2/code/ocaml/snippet19.ml b/src/content/2.2/code/ocaml/snippet19.ml index 9a0c9db77..164a0d74d 100644 --- a/src/content/2.2/code/ocaml/snippet19.ml +++ b/src/content/2.2/code/ocaml/snippet19.ml @@ -1 +1 @@ -'r -> ('a, 'b) ~ ('r -> 'a, 'r -> 'b) +'r -> ('a * 'b) ~ ('r -> 'a * 'r -> 'b) From cd63d30d72d139b62eb92dfacbc18e37f2aea2d2 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:19:29 +0100 Subject: [PATCH 13/27] Update Ch. 13 OCaml snippets --- src/content/2.3/code/ocaml/snippet01.ml | 6 +++--- src/content/2.3/code/ocaml/snippet02.ml | 6 +++--- src/content/2.3/code/ocaml/snippet03.ml | 2 +- src/content/2.3/code/ocaml/snippet04.ml | 2 +- src/content/2.3/code/ocaml/snippet05.ml | 2 +- src/content/2.3/code/ocaml/snippet09.ml | 2 +- src/content/2.3/code/ocaml/snippet10.ml | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/content/2.3/code/ocaml/snippet01.ml b/src/content/2.3/code/ocaml/snippet01.ml index 2642b2645..cd4412bba 100644 --- a/src/content/2.3/code/ocaml/snippet01.ml +++ b/src/content/2.3/code/ocaml/snippet01.ml @@ -1,6 +1,6 @@ module type Monoid = sig - type m + type t - val mempty : m - val mappend : m -> m -> m + val mempty : t + val mappend : t -> t -> t end diff --git a/src/content/2.3/code/ocaml/snippet02.ml b/src/content/2.3/code/ocaml/snippet02.ml index 11549d28f..a391a5c94 100644 --- a/src/content/2.3/code/ocaml/snippet02.ml +++ b/src/content/2.3/code/ocaml/snippet02.ml @@ -1,8 +1,8 @@ module ListMonoid (T1 : sig type a -end) : Monoid with type m = T1.a list = struct - type m = T1.a list +end) : Monoid with type t = T1.a list = struct + type t = T1.a list let mempty = [] - let mappend xs ys = List.append xs ys + let mappend xs ys = xs @ ys end diff --git a/src/content/2.3/code/ocaml/snippet03.ml b/src/content/2.3/code/ocaml/snippet03.ml index fa4e639b1..a655a7476 100644 --- a/src/content/2.3/code/ocaml/snippet03.ml +++ b/src/content/2.3/code/ocaml/snippet03.ml @@ -1,2 +1,2 @@ 2 * 3 = 6 -List.append [2] [3] = [2; 3] +[2] @ [3] = [2; 3] diff --git a/src/content/2.3/code/ocaml/snippet04.ml b/src/content/2.3/code/ocaml/snippet04.ml index fd024d052..f7fe70233 100644 --- a/src/content/2.3/code/ocaml/snippet04.ml +++ b/src/content/2.3/code/ocaml/snippet04.ml @@ -1 +1 @@ -let h (a * b) = h a * h b +h (a * b) = h a * h b diff --git a/src/content/2.3/code/ocaml/snippet05.ml b/src/content/2.3/code/ocaml/snippet05.ml index 694a1e55b..1241aee0b 100644 --- a/src/content/2.3/code/ocaml/snippet05.ml +++ b/src/content/2.3/code/ocaml/snippet05.ml @@ -1 +1 @@ -List.append [2] [3] = [2; 3] +[ 2 ] @ [ 3 ] = [ 2; 3 ] diff --git a/src/content/2.3/code/ocaml/snippet09.ml b/src/content/2.3/code/ocaml/snippet09.ml index ec5b36119..7b567a177 100644 --- a/src/content/2.3/code/ocaml/snippet09.ml +++ b/src/content/2.3/code/ocaml/snippet09.ml @@ -1 +1 @@ -val h : m -> n +val h : 'm -> 'n diff --git a/src/content/2.3/code/ocaml/snippet10.ml b/src/content/2.3/code/ocaml/snippet10.ml index f1ffddbb4..b6bbbc18e 100644 --- a/src/content/2.3/code/ocaml/snippet10.ml +++ b/src/content/2.3/code/ocaml/snippet10.ml @@ -1 +1 @@ -val q = compose uh p +q = Fun.compose uh p From 651fa482103d34971112afc97b151f473f39b6bd Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:19:48 +0100 Subject: [PATCH 14/27] Update Ch. 14 OCaml snippets --- src/content/2.4/code/ocaml/snippet02.ml | 2 +- src/content/2.4/code/ocaml/snippet04.ml | 2 +- src/content/2.4/code/ocaml/snippet05.ml | 12 +++++++----- src/content/2.4/code/ocaml/snippet06.ml | 2 +- src/content/2.4/code/ocaml/snippet07.ml | 2 +- src/content/2.4/code/ocaml/snippet08.ml | 2 +- src/content/2.4/code/ocaml/snippet09.ml | 2 +- src/content/2.4/code/ocaml/snippet11.ml | 2 +- src/content/2.4/code/ocaml/snippet15.ml | 1 - 9 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/content/2.4/code/ocaml/snippet02.ml b/src/content/2.4/code/ocaml/snippet02.ml index 8b9d63b2b..9deda3811 100644 --- a/src/content/2.4/code/ocaml/snippet02.ml +++ b/src/content/2.4/code/ocaml/snippet02.ml @@ -1,6 +1,6 @@ module ReaderFunctor (T : sig type r -end) : Functor = struct +end) : Functor with type 'a t = (T.r, 'a) reader = struct type 'a t = (T.r, 'a) reader let fmap f h a = f (h a) diff --git a/src/content/2.4/code/ocaml/snippet04.ml b/src/content/2.4/code/ocaml/snippet04.ml index ba8a193c2..d7bc43a41 100644 --- a/src/content/2.4/code/ocaml/snippet04.ml +++ b/src/content/2.4/code/ocaml/snippet04.ml @@ -1,6 +1,6 @@ module OpContravariant (T : sig type r -end) : Contravariant = struct +end) : Contravariant with type 'a t = (T.r, 'a) op = struct type 'a t = (T.r, 'a) op let contramap f h b = h (f b) diff --git a/src/content/2.4/code/ocaml/snippet05.ml b/src/content/2.4/code/ocaml/snippet05.ml index f54ef523c..81633e93f 100644 --- a/src/content/2.4/code/ocaml/snippet05.ml +++ b/src/content/2.4/code/ocaml/snippet05.ml @@ -1,7 +1,9 @@ -module ProfunctorArrow : Profunctor = struct - type ('a, 'b) p = 'a -> 'b +module ProfunctorArrow : Profunctor +with type ('a, 'b) t = 'a -> 'b = struct + type ('a, 'b) t = 'a -> 'b - let dimap f g p = compose g (compose p f) - let lmap f p = (flip compose) f p - let rmap = compose + let ( % ) = Fun.compose + let dimap ab cd f = cd % f % ab + let lmap f g = g % f + let rmap f g = f % g end diff --git a/src/content/2.4/code/ocaml/snippet06.ml b/src/content/2.4/code/ocaml/snippet06.ml index b6c742bf2..f96a8fe8a 100644 --- a/src/content/2.4/code/ocaml/snippet06.ml +++ b/src/content/2.4/code/ocaml/snippet06.ml @@ -2,5 +2,5 @@ module type NT_AX_FX = sig type a type 'x t - val alpha : (a -> 'x) -> 'x t + val alpha : 'x. (int -> 'x) -> 'x t end diff --git a/src/content/2.4/code/ocaml/snippet07.ml b/src/content/2.4/code/ocaml/snippet07.ml index 80888385e..4061b70d7 100644 --- a/src/content/2.4/code/ocaml/snippet07.ml +++ b/src/content/2.4/code/ocaml/snippet07.ml @@ -1 +1 @@ -compose (F.fmap f) NT.alpha = compose NT.alpha (F.fmap f) +F.fmap f % alpha = alpha % F.fmap f diff --git a/src/content/2.4/code/ocaml/snippet08.ml b/src/content/2.4/code/ocaml/snippet08.ml index 4d3519fba..9e6de5deb 100644 --- a/src/content/2.4/code/ocaml/snippet08.ml +++ b/src/content/2.4/code/ocaml/snippet08.ml @@ -1 +1 @@ -F.fmap f (N.alpha h) = N.alpha (compose f h) +F.fmap f (alpha h) = alpha (f % h) diff --git a/src/content/2.4/code/ocaml/snippet09.ml b/src/content/2.4/code/ocaml/snippet09.ml index 041a32d60..01936b0cb 100644 --- a/src/content/2.4/code/ocaml/snippet09.ml +++ b/src/content/2.4/code/ocaml/snippet09.ml @@ -2,5 +2,5 @@ module type NT_FX_AX = sig type a type 'x t - val beta : 'x t -> a -> 'x + val beta : 'x. ('x t -> a) -> 'x t end diff --git a/src/content/2.4/code/ocaml/snippet11.ml b/src/content/2.4/code/ocaml/snippet11.ml index 5388056fc..2aadf0391 100644 --- a/src/content/2.4/code/ocaml/snippet11.ml +++ b/src/content/2.4/code/ocaml/snippet11.ml @@ -1 +1 @@ -F.fmap f (F.fmap h [12]) = F.fmap (compose f h) [12] +F.fmap f (F.fmap h [12]) = F.fmap (f % h) [12] diff --git a/src/content/2.4/code/ocaml/snippet15.ml b/src/content/2.4/code/ocaml/snippet15.ml index 10a5351dd..7077005be 100644 --- a/src/content/2.4/code/ocaml/snippet15.ml +++ b/src/content/2.4/code/ocaml/snippet15.ml @@ -6,5 +6,4 @@ module StreamRepresentable : Representable = struct let rec index (Cons (b, bs)) n = if n = 0 then b else index (Lazy.force bs) (n - 1) - ;; end From d4c9580bba80903b1aed29b6ffbaa4be95a233b6 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:20:21 +0100 Subject: [PATCH 15/27] Update Ch. 15 OCaml snippets --- src/content/2.5/code/ocaml/snippet02.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/2.5/code/ocaml/snippet02.ml b/src/content/2.5/code/ocaml/snippet02.ml index 3ea945192..6b5b8a7f3 100644 --- a/src/content/2.5/code/ocaml/snippet02.ml +++ b/src/content/2.5/code/ocaml/snippet02.ml @@ -1,7 +1,7 @@ module ReaderFunctor (T : sig type a -end) : Functor = struct +end) : Functor with type 'x t = (T.a, 'x) reader = struct type 'x t = (T.a, 'x) reader - let fmap : ('x -> 'y) -> 'x t -> 'y t = fun f r a -> f (r a) + let fmap (f : 'x -> 'y) (r : 'x t) : 'y t = Fun.compose f r end From b1be757201c78520ffa0251f25fd7658e38bd104 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:20:40 +0100 Subject: [PATCH 16/27] Update Ch. 16 OCaml snippets --- src/content/2.6/code/ocaml/snippet01.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/2.6/code/ocaml/snippet01.ml b/src/content/2.6/code/ocaml/snippet01.ml index 13d39cfb4..a2a62e3d8 100644 --- a/src/content/2.6/code/ocaml/snippet01.ml +++ b/src/content/2.6/code/ocaml/snippet01.ml @@ -7,5 +7,5 @@ end (* Define the Yoneda embedding *) module Yoneda_Embedding (E : BtoA) = struct - let fromY : 'x. (E.a -> 'x) -> E.b -> 'x = fun f b -> f (E.btoa b) + let fromY (f : E.a -> 'x) (b : E.b) : 'x = f (E.btoa b) end From d0d829cf108d896344d08074dd649bcd021f7f6c Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:22:15 +0100 Subject: [PATCH 17/27] Update Ch. 18 OCaml snippets --- src/content/3.2/code/ocaml/snippet02.ml | 2 +- src/content/3.2/code/ocaml/snippet06.ml | 1 - src/content/3.2/code/ocaml/snippet08.ml | 4 ++-- src/content/3.2/code/ocaml/snippet09.ml | 4 +++- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/content/3.2/code/ocaml/snippet02.ml b/src/content/3.2/code/ocaml/snippet02.ml index 00450ef3f..50c4ef344 100644 --- a/src/content/3.2/code/ocaml/snippet02.ml +++ b/src/content/3.2/code/ocaml/snippet02.ml @@ -1,5 +1,5 @@ module type Unit_Example = sig - type 'a m + type 'd m val return : 'd -> 'd m end diff --git a/src/content/3.2/code/ocaml/snippet06.ml b/src/content/3.2/code/ocaml/snippet06.ml index c11dd39ea..ce892af46 100644 --- a/src/content/3.2/code/ocaml/snippet06.ml +++ b/src/content/3.2/code/ocaml/snippet06.ml @@ -45,7 +45,6 @@ functor let counit : 'a. 'a u f -> 'a = fun fua -> M.right_adjunct idty fua - ;; end (* Implementing left and right adjunct from unit/counit Definitions *) diff --git a/src/content/3.2/code/ocaml/snippet08.ml b/src/content/3.2/code/ocaml/snippet08.ml index e09ff802f..7c6a715cf 100644 --- a/src/content/3.2/code/ocaml/snippet08.ml +++ b/src/content/3.2/code/ocaml/snippet08.ml @@ -1,2 +1,2 @@ -compose fst (factorizer p q) = p -compose snd (factorizer p q) = q +Fun.compose fst (factorizer p q) = p +Fun.compose snd (factorizer p q) = q diff --git a/src/content/3.2/code/ocaml/snippet09.ml b/src/content/3.2/code/ocaml/snippet09.ml index 230e69b58..f471004fe 100644 --- a/src/content/3.2/code/ocaml/snippet09.ml +++ b/src/content/3.2/code/ocaml/snippet09.ml @@ -1 +1,3 @@ -int * bool ~ (int, bool) +(* This notation isn't possible in OCaml. + A product type is simply written as: *) +int * bool From ba65eed5e297de9e395dda303b89d0a0122e2eba Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:22:33 +0100 Subject: [PATCH 18/27] Update Ch. 19 OCaml snippets --- src/content/3.3/code/ocaml/snippet01.ml | 3 +++ src/content/3.3/code/ocaml/snippet02.ml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/content/3.3/code/ocaml/snippet01.ml b/src/content/3.3/code/ocaml/snippet01.ml index 2e2f4e126..12aa8cafc 100644 --- a/src/content/3.3/code/ocaml/snippet01.ml +++ b/src/content/3.3/code/ocaml/snippet01.ml @@ -1 +1,4 @@ +(* Note: in OCaml, strings are not equivalent to char lists. + Strings are treated as immutable sequences of bytes, while + a char list is a linked list of characters. *) type string = char list diff --git a/src/content/3.3/code/ocaml/snippet02.ml b/src/content/3.3/code/ocaml/snippet02.ml index 048eddf75..32b99f5c8 100644 --- a/src/content/3.3/code/ocaml/snippet02.ml +++ b/src/content/3.3/code/ocaml/snippet02.ml @@ -1,2 +1,2 @@ let to_nat : unit list -> int = List.length -let to_lst : int -> unit list = fun n -> List.init n ~f:(fun _ -> ()) +let to_lst (n : int) : unit list = List.init n (fun _ -> ()) From 56f05f08241d648a8a832b6d1f09a5b0d10fa379 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:22:52 +0100 Subject: [PATCH 19/27] Update Ch. 20 OCaml snippets --- src/content/3.4/code/ocaml/snippet01.ml | 16 ++++++---------- src/content/3.4/code/ocaml/snippet02.ml | 2 ++ src/content/3.4/code/ocaml/snippet04.ml | 6 +++--- src/content/3.4/code/ocaml/snippet05.ml | 8 ++++---- src/content/3.4/code/ocaml/snippet09.ml | 2 +- src/content/3.4/code/ocaml/snippet10.ml | 6 +++--- src/content/3.4/code/ocaml/snippet12.ml | 2 +- src/content/3.4/code/ocaml/snippet13.ml | 5 +---- src/content/3.4/code/ocaml/snippet17.ml | 11 +++++++---- src/content/3.4/code/ocaml/snippet18.ml | 2 +- src/content/3.4/code/ocaml/snippet19.ml | 2 +- src/content/3.4/code/ocaml/snippet20.ml | 7 +++++-- src/content/3.4/code/ocaml/snippet21.ml | 2 +- src/content/3.4/code/ocaml/snippet22.ml | 15 ++++++++++----- src/content/3.4/code/ocaml/snippet23.ml | 9 ++++----- src/content/3.4/code/ocaml/snippet25.ml | 7 +++---- 16 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/content/3.4/code/ocaml/snippet01.ml b/src/content/3.4/code/ocaml/snippet01.ml index 7b7947a1c..d55e42ec6 100644 --- a/src/content/3.4/code/ocaml/snippet01.ml +++ b/src/content/3.4/code/ocaml/snippet01.ml @@ -1,12 +1,8 @@ -(* Depends on OCaml library Core *) -module Vlen (F : Functor with type 'a t = 'a list) = struct - let summable = - (module Float : Base__.Container_intf.Summable - with type t = float) +let ( % ) = Fun.compose +(* There's no sum function in the List module *) +let sum = List.fold_left ( +. ) 0. - let vlen = - Float.sqrt - <.> List.sum summable ~f:Fn.id - <.> F.fmap (flip Float.int_pow 2) -end +(* The vlen function, like the Haskell version, using + List.map as the list functor fmap *) +let vlen = sqrt % sum % List.map (Fun.flip ( ** ) 2.) diff --git a/src/content/3.4/code/ocaml/snippet02.ml b/src/content/3.4/code/ocaml/snippet02.ml index b7f71134f..4d8770b5e 100644 --- a/src/content/3.4/code/ocaml/snippet02.ml +++ b/src/content/3.4/code/ocaml/snippet02.ml @@ -1,3 +1,5 @@ +type ('w, 'a) writer = Writer of 'a * 'w + module WriterInstance (W : sig type w end) : Functor with type 'a t = (W.w, 'a) writer = struct diff --git a/src/content/3.4/code/ocaml/snippet04.ml b/src/content/3.4/code/ocaml/snippet04.ml index 82df8ae98..df8302c78 100644 --- a/src/content/3.4/code/ocaml/snippet04.ml +++ b/src/content/3.4/code/ocaml/snippet04.ml @@ -1,6 +1,6 @@ module type Monad = sig - type 'a m + type 'a t - val ( >=> ) : ('a -> 'b m) -> ('b -> 'c m) -> 'a -> 'c m - val return : 'a -> 'a m + val ( >=> ) : ('a -> 'b t) -> ('b -> 'c t) -> 'a -> 'c t + val return : 'a -> 'a t end diff --git a/src/content/3.4/code/ocaml/snippet05.ml b/src/content/3.4/code/ocaml/snippet05.ml index 29e2c98a6..a6a646d98 100644 --- a/src/content/3.4/code/ocaml/snippet05.ml +++ b/src/content/3.4/code/ocaml/snippet05.ml @@ -1,12 +1,12 @@ -module WriterMonad (W : Monoid) : - Monad with type 'a m = (W.a, 'a) writer = struct - type 'a m = (W.a, 'a) writer +module WriterMonad (W : Monoid) : Monad +with type 'a t = (W.t, 'a) writer = +struct + type 'a t = (W.t, 'a) writer let ( >=> ) f g a = let (Writer (b, w)) = f a in let (Writer (c, w')) = g b in Writer (c, W.mappend w w') - let return a = Writer (a, W.mempty) end diff --git a/src/content/3.4/code/ocaml/snippet09.ml b/src/content/3.4/code/ocaml/snippet09.ml index 2ffe02854..50b1b5ef9 100644 --- a/src/content/3.4/code/ocaml/snippet09.ml +++ b/src/content/3.4/code/ocaml/snippet09.ml @@ -1 +1 @@ -val ( >>= ) : 'a m -> ('a -> 'b m) -> 'b m +val ( >>= ) : 'a M.t -> ('a -> 'b M.t) -> 'b M.t diff --git a/src/content/3.4/code/ocaml/snippet10.ml b/src/content/3.4/code/ocaml/snippet10.ml index 6c4bbee47..a2f46d1cd 100644 --- a/src/content/3.4/code/ocaml/snippet10.ml +++ b/src/content/3.4/code/ocaml/snippet10.ml @@ -1,6 +1,6 @@ module type Monad_Bind = sig - type 'a m + type 'a t - val ( >>= ) : 'a m -> ('a -> 'b m) -> 'b m - val return : 'a -> 'a m + val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t + val return : 'a -> 'a t end diff --git a/src/content/3.4/code/ocaml/snippet12.ml b/src/content/3.4/code/ocaml/snippet12.ml index 1c2d79179..e291a2193 100644 --- a/src/content/3.4/code/ocaml/snippet12.ml +++ b/src/content/3.4/code/ocaml/snippet12.ml @@ -1 +1 @@ -val join : ('a m) m : 'a m +val join : ('a M.t) M.t -> 'a M.t diff --git a/src/content/3.4/code/ocaml/snippet13.ml b/src/content/3.4/code/ocaml/snippet13.ml index a3ce5d8c3..8f0acb11f 100644 --- a/src/content/3.4/code/ocaml/snippet13.ml +++ b/src/content/3.4/code/ocaml/snippet13.ml @@ -1,9 +1,6 @@ module BindUsingFunctionAndJoin (F : Functor) = struct type 'a m = 'a F.t - external join : 'a m m -> 'a m = "%identity" - (** Make the type signature of join work - without providing an implementation **) - + (* Assuming that join is defined before *) let ( >>= ) ma f = join (F.fmap f ma) end diff --git a/src/content/3.4/code/ocaml/snippet17.ml b/src/content/3.4/code/ocaml/snippet17.ml index 8c73d56f0..662da63db 100644 --- a/src/content/3.4/code/ocaml/snippet17.ml +++ b/src/content/3.4/code/ocaml/snippet17.ml @@ -1,7 +1,10 @@ -(* Import Str module using this - #require "str" *) -let to_words s = Writer (Str.split (Str.regexp "\b") s, "to_words") +let up_case s = Writer (String.uppercase_ascii s, "up_case ") +let to_words s = Writer (String.split_on_char ' ' s, "to_words ") -module Writer_Process (W : Monad with type 'a m = (string, 'a) writer) = +module Writer_Process + (W : Monad with type 'a t = (string, 'a) writer) = struct - let process = W.(up_case >=> to_words) + open W + + let process = up_case >=> to_words end diff --git a/src/content/3.4/code/ocaml/snippet18.ml b/src/content/3.4/code/ocaml/snippet18.ml index f84566045..512520298 100644 --- a/src/content/3.4/code/ocaml/snippet18.ml +++ b/src/content/3.4/code/ocaml/snippet18.ml @@ -1,5 +1,5 @@ module Process_Do - (W : Monad_Bind with type 'a m = (string, 'a) writer) = + (W : Monad_Bind with type 'a t = (string, 'a) writer) = struct (* Needs OCaml compiler >= 4.08 *) let ( let* ) = W.( >>= ) diff --git a/src/content/3.4/code/ocaml/snippet19.ml b/src/content/3.4/code/ocaml/snippet19.ml index 1003c925e..d50bb0324 100644 --- a/src/content/3.4/code/ocaml/snippet19.ml +++ b/src/content/3.4/code/ocaml/snippet19.ml @@ -1 +1 @@ -let up_case s = Writer (String.uppercase s, "up_case ") +let up_case s = Writer (String.uppercase_ascii s, "up_case ") diff --git a/src/content/3.4/code/ocaml/snippet20.ml b/src/content/3.4/code/ocaml/snippet20.ml index b1a905487..92ad41821 100644 --- a/src/content/3.4/code/ocaml/snippet20.ml +++ b/src/content/3.4/code/ocaml/snippet20.ml @@ -1,5 +1,8 @@ module Process_Bind_Without_Do - (W : Monad_Bind with type 'a m = (string, 'a) writer) = + (W : Monad_Bind with type 'a t = (string, 'a) writer) = struct - let process s = W.(up_case s >>= fun up_str -> to_words up_str) + open W + + (* let* x = m in n is equivalent to m >>= fun x -> n *) + let process s = up_case s >>= fun up_str -> to_words up_str end diff --git a/src/content/3.4/code/ocaml/snippet21.ml b/src/content/3.4/code/ocaml/snippet21.ml index 21ea3a736..4736a4b48 100644 --- a/src/content/3.4/code/ocaml/snippet21.ml +++ b/src/content/3.4/code/ocaml/snippet21.ml @@ -1 +1 @@ -let* up_str <- up_case s +let* up_str = up_case s in diff --git a/src/content/3.4/code/ocaml/snippet22.ml b/src/content/3.4/code/ocaml/snippet22.ml index f53217ca1..2f234bd09 100644 --- a/src/content/3.4/code/ocaml/snippet22.ml +++ b/src/content/3.4/code/ocaml/snippet22.ml @@ -1,12 +1,17 @@ +(* Defining words and tell functions *) +let words s = String.split_on_char ' ' s +let tell w = Writer ((), w) + module Process_Tell - (W : Monad_Bind with type 'a m = (string, 'a) writer) = + (W : Monad_Bind with type 'a t = (string, 'a) writer) = struct + open W + (* Needs OCaml compiler >= 4.08 *) - let ( let* ) = W.( >>= ) - let tell w = Writer ((), w) + let ( let* ) = ( >>= ) let process s = let* up_str = up_case s in - let* _ = tell "to_words " in - to_words up_str + let* () = tell "to_words " in + return (words up_str) end diff --git a/src/content/3.4/code/ocaml/snippet23.ml b/src/content/3.4/code/ocaml/snippet23.ml index 88de8d41b..72a28839b 100644 --- a/src/content/3.4/code/ocaml/snippet23.ml +++ b/src/content/3.4/code/ocaml/snippet23.ml @@ -1,10 +1,9 @@ module Process_Bind_Without_Do - (W : Monad_Bind with type 'a m = (string, 'a) writer) = + (W : Monad_Bind with type 'a t = (string, 'a) writer) = struct - let tell w = Writer ((), w) + open W let process s = - W.( - up_case s - >>= fun up_str -> tell "to_words" >>= fun _ -> to_words up_str) + up_case s >>= fun up_str -> + tell "to_words" >>= fun () -> return (words up_str) end diff --git a/src/content/3.4/code/ocaml/snippet25.ml b/src/content/3.4/code/ocaml/snippet25.ml index 708a92242..18724d7ec 100644 --- a/src/content/3.4/code/ocaml/snippet25.ml +++ b/src/content/3.4/code/ocaml/snippet25.ml @@ -1,11 +1,10 @@ module Process_Bind_Without_Do - (W : Monad_Bind with type 'a m = (string, 'a) writer) = + (W : Monad_Bind with type 'a t = (string, 'a) writer) = struct + open W open Monad_Ops (W) let tell w = Writer ((), w) - let process s = - W.( - up_case s >>= fun up_str -> tell "to_words" >> to_words up_str) + up_case s >>= fun up_str -> tell "to_words" >> to_words up_str end From 16d42e6e04eb4860d683f880188ba67d992073df Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:23:15 +0100 Subject: [PATCH 20/27] Update Ch. 21 OCaml snippets --- src/content/3.5/code/ocaml/snippet01.ml | 11 +++++++---- src/content/3.5/code/ocaml/snippet02.ml | 2 +- src/content/3.5/code/ocaml/snippet03.ml | 23 ++++++++++++----------- src/content/3.5/code/ocaml/snippet04.ml | 6 +++--- src/content/3.5/code/ocaml/snippet05.ml | 14 +------------- src/content/3.5/code/ocaml/snippet12.ml | 7 +++---- src/content/3.5/code/ocaml/snippet15.ml | 10 ++++++---- src/content/3.5/code/ocaml/snippet19.ml | 7 +++---- src/content/3.5/code/ocaml/snippet22.ml | 5 ++--- src/content/3.5/code/ocaml/snippet28.ml | 1 - src/content/3.5/code/ocaml/snippet29.ml | 4 ++-- src/content/3.5/code/ocaml/snippet37.ml | 6 +++--- 12 files changed, 43 insertions(+), 53 deletions(-) diff --git a/src/content/3.5/code/ocaml/snippet01.ml b/src/content/3.5/code/ocaml/snippet01.ml index 4690b6f18..2dc0dfa31 100644 --- a/src/content/3.5/code/ocaml/snippet01.ml +++ b/src/content/3.5/code/ocaml/snippet01.ml @@ -1,6 +1,9 @@ -module List_Monad : Monad_Join = struct - type 'a m = 'a list +(* For infinite lists, like in Haskell, we use the [Seq] module. *) +module Seq_Monad : Monad_Join with type 'a t = 'a Seq.t = struct + type 'a t = 'a Seq.t - let join = List.concat - let return a = [ a ] + let join = Seq.concat + + (* [Seq] already has a [return] function. *) + let return a = Seq.return a end diff --git a/src/content/3.5/code/ocaml/snippet02.ml b/src/content/3.5/code/ocaml/snippet02.ml index 0220b7510..f5e9aa519 100644 --- a/src/content/3.5/code/ocaml/snippet02.ml +++ b/src/content/3.5/code/ocaml/snippet02.ml @@ -1 +1 @@ -let ( >>= ) xs k = List.concat (List.map k xs) +let ( >>= ) seq f = Seq.concat (Seq.map f seq) diff --git a/src/content/3.5/code/ocaml/snippet03.ml b/src/content/3.5/code/ocaml/snippet03.ml index 8245c2d0c..4c53566d5 100644 --- a/src/content/3.5/code/ocaml/snippet03.ml +++ b/src/content/3.5/code/ocaml/snippet03.ml @@ -1,12 +1,13 @@ -module Pythagorean = struct - let ( let* ) = Fn.flip Gen.flat_map - let ( let+ ) x f = Gen.map f x - let guard b = if b then Gen.return () else Gen.empty +(* Defining the bind operator for let* syntax sugar *) +let ( let* ) = ( >>= ) - let triples = - let* z = Gen.init (fun i -> i + 1) in - let* x = Gen.init ~limit:z (fun i -> i + 1) in - let* y = Gen.init ~limit:z (fun i -> i + x) in - let+ _ = guard ((x * x) + (y * y) = z * z) in - Gen.return (x, y, z) -end +(* The triples function can thus be defined as follows + using the bind operator we defined previously and the + return function from the [Seq] module, assuming that + the [guard] function is defined as well. *) +let triples = + let* z = Seq.ints 1 in + let* x = Seq.take z (Seq.ints 1) in + let* y = Seq.take (z - x) (Seq.ints x) in + let* () = guard ((x * x) + (y * y) = z * z) in + Seq.return (x, y, z) diff --git a/src/content/3.5/code/ocaml/snippet04.ml b/src/content/3.5/code/ocaml/snippet04.ml index c1e5237e4..c74b86471 100644 --- a/src/content/3.5/code/ocaml/snippet04.ml +++ b/src/content/3.5/code/ocaml/snippet04.ml @@ -1,3 +1,3 @@ -let guard = function - | true -> [ () ] - | false -> [] +let guard b = + if b then Seq.return () (* Analogous to [()] *) + else Seq.empty (* Analogous to [] *) diff --git a/src/content/3.5/code/ocaml/snippet05.ml b/src/content/3.5/code/ocaml/snippet05.ml index c02c2d052..d4dd3ed9f 100644 --- a/src/content/3.5/code/ocaml/snippet05.ml +++ b/src/content/3.5/code/ocaml/snippet05.ml @@ -1,13 +1 @@ -module Pythagorean = struct - let ( let* ) = Fn.flip Gen.flat_map - let ( let+ ) x f = Gen.map f x - let guard b = if b then Gen.return () else Gen.empty - - let triples = - let* z = Gen.init (fun i -> i + 1) in - let* x = Gen.init ~limit:z (fun i -> i + 1) in - let* y = Gen.init ~limit:z (fun i -> i + x) in - if (x * x) + (y * y) = z * z - then Gen.return (x, y, z) - else Gen.empty -end +(* There's no list comprehension in OCaml *) diff --git a/src/content/3.5/code/ocaml/snippet12.ml b/src/content/3.5/code/ocaml/snippet12.ml index 4d9836cf9..ed815a91e 100644 --- a/src/content/3.5/code/ocaml/snippet12.ml +++ b/src/content/3.5/code/ocaml/snippet12.ml @@ -1,10 +1,9 @@ module ReaderMonad (T : sig type t -end) : Monad_Bind = struct - type 'a m = (T.t, 'a) reader - - let return a = Reader (fun e -> a) +end) : Monad_Bind with type 'a t = (T.t, 'a) reader = struct + type 'a t = (T.t, 'a) reader let ( >>= ) ra k = Reader (fun e -> run_reader (k (run_reader ra e)) e) + let return a = Reader (fun e -> a) end diff --git a/src/content/3.5/code/ocaml/snippet15.ml b/src/content/3.5/code/ocaml/snippet15.ml index 61eaacd47..179f5a63c 100644 --- a/src/content/3.5/code/ocaml/snippet15.ml +++ b/src/content/3.5/code/ocaml/snippet15.ml @@ -1,9 +1,11 @@ -module WriterMonad (W : Monoid) : Monad_Bind = struct - type 'a m = (W.t, 'a) writer - - let return a = Writer (a, W.mempty) +module WriterMonad (W : Monoid) : Monad_Bind +with type 'a t = (W.t, 'a) writer = +struct + type 'a t = (W.t, 'a) writer let ( >>= ) (Writer (a, w)) k = let a', w' = run_writer (k a) in Writer (a', W.mappend w w') + + let return a = Writer (a, W.mempty) end diff --git a/src/content/3.5/code/ocaml/snippet19.ml b/src/content/3.5/code/ocaml/snippet19.ml index 94e7168ec..6fbd87405 100644 --- a/src/content/3.5/code/ocaml/snippet19.ml +++ b/src/content/3.5/code/ocaml/snippet19.ml @@ -1,7 +1,7 @@ module State_Monad (S : sig type t -end) : Monad_Bind = struct - type 'a m = (S.t, 'a) state +end) : Monad_Bind with type 'a t = (S.t, 'a) state = struct + type 'a t = (S.t, 'a) state let ( >>= ) sa k = State @@ -10,6 +10,5 @@ end) : Monad_Bind = struct let sb = k a in run_state sb s') - - let return a = State (fun s -> a, s) + let return a = State (fun s -> (a, s)) end diff --git a/src/content/3.5/code/ocaml/snippet22.ml b/src/content/3.5/code/ocaml/snippet22.ml index a1f11917f..9d65b6739 100644 --- a/src/content/3.5/code/ocaml/snippet22.ml +++ b/src/content/3.5/code/ocaml/snippet22.ml @@ -1,10 +1,9 @@ -module OptionMonad : Monad_Bind = struct - type 'a m = 'a option +module OptionMonad : Monad_Bind with type 'a t = 'a option = struct + type 'a t = 'a option let ( >>= ) = function | Some a -> fun k -> k a | None -> fun _ -> None - let return a = Some a end diff --git a/src/content/3.5/code/ocaml/snippet28.ml b/src/content/3.5/code/ocaml/snippet28.ml index 2532b0ef1..345ed9bcc 100644 --- a/src/content/3.5/code/ocaml/snippet28.ml +++ b/src/content/3.5/code/ocaml/snippet28.ml @@ -1,4 +1,3 @@ -;; run_cont ka (fun a -> let kb = kab a in run_cont kb hb) diff --git a/src/content/3.5/code/ocaml/snippet29.ml b/src/content/3.5/code/ocaml/snippet29.ml index cb0e92803..0ec2bdd7a 100644 --- a/src/content/3.5/code/ocaml/snippet29.ml +++ b/src/content/3.5/code/ocaml/snippet29.ml @@ -1,7 +1,7 @@ module Cont_Monad (R : sig type t -end) : Monad_Bind = struct - type 'a m = (R.t, 'a) cont +end) : Monad_Bind with type 'a t = (R.t, 'a) cont = struct + type 'a t = (R.t, 'a) cont let return a = Cont (fun ha -> ha a) diff --git a/src/content/3.5/code/ocaml/snippet37.ml b/src/content/3.5/code/ocaml/snippet37.ml index 2e27ec6f2..96dabf542 100644 --- a/src/content/3.5/code/ocaml/snippet37.ml +++ b/src/content/3.5/code/ocaml/snippet37.ml @@ -1,6 +1,6 @@ (* Monad implementation for type io *) -module IOMonad : Monad_Bind with type 'a m = 'a io = struct - type 'a m = 'a io +module IOMonad : Monad_Bind with type 'a t = 'a io = struct + type 'a t = 'a io let return x = IO (fun () -> x) @@ -17,6 +17,6 @@ module IO_Main = struct let ( let* ) = IOMonad.( >>= ) let main = - let* _ = put_str "Hello" in + let* () = put_str "Hello" in put_str "world!" end From 35d132e5a2cd04b95a4d46071d23e65528267f77 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:23:33 +0100 Subject: [PATCH 21/27] Update Ch. 22 OCaml snippets --- src/content/3.6/code/ocaml/snippet01.ml | 7 +++---- src/content/3.6/code/ocaml/snippet02.ml | 5 +++-- src/content/3.6/code/ocaml/snippet03.ml | 6 +++--- src/content/3.6/code/ocaml/snippet04.ml | 2 +- src/content/3.6/code/ocaml/snippet05.ml | 2 +- src/content/3.6/code/ocaml/snippet07.ml | 1 - src/content/3.6/code/ocaml/snippet08.ml | 3 +-- src/content/3.6/code/ocaml/snippet09.ml | 3 +-- src/content/3.6/code/ocaml/snippet10.ml | 3 +-- src/content/3.6/code/ocaml/snippet12.ml | 3 +-- src/content/3.6/code/ocaml/snippet13.ml | 1 - src/content/3.6/code/ocaml/snippet14.ml | 6 ++---- src/content/3.6/code/ocaml/snippet17.ml | 4 ++-- src/content/3.6/code/ocaml/snippet21.ml | 11 ++++------- src/content/3.6/code/ocaml/snippet25.ml | 7 ++++--- 15 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/content/3.6/code/ocaml/snippet01.ml b/src/content/3.6/code/ocaml/snippet01.ml index 215d19ab5..931f98efc 100644 --- a/src/content/3.6/code/ocaml/snippet01.ml +++ b/src/content/3.6/code/ocaml/snippet01.ml @@ -1,5 +1,4 @@ -module Kleisli (M : MonadJoin) = struct - (* compose *) - let ( <.> ) f g x = f (g x) - let ( >=> ) f g = M.join <.> M.fmap g <.> f +module Kleisli (M : Monad_Join) = struct + let ( % ) = Fun.compose + let ( >=> ) f g = M.join % M.fmap g % f end diff --git a/src/content/3.6/code/ocaml/snippet02.ml b/src/content/3.6/code/ocaml/snippet02.ml index 5c547e4d3..7b32af53a 100644 --- a/src/content/3.6/code/ocaml/snippet02.ml +++ b/src/content/3.6/code/ocaml/snippet02.ml @@ -1,3 +1,4 @@ -module Kleisli (M : MonadJoin) = struct - let ( >=> ) f g a = M.join (M.fmap g (f a)) +module Kleisli (M : Monad_Join) + (F : Functor with type 'a t = 'a M.t) = struct + let ( >=> ) f g a = M.join (F.fmap g (f a)) end diff --git a/src/content/3.6/code/ocaml/snippet03.ml b/src/content/3.6/code/ocaml/snippet03.ml index 22aab74e4..9b99b77b6 100644 --- a/src/content/3.6/code/ocaml/snippet03.ml +++ b/src/content/3.6/code/ocaml/snippet03.ml @@ -1,6 +1,6 @@ module type Monoid = sig - type m + type t - val mappend : m -> m -> m - val mempty : m + val mappend : t -> t -> t + val mempty : t end diff --git a/src/content/3.6/code/ocaml/snippet04.ml b/src/content/3.6/code/ocaml/snippet04.ml index 701322cf8..85d752730 100644 --- a/src/content/3.6/code/ocaml/snippet04.ml +++ b/src/content/3.6/code/ocaml/snippet04.ml @@ -1 +1 @@ -val mappend : m -> m -> m +val mappend : 'm -> ('m -> 'm) diff --git a/src/content/3.6/code/ocaml/snippet05.ml b/src/content/3.6/code/ocaml/snippet05.ml index 3bed3e3f9..65748389d 100644 --- a/src/content/3.6/code/ocaml/snippet05.ml +++ b/src/content/3.6/code/ocaml/snippet05.ml @@ -1 +1 @@ -val mu : (m, m) -> m +val mu : 'm * 'm -> m diff --git a/src/content/3.6/code/ocaml/snippet07.ml b/src/content/3.6/code/ocaml/snippet07.ml index ed7fb7bb6..8130cc81b 100644 --- a/src/content/3.6/code/ocaml/snippet07.ml +++ b/src/content/3.6/code/ocaml/snippet07.ml @@ -1,2 +1 @@ -;; mu (x, mu (y, z)) = mu (mu (x, y), z) diff --git a/src/content/3.6/code/ocaml/snippet08.ml b/src/content/3.6/code/ocaml/snippet08.ml index 07b560e77..fcb941fcc 100644 --- a/src/content/3.6/code/ocaml/snippet08.ml +++ b/src/content/3.6/code/ocaml/snippet08.ml @@ -1,2 +1 @@ -;; -(compose mu (bimap id mu)) (x, (y, z)) +(mu % bimap Fun.id mu) (x, (y, z)) diff --git a/src/content/3.6/code/ocaml/snippet09.ml b/src/content/3.6/code/ocaml/snippet09.ml index 0aa655823..74e9fb941 100644 --- a/src/content/3.6/code/ocaml/snippet09.ml +++ b/src/content/3.6/code/ocaml/snippet09.ml @@ -1,2 +1 @@ -;; -(compose mu (bimap mu id)) ((x, y), z) +(mu % bimap mu Fun.id) ((x, y), z) diff --git a/src/content/3.6/code/ocaml/snippet10.ml b/src/content/3.6/code/ocaml/snippet10.ml index 3e389a037..9c673ad42 100644 --- a/src/content/3.6/code/ocaml/snippet10.ml +++ b/src/content/3.6/code/ocaml/snippet10.ml @@ -1,2 +1 @@ -;; -compose mu (bimap id mu) = compose mu (bimap mu id) +mu % bimap Fun.id mu = mu % bimap mu Fun.id diff --git a/src/content/3.6/code/ocaml/snippet12.ml b/src/content/3.6/code/ocaml/snippet12.ml index 9f6cf8c28..dd71be4cb 100644 --- a/src/content/3.6/code/ocaml/snippet12.ml +++ b/src/content/3.6/code/ocaml/snippet12.ml @@ -1,2 +1 @@ -;; -compose mu (compose (bimap id mu) alpha) = compose mu (bimap mu id) +mu % bimap Fun.id mu % alpha = mu % bimap mu Fun.id diff --git a/src/content/3.6/code/ocaml/snippet13.ml b/src/content/3.6/code/ocaml/snippet13.ml index 913279578..e72c34e24 100644 --- a/src/content/3.6/code/ocaml/snippet13.ml +++ b/src/content/3.6/code/ocaml/snippet13.ml @@ -1,2 +1 @@ -;; mu (eta (), x) = x mu (x, eta ()) = x diff --git a/src/content/3.6/code/ocaml/snippet14.ml b/src/content/3.6/code/ocaml/snippet14.ml index 3116d4dc1..70665680a 100644 --- a/src/content/3.6/code/ocaml/snippet14.ml +++ b/src/content/3.6/code/ocaml/snippet14.ml @@ -1,4 +1,2 @@ -;; -(compose mu (bimap eta id)) ((), x) -= lambda ((), x) (compose mu (bimap id eta)) (x, ()) -= rho (x, ()) +(mu % bimap eta Fun.id) ((), x) = lambda ((), x) +(mu % bimap Fun.id eta) (x, ()) = rho (x, ()) diff --git a/src/content/3.6/code/ocaml/snippet17.ml b/src/content/3.6/code/ocaml/snippet17.ml index a3f9bc766..19a63919a 100644 --- a/src/content/3.6/code/ocaml/snippet17.ml +++ b/src/content/3.6/code/ocaml/snippet17.ml @@ -1,2 +1,2 @@ -;; -mu.bimap id eta = rho mu.bimap eta id = lambda +mu % bimap Fun.id eta = rho +mu % bimap eta Fun.id = lambda diff --git a/src/content/3.6/code/ocaml/snippet21.ml b/src/content/3.6/code/ocaml/snippet21.ml index 738d6d422..df8261869 100644 --- a/src/content/3.6/code/ocaml/snippet21.ml +++ b/src/content/3.6/code/ocaml/snippet21.ml @@ -1,15 +1,12 @@ module AdjunctionState (S : sig type s -end) -(F : Functor with type 'a t = (S.s, 'a) prod) -(R : Representable with type 'a t = (S.s, 'a) reader) : Adjunction = +end) : + Adjunction with type 'a f = (S.s, 'a) prod + and type 'a r = (S.s, 'a) reader = struct type 'a f = (S.s, 'a) prod type 'a r = (S.s, 'a) reader - include F - include R - - let unit a = Reader (fun s -> Prod (a, s)) let counit (Prod (Reader f, s)) = f s + let unit a = Reader (fun s -> Prod (a, s)) end diff --git a/src/content/3.6/code/ocaml/snippet25.ml b/src/content/3.6/code/ocaml/snippet25.ml index 1a557e487..1987d6752 100644 --- a/src/content/3.6/code/ocaml/snippet25.ml +++ b/src/content/3.6/code/ocaml/snippet25.ml @@ -1,3 +1,4 @@ -let join : ('s, ('s, 'a) state) state -> ('s, 'a) state = - fun ssa -> State (uncurry run_state <.> run_state ssa) -;; +let uncurry f (a, b) = f a b + +let join (ssa : ('s, ('s, 'a) state) state) : ('s, 'a) state = + State (uncurry run_state % run_state ssa) From 3db8b2bf9668a8e84679022bf2da1f1ca3347f62 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:23:50 +0100 Subject: [PATCH 22/27] Update Ch. 23 OCaml snippets --- src/content/3.7/code/ocaml/snippet09.ml | 1 - src/content/3.7/code/ocaml/snippet13.ml | 5 ++--- src/content/3.7/code/ocaml/snippet17.ml | 6 ++---- src/content/3.7/code/ocaml/snippet19.ml | 1 - src/content/3.7/code/ocaml/snippet20.ml | 1 - src/content/3.7/code/ocaml/snippet22.ml | 1 - src/content/3.7/code/ocaml/snippet28.ml | 12 ++---------- src/content/3.7/code/ocaml/snippet29.ml | 1 - 8 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/content/3.7/code/ocaml/snippet09.ml b/src/content/3.7/code/ocaml/snippet09.ml index ba933092e..1a82bdf61 100644 --- a/src/content/3.7/code/ocaml/snippet09.ml +++ b/src/content/3.7/code/ocaml/snippet09.ml @@ -2,4 +2,3 @@ let ( =>= ) f g (P (e, a)) = let b = f (P (e, a)) in let c = g (P (e, b)) in c -;; diff --git a/src/content/3.7/code/ocaml/snippet13.ml b/src/content/3.7/code/ocaml/snippet13.ml index d4d961eb0..f20958c0a 100644 --- a/src/content/3.7/code/ocaml/snippet13.ml +++ b/src/content/3.7/code/ocaml/snippet13.ml @@ -7,7 +7,6 @@ end module CoKleisliImpl (C : CoKleisliExtend) = struct type 'a w = 'a C.w - let ( =>= ) : ('a w -> 'b) -> ('b w -> 'c) -> 'a w -> 'c = - fun f g -> compose g (C.extend f) - ;; + let ( =>= ) (f : 'a w -> 'b) (g : 'b w -> 'c) : 'a w -> 'c = + Fun.compose g (C.extend f) end diff --git a/src/content/3.7/code/ocaml/snippet17.ml b/src/content/3.7/code/ocaml/snippet17.ml index ce2860a82..f6a056c33 100644 --- a/src/content/3.7/code/ocaml/snippet17.ml +++ b/src/content/3.7/code/ocaml/snippet17.ml @@ -1,8 +1,6 @@ module StreamFunctor : Functor with type 'a t = 'a stream = struct type 'a t = 'a stream - let rec fmap f = function - | Cons (x, xs) -> - Cons (f x, Lazy.from_val (fmap f (Lazy.force xs))) - ;; + let rec fmap f (Cons (x, xs)) = + Cons (f x, Lazy.from_val (fmap f (Lazy.force xs))) end diff --git a/src/content/3.7/code/ocaml/snippet19.ml b/src/content/3.7/code/ocaml/snippet19.ml index f6bb8aea8..0c62fcbf9 100644 --- a/src/content/3.7/code/ocaml/snippet19.ml +++ b/src/content/3.7/code/ocaml/snippet19.ml @@ -1,3 +1,2 @@ let rec duplicate (Cons (x, xs)) = Cons (Cons (x, xs), Lazy.from_val (duplicate (Lazy.force xs))) -;; diff --git a/src/content/3.7/code/ocaml/snippet20.ml b/src/content/3.7/code/ocaml/snippet20.ml index 672004c3b..ef76a6dfd 100644 --- a/src/content/3.7/code/ocaml/snippet20.ml +++ b/src/content/3.7/code/ocaml/snippet20.ml @@ -15,7 +15,6 @@ module StreamComonadDuplicate : let rec duplicate (Cons (x, xs)) = Cons (Cons (x, xs), Lazy.from_val (duplicate (Lazy.force xs))) - ;; end (* Generate full Comonad Instance *) diff --git a/src/content/3.7/code/ocaml/snippet22.ml b/src/content/3.7/code/ocaml/snippet22.ml index fe1c591b7..295b150df 100644 --- a/src/content/3.7/code/ocaml/snippet22.ml +++ b/src/content/3.7/code/ocaml/snippet22.ml @@ -1,3 +1,2 @@ let rec sum_s n (Cons (x, xs)) = if n <= 0 then 0 else x + sum_s (n - 1) (Lazy.force xs) -;; diff --git a/src/content/3.7/code/ocaml/snippet28.ml b/src/content/3.7/code/ocaml/snippet28.ml index c51071a4b..7fe0a4d0f 100644 --- a/src/content/3.7/code/ocaml/snippet28.ml +++ b/src/content/3.7/code/ocaml/snippet28.ml @@ -1,11 +1,3 @@ (* lambda is the left unitor and rho is the right unitor *) -(* <.> is used as compose below *) - -;; -lambda -<.> bimap destroy id -<.> split -= id rho -<.> bimap id destroy -<.> split -= id +lambda % bimap destroy id % split = id +rho % bimap id destroy % split = id diff --git a/src/content/3.7/code/ocaml/snippet29.ml b/src/content/3.7/code/ocaml/snippet29.ml index 739b7c7e2..25542c56e 100644 --- a/src/content/3.7/code/ocaml/snippet29.ml +++ b/src/content/3.7/code/ocaml/snippet29.ml @@ -1,4 +1,3 @@ -;; lambda (bimap destroy id (split x)) = lambda (bimap destroy id (f x, g x)) = lambda ((), g x) From 9d56fe854d6d09c3049cc78f075a848c5756a783 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:24:16 +0100 Subject: [PATCH 23/27] Update Ch. 24 OCaml snippets --- src/content/3.8/code/ocaml/snippet04.ml | 1 - src/content/3.8/code/ocaml/snippet06.ml | 1 - src/content/3.8/code/ocaml/snippet10.ml | 4 ++-- src/content/3.8/code/ocaml/snippet11.ml | 4 ++-- src/content/3.8/code/ocaml/snippet12.ml | 6 +++--- src/content/3.8/code/ocaml/snippet13.ml | 15 ++++++++++++--- src/content/3.8/code/ocaml/snippet16.ml | 12 +++--------- src/content/3.8/code/ocaml/snippet18.ml | 1 - src/content/3.8/code/ocaml/snippet21.ml | 1 - src/content/3.8/code/ocaml/snippet23.ml | 1 - src/content/3.8/code/ocaml/snippet25.ml | 10 ++++------ src/content/3.8/code/ocaml/snippet26.ml | 25 +++++++++++++++++-------- src/content/3.8/code/ocaml/snippet28.ml | 22 ++++++++++++---------- src/content/3.8/code/ocaml/snippet29.ml | 23 +++++++++++++++-------- src/content/3.8/code/ocaml/snippet30.ml | 18 ++++++------------ src/content/3.8/code/ocaml/snippet31.ml | 4 +--- src/content/3.8/code/ocaml/snippet33.ml | 2 +- 17 files changed, 78 insertions(+), 72 deletions(-) diff --git a/src/content/3.8/code/ocaml/snippet04.ml b/src/content/3.8/code/ocaml/snippet04.ml index 8566e9502..a0212625a 100644 --- a/src/content/3.8/code/ocaml/snippet04.ml +++ b/src/content/3.8/code/ocaml/snippet04.ml @@ -9,5 +9,4 @@ module Ring = struct | RAdd (m, n) -> m + n | RMul (m, n) -> m * n | RNeg n -> -n - ;; end diff --git a/src/content/3.8/code/ocaml/snippet06.ml b/src/content/3.8/code/ocaml/snippet06.ml index d35256197..37f496df8 100644 --- a/src/content/3.8/code/ocaml/snippet06.ml +++ b/src/content/3.8/code/ocaml/snippet06.ml @@ -4,4 +4,3 @@ let rec eval_z : expr -> int = function | RAdd (e1, e2) -> eval_z e1 + eval_z e2 | RMul (e1, e2) -> eval_z e1 * eval_z e2 | RNeg e -> -eval_z e -;; diff --git a/src/content/3.8/code/ocaml/snippet10.ml b/src/content/3.8/code/ocaml/snippet10.ml index c57c26f78..63dd10e1e 100644 --- a/src/content/3.8/code/ocaml/snippet10.ml +++ b/src/content/3.8/code/ocaml/snippet10.ml @@ -1,3 +1,3 @@ -module Fix (F : Functor) = struct - type 'a fix = Fix of 'a fix F.t +module Fixpoint (F : Functor) = struct + type 'a t = Fix of 'a t F.t end diff --git a/src/content/3.8/code/ocaml/snippet11.ml b/src/content/3.8/code/ocaml/snippet11.ml index 34a157fed..9fcb9bda6 100644 --- a/src/content/3.8/code/ocaml/snippet11.ml +++ b/src/content/3.8/code/ocaml/snippet11.ml @@ -1,3 +1,3 @@ -module Fix (F : Functor) = struct - type 'a fix = In of 'a fix F.t +module Fixpoint (F : Functor) = struct + type 'a t = In of 'a t F.t end diff --git a/src/content/3.8/code/ocaml/snippet12.ml b/src/content/3.8/code/ocaml/snippet12.ml index 4e62e65b5..6017c0762 100644 --- a/src/content/3.8/code/ocaml/snippet12.ml +++ b/src/content/3.8/code/ocaml/snippet12.ml @@ -1,5 +1,5 @@ -module Fix (F : Functor) = struct - type 'a fix = Fix of 'a fix F.t +module Fixpoint (F : Functor) = struct + type 'a t = Fix of 'a t F.t - let fix : 'a fix F.t -> 'a fix = fun f -> Fix f + let fix (f : 'a t F.t) : 'a t = Fix f end diff --git a/src/content/3.8/code/ocaml/snippet13.ml b/src/content/3.8/code/ocaml/snippet13.ml index 6cb967281..d389bc90d 100644 --- a/src/content/3.8/code/ocaml/snippet13.ml +++ b/src/content/3.8/code/ocaml/snippet13.ml @@ -1,5 +1,14 @@ -module Fix (F : Functor) = struct - type 'a fix = Fix of 'a fix F.t +module Fixpoint (F : Functor) = struct + type 'a t = Fix of 'a t F.t - let unfix : 'a fix -> 'a fix F.t = fun (Fix f) -> f + let unfix (Fix f) : 'a t F.t = f +end + +(* For a complete type signature, it could be something like: *) +module type Fixpoint = sig + type 'a funct + type 'a t = Fix of 'a t funct + + val fix : 'a t funct -> 'a t + val unfix : 'a t -> 'a t funct end diff --git a/src/content/3.8/code/ocaml/snippet16.ml b/src/content/3.8/code/ocaml/snippet16.ml index 322e356a8..1695cdae0 100644 --- a/src/content/3.8/code/ocaml/snippet16.ml +++ b/src/content/3.8/code/ocaml/snippet16.ml @@ -1,10 +1,4 @@ -module Cata (F : Functor) = struct - type 'a fix = Fix of 'a fix F.t - - let fix : 'a fix F.t -> 'a fix = fun f -> Fix f - let unfix : 'a fix -> 'a fix F.t = fun (Fix f) -> f - - let rec cata : ('a F.t -> 'a) -> 'a fix -> 'a = - fun alg fixf -> alg (F.fmap (cata alg) (unfix fixf)) - ;; +module Cata (F : Functor) (Fix : Fixpoint with type 'a funct = 'a F.t) = struct + let rec cata (alg : 'a F.t -> 'a) (fixf : 'a Fix.t) : 'a = + alg (F.fmap (cata alg) (Fix.unfix fixf)) end diff --git a/src/content/3.8/code/ocaml/snippet18.ml b/src/content/3.8/code/ocaml/snippet18.ml index 2252f09de..2b6600893 100644 --- a/src/content/3.8/code/ocaml/snippet18.ml +++ b/src/content/3.8/code/ocaml/snippet18.ml @@ -1,4 +1,3 @@ let rec fib = function | ZeroF -> 1, 1 | SuccF (m, n) -> n, m + n -;; diff --git a/src/content/3.8/code/ocaml/snippet21.ml b/src/content/3.8/code/ocaml/snippet21.ml index 6513583fb..c069e5ec6 100644 --- a/src/content/3.8/code/ocaml/snippet21.ml +++ b/src/content/3.8/code/ocaml/snippet21.ml @@ -1,4 +1,3 @@ let len_alg = function | ConsF (e, n) -> n + 1 | NilF -> 0 -;; diff --git a/src/content/3.8/code/ocaml/snippet23.ml b/src/content/3.8/code/ocaml/snippet23.ml index 1bfaf81cd..d9a3ed91e 100644 --- a/src/content/3.8/code/ocaml/snippet23.ml +++ b/src/content/3.8/code/ocaml/snippet23.ml @@ -1,4 +1,3 @@ let sum_alg = function | ConsF (e, s) -> e +. s | NilF -> 0.0 -;; diff --git a/src/content/3.8/code/ocaml/snippet25.ml b/src/content/3.8/code/ocaml/snippet25.ml index 73c751719..737c3b53f 100644 --- a/src/content/3.8/code/ocaml/snippet25.ml +++ b/src/content/3.8/code/ocaml/snippet25.ml @@ -1,7 +1,5 @@ -module Ana (F : Functor) = struct - type 'a fix = Fix of 'a fix F.t - - let rec ana : ('a -> 'a F.t) -> 'a -> 'a fix = - fun coalg a -> Fix (F.fmap (ana coalg) (coalg a)) - ;; +module Ana (F : Functor) (Fix : Fixpoint with type 'a funct = 'a F.t) = +struct + let rec ana (coalg : 'a -> 'a F.t) (a : 'a) : 'a Fix.t = + Fix.Fix (F.fmap (fun x -> ana coalg x) (coalg a)) end diff --git a/src/content/3.8/code/ocaml/snippet26.ml b/src/content/3.8/code/ocaml/snippet26.ml index 5bea72c78..08d407491 100644 --- a/src/content/3.8/code/ocaml/snippet26.ml +++ b/src/content/3.8/code/ocaml/snippet26.ml @@ -1,11 +1,20 @@ -type ('e, 'a) stream_f = StreamF of ('e * 'a) +(* OCaml is strict by default, so to prevent hanging due + to the streams being infinite, we make them lazy. *) +type ('e, 'a) stream_f = StreamF of ('e * 'a Lazy.t) -module Stream_Functor (E : sig - type e -end) : Functor with type 'a t = (E.e, 'a) stream_f = struct - type 'a t = (E.e, 'a) stream_f +(* Wrapper for parameterized stream *) +module type Type = sig + type t +end + +(* The stream functor could be thus defined as follows: *) +module Stream_Functor (E : Type) : Functor +with type 'a t = (E.t, 'a) stream_f = +struct + type 'a t = (E.t, 'a) stream_f - let fmap f = function - | StreamF (e, a) -> StreamF (e, f a) - ;; + let fmap f (StreamF (e, a)) = StreamF (e, lazy (f (Lazy.force a))) end + +(* And applied to integers *) +module Stream_Functor_Int = Stream_Functor (Int) diff --git a/src/content/3.8/code/ocaml/snippet28.ml b/src/content/3.8/code/ocaml/snippet28.ml index a757760ed..b862a07a4 100644 --- a/src/content/3.8/code/ocaml/snippet28.ml +++ b/src/content/3.8/code/ocaml/snippet28.ml @@ -1,10 +1,12 @@ -(* OCaml library `gen` provides useful helpers for - potentially infinite iterators. You can install it - with `opam install gen`. To use it in the toplevel, - you need to `#require "gen"` *) -let era : int Gen.t -> (int, int Gen.t) stream_f = - fun ilist -> - let notdiv p n = n mod p != 0 in - let p = Gen.get_exn ilist in - StreamF (p, Gen.filter (notdiv p) ilist) -;; +(* Extract the head of a sequence *) +let seq_hd_exn seq = + match seq () with + | Seq.Nil -> failwith "Empty sequence" + | Seq.Cons (p, ns) -> p + +(* For Haskell-like infinite lists, + we'll use OCaml's sequences instead *) +let era (seq : int Seq.t) : (int, int Seq.t) stream_f = + let notdiv p n = n mod p <> 0 in + let p = seq_hd_exn seq in + StreamF (p, lazy (Seq.filter (notdiv p) seq)) diff --git a/src/content/3.8/code/ocaml/snippet29.ml b/src/content/3.8/code/ocaml/snippet29.ml index 09ad9e290..e8d0cb770 100644 --- a/src/content/3.8/code/ocaml/snippet29.ml +++ b/src/content/3.8/code/ocaml/snippet29.ml @@ -1,10 +1,17 @@ -module Stream_Int = Stream_Functor (struct - type e = int -end) +(* Using the previous signature for Fixpoint *) +module Fixpoint (F : Functor) : Fixpoint +with type 'a funct = 'a F.t = struct + type 'a funct = 'a F.t + type 'a t = Fix of 'a t funct -module Ana_Stream = Ana (Stream_Int) + let fix (f : 'a t funct) : 'a t = Fix f + let unfix (Fix f) : 'a t funct = f +end -(* The fixpoint translated to OCaml is eager in its evaluation. -Hence, executing the following function will cause overflow. -So, wrapping it inside a lazy *) -let primes = lazy (Ana_Stream.ana era (Gen.init (fun i -> i + 2))) +(* Fixpoint for an int stream *) +module Fix = Fixpoint (Stream_Functor_Int) + +(* We can make the Ana module for an int stream *) +module Stream_Ana = Ana (Stream_Functor_Int) (Fix) + +let primes = Stream_Ana.ana era (Seq.ints 2) diff --git a/src/content/3.8/code/ocaml/snippet30.ml b/src/content/3.8/code/ocaml/snippet30.ml index 869e9e003..efa5c0b28 100644 --- a/src/content/3.8/code/ocaml/snippet30.ml +++ b/src/content/3.8/code/ocaml/snippet30.ml @@ -1,14 +1,8 @@ -module List_C (E : sig - type e -end) = -struct - module Stream_F : Functor with type 'a t = (E.e, 'a) stream_f = - Stream_Functor (E) +(* We can also make the Cata module for an int stream *) +module Stream_Cata = Cata (Stream_Functor_Int) (Fix) - module Cata_Stream = Cata (Stream_F) +let to_seq_c : int Seq.t Fix.t -> int Seq.t = + let al (StreamF (e, a)) = fun () -> Seq.Cons (e, Lazy.force a) in + Stream_Cata.cata al - let to_list_c : E.e list Cata_Stream.fix -> E.e list = - fun s_fix -> - Cata_Stream.cata (fun (StreamF (e, a)) -> e :: a) s_fix - ;; -end +let list_primes = to_seq_c primes diff --git a/src/content/3.8/code/ocaml/snippet31.ml b/src/content/3.8/code/ocaml/snippet31.ml index f0d36662b..32e80a0c8 100644 --- a/src/content/3.8/code/ocaml/snippet31.ml +++ b/src/content/3.8/code/ocaml/snippet31.ml @@ -1,3 +1 @@ -(* Gen.t is used to represent infinite data structures like haskell's - lazy list *) -val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a Gen.t +val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a Seq.t diff --git a/src/content/3.8/code/ocaml/snippet33.ml b/src/content/3.8/code/ocaml/snippet33.ml index caf74ebeb..73168888d 100644 --- a/src/content/3.8/code/ocaml/snippet33.ml +++ b/src/content/3.8/code/ocaml/snippet33.ml @@ -1 +1 @@ -(a, (s, s -> a)) +'a -> ('s * 's -> 'a) From 33e0b25a4dedeea47f07fe45fcd37213f8523f22 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:24:47 +0100 Subject: [PATCH 24/27] Update Ch. 25 OCaml snippets --- src/content/3.9/code/ocaml/snippet01.ml | 4 ++-- src/content/3.9/code/ocaml/snippet03.ml | 4 +--- src/content/3.9/code/ocaml/snippet04.ml | 1 - src/content/3.9/code/ocaml/snippet05.ml | 8 ++++++-- src/content/3.9/code/ocaml/snippet06.ml | 2 +- src/content/3.9/code/ocaml/snippet08.ml | 1 - src/content/3.9/code/ocaml/snippet09.ml | 2 +- src/content/3.9/code/ocaml/snippet10.ml | 5 +---- src/content/3.9/code/ocaml/snippet11.ml | 1 - src/content/3.9/code/ocaml/snippet12.ml | 2 +- src/content/3.9/code/ocaml/snippet13.ml | 3 --- src/content/3.9/code/ocaml/snippet14.ml | 1 - src/content/3.9/code/ocaml/snippet15.ml | 1 - 13 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/content/3.9/code/ocaml/snippet01.ml b/src/content/3.9/code/ocaml/snippet01.ml index ace36491d..a87d590f1 100644 --- a/src/content/3.9/code/ocaml/snippet01.ml +++ b/src/content/3.9/code/ocaml/snippet01.ml @@ -1,2 +1,2 @@ -;; -compose alg return = id compose alg join = compose alg (fmap alg) +alg % return = Fun.id +alg % join = alg % fmap alg diff --git a/src/content/3.9/code/ocaml/snippet03.ml b/src/content/3.9/code/ocaml/snippet03.ml index 35f550129..670165780 100644 --- a/src/content/3.9/code/ocaml/snippet03.ml +++ b/src/content/3.9/code/ocaml/snippet03.ml @@ -1,4 +1,2 @@ -(* List module in the OCaml standard library accepts list before z *) - -;; +(* In the [List] module, [ x ] is taken before z *) List.fold_right f [ x ] z = f x z diff --git a/src/content/3.9/code/ocaml/snippet04.ml b/src/content/3.9/code/ocaml/snippet04.ml index db7bea0ac..2b297e9e3 100644 --- a/src/content/3.9/code/ocaml/snippet04.ml +++ b/src/content/3.9/code/ocaml/snippet04.ml @@ -1,2 +1 @@ -;; f x z = x diff --git a/src/content/3.9/code/ocaml/snippet05.ml b/src/content/3.9/code/ocaml/snippet05.ml index 8152bba36..96848731f 100644 --- a/src/content/3.9/code/ocaml/snippet05.ml +++ b/src/content/3.9/code/ocaml/snippet05.ml @@ -1,3 +1,7 @@ -module Kleisli_Composition (T : MonadJoin) = struct - let h g f = T.join <.> T.fmap g <.> f +module Kleisli_Composition + (T : Monad_Join) + (F : Functor with type 'a t = 'a T.t) = +struct + let ( % ) = Fun.compose + let h g f = T.join % F.fmap g % f end diff --git a/src/content/3.9/code/ocaml/snippet06.ml b/src/content/3.9/code/ocaml/snippet06.ml index 59db33e16..b78055180 100644 --- a/src/content/3.9/code/ocaml/snippet06.ml +++ b/src/content/3.9/code/ocaml/snippet06.ml @@ -1,3 +1,3 @@ module C_to_CT (T : Monad) = struct - let on_objects = T.return <.> f + let on_objects f = Fun.compose T.return f end diff --git a/src/content/3.9/code/ocaml/snippet08.ml b/src/content/3.9/code/ocaml/snippet08.ml index a927377a0..dd6aaaabd 100644 --- a/src/content/3.9/code/ocaml/snippet08.ml +++ b/src/content/3.9/code/ocaml/snippet08.ml @@ -11,5 +11,4 @@ end) let duplicate : 'a w -> 'a w w = fun (Store (f, s)) -> Store ((fun s -> Store (f, s)), s) - ;; end diff --git a/src/content/3.9/code/ocaml/snippet09.ml b/src/content/3.9/code/ocaml/snippet09.ml index f9cd35225..2a46e686c 100644 --- a/src/content/3.9/code/ocaml/snippet09.ml +++ b/src/content/3.9/code/ocaml/snippet09.ml @@ -4,5 +4,5 @@ end) : Functor with type 'a t = (S.s, 'a) store = struct type 'a w = (S.s, 'a) store type 'a t = 'a w - let fmap g (Store (f, s)) = Store (compose g f, s) + let fmap g (Store (f, s)) = Store (Fun.compose g f, s) end diff --git a/src/content/3.9/code/ocaml/snippet10.ml b/src/content/3.9/code/ocaml/snippet10.ml index 6f8f3106e..6462b132a 100644 --- a/src/content/3.9/code/ocaml/snippet10.ml +++ b/src/content/3.9/code/ocaml/snippet10.ml @@ -1,4 +1 @@ -(* Assume <.> acts as compose *) - -;; -Store (coalg_store <.> set a, get a) +Store (coalg_store % set a, get a) diff --git a/src/content/3.9/code/ocaml/snippet11.ml b/src/content/3.9/code/ocaml/snippet11.ml index 59543f7ef..25c834961 100644 --- a/src/content/3.9/code/ocaml/snippet11.ml +++ b/src/content/3.9/code/ocaml/snippet11.ml @@ -1,2 +1 @@ -;; Store ((fun s -> Store (set a, s)), get a) diff --git a/src/content/3.9/code/ocaml/snippet12.ml b/src/content/3.9/code/ocaml/snippet12.ml index 60a80262d..88aef0b73 100644 --- a/src/content/3.9/code/ocaml/snippet12.ml +++ b/src/content/3.9/code/ocaml/snippet12.ml @@ -1,2 +1,2 @@ (* Pseudo OCaml *) -let coalg_store (set a s) = Store ((set a), s) +coalg_store (set a s) = Store (set a, s) diff --git a/src/content/3.9/code/ocaml/snippet13.ml b/src/content/3.9/code/ocaml/snippet13.ml index a5dcc0f68..ffad60999 100644 --- a/src/content/3.9/code/ocaml/snippet13.ml +++ b/src/content/3.9/code/ocaml/snippet13.ml @@ -1,4 +1 @@ -(* Expaning coalg_store *) - -;; Store (set (set a s), get (set a s)) = Store (set a, s) diff --git a/src/content/3.9/code/ocaml/snippet14.ml b/src/content/3.9/code/ocaml/snippet14.ml index f9fecb072..1da1284e7 100644 --- a/src/content/3.9/code/ocaml/snippet14.ml +++ b/src/content/3.9/code/ocaml/snippet14.ml @@ -1,2 +1 @@ -;; set (set a s) = set a diff --git a/src/content/3.9/code/ocaml/snippet15.ml b/src/content/3.9/code/ocaml/snippet15.ml index 62ce811cb..13534aa91 100644 --- a/src/content/3.9/code/ocaml/snippet15.ml +++ b/src/content/3.9/code/ocaml/snippet15.ml @@ -1,2 +1 @@ -;; get (set a s) = s From cca3f37ba0a9462c05259b4ed9342e2382da61cc Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:25:12 +0100 Subject: [PATCH 25/27] Update Ch. 26 OCaml snippets --- src/content/3.10/code/ocaml/snippet01.ml | 12 ++---------- src/content/3.10/code/ocaml/snippet02.ml | 2 +- src/content/3.10/code/ocaml/snippet03.ml | 2 +- src/content/3.10/code/ocaml/snippet04.ml | 5 +---- src/content/3.10/code/ocaml/snippet06.ml | 3 +-- src/content/3.10/code/ocaml/snippet08.ml | 4 ++-- src/content/3.10/code/ocaml/snippet09.ml | 10 ++++------ src/content/3.10/code/ocaml/snippet10.ml | 4 ++-- src/content/3.10/code/ocaml/snippet11.ml | 4 ++-- src/content/3.10/code/ocaml/snippet12.ml | 8 +++----- src/content/3.10/code/ocaml/snippet14.ml | 2 +- src/content/3.10/code/ocaml/snippet16.ml | 12 ++++-------- 12 files changed, 24 insertions(+), 44 deletions(-) diff --git a/src/content/3.10/code/ocaml/snippet01.ml b/src/content/3.10/code/ocaml/snippet01.ml index 0acb65030..65056e194 100644 --- a/src/content/3.10/code/ocaml/snippet01.ml +++ b/src/content/3.10/code/ocaml/snippet01.ml @@ -1,13 +1,5 @@ -module type Functor = sig - type 'a t - - val fmap : ('a -> 'b) -> 'a t -> 'b t -end - module type Profunctor = sig - type ('a, 'b) p + type ('a, 'b) t - val dimap : ('c -> 'a) -> ('b -> 'd) -> ('a, 'b) p -> ('c, 'd) p + val dimap : ('c -> 'a) -> ('b -> 'd) -> ('a, 'b) t -> ('c, 'd) t end - -let id a = a diff --git a/src/content/3.10/code/ocaml/snippet02.ml b/src/content/3.10/code/ocaml/snippet02.ml index cf511509d..4b9e74b44 100644 --- a/src/content/3.10/code/ocaml/snippet02.ml +++ b/src/content/3.10/code/ocaml/snippet02.ml @@ -1 +1 @@ -let dimap f id (P (b, b)) : ('a, 'b) p +val dimap f Fun.id (P (b, b)) : ('a, 'b) p diff --git a/src/content/3.10/code/ocaml/snippet03.ml b/src/content/3.10/code/ocaml/snippet03.ml index 90ec92ad8..8a97e9665 100644 --- a/src/content/3.10/code/ocaml/snippet03.ml +++ b/src/content/3.10/code/ocaml/snippet03.ml @@ -1 +1 @@ -let dimap id f (P (a, a)) : ('a, 'b) p +val dimap Fun.id f (P (a, a)) : ('a, 'b) p diff --git a/src/content/3.10/code/ocaml/snippet04.ml b/src/content/3.10/code/ocaml/snippet04.ml index 4a8d89de0..3ff7cdd5d 100644 --- a/src/content/3.10/code/ocaml/snippet04.ml +++ b/src/content/3.10/code/ocaml/snippet04.ml @@ -1,4 +1 @@ -(* There is no compose operator in OCaml *) - -;; -compose (dimap id f) alpha = compose (dimap f id) alpha +dimap Fun.id f % alpha = dimap f Fun.id % alpha diff --git a/src/content/3.10/code/ocaml/snippet06.ml b/src/content/3.10/code/ocaml/snippet06.ml index fe9e8d6cc..7b7202241 100644 --- a/src/content/3.10/code/ocaml/snippet06.ml +++ b/src/content/3.10/code/ocaml/snippet06.ml @@ -1,2 +1 @@ -;; -compose (dimap f id) pi = compose (dimap id f) pi +dimap f id % pi = dimap id f % pi diff --git a/src/content/3.10/code/ocaml/snippet08.ml b/src/content/3.10/code/ocaml/snippet08.ml index 010af1fa3..cd76eb3a2 100644 --- a/src/content/3.10/code/ocaml/snippet08.ml +++ b/src/content/3.10/code/ocaml/snippet08.ml @@ -1,5 +1,5 @@ module Pi (P : Profunctor) = struct - type rank2_p = { p : 'a. ('a, 'a) P.p } + type rank2_p = { p : 'a. ('a, 'a) P.t } - let pi : rank2_p -> ('c, 'c) P.p = fun e -> e.p + let pi (e : rank2_p) : ('c, 'c) P.t = e.p end diff --git a/src/content/3.10/code/ocaml/snippet09.ml b/src/content/3.10/code/ocaml/snippet09.ml index d9f7f77ed..51345fba2 100644 --- a/src/content/3.10/code/ocaml/snippet09.ml +++ b/src/content/3.10/code/ocaml/snippet09.ml @@ -1,9 +1,7 @@ module EndsEqualizer (P : Profunctor) = struct - let lambda : ('a, 'a) P.p -> ('a -> 'b) -> ('a, 'b) P.p = - fun paa f -> P.dimap id f paa - ;; + let lambda (paa : ('a, 'a) P.t) (f : 'a -> 'b) : ('a, 'b) P.t = + P.dimap Fun.id f paa - let rho : ('b, 'b) P.p -> ('a -> 'b) -> ('a, 'b) P.p = - fun pbb f -> P.dimap f id pbb - ;; + let rho (pbb : ('b, 'b) P.t) (f : 'a -> 'b) : ('a, 'b) P.t = + P.dimap f Fun.id pbb end diff --git a/src/content/3.10/code/ocaml/snippet10.ml b/src/content/3.10/code/ocaml/snippet10.ml index e4c5bd386..67f92c7c5 100644 --- a/src/content/3.10/code/ocaml/snippet10.ml +++ b/src/content/3.10/code/ocaml/snippet10.ml @@ -1,4 +1,4 @@ module type ProdP = sig - type ('a, 'b) p - type ('a, 'b) prod_p = ('a -> 'b) -> ('a, 'b) p + type ('a, 'b) t + type ('a, 'b) prod_p = ('a -> 'b) -> ('a, 'b) t end diff --git a/src/content/3.10/code/ocaml/snippet11.ml b/src/content/3.10/code/ocaml/snippet11.ml index a4571b9f2..ce3877b7e 100644 --- a/src/content/3.10/code/ocaml/snippet11.ml +++ b/src/content/3.10/code/ocaml/snippet11.ml @@ -1,4 +1,4 @@ module type DiaProd = sig - type ('a, 'b) p - type 'a diaprod = DiaProd of ('a, 'a) p + type ('a, 'b) t + type 'a diaprod = DiaProd of ('a, 'a) t end diff --git a/src/content/3.10/code/ocaml/snippet12.ml b/src/content/3.10/code/ocaml/snippet12.ml index e5386ec04..e8b4e65d6 100644 --- a/src/content/3.10/code/ocaml/snippet12.ml +++ b/src/content/3.10/code/ocaml/snippet12.ml @@ -1,15 +1,13 @@ module EndsWithDiaProd (P : Profunctor) - (D : DiaProd with type ('a, 'b) p = ('a, 'b) P.p) - (PP : ProdP with type ('a, 'b) p = ('a, 'b) P.p) = + (D : DiaProd with type ('a, 'b) t = ('a, 'b) P.t) + (PP : ProdP with type ('a, 'b) t = ('a, 'b) P.t) = struct module E = EndsEqualizer (P) let lambdaP : 'a D.diaprod -> ('a, 'b) PP.prod_p = fun (DiaProd paa) -> E.lambda paa - ;; let rhoP : 'b D.diaprod -> ('a, 'b) PP.prod_p = - fun (DiaProd pbb) -> E.rho pbb - ;; + fun (DiaProd pbb) -> E.rho pbb end diff --git a/src/content/3.10/code/ocaml/snippet14.ml b/src/content/3.10/code/ocaml/snippet14.ml index 2424ce89a..6e9407b47 100644 --- a/src/content/3.10/code/ocaml/snippet14.ml +++ b/src/content/3.10/code/ocaml/snippet14.ml @@ -1,3 +1,3 @@ module Coend (P : Profunctor) = struct - type coend = Coend of { c : 'a. ('a, 'a) P.p } + type coend = Coend of { c : 'a. ('a, 'a) P.t } end diff --git a/src/content/3.10/code/ocaml/snippet16.ml b/src/content/3.10/code/ocaml/snippet16.ml index f482800bd..52d38dc55 100644 --- a/src/content/3.10/code/ocaml/snippet16.ml +++ b/src/content/3.10/code/ocaml/snippet16.ml @@ -10,26 +10,22 @@ module CoEndImpl (P : Profunctor) = struct type b module type Sum_P = - SumP - with type ('a, 'b) p = ('a, 'b) P.p - and type a = a - and type b = b + SumP with type ('a, 'b) p = ('a, 'b) P.t + and type a = a and type b = b let lambda (module S : Sum_P) = (module struct type a = S.b - type ('a, 'b) p = ('a, 'b) P.p + type ('a, 'b) p = ('a, 'b) P.t let paa = P.dimap S.f id S.pab end : DiagSum) - ;; let rho (module S : Sum_P) = (module struct type a = S.a - type ('a, 'b) p = ('a, 'b) P.p + type ('a, 'b) p = ('a, 'b) P.t let paa = P.dimap id S.f S.pab end : DiagSum) - ;; end From 3663638cb5cb9ad9f54f4c7788245b055126b2cc Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:25:28 +0100 Subject: [PATCH 26/27] Update Ch. 27 OCaml snippets --- src/content/3.11/code/ocaml/snippet04.ml | 4 ++-- src/content/3.11/code/ocaml/snippet05.ml | 24 ++++++++++++++---------- src/content/3.11/code/ocaml/snippet08.ml | 2 -- src/content/3.11/code/ocaml/snippet10.ml | 14 +++++--------- src/content/3.11/code/ocaml/snippet12.ml | 7 +++---- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/content/3.11/code/ocaml/snippet04.ml b/src/content/3.11/code/ocaml/snippet04.ml index e9eb201bc..0fdc651d3 100644 --- a/src/content/3.11/code/ocaml/snippet04.ml +++ b/src/content/3.11/code/ocaml/snippet04.ml @@ -2,9 +2,9 @@ module type Lst = sig type a type m - module M : Monoid with type m = m + module M : Monoid with type t = m - type lst = (a -> M.m) -> M.m + type lst = (a -> M.t) -> M.t val f : lst end diff --git a/src/content/3.11/code/ocaml/snippet05.ml b/src/content/3.11/code/ocaml/snippet05.ml index 9e55f9cc3..65133ab71 100644 --- a/src/content/3.11/code/ocaml/snippet05.ml +++ b/src/content/3.11/code/ocaml/snippet05.ml @@ -1,29 +1,33 @@ -let fold_map (type i) (module M : Monoid with type m = i) xs f = +module ListMonoid (A : sig + type a +end) : Monoid with type t = A.a list = struct + type t = A.a list + + let mempty = [] + let mappend = List.append +end + +let fold_map (type i) (module M : Monoid with type t = i) xs f = List.fold_left (fun acc a -> M.mappend acc (f a)) M.mempty xs -;; let to_lst (type x) (xs : x list) = - let module LM : Monoid with type m = x list = - ListMonoid (struct - type a = x - end) - in + let module LM : Monoid with type t = x list = ListMonoid (struct + type a = x + end) in (module struct type a = x type m = x list module M = LM - type lst = (a -> LM.m) -> LM.m + type lst = (a -> LM.t) -> LM.t let f g = fold_map (module LM) xs g end : Lst with type a = x) -;; let from_lst (type x) (module LstImpl : Lst with type a = x and type m = x list) = LstImpl.f (fun x -> [ x ]) -;; diff --git a/src/content/3.11/code/ocaml/snippet08.ml b/src/content/3.11/code/ocaml/snippet08.ml index c3439edce..2260ae54c 100644 --- a/src/content/3.11/code/ocaml/snippet08.ml +++ b/src/content/3.11/code/ocaml/snippet08.ml @@ -11,7 +11,6 @@ let to_exp (type a' b') f = end : Exp with type a = a' and type b = b') -;; let from_exp (type a' b') @@ -20,4 +19,3 @@ let from_exp = let (I i) = E.di in E.fk (a, i) -;; diff --git a/src/content/3.11/code/ocaml/snippet10.ml b/src/content/3.11/code/ocaml/snippet10.ml index 32fd08b42..592716235 100644 --- a/src/content/3.11/code/ocaml/snippet10.ml +++ b/src/content/3.11/code/ocaml/snippet10.ml @@ -1,17 +1,14 @@ module FreeFunctor (F : sig - type 'a f + type 'a t end) : Functor = struct - module type F = FreeF with type 'a f = 'a F.f + module type F = FreeF with type 'a f = 'a F.t type 'a t = (module F with type a = 'a) - let fmap - (type a' b') - (f : a' -> b') - (module FF : F with type a = a') - = + let fmap (type a' b') (f : a' -> b') + (module FF : F with type a = a') = (module struct - type 'a f = 'a F.f + type 'a f = 'a F.t type a = b' type i = FF.i @@ -19,5 +16,4 @@ end) : Functor = struct let fi = FF.fi end : F with type a = b') - ;; end diff --git a/src/content/3.11/code/ocaml/snippet12.ml b/src/content/3.11/code/ocaml/snippet12.ml index d701bc156..cd133f5ed 100644 --- a/src/content/3.11/code/ocaml/snippet12.ml +++ b/src/content/3.11/code/ocaml/snippet12.ml @@ -1,17 +1,16 @@ module FreeFunctorAlt (F : sig - type 'a f + type 'a t end) : Functor = struct - module type F = FreeF_Alt with type 'a f = 'a F.f + module type F = FreeF_Alt with type 'a f = 'a F.t type 'a t = (module F with type a = 'a) let fmap (type a' b') f (module FF : F with type a = a') = (module struct type a = b' - type 'a f = 'a F.f + type 'a f = 'a F.t let freeF bi = FF.freeF (fun a -> bi (f a)) end : F with type a = b') - ;; end From 837676c64f1b08e16384dc81890f80924d9c6203 Mon Sep 17 00:00:00 2001 From: ComanderP <8596680+ComanderP@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:26:35 +0100 Subject: [PATCH 27/27] Update Ch. 30 OCaml snippets --- src/content/3.14/code/ocaml/snippet01.ml | 6 +----- src/content/3.14/code/ocaml/snippet03.ml | 2 +- src/content/3.14/code/ocaml/snippet04.ml | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/content/3.14/code/ocaml/snippet01.ml b/src/content/3.14/code/ocaml/snippet01.ml index 4e78157b5..002c3e68f 100644 --- a/src/content/3.14/code/ocaml/snippet01.ml +++ b/src/content/3.14/code/ocaml/snippet01.ml @@ -1,5 +1 @@ -type ('a, 'b) either = - | Left of 'a - | Right of 'b - -type two = (unit, unit) either +type two = (unit, unit) Either.t diff --git a/src/content/3.14/code/ocaml/snippet03.ml b/src/content/3.14/code/ocaml/snippet03.ml index aee7e1883..a31a5879e 100644 --- a/src/content/3.14/code/ocaml/snippet03.ml +++ b/src/content/3.14/code/ocaml/snippet03.ml @@ -1 +1 @@ -type 'a option = (unit, 'a) either +type 'a option = (unit, 'a) Either.t diff --git a/src/content/3.14/code/ocaml/snippet04.ml b/src/content/3.14/code/ocaml/snippet04.ml index e30a56a0c..d6813a040 100644 --- a/src/content/3.14/code/ocaml/snippet04.ml +++ b/src/content/3.14/code/ocaml/snippet04.ml @@ -1,3 +1 @@ -type 'a option = - | None - | Some of 'a +type 'a option = None | Some of 'a