0% found this document useful (0 votes)
3 views107 pages

02-Valuesandtypes PLC

The document discusses programming languages, focusing on the concepts of values and types, including primitive and composite types, type systems, and mappings. It covers various data structures such as arrays, functions, and recursive types, and explains the significance of type cardinality and disjoint unions. Additionally, it provides examples from languages like C and Haskell to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views107 pages

02-Valuesandtypes PLC

The document discusses programming languages, focusing on the concepts of values and types, including primitive and composite types, type systems, and mappings. It covers various data structures such as arrays, functions, and recursive types, and explains the significance of type cardinality and disjoint unions. Additionally, it provides examples from languages like C and Haskell to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

Programming Languages/Values and Types

Programming Languages/Values and Types

Onur Tolga Şehitoğlu

Computer Engineering, METU

Spring’2008
Programming Languages/Values and Types

Outline

1 Value and Type 8 Type Systems


Static Type Checking
2 Primitive vs Composite Types
Dynamic Type Checking
3 Cartesian Product Type Equality
4 Disjoint Union 9 Type Completeness
5 Mappings 10 Expressions
Arrays Literals/Variable and Constant Acces
Functions Aggregates
6 Powerset Variable References
7 Recursive Types Function Calls
Lists Conditional Expressions
General Recursive Types Iterative Expressions
Strings 11 Summary
Programming Languages/Values and Types
Value and Type

What are Value and Type?

Value anything that exist, that can be computed, stored, take


part in data structure.
Constants, variable content, parameters, function return
values, operator results...
Type set of values of same kind.
Programming Languages/Values and Types
Value and Type

What are Value and Type?

Value anything that exist, that can be computed, stored, take


part in data structure.
Constants, variable content, parameters, function return
values, operator results...
Type set of values of same kind.
C types:
int, char, long,...
float, double
pointers
structures: struct, union
arrays
Programming Languages/Values and Types
Value and Type

Haskell types
Bool, Int, Float, ...
Char, String
tuples,(N-tuples), records
lists
functions
Each type represents a set of values. Is that enough?
Programming Languages/Values and Types
Value and Type

Haskell types
Bool, Int, Float, ...
Char, String
tuples,(N-tuples), records
lists
functions
Each type represents a set of values. Is that enough?
What about the following set? Is it a type?
{"ahmet", 1 , 4 , 23.453, 2.32, ’b’}
Programming Languages/Values and Types
Value and Type

Haskell types
Bool, Int, Float, ...
Char, String
tuples,(N-tuples), records
lists
functions
Each type represents a set of values. Is that enough?
What about the following set? Is it a type?
{"ahmet", 1 , 4 , 23.453, 2.32, ’b’}
Values should exhibit a similar behavior. The same group of
operations should be defined on them.
Programming Languages/Values and Types
Primitive vs Composite Types

Primitive vs Composite Types

Primitive Types: Values that cannot be decomposed into


other sub values.
C: int, float, double, char, long, short, pointers
Haskell: Bool, Int, Float, function values
cardinality of a type: The number of distinct values that a
datatype has. Denoted as: ”#Type”.
#Bool = 2 #char = 256 #short = 216
#int = 232 #double = 232 , ...
What does cardinality mean?
Programming Languages/Values and Types
Primitive vs Composite Types

Primitive vs Composite Types

Primitive Types: Values that cannot be decomposed into


other sub values.
C: int, float, double, char, long, short, pointers
Haskell: Bool, Int, Float, function values
cardinality of a type: The number of distinct values that a
datatype has. Denoted as: ”#Type”.
#Bool = 2 #char = 256 #short = 216
#int = 232 #double = 232 , ...
What does cardinality mean? How many bits required to store
the datatype?
Programming Languages/Values and Types
Primitive vs Composite Types

User Defined Primitive Types

enumerated types
enum days {mon, tue, wed, thu, fri, sat, sun};
enum months {jan, feb, mar, apr, .... };
ranges (Pascal and Ada)
type Day = 1..31;
var g:Day;
Discrete Ordinal Primitive Types Datatypes values have one
to one mapping to a range of integers.
C: Every ordinal type is an alias for integers.
Pascal, Ada: distinct types
DOPT’s are important as they
i. can be array indices, switch/case labels
ii. can be used as for loop variable (some languages like
pascal)
Programming Languages/Values and Types
Primitive vs Composite Types

Composite Datatypes

User defined types with composition of one or more other


datatypes. Depending on composition type:
Cartesian Product (struct, tuples, records)
Programming Languages/Values and Types
Primitive vs Composite Types

Composite Datatypes

User defined types with composition of one or more other


datatypes. Depending on composition type:
Cartesian Product (struct, tuples, records)
Disjoint union (union (C), variant record (pascal), Data
(haskell))
Programming Languages/Values and Types
Primitive vs Composite Types

Composite Datatypes

User defined types with composition of one or more other


