Types and Programming Languages

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 15 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
AE1APS Algorithmic Problem Solving John Drake
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Logic & program control part 2: Simple selection structures.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
CS 355 – Programming Languages
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 4 By Herb I. Gross and Richard A. Medeiros next.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Axiomatic Semantics Dr. M Al-Mulhem ICS
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
MinML: an idealized programming language CS 510 David Walker.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Semantics for MinML COS 441 Princeton University Fall 2004.
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
Ch. 9 Fundamental of Hypothesis Testing
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
C H A P T E R S E V E N Expressions and Assignment Statements.
CS1Q Computer Systems Lecture 8
1 Knowledge Based Systems (CM0377) Lecture 4 (Last modified 5th February 2001)
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS1Q Computer Systems Lecture 6 Simon Gay. Lecture 6CS1Q Computer Systems - Simon Gay2 Algebraic Notation Writing AND, OR, NOT etc. is long-winded and.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
Of 33 lecture 12: propositional logic – part I. of 33 propositions and connectives … two-valued logic – every sentence is either true or false some sentences.
CS 611: Lecture 6 Rule Induction September 8, 1999 Cornell University Computer Science Department Andrew Myers.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Looping and Counting Lecture 3 Hartmut Kaiser
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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Chapter 3 Part II Describing Syntax and Semantics.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS1Q Computer Systems Lecture 6 Simon Gay. Lecture 6CS1Q Computer Systems - Simon Gay2 Algebraic Notation Writing AND, OR, NOT etc. is long-winded and.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Boolean Functions and Boolean Algebra Laxmikant Kale.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CSE 311: Foundations of Computing Fall 2013 Lecture 8: Proofs and Set theory.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CSE-321 Programming Languages Simply Typed -Calculus
Propositional Calculus: Boolean Functions and Expressions
Syntax versus Semantics
Propositional Calculus: Boolean Functions and Expressions
Programming Languages 2nd edition Tucker and Noonan
Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes.
Copyright © Cengage Learning. All rights reserved.
Computer Security: Art and Science, 2nd Edition
ece 720 intelligent web: ontology and beyond
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Types and Programming Languages Lecture 2 Simon Gay Department of Computing Science University of Glasgow 2006/07

