view src/zorn.agda @ 1027:91988ae176ab

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 26 Nov 2022 16:34:38 +0900
parents 8b3d7c461a84
children d1eecfc6cdfa
line wrap: on
line source

{-# OPTIONS --allow-unsolved-metas #-}
open import Level hiding ( suc ; zero )
open import Ordinals
open import Relation.Binary
open import Relation.Binary.Core
open import Relation.Binary.PropositionalEquality
import OD
module zorn {n : Level } (O : Ordinals {n}) (_<_ : (x y : OD.HOD O ) → Set n ) (PO : IsStrictPartialOrder _≡_ _<_ ) where

--
-- Zorn-lemma : { A : HOD }
--     → o∅ o< & A
--     → ( ( B : HOD) → (B⊆A : B ⊆ A) → IsTotalOrderSet B → SUP A B   ) -- SUP condition
--     → Maximal A
--

open import zf
open import logic
-- open import partfunc {n} O

open import Relation.Nullary
open import Data.Empty
import BAlgbra

open import Data.Nat hiding ( _<_ ; _≤_ )
open import Data.Nat.Properties
open import nat


open inOrdinal O
open OD O
open OD.OD
open ODAxiom odAxiom
import OrdUtil
import ODUtil
open Ordinals.Ordinals  O
open Ordinals.IsOrdinals isOrdinal
open Ordinals.IsNext isNext
open OrdUtil O
open ODUtil O


import ODC

open _∧_
open _∨_
open Bool

open HOD

--
-- Partial Order on HOD ( possibly limited in A )
--

_<<_ : (x y : Ordinal ) → Set n
x << y = * x < * y

_<=_ : (x y : Ordinal ) → Set n    -- we can't use * x ≡ * y, it is Set (Level.suc n). Level (suc n) troubles Chain
x <= y = (x ≡ y ) ∨ ( * x < * y )

POO : IsStrictPartialOrder _≡_ _<<_
POO = record { isEquivalence = record { refl = refl ; sym = sym ; trans = trans }
   ; trans = IsStrictPartialOrder.trans PO
   ; irrefl = λ x=y x<y → IsStrictPartialOrder.irrefl PO (cong (*) x=y) x<y
   ; <-resp-≈ =  record { fst = λ {x} {y} {y1} y=y1 xy1 → subst (λ k → x << k ) y=y1 xy1 ; snd = λ {x} {x1} {y} x=x1 x1y → subst (λ k → k << x ) x=x1 x1y } }

_≤_ : (x y : HOD) → Set (Level.suc n)
x ≤ y = ( x ≡ y ) ∨ ( x < y )

≤-ftrans : {x y z : HOD} → x ≤ y → y ≤ z → x ≤ z
≤-ftrans {x} {y} {z} (case1 refl ) (case1 refl ) = case1 refl
≤-ftrans {x} {y} {z} (case1 refl ) (case2 y<z) = case2 y<z
≤-ftrans {x} {_} {z} (case2 x<y ) (case1 refl ) = case2 x<y
≤-ftrans {x} {y} {z} (case2 x<y) (case2 y<z) = case2 ( IsStrictPartialOrder.trans PO x<y y<z )

<=-trans : {x y z : Ordinal } →  x <=  y →  y <=  z →  x <=  z
<=-trans {x} {y} {z} (case1 refl ) (case1 refl ) = case1 refl
<=-trans {x} {y} {z} (case1 refl ) (case2 y<z) = case2 y<z
<=-trans {x} {_} {z} (case2 x<y ) (case1 refl ) = case2 x<y
<=-trans {x} {y} {z} (case2 x<y) (case2 y<z) = case2 ( IsStrictPartialOrder.trans PO x<y y<z )

ftrans<=-< : {x y z : Ordinal } →  x <=  y →  y << z →  x <<  z
ftrans<=-< {x} {y} {z} (case1 eq) y<z = subst (λ k → k < * z) (sym (cong (*) eq))  y<z
ftrans<=-< {x} {y} {z} (case2 lt) y<z = IsStrictPartialOrder.trans PO lt y<z

ftrans<-<= : {x y z : Ordinal } →  x <<  y →  y <= z →  x <<  z
ftrans<-<= {x} {y} {z} x<y (case1 eq) = subst (λ k → * x < k ) ((cong (*) eq)) x<y
ftrans<-<= {x} {y} {z} x<y (case2 lt) = IsStrictPartialOrder.trans PO x<y lt 

<=to≤ : {x y : Ordinal } → x <= y → * x ≤ * y
<=to≤ (case1 eq) = case1 (cong (*) eq)
<=to≤ (case2 lt) = case2 lt

≤to<= : {x y : Ordinal } → * x ≤ * y → x <= y
≤to<= (case1 eq) = case1 ( subst₂ (λ j k → j ≡ k ) &iso &iso  (cong (&) eq) )
≤to<= (case2 lt) = case2 lt

<-irr : {a b : HOD} → (a ≡ b ) ∨ (a < b ) → b < a → ⊥
<-irr {a} {b} (case1 a=b) b<a = IsStrictPartialOrder.irrefl PO   (sym a=b) b<a
<-irr {a} {b} (case2 a<b) b<a = IsStrictPartialOrder.irrefl PO   refl
          (IsStrictPartialOrder.trans PO     b<a a<b)

ptrans =  IsStrictPartialOrder.trans PO

open _==_
open _⊆_

-- <-TransFinite : {A x : HOD} → {P : HOD → Set n} → x ∈ A
--     → ({x : HOD} → A ∋ x →  ({y : HOD} → A ∋  y → y < x → P y ) → P x) → P x
-- <-TransFinite = ?

--
-- Closure of ≤-monotonic function f has total order
--

≤-monotonic-f : (A : HOD) → ( Ordinal → Ordinal ) → Set (Level.suc n)
≤-monotonic-f A f = (x : Ordinal ) → odef A x → ( * x ≤ * (f x) ) ∧  odef A (f x )

<-monotonic-f : (A : HOD) → ( Ordinal → Ordinal ) → Set n
<-monotonic-f A f = (x : Ordinal ) → odef A x → ( * x < * (f x) ) ∧  odef A (f x )

data FClosure (A : HOD) (f : Ordinal → Ordinal ) (s : Ordinal) : Ordinal → Set n where
   init : {s1 : Ordinal } → odef A s → s ≡ s1  → FClosure A f s s1
   fsuc : (x : Ordinal) ( p :  FClosure A f s x ) → FClosure A f s (f x)

A∋fc : {A : HOD} (s : Ordinal) {y : Ordinal } (f : Ordinal → Ordinal) (mf : ≤-monotonic-f A f) → (fcy : FClosure A f s y ) → odef A y
A∋fc {A} s f mf (init as refl ) = as
A∋fc {A} s f mf (fsuc y fcy) = proj2 (mf y ( A∋fc {A} s  f mf fcy ) )

A∋fcs : {A : HOD} (s : Ordinal) {y : Ordinal } (f : Ordinal → Ordinal) (mf : ≤-monotonic-f A f) → (fcy : FClosure A f s y ) → odef A s
A∋fcs {A} s f mf (init as refl) = as
A∋fcs {A} s f mf (fsuc y fcy) = A∋fcs {A} s f mf fcy

s≤fc : {A : HOD} (s : Ordinal ) {y : Ordinal } (f : Ordinal → Ordinal) (mf : ≤-monotonic-f A f) → (fcy : FClosure A f s y ) → * s ≤ * y
s≤fc {A} s {.s} f mf (init x refl ) = case1 refl
s≤fc {A} s {.(f x)} f mf (fsuc x fcy) with proj1 (mf x (A∋fc s f mf fcy ) )
... | case1 x=fx = subst (λ k → * s ≤ * k ) (*≡*→≡ x=fx) ( s≤fc {A} s f mf fcy )
... | case2 x<fx with s≤fc {A} s f mf fcy
... | case1 s≡x = case2 ( subst₂ (λ j k → j < k ) (sym s≡x) refl x<fx )
... | case2 s<x = case2 ( IsStrictPartialOrder.trans PO s<x x<fx )

fcn : {A : HOD} (s : Ordinal) { x : Ordinal} {f : Ordinal → Ordinal} → (mf : ≤-monotonic-f A f) → FClosure A f s x → ℕ
fcn s mf (init as refl) = zero
fcn {A} s {x} {f} mf (fsuc y p) with proj1 (mf y (A∋fc s f mf p))
... | case1 eq = fcn s mf p
... | case2 y<fy = suc (fcn s mf p )

fcn-inject : {A : HOD} (s : Ordinal) { x y : Ordinal} {f : Ordinal → Ordinal} → (mf : ≤-monotonic-f A f)
     → (cx : FClosure A f s x ) (cy : FClosure A f s y ) → fcn s mf cx  ≡ fcn s mf cy → * x ≡ * y
fcn-inject {A} s {x} {y} {f} mf cx cy eq = fc00 (fcn s mf cx) (fcn s mf cy) eq cx cy refl refl where
     fc06 : {y : Ordinal } { as : odef A s } (eq : s ≡ y  ) { j : ℕ } →  ¬ suc j ≡ fcn {A} s {y} {f} mf (init as eq )
     fc06 {x} {y} refl {j} not = fc08 not where
        fc08 :  {j : ℕ} → ¬ suc j ≡ 0
        fc08 ()
     fc07 : {x : Ordinal } (cx : FClosure A f s x ) → 0 ≡ fcn s mf cx → * s ≡ * x
     fc07 {x} (init as refl) eq = refl
     fc07 {.(f x)} (fsuc x cx) eq with proj1 (mf x (A∋fc s f mf cx ) )
     ... | case1 x=fx = subst (λ k → * s ≡  k ) x=fx ( fc07 cx eq )
     -- ... | case2 x<fx = ?
     fc00 :  (i j : ℕ ) → i ≡ j  →  {x y : Ordinal } → (cx : FClosure A f s x ) (cy : FClosure A f s y ) → i ≡ fcn s mf cx  → j ≡ fcn s mf cy → * x ≡ * y
     fc00 (suc i) (suc j) x cx (init x₃ x₄) x₁ x₂ = ⊥-elim ( fc06 x₄ x₂ )
     fc00 (suc i) (suc j) x (init x₃ x₄) (fsuc x₅ cy) x₁ x₂ = ⊥-elim ( fc06 x₄ x₁ )
     fc00 zero zero refl (init _ refl) (init x₁ refl) i=x i=y = refl
     fc00 zero zero refl (init as refl) (fsuc y cy) i=x i=y with proj1 (mf y (A∋fc s f mf cy ) )
     ... | case1 y=fy = subst (λ k → * s ≡ k ) y=fy (fc07 cy i=y) -- ( fc00 zero zero refl (init as refl) cy i=x i=y )
     fc00 zero zero refl (fsuc x cx) (init as refl) i=x i=y with proj1 (mf x (A∋fc s f mf cx ) )
     ... | case1 x=fx = subst (λ k → k ≡ * s ) x=fx (sym (fc07 cx i=x)) -- ( fc00 zero zero refl cx (init as refl) i=x i=y )
     fc00 zero zero refl (fsuc x cx) (fsuc y cy) i=x i=y with proj1 (mf x (A∋fc s f mf cx ) ) | proj1 (mf y (A∋fc s f mf cy ) )
     ... | case1 x=fx  | case1 y=fy = subst₂ (λ j k → j ≡ k ) x=fx y=fy ( fc00 zero zero refl cx cy  i=x i=y )
     fc00 (suc i) (suc j) i=j {.(f x)} {.(f y)} (fsuc x cx) (fsuc y cy) i=x j=y with proj1 (mf x (A∋fc s f mf cx ) ) | proj1 (mf y (A∋fc s f mf cy ) )
     ... | case1 x=fx | case1 y=fy = subst₂ (λ j k → j ≡ k ) x=fx y=fy ( fc00 (suc i) (suc j) i=j cx cy  i=x j=y )
     ... | case1 x=fx | case2 y<fy = subst (λ k → k ≡ * (f y)) x=fx (fc02 x cx i=x) where
          fc02 : (x1 : Ordinal) → (cx1 : FClosure A f s x1 ) →  suc i ≡ fcn s mf cx1 → * x1 ≡ * (f y)
          fc02 x1 (init x₁ x₂) x = ⊥-elim (fc06 x₂ x)
          fc02 .(f x1) (fsuc x1 cx1) i=x1 with proj1 (mf x1 (A∋fc s f mf cx1 ) )
          ... | case1 eq = trans (sym eq) ( fc02  x1 cx1 i=x1 )  -- derefence while f x ≡ x
          ... | case2 lt = subst₂ (λ j k → * (f j) ≡ * (f k )) &iso &iso ( cong (λ k → * ( f (& k ))) fc04) where
               fc04 : * x1 ≡ * y
               fc04 = fc00 i j (cong pred i=j) cx1 cy (cong pred i=x1) (cong pred j=y)
     ... | case2 x<fx | case1 y=fy = subst (λ k → * (f x) ≡ k ) y=fy (fc03 y cy j=y) where
          fc03 : (y1 : Ordinal) → (cy1 : FClosure A f s y1 ) →  suc j ≡ fcn s mf cy1 → * (f x)  ≡ * y1
          fc03 y1 (init x₁ x₂) x = ⊥-elim (fc06 x₂ x)
          fc03 .(f y1) (fsuc y1 cy1) j=y1 with proj1 (mf y1 (A∋fc s f mf cy1 ) )
          ... | case1 eq = trans ( fc03  y1 cy1 j=y1 ) eq
          ... | case2 lt = subst₂ (λ j k → * (f j) ≡ * (f k )) &iso &iso ( cong (λ k → * ( f (& k ))) fc05) where
               fc05 : * x ≡ * y1
               fc05 = fc00 i j (cong pred i=j) cx cy1 (cong pred i=x) (cong pred j=y1)
     ... | case2 x₁ | case2 x₂ = subst₂ (λ j k → * (f j) ≡ * (f k) ) &iso &iso (cong (λ k → * (f (& k))) (fc00 i j (cong pred i=j) cx cy (cong pred i=x) (cong pred j=y)))


fcn-< : {A : HOD} (s : Ordinal ) { x y : Ordinal} {f : Ordinal → Ordinal} → (mf : ≤-monotonic-f A f)
    → (cx : FClosure A f s x ) (cy : FClosure A f s y ) → fcn s mf cx Data.Nat.< fcn s mf cy  → * x < * y
fcn-< {A} s {x} {y} {f} mf cx cy x<y = fc01 (fcn s mf cy) cx cy refl x<y where
     fc06 : {y : Ordinal } { as : odef A s } (eq : s ≡ y  ) { j : ℕ } →  ¬ suc j ≡ fcn {A} s {y} {f} mf (init as eq )
     fc06 {x} {y} refl {j} not = fc08 not where
        fc08 :  {j : ℕ} → ¬ suc j ≡ 0
        fc08 ()
     fc01 : (i : ℕ ) → {y : Ordinal } → (cx : FClosure A f s x ) (cy : FClosure A f s y ) → (i ≡ fcn s mf cy ) → fcn s mf cx Data.Nat.< i → * x < * y
     fc01 (suc i) cx (init x₁ x₂) x (s≤s x₃) = ⊥-elim (fc06 x₂ x)
     fc01 (suc i) {y} cx (fsuc y1 cy) i=y (s≤s x<i) with proj1 (mf y1 (A∋fc s f mf cy ) )
     ... | case1 y=fy = subst (λ k → * x < k ) y=fy ( fc01 (suc i) {y1} cx cy i=y (s≤s x<i)  )
     ... | case2 y<fy with <-cmp (fcn s mf cx ) i
     ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> x<i c )
     ... | tri≈ ¬a b ¬c = subst (λ k → k < * (f y1) ) (fcn-inject s mf cy cx (sym (trans b (cong pred i=y) ))) y<fy
     ... | tri< a ¬b ¬c = IsStrictPartialOrder.trans PO fc02 y<fy where
          fc03 :  suc i ≡ suc (fcn s mf cy) → i ≡ fcn s mf cy
          fc03 eq = cong pred eq
          fc02 :  * x < * y1
          fc02 =  fc01 i cx cy (fc03 i=y ) a


fcn-cmp : {A : HOD} (s : Ordinal) { x y : Ordinal } (f : Ordinal → Ordinal) (mf : ≤-monotonic-f A f)
    → (cx : FClosure A f s x) → (cy : FClosure A f s y ) → Tri (* x < * y) (* x ≡ * y) (* y < * x )
fcn-cmp {A} s {x} {y} f mf cx cy with <-cmp ( fcn s mf cx ) (fcn s mf cy )
... | tri< a ¬b ¬c = tri< fc11 (λ eq → <-irr (case1 (sym eq)) fc11) (λ lt → <-irr (case2 fc11) lt) where
      fc11 : * x < * y
      fc11 = fcn-< {A} s {x} {y} {f} mf cx cy a
... | tri≈ ¬a b ¬c = tri≈ (λ lt → <-irr (case1 (sym fc10)) lt) fc10 (λ lt → <-irr (case1 fc10) lt) where
      fc10 : * x ≡ * y
      fc10 = fcn-inject {A} s {x} {y} {f} mf cx cy b
... | tri> ¬a ¬b c = tri> (λ lt → <-irr (case2 fc12) lt) (λ eq → <-irr (case1 eq) fc12) fc12  where
      fc12 : * y < * x
      fc12 = fcn-< {A} s {y} {x} {f} mf cy cx c



-- open import Relation.Binary.Properties.Poset as Poset

IsTotalOrderSet : ( A : HOD ) → Set (Level.suc n)
IsTotalOrderSet A = {a b : HOD} → odef A (& a) → odef A (& b)  → Tri (a < b) (a ≡ b) (b < a )

⊆-IsTotalOrderSet : { A B : HOD } →  B ⊆ A  → IsTotalOrderSet A → IsTotalOrderSet B
⊆-IsTotalOrderSet {A} {B} B⊆A T  ax ay = T (incl B⊆A ax) (incl B⊆A ay)

_⊆'_ : ( A B : HOD ) → Set n
_⊆'_ A B = {x : Ordinal } → odef A x → odef B x

--
-- inductive maxmum tree from x
-- tree structure
--

record HasPrev (A B : HOD) ( f : Ordinal → Ordinal ) (x : Ordinal )   : Set n where
   field
      ax : odef A x
      y : Ordinal
      ay : odef B y
      x=fy :  x ≡ f y

record IsSUP (A B : HOD) {x : Ordinal } (xa : odef A x)     : Set n where
   field
      x≤sup   : {y : Ordinal} → odef B y → (y ≡ x ) ∨ (y << x )

record IsMinSUP (A B : HOD) ( f : Ordinal → Ordinal ) {x : Ordinal } (xa : odef A x)     : Set n where
   field
      x≤sup   : {y : Ordinal} → odef B y → (y ≡ x ) ∨ (y << x )
      minsup : { sup1 : Ordinal } → odef A sup1
         →  ( {z : Ordinal  } → odef B z → (z ≡ sup1 ) ∨ (z << sup1 ))  → x o≤ sup1
      not-hp : ¬ ( HasPrev A B f x )

record SUP ( A B : HOD )  : Set (Level.suc n) where
   field
      sup : HOD
      as : A ∋ sup
      x≤sup : {x : HOD} → B ∋ x → (x ≡ sup ) ∨ (x < sup )   -- B is Total, use positive

--
--  sup and its fclosure is in a chain HOD
--    chain HOD is sorted by sup as Ordinal and <-ordered
--    whole chain is a union of separated Chain
--    minimum index is sup of y not ϕ
--

record ChainP (A : HOD ) ( f : Ordinal → Ordinal ) (mf : ≤-monotonic-f A f)  {y : Ordinal} (ay : odef A y) (supf : Ordinal → Ordinal) (u : Ordinal) : Set n where
   field
      fcy<sup  : {z : Ordinal } → FClosure A f y z → (z ≡ supf u) ∨ ( z << supf u )
      order    : {s z1 : Ordinal} → (lt : supf s o< supf u ) → FClosure A f (supf s ) z1 → (z1 ≡ supf u ) ∨ ( z1 << supf u )
      supu=u   : supf u ≡ u

data UChain  ( A : HOD )    ( f : Ordinal → Ordinal )  (mf : ≤-monotonic-f A f) {y : Ordinal } (ay : odef A y )
       (supf : Ordinal → Ordinal) (x : Ordinal) : (z : Ordinal) → Set n where
    ch-init : {z : Ordinal } (fc : FClosure A f y z) → UChain A f mf ay supf x z
    ch-is-sup  : (u : Ordinal) {z : Ordinal }  (u<x : u o< x) ( is-sup : ChainP A f mf ay supf u )
        ( fc : FClosure A f (supf u) z ) → UChain A f mf ay supf x z

--
--         f (f ( ... (supf y))) f (f ( ... (supf z1)))
--        /          |         /             |
--       /           |        /              |
--    supf y   <       supf z1          <    supf z2
--           o<                      o<
--
--    if sup z1 ≡ sup z2, the chain is stopped at sup z1, then f (sup z1) ≡ sup z1


fc-stop : ( A : HOD )    ( f : Ordinal → Ordinal ) (mf : ≤-monotonic-f A f) { a b : Ordinal } 
    → (aa : odef A a ) →(  {y : Ordinal} → FClosure A f a y → (y ≡ b ) ∨ (y << b )) → a ≡ b → f a ≡ a
fc-stop A f mf {a} {b} aa x≤sup a=b with x≤sup (fsuc a (init aa refl ))
... | case1 eq = trans eq (sym a=b)
... | case2 lt = ⊥-elim (<-irr (case1 (cong (λ k → * (f k) ) (sym a=b))) (ftrans<-<= lt (≤to<= fc00 )) ) where
     fc00 :  * b ≤ * (f b)
     fc00 = proj1 (mf _ (subst (λ k → odef A k) a=b aa ))

--
-- data UChain is total

chain-total : (A : HOD ) ( f : Ordinal → Ordinal ) (mf : ≤-monotonic-f A f)  {y : Ordinal} (ay : odef A y) (supf : Ordinal → Ordinal )
   {s s1 a b : Ordinal } ( ca : UChain A f mf ay supf s a ) ( cb : UChain A f mf ay supf s1 b ) → Tri (* a < * b) (* a ≡ * b) (* b < * a )
chain-total A f mf {y} ay supf {xa} {xb} {a} {b} ca cb = ct-ind xa xb ca cb where
     ct-ind : (xa xb : Ordinal) → {a b : Ordinal} → UChain A f mf ay supf xa a → UChain A f mf ay supf xb b → Tri (* a < * b) (* a ≡ * b) (* b < * a)
     ct-ind xa xb {a} {b} (ch-init fca) (ch-init fcb) = fcn-cmp y f mf fca fcb
     ct-ind xa xb {a} {b} (ch-init fca) (ch-is-sup ub u<x supb fcb) with ChainP.fcy<sup supb fca
     ... | case1 eq with s≤fc (supf ub) f mf fcb
     ... | case1 eq1 = tri≈ (λ lt → ⊥-elim (<-irr (case1 (sym ct00)) lt)) ct00  (λ lt → ⊥-elim (<-irr (case1 ct00) lt)) where
          ct00 : * a ≡ * b
          ct00 = trans (cong (*) eq) eq1
     ... | case2 lt = tri< ct01  (λ eq → <-irr (case1 (sym eq)) ct01) (λ lt → <-irr (case2 ct01) lt)  where
          ct01 : * a < * b
          ct01 = subst (λ k → * k < * b ) (sym eq) lt
     ct-ind xa xb {a} {b} (ch-init fca) (ch-is-sup ub u<x supb fcb) | case2 lt = tri< ct01  (λ eq → <-irr (case1 (sym eq)) ct01) (λ lt → <-irr (case2 ct01) lt)  where
          ct00 : * a < * (supf ub)
          ct00 = lt
          ct01 : * a < * b
          ct01 with s≤fc (supf ub) f mf fcb
          ... | case1 eq =  subst (λ k → * a < k ) eq ct00
          ... | case2 lt =  IsStrictPartialOrder.trans POO ct00 lt
     ct-ind xa xb {a} {b} (ch-is-sup ua u<x supa fca) (ch-init fcb) with ChainP.fcy<sup supa fcb
     ... | case1 eq with s≤fc (supf ua) f mf fca
     ... | case1 eq1 = tri≈ (λ lt → ⊥-elim (<-irr (case1 (sym ct00)) lt)) ct00  (λ lt → ⊥-elim (<-irr (case1 ct00) lt)) where
          ct00 : * a ≡ * b
          ct00 = sym (trans (cong (*) eq) eq1 )
     ... | case2 lt = tri> (λ lt → <-irr (case2 ct01) lt) (λ eq → <-irr (case1 eq) ct01) ct01    where
          ct01 : * b < * a
          ct01 = subst (λ k → * k < * a ) (sym eq) lt
     ct-ind xa xb {a} {b} (ch-is-sup ua u<x supa fca) (ch-init fcb) | case2 lt = tri> (λ lt → <-irr (case2 ct01) lt) (λ eq → <-irr (case1 eq) ct01) ct01    where
          ct00 : * b < * (supf ua)
          ct00 = lt
          ct01 : * b < * a
          ct01 with s≤fc (supf ua) f mf fca
          ... | case1 eq =  subst (λ k → * b < k ) eq ct00
          ... | case2 lt =  IsStrictPartialOrder.trans POO ct00 lt
     ct-ind xa xb {a} {b} (ch-is-sup ua ua<x supa fca) (ch-is-sup ub ub<x supb fcb) with trio< ua ub
     ... | tri< a₁ ¬b ¬c with ChainP.order supb  (subst₂ (λ j k → j o< k ) (sym (ChainP.supu=u supa )) (sym (ChainP.supu=u supb )) a₁)  fca
     ... | case1 eq with s≤fc (supf ub) f mf fcb
     ... | case1 eq1 = tri≈ (λ lt → ⊥-elim (<-irr (case1 (sym ct00)) lt)) ct00  (λ lt → ⊥-elim (<-irr (case1 ct00) lt)) where
          ct00 : * a ≡ * b
          ct00 = trans (cong (*) eq) eq1
     ... | case2 lt =  tri< ct02  (λ eq → <-irr (case1 (sym eq)) ct02) (λ lt → <-irr (case2 ct02) lt)  where
          ct02 : * a < * b
          ct02 = subst (λ k → * k < * b ) (sym eq) lt
     ct-ind xa xb {a} {b} (ch-is-sup ua ua<x supa fca) (ch-is-sup ub ub<x supb fcb) | tri< a₁ ¬b ¬c | case2 lt = tri< ct02  (λ eq → <-irr (case1 (sym eq)) ct02) (λ lt → <-irr (case2 ct02) lt)  where
          ct03 : * a < * (supf ub)
          ct03 = lt
          ct02 : * a < * b
          ct02 with s≤fc (supf ub) f mf fcb
          ... | case1 eq =  subst (λ k → * a < k ) eq ct03
          ... | case2 lt =  IsStrictPartialOrder.trans POO ct03 lt
     ct-ind xa xb {a} {b} (ch-is-sup ua ua<x supa fca) (ch-is-sup ub ub<x  supb fcb) | tri≈ ¬a  eq ¬c
         = fcn-cmp (supf ua) f mf fca (subst (λ k → FClosure A f k b ) (cong supf (sym eq)) fcb )
     ct-ind xa xb {a} {b} (ch-is-sup ua ua<x supa fca) (ch-is-sup ub ub<x supb fcb) | tri> ¬a ¬b c with ChainP.order supa (subst₂ (λ j k → j o< k ) (sym (ChainP.supu=u supb )) (sym (ChainP.supu=u supa )) c) fcb
     ... | case1 eq with s≤fc (supf ua) f mf fca
     ... | case1 eq1 = tri≈ (λ lt → ⊥-elim (<-irr (case1 (sym ct00)) lt)) ct00  (λ lt → ⊥-elim (<-irr (case1 ct00) lt)) where
          ct00 : * a ≡ * b
          ct00 = sym (trans (cong (*) eq) eq1)
     ... | case2 lt =  tri> (λ lt → <-irr (case2 ct02) lt) (λ eq → <-irr (case1 eq) ct02) ct02    where
          ct02 : * b < * a
          ct02 = subst (λ k → * k < * a ) (sym eq) lt
     ct-ind xa xb {a} {b} (ch-is-sup ua ua<x supa fca) (ch-is-sup ub ub<x supb fcb) | tri> ¬a ¬b c | case2 lt = tri> (λ lt → <-irr (case2 ct04) lt) (λ eq → <-irr (case1 (eq)) ct04) ct04    where
          ct05 : * b < * (supf ua)
          ct05 = lt
          ct04 : * b < * a
          ct04 with s≤fc (supf ua) f mf fca
          ... | case1 eq =  subst (λ k → * b < k ) eq ct05
          ... | case2 lt =  IsStrictPartialOrder.trans POO ct05 lt

∈∧P→o< :  {A : HOD } {y : Ordinal} → {P : Set n} → odef A y ∧ P → y o< & A
∈∧P→o< {A } {y} p = subst (λ k → k o< & A) &iso ( c<→o< (subst (λ k → odef A k ) (sym &iso ) (proj1 p )))

-- Union of supf z which o< x
--
UnionCF : ( A : HOD )    ( f : Ordinal → Ordinal )  (mf : ≤-monotonic-f A f) {y : Ordinal } (ay : odef A y )
    ( supf : Ordinal → Ordinal ) ( x : Ordinal ) → HOD
UnionCF A f mf ay supf x
   = record { od = record { def = λ z → odef A z ∧ UChain A f mf ay supf x z } ; odmax = & A ; <odmax = λ {y} sy → ∈∧P→o< sy }

supf-inject0 : {x y : Ordinal } {supf : Ordinal → Ordinal } → (supf-mono : {x y : Ordinal } →  x o≤  y  → supf x o≤ supf y )
   → supf x o< supf y → x o<  y
supf-inject0 {x} {y} {supf} supf-mono sx<sy with trio< x y
... | tri< a ¬b ¬c = a
... | tri≈ ¬a refl ¬c = ⊥-elim ( o<¬≡ (cong supf refl) sx<sy )
... | tri> ¬a ¬b y<x with osuc-≡< (supf-mono (o<→≤ y<x) )
... | case1 eq = ⊥-elim ( o<¬≡ (sym eq) sx<sy )
... | case2 lt = ⊥-elim ( o<> sx<sy lt )

record MinSUP ( A B : HOD )  : Set n where
   field
      sup : Ordinal
      asm : odef A sup
      x≤sup : {x : Ordinal } → odef B x → (x ≡ sup ) ∨ (x << sup )
      minsup : { sup1 : Ordinal } → odef A sup1
         →  ( {x : Ordinal  } → odef B x → (x ≡ sup1 ) ∨ (x << sup1 ))  → sup o≤ sup1

z09 : {b : Ordinal } { A : HOD } → odef A b → b o< & A
z09 {b} {A} ab = subst (λ k → k o< & A) &iso ( c<→o< (subst (λ k → odef A k ) (sym &iso ) ab))

M→S  : { A : HOD } { f : Ordinal → Ordinal } {mf : ≤-monotonic-f A f} {y : Ordinal} {ay : odef A y}  { x : Ordinal }
      →  (supf : Ordinal → Ordinal )
      →  MinSUP A (UnionCF A f mf ay supf x)
      → SUP A (UnionCF A f mf ay supf x)
M→S {A} {f} {mf} {y} {ay} {x} supf ms = record { sup = * (MinSUP.sup ms)
        ; as = subst (λ k → odef A k) (sym &iso) (MinSUP.asm ms) ; x≤sup = ms00 } where
   msup = MinSUP.sup ms
   ms00 : {z : HOD} → UnionCF A f mf ay supf x ∋ z → (z ≡ * msup) ∨ (z < * msup)
   ms00 {z} uz with MinSUP.x≤sup ms uz
   ... | case1 eq = case1 (subst (λ k → k ≡ _) *iso ( cong (*) eq))
   ... | case2 lt = case2 (subst₂ (λ j k →  j < k ) *iso refl lt )


chain-mono : {A : HOD}  ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f )  {y : Ordinal} (ay : odef A y) (supf : Ordinal → Ordinal )
   (supf-mono : {x y : Ordinal } →  x o≤  y  → supf x o≤ supf y ) {a b c : Ordinal} → a o≤ b
        → odef (UnionCF A f mf ay supf a) c → odef (UnionCF A f mf ay supf b) c
chain-mono f mf ay supf supf-mono {a} {b} {c} a≤b ⟪ ua , ch-init fc  ⟫ =
        ⟪ ua , ch-init fc  ⟫
chain-mono f mf ay supf supf-mono {a} {b} {c} a≤b ⟪ uaa ,  ch-is-sup ua ua<x is-sup fc  ⟫ =
        ⟪ uaa , ch-is-sup ua (ordtrans<-≤ ua<x a≤b) is-sup fc  ⟫

