Title: Aspectual%20Caml%20an%20Aspect-Oriented%20Functional%20Language
1Aspectual Camlan Aspect-Oriented Functional
Language
- Hideaki TatsuzawaHidehiko MasuharaAkinori
Yonezawa - University of Tokyo
2Background Studies on AOPLs
- Practical languages are mostly based on OOPLs
- Java Kiczales et al. 2001
- C Spinczyk et al. 2002
- etc.
- AOPLs based on functional languages are designed
for theoretical purposes - MiniAML Walker et al. 2003
- TinyAspect Aldrich2004
- etc.
3Motivation
- Design and implement a functional AOPL Aspectual
Caml by adopting advanced AOP features (e.g.
inter-type declarations) for - modularizing large functional programs
- compilers, theorem provers, etc.
- providing a foundation of further theoretical
studies - under clear semantics of functional languages
4Contributions of Aspectual Caml
- Designed AspectJ-like AOP features in a
functional language - pointcut-advice mechanism
- curried pointcuts
- type inference of aspects
- polymorphic and monomorphic pointcuts
- type extension mechanism (cf. inter-type
declarations in AspectJ) - Showed an implementation framework
5Motivating Example
Simple Interpreter
Aspects
type extension
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env
t) match t with Sub(t1, t2) -gt
let e eval env in e t1 - e t2
_ -gt proceed tend
type t Add of t t Num of int
Let of string t t Var of
string let rec eval env t match t with
Add(t1, t2) -gt (eval env t1) (eval env
t2) Num(i) -gt i Let(s, t1, t2) -gt eval
((s, eval env t1)env) t2 Var(s) -gt List.assoc
s env
type declaration of terms
extension of function behavior
function definition for evaluation of terms
logging evaluation of terms
aspect LogEval advice log_eval before
(call eval _ _) print_string called
eval\nend
6Motivating Example
Simple Interpreter
Aspects
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env
t) match t with Sub(t1, t2) -gt
let e eval env in e t1 - e t2
_ -gt proceed tend
type t Add of t t Num of int
Let of string t t Var of
string let rec eval env t match t with
Add(t1, t2) -gt (eval env t1) (eval env
t2) Num(i) -gt i Let(s, t1, t2) -gt eval
((s, eval env t1)env) t2 Var(s) -gt List.assoc
s env
extension ofthe type declaration
specifies 2nd applicationsto function eval
aspect LogEval advice log_eval before
(call eval _ _) print_string called
eval\nend
7Required Features to Aspectual Caml
- 2 kinds of AOP features
- Pointcut-advice mechanism
- Type extension mechanism
- Type inference of aspects without base code
- writing aspects without type annotations
- ensuring type safety of woven code
- enabling separate compilation
8Key Designs of Aspectual Caml
- Curried pointcuts
- Type extension mechanism
- Weaving with type inference
- 2 kinds of pointcuts
- polymorphic and monomorphic
9Key Designs of Aspectual Caml
- Curried pointcuts
- Type extension mechanism
- Weaving with type inference
- 2 kinds of pointcuts
- polymorphic and monomorphic
Todaysmaintopic
10Curried Pointcuts
- Specify applications to curried functions
easily - cover application to variables from the result of
partial applications
call eval env t specifies 2nd applications to
eval
call eval env t covers the application e tin
the context of let e eval env in e t
11Type Extensioncf. inter-type declarations in
AspectJ
- Constructor addition
- Field addition
type t Sub t t adds the new
constructor Subthat takes 2 arguments of the
type t
type t Var of int0 adds the new
integer field to the constructor Var and 0 is
the default value for the extra field
12Key Designs of Aspectual Caml
- Curried pointcuts
- Type extension mechanism
- Weaving with type inference
- ensures type safety of woven code
- allows to define pointcuts and advices without
type annotations - checks type of aspects without base code
- cf. C templates
- 2 kinds of pointcuts
- polymorphic and monomorphic
13Possible 2 Approaches for Type Safety
Weaving of Type Inference Background
- Type checking woven code after weaving
- no need of aspect typing
- impossible separate compilations
- Type checking aspect code and base code before
weaving - need of aspect typing
- needed for separate compilations
14Possible 2 Approaches for Type Safety
Weaving of Type Inference Background
- Type checking woven code after weaving
- no need of aspect typing
- impossible separate compilations
- Type checking aspect code and base code before
weaving - need of aspect typing
- needed for separate compilations
Our approach
15Type System for Aspects
- Should deal with all kinds of declarations in
aspects - pointcuts
- advices
- type extensions
- local variables
16Example of Aspect Type Inference
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e t2
_ -gt proceed tend
17Type System for Aspects
- Should deal with all kinds of declarations in
aspects - pointcuts
- infers types from explicitly specified types and
kinds of pointcuts - advices
- type extensions
- local variables
18Type Inference of Pointcuts
eval a -gt ß -gt ?env a t ß
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e t2
_ -gt proceed tend
does not use type information in base
code (eval (string int) list -gt t -gt int)
19Type System for Aspects
- Should deal with all kinds of declarations in
aspects - pointcuts
- advices
- infers types of an advice body using extended
environment with top-level variables of base
code, variables bound by pointcuts, and proceed - checks whether a type of an advice body match
with one expected by contexts - type extensions
- local variables
20Type Inference of proceed
eval a -gt ß -gt ?env a t ßproceed ß -gt ?
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e t2
_ -gt proceed tend
proceed means the continuation that takes the
2nd argument of evaland returns the result
21Type Inference of Advice Body
eval a -gt ß -gt ?env a t ßproceed ß -gt ?
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e
t2 _ -gt proceed tend
infer the type of the advice bodywith the type
environment extended withbound variables and
proceed
22Type Inference of Advice Body
eval a -gt t -gt ?env a t tproceed t -gt ?
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e
t2 _ -gt proceed tend
23Type Inference of Advice Body
eval a -gt t -gt intenv a t tproceed t -gt
int
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e t2
_ -gt proceed tend
24Checks Whether Type of Advice Body Matches
Expected Type
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e t2
_ -gt proceed tend
the expected type is the return type of
proceed
25Checks Whether Type of Advice Body Matches
Expected Type
eval a -gt t -gt intenv a t tproceed t -gt
int
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e
t2 _ -gt proceed tend
the type of advice body matches the expected type
the expected type is the return type of
proceed
26Type System for Aspects
- Should deal with all kinds of declarations in
aspects - pointcuts
- advices
- type extensions
- replaces corresponding information of type
environment with the extended information - local variables
27Type Extension
type t Num of int Add of t t Let of
string t t Var of string Sub of t t
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env t)
match t with Sub(t1, t2) -gt let e eval
env in e t1 - e t2
_ -gt proceed tend
extends environment that is used in aspects
28Type System for Aspects
- Should deal with all kinds of declarations in
aspects - pointcuts
- advices
- type extensions
- local variables
- infers types using extended environment with
top-level variables of base code
29Weaving with Type Information
- Generates type safe woven code from typed base
code and typed aspect code - judges join points that advices are woven into
- kinds of pointcut
- specified names
- type information of each code
- reflects type extensions as changing
corresponding type declarations
30Example of Weaving
type t Add of t t Num of int
Let of string t t Var of
string let rec eval env t match t with
Add(t1, t2) -gt (eval env t1) (eval env
t2) Num(i) -gt i Let(s, t1, t2) -gt eval
((s, eval env t1)env) t2 Var(s) -gt List.assoc
s env
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env
t) match t with Sub(t1, t2) -gt
let e eval env in e t1 - e t2
_ -gt proceed tend
31Type Extension
type t Add of t t Num of int
Let of string t t Var of
string let rec eval env t match t with
Add(t1, t2) -gt (eval env t1) (eval env
t2) Num(i) -gt i Let(s, t1, t2) -gt eval
((s, eval env t1)env) t2 Var(s) -gt List.assoc
s env
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env
t) match t with Sub(t1, t2) -gt
let e eval env in e t1 - e t2
_ -gt proceed tend
type declaraion in woven code includesthe
constructor Sub
32Woven Code
type t Add of t t Num of int
Let of string t t Var of string
Sub of t t
33Weaving Judgment
type t Add of t t Num of int
Let of string t t Var of
string let rec eval env t match t with
Add(t1, t2) -gt (eval env t1) (eval env
t2) Num(i) -gt i Let(s, t1, t2) -gt eval
((s, eval env t1)env) t2 Var(s) -gt List.assoc
s env
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env
t) match t with Sub(t1, t2) -gt
let e eval env in e t1 - e t2
_ -gt proceed tend
specifies function calls to eval
34Weaving Judgment
eval ß-gt t -gtint env ß t t
eval(stringint)list-gtt-gt int env(stringint)
list t1 t
type t Add of t t Num of int
Let of string t t Var of
string let rec eval env t match t with
Add(t1, t2) -gt (eval env t1) (eval env
t2) Num(i) -gt i Let(s, t1, t2) -gt eval
((s, eval env t1)env) t2 Var(s) -gt List.assoc
s env
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env
t) match t with Sub(t1, t2) -gt
let e eval env in e t1 - e t2
_ -gt proceed tend
type safe substitution ß (stringint) list
types of the pointcut matchestypes of the
applications
weaves the advice into the join point
35Woven Code
type t Add of t t Num of int
Let of string t t Var of string
Sub of t t let rec eval env t match t
with Add(t1, t2) -gt (eval_sub eval env
t1) (eval_sub eval env t2) Num(i) -gt i
Let(s, t1, t2) -gt eval_sub eval ((s,
eval_sub eval env t1)env) t2 Var(s) -gt
List.assoc s env
let rec eval_sub proceed call eval env t
match t with Sub(t1, t2) -gt let
e eval env in e t1 - e t2 _ -gt
proceed tend
function definition for advice
36Example of Weaving
eval ß-gt t -gtint env ß t t
let eval t u t u in eval 2 3
aspect SubExtension type t ... Sub of t
t advice eval_sub around (call eval env
t) match t with Sub(t1, t2) -gt
let e eval env in e t1 - e t2
_ -gt proceed tend
eval int -gt int -gt int
types of the pointcut does not matchtypes of the
application
do not weave the advice into the join point
37Key Designs of Aspectual Caml
- Curried pointcuts
- Type extension mechanism
- Weaving with type inference for aspects
- 2 kinds of pointcuts
- polymorphic pointcuts
- writing pointcuts without explicit type
annotations - monomorphic pointcuts
- identifying join points specified by pointcuts
only from their definitions
38Why 2 Kinds of Pointcuts?
- Writing pointcuts without type annotations
- explicit type annotations are tedious
- pointcut call_eval t call eval t is better
rather than pointcut call_eval t call
(eval (string int) list -gt t -gt int) (t t) - automatic type inference is needed
- familiar requirement for ML programmers
39Why 2 Kinds of Pointcuts?
- Identifying join points specified by pointcuts
only from their definitions - different advices using the same pointcuts should
affect the same join points
.. . . ..
pointcut logpoint advice a after logpoint
advice b before logpoint ...
402 Conflicting Requirements
- Automatic type inference can instantiate types of
pointcut variables differently - cf. types of polymorphic functions arguments
41Advices with the Same PointcutsMay Affect
Different Join Points
pointcut logpoint x call List.assoc x advice
before_trace before logpoint x
print_string (enter"
(string_of_int x)) advice after_trace
after logpoint x print_string leave
42Advices with the Same PointcutsMay Affect
Different Join Points
pointcut logpoint x call List.assoc x advice
before_trace before logpoint x
print_string (enter "
(string_of_int x)) advice after_trace
after logpoint x print_string leave
Specifies applications to assoc
43Advices with the Same PointcutsMay Affect
Different Join Points
2 advice decls. using the same pointcut logpoint
pointcut logpoint x call List.assoc x advice
before_trace before logpoint x
print_string (enter"
(string_of_int x)) advice after_trace
after logpoint x print_string leave
traces before calls to assoc
traces after calls to assoc
44Advices with the Same PointcutsMay Affect
Different Join Points
pointcut logpoint x call List.assoc x advice
before_trace before logpoint x
print_string (enter"
(string_of_int x)) advice after_trace
after logpoint x print_string leave
used as int
any type
45Advices with the Same PointcutsMay Affect
Different Join Points
match differentjoin points
pointcut logpoint x call List.assoc x advice
before_trace before logpoint x
print_string (enter"
(string_of_int x)) advice after_trace
after logpoint x print_string leave
used as int
any type
46This output is not intended result
base codeassoc 2 int_envassoc 3 int_envassoc
a string_env
pointcut logpoint x call List.assoc x advice
before_trace before logpoint x
print_string (enter"
(string_of_int x)) advice after_trace
after logpoint x print_string leave
outputenter2 leave enter3 leave leave
leave without enter!
47Proposal 2 Kinds of Pointcuts
- Polymorphic pointcuts
- variable types in pointcuts are inferred from
advice bodies - one pointcut used in different advice decls may
match different join points - Monomorphic pointcuts
- variable types must not be instantiatedin advice
bodies - one pointcut used in any advice decl matches the
same join points - examplecall (List.assocint -gt (int string)
list -gt string) x y
48Current Implementation
- Source to source translator
- Extension of OCaml compiler
- Essential AOP features are implemented
- type extension
- around advice
- 2 kinds of pointcuts
- type inference of aspects
- weaving
- most primitive pointcuts except for wild card
- call, exec, match, and, within
49Related Work
- AspectJ Kiczales et al. 2001
- a mature AOP language
- practical with various AOP features
- AOP features of Aspectual Caml import
fromAspectJ - too complicated for theoretical analysis
50Related Work
- MiniAML Walker et al. 2003
- proposes minimal typed functional AOP language
core calculus - defines semantics of the calculus and proves its
soundness - the semantics of MiniAML are defined as a
translation into the calculus - TinyAspect Aldrich 2004
- for studying interference between aspects and
modules - proposes small typed functional AOP language
including module system - defines the semantics and proves its soundness
51Related Work
polymor-phism pointcut typeextension semanticsdefinition modulesystem soundnessproof
MiniAML only callwith 1 arg ? ?
TinyAspect only callwith 1 arg ? ? ?
AspectualCaml ? many ?
52Conclusion
- Designed and implemented a practical AOP language
based on a typed functional language - Proposed solutions to problems in adaptations AOP
to functional languages - curried pointcuts
- weaving and type inference for aspects
- 2 kinds of pointcuts
- type extension mechanism
53Future Work
- Define formal semantics
- Study well-typedness properties at aspects
- Implement ßversion
- Introduce more expressive AOP features
54Fin
55Problems of Constructor Addition
- New constructor makes pattern matches in base
code non-exhaustive - aspect programmers should supplement the lack
case of pattern matches declaring proper advices - this non-exhaustiveness can be found using
information in type check of woven code
56Default Value of Field Addition
- Aspect programmers should set proper initial
values for new fields using advices - Without proper advices default values preserve
type safety of woven code
57Judgment of Weaving or Not
- Types and names are used to judge whether weave
or not
aspect LogAssoc advice log_assoc around
call assoc elem env print_string
(assoc withelem\n) proceed
env end
let assoc1 assoc str assoc1 env assoc1
env2 assoc 20 env3
58Judgment of Weaving or Not
- Types and names are used to judge whether weave
or not
assocstring -gt ß -gt ?
aspect LogAssoc advice log_assoc around
call assoc elem env print_string
(assoc withelem\n) proceed
env end
let assoc1 assoc str assoc1 env assoc1
env2 assoc 20 env3
type safe substitution ß (string a) list, ?
a
the advice body can be evaluatedpreserving type
safety of target code
assocstring -gt (string a) list -gt a
59Judgment of Weaving or Not
- Types and names are used to judge whether weave
or not
assocstring -gt ß -gt ?
aspect LogAssoc advice log_assoc around
call assoc elem env print_string
(assoc withelem\n) proceed
env end
let assoc1 assoc str assoc1 env assoc1
env2 assoc 20 env3
no substitutions preserve type safety
evaluation of the advice body destroystype
safety of target code at the join point
assocint -gt (int a) list -gt a
60Flow of Compile
Wovenprogram
Ocaml Source
typed Ocaml AST
typeinference
weavewith types
typeextension
type inferencefor aspects
Aspect Source
typed Aspect
61Example of Aspect Type Inference
assoca -gt ß -gt ? elema envß
aspect LogAssoc advice log_assoc around
call assoc elem env print_string (assoc
withelem\n) proceed env end
type inference of pointcuts does not use type
information of target code (assoca -gt (aß) list
-gt ß)
62Example of Aspect Type Inference
assoca -gt ß -gt ? elema envß proceedß -gt ?
aspect LogAssoc advice log_assoc around
call assoc elem env print_string (assoc
withelem\n) proceed env end
proceed means the continuation that takes the
2nd argument of assocand returns the result
63Example of Aspect Type Inference
assoca -gt ß -gt ? elema envß proceedß -gt ?
aspect LogAssoc advice log_assoc around
call assoc elem env print_string (assoc
withelem\n) proceed env end
infer the type of the advice bodywith the type
environment extended withbound variables and
proceed
64Example of Aspect Type Inference
assocstring -gt ß -gt ? elemstring envß proceedß
-gt ?
aspect LogAssoc advice log_assoc around
call assoc elem env print_string (assoc
withelem\n) proceed env end
65Example of Aspect Type Inference
the expected type is the return type of proceed
aspect LogAssoc advice log_assoc around
call assoc elem env print_string (assoc
withelem\n) proceed env end
the type of this expression matches the expected
type
66Limitation of Simple Call Pointcuts
Impossible to specify applications to partially
applied functions
aspect LogAssoc advice log_assoc around
call assoc elem print_string
(assoc withelem\n) proceed elem end
let assoc1 assoc str assoc1 env assoc1
env2 assoc 20 env3
specifies applications to assoc
67Limitation of Simple Call Pointcuts
Impossible to specify applications to partially
applied functions
aspect LogAssoc advice log_assoc around
call assoc elem print_string
(assoc withelem\n) proceed elem end
let assoc1 assoc str assoc1 env assoc1
env2 assoc 20 env3
defines advice code
68Limitation of Simple Call Pointcuts
Impossible to specify applications to partially
applied functions
aspect LogAssoc advice log_assoc around
call assoc elem print_string
(assoc withelem\n) proceed elem end
let assoc1 assoc str assoc1 env assoc1
env2 assoc 20 env3
impossible to specify 2nd applicationsto
functions assocsince it takes 2 arguments
69Proposal Curried Pointcuts
- Curried pointcuts allow to easily
specifyapplications to curried functions - call/exec pointcuts in AspectJ can specifyonly
1st applications
call assoc elem env specifies 2nd applications
to functions assoc
70Design for Curried Pointcuts
- Specify applications to variables bound to
partially applied closures
aspect LogAssoc advice log_assoc around
call assoc elem env print_string
(assoc withelem\n) proceed
env end
let assoc1 assoc str assoc1 env assoc1
env2 assoc 20 env3
71Aspect-Oriented Programming (AOP)
- modularizing program units over multiple modules
- Better readability and flexibility of programs
- industrial benefits
- refactoring a huge product with AspectJ Adrian
et.al 2004
crosscutting concerns
72An Example of an Aspect Definition
Logging function calls to assoc
aspect LogAssoc advice log_assocaround call
assoc elem print_string (assoc
withelem\n) proceed elem end
73An Example of an Aspect Definition
Logging function calls to assoc
specifies function calls to assoc
aspect LogAssoc advice log_assoc around call
assoc elem print_string (assoc
withelem\n) proceed elem end
74An Example of an Aspect Definition
Logging function calls to assoc
execution points of applications to a function
named assoc
aspect LogAssoc advice log_assoc around call
assoc elem print_string (assoc
withelem\n) proceed elem end
prints a message
75An Example of an Aspect Definition
pointcut
Logging function calls to assoc
execution points of applications to a function
named assoc
aspect LogAssoc advice log_assoc around call
assoc elem print_string (assoc
withelem\n) proceed elem end
advice body
print the message