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 DC7907F6C4 for ; Tue, 21 Jan 2014 15:30:13 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of Jocelyn.SEROT@univ-bpclermont.fr) identity=pra; client-ip=195.221.122.150; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="Jocelyn.SEROT@univ-bpclermont.fr"; x-sender="Jocelyn.SEROT@univ-bpclermont.fr"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of Jocelyn.SEROT@univ-bpclermont.fr) identity=mailfrom; client-ip=195.221.122.150; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="Jocelyn.SEROT@univ-bpclermont.fr"; x-sender="Jocelyn.SEROT@univ-bpclermont.fr"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@ubpmes.univ-bpclermont.fr) identity=helo; client-ip=195.221.122.150; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="Jocelyn.SEROT@univ-bpclermont.fr"; x-sender="postmaster@ubpmes.univ-bpclermont.fr"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmQBADeD3lLD3XqWnGdsb2JhbABag0NWgn24cIEPFg4BAQEBAQgUCTyCJgEBBCMECwEFQRALGgImAgIfOAYTiAmoNpwtF4EpjSMzB4JvNYEUBI5ghVyDZQGBMo8RhQM X-IPAS-Result: AmQBADeD3lLD3XqWnGdsb2JhbABag0NWgn24cIEPFg4BAQEBAQgUCTyCJgEBBCMECwEFQRALGgImAgIfOAYTiAmoNpwtF4EpjSMzB4JvNYEUBI5ghVyDZQGBMo8RhQM X-IronPort-AV: E=Sophos;i="4.95,696,1384297200"; d="scan'208";a="54203465" Received: from ubpmes.univ-bpclermont.fr ([195.221.122.150]) by mail2-smtp-roc.national.inria.fr with ESMTP; 21 Jan 2014 15:30:13 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by ubpmes.univ-bpclermont.fr (Postfix) with ESMTP id 2617E7500CA3; Tue, 21 Jan 2014 15:30:13 +0100 (CET) X-Virus-Scanned: amavisd-new at univ-bpclermont.fr Received: from ubpmes.univ-bpclermont.fr ([127.0.0.1]) by localhost (ubpmes.univ-bpclermont.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id RXTPWwkMqwZ0; Tue, 21 Jan 2014 15:30:07 +0100 (CET) Received: from univ-bpclermont.fr (ubpwme.univ-bpclermont.fr [195.221.122.180]) by ubpmes.univ-bpclermont.fr (Postfix) with ESMTP id 339617500C15; Tue, 21 Jan 2014 15:30:07 +0100 (CET) Received: from lav63-2-88-164-92-250.fbx.proxad.net (lav63-2-88-164-92-250.fbx.proxad.net [88.164.92.250]) by wmail.univ-bpclermont.fr (Horde Framework) with HTTP; Tue, 21 Jan 2014 15:30:07 +0100 Date: Tue, 21 Jan 2014 15:30:07 +0100 Message-ID: <20140121153007.Horde.911-b1nON_CEk0EaoT-0QA2@wmail.univ-bpclermont.fr> From: SEROT Jocelyn To: Josh Berdine Cc: Leo White , caml-list References: <87ppnm5q2x.fsf@study.localdomain> <20140121120954.Horde.QQKslk0F5ldpqozcxKyq3g1@wmail.univ-bpclermont.fr> <87txcxlggc.fsf@berdine.net> In-Reply-To: <87txcxlggc.fsf@berdine.net> User-Agent: Internet Messaging Program (IMP) H5 (6.0.4) Content-Type: text/plain; charset=UTF-8; format=flowed; DelSp=Yes MIME-Version: 1.0 Content-Disposition: inline Content-Transfer-Encoding: 8bit Subject: Re: [Caml-list] Question on functors (again...) Josh Berdine a écrit : > > Shouldn't Elt.t and elt in SET be equal? The following typechecks, > though maybe not what you want: > >> [...] > > Cheers, Josh Thanks a lot Josh ! The magic line to add was : type elt = Elt.t in the SET signature.. This is so obvious afterwards that i should have thought about it right from the start (as often...) ;-) For those interested, here are the signature and implementation of a set module extending that provided in the std lib with a cartesian product operation (in fact two product ops) and a "string_of" op. Thx again. Best wishes Jocelyn (* mset.mli *) module type ELT = sig include Set.OrderedType val string_of: t -> string end module type SET = sig module Elt : ELT include Set.S with type elt = Elt.t val of_list: elt list -> t val string_of: t -> string end (* The [Make] functor builds an implementation of a Set given an implementation of its elements *) module Make (E : ELT) : SET with module Elt = E and type elt = E.t (* The [MakeProduct] functor builds an implementation the cartesian product of two sets *) module MakeProduct (S1: SET) (S2: SET) : sig include SET with type Elt.t = S1.elt * S2.elt val product : S1.t -> S2.t -> t end (* This is the signature of the last argument to the [MakeProduct'] functor. It describes how to build the product of two set elements *) module type MakePair = functor (E1: ELT) -> functor (E2: ELT) -> sig include ELT with type t = E1.t * E2.t val product : E1.t -> E2.t -> t end (* The [MakeProduct'] higher-order functor builds a customized implementation of the cartesian products of two sets when the product of the respective set elements is explicitely specified with a dedicated [MakePair] functor *) (* Note: the [MakeProduct] functor is just a specialization of [MakeProduct'] *) module MakeProduct' (S1: SET) (S2: SET) (F: MakePair) : sig include SET with type Elt.t = S1.elt * S2.elt val product : S1.t -> S2.t -> t end (* mset.ml *) module type ELT = sig include Set.OrderedType val string_of: t -> string end module type SET = sig module Elt : ELT include Set.S with type elt = Elt.t val of_list: elt list -> t val string_of: t -> string end let string_of_list sep f s = let rec h = function [] -> "" | [x] -> f x | x::xs -> f x ^ sep ^ h xs in h s module Make (E : ELT) : SET with module Elt = E and type elt = E.t = struct module Elt = E module S = Set.Make(E) include S let string_of s = "{" ^ string_of_list "," E.string_of (S.elements s) ^ "}" let of_list l = List.fold_left (fun s e -> S.add e s) S.empty l end module type MakePair = functor (E1: ELT) -> functor (E2: ELT) -> sig include ELT with type t = E1.t * E2.t val product : E1.t -> E2.t -> t end module MakeProduct' (S1: SET) (S2: SET) (F: MakePair) : sig include SET with type Elt.t = S1.elt * S2.elt val product : S1.t -> S2.t -> t end = struct module P = F(S1.Elt)(S2.Elt) module S = Make(P) include S let product s1 s2 = let f x y t = S.add (P.product x y) t in let g x t = S2.fold (f x) s2 t in S1.fold g s1 S.empty end module MakeProduct (S1: SET) (S2: SET) = MakeProduct' (S1) (S2) (functor (E1: ELT) -> functor (E2: ELT) -> struct type t = E1.t * E2.t let compare = Pervasives.compare let product x y = x,y let string_of (x,y) = "(" ^ E1.string_of x ^ "," ^ E2.string_of y ^ ")" end)