1 Introducing ASML Sequences, Parallel evaluation, Maps, Non-Determinism Lecture 12 Software Engineering COMP201.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Recognising Languages We will tackle the problem of defining languages by considering how we could recognise them. Problem: Is there a method of recognising.
Arrays.
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Statement-Level Control Structures
ISBN Chapter 8 Statement-Level Control Structures.
1 Introducing ASML ASML version 2, “Steps and Updates ” Lecture 9 Software Engineering COMP201.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page: Lecture.
1 Introducing ASML Enumerations, Conditionals and Loops, Quantifiers Lecture 13 Software Engineering COMP201.
1 Introducing ASML Classes, Structured Values, Sets, Sequences Lecture 11 Software Engineering COMP201.
1 Introducing ASML Methods, Values, Constraints, Constants, Variables Lecture 10 Software Engineering COMP201.
1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201.
ISBN Chapter 8 Statement-Level Control Structures.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Software Engineering, COMP201 Slide 1 Lectures 9,10 Formal Specifications.
Tcl/Tk 2 CS 414, Software Engineering I Mark Ardis Rose-Hulman Institute December 5, 2002.
Lecture 14 Sort Quicksort Shortest Paths
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 2: Algorithm Discovery and Design
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Statement-Level Control Structures
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Built-in Data Structures in Python An Introduction.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
W E E K F I V E Statement-Level Control Structures.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Fortran: Control Structures Session Three ICoCSIS.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming javascript arrays.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Chapter 8: Control Structures
Arrays, For loop While loop Do while loop
Statement-Level Control Structures
CS190/295 Programming in Python for Life Sciences: Lecture 6
String and Lists Dr. José M. Reyes Álamo.
Statement-Level Control Structures
Introduction to Programming
Presentation transcript:

1 Introducing ASML Sequences, Parallel evaluation, Maps, Non-Determinism Lecture 12 Software Engineering COMP201

2 I. Sequences A Sequence is a collection of elements of the same type, just as a set is. Sequences differ from sets in two ways: –A sequence is ordered while a set is not. –A sequence can contain duplicate elements while a set does not. Elements of sequences are contained within square brackets: [ ] [1,2,3,4], [4,3,2,1], [a,e,i,o,u], [a,a,e,i,o,u] X={1,2,3,4} Y={1,1,2,3,4} Z=[1,1,2,3,4] Main() step WriteLine(“X=” +X) step WriteLine (“Y=” +Y) The result is: X = {1,2,3,4} Y = {1,2,3,4} Z = [1,1,2,3,4]

3 Ordering of sequences A = {1,2,3,4} B = {4,3,2,1} C = [4,3,2,1] D = [1,2,3,4] Main() step if A eq B then WriteLine (“A = B”) else WriteLine (“A <> B”) step if C eq D then WriteLine (“C = D”) else WriteLine (“C <> D”) A = B The result is: A = B C <> D eq= ne  lt < gt > in  notin  subset  superset  subseteq  superseteq 

4 Accessing sequence entries by index Sequences are zero-based, which means that the first element in the sequence is indexed by zero (“0”) To select a specific element from the sequence, use the sequence name followed by element number, enclosed in parentheses m = [1..5] Main() step WriteLine (m(1)) The code displays the number 2

5 II. Parallel evaluation It is possible to make updates in parallel using the forall statement that evaluates all the members of a set or sequence in a single step Parallel evaluation is helpful because, it greatly reduces the number of steps required to specify an algorithm forall binders statement-list

6 Parallel Evaluation Example class Person var age as Integer Alice = new Person(20) Bob = new Person(16) Ted = new Person(40) People= {Alice, Bob, Ted} GrowOlder() forall p in People p.age := p.age + 1 var year = 2002 Main() step while year < 2010 WriteLine (“Alice’s”+Alice.age + “ in ” + year) WriteLine (“Bob’s”+Alice.age + “ in ” + year) WriteLine (“Ted’s”+Alice.age + “ in ” + year) GrowOlder() year := year + 1 S 1 Alice.age =20 Bob.age =16 Ted.age =40 S 2 Alice.age =21 Bob.age =17 Ted.age =41 S 3 Alice.age =22 Bob.age =18 Ted.age =42

7 Sequential iteration AsmL provides for sequential iteration through the elements in a collection using the step foreach while and until statement step foreach boundedvars step while expression step until ( fixpoint | expression ) If you need to use step foreach, and you are using it with sets remember that sets have no inherent order If the order is important, use sequences rather than sets

8 Sequential iteration over a collection class Person var age as Integer Alice = new Person(20) Bob = new Person(16) Ted = new Person(40) People= {Alice, Bob, Ted} GrowOlder() step foreach p in People p.age := p.age + 1 var year = 2002 Main() step while year < 2010 WriteLine(“Alice’s”+Alice.age + “ in ” + year) WriteLine (“Bob’s”+Alice.age + “ in ” + year) WriteLine (“Ted’s”+Alice.age + “ in ” + year) GrowOlder() year := year + 1 S 1 Alice.age =20 Bob.age =16 Ted.age =40 S 2 Alice.age =20 Bob.age =17 Ted.age =40 S 3 Alice.age =21 Bob.age =17 Ted.age =40

