From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 99C32BBAF for ; Wed, 18 Nov 2009 15:58:11 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApQBAEiaA0vRVdi/kGdsb2JhbACCHzGNO4QlhxM/AQEBAQkJDAcTA6pBhkSIawEDAwWENgSBb4h5 X-IronPort-AV: E=Sophos;i="4.44,765,1249250400"; d="scan'208";a="38315889" Received: from mail-px0-f191.google.com ([209.85.216.191]) by mail3-smtp-sop.national.inria.fr with ESMTP; 18 Nov 2009 15:58:10 +0100 Received: by pxi29 with SMTP id 29so835287pxi.1 for ; Wed, 18 Nov 2009 06:58:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type; bh=hddUXRM9umNztR6VGStQxWp7kjIxSqEaWu6pRNYOfEE=; b=ctNXGpJ8XPnDUXPoweZRzqrsS3bGlQW6d/I2FQXEzgD982UMQiKSGgl3PP0WhvfQyC w3iOFFYDoln5oXMxp/xNGc6kxody5YAJlgGdrvjbB3aNaYWzrtlwt2gZmgu3Z+/tT6oI PVou4Yztr2K4wMwuvZ77X9bzs81IaT98Xp2ys= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; b=tJ6HfXEAEidel3B0BEuudIfFrHH16xsumrobsBnhV2D+aEkYHH5SVpk0b6aHtwzMkU LwCLvMfjwqtuEY+Vod9SQ9rL+sB7N0pxzM+vYCoX3/QeJM57j+AnKRTy6R5N0DzvU1ST UvCWdbBFfH2ki8CB5YxYea/x1F6JwP+wguScs= MIME-Version: 1.0 Sender: marc.defalco@gmail.com Received: by 10.140.126.16 with SMTP id y16mr636575rvc.267.1258556289339; Wed, 18 Nov 2009 06:58:09 -0800 (PST) In-Reply-To: <4B0405CA.80704@simula.no> References: <4B0405CA.80704@simula.no> Date: Wed, 18 Nov 2009 15:58:09 +0100 X-Google-Sender-Auth: 7d298adac5b98b35 Message-ID: Subject: Re: [Caml-list] Polymorphic function in reference cell From: Marc de Falco To: Hans Ole Rafaelsen Cc: caml-list@inria.fr Content-Type: multipart/alternative; boundary=000e0cd254308e52c50478a6783c X-Spam: no; 0.00; functor:01 foo:01 sig:01 val:01 struct:01 foo:01 rafaelsen:01 struct:01 beginner's:01 ocaml:01 bug:01 functor:01 sig:01 val:01 rafaelsen:01 --000e0cd254308e52c50478a6783c Content-Type: text/plain; charset=ISO-8859-1 I might have misunderstood your problem, and I do not have a solution pertaining to your exact question, but hiding the function and its type inside a module argument for a functor does the trick, no ? For example, if you define module Foo (M : sig type t val f : t -> t end) = struct let use_f x = M.f x end You can define too modules Foo1 and Foo2 using your different functions. Marc 2009/11/18 Hans Ole Rafaelsen > Hi, > > I have a module that have several functions that take a polymorphic > function as part of their arguments (Foo.f1 and Foo.f2 in the example). > > module Foo = struct > > let f1 f = > f 1 > > let f2 f = > f 1.0 > > let f1_ref = (ref (fun _ -> raise (Failure "undefined") : ('a -> 'a) )) > let set_f1_ref f = > f1_ref := f > > let f2_ref = (ref (fun _ -> raise (Failure "undefined") : ('a -> 'a) )) > let set_f2_ref f = > f2_ref := f > > let use_f1 () = > !f1_ref 1 > > let use_f2 () = > !f2_ref 1.0 > > let internal_f v = > v > > let internal_f1 () = > internal_f 1 > > let internal_f2 () = > internal_f 1.0 > > end > > let f v = > v > > let a = Foo.f1 f > let b = Foo.f2 f > let () = Foo.set_f1_ref f > let () = Foo.set_f2_ref f > let c = Foo.use_f1 () > let d = Foo.use_f2 () > > > In my code I don't want to pass this function around to to all functions > where Foo.f1 is called. For this reason I would like to store this function > that Foo.f1 needs within Foo. (The function is created outside the module > and bound with values generated outside the module.) However you can not > store polymorphic functions in references. So I have to make one reference > for each concrete type it is used for. What I really would like is use it as > if it was defined within Foo, like internal_f and used like the internal_f1 > and internal_f2. > > Is it possible to only store one reference to this kind of function within > a module, or do I really have to write a reference for each usage of the > function? If I have to create a reference for each case, do anyone have some > trick to avoid code repetition? > > Regards, > > Hans Ole > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > --000e0cd254308e52c50478a6783c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I might have misunderstood your problem, and I do not have a solution perta= ining to your exact question,
but hiding the function and its type insi= de a module argument for a functor does the trick, no ?
For example, if = you define

module Foo (M : sig type t val f : t -> t end) =3D
=A0=A0 struct= let use_f x =3D M.f x end

You can define too modules Foo1 and Foo2 = using your different functions.

Marc

2009/11/18 Hans Ole Rafaelsen <hans@simula.no>
Hi,

I have a module that have several functions that take a polymorphic functio= n as part of their arguments (Foo.f1 and Foo.f2 in the example).

module Foo =A0=3D struct

=A0let f1 f =3D
=A0 f 1

=A0let f2 f =3D
=A0 f 1.0

=A0let f1_ref =3D (ref (fun _ -> raise (Failure "undefined") := ('a -> 'a) ))
=A0let set_f1_ref f =3D
=A0 f1_ref :=3D f

=A0let f2_ref =3D (ref (fun _ -> raise (Failure "undefined") := ('a -> 'a) ))
=A0let set_f2_ref f =3D
=A0 f2_ref :=3D f

=A0let use_f1 () =3D
=A0 !f1_ref 1

=A0let use_f2 () =3D
=A0 !f2_ref 1.0

=A0let internal_f v =3D
=A0 v

=A0let internal_f1 () =3D
=A0 internal_f 1

=A0let internal_f2 () =3D
=A0 internal_f 1.0

end

let f v =3D
=A0v

let a =3D Foo.f1 f
let b =3D Foo.f2 f
let () =3D Foo.set_f1_ref f
let () =3D Foo.set_f2_ref f
let c =3D Foo.use_f1 ()
let d =3D Foo.use_f2 ()


In my code I don't want to pass this function around to to all function= s where Foo.f1 is called. For this reason I would like to store this functi= on that Foo.f1 needs within Foo. (The function is created outside the modul= e and bound with values generated outside the module.) However you can not = store polymorphic functions in references. So I have to make one reference = for each concrete type it is used for. What I really would like is use it a= s if it was defined within Foo, like internal_f and used like the internal_= f1 and internal_f2.

Is it possible to only store one reference to this kind of function within = a module, or do I really have to write a reference for each usage of the fu= nction? If I have to create a reference for each case, do anyone have some = trick to avoid code repetition?

Regards,

Hans Ole

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.in= ria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--000e0cd254308e52c50478a6783c--