From: Leo White <lpw25@cam.ac.uk>
To: Berke Durak <berke.durak@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Trying to define a functor combining polymorphic variants
Date: Fri, 02 May 2014 00:55:22 +0100 [thread overview]
Message-ID: <87ppjxhx5h.fsf@study.localdomain> (raw)
In-Reply-To: <CAALTfKDwuX06h7BmnOoe30xoy2c_tBHM+3TW3L9pj_L8L4wZkg@mail.gmail.com> (Berke Durak's message of "Thu, 1 May 2014 16:58:09 -0400")
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 <berke.durak@gmail.com> 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
next prev parent reply other threads:[~2014-05-01 23:43 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-01 20:58 Berke Durak
2014-05-01 23:55 ` Leo White [this message]
2014-05-02 6:14 ` Jacques Garrigue
2014-05-02 15:50 ` Berke Durak
2014-05-03 1:43 ` Jacques Garrigue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ppjxhx5h.fsf@study.localdomain \
--to=lpw25@cam.ac.uk \
--cc=berke.durak@gmail.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox