Indexed Programming. Jeremy Gibbons

Similar documents
IA014: Advanced Functional Programming

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

Overloading, Type Classes, and Algebraic Datatypes

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

Programming in Omega Part 1. Tim Sheard Portland State University

GADTs. GADTs in Haskell

Advances in Programming Languages

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

Shared Subtypes. Subtyping Recursive Parameterized Algebraic Data Types

Advanced features of Functional Programming (Haskell)

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

More Lambda Calculus and Intro to Type Systems

Simple Unification-based Type Inference for GADTs

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Generic polymorphism on steroids

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

At build time, not application time. Types serve as statically checkable invariants.

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

More Lambda Calculus and Intro to Type Systems

The list abstract data type defined a number of operations that all list-like objects ought to implement:

Simply-Typed Lambda Calculus

CS558 Programming Languages

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Inductive Data Types

CSCI-GA Scripting Languages

From FP to OOP. Start with data: class Posn { int x; int y; Posn(int x, int y) { this.x = x; this.y = y; } }

Type Checking and Type Inference

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Topic 7: Algebraic Data Types

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

CSC324 Principles of Programming Languages

CA Compiler Construction

CMSC 330: Organization of Programming Languages

Type families and data kinds

More Lambda Calculus and Intro to Type Systems

Typed Racket: Racket with Static Types

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Structural polymorphism in Generic Haskell

! Two questions when we design an OO system : ! Our Initial goal: learn to design and implement simple objects.

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CSE 431S Type Checking. Washington University Spring 2013

Type Systems, Type Inference, and Polymorphism

Harvard School of Engineering and Applied Sciences Computer Science 152

Programming in Omega Part 2. Tim Sheard Portland State University

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Programming with Universes, Generically

Motivation for typed languages

An introduction introduction to functional functional programming programming using usin Haskell

Topic 9: Type Checking

Topic 9: Type Checking

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Last class. -More on polymorphism -super -Introduction to interfaces

Intermediate Code Generation

The Typed Racket Guide

CSc 520. Principles of Programming Languages 25: Types Introduction

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

Adding GADTs to OCaml the direct approach

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

CS558 Programming Languages

CS558 Programming Languages

3. Java - Language Constructs I

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Polymorphism COSC346

CSE 12 Week Eight, Lecture One

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Subtyping. Lecture 13 CS 565 3/27/06

Generic programming POLYMORPHISM 10/25/13

ITI Introduction to Computing II

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Making New instances of Classes

Assignment 2 - Specifications and Modeling

Properties of an identifier (and the object it represents) may be set at

Typed Scheme: Scheme with Static Types

Core Competency DOO (Design Object-Oriented)

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Programming Languages

Haskell Overview III (3A) Young Won Lim 10/4/16

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

ITI Introduction to Computing II

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

CSCE 314 Programming Languages. Type System

CSC324 Principles of Programming Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

APCS Semester #1 Final Exam Practice Problems

Type Conversion. and. Statements

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Defining Languages GMU

Types and Programming Languages. Lecture 5. Extensions of simple types

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Explicit Recursion in Generic Haskell

Programming Languages and Techniques (CIS120)

COS 320. Compiling Techniques

Transcription:

Indexed Programming Jeremy Gibbons

