lesson 9 recursive types
play

Lesson 9 Recursive Types 2/19, 21 Chapters 20, 21 Recursive type - PDF document

Lesson 9: Recursive Types Lesson 9 Recursive Types 2/19, 21 Chapters 20, 21 Recursive type Recursive type terms are infinite, but regular For finite terms, use induction based on least fixed points For infinite terms/trees, we


  1. Lesson 9: Recursive Types Lesson 9 Recursive Types 2/19, 21 Chapters 20, 21 Recursive type • Recursive type terms are infinite, but regular • For finite terms, use induction based on least fixed points • For infinite terms/trees, we have to use coinduction, based on greatest fixed points. • For least fixed points, recursively explore all subterms • For greatest fixed points, recursively explore the support set (and hope that it is finite) Lesson 9: Recursive Types 2 1

  2. Lesson 9: Recursive Types Greatest fixed point algorithm How to check for membership in the gfp (of an invertible generating function F)? gfp(X) = if support(X) ↑ then false else if support(X) Õ X then true else gfp(support(X) » X) Correctness: Thm: 1. If gfp(X) = true then X Õ n F 2. If gfp(X) = false then X À n F Proof: induction on recursive computation of gfp(X) Lesson 9: Recursive Types 3 Gfp algorithm: termination pred(x) = ∅ if support(x) ↑ = support(X) if support(X) Ø pred(X) = » x Œ X pred(x) reachable(X) = » n ≥ 0 pred n (x) reachable(x) = reachable({x}) F is finite state if reachable(x) is finite for each x Thm: If reachable(X) is finite, then gfp(X) is defined. If F is finite state, gfp(X) terminates for any finite X. Lesson 9: Recursive Types 4 2

  3. Lesson 9: Recursive Types More efficient gfp: gfp a Avoid repeated addition of elements by keeping track of all elements examined before in a new argument A. gfp a (A,X) = if support(X) ↑ then false else if X = ∅ then true else gfp a (A » X, support(X)\(A » X)) This version considers only new elements added by support function. x Œ n F iff gfp a ( ∅ ,{x}) Lesson 9: Recursive Types 5 More efficient gfp: gfp t Threaded version of gfp, adding one element at a time and producing visited set as the result (assume support(x) finite): gfp t (A,x) = if x Œ A then A else if support({x}) ↑ then fail else fold gfp t (A » {x}) (support(x)) where fold is a function like list fold but operating on sets: fold f X ∅ = X fold f X {y 1 , y 2 , ..., y n } = fold f (f(X,y 1 )) {y 2 , ..., y n } Correctness: x Œ n F iff gfp t ( ∅ ,x) Ø Lesson 9: Recursive Types 6 3

  4. Lesson 9: Recursive Types Regular Trees Def : S Œ T is a subtree of T Œ T if S = ls . T( p , s ) for some path p Œ dom(T). subtrees(T) is the set of subtrees of T. Def : T Œ T is regular if subtrees(T) is finite. T r is the set of regular trees. Lesson 9: Recursive Types 7 Subtype relation The subtyping relation on T is defined as the greatest fixed point of the relation generator function S(R) = {(T, Top) | T Œ T } » {(S1 ¥ S2, T1 ¥ T2) | (S1,T1), (S2,T2) Œ R} » {(S1 Æ S2, T1 Æ T2) | (T1,S1), (S2,T2) Œ R} S r is the restriction of S to T r (regular trees). Prop: S r is finite state. Lesson 9: Recursive Types 8 4

  5. Lesson 9: Recursive Types m -Types Def : T m-raw , the set of raw m -types, is defined by the grammar: T ::= X | Top | T ¥ T | T Æ T | m X.T This contains useless terms like m X.X that we should exclude. Def : T Œ T m-raw is contractive if for any subtree of Tof the form m X. m X 1 . ... m X n . S, S is not X. (I.e. there is always an occurrence of ¥ or Æ between a binder m X and an applied occurrence of X. T m denotes the set of contractive m -types. Can define a function treeof : T m Æ T mapping m -types to tree types. Lesson 9: Recursive Types 9 Subtype relation for m -Types The generating function S m for the subtyping relation on T m is given by S m (R) = {(S, Top) | S Œ T m } » {(S 1 ¥ S 2 , T 1 ¥ T 2 ) | (S 1 ,T 1 ), (S 2 ,T 2 ) Œ R} » {(S 1 Æ S 2 , T 1 Æ T 2 ) | (T 1 ,S 1 ), (S 2 ,T 2 ) Œ R} » {(S, m X.T) | (S, [X -> m X.T]T) Œ R} » {( m X.S, T) | ([X -> m X.S]S, T) Œ R, T ≠ Top, T ≠ m Y.T 1 } Lesson 9: Recursive Types 10 5

  6. Lesson 9: Recursive Types Subtype relation for m -Types S m is invertible with support function support(S,T) = ∅ if T = Top = {(S 1 ,T 1 ), (S 2 ,T 2 )} if S = S 1 ¥ S 2 and T = T 1 ¥ T 2 = {(T 1 ,S 1 ), (S 2 ,T 2 )} if S = S 1 Æ S 2 and T = T 1 Æ T 2 = {(S, [X -> m X.T 1 ]T 1 )} if T = m X.T 1 = {([X -> m X.S 1 ]S 1 , T)} if S = m X.S 1 , T ≠ Top, T ≠ m Y.T 1 = ↑ Thm: (S,T) Œ n S m iff treeof(S,T) Œ n S Lesson 9: Recursive Types 11 Subtyping algorithm Specialize gfp t to S m . subtype (S,T) = if (S,T) Œ A then A else let A 0 = A » {(S,T)} in if T = Top then A 0 else if S = S 1 ¥ S 2 and T = T 1 ¥ T 2 then let A 1 = subtype(A 0 ,S 1 ,T 1 ) in subtype(A 1 ,S 2 ,T 2 ) else if S = S 1 Æ S 2 and T = T 1 Æ T 2 then let A 1 = subtype(A 0 ,T 1 ,S 1 ) in subtype(A 1 ,S 2 ,T 2 ) else if T = m X.T 1 then subtype(A 0 ,S,[X -> m X.T 1 ]T 1 ) else if S = m X.S 1 then subtype(A 0 ,[X -> m X.S 1 ]S 1 ) else fail Lesson 9: Recursive Types 12 6

  7. Lesson 9: Recursive Types Subtyping Iso-recursive types The Amber rule: S , X <: Y |- S <: T (S-Amber) S |- m X.S <: m Y.T X <: Y ΠS (S-Assumption) S |- X <: Y Lesson 9: Recursive Types 13 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend