Skip to content

Commit 7526e82

Browse files
committed
Update examples
1 parent dd32cd1 commit 7526e82

File tree

5 files changed

+56
-40
lines changed

5 files changed

+56
-40
lines changed

examples/02-freezing.frank

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ freeze(x)
99
# Assigning to a field of `x` afterward will throw an error
1010
# x.field = {} # ERROR
1111

12-
# Mutability only applies to the value variables pointing to
13-
# immutable data can be reassigned.
12+
# Calling `freeze(x)` freezes the object `x` points to but not the variable
13+
# itself, so x can be reassigned
1414
x = {}
1515
x.f = {} # Mutation of x
1616

@@ -20,9 +20,9 @@ x.f = {} # Mutation of x
2020
freeze(x)
2121

2222
# The immutable status in Lungfish refers to the observable state.
23-
# This means that the reference can remain mutable as long as it is
24-
# not observable in the program. Notice how this new reference
25-
# increases the reference count.
23+
# This means that the reference count can remain mutable as long as
24+
# it is not observable in the program. Notice how this new reference
25+
# increases the reference count. See §5.3.5 for more details.
2626
xx = x
2727

2828
# Setting all references to an immutable object to `None`

examples/03-regions1.frank

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
# The previous examples only worked in the local region drawn in light
33
# green. This example shows how new regions can be created and used.
44
#
5-
# The `Region()` constructor creates a new region and returns the bridge
5+
# The `Region()` constructor creates a new region and returns its bridge
66
# object. The new region is drawn in yellow. Any object in the yellow
7-
# rectangle belongs to the region. The trapezoid shape identifies the
7+
# rectangle belongs to the region. The trapezoid shape denotes the
88
# bridge object. It displays the following information about the region:
9-
# - LRC: The number of incomming references from the local region.
10-
# - SBRC: The number of open subregion.
9+
# - LRC: The number of incoming references from the local region. This
10+
# counter tracks references to all objects in the region not
11+
# just the bridge object. See §3.1 for more details.
12+
# - SBRC: The number of open subregions.
1113
# - RC: The reference count of the bridge object.
1214
r = Region()
1315

14-
# Any objects assigned to the bridge object or an object in the region
16+
# Any objects reachable from the bridge or an object in the region
1517
# will automatically be part of the region. Notice how the new dictionaries
1618
# are members of the region, indicated by them being in the same yellow box.
1719
r.field = {}
@@ -22,23 +24,24 @@ r.field.data = {}
2224
r.field.bridge = r
2325
r.field.data.parent = r.field
2426

25-
# All objects are by default created in the local region.
27+
# All objects are by created in the local region.
2628
x = {}
2729
x.data = {}
2830

29-
# When a region references an object in the local region, it takes ownership of the
30-
# object. All referenced objects are also moved into the region. Figure 7 in the paper
31-
# shows the individual steps of this process.
31+
# An object in the local region is moved into a region, when an object in the
32+
# region references it. All reachable objects from the moved object are also
33+
# moved into the region. Figure 7 in the paper shows the individual steps of
34+
# this process.
3235
r.x = x
3336

34-
# Moving the value of `x` into the region increased the LRC since the variable x
35-
# is a reference from the local frame into the region. Setting x to `None` will
36-
# decrement the LRC again.
37+
# Moving the value of `x` into the region increased the LRC since the variable `x`
38+
# is a reference from the local frame into the region. Reassigning `x` to another
39+
# value will decrement the LRC again. This is done by a write barrier, discussed in
40+
# §3.2 of the paper.
3741
x = None
3842

39-
# References from within a region are allowed to reference frozen objects. This
40-
# is safe since frozen objects don't have an owner and can be safely shared across
41-
# threads.
43+
# References to frozen objects are unrestricted. Table 1 in the paper provides a
44+
# good overview of what references are allowed.
4245
r.x.data = None
4346

4447
# When a region has no incoming references it and all contained objects can

examples/04-regions2.frank

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
# The previous example covers how new regions can be created. This
1+
# 03-regions1.frank covered how new regions can be created. This
22
# example covers how ownership is enforced with regions.
33
r1 = Region()
44
r1.data = {}
55
r2 = Region()
66

7-
# Region 1 owns a dictionary with the name `data`. All objects can at most have
8-
# one owner. Created a reference from one region to an object in another region
7+
# Region 1 owns an object with the name `data`. All objects can at most have
8+
# one owner. Creating a reference from one region to an object in another region
99
# will result in an error
1010
# r2.data = r1.data # Error
1111

