CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Similar documents
CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Lecture Notes on Data Representation

CSE-321 Programming Languages 2010 Final

We defined congruence rules that determine the order of evaluation, using the following evaluation

Programming Languages Lecture 15: Recursive Types & Subtyping

Lecture 14: Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

CSE-321 Programming Languages 2011 Final

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

Recursive Types and Subtyping

1 Introduction. 3 Syntax

CS611 Lecture 33 Equirecursive types & Recursive domain equations November 19, 2001

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Inductive datatypes in HOL. lessons learned in Formal-Logic Engineering

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Harvard School of Engineering and Applied Sciences Computer Science 152

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Recursive Types and Subtyping

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Less naive type theory

Supplementary Notes on Recursive Types

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

CIS 500 Software Foundations Midterm I

λ calculus is inconsistent

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

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

Denotational Semantics. Domain Theory

ADT. Typing. Recursive Types. Java. Preliminary: syntax, operational semantics. Untyped lambda calculus. Simply typed lambda calculus

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Lambda Calculus. Lecture 4 CS /26/10

Introduction to System F. Lecture 18 CS 565 4/20/09

CSE505, Fall 2012, Midterm Examination October 30, 2012

Derivation for the Necessity of Recursive Types and its Basic Usage

CSE-321 Programming Languages 2014 Final

Lecture #23: Conversion and Type Inference

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

Pattern Matching and Abstract Data Types

Theorem Proving Principles, Techniques, Applications Recursion

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

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

CSE-321 Programming Languages 2010 Midterm

CSE 505, Fall 2006, Final Examination 11 December Please do not turn the page until everyone is ready.

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CSE-321 Programming Languages 2012 Midterm

Type-indexed functions in Generic Haskell

Recursive Types. Chapter 7

Structural polymorphism in Generic Haskell

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

Metaprogramming assignment 3

Type Systems Winter Semester 2006

Typing Control. Chapter Conditionals

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Official Survey. Cunning Plan: Focus On Objects. The Need for a Calculus. Object Calculi Summary. Why Not Use λ-calculus for OO?

Typed Compilation Against Non-Manifest Base Classes

CIS 500 Software Foundations Fall September 25

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Lambda calculus. Chapter 2

CSE 505: Concepts of Programming Languages

Lecture 13 CIS 341: COMPILERS

Hiding local state in direct style: a higher-order anti-frame rule

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Lecture Notes on Program Equivalence

Exercise 1 ( = 24 points)

Variables. Substitution

References and Exceptions. CS 565 Lecture 14 4/1/08

Introduction to the λ-calculus

Polymorphism. Lecture 19 CS 565 4/17/08

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

Mutable References. Chapter 1

MLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen

CMSC 330: Organization of Programming Languages. Operational Semantics

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Semantics of programming languages

CMSC 330: Organization of Programming Languages

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

More Lambda Calculus and Intro to Type Systems

Exercise 1 ( = 18 points)

Programming Language Concepts: Lecture 19

Using Strategies for Assessment of Functional Programming Exercises

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

Typed Lambda Calculus for Syntacticians

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

If a program is well-typed, then a type can be inferred. For example, consider the program

Introduction to Lambda Calculus. Lecture 7 CS /08/09

4.6 Recursive Types. 4.6 Recursive Types 105

Advances in Programming Languages

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

Chapter 5: The Untyped Lambda Calculus

Preuves Interactives et Applications

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

Recitation 8: Dynamic and Unityped Languages : Foundations of Programming Languages

Transcription:

CS 4110 Programming Languages & Logics Lecture 27 Recursive Types 4 November 2016

Announcements 2 My office hours are at the normal time today but canceled on Monday Guest lecture by Seung Hee Han on Monday

Recursive Types 3 Many languages support data types that refer to themselves: Java class Tree { Tree leftchild, rightchild; int data; }

Recursive Types 3 Many languages support data types that refer to themselves: Java class Tree { Tree leftchild, rightchild; int data; } OCaml type tree = Leaf Node of tree * tree * int

Recursive Types 3 Many languages support data types that refer to themselves: Java class Tree { Tree leftchild, rightchild; int data; } OCaml type tree = Leaf Node of tree * tree * int λ-calculus? tree = unit + int tree tree

Recursive Type Equations 4 We would like tree to be a solution of the equation: α = unit + int α α However, no such solution exists with the types we have so far...

Unwinding Equations 5 We could unwind the equation: α = unit + int α α

Unwinding Equations 5 We could unwind the equation: α = unit + int α α = unit + int (unit + int α α) (unit + int α α)

Unwinding Equations 5 We could unwind the equation: α = unit + int α α = unit + int (unit + int α α) (unit + int α α) = unit + int (unit + int (unit + int α α) (unit + int α α)) (unit + int (unit + int α α) (unit + int α α))

Unwinding Equations 5 We could unwind the equation: α = unit + int α α = unit + int (unit + int α α) (unit + int α α) = unit + int (unit + int (unit + int α α) (unit + int α α)) (unit + int (unit + int α α) (unit + int α α)) =

Unwinding Equations We could unwind the equation: α = unit + int α α = unit + int (unit + int α α) (unit + int α α) = unit + int (unit + int (unit + int α α) (unit + int α α)) (unit + int (unit + int α α) (unit + int α α)) = If we take the limit of this process, we have an infinite tree. 5

Infinite Types 6 Think of this as an infinite labeled graph whose nodes are labeled with the type constructors, +, int, and unit. This infinite tree is a solution of our equation, and this is what we take as the type tree.

µ Types 7 We ll specify potentially-infinite solutions to type equations using a finite syntax based on the fixed-point type constructor µ. µα. τ

