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

Similar documents
Programming with Universes, Generically

Generic Constructors and Eliminators from Descriptions

Introduction to dependent types in Coq

Fully Generic Programming Over Closed Universes of Inductive- Recursive Types

An Introduction to Programming and Proving in Agda (incomplete draft)

Types, Universes and Everything

Contents. Chapter 1 SPECIFYING SYNTAX 1

Generic Programming With Dependent Types: II

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Lecture 8: Summary of Haskell course + Type Level Programming

Inductive Definitions, continued

On Agda JAIST/AIST WS CVS/AIST Yoshiki Kinoshita, Yoriyuki Yamagata. Agenda

LOGIC AND DISCRETE MATHEMATICS

Negations in Refinement Type Systems

Dependent Polymorphism. Makoto Hamana

From natural numbers to the lambda calculus

GADTs. GADTs in Haskell

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

HOL DEFINING HIGHER ORDER LOGIC LAST TIME ON HOL CONTENT. Slide 3. Slide 1. Slide 4. Slide 2 WHAT IS HIGHER ORDER LOGIC? 2 LAST TIME ON HOL 1

CIS 194: Homework 8. Due Wednesday, 8 April. Propositional Logic. Implication

Introduction to Coq Proof Assistant

Inductive Types for Free

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

Typed Lambda Calculus

Generic programming with ornaments and dependent types

Automata and Formal Languages - CM0081 Introduction to Agda

A NEW PROOF-ASSISTANT THAT REVISITS HOMOTOPY TYPE THEORY THE THEORETICAL FOUNDATIONS OF COQ USING NICOLAS TABAREAU

Introduction to Co-Induction in Coq

Type families and data kinds

Denotational Semantics. Domain Theory

Programming with Ornaments

EXTENSIONS OF FIRST ORDER LOGIC

Parametricity and Dependent Types

Provably Correct Software

Modular dependent induction in Coq, Mendler-style. Paolo Torrini

Lecture 14: Recursive Types

Typed Lambda Calculus for Syntacticians

Lecture slides & distribution files:

Programming in Omega Part 1. Tim Sheard Portland State University

Measurable Preorders and Complexity

COMP 3141 Software System Design and Implementation

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

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

DRAFT. Dependent types. Chapter The power of and

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

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

Programming Languages Third Edition

Pretty-Big-Step Semantics

Reflection in the Chomsky Hierarchy

Proofs are Programs. Prof. Clarkson Fall Today s music: Proof by Paul Simon

Martin-L f's Type Theory. B. Nordstr m, K. Petersson and J. M. Smith. Contents. 5.4 The set of functions (Cartesian product of a family of sets) 24

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

Alonzo a Compiler for Agda

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

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Less Is More. Generic Programming Theory and Practice. José Pedro Magalhães

Mathematics for Computer Scientists 2 (G52MC2)

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

Introduction to Homotopy Type Theory

Overview. A Compact Introduction to Isabelle/HOL. Tobias Nipkow. System Architecture. Overview of Isabelle/HOL

> module Lambda where > import Data.List -- I need some standard list functions

Dependent Types and Irrelevance

ABriefOverviewofAgda A Functional Language with Dependent Types

10 Years of Partiality and General Recursion in Type Theory

Dependently Typed Meta-programming

Representing Cyclic Structures as Nested Datatypes. Makoto Hamana

CSCI-GA Scripting Languages

Calculus of Inductive Constructions

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

Lecture Notes on Data Representation

Proofs are Programs. Prof. Clarkson Fall Today s music: Two Sides to Every Story by Dyan Cannon and Willie Nelson

Finite Model Generation for Isabelle/HOL Using a SAT Solver

HIGHER-ORDER ABSTRACT SYNTAX IN TYPE THEORY

A modern back-end for a dependently typed language

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Constructing Strictly Positive Families

type classes & locales

Discrete Mathematics Lecture 4. Harper Langston New York University

The Truth about Types. Bartosz Milewski

Programming Languages Fall 2014

Introduction to Type Theory August 2007 Types Summer School Bertinoro, It. Herman Geuvers Nijmegen NL

Programming with Math and Logic

Mendler-style Recursion Schemes for Mixed-Variant Datatypes

Programming Languages Lecture 15: Recursive Types & Subtyping

Basic Foundations of Isabelle/HOL

Toward explicit rewrite rules in the λπ-calculus modulo

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

The Rule of Constancy(Derived Frame Rule)

Structural polymorphism in Generic Haskell

The three faces of homotopy type theory. Type theory and category theory. Minicourse plan. Typing judgments. Michael Shulman.

From Math to Machine A formal derivation of an executable Krivine Machine Wouter Swierstra Brouwer Seminar

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

Initial Algebra Semantics for Cyclic Sharing Structures. Makoto Hamana

Combining Programming with Theorem Proving

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

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

From Types to Sets in Isabelle/HOL

Programming Proofs and Proving Programs. Nick Benton Microsoft Research, Cambridge

Transcription:

for Data Peter Morris University of Nottingham November 12, 2009

