From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 1A8527F71A for ; Tue, 22 Apr 2014 10:31:17 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of jdimino@janestreet.com) identity=pra; client-ip=38.105.200.229; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="jdimino@janestreet.com"; x-sender="jdimino@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of jdimino@janestreet.com designates 38.105.200.229 as permitted sender) identity=mailfrom; client-ip=38.105.200.229; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="jdimino@janestreet.com"; x-sender="jdimino@janestreet.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mxout3.mail.janestreet.com) identity=helo; client-ip=38.105.200.229; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="jdimino@janestreet.com"; x-sender="postmaster@mxout3.mail.janestreet.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApQBAFwoVlMmacjlnGdsb2JhbAA/GoNVV7sdgT+HO4EMHg4BAQEBAQYWCTyCJQEBBAEnGQEBNwEECwsEBzsiEgEFARwGE4g5CAMCCDacTosahFQBBZ86EQaOUgQHgi4PgXuYdIE3jyoYKYRi X-IPAS-Result: ApQBAFwoVlMmacjlnGdsb2JhbAA/GoNVV7sdgT+HO4EMHg4BAQEBAQYWCTyCJQEBBAEnGQEBNwEECwsEBzsiEgEFARwGE4g5CAMCCDacTosahFQBBZ86EQaOUgQHgi4PgXuYdIE3jyoYKYRi X-IronPort-AV: E=Sophos;i="4.97,902,1389740400"; d="scan'208";a="69447082" Received: from mx5.janestreet.com (HELO mxout3.mail.janestreet.com) ([38.105.200.229]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 22 Apr 2014 10:31:16 +0200 Received: from tot-oib-smtp1.delacy.com ([172.27.22.15] helo=tot-smtp) by mxout3.mail.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1WcW6j-0000N0-Pi for caml-list@inria.fr; Tue, 22 Apr 2014 04:31:13 -0400 Received: from tot-dmz-mxgoog1.delacy.com ([172.27.224.14] helo=mxgoog2.janestreet.com) by tot-smtp with esmtps (TLSv1:AES256-SHA:256) (Exim 4.72) (envelope-from ) id 1WcW6j-0007jJ-OZ for caml-list@inria.fr; Tue, 22 Apr 2014 04:31:13 -0400 Received: from mail-ig0-f178.google.com ([209.85.213.178]) by mxgoog2.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1WcW6j-0004mU-LS for caml-list@inria.fr; Tue, 22 Apr 2014 04:31:13 -0400 Received: by mail-ig0-f178.google.com with SMTP id hn18so2716978igb.5 for ; Tue, 22 Apr 2014 01:31:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=03JW2vmZwYbILgpptRj2igMf2tTDckB6M0v8MTWJaso=; b=i7bmBjm2WtSGhas8eyYH0kuhEEhN9aubSiqja6T/AGP2Xoy+gVC3TQC2fgEPhOLADU z9VtjB/NSRRI89ayEOtol54XAxu7+H3vJhzBwN7BK4NDyZkspYK9qFtXZbrm6nxSMkm2 iE2k2p1eiQkKR2ro6Xt/nQ036NuGkT1mf88Aw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=03JW2vmZwYbILgpptRj2igMf2tTDckB6M0v8MTWJaso=; b=ECzgnh+J83l11DgafpvUti1wFcmm0AJEhkRtcge58PYZo+X2tIwXVQ6rvXOahN4/KE kUWejK82eECjAW8t1suXZgo39xw7gHFQP1B45yLYnYLzN1Al9R4vRt2vht/30FwGLLyU QP1RaGKXLV1rid+mCR1y7IdCqvWwHfBJCkea9gc3sFTuchFPWQDCWnzPey/Q1VqXAYnC zvmMhPGUM6ykidnxfuda7VZIS/2cCZJTzTBofsph+9y1gZ/P69tN9xvYtgVOJUvcIuMe iCECV4NpL/aiimDGbarUAx/BRHTfoP3iKzduFLeN1HDXdC+deXrr4mY3aL41O+83a83B IB6A== X-Gm-Message-State: ALoCoQlL56Wa6DrWzMyDQLKaJKPv/2DnaeSJrmKGkvRHhzfhz3m98RpHv28naxT2/tnXDUNO5JEI5PAOlFAwYZY8cgIrMnQtzA1/BctvA3TPxvqjXJeCZDChhAyOGD5FxPZtcMhqrled X-Received: by 10.50.43.170 with SMTP id x10mr28221454igl.36.1398155473410; Tue, 22 Apr 2014 01:31:13 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.50.43.170 with SMTP id x10mr28221442igl.36.1398155473204; Tue, 22 Apr 2014 01:31:13 -0700 (PDT) Received: by 10.50.117.40 with HTTP; Tue, 22 Apr 2014 01:31:13 -0700 (PDT) In-Reply-To: <5356225B.1090305@cryptosense.com> References: <5356225B.1090305@cryptosense.com> Date: Tue, 22 Apr 2014 09:31:13 +0100 Message-ID: From: Jeremie Dimino To: Romain Bardou Cc: Ocaml Mailing List Content-Type: multipart/alternative; boundary=089e01176cad52520304f79d7076 Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers --089e01176cad52520304f79d7076 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Apr 22, 2014 at 9:03 AM, Romain Bardou wrote: > I want to implement the trick with GADTs where you test equality of > unique identifiers, and if they match this adds an equality constraint > on types. I want this code to be small and well abstracted in a module > so that if this module is safe, then using this module cannot cause a > seg fault. > > Here is the signature of my module: > [...] > We do exactly this at Jane Street in our type_equal module: https://github.com/janestreet/core_kernel/blob/master/lib/type_equal.ml#L87 Note that using the open_types branch of ocaml [1] there is a cool way to do this: (************************************************************************) type 'a key = .. type (_, _) equal = | Equal: ('a, 'a) equal | Different: ('a, 'b) equal module type S = sig type t type 'a key += T : t key end type 'a t = (module S with type t = 'a) let fresh (type a) () = let module M = struct type t = a type 'a key += T : t key end in (module M : S with type t = a) let equal (type a) (type b) (module A : S with type t = a) (module B : S with type t = b) : (a, b) equal = match A.T with | B.T -> Equal | _ -> Different let to_int = Hashtbl.hash (************************************************************************) [1] http://caml.inria.fr/mantis/view.php?id=5584 -- Jeremie --089e01176cad52520304f79d7076 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On T= ue, Apr 22, 2014 at 9:03 AM, Romain Bardou <romain@cryptosense.com> wrote:
I want to implement the trick with GADTs where you test eq= uality of
unique identifiers, and if they match this adds an equality constraint
on types. I want this code to be small and well abstracted in a module
so that if this module is safe, then using this module cannot cause a
seg fault.

Here is the signature of my module:
[...]

We do exactly this at Jane Street= in our type_equal module:

Note that using the open_types branch of ocaml [1= ] there is a cool way to do this:

(***************= *********************************************************)
type 'a key =3D ..

type (_, _) equal =3D<= /div>
=A0 | Equal: ('a, 'a) equal
=A0 | Different: (&= #39;a, 'b) equal

module type S =3D sig
=A0 type t
=A0 type 'a key +=3D T : t key
end

<= div>type 'a t =3D (module S with type t =3D 'a)

let fresh (type a) () =3D
=A0 let module M =3D struct
=
=A0 =A0 type t =3D a
=A0 =A0 type 'a key +=3D T : t key
=A0 end in
= =A0 (module M : S with type t =3D a)

let equal (ty= pe a) (type b)
=A0 =A0 =A0 (module A : S with type t =3D a)
=
=A0 =A0 =A0 (module B : S with type t =3D b)
=A0 : (a, b) equal =3D
=A0 match A.T with
=A0 | B.= T -> Equal
=A0 | _ =A0 -> Different

let to_int =3D Hashtbl.hash
(****************************= ********************************************)

--
Jeremie
--089e01176cad52520304f79d7076--