Professional Documents
Culture Documents
CNG242 Lecture2 Types Values
CNG242 Lecture2 Types Values
{false, true} is a type because the operations not, and, or operate uniformly over the values
false and true.
{...,-2, -1, 0, +1, +2,...} is a type because operations such as addition and multiplication
• Values in a type should exhibit a similar behaviour.
operate uniformly over all these values.
{13, true,
• The sameMonday}. is not
group of operations abetype,
should definedsince
on them.there are no useful operations over this set of
values
• Type: a set of values, equipped operations that can be applied uniformly to all these values
Primitive types
• Primitive Types: Values that cannot be decomposed into other sub values.
– C: int, float, double, char, long, short, pointer
– 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 (ISO LATIN character set)
– #char = 216 (UNICODE character set), #short = 216, #int = 232
• Not all languages have the same primitives
• Some languages does not have a distinct type for Boolean or Character (e.g., C++ has a type named bool, but its values are
just small integers; there is a convention that zero represents false and any other integer represents true, similar for char)
• Some languages provide several integer types : byte, short
Primitive types (Example)
• Need 2 variable to store “population of the country (294,906)” and “population of the world (7.046 billion)”
• In C what would you choose?
short
• Real world –32,768
problem: spacecraft Ariane5 (1996) is lost due to to 32,767
integer overflow!
– Resulting $370 Million loss
– data int –2,147,483,648
conversion from a 64-bit floating point numberto
to a2,147,483,647
16-bit signed integer value
https://www.youtube.com/watch?v=5tJPXYA0Nec
User defined primitive types
• enumerated types
– e.g. C enum days {mon, tue, wed, thu, fri, sat, sun};
– enum months {jan, feb, mar, apr, .... };
– enum days today; /*usage as var*/
• e.g. Java
– public enum Currency {PENNY, NICKLE, DIME, QUARTER};
– Currency coin = Currency.PENNY;
• Most programming languages allow only integers to be used for counting and array indexing.
– C: Every ordinal type is an alias for integers.
– Pascal, Ada: distinct types
#S x #T
(a,1) is a pair. if there were N sets, then the elements will be n-tuple
Cartesian product (2)
struct Person{
char[20] name;
int no;
Month m;} x={“Dennis Ritche”, 194183, May};
Cartesian product (4)
Homogeneous Cartesian product
Disjoint union
• In Haskell, algebraic types are used for
disjoint unions
Arrays Functions
#(S x T) = #S x #T
• S={empty}+ (int x S)
• T= ….T….
• The cardinality of recursive types are infinite
• Let S=int x S , a=Left (1,Left(2,a))
– Is a ∈ S? Yes
The value of a is [1,2,1,2,1,2……]
– Some languages like Haskell lets you define such values.
Infinite lists
– Most languages allow only a subset of S, the subset of
finite values.
Tree – a recursive type example
Tree – a recursive type example
• Tree is an abstract data type
•Haskell
• Typical operations:
– Length, equality, lexicographic comparison, character
selection, substring selection, concatenation
• Language design choice:
– Character array (C, C++,Pascal)
– Character list (Haskell, Lisp, Prolog)
– Primitive type Meta Language (ML). There is an internal
table of strings
– Objects (Java)
• Choice effects the complexity and efficiency
TYPE SYSTEMS
Type systems
•Types are required to provide data processing integrity
checking, efficiency, access controls
•Type compatibility on operators is essential
• a PL’s type system groups values into types
– Helps us to describe data effectively
– Prevent nonsensical operations
• y=true * 12;
• x=12; x[1]=6;
• x=5; x.a = 4
• Simple bugs can be avoided at compile time
– Type checking
When to do type checking?
1. Compile time
– Static type checking
– Statically typed languages
2. Run time
– Dynamic type checking
– Dynamically typed languages
• The latest time to type check is the time just before the
operation.
Static type checking
• Compile time type information is used to do type checking.
• All incompatibilities are resolved at compile time.
bool even (int n){ return n%2==0;}
– Although compiler does not know the value of n, it knows that this
def month(m):
struct Point x;
Point2D y;
x=y; //valid. Name equivalence
2. Structural equivalence
– Assignment
– Function parameter
– Take part in compositions
– Return value from a function
expression
• Expressions:
– Literals
– Variable and constant access
– Aggregate
– Variable references
– Function calls
– Conditional expr
– Iterative expr (in Haskell)
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
• r-value vs l-value
• Expressions that refer to memory locations are called "l-
value" expressions.
– An l-value represents a storage region's "locator"
value, or a "left" value of an assignment
• R-value : value of an expression
– Not all r-values are l-values, but all l-values are r-
values
• Example: b=a
– evaluate a (variable access) : r-value
– assign that r-value to the location value of b (l-
value)
Example: C
Any of these can be an l-value expression:
• 𝐹(𝐺𝑝1𝐺𝑝2, … , 𝐺𝑝𝑛)
• 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.
Conditional Expressions
x = (a >b)? a:b;
y = ((a >b)? s i n : cos )( x ); /* Does it work ? Try */
expression
Conditional Expression (cont) Haskell
-- maxi [3,7,8] is
--max 3 ( maxi [7,8]= max 7 maxi(8))
Some examples (2)