view src/zorn.agda @ 522:8e36b5c35777

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 17 Apr 2022 19:58:58 +0900
parents 5d3df69d3732
children f351c183e712
line wrap: on
line source

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

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

open import Relation.Nullary 
open import Relation.Binary 
open import Data.Empty 
open import Relation.Binary
open import Relation.Binary.Core
open import Relation.Binary.PropositionalEquality
import BAlgbra 


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

record Element (A : HOD) : Set (Level.suc n) where
    field
       elm : HOD
       is-elm : A ∋ elm

open Element

_<A_ : {A : HOD} → (x y : Element A ) → Set n
x <A y = elm x < elm y
_≡A_ : {A : HOD} → (x y : Element A ) → Set (Level.suc n)
x ≡A y = elm x ≡ elm y

IsPartialOrderSet : ( A : HOD ) → Set (Level.suc n)
IsPartialOrderSet A = IsStrictPartialOrder (_≡A_ {A}) _<A_  

open _==_
open _⊆_

isA : { A B  : HOD } → B ⊆ A → (x : Element B) → Element A
isA B⊆A x = record { elm = elm x ; is-elm = incl B⊆A (is-elm x) }

⊆-IsPartialOrderSet : { A B  : HOD } → B ⊆ A → IsPartialOrderSet A → IsPartialOrderSet B 
⊆-IsPartialOrderSet {A} {B} B⊆A  PA = record {
       isEquivalence = record { refl = refl ; sym = sym ; trans = trans } ;  trans = λ {x} {y} {z} → trans1 {x} {y} {z} 
     ; irrefl = λ {x} {y} → irrefl1 {x} {y} ; <-resp-≈ = resp0 
   } where
   _<B_ : (x y : Element B ) → Set n
   x <B y = elm x < elm y
   trans1 : {x y z : Element B} → x <B y → y <B z → x <B z 
   trans1 {x} {y} {z} x<y y<z  = IsStrictPartialOrder.trans PA {isA B⊆A x} {isA B⊆A y} {isA B⊆A z} x<y y<z 
   irrefl1 : {x y : Element B} → elm x ≡ elm y → ¬ ( x <B y  )
   irrefl1 {x} {y} x=y x<y = IsStrictPartialOrder.irrefl PA {isA B⊆A x} {isA B⊆A y} x=y x<y 
   open import Data.Product
   resp0 : ({x y z : Element B} → elm y ≡ elm z → x <B y → x <B z) × ({x y z : Element B} → elm y ≡ elm z → y <B x → z <B x) 
   resp0 = Data.Product._,_  (λ {x} {y} {z} → proj₁ (IsStrictPartialOrder.<-resp-≈ PA) {isA B⊆A x } {isA B⊆A y }{isA B⊆A z }) 
                             (λ {x} {y} {z} → proj₂ (IsStrictPartialOrder.<-resp-≈ PA) {isA B⊆A x } {isA B⊆A y }{isA B⊆A z })

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

IsTotalOrderSet : ( A : HOD ) →  Set (Level.suc n)
IsTotalOrderSet A = IsStrictTotalOrder  (_≡A_ {A}) _<A_ 

me : { A a : HOD } → A ∋ a → Element A
me {A} {a} lt = record { elm = a ; is-elm = lt }

A∋x-irr : (A : HOD) {x y : HOD} → x ≡ y → (A ∋ x) ≡ (A ∋ y )
A∋x-irr A {x} {y} refl = refl

me-elm-refl : (A : HOD) → (x : Element A) → elm (me {A} (d→∋ A (is-elm x))) ≡ elm x
me-elm-refl A record { elm = ex ; is-elm = ax } = *iso 

open import Relation.Binary.HeterogeneousEquality as HE using (_≅_ ) 

-- postulate   -- may be proved by transfinite induction and functional extentionality
--   ∋x-irr : (A : HOD) {x y : HOD} → x ≡ y → (ax : A ∋ x) (ay : A ∋ y ) → ax ≅ ay 
--   odef-irr : (A : OD) {x y : Ordinal} → x ≡ y → (ax : def A x) (ay : def A y ) → ax ≅ ay 

-- is-elm-irr : (A : HOD) → {x y : Element A } → elm x ≡ elm y → is-elm x ≅ is-elm y 
-- is-elm-irr A {x} {y} eq = ∋x-irr A eq (is-elm x) (is-elm y )

El-irr2 : (A : HOD) → {x y : Element A } → elm x ≡ elm y → is-elm x ≅ is-elm y → x ≡ y
El-irr2  A {x} {y} refl HE.refl = refl

