Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: keiko@kurims.kyoto-u.ac.jp
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] a function for polymorphic and monomorphic objects
Date: Fri, 09 Nov 2007 11:50:27 +0900 (JST)	[thread overview]
Message-ID: <20071109.115027.42788254.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20071108.180532.68538077.keiko@kurims.kyoto-u.ac.jp>

From: Keiko Nakata <keiko@kurims.kyoto-u.ac.jp>
> Can I define a function taking as argument 
> an object with a method, say, "map" whose type is instantiatable to
> (int -> int) -> int list?
> 
> I have two object; 
> one with a polymorphic map, the other with a monomorphic map.
> 
> let p = object 
>   method map : 'a.(int -> 'a) -> 'a list =  fun f -> List.map f [1;2;3]
> end;;
>   
> let q = object
>   method map : (int -> int) -> int list = fun f -> List.map f [1;2;3]
> end;;
> 
> I want to define a function like double below, 
> which is applicable to both p and q.
> 
> let double o = o#map (fun x -> x*2);;
> 
> The only workaround I came up with is
> 
> let double2 f = f (fun x -> x*2);;
> double2 p#map;;
> double2 q#map;;
> 
> Yet this is not very nice....

Currently, this is the only workaround available.
A possibility once considered was to allow subtyping between the type
of p and the type of q. This way you could use your function double,
after applying a coercion for p.
However this is not so easy technically (mixing subtyping and
polymorphism...), and there has never been much demand for such
subtyping. Indeed, if one is required to do a (double) coercion
anyway, then double2 is not much worse (actually it is even shorter
here...) 

Cheers,

Jacques Garrigue


      parent reply	other threads:[~2007-11-09  2:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-08  9:05 Keiko Nakata
2007-11-08 10:48 ` [Caml-list] " Andrej Bauer
2007-11-08 12:09   ` Keiko Nakata
2007-11-09  1:35     ` Peng Zang
2007-11-09  2:50 ` Jacques Garrigue [this message]

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=20071109.115027.42788254.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=keiko@kurims.kyoto-u.ac.jp \
    /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