5 Types - 10/4/10 cs242 General discussion of types What is...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
10/4/10 1 Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Revised Chapter 6 General discussion of types What is a type? Compile-time versus run-time checking Conservative program analysis Type inference Discuss algorithm and examples Good example of static analysis algorithm Polymorphism Uniform versus non-uniform implementations Thoughts to keep in mind What features are convenient for programmer? What other features do they prevent? What are design tradeoffs? Easy to write but harder to read? Easy to write but poorer error messages? What are the implementation costs? Architect Compiler, Runtime environ- ment Programmer Tester Diagnostic Tools Programming Language A type is a collection of computable values that share some structural property . Examples Non-examples Integer String Int Bool (Int Int) Bool { 3, True, \x->x } Even integers { f:Int Int | x>3 => f(x) > x *(x+1) } Distinction between sets of values that are types and sets that are not types is language dependent . Program organization and documentation Separate types for separate concepts Represent concepts from problem domain Indicate intended use of declared identifiers Types can be checked, unlike program comments Identify and prevent errors Compile-time or run-time checking can prevent meaningless computations such as 3 + true – “Bill” Support optimization Example: short integers require fewer bits Access record component by known offset A type error is something the compiler/ interpreter reports when I make a mistake in my syntactically correct program? Languages represent values as sequences of bits. A type error occurs when a bit sequence written for one type is used as a bit sequence for another type? A type error occurs when a value is used in a way inconsistent with its definition.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10/4/10 2 Array out of bounds access C/C++: runtime errors. Haskell/Java: dynamic type errors. Null pointer dereference C/C++: null pointer dereferences are run-time errors. In Haskell/ML, pointers are hidden inside datatypes. Null pointer dereferences correspond to incorrect use of these datatypes. Such errors are static type errors. JavaScript and Lisp use run-time type checking f(x) Make sure f is a function before calling f. Haskell and Java use compile-time type checking f(x) Must have f :: A B and x :: A Basic tradeoff Both kinds of checking prevent type errors. Run-time checking slows down execution. Compile-time checking restricts program flexibility. JavaScript array: elements can have different types Haskell list: all elements must have same type Which gives better programmer diagnostics? In JavaScript, we can write a function like
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2011 for the course CS 242 at Stanford.

Page1 / 9

5 Types - 10/4/10 cs242 General discussion of types What is...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online