datatypes. Depending on composition type:
Cartesian Product (struct, tuples, records)
Disjoint union (union (C), variant record (pascal), Data
(haskell))
Mapping (arrays, functions)
Programming Languages/Values and Types
Primitive vs Composite Types

Composite Datatypes

User defined types with composition of one or more other


datatypes. Depending on composition type:
Cartesian Product (struct, tuples, records)
Disjoint union (union (C), variant record (pascal), Data
(haskell))
Mapping (arrays, functions)
Powerset (set datatype (Pascal))
Programming Languages/Values and Types
Primitive vs Composite Types

Composite Datatypes

User defined types with composition of one or more other


datatypes. Depending on composition type:
Cartesian Product (struct, tuples, records)
Disjoint union (union (C), variant record (pascal), Data
(haskell))
Mapping (arrays, functions)
Powerset (set datatype (Pascal))
Recursive compositions (lists, trees, complex data structures)
Programming Languages/Values and Types
Cartesian Product

Cartesian Product

S × T = {(x, y ) | x ∈ S, y ∈ T }
Example:
S = {a, b, c} T = {1, 2}
S × T = {(a, 1), (a, 2), (b, 1), (b, 2), (c, 1), (c, 2)}
•a •(a,1) •(a,2)
•1
•b × = •(b,1) •(b,2)
•2
•c •(c,1) •(c,2)
#(S × T ) =
Programming Languages/Values and Types
Cartesian Product

Cartesian Product

S × T = {(x, y ) | x ∈ S, y ∈ T }
Example:
S = {a, b, c} T = {1, 2}
S × T = {(a, 1), (a, 2), (b, 1), (b, 2), (c, 1), (c, 2)}
•a •(a,1) •(a,2)
•1
•b × = •(b,1) •(b,2)
•2
•c •(c,1) •(c,2)
#(S × T ) =#S · #T
Programming Languages/Values and Types
Cartesian Product

C struct, Pascal record, functional languages tuple


in C: string × int
struct P e r s o n {
char name [20];
int no ;
} x = { " Osman Hamdi " ,23141};

in Haskell: string × int


type P e o p l e =( String , Int )
...
( x :: P e o p l e ) = ( " Osman Hamdi " ,23141)
Programming Languages/Values and Types
Cartesian Product

Multiple Cartesian products:


C: string × int × {MALE,FEMALE}
struct P e r s o n {
char name [20];
int no ;
enum Sex {MALE, FEMALE} s e x ;
} x = { " Osman Hamdi " ,23141 ,FEMALE};

Haskell: string × int × float × String


x = ( " Osman Hamdi " ,23141 ,3.98 , " Yazar " )
Programming Languages/Values and Types
Cartesian Product

Homogeneous Cartesian Products

