• 沒有找到結果。

以型態導向方法發展多型剖面的織入技術與應用 (II)

N/A
N/A
Protected

Academic year: 2021

Share "以型態導向方法發展多型剖面的織入技術與應用 (II)"

Copied!
5
0
0

加載中.... (立即查看全文)

全文

(1)

摘要 近幾年來關於剖面程式設計的各項研究 蓬勃發展,從剖面程式語言、開發工具、 剖面設計與分析,到剖面的理論基礎,都 可 見 到 了 許 多 的 研 究 成 果 陸 續 發 表 出 來 。 這 些 研 究 成 果 大 多 集 中 在 探 討 像 AspectJ 這類以物件導向為基礎的剖面語 言的各個面向,因這類語言都不具備參數 式 多 型 的 功 能 , 所 以 關 於 多 型 剖 面 (Polymorphic/generic aspects)的研究尚屬 起步階段。本計畫以型態導向的方法發展 一套多型剖面的織入技術,並設計一具剖 面機制的函數式程式語言, AspectFun,來 實驗我們的方法。第一年進度順利,目前 正在透過定義這個語言的語義來驗證我 們的靜態織入方法的正確性。此外,我們 正在開發 AspectFun 的編譯環境與發展動 態切點(Cflow)的優化分析方法。 關鍵詞:剖面導向程式設計,多型剖面, 型態推演,織入,多型型態系統 Abstract

Introducing aspect orientation to a polymorphically typed functional language strengthens the importance of type-scoped advices; i.e., advices with their effects harnessed by type constraints. As types are typically treated as compile time entities, it is highly desirable to be able to perform static weaving to determine at compile time the chaining of type-scoped advices to their associated join points. In the first year of this project, we develop a compilation model, as well as its implementation, that supports static type inference and static weaving of programs in an aspect-oriented polymorphically typed lazy functional language, AspectFun. We present a type-directed weaving scheme that coherently weaves type-scoped advices into the base program at compile time. We shall prove the correctness of the static weaving with respect to the operational semantics of

AspctFun and develop analysis algorithms for optimizing control-flow based pointcuts (such as Cflow) that can improve the efficiency of woven code.

Keywords: Aspect-oriented programming, polymorphic aspect, type inference, static weaving, functional languages

1. 前言 近幾年來關於剖面程式設計的各項研 究蓬勃發展,從剖面程式語言、開發工 具、剖面設計與分析,到剖面的理論基 礎,都可見到了許多的研究成果陸續發表 出來。這些研究成果大多集中在探討像 AspectJ 這類以物件導向為基礎的剖面語 言的各個面向,因這類語言都不具備參數 式 多 型 的 功 能 , 所 以 關 於 多 型 剖 面 (Polymorphic/generic aspects) 的研究尚屬 起步階段。 為了進一步分析多型剖面的特性,我 們和新加坡國立大學的 Khoo, Siau Cheng 教授合組了一個研究小組,針對如何提供 完整的多型剖面的理論基礎進行研究,希 望透過發展一新穎的 type system 來提供 更完整與深入的多型剖面織入的理論與 技術。在過去一年中,我們這個研究小組 透過網路進行密集的討論,並利用在暑假 期間訪問新加坡進行密集腦力激盪,漸漸 地對這個研究議題提出了一些與其他研 究 團 隊 不 同 的 解 決 途 徑 。 我 們 採 用 type-directed 的 方 式 , 提 出 type-scoped advice 以及 advised types 的機制來刻畫 static weaving 的需求,(例如,範例一中的 函 數 f 的 型 態 以 forall a. (h:a->a).a->a 表示,其中的(h:a->a) 即為一 advice predicate,表示呼叫執行 函數 f 時,必須要織入對函數 h 的在此型態 上相容的 advice。) 並且制定了一套基於 type inference 的 translation rules 來訂定

(2)

static weaving 的運作與語義: 們這套規則將原來含有advice(aspect) 宣告 2. 究目的 目的將以目前的這套 type infer 向的形式推理 (2) 式 系 統 制 定 出 type (3) 建置一雛形系統來進一 (4) 檢視如何在 Java Generics 3. 獻探討 探討主要是起始於函數式 語言 東 京大 . 研究方法 我 的程式e,在經過type inference後,將 轉譯成不含advice的程式e’,其中具有型 態中advice predicates的函數將增加 搭配的advice parameters;而advice 將轉成一般函數,並依其所advise的相關 函 數 在 呼 叫 時 的 type 資 訊 , 織 入 advice chain。以<h, {n1, ..nk}>符號表示函數h的執 行必須先經過這些advice: n1,…, nk的諮 詢。 研 本計畫的 ence/translation rules 為基礎,以兩年 的時間完成整個理論基礎與雛型系統,並 在一些選定的領域發展其應用。具體而 言,本計畫有以下目的: (1) (繼續)發展這套型態導 系統,以 model 多型剖面與其靜態織 入的方法。 替 這 套 形 inference/translation 演算法並証明其 正確性。 依這套理論 步探索這中間的關於多型剖面的設 計課題。 依這套理去 的基礎上,訂出適當的多型剖面的理 論與機制。 文 多型剖面的 。Walker et al[25]提出了一個 將 join point 視 為 first-class value的形式演算系統,並從而設計了一 簡化的 AO 函數式語言。但這個語言不支 援多型剖面也無法處理 higher-order 函數。他們接者在今年又發表了新的可以 支 援 多 型 剖 面 與 cflow-like 機 制 的 PolyAML[33]語言雛形,其中用到了極 為複雜的 local type inference 來 推 導 多 元 型 態 。 不 過 他 們 在 處 理 non-polymorphic aspects方面用了 需 要 動 態 型 態 檢 查 的 機 制 (type-case) , 所 以 無 法 在 compile-time 就完成型態檢查與剖面 織 入 , 此 外 , PolyAML 也 不 能 支 援 higher-order函數的剖面織入。 有別於 PolyAML 從理論角度切入, 學 Masuhara et al[34]則從多型 剖面的實驗平台著手。他們以 Object Caml語言為基礎,仿 AspectJ 的機制在 Caml 上定義了相當完整的靜態與動態剖 面機制,並支援多型剖面。他們的目的在 於透過此雛型語言累積多型剖面等方面 的經驗,所以尚未發展此語言的語意規 則。此外,Aspectual Caml 所採用的 剖面織入方法是在程式編譯後,對程式的 語法樹(Abstract Syntax Tree)進行 追蹤,尋找適合的剖面織入對象,雖然也 會利用 advice 的型態資訊來避免造成型 態錯誤,但跟我們充分利用函數的型態資 訊來進行織入動作相比,他們的方法還是 只 能 算 是 語 法 結 構 導 向 。 因 為 像 higher-order 函數的呼叫往往是間接 的,並不一定能從語法樹上找出來,再加 上多型帶來的複雜因素,他們只能在非多 型 higher-order 函數的使用上正確織 入 advice。其次,先前提到的多型剖面 帶來的 coherence 織入的問題,也無法 由他們的語法結構導向的方法解決。 4

(3)

我們所採用的型態導向織入方法的核 心 a g 出來的型態 推演 們並 設 計 觀 念 ─ advised types 是 受 到 Haskell 語言[49]處理 par metric overloading(type classes) 的 predicated types[50]的啟發而設計 出來的。但在處理多型剖面的織入上,我 們的 type inference/translation 與 Haskell 的有者一些顯著的不同,所 要 解 決 的 問 題 也 不 一 樣 。 首 先 , higher-order函數對 type classes 不是一個問題,而在多型剖面的處理上, higher-order 函數卻是一個重要的挑 戰。其次,type classes 對於其 type instances 之 間 要 求 不 得 有 overlappin 的情形發生,以避免造成 歧義;但是在我們多型剖面的處理上, overlapped advices是常見也自然的 一種需求,所以我們必須小心處理它們所 可能造成的 incoherence 問題。由於有 這些本質上的差異,我們在設計型態系統 時必須採用相當不同的技巧來達到支援 多型剖面的型態導向織入。 附錄一中列出我們所發展 與轉譯的主要規則。這套推演規則雖 然是 non-syntax-directed,但是它 基本上還是依循 Hindley-Milner 型態 系統[]的架構,再加上 advised types 的處理。基於我過去在 Haskell type class 方面的經驗,我很順利將著名的 W-Algorithm 的 方 式 , 以 context based unification方式加以擴充,設 計了型態推演與轉譯的演算法,。 為了驗證我們的方法與技術,我 一 具 剖 面 機 制 的 函 數 式 程 式 語 言 , AspectFun,來實驗我們的方法。圖一展示 AspectFun 語言的語法與主要機制。 圖一:AspectFun 語言語法與機制 AspectFun 語言的編譯流程分為 9 步 驟,圖二勾勒出這些步驟的進行流程。其 中第四步進行 advised type inference 與靜 態織入,需要 advice 的地方會以 chain expressions 方 式 改 寫 , 這 些 chain expressions 在第八步會加以擴充成一般 Haskell.程式碼。Cflow 切點的處理比較特 殊,我們以 reader monad 來實現 state-based 的編譯方法,分成共三步驟(5,6,7)來完成。