Simple Expression Language To introduce the main ideas of the course, we will begin by looking at a simple language of expressions. Abstract syntax of expressions e is defined by this grammar: e ::= integer literal | true | false | e + e | e == e | e & e | if e then e else e Abstract expressions should be understood as trees and we use brackets to make this clear. (1+2)+3 + + 3 1 2 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Types and Programming Languages Lecture 2 - Simon Gay We are going to define the meaning of expressions by specifying how to evaluate them, giving the expected results: e.g. (if 1==2 then 3 else 4)+1 evaluates to 5 . The language contains many nonsensical expressions such as (2==3)+4 which we will eliminate by means of a type system. It’s useful to separate out the integer and boolean literals: we call them values, v. The grammar becomes v ::= integer literal | true | false e ::= v | e + e | e == e | e & e | if e then e else e Sometimes we will refer to expressions as terms. 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions We define the semantics (meaning) of expressions by formalizing step-by-step calculation. e  e’ means that e is transformed into e’ by one step of calculation or evaluation. We say that e reduces to e’.  is called the reduction relation. We write e * e’ if e is transformed into e’ by some number of reduction steps (possibly zero). Examples: 1+(2+3) * 6 if 2==4 then 1 else 3 * 3 if we define  in a suitable way. This approach is called operational semantics. 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions We want + to represent addition. For values: 0 + 1  1 1 + 1  2 and so on… In general: u + v  w if u and v are integer literals and w is their sum 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions Similarly we want == to represent the equality test: u == v  true if u and v are the same integer literal u == v  false if u and v are different integer literals We want & to represent logical conjunction: u & v  w if u and v are boolean literals and w is their logical conjunction 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions Conditional expressions are similar when the condition is a value: if true then e else e’  e if false then e else e’  e’ Notice that in these cases we are not necessarily reducing directly to a value - in general further steps are required. Example: if true then 1+2 else 4  1+2  3 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions Exercise: Give some examples of expressions for which we have not yet specified reductions. Two categories: 1. Expressions which we do not intend to have a value e.g. 1+true if 3 then 4 else 5 2 & 3 We will not define reductions - these expressions are stuck. (Alternative: 1+true  error where error is a new expression.) 2. Sensible expressions which are more complex e.g. 1+(2+3) if 3==4 then 5 else 6 (1==2)&(3==4) We need to deal with these… 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions We define rules allowing part of an expression to be reduced, maintaining the overall structure. Addition: to reduce e+f when e is not a value, first reduce e by one step, giving e’, then we have e’+f. more concisely: if e  e’ then e+f  e’+f notation: general: IF we know that the reduction above the line is possible THEN we conclude that the reduction below the line is possible 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions Exercise: can we reduce 1+(2+3) ? If so, what do we get? Our rule does not apply to 1+(2+3) because 1 is already a value and has no reductions. In general, applying the rule repeatedly leads to an expression of the form v+e so we need a new rule for expressions of this form. Exercise: what should the rule be? Exercise: how does (1+2)+(2+3) reduce? (1+2)+(2+3)  3+(2+3)  3+5  8 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions The rules for + specify an evaluation order: evaluate the left operand first. For + this is not very important, but for conditionals it is: Exercise: how does if 1==2 then 3+4 else 1+2 reduce? if 1==2 then 3+4 else 1+2  if false then 3+4 else 1+2  1+2  3 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Operational semantics of expressions Exercise: what are the rest of the reduction rules for == ? Similarly we can complete the rules for & : Alternatively we can define shortcut evaluation: and then we no longer need the u & v  w rule. 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Summary: operational semantics of expressions u + v  w (R-SumLit) if u and v are integer literals and w is their sum (R-SumL) (R-SumR) u & v  w (R-AndLit) if u and v are boolean literals and w is their conjunction (R-AndL) (R-AndR) 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Summary: operational semantics of expressions u == v  true (R-EqT) if u and v are the same int. lit. u == v  false (R-EqF) if u and v are different int. lits. (R-EqL) (R-EqR) (R-IfT) (R-IfF) (R-IfC) Structural Operational Semantics: due to Gordon Plotkin. 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Summary: operational semantics of expressions The meaning of an expression e is calculated by reducing it: until a stuck expression is reached. Reductions are only possible if they can be derived from the rules. A stuck expression is either a value (representing the meaning of the original expression) or a non-value which cannot be reduced any further (representing a run-time error). 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Types and Programming Languages Lecture 2 - Simon Gay Deriving reductions Formally the existence of each reduction is justified by a derivation using the reduction rules. For example, the reduction if 1==2 then 3+4 else 1+2  if false then 3+4 else 1+2 is justified by 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Types and Programming Languages Lecture 2 - Simon Gay What have we defined? Let Expr be the set of all expressions. We have defined a relation R on Expr: a subset of Expr  Expr. When (e,f)  R we write e  f . R is the smallest relation which is closed under the rules. That’s what we mean by saying that reductions are only those specified by the rules. This is called an inductive definition. The rules are called inference rules. A rule with no hypothesis (e.g. u == v  true) is called an axiom. 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Avoiding run-time errors We will say that e  Expr is valid if whenever it reduces to a stuck expression s, s is a value. Let Valid be the set of all valid expressions: Valid  Expr. In fact, Valid  Expr. (If Valid = Expr: no problem!) The Simple Expression Language is so simple that given e  Expr we can work out whether or not e  Valid simply by reducing e until we get stuck (which is bound to happen) and then examining the stuck expression. Exercise: why doesn’t this work in real programming languages? 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Avoiding run-time errors We would like to define a set of safe expressions, Safe  Expr, such that given an expression e we can work out whether or not e  Safe without reducing e Safe  Valid, so that if e  Safe then e is valid (has no run-time errors) we expect that Safe  Valid but we want to make sure that the gap between Safe and Valid is not too large: if we adopt the principle of rejecting unsafe expressions, then we don’t want to reject too many valid expressions by mistake. The idea is to define a type system, and then Safe will be the set of expressions that are accepted by the typechecker. 2006/07 Types and Programming Languages Lecture 2 - Simon Gay

Types and Programming Languages Lecture 2 - Simon Gay Reading Pierce: 3.1, 3.2, 3.4, 3.5 Exercises Pierce: 3.5.10, 3.5.18 Exercise sheet 1 2006/07 Types and Programming Languages Lecture 2 - Simon Gay