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

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

Lecture 14: Recursive Types

Types and Programming Languages. Lecture 8. Recursive type

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

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

Derivation for the Necessity of Recursive Types and its Basic Usage

Derivation for the Necessity of Recursive Types and its Basic Usage

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

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

Pattern Matching and Abstract Data Types

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

Recursive Types and Subtyping

The University of Nottingham

Programming Languages Lecture 14: Sum, Product, Recursive Types

Graphical Untyped Lambda Calculus Interactive Interpreter

Flat (Draft) Pasqualino Titto Assini 27 th of May 2016

Formal Systems and their Applications

Recursive Types and Subtyping

Lecture Notes on Data Representation

Exercise 1 ( = 22 points)

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

CS 320: Concepts of Programming Languages

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

Exercise 1 ( = 24 points)

The Untyped Lambda Calculus

CSE-321 Programming Languages 2011 Final

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

Exercise 1 ( = 18 points)

CIS 500 Software Foundations Fall December 6

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation

Programming Languages Lecture 15: Recursive Types & Subtyping

Induction in Coq. Nate Foster Spring 2018

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

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

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

Chapter 5: The Untyped Lambda Calculus

Introduction to Functional Programming

Chapter 22: Type Reconstruction (Type Inference)

Chapter 5: The Untyped Lambda Calculus

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

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

Exercise 1 (2+2+2 points)

3 Pairs and Lists. 3.1 Formal vs. Informal Proofs

Lesson 11 Universal Types. Universal Types and System F

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

The Untyped Lambda Calculus

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

CIS 500 Software Foundations Fall December 4

The Untyped Lambda Calculus

Part III. Chapter 15: Subtyping

Introduction to Co-Induction in Coq

CIS 500 Software Foundations Fall October 2

Programming Languages Fall 2014

Lambda Calculus and Type Inference

Recursive Types. Chapter 7

Exam for 2G1512 DatalogiII, Apr 12th

(Advanced) topics in Programming Languages

Programming Language Concepts, CS2104 Lecture 7

Programming in Omega Part 1. Tim Sheard Portland State University

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

Graph reduction. Simple graph reduction with visualisation

Inductive Definitions, continued

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

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

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.

Lambda Calculus and Type Inference

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

Theorem Proving Principles, Techniques, Applications Recursion

Code reuse through polymorphic variants

CSCI-GA Scripting Languages

CSE-321 Programming Languages 2010 Final

Programming Language Concepts: Lecture 19

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

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Introduction to lambda calculus Part 3

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

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

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

Lambda Calculi With Polymorphism

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Introduction to the Lambda Calculus

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

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

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

λ calculus is inconsistent

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

Homework 3 COSE212, Fall 2018

Local Specialization in Open-Axiom

Autosubst Manual. May 20, 2016

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

Type Checking and Type Inference

COP4020 Spring 2011 Midterm Exam

A Simple Supercompiler Formally Verified in Coq

Contents. Chapter 1 SPECIFYING SYNTAX 1

Introduction to Functional Programming in Haskell 1 / 56

Part III Chapter 15: Subtyping

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

Transcription:

Review

Preliminary: syntax, operational semantics Untyped lambda calculus Simply typed lambda calculus Simple extension: tuples, sums, lists Subtyping Typing ADT Universal type: system F Java Recursive Types

Chapter 20: Recursive Types Examples Formalities Subtyping

Review: Lists Defined in Chapter 11 List T describes finite-length lists whose elements are drawn from T.

Examples of Recursive Types

Lists NatList = <nil:unit, cons:{nat, NatList}> Infinite Tree

NatList = µx. <nil:unit, cons:{nat,x}> This means that let NatList be the infinite type satisfying the equation: X = <nil:unit, cons:{nat, X}>.

Defining functions over lists nil = <nil=unit> as NatList isnil = l:natlist. case l of <nil=u> true hd = l:natlist. case l of <nil=u> 0 <cons=p> p.1 tl = l:natlist. case l of <nil=u> l <cons=p> p.2 sumlist = fix ( s:natlist Nat. l:natlist. if isnil l then 0 else plus (hd l) (s (tl l)))

Hungry Functions Hungry Functions: accepting any number of numeric arguments and always return a new function that is hungry for more Hungry = µa. Nat A f : Hungry f = fix ( f: Nat Hungry. n:nat. f) f 0 1 2 3 4 5 : Hugary

Streams Streams: consuming an arbitrary number of unit values, each time returning a pair of a number and a new stream Stream = µa. Unit {Nat, A}; upfrom0 : Stream upfrom0 = fix ( f: Nat Stream. n:nat. _:Unit. {n,f (succ n)}) 0; hd : Stream Nat (Process = µa. Nat {Nat, A})

Objects Objects Counter = µc. { get : Nat, inc : Unit C, dec : Unit C } c : Counter c = let create = fix ( f: {x:nat} Counter. s: {x:nat}. in create {x=0}; ((c.inc unit).inc unit).get è 2

Recursive Values from Recursive Types Recursive Values from Recursive Types F = µa.a T (Breaking the strong normalizing property: diverge = becomes typable)

Untyped Lambda Calculus Untyped Lambda-Calculus: we can embed the whole untyped lambda-calculus - in a well-typed way - into a statically typed language with recursive types. lam : D lam = f:d D. f as D; ap : D ap = f:d. a:d. f a;

Formalities What is the relation between the type µx.t and its one-step unfolding?

Two Approaches The equi-recursive approach takes these two type expressions as definitionally equal interchangeable in all contexts since they stand for the same infinite tree. more intuitive, but places stronger demands on the typechecker. 2. The iso-recursive approach takes a recursive type and its unfolding as different, but isomorphic. Notationally heavier, requiring programs to be decorated with fold and unfold instructions wherever recursive types are used.

The Iso-Recursive Approach Unfolding of type µx.t Witness functions (for isomorphism)

Iso-recursive types (λµ)

Lists (Revisited) NatList = µx. <nil:unit, cons:{nat,x}> 1-step unfolding of NatList: NLBody = <nil:unit, cons:{nat, NatList}> Definitions of functions on NatList Constructors nil = fold [NatList] (<nil=unit> as NLBody) Cons = n:nat. l:natlist. Destructors hd = l:natlist. fold [NatList] <cons={n,l}> as NLBody case unfold [NatList] l of [ Exercises: Define tl, sinil ]

Subtyping

Can we deduce µx. Nat (Even X) <: µx. Even (Nat X) from Even <: Nat?

Homework Problem (Chapter 20) Natural number can be defined recursively by Nat = µx. <zero: Nil, succ: X> Define the following functions in terms of fold and unfold. (1) szero n: check whether a natural number n is. zero or not. (2) add1 n: increase a natural number n by 1. (3) plus m n:add two natural numbers.