From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p3UKvpmY007691 for ; Sat, 30 Apr 2011 22:57:51 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgcDALx2vE3RVaE2kGdsb2JhbACmDwgUAQEBAQkJDQcUBCGIcaBBinwKgiCDfjSIXgEBAwaFegSOeYR0gyOCIzsqgx8 X-IronPort-AV: E=Sophos;i="4.64,294,1301868000"; d="scan'208";a="98408277" Received: from mail-fx0-f54.google.com ([209.85.161.54]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 30 Apr 2011 22:57:46 +0200 Received: by fxm11 with SMTP id 11so6411945fxm.27 for ; Sat, 30 Apr 2011 13:57:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:reply-to:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=ERS6XpLbqUbrOclh3GSGDVx0kikdO2+kbrM9s0lA/RM=; b=RNPCXaYZzHVIBB9EUDh8cWraVITK6xsEzbGiyARNveD4jwF15gKLVcCGwVAT0r71Dq PtmgNBH6tNKeZSTLcbXIHapJHiXoIhQriP+0Cow4HOG8Z3266I3X8M4RYRnYD7SUtKv4 5OA27376iAy2Sv+aMl5AngXa3rKhJlBxgaEMU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; b=EZlcpb5PwPrmCL9whvk0XFIoBVsxYbs68GEdg96BKIcg/SxSusECDh0k/hbvfLnbp/ tbBTnpaUw1KUsL2vwsn4o8XEt5NphKMycLKtm/F8Fm3L6beMkk3WAL9hiBtAQEMOvwdc joom8Bz9KzYAcwhg3kt6d1CWCJUtJDeXYWLro= MIME-Version: 1.0 Received: by 10.223.48.139 with SMTP id r11mr1390824faf.63.1304197065874; Sat, 30 Apr 2011 13:57:45 -0700 (PDT) Received: by 10.223.81.68 with HTTP; Sat, 30 Apr 2011 13:57:45 -0700 (PDT) Reply-To: yminsky@gmail.com In-Reply-To: References: <164004794.892685.1304067487325.JavaMail.root@zmbs2.inria.fr> <20110429093355.GA25892@yquem.inria.fr> <4746acd6-ddad-498c-a535-a3bfdcb5fec7@email.android.com> Date: Sat, 30 Apr 2011 16:57:45 -0400 Message-ID: From: Yaron Minsky To: Gabriel Scherer Cc: "craff73@gmail.com" , luc.maranget@inria.fr, Dmitry Bely , caml-list@inria.fr Content-Type: multipart/alternative; boundary=00151747b8d8d41c2404a2290ae6 Subject: Re: [Caml-list] Comparing variant types --00151747b8d8d41c2404a2290ae6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable When I have to do this by hand, my favorite style is this: type t =3D A of float | B of string let compare x y =3D let tag_idx =3D function A _ -> 0 | B _ -> 1 in match x, y with | A a1, A a2 -> Float.compare a1 a2 | B b1, B b2 -> String.compare b1 b2 | (B _ | A _), _ -> Int.compare (tag_idx x) (tag_idx y) You will get the right kind of warnings when the type t changes, and it's pleasantly explicit about the ordering implied by the tags. That said, this is unpleasant boilerplate to have to write by hand. My expectation is that in the next release of Core, we will include a set of macros for autogenerating equality, comparison and hash functions, as well as type-hashes, all driven by the type definitions. On Sat, Apr 30, 2011 at 4:19 PM, Gabriel Scherer wrote: > You want to avoid code size quadratic in the number of constructors. Which >> is possible: >> > > > let cmp x y =3D match x, y with >> A, A -> true >> | A, _ | _, A -> false >> | B, B -> true >> | B, _ | _, B -> false > > > With one twist though: if done that way for (type foo =3D A | B), the last > (_, B) pattern is actually redundant. If you want to avoid getting a warn= ing > here, you should comment it out or remove it: > > let cmp x y =3D match x, y with > | A, A -> true > | A, _ | _, A -> false > | B, B -> true > | B, _ (*| _, B*) -> false > > That's the style I use. > > On Sat, Apr 30, 2011 at 3:43 PM, craff73@gmail.com wro= te: > >> Hello, >> >> You want to avoid code size quadratic in the number of constructors. Whi= ch >> is possible: >> >> let cmp x y =3D match x, y with >> A, A -> true >> | A, _ | _, A -> false >> | B, B -> true >> | B, _ | _, B -> false >> ... >> >> Cheers >> Christophe >> >> -- >> Envoy=E9 de mon t=E9l=E9phone Android avec K-9 Mail. Excusez la bri=E8ve= t=E9. >> >> > --00151747b8d8d41c2404a2290ae6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable When I have to do this by hand, my favorite style is this:

type t = =3D A of float | B of string

let compare x y =3D
=A0=A0 let tag_= idx =3D function=A0 A _ -> 0 | B _ -> 1 in
=A0=A0 match x, y with<= br>=A0=A0 | A a1, A a2 -> Float.compare a1 a2
=A0=A0 | B b1, B b2 -> String.compare b1 b2
=A0=A0 | (B _ | A _), _ -= > Int.compare (tag_idx x) (tag_idx y)

You will get the right kind= of warnings when the type t changes, and it's pleasantly explicit abou= t the ordering implied by the tags.

That said, this is unpleasant boilerplate to have to write by hand.=A0 = My expectation is that in the next release of Core, we will include a set o= f macros for autogenerating equality, comparison and hash functions, as wel= l as type-hashes, all driven by the type definitions.

On Sat, Apr 30, 2011 at 4:19 PM, Gabriel Sch= erer <gab= riel.scherer@gmail.com> wrote:
You want to avoid code size quadratic in the number of constructors. Which = is possible:
=A0
let cmp x y =3D match x, y with
A, A -> true
| A, _ | _, A -> f= alse
| B, B -> true
| B, _ | _, B -> false

With one twist though: if done that way for (type foo = =3D A | B), the last (_, B) pattern is actually redundant. If you want to a= void getting a warning here, you should comment it out or remove it:

=A0=A0let cmp x y =3D match x, y with=
=A0=A0| A, A -> true
=A0=A0| A, _ | _, A -> fals= e
=A0=A0| B, B -> true
=A0=A0| B, _ (*| _, B*) ->= false

That's the style I use.

On Sat, Apr 30, 2011 at 3:43 PM, craff73@gmail.com <craff73= @gmail.com> wrote:
Hello,

You want to avoid code size quadratic in the number of constructors. Which = is possible:

let cmp x y =3D match x, y with
A, A -> true
| A, _ | _, A -> false
| B, B -> true
| B, _ | _, B -> false
...

Cheers
Christophe

--
Envoy=E9 de mon t=E9l=E9phone Android avec K-9 Mail. Excusez la bri=E8vet= =E9.



--00151747b8d8d41c2404a2290ae6--