refining mechanized metatheory: subtyping for lf

37
Refining Mechanized Metatheory: Subtyping for LF William Lovas (with Frank Pfenning)

Upload: kris

Post on 07-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Refining Mechanized Metatheory: Subtyping for LF. William Lovas (with Frank Pfenning). LF: a framework for defining logics. (Harper, Honsell, and Plotkin, 1987, 1993) Dependently-typed lambda calculus Encode deductive systems and metatheory, in a machine-checkable way - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Refining Mechanized Metatheory: Subtyping for LF

Refining Mechanized Metatheory: Subtyping for LF

William Lovas(with Frank Pfenning)

Page 2: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '072

LF: a framework for defining logics

(Harper, Honsell, and Plotkin, 1987, 1993) Dependently-typed lambda calculus Encode deductive systems and metatheory,

in a machine-checkable way– e.g. a programming language and its type safety

theorem

Guiding principle: “judgements as types”

Page 3: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '073

Judgements as types

On paper: Syntax

– e ::= … Judgement

– Γ ⊢ e : τ Deduction

– D :: Γ ⊢ e : τ Proof checking

In LF: Simple type

– exp : type. Type family

– of : exp →tp → type. Well-typed term

– M : of E T Type checking

Page 4: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '074

Inclusion as subtyping

Some judgements have a natural notion of inclusion– all values are expressions– all odd natural numbers are positive

More interesting types means more interesting judgements!

Page 5: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '075

Example: natural numbers

nat : type.z : nat.s : nat → nat.double : nat → nat → type. % plus ruleseven : nat → type. % plus some rules…odd : nat → type. % plus some rules…dbl-even : ΠX:nat. ΠY:nat. % plus cases

double X Y → even Y → type.

Page 6: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '076

Example: nats using refinements

nat : type.z : nat.s : nat → nat.double : nat → nat → type. % plus ruleseven : nat → type. % plus some rules…odd : nat → type. % plus some rules…dbl-even : ΠX:nat. ΠY:nat. % plus cases

double X Y → even Y → type.

nat : type. even ≤ nat. odd ≤ nat.z : even.s : even → odd ∧ odd → even.double : nat → even → type. % plus rules

metatheorem checking problem⇝ typechecking problem!

Page 7: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '077

Example: nats using refinements

nat : type. even ≤ nat. odd ≤ nat.z : even.s : even → odd ∧ odd → even.double : nat → even → type.dbl-z : double z z.dbl-s : ΠX:nat. ΠY:even.

double X Y → double (s X) (s (s Y)).dbl-s : ΠX:nat. ΠY:even.

double X Y → double (s X) (s (s Y)).

Page 8: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '078

Example: the lambda calculus

exp : type.val : type.lam : (val → exp) → val.app : exp → exp → exp.$ : val → exp. lam (λx. app ($ x) ($ x)) : val

Intrinsic values encoding:

val ≤ exp.

lam (λx. app x x) : val

Page 9: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '079

Technology

Page 10: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0710

Adequacy

Does this really mean what I think it means?

Strategy: exhibit a compositional bijection between mathematical objects and canonical forms following judgements as types.– “canonical forms” are β-normal and η-long.

Page 11: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0711

Canonical forms method

Represent only the canonical forms.– β-normal: syntactically– η-long: through typing– Hereditary substitutions contract redexes

Simplifies metatheory, emphasizes adequacy Concurrent LF (Watkins, et al, 2003)

Page 12: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0712

LF typing

Bidirectional typing

Synthesis: Γ ⊢ R ⇒ A– elims: R ::= x | c | R N

Checking: Γ ⊢ N ⇐ A– intros: N ::= R | λx. N

Page 13: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0713

Checking

Γ ⊢ R ⇒ P’ P’ = P

Γ ⊢ R ⇐ P

Γ ⊢ N ⇐ A Key rule:

– base type, so atoms fully applied– the only appeal to type equality

Page 14: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0714

Checking… with subtyping!

Γ ⊢ R ⇒ P’ P’ ≤ P

Γ ⊢ R ⇐ P

Γ ⊢ N ⇐ A Easy to adapt!

– just change equality to subtyping– subtyping… only at base type?

Page 15: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0715

Intersections

Kind of like pairs, but the terms don’t change

Γ ⊢ N ⇐ A1 Γ ⊢ N ⇐ A2

Γ ⊢ N ⇐ A1 ∧ A2 Γ ⊢ N ⇐ ⊤

Γ ⊢ R ⇒ A1 ∧ A2

Γ ⊢ R ⇒ A1

Γ ⊢ R ⇒ A1 ∧ A2

Γ ⊢ R ⇒ A2

Page 16: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0716

Metatheory

Page 17: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0717

LF(R) as a logic

Entailment should be reflexive:– A ⊢ A

