1 / 136

Declarative Computation Model

Declarative Computation Model. Seif Haridi KTH Peter Van Roy UCL. Programming. A computation model : describes a language and how the sentences (expressions, statements) of the language are executed by an abstract machine

Download Presentation

Declarative Computation Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Declarative Computation Model Seif Haridi KTH Peter Van Roy UCL S. Haridi and P. Van Roy

  2. Programming • A computation model: describes a language and how the sentences (expressions, statements) of the language are executed by an abstract machine • A set of programming techniques: to express solutions to the problems you want to solve • A set of reasoning techniques: to reason about programs to increase the confidence that they behave correctly and calculate their efficiency S. Haridi and P. Van Roy

  3. Declarative Programming Model • Guarantees that the computations are evaluating functions on (partial) data structures • The core of functional programming (LISP, Scheme, ML, Haskell) • The core of logic programming (Prolog, Mercury) • Stateless programming vs. stateful (imperative) programming • We will see how declarative programming underlies concurrent and object-oriented programming (Erlang, Java) S. Haridi and P. Van Roy

  4. Defining a programming language • Syntax (grammar) • Semantics (meaning) S. Haridi and P. Van Roy

  5. Language syntax • Defines what are the legal programs, i.e. programs that can be executed by a machine (interpreter) • Syntax is defined by grammar rules • A grammar defines how to make ‘sentences’ out of ‘words’ • For programming languages: sentences are called statements (commands, expressions) • For programming languages: words are called tokens • Grammar rules are used to describe both tokens and statements S. Haridi and P. Van Roy

  6. Language syntax (2) • A statement is a sequence of tokens • A token is a sequence of characters • A program that recognizes a sequence of characters and produces a sequence of tokens is called a lexical analyzer • A program that recognizes a sequence of tokens and produces a sequence of statement representation is called a parser • Normally statements are represented as (parse) trees characters Lexical analyzer tokens Parser sentences S. Haridi and P. Van Roy

  7. Extended Backus-Naur Form • EBNF (Extended Backus-Naur Form) is a common notation to define grammars for programming languages • Terminal symbols and non-terminal symbols • Terminal symbol is a token • Nonterminal symbol is a sequence of tokens, and is represented by a grammar rule • nonterminal ::= rule body S. Haridi and P. Van Roy

  8. Grammar rules • digit ::= 0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | • digit is defined to represent one of the ten tokens 0, 1, …, 9 • The symbol ‘|’ is read as ‘or’ • Another reading is that digit describes the set of tokens {0,1,…, 9} • Grammar rules may refer to other nonterminals • integer ::= digit { digit } • integer is defined as the sequence of a digit followed by a zero or more digit’s S. Haridi and P. Van Roy

  9. How to read grammar rules • x : is a nonterminalx • x ::=Body :x is defined byBody • x|y : either xor y(choice) • x y : the sequence x followed by y • {x} : a sequence of zero or more occurrences of x • {x}+ : a sequence of one or more occurrences of x • [x] : zero or one occurrences of x • Read the grammar rule from left to right to give the following sequence: • Each terminal symbol is added to the sequence • Each nonterminal is replaced by its definition • For each x|y pick any of the alternatives • For each x yis the sequence x followed by the sequence y S. Haridi and P. Van Roy

  10. Context-free and context-sensitive grammars • Grammar rules can be used to either • verify that a statement is legal, or • to generate all possible statements • The set of all possible statements generated from a grammar and one nonterminal symbol is called a (formal) language • EBNF notation defines a class of grammars called context-free grammars • Expansion of a nonterminal is always the same regardless of where it is used • For practical languages context-free grammar is not enough, usually a condition on the context is sometimes added S. Haridi and P. Van Roy

  11. Context-free and context-sensitive grammars • It is easy to read and understand • defines a superset of the language • Expresses restrictions imposed by the language (e.g. variable must be declared before use) • Makes the grammar rules context sensitive Context-free grammar (e.g. with EBNF) + Set of extra conditions S. Haridi and P. Van Roy

  12. Examples • statement ::=skip| expression ‘=‘ expression | … • expression ::= variable | integer |… • statement ::=if expression then statement {elseif expression then statement }[ else statement ]end| … S. Haridi and P. Van Roy

  13. Example: (Parse Trees) • if expression then statement1else statement2end conditional if then else expression statement1 statement2 S. Haridi and P. Van Roy

  14. Language Semantics • Semantics defines what a program does when it executes • Semantics should be simple and yet allows a programmer to reason about programs (correctness, execution time, and memory use) • How can this be achieved for a practical language that is used to build complex systems (millions lines of code) ? • The kernel language approach S. Haridi and P. Van Roy

  15. Kernel Language Approach • Define a very simple language (kernel language) • Define the computation model of the kernel language • By defining how the constructs (statements) of the language manipulate (create and transform) the data structures (the entities) of the language • Define a mapping scheme (translation) of full programming language into the kernel language • Two kinds of translations: linguistic abstractions and syntactic sugar S. Haridi and P. Van Roy

  16. Kernel Language Approach fun {Sqr X} X*X endB = {Sqr {Sqr A}} Practical language • Provides useful abstractions for the programmer • Can be extended with linguistic abstractions Translation kernel language proc {Sqr X Y} { * X X Y}endlocal T in{Sqr A T} {Sqr T B}end • Is easy to understand and reason with • Has a precise (formal) semantics S. Haridi and P. Van Roy

  17. Linguistic abstractions vs. syntactic sugar • Linguistic abstractions, provide higher level concepts that the programmer can use to model, and reasons about programs (systems) • Examples: functions (fun), iterations (for), classes and objects (class), mailboxes (receive) • The functions (calls) are translated to procedures (calls) • The translation answers questions about the functions:{F1 {F2 X} {F3 X}} S. Haridi and P. Van Roy

  18. Linguistic abstractions vs. syntactic sugar • Linguistic abstractions, provide higher level concepts that the programmer can use to model, and reasons about programs (systems) • Syntactic sugar are short cuts and conveniences to improve readability if N==1 then [1]elselocal L in …endend if N==1 then [1]else L in …end S. Haridi and P. Van Roy

  19. Approaches to semantics Programming Language Operational model Kernel Language Formal calculus Abstract machine Aid the programmerin reasoning andunderstanding Mathematical study ofprogramming (languages)-calculus, predicate calculus,-calculus Aid to the implementerEfficient execution ona real machine S. Haridi and P. Van Roy

  20. 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 S. Haridi and P. Van Roy

  21. 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, x1, x2, and x3 The Store x1 unbound x2 unbound x3 unbound S. Haridi and P. Van Roy

  22. Single assignment store (2) • Variables in the store may be bound to values • Example: assume we allow as values, integers and lists of integers The Store x1 unbound x2 unbound x3 unbound S. Haridi and P. Van Roy

  23. Single assignment store (3) • Variables in the store may be bound to values • Assume we allow as values, integers and lists of integers • Example: x1 is bound to the integer 314, x2 is bound to the list [1 2 3], and x3 is still unbound The Store x1 314 x2 1 | 2 | 3 | nil x3 unbound S. Haridi and P. Van Roy

  24. 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 x1 314 x2 1 | 2 | 3 | nil x3 unbound S. Haridi and P. Van Roy

  25. Value store • A store where all variables are bound to values is called a value store • Example: a value store where x1 is bound to integer 314, x2 to the list [1 2 3],and x3 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) The Store x1 314 x2 1 | 2 | 3 | nil x3 person name age “George” 25 S. Haridi and P. Van Roy

  26. Operations on the store (1)Single assignment x = v • x1 = 314 • x2= [1 2 3] • This assumes that xis unbound The Store x1 unbound x2 unbound x3 unbound S. Haridi and P. Van Roy

  27. Single-assignment x = value • x1 = 314 • x2 = [1 2 3] The Store x1 314 x2 unbound x3 unbound S. Haridi and P. Van Roy

  28. Single-assignment (2) x = v • x1 = 314 • x2= [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 The Store x1 314 x2 1 | 2 | 3 | nil x3 unbound S. Haridi and P. Van Roy

  29. 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”  x1} The Store ”X” x1 Unbound S. Haridi and P. Van Roy

  30. Variable-value binding revisited (1) • X = [1 2 3] • Once bound the variable is indistinguishable from its value The Store “X” x1 1 | 2 | 3 | nil S. Haridi and P. Van Roy

  31. Variable-value binding revisited (2) • 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 dereferencingand is invisible to the programmer The Store “X” x1 1 | 2 | 3 | nil S. Haridi and P. Van Roy

  32. Partial Values • Is a data structure that may contain unbound variables • The store contains the partial value: person(name: “George” age: x2) • declare Y XX = person(name: “George” age: Y) • The identifier ’Y’ refers to x2 The Store x1 person “X” name age “George” Unbound x2 “Y” S. Haridi and P. Van Roy

  33. Partial Values (2) Partial Values may be complete • declare Y XX = person(name: “George” age: Y) • Y = 25 The Store x1 person “X” name age “George” x2 25 “Y” S. Haridi and P. Van Roy

  34. Variable to variable binding x1 = x2 • It is to perform the bind operation between variables • Example: • X = Y • X = [1 2 3] • The operations equates (merges) the two variables The Store X x1 unbound x2 unbound Y S. Haridi and P. Van Roy

  35. Variable to variable binding (2) x1 = x2 • 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 X x1 x2 Y S. Haridi and P. Van Roy

  36. Variable to variable binding (3) x1 = x2 • 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 X x1 1 | 2 | 3 | nil x2 Y S. Haridi and P. Van Roy

  37. SummaryVariables 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 S. Haridi and P. Van Roy

  38. 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? • Use whatever value happens to be in the memory cell occupied by the variable (C, C++) • The variable is initialized to a default value (Java), use the default • An error is signaled (Prolog). Makes sense if there is a single activity running (pure sequential programs) • An attempt to use the variable will wait (suspends) until another activity binds the variable (Oz/Mozart) S. Haridi and P. Van Roy

  39. 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) S. Haridi and P. Van Roy

  40. Kernel language syntax 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 | s1 s2 sequential composition | local x in s1 end declaration | if x then s1 else s2 end conditional | { x y1 … yn } procedural application | case x of pattern then s1 else s2 end pattern matching v::= ... value expression pattern::= ... S. Haridi and P. Van Roy

  41. 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) S. Haridi and P. Van Roy

  42. 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 typeInt • 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., abstractdata types ADT S. Haridi and P. Van Roy

  43. Data types Value Number Record Procedure Tuple Int Float Literal List Char Atom Boolean String True False S. Haridi and P. Van Roy

  44. Data types (2) Value Number Record Procedure Tuple Int Float Literal List Char Atom Boolean String True False S. Haridi and P. Van Roy

  45. Value expressions v::= procedure | record | number procedure ::= proc {$ y1 … yn} s end record, pattern::= literal | literal (feature1 : x1 … featuren : xn) literal ::= atom | bool feature ::= int | atom | bool bool ::= true | false number ::= int | float S. Haridi and P. Van Roy

  46. Numbers • Integers • 314, 0 • ~10 (minus 10) • Floats • 1.0, 3.4, 2.0e2, 2.0E2 (2102) S. Haridi and P. Van Roy

  47. Atoms and booleans • A sequence starting with a lower-case character followed by characters or digits, … • person, peter • ‘Seif Haridi’ • Booleans: • true • false S. Haridi and P. Van Roy

  48. Records • Compound representation (data-structures) • l(f1 : x1 … fn : xn) • l is a literal • Examples • person(age:X1 name:X2) • person(1:X1 2:X2) • ‘|’(1:H 2:T) • nil • person S. Haridi and P. Van Roy

  49. Syntactic sugar (tuples) • Tuplesl(x1 … xn) (tuple) • This is equivalent to the record l(1: x1 … n: xn) • Example: person(‘George’ 25) • This is the record person(1:‘George’ 2:25) S. Haridi and P. Van Roy

  50. Syntactic sugar (lists) • Listsx1 | x2 (a cons with the infix operator ‘|’) • This is equivalent to the tuple ‘|’(x1 x2) • Example: H | T • This is the tuple ‘|’(H T) S. Haridi and P. Van Roy

More Related