caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* recursive modules: Cannot safely evaluate the definition
@ 2008-04-04  8:44 Hendrik Tews
  2008-04-04  9:00 ` [Caml-list] " Keiko Nakata
  0 siblings, 1 reply; 6+ messages in thread
From: Hendrik Tews @ 2008-04-04  8:44 UTC (permalink / raw)
  To: caml-list

Hi,

for the following program

    module type Result_sig = sig
      val result : int
    end

    module type Build_sig = sig
      val compute : int -> unit
    end

    module rec Rec : functor(Arg : Result_sig) -> Build_sig = 
      functor(Arg : Result_sig) ->
    struct
      let compute = function
	| 0 -> Printf.printf "result: %d\n" Arg.result
	| n ->
	    let module Arg_x_2 = struct
	      let result = Arg.result * 2
	    end
	    in
	    let module Rec_tail = Rec(Arg_x_2)
	    in
	      Rec_tail.compute (n-1)
    end


I get the compiler error 

  Cannot safely evaluate the definition of the recursively-defined module Rec

Could somebody explain to me why? As far as I can see, module Rec
is safe in the sense of section 7.9 of the reference manual. 

[Background: I am trying to solve the following Ocaml puzzle:
Build a (nested) functor application, where the functors applied
depend on an input value of the program. In particular the depth
of the functor application depends on the input value and could
be arbitrary high.]


Bye,

Hendrik


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

* Re: [Caml-list] recursive modules: Cannot safely evaluate the definition
  2008-04-04  8:44 recursive modules: Cannot safely evaluate the definition Hendrik Tews
@ 2008-04-04  9:00 ` Keiko Nakata
  2008-04-04 12:44   ` Jean-Christophe Filliâtre
  0 siblings, 1 reply; 6+ messages in thread
From: Keiko Nakata @ 2008-04-04  9:00 UTC (permalink / raw)
  To: tews; +Cc: caml-list

Hello.

I think that functors cannot be recursive in the current OCaml.

You may want to look at the paper found at:

caml.inria.fr/pub/papers/xleroy-recursive_modules-03.pdf

In particular, the footnote in page 6.

With best regards,
Keiko



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

* Re: [Caml-list] recursive modules: Cannot safely evaluate the definition
  2008-04-04  9:00 ` [Caml-list] " Keiko Nakata
@ 2008-04-04 12:44   ` Jean-Christophe Filliâtre
  2008-04-04 15:23     ` Hendrik Tews
  0 siblings, 1 reply; 6+ messages in thread
From: Jean-Christophe Filliâtre @ 2008-04-04 12:44 UTC (permalink / raw)
  To: Keiko Nakata; +Cc: tews, caml-list

Keiko Nakata wrote:
> Hello.
> 
> I think that functors cannot be recursive in the current OCaml.

They can:

-----------------------------------------------------------------
module type S = sig type t = int end

module rec F : functor(X : S) -> S = functor(X: S) -> struct
  type t = M.t
end
and M : S = F(struct type t = int end)

let x : M.t = 42
-----------------------------------------------------------------

