On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

Similar documents
Programming Language Concepts, cs2104 Lecture 04 ( )

Declarative Computation Model

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)

State, Object-Oriented Programming Explicit State, Polymorphism (CTM ) Objects, Classes, and Inheritance (CTM )

Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces

Declarative Concurrency (CTM 4)

Lazy evaluation. Lazy evaluation (3) Lazy evaluation (2) Lazy execution. Example. Declarative Concurrency. Lazy Execution (VRH 4.

Carlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

Declarative Programming Techniques

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Declarative Concurrency (CTM 4)

Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011

Programming Language Concepts, cs2104 Lecture 01 ( )

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

An Oz Subset. 1 Microsyntax for An Oz Subset. Notational Conventions. COP 4020 Programming Languages 1 January 17, 2012

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms

CS 106 Introduction to Computer Science I

Overview. Distributed Computing with Oz/Mozart (VRH 11) Mozart research at a glance. Basic principles. Language design.

SMURF Language Reference Manual Serial MUsic Represented as Functions

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011

F28PL1 Programming Languages. Lecture 11: Standard ML 1

Introduction to the C++ Programming Language

Topic 1: Introduction

The PCAT Programming Language Reference Manual

1 Lexical Considerations

SML A F unctional Functional Language Language Lecture 19

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

BASIC ELEMENTS OF A COMPUTER PROGRAM

REVIEW. The C++ Programming Language. CS 151 Review #2

Full file at

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Principles of Programming Languages 2017W, Functional Programming

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

Programming Language Concepts, cs2104 Lecture 09 ( )

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

3. Java - Language Constructs I

General Overview of Mozart/Oz

Values and Variables 1 / 30

Note that pcall can be implemented using futures. That is, instead of. we can use

CSc 520. Principles of Programming Languages 11: Haskell Basics

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

The SPL Programming Language Reference Manual

CIS 110: Introduction to Computer Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

FRAC: Language Reference Manual

Typed Racket: Racket with Static Types

Homework 3 COSE212, Fall 2018

Automated Reasoning. Natural Deduction in First-Order Logic

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

To figure this out we need a more precise understanding of how ML works

Chapter 15. Functional Programming Languages

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Lexical Considerations

COMP Primitive and Class Types. Yi Hong May 14, 2015

corgi Language Reference Manual COMS W4115

Concepts, Techniques, and Models of Computer Programming

Flang typechecker Due: February 27, 2015

CS 320: Concepts of Programming Languages

Programming Lecture 3

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Semantic Analysis and Type Checking

Introduction to Bioinformatics

C++ Programming: From Problem Analysis to Program Design, Third Edition

Datatype declarations

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Typescript on LLVM Language Reference Manual

Functional Programming. Pure Functional Programming

Fall Lecture 3 September 4. Stephen Brookes

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The role of semantic analysis in a compiler

Lexical Considerations

A Concepts-Based Approach for Teaching Programming

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Functional Programming Languages (FPL)

CS313D: ADVANCED PROGRAMMING LANGUAGE

Functional Logic Programming Language Curry

Handout 10: Imperative programs and the Lambda Calculus

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Programming Languages Third Edition

Mozart System Limitations for Version (draft)

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

ML 4 A Lexer for OCaml s Type System

Computer Programming : C++

CSE 307: Principles of Programming Languages

Server side basics CS380

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Part I Logic programming paradigm

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction Cyrille Berger

RSL Reference Manual

This book is licensed under a Creative Commons Attribution 3.0 License

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

CS 11 Haskell track: lecture 1

Transcription:

Declarative Computation Model Single assignment store (VRH 2.2) Kernel language syntax (VRH 2.3) Carlos Varela RPI October 6, 2009 Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL On Academic Dishonesty Academic dishonesty policies apply to this course, including: Academic Fraud Collaboration Copying Cribbing Fabrication Plagiarism Sabotage Substitution If in doubt, ask the instructor, or see Rensselaer Handbook of Student Rights and Responsibilities at: http://www.rpi.edu/dept/doso/handbook.html Students found in violation of academic dishonesty policies may receive a failing grade for this course. 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 2 Sequential declarative computation model Single assignment store The single assignment store declarative (dataflow) variables partial values (variables and values are also called entities) The kernel language syntax The kernel language semantics The environment: maps textual variable names (variable identifiers) into entities in the store Interpretation (execution) of the kernel language elements (statements) by the use of an abstract machine Abstract machine consists of an execution stack of statements transforming the store A single assignment store is a store (set) of variables Initially the variables are, i.e. do not have a defined value a store with three variables,,, and 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 Single assignment store (2) Single assignment store (3) Variables in the store may be bound to values assume we allow as values, integers and lists of integers Variables in the store may be bound to values Assume we allow as values, integers and lists of integers is bound to the integer, is bound to the list [1 2 3], and is still 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

Declarative (single-assignment) variables Value store A declarative variable starts out as being when created It can be bound to exactly one value Once bound it stays bound through the computation, and is indistinguishable from its value A store where all variables are bound to values is called a value store a value store where is bound to integer, to the list [1 2 3], and to the record (labeled tree) person(name: George age: 25) Functional programming computes functions on values, needs only a value store This notion of value store is enough for functional programming (ML, Haskell, Scheme) person name age George 25 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 Operations on the store (1) Single assignment Single-assignment x = v = = [1 2 3] x = value = x2 = [1 2 3] This assumes that x is 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 Single-assignment (2) Variable identifiers x = v = = [1 2 3] The single assignment operation ( = ) constructs the v in the store and binds the variable x to this value If the variable is already bound, the operation will test the compatibility of the two values If the test fails an error is raised Variable identifiers refers to store entities (variables or values) The environment maps variable identifiers to variables declare X : local X in X is a (variable) identifier This corresponds to environment { X } X Unbound 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 12 2

Variable-value binding revisited (1) Variable-value binding revisited (2) Once bound the variable is indistinguishable from its value X Once bound the variable is indistinguishable from its value The operation of traversing variable cells to get the value is known as dereferencing and is invisible to the programmer X 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 Partial Values Partial Values (2) A partial value is a data structure that may contain variables The store contains the partial value: person(name: George age: ) declare Y X X = person(name: George age: Y) The identifier Y refers to X person name George age Unbound Partial Values may be complete declare Y X X = person(name: George age: Y) Y = 25 X person name age George 25 Y Y 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 Variable to variable binding Variable to variable binding (2) = It is to perform the bind operation between variables X X = Y The operations equates (merges) Y the two variables = It is to perform a single assignment between variables X X = Y The operations equates the two Y variables (forming an equivalence class) 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

Variable to variable binding (3) = It is to perform a single assignment X between variables X = Y Y All variables (X and Y) are bound to [1 2 3] Summary Variables and partial values Declarative variable: is an entity that resides in a single-assignment store, that is initially, and can be bound to exactly one (partial) value it can be bound to several (partial) values as long as they are compatible with each other Partial value: is a data-structure that may contain variables when one of the variables is bound, it is replaced by the (partial) value it is bound to a complete value, or value for short is a data structure that does not contain any variables C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 19 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 20 Declaration and use of variables Assume that variables can be declared (introduced) and used separately What happens if we try to use a variable before it is bound? 1. Use whatever value happens to be in the memory cell occupied by the variable (C, C++) 2. The variable is initialized to a default value (Java), use the default 3. An error is signaled (Prolog). Makes sense if there is a single activity running (pure sequential programs) 4. An attempt to use the variable will wait (suspends) until another activity binds the variable (Oz/Mozart) Declaration and use of variables (2) An attempt to use the variable will wait (suspends) until another activity binds the variable (Oz/Mozart) Declarative (single assignment) variables that have this property are called dataflow variables It allows multiple operations to proceed concurrently giving the correct result A = 23 running concurrently with B = A+1 Functional (concurrent) languages do not allow the separation between declaration and binding (ML, Haskell, and Erlang) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 21 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 22 Kernel language syntax Variable identifiers The following defines the syntax of a statement, 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 end declaration if x then s 1 else s 2 end conditional { x y 1 y n } procedural application case x of pattern then s 1 else s 2 end pattern matching v ::=... value expression x, y, z stand for variables In the concrete kernel language variables begin with uppercase letter followed by a (possibly empty) sequence of alphanumeric characters or underscore Any sequence of printable characters within back-quote Examples: X Y1 Hello_World `hello this is a $5 bill` (back-quote) pattern ::=... C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 23 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 24 4

Values and types Data types A data type is a set of values and a set of associated operations Int is the the data type Integer, i.e set of all integer values 1 is of type Int Int has a set of operations including +,-,*,div, etc The model comes with a set of basic types Programs can define other types, e.g., abstract data types ADT Number Int Float Char Atom Value Record Tuple Literal Boolean true false Procedure List String C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 25 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 26 Data types (2) Value Value expressions v ::= procedure record number Number Record Tuple Procedure procedure ::= proc { $ y 1 y n } s end record, pattern ::= literal literal ([ feature 1 : feature n : x n ]) Int Float Literal List literal ::= atom bool feature ::= int atom bool Char Atom true Boolean false String bool ::= true false number ::= int float C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 27 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 28 Integers Numbers, 0 ~10 (minus 10) Floats 1.0, 3.4, 2.0e2, 2.0E2 (2 10 2 ) Atoms and booleans A sequence starting with a lower-case character followed by characters or digits, person, peter Seif Haridi Booleans: true false C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 29 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 30 5

Records Compound representation (data-structures) l ( f 1 : f n : x n ) l is a literal Examples person(age:x1 name:x2) person(1:x1 2:X2) (1:H 2:T) nil person Syntactic sugar (tuples) Tuples l ( x n ) (tuple) This is equivalent to the record l (1: n: x n ) person( George 25) This is the record person(1: George 2:25) 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 32 Syntactic sugar (lists) Syntactic sugar (lists) Lists (a cons with the infix operator ) This is equivalent to the tuple ( ) Lists associates to the right ( ) H T This is the tuple (H T) Is 1 ( 2 (3 nil )) 1 2 3 nil 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 Syntactic sugar (complete lists) Strings Complete lists [1 2 3] Is 1 ( 2 (3 nil )) 1 A string is a list of character codes enclosed with double quotes Ex: E=mc^2 Means the same as [69 61 109 99 94 50] 2 3 nil 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

Procedure declarations According to the kernel language x = proc {$ y 1 y n } s end is a legal statement It binds x to a procedure value This statement actually declares (introduces) a procedure Another syntactic variant which is more familiar is proc { x y 1 y n } s end This introduces (declares) the procedure x C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 37 Operations of basic types Arithmetics Floating point numbers: +,-,*, and / Integers: +,-,*,div (integer division, i.e. truncate fractional part), mod (the remainder after a division, e.g.10 mod 3 = 1) Record operations Arity, Label, and. X = person(name: George age:25) {Arity X} = [age name] {Label X} = person, X.age = 25 Comparisons Boolean comparisons, including ==, \= (equality) Numeric comparisons, =<, <, >, >=, compares integers, floats, and atoms C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 38 Value expressions v ::= procedure record number basicexpr basicexpr ::=... numberexpr... numberexpr ::= x 1 + x 2...... Syntactic sugar (multiple variables) Multiple variable introduction local X Y in statement end is transformed to local X in local Y in statement end end 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 Syntactic sugar (basic expressions) Basic expression nesting if basicexpr then statement 1 else statement 2 end is transformed to local T in T = basicexpr if T then statement 1 else statement 2 end end where T is a fresh ( new ) variable identifier Syntactic sugar (variables) Variable initialization local X = value in statement end Is transformed to local X in X = value statement end C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 41 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 42 7

Exercises 42. Using Oz, perform a few basic operations on numbers, records, and booleans (see Appendix B1-B3) 43. Explain the behavior of the declare statement in the interactive environment. Give an example of an interactive Oz session where declare and declare in produce different results. Explain why. 44. VRH Exercise 2.9.1 45. Describe what an anonymous procedure is, and write one in Oz. When are anonymous procedures useful? C. Varela; Adapted w/permission from S. Haridi and P. Van Roy 43 8