380 likes | 543 Views
Z Types and Sets. Every Z expression (arithmetic, set) has a type that can be determined A type is either a given set, or a compound type built up from simpler types using a type constructor A set is not necessarily a type, but a type is a set
E N D
Z Types and Sets • Every Zexpression (arithmetic, set) has a type that can be determined • A type is either • a given set, or • a compound type built up from simpler types using a type constructor A set is not necessarily a type, but a type is a set • There are a few predefined sets: Z , N , N1 • All sets must contain elements of the same type • The only predefined type is Z • N is a set, but all of its elements are members of Z
Predicates, Equations • There is no Boolean type in Z: • Predicates are not expressions, they are assertions about values. • Equations are not expressions, they are predicates that equate to values.
Possible Set Definitions Note All types are disjoint (not for sets that are used as types) All terms have a unique type
Defining types and abbreviations [CHAR] introduce a basic data type by enclosing its name in square brackets COLOR ::= red | green | blue introduce a free type by enumerating its elements TEXT == seq CHAR introduce an abbreviation by using “==“ Convention: upcase
Normalization • Normalization: stating the types (not just the sets) of each variable SMALL = { 1, 2, 3 } BIG = { 100, 200, 300} s, b: Z s: SMALL; b: BIG s SMALL b BIG un-normalized (declaration with sets) normalized
Expressions • Sets • union: { 1, 2, 3} {3, 4} = { 1, 2, 3, 4} • difference: {1, 2, 3, 4} \ { 2, 3} = { 1, 4} • intersection: {1, 2, 3, 4} {2, 3} = { 2, 3} • cardinality: # { red, white, blue} = 3 • Arithmetic • mathematical tool-kit contains standard operators: +, -, * • Integer division only 13 div 5 = 2 13 mod 5 = 3
Z Basic Constructs • Declarations introduce variables. • Expressions describe values that variables can assume. • Predicatesplace constraints on the values that variables do assume.
Declarations and Variables • Variables are declared as being of a type or set: i: Z color: { red, yellow, green } • Variables may be constrained when defined: size: N size > 640
Axiomatic definitions Declarations above the line iroot: N → N • a: N• iroot(a)*iroot(a) a < (iroot(a)+1) * (iroot(a)+1) Z paragraph Predicates below the line
Tuples and Records • Cartesian product types (or cross product types) can be used to describe tuples: DAY == 1..31; MONTH == 1..12; YEAR == Z DATE == DAY × MONTH × YEAR landing, birth: DATE landing = (20, 7, 1969) birth = (7, 4, 1949)
Relations • A set of tuples defines a relation [NAME] ID == N DEPT ::= admin | man | rsrch EMPLOYEE == ID × NAME × DEPT Employee: P EMPLOYEE Employee = { ( 111, Larry, admin ) ( 222, Curly, man ) ( 333, Mo, rsrch ) }
Binary Relations • A set of 2-tuples (pairs) defines a binary relation [NAME] PHONE == 0..9999 phone: NAME PHONE phone = { groucho | 1234, harpo | 2345, chico | 3456 }
Domain and Range • Recall this binary relation: phone = { groucho | 1234, harpo | 2345, chico | 3456 } domphone = { groucho, harpo, chico } ran phone = { 1234, 2345, 3456 }
Operators for Relations • Restriction { groucho, harpo } <| phone = { groucho | 1234, harpo | 2345 } phone |> (3000..3999) = { chico | 3456 } • Overriding phone + { groucho | 5555 } = { groucho | 5555, harpo | 2345, chico | 3456 } • Inverse phone ~ = { 5555 |groucho, 2345 |harpo, 3456 | chico }
Sequences • sequences are functions, which are relations, which are sets week day == {1 |Monday, 2 | Tuesday, 3 |Wednesday, 4 | Thursday, 5 |Friday} weekday == <Monday, Tuesday, Wednesday, Thursday, Friday>
Sequence operations head: head (<Good, Fair, Poor>) = Good last: last (<Good, Fair, Poor>) = Poor tail: tail (<Good, Fair, Poor>) = <Fair,Poor> front: front (<Good, Fair, Poor>) = <Good, Fair> Concatenation: week == <Sunday> weekday <Saturday>
Lambda Expression Function isqr isqr == {i: Z• i i*i} (def using set) isqr == ( i: Z • i*i ) (lambda expression) isqr: Z → N i: Z •isqr i = i*i
Set and Type Set Name: Z, DICE Set Expression: {i: Z|1 i 6} Every object belongs to a set called its type. Free Type Basic Type Z, [NAME] - include indefinite number of elements
Exercise: Set Expression in Z SetName == {x: T | pred(x)• expr(x)} Natural numbers: N == {i: Z|i 1} Odd numbers: ODD == {i: Z• 2*i+1} // the first part introduces local variables; their scope includes the expression after • Prime numbers:
Introduce Variables in Z Declaration: x: Z Axiomatic definitions: define global, optional constraints size: N size > 640 Normalized Declaration: s, b: Z s SMALL b BIG
Expressions and Predicates • Expressions: computing values • Constants: 1, 2, red • Operators on constants and variables: arithmetic, set • Predicate: constraining values – return true/false • Equality • Arithmetic relations • Set membership • Set relations • Logic: building complex predicate
Structures • Tuples: instances of Cartesian product types [NAME] ID==N DEPT::= admin|manufacturing|research EMPLOYEE == ID× NAME ×DEPT • Relations: a set of tuples (table/database)
Binary Relations and Functions Function: binary relations where each element in the domain appears just once phonef(doug) = 4107
Lambda Expressions Define functions ( declaration | predicate expression) Use functions without writing declarations Compare to set definitions! •
Sequence: Model Array and Lists Sequence: <>