Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.

Slides:



Advertisements
Similar presentations
You can do more than what you think ……… If you believe you can.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
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.
Chapter 7:: Data Types Programming Language Pragmatics
1 Introduction to Data Types (Section 7.1) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Kernighan/Ritchie: Kelley/Pohl:
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
Compiler Construction
Chapter 9 Imperative and object-oriented languages 1.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Mark Hennessy Dept. Computer Science NUI Maynooth 1 CaML continued CS 351 Programming Paradigms Dept. Computer Science NUI Maynooth.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ISBN Chapter 6 Data Types: Structured types.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Data Types.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Adapted from slides for COMP 144 Programming Language Concepts Spring 2002 by Felix Hernandez-Campos The University of North Carolina at Chapel Hill.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Compiler Construction
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 COSC3306: Programming Paradigms Lecture 2: Data Types Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Basic Semantics Associating meaning with language entities.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Programming Languages and Paradigms Imperative Programming.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 4 of Programming Languages by Ravi Sethi.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Python Primer 1: Types and Operators
Compiler Construction
Course Overview PART I: overview material PART II: inside a compiler
C Language B. DHIVYA 17PCA140 II MCA.
Compiler Construction
Presentation transcript:

Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms

Mark Hennessy CS351 Dept Computer Science NUI Maynooth2 Data Types Nearly all programming languages include the notion of a type for expressions and objects*. 1. a + b allows a and b to be of some numeric type e.g int, float etc. 2. Types limit the set of operations that can be performed on some object*.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth3 Type Systems A type system consists of the following: 1. A mechanism to define types and associate them with certain language constructs. 2. A set of rules for type equivalence, type compatibility and type inference. ``Types’’ of types: constants, variables, records, arrays, parameters etc.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth4 Type Systems cont… Type Equivalence: rules determine when two values are the same type. Type Compatibility: rules to determine if a value of a certain type can be used in a given context, e.g in an l-value assignment. Type Inference: These rules define the type based upon the types of its constituent parts.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth5 Type Checking Type checking is a process of ensuring that a program obeys the languages’ type rules. A language is strongly typed if it prohibits the application of any illegal operation. A language is statically typed is it is strongly typed and type checking is performed at compile time. Ada is strongly typed, Pascal and C less so. Dynamic type checking is a form of binging types to variables only when they are seen at runtime. Most scripting languages and those with dynamic scope use dynamic type checking.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth6 Polymorphism No, not in the OO sense! It allows a single body of code to work with multiple types. Examples: The math operators +,-,*,/ work on ints, floats, shorts, doubles in Java! In C++ we can overload those operators to work on classes!

Mark Hennessy CS351 Dept Computer Science NUI Maynooth7 Classification of Types Nearly all languages will have simple built in types. 1. Boolean type, a 1-byte representing T or F. 2. Characters, 1-byte representing 256 ASCII characters. Old skool, there are now supports for ISO which supports charaters for all human ( and non-human! ) characters. 3. Numeric types: generally integer types which can be signed or unsigned (C,C++). Floating point types correspond to the IEEE 754 floating point number standard.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth8 Enumerated Types Built-in types are known as discrete types as they all in the countable domain. It is possible to extend the built in types through the use of enumeration. enum weekdays = { mon, tues, wed, thurs, fri };  typedef int weekday; const weekday mon = 0, tues = 1, wed = 2, thurs = 3, fri = 4;

Mark Hennessy CS351 Dept Computer Science NUI Maynooth9 Enumeration in Java? Old Way: public static final int mon = 0; public static final int tues = 1; … int day = weekdays.mon; New Way : enum weekdays { mon(0), tues(1) … fri(3); private final int day; weekdays(int d) day = d; public int day() return day; } int day = weekdays.mon.day();

Mark Hennessy CS351 Dept Computer Science NUI Maynooth10 Composite Types Not built-in but not explicitly user defined. 1. Records or Structures. 2. Arrays. 3. Sets. 4. Pointers. 5. Lists. 6. Files.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth11 Implementing Type Checking Every definition of some variable must include a type definition. For builtin types it is quite easy to determine the type without knowing where the declaration took place? What does the C/C++ keyword sizeof do? E.g int a = 5; std::cout<<sizeof(a);

Mark Hennessy CS351 Dept Computer Science NUI Maynooth12 User defined Type Equivalence Do we compare user-defined types by their internal structure or via their names? Given some user-defined type type foo = record a:int b:int end; type foo2 = record b:int a:int end; foo one; foo2 two; Print one == two;

Mark Hennessy CS351 Dept Computer Science NUI Maynooth13 What about? type student = record name, address = string age :int end; type school = record name, address = string Num_students: int end; x: student; y: school; Print x == y; Is this an error? No!! But we surely didn’t mean to assign student the value of school. Structural equivalence is not a good way of type checking.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth14 Name Equivalence This is based on the premise that is we have declared two different types then they are more then likely different types! x: student will be linked to the first record type and y: school will be linked to the second record type. Hence the Print x == y will result in an error. At what point, compile or run-time?

Mark Hennessy CS351 Dept Computer Science NUI Maynooth15 Type Conversion In a language with static typing, there are many contexts in which a specific type are expected. Consider: a = square (5); What can we assume about the expression above? Specifically in relation to the l- and r-values?

