Arrays Using array as an expression, on the right-hand side Assigning to an array.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
Air Force Institute of Technology Electrical and Computer Engineering
Intermediate Code Generation
Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Discrete Mathematics Math 6A Homework 9 Solution.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Recursive Definitions and Structural Induction
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
1 Semantics Q S EMANTICS (Q1,’07) Week 4 Jacob Andersen PhD student
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Types for Positive and Negative Ints Int = {..., -2, -1, 0, 1, 2,... } Pos = { 1, 2,... } (not including zero) Neg = {..., -2, -1 } (not including zero)
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Functional Programming Element of Functional Programming.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
CS 363 Comparative Programming Languages Semantics.
Logic (continuation) Boolean Logic and Bit Operations.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Perform Calculations and Control Flow Telerik Software Academy Learning & Development Team.
Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Language: Set of Strings
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Exercise Determine the output of the following program assuming static and dynamic scoping. Explain the difference, if there is any. object MyClass { val.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Copyright © Cengage Learning. All rights reserved.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Parsing using CYK Algorithm Transform grammar into Chomsky Form: 1.remove unproductive symbols 2.remove unreachable symbols 3.remove epsilons (no non-start.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
1 Melikyan/DM/Fall09 Discrete Mathematics Ch. 7 Functions Instructor: Hayk Melikyan Today we will review sections 7.3, 7.4 and 7.5.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
CS104:Discrete Structures Chapter 2: Proof Techniques.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Soundness of Types Ensuring that a type system is not broken.
CMSC 330: Organization of Programming Languages Operational Semantics.
Type soundness In a more formal way. Proving Soundness of Type Systems Goal of a sound type system: –if the program type checks, then it never “crashes”
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Foundations of Discrete Mathematics Chapter 1 By Dr. Dalia M. Gil, Ph.D.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Notions & Notations - 1ICOM 4075 (Fall 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez Fall 2010 ICOM.
 Type Called bool  Bool has only two possible values: True and False.
Programming Languages and Compilers (CS 421)
Selection (also known as Branching) Jumail Bin Taliba by
Niu Kun Discrete Mathematics Chapter 1 The Foundations: Logic and Proof, Sets, and Functions Niu Kun 离散数学.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
MODULE 4 EQUATIONS AND INEQUALITIES.
Logics for Data and Knowledge Representation
Logic Logic is a discipline that studies the principles and methods used to construct valid arguments. An argument is a related sequence of statements.
Presentation transcript:

Arrays Using array as an expression, on the right-hand side Assigning to an array

Example with Arrays def next(a : Array[Int], k : Int) : Int = { a[k] = a[a[k]] } void

Type Rules (1) variable constant function application plus if assignment while

Type Rules (2) array use array assignment block

Type Rules (3) field use field assignment  c - top-level environment of class C class C { var x: Int; def m(p: Int): Boolean = {…} }  c = { (x, Int), (m, C x Int  Boolean)} method invocation C

Does this program type check? class Rectangle { var width: Int var height: Int var xPos: Int var yPos: Int def area(): Int = { if (width > 0 && height > 0) width * height else 0 } def resize(maxSize: Int) { while (area > maxSize) { width = width / 2 height = height / 2 } } }

Semantics of Types Operational view: Types are named entities –such as the primitive types (Int, Bool etc.) and explicitly declared classes, traits … –their meaning is given by methods they have –constructs such as inheritance establish relationships between classes Mathematically, Types are sets of values –Int = {..., -2, -1, 0, 1, 2,... } –Boolean = { false, true } –Int  Int = { f : Int -> Int | f is computable }

Types as Sets Sets so far were disjoint Sets can overlap Boolean true, false String “Richard” “cat” Int  Int Int  Pos Int Pos (1, 2) Neg (-1) 16 bit class C class F class D class E F extends D, D extends C C ED F C represents not only declared C, but all possible extensions as well

SUBTYPING

Subtyping Subtyping corresponds to subset Systems with subtyping have non-disjoint sets T 1 <: T 2 means T 1 is a subtype of T 2 –corresponds to T 1  T 2 in sets of values Rule for subtyping: analogous to set reasoning In terms of sets Int Pos

Types for Positive and Negative Ints Int = {..., -2, -1, 0, 1, 2,... } Pos = { 1, 2,... } (not including zero) Neg = {..., -2, -1 } (not including zero) Pos <: Int Neg <: Int Pos  Int Neg  Int (y not zero) (x/y well defined) types:sets:

Rules for Neg, Pos, Int

More Rules More rules for division?

Making Rules Useful Let x be a variable var x : Int var y : Int if (y > 0) { if (x > 0) { var z : Pos = x * y res = 10 / z } } type system proves: no division by zero

Subtyping Example def f(x:Int) : Pos = { if (x < 0) –x else x+1 } var p : Pos var q : Int q = f(p) Does this statement type check?

Subtyping Example def f(x:Pos) : Pos = { if (x < 0) –x else x+1 } var p : Int var q : Int q = f(p) Does this statement type check? does not type check

What Pos/Neg Types Can Do def multiplyFractions(p1 : Int, q1 : Pos, p2 : Int, q2 : Pos) : (Int,Pos) { (p1*q1, q1*q2) } def addFractions(p1 : Int, q1 : Pos, p2 : Int, q2 : Pos) : (Int,Pos) { (p1*q2 + p2*q1, q1*q2) } def printApproxValue(p : Int, q : Pos) = { print(p/q) // no division by zero } More sophisticated types can track intervals of numbers and ensure that a program does not crash with an array out of bounds error.

Subtyping and Product Types

Subtyping for Products T 1 <: T 2 implies for all e: So, we might as well add: covariant subtyping for pair types denoted ( T 1, T 2 ) or Pair[T 1, T 2 ] Type for a tuple:

Analogy with Cartesian Product A x B = { (a, b) | a  A, b  B} T1T1 T2T2 T1’T1’ T2’T2’

Subtyping and Function Types

Subtyping for Function Types Consequence: contravariance covariance T 1 <: T 2 implies for all e: ( () ) as if  |- m: T ’ 1 x … x T n ’  T’

A function type is a set of functions (function space) defined as follows: T 1  T 2 = { f|  x. (x  T 1  f(x)  T 2 )} We can prove Function Space as Set contravariance because x  T 1 is left of implication

Proof T 1  T 2 = { f |  x. (x  T 1  f(x)  T 2 )}

Subtyping for Classes Class C contains a collection of methods For class sub-typing, we require that methods named the same are subtypes

Example class C { def m(x : T 1 ) : T 2 = {...} } class D extends C { override def m(x : T’ 1 ) : T’ 2 = {...} } D <: C so need to have (T' 1  T' 2 ) <: (T 1  T 2 ) Therefore, we need to have: T’ 2 <: T 2 (result behaves like the class) T 1 <: T’ 1 (argument behaves opposite)

Mutable and Immutable Fields We view field var f: T as two methods –getF : TT –setF(x:T): void T  void For val f: T (immutable): we have only getF

Could we allow this? class A {} class B extends A {…}B <: A class C { val x : A = … } class D extends C { override val x : B = … } Because B <: A, this is a valid way for D to extend C ( D <: C) Substitution principle: If someone uses z:D thinking it is z:C, the fact that they read z.x and obtain B as a specific kind of A is not a problem.

What if x is a var ? class A {} class B extends A {…} B <: A class C { var x : A = … } class D extends C { override var x : B = …?!? } If we now imagine the setter method (i.e. field assignment), in the first case the setter has type, for D <: C B <: A, because of setter (reading values) (B -> void) void), so by contravariance A <: B Thus A=B

Soundness of Types ensuring that a type system is not broken For every program and every input, if it type checks, it does not break.