-- El-irr : {A : HOD} → {x y : Element A } → elm x ≡ elm y → x ≡ y
-- El-irr {A} {x} {y} eq = El-irr2 A eq ( is-elm-irr A eq ) 

record ZChain ( A : HOD ) (y : Ordinal)   : Set (Level.suc n) where
   field
      max : HOD
      A∋max : A ∋ max
      y<max : y o< & max
      chain : HOD
      chain⊆A : chain ⊆ A  
      total : IsTotalOrderSet chain 
      chain-max : (x : HOD) → chain ∋ x → (x ≡ max ) ∨ ( x < max )

data IChain  (A : HOD) : Ordinal → Set n where
    ifirst : {ox : Ordinal} → odef A ox → IChain A ox
    inext  : {ox oy : Ordinal} → odef A oy → * ox < * oy → IChain A ox → IChain A oy

--   * ox < .. < * oy
ic-connect :  {A : HOD} {oy : Ordinal} → (ox : Ordinal) →  (iy : IChain A oy) → Set n 
ic-connect {A} ox (ifirst {oy} ay) = Lift n ⊥
ic-connect {A} ox (inext {oy} {oz} ay y<z iz) = (ox ≡ oy) ∨ ic-connect ox iz 

ic→odef : {A : HOD} {ox : Ordinal} → IChain A ox → odef A ox
ic→odef {A} {ox} (ifirst ax) = ax
ic→odef {A} {ox} (inext ax x<y ic) = ax

ic→< :  {A : HOD} → IsPartialOrderSet A → (x : Ordinal) → odef A x → {y : Ordinal} → (iy : IChain A y) → ic-connect {A} {y} x iy → * x < * y
ic→< {A} PO x ax {y} (ifirst ay) ()
ic→< {A} PO x ax {y} (inext ay x<y iy) (case1 refl) = x<y
ic→< {A} PO x ax {y} (inext {oy} ay x<y iy) (case2 ic) = IsStrictPartialOrder.trans PO 
     {me (subst (λ k → odef A k) (sym &iso) ax )} {me (subst (λ k → odef A k) (sym &iso) (ic→odef {A} {oy} iy) ) }  {me (subst (λ k → odef A k) (sym &iso) ay) }
    (ic→< {A} PO x ax iy ic ) x<y

record IChained (A : HOD) (x y : Ordinal) : Set n where
   field
      iy : IChain A y
      ic : ic-connect x iy 

IChainSet : {A : HOD} → Element A → HOD
IChainSet {A} ax = record { od = record { def = λ y → odef A y ∧ IChained A (& (elm ax)) y }
    ; odmax = & A ; <odmax = λ {y} lt → subst (λ k → k o< & A) &iso (c<→o< (subst (λ k → odef A k) (sym &iso) (proj1 lt))) } 

IChainSet⊆A :  {A : HOD} → (x : Element A ) → IChainSet x ⊆ A
IChainSet⊆A {A} x = record { incl = λ {oy} y → proj1 y }

¬IChained-refl : (A : HOD) {x : Ordinal} → IsPartialOrderSet A → ¬ IChained A x x
¬IChained-refl A {x} PO record { iy = iy ; ic = ic } = IsStrictPartialOrder.irrefl PO
        {me (subst (λ k → odef A k ) (sym &iso) ic0) } {me (subst (λ k → odef A k ) (sym &iso) ic0) } refl (ic→< {A} PO x ic0 iy ic )  where
     ic0 : odef A x
     ic0 = ic→odef {A} iy

-- there is a y, & y > & x

record OSup> (A : HOD) {x : Ordinal} (ax : A ∋ * x) : Set n where
   field
      y : Ordinal
      icy : odef (IChainSet {A} (me ax)) y 
      y>x : x o< y

record IChainSup> (A : HOD) {x : Ordinal} (ax : A ∋ * x) : Set n where
   field
      y : Ordinal
      A∋y : odef A y
      y>x  : * x < * y

-- finite IChain

ic→A∋y : (A : HOD) {x y : Ordinal}  (ax : A ∋ * x) → odef (IChainSet {A} (me ax)) y → A ∋ * y
ic→A∋y A {x} {y} ax ⟪ ay , _ ⟫ = subst (λ k → odef A k) (sym &iso) ay

record InFiniteIChain (A : HOD) (max : Ordinal) {x : Ordinal}  (ax : A ∋ * x) : Set n where
   field
      chain<x : (y : Ordinal ) → odef (IChainSet {A} (me ax)) y →  y o< max
      c-infinite : (y : Ordinal ) → (cy : odef (IChainSet {A} (me ax)) y  )
          → IChainSup> A (ic→A∋y A ax cy)

