definition annotation

A set is, informally, a collection of things called elements. We write if is an element of . Repetition and order do not matter: .

Sources: 7 Sketches §1.2.1 (Example 1.9); Kittenlab Lecture 1 & 3; DaoFP Preface (“Set theory”); CTfS §2.1.1 (Notation 2.1.1.1, Exercise 2.1.1.2)

Important sets and notation

NotationMeaning
the empty set
, also a one-element set
the Booleans
the Natural Numbers
the -th ordinal ;
, integers, reals

Operations on sets

  • Subset: if every element of is in . Set-builder notation picks out the elements satisfying a property . See Subset.
  • Union , intersection of subsets of ; also and .
  • Product : the set of pairs . See Product.
  • Disjoint union : pairs with and with . See Coproduct.
  • Power set : the set of all subsets. See Power Set.

Notation: assigns meaning to ; merely asserts equality.

What must a set provide? CTfS: we can think of a set as “a collection of things , each of which is recognizable as being in and such that for each pair of named elements we can tell if or not. The set of pendulums is the collection of things we agree to call pendulums, each of which is recognizable as being a pendulum, and for any two people pointing at pendulums we can tell if they’re pointing at the same pendulum.” This is the standard an Olog type must meet. Quantifiers: “there exists”, “there exists a unique”, “for all” — e.g. ; has 8 subsets.

Three views of “set”

  1. 7 Sketches takes sets as primitive collections; functions are special Relations (Definition 1.22, see Function).
  2. Kittenlab takes Julia values as primitive. A Finite Set is a list of primitive things; a general (possibly infinite) set is a predicate Any -> Bool. If the predicate is expressible in Julia the set is computable. There is no set of all sets, but there is a set of all computable sets.
  3. DaoFP notes that category theory needs only “elementary” set theory, and uses sets as the model for types, while emphasizing that the arrows (functions) are what matter (see Category of Sets).

Docs: Sets · Limits & colimits — Kittenlab Lecture 1, Lecture 3

# Kittenlab Lecture 3: a (possibly infinite) set is a predicate on Julia values
abstract type ComputableSet end
Base.in(x, s::ComputableSet) = error("no specific definition found")
 
struct FiniteSet <: ComputableSet
  A::Vector{Any}
end
Base.in(x, χ::FiniteSet) = x ∈ χ.A
 
struct TypeSet <: ComputableSet      # any Julia type is a set
  T::Type
end
Base.in(x, χ::TypeSet) = x isa χ.T
 
struct IntersectionSet <: ComputableSet
  X::ComputableSet; Y::ComputableSet
end
Base.in(x, χ::IntersectionSet) = x ∈ χ.X && x ∈ χ.Y
 
struct UnionSet <: ComputableSet
  X::ComputableSet; Y::ComputableSet
end
Base.in(x, χ::UnionSet) = x ∈ χ.X || x ∈ χ.Y
 
# product and sum as predicates
product(X, Y) = z -> (z isa Tuple) && length(z) == 2 && z[1] ∈ X && z[2] ∈ Y
struct Left;  val::Any end
struct Right; val::Any end
sum(X, Y) = x -> x isa Left ? x.val ∈ X : x isa Right ? x.val ∈ Y : false
-- In Lean/Mathlib a "set of α" is a predicate α → Prop
#check (Set : Type u → Type u)          -- def Set (α : Type u) := α → Prop
example (α : Type) (s t : Set α) : Set α := s ∪ t
example (α : Type) (s t : Set α) : Set α := s ∩ t
example (α β : Type) : Type := α × β     -- product
example (α β : Type) : Type := α ⊕ β     -- disjoint union
#check (Set.powerset : Set α → Set (Set α))
import qualified Data.Set as S
 
-- Finite sets from containers
s1, s2 :: S.Set Int
s1 = S.fromList [1,2,3]
s2 = S.fromList [3,4]
 
u = S.union s1 s2               -- {1,2,3,4}
i = S.intersection s1 s2        -- {3}
p = S.cartesianProduct s1 s2    -- product
-- Disjoint union is Either; product is (,)
d :: [Either Int Int]
d = map Left (S.toList s1) ++ map Right (S.toList s2)