06 Types-1 - Types (Scott, chapter 7) • Purposes of types...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Types (Scott, chapter 7) • Purposes of types in a language – Provide implicit context for operations • meaning of + depends on type • result of new p depend on type • overloading – Limits the set of operations that can be performed on a semantically valid program • Can’t add a boolean to a char, for example • Eliminates many programming errors. Type system • mechanism to define types • a way to associate types with objects in language • a set of rules for – type equivalence • when are type of two values considered to be the same? (name vs. structure) – type compatibility • when can a value of a given type be used in a particular context? – type inference • How is the type of an expression determined? Some terminology • Type checking : – process of ensuring that a program satisfies languages type compatibility rules. • Str ongly typed : – language implementation prohibits application of operation to object not intended to support operation • Statically typed : – strongly typed + type checking (mostly) performed before run-time. • Dynamically typed : – type checking performed at run-time. 3 differ ent ways to think about types • Denotational • Constructive • Abstraction-based Denotional types • a type is a set of values called a domain. – A value has a type if it belongs to the set – An object has a type if its value is guaranteed to be in the set. Examples of denotational types boolean {true, false} int {…. –1,0,1,… } = Z remark: we usually ignore limited precision and deal with it outside of type system Record types (correspond to Cartesian product of component types) TYPE point = RECORD x:integer, y:integer END {(x,y) | x Є Z and y Є Z } or Cartesian product Z X Z TYPE person = RECORD name: string, age: integer END {(name, age) | name Є string, age Є Z } = string X Z Enumerated type: TYPE hue = (red,green,blue) Constructive types • A type is either – a primitive (built-in) type • int, • char, • boolean, • etc. – or a composite formed by applying a type constructor to simpler types Examples from Modula-2 TYPE t = ARRAY [0..31] OF CHAR; TYPE b = [ .. 1 ] subrange type (values 0,1) TYPE s = SET OF b values associated with s are {}, {0}, {1}, {0,1}) Examples from ML datatype intList = nil | Cons of int * intList datatype tree = Leaf of int | Node of tree * tree Examples from ML datatype intList = Nil | Cons of int * intList datatype tree = Leaf of int | Node of tree * tree Note that these types are recursive Note that these types are recursive Abstraction-based types • a type is an interface consisting of a set of operations with well-defined semantics • Examples: – int operations +,-,*,/, % – abstract data types • stack: push, pop, top, size • remark: Algebraic specifications for types define a type without explicitly talking about the values....
View Full Document

This note was uploaded on 12/09/2009 for the course COP 5555 taught by Professor Staff during the Fall '08 term at University of Florida.

Page1 / 79

06 Types-1 - Types (Scott, chapter 7) • Purposes of types...

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

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