English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Unable to define a polymorphic map method
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-04-17 (09:37)
From: Jean Balthazar <jean.balthazar.fr@g...>
Subject: Unable to define a polymorphic map method
Dear OCamlers,

I try to define a polymorphic container  encapsulating a list. I'd like to
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
me why it works for fold and not for map?

All the best,


class ['elt] container arg =

  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)

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