Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming Languages and Paradigms
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Names and Bindings.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
The Assembly Language Level
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Type Checking.
Compiler Construction
Chapter 9 Imperative and object-oriented languages 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Disjoint Unions (quick review)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Composite types Cartesian products –tuples, records, structures disjoint unions –union, discriminated or variant records mappings –arrays, functions recursive.
Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.
Type Checking  Legality checks  Operator determination  Overload resolution.
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Data Types.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
Names Variables Type Checking Strong Typing Type Compatibility 1.
2-1 © 2004, D.A. Watt, University of Glasgow 2 Values and Types  Types of values.  Primitive, composite, recursive types.  Type systems: static vs dynamic.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming in C Structs and Unions. Java vs C Suppose you were assigned a write an application about points and straight lines in a coordinate plane.
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
1 COMP313A Programming Languages Data Types (2). 2 Overview Type Constructors Type Equivalence Type Checking Type Conversion.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Language: Set of Strings
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Non Leap YearLeap Year DateDay NumberMod 7Day NumberMod 7 13-Jan Feb Mar Apr May Jun Jul
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
2-1 © 2004, D.A. Watt, University of Glasgow 2 Values and Types  Types of values.  Primitive, composite, recursive types.  Type systems: static vs dynamic.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types In Text: Chapter 6.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
CS 326 Programming Languages, Concepts and Implementation
Records Design Issues: 1. What is the form of references?
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Compiler Construction
Assignments and Procs w/Params
Compiler Construction
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes recursive types) Types can be –built-in –user-defined

