Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.

Slides:



Advertisements
Similar presentations
Tom Schrijvers K.U.Leuven, Belgium with Manuel Chakravarty, Martin Sulzmann and Simon Peyton Jones.
Advertisements

Reconciling OO and Haskell-Style Overloading Mark Shields Simon Peyton Jones Microsoft Research Cambridge
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Typed Compilation of Recursive Datatypes Joseph C. Vanderwaart, Derek Dreyer, Leaf Petersen, Karl Crary, Robert Harper, and Perry Cheng Carnegie Mellon.
System F with type equality coercions Simon Peyton Jones (Microsoft) Manuel Chakravarty (University of New South Wales) Martin Sulzmann (University of.
Type Checking.
Principal Type Schemes for Modular Programs Derek Dreyer and Matthias Blume Toyota Technological Institute at Chicago ESOP 2007 Braga, Portugal.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Generative type abstraction and type-level computation (Wrestling with System FC) Stephanie Weirich, Steve Zdancewic University of Pennsylvania Dimitrios.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Semantics for MinML COS 441 Princeton University Fall 2004.
Chapter 10 Classes Continued
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Abstract Data Types and Encapsulation Concepts
Computer Science I Inheritance Professor Evan Korth New York University.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
Generic Programming with Dependent Types Stephanie Weirich University of Pennsylvania.
Type Classes with Functional Dependencies Mark P Jones, Oregon Graduate Institute The theory of relational databases meets.
Programming Languages and Paradigms Object-Oriented Programming.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Advanced Functional Programming Tim Sheard 1 Lecture 1 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Evolving the ML Module System Derek Dreyer Toyota Technological Institute at Chicago April 15, 2004.
© 2011 Pearson Education 1 Chapter 13 (Online): Object-Oriented Databases Modern Database Management 10 th Edition, International Edition Jeffrey A. Hoffer,
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
OO as a language for acm l OO phrase l Mental model of key concepts.
Object Oriented Software Development
Object-Oriented Data Modeling
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Do-it-yourself dictionaries in Haskell1 Ingmar Brouns & Lukas Spee.
A Type System for Higher-Order Modules Derek Dreyer, Karl Crary, and Robert Harper Carnegie Mellon University POPL 2003.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Copyright 2005, The Ohio State University CSE – Introduction to C++ Name: Shirish Tatikonda Time: T 3:30 PM Room: BE 394
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Module 9. Dealing with Generalization Course: Refactoring.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Functional Programming
Type Checking and Type Inference
Object-Oriented Programming
Road Map Inheritance Class hierarchy Overriding methods Constructors
UML Class Diagrams: Basic Concepts
CSC 143 Inheritance.
Learning Objectives Classes Constructors Principles of OOP
Records and Type Classes
AP Computer Science DYRT Quiz
Advanced Functional Programming
Records and Type Classes
Presentation transcript:

Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France

Specifications Haskell:

Specifications ML:

Implementations Haskell:

Implementations ML:

Generic Implementations Haskell:

Generic Implementations Haskell: ML:

Type Classes vs. Modules Haskell type classes (Wadler and Blott, Jones, et al.): +Elegant account of ad-hoc polymorphism –May want different instances of integer equality to be canonical in different scopes –Not a substitute for a module system ML modules (MacQueen, Harper, Leroy, et al.): +Explicit, fine-grained control over linking and abstraction –No support for implicit program construction

Our Proposal Start with ML modules: –Classes are just signatures of a special form –Instances are just structures and functors of a special form –Allow programmers to designate certain instance modules as canonical within a particular scope No tradeoff necessary – Get the best of both worlds! –Have access to full-featured module system by default –Can hook into the type inference engine and make use of Haskell-style overloading, if/when you want to

Classes and Instances in ML Great, but now how do we create the eq function?

Creating an Overloaded Function We employ an overload mechanism: This creates a “polymorphic value” eq, represented internally (in the semantics) as an implicit functor: Analogous to Haskell’s qualified types:

Using eq If we apply eq to some arguments of type int * int : This initiates a demand for a canonical module of signature But none exists ) Type error!

Making an Instance Canonical Designate EqInt and EqProd as canonical in a certain scope:

Making an Instance Canonical Now if we apply eq in that scope: Then the above code typechecks and translates internally to: Similar to evidence translation in Haskell: –Here we use modules as evidence

Class Hierarchies Haskell subclass declaration: How to support class hierarchies using modules?

Class Hierarchies Haskell subclass declaration: How to support class hierarchies using modules? Use module hierarchies!

Composite Class Signatures Instances of ORD are automatically computed from instances of EQ and instances of LT

Associated Types Proposed by Chakravarty et al. (2005) as an alternative to “functional dependencies” –Basic idea: Add type components to classes Falls out naturally from modular framework –Associated types are just type components of class signatures other than the one called t In Haskell, requires the introduction of “equality constraints” –In ML, these are just where type constraints

What Else is in the Paper Other design points: –Coercions between implicit and explicit functors –Coherence in the presence of explicitly-scoped instances –Handling of overlapping instances Type-theoretic formalization (details in the tech report): –Declarative elaboration semantics (a la Harper-Stone) –Type inference algorithm –Algorithm is sound, but not complete (due to problems inherited from both Haskell and ML) Related work

Thank You!

Extra Slides

Associated Types (Chakravarty et al. 05) A function sumColl that sums the elements of a collection would have polymorphic type:

Haskell Approach Beef up type classes: –Multi-parameter classes (Jones, Peyton Jones, et al.) –Constructor classes (Jones 95) –Functional dependencies (Jones 00) –Associated types (Chakravarty et al. 05) –…and more Each extension brings classes closer to modules, but still no explicit control over linking –Only attempt at explicit control is “named instances” (Kahl and Scheffczyk 01)

Instance of Ord at Int Haskell:

Instance of Ord at Int Haskell: ML:

Instance of Ord at £ Haskell:

Instance of Ord at £ Haskell: ML:

Instance of Ord at £ Haskell: ML:

Computing a Composite Instance Assuming LtInt and LtProd have been “used”, the canonical module of signature is

Another Example