-
Notifications
You must be signed in to change notification settings - Fork 15
Types and Programming Languages Chapter 16 Metatheory of Subtyping
And so it was that on the 19th of September, the Year of our Lord 2017, the Computation Club of London met with every intention of blasting through The Metatheory of Subtyping. Per Tom's chapter update:
It’s called “Metatheory of Subtyping”, making it perhaps the most intimidatingly-titled chapter we’ve seen so far, which is unfortunate because it’s actually quite practical and engineering-y. Of course we have to wade through the usual mathematical language to get to the practical engineering bits, but ultimately the chapter could’ve easily been called “Actually Implementing Subtyping: How Do You Flipping Even?” if Piercey Pierce hadn’t been so keen to impress at parties.
We like to think of ourselves as quite practical and engineering-y, so how did we get on? PROBABLY FINE, RIGHT! Basically we had this one pencilled in as a gimme.
Unfortunately, as you probably already know, computers.
We spent a moment recapping why subtyping presents a problem for implementation at all. Tom said:
The typing rules we saw in chapter 15 are like great and everything, but they’re not in a fit state to be directly implemented as a typechecking algorithm. The T-Sub rule (from the typing relation) has such a vague conclusion that it can be applied to any term; the S-Trans rule (from the subtyping relation) has the same problem, plus it expects us to somehow invent an intermediate type when gluing two other subtyping derivations together. In both cases the necessary “guesswork” makes the previously syntax-directed process of typechecking much less obvious. We have coped with this during whiteboarding sessions by using intuition to decide which rules to apply at which points, but that’s not good enough for a computer is it? Let’s sort it! Get out of my pub!
We spent a bit of time discussing what the implications of this were for implementation algorithms. Previously our inference rules for the different relations (typing, evaluation...) had been so mechanical and so reductive that we could turn them more-or-less directly into an algorithm to derive the relation they described.
For example, T-Abs
is applicable only to lambda abstractions, and the single premise is such that we know we're type-checking a smaller term (i.e. the body of the lambda abstraction):
Γ,x:T1 ⊢ t2 : T2
----------------------
Γ ⊢ λx:T1.t2 : T1 → T2
By contrast, the new T-Sub
rule is not so restrictive - it applies to literally any term:
Γ ⊢ t : S, S <: T
-----------------
Γ ⊢ t : T
Up until now our implementations have effectively been a big case statement for different sorts of terms:
def type_of(term, context = {})
case term
when Term::True, Term::False
# ...
when Term::Var
# ...
when Term::Abs
# ...
when Term::If
With T-Sub
we've got no such restriction - the bare metavariable t
will match any term, and as it appears directly in the premise (Γ ⊢ t : S
) we could end up executing this rule forever if we implemented it directly as written. Until now our rules have been syntax-directed, which is to say that each rule has matched a single syntactic form, meaning at any given point we always know which rule to apply. That's no longer the case!
With the subtyping relation, this is even more troublesome - not only do we have a bare metavariable S
in the conclusion, we have a completely new intermediate variable U
in the premises:
S <: U U <: T
---------------
S <: T
When considering this rule before we've intuited U
using our enormous human brains, but that doesn't really work for programming, more's the pity. The rest of the chapter discusses elegant logical workarounds / appalling hacks around the above problems, which let us turn the above inference rules back into something syntax-directed.
- Home
- Documentation
- Choosing a Topic
- Shows & Tells
- Miscellaneous
- Opt Art
- Reinforcement Learning: An Introduction
- 10 Technical Papers Every Programmer Should Read (At Least Twice)
- 7 More Languages in 7 Weeks
- Lua, Day 1: The Call to Adventure
- Lua, Day 2: Tables All the Way Down
- Lua, Day 3
- Factor, Day 1: Stack On, Stack Off
- Factor, Day 2: Painting the Fence
- Factor, Day 3: Balancing on a Boat
- Elm, Day 1: Handling the Basics
- Elm, Day 2: The Elm Architecture
- Elm, Day 3: The Elm Architecture
- Elixir, Day 1: Laying a Great Foundation
- Elixir, Day 2: Controlling Mutations
- Elixir, Day 3: Spawning and Respawning
- Julia, Day 1: Resistance Is Futile
- Julia, Day 2: Getting Assimilated
- Julia, Day 3: Become One With Julia
- Minikanren, Days 1-3
- Minikanren, Einstein's Puzzle
- Idris Days 1-2
- Types and Programming Languages
- Chapter 1: Introduction
- Chapter 2: Mathematical Preliminaries
- Chapter 3: Untyped Arithmetic Expressions
- Chapter 4: An ML Implementation of Arithmetic Expressions
- Chapter 5: The Untyped Lambda-Calculus
- Chapters 6 & 7: De Bruijn Indices and an ML Implementation of the Lambda-Calculus
- Chapter 8: Typed Arithmetic Expressions
- Chapter 9: The Simply-Typed Lambda Calculus
- Chapter 10: An ML Implementation of Simple Types
- Chapter 11: Simple Extensions
- Chapter 11 Redux: Simple Extensions
- Chapter 13: References
- Chapter 14: Exceptions
- Chapter 15: Subtyping – Part 1
- Chapter 15: Subtyping – Part 2
- Chapter 16: The Metatheory of Subtyping
- Chapter 16: Implementation
- Chapter 18: Case Study: Imperative Objects
- Chapter 19: Case Study: Featherweight Java
- The New Turing Omnibus
- Errata
- Chapter 11: Search Trees
- Chapter 8: Random Numbers
- Chapter 35: Sequential Sorting
- Chapter 58: Predicate Calculus
- Chapter 27: Perceptrons
- Chapter 9: Mathematical Research
- Chapter 16: Genetic Algorithms
- Chapter 37: Public Key Cryptography
- Chapter 6: Game Trees
- Chapter 5: Gödel's Theorem
- Chapter 34: Satisfiability (also featuring: Sentient)
- Chapter 44: Cellular Automata
- Chapter 47: Storing Images
- Chapter 12: Error-Correcting Codes
- Chapter 32: The Fast Fourier Transform
- Chapter 36: Neural Networks That Learn
- Chapter 41: NP-Completeness
- Chapter 55: Iteration and Recursion
- Chapter 19: Computer Vision
- Chapter 61: Searching Strings
- Chapter 66: Church's Thesis
- Chapter 52: Text Compression
- Chapter 22: Minimum spanning tree
- Chapter 64: Logic Programming
- Chapter 60: Computer Viruses
- Show & Tell
- Elements of Computing Systems
- Archived pages