IA010: Principles of Programming Languages

Size: px
Start display at page:

Download "IA010: Principles of Programming Languages"

Transcription

1 IA Types 1 IA010: Principles of Programming Languages 3. Types Jan Obdržálek obdrzalek@fi.muni.cz Faculty of Informatics, Masaryk University, Brno

2 Data types A data type is a collection of data values and a set of predefined operations on these values. Why use types error detection improves reliability "IQ" implicit context for many operations improves writability a + b, new p code documentation improves readability A type system consists of 1 a mechanism to define types and associate them with certain language constructs 2 set of rules for type equivalence, type compatibility and type inference IA Types 2

3 Outline IA Types 3 Primitive data types Type checking Composite data types Array types Record types Union types List types Pointer and reference types Type inference

4 Basic type taxonomy boolean type numeric types Primitive types character type character string types enumeration types, subrange types record types union types array types Composite types list types set types pointer and reference types What about primitive and composite data types? IA Types 4

5 Primitive data types IA Types 5

6 Primitive and composite types IA Types 6 primitive data types two meanings, may coincide 1 with support built-in the programming language also called built-in types 2 building blocks for composite types also called basic types composite data types created by applying a type constructor (record, array, set...) to one or more simpler types (either primitive or composite) The distinction is not always clear and may depend on the language.

7 Numeric types IA Types 7 historically oldest, typically reflect hardware integers and floats, complex numbers range can implementation-dependent (problem: portability) Integer types different lengths: C99 signed char, short, int, long, long long (using at least 1, 2, 2, 4 and 8 bytes, respectively) may be signed or unsigned (typical implementation: twos complement) arbitrary precision: string representation " L" (Python long integer) typical for scripting languages performance penalty

8 IA Types 8 Numeric types II floating-point types model real numbers single (float/real) or double (double) precision standard IEEE 754 cannot precisely express all real numbers: 1 irrational numbers: e, π,... 2 even short numbers in decimal: 0.1 (base 10) = (base 2) decimal types fixed number of decimal digits each digit the same number of bits (usually 4 or 8) use business applications, precise decimal number representation (0.1) especially useful, if available in hardware (BCD) examples: COBOL, C#, F#

9 Non-numeric primitive types IA Types 9 boolean type just two values (true, false) missing in C89, arbitrary numeric type can be used (zero/non-zero) usually implemented using more than a single bit character type to store a single character size of representation depends on the encoding used (ASCII, modern languages: Unicode, UTF-xx) (may vary for different characters e.g. in UTF-8) sometimes missing from the language (Python: strings of length 1) may be even handled like a numeric type (C)

10 Character string types IA Types 10 string a sequence of characters strings according to length static length Python, Java (String), C# limited dynamic length C (upper bound on the length) dynamic length JavaScript, Perl, standard C++ library strings according to implementation special kind of an array of characters C, C++ (terminated by the null character \0) primitive data type PYTHON class JAVA, F# supported operations concatenation, comparison, substring selection pattern matching Perl, JavaScript, Ruby, PHP

11 Ordinal (discrete) types IA Types 11 An ordinal type is a type which can be mapped to a range of integers. 1 primitive ordinal types provided in the language e.g. Java: integer, char, boolean 2 user defined ordinal types enumeration types subranges

12 User-defined ordinal types IA Types 12 Enumeration types the values, called enumeration constants, are enumerated in the definition enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; typical implementation implicit numerical value the value can often be given explicitly (Fri=2) advantages over named constants: type checking! Important aspects can one enum. constant name be used in multiple types? are enum. constants coerced to integers? (C: yes; JAVA 5.0, C#, F#: no)

13 User-defined ordinal types IA Types 13 Subrange types contiguous subsequence of an ordinal type PASCAL, ADA Example (ADA) type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); subtype Weekdays is Days range Mon..Fri; subtype Index is Integer range ; operations of the parent type are preserved (as long as the result stays in range) require run-time type checking advantages: readability, range checks can be simulated by asserts

14 Type checking IA Types 14

15 Type checking IA Types 15 topics type equivalence type compatibility type conversion (cast) type coercion nonconverting type cast type inference (later)

16 Type checking Examples Ada, Java, C# strongly typed (except for the explicit cast) Pascal almost strongly statically typed (except for the untagged variant records) C89 weak typing (unions, pointers, arrays,... ) Scheme, Lisp, ML, F# strongly typed Python, Ruby strongly dynamically typed IA Types 16 Ensuring that the operands of an operation are of compatible types. A language can be strongly typed an operation cannot be applied to any object which does not support the operation statically typed checking can be performed at compile-time dynamically typed checking performed at run-time (a form of late binding) (languages with dynamic scoping)

17 Type equivalence When are two types equivalent? Nontrivial in a language which allows defining of new types (records, arrays,... ) Two variables have the same type, if they were defined in the same declaration, or in a declaration using the same type name name equivalence: Pascal, Ada, Java, C# if their types are identical as structures structural equivalence: Algol, Modula, C, ML combination of both approaches, e.g. C: name equivalence for struct, union, enum structural equivalence otherwise IA Types 17

