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 E73C67F8BE for ; Fri, 2 May 2014 01:43:28 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of lpw25@cam.ac.uk) identity=pra; client-ip=131.111.8.140; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="lpw25@cam.ac.uk"; x-sender="lpw25@cam.ac.uk"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of lpw25@cam.ac.uk) identity=mailfrom; client-ip=131.111.8.140; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="lpw25@cam.ac.uk"; x-sender="lpw25@cam.ac.uk"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@ppsw-40.csi.cam.ac.uk) identity=helo; client-ip=131.111.8.140; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="lpw25@cam.ac.uk"; x-sender="postmaster@ppsw-40.csi.cam.ac.uk"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnIBAM/bYlODbwiMnGdsb2JhbABarnyaCoEUFg4BAQEBAQgLCQkUKIIlAQEEAScTPwULCyElDwEEDTwTiC0DCQgEwy0NV4VuF4VWhmWCFweEOQSXPYFyjROJDw X-IPAS-Result: AnIBAM/bYlODbwiMnGdsb2JhbABarnyaCoEUFg4BAQEBAQgLCQkUKIIlAQEEAScTPwULCyElDwEEDTwTiC0DCQgEwy0NV4VuF4VWhmWCFweEOQSXPYFyjROJDw X-IronPort-AV: E=Sophos;i="4.97,968,1389740400"; d="scan'208";a="71543361" Received: from ppsw-40.csi.cam.ac.uk ([131.111.8.140]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 02 May 2014 01:43:27 +0200 X-Cam-AntiVirus: no malware found X-Cam-ScannerInfo: http://www.cam.ac.uk/cs/email/scanner/ Received: from host5-81-227-48.range5-81.btcentralplus.com ([5.81.227.48]:56716 helo=study.localdomain) by ppsw-40.csi.cam.ac.uk (smtp.hermes.cam.ac.uk [131.111.8.156]:587) with esmtpsa (PLAIN:lpw25) (TLSv1.2:DHE-RSA-AES128-SHA:128) id 1Wg0dS-0007TD-l7 (Exim 4.82_3-c0e5623) (return-path ); Fri, 02 May 2014 00:43:26 +0100 From: Leo White To: Berke Durak Cc: caml-list References: X-Face: "XWxb[u_Z\PA_Y?9@|IA!!+jTb(/290-*ea/Un$I0B98.$n%eL.;2w*,z]WR#T:,p[ NBd++M7l]#7zj7!{~iw $W-"/=|dVjhT[D{4~gE}gK<2`.6fs!;uqqud]vs2N/3^m7{aS1V, Date: Fri, 02 May 2014 00:55:22 +0100 In-Reply-To: (Berke Durak's message of "Thu, 1 May 2014 16:58:09 -0400") Message-ID: <87ppjxhx5h.fsf@study.localdomain> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: [Caml-list] Trying to define a functor combining polymorphic variants Hi, I'm afraid that I don't think you can do what you are trying to do. Without going into full detail, consider what would happen with your imagined functor `PROD` in the following case: module A = struct type message = [`Alpha of int] let string_of_message = function `Alpha i -> string_of_int i end module B = struct type message = [`Alpha of string] let string_of_message = function `Alpha s -> s end let b : B.message = `Alpha "hello" module AB = PROD(A)(B) let bad = AB.string_of_message b `b` would match the pattern for `#A.message`, and you would get a segfault. It is not sufficient to restrict `A.message` and `B.message` to be polymorphic variants, they must be polymorphic variants that share no tags. Regards, Leo Berke Durak writes: > Hello all, > > I have been using the following kind of construct: > > module A = struct > type message = [`Alpha] > let string_of_message = function `Alpha -> "alpha" > end > > module B = struct > type message = [`Beta] > let string_of_message = function `Beta -> "beta" > end > > module AB = struct > type message = [ A.message | B.message ] > let string_of_message = function > | #A.message as msg -> A.string_of_message msg > | #B.message as msg -> B.string_of_message msg > end > > So I naturally wanted to write a functor that does what the module AB does: > > module type S = sig > type message > val string_of_message : message -> string > end > > module PROD(A : S)(B : S) = struct > type message = [ A.message | B.message ] > let string_of_message = function > | #A.t as msg -> A.string_of_message msg > | #B.t as msg -> B.string_of_message msg > end > > But we (me + people on #ocaml: mrvn, drup, ggole, whitequark...) couldn't find a > way to specify, in the signature S, that message is a polymorphic variant so > that [ A.message | B.message ] is legal. > > We tried things like: > > module type S = sig type 'a t = ([> ] as 'a) end > > module PROD(X : S)(Y : S) = struct > type ('a,'b) t = [ 'a X.t | 'b Y.t ] > end > > but all we get is: > > Error: The type [> ] A.message is not a polymorphic variant type > > Any suggestions? > -- > Berke Durak