Adobe PDF
Adobe PDF
Adobe PDF
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Today’s Agenda<br />
Types<br />
* Polymorphism<br />
- Types and Reuse<br />
- Ad-hoc vs. Universal Polymorphism<br />
* Parametric Types<br />
* Subtyping
Types, Abstraction and Reuse<br />
Use of Types<br />
Recall: Readability, Classification<br />
Classification helps in commonizing representation<br />
and implementation of operation<br />
Types are abstractions<br />
Capture specific features of a set of values for<br />
classification<br />
Types can also improve reuse (of code)<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 2
Types and Reuse<br />
Consider the following examples:<br />
NULL pointer in C (or null in Java)<br />
What is its type?<br />
A function for computing the length of a linked list<br />
in C<br />
What is its type?<br />
What should be its type?<br />
A function for computing the area of (closed<br />
graphic) shapes<br />
What should be its type?<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 3
Polymorphism - Definition<br />
poly ( many ) + morph (forms i.e shapes i.e.<br />
types)<br />
polymorphism = values taking more than one type<br />
(recall: values of a type share representation i.e share<br />
shape)<br />
Values – may be simple data, functions, or objects.<br />
2 major kinds<br />
ad-hoc : not true but apparent polymorphism.<br />
Syntactic Reuse<br />
universal: true polymorphism.<br />
Semantic Reuse<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 4
Polymorphism - Adhoc<br />
Ad-hoc polymorphism is further divided<br />
into two kinds:<br />
Coercion (Implicit/Explicit Type Conversion)<br />
e.g. Integer values in C conditional statement<br />
E.g. Type conversion in C assignment statements<br />
Explicit (programmer specified) type casting<br />
Overloading<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 5
Polymorphism – Ad hoc<br />
Overloading<br />
e.g. In C,<br />
0 : int; 0:float; 0:char.<br />
+ : int int ; + : float float<br />
e.g., In C++/Java:<br />
void print(int x);<br />
void print(String s);<br />
In C++/Java programmers are allowed to define<br />
overloaded functions/methods.<br />
The right function definition (for a given call) is selected<br />
by the compiler based on type information.
Polymorphism – Ad hoc<br />
Idiosyncracies:<br />
C++ allows overloading of operators<br />
But overloaded definitions cannot change associativity,<br />
precedence or arity – Why?<br />
Consider the expression 3+4.5<br />
How is this is evaluated in C?<br />
Is this overloading or coercion?<br />
Exercise: Find out what gcc does.<br />
Referred to as ad-hoc polymorphism<br />
Because it is syntactic – can be rewritten as<br />
separate types<br />
Leads to syntactic reuse<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 7
Polymorphism - Universal<br />
Universal polymorphism is divided into<br />
two kinds:<br />
Parametric Polymorphism<br />
e.g. In Pascal / C NULL : forall(Y) Pointer(Y)<br />
Wait for more on this!<br />
Inclusion Polymorphism<br />
e.g. Subrange types in Pascal<br />
type minutes = 0..60;<br />
Given,<br />
5 : minutes; 5 : integer;<br />
More on this as well!<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 8
Polymorphism - Parametric<br />
Consider the definition of IntList:<br />
IntList = EmptyList ⊕ (Int x IntList)<br />
This can be generalized to:<br />
List = Forall(Y) EmptyList ⊕ (Y x List(Y))<br />
Can Y be any type?<br />
What if Y is List? (map this into Russel's paradox?)<br />
Examples:<br />
Templates in C++<br />
Generics in Java (1.5)<br />
What is the use?<br />
Re-use<br />
Compare this with Overloading!<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 9
Polymorphism - Parametric<br />
Example<br />
length: forall(E) List(E) int<br />
C++ code<br />
template <br />
int length(List head) {<br />
if (head == NULL) return 0;<br />
else return 1+length(head->next);<br />
}<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 10
Polymorphism - Parametric<br />
filter = λ f. λ ls. IF (empty? ls)<br />
emptyList<br />
(IF (f (first ls))<br />
(filter f (rest ls))<br />
(cons (first ls) (filter f (rest ls))))<br />
filter: forall(E) Eboolean, List(E) List(E)<br />
Compare this what we can derive w/o polymorphism!<br />
Anytype -> boolean, List List<br />
Dynamic Typing vs. Static Typing Issues<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 11
Polymorphism - Parametric<br />
Consider the type of a sort function.<br />
sort : IntList IntList<br />
Generalize this to<br />
sort : Forall(Y) List(Y) List(Y)<br />
But what about the comparison operation on<br />
elements of Y?<br />
That would be of the form<br />
compare : Y x Y Bool for any Y.<br />
So, sort will take an additional argument: the<br />
function compare.<br />
sort : Forall(Y) List(Y) x (Y x Y Bool) List(Y)<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 12
Polymorphism – Parametric Existentials<br />
Recall that Existential Quantification captures<br />
encapsulation.<br />
Can we combine Existentials and Universals?<br />
e.g. recall the ADT List<br />
PackList = Exists(Y) Y x (Y Int)<br />
is the type of ADT List where Y is the hidden representation;<br />
and<br />
PackList with Y = ArrayZ<br />
is the type of ADT List with ArrayZ as the representation.<br />
We can abstract out element type of "ArrayZ"<br />
ParamPackList = Forall(Z) Exists(Y) Y(Z) x (Y(Z) Int)<br />
All combinations are feasible:<br />
Z=int, Y=array; Z=int, Y=linkedlist; Z=student,Y=array;
Polymorphism – Parametric Existentials<br />
Template Collection Classes in C++<br />
Standard Template Library<br />
Generic Collections in Java<br />
E.g in Java like syntax<br />
class Hashtable {<br />
private (LinkedList)[] table;<br />
private int hash(K key) { .. }<br />
public void put(K key, V value) { .. }<br />
..<br />
}<br />
Hashtable h;<br />
4/11/2008 Sundar B., BITS, Pilani. CS C362 14
Polymorphism - Subtyping/Inclusion<br />
Denote "A is a subtype of B" by A
Polymorphism - Subtyping<br />
Given function types A1B1 and A2B2<br />
when one of them is the subtype of the other?<br />
Given<br />
type Digit = 0..9;<br />
type Bit = 0..1;<br />
type DigitChar = '0'..'9';<br />
Conisder the function type<br />
type P = Digit Char<br />
and the function types:<br />
type F = Digit DigitChar<br />
type G = Bit Char<br />
type H = Int DigitChar<br />
Is F
Polymorphism - Subtyping<br />
Consider function superPrint defined by<br />
superPrint(p:P, d:Digit) = { Char c := p(d); }<br />
P = Digit Char<br />
F = Digit DigitChar<br />
What happens to<br />
superPrint(f, 5) where f:F ?<br />
Evaulating f(5) is fine since 5:Digit and f accepts<br />
any Digit.<br />
f(5) returns a DigitChar and<br />
assigning c := f(5) is fine since c can be assigned<br />
any Char (including DigitChar).<br />
Empirical Conclusion - F
Polymorphism - Subtyping<br />
P = Digit Char<br />
G = Bit Char<br />
Is G
Subtyping - Rules<br />
Function Subtyping Rule:<br />
A1B1
Subtyping - Rules<br />
Subtyping and Structures<br />
Consider<br />
type Point = struct { x:Int, y:Int };<br />
And<br />
type ColorPoint = struct { x:Int, y:Int, c:Color };<br />
Intuitively, all ColorPoints are Points since they<br />
include x:Int and y:Int fields<br />
So, model Point and ColorPoint as follows:<br />
Point = { (('x',x),('y',y))| x:Int, y:Int }<br />
ColorPoint =<br />
{ (('x',x),('y',y)) | (('x',x),('y',y)) is a member of Point<br />
with additional attribute c:Color }
Subtyping - Rules<br />
Then the general subtyping rule for structures<br />
says, given<br />
T1 = { a1:A1, a2:A2, ..., an:An } where each ai is<br />
unique and<br />
T2 = { b1:B1, b2:B2, ..., bm:Bm } where each bi is<br />
unique, then<br />
T1