[personal profile] sassa_nf
Prove union of sets is commutative.

Ok, there may be a problem with the question - in ZFC Union is defined on a set of sets, so no order is there.

At first I solved for a system where sets are defined through a membership function, and define union as a set where membership is a logical or of two memberships.

But in ZFC Union of a Pair of sets is not defined through membership functions. Let's show then that the two definitions of Union are equivalent.

One problem later, here it goes:

data False : Set where

data _==_ {A : Set} (x : A) : A -> Set where
   refl : x == x

data Bool : Set where
   true  : Bool
   false : Bool

_||_ : Bool -> Bool -> Bool
true  || n = true
false || n = n

-- introduce a Set (well, let's call it type S) consisting of a membership function.
data S {A : Set} : Set where
   set : (A -> Bool) -> S {A}

ismember : {A : Set} -> S {A} -> A -> Bool
ismember (set m) = m

-- then a union is a set whose membership function computes a boolean disjunction. Fair?
union : {A : Set} -> S {A} -> S {A} -> S {A}
union (set m) (set n) = set \x -> (m x) || (n x)

-- extensionality axiom: if for all x we can tell that the membership function of set m and n returns the same result,
-- then the two sets are equal
postulate ext : {A : Set} -> (m n : S {A}) -> ((x : A) -> (ismember m x) == (ismember n x)) -> m == n

{-# BUILTIN EQUALITY _==_ #-}

-- lemma: both ways of constructing a union of two sets produces equal membership function.
-- the definition of union can be used to prove that membership functions of unions return the same result
union-comm' : {A : Set} {m n : S {A}} -> (x : A) -> (ismember (union m n) x) == (ismember (union n m) x)
union-comm' {A} {set m} {set n} x with m x | n x
...                                  | false | false = refl -- break down case by case, so Agda can follow different branches in two unions
...                                  | false | true  = refl
...                                  | true  | false = refl
...                                  | true  | true  = refl

-- commutativity of union of sets: extensionality axiom combined with the lemma that both ways of
-- constructing a union of two sets produces the same membership result
union-comm : {A : Set} {m n : S {A}} -> (union m n) == (union n m)
union-comm {A} {m} {n} = ext {A} (union m n) (union n m) (union-comm' {A} {m} {n})



postulate set-pair : {A : Set} -> (m n : S {A}) -> S {S {A}}
postulate pair-member-l : {A : Set} -> (m n : S {A}) -> (ismember (set-pair m n) m) == true
postulate pair-member-r : {A : Set} -> (m n : S {A}) -> (ismember (set-pair m n) n) == true
postulate pair-is : {A : Set} {m n : S {A}} {x : S {A}} ->
                    (ismember (set-pair m n) x) == true ->
                    (m == x -> False) -> (n == x -> False) -> False

postulate set-union : {A : Set} -> S {S {A}} -> S {A}
postulate union-member : {A : Set} -> (a : S {S {A}}) ->
                         (x : A) -> (d : S {A}) ->
                                    (ismember d x) == true ->
                                    (ismember a d) == true -> (ismember (set-union a) x) == true
postulate union-set : {A : Set} -> (a : S {S {A}}) -> (x : A) -> (ismember (set-union a) x) == true -> S {A}
postulate union-set-member : {A : Set} {a : S {S {A}}} {x : A} ->
                             (p : (ismember (set-union a) x) == true) ->
                             (ismember (union-set a x p) x) == true
postulate union-set-member-set : {A : Set} {a : S {S {A}}} {x : A} ->
                                 (p : (ismember (set-union a) x) == true) ->
                                 (ismember a (union-set a x p)) == true

false-elim : {A : Set} -> False -> A
false-elim ()

bool-mismatch : false == true -> False
bool-mismatch ()

transport : {A : Set} {B : A -> Set} {x y : A} -> x == y -> B x -> B y
transport refl bx = bx

trans : {A : Set} {x y z : A} -> x == y -> y == z -> x == z
trans refl refl = refl

sym : {A : Set} {x y : A} -> x == y -> y == x
sym refl = refl

ext-inv : {A : Set} {m n : S {A}} -> m == n -> (x : A) -> (ismember m x) == (ismember n x)
ext-inv {A} {m} {n} m=n x = transport {B = \n -> (ismember m x) == (ismember n x)} m=n refl

non-eq-set : {A : Set} {m n : S {A}} {x : A} -> (ismember m x) == false -> (ismember n x) == true -> m == n -> False
non-eq-set {A} {m} {n} {x} nxm x-n m=n = bool-mismatch (trans (sym nxm) (trans (ext-inv m=n x) x-n))

union-match : {A : Set} -> (m n : S {A}) -> (x : A) ->
              ((ismember m x) || (ismember n x)) == (ismember (union m n) x)
union-match (set m) (set n) x with m x | n x
...                  | false | false = refl
...                  | true  | false = refl
...                  | false | true  = refl
...                  | true  | true  = refl

member-|| : {A : Set} {x : A} {a b : Bool} -> (m n : S {A}) ->
            (ismember m x) == a -> (ismember n x) == b -> (a || b) == ((ismember m x) || (ismember n x))
member-|| _ _ m=a n=b rewrite m=a | n=b = refl

{-- originally a dumb straightforward christmas tree of cases and matching
-- this does not require defining BUILTIN EQUALITY for rewrite syntax sugar to work
member-|| m n x a b m=a n=b with a | ismember m x | b | ismember n x
...                         | false | false | false | false = refl
...                         | false | false | true  | true  = refl
...                         | true  | true  | _     | _     = refl
...                         | false | true  | _     | _     = false-elim (bool-mismatch (sym m=a))
...                         | true  | false | _     | _     = false-elim (bool-mismatch m=a)
...                         | _     | _     | false | true  = false-elim (bool-mismatch (sym n=b))
...                         | _     | _     | true  | false = false-elim (bool-mismatch n=b)
--}

data _==?_ {A : Set} (x y : A) : Set where
   yes : x == y -> x ==? y
   no  : (x == y -> False) -> x ==? y

ismember? : {A : Set} -> (m : S {A}) (x : A) -> (ismember m x) ==? true
ismember? m x with ismember m x
...              | true  = yes refl
...              | false = no \()

not-true : {x : Bool} -> (x == true -> False) -> x == false
not-true {true}  nxm = false-elim (nxm refl)
not-true {false} _   = refl

member-union-l : {A : Set} {x : A} -> (m n : S {A}) -> (ismember m x) == true -> true == (ismember (union m n) x)
member-union-l {A} {x} m n x-m with ismember? n x
...                               | yes x-n = trans (member-|| m n x-m x-n) (union-match m n x)
...                               | no nxn  = trans (member-|| m n x-m (not-true nxn)) (union-match m n x)

member-union-r : {A : Set} {x : A} -> (m n : S {A}) -> (ismember n x) == true -> true == (ismember (union m n) x)
member-union-r {A} {x} m n x-n = transport {B = \u -> true == (ismember u x)}
                                           (union-comm {A} {n} {m})
                                           (member-union-l n m x-n) 

member-union-f : {A : Set} {x : A} -> (m n : S {A}) -> (ismember m x) == false ->
                                                       (ismember n x) == false -> false == (ismember (union m n) x)
member-union-f {A} {x} m n nxm nxn = trans (member-|| m n nxm nxn)
                                           (union-match m n x)


-- axiom that set equality is decidable
-- this is not needed for the proof of equality of two representations of unions,
-- but it is needed if we were to express union-member-r from union-member-l -
-- that requires pairs-eq
postulate sets-equal? : {A : Set} (m n : S {A}) -> m ==? n

pairs-eq' : {A : Set} -> (m n x : S {A}) -> (ismember (set-pair m n) x) == (ismember (set-pair n m) x)
pairs-eq' m n x with sets-equal? m x | sets-equal? n x
...                | yes m=x | _       = trans (transport {B = \x -> ismember (set-pair m n) x == true} m=x (pair-member-l m n))
                                          (sym (transport {B = \x -> ismember (set-pair n m) x == true} m=x (pair-member-r n m)))
...                | _       | yes n=x = trans (transport {B = \x -> ismember (set-pair m n) x == true} n=x (pair-member-r m n))
                                          (sym (transport {B = \x -> ismember (set-pair n m) x == true} n=x (pair-member-l n m)))
...                | no nxm  | no nxn  = trans (not-true \p -> pair-is p nxm nxn)
                                          (sym (not-true \p -> pair-is p nxn nxm))

pairs-eq : {A : Set} -> (m n : S {A}) -> (set-pair m n) == (set-pair n m)
pairs-eq m n = ext (set-pair m n) (set-pair n m) (pairs-eq' m n)

union-member-l : {A : Set} {m n : S {A}} {x : A} -> (ismember m x) == true -> (ismember (set-union (set-pair m n)) x) == true
union-member-l {A} {m} {n} {x} x-m = union-member (set-pair m n) x m x-m (pair-member-l m n)

union-member-r : {A : Set} {m n : S {A}} {x : A} -> (ismember n x) == true -> (ismember (set-union (set-pair m n)) x) == true
union-member-r {A} {m} {n} {x} x-n = transport {B = \u -> (ismember (set-union u) x) == true}
                                               (pairs-eq n m)
                                               (union-member-l {A} {n} {m} x-n)

union-pair' : {A : Set} -> (m n : S {A}) -> (x : A) ->
                           (ismember (set-union (set-pair m n)) x) == (ismember (union m n) x)
union-pair' {A} m n x with ismember? m x | ismember? n x
...                  | yes x-m | _       = trans (union-member-l x-m) (member-union-l m n x-m)
...                  | _       | yes x-n = trans (union-member-r x-n) (member-union-r m n x-n)
...                  | no nxm  | no nxn with ismember? (set-union (set-pair m n)) x
...                                        | yes x-u = false-elim (pair-is (union-set-member-set x-u)
                                                                           (non-eq-set (not-true nxm) (union-set-member x-u))
                                                                           (non-eq-set (not-true nxn) (union-set-member x-u)))
...                                        | no nxu  = trans (not-true nxu)
                                                             (member-union-f m n (not-true nxm) (not-true nxn))

-- The two representations of unions of sets are equivalent
union-pair : {A : Set} -> (m n : S {A}) -> (set-union (set-pair m n)) == (union m n)
union-pair m n = ext (set-union (set-pair m n)) (union m n) (union-pair' m n)

Date: 2019-11-06 06:41 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
Wow, it gets more complicated with each line. Impressive.

Do I understand it correct that you have to use underlying sets to define this `Set`?

Date: 2019-11-06 09:50 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
I see the reasons now. But Agda scares me off right now. Well... it's just a temporary condition maybe.

Date: 2019-11-07 01:14 am (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
I'd prefer the lack of implicit tactics.

Interesting about the union. Categorically, they have to be isomorphic.

Date: 2019-11-07 03:38 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
Why exactly two in the pair set? Could be one.

Date: 2019-11-07 05:15 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
That axiom is not enough; you need Separation Axiom, I think.

Date: 2019-11-07 10:41 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
Pairing axiom gives us a set C that contains both A and B, but does not guarantee that these are the only elements of C. Separation axiom ensures that we can take a subset of C containing only A and B.

Date: 2019-11-07 10:57 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
Yes. {x ∈ C | (x == A ∨ x == B)}
Edited Date: 2019-11-07 10:58 pm (UTC)

Date: 2019-11-08 02:05 am (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
This form is definitely enough.

Profile

sassa_nf

January 2026

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 22nd, 2026 05:30 pm
Powered by Dreamwidth Studios