sheep cloning - github pages · paley li, nicholas cameron, and james noble 1 . object cloning...

79
SHEEP CLONING Paley Li, Nicholas Cameron, and James Noble 1

Upload: others

Post on 09-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SHEEP CLONING - GitHub Pages · 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

SHEEP CLONING

Paley Li, Nicholas Cameron, and James Noble

1

Page 2: SHEEP CLONING - GitHub Pages · 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

Object cloning

• How do you do object cloning?

2

Page 3: SHEEP CLONING - GitHub Pages · 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

Shallow cloning

• Copies an object and alias the references in that object.

3

Page 4: SHEEP CLONING - GitHub Pages · 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

Shallow cloning

3

foo

a

b

• Copies an object and alias the references in that object.

Page 5: SHEEP CLONING - GitHub Pages · 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

Shallow cloning

3

foo’

b

a

foo

a

b

• Copies an object and alias the references in that object.

Page 6: SHEEP CLONING - GitHub Pages · 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

Deep cloning

• Copies the object and its referenced objects.

4

Page 7: SHEEP CLONING - GitHub Pages · 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

Deep cloning

4

• Copies the object and its referenced objects.

foo

a

b

Page 8: SHEEP CLONING - GitHub Pages · 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

Deep cloning

• Copies the object and its referenced objects.

4

a

foo

b

a

foo’

b

Page 9: SHEEP CLONING - GitHub Pages · 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

- Shallow cloning is too shallow

9

displayWindow

5

scrollBar

Page 10: SHEEP CLONING - GitHub Pages · 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

- Shallow cloning is too shallow

10

displayWindow

5

scrollBar

displayWindow’

Page 11: SHEEP CLONING - GitHub Pages · 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

11

6

- Shallow cloning is too shallow

displayWindow displayWindow’

Page 12: SHEEP CLONING - GitHub Pages · 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

7

- Deep cloning is too deep

displayWindow

imageDatabase

Page 13: SHEEP CLONING - GitHub Pages · 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

7

- Deep cloning is too deep

displayWindow

imageDatabase

displayWindow’

imageDatabase’

Page 14: SHEEP CLONING - GitHub Pages · 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

8

- Deep cloning is too deep

displayWindow displayWindow’

Page 15: SHEEP CLONING - GitHub Pages · 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

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

Page 16: SHEEP CLONING - GitHub Pages · 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

Common practices

10

• Cloning in C++ :

• Copy constructors and assignment operators.

• Cloning in Eiffel :

• Inherit shallow and deep cloning from the

ANY class.

Page 17: SHEEP CLONING - GitHub Pages · 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

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

Page 18: SHEEP CLONING - GitHub Pages · 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

• 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

Page 19: SHEEP CLONING - GitHub Pages · 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

Ownership Types

• Ownership types enforce a hierarchical topology over the

heap.

13

Page 20: SHEEP CLONING - GitHub Pages · 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

• 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

Page 21: SHEEP CLONING - GitHub Pages · 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

• 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 =

Page 22: SHEEP CLONING - GitHub Pages · 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

Deep Ownership

15

• All reference paths to an object must pass through that

object’s owner.

• Also known as owners-as-dominators.

X

X

Page 23: SHEEP CLONING - GitHub Pages · 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

Deep Ownership

15

• All reference paths to an object must pass through that

object’s owner.

• Also known as owners-as-dominators.

Page 24: SHEEP CLONING - GitHub Pages · 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

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

Page 25: SHEEP CLONING - GitHub Pages · 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

17

- Sheep cloning is just right!

displayWindow

Page 26: SHEEP CLONING - GitHub Pages · 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

17

- Sheep cloning is just right!

displayWindow displayWindow’

Page 27: SHEEP CLONING - GitHub Pages · 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

17

- Sheep cloning is just right!

displayWindow displayWindow’

Page 28: SHEEP CLONING - GitHub Pages · 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

17

- Sheep cloning is just right!

displayWindow displayWindow’

Page 29: SHEEP CLONING - GitHub Pages · 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

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

Page 30: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 31: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 32: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 33: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 34: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 35: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 36: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 37: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 38: SHEEP CLONING - GitHub Pages · 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

A touch of formal

19

Page 39: SHEEP CLONING - GitHub Pages · 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

A touch of formal

20

Page 40: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

Page 41: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

Original object

Page 42: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

Original object

Page 43: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

Original object

Page 44: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

Original heap

Page 45: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

Map

Page 46: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

Sheep clone

Page 47: SHEEP CLONING - GitHub Pages · 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

A touch of formal

21

New heap (containing the

Sheep clone)

Page 48: SHEEP CLONING - GitHub Pages · 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

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.

Page 49: SHEEP CLONING - GitHub Pages · 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

Can we clone it?

23

B

A

C

D

• Lets Sheep clone object A.

Page 50: SHEEP CLONING - GitHub Pages · 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

Can we clone it?

23

• R-SheepInside creates the object A’ by copying A.

B

A

C

D

A’

Map: A A’

Page 51: SHEEP CLONING - GitHub Pages · 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

Can we clone it?

23

• R-SheepOutside creates an alias to D.

B

A

C

D

A’

Map: A A’

D D

Page 52: SHEEP CLONING - GitHub Pages · 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

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’

Page 53: SHEEP CLONING - GitHub Pages · 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

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’

Page 54: SHEEP CLONING - GitHub Pages · 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

…. 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’

Page 55: SHEEP CLONING - GitHub Pages · 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

Proving the formalism

24

Page 56: SHEEP CLONING - GitHub Pages · 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

Proving the formalism

24

Page 57: SHEEP CLONING - GitHub Pages · 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

Proving the formalism

25

Page 58: SHEEP CLONING - GitHub Pages · 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

Proving the formalism

25

Page 59: SHEEP CLONING - GitHub Pages · 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

Proving the formalism

25

Page 60: SHEEP CLONING - GitHub Pages · 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

Proving the formalism

25

Page 61: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

26

B

A

C

D

A’

B’

C’

= A Where:

= A’

Page 62: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

26

B

A

C

D

A’

B’

C’

= A Where:

= A’

Page 63: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

27

B

A

C

D

A’

B’

C’

Page 64: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

27

B

A

C

D

A’

B’

C’

Page 65: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

27

B

A

C

D

A’

B’

C’

Page 66: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

28

B

A

C

D

A’

B’

C’

Page 67: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

28

B

A

C

D

A’

B’

C’

A’’ A’

Page 68: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

28

B

A

C

D

A’

B’

C’

B’ A’, C’’ A’

Page 69: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

29

B

A

C

D

A’

B’

C’

A ’ D

Page 70: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

29

B

A

C

D

A’

B’

C’

A’ D

Page 71: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

30

B

A

C

A’

B’

C’

D

Page 72: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

30

B

A

C

A’

B’

C’

A ’ D

D

Page 73: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

31

B

A

C

D

A’

B’

C’

A’ ’ D

Page 74: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

31

B

A

C

A’

B’

C’

D

A’ ’ D

Page 75: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

32

B

A

C

D

A’

B’

C’

Page 76: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

32

B

A

C

D

A’

B’

C’

Page 77: SHEEP CLONING - GitHub Pages · 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

Correctness of the formalism

32

B

A

C

D

A’

B’

C’

Page 78: SHEEP CLONING - GitHub Pages · 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

Summary

• Shallow is too shallow.

• Deep is too deep.

• Sheep = shallow + deep.

• Formalised sheep cloning.

• Proved soundness and correctness.

33

Page 79: SHEEP CLONING - GitHub Pages · 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

Thank you.

Questions?

34