But it is likely that you won't do anything useful with a functor like
this (try adding a "val x : t" in the signature and to define x in F as
equal to M.x and you'll bump on the "Cannot safely evaluate the
definition of ..." error).

-- 
Jean-Christophe


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

* Re: [Caml-list] recursive modules: Cannot safely evaluate the definition
  2008-04-04 12:44   ` Jean-Christophe Filliâtre
@ 2008-04-04 15:23     ` Hendrik Tews
  2008-04-04 15:45       ` Keiko Nakata
  0 siblings, 1 reply; 6+ messages in thread
From: Hendrik Tews @ 2008-04-04 15:23 UTC (permalink / raw)
  To: caml-list

Jean-Christophe Filliâtre <Jean-Christophe.Filliatre@lri.fr>
writes:

   Keiko Nakata wrote:
   > Hello.
   > 
   > I think that functors cannot be recursive in the current OCaml.

   They can:

   [example deleted]


For those who are interested but were too lazy to read the paper
that Keiko pointed to: In a set of recursive modules every
dependency cycle has to go through a safe module, whose signature
consists only of functions, lazy values and exceptions and
submodules with the same requirement.

Initialization first initializes all safe modules by providing
function bodies that throw execptions. Then initialize the
remaining modules in the order of their dependency. In the end
the functions in the safe modules are replace in place with the
right closures. For this replacement trick to work you need to
know the size of the safe modules. Functors are closures whose
size cannot determined from their signature, thus functors are
never safe.

This does not exclude functors in recursive modules. It only
means that apart from the functors you need at least one safe
module. See 7.9 in the reference manual for a very sensible
example with a functor.

If there are dependency cycles without a safe module, you
apparently get the "Cannot safely evaluate ..." error. 

If you have dependency cycles with two or more safe modules than
the compiler might choose an initialization order that does not
work. Then it might help to make some of the safe modules unsafe. 

Bye,

Hendrik


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

* Re: [Caml-list] recursive modules: Cannot safely evaluate the definition
  2008-04-04 15:23     ` Hendrik Tews
@ 2008-04-04 15:45       ` Keiko Nakata
  2008-04-04 16:17         ` Sebastien Ferre
  0 siblings, 1 reply; 6+ messages in thread
From: Keiko Nakata @ 2008-04-04 15:45 UTC (permalink / raw)
  To: caml-list

Thank you for the correction and citation.

>    Keiko Nakata wrote:
>    > I think that functors cannot be recursive in the current OCaml.

Yes, I was incorrect.
We have: 

module rec F: functor (X:sig end) -> sig val f : int -> int end = 
  functor(X:sig end) -> struct  
    let f x = if x = 0 then 0 else M.f (x-1)
  end

and M : sig val f: int -> int end = F(M)

This should imply interesting examples are available. 

Best,
Keiko


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

* Re: [Caml-list] recursive modules: Cannot safely evaluate the definition
  2008-04-04 15:45       ` Keiko Nakata
@ 2008-04-04 16:17         ` Sebastien Ferre
  0 siblings, 0 replies; 6+ messages in thread
From: Sebastien Ferre @ 2008-04-04 16:17 UTC (permalink / raw)
  To: Keiko Nakata, caml-list

Hi,

Keiko Nakata wrote:
> Thank you for the correction and citation.
> 
>>    Keiko Nakata wrote:
>>    > I think that functors cannot be recursive in the current OCaml.
> 
> Yes, I was incorrect.
> We have: 
> 
> module rec F: functor (X:sig end) -> sig val f : int -> int end = 
>   functor(X:sig end) -> struct  
>     let f x = if x = 0 then 0 else M.f (x-1)
>   end
> 
> and M : sig val f: int -> int end = F(M)
> 
> This should imply interesting examples are available. 

For such examples, you can look at my library LogFun, which
massively uses functors and recursive modules.

http://www.irisa.fr/LIS/ferre/logfun/

It defines a set of components, in the form of OCaml functors,
which can be composed in a recursive way. The most illustrative
component is the file 'mltype.ml' that defines a language of ML
types and patterns by a recursive combination of simpler components.
It is used in Camelis (http://www.irisa.fr/LIS/ferre/camelis/)
for browsing ML functions from a set of .mli files. It is not very
well documented, but if you are interested, you can contact me.

Best,
Sebastien


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

end of thread, other threads:[~2008-04-04 16:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-04  8:44 recursive modules: Cannot safely evaluate the definition Hendrik Tews
2008-04-04  9:00 ` [Caml-list] " Keiko Nakata
2008-04-04 12:44   ` Jean-Christophe Filliâtre
2008-04-04 15:23     ` Hendrik Tews
2008-04-04 15:45       ` Keiko Nakata
2008-04-04 16:17         ` Sebastien Ferre

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