Contextual Modal Type Theory YUITO MURASE THE UNIVERSITY OF TOKYO - - PowerPoint PPT Presentation

contextual modal type theory
SMART_READER_LITE
LIVE PREVIEW

Contextual Modal Type Theory YUITO MURASE THE UNIVERSITY OF TOKYO - - PowerPoint PPT Presentation

Kripke-Style Contextual Modal Type Theory YUITO MURASE THE UNIVERSITY OF TOKYO Agenda Background Logic Type System Future Plan/Related Work Background: Syntactical Metaprogramming Extend the syntax of programming languages


slide-1
SLIDE 1

Kripke-Style Contextual Modal Type Theory

YUITO MURASE THE UNIVERSITY OF TOKYO

slide-2
SLIDE 2

Agenda

 Background  Logic  Type System  Future Plan/Related Work

slide-3
SLIDE 3

Background: Syntactical Metaprogramming

 Extend the syntax of programming languages

 Macros in Lisp Family  Template Haskell  Scala Macros  … etc.

 They are not type-safe

 Well-typed code with syntactic extension can extend to ill-typed code  We want type theory for syntactical metaprogramming

 Especially logical foundation (via the Curry-Howard Isomorphism)

slide-4
SLIDE 4

Example: or macro

 Quasi-quotation: basic construct for code generation

 Lisp-family, Template Haskell

 Macros are functions from code to code

 Including open code

