Records A Tuple (Mathematically): is a function f : { 0, 1, , n 1 - - PowerPoint PPT Presentation

records
SMART_READER_LITE
LIVE PREVIEW

Records A Tuple (Mathematically): is a function f : { 0, 1, , n 1 - - PowerPoint PPT Presentation

Records A Tuple (Mathematically): is a function f : { 0, 1, , n 1 } Set Example: ( 5, 3.1, 6.0, c , foo , 13, 14, d ) J.Carette (McMaster) CS 2S03 1 / 19 Records A Tuple (Mathematically): is a


slide-1
SLIDE 1

Records

A Tuple (Mathematically): is a function f : {0, 1, · · · , n − 1} → Set Example: (5, −3.1, 6.0,′ c ′,′′ foo ′′, 13, 14,′ d ′)

J.Carette (McMaster) CS 2S03 1 / 19

slide-2
SLIDE 2

Records

A Tuple (Mathematically): is a function f : {0, 1, · · · , n − 1} → Set Example: (5, −3.1, 6.0,′ c ′,′′ foo ′′, 13, 14,′ d ′) bad style!: Instead of implicit labels use explicit labels! Tuples (Programming Languages): usually of named fields tuple of named fields ≡ records Example: labels: {latitude, longitude, altitude} record: { latitude = 48.715, longitude = 2.208, altitude = 156}

J.Carette (McMaster) CS 2S03 1 / 19

slide-3
SLIDE 3

Defining a Record (Java)

class Point { final double latitude; final double longitude; final double altitude ;}

J.Carette (McMaster) CS 2S03 2 / 19

slide-4
SLIDE 4

Records - Declaration

Point x;

r

x

null

J.Carette (McMaster) CS 2S03 3 / 19

slide-5
SLIDE 5

Records - Declaration

Point x;

r

x

null

new Point ();

x 0.0 0.0 0.0

The value of this expression is the reference r ′ = {latitude = 0.0, longitude = 0.0, altitude = 0.0}

J.Carette (McMaster) CS 2S03 3 / 19

slide-6
SLIDE 6

Records - Allocation

x = new Point ();

x 0.0 0.0 0.0

the environment is e ⊕ [x = r] the memory is [r = r ′, r ′ = {latitude = 0.0, longitude = 0.0, altitude = 0.0}] You can also write:

Point x = new Point ();

J.Carette (McMaster) CS 2S03 4 / 19

slide-7
SLIDE 7

Records - Allocation - Cont.

The following statement creates the environment [x = r ′] and the memory state [r ′ = {latitude = 0.0, longitude = 0.0, altitude = 0.0}].

final Point x = new Point ();

x 0.0 0.0 0.0

J.Carette (McMaster) CS 2S03 5 / 19

slide-8
SLIDE 8

Records - Accessing Fields

x.latitude x.thing : error at compile time

Consider an expression x with x mutable of type Point. Then value of x is m(e(x) (a record) and the value of x.latitude is that of the field latitude of m(m(e(x)).

x 0.0 0.0 0.0

e" m" m"

J.Carette (McMaster) CS 2S03 6 / 19

slide-9
SLIDE 9

Assignment of Fields

class Point { final double latitude; final double longitude; final double altitude ;} Point x = new Point ();

x 0.0 0.0 0.0

✖!

x 0.0 0.0 0.0

✔!

The compiler “knows” which fields are mutable and not and will enforce it!

J.Carette (McMaster) CS 2S03 7 / 19

slide-10
SLIDE 10

Constructs for Handling Records

define a type: class allocate a cell: new access a field: t.l assign to a field: t.l = u;

J.Carette (McMaster) CS 2S03 8 / 19

slide-11
SLIDE 11

Constructors

x = new Point (48.715 , 2.208 , 156.0);

To use, we need to change the class and add a constructor:

Point (final double x, final double y, final double z) { this.latitude = x; this.longitude = y; this.altitude = z;}

a constructor must always have the same name as the type it belongs to, in the definition of a constructor, you never define a return type, you never use the return statement in the body of a constructor, in the body of a constructor, you can assign to fields of the allocated record using the keyword this to refer to this record.

J.Carette (McMaster) CS 2S03 9 / 19

slide-12
SLIDE 12

Constructors - Cont.

1 Constructors can be overloaded! J.Carette (McMaster) CS 2S03 10 / 19

slide-13
SLIDE 13

Constructors - Cont.

1 Constructors can be overloaded! 2 When no constructor is defined, there is a default constructor with no

arguments. You cannot use the default constructor once one has been defined in your program. (bad style!)

J.Carette (McMaster) CS 2S03 10 / 19

slide-14
SLIDE 14

Constructors - Cont.

1 Constructors can be overloaded! 2 When no constructor is defined, there is a default constructor with no

arguments. You cannot use the default constructor once one has been defined in your program. (bad style!)

3 When declaring a type, it is also possible to define default values for

each field:

class Point { double latitude = 90.0; double longitude = 0.0; double altitude = 0.0;}

J.Carette (McMaster) CS 2S03 10 / 19

slide-15
SLIDE 15

The Semantics of Records

To cover records: we need to add a fifth argument to the functions Σ and Θ: the list of constructed types, each type being associated to an ordered pair composed: T → (list of fields, list of constructors)

J.Carette (McMaster) CS 2S03 11 / 19

slide-16
SLIDE 16

Sharing

In the first case (sharing), all changes of the cell associated with x automatically change the cell associated with y, and vice versa.

y x 90.0 0.0 0.0 48.715 2.208 156.0

48.715! 156.0! 2.208! 90.0! 0.0! 0.0! x! y!

x = y (Sharing!)

y x 90.0 0.0 0.0 48.715 2.208 156.0

48.715! 156.0! 2.208! 90.0! 0.0! 0.0! x! y!

y,lat = x.lat

y x 48.715 2.208 156.0 48.715 2.208 156.0

x! y! 48.715! 156.0! 2.208! 2.208! 48.715! 156.0!

y.long = x.long y.alt = x.alt

J.Carette (McMaster) CS 2S03 12 / 19

slide-17
SLIDE 17

Equality

a = new Point (1, 2, 3); b = new Point (1, 2, 3);

Two types of equality:

  • Physical. Two records of the same type are physically equivalent

(a==b) only when they are identical (share the same cell). Hence, a == b is false.

  • Structural. Two records of the same type are structural equivalent when

their field’s value are equal. Hence, a and b are structural equivalent.

J.Carette (McMaster) CS 2S03 13 / 19

slide-18
SLIDE 18

Wrapper Types (from Base type to Object type)

A wrapper is a type of record with one lone field.

class Integer { int c; Integer (int x) { this.c = x; } }

Why?

1 Uniformity 2 Sharing J.Carette (McMaster) CS 2S03 14 / 19

slide-19
SLIDE 19

Wrapper Types - Cont.

x 5

x! 5!

y 4

y! 4!

Integer x = new Integer(4); int x = 4; Integer y = new Integer(x.c); int y = x; x.c = 5; x = 5; System.out.println(y.c); System.out.println(y); prints 4 prints 4 If we replace the second line with Integer y = x then it prints 5

y x 5

x! 5! y!

J.Carette (McMaster) CS 2S03 15 / 19

slide-20
SLIDE 20

Wrapper Types - Cont.

The following function swaps values of x and y.

static void swap (Integer x, Integer y) { int temp = x.c; x.c = y.c; y.c = temp ;}

Remember, the following functions do nothing on x and y:

static void swp (int x, int y) { int temp = x; x = y; y = temp ;}

J.Carette (McMaster) CS 2S03 16 / 19

slide-21
SLIDE 21

Wrapper Types - Cont.

When we call the function swap(a, b), we create the state:

4 a x

a! 4! x!

7 b y

b! 7! y!

and so swapping the contents of x and y will also swap those of a and b. If a, b are constant (final), and x, y too, then:

4 a x b y 7

a! x! b! y! 4! 7!

and swap works too.

J.Carette (McMaster) CS 2S03 17 / 19

slide-22
SLIDE 22

Records in Caml

1 Record Type:

type point = { latitude : double; longitude : double; altitude : double ;}

2 Creating:

let x = {lat = 90.0; longi = 0.0; altitude = 0.0;} or let y = ref{lat = 90.0; longi = 0.0; altitude = 0.0;}

3 Accessing:

x.latitude (!y). latitude

4 Assigning: all fields are constant by default!

type int -wrap = {mutable c: int} let x = {c = ref 5} x.c <- 4

J.Carette (McMaster) CS 2S03 18 / 19

slide-23
SLIDE 23

Records in C

1 Record Type:

struct Point { double latitude; double longitude; double altitude ;};

2 Creating:

struct Point x; or struct Point x = {5.0 ,7.0 ,100.0};

3 Accessing:

x.latitude

4 Assigning: all fields are constant by default!

x.latitude = 12.0

Call by copy! (the value of x is not a reference associated with a record in memory as it is in Java and Caml!)

J.Carette (McMaster) CS 2S03 19 / 19