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.

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to