Lectures onType Theory
ch:typed-metaprogramming: ch:typed-metaprogramming
appendix sectionsolutions

ch:typed-metaprogramming: ch:typed-metaprogramming

Exercise 116.1.

Let the caller’s printed x carry scope set S. Expansion allocates fresh stamps s1,s2S and produces let xS{s1}=e1 in let xS{s2}=e2 in (xS{s2},xS{s1}). Identifiers copied from e1,e2 retain their caller scopes. A free xS inside e1 therefore resolves to the caller declaration: neither introduced binder has a scope set contained in S. The two generated references resolve respectively to the unique declarations whose scope sets are S{s2} and S{s1}. Maximal-subset resolution is unique in all three cases.

Exercise 116.2.

For Γ0=(c:Code(N)@0), the complete tree is (c:Code(N)@0)Γ0Γ00c:Code(N)QVarΓ01splice(c):NQSplice Γ011:NNatΓ01splice(c)+1:NAddΓ00quote(splice(c)+1):Code(N)QQuote. For Γ1=(d:Code(N)@1), the nested tree is (d:Code(N)@1)Γ1Γ11d:Code(N)QVarΓ12splice(d):NQSpliceΓ11quote(splice(d)):Code(N)QQuoteΓ10quote(quote(splice(d))):Code(Code(N))QQuote. The phases are therefore 1 at the variable premise, 2 at the splice conclusion, 1 at the inner quotation conclusion, and 0 at the outer quotation conclusion. Finally, Γx=(x:N@0) contains no declaration x:N@1. Hence the attempted addition premise Γx1x:N has no Q-Var derivation, so neither Γx1x+1:N nor Γx0quote(x+1):Code(N) is derivable.

Exercise 116.3.

The analytical Boolean macro has the two clauses unliftBooltttt,unliftBoolffff. The first pattern judgment checks the quoted pattern at code type Code2 and returns the empty pattern environment; the second has the same input and output. Consequently both branches check at 2, so the whole match has result type 2. A pattern y is rejected because its body contains the splice form itself, whereas the pattern judgment matches only the simply typed fragment with fix, not quotation or splicing forms. No pattern rule can derive a judgment for that body, regardless of the type assigned to y. Admitting it would require extending the pattern grammar, pattern reduction, and the source’s preservation proof.

Search the book

Type to search the local edition.