TIVDM1Modelling ordered collections1 Peter Gorm Larsen.

Slides:



Advertisements
Similar presentations
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Advertisements

Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Models of Concurrency Manna, Pnueli.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
1 Modelling using sets Sets: The finite set type constructor Value definitions: enumeration, subrange, comprehension Operators on sets Case Study: the.
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.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Vienna Development Method SIM5104. one of the longest-established Formal Methods for the development of computer- based systemsFormal Methods Originating.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Predicates and quantifiers Chapter 8 Formal Specification using Z.
JavaScript, Third Edition
Chapter 9 Introduction to Arrays
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Sequences for system modelling. At the end of this lecture you should be able to: provide a definition of a VDM sequence; identify situations in which.
Functional Programming Element of Functional Programming.
Sequences At the end of this lecture you should be able to: provide a definition of a VDM sequence; utilize and interpret sequence notation; make appropriate.
Sequences At the end of this lecture you should be able to: provide a definition of a VDM sequence; utilize and interpret sequence notation; make appropriate.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Analyzing the Requirements with Formal Specifications Vienna Development Method Specification Language (VDM-SL) Book: Formal Software Development From.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Formal Methods in SE Lecture 20. Agenda 2  Relations in Z Specification Formal Methods in SE.
TIVDM1Modelling ordered collections1 Peter Gorm Larsen.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
TIVDM1Modelling unordered collections1 Peter Gorm Larsen.
TIVDM1Modelling unordered collections1 Peter Gorm Larsen.
Chapter 9: Functional Programming in a Typed Language.
Built-in Data Structures in Python An Introduction.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
TIVDM1Modelling relationships1 Peter Gorm Larsen.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
TIVDM1Sortation System, Tray Allocation1 Tray allocation for a sortation system Iteration III TI-VDM1 Project by José Antonio Esparza and Kim Bjerge.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
1 Modelling using Sequences Sequences The finite sequence constructor Value definitions: enumeration, subsequence Operators on Sequences Case Study: the.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Functional Programming Lecture 3 - Lists Muffy Calder.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
String and Lists Dr. José M. Reyes Álamo.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Formal Methods in SE Lecture 21.
Types CSCE 314 Spring 2016.
VDM: Vienna Development Method
ML: a quasi-functional language with strong typing
Sequences and Indexing
A lightening tour in 45 minutes
VCU, Department of Computer Science CMSC 302 Sequences and Summations Vojislav Kecman 9/19/2018.
An aggregation mechanism
String and Lists Dr. José M. Reyes Álamo.
Python Primer 1: Types and Operators
PROGRAMMING IN HASKELL
Announcements Quiz 5 HW6 due October 23
Topics Basic String Operations String Slicing
Introduction to Computer Science
Python Review
Chapter 17 JavaScript Arrays
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

TIVDM1Modelling ordered collections1 Peter Gorm Larsen

TIVDM1Modelling ordered collections2 Agenda  Sequence Characteristics and Primitives The Congestion Warning System

TIVDM1Modelling ordered collections3 Sequence Characteristics Sequences are ordered collections of elements There can be many copies of each element The elements themselves can be arbitrary complex, e.g. they can be sequences as well Sequences in VDM++ are finite Sequence types in VDM++ are written as: seq of Type seq1 of Type (for non-empty sequences)

TIVDM1Modelling ordered collections4 Sequence Enumeration A sequence enumeration consists of a comma- separated list enclosed between square brackets, ”[…]” For example [1,5,8,1,3] [true, false] [{}, {4,3},{2,4}] [‘g’,’o’,’d’] [3.567, ,7,7,7,7] Are all sequences The empty sequence can be written as “[ ]”

TIVDM1Modelling ordered collections5 Sequence Length The length of a sequence is the number of elements in the sequence i.e. its size Multiple occurrences of the same value counts The length of a sequence L is written as “ len L” Quick examples: len [1,2,3] len [ ] len [3,2,3,2]