12-
# However, it is allowed to create a single reference to a bridge object of
13-
# another region. This reference is called the owning reference, indicated by
12+
# However, creating a single reference to a bridge object of another region
13+
# is allowed. This reference is called the owning reference, indicated by
1414
# the solid orange arrow. The region of the referenced bridge object is now
1515
# a subregion of the first region.
1616
r1.data.r2 = r2
1717

1818
# Attempts to add additional owning references will result in an error:
1919
# r1.new = r2 # Error
2020

21+
# Regions are arranged in a forest. It is not allowed to create a cycle
22+
# between regions. Attempting to make `r1` a subregion of `r2` which is
23+
# currently a subregion of `r1` will result in an error:
24+
# r2.r1 = r1 # Error
25+
2126
# Region 2 is currently considered open as it has an incoming reference from
2227
# the local region. This is indicated by the LRC not being zero. The parent
23-
# region r1 has a SBRC of 1 indicating that a subregion of it is open.
28+
# region `r1` has a SBRC of 1 indicating that a subregion of it is open.
2429
#
25-
# Removing the local reference into r2 will close the region and also decrement
30+
# Removing the local reference into `r2` will close the region and also decrement
2631
# the SBRC of the parent region.
2732
r2 = None
2833

@@ -33,15 +38,15 @@ r2 = r1.data.r2
3338
# This is possible since there can only be one owing reference at a time.
3439
r1.data.r2 = None
3540

36-
# Region 2 can now take ownership of r1
41+
# Region 2 can now take ownership of `r1`.
3742
r2.r1 = r1
3843

3944
# Regions can also be frozen to allow multiple regions to reference them.
4045
# The bridge object of the frozen region will remain, but it will lose the
4146
# `[RegionObject]` prototype.
4247
freeze(r1)
4348

44-
# The previous bridge object referenced by r1 can now be referenced by other
49+
# The previous bridge object referenced by `r1` can now be referenced by other
4550
# regions.
4651
r3 = Region()
4752
r3.r1 = r1

examples/05-regions3.frank

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 04-regions2.frank covered how ownership is enfored with regions.
12
# This example covers some built-in functions for regions.
23

34
r1 = Region()
@@ -28,6 +29,9 @@ merge(r2.child, r2)
2829

2930
# Regions can also be dissolved, thereby moving all contained
3031
# objects back into the local region.
32+
#
33+
# This function differs syntactically from the paper which
34+
# uses `merge(r2)`.
3135
dissolve(r2)
3236

3337
# This can be used to reconstruct regions. This example uses

examples/06-cowns.frank

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
# This is the sixth example and assumes the knowledge of the
2+
# previous examples.
3+
#
14
# Lungfish uses concurrent owners, called Cowns, to coordinate
2-
# concurrent access at runtime.
5+
# concurrent access at runtime. Cowns and their states are
6+
# introduced in §2.3.1 of the paper.
37
#
48
# Cowns can store immutable objects, regions and other cowns.
5-
# The following will create a cown pointing to the frozen value
6-
# `None`.
9+
# The following will create a cown pointing to the built-in
10+
# frozen value `None`.
711
c1 = Cown(None)
812

9-
# The cown has the status "Release" which means that a concurrent
13+
# The cown has the status "Released" which means that a concurrent
1014
# unit can aquire it and access its data. Attempting to access
1115
# the data in this state will result in an error.
1216
# ref = c1.value # Error
@@ -17,15 +21,15 @@ r1 = Region()
1721
r1.data = {}
1822
data = r1.data
1923

20-
# A cown created from an open region has the status "Pending". This
21-
# status allows access to the contained value. However, it will switch
22-
# to the "Released" status one the region is closed
24+
# A cown created from an open region has the status "Pending Release".
25+
# This status allows access to the contained value. However, it will
26+
# switch to the "Released" status when the region is closed
2327
c2 = Cown(r1)
2428
close(c2.value)
2529

26-
# Cowns can be safely shared across threads since they enfore ownership
27-
# and concurrent coordination at runtime. They are therefore allowed to
28-
# be referenced by frozen objects. The freeze will not effect the cown
30+
# Cowns can be safely shared across threads since they dynamically enforce
31+
# ownership and concurrent coordination at runtime. They are therefore allowed
32+
# to be referenced by frozen objects. The freeze will not effect the cown
2933
# or the contained values.
3034
x = {}
3135
x.cown = c2
@@ -44,6 +48,6 @@ r3.cown = c2
4448
c3 = Cown(move r2)
4549

4650
# Cowns can also point to other cowns. The second cown is also
47-
# in the *Released* state since the given cown is allowed to
51+
# in the "Released" state since the given cown is allowed to
4852
# have local references.
4953
c4 = Cown(c2)

0 commit comments

Comments
 (0)