record ZChain ( A : HOD )    ( f : Ordinal → Ordinal )  (mf : ≤-monotonic-f A f)
        {y : Ordinal} (ay : odef A y)  ( z : Ordinal ) : Set (Level.suc n) where
   field
      supf :  Ordinal → Ordinal
      sup=u : {b : Ordinal} → (ab : odef A b) → b o≤ z
           → IsSUP A (UnionCF A f mf ay supf b) ab ∧ (¬ HasPrev A (UnionCF A f mf ay supf b) f b ) → supf b ≡ b
      cfcs : (mf< : <-monotonic-f A f)
         {a b w : Ordinal } → a o< b → b o≤ z → supf a o< b  → FClosure A f (supf a) w → odef (UnionCF A f mf ay supf b) w

      asupf :  {x : Ordinal } → odef A (supf x)
      supf-<= : {x y w : Ordinal } → x o< y → FClosure A f (supf x) w → w <= supf y
      supf-mono : {x y : Ordinal } → x o≤  y → supf x o≤ supf y
      supfmax : {x : Ordinal } → z o< x → supf x ≡ supf z

      minsup : {x : Ordinal } → x o≤ z  → MinSUP A (UnionCF A f mf ay supf x)
      supf-is-minsup : {x : Ordinal } → (x≤z : x o≤ z) → supf x ≡ MinSUP.sup ( minsup x≤z )

   chain : HOD
   chain = UnionCF A f mf ay supf z
   chain⊆A : chain ⊆' A
   chain⊆A = λ lt → proj1 lt

   sup : {x : Ordinal } → x o≤ z  → SUP A (UnionCF A f mf ay supf x)
   sup {x} x≤z = M→S supf (minsup x≤z)

   s=ms : {x : Ordinal } → (x≤z : x o≤ z ) → & (SUP.sup (sup x≤z)) ≡ MinSUP.sup (minsup x≤z)
   s=ms {x} x≤z = &iso

   chain∋init : odef chain y
   chain∋init = ⟪ ay , ch-init (init ay refl)    ⟫
   f-next : {a z : Ordinal} → odef (UnionCF A f mf ay supf z) a → odef (UnionCF A f mf ay supf z) (f a)
   f-next {a} ⟪ aa , ch-init fc ⟫ = ⟪ proj2 (mf a aa) , ch-init (fsuc _ fc)  ⟫
   f-next {a} ⟪ aa , ch-is-sup u u<x is-sup fc ⟫ = ⟪ proj2 (mf a aa) , ch-is-sup u u<x is-sup (fsuc _ fc ) ⟫
   initial : {z : Ordinal } → odef chain z → * y ≤ * z
   initial {a} ⟪ aa , ua ⟫  with  ua
   ... | ch-init fc = s≤fc y f mf fc
   ... | ch-is-sup u u<x is-sup fc = ≤-ftrans (<=to≤ zc7) (s≤fc _ f mf fc)  where
        zc7 : y <= supf u
        zc7 = ChainP.fcy<sup is-sup (init ay refl)
   f-total : IsTotalOrderSet chain
   f-total {a} {b} ca cb = subst₂ (λ j k → Tri (j < k) (j ≡ k) (k < j)) *iso *iso uz01 where
               uz01 : Tri (* (& a) < * (& b)) (* (& a) ≡ * (& b)) (* (& b) < * (& a) )
               uz01 = chain-total A f mf ay supf ( (proj2 ca)) ( (proj2 cb))

   supf-inject : {x y : Ordinal } → supf x o< supf y → x o<  y
   supf-inject {x} {y} sx<sy with trio< x y
   ... | tri< a ¬b ¬c = a
   ... | tri≈ ¬a refl ¬c = ⊥-elim ( o<¬≡ (cong supf refl) sx<sy )
   ... | tri> ¬a ¬b y<x with osuc-≡< (supf-mono (o<→≤ y<x) )
   ... | case1 eq = ⊥-elim ( o<¬≡ (sym eq) sx<sy )
   ... | case2 lt = ⊥-elim ( o<> sx<sy lt )

   supf<A : {x : Ordinal } → supf x o< & A
   supf<A = z09 asupf

   csupf : (mf< : <-monotonic-f A f) {b : Ordinal } 
      → supf b o< supf z → supf b o< z → odef (UnionCF A f mf ay supf z) (supf b) -- supf z is not an element of this chain
   csupf mf< {b} sb<sz sb<z = cfcs mf< (supf-inject sb<sz) o≤-refl sb<z (init asupf refl)

   fcy<sup  : {u w : Ordinal } → u o≤ z  → FClosure A f y w → (w ≡ supf u ) ∨ ( w << supf u ) -- different from order because y o< supf
   fcy<sup  {u} {w} u≤z fc with MinSUP.x≤sup (minsup u≤z) ⟪ subst (λ k → odef A k ) (sym &iso) (A∋fc {A} y f mf fc)
       , ch-init (subst (λ k → FClosure A f y k) (sym &iso) fc ) ⟫
   ... | case1 eq = case1 (subst (λ k → k ≡ supf u ) &iso  (trans eq (sym (supf-is-minsup u≤z ) ) ))
   ... | case2 lt = case2 (subst₂ (λ j k → j << k ) &iso (sym (supf-is-minsup u≤z )) lt )

   -- ordering is not proved here but in ZChain1

   IsMinSUP→NotHasPrev : {x sp : Ordinal } → odef A sp
       → ({y : Ordinal} → odef (UnionCF A f mf ay supf x) y → (y ≡ sp ) ∨ (y << sp ))
       → ( {a : Ordinal } → odef A a → a << f a )
       → ¬ ( HasPrev A (UnionCF A f mf ay supf x) f sp )
   IsMinSUP→NotHasPrev {x} {sp} asp is-sup <-mono-f hp = ⊥-elim (<-irr ( <=to≤  fsp≤sp) sp<fsp ) where
       sp<fsp : sp << f sp
       sp<fsp = <-mono-f asp
       pr = HasPrev.y hp
       im00 : f (f pr) <= sp
       im00 = is-sup ( f-next (f-next (HasPrev.ay hp)))
       fsp≤sp : f sp <=  sp
       fsp≤sp = subst (λ k → f k <= sp ) (sym (HasPrev.x=fy hp)) im00

   supf-¬hp : {x  : Ordinal } → x o≤ z 
       → ( {a : Ordinal } → odef A a → a << f a )
       → ¬ ( HasPrev A (UnionCF A f mf ay supf x) f (supf x) )
   supf-¬hp {x} x≤z <-mono hp = IsMinSUP→NotHasPrev asupf (λ {w} uw → 
       (subst (λ k → w <= k) (sym (supf-is-minsup x≤z)) ( MinSUP.x≤sup (minsup x≤z) uw) )) <-mono hp

   supf-idem : (mf< : <-monotonic-f A f) {b : Ordinal } → b o≤ z → supf b o≤ z  → supf (supf b) ≡ supf b
   supf-idem  mf< {b} b≤z sfb≤x = z52 where
       z54 :  {w : Ordinal} → odef (UnionCF A f mf ay supf (supf b)) w → (w ≡ supf b) ∨ (w << supf b)
       z54 {w} ⟪ aw , ch-init fc ⟫ = fcy<sup b≤z fc
       z54 {w} ⟪ aw , ch-is-sup u u<x is-sup fc ⟫ = subst (λ k → (w ≡ k) ∨ (w << k )) 
                   (sym (supf-is-minsup b≤z)) 
                   (MinSUP.x≤sup (minsup b≤z) (cfcs mf< u<b b≤z (subst (λ k → k o< b) (sym (ChainP.supu=u is-sup)) u<b) fc )) where
               u<b : u o< b
               u<b = supf-inject ( subst (λ k → k o< supf b) (sym (ChainP.supu=u is-sup)) u<x )
       z52 : supf (supf b) ≡ supf b
       z52 = sup=u asupf sfb≤x ⟪ record { x≤sup = z54  } , IsMinSUP→NotHasPrev asupf z54 ( λ ax → proj1 (mf< _ ax)) ⟫ 

   -- cp : (mf< : <-monotonic-f A f) {b : Ordinal } → b o≤ z → supf b o≤ z  → ChainP A f mf ay supf (supf b)
   --    the condition of cfcs is satisfied, this is obvious

record ZChain1 ( A : HOD )    ( f : Ordinal → Ordinal )  (mf : ≤-monotonic-f A f)
        {y : Ordinal} (ay : odef A y)  (zc : ZChain A f mf ay (& A)) ( z : Ordinal ) : Set (Level.suc n) where
   supf = ZChain.supf zc
   field
      is-max :  {a b : Ordinal } → (ca : odef (UnionCF A f mf ay supf z) a ) → b o< z  → (ab : odef A b)
          → HasPrev A (UnionCF A f mf ay supf z) f b ∨  IsSUP A (UnionCF A f mf ay supf b) ab
          → * a < * b  → odef ((UnionCF A f mf ay supf z)) b

record Maximal ( A : HOD )  : Set (Level.suc n) where
   field
      maximal : HOD
      as : A ∋ maximal
      ¬maximal<x : {x : HOD} → A ∋ x  → ¬ maximal < x       -- A is Partial, use negative

init-uchain : (A : HOD)  ( f : Ordinal → Ordinal )  (mf : ≤-monotonic-f A f) {y : Ordinal } → (ay : odef A y )
    { supf : Ordinal → Ordinal } { x : Ordinal } → odef (UnionCF A f mf ay supf x) y
init-uchain A f mf ay = ⟪ ay , ch-init (init ay refl)   ⟫

record IChain  (A : HOD)  ( f : Ordinal → Ordinal ) {x : Ordinal } (supfz : {z : Ordinal } → z o< x → Ordinal) (z : Ordinal ) : Set n where
  field
     i : Ordinal  
     i<x : i o< x
     fc : FClosure A f (supfz i<x) z 

--
-- supf in TransFinite indution may differ each other, but it is the same because of the minimul sup
--
supf-unique :  ( A : HOD )    ( f : Ordinal → Ordinal )  (mf : ≤-monotonic-f A f)
        {y xa xb : Ordinal} → (ay : odef A y) →  (xa o≤ xb ) → (za : ZChain A f mf ay xa ) (zb : ZChain A f mf ay xb ) 
      → {z : Ordinal } → z o≤ xa → ZChain.supf za z ≡ ZChain.supf zb z
supf-unique A f mf {y} {xa} {xb} ay xa≤xb za zb {z} z≤xa = TransFinite0 {λ z → z o≤ xa → ZChain.supf za z ≡ ZChain.supf zb z } ind z z≤xa  where
       supfa = ZChain.supf za
       supfb = ZChain.supf zb
       ind : (x : Ordinal) → ((w : Ordinal) → w o< x → w o≤ xa → supfa w ≡ supfb w) → x o≤ xa → supfa x ≡ supfb x
       ind x prev x≤xa = sxa=sxb where
           ma = ZChain.minsup za x≤xa 
           mb = ZChain.minsup zb (OrdTrans x≤xa xa≤xb )
           spa = MinSUP.sup ma
           spb = MinSUP.sup mb
           sax=spa : supfa x ≡ spa
           sax=spa = ZChain.supf-is-minsup za x≤xa 
           sbx=spb : supfb x ≡ spb
           sbx=spb = ZChain.supf-is-minsup zb (OrdTrans x≤xa xa≤xb )
           sxa=sxb : supfa x ≡ supfb x
           sxa=sxb with trio< (supfa x) (supfb x)
           ... | tri≈ ¬a b ¬c = b
           ... | tri< a ¬b ¬c = ⊥-elim ( o≤> (
               begin
                 supfb x  ≡⟨ sbx=spb ⟩
                 spb  ≤⟨ MinSUP.minsup mb (MinSUP.asm ma) (λ {z} uzb → MinSUP.x≤sup ma (z53 uzb)) ⟩
                 spa ≡⟨ sym sax=spa ⟩ 
                 supfa x ∎ ) a ) where 
                    open o≤-Reasoning O
                    z53 : {z : Ordinal } →  odef (UnionCF A f mf ay (ZChain.supf zb) x) z →  odef (UnionCF A f mf ay (ZChain.supf za) x) z
                    z53 ⟪ as , ch-init fc ⟫ = ⟪ as , ch-init fc ⟫ 
                    z53 {z} ⟪ as , ch-is-sup u u<x is-sup fc ⟫ =  ⟪ as , ch-is-sup u u<x z54 z55  ⟫ where
                        ua=ub : supfa u ≡ supfb u
                        ua=ub = prev u u<x (ordtrans u<x x≤xa )
                        order :  {s z1 : Ordinal} → ZChain.supf za s o< ZChain.supf za u → FClosure A f (ZChain.supf za s) z1 →
                            (z1 ≡ ZChain.supf za u) ∨ (z1 << ZChain.supf za u)
                        order {s} {z1} lt fc = subst (λ k → z1 <= k) (sym ua=ub) 
                             (ChainP.order is-sup (subst₂ ( λ j k → j o< k ) z56 ua=ub lt ) (subst (λ k → FClosure A f k z1 ) z56 fc )) where
                             s<x : s o< x
                             s<x = ordtrans (ZChain.supf-inject za lt) u<x
                             z56 : supfa s ≡ supfb s
                             z56 = prev s s<x (ordtrans s<x x≤xa)
                        z54 : ChainP A f mf ay (ZChain.supf za) u  
                        z54 = record { fcy<sup = λ {w} fc → subst (λ k → w <= k ) (sym ua=ub) (ChainP.fcy<sup is-sup fc )
                          ; order = order
                          ; supu=u = trans ua=ub (ChainP.supu=u is-sup) } 
                        z55 : FClosure A f (ZChain.supf za u) z
                        z55 = subst (λ k → FClosure A f k z ) (sym ua=ub) fc
           ... | tri> ¬a ¬b c = ⊥-elim ( o≤> (
               begin
                 supfa x  ≡⟨ sax=spa ⟩
                 spa  ≤⟨ MinSUP.minsup ma (MinSUP.asm mb) (λ uza → MinSUP.x≤sup mb (z53 uza)) ⟩
                 spb  ≡⟨ sym sbx=spb ⟩
                 supfb x ∎ ) c ) where 
                    open o≤-Reasoning O
                    z53 : {z : Ordinal } →  odef (UnionCF A f mf ay (ZChain.supf za) x) z →  odef (UnionCF A f mf ay (ZChain.supf zb) x) z
                    z53 ⟪ as , ch-init fc ⟫ = ⟪ as , ch-init fc ⟫ 
                    z53 {z} ⟪ as , ch-is-sup u u<x is-sup fc ⟫ =  ⟪ as , ch-is-sup u u<x z54 z55  ⟫ where
                        ub=ua : supfb u ≡ supfa u
                        ub=ua = sym ( prev u u<x (ordtrans u<x x≤xa ))
                        order :  {s z1 : Ordinal} → ZChain.supf zb s o< ZChain.supf zb u → FClosure A f (ZChain.supf zb s) z1 →
                            (z1 ≡ ZChain.supf zb u) ∨ (z1 << ZChain.supf zb u)
                        order {s} {z1} lt fc = subst (λ k → z1 <= k) (sym ub=ua) 
                             (ChainP.order is-sup (subst₂ ( λ j k → j o< k ) z56 ub=ua lt ) (subst (λ k → FClosure A f k z1 ) z56 fc )) where
                             s<x : s o< x
                             s<x = ordtrans (ZChain.supf-inject zb lt) u<x
                             z56 : supfb s ≡ supfa s
                             z56 = sym (prev s s<x (ordtrans s<x x≤xa))
                        z54 : ChainP A f mf ay (ZChain.supf zb) u  
                        z54 = record { fcy<sup = λ {w} fc → subst (λ k → w <= k ) (sym ub=ua) (ChainP.fcy<sup is-sup fc )
                          ; order = order
                          ; supu=u = trans ub=ua (ChainP.supu=u is-sup) } 
                        z55 : FClosure A f (ZChain.supf zb u) z
                        z55 = subst (λ k → FClosure A f k z ) (sym ub=ua) fc

