linear regions are all you need
DESCRIPTION
Linear Regions Are All You Need. Matthew Fluet Cornell University Greg Morrisett & Amal Ahmed Harvard University. Memory Management. Dynamic allocation pervasive in computation. Memory Management. Dynamic allocation pervasive in computation Region-based Memory Management - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/1.jpg)
Linear Regions Are All You Need
Matthew Fluet
Cornell University
Greg Morrisett & Amal Ahmed
Harvard University
![Page 2: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/2.jpg)
2
Memory Management
Dynamic allocation pervasive in computation
![Page 3: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/3.jpg)
3
Memory Management
Dynamic allocation pervasive in computation
Region-based Memory Management– Memory is divided into regions– Objects are individually allocated in a region
constant-time operation
– All objects in a region are deallocated together constant-time operation
![Page 4: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/4.jpg)
4
Application: Cyclone
Cyclone Safe-C Project– type-safety– with the “virtues” of C
low-level interface with manifest cost model
– range of memory management options regions are an organizing principle
![Page 5: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/5.jpg)
5
Cyclone: Regions
Region varietyAllocation
(objects)
DeallocationAliasing
(objects)(what) (when)
Stack static
whole region
exit of lexical scope
unrestricted
Lexical
dynamic
Dynamicmanual
Dynamic seq.
Heap (`H)
single objects
automatic(BDW GC)
Unique (`U)
manual restrictedRef-counted (`RC)
![Page 6: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/6.jpg)
6
Application: Cyclone
MediaNET– TCP benchmark (packet forwarding)– Cyclone v.0.1 (lexical regions & garbage collector)
High water mark: 840 KB 130 collections Basic throughput: 50 MB/s
– Cyclone v.0.5 (unique pointers & dynamic regions) High water mark: 8 KB 0 collections Basic throughput: 74MB/s
![Page 7: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/7.jpg)
7
Application: Cyclone
MediaNET– TCP benchmark (packet forwarding)– Cyclone v.0.1 (lexical regions & garbage collector)
High water mark: 840 KB 130 collections Basic throughput: 50 MB/s
– Cyclone v.0.5 (unique pointers & dynamic regions) High water mark: 8 KB 0 collections Basic throughput: 74MB/s
![Page 8: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/8.jpg)
8
Cyclone: Regions
Region varietyAllocation
(objects)
DeallocationAliasing
(objects)(what) (when)
Stack static
whole region
exit of lexical scope
unrestricted
Lexical
dynamic
Dynamicmanual
Dynamic seq.
Heap (`H)
single objects
automatic(BDW GC)
Unique (`U)
manual restrictedRef-counted (`RC)
Proving type safety of Cyclone is a nightmare!!
![Page 9: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/9.jpg)
9
Cyclone: Regions
Region varietyAllocation
(objects)
DeallocationAliasing
(objects)(what) (when)
Stack static
whole region
exit of lexical scope
unrestricted
Lexical
dynamic
Dynamicmanual
Dynamic seq.
Heap (`H)
single objects
automatic(BDW GC)
Unique (`U)
manual restrictedRef-counted (`RC)
Goal: simple model where we can easily encode the key features of Cyclone in a target language with
a simpler type system.
![Page 10: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/10.jpg)
10
Cyclone: Regions
Region varietyAllocation
(objects)
DeallocationAliasing
(objects)(what) (when)
Stack static
whole region
exit of lexical scope
unrestricted
Lexical
dynamic
Dynamicmanual
Dynamic seq.
Heap (`H)
single objects
automatic(BDW GC)
Unique (`U)
manual restrictedRef-counted (`RC)
Linear RegionsAre All You Need
![Page 11: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/11.jpg)
11
Outline
Introduction
Monadic Type System (FRGN) [ICFP’04]
Substructural Type System (rgnUL)– Translation Sketch
Conclusion
![Page 12: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/12.jpg)
12
Monadic Type System for Regions [ICFP’04]
Extend the runST “trick” to nested regions [L-PJ ’94]
– Polymorphic type system ensures safety
Key insights (FRGN):– Effects map to an indexed monadic type– Region subtyping witnessed by types– Sufficient for encoding Tofte-Talpin region calculus
and “core” Cyclone region features
![Page 13: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/13.jpg)
13
RGN monad: Types
Monadic type
RGN computations in stack of regions returning values of type ;a “stack” transformer
![Page 14: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/14.jpg)
14
RGN monad: Operations
Monadic unit and bind
returnRGN ::
8,. ! RGN
thenRGN ::
8,,. RGN ! ( ! RGN ) ! RGN
![Page 15: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/15.jpg)
15
RGN monad: Operations
Monadic unit and bind
returnRGN ::
8,. ! RGN
thenRGN ::
8,,. RGN ! ( ! RGN ) ! RGN
![Page 16: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/16.jpg)
16
RGN monad: Operations
Monadic unit and bind
returnRGN ::
8,. ! RGN
thenRGN ::
8,,. RGN ! ( ! RGN ) ! RGN
![Page 17: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/17.jpg)
17
RGN monad: Types
Reference type
Ref values of type allocated in region
at the top of the stack of regions
![Page 18: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/18.jpg)
18
RGN monad: Operations
Create and read region allocated values
new ::
8,. ! RGN (Ref )
read ::
8,. Ref ! RGN
![Page 19: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/19.jpg)
19
RGN monad: Operations
Create and read region allocated values
new ::
8,. ! RGN (Ref )
read ::
8,. Ref ! RGN
![Page 20: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/20.jpg)
20
RGN monad: Encapsulation
Encapsulate and run a monadic computation
runRGN ::
8. (8. RGN ) !
![Page 21: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/21.jpg)
21
RGN monad: Encapsulation
Encapsulate and run a monadic computation
runRGN ::
8. (8. RGN ) !
![Page 22: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/22.jpg)
22
RGN monad: Encapsulation
Encapsulate and run a monadic computation
runRGN ::
8. (8. RGN ) !
“for all stacks” ) no assumptions about
stack of regions
![Page 23: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/23.jpg)
23
RGN monad: Encapsulation
Encapsulate and run a monadic computation
runRGN ::
8. (8. RGN ) !
“for all stacks” ) no assumptions about
stack of regions
![Page 24: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/24.jpg)
24
RGN monad: Encapsulation
Encapsulate and run a monadic computation
runRGN ::
8. (8. RGN ) !
result is independent of stack ) 62 frv() )
region values don’t escape
“for all stacks” ) no assumptions about
stack of regions
![Page 25: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/25.jpg)
25
RGN monad: Example
runRGN ( 1.
do a à new [1] 1
c à runRGN ( 2.
do b à new [2] 7
… z = …
new [1] z )
… c … )
1
![Page 26: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/26.jpg)
26
RGN monad: Example
runRGN ( 1.
do a à new [1] 1
c à runRGN ( 2.
do b à new [2] 7
… z = …
new [1] z )
… c … )
1 a : 1
inputallocated in first region
![Page 27: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/27.jpg)
27
RGN monad: Example
runRGN ( 1.
do a à new [1] 1
c à runRGN ( 2.
do b à new [2] 7
… z = …
new [1] z )
… c … )
1
2
a : 1
input allocated in first region
![Page 28: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/28.jpg)
28
RGN monad: Example
runRGN ( 1.
do a à new [1] 1
c à runRGN ( 2.
do b à new [2] 7
… z = …
new [1] z )
… c … )
1
2
a : 1
b : 7
temporary allocated in second region
inputallocated in first region
![Page 29: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/29.jpg)
29
RGN monad: Example
runRGN ( 1.
do a à new [1] 1
c à runRGN ( 2.
do b à new [2] 7
… z = …
new [1] z )
… c … )
1
2
a : 1
c : 8
b : 7
temporary allocated in second region
input and outputallocated in first region
![Page 30: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/30.jpg)
30
RGN monad: Example
runRGN ( 1.
do a à new [1] 1
c à runRGN ( 2.
do b à new [2] 7
… z = …
new [1] z )
… c … )
1 a : 1
c : 8
temporary allocated in second region
input and outputallocated in first region
![Page 31: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/31.jpg)
31
RGN monad: Example
runRGN ( 1.
do a à new [1] 1
c à runRGN ( 2.
do b à new [2] 7
… z = …
new [1] z )
… c … )
allocating in older region
requires RGN 1 type
allocating in younger region
requires RGN 2 type
![Page 32: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/32.jpg)
32
RGN monad: Witnesses
Witness type
Pf(1 · 2) –
type-level proof that the stack of regions 1
is a substack of the stack of regions 2
![Page 33: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/33.jpg)
33
RGN monad: Witnesses
Witness operations
coerceRGN ::
81,2,. Pf(1 · 2) ! RGN 1 ! RGN 2
transSub ::
81,2,3. Pf(1 · 2) ! Pf(2 · 3) ! Pf(1 · 3)
![Page 34: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/34.jpg)
34
RGN monad: Regions
Regions are created and destroyedwith a lexically scoped construct
letRGN ::
81,. (82. Pf(1 · 2) ! RGN 2 ) ! RGN 1
![Page 35: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/35.jpg)
35
RGN monad: Regions
Regions are created and destroyedwith a lexically scoped construct
letRGN ::
81,. (82. Pf(1 · 2) ! RGN 2 ) ! RGN 1
![Page 36: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/36.jpg)
36
RGN monad: Example
letRGN ( 1. pf1.
do a à new [1] 1
c à letRGN ( 2. pf2.
do b à new [2] 7
… z = …
coerceRgn pf (new [1] z ))
… c … )
1
2
a : 1
c : 8
b : 7
![Page 37: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/37.jpg)
37
Limitations of LIFO Regions
Lexical scope is ill-suited for– iterative computations
Conway’s Game of Life; copying GC
– CPS-based computations– event-based computations
![Page 38: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/38.jpg)
38
Limitations of LIFO Regions
Lexical scope is ill-suited for– iterative computations
Conway’s Game of Life; copying GC
– CPS-based computations– event-based computations
But, lexical scope was ensuring that the stack of regions was used in a single-threaded manner
![Page 39: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/39.jpg)
39
Substructural Type Systems
Provide core mechanisms to restrict the number and order of uses of data and operations– generalization of linear type systems
![Page 40: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/40.jpg)
40
Substructural Type System: UL
Qualifiers
q ::= U j L
PreTypes
::= 1 j 1 £ 2 j 1 ! 2 j 8. j 9.
Types
::= q
![Page 41: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/41.jpg)
41
Substructural Type System: UL
Qualifiers
q ::= U j L
PreTypes
::= 1 j 1 £ 2 j 1 ! 2 j 8. j 9.
Types
::= q
How maythe value be used?
![Page 42: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/42.jpg)
42
Substructural Type System: UL
Qualifiers
q ::= U j L
PreTypes
::= 1 j 1 £ 2 j 1 ! 2 j 8. j 9.
Types
::= q
How maythe value be used?
How often maythe value be used?
![Page 43: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/43.jpg)
43
Substructural Qualifiers
UnrestrictedDrop Copy
Linear
must be “used” exactly once
may be “used” an arbitrary # of times
![Page 44: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/44.jpg)
44
Substructural Type System for Regions
Provide core mechanisms to restrict the number and order of uses of data and operations– generalization of linear type systems
Key insights (rgnUL):– Separate region names from region liveness– Region liveness witnessed by types– Sufficient for encoding FRGN calculus
and “advanced” Cyclone region features
![Page 45: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/45.jpg)
45
rgnUL = UL + Regions
PreTypes
::= … j cap j ref j 8. j 9.
“capability” for region ;mediates all access to a region
for allocating, reading, and writing
![Page 46: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/46.jpg)
46
rgnUL: Region Primitives
Regions are created and destroyedwith separate operations
newrgn ::U1 ! (9. Lcap )
freergn ::
8. (Lcap ! U1)
![Page 47: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/47.jpg)
47
rgnUL: Region Primitives
Regions are created and destroyedwith separate operations
newrgn ::U1 ! (9. Lcap )
freergn ::
8. (Lcap ! U1)
Produces a capability.
Consumes a capability.
![Page 48: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/48.jpg)
48
rgnUL: Region Primitives
Regions are created and destroyedwith separate operations
newrgn ::U1 ! (9. Lcap )
freergn ::
8. (Lcap ! U1)
![Page 49: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/49.jpg)
49
rgnUL: Region Primitives
new ::
8,. ((Lcap £ U) !(Lcap £ Uref U))
read ::
8,. ((Lcap £ Uref U) !(Lcap £ U))
![Page 50: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/50.jpg)
50
rgnUL: Region Primitives
new ::
8,. ((Lcap £ U) !(Lcap £ Uref U))
read ::
8,. ((Lcap £ Uref U) !(Lcap £ U))
Returns a capability.
Requires a capability.
![Page 51: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/51.jpg)
51
rgnUL: Region Primitives
new ::
8,. ((Lcap £ U) !(Lcap £ Uref U)
read ::
8,. ((Lcap £ Uref U) !(Lcap £ U)
![Page 52: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/52.jpg)
52
Translation: FRGN to rgnUL, Types
« RGN ¬ = U( ! L( £ «¬))
![Page 53: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/53.jpg)
53
Translation: FRGN to rgnUL, Types
« RGN ¬ = ! ( £ «¬)
– operational behavior of monad is store/stack-passing
![Page 54: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/54.jpg)
54
Translation: FRGN to rgnUL, Types
« RGN ¬ = ! ( £ «¬)
– operational behavior of monad is store/stack-passing
![Page 55: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/55.jpg)
55
Translation: FRGN to rgnUL, Types
« RGN ¬ = ! ( £ «¬)
– operational behavior of monad is store/stack-passing– represent “stack of regions”
as a sequence of linear capabilities,formed out of nested linear tuples
![Page 56: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/56.jpg)
56
Translation: FRGN to rgnUL, Types
« RGN ¬ = ! ( £ «¬)
– operational behavior of monad is store/stack-passing– represent “stack of regions”
as a sequence of linear capabilities,formed out of nested linear tuples
![Page 57: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/57.jpg)
57
Translation: FRGN to rgnUL, Ops
« returnRGN [] [] e ¬ =let res : «¬ = «e¬ inUstk:. Lhstk,resi
« thenRGN [] [a] [b] e1 e2 ¬ =let f : «RGN a¬= «e1¬ inlet g : «a ! RGN b¬ = «e2¬ inUstk:. let hstk,resi = f stk in g res stk
![Page 58: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/58.jpg)
58
Translation: FRGN to rgnUL, Ops
« returnRGN [] [] e ¬ =let res : «¬ = «e¬ inUstk:. Lhstk,resi
« thenRGN [] [a] [b] e1 e2 ¬ =let f : «RGN a¬= «e1¬ inlet g : «a ! RGN b¬ = «e2¬ inUstk:. let hstk,resi = f stk in g res stk
Store-passing
encoding
![Page 59: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/59.jpg)
59
Translation: FRGN to rgnUL, Types
« Pf(1 · 2) ¬ = U(9’. Iso(2, L(1 £ ’)))
![Page 60: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/60.jpg)
60
Translation: FRGN to rgnUL, Types
« Pf(1 · 2) ¬ = U(9’. Iso(2, L(1 £ ’)))
– Isomorphism between 2 and L(1 £ ’), for some “slack” ’
![Page 61: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/61.jpg)
61
Translation: FRGN to rgnUL, Types
« Pf(1 · 2) ¬ = U(9’. Iso(2, L(1 £ ’)))
– Isomorphism between 2 and L(1 £ ’), for some “slack” ’
– Proof that 1 is a substack of 2 is persistent
– Liveness of 1 and 2 is ephemeral
![Page 62: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/62.jpg)
62
Translation: FRGN to rgnUL, Types
« Ref ¬ = U(9. U(U(9’. Iso(, L(’ £ Lcap )))« Ref ¬ = U(9. U(£ Uref «¬))
![Page 63: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/63.jpg)
63
Translation: FRGN to rgnUL, Types
« Ref ¬ = 9. (9’. Iso(, L(’ £ Lcap ))« Ref ¬ = 9. (£ Uref «¬)
Existential fixes region
![Page 64: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/64.jpg)
64
Translation: FRGN to rgnUL, Types
« Ref ¬ = 9. (9’. Iso(, L(’ £ Lcap ))« Ref ¬ = 9. (£ Uref «¬)
Existential fixes region Isomorphism witnesses membership of in
![Page 65: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/65.jpg)
65
Translation: FRGN to rgnUL, Ops
« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! Hnd 2 ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,hcap,hndi) = newrgn Lhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let phnd = Upack(,UhUpack(1,Uhid,idi),hndi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit phnd stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] Lhcap,hndi inUstk1:1.Lhstk1,resi
![Page 66: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/66.jpg)
66
Translation: FRGN to rgnUL, Ops
« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi
Stack-passing encoding
![Page 67: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/67.jpg)
67
Translation: FRGN to rgnUL, Ops
« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi
Create & destroy a new region
![Page 68: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/68.jpg)
68
Translation: FRGN to rgnUL, Ops
« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi
Construct rep.of new stack
![Page 69: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/69.jpg)
69
Translation: FRGN to rgnUL, Ops
« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi
Run comp. and recover old stack
and new cap
![Page 70: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/70.jpg)
70
Translation: FRGN to rgnUL, Ops
« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi
Construct isomorphism
![Page 71: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/71.jpg)
71
Translation: FRGN to rgnUL, Ops
« letRGN [1] [] e ¬ = let f : «82. Pf(1·2) ! RGN 2 ¬ = «e¬ inUstk1:1.let pack(,cap) = newrgn Uhi inUstk1:1.let stk2 = Lhstk1,capi inUstk1:1.let id = Ustk: L(1 Lcap ).stk inUstk1:1.let pwit = Upack(Lcap ,Uhid,idi) inUstk1:1.let hstk2,resi = f [L(1 Lcap )] pwit stk2 inUstk1:1.let hstk1,capi = stk2 inUstk1:1.let hi = freergn [] cap inUstk1:1.Lhstk1,resi
![Page 72: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/72.jpg)
72
Encoding Cyclone Features
Many of Cyclone’s features fit into this framework– Lexical Regions– Dynamic Regions– Heap– Reaps– Unique Pointers
See paper for more details.
![Page 73: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/73.jpg)
73
Future Work
In practice, capabilities shouldn’t be values
Encode results of region analyses– Aiken et.al. [PLDI’95], Henglein et.al. [PPDP’01]
Combine convenience of monadic encapsulation with power of substructural threading
![Page 74: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/74.jpg)
74
Conclusion
Substructural type systems applicable to encoding region-based memory management– target-level language exposes commonalities
in source-level language features
Scope vs. Lifetime– Lexical scope of region name– Un-scoped lifetime of region capability
Unrestricted witnesses vs. Linear capabilities
![Page 75: Linear Regions Are All You Need](https://reader035.vdocuments.site/reader035/viewer/2022062408/56813f65550346895daa3a72/html5/thumbnails/75.jpg)
75