caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: goswin-v-b@web.de
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Subtyping
Date: Wed, 08 Apr 2009 11:43:35 +0900 (JST)	[thread overview]
Message-ID: <20090408.114335.221144129.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20090408.103501.38023937.garrigue@math.nagoya-u.ac.jp>

Here is a slightly more usable version, using helper functions to
avoid writing intermediate closures by hand.

  type 'a base = {x : 'a; fn : 'a -> unit}
  (* 4 next lines are boilerplate, could be auto-generated *)
  type 'b base_op = {bop: 'a. 'a base -> 'b}
  type base_wrapper = {base: 'b. 'b base_op -> 'b}
  let wrap a = {base = fun x -> x.bop a}
  let apply op w = w.base op

  let l =
    [wrap {x = 1; fn = print_int}; wrap {x = 1.2; fn = print_float}];;

  List.iter (apply {bop = fun r -> r.fn r.x}) l

The only thing you cannot abbreviate is the {bop = ...} part, as this
is where universality is checked, to ensure that no cross application
can be done.

Jacques Garrigue

> Then what you are asking for is existential types. There is no syntax
> for them in ocaml, but they can be encoded through universal types
> (interestingly, the dual is not true).
> 
> type 'a base = {x : 'a; fn : 'a -> unit}
> type 'b base_op = {bop: 'a. 'a base -> 'b}
> type base_wrapper = {base: 'b. 'b base_op -> 'b}
> 
> let l =
>   let a = {x = 1; fn = print_int}
>   and b = {x = 1.2; fn = print_float} in
>   [{base = fun x -> x.bop a}; {base = fun x -> x.bop b}]
> 
> List.iter (fun w -> w.base {bop = fun r -> r.fn r.x}) l
> 
> As you can see, the result is rather verbose, but this works.
> Fortunately, closure and objects are usually enough...


  reply	other threads:[~2009-04-08  2:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-07  5:48 Subtyping Goswin von Brederlow
2009-04-07  7:41 ` [Caml-list] Subtyping David MENTRE
2009-04-07 13:39   ` Peng Zang
2009-04-07 21:33     ` Goswin von Brederlow
2009-04-07 21:32   ` Goswin von Brederlow
2009-04-08  0:38 ` Goswin von Brederlow
2009-04-08  1:35   ` Jacques Garrigue
2009-04-08  2:43     ` Jacques Garrigue [this message]
2009-04-08  5:16     ` Goswin von Brederlow

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=20090408.114335.221144129.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /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;
as well as URLs for NNTP newsgroup(s).