Indexed Programming 2 1. Collections public interface List{ void add(int index, Object element); boolean contains(object o); Object get(int index); int size(); Loss of precision: List l= new ArrayList(); //... which implements List String s="abc"; l.add(0, s); // upcasting when inserting s=(string) l.get(0); // downcasting when retrieving

Indexed Programming 3 2. Generics public interface List E { void add(int index, E element); boolean contains(object o); E get(int index); int size(); More precise code: List String l = new ArrayList String (); String s="abc"; l.add(0, s); // no upcasting or... s= l.get(0); //... downcasting needed This is called parametric polymorphism.

Indexed Programming 4 3. Algebraic datatypes Expr 3 2 eval():object 1 Num Add Bool IsZero If int boolean

Indexed Programming 5 3.1. Expression datatype in Java public abstract class Expr{ public abstract Object eval(); public class Num extends Expr{ private Integer n; public Num(Integer n){this.n=n; public Object eval(){return n; public class Add extends Expr{ private Expr x, y; public Add(Expr x, Expr y){ this.x= x; this.y= y; public Object eval(){ return(integer)(x.eval()) +(Integer)(y.eval()); public class Bool extends Expr{ private Boolean b; public Bool(Boolean b){this.b = b; public Object eval(){return b; public class IsZero extends Expr{ private Expr e; public IsZero(Expr e){this.e = e; public Object eval(){ return new Boolean((Integer)(e.eval()) == 0); public class If extends Expr{ private Expr b; private Expr t, e; public If (Expr b, Expr t, Expr e){ this.b= b; this.t= t; this.e=e; public Object eval(){ if(((boolean) b.eval()).booleanvalue()) return t.eval(); else return e.eval();

Indexed Programming 6 3.2. Expression datatype in Haskell data Expr :: where N :: Int Expr B :: Bool Expr Add :: Expr Expr Expr IsZ :: Expr Expr If :: Expr Expr Expr Expr data Result= NR Int BR Bool eval :: Expr Result eval(n n) = NR n eval(b b) = BR b eval(add x y)=case(eval x, eval y) of(nr m, NR n) NR(m+n) eval(isz x) = case(eval x) of NR n NB(0 n) eval(if x y z) = case(eval x) of NB b if b then eval y else eval z

Indexed Programming 7 4. Indexing Loss of precision again: expressions of different types. Note the explicit tagging and untagging in Haskell, and the casts in Java. (And the lack of error-checking for ill-formed expressions!) Can we capture the precise constraints in code, and exploit them?

Indexed Programming 8 4.1. Indexed datatypes Parametrise the datatype (where parameter expresses represented type): data Expr :: where N :: Int Expr Int B :: Bool Expr Bool Add :: Expr Int Expr Int IsZ :: Expr Int If Expr Int Expr Bool :: Expr Bool Expr a Expr a Expr a Note that the parameter denotes a phantom type: a value of type Expr a need not contain elements of type a.

Indexed Programming 9 4.2. Indexed programming Specialised return types of constructors induce type constraints, which are exploited in type-checking definitions. eval :: Expr a a eval(n n) = n eval(b b) = b eval(add x y)=eval x+ eval y eval(isz x) = 0 eval x eval(if x y z) = if eval x then eval y else eval z Note that all the tagging and untagging has gone, and with it the possibility of run-time errors. By explicitly stating a property formerly implicit in the code, we have gained both in safety and in efficiency.

Indexed Programming 10 4.3. Indexed programming in Java It can be done with Java (or C#) generics, but it s not so pretty: public class If T extends Expr T { private Expr Boolean b; private Expr T t, e; public If (Expr Boolean b, Expr T t, Expr T e){ this.b=b; this.t= t; this.e=e; public T eval(){ if(b.eval().booleanvalue()) return t.eval(); else return e.eval(); (Actually, not quite all the checking can be done at compile-time; sometimes some casts are still necessary.)

Indexed Programming 11 5. Other applications Indexing by: size: eg bounded vectors shape: eg red-black trees state: eg locking of resources unit: eg physical dimensions type: eg datatype-generic programming proof: eg web applets

Indexed Programming 12 6. Application: indexing by size Empty datatypes as indices (so S(S Z) is a type). data Z data S n Size-indexed type of vectors: data Vector :: where VNil :: Vector a Z VCons :: a Vector a n Vector a(s n) Size constraint on vzip is captured in the type: vzip :: Vector a n Vector b n Vector(a, b) n vzip VNil VNil = VNil vzip(vcons a x)(vcons b y) = VCons(a, b)(vzip x y)

Indexed Programming 13 7. Application: indexing by shape 2-3-4 trees are perfectly-balanced search trees. 1 2 3 4 5 6 7 8 9 Representable as red-black trees binary search trees in which: every node is coloured either red or black every red node has a black parent every path from the root to a leaf contains the same number of black nodes (enforcing approximate balance) 1 2 3 4 5 6 7 8 9

Indexed Programming 14 In RBTree a c n, a is the element type; c is the root colour; n is the black height. data R data B data RBTree :: where Empty :: RBTree a B Z Red :: RBTree a B n a RBTree a B n RBTree a R n Black :: RBTree a c n a RBTree a c n RBTree a B(S n)

Indexed Programming 15 8. Application: indexing by state The ketchup problem : data O data C opened close closed data Edge :: where Open :: Edge O C Close :: Edge C O open shake Shake :: Edge C C data Path :: where Empty :: Path s s PCons :: Edge x y Path y z Path x z scenario :: Path O O scenario=pcons Open(PCons Shake(PCons Close Empty))

Indexed Programming 16 9. Application: indexing by unit Suppose dimensions of non-negative powers of metres and seconds: data Dim :: where D :: Float Dim m s distance :: Dim(S Z) Z distance=d 3.0 time :: Dim Z(S Z) time=d 2.0 A dimensioned value is a Float with two type-level tags. dadd :: Dim m s Dim m s Dim m s dadd(d x)(d y)=d(x+ y) Now dadd time time is well-typed, but dadd distance time is ill-typed. (More interesting to allow negative powers too, but for brevity... )

Indexed Programming 17 9.1. Type-level functions Proofs of properties about indices: data Add :: where AddZ :: Add Z n n AddS :: Add m n p Add(S m) n(s p) Used to constrain the type of dimensioned multiplication: dmult ::(Add m 1 m 2 m, Add s 1 s 2 s) Dim m 1 s 1 Dim m 2 s 2 Dim m s dmult(, )(D x)(d y)=d(x y) Thus, type-index of product is computed from indices of arguments.

Indexed Programming 18 9.2. Inferring proofs of properties Capture the proof as a type class (multi-parameter, with functional dependency; essentially a function on types). class Add m n p m n p instance Add Z n n instance Add m n p Add(S m) n(s p) Now the proof can be (type-)inferred rather than passed explicitly. dmult ::(Add m 1 m 2 m, Add s 1 s 2 s) Dim m 1 s 1 Dim m 2 s 2 Dim m s dmult(d x)(d y)=d(x y) Note that the type class has no methods, so corresponds to an empty dictionary; it can be optimized away.

Indexed Programming 19 10. Application: indexing by type Generic programming is about writing programs parametrized by datatypes; for example, a generic data marshaller. One implementation of generic programming manifests the parameters as some family of type representations. For example, C s sprintf is generic over a family of format specifiers. data Format :: where I :: Format a Format(Int a) B :: Format a Format(Bool a) S :: String Format a Format a F :: Format String A term of type Format a is a representation of the type a, for various types a (such as Int Bool String) appropriate for sprintf.

Indexed Programming 20 10.1. Type-indexed dispatching The function sprintf interprets the representation, generating a function of the appropriate type: sprintf :: Format a a sprintf fmt= aux id fmt where aux ::(String String) Format a a aux f (I fmt) =λn aux(f (show n+)) fmt aux f (B fmt) =λb aux(f (show b+)) fmt aux f (S s fmt)=aux(f (s+)) fmt aux f (F) = f "" For example, sprintf f 13 True="Int is 13, bool is True.", where f :: Format(Int Bool String) f = S "Int is "(I(S ", bool is "(B(S "." F))))

Indexed Programming 21 11. Application: indexing by proof The game of Mini-Nim: a pile of matchsticks players take turns to remove one match or two player who removes the last match wins Index positions by size and proof of destiny. data Win data Lose data Position n r where Empty :: Position Z Lose Take1 :: Position n Lose Position(S n) Win Take2 :: Position n Lose Position(S(S n)) Win Fail :: Position n Win Position(S n) Win Position(S(S n)) Lose

Indexed Programming 22 12. Adding weight We have shown some examples in Haskell with small extensions. This is a very lightweight approach to dependently-typed programming. Lightweight approaches have low entry cost, but relatively high continued cost: encoding via type classes etc is a bit painful. Tim Sheard sωmega is a cut-down version of Haskell with explicit support for GADTs: kind declarations type-level functions statically-generated witnesses Xi and Pfenning s Dependent ML provides natural-number indices, and incorporates decision procedures for discharging proof obligations. These are more heavyweight approaches (such as McBride et al s Epigram).

Indexed Programming 23 13. Conclusions generics have become mainstream... but so much more than parametric polymorphism! indexed programming: a form of lightweight dependent types Generic and Indexed Programming project at Oxford perhaps algebraic datatypes will jump the gap next? (cf Scala)

Indexed Programming 24 14. Shameless plug...