1-- Natural-valued characteristic functions of sets.2-- 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.Indicator8import Nat9import Function10import Core1112defunique indicator (A x : Set) (value ∈ ℕ) :=13 (x ∈ A → value =1) ∧ (¬(x ∈ A) → value =0)14proof15have alternatives : x ∈ A ∨¬(x ∈ A)16tauto17cases alternatives with member | absent18 . witness119refine⟨nat.omega.forward at1withassumption, _, _⟩20 . intro present21rfl22 . intro impossible23exfalso24exact impossible with member25 . witness026refine⟨nat.omega.forward at0withassumption, _, _⟩27 . intro impossible28exfalso29exact absent with impossible30 . intro missing31rfl3233uniqueness34intros u uMember first v vMember second35have alternatives : x ∈ A ∨¬(x ∈ A)36tauto37cases alternatives with present | absent38 . have uValue : u =139tauto40have vValue : v =141tauto42cc43 . have uValue : u =044tauto45have vValue : v =046tauto47cc4849theorem indicator.natural (A x : Set)50showsNat(indicator(A, x))51proof52obtain⟨natural, present, absent⟩:= indicator.selected at A, x53exact nat.omega.backward with natural5455theorem indicator.member (A x : Set)56requires member : x ∈ A57shows indicator(A, x) =158proof59have selected := indicator.selected at A, x60tauto6162theorem indicator.absent (A x : Set)63requires absent : ¬(x ∈ A)64shows indicator(A, x) =065proof66have selected := indicator.selected at A, x67tauto6869theorem indicator.congr (A B x y : Set)70requires equivalent : x ∈ A ↔ y ∈ B71shows indicator(A, x) = indicator(B, y)72proof73have alternatives : x ∈ A ∨¬(x ∈ A) bytauto74cases alternatives with present | absent75 . have other : y ∈ B76tauto77haveleft:= indicator.member with present78haveright:= indicator.member with other79rw [left, right]80rfl81 . have other : ¬(y ∈ B)82tauto83haveleft:= indicator.absent with absent84haveright:= indicator.absent with other85rw [left, right]86rfl8788theorem indicator.singleton_self (x : Set)89shows indicator({x}, x) =190proof91have member := singleton.self at x92exact indicator.member with member9394theorem indicator.singleton_other (x y : Set)95requires different : x ≠ y96shows indicator({x}, y) =097proof98have absent : ¬(y ∈ {x}) bysimp99simp [singleton.membership, indicator.absent]100101-- Count membership along a finite enumeration f[0], ..., f[n-1].102-- With a bijective enumeration this is the cardinality of the intersection.103def countMembers (A f : Set, n : Nat) →Set104| A, f, zero =>0105| A, f, succ(k) => add(countMembers(A, f, k), indicator(A, f[k]))106107theorem countMembers.natural (A f : Set, n : Nat)108showsNat(countMembers(A, f, n))109proof110induction n111| zero =>112rw countMembers113typecheck114| succ(k) using ih =>115rw countMembers116typecheck117