02-Valuesandtypes PLC
02-Valuesandtypes PLC
Spring’2008
Programming Languages/Values and Types
Outline
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
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
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 ) =
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
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 ;
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;
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
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
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
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 ))), ...}
Programming Languages/Values and Types
Recursive Types
Lists
Haskell lists.
data List = Left ( Int , List ) | Empty
Haskell Lists
Haskell Lists
Haskell Lists
T = ...T ...
Programming Languages/Values and Types
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
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
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
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
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
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?
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
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 ×
Programming Languages/Values and Types
Expressions
Expressions
Aggregates
Variable References
Function Calls
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
Summary