caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dmitry Grebeniuk <gdsfh1@gmail.com>
To: Jacques Garrigue <caml-list@inria.fr>
Subject: Re: [Caml-list] recursive module, object types, tying knot
Date: Tue, 13 Sep 2011 07:56:06 +0300	[thread overview]
Message-ID: <CAPi0vKX27x8pgMi9uHCRdAK0tBfZ7A9PEnfw+gyMNfxHfKSXCA@mail.gmail.com> (raw)
In-Reply-To: <AD8C45FF-E066-4879-AB24-00C4278BA26A@math.nagoya-u.ac.jp>

Hello.

>> I can't constrain the parametric type of module:
>> let module M = (lst#mapmodule : Mappable with type t 'a = list a)
>> -- one more way is blocked (but don't know was it the way
>> really).
>
> This is indeed a restriction of locally abstract types that one
> cannot use them as row variables. This might be done if there
> are applications.

  I don't know whether it can help.  In general, there seems to be
no way to write a type of function that is polymorphic on some
parametric type:
"forall _ t, forall 'a, 'b, map: ('a -> 'b) -> 'a t -> 'b t".
  Maybe something like this could work:
module type MAP = sig
  type 'a t
  val map : ('a -> 'b) -> 'a t -> 'b t
end
let gen_map1 =
  fun (type 'a tt) ->
  fun mapmodule ->
  let module M = (val mapmodule : MAP with type 'a t = 'a tt) in
  fun f -> fun (x : 'a tt) -> M.map f x
(* or *)
let gen_map2 mapmodule =
  let module M = (val mapmodule : MAP) in
  fun f (x : 'a M.t) -> M.map f x

  But the changes in typechecker should be too massive
to make it work.  So, it's better to forget my perfectionistic
attempts.

  Anyway, I've learned many things about OCaml type
system, so this was not completely useless work for me.

  And if some code should use "any mappable data
structure", there is no other way than wrapping this code
into functor.

> On the other hand, fold can be defined as a polymorphic
> method, allowing you to define functions building various kinds
> of containers, starting from an arbitrary one.

  I've tried to write some code where the initial container is
stored in the object itself, but couldn't make it work.  And
this approach (map = fold_right + cons) is somewhat limited,
it will work good for single-linked lists, but I can't imagine
it can work with more complex data structures like trees
(and I can imagine the overhead of folding + consing
over arrays).  So I prefer to avoid such style of mapping.

  reply	other threads:[~2011-09-13  4:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-07 21:29 Dmitry Grebeniuk
2011-09-07 21:44 ` Jacques Garrigue
2011-09-09 11:14   ` Dmitry Grebeniuk
2011-09-09 13:50     ` Jacques Garrigue
2011-09-13  4:56       ` Dmitry Grebeniuk [this message]
2011-09-13  7:22         ` Gabriel Scherer

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=CAPi0vKX27x8pgMi9uHCRdAK0tBfZ7A9PEnfw+gyMNfxHfKSXCA@mail.gmail.com \
    --to=gdsfh1@gmail.com \
    --cc=caml-list@inria.fr \
    /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).