09 the language impl
play

09The Language imPL CS 4215: Programming Language Implementation - PowerPoint PPT Presentation

imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL 09The Language imPL CS 4215: Programming Language Implementation Martin Henz March 16, 2012 Generated on Thursday 15


  1. imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL 09—The Language imPL CS 4215: Programming Language Implementation Martin Henz March 16, 2012 Generated on Thursday 15 March, 2012, 22:56 CS 4215: Programming Language Implementation 09—The Language imPL

  2. imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Introduction simPL, rePL: an identifier refers to a value once computed, the value does not change pass-by-need exploits this fact referential transparency good for formal reasoning CS 4215: Programming Language Implementation 09—The Language imPL

  3. imPL0 imPL1 Pass-by-reference, Pass-by-copy Imperative Programming and Exception Handling A Virtual Machine for imPL Motivation many algorithms are more natural when presented using random-access memory assigment allows to change the value stored in the memory location associated with an identifier imPL0 allows assignment imPL1 adds assignment to record properties many interesting variants... CS 4215: Programming Language Implementation 09—The Language imPL

  4. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL 1 imPL0 Syntax Examples Denotational Semantics 2 imPL1 3 Pass-by-reference, Pass-by-copy 4 Imperative Programming and Exception Handling 5 A Virtual Machine for imPL CS 4215: Programming Language Implementation 09—The Language imPL

  5. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Syntax of imPL0 E Assignment x := E E 1 E 2 Sequence E 1 ; E 2 E 1 E 2 Loop while E 1 do E 2 end CS 4215: Programming Language Implementation 09—The Language imPL

  6. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Example let x = 0 in x := 1; x := x + 2; x := x + 3; x end CS 4215: Programming Language Implementation 09—The Language imPL

  7. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Another Example fun x -> let i = 1 f = 1 in while \ i > x do f := f * i; i := i + 1 end; f end end CS 4215: Programming Language Implementation 09—The Language imPL

  8. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Yet Another Example let gcd = fun a b -> while \(a = b) do if a > b then a := a - b else b := b - a end end; a end in (gcd 6 10) end CS 4215: Programming Language Implementation 09—The Language imPL

  9. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Denotational Semantics: Then How? let x = 0 in x := 1; x := x + 2; x := x + 3; x end CS 4215: Programming Language Implementation 09—The Language imPL

  10. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Denotational Semantics: Idea identifiers refer to locations a store maps locations to values the store is passed to and returned from the semantic function CS 4215: Programming Language Implementation 09—The Language imPL

  11. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Semantic Domains Domain name Definition EV Int + Bool + Fun + { error } SV Int + Bool + Fun DV Loc Fun DV ∗ · · · ∗ DV ∗ Store � (EV, Store) Store Loc � SV Env Id � DV CS 4215: Programming Language Implementation 09—The Language imPL

  12. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Example Let us say we have a store with the value 1 at location l Σ = ∅ Store [ l ← 1] and an environment that carries location l at identifier x ∆ = ∅ Env [ x ← l ] Then we can access the value of x in the store as follows: Σ(∆( x )) = 1 CS 4215: Programming Language Implementation 09—The Language imPL

  13. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL The Main Semantic Function · ֌ · : imPL0 → EV ∅ Store | ∅ Env � E ֌ ( v , Σ) E ֌ v · | · � · ֌ · : Store ∗ Env ∗ imPL0 → EV ∗ Store CS 4215: Programming Language Implementation 09—The Language imPL

  14. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Let Expressions Σ ′ [ l 1 ← v 1 ] | ∆[ x 1 ← l 1 ] � E ֌ ( v , Σ ′′ ) Σ | ∆ � let x 1 = E 1 in E end ֌ ( v , Σ ′′ ) if Σ | ∆ � E 1 ֌ ( v 1 , Σ ′ ), and l 1 is a new location, which means Σ ′ ( l 1 ) is undefined CS 4215: Programming Language Implementation 09—The Language imPL

  15. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Identifiers Σ | ∆ � x ֌ (Σ(∆( x )) , Σ) CS 4215: Programming Language Implementation 09—The Language imPL

  16. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Assignment Σ | ∆ � E ֌ ( v , Σ ′ ) Σ | ∆ � x := E ֌ ( v , Σ ′ [∆( x ) ← v ]) CS 4215: Programming Language Implementation 09—The Language imPL

  17. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Example ∅ Store [ l ← 1] | ∅ Env [ a ← l ] � a := 2 ֌ (2 , ∅ Store [ l ← 1][ l ← 2]) The resulting store ∅ Store [ l ← 1][ l ← 2]) is of course the same as ∅ Store [ l ← 2]. The original binding of l to 1 is overwritten by the new value 2. CS 4215: Programming Language Implementation 09—The Language imPL

  18. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Function Definition where f ( l , Σ ′ ) = ( v ′ , Σ ′′ ), where Σ ′ | ∆[ x ← l ] � E ֌ ( v ′ , Σ ′′ ) Σ | ∆ � fun x -> E end ֌ ( f , Σ) CS 4215: Programming Language Implementation 09—The Language imPL

  19. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Function Application Σ ′ | ∆ � E 2 ֌ ( v 2 , Σ ′′ ) Σ | ∆ � E 1 ֌ ( f , Σ ′ ) Σ | ∆ � ( E 1 E 2 ) ֌ f ( l , Σ ′′ [ l ← v 2 ]) where l is a new location in Σ ′′ . CS 4215: Programming Language Implementation 09—The Language imPL

  20. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL Sequence Σ ′ | ∆ � E 2 ֌ ( v 2 , Σ ′′ ) Σ | ∆ � E 1 ֌ ( v 1 , Σ ′ ) Σ | ∆ � E 1 ; E 2 ֌ ( v 2 , Σ ′′ ) CS 4215: Programming Language Implementation 09—The Language imPL

  21. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL While Loop Σ | ∆ � E 1 ֌ ( v 1 , Σ ′ ) if v 1 = false Σ | ∆ � while E 1 do E 2 end ֌ ( true , Σ ′ ) CS 4215: Programming Language Implementation 09—The Language imPL

  22. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Examples Imperative Programming and Exception Handling Denotational Semantics A Virtual Machine for imPL While Loop Σ | ∆ � E 1 ֌ ( v 1 , Σ ′ ) Σ | ∆ � while E 1 do E 2 end ֌ ( v , Σ ′′′ ) if v 1 = true , where Σ ′ | ∆ � E 2 ֌ ( v 2 , Σ ′′ ) and Σ ′′ | ∆ � while E 1 do E 2 end ֌ ( v , Σ ′′′ ) CS 4215: Programming Language Implementation 09—The Language imPL

  23. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Denotational Semantics Imperative Programming and Exception Handling Example A Virtual Machine for imPL 1 imPL0 2 imPL1 Syntax Denotational Semantics Example 3 Pass-by-reference, Pass-by-copy 4 Imperative Programming and Exception Handling 5 A Virtual Machine for imPL CS 4215: Programming Language Implementation 09—The Language imPL

  24. imPL0 imPL1 Syntax Pass-by-reference, Pass-by-copy Denotational Semantics Imperative Programming and Exception Handling Example A Virtual Machine for imPL Record Property Assignment E 1 E 2 E 1 . q := E 2 CS 4215: Programming Language Implementation 09—The Language imPL

Recommend


More recommend