Title: Singleton Kinds
1Singleton Kinds
- Derek Dreyer
- Fall Semester, 2002
2Part 1Motivation
3Type-Directed Compilation
- Retain types throughout compilation using typed
intermediate languages. - Enables type-based optimizations
- Typechecking after compiler phases improves
robustness of compiler - Makes it possible to target typed assembly
language
4The TIL Compiler
- Type-directed compiler for Core ML (Tarditi et
al., PLDI 96) - Highly optimized
- Intensional type analysis (Harper and Morrisett,
POPL 95) - Other traditional optimizations, not previously
implemented for functional languages (Tarditis
thesis, 97)
5Problems with TIL
- Large type representations
- Whole-program optimizations
- Doesnt support modules!
6TILT (TIL Two)
- Complete re-engineering of TIL
- Compiles full Standard ML
- Supports separate compilation
- Sharing of type representations via singleton
kinds.
7Structure of TILT
Standard ML
Elaboration
High-level Intermediate Language
HIL
Phase-Splitting
Mid-level Intermediate Language
MIL
Code Generation
Optimizations
8HIL
- Described in Harper-Stone 98, along with the
elaboration algorithm - Based on Harper-Lillibridge (HL) formalism,
restricted to second-class modules - Coercive signature subtyping (e.g. dropping
components) handled by elaboration - HIL subtyping (like HLs) strictly about
forgetting of type identity
9Phase-Splitting
- Erase sealing
- Sealing just restricts the use of a module
- Unnecessary after elaboration/typechecking
- Split modules and signatures into two parts
- The static part has the type components
- The dynamic part has the term components
- Once separated, both parts can be written in the
MIL.
10Phase-Splitting
- X sig type t val x t end
- X_Static sig type t end
- X_Dynamic sig val x X_Static.t end
11MIL Syntactic Classes
- X_Static is a type constructor, which is
classified by a kind. - X_Dynamic is a term, which is classified by a
type.
12Simple Kinds
- Base kind is T, the kind of types.
- int T, bool T
- Function and product kinds
- K1 ! K2, K1 K2
- Example
- type (a,b) pair a b
- pair l(gT T). p1g p2g
- pair (T T) ! T
13Type Definitions
- X sig
- type t
- type u A ( where A is big )
- val x t
- end
- X_Static T ( just the t component )
- X_Dynamic t A / u, X.Static / t
14Dependent Singleton Kinds(Stone and Harper,
POPL 00)
- To avoid space blowup, introduce
- new singleton kind S(A)
- B S(A) ? B A T
- S(A) 6 T
- Must also generalize ! and to
- Dependent function kind PaK1.K2
- Dependent sum kind SaK1.K2
15Dependent S Example
- X_Static S tT.S(A)
- X_Dynamic
- t p1(X_Static) / t, p2(X_Static) / u
- We havent lost any equivalences
- p2(X_Static) Ap1(X_Static) / t
16Dependent P Example
- F funsig (Xsig type elem end) -gt
- sig type elem X.elem
- type set
- ...
- end
- F_Static P a T. (S(a) T)
17Advantages of Singletons
- Carry advantages of translucency over to the MIL
level - Precise control of type propagation
- Sharing of type representations
- Clean handling of open-scope type definitions.
18Part 2Type System
19Syntax
- A a laK.A A1 A2
- ltaA1,A2gt p1A p2A
- K T PaK1.K2 SaK1.K2 S(A)
- G ² G, aK
20Typing Rules
- Standard dependent typing rules
21Selfification
- Recall the idea of selfification from the
translucent sums formalism - If X sig type t end, then X sig type t X.t
end.
22Selfification
- Recall the idea of selfification from the
translucent sums formalism - If X sig type t end, then X sig type t X.t
end. - Selfification arises naturally here as singleton
introduction
23Everything is Determinate
- Unlike Harper-Lillibridge, there is no value
restriction here on what can be selfified. - Every type constructor A is determinate because
the language is pure and there is no sealing.
24Higher-Order Selfification
- If X sig structure A SIGA
structure B SIGB end,then X sig
structure A Selfify(SIGA, X.A)
structure B Selfify(SIGB, X.B) end
25Higher-Order Selfification
- If X sig structure A SIGA
structure B SIGB end,then X sig
structure A Selfify(SIGA, X.A)
structure B Selfify(SIGB, X.B) end - Achieved in type theory by a kind strengthening
rule
26Higher-Order Selfification
- If F funsig (XSIGA) ! SIGB,then F
funsig (XSIGA) ! Selfify(SIGB, F(X)) - Wait a sec! Can we do that?What about the
generativity of F? - Well...the types in the body of F cant really
depend on run-time conditions.
27Higher-Order Selfification
- Type constructors clearly can be selfified
- list T ! T
- Principal kind of list is PaT.S(list a)
28Higher-Order Selfification
- Type constructors clearly can be selfified
- list T ! T
- Principal kind of list is PaT.S(list a)
- And now for the kind strengthening rule
- Left premise necessary to ensure a not in the
free variables of A
29Subkinding
- Subkinding very similar to signature subtyping in
Harper-Lillibridge
30Question
- So now that weve defined the typing judgment,
onto equivalence. - But wait if A B T, then how do we prove A
S(B)?
31Answer
- So now that weve defined the typing judgment,
onto equivalence. - But wait if A B T, then how do we prove A
S(B)? - Answer
- A S(A), and if A B, then S(A) 6 S(B), so by
subsumption A S(B).
32Type Constructor Equivalence
- Standard structural rules
- Reflexive, symmetric, transitive
- Beta-equivalence rules
- Subsumption rule
- Singleton elimination rule
33Extensionality
- Eta-equivalence provided in the form of
extensionality rules
34Admissibility of Eta
- If F PaK1.K2, then assuming aK1,we have
(laK1.F(a))(a) F(a) K2,so by
extensionality, (laK1.F(a)) F. - Cool property of extensionality
- Self rules are just reflexive forms of
equivalence rules (nice for proofs later on - Can think of self rules as follows
35Higher-Order Singletons
- S(A) only valid if A T.
- In fact, S(A K) at higher K is definable so
that the singleton rules hold at any kind - If A S(B K) and B K, then A B S(B K).
- If A K, then A S(A K) and S(A K) 6 K.
- If A1 A2 K1 and K1 6 K2,then S(A1 K1) 6
S(A2 K2).
36Higher-Order Singletons
- Extensionality important here!
- S(A K) classifies constructors that are
extensionally equal to the eta-expansion of A.
37Admissibility of Beta
- It turns out that beta-equivalence is admissible
in the presence of higher-order singletons
38Subtleties of Equivalence
- Constructor equivalence obviously depends on the
context - aT, bT a b T.
- aT, bS(a) a b T.
39Subtleties of Equivalence
- It less obviously depends on the kind at which
the constructors are compared. - laT.a laT.int T ! T.
- laT.a laT.int S(int) ! T.
- This turns out to have a profound effect on the
type equivalence algorithm.
40Part 3Decidability of Typechecking
41Typechecking Algorithm
- Want to determine if G A K
- First synthesize principal kind KP of A
- Then check if G KP 6 K
42Principal Kind Synthesis
- G A K synthesizes principal K of A
- Synthesis for intro and elim forms corresponds
precisely to typing rules - For variables, we selfify
- G a S(a G(a))
- Theorem If G A K, then G A KP and G KP
6 K.
43Question
- How do we prove theorem for self rule?
44Answer
- How do we prove theorem for self rule?
- Answer Need to generalize theorem
- If G A K, then G A KP
- and G KP 6 S(A K).
- (Lemma 2.2 in Stone-Harper)
45Subkinding
- Subkinding is syntax-directed
- To decide S(A) 6 S(B), we need to decide whether
A B T - Decidability of typechecking reduces to
decidability of constructor equivalence
46Equivalence Algorithm
- Typically, to decide whether A B, we
normalize-and-compare - Define some reduction relation that is confluent
and strongly normalizing - Then A B iff their normal forms are equal
- Doesnt work when equivalence is kind- and
context-dependent - e.g. laT.a laT.int S(int) ! T
47Equivalence Algorithm
- Inspired by Coquand 91 to define equivalence
algorithm directly - Algorithmic judgment G A1 , A2 K
- Big picture
- Constructor equivalence reduces to type
equivalence (i.e. G A1 , A2 T) - At kind T, reduce A1 and A2 to paths p1 and p2,
expanding out singletons along the way - Compare p1 and p2 structurally
48Reducing to Type Equivalence
- When checking G A1 , A2 K, we can assume that
G A1 K and G A2 K - If K is a singleton S(A), we can immediately
accept because A1 A and A2 A, so
49Reducing to Type Equivalence
- At higher kind, algorithmic equivalence is
precisely extensional equivalence - Note that the kinds are smaller in the premises
than in the conclusions.
50Reducing to Type Equivalence
- At base kind T, we reduce A1 and A2 to a form in
which we can structurally compare them, namely
path form, also called weak head normal form - Note we could have done the singleton case this
way as well.
51Paths
- Define elimination contexts E as follows
- E ² E A p1 E p2 E
- Paths p are types of the form Ea
52Weak Head Reduction
- Possible non-paths and their corresponding weak
head reductions
53The Next Step
- So weve weak head reduced A1 and A2 to p1 and p2
- Are we ready for structural comparison?
54Whoops...
- So weve weak head reduced A1 and A2 to p1 and p2
- Are we ready for structural comparison?
- NO. Suppose G aT, bS(a).
- G a b T
- a and b are paths
- a and b are not structurally equivalent
55Expanding Out Singletons
- b S(a), so we want to expand out the singleton
definition of b to a. - But every p S(p), so how do we know which
singletons to expand out?
56Expanding Out Singletons
- b S(a), so we want to expand out the singleton
definition of b to a. - But every p S(p), so how do we know which
singletons to expand out? - Answer We only reduce p if its really defined
to be another type.
57Natural Kind Synthesis
- The natural kind of p is the kind you would come
up with if you had never heard of selfification. - Natural kind synthesis G p " K
- Defined just as principal kind synthesis, except
variables arent selfified - G a " G(a)
58Expanding Out Singletons
- To incorporate expansion of singleton definitions
back into weak head reduction - A path p is in weak head normal form if G p "
T, i.e. if p is an abstract type.
59Path Comparison
- Two normal-form paths (abstract types) are
equivalent if they are structurally equivalent. - Path equivalence G p1 p2 " K
60Path Comparison
- Interesting case is application
- K1 is synthesized by the first premise
- Second premise loops back to the main algorithmic
judgment, directed by K1
61Test
- Lets try it out on our original example
62Test
- Lets try it out on our original example
63Some Questions
- OK, well buy the algorithm is sound. But
- How do we know that it is complete?
- How do we know that it terminates?
- How would we prove 1 or 2 anyway?
- How would we come up with the algorithm if it had
not been handed down to us by Stone and Harper?
64Logical Relations
- Method of logical relations used originally by
Tait to prove strong normalization for the
simply-typed l-calculus - Want to show property R of well-typed terms that
is hard to show directly. - Basic Idea Strengthen R to more powerful
induction hypothesis S, which is called a logical
relation.
65Logical Relations
- Logical relations proofs are complex, but have a
very regular structure. - When proving that S(A) ) R(A)
- Must prove that R(p) implies S(p) for all paths
p. - When proving that 8 well-typed A. S(A)
- Must prove that property S is preserved under
weak head expansion.
66Logical Relations
- Stone-Harper algorithm reflects the structure of
a logical relations proof. - To prove completeness and termination, define
logical equivalence of constructors. - Must prove that A1 A2 K implies logical
equivalence of A1 and A2 at K - Must prove that logical equivalence ofA1 and A2
at K implies A1 , A2 K
67Symmetry and Transitivity
- Algorithm is not obviously symmetric or
transitive. For instance - By induction, G p1A2 , p1A1 K andG p2A2 ,
p2A1 Kp1A1/a - We need this kind here to be Kp1A2/a
68Good Fences Make Good Neighbors
- Solution Make the algorithm maintain a separate
kind and context for each type constructor. - New judgments
- G1 A1 K1 , G2 A2 K2.
- G1 p1 " K1 G2 p2 " K2.
- Of course, the algorithm only makes sense if the
contexts and kinds are equivalent.
69Good Fences Make Good Neighbors
- The judgment is a monster, but the problematic
case is no longer a problem
70Good Fences Make Good Neighbors
- The form of the algorithm is reflected in the
form of the logical relation, written - After all is said and proved, we can show that
the simpler form of the algorithm is complete
with respect to the monstrous one.
71Conclusions
- Singleton kinds provide the benefits of
translucency in the absence of modules. - Selfification at base kind singleton
introduction. - Selfification at higher kind reflexive
extensionality. - Singletons preclude normalize-and-compare
algorithm for deciding type equivalence. - Direct equivalence algorithm rather involved, but
its structure is based on the method of logical
relations. - Needed to symmetricize the algorithm and the
logical relations, in order to make the proof
work.