Programming Language Concepts, CS2104 Lecture 3

Similar documents
Declarative Computation Model. Conditional. Case statement. Procedure values (2) Procedure values. Sequential declarative computation model

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Programming Language Concepts, cs2104 Tutorial 1. Answers

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Accumulators. A Trivial Example in Oz. A Trivial Example in Prolog. MergeSort Example. Accumulators. Declarative Programming Techniques

Declarative Programming Techniques

Declarative Programming Techniques

Programming with classical quantum datatypes

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

Register machines L2 18

Principles of Program Analysis: Control Flow Analysis

Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2)

Roy L. Crole. Operational Semantics Abstract Machines and Correctness. University of Leicester, UK

THEORY OF COMPILATION

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

CFGs and PDAs are Equivalent. We provide algorithms to convert a CFG to a PDA and vice versa.

Part I: Definitions and Properties

Models of Computation, Recall Register Machines. A register machine (sometimes abbreviated to RM) is specified by:

INTRODUCTION TO PREDICATE LOGIC HUTH AND RYAN 2.1, 2.2, 2.4

Lecture 16: Computation Tree Logic (CTL)

Axiomatic Semantics. Lecture 9 CS 565 2/12/08

Denotational semantics

Reasoning About Imperative Programs. COS 441 Slides 10b

T Reactive Systems: Temporal Logic LTL

ITI Introduction to Computing II

Compiling Techniques

(a) Definition of TMs. First Problem of URMs

Theory of Computation

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

CS522 - Programming Language Semantics

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

The Expressivity of Universal Timed CCP: Undecidability of Monadic FLTL and Closure Operators for Security

Context-Free Languages

ITI Introduction to Computing II

V Honors Theory of Computation

7. The Recursion Theorem

Design of Distributed Systems Melinda Tóth, Zoltán Horváth

Computer Science Introductory Course MSc - Introduction to Java

GOTO the past / programs choose their own adventure. CSE 505: Programming Languages

7. The Recursion Theorem

Introduction to lambda calculus Part 2

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

7. The Recursion Theorem

Multicore Semantics and Programming

COP-5555 PROGRAMMING LANGUAGEPRINCIPLES NOTES ON THE MECHANICAL EVALUATION OF APPLICATIVE EXPRESSSIONS

CS422 - Programming Language Design

Lecture Notes on Inductive Definitions

Dynamic Semantics. Dynamic Semantics. Operational Semantics Axiomatic Semantics Denotational Semantic. Operational Semantics

COSE312: Compilers. Lecture 17 Intermediate Representation (2)

CS357: CTL Model Checking (two lectures worth) David Dill

Recitation 2: Binding, Semantics, and Safety : Foundations of Programming Languages

CISC4090: Theory of Computation

CSC 7101: Programming Language Structures 1. Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11.

Communicating Parallel Processes. Stephen Brookes

Operational Semantics

Constructors - Cont. must be distinguished by the number or type of their arguments.

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) October,

Parallelism and Machine Models

Data Structures and Algorithms Chapter 2

Introduction to Theory of Computing

Advanced Topics in LP and FP

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

Accept or reject. Stack

Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

Static Program Analysis

Turing machines and linear bounded automata

Axiomatic Semantics. Stansifer Ch 2.4, Ch. 9 Winskel Ch.6 Slonneger and Kurtz Ch. 11 CSE

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Functional Big-step Semantics

Introduction to Logic in Computer Science: Autumn 2006

Classical Program Logics: Hoare Logic, Weakest Liberal Preconditions

Automata-Theoretic Model Checking of Reactive Systems

Lecture Notes: Program Analysis Correctness

Compiling Techniques

G54FOP: Lecture 17 & 18 Denotational Semantics and Domain Theory III & IV

Lambda Calculus! Gunnar Gotshalks! LC-1

Functional Programming with F# Overview and Basic Concepts

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Programming Languages

Syntax Analysis Part I

Turing machines and linear bounded automata

Syntax Analysis Part I

Brief Introduction to Prolog

Syntactic Analysis. Top-Down Parsing

Intro to Theory of Computation

The Turing machine model of computation

The L Machines are very high-level, in two senses:

Automata Theory (2A) Young Won Lim 5/31/18

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Asymptotic Algorithm Analysis & Sorting

How to Pop a Deep PDA Matters

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Chapter 2 Propositional Logic: Formulas, Models, Tableaux

The Lifting Lemma. Ralf Hinze

Chapter 6. Properties of Regular Languages

Lambda-Calculus (cont): Fixpoints, Naming. Lecture 10 CS 565 2/10/08

CSE 105 THEORY OF COMPUTATION

Lecture Notes on The Curry-Howard Isomorphism

Theory of Computation

Transcription:

Programming Language Concepts, CS2104 Lecture 3 Statements, Kernel Language, Abstract Machine 31 Aug 2007 CS2104, Lecture 3 1

Reminder of last lecture Programming language definition: syntax, semantics CFG, EBNF Data structures simple: integers, floats, literals compound: records, tuples, lists Kernel language linguistic abstraction data types variables and partial values unification 31 Aug 2007 CS2104, Lecture 3 2

Overview Some Oz concepts Pattern matching Tail recursion Lazy evaluation Kernel language statements and expressions Kernel language semantics Use operational semantics Aid programmer in reasoning and understanding The model is a sort of an abstract machine, but leaves out details about registers and explicit memory address Aid implementer to do an efficient execution on a real machine 31 Aug 2007 CS2104, Lecture 3 3

Pattern-Matching on Numbers fun {Fact N} case N of 0 then 1 [] N then N*{Fact (N-1)} 31 Aug 2007 CS2104, Lecture 3 4

Pattern Matching on Structures fun {Depth T} case Xs of leaf(value:_) then 1 [] node(left:l right:r value:_) then 1+{Max {Depth L} {Depth R}} 31 Aug 2007 CS2104, Lecture 3 5

Compared to Conditional fun {SumList Xs} case Xs of nil then 0 [] X Xr then X + {SumList Xr} Using only Conditional fun {SumList Xs} if {Label Xs}== nil then 0 elseif {Label Xs}== andthen {Width Xs}==2 then Xs.1 +{SumList Xs.2} 31 Aug 2007 CS2104, Lecture 3 6

Linear Recursion fun {Fact N} case N of 0 then 1 [] N then N * {Fact (N-1)} {Fact 3} Ÿ 3*{Fact 2} Ÿ 3*(2*{Fact 1}) Ÿ 3*(2*(1*{Fact 0})) Ÿ 3*(2*(1*1)) Ÿ 3*(2*1) Ÿ 3*2 Ÿ 6 going down recursion return from recursion 31 Aug 2007 CS2104, Lecture 3 7

Accumulating Parameter fun {Fact N } {FactT N 1} Accumulating Parameter fun {FactT N Acc} case N of 0 then Acc [] N then {FactT (N-1) N*Acc} 31 Aug 2007 CS2104, Lecture 3 8

Accumulating Parameter {Fact 3} Ÿ {FactT 3 1} Ÿ {FactT 2 3*1} Ÿ {FactT 2 3}) Ÿ {FactT 1 2*3} Ÿ {FactT 1 6} Ÿ {FactT 0 1*6} Ÿ {FactT 0 6} Ÿ 6 going down recursion and accumulating result in parameter Accumulating Parameter = Tail Recursion = Loop! 31 Aug 2007 CS2104, Lecture 3 9

Tail Recursion = Loop fun {FactT N Acc} case N of 0 then Acc [] N then N=N-1 Acc=N*Acc jump {FactT N Acc} Last call = Tail call 31 Aug 2007 CS2104, Lecture 3 10

Lazy Evaluation Infinite list of numbers! fun lazy {Ints N} N {Ints N+1} {Ints 2} Ÿ 2 {Ints 3} Ÿ 2 (3 {Ints 4}) Ÿ 2 (3 (4 {Ints 5})) Ÿ 2 (3 (4 (5 {Ints 6}))) Ÿ 2 (3 (4 (5 (6 {Ints 7})))) : What if we were to compute : {SumList {Ints 2}}? 31 Aug 2007 CS2104, Lecture 3 11

Taking first N elements of List fun {Take L N} if N<=0 then nil else case L of nil then nil [] X Xs then X {Take Xs (N-1)} {Take [a b c d] 2} Ÿ a {Take [b c d] 1} Ÿ a b {Take [c d] 0} Ÿ a b nil {Take {Ints 2} 2} Ÿ? 31 Aug 2007 CS2104, Lecture 3 12

Eager Evaluation Loop as Infinite list {Take {Ints 2} 2} eagerly evaluated! Ÿ {Take 2 {Ints 3} 2} Ÿ {Take 2 (3 {Ints 4}) 2} Ÿ {Take 2 (3 (4 {Ints 5}))} 2} Ÿ {Take 2 (3 (4 (5 {Ints 6}))) 2} Ÿ {Take 2 (3 (4 (5 (6 {Ints 7})))) 2} : 31 Aug 2007 CS2104, Lecture 3 13

Lazy Evaluation Evaluate the lazy argument only as needed {Take {Ints 2} 2} Ÿ {Take 2 {Ints 3} 2} Ÿ 2 {Take {Ints 3} 1} Ÿ 2 {Take 3 {Ints 4} 1} Ÿ 2 (3 {Take {Ints 4} 0}) Ÿ 2 (3 nil) terminates despite infinite list 31 Aug 2007 CS2104, Lecture 3 14

Kernel Concepts Single-assignment store Environment Semantic statement Execution state and Computation Statements Execution for: skip and sequential composition variable declaration store manipulation conditional 31 Aug 2007 CS2104, Lecture 3 15

Procedure Declarations Kernel language x² = proc{$ y 1 ² y n ²} s² is a legal statement binds x² to procedure value declares (introduces a procedure) Familiar syntactic variant proc{ x² y 1 ² y n ²} s² introduces (declares) the procedure x² A procedure declaration is a value, whereas a procedure application is a statement! 31 Aug 2007 CS2104, Lecture 3 16

What Is a Procedure? It is a value of the procedure type. Java: methods with void as return type declare X = proc {$ Y} {X 3} {Browse X} {Browse 2*Y} But how to return a result (as parameter) anyway? Idea: use an unbound variable $ is the nesting operator Why: we can supply its value after we have computed it! 6 <P/1 X> 31 Aug 2007 CS2104, Lecture 3 17

Operations on Procedures Three basic operations: Defining them (with proc statement) Calling them (with { } notation) Testing if a value is a procedure {IsProcedure P} returns true if P is a procedure, and false otherwise declare X = proc {$ Y} {Browse 2*Y} {Browse {IsProcedure X}} 31 Aug 2007 CS2104, Lecture 3 18

Towards Computation Model Step One: Make the language small Transform the language of function on partial values to a small kernel language Kernel language procedures no functions records no tuple syntax no list syntax local declarations no nested calls no nested constructions 31 Aug 2007 CS2104, Lecture 3 19

From Function to Procedure fun {Sum Xs} case Xs of nil then 0 [] X Xr then X+{Sum Xr} Introduce an output parameter for procedure proc {SumP Xs N} case Xs of nil then N=0 [] X Xr then N=X+{Sum Xr} 31 Aug 2007 CS2104, Lecture 3 20

Why we need local statements? proc {SumP Xs N} case Xs of nil then N=0 [] X Xr then local M in {SumP Xr M} N=X+M Local declaration of variables supported. Needed to allow kernel language to be based entirely on procedures 31 Aug 2007 CS2104, Lecture 3 21

How N was actually transmitted? Having the call {SumP [1 2 3] C}, the identifier Xs is bound to [1 2 3] and C is unbound. At the callee of SumP, whenever N is being bound, so will be C. This way of passing parameters is called call by reference. Procedures output are passed as references to unbound variables, which are bound inside the procedure. 31 Aug 2007 CS2104, Lecture 3 22

Local Declarations local X in Introduces the variable identifier X visible between in and called scope of the variable/declaration Creates a new store variable Links environment identifier to store variable 31 Aug 2007 CS2104, Lecture 3 23

Abbreviations for Declarations Kernel language just one variable introduced at a time no assignment when first declared Oz language syntax supports: several variables at a time variables can be also assigned (initialized) when introduced 31 Aug 2007 CS2104, Lecture 3 24

Transforming Declarations Multiple Variables local X Y in statement² Ö local X in local Y in statement² 31 Aug 2007 CS2104, Lecture 3 25

Transforming away Declarations Initialization local in X= expression² statement² Ö local X in X= expression² statement² 31 Aug 2007 CS2104, Lecture 3 26

Transforming Expressions Replace function calls by procedure calls Use local declaration for intermediate values Order of replacements: left to right innermost first it is different for record construction: outermost first Left associativity: 1+2+3 means((1+2)+3) Right associativity:a b X means(a (b X)), so build the first, then the second 31 Aug 2007 CS2104, Lecture 3 27

Function Call to Procedure Call X={F Y} Ö {F Y X} 31 Aug 2007 CS2104, Lecture 3 28

Replacing Nested Calls {P {F X Y} Z} Ö local U1 in {F X Y U1} {P U1 Z} 31 Aug 2007 CS2104, Lecture 3 29

Replacing Nested Calls {P {F {G X} Y} Z} Ö local U2 in local U1 in {G X U1} {F U1 Y U2} {P U2 Z} 31 Aug 2007 CS2104, Lecture 3 30

Replacing Conditionals if X>Y then else Ö local B in B = (X>Y) if B then else 31 Aug 2007 CS2104, Lecture 3 31

Expressions to Statements X = if B then else Ö if B then X = else X = 31 Aug 2007 CS2104, Lecture 3 32

Functions to Procedures: Length (0) fun {Length Xs} case Xs of nil then 0 [] X Xr then 1+{Length Xr} 31 Aug 2007 CS2104, Lecture 3 33

Functions to Procedures: Length (1) proc {Length Xs N} N=case Xs of nil then 0 [] X Xr then 1+{Length Xr} Make it a procedure 31 Aug 2007 CS2104, Lecture 3 34

Functions to Procedures: Length (2) proc {Length Xs N} case Xs of nil then N=0 [] X Xr then N=1+{Length Xr} Expressions to statements 31 Aug 2007 CS2104, Lecture 3 35

Functions to Procedures: Length (3) proc {Length Xs N} case Xs of nil then N=0 [] X Xr then local U in {Length Xr U} N=1+U Replace function call by its corresponding proc call. 31 Aug 2007 CS2104, Lecture 3 36

Functions to Procedures: Length (4) proc {Length Xs N} case Xs of nil then N=0 [] X Xr then local U in {Length Xr U} {Number. + 1 U N} Replace operation (+, dot-access, <, >, ): procedure! 31 Aug 2007 CS2104, Lecture 3 37

Kernel Language Statement Syntax s² denotes a statement s² ::=skip empty statement x² = y² variable-variable binding x² = v² variable-value binding s 1 ² s 2 ² sequential composition local x² in s 1 ² declaration if x² then s 1 ² else s 2 ² conditional { x² y 1 ² y n ² } procedure application case x² of pattern² then s 1 ² else s 2 ² pattern matching v² ::=... value expression pattern² ::=... 31 Aug 2007 CS2104, Lecture 3 38

Abstract Machine Environment maps variable identifiers to store entities Semantic statement is a pair of: statement environment Execution state is a pair of: stack of semantic statements single assignment store Computation is a sequence of execution states An abstract machine performs a computation 31 Aug 2007 CS2104, Lecture 3 39

Single Assignment Store Single assignment store V set of store variables partitioned into sets of variables that are equivalent but unbound variables bound to a value (number, record or procedure) Example store {x 1, x 2 =x 3, x 4 =a x 2 } x 1 unbound x 2, x 3 equal and unbound x 4 bound to partial value a x 2 31 Aug 2007 CS2104, Lecture 3 40

Environment Environment E maps variable identifiers to entities in store V written as set of pairs X x identifier X store variable x Example of environment: { X x, Y y } maps identifier X to store variable x maps identifier Y to store variable y 31 Aug 2007 CS2104, Lecture 3 41

Environment and Store Given: environment E, store V Looking up value for identifier X: find store variable in environment using E(X) take value from V for E(X) Example: V={x 1, x 2 =x 3, x 4 =a x 2 } E = { X x 1, Y x 4 } E(X) = x 1 where no information in V on x 1 E(Y) = x 4 where V binds x 4 to a x 2 31 Aug 2007 CS2104, Lecture 3 42

Calculating with Environments Program execution looks up values assume store V given identifier x² E( x²) is the value of x² in store V Program execution modifies environments for example: declaration add mappings for new identifiers overwrite existing mappings restrict mappings on sets of identifiers 31 Aug 2007 CS2104, Lecture 3 43

Environment Adjunction Given: Environment E then E + { x² 1 x 1,, x² n x n } is a new environment E with mappings added: always take store entity from new mappings might overwrite (or shadow) old mappings 31 Aug 2007 CS2104, Lecture 3 44

Environment Projection Given: Environment E E { x² 1,, x² n } is a new environment E where only mappings for { x² 1,, x² n } are retained from E 31 Aug 2007 CS2104, Lecture 3 45

Adjunction Example E 0 = { Y² 1 } E 1 = E 0 + { X² 2 } corresponds to { X² 2, Y² 1 } E 1 ( X²) = 2 E 2 = E 1 + { X² 3 } corresponds to { X² 3, Y² 1 } E 2 ( X²) = 3 31 Aug 2007 CS2104, Lecture 3 46

Why Adjunction? E 0 local X in X = 2 local X in X = 3 { Y} E 2 E 1 E 0 31 Aug 2007 CS2104, Lecture 3 47

Semantic Statements Semantic statement ( s², E ) pair of (statement, environment) To actually execute statement: environment to map identifiers modified with execution of each statement each statement has its own environment store to find values all statements modify same store single store 31 Aug 2007 CS2104, Lecture 3 48

Stacks of Statements Execution maintains stack of semantic statements ST = [( s² 1, E 1 ),, ( s² n, E n )] always topmost statement ( s² 1, E 1 ) executes first <s> is statement E denotes the environment mapping rest of stack: remaining work to be done Also called: semantic stack 31 Aug 2007 CS2104, Lecture 3 49

Execution State Execution state ( ST, V ) pair of ( semantic stack, store ) Computation (ST 1, V 1 ) Ö (ST 2, V 2 ) Ö (ST 3, V 3 ) Ö sequence of execution states 31 Aug 2007 CS2104, Lecture 3 50

Program Execution Initial execution state ( [( s², )], ) empty store stack with semantic statement [( s², )] single statement s², empty environment At each execution step pop topmost element of semantic stack execute according to statement If semantic stack is empty, then execution stops 31 Aug 2007 CS2104, Lecture 3 51

Semantic Stack States Semantic stack can be in following states terminated stack is empty runnable can do execution step susped stack not empty, no execution step possible Statements non-susping can always execute susping need values from store dataflow behavior 31 Aug 2007 CS2104, Lecture 3 52

Summary up to now Single assignment store V Environments E adjunction, projection E + { } E { } Semantic statements ( s², E) Semantic stacks [( s², E) ] Execution state (ST, V) Computation = sequence of execution states Program execution runnable, terminated, susped Statements susping, non-susping 31 Aug 2007 CS2104, Lecture 3 53

Statement Execution Simple statements skip and sequential composition variable declaration store manipulation Conditional (if statement) Computing with procedures (later lecture) lexical scoping closures procedures as values procedure call 31 Aug 2007 CS2104, Lecture 3 54

Simple Statements s² denotes a statement s² ::= skip empty statement x² = y² variable-variable binding x² = v² variable-value binding s 1 ² s 2 ² sequential composition local x² in s 1 ² declaration if x² then s 1 ² else s 2 ² conditional v² ::=... value expression (no procedures here) 31 Aug 2007 CS2104, Lecture 3 55

Executing skip Execution of semantic statement (skip, E) Do nothing means: continue with next statement non-susping statement 31 Aug 2007 CS2104, Lecture 3 56

Executing skip (skip, E) ST + V ST + V No effect on store V Non-susping statement 31 Aug 2007 CS2104, Lecture 3 57

Executing skip (skip, E) ST + V ST + V Remember: topmost statement is always popped! 31 Aug 2007 CS2104, Lecture 3 58

Executing Sequential Composition Semantic statement is ( s² 1 s² 2, E) Push in following order s² 2 executes after s² 1 executes next Statement is non-susping 31 Aug 2007 CS2104, Lecture 3 59

Sequential Composition ( s² 1, E) ( s² 1 s² 2, E) ( s² 2, E) ST + V ST + V Decompose statement sequences environment is given to both statements 31 Aug 2007 CS2104, Lecture 3 60

Executing local Semantic statement is (local x²in s², E) Execute as follows: create new variable y in store create new environment E = E + { x² y} push ( s², E ) Statement is non-susping 31 Aug 2007 CS2104, Lecture 3 61

Executing local (local x² in s², E) ( s², E ) ST + V ST + y V With E = E + { x² y} 31 Aug 2007 CS2104, Lecture 3 62

Variable-Variable Equality Semantic statement is ( x² = y², E) Execute as follows bind E( x²) and E( y²) in store Statement is non-susping 31 Aug 2007 CS2104, Lecture 3 63

Executing Variable-Variable Equality ( x² = y², E) ST + V ST + V V is obtained from V by binding E( x²) and E( y²) in store 31 Aug 2007 CS2104, Lecture 3 64

Variable-Value Equality Semantic statement is ( x² = v², E) where v² is a number or a record (procedures will be discussed later) Execute as follows create a variable y in store and let y refers to value v² any identifier z² from v² is replaced by E( z²) bind E( x²) and y in store Statement is non-susping 31 Aug 2007 CS2104, Lecture 3 65

Executing Variable-Value Equality ( x² = v², E) ST + V ST + y V y refers to value v² Store V is modified into V such that: any identifier z² from v² is replaced by E( z²) bind E( x²) and y in store V 31 Aug 2007 CS2104, Lecture 3 66

Susping Statements All statements so far can always execute non-susping (or immediate) Conditional? requires condition x² to be bound variable activation condition: x² is bound (determined) 31 Aug 2007 CS2104, Lecture 3 67

Executing if Semantic statement is (if x²then s² 1 else s² 2, E) If the activation condition bound( x²) is true if E( x²) bound to true push s² 1 if E( x²) bound to false push s² 2 otherwise, raise error Otherwise, susp the if statement 31 Aug 2007 CS2104, Lecture 3 68

Executing if If the activation condition bound( x²) is true if E( x²) bound to true (if x² then s² 1 else s² 2, E) ST + V ( s² 1, E) ST + V 31 Aug 2007 CS2104, Lecture 3 69

Executing if If the activation condition bound( x²) is true if E( x²) bound to false (if x² then s² 1 else s² 2, E) ST + V ( s² 2, E) ST + V 31 Aug 2007 CS2104, Lecture 3 70

An Example local X in local B in B=true if B then X=1 else skip We can reason that X will be bound to 1 31 Aug 2007 CS2104, Lecture 3 71

Example: Initial State ([(local X in local B in B=true if B then X=1 else skip, )], ) Start with empty store and empty environment 31 Aug 2007 CS2104, Lecture 3 72

Example: local ([(local B in B=true if B then X=1 else skip, {X x})], {x}) Create new store variable x Continue with new environment 31 Aug 2007 CS2104, Lecture 3 73

Example: local ([(B=true if B then X=1 else skip, {B b, X x})], {b,x}) Create new store variable b Continue with new environment 31 Aug 2007 CS2104, Lecture 3 74

Example: Sequential Composition ([(B=true, {B b, X x}), (if B then X=1 else skip, {B b, X x})], {b,x}) Decompose to two statements Stack has now two semantic statements 31 Aug 2007 CS2104, Lecture 3 75

Example: Variable-Value Assignment ([(if B then X=1 else skip, {B b, X x})], {b=true, x}) Environment maps B to b Bind b to true 31 Aug 2007 CS2104, Lecture 3 76

Example: if ([(X=1, {B b, X x})], {b=true, x}) Environment maps B to b Bind b to true Because the activation condition bound( x²) is true, continue with then branch of if statement 31 Aug 2007 CS2104, Lecture 3 77

Example: Variable-Value Assignment ([], {b=true, x=1}) Environment maps X to x Binds x to 1 Computation terminates as stack is empty 31 Aug 2007 CS2104, Lecture 3 78

Summary up to now Semantic statement execute by popping itself always creating environment local manipulating store local, = pushing new statements local, if Semantic statement can susp activation condition (if statement) read store sequential composition 31 Aug 2007 CS2104, Lecture 3 79

Pattern Matching Semantic statement is (case x² of lit²( feat² 1 : y² 1 feat² n : y² n ) then s² 1 else s² 2, E) It is a susping statement Activation condition is: bound( x²) If activation condition is false, then susp! 31 Aug 2007 CS2104, Lecture 3 80

Pattern Matching Semantic statement is (case x² of lit²( feat² 1 : y² 1 feat² n : y² n ) then s² 1 else s² 2, E) If E( x²) matches the pattern, that is, label of E( x²) is lit² and its arity is [ feat² 1 feat² n ]), then push ( s² 1, E + { y² 1 E( x²). feat² 1,, y² n E( x²). feat² n }) 31 Aug 2007 CS2104, Lecture 3 81

Pattern Matching Semantic statement is (case x² of lit²( feat² 1 : y² 1 feat² n : y² n ) then s² 1 else s² 2, E) If E( x²) does not match pattern, push ( s² 2, E) 31 Aug 2007 CS2104, Lecture 3 82

Pattern Matching Semantic statement is (case x² of lit²( feat² 1 : y² 1 feat² n : y² n ) then s² 1 else s² 2, E) It does not introduce new variables in the store Identifiers y² 1 y² n are visible only in s² 1 31 Aug 2007 CS2104, Lecture 3 83

Executing case If the activation condition bound( x²) is true if E( x²) matches the pattern (case x² of lit²( feat² 1 : y² 1 feat² n : y² n ) then s² 1 else s² 2, E) ST ( s² 1, E + { y² 1 E( x²). feat² 1,, y² n E( x²). feat² n }) + V + V ST 31 Aug 2007 CS2104, Lecture 3 84

Executing case If the activation condition bound( x²) is true if E( x²) does not match the pattern (case x² of lit²( feat² 1 : y² 1 feat² n : y² n ) then s² 1 else s² 2, E) ST ( s² 2, E) + V + V ST 31 Aug 2007 CS2104, Lecture 3 85

Example: case Statement ([(case X of f(x1 X2) then Y = g(x2 X1) else Y = c, {X v1, Y v2})], % Env {v1=f(v3 v4), v2, v3=a, v4=b} % Store ) We declared X, Y, X1, X2 as local identifiers and X=f(v3 v4), X1=a and X2=b What is the value of Y after executing case? 31 Aug 2007 CS2104, Lecture 3 86

Example: case Statement ([(Y = g(x2 X1), {X v1, Y v2, X1 v3, X2 v4}) ], {v1=f(v3 v4), v2, v3=a, v4=b} ) The activation condition bound( x²) is true Remember that X1=a, X2=b 31 Aug 2007 CS2104, Lecture 3 87

Example: case Statement ([], {v1=f(v3 v4), v2=g(v4 v3),v3=a,v4=b} ) Remember Y refers to v2, so Y = g(b a) 31 Aug 2007 CS2104, Lecture 3 88

Summary Kernel language linguistic abstraction data types variables and partial values statements and expressions Computing with procedures (next lecture) lexical scoping closures procedures as values procedure call 31 Aug 2007 CS2104, Lecture 3 89

Reading Suggestions from [van Roy,Haridi; 2004] Chapter 2, Sections 2.1.1-2.3.5, 2.8 Appices B, C, D Exercises 2.9.1-2.9.3, 2.9.13 31 Aug 2007 CS2104, Lecture 3 90