sheep cloning - github pages · paley li, nicholas cameron, and james noble 1 . object cloning...
TRANSCRIPT
SHEEP CLONING
Paley Li, Nicholas Cameron, and James Noble
1
Object cloning
• How do you do object cloning?
2
Shallow cloning
• Copies an object and alias the references in that object.
3
Shallow cloning
3
foo
a
b
• Copies an object and alias the references in that object.
Shallow cloning
3
foo’
b
a
foo
a
b
• Copies an object and alias the references in that object.
Deep cloning
• Copies the object and its referenced objects.
4
Deep cloning
4
• Copies the object and its referenced objects.
foo
a
b
Deep cloning
• Copies the object and its referenced objects.
4
a
foo
b
a
foo’
b
- Shallow cloning is too shallow
9
displayWindow
5
scrollBar
- Shallow cloning is too shallow
10
displayWindow
5
scrollBar
displayWindow’
11
6
- Shallow cloning is too shallow
displayWindow displayWindow’
7
- Deep cloning is too deep
displayWindow
imageDatabase
7
- Deep cloning is too deep
displayWindow
imageDatabase
displayWindow’
imageDatabase’
8
- Deep cloning is too deep
displayWindow displayWindow’
Common practices
• Cloning in Java (Cloneable) and C# (ICloneable):
• Default clone() method is shallow.
• Defining deep cloning is inconvenient and prone to bugs.
• Requires type casting.
9
Common practices
10
• Cloning in C++ :
• Copy constructors and assignment operators.
• Cloning in Eiffel :
• Inherit shallow and deep cloning from the
ANY class.
Common practices
• Most practices still suffer from the flaws of shallow and
deep cloning.
• Not automated.
• “Programmer knows best” - they have to define their own cloning.
• What if we have the information to produce more sensible
clones, but had overlooked it?
11
• We aim to formalise a cloning model that is just right.
• It needs to be able to identify areas that are “important” to
an object.
• Only copy those “important” areas.
12
- The ideal model
Ownership Types
• Ownership types enforce a hierarchical topology over the
heap.
13
• Context is the formal set of objects owned by an object.
• Representation is the set of objects which are
conceptually part of an object.
14
Ownership Types
• Context is the formal set of objects owned by an object.
• Representation is the set of objects which are
conceptually part of an object.
14
Ownership Types
Representation = context =
Deep Ownership
15
• All reference paths to an object must pass through that
object’s owner.
• Also known as owners-as-dominators.
X
X
Deep Ownership
15
• All reference paths to an object must pass through that
object’s owner.
• Also known as owners-as-dominators.
Sheep = Shallow + Deep
• Utilises ownership types to identify the “important bits” of
each object.
• Cloning an object’s representation:
• Copies every object inside the object’s context.
• Aliases every reference to objects outside the object’s context.
16
17
- Sheep cloning is just right!
displayWindow
17
- Sheep cloning is just right!
displayWindow displayWindow’
17
- Sheep cloning is just right!
displayWindow displayWindow’
17
- Sheep cloning is just right!
displayWindow displayWindow’
Sheep cloning
• We have formalised sheep cloning in an ownership
system with deep ownership.
• We have proved soundness and an assortment of
correctness property of our formalism.
18
A touch of formal
19
A touch of formal
19
A touch of formal
19
A touch of formal
19
A touch of formal
19
A touch of formal
19
A touch of formal
19
A touch of formal
19
A touch of formal
19
A touch of formal
20
A touch of formal
21
A touch of formal
21
Original object
A touch of formal
21
Original object
A touch of formal
21
Original object
A touch of formal
21
Original heap
A touch of formal
21
Map
A touch of formal
21
Sheep clone
A touch of formal
21
New heap (containing the
Sheep clone)
A touch of formal
22
• SheepAux function:
• R-SheepInside: Copies the object if it is inside the original
object.
• R-SheepOutside: Creates an alias to the object if the object is
outside the original object.
• R-SheepRef: Creates a reference to an existing Sheep clone of an
object using the Map.
• R-SheepNull: Returns a null, when Sheep cloning a null.
Can we clone it?
23
B
A
C
D
• Lets Sheep clone object A.
Can we clone it?
23
• R-SheepInside creates the object A’ by copying A.
B
A
C
D
A’
Map: A A’
Can we clone it?
23
• R-SheepOutside creates an alias to D.
B
A
C
D
A’
Map: A A’
D D
Can we clone it?
23
• R-SheepInside creates the object B’ by copying B.
B
A
C
D
A’
Map: A A’
D D B B’
B’
Can we clone it?
23
• R-SheepInside creates the object C’ by copying C.
B
A
C
D
A’
B’
C’
Map: A A’
D D B B’
C C’
…. Yes we can!
23
• R-SheepRef creates the reference from object C’ to object B’ using the map.
B
A
C
D
A’
B’
C’
Map: A A’
D D B B’
C C’
Proving the formalism
24
Proving the formalism
24
Proving the formalism
25
Proving the formalism
25
Proving the formalism
25
Proving the formalism
25
Correctness of the formalism
26
B
A
C
D
A’
B’
C’
= A Where:
= A’
Correctness of the formalism
26
B
A
C
D
A’
B’
C’
= A Where:
= A’
Correctness of the formalism
27
B
A
C
D
A’
B’
C’
Correctness of the formalism
27
B
A
C
D
A’
B’
C’
Correctness of the formalism
27
B
A
C
D
A’
B’
C’
Correctness of the formalism
28
B
A
C
D
A’
B’
C’
Correctness of the formalism
28
B
A
C
D
A’
B’
C’
A’’ A’
Correctness of the formalism
28
B
A
C
D
A’
B’
C’
B’ A’, C’’ A’
Correctness of the formalism
29
B
A
C
D
A’
B’
C’
A ’ D
Correctness of the formalism
29
B
A
C
D
A’
B’
C’
A’ D
Correctness of the formalism
30
B
A
C
A’
B’
C’
D
Correctness of the formalism
30
B
A
C
A’
B’
C’
A ’ D
D
Correctness of the formalism
31
B
A
C
D
A’
B’
C’
A’ ’ D
Correctness of the formalism
31
B
A
C
A’
B’
C’
D
A’ ’ D
Correctness of the formalism
32
B
A
C
D
A’
B’
C’
Correctness of the formalism
32
B
A
C
D
A’
B’
C’
Correctness of the formalism
32
B
A
C
D
A’
B’
C’
Summary
• Shallow is too shallow.
• Deep is too deep.
• Sheep = shallow + deep.
• Formalised sheep cloning.
• Proved soundness and correctness.
33
Thank you.
Questions?
34