1-- Derived consequences of Foundation2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Set.Foundation8import Relation910-- The nth downward membership layer starting from {x}.11def layer (x : Set, n : Nat) →Set12| x, zero => {x}13| x, succ(n) =>⋃(layer(x, n))1415-- Replacement collects all finite layers; their union is the16-- transitive closure containing x.17def layers (x : Set) : Set:= { layer(x, n) | n ∈ ℕ }18def closure (x : Set) : Set:=⋃(layers(x))1920-- Membership restricted to a set, represented as a relation set.21def membershipOn (A : Set) : Set:= { p ∈ A × A |∃ x ∈ A, ∃ y ∈ A, x ∈ y ∧ p = (x, y) }2223theorem layer.zero (x : Set)24shows layer(x, 0) = {x}25proof26simp [layer, nat.zero]2728theorem layer.next (x : Set, n : Nat)29shows layer(x, succ(n)) =⋃(layer(x, n))30proof31simp [layer]3233theorem omega.natural (n : Set)34requires hn : n ∈ ℕ35showsNat(n)36proof37simp [nat.omega]3839theorem omega.successor (n : Set)40requires hn : n ∈ ℕ41shows succ(n) ∈ ℕ42proof43have hnat := omega.natural with hn44have infinity :=set.infinity45cases infinity with hzeroIn ∧ hsuccessor46specialize hsuccessor with n, hn47have hsucc := nat.succ with hnat48 rwa hsucc4950theorem closure.contains (x : Set)51shows x ∈ closure(x)52proof53unfold closure54applyset.union.backward55witness layer(x, 0)56constructor57apply layers.membership.backward58witness059constructor60have infinity :=set.infinity61cases infinity with hzeroIn ∧ hsuccessor62 rwa nat.zero63rfl64have hzero := layer.zero at x65rw hzero66exact singleton.self6768theorem closure.transitive (x : Set)69shows∀ y ∈ closure(x), ∀ z ∈ y, z ∈ closure(x)70proof71intros y hy z hz72unfold closure at hy73simp [set.union] at hy74obtain⟨L, hLy⟩:= hy75cases hLy with hL ∧ hyL76simp [layers] at hL77obtain⟨n, hnLayer⟩:= hL78cases hnLayer with hn ∧ hLayer79unfold closure80applyset.union.backward81witness layer(x, succ(n))82constructor83apply layers.membership.backward84witness succ(n)85constructor86exact omega.successor with hn87rfl88have hnext := layer.next at x, n89rw [hnext, hLayer]90exactset.union.backward with⟨y, hyL, hz⟩9192theorem membershipOn.relates (a b A : Set)93requires ha : a ∈ A94requires hb : b ∈ A95shows relates(membershipOn(A), a, b) ↔ a ∈ b96proof97constructor98 . intro h99unfold relates at h100simp [membershipOn] at h101obtain⟨hproduct, x, hxA, y, hyA, hxy, hpair⟩:= h102obtain⟨hax, hby⟩:= orderedPair.injective with hpair103rw hax104 rwa hby105 . intro h106unfold relates107exact membershipOn.backward with⟨(product.pair with ha, hb), a, ha, b, hb, h, rfl⟩108109theorem membershipOn.relation (A : Set)110shows RelationBetween(membershipOn(A), A, A)111proof112simp [RelationBetween, Subset]113114theorem membershipOn.wellFounded (A : Set)115shows wellFoundedOn(membershipOn(A), A)116proof117intros s hs118cases hs with hsubset ∧ hnonempty119obtain⟨m, hmMinimal⟩:=set.foundation with hnonempty120cases hmMinimal with hm ∧ hminimal121witness m122constructor123exact hm124intros y hy hrel125have hmA := hsubset with hm126have hyA := hsubset with hy127have relation := membershipOn.relates with hyA, hmA128simp [relation] at hrel129exact hminimal with y, hrel, hy130131-- This tiny theorem is a regression test for the derived induction tactic.132theorem membership.induction_reflexive (x : Set)133shows x = x134proof135simp136137theorem membership.irreflexive (x : Set)138shows¬x ∈ x139proof140induction x using membership141intros current ih hSelf142exact ih with hSelf, hSelf143