substructural typestates
play

Substructural Typestates Filipe Milito (CMU & UNL) Jonathan - PowerPoint PPT Presentation

Programming Languages meets Program Verification 2014 Substructural Typestates Filipe Milito (CMU & UNL) Jonathan Aldrich (CMU) Lus Caires (UNL) Motivation File file = new File( out.txt ); file


  1. Programming Languages meets Program Verification 2014 Substructural Typestates Filipe Militão (CMU & UNL) Jonathan Aldrich (CMU) Luís Caires (UNL)

  2. Motivation � File file = new File( “out.txt” ); � file .write( “stuff” ); � file .close(); � file .write( “more stuff” ); Note: consider a simplified File object, similar to Java’s FileOutputStream . 2

  3. Motivation � File file = new File( “out.txt” ); � file .write( “stuff” ); � file .close(); � file .write( “more stuff” ); FAILS with runtime exception (“invalid file descriptor”) 3

  4. Motivation class File { FileDescriptor fd ; File( string filename ){ fd = OS.createFile( filename ); } void write( string s ){ if ( fd == null ) throw Exception( “invalid file descriptor” ); fd .write( s ); } void close(){ fd = null ; } } 4

  5. Motivation class File { FileDescriptor fd ; File( string filename ){ fd = OS.createFile( filename ); } File void write( string s ){ File if ( fd == null ) throw Exception( “invalid file descriptor” ); fd .write( s ); File } The File type abstraction does not precisely File void close(){ express the changing properties of fd = null ; File ’s internal state ( fd ). File } } 4

  6. Motivation class File { FileDescriptor fd ; File( string filename ){ fd = OS.createFile( filename ); } Open void write( string s ){ Open if ( fd == null ) throw Exception( “invalid file descriptor” ); fd .write( s ); Open } Superfluous if statically ensured to Open void close(){ only be used when File is open. fd = null ; Closed } } 5

  7. Motivation class File { FileDescriptor fd ; File( string filename ){ fd = OS.createFile( filename ); } Open void write( string s ){ Open if ( fd == null ) throw Exception( “invalid file descriptor” ); fd .write( s ); Open } Superfluous if statically ensured to Open void close(){ only be used when File is open. fd = null ; Closed } } Open and Close are typestates . 5

  8. Contributions 1. Reconstruct typestate features from standard type-theoretic programming language primitives. We focus on the following set of typestate features: a) state abstraction, hiding an object representation while expressing the type of the state; b) state “dimensions”, enabling multiple orthogonal typestates over the same object; c) “dynamic state tests”, allowing a case analysis over the abstract state. 2. We show how to idiomatically support both state-based ( typestate ) and transition-based ( behavioral types ) specifications of abstract state evolution. 6

  9. Language • Polymorphic λ -calculus with mutable references (and immutable records, tagged sums, ...). • Technically, we use a variant of L 3 adapted for usability (by simplifying the handling of capabilities, adding support for sum types, universal/existential type quantification, alternatives, labeled records, ...). Ahmed, Fluet, and Morrisett. L 3 : A linear language with locations . Fundam. Inform. 2007. 7

  10. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. 8

  11. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. ref A 8

  12. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. type of contents of cell ref A 8

  13. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. type of contents of cell becomes ref A ref p rw p A 8

  14. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. type of contents of cell becomes ref A ref p rw p A location p links ref to r ead+ w rite capability that tracks the contents of that cell 8

  15. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. type of contents of cell p type of contents of cell (linear - cannot be duplicated) becomes ref A ref p rw p A location p links ref to r ead+ w rite capability that tracks the contents of that cell 8

  16. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. type of contents of cell p can be freely copied (pure) type of contents of cell (linear - cannot be duplicated) becomes ref A ref p rw p A location p links ref to r ead+ w rite capability that tracks the contents of that cell 8

  17. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. 9

  18. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. y : ref p x : ref p z : ref q 9

  19. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. rw q B y : ref p x : ref p z : ref q rw p A 9

  20. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. rw q B y : ref p x : ref p z : ref q rw p A ! x (“de-reference x”) 9

  21. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. rw q B y : ref p x : ref p z : ref q rw p A ! x (“de-reference x”) 9

  22. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. rw q B y : ref p x : ref p z : ref q rw p A ! x (“de-reference x”) 9

  23. Language • Mutable state handled as a linear resource: - split in pure references and linear capabilities. - use location-dependent types to link both. rw q B y : ref p x : ref p z : ref q rw p A A ! x (“de-reference x”) 9

  24. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: 10

  25. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: Lexical Typing Environment 10

  26. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: Lexical Typing Environment Initial Linear Typing Environment 10

  27. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: Lexical Typing Environment Type of Expression Initial Linear Typing Environment 10

  28. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: Resulting Effects Lexical Typing Environment Type of Expression Initial Linear Typing Environment 10

  29. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: resources are either consumed 11

  30. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: resources are either consumed 11

  31. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: or, threaded through 12

  32. Language • Capabilities are (linear) typing artifacts (not values) that are threaded and stacked implicitly. • For that, we use a Type -and- Effect system. • Typing judgement format: or, threaded through 12

  33. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  34. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  35. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  36. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  37. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  38. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  39. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  40. Language • Capabilities can be stacked and unstacked on top of some type, allowing them to accompany that type. 13

  41. Types 14

  42. Syntax 15

Recommend


More recommend