Types-2. Type Equivalence

Similar documents
Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

Types. What is a type?

On Orthogonality. by remembering only m + n things, we get m * n capabilities.

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CSE 431S Type Checking. Washington University Spring 2013

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Data Types In Text: Ch C apter 6 1

CS 230 Programming Languages

Lab 3. Pointers Programming Lab (Using C) XU Silei

Chapter 6. Structured Data Types. Topics. Structured Data Types. Vectors and Arrays. Vectors. Vectors: subscripts

EL2310 Scientific Programming

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CSE 307: Principles of Programming Languages

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Arrays and Pointers in C. Alan L. Cox

Tokens, Expressions and Control Structures

Inheritance, Polymorphism and the Object Memory Model

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

CS558 Programming Languages

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

22c:111 Programming Language Concepts. Fall Types I

CSCI 171 Chapter Outlines

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Organization of Programming Languages CS3200 / 5200N. Lecture 06

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Programming Languages

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Programming in C - Part 2

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Chapter 8 :: Composite Types

COS 140: Foundations of Computer Science

CSc 520. Principles of Programming Languages 25: Types Introduction

Short Notes of CS201

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Lectures 5-6: Introduction to C

Midterm CSE 131B Spring 2005

CS201 - Introduction to Programming Glossary By

G Programming Languages - Fall 2012

CS 61c: Great Ideas in Computer Architecture

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

A Fast Review of C Essentials Part I

CSC 533: Organization of Programming Languages. Spring 2005

CS558 Programming Languages

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

A brief introduction to C programming for Java programmers

Topic IV. Block-structured procedural languages Algol and Pascal. References:

FUNCTIONS POINTERS. Pointers. Functions

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

High Performance Computing in C and C++

CSE 3302 Programming Languages Lecture 5: Control

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

CSCI-GA Scripting Languages

Computer Systems Principles. C Pointers

Homework #3 CS2255 Fall 2012

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CSE 307: Principles of Programming Languages

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

MYcsvtu Notes LECTURE 34. POINTERS

Types and Type Inference

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Lectures 5-6: Introduction to C

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Summer May 18, 2010

Types II. Hwansoo Han

COS 140: Foundations of Computer Science

[0569] p 0318 garbage

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Character Strings. String-copy Example

Programming Languages

Principles of Programming Languages

CS558 Programming Languages

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Fundamentals of Programming Languages

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

CSI33 Data Structures

Types and Type Inference

Object-Oriented Principles and Practice / C++

Type systems. Static typing

CS 376b Computer Vision

Data Structures Unit 02

Low-Level C Programming. Memory map Pointers Arrays Structures

4 Bindings and Scope. Bindings and environments. Scope, block structure, and visibility. Declarations. Blocks. 2004, D.A. Watt, University of Glasgow

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Instantiation of Template class

First of all, it is a variable, just like other variables you studied

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Transcription:

Types-2 Type equivalence Structural Name Algol68 Arrays Array addressing User-defined modes Discriminated unions Dereferencing as type conversion 1 Type Equivalence Structural equivalence SE1: a type name is structurally equivalent to itself SE2: two types are structurally equivalent if they are formed by applying the same type constructor to two structurally equivalent types SE3: after type declaration type n=t the type name n is structurally equivalent to T 2 1

Structural Equivalence Implies same shape type in a user-defined type type S = array [0..99] of char type T = array [0..99] of char typedef struct{ int j, int k, int *ptr}cell; typedef struct{ int n, int m, int *p}element; 3 Other Def ns of Equivalence T: type array [1..20] of int; x,y,w,z,v are all the same type x,y: array [1..20] of int; by structural equivalence w,z : T; v: T; Name equivalence: Types are equivalent which have the same name or are formed by the same type expression. x and y are of same type, w, z,v are of same type, but x and w are of diferent types! 4 2

Fine Points of Type Equiv. C/C++ use structural equivalence for all types except structs type A = record In C and Algol68, A and B not x,y : real; equivalent types because the end; fieldname is part of the type. type B = record z,w : real end; 5 Algol68 Explicitly typed, statically checkable PL Block structured, extensible type system Objects - entities stored in memory during execution of a program Mode of an object - analogous to type 1 is of mode int Variable - considered an L-value declared using the type of objects that can be assigned to it Mode of a variable - int k means that k is of mode ref int and k can store int values. A.Tanenbaum, Tutorial on Algol68, ACM Computing Surveys, June 1976, Vol 8 No 2 in SERC Reading Room 6 3

