Martin Escardo & Tom de Jong, June 2023.

Iterative sets.

We define the type of iterative sets as a subtype of that of multisets.

  * H. R. Gylterud, "From multisets to sets in homotopy type theory".
    The Journal of Symbolic Logic, vol. 83, no. 3, pp. 1132โ€“1146,
    2018. https://doi.org/10.1017/jsl.2017.84

See the module Iterative.index for further bibliographic references.

The previous module Iterative.Multisets doesn't make significant use
of univalence, and so we assumed it only for specific
constructions. But here the use of univalence is more pervasive, and
so we assume it globally.

\begin{code}

{-# OPTIONS --safe --without-K --lossy-unification #-}

open import MLTT.Spartan
open import UF.Univalence

module Iterative.Sets
        (ua : Univalence)
        (๐“ค : Universe)
       where

open import UF.FunExt
open import UF.UA-FunExt

private
 ๐“คโบ : Universe
 ๐“คโบ = ๐“ค โบ

 fe : Fun-Ext
 fe = Univalence-gives-Fun-Ext ua

 fe' : FunExt
 fe' ๐“ค ๐“ฅ = fe {๐“ค} {๐“ฅ}

open import Iterative.Multisets ๐“ค
open import Ordinals.Notions
open import UF.Base
open import UF.DiscreteAndSeparated
open import UF.Embeddings
open import UF.Equiv
open import UF.EquivalenceExamples
open import UF.PairFun
open import UF.Retracts
open import UF.Sets
open import UF.Size
open import UF.Subsingletons
open import UF.Subsingletons-FunExt
open import W.Type

\end{code}

An iterative set is a multiset whose subforests are all
embeddings. The effect of that is that the membership relation on
iterative sets is proposition-valued, rather than just type-valued, as
is the case for general multisets.

\begin{code}

is-iterative-set : ๐•„ โ†’ ๐“คโบ ฬ‡
is-iterative-set (ssup X ฯ†) = is-embedding ฯ†
                            ร— ((x : X) โ†’ is-iterative-set (ฯ† x))
\end{code}

It is convenient to name the projections for the sake of clarity:

\begin{code}

๐•„-forest-is-embedding : (M : ๐•„)
                      โ†’ is-iterative-set M
                      โ†’ is-embedding (๐•„-forest M)
๐•„-forest-is-embedding (ssup _ _) = prโ‚

๐•„-subtrees-are-iterative : (M : ๐•„)
                         โ†’ is-iterative-set M
                         โ†’ (x : ๐•„-root M) โ†’ is-iterative-set (๐•„-forest M x)
๐•„-subtrees-are-iterative (ssup _ _) = prโ‚‚

\end{code}

It is crucial that the notion of iterative set is property rather than
data:

\begin{code}

being-iset-is-prop : (M : ๐•„) โ†’ is-prop (is-iterative-set M)
being-iset-is-prop (ssup X ฯ†) =
 ร—-is-prop
  (being-embedding-is-prop fe ฯ†)
  (ฮ -is-prop fe (ฮป x โ†’ being-iset-is-prop (ฯ† x)))

\end{code}

The type of iterative sets as a subtype of that of iterative
multisets:

\begin{code}

๐• : ๐“คโบ ฬ‡
๐• = ฮฃ M ๊ž‰ ๐•„ , is-iterative-set M

๐•-is-locally-small : is-locally-small ๐•
๐•-is-locally-small = subtype-is-locally-small
                      being-iset-is-prop
                      (๐•„-is-locally-small ua)
\end{code}

We again name the projections for the sake of clarity:

\begin{code}

underlying-mset : ๐• โ†’ ๐•„
underlying-mset = prโ‚

isets-are-iterative : (A : ๐•) โ†’ is-iterative-set (underlying-mset A)
isets-are-iterative = prโ‚‚

\end{code}

Because the notion of iterative set is property, we get that ๐• is
indeed a subtype of ๐•„.

\begin{code}

underlying-mset-is-embedding : is-embedding underlying-mset
underlying-mset-is-embedding = prโ‚-is-embedding being-iset-is-prop

\end{code}

We define the root and the forest of an iterative set in terms of
those for multisets, but we need to add a "proof obligation" in the
case of the forest.

\begin{code}

๐•-root : ๐• โ†’ ๐“ค ฬ‡
๐•-root A = ๐•„-root (underlying-mset A)

๐•-forest : (A : ๐•) โ†’ ๐•-root A โ†’ ๐•
๐•-forest A x = ๐•„-forest (underlying-mset A) x ,
               ๐•„-subtrees-are-iterative
                (underlying-mset A)
                (isets-are-iterative A)
                x

\end{code}

A criterion for equality in ๐•:

\begin{code}

to-๐•-๏ผ : {X Y : ๐“ค ฬ‡ }
          {ฯ† : X โ†’ ๐•„}
          {ฮณ : Y โ†’ ๐•„}
        โ†’ (ฮฃ p ๊ž‰ X ๏ผ Y , ฯ† ๏ผ ฮณ โˆ˜ Idtofun p)
        โ†’ (i : is-iterative-set (ssup X ฯ†))
          (j : is-iterative-set (ssup Y ฮณ))
        โ†’ (ssup X ฯ† , i) ๏ผ[ ๐• ] (ssup Y ฮณ , j)
to-๐•-๏ผ ฯƒ i j = to-subtype-๏ผ being-iset-is-prop (to-๐•„-๏ผ ฯƒ)

\end{code}

We define membership of iterative sets in terms of that for multisets:

\begin{code}

_โˆˆ_ : ๐• โ†’ ๐• โ†’ ๐“คโบ ฬ‡
A โˆˆ B = underlying-mset A โ… underlying-mset B

\end{code}

As is the case for iterative multisets, there is a resized down,
equivalent definition of membership.

\begin{code}

_โˆˆโป_ : ๐• โ†’ ๐• โ†’ ๐“ค ฬ‡
A โˆˆโป B = underlying-mset A โ…โป underlying-mset B

โˆˆโปโ‰ƒโˆˆ : (A B : ๐•) โ†’ (A โˆˆ B) โ‰ƒ (A โˆˆโป B)
โˆˆโปโ‰ƒโˆˆ A B = โ…โปโ‰ƒโ… ua (underlying-mset A) (underlying-mset B)

\end{code}

As discussed above, the membership relation becomes a proposition
precisely because we required forests to be embeddings to define the
subtype of iterative sets.

\begin{code}

โˆˆ-is-prop-valued : (A B : ๐•) โ†’ is-prop (A โˆˆ B)
โˆˆ-is-prop-valued (M , _) (ssup X ฯ† , ฯ†-emb , _) = ฯ†-emb M

๐•-forest-โˆˆ : (A : ๐•) (x : ๐•-root A) โ†’ ๐•-forest A x โˆˆ A
๐•-forest-โˆˆ A x = ๐•„-forest-โ… (underlying-mset A) x

\end{code}

The subset relation is defined in the usual way and is
proposition-valued:

\begin{code}

_โŠ†_ : ๐• โ†’ ๐• โ†’ ๐“คโบ ฬ‡
A โŠ† B = (C : ๐•) โ†’ C โˆˆ A โ†’ C โˆˆ B

โŠ†-is-prop-valued : (A B : ๐•) โ†’ is-prop (A โŠ† B)
โŠ†-is-prop-valued A B = ฮ โ‚‚-is-prop fe (ฮป C _ โ†’ โˆˆ-is-prop-valued C B)

\end{code}

It is in the following that the univalence axiom is used for the first
time, to establish the extensionality axiom for iterative sets:

\begin{code}

โˆˆ-is-extensional : (A B : ๐•) โ†’ A โŠ† B โ†’ B โŠ† A โ†’ A ๏ผ B
โˆˆ-is-extensional A@(M@(ssup X ฯ†) , ฯ†-emb , g)
                 B@(N@(ssup Y ฮณ) , ฮณ-emb , h) u v = V
 where
  have-uv : (A โŠ† B) ร— (B โŠ† A)
  have-uv = u , v

  I : (x : X) โ†’ ฮฃ y ๊ž‰ Y , ฮณ y ๏ผ ฯ† x
  I x = u (ฯ† x , g x) (๐•„-forest-โ… M x)

  II : (y : Y) โ†’ ฮฃ x ๊ž‰ X , ฯ† x ๏ผ ฮณ y
  II y = v (ฮณ y , h y) (๐•„-forest-โ… N y)

  f : X โ†’ Y
  f x = prโ‚ (I x)

  fโปยน : Y โ†’ X
  fโปยน y = prโ‚ (II y)

  ฮท : fโปยน โˆ˜ f โˆผ id
  ฮท x = embeddings-are-lc ฯ† ฯ†-emb
         (ฯ† (fโปยน (f x)) ๏ผโŸจ prโ‚‚ (II (f x)) โŸฉ
          ฮณ (f x)       ๏ผโŸจ prโ‚‚ (I x) โŸฉ
          ฯ† x           โˆŽ)

  ฮต : f โˆ˜ fโปยน โˆผ id
  ฮต y = embeddings-are-lc ฮณ ฮณ-emb
         (ฮณ (f (fโปยน y)) ๏ผโŸจ prโ‚‚ (I (fโปยน y)) โŸฉ
          ฯ† (fโปยน y)     ๏ผโŸจ prโ‚‚ (II y) โŸฉ
          ฮณ y           โˆŽ)

  ๐•— : X โ‰ƒ Y
  ๐•— = qinveq f (fโปยน , ฮท , ฮต)

  p : X ๏ผ Y
  p = eqtoid (ua ๐“ค) X Y ๐•—

  III : Idtofun p ๏ผ f
  III = Idtofun-eqtoid (ua ๐“ค) ๐•—

  IV = ฮป x โ†’
   ฯ† x             ๏ผโŸจ (prโ‚‚ (I x))โปยน โŸฉ
   ฮณ (f x)         ๏ผโŸจ ap (ฮป - โ†’ ฮณ (- x)) (III โปยน) โŸฉ
   ฮณ (Idtofun p x) โˆŽ

  V : A ๏ผ B
  V = to-๐•-๏ผ (p , dfunext fe IV) (ฯ†-emb , g) (ฮณ-emb , h)

\end{code}

It follows that ๐• is 0-type, or set, in the sense of the HoTT
book. But notice that we now have two notions of set in this
discussion: the "material" (iterative set) one and the "structural"
one (0-type or set). The reader should keep this distinction in mind
for the comments and code below.

The following uses the fact that any type with an extensional order is
automatically a set.

\begin{code}

๐•-is-set : is-set ๐•
๐•-is-set = extensionally-ordered-types-are-sets _โˆˆ_ fe'
            โˆˆ-is-prop-valued
            โˆˆ-is-extensional

\end{code}

Here is a second, more direct, proof of this fact.

The following says that ssup ฯ† ๏ผ M is a proposition for every M : ๐•„
if ฯ† is an embedding.

The following doesn't seem to have been observed before in the
literature.

\begin{code}

๐•„-ssup-is-h-isolated : (X : ๐“ค ฬ‡ ) (ฯ† : X โ†’ ๐•„)
                     โ†’ is-embedding ฯ†
                     โ†’ is-h-isolated (ssup X ฯ†)
๐•„-ssup-is-h-isolated X ฯ† ฯ†-emb {M} = III
 where
  I = (ssup X ฯ† ๏ผ M)                        โ‰ƒโŸจ ๏ผ-flip โŸฉ
      (M ๏ผ ssup X ฯ†)                        โ‰ƒโŸจ ๐•„-๏ผ' M (ssup X ฯ†) โŸฉ
      fiber ((ฯ† โˆ˜_) โˆ˜ Idtofun) (๐•„-forest M)  โ– 

  II : is-embedding ((ฯ† โˆ˜_) โˆ˜ Idtofun)
  II = โˆ˜-is-embedding
        (Idtofun-is-embedding (ua ๐“ค) fe)
        (postcomp-is-embedding fe' ฯ† ฯ†-emb)

  III : is-prop (ssup X ฯ† ๏ผ M)
  III = equiv-to-prop I (II (๐•„-forest M))

\end{code}

And a particular case of this is that if M is an iterative set then
M ๏ผ N is a proposition for every *multiset* N.

\begin{code}

isets-are-h-isolated : (M : ๐•„)
                     โ†’ is-iterative-set M
                     โ†’ is-h-isolated M
isets-are-h-isolated (ssup X ฯ†) (ฯ†-emb , _) = ๐•„-ssup-is-h-isolated X ฯ† ฯ†-emb

\end{code}

Because a subtype of any type whatsoever consisting of h-isolated
elements is a 0-type, we get a second proof that the type of iterative
sets is a 0-type.

\begin{code}

๐•-is-set' : is-set ๐•
๐•-is-set' {M , M-is-is-set} =
 equiv-to-prop
  (โ‰ƒ-sym (to-subtype-๏ผ-โ‰ƒ being-iset-is-prop))
  (isets-are-h-isolated M M-is-is-set)

\end{code}

By definition, an iterative multiset is an iterative set if its
๐•„-forests are all embeddings. The ๐•-forests are also embeddings:

\begin{code}

๐•-forest-is-embedding : (A : ๐•) โ†’ is-embedding (๐•-forest A)
๐•-forest-is-embedding A@(ssup X ฯ† , ฯ†-emb , ฯ†-iter) =
 pair-fun-is-embedding-special ฯ† ฯ†-iter ฯ†-emb being-iset-is-prop

\end{code}

We construct elements of ๐•„ using the constructor ssup. We now
introduce a corresponding constructor ๐•-ssup to construct elements of
the type ๐•.

\begin{code}

๐•-ssup : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) โ†’ is-embedding ฯ• โ†’ ๐•
๐•-ssup X ฯ• ฯ•-emb = ssup X ฯ† , ฯ†-emb , ฯ†-iset
 where
  ฯ† : X โ†’ ๐•„
  ฯ† = underlying-mset โˆ˜ ฯ•

  ฯ†-iset : (x : X) โ†’ is-iterative-set (ฯ† x)
  ฯ†-iset = isets-are-iterative โˆ˜ ฯ•

  ฯ†-emb : is-embedding ฯ†
  ฯ†-emb = โˆ˜-is-embedding ฯ•-emb underlying-mset-is-embedding

\end{code}

It behaves as expected with respect to the corresponding destructors:

\begin{code}

๐•-ssup-root : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
            โ†’ ๐•-root (๐•-ssup X ฯ• e) ๏ผ X
๐•-ssup-root X ฯ• e = refl

๐•-ssup-forest : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
              โ†’ ๐•-forest (๐•-ssup X ฯ• e) ๏ผ ฯ•
๐•-ssup-forest X ฯ• e = refl

\end{code}

Notice that the identifications are definitional.

We have the following ฮท rules for ๐•, where the first is more general
and the second is more natural. In both cases the identifications are
not definitional.

\begin{code}

๐•-ฮท' : (A : ๐•) (e : is-embedding (๐•-forest A))
     โ†’ ๐•-ssup (๐•-root A) (๐•-forest A) e ๏ผ A
๐•-ฮท' (ssup _ _ , _) _ = to-subtype-๏ผ being-iset-is-prop refl

๐•-ฮท : (A : ๐•) โ†’ ๐•-ssup (๐•-root A) (๐•-forest A) (๐•-forest-is-embedding A) ๏ผ A
๐•-ฮท A = ๐•-ฮท' A (๐•-forest-is-embedding A)

\end{code}

Here are two characterizations of the membership relation:

\begin{code}

โˆˆ-behaviour : (A : ๐•) (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
            โ†’ A โˆˆ ๐•-ssup X ฯ• e โ‰ƒ (ฮฃ x ๊ž‰ X , ฯ• x ๏ผ A)
โˆˆ-behaviour A X ฯ• e =
 (A โˆˆ ๐•-ssup X ฯ• e)                                     โ‰ƒโŸจ โ‰ƒ-refl _ โŸฉ
 (ฮฃ x ๊ž‰ X , underlying-mset (ฯ• x) ๏ผ underlying-mset A) โ‰ƒโŸจ ฮฃ-cong I โŸฉ
 (ฮฃ x ๊ž‰ X , ฯ• x ๏ผ A)                                   โ– 
  where
   I : (x : X) โ†’ (underlying-mset (ฯ• x) ๏ผ underlying-mset A) โ‰ƒ (ฯ• x ๏ผ A)
   I x = embedding-criterion-converse
          underlying-mset
          underlying-mset-is-embedding
          (ฯ• x)
          A

โˆˆ-behaviour' : (A B : ๐•) โ†’ A โˆˆ B โ‰ƒ (ฮฃ x ๊ž‰ ๐•-root B , ๐•-forest B x ๏ผ A)
โˆˆ-behaviour' A B =
 transport
  (ฮป - โ†’ A โˆˆ - โ‰ƒ (ฮฃ x ๊ž‰ ๐•-root - , ๐•-forest - x ๏ผ A))
  (๐•-ฮท B)
  (โˆˆ-behaviour A (๐•-root B) (๐•-forest B) (๐•-forest-is-embedding B))

private
 โˆˆ-remark : (A B : ๐•) โ†’ A โˆˆ B โ‰ƒ fiber (๐•-forest B) A
 โˆˆ-remark = โˆˆ-behaviour'

\end{code}

It also follows from the facts that ๐• is a set and that ๐•-forest is an
embedding that the root of any iterative set is a 0-type:

\begin{code}

๐•-root-is-set : (A : ๐•) โ†’ is-set (๐•-root A)
๐•-root-is-set A = subtypes-of-sets-are-sets
                   (๐•-forest A)
                   (๐•-forest-is-embedding A)
                   ๐•-is-set
\end{code}

It would be nice if we could define ๐• inductively as follows:

 data ๐• : ๐“คโบ ฬ‡ where
  ๐•-ssup : (X : ๐“ค ฬ‡ ) (ฯ† : X โ†’ ๐•) โ†’ is-embedding ฯ† โ†’ ๐•

However, this is not a strictly positive definition, for the criterion
of strict positivity adopted by Agda, and so it is not accepted.

Nevertheless, all iterative sets *are* generated by the "constructor"
๐•-ssup, in the following sense, so that we can view ๐• as really
inductively defined by the above data declaration.

The following result, implementing the above idea, seems to be new.

\begin{code}

๐•-Induction'
 : (P : ๐• โ†’ ๐“ฅ ฬ‡ )
   (f : (A : ๐•) โ†’ ((x : ๐•-root A) โ†’ P (๐•-forest A x)) โ†’ P A)
 โ†’ ฮฃ h ๊ž‰ ((A : ๐•) โ†’ P A)
       , ((A : ๐•) โ†’ h A ๏ผ f A (ฮป x โ†’ h (๐•-forest A x)))
๐•-Induction' P f = (ฮป (M , i) โ†’ H M i) , p
 where
  H : (M : ๐•„) (i : is-iterative-set M) โ†’ P (M , i)
  H M@(ssup X ฯ†) i@(_ , ฯ†-iter) = f (M , i) (ฮป x โ†’ H (ฯ† x) (ฯ†-iter x))

  p : (A : ๐•) โ†’ _ ๏ผ _
  p (M@(ssup X ฯ†) , i@(_ , ฯ†-iter)) = refl

๐•-Induction
 : (P : ๐• โ†’ ๐“ฅ ฬ‡ )
 โ†’ (f : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
      โ†’ ((x : X) โ†’ P (ฯ• x))
      โ†’ P (๐•-ssup X ฯ• e))
 โ†’ ฮฃ h ๊ž‰ ((A : ๐•) โ†’ P A)
       , ((X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
       โ†’ h (๐•-ssup X ฯ• e) ๏ผ f X ฯ• e (ฮป x โ†’ h (ฯ• x)))
๐•-Induction {๐“ฅ} P f = h , IV
 where
  f' : (A : ๐•) โ†’ ((x : ๐•-root A) โ†’ P (๐•-forest A x)) โ†’ P A
  f' A@(M@(ssup X ฯ†) , i@(ฯ†-emb , ฯ†-iter)) g = II
   where
    I : P (๐•-ssup X (๐•-forest A) (๐•-forest-is-embedding A))
    I = f X (๐•-forest A) (๐•-forest-is-embedding A) g

    II : P A
    II = transport P (๐•-ฮท A) I

  h : (A : ๐•) โ†’ P A
  h = prโ‚ (๐•-Induction' P f')

  III : (A : ๐•) โ†’ h A ๏ผ f' A (ฮป x โ†’ h (๐•-forest A x))
  III = prโ‚‚ (๐•-Induction' P f')

  IV : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
     โ†’ h (๐•-ssup X ฯ• e) ๏ผ f X ฯ• e (ฮป x โ†’ h (ฯ• x))
  IV X ฯ• e =
   h A                                                               ๏ผโŸจ III A โŸฉ
   f' A (ฮป x โ†’ h (ฯ• x))                                              ๏ผโŸจ refl โŸฉ
   t P                (๐•-ฮท A)             (f X ฯ• e' (ฮป x โ†’ h (ฯ• x))) ๏ผโŸจ i โŸฉ
   t P                (ap (๐•-ssup X ฯ•) p) (f X ฯ• e' (ฮป x โ†’ h (ฯ• x))) ๏ผโŸจ ii โŸฉ
   t (P โˆ˜ ๐•-ssup X ฯ•) p                   (f X ฯ• e' (ฮป x โ†’ h (ฯ• x))) ๏ผโŸจ iii โŸฉ
   f X ฯ• e (ฮป x โ†’ h (ฯ• x))                                           โˆŽ
    where
     t = transport
     A  = ๐•-ssup X ฯ• e
     e' = ๐•-forest-is-embedding A

     p : e' ๏ผ e
     p = being-embedding-is-prop fe ฯ• e' e

     q : ๐•-ฮท A ๏ผ ap (๐•-ssup X ฯ•) p
     q = ๐•-is-set _ _

     i   = ap (ฮป - โ†’ t P - (f X ฯ• e' (ฮป x โ†’ h (ฯ• x)))) q
     ii  = (transport-ap P (๐•-ssup X ฯ•) p)โปยน
     iii = apd (ฮป - โ†’ f X ฯ• - (ฮป x โ†’ h (ฯ• x))) p

๐•-induction : (P : ๐• โ†’ ๐“ฅ ฬ‡ )
            โ†’ ((X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
                  โ†’ ((x : X) โ†’ P (ฯ• x))
                  โ†’ P (๐•-ssup X ฯ• e))
            โ†’ (A : ๐•) โ†’ P A
๐•-induction P f = prโ‚ (๐•-Induction P f)

๐•-induction-behaviour
 : (P : ๐• โ†’ ๐“ฅ ฬ‡ )
 โ†’ (f : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
      โ†’ ((x : X) โ†’ P (ฯ• x))
      โ†’ P (๐•-ssup X ฯ• e))
 โ†’ (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
 โ†’ ๐•-induction P f (๐•-ssup X ฯ• e) ๏ผ f X ฯ• e (ฮป x โ†’ ๐•-induction P f (ฯ• x))
๐•-induction-behaviour P f = prโ‚‚ (๐•-Induction P f)

๐•-recursion : (P : ๐“ฅ ฬ‡ )
            โ†’ ((X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•)
                  โ†’ is-embedding ฯ•
                  โ†’ (X โ†’ P)
                  โ†’ P)
            โ†’ ๐• โ†’ P
๐•-recursion P = ๐•-induction (ฮป _ โ†’ P)

๐•-recursion-behaviour
 : (P : ๐“ฅ ฬ‡ )
 โ†’ (f : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
      โ†’ (X โ†’ P)
      โ†’ P)
 โ†’ (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
 โ†’ ๐•-recursion P f (๐•-ssup X ฯ• e) ๏ผ f X ฯ• e (ฮป x โ†’ ๐•-recursion P f (ฯ• x))
๐•-recursion-behaviour P = ๐•-induction-behaviour (ฮป _ โ†’ P)

๐•-iteration : (P : ๐“ฅ ฬ‡ )
            โ†’ ((X : ๐“ค ฬ‡ ) โ†’ (X โ†’ P) โ†’ P)
            โ†’ ๐• โ†’ P
๐•-iteration P f = ๐•-recursion P (ฮป X ฯ• e โ†’ f X)

๐•-iteration-behaviour
 : (P : ๐“ฅ ฬ‡ )
 โ†’ (f : (X : ๐“ค ฬ‡ ) โ†’ (X โ†’ P) โ†’ P)
 โ†’ (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
 โ†’ ๐•-iteration P f (๐•-ssup X ฯ• e) ๏ผ f X (ฮป x โ†’ ๐•-iteration P f (ฯ• x))
๐•-iteration-behaviour P f = ๐•-recursion-behaviour P (ฮป X ฯ• e โ†’ f X)

\end{code}

So we are essentially working with (an encoding) of the above
non-strictly positive data type.

The usual induction principle for iterative sets follows directly from
the above form of induction. This consequence is already in Gylterud [4].

\begin{code}

โˆˆ-induction : (P : ๐• โ†’ ๐“ฅ ฬ‡ )
            โ†’ ((A : ๐•) โ†’ ((B : ๐•) โ†’ B โˆˆ A โ†’ P B) โ†’ P A)
            โ†’ (A : ๐•) โ†’ P A
โˆˆ-induction P IH = ๐•-induction P f
 where
  f : (X : ๐“ค ฬ‡ ) (ฯ• : X โ†’ ๐•) (e : is-embedding ฯ•)
    โ†’ ((x : X) โ†’ P (ฯ• x))
    โ†’ P (๐•-ssup X ฯ• e)
  f X ฯ• e IH' = IH A s
   where
    A : ๐•
    A = ๐•-ssup X ฯ• e

    s : (B : ๐•) โ†’ B โˆˆ A โ†’ P B
    s B@(.(underlying-mset (ฯ• x)) , j) (x , refl) = II
     where
      I : P (ฯ• x)
      I = IH' x

      II : P (underlying-mset (ฯ• x) , j)
      II = transport P (to-subtype-๏ผ being-iset-is-prop refl) I

\end{code}

And then it follows immediately that the membership relation is
accessible:

\begin{code}

โˆˆ-is-accessible : (A : ๐•) โ†’ is-accessible _โˆˆ_ A
โˆˆ-is-accessible = โˆˆ-induction (is-accessible _โˆˆ_) (ฮป _ โ†’ acc)

\end{code}

Singleton sets can be constructed as follows.

\begin{code}

โด_โต : (A : ๐•) โ†’ ๐•
โด A โต = ๐•-ssup ๐Ÿ™ (ฮป _ โ†’ A) (global-point-is-embedding (ฮป _ โ†’ A) ๐•-is-set)

โดโต-behaviour : (A : ๐•) (B : ๐•) โ†’ B โˆˆ โด A โต โ‰ƒ (B ๏ผ A)
โดโต-behaviour A B = B โˆˆ โด A โต    โ‰ƒโŸจ โˆˆ-behaviour' B โด A โต โŸฉ
                   ๐Ÿ™ ร— (A ๏ผ B) โ‰ƒโŸจ ๐Ÿ™-lneutral โŸฉ
                   (A ๏ผ B)     โ‰ƒโŸจ ๏ผ-flip โŸฉ
                   (B ๏ผ A)     โ– 

\end{code}

Given a family of iterative sets indexed by a small type, we construct
its union as in [4].

We make use of propositional truncations (to define the image of a
map) and of set replacement (which follows from having set quotients).

\begin{code}

open import UF.PropTrunc

module _ (pt : propositional-truncations-exist) where

 open PropositionalTruncation pt
 open import UF.ImageAndSurjection pt

 module unions-of-iterative-sets (sr : Set-Replacement pt) where

  private
   module union-construction
          {I : ๐“ค ฬ‡ }
          (๐“ : I โ†’ ๐•)
         where

    im : ๐“คโบ ฬ‡
    im = image ๐“

    im-is-small : image ๐“ is ๐“ค small
    im-is-small = sr ๐“ (I , โ‰ƒ-refl I) ๐•-is-locally-small ๐•-is-set

    imโป : ๐“ค ฬ‡
    imโป = resized im im-is-small

    imโป-โ‰ƒ-im : imโป โ‰ƒ im
    imโป-โ‰ƒ-im = resizing-condition im-is-small

    ฯ€ : im โ†’ ๐•
    ฯ€ = restriction ๐“

    ฯ€โป : imโป โ†’ ๐•
    ฯ€โป = ฯ€ โˆ˜ โŒœ imโป-โ‰ƒ-im โŒ

    ฯ€-is-embedding : is-embedding ฯ€
    ฯ€-is-embedding = restrictions-are-embeddings ๐“

    ฯ€โป-is-embedding : is-embedding ฯ€โป
    ฯ€โป-is-embedding = โˆ˜-is-embedding
                       (equivs-are-embeddings
                         โŒœ imโป-โ‰ƒ-im โŒ
                         (โŒœโŒ-is-equiv imโป-โ‰ƒ-im))
                       ฯ€-is-embedding

  โ‹ƒ : {I : ๐“ค ฬ‡ } (๐“ : I โ†’ ๐•) โ†’ ๐•
  โ‹ƒ {I} ๐“ = ๐•-ssup imโป ฯ€โป ฯ€โป-is-embedding
   where
    open union-construction ๐“

  โ‹ƒ-behaviour : {I : ๐“ค ฬ‡ } (๐“ : I โ†’ ๐•) (B : ๐•)
              โ†’ B โˆˆ โ‹ƒ ๐“ โ‰ƒ (โˆƒ i ๊ž‰ I , B ๏ผ ๐“ i)
  โ‹ƒ-behaviour {I} ๐“ B =
   B โˆˆ โ‹ƒ ๐“                                    โ‰ƒโŸจ โˆˆ-behaviour' B (โ‹ƒ ๐“) โŸฉ
   (ฮฃ j ๊ž‰ imโป , ฯ€โป j ๏ผ B)                    โ‰ƒโŸจ eโ‚ โŸฉ
   (ฮฃ j ๊ž‰ im , ฯ€ j ๏ผ B)                      โ‰ƒโŸจ ฮฃ-assoc โŸฉ
   (ฮฃ C ๊ž‰ ๐• , C โˆˆimage ๐“ ร— (C ๏ผ B))          โ‰ƒโŸจ ฮฃ-cong (ฮป C โ†’ ร—-comm) โŸฉ
   (ฮฃ C ๊ž‰ ๐• , (C ๏ผ B) ร— (C โˆˆimage ๐“))        โ‰ƒโŸจ โ‰ƒ-sym ฮฃ-assoc โŸฉ
   (ฮฃ s ๊ž‰ singleton-type' B , prโ‚ s โˆˆimage ๐“) โ‰ƒโŸจ โ‰ƒ-sym eโ‚‚ โŸฉ
   ๐Ÿ™ {๐“ค} ร— B โˆˆimage ๐“                         โ‰ƒโŸจ ๐Ÿ™-lneutral โŸฉ
   (โˆƒ i ๊ž‰ I , ๐“ i ๏ผ B)                       โ‰ƒโŸจ โˆƒ-cong pt (ฮป i โ†’ ๏ผ-flip) โŸฉ
   (โˆƒ i ๊ž‰ I , B ๏ผ ๐“ i)                       โ– 
    where
     open union-construction ๐“
     eโ‚ = ฮฃ-change-of-variable-โ‰ƒ _ imโป-โ‰ƒ-im
     eโ‚‚ = ฮฃ-change-of-variable-โ‰ƒ _
           (singleton-โ‰ƒ-๐Ÿ™' (singleton-types'-are-singletons B))

\end{code}

Any iterative set is the union of its ๐•-forest.

\begin{code}

  โ‹ƒ-ฮท : (A : ๐•) โ†’ โ‹ƒ (๐•-forest A) ๏ผ A
  โ‹ƒ-ฮท A = โˆˆ-is-extensional (โ‹ƒ (๐•-forest A)) A i j
   where
    i : โ‹ƒ (๐•-forest A) โŠ† A
    i B m = โˆฅโˆฅ-rec (โˆˆ-is-prop-valued B A) f (โŒœ โ‹ƒ-behaviour (๐•-forest A) B โŒ m)
     where
      f : (ฮฃ a ๊ž‰ ๐•-root A , B ๏ผ ๐•-forest A a) โ†’ B โˆˆ A
      f (a , refl) = ๐•-forest-โˆˆ A a
    j : A โŠ† โ‹ƒ (๐•-forest A)
    j B m = โŒœ โ‹ƒ-behaviour (๐•-forest A) B โŒโปยน โˆฃ a , e โˆฃ
     where
      abstract
       m' : ฮฃ a ๊ž‰ ๐•-root A , ๐•-forest A a ๏ผ B
       m' = โŒœ โˆˆ-behaviour' B A โŒ m
       a : ๐•-root A
       a = prโ‚ m'
       e : B ๏ผ ๐•-forest A a
       e = (prโ‚‚ m') โปยน

\end{code}

Unions allow us to construct a retraction of the inclusion ๐• โ†ช ๐•„, and
this seems to be a new result.

\begin{code}

  ๐•„-to-๐• : ๐•„ โ†’ ๐•
  ๐•„-to-๐• (ssup X ฯ†) = โ‹ƒ (๐•„-to-๐• โˆ˜ ฯ†)

  ๐•„-to-๐•-is-retraction-of-inclusion : ๐•„-to-๐• โˆ˜ underlying-mset ๏ผ id
  ๐•„-to-๐•-is-retraction-of-inclusion = dfunext fe (โˆˆ-induction _ f)
   where
    f : (A : ๐•) โ†’ ((B : ๐•) โ†’ B โˆˆ A โ†’ (๐•„-to-๐• โˆ˜ underlying-mset) B ๏ผ B)
      โ†’ (๐•„-to-๐• โˆ˜ underlying-mset) A ๏ผ A
    f A IH = ๐•„-to-๐• Aโ‚˜                                 ๏ผโŸจ I โŸฉ
             ๐•„-to-๐• (ssup (๐•-root A) (๐•„-forest Aโ‚˜))    ๏ผโŸจ refl โŸฉ
             โ‹ƒ (๐•„-to-๐• โˆ˜ ๐•„-forest Aโ‚˜)                  ๏ผโŸจ refl โŸฉ
             โ‹ƒ (๐•„-to-๐• โˆ˜ underlying-mset โˆ˜ ๐•-forest A) ๏ผโŸจ II โŸฉ
             โ‹ƒ (๐•-forest A)                            ๏ผโŸจ โ‹ƒ-ฮท A โŸฉ
             A                                         โˆŽ
              where
               Aโ‚˜ : ๐•„
               Aโ‚˜ = underlying-mset A
               I  = ap (๐•„-to-๐• โˆ˜ underlying-mset) (๐•-ฮท A โปยน)
               II = ap โ‹ƒ (dfunext fe (ฮป a โ†’ IH (๐•-forest A a) (๐•-forest-โˆˆ A a)))

  ๐•-is-retract-of-๐•„ : retract ๐• of ๐•„
  ๐•-is-retract-of-๐•„ = ๐•„-to-๐• ,
                      underlying-mset ,
                      happly ๐•„-to-๐•-is-retraction-of-inclusion

\end{code}