File: test_rec_mod.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (84 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2020 Hugo Heuzard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

open Js_of_ocaml_compiler.Stdlib

(* https://github.com/ocaml/ocaml/pull/9497 *)
(* Original test case from the issue: *)

module rec Id : sig
  type t = { id : int }

  val compare : t -> t -> int
end =
  (* error here: undefined compare function *)
  Id

module IdSet = Set.Make (Id)

let%expect_test _ =
  try
    let basic_set = IdSet.singleton { id = 0 } in
    ignore (IdSet.mem { id = 1 } basic_set : bool)
    (* diverge here *)
  with e ->
    if String.is_suffix ~suffix:"Undefined recursive module" (Printexc.to_string e)
    then ()
    else raise e

(* Looping version *)
module rec M1 : sig
  val f : int -> int

  val g : int -> int

  val h : int -> int
end = struct
  let f = M1.g

  let g i = if i < 0 then 2 else M1.f (i - 1)

  let h = M1.f
end

let%expect_test _ =
  (try print_int (M1.f 3) with e -> print_endline @@ Printexc.to_string e);
  [%expect {| 2 |}];
  (try print_int (M1.g 3) with e -> print_endline @@ Printexc.to_string e);
  [%expect {| 2 |}];
  (try print_int (M1.h 3) with e -> print_endline @@ Printexc.to_string e);
  [%expect {| 2 |}]

module rec Odd : sig
  val odd : int -> bool
end = struct
  let odd x = if x = 0 then false else Even.even (pred x)
end

and Even : sig
  val even : int -> bool
end = struct
  let even x = if x = 0 then true else Odd.odd (pred x)
end

let%expect_test _ =
  Printf.printf "%b" (Even.even 1000);
  [%expect {| true |}];
  Printf.printf "%b" (Odd.odd 1000);
  [%expect {| false |}]