caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Unable to define a polymorphic map method
@ 2009-04-17  9:37 Jean Balthazar
  2009-04-17 12:01 ` [Caml-list] " Peng Zang
  0 siblings, 1 reply; 3+ messages in thread
From: Jean Balthazar @ 2009-04-17  9:37 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1228 bytes --]

Dear OCamlers,

I try to define a polymorphic container  encapsulating a list. I'd like to
define
a map and a fold method. I have read in the documentation how
to specify a polymorphic fold method but I'm unable to transpose
for the map method. I include in the code below a tentative: the map1 method
is not polymorphic. If I uncomment the map2 method, I have the error

This type scheme cannot quantify 'c :
it escapes this scope.

Do you have any idea how to solve this problem? And aditionnaly, could you
explain
me why it works for fold and not for map?

All the best,
Jean

******************************************************************************************************

class ['elt] container arg =
object

  val  _repr = arg

  method fold : 'b. ('b -> 'elt -> 'b) -> 'b -> 'b
    = fun f accu -> List.fold_left f accu _repr

  method map1 f = {< _repr = List.map f _repr >}

(*
  method map2 : 'c.('elt -> 'c) -> ('c container)
    = function f -> new container (List.map f _repr)
*)
end


let l = new container [1;2;3]

let fint = l#fold (fun acc x -> x + acc) 0
and fstring = l#fold (fun acc x -> (string_of_int x)^acc) "empty"


let mint = l#map1 (function x -> 0)
let mstring = l#map1 (function x -> "zero")

[-- Attachment #2: Type: text/html, Size: 1572 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Unable to define a polymorphic map method
  2009-04-17  9:37 Unable to define a polymorphic map method Jean Balthazar
@ 2009-04-17 12:01 ` Peng Zang
  2009-04-17 12:49   ` Jean Balthazar
  0 siblings, 1 reply; 3+ messages in thread
From: Peng Zang @ 2009-04-17 12:01 UTC (permalink / raw)
  To: caml-list; +Cc: Jean Balthazar, caml-list

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

See previous answers to this question:

http://groups.google.com/group/fa.caml/browse_thread/thread/9bb6ff19373371fb

I've used the classical solution thus far and have not run into any trouble..

Peng

On Friday 17 April 2009 05:37:52 am Jean Balthazar wrote:
> Dear OCamlers,
>
> I try to define a polymorphic container  encapsulating a list. I'd like to
> define
> a map and a fold method. I have read in the documentation how
> to specify a polymorphic fold method but I'm unable to transpose
> for the map method. I include in the code below a tentative: the map1
> method is not polymorphic. If I uncomment the map2 method, I have the error
>
> This type scheme cannot quantify 'c :
> it escapes this scope.
>
> Do you have any idea how to solve this problem? And aditionnaly, could you
> explain
> me why it works for fold and not for map?
>
> All the best,
> Jean
>
> ***************************************************************************
>***************************
>
> class ['elt] container arg =
> object
>
>   val  _repr = arg
>
>   method fold : 'b. ('b -> 'elt -> 'b) -> 'b -> 'b
>     = fun f accu -> List.fold_left f accu _repr
>
>   method map1 f = {< _repr = List.map f _repr >}
>
> (*
>   method map2 : 'c.('elt -> 'c) -> ('c container)
>     = function f -> new container (List.map f _repr)
> *)
> end
>
>
> let l = new container [1;2;3]
>
> let fint = l#fold (fun acc x -> x + acc) 0
> and fstring = l#fold (fun acc x -> (string_of_int x)^acc) "empty"
>
>
> let mint = l#map1 (function x -> 0)
> let mstring = l#map1 (function x -> "zero")
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD4DBQFJ6G+ffIRcEFL/JewRAmmdAKCYZabKzST8EgFhirfKumt0AwbP5ACYq4Qf
BIX7A9TN5v4zce0seJbMEA==
=mhdV
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Unable to define a polymorphic map method
  2009-04-17 12:01 ` [Caml-list] " Peng Zang
@ 2009-04-17 12:49   ` Jean Balthazar
  0 siblings, 0 replies; 3+ messages in thread
From: Jean Balthazar @ 2009-04-17 12:49 UTC (permalink / raw)
  To: peng.zang; +Cc: caml-list, caml-list

[-- Attachment #1: Type: text/plain, Size: 2216 bytes --]

Thank your for your answer, but it does not solve my actual
problem: suppose I have a data-structure with no cons (for example
a matrix). I know how to implement a map function but I don't know
how to type it as a method.

Jean

2009/4/17 Peng Zang <peng.zang@gmail.com>

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> See previous answers to this question:
>
>
> http://groups.google.com/group/fa.caml/browse_thread/thread/9bb6ff19373371fb
>
> I've used the classical solution thus far and have not run into any
> trouble..
>
> Peng
>
> On Friday 17 April 2009 05:37:52 am Jean Balthazar wrote:
> > Dear OCamlers,
> >
> > I try to define a polymorphic container  encapsulating a list. I'd like
> to
> > define
> > a map and a fold method. I have read in the documentation how
> > to specify a polymorphic fold method but I'm unable to transpose
> > for the map method. I include in the code below a tentative: the map1
> > method is not polymorphic. If I uncomment the map2 method, I have the
> error
> >
> > This type scheme cannot quantify 'c :
> > it escapes this scope.
> >
> > Do you have any idea how to solve this problem? And aditionnaly, could
> you
> > explain
> > me why it works for fold and not for map?
> >
> > All the best,
> > Jean
> >
> >
> ***************************************************************************
> >***************************
> >
> > class ['elt] container arg =
> > object
> >
> >   val  _repr = arg
> >
> >   method fold : 'b. ('b -> 'elt -> 'b) -> 'b -> 'b
> >     = fun f accu -> List.fold_left f accu _repr
> >
> >   method map1 f = {< _repr = List.map f _repr >}
> >
> > (*
> >   method map2 : 'c.('elt -> 'c) -> ('c container)
> >     = function f -> new container (List.map f _repr)
> > *)
> > end
> >
> >
> > let l = new container [1;2;3]
> >
> > let fint = l#fold (fun acc x -> x + acc) 0
> > and fstring = l#fold (fun acc x -> (string_of_int x)^acc) "empty"
> >
> >
> > let mint = l#map1 (function x -> 0)
> > let mstring = l#map1 (function x -> "zero")
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.7 (GNU/Linux)
>
> iD4DBQFJ6G+ffIRcEFL/JewRAmmdAKCYZabKzST8EgFhirfKumt0AwbP5ACYq4Qf
> BIX7A9TN5v4zce0seJbMEA==
> =mhdV
> -----END PGP SIGNATURE-----
>

[-- Attachment #2: Type: text/html, Size: 3100 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-04-17 12:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-17  9:37 Unable to define a polymorphic map method Jean Balthazar
2009-04-17 12:01 ` [Caml-list] " Peng Zang
2009-04-17 12:49   ` Jean Balthazar

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