Type System and Polymorphism in Programming Languages, Study notes of Programming Languages

The concept of types in programming languages, including data types, type checking, and polymorphism. It explains how types provide context for operations and limit the set of allowed operations. The document also covers type equivalence and compatibility, and the importance of type checking in ensuring a program obeys the language's rules.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-3uq
koofers-user-3uq 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
Programming Languages
Tevfik Koşar
Lecture - XV
March 14th, 2006
2
Roadmap
Data Types
Type Checking
Polymorphism
Type Equivalence
Polymorphism
pf3
pf4
pf5

Partial preview of the text

Download Type System and Polymorphism in Programming Languages and more Study notes Programming Languages in PDF only on Docsity!

1

Programming Languages

Tevfik Koşar

Lecture - XV March 14th, 2006

2

Roadmap

  • Data Types
  • Type Checking
  • Polymorphism
  • Type Equivalence
  • Polymorphism

3

Data Types

Types provide implicit context for many operations

  • a+b
    • Types of a and b determine which addition function to use
    • eg. integer addition, or floating point addition
  • new p
    • will allocate storage from the heap right size to hold the object pointed by p

4

Data Types

Types limit set of operations

  • prevents illegal or meaningless operations:
    • adding a record and a char
    • passing a file as parameter to a functions which expects an int
    • taking sinus of a pointer

7

Type Checking

  • Every definition of an object must specify the object’s type (statically types lang.)
  • In type checking, three important notions:
    • type equivalence
    • type compatibility
    • type inference

8

Type Equivalence

Two principal ways of defining type equivalence:

  • Structural equivalence: Two types are equal if they consist of the same components
  1. type foo1 = record a, b: integer end;
  2. type foo2 = record a , b: integer end;
  3. type foo3 = record a: integer; b: integer; end;
  4. type foo4 = record b: integer; a: integer; end;
  • The last one is equal to the rest in most languages, but not in ML!

9

Type Equivalence

  • Structural equivalence: Example 2:
  1. type str1 = array [1..10] of char;

  2. type str2 = array [1..2*5] of char;

  3. type str3 = array [0..9] of char;

  • The second one is equal to the first one, but not the third one! (length of the array is same, but index values are different)

10

Type Equivalence

  • Name equivalence: Each definition introduces a new type.
  1. type foo1 = record a, b: integer end;
  2. type foo2 = record a, b: integer end;

foo1 and foo2 are considered as different types!