Introduction Outline 1 Introduction What is DTP? Data Types in DTP Schemas for Inductive Families 2 of Data Inductive Types Inductive Families A Closed Type Theory 3 Generic Programming Another motivation again

Introduction Roadmap 1 Introduction What is DTP? Data Types in DTP Schemas for Inductive Families 2 of Data Inductive Types Inductive Families A Closed Type Theory 3 Generic Programming Another motivation again

Introduction What is DTP? Curry-Howard and Dependently Typed Programming Dependently Typed Programming is based on the idea that Types are Propositions, and Programs are Proofs. This is the Curry-Howard Isomorphism. First we identify the type of propositions with Set. The implication A = B is a function A B The conjunction A B is a Cartesian-product A B The disjunction A B is a disjoint union A + B So we can interpret Propositional Logic as a simply typed lambda calculus. But what about Predicate logic?

Introduction What is DTP? Curry-Howard and Predicate Logic A predicate on A is a function P : A Set How do we interpret the proposition a : A.P a? As a dependent function space (a : A) P a. The type of the output of such a function, varies depending on the input. What the proposition a : A.P a? We'll come back to that...

Introduction Data Types in DTP Indexed Families The datatypes of dependently typed languages can also depend on data: Natural Numbers data Nat : Set where zero : Nat succ : (n : Nat) Nat Lists data List (A : Set) : Set where ɛ : List A _::_ : (a : A) (as : List A) List A

Introduction Data Types in DTP Finer Program Control Safe hd We can use these indicies to prevent programs from going wrong hd : {n} {A} Vec A (succ n) A hd (a :: as) = a Compare with the version for lists: Maybe hd maybehd : {A} List A Maybe A maybehd ɛ = no maybehd (a :: as) = yes a

Introduction Data Types in DTP Finite Sets We can dene a set Fin n which has exactly n elements: Finite Sets data Fin : Nat Set where zero : {n} Fin (succ n) succ : {n} Fin n Fin (succ n) Which can help dene a type of well scoped lambda terms: Scoped Lambda-Terms data Lam : Nat Set where var : {n} Fin n Lam n app : {n} Lam n Lam n Lam n abs : {n} Lam (succ n) Lam n

Introduction Data Types in DTP Existential quantication and equality Using these indexed families, we can return to the question of interpreting existentials - as Sigma-types: Sigma Types data Σ (A : Set) (P : A Set) : Set where, : (x : A) (y : P x) Σ A P so a : A.P a is interpreted as Σ A \a P a We can also dene predicates and relations inductively, for instance equality: Sigma Types data _ _ {A : Set} (a : A) : A Set where re : a a

Introduction Schemas for Inductive Families Schemas What is the status of these Datatypes with respect to the Type Theory of the programming language? We have to be careful of what denitions we allow... With languages like Agda, and Epigram an external piece of code, a schema checker, looks to see if each denition is OK with a syntactic check. If it is the TT is extended with the introduction, computation and equality rules for the data type. This approach, however, brings about problems for reasoning about the language, we need an external framework to prove the schema checker correct. It also precludes any attempt to interpret the language in itself, Agda in Adga.

Introduction Informally universe is a collection of types (sets). Russell's solution to the paradoxes of Set-theory was to introduce a predicative hierarchy of universes: Russell's Universe Hierarchy Set 0 : Set 1 : Set 2 :... : Set i : Set i+1 :...... Or alternatively: Tarski's Universe Hierarchy U i : Set El i : U i Set u i : U i+1 s.t. El i+1 u i U i

Introduction for Data We can use Tarski style universes to capture other interesting collections of types, in general we'll need: Tarski's U : Set El : U Set If we can capture a universe of inductive families in our language, then we can do without external schemas. With such a universe, creating a datatype would no longer extend the logic, making it easier to reason about the system itself.

of Data Roadmap 1 Introduction What is DTP? Data Types in DTP Schemas for Inductive Families 2 of Data Inductive Types Inductive Families A Closed Type Theory 3 Generic Programming Another motivation again

of Data Inductive Types The Syntax of Inductive Types A syntax Lets start by simply encoding the syntax of data denitions: data Desc : Set where done : Desc arg : (A : Set) (φ : A Desc) Desc ind : (H : Set) (φ : Desc) Desc Every data description gives rise to a functor: Interpreting the syntax _ : Desc Set Set done D = 1 arg A φ D = Σ A \a φ a D ind H φ D = Σ (H D) \h φ D

of Data Inductive Types The Syntax of Inductive Types (2) The initial algebras of these functors are our data types: Initial Algebras data µ (φ : Desc) : Set where intro : φ (µ φ) µ φ By adding this as a rule to our theory we encode introduction rules for all inductive types in one go.

of Data Inductive Types The Syntax of Inductive Types (3) Example Lists ListC : Set Desc ListC A = arg [cnil ccons] \x case x of cnil done ccons arg A \_ ind 1 done nil : {A : Set} µ (ListC A) nil : intro (cnil, ) cons : {A : Set} A µ (ListC A) µ (ListC A) cons a as = intro (ccons, (a, as, ))

