Lectures onType Theory
ch:polymorphic-record-compilation: ch:polymorphic-record-compilation
appendix sectionsolutions

ch:polymorphic-record-compilation: ch:polymorphic-record-compilation

Exercise 8.1.

For a::U, the image String is a well-formed type. For r::{{Name:a}}, substitution produces the obligation {Age:Nat,Name:String}::{{Name:String}}. Its finite-map inclusion premise holds because the concrete type contains the required field with exactly the substituted type. Thus the pair respects both bindings of K, and K-Rec derives the displayed judgment.

Exercise 8.2.

The first label set sorts as [Name,Office], so Name has position 1. The second sorts as [Age,Name,Phone], so it has position 2. Therefore the two record values compile to ["Joe",403] and [21,"Hanako",7222]. Rule V-Nth types the selections at String, and the two target roots are ["Joe",403]10"Joe",[21,"Hanako",7222]20"Hanako".

Exercise 8.3.

After the two type applications, the selector has target term λiI.λx:τH.xI and index-arrow result idx(Name,τH)i(τHString), where τH is the displayed concrete record type. In its translated layout, Name has position 2, so IV-Pos derives ;Vi2:idx(Name,τH). Rule V-IApp therefore derives (λiI.λx:τH.xI)@2:τHString. Rule V-Vec assigns the vector the type τH; target application gives type String. The two target contractions yield [21,"Hanako",7222]20"Hanako".

Exercise 8.4.

The two source roots are {Name="Joe",Office=403}.Name0"Joe",{Age=21,Name="Hanako",Phone=7222}.Name0"Hanako". Write vJ and vH for the two source record values in the displayed roots, put AJ=["Joe",403] and AH=[21,"Hanako",7222], and let τJ and τH be their respective record types. The record clause gives the two exact membership judgments (vJ,AJ)RrecτJ,(vH,AH)RrecτH. The canonical positions are 1 and 2, so the target roots are ["Joe",403]10"Joe",[21,"Hanako",7222]20"Hanako". The base clause relates each resulting string constant to itself, which is the projection case of the fundamental relation for both records.

Exercise 8.5.

The translated type is a::U.r::{{Age:Nat,Name:a}}.idx(Age,r)iidx(Name,r)ir{Age:Nat,Name:a}. The term inserts index abstractions in canonical label order: Λa.Λr.λiIr,Age.λiIr,Name.λx.[xIr,Age,xIr,Name]. At the stated concrete type, the canonical layout is [Age,Name,Phone], so the supplied indices are 1,2. The body is [x1,x2], whose result layout is [Age,Name].

Exercise 8.6.

For the record-kinded abstraction case, fix k={{i:τi}}i=1m in canonical order. The induction hypothesis is K,t::k;(LK),(LK,t);TVC:σ. Every variable in LK,t has the form It,i. Every variable in LK has the form Iu, with udom(K). Freshness of t and global injectivity therefore make the domains disjoint. Applying V-IAbs for i=m,m1,,1 derives the index-arrow suffix, and V-TAbs derives the translated universal.

For type application, canonical translation gives KVτ::k. Index availability certifies (i,τ) for Ji in the source index judgment; clause 3 of lemma 8.13 gives, for every i, K;(LK)ViJi:idx(i,τ). Rule V-TApp first derives C[τ]; applying V-IApp in the canonical order derives the type σ[τ/t]. By lemma 8.13, this type is (σ[τ/t]), which is the required conclusion.

Exercise 8.7.

Erasure leaves one target body λx.xi. In the first opening layout the name is at i=1; in the second it is at i=2. Since 12, no numeral makes both calls select the name. Searching the vector for a run-time label can choose the right component, but the target operation is then a dynamic search rather than the constant numeric selection required by the compiler specification.

Exercise 8.8.

Use the record types τ1={Name:String,Office:Nat},τ2={Age:Nat,Name:String}. Both have exactly two distinct labels, so |layout(τ1)|=2=|layout(τ2)|. Their layouts are [Name,Office] and [Age,Name], and their name positions are 1 and 2. An unlabeled vector of length two does not reveal which layout produced it, so length and vector shape cannot determine the source projection. Passing the singleton-typed index I:idx(Name,r), instantiated to the concrete position at type application, restores exactly the missing information.

Search the book

Type to search the local edition.