comparison automaton-in-agda/src/finiteSetUtil.agda @ 346:4456eebbd1bc

copying countable bijection may not easy
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 17 Jul 2023 11:00:19 +0900
parents bcf3ca6ba87b
children
comparison
equal deleted inserted replaced
345:bcf3ca6ba87b 346:4456eebbd1bc
1 {-# OPTIONS --allow-unsolved-metas #-} 1 {-# OPTIONS --allow-unsolved-metas #-}
2 2
3 module finiteSetUtil where 3 module finiteSetUtil where
4 4
5 open import Data.Nat hiding ( _≟_ ) 5 open import Data.Nat hiding ( _≟_ )
6 open import Data.Fin renaming ( _<_ to _<<_ ; _>_ to _f>_ ; _≟_ to _f≟_ ) hiding (_≤_ ) 6 open import Data.Fin renaming ( _<_ to _<<_ ; _>_ to _f>_ ; _≟_ to _f≟_ ) hiding (_≤_ ; pred )
7 open import Data.Fin.Properties hiding ( <-trans ) renaming ( <-cmp to <-fcmp ) 7 open import Data.Fin.Properties hiding ( <-trans ) renaming ( <-cmp to <-fcmp ) hiding (≤-refl ; ≤-trans )
8 open import Data.Empty 8 open import Data.Empty
9 open import Relation.Nullary 9 open import Relation.Nullary
10 open import Relation.Binary.Definitions 10 open import Relation.Binary.Definitions
11 open import Relation.Binary.PropositionalEquality 11 open import Relation.Binary.PropositionalEquality
12 open import logic 12 open import logic
570 dl01 = subst (λ k → fin-dup-in-list k (map (F←Q finq) qs) ≡ true ) 570 dl01 = subst (λ k → fin-dup-in-list k (map (F←Q finq) qs) ≡ true )
571 (sym (finiso← finq _)) ( FDup-in-list.is-dup dl ) 571 (sym (finiso← finq _)) ( FDup-in-list.is-dup dl )
572 572
573 open import bijection using ( InjectiveF ; Is ) 573 open import bijection using ( InjectiveF ; Is )
574 574
575 record countB (B : Set) : Set where
576 field
577 cb : ℕ
578 ib : {i : ℕ} → i < cb → B
579 ib-inject : {i j : ℕ} → (i<cb : i < cb) → (j<cb : j < cb) → ib i<cb ≡ ib j<cb → i ≡ j
580
581 inject-fin : {A B : Set} (fa : FiniteSet A ) 575 inject-fin : {A B : Set} (fa : FiniteSet A )
582 → (fi : InjectiveF B A) 576 → (gi : InjectiveF B A)
583 → (is-B : (a : A ) → Dec (Is B A (InjectiveF.f fi) a) ) 577 → (is-B : (a : A ) → Dec (Is B A (InjectiveF.f gi) a) )
584 → FiniteSet B 578 → FiniteSet B
585 inject-fin {A} {B} fa fi is-B = record { 579 inject-fin {A} {B} fa gi is-B with finite fa | inspect finite fa
586 finite = countB.cb (cb00 (finite fa) NP.≤-refl ) 580 inject-fin {A} {B} fa gi is-B | zero | record { eq = eq1 } = ?
587 ; Q←F = λ fb → countB.ib (cb00 (finite fa) NP.≤-refl ) (fin<n {_} {fb}) 581 inject-fin {A} {B} fa gi is-B | suc pfa | record { eq = eq1 } = record {
588 ; F←Q = λ b → fromℕ< (cb<cb (fin<n {_} {F←Q fa (InjectiveF.f fi b)} )) 582 finite = maxb
583 ; Q←F = λ fb → ntob (toℕ fb) fin<n
584 ; F←Q = λ b → fromℕ< (bton<maxb b)
589 ; finiso→ = ? 585 ; finiso→ = ?
590 ; finiso← = ? 586 ; finiso← = ?
591 } where 587 } where
592 f = InjectiveF.f fi 588 g = InjectiveF.f gi
593 cb00 : (i : ℕ) → i ≤ finite fa → countB B 589 pfa<fa : pfa < finite fa
594 cb00 0 i<fa = record { cb = 0 ; ib = λ () ; ib-inject = λ () } 590 pfa<fa = subst (λ k → pfa < k ) (sym eq1) a<sa
595 cb00 (suc i) i<fa with is-B (Q←F fa (fromℕ< i<fa)) 591 h : ℕ → A
596 ... | yes y = record { cb = suc (countB.cb (cb00 i (NP.≤-trans a≤sa i<fa))) ; ib = cb01 ; ib-inject = cb02 } where 592 h i with <-cmp i (finite fa)
597 pcb : countB B 593 ... | tri< a ¬b ¬c = Q←F fa (fromℕ< a )
598 pcb = cb00 i (NP.≤-trans a≤sa i<fa) 594 ... | tri≈ ¬a b ¬c = Q←F fa (fromℕ< pfa<fa )
599 cb01 : {j : ℕ} → j < suc (countB.cb (cb00 i (NP.≤-trans a≤sa i<fa))) → B 595 ... | tri> ¬a ¬b c = Q←F fa (fromℕ< pfa<fa )
600 cb01 {j} j<c with <-∨ j<c 596 C = A
601 ... | case1 eq = Is.a y 597 h⁻¹ : A → ℕ
602 ... | case2 lt = countB.ib (cb00 i (NP.≤-trans a≤sa i<fa)) lt 598 h⁻¹ a = toℕ (F←Q fa a)
603 cb02 : {i1 : ℕ} {j : ℕ} 599
604 (i<cb : i1 < suc (countB.cb (cb00 i (NP.≤-trans a≤sa i<fa)))) 600 h-iso : (i : ℕ) → i < finite fa → h⁻¹ (h i) ≡ i
605 (j<cb : j < suc (countB.cb (cb00 i (NP.≤-trans a≤sa i<fa)))) → 601 h-iso = ?
606 cb01 i<cb ≡ cb01 j<cb → i1 ≡ j 602
607 cb02 = ? 603 count-B : ℕ → ℕ
608 ... | no n = record { cb = countB.cb (cb00 i (NP.≤-trans a≤sa i<fa)) ; ib = cb01 604 count-B zero with is-B (h zero)
609 ; ib-inject = countB.ib-inject (cb00 i (NP.≤-trans a≤sa i<fa))} where 605 ... | yes isb = 1
610 cb01 : {j : ℕ} → j < countB.cb (cb00 i (NP.≤-trans a≤sa i<fa)) → B 606 ... | no nisb = 0
611 cb01 {j} j<c = countB.ib (cb00 i (NP.≤-trans a≤sa i<fa)) j<c 607 count-B (suc n) with is-B (h (suc n))
612 608 ... | yes isb = suc (count-B n)
613 cb<cb : {i : ℕ} → (i<fa : i ≤ finite fa) → countB.cb (cb00 _ i<fa) < countB.cb (cb00 _ NP.≤-refl) 609 ... | no nisb = count-B n
614 cb<cb = ? 610
615 611 maxb : ℕ
612 maxb = count-B (finite fa)
613
614 --
615 -- countB record create ℕ → B and its injection
616 --
617 record CountB (n : ℕ) : Set where
618 field
619 b : B
620 cb : ℕ
621 b=cn : h cb ≡ g b
622 cb=n : count-B cb ≡ suc n
623 cb-inject : (cb1 : ℕ) → Is B C g (h cb1) → count-B cb ≡ count-B cb1 → cb ≡ cb1
624
625 count-B-mono : {i j : ℕ} → i ≤ j → count-B i ≤ count-B j
626 count-B-mono {i} {j} i≤j with ≤-∨ i≤j
627 ... | case1 refl = ≤-refl
628 ... | case2 i<j = lem00 _ _ i<j where
629 lem00 : (i j : ℕ) → i < j → count-B i ≤ count-B j
630 lem00 i (suc j) (s≤s i<j) = ≤-trans (count-B-mono i<j) (lem01 j) where
631 lem01 : (j : ℕ) → count-B j ≤ count-B (suc j)
632 lem01 zero with is-B (h (suc zero))
633 ... | yes isb = refl-≤s
634 ... | no nisb = ≤-refl
635 lem01 (suc n) with is-B (h (suc (suc n)))
636 ... | yes isb = refl-≤s
637 ... | no nisb = ≤-refl
638
639 lem01 : (n i : ℕ) → suc n ≤ count-B i → CountB n
640 lem01 n i le = lem09 i (count-B i) le refl where
641 -- injection of count-B
642 ---
643 lem06 : (i j : ℕ ) → Is B C g (h i) → Is B C g (h j) → count-B i ≡ count-B j → i ≡ j
644 lem06 i j bi bj eq = lem08 where
645 lem20 : (i j : ℕ) → i < j → Is B C g (h i) → Is B C g (h j) → count-B j ≡ count-B i → ⊥
646 lem20 zero (suc j) i<j bi bj le with is-B (h 0) | inspect count-B 0 | is-B (h (suc j)) | inspect count-B (suc j)
647 ... | no nisc | _ | _ | _ = ⊥-elim (nisc bi)
648 ... | yes _ | _ | no nisc | _ = ⊥-elim (nisc bj)
649 ... | yes _ | record { eq = eq1 } | yes _ | record { eq = eq2 } = ⊥-elim ( nat-≤> lem25 a<sa) where
650 lem22 : 1 ≡ count-B 0
651 lem22 with is-B (h 0) | inspect count-B 0
652 ... | yes _ | record { eq = eq1 } = refl
653 ... | no nisa | _ = ⊥-elim ( nisa bi )
654 lem24 : count-B j ≡ 0
655 lem24 = cong pred le
656 lem25 : 1 ≤ 0
657 lem25 = begin
658 1 ≡⟨ lem22 ⟩
659 count-B 0 ≤⟨ count-B-mono {0} {j} z≤n ⟩
660 count-B j ≡⟨ lem24 ⟩
661 0 ∎ where open ≤-Reasoning
662 lem20 (suc i) zero () bi bj le
663 lem20 (suc i) (suc j) (s≤s i<j) bi bj le = ⊥-elim ( nat-≡< lem24 lem21 ) where
664 --
665 -- i < suc i ≤ j
666 -- cb i < suc (cb i) < cb (suc i) ≤ cb j
667 -- suc (cb i) ≡ suc (cb j) → cb i ≡ cb j
668 lem22 : suc (count-B i) ≡ count-B (suc i)
669 lem22 with is-B (h (suc i)) | inspect count-B (suc i)
670 ... | yes _ | record { eq = eq1 } = refl
671 ... | no nisa | _ = ⊥-elim ( nisa bi )
672 lem23 : suc (count-B j) ≡ count-B (suc j)
673 lem23 with is-B (h (suc j)) | inspect count-B (suc j)
674 ... | yes _ | record { eq = eq1 } = refl
675 ... | no nisa | _ = ⊥-elim ( nisa bj )
676 lem24 : count-B i ≡ count-B j
677 lem24 with is-B (h (suc i)) | inspect count-B (suc i) | is-B (h (suc j)) | inspect count-B (suc j)
678 ... | no nisc | _ | _ | _ = ⊥-elim (nisc bi)
679 ... | yes _ | _ | no nisc | _ = ⊥-elim (nisc bj)
680 ... | yes _ | record { eq = eq1 } | yes _ | record { eq = eq2 } = sym (cong pred le)
681 lem21 : suc (count-B i) ≤ count-B j
682 lem21 = begin
683 suc (count-B i) ≡⟨ lem22 ⟩
684 count-B (suc i) ≤⟨ count-B-mono i<j ⟩
685 count-B j ∎ where
686 open ≤-Reasoning
687 lem08 : i ≡ j
688 lem08 with <-cmp i j
689 ... | tri< a ¬b ¬c = ⊥-elim ( lem20 i j a bi bj (sym eq) )
690 ... | tri≈ ¬a b ¬c = b
691 ... | tri> ¬a ¬b c₁ = ⊥-elim ( lem20 j i c₁ bj bi eq )
692
693 lem07 : (n i : ℕ) → count-B i ≡ suc n → CountB n
694 lem07 n 0 eq with is-B (h 0) | inspect count-B 0
695 ... | yes isb | record { eq = eq1 } = record { b = Is.a isb ; cb = 0 ; b=cn = sym (Is.fa=c isb) ; cb=n = trans eq1 eq
696 ; cb-inject = λ cb1 iscb1 cb1eq → lem12 cb1 iscb1 (subst (λ k → k ≡ count-B cb1) eq1 cb1eq) } where
697 lem12 : (cb1 : ℕ) → Is B C g (h cb1) → 1 ≡ count-B cb1 → 0 ≡ cb1
698 lem12 cb1 iscb1 cbeq = lem06 0 cb1 isb iscb1 (trans eq1 cbeq)
699 ... | no nisb | record { eq = eq1 } = ⊥-elim ( nat-≡< eq (s≤s z≤n ) )
700 lem07 n (suc i) eq with is-B (h (suc i)) | inspect count-B (suc i)
701 ... | yes isb | record { eq = eq1 } = record { b = Is.a isb ; cb = suc i ; b=cn = sym (Is.fa=c isb) ; cb=n = trans eq1 eq
702 ; cb-inject = λ cb1 iscb1 cb1eq → lem12 cb1 iscb1 (subst (λ k → k ≡ count-B cb1) eq1 cb1eq) } where
703 lem12 : (cb1 : ℕ) → Is B C g (h cb1) → suc (count-B i) ≡ count-B cb1 → suc i ≡ cb1
704 lem12 cb1 iscb1 cbeq = lem06 (suc i) cb1 isb iscb1 (trans eq1 cbeq)
705 ... | no nisb | record { eq = eq1 } = lem07 n i eq
706
707 -- starting from 0, if count B i ≡ suc n, this is it
708
709 lem09 : (i j : ℕ) → suc n ≤ j → j ≡ count-B i → CountB n
710 lem09 0 (suc j) (s≤s le) eq with ≤-∨ (s≤s le)
711 ... | case1 eq1 = lem07 n 0 (sym (trans eq1 eq ))
712 ... | case2 (s≤s lt) with is-B (h 0) | inspect count-B 0
713 ... | yes isb | record { eq = eq1 } = ⊥-elim ( nat-≤> (≤-trans (s≤s lt) (refl-≤≡ eq) ) (s≤s (s≤s z≤n)) )
714 ... | no nisb | record { eq = eq1 } = ⊥-elim (nat-≡< (sym eq) (s≤s z≤n))
715 lem09 (suc i) (suc j) (s≤s le) eq with ≤-∨ (s≤s le)
716 ... | case1 eq1 = lem07 n (suc i) (sym (trans eq1 eq ))
717 ... | case2 (s≤s lt) with is-B (h (suc i)) | inspect count-B (suc i)
718 ... | yes isb | record { eq = eq1 } = lem09 i j lt (cong pred eq)
719 ... | no nisb | record { eq = eq1 } = lem09 i (suc j) (≤-trans lt a≤sa) eq
720
721 bton : B → ℕ
722 bton b = pred (count-B (h⁻¹ (g b)))
723
724 lem22 : {n : ℕ } → n < maxb → suc n ≤ count-B n
725 lem22 {n} n<mb = ?
726
727 lem23 : (b : B) → 0 < count-B (h⁻¹ (g b))
728 lem23 = ?
729
730 bton<maxb : (b : B) → bton b < maxb
731 bton<maxb b = begin
732 suc (bton b) ≡⟨ sucprd (lem23 b) ⟩
733 count-B (h⁻¹ (g b)) ≤⟨ count-B-mono {h⁻¹ (g b)} {finite fa} (<to≤ fin<n) ⟩
734 count-B (finite fa) ≡⟨ refl ⟩
735 maxb ∎ where
736 open ≤-Reasoning
737
738 ntob : (n : ℕ) → n < maxb → B
739 ntob n n<mb = CountB.b (lem01 n n (lem22 n<mb))
740
741 biso : (n : ℕ) → (n<mb : n < maxb) → bton (ntob n n<mb ) ≡ n
742 biso n n<mb = begin
743 bton (ntob n n<mb ) ≡⟨ refl ⟩
744 pred (count-B (h⁻¹ (g (CountB.b CB)))) ≡⟨ sym ( cong (λ k → pred (count-B (h⁻¹ k))) ( CountB.b=cn CB)) ⟩
745 pred (count-B (h⁻¹ (h (CountB.cb CB)))) ≡⟨ cong (λ k → pred (count-B k)) (h-iso _ cb<fa) ⟩
746 pred (count-B (CountB.cb CB)) ≡⟨ cong pred (CountB.cb=n CB) ⟩
747 pred (suc n) ≡⟨ refl ⟩
748 n ∎ where
749 open ≡-Reasoning
750 CB : CountB n
751 CB = lem01 n n (lem22 n<mb)
752 cb<fa : CountB.cb CB < finite fa
753 cb<fa = ?
754
755
756 --
757 -- uniqueness of ntob is proved by injection
758 --
759 biso1 : (b : B) → ntob (bton b) (bton<maxb b) ≡ b
760 biso1 b = ?
761