Download presentation
Presentation is loading. Please wait.
Published byJoseph Maximillian Ford Modified over 9 years ago
1
Edwin Westbrook, Mathias Ricken, Jun Inoue, Yilong Yao, Tamer Abdlatif, Walid Taha
2
Abstractions are Expensive 2 power(2,17) : 41 nspower17(2) : 9 ns public static int power (int x, int n){ if (n == 1) return x; else return x * power(x, n-1); } public static int power17(int x){ return x * … * x; }
3
Bringing MSP to the Mainstream Want Multi-stage programming in Java Difficulty: MSP + side effects [Kameyama et al., 09],[Aktemur 09], [Kameyama et al., 08],[Kim et al. 06],[Calcagno et al., 00] Existing solutions are complex type systems Hard for mainstream programmers to use Do not allow “the Java way” of programming Our solution: weak separability MSP that fits with Java programming 3
4
Outline Multi-stage programming in Java Mint Multi-stage reflection library MSP + effects problem: scope extrusion Our solution: weak separability A type system for weak separability Performance 4
5
MSP in Mint Code has type Code Code built with brackets Code spliced with escapes `e Code compiled and run with run() method Code x = ; Code y = ; Integer z = y.run(); // returns 6 5
6
Staged power in Mint 6 public static Code spower (Code x, Integer n) { if (n == 1) { return x; } else { return ; } spower (, 17) Example:
7
Staged Reflection in Mint 7 Class ClassCode Field FieldCode Standard primitivesStaged primitives Field[] Class.getFields() FieldCode [] Class.getFields() Object Field.get(Object) B FieldCode.get(A)
8
Simple Staged Serializer 8 public static Code sserialize (ClassCode t, Code o) { // handle base types if (t.getCodeClass () == Integer.class) return )o)); } |>; // handle defined classes Code result = ; for (FieldCode fc: t.getFields ()) { result = <| { `result; `(serializeField (fc, o)); } |>; } return result; }
9
The Scope Extrusion Problem 9 Code x = ; Code c = <| { Integer y = foo(); Integer z = `(x = ); } |>
10
Scope Extrusion via Exceptions 10 Code meth (Code c) { throw new CodeContainerException (c); } Code c = )); } |>; CodeContainerException ( )
11
Scope Extrusion via Cross-Stage Persistence See paper! 11
12
Solution: Weak Separability No effects containing code be seen outside of escapes 12 ` e e2e2 effect (1, 3) effect (, 7)
13
Weak Separability part 1: Type T is Code-Free iff: T not a subtype of Code for some T’ All field types of T are code-free All method return types of T are code-free T is final 13 Not code-free: class C { Code c; Object x; Code foo () { … } } Not final Field not code-free Return not code-free
14
Weak Separability part 2: Term t is Weakly Separable iff: All assignments are either: At code-free types, OR To variables bound in term t Each throw new C(e 1, …, e n ); has code-free e i Cross-stage persistence for final code-free variables 14
15
Expressivity of Weak Separability Build code with accumulators: <| `(for (i = 0; i < n; ++i) accum = ;) |> Throw exceptions out of code generators: <| `(if (malformed (data)) throw new BadData (data); …) |> Update global counters Generated code can contain effects 15
16
Typing for Weak Separability 16 <| { Integer y = foo(); return `(e); } |> e can see heap values with Should not see outside
17
Consider a Small-Step Trace 17 <| { Integer y = foo(); return `(e 1 ); } |> <| { Integer y = foo(); return e 2 ; } |> e1e1 * heap: l 1 = 0 l 2 = l 3 = B(f=l 1 ) heap: l 1 = 0 l 2 = l 3 = B(f=l 1 ) heap: l 1 = 0 l 2 = l 3 = B(f=l 1 ) heap: l 1 = 0 l 2 = l 3 = B(f=l 1 ) heap: l 1 = 2 l 2 = l 3 = B(f=l 4 ) l 4 = 7 l 5 = heap: l 1 = 2 l 2 = l 3 = B(f=l 4 ) l 4 = 7 l 5 = heap: l 1 = 2 l 2 = l 3 = B(f=l 4 ) l 4 = 7 l 5 = heap: l 1 = 2 l 2 = l 3 = B(f=l 4 ) l 4 = 7 l 5 = Bad!
18
Solution: Stack of Heap Typings 18 <| { Integer y = foo(); return `(e 1 ); } |> <| { Integer y = foo(); return e 2 ; } |> e1e1 * typing: l 1 : Integer l 2 : Code l 3 : B typing: l 1 : Integer l 2 : Code l 3 : B typing: l 1 : Integer l 2 : Code l 3 : B typing: l 1 : Integer l 2 : Code l 3 : B typing: l 1 : Integer l 2 = Code l 3 : B typing: l 1 : Integer l 2 = Code l 3 : B typing: l 1 : Integer l 2 : Code l 3 : B l 4 : Integer typing: l 1 : Integer l 2 : Code l 3 : B l 4 : Integer typing: l 4 : Integer l 5 : Code l 4 : Integer l 5 : Code Smashing Lemma
19
Typing in Symbols 19 1 ; …; n ; |- (H, e) : T Type heaps and expressions together One heap typing for each dynamic binding
20
Typing in Symbols 20 <| { Integer y = foo(); return `(e); } |> 1 ; …; n ; |- (H, 1 ; …; n ; ; , y:Integer |- e : Integer ) 1 ; …; n ; ; , |- H H typed with Γ H constrains Σ
21
Smashing Lemma (approx) If: 1 ; …; n ; |- H 1 1 ; …; n ; ; |- H 2 H 1 | L = H 2 | L for L = dom( i i ) - dom(cf( i i )) Then: 1 ; …; n-1 ; n cf( ); |- H 2 21
22
Performance Results 22
23
Conclusion: Mint = Java + MSP MSP reduces the cost of abstractions Mint brings MSP to the mainstream Key insight: weak separability Only code-free effects can be seen outside of escapes Can do MSP with common Java idioms: Build code with an accumulator Throw exceptions out of generators 23
24
Future Work: Faster Compilation Currently we call full compiler at runtime: SLOW Maybe we could generate code snippets? 24 public static Code spower (Code x, Integer n) { if (n == 1) { return x; } else { return <| `x * `(spower (x, n-1)) |>; } interface I$$1 { Integer getValue (); } class C$$1 implements I$$1 { Helper$$1 x, rec_call; Integer getValue () { return x.getValue() * rec_call.getValue(); }
25
25 QUESTIONS?
26
Running Generators 26 interface class PowerFun{ public Integer call (Integer x); } PowerFun f = <| new PowerFun () { public Integer run (Integer x) { `(spower(, 17)); } } |>.run ();
27
Scope Extrusion via Cross-Stage Persistence 27 Code > doCSP (Thunk t) { return ; } Code c = <| { Integer y = foo(); Code d = `(doCSP(new Thunk() { Code call () { return ; })) } |>;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.