CSC302 2005S, Class 17: Assertions about arrays Admin * Read and ask questions about chapter 6 * Variant: Why are we doing this stuff and how will it help you? * Homework due! * Questions on chapter 5 answered online Overview * Ways to think of arrays * Notations to represent arrays formally * Graphical notation (less formal, but still formal) * Extended Example What is an array? * Arrays are a lot like functions from a domain (indices) to range (type of contents) * Slightly different notation than traditional functions f[x] rather than f(x) * Easier to mutate * Implementation perspective: Collection of values contiguous in memory * Allows us to get quick set and get. * For arrays whose indices start at 0, the value in position p is at memory location "base of array" + p * "size of element" * Client perspective: Arrays as ADT. Care about primary methods * set the value at position p * get the value at position p * assignment (maybe) * get the size * If the base is other than 0, get the valid indices * Assumption: get and set are *fast* (constant time) * Another perspective: An array is a shorthand for a group of subscripted variables Why are we studying arrays in Gries? * Fundamental data structure (or building block of data structures) * Almost every ds is a combination of arrays, records, pointers, and primitive types * And records are just a notational convenience * Many of the easy-to-study examples for program verification involve arrays * Lots and lots of important algorithms involve arrays Need notations to study arrays! * Notation for arrays in the language we're defining var a : array[lb:ub] of type -- one dimensional arrays var a : array[lb1:ub1][lb2:ub2]...[lbn:ubn] of type -- multidimensional arrays var a : array[lb1:ub1][lb2:ub2] of type -- 2D arrays Why have all the arrays we've seen (in Scheme, Java, C, and that ilk) been 0-based? * Starting at 0 makes sense to computer scientists * Can make iteration easier In C for (i=size; !i; --i) Is that easier than for (i = A.ub; i >= A.lb; i--) * Makes relating number of elements and indices easy * Simplifies implementation of arrays: base-of-array + ... Wirth designed Pascal to be good for novices (particularly students) * Don't force the student to figure out how to convert indices from "logical domain" to "actual 0-based domain" * Errors tend to crop up * Harder to verify that the index is correct * Likely that the compiler can optimize the conversion better than the programmer * Notation for arrays in the "semantic language" * Constant: ( val0, val1, val2, val3, ... valn ) * Note: Does not specify lb and ub * In terms of already-defined arrays * b = (a; i:x) - an array almost exactly like a, except that this new array has value x at position i. b[i] = x * We can build all sorts of assertions/predicates about arrays * Two arrays are equal b = (val0, val1, val2 ) * The lower-bound or upper-bound of an array is a particular value b.lb = 1; b.ub = 3; * Two arrays are permutations of each other perm(a,b) * An array is in sorted order ordered(a) * A single number can represent an array of the appropriate size of that number E.g., if we use 5 in the context of an array of size 4, it means (5, 5, 5, 5) * a[newlb:newub] represents "the portion of the array between newlb and newub, inclusive" * We often build assertions about the whole array from assertions about portions, as in sorting Some subtleties about the notation * For a particular i, perm((a,i:x), a) is the same as saying a[i] = x * Particular from reading perm((b,k:x), B) is a shorthand for perm(b,B), b[k] = x A very different notation: The array picture N parts index0 index1 index2 index3 index4 +------------+--------------------+---------+---------+ | |assertion about | | | | |values with indices | | | | |index1 to index2 | | | +------------+--------------------+---------+---------+