µ Types 7 We ll specify potentially-infinite solutions to type equations using a finite syntax based on the fixed-point type constructor µ. µα. τ Here s a tree type satisfying our original equation: tree µα. unit + int α α.

Static Semantics (Equirecursive) 8 We ll define two treatments of recursive types. With equirecursive types, a recursive type is equal to its unfolding: µα. τ is a solution to α = τ, so: µα. τ = τ{µα. τ/α}

Static Semantics (Equirecursive) 8 We ll define two treatments of recursive types. With equirecursive types, a recursive type is equal to its unfolding: µα. τ is a solution to α = τ, so: µα. τ = τ{µα. τ/α} Two typing rules let us switch between folded and unfolded: Γ e : τ{µα. τ/α} Γ e : µα. τ µ-intro Γ e : µα. τ Γ e : τ{µα. τ/α} µ-elim

Isorecursive Types 9 Alternatively, isorecursive types avoid infinite type trees. The type µα. τ is distinct but transformable to and from τ{µα. τ/α}.

Isorecursive Types 9 Alternatively, isorecursive types avoid infinite type trees. The type µα. τ is distinct but transformable to and from τ{µα. τ/α}. Converting between the two uses explicit fold and unfold operations: unfold µα. τ : µα. τ τ{µα. τ/α} fold µα. τ : τ{µα. τ/α} µα. τ

Static Semantics (Isorecursive) 10 The typing rules introduce and eliminate µ-types: Γ e : τ{µα. τ/α} Γ fold e : µα. τ µ-intro Γ e : µα. τ Γ unfold e : τ{µα. τ/α} µ-elim

Dynamic Semantics 11 We also need to augment the operational semantics: unfold (fold e) e Intuitively, to access data in a recursive type µα. τ, we need to unfold it first. And the only way that values of type µα. τ could have been created is via fold.

Example 12 Here s a recursive type for lists of numbers: intlist µα. unit + int α.

Example 12 Here s a recursive type for lists of numbers: intlist µα. unit + int α. Here s how to add up the elements of an intlist: let sum = fix (λf:intlist intlist λl:intlist. case unfold l of (λu : unit. 0) (λp : int intlist. (#1 p) + f (#2 p)))

Encoding Numbers 13 Recursive types let us encode the natural numbers!

Encoding Numbers 13 Recursive types let us encode the natural numbers! A natural number is either 0 or the successor of a natural number: nat µα. unit + α

Encoding Numbers 13 Recursive types let us encode the natural numbers! A natural number is either 0 or the successor of a natural number: nat µα. unit + α 0 fold (inl unit+nat ())

Encoding Numbers 13 Recursive types let us encode the natural numbers! A natural number is either 0 or the successor of a natural number: nat µα. unit + α 0 fold (inl unit+nat ()) 1 fold (inr unit+nat 0) 2 fold (inr unit+nat 1),.

Encoding Numbers 13 Recursive types let us encode the natural numbers! A natural number is either 0 or the successor of a natural number: nat µα. unit + α 0 fold (inl unit+nat ()) 1 fold (inr unit+nat 0) 2 fold (inr unit+nat 1), The successor function has type nat nat:. (λx : nat. fold (inr unit+nat x))

Self-Application and Ω 14 Recall Ω defined as: ω λx. x x Ω ω ω. Ω was impossible to type... until now!

Self-Application and Ω 14 Recall Ω defined as: ω λx. x x Ω ω ω. Ω was impossible to type... until now! x is a function. Let s say it has the type σ τ.

Self-Application and Ω 14 Recall Ω defined as: ω λx. x x Ω ω ω. Ω was impossible to type... until now! x is a function. Let s say it has the type σ τ. x is used as the argument to this function, so it must have type σ.

Self-Application and Ω 14 Recall Ω defined as: ω λx. x x Ω ω ω. Ω was impossible to type... until now! x is a function. Let s say it has the type σ τ. x is used as the argument to this function, so it must have type σ. So let s write a type equation: σ = σ τ

Self-Application and Ω 15 Putting these pieces together, the fully typed ω term is: ω λx : µα. (α τ). (unfold x) x

Self-Application and Ω 15 Putting these pieces together, the fully typed ω term is: ω λx : µα. (α τ). (unfold x) x The type of ω is (µα. (α τ)) τ. So the type of fold ω is µα. (α τ).

Self-Application and Ω 15 Putting these pieces together, the fully typed ω term is: ω λx : µα. (α τ). (unfold x) x The type of ω is (µα. (α τ)) τ. So the type of fold ω is µα. (α τ). Now we can define Ω = ω (fold ω). It has type τ.

Self-Application and Ω 16 We can even write ω in OCaml: # type u = Fold of (u -> u);; type u = Fold of (u -> u) # let omega = fun x -> match x with Fold f -> f x;; val omega : u -> u = <fun> # omega (Fold omega);;...runs forever until you hit control-c

Encoding λ-calculus 17 With recursive types, we can type everything in the untyped lambda calculus!

Encoding λ-calculus 17 With recursive types, we can type everything in the untyped lambda calculus! Every λ-term can be applied as a function to any other λ-term. So let s define an untyped type: U µα. α α

Encoding λ-calculus With recursive types, we can type everything in the untyped lambda calculus! Every λ-term can be applied as a function to any other λ-term. So let s define an untyped type: U µα. α α The full translation is: [[x]] x [[e 0 e 1 ]] (unfold [[e 0 ]]) [[e 1 ]] [[λx. e]] fold λx : U. [[e]] Every untyped term maps to a term of type U. 17