n
z }| {
Sn = S × S × S × ... × S
double4 :
struct quad { double x , y , z ,q; };

S 0 = {()} is 0-tuple.
not empty set. A set with a single value.
terminating value (nil) for functional language lists.
C void. Means no value. Error on evaluation.
Programming Languages/Values and Types
Disjoint Union

Disjoint Union

S + T = {left x | x ∈ S} ∪ {right x | x ∈ T }
Example:
S = {1, 2, 3} T = {3, 4}
S + T = {left 1, left 2, left 3, right 3, right 4}
•1 •left 1 •left 2
•3
•2 + = •left 3
•4
•3 •right 3 •right 4
#(S + T ) =
Programming Languages/Values and Types
Disjoint Union

Disjoint Union

S + T = {left x | x ∈ S} ∪ {right x | x ∈ T }
Example:
S = {1, 2, 3} T = {3, 4}
S + T = {left 1, left 2, left 3, right 3, right 4}
•1 •left 1 •left 2
•3
•2 + = •left 3
•4
•3 •right 3 •right 4
#(S + T ) =#S + #T
C union’s are disjoint union?
Programming Languages/Values and Types
Disjoint Union

C: int + double:
union number { double r e a l ; int i n t e g e r ; } x ;

C union’s are not safe! Same storage is shared. Valid field is


unknown:
x . r e a l =3.14; p r i n t f ( " % d \ n " , x . i n t e g e r );

Haskel: Float + Int + (Int × Int):


data Number = R e a l V a l Float | I n t V a l Int | Rational ( Int , Int )
x = Rational (3 ,4)
y = R e a l V a l 3.14
z = I n t V a l 12 { - - You cannot access d i f f e r e n t values - -}
Programming Languages/Values and Types
Mappings

Mappings

The set of all possible mappings


S 7→ T = {V | ∀(x ∈ S)∃(y ∈ T ), (x 7→ y ) ∈ V }
Example: S = {a, b} T = {1, 2, 3}
•1
•a Each color is a mapping value
•2
•b There are many others
•3
S 7→ T = {{a 7→ 1, b 7→ 1}, {a 7→ 1, b 7→ 2}, {a 7→ 1, b 7→ 3},
{a 7→ 2, b 7→ 1}, {a 7→ 2, b 7→ 2}, {a 7→ 2, b 7→ 3},
{a 7→ 3, b 7→ 1}, {a 7→ 3, b 7→ 2}, {a 7→ 3, b 7→ 3}}
#(S 7→ T ) =
Programming Languages/Values and Types
Mappings

Mappings

The set of all possible mappings


S 7→ T = {V | ∀(x ∈ S)∃(y ∈ T ), (x 7→ y ) ∈ V }
Example: S = {a, b} T = {1, 2, 3}
•1
•a Each color is a mapping value
•2
•b There are many others
•3
S 7→ T = {{a 7→ 1, b 7→ 1}, {a 7→ 1, b 7→ 2}, {a 7→ 1, b 7→ 3},
{a 7→ 2, b 7→ 1}, {a 7→ 2, b 7→ 2}, {a 7→ 2, b 7→ 3},
{a 7→ 3, b 7→ 1}, {a 7→ 3, b 7→ 2}, {a 7→ 3, b 7→ 3}}
#(S 7→ T ) =#T #S
Programming Languages/Values and Types
Mappings
Arrays

Arrays
double a[3]={1.2,2.4,-2.1};
a ∈ ({0, 1, 2} 7→ double)
a = (0 7→ 1.2, 1 7→ 2.4, 2 7→ −2.1)
Arrays define a mapping from an integer range (or DOPT) to
any other type
C: T x[N] ⇒ x ∈ ({0, 1, ..., N − 1} 7→ T )
Other array index types (Pascal):
type
Day = (Mon,Tue ,Wed,Thu, F r i , Sat , Sun );
Month = ( Jan , Feb ,Mar, Apr ,May, Jun , J u l ,Aug , Sep , Oct ,Nov , Dec );
var
x : array Day of real ;
y : array Month of integer ;
...
x [Tue] := 2.4;
y [ Feb ] := 28;
Programming Languages/Values and Types
Mappings
Functions

Functions

C function:
int f ( int a ) {
if ( a %2 == 0) return 0;
else return 1;
}

f : int 7→ {0, 1}
regardless of the function body: f : int 7→ int
Haskell:
f a = if mod a 2 == 0 then 0 else 1

in C, f expression is a pointer type int (*)(int)


in Haskell it is a mapping: int7→int
Programming Languages/Values and Types
Mappings
Functions

Array and Function Difference

Functions
Arrays: Defined by algorithms
Values stored in memory Efficiency, resource usage
Restricted: only integer All types of mappings
domain possible
double7→double ? Side effect, output, error,
termination problem.
Cartesian mappings:
double a[3][4];
double f(int m, int n);
int×int7→double and int7→(int7→double)
Programming Languages/Values and Types
Mappings
Functions

Cartesian Mapping vs Nested mapping

Pascal arrays
var
x : array [1..3 ,1..4] of d o u b l e ;
y : array [1..3] of array [1..4] of d o u b l e ;
...
x [1 ,3] := x [2 ,3]+1; y [1 ,3] := y [2 ,3]+1;

Row operations: x y

y[1] := y[2] ; →
x[1] := x[2] ; × →

Programming Languages/Values and Types
Mappings
Functions

Haskell functions:
f (x ,y) = x+y
g x y = x+y
...
f (3+2)
g 3 2

g 3
f 3×
Reuse the old definition to define a new function:
increment = g 1
increment 1
2
Programming Languages/Values and Types
Powerset

Powerset

P(S) = {T | T ⊆ S}
The set of all subsets
•1 •∅ •{1} •{2} •{3}
S = •2 P(S) = •{1, 2} •{1, 3}
•3 •{2, 3} •{1, 2, 3}
#P(S) =
Programming Languages/Values and Types
Powerset

Powerset

P(S) = {T | T ⊆ S}
The set of all subsets
•1 •∅ •{1} •{2} •{3}
S = •2 P(S) = •{1, 2} •{1, 3}
•3 •{2, 3} •{1, 2, 3}
#P(S) =2#S
Programming Languages/Values and Types
Powerset

Set datatype is restricted and special datatype. Only exists in


Pascal and special set languages like SetL
set operations (Pascal)
type
c o l o r = ( r e d , g r e e n , b l u e , w h i t e , b l a c k );
c o l o r s e t = set of c o l o r ;
var
a ,b : c o l o r s e t ;
...
a := [ r e d , b l u e ];
b := a *b; (* i n t e r s e c t i o n *)
b := a +[ g r e e n , r e d ]; (* union *)
b := a -[ b l u e ]; (* d i f f e r e n c e *)
if ( g r e e n in b) then ... (* element test *)
if ( a = []) then ... (* set e q u a l i t y *)

in C++ supported by library.


Programming Languages/Values and Types
Recursive Types
Lists

Recursive Types

S = ...S...
Types including themselves in composition.
Lists
S = Int × S + {null}

S = {right empty } ∪ {left (x, empty ) | x ∈ Int}∪


{left (x, left (y , empty )) | x, y ∈ Int}∪
{left (x, left (y , left (z, empty ))) | x, y , z ∈ Int} ∪ ...

S=
{right empty , left(1, empty ), left(2, empty ), left(3, empty ), ...,
left(1, left(1, empty )), left(1, left(2, empty )), left(1, left(3, empty ), ...,
left(1, left(1, left(1, empty ))), left(1, left(1, left(2, empty ))), ...}
Programming Languages/Values and Types
Recursive Types
Lists

C lists: pointer based. Not actual recursion.


struct L i s t {
int x ;
L i s t * next ;
} a;
Programming Languages/Values and Types
Recursive Types
Lists

C lists: pointer based. Not actual recursion.


struct L i s t {
int x ;
L i s t * next ;
} a;

Haskell lists.
data List = Left ( Int , List ) | Empty

x = Left (1 , Left (2 , Left (3 , Empty ))) { - - [1 ,2 ,3] list - -}


y = Empty { - - empty list , []
- -}
Programming Languages/Values and Types
Recursive Types
Lists

Polymorphic lists: a single definition defines lists of many


types.
Programming Languages/Values and Types
Recursive Types
Lists

Polymorphic lists: a single definition defines lists of many


types.
List α = α × (List α) + {empty }
data List a l p h a = Left ( a l p h a , List a l p h a ) | Empty

x = Left (1 , Left (2 , Left (3 , Empty ))) { - - [1 ,2 ,3] list - -}


y = Left ( " ali " , Left ( " ahmet " ,Empty )) { - - [" ali " ," ahmet "] - -
z = Left (23.1 , Left (32.2 , Left (1.0 , Empty ))) { - - [23.1 ,32.2 ,1.0] -
Programming Languages/Values and Types
Recursive Types
Lists

Polymorphic lists: a single definition defines lists of many


types.
List α = α × (List α) + {empty }
data List a l p h a = Left ( a l p h a , List a l p h a ) | Empty

x = Left (1 , Left (2 , Left (3 , Empty ))) { - - [1 ,2 ,3] list - -}


y = Left ( " ali " , Left ( " ahmet " ,Empty )) { - - [" ali " ," ahmet "] - -
z = Left (23.1 , Left (32.2 , Left (1.0 , Empty ))) { - - [23.1 ,32.2 ,1.0] -

Left(1, Left(“ali ′′ , Left(15.23, Empty ) ∈ List α ?


Programming Languages/Values and Types
Recursive Types
Lists

Polymorphic lists: a single definition defines lists of many


types.
List α = α × (List α) + {empty }
data List a l p h a = Left ( a l p h a , List a l p h a ) | Empty

x = Left (1 , Left (2 , Left (3 , Empty ))) { - - [1 ,2 ,3] list - -}


y = Left ( " ali " , Left ( " ahmet " ,Empty )) { - - [" ali " ," ahmet "] - -
z = Left (23.1 , Left (32.2 , Left (1.0 , Empty ))) { - - [23.1 ,32.2 ,1.0] -

Left(1, Left(“ali ′′ , Left(15.23, Empty ) ∈ List α ?


Programming Languages/Values and Types
Recursive Types
Lists

Polymorphic lists: a single definition defines lists of many


types.
List α = α × (List α) + {empty }
data List a l p h a = Left ( a l p h a , List a l p h a ) | Empty

x = Left (1 , Left (2 , Left (3 , Empty ))) { - - [1 ,2 ,3] list - -}


y = Left ( " ali " , Left ( " ahmet " ,Empty )) { - - [" ali " ," ahmet "] - -
z = Left (23.1 , Left (32.2 , Left (1.0 , Empty ))) { - - [23.1 ,32.2 ,1.0] -

Left(1, Left(“ali ′′ , Left(15.23, Empty ) ∈ List α ? No.


Most languages only permits homogeneous lists.
Programming Languages/Values and Types
Recursive Types
Lists

Haskell Lists

binary operator “:” for list construction:


data [alpha] = (alpha : [alpha]) | []
Programming Languages/Values and Types
Recursive Types
Lists

Haskell Lists

binary operator “:” for list construction:


data [alpha] = (alpha : [alpha]) | []
x = (1:(2:(3:[])))
Programming Languages/Values and Types
Recursive Types
Lists

Haskell Lists

binary operator “:” for list construction:


data [alpha] = (alpha : [alpha]) | []
x = (1:(2:(3:[])))
Syntactic sugar:
[1,2,3] ≡ (1:(2:(3:[])))
["ali"] ≡ ("ali":[])
Programming Languages/Values and Types
Recursive Types
General Recursive Types

General Recursive Types

T = ...T ...
Programming Languages/Values and Types
Recursive Types
General Recursive Types

General Recursive Types

T = ...T ...
Formula requires a minimal solution to be representable:
S = Int × S
Is it possible to write a single value? No minimum solution
here!
Programming Languages/Values and Types
Recursive Types
General Recursive Types

General Recursive Types

T = ...T ...
Formula requires a minimal solution to be representable:
S = Int × S
Is it possible to write a single value? No minimum solution
here!
List example:
x = Left(1,Left(2,x))
x ∈ S?
Programming Languages/Values and Types
Recursive Types
General Recursive Types

General Recursive Types

T = ...T ...
Formula requires a minimal solution to be representable:
S = Int × S
Is it possible to write a single value? No minimum solution
here!
List example:
x = Left(1,Left(2,x))
x ∈ S?
Programming Languages/Values and Types
Recursive Types
General Recursive Types

General Recursive Types

T = ...T ...
Formula requires a minimal solution to be representable:
S = Int × S
Is it possible to write a single value? No minimum solution
here!
List example:
x = Left(1,Left(2,x))
x ∈ S? Yes
can we process [1,2,1,2,1,2,...] value?
Some languages like Haskell lets user define such values. All
iterations go infinite. Useful in some domains though.
Programming Languages/Values and Types
Recursive Types
General Recursive Types

General Recursive Types

T = ...T ...
Formula requires a minimal solution to be representable:
S = Int × S
Is it possible to write a single value? No minimum solution
here!
List example:
x = Left(1,Left(2,x))
x ∈ S? Yes
can we process [1,2,1,2,1,2,...] value?
Some languages like Haskell lets user define such values. All
iterations go infinite. Useful in some domains though.
Most languages allow only a subset of S, the subset of finite
values.
Programming Languages/Values and Types
Recursive Types
General Recursive Types

Tree α = empty + node α × Treeα × Treeα


Tree α = {empty } ∪ {node(x, empty , empty ) | x ∈ α}∪
{node(x, node(y , empty , empty ), empty ) | x, y ∈ α}∪
{node(x, empty , node(y , empty , empty )) | x, y ∈ α}∪
{node(x, node(y , empty , empty ), node(z, empty , empty )) | x, y , z ∈ α} ∪ ..
Programming Languages/Values and Types
Recursive Types
General Recursive Types

Tree α = empty + node α × Treeα × Treeα


Tree α = {empty } ∪ {node(x, empty , empty ) | x ∈ α}∪
{node(x, node(y , empty , empty ), empty ) | x, y ∈ α}∪
{node(x, empty , node(y , empty , empty )) | x, y ∈ α}∪
{node(x, node(y , empty , empty ), node(z, empty , empty )) | x, y , z ∈ α} ∪ ..

C++ (pointers and template definition)


template < class Alpha >
struct T r e e {
Alpha x;
T r e e * l e f t ,* r i g h t ;
} root ;
Programming Languages/Values and Types
Recursive Types
General Recursive Types

Tree α = empty + node α × Treeα × Treeα


Tree α = {empty } ∪ {node(x, empty , empty ) | x ∈ α}∪
{node(x, node(y , empty , empty ), empty ) | x, y ∈ α}∪
{node(x, empty , node(y , empty , empty )) | x, y ∈ α}∪
{node(x, node(y , empty , empty ), node(z, empty , empty )) | x, y , z ∈ α} ∪ ..

C++ (pointers and template definition)


template < class Alpha >
struct T r e e {
Alpha x;
T r e e * l e f t ,* r i g h t ;
} root ;

Haskell
data T r e e a l p h a = Empty |
Node ( a l p h a , T r e e a l p h a , T r e e a l p h a )

x = Node (1 , Node (2 , Empty , Empty) , Node (3 , Empty , Empty ))


y = Node (3 , Empty , Empty)
Programming Languages/Values and Types
Recursive Types
Strings

Strings

Language design choice:


1 Primitive type (ML):
Language keeps an internal table of strings

Design choice affects the complexity and efficiency of:


concatenation, assignment, equality, lexical order,
decomposition
Programming Languages/Values and Types
Recursive Types
Strings

Strings

Language design choice:


1 Primitive type (ML):
Language keeps an internal table of strings
2 Character array (C, Pascal, ...)

Design choice affects the complexity and efficiency of:


concatenation, assignment, equality, lexical order,
decomposition
Programming Languages/Values and Types
Recursive Types
Strings

Strings

Language design choice:


1 Primitive type (ML):
Language keeps an internal table of strings
2 Character array (C, Pascal, ...)
3 Character list (Haskell, Prolog, Lisp)

Design choice affects the complexity and efficiency of:


concatenation, assignment, equality, lexical order,
decomposition
Programming Languages/Values and Types
Type Systems

Type Systems

Types are required to provide data processing, integrity


checking, efficiency, access controls. Type compatibility on
operators is essential.
Programming Languages/Values and Types
Type Systems

Type Systems

Types are required to provide data processing, integrity


checking, efficiency, access controls. Type compatibility on
operators is essential.
Simple bugs can be avoided at compile time.
Programming Languages/Values and Types
Type Systems

Type Systems

Types are required to provide data processing, integrity


checking, efficiency, access controls. Type compatibility on
operators is essential.
Simple bugs can be avoided at compile time.
Irrelevant operations:
y=true * 12;
x=12; x[1]=6;
y=5; x.a = 4;
Programming Languages/Values and Types
Type Systems

Type Systems

Types are required to provide data processing, integrity


checking, efficiency, access controls. Type compatibility on
operators is essential.
Simple bugs can be avoided at compile time.
Irrelevant operations:
y=true * 12;
x=12; x[1]=6;
y=5; x.a = 4;
When to do type checking? Latest time is before the
operation. Two options:
Programming Languages/Values and Types
Type Systems

Type Systems

Types are required to provide data processing, integrity


checking, efficiency, access controls. Type compatibility on
operators is essential.
Simple bugs can be avoided at compile time.
Irrelevant operations:
y=true * 12;
x=12; x[1]=6;
y=5; x.a = 4;
When to do type checking? Latest time is before the
operation. Two options:
1 Compile time → static type checking
Programming Languages/Values and Types
Type Systems

Type Systems

Types are required to provide data processing, integrity


checking, efficiency, access controls. Type compatibility on
operators is essential.
Simple bugs can be avoided at compile time.
Irrelevant operations:
y=true * 12;
x=12; x[1]=6;
y=5; x.a = 4;
When to do type checking? Latest time is before the
operation. Two options:
1 Compile time → static type checking
2 Run time → dynamic type checking
Programming Languages/Values and Types
Type Systems
Static Type Checking

Static Type Checking

Compile time type information is used to do type checking.


Programming Languages/Values and Types
Type Systems
Static Type Checking

Static Type Checking

Compile time type information is used to do type checking.


All incompatibilities are resolved at compile time. Variables
have a fixed time during their lifetime.
Programming Languages/Values and Types
Type Systems
Static Type Checking

Static Type Checking

Compile time type information is used to do type checking.


All incompatibilities are resolved at compile time. Variables
have a fixed time during their lifetime.
Most languages do static type checking
Programming Languages/Values and Types
Type Systems
Static Type Checking

Static Type Checking

Compile time type information is used to do type checking.


All incompatibilities are resolved at compile time. Variables
have a fixed time during their lifetime.
Most languages do static type checking
User defined constants, variable and function types:
Programming Languages/Values and Types
Type Systems
Static Type Checking

Static Type Checking

Compile time type information is used to do type checking.


All incompatibilities are resolved at compile time. Variables
have a fixed time during their lifetime.
Most languages do static type checking
User defined constants, variable and function types:
Strict type checking. User has to declare all types (C, C++,
Fortran,...)
Programming Languages/Values and Types
Type Systems
Static Type Checking

Static Type Checking

Compile time type information is used to do type checking.


All incompatibilities are resolved at compile time. Variables
have a fixed time during their lifetime.
Most languages do static type checking
User defined constants, variable and function types:
Strict type checking. User has to declare all types (C, C++,
Fortran,...)
Languages with type inference (Haskell, ML, Scheme...)
Programming Languages/Values and Types
Type Systems
Static Type Checking

Static Type Checking

Compile time type information is used to do type checking.


All incompatibilities are resolved at compile time. Variables
have a fixed time during their lifetime.
Most languages do static type checking
User defined constants, variable and function types:
Strict type checking. User has to declare all types (C, C++,
Fortran,...)
Languages with type inference (Haskell, ML, Scheme...)
No type operations after compilation. All issues are resolved.
Direct machine code instructions.
Programming Languages/Values and Types
Type Systems
Dynamic Type Checking

Dynamic Type Checking

Run-time type checking. No checking until the operation is to


be executed.
Programming Languages/Values and Types
Type Systems
Dynamic Type Checking

Dynamic Type Checking

Run-time type checking. No checking until the operation is to


be executed.
Interpreted languages like Lisp, Prolog, PHP, Perl, Python.
Programming Languages/Values and Types
Type Systems
Dynamic Type Checking

Dynamic Type Checking

Run-time type checking. No checking until the operation is to


be executed.
Interpreted languages like Lisp, Prolog, PHP, Perl, Python.
A hypothetical language:
int whichmonth ( i n p u t ) {
if ( i s i n t e g e r ( i n p u t )) return i n p u t ;
else if ( i s s t r i n g ( i n p u t ))
switch ( i n p u t ) {
case " January " : return 1;
case " February " : return 2;
...
case " December " : return 12;}
}
...
read ( input ) /* user input at run time ? */
ay = whichmonth ( i n p u t )
Programming Languages/Values and Types
Type Systems
Dynamic Type Checking

Run time decision based on users choice is possible.


Has to carry type information along with variable at run time.
Type of a variable can change at run-time (depends on the
language).
Programming Languages/Values and Types
Type Systems
Dynamic Type Checking

Static vs Dynamic Type Checking

Static type checking is faster. Dynamic type checking does


type checking before each operation at run time. Also uses
extra memory to keep run-time type information.
Static type checking is more restrictive meaning safer. Bugs
avoided at compile time, earlier is better.
Dynamic type checking is less restrictive meaning more
flexible. Operations working on dynamic run-time type
information can be defined.
Programming Languages/Values and Types
Type Systems
Type Equality

Type Equality
?
S ≡ T How to decide?
Programming Languages/Values and Types
Type Systems
Type Equality

Type Equality
?
S ≡ T How to decide?
Name Equivalence: Types should be defined at the same exact
place.
Programming Languages/Values and Types
Type Systems
Type Equality

Type Equality
?
S ≡ T How to decide?
Name Equivalence: Types should be defined at the same exact
place.
Structural Equivalence: Types should have same value set.
(mathematical set equality).
Programming Languages/Values and Types
Type Systems
Type Equality

Type Equality
?
S ≡ T How to decide?
Name Equivalence: Types should be defined at the same exact
place.
Structural Equivalence: Types should have same value set.
(mathematical set equality).
Most languages use name equivalence.
Programming Languages/Values and Types
Type Systems
Type Equality

Type Equality
?
S ≡ T How to decide?
Name Equivalence: Types should be defined at the same exact
place.
Structural Equivalence: Types should have same value set.
(mathematical set equality).
Most languages use name equivalence.
C example:
typedef struct Comp { double x , y ;} Complex ;
struct COMP { double x , y ; };

struct Comp a ;
Complex b;
struct COMP c ;

/* ... */
a =b; /* Valid , equal types */
a=c; /* Compile error , i n c o m p a t i b l e types */
Programming Languages/Values and Types
Type Systems
Type Equality

Structural Equality

S ≡ T if and only if:


1 S and T are primitive types and S = T (same type),
2 if S = A × B, T = A′ × B ′ , A ≡ A′ , and B ≡ B ′ ,
3 if S = A + B, T = A′ + B ′ , and (A ≡ A′ and B ≡ B ′ ) or
(A ≡ B ′ and B ≡ A′ ),
4 if S = A 7→ B, T = A′ 7→ B ′ , A ≡ A′ and B ≡ B ′ ,
5 if S = P(A), T = P(A′ ), and A ≡ A′ .
Otherwise S 6≡ T
Programming Languages/Values and Types
Type Systems
Type Equality

Harder to implement structural equality. Especially recursive


cases.
Programming Languages/Values and Types
Type Systems
Type Equality

Harder to implement structural equality. Especially recursive


cases.
T = {nil} + A × T , T ′ = {nil} + A × T ′
Programming Languages/Values and Types
Type Systems
Type Equality

Harder to implement structural equality. Especially recursive


cases.
T = {nil} + A × T , T ′ = {nil} + A × T ′
Programming Languages/Values and Types
Type Systems
Type Equality

Harder to implement structural equality. Especially recursive


cases.
T = {nil} + A × T , T ′ = {nil} + A × T ′
T = {nil} + A × T ′ , T ′ = {nil} + A × T
struct Circle { double x,y,a;};
struct Square { double x,y,a;};
Two types have a semantical difference. User errors may need
less tolerance in such cases.
Programming Languages/Values and Types
Type Systems
Type Equality

Harder to implement structural equality. Especially recursive


cases.
T = {nil} + A × T , T ′ = {nil} + A × T ′
T = {nil} + A × T ′ , T ′ = {nil} + A × T
struct Circle { double x,y,a;};
struct Square { double x,y,a;};
Two types have a semantical difference. User errors may need
less tolerance in such cases.
Automated type conversion is a different concept. Does not
necessarily conflicts with name equivalence.
enum Day {Mon, Tue , Wed, Thu, F r i , Sat , Sun } x ;
x =3;
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Function parameter
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Function parameter
Take part in compositions
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Function parameter
Take part in compositions
Return value from a function
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Function parameter
Take part in compositions
Return value from a function
Most imperative languages (Pascal, Fortran) classify functions
as second order value. (C represents function names as
pointers)
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Function parameter
Take part in compositions
Return value from a function
Most imperative languages (Pascal, Fortran) classify functions
as second order value. (C represents function names as
pointers)
Functions are first order values in most functional languages
like Haskell and Scheme .
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Function parameter
Take part in compositions
Return value from a function
Most imperative languages (Pascal, Fortran) classify functions
as second order value. (C represents function names as
pointers)
Functions are first order values in most functional languages
like Haskell and Scheme .
Arrays, structures (records)?
Programming Languages/Values and Types
Type Completeness

Type Completeness

First order values:


Assignment
Function parameter
Take part in compositions
Return value from a function
Most imperative languages (Pascal, Fortran) classify functions
as second order value. (C represents function names as
pointers)
Functions are first order values in most functional languages
like Haskell and Scheme .
Arrays, structures (records)?
Type completeness principle: First order values should take
part in all operations above, no arbitrary restrictions should
exist.
Programming Languages/Values and Types
Type Completeness

C Types:
Primitive
√ Array Struct
√ Func.
Assignment √ × √ ×
Function parameter √ × √ ×
Function return √ ×
√ √ ×
In compositions ×

Haskell Types:
Primitive
√ Array
√ Struct
√ Func.

Variable definition √ √ √ √
Function parameter √ √ √ √
Function return √ √ √ √
In compositions

Pascal Types:
Primitive
√ Array
√ Struct.
√ Func.
Assignment √ √ √ ×
Function parameter √ ×
Function return √ ×
√ ×
√ ×
In compositions ×
Programming Languages/Values and Types
Expressions

Expressions

Program segments that gives a value when evaluated:


Literals
Variable and constant access
Aggregates
Variable references
Function calls
Conditional expressions
Iterative expressions (Haskell)
Programming Languages/Values and Types
Expressions
Literals/Variable and Constant Access

Literals/Variable and Constant Access

Literals: Constants with same value with their notation


123, 0755, 0xa12, 12451233L, -123.342,
-1.23342e-2, ’c’, ’\021’, "ayse", True, False
Variable and constant access: User defined constants and
variables give their content when evaluated.
int x;
#define pi 3.1416
x=pi*r*r
Programming Languages/Values and Types
Expressions
Aggregates

Aggregates

Used to compose composite values lexically.


x =(12 , " ali " , True ) {-- 3 Tuple - -}
y ={ name= " ali " , no =12} {-- record - -}
f =\ x -> x * x {-- f u n c t i o n - -}
l =[1 ,2 ,3 ,4] {-- r e c u r s i v e type , list - -}

C only has aggregates at


definition. There is no aggregates in the executable expressions!
struct P e r s o n { char name [20] , int no } p = { " Ali Cin " , 332314
double d i z i [3][2] = {{0 ,1} , {1.2 ,4} , {12 , 1.4}};
p ={ " Veli Cin " ,123412}; × /* not p o s s i b l e ! */
Programming Languages/Values and Types
Expressions
Variable References

Variable References

Variable access vs variable reference


value vs l-value
pointers are not references! You can use pointers as references
with special operators.
Some languages regard references like first order values (Java,
C++ partially)
Some languages distinguish the reference from the content of
the variable (Unix shells, ML)
Programming Languages/Values and Types
Expressions
Function Calls

Function Calls

F (Gp1 , Gp2 , ..., Gpn )


Function name followed by actual parameter list. Function is
called, executed and the returned value is substituted in the
expression position.
Actual parameters: parameters send in the call
Formal parameters: parameter names used in function
definition
Operators can be considered as function calls. The difference
is the infix notation.
⊕(a, b) vs a ⊕ b
languages has built-in mechanisms for operators. Some
languages allow user defined operators (operator overloading):
C++, Haskell.
Programming Languages/Values and Types
Expressions
Conditional Expressions

Conditional Expressions

Evaluate to different values based on a condition.


Programming Languages/Values and Types
Expressions
Conditional Expressions

Conditional Expressions

Evaluate to different values based on a condition.


Haskell: if condition then exp1 else exp2 .
case value of p1 -> exp1 ; p2 -> exp2 ...
Programming Languages/Values and Types
Expressions
Conditional Expressions

Conditional Expressions

Evaluate to different values based on a condition.


Haskell: if condition then exp1 else exp2 .
case value of p1 -> exp1 ; p2 -> exp2 ...
C: (condition )?exp1 :exp2 ;
Programming Languages/Values and Types
Expressions
Conditional Expressions

Conditional Expressions

Evaluate to different values based on a condition.


Haskell: if condition then exp1 else exp2 .
case value of p1 -> exp1 ; p2 -> exp2 ...
C: (condition )?exp1 :exp2 ;
if .. else in C is not conditional expression but
conditional statement. No value when evaluated!
x = (a >b )? a :b;
y = (( a >b )? s i n : c o s )( x ); /* Does it work ? try y o u r s e l f ... *
Programming Languages/Values and Types
Expressions
Conditional Expressions

Haskell:
x = if (a >b) then a else b
y = ( if (a >b) then (+) else (*)) x y
data Day = Mon | Tue | Wed | Thu | F r i | S a t | Sun
c o n v e r t a = case a of
Left ( x , r e s t ) -> x : ( c o n v e r t r e s t )
Empty -> []
daynumber g = case g of
Mon -> 1
Tue -> 2
...
Sun -> 7

case checks for a pattern and evaluate the RHS expression


with substituting variables according to pattern at LHS.
Programming Languages/Values and Types
Expressions
Iterative Expressions

Iterative Expressions

Expressions that do a group of operations on elements of a list


or data structure, and returns a value.
[ expr | variable <- list , condition ]
Similar to set notation in math:
{expr |var ∈ list, condition}

x =[1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12]


y =[ a *2 | a <- x ] { - - [2 ,4 ,6 ,8 ,...24 ] - -}
z =[ a | a <- x , mod a 3 == 1 ] { - - [1 ,4 ,7 ,10] - -}
Programming Languages/Values and Types
Summary

Summary

Value and type


Primitive types
Composite types
Recursive types
When to type check
How to type check
Expressions

You might also like