Primitive types Typical primitive types –Boolean = {true,false} –Character = {…,’a’,…’z’,…’0’,…,’9’,…} –Integer = {0, 1, -1, 2, -2, … } –Float = {0.0, …, 1.0, -1.0, …, , …} Set of primitive types is language-dependent Value of primitive types typically implementation-dependent, though can be written into language spec (e.g. Java, C#)

Gotcha’s Boolean is not always a distinct type –C++ bools are really ints Character is not always a distinct type –C, C++, Java are really just (unsigned) ints Languages sometimes provide many variants on a primitive type: –Java’s Integer types: byte, short, int, long

User-defined primitives Enumerations allow programmers to create their own primitive (non- decomposable) types: –C++ enum day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; Monday prints as 1 –Java enum Day {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} Monday prints as Monday methods can be attached to enumerands

Composite types Cartesian products –tuples, records, structures disjoint unions –union, discriminated or variant records mappings –arrays, functions recursive types –lists, trees

Cartesian products S x T = {(x,y) | x in S, y in T} Basic operations: –construction of a pair –selection of a component Cardinality (#) of a Cartesian product: –#(S x T) = #S * #T Can be extended naturally beyond pairs to general n-tuples

C structures enum Month {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec}; struct Date { Month m; int d; }; Date today = {mar,6};

Pascal records month = (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec); date = record m: month; d: 1..31; end; today : date; today.m := mar; today.d := 6;

ML records Form of record: { :, :,... : } Accessing a member: # ( ) datatype month = jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec; val today = { m=mar, d=6 };

ML tuples A tuple in ML is a special type of record, whose field labels are integers starting from 1. (“fred”,false) is a tuple whose type is reported as string * bool. This is underlyingly the record { 1=“fred”, 2=false }, but the (…) are special syntactic sugar.

Tuples and records in ML val a = (“fred”, false) has type string*bool. val b = {1=“fred”, 2=false) has type string*bool, and is printed as (“fred”,false). val c = {one=“fred”, two=false} has type {one:string, two:bool} and is printed as {one=“fred”, two=false}.

Making a date in ML datatype month = jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec; datatype date = Date of month * int; val today = Date(mar,6); Date is a new type which is built using a tuple, a special type of record.

Accessing members of a datatype val today = Date(mar,6); val Date(x,y) = today; This binds x to mar and y to 6!

Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It is the type Unit, whose value is (). –Unit corresponds to void in C, C++ and Java.

Mappings Types of mappings: –Arrays –Functions Arrays map integers to the declared type of the variables in the array. Functions map an arbitrary domain type to an arbitrary range type.

Mappings in general S = {0,1} T = {a,b,c} How many mappings S  T are there? #(S  T) = #(T) #(S) The possible S  T mappings are: {0  a,1  a}, {0  a,1  b}, {0  a,1  c}, {0  b,1  a}, {0  b,1  b}, {0  b,1  c}, {0  c,1  a}, {0  c,1  b}, {0  c,1  c}

Implementing a finite mapping as an array T[] map = {a,c}; defines map to be the third mapping given on previous slide: map[0] = a map[1] = c

Implementing a finite mapping as a function We can also define the mapping as a function (ML example): fun map 0 = a | map 1 = c; or (in Pascal): function map(s:S):T; begin case (s) of 0: map:=a; 1: map:=c; end;

More discussion We can also define the mapping as a function (ML example): fun map x = case x of 0 => a 1 => c (Notice function syntax in case expression!) In C: T map(int x) { switch (x) { case 0: return a; case 1: return c; }

More general finite mappings Hash tables let us define more general finite mappings (domain type does not need to be integral).

Memoization Combination of computation and storage!

Array-based vs. function-based mappings An array-based mapping is explicit: –it gives domain-range pairs explicitly A function-based mapping is implicit: –for each domain element it gives a method for computing the corresponding range element.

Disjoint Unions Pascal: variant record C/C++: union ML: union type Ada: discriminated record

Disjoint Unions (quick review) Pascal: variant record C/C++: union ML: union type Ada: discriminated record

What is a disjoint union? “Another kind of composite value is the disjoint union, whereby a value is chosen from one of several (usually different) sets.” David Watt Programming Language Design Concepts page 27

S+T Values are tagged to indicate which set they are from: S+T = { left(x) | x in S }  { right(y) | y in T } Cardinality? #(S+T) = #S + #T

ML datatype number = Exact of int | Inexact of real; datatype person = King | Peer of string*string*int | Knight of string | Peasant of string; Values:King, Peer(“Earl”, “Carlisle”, 7), Knight(“Gawain”), Peasant(“Jack Cade”) Paulson, ML for the Working Programmer pages

OO disjoint unions (new material) The main purpose of a disjoint union is to bring together values from different sets in a way that you can determine from which set a given value is drawn. This type of structure is used very commonly in OO programming. Polymorphism feeds off of this idea: implicit selection based on type.

OO disjoint union example Disjoint union

Recursive types A type defined in terms of itself Examples: lists, strings, trees, … If R is a recursive type, #(R) is infinite. Members of a recursive type are often finite, tough infinite members are possible (the list of all primes is a list, but it is an infinite list). How? They are implicitly represented (think of function vs. array representation of mappings): e.g. lazy lists.

Recursive type membership Consider the list type: datatype ’a List = Nil | Cons of ’a * ’a List; What are the members of this type? List(0) = {Nil} (lists of length zero) List(1) = {Cons(x,y)| y in List(0)} (lists of length one) … List(k) = {Cons(x,y)| y in List(k-1)} (lists of length k) List = U n=0… ∞ List(n) (lists of all lengths, zero or greater)

C++ & pointer types Languages like C++ do not directly support recursive types, but rather require the use of an indirection mechanism, the pointer. This has to do with the semantics of assignment. If v is a recursive structure, what are reasonable semantics for w=v? –copy semantics – expensive –reference semantics – sharing If language does not permit selective updating of a structure (as in a pure functional language) the semantics are indistinguishable).

Type equivalence Type equivalence addresses the question of when two types are considered equivalent. –Structural equivalence two types are equivalent if and only if they have the same set of values –Name equivalence two types are equivalent if and only if they are defined in the same place

Structural equivalence Types S and T are equivalent iff –S and T are both primitive and S and T are identical –S=AxB and T=CxD are both cartesian products and A and C are equivalent and B and D are equivalent –S=A  B and T=C  D are both mappings and A and C are equivalent and B and D are equivalent –S=A+B and T=C+D are both disjoint unions and A and C are equivalent and B and D are equivalent Otherwise, S and T are not equivalent.

Name equivalence Strictly, types are unique – each new type definition occurs in a different place, and so results in a new distinct type. Pascal technically uses name equivalence, but in practice the rules are relaxed: –For example, suppose we wanted to define a file type to permit two programs to communicate via a file. Since the file type is defined in two different places (in two different programs) their files types are considered different (and hence incompatible).

Name vs. Structural equivalence Name equivalence forces each distinct type to be defined in one and only one place. This is sometimes inconvenient, but it helps to make the program more maintainable. (If the same type is defined in several places, and subsequently it has to be changed, the change must be made consistently in several places.) Structural equivalence allows confusion between types that are only coincidentally similar. Watt, page 42

Type Completeness Principle “No operation should be arbitrarily restricted in the types of its operands.” Watt, page 43 Types whose values are unrestricted in their usage are termed “first-class”, whereas those that are restricted are termed “second-class”. In Pascal procedures can be passed as arguments to other procedures, but cannot, for example, be part of a composite value. Pascal procedures are therefore second class. A language like ML imposes no such arbitrary type distinctions. All types are first-class.

Erlang About Erlang Erlang at the movies!