homogeneous tuples what are they? –s 2 = s x s –s n = s x s x … x s cardinalities –#(s 2 )=...

25
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S • Cardinalities – #(S 2 )= (#S) 2 – #(S n )= (#S) n – #(S 0 )= (#S) 0 =1 What is S 0 ? It is the type Unit, whose value is (). Unit corresponds to void in C, C++ and Java.

Upload: cameron-wells

Post on 18-Jan-2016

227 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Homogeneous tuples

• What are they?– S2 = S x S– Sn = S x S x … x S

• Cardinalities– #(S2)= (#S)2

– #(Sn)= (#S)n

– #(S0)= (#S)0=1

• What is S0? – It is the type Unit, whose value is ().– Unit corresponds to void in C, C++ and Java.

Page 2: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Mappings

• Types of mappings:– Arrays– Functions

• Arrays map integers to the declared type of the variables in the array.

• Functions map an arbitrary domain type to an arbitrary range type.

Page 3: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Mappings in general

• S = {0,1}

• T = {a,b,c}

• How many mappings ST are there?

• #(ST) = #(T)#(S)

• The possible ST mappings are:

{0a,1a}, {0a,1b}, {0a,1c}, {0b,1a}, {0b,1b}, {0b,1c}, {0c,1a}, {0c,1b}, {0c,1c}

Page 4: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Implementing a finite mapping as an array

T[] map = {a,c};

defines map to be the third mapping given on previous slide:

map[0] = a

map[1] = c

Page 5: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Implementing a finite mapping as a function

We can also define the mapping as a function (ML example):fun map 0 = a | map 1 = c;

or (in Pascal): function map(s:S):T;

begin case (s) of

0: map:=a;1: map:=c;

end;

Page 6: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

More discussionWe can also define the mapping as a function (ML example):

fun map x = case x of

0 => a1 => c

(Notice function syntax in case expression!)

In C:T map(int x) {

switch (x) { case 0: return a; case 1: return c; }

}

Page 7: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

More general finite mappings

Hash tables let us define more general finite mappings (domain type does not need to be integral).

Page 8: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Memoization

• Combination of computation and storage!

Page 9: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Array-based vs. function-based mappings

• An array-based mapping is explicit:– it gives domain-range pairs explicitly

• A function-based mapping is implicit:– for each domain element it gives a method for

computing the corresponding range element.

Page 10: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Disjoint Unions

• Pascal: variant record

• C/C++: union

• ML: union type

• Ada: discriminated record

Page 11: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Disjoint Unions(quick review)

• Pascal: variant record

• C/C++: union

• ML: union type

• Ada: discriminated record

Page 12: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

What is a disjoint union?

• “Another kind of composite value is the disjoint union, whereby a value is chosen from one of several (usually different) sets.”

David Watt

Programming Language Design Concepts page 27

Page 13: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

S+T

Values are tagged to indicate which set they are from:

S+T = { left(x) | x in S } { right(y) | y in T }

Cardinality? #(S+T) = #S + #T

Page 14: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

MLdatatype number = Exact of int | Inexact of real;

datatype person = King

| Peer of string*string*int

| Knight of string

| Peasant of string;

Values: King, Peer(“Earl”, “Carlisle”, 7),

Knight(“Gawain”), Peasant(“Jack Cade”)

Paulson, ML for the Working Programmer

pages 124-125

Page 15: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

OO disjoint unions(new material)

• The main purpose of a disjoint union is to bring together values from different sets in a way that you can determine from which set a given value is drawn.

• This type of structure is used very commonly in OO programming.

• Polymorphism feeds off of this idea: implicit selection based on type.

Page 16: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

OO disjoint union example

Disjoint union

Page 17: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Recursive types

• A type defined in terms of itself• Examples: lists, strings, trees, …• If R is a recursive type, #(R) is infinite.• Members of a recursive type are often

finite, tough infinite members are possible (the list of all primes).

• How? They are implicitly represented (think of function vs. array representation of mappings): e.g. lazy lists.

Page 18: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Recursive type membership• Consider the list type:datatype ’a List = Nil

| Cons of ’a * ’a List;

• What are the members of this type?List(0) = NilList(1) = List(0) U {Cons(x,y)| y in List(0)}…List(k) = List(k-1) U {Cons(x,y)| y in List(k-1)}

List = Un=0…∞ List(n)

Page 19: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

C++ & pointer types

• Languages like C++ do not directly support recursive types, but rather require the use of an indirection mechanism, the pointer.

• This has to do with the semantics of assignment. If v is a recursive structure, what are reasonable semantics for w=v?– copy semantics – expensive – reference semantics – sharing

• If language does not permit selective updating of a structure (as in a pure functional language) the semantics are indistinguishable).

Page 20: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Type equivalence

Type equivalence addresses the question of when two types are considered equivalent.

– Structural equivalence• two types are equivalent if and only if they have

the same set of values

– Name equivalence• two types are equivalent if and only if they are

defined in the same place

Page 21: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Structural equivalence

• Types S and T are equivalent iff– S and T are both primitive and S and T are identical– S=AxB and T=CxD are both cartesian products and A

and C are equivalent and B and D are equivalent– S=AB and T=CD are both mappings and A and C

are equivalent and B and D are equivalent– S=A+B and T=C+D are both disjoint unions and A

and C are equivalent and B and D are equivalent

• Otherwise, S and T are not equivalent.

Page 22: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Name equivalence

• Strictly, types are unique – each new type definition occurs in a different place, and so results in a new distinct type.

• Pascal technically uses name equivalence, but in practice the rules are relaxed:– For example, suppose we wanted to define a file type

to permit two programs to communicate via a file. Since the file type is defined in two different places (in two different programs) their files types are considered different (and hence incompatible).

Page 23: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Name vs. Structural equivalence

• Name equivalence forces each distinct type to be defined in one and only one place. This is sometimes inconvenient, but it helps to make the program more maintainable. (If the same type is defined in several places, and subsequently it has to be changed, the change must be made consistently in several places.)

• Structural equivalence allows confusion between types that are only coincidentally similar.

Watt, page 42

Page 24: Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It

Type Completeness Principle

“No operation should be arbitrarily restricted in the types of its operands.”

Watt, page 43Types whose values are unrestricted in their usage are termed “first-class”, whereas those that are restricted are termed “second-class”.In Pascal procedures can be passed as arguments to other procedures, but cannot, for example, be part of a composite value. Pascal procedures are therefore second class.A language like ML imposes no such arbitrary type distinctions. All types are first-class.