1 / 38

Z Types and Sets

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

cutter
Download Presentation

Z Types and Sets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. Built in Operators

  4. Set

  5. Set

  6. Set

  7. Set

  8. Types

  9. Compound Type

  10. Possible Set Definitions Note All types are disjoint (not for sets that are used as types) All terms have a unique type

  11. 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

  12. 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

  13. Normalization

  14. 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

  15. Variables

  16. Z Basic Constructs • Declarations introduce variables. • Expressions describe values that variables can assume. • Predicatesplace constraints on the values that variables do assume.

  17. 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

  18. Axiomatic Definitions

  19. 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

  20. 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)

  21. 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 ) }

  22. 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 }

  23. Domain and Range • Recall this binary relation: phone = { groucho | 1234, harpo | 2345, chico | 3456 } domphone = { groucho, harpo, chico } ran phone = { 1234, 2345, 3456 }

  24. 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 }

  25. 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>

  26. 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>

  27. Functions

  28. 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

  29. 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

  30. 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:

  31. 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

  32. 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

  33. Exercise: Reading Z logic operations and predicate

  34. 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)

  35. Binary Relation and Relation Calculus

  36. Binary Relations and Functions Function: binary relations where each element in the domain appears just once phonef(doug) = 4107

  37. Lambda Expressions Define functions ( declaration | predicate expression) Use functions without writing declarations Compare to set definitions! •

  38. Sequence: Model Array and Lists Sequence: <>

More Related