TIVDM1Modelling ordered collections6 Sequence Equality Two sequences are equal if both have the same length and for all indices in the sequences the respective index values are equal Quick examples: [2,4,1,2] = [4,1,2] [true, true, false] = [false, true] [1,1,1,1,1,1,1,1,1,1,1,1] = [1] [{3,4,5},{4}] = [{3,5,4},{4,4,4}]

TIVDM1Modelling ordered collections7 Sequence Head and Tail A non-empty sequence can be divided into its head ( hd ) and its tail ( tl ). The head of a sequence is the first element The tail of a sequence is the rest of the sequence Quick examples: hd [1,2,3,4,5] tl [1,2,3,4,5] hd [[5],[6,1],[4,4,4]] tl [[5],[6,1],[4,4,4]]

TIVDM1Modelling ordered collections8 Sequence Elements It is possible to extract the elements of a sequence using an elems operator elems takes a sequence an yield a set of its elements (i.e. destroying the ordering information) Quick examples: elems [1,2,2] elems [ ] elems [[3],[2,3],[1]]

TIVDM1Modelling ordered collections9 Sequence Indices It is possible to get hold of the indices for a sequence using the inds operator In VDM++ indexing starts with 1 Quick examples: inds [1,2,21,6,5] inds [{ }, {true}] inds [ ] inds [[3,2],[3],[1]]

TIVDM1Modelling ordered collections10 Sequence Application Given a non-empty sequence it is possible to hold of its contents at the i th index Sequence application is written as function application, i.e. sequence(index expression) Quick examples: [1,2,21,6,5](3) [{ },{false}](2) [[3,2],[3,1],[4]](1)

TIVDM1Modelling ordered collections11 Sequence Modification Given a non-empty sequence it is possible to obtain a new sequence where the contents of certain indices are changed A sequence modification expression looks as: sequence ++ modified mapping The modified mapping goes from index to new value at that index Quick examples [{2,4},{3,1,2},{2,3,4,3}] ++ {1 |-> {}} [[2,4],[3,1,1],[ ]] ++ {2 |-> [7,5],1 |-> [8]} [{true},{false},{}] ++ {3 |-> {true,false}}

TIVDM1Modelling ordered collections12 Sequence Concatenation Two sequences A and B can be concatenated together to form a new sequence where A’s elements are followed by B’s elements Sequence concatenation is written as ”A ^ B” Quick examples: [1,2,2] ^ [1,6,5] [ ] ^ [true] [{3,2},{3},{1}] ^ [{4}]

TIVDM1Modelling ordered collections13 Distributed Sequence Concatenation If we have a sequence of sequences then the elements can be concatenated together in a distributed fashion Distributed sequence concatenation is written as ”conc SS” where SS is a sequence of sequences Quick examples: conc [[1,2,2], [1,6,5], [ ], [8,3]] conc [[ ],[true],[false]] conc [[{3,2},{3},{1}],[ ],[{9,5}],[{4}]]

TIVDM1Modelling ordered collections14 Sequence Operators hd l Head seq1 of A -> A tl l Tail seq1 of A -> seq of A len l Length seq of A -> nat elems l Elements seq of A -> set of A inds l Indexes seq of A -> set of nat1 l1 ^ l2 Concatenation seq of A * seq of A -> seq of A conc ll Distr. conc. seq of seq of A -> seq of A l(i) Seq. application seq1 of A * nat1 -> A l ++ m Seq. modification seq1 of A * map nat1 to A -> seq1 of A l1 = l2 Equality seq of A * seq of A -> bool l1 <> l2 Inequality seq of A * seq of A -> bool

TIVDM1Modelling ordered collections15 Sequence Comprehensions Using predicates to define sequences implicitly In VDM++ formulated like: [element | numeric set binding & predicate] The predicate part is optional The numeric order of the binding is used to determine the order in the sequence The smallest number is taken to be the first index Quick examples [3 * x | x in set {0,…,2}] [x | x in set {0,…,4} & x > 2]

