POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful



Similar documents
Fun with Phantom Types

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Deterministic Discrete Modeling

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

Functional Programming

Moving from CS 61A Scheme to CS 61B Java

Lecture Data Types and Types of a Language

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

Aspect-Oriented Programming with Type Classes

The countdown problem

Types and Programming Languages

Lecture 11: Tail Recursion; Continuations

Types, Polymorphism, and Type Reconstruction

Java EE Web Development Course Program

Introduction to type systems

Dependent Types at Work

A tutorial on the universality and expressiveness of fold

WHAT ARE MATHEMATICAL PROOFS AND WHY THEY ARE IMPORTANT?

Programming Languages in Artificial Intelligence

The Lean Theorem Prover

Computing Concepts with Java Essentials

The Classes P and NP

OPERATIONAL TYPE THEORY by Adam Petcher Prepared under the direction of Professor Aaron Stump A thesis presented to the School of Engineering of

Android Application Development Course Program

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Pretty-big-step semantics

Observational Program Calculi and the Correctness of Translations

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

Object Oriented Software Design

Adding GADTs to OCaml the direct approach

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

Type Systems. Luca Cardelli. Microsoft Research

Chapter 7: Functional Programming Languages

Idris, a General Purpose Dependently Typed Programming Language: Design and Implementation

Object Oriented Software Design

[Refer Slide Time: 05:10]

CS510 Software Engineering

Chapter 15 Functional Programming Languages

Relations: their uses in programming and computational specifications

Type Classes with Functional Dependencies

Probability Using Dice

How To Write A Type System

VB.NET Programming Fundamentals

Special Directions for this Test

Advanced Functional Programming (9) Domain Specific Embedded Languages

Journal of Functional Programming, volume 3 number 4, Dynamics in ML

AURA: A language with authorization and audit

Simulation-Based Security with Inexhaustible Interactive Turing Machines

ML for the Working Programmer

discuss how to describe points, lines and planes in 3 space.

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

COMPUTER SCIENCE TRIPOS

Java SE 8 Programming

3. Mathematical Induction

Andrew Pitts chapter for D. Sangorgi and J. Rutten (eds), Advanced Topics in Bisimulation and Coinduction, Cambridge Tracts in Theoretical Computer

How To Program In Scheme (Prolog)

A Propositional Dynamic Logic for CCS Programs

Mathematical Induction

Chapter 1: Key Concepts of Programming and Software Engineering

Glossary of Object Oriented Terms

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Lecture 8 The Subjective Theory of Betting on Theories

CIS 500 Software Foundations Midterm I, Review Questions

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Math 319 Problem Set #3 Solution 21 February 2002

Mathematical Induction. Lecture 10-11

PROGRAM-ing Finger Trees in COQ

Informatica e Sistemi in Tempo Reale

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

You know from calculus that functions play a fundamental role in mathematics.

Teaching Statement Richard A. Eisenberg

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

History OOP languages Year Language 1967 Simula Smalltalk

SOLUTIONS TO EXERCISES FOR. MATHEMATICS 205A Part 3. Spaces with special properties

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Chapter 1 Fundamentals of Java Programming

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

PL / SQL Basics. Chapter 3

by Mario Fusco Monadic

Reading 13 : Finite State Automata and Regular Expressions

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

SOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University

parent ROADMAP MATHEMATICS SUPPORTING YOUR CHILD IN HIGH SCHOOL

First-Class Type Classes

Lecture Notes on Polynomials

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Using Files as Input/Output in Java 5.0 Applications

CS 6371: Advanced Programming Languages

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Java Application Developer Certificate Program Competencies

Transcription:

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful RALF HINZE Institute of Information and Computing Sciences Utrecht University Email: ralf@cs.uu.nl Homepage: http://www.cs.uu.nl/~ralf/ March, 2001 (Pick the slides at.../~ralf/talks.html#t23.)

A brief outline of the talk Polytypic programming is about making programming easier and more economical. Why? (2 7) How? A programmer s perspective (9 13) How? From an implementor s point of view (15 26) Prerequisites: we use the functional programming language Haskell 98 for the examples. 1

Recap : Haskell 98 Haskell 98 is a statically typed language. New types are introduced via data declarations. data List A = nil cons A (List A) Here, List is a type constructor, nil and cons are data constructors. Functions are defined via pattern matching. size :: A. List A Int size nil = 0 size (cons a as) = 1 + size as size is a polymorphic function. 2

