编程语言的设计原理 recursive types · •try to determine the following subtype relations...
TRANSCRIPT
![Page 1: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/1.jpg)
Design Principles of Programming Languages
Metatheory of Recursive Types
Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong
Peking University, Spring Term, 2016
![Page 2: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/2.jpg)
提醒:课程项目
• 6月8日课程项目报告• 每组报告20分钟,提问5分钟
• 组队的同学请介绍每名成员的贡献
• 6月7日课程书面报告和代码提交• 邮件发给吴迪、杨至轩和我
2
![Page 3: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/3.jpg)
Review: Iso-recursive Types
• What are the types of the following terms?• Hungry = 𝜇A. Nat→A;
• h = fix (𝜆f: Nat→ Hungry. 𝜆n:Nat. f)• h ?
• fold[Hungry] h ?
• unfold[Hungry] (h 1) ?
3
![Page 4: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/4.jpg)
Review: Iso-recursive Types
4
![Page 5: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/5.jpg)
Equi-recursive approach
• Do not use explicit fold/unfold
• If type A can be constructed from type B by applying only fold and/or unfold, A and B are equal
• Example: the following three types are equal• Hungry
• Nat→Hungry
• Nat→Nat→Hungry
5
![Page 6: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/6.jpg)
Solution
• Alternative 1: Deduce all equal types for a term• possibly infinite number of types
6
![Page 7: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/7.jpg)
Solution
• Alternative 1: Deduce all equal types for a term• possibly infinite number of types
• Alternative 2: use algorithms to determine the subtyping relations• An algorithm to determine if type A is a subtype of type
B
• We do not need an algorithm to determine the equality of two types• It can be deduced from subtyping relations
• 𝐴 <:𝐵 ∧ 𝐵 <: 𝐴 → 𝐴 = 𝐵
• It will never be used
7
![Page 8: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/8.jpg)
Iso-recursive Subtyping
8
Even <:Nat
![Page 9: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/9.jpg)
Without the last rule:A Derivation Tree
9
𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝐸𝑣𝑒𝑛 <: 𝐸𝑣𝑒𝑛 → (𝑁𝑎𝑡 × 𝑁𝑎𝑡)
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡 𝐸𝑣𝑒𝑛 × 𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡 × 𝑁𝑎𝑡
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡 𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡
![Page 10: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/10.jpg)
With the last rule:A Derivation Graph
10
𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝑋 <: 𝐸𝑣𝑒𝑛 → (𝑁𝑎𝑡 × 𝑌)
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡 𝐸𝑣𝑒𝑛 × 𝑋 <:𝑁𝑎𝑡 × 𝑌
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡
𝑋 <: 𝑌𝜇𝑋.𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝑋 <: 𝜇𝑌. 𝐸𝑣𝑒𝑛 → (𝑁𝑎𝑡 × 𝑌)
![Page 11: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/11.jpg)
The premise function
• 𝑝𝑟𝑒𝑚𝑖𝑠𝑒 𝑆 <: 𝑇 =∅ 𝑖𝑓 𝑇 = 𝑇𝑜𝑝 ∨ (𝑆 = 𝐸𝑣𝑒𝑛 ∧ 𝑇 = 𝑁𝑎𝑡)
{𝑆1 <: 𝑇1, 𝑆2 <: 𝑇2} 𝑖𝑓 𝑆 = 𝑆1 × 𝑆2 ∧ 𝑇 = 𝑇1 × 𝑇2𝑇1 <: 𝑆1, 𝑆2 <: 𝑇2 𝑖𝑓 𝑆 = 𝑆1 → 𝑆2 ∧ 𝑇 = 𝑇1 → 𝑇2
{𝑆1 <: 𝑇1} 𝑖𝑓 𝑆 = 𝜇𝑋. 𝑆1 ∧ 𝑇 = 𝜇𝑋. 𝑇1↑ 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
• 𝑝𝑟𝑒𝑚𝑖𝑠𝑒 𝑋 =
ቊ𝑥∈𝑋ڂ 𝑝𝑟𝑒𝑚𝑖𝑠𝑒(𝑥) 𝑖𝑓 ∀𝑥 ∈ 𝑋. 𝑝𝑟𝑒𝑚𝑖𝑠𝑒 𝑥 ↓
↑ 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
11
![Page 12: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/12.jpg)
The derivation function
• 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑜𝑛 𝑆 <: 𝑇 =
ቊ{𝑆 <: 𝑇, 𝑋 <: 𝑌} 𝑖𝑓 𝑆 = 𝜇𝑋. 𝑆1 ∧ 𝑇 = 𝜇𝑌. 𝑇1
{𝑆 <: 𝑇} 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
• 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑜𝑛 𝑋 = 𝑥∈𝑋ڂ 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑜𝑛(𝑥)
12
![Page 13: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/13.jpg)
The subtyping algorithm
• gfp(X)=if premise(X)↑ then falseelse premise(X)⊆derivation(X) then trueelse gfp(premise(X)∪X)
• isSubtype(S<:T)=gfp({S<:T})
13
![Page 14: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/14.jpg)
Termination• X grows larger in every iteration
• Function premise() only produce subexpressions• subexpression: a sub tree in the AST
• There are finite number of subexpressions for a type
14
𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝑋 <: 𝐸𝑣𝑒𝑛 → (𝑁𝑎𝑡 × 𝑌)
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡 𝐸𝑣𝑒𝑛 × 𝑋 <:𝑁𝑎𝑡 × 𝑌
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡
𝑋 <: 𝑌𝜇𝑋.𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝑋 <: 𝜇𝑌. 𝐸𝑣𝑒𝑛 → (𝑁𝑎𝑡 × 𝑌)
![Page 15: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/15.jpg)
Exercises
• Try to determine the following subtype relations using the algorithm• 𝜇𝑋. 𝜇𝑌. 𝑋 × 𝑌 <: 𝜇𝐴. 𝜇𝐵. 𝐴 × 𝐵
• 𝜇𝑋. 𝑋 → 𝑁𝑎𝑡 <: 𝜇𝑌. 𝑌 → 𝑁𝑎𝑡
• 𝜇𝑋.𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝑋 <: 𝐸𝑣𝑒𝑛 → ൫𝑁𝑎𝑡 ×
15
![Page 16: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/16.jpg)
Exercises
• Try to determine the following subtype relationsusing the algorithm• 𝜇𝑋. 𝜇𝑌. 𝑋 × 𝑌 <: 𝜇𝐴. 𝜇𝐵. 𝐴 × 𝐵
• true
• 𝜇𝑋. 𝑋 → 𝑁𝑎𝑡 <: 𝜇𝑌. 𝑌 → 𝑁𝑎𝑡• false• the current algorithm allows only to unfold once
• 𝜇𝑋.𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝑋 <: 𝐸𝑣𝑒𝑛 → ൫𝑁𝑎𝑡 ×
16
![Page 17: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/17.jpg)
Changing the typing rule
17
Σ, 𝜇𝑋. 𝑆 <: 𝑇 ⊢ 𝑋 → 𝜇𝑋. 𝑆 𝑆 <: 𝑇
Σ ⊢ 𝜇𝑋. 𝑆 <: 𝑇
Σ, 𝑆 <: 𝜇𝑌. 𝑇 ⊢ 𝑆 <: [𝑌 → 𝜇𝑌. 𝑇]𝑇
Σ ⊢ 𝑆 <: 𝜇𝑌. 𝑇
What is the derviation graph of XX <: 𝐸𝑣𝑒𝑛 → 𝑁𝑎𝑡 × 𝑌𝑌 ?
![Page 18: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/18.jpg)
New Derivation Graph
18
𝑁𝑎𝑡 → 𝐸𝑣𝑒𝑛 × 𝑋𝑋 <: 𝐸𝑣𝑒𝑛 → (𝑁𝑎𝑡 × 𝑌𝑌)
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡 𝐸𝑣𝑒𝑛 × 𝑋𝑋 <:𝑁𝑎𝑡 × 𝑌𝑌
𝐸𝑣𝑒𝑛 <:𝑁𝑎𝑡
XX <: 𝐸𝑣𝑒𝑛 → 𝑁𝑎𝑡 × 𝑌𝑌
𝑋𝑋 <: 𝑌𝑌
![Page 19: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/19.jpg)
Support Function
• 𝑠𝑢𝑝𝑝𝑜𝑟𝑡𝑆𝑚 𝑆 <: 𝑇 =
∅ 𝑖𝑓 𝑇 = 𝑇𝑜𝑝 ∨ (𝑆 = 𝐸𝑣𝑒𝑛 ∧ 𝑇 = 𝑁𝑎𝑡)
{𝑆1 <:𝑇1, 𝑆2 <: 𝑇2} 𝑖𝑓 𝑆 = 𝑆1 × 𝑆2 ∧ 𝑇 = 𝑇1 × 𝑇2𝑇1 <: 𝑆1, 𝑆2 <: 𝑇2 𝑖𝑓 𝑆 = 𝑆1 → 𝑆2 ∧ 𝑇 = 𝑇1 → 𝑇2
𝑆 <: 𝑋 ↦ 𝜇𝑋. 𝑇1 𝑇1 𝑖𝑓 𝑇 = 𝜇𝑋. 𝑇1𝑋 ↦ 𝜇𝑋. 𝑆1 𝑆1 <:𝑇 𝑖𝑓 𝑆 = 𝜇𝑋. 𝑆1 ∧ 𝑇 ≠ 𝜇𝑋. 𝑇1, 𝑇 ≠ 𝑇𝑜𝑝
↑ 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
• 𝑠𝑢𝑝𝑝𝑜𝑟𝑡𝑆𝑚 𝑋 =
ቊ𝑥∈𝑋ڂ 𝑠𝑢𝑝𝑝𝑜𝑟𝑡𝑆𝑚(𝑥) 𝑖𝑓 ∀𝑥 ∈ 𝑋. 𝑠𝑢𝑝𝑝𝑜𝑟𝑡𝑆𝑚 𝑥 ↓
↑ 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
19
![Page 20: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/20.jpg)
The algorithm
20
![Page 21: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/21.jpg)
Termination
• X grows larger in every iteration
• S is a subexpression of T either• S forms a sub tree in the AST of T
• S forms a sub tree in the AST of [X → 𝜇𝑋. 𝑇1]𝑇1 if T=𝜇𝑋. 𝑇1
• All pairs produced by 𝑠𝑢𝑝𝑝𝑜𝑟𝑡𝑆𝑚() are subexpressions of the original one
• There is only a finite number of subexpressions
21
![Page 22: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/22.jpg)
Inversible Subtyping Rules
• Functions premise/support requires the subtyping rules are inversible:• There is only one set of premise for each conclusion
• The algorithm will be much more complex if the subtyping rules are not inversible
• Example: uninversible rules
22
[𝑋 → 𝜇𝑋. 𝑆]𝑆 <: 𝑇
𝜇𝑋. 𝑆 <: 𝑇
𝑆 <: [𝑌 → 𝜇𝑌. 𝑇]𝑇
𝑆 <: 𝜇𝑌. 𝑇
![Page 23: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/23.jpg)
Inversible Subtyping Rules
• Functions premise/support requires the subtyping rules are inversible:• There is only one set of premise for each conclusion
• The algorithm will be much more complex if the subtyping rules are not inversible
• Example: uninversible rules
23
[𝑋 → 𝜇𝑋. 𝑆]𝑆 <: 𝑇 ∧ 𝑇 ≠ 𝜇𝑌. 𝑇1 ∧ 𝑇 ≠ 𝑇𝑂𝑃
𝜇𝑋. 𝑆 <: 𝑇
𝑆 <: [𝑌 → 𝜇𝑌. 𝑇]𝑇
𝑆 <: 𝜇𝑌. 𝑇
![Page 24: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/24.jpg)
Exercise
• Find two types S<:T where S<:T does not hold in iso-recursive types (even with the help of fold/unfold) but holds in equi-recursive types.
24
![Page 25: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/25.jpg)
Exercise
• Find two types S<:T where S<:T does not hold in iso-recursive types (even with the help of fold/unfold) but holds in equi-recursive types.
• 𝑆 = 𝜇𝑋.𝑁𝑎𝑡 × 𝑋
• 𝑇 = 𝜇𝑋.𝑁𝑎𝑡 × (𝑁𝑎𝑡 × 𝑋)
25
![Page 26: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/26.jpg)
Fixpoints, Induction, and Coinduction
26
![Page 27: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/27.jpg)
Fixed points
• The fixed point of a function f:T→T, is a value (fix f)∈T satisfying the following condition:• fix f = f (fix f)
• When T is a function type• fix f is a recursive function• Y and fix combinators produce such fixed point
• When T is not a function• Y and fix combinators no longer work
27
![Page 28: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/28.jpg)
Review: Terms, by Inference RulesThe set of terms is defined by the following rules:
Inference rules = Axioms + Proper rules
![Page 29: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/29.jpg)
Review: Terms, Concretely
For each natural number i, define a set Si as follows:
Finally, let
![Page 30: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/30.jpg)
Generating Function
• f(X) = {true, false, 0}∪ {succ 𝑡1, pred 𝑡1, iszero 𝑡1 ∣ 𝑡1 ∈ 𝑋}∪ {if 𝑡1 then 𝑡2 else 𝑡3 ∣ 𝑡1, 𝑡2, 𝑡3 ∈ 𝑋}
• S = ڂ𝑓𝑛 ∅
• We will show that S is the least fixed point of f
30
![Page 31: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/31.jpg)
Monotone function and closed sets• Monotone function: f : P(U)→P(U) is monotone iff
• ∀𝑋, 𝑌: 𝑋 ⊆ 𝑌 = 𝑓 𝑋 ⊆ 𝑓(𝑌)
• Let 𝑓: 𝑃 𝑈 → 𝑃(𝑈), X is f-closed if 𝑓 𝑋 ⊆ 𝑋.
31
![Page 32: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/32.jpg)
Knaster-Tarski Theorem
• Knaster-Tarski Theorem• The intersection of all f-closed sets is the least fixed
point of monotone function f, denoted 𝑙𝑓𝑝(𝑓).
• Proof:• Let K be the intersection of all f-closed sets
• Let A be an arbitrary f-closed set
• 𝐾 ⊆ 𝐴 → 𝑓 𝐾 ⊆ 𝑓 𝐴 → 𝑓 𝐾 ⊆ 𝐴
• Since A can be any f-closed set, f 𝐾 ⊆ 𝐾
• 𝑓 𝐾 ⊆ 𝐾 → 𝑓 𝑓 𝐾 ⊆ 𝑓 𝐾 → 𝑓 𝐾 is f-closed→ 𝐾 ⊆ 𝑓 𝐾
• Therefore 𝑓 𝐾 = 𝐾
• K is the least because any fixed point is f-closed
32
![Page 33: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/33.jpg)
Principle of Induction
• If X is f-closed, then 𝑙𝑓𝑝 𝑓 ⊆ 𝑋.
• Proving S = ڂ𝑓𝑛 ∅ = 𝑙𝑓𝑝 𝑓• ∅ ⊆ 𝑙𝑓𝑝 𝑓 → 𝑓𝑛 ∅ ⊆ 𝑙𝑓𝑝(𝑓) for any n
• Thus, 𝑆 ⊆ 𝑙𝑓𝑝 𝑓
• Let 𝐴 ⊆ 𝐵, we have 𝑓 𝐴 ∪ 𝐵 = 𝑓 𝐴 ∪ 𝑓(𝐵)
• From ∅ ⊆ 𝑓(∅), we have 𝑓𝑛 ∅ ⊆ 𝑓𝑛+1(∅) for any n
• 𝑓 𝑆 = 𝑓 (∅)𝑓𝑛ڂ = 𝑓𝑛+1ڂ ∅ = 𝑆, e.g., S is f-closed
• 𝑙𝑓𝑝 𝑓 ⊆ 𝑆
33
![Page 34: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/34.jpg)
Proving Mathematical Induction• Mathematical induction
1. Show P holds for case n=02. When P holds for case n=k, show P holds for case n=k+13. P holds for any natural number
• Let 𝑓 𝑋 = 0 ∪ 𝑖 + 1 𝑖 ∈ 𝑋 . We have lfp(f) is the whole set of natural numbers
• Let PP be the set of natural number where P holds. We have• 0 ∈ 𝑃𝑃 ∧ 𝑖 ∈ 𝑃𝑃 → 𝑖 + 1 ∈ 𝑃𝑃• PP is f-closed
• 𝑙𝑓𝑝 𝑓 ⊆ 𝑃𝑃
34
![Page 35: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/35.jpg)
Infinite Values
• Let f(X)={nil}∪{cons i t | i∈Nat, t∈X}
• What is in lfp(X)?
35
![Page 36: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/36.jpg)
Principle of Coinduction
• Let 𝑓: 𝑃 𝑈 → 𝑃(𝑈), X is f-consistent if 𝑋 ⊆ 𝑓 𝑋 .
• The dual of Knaster-Tarski Theorem• The union of all f-consistent sets is the greatest fixed
point of monotone function f, denoted g𝑓𝑝(𝑓).• Proof: By duality
• Principle of Coinduction• If X is f-consistent, then X ⊆ 𝑔𝑓𝑝 𝑓 .
• Proof: By duality
36
![Page 37: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/37.jpg)
Infinite Members and Greatest Fixed Point• 𝑔𝑓𝑝 𝑓 = ⋂𝑓𝑛 𝑈 , 𝑛 is any natural number
is the greatest fixed point of the monotone function f and the universal set U
• Let f(X)={nil}∪{cons i t | i∈Nat, t∈X}, gfp(f) contains all finite and infinite lists
37
![Page 38: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/38.jpg)
Summary
• Rules can be represented as generating functions
• The least fixed point is the set of finite terms
• The greatest fixed point is the set of finite and infinite terms
• Principles of Induction and Coinduction are useful in proving theorems• See book for examples of using principles of coinduction
38
![Page 39: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/39.jpg)
Exercise
• Defining a generating function s for the subtyping relation, where gfp(s) is the set of all pairs of (A, B) where A<:B
39
Even <:Nat
𝑋 → 𝜇𝑋. 𝑆 𝑆 <: 𝑇
𝜇𝑋. 𝑆 <: 𝑇
𝑆 <: [𝑌 → 𝜇𝑌. 𝑇]𝑇
𝑆 <: 𝜇𝑌. 𝑇
![Page 40: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/40.jpg)
Exercise
• Defining a generating function s for the subtyping relation, where gfp(s) is the set of all pairs of (A, B) where A<:B
40
s(R) = { S <: Top | for any type S }∪ {𝑆1 × 𝑆2 <: 𝑇1 × 𝑇2 | 𝑆1 <: 𝑇1, 𝑆2 <: 𝑇2 ∈ 𝑅}∪ {𝑆1 → 𝑆2 <: 𝑇1 → 𝑇2|𝑇1 <: 𝑆1, 𝑆2 <:𝑇2 ∈ 𝑅}∪ {𝑆 <: 𝜇𝑋. 𝑇 | 𝑆 <: 𝑋 ↦ 𝜇𝑋. 𝑇 𝑇 ∈ 𝑅}∪ {𝜇𝑋. 𝑆 <: 𝑇 | [X ↦ 𝜇𝑋. 𝑇]𝑆 <: 𝑇 ∈ 𝑅}
![Page 41: 编程语言的设计原理 Recursive Types · •Try to determine the following subtype relations using the algorithm • . . ×](https://reader030.vdocuments.pub/reader030/viewer/2022041113/5f1da1ee808f22586a6e1f13/html5/thumbnails/41.jpg)
Homework
• Choose a language with high-order function support, and investigate• (1) Whether and how this language supports recursive
types,
• (2) How this support differs from what we learned in the course, and
• (3) Why this design is adopted for the language.
• Summarize the findings as a report.
41