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,

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.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
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.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
5/15/2015COSC , Lecture 61 Programming Language Concepts, COSC Lecture 6 Higher-Order Programming and Abstract Data Types.
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.
Seif Haridi and Peter Van Roy1 Explicit State Seif Haridi Peter Van Roy.
C. Varela1 Typing and Parameter Passing Dynamic and Static Typing (EPL 4.1-4, VRH 2.8.3) Parameter Passing Mechanisms (VRH 6.4.4) Carlos Varela RPI.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Objects, Classes and Inheritance (VRH 7.1-2) Polymorphism (VRH 6.4.3) Carlos.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Introduction to Programming Concepts Carlos Varela RPI Adapted with permission from: Seif.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Data Abstraction, State, and Objects Abstract Data Types (VRH 3.7) State / Data Abstraction(VRH.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Active Objects (VRH 7.8) Carlos Varela RPI Adapted with permission from:
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Objects, Classes and Inheritance (VRH 7.1-4) Polymorphism (VRH 6.4.3) Carlos.
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 Declarative Programming Techniques Non-declarative needs (VRH 3.8) Program design in the.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Declarativeness, iterative computation (VRH 3.1-2) Higher-order.
C++ fundamentals.
9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC Lecture 8 (Multiple) Threads Semantics.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 3 Introduction to Collections – Stacks Modified
C. Varela1 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)
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.
S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.
C. Varela1 Programming Languages (CSCI 4430/6430) Part 1: Functional Programming: Summary Carlos Varela Rennselaer Polytechnic Institute September 29,
Built-in Data Structures in Python An Introduction.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Python Functions.
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.
S. Haridi and P. Van Roy1 Concurrency and State Seif Haridi KTH Peter Van Roy UCL.
Data Abstaraction Chapter 10.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
S. Haridi and P. Van Roy1 Introduction to Programming Seif Haridi KTH Peter Van Roy UCL.
Salman Marvasti Sharif University of Technology Winter 2015.
2004 Hawaii Inter Conf Comp Sci1 Specifying and Proving Object- Oriented Programs Arthur C. Fleck Computer Science Department University of Iowa.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
SWE 4743 Abstraction Richard Gesick. CSE Abstraction the mechanism and practice of abstraction reduces and factors out details so that one can.
1/21/2016COSC , Lecture 91 Programming Language Concepts, COSC Lecture 9 Stateful programming Object-Oriented Programming.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
CMSC 330: Organization of Programming Languages Operational Semantics.
C. Varela1 Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Higher-Order Programming: Iterative computation (CTM Section 3
Sections 3.4 Formal Specification
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) Carlos Varela.
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:
Higher-Order Programming: Iterative computation (CTM Section 3
Carlos Varela RPI September 22, 2017 Adapted with permission from:
State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6
Declarative Programming Techniques Accumulators (CTM 3. 4
Carlos Varela Rensselaer Polytechnic Institute December 5, 2017
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
Declarative Programming Techniques Declarativeness, iterative computation (CTM ) Higher-order programming (CTM 3.6) Abstract data types (CTM.
Object-Oriented Programming Encapsulation Control/Visibility (VRH 7. 3
Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter.
Declarative Programming Techniques Accumulators (CTM 3. 4
Introduction to Data Structure
Introduction to Programming Concepts (VRH )
Declarative Computation Model Single assignment store (VRH 2
Introduction to Programming Concepts
State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6
Declarative Programming Techniques Accumulators (CTM 3. 4
Presentation transcript:

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, 6.4.2) 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 Abstract data types A datatype is a set of values and an associated set of operations A datatype is abstract if only it is completely described by its set of operations regradless of its implementation This means that it is possible to change the implementation of the datatype without changing its use The datatype is thus described by a set of procedures These operations are the only thing that a user of the abstraction can assume

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy3 Example: A Stack Assume we want to define a new datatype  stack T  whose elements are of any type T fun {NewStack}:  Stack T  fun {Push  Stack T   T  }:  Stack T  fun {Pop  Stack T   T  }:  Stack T  fun {IsEmpty  Stack T  }:  Bool  These operations normally satisfy certain conditions: {IsEmpty {NewStack}} = true for any E and S0, S1={Push S0 E} and S0 ={Pop S1 E} hold {Pop {NewStack} E} raises error

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy4 Stack (implementation) fun {NewStack} nil end fun {Push S E} E|S end fun {Pop S E} case S of X|S1 then E = X S1 end end fun {IsEmpty S} S==nil end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy5 Stack (another implementation) fun {NewStack} nil end fun {Push S E} E|S end fun {Pop S E} case S of X|S1 then E = X S1 end end fun {IsEmpty S} S==nil end fun {NewStack} emptyStack end fun {Push S E} stack(E S) end fun {Pop S E} case S of stack(X S1) then E = X S1 end end fun {IsEmpty S} S==emptyStack end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy6 Dictionaries The datatype dictionary is a finite mapping from a set T to  value , where T is either  atom  or  integer  fun {NewDictionary} –returns an empty mapping fun {Put D Key Value} –returns a dictionary identical to D except Key is mapped to Value fun {CondGet D Key Default} –returns the value corresponding to Key in D, otherwise returns Default fun {Domain D} –returns a list of the keys in D

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy7 Implementation fun {h} nil end fun {Put Ds Key Value} case Ds of nil then [Key#Value] [] (K#V)|Dr andthen Key==K then (Key#Value) | Dr [] (K#V)|Dr andthen K>Key then (Key#Value)|(K#V)|Dr [] (K#V)|Dr andthen K<Key then (K#V)|{Put Dr Key Value} end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy8 Implementation fun {CondGet Ds Key Default} case Ds of nil then Default [] (K#V)|Dr andthen Key==K then V [] (K#V)|Dr andthen K>Key then Default [] (K#V)|Dr andthen K<Key then {CondGet Dr Key Default} end fun {Domain Ds} {Map Ds fun {$ K#_} K end} end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy9 Further implementations Because of abstraction, we can replace the dictionary ADT implementation using a list, whose complexity is linear (i.e., O(n)), for a binary tree implementation with logarithmic operations (i.e., O(log(n)). Data abstraction makes clients of the ADT unaware (other than through perceived efficiency) of the internal implementation of the data type. It is important that clients do not use anything about the internal representation of the data type (e.g., using {Length Dictionary} to get the size of the dictionary). Using only the interface (defined ADT operations) ensures that different implementations can be used in the future.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy10 Secure abstract data types: Stack is not secure fun {NewStack} nil end fun {Push S E} E|S end fun {Pop S E} case S of X|S1 then E=X S1 end end fun {IsEmpty S} S==nil end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy11 Secure abstract data types II The representation of the stack is visible: [a b c d] Anyone can use an incorrect representation, i.e., by passing other language entities to the stack operation, causing it to malfunction (like a|b|X or Y=a|b|Y) Anyone can write new operations on stacks, thus breaking the abstraction-representation barrier How can we guarantee that the representation is invisible?

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy12 Secure abstract data types III It is not possible to build secure abstract data types in the declarative model we have seen so far (think about it!) The model has to be extended. Here are two ways: –By adding a new basic type, an unforgeable constant called a name –By adding encapsulated state. A name is like an atom except that it cannot be typed in on a keyboard or printed! –The only way to have a name is if one is given it explicitly There are just two operations on names: N={NewName} : returns a fresh name N1==N2 : returns true or false

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy13 Secure abstract datatypes IV proc {NewWrapper ?Wrap ?Unwrap} Key={NewName} in fun {Wrap X} fun {$ K} if K==Key then X end end end fun {Unwrap C} {C Key} end We want to « wrap » and « unwrap » values Let us use names to define a wrapper & unwrapper

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy14 Secure abstract data types: A secure stack With the wrapper & unwrapper we can build a secure stack local Wrap Unwrap in {NewWrapper Wrap Unwrap} fun {NewStack} {Wrap nil} end fun {Push S E} {Wrap E|{Unwrap S}} end fun {Pop S E} case {Unwrap S} of X|S1 then E=X {Wrap S1} end end fun {IsEmpty S} {Unwrap S}==nil end end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy15 Capabilities and security We say a computation is secure if it has well-defined and controllable properties, independent of the existence of other (possibly malicious) entities (either computations or humans) in the system What properties must a language have to be secure? One way to make a language secure is to base it on capabilities –A capability is an unforgeable language entity (« ticket ») that gives its owner the right to perform a particular action and only that action –In our model, all values are capabilities (records, numbers, procedures, names) since they give the right to perform operations on the values –Having a procedure gives the right to call that procedure. Procedures are very general capabilities, since what they do depends on their argument –Using names as procedure arguments allows very precise control of rights; for example, it allows us to build secure abstract data types Capabilities originated in operating systems research –A capability can give a process the right to create a file in some directory

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy16 Secure abstract datatypes V We add two new concepts to the computation model {NewChunk Record} –returns a value similar to record but its arity cannot be inspected –recall {Arity foo(a:1 b:2)} is [a b] {NewName} –a function that returns a new symbolic (unforgeable, i.e. cannot be guessed) name –foo(a:1 b:2 {NewName}:3) makes impossible to access the third component, if you do not know the arity {NewChunk foo(a:1 b:2 {NewName}:3) } –Returns what ?

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy17 Secure abstract datatypes VI proc {NewWrapper ?Wrap ?Unwrap} Key={NewName} in fun {Wrap X} {NewChunk foo(Key:X)} end fun {Unwrap C} C.Key end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy18 Secure abstract data types: Another secure stack With the new wrapper & unwrapper we can build another secure stack (since we only use the interface to wrap and unwrap, the code is identical to the one using higher-order programming) local Wrap Unwrap in {NewWrapper Wrap Unwrap} fun {NewStack} {Wrap nil} end fun {Push S E} {Wrap E|{Unwrap S}} end fun {Pop S E} case {Unwrap S} of X|S1 then E=X {Wrap S1} end end fun {IsEmpty S} {Unwrap S}==nil end end

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy19 What is state? State is a sequence of values in time that contains the intermediate results of a desired computation Declarative programs can also have state according to this definition Consider the following program fun {Sum Xs A } case Xs of X|Xr then {Sum Xr A+X } [] nil then A end {Browse {Sum [ ] 0}}

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy20 What is implicit state? The two arguments Xs and A represent an implicit state XsA [ ]0 [2 3 4]1 [3 4]3 [4]6 nil10 fun {Sum Xs A } case Xs of X|Xr then {Sum Xr A+X } [] nil then A end {Browse {Sum [ ] 0}}

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy21 What is explicit state: Example? X An unbound variable X A cell C is created with initial value 5 X is bound to C 5 X The cell C, which X is bound to, is assigned the value 6 6 C C

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy22 What is explicit state: Example? X An unbound variable X A cell C is created with initialvalue 5 X is bound to C 5 X The cell C, which X is bound to, is assigned the value 6 6 C C The cell is a value container with a unique identity X is really bound to the identity of the cell When the cell is assigned, X does not change

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy23 What is explicit state? X = {NewCell I} –Creates a cell with initial value I –Binds X to the identity of the cell Example: X = {NewCell 0} {Assign X J} –Assumes X is bound to a cell C (otherwise exception) –Changes the content of C to become J Y = {Access X} –Assumes X is bound to a cell C (otherwise exception) –Binds Y to the value contained in C

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy24 Examples X = {NewCell 0} {Assign X 5} Y = X {Assign Y 10} {Access X} == 10 % returns true X == Y % returns true X 0 X 5 Y X 10 Y

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy25 Examples X = {NewCell 10} Y = {NewCell 10} X == Y % returns false Because X and Y refer to different cells, with different identities {Access X} == {Access Y} returns true X 10 Y

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy26 Examples X = {NewCell 0} {Assign X 5} Y = X {Assign Y 10} X == 10 % returns false {Access X} == 10 % returns true X 0 X 5 Y X 10 Y

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy27 The model extended with cells Semantic stack w = f(x) z = person(a:y) y =  1 u =  2 x  1: w  2: x.... single assignment store mutable store

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy28 The stateful model  s  ::= skip empty statement |  s 1   s 2  statement sequence |... | {NewCell  x   c  } cell creation |{Exchange  c   x   y  } cell exchange Exchange: bind  x  to the old content of  c  and set the content of the cell  c  to  y 

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy29 The stateful model | {NewCell  x   c  } cell creation |{Exchange  c   x   y  } cell exchange proc {Assign C X} {Exchange C _ X} end fun {Access C} X in{Exchange C X X}X end Exchange: bind  x  to the old content of  c  and set the content of the cell  c  to  y  C := X is syntactic sugar for {Assign C is syntactic sugar for {Access C}

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy30 Abstract data types (revisited) For a given functionality, there are many ways to package the ADT. We distinguish three axes. Open vs. secure ADT: is the internal representation visible to the program or hidden? Declarative vs. stateful ADT: does the ADT have encapsulated state or not? Bundled vs. unbundled ADT: is the data kept together from the operations or is it separable? Let us see what our stack ADT looks like with some of these possibilities

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy31 Stack: Open, declarative, and unbundled Here is the basic stack, as we saw it before: fun {NewStack} nil end fun {Push S E} E|S end fun {Pop S E} case S of X|S1 then E=X S1 end end fun {IsEmpty S} S==nil end This is completely unprotected. Where is it useful? Primarily, in small programs in which expressiveness is more important than security.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy32 Stack: Secure, declarative, and unbundled We can make the declarative stack secure by using a wrapper: local Wrap Unwrap in {NewWrapper Wrap Unwrap} fun {NewStack} {Wrap nil} end fun {Push S E} {Wrap E|{Unwrap S}} end fun {Pop S E} case {Unwrap S} of X|S1 then E=X {Wrap S1} end end fun {IsEmpty S} {Unwrap S} ==nil end end Where is this useful? In large programs where we want to protect the implementation of a declarative component.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy33 Stack: Secure, stateful, and bundled This is the simplest way to make a secure stateful stack: proc {NewStack ?Push ?Pop ?IsEmpty} C={NewCell nil} in proc {Push X} {Assign C X|{Access C}} end fun {Pop} case {Access C} of X|S then {Assign C S} X end end fun {IsEmpty} {Access C} ==nil end end Compare the declarative with the stateful versions: the declarative version needs two arguments per operation, the stateful version uses higher-order programming (instantiation) With some syntactic support, this is object-based programming

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy34 Stack: Secure, stateful, and unbundled Let us combine the wrapper with state: local Wrap Unwrap in {NewWrapper Wrap Unwrap} fun {NewStack} {Wrap {NewCell nil}} end proc {Push W X} C={Unwrap W} in {Assign C X|{Access C}} end fun {Pop W} C={Unwrap W} in case {Access C} of X|S then {Assign C S} X end end fun {IsEmpty S} {Access {Unwrap W}}==nil end end This version is stateful but lets us store the stack separate from the operations. The same operations work on all stacks.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy35 Four ways to package a stack Open, declarative, and unbundled: the usual declarative style, e.g., in Prolog and Scheme Secure, declarative, and unbundled: use wrappers to make the declarative style secure Secure, stateful, and bundled: the usual object-oriented style, e.g., in Smalltalk and Java Secure, stateful, and unbundled: an interesting variation on the usual object-oriented style Other possibilities: there are four more possibilities! Exercise: Try to write all of them.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy36 Exercises Write the higher-order function Map used by the Domain function in the Dictionary ADT. Solve problem 17 in Section 3.10 (pg. 232). You do not need to implement it using gump, simply specify how you would add currying to Oz (syntax and semantics). Do exercise 2 in Section 6.10 (pg. 482). Read Sections and 6.4.4