------------------------------------------------------------------------ -- The Agda standard library -- -- Some properties about subsets ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} module Data.Fin.Subset.Properties where import Algebra.Definitions as AlgebraicDefinitions import Algebra.Structures as AlgebraicStructures using (IsMagma; IsSemigroup; IsMonoid; IsBand; IsCommutativeMonoid ; IsIdempotentCommutativeMonoid) import Algebra.Lattice.Structures as AlgebraicLatticeStructures using (IsSemilattice; IsLattice; IsDistributiveLattice; IsBooleanAlgebra) open import Algebra.Bundles using (Magma; Semigroup; Monoid; Band; CommutativeMonoid ; IdempotentCommutativeMonoid) open import Algebra.Lattice.Bundles using (Semilattice; Lattice; DistributiveLattice; BooleanAlgebra) import Algebra.Lattice.Properties.Lattice as L import Algebra.Lattice.Properties.DistributiveLattice as DL import Algebra.Lattice.Properties.BooleanAlgebra as BA open import Data.Bool.Base using (not) open import Data.Bool.Properties open import Data.Fin.Base using (Fin; suc; zero) open import Data.Fin.Subset open import Data.Fin.Properties using (any?; decFinSubset) open import Data.Nat.Base hiding (∣_-_∣) import Data.Nat.Properties as ℕ open import Data.Product as Product using (∃; ∄; _×_; _,_; proj₁) open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]′) open import Data.Vec.Base using (Vec; []; _∷_; here; there) open import Data.Vec.Properties open import Function.Base using (_∘_; const; id; case_of_) open import Function.Bundles using (_⇔_; mk⇔) open import Level using (Level) open import Relation.Binary.Core using (_⇒_) open import Relation.Binary.Structures using (IsPreorder; IsPartialOrder; IsStrictPartialOrder; IsDecStrictPartialOrder) open import Relation.Binary.Bundles using (Preorder; Poset; StrictPartialOrder; DecStrictPartialOrder) open import Relation.Binary.Definitions as B hiding (Decidable; Empty) open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong; cong₂; subst; _≢_; sym) open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning; isEquivalence) open import Relation.Nullary.Decidable as Dec using (Dec; yes; no; _⊎-dec_) open import Relation.Nullary.Negation using (contradiction) open import Relation.Unary using (Pred; Decidable; Satisfiable) private variable ℓ : Level n : ℕ s t : Side x y : Fin n p q : Subset n ------------------------------------------------------------------------ -- Constructor mangling drop-there : suc x ∈ s ∷ p → x ∈ p drop-there (there x∈p) = x∈p drop-not-there : suc x ∉ s ∷ p → x ∉ p drop-not-there x∉sp x∈p = contradiction (there x∈p) x∉sp drop-∷-⊆ : s ∷ p ⊆ t ∷ q → p ⊆ q drop-∷-⊆ sp⊆tq x∈p = drop-there (sp⊆tq (there x∈p)) drop-∷-⊂ : s ∷ p ⊂ s ∷ q → p ⊂ q drop-∷-⊂ {s = inside} (_ , zero , _ , x∉sp) = contradiction here x∉sp drop-∷-⊂ {s} (sp⊆sq , suc x , there x∈q , x∉sp) = drop-∷-⊆ sp⊆sq , x , x∈q , drop-not-there x∉sp out⊆ : p ⊆ q → outside ∷ p ⊆ s ∷ q out⊆ p⊆q (there ∈p) = there (p⊆q ∈p) out⊆-⇔ : p ⊆ q ⇔ outside ∷ p ⊆ s ∷ q out⊆-⇔ = mk⇔ out⊆ drop-∷-⊆ in⊆in : p ⊆ q → inside ∷ p ⊆ inside ∷ q in⊆in p⊆q here = here in⊆in p⊆q (there ∈p) = there (p⊆q ∈p) in⊆in-⇔ : p ⊆ q ⇔ inside ∷ p ⊆ inside ∷ q in⊆in-⇔ = mk⇔ in⊆in drop-∷-⊆ s⊆s : p ⊆ q → s ∷ p ⊆ s ∷ q s⊆s p⊆q here = here s⊆s p⊆q (there ∈p) = there (p⊆q ∈p) s⊂s : p ⊂ q → s ∷ p ⊂ s ∷ q s⊂s (p⊆q , v , v∈p , v∉q) = s⊆s p⊆q , suc v , there v∈p , v∉q ∘ drop-there out⊂ : p ⊂ q → outside ∷ p ⊂ s ∷ q out⊂ (p⊆q , x , x∈q , x∉p) = out⊆ p⊆q , suc x , there x∈q , x∉p ∘ drop-there in⊂in : p ⊂ q → inside ∷ p ⊂ inside ∷ q in⊂in = s⊂s out⊂in : p ⊆ q → outside ∷ p ⊂ inside ∷ q out⊂in p⊆q = out⊆ p⊆q , zero , here , λ () out⊂in-⇔ : p ⊆ q ⇔ outside ∷ p ⊂ inside ∷ q out⊂in-⇔ = mk⇔ out⊂in (drop-∷-⊆ ∘ proj₁) out⊂out-⇔ : p ⊂ q ⇔ outside ∷ p ⊂ outside ∷ q out⊂out-⇔ = mk⇔ out⊂ drop-∷-⊂ in⊂in-⇔ : p ⊂ q ⇔ inside ∷ p ⊂ inside ∷ q in⊂in-⇔ = mk⇔ in⊂in drop-∷-⊂ ------------------------------------------------------------------------ -- _∈_ infix 4 _∈?_ _∈?_ : ∀ x (p : Subset n) → Dec (x ∈ p) zero ∈? inside ∷ p = yes here zero ∈? outside ∷ p = no λ() suc n ∈? s ∷ p = Dec.map′ there drop-there (n ∈? p) ------------------------------------------------------------------------ -- Empty drop-∷-Empty : Empty (s ∷ p) → Empty p drop-∷-Empty ¬∃∈ (x , x∈p) = ¬∃∈ (suc x , there x∈p) Empty-unique : Empty p → p ≡ ⊥ Empty-unique {p = []} ¬∃∈ = refl Empty-unique {p = inside ∷ p} ¬∃∈ = contradiction (zero , here) ¬∃∈ Empty-unique {p = outside ∷ p} ¬∃∈ = cong (outside ∷_) (Empty-unique (drop-∷-Empty ¬∃∈)) nonempty? : Decidable {A = Subset n} Nonempty nonempty? p = any? (_∈? p) ------------------------------------------------------------------------ -- ∣_∣ ∣p∣≤n : ∀ (p : Subset n) → ∣ p ∣ ≤ n ∣p∣≤n = count≤n (_≟ inside) ∣p∣≤∣x∷p∣ : ∀ x (p : Subset n) → ∣ p ∣ ≤ ∣ x ∷ p ∣ ∣p∣≤∣x∷p∣ outside p = ℕ.≤-refl ∣p∣≤∣x∷p∣ inside p = ℕ.n≤1+n ∣ p ∣ ------------------------------------------------------------------------ -- ⊥ ∉⊥ : x ∉ ⊥ ∉⊥ (there p) = ∉⊥ p ⊥⊆ : ⊥ ⊆ p ⊥⊆ x∈⊥ = contradiction x∈⊥ ∉⊥ ∣⊥∣≡0 : ∀ n → ∣ ⊥ {n = n} ∣ ≡ 0 ∣⊥∣≡0 zero = refl ∣⊥∣≡0 (suc n) = ∣⊥∣≡0 n ------------------------------------------------------------------------ -- ⊤ ∈⊤ : x ∈ ⊤ ∈⊤ {x = zero} = here ∈⊤ {x = suc x} = there ∈⊤ ⊆⊤ : p ⊆ ⊤ ⊆⊤ = const ∈⊤ ∣⊤∣≡n : ∀ n → ∣ ⊤ {n} ∣ ≡ n ∣⊤∣≡n zero = refl ∣⊤∣≡n (suc n) = cong suc (∣⊤∣≡n n) ∣p∣≡n⇒p≡⊤ : ∣ p ∣ ≡ n → p ≡ ⊤ {n} ∣p∣≡n⇒p≡⊤ {p = []} _ = refl ∣p∣≡n⇒p≡⊤ {p = outside ∷ p} |p|≡n = contradiction |p|≡n (ℕ.<⇒≢ (s≤s (∣p∣≤n p))) ∣p∣≡n⇒p≡⊤ {p = inside ∷ p} |p|≡n = cong (inside ∷_) (∣p∣≡n⇒p≡⊤ (ℕ.suc-injective |p|≡n)) ------------------------------------------------------------------------ -- ⁅_⁆ x∈⁅x⁆ : ∀ (x : Fin n) → x ∈ ⁅ x ⁆ x∈⁅x⁆ zero = here x∈⁅x⁆ (suc x) = there (x∈⁅x⁆ x) x∈⁅y⁆⇒x≡y : ∀ {x} (y : Fin n) → x ∈ ⁅ y ⁆ → x ≡ y x∈⁅y⁆⇒x≡y zero here = refl x∈⁅y⁆⇒x≡y zero (there p) = contradiction p ∉⊥ x∈⁅y⁆⇒x≡y (suc y) (there p) = cong suc (x∈⁅y⁆⇒x≡y y p) x∈⁅y⁆⇔x≡y : x ∈ ⁅ y ⁆ ⇔ x ≡ y x∈⁅y⁆⇔x≡y {x = x} {y = y} = mk⇔ (x∈⁅y⁆⇒x≡y y) (λ x≡y → subst (λ y → x ∈ ⁅ y ⁆) x≡y (x∈⁅x⁆ x)) x≢y⇒x∉⁅y⁆ : x ≢ y → x ∉ ⁅ y ⁆ x≢y⇒x∉⁅y⁆ x≢y = x≢y ∘ x∈⁅y⁆⇒x≡y _ x∉⁅y⁆⇒x≢y : x ∉ ⁅ y ⁆ → x ≢ y x∉⁅y⁆⇒x≢y x∉⁅x⁆ refl = x∉⁅x⁆ (x∈⁅x⁆ _) ∣⁅x⁆∣≡1 : ∀ (i : Fin n) → ∣ ⁅ i ⁆ ∣ ≡ 1 ∣⁅x⁆∣≡1 {suc n} zero = cong suc (∣⊥∣≡0 n) ∣⁅x⁆∣≡1 {_} (suc i) = ∣⁅x⁆∣≡1 i ------------------------------------------------------------------------ -- _⊆_ ⊆-refl : Reflexive {A = Subset n} _⊆_ ⊆-refl = id ⊆-reflexive : _≡_ {A = Subset n} ⇒ _⊆_ ⊆-reflexive refl = ⊆-refl ⊆-trans : Transitive {A = Subset n} _⊆_ ⊆-trans p⊆q q⊆r x∈p = q⊆r (p⊆q x∈p) ⊆-antisym : Antisymmetric {A = Subset n} _≡_ _⊆_ ⊆-antisym {i = []} {[]} p⊆q q⊆p = refl ⊆-antisym {i = x ∷ xs} {y ∷ ys} p⊆q q⊆p with x | y ... | inside | inside = cong₂ _∷_ refl (⊆-antisym (drop-∷-⊆ p⊆q) (drop-∷-⊆ q⊆p)) ... | inside | outside = contradiction (p⊆q here) λ() ... | outside | inside = contradiction (q⊆p here) λ() ... | outside | outside = cong₂ _∷_ refl (⊆-antisym (drop-∷-⊆ p⊆q) (drop-∷-⊆ q⊆p)) ⊆-min : Minimum {A = Subset n} _⊆_ ⊥ ⊆-min p = ⊥⊆ ⊆-max : Maximum {A = Subset n} _⊆_ ⊤ ⊆-max p = ⊆⊤ infix 4 _⊆?_ _⊆?_ : B.Decidable {A = Subset n} _⊆_ [] ⊆? [] = yes id outside ∷ p ⊆? y ∷ q = Dec.map out⊆-⇔ (p ⊆? q) inside ∷ p ⊆? outside ∷ q = no (λ p⊆q → case (p⊆q here) of λ()) inside ∷ p ⊆? inside ∷ q = Dec.map in⊆in-⇔ (p ⊆? q) module _ (n : ℕ) where ⊆-isPreorder : IsPreorder {A = Subset n} _≡_ _⊆_ ⊆-isPreorder = record { isEquivalence = isEquivalence ; reflexive = ⊆-reflexive ; trans = ⊆-trans } ⊆-isPartialOrder : IsPartialOrder {A = Subset n} _≡_ _⊆_ ⊆-isPartialOrder = record { isPreorder = ⊆-isPreorder ; antisym = ⊆-antisym } ⊆-preorder : Preorder _ _ _ ⊆-preorder = record { isPreorder = ⊆-isPreorder } ⊆-poset : Poset _ _ _ ⊆-poset = record { isPartialOrder = ⊆-isPartialOrder } p⊆q⇒∣p∣≤∣q∣ : p ⊆ q → ∣ p ∣ ≤ ∣ q ∣ p⊆q⇒∣p∣≤∣q∣ {p = []} {[]} p⊆q = z≤n p⊆q⇒∣p∣≤∣q∣ {p = outside ∷ p} {outside ∷ q} p⊆q = p⊆q⇒∣p∣≤∣q∣ (drop-∷-⊆ p⊆q) p⊆q⇒∣p∣≤∣q∣ {p = outside ∷ p} {inside ∷ q} p⊆q = ℕ.m≤n⇒m≤1+n (p⊆q⇒∣p∣≤∣q∣ (drop-∷-⊆ p⊆q)) p⊆q⇒∣p∣≤∣q∣ {p = inside ∷ p} {outside ∷ q} p⊆q = contradiction (p⊆q here) λ() p⊆q⇒∣p∣≤∣q∣ {p = inside ∷ p} {inside ∷ q} p⊆q = s≤s (p⊆q⇒∣p∣≤∣q∣ (drop-∷-⊆ p⊆q)) ------------------------------------------------------------------------ -- _⊂_ p⊂q⇒p⊆q : p ⊂ q → p ⊆ q p⊂q⇒p⊆q = proj₁ ⊂-trans : Transitive {A = Subset n} _⊂_ ⊂-trans (p⊆q , x , x∈q , x∉p) (q⊆r , _ , _ , _) = ⊆-trans p⊆q q⊆r , x , q⊆r x∈q , x∉p ⊂-⊆-trans : Trans {A = Subset n} _⊂_ _⊆_ _⊂_ ⊂-⊆-trans (p⊆q , x , x∈q , x∉p) q⊆r = ⊆-trans p⊆q q⊆r , x , q⊆r x∈q , x∉p ⊆-⊂-trans : Trans {A = Subset n} _⊆_ _⊂_ _⊂_ ⊆-⊂-trans p⊆q (q⊆r , x , x∈r , x∉q) = ⊆-trans p⊆q q⊆r , x , x∈r , x∉q ∘ p⊆q ⊂-irref : Irreflexive {A = Subset n} _≡_ _⊂_ ⊂-irref refl (_ , x , x∈p , x∉q) = contradiction x∈p x∉q ⊂-antisym : Antisymmetric {A = Subset n} _≡_ _⊂_ ⊂-antisym (p⊆q , _) (q⊆p , _) = ⊆-antisym p⊆q q⊆p ⊂-asymmetric : Asymmetric {A = Subset n} _⊂_ ⊂-asymmetric (p⊆q , _) (_ , x , x∈p , x∉q) = contradiction (p⊆q x∈p) x∉q infix 4 _⊂?_ _⊂?_ : B.Decidable {A = Subset n} _⊂_ [] ⊂? [] = no λ () outside ∷ p ⊂? outside ∷ q = Dec.map out⊂out-⇔ (p ⊂? q) outside ∷ p ⊂? inside ∷ q = Dec.map out⊂in-⇔ (p ⊆? q) inside ∷ p ⊂? outside ∷ q = no (λ {(p⊆q , _) → case (p⊆q here) of λ ()}) inside ∷ p ⊂? inside ∷ q = Dec.map in⊂in-⇔ (p ⊂? q) module _ (n : ℕ) where ⊂-isStrictPartialOrder : IsStrictPartialOrder {A = Subset n} _≡_ _⊂_ ⊂-isStrictPartialOrder = record { isEquivalence = isEquivalence ; irrefl = ⊂-irref ; trans = ⊂-trans ; <-resp-≈ = (λ {refl → id}) , (λ {refl → id}) } ⊂-isDecStrictPartialOrder : IsDecStrictPartialOrder {A = Subset n} _≡_ _⊂_ ⊂-isDecStrictPartialOrder = record { isStrictPartialOrder = ⊂-isStrictPartialOrder ; _≟_ = ≡-dec _≟_ ; _