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 B8DB27EE4A for ; Mon, 16 Sep 2013 09:42:20 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=pra; client-ip=212.227.17.11; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of goswin-v-b@web.de) identity=mailfrom; client-ip=212.227.17.11; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="goswin-v-b@web.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mout.web.de) identity=helo; client-ip=212.227.17.11; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="goswin-v-b@web.de"; x-sender="postmaster@mout.web.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtICAGW1NlLU4xELlGdsb2JhbABav06FOIEYFg4BAQEBBwsLCRIqgiUBAQUnCwFWCxgJJQ8FDRs0h3ABAxOwLR8rDYkmjQWCdRaDCIEAA5YSgWiGHYYliFk X-IPAS-Result: AtICAGW1NlLU4xELlGdsb2JhbABav06FOIEYFg4BAQEBBwsLCRIqgiUBAQUnCwFWCxgJJQ8FDRs0h3ABAxOwLR8rDYkmjQWCdRaDCIEAA5YSgWiGHYYliFk X-IronPort-AV: E=Sophos;i="4.90,913,1371074400"; d="scan'208";a="33002595" Received: from mout.web.de ([212.227.17.11]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 16 Sep 2013 09:42:19 +0200 Received: from frosties.localnet ([95.208.119.3]) by smtp.web.de (mrweb002) with ESMTPSA (Nemesis) id 0MfqWi-1VXcVj2SKS-00NEEA for ; Mon, 16 Sep 2013 09:42:19 +0200 Received: from mrvn by frosties.localnet with local (Exim 4.80) (envelope-from ) id 1VLTRr-0002ie-2k for caml-list@inria.fr; Mon, 16 Sep 2013 09:42:19 +0200 Date: Mon, 16 Sep 2013 09:42:19 +0200 From: Goswin von Brederlow To: caml-list@inria.fr Message-ID: <20130916074218.GB9309@frosties> References: <87txhlbdna.fsf@golf.niidar.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Provags-ID: V03:K0:tvvouIel2gOkOcADmo0CRlb/4Xq4QzslYQWiV5kA91WzNODrd3Z ilkTX2tTJKMwZ4KvsjGQHBu2B6dK6kMCyLILOTyg9CKHomU+bcXgGEENsg8GMnHadzkB/U1 WCMYjA5trQ6I3AigwBLIwBDL1PPZdS2awnhCNggOdof56mzt7fVG5CQEcpMLWpf9BdpOA3u oiPSec+J6PMXeZ4AxbT8w== Subject: Re: [Caml-list] "subclassing" a char map On Sun, Sep 15, 2013 at 09:31:34PM -0700, Martin DeMello wrote: > Ah, thanks, that will do very nicely. I just wanted a type of char map > I couldn't insert anything else other than A-Z into, that the > typechecker would prevent me from mixing with the regular char map. > > martin > > On Sun, Sep 15, 2013 at 9:20 PM, Ivan Gotovchits wrote: > > Martin DeMello writes: > > > >> I have a char multiset defined via > >> > >> module MultiSet = Map.Make(struct type t = char let compare = compare end) > >> > >> Now I would like to split off a distinct type that can only contain > >> A-Z as keys. What's the best way to do this? > >> > >> martin > > > > Not sure, that I correctly understood your needs... but you can > > implement «A-Z keys» as an abstract type, contained in a module with the > > following signature: > > > > module type Caps = sig > > type t > > val create: char -> t option > > val compare: t -> t -> int > > val project: t -> char > > end > > > > Next, you can instatiate a Map from abstract type Cap.t to 'a: > > > > module CapMap = Map.Make(Caps) > > > > where Caps is an implementation, conforming to the signature Caps. For > > example like this: > > > > module Caps : Caps = struct > > type t = char > > let create = function > > | 'A'..'Z' as ch -> Some ch > > | otherwise -> None > > let compare = compare > > let project ch = ch > > end I recommend using a private type (in the module type) over an abstract one: type t = private char That way a type t instance can only be constructed in the module, where you check the upper case, but can be used in place of a char otherwise. No need to manually project it everywhere. MfG Goswin