1 SHEEP CLONING Paley Li, Nicholas Cameron, and James Noble
2 Object cloning • How do you do object cloning?
3 Shallow cloning • Copies an object and alias the references in that object.
3 Shallow cloning • Copies an object and alias the references in that object. foo a b
3 Shallow cloning • Copies an object and alias the references in that object. foo ’ foo a a b b
4 Deep cloning • Copies the object and its referenced objects.
4 Deep cloning • Copies the object and its referenced objects. foo a b
4 Deep cloning • Copies the object and its referenced objects. foo ’ foo a a b b
5 - Shallow cloning is too shallow displayWindow scrollBar 9
5 - Shallow cloning is too shallow displayWindow ’ displayWindow scrollBar 10
6 - Shallow cloning is too shallow displayWindow ’ displayWindow 11
7 - Deep cloning is too deep displayWindow imageDatabase
7 - Deep cloning is too deep displayWindow ’ displayWindow imageDatabase’ imageDatabase
8 - Deep cloning is too deep displayWindow ’ displayWindow
9 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.
10 Common practices • Cloning in C++ : • Copy constructors and assignment operators. • Cloning in Eiffel : • Inherit shallow and deep cloning from the ANY class.
11 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?
12 - The ideal model • 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. • O nly copy those “important” areas.
13 Ownership Types • Ownership types enforce a hierarchical topology over the heap.
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 • Context is the formal set of objects owned by an object. • Representation is the set of objects which are conceptually part of an object. Representation = context =
15 Deep Ownership • All reference paths to an object must pass through that object’s owner. • Also known as owners-as-dominators. X X
15 Deep Ownership • All reference paths to an object must pass through that object’s owner. • Also known as owners-as-dominators.
16 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.
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
18 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.
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 A touch of formal Original object
21 A touch of formal Original object
21 A touch of formal Original object
21 A touch of formal Original heap
21 A touch of formal Map
21 A touch of formal Sheep clone
21 A touch of formal New heap (containing the Sheep clone)
22 A touch of formal • 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.
23 Can we clone it? • Lets Sheep clone object A. D A C B
23 Can we clone it? • R-SheepInside creates the object A ’ by copying A. D A ’ A C B Map: A A ’
23 Can we clone it? • R-SheepOutside creates an alias to D. D A ’ A C B Map: A A ’ D D
23 Can we clone it? • R-SheepInside creates the object B ’ by copying B. D A ’ A C B ’ B Map: A A ’ D D B B ’
23 Can we clone it? • R-SheepInside creates the object C ’ by copying C. D A ’ A C ’ C B ’ B Map: A A ’ D D B B ’ C C ’
23 …. Yes we can! • R-SheepRef creates the reference from object C ’ to object B ’ using the map. D A ’ A C ’ C B ’ B Map: A A ’ D D B B ’ C C ’
24 Proving the formalism
24 Proving the formalism
25 Proving the formalism
25 Proving the formalism
25 Proving the formalism
25 Proving the formalism
26 Correctness of the formalism Where: = A = A ’ D A ’ A C ’ C B ’ B
26 Correctness of the formalism Where: = A = A ’ D A ’ A C ’ C B ’ B
27 Correctness of the formalism D A ’ A C ’ C B ’ B
27 Correctness of the formalism D A ’ A C ’ C B ’ B
27 Correctness of the formalism D A ’ A C ’ C B ’ B
28 Correctness of the formalism D A ’ A C ’ C B ’ B
28 Correctness of the formalism A ’ ’ A ’ D A ’ A C ’ C B ’ B
28 Correctness of the formalism B ’ A ’ , C ’ ’ A ’ D A ’ A C ’ C B ’ B
29 Correctness of the formalism A ’ D D A ’ A C ’ C B ’ B
29 Correctness of the formalism A ’ D D A ’ A C ’ C B ’ B
30 Correctness of the formalism D A ’ A C ’ C B ’ B
30 Correctness of the formalism A ’ D D A ’ A C ’ C B ’ B
31 Correctness of the formalism A ’ ’ D D A ’ A C ’ C B ’ B
31 Correctness of the formalism A ’ ’ D D A ’ A C ’ C B ’ B
32 Correctness of the formalism D A ’ A C ’ C B ’ B
32 Correctness of the formalism D A ’ A C ’ C B ’ B
32 Correctness of the formalism D A ’ A C ’ C B ’ B
33 Summary • Shallow is too shallow. • Deep is too deep. • Sheep = shallow + deep. • Formalised sheep cloning. • Proved soundness and correctness.
34 Thank you. Questions?
Recommend
More recommend