Towards Interface Types for Haskell



Similar documents
Practical Generic Programming with OCaml

Adding GADTs to OCaml the direct approach

Lecture 12: Abstract data types

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

CSCE 314 Programming Languages

Introducing Variance into the Java Programming Language DRAFT

Part I. Multiple Choice Questions (2 points each):

Java (12 Weeks) Introduction to Java Programming Language

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Chapter 1 Fundamentals of Java Programming

Java SE 7 Programming

Busy Java Developer's Guide to Functional Programming. Ted Neward Neward & Associates

Class 16: Function Parameters and Polymorphism

>

Konzepte objektorientierter Programmierung

Abstract Class & Java Interface

Java Programming Language

AP Computer Science Java Subset

Translating Generalized Algebraic Data Types to System F

Web Development in Java

Course MS10975A Introduction to Programming. Length: 5 Days

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Moving from CS 61A Scheme to CS 61B Java

Programming Languages Featherweight Java David Walker

Gluing things together with Haskell. Neil Mitchell

Chapter 15 Functional Programming Languages

Topic VII. Data abstraction and modularity SML Modules a. References: Chapter 7 of ML for the working programmer (2ND

Translating a Subset of OCaml into System F. Jonatha Protzenk under the dire ion of François Po ier

Lecture 1: Introduction

Scala type classes and machine learning. David Andrzejewski Bay Area Scala Enthusiasts - 1/14/2013, 1/16/2013

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Programming and Software Development CTAG Alignments

History OOP languages Year Language 1967 Simula Smalltalk

Taming Wildcards in Java s Type System

Inheritance, overloading and overriding

Introduction to Object-Oriented Programming

Semester Review. CSC 301, Fall 2015

MLS module Sealing and Functor Design

N3458: Simple Database Integration in C++11

COMPARISON OF OBJECT-ORIENTED AND PROCEDURE-BASED COMPUTER LANGUAGES: CASE STUDY OF C++ PROGRAMMING

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Java Application Developer Certificate Program Competencies

The Needle Programming Language

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

COURSE OUTLINE. Prerequisites: Course Description:

Programming Languages CIS 443

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Real SQL Programming 1

Chapter 7: Functional Programming Languages

1.1 WHAT IS A PROGRAMMING LANGUAGE?

Functional Programming in C++11

First Class Overloading via Insersection Type Parameters

Java SE 7 Programming

A Generic Type-and-Effect System

Java SE 7 Programming

Aspect-Oriented Programming with Type Classes

OutsideIn(X) Modular type inference with local assumptions

CS 111 Classes I 1. Software Organization View to this point:

Testing Tools Content (Manual with Selenium) Levels of Testing

AURA: A language with authorization and audit

ios Dev Crib Sheet In the Shadow of C

Concepts and terminology in the Simula Programming Language

Contents. 9-1 Copyright (c) N. Afshartous

OUR COURSES 19 November All prices are per person in Swedish Krona. Solid Beans AB Kungsgatan Göteborg Sweden

DIPLOMADO DE JAVA - OCA

Using Files as Input/Output in Java 5.0 Applications

Annotated bibliography for the tutorial on Exploring typed language design in Haskell

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Visual Basic Programming. An Introduction

Programming Languages

Specialized Programme on Web Application Development using Open Source Tools

Unboxed Polymorphic Objects for Functional Numerical Programming

Fundamentals of Java Programming

The Interface Concept

Jedd: A BDD-based Relational Extension of Java

Object-Oriented Databases

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Formal Engineering for Industrial Software Development

Coeffects: Unified static analysis of context-dependence

CSC Software II: Principles of Programming Languages

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

<is web> Information Systems & Semantic Web University of Koblenz Landau, Germany

Object systems available in R. Why use classes? Information hiding. Statistics 771. R Object Systems Managing R Projects Creating R Packages

Lecture Data Types and Types of a Language

The CLIPS environment. The CLIPS programming language. CLIPS production rules language - facts. Notes

Java SE 8 Programming

StaRVOOrS: A Tool for Combined Static and Runtime Verification of Java

Transcription:

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

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

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

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

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 ()

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 ()

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 ()

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?

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?

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?

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

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

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

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...

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

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

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

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

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

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

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)

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...

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...

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

Subtyping (S-refl) t t (S-trans) t 1 t 2 t 2 t 3 t 1 t 3 (S-subclass) I C J I J (S-instance) m I J m J (S-tycon) s t T s T t (S-fun) t 1 s 1 s 2 t 2 s 1 s 2 t 1 t 2 (S-qual) s t a.q s a.q t

Restricted Subtyping t t t 1 t 2 t 2 t 3 t 1 t 3 s t T s T t t 1 s 1 s 2 t 2 s 1 s 2 t 1 t 2 Restricted subtyping vs generic instance Lemma If s t and s s and t t then true s t.

Restricted Subtyping t t t 1 t 2 t 2 t 3 t 1 t 3 s t T s T t t 1 s 1 s 2 t 2 s 1 s 2 t 1 t 2 Restricted subtyping vs generic instance Lemma If s t and s s and t t then true s t.

Translation of Types a /a t i C i /t i T t mapt (λx.c i [x]) /T t t 1 π 1 t 1 t 2 C 2 /t 2 t 1 t 2 λx.c 2 [ x]/π 1 (t 1 t 2 ) I K I /W I t C /t a.p t C / a.p t a a t π t T t π T t t 1 π 1 t 1 t 2 π 2 t 2 t 1 t 2 π 2 π 1 (t 1 t 2 ) I c.i c c t π t a.q t π a.q t

Translation of Terms x x e e e e s s λx.e λx.e λ(x :: s).e λ(x :: s ).e e e s c.q s s C /s λ(x :: s).e Λc(Q).λ(y :: s ).(λ(x :: s ).e ) (C [y]) f f e e f e f e e e f f let x = e in f let x = e in f e e s C /s (e :: s) (C [e ] :: s )

Results Let P Γ e : s be the typing judgment for Haskell with higher-rank qualified polymorphism. If P Γ e : s, s s, Γ Γ, and e e, then P Γ e : s.

Conclusions Type translation maps subtyping to generic instantiation Term translation is typing preserving Both are purely syntactic Q: Is the term translation meaning preserving? Q: Is the translated term amenable to type inference? Q: Can we do direct inference and translation to F2? If Java interface types make sense for Haskell, then how about type classes for Java? JavaGI @ECOOP 07

Conclusions Type translation maps subtyping to generic instantiation Term translation is typing preserving Both are purely syntactic Q: Is the term translation meaning preserving? Q: Is the translated term amenable to type inference? Q: Can we do direct inference and translation to F2? If Java interface types make sense for Haskell, then how about type classes for Java? JavaGI @ECOOP 07

Digression: The ML way 1 signature CONNECTION = 2 sig type connection 3 val exec : connection > string > queryresult 4 end 5 6 signature BETTERCONNECTION = 7 sig type connection 8 val exec : connection > string > queryresult 9 val notify : connection > string > unit 10 end 11 12 structure PostgreSQL : BETTERCONNECTION = 13 struct type connection = postgresqlconnection 14 val exec =... 15 val notify =... 16 end Encapsulation and Extensibility: BETTERCONNECTION <: CONNECTION But: application code as a functor taking a connection.