Lecture 14: Recursive Types

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

Types and Programming Languages. Lecture 8. Recursive type

Derivation for the Necessity of Recursive Types and its Basic Usage

Design Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types

Design Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term, 2014

Lecture 13: Subtyping

Chapter 21: Metatheory of Recursive Types. Induction and Coinduction Finite and Infinite Types/Subtyping Membership Checking

Derivation for the Necessity of Recursive Types and its Basic Usage

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Programming Languages Lecture 15: Recursive Types & Subtyping

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

CSE-321 Programming Languages 2011 Final

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

Lecture 5: The Untyped λ-calculus

Lecture Notes on Data Representation

1 Introduction. 3 Syntax

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

The University of Nottingham

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

Inductive Definitions, continued

Exercise 1 (2+2+2 points)

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

Theorem Proving Principles, Techniques, Applications Recursion

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

Exercise 1 ( = 22 points)

Exercise 1 ( = 24 points)

Denotational Semantics. Domain Theory

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

Induction in Coq. Nate Foster Spring 2018

CS558 Programming Languages

A Simple Supercompiler Formally Verified in Coq

λ calculus is inconsistent

Exercise 1 ( = 18 points)

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

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

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

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

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

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

Lecture 7: The Untyped Lambda Calculus

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

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

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

Graphical Untyped Lambda Calculus Interactive Interpreter

Lecture Notes on Program Equivalence

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

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

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

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

CSCI-GA Scripting Languages

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

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

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

Integration of SMT Solvers with ITPs There and Back Again

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

CSE-321 Programming Languages 2010 Final

Lecture 8: Summary of Haskell course + Type Level Programming

Lecture 15 CIS 341: COMPILERS

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

Lectures 20, 21: Axiomatic Semantics

CSE 505: Concepts of Programming Languages

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

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

Calculus of Inductive Constructions

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

CIS 500 Software Foundations Midterm I

Mendler-style Recursion Schemes for Mixed-Variant Datatypes

Functional Programming

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Lecture 4: Higher Order Functions

The Untyped Lambda Calculus

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

Project Compiler. CS031 TA Help Session November 28, 2011

Dynamic Data Types - Recursive Records

Chapter 5: The Untyped Lambda Calculus

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

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Lambda Calculus. Lecture 4 CS /26/10

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Coq. LASER 2011 Summerschool Elba Island, Italy. Christine Paulin-Mohring

Introduction to Co-Induction in Coq

Introduction to Functional Programming in Haskell 1 / 56

Concepts of programming languages

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Formal Methods in Software Engineering. Lecture 07

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Recursive Types and Subtyping

Lambda Calculus and Extensions as Foundation of Functional Programming

1 The language χ. Models of Computation

Relaxing a Linear Typing for In-Place Update

Formal Systems and their Applications

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

Recursive Types and Subtyping

Lecture #23: Conversion and Type Inference

Lambda Calculi With Polymorphism

Transcription:

Lecture 14: Recursive Types Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Recursive Types CS546, 2018-2019 1 / 11

Motivation Lists, so far Introduce a type constructor List T Values are either nil or cons (e hd, e tl ) List have arbitrary size, but regular structure Similarly, queues, binary trees, labeled trees, ASTs, etc It is impractical to extend the language with each as an additional primitive type! Solution: recursive types Pratikakis (CSD) Recursive Types CS546, 2018-2019 2 / 11

Example Lists of numbers: NatList = nil : Unit, cons : {Nat, NatList} This equation defines an infinite tree To change into a definition, use abstraction NatList = µx nil : Unit, cons : {Nat, X} µ is the explicit recursion operator for types Intuitively: NatList is the type that satisfies the equation X = nil : Unit, cons : {Nat, X} Pratikakis (CSD) Recursive Types CS546, 2018-2019 3 / 11

Example: Lists Lists nil = nil = () as NatList cons = λx : Natλl : NatList cons = {x, l} as NatList isnil = λl : NatListcase l of nil(_) => true cons(_) => false hd = λl : NatListcase l of nil(_) => 0 cons(p) => p1 tl = λl : NatListcase l of nil(_) => l cons(p) => p2 sum = fix λf : NatList Natλl : NatList case l of nil(_) => 0 cons(p) => p1 + (f p2) Pratikakis (CSD) Recursive Types CS546, 2018-2019 4 / 11

Hungry functions A function that can always take more: hungry = µxnat X Such a function is a fixpoint (recursive function): f = fix (λf : Nat hungryλn : Natf) What is the type of f 1 2 3 4 5? Pratikakis (CSD) Recursive Types CS546, 2018-2019 5 / 11

Streams A stream is a function that can return an arbitrary number of values Each time it consumes a unit, returns a new value Stream = µxunit {Nat, X} We can use it like an infinite list Next item hd = λs : Stream(s ())1 Rest of stream tl = λs : Stream(s ())2 The stream of all natural numbers: fix (λf : Nat Streamλn : Natλ_ : Unit {n, f(succ n)})0 Pratikakis (CSD) Recursive Types CS546, 2018-2019 6 / 11

Objects Objects can also be recursive types Counter = µc {get : Nat, inc : Unit C} Unlike last time, this is a functional object: inc returns the new object Java strings are immutable Pratikakis (CSD) Recursive Types CS546, 2018-2019 7 / 11

Recursive type of fixpoint Using recursive types we can type the fixpoint operator fix T = λf : T T (λx : (µxx T)f (x x)) (λx : (µxx T)f (x x)) Without types this is the fixpoint combinator of untyped calculus Allows programs to diverge: not strongly normalizing A term that doesn t terminate can have any type T! By Curry-Howard: All propositions are proved, including false! The corresponding logic is inconsistent Pratikakis (CSD) Recursive Types CS546, 2018-2019 8 / 11

Type system Two ways to treat recursive types Depending on the relation between folded/unfolded type eg: NatList and nil : Unit, cons : {Nat, NatList} Implicit fold/unfold, the above types are equal in all contexts Transparent to the programmer More complex to write typechecker All proofs remain the same (except induction on type expressions) Explicit fold/unfold using language primitives Programmer must write fold/unfold primitives to help typechecker Easier to typecheck Requires extra proof cases for soundness: fold/unfold Pratikakis (CSD) Recursive Types CS546, 2018-2019 9 / 11

Type system (cont d) Syntax: Typing e ::= fold [T] e unfold [T] e v ::= fold [T] v T ::= X µxt [T-Fold] U = µxt Γ e : T[U/X] Γ fold [U] e : U [T-Unfold] U = µxt Γ e : U Γ unfold [U] e : T[U/X] Pratikakis (CSD) Recursive Types CS546, 2018-2019 10 / 11

Semantics unfold [S] (fold [T] v) v e e fold [T] e fold [T] e e e unfold [T] e unfold [T] e Pratikakis (CSD) Recursive Types CS546, 2018-2019 11 / 11