Zorn-lemma : { A : HOD }
    → o∅ o< & A
    → ( ( B : HOD) → (B⊆A : B ⊆' A) → IsTotalOrderSet B → SUP A B   ) -- SUP condition
    → Maximal A
Zorn-lemma {A}  0<A supP = zorn00 where
     <-irr0 : {a b : HOD} → A ∋ a → A ∋ b  → (a ≡ b ) ∨ (a < b ) → b < a → ⊥
     <-irr0 {a} {b} A∋a A∋b = <-irr
     z07 :   {y : Ordinal} {A : HOD } → {P : Set n} → odef A y ∧ P → y o< & A
     z07 {y} {A} p = subst (λ k → k o< & A) &iso ( c<→o< (subst (λ k → odef A k ) (sym &iso ) (proj1 p )))
     s : HOD
     s = ODC.minimal O A (λ eq → ¬x<0 ( subst (λ k → o∅ o< k ) (=od∅→≡o∅ eq) 0<A ))
     as : A ∋ * ( & s  )
     as =  subst (λ k → odef A (& k) ) (sym *iso) ( ODC.x∋minimal O A (λ eq → ¬x<0 ( subst (λ k → o∅ o< k ) (=od∅→≡o∅ eq) 0<A ))  )
     as0 : odef A  (& s  )
     as0 =  subst (λ k → odef A k ) &iso as
     s<A : & s o< & A
     s<A = c<→o< (subst (λ k → odef A (& k) ) *iso as )
     HasMaximal : HOD
     HasMaximal = record { od = record { def = λ x → odef A x ∧ ( (m : Ordinal) →  odef A m → ¬ (* x < * m)) }  ; odmax = & A ; <odmax = z07 }
     no-maximum : HasMaximal =h= od∅ → (x : Ordinal) → odef A x ∧ ((m : Ordinal) →  odef A m →  odef A x ∧ (¬ (* x < * m) )) →  ⊥
     no-maximum nomx x P = ¬x<0 (eq→ nomx {x} ⟪ proj1 P , (λ m ma p → proj2 ( proj2 P m ma ) p ) ⟫ )
     Gtx : { x : HOD} → A ∋ x → HOD
     Gtx {x} ax = record { od = record { def = λ y → odef A y ∧ (x < (* y)) } ; odmax = & A ; <odmax = z07 }
     z08  : ¬ Maximal A →  HasMaximal =h= od∅
     z08 nmx  = record { eq→  = λ {x} lt → ⊥-elim ( nmx record {maximal = * x ; as = subst (λ k → odef A k) (sym &iso) (proj1 lt)
         ; ¬maximal<x = λ {y} ay → subst (λ k → ¬ (* x < k)) *iso (proj2 lt (& y) ay) } ) ; eq← =  λ {y} lt → ⊥-elim ( ¬x<0 lt )}
     x-is-maximal : ¬ Maximal A → {x : Ordinal} → (ax : odef A x) → & (Gtx (subst (λ k → odef A k ) (sym &iso) ax)) ≡ o∅ → (m : Ordinal) → odef A m → odef A x ∧ (¬ (* x < * m))
     x-is-maximal nmx {x} ax nogt m am  = ⟪ subst (λ k → odef A k) &iso (subst (λ k → odef A k ) (sym &iso) ax) ,  ¬x<m  ⟫ where
        ¬x<m :  ¬ (* x < * m)
        ¬x<m x<m = ∅< {Gtx (subst (λ k → odef A k ) (sym &iso) ax)} {* m} ⟪ subst (λ k → odef A k) (sym &iso) am , subst (λ k → * x < k ) (cong (*) (sym &iso)) x<m ⟫  (≡o∅→=od∅ nogt)

     --
     -- we have minsup using LEM, this is similar to the proof of the axiom of choice
     --
     minsupP :  ( B : HOD) → (B⊆A : B ⊆' A) → IsTotalOrderSet B → MinSUP A B
     minsupP B B⊆A total = m02 where
         xsup : (sup : Ordinal ) → Set n
         xsup sup = {w : Ordinal } → odef B w → (w ≡ sup ) ∨ (w << sup )
         ∀-imply-or :  {A : Ordinal  → Set n } {B : Set n }
                        → ((x : Ordinal ) → A x ∨ B) →  ((x : Ordinal ) → A x) ∨ B
         ∀-imply-or  {A} {B} ∀AB with ODC.p∨¬p O ((x : Ordinal ) → A x) -- LEM
         ∀-imply-or  {A} {B} ∀AB | case1 t = case1 t
         ∀-imply-or  {A} {B} ∀AB | case2 x  = case2 (lemma (λ not → x not )) where
               lemma : ¬ ((x : Ordinal ) → A x) →  B
               lemma not with ODC.p∨¬p O B
               lemma not | case1 b = b
               lemma not | case2 ¬b = ⊥-elim  (not (λ x → dont-orb (∀AB x) ¬b ))
         m00 : (x : Ordinal ) → ( ( z : Ordinal) → z o< x →  ¬ (odef A z ∧ xsup z) ) ∨ MinSUP A B
         m00 x = TransFinite0 ind x where
            ind : (x : Ordinal) → ((z : Ordinal) → z o< x → ( ( w : Ordinal) → w o< z →  ¬ (odef A w ∧ xsup w ))  ∨ MinSUP A B)
                  → ( ( w : Ordinal) → w o< x →  ¬ (odef A w ∧ xsup w) )  ∨ MinSUP A B
            ind x prev  =  ∀-imply-or m01 where
                m01 : (z : Ordinal) → (z o< x → ¬ (odef A z ∧ xsup z)) ∨ MinSUP A B
                m01 z with trio< z x
                ... | tri≈ ¬a b ¬c = case1 ( λ lt →  ⊥-elim ( ¬a lt )  )
                ... | tri> ¬a ¬b c = case1 ( λ lt →  ⊥-elim ( ¬a lt )  )
                ... | tri< a ¬b ¬c with prev z a
                ... | case2 mins = case2 mins
                ... | case1 not with ODC.p∨¬p O (odef A z ∧ xsup z)
                ... | case1 mins = case2 record { sup = z ; asm = proj1 mins ; x≤sup = proj2 mins ; minsup = m04 } where
                  m04 : {sup1 : Ordinal} → odef A sup1 → ({w : Ordinal} → odef B w → (w ≡ sup1) ∨ (w << sup1)) → z o≤ sup1
                  m04 {s} as lt with trio< z s
                  ... | tri< a ¬b ¬c = o<→≤ a
                  ... | tri≈ ¬a b ¬c = o≤-refl0 b
                  ... | tri> ¬a ¬b s<z = ⊥-elim ( not s s<z ⟪ as , lt ⟫  )
                ... | case2 notz = case1 (λ _ → notz )
         m03 : ¬ ((z : Ordinal) → z o< & A → ¬ odef A z ∧ xsup z)
         m03 not = ⊥-elim ( not s1 (z09 (SUP.as S)) ⟪ SUP.as S , m05 ⟫ ) where
             S : SUP A B
             S = supP B  B⊆A total
             s1 = & (SUP.sup S)
             m05 : {w : Ordinal } → odef B w → (w ≡ s1 ) ∨ (w << s1 )
             m05 {w} bw with SUP.x≤sup S {* w} (subst (λ k → odef B k) (sym &iso) bw )
             ... | case1 eq = case1 ( subst₂ (λ j k → j ≡ k ) &iso refl (cong (&) eq) )
             ... | case2 lt = case2 ( subst (λ k → _ < k ) (sym *iso) lt )
         m02 : MinSUP A B
         m02 = dont-or (m00 (& A)) m03

     -- Uncountable ascending chain by axiom of choice
     cf : ¬ Maximal A → Ordinal → Ordinal
     cf  nmx x with ODC.∋-p O A (* x)
     ... | no _ = o∅
     ... | yes ax with is-o∅ (& ( Gtx ax ))
     ... | yes nogt = -- no larger element, so it is maximal
         ⊥-elim (no-maximum (z08 nmx) x ⟪ subst (λ k → odef A k) &iso ax , x-is-maximal nmx (subst (λ k → odef A k ) &iso ax) nogt ⟫ )
     ... | no not =  & (ODC.minimal O (Gtx ax) (λ eq → not (=od∅→≡o∅ eq)))
     is-cf : (nmx : ¬ Maximal A ) → {x : Ordinal} → odef A x → odef A (cf nmx x) ∧ ( * x < * (cf nmx x) )
     is-cf nmx {x} ax with ODC.∋-p O A (* x)
     ... | no not = ⊥-elim ( not (subst (λ k → odef A k ) (sym &iso) ax ))
     ... | yes ax with is-o∅ (& ( Gtx ax ))
     ... | yes nogt = ⊥-elim (no-maximum (z08 nmx) x ⟪ subst (λ k → odef A k) &iso ax , x-is-maximal nmx (subst (λ k → odef A k ) &iso ax) nogt ⟫ )
     ... | no not = ODC.x∋minimal O (Gtx ax) (λ eq → not (=od∅→≡o∅ eq))

     ---
     --- infintie ascention sequence of f
     ---
     cf-is-<-monotonic : (nmx : ¬ Maximal A ) → (x : Ordinal) →  odef A x → ( * x < * (cf nmx x) ) ∧  odef A (cf nmx x )
     cf-is-<-monotonic nmx x ax = ⟪ proj2 (is-cf nmx ax ) , proj1 (is-cf nmx ax ) ⟫
     cf-is-≤-monotonic : (nmx : ¬ Maximal A ) →  ≤-monotonic-f A ( cf nmx )
     cf-is-≤-monotonic nmx x ax = ⟪ case2 (proj1 ( cf-is-<-monotonic nmx x ax  ))  , proj2 ( cf-is-<-monotonic nmx x ax  ) ⟫

     --
     -- maximality of chain
     --
     --     supf is fixed for z ≡ & A , we can prove order and is-max
     --     we have supf-unique now, it is provable in the first Tranfinte induction

     SZ1 : ( f : Ordinal → Ordinal )  (mf : ≤-monotonic-f A f) (mf< : <-monotonic-f A f)
        {init : Ordinal} (ay : odef A init) (zc : ZChain A f mf ay (& A)) (x : Ordinal) → x o≤ & A → ZChain1 A f mf ay zc x
     SZ1 f mf mf< {y} ay zc x x≤A = zc1 x x≤A  where
        chain-mono1 :  {a b c : Ordinal} → a o≤ b
            → odef (UnionCF A f mf ay (ZChain.supf zc) a) c → odef (UnionCF A f mf ay (ZChain.supf zc) b) c
        chain-mono1  {a} {b} {c} a≤b = chain-mono f mf ay (ZChain.supf zc) (ZChain.supf-mono zc) a≤b
        is-max-hp : (x : Ordinal) {a : Ordinal} {b : Ordinal} → odef (UnionCF A f mf ay (ZChain.supf zc) x) a → (ab : odef A b)
            → HasPrev A (UnionCF A f mf ay (ZChain.supf zc) x) f b
            → * a < * b → odef (UnionCF A f mf ay (ZChain.supf zc) x) b
        is-max-hp x {a} {b} ua ab has-prev a<b with HasPrev.ay has-prev
        ... | ⟪ ab0 , ch-init fc ⟫ = ⟪ ab , ch-init ( subst (λ k → FClosure A f y k) (sym (HasPrev.x=fy has-prev)) (fsuc _ fc )) ⟫
        ... | ⟪ ab0 , ch-is-sup u u<x is-sup fc ⟫ = ⟪ ab , subst (λ k → UChain A f mf ay (ZChain.supf zc) x k )
                      (sym (HasPrev.x=fy has-prev)) ( ch-is-sup u u<x is-sup (fsuc _ fc))  ⟫

        supf = ZChain.supf zc

        zc1 :  (x : Ordinal ) → x o≤ & A →   ZChain1 A f mf ay zc x
        zc1 x x≤A with Oprev-p x  
        ... | yes op = record { is-max = is-max } where
               px = Oprev.oprev op
               is-max :  {a : Ordinal} {b : Ordinal} → odef (UnionCF A f mf ay supf x) a →
                  b o< x → (ab : odef A b) →
                  HasPrev A (UnionCF A f mf ay supf x) f b  ∨ IsSUP A (UnionCF A f mf ay supf b) ab →
                  * a < * b → odef (UnionCF A f mf ay supf x) b
               is-max {a} {b} ua b<x ab P a<b with ODC.or-exclude O P
               is-max {a} {b} ua b<x ab P a<b | case1 has-prev = is-max-hp x {a} {b} ua ab has-prev a<b
               is-max {a} {b} ua b<x ab P a<b | case2 is-sup with osuc-≡< (ZChain.supf-mono zc (o<→≤ b<x))
               ... | case2 sb<sx = m10 where
                  b<A : b o< & A
                  b<A = z09 ab
                  m04 : ¬ HasPrev A (UnionCF A f mf ay supf b) f b
                  m04 nhp = proj1 is-sup ( record { ax = HasPrev.ax nhp ; y = HasPrev.y nhp ; ay =
                        chain-mono1 (o<→≤ b<x) (HasPrev.ay  nhp) ; x=fy = HasPrev.x=fy nhp } )
                  m05 : ZChain.supf zc b ≡ b
                  m05 =  ZChain.sup=u zc ab (o<→≤ (z09 ab) ) ⟪ record { x≤sup = λ {z} uz → IsSUP.x≤sup (proj2 is-sup) uz  }  , m04 ⟫
                  m10 : odef (UnionCF A f mf ay supf x) b
                  m10 = ZChain.cfcs zc mf< b<x x≤A (subst (λ k → k o< x) (sym m05) b<x) (init (ZChain.asupf zc) m05)
               ... | case1 sb=sx = ⊥-elim (<-irr (case1 (cong (*) m10)) (proj1 (mf< (supf b) (ZChain.asupf zc)))) where
                  m17 : MinSUP A (UnionCF A f mf ay supf x) -- supf z o< supf ( supf x )
                  m17 = ZChain.minsup zc x≤A
                  m18 : supf x ≡ MinSUP.sup m17 
                  m18 = ZChain.supf-is-minsup zc x≤A
                  m10 : f (supf b) ≡ supf b
                  m10 = fc-stop A f mf (ZChain.asupf zc) m11 sb=sx where
                      m11 : {z : Ordinal} → FClosure A f (supf b) z → (z ≡ ZChain.supf zc x) ∨ (z << ZChain.supf zc x)
                      m11 {z} fc = subst (λ k → (z ≡ k) ∨ (z << k)) (sym m18) ( MinSUP.x≤sup m17 m13 ) where
                          m04 : ¬ HasPrev A (UnionCF A f mf ay supf b) f b
                          m04 nhp = proj1 is-sup ( record { ax = HasPrev.ax nhp ; y = HasPrev.y nhp ; ay =
                                chain-mono1 (o<→≤ b<x) (HasPrev.ay  nhp) ; x=fy = HasPrev.x=fy nhp } )
                          m05 : ZChain.supf zc b ≡ b
                          m05 =  ZChain.sup=u zc ab (o<→≤ (z09 ab) ) ⟪ record { x≤sup = λ {z} uz → IsSUP.x≤sup (proj2 is-sup) uz  }  , m04 ⟫
                          m14 : ZChain.supf zc b o< x
                          m14 = subst (λ k → k o< x ) (sym m05)  b<x
                          m13 :  odef (UnionCF A f mf ay supf x) z
                          m13 = ZChain.cfcs zc mf< b<x x≤A m14 fc

        ... | no lim = record { is-max = is-max }  where
               is-max :  {a : Ordinal} {b : Ordinal} → odef (UnionCF A f mf ay supf x) a →
                  b o< x → (ab : odef A b) →
                  HasPrev A (UnionCF A f mf ay supf x) f b  ∨ IsSUP A (UnionCF A f mf ay supf b) ab →
                  * a < * b → odef (UnionCF A f mf ay supf x) b
               is-max {a} {b} ua b<x ab P a<b with ODC.or-exclude O P
               is-max {a} {b} ua b<x ab P a<b | case1 has-prev = is-max-hp x {a} {b} ua ab has-prev a<b
               is-max {a} {b} ua b<x ab P a<b | case2 is-sup with IsSUP.x≤sup (proj2 is-sup) (init-uchain A f mf ay )
               ... | case1 b=y = ⟪ subst (λ k → odef A k ) b=y ay , ch-init (subst (λ k → FClosure A f y k ) b=y (init ay refl ))  ⟫
               ... | case2 y<b with osuc-≡< (ZChain.supf-mono zc (o<→≤ b<x))
               ... | case2 sb<sx = m10 where
                  m09 : b o< & A
                  m09 = subst (λ k → k o< & A) &iso ( c<→o< (subst (λ k → odef A k ) (sym &iso ) ab))
                  m04 : ¬ HasPrev A (UnionCF A f mf ay supf b) f b
                  m04 nhp = proj1 is-sup ( record { ax = HasPrev.ax nhp ; y = HasPrev.y nhp ; ay =
                          chain-mono1 (o<→≤ b<x) (HasPrev.ay  nhp)
                     ; x=fy = HasPrev.x=fy nhp } )
                  m05 : ZChain.supf zc b ≡ b
                  m05 = ZChain.sup=u zc ab (o<→≤  m09) ⟪ record { x≤sup = λ lt → IsSUP.x≤sup (proj2 is-sup) lt } , m04 ⟫    -- ZChain on x
                  m10 : odef (UnionCF A f mf ay supf x) b
                  m10 = ZChain.cfcs zc mf< b<x x≤A (subst (λ k → k o< x) (sym m05) b<x) (init (ZChain.asupf zc) m05)
               ... | case1 sb=sx = ⊥-elim (<-irr (case1 (cong (*) m10)) (proj1 (mf< (supf b) (ZChain.asupf zc)))) where
                  m17 : MinSUP A (UnionCF A f mf ay supf x) -- supf z o< supf ( supf x )
                  m17 = ZChain.minsup zc x≤A
                  m18 : supf x ≡ MinSUP.sup m17 
                  m18 = ZChain.supf-is-minsup zc x≤A
                  m10 : f (supf b) ≡ supf b
                  m10 = fc-stop A f mf (ZChain.asupf zc) m11 sb=sx where
                      m11 : {z : Ordinal} → FClosure A f (supf b) z → (z ≡ ZChain.supf zc x) ∨ (z << ZChain.supf zc x)
                      m11 {z} fc = subst (λ k → (z ≡ k) ∨ (z << k)) (sym m18) ( MinSUP.x≤sup m17 m13 ) where
                          m04 : ¬ HasPrev A (UnionCF A f mf ay supf b) f b
                          m04 nhp = proj1 is-sup ( record { ax = HasPrev.ax nhp ; y = HasPrev.y nhp ; ay =
                                chain-mono1 (o<→≤ b<x) (HasPrev.ay  nhp) ; x=fy = HasPrev.x=fy nhp } )
                          m05 : ZChain.supf zc b ≡ b
                          m05 =  ZChain.sup=u zc ab (o<→≤ (z09 ab) ) ⟪ record { x≤sup = λ {z} uz → IsSUP.x≤sup (proj2 is-sup) uz  }  , m04 ⟫
                          m14 : ZChain.supf zc b o< x
                          m14 = subst (λ k → k o< x ) (sym m05)  b<x
                          m13 :  odef (UnionCF A f mf ay supf x) z
                          m13 = ZChain.cfcs zc mf< b<x x≤A m14 fc

     uchain : ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f ) {y : Ordinal} (ay : odef A y) → HOD
     uchain f mf {y} ay = record { od = record { def = λ x → FClosure A f y x } ; odmax = & A ; <odmax =
             λ {z} cz → subst (λ k → k o< & A) &iso ( c<→o< (subst (λ k → odef A k ) (sym &iso ) (A∋fc y f mf cz ))) }

     utotal : ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f ) {y : Ordinal} (ay : odef A y)
        → IsTotalOrderSet (uchain f mf ay)
     utotal f mf {y} ay {a} {b} ca cb = subst₂ (λ j k → Tri (j < k) (j ≡ k) (k < j)) *iso *iso uz01 where
               uz01 : Tri (* (& a) < * (& b)) (* (& a) ≡ * (& b)) (* (& b) < * (& a) )
               uz01 = fcn-cmp y f mf ca cb

     ysup : ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f ) {y : Ordinal} (ay : odef A y)
       →  MinSUP A (uchain f mf ay)
     ysup f mf {y} ay = minsupP (uchain f mf ay) (λ lt → A∋fc y f mf lt)  (utotal f mf ay)


     SUP⊆ : { B C : HOD } → B ⊆' C → SUP A C → SUP A B
     SUP⊆ {B} {C} B⊆C sup = record { sup = SUP.sup sup ; as = SUP.as sup ; x≤sup = λ lt → SUP.x≤sup sup (B⊆C lt)    }

     record xSUP (B : HOD) (f : Ordinal → Ordinal ) (x : Ordinal) : Set n where
        field
           ax : odef A x
           is-sup : IsMinSUP A B f ax

     zc43 : (x sp1 : Ordinal ) → ( x o< sp1 ) ∨ ( sp1 o≤ x )
     zc43 x sp1 with trio< x sp1
     ... | tri< a ¬b ¬c = case1 a
     ... | tri≈ ¬a b ¬c = case2 (o≤-refl0 (sym b))
     ... | tri> ¬a ¬b c = case2 (o<→≤ c)

     --
     -- create all ZChains under o< x
     --

     ind : ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f ) {y : Ordinal} (ay : odef A y) → (x : Ordinal)
         → ((z : Ordinal) → z o< x → ZChain A f mf ay z) → ZChain A f mf ay x
     ind f mf {y} ay x prev with Oprev-p x
     ... | yes op = zc41 where
          --
          -- we have previous ordinal to use induction
          --
          px = Oprev.oprev op
          zc : ZChain A f mf ay (Oprev.oprev op)
          zc = prev px (subst (λ k → px o< k) (Oprev.oprev=x op) <-osuc )
          px<x : px o< x
          px<x = subst (λ k → px o< k) (Oprev.oprev=x op) <-osuc
          opx=x : osuc px ≡ x
          opx=x = Oprev.oprev=x op

          zc-b<x : (b : Ordinal ) → b o< x → b o< osuc px
          zc-b<x b lt = subst (λ k → b o< k ) (sym (Oprev.oprev=x op)) lt

          supf0 = ZChain.supf zc
          pchain  : HOD
          pchain   = UnionCF A f mf ay supf0 px

          supf-mono : {a b : Ordinal } → a o≤ b → supf0 a o≤ supf0 b
          supf-mono = ZChain.supf-mono zc

          zc04 : {b : Ordinal} → b o≤ x → (b o≤ px ) ∨ (b ≡ x )
          zc04 {b} b≤x with trio< b px
          ... | tri< a ¬b ¬c = case1 (o<→≤ a)
          ... | tri≈ ¬a b ¬c = case1 (o≤-refl0 b)
          ... | tri> ¬a ¬b px<b with osuc-≡< b≤x
          ... | case1 eq = case2 eq
          ... | case2 b<x = ⊥-elim ( ¬p<x<op ⟪ px<b , subst (λ k → b o< k ) (sym (Oprev.oprev=x op)) b<x  ⟫ )

          --
          -- find the next value of supf
          --

          pchainpx : HOD
          pchainpx = record { od = record { def = λ z →  (odef A z ∧ UChain A f mf ay supf0 px z )
                ∨ FClosure A f (supf0 px) z  } ; odmax = & A ; <odmax = zc00 } where
               zc00 : {z : Ordinal } → (odef A z ∧ UChain A f mf ay supf0 px z ) ∨ FClosure A f (supf0 px) z → z o< & A
               zc00 {z} (case1 lt) = z07 lt
               zc00 {z} (case2 fc) = z09 ( A∋fc (supf0 px) f mf fc )

          zc02 : { a b : Ordinal } → odef A a ∧ UChain A f mf ay supf0 px a → FClosure A f (supf0 px) b → a <= b
          zc02 {a} {b} ca fb = zc05 fb where
             zc06 : MinSUP.sup (ZChain.minsup zc o≤-refl) ≡ supf0 px
             zc06 = trans (sym ( ZChain.supf-is-minsup zc o≤-refl )) refl
             zc05 : {b : Ordinal } → FClosure A f (supf0 px) b → a <= b
             zc05 (fsuc b1 fb ) with proj1 ( mf b1 (A∋fc (supf0 px) f mf fb ))
             ... | case1 eq = subst (λ k → a <= k ) (subst₂ (λ j k → j ≡ k) &iso &iso (cong (&) eq)) (zc05 fb)
             ... | case2 lt = <=-trans (zc05 fb) (case2 lt)
             zc05 (init b1 refl) with MinSUP.x≤sup (ZChain.minsup zc o≤-refl)
                (subst (λ k → odef A k ∧ UChain A f mf ay supf0 px k) (sym &iso) ca )
             ... | case1 eq = case1 (subst₂ (λ j k → j ≡ k ) &iso zc06 eq )
             ... | case2 lt = case2 (subst₂ (λ j k → j < k ) *iso (cong (*) zc06) lt )

          ptotal : IsTotalOrderSet pchainpx
          ptotal (case1 a) (case1 b) =  subst₂ (λ j k → Tri (j < k) (j ≡ k) (k < j)) *iso *iso
                   (chain-total A f mf ay supf0 (proj2 a) (proj2 b))
          ptotal {a0} {b0} (case1 a) (case2 b) with zc02 a b
          ... | case1 eq = tri≈ (<-irr (case1 (sym eq1))) eq1 (<-irr (case1 eq1)) where
               eq1 : a0 ≡ b0
               eq1 = subst₂ (λ j k → j ≡ k ) *iso *iso (cong (*) eq )
          ... | case2 lt = tri< lt1 (λ eq → <-irr (case1 (sym eq)) lt1) (<-irr (case2 lt1)) where
               lt1 : a0 < b0
               lt1 = subst₂ (λ j k → j < k ) *iso *iso lt
          ptotal {b0} {a0} (case2 b) (case1 a) with zc02 a b
          ... | case1 eq = tri≈ (<-irr (case1 eq1)) (sym eq1) (<-irr (case1 (sym eq1))) where
               eq1 : a0 ≡ b0
               eq1 = subst₂ (λ j k → j ≡ k ) *iso *iso (cong (*) eq )
          ... | case2 lt = tri> (<-irr (case2 lt1)) (λ eq → <-irr (case1 eq) lt1) lt1  where
               lt1 : a0 < b0
               lt1 = subst₂ (λ j k → j < k ) *iso *iso lt
          ptotal (case2 a) (case2 b) = subst₂ (λ j k → Tri (j < k) (j ≡ k) (k < j)) *iso *iso (fcn-cmp (supf0 px) f mf a b)

          pcha : pchainpx ⊆' A
          pcha (case1 lt) = proj1 lt
          pcha (case2 fc) = A∋fc _ f mf fc

          sup1 : MinSUP A pchainpx
          sup1 = minsupP pchainpx pcha ptotal
          sp1 = MinSUP.sup sup1

          sfpx<=sp1 : supf0 px <= sp1
          sfpx<=sp1 = MinSUP.x≤sup sup1 (case2 (init (ZChain.asupf zc {px}) refl ))

          sfpx≤sp1 : supf0 px o≤ sp1
          sfpx≤sp1 = subst ( λ k → k o≤ sp1) (sym (ZChain.supf-is-minsup zc o≤-refl ))  
            ( MinSUP.minsup (ZChain.minsup zc o≤-refl) (MinSUP.asm sup1)  
              (λ {x} ux → MinSUP.x≤sup sup1 (case1 ux)) )

          --
          --     supf0 px o≤ sp1
          --

          zc41 : ZChain A f mf ay x
          zc41 with zc43 x sp1
          zc41 | (case2 sp≤x ) =  record { supf = supf1 ; sup=u = ? ; asupf = asupf1 ; supf-mono = supf1-mono 
              ; supfmax = ? ; minsup = ? ; supf-is-minsup = ? ; cfcs = cfcs  }  where

                 supf1 : Ordinal → Ordinal
                 supf1 z with trio< z px
                 ... | tri< a ¬b ¬c = supf0 z
                 ... | tri≈ ¬a b ¬c = supf0 z
                 ... | tri> ¬a ¬b c = sp1

                 sf1=sf0 : {z : Ordinal } → z o≤ px → supf1 z ≡ supf0 z
                 sf1=sf0 {z} z≤px with trio< z px
                 ... | tri< a ¬b ¬c = refl
                 ... | tri≈ ¬a b ¬c = refl
                 ... | tri> ¬a ¬b c = ⊥-elim ( o≤> z≤px c )

                 sf1=sp1 : {z : Ordinal } → px o< z → supf1 z ≡ sp1
                 sf1=sp1 {z} px<z with trio< z px
                 ... | tri< a ¬b ¬c = ⊥-elim ( o<> px<z a )
                 ... | tri≈ ¬a b ¬c = ⊥-elim ( o<¬≡ (sym b) px<z )
                 ... | tri> ¬a ¬b c = refl

                 sf=eq :  { z : Ordinal } → z o< x → supf0 z ≡ supf1 z
                 sf=eq {z} z<x = sym (sf1=sf0 (subst (λ k → z o< k ) (sym (Oprev.oprev=x op)) z<x ))

                 asupf1 : {z : Ordinal } → odef A (supf1 z)
                 asupf1 {z} with trio< z px
                 ... | tri< a ¬b ¬c = ZChain.asupf zc
                 ... | tri≈ ¬a b ¬c = ZChain.asupf zc
                 ... | tri> ¬a ¬b c = MinSUP.asm sup1

                 supf1-mono : {a b : Ordinal } → a o≤ b → supf1 a o≤ supf1 b
                 supf1-mono {a} {b} a≤b with trio< b px
                 ... | tri< a ¬b ¬c =  subst₂ (λ j k → j o≤ k ) (sym (sf1=sf0 (o<→≤ (ordtrans≤-< a≤b a)))) refl ( supf-mono a≤b )
                 ... | tri≈ ¬a b ¬c =  subst₂ (λ j k → j o≤ k ) (sym (sf1=sf0 (subst (λ k → a o≤ k) b a≤b))) refl ( supf-mono a≤b )
                 supf1-mono {a} {b} a≤b | tri> ¬a ¬b c with trio< a px
                 ... | tri< a<px ¬b ¬c = zc19 where
                       zc21 : MinSUP A (UnionCF A f mf ay supf0 a)
                       zc21 = ZChain.minsup zc (o<→≤ a<px)
                       zc24 : {x₁ : Ordinal} → odef (UnionCF A f mf ay supf0 a) x₁ → (x₁ ≡ sp1) ∨ (x₁ << sp1)
                       zc24 {x₁} ux = MinSUP.x≤sup sup1 (case1 (chain-mono f mf ay supf0 (ZChain.supf-mono zc) (o<→≤ a<px) ux ) )
                       zc19 : supf0 a o≤ sp1
                       zc19 = subst (λ k → k o≤ sp1) (sym (ZChain.supf-is-minsup zc  (o<→≤ a<px))) ( MinSUP.minsup zc21 (MinSUP.asm sup1) zc24 )
                 ... | tri≈ ¬a b ¬c = zc18 where
                       zc21 : MinSUP A (UnionCF A f mf ay supf0 a)
                       zc21 = ZChain.minsup zc (o≤-refl0 b)
                       zc20 : MinSUP.sup zc21 ≡ supf0 a
                       zc20 = sym (ZChain.supf-is-minsup zc (o≤-refl0 b))
                       zc24 : {x₁ : Ordinal} → odef (UnionCF A f mf ay supf0 a) x₁ → (x₁ ≡ sp1) ∨ (x₁ << sp1)
                       zc24 {x₁} ux = MinSUP.x≤sup sup1 (case1 (chain-mono f mf ay supf0 (ZChain.supf-mono zc) (o≤-refl0 b) ux ) )
                       zc18 : supf0 a o≤ sp1
                       zc18 = subst (λ k → k o≤ sp1) zc20( MinSUP.minsup zc21 (MinSUP.asm sup1) zc24 )
                 ... | tri> ¬a ¬b c = o≤-refl

                 sf≤ :  { z : Ordinal } → x o≤ z → supf0 x o≤ supf1 z
                 sf≤ {z} x≤z with trio< z px
                 ... | tri< a ¬b ¬c = ⊥-elim ( o<> (osucc a) (subst (λ k → k o≤ z) (sym (Oprev.oprev=x op)) x≤z ) )
                 ... | tri≈ ¬a b ¬c = ⊥-elim ( o≤> x≤z (subst (λ k → k o< x ) (sym b) px<x ))
                 ... | tri> ¬a ¬b c = subst₂ (λ j k → j o≤ k ) (trans (sf1=sf0 o≤-refl ) (sym (ZChain.supfmax zc px<x))) (sf1=sp1 c) 
                     (supf1-mono (o<→≤ c ))
                      --  px o<z → supf x ≡ supf0 px ≡ supf1 px o≤ supf1 z

                 fcup : {u z : Ordinal } → FClosure A f (supf1 u) z → u o≤ px → FClosure A f (supf0 u) z
                 fcup {u} {z} fc u≤px = subst (λ k → FClosure A f k z ) (sf1=sf0 u≤px) fc
                 fcpu : {u z : Ordinal } → FClosure A f (supf0 u) z → u o≤ px →  FClosure A f (supf1 u) z
                 fcpu {u} {z} fc u≤px = subst (λ k → FClosure A f k z ) (sym (sf1=sf0 u≤px)) fc

                 -- this is a kind of maximality, so we cannot prove this without <-monotonicity
                 --
                 cfcs : (mf< : <-monotonic-f A f) {a b w : Ordinal } 
                     → a o< b → b o≤ x → supf1 a o< b  → FClosure A f (supf1 a) w → odef (UnionCF A f mf ay supf1 b) w
                 cfcs mf< {a} {b} {w} a<b b≤x sa<b fc with zc43 (supf0 a) px
                 ... | case2 px≤sa = z50 where
                      a<x : a o< x
                      a<x = ordtrans<-≤ a<b b≤x
                      a≤px : a o≤ px
                      a≤px = subst (λ k → a o< k) (sym (Oprev.oprev=x op)) (ordtrans<-≤ a<b b≤x)
                      --  supf0 a ≡ px we cannot use previous cfcs, it is in the chain because
                      --       supf0 a ≡ supf0 (supf0 a) ≡ supf0 px o< x
                      z50 : odef (UnionCF A f mf ay supf1 b) w
                      z50 with osuc-≡< px≤sa
                      ... | case1 px=sa = ⟪ A∋fc {A} _ f mf fc , ch-is-sup (supf0 px) z51 cp (subst (λ k → FClosure A f k w) z52 fc)  ⟫ where
                          sa≤px : supf0 a o≤ px
                          sa≤px = subst₂ (λ j k → j o< k) px=sa (sym (Oprev.oprev=x op)) px<x
                          spx=sa : supf0 px ≡ supf0 a
                          spx=sa = begin
                                supf0 px ≡⟨ cong supf0 px=sa  ⟩ 
                                supf0 (supf0 a) ≡⟨ ZChain.supf-idem zc mf< a≤px sa≤px  ⟩
                                supf0 a ∎  where open ≡-Reasoning
                          z51 : supf0 px o< b
                          z51 = subst (λ k → k o< b ) (sym ( begin supf0 px ≡⟨ spx=sa ⟩ 
                                supf0 a ≡⟨ sym (sf1=sf0 a≤px) ⟩ 
                                supf1 a ∎ )) sa<b where open ≡-Reasoning
                          z52 : supf1 a ≡ supf1 (supf0 px)
                          z52 = begin supf1 a ≡⟨ sf1=sf0 a≤px ⟩ 
                                supf0 a ≡⟨ sym (ZChain.supf-idem zc mf< a≤px sa≤px ) ⟩ 
                                supf0 (supf0 a) ≡⟨ sym (sf1=sf0 sa≤px)  ⟩ 
                                supf1 (supf0 a) ≡⟨ cong supf1 (sym spx=sa) ⟩ 
                                supf1 (supf0 px) ∎ where open ≡-Reasoning
                          m : MinSUP A (UnionCF A f mf ay supf0 px)
                          m = ZChain.minsup zc o≤-refl
                          m=spx : MinSUP.sup m ≡ supf1 (supf0 px)
                          m=spx = begin 
                                MinSUP.sup m ≡⟨ sym ( ZChain.supf-is-minsup zc o≤-refl)  ⟩ 
                                supf0 px ≡⟨ cong supf0 px=sa  ⟩ 
                                supf0 (supf0 a) ≡⟨ sym (sf1=sf0 sa≤px)  ⟩
                                supf1 (supf0 a) ≡⟨ cong supf1 (sym spx=sa)  ⟩
                                supf1 (supf0 px)  ∎  where open ≡-Reasoning 
                          z53 : supf1 (supf0 px) ≡ supf0 px
                          z53 = begin
                                supf1 (supf0 px)  ≡⟨ cong supf1 spx=sa ⟩ 
                                supf1 (supf0 a)  ≡⟨ sf1=sf0 sa≤px ⟩ 
                                supf0 (supf0 a)  ≡⟨ sym ( cong supf0 px=sa ) ⟩ 
                                supf0 px  ∎  where open ≡-Reasoning 
                          cp : ChainP A f mf ay supf1 (supf0 px)
                          cp = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ ( z << k ) ) m=spx (MinSUP.x≤sup m ⟪  A∋fc _ f mf fc , ch-init fc ⟫ )  
                            ; order = order 
                            ; supu=u = z53 } where
                             uz : {s z1 : Ordinal } → supf1 s o< supf1 (supf0 px) → FClosure A f (supf1 s) z1 → odef (UnionCF A f mf ay supf0 px) z1
                             uz {s} {z1} ss<sp fc = ZChain.cfcs zc mf< s<px o≤-refl ss<px (subst (λ k → FClosure A f k z1) 
                                     (sf1=sf0 (o<→≤ s<px))  fc ) where
                                s<spx : s o< supf0 px
                                s<spx = supf-inject0 supf1-mono ss<sp
                                s<px : s o< px
                                s<px = osucprev ( begin
                                     osuc s  ≤⟨  osucc s<spx   ⟩  
                                     supf0 px  ≡⟨ spx=sa  ⟩ 
                                     supf0 a ≡⟨ sym px=sa  ⟩ 
                                     px ∎ )   where open o≤-Reasoning O
                                ss<px : supf0 s o< px
                                ss<px = osucprev ( begin
                                     osuc (supf0 s)  ≡⟨ cong osuc (sym (sf1=sf0 (o<→≤ s<px)))  ⟩ 
                                     osuc (supf1 s)  ≤⟨ osucc ss<sp  ⟩ 
                                     supf1 (supf0 px)  ≡⟨ sym z52  ⟩ 
                                     supf1 a  ≡⟨ sf1=sf0 a≤px  ⟩ 
                                     supf0 a  ≡⟨ sym px=sa  ⟩ 
                                     px ∎ )   where open o≤-Reasoning O
                             order : {s : Ordinal} {z1 : Ordinal} → supf1 s o< supf1 (supf0 px) →
                                    FClosure A f (supf1 s) z1 → (z1 ≡ supf1 (supf0 px)) ∨ (z1 << supf1 (supf0 px))
                             order {s} {z} s<u fc  = subst (λ k → (z ≡ k) ∨ ( z << k ) ) m=spx (MinSUP.x≤sup m (uz s<u fc) )
                      ... | case2 px<sa = ⊥-elim ( ¬p<x<op ⟪ px<sa , subst₂ (λ j k → j o< k ) (sf1=sf0 a≤px) (sym (Oprev.oprev=x op)) z53 ⟫  ) where
                          z53  : supf1 a o< x
                          z53  = ordtrans<-≤ sa<b b≤x
                 ... | case1 sa<px with trio< a px
                 ... | tri< a<px ¬b ¬c = z50 where
                      z50 : odef (UnionCF A f mf ay supf1 b) w
                      z50 with osuc-≡< b≤x
                      ... | case2 lt with ZChain.cfcs zc mf< a<b (subst (λ k → b o< k) (sym (Oprev.oprev=x op)) lt) sa<b fc  
                      ... | ⟪ az , ch-init fc ⟫ = ⟪ az , ch-init fc ⟫ 
                      ... | ⟪ az , ch-is-sup u u<b is-sup fc ⟫ = ⟪ az , ch-is-sup u u<b cp1 (fcpu fc u≤px )  ⟫  where -- u o< px → u o< b ?
                           u≤px : u o≤ px
                           u≤px = subst (λ k → u o< k) (sym (Oprev.oprev=x op))  (ordtrans<-≤ u<b b≤x )
                           u<x : u o< x
                           u<x = ordtrans<-≤ u<b b≤x 
                           cp1 : ChainP A f mf ay supf1 u
                           cp1 = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sf=eq u<x) (ChainP.fcy<sup is-sup fc )  
                               ; order =  λ {s} {z} s<u fc  → subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sf=eq u<x) 
                                  (ChainP.order is-sup (subst₂ (λ j k → j o< k ) (sym (sf=eq (ordtrans (supf-inject0 supf1-mono s<u) u<x) )) 
                                   (sym (sf=eq u<x)) s<u)  
                                    (subst (λ k → FClosure A f k z ) (sym (sf=eq (ordtrans (supf-inject0 supf1-mono s<u) u<x) )) fc ))
                               ; supu=u = trans (sym (sf=eq u<x)) (ChainP.supu=u is-sup)  }
                      z50 | case1 eq with ZChain.cfcs zc mf< a<px o≤-refl sa<px fc  
                      ... | ⟪ az , ch-init fc ⟫ = ⟪ az , ch-init fc ⟫ 
                      ... | ⟪ az , ch-is-sup u u<px is-sup fc ⟫ = ⟪ az , ch-is-sup u u<b cp1 (fcpu fc (o<→≤ u<px)) ⟫  where -- u o< px → u o< b ?
                           u<b : u o< b
                           u<b = subst (λ k → u o< k ) (trans (Oprev.oprev=x op) (sym eq) ) (ordtrans u<px <-osuc )
                           u<x : u o< x
                           u<x = subst (λ k → u o< k ) (Oprev.oprev=x op) ( ordtrans u<px <-osuc )
                           cp1 : ChainP A f mf ay supf1 u
                           cp1 = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sf=eq u<x) (ChainP.fcy<sup is-sup fc )  
                               ; order =  λ {s} {z} s<u fc  → subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sf=eq u<x) 
                                  (ChainP.order is-sup (subst₂ (λ j k → j o< k ) (sym (sf=eq (ordtrans (supf-inject0 supf1-mono s<u) u<x) )) 
                                   (sym (sf=eq u<x)) s<u)  
                                    (subst (λ k → FClosure A f k z ) (sym (sf=eq (ordtrans (supf-inject0 supf1-mono s<u) u<x) )) fc ))
                               ; supu=u = trans (sym (sf=eq u<x)) (ChainP.supu=u is-sup)  }
                 ... | tri≈ ¬a a=px ¬c = csupf1 where
                      -- a ≡ px , b ≡ x, sp o≤ x 
                      px<b : px o< b
                      px<b = subst₂ (λ j k → j o< k) a=px refl a<b
                      b=x : b ≡ x
                      b=x with trio< b x
                      ... | tri< a ¬b ¬c = ⊥-elim ( ¬p<x<op ⟪ px<b , subst (λ k → b o< k ) (sym (Oprev.oprev=x op)) a ⟫ ) --  px o< b o< x
                      ... | tri≈ ¬a b ¬c = b
                      ... | tri> ¬a ¬b c = ⊥-elim ( o≤> b≤x c ) --   x o< b
                      z51 : FClosure A f (supf1 px) w
                      z51 = subst (λ k → FClosure A f k w) (sym (trans (cong supf1 (sym a=px)) (sf1=sf0 (o≤-refl0 a=px) ))) fc
                      z53 : odef A w
                      z53 = A∋fc {A} _ f mf fc
                      csupf1 : odef (UnionCF A f mf ay supf1 b) w
                      csupf1 with trio< (supf0 px) x
                      ... | tri< sfpx<x ¬b ¬c = ⟪ z53 , ch-is-sup spx (subst (λ k → spx o< k) (sym b=x) sfpx<x) cp1 fc1 ⟫  where
                          spx = supf0 px
                          spx≤px : supf0 px o≤ px
                          spx≤px = zc-b<x _ sfpx<x
                          z52 : supf1 (supf0 px) ≡ supf0 px
                          z52 = trans (sf1=sf0 (zc-b<x _ sfpx<x)) ( ZChain.supf-idem zc mf< o≤-refl (zc-b<x _ sfpx<x ) )
                          fc1 : FClosure A f (supf1 spx) w
                          fc1 = subst (λ k → FClosure A f k w ) (trans (cong supf0 a=px) (sym z52) ) fc
                          order : {s z1 : Ordinal} → supf1 s o< supf1 spx → FClosure A f (supf1 s) z1 → (z1 ≡ supf1 spx) ∨ (z1 << supf1 spx)
                          order {s} {z1} ss<spx fcs = subst (λ k → (z1 ≡ k) ∨ (z1 << k )) 
                                   (trans (sym (ZChain.supf-is-minsup zc spx≤px )) (sym (sf1=sf0 spx≤px) ) )
                                   (MinSUP.x≤sup (ZChain.minsup zc spx≤px) (ZChain.cfcs zc mf< (supf-inject0 supf1-mono ss<spx) 
                                       spx≤px ss0<spx (fcup fcs (ordtrans (supf-inject0 supf1-mono ss<spx) spx≤px ) ))) where
                                          ss0<spx : supf0 s o< spx
                                          ss0<spx = osucprev ( begin
                                            osuc (supf0 s)  ≡⟨ cong osuc (sym (sf1=sf0 ( begin
                                               s <⟨ supf-inject0 supf1-mono ss<spx ⟩
                                               supf0 px  ≤⟨ spx≤px ⟩
                                               px ∎ ) )) ⟩
                                            osuc (supf1 s)  ≤⟨ osucc ss<spx ⟩
                                            supf1 spx  ≡⟨ sf1=sf0 spx≤px  ⟩
                                            supf0 spx  ≤⟨ ZChain.supf-mono zc spx≤px ⟩
                                            supf0 px  ∎  ) where open o≤-Reasoning O
                          cp1 : ChainP A f mf ay supf1 spx
                          cp1 = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ (z << k )) (sym (sf1=sf0 spx≤px )) 
                                  ( ZChain.fcy<sup zc spx≤px fc )
                                       ; order =  order
                                       ; supu=u = z52 }
                      ... | tri≈ ¬a spx=x ¬c = ⊥-elim (<-irr (case1 (cong (*) m10)) (proj1 (mf< (supf0 px) (ZChain.asupf zc)))) where 
                          -- supf px ≡ x then the chain is stopped, which cannot happen when <-monotonic case
                          m12 : supf0 px ≡ sp1
                          m12 with osuc-≡< sfpx≤sp1
                          ... | case1 eq = eq
                          ... | case2 lt = ⊥-elim ( o≤> sp≤x (subst (λ k → k o< sp1) spx=x lt )) -- supf0 px o< sp1 , x o< sp1
                          m10 : f (supf0 px) ≡ supf0 px
                          m10 = fc-stop A f mf (ZChain.asupf zc) m11 m12 where
                              m11 : {z : Ordinal} → FClosure A f (supf0 px) z → (z ≡ sp1) ∨ (z << sp1)
                              m11 {z} fc = MinSUP.x≤sup sup1 (case2 fc)  
                      ... | tri> ¬a ¬b c = ⊥-elim ( o<¬≡ refl (ordtrans<-≤ c (OrdTrans sfpx≤sp1 sp≤x)))   -- x o< supf0 px o≤ sp1 ≤ x
                 ... | tri> ¬a ¬b c = ⊥-elim ( ¬p<x<op ⟪ c , subst (λ k → a o< k ) (sym (Oprev.oprev=x op)) ( ordtrans<-≤ a<b b≤x) ⟫ ) --  px o< a o< b o≤ x

                 zc11 : {z : Ordinal} → odef (UnionCF A f mf ay supf1 x) z → odef pchainpx z
                 zc11 {z} ⟪ az , ch-init fc ⟫ = case1 ⟪ az , ch-init fc ⟫
                 zc11 {z} ⟪ az , ch-is-sup u u<x is-sup fc ⟫ = zc21 fc where
                    u≤px : u o≤ px
                    u≤px = zc-b<x _ u<x
                    zc21 : {z1 : Ordinal } → FClosure A f (supf1 u) z1 → odef pchainpx z1
                    zc21 {z1} (fsuc z2 fc ) with zc21 fc
                    ... | case1 ⟪ ua1 , ch-init fc₁ ⟫ = case1 ⟪ proj2 ( mf _ ua1)  , ch-init (fsuc _ fc₁)  ⟫
                    ... | case1 ⟪ ua1 , ch-is-sup u u<x u1-is-sup fc₁ ⟫ = case1 ⟪ proj2 ( mf _ ua1)  , ch-is-sup u u<x u1-is-sup (fsuc _ fc₁) ⟫
                    ... | case2 fc = case2 (fsuc _ fc)
                    zc21 (init asp refl ) with trio< (supf0 u) (supf0 px) | inspect supf1 u
                    ... | tri< a ¬b ¬c | _ = case1 ⟪ asp , ch-is-sup u u<px record {fcy<sup = zc13 ; order = zc17
                         ; supu=u = trans (sym (sf1=sf0 (o<→≤ u<px))) (ChainP.supu=u is-sup) } (init asp0 (sym (sf1=sf0 (o<→≤ u<px))) ) ⟫ where
                        u<px :  u o< px
                        u<px =  ZChain.supf-inject zc a
                        asp0 : odef A (supf0 u)
                        asp0 = ZChain.asupf zc
                        zc17 :  {s : Ordinal} {z1 : Ordinal} → supf0 s o< supf0 u →
                            FClosure A f (supf0 s) z1 → (z1 ≡ supf0 u) ∨ (z1 << supf0 u)
                        zc17 {s} {z1} ss<spx fc = subst (λ k → (z1 ≡ k) ∨ (z1 << k)) ((sf1=sf0 u≤px)) ( ChainP.order is-sup
                         (subst₂ (λ j k → j o< k ) (sym (sf1=sf0 zc18)) (sym (sf1=sf0 u≤px)) ss<spx) (fcpu fc zc18) ) where
                            zc18 : s o≤ px
                            zc18 = ordtrans (ZChain.supf-inject zc ss<spx) u≤px
                        zc13 : {z : Ordinal } → FClosure A f y z → (z ≡ supf0 u) ∨ ( z << supf0 u )
                        zc13 {z} fc = subst (λ k → (z ≡ k) ∨ ( z << k )) (sf1=sf0 (o<→≤ u<px)) ( ChainP.fcy<sup is-sup fc )
                    ... | tri≈ ¬a b ¬c | _ = case2 (init (subst (λ k → odef A k) b (ZChain.asupf zc) ) (sym (trans (sf1=sf0 u≤px) b )))
                    ... | tri> ¬a ¬b c | _ = ⊥-elim ( ¬p<x<op ⟪ ZChain.supf-inject zc c , subst (λ k → u o< k ) (sym (Oprev.oprev=x op)) u<x  ⟫ )

                 record STMP {z : Ordinal} (z≤x : z o≤ x ) : Set (Level.suc n) where
                     field
                         tsup : MinSUP A (UnionCF A f mf ay supf1 z)
                         tsup=sup : supf1 z ≡ MinSUP.sup tsup

                 sup : {z : Ordinal} → (z≤x : z o≤ x ) → STMP z≤x
                 sup {z} z≤x with trio< z px
                 ... | tri< a ¬b ¬c = record { tsup = record { sup = MinSUP.sup m  ; asm = MinSUP.asm m
                         ; x≤sup = ms00 ; minsup = ms01 } ; tsup=sup = trans (sf1=sf0 (o<→≤ a) ) (ZChain.supf-is-minsup zc (o<→≤ a)) } where
                    m = ZChain.minsup zc (o<→≤ a)
                    ms00 :  {x : Ordinal} → odef (UnionCF A f mf ay supf1 z) x → (x ≡ MinSUP.sup m) ∨ (x << MinSUP.sup m)
                    ms00 {x} ux = MinSUP.x≤sup m ?
                    ms01 : {sup2 : Ordinal} → odef A sup2 → ({x : Ordinal} →
                        odef (UnionCF A f mf ay supf1 z) x → (x ≡ sup2) ∨ (x << sup2)) → MinSUP.sup m o≤ sup2
                    ms01 {sup2} us P = MinSUP.minsup m us ?
                 ... | tri≈ ¬a b ¬c = record { tsup = record { sup = MinSUP.sup m  ; asm = MinSUP.asm m
                         ; x≤sup = ms00 ; minsup = ms01 } ; tsup=sup = trans (sf1=sf0 (o≤-refl0 b) ) (ZChain.supf-is-minsup zc (o≤-refl0 b))  } where
                    m = ZChain.minsup zc (o≤-refl0 b)
                    ms00 :  {x : Ordinal} → odef (UnionCF A f mf ay supf1 z) x → (x ≡ MinSUP.sup m) ∨ (x << MinSUP.sup m)
                    ms00 {x} ux = MinSUP.x≤sup m ?
                    ms01 : {sup2 : Ordinal} → odef A sup2 → ({x : Ordinal} →
                        odef (UnionCF A f mf ay supf1 z) x → (x ≡ sup2) ∨ (x << sup2)) → MinSUP.sup m o≤ sup2
                    ms01 {sup2} us P = MinSUP.minsup m us ?
                 ... | tri> ¬a ¬b px<z = record { tsup = record { sup = sp1 ; asm = MinSUP.asm sup1
                         ; x≤sup = ms00 ; minsup = ms01 } ; tsup=sup = sf1=sp1 px<z } where
                    m = sup1
                    ms00 :  {x : Ordinal} → odef (UnionCF A f mf ay supf1 z) x → (x ≡ MinSUP.sup m) ∨ (x << MinSUP.sup m)
                    ms00 {x} ux = MinSUP.x≤sup m ?
                    ms01 : {sup2 : Ordinal} → odef A sup2 → ({x : Ordinal} →
                        odef (UnionCF A f mf ay supf1 z) x → (x ≡ sup2) ∨ (x << sup2)) → MinSUP.sup m o≤ sup2
                    ms01 {sup2} us P = MinSUP.minsup m us ?


          zc41 | (case1 x<sp ) = record { supf = supf0 ; sup=u = ? ; asupf = ZChain.asupf zc ; supf-mono = ZChain.supf-mono zc 
              ; supfmax = ? ; minsup = ? ; supf-is-minsup = ? ; cfcs = cfcs    }  where

                 --  supf0 px not is included by the chain
                 --     supf1 x ≡ supf0 px because of supfmax

                 cfcs : (mf< : <-monotonic-f A f) {a b w : Ordinal } 
                     → a o< b → b o≤ x →  supf0 a o< b → FClosure A f (supf0 a) w → odef (UnionCF A f mf ay supf0 b) w
                 cfcs mf< {a} {b} {w} a<b b≤x sa<b fc with trio< b px 
                 ... | tri< a ¬b ¬c = ZChain.cfcs zc mf< a<b (o<→≤ a) sa<b fc
                 ... | tri≈ ¬a refl ¬c = ZChain.cfcs zc mf< a<b o≤-refl sa<b fc 
                 ... | tri> ¬a ¬b px<b = cfcs1 where
                     x=b : x ≡ b
                     x=b with trio< x b
                     ... | tri< a ¬b ¬c = ⊥-elim ( o≤> b≤x a )
                     ... | tri≈ ¬a b ¬c = b
                     ... | tri> ¬a ¬b c = ⊥-elim (  ¬p<x<op ⟪ px<b , zc-b<x _ c ⟫  )  -- px o< b o< x
                     --  a o< x, supf a o< x
                     --      a o< px , supf a o< px → odef U w
                     --      a ≡ px         -- supf0 px o< x → odef U w
                     --      supf a ≡ px    -- a o< px → odef U w
                     --                        a ≡ px → supf px ≡ px → odef U w

                     cfcs0 : a ≡ px → odef (UnionCF A f mf ay supf0 b) w
                     cfcs0 a=px = ⟪  A∋fc {A} _ f mf fc , ch-is-sup (supf0 px) spx<b cp fc1 ⟫ where
                         spx<b : supf0 px o< b
                         spx<b = subst (λ k → supf0 k o< b) a=px sa<b
                         cs01 : supf0 a ≡ supf0 (supf0 px)
                         cs01 = trans (cong supf0 a=px) ( sym ( ZChain.supf-idem zc mf< o≤-refl 
                              (subst (λ k → supf0 px o< k ) (sym (Oprev.oprev=x op)) (ordtrans<-≤ spx<b b≤x))))
                         fc1 : FClosure A f (supf0 (supf0 px)) w
                         fc1 = subst (λ k → FClosure A f k w) cs01 fc
                         m : MinSUP A (UnionCF A f mf ay supf0 (supf0 px))
                         m = ZChain.minsup zc (zc-b<x _ (ordtrans<-≤ spx<b b≤x))
                         m=sa : MinSUP.sup m ≡ supf0 (supf0 px)
                         m=sa = begin 
                                MinSUP.sup m ≡⟨ sym ( ZChain.supf-is-minsup zc (zc-b<x _ (ordtrans<-≤ spx<b b≤x) ))  ⟩ 
                                supf0 (supf0 px)  ∎  where open ≡-Reasoning 
                         cp : ChainP A f mf ay supf0 (supf0 px)
                         cp = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ ( z << k ) ) m=sa (MinSUP.x≤sup m ⟪ A∋fc _ f mf fc , ch-init fc ⟫ )
                            ; order = order 
                            ; supu=u = sym (trans (cong supf0 (sym a=px)) cs01) } where
                             uz : {s z1 : Ordinal } → supf0 s o< supf0 (supf0 px) → FClosure A f (supf0 s) z1 
                                 → odef (UnionCF A f mf ay supf0 (supf0 px)) z1
                             uz {s} {z1} ss<sp fc = ZChain.cfcs zc mf< s<spx spx≤px 
                                         (subst (λ k → supf0 s o< k) (sym (trans (cong supf0 (sym a=px)) cs01) ) ss<sp) fc where
                                 s<spx : s o< supf0 px
                                 s<spx = ZChain.supf-inject zc ss<sp 
                                 spx≤px : supf0 px o≤ px
                                 spx≤px = zc-b<x _ (ordtrans<-≤ spx<b b≤x)
                             order : {s : Ordinal} {z1 : Ordinal} → supf0 s o< supf0 (supf0 px) →
                                    FClosure A f (supf0 s) z1 → (z1 ≡ supf0 (supf0 px)) ∨ (z1 << supf0 (supf0 px))
                             order {s} {z} s<u fc  = subst (λ k → (z ≡ k) ∨ ( z << k ) ) m=sa (MinSUP.x≤sup m (uz s<u fc) )


                     cfcs1 : odef (UnionCF A f mf ay supf0 b) w
                     cfcs1 with trio< a px
                     ... | tri< a<px ¬b ¬c = cfcs2 where
                         sa<x : supf0 a o< x
                         sa<x = ordtrans<-≤ sa<b b≤x
                         cfcs2 : odef (UnionCF A f mf ay supf0 b) w
                         cfcs2 with trio< (supf0 a) px
                         ... | tri< sa<x ¬b ¬c = chain-mono f mf ay (ZChain.supf zc) (ZChain.supf-mono zc) (o<→≤ px<b) 
                             ( ZChain.cfcs zc mf< a<px o≤-refl sa<x fc ) 
                         ... | tri> ¬a ¬b c = ⊥-elim ( ¬p<x<op ⟪ c , (zc-b<x _ sa<x)  ⟫ )
                         ... | tri≈ ¬a sa=px ¬c with trio< a px
                         ... | tri< a<px ¬b ¬c = ⟪  A∋fc {A} _ f mf fc , ch-is-sup (supf0 a) sa<b cp fc1 ⟫ where
                              cs01 : supf0 a ≡ supf0 (supf0 a)
                              cs01 =  sym ( ZChain.supf-idem zc mf< (zc-b<x _ (ordtrans<-≤ a<b b≤x)) (zc-b<x _ (ordtrans<-≤ sa<b b≤x)))  
                              fc1 : FClosure A f (supf0 (supf0 a)) w
                              fc1 = subst (λ k → FClosure A f k w) cs01 fc
                              m : MinSUP A (UnionCF A f mf ay supf0 (supf0 a))
                              m = ZChain.minsup zc (o≤-refl0 sa=px)
                              m=sa : MinSUP.sup m ≡ supf0 (supf0 a)
                              m=sa = begin 
                                    MinSUP.sup m ≡⟨ sym ( ZChain.supf-is-minsup zc (o≤-refl0 sa=px) )  ⟩ 
                                    supf0 (supf0 a)  ∎  where open ≡-Reasoning 
                              cp : ChainP A f mf ay supf0 (supf0 a)
                              cp = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ ( z << k ) ) m=sa (MinSUP.x≤sup m ⟪  A∋fc _ f mf fc  , ch-init fc ⟫ )  
                                ; order = order 
                                ; supu=u = sym cs01 } where
                                 uz : {s z1 : Ordinal } → supf0 s o< supf0 (supf0 a) → FClosure A f (supf0 s) z1 
                                     → odef (UnionCF A f mf ay supf0 (supf0 a)) z1
                                 uz {s} {z1} ss<sp fc = ZChain.cfcs zc mf< (ZChain.supf-inject zc ss<sp) 
                                         (zc-b<x _ (ordtrans<-≤ sa<b b≤x)) ss<sa fc where
                                    ss<sa : supf0 s o< supf0 a
                                    ss<sa = subst (λ k → supf0 s o< k ) (sym cs01) ss<sp 
                                 order : {s : Ordinal} {z1 : Ordinal} → supf0 s o< supf0 (supf0 a) →
                                        FClosure A f (supf0 s) z1 → (z1 ≡ supf0 (supf0 a)) ∨ (z1 << supf0 (supf0 a))
                                 order {s} {z} s<u fc  = subst (λ k → (z ≡ k) ∨ ( z << k ) ) m=sa (MinSUP.x≤sup m (uz s<u fc) )
                         ... | tri≈ ¬a a=px ¬c = cfcs0 a=px
                         ... | tri> ¬a ¬b c = ⊥-elim ( ¬p<x<op ⟪ c , (zc-b<x _ (ordtrans<-≤ a<b b≤x) )  ⟫ )
                     ... | tri≈ ¬a a=px ¬c = cfcs0 a=px
                     ... | tri> ¬a ¬b c = ⊥-elim ( o≤> (zc-b<x _ (ordtrans<-≤ a<b b≤x)) c )

                 zc17 : {z : Ordinal } → supf0 z o≤ supf0 px
                 zc17 {z} with trio< z px
                 ... | tri< a ¬b ¬c = ZChain.supf-mono zc (o<→≤ a)
                 ... | tri≈ ¬a b ¬c = o≤-refl0 (cong supf0 b)
                 ... | tri> ¬a ¬b px<z = o≤-refl0 zc177 where
                      zc177 : supf0 z ≡ supf0 px
                      zc177 = ZChain.supfmax zc px<z  -- px o< z, px o< supf0 px

                 zc11 : {z : Ordinal} → odef (UnionCF A f mf ay supf0 x) z → odef pchainpx z
                 zc11 {z} ⟪ az , ch-init fc ⟫ = case1 ⟪ az , ch-init fc ⟫
                 zc11 {z} ⟪ az , ch-is-sup u u<x is-sup fc ⟫ = zc21 fc where
                    u≤px : u o≤ px
                    u≤px = zc-b<x _ u<x
                    zc21 : {z1 : Ordinal } → FClosure A f (supf0 u) z1 → odef pchainpx z1
                    zc21 {z1} (fsuc z2 fc ) with zc21 fc
                    ... | case1 ⟪ ua1 , ch-init fc₁ ⟫ = case1 ⟪ proj2 ( mf _ ua1)  , ch-init (fsuc _ fc₁)  ⟫
                    ... | case1 ⟪ ua1 , ch-is-sup u u<x u1-is-sup fc₁ ⟫ = case1 ⟪ proj2 ( mf _ ua1)  , ch-is-sup u u<x u1-is-sup (fsuc _ fc₁) ⟫
                    ... | case2 fc = case2 (fsuc _ fc)
                    zc21 (init asp refl ) with trio< (supf0 u) (supf0 px) | inspect supf0 u
                    ... | tri< a ¬b ¬c | _ = case1 ⟪ asp , ch-is-sup u u<px is-sup (init asp refl ) ⟫ where
                        u<px :  u o< px
                        u<px =  ZChain.supf-inject zc a
                    ... | tri≈ ¬a b ¬c | _ = case2 (init (subst (λ k → odef A k) b (ZChain.asupf zc) ) (sym  b ))
                    ... | tri> ¬a ¬b c | _ = ⊥-elim ( ¬p<x<op ⟪ ZChain.supf-inject zc c , subst (λ k → u o< k ) (sym (Oprev.oprev=x op)) u<x  ⟫ )

                 record STMP {z : Ordinal} (z≤x : z o≤ x ) : Set (Level.suc n) where
                     field
                         tsup : MinSUP A (UnionCF A f mf ay supf0 z)
                         tsup=sup : supf0 z ≡ MinSUP.sup tsup

                 sup : {z : Ordinal} → (z≤x : z o≤ x ) → STMP z≤x
                 sup {z} z≤x with trio< z px
                 ... | tri< a ¬b ¬c = ? -- jrecord { tsup = ZChain.minsup zc (o<→≤ a)  ; tsup=sup = ZChain.supf-is-minsup zc (o<→≤ a) }
                 ... | tri≈ ¬a b ¬c = ? -- record { tsup = ZChain.minsup zc (o≤-refl0 b)  ; tsup=sup = ZChain.supf-is-minsup zc (o≤-refl0 b) }
                 ... | tri> ¬a ¬b px<z = zc35 where
                     zc30 : z ≡ x
                     zc30 with osuc-≡< z≤x
                     ... | case1 eq = eq
                     ... | case2 z<x = ⊥-elim (¬p<x<op ⟪ px<z , subst (λ k → z o< k ) (sym (Oprev.oprev=x op)) z<x ⟫ )
                     zc32 = ZChain.sup zc o≤-refl
                     zc34 : ¬ (supf0 px ≡ px) → {w : HOD} → UnionCF A f mf ay supf0 z ∋ w → (w ≡ SUP.sup zc32) ∨ (w < SUP.sup zc32)
                     zc34 ne {w} lt = ?
                     zc33 : supf0 z ≡ & (SUP.sup zc32)
                     zc33 = ? -- trans (sym (supfx (o≤-refl0 (sym zc30)))) ( ZChain.supf-is-minsup zc o≤-refl  )
                     zc36 : ¬ (supf0 px ≡ px) → STMP z≤x
                     zc36 ne = ? -- record { tsup = record { sup = SUP.sup zc32 ; as = SUP.as zc32 ; x≤sup = zc34 ne } ; tsup=sup = zc33  }
                     zc35 : STMP z≤x
                     zc35 with trio< (supf0 px) px
                     ... | tri< a ¬b ¬c = zc36 ¬b
                     ... | tri> ¬a ¬b c = zc36 ¬b
                     ... | tri≈ ¬a b ¬c = record { tsup = ? ; tsup=sup = ?  } where
                          zc37 : MinSUP A (UnionCF A f mf ay supf0 z)
                          zc37 = record { sup = ? ; asm = ? ; x≤sup = ? }
                 sup=u : {b : Ordinal} (ab : odef A b) →
                    b o≤ x → IsMinSUP A (UnionCF A f mf ay supf0 b) supf0 ab ∧ (¬ HasPrev A (UnionCF A f mf ay supf0 b) f b ) → supf0 b ≡ b
                 sup=u {b} ab b≤x is-sup with trio< b px
                 ... | tri< a ¬b ¬c = ZChain.sup=u zc ab (o<→≤ a) ⟪ record { x≤sup = λ lt → IsMinSUP.x≤sup (proj1 is-sup) lt } , proj2 is-sup ⟫
                 ... | tri≈ ¬a b ¬c = ZChain.sup=u zc ab (o≤-refl0 b) ⟪ record { x≤sup = λ lt → IsMinSUP.x≤sup (proj1 is-sup) lt } , proj2 is-sup ⟫
                 ... | tri> ¬a ¬b px<b = zc31 ? where
                     zc30 : x ≡ b
                     zc30 with osuc-≡< b≤x
                     ... | case1 eq = sym (eq)
                     ... | case2 b<x = ⊥-elim (¬p<x<op ⟪ px<b , subst (λ k → b o< k ) (sym (Oprev.oprev=x op)) b<x ⟫ )
                     --  x o< sp      supf0 b ≡ supf0 x o≤ supf0 sp
                     --      supf0 sp ≡ sp (?)
                     zcsup : xSUP (UnionCF A f mf ay supf0 px) supf0 x
                     zcsup with zc30
                     ... | refl = record { ax = ab ; is-sup = record { x≤sup = λ {w} lt →
                        IsMinSUP.x≤sup (proj1 is-sup) ? ; minsup = ? } }
                     zc31 : ( (¬ xSUP (UnionCF A f mf ay supf0 px) supf0 x ) ∨  HasPrev A (UnionCF A f mf ay supf0 px) f x ) → supf0 b ≡ b
                     zc31 (case1 ¬sp=x) with zc30
                     ... | refl = ⊥-elim (¬sp=x zcsup )
                     zc31 (case2 hasPrev ) with zc30
                     ... | refl = ⊥-elim ( proj2 is-sup record { ax = HasPrev.ax hasPrev ; y = HasPrev.y hasPrev
                                ; ay = ? ; x=fy = HasPrev.x=fy hasPrev } )

     ... | no lim = record { supf = supf1 ; sup=u = ? ; asupf = ? ; supf-mono = supf-mono 
              ; supfmax = ? ; minsup = ? ; supf-is-minsup = ? ; cfcs = cfcs    }  where

          pzc : {z : Ordinal} → z o< x → ZChain A f mf ay z
          pzc {z} z<x = prev z z<x

          ysp =  MinSUP.sup (ysup f mf ay)

          supfz : {z : Ordinal } → z o< x → Ordinal
          supfz {z} z<x = ZChain.supf (pzc  (ob<x lim z<x)) z

          pchainx : HOD
          pchainx = record { od = record { def = λ z → IChain A f supfz z } ; odmax = & A ; <odmax = zc00 } where
               zc00 : {z : Ordinal } → IChain A f supfz z → z o< & A
               zc00 {z} ic = z09 ( A∋fc (supfz (IChain.i<x ic)) f mf (IChain.fc ic) )

          aic : {z : Ordinal } → IChain A f supfz z → odef A z
          aic {z} ic =  A∋fc _ f mf (IChain.fc ic )

          zeq : {xa xb z : Ordinal } 
             → (xa<x : xa o< x) → (xb<x : xb o< x) → xa o≤ xb → z o≤ xa 
             → ZChain.supf (pzc  xa<x) z ≡  ZChain.supf (pzc  xb<x) z  
          zeq {xa} {xb} {z} xa<x xb<x xa≤xb z≤xa =  supf-unique A f mf ay xa≤xb  
              (pzc xa<x)  (pzc xb<x)  z≤xa

          iceq : {ix iy : Ordinal } → ix ≡ iy → {i<x : ix o< x } {i<y : iy o< x } → supfz i<x ≡ supfz i<y 
          iceq refl = cong supfz  o<-irr 

          ifc≤ : {za zb : Ordinal } ( ia : IChain A f supfz za ) ( ib : IChain A f supfz zb ) → (ia≤ib : IChain.i ia o≤ IChain.i ib )
              → FClosure A f (ZChain.supf (pzc (ob<x lim (IChain.i<x ib))) (IChain.i ia)) za
          ifc≤ {za} {zb} ia ib ia≤ib = subst (λ k → FClosure A f k _ ) (zeq _ _ (osucc ia≤ib) (o<→≤ <-osuc) )   (IChain.fc ia)

          ptotalx : IsTotalOrderSet pchainx
          ptotalx {a} {b} ia ib = subst₂ (λ j k → Tri (j < k) (j ≡ k) (k < j)) *iso *iso uz01 where
               uz01 : Tri (* (& a) < * (& b)) (* (& a) ≡ * (& b)) (* (& b) < * (& a) )
               uz01 with trio< (IChain.i ia)  (IChain.i ib)
               ... | tri< ia<ib ¬b ¬c with 
                    <=-trans (ZChain.supf-<= (pzc (ob<x lim (IChain.i<x ib))) ia<ib (ifc≤ ia ib (o<→≤ ia<ib))) (≤to<= (s≤fc _ f mf (IChain.fc ib)))
               ... | case1 eq1 = tri≈ (λ lt → ⊥-elim (<-irr (case1 (sym ct00)) lt)) ct00  (λ lt → ⊥-elim (<-irr (case1 ct00) lt)) where
                   ct00 : * (& a) ≡ * (& b)
                   ct00 = cong (*) eq1
               ... | case2 lt = tri< lt  (λ eq → <-irr (case1 (sym eq)) lt) (λ lt1 → <-irr (case2 lt) lt1)  
               uz01 | tri≈ ¬a ia=ib ¬c = fcn-cmp _ f mf (IChain.fc ia) (subst (λ k → FClosure A f k (& b)) (sym (iceq ia=ib)) (IChain.fc ib)) 
               uz01 | tri> ¬a ¬b ib<ia  with
                    <=-trans (ZChain.supf-<= (pzc (ob<x lim (IChain.i<x ia))) ib<ia (ifc≤ ib ia (o<→≤ ib<ia))) (≤to<= (s≤fc _ f mf (IChain.fc ia)))
               ... | case1 eq1 = tri≈ (λ lt → ⊥-elim (<-irr (case1 (sym ct00)) lt)) ct00  (λ lt → ⊥-elim (<-irr (case1 ct00) lt)) where
                   ct00 : * (& a) ≡ * (& b)
                   ct00 = sym (cong (*) eq1)
               ... | case2 lt = tri> (λ lt1 → <-irr (case2 lt) lt1)    (λ eq → <-irr (case1 eq) lt) lt

          usup : MinSUP A pchainx
          usup = minsupP pchainx (λ ic → A∋fc _ f mf (IChain.fc ic ) ) ptotalx
          spu = MinSUP.sup usup

          supf1 : Ordinal → Ordinal
          supf1 z with trio< z x
          ... | tri< a ¬b ¬c = ZChain.supf (pzc  (ob<x lim a)) z  
          ... | tri≈ ¬a b ¬c = spu
          ... | tri> ¬a ¬b c = spu

          pchain : HOD
          pchain = UnionCF A f mf ay supf1 x

          -- pchain ⊆ pchainx

          ptotal : IsTotalOrderSet pchain
          ptotal {a} {b} ca cb = subst₂ (λ j k → Tri (j < k) (j ≡ k) (k < j)) *iso *iso uz01 where
               uz01 : Tri (* (& a) < * (& b)) (* (& a) ≡ * (& b)) (* (& b) < * (& a) )
               uz01 = chain-total A f mf ay supf1 ( (proj2 ca)) ( (proj2 cb))

          sf1=sf : {z : Ordinal } → (a : z o< x ) → supf1 z ≡ ZChain.supf (pzc  (ob<x lim a)) z  
          sf1=sf {z} z<x with trio< z x
          ... | tri< a ¬b ¬c = cong ( λ k → ZChain.supf (pzc (ob<x lim k)) z) o<-irr
          ... | tri≈ ¬a b ¬c = ⊥-elim (¬a z<x)
          ... | tri> ¬a ¬b c = ⊥-elim (¬a z<x)

          sf1=spu : {z : Ordinal } → (a : x o≤ z ) → supf1 z ≡ spu
          sf1=spu {z} x≤z with trio< z x
          ... | tri< a ¬b ¬c = ⊥-elim (o≤> x≤z a)
          ... | tri≈ ¬a b ¬c = refl
          ... | tri> ¬a ¬b c = refl

          zc11 : {z : Ordinal } → odef pchain z → odef pchainx z
          zc11 {z} lt = ?

          sfpx<=spu : {z : Ordinal } → supf1 z <= spu
          sfpx<=spu {z} with trio< z x
          ... | tri< a ¬b ¬c = MinSUP.x≤sup usup ? -- (init (ZChain.asupf (pzc  (ob<x lim a)) ) refl )
          ... | tri≈ ¬a b ¬c = case1 refl
          ... | tri> ¬a ¬b c = case1 refl

          sfpx≤spu : {z : Ordinal } → supf1 z o≤ spu
          sfpx≤spu {z} with trio< z x
          ... | tri< a ¬b ¬c = subst ( λ k → k o≤ spu) ?
                    ( MinSUP.minsup (ZChain.minsup ? o≤-refl) ? (λ {x} ux → MinSUP.x≤sup ? ?) )
          ... | tri≈ ¬a b ¬c = ?
          ... | tri> ¬a ¬b c = ?

          supf-mono : {x y : Ordinal } → x o≤  y → supf1 x o≤ supf1 y
          supf-mono {x} {y} x≤y with trio< y x
          ... | tri< a ¬b ¬c = ?
          ... | tri≈ ¬a b ¬c = ?
          ... | tri> ¬a ¬b c = ?

          cfcs : (mf< : <-monotonic-f A f) {a b w : Ordinal } 
                 → a o< b → b o≤ x →  supf1 a o< b → FClosure A f (supf1 a) w → odef (UnionCF A f mf ay supf1 b) w
          cfcs mf< {a} {b} {w} a<b b≤x sa<b fc with osuc-≡< b≤x
          ... | case1 b=x with trio< a x 
          ... | tri< a<x ¬b ¬c = zc40 where
               sa = ZChain.supf (pzc  (ob<x lim a<x)) a
               m =  omax a sa     -- x is limit ordinal, so we have sa o< m o< x
               m<x : m o< x
               m<x with trio< a sa | inspect (omax a) sa
               ... | tri< a<sa ¬b ¬c | record { eq = eq } = ob<x lim (ordtrans<-≤ sa<b b≤x )
               ... | tri≈ ¬a a=sa ¬c | record { eq = eq } = subst (λ k → k o< x) eq zc41 where
                   zc41 : omax a sa o< x
                   zc41 = osucprev ( begin
                       osuc ( omax a sa ) ≡⟨ cong (λ k → osuc (omax a k)) (sym a=sa) ⟩
                       osuc ( omax a a ) ≡⟨ cong osuc (omxx _) ⟩
                       osuc ( osuc  a ) ≤⟨ o<→≤ (ob<x lim (ob<x lim a<x))  ⟩
                       x ∎ ) where open o≤-Reasoning O
               ... | tri> ¬a ¬b c | record { eq = eq } = ob<x lim a<x
               sam = ZChain.supf (pzc (ob<x lim m<x)) a 
               zc42 : osuc a o≤ osuc m
               zc42 = osucc (o<→≤ ( omax-x _ _ ) )
               sam<m : sam o< m
               sam<m = subst (λ k → k o< m ) (supf-unique A f mf ay zc42 (pzc (ob<x lim a<x)) (pzc (ob<x lim m<x)) (o<→≤ <-osuc)) ( omax-y _ _ )
               fcm : FClosure A f (ZChain.supf (pzc (ob<x lim m<x)) a) w
               fcm = subst (λ k → FClosure A f k w ) (zeq (ob<x lim a<x) (ob<x lim m<x) zc42 (o<→≤ <-osuc) ) fc
               zcm : odef (UnionCF A f mf ay (ZChain.supf (pzc  (ob<x lim m<x))) (osuc (omax a sa))) w
               zcm = ZChain.cfcs (pzc  (ob<x lim m<x)) mf< (o<→≤ (omax-x _ _)) o≤-refl (o<→≤ sam<m) fcm
               zc40 : odef (UnionCF A f mf ay supf1 b) w
               zc40 with ZChain.cfcs (pzc  (ob<x lim m<x)) mf< (o<→≤ (omax-x _ _)) o≤-refl (o<→≤ sam<m) fcm
               ... | ⟪ az , ch-init fc ⟫ = ⟪ az , ch-init fc ⟫ 
               ... | ⟪ az , ch-is-sup u u<x is-sup fc1 ⟫ = ⟪ az , ch-is-sup u u<b cp fc2 ⟫  where 
                   zc55 : u o< osuc m
                   zc55 = u<x
                   u<b : u o< b 
                   u<b = subst (λ k → u o< k ) (sym b=x) ( ordtrans u<x (ob<x lim m<x))
                   fc1m : FClosure A f (ZChain.supf (pzc (ob<x lim m<x)) u) w
                   fc1m = fc1
                   fc1a : FClosure A f (ZChain.supf (pzc (ob<x lim a<x)) a) w
                   fc1a = fc
                   fc2 : FClosure A f (supf1 u) w
                   fc2 = subst (λ k → FClosure A f k w) (trans (sym (zeq _ _ zc57 (o<→≤ <-osuc))) (sym (sf1=sf (ordtrans≤-< u<x m<x))) )  fc1 where
                       zc57 : osuc u o≤ osuc m 
                       zc57 = osucc u<x
                   sb=sa : {a : Ordinal } → a o≤ m → supf1 a ≡ ZChain.supf (pzc (ob<x lim m<x)) a 
                   sb=sa {a} a≤m = trans (sf1=sf (ordtrans≤-< a≤m m<x)) (zeq _ _ (osucc a≤m) (o<→≤ <-osuc))
                   cp : ChainP A f mf ay supf1 u
                   cp = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sym (sb=sa u<x)) (ChainP.fcy<sup is-sup fc )  
                        ; order = order 
                        ; supu=u = trans (sb=sa u<x ) (ChainP.supu=u is-sup)  } where
                         order : {s : Ordinal} {z1 : Ordinal} → supf1 s o< supf1 u →
                                FClosure A f (supf1 s) z1 → (z1 ≡ supf1 u) ∨ (z1 << supf1 u)
                         order {s} {z} s<u fc  = subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sym (sb=sa u<x)) 
                           (ChainP.order is-sup (subst₂ (λ j k → j o< k ) (sb=sa s≤m) (sb=sa u<x) s<u)  
                              (subst (λ k → FClosure A f k z) (sb=sa s≤m ) fc )) where
                              s≤m : s o≤ m 
                              s≤m = ordtrans (supf-inject0 supf-mono  s<u ) u<x
          ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a (ordtrans<-≤ a<b b≤x))
          ... | tri> ¬a ¬b c = ⊥-elim ( ¬a (ordtrans<-≤ a<b b≤x))
          cfcs mf< {a} {b} {w} a<b b≤x sa<b fc | case2 b<x = zc40 where
               supfb =  ZChain.supf (pzc (ob<x lim b<x)) 
               sb=sa : {a : Ordinal } → a o< b → supf1 a ≡ ZChain.supf (pzc (ob<x lim b<x)) a 
               sb=sa {a} a<b = trans (sf1=sf (ordtrans<-≤ a<b b≤x)) (zeq _ _ (o<→≤ (osucc a<b)) (o<→≤ <-osuc) )
               fcb : FClosure A f (supfb a) w
               fcb = subst (λ k → FClosure A f k w) (sb=sa a<b) fc 
               --  supfb a o< b assures it is in Union b
               zcb : odef (UnionCF A f mf ay supfb b) w
               zcb = ZChain.cfcs (pzc (ob<x lim b<x)) mf< a<b (o<→≤ <-osuc) (subst (λ k → k o< b) (sb=sa a<b) sa<b) fcb
               zc40 : odef (UnionCF A f mf ay supf1 b) w
               zc40 with zcb
               ... | ⟪ az , ch-init fc ⟫ = ⟪ az , ch-init fc ⟫ 
               ... | ⟪ az , ch-is-sup u u<x is-sup fc1 ⟫ = ⟪ az , ch-is-sup u u<x cp (subst (λ k → FClosure A f k w) (sym (sb=sa u<x)) fc1 ) ⟫ where
                   cp : ChainP A f mf ay supf1 u
                   cp = record { fcy<sup = λ {z} fc → subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sym (sb=sa u<x)) (ChainP.fcy<sup is-sup fc )  
                        ; order = order
                        ; supu=u = trans (sb=sa u<x) (ChainP.supu=u is-sup)  } where
                         order : {s : Ordinal} {z1 : Ordinal} → supf1 s o< supf1 u →
                                FClosure A f (supf1 s) z1 → (z1 ≡ supf1 u) ∨ (z1 << supf1 u)
                         order {s} {z} s<u fc  = subst (λ k → (z ≡ k) ∨ ( z << k ) ) (sym (sb=sa u<x)) 
                           (ChainP.order is-sup (subst₂ (λ j k → j o< k ) (sb=sa s<b) (sb=sa u<x) s<u)  
                              (subst (λ k → FClosure A f k z) (sb=sa s<b ) fc )) where
                              s<b : s o< b 
                              s<b = ordtrans (supf-inject0 supf-mono  s<u ) u<x
     ---
     --- the maximum chain  has fix point of any ≤-monotonic function
     ---

     SZ : ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f ) → {y : Ordinal} (ay : odef A y) → (x : Ordinal) → ZChain A f mf ay x
     SZ f mf {y} ay x = TransFinite {λ z → ZChain A f mf ay z  } (λ x → ind f mf ay x   ) x

     msp0 : ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f ) {x y : Ordinal} (ay : odef A y)
         → (zc : ZChain A f mf ay x )
         → MinSUP A (UnionCF A f mf ay (ZChain.supf zc) x)
     msp0 f mf {x} ay zc = minsupP (UnionCF A f mf ay (ZChain.supf zc) x) (ZChain.chain⊆A zc) (ZChain.f-total zc)

     fixpoint :  ( f : Ordinal → Ordinal ) → (mf : ≤-monotonic-f A f ) (mf< : <-monotonic-f A f )  (zc : ZChain A f mf as0 (& A) )
            → (sp1 : MinSUP A (ZChain.chain zc))
            → f (MinSUP.sup sp1)  ≡ MinSUP.sup sp1
     fixpoint f mf mf< zc sp1 = z14 where
           chain = ZChain.chain zc
           supf = ZChain.supf zc
           sp : Ordinal
           sp = MinSUP.sup sp1
           asp : odef A sp
           asp = MinSUP.asm sp1
           z10 :  {a b : Ordinal } → (ca : odef chain a ) → b o< (& A) → (ab : odef A b )
              →  HasPrev A chain f b  ∨  IsSUP A (UnionCF A f mf as0 (ZChain.supf zc) b) ab
              → * a < * b  → odef chain b
           z10 = ZChain1.is-max (SZ1 f mf mf< as0 zc (& A) o≤-refl )
           z22 : sp o< & A
           z22 = z09 asp
           z12 : odef chain sp
           z12 with o≡? (& s) sp
           ... | yes eq = subst (λ k → odef chain k) eq ( ZChain.chain∋init zc )
           ... | no ne = ZChain1.is-max (SZ1 f mf mf< as0 zc (& A) o≤-refl) {& s} {sp} ( ZChain.chain∋init zc ) (z09 asp) asp (case2 z19 ) z13 where
               z13 :  * (& s) < * sp
               z13 with MinSUP.x≤sup sp1 ( ZChain.chain∋init zc )
               ... | case1 eq = ⊥-elim ( ne eq )
               ... | case2 lt = lt
               z19 : IsSUP A (UnionCF A f mf as0 (ZChain.supf zc) sp) asp
               z19 = record {   x≤sup = z20 }  where
                   z20 : {y : Ordinal} → odef (UnionCF A f mf as0 (ZChain.supf zc) sp) y → (y ≡ sp) ∨ (y << sp)
                   z20 {y} zy with MinSUP.x≤sup sp1
                       (subst (λ k → odef chain k ) (sym &iso) (chain-mono f mf as0 supf (ZChain.supf-mono zc) (o<→≤ z22)  zy ))
                   ... | case1 y=p = case1 (subst (λ k → k ≡ _ ) &iso y=p )
                   ... | case2 y<p = case2 (subst (λ k → * k < _ ) &iso y<p )
           z14 :  f sp ≡ sp
           z14 with ZChain.f-total zc (subst (λ k → odef chain k) (sym &iso)  (ZChain.f-next zc z12 )) (subst (λ k → odef chain k) (sym &iso) z12 )
           ... | tri< a ¬b ¬c = ⊥-elim z16 where
               z16 : ⊥
               z16 with proj1 (mf (( MinSUP.sup sp1)) ( MinSUP.asm sp1 ))
               ... | case1 eq = ⊥-elim (¬b (sym eq) )
               ... | case2 lt = ⊥-elim (¬c lt )
           ... | tri≈ ¬a b ¬c = subst₂ (λ j k → j ≡ k ) &iso &iso ( cong (&) b )
           ... | tri> ¬a ¬b c = ⊥-elim z17 where
               z15 : (f sp ≡ MinSUP.sup sp1) ∨ (* (f sp) < * (MinSUP.sup sp1) )
               z15  = MinSUP.x≤sup sp1 (ZChain.f-next zc z12 )
               z17 : ⊥
               z17 with z15
               ... | case1 eq = ¬b (cong (*) eq)
               ... | case2 lt = ¬a lt

     tri : {n : Level} (u w : Ordinal ) { R : Set n }  → ( u o< w → R ) → ( u ≡  w → R ) → ( w o< u → R ) → R
     tri {_} u w p q r with trio< u w
     ... | tri< a ¬b ¬c = p a
     ... | tri≈ ¬a b ¬c = q b
     ... | tri> ¬a ¬b c = r c

     or : {n m r : Level } {P : Set n } {Q : Set m} {R : Set r}  → P ∨ Q → ( P → R ) → (Q → R ) → R
     or (case1 p) p→r q→r = p→r p
     or (case2 q) p→r q→r = q→r q


     -- ZChain contradicts ¬ Maximal
     --
     -- ZChain forces fix point on any ≤-monotonic function (fixpoint)
     -- ¬ Maximal create cf which is a <-monotonic function by axiom of choice. This contradicts fix point of ZChain
     --

     z04 :  (nmx : ¬ Maximal A ) → (zc : ZChain A (cf nmx) (cf-is-≤-monotonic nmx) as0 (& A)) → ⊥
     z04 nmx zc = <-irr0  {* (cf nmx c)} {* c}
           (subst (λ k → odef A k ) (sym &iso) (proj1 (is-cf nmx (MinSUP.asm  msp1 ))))
           (subst (λ k → odef A k) (sym &iso) (MinSUP.asm msp1) )
           (case1 ( cong (*)( fixpoint (cf nmx) (cf-is-≤-monotonic nmx ) (cf-is-<-monotonic nmx ) zc msp1  ))) -- x ≡ f x ̄
                (proj1 (cf-is-<-monotonic nmx c (MinSUP.asm msp1 ))) where          -- x < f x

          supf = ZChain.supf zc
          msp1 : MinSUP A (ZChain.chain zc)
          msp1 = msp0 (cf nmx) (cf-is-≤-monotonic nmx) as0 zc
          c : Ordinal
          c = MinSUP.sup msp1

     zorn00 : Maximal A
     zorn00 with is-o∅ ( & HasMaximal )  -- we have no Level (suc n) LEM
     ... | no not = record { maximal = ODC.minimal O HasMaximal  (λ eq → not (=od∅→≡o∅ eq)) ; as = zorn01 ; ¬maximal<x  = zorn02 } where
         -- yes we have the maximal
         zorn03 :  odef HasMaximal ( & ( ODC.minimal O HasMaximal  (λ eq → not (=od∅→≡o∅ eq)) ) )
         zorn03 =  ODC.x∋minimal  O HasMaximal  (λ eq → not (=od∅→≡o∅ eq))   -- Axiom of choice
         zorn01 :  A ∋ ODC.minimal O HasMaximal (λ eq → not (=od∅→≡o∅ eq))
         zorn01  = proj1  zorn03
         zorn02 : {x : HOD} → A ∋ x → ¬ (ODC.minimal O HasMaximal (λ eq → not (=od∅→≡o∅ eq)) < x)
         zorn02 {x} ax m<x = proj2 zorn03 (& x) ax (subst₂ (λ j k → j < k) (sym *iso) (sym *iso) m<x )
     ... | yes ¬Maximal = ⊥-elim ( z04 nmx (SZ (cf nmx) (cf-is-≤-monotonic nmx) as0 (& A) )) where
         -- if we have no maximal, make ZChain, which contradict SUP condition
         nmx : ¬ Maximal A
         nmx mx =  ∅< {HasMaximal} zc5 ( ≡o∅→=od∅  ¬Maximal ) where
              zc5 : odef A (& (Maximal.maximal mx)) ∧ (( y : Ordinal ) →  odef A y → ¬ (* (& (Maximal.maximal mx)) < * y))
              zc5 = ⟪  Maximal.as mx , (λ y ay mx<y → Maximal.¬maximal<x mx (subst (λ k → odef A k ) (sym &iso) ay) (subst (λ k → k < * y) *iso mx<y) ) ⟫

-- usage (see filter.agda )
--
-- _⊆'_ : ( A B : HOD ) → Set n
-- _⊆'_ A B = (x : Ordinal ) → odef A x → odef B x

-- MaximumSubset : {L P : HOD}
--        → o∅ o< & L →  o∅ o< & P → P ⊆ L
--        → IsPartialOrderSet P _⊆'_
--        → ( (B : HOD) → B ⊆ P → IsTotalOrderSet B _⊆'_ → SUP P B _⊆'_ )
--        → Maximal P (_⊆'_)
-- MaximumSubset {L} {P} 0<L 0<P P⊆L PO SP  = Zorn-lemma {P} {_⊆'_} 0<P PO SP