Dependent Polymorphism. Makoto Hamana

Similar documents
Initial Algebra Semantics for Cyclic Sharing Structures. Makoto Hamana

Representing Cyclic Structures as Nested Datatypes. Makoto Hamana

Logic - CM0845 Introduction to Haskell

Coq with Classes. Matthieu Sozeau. Journées PPS 2011 September 5th 2011 Trouville, France. Project Team πr 2 INRIA Paris

Alonzo a Compiler for Agda

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Programming with Universes, Generically

Provably Correct Software

Generic programming with ornaments and dependent types

Structural polymorphism in Generic Haskell

Lecture 8: Summary of Haskell course + Type Level Programming

Universes. Universes for Data. Peter Morris. University of Nottingham. November 12, 2009

Fully Generic Programming Over Closed Universes of Inductive- Recursive Types

Inductive Types for Free

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

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

Type-indexed functions in Generic Haskell

Programming with Math and Logic

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

Giving Haskell a Promotion

IA014: Advanced Functional Programming

Lightweight Invariants with Full Dependent Types

Exercise 1 (2+2+2 points)

Many Holes in Hindley-Milner

Idris: Implementing a Dependently Typed Programming Language

Vectors are records, too!

Functional Programming and Modeling

Parametricity and Dependent Types

Lesson 11 Universal Types. Universal Types and System F

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Theorem Proving Principles, Techniques, Applications Recursion

Generic Programming With Dependent Types: II

Generic Constructors and Eliminators from Descriptions

From natural numbers to the lambda calculus

Type checking by theorem proving in IDRIS

Idris. Programming with Dependent Types. Edwin Brady University of St Andrews, Scotland,

Type families and data kinds

Ideas over terms generalization in Coq

First-Class Type Classes

Idris, a language with dependent types Extended Abstract

Nonuniform (Co)datatypes

Coercion Quantification

Towards Reasoning about State Transformer Monads in Agda. Master of Science Thesis in Computer Science: Algorithm, Language and Logic.

CS 320: Concepts of Programming Languages

10 Years of Partiality and General Recursion in Type Theory

Generic discrimination

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Programming in Omega Part 1. Tim Sheard Portland State University

Structures in Coq January 27th 2014

EPIGRAM 1: a perspective on functional programming with dependent types

Static Contract Checking for Haskell

Polymorphism and System-F (OV)

Proofs for free. Parametricity for dependent types. JEAN-PHILIPPE BERNARDY and PATRIK JANSSON

Exercise 1 ( = 24 points)

Programming Languages 3. Definition and Proof by Induction

From Types to Contracts

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Overloading, Type Classes, and Algebraic Datatypes

SOFTWARE VERIFICATION AND COMPUTER PROOF (lesson 1) Enrico Tassi Inria Sophia-Antipolis

Depending on Types. Stephanie Weirich University of Pennsylvania

Combining Programming with Theorem Proving

Type Theory. Lecture 2: Dependent Types. Andreas Abel. Department of Computer Science and Engineering Chalmers and Gothenburg University

Programming Up-to-Congruence, Again. WG 2.8 Estes Park

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

ABriefOverviewofAgda A Functional Language with Dependent Types

Programming with dependent types: passing fad or useful tool?

Faith, Evolution, and Programming Languages. Philip Wadler University of Edinburgh

Embedded Domain Specific Language Implementation using Dependent Types

Coq Summer School, Session 2 : Basic programming with numbers and lists. Pierre Letouzey

Compiling Exceptions Correctly

COMP 3141 Software System Design and Implementation

An Algebra of Dependent Data Types

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Refinements for free! 1

CSCE 314 Programming Languages

Graphical Untyped Lambda Calculus Interactive Interpreter

Algebra of Programming using Dependent Types

Extended Static Checking for Haskell (ESC/Haskell)

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Expr_annotated.v. Expr_annotated.v. Printed by Zach Tatlock

Bag Equivalence via a Proof-Relevant Membership Relation

Adam Chlipala University of California, Berkeley ICFP 2006

Libraries for Generic Programming in Haskell

Programming with Ornaments

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

Shell CSCE 314 TAMU. Higher Order Functions

Exploring Lightweight Implementations of Generics. University of Oxford Page 1

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

CSC324 Principles of Programming Languages

MoreIntro.v. MoreIntro.v. Printed by Zach Tatlock. Oct 07, 16 18:11 Page 1/10. Oct 07, 16 18:11 Page 2/10. Monday October 10, 2016 lec02/moreintro.

Inductive data types

CS 320: Concepts of Programming Languages

Witnessing Purity, Constancy and Mutability APLAS Ben Lippmeier Australian National University 2009/12/14

Advanced features of Functional Programming (Haskell)

Dependent types and program equivalence. Stephanie Weirich, University of Pennsylvania with Limin Jia, Jianzhou Zhao, and Vilhelm Sjöberg

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Haskell-style type classes with Isabelle/Isar

CIS 500: Software Foundations

Transcription:

1 Dependent Polymorphism Makoto Hamana Department of Computer Science, Gunma University, Japan http://www.cs.gunma-u.ac.jp/ hamana/

This Talk 2 [I] A semantics for dependently-typed programming [II] A kind of polymorphism from semantics dependent polymorphism

This Talk 3 [I] A semantics for dependently-typed programming [II] A kind of polymorphism from semantics dependent polymorphism

4 Dependent type

Dependently-typed Programming, Now! 5 (i) Agda [Chalmers 07-,AIST] (ii) Coq with program/equations tactic [Sozeau ICFP 07,ITP 10] (iii) Epigram [McBride,McKinna 04-] (iv) Haskell with type classes/gadts [McBride JFP 02, Hinze 03] Origin Dependently Typed Functional Programs and Their Proofs Conor McBride, Ph.D thesis, University of Edinburgh, 1999.

