caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Polymorphic function in reference cell
@ 2009-11-18 14:33 Hans Ole Rafaelsen
  2009-11-18 14:58 ` [Caml-list] " Marc de Falco
  0 siblings, 1 reply; 3+ messages in thread
From: Hans Ole Rafaelsen @ 2009-11-18 14:33 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] Polymorphic function in reference cell
  2009-11-18 14:33 Polymorphic function in reference cell Hans Ole Rafaelsen
@ 2009-11-18 14:58 ` Marc de Falco
  2009-11-18 16:59   ` Hans Ole Rafaelsen
  0 siblings, 1 reply; 3+ messages in thread
From: Marc de Falco @ 2009-11-18 14:58 UTC (permalink / raw)
  To: Hans Ole Rafaelsen; +Cc: caml-list

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

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 <hans@simula.no>

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

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

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

* Re: [Caml-list] Polymorphic function in reference cell
  2009-11-18 14:58 ` [Caml-list] " Marc de Falco
@ 2009-11-18 16:59   ` Hans Ole Rafaelsen
  0 siblings, 0 replies; 3+ messages in thread
From: Hans Ole Rafaelsen @ 2009-11-18 16:59 UTC (permalink / raw)
  To: Marc de Falco; +Cc: caml-list

Thanks Marc,

I have been experimenting some with modules as well. But I think it 
boils down to the same problem. I want to define a polymorphic function 
outside the module that uses it (at the top of my module hierarchy). So 
if the module UseFoo could be created using the function defined in the 
top module (Top.f_fun) for the f function, then I think I would be able 
to solve my problem.

module Bar = struct
     let f_full a b =
       Printf.printf "%s" a; b
end

module Foo (M : sig val f : 'a -> 'a end) =
   struct
     let use_f1 () = M.f 1
     let use_f2 () = M.f 1.0
   end

let f_fun b =
  Bar.f_full "dummy" b

module UseFoo =
  Foo(struct
    let f b = f_fun b
      end)


module Top =
struct

  (* This is the function we want to be "f_fun" in UseFoo *)
  let f_fun b =
    Bar.f_full "bar" b

  let a = UseFoo.use_f1 ()

  let b = UseFoo.use_f2 ()
end


Marc de Falco wrote:
> 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 <hans@simula.no <mailto:hans@simula.no>>
>
>     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
>
>


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

end of thread, other threads:[~2009-11-18 16:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-18 14:33 Polymorphic function in reference cell Hans Ole Rafaelsen
2009-11-18 14:58 ` [Caml-list] " Marc de Falco
2009-11-18 16:59   ` Hans Ole Rafaelsen

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