1 Modelling using sets Sets: The finite set type constructor Value definitions: enumeration, subrange, comprehension Operators on sets Case Study: the.

Slides:



Advertisements
Similar presentations
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 9 Imperative and object-oriented languages 1.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CSCI 639 Topics in Software Engineering Assignment #3 Fall 2008.
Katz Formal Specifications Larch 1 Algebraic Specification and Larch Formal Specifications of Complex Systems Shmuel Katz The Technion.
Discrete Mathematics Lecture 4 Harper Langston New York University.
Introduction to Sets. A set is just a collection of stuff But the stuff must be written inside curly braces Each item in the curly braces is separated.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Vienna Development Method SIM5104. one of the longest-established Formal Methods for the development of computer- based systemsFormal Methods Originating.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Basic Elements of C++ Chapter 2.
Vocabulary word (put this word on the back of the card.) THIS WILL BE THE DEFINITION – FILL IN THE BLANKS ON THE VOCABULARY CARDS PROVIDED.
Review of C++ Programming Part II Sheng-Fang Huang.
22 March 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Analyzing the Requirements with Formal Specifications Vienna Development Method Specification Language (VDM-SL) Book: Formal Software Development From.
Model-Based Specification CIS 376 Bruce R. Maxim UM-Dearborn.
TIVDM1Modelling ordered collections1 Peter Gorm Larsen.
CSC264 Modelling and Computation 10. Modelling State Steve Riddle, John Fitzgerald, Maciej Koutny Computing Science Semester /06.
1 C++ Syntax and Semantics, and the Program Development Process.
TIVDM1Modelling unordered collections1 Peter Gorm Larsen.
TIVDM1Modelling unordered collections1 Peter Gorm Larsen.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Copyright © 2014 Curt Hill Sets Introduction to Set Theory.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A Logic of Partially Satisfied Constraints Nic Wilson Cork Constraint Computation Centre Computer Science, UCC.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Chapter SETS DEFINITION OF SET METHODS FOR SPECIFYING SET SUBSETS VENN DIAGRAM SET IDENTITIES SET OPERATIONS.
Chapter P Prerequisites: Fundamental Concepts of Algebra 1 Copyright © 2014, 2010, 2007 Pearson Education, Inc. 1 P.1 Algebraic Expressions, Mathematical.
8. The set types The set types are one of the compound types available in SOFL, and usually used for the abstraction of data items that have a collection.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Logic Our ability to state invariants, record preconditions and post- conditions, and the ability to reason about a formal model depend on the logic.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 Modelling using Sequences Sequences The finite sequence constructor Value definitions: enumeration, subsequence Operators on Sequences Case Study: the.
Learners Support Publications Constructors and Destructors.
Week 14 - Friday.  What did we talk about last time?  Simplifying FSAs  Quotient automata.
Section 6.1 Set and Set Operations. Set: A set is a collection of objects/elements. Ex. A = {w, a, r, d} Sets are often named with capital letters. Order.
Constructors and Destructors
Chapter 2 Sets and Functions.
Chapter Topics The Basics of a C++ Program Data Types
Formal Methods in SE Lecture 21.
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
VDM: Vienna Development Method
ML: a quasi-functional language with strong typing
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Variables and Primative Types
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
Stacks Chapter 4.
Basic Elements of C++ Chapter 2.
Algebra 1 Section 1.1.
Precalculus Mathematics for Calculus Fifth Edition
The Metacircular Evaluator
Constructors and Destructors
Copyright © Cengage Learning. All rights reserved.
Chapter 2: Intro to Relational Model
Lecture Sets 2.2 Set Operations.
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

1 Modelling using sets Sets: The finite set type constructor Value definitions: enumeration, subrange, comprehension Operators on sets Case Study: the explosives store To define a type: a type constructor ways of writing down values ways of operating on values

2 The idea of a set... An unordered collection of values: The order doesn’t matter: Nor do duplicates:

3 Would you represent each of the following as a set? Why? The departures display board at an airport, showing outgoing flights in order of departure. A database keeping track of who is in a restricted area at any given time.

4 The set type constructor The finite set type constructor is: set of _ What are the types of the following expressions? {1, -3, 12} { {9, 13, 77}, {32, 8}, {}, {77} }

5 The set type constructor The type set of X is the class of all possible finite sets of values drawn from the type X. For example: set of nat1 sets of non-zero Natural numbers set of Student sets of student records set of (seq of char) sets of sequences of characters (e.g. sets of names) set of (set of int) sets of sets of integers, e.g. { {3,56,2},{-2},{},{-33,5} }

6 Defining sets... (1) Enumeration: (2) Subrange (integers only): {integer1,...,integer2} e.g. {12,...,20} = {12,...,12} = {9,...,3} = (0) Empty Set: {} e.g. {“Ann”, “Peter”}

7 Defining sets... (3) Comprehension { expression | binding & predicate } The set of values of the expression under each assignment of values to bound variables satisfying the predicate. Consider all the values that can be taken by the variables in the binding. Restrict this to just those combinations of values which satisfy the predicate. Evaluate the expression for each combination. This gives you the values in the set. e.g. { x**2 | x:nat & x < 5 }

8 Defining sets... Examples of Comprehensions: {x | x:nat & x < 5} {y | y:nat & y < 0} {x+y | x,y:nat & x<3 and y<4}

9 Defining sets … Finiteness In VDM-SL, sets should be finite, so be careful when writing comprehensions that you don’t define a predicate that could be satisfied by an infinite number of values. Example: {x | x:nat & x>10} To define infinitely large collections of values, a type is required: NewNats = nat inv n == n>10

10 Operators on Sets There are plenty of built-in operators on sets. Each one has a signature defining the number and types of operand expected, e.g. the set union operator: _ union _ : set of A * set of A -> set of A Name of the operator. The underscores show where the arguments go when the operator is used. Types of the inputs, in order. The “ * ” separates each input type. The type of the result. What can you tell about the union operator from this signature?

11 Operators on Sets _ union _ : set of A * set of A -> set of A Are the following expressions legal, according to the signature? union({4, 7, 9} {23, 6}) 3 union {7, 1, 12} {12,…,15} union {x-y | x,y:nat & x<4 and y<10} {} union {} {12} union {x**y | x,y:nat & x 2}

12 Operators on Sets In order to use operators effectively, you have to know the number and types of operands that they expect, and the types of value that they return as a result. This information is given in the notes and the text, and you really need to know it in order to use the language effectively.

13 Operators on Sets _ union _ : set of A * set of A -> set of A _ inter _ : set of A * set of A -> set of A _ \ _ : set of A * set of A -> set of A dunion : set of (set of A) -> set of A dinter : set of (set of A) -> set of A card : set of A -> nat _ in set _ : A * set of A -> bool _ subset _ : set of A * set of A -> bool Note: we don’t show the underscores when the operator is normally used in a prefix form, e.g. card {12, 45, 12, 3} = 4

14 Operators on Sets distributed operators The most common operators have special forms in which they are extended to a whole set of arguments, not just two. dunion : set of (set of A) -> set of A dinter : set of (set of A) -> set of A

15 Operators on Sets selecting elements Inside a function definition, we may need to select an arbitrary element from a set, not caring how it is selected. We can do this by using a local definition, i.e. in the body of the function say let x in set S in … (now x stands for some arbitrary member of S ) Alternatively, we could just define a general function for selecting an element from a set. Since we are not interested in the means of selection, we could do this by an implicit function definition: Select (s:set of X) result:X pre s <> {} post result in set s … and now we can use Select(_) whenever we want to select an element of a set.

16 Case Study: the explosives storage example The system to be modelled is part of a controller for a robot that positions explosives such as dynamite and detonators in a store. The store is a rectangular building. Positions within the building are represented as coordinates with respect to one corner designated the origin. The store’s dimensions are represented as maximum x and y coordinates. Objects in the store are rectangular packages, aligned with the walls of the store. Each object has dimensions in the x and y directions. The position of an object is represented as the coordinates of its lower left corner. All objects must fit within the store and there must be no overlap between objects.

17 Case Study: the explosives storage example The positioning controller must provide functions to: 1. return the number of objects in a given store; 2. suggest a position where a given object may be accommodated in a given store; 3. update a store record to note that a given object has been placed in a given position; 4. update a store record to note that all the objects at a given set of positions have been removed. Purpose of the model: to clarify the rules for the storage of explosives.

18 Case Study: the explosives storage example y x

19 Case Study: the explosives storage example Store :: contents : xbound : ybound : Object :: position : xlength : ylength :

20 Case Study: the explosives storage example Store :: contents : set of Object xbound : nat ybound : nat Object :: position : Point xlength : nat ylength : nat Point :: x : nat y : nat

