Loading ...
Sorry, an error occurred while loading the content.

Frames

Expand Messages
  • Pupeno
    Hello people, Sorry for not doing my task as a organizer of this group, I couldn t keep up myself with the course, I m sorry. Anyway, I m trying to
    Message 1 of 3 , Jun 4, 2005
    • 0 Attachment
      Hello people,
      Sorry for not doing my task as a 'organizer' of this group, I couldn't keep up
      myself with the course, I'm sorry.
      Anyway, I'm trying to continue... maybe this forum can be turned in a steady
      studing place where anyone can be at his/her own frecuency and still ask
      questions, let's try that!
      Anyway, I have just watched lecture 5a[1], where the concept of environment
      and frames are introduced. Taking the example of make-counter the following
      code is run:
      (define make-counter
      (lambda (n)
      (lambda ()
      (set! n (inc n))
      x)))
      (define c1 (make-counter 0))
      (define c2 (make-counter 0))
      leading the to environment drawn in the side blackboard. Then he run
      (c1)
      (c2)
      (c1)
      (c2)
      modifing the environment. I'm attaching an svg file represting the modified
      environment (and a png for those that can't handel the svg).
      Now, in the environment he draw the class to make-counter, but he didn't draw
      the calls to c1 and c2 (which would be composed of some empty frames). I'm
      not quite sure where those frames and calls would be drawn, can anybody tell
      me that ?
      Thanks.
      --
      Pupeno <pupeno@...> (http://pupeno.com)
      Reading ? Science Fiction ? http://sfreaders.com.ar

      [1] Watching the lectures is easier than reading the book, so, I'm going to
      watch all the lectures and then maybe read the book.
    • John Berthels
      Hi Pupeno, A slow response, because I only reached lecture 5a today :-) You ve probably sorted this out in your head already, but I ll reply because it ll help
      Message 2 of 3 , Jun 27, 2005
      • 0 Attachment
        Hi Pupeno,

        A slow response, because I only reached lecture 5a today :-) You've
        probably sorted this out in your head already, but I'll reply because
        it'll help me to put my thoughts in order and others may correct me or
        clarify.

        > Now, in the environment he draw the class to make-counter, but he didn't draw
        > the calls to c1 and c2 (which would be composed of some empty frames). I'm
        > not quite sure where those frames and calls would be drawn, can anybody tell
        > me that ?

        This is interesting. Each evaluation of a procedure creates a new
        frame, binding the procedure arguments for that invocation to concrete
        values. So if we have a procedure (which is a pair, the procedure
        instructions and a pointer to the environment in which the procedure
        was created):

        (define (add5 n) (+ 5 n))

        then evaluating

        (add5 6)

        would create a frame. This frame would have n bound to 6, and form an
        environment by chaining off to the environment stored in the
        procedure.

        Now, we've seen frames/environments created by procedure application
        before, the N=2 and N=12 in your diagram. However, the difference is
        that those frames are referenced by the procedure, and the procedures
        are in turn referenced by the name bound to the procedure.

        So...some frames are "transient", in that they need to exist only for
        the duration of the procedure evaluation. Other frames will persist,
        since references exist to them (and they are ultimately rooted in the
        global environment, as seen in your diagram).

        Which I guess will lead us nicely into the subject of garbage
        collection, in that there is no difference between the permanent and
        temporary frames, just that when we lose all references to them, they
        may as well not exist (and so they get garbage collected).

        This is also a nice way to think about lambda. A lambda creates a
        procedure, which includes a frame. This allows us to save a reference
        to the procedure, and hence the frame, hence the frame persists.

        So I guess environments+frames lead nicely into some of the really
        lispy ideas...

        regards,

        jb
      • John Berthels
        ... I ve confused myself a little here. A lambda creates a procedure which includes a reference to the enclosing environment, i.e. to the frame created for the
        Message 3 of 3 , Jun 27, 2005
        • 0 Attachment
          I wrote earlier:

          > This is also a nice way to think about lambda. A lambda creates a
          > procedure, which includes a frame. This allows us to save a reference
          > to the procedure, and hence the frame, hence the frame persists.

          I've confused myself a little here. A lambda creates a procedure which
          includes a reference to the enclosing environment, i.e. to the frame
          created for the procedure invocation in which the lambda is running.

          Rule 1: lambda creates a procedure (which is code+environment)
          Rule 2: procedure application creates a new frame (binding the args
          for this application) which chains to the procedure's environment.

          So...a frame is associated with a procedure invocation. It will only
          persist once the procedure returns if a reference to the frame is
          captured. Which is pretty much iff a lambda is executed in the
          procedure body and that lambda value leaves the procedure, either as
          the return value or by being set! into another variable.

          jb
          --
          john@...
        Your message has been successfully submitted and would be delivered to recipients shortly.