Sunteți pe pagina 1din 18

PRINCIPLES OF PROGRAMMING

LANGUAGES

Dr. D. C. Kiran
Associate Professor
Department of
Computer Science & Engineering
PRINCIPLES OF PROGRAMMING LANGUAGES

Type System : Part 1

Dr. D. C. Kiran
Department of Computer Science and Engineering
PRINCIPLES OF PROGRAMMING LANGUAGES
Topics To Be Covered

• Type
• Type Binding
• Type Expressions
• Type System
• Type Checking
PRINCIPLES OF PROGRAMMING LANGUAGES
Type System

• Type – “A Collection of computational entities that share


some common property”
 Set of values
 with a common collection of operations.
 which share a common representation.

int, float, Decimal, string, complex, Boolean…etc

4
PRINCIPLES OF PROGRAMMING LANGUAGES
Why Typing?

 Determines the range of values of variables.


 Defines the set of operations for the values.
 in the case of floating point, type also determines the
precision
 error detection: early detection of common programming
errors
 Offer Readability
 e.g. Celsius instead of integer
 Offer Protection.
 E.g. Float value stored in integer location can lead to loss of
data –
 can be prevented by compiler / run-time system that checks
for type matching.
PRINCIPLES OF PROGRAMMING LANGUAGES
Static Type Binding
PRINCIPLES OF PROGRAMMING LANGUAGES
Static Type Binding

E1  E + T E
ET
TT*F E + T
TF
F  id
T T * F
F(E)

F F id

id id
PRINCIPLES OF PROGRAMMING LANGUAGES
Static Type Binding

E1  E + T
ET
T1  T * F
E {24.2, real}

TF
F  id {2,int} E + T {12.2, real}
F(E)

{2,int} T {3,int} T * F {4.2, real}

{2,int} F {3,int} F id {place}

{place} id id {place}
PRINCIPLES OF PROGRAMMING LANGUAGES
How? :- Type Expression

• The type of a language construct is denoted by a type


expression.
• A type expression can be:
– A basic type
• a primitive data type such as integer, real, char,
boolean, …
• type-error to signal a type error
• void : no type
– A type constructor applies to other type expressions.
• Arrays:
• Records
• Functions:
• Pointers:
PRINCIPLES OF PROGRAMMING LANGUAGES
Type Constructor

Arrays: If T is a type expression, then array(I,T) is a type


expression where I denotes index range. Ex: array(0..99,int)

Functions:
•Treat functions in a programming language as mapping
from a domain type D to a range type R.
•The type of a function can be denoted by the type
expression D→R where D are R type expressions.
•Ex: int→int represents the type of a function which takes
an int value as parameter, and its return type is also int.
PRINCIPLES OF PROGRAMMING LANGUAGES
Type Constructor

if (s and t are same basic types) then return true


else if (s=array(s1,s2) and t=array(t1,t2)) then return (equiv(s1,t1)
and equiv(s2,t2))
else if (s = s1  s2 and t = t1  t2) then return (equiv(s1,t1) and
equiv(s2,t2))
else return false
PRINCIPLES OF PROGRAMMING LANGUAGES
Polymorphic Typing

• A language is polymorphic is language constructs can have more


than one type
procedure swap(anytype x, y)
where anytype is considered to be a type variable
• Polymorphic functions have type patterns instead of actual type
expressions
• The type checker must check that the types of the actual
parameters fit the pattern
– Technically, the type checker must find a substitution of actual
types for type variables that satisfies the type equivalence
between the formal type pattern and the actual type
expression
• Most notably in the language ML
• Java and C# allow polymorphic type constructors, called generics
PRINCIPLES OF PROGRAMMING LANGUAGES
Polymorphism

A polymorphic function accepts arguments of different types:

Example parametric polymorphism in ML Example function polymorphism in ML


fun I(x) =x; fun Length(nil) =0
val I=fn :α->α | Length(a::y)=1+length(y);
val Length=fn :α list->int

Fired ( Employee) //Polymorphism due to sub-typing or Inclusive typing


Person Employee / Manager
If Employee Is of Type T
Manager is Sub-type of Type T
PRINCIPLES OF PROGRAMMING LANGUAGES
Where:- Type System
A collection of rules for assigning type expressions to the
various parts of a program.
 Type Checker: An implementation of a type system.
 Syntax Directed.

 Sound Type System: eliminates the need for checking


type errors during run time.
• A programming language is strongly-typed, if every
program its compiler accepts will execute without type
errors.
– In practice, some of type checking operations are
done at run-time (so, most of the programming
languages are not strongly-typed).
– Ex: int x[100]; … x[i] most of the compilers cannot
guarantee that i will be between 0 and 99
PRINCIPLES OF PROGRAMMING LANGUAGES
Strong vs Weak Typing

• In Strong typing any operation on wrong type will raise error.


This make language Type safe.
Example: int i=4; char c=“5”
operation i+5 will be an error.
• In Weak typing above operation can be legal.
Example: Visual basic will convert “5” to number 5 and add with
4. Java script will convert 4 to string and add the string.
PRINCIPLES OF PROGRAMMING LANGUAGES
Type Checking

• Type equivalence
– When are the types of two values the same?
• Type compatibility
– When can a value of type A be used in a context that
expects type B?
• Type inference
– What is the type of an expression, given the types of the
operands?
PRINCIPLES OF PROGRAMMING LANGUAGES
Summary

• Type
• Type Binding
 Static and Dynamic Type Binding
 Polymorphic Type System
• Type Expression
 Type Constructors: Array, Records, Pointers
• Why and Where Type System is used
• Type Checking
 Equivalence
 Compatibility
 Inference
THANK YOU

Dr. D. C. Kiran
Department of Computer Science and Engineering
dckiran@pes.edu
9829935135

S-ar putea să vă placă și