(defmacro or (x y) `(if ,x true ,y)) (or a (is-zero b)) => (if a true (is-zero b)

slide-5
SLIDE 5

Example: or macro

 Quasi-quotation: basic construct for code generation

 Lisp-family, Template Haskell

 Macros are functions from code to code

 Including open code

(defmacro or (x y) `(if ,x true ,y)) (or a (is-zero b)) => (if a true (is-zero b)

slide-6
SLIDE 6

Example: or macro

 Quasi-quotation: basic construct for code generation

 Lisp-family, Template Haskell

 Macros are functions from code to code

 Including open code

(defmacro or (x y) `(if ,x true ,y)) (or a (is-zero b)) => (if a true (is-zero b)

slide-7
SLIDE 7

(bind (= x x)) => (lambda (x) (= x x))

Example: Binding Manipulation

 Generate a new binding  Access to free variables in code

(defmacro bind (body) `(lambda (x) ,body))

slide-8
SLIDE 8

Background: Modal Type Theory

 Type theory that corresponds to modal logic

 The Curry-Howard Isomorphism

 □A

 Logic : proposition for “A is valid”  Type theory : type of “closed code of type A”

 Some formulation for modal logic

 Dual context formulation  Kripke-style formulation

slide-9
SLIDE 9

Dual-Context Formulation

 Proposed by Pfenning and Davies[2001]  Based on the idea of categorical judgment  Hypothetical judgment have two-levels

 Object-level and meta-level  Syntax includes meta-variables

 Corresponds to S4 modal logic

  • r := λx:□bool. λy:□bool.

let box u = x in let box v = y in `(if u then true else v)

slide-10
SLIDE 10

Kripke-Style Formulation

 Proposed by Martini and Masini(1996), Pfenning and Wong(1995)  Hypothetical Judgment have context stack

 Justified by Kripke’s multiple-world semantics(1963)  Namespace for variables are uniform

 Syntax have quasi-quotaion  4 variations: K, T, K4, S4

  • r := λx:□bool. λy:□bool.

`(if ,x then true else ,v)

slide-11
SLIDE 11

Contextual Modal Type Theory

 Introduced by Nanevski et al(2007)  Contextual modality : [Γ]A

 Logic: A is valid under the context Γ  Kripke Semantics: For any next world where Γ holds, A also holds  Type: Code with free variables

 Generalization of dual-context modal calculi  Syntax have meta-variables and explicit substitution bind := λx:[A]bool. let box u = x in `(λy:A.u[y]) bind `<x:A>(x==x) → λx:A(x==x)

slide-12
SLIDE 12

What we want?

 Quasi-quotation → Kripke-style formulation  The axiom T is not necessary → Kripke-style formulation

 T corresponds to run-time code evaluation

 Binding manipulation → Contextual modal type  ⇒ Kripke-style contextual modal type theory

Dual-context Kripke-style Modal Pfenning and Davies[2001] Martini and Masini[1996] Pfenning and Wong[1995] Contextual Nanevski et al[2007] HERE!

slide-13
SLIDE 13

Kripke-style Contextual Modal Type Theory

 Another Contextual Modal Type Theory  Generalization of Kripke-style modal type theory

 Kripke-style formulation  Quasi-quotation  Capable of binding-manipulation  Four variations (correspondence to K, T, K4, S4)

bind := λy:[A]bool. `<>(λx:A. ,<x>y)

slide-14
SLIDE 14

Agenda

 Background  Logic  Type System  Future Plan/Related Work

slide-15
SLIDE 15

Kripke-style Hypothetical Judgment

 Proposed by [Pfenning and Wong, 1995]  Contexts form a stack  Correspondence to Kripke’s multiple-world semantics(1963)

 The stack can be regarded as a sequence of worlds

𝑥𝑜 𝑥1 𝑥𝑜−1

slide-16
SLIDE 16

Kripke-style Hypothetical Judgment

 Substitution Principle  Reflexive Principle – assuming reflexivity  Transitive Principle – assuming transitivity  Four Variations

Reflexive ✓ ✓ Transitive ✓ ✓ K T K4 S4

slide-17
SLIDE 17

Deduction Rules

Rightmost Context Rest of the Context Stack

slide-18
SLIDE 18

Deduction Rules

𝑥3 𝑥1 𝑥2 𝑥3 𝑥2

slide-19
SLIDE 19

Deduction Rules

𝑥2 𝑥2 𝑥1 𝑥2 𝑥1

slide-20
SLIDE 20

Deduction Rules

slide-21
SLIDE 21

Examples

1. ⊢𝐿 [C](𝐵 → 𝐶) → 𝐷 𝐵 → 𝐷 𝐶 2. ⊢𝑈 []𝐵 → 𝐵 3. ⊢𝐿4 𝐶 𝐵 → 𝐷 𝐶 𝐵 4. ⊢𝐿 []𝐵 → 𝐶, 𝐷 𝐵 5. ⊢𝐿 𝐶, 𝐷 𝐵 → 𝐷, 𝐶 𝐵 6. ⊢𝐿 𝐶, 𝐶 𝐵 → 𝐶 𝐵 7. ⊢𝐿 𝐶 𝐵 → 𝐷, 𝐸 𝐶 → 𝐷, 𝐸 𝐵 8. ⊢𝐿 𝐶 𝐵 → [] 𝐶 → 𝐵 9. ⊢𝐿[] 𝐶 → 𝐵 → 𝐶 𝐵

slide-22
SLIDE 22

Examples

1. ⊢𝐿 [C](𝐵 → 𝐶) → 𝐷 𝐵 → 𝐷 𝐶 2. ⊢𝑈 []𝐵 → 𝐵 3. ⊢𝐿4 𝐶 𝐵 → 𝐷 𝐶 𝐵 4. ⊢𝐿 []𝐵 → 𝐶, 𝐷 𝐵 5. ⊢𝐿 𝐶, 𝐷 𝐵 → 𝐷, 𝐶 𝐵 6. ⊢𝐿 𝐶, 𝐶 𝐵 → 𝐶 𝐵 7. ⊢𝐿 𝐶 𝐵 → 𝐷, 𝐸 𝐶 → 𝐷, 𝐸 𝐵 8. ⊢𝐿 𝐶 𝐵 → [] 𝐶 → 𝐵 9. ⊢𝐿[] 𝐶 → 𝐵 → 𝐶 𝐵

slide-23
SLIDE 23

Examples

1. ⊢𝐿 [C](𝐵 → 𝐶) → 𝐷 𝐵 → 𝐷 𝐶 2. ⊢𝑈 []𝐵 → 𝐵 3. ⊢𝐿4 𝐶 𝐵 → 𝐷 𝐶 𝐵 4. ⊢𝐿 []𝐵 → 𝐶, 𝐷 𝐵 5. ⊢𝐿 𝐶, 𝐷 𝐵 → 𝐷, 𝐶 𝐵 6. ⊢𝐿 𝐶, 𝐶 𝐵 → 𝐶 𝐵 7. ⊢𝐿 𝐶 𝐵 → 𝐷, 𝐸 𝐶 → 𝐷, 𝐸 𝐵 8. ⊢𝐿 𝐶 𝐵 → [] 𝐶 → 𝐵 9. ⊢𝐿[] 𝐶 → 𝐵 → 𝐶 𝐵

slide-24
SLIDE 24

Examples

1. ⊢𝐿 [C](𝐵 → 𝐶) → 𝐷 𝐵 → 𝐷 𝐶 2. ⊢𝑈 []𝐵 → 𝐵 3. ⊢𝐿4 𝐶 𝐵 → 𝐷 𝐶 𝐵 4. ⊢𝐿 []𝐵 → 𝐶, 𝐷 𝐵 5. ⊢𝐿 𝐶, 𝐷 𝐵 → 𝐷, 𝐶 𝐵 6. ⊢𝐿 𝐶, 𝐶 𝐵 → 𝐶 𝐵 7. ⊢𝐿 𝐶 𝐵 → 𝐷, 𝐸 𝐶 → 𝐷, 𝐸 𝐵 8. ⊢𝐿 𝐶 𝐵 → [] 𝐶 → 𝐵 9. ⊢𝐿[] 𝐶 → 𝐵 → 𝐶 𝐵

slide-25
SLIDE 25

Agenda

 Background  Logic  Type System  Future Plan/Related Work

slide-26
SLIDE 26

Kripke-style Contextual Modal Type Theory

 Correspond to KCML under the Curry-Howard Isomorphism

 Proposition ⇔ Type  Derivation Tree ⇔ Program

quotation unquotation Contextual Modal Type

slide-27
SLIDE 27

Typing Rules

slide-28
SLIDE 28

Quote

 A binding form  Term representation for hypothetical judgment

 Γ : a list of assumptions  M : derivation tree

 Can be seen as “code with free variables”

 Γ : a list of free variables  M : body of code

slide-29
SLIDE 29

Unquote

 An application form  Instantiate the quoted hypothetical judgment  Can be seen as “evaluation of the code through 𝑚-stages”

 𝑂1 ⋯ 𝑂𝑜 are the top-level definitions of the free variables  𝑚 = 0 → run-time code evaluation e.g. eval function

slide-30
SLIDE 30

Substitution

𝑂1/𝑦1 ⋯ 𝑂𝑜/𝑦𝑜 𝑚

 Substitute free variables at level 𝑚

(Substitution Lemma)

Ψ; Γ

𝑚; ⋯ ; Γ 1⊢M:T

slide-31
SLIDE 31

Level Substitution

↑𝑚

𝑜

 Merge the 𝑚th context with the 𝑚 + 1th context (when 𝑜 = 0)  Insert 𝑜 − 1 context upon the 𝑚th context (when n ≥ 1)

(Level Substitution Lemma)

slide-32
SLIDE 32

Reduction/Expansion Rules

 β-Reduction  η-Expansion

slide-33
SLIDE 33

Example: or macro

(defmacro or (x y) `(if ,x true ,y))

  • r := λx:[A]bool. λy:[A]bool.

