<<

Types and Programming Languages

Lecture 8. Recursive type

Xiaojuan Cai

[email protected]

BASICS Lab, Shanghai Jiao Tong University

Fall, 2016 List[]

I List[T] is a whose elements are lists with elements of type T.

I A list is either null or else a pair (cons cell) of an element and another list.

I Similar structures include: queues, binary trees, etc.

I We need a general mechanism with which they can be defined from simpler elements. This mechanism is called recursive types.

The system studied in this lecture is simply typed λ-calculus with recursive types. And we will only focus on NatList. Another mechanism for List[T] is type operator (Chapter 29). Outline

Recursive types

Formalities

Metatheory of recursive types Induction and coinduction Finite and infinite types Membership checking Regular trees µ-types Recursive types

I How to define the type NatList?

I It’s a variant type:

NatList =< nil : Unit, cons : {Nat, ...} >

I The ... is another list of number!

NatList =< nil : Unit, cons : {Nat, NatList} >

I Above definition will introduce divergence. Like recursive functions, we introduce an explicit recursion operator µ for types:

NatList = µX. < nil : Unit, cons : {Nat, X} > NatList operations Recall in Figure 11-13, we define lots of operations for List[T] as built-in. Here we will define them as functions.

I nil = as NatList;

I cons = λn:Nat.λl:NatList. as NatList;

I isnil = λl : NatList. case l of < nil = u > ⇒ true | < cons = p > ⇒ false I hd = λl : NatList. case l of < nil = u > ⇒ 0 | < cons = p > ⇒ p.1 I tl = λl : NatList. case l of < nil = u > ⇒ 0 | < cons = p > ⇒ p.2 NatList functions

I sumlist = fix (λs : NatList → Nat. λl : NatList. case l of < nil = u > ⇒ 0 | < cons = p > ⇒ plus p.1 (s p.2)) I mylist = cons 2 (cons 3 (cons 5 nil))

I sumlist mylist returns 10. Hungry functions

I Hungry = µA.Nat-> A

I f = fix (λf:Hungry.λn:Nat.f)

I f 0 1 2 3 4 5; Streams

I is the type of functions that can produce an arbitrary number of numbers (or other types).

I Stream = µA. Unit->{Nat,A}

I hd = λs:Stream.(s unit).1;

I tl = λs:Stream.(s unit).2;

I But how to define a stream?

I upfrom0 = fix (λf:Nat->Stream.λn:Nat.λ :Unit.{n,f (succ n)}) 0;

I hd upfrom0 returns 0.

I hd (tl (tl (tl upfrom0))) returns 3. Quiz. Define a stream that yields all the elements to be 1. ones = fix (λs:Stream.λ :Unit.{1,s}) Processes

I Processes are functions that accept a number and return a number and a new process.

I Process = µA. Nat->{Nat,A}

I p = fix (λf:Nat->Process.λacc.λn:Nat. let newacc = plus acc n in {newacc,f newacc}) 0;

I curr = λs:Process.(s 0).1

I send = λn:Nat.λs:Process.(s n).2

I curr (send 20 (send 3 (send 5 p))) returns 28. Objects

I Processes are similar to objects which interacting with data.

I Counter = µC.{get:Nat, inc:Unit->C}

I c = let create = fix (λf:{x:Nat}->Counter.λs:{x:Nat}. {get = s.x, inc = λ :Unit.f {x=succ(s.x)}) in create {x=0}

I c1 = c.inc unit; c2 = c1.inc unit; c2.get; Recursive values from recursive types

I A more surprising use of recursive types is a well-typed implementation of the fixed-point combinator. Quiz. Please rewrite the fix-point operator into simply typed λ-term with recursive type. fix = λf.(λx.f(x x))(λx.f(x x))

I fixT = λf : T → T.(λx :(µA.A → T).f (x x)) (λx :(µA.A → T).f (x x);

I Recursive types break the strong normalization property:

divergeT = λ : Unit.fixT (λx : T.x)

I This reveals the expressive power of recursive types. Untyped λ-calculus

I Moreover, we can embed the whole untyped λ-calculus into a statically typed language with recursive types.

I D = µX.X->X

I lam = λf:D->D.f as D

I ap = λf:D.λa:D.f a

I Here comes the encoding from untyped λ-calculus into a well-typed one:

kxk = x kλx.Mk = lam (λx : D.kMk) kMNk = ap kMk kNk Untyped λ-calculus with other features

I If we extend untyped λ-calculus with numbers, then the whole datatype is extended to be a variant type.

I D = µX.X>

I ap = λf:D.λa:D.case f of ⇒ divergeD unit | ⇒ f a Outline

Recursive types

Formalities

Metatheory of recursive types Induction and coinduction Finite and infinite types Subtyping Membership checking Regular trees µ-types Formalities

I There are two basic approaches to recursive types.

I The essential difference is their reponse to the question: What is the relation between the type µX.T and its one-step unfolding? For example, NatList and .

I The equi-recursive approach takes these two expressions definitionally equal – interchangeable in any context.

I The iso-recursive approach takes these two expression different, but isomorphic. Equi-recursive

I Pros:

I More intuitive; I Match with all the previous presentations. Definitions, safety theorems and even proofs remains unchanged. I Cons:

I The implementation requires some work, Since type checking can not work directly with infinite structures. (Chapter 21) I Interactions with other advanced features, such as quantification, lead to theoretical difficulties, even undecidability. Iso-recursive

I The unfolding of µX.T is using the standard notation for substitution: NatList = µX. < nil : Unit, cons : {Nat, X} > unfolds to < nil : Unit, cons : {Nat, µX. < nil : Unit, cons : {Nat, X} >} >

I We need to introduce fold[µX.T] and unfold[µX.T] explicitly into syntax. Iso-recursive: pros and cons

I Pros:

I Less work for type systems. I Easy to interact with other features. I Cons:

I Heavier: requiring programs to be decorated with fold and unfold. I Iso-recursive is quite palatable in practice. Since the fold and unfold notations can be hidden by coalescing them with other annotations.

I Each use of constructors is to build a value with implicitly include a fold; I Each use of case implicitly forces an unfold. Subtyping

I Assume Even is a subtype of Nat. What the relation between these two types? µX.Nat → (Even × X) and µX.Even → (Nat × X) Outline

Recursive types

Formalities

Metatheory of recursive types Induction and coinduction Finite and infinite types Subtyping Membership checking Regular trees µ-types Coming soon

I We will develop the theoretical foundations of typecheckers for equi-recursive types.

I We will deal with a system including both recursive types and subtyping.

I We will use coinduction to make equi-recursive types precise. Induction and coinduction

Let’s fix some universal U, which denotes “everything in the world”.

Definition. A function F ∈ P(U) → P(U) is monotone if X ⊆ Y implies F (X ) ⊆ F (Y ), where P(U) is the powerset of U.

In the following, F is always monotone, also called as generating function.

Definition. Let X be a subset of U.

I X is F -closed if F (X ) ⊆ X .

I X is F -consistent if X ⊆ F (X ).

I X is a fixed point if X = F (X ). Example

Consider the three-element universe U = {a, b, c}:

E1(∅) = {c} E1({a, b}) = {c} E1({a}) = {c} E1({a, c}) = {b, c} E1({b}) = {c} E1({b, c}) = {a, b, c} E1({c}) = {b, c} E1({a, b, c}) = {a, b, c}

I {a, b, c} is E1-closed;

I ∅, {c}, {b, c} and {a, b, c} are E1-consistent;

I {a, b, c} is a fixed point of E1.

Actually, E1 can be represented as a set of inference rules: c b c c b a Knaster-Tarski theorem

Theorem [Knaster-Tarski].

I The intersection of all F -closed sets is the least fixed point of F , denoted µF ;

I The union of all F -consistent sets is the greatest fixed point of F , denoted νF .

Example. (continued) µE1 = νE1 = {a, b, c}.

Corollary.

I Principle of induction: If X is F -closed, then µF ⊆ X ;

I Principle of coinduction: If X is F -consistent, then X ⊆ νF ; Outline

Recursive types

Formalities

Metatheory of recursive types Induction and coinduction Finite and infinite types Subtyping Membership checking Regular trees µ-types Finite and infinite types Definition. A type is a partial function T ∈ {1, 2}∗ → {→, ×, Top} satisfying:

I T (•) is defined;

I if T (π, σ) is defined then T (π) is defined;

I if T (π) =→ or T (π) = ×, then T (π, 1) and T (π, 2) are defined;

I if T (π) = Top, then T (π, 1) and T (π, 2) are undefined; An alternative definition for finite tree types

A tree type T is finite if dom(T ) is finite. The set of finite tree types can be defined more compactly by a grammar: T ::= Top | T × T | T → T

I The set of all finite tree types is the least fixed point of the generating function described by the grammar.

I The set of all tree types is the greatest fixed point of the generating function described by the grammar. Outline

Recursive types

Formalities

Metatheory of recursive types Induction and coinduction Finite and infinite types Subtyping Membership checking Regular trees µ-types Subtyping for finite types

Definition 21.3.1. Two finite tree types S and T are in the subtype relation if (S, T ) ∈ µSf , where the monotone function Sf ∈ P(Tf × Tf ) → P(Tf × Tf ) is defined by

Sf (R) = {(T , Top) | T ∈ Tf } ∪ {(S1 × S2, T1 × T2) | (S1, T1), (S2, T2) ∈ R} ∪ {(S1 → S2, T1 → T2) | (T1, S1), (S2, T2) ∈ R}.

Quiz. Give a set of inference rules that precisely capture the function above. Subtyping for infinite types

Definition 21.3.2. Two tree types S and T are in the subtype relation 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}.

Quiz.

I Check that νS is not the whole of T × T . Outline

Recursive types

Formalities

Metatheory of recursive types Induction and coinduction Finite and infinite types Subtyping Membership checking Regular trees µ-types Membership checking

How to decide, given a generating function F on some universe U and an element x ∈ U, whether or not x ∈ νF ?

I An element x may generated from some set X such that x ∈ F (X ).

I We call X the generating set for x.

I There are minimal generating set for x.

I We will furthur focus on the class of invertible functions, where each x has at most one minimal generating set. Invertible generating functions Definition. A generating function F is said to be invertible if, for all x ∈ U, the collection of sets

Gx = {X ⊆ U | x ∈ F (X )}

either is empty or contains a unique member that is a subset of all the others.

Definition. If F is invertible, the partial function supportF ∈ U * P(U) is defined as follows:

 0 0 X if X ∈ Gx and ∀X ∈ Gx .X ⊆ X supportF (x) = ↑ if Gx = ∅

Which can be lifted to sets:  S support (x) if ∀x ∈ X .support (x) ↓ support (X ) = x∈X F F F ↑ o.w. Towards an algorithm

I An element x is F -supported if supportF (x) ↓; I Otherwise, x is F -unsupported. I An F -supported element is called F -ground if supportF (x) = ∅. I An invertible function can be visualized as a support graph.

Quiz. Give the inference rules for this function E. gfpF funciton

Definition. Suppose F is an invertible generating function. Define the boolean valued function gfpF (or just gfp) as follows:

gfp(X ) = if support(X ) ↑, then false else if support(X ) ⊆ X , then true else gfp(support(X ) ∪ X ).

If gfp(x) = gfp({x}) returns true, then x ∈ νF ; If it returns false, x 6∈ νF .

The remainder of this part is devoted to proving the correctness and termination of gfp. Correctness

Lemma 21.5.7. X ⊆ F (Y ) iff supportF (X ) ↓ and supportF (X ) ⊆ Y .

Lemma 21.5.8. Suppose P is a fixed point of F . Then X ⊆ P iff supportF (X ) ↓ and supportF (X ) ⊆ P.

Theorem 21.5.9.

I If gfp(X ) = true, then X ⊆ νF ;

I If gfp(X ) = false, then X 6⊆ νF ; Termination

Given an invertible generating function F , and an element x ∈ U:  ∅ if support(x) ↑ pred(x) = I support(x) if support(x) ↓ S I pred(X ) = x∈X pred(x). S n I reachable(X ) = n≥0 pred (X ). I F is finite state if reachable(x) is finite for any x ∈ U. Theorem 21.5.12. if reachable(X ) is finite, then gfp(X ) terminates for any finite X ⊆ U. Coming soon

I We have algorithms for checking membership of νF for some invertible and finite state generating function F .

I Now we will consider our subtyping function S, which is invertible but may not be finite state.

I We will show that the algorithms always terminate for regular type trees. Subtrees

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, i.e., if T has finitely many distinct subtrees. The set of regular tree types is written Tr . Examples

I Every finite tree type is regular, for example, T = Top->(Top×Top);

I Some infinite tree types are regular, for example, T = Top × (Top × (Top × ...));

I Some infinite tree types are irregular, for example, T=B×(A×(B×(A×(A×(B×(A×(A×(A×(B×...)))))))))

Proposition. The restriction Sr of generating function S on Tr is finite state. Outline

Recursive types

Formalities

Metatheory of recursive types Induction and coinduction Finite and infinite types Subtyping Membership checking Regular trees µ-types µ-types

The set of raw µ-types can be defined by this grammar:

T ::= X | Top | T × T | T → T | µX.T

Not all the raw µ-types can be reasonably interpreted as representations of tree types, for example µX .X . Definition. A raw µ-type T is contractive if, for any subexpression of T of the form µX.µX1.µX2...µXn.S the body S is not X . µ-types are contractive raw µ-types. The set of µ-types are denoted as Tm. We write µ-height(T ) for number of µ-bindings at the front of T . From µ-types to tree types Definition. The function treeof , mapping closed µ-types to tree types, is defined inductively as follows:

treeof(Top)(•) = Top treeof(T1 → T2)(•) =→ treeof(T1 → T2)(i, π) = treeof(Ti)(π) treeof(T1 × T2)(•) = × treeof(T1 × T2)(i, π) = treeof(Ti)(π) treeof(µX.T)(π) = treeof([X → µX.T]T)(π)

Example. What is treeof (µX.((X × Top) → X)).

I Every recursive use of treeof on the right-hand side reduces the lexicographic size of the pair (|π|, µ-height(T ));

I All recursive calls preserve contractiveness and closure of the argument types. Subtyping for µ-types

S <:[X → µX.T]T [X → µX.S]S <: T S <: µX.T µX.S <: T

We modify the subtyping generation function from S to Sm. Definition 21.8.4. Two finite tree types S and T are in the subtype relation if (S, T ) ∈ νSm, where the monotone function Sm ∈ P(Tm × Tm) → P(Tm × Tm) is defined by

Sm(R) = {(T , Top) | T ∈ T } ∪ {(S1 × S2, T1 × T2) | (S1, T1), (S2, T2) ∈ R} ∪ {(S1 → S2, T1 → T2) | (T1, S1), (S2, T2) ∈ R} ∪ {(S, µX .T ) | (S, [X → µX .T ]T ) ∈ R} ∪ {(µX .S, T ) | ([X → µX .S]S, T ) ∈ R, T 6= Top, and T 6= µY .T1}. Support function

The generating function Sm is invertible because the corresponding support function is well-defined:

 ∅ if T = Top   {(S1, T1), (S2, T2)} if S = S1 × S2,   T = T1 × T2   {(T1, S1), (S2, T2)} if S = S1 → S2, supportSm (S, T ) =  T = T1 → T2   {(S, [X → µX .T1]T1)} if T = µX .T1   {([X → µX .S]S, T )} if S = µX .S  T 6= Top, T 6= µX .T1 Correspondence

Theorem. 21.8.7. Let (S, T ) ∈ Tm × Tm. Then (S, T ) ∈ νSm iff treeof (S, T ) ∈ νS. Subtyping iso-recursive types

The most common definition of iso-recursive subtyping is the Amber rule, after Cardelli’s Amber language (1986):

Σ, X <: Y ` S <: T S-Amber Σ ` µX .S <: µY .T

Quiz. Find recursive types S and T such that S <: T using the equi-recursive definition, but not using the Amber rule. Answer: µX.Nat × (Nat × X) and µX.Nat × X Conclusion

I Recursive types can be defined by using µ operator.

I Recursive types are quite expressive: with it we can embed the whole untyped λ-calculus into well-typed λ-calculus.

I Iso-recursive is heavy weight with notations, but easy to implement.

I Equi-recursive is very natural but really needs lots of work to build the .

I Coinduction is a standard method to handle infinite structures.

I Subtyping relation of equi-recursive types needs to compute the greatest fixed point of a subtyping generating function. Homework

I 20.1.1 (without testing part), 20.1.2, 21.1.7, 21.3.4, 21.11.1.