Ceng242 SL Valuesandtypes
Ceng242 SL Valuesandtypes
ODTÜ
METU
Value and Type Primitive vs Composite Types Cartesian Product Disjoint Union Mappings Powerset Recursive Types Type Sy
Outline
Haskell types
Bool, Int, Float, ...
Char, String
tuples,(N-tuples), records
lists
functions
Each type represents a set of values. Is that enough?
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’}
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.
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)
Composite Datatypes
Composite Datatypes
Composite Datatypes
Composite Datatypes
Composite Datatypes
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 ) =
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
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.
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 ) =
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?
C: int + double:
union number { double r e a l ; int i n t e g e r ; } x ;
Mappings
Mappings
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;
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
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)
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] ; × →
→
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
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) =
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
Recursive Types
S = ...S...
Types including themselves in composition.
Lists
S = Int × S + {null}
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 ))), ...}
Haskell lists.
data List = Left ( Int , List ) | Empty
Haskell Lists
Haskell Lists
Haskell Lists
T = ...T ...
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!
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?
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?
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.
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.
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 )
Strings
Strings
Strings
Type Systems
Type Systems
Type Systems
Type Systems
Type Systems
Type Systems
Type Equality
?
S ≡ T How to decide?
Type Equality
?
S ≡ T How to decide?
Name Equivalence: Types should be defined at the same exact
place.
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).
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.
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 */
Structural Equality
Type Completeness
Type Completeness
Type Completeness
Type Completeness
Type Completeness
Type Completeness
Type Completeness
Type Completeness
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 ×
Expressions
Aggregates
Python:
x = (12 , " ali " , True )
y = [ 1 , 2 , [2 , 3] , " a " ]
z = { ’ name ’: ’ ali ’ , ’ no ’: ’ 12 ’}
f = lambda x : x +1
Variable References
Function Calls
Conditional Expressions
Conditional Expressions
Conditional Expressions
Conditional 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
Iterative Expressions
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]
Block Expressions
Summary