How to Use Dependent Types in Programming? 6 data Nat : Set where zero : Nat suc : Nat -> Nat data Vec : Nat -> Set where -- an inductive family [] : Vec zero _::_ : {n : Nat} -> (a : A) -> Vec n -> Vec (suc n) Vec type of length-indexed lists [] : Vec zero a1 :: [] : Vec (suc zero) a2 :: a1 :: [] : Vec (suc (suc zero))

How to Use Dependent Types in Programming? 7 Safe head head : {n : Nat} -> Vec (suc n) -> A head (x :: xs) = x Never fails

Typical Example: append 8 _++_ : {m n : Nat} -> Vec m -> Vec n -> Vec (m + n) [] ++ ys = ys (x :: xs) ++ ys = x :: (xs ++ ys) The index of result type precisely specifies the resulting list Is it always possible?

More Example: filter 9 Agda filter : {n : Nat} -> (Nat -> Bool) -> Vec n -> Vec (?) filter p [] = [] filter p (x :: xs) with p x... False = filter p xs... True = x :: filter p xs

More Example: filter 10 Agda: first attempt filter : {n : Nat} -> (p : Nat -> Bool) -> (xs : Vec n) -> Vec (length (filter p xs)) filter p [] = [] filter p (x :: xs) with p x... False = filter p xs... True = x :: filter p xs

More Example: filter 11 Agda: correct code len-filter : {n : Nat} -> (Nat -> Bool) -> Vec n -> Nat len-filter p [] = 0 len-filter p (x :: xs) with p x... False = len-filter p xs... True = suc (len-filter p xs) filter : {n : Nat} -> (p : Nat -> Bool) -> (xs : Vec n) -> Vec (len-filter p xs) filter p [] = [] filter p (x :: xs) with p x... False = filter p xs... True = x :: filter p xs

More Example: filter 12 Agda: correct code len-filter : {n : Nat} -> (Nat -> Bool) -> Vec n -> Nat len-filter p [] = 0 len-filter p (x :: xs) with p x... False = len-filter p xs... True = suc (len-filter p xs) filter : {n : Nat} -> (p : Nat -> Bool) -> (xs : Vec n) -> Vec (len-filter p xs) filter p [] = [] Dependent polymorphism helps filter p (x :: xs) with p x... False = filter p xs... True = x :: filter p xs

Classification of Polymorphism 13 Straychey [1967], Reynolds [1983] Ad-hoc Int Int + Int Int Real Real + Real Real Parametric Int Int Int R R R Real Real Real fst Int Int R fst Real Real Dependent V ec V ec V ec + V ec dependency Nat Nat Nat Nat +

This Talk 14 [I] A semantics for dependently-typed programming [II] A kind of polymorphism from semantics

New Semantics of Inductive Families 15 1) Simplified version of semantics of dependently-sorted abstract syntax [Fiore LICS 08] 2) Dependency category S of sorts 3) The category of discourse is Set S

Dependency Category S 16 data N at : Set where zero : Nat suc : Nat Nat data Vec : Nat Set where nil : Vec Zero cons : (n : Nat) (a : A) Vec n Vec (suc n) Dependency category S of sorts skeletal, DAG N len V Objects: sorts Arrows : sort dependencies

Semantic Construction of Models 17 data Nat : Set where zero : Nat suc : Nat Nat data Vec : Nat Set where nil : Vec Zero cons : (n : Nat) (a : A) Vec n Vec (suc n) Functor F : Set S Set S modelling an inductive family Initial F -algebra

Why Interesting? Programming Viewpoint 18 The category of discourse Set S A natural transformation f : A B in Set S is a family of functions {f s : A s B s s S} satisfying naturality polymorphism? s A s f s B s d s A(d) As f s B s B(d) in S in Set

Sort Dependency in Models 19 Term model T Set S T N = {zero} {suc(n) n T N } T V = {nil} {cons(n, b, y) n T N, b T B, y T V, T (len)(y) = n} Functoriality of T : S Set N len V in S T T N T (len) T V in Set

Sort Dependency in Model 20 Term model T Set S T N = {zero} {suc(n) n T N } T V = {nil} {cons(n, b, y) n T N, b T B, y T V, T (len)(y) = n} Functoriality of T : S Set N len V in S T T N T (len) length T V in Set 0 nil suc(n) cons(n, a, y) term level dependency

Dependent Polymorphism 21 ++ : V ec(m) V ec(n) V ec(m + n) + : Nat Nat nil ++ ys = ys zero + y = y (x : xs) ++ ys = x : (xs ++ ys) suc(n) + y = suc(n + y) V T V T V + T V

Dependent Polymorphism 22 ++ : V ec(m) V ec(n) V ec(m + n) + : Nat Nat nil ++ ys = ys zero + y = y (x : xs) ++ ys = x : (xs ++ ys) suc(n) + y = suc(n + y) V T V T V + T V len N T len T len T N T N + T T N len in S in Set

Dependent Polymorphism 23 ++ : V ec(m) V ec(n) V ec(m + n) + : Nat Nat nil ++ ys = ys zero + y = y (x :: xs) ++ ys = x :: (xs ++ ys) suc(n) + y = suc(n + y) V T V T V + T V len N T len T len T N T N + T T N len in S in Set T T Schematic definition z y = y c(n) y = c(n y) T in Set S is dependently polymorphic

Conclusion: What types admit this reading? 24 1. When indices are the shapes of data in a type (i) Vectors a2 :: (a1 :: []) : Vec (suc (suc zero)) (ii) Shape indexed type of trees [Hamana LMCS 10] e.g. bin( lf(3), lf(5) ) : Tree (B(L,L)) 2. When indices are calculated by fold Theoretical basis of code reuse in dependently-typed programming