Examples Algol68 Integer constant, mode int (1) Integer-valued variable, mode ref int (j) Well-typed assignment statement lhs = rhs where lhs is convertible to an object of mode ref int and rhs is convertible to an object of mode int int j,k,l; -- means all variables on list are mode ref int j := 2; -- j can store mode int objects k := j+l; -- + operator on mode int objects k := j; --dereference j to obtain 2 of mode int Strings are primitive 7 Algol68 - Arrays Vector (array) - one dimensional sequence of objects all having same mode [ ] int row of int [, ] real row row of real Array type only includes dimensionality, not bounds [1:12] int month;[1:7] int day; mode row int [0:10,0:10]real matrix; [-4:10,6:9]real table mode row row int Note table and matrix are type equivalent! 8 4

Algol68 - Arrays [1:10] [1:5,1:5] int kinglear; /kinglear is a vector of 10 elements each of which is a row row int array of 25 elements. kinglear[j] is legal wherever [, ]int mode is legal kinglear[j][1,2] is legal wherever int mode is legal kinglear[1,2,3] is ILLEGAL! 9 Array Operations Trimming: yields some cross section of an original Algol68 array (slicing an array into subarrays) Subscripting: limiting 1 dimension to a single index value [1:10]int a,b; [1:20] real x; [1:20,1:20] real xx; b[1:4] := a[1:4] -- assigns 4 elements b:= a -- assigns all of a to b, same as b[1:10]:=a[1:10] xx[4,1:20]:=x --assigns 20 elements to row 4 of xx xx[8:9,7] := x[1:2] --assigns x[1] to xx[8,7] and x[2] to xx[9,7] 10 5

Array Addressing X[low:high] of E bytes each data item. What s the address of X[j]? addr(x) + (j-low) *E <= addr(x) +(high-low)*e Note: addr(x)-low*e is a compile-time constant X[ ] row real (4 bytes each); X[3] is addr(x[0]) + (3-0)*4 = addr(x) + 12 X[0], X[1] is at address X[0]+4, X[2] is at address X[0]+8, etc 11 Array Addressing Assume arrays are stored in row major order y[0,0], y[0,1], y[0,2],, y[1,*], y[2,*], Consider memory a sequence of locations Then if have y[low1:hi1,low2:hi2] in Algol68, location y[j,k] is addr(y[low1,low2]) + (hi2-low2+1)*e*(j-low1) +(k-low2)*e #locs per row #rows in front # elements in row j in of row j front of element [j,k] 12 6

Example y[0:2, 0:5] in Algol68, an int array. Assume row major storage and find address of y[1,3]. address of y[1,3] = addr(y[0,0])+(5-0+1)*4*(1-0)+(3-0)*4 6 elements per row 1 row before row 1 3 elements in row 1 before 3 = addr(y[0,0])+24+12 = addr(y[0,0])+36 Analogous formula holds for column major order. 13 Algol68 - User-defined Modes Similar to typedefs in C, but without pointers mode vector = [1:n] real; mode person = struct (string initials, int age, bool married); mode family = struct(person mom,dad; [1:2]person child); //last is a row of person structs, child[1] and child[2] are two different person structs. mode tree = struct(int value, ref tree right, left); //recursive data structure is well-defined; couldn t put tree field in tree mode, but ref tree field is okay. 14 7

Example person tom := ( tj, 40, true); person mary := ( mah, 37, true); family jones := (mary,tom,(skip,skip)); initials of mom of jones := mhj ; if (age of mom of jones = age of data of jones). Can define new operators on user-defined modes Can initialize such variables Can use component selection Equality comparisons use structural equivalence which includes the fieldnames 15 Type Unions Idea: allow a variable to contain values of different types during execution Pascal: variant records; C, Algol68: unions Problem: with unions, how can we assure type-safe programs? Pascal and C are not safe. Algol68 is type-safe!! Uses discriminated unions Usually all versions of union use same storage 16 8

C: Example union{double f;int j} fi; fi.f =3.14159; printf( %d\n, fi.j);. Means that fi sometimes contains int values and sometimes double values Can check all uses of union variables by runtime check of current type tag, as in PLs with implicit typing Pascal: type tag tells which type value the variant contains but checking it is optional! 17 Discriminated Unions (Algol68) union(int, real,bool) kitchensink; kitchensink := 3; kitchensink := 3.14; kitchensink := true; if random <.5 then kitchensink := 1 else kitchensink := 2.78 fi; case kitchensink in (int j): print (( integer,j)); (real r): print (( real,r)); (bool b): print (( bool,b)); esac; 18 9

Problems with Unions What is meaning of assignment to tag field without assignment to variant fields? Ada: must change both value and tag together If tag not kept in record itself (Pascal), how can its value be checked? Should tag fields be required to be initialized? Component selection has to be runtime checked 19 Why use Unions? Simulate a logical shift on sequence of bits using a multiply or divide by 2 on an integer To allow structs with an initial portion that has essential information, followed by properties that vary per person represented (e.g., has_children, #children, married, spouse_name) 20 10

Ref Mode Algol68 integer variables are of mode ref int (reference to integer) Assignment statement ref int mode object := mode int object Either side of assignment may need dereferencing to obtain right mode. (However, implicit dereferencing of the rhs is not performed in Algol68) Algol68 does allow more than one automatic dereference on rhs, whereas Pascal and C each allow only 1. int j,k; j:=k; dereference k to get mode int object 21 Legal Assignments Let level be #refs in mode of a variable declarations: int j; ref int p; j is ref int mode, p is ref ref int mode level(3) = 0; level(j) = 1; level(p) = 2 Consider an assignment lhs := rhs level(lhs) <= level(rhs)+1 for a legal assignment int j,k; ref int q; j:=k;k:=q;q:=j;q:=1; --level(j)=level(k)=1, so level(j)=1 <= 1+1 okay --level(k)=1,level(q)=2, so level(k) <=2+1 okay --level(q)=2,level(j)=1, so level(q)=2 <= 1 + 1 okay --level(q)=2,level(1)=0, so is 2<= 0+1? NO 22 11

Example int j,k; ref int ptr, sptr; ref ref int pp; ptr := j; --level(ptr)=2,level(j)=1, 2=1+1 no deref ptr j j := pp; k := j; --level(j)=1,level(pp)=3, 1<3+1=4, dereference necessary pp 4 4 j --level(j)=1, level(k)=1, 1< 1+1 dereference necessary 4 4 k j 23 Example int x,y; ref int rx,ry; ref ref int rrx,rry; x y x:=1; y:=2; 1 2 x:= y; --1 deref needed x y 1 1 2 2 rx := y; --no deref needed 2 1 rx rrx := rx; --no deref needed 3 2 rrx rry := y; --illegal! 3 1 3 <= 1+1=2 NO! ry := rrx; --2 derefs needed 2 3 rrx rx x y ry 2 2 24 12

Ref Mode What would happen in a PL which did no automatic dereferencing on the rhs of assignments? You would need to explicitly encode all dereferences Can you think of an assignment that is legal in Algol68 but not in C? C int * pointers correspond to ref ref int variables & operator prevents automatic dereferencing on rhs of assignment 25 Allowing LHS Dereferences In C, int *rx;int y;*rx =y; is legal; Can we achieve this in Algol68 without an explicit dereference operator? ref int rx; int y rx := y might have 2 interpretations 1. no dereferencing on lhs rx y before: after: 2. deref both lhs, rhs once 1 1 rx y y Which is meant? Can t tell. 1 Need to use an explicit cast x x ref int (rx):=y forces rx, a mode ref ref int variable,to be dereferenced once to become a mode ref int variable 26 13

Allowing LHS Dereferences Another example rrx := rx --3 interpretations possible 1. no dereferences 2. 1 deref of rhs and lhs 3. 2 derefs of rhs and lhs 2 derefs rrx ry rx rrx 2 2 x ry y rx no deref rrx rx 1 deref 2 rrx rx x ry y y ref ref int (rrx) := rx ref int (rrx) := rx 2 x 27 14