Docsity

Types and Type Systems in Modern Programming Languages, Slides of Advanced Computer Programming

Partial preview of the text

Download Types and Type Systems in Modern Programming Languages and more Slides Advanced Computer Programming in PDF only on Docsity!

Types

Chapter Six Modern Programming Languages, 2nd ed. (^) Docsity.com 1

A Type Is A Set

 When you declare that a variable has a

certain type, you are saying that the values

the variable can have are elements of a

certain set

 A type is a set of values

  • plus a low-level representation
  • plus a collection of operations that can be

applied to those values

Chapter Six Modern Programming Languages, 2nd ed. 2

int n;

Docsity.com

Outline

 Type Menagerie

  • Primitive types
  • Constructed types

 Uses For Types

  • Type annotations and type inference
  • Type checking
  • Type equivalence issues

Chapter Six Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Primitive vs. Constructed Types

 Any type that a program can use but cannot define

for itself is a primitive type in the language

 Any type that a program can define for itself (using

the primitive types) is a constructed type

 Some primitive types in ML: int , real , char

  • An ML program cannot define a type named int that works like the predefined int

 A constructed type: int list

  • Defined using the primitive type int and the list type constructor

Chapter Six Modern Programming Languages, 2nd ed. (^) Docsity.com 5

Comparing Integral Types

Chapter Six Modern Programming Languages, 2nd ed. 7

C:

char unsigned char short int unsigned short int int unsigned int long int unsigned long int No standard implementation, but longer sizes must provide at least as much range as shorter sizes.

Java: byte (1-byte signed) char (2-byte unsigned) short (2-byte signed) int (4-byte signed) long (8-byte signed)

Scheme: integer Integers of unbounded range

Docsity.com

Issues

 What sets do the primitive types signify?

  • How much is part of the language specification, how much left up to the implementation?
  • If necessary, how can a program find out? ( INT_MAX in C, Int.maxInt in ML, etc.)

 What operations are supported?

  • Detailed definitions: rounding, exceptions, etc.

 The choice of representation is a critical part of

these decisions

Chapter Six Modern Programming Languages, 2nd ed. (^) Docsity.com 8

Constructed Types

 Additional types defined using the language

 Today: enumerations, tuples, arrays, strings,

lists, unions, subtypes, and function types

 For each one, there is connection between

how sets are defined mathematically, and

how types are defined in programming

languages

Chapter Six Modern Programming Languages, 2nd ed. (^) Docsity.com 10

Making Sets by Enumeration

 Mathematically, we can construct sets by

just listing all the elements:

Chapter Six Modern Programming Languages, 2nd ed. 11

S = { a , b , c }

Docsity.com

Representing Enumeration

Values

 A common representation is to treat the

values of an enumeration as small integers

 This may even be exposed to the

programmer, as it is in C:

Chapter Six Modern Programming Languages, 2nd ed. 13

enum coin { penny = 1, nickel = 5, dime = 10, quarter = 25 };

enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r' };

Docsity.com

Operations on Enumeration

Values

 Equality test:

 If the integer nature of the representation is

exposed, a language will allow some or all

integer operations:

Chapter Six Modern Programming Languages, 2nd ed. 14

fun isWeekend x = (x = Sa orelse x = Su);

Pascal: for C := red to blue do P(C) C: int x = penny + nickel + dime;

Docsity.com

Making Types by Tupling

 Some languages support pure tuples:

 Many others support record types, which

are just tuples with named fields:

Chapter Six Modern Programming Languages, 2nd ed. 16

fun get1 (x : real * real) = #1 x;

C:

struct complex { double rp; double ip; };

ML:

type complex = { rp:real, ip:real }; fun getip (x : complex) = #ip x;

Docsity.com

Representing Tuple Values

 A common representation is to just place

the elements side-by-side in memory

 But there are lots of details:

  • in what order?
  • with “holes” to align elements (e.g. on word

boundaries) in memory?

  • is any or all of this visible to the programmer?

Chapter Six Modern Programming Languages, 2nd ed. (^) Docsity.com 17

Operations on Tuple Values

 Selection, of course:

 Other operations depending on how much

of the representation is exposed:

Chapter Six Modern Programming Languages, 2nd ed. 19

C: x.ip ML: #ip x

C: **double y = *((double *) &x); struct person { char firstname; char lastname; } p1 = {"marcia","brady"};

Docsity.com

Sets Of Vectors

 Fixed-size vectors:

 Arbitrary-size vectors:

Chapter Six Modern Programming Languages, 2nd ed. 20

{ ( (^) x x ) i x X }

S X

n i

n

= ∀ ∈

=

1 ,, |^.

i

i X

S X

=

=

∗

Docsity.com