Title: Explicit Contexts in LF
1Explicit Contexts in LF
- Karl Crary
- Carnegie Mellon University
Workshop on Mechanized Metatheory, 9/21/06
2The LF methodology
- Construct isomorphisms between
- Syntactic classes and LF types
- Expressions and LF terms(of appropriate type)
- Judgements and LF types
- Derivations and LF terms(of appropriate type)
3The LF methodology
- Isomorphisms must commute with substitution.
- To do so
- Identify OL variables with LF variables.
- Identify OL assumptions with LF assumptions.
- Consequently, identify OL contexts with
(fragments of) LF contexts.
4LF example Syntax
tp type. exp type. o tp. arrow
tp -gt tp -gt tp. b exp. lam tp -gt
(exp -gt exp) -gt exp. app exp -gt exp -gt exp.
5LF example Semantics
of exp -gt tp -gt type. of/b of b o. of/lam
of (lam A (x M x)) (arrow A B)
lt- (x of x A -gt of (M x) B). of/app
of (app M N) B lt- of M (arrow A B)
lt- of N A.
6The problem
- Contexts are implicit.
- Cannot be manipulated by proofs.
- Can be a problem for theorems involving a
distinguished bound variable.
7Distinguished bound variable
- If of M Aand (x of x A -gt of (N x) B)then
of (N M) B - In Twelf syntax
subst of M A -gt (x of x A -gt of (N x) B)
-gt of (N M) B -gt type. mode subst D1 D2
-D3.
8Important caveat
- Theres no reason to do this!
- Substitution lemma is free in LF.
- If D1 of M Aand D2 (x of x A -gt of (N x)
B)then D2 M D1 of (N M) B - Illustrative example, not a motivating one.
9Motivating examples
- Substitution lemma for languages with different
judgements on left and right - Sequent calculus, imperative type systems
- Narrowing in F-sub (Poplmark challenge)
- Functionality
- Defined notions of substitution
- Hereditary substitution
- Linear and modal logic
10Substitution theorem
- If of M Aand (x of x A -gt of (N x) B)then
of (N M) B - By induction on the second argument.
11A proof case
- SupposeD1 of M AD2 x dof x A
of/lam (y e D x d y e) x of x A
-gt of (lam B (y N x y)) (arrow B
C)D x of x A -gt y of y B -gt of (N x
y) C
12A proof case
- Let yexp and eof y B be arbitrary.
- Thereforex dof x A D x d y e x of x A
-gt of (N x y) C - By induction there existsD y e of (N M y) C
- Thereforeof/lam D of (lam B (y N M y))
(arrow B C)
13A proof case in Twelf
- subst D1 (x dof x A of/lam
(y eof y B D x d y e)) (of/lam D') lt-
(y eof y B subst D1 (x
dof x A D x d y e) (D' y e of (N M
y) C)).
14Assumption permutation
- Proof permutes x d with y e.
- No room for bindings between distinguished
variable and its scope. - Undistinguished variables go in context.
- In essence, the distinguished variable must
appear last. - Permute assumptions to preserve this condition.
15Uh oh!
- With dependent types, we cannot permute
assumptions. - When es type depends on x, it cannot be pulled
outside.
16The problem
- When
- doing an inductive proof in Twelf
- that involves a distinguished bound variable,
- and the setting includes dependent types,
- You have a problem
- Cannot keep the designated bound variable last.
17Explicit contexts
- Make the context into an explicit object that the
proof can manipulate. - This allows us to place the variable of interest
anywhere in the context. - Proof technique only!
- No change to LF or Twelf.
- No change to syntax!
- Still using higher-order abstract syntax.
- Can convert from and to implicit contexts.
18Methodology
- Syntax is still entirely higher-order.
- Give two versions of the semantics.
- Implicit and explicit context.
- Convert derivations to use explicit contexts when
necessary.
19Contexts
ctx type.nil ctx.cons ctx -gt exp -gt tp
-gt ctx.
20First cut lookup
lookup ctx -gt exp -gt tp -gt type.lookup/hit
lookup (cons G X A) X A.lookup/miss lookup
(cons G Y _) X A lt- lookup G X A.
21First cut semantics
ofe ctx -gt exp -gt tp -gt type. ofe/var ofe
G X A lt- lookup G X A. ofe/closed
ofe G M A lt- of M A.
22First cut semantics
ofe/lam ofe G (lam A (x M x))
(arrow A B) lt- (xexp ofe (cons G x
A) (M x) B). ofe/app ofe G (app M N) B lt-
ofe G M (arrow A B) lt- ofe G N A.
23Problem bad contexts
- Contexts are merely association lists of terms
and types. - Syntax permits
- Binding of non-variables.
- Multiple bindings of a single variable.
- Need a context formation judgement
- Each term should be a distinct variable.
24Context formation
- Distinguish variables using a hypothetical
judgement. - Also assigns an ordering to variables.
- Context formation judgement
- Only variables may appear.
- Variables must be ordered.(Hence, no duplicates.)
25Variable ordering
isvar exp -gt nat -gt type. Assumption "isvar
X I" indicates (1) X is a variable, and (2) x
carries order stamp I. precedes exp -gt exp
-gt type. precedes/i precedes X Y lt-
isvar X I lt- isvar Y J lt- lt I J.
26Context formation
bounded ctx -gt exp -gt type.bounded/nil
bounded nil X lt- isvar X
_.bounded/cons bounded (cons G Y _) X
lt- precedes Y X lt- bounded
G Y. ordered ctx -gt type.ordered/nil
ordered nil.ordered/cons ordered (cons G X _)
lt- bounded G X.
27Lookup
lookup ctx -gt exp -gt tp -gt type.lookup/hit
lookup (cons G X A) X A lt- bounded G
X. lookup/miss lookup (cons G Y _) X A lt-
bounded G Y lt- lookup G X A.
- Lemma if lookup G X A then ordered G.
28Semantics
ofe/closed ofe G M A lt- of M A lt-
ordered G. ofe/lam ofe G (lam A (x M x))
(arrow A B) lt- (xexp isvar x I
-gt ofe (cons G x A) (M x) B).
29Usage
- When combined, these theorems allow us to do
proofs for the implicit system. - Convert to explicit form.
- Perform the desired proof.
- Convert back to implicit form.
30Substitution theorem
- Before
- If ? ? M Aand ?, xA ? N Bthen ? ? M/x N
B - Now
- If ?1 ? M Aand ?1, xA, ?2 ? N Bthen ?1, ?2
? M/x N B
31Substitution theorem in Twelf
- If (x append (cons G1 x A) G2 (G
x))and append G1 G2 Gand ofe G1 M Aand (x
isvar x I -gt ofe (G x) (N x) B)then
ofe G (N M) B
32Context Lemmas
- If lookup G X A then X is not a lambda or
application. - That is, contexts bind only variables.
- If (x append (cons G1 x A) G2 (G
x))and (x isvar x I -gt lookup (G x) x
B)then tp-eq A B. - That is, contexts bind distinct variables.
33Translation to implicit form
- If ofe nil M Athen of M A
- Proof is not very hard.
34Translation to explicit form
- If of M Athen ofe nil M A
- Proof is tricky.
- This is the enabling technical achievement.
35Cut elimination
- Main lemma is a form of cut elimination.
- Cut explicit-context lookup againstimplicit-con
text of assumption. - Prove simultaneously for cuts into of and ofe.
- If (x of x A -gt of (M x) B)and (x isvar x I
-gt lookup (G x) x A)then (x isvar x I
-gt ofe (G x) (M x) B)
36Conclusion
- General technique for proofs involving
- A distinguished bound variable
- Dependent types
- Used in type safety proof for SML IL.
- See Daniel Lees talk this afternoon.
- Not an extension to LF.
- Not a new representation technique
- Still use higher-order syntax and judgements.