18 Structural equivalence issues IA Types 18 Are the following types the same? type T1 = record type s = array [1..10] of char; a,b : integer type t = array [0..9] of char; end; type T2 = record a : integer; b : integer; end; type T3 = record b : integer; a : integer; end; T1 and T2: yes T2 and T3: no (most languages), yes (ML) s and t: no (most languages), yes (Fortran, Ada)

19 Deciding type equivalence IA Types 19 Structural equivalence type names are (recursively) replaced by their definitions resulting strings are simply compared obstacles (surmountable): recursive types, pointers Name equivalence straightforward name comparison assumption: if the programmer gave two definitions of the same type, he probably had a particular use in mind

20 Name equivalence IA Types 20 TYPE new_type = old_type (* Modula-2 *) old_type and new_type are aliases two types, or two names for the same type? two types: strict name equivalence same type: loose name equivalence (Pascal) Example: strict name equivalence TYPE imperial_distance = REAL; metric_distance = REAL; VAR i : imperial_distance; m : metric_distance;... m := i; (* this should probably be an error *)

21 Name equivalence in Ada IA Types 21 a restrictive version of name type equivalence subtype a type equivalent to the parent type subtype new_int is integer; subtype small_int is integer range ; derived type a new type type imperial_distance is new float; type metric_distance is new float; note the difference: type derived_small_int is new integer range ; subtype subrange_small_int is integer range ;

22 Type conversion (cast) Change of types, explicitly stated in the program code. Implementation three principle cases: 1 structurally equivalent types (same internal representation) (no code executed the conversion is for free ) 2 different types, same representation (e.g. subtypes) (run-time check, value can be used if successful) 3 different types with related values (e.g. int vs float) (the specified conversion is performed) Nonconverting type cast no conversion is performed, the stored value is only interpreted as of the new type uses: systems programming, significand/exponent extraction,... IA Types 22

23 Type conversion examples type test_score is new integer range ; type celsius_temp is new integer;... n : integer; -- assume 32 bits r : real; -- assume IEEE double-precision t : test_score; c : celsius_temp;... t := test_score(n); -- run-time semantic check required n := integer(t); -- no check req.; every test_score is an int r := real(n); -- requires run-time conversion n := integer(r); -- requires run-time conversion and check n := integer(c); -- no run-time code required c := celsius_temp(n); -- no run-time code required IA Types 23

24 Type compatibility IA Types 24 of prime importance to the programmer full type equivalence is not always needed we often need only compatible types: addition two numeric type operands assignment target type compatible with the source type subroutine call formal parameters compatible with arguments the definition of compatibility differs significantly among various languages Coercion implicit type conversion; for compatiible types implementation similar to type cast (explicit type conversion)

25 Coercion short int s; unsigned long int l; char c; /* may be signed or unsigned */ float f; /* usually IEEE single-precision */ double d; /* usually IEEE double-precision */... s = l; /* low bits are interpreted as a signed number */ l = s; /* sign-extended, then interpreted as unsigned */ s = c; /* either sign-extended or zero-extended */ f = l; /* precision may be lost */ d = f; /* no precision lost */ f = d; /* precision may be lost, undefined possible */ IA Types 25 Coercion causes significant weakening of the type system! trends: less (or no) coercion, but... improves writability, supports abstraction today: scripting languages, C++

26 Composite data types IA Types 26

27 Array types IA Types 27 the most used and most important combined data type homogeneous aggregate of data elements elements are identified by their relative position semantically finite mappings: array_name(index) element design decisions: which types can be used for indexing? are the bounds checked on access? when are the bounds fixed? when does array allocation take place? rectangullar or ragged multidimensional arrays? initialization when allocated? what kind of slices are allowed, if any?

28 Arrays indexing IA Types 28 Which type can be used for indexing? integer types (Fortran, C,... ) any ordinal type (Ada) user-defined keys (associative arrays e.g. Python) Bound checking expensive operation, historically usually omitted (C) however common in modern languages (Java, C#, ML) Lower bounds fixed: C (0) and its successors user defined: Ada, Fortran95+ (1 by default)

29 Multidimensional arrays IA Types 29 Language interface pure multidimensional array (access: [2,3]) mat: array (1..10,1..10) of real; -- Ada array of arrays (access: [2][3]) VAR mat = ARRAY [1..10] OF ARRAY [1..10] OF REAL; Array shape rectangular array all rows of the same length jagged array length may differ between the rows (C, Java) typical for the array of arrays approach

30 Arrays slices [Scott] IA Types 30 a slice is some substructure of an array trivially a single row/column many othe options (Fortran 90):

31 Array bounds and storage bindings IA Types 31 type bounds allocation storage static static static fixed stack-dynamic static elaboration stack stack-dynamic elaboration elaboration stack fixed heap-dynamic execution execution heap heap-dynamic dynamic dynamic heap elaboration when the declaration is elaborated execution when program actually requests the array dynamic execution + can change during run-time C-family languages: C89: fixed stack-dynamic, static (static), fixed heap-dynamic (malloc/free) JAVA: fixed heap-dynamic C#: fixed heap-dynamic, heap-dynamic (List class)

32 Array type examples IA Types 32 fixed stack-dynamic array (C89) void foo() { int fixed_stack_dynamic_array[7]; /*... */ } stack-dynamic array (C99) void foo(int n) { int stack_dynamic_array[n]; /*... */ } fixed heap-dynamic array (C89) int * fixed_heap_dynamic_array = malloc(7 * sizeof(int));

33 Array implementation storage IA Types 33 Two basic choices: 1 a block of adjacent memory cells advantages: simple adressing mapping to one dimension by rows (row major) almost all languages by columns (column major) Fortran 2 array of arrays cons: may need more space pros: jagged arrays, rows can be shared Some languages support both approaches e.g. C.

34 Arrays in C IA Types 34 [Scott]

35 Record types heterogeneous (unlike arrays) model collections of related data correspond to cartesian products struct rpg_character { char name[20]; int strength, stamina, dexterity, inteligence; _Bool male; };... individual elements are often called fields access usually using the dot notation: frodo.strength nesting usually allowed C struct, C++ special version of class JAVA ordinary classes used instead IA Types 35

36 Record type implementation IA Types 36 usually consecutive memory cells may contain holes (to align with word-length) struct element { char name[2]; int atomic_number; double atomic_weight; _Bool metallic; }; Likely layout on a 32-bit machine [Scott]

37 Record packing and reordering to make records both space- and speed-efficient may be problematic (systems programming, FFI) (solution: nonstandard alignment can be specified (Ada, C++)) IA Types 37 record packing usually explicitly requested by the programmer (Pascal) space/speed trade-off (breaks alignment) record reordering

38 IA Types 38 Tuple types similar to record types, elements are not named use: functions returning more values Python immutable type, can be converted to an array and back elements accessed using arrays syntax of any number of elements (even 0) mytuple = (42, 2.7, 'mtb') mytuple[1] F# pairs can be addressed using fst and snd otherwise using tuple patterns let tup = (42, 50, 1729); let a, b, c = tup;

39 Union (variant) types Allow a variable to store different type values at different times during program execution. (Correspond to set unions.) union flextype { int intel; float floatel; }; storage allocated for the largest variant uses system programming (non-converting type cast) representing alternatives in a record problem: free unions are not type checked: union flextype el1; float x;... el1.intel = 27; x = el1.floatel; Unions are often missing in modern languages (e.g. Java, C#). IA Types 39

40 Discriminated (tagged) unions IA Types 40 each union variable keeps and information tag/discriminant which variant is currently in use support type checking common in functional languages (ML, Haskell, F#) type intreal = // F# IntValue of int RealValue of float; let printtype value = match value with IntValue value -> printfn "It is an integer" RealValue value -> printfn "It is a float"; let ir2 = RealValue 3.4; printtype ir2; It is a float

41 Variant records IA Types 41 type shapekind = (square, rectangle, circle); (* Pascal *) shape = record centerx : integer; centery : integer; case kind : shapekind of square : (side : integer); rectangle : (length, height : integer); circle : (radius : integer); end;

42 Variant records in C/C++ void setsquareside(struct Shape* s, int side) { s->kind = Square; s->side = side; IA Types 42 num ShapeKind { Square, Rectangle, Circle }; struct Shape { int centerx; int centery; enum ShapeKind kind; union { struct { int side; }; /* Square */ struct { int length, height; }; /* Rectangle */ struct { int radius; }; /* Circle */ }; }; int getsquareside(struct Shape* s) { assert(s->kind == Square); return s->side; }

43 Lists IA Types 43 defined recursively: a list is either an empty list, or a pair of an object and another (shorter) list particularly useful in functional languages (which use recursion and higher order functions) common in imperative scripting languages (Python) can be modelled using records and pointers two main kinds homogeneous (every element of the same type ML) heterogenous (any object can be placed in the list Lisp) terminology: head the first element, tail the remainder of the list

44 Lists Lisp and ML IA Types 44 Lisp program is a list (can be modified during execution!) quote prevents evaluation: (a b c d), quote(a b c d) implementation: chain of cons cells (a pair of pointers) (printed as dotted pairs: (cons 1 2) => (1. 2)) pointer names: car (head) and cdr (tail) (a b c d) ;; list syntax (a. (b. (c. (d. null)))) ;; proper list (a. (b. (c. d))) ;; improper list (a (b c) d) ;; list nesting ML implementation: chain of blocks [(object, value) pairs] operations hd (head) and tl (tail) [a, b, c, d]

45 List examples IA Types 45 Lisp (cons 'a '(b)) (a b) (car '(a b)) a (car nil) ; either nil or error (cdr '(a b c)) (b c) (cdr '(a)) nil (cdr nil) ; either nil or error (append '(a b) '(c d)) (a b c d) ML a :: [b] [a, b] hd [a, b] a hd [] (* run-time exception *) tl [a, b, c] [b, c] tl [a] nil tl [] (* run-time exception *) [a, [c, d] [a, b, c, d]

46 List comprehensions IA Types 46 so-called generator notation create list from lists based on traditional mathematical notation (set comprehensions) Miranda, Haskell, Python, F# {i i i {1,..., 100} i mod 2 = 1} Haskell: [i*i i <- [1..100], i `mod` 2 ==1] Python: [i*i for i in range (1,100) if i % 2 ==1] F#: [for i in do if i % 2 = 1 then yield i*i]

47 Pointer and reference types IA Types 47

48 IA Types 48 Pointer types Values are memory addresses and apecial value nil. Uses 1 indirect addressing 2 way to manage dynamic storage heap-dynamic variables often do not have an associated name (anonymous variables) accessible only through a pointer or a reference Pointers are not structured types (even though usually defined using a type operator) scalar types (values are not data, but references to variables)

49 Pointer operations IA Types 49 Basic operations 1 assignment (sets value to an address) use of an operator for objects outside heap 2 dereferencing (value of the variable pointed to) implicit (Fortran95) explicit (C, the * operator) Accessing record fields (*p).age / p->age (C, C++) p.age (Ada, implicit dereference) Heap management explicit alocation required malloc (C), new (C++) (in languages using pointers for heap management)

50 Problem 1: Dangling pointers The pointer contains an address of a deallocated variable. Why it is a problem? new varible can be allocated to the same address heap management can use the empty memory Creating a dangling pointer: 1 new variable is allocated on the heap, pointed to by p1 2 p2:=p1 3 the variable is deallocated through p1 (p2 is now dangling) int * arrayptr1; // C++ int * arrayptr2 = new int[100]; arrayptr1 = arrayptr2; delete [] arrayptr2; In C++ both arrayptr1 and arrayptr2 are now dangling! Solution: prohibit deallocation IA Types 50

51 Problem 2: Lost variables (garbage) IA Types 51 There is a variable on the heap, which is no longer accessible. Creating a lost variable: 1 new variable, pointed to by p1, is allocated on the heap 2 some other address is assigned to p1 consequence: memory leak solution: garbage collection

52 IA Types 52 Pointers in C/C++ typed can point anywhere (as in assembly languages) extremely flexible, extra caution necessary operations: * dereference, & address of a variable Pointer arithmetic ptr + index = ptr plus index * sizeof(*ptr) int list [10]; int *ptr; ptr = list; The following holds: * (ptr + 1) is the same as list[1] * (ptr + index) is the same as list[index] ptr[index] is the same as list[index]

53 Pointers in C/C++ (2) IA Types 53 pointers pointing to functions used to pass functions as parameters pointers of type void * can point to values of any type (generic pointers) cannot be dereferenced (so the type checker would not complain) use: parameters/results of functions which operate on memory (e.g. malloc)

54 Reference types IA Types 54 A variable of a reference type refers to an object or a value in memory, not a to memory address. no point of doing arithmetics C++ constant pointer, always implicitly dereferenced uses: parameter passing two-way communication (advantage over pointers: no need to dereference) Java non-constant, can point to any instance of the same class used for referencing class instances no explicit deallocation (no dangling references) String str1; // value: null... str1 = "This is a Java literal string";

55 Reference types (2) C# both (C-style) pointers and (Java-style) references use of pointers is strongly discouraged (unsafe modifier for subprograms using pointers) Python, Smalltalk, Ruby all variables are references always implicitly dereferenced pointers vs references Their (pointers) introduction into high-level languages has been a step backward from which we may never recover. C. A. R. Hoare References provide some of the flexibility and capabilities of pointers, without their hazards. IA Types 55

56 Type inference IA Types 56

57 ML type inference IA Types 57 Is it necessary to always specify a type? Example 1 val s : string = "Arthur Dent" val n : int = 42 The type is obvious from the syntax. Example 2 fun twice x = 2 * x We know that 2 is of type int, and * is of type int -> (int -> int). Therefore twice is of type int -> int.

58 IA Types 58 Example 3 ML type inference Is it necessary to always specify a type? fun add [] = 0 add (a :: L) = a + add L We know that 0 is of type int, and [] and a::l are of type list + is of type int -> (int -> int). Therefore add is of type int list -> int. In the ML language, it is always possible to infer the type (for any correct program).

59 Type inference IA Types 59 makes programs more readable supports abstraction (guarantees the most general type) present in many modern languages (ML, Haskell, F#, C# 3.0, C++11, VisualBasic 9.0,... ) based on the Hindley-Milner (Damas-Hindley-Milner) algorithm History 1958 Curry, Feis: simply typed λ-calculus 1969 Hindley: extended, the most geneal type (proof) 1978 Milner: an equivalent algorithm (the algorithm W ) 1982 Damas: proof of completenes

60 IA Types 60 Three stages of type inference fun add [] = 0 add (a :: L) = a + add L 1 each (sub)expression is assigned a new type add : α, b : β, L : γ,... 2 the inference rules are applied built-in expressions: 0 : int, [] : δ list add is a function, applied to a list therefore α = ι κ a ι = δ list... 3 the system of equations (constraints) created in step 2 is then solved (using type unification)

61 Type inference possible results IA Types 61 1 there is exactly one solution 2 the constraint system cannot be solved (e.g. x is required to be of type int and string) type error 3 there are multiple solutions a) polymorphism: the result contains parametric types (i.e. includes type variables) b) ambiguity: Is fun f(x, y) = x + y of type (int * int) -> int, or (string * string) -> string?

62 IA Types 62 ML type expressions (simplified) type expression syntax primitive types: int, bool type variables: a, b, c,... type constructor list: T list (where T is a type expression) type constructor n-tuple: T 1* T 2* T 3 (where T 1, T 2, T 3 are type expressions) function type: T 1 -> T 2 (where T 1 and T 2 are type expressions) our problem will be formulated as a system of type equalities between pairs of type expressions to get a solution we need to find substitutions for type variables

63 Finding a substitution IA Types 63 Simple cases: a list = int list a = int a list = b list list a = int list b list = int list b = int a list = b -> b a list = b b list = a What about the following? does not have a solution does not have a finite solution a list = b list list a = int list; b = int a = (int -> int) list; b = int -> int a = (int list) list; b = int list...

64 The most general solution IA Types 64 a list = b list list problem: infinitely many solutions we aim to find the most general solution observation: for a we need to substitute some suitable list b must be of the element of a list of type a type no other constraints solution: a = b list, where b is a free type variable The solutions of TI will be sets of equalities ai = T i, where: T i are type expressions no ai appears in any T i

65 IA Types 65 Finding the most-general solution Unification of (two) type expressions Finding substitutions for type variables, so the expressions are identical after performing the substitutions. the resulting set of substitutions is called the unifier substitutions are represented by a binding between the type variable a and a type expression τ(a) at the beginnig is each variable free (not bound) we define τ (T ) = { T T = a τ( a) = T T otherwise

66 The unification algorithm Unify (T 1, T 2 ): T 1 := τ (T 1 ); T 2 := τ (T 2 ) if T 1 =T 2 then return true else if T 1 = a ( a does not appear in T 2 ) then τ( a):=t 2 ; return true else if T 2 = b ( b does not appear in T 1 ) then τ( b):=t 1 ; return true else if T 1 =T 1 list T 2=T 2 list then return Unify (T 1, T 2 ) else if T 1 =D 1 ->C 1 T 2 =D 2 ->C 2 then return Unify (D 1, D 2 ) && Unify (C 1, C 2 ) else return false end As a side-effect the algorithm produces the substitution τ. IA Types 66

67 Unification example IA Types 67 'b list = 'a list; 'a->'b='c; 'c-> bool = (bool -> bool) -> bool 'a: bool 'b: a bool 'c: a-> b bool->bool Unify( b list, a list) Unify( b, a) Unify( a-> b, c) Unify( c->bool, (bool->bool)->bool) Unify( c, bool->bool) Unify( a-> b, bool->bool) Unify( a, bool) Unify( b, bool) Unify(bool, bool) Unify(bool, bool)

68 IA Types 68 Constraint generation Selected type rules expression type constraints 1,2,3,... int [] a list hd(l) a L : a list tl(l) a list L : a list E 1 ::E 2 a list E 1 : a, E 2 : a list E 1 +E 2 int E 1 :int, E 2 :int E 1* E 2 int E 1 :int, E 2 :int E 1 =E 2 bool E 1 : a, E 2 : a if E 1 then E 2 else E 3 a E 1 :bool, E 2 : a, E 3 : a E 1 E 2 b E 1 : a -> b, E 2 : a fun f x1.. xn = E x1: a1,..,xn: an, E: b f: a1->...-> an-> b

69 Example IA Types 69 fun f x L = if L = [] then [] else if x <> (hd L) then f x (tl L) else x :: f x (tl L) introduce new type variables f, x, L,... generate constraints using rules 'f = 'a0 -> 'a1 -> 'a2 (* fun *) 'L = 'a3 list (* "=" and "[]" *) 'L = 'a4 list (* hd *) 'x = 'a4 (* "<>" *) 'x = 'a0 (* application *) 'L = 'a5 list (* tl *) 'a1 = 'a5 list (* tl, application *)...

70 Type checking IA Types 70 Direct: fun f g = g 2 fun not x = if x then false else true f not Error: operator and operand don't agree [tycon mismatch] operator domain: int -> 'Z operand: bool -> bool in expression: f not Indirect: fun reverse [] = [] reverse (x:xs) = reverse xs val reverse = fn : a list -> b list changes the type of the list something is wrong

71 Conclusion IA Types 71 type inference computes the types of expressions type declarations are not needed we look for the most general type solving a system of constraints, using unification leads to polymorphism type checking possible errors are discovered statically sometimes the error can be deduced from the expression type disadvantages makes it harder to find the origin of a type error

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CPSC 3740 Programming Languages University of Lethbridge. Data Types Data Types A data type defines a collection of data values and a set of predefined operations on those values Some languages allow user to define additional types Useful for error detection through type

More information

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types Chapter 6 Topics WEEK E FOUR Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer and Reference

More information

Chapter 6. Data Types ISBN

Chapter 6. Data Types ISBN Chapter 6 Data Types ISBN 0-321 49362-1 Chapter 6 Topics Introduction Primitive Data Types Character String Types Enumeration Types Array Types Associative Arrays Record Types Tuple Types List Types Union

More information

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Organization of Programming Languages CS3200 / 5200N. Lecture 06 Organization of Programming Languages CS3200 / 5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Data Types A data type defines a collection of data objects

More information

Lecture 12: Data Types (and Some Leftover ML)

Lecture 12: Data Types (and Some Leftover ML) Lecture 12: Data Types (and Some Leftover ML) COMP 524 Programming Language Concepts Stephen Olivier March 3, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts Goals

More information

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages ICOM 4036 Programming Languages Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer and Reference Types Data Types This

More information

Chapter 6. Data Types. *modified by Stephanie Schwartz ISBN

Chapter 6. Data Types. *modified by Stephanie Schwartz ISBN Chapter 6 Data Types *modified by Stephanie Schwartz ISBN 0-321 49362-1 Chapter 6 Topics Introduction Primitive Data Types Character String Types Enumeration Types Array Types Associative Arrays Record

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 6 Thomas Wies New York University Review Last week Functional Languages Lambda Calculus Scheme Outline Types Sources: PLP, ch. 7 Types What is a type?

More information

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking CS 430 Spring 2015 Mike Lam, Professor Data Types and Type Checking Type Systems Type system Rules about valid types, type compatibility, and how data values can be used Benefits of a robust type system

More information

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN Chapter 6 part 1 Data Types (updated based on 11th edition) ISBN 0-321 49362-1 Chapter 6 Topics Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative

More information

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor COSC252: Programming Languages: Basic Semantics: Data Types Jeremy Bolton, PhD Asst Teaching Professor Copyright 2015 Pearson. All rights reserved. Common Types and Design Concerns Primitive Data Types

More information

Chapter 6. Data Types ISBN

Chapter 6. Data Types ISBN Chapter 6 Data Types ISBN 0-321 49362-1 Chapter 6 Topics Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer

More information

Software II: Principles of Programming Languages

Software II: Principles of Programming Languages Software II: Principles of Programming Languages Lecture 6 Data Types Some Basic Definitions A data type defines a collection of data objects and a set of predefined operations on those objects A descriptor

More information

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 6 Robert Grimm, New York University 1 Review Last week Function Languages Lambda Calculus SCHEME review 2 Outline Promises, promises, promises Types,

More information

Chapter 6. Data Types

Chapter 6. Data Types Chapter 6 Data Types Introduction A data type defines a collection of data objects and a set of predefined operations on those objects A descriptor is the collection of the attributes of a variable Copyright

More information

Chapter 6. Data Types ISBN

Chapter 6. Data Types ISBN Chapter 6 Data Types ISBN 0-321 49362-1 Chapter 6 Topics Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer

More information

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science Organization of Programming Languages CS320/520N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Data Types A data type defines a collection of data objects and

More information

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6] 1 Lecture Overview Types 1. Type systems 2. How to think about types 3. The classification of types 4. Type equivalence structural equivalence name equivalence 5. Type compatibility 6. Type inference [Scott,

More information

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result. Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs

More information

Programming Languages

Programming Languages Programming Languages Types CSCI-GA.2110-001 Summer 2011 What is a type? A type consists of a set of values The compiler/interpreter defines a mapping of these values onto the underlying hardware. 2 /

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

More information

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2 Data Types In Text: Chapter 6 1 Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers Chapter 6: Data Types 2 5-1 Data Types Two components: Set of objects in the type (domain

More information

Types and Type Inference

Types and Type Inference Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

More information

Programming Languages, Summary CSC419; Odelia Schwartz

Programming Languages, Summary CSC419; Odelia Schwartz Programming Languages, Summary CSC419; Odelia Schwartz Chapter 1 Topics Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation Criteria Influences on Language Design

More information

Data Types In Text: Ch C apter 6 1

Data Types In Text: Ch C apter 6 1 Data Types In Text: Chapter 6 1 Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 2 Data Types Two components: Set of objects in the type (domain of values) Set of applicable

More information

Chapter 8 :: Composite Types

Chapter 8 :: Composite Types Chapter 8 :: Composite Types Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter08_Composite_Types_4e - Tue November 21, 2017 Records (Structures) and Variants

More information

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011 Data Types (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011 Based in part on slides and notes by Bjoern 1 Brandenburg, S. Olivier and A. Block. 1 Data Types Hardware-level:

More information

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University Note 3 Types Yunheung Paek Associate Professor Software Optimizations and Restructuring Lab. Seoul National University Topics Definition of a type Kinds of types Issues on types Type checking Type conversion

More information

Programming Languages

Programming Languages Programming Languages Types CSCI-GA.2110-003 Fall 2011 What is a type? An interpretation of numbers Consists of a set of values The compiler/interpreter defines a mapping of these values onto the underlying

More information

A data type defines a collection of data objects and a set of predefined operations on those objects

A data type defines a collection of data objects and a set of predefined operations on those objects Data Types Introduction A data type defines a collection of data objects and a set of predefined operations on those objects Language selection criterion who well does supported data types match problem

More information

Types. What is a type?

Types. What is a type? Types What is a type? Type checking Type conversion Aggregates: strings, arrays, structures Enumeration types Subtypes Types, CS314 Fall 01 BGRyder 1 What is a type? A set of values and the valid operations

More information

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala faisalgrw123@gmail.com MEGA File Solved MCQ s For Final TERM EXAMS CS508- Modern Programming Languages Question No: 1 ( Marks: 1 ) -

More information

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08 Francesco Nidito in the Programmazione Avanzata AA 2007/08 Outline 1 2 3 in the in the 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 7 What is a type? in the What is a type?

More information

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic: For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic: a) in C++: int* x = new(int); b) in Java: class book { protected string title; book(string x)

More information

Data Types. CSE 307 Principles of Programming Languages Stony Brook University

Data Types. CSE 307 Principles of Programming Languages Stony Brook University Data Types CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Data Types We all have developed an intuitive notion of what types are; what's behind

More information

22c:111 Programming Language Concepts. Fall Types I

22c:111 Programming Language Concepts. Fall Types I 22c:111 Programming Language Concepts Fall 2008 Types I Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Chapter 5 Names, Binding, Type Checking and Scopes

Chapter 5 Names, Binding, Type Checking and Scopes Chapter 5 Names, Binding, Type Checking and Scopes Names - We discuss all user-defined names here - Design issues for names: -Maximum length? - Are connector characters allowed? - Are names case sensitive?

More information

Data Types The ML Type System

Data Types The ML Type System 7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

More information

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::

More information

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages.

CSE 452: Programming Languages. Where are we? Types: Intuitive Perspective. Data Types. High-level Programming Languages. CSE 452: Programming Languages Data Types Where are we? Machine Language High-level Programming Languages Assembly Language Logic Object Oriented Imperative Functional You are here Concepts specification

More information

Chapter 5. Names, Bindings, and Scopes

Chapter 5. Names, Bindings, and Scopes Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative

More information

TYPES, VALUES AND DECLARATIONS

TYPES, VALUES AND DECLARATIONS COSC 2P90 TYPES, VALUES AND DECLARATIONS (c) S. Thompson, M. Winters 1 Names, References, Values & Types data items have a value and a type type determines set of operations variables Have an identifier

More information

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can

More information

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction Introduction Primitive Composite Structured Abstract Introduction Introduction Data Type is a Collection of Data Objects Possible r-values for a memory cell Set of operations on those objects Descriptor

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

Types, Type Inference and Unification

Types, Type Inference and Unification Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,

More information

Types II. Hwansoo Han

Types II. Hwansoo Han Types II Hwansoo Han Arrays Most common and important composite data types Homogeneous elements, unlike records Fortran77 requires element type be scalar Elements can be any type (Fortran90, etc.) A mapping

More information

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

More information

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms

More information

COS 140: Foundations of Computer Science

COS 140: Foundations of Computer Science COS 140: Foundations of Computer Science Variables and Primitive Data Types Fall 2017 Introduction 3 What is a variable?......................................................... 3 Variable attributes..........................................................

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

NOTE: Answer ANY FOUR of the following 6 sections:

NOTE: Answer ANY FOUR of the following 6 sections: A-PDF MERGER DEMO Philadelphia University Lecturer: Dr. Nadia Y. Yousif Coordinator: Dr. Nadia Y. Yousif Internal Examiner: Dr. Raad Fadhel Examination Paper... Programming Languages Paradigms (750321)

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts Topics Chapter 6 Structured Data Types Vectors Arrays Slices Associative Arrays Records Unions Lists Sets 2 Structured Data Types Virtually all languages have included some mechanisms for creating complex

More information

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes 長庚大學資訊工程學系 陳仁暉 助理教授 Tel: (03) 211-8800 Ext: 5990 E-mail: jhchen@mail.cgu.edu.tw URL: http://www.csie.cgu.edu.tw/jhchen All rights reserved. No part

More information

CSC 533: Organization of Programming Languages. Spring 2005

CSC 533: Organization of Programming Languages. Spring 2005 CSC 533: Organization of Programming Languages Spring 2005 Language features and issues variables & bindings data types primitive complex/structured expressions & assignments control structures subprograms

More information

CS 230 Programming Languages

CS 230 Programming Languages CS 230 Programming Languages 11 / 20 / 2015 Instructor: Michael Eckmann Questions/comments? Chapter 6 Arrays Pointers Today s Topics We all know what arrays are. Design issues Legal types for subscripts

More information

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered

More information

Structuring the Data. Structuring the Data

Structuring the Data. Structuring the Data 2017-05-12 Structuring the Data Structuring the Data 2017-05-12 Structuring the Data 1 Why Use (Static, Strong) Types? hide machine representation improves programming style and maintainability consistent

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages Third Edition. Chapter 7 Basic Semantics Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol

More information

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

CS321 Languages and Compiler Design I Winter 2012 Lecture 13 STATIC SEMANTICS Static Semantics are those aspects of a program s meaning that can be studied at at compile time (i.e., without running the program). Contrasts with Dynamic Semantics, which describe how

More information

Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE 3302 Programming Languages Lecture 8: Functional Programming CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1 Functional Programming Languages

More information

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007 CSE 3302 Programming Languages Data Types (cont.) Chengkai Li Fall 2007 Subset U = { v v satisfies certain conditions and v V} Ada subtype Example 1 type Digit_Type is range 0..9; subtype IntDigit_Type

More information

CSCI312 Principles of Programming Languages!

CSCI312 Principles of Programming Languages! CSCI312 Principles of Programming Languages! Chapter 5 Types Xu Liu! ! 5.1!Type Errors! 5.2!Static and Dynamic Typing! 5.3!Basic Types! 5.4!NonBasic Types! 5.5!Recursive Data Types! 5.6!Functions as Types!

More information

Lecture 4 Memory Management

Lecture 4 Memory Management Lecture 4 Memory Management Dr. Wilson Rivera ICOM 4036: Programming Languages Electrical and Computer Engineering Department University of Puerto Rico Some slides adapted from Sebesta s textbook Lecture

More information

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5 Outline Name, Scope and Binding In Text: Chapter 5 Names Variable Binding Type bindings, type conversion Storage bindings and lifetime Scope Lifetime vs. Scope Referencing Environments N. Meng, S. Arthur

More information

Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright 2016 Elsevier 1 Chapter11_Functional_Languages_4e - Tue November 21, 2017 Historical Origins The imperative

More information

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1 Polymorphism Chapter Eight Modern Programming Languages, 2nd ed. 1 Introduction Compare these function types The ML function is more flexible, since it can be applied to any pair of the same (equality-testable)

More information

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no Questions? First exercise is online: http://www.win.tue.nl/~mvdbrand/courses/glt/1112/ Deadline 17 th of October Next week on Wednesday (5 th of October) no lectures!!! Primitive types Primitive value

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics

More information

CS321 Languages and Compiler Design I. Fall 2013 Week 8: Types Andrew Tolmach Portland State University

CS321 Languages and Compiler Design I. Fall 2013 Week 8: Types Andrew Tolmach Portland State University CS321 Languages and Compiler Design I Fall 2013 Week 8: Types Andrew Tolmach Portland State University 1 THE TYPE ZOO int x = 17 Z[1023] := 99; double e = 2.81828 type emp = {name: string, age: int} class

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad-00 014 Subject: PPL Class : CSE III 1 P a g e DEPARTMENT COMPUTER SCIENCE AND ENGINEERING S No QUESTION Blooms Course taxonomy level Outcomes UNIT-I

More information

CMSC 331 Final Exam Section 0201 December 18, 2000

CMSC 331 Final Exam Section 0201 December 18, 2000 CMSC 331 Final Exam Section 0201 December 18, 2000 Name: Student ID#: You will have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for

More information

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides. cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides. We are looking for homework graders. If you are interested, send mail to cs242cs.stanford.edu

More information

CS 415 Midterm Exam Spring 2002

CS 415 Midterm Exam Spring 2002 CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming

More information

Static Checking and Type Systems

Static Checking and Type Systems 1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical

More information

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Types. Chapter Six Modern Programming Languages, 2nd ed. 1 Types Chapter Six Modern Programming Languages, 2nd ed. 1 A Type Is A Set int n; When you declare that a variable has a certain type, you are saying that the values the variable can have are elements of

More information

int f(char a, char b) {

int f(char a, char b) { Polymorphism Chapter Eight Modern Programming Languages 1 Introduction Compare these function types The ML function is more flexible, since it can be applied to any pair of the same (equality-testable)

More information

CA341 - Comparative Programming Languages

CA341 - Comparative Programming Languages CA341 - Comparative Programming Languages and David Sinclair Data, Values and Types In 1976 Niklaus Wirth (inventor of Pascal, Modula, Oberon, etc) wrote a book called Algorithms + Data Structures = Programs

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no INF 3110-2008 Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.)

More information

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture Topics Chapter 5 Variables Imperative Paradigm Variables Names Address Types Assignment Binding Lifetime Scope Constants 2 Imperative Paradigm The most widely used and well-developed programming paradigm.

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information

COS 140: Foundations of Computer Science

COS 140: Foundations of Computer Science COS 140: Foundations of Variables and Primitive Data Types Fall 2017 Copyright c 2002 2017 UMaine School of Computing and Information S 1 / 29 Homework Reading: Chapter 16 Homework: Exercises at end of

More information

Lecture 13: Complex Types and Garbage Collection

Lecture 13: Complex Types and Garbage Collection Lecture 13: Complex Types and Garbage Collection COMP 524 Programming Language Concepts Stephen Olivier March 17, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts CSE 341: Programming Languages Course outline Explore several other programming paradigms 1 lecture: Concepts ML, Scheme,...: functional programming, lists, recursion, pattern-matching, polymorphic typing,

More information

Type Bindings. Static Type Binding

Type Bindings. Static Type Binding Type Bindings Two key issues in binding (or associating) a type to an identifier: How is type binding specified? When does the type binding take place? N. Meng, S. Arthur 1 Static Type Binding An explicit

More information

Final-Term Papers Solved MCQS with Reference

Final-Term Papers Solved MCQS with Reference Solved MCQ(S) From FinalTerm Papers BY Arslan Jan 14, 2018 V-U For Updated Files Visit Our Site : Www.VirtualUstaad.blogspot.com Updated. Final-Term Papers Solved MCQS with Reference 1. The syntax of PHP

More information

Imperative Programming

Imperative Programming Naming, scoping, binding, etc. Instructor: Dr. B. Cheng Fall 2004 1 Imperative Programming The central feature of imperative languages are variables Variables are abstractions for memory cells in a Von

More information

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, Chapter 5 Basic Semantics Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, and the Environment Variables

More information

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility Quiz Review Q1. What is the advantage of binding things as early as possible? Is there any advantage to delaying binding? Answer: Early binding generally leads to greater efficiency (compilation approach)

More information

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309 A Arithmetic operation floating-point arithmetic, 11 12 integer numbers, 9 11 Arrays, 97 copying, 59 60 creation, 48 elements, 48 empty arrays and vectors, 57 58 executable program, 49 expressions, 48

More information