open import Data.Nat hiding (_<_) 
import Data.Nat.Properties as NP
open import nat

data Chain (A : HOD) (s : Ordinal) (next : Ordinal  → Ordinal ) : ( x : Ordinal  ) → Set n where
     cfirst : odef A s → Chain A s next s
     csuc : (x : Ordinal ) → (ax : odef A x ) → Chain A s next x → odef A (next x) → Chain A s next (next x )

ct∈A : (A : HOD ) (s : Ordinal)  → (next : Ordinal  → Ordinal ) → {x : Ordinal} → Chain A s next x → odef A x
ct∈A A s next {x} (cfirst x₁) = x₁ 
ct∈A A s next {.(next x )} (csuc x ax t anx) = anx

--
-- extract single chain from countable infinite chains
--
ChainClosure : (A : HOD) (s : Ordinal) →  (next : Ordinal → Ordinal ) → HOD
ChainClosure A s next = record { od = record { def = λ x → Chain A s next x } ; odmax = & A ; <odmax = cc01 } where
    cc01 : {y : Ordinal} → Chain A s next y → y o< & A
    cc01 {y} cy = subst (λ k → k o< & A ) &iso ( c<→o< (subst (λ k → odef A k ) (sym &iso) ( ct∈A A s next cy ) ) )

cton0 : (A : HOD ) (s : Ordinal) → (next : Ordinal  → Ordinal )  {y : Ordinal } → Chain A s next y → ℕ
cton0 A s next (cfirst _)  = zero
cton0 A s next (csuc x ax z _) = suc (cton0 A s next z) 
cton : (A : HOD ) (s : Ordinal)   → (next : Ordinal → Ordinal ) → Element (ChainClosure A s next) → ℕ
cton A s next y = cton0 A s next (is-elm y)

cinext :  (A : HOD) {x max : Ordinal } → (ax : A ∋ * x ) → (ifc : InFiniteIChain A max ax ) → Ordinal  →  Ordinal
cinext A ax ifc y with ODC.∋-p O (IChainSet (me ax)) (* y)
... | yes ics-y = IChainSup>.y ( InFiniteIChain.c-infinite ifc y (subst (λ k → odef (IChainSet (me ax)) k) &iso ics-y ))
... | no _ = o∅

InFCSet : (A : HOD) → {x max : Ordinal}  (ax : A ∋ * x)
     → (ifc : InFiniteIChain A max ax ) → HOD
InFCSet A {x} ax ifc =  ChainClosure (IChainSet {A} (me ax)) x (cinext A ax ifc ) 

InFCSet⊆A : (A : HOD) → {x max : Ordinal}  (ax : A ∋ * x) →  (ifc : InFiniteIChain A max ax ) → InFCSet A ax ifc ⊆ A
InFCSet⊆A A {x} ax ifc = record { incl = λ {y} lt → incl (IChainSet⊆A (me ax)) (
     ct∈A (IChainSet {A} (me ax)) x (cinext A ax ifc) lt ) }

ChainClosure-is-total : (A : HOD) → {x max : Ordinal}  (ax : A ∋ * x)
     → IsPartialOrderSet A 
     → (ifc : InFiniteIChain A max ax )
     → IsTotalOrderSet ( InFCSet A ax ifc )
