------------------------------------------------------------------------ -- The Agda standard library -- -- Properties related to Fin, and operations making use of these -- properties (or other properties not available in Data.Fin) ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} {-# OPTIONS --warning=noUserWarning #-} -- for deprecated _≺_ and _≻toℕ_ (issue #1726) module Data.Fin.Properties where open import Axiom.Extensionality.Propositional open import Algebra.Definitions using (Involutive) open import Effect.Applicative using (RawApplicative) open import Effect.Functor using (RawFunctor) open import Data.Bool.Base using (Bool; true; false; not; _∧_; _∨_) open import Data.Empty using (⊥; ⊥-elim) open import Data.Fin.Base open import Data.Fin.Patterns open import Data.Nat.Base as ℕ using (ℕ; zero; suc; s≤s; z≤n; z) open import Data.Product.Properties using (,-injective) open import Data.Product.Algebra using (×-cong) open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]; [_,_]′) open import Data.Sum.Properties using ([,]-map; [,]-∘) open import Function.Base using (_∘_; id; _$_; flip) open import Function.Bundles using (Injection; _↣_; _⇔_; _↔_; mk⇔; mk↔ₛ′) open import Function.Definitions using (Injective; Surjective) open import Function.Consequences.Propositional using (contraInjective) open import Function.Construct.Composition as Comp hiding (injective) open import Level using (Level) open import Relation.Binary.Definitions as B hiding (Decidable) open import Relation.Binary.Core using (_⇒_; _Preserves_⟶_) open import Relation.Binary.Bundles using (Preorder; Setoid; DecSetoid; Poset; TotalOrder; DecTotalOrder; StrictPartialOrder; StrictTotalOrder) open import Relation.Binary.Structures using (IsDecEquivalence; IsPreorder; IsPartialOrder; IsTotalOrder; IsDecTotalOrder; IsStrictPartialOrder; IsStrictTotalOrder) open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_; _≢_; refl; sym; trans; cong; cong₂; subst; _≗_) open import Relation.Binary.PropositionalEquality.Properties as ≡ using (module ≡-Reasoning) open import Relation.Nullary.Decidable as Dec using (Dec; _because_; yes; no; _×-dec_; _⊎-dec_; map′) open import Relation.Nullary.Negation.Core using (¬_; contradiction) open import Relation.Nullary.Reflects using (Reflects; invert) open import Relation.Unary as U using (U; Pred; Decidable; _⊆_; Satisfiable; Universal) open import Relation.Unary.Properties using (U?) private variable a : Level A : Set a m n o : ℕ i j : Fin n ------------------------------------------------------------------------ -- Fin ------------------------------------------------------------------------ ¬Fin0 : ¬ Fin 0 ¬Fin0 () nonZeroIndex : Fin n → ℕ.NonZero n nonZeroIndex {n = suc _} _ = _ ------------------------------------------------------------------------ -- Bundles 0↔⊥ : Fin 0 ↔ ⊥ 0↔⊥ = mk↔ₛ′ ¬Fin0 (λ ()) (λ ()) (λ ()) 1↔⊤ : Fin 1 ↔ ⊤ 1↔⊤ = mk↔ₛ′ (λ { 0F → tt }) (λ { tt → 0F }) (λ { tt → refl }) λ { 0F → refl } 2↔Bool : Fin 2 ↔ Bool 2↔Bool = mk↔ₛ′ (λ { 0F → false; 1F → true }) (λ { false → 0F ; true → 1F }) (λ { false → refl ; true → refl }) (λ { 0F → refl ; 1F → refl }) ------------------------------------------------------------------------ -- Properties of _≡_ ------------------------------------------------------------------------ 0≢1+n : zero ≢ Fin.suc i 0≢1+n () suc-injective : Fin.suc i ≡ suc j → i ≡ j suc-injective refl = refl infix 4 _≟_ _≟_ : DecidableEquality (Fin n) zero ≟ zero = yes refl zero ≟ suc y = no λ() suc x ≟ zero = no λ() suc x ≟ suc y = map′ (cong suc) suc-injective (x ≟ y) ------------------------------------------------------------------------ -- Structures ≡-isDecEquivalence : IsDecEquivalence {A = Fin n} _≡_ ≡-isDecEquivalence = record { isEquivalence = ≡.isEquivalence ; _≟_ = _≟_ } ------------------------------------------------------------------------ -- Bundles ≡-preorder : ℕ → Preorder _ _ _ ≡-preorder n = ≡.preorder (Fin n) ≡-setoid : ℕ → Setoid _ _ ≡-setoid n = ≡.setoid (Fin n) ≡-decSetoid : ℕ → DecSetoid _ _ ≡-decSetoid n = record { isDecEquivalence = ≡-isDecEquivalence {n} } ------------------------------------------------------------------------ -- toℕ ------------------------------------------------------------------------ toℕ-injective : toℕ i ≡ toℕ j → i ≡ j toℕ-injective {zero} {} {} _ toℕ-injective {suc n} {zero} {zero} eq = refl toℕ-injective {suc n} {suc i} {suc j} eq = cong suc (toℕ-injective (cong ℕ.pred eq)) toℕ-strengthen : ∀ (i : Fin n) → toℕ (strengthen i) ≡ toℕ i toℕ-strengthen zero = refl toℕ-strengthen (suc i) = cong suc (toℕ-strengthen i) ------------------------------------------------------------------------ -- toℕ-↑ˡ: "i" ↑ˡ n = "i" in Fin (m + n) ------------------------------------------------------------------------ toℕ-↑ˡ : ∀ (i : Fin m) n → toℕ (i ↑ˡ n) ≡ toℕ i toℕ-↑ˡ zero n = refl toℕ-↑ˡ (suc i) n = cong suc (toℕ-↑ˡ i n) ↑ˡ-injective : ∀ n (i j : Fin m) → i ↑ˡ n ≡ j ↑ˡ n → i ≡ j ↑ˡ-injective n zero zero refl = refl ↑ˡ-injective n (suc i) (suc j) eq = cong suc (↑ˡ-injective n i j (suc-injective eq)) ------------------------------------------------------------------------ -- toℕ-↑ʳ: n ↑ʳ "i" = "n + i" in Fin (n + m) ------------------------------------------------------------------------ toℕ-↑ʳ : ∀ n (i : Fin m) → toℕ (n ↑ʳ i) ≡ n ℕ.+ toℕ i toℕ-↑ʳ zero i = refl toℕ-↑ʳ (suc n) i = cong suc (toℕ-↑ʳ n i) ↑ʳ-injective : ∀ n (i j : Fin m) → n ↑ʳ i ≡ n ↑ʳ j → i ≡ j ↑ʳ-injective zero i i refl = refl ↑ʳ-injective (suc n) i j eq = ↑ʳ-injective n i j (suc-injective eq) ------------------------------------------------------------------------ -- toℕ and the ordering relations ------------------------------------------------------------------------ toℕ (λ()) (λ()) z i≮j i≢j j (i≮j ∘ s _ _ i>k = contradiction (sym cᵢⱼ≡cₖₗ) (<⇒≢ (combine-monoˡ-< l j i>k)) combine-injectiveʳ : ∀ (i : Fin m) (j : Fin n) (k : Fin m) (l : Fin n) → combine i j ≡ combine k l → j ≡ l combine-injectiveʳ {m} {n} i j k l cᵢⱼ≡cₖₗ with refl ← combine-injectiveˡ i j k l cᵢⱼ≡cₖₗ = toℕ-injective (ℕ.+-cancelˡ-≡ (n ℕ.* toℕ i) _ _ (begin n ℕ.* toℕ i ℕ.+ toℕ j ≡⟨ toℕ-combine i j ⟨ toℕ (combine i j) ≡⟨ cong toℕ cᵢⱼ≡cₖₗ ⟩ toℕ (combine i l) ≡⟨ toℕ-combine i l ⟩ n ℕ.* toℕ i ℕ.+ toℕ l ∎)) where open ≡-Reasoning combine-injective : ∀ (i : Fin m) (j : Fin n) (k : Fin m) (l : Fin n) → combine i j ≡ combine k l → i ≡ k × j ≡ l combine-injective i j k l cᵢⱼ≡cₖₗ = combine-injectiveˡ i j k l cᵢⱼ≡cₖₗ , combine-injectiveʳ i j k l cᵢⱼ≡cₖₗ combine-surjective : ∀ (i : Fin (m ℕ.* n)) → ∃₂ λ j k → combine j k ≡ i combine-surjective {m} {n} i with j , k ← remQuot {m} n i in eq = j , k , (begin combine j k ≡⟨ uncurry (cong₂ combine) (,-injective eq) ⟨ uncurry combine (remQuot {m} n i) ≡⟨ combine-remQuot {m} n i ⟩ i ∎) where open ≡-Reasoning ------------------------------------------------------------------------ -- Bundles *↔× : Fin (m ℕ.* n) ↔ (Fin m × Fin n) *↔× {m} {n} = mk↔ₛ′ (remQuot {m} n) (uncurry combine) (uncurry remQuot-combine) (combine-remQuot {m} n) ------------------------------------------------------------------------ -- fin→fun ------------------------------------------------------------------------ funToFin-finToFin : funToFin {m} {n} ∘ finToFun ≗ id funToFin-finToFin {zero} {n} zero = refl funToFin-finToFin {suc m} {n} k = begin combine (finToFun {n} {suc m} k zero) (funToFin (finToFun {n} {suc m} k ∘ suc)) ≡⟨⟩ combine (quotient {n} (n ^ m) k) (funToFin (finToFun {n} {m} (remainder {n} (n ^ m) k))) ≡⟨ cong (combine (quotient {n} (n ^ m) k)) (funToFin-finToFin {m} (remainder {n} (n ^ m) k)) ⟩ combine (quotient {n} (n ^ m) k) (remainder {n} (n ^ m) k) ≡⟨⟩ uncurry combine (remQuot {n} (n ^ m) k) ≡⟨ combine-remQuot {n = n} (n ^ m) k ⟩ k ∎ where open ≡-Reasoning finToFun-funToFin : (f : Fin m → Fin n) → finToFun (funToFin f) ≗ f finToFun-funToFin {suc m} {n} f zero = begin quotient (n ^ m) (combine (f zero) (funToFin (f ∘ suc))) ≡⟨ cong proj₁ (remQuot-combine _ _) ⟩ proj₁ (f zero , funToFin (f ∘ suc)) ≡⟨⟩ f zero ∎ where open ≡-Reasoning finToFun-funToFin {suc m} {n} f (suc i) = begin finToFun (remainder {n} (n ^ m) (combine (f zero) (funToFin (f ∘ suc)))) i ≡⟨ cong (λ rq → finToFun (proj₂ rq) i) (remQuot-combine {n} _ _) ⟩ finToFun (proj₂ (f zero , funToFin (f ∘ suc))) i ≡⟨⟩ finToFun (funToFin (f ∘ suc)) i ≡⟨ finToFun-funToFin (f ∘ suc) i ⟩ (f ∘ suc) i ≡⟨⟩ f (suc i) ∎ where open ≡-Reasoning ------------------------------------------------------------------------ -- Bundles ^↔→ : Extensionality _ _ → Fin (m ^ n) ↔ (Fin n → Fin m) ^↔→ {m} {n} ext = mk↔ₛ′ finToFun funToFin (ext ∘ finToFun-funToFin) (funToFin-finToFin {n} {m}) ------------------------------------------------------------------------ -- lift ------------------------------------------------------------------------ lift-injective : ∀ (f : Fin m → Fin n) → Injective _≡_ _≡_ f → ∀ k → Injective _≡_ _≡_ (lift k f) lift-injective f inj zero {_} {_} eq = inj eq lift-injective f inj (suc k) {zero} {zero} eq = refl lift-injective f inj (suc k) {suc _} {suc _} eq = cong suc (lift-injective f inj k (suc-injective eq)) ------------------------------------------------------------------------ -- pred ------------------------------------------------------------------------ <⇒≤pred : i < j → i ≤ pred j <⇒≤pred {i = zero} {j = suc j} z ∃-here : P zero → ∃⟨ P ⟩ ∃-here = zero ,_ ∃-there : ∃⟨ P ∘ suc ⟩ → ∃⟨ P ⟩ ∃-there = map suc id ∃-toSum : ∃⟨ P ⟩ → P zero ⊎ ∃⟨ P ∘ suc ⟩ ∃-toSum ( zero , P₀ ) = inj₁ P₀ ∃-toSum (suc f , P₁₊) = inj₂ (f , P₁₊) ⊎⇔∃ : (P zero ⊎ ∃⟨ P ∘ suc ⟩) ⇔ ∃⟨ P ⟩ ⊎⇔∃ = mk⇔ [ ∃-here , ∃-there ] ∃-toSum decFinSubset : ∀ {p q} {P : Pred (Fin n) p} {Q : Pred (Fin n) q} → Decidable Q → (∀ {i} → Q i → Dec (P i)) → Dec (Q ⊆ P) decFinSubset {zero} {_} {_} Q? P? = yes λ {} decFinSubset {suc n} {P = P} {Q} Q? P? with Q? zero | ∀-cons {P = λ x → Q x → P x} ... | false because [¬Q0] | cons = map′ (λ f {x} → cons (⊥-elim ∘ invert [¬Q0]) (λ x → f {x}) x) (λ f {x} → f {suc x}) (decFinSubset (Q? ∘ suc) P?) ... | true because [Q0] | cons = map′ (uncurry λ P0 rec {x} → cons (λ _ → P0) (λ x → rec {x}) x) < _$ invert [Q0] , (λ f {x} → f {suc x}) > (P? (invert [Q0]) ×-dec decFinSubset (Q? ∘ suc) P?) any? : ∀ {p} {P : Pred (Fin n) p} → Decidable P → Dec (∃ P) any? {zero} {P = _} P? = no λ { (() , _) } any? {suc n} {P = P} P? = Dec.map ⊎⇔∃ (P? zero ⊎-dec any? (P? ∘ suc)) all? : ∀ {p} {P : Pred (Fin n) p} → Decidable P → Dec (∀ f → P f) all? P? = map′ (λ ∀p f → ∀p tt) (λ ∀p {x} _ → ∀p x) (decFinSubset U? (λ {f} _ → P? f)) private -- A nice computational property of `all?`: -- The boolean component of the result is exactly the -- obvious fold of boolean tests (`foldr _∧_ true`). note : ∀ {p} {P : Pred (Fin 3) p} (P? : Decidable P) → ∃ λ z → Dec.does (all? P?) ≡ z note P? = Dec.does (P? 0F) ∧ Dec.does (P? 1F) ∧ Dec.does (P? 2F) ∧ true , refl -- If a decidable predicate P over a finite set is sometimes false, -- then we can find the smallest value for which this is the case. ¬∀⟶∃¬-smallest : ∀ n {p} (P : Pred (Fin n) p) → Decidable P → ¬ (∀ i → P i) → ∃ λ i → ¬ P i × ((j : Fin′ i) → P (inject j)) ¬∀⟶∃¬-smallest zero P P? ¬∀P = contradiction (λ()) ¬∀P ¬∀⟶∃¬-smallest (suc n) P P? ¬∀P with P? zero ... | false because [¬P₀] = (zero , invert [¬P₀] , λ ()) ... | true because [P₀] = map suc (map id (∀-cons (invert [P₀]))) (¬∀⟶∃¬-smallest n (P ∘ suc) (P? ∘ suc) (¬∀P ∘ (∀-cons (invert [P₀])))) -- When P is a decidable predicate over a finite set the following -- lemma can be proved. ¬∀⟶∃¬ : ∀ n {p} (P : Pred (Fin n) p) → Decidable P → ¬ (∀ i → P i) → (∃ λ i → ¬ P i) ¬∀⟶∃¬ n P P? ¬P = map id proj₁ (¬∀⟶∃¬-smallest n P P? ¬P) ------------------------------------------------------------------------ -- Properties of functions to and from Fin ------------------------------------------------------------------------ -- The pigeonhole principle. pigeonhole : m ℕ.< n → (f : Fin n → Fin m) → ∃₂ λ i j → i < j × f i ≡ f j pigeonhole z ∀iPi zero <*> sequence (∀iPi ∘ suc) module _ {f} {F : Set f → Set f} (RF : RawFunctor F) where open RawFunctor RF sequence⁻¹ : ∀ {A : Set f} {P : Pred A f} → F (∀ i → P i) → (∀ i → F (P i)) sequence⁻¹ F∀iPi i = (λ f → f i) <$> F∀iPi ------------------------------------------------------------------------ -- If there is an injection from a type A to a finite set, then the type -- has decidable equality. module _ {ℓ} {S : Setoid a ℓ} (inj : Injection S (≡-setoid n)) where open Setoid S inj⇒≟ : B.Decidable _≈_ inj⇒≟ = Dec.via-injection inj _≟_ inj⇒decSetoid : DecSetoid a ℓ inj⇒decSetoid = record { isDecEquivalence = record { isEquivalence = isEquivalence ; _≟_ = inj⇒≟ } } ------------------------------------------------------------------------ -- Opposite ------------------------------------------------------------------------ opposite-prop : ∀ (i : Fin n) → toℕ (opposite i) ≡ n ∸ suc (toℕ i) opposite-prop {suc n} zero = toℕ-fromℕ n opposite-prop {suc n} (suc i) = begin toℕ (inject₁ (opposite i)) ≡⟨ toℕ-inject₁ (opposite i) ⟩ toℕ (opposite i) ≡⟨ opposite-prop i ⟩ n ∸ suc (toℕ i) ∎ where open ≡-Reasoning opposite-involutive : Involutive {A = Fin n} _≡_ opposite opposite-involutive {suc n} i = toℕ-injective (begin toℕ (opposite (opposite i)) ≡⟨ opposite-prop (opposite i) ⟩ n ∸ (toℕ (opposite i)) ≡⟨ cong (n ∸_) (opposite-prop i) ⟩ n ∸ (n ∸ (toℕ i)) ≡⟨ ℕ.m∸[m∸n]≡n (toℕ≤pred[n] i) ⟩ toℕ i ∎) where open ≡-Reasoning opposite-suc : ∀ (i : Fin n) → toℕ (opposite (suc i)) ≡ toℕ (opposite i) opposite-suc {n} i = begin toℕ (opposite (suc i)) ≡⟨ opposite-prop (suc i) ⟩ suc n ∸ suc (toℕ (suc i)) ≡⟨⟩ n ∸ toℕ (suc i) ≡⟨⟩ n ∸ suc (toℕ i) ≡⟨ opposite-prop i ⟨ toℕ (opposite i) ∎ where open ≡-Reasoning ------------------------------------------------------------------------ -- DEPRECATED NAMES ------------------------------------------------------------------------ -- Please use the new names as continuing support for the old names is -- not guaranteed. -- Version 1.5 inject+-raise-splitAt = join-splitAt {-# WARNING_ON_USAGE inject+-raise-splitAt "Warning: inject+-raise-splitAt was deprecated in v1.5. Please use join-splitAt instead." #-} -- Version 2.0 toℕ-raise = toℕ-↑ʳ {-# WARNING_ON_USAGE toℕ-raise "Warning: toℕ-raise was deprecated in v2.0. Please use toℕ-↑ʳ instead." #-} toℕ-inject+ : ∀ {m} n (i : Fin m) → toℕ i ≡ toℕ (i ↑ˡ n) toℕ-inject+ n i = sym (toℕ-↑ˡ i n) {-# WARNING_ON_USAGE toℕ-inject+ "Warning: toℕ-inject+ was deprecated in v2.0. Please use toℕ-↑ˡ instead. NB argument order has been flipped: the left-hand argument is the Fin m the right-hand is the Nat index increment." #-} splitAt-inject+ : ∀ m n i → splitAt m (i ↑ˡ n) ≡ inj₁ i splitAt-inject+ m n i = splitAt-↑ˡ m i n {-# WARNING_ON_USAGE splitAt-inject+ "Warning: splitAt-inject+ was deprecated in v2.0. Please use splitAt-↑ˡ instead. NB argument order has been flipped." #-} splitAt-raise : ∀ m n i → splitAt m (m ↑ʳ i) ≡ inj₂ {B = Fin n} i splitAt-raise = splitAt-↑ʳ {-# WARNING_ON_USAGE splitAt-raise "Warning: splitAt-raise was deprecated in v2.0. Please use splitAt-↑ʳ instead." #-} Fin0↔⊥ : Fin 0 ↔ ⊥ Fin0↔⊥ = 0↔⊥ {-# WARNING_ON_USAGE Fin0↔⊥ "Warning: Fin0↔⊥ was deprecated in v2.0. Please use 0↔⊥ instead." #-} eq? : A ↣ Fin n → DecidableEquality A eq? = inj⇒≟ {-# WARNING_ON_USAGE eq? "Warning: eq? was deprecated in v2.0. Please use inj⇒≟ instead." #-} private z≺s : ∀ {n} → zero ≺ suc n z≺s = _ ≻toℕ zero s≺s : ∀ {m n} → m ≺ n → suc m ≺ suc n s≺s (n ≻toℕ i) = (suc n) ≻toℕ (suc i) <⇒≺ : ℕ._<_ ⇒ _≺_ <⇒≺ {zero} z