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

Similar documents
Types and Programming Languages. Lecture 8. Recursive type

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

Lecture 14: Recursive Types

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

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

Negations in Refinement Type Systems

Subsumption. Principle of safe substitution

Lecture Note: Types. 1 Introduction 2. 2 Simple Types 3. 3 Type Soundness 6. 4 Recursive Types Subtyping 17

Lambda Calculus and Type Inference

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

INCREMENTAL SOFTWARE CONSTRUCTION WITH REFINEMENT DIAGRAMS

TOPOLOGY, DR. BLOCK, FALL 2015, NOTES, PART 3.

THREE LECTURES ON BASIC TOPOLOGY. 1. Basic notions.

ROUGH MEMBERSHIP FUNCTIONS: A TOOL FOR REASONING WITH UNCERTAINTY

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Formal Systems and their Applications

Type Systems COMP 311 Rice University Houston, Texas

A fuzzy subset of a set A is any mapping f : A [0, 1], where [0, 1] is the real unit closed interval. the degree of membership of x to f

Topology notes. Basic Definitions and Properties.

COMPUTABILITY THEORY AND RECURSIVELY ENUMERABLE SETS

9.5 Equivalence Relations

T. Background material: Topology

1.3. Conditional expressions To express case distinctions like

Dependent Object Types - A foundation for Scala s type system

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

CSC Discrete Math I, Spring Sets

The University of Nottingham

LOGIC AND DISCRETE MATHEMATICS

Chapter 22: Type Reconstruction (Type Inference)

Featherweight Java (FJ)

CIS 500 Software Foundations Fall October 2

MATHEMATICS 191, FALL 2004 MATHEMATICAL PROBABILITY Outline #1 (Countability and Uncountability)

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

2. Functions, sets, countability and uncountability. Let A, B be sets (often, in this module, subsets of R).

Recursive Types and Subtyping

EXTENSIONS OF FIRST ORDER LOGIC

Discrete Mathematics Lecture 4. Harper Langston New York University

CSE 20 DISCRETE MATH. Fall

STABILITY AND PARADOX IN ALGORITHMIC LOGIC

Rational subsets in HNN-extensions

1.7 The Heine-Borel Covering Theorem; open sets, compact sets

Lecture Notes on Program Equivalence

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

1 Problem Description

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Sections p.

Natural Numbers. We will use natural numbers to illustrate several ideas that will apply to Haskell data types in general.

Inductive Definitions, continued

JOURNAL OF OBJECT TECHNOLOGY

Introduction to Homotopy Type Theory

Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Sets Fall / 31

Type Inference with Inequalities

Real Analysis, 2nd Edition, G.B.Folland

Elementary Recursive Function Theory

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Lambda Calculus and Type Inference

To illustrate what is intended the following are three write ups by students. Diagonalization

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Lecture 17: Continuous Functions

Propositional Logic. Part I

Decompositions of infinite graphs: Part II Circuit decompositions

Functional Logic Programming: From Theory to Curry

1 Introduction. 3 Syntax

Chapter 6. Curves and Surfaces. 6.1 Graphs as Surfaces

Coalgebraic Semantics in Logic Programming

A Modality for Recursion

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

Recursive Types. Chapter 7

Contents. Chapter 1 SPECIFYING SYNTAX 1

Generic Programming With Dependent Types: II

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Mathematics for Computer Scientists 2 (G52MC2)


Chapter 1. Preliminaries

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

CS4215 Programming Language Implementation. Martin Henz

Consistency and Set Intersection

Part III. Chapter 15: Subtyping

