摘要: | 剖面導向程式設計(Aspect-Oriented Programming, AOP)是近年來繼物件導向程式設計(OOP)後,所興起的一種新的模組化程式設計方法。從AOP的觀點來看,應用程式除了功能邏輯以外,還有許多像安全需求等的橫跨性關注(crosscutting concerns);實現這些橫跨性關注的程式碼應該要從功能模組中分離出來,自成一模組並稱之為剖面。剖面與功能模組之間的界接點由所謂橫切點(pointcut)來定義,並透過稱之為織入(weaving)的機制將剖面程式碼整合入功能模組中,從而合成完整程式,滿足系統整體需求。這樣實現橫跨性關注的程式碼就可以集中封裝於適當的模組中,避免掉程式碼糾結與重複的問題。過去兩年間,我們的國科會研究計畫以型態導向的方法,發展了一套多型剖面的織入技術,並據以設計實作了一個實驗性的剖面導向函數語言AspectFun。我們提出所謂的諮詢型態(advised types)的概念,將剖面織入的需求嵌入型態之中,透過型態推理(type inference)的過程,選擇出型態相容的剖面在適當的環境下織入功能程式模組。這樣以型態推演程序完成織入的作法,不僅可以避免單就語法結構來決定織入剖面的缺點,並有機會可以替多型剖面奠定一個良好的理論基礎,進而在泛型程式的設計上,導入剖面機制,改善泛型程式的模組性。此外,我們也針對依賴流程決定的剖面,以個體(monad)轉換方式發展了有效的狀態判斷織入技術,並納入AspectFun語言的實作中。本計劃將在AspectFun語言既有的基礎上,進一步來探討剖面機制在函數式語言的程式模組化與擴充性方面的一些課題,所要進行的探討涵蓋實作、語言設計與理論模型與驗證各個面向,主題則大致上可分為三部份: (1) AspectFun語言核心機制的擴充與實作,以奠定後續研究的基礎。(2)探討如何以剖面與編譯技術來模組化純粹函數語言內最基本的橫跨性關注:狀態處理 (state manipulation via assignment) ,將設計與實作具備狀態處理功能的剖面 (side-effecting aspects)來處理的狀態改變,以提高純粹函數程式的模組化。(3)探討如何以剖面處理函數語言型態擴充(type extension)的需求,嘗試為在程式擴充性方面著名的Expression Problem 尋找一個剖面導向的解決方案。 Aspect-oriented programming (AOP) aims at modularizing concerns such as profiling and security that crosscut the components of a software system. In AOP, a program consists of many functional modules and some aspects that encapsulate the crosscutting concerns. An aspect provides two specifications: A pointcut, comprising a set of functions, designate when and where to crosscut other modules; and an advice, which is a piece of code, that will be executed when a pointcut is reached. The complete program behavior is derived by some novel ways of composing functional modules and aspects according to the specifications given within the aspects. This is called weaving in AOP. Weaving results in the behavior of those functional modules impacted by aspects being modified accordingly. Recently we have developed an aspect-oriented functional language, AspectFun, for investigating various issues in introducing aspect orientation to a polymorphically typed functional language. Specifically, we have developed some novel techniques for static weaving of polymorphic aspects based on the notion advised types. Along with the type inference process, we employ a type-directed translation scheme which resolves all advice applications at static time and transforms AspectFun source programs into executable code in Haskell. We have also devised a monadic rewriting scheme to handle control-flow based pointcuts that requires knowledge of a program’s execution state. In this project, we shall carry on our work of AspectFun. Besides extending AspectFun’s core mechanism for future research, we shall investigate two issues of using aspects for enhancing program modularity and extensibility. First, in a purely functional language, a basic crosscutting concern is “side effect”, i.e., state manipulation via assignments. Indeed, even we can hide the hairy details of manipulating state by using the state-of-the-art technology of monadic assignments; we have to do a comprehensive rewriting of our functional programs. Unfortunately, typical aspects such as profiling and tracing are most naturally implemented using side effects. If we cannot handle side effects directly in an aspect, the modularity acquired through aspects will be greatly compromised. Therefore, we plan to extend AspectFun with side-effecting aspects which support state manipulation mechanisms such as user-defined variables and assignments directly. On the implementation side, we shall develop some compilation techniques which transform the pure-functional part into a monadic style, and weave in the aspects. The ultimate goal is to preserve the lazy semantics of AspectFun while being able to execute side-effecting aspects in a non-interfering way. Second, we shall investigate the feasibility of using aspects to address the type extension requirements as stated in the famous expression problem (which calls for language mechanisms that can support type-safe program extension in both the dimensions of data types and associated operations, yet neither code duplication nor code rewriting is required.) In general, it is easier to extend the set of operations than to extend the elements of a data type in a functional program. (By contrast, it is the opposite in an object-oriented program.) Here we plan to extend AspectFun with an inter-type declaration mechanism, following the spirit of that of AspectJ, to support type extension in a modular manner. Specifically, when a data type is extended with some new variants, we will employ aspects to define the required extension for the operations associated with the data type. Consequently, the entailed extensions of those operations can be modularly encapsulated in aspects. |