towards interface types for haskell
play

Towards Interface Types for Haskell Work in Progress Peter Thiemann - PowerPoint PPT Presentation

Towards Interface Types for Haskell Work in Progress Peter Thiemann Joint work with Stefan Wehr Universit at Freiburg, Germany WG 2.8 Meeting, Nesjavellir, Island, 17.07.2007 What is a type class? A type class is a signature of an


  1. Towards Interface Types for Haskell Work in Progress Peter Thiemann Joint work with Stefan Wehr Universit¨ at Freiburg, Germany WG 2.8 Meeting, Nesjavellir, Island, 17.07.2007

  2. What is a type class? ◮ A type class is a signature of an abstract data type. ◮ But where is the abstract type?

  3. What is a type class? ◮ A type class is a signature of an abstract data type. ◮ But where is the abstract type?

  4. What is a type class? ◮ A type class is a signature of an abstract data type. ◮ But where is the abstract type?

  5. Example: HDBC interface ◮ Signature of abstract data type module HDBC where class Connection conn where exec :: conn − > String − > IO QueryResult ◮ Implementation of abstract data type module PostgreSQLDB where import HDBC instance Connection PostgreSQLConnection where exec = pgsqlExec ◮ Extending the abstract data type class Connection conn = > BetterConnection conn where notify :: conn − > String − > IO ()

  6. Example: HDBC interface ◮ Signature of abstract data type module HDBC where class Connection conn where exec :: conn − > String − > IO QueryResult ◮ Implementation of abstract data type module PostgreSQLDB where import HDBC instance Connection PostgreSQLConnection where exec = pgsqlExec ◮ Extending the abstract data type class Connection conn = > BetterConnection conn where notify :: conn − > String − > IO ()

  7. Example: HDBC interface ◮ Signature of abstract data type module HDBC where class Connection conn where exec :: conn − > String − > IO QueryResult ◮ Implementation of abstract data type module PostgreSQLDB where import HDBC instance Connection PostgreSQLConnection where exec = pgsqlExec ◮ Extending the abstract data type class Connection conn = > BetterConnection conn where notify :: conn − > String − > IO ()

  8. Why want an abstract type? ◮ Encapsulation What is a good type for connect ? ◮ Can do with connectWith :: URL − > ( forall c. Connection c = > c − > IO a) − > IO a ◮ but: requires user code in continuation ◮ no “connection value” that can be stored ◮ not possible as member of class Connection ◮ How about connect :: URL − > IO Connection where Connection behaves like a Java interface type?

  9. Why want an abstract type? ◮ Encapsulation What is a good type for connect ? ◮ Can do with connectWith :: URL − > ( forall c. Connection c = > c − > IO a) − > IO a ◮ but: requires user code in continuation ◮ no “connection value” that can be stored ◮ not possible as member of class Connection ◮ How about connect :: URL − > IO Connection where Connection behaves like a Java interface type?

  10. Why want an abstract type? ◮ Encapsulation What is a good type for connect ? ◮ Can do with connectWith :: URL − > ( forall c. Connection c = > c − > IO a) − > IO a ◮ but: requires user code in continuation ◮ no “connection value” that can be stored ◮ not possible as member of class Connection ◮ How about connect :: URL − > IO Connection where Connection behaves like a Java interface type?

  11. Interfaces for Haskell A Design Proposal ◮ Type class I ⇒ interface type I ◮ Type I is exists c. (I c) = > c ◮ Subtyping for interface types if I is a subclass of J , then I ≤ J ◮ Subtyping for instance types if t is an instance type of J , then t ≤ J ◮ Introduction by type annotation ⇒ no new syntax

  12. Interfaces for Haskell A Design Proposal ◮ Type class I ⇒ interface type I ◮ Type I is exists c. (I c) = > c ◮ Subtyping for interface types if I is a subclass of J , then I ≤ J ◮ Subtyping for instance types if t is an instance type of J , then t ≤ J ◮ Introduction by type annotation ⇒ no new syntax

  13. Interfaces for Haskell A Design Proposal ◮ Type class I ⇒ interface type I ◮ Type I is exists c. (I c) = > c ◮ Subtyping for interface types if I is a subclass of J , then I ≤ J ◮ Subtyping for instance types if t is an instance type of J , then t ≤ J ◮ Introduction by type annotation ⇒ no new syntax

  14. Example Patterns of Use ◮ Create a connection betterConnect :: URL − > IO BetterConnection betterConnect url = do c < − pgconnect url −− c :: PGSQLConnection return (c :: BetterConnection) ◮ Wrapper dbwrapper :: URL − > (URL − > IO Connection) − > IO Result dbwrapper url connect = do c < − connect url do something c ... dbwrapper url betterConnect ... ◮ Worker worker :: Connection − > IO Result withBetterConnection :: (BetterConnection − > IO a) − > IO a ... withBetterConnection worker ...

  15. Collecting the Pieces Surprise! ◮ Everything needed is (almost) there

  16. Collecting the Pieces Existential Types in Haskell data T Connection where T Connection :: forall conn. Connection conn = > conn − > T Connection data T BetterConnection where T BetterConnection :: forall conn. BetterConnection conn = > conn − > T BetterConnection instance T Connection Connection where ... instance T Connection BetterConnection where ... instance T BetterConnection BetterConnection where ... ◮ Tagged existentials ◮ Need pattern match to unpack

  17. Collecting the Pieces Subtyping in Haskell ◮ There is no subtyping in Haskell! ◮ But, there is the generic instance relation: forall c. BetterConnection c = > c − > T � forall c. Connection c = > c − > T ◮ And there is the double negation equivalence: exists a. P = > T = ( forall a. P = > T − > x) − > x ◮ Approach: Translate existential types to (higher-rank) polymorphism where possible

  18. Collecting the Pieces Subtyping in Haskell ◮ There is no subtyping in Haskell! ◮ But, there is the generic instance relation: forall c. BetterConnection c = > c − > T � forall c. Connection c = > c − > T ◮ And there is the double negation equivalence: exists a. P = > T = ( forall a. P = > T − > x) − > x ◮ Approach: Translate existential types to (higher-rank) polymorphism where possible

  19. Collecting the Pieces Subtyping in Haskell ◮ There is no subtyping in Haskell! ◮ But, there is the generic instance relation: forall c. BetterConnection c = > c − > T � forall c. Connection c = > c − > T ◮ And there is the double negation equivalence: exists a. P = > T = ( forall a. P = > T − > x) − > x ◮ Approach: Translate existential types to (higher-rank) polymorphism where possible

  20. Collecting the Pieces Subtyping in Haskell ◮ There is no subtyping in Haskell! ◮ But, there is the generic instance relation: forall c. BetterConnection c = > c − > T � forall c. Connection c = > c − > T ◮ And there is the double negation equivalence: exists a. P = > T = ( forall a. P = > T − > x) − > x ◮ Approach: Translate existential types to (higher-rank) polymorphism where possible

  21. Example Translation Create a Connection betterConnect :: URL − > IO BetterConnection betterConnect url = do c < − pgconnect url −− c :: PGSQLConnection return (c :: BetterConnection) translates to betterConnect’ :: URL − > IO T BetterConnection betterConnect’ url = do c < − pgconnect url return (T BetterConnection c)

  22. Example Translation Wrapper dbwrapper :: URL − > (URL − > IO Connection) − > IO Result dbwrapper url connect = do c < − connect url do something c ... dbwrapper url betterConnect ... translates to dbwrapper’ :: URL − > forall c. Connection c = > (URL − > IO c) − > IO Result dbwrapper’ url connect = do c < − connect url do something c betterConnect’ :: URL − > IO T BetterConnection ... dbwrapper’ url betterConnect’ ...

  23. Example Translation Worker worker :: Connection − > IO Result withBetterConnection :: (BetterConnection − > IO a) − > IO a ... withBetterConnection worker ... translates to worker’ :: forall c . Connection c = > c − > IO Result withBetterConnection’ :: ( forall c. BetterConnection c = > c − > IO a) − > IO a ... withBetterConnection’ worker’ ...

  24. Interfaces for Haskell Translational Approach ◮ Starting point: Haskell with higher-rank polymorphism (as in current implementations) ◮ Extensions: Extended syntax of types s , t ::= · · · | I Typing rules (E-ann’) P | Γ ⊢ ′ e : s s ≤ t P | Γ ⊢ ′ ( e :: t ) : t (E-sub’) P | Γ ⊢ ′ e : s s ≤ ′ t P | Γ ⊢ ′ e : t

  25. Subtyping (S-trans) t 1 ≤ t 2 t 2 ≤ t 3 (S-refl) t ≤ t t 1 ≤ t 3 (S-subclass) I ⇒ C J (S-instance) m ∈ I J I ≤ J m ≤ J s ≤ t t 1 ≤ s 1 s 2 ≤ t 2 (S-tycon) (S-fun) T s ≤ T t s 1 − → s 2 ≤ t 1 − → t 2 s ≤ t (S-qual) ∀ a . Q ⇒ s ≤ ∀ a . Q ⇒ t

  26. Restricted Subtyping t 1 ≤ ′ t 2 t 2 ≤ ′ t 3 s ≤ ′ t t ≤ ′ t t 1 ≤ ′ t 3 T s ≤ ′ T t s 2 ≤ ′ t 2 t 1 ≤ s 1 → s 2 ≤ ′ t 1 − s 1 − → t 2 Restricted subtyping vs generic instance Lemma If s ≤ ′ t and s ❀ ′ s ′ and t ❀ ′ t ′ then true ⊢ s ′ � t ′ .

  27. Restricted Subtyping t 1 ≤ ′ t 2 t 2 ≤ ′ t 3 s ≤ ′ t t ≤ ′ t t 1 ≤ ′ t 3 T s ≤ ′ T t s 2 ≤ ′ t 2 t 1 ≤ s 1 → s 2 ≤ ′ t 1 − s 1 − → t 2 Restricted subtyping vs generic instance Lemma If s ≤ ′ t and s ❀ ′ s ′ and t ❀ ′ t ′ then true ⊢ s ′ � t ′ .

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