TIVDM1Modelling ordered collections16 Questions What are the sequence enumerations for: [x|x in set {8,…,1} & x < 3] [x|x in set {1,…,10} & x > 3 and x < 6] [{y}| y in set {3,1,7,3}] [x+6| x in set {1,2}] [mk_(x,8)| x in set {1,2,7} & x > 4] [y|y in set {0,1,2} & exists x in set {0,…,3} & x = 2 * y] [x = 7| x in set {1,…,10} & x < 6]

TIVDM1Modelling ordered collections17 Sub-sequence Expressions A subsequence of a sequence L is a sequence formed from consecutive elements of L; from index n1 up to and including index n2. It has the form: L(n1,..., n2) where n1 and n2 are integer expressions. Quick Examples [5,4,3,7,8,2](2,…,4) [5,4,3,7,8,2](-6,…,4) [5,4,3,7,8,2](2,…,8) [5,4,3,7,8,2](6,…,4)

TIVDM1Modelling ordered collections18 Agenda Sequence Characteristics and Primitives  The Congestion Warning System

TIVDM1Modelling ordered collections19 The Congestion Warning System A system for warning drivers of upcoming congestion on highways with lower speed limits to reduce the likelihood of collisions.

TIVDM1Modelling ordered collections20 The Main CWS Components Sensors: These are used to derive status information about the traffic. Sensors include video cameras, radar and human observers. Traffic Controls: This interpret the data coming from sensors and take appropriate action. Actuators: These are used to signal to the drivers about potential congestions. Here traffic signs will be used but different technologies could be envisaged as well.

TIVDM1Modelling ordered collections21 Overview of the CWS System

TIVDM1Modelling ordered collections22 UML Class Diagram for CWS

TIVDM1Modelling ordered collections23 Example Journey Plan class CWS … instance variables roadNetwork: seq of CongestionMonitor := []; sensors : seq of PassageSensor := []; inv len roadNetwork = len sensors; am: ActuatorManager := new ActuatorManager(); op: OperatorControl := new OperatorControl(); types Location = nat1 end CWS

TIVDM1Modelling ordered collections24 Multiple Assignment Statements We somehow need to update the roadNetwork and the sensors instance variables synchronously to ensure the invariant VDM++ Construct: atomic (assignment statement 1; assignment statement 2;... assignment statement n )

TIVDM1Modelling ordered collections25 The AddCongestionMonitor Operation public AddCongestionMonitor: Location ==> () AddCongestionMonitor(loc) == (def sensor = new PassageSensor(loc); cm = new CongestionMonitor(loc, sensor, am, op) in let numberOfWarners = len roadNetwork in atomic(roadNetwork := roadNetwork(1,...,loc) ^ [cm] ^ roadNetwork(loc+1,..., numberOfWarners); sensors := sensors(1,...,loc) ^ [sensor] ^ sensors(loc+1,...,numberOfWarners) ); am.AddActuator(loc) )

TIVDM1Modelling ordered collections26 Different kinds of Sensors

