From: Christophe Raffalli <christophe@raffalli.eu>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] module equality
Date: Tue, 20 Jun 2017 11:36:41 +0200 [thread overview]
Message-ID: <20170620093641.ovz6dnsghtdj6tv6@delli7.univ-savoie.fr> (raw)
[-- Attachment #1: Type: text/plain, Size: 1457 bytes --]
Dear camelers,
I thought that two applications of Set.Make to
the same module would result in two distinct
modules with two distinct types 't'.
This is not the case, and I am no sure it is a
good choice as it allows to write the following,
where you can break the invariant of a functor:
-------------------------------------------------------------------------
let is_prime n =
let rec fn d =
if d * d > n then true
else if n mod d = 0 then false else fn (d+1)
in
fn 2
let rec random_prime n =
let p = 2 + Random.int (n-2) in
if is_prime p then p else random_prime n
module type S = sig
type elt
type t
val create : elt list -> t
end
module F(O:Set.OrderedType) : S with type elt = O.t = struct
type elt = O.t
type t = O.t list
let salt = random_prime 1_000_000
let _ = Printf.printf "salf: %d\n%!" salt
let f x = Hashtbl.hash (Hashtbl.hash x * salt)
let cmp x y =
match compare (f x) (f y) with
| 0 -> compare x y
| c -> c
let create l = List.sort cmp l
end
module Int = struct
type t = int
let compare = compare
end
module M1 = F(Int)
module M2 = F(Int)
let _ = Printf.printf "test: %b\n%!" (M1.create [1;2;3;4;5] = M2.create [1;2;3;4;5])
----------------------------------------------------------------------------
OK, I know how to rewrite this functor so that M1.t and M2.t are distinct.
But still the above code should be rejected, shouldn't it ?
Cheers,
Christophe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next reply other threads:[~2017-06-20 9:36 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-20 9:36 Christophe Raffalli [this message]
2017-06-20 10:03 ` Dario Teixeira
2017-06-20 10:05 ` Jeremy Yallop
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170620093641.ovz6dnsghtdj6tv6@delli7.univ-savoie.fr \
--to=christophe@raffalli.eu \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox