<<

,

s

s e p y TpsT

anguage CPS 506 L

Type Systems

Semantics and Data

Comparative Programming 2

anguage: d l ne Systems fi e d y l e Z

Type t e l erations={AND, OR, NOT, XOR} p Values= Operations={+, -, *, /, mod} Values={true, false} O • • • • comp Boolean int – – A l t l dfi d ldfiA l t d Defined syntax, semantics and type l system Type: A of values and operations • • 3 a part of :

Systems

Type em st y S Type checked in compile time Type checked in of part a syntax analysis Type checked in run time: a part of semantics • • A system of types and their associated A bridge between syntax and semantics variables and objects in a program formalize the definition of data types To and their usage in a ype – – – T Sst • 4

s i e ) t bl ’ e a i yp

con t var le t h g gyp eac

: d ype Systems ( T y ll ca i Type Could be explicit or implicit declaration Example: C and Java, Perl Type rules are defined on abstract syntax (Static Semantics) tat – – – Sill Td h ibl i i Td h ibl Sill S associated with a sin during its life in run time. • 5

ype ) t t ’ e bl a i con t var

a

: d 5] . 3 ype T

Systems ( , y 2 . ll ca [10 47 i

=

Type = t s Example: LISP, JavaScript, PHP More flexible Less reliable, difficult to debug Fast compilation Slow execution (Type checking in run-time) ynam Java Script example: – Li t… [10List = 2 47 – 3– 5] – – Dill Td ibl t t ibl Td Dill D can be changed in run time • 6

) t ’

con t defined -

Systems ( ; f ; f x; Type . oat t uf u

Another example in C ? Another example in Example: union in C fl f int i; oa =

… u.I = 10; }; union flexType u; – union flexType { fl t x= x … – Type Error: a non well operation on a variable in run time • 7

) t t no ’ s t C i u

con t b , d ype t y l x rong t s

y l near

Systems ( le s p i ava ng i J yp e exam e: l t y yp p Type the type regardless of rong xamp t ti Ex+1 l J i s l t i l t C bt t d More reliable on (implicit ) Coercion rules have an effect rint x+ – – – Weak t Strongly Typed: All type errors compile or are detected in run time before execution x = 2; y = “5”; p Visual Basic: 7 JavaScript: “25” • • 8

) e

t f ’

con t e Sa yp f ed -> T yp Systems ( T y l g gy yp

Type Example: Java, Haskell, and ML Stron – – type error Type Safe: A language without • 9 10

an

ng ti a i is bound to the is bound to value t i i assoc ec f o bj

o is bound to a location

Type Binding 10 an

Identifier Identifier o t ; process

value 10 e = integer type and to a location specified by the underlying or ype, int i; i Th f iti itif Th attribute, name, location, value, or t bjt Example • • 10

) t ’ block y con t and real numbers are and , int ned to a memor ned to g gy me ers are bound to d ti g float oa /L e il Type Binding ( time g Java: Inte Declaration of variables memory or fixed objects to stack Bounding static Execution code is assi Value are bound to variables bound to Bounding real values to IEEE 754 standard omp • • • • • • Language definition time Language implementation time Program writing time Run time Cil/Ld ti Cil/Ld C – – – – – Bindin time • 11

) t ’

con t ossible time p until the last g g bindin

Type Binding ( y yg p ossible An element is bound to a property as early as as early An element is bound to a property The earlier the binding more efficient The earlier the language the binding more flexible language The later Supports overloading and overriding in Object C++ example ? p Dela Oriented languages – – – – – – Late Binding Early bindin • • 12 e t a g er- ua l th g g in ur compi

y ns e b , f nearly all type nearly all d , o type checking

, y it v ti ac converte

y e l s are static g icit l is th g imp

Type Checking erator, or is allowed under lan kin e p pgg ec b h to c

e es p l b b l l l b l d y T pthe operands of an operator are of hcompatible types kin is th tiru it f nstype bindinall If all type bindin s are static intype nearly all checking can be static th t type bindings are dynamicIf type bindings are dynamic type checking must be dynamic generated code, to a legal type A compatible type is one that is either legal that is one compatible type is A for the o • • • • 13 t f a o

float th int to ues l to one

va s

int i e float on i . f th g o ll ect to a type that e e. a j

yp e convers

d u l ng i inal t nc

Type Conversion g gypg t i l d ll f th l f narrow A widening conversion is one in A i i i tht i A iconverts an ob icanno the ori which an object is converted to a type that can include at least approximations to all of the values of the original type e.g. • • 14

) , t ’

con t

on i e error detection yp t onvers it C c li

Type Conversion ( mp decreases Cability. In most languages, all numeric i types are coerced in expressions using widening conversions. Ada has no iliit i – (Coercion) Implicit type conversion Implicit type • 15

) t ’ ; 2 con t = x + Java int x; d2 d double d; x = 5; – ; i

=

Type Conversion ( C double d; long l; int… d = i; i; l= l if (d == l) d = 2 * l; – 16

) t ’

con t expression - 3.14; = t(2)

d oa

Fl name ) cast name ) - + 0 . double d int i = (int) d; boolean t = true; b = (byte) (t ? 1 : 0); 3 * Integer(2.0) 2 0 Fl t(2) C Java Ada to function (similar call) • • •

Type Conversion ( ( type – Explicit type conversion (Casting) • 17 >} n , Val n >,…, value> , , , Val 2 n i >,

Semantic Domains ti A set of pairs A set of pairs • • A set with well-defined properties and Environment Memory operations Product of environment and its memory = {

) s t p ’

con t e h (x+y) => 6 (x+y) => t σ

ne utational ste p fi e (y) => 2 -> (y) => d σ to

> Conclusion - ways

uences of com q qpp (x) => 4 and Premise σ Program is interpreted as a set of is interpreted Program se A set of execution rules ree • •

Semantic Domains ( Operational Semantics – Th dfi hdfi Th meaning of a program • 19 f o

) t ’ ng i s )

con t k t ’ mean oo

b e con ( text th cs d i an ne

s l fi e d Semant manua o

l t define program behavior in terms define : ona i anguage ways L l d bk d L l Teaching programming languages

– – Usage Structural: define program behavior in Structural: define terms of the behavior its parts Natural program behavior in terms define of its overall effects, and not from single steps perat • • • ree Oil i ( Semantic Domains ( i Oil O – Tha program t d fi th i f • 20

) ic g t

’ lo g con t ram usin g condition} - ro p pg gg {Post

s s c

nti a assertions

m e S c condition} s {Post - ti a m {Pre {a = 2} b a; {b {?} a = b+1; {a > 1} o Agreement of the program result and Agreement of the program specification Formal verification of a expressions, Hoare triple The program does what it is supposed to do Example Weakest Pre-condition • • • • • •

Semantic Domains ( Axi m ti S m nti s – 21

) ′ }

t } Q ’ Q Q { { b s ⇒ } c ]} Q b ¬ , } \ true ′ } } P ∧ R con t a Q Q } [ { } P { { a ⇒ { a s R Q Q ′ } } } { { ′ }, 2 ∧ P P } P s P ) { Q ) { } Q { Q }, a { { t }, { R a ’ 2 Q a { = } Q s { } n nt c 1 { a }, s P a b } ∧ } R { } : P P co { P P 1 { { s { { s } s ( P { c b on nti a else diti

m a on e S f C then o c

c e ti l a u Rule of Conjunction Rule of Assignment ( Rule of sequence Rule of Consequence Cditi RlR f : if m

– – – – – s o Axioms •

Semantic Domains ( Axi m ti S m nti ( s – 22 at

) p, t ’ } } c I ¬ ) {

con t b ∧ ’t } c I ore the loo { f ∧ s } I I con { { ( cs ti eman S c Invariant is true be ti p pfp, while c do b end : Loo in each iteration, of the loop the bottom and when the loop is terminated. Find the loop invariant to prove the correctness of the loop Rule of Loop invariant I is loop oma – s – – – i Axioms x •

Semantic Domains ( AiA S ti ( ti – 23

) t ’

con t on ti E2 | E1 * E2 | E1 / E1 * E2 | E2 | unc – l f ca ti E2 | E1 E2 | + ema cs i E2| (E) | N E1 = th semantics of Integer arithmetic semantics n :: ma o

ssi ng Semant i l re Production rules: orm Number ::= N D | D | Number ::= N D 9 … | 1 | ::= 0 | Digit Exp ssi n :: E1 E1 E2 | ona i » sf Denotational expressions – ran t sfvalues of the active objects i thf ti l ti A state of a program indicates the current Example Define the meaning of statement as a state- enotat • • •

Semantic Domains ( Dil D – 24

) ])

t E2

’ [ []) r p ) Ex ’t con t ,

] E1 con [ [], r p } s ( ber … c

Ex , m ( ti 1 , lus 0 p , 1 0 } - =

eman ]] …, ner => Nu Stis ( Stis S unctions: l m f E1+E2 ona iary [[ ]] p ( l f r ti p a t plus: Number + => Semantic domain: Integer = { Semantic functions: Value: Nu Digit: Digit => Number Expr: Expression => Integer Auxi … Semantic equations: Ex – – – – eno

Semantic Domains ( Dttil D – 25 e p y t a t a d

Data Types a f o licit nts p 5 is implicitly integer implicitly, in Fortran I is integer, Using variable or function declaration e • • • m Set of operations External representation Im Explicit Set of possible values Internal representation e – – – – – – El mdttp nts f Type information • • 26 e g

) t ’ ore usa f

con t ons i ined be cat f ifi ass l c

in - Primitive Recursive Composite

Data Types ( type eclared and de

– – – Included in the language definition Data types defined by users D Dffg • • • Built User-defined ata – – D lifii D • 27 es titi en

e ibl s i v di n d i bers m an d ure oint nu t p pm th er, Real, Boolean, Char ruc g g

Primitive Data Types ain t ixed- m f COBOL: fixed-length strings and SNOBOL: Strings with different len Scheme: integer, rational, real, complex ns – – – U t tidi i d ibl titi Depends to the language application do Inte • • • 28

) t ’

con t char?

, char ean

Longint l ,

, oo boolean? bl b

, word ,

Real ,

ar , h char c int

real? , , , Char t ,

, oa real er?

float fl t , g

e , , l t n it i Integer bool int inte • • • • • C Java Pascal ML Scheme xamp – – – – – Primitive Data Types ( El E • 29

, ) t ’ short

, con t byte

zes: i s

eger t n d i gne i s

l s ’ a long i v , iili ava Almost always an exact reflection of the hardware so mapping is tr i it There may be as many eight different integer types in a language id J’ J int – – – Primitive Data Types ( Integer • 30

) t

t u

’ b

con t e.g., ( ware, d ar ypes h t e n it tit i ( th t e n i o ng-po lik Pit P y ti tl ons ng- oa ti ti fl exac oa ma

iti i wo y ll t t sua Standard 754 eas approx hdlbtthfloat and double; sometimes more t tUlik fl not always ll ti tlIEEE Fl ti Languages for scientific use support at Languages for Model real numbers, but only as oa – – – – – Primitive Data Types ( Fl t • 31

) t

’ ,

con t : ) Python

in ( form l ( ) l ex l 3j), where 7 is the real part and + Some languages support a complex type, e.g., C99, Fortran, and Python Each value consists of two floats the real part and imaginary Litera (7 3j), where 7 is the real part and part 3 is the imaginary omp – – – Primitive Data Types ( Cl C • 32

) t ’ igits, l d con t wastes

ecima f d range, o

d imite l number d ixe f l) a a f d d f d l l vantages: d im ma Essential to COBOL C# offers a i ec isa • • Store in coded form (BCD) (Binary-Coded Diml) D DdD memory l d For business applications (money) For Advantage: accuracy ec – – – – Primitive Data Types ( DiD l • 33

) t ’

con t Simplest of all Range of values: two elements, one for “true” and “false” Could be implemented as , but often as – – – Primitive Data Types ( Boolean • 34

) t ’

con t ASCII ng: di co d use

y l es g er 2) (Universal Set) ua common t - g gg t Includes characters from most natural Includes characters from most lan Java in Originally used C# and JavaScript also support Unicode with 2003 starting Supported by Fortran, os • • • • arac Stored as numeric codings numeric Stored as alternative, 16- coding: Unicode An M t l d di ASCII di d M t l (UCS 32-bit Unicode (UCS-4) – – – – Primitive Data Types ( Ch t • 35 ypes d t y compoun unction

, Enumeration, Pointer, F g or d ist, L , ure eneous like Arra , Strin d t y g y yg o Composite Data Types m mg y ruc ecor Rd L F St L tRd R d t Fixed size like Array d Heterogeneous like Record Dynamic size like Linked List Inside the core or as a separate librar Ho Arra • • • • • • • 36

) t ’ enum

con t , Struct

), ) * ^ ( ( Pointer , Pointer ), ( [] ( Array Array

, , y y( Arra String Record • • • Java C Pascal – – – Example

Composite Data Types ( • 37

) t ’

con t and PHP y, Rub , t p JavaScri , ,p,y, Not primitive that Use char arrays and of functions a provide operations Primitive elaborate pattern matching Many operations, including and several operations Primitive type with assignment Primitive via the String class expressions regular pattern matching, using Provide built-in • • • • • • • ng i SNOBOL4 (a string manipulation language) manipulation SNOBOL4 (a string Java Perl C and C++ Fortran and Python r – – – – – St i

Composite Data Types ( • 38

) t ’

con t th class g g the len the g gg a special character is used , ava’s Strin J

L, J g option h engt lh lh l In these languages In these languages a special character is used to indicate the end of a string’s characters, rather than maintainin • Limited Dynamic Length: C and C++ Dynamic (no maximum): SNOBOL4, Perl, Adaoptions three string length supports all Static: COBO JavaScript – – – – String

Composite Data Types ( • 39

) t ’

con t allocation is - time descriptor - ) Limited dynamic length: may need a Dynamic length: need run-time Static length: compile run-time descriptor for length (but not in C and C++ descriptor; allocation/de the biggest implementation problem – – – String Implementation

Composite Data Types ( • 40

) t ’

con t inition f e d e h t

in d e dd h df h dd d Is an enumeration constant allowed to appear in more an type of is the how and if so, one type definition, than occurrence of that constant checked? integer? Are enumeration values coerced to an enumeration type? type coerced to Any other • • • provi enum days {mon, tue, wed, thu, fri, sat, sun}; All possible values, which are named constants, possible values, All Design issues C# example numeration – – – E

Composite Data Types ( • 41

) t e ’ tsid ou

type ue

con t l va ort for

a pp d gne ssi a

enumeration e b can

rovide better su rovide better e p ppp bl a i ) t ’ var

on con ti ( enumera er o b operations (don’t allow colors to be added) N ti ibl b ssid l tsid l ssid b iblN tiits defined range C#, and Java 5.0 Ada, enumeration than C++ because types variables in these languages are not coerced into integer • • • num enum Colors {Red, Blue, Green, Yellow}; Aid to reliability, e.g. compiler can check: Aid to readability, e.g. no need to code a color as numeration – – E (

Composite Data Types ( • 42

) t ’

con t gn Types i

es di d s ’ Days; a Example: 12..18 integer type Example: is a sub-range of

-range • b type Ad ’ An ordered contiguous subsequence of an ordinal – – type Days is (mon, tue, wed, thu, fri, sat, sun); subtype Weekdays is Days range mon..fri; subtype Index is Integer range 1..100; Day1: Days; Day2: Weekday; Day2 := Day1; Su

Composite Data Types ( • 43

) t ’

con t

Sub-range

and

lementation p Enumeration types are implemented as Sub-range types are implemented like the integers code inserted (by the parent types with compiler) to restrict assignments to sub- range variables numeration – – E im

Composite Data Types ( • 44

) t ’ and Ruby con t , eneous g o m osition in the osition in p ho f JavaScript , its y ate o g Python , re ied b f gg is an a an is y ygggfmg ent is identi m mfyp Supported by Perl Python JavaScript and Ruby • An arra data elements in which an individual ele aggregate, relative to the first element. A heterogeneous array is one in which the elements need not be of the same type – – Array

Composite Data Types ( • 45

) t ’ t it u b

con t ng, ki ec h range checking c

range

re i requ

o t s lt i au f e Type d

e th th d f lt i t i h ki b t it ex a, d Ad In

d n C, C++, Perl, and Fortran do not specify C, ML, C# specify range Java, checking I can be turned off • • • Java: integer types only Index range checking FORTRAN, C: integer only enumeration (includes Boolean and integer or Ada: char) – – – – Array

Composite Data Types ( • 46

) t ’ 0]

con t == 3

% x if

(12) n range i or x f

2 ization

l ** x st [] = {1, 3, 5, 7} st [] = [ i Initia l = t s nt l i Ada List : array (1..5) of Integer := (1 => 17, 3 => 34, others 0); li tputs [0, 9, 36, 81] in list [ ** 2 f i (12) if % 3 0] char *names [] = {“Mike”, “Fred”,“Mary Lou”}; C-based languages Python List comprehensions – – – Array

Composite Data Types ( • 47

)

on t g ’ ti

y ena l t on con t rocessin p

are conca y

so ey l a t t th u u b t b s, t owerful arra p gnmen i gnmen i ass

ass

array

array

s ts) ows ’ Operations ll i t bt lll itbt ti t rovides the most

p ppypg on aa th emen lts) l y operations for vectors and matrixes as well and matrixes operations for vectors column to reverse unary operators (for example, l e th Ad ibt Pth’ P reference changes. Python also supports array t operations membership element concatenation and APL – – – Array

Composite Data Types ( • 48

) t ’

con t ons ti opera l a of the sums of sums the of t y emen l e

es id prov s

t s results in an arra ran y yy t emen or lt l For example, + operator between two arra Ruby also provides array concatenation ltl ti FtF id because they are between pairs of array e the element pairs of two arrays – – – Array Operations (con’t)

Composite Data Types ( • 49

) t ’ f o

con t er b num

same

e h t

Arrays agged arrays j d ave h agged arrays) j Jagge rows

d e h t an

f o ar l d d ll a h Possible when multi-dimensioned arrays actually appear Possible when multi-dimensioned arrays actually as arrays of ic h h ll f h f h ll hh h b f • w number of same the and all columns have elements elements elements (C# also supports Fortran, Ada, and C# support rectangular arrays A rectangular array is a multi-dimensioned array in A jagged matrix has rows with varying number of C, C++, and Java support – – – – Rectangu

Composite Data Types ( • 50

) t ’

con t Cube

or t :: ec V 4) anism h , 3 :: , mec

(10) (3

on returns the third and fourth i erencing f mens re

Di Dimension a

orts slices with the slice method , an pp ons h t ti h f h su eger, y ypp t n I tInteger Dimension Dielement array Vector (3:6) is a four (3 3 i 4) :: Cube (10) V t Integer, Dimension (3, 3) :: Mat elements of list more opera Rub list.slice(2, 2) A slice is some substructure of an array; nothing Slices are only useful in languages that have array Fortran 95 ices l – – – – S

Composite Data Types ( • 51

) t ’

con t

Composite Data Types ( 52

) t ’ ran con t t or F n d i F t –use ) umns l co

y

(b er d array

or + ((k-lower_bound) * element_size)

e or h j t

ma in

umn Access l j d (b l ) l ) (b d l j

ress Row major order (by rows) –Row languages used in most co • • ddh a = address (list[lower_bound]) address(list[k]) Two common ways: Access function Access maps subscript expressions to an single-dimensioned arrays: function for Access – – – Array

Composite Data Types ( • 53

) s; t d ’ are

recor con t d e ements t l e l nes

ua d ow h ivi s

d o in

t e X(20). h

t ers b h PIC ic

h num w l

in

FIRST eve

l . 05 FIRST PIC05 MID PIC X(10). X(20). 05 LAST PIC X(20). uses REC ements - l e 02 EMP-NAME. 02 HOURLY-RATE PIC 99V99.

d EMP

ata d l hh h ddl l ddl h l hh d hlltdnames identified by t b d COBOL others use recursive definition 01 EMP A record is a possibly heterogeneous aggregate of – – Recor

Composite Data Types ( • 54

) t ’

con t (1..20); g ) con’t ( First: String (1..20); Mid: String (1..10); Last: Strin Hourly_Rate: Float; type Emp_Rec_Type is record Emp_Rec: Emp_Rec_Type; Ada end record; – Record

Composite Data Types ( • 55

)

t Oct ’ p, Se con t g, Au , Jul , Jun y, Ma , r p A , Mar , Feb , , , p , y, , , g, p, : 1..31; y ; Jan Pascal ( ,Nov,Dec); Month : MonthType; Da Year : 1900..2000; DateType = record end – MonthType = Record (con’t)

Composite Data Types ( • 56

) t ’

con t

; C char name[20]; int ID – struct student_type { } Record (con’t)

Composite Data Types ( • 57

) t ’

con t It is defined using It is . ; number _ { id number age;

String name; int Date birthday; int Person

Java: No record in Javadefined using It is class. } – class Person { Record (con’t)

Composite Data Types ( • 58

) t ’

con t e of values that e of values g Types

e variable has a ran Reference

yp and

ointer t p pyp g consists of memory addresses and a special value, nil Provide the power of indirect addressing Provide a way to manage dynamic memory A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap) A – – – – Pointer

Composite Data Types ( • 59

) t ’

con t th? ointer o p b or

ng, i ress dd a t e and lifetime of a p pp Issues rec or both?

, di n i , t i di t dd i b th? Design

variable? Are pointers restricted as to the type of value to which they can point? pointers used for dynamic storage Are managemen reference types or both? What are the sco Should the language support pointer types, – – – – Pointer

Composite Data Types ( • 60

) t ’

con t ue l va

s ’ nment and er g ptr t n i * po

e th y erations: assi d b p pg e t address l to the value located at represen

j Operations

on ti Dereferencing can be explicit or implicit Dereferencing can be explicit C++ uses an explicit operation via j = *ptr sets • • oca lti td b th it’ dereferencing l a pointer variable’s value is used to set Assignment it’ to some usefu th b lti l td Two fundamental o Dereferencing yields the value stored at – – – Pointer

Composite Data Types ( • 61

) t ’

con t j = *ptr eration p ustration nment o ll g gp I ll The assi – Pointer

Composite Data Types ( • 62

) t

’ - p

con t created hea y newly created heap-dynamic variable oint to another newl p 1 is later set to 1 is later set p pp yp Pointer Pointer p1 is set to point a Pointer p1 is set dynamic variable The process of losing heap-dynamic variables is called memory leakage – – – A pointer has been points to a heap-dynamic variable that de-allocated is no longer variable that heap-dynamic An allocated accessible to the user program (often called garbage) user program (often called to the accessible • • Lost heap-dynamic variable Dangling pointers (dangerous) – – Pointer Problems

Composite Data Types ( • 63

) t ’ - con t c storage management and management c storage i ++ dynamic objects can be automatically de scope end of pointer's type the allocated at Pointers can point at any variable regardless of when or where it was allocated Used for dynamdynam c storage management and addressing Some dangling Some pointers are disallowed because be used with care flexible but must Extremely • • • • Ada C, C – – Pointer Problems (con’t)

Composite Data Types ( • 64

) t ’ ) * con t of - void referenced) - void * can point to any type and be Explicit dereferencing and address operators Domain type need not be fixed ( checked (cannot be de Pointer arithmetic is possible C++ , • • • C C++ – Pointer Problems (con’t)

Composite Data Types ( • 65

) t ’ [5] p p[i] con t and and

ff[5] u t ff[5] s stuff[i] is equivalent to is equivalent to stuff[100];

+5) p = stuff; float stuff[100]; float *p; p *( +5) *(p+i) Pointer Arithmetics in C, C++

Composite Data Types ( • 66

) ue t l ’ y-va b

con t e called a pass- yp d an

ointer t erence p f d f d y-re b pass- h ecial of ot p ppyp f b o

Types

vantages References are references to objects, rather than being rather than objects, References are references to addresses Adb f h • • reference type that is used primarily for formal parameters to replace pointers entirely them C++ includes a s Java extends C++’s reference variables and allows C# includes both the references of Java and pointers of C++ – – – Reference

Composite Data Types ( • 67

) t on

’ ti ama con t l rec

: h) time process - approac

azy y t (l h) l ti emp

-sweep k le-size cells vs. variable-size cells ar ecomes g Reference counters (eager approach): reclamation is gradual Mk M occurs when the list of variable space b t • • Sin Two approaches to reclaim garbage A very complex run – – – Heap Management

Composite Data Types ( • 68

) t ’

con t lications for cells connected lications for cells p com , uired q q,p number of pointers currently pointing at the cell number of pointers currently pointing at the circularly it is intrinsically incremental, so Advantage: application execution in the significant delays are avoided Maintain a counter in every cell that store the cell that store Maintain a counter in every space required, execution time Disadvantages: re • • • Reference counters – Heap Management (con’t)

Composite Data Types ( • 69

) y. l t ’ requent f in con t too

one d was

) it ms avoid this by doing it more

delays in application execution. -sweep orm, it used by collection algorithm k and reachable cells marked as not cells and reachable , l f con’t mar l ( a t origina

its

in

ncremen d i p e ll d i t l k vantages: d ins en—ca Management g isa ( ft All cells initially set to garbage Contemporary mark-sweep algorith The run-time system allocates storage cells as requested and run-time system allocates storage cells The disconnects pointers from cells as necessary; mark-sweep then be Every heap cell has an extra b All pointers traced into into heap heap and reachable cells marked as not garbage returned to list of available cells cells garbage All DdD When done, it caused significant o f d f l l • • • • • • Mark-Swee – Heap

Composite Data Types ( • 70 f o

s t intlist * ec bj o

rom d f 10 le compose p

5

Recursive Data Types datatype intlist = nil | of int Linked list in C and Pascal ML ype – – T d f bjt f bjt f Tthe same type d Recursive or circular data types Exam • • • 71

owing ) ll e o l f e h t examp f

o y h b

Exercises ic h ain l w

xp (E y not: (Elb l ) etermine Ada Perl Python Haskell Prolog Fortran Rub D hh f h fll fll h f D hh are statically typed programming languages or – – – – – – – . 1 72

g C.

in

error

and late bindin and y condition of type - f o

e l les for earl p py g examp

Exercises er h anot

{?} a = b * -1; {a > 10} to float? ring B h l f f B h l in a language. Is there any programming language which implicit type conversion, say does not allow int compute the Weakest Pre . 2. 4. 5. Which type of coercions is not safe? 6 3. Show two exam 73 f o

e l ru

′ e Q h t

⇒ Q , } ow ′ P Q h { s ⇒ a

′ } ′ P P e, { l }, Q

Exercises { a } P { examp

s = + i; i = i + 1; an l h h l f f l h h l i = 1; s = 0; while (i <= 10) { } . consequence in axiomatic semantic. loop 2. Using 3. Find the loop invariant of the following while 74 a d A

les. p except ) s ( anguage l

Exercises programming h ic -reference in Pascal? Give exam hh l() l() hh h y and different versions of C, support pointer? automatic garbage collection. What are the automatic garbage collection. negative and positive effects of this language? a operation in b 7. W 8.and call- What are the rules of call-by-value 9. Name two programming languages which have