Skip to content

Instantly share code, notes, and snippets.

@smondet
Created September 7, 2016 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smondet/aad54d4870688864b9c74b5b077cdbd6 to your computer and use it in GitHub Desktop.
Save smondet/aad54d4870688864b9c74b5b077cdbd6 to your computer and use it in GitHub Desktop.
module Abstract_value :
sig
type t
val create : unit -> t
end =
struct
type t = unit -> unit
let create () =
fun () -> ()
end
type t = {
mutable abstract_value : Abstract_value.t;
(* Regular fields *)
x : int;
}
let make ~x = {
x;
abstract_value = Abstract_value.create ();
}
(*
The proper comparison function for this type,
which is incompatible with Pervasives.compare.
*)
let compare a b =
compare (abs a.x) (abs b.x)
let (=) a b =
compare a b = 0
let (<>) a b =
compare a b <> 0
let (<) a b =
compare a b < 0
let (<=) a b =
compare a b <= 0
let (>) a b =
compare a b > 0
let (>=) a b =
compare a b >= 0
(*
Make sure to run these tests with ocamlc, ocamlopt and any other OCaml
compiler you're using.
*)
let test_exception () =
try
ignore (Pervasives.compare (make ~x:0) (make ~x:0));
false
with Invalid_argument "equal: functional value" ->
true
let () =
print_endline (if test_exception () then "Passed" else "Failed")
@smondet
Copy link
Author

smondet commented Sep 7, 2016

Like this?

  $ ocaml test_mj.ml
Passed
 $ ocamlc test_mj.ml -o testmj.byte && testmj.byte
Passed
 $ ocamlopt test_mj.ml -o testmj.opt && testmj.opt
Passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment