-
Notifications
You must be signed in to change notification settings - Fork 0
/
non-cflow-advice-with-closures.ml
58 lines (45 loc) · 1.49 KB
/
non-cflow-advice-with-closures.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(*
Neel Krishnaswami's "For non-cflow advice..." comment from "Lambda The Ultimate".
See http://lambda-the-ultimate.org/node/3465#comment-49813
*)
(* An "AOP-style fixed point" -- use backpatching to build a recursive function,
and then return the ref cell (aka join point) so people can mutate it.
Peter Landin may have invented this method, btw -- though he was too
disciplined to let the ref cell escape!
*)
let aop_fix f =
let joinpoint = ref (fun _ -> assert false) in
let () = joinpoint := f (fun z -> !joinpoint z) in
((fun z -> !joinpoint z), joinpoint)
(* Here's how you can add before and after advice to a function. By
updating the joinpoint/recursive-ref, we can add code to be run on
recursive calls to a function.
*)
let before_advice advice joinpoint =
let f = !joinpoint in
joinpoint := (fun x -> let () = advice x in f x)
let after_advice advice joinpoint =
let f = !joinpoint in
joinpoint := (fun x ->
let v = f x in
let () = advice x v in
v)
(* Of course, every example must be factorial. Notice that we get a
function we can call, as well as a join point fjoin.
*)
let (fact, fjoin) =
aop_fix (fun fact n -> if n = 0 then 1 else n * fact (n - 1))
(* Add some advice to log what the argument is. *)
let () =
before_advice (fun n -> Format.printf "Called with: %d\n" n) fjoin
(* We get the following results *)
(*
# fact 5;;
Called with: 5
Called with: 4
Called with: 3
Called with: 2
Called with: 1
Called with: 0
- : int = 120
*)