Polymorphic type systems Many of us (most of us?) will probably agree that static type systems, especially, polymorphic type systems are a good thing. For reasons of Security Flexibility soundness results guarantee that well-typed programs cannot go wrong. polymorphism allows the definition of functions that behave uniformly over all types (such as size). This is an uninteresting talk for addicts of untyped languages. 3

However,...... even polymorphic type systems are sometimes less flexible than one would wish. For instance, it is not possible to define a polymorphic equality function. eq :: T. T T Bool A deep theorem, the parametricity theorem, implies that a function of this type must necessarily be constant roughly speaking, the two arguments cannot be inspected. 4

As a consequence, the programmer is forced to program a separate equality function for each type from scratch. Here is how one would define equality for lists. eqlist :: A. (A A Bool) (List A List A Bool) eqlist eqa nil nil = True eqlist eqa nil (cons a 2 as 2 ) = False eqlist eqa (cons a 1 as 1 ) nil = False eqlist eqa (cons a 1 as 1 ) (cons a 2 as 2 ) = eqa a 1 a 2 eqlist as 1 as 2 eqlist is actually an equality transformer. 5

Polytypic programming comes to rescue Polytypic programming (also known as generic programming, type parametric programming, shape polymorphism, structural polymorphism) addresses this problem. Basic idea: define the equality function by induction on the structure of types. Note that an instance of eq, such as eqlist, is typically defined by induction on the structure of values. 6

Polytypic programs are ubiquitous Equality and comparison functions. Reductions (such as size, sum, listify). Pretty printers (such as Haskell s show function). Parsers (such as Haskell s read function). Data conversion (Jansson and Jeuring, ESOP 99). Digital searching (Hinze, JFP). 7

A brief outline of the talk Why? (2 7) How? A programmer s perspective (9 13) How? From an implementor s point of view (15 26) 8

Polytypic definitions In order to define a polytypic value it suffices to give instances for the following three data types plus an instance for every primitive type. data 1 = () data A 1 + A 2 = inl A 1 inr A 2 data A 1 A 2 = (A 1, A 2 ) Here, 1 is the unit data type, + is a binary sum, and is a binary product (pairs). 9

Polytypic definitions equality For emphasis, the type argument of eq is enclosed in angle brackets. eq{ A } :: A A Bool eq{ 1 } () () = True eq{ Int } i 1 i 2 = eqint i 1 i 2 eq{ A + B } (inl a 1 ) (inl a 2 ) = eq{ A } a 1 a 2 eq{ A + B } (inl a 1 ) (inr b 2 ) = False eq{ A + B } (inr b 1 ) (inl a 2 ) = False eq{ A + B } (inr b 1 ) (inr b 2 ) = eq{ B } b 1 b 2 eq{ A B } (a 1, b 1 ) (a 2, b 2 ) = eq{ A } a 1 a 2 eq{ B } b 1 b 2 This simple definition contains all ingredients needed to derive specializations for arbitrary data types. 10

Haskell s type classes Haskell supports overloading, based on type classes. class Eq T where eq :: T T Bool However, overloading is not polytypic programming. For each type, you have to give an explicit instance declaration, containing the code for equality. instance (Eq A) Eq (List A) where eq nil nil = True eq nil (cons a 2 as 2 ) = False eq (cons a 1 as 1 ) nil = False eq (cons a 1 as 1 ) (cons a 2 as 2 ) = eq a 1 a 2 eq as 1 as 2 11

deriving However, for a handful of built-in classes Haskell provides special support the so-called deriving mechanism. data List A = nil cons A (List A) deriving (Eq) The deriving (Eq) part tells Haskell to generate the obvious code for equality. Alas, you cannot define your own derivable type classes. 12

Derivable type classes Idea: use polytypic definitions to specify default method declarations. class Eq T where eq :: T T Bool eq{ 1 } () () = True eq{ A + B } (inl a 1 ) (inl a 2 ) = eq a 1 a 2 eq{ A + B } (inl a 1 ) (inr b 2 ) = False eq{ A + B } (inr b 1 ) (inl a 2 ) = False eq{ A + B } (inr b 1 ) (inr b 2 ) = eq b 1 b 2 eq{ A B } (a 1, b 1 ) (a 2, b 2 ) = eq a 1 a 2 eq b 1 b 2 This extension is implemented in the Glasgow Haskell Compiler (Version 5.00). 13