ChainClosure-is-total A {x} ax PO ifc = record { isEquivalence = IsStrictPartialOrder.isEquivalence IPO
   ; trans = λ {x} {y} {z} → IsStrictPartialOrder.trans IPO {x} {y} {z}  ; compare = cmp } where
    IPO : IsPartialOrderSet (InFCSet A ax ifc )
    IPO = ⊆-IsPartialOrderSet record { incl = λ {y} lt → incl (InFCSet⊆A A {x} ax ifc) lt} PO
    B = IChainSet {A} (me ax)
    cnext = cinext A ax ifc
    ct02 : {oy : Ordinal} → (y : Chain B x cnext oy ) → A ∋ * oy 
    ct02 y = incl (IChainSet⊆A {A} (me ax)) (subst (λ k → odef (IChainSet (me ax)) k) (sym &iso) (ct∈A B x cnext y) ) 
    ct-inject : {ox oy : Ordinal} → (x1 : Chain B x cnext ox ) → (y : Chain B x cnext oy )
       → (cton0 B x cnext x1) ≡ (cton0 B x cnext y) → ox ≡ oy
    ct-inject {ox} {ox} (cfirst x) (cfirst x₁) refl = refl
    ct-inject {.(cnext x₀ )} {.(cnext x₃ )} (csuc x₀ ax x₁ x₂) (csuc x₃ ax₁ y x₄) eq = cong cnext ct05 where
        ct06 : {x y : ℕ} → suc x ≡ suc y → x ≡ y
        ct06 refl = refl 
        ct05 : x₀ ≡ x₃
        ct05 = ct-inject x₁ y (ct06 eq)
    ct-monotonic : {ox oy : Ordinal} → (x1 : Chain B x cnext ox ) → (y : Chain B x cnext oy )
       → (cton0 B x cnext x1) Data.Nat.< (cton0 B x cnext y) → * ox < * oy
    ct-monotonic {ox} {oy} x1 (csuc oy1 ay y _) (s≤s lt) with NP.<-cmp ( cton0 B x cnext x1 ) ( cton0 B x cnext y )
    ... | tri< a ¬b ¬c = ct07 where
        ct07 :  * ox < * (cnext oy1)
        ct07 with ODC.∋-p O (IChainSet {A} (me ax)) (* oy1)
        ... | no not  = ⊥-elim ( not (subst (λ k → odef (IChainSet {A} (me ax)) k ) (sym &iso) ay ) )
        ... | yes ay1 = IsStrictPartialOrder.trans PO {me (ct02 x1) } {me (ct02 y)} {me ct031 } (ct-monotonic x1 y a ) ct011 where
           ct031 :  A ∋ * (IChainSup>.y (InFiniteIChain.c-infinite ifc oy1 (subst (λ k → odef (IChainSet {A} (me ax)) k) &iso ay1 ) )) 
           ct031 = subst (λ k → odef A k ) (sym &iso) (
              IChainSup>.A∋y (InFiniteIChain.c-infinite ifc oy1 (subst (λ k → odef (IChainSet {A} (me ax)) k) &iso ay1 )) )
           ct011 :  * oy1 < * ( IChainSup>.y (InFiniteIChain.c-infinite ifc oy1 (subst (λ k → odef (IChainSet {A} (me ax)) k) &iso ay1 )) )
           ct011 = IChainSup>.y>x (InFiniteIChain.c-infinite ifc oy1 (subst (λ k → odef (IChainSet {A} (me ax)) k) &iso ay1 )) 
    ... | tri≈ ¬a b ¬c = ct11 where
           ct11 : * ox < * (cnext oy1)
           ct11 with ODC.∋-p O (IChainSet {A} (me ax)) (* oy1)
           ... | no not  = ⊥-elim ( not (subst (λ k → odef (IChainSet {A} (me ax)) k ) (sym &iso) ay ) )
           ... | yes ay1 = subst (λ k → * k < _) (sym (ct-inject _ _ b)) ct011  where
              ct011 :  * oy1 < * ( IChainSup>.y (InFiniteIChain.c-infinite ifc oy1 (subst (λ k → odef (IChainSet {A} (me ax)) k) &iso ay1 )) )
              ct011 = IChainSup>.y>x (InFiniteIChain.c-infinite ifc oy1 (subst (λ k → odef (IChainSet {A} (me ax)) k) &iso ay1 )) 
    ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> lt c ) 
    ct12 : {y z : Element (ChainClosure B x cnext) } → elm y ≡ elm z → elm y < elm z → ⊥ 
    ct12 {y} {z} y=z y<z = IsStrictPartialOrder.irrefl IPO {y} {z} y=z y<z
    ct13 : {y z : Element (ChainClosure B x cnext) } → elm y < elm z → elm z < elm y → ⊥ 
    ct13 {y} {z} y<z y>z = IsStrictPartialOrder.irrefl IPO {y} {y} refl ( IsStrictPartialOrder.trans IPO {y} {z} {y} y<z y>z )
    ct17 : (x1 : Element (ChainClosure B x cnext)) → Chain B x cnext (& (elm x1))
    ct17 x1 = is-elm x1
    cmp : Trichotomous _ _ 
    cmp x1 y with NP.<-cmp (cton B x cnext x1) (cton B x cnext y)
    ... | tri< a ¬b ¬c = tri< ct04 ct14 ct15 where
        ct04 : elm x1 < elm y
        ct04 = subst₂ (λ j k → j < k  ) *iso *iso (ct-monotonic (is-elm x1) (is-elm y) a)
        ct14 : ¬ elm x1 ≡  elm y
        ct14 eq = ct12 {x1} {y} eq (subst₂ (λ j k → j < k ) *iso *iso (ct-monotonic (is-elm x1) (is-elm y) a )  )
        ct15 : ¬ (elm y <  elm x1)
        ct15 lt = ct13 {y} {x1} lt (subst₂ (λ j k → j < k ) *iso *iso (ct-monotonic (is-elm x1) (is-elm y) a )  )
    ... | tri≈ ¬a b ¬c = tri≈ (ct12 {x1} {y} ct16)  ct16 (ct12 {y} {x1} (sym ct16)) where
        ct16 :  elm x1 ≡ elm y
        ct16 = subst₂ (λ j k → j ≡ k ) *iso *iso (cong (*) (ct-inject {& (elm x1)} {& (elm y)} (is-elm x1) (is-elm y) b ))
    ... | tri> ¬a ¬b c = tri> ct15 ct14 ct04 where
        ct04 : elm y < elm x1
        ct04 = subst₂ (λ j k → j < k  ) *iso *iso (ct-monotonic (is-elm y) (is-elm x1) c)
        ct14 : ¬ elm x1 ≡  elm y
        ct14 eq = ct12 {y} {x1} (sym eq) (subst₂ (λ j k → j < k ) *iso *iso (ct-monotonic (is-elm y) (is-elm x1) c )  )
        ct15 : ¬ (elm x1 <  elm y)
        ct15 lt = ct13 {x1} {y} lt (subst₂ (λ j k → j < k ) *iso *iso (ct-monotonic (is-elm y) (is-elm x1) c )  )

      
