Lecture 18-19 Data Types and Types of a Language



Similar documents
Tail call elimination. Michel Schinz

Chapter 5 Names, Bindings, Type Checking, and Scopes

: provid.ir

1 The Java Virtual Machine

Semantic Analysis: Types and Type Checking

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Cyclone: A Type-Safe Dialect of C

Semester Review. CSC 301, Fall 2015

C++ Programming Language

The C Programming Language course syllabus associate level

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Practical Generic Programming with OCaml

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Object Oriented Software Design II

Glossary of Object Oriented Terms

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Data Structure with C

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

Data Structure Reverse Engineering

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Chapter 7: Functional Programming Languages

Moving from CS 61A Scheme to CS 61B Java

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Stack Allocation. Run-Time Data Structures. Static Structures

How To Write A Program In Anieme Frontend (For A Non-Programmable Language)

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Informatica e Sistemi in Tempo Reale

CS2210: Compiler Construction. Runtime Environment

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

CSC Software II: Principles of Programming Languages

Fundamentals of Java Programming

Functional Programming

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

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

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Fun with Phantom Types

An Incomplete C++ Primer. University of Wyoming MA 5310

Object-Oriented Databases

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

C++ for Safety-Critical Systems. DI Günter Obiltschnig Applied Informatics Software Engineering GmbH

Evolution of the Major Programming Languages

2! Multimedia Programming with! Python and SDL

Habanero Extreme Scale Software Research Project

Ed. v1.0 PROGRAMMING LANGUAGES WORKING PAPER DRAFT PROGRAMMING LANGUAGES. Ed. v1.0

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

CS143 Handout 18 Summer 2012 July 16 th, 2012 Semantic Analysis

7.1 Our Current Model

Write Barrier Removal by Static Analysis

1 Abstract Data Types Information Hiding

The Needle Programming Language

C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

Chapter 15 Functional Programming Languages

Basic Programming and PC Skills: Basic Programming and PC Skills:

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Android Application Development Course Program

CS 241 Data Organization Coding Standards

Java SE 7 Programming

Java Application Developer Certificate Program Competencies

Programming languages C

Programming Language Concepts: Lecture Notes

Object Oriented Software Design II

C Interview Questions

C++ INTERVIEW QUESTIONS

Tutorial on C Language Programming

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Chapter 2: Elements of Java

AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Programming Language Pragmatics

Unboxed Polymorphic Objects for Functional Numerical Programming

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Java EE Web Development Course Program

Monitoring Java enviroment / applications

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Persistent Binary Search Trees

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Course MS10975A Introduction to Programming. Length: 5 Days

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

The countdown problem

CS 111 Classes I 1. Software Organization View to this point:

Item 7: Prefer Immutable Atomic Value Types

Transcription:

Lecture 18-19 Data Types and Types of a Language April 29, 2014

Data Types and Types of a Language Data, Data Types and Types Type: Generalities Type Systems and Type Safety Type Equivalence, Coercion and Cast Polimorphism: Ad Hoc, Generic and Subtype Data Structures and Expressiveness Memory Allocation and Deallocation

Terminology: Data, Data Types, Types Data: The simplest structure for introducing values in a program Distinctive features are the ways in which they can be used: Computable values; Denotable values; Storable values; Expressible values. They depend on the characteristics of the used language Data Types: Collections of values that are homogeneous in respect of the operations that can be applied on them Distinctive features are the allowed operations that obviously, depend on the used language Types: Categorization Structures that classify uniquely (all) the structures that occur in a program They highlight the use (from the behavioral viewpoint) of each program structure They may mark each program structure in a way that is useful to investigate static properties of the program

Types: Introduction Example The program below is considered a correct program in some languages, including Ansi C int main (int argc, char argv[]){//cosa calcola? int x = 10; char a = e ; printf( Totale da pagare in euro : %2d\n, x + a); return0; } The program terminates computing : Totale da pagare in euro : 111 It should be evident that the programmer has made mistakes Nevertheless, this is the worst situation that can happen in programming The program is wrong but it appears to be correct The program appears correct because: - It has passed the compiler checks, and - Moreover, its computation runs by traversing apparently, legal computation states What means legal computation state?

Types: Generalities What means Legal Computation State? A Legal Computation State of a program = is any state in which it is assumed that the program can traverse during its execution. A legal State may contain exceptions of different nature ( illegal division by 0, array outbounds,...) or Anomalies in the used resources (too many active AR s, AR that consumes too much in time or in dynamic memory) All these anomalies are signalled by the executor Any program can be enriched to recover from these anomalies A Non-legal Computation State (or Stuck) = is any state that no correct execution of the program should reach For instance, the sum of the effective amount, 10, with the integer representing the tag e of the symbol for euros. No program can recognizes such an unexpected situation. Hence, no recover code may be written for the program The program is dangerous: The program must be rewritten

Type System (for Property Investigations It consists of 3 structures: The Domain of the Types (including the basic language types) The (language of) Type Expressions with which new (derived) types can be defined The Rules with which the language associates one type to each structure of a program of the language The type system F1 the Typed Lambda Calculus is shown below

Type System: F1 - NO The system F1 apply to the programs of the Typed Lambda Calculus: If the program passes the checks then the program never gets stuck A program P is correctly typed if and only if one type T exists such that: P : T holds in F1 (i.e. can be obtained by using the rules of F1)

Type Safety or Soundness = Progress + Preservation A language can have a Type System which guarantees that the language programs are Type Safe (Haskell, Ocaml, Java) Progress. It ensures that the execution of a well typed program, [ P : T], never gets stuck. It reaches: [below, = 1 program computation step] Either a final states with the expected values, [P Val]; Or a new legal state, [ P P ] and [ P : T ], for some T. Preservation or Subject Reduction. A well typed program, [ P : T], leaves unchanged its type and that of tits components during the execution [if P P then, P : T and T = T ]. Checking for Type Safety can be done statically (Haskell, Ocaml, Java) at compile time strong typing: Programs that do not pass the check are rejected types are no more useful during computation: then are removed form the object code Checking for Type Safety is done only dynamically (C++), Executor checks the operands for the right type Wrong Programs are stopped only when the type check fails Types are maintained in the object code Checking for Type Safety cannot be done (C) Dangerous program run without anyone noticing

Type Expressions Type Expressions allow the definition of new types: Example using type operators that may include product (record or struct) [ *,in Ocaml], sum (union) [, in Ocaml), map [,in Ocaml]; using type constructor; State = Env * Store //naming, product Env = Ide Den //naming, map Store = M(Loc * Loc Mem) //naming, type constructor, product Enum = A B C //naming, sum, type constructors Example using generic polymorphism, i.e. type expressions with type variables (universally qualified variables, raging over the domain of the types); using naming and recursively defined types; ( a, b)env = a b //type variables, naming, map a list = Cons a ( a list) Nil //type variable, type constructors, sum, recursive types

Relations and Properties on the type structures Equivalence:. Let x:t and y:t : Are x and y of the same type? Nominal: It is equivalent only to itself Structural Same type expression when replacing names by definitions Subtype Explicit (Java) or Implicit (contra-co-variance of functions) Coercion: Conversion of a value representation and consequent change of type; Cast: Type constraint that must be satisfied at run time; Overloading Different types and values for a same (Function) identifier; Subtype Polymorphism In combination with the subtypes: A method also applies to values of subtypes of the types expected.

Data: Generalities Structure: Scalar Data are atomic values: Only operations for computing new values Structured Data: Have in addition, components and operations for visiting (and possibly, modifying) components Mutability Scalar Data are not mutable values, but may be used in mutable values Structured Data may be mutable or not, may have mutable components In functional languages, Structured Data are immutable with immutable components Expressiveness Scalar Data are in general, expressible values In Imperative Languages, Structured Data are not expressible values In functional languages, all data are alway expressible values Ocaml (in addition to scalar data, functions, tuples, and lists) has mutable, arrays and records values: All such data are expressible values Allocation Static: At Compile/Loading Time Dynamic (for intermediate values) in the stack RI, locally to the AR s Heap: With program controlled allocation/deallocation In a pool memory (Heap): With automatic allocation/deallocation In functional languages, is automatic and made transparent to computation.

Dynamic Allocation and Expressiveness - NO Example User Controlled Allocation: The constructor implementation is under user responsibility Automatic Allocation: The constructor implementation is automatically provided by the language struct elem { int hd; struct elem *tl; }; typedef struct elem *list; list Cons(int v, list n){ list r = malloc(sizeof(struct elem)); r->hd = v; r->tl = n; return r; }//User Controlled - in Ansi-C Example type list = NULL Cons of int * list;; //Automatic in Ocaml Expressiveness: Data Constructors furnish a Data Presentation of the values int main (int argc, char *argv[]){ # let r = ref (Cons(3,NULL));; list r = Cons(3,NULL); val r : list ref = {contents = Cons (3, NULL)} r = Cons(2,r); # (r:= Cons(2,!r);!r);; } - : list = Cons (2, Cons (3, NULL))

Controlled and Automatic, Dynamic, Deallocation - NO Controlled Deallocation. The Tombstone Technique: The access to the dynamically allocated structure is through a guard, called Tombstone. They are marked when the structure is released (dispose, free): Dangling References Locks and Keys are used to re-allocate the memory and to recognize accesses from dangling references Automatic Deallocation (Garbage Collector). The Counter Technique: A counter, C(V) is set to 1 when a new structure is created and with reference V (e.g. malloc...) Whenever a copy (i.e. assignment, parameter passing by value,...) with source p s and target p t is made: C(V ps )++, where V ps be the dynamic allocated structure of reference p s; C(V pt )--, where V pt be the dynamic allocated structure of reference p t; With each pop of an AR (end/exit of an inline block, or return/exit of a procedure) We consider all local variables and for each of them, the possibly reference to a dynamically allocated structure or to a component of it. Let {p si } be the set of them. C(V psi )--, for each i If C(V psi ) == 0, then the structure V psi is deallocated.

Excercises Exercise1. a What kind of type equivalence is defined in Ocaml?; b Show an example that confirms it. Exercise2. a What kind of type equivalence is defined in Java?; b Show an example that confirms it. Exercise3. a Write in Ocaml, a type for data representing Activation Records in static languages; b Write in Java, a type for data representing Activation Records in static languages; b Write in C, a type for data representing Activation Records in static languages;