Typesystems New

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Type Systems

CNG 242 Programming Language Concepts


Outline
• Inclusion polymorphism
– Concerned with subtypes and subclasses, and their ability to
inherit operations from their parent types and superclasses;
• Parametric polymorphism
– Concerned with procedures that operate uniformly on
arguments of a whole family of types;
• Overloading
– The same identifier can denote several distinct procedures in the
same scope;
• Type conversion-casting and coersion
• Type inference
Type systems
• Design choices for types:
– monomorphic vs polymorphic type system.
– overloading allowed?
– coercion(auto type conversion) applied, how?
– type relations and subtypes exist?
Polymorphism
• Monomorphic types:
– Each value has a single specific type.
– Functions operate on a single type.
– C and most languages are monomorphic.
• Polymorphism: A type system allowing different data types handled in a
uniform interface
Inclusion polymorphism: Polymorphism based on sub-typing relation.
Function applies to a type and all subtypes of the type (class and all
subclasses).

Ad-hoc polymorphism : Also called overloading. Functions that can be


applied to different types and behave differently.

Parametric polymorphism: Functions that are general and can operate


identically on different types
Inclusion polymorphism
• A function applies to a type and all its
subtypes uniformly
• Let Sub be a subtype of T
– i.e.
– therefore may be used in a context where a value
of type T is expected
• Subtype examples:

OOP: HospitalDoctor is a subtype of Doctor


subtypes
Inheritance as subtype
• class Shape{protected: int x,y;}
class Point:public Shape {}
class Circle:public Shape {private: int r;..}
class Rectangle: public Shape{private: int w,h;}
• void move(Point p,int dx, int dy) {…..}
Inclusion polymorphism
• A function applies to a type and all its subtypes
• class Cat:public Animal{…
public: virtual string talk(){return “miyaw”;};
class Dog: public Animal{..
public: virtual string talk(){return “havhav”;};

void write(Animal* a){ printf (“%s”,a->talk());}


int main(){
write (new Cat());
write (new Dog());
}
• “write” applies for any subtype of Animal
• One “talk” method is selected depending on the type of target object
• Ad-hoc polymorphism: This polymorphism let a function to have
different implementations based on its parameters and return
type. Ad-hoc polymorphism is supported through function and
method overloading.
• Parametric polymorphism: lets you write a piece of code that is
not associated with a particular type and therefore can be used
with any type. Object oriented languages like C# achieve it
through generics. C++ uses templates
• Inclusion polymorphism: let a type hold instances of many
different types as long as the types are related by some common
parent class. Object oriented languages like C++ achieve inclusion
polymorphism through inheritance.
Parametric polymorphism
Functions that are general and can operate
identically on different types
– No subtype restriction.
– Functional languages like Haskell
swap (x,y)= (y,x)
--works with any type of pairs
– Generic abstraction
• Template in C++
template <class U> void swap(U &a , U &b) {
U tmp; tmp=a; a=b; b=tmp; }
Polymorphic type
• Polymorphic types: A value can have multiple
types.
• Functions operate on multiple types uniformly
• Haskell: identity x = x function. type: α → α
– identity 4 is 4 : int ->int
– identity “abc” is “abc” : String ->String
– identity (5,”a”) is (5,”a”) : (intxString)-> (intxString)
• Example 2: the function body works the same
independent of the parameter types.

output
output

output
overloading
• Functions that can be applied to different types
and behave differently
• AD-HOC polymorphism
• Overloading: using same identifier for different
procedures/functions/operators in same scope
• Example: Two different functions, two distinct
types, same name.
• Polymorphic function: one function that can
process multiple types. DIFFERENT!!
overloading

• Same identifier is bound to different function abstractions


overloading
• Binding is more complicated. not only
according to name but according to name and
type
Context independent overloading
• Most overloading languages are context independent
– E.g. c++ and java
• Which function body to bind is determined uniquely
by the function identifier and the type of the actual
parameters
• Overloading is not much useful. So languages avoid it.
Context dependent overloading
Context dependent overloading
• Context dependent overloading is confusing,
complex, more expensive
• Type conversion
• Casting: explicit type conversion
• Coercion: implicit type conversion.
– Performed automatically whenever the syntactic
contexts demands it
– E.g. C converts narrow range to wide range
integers
int x=-10;
unsigned int y= x;

int *xp= &x;


double * yp= (double *) xp;

cout<<y<<endl;
cout<<* xp<<endl;
cout<<*yp<<endl;
Type inference
• Type system may force user to declare all types
(C and most compiled imperative languages), or
• Language processor infers types. How?
• Each expression position provide information
(put a constraint) on type inference
1. Equality: y=x,
2. Arithmetic expressions e= a + f x
+::Num->Num ->Num THEN a::Num, f:: alpha ->
Num, e::Num
3. Function application: e=f x
e:: beta, x:: alpha , f:: alpha -> beta
4. Type constructors: f(x:r)=t
x:: a , t:: b, f:: ([a] -> b)

• Inference of all values start from the most general


type (i.e: any type )
• Type inference finds the most general type
satisfying the constraints.
• Exercise: determine the type of .
(.) f g x = (f.g) x = f (g x)
(.) :: (b -> c) -> (a -> b) -> a -> c
• Determine the type of func and x
func x = (x `mod` 2 == 0)

x :: a
func :: a ->Bool

You might also like