From: Yaron Minsky <yminsky@gmail.com>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: "craff73@gmail.com" <craff73@gmail.com>,
luc.maranget@inria.fr, Dmitry Bely <dmitry.bely@gmail.com>,
caml-list@inria.fr
Subject: Re: [Caml-list] Comparing variant types
Date: Sat, 30 Apr 2011 16:57:45 -0400 [thread overview]
Message-ID: <BANLkTik_Lb2x7e7NR1uAJXean_GRcVUNQg@mail.gmail.com> (raw)
In-Reply-To: <BANLkTinq3eZPzt3CwFD2P4mL1Te+ykEZdA@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1900 bytes --]
When I have to do this by hand, my favorite style is this:
type t = A of float | B of string
let compare x y =
let tag_idx = 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
<gabriel.scherer@gmail.com>wrote:
> You want to avoid code size quadratic in the number of constructors. Which
>> is possible:
>>
>
>
> let cmp x y = 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 = A | B), the last
> (_, B) pattern is actually redundant. If you want to avoid getting a warning
> here, you should comment it out or remove it:
>
> let cmp x y = 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 <craff73@gmail.com>wrote:
>
>> Hello,
>>
>> You want to avoid code size quadratic in the number of constructors. Which
>> is possible:
>>
>> let cmp x y = match x, y with
>> A, A -> true
>> | A, _ | _, A -> false
>> | B, B -> true
>> | B, _ | _, B -> false
>> ...
>>
>> Cheers
>> Christophe
>>
>> --
>> Envoyé de mon téléphone Android avec K-9 Mail. Excusez la brièveté.
>>
>>
>
[-- Attachment #2: Type: text/html, Size: 3404 bytes --]
next prev parent reply other threads:[~2011-04-30 20:57 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-28 19:16 Ethan Burns
2011-04-28 21:07 ` Vincent Aravantinos
2011-04-28 22:56 ` Ethan Burns
[not found] ` <354087020.772283.1304031467793.JavaMail.root@zmbs4.inria.fr>
2011-04-29 8:46 ` Fabrice Le Fessant
2011-04-29 8:57 ` Dmitry Bely
[not found] ` <164004794.892685.1304067487325.JavaMail.root@zmbs2.inria.fr>
2011-04-29 9:33 ` luc.maranget
2011-04-29 10:54 ` Andrew
2011-04-29 11:17 ` Dmitry Bely
2011-04-29 12:15 ` Jon Harrop
2011-04-30 16:38 ` Andrew
2011-04-29 11:32 ` Dmitry Bely
2011-04-30 13:43 ` craff73
2011-04-30 19:26 ` Andrew
2011-04-30 20:19 ` Gabriel Scherer
2011-04-30 20:57 ` Yaron Minsky [this message]
[not found] <fa.FGXk5PCsQgS8TidmFkgljpqbLZo@ifi.uio.no>
2011-04-29 11:54 ` Ethan Burns
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=BANLkTik_Lb2x7e7NR1uAJXean_GRcVUNQg@mail.gmail.com \
--to=yminsky@gmail.com \
--cc=caml-list@inria.fr \
--cc=craff73@gmail.com \
--cc=dmitry.bely@gmail.com \
--cc=gabriel.scherer@gmail.com \
--cc=luc.maranget@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