(4)

AspectFun 的編譯器由 Haskell 語言開 發,將 AspectFun 程式轉譯成 Haskell 程 式。目前開發工作進行順利,除優化分析 部份將可按預定進度完成。 5. 結果與討論 本計畫第一年的結果符合預定進度, 我們除了發展了具有 second-order advice 與控制流程(cflow)切點的靜態織入方法 外,也勾勒出如何在既有的架構下進行 cflow 優化的技術。此外,所有的成果將 透過 Aspect 語言的設計與實作來進行實 驗驗證。同時,我們並正在發展 AspectFun 語言的操作型語義,為第二年的計畫奠定 了良好的基礎。最後,我們已將這些成果 寫成一篇論文,要投稿到知名的國際學術 會議。 6. 參考文獻

[1] G. Kiczales, J. Lamping, A. Menhdhekar, C. Maeda, C. Lopes, J.-M. Loingtier, and J. Irwin, Aspect-oriented programming, in ECOOP '97 Object-Oriented Programming 11th European Conference," Finland (M. Aksit and S. Matsuoka, eds.), vol. 1241, pp. 220-242, New York, NY: Springer-Verlag, 1997.

[2] AspectJ website:

http://www.eclipse.org/aspectj/

[]

[] D. Walker, S. Zdancewic, and J. Ligatti. A theory of aspects. In ICFP ’03: Proceedings of the eighth ACM SIGPLAN international conference on Functional programming, pages 127–139, New York, NY, USA, 2003. ACM Press.

[26] D. Tucker and S. Krishnamurthi. Pointcuts and advice in higher-order languages. In Proceedings of the 2nd International Conference on

Aspect-Oriented Software Development, 2003.

[27] R. Jagadeesan, A. Jeffrey, and J. Riely. A calculus of untyped aspect-oriented programs. In Proceedings of the 2003 European Conference on Object Oriented Programming, pages 54–73. Springer, 2003.

[28] M. Wand, G. Kiczales, and C. Dutchyn. A semantics for advice and dynamic join points in aspect-oriented programming. Foundations of Aspect-Oriented Languages (FOAL), 2002, Extended version: ACM TOPLAS, 26(5):890–910, September 2004.

[29] C. Clifton and G. Leavens. Minimao: Investigating the semantics of proceed. In Proceedings of the Foundations of Aspect-Oriented Languages, 2005. [15] The Haskell Language Report,

http://www.haskell.org/onlinereport/

[50] P. Wadler and S. Blott. How to make ad-hoc polymorphism less adhoc. In Conference Record of the 16th Annual ACM Symposium on Principles of Programming Languages, pages 60–76. ACM, January 1989. 7. 計畫自評 本計劃進行至目前為止,大致都按照原 定的計畫進度執行中,在雛型系統部份則 有所超前,已經完成 AspectFun 語言的設 計,編譯環境的開發也接近尾聲。目前要 加強的是關於動態切點優化的部份,相關 的演算法正在修正中,預計年底前可以完 成並實作在 AspectFun 語言的編譯器內。 至於 AspectFun 語言的操作型語義也 已經開始定義中,不過遭遇了一些小困 難,但已經陸續克服中,所以這部份的進 度應該也會超前達成。

(5)

參考文獻

相關文件

Regression model between numbers of hotel to sustainable tourism factors 1. Its regression equation is upward sloping since 68.99 is positive number, each unit increase in number

temperature T (in C) as a function of the height h (in kilometers), assuming that a linear model is appropriate.. (b) Draw the graph of the function in

In this talk, we present a novel algorithm by using adaptive grids as well as well as a tailored finite point method for a time dependent convection diffusion model to implement

Recall that we defined the moment of a particle about an axis as the product of its mass and its directed distance from the axis.. We divide D into

In JSDZ, a model process in the modeling phase is treated as an active entity that requires an operation on its data store to add a new instance to the collection of

Isakov [Isa15] showed that the stability of this inverse problem increases as the frequency increases in the sense that the stability estimate changes from a logarithmic type to

In part II (“Invariance of quan- tum rings under ordinary flops II”, Algebraic Geometry, 2016), we develop a quantum Leray–Hirsch theorem and use it to show that the big

To this end, we introduce a new discrepancy measure for assessing the dimensionality assumptions applicable to multidimensional (as well as unidimensional) models in the context of