Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Understanding Subtyping in Programming Language Theory: A Step-by-Step Proof
Nov 13, 2024
421 views
Written by Prashant Basnet
👋 Welcome to my Signature, a space between logic and curiosity.
I’m a Software Development Engineer who loves turning ideas into systems that work beautifully.
This space captures the process: the bugs, breakthroughs, and “aha” moments that keep me building.
this lecture is from CSCE-550, Nov 12, 2024.
Why understand the lecture on Programming language theory (Subtyping) is important to study ?
Today's lecture highlighted the significance of programming language theory, particularly in the context of type systems. This theory is crucial for:
interface Person { name: string; age: number; } interface Employee { name: string; age: number; salary: number; } let person: Person; let employee: Employee = {name: "Alice", age: 30, salary: 50000}; person = employee; // This works! Thanks to width subtypingFirst thing first let's note:
Subtyping Rules:
{name: String, age: Int} is the same as {age: Int, name: String}Proving Subtyping Relationships:
{y:Nat, x:{a:Nat,b:Nat}} <: {x:{a:Nat}}// Complex type (left side of <:) { y: 42, // has a y field that's a number x: { // has an x field that's a record a: 1, // with an a field that's a number b: 2 // and a b field that's a number } } // Simple type (right side of <:) { x: { // only needs an x field that's a record a: 1 // with just an a field that's a number } } // Simple interface - what a function might expect interface SimpleType { x: { a: number } } // A function that only cares about x.a function printA(obj: SimpleType) { console.log(obj.x.a); } // Our complex type with extra stuff const complexObj = { y: 42, x: { a: 1, b: 2 } } // This should work fine! Even though complexObj has extra fields printA(complexObj); // prints: 1So we pick a middle ground - T2:
// T2 (our intermediate step) { y: 42, // still has the y field x: { // has the x field a: 1 // but x only has a, dropped b } }let me explain T2 with a simpler analogy!
Think of it like proving a friend can get from New York to Los Angeles:
Chicago is like our T2 - it's a stepping stone or midpoint that makes the proof easier.
In our type problem:
Start: {y:Nat, x:{a:Nat,b:Nat}} // Like New York T2: {y:Nat, x:{a:Nat}} // Like Chicago (our midpoint!) End: {x:{a:Nat}} // Like Los Angeles We need to prove: 1. Start → T2 (Drop b) 2. T2 → End (Drop y)T2 is just a midpoint type we invented to make the proof easier. Instead of making one big jump (proving Start can be used as End directly), we prove:
We don't drop both at once! That's the whole point of having T2 - it lets us drop one thing at a time, making each step simpler to prove. It's like:
{y:Nat, x:{a:Nat,b:Nat}} <: {x:{a:Nat}}This is our end goal!
{y:Nat, x:{a:Nat,b:Nat}} <: T2where T2 = {y:Nat, x:{a:Nat}}, This shows we can drop the b field.
After dropping b, we have T2 = {y:Nat, x:{a:Nat}} and we want to get to {x:{a:Nat}}
But we don't just drop y directly.
{y:Nat, x:{a:Nat}} <: {x:{a:Nat}}let's hold on R3 at the moment, we will come back to R3 after R4 & R5
Now with THIS RESULT, R1 & R2 we do R4:
{y:Nat, x:{a:Nat}} <: {x:{a:Nat}, y:Nat}This just says we can reorder fields:
Applying S-Width rule (R4):
The S-Width rule says we can drop fields from a record type, but in many type systems, we want this to be predictable and mechanical. So the convention is that we can drop fields from the end or right side of the record.
for example: // We can drop the last field using S-Width {x:Nat, y:Nat, z:Nat} <: {x:Nat, y:Nat} // OK-dropping from end {x:Nat, y:Nat} <: {x:Nat} // OK - dropping from end // But we can't directly drop from middle {x:Nat, y:Nat, z:Nat} <: {x:Nat, z:Nat} // NOT OK-can't drop y from middlethat's why we needed step R4 (permutation) first:
It's like:
{x:{a:Nat}, y:Nat} <: {x:{a:Nat}}Conclusion
Thus, using valid subtyping rules, we have shown that
{y: Nat, x: {a: Nat, b: Nat}} <: {x: {a: Nat}}This is proven by:
This proves our original sub typing relationship is valid!
Each step used only our allowed rules (S-Width, S-Perm, S-Depth, etc.) to get from start to finish.
This demonstrates how we can derive valid conclusions through a series of logical steps.