Docsity

Notes on Example - Programming Languages | CS 6390, Study notes of Programming Languages

Partial preview of the text

Download Notes on Example - Programming Languages | CS 6390 and more Study notes Programming Languages in PDF only on Docsity!

EXAMPLE

-^

Consider the following expression:^ if (expression) then 0 else "foo"

-^

Making no assumptions about the underlyingtype system, what should be the type of thisconditional expression?

•^

Possibilities^ –

Int

if you can prove that

expression

always evaluates to

true

-^

0

as a very small type; likewise for

"foo"

-^

String

if you can prove that

expression

always evaluates

to

false

; or if you treat

0

as the null string pointer

-^

Int

∪

String

-^

Error

because the two arms have different types

-^

Object

as the least common ancestor

-^

Undefined

(⊥

) if

expression

is undefined

•^

The type system for a programming language givesanswers to questions like this

EXAMPLE - 2

DEFINITION

•^

A type system is a tractable syntactic method forproving the absence of certain program behaviors byclassifying phrases according to the kinds of valuesthey compute.

-- Pierce

Syntactic

implies statically analyzable

Proving

implies that a type system is a formal

semantic system

Tractable

implies efficient algorithms

Absence

implies conservative estimation

BASICS

-^

Type

is a set of values

-^

Type system: s

et of rules for defining and

using types in a programming language^ – Type systems enable interesting subsets to be

given names and properties

-^

In

monomorphic

type systems, each value is

given exactly one type

-^

In

polymorphic

type systems, a value may

have more than one type

TYPE CHECKING

-^

Strong typing:

every identifier and every

expression has a well-defined type;– Prevention of run-time type errors

-^

Static typing:

types determinable at compile

time

-^

Dynamic typing:

types determined at run-time

-^

Type inference:

compiler/interpreter

determination of types in the absence ofdeclarations

c 2007 by Spencer Rugaber

TYPING INSECURITIES

  • Untyped pointers• Subrange narrowing (similar to

downcasting

), e.g. int to char

  • Untagged variant records (

type punning

)

  • Procedure parameters

1. TYPE EQUIVALENCE

-^

Name equivalence:

types declared using the

same type name– Problem: anonymous types– Problem: name shadowing in different

scopes

-^

Structure equivalence:

types have the same

structure– Problem: recursive declarations– Problem: constituent ordering– Problem: field names part of type definition

-^

Types for literals: automatic overload

ANONYMOUS TYPES EXAMPLE

type

T = record a: integer; b: char end; var

x,y: array [1..2] of record a: integer; b: char end;z:

array [1..2] of T;

u,v: array [1..2] of record a: integer; b: char end; type

T = record a: integer; b: char end;Anon1 = array [1..2] record a: integer; b: char end;Anon2 = array [1..2] of T; var

x,y: Anon1;z:

Anon2;

u,v: Anon1;

RECURSIVE DECLARATION EXAMPLES

type T1 is record

X: INTEGER;N: access T1; end record;type T2 is record

X: INTEGER;N: access T2; end record;

type T3 is record

X: INTEGER;N: access record

X: INTEGER;N: access T3; end record; end record;

c 2007 by Spencer Rugaber

CONSTITUENT ORDERING EXAMPLE

type T1 is record

X: INTEGER;N: access T1; end record;type T2 is record

N: access T2;X: INTEGER; end record;

Note that fieldordering is significantfor C

structs

in

terms of memoryalignment andportability

c 2007 by Spencer Rugaber

2. TYPE CONVERSIONS

•^

Conversion:

treatment of a value of a given type as if

it had a different type– May actually change the underlying representation

(e.g.

int

real

  • May change storage requirements (e.g.

int

long

  • May be limited to appeasing the type checker

(void *

char *)

•^

Casting:

explicit type conversion

•^

Coercion:

implicit type conversion

c 2007 by Spencer Rugaber

3. SUBTYPING

  • Subtyping is the ability to define one

type (T

) as a refinement of another (T 2

) 1

  • Mathematically, subtyping is an ordering

relation on types such that if T

2

< T

1

then all values of type T

2

are also legal

values of type T

1

  • T

1

is said to

subsume

T

2

c 2007 by Spencer Rugaber

CLASSES AND TYPES

-^

Consider the following additional property– For objects

o

1

and

o

, if the class of 2

o

1

is a

subclass of the class of

o

2

then the type of

o

1

type of

o

2

  • This property is true of class-based

languages only if they obey subtyping

  • Languages that do not support this

property can still provide inheritance

LIMITATIONS OF TYPES

•^

If types are sets of values, consider the set of legaltelephone area codes represented as integers

-^

This is also a set of values, but we would notnormally consider it a type

-^

We might still want to check whether a value was inthis set or not. In principle, we could do this atcompile time, but we are more likely to add a run-timecheck

-^

More generally, there exist subsets of values that canonly be checked at run-time and that we wouldnormally refrain from calling types