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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id B8CB87EE25 for ; Thu, 14 Nov 2013 13:03:58 +0100 (CET) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of yminsky@janestreet.com) identity=pra; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of yminsky@janestreet.com designates 38.105.200.229 as permitted sender) identity=mailfrom; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="yminsky@janestreet.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@tot-dmz-mxout1.janestreet.com) identity=helo; client-ip=38.105.200.229; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yminsky@janestreet.com"; x-sender="postmaster@tot-dmz-mxout1.janestreet.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmEBANC7hFImacjlnGdsb2JhbABagz9HDKxKklaBGR4OAQEBAQEGDQkJFCiCJQEBBUABASwLAQ8LCwMKDSEiEgEFAQoSBhMSh10DDwMCCKEbiw6EUgEFhR4DiW8GjywzB4QxiUWOToEvjnEYKYRx X-IPAS-Result: AmEBANC7hFImacjlnGdsb2JhbABagz9HDKxKklaBGR4OAQEBAQEGDQkJFCiCJQEBBUABASwLAQ8LCwMKDSEiEgEFAQoSBhMSh10DDwMCCKEbiw6EUgEFhR4DiW8GjywzB4QxiUWOToEvjnEYKYRx X-IronPort-AV: E=Sophos;i="4.93,699,1378850400"; d="scan'208";a="35508800" Received: from mx5.janestreet.com (HELO tot-dmz-mxout1.janestreet.com) ([38.105.200.229]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 14 Nov 2013 13:03:57 +0100 Received: from tot-oib-smtp1.delacy.com ([172.27.22.15] helo=tot-smtp) by tot-dmz-mxout1.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1VgveN-0006jJ-SU for caml-list@inria.fr; Thu, 14 Nov 2013 07:03:55 -0500 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 1VgveN-0008TR-RG for caml-list@inria.fr; Thu, 14 Nov 2013 07:03:55 -0500 Received: from mail-wg0-f51.google.com ([74.125.82.51]) by mxgoog2.janestreet.com with esmtp (Exim 4.76) (envelope-from ) id 1VgveN-0006pu-ML for caml-list@inria.fr; Thu, 14 Nov 2013 07:03:55 -0500 Received: by mail-wg0-f51.google.com with SMTP id m15so1777975wgh.6 for ; Thu, 14 Nov 2013 04:03:55 -0800 (PST) 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:content-transfer-encoding; bh=QBZ1kn+VoxiVGDknf5n1XZz9Sz4nzy1FAY/iy/vH6V8=; b=d291HfkJo0cCQSpc++6m4+7SigC1kb2lUKRI+w9kYMVspO2ecVhkqEwrP50kfn6+JJ NapB+rEDD7B0MEVWk0fSu60eOIMUmJ2U/knOyUajkE4wo4mzXYgfR0r/ID527PDxzEao aiUu7REaZMx/oj80hwJcg67ZrMIFFSDxEmiks= 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 :content-transfer-encoding; bh=QBZ1kn+VoxiVGDknf5n1XZz9Sz4nzy1FAY/iy/vH6V8=; b=BrntCqSPiFbdYWDakyySKbmEu+uhehMcjGaXPTEbjKBskC5vAXrnZtAlhIhS0V330m zNlWvErVsbCu4O48dQP2Hyd8mrQZ+Sa9nzuIMH2DBArjptXG5SsTKdqCMTqV1TS3ytId ohpD6snn9eU6IGI666lDIirbBxwxOk868BKSbTgRY66VmmmEtfNP+aUv4tTP5om17hsp qArv1sVuL/B+hZAtHDspKvI5ruFNHNvMloLyZF2nvF0Zf2nUUIJD7yrb6Lkc6sXn7wx9 55WoRrJO+InjqaYaqMTdDGPj2fLdaqG0hmhT5cLOrojmiM4lHutz48/EoK3Ee0FM1m89 6OXA== X-Gm-Message-State: ALoCoQlU0Yc2FRqZReQ6/w8rp7bsK8dVilbhy5+2rx6xrAMtFAFJH/eHT/cekdCRyz91GNJEOnOWB4TNmGcrrIwOEcCJnAFFnHHeqAsPX89lcIx1ruTkfL1jNZWJXv3P4+Ou2/hCuPiABcFN9hH7dvfIk+NwO8B5JA== X-Received: by 10.180.39.205 with SMTP id r13mr2088923wik.55.1384430635170; Thu, 14 Nov 2013 04:03:55 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.180.39.205 with SMTP id r13mr2088862wik.55.1384430634497; Thu, 14 Nov 2013 04:03:54 -0800 (PST) Received: by 10.223.180.132 with HTTP; Thu, 14 Nov 2013 04:03:54 -0800 (PST) In-Reply-To: References: <7389AC6E-C25D-43B3-9A39-E59F92EF35AB@my.bristol.ac.uk> Date: Thu, 14 Nov 2013 07:03:54 -0500 Message-ID: From: Yaron Minsky To: Mark Shinwell Cc: Ollie Frolovs , caml users Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [Caml-list] Creating a Map from custom type It's worth noting that if you want a simply polymorphic map, you can have one, you just need to be explicit about what you're doing: utop # let map =3D Map.Poly.of_alist_exn [1, "one"; 2, "two"; 3, "three"];; val map : (int, string) Map.Poly.t =3D which is probably the lowest-pain approach. Polymorphic compare (which is what this is based on) has some downside, but is perfectly good for many use-cases. RWO covers in detail the pitfalls of polymorphic compare, but it doesn't mean you should avoid it, especially when you're getting started. As for David's point about Core vs the stdlib, I think creating a map with a polymorphic comparison function is actually quite a bit easier in Core than it is with the stdlib, since no functor application is required. y On Thu, Nov 14, 2013 at 6:21 AM, Mark Shinwell w= rote: > On 14 November 2013 10:39, Ollie Frolovs wrote: >> My current implementation uses associative lists, in subroute.ml: >> >> open Core.Std >> type t =3D ((int list * int) * (int * int list)) list >> let empty =3D [] >> let to_list x =3D x >> let add t k v =3D List.Assoc.add t k v >> let find t k =3D List.Assoc.find t k >> >> I would like to use a Map instead. The key for my Map must have the type= (int list * int) and the values have the type (int * int list). If i under= stood it correctly, because the key is a tuple and not one of the built-in = simple data types (string, int, etc), because of this i have to provide a c= ustom comparator. This is first thing that i am not sure of. > > I think you need something like: > > module Key =3D struct > type t =3D int list * int with sexp, compare > end > > module My_map =3D Map.Make (Key) > > Then, for example: > > let map =3D > My_map.add My_map.empty > ~key:([42], 0) > ~data:(1, [2]) > > The "with compare" auto-generates a "compare" function for > you. Using explicit comparison functions, as Core strongly > encourages, does unfortunately mean a bit more code at times > but is arguably vastly more sustainable as your codebase > grows. In such scenarios it becomes increasingly unlikely > that the correct notion of comparison on one of your many > abstract types coincides with the structural comparison > on whatever implementation those types happen to have today. > > Mark > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs