From: Yotam Barnoy <yotambarnoy@gmail.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Undefined recursive module
Date: Wed, 10 Sep 2014 19:16:37 -0400 [thread overview]
Message-ID: <CAN6ygO=q+hsugSMzFP-Hf3WM40yfORv+O8KYN3uQ=5n5OxNLdg@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=E2XnZ+r=WmH4Ao-4gSPCj6NBR2eV1OdOpokCm6Z7v4Tw@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 3294 bytes --]
Thank you for the detailed explanation. That makes sense, as much as I wish
there was a static detection mechanism in place for this kind of thing.
On Wed, Sep 10, 2014 at 6:50 PM, Jeremy Yallop <yallop@gmail.com> wrote:
> On 10 September 2014 23:26, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
> > On Wed, Sep 10, 2014 at 6:20 PM, Jeremy Yallop <yallop@gmail.com> wrote:
> >>
> >> On 10 September 2014 21:32, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
> >> > I just encountered this nasty RUNTIME error in my code. What does it
> >> > mean?
> >> > How does it happen?
> >>
> >> The behaviour is documented in the manual:
> >>
> >> http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc75
> >>
> >> See the paragraph beginning "Currently, the compiler requires [...]".
> >
> > Thanks. Does it make sense that changing a function definition from
> > point-free to an explicit definition should eliminate this exception?
>
> Yes, that's the expected behaviour. Initially the fields in a
> recursive module are bound to functions that raise an exception. The
> module initialization then overwrites each field with its actual
> value, which is determined by evaluating the expression on the right
> hand side of the field definition. If evaluating the right hand side
> involves reading one of the fields of the module that has not yet been
> overwritten then the field will resolve to the exception-raising
> function, which may lead to the runtime error that you've seen.
>
> Here's an example. Suppose you have a recursive module like this:
>
> module rec M1
> : sig val x : unit -> unit end =
> struct
> let x = M1.x
> end
>
> Then the initial state of the module at runtime has x bound to a
> function that raises an exception:
>
> module rec M1
> : sig val x : unit -> unit end =
> struct
> let x = fun _ -> raise Undefined_recursive_module
> end
>
> Module initialization then overwrites the field with the result of
> evaluating the right-hand side -- that is, by the result of evaluating
> M1.x:
>
> M1.x <- (fun _ -> raise Undefined_recursive_module)
>
> Calling M1.x will lead to the exception being raised:
>
> M1.x ()
> => raise Undefined_recursive_module
>
> Now consider what happens when you eta-expand the definition of x.
> Here's the source program
>
> module rec M2 : sig val x : unit -> unit end =
> struct
> let x = fun e -> M2.x e
> end
>
> The initial state of the module at runtime is the same as for M1:
>
> module rec M2 : sig val x : unit -> unit end =
> struct
> let x = fun _ -> raise Undefined_recursive_module end
> end
>
> Once again, module initialization overwrites the field with the result
> of evaluating the right-hand side. This time, however, evaluating the
> right-hand side doesn't require resolving M2.x, since M2.x is under a
> 'fun' binding:
>
> M2.x <- (fun e -> M2.x e)
>
> When you come to call M2.x the recursive reference resolves to the new
> value of the field and evaluation proceeds as expected:
>
> M2.x ()
> => M2.x ()
>
> As the manual says, all of this is subject to change, and it's best
> not to rely on the current behaviour. I recommend that you avoid
> using recursive modules for value-level recursion, if possible.
>
[-- Attachment #2: Type: text/html, Size: 4371 bytes --]
next prev parent reply other threads:[~2014-09-10 23:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-10 20:32 Yotam Barnoy
2014-09-10 22:20 ` Jeremy Yallop
2014-09-10 22:26 ` Yotam Barnoy
2014-09-10 22:50 ` Jeremy Yallop
2014-09-10 23:16 ` Yotam Barnoy [this message]
-- strict thread matches above, loose matches on Subject: below --
2004-07-27 1:23 [Caml-list] Undefined_recursive_module Hawkins, Thomas
2004-07-27 7:22 ` Alain Frisch
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAN6ygO=q+hsugSMzFP-Hf3WM40yfORv+O8KYN3uQ=5n5OxNLdg@mail.gmail.com' \
--to=yotambarnoy@gmail.com \
--cc=caml-list@inria.fr \
--cc=yallop@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox