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

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

Programming Language Concepts, CS2104 Lecture 3

Declarative Programming Techniques

Declarative Programming Techniques

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

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

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

Structure. Background. them to their higher order equivalents. functionals in Standard ML source code and converts

Programming Language Concepts, cs2104 Tutorial 1. Answers

Lecture 2: Axiomatic semantics

Pushdown Automata (2015/11/23)

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

Lists, Stacks, and Queues (plus Priority Queues)

Syntax Analysis Part I

Syntax Analysis Part I

T Reactive Systems: Temporal Logic LTL

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

Verification of Recursive Programs. Andreas Podelski February 8, 2012

Programming with classical quantum datatypes

Timo Latvala. February 4, 2004

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

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

Register machines L2 18

A Polymorphic Type and System for Multi-Staged Exceptions

Tute 10. Liam O'Connor. May 23, 2017

Part 1: Propositional Logic

CS422 - Programming Language Design

Principles of Program Analysis: Control Flow Analysis

Lecture Notes on Inductive Definitions

Complete Partial Orders, PCF, and Control

Notes. Corneliu Popeea. May 3, 2013

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

Logic for Computer Science - Week 4 Natural Deduction

7 RC Simulates RA. Lemma: For every RA expression E(A 1... A k ) there exists a DRC formula F with F V (F ) = {A 1,..., A k } and

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

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

Syntax Analysis - Part 1. Syntax Analysis

Syntactic Analysis. Top-Down Parsing

Limitations of OCAML records

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

Introduction to Logic in Computer Science: Autumn 2006

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

Modular Bisimulation Theory for Computations and Values

CISC4090: Theory of Computation

Python. chrysn

A Modular Rewriting Semantics for CML

Space-aware data flow analysis

CPS 220 Theory of Computation Pushdown Automata (PDA)

The syntactic guard condition of Coq

Programming Languages and Types

Denotational Semantics of Programs. : SimpleExp N.

CIS 500: Software Foundations

Syntax Directed Transla1on

Compiling Techniques

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

THEORY OF COMPILATION

CA Compiler Construction

Logic. Propositional Logic: Syntax. Wffs

1. Object Calculus. Object calculus is to OO languages what lambda calculus is to functional languages

Automata-Theoretic LTL Model-Checking

Relative Hilbert-Post completeness for exceptions

Program verification. Hoare triples. Assertional semantics (cont) Example: Semantics of assignment. Assertional semantics of a program

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

Automata theory. An algorithmic approach. Lecture Notes. Javier Esparza

Part I: Definitions and Properties

Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

Propositional Logic. Testing, Quality Assurance, and Maintenance Winter Prof. Arie Gurfinkel

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

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

Process Algebras and Concurrent Systems

MA/CSSE 474 Theory of Computation

Theory of Computation Turing Machine and Pushdown Automata

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

The Assignment Axiom (Hoare)

Hoare Calculus and Predicate Transformers

Axiomatic Semantics. Operational semantics. Good for. Not good for automatic reasoning about programs

And, even if it is square, we may not be able to use EROs to get to the identity matrix. Consider

Temporal & Modal Logic. Acronyms. Contents. Temporal Logic Overview Classification PLTL Syntax Semantics Identities. Concurrency Model Checking

An Introduction to Logical Relations Proving Program Properties Using Logical Relations

Advanced Topics in LP and FP

1 Introduction. 2 First Order Logic. 3 SPL Syntax. 4 Hoare Logic. 5 Exercises

Notes on Inductive Sets and Induction

Information Flow Inference for ML

Propositional Logic: Syntax

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

Lecture Notes on Inductive Definitions

Decentralized Control of Discrete Event Systems with Bounded or Unbounded Delay Communication

Lecture Notes on Compositional Reasoning

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

Chapter 2. Assertions. An Introduction to Separation Logic c 2011 John C. Reynolds February 3, 2011

Reasoning about Trace Properties of Higher-order Programs

Lambda Calculus! Gunnar Gotshalks! LC-1

Propositional logic ( ): Review from Mat 1348

Deductive Verification

Lecture Notes on The Curry-Howard Isomorphism

Outline F eria AADL behavior 1/ 78

Foundations of the X-machine Theory for Testing

Automated Reasoning Lecture 5: First-Order Logic

DFA of non-distributive properties

List of errors in and suggested modifications for First-Order Modal Logic Melvin Fitting and Richard L. Mendelsohn August 11, 2013

Transcription:

Declarative Computation Model Kernel language semantics revisited (VRH.4.5) From kernel to practical language (VRH.6) Exceptions (VRH.7) Carlos Varela RPI October 0, 009 Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL Sequential declarative computation model The kernel language semantics revisited. Suspable statements: if, case, procedure application. Procedure s Procedure introduction Procedure application. C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 1 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy Conditional ( if x then s 1 else s, E) If the activation condition (E( x ) is determined) is true: If E( x ) is not Boolean (true, false), raise an error E( x ) is true, push ( s 1, E) on the stack E( x ) is false, push ( s, E) on the stack If the activation condition (E( x ) is determined) is false: Susp C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 3 Case statement ( case x of l ( f 1 : x 1 f n : x n ) then s 1 else s, E) If the activation condition (E( x ) is determined) is true: If E( x ) is a record, the label of E( x ) is l and its arity is [ f 1 f n ]: push (local x 1 = x. f 1 x n = x. f n in s 1, E) on the stack Otherwise, push ( s, E) on the stack If the activation condition (E( x ) is determined) is false: Susp C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 4 Procedure s Procedure s () Constructing a procedure in the store is not simple because a procedure may have external references P x 1 (, ) local P Q in Q = proc {$} {Browse hello} P = proc {$} {Q} local Q in Q = proc {$} {Browse hi} {P} local P Q in proc {$} {Q} Q x Q = proc {$} {Browse hello} P = proc {$} {Q} local Q in Q = proc {$} {Browse hi} {P} x (, ) proc {$} {Browse hello} Browse x 0 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 5 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 6 1

Procedure s (3) Procedure introduction (proc {$ y ( x = proc {$ y 1... y n } s, E) 1... y n } y 1... y n are the (formal) parameters of the s procedure, Other free identifiers in s are called external references z 1... z k CE) These are defined by the environment E where the procedure is declared (lexical scoping) The contextual environment of the procedure CE is E { z1... z k } When the procedure is called CE is used to construct the environment for execution of s ( x = proc {$ y 1... y n } s, E) Create a contextual environment: CE = E { z1... z k } where z 1... z k are external references in s. Create a new procedure of the form: (proc {$ y 1... y n } s, CE), refer to it by the variable x P Bind the store variable E( x ) to x P Continue to next execution step C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 7 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 8 Procedure application Execution examples ({ x y 1 y n }, E) If the activation condition (E( x ) is determined) is true: If E( x ) is not a procedure, or it is a procedure with arity that is not equal to n, raise an error If E( x ) is (proc {$ z 1... z n } s, CE), push ( s, CE + { z 1 E( y 1 ) z n E( y n )} ) on the stack If the activation condition (E( x ) is determined) is false: Susp s s 3 if X >= Y then Z=X else Z=Y {Max 3 5 C} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 9 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 10 Execution examples () s s 3 if X >= Y then Z=X else Z=Y s 4 {Max 3 5 C} Initial state ([(, )], ) After ( [( s, {Max m, C c})], {m, c} ) After Max binding ( [( s 4, {Max m, C c})], {m = (proc{$ X Y Z} s 3, ), c} ) Execution examples (3) s s 3 if X >= Y then Z=X else Z=Y s 4 {Max 3 5 C} After Max binding ( [( s 4, {Max m, C c})], {m = (proc{$ X Y Z} s 3, ), c} ) After procedure call ( [( s 3, {X t 1, Y t, Z c}) ], {m = (proc{$ X Y Z} s 3, ), t 1 =3, t =5, c} ) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 11 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 1

Execution examples (4) Execution examples (5) s s 3 if X >= Y then Z=X else Z=Y s 4 {Max 3 5 C} s s 3 if X >= Y then Z=X else Z=Y s 4 {Max 3 5 C} After procedure call ( [( s 3, {X t 1, Y t, Z c})], {m = (proc{$ X Y Z} s 3, ), t 1 =3, t =5, c} ) After T = (X>=Y) ( [( s 3, {X t 1, Y t, Z c, T t}) ], {m = (proc{$ X Y Z} s 3, ), t 1 =3, t =5, c, t=false} ) ( [(Z=Y, {X t 1, Y t, Z c, T t}) ], {m = (proc{$ X Y Z} s 3, ), t 1 =3, t =5, c, t=false} ) ( [(Z=Y, {X t 1, Y t, Z c, T t}) ], {m = (proc{$ X Y Z} s 3, ), t 1 =3, t =5, c, t=false} ) ( [ ], {m = (proc{$ X Y Z} s 3, ), t 1 =3, t =5, c=5, t=false} ) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 13 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 14 Procedures with external references Procedures with external references s s 3 if X >= Y then Z=X else Z=Y s s 3 if X >= Y then Z=X else Z=Y The procedure of LB is (proc{$ X Z} s 3, {Y y}) The store is {y = 5, } C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 15 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 16 Procedures with external references Procedures with external references s s 3 if X >= Y then Z=X else Z=Y The procedure of LB is (proc{$ X Z} s 3, {Y y}) The store is {y = 5, } STACK: [( {LB T C}, {Y y, LB lb, C c, T t}) ] STORE: {y = 5, lb = (proc{$ X Z} s 3, {Y y}), t = 3, c} s s 3 if X >= Y then Z=X else Z=Y STACK: [( {LB T C}, {Y y, LB lb, C c, T t}) ] STORE: {y = 5, lb = (proc{$ X Z} s 3, {Y y}), t = 3, c} STACK: [( s 3, {Y y, X t, Z c}) ] STORE: {y = 5, lb = (proc{$ X Z} s 3, {Y y}), t = 3, c} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 17 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 18 3

Procedures with external references s s 3 if X >= Y then Z=X else Z=Y STACK: [( s 3, {Y y, X t, Z c}) ] STORE: {y = 5, lb = (proc{$ X Z} s 3, {Y y}), t = 3, c} STACK: [(Z=Y, {Y y, X t, Z c}) ] STORE: {y = 5, lb = (proc{$ X Z} s 3, {Y y}), t = 3, c} STACK: [ ] STORE: {y = 5, lb = (proc{$ X Z} s 3, {Y y}), t = 3, c = 5} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 19 From the kernel language to a practical language Interactive interface the declare statement and the global environment Ext kernel syntax to give a full, practical syntax nesting of partial s implicit variable initialization expressions nesting the if and case statements andthen and orelse operations Linguistic abstraction Functions Exceptions C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 0 The interactive interface (declare) The interactive interface (declare) The interactive interface is a program that has a single global environment Browse procedure Store declare X Y Augments (and overrides) the environment with new mappings for X and Y R X Y a {Browse X} Inspects the store and shows partial s, and incremental changes Environment b C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 1 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy declare X Y Syntactic extensions Browse F X Y Environment procedure b a unbound unbound x i x i+1 Store Nested partial s person(name: George age:5) local A B in A= George B=5 person(name:a age:b) Implicit variable initialization local pattern = expression in Example: assume T has been defined, then local tree(key:a left:b right:c :D) = T in is the same as: local A B C D in T = tree(key:a left:b right:c :D) <statement> C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 3 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 4 4

Extracting fields in local statement declare T : T = tree(key:seif age:48 profession:professor) : local tree(key:a...) = T in C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 5 Nested if and case statements Observe a pair notation is: 1 #, is the tuple # (1 ) case Xs # Ys of nil # Ys then [] Xs # nil then s [] (X Xr) # (Y Yr) andthen X=<Y then s 3 else s 4 Is translated into case Xs of nil then else case Ys of nil then s else case Xs of X Xr then case Ys of Y Yr then if X=<Y then s 3 else s 4 else s 4 else s 4 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 6 Expressions Functions as linguistic abstraction An expression is a sequence of operations that returns a A statement is a sequence of operations that does not return a. Its effect is on the store, or outside of the system (e.g. read/write a file) 11*11 X=11*11 expression statement R = {F X1... Xn} {F X1... Xn R} fun {F X1... Xn} expression proc {F X1... Xn R} R = expression C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 7 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 8 Nesting in data structures Ys = {F X} {Map Xr F} Is unnested to: local Y Yr in Ys = Y Yr {F X Y} {Map Xr F Yr} The unnesting of the calls occurs after the data structure Functional nesting Nested notations that allows expressions as well as statements local R in {F X1... Xn R} {Q R...} Is written as (equivalent to): {Q {F X1... Xn}...} expression statement C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 9 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 30 5

Conditional expressions Example R = if expr 1 then expr else expr 3 expression fun {Max X Y} if X>=Y then X else Y if expr 1 then R = expr else R = expr 3 proc {Max X Y R} R = ( if X>=Y then X else Y ) fun {Max X Y} if X>=Y then X else Y proc {Max X Y R} if X>=Y then R = X else R = Y proc {Max X Y R} R = ( if X>=Y then X else Y ) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 31 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 3 andthen and orelse Function calls if expr then 1 expr expr 1 andthen expr else false Observe {F1 {F X} {F3 Y}} local R1 R in R1 = {F X} R = {F3 Y} {F1 R1 R} if expr then 1 true expr 1 orelse expr else expr The arguments of a function are evaluated first from left to right C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 33 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 34 A complete example Exceptions fun {Map Xs F} case Xs of nil then nil [] X Xr then {F X} {Map Xr F} proc {Map Xs F Ys} case Xs of nil then Ys = nil [] X Xr then Y Yr in Ys = Y Yr {F X Y} {Map Xr F Yr} How to handle exceptional situations in the program? Examples: divide by 0 opening a nonexistent file Some errors are programming errors Some errors are imposed by the external environment Exception handling statements allow programs to handle and recover from errors C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 35 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 36 6

Exceptions Basic concepts The error confinement principle: Define your program as a structured layers of components Errors are visible only internally and a recovery procedure corrects the errors: either errors are not visible at the component boundary or are reported (nicely) to a higher level In one operation, exit from arbitrary depth of nested contexts Essential for program structuring; else programs get complicated (use boolean variables everywhere, etc.) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 37 A program that encounters an error (exception) should transfer execution to another part, the exception handler and give it a (partial) that describes the error try catch x then s raise x Introduce an exception marker on the semantic stack The execution is equivalent to if it executes without raising an error Otherwise, is aborted and the stack is popped up to the marker, the error is transferred through x, and s is executed C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 38 Exceptions (Example) Exceptions (Example) fun {Eval E} if {IsNumber E} then E else case E of plus(x Y) then {Eval X}+{Eval Y} [] times(x Y) then {Eval X}*{Eval Y} else raise illformedexpression(e) plus times 5 6 4 try {Browse {Eval plus(5 6) }} {Browse {Eval plus(times(5 5) 6) }} {Browse {Eval plus(minus(5 5) 6) }} catch illformedexpression(e) then {System.showInfo **** illegal expresion **** # E} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 39 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 40 Try semantics Raise semantics (try catch y then s, E) Push the semantic statement (catch y then s, E) on ST Push (, E) on ST Continue to next execution step C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 41 (raise x, E) Pop elements off ST looking for a catch statement: If a catch statement is found, pop it from the stack If the stack is emptied and no catch is found, then stop execution with the error message Uncaught exception Let (catch y then s, E c ) be the catch statement that is found Push ( s, E c +{<y> E(<x>)}) on ST Continue to next execution step C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 4 7

Catch semantics (catch x then s, E) Continue to next execution step (like skip) Full exception syntax Exception statements (expressions) with multiple patterns and finally clause Example: : FH = {OpenFile xxxxx } : try {ProcessFile FH} catch X then {System.showInfo ***** Exception when processing ***** # X} finally {CloseFile FH} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 43 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 44 Exercises 53. VRH Exercise.9.3 (page 107). 54. VRH Exercise.9.7 (page 108) translate example to kernel language and execute using operational semantics. 55. Write an example of a program that susps. Now, write an example of a program that never terminates. Use the operational semantics to prove suspension or nontermination. 56. VRH Exercise.9.1 (page 110). 57. Change the semantics of the case statement, so that patterns can contain variable labels and variable feature names. Exercises 58. Restrict the kernel language to make it strictly functional (i.e., without dataflow variables) Language similar to Scheme (dynamically typed functional language) This is done by disallowing variable declaration (without initialization) and disallowing procedural syntax Only use implicit variable initialization Only use functions C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 45 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 46 8