TIVDM1Modelling ordered collections27 Sensors and PassageSensors class Sensor instance variables protected location: CWS`Location end Sensor class PassageSensor is subclass of Sensor instance variables passages: seq of CWS`Speed := [] … operations public PassageSensor: CWS`Location ==> PassageSensor PassageSensor(loc) == location := loc; end PassageSensor

TIVDM1Modelling ordered collections28 Finding the Average Speed class PassageSensor is subclass of Sensor … public AverageSpeed: nat1 ==> CWS`Speed AverageSpeed(numberOfPassages) == ( dcl accSpeed: CWS`Speed := 0; let passInAccount = passages(1,...,numberOfPassages) in ( for speed in passInAccount do accSpeed := accSpeed + speed; return (accSpeed/numberOfPassages) ) ) pre len passages >= numberOfPassages end PassageSensor

TIVDM1Modelling ordered collections29 The Congestion Sensor class CongestionSensor is subclass of Sensor types public CongestionStatus = | | operations public CongestionSensor: PassageSensor ==> CongestionSensor CongestionSensor(sensor) == passageSensor := sensor; public IssueCongestionStatus: () ==> CongestionStatus IssueCongestionStatus() == def averageSpeed = passageSensor.AverageSpeed(noPassages) in if averageSpeed < congestionThreshold then return elseif averageSpeed > noCongestionThreshold then return else return end CongestionSensor

TIVDM1Modelling ordered collections30 Actuator Structure as: seq of Actuator public Signal = | | ;

TIVDM1Modelling ordered collections31 Show Signal in Actuation Manager class ActuationManager … public ShowSignal: CWS`Location * CongestionMonitor`Signal ==> () ShowSignal(location, signal) == (let downstream = as(location + 1), actuator = as(location), upstream = as(location - 1) in -- Set the right signal at the location itself (ShowSignalAtLoc(signal,downstream,actuator); -- Set the right signal upstream ShowSignalUpstream(signal,upstream) ) ) pre location in set {2,..., len as -1} and (signal = or signal = ); end ActuationManager

TIVDM1Modelling ordered collections32 Show Signal at a given Location class ActuationManager … ShowSignalAtLoc: CongestionMonitor`Signal * Actuator * Actuator ==> () ShowSignalAtLoc(signal,downstream,actuator) == if signal = then def downstreamsignal = downstream.GetSignal() in if downstreamsignal = then actuator.SetSignal( ) else actuator.SetSignal( ) else def currentsignal = actuator.GetSignal() in let safest = MostRestrictive(currentsignal, signal) in actuator.SetSignal(safest); end ActuationManager

TIVDM1Modelling ordered collections33 Most Restrictive Signal class ActuationManager … functions MostRestrictive: CongestionMonitor`Signal * CongestionMonitor`Signal -> CongestionMonitor`Signal MostRestrictive(s1, s2) == if s1 = or s2 = then elseif s1 = or s2 = then else end ActuationManager

TIVDM1Modelling ordered collections34 Adding and Replacing Actuators class ActuationManager … public AddActuator: CWS`Location ==> () AddActuator(loc) == def act = new Actuator() in as := as(1,...,loc) ^ [act] ^ as(loc+1,..., len as) pre loc in set inds as; public ReplaceActuator: CWS`Location ==> () ReplaceActuator(loc) == def act = new Actuator() in as := as ++ {loc |-> act} pre loc in set inds as; end ActuationManager

TIVDM1Modelling ordered collections35 Operator Control class OperatorControl … instance variables messageLog: seq of seq1 of char := []; locations : seq of CWS`Location := []; inv len messageLog = len locations end OperatorControl

TIVDM1Modelling ordered collections36 Manipulating Log Messages class OperatorControl … operations public ResetLog: () ==> () ResetLog() == atomic (messageLog := []; locations :=[] ); public WriteLog: seq1 of char * CWS`Location ==> () WriteLog(message, location) == atomic (messageLog := messageLog ^ [message ^ ConvertNum2String(location)]; locations := locations ^ [location] ); end OperatorControl Notice that WriteLog has an error in the book. This is the right version.

TIVDM1Modelling ordered collections37 Operator Utilities class OperatorControl … operations public CongestionSpots: () ==> set of CWS`Location CongestionSpots() == return elems locations; ConvertLog2File: () ==> seq of char ConvertLog2File() == return conc messageLog end OperatorControl

TIVDM1Modelling ordered collections38 Summary What have I presented today? The notion of sequences as ordered collections The basic operations in VDM++ for manipulating sequences The congestion warning system example What do you need to do now? Continue with your project Present your status to all of us Read chapter 8 before next lecture

TIVDM1Modelling ordered collections39 Quote of the day By Sir Francis Darwin ( ) In science the credit goes to the man who convinces the world, not the man to whom the idea first occurs.