9 III. Maps Maps are tables that associate keys to values Like arrays, maps have a set of unique keys and a set of values associated with those keys Example. Map declaration var phoneNumber as Map of String to Integer Example. Enumerating map entries phoneNumber = {“Bob” –>100, “Carol” |–>101} The “ –> ” symbol associated keys with values. It is read as “map to”

10 Maps with single argument Example. Looking up values in a map var phoneNumber as Map of String to Integer = {“Bob” –>100, “Carol” –>101} Main() step WriteLine (“Carol’s extension is” + phoneNumber(“Carol”)) Example. Map-based binding var phoneNumber as Map of String to Integer = {“Bob” –>100, “Carol” –> 101, “Ted” –>102, “Alice” –>103} Main() step y = {j | i –> j in phoneNumber where j < 103} WriteLine(“The set of extensions less than 103 is”+y)

11 Map construction Map display is an enumeration of individual element-to-element associations in the form {d 1 –> r 1, d 2 –> r 2, …} Map comprehension denotes a map in terms of iterated expressions. Its from is: {expr 1 –> expr 2 | binder 1, binder 2, …} Example. Constructing maps X = {2..5} Y = {i –> i + 1 | i in X where i <4 } // same as z Z = {2 –> 3, 3 –> 4} WriteLine (z(2)) // prints 3

12 Maps with multiple arguments Maps whose keys are tuples can be thought of as multidimensional arrays. Each argument is one of the indexers into the table. var phoneNumber1 as Map of (String,String) to Integer = {(“Bob”, “Home”) –> , (“Bob”, “Work”) –> 100 )} var phoneNumber2 as Map of String to Map of String to Integer = {“Bob” –>{ “Home”) –> }} Main() step WriteLine (phoneNumber1) step WriteLine (phoneNumber2) When you declare the map, separate each of the argument types with a comma “,” and enclose them all in parentheses “(” and “)”. Example. Tuples as map keys and Nested maps

13 Map Operations dom – to find the domain of a map ran - to find the range of a map var phoneNumber as Map of String to Integer = {“Bob” –>100, “Carol” –>101} Main() step WriteLine (“The keys are” + dom(phoneNumber)) step WriteLine(“The values are” + ran(phoneNumber)) The result is: The keys are {Bob,Carol} The values are {100,101}

14 Map merge The merge operation combines two maps: A = {“Bob” –>100, “Carol” –>101, “Ted” –>100, “Carol” –>101} B = {“Jeff” –>104, “George” –>105, “Ann” –>106, “Beth” –>107} Main() step WriteLine (B merge A) The result is: {“Bob” |–>100, “Carol” |–>101, “Ted” |–>100, “Carol” |–>101, “Jeff” |–>104, “George” |–>105, “Ann” |–>106, “Beth” |–>107}

15 Partial updates of maps Along with sets, partial updates are also useful with maps. var Extension as Map of String to Integer = {–>} Main() step Extension(“Bob”) := 100 Extension(“Carol”) := 101 step WriteLine(“Bob’s extension is”+ Extension(“Bob”)) WriteLine (“Carol’s extension is”+ Extension(“Bob”)) A name (a string) is associated with an extension (an integer). Initially, the map is empty. We can then add entries to it, one person at a time

16 IV. Non-Determinism Non-deterministic systems exhibits two characteristic: –There is a finite set of possibilities –Within that set the result may be any value, but we don’t know which one In addition to accuracy, non-determinism provides flexibility –A specification should not limit the possible implementations –Non-determinism can be used to show where multiple options are possible Using non-determinism to model aspects of your system is an important part of keeping your model focused –It helps to avoid being distracted by detail that does not matter for chosen view

17 Non-deterministic choice Successful modellers are conscientious about excluding what does not matter (for the chosen level of abstraction) and including what does. Example. ND choice, expression level A = {1..10} Main() step x = any y | y in A WriteLine(“x is ” + x) //prints any element from A Example. ND choice, statement level S = {1, 6, 3} Main() step choose i in S where i > 4 WriteLine (i + “ was chosen”) // prints any // elements from A that is greater than 4 Possible ERROR !

18 Non-deterministic choice + ifnone If there are no elements that fit the qualifications, the system generates a runtime error You can avoid this with ifnone Example. Default choice S = {1,6,3} Main() step choose i in S where i > 4 WriteLine (i + “ was chosen”) // prints any // elements from A that is greater than 4 ifnone WriteLine (“There were none to choose.”)

19 External non-determinism Another kind of non-determinism occurs when you make a method call outside of AsmL (for example, into an external library) You should not assume that external functions appears in a method body will be the same as the order they are invoked at runtime, unless a “step” separates them Main() step WriteLine (“This could print second”) WriteLine (“This could print first”) step WriteLine (“This will print last”)

20 Non-determinism of “new” The “new” operator that is used to create instances of a class can be seen as an external or non- deterministic function The reason for this is that “new” expressions like new Person(“Bill”, 40)) in the earlier examples) return a different value every time they are invoked