21 Case Study: the explosives storage example Store :: contents : xbound : ybound : inv mk_Store(contents,xbound,ybound) == forall o in set contents & InBounds(o,xbound,ybound) and not exists o1, o2 in set contents & o1 <> o2 and Overlap(o1,o2) InBounds: Object * nat * nat -> bool InBounds(o,xb,yb) == o.position.x + o.xlength <= xbound and o.position.y + o.ylength <= ybound

22 Case Study: the explosives storage example inv mk_Store(contents,xbound,ybound) == forall o in set contents & InBounds(o,xbound,ybound) and not exists o1, o2 in set contents & o1 <> o2 and Overlap(o1,o2) Overlap: Overlap(o1,o2) == Points(o1) inter Points(o2) <> {} Points: Object -> set of Point Points(mk_Object(pos, xlen, ylen)) == {mk_Point(x,y) | x in set {pos.x, …, pos.x + xlen}, y in set {pos.y, …, pos.y + ylen}}

23 Case Study: the explosives storage example 1. return the number of objects in a given store; NumObjects: Store -> nat 2. suggest a position where a given object may be accommodated in a given store; SuggestPos: nat * nat * Store -> Store 3. update a store record to note that a given object has been placed in a given position; Place: Object * Store Point -> Store 4. update a store record to note that all the objects at a given set of positions have been removed. Remove: Store * set of Point -> Store

24 Case Study: the explosives storage example NumObjects: Store -> nat NumObject(s) == card s.contents

25 Case Study: the explosives storage example SuggestPos: nat * nat * Store -> Store SuggestPos(xlength,ylength,s) == ??? There might be any number of viable positions, but the requirements are not specific about which one ought to be returned - any point with sufficient space will do. Since we do not have to give a specific point, there is not need to give an algorithm for finding a suitable point: we can use an implicit function definition instead.

26 Case Study: the explosives storage example An implicit definition does not have a body, but does describe the result by means of a postcondition. functionName (input vars & types) result & type preprecondition postpostcondition sqrt(x:real) r:real pre x >= 0 post r*r = x

27 Case Study: the explosives storage example SuggestPos: nat * nat * Store -> [Point] SuggestPos(xlength,ylength,s) == ??? SuggestPos(xlength:nat, ylength:nat, s:Store) p:[Point] post -- if there is a point with enough room -- then return some point where there is -- enough room -- else return nil if exists poss:Point & RoomAt(xlength,ylength,s,poss) then p = poss else p = nil

28 Case Study: the explosives storage example RoomAt: nat * nat * Store * Point -> bool RoomAt(x,y,s,p) == let o1 = mk_Object(p,x,y) in InBounds(o1, s.xbound, s.ybound) and not exists o2 in set s.contents & Overlap(o2,o1)

29 Case Study: the explosives storage example 3. update a store record to note that a given object has been placed in a given position; Place: Object * Store * Point -> Store Place(o,s,p) == let o1 = mk_Object(p,o.xlength,o.ylength) in mk_Store (s.contents union {o1}, s.xbound, s.ybound ) pre RoomAt(o.xlength, o.ylength, s, p)

30 Case Study: the explosives storage example 4. Remove the objects at a given set of points in a store: Remove: Store * set of Point -> Store Remove(mk_Store(cts, xb, yb),sp) == let os = {o| o in set cts & o.position in set sp} in mk_Store (cts \os, xb, yb} pre sp subset {o.position | o in set cts}

31 Case Study: the explosives storage example An extension - Suppose we have a site which consists of a collection of stores: Store :: name : token... Site = set of Store inv site == forall store1, store2 in set site & store1.name = store2.name => store1 = store2 and we need to take an inventory of the site: Inventory = set of inventoryItem InventoryItem :: store : token item : Object

32 Case Study: the explosives storage example We could take the union of the individual inventories of each store: SiteInventory: Site -> Inventory SiteInventory(site) == dunion{StoreInventory(store) | store in set site} StoreInventory: Store -> Inventory StoreInventory(store) == {mk_InventoryItem(store.name,o) | o in set store.contents}

33 Review The set constructor: Individual sets can be defined by: enumeration subrange comprehension Operators on sets: defined with signatures; include distributed versions of union and intersection. The explosives example: use implicit specification (postcondition) when there is no need to give a particular result; use auxiliary function definitions to break down and simplify the task of building the model.