39
Type Systems Daniel Quirino Oliveira [email protected] Tuesday, January 18, 2011

Type systems

Embed Size (px)

Citation preview

Page 1: Type systems

Type SystemsDaniel Quirino Oliveira

[email protected]

Tuesday, January 18, 2011

Page 3: Type systems

What?

Tuesday, January 18, 2011

Page 4: Type systems

A set of syntatic constraints and rules that classifies programming language statements according to that values they deal with by associating types to values.

Tuesday, January 18, 2011

Page 5: Type systems

Purpose?

Tuesday, January 18, 2011

Page 6: Type systems

Check and prove a program correctness

Tuesday, January 18, 2011

Page 7: Type systems

Wait, wait... What’s type?

Tuesday, January 18, 2011

Page 8: Type systems

Intuitively...A set of values something may assumeA set of operations something may do

Or both

Tuesday, January 18, 2011

Page 9: Type systems

Label and a set of properties used by a type system to prove

program's correctness

Tuesday, January 18, 2011

Page 10: Type systems

Types of Type Systems

Tuesday, January 18, 2011

Page 11: Type systems

Monomorphiceach constant/variable/function may assume an unique type according to its value

Tuesday, January 18, 2011

Page 12: Type systems

e.g.: Pascal, C

procedure foo() begin foo := 10;end

i : integer;c : character;

i := 42;c := ‘a’;

int foo() { return 10;}

int i = 42;char c = ‘a’;

Tuesday, January 18, 2011

Page 13: Type systems

Polymorphiceach constant/variable/function may assume more than one type, given its value, according to context

Tuesday, January 18, 2011

Page 14: Type systems

e.g.: Java, C# ...

Number toNumber(int a) { return new Integer(a);}

Number toNumber(double b) { return new Double(b);}

Number a = toNumber(42);Number b = toNumber(4.2);Double c = (Double) b;Integer d = (Integer) a;

Tuesday, January 18, 2011

Page 15: Type systems

So, what static/dynamic... typing is about?

Tuesday, January 18, 2011

Page 16: Type systems

Type CHECKING!

Tuesday, January 18, 2011

Page 17: Type systems

Static Type Checking SystemA given variable is allowed assume only one TYPE in a given context

All program types are checked at least once by a “compiler” during the semantic analysis (parsing)

Tuesday, January 18, 2011

Page 18: Type systems

Remember that...

Tuesday, January 18, 2011

Page 19: Type systems

e.g.: Java, Scala

void foo() { Integer a = 42; String a = “hello”;}

def foo()= { var a = 0; a = “hello”;}

compile-time errorSymbol “a” already defined as being of another type.

run/compile-time errorSymbol “a” already defined as being of another type.

Tuesday, January 18, 2011

Page 20: Type systems

Dynamic Type Checking SystemA given variable may assume values of different TYPES in a given context during the program flow

I.e., values do have TYPES whereas variables have VALUES

Tuesday, January 18, 2011

Page 21: Type systems

e.g.: Groovy, Ruby

def foo() { def a = 42; a = “hello”;}

def foo a = 0 a = “hello”end

Tuesday, January 18, 2011

Page 22: Type systems

Misconceptions

Tuesday, January 18, 2011

Page 23: Type systems

Statically type checked languages require type

declarations

Tuesday, January 18, 2011

Page 24: Type systems

So, what about C#, Scala and ML?

Tuesday, January 18, 2011

Page 25: Type systems

All “scripting” languages are dynamically type checked

Tuesday, January 18, 2011

Page 26: Type systems

Well, Scala is statically type checked

Tuesday, January 18, 2011

Page 27: Type systems

All dynamically type checked languages are not compiled

Tuesday, January 18, 2011

Page 28: Type systems

Really? So, what is Groovy?

Tuesday, January 18, 2011

Page 29: Type systems

Dynamically type checked languages are weakly typed

Tuesday, January 18, 2011

Page 30: Type systems

Wait! Weak?!

Tuesday, January 18, 2011

Page 31: Type systems

Strong vs. Weak dilemma

Tuesday, January 18, 2011

Page 32: Type systems

Strongly typed languaguesA language that defines a set of rules that restrict values of different types of interacting

Tuesday, January 18, 2011

Page 33: Type systems

a = 1b = “1”c = a + bTypeError: String can't be coerced into Fixnum

e.g.: Ruby

Tuesday, January 18, 2011

Page 34: Type systems

Weakly typed languaguesA language that does not define a set of rules that restrict values of different types of interacting but

automatically tries to convert somehow one value’s type into the other’s

Tuesday, January 18, 2011

Page 35: Type systems

Dynamically type checked languages are weakly typed not

manifest typed

Tuesday, January 18, 2011

Page 36: Type systems

a = 1b = “1”c = a + b // 11

e.g.: Javascript

Tuesday, January 18, 2011

Page 37: Type systems

Q&A

Tuesday, January 18, 2011

Page 38: Type systems

ReferencesBooks

Piece, Benjamin (2002). Types and Programming Languages.Scott, Michael (2009). Programming Language Pragmatics.

Wirth, Niklaus (1996). Compiler Construction.

Web

http://lucacardelli.name/Papers/TypeSystems.pdfhttp://web.archive.org/web/20080822101209/http://www.pphsg.org/cdsmith/types.html

http://web.archive.org/web/20080331215906/perl.plover.com/yak/typing/notes.html http://plato.stanford.edu/entries/type-theory/

http://www.jstor.org/stable/2266170 http://www.cs.cornell.edu/info/projects/nuprl/book/node31.html

http://research.microsoft.com/en-us/um/people/emeijer/Papers/RDL04Meijer.pdfhttp://docs.google.com/View?id=dcsvntt2_25wpjvbbhk

Tuesday, January 18, 2011

Page 39: Type systems

Danke :)

Tuesday, January 18, 2011