objectives state
play

Objectives State Describe the property of referential transparency - PowerPoint PPT Presentation

-- well, mostly Objectives Equational Reasoning References Objectives Equational Reasoning References Objectives State Describe the property of referential transparency . Dr. Mattox Beckman Explain how stateful computations


  1. -- well, mostly Objectives Equational Reasoning References Objectives Equational Reasoning References Objectives State ◮ Describe the property of referential transparency . Dr. Mattox Beckman ◮ Explain how stateful computations complicate the meaning of programs. ◮ Use OCaml ’s references to model state. University of Illinois at Urbana-Champaign Department of Computer Science Objectives Equational Reasoning References Objectives Equational Reasoning References Defjnition The rule of referential transparency: ◮ You can use equational reasoning to make the following equivalence: e 1 → ∗ v e 2 → ∗ v f e 1 → ∗ w f ( if e 1 then e 2 else e 3 ) ≡ if e 1 then f ( e 2 ) else f ( e 3 ) f e 2 → ∗ w 1 x * ( if foo then 20 / x else 23 / x) -- equivalent to ◮ If you have two expressions that evaluate to be the same thing then you can use one for 2 if foo then 20 else 23 the other without changing the meaning of the whole program. ◮ You have the basis now of many compiler optimization opportunities! ◮ E.g. f(x) + f(x) == 2 * f(x) ◮ You can prove this by induction, using the natural semantic rules from the previous lectures.

  2. Objectives Equational Reasoning References Objectives Equational Reasoning References A Complication A Counterexample 1 # let counter = -- something 2 val counter : unit -> int = <fun> ◮ f(x) + f(x) == 2 * f(x) 3 # counter () ;; 1 # 2 * counter () ;; 4 - : int = 1 2 - : int = 8 5 # counter () ;; 3 # counter () + counter () ;; 6 - : int = 2 4 - : int = 11 7 # counter () ;; 8 - : int = 3 ◮ Congratulations. You just broke mathematics. 9 # ◮ Can we still use equational reasoning to talk about programs now? Objectives Equational Reasoning References Objectives Equational Reasoning References Reference Operator Natural Semantics e ⇓ v , where $ i is a free location in the state, initialized to v . ref e ⇓ $ i Transition Semantics ref v → $ i , where $ i is a free location in the state, initialized to v . e ⇓ $ i , if state location $ i contains v . ! $ i → v , if state location $ i contains v . ! e ⇓ v $ i := v → () , and state location $ i is assigned v . (); e → e e 1 ⇓ $ i e 2 ⇓ v , and location $ i is set to v . Note that references are different than pointers: once created, they cannot be moved, only e 1 := e 2 ⇓ () assigned to and read from. e 1 ⇓ () e 2 ⇓ v e 1 ; e 2 ⇓ v

  3. ! ct ;; ct := ! ct + 1 ; Objectives Equational Reasoning References Objectives Equational Reasoning References Counter, Method 1 Bad Things for Counter 1 # let ct = ref 0 ;; ct is globally defjned. Two bad things could occur because of this. 2 val ct : int ref = { contents = 0 } 1. What if you already had a global variable ct defjned? 3 # let counter () = ◮ Correct solution: use modules. 4 2. The Stupid User TM might decide to change ct just for fun. 5 ◮ Now your counter won’t work like it’s supposed to! 6 val counter : unit -> int = <fun> ◮ Now you can’t change the representation without getting tech support calls. 7 # counter () ;; ◮ Remember the idea of abstraction . 8 - : int = 1 9 # counter () ;; 10 - : int = 2 Objectives Equational Reasoning References Conclusions about State State is bad because: ◮ It breaks our ability to use equational reasoning. ◮ Users can get to our global variables and change them without permission. State is good because: ◮ Certain constructs are almost impossible without state (e.g., graphs). ◮ Our world is a stateful one.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend