C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.

Slides:



Advertisements
Similar presentations
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Advertisements

C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Data Abstraction and State Abstract Data Types (3.7) State/Data Abstraction(6.1, 6.3, 6.4.1,
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Introduction to Programming Concepts Carlos Varela RPI Adapted with permission from: Seif.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Data types and variables
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
Introducing the Erlang Language Why Erlang?  Exemplifies the “you can have code and concurrency that is free from side effects…there is no other way”
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Declarativeness, iterative computation (VRH 3.1-2) Higher-order.
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
Objectives You should be able to describe: Data Types
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC Lecture 8 (Multiple) Threads Semantics.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
10/18/2015COSC , Lecture 21 Programming Language Concepts, COSC Lecture 2 Oz Syntax, Data structures.
CPS120: Introduction to Computer Science
S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Lecture 2: Basic Control and Procedures Per Brand.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Overview of the Haskell 98 Programming Language
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC Lecture 4 Procedures, last call optimization.
Copyright Curt Hill Variables What are they? Why do we need them?
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CPS120: Introduction to Computer Science Variables and Constants.
S. Haridi and P. Van Roy1 Declarative Computation Model Seif Haridi KTH Peter Van Roy UCL.
1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process 
C. Varela1 Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
A Sample Program #include using namespace std; int main(void) { cout
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Higher-Order Programming: Iterative computation (CTM Section 3
Principles of programming languages 12: Functional programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Chapter 2: Introduction to C++
Higher-Order Programming: Iterative computation (CTM Section 3
Carlos Varela RPI September 22, 2017 Adapted with permission from:
Variables and Primative Types
Carlos Varela Rensselaer Polytechnic Institute December 5, 2017
Type Conversion, Constants, and the String Object
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
Building Java Programs Chapter 2
Chapter 2 Variables.
C++ Data Types Data Type
Variables, Identifiers, Assignments, Input/Output
Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter.
Python Primer 1: Types and Operators
Introduction to Programming Concepts (VRH )
Declarative Computation Model Single assignment store (VRH 2
Variables and Constants
Presentation transcript:

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela RPI Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy2 Sequential declarative computation model The single assignment store, declarative (dataflow) variables, and values (together are called entities) The kernel language syntax 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 execution stack of statements (define control), and the store Execution transforms the store by a sequence of steps

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy3 Single assignment store A single assignment store is a store (set) of variables Initially the variables are unbound, i.e. do not have a defined value Example: a store with three variables, x 1, x 2, and x 3 unbound The Store x1x1 unbound x2x2 x3x3

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy4 Single assignment store (2) Variables in the store may be bound to values Example: assume we allow as values, integers and lists of integers unbound The Store x1x1 unbound x2x2 x3x3

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy5 Single assignment store (3) Variables in the store may be bound to values Assume we allow as values, integers and lists of integers Example: x 1 is bound to the integer 314, x 2 is bound to the list [1 2 3], and x 3 is still unbound The Store x1x1 x2x2 unbound x3x |2 |3 | nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy6 Declarative (single-assignment) variables A declarative variable starts out as being unbound when created It can be bound to exactly one value Once bound it stays bound through the computation, and is indistinguishable from its value The Store x1x1 x2x2 unbound x3x |2 |3 | nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy7 Value store A store where all variables are bound to values is called a value store Example: a value store where x 1 is bound to integer 314, x 2 to the list [1 2 3], and x 3 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) |2 |3 | nil person “George”25 nameage The Store x1x1 x2x2 x3x3

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy8 Operations on the store (1) Single assignment  x  =  v  x 1 = 314 x 2 = [1 2 3] This assumes that  x  is unbound unbound The Store x1x1 unbound x2x2 x3x3

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy9 Single-assignment  x  =  value  x 1 = 314 x2 = [1 2 3] 314 The Store x1x1 unbound x2x2 x3x3

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy10 Single-assignment (2)  x  =  v  x 1 = 314 x 2 = [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 314 The Store x1x1 x2x2 unbound x3x3 1 |2 |3 | nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy11 Variable identifiers Variable identifiers refers to store entities (variables or value) The environment maps variable identifiers to variables declare X : local X in … ”X” is a (variable) identifier This corresponds to ’environment’ {”X”  x 1 } The Store ”X””X” Unbound x1x1

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy12 Variable-value binding revisited (1) The Store “X” x1x1 X = [1 2 3] Once bound the variable is indistinguishable from its value 1 |2 |3 | nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy13 Variable-value binding revisited (2) The Store “X” x1x1 X = [1 2 3] 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 1 |2 |3 | nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy14 Partial Values Is a data structure that may contain unbound variables The store contains the partial value: person(name: “George” age: x 2 ) declare Y X X = person(name: “George” age: Y) The identifier ’Y’ refers to x 2 person “George”Unbound nameage The Store “X” “Y” x1x1 x2x2

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy15 Partial Values (2) Partial Values may be complete declare Y X X = person(name: “George” age: Y) Y = 25 person “George”25 nameage The Store “X” “Y” x1x1 x2x2

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy16 Variable to variable binding  x 1  =  x 2  It is to perform the bind operation between variables Example: X = Y X = [1 2 3] The operations equates (merges) the two variables unbound The Store x1x1 unbound x2x2 X Y

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy17 Variable to variable binding (2)  x 1  =  x 2  It is to perform a single assignment between variables Example: X = Y X = [1 2 3] The operations equates the two variables (forming an equivalence class) The Store x1x1 x2x2 X Y

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy18 Variable to variable binding (3)  x 1  =  x 2  It is to perform a single assignment between variables Example: X = Y X = [1 2 3] All variables (X and Y) are bound to [1 2 3] The Store x1x1 x2x2 X Y 1 |2 |3 | nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy19 Summary Variables and partial values Declarative variable: –is an entity that resides in a single-assignment store, that is initially unbound, 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 unbound 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 unbound variables

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy20 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)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy21 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 Example: A = 23 running concurrently with B = A+1 Functional (concurrent) languages do not allow the separation between declaration and use (ML, Haskell, and Erlang)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy22 Kernel language syntax  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  pattern  ::=... The following defines the syntax of a statement,  s  denotes a statement

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy23 Variable identifiers  x ,  y ,  z  stand for variables In the concrete kernel language variables begin with upper- case 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)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy24 Values and types A data type is a set of values and a set of associated operations Example: 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

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy25 Data types Value Number Literal RecordProcedure IntIntFloat AtomBoolean TrueFalse Char Tuple List String

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy26 Data types (2) Value Number Literal RecordProcedure IntIntFloat AtomBoolean TrueFalse Char Tuple List String

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy27 Value expressions  v  ::=  procedure  |  record  |  number   procedure  ::= proc {$  y 1  …  y n  }  s  end  record ,  pattern  ::=  literal  |  literal  (  feature 1  :  x 1  …  feature n  :  x n  )  literal  ::=  atom  |  bool   feature  ::=  int  |  atom  |  bool   bool  ::= true | false  number  ::=  int  |  float 

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy28 Numbers Integers –314, 0 –~10 (minus 10) Floats –1.0, 3.4, 2.0e2, 2.0E2 ( 2  10 2 )

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy29 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 Roy30 Records Compound representation (data-structures) –  l  (  f 1  :  x 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

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy31 Syntactic sugar (tuples) Tuples  l  (  x 1  …  x n  )(tuple) This is equivalent to the record  l  (1:  x 1  … n:  x n  ) Example: person(‘George’ 25) This is the record person(1:‘George’ 2:25)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy32 Syntactic sugar (lists) Lists  x 1  |  x 2  (a cons with the infix operator ‘|’) This is equivalent to the tuple ‘|’ (  x 1   x 2  ) Example: H | T This is the tuple ‘|’(H T)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy33 Syntactic sugar (lists) Lists  x 1  |  x 2  |  x 3  ‘|’ associates to the right  x 1  | (  x 2  |  x 3  ) Example: 1 | 2 | 3 | nil Is 1 | ( 2 | (3 | nil )) ‘|’ 1 2 3nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy34 Syntactic sugar (complete lists) Lists Example: [1 2 3] Is 1 | ( 2 | (3 | nil )) ‘|’ 1 2 3nil

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy35 Strings A string is a list of character codes enclosed with double quotes Ex: ”E=mc^2” Means the same as [ ]

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy36 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 Roy37 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 Roy38 Value expressions  v  ::=  procedure  |  record  |  number  |  basicExpr   basicExpr  ::=... |  numberExpr  |...  numberExpr  ::=  x  1 +  x  2 |

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy39 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 Roy40 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 T is a ’new’ variable identifier

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy41 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 Roy42 Exercises 1.Practice with the basic operations on numbers, records, and booleans (read Appendix B1-B3) 2.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. 3.Describe what an anonymous procedure is, and write one in Oz. When are anonymous procedures useful? 4.Write an Oz program that parses lambda calculus expressions. 5.Read VRH Section 2.4 carefully.