and transitive:– if A ⊢ B and B ⊢ C, then A ⊢ C

Page 18: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0718

LF(R) as a logic

Assume x is a proof of A. Is x a proof of A?– not necessarily!

x : A1 → A2 ⊬ x ⇐ A1 → A2

– have to η-expand:

x : A1 → A2 ⊢ λy. x y ⇐ A1 → A2

Page 19: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0719

LF(R) as a logic

Assume x is a proof of A. Can M ⇐ A stand in for x?– if substitution is hereditary?

[M/x]A N not obviously defined…

Page 20: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0720

Important principles

Substitutionif Γ, x:A ⊢ N ⇐ B and Γ ⊢ M ⇐ A ,then Γ ⊢ [M/x]A N ⇐ [M/x]A B .

Identityfor all A, Γ, x:A ⊢ ηA(x) ⇐ A .

“Substitution” morally a normalization proof

Page 21: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0721

More about subtyping

Page 22: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0722

Key rule:

Bidirectional: subtyping only at mode switch Canonical: mode switch only at base type

Subtyping

Γ ⊢ R ⇒ P’ P’ ≤ P

Γ ⊢ R ⇐ P

Γ ⊢ N ⇐ A

Page 23: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0723

Subtyping at higher types?

What happened to the structural rules? E.g.,

Distributivity?

A2 ≤ A1 B1 ≤ B2

A1 → B1 ≤ A2 → B2

(A → B1) ∧ (A → B2) ≤ A → (B1 ∧ B2)

Page 24: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0724

Subtyping at higher types!

Intrinsic subtyping: if A ≤ B and Γ ⊢ N ⇐ A , then Γ ⊢ N ⇐ B .

Equivalently: if A ≤ B then x:A ⊢ ηA(x) ⇐ B .– Just like the Identity principle!– … also the Substitution principle…

Usual rules are all sound in this sense.

Page 25: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0725

Subtyping at higher types!?

… and also complete! Theorem: if x:A ⊢ ηA(x) ⇐ B then A ≤ B . Also: if Γ ⊢ N ⇐ A implies Γ ⊢ N ⇐ B ,

then A ≤ B .

There are no new subtyping principles.

Page 26: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0726

Future work

Two directions:1. Extend LFR with Twelf stuff

• type reconstruction• unification• proof search

2. Extend LFR with CLF stuff• more type constructors• subtyping with linearity?

Page 27: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0727

Summary

Refinement types are a useful addition to LF. Canonical forms method is up to the task. Concentrating only on canonical forms and

bidirectional typing yields new insights into subtyping.

Page 28: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0728

secret slides

Page 29: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0729

Related work

Refinement types– Tim Freeman, Rowan Davies, Joshua Dunfield

Logical frameworks– Robert Harper, Furio Honsell, Gordon Plotkin – Frank Pfenning

Subtyping and dependent types– David Aspinall, Adriana Compagnoni

Page 30: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0730

LF syntax

Terms

Types

R ::= c | x | R NN ::= R | λx. N

P ::= a | P NA, B ::= P | Πx:A.B

atomic

normal

atomic

normal

no redexes

Page 31: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0731

Hereditary substitution

Substitution must contract redexes Example:

Indexed by type subscript for termination

Sometimes undefined:

[(λx. d x x) / y] y z = d z z

[M/x]A N

[(λx. x x) / y]A y y fails by induction on A

Page 32: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0732

Synthesis

Γ ⊢ R ⇒ Πx:A.B Γ ⊢ N ⇐ AΓ ⊢ R N ⇒ [N/x]A B

x:A ∈ ΓΓ ⊢ x ⇒ A

c:A ∈ ΣΓ ⊢ c ⇒ A

Γ ⊢ R ⇒ A

hereditary substitution

Page 33: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0733

Checking

Γ ⊢ R ⇒ P’ P’ = P

Γ ⊢ R ⇐ P

Γ, x:A ⊢ N ⇐ BΓ ⊢ λx.N ⇐ Πx:A.B

Γ ⊢ N ⇐ A

Page 34: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0734

Example: the lambda calculus

exp : type.val ⊏ exp.

lam : (exp → exp) → exp.app : exp → exp → exp.

value : exp → type.

lam : (val → exp) → val.

Page 35: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0735

s ⊏ a :: L

Γ ⊢ R ⇒ P’ P’ = P

Γ ⊢ R ⇐ PΓ ⊢ R ⇒ P’ P’ ≤

PΓ ⊢ R ⇐ P

Page 36: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0736

Subtyping

Key rule:

Bidirectional: subtyping only at mode switch Canonical: mode switch only at base type

subtyping only at base type!

Page 37: Refining Mechanized Metatheory: Subtyping for LF

LFMTP '0737

⇐Γ, x:A ⊢ M ⇐ B ≤ e abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ

Γ ⊢ R ⇒ A