record IsFC (A : HOD) {x : Ordinal}  (ax : A ∋ * x) (y : Ordinal) : Set n where
   field
      icy : odef (IChainSet {A} (me ax)) y  
      c-finite : ¬ IChainSup> A (subst (λ k → odef A k ) (sym &iso) (proj1 icy) )
      
record Maximal ( A : HOD )  : Set (Level.suc n) where
   field
      maximal : HOD
      A∋maximal : A ∋ maximal
      ¬maximal<x : {x : HOD} → A ∋ x  → ¬ maximal < x       -- A is Partial, use negative

--
-- possible three cases in a limit ordinal step
-- 
-- case 1) < goes > x                       (will contradic in the transfinite induction )
-- case 2) no > x in some chain ( maximal )
-- case 3) countably infinite chain below x (will be prohibited by sup condtion )
--
Zorn-lemma-3case : { A : HOD } 
    → o∅ o< & A 
    → IsPartialOrderSet A 
    → (x : Element A) → OSup> A (d→∋ A (is-elm x)) ∨ Maximal A ∨ InFiniteIChain A (& (elm x))  (d→∋ A (is-elm x))
Zorn-lemma-3case {A}  0<A PO x = zc2 where
    Gtx : HOD
    Gtx = record { od = record { def = λ y → odef ( IChainSet x ) y ∧  ( & (elm x) o< y ) } ; odmax = & A
        ; <odmax = λ lt → subst (λ k → k o< & A) &iso (c<→o< (d→∋  A (proj1 (proj1 lt))))  }
    HG : HOD
    HG = record { od = record { def = λ y → odef A y ∧ IsFC A (d→∋ A (is-elm x) ) y } ; odmax = & A
        ; <odmax = λ lt → subst (λ k → k o< & A) &iso (c<→o< (d→∋  A  (proj1 lt) ))  }
    zc2 :  OSup> A (d→∋ A (is-elm x))  ∨ Maximal A ∨ InFiniteIChain A (& (elm x))  (d→∋ A (is-elm x))
    zc2 with  is-o∅ (& Gtx)
    ... | no not = case1 record { y = & y ; icy = zc4 ; y>x = proj2 zc3 } where
        y : HOD
        y =  ODC.minimal O Gtx  (λ eq → not (=od∅→≡o∅ eq))
        zc3 :  odef ( IChainSet x ) (& y) ∧  ( & (elm x) o< (& y ))
        zc3  = ODC.x∋minimal O Gtx  (λ eq → not (=od∅→≡o∅ eq))
        zc4 : odef (IChainSet (me (subst (OD.def (od A)) (sym &iso) (is-elm x)))) (& y)
        zc4 = ⟪ proj1 (proj1 zc3) , subst (λ k → IChained A (& k) (& y) ) (sym *iso) (proj2 (proj1 zc3)) ⟫ 
    ... | yes nogt with is-o∅ (& HG)
    ... | no  finite-chain = case2 (case1 record { maximal = y ; A∋maximal = proj1 zc3 ; ¬maximal<x = zc4 } ) where
        y : HOD
        y =  ODC.minimal O HG (λ eq → finite-chain (=od∅→≡o∅ eq))
        zc3 :  odef A (& y) ∧ IsFC A (d→∋ A (is-elm x) ) (& y)
        zc3  = ODC.x∋minimal O HG  (λ eq → finite-chain (=od∅→≡o∅ eq))
        zc5 : odef (IChainSet {A} (me (d→∋ A (is-elm x) ))) (& y)
        zc5 = IsFC.icy (proj2 zc3)
        zc4 : {z : HOD} → A ∋ z → ¬ (y < z)
        zc4 {z} az y<z = IsFC.c-finite (proj2 zc3) record { y = & z ; A∋y =  az ; y>x = subst₂ (λ j k → j < k ) (sym *iso) (sym *iso) y<z } where
            zc8 : ic-connect (& (* (& (elm x)))) (IChained.iy (proj2 zc5)) 
            zc8 = IChained.ic (proj2 zc5)
            zc7 : elm x < y
            zc7 = subst₂ (λ j k → j < k ) *iso *iso ( ic→< {A} PO (& (elm x)) (is-elm x) (IChained.iy (proj2 zc5))
                (subst (λ k → ic-connect (& k) (IChained.iy (proj2 zc5)) ) (me-elm-refl A x) (IChained.ic (proj2 zc5)) )  )
            zc6 : elm x < z
            zc6 = IsStrictPartialOrder.trans PO {x} {me (proj1 zc3)} {me az} zc7 y<z
    ... | yes inifite = case2 (case2 record {    c-infinite = zc9 ; chain<x = zc10} ) where
        B : HOD
        B = IChainSet {A} (me (subst (OD.def (od A)) (sym &iso) (is-elm x)))
        Nx : (y : Ordinal) → odef A y → HOD
        Nx y ay = record { od = record { def = λ x → odef A x ∧ ( * y < * x ) } ; odmax = & A
              ; <odmax = λ lt → subst (λ k → k o< & A) &iso (c<→o< (d→∋  A (proj1 lt)))  }
        zc10 : (oy : Ordinal) → odef (IChainSet {A} (me (subst (OD.def (od A)) (sym &iso) (is-elm x)))) oy → oy o< & (elm x)
        zc10 oy icsy = zc21 where
             zc20 : (y : HOD) → (IChainSet x) ∋ y  → & (elm x) o< & y → ⊥
             zc20 y icsy lt = ¬A∋x→A≡od∅ Gtx ⟪ icsy , lt ⟫ nogt
             zc22 : IChainSet x ∋ * oy
             zc22 = ⟪ subst (λ k → odef A k) (sym &iso) (proj1 icsy)
                 , subst₂ (λ j k → IChained A j k) (cong (&) (me-elm-refl A x))  (sym &iso) (proj2 icsy)  ⟫
             zc21 : oy o< & (elm x) 
             zc21 with trio< oy (& (elm x) )
             ... | tri< a ¬b ¬c = a
             ... | tri≈ ¬a b ¬c = ⊥-elim (¬IChained-refl A PO (subst₂ (λ j k → IChained A j k ) zc23 b (proj2 icsy)) ) where
                 zc23 : & (* (& (elm x))) ≡ & (elm x)
                 zc23 = cong (&) *iso 
             ... | tri> ¬a ¬b c = ⊥-elim ( zc20 (* oy) zc22 (subst (λ k → & (elm x) o< k) (sym &iso) c ))
        zc9 : (y : Ordinal) (cy : odef B y) →
            IChainSup> A (ic→A∋y A (subst (OD.def (od A)) (sym &iso) (is-elm x)) cy)
        zc9 y cy with is-o∅ (& (Nx y (proj1 cy) ))
        ... | yes no-next = ⊥-elim zc16 where
             zc18 :  ¬ IChainSup> A (subst (odef A) (sym &iso) (proj1 (subst (λ k → odef (IChainSet (me (d→∋ A (is-elm x)))) k) (sym &iso) cy)))
             zc18 ics = ¬A∋x→A≡od∅ (Nx y (proj1 cy) ) ⟪ subst (λ k → odef A k ) (sym &iso) (IChainSup>.A∋y ics)
                  ,  subst₂ (λ j k → j < k ) *iso (cong (*) (sym &iso))( IChainSup>.y>x ics) ⟫ no-next  
             zc17 : IsFC A {& (elm x)} (d→∋ A (is-elm x)) (& (* y))
             zc17 = record { icy = subst (λ k → odef (IChainSet (me (d→∋ A (is-elm x)))) k ) (sym &iso) cy ; c-finite = zc18 }
             zc16 : ⊥
             zc16 = ¬A∋x→A≡od∅ HG ⟪ subst (λ k → odef A k ) (sym &iso) (proj1 cy ) , zc17 ⟫ inifite 
        ... | no not = record { y = & zc13 ; A∋y = proj1 zc12  ; y>x = proj2 zc12 }  where
             zc13 = ODC.minimal O (Nx y (proj1 cy)) (λ eq → not (=od∅→≡o∅ eq ))
             zc12 : odef A (& zc13 ) ∧ ( * y < * ( & zc13 ))
             zc12 = ODC.x∋minimal O (Nx y (proj1 cy)) (λ eq → not (=od∅→≡o∅ eq ))

