incremental type checking for metaprograms
play

Incremental Type-Checking for Metaprograms Weiyu Miao Weiyu Miao 1 - PowerPoint PPT Presentation

Incremental Type-Checking for Metaprograms Weiyu Miao Weiyu Miao 1 / 20 Introduction to Metaprogramming Metaprogramming is a programming technique for generating and manipulating other programs. MetaML, MetaOCaml, Haskell Templates, C++


  1. Incremental Type-Checking for Metaprograms Weiyu Miao Weiyu Miao 1 / 20

  2. Introduction to Metaprogramming Metaprogramming is a programming technique for generating and manipulating other programs. MetaML, MetaOCaml, Haskell Templates, C++ Templates, etc Code generation using C++ templates. template <int N> int powN(int M) { return M * powN<N-1>(M); } template<> int powN<0>(int M) { return 1; } template int powN<2>(int); at compile time , it generates the code M * M * 1 Meta Evaluation Weiyu Miao 2 / 20

  3. Introduction to Metaprogramming (Cont.) Reflective metaprogramming 1 language syntax: The language in which the metaprogram is written is called the meta language. meta language e m :: = x | c m | λ x : τ m . e m | e m e m | < e c > | % e m | γ | e m → e m | → ? | dom e m | cod e m | typeof e m | == τ | · · · meta type τ m :: = type | γ | code γ | τ m → τ m The language of the programs that are manipulated is called the object language. object language e c :: = x | c c | λ x : e m . e c | e c e c | ∼ e m | · · · 1 supported by NSF, CCF-0702362 Weiyu Miao 3 / 20

  4. Introduction to Metaprogramming (Cont.) Partial evaluation. template <typename T, int U> T powerN (T m) { return m * powerN<T, U-1>(m) } let rec meta powerN = λ T : type. λ U : int. < λ m : T. m * (( ∼ powerN T (U-1)) m)> Weiyu Miao 4 / 20

  5. Introduction to Metaprogramming (Cont.) Type reflection. let meta rec type2string = λ t : type. if t == τ int then "int" else if t == τ bool then "bool" else if → ? t then let meta t1 = type2string (dom t) in let meta t2 = type2string (cod t) in t1 + "->" + t2 in let f = λ x : int. λ y : bool. x in (type2string (typeof <f>)) Weiyu Miao 5 / 20

  6. Introduction to Metaprogramming (Cont.) Dependent types. let meta fun = λ X : type. λ Y : bool. λ Z : type. < λ a : X. λ b : if Y then (int → int) else int. λ c : Z. if a then (c b) else b> in (fun bool false (int → int)) Weiyu Miao 6 / 20

  7. System F Style let meta fun = λ X : type. λ Y : bool. λ Z : type. < λ a : X. λ b : if Y then (int → int) else int. λ c : Z. if a then (c b) else b> (fun bool true ((int → int) → int → int))) (fun bool false (int → bool)) ✑ Type-checking at definition time – before meta evaluation. Suppose Γ = X : type, Y : bool, Z : type and ∆ = a : X, b : if Y then (int → int) else int, c : Z (1) Γ , ∆ � a : bool (2) Γ , ∆ � c : (typeof b) → (typeof b) Weiyu Miao 7 / 20

  8. System F Style (Cont.) drawback : precluding well-typed expressions. let meta fun = λ X : type. λ Y : bool. λ Z : type. < λ a : X. λ b : if Y then (int → int) else int. λ c : Z. if a then (c b) else b> (fun bool false (int → int)) (fun bool true ((int → int) → int → int)) Weiyu Miao 8 / 20

  9. C++ Templates Style let meta fun = λ X : type. λ Y : bool. λ Z : type. < λ a : X. λ b : if Y then (int → int) else int. λ c : Z. if a then (c b) else b> (fun bool true ((int → int) → int → int)) · · · (fun bool false (int → bool)) · · · ✑ Type-checking at instantiation time – after meta evaluation 2 . ✦ (1) < λ a : bool. λ b : int → int. λ c : (int → int) → int → int. if a then (c b) else b> ★ (2) < λ a : bool. λ b : int. λ c : int → bool. if a then (c b) else b> 2 Ronald Garcia. Static Computation and Reflection. PhD thesis, Indiana University, September 2008. Weiyu Miao 9 / 20

  10. C++ Templates Style (Cont.) drawback : inefficiency. let meta fun = λ X : bool. < λ a 0 : if X then int else bool. λ a 1 : e m λ a 2 : e m · · · λ a n : e m 1 . 2 . n . let x = e m in /* Θ ( e m ) = 2 n */ if a 0 then · · · else · · · > fun true = < λ a 0 : int. λ a 1 : v m a 2 : v m · · · a n : v m 1 . 2 . n . let x = v m in if a 0 then · · · else · · · > Weiyu Miao 10 / 20

  11. Idea : Incremental Type-Checking System F metaprogram e e' e'' . . . . . . type check . . meta evaluation . . . . . . . . . . . . . . . . e' ... ' metavalue v C++ Templates Weiyu Miao 11 / 20

  12. Idea : Incremental Type-Checking (Cont.) λ λ λ a : e m . a : e m ... a : e m . e 1 1 2 2 n n λ λ λ a : v m . a : e m ... a : e m . e 1 1 2 2 n n λ λ λ a : v m . a : v m ... a : e m . e 1 1 2 2 n n . . . . . . type check . . . . meta evaluation . . . . . . . . . . . . . . λ m λ m λ m a : v . a : v ... a : v . e 1 1 2 2 n n λ λ λ m m m a : v . a : v ... a : v . v 1 1 2 2 n n Weiyu Miao 12 / 20

  13. Incremental Type-Checking Based on Unification type check e 0 constraint set C 0 unify extra typing info. e 1 C 1 unify extra typing info. e 2 C 2 unify meta evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . unify extra typing info. e n C n unify metavalue v solution C Weiyu Miao 13 / 20

  14. Example let meta fun = λ X : type. λ Y : bool. λ Z : type. < λ a : (X) α . λ b : (if Y then int → int else int) β . λ c : (Z) γ . if a then (c b) else b> Suppose Γ = X : type, Y : bool, Z : type and ∆ = a : α , b : β , c : γ a ′ is fresh Γ , ∆ ⊢ c : γ | {} Γ , ∆ ⊢ b : β | {} Γ , ∆ ⊢ ( c b ) : a ′ | { γ = β → a ′ } Γ , ∆ ⊢ ( c b ) : a ′ | { γ = β → a ′ } Γ , ∆ ⊢ a : α Γ , ∆ ⊢ b : β | {} Γ , ∆ ⊢ ( if a then ( c b ) else b ) : a ′ | { α = bool , γ = β → a ′ , a ′ = β } unify ( { α = bool , γ = β → a ′ , a ′ = β } ) = { α = bool , γ = β → β, a ′ = β } Weiyu Miao 14 / 20

  15. Example (Cont.) let meta fun = λ X : type. λ Y : bool. λ Z : type. < λ a : (X) α . λ b : (if Y then int → int else int) β . λ c : (Z) γ . if a then (c b) else b> C = { α = bool , γ = β → β, a ′ = β } fun int false (int → int) = < λ a : (int) α . λ b : (if false then int → int else int) β . λ c : (int → int) γ . if a then (c b) else b> C ′ = [ int / α ] C = { int = bool , γ = β → β, a ′ = β } Weiyu Miao 15 / 20

  16. A More Convincing Example let meta fun = λ X : bool. < λ a 0 : (if X then int else bool) α . λ a 1 : ( e m 1 ) β . λ a 2 : ( e m 2 ) γ . · · · λ a n : ( e m n ) δ . let x = e m in /* Θ ( e m ) = 2 n */ if a 0 then · · · else · · · > C = { α = bool , · · · } fun true = < λ a 0 : (int) α . λ a 1 : ( e m 1 ) β . λ a 2 : ( e m 2 ) γ . · · · λ a n : ( e m n ) δ . let x = e m in /* Θ ( e m ) = 2 n */ if a 0 then · · · else · · · > C ′ = [ int /α ] C = { int = bool , · · · } Weiyu Miao 16 / 20

  17. Dealing with Let-Polymorphism let meta fun = λ X : bool. < λ a 0 : (if X then int else bool) α . · · · > in ∼ (fun true) 100 /* α = int */ → /* α _ 1 = int */ ∼ (fun false) true /* α = bool */ → /* α _ 2 = bool */ Weiyu Miao 17 / 20

  18. Dealing with Error Messages (future work) Because of unification based approach, type errors are difficult to be tracked. Idea: Blame tracking approach 3 . 3 Jeremy Siek, Ronald Garcia, and Walid Taha. Exploring the Design Space of Higher-Order Casts. European Symposium on Programming, 2009 Weiyu Miao 18 / 20

  19. Conclusion (1) Compared with System F style, our approach will not preclude well-type expressions of object language. (2) Compared with C++ templates style, our approach can discover type errors earlier and save compile time by avoiding unnecessary meta-evaluation. Weiyu Miao 19 / 20

  20. THANK YOU Weiyu Miao 20 / 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend