From mboxrd@z Thu Jan 1 00:00:00 1970 X-Sympa-To: caml-list@inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q3HA3JOe008944 for ; Tue, 17 Apr 2012 12:03:19 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmcBAPw+jU/RVdQ2kGdsb2JhbABEsSUIIgEBAQEHCw0HFAQjggkBAQEDARICExkBGx0BAwELBgULDS4iAREBBQEcBhMihW+BbgEDBgWbJwqMIIJzhR4KGScNV4h2AQULkGwElW+OVj2EDIFa X-IronPort-AV: E=Sophos;i="4.75,433,1330902000"; d="scan'208";a="154374413" Received: from mail-vb0-f54.google.com ([209.85.212.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 17 Apr 2012 12:03:13 +0200 Received: by vbmv11 with SMTP id v11so7752037vbm.27 for ; Tue, 17 Apr 2012 03:03:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=i9x7S8YGM0ApSYcf5q6EclRQEe0EYsE1zm7MWzvwLsc=; b=vsGVGzLMY87GIzjeTGB7mzSrA+kljOQZKgG4z8mtHM/F1Lr4+Y/1T3gXeFgjzfpXf9 IAqXibF2O2zD8e2bhmZ62PsOmW/HQpSCOiroxPlqEBHolyINeAtMmN+iWCAdswJxQT03 bvuha9O30uG3xpUXTYw2exg2M4Gr9Li1wdn4FJRHcSncn1Bn+PV03zFbkYKBWjKDuyld bzsm7HJZ4AXsX8QaitpkznN5NC0Y5WF1QQf5CRxH37R+/U9tyoZL5nRYvTOor0OcEoUU hA5h9J5LY2hXLP0KVL+ZA14BiUm5lTbf5fbDqrapJt/JTt7KILBU3aDSrozLwUZfNTVJ 0fZg== Received: by 10.220.35.73 with SMTP id o9mr8418472vcd.74.1334656992588; Tue, 17 Apr 2012 03:03:12 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.92.97 with HTTP; Tue, 17 Apr 2012 03:02:52 -0700 (PDT) In-Reply-To: <4F86E6BC.7070706@frisch.fr> References: <4F86E0C0.8020003@frisch.fr> <4F86E6BC.7070706@frisch.fr> From: Philippe Veber Date: Tue, 17 Apr 2012 12:02:52 +0200 Message-ID: To: Alain Frisch Cc: Gabriel Scherer , Leo P White , caml Content-Type: multipart/alternative; boundary=485b393aaadff0b5a504bddd0bd6 X-Validation-by: philippe.veber@gmail.com Subject: Re: [Caml-list] [ANN] ocamlopen 1.0.2 --485b393aaadff0b5a504bddd0bd6 Content-Type: text/plain; charset=ISO-8859-1 2012/4/12 Alain Frisch > On 04/12/2012 04:16 PM, Philippe Veber wrote: > >> Isn't this a good use case for polymorphic variants too ? >> > > I don't see how to use polymorphic variants here. The message bus itself > need to provide functions like: > > val dispatch: message -> unit > val register_listener: (message -> unit) -> unit > > > How do you define the message type without having access to all possible > message constructors? I reckon you can't, but I mistakenly thought you could at least define the bus without writing the full message type: module Bus : sig type 'a t val create : unit -> 'a t val dispatch : 'a t -> 'a -> unit val register : 'a t -> ('a -> unit) -> unit end = struct type 'a t = ('a -> unit) list ref let create () = ref [] let dispatch bus msg = List.iter (fun f -> f msg) !bus let register bus f = bus := f :: !bus end let bus = Bus.create () let () = Bus.register bus (function `a -> print_char 'a') let () = Bus.register bus (function `b n -> print_int n) However this is not a legal program: let () = Bus.register bus (function `b n -> print_int n);; ^^^^ Error: This pattern matches values of type [< `b of 'a ] but a pattern was expected which matches values of type [< `a ] These two variant types have no intersection Well, I mentionned polymorphic variants because you can at least define listeners without knowing the full message type and even reuse them for buses with different message types. So you have a certain flexibility, compared to monomorphic types. But ok, maybe the remark wasn't even worth 2 cents :o). Cheers, ph. --485b393aaadff0b5a504bddd0bd6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

2012/4/12 Alain Frisch <alain@frisch.fr>
On 04/12/2012 04:16 PM, Philippe Veber wrote:
Isn't this a good use case for polymorphic variants too ?

I don't see how to use polymorphic variants here. =A0The message bus it= self need to provide functions like:

=A0val dispatch: message -> unit
=A0val register_listener: (message -> unit) -> unit


How do you define the message type without having access to all possible me= ssage constructors?
I reckon you can't, but I mistaken= ly thought you could at least define the bus without writing the full messa= ge type:

module Bus : sig
=A0 ty= pe 'a t
=A0 val create : unit -&g= t; 'a t
=A0 val dispatch : 'a t -> = 'a -> unit
=A0 val register : 'a= t -> ('a -> unit) -> unit
end
=3D
struct
=A0 type 'a t =3D ('a = -> unit) list ref

=A0 let create () =3D ref= []
=A0 let dispatch bus msg =3D
=A0=A0=A0 List.iter (fun = f -> f msg) !bus
<= span style=3D"font-family:courier new,monospace">=A0 let register bus f =3D=
=A0=A0=A0 bus :=3D f :: != bus
end


let = bus =3D Bus.create ()

let () =3D Bus.register b= us (function `a -> print_char 'a')
= let () =3D Bus.register bus (function `b n -> print_int n)

However this is not a legal program:

=A0 let () =3D Bus.register bus (function `b n ->= print_int n);;
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 ^^^^
Error: This pattern matches values of type [< `b of 'a ]
=A0=A0= =A0=A0=A0=A0 but a pattern was expected which matches values of type [< = `a ]
=A0=A0=A0=A0=A0=A0 These two variant types have no intersection
=
Well, I mentionned polymorphic variants because you can at least define= listeners without knowing the full message type and even reuse them for bu= ses with different message types. So you have a certain flexibility, compar= ed to monomorphic types.

But ok, maybe the remark wasn't even worth 2 cents :o).

Chee= rs,
ph.

--485b393aaadff0b5a504bddd0bd6--