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 10:35:01 +0900 (JST)	[thread overview]
Message-ID: <20090408.103501.38023937.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <87bpr86kti.fsf@frosties.localdomain>

From: Goswin von Brederlow <goswin-v-b@web.de>
> Small extra question concerning this. Can I get ocaml to recognise a
> type like this?
> 
> type base = 'a. {
>   x : 'a;
>   fn : 'a -> unit;
> }
> 
> List.iter
>   (fun r -> r.fn r)
>   [{x = 1; fn = (fun r -> print_int r.x); };
>    {x = 1.2; fn = (fun r -> print_float r.x); }]
> 
> The difference to a "'a base" type would be that the 'a is only
> infered and fixed inside the record but remains abstract outside of
> it.

First reaction: but your "base" type is just a closure. But this is
certainly not your question.

More disturbing: the rest of your code does not agree with base.
So I will assume you actually meant

List.iter
  (fun r -> r.fn r.x)
  [{x = 1; fn = print_int}; {x = 1.2; fn = print_float}]

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...

Jacques Garrigue


  reply	other threads:[~2009-04-08  1:35 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 [this message]
2009-04-08  2:43     ` Jacques Garrigue
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.103501.38023937.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).