mostly functional behavior in java programs
play

Mostly-functional behavior in Java programs William C. Benton Red - PowerPoint PPT Presentation

Mostly-functional behavior in Java programs William C. Benton Red Hat Emerging Technologies and University of Wisconsin Charles N. Fischer University of Wisconsin Motivation Wed like to do aggressive code transformations, specification


  1. Mostly-functional behavior in Java programs William C. Benton Red Hat Emerging Technologies and University of Wisconsin Charles N. Fischer University of Wisconsin

  2. Motivation We’d like to do aggressive code transformations, specification checking and analysis of large object-oriented programs. 2

  3. The problem Java programs are difficult to analyze, transform, and reason about (in part) due to mutable state. 3

  4. Our contribution Type-and-effect system and type-based analysis 4

  5. Our contribution Type-and-effect system and type-based analysis Initialization effects 4

  6. Our contribution Type-and-effect system and type-based analysis Initialization Quiescing field effects inference 4

  7. Our contribution Type-and-effect system and type-based analysis Initialization Quiescing field Degrees of effects inference method purity 4

  8. Our contribution Type-and-effect system and type-based analysis Initialization Quiescing field Degrees of effects inference method purity 4

  9. Our contribution Type-and-effect system and type-based analysis Initialization Quiescing field Degrees of effects inference method purity Surprising result: substantial mostly- functional behavior in Jav a! 4

  10. Forecast • A simple object-oriented effects system • Initializers and initialization effects • Final fields and eventual immutability • Inferring quiescing fields • Evaluating quiescing field inference 5

  11. Effect systems: motivation // method1: List<T> → int int method1(List<T> l ) { return l .size(); } // method2: List<T> → int int method2(List<T> l ) { int i = 0; while (! l. isEmpty() ) { l .remove(0); i++; } return i ; } 6

  12. Effect systems: motivation // method1: List<T> → int READS state of l int method1(List<T> l ) { return l .size(); } // method2: List<T> → int int method2(List<T> l ) { int i = 0; while (! l. isEmpty() ) { l .remove(0); i++; } return i ; } 6

  13. Effect systems: motivation // method1: List<T> → int READS state of l int method1(List<T> l ) { READS , WRITES state of l return l .size(); } // method2: List<T> → int int method2(List<T> l ) { int i = 0; while (! l. isEmpty() ) { l .remove(0); i++; } return i ; } 6

  14. Effect systems: motivation // method1: List<T> → int READS state of l int method1(List<T> l ) { READS , WRITES state of l return l .size(); } // method2: List<T> → int int method2(List<T> l ) { Type systems: int i = 0; “what?” while (! l. isEmpty() ) { l .remove(0); i++; Effect systems: } “how?” return i ; } 6

  15. Effect systems: motivation // method1: List<T> → int READS state of l int method1(List<T> l ) { READS , WRITES state of l return l .size(); } // method2: List<T> → int int method2(List<T> l ) { Type systems: int i = 0; “How” consists of an “what?” while (! l. isEmpty() ) { effect ( READ or WRITE ) l .remove(0); i++; in some region . Effect systems: } “how?” return i ; } 6

  16. Object-oriented effect systems • Classic effect systems typically feature lexically-scoped regions • Object-oriented effect systems better support classes, fields, &c. • See Greenhouse & Boyland ( ECOOP 99) or Bierman & Parkinson ( WOOD 03) 7

  17. Inferring effects for bytecodes   l = l h . ν l h . ν = l load rpt store rpt     method invocations pmap 8

  18. Inferring effects for bytecodes   l = l h . ν l h . ν = l load rpt store rpt     method invocations pmap 8

  19. Inferring effects for bytecodes   l h . ν = l load rpt store rpt     method invocations pmap 8

  20. Inferring effects for bytecodes   l h . ν = l load rpt store rpt     method invocations pmap Regions we consider: ρ this , ρ 0.. n , and ⊤ . 8

  21. Inferring effects for bytecodes   l h . ν = l load rpt store rpt     method invocations pmap Regions we consider: ρ this , ρ 0.. n , and ⊤ . 8

  22. Inferring effects for bytecodes   load rpt store rpt     method invocations pmap Regions we consider: ρ this , ρ 0.. n , and ⊤ . 8

  23. Inferring effects for bytecodes   load rpt store rpt     method invocations pmap Regions we consider: ρ this , ρ 0.. n , and ⊤ . 8

  24. Inferring effects for bytecodes   load rpt store rpt     pmap Regions we consider: ρ this , ρ 0.. n , and ⊤ . 8

  25. Extending the simple system Type-and-effect system and type-based analysis Initialization Quiescing field Degrees of effects inference method purity 9

  26. Extending the simple system Type-and-effect system and type-based analysis Initialization Quiescing field Degrees of effects inference method purity 9

  27. Extending the simple system Type-and-effect system and type-based analysis Initialization Quiescing field Degrees of effects inference method purity 9

  28. Forecast • A simple object-oriented effects system • Initializers and initialization effects • Final fields and eventual immutability • Inferring quiescing fields • Evaluating quiescing field inference 10

  29. Motivating initialization effects class IntBox { private int i ; IntBox( int j ) { this .i = j ; } int get() { return i ; } } 11

  30. Motivating initialization effects class IntBox { private int i ; IntBox( int j ) { writes IntBox.i to this this .i = j ; } int get() { return i ; reads IntBox.i from this } } 11

  31. Motivating initialization effects class IntBox { private int i ; IntBox( int j ) { writes IntBox.i to this this .i = j ; } These two effects cannot interfer e! int get() { return i ; reads IntBox.i from this } } 11

  32. Motivating initialization effects class IntBox { private int i ; IntBox( int j ) { initializes IntBox.i of this this .i = j ; } These two e ff ects cannot interfere! int get() { return i ; reads IntBox.i from this } } 11

  33. Motivating initialization effects class IntBox { IntBox factory( int j ) { private int i ; IntBox r = IntBox( int j ) { new IntBox( j ); this .i = j ; return r; } } int get() { return i ; } } 12

  34. Motivating initialization effects class IntBox { IntBox factory( int j ) { private int i ; IntBox r = IntBox( int j ) { new IntBox( j ); this .i = j ; return r; } } int get() { “Pure” methods can modify newly-allocated return i ; objects (Leavens et al.; } Rugina and Cherem) } 12

  35. Defining initialization effects class IntBox { private int i ; IntBox( int j ) { this .i = j ; initializes IntBox.i field of this } int get() { return i ; } } 13

  36. Defining initialization effects class IntBox { An initialization e ff ect is a private int i ; WRITE to the state of an object during its creation. IntBox( int j ) { this .i = j ; An initializer is a method } that executes on an object during the dynamic lifetime int get() { of its constructor. return i ; } } 13

  37. Inferring initializer methods 14

  38. Inferring initializer methods 14

  39. Inferring initializer methods this this this this 14

  40. Inferring initializer methods A constructor is an initializer on its receiver object. this this this this 14

  41. Inferring initializer methods A constructor is an TWO A constructor is an initializer on its initializer on its receiver object. receiver object. this A method that is only this this this invoked via this-edges from an initializer is also an initializer on its receiver object. 14

  42. Inferring initialization effects Initialization effects are writes to fields of this that occur within an initializer. 15

  43. Forecast • A simple object-oriented effects system • Initializers and initialization effects • Final fields and eventual immutability • Inferring quiescing fields • Evaluating quiescing field inference 16

  44. Final fields class IntBox { private int i ; IntBox( int j ) { this .i = j ; } int get() { return i ; } } 17

  45. Final fields class IntBox { private final int i ; IntBox( int j ) { this .i = j ; } int get() { return i ; } } 17

  46. Final fields class IntBox { private final int i ; i is a run-time constant IntBox( int j ) { this .i = j ; } int get() { return i ; } } 17

  47. Final fields class IntBox { private final int i ; IntBox( int j ) { this .i = j ; } int get() { return i ; } } 17

  48. Final fields class IntBox { private void init( int j ) { private final int i ; this . i = j ; IntBox( int j ) { } init( i ); } } int get() { return i ; } 17

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