Mutation via Explicit Ref Cells - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Mutation via Explicit Ref Cells

Description:

A store maps locations to values (an abstracted form of a machine memory) ... The store and location environment must be passed to all subfunctions that refer ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 18
Provided by: tri5114
Category:
Tags: cells | explicit | mutation | ref | via

less

Transcript and Presenter's Notes

Title: Mutation via Explicit Ref Cells


1
Mutation via Explicit Ref Cells
  • Eric Allen
  • Rice University
  • September 20, 2002

2
Mutation via Implicit Boxing
  • Requires modification of environment
    implementation
  • Requires modification of variable binding
  • Requires modification of variable lookup

3
Mutation via Explicit Boxing
  • (ref M), which creates a ref cell
  • (! M), which assumes that M will evaluate to a
    ref cell
  • ( M1 M2) which replaces the contents of that M1
    with the value M2 reduces to.

4
Evaluating Ref Cells
  • ((ref? M)
  • (box (MEval (ref-init M) env)))

5
Evaluating !
  • ((!? M)
  • (unbox (MEval (!-expr M) env)))

6
Evaluating
  • ((? M)
  • (set-box! (MEval (-lhs M) env)
  • (MEval (-rhs M) env))

7
Advantages of Explicit Boxing
  • Orthogonal design None of the existing code was
    modified
  • Programmers are made more aware of a decision to
    use mutation
  • Can simulate call-by-reference through explicit
    boxing

8
Disadvantages of Explicit Boxing
  • Programming language becomes more complex
  • Programming language becomes wordier
  • Semantics may not be as intuitive

9
Minimizing Mutation
  • As weve shown, adding mutation to a language
    strongly impacts the complexity of the language
  • For this same reason, using mutation in a program
    quickly increases the complexity of the program

10
Values in Algol-like Languages
  • In Scheme, ML, Java, etc., mutable objects are
    values
  • In Algol, C, C, etc., values are immutable!
  • Pass-by-value creates copies. How to share?

11
Values in Algol-like Languages
  • Share with passed pointers
  • Pointers are passed-by-value (like ints)
  • Dereferencing pointers allows sharing
  • How are pointers different than boxes?

12
Modeling Mutation Mathematically
  • Both means of modeling mutation weve discussed
    rely on mutation themselves
  • Why might we want to understand mutation through
    a purely functional world?
  • How can we understand mutation in such a world?

13
Attempt 1
  • Construct a new environment, and pass recursively
    to interpreter
  • In this new environment, bind variables to their
    new values
  • Why doesnt this approach work?

14
Attempt 2
  • Let Meval return closures instead of values
  • Then environments can be passed to the calling
    contexts
  • Sharing is not modeled! Consider closures with
    references to the shared data.

15
Solution
  • Introduce two environments
  • A location environment maps variables to
    locations (boxes)
  • A store maps locations to values (an abstracted
    form of a machine memory)

16
Sharing
  • A location environment captures sharing by
    mapping multiple variable to the same location
  • New locations must be generated when new data is
    allocated
  • In the purely functional world, this last step
    requires explicitly passing a stream

17
Sharing
  • The store and location environment must be passed
    to MEval
  • The store and location environment must be passed
    to all subfunctions that refer to the environment
Write a Comment
User Comments (0)
About PowerShow.com