caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Glen Mével" <glen.mevel@crans.org>
To: Mikhail Mandrykin <mandrykin@ispras.ru>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] "map"-ing parameterized class types
Date: Fri, 27 Nov 2015 00:25:35 +0100	[thread overview]
Message-ID: <565794EF.3050501@crans.org> (raw)
In-Reply-To: <2885279.d7QpSRfKLJ@molnar>

Le 20/10/2015 13:57, Mikhail Mandrykin a écrit :
> Just in case an another possible work-around would be to use an abstract type 
> instead of a concrete nominal type as a proxy. This can be a little bit more 
> suitable as an abstract type can be later bound to the same structural type 
> and wrapping/unwrapping would become identity in the implementation. This is 
> more heavyweight, though, e.g:
> 
> module type S = sig
>   type 'a c'
>   val new_c : 'a -> 'a c'
> end
> 
> module rec F' :
>   functor (M : S) -> sig
>     class ['a] c : 'a -> object
>         method get : 'a
>         method map : 'b. ('a -> 'b) -> 'b M.c'
>       end
>   end =
>   functor (M : S) -> struct
>     class ['a] c (a : 'a) = object
>       method get = a
>       method map : 'b. (_ -> 'b) -> 'b M.c' = fun f -> M.new_c (f a)
>     end
>   end
> 
> module rec F : sig
>   type 'a c'
>   class ['a] c : 'a -> object
>       method get : 'a
>       method map : 'b. ('a -> 'b) -> 'b F.c'
>     end
>   val new_c : 'a -> 'a F.c'
>   val self_cast : 'a c' -> 'a c
> end = struct
>   module F' = F'(F)
>   class ['a] c = ['a] F'.c
>   type 'a c' = 'a c
>   let new_c a = new F.c a
>   let self_cast (x : 'a c') = (x : 'a c)
> end

nice solution! just saying, it can be simplified, with no need for functor:

    module rec F :
    sig
      class ['a] c : 'a -> object
        method get : 'a
        method map : 'b. ('a -> 'b) -> 'b F.t
      end
      type 'a t
      val make : 'a -> 'a t
      val cast : 'a t -> 'a c
    end =
    struct
      class ['a] c x = object
        method get = x
        method map : 'b. ('a -> 'b) -> 'b F.t = fun f -> F.make (f x)
      end
      type 'a t = 'a c
      let make x = new c x
      let cast x = x
    end

-- 
Glen Mével

  reply	other threads:[~2015-11-26 23:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-19 16:58 Spiros Eliopoulos
2015-10-19 18:14 ` Jeremy Yallop
2015-10-19 20:15   ` Spiros Eliopoulos
2015-10-20 11:57     ` Mikhail Mandrykin
2015-11-26 23:25       ` Glen Mével [this message]
2015-10-22 15:04     ` Oleg
2015-10-23  6:48       ` 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=565794EF.3050501@crans.org \
    --to=glen.mevel@crans.org \
    --cc=caml-list@inria.fr \
    --cc=mandrykin@ispras.ru \
    /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).