Programming Languages
Values and Types
Onur Tolga Şehitoğlu
Computer Engineering, METU
ODTÜ
METU
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Outline
1 Value and Type Static Type Checking
2 Primitive vs Composite Types Dynamic Type Checking
Type Equality
3 Cartesian Product
9 Type Completeness
4 Disjoint Union
10 Expressions
5 Mappings Literals/Variable and
Arrays Constant Access
Functions Aggregates
6 Powerset Variable References
7 Recursive Types Function Calls
Lists Conditional Expressions
General Recursive Types Iterative Expressions
Strings Block Expressions
8 Type Systems 11 Summary
METU Computer Engineering Programming Languages / Values and Types 2 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 3 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 3 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Haskell types
Bool, Int, Float, ...
Char, String
tuples,(N-tuples), records
lists
functions
Each type represents a set of values. Is that enough?
METU Computer Engineering Programming Languages / Values and Types 4 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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’}
METU Computer Engineering Programming Languages / Values and Types 4 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 4 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
Python: bool, int, float, str, functions
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?
METU Computer Engineering Programming Languages / Values and Types 5 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
Python: bool, int, float, str, functions
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?
METU Computer Engineering Programming Languages / Values and Types 5 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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)
METU Computer Engineering Programming Languages / Values and Types 6 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Composite Datatypes
User defined types with composition of one or more other
datatypes. Depending on composition type:
Cartesian Product (struct, tuples, records)
METU Computer Engineering Programming Languages / Values and Types 7 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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))
METU Computer Engineering Programming Languages / Values and Types 7 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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)
METU Computer Engineering Programming Languages / Values and Types 7 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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))
METU Computer Engineering Programming Languages / Values and Types 7 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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)
METU Computer Engineering Programming Languages / Values and Types 7 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ) =
METU Computer Engineering Programming Languages / Values and Types 8 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 8 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 = ( " Osman Hamdi " ,23141):: P e o p l e
in Python: string × int
x = ( " Osman Hamdi " , 23141)
type ( x )
< type ’ tuple ’ >
METU Computer Engineering Programming Languages / Values and Types 9 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 " )
Python: str × int × float × str
x = ( " Osman Hamdi " ,23141 ,3.98 , " Yazar " )
METU Computer Engineering Programming Languages / Values and Types 10 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
Python: () . None used for no value.
METU Computer Engineering Programming Languages / Values and Types 11 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 • right 3
•3
•2 + = • left 2 • right 4
•4
•3 • left 3
#(S + T ) =
METU Computer Engineering Programming Languages / Values and Types 12 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 • right 3
•3
•2 + = • left 2 • right 4
•4
•3 • left 3
#(S + T ) =#S + #T
C union’s are disjoint union?
METU Computer Engineering Programming Languages / Values and Types 12 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 - -}
METU Computer Engineering Programming Languages / Values and Types 13 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 Each color is a value in the
•a mapping. Other 6 values are
•2 not drawn
•b
•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 ) =
METU Computer Engineering Programming Languages / Values and Types 14 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 Each color is a value in the
•a mapping. Other 6 values are
•2 not drawn
•b
•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
METU Computer Engineering Programming Languages / Values and Types 14 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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;
METU Computer Engineering Programming Languages / Values and Types 15 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 16 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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)
METU Computer Engineering Programming Languages / Values and Types 17 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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] ; × →
→
METU Computer Engineering Programming Languages / Values and Types 18 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 19 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Powerset
P(S) = {T | T ⊆ S}
The set of all subsets
•∅ •{3} •{2, 3}
•a
S= P(S) = •{1} •{1, 2} •{1, 2, 3}
•b
•{2} •{1, 3}
#P(S) =
METU Computer Engineering Programming Languages / Values and Types 20 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Powerset
P(S) = {T | T ⊆ S}
The set of all subsets
•∅ •{3} •{2, 3}
•a
S= P(S) = •{1} •{1, 2} •{1, 2, 3}
•b
•{2} •{1, 3}
#P(S) =2#S
METU Computer Engineering Programming Languages / Values and Types 20 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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++ and Python implemented as class.
METU Computer Engineering Programming Languages / Values and Types 21 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ))), ...}
METU Computer Engineering Programming Languages / Values and Types 22 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
C lists: pointer based. Not actual recursion.
struct L i s t {
int x ;
L i s t * next ;
} a;
METU Computer Engineering Programming Languages / Values and Types 23 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 , [] - -}
METU Computer Engineering Programming Languages / Values and Types 23 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Polymorphic lists: a single definition defines lists of many
types.
METU Computer Engineering Programming Languages / Values and Types 24 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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] -
METU Computer Engineering Programming Languages / Values and Types 24 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 00 , Left(15.23, Empty ) ∈ List α ?
METU Computer Engineering Programming Languages / Values and Types 24 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 00 , Left(15.23, Empty ) ∈ List α ?
METU Computer Engineering Programming Languages / Values and Types 24 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 00 , Left(15.23, Empty ) ∈ List α ? No.
Most languages only permits homogeneous lists.
METU Computer Engineering Programming Languages / Values and Types 24 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Haskell Lists
binary operator “:” for list construction:
data [alpha] = (alpha : [alpha]) | []
METU Computer Engineering Programming Languages / Values and Types 25 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Haskell Lists
binary operator “:” for list construction:
data [alpha] = (alpha : [alpha]) | []
x = (1:(2:(3:[])))
METU Computer Engineering Programming Languages / Values and Types 25 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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":[])
METU Computer Engineering Programming Languages / Values and Types 25 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
General Recursive Types
T = ...T ...
METU Computer Engineering Programming Languages / Values and Types 26 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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!
METU Computer Engineering Programming Languages / Values and Types 26 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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?
METU Computer Engineering Programming Languages / Values and Types 26 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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?
METU Computer Engineering Programming Languages / Values and Types 26 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 26 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 26 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ∈ α} ∪ ..
METU Computer Engineering Programming Languages / Values and Types 27 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ;
METU Computer Engineering Programming Languages / Values and Types 27 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 )
METU Computer Engineering Programming Languages / Values and Types 27 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Strings
Language design choice:
1 Primitive type (ML, Python):
Language keeps an internal table of strings
Design choice affects the complexity and efficiency of:
concatenation, assignment, equality, lexical order,
decomposition
METU Computer Engineering Programming Languages / Values and Types 28 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Strings
Language design choice:
1 Primitive type (ML, Python):
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
METU Computer Engineering Programming Languages / Values and Types 28 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Strings
Language design choice:
1 Primitive type (ML, Python):
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
METU Computer Engineering Programming Languages / Values and Types 28 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Systems
Types are required to provide data processing, integrity
checking, efficiency, access controls. Type compatibility on
operators is essential.
METU Computer Engineering Programming Languages / Values and Types 29 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 29 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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;
METU Computer Engineering Programming Languages / Values and Types 29 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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:
METU Computer Engineering Programming Languages / Values and Types 29 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 29 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 29 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Static Type Checking
Compile time type information is used to do type checking.
METU Computer Engineering Programming Languages / Values and Types 30 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 30 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 30 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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:
METU Computer Engineering Programming Languages / Values and Types 30 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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,...)
METU Computer Engineering Programming Languages / Values and Types 30 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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...)
METU Computer Engineering Programming Languages / Values and Types 30 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 30 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Dynamic Type Checking
Run-time type checking. No checking until the operation is to
be executed.
METU Computer Engineering Programming Languages / Values and Types 31 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Dynamic Type Checking
Run-time type checking. No checking until the operation is to
be executed.
Interpreted languages like Lisp, Prolog, PHP, Perl, Python.
METU Computer Engineering Programming Languages / Values and Types 31 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Dynamic Type Checking
Run-time type checking. No checking until the operation is to
be executed.
Interpreted languages like Lisp, Prolog, PHP, Perl, Python.
Python:
def whichmonth ( i n p ):
if isinstance ( i n p , int ):
return i n p
elif isinstance ( i n p , str ):
if i n p == " January " :
return 1
elif i n p == " February " :
return 2
....
elif i n p == " December " :
return 12
...
i n p = input () /* u s e r input a t r u n t i m e ? */
month = whichmonth ( i n p )
METU Computer Engineering Programming Languages / Values and Types 31 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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).
METU Computer Engineering Programming Languages / Values and Types 32 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 33 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Equality
?
S ≡ T How to decide?
METU Computer Engineering Programming Languages / Values and Types 34 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Equality
?
S ≡ T How to decide?
Name Equivalence: Types should be defined at the same exact
place.
METU Computer Engineering Programming Languages / Values and Types 34 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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).
METU Computer Engineering Programming Languages / Values and Types 34 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 34 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 */
METU Computer Engineering Programming Languages / Values and Types 34 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 = A0 × B 0 , A ≡ A0 , and B ≡ B 0 ,
3 if S = A + B, T = A0 + B 0 , and (A ≡ A0 and B ≡ B 0 ) or
(A ≡ B 0 and B ≡ A0 ),
4 if S = A 7→ B, T = A0 7→ B 0 , A ≡ A0 and B ≡ B 0 ,
5 if S = P(A), T = P(A0 ), and A ≡ A0 .
Otherwise S 6≡ T
METU Computer Engineering Programming Languages / Values and Types 35 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Harder to implement structural equality. Especially recursive
cases.
METU Computer Engineering Programming Languages / Values and Types 36 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Harder to implement structural equality. Especially recursive
cases.
T = {nil} + A × T , T 0 = {nil} + A × T 0
METU Computer Engineering Programming Languages / Values and Types 36 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Harder to implement structural equality. Especially recursive
cases.
T = {nil} + A × T , T 0 = {nil} + A × T 0
METU Computer Engineering Programming Languages / Values and Types 36 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Harder to implement structural equality. Especially recursive
cases.
T = {nil} + A × T , T 0 = {nil} + A × T 0
T = {nil} + A × T 0 , T 0 = {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.
METU Computer Engineering Programming Languages / Values and Types 36 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Harder to implement structural equality. Especially recursive
cases.
T = {nil} + A × T , T 0 = {nil} + A × T 0
T = {nil} + A × T 0 , T 0 = {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;
METU Computer Engineering Programming Languages / Values and Types 36 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Completeness
First order values:
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Completeness
First order values:
Assignment
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Completeness
First order values:
Assignment
Function parameter
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Completeness
First order values:
Assignment
Function parameter
Take part in compositions
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Type Completeness
First order values:
Assignment
Function parameter
Take part in compositions
Return value from a function
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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)
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 .
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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)?
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 37 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ×
METU Computer Engineering Programming Languages / Values and Types 38 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Expressions
Program segments that gives a value when evaluated:
Literals
Variable and constant access
Aggregates
Variable references
Function calls
Conditional expressions
Iterative expressions (Haskell)
METU Computer Engineering Programming Languages / Values and Types 39 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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
METU Computer Engineering Programming Languages / Values and Types 40 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Aggregates
Used to construct composite values without any
declaration/definition. Haskell:
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 - -}
Python:
x = (12 , " ali " , True )
y = [ 1 , 2 , [2 , 3] , " a " ]
z = { ’ name ’: ’ ali ’ , ’ no ’: ’ 12 ’}
f = lambda x : x +1
METU Computer Engineering Programming Languages / Values and Types 41 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Ansi C has aggregates
only at the definition. There is no aggregates in the statements!
struct P e r s o n { char name [20] , int no };
struct P e r s o n p = { " Ali Cin " , 332314};
double a r r [3][2] = {{0 ,1} , {1.2 ,4} , {12 , 1.4}};
p ={ " Veli Cin " ,123412}; × /* not p o s s i b l e in ANSI C ! */
C99 Compound literals allow array and structure aggragates
int (* a r r )[2]; √
a r r = {{0 , 1} , {1.2 ,4} , {12 , 1.4}}; √
p = ( struct p e r s o n ) { " Veli Cin " ,123412}; /* C99 */
C++11 has function aggragetes (lambda)
void s o r t ( int a [] , int n , (* f )( int , int )) {
...
}
auto f = []( int a ) { return a +1;} ;
...
s o r t ( a r r , n , []( int a , int b) { return a -b ;});
n = f (n)
METU Computer Engineering Programming Languages / Values and Types 42 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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)
METU Computer Engineering Programming Languages / Values and Types 43 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 44 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Conditional Expressions
Evaluate to different values based on a condition.
METU Computer Engineering Programming Languages / Values and Types 45 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Conditional Expressions
Evaluate to different values based on a condition.
Haskell: if condition then exp1 else exp2 .
case value of p1 -> exp1 ; p2 -> exp2 ...
METU Computer Engineering Programming Languages / Values and Types 45 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ;
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 ... *
METU Computer Engineering Programming Languages / Values and Types 45 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ;
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 ... *
Python: exp1 if condition else exp2
METU Computer Engineering Programming Languages / Values and Types 45 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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 ;
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 ... *
Python: exp1 if condition else exp2
if .. else in C is not conditional expression but
conditional statement. No value when evaluated!
METU Computer Engineering Programming Languages / Values and Types 45 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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.
METU Computer Engineering Programming Languages / Values and Types 46 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
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}
Haskell:
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] - -}
Python:
x = [1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12]
y = [ a *2 for a in x ] # [2 ,4 ,6 ,8 ,...24 ]
z = [ a for a in x if a % 3 == 1 ] # [1 ,4 ,7 ,10]
METU Computer Engineering Programming Languages / Values and Types 47 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Block Expressions
Some languages allow multiple/statements in a block to
calculate a value.
GCC extension for compound statement expressions:
double s , i , a r r [10];
s = ( { double t = 0;
for ( i = 0; i < 10; i ++)
t += a r r [ i ];
t ;}) + 1;
Value of the last expression is the value of the block.
ML has similar block expression syntax.
This allows arbitrary computation for evaluation of the
expression.
METU Computer Engineering Programming Languages / Values and Types 48 / 49
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Summary
Value and type
Primitive types
Composite types
Recursive types
When to type check
How to type check
Expressions
METU Computer Engineering Programming Languages / Values and Types 49 / 49