caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] module equality
@ 2017-06-20  9:36 Christophe Raffalli
  2017-06-20 10:03 ` Dario Teixeira
  2017-06-20 10:05 ` Jeremy Yallop
  0 siblings, 2 replies; 3+ messages in thread
From: Christophe Raffalli @ 2017-06-20  9:36 UTC (permalink / raw)
  To: caml-list

[-- 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 --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-06-20 10:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-20  9:36 [Caml-list] module equality Christophe Raffalli
2017-06-20 10:03 ` Dario Teixeira
2017-06-20 10:05 ` Jeremy Yallop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).