Sets, Relations, Maps, Orders and Software CS 366 Programming Languages Spring 2015 Robert Muller The Idea - Computer software is often required to keep track of "collections" of things. - Mathematicians have thought carefully about these collections, and know them as sets. - Software is also often required to keep track of the association between items from one set (the "keys") and another (the "values"). Basic Sets - A set is a collection of items with no duplicates. - Examples: A = {1, 2, 3} B = {Bob, Alice, Joe} N = {0, 1, 2, ...} natural numbers - NB: Only restriction on elements is identity. Nomenclature - Notation: -- alpha, beta, gamma, Gamma, delta, epsilon -- A, B, C, ... for sets; -- or {} the empty set; -- a, b, c, ... for elements of sets; -- a in A means a is an element of set A; -- (a1, ..., an) is an n-tuple; Variables and Quantifiers - x, y, z for variables (which vary over sets!) - forall x in A . statement means statement holds for every element of A; - exists x in A . statement means statement holds for some element of A; Set Comprehensions - { x | statement } means set of all x such that statement holds; - Example: Evens = { x | x in N and exists y in N such that x = 2y } or Evens = { x in N | exists y in N such that x = 2y } Basic Sets - Notation: -- Subset : A subseteq B means forall x in A . x in B; -- Set Equality : A = B means A subseteq B and B subseteq A; Operations on Sets - Union : A1 union A2 = { a | a in A1 or a in A2}; - Intersection : A1 intersect A2 = { a | a in A1 and a in A2}; - Disjoint Union : A1 + ... + An = { (a, i) | a in Ai }; - Product : A1 x ... x An = { (a1, ..., an) | ai in Ai }; Operations on Sets : Sequences - Let A be a set and let e denote empty. A* = { w | w = e or w = aw' with a in A & w' in A*} Example: {a, b}* = {e, ae, ae, aae, abe, ... } Notes: - This is an example of an inductive definition, - Trailing es are usually omitted. Operations on Sets : Powerset - Powerset : P(A) = { A' | A' subseteq A}; Example : P({1,2}) = {{}, {1}, {2}, {1, 2}}; Diversion: Russell's Paradox - Let A = { B | B notin B}. E.g., {a} in A. - Assume that A notin A. Then A in A. - Assume that A in A. Then A notin A. - So A in A iff A notin A. Naive set theory is inconsistent. Relations - R is a(n n-ary) relation on A1, ..., An if and only if R subseteq A1 x ... x An; - When R is an n-ary relation on A1, ..., An and A1 = ... = An we say that R is an n-ary relation on A1; - When R is a finite set we say it is a finite relation. Domain of Definition of a Relation - For the special case n = 2 of a binary relation R on A1, A2 we define: DomDef(R) = { a1 in A1 | exists a2 in A2 . (a1, a2) in R} Example Relations A = {1, 2, 3}, B = {Bob, Alice} - R1 = A x B = {(1, Bob), (1, Alice), (2, Bob), (2, Alice), (3, Bob), (3, Alice)} - R2 = {} - R3 = {(1, Bob), (3, Alice)} // e.g., DomDef(R3) = {1, 3} - R4 = {(1, Alice), (2, Alice), (3, Alice)} Partial Maps (aka Partial Functions) - Let R be a binary relation on A, B. R is a partial map from A to B if and only if whenever (a, b) and R and (a, b') and R then b = b'. Examples A = {1, 2, 3}, B = {Bob , Alice} - R1 = A x B = {(1, Bob), (1, Alice), (2, Bob), (2, Alice), (3, Bob), (3, Alice)} - R2 = {} - R3 = {(1, Bob), (3, Alice)} - R4 = {(1, Bob), (2, Bob), (3, Bob)} Maps/Functions - We usually use f, g, h, ... for partial maps and we write: f(a) = b in place of (a, b) in f; - Let f be a partial map from A to B. f is a total map from A to B iff DomDef(f) = A. Examples A = {1, 2, 3}, B = {Bob, Alice} - R1 = A x B = {(1, Bob), (1, Alice), (2, Bob), (2, Alice), (3, Bob), (3, Alice)} - R2 = {} - R3 = {(1, Bob), (3, Alice)} - R4 = {(1, Bob), (2, Bob), (3, Bob)} New Operations on Sets - The set of all partial maps from A to B: A -o-> B = { f | f is a partial map from A to B } - The set of all total maps from A to B: A --> B = { f | f is a total map from A to B } Examples {1, 2} --> {Bob, Alice} = { {(1, Bob), (2, Bob)}, {(1, Alice), (2, Alice)}, {(1, Bob), (2, Alice)}, {(1, Alice), (2, Bob)} } {1, 2} -o-> {Bob, Alice} = {1, 2} --> {Bob , Alice} U { {}, {(1, Bob)}, {(1, Alice)}, {(2, Bob)}, {(2, Alice)} } Predicates - Let Bool = {true, false}; = {void} + {void} = {(void, 0), (void, 1)} - P is a predicate on A if P is a map from A to Bool. - Example: >2 is a unary predicate on N: >2 = {(0, false), (1, false), (2, false), (3, true), ...} Implementing Maps Finite maps (from A --> B, or A -o-> B) implemented with a data structure such as hash table, e.g., Java's HashMap - The set of "keys" in A must be identifiable (see equivalence below) - Other efficient data structures can be used if A is totally ordered Order Properties of Relations - Let R be a relation on A. R is reflexive iff forall x in A . (x, x) in R; - Let R be a relation on A. R is transitive iff forall x, y, z in A. If (x,y) in R and (y,z) in R then (x,z) in R - A relation that is both reflexive and transitive is called a preorder. Partially Ordered Sets - Let R be a binary relation on A. R is symmetric iff forall x, y in A . If (x, y) in R then (y, x) in R; - Let R be as above. R is antisymmetric iff forall x, y in A. If (x, y) in R and (y, x) in R then x = y; - A symmetric preorder is called an equivalence relation. - An antisymmetric preorder is called a partial order. Partially Ordered Sets - If R is a reflexive, antisymmetric and transitive binary relation on A, we say that -- R is a partial order on set A -- The set A is partially ordered by R -- A is a partially ordered set (not mentioning R) -- A is a poset Partially Ordered Sets - If set A is partially ordered by R, we write -- (A, R) or more often -- (A, <=R) or (A, <=) if R is implied in context; - For a, a' in A, instead of writing (a, a') in R we usually write a <=R a' or a <= a' if R is implied. - If a <= a' and a != a' we write a < a'. Examples A = {Bob, Alice}, R = {(Bob, Bob), (Alice, Alice), (Bob, Alice)} Hasse Diagram of R Alice | Bob Examples A = P({Bob, Alice, Joe}) = { {}, {Bob}, {Alice}, {Joe}, {Bob, Alice}, {Bob, Joe}, {Alice, Joe}, {Bob, Alice, Joe} } R = subseteq = {({}, {}), ({}, {Bob}), ({}, {Bob, Alice}), ...} Hasse Diagram of A Totally Ordered Sets - Let R be a partial order on A. R is a total order on A iff forall x, y in A . either (x, y) in R or (y, x) in R. - Example : (N, <=) Lexicographic Ordering - Let A be a set (partially ordered by <=A). - We derive a partial order <=A* on A* the sequences of elements from A. - w <=A* w' iff either w = e or w = av, w' = a'v' and either a and -->. Of these, sum, product, sequence, -o-> have direct computational interpretations.