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 4227C7EE25 for ; Thu, 14 Nov 2013 11:39:55 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of of12343@my.bristol.ac.uk) identity=pra; client-ip=209.85.214.52; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="of12343@my.bristol.ac.uk"; x-sender="of12343@my.bristol.ac.uk"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of of12343@my.bristol.ac.uk) identity=mailfrom; client-ip=209.85.214.52; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="of12343@my.bristol.ac.uk"; x-sender="of12343@my.bristol.ac.uk"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-bk0-f52.google.com) identity=helo; client-ip=209.85.214.52; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="of12343@my.bristol.ac.uk"; x-sender="postmaster@mail-bk0-f52.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Am4BAJ2nhFLRVdY0m2dsb2JhbABaxFMWDgEBAQEBBgsLCRQoglNGgSEBBQEiLodmBAGeEYMElWaJFJMGgREDnk6JYkGEUw X-IPAS-Result: Am4BAJ2nhFLRVdY0m2dsb2JhbABaxFMWDgEBAQEBBgsLCRQoglNGgSEBBQEiLodmBAGeEYMElWaJFJMGgREDnk6JYkGEUw X-IronPort-AV: E=Sophos;i="4.93,699,1378850400"; d="scan'208";a="42715862" Received: from mail-bk0-f52.google.com ([209.85.214.52]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 14 Nov 2013 11:39:54 +0100 Received: by mail-bk0-f52.google.com with SMTP id u14so797854bkz.11 for ; Thu, 14 Nov 2013 02:39:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:content-type:content-transfer-encoding :subject:message-id:date:to:mime-version; bh=mqRz7TdqIzPsQev2WzJHDiA83XODnKGU7RG3EPAVLQ0=; b=UaTBoRf5c929AH7Ud8l8bzE1/P1iD50DYb6RkkjZy+qFQMzDC/gCqT6dJkxjNwQl8E fDj9UrCd33rNIuAX6udxMJcOXYCD6lDOre8Q42JRjjgaHcV1TsUue5hLxyIHoE6gp4Sr SnREOUWG2wTA7rvs8rilp2q+oRpyTXQCkQAsg65Ul381PurD9e77XKycZQAuR1LylmHp KaGOhjByt7gevin21Cp/uIZehknKmZR8joTkVrvcLcHl1LmHrHTxfbYlHaiG3zImhbkb 1DI4KGzMjy/cNFItI4odYUn7SI2TC59xtC2wDedRPfDSFwl/aeoHO/NI0hBFZ5lJPOW0 MPwg== X-Gm-Message-State: ALoCoQkx3Wfu70C3fDMx1S79WKBR7Za6B0kNYhTnD8HylkPrAw7g4jguhnycaxphoAagmc2xiemW X-Received: by 10.204.229.12 with SMTP id jg12mr821867bkb.79.1384425594059; Thu, 14 Nov 2013 02:39:54 -0800 (PST) Received: from [10.0.0.92] (94.197.121.34.threembb.co.uk. [94.197.121.34]) by mx.google.com with ESMTPSA id it1sm2333808bkb.17.2013.11.14.02.39.52 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 14 Nov 2013 02:39:53 -0800 (PST) From: Ollie Frolovs Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Message-Id: <7389AC6E-C25D-43B3-9A39-E59F92EF35AB@my.bristol.ac.uk> Date: Thu, 14 Nov 2013 10:39:49 +0000 To: caml users Mime-Version: 1.0 (Mac OS X Mail 7.0 \(1822\)) X-Mailer: Apple Mail (2.1822) Subject: [Caml-list] Creating a Map from custom type Hello, list! I=92m learning how to use Maps in OCaml and after having read the relevant = chapter in Real World Ocaml i still do not understand what i need to do to = solve my problem =96 I defined a datatype in subroutes.mli: open Core.Std (** A data structure to store solved subproblems for=20 dynamic programming (Bellman 1961) approach to solving TSP *) type t (** The empty data structure *) val empty : t (** Store a solution to a subproblem *) val add : t -> (int list * int) -> (int * int list) -> t (** Find a solution to a subproblem *) val find : t -> (int list * int) -> (int * int list) option (** Convert the data structure to an association list. Useful for printing but much too slow for anything else *) val to_list : t -> ((int list * int) * (int * int list)) list 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 (i= nt list * int) and the values have the type (int * int list). If i understo= od it correctly, because the key is a tuple and not one of the built-in sim= ple data types (string, int, etc), because of this i have to provide a cust= om comparator. This is first thing that i am not sure of. Assuming that i do need to provide a comparator, what i don=92t understand = is this =96 On page 272 of RWO they create a comparator for a string to int Map as foll= owing: module Reverse =3D Comparator.Make(struct type t =3D string let sexp_of_t =3D String.sexp_of_t let t_of_sexp =3D String.t_of_sexp let compare x y =3D String.compare y x end);; What is the type t =96 is it the type of the key only? Would, in my case it= be type t =3D (int list * int) ? Is there an easy way to have sexp part auto-generated/ignored? They discuss= =93with sexp=94 on later pages but it uses functors (which i am not yet fa= miliar with) and i found that example very confusing. Also, i do not understand the purpose of the comparison function. I just st= ore different subroutes, there really isn=92t anything i would =93compare= =94 them with/for. I understand the compare function is necessary for the M= ap to be able to find things, but how do i define it for my type in the mos= t painless way? I guess what i want, ideally, is like a hash from Python where i could just= stuff a key and value into and not worry about the implementation. I under= stand that OCaml is strongly typed, so it cannot be that simple but as of n= ow i don=92t know what to do to have this problem solved. I googled for exp= lanations but did not find anything useful yet.=20 Please help?! =97 Ollie