,, 1{48 () c Kluwer Academic Publishers, Boston. Manufactured in The Netherlands. Optimal Representations of Polymorphic Types with Subtyping * ALEXAN

Programming Languages Fall 2014

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

Infinity and Uncountability. Countable Countably infinite. Enumeration

Symmetry in Type Theory

Lecture 1. 1 Notation

Week 9-10: Connectivity

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

CSE 20 DISCRETE MATH. Winter

(Refer Slide Time: 0:19)

The Untyped Lambda Calculus

SAT-CNF Is N P-complete

Module 11. Directed Graphs. Contents

Lecture 13: Subtyping

The Untyped Lambda Calculus

Vertex Cover is Fixed-Parameter Tractable

Part VI. Imperative Functional Programming

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

Point-Set Topology 1. TOPOLOGICAL SPACES AND CONTINUOUS FUNCTIONS

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

Recursion and Induction

Transcription:

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

Review of Chapter 20

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

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

Hungry Functions: accepting any number of numeric arguments and always return a new function that is hungry for more Hungry = µa. Nat A

Streams: consuming an arbitrary number of unit values, each time return- ing a pair of a number and a new stream Stream = µa. Unit {Nat, A}; (Process = µa. Nat {Nat, A})

Objects Counter = µc. {get:nat, inc:unit C, dec:unit C}

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

Untyped Lambda-Calculus: we can embed the whole untyped lambda-calculus in a well-typed way into a statically typed language with recursive types. We can extend it to include features like numbers. D= µx. <nat:nat, fn:x X>

Relation between µ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.

Subtyping and Recursive Types Can we deduce µx. Nat (Even X) <: µx. Even (Nat X) from Even <: Nat?

21.1 Induction and Coinduction

Universal Set U Type: a subset of U Inductive/Coinductive Definition U: everything in the world

Generating Function Definition: A function F P(U) P(U) is monotone if X Y implies F(X) F(Y). Definition: Let X be a subset of U. X is F-closed if F(X) X. X is F-consistent if X F(X). X is a fixed point of F if F(X) = X.

Exercise: Consider the following generating function on the three-element universe U={a, b, c}: E1( ) = {c} E1({a}) = {c} E1({b}) = {c} E1({c}) = {b, c} E1({a,b}) = {c} E1({a, c}) = {b, c} E1({b, c}) = {a, b, c} E1({a, b, c}) = {a, b, c} Q: Which subset is E1-closed, E1-consistent?

Knaster-Tarski Theorem (1955) Theorem The intersection of all F-closed sets is the least fixed point of F. The union of all F-consistent sets is the greatest fixed point of F. Definition: The least fixed point of F is written µf. The greatest fixed point of F is written F.

Exercise: Consider the following generating function on the three-element universe U={a, b, c}: E1( ) = {c} E1({a}) = {c} E1({b}) = {c} E1({c}) = {b, c} E1({a,b}) = {c} E1({a, c}) = {b, c} E1({b, c}) = {a, b, c} E1({a, b, c}) = {a, b, c} Q: What are µe1 and E1?

Exercise: Suppose a generating function E2 on the universe {a, b, c} is defined by the following inference rules: Q: Write out the set of pairs in the relation E2 explicitly, as we did for E1 above. List all the E2-closed and E2-consistent sets. What are µe2 and E2?

Principles of Induction/Coinduction Corollary: Principle of induction: If X is F-closed, then µf X. Principle of coinduction: If X is F-consistent, then X F. The induction principle says that any property whose characteristic set is closed under F is true of all the elements of the inductively defined set µf. The coinduction principle, gives us a method for establishing that an element x is in the coinductively defined set F.

21.2 Finite and Infinite Types To instantiate the general definitions of greatest fixed points and the coinductive proof method with the specifics of subtyping.

Tree Type Definition: A tree type (or, simply, a tree) is a partial function T {1,2} {,,Top} satisfying the following constraints: T( ) is defined; if T(π, ) is defined then T(π) is defined; if T(π) = or T(π) = then T(π,1) and T(π,2) are defined; if T(π) = Top then T(π,1) and T(π,2) are undefined. Note: T(.) = Top

Definition: A tree type T is finite if dom(t) is finite. The set of all tree types is written T; the subset of all finite tree types is written T f. Exercise: Following the ideas in the previous paragraph, suggest a universe U and a generating function F P(U) P(U) such that the set of finite tree types T f is the least fixed point of F and the set of all tree types T is its greatest fixed point.

21.3 Subtyping

Finite Subtyping Definition: Two finite tree types S and T are in the subtype relation ( S is a subtype of T ) if (S,T) µs f, where the monotone function S f P(T f T f ) P(T f T f ) is defined by S f (R) = {(T,Top) T T f } {(S1 S2,T1 T2) (S1,T1), (S2,T2) R} {(S1 S2,T1 T2) (T1,S1), (S2,T2) R}.

Inference Rules T <: Top S1 <: T1 S2 <: T2 ------------------ S1 S2 <: T1 T2 T1 <: S1 S2 <: T2 ------------------- S1 S2 <: T1 T2

Infinite Subtyping Definition: Two (finite or infinite) tree types S and T are in the subtype relation ( S is a subtype of T ) if (S,T) S, where the monotone function S P(T T ) P(T T ) is defined by S(R) = {(T,Top) T T } {(S1 S2,T1 T2) (S1,T1), (S2,T2) R} {(S1 S2,T1 T2) (T1,S1), (S2,T2) R}.

Transitivity Definition: A relation R U U is transitive if R is closed under the monotone function TR(R) = {(x,y) z U. (x,z), (z,y) R}, i.e., if TR(R) R. Lemma: Let F P(U U) P(U U) be a monotone function. If TR(F(R)) F(TR(R)) for any R U U, then F is transitive. Theorem: S is transitive.

A Digression on Transitivity The possibility of giving a declarative presentation with the rule of transitivity turns out to be a consequence of a trick that can be played with inductive, but not coinductive, definitions. The union of two sets of rules, when applied inductively, generates the least relation that is closed under both sets of rules separately. Adding transitivity to the rules generating a coinductively defined relation always gives us a degenerate relation.

21.5 Membership Checking Given a generating function F on some universe U and an element x U, check whether or not x falls in F.

Invertible Generating Function Definition: A generating function F is said to be invertible if, for all x U, the collection of sets G x ={X U x F(X)} either is empty or contains a unique member that is a subset of all the others. We will consider invertible generating function in the rest of this chapter.

F-Supported/F-Ground When F is invertible, we define: Definition: An element x is F-supported if support F (x) ; otherwise, x is F- unsupported. An F-supported element is called F-ground if support F (x) =.

Support Graph A Example of the support graph of E function on {a,b,c,d,e,f,g,h,i} x is in the greatest fixed point iff no unsupported element is reachable from x in the support graph.

Greatest Fixed Point Definition: Suppose F is an invertible generating function. Define the boolean-valued function gfp F (or just gfp) as follows: Theorem (Sound): 1. If gfp F (X) = true, then X F. 2. 2. If gfp F (X) = false, then X / F. Theorem (Terminate): If reachable F (X) is finite, then gfp F (X) is defined. Consequently, if F is finite state, then gfp F (X) terminates for any finite X U.

More Efficient Algorithms

Inefficiency Recomputation of support support(a) is recomputed four times!

A More Efficient Algorithm Definition: Suppose F is an invertible generating function. Define the function gfp a as follows Example: Tail-recursion

Variation 1 Definition: A small variation on gfp s has the algorithm pick just one element at a time from X and expand its support. The new algorithm is called gfp s

Variation 2 Definition: Given an invertible generating function F, define the function gfp t as follows:

Regular Trees If we restrict ourselves to regular types, then the sets of reachable states will be guaranteed to remain finite and the subtype checking algorithm will always terminate.

Regular Trees Definition: A tree type S is a subtree of a tree type T if S =. T(π, ) for some π. Definition: A tree type T T is regular if subtrees(t) is finite. Examples: Every finite tree type is regular. T = Top x (Top x (Top x )) is regular. T = B x (A x (B x (A x (A x (B x (A x (A x (A x (B ) is irregular.

Proposition: The restriction of the generating function S to regular tree types is finite state. Proof: We need to show that for any pair (S,T) of regular tree types, the set reachable(s,t) is finite. Since reachable (S,T) subtrees(s) subtrees(t); the latter is finite as S and T are regular.

Establishes the correspondence between subtyping on µ-expressions and the subtyping on tree types

µ-types: Definition: Let X range over a fixed countable set {X1,X2,...} of type variables. The set of raw µ-types is the set of expressions defined by the following grammar: T m Definition: A raw µ-type T is contractive (and called µ-types) if, for any subexpression of T of the form µx.µx1...µxn.s, the body S is not X.

Finite Notation for Infinite Tree Types Definition: The function treeof, mapping closed µ- types to tree types, is defined inductively as follows:

Subtyping Correspondence: µ-types and Tree Types Definition: Two µ-types S and T are said to be in the subtype relation if (S,T) S m, where the monotone function S m P(T m T m ) P(T m T m ) is defined by: Theorem: Let (S,T) T m T m. Then (S,T) S m iff (treeof S, treesof T) S.

Exercise: What is the support for S m?

Subtyping Algorithm for µ-types Instantiating gfp t for subtyping relation on µ-types. Terminate?

An Digressed (Exponential) Subtyping Algorithm

Summary We study the theoretical foundation of type checkers (subtyping) for equi-recursive types. Induction/coinduction & proof principles Finite and Infinite Types/Subtyping Membership checking algorithm

Homework