CS 641 lecture -*- Outline -*- * second-order calculi (chapter 13) The purpose of the type quantifiers is to allow more polymorphism One goal is to formalize the type Self, the type of a method that returns the self parameter, which varies when the method is inherited... ** the universal quantifier (13.1) *** parametric polymorphism (13.1.1) Q: what is parametric polymorphism? the notation used for type abstraction is a lambda with a type variable, correspondingly, application to a type is written a(A). Q: what is the difference between the type applications and instantiation? Q: what is the type of a type abstraction? Of a C++ template? Q: how are the type rules for type abstractions and type applications analogous to lambda abstractions and applications? Q: what are the differences? Q: how does the (Val Fun2<:) rule relate to logic? Q: within the scope of a lambda, how is subtyping handled for the universally quantified variable? Is it structural or by name? Q: what's going on in the hypothesis of the (Eq Fun2) rule? Q: can you explain the side condition in the (Eval Eta2) rule? Nomenclature: system F, or F_2. Q: how can you encode the Booleans within system F? example: Boolean = \forall(X) X -> X -> X true: Boolean = \(X)\(y:X) -> \(z:X) -> y false: Boolean = \(X)\(y:X) -> \(z:X) -> z if: \forall(Y) Boolean -> Y -> Y -> Y = \(Y)\(b:Boolean)\(c:Y)\(a:Y) b(Y)(c)(a) so constants are not needed... system F is strongly normalizing, so every term has a normal form. Q: is Ob strongly normalizing? Why or why not? *** bounded parametric polymorphism (13.1.2) we can add subtyping in two ways: subtyping among universal types, subtyping as a way to bound to the applicability of a universal type Q: where are these uses of subtyping in the typing rules on page 171? Q: what is the variance of universal quantification? Q: can you explain the equational rules? *** structural update (13.1.3) Q: what is the difference between the (Val Structural Update) rule and the (Val Update) rule on page 81? Q: what advantage does it have in programming? Q: what are the potential problems with the new rule? ** existential qualifiers (13.2) An existentially quantified type, \exists(X <:A) B{X}, is the type of pairs (A',b), where A' <: A, and b: B{A'}. This can be seen as a partially abstract data type, with interface B{X}, and with hidden representation type X, but the hiding is partial in that X is known to be a subtype of A. a pair (A',b) having this type is a particular implementation of the data abstraction with A' being the representation type, and b the actual implementation. Nomenclature: dependent sum types, dependent pairs. notation, pack X<:A = A' with b{X}:B{X} stands for (A',b{{A'}}) : \exists(X <:A) B{X} e.g., pair(S)(T) = \exists(X<:S)\exists(Y<:T) [fst: X, snd: Y] pair12 : pair(Int)(Int) = pack X<:Top = Nat with pack Y<:Top = Nat with [fst=1, snd=2] : [fst: X, snd: Y] : \exists(Y<:Int) [fst: X, snd: Y] to use an element of an existential type, one needs a binding form that can give access to the representation type and the implementation open c as X <:A, x:B{X} in d{X,x}: D in this form, the result type, D, must not depend on X. firstgreater : \exists(X<:Int)\exists(Y<:Int) [fst: X, snd: Y] -> Int = \(pnt) open pnt as X<:Int, p2:\exists(Y<:Int) [fst:X, snd: Y] in open p2 as Y<:Int, p: [fst:X, snd: Y] in p.fst + p.snd Q: how does this correspond to a data abstraction with a hidden type? Q: what good does it do us to have types for data abstractions? Q: how does the (Val Pack<:) rule relate to logic? Q: how is the constraint on D reflected in the type rule (Val Open<:)? Q: what is the variance of an existential type? Q: are the subtyping relationships of an existentially quantified type variable handled structurally or by name? Q: does the equational theory make it possible to ignore the representation type? Q: what does the (Eval Repack<:) rule do? Q: can you explain the encoding of bounded existentials on page 175? Q: how would the type checking rule for eval open be derived? Q: what to the definitions of the various calculi mean on the bottom of p. 175? ** variance properties (13.3) Q: what is the notation for saying that a variable occurs only positively in a term or type? Q: for saying that it occurs either positively or negatively? ** encodings (13.4) Q: what would an example of the translation of a function type be? Q: does this translation make sense? Why? Q: how do they get the desired variance properties? Q: how do they encode product types? Q: are these immutable? Updatable? Q: how would you encode the record type ? "these encodings confirm the expressiveness of Ob<:" ** the Self quantifier (13.5) this section introduces another quantifier, the idea is to use it eventually to help in representing the type Self *** definition (13.5.1) the definition combines recursion and bounded existentials S(X)B = mu(Y)\exists(X<:)B (Y not occurring in B) Q: what is the subtyping property of the self quantifier?