CS4450: Principles of Programming Languages

Slides:



Advertisements
Similar presentations
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Chapter 2, Part 1: An interpreter architecture for C-like languages Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall 2010.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 5 LECTURE: DENOTIONAL SEMANTICS OF A SIMPLE LANGUAGE : INTERPRETATION IN HASKELL.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CS 312 Spring 2002 Lecture 16 The Environment Model.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Closure and Environment Compiler Baojian Hua
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Imperative Programming
Chapter 1 - Introduction
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Built-in Data Structures in Python An Introduction.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
CS April 2002 Lazy Evaluation, Thunks, and Streams.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
CS314 – Section 5 Recitation 9
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
Functional Programming
Object Lifetime and Pointers
Computer Organization and Design Pointers, Arrays and Strings in C
Principles of programming languages 12: Functional programming
Learning to Program D is for Digital.
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Introduction to Pointers
Introduction to Pointers
Chapter 9 :: Subroutines and Control Abstraction
Chapter 16 Pointers and Arrays
Introduction (Sections )
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
FP Foundations, Scheme In Text: Chapter 14.
Chapter 16 Pointers and Arrays
6.001 SICP Variations on a Scheme
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Lec17 Structs.
Assignments and Procs w/Params
A simple function.
Pointers and dynamic objects
Scope, Function Calls and Storage Management
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Introduction to Pointers
FUNCTION ||.
Presentation transcript:

CS4450: Principles of Programming Languages Imperative features; reference types Dr William Harrison

Today: Imperative programming i.e., programming with ; := Case Study: imperative programming in ML “references” = “assignable variables” “reference types”: assign-ability tracked in the type system How to interpret “:=” and “;”

Reference types ML has types for “assignable variables” called reference types i.e., in order for a variable to be on the l.h.s. of a “:=”, it must have a reference type References are like pointers, but type-safe

must use “!” to read and “:=“ to write a reference Example Create a reference with “ref” - val x = ref 1; val x = ref 1 : int ref reference type Read a reference with “!” - !x; val it = 1 : int must use “!” to read and “:=“ to write a reference Write a reference with “!” - x := !x + 1; val it = () : unit

Sequencing (e1 ; … ; en ) The arguments to a sequence can be anything (1 ; "hey" ; 3.14); …and the type of the whole sequence is the type of the last thing: val it = 3.14 : real

Reference is like a pointer (int *x), except… There is no explicit allocation/deallocation of memory* ML C - val x = ref 1; val x = ref 1 : int ref x = malloc(sizeof int); No casting (i.e., references are type-safe) C y = (real) *x * …and no possibility of dereferencing a null pointer!

But there is “aliasing” - val p = ref 9; val p = ref 9 : int ref - val q = p; val q = ref 9 : int ref - !p; val it = 9 : int - !q; - p := 5; val it = () : unit - !p; val it = 5 : int - !q;

“( loc := 2 ; loc := !loc + !loc ; !loc )” What is a “side effect”? Heretofore, the entire meaning of a program is its value “(\ x -> x + x) 2” is 4 With imperative features, values tell only part of the story “( loc := 2 ; loc := !loc + !loc ; !loc )” has value 4 …but this expression also involves hidden (aka, “side”) effects

Referential transparency - val f = (fn y => y + y); val f = fn : int -> int - val arg = 2; val arg = 2 : int - f arg; val it = 4 : int same expression same result, no matter how many times it’s eval’d * “fn x =>” in ML is the same as “\ x ->” in Haskell

Side effects negate referential transparency - val x = ref 10; val x = ref 10 : int ref - f (x := !x * !x ; !x ); val it = 200 : int val it = 20000 : int val it = 200000000 : int uncaught exception overflow

What is the type of “x:=e”? What is the Value of an assignment? - x := 1; val it = () : unit why unit?

What is the meaning of “x:=e”? before after … … x x ? 1 x := 1;

What is the meaning of “x:=e”? before after … … x x ? 1 x := 1; It takes a “Store” as input and returns a “Store” as output I.e., it is a function of type “Store  Store”

How could one represent Store in Haskell? … a v …it is something that takes an address (a) and returns a Value (v)

Store takes an address (a) and returns a Value (v) … a v …i.e, it is a function from Addresses to Values

Representing Store There are a number of choices for how we represent Store Think of addresses as variable names i.e., type Loc = String Then Store can be implemented As functions of type Loc  Int type Store = Loc  Int …or as association lists of type type Store = [(Loc,Int)]

Store in Haskell (first pass) type Loc = String data Store = Mem [(Loc,Int)] initStore = Mem []

A really simple language data Imp = Assign Loc Int | Seq Imp Imp c1 = Assign "x” 1 c2 = Assign "x” 2 c3 = Seq c1 c2 These are respectively: x := 1 x := 2 x := 1 ; x := 2

The Imp interpreter exec (Assign l i) (Mem s) = Mem ((l,i) : (dropfst l s)) exec (Seq c1 c2) mem0 = let mem1 = exec c1 mem0 in exec c2 mem1

The Imp interpreter Assignment just adds the “memory cell” to the Store. Think of dropfst as a garbage collector. exec (Assign l i) (Mem s) = Mem ((l,i) : (dropfst l s)) exec (Seq c1 c2) mem0 = let mem1 = exec c1 mem0 in exec c2 mem1 dropfst x [] = [] dropfst x ((y,v):rs) = if x==y then dropfst x rs else (y,v) : dropfst x rs

The Imp interpreter 1st c1, 2nd c2 1st c1, 2nd c2 “;” threads the Store through c1 first then c2 exec (Assign l i) (Mem s) = Mem ((l,i) : (dropfst l s)) exec (Seq c1 c2) mem0 = let mem1 = exec c1 mem0 in exec c2 mem1 1st c1, 2nd c2 1st c1, 2nd c2

Extension: multiple return values We consider two extensions to this language return values – i.e., what if you want to define “+”? errors – i.e., what happens when something goes wrong? e.g., when something isn’t declared.

returning multiple values Currently, exec : Imp -> Store -> Store How would we add arithmetic? exec (Litint i) memi = ? exec (Var x) memi = ? exec (Add i1 i2) memi = ? Want the “?” to be an int, but doesn’t type check. Idea: change exec to return two values exec : Imp -> Store -> (Value , Store)

Extending the Abstract Syntax and adding Values Add some new abstract syntax data Imp = Assign Loc Int | Seq Imp Imp | Litint Int | Add Imp Imp | Var String data Value = NilVal | I Int …and values How do we represent this as an Imp? As in ML, expressions can have side effects - val x = ref 1; val x = ref 1 : int ref - (x := 3; !x) + 5; val it = 8 : int Add (Seq (Assign “x” 3)) (Var “x”),Litint 5)

Two cases data Value = NilVal | I Int exec :: Imp -> Store -> (Value, Store) exec (Assign l i) (Mem s) = (NilVal, Mem ((l,i) : (dropfst l s))) … exec (Litint i) mem = (I i, mem) In these cases, the “action” occurs in different components of the returned pair.

All cases exec (Assign l i) (Mem s) = (NilVal, Mem ((l,i) : (dropfst l s))) exec (Litint i) mem = (I i, mem) exec (Seq c1 c2) mem0 = let (_,mem1) = exec c1 mem0 in exec c2 mem1 exec (Add i1 i2) mem = let (I v1, mem1) = exec i1 mem (I v2, mem2) = exec i2 mem1 (I (v1 + v2), mem2) exec (Var x) (Mem m) = (I i, Mem m) where Just i = lookup x m

Example Let xsto = Mem [(“x”,0)], then Imp> exec (Var "x") xsto (I 0,Mem [("x",0)]) Why? exec (Var "x") (Mem [(“x”,0)]) = (I i, Mem [(“x”,0)]) where Just i = lookup “x” [(“x”,0)]) = (I 0, Mem [(“x”,0)])

Summary We defined a simple language for imperative programs: type Loc = String data Imp = Assign Loc Int | Seq Imp Imp c1 = Assign "x” 1 c2 = Assign "x” 2 c3 = Seq c1 c2 data Store = Mem [(Loc,Int)] initsto = Mem [] Storage (aka “memory” or “state”) was defined as lists of memory cells:

Summary Then we defined exec as a function of type Imp->Store->Store dropfst x [] = [] dropfst x ((y,v):rs) = if x==y then dropfst x rs else (y,v) : dropfst x rs exec :: Imp -> Store -> Store exec (Assign l i) (Mem s) = Mem ((l,i) : (dropfst l s)) exec (Seq c1 c2) mem0 = let mem1 = exec c1 mem0 in exec c2 mem1