all-climb-case : { A : HOD } → (0<A : o∅ o< & A) → IsPartialOrderSet A
     → (( x : Element A) → OSup> A (d→∋ A (is-elm x) ))
     → InFiniteIChain A (& A) (d→∋ A (ODC.x∋minimal O A (λ eq → ¬x<0 ( subst (λ k → o∅ o< k ) (=od∅→≡o∅ eq) 0<A )) ))
all-climb-case {A} 0<A PO climb = record {    c-infinite = λ y cy → ac00 (& x) y (proj1 cy) (subst (λ k → IChained A k y  )
         (cong (&) (me-elm-refl A (me ax))) (proj2 cy))
    ; chain<x = ac01 }  where
        x = ODC.minimal O A (λ eq → ¬x<0 (subst (_o<_ o∅) (=od∅→≡o∅ eq) 0<A))
        ax = ODC.x∋minimal O A (λ eq → ¬x<0 (subst (_o<_ o∅) (=od∅→≡o∅ eq) 0<A))
        B = IChainSet {A} (me (d→∋ A ax))
        ac01 : (y : Ordinal) → odef (IChainSet {A} (me (d→∋ A ax))) y → y o< & A
        ac01 y ⟪ ay , _ ⟫ = subst (λ k → k o< & A ) &iso (c<→o< (subst (λ k → odef A k ) (sym &iso) ay) )
        ac00 : (x y : Ordinal) (ay : odef A y) (cy :  IChained A x y) → IChainSup> A (subst (λ k → odef A k ) (sym &iso) ay )
        ac00 x y ay cy = record { y = z ; A∋y = az ; y>x = {!!} } where
             z : Ordinal
             z = OSup>.y ( climb (me (subst (λ k → odef A k ) (sym &iso) ay) ) )
             az : odef A z
             icy :  odef (IChainSet {A} (me (subst (λ k → odef A k ) {!!} ay))) z
             icy  = OSup>.icy ( climb (me (subst (λ k → odef A k ) (sym &iso) ay) ) )
             az = {!!}
             -- incl (IChainSet⊆A {A} ? ) (subst (λ k → odef (IChainSet {A} ? ) k ) ? (OSup>.icy ( climb (me (subst (λ k → odef A k ) (sym &iso) ay) ) )))
             - = OSup>.y ( climb (me (subst (λ k → odef A k ) (sym &iso) ay) ) )
             -- iy0 : IChained A (& (* (& (ODC.minimal O A (λ eq → ¬x<0 (subst (_o<_ o∅) (=od∅→≡o∅ eq) 0<A)))))) ?
             -- iy0 = iy 



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