A brief outline of the talk Why? (2 7) How? A programmer s perspective (9 13) How? From an implementor s point of view (15 26) 14

A closer look at Haskell s data construct It features type abstraction. data List A = nil cons A (List A) It features type application. data List A = nil cons A (List A) It features type recursion. data List A = nil cons A (List A) The type language corresponds to the simply typed λ-calculus. 15

Kinds and types Kinds can be seen as the types of types. T, U Kind ::= kind of types T U function kind Types. T, U Type ::= C type constant A type variable ΛA. T type abstraction T U type application 16

Modelling data types Assuming the following type constants 1 :: (+) :: ( ) :: Fix :: (( ) ( )) ( ), we can rewrite List as a lambda term: List = Fix (ΛL. ΛA. 1 + A L A). 17

Specializing: unfolding... We could specialize a polytypic value by unfolding its definition: eq{ List Int } = eq{ 1 + Int List Int } = eq + eq 1 (eq eq Int (eq{ List Int })), where eq 1 () () = True eq + eq A eq B (inl a 1 ) (inl a 2 ) = eq A a 1 a 2 eq + eq A eq B (inl a 1 ) (inr b 2 ) = False eq + eq A eq B (inr b 1 ) (inl a 2 ) = False eq + eq A eq B (inr b 1 ) (inr b 2 ) = eq B b 1 b 2 eq eq A eq B (a 1, b 1 ) (a 2, b 2 ) = eq A a 1 a 2 eq B b 1 b 2. 18

... does not work Consider the following data type. data Power A = zero A succ (Power (A A)) Since the type recursion is nested, unfolding eq s definition will loop. eq{ Power Int } = eq{ Int + Power (Int Int) } = eq + eq Int (eq{ Power (Int Int) }) = eq + eq Int (eq + (eq eq Int eq Int ) (eq{ Power ((Int Int) (Int Int)) })) =... 19

Specializing polytypic values Basic idea: Let function follow type. eq{ List Int } = eqlist eqint eq{ Power Int } = eqpower eqint Since List and Power are functions on types, eqlist and eqpower are consequently functions on equality functions: eqlist maps eq{ A } to eq{ List A }. 20

Specializing generic values continued In general, the type of eq{ T :: T } is given by eq{ T :: T } :: Equal{ T } T, where Equal{ T } is defined by induction on the structure of kinds. Equal{ } T = T T Bool Equal{ A B } T = A. Equal{ A } A Equal{ B } (T A) eqlist has type Equal{ } List. 21

Specializing generic values continued The specialization of eq to types of arbitrary kinds is given by eq{ A } = eq A eq{ C } = eq c eq{ T 1 T 2 } = (eq{ T 1 }) T 2 (eq{ T 2 }) eq{ ΛA. T } = λa. λeq A. eq{ T }. Type application is mapped to value application, type abstraction to value abstraction, and type recursion to value recursion (eq Fix = fix). This is very similar to an interpretation of the simply typed λ-calculus. 22

Recap: applicative structures An applicative structure E is a triple (E, app, const) such that E = (E T T Kind), app = (app T,U : E T U (E T E U ) T, U Kind), and const : const E with const(c :: T) E T. 23

Recap: environment models An applicative structure E = (E, app, const) is an environment model if it is extensional (app is one-to-one) and if the clauses below define a total meaning function. E C :: T η = const(c ) E A :: T η = η(a) E (ΛA. T ) :: (S T) η = the unique ϕ E S T such that app S,T ϕ δ = E T :: T η(a := δ) E (T U ) :: V η = app U,V (E T :: U V η) (E U :: U η) 24

Specialization as an interpretation The applicative structure E = (E, app, const) with E T = (T :: T; Equal{ T } T ) app T,U (F ; f ) (A; a) = (F A; f A a) const(c ) = (C ; eq C ) is an environment model. Here, (T :: T; F T ) denotes a dependent product. 25

Benefits Since the definition of app and the interpretation of Fix are the same for all polytypic functions, the polytypic programmer merely has to specify the interpretation of the remaining constants: 1, +,, Int, and so on. We can use the basic proof method of the simply typed λ- calculus, which is based on so-called logical relations to prove properties of polytypic values. 26

A brief outline of the talk Why? (2 7) How? A programmer s perspective (9 13) How? From an implementor s point of view (15 26) 27