`<w:A>(if ,1<w>(x) then true else ,1<w>(y))

slide-34
SLIDE 34

Example: or macro

(or (= x x) false) => (if (= x x) true false) ,<w>(or `<w:A>(w=w) `<w:A>(false)) => if w = w then true else false

slide-35
SLIDE 35

Example: binding manipulation

(defmacro bind (y) `(lambda (x) ,y)) bind := λy:[A]bool. `<>(λx:A.,1<x>y)

slide-36
SLIDE 36

Example: binding manipulation

(bind (= x x)) => (lambda (x) (= x x)) ,<>(bind (`<x:A>(x=x))) => λx:A. x=x

slide-37
SLIDE 37

Agenda

 Background  Logic  Type System  Future Plan/Related Work

slide-38
SLIDE 38

Future Plan

 Motivation: Reasoning syntactical metaprogramming  Develop stronger type theory

 Environment Polymorphism

 Develop a programming languages with type-safe syntactical metaprogramming  Other problems

 Confluency and Strong normalization  Categorical Semantics

slide-39
SLIDE 39

Environment Polymorphism

 The type of or macro is too specific

 []bool→ []bool→ []bool  [A]bool→ [A]bool→ [A]bool  [B, C]bool→ [B, C]bool→ [B, C]bool

 Quantify the environment

 ∀γ.[γ]bool→[γ]bool→[γ]bool

 Under construction

slide-40
SLIDE 40

Related work: Linear Temporal Type Theory

 λ○ [Davies, 1996] Correspond to linear temporal logic

 Treats open code  Code generation is essentially hygienic  Cannot express the bind macro

λ○[Davies,1996] Environment Classifiers [Taha&Nielsen, 2003] MacroML[Ganz et al, 2001] Dual-Context Kripke-Style CMTT KCMTT AIM[Moggi et al., 1999] λ○□[Yuse&Igarashi, 2006] K,T,K4,S4 Modal Type Theory Linear Temporal Type Theory

slide-41
SLIDE 41

Related work: λ𝑝𝑞𝑓𝑜

𝑡𝑗𝑛

 Proposed by Kim et al.(2006)

 “A polymorphic modal type system for lisp-like multi-staged languages”

 Extend modal types to have context

 □(Γ ▷ 𝐵)  Programming language with imperative features

 Undesirable nature as typed lambda calculi

 α-renaming is restricted  Reduction rule is restricted

slide-42
SLIDE 42

Summary

 We want type theory that reasons syntactical metaprogramming  We proposed Kripke-style contextual modal type theory

 Lisp-like quasi-quotation syntax  Contextual modal type  4 variants(K, T, K4, S4)  Proved subject reduction of KCMTT

 Future work

 Prove confluence and strong normalization  Develop the type theory  Develop the programming language