Type u A type is a collection of values and operations on those - - PDF document

type
SMART_READER_LITE
LIVE PREVIEW

Type u A type is a collection of values and operations on those - - PDF document

10/11/17 CSCI 2320 Principles of Programming Languages Types & Type System Reading: Ch 5 & 6 (Tucker & Noonan) Type u A type is a collection of values and operations on those values. u Example u Integer type has values ..., -2, -1,


slide-1
SLIDE 1

10/11/17 1

CSCI 2320 Principles of Programming Languages Types & Type System Reading: Ch 5 & 6 (Tucker & Noonan)

Type

u A type is a collection of values and

  • perations on those values.

u Example

u Integer type has values ..., -2, -1, 0, 1, 2, ... and

  • perations +, -, *, /, <, …

u Boolean type has values true and false and

  • perations ∧, ∨, ¬.

u Question 1: Will the values be bounded? u Question 2: Will the types be pre-defined?

slide-2
SLIDE 2

10/11/17 2

Type errors & Type systems

u Type error: Incompatibility between data

type and operations

u Type system: Detects type errors

u Can’t we detect type errors just by looking at the

values? u Machine level:

0100 0000 0101 1000 0000 0000 0000 0000

  • The floating point number 3.375
  • The 32-bit integer 1,079,508,992
  • Two 16-bit integers 16472 and 0
  • Four ASCII characters: @ X NUL NUL
slide-3
SLIDE 3

10/11/17 3

Static vs. dynamic type systems

u A language is statically typed if the types of

all variables are fixed when they are declared at compile time.

u Example?

u A language is dynamically typed if the type

  • f a variable can vary at run time depending
  • n the value assigned.

u Example?

u Both static and dynamic typing?

u Example program next

u Which one is “better”—static or dynamic?

Java: Why dynamic typing for type casting?

slide-4
SLIDE 4

10/11/17 4

Strongly typed language

u A language is strongly typed if its type

system allows all type errors in a program to be detected either at compile time or at run time.

u Independent of static or dynamic typing

u Very confusing area:

u http://ericlippert.com/2012/10/15/is-c-a-

strongly-typed-or-a-weakly-typed-language/

u http://c2.com/cgi/wiki?StronglyTyped

Is C strongly typed?

u No, because errors can go undetected.

#include <stdio.h> union {int i; float f;} u; int main() { float x = 0.0; u.i = 987654321; x = x + u.f; printf ("%f\n", x); return 0; }

slide-5
SLIDE 5

10/11/17 5

Basic vs. non-basic types

u Basic: int, float, bool, char

u Implicit type conversion: narrowing vs. widening

u Non-basic: pointer, string, array, list, etc. +

programmer-defined types

u Implementation issues

Implementation of arrays

u C:

int a[5];

u Static memory allocation: “dope vector”

followed by actual array elements

u Dope vector: element size, element type, # of

elements u Array elements must be stored contiguously

u Why?

slide-6
SLIDE 6

10/11/17 6

Arrays: C vs. Java

u Java: dynamic allocation, but array size/type

cannot be changed thereafter int[] a = new int(5);

u Pre-initialized to 0s u Contiguous in memory?

u Not guaranteed

List vs. array

u Size of list may vary at run-time u List elements may not be contiguous in

memory

u List may contain heterogeneous data

(Python)

slide-7
SLIDE 7

10/11/17 7

Other issues

u Functions as types – C vs. Java

u C: void qsort(void *base, size_t nitems, size_t size,

int (*compare)(const void *, const void*))

u Java?

u Sub-types

u OOP: Subclass

u Polymorphism

Type System

  • Ch. 6
slide-8
SLIDE 8

10/11/17 8

Motivation: Detecting Type Errors

u The detection of type errors, either at compile time

  • r at run time, is called type checking.

u Type errors occur frequently in programs. u Type errors can’t be prevented/detected by EBNF u If undetected, type errors can cause severe run-time errors. u A type system can identify type errors before they occur.

Type System for CLite

u Static typing and type checking at compile time u Single function: main u Single scope: no nesting, no global variables u Type rules

1.

Each referenced variable must be declared

2.

Each declared variable must have a unique identifier

3.

Identifier must not be a keyword (syntactically enforced)

slide-9
SLIDE 9

10/11/17 9

Example Clite Program


 void main ( ) { int n; int i; int result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } print result; }

Type Rule 1

u All referenced variables must be declared.

u Type map is a set of ordered pairs

E.g., {<n, int>, <i, int>, <result, int>}

u Can implement as a hash table

slide-10
SLIDE 10

10/11/17 10

Type Rule 2

u All declared variables must have unique names.


 void main ( ) { int n; int i; int result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; }
 print result; } These must all be unique

Type checking a program

u A program is valid if

u Declarations are valid and u Rest is valid wrt Declarations

void main ( ) { int n; int i; int result; n = 8; i = 1; result = 1; while (i < n) { i = i + 1; result = result * i; } 
 print result; }

slide-11
SLIDE 11

10/11/17 11

Type checking a program (cont…)

u Validity of a Statement: u Assignment statement is valid if

u Its target Variable is declared u Its source Expression is valid u If the target Variable is float, then the type of the

source Expression must be either float or int

u Otherwise if the target Variable is int, then the

type of the source Expression must be either int or char

u Otherwise, the target Variable must have the same

type (e.g., bool) as the source Expression u A conditional stmt is valid if:

u Its test Expression is valid and has type bool u Its body is valid

u A while loop is valid if:

u Its test Expression is valid and has type bool u Its body is valid

Type checking a program (cont…)

slide-12
SLIDE 12

10/11/17 12

u Validity of an Expression:

u A Value is always valid. u A Variable is valid if it appears in the type map. u A binary operation is valid if:

u Its Expressions term1 and term2 are valid u If its Operator op is arithmetic, then both Expressions must be

either int or float

u If op is relational, then both Expressions must have the same

type

u If op is && or ||, then both Expressions must be bool

u A unary operation is valid if:

u Its Expression term is valid, u …

u The type of an Expression e is:

u If e is a Value, then the type of that Value. u If e is a Variable, then the type of that Variable. u If e is a Binary op term1 term2, then:

u If op is arithmetic, then the (common) type of term1 or term2 u If op is relational, && or ||, then bool

u If e is a Unary op term, then:

u If op is ! then bool u …