{-# OPTIONS --cubical-compatible --safe #-}
module Data.Fin.Subset where
open import Algebra.Core using (Op₁; Op₂)
open import Data.Bool using (not; _∧_; _∨_; _≟_)
open import Data.Fin.Base using (Fin; zero; suc)
open import Data.List.Base using (List; foldr; foldl)
open import Data.Nat.Base using (ℕ)
open import Data.Product.Base using (∃; _×_)
open import Data.Vec.Base hiding (foldr; foldl)
open import Relation.Nullary
private
variable
n : ℕ
open import Data.Bool.Base public
using () renaming (Bool to Side; true to inside; false to outside)
Subset : ℕ → Set
Subset = Vec Side
⊥ : Subset n
⊥ = replicate _ outside
⊤ : Subset n
⊤ = replicate _ inside
⁅_⁆ : Fin n → Subset n
⁅ zero ⁆ = inside ∷ ⊥
⁅ suc i ⁆ = outside ∷ ⁅ i ⁆
infix 4 _∈_ _∉_ _⊆_ _⊈_ _⊂_ _⊄_
_∈_ : Fin n → Subset n → Set
x ∈ p = p [ x ]= inside
_∉_ : Fin n → Subset n → Set
x ∉ p = ¬ (x ∈ p)
_⊆_ : Subset n → Subset n → Set
p ⊆ q = ∀ {x} → x ∈ p → x ∈ q
_⊈_ : Subset n → Subset n → Set
p ⊈ q = ¬ (p ⊆ q)
_⊂_ : Subset n → Subset n → Set
p ⊂ q = p ⊆ q × ∃ (λ x → x ∈ q × x ∉ p)
_⊄_ : Subset n → Subset n → Set
p ⊄ q = ¬ (p ⊂ q)
infixr 7 _∩_
infixr 6 _∪_
infixl 5 _─_ _-_
∁ : Op₁ (Subset n)
∁ p = map not p
_∩_ : Op₂ (Subset n)
p ∩ q = zipWith _∧_ p q
_∪_ : Op₂ (Subset n)
p ∪ q = zipWith _∨_ p q
_─_ : Op₂ (Subset n)
p ─ q = zipWith diff p q
where
diff : Side → Side → Side
diff x inside = outside
diff x outside = x
⋃ : List (Subset n) → Subset n
⋃ = foldr _∪_ ⊥
⋂ : List (Subset n) → Subset n
⋂ = foldr _∩_ ⊤
_-_ : Subset n → Fin n → Subset n
p - x = p ─ ⁅ x ⁆
∣_∣ : Subset n → ℕ
∣ p ∣ = count (_≟ inside) p
Nonempty : ∀ (p : Subset n) → Set
Nonempty p = ∃ λ f → f ∈ p
Empty : ∀ (p : Subset n) → Set
Empty p = ¬ Nonempty p
Lift : ∀ {ℓ} → (Fin n → Set ℓ) → (Subset n → Set ℓ)
Lift P p = ∀ {x} → x ∈ p → P x