CPS CPS Continuation Passing Style Plotkin callbyvalue CPS
CPS 変換 • ラムダ式を CPS に変換する (Continuation Passing Style) Plotkin による call-by-value な CPS 変換の定義 [[x]] = λk. k x [[λx. t]]= λk. k (λx. [[t]]) [[t 1 t 2]] = λk. [[t 1]] (λv 1. [[t 2]] (λv 2. v 1 v 2 k)) G. D. Plotkin. “Call-by-name, call-by-value and the lambda-calculus. ” 1975 5
ラムダ計算の定式化 • 色々なやり方がある – Named (普通の書き方) • λx. λy. x z y – De Bruijn index • λ. λ. 1 2 0 – Locally nameless • λ. λ. 1 z 0 変数を名前で区別 変数を番号で区別 Named + de Bruijn index 置換を扱いやすい N. G. de Bruijn. “Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser theorem. ” 1972. B. Aydemir, et al. “Engineering formal metatheory. ” 2008. 11
依存型 • 型の中に項がある ⊢ nil : List 0 ⊢ cons : Πn: Nat. String → List n → List (n+1) ⊢ cons 0 “foo” nil : List 1 リストの型 List n は リストの長さを表す項 n を含んで いる R. Harper, et al. “A framework for defining logics. ” 1993. 21
26
ラムダ計算の定式化 • Higher-order abstract syntax (HOAS) – 定式化する言語の変数で 定式化される言語の変数を表す – F. Pfenning and C. Elliot. “Higher-order abstract syntax. ” 1988. • Nominal logic – Alpha-equivalent な項を構文的に等しいとして 公理化する – Andrew M. Pitts. “Nominal logic, a first order theory of names and binding. ” 2003. 27
CPS 変換の検証 • CPS 変換の依存型の型保存の証明 – Call-by-name な CPS 変換 – Gilles Barthe et al. “CPS transformations and applications: The cube and beyond. ” 1999. – 型の中の項を型に変換 – Zhong Shao et al. “A type system for certified binaries. ” 2005. 28
Certified Compiler • Comp. Cert – C のサブセットから Power. PC アセンブリへ – http: //compcert. inria. fr/ • Chlipala’s compiler – 単純型付きラムダ計算から抽象アセンブリへ – Adam Chlipala. “A certified type-preserving compiler from lambda calculus to assembly language. ” 2007. 29
Theorem cps_term_preserves_typing : forall (Cs : sctxt) (T : stype) (t : sterm), sterm_typing Cs t T -> forall Cc : cctxt, cterm_typing (cps_ctxt Cs) Cc (cps_term t) (! ! cps_type T). Proof. induction 1 as [ Cs x T H 1 H 2 | Cs l TA TR t H 1 IH 1 | Cs TA TR tf ta H 1 IH 1 H 2 IH 2 ]; intro Cc; simpl. (* case stt_fvar *) apply ctt_cabs with (l : = domain_of_cctxt Cc). intros x 1 H 3. unfold open_cterm_c; simpl open_cterm_c_n. apply ctt_app with (TA : = cps_type T). apply ctt_cfvar; auto_in constructor. apply ctt_sfvar. apply cps_ctxt_preserves_in, H 1. 31
証明の行数 Unsorted named Definition Two-sorted named de Bruijn index Two-sorted loc. nameless 194 230 160 251 Name lemmas 57 57 0 12 List manipulation 134 12 18 151 Proof of type preservation 120 83 57 124 Total 505 382 235 538 32
- Slides: 36