of Data Inductive Types Elimination Induction : (φ : Desc) (D : Set) (P : D Set) (v : φ D) Set done D P v = 1 (arg A φ) D P (a, b) = (φ a) D P b (ind H φ) D P (a, b) = Σ ((h : H) P (a h)) \_ φ D P b map : (φ : Desc) (D : Set) (P : D Set) (p : (d : D) P d) (v : φ D) φ D P v map done D P p v = map (arg A φ) D P p (a, b) = map (φ a) D P p b map (ind H φ) D P p (a, b) = (\h p (a h)), map φ D P p b elim : (φ : Desc) (P : µ φ Set) (p : (x : φ (µ φ)) φ (µ φ) P x P (intro x)) (v : µ φ) P v elim φ P p (intro v) = p v (map φ (µ φ) P (elim φ P p) v)

of Data Inductive Families The Syntax of Inductive Families A syntax We can extend our syntax to include the necessary indexing information: data Desc (I : Set) : Set where done : I Desc I arg : (A : Set) (φ : A Desc I) Desc I ind : (H : Set) (is : H I) (φ : Desc I) Desc I Every description gives rise to an I-indexed functor: Interpreting the syntax _ : {I : Set} Desc I (I Set) (I Set) done j D i = i j arg A φ D i = Σ A \a φ a D i ind H is φ D i = Σ ((h : H) D (is h)) \h φ D i

of Data Inductive Families The Syntax of Inductive Families (2) The initial algebras of these functors are our data types: Initial Algebras data µ {I : Set} (φ : Desc I) : I Set where intro : {i : I} φ (µ φ) i µ φ i By adding this as a rule to our TT we encode introduction rules for all inductive families in one go.

of Data Inductive Families The Syntax of Inductive Families (3) Example Vectors VecC : Set Desc Nat VecC A = arg [cnil ccons] \x case x of cnil done zero ccons arg Nat \n arg A \_ ind 1 (\_ n) done (succ n) nil : {A : Set} µ (VecC A) zero nil : intro (cnil, re) cons : {n A} A µ (VecC A) n µ (VecC A) (succ n) cons {n} a as = intro (ccons, (n, a, as, re))

of Data Inductive Families Elimination Induction : {I : Set} (φ : Desc I) (D : I Set) (P : {i : I} D i Set) {i : I} (v : φ D i) Set (done i) D P re = 1 (arg A φ) D P (a, b) = (φ a) D P b (ind H is φ) D P (a, b) = Σ ((h : H) P (a h)) \_ φ D P b map : {I : Set} (φ : Desc I) (D : I Set) (P : {i : I} D i Set) (p : {i : I} (d : D i) P d) {i : I} (v : φ D i) φ D P v map (done i) D P p re = map (arg A φ) D P p (a, b) = map (φ a) D P p b map (ind H is φ) D P p (a, b) = (\h p (a h)), map φ D P p b elim : {I : Set} (φ : Desc I) (P : {i : I} µ φ i Set) (p : {i : I} (x : φ (µ φ) i) φ (µ φ) P x P (intro x)) {i : I} (v : µ φ i) P v elim φ P p (intro v) = p v (map φ (µ φ) P (elim φ P p) v)

of Data A Closed Type Theory What does this buy us? Given a Type Theory with nite types and sigma types we can add datatypes by adding the rules for the universe described above. This new type theory is closed under the denition of new data-types, in some sense they are already present in the theory. In fact, we can go further, since the data types Desc and µ are themselves inductive families, we should be able to dene them as codes in the Desc universe. But that's a bit circular, so we need a hierarchy of data universes Desc i : Desc i+1. In this way we only have to add rules to our TT for _ and elim.

Generic Programming Roadmap 1 Introduction What is DTP? Data Types in DTP Schemas for Inductive Families 2 of Data Inductive Types Inductive Families A Closed Type Theory 3 Generic Programming Another motivation again

Generic Programming Another motivation Type Proliferation The properties and invariants we might want to specify are endless: From Lists.... to Vectors.... to Bounded Length Lists.. to Sorted Lists.... to Sorted Vectors.... to Sorted, Bounded Lists.. to Fresh Lists..... Prot? And each incarnation may need to be equipped with some notion of Map Concatenation Fold Filter

Generic Programming Another motivation Generics Functional languages like Haskell already suer from this problem (lite). There is a large research community pursuing a solution called generic or polytypic programming. A generic program is one that works on any of class of types, specialising its operation on the structure of type. Generic programming systems tend to be written as preprocessors, or make heavy use of experimental language systems. It turns out, what they really need is universes..

Generic Programming again for Generics Given a universe of data, a generic function is one that has this shape: The shape of a generic function foo : {u : U} (x : El u) T u x Such a function will work for any type in the universe U, specialising its operation on the structure of the code u. In fact the function elim for the Desc universe we saw above, is a generic function.

Generic Programming again Carving out useful universes We don't win just yet though, since the Desc universe is relatively large it supports very few generic programs...in fact only elim We don't need just one universe for generics, but rather many small universes, each supporting a dierent class of generic functions. Typically the functions we want to write determine the class of types the universe should capture. Looking at it in this way, we can see that Desc supports elim because it captures exactly those families which have a sound induction principle.