Mark Hennessy CS351 Dept Computer Science NUI Maynooth16 Type Conversion cont… Sometimes we need to perform type casting to change the type of data contained in one variable and assign it to another. There are ~ 2 cases: 1. The types are structurally equivalent but the language uses name-equaivalences. This is very easy, recall our earlier example: x: student; y: school; x.age = y.num_students; 2. The types have different representations ``under the hood’’. E.g, any int can be written as floating point number. int a = 5; float f = (float) a;

Mark Hennessy CS351 Dept Computer Science NUI Maynooth17 Type Conversion cont… In C++ there are builtin functions to perform the casts: int a = 5; double a = static_cast (a); double b = reinterpret_cast (a); What is the difference?

Mark Hennessy CS351 Dept Computer Science NUI Maynooth18 Type Compatibility To facilitate the creation of container classes such as Stack, Vector etc several languages provide a generic reference type. In C\C++ this is void * which can refer to almost anything within a program. With void *, any l-value can be assigned to a void *. void * p = NULL: int a; double b; p = a; p = b; Java has this feature too, the generic ability to point to anything. What is it?

Mark Hennessy CS351 Dept Computer Science NUI Maynooth19 Type Inference How do you determine the overall type of some expression? Easily: The result of arithmetic has the same type as its operands. The result of any logical statement is a boolean. The result of a function is always declared by the user. The result of an assignment is the same as the l-value type.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth20 Composite Types 1 – Structures and Unions These are an old style way of tying together data in a kind of manner like a class. struct example { char name[2]; int number; } Each of the components is known as a field. To access the data, just use a pointer (in C) or a ``dot’’ (most other languages). example *one; one->number = 7; example two; two.number = 8;

Mark Hennessy CS351 Dept Computer Science NUI Maynooth Arrays One of the most common and important data types. The data within an array is typically homogenous. Arrays can be thought of as a mapping from an index to a particular component. This mapping is typically an integer but more recent scripting languages allow array indices to be whatever we want. These are just hash tables in disguise. We will cover them again. Arrays typically use the ``[ ]’’ brackets to index them.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth22 2 – Arrays cont… Some languages allow for a powerful feature known as an array slice. Eg in python: S = “Hello World” print S[6:] What is the answer?

Mark Hennessy CS351 Dept Computer Science NUI Maynooth23 2 – Arrays cont… Array sizes can be fixed when the array is declared. Eg. int a[] = new int[5]; This memory can then be allocated at compile time. Arrays can also be allocated at run-time: int a [] = new int[Integer.parseInt(args[0])]; Arrays can even have their length increased: String s = “hel”; s = s+”lo”;

Mark Hennessy CS351 Dept Computer Science NUI Maynooth Arrays cont… When an array is declared affects how memory for the array is allocated. 1. Global lifetime, static shape: If the shape is known at compile time and the array is global (static) then the array can exist throughout the lifetime of the program. 2. Local lifetime, static shape: If the shape is known at compile time but the instance is a local one, then the array will only exist on the stack frame when the function with the array is executed. 3. Local lifetime, shape bound at runtime: If the shape of the array is not known until runtime, then a reference can to the array is created on the stack frame and a pointer to the size of the array then used to calculate the offset from the start of the array reference on the stack frame. 4. Arbitrary lifetime, shape bound at runtime: In Java every array variable is a reference to an array object. Declaring int a[] does not create any space in memory, it just creates the reference. Only when the keyword new is called is the memory allocated. Once allocated the size never changes. 5. Arbitrary Lifetime, dynamic shape: It is possible that the size of the array can change many times after the initialisation, therefore the strategy in 3 will not suffice because the space may grow below the initial pointer to the array. What must happen is that new memory must be allocated, all of the data copied over and then the deletion of the old data.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth Sets Some languages such as Pascal provide a builtin composite type of Set that allow for union, set difference and intersection. OO programming allow a Set type to be readily defined using arrays or hash- tables.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth Pointers We will have lots of fun with these when we cover OO via C++. We will leave the discussion on them until then!

Mark Hennessy CS351 Dept Computer Science NUI Maynooth Lists A list is defined recursively as either the empty set or a list consisting of a single element known as the head and a another smaller list called the tail. In CaML, a list consists of homogeneous elements Eg – let l = [1;2;3;4];; defines l as a list of 4 ints from 1-4. To perform operations on lists: let l2 = 5::[1;2;3;4];; => [5;1;2;3;4;] hd l2;; => 5 hd [];; => Runtime Error tl l2;; => [1;2;3;4] tl [];; => Runtime Error [3;4];; => [1;2;3;4] list_length l2;; => 5 What is the result of hd [5];; ? What is the result of tl [5];;?

Mark Hennessy CS351 Dept Computer Science NUI Maynooth28 6 – File Types We will leave this until we are doing some OO and scripting. We just need to know that is obviously possible to have different types of file available to us when working with I/O.

Mark Hennessy CS351 Dept Computer Science NUI Maynooth29 Checking the Equality of Types Checking the value of builtin types is trivial: Use bitwise comparison for example. How should two strings be compared? 1. Occupy the same bits over their full length? 2. Contain the same sequence of characters? 3. Check they print the same ``value’’? It all depends on the distinction between the l- and r-values. Are we using the value or reference model. In the reference model, we can make two kinds of checks: 1. Are they both referring (pointing) to the same object? 2. Are they both referring to the same value within the object they refer to? The first option is known as a shallow comparison. The second is a deep comparison.