Zorn-lemma : { A : HOD } 
    → o∅ o< & A 
    → IsPartialOrderSet A 
    → ( ( B : HOD) → (B⊆A : B ⊆ A) → IsTotalOrderSet B → SUP A B   ) -- SUP condition
    → Maximal A 
Zorn-lemma {A}  0<A PO supP = zorn04 where
     z01 : {a b : HOD} → A ∋ a → A ∋ b  → (a ≡ b ) ∨ (a < b ) → b < a → ⊥
     z01 {a} {b} A∋a A∋b (case1 a=b) b<a = IsStrictPartialOrder.irrefl PO {me A∋b} {me A∋a} (sym a=b) b<a
     z01 {a} {b} A∋a A∋b (case2 a<b) b<a = IsStrictPartialOrder.irrefl PO {me A∋b} {me A∋b} refl (IsStrictPartialOrder.trans PO {me A∋b} {me A∋a} {me A∋b}  b<a a<b)
     z02 : {x : Ordinal } → (ax : A ∋ * x ) → InFiniteIChain A x ax  → ⊥
     z02 {x} ax ic = zc5 ic where
              FC : HOD
              FC = IChainSet {A} (me ax)
              zc6 :  InFiniteIChain A x ax  → ¬ SUP A FC 
              zc6 inf = {!!}
              FC-is-total : IsTotalOrderSet FC
              FC-is-total = {!!}
              FC⊆A :  FC ⊆ A
              FC⊆A = record { incl = λ {x} lt → proj1 lt }
              zc5 : InFiniteIChain A x ax → ⊥
              zc5 x = zc6 x ( supP FC FC⊆A FC-is-total )
     -- ZChain is not compatible with the SUP condition
     ind : (x : Ordinal) → ((y : Ordinal) → y o< x →  ZChain A y ∨ Maximal A )
         →  ZChain A x ∨ Maximal A 
     ind x prev with Oprev-p x
     ... | yes op with ODC.∋-p O A (* x)
     ... | no ¬Ax = zc1 where
          -- we have previous ordinal and ¬ A ∋ x, use previous Zchain
          px = Oprev.oprev op
          zc1 : ZChain A x ∨ Maximal A
          zc1 with prev px (subst (λ k → px o< k) (Oprev.oprev=x op) <-osuc) 
          ... | case2 x = case2 x  -- we have the Maximal
          ... | case1 z with trio< x (& (ZChain.max z))
          ... | tri< a ¬b ¬c = case1 record { max = ZChain.max z ; y<max = a }
          ... | tri≈ ¬a b ¬c = {!!} -- x = max so ¬ A ∋ max 
          ... | tri> ¬a ¬b c = {!!} -- can't happen
     ... | yes ax = zc4 where -- we have previous ordinal and A ∋ x
          px = Oprev.oprev op
          zc1 : OSup> A (subst (OD.def (od A)) (sym &iso) ax) → ZChain A x ∨ Maximal A
          zc1 os with  prev px (subst (λ k → px o< k) (Oprev.oprev=x op) <-osuc) 
          ... | case2 x = case2 x
          ... | case1 x = {!!}
          zc4 : ZChain A x ∨ Maximal A
          zc4 with Zorn-lemma-3case 0<A PO (me ax)
          ... | case1 y>x = zc1 y>x
          ... | case2 (case1 x) = case2 x
          ... | case2 (case2 ex) = ⊥-elim (zc5 {!!} ) where
              FC : HOD
              FC = IChainSet {A} (me ax)
              B : InFiniteIChain A x ax  → HOD
              B ifc =  InFCSet A ax ifc
              zc6 :  (ifc : InFiniteIChain A x ax ) → ¬ SUP A (B ifc)
              zc6 = {!!}
              FC-is-total : (ifc : InFiniteIChain A x ax)  → IsTotalOrderSet (B ifc)
              FC-is-total ifc = ChainClosure-is-total A ax PO ifc 
              B⊆A : (ifc : InFiniteIChain A x ax)  →   B ifc ⊆ A
              B⊆A = {!!}
              ifc : InFiniteIChain A x (subst (OD.def (od A)) (sym &iso) ax) → InFiniteIChain A x ax
              ifc record {  c-infinite = c-infinite } = record {  c-infinite = {!!} }  where
                  ifc01 : {!!} -- me (subst (OD.def (od A)) (sym &iso) ax)
                  ifc01 = {!!}
               -- (y : Ordinal) → odef (IChainSet (me (subst (OD.def (od A)) (sym &iso) ax))) y → y o< & (* x₁)
               --  (y : Ordinal) → odef (IChainSet (me ax)) y → y o< x₁
              zc5 : InFiniteIChain A x (subst (OD.def (od A)) (sym &iso) ax) → ⊥
              zc5 x = zc6 (ifc x) ( supP (B (ifc x)) (B⊆A (ifc x)) (FC-is-total (ifc x) ))
     ind x prev | no ¬ox with trio< (& A) x   --- limit ordinal case
     ... | tri< a ¬b ¬c = {!!} where
          zc1 : ZChain A (& A) 
          zc1 with prev (& A) a 
          ... | t = {!!}
     ... | tri≈ ¬a b ¬c = {!!} where
     ... | tri> ¬a ¬b c with ODC.∋-p O A (* x)
     ... | no ¬Ax = {!!} where
     ... | yes ax = {!!}
     zorn03 : (x : Ordinal) → ZChain A x  ∨ Maximal A 
     zorn03 x = TransFinite ind  x
     zorn04 : Maximal A 
     zorn04 with zorn03 (& A)
     ... | case1 chain = ⊥-elim ( o<> (c<→o< {ZChain.max chain} {A} (ZChain.A∋max chain))  (ZChain.y<max chain) )
     ... | case2 m = m

-- 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