{-# OPTIONS --without-K --safe #-}
module Colist where
open import Conat using (Conat; zero; suc; force; infinity)
open import Equality.Propositional as E using (_≡_; refl)
open import Logical-equivalence using (_⇔_)
open import Prelude
open import Function-universe E.equality-with-J hiding (id; _∘_)
import Nat E.equality-with-J as Nat
mutual
infixr 5 _∷_
data Colist {a} (A : Set a) (i : Size) : Set a where
[] : Colist A i
_∷_ : A → Colist′ A i → Colist A i
record Colist′ {a} (A : Set a) (i : Size) : Set a where
coinductive
field
force : {j : Size< i} → Colist A j
open Colist′ public
infixr 5 _∷′_
_∷′_ : ∀ {i a} {A : Set a} → A → Colist A i → Colist A i
x ∷′ xs = x ∷ λ { .force → xs }
tail : ∀ {a} {A : Set a} {i} → Colist A i → Colist′ A i
tail xs@[] = λ { .force → xs }
tail (x ∷ xs) = xs
map : ∀ {a b i} {A : Set a} {B : Set b} →
(A → B) → Colist A i → Colist B i
map f [] = []
map f (x ∷ xs) = f x ∷ λ { .force → map f (force xs) }
length : ∀ {a i} {A : Set a} → Colist A i → Conat i
length [] = zero
length (n ∷ ns) = suc λ { .force → length (force ns) }
replicate : ∀ {a i} {A : Set a} → Conat i → A → Colist A i
replicate zero x = []
replicate (suc n) x = x ∷ λ { .force → replicate (force n) x }
repeat : ∀ {a i} {A : Set a} → A → Colist A i
repeat = replicate infinity
infixr 5 _++_
_++_ : ∀ {a i} {A : Set a} → Colist A i → Colist A i → Colist A i
[] ++ ys = ys
(x ∷ xs) ++ ys = x ∷ λ { .force → force xs ++ ys }
cycle : ∀ {a i} {A : Set a} → A → Colist′ A i → Colist A i
cycle x xs = x ∷ λ { .force → force xs ++ cycle x xs }
scanl : ∀ {a b i} {A : Set a} {B : Set b} →
(A → B → A) → A → Colist B i → Colist A i
scanl c n [] = n ∷ λ { .force → [] }
scanl c n (x ∷ xs) = n ∷ λ { .force → scanl c (c n x) (force xs) }
nats : ∀ {i} → Colist ℕ i
nats = 0 ∷ λ { .force → map suc nats }
nats-from : ∀ {i} → ℕ → Colist ℕ i
nats-from n = n ∷ λ { .force → nats-from (suc n) }
take : ∀ {a} {A : Set a} → ℕ → Colist A ∞ → List A
take zero _ = []
take _ [] = []
take (suc n) (x ∷ xs) = x ∷ take n (force xs)
module _ {a} {A : Set a} where
mutual
infix 4 [_]_∼_ [_]_∼′_
data [_]_∼_ (i : Size) : Colist A ∞ → Colist A ∞ → Set a where
[] : [ i ] [] ∼ []
_∷_ : ∀ {x y xs ys} →
x ≡ y → [ i ] force xs ∼′ force ys → [ i ] x ∷ xs ∼ y ∷ ys
record [_]_∼′_ (i : Size) (xs ys : Colist A ∞) : Set a where
coinductive
field
force : {j : Size< i} → [ j ] xs ∼ ys
open [_]_∼′_ public
reflexive-∼ : ∀ {i} xs → [ i ] xs ∼ xs
reflexive-∼ [] = []
reflexive-∼ (x ∷ xs) = refl ∷ λ { .force → reflexive-∼ (force xs) }
symmetric-∼ : ∀ {i xs ys} →
[ i ] xs ∼ ys → [ i ] ys ∼ xs
symmetric-∼ [] = []
symmetric-∼ (p₁ ∷ p₂) =
E.sym p₁ ∷ λ { .force → symmetric-∼ (force p₂) }
transitive-∼ : ∀ {i xs ys zs} →
[ i ] xs ∼ ys → [ i ] ys ∼ zs → [ i ] xs ∼ zs
transitive-∼ [] [] = []
transitive-∼ (p₁ ∷ p₂) (q₁ ∷ q₂) =
E.trans p₁ q₁ ∷ λ { .force → transitive-∼ (force p₂) (force q₂) }
infix -1 _∎
infixr -2 step-∼ step-≡ _∼⟨⟩_
_∎ : ∀ {i} xs → [ i ] xs ∼ xs
_∎ = reflexive-∼
step-∼ : ∀ {i} xs {ys zs} →
[ i ] ys ∼ zs → [ i ] xs ∼ ys → [ i ] xs ∼ zs
step-∼ _ ys∼zs xs∼ys = transitive-∼ xs∼ys ys∼zs
syntax step-∼ xs ys∼zs xs∼ys = xs ∼⟨ xs∼ys ⟩ ys∼zs
step-≡ : ∀ {i} xs {ys zs} → [ i ] ys ∼ zs → xs ≡ ys → [ i ] xs ∼ zs
step-≡ _ ys∼zs refl = ys∼zs
syntax step-≡ xs ys∼zs xs≡ys = xs ≡⟨ xs≡ys ⟩ ys∼zs
_∼⟨⟩_ : ∀ {i} xs {ys} → [ i ] xs ∼ ys → [ i ] xs ∼ ys
_ ∼⟨⟩ xs∼ys = xs∼ys
∷∼∷′ : ∀ {i} {x : A} {xs} →
[ i ] x ∷ xs ∼ x ∷′ force xs
∷∼∷′ = refl ∷ λ { .force → reflexive-∼ _ }
map-id :
∀ {a i} {A : Set a} (xs : Colist A ∞) →
[ i ] map id xs ∼ xs
map-id [] = []
map-id (_ ∷ xs) = refl ∷ λ { .force → map-id (force xs) }
map-∘ :
∀ {a b c i} {A : Set a} {B : Set b} {C : Set c}
{f : B → C} {g : A → B}
(xs : Colist A ∞) →
[ i ] map (f ∘ g) xs ∼ map f (map g xs)
map-∘ [] = []
map-∘ (_ ∷ xs) = refl ∷ λ { .force → map-∘ (force xs) }
head-cong : ∀ {a i} {A : Set a} {x y : A} {xs ys} →
[ i ] x ∷ xs ∼ y ∷ ys → x ≡ y
head-cong (p ∷ _) = p
tail-cong :
∀ {a i} {A : Set a} {xs ys : Colist A ∞} →
[ i ] xs ∼ ys → [ i ] force (tail xs) ∼′ force (tail ys)
tail-cong [] = λ { .force → [] }
tail-cong (_ ∷ ps) = ps
map-cong :
∀ {a b i} {A : Set a} {B : Set b} {f g : A → B} {xs ys} →
(∀ x → f x ≡ g x) → [ i ] xs ∼ ys → [ i ] map f xs ∼ map g ys
map-cong f≡g [] = []
map-cong f≡g (refl ∷ ps) =
f≡g _ ∷ λ { .force → map-cong f≡g (force ps) }
length-cong :
∀ {a i} {A : Set a} {xs ys : Colist A ∞} →
[ i ] xs ∼ ys → Conat.[ i ] length xs ∼ length ys
length-cong [] = zero
length-cong (_ ∷ ps) = suc λ { .force → length-cong (force ps) }
replicate-cong :
∀ {a i} {A : Set a} {m n} {x : A} →
Conat.[ i ] m ∼ n → [ i ] replicate m x ∼ replicate n x
replicate-cong zero = []
replicate-cong (suc p) = refl ∷ λ { .force → replicate-cong (force p) }
infixr 5 _++-cong_
_++-cong_ :
∀ {a i} {A : Set a} {xs₁ xs₂ ys₁ ys₂ : Colist A ∞} →
[ i ] xs₁ ∼ ys₁ → [ i ] xs₂ ∼ ys₂ → [ i ] xs₁ ++ xs₂ ∼ ys₁ ++ ys₂
[] ++-cong qs = qs
(p ∷ ps) ++-cong qs = p ∷ λ { .force → force ps ++-cong qs }
cycle-cong :
∀ {a i} {A : Set a} {x : A} {xs ys} →
[ i ] force xs ∼′ force ys → [ i ] cycle x xs ∼ cycle x ys
cycle-cong p = refl ∷ λ { .force → force p ++-cong cycle-cong p }
scanl-cong :
∀ {a b i} {A : Set a} {B : Set b} {c : A → B → A} {n : A} {xs ys} →
[ i ] xs ∼ ys → [ i ] scanl c n xs ∼ scanl c n ys
scanl-cong [] = refl ∷ λ { .force → [] }
scanl-cong (refl ∷ ps) = refl ∷ λ { .force → scanl-cong (force ps) }
take-cong :
∀ {a} {A : Set a} n {xs ys : Colist A ∞} →
[ ∞ ] xs ∼ ys → take n xs ≡ take n ys
take-cong n [] = refl
take-cong zero (p ∷ ps) = refl
take-cong (suc n) (p ∷ ps) = E.cong₂ _∷_ p (take-cong n (force ps))
length-replicate :
∀ {a i} {A : Set a} {x : A} n →
Conat.[ i ] length (replicate n x) ∼ n
length-replicate zero = zero
length-replicate (suc n) =
suc λ { .force → length-replicate (force n) }
map-+-nats∼nats-from :
∀ {i} n → [ i ] map (n +_) nats ∼ nats-from n
map-+-nats∼nats-from n = Nat.+-right-identity ∷ λ { .force →
map (n +_) (map suc nats) ∼⟨ symmetric-∼ (map-∘ _) ⟩
map ((n +_) ∘ suc) nats ∼⟨ map-cong (λ _ → E.sym (Nat.suc+≡+suc _)) (_ ∎) ⟩
map (suc n +_) nats ∼⟨ map-+-nats∼nats-from (suc n) ⟩
nats-from (suc n) ∎ }
nats∼nats-from-0 : ∀ {i} → [ i ] nats ∼ nats-from 0
nats∼nats-from-0 =
nats ∼⟨ symmetric-∼ (map-id _) ⟩
map id nats ∼⟨⟩
map (0 +_) nats ∼⟨ map-+-nats∼nats-from _ ⟩
nats-from 0 ∎
data ◇ {a p} {A : Set a} (i : Size)
(P : A → Set p) : Colist A ∞ → Set (a ⊔ p) where
here : ∀ {x xs} → P x → ◇ i P (x ∷ xs)
there : ∀ {x xs} {j : Size< i} → ◇ j P (force xs) → ◇ i P (x ∷ xs)
◇-∼ :
∀ {a p i} {A : Set a} {P : A → Set p} {xs ys} →
[ ∞ ] xs ∼ ys → ◇ i P xs → ◇ i P ys
◇-∼ (refl ∷ _) (here p) = here p
◇-∼ (_ ∷ b) (there p) = there (◇-∼ (force b) p)
◇-map : ∀ {a p q i} {A : Set a} {P : A → Set p} {Q : A → Set q} →
(∀ {x} → P x → Q x) →
(∀ {xs} → ◇ i P xs → ◇ i Q xs)
◇-map f (here p) = here (f p)
◇-map f (there p) = there (◇-map f p)
◇-map′ : ∀ {a b c p q i} {A : Set a} {B : Set b} {C : Set c}
{P : B → Set p} {Q : C → Set q}
{f : A → B} {g : A → C} →
(∀ {x} → P (f x) → Q (g x)) →
(∀ {xs} → ◇ i P (map f xs) → ◇ i Q (map g xs))
◇-map′ g {_ ∷ _} (here p) = here (g p)
◇-map′ g {_ ∷ _} (there p) = there (◇-map′ g p)
◇-map′ g {[]} ()
◇-witness : ∀ {a p i} {A : Set a} {P : A → Set p} {xs} →
◇ i P xs → ∃ P
◇-witness (here p) = _ , p
◇-witness (there p) = ◇-witness p
◇-const : ∀ {a p i} {A : Set a} {P : Set p} {xs : Colist A ∞} →
◇ i (const P) xs → P
◇-const = proj₂ ∘ ◇-witness
infix 4 [_]_∈_
[_]_∈_ : ∀ {a} {A : Set a} → Size → A → Colist A ∞ → Set a
[ i ] x ∈ xs = ◇ i (x ≡_) xs
◇⇔∈× : ∀ {a p i} {A : Set a} {P : A → Set p} {xs} →
◇ i P xs ⇔ ∃ λ x → [ i ] x ∈ xs × P x
◇⇔∈× {P = P} = record { to = to; from = from }
where
to : ∀ {i xs} → ◇ i P xs → ∃ λ x → [ i ] x ∈ xs × P x
to (here p) = _ , here refl , p
to (there p) = Σ-map id (Σ-map there id) (to p)
from : ∀ {i xs} → (∃ λ x → [ i ] x ∈ xs × P x) → ◇ i P xs
from (x , here refl , p) = here p
from (x , there x∈xs , p) = there (from (x , x∈xs , p))
◇-replicate-suc⇔ :
∀ {a p i} {A : Set a} {P : A → Set p} {x : A} {n} →
◇ i P (replicate (suc n) x) ⇔ P x
◇-replicate-suc⇔ {P = P} {x} = record
{ to = to _
; from = here
}
where
to : ∀ {i} n → ◇ i P (replicate n x) → P x
to zero ()
to (suc n) (here p) = p
to (suc n) (there p) = to (force n) p
◇-cycle⇔ :
∀ {a p i} {A : Set a} {P : A → Set p} {x : A} {xs} →
◇ i P (cycle x xs) ⇔ ◇ i P (x ∷ xs)
◇-cycle⇔ {i = i} {P = P} {x} {xs} = record
{ to = ◇ i P (cycle x xs) ↝⟨ ◇-∼ (transitive-∼ ∷∼∷′ (symmetric-∼ ∷∼∷′)) ⟩
◇ i P ((x ∷ xs) ++ cycle x xs) ↝⟨ to _ ⟩
◇ i P (x ∷ xs) ⊎ ◇ i P (x ∷ xs) ↝⟨ [ id , id ] ⟩□
◇ i P (x ∷ xs) □
; from = ◇ i P (x ∷ xs) ↝⟨ from ⟩
◇ i P ((x ∷ xs) ++ cycle x xs) ↝⟨ ◇-∼ (transitive-∼ ∷∼∷′ (symmetric-∼ ∷∼∷′)) ⟩□
◇ i P (cycle x xs) □
}
where
to : ∀ {i} ys → ◇ i P (ys ++ cycle x xs) → ◇ i P ys ⊎ ◇ i P (x ∷ xs)
to [] (here p) = inj₂ (here p)
to [] (there p) = case to (force xs) p of
[ inj₂ ∘ there , inj₂ ]
to (y ∷ ys) (here p) = inj₁ (here p)
to (y ∷ ys) (there p) = ⊎-map there id (to (force ys) p)
from : ∀ {i ys} → ◇ i P ys → ◇ i P (ys ++ cycle x xs)
from (here p) = here p
from (there ps) = there (from ps)
mutual
data □ {a p} {A : Set a} (i : Size)
(P : A → Set p) : Colist A ∞ → Set (a ⊔ p) where
[] : □ i P []
_∷_ : ∀ {x xs} → P x → □′ i P (force xs) → □ i P (x ∷ xs)
record □′ {a p} {A : Set a} (i : Size)
(P : A → Set p) (xs : Colist A ∞) : Set (a ⊔ p) where
coinductive
field
force : {j : Size< i} → □ j P xs
open □′ public
□-head : ∀ {a p i} {A : Set a} {P : A → Set p} {x xs} →
□ i P (x ∷ xs) → P x
□-head (p ∷ _) = p
□-tail : ∀ {a p i} {j : Size< i} {A : Set a} {P : A → Set p} {x xs} →
□ i P (x ∷ xs) → □ j P (force xs)
□-tail (_ ∷ ps) = force ps
□-∼ :
∀ {i a p} {A : Set a} {P : A → Set p} {xs ys} →
[ i ] xs ∼ ys → □ i P xs → □ i P ys
□-∼ [] _ = []
□-∼ (refl ∷ b) (p ∷ ps) = p ∷ λ { .force → □-∼ (force b) (force ps) }
□⇔∈→ : ∀ {a p i} {A : Set a} {P : A → Set p} {xs} →
□ i P xs ⇔ (∀ x → [ i ] x ∈ xs → P x)
□⇔∈→ {P = P} = record { to = to; from = from _ }
where
to : ∀ {i xs} → □ i P xs → (∀ x → [ i ] x ∈ xs → P x)
to (p ∷ ps) x (here refl) = p
to (p ∷ ps) x (there x∈xs) = to (force ps) x x∈xs
from : ∀ {i} xs → (∀ x → [ i ] x ∈ xs → P x) → □ i P xs
from [] f = []
from (x ∷ xs) f =
f x (here refl) ∷ λ { .force → from (force xs) (λ x → f x ∘ there) }
□-replicate : ∀ {a p i} {A : Set a} {P : A → Set p} →
(∀ x → P x) →
(∀ xs → □ i P xs)
□-replicate f _ = _⇔_.from □⇔∈→ (λ x _ → f x)
infixl 4 _□-⊛_
_□-⊛_ : ∀ {i a p q} {A : Set a} {P : A → Set p} {Q : A → Set q} {xs} →
□ i (λ x → P x → Q x) xs → □ i P xs → □ i Q xs
[] □-⊛ _ = []
(f ∷ fs) □-⊛ (p ∷ ps) = f p ∷ λ { .force → force fs □-⊛ force ps }
□-map : ∀ {a p q i} {A : Set a} {P : A → Set p} {Q : A → Set q} →
(∀ {x} → P x → Q x) →
(∀ {xs} → □ i P xs → □ i Q xs)
□-map f ps = □-replicate (λ _ → f) _ □-⊛ ps
□-map′ : ∀ {a b c p q i} {A : Set a} {B : Set b} {C : Set c}
{P : B → Set p} {Q : C → Set q}
{f : A → B} {g : A → C} →
(∀ {x} → P (f x) → Q (g x)) →
(∀ {xs} → □ i P (map f xs) → □ i Q (map g xs))
□-map′ g {[]} [] = []
□-map′ g {_ ∷ _} (p ∷ ps) = g p ∷ λ { .force → □-map′ g (force ps) }
infixl 4 _□◇-⊛_
_□◇-⊛_ : ∀ {a p q i} {A : Set a} {P : A → Set p} {Q : A → Set q} {xs} →
□ i (λ x → P x → Q x) xs → ◇ i P xs → ◇ i Q xs
(f ∷ _) □◇-⊛ (here p) = here (f p)
(_ ∷ fs) □◇-⊛ (there p) = there (force fs □◇-⊛ p)
□◇-witness :
∀ {a p q i} {A : Set a} {P : A → Set p} {Q : A → Set q} {xs} →
□ i P xs → ◇ i Q xs → ∃ λ x → P x × Q x
□◇-witness p q = ◇-witness (□-map _,_ p □◇-⊛ q)
□-replicate-suc⇔ :
∀ {a p i} {A : Set a} {P : A → Set p} {x : A} {n} →
□ i P (replicate (suc n) x) ⇔ P x
□-replicate-suc⇔ {P = P} {x} = record
{ to = □-head
; from = from _
}
where
from : ∀ {i} n → P x → □ i P (replicate n x)
from zero p = []
from (suc n) p = p ∷ λ { .force → from (force n) p }
□-cycle⇔ :
∀ {a p i} {A : Set a} {P : A → Set p} {x : A} {xs} →
□ i P (cycle x xs) ⇔ □ i P (x ∷ xs)
□-cycle⇔ {i = i} {P = P} {x} {xs} = record
{ to = □ i P (cycle x xs) ↝⟨ (λ { (p ∷ ps) → p ∷ ps }) ⟩
□ i P ((x ∷ xs) ++ cycle x xs) ↝⟨ to _ ⟩□
□ i P (x ∷ xs) □
; from = □ i P (x ∷ xs) ↝⟨ (λ hyp → from hyp hyp) ⟩
□ i P ((x ∷ xs) ++ cycle x xs) ↝⟨ (λ { (p ∷ ps) → p ∷ ps }) ⟩□
□ i P (cycle x xs) □
}
where
to : ∀ {i} ys → □ i P (ys ++ cycle x xs) → □ i P ys
to [] _ = []
to (y ∷ ys) (p ∷ ps) = p ∷ λ { .force → to (force ys) (force ps) }
from : ∀ {i ys} →
□ i P (x ∷ xs) → □ i P ys → □ i P (ys ++ cycle x xs)
from ps (q ∷ qs) = q ∷ λ { .force → from ps (force qs) }
from (p ∷ ps) [] = p ∷ λ { .force → from (p ∷ ps) (force ps) }
data ◇ˢ {a p} {A : Set a} (i : Size)
(P : Size → A → Set p) : Colist A ∞ → Set (a ⊔ p) where
here : ∀ {x xs} → P i x → ◇ˢ i P (x ∷ xs)
there : ∀ {x xs} {j : Size< i} → ◇ˢ j P (force xs) → ◇ˢ i P (x ∷ xs)
◇ˢ-∼ :
∀ {a p i} {A : Set a} {P : Size → A → Set p} {xs ys} →
[ ∞ ] xs ∼ ys → ◇ˢ i P xs → ◇ˢ i P ys
◇ˢ-∼ (refl ∷ _) (here p) = here p
◇ˢ-∼ (_ ∷ b) (there p) = there (◇ˢ-∼ (force b) p)
◇ˢ-upwards-closed :
∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i} {j : Size< i} {x} → P j x → P i x) →
(∀ {i} {j : Size< i} {xs} → ◇ˢ j P xs → ◇ˢ i P xs)
◇ˢ-upwards-closed P-closed (here p) = here (P-closed p)
◇ˢ-upwards-closed P-closed (there p) =
there (◇ˢ-upwards-closed P-closed p)
◇ˢ-upwards-closed-∞ :
∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i x} → P i x → P ∞ x) →
(∀ {i xs} → ◇ˢ i P xs → ◇ˢ ∞ P xs)
◇ˢ-upwards-closed-∞ P-closed-∞ (here p) = here (P-closed-∞ p)
◇ˢ-upwards-closed-∞ P-closed-∞ (there p) =
there (◇ˢ-upwards-closed-∞ P-closed-∞ p)
◇ˢ-map : ∀ {a p q i}
{A : Set a} {P : Size → A → Set p} {Q : Size → A → Set q} →
(∀ {i x} → P i x → Q i x) →
(∀ {xs} → ◇ˢ i P xs → ◇ˢ i Q xs)
◇ˢ-map f (here p) = here (f p)
◇ˢ-map f (there p) = there (◇ˢ-map f p)
◇ˢ-map′ : ∀ {a b c p q i} {A : Set a} {B : Set b} {C : Set c}
{P : Size → B → Set p} {Q : Size → C → Set q}
{f : A → B} {g : A → C} →
(∀ {i x} → P i (f x) → Q i (g x)) →
(∀ {xs} → ◇ˢ i P (map f xs) → ◇ˢ i Q (map g xs))
◇ˢ-map′ g {_ ∷ _} (here p) = here (g p)
◇ˢ-map′ g {_ ∷ _} (there p) = there (◇ˢ-map′ g p)
◇ˢ-map′ g {[]} ()
◇ˢ-witness : ∀ {a p i} {A : Set a} {P : Size → A → Set p} {xs} →
(∀ {i} {j : Size< i} {x} → P j x → P i x) →
◇ˢ i P xs → ∃ (P i)
◇ˢ-witness closed (here p) = _ , p
◇ˢ-witness closed (there p) = Σ-map id closed (◇ˢ-witness closed p)
∈×∞→◇ˢ : ∀ {a p} {A : Set a} {P : Size → A → Set p} {x xs} →
[ ∞ ] x ∈ xs → P ∞ x → ◇ˢ ∞ P xs
∈×∞→◇ˢ (here refl) p = here p
∈×∞→◇ˢ (there x∈xs) p = there (∈×∞→◇ˢ x∈xs p)
◇ˢ⇔∈× : ∀ {a p} {A : Set a} {P : Size → A → Set p} {xs} →
(∀ {i x} → P i x → P ∞ x) →
◇ˢ ∞ P xs ⇔ (∃ λ x → [ ∞ ] x ∈ xs × P ∞ x)
◇ˢ⇔∈× {P = P} →∞ = record
{ to = to
; from = λ { (_ , x∈xs , p) → ∈×∞→◇ˢ x∈xs p }
}
where
to : ∀ {i xs} → ◇ˢ i P xs → ∃ λ x → [ ∞ ] x ∈ xs × P ∞ x
to (here p) = _ , here refl , →∞ p
to (there p) = Σ-map id (Σ-map there id) (to p)
◇ˢ→∈× : ∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i} {j : Size< i} {x} → P j x → P i x) →
∀ {i xs} → ◇ˢ i P xs → ∃ λ x → [ i ] x ∈ xs × P i x
◇ˢ→∈× closed (here p) = _ , here refl , p
◇ˢ→∈× closed (there p) = Σ-map id (Σ-map there closed) (◇ˢ→∈× closed p)
∈×→◇ˢ : ∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i} {j : Size< i} {x} → P i x → P j x) →
∀ {i x xs} → [ i ] x ∈ xs → P i x → ◇ˢ i P xs
∈×→◇ˢ closed (here refl) p = here p
∈×→◇ˢ closed (there x∈xs) p = there (∈×→◇ˢ closed x∈xs (closed p))
◇∞→◇ˢ∞ : ∀ {a p} {A : Set a} {P : Size → A → Set p} {xs} →
◇ ∞ (P ∞) xs → ◇ˢ ∞ P xs
◇∞→◇ˢ∞ {P = P} {xs} =
◇ ∞ (P ∞) xs ↝⟨ _⇔_.to ◇⇔∈× ⟩
(∃ λ x → [ ∞ ] x ∈ xs × P ∞ x) ↝⟨ (λ { (_ , x∈xs , p) → ∈×∞→◇ˢ x∈xs p }) ⟩□
◇ˢ ∞ P xs □
◇ˢ∞⇔◇∞ : ∀ {a p} {A : Set a} {P : Size → A → Set p} {xs} →
(∀ {i x} → P i x → P ∞ x) →
◇ˢ ∞ P xs ⇔ ◇ ∞ (P ∞) xs
◇ˢ∞⇔◇∞ {P = P} {xs} →∞ =
◇ˢ ∞ P xs ↝⟨ ◇ˢ⇔∈× →∞ ⟩
(∃ λ x → [ ∞ ] x ∈ xs × P ∞ x) ↝⟨ inverse ◇⇔∈× ⟩□
◇ ∞ (P ∞) xs □
◇ˢ⇔◇ : ∀ {a p i} {A : Set a} {P : A → Set p} {xs} →
◇ˢ i (const P) xs ⇔ ◇ i P xs
◇ˢ⇔◇ {P = P} {xs} = record { to = to; from = from }
where
to : ∀ {i xs} → ◇ˢ i (const P) xs → ◇ i P xs
to (here p) = here p
to (there p) = there (to p)
from : ∀ {i xs} → ◇ i P xs → ◇ˢ i (const P) xs
from (here p) = here p
from (there p) = there (from p)
◇ˢ-cycle← :
∀ {a p i} {A : Set a} {P : Size → A → Set p} {x : A} {xs} →
◇ˢ i P (x ∷ xs) → ◇ˢ i P (cycle x xs)
◇ˢ-cycle← {i = i} {P = P} {x} {xs} =
◇ˢ i P (x ∷ xs) ↝⟨ from ⟩
◇ˢ i P ((x ∷ xs) ++ cycle x xs) ↝⟨ ◇ˢ-∼ (transitive-∼ ∷∼∷′ (symmetric-∼ ∷∼∷′)) ⟩□
◇ˢ i P (cycle x xs) □
where
from : ∀ {i ys} → ◇ˢ i P ys → ◇ˢ i P (ys ++ cycle x xs)
from (here p) = here p
from (there ps) = there (from ps)
◇ˢ-cycle⇔ :
∀ {a p} {A : Set a} {P : Size → A → Set p} {x : A} {xs} →
(∀ {i x} → P i x → P ∞ x) →
◇ˢ ∞ P (cycle x xs) ⇔ ◇ˢ ∞ P (x ∷ xs)
◇ˢ-cycle⇔ {P = P} {x} {xs} →∞ = record
{ to = ◇ˢ ∞ P (cycle x xs) ↝⟨ ◇ˢ-∼ (transitive-∼ ∷∼∷′ (symmetric-∼ ∷∼∷′)) ⟩
◇ˢ ∞ P ((x ∷ xs) ++ cycle x xs) ↝⟨ to _ ⟩
◇ˢ ∞ P (x ∷ xs) ⊎ ◇ˢ ∞ P (x ∷ xs) ↝⟨ [ id , id ] ⟩□
◇ˢ ∞ P (x ∷ xs) □
; from = ◇ˢ-cycle←
}
where
to :
∀ {i} ys → ◇ˢ i P (ys ++ cycle x xs) → ◇ˢ ∞ P ys ⊎ ◇ˢ ∞ P (x ∷ xs)
to [] (here p) = inj₂ (here (→∞ p))
to [] (there p) = case to (force xs) p of
[ inj₂ ∘ there , inj₂ ]
to (y ∷ ys) (here p) = inj₁ (here (→∞ p))
to (y ∷ ys) (there p) = ⊎-map there id (to (force ys) p)
mutual
data □ˢ {a p} {A : Set a} (i : Size)
(P : Size → A → Set p) : Colist A ∞ → Set (a ⊔ p) where
[] : □ˢ i P []
_∷_ : ∀ {x xs} → P i x → □ˢ′ i P (force xs) → □ˢ i P (x ∷ xs)
record □ˢ′ {a p} {A : Set a} (i : Size)
(P : Size → A → Set p) (xs : Colist A ∞) : Set (a ⊔ p) where
coinductive
field
force : {j : Size< i} → □ˢ j P xs
open □ˢ′ public
□ˢ-head : ∀ {a p i} {A : Set a} {P : Size → A → Set p} {x xs} →
□ˢ i P (x ∷ xs) → P i x
□ˢ-head (p ∷ _) = p
□ˢ-tail : ∀ {a p i} {j : Size< i}
{A : Set a} {P : Size → A → Set p} {x xs} →
□ˢ i P (x ∷ xs) → □ˢ j P (force xs)
□ˢ-tail (_ ∷ ps) = force ps
□ˢ-∼ :
∀ {i a p} {A : Set a} {P : Size → A → Set p} {xs ys} →
[ i ] xs ∼ ys → □ˢ i P xs → □ˢ i P ys
□ˢ-∼ [] _ = []
□ˢ-∼ (refl ∷ b) (p ∷ ps) = p ∷ λ { .force → □ˢ-∼ (force b) (force ps) }
□ˢ-downwards-closed :
∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i} {j : Size< i} {x} → P i x → P j x) →
(∀ {i} {j : Size< i} {xs} → □ˢ i P xs → □ˢ j P xs)
□ˢ-downwards-closed P-closed [] = []
□ˢ-downwards-closed P-closed (p ∷ ps) =
P-closed p ∷ λ { .force → □ˢ-downwards-closed P-closed (force ps) }
□ˢ-downwards-closed-∞ :
∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i x} → P ∞ x → P i x) →
(∀ {i xs} → □ˢ ∞ P xs → □ˢ i P xs)
□ˢ-downwards-closed-∞ P-closed-∞ [] = []
□ˢ-downwards-closed-∞ P-closed-∞ (p ∷ ps) =
P-closed-∞ p ∷ λ { .force →
□ˢ-downwards-closed-∞ P-closed-∞ (force ps) }
□ˢ∞∈→ : ∀ {a p} {A : Set a} {P : Size → A → Set p} {xs x} →
□ˢ ∞ P xs → [ ∞ ] x ∈ xs → P ∞ x
□ˢ∞∈→ (p ∷ ps) (here refl) = p
□ˢ∞∈→ (p ∷ ps) (there x∈xs) = □ˢ∞∈→ (force ps) x∈xs
□ˢ⇔∈→ : ∀ {a p} {A : Set a} {P : Size → A → Set p} {xs} →
(∀ {i x} → P ∞ x → P i x) →
□ˢ ∞ P xs ⇔ (∀ x → [ ∞ ] x ∈ xs → P ∞ x)
□ˢ⇔∈→ {P = P} ∞→ = record { to = λ p _ → □ˢ∞∈→ p; from = from _ }
where
from : ∀ {i} xs → (∀ x → [ ∞ ] x ∈ xs → P ∞ x) → □ˢ i P xs
from [] f = []
from (x ∷ xs) f =
∞→ (f x (here refl)) ∷ λ { .force →
from (force xs) (λ x → f x ∘ there) }
□ˢ∈→ : ∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i} {j : Size< i} {x} → P j x → P i x) →
∀ {i x xs} → □ˢ i P xs → [ i ] x ∈ xs → P i x
□ˢ∈→ closed (p ∷ ps) (here refl) = p
□ˢ∈→ closed (p ∷ ps) (there x∈xs) = closed (□ˢ∈→ closed (force ps) x∈xs)
∈→→□ˢ : ∀ {a p} {A : Set a} {P : Size → A → Set p} →
(∀ {i} {j : Size< i} {x} → P i x → P j x) →
∀ {i} xs → (∀ x → [ i ] x ∈ xs → P i x) → □ˢ i P xs
∈→→□ˢ closed [] f = []
∈→→□ˢ closed (x ∷ xs) f =
f x (here refl) ∷ λ { .force →
∈→→□ˢ closed (force xs) (λ x → closed ∘ f x ∘ there) }
□ˢ∞→□∞ : ∀ {a p} {A : Set a} {P : Size → A → Set p} {xs} →
□ˢ ∞ P xs → □ ∞ (P ∞) xs
□ˢ∞→□∞ {P = P} {xs} =
□ˢ ∞ P xs ↝⟨ (λ p _ → □ˢ∞∈→ p) ⟩
(∀ x → [ ∞ ] x ∈ xs → P ∞ x) ↝⟨ _⇔_.from □⇔∈→ ⟩□
□ ∞ (P ∞) xs □
□ˢ∞⇔□∞ : ∀ {a p} {A : Set a} {P : Size → A → Set p} {xs} →
(∀ {i x} → P ∞ x → P i x) →
□ˢ ∞ P xs ⇔ □ ∞ (P ∞) xs
□ˢ∞⇔□∞ {P = P} {xs} ∞→ =
□ˢ ∞ P xs ↝⟨ □ˢ⇔∈→ ∞→ ⟩
(∀ x → [ ∞ ] x ∈ xs → P ∞ x) ↝⟨ inverse □⇔∈→ ⟩□
□ ∞ (P ∞) xs □
□ˢ⇔□ : ∀ {a p i} {A : Set a} {P : A → Set p} {xs} →
□ˢ i (const P) xs ⇔ □ i P xs
□ˢ⇔□ {P = P} {xs} = record { to = to; from = from }
where
to : ∀ {i xs} → □ˢ i (const P) xs → □ i P xs
to [] = []
to (p ∷ ps) = p ∷ λ { .force → to (force ps) }
from : ∀ {i xs} → □ i P xs → □ˢ i (const P) xs
from [] = []
from (p ∷ ps) = p ∷ λ { .force → from (force ps) }
□ˢ-replicate : ∀ {a p i} {A : Set a} {P : Size → A → Set p} →
(∀ {i} x → P i x) →
(∀ xs → □ˢ i P xs)
□ˢ-replicate f [] = []
□ˢ-replicate f (x ∷ xs) = f x ∷ λ { .force → □ˢ-replicate f (force xs) }
infixl 4 _□ˢ-⊛_
_□ˢ-⊛_ : ∀ {i a p q} {A : Set a}
{P : Size → A → Set p} {Q : Size → A → Set q} {xs} →
□ˢ i (λ j x → P j x → Q j x) xs → □ˢ i P xs → □ˢ i Q xs
[] □ˢ-⊛ _ = []
(f ∷ fs) □ˢ-⊛ (p ∷ ps) = f p ∷ λ { .force → force fs □ˢ-⊛ force ps }
□ˢ-map : ∀ {a p q i}
{A : Set a} {P : Size → A → Set p} {Q : Size → A → Set q} →
(∀ {i x} → P i x → Q i x) →
(∀ {xs} → □ˢ i P xs → □ˢ i Q xs)
□ˢ-map f ps = □ˢ-replicate (λ _ → f) _ □ˢ-⊛ ps
□ˢ-map′ : ∀ {a b c p q i} {A : Set a} {B : Set b} {C : Set c}
{P : Size → B → Set p} {Q : Size → C → Set q}
{f : A → B} {g : A → C} →
(∀ {i x} → P i (f x) → Q i (g x)) →
(∀ {xs} → □ˢ i P (map f xs) → □ˢ i Q (map g xs))
□ˢ-map′ g {[]} [] = []
□ˢ-map′ g {_ ∷ _} (p ∷ ps) = g p ∷ λ { .force → □ˢ-map′ g (force ps) }
infixl 4 _□ˢ◇ˢ-⊛_
_□ˢ◇ˢ-⊛_ : ∀ {a p q i} {A : Set a}
{P : Size → A → Set p} {Q : Size → A → Set q} {xs} →
□ˢ i (λ j x → P j x → Q j x) xs → ◇ˢ i P xs → ◇ˢ i Q xs
(f ∷ _) □ˢ◇ˢ-⊛ (here p) = here (f p)
(_ ∷ fs) □ˢ◇ˢ-⊛ (there p) = there (force fs □ˢ◇ˢ-⊛ p)
□ˢ◇ˢ-witness :
∀ {a p q i}
{A : Set a} {P : Size → A → Set p} {Q : Size → A → Set q} {xs} →
(∀ {i} {j : Size< i} {x} → P j x → P i x) →
(∀ {i} {j : Size< i} {x} → Q j x → Q i x) →
□ˢ i P xs → ◇ˢ i Q xs → ∃ λ x → P i x × Q i x
□ˢ◇ˢ-witness P-closed Q-closed p q =
◇ˢ-witness (λ { {_} {_} → Σ-map P-closed Q-closed })
(□ˢ-map _,_ p □ˢ◇ˢ-⊛ q)
□ˢ-cycle→ :
∀ {a p i} {A : Set a} {P : Size → A → Set p} {x : A} {xs} →
□ˢ i P (cycle x xs) → □ˢ i P (x ∷ xs)
□ˢ-cycle→ {i = i} {P = P} {x} {xs} =
□ˢ i P (cycle x xs) ↝⟨ (λ { (p ∷ ps) → p ∷ ps }) ⟩
□ˢ i P ((x ∷ xs) ++ cycle x xs) ↝⟨ to _ ⟩□
□ˢ i P (x ∷ xs) □
where
to : ∀ {i} ys → □ˢ i P (ys ++ cycle x xs) → □ˢ i P ys
to [] _ = []
to (y ∷ ys) (p ∷ ps) = p ∷ λ { .force → to (force ys) (force ps) }
□ˢ-cycle⇔ :
∀ {a p} {A : Set a} {P : Size → A → Set p} {x : A} {xs} →
(∀ {i x} → P ∞ x → P i x) →
□ˢ ∞ P (cycle x xs) ⇔ □ˢ ∞ P (x ∷ xs)
□ˢ-cycle⇔ {P = P} {x} {xs} ∞→ = record
{ to = □ˢ-cycle→
; from = □ˢ ∞ P (x ∷ xs) ↝⟨ (λ hyp → from hyp hyp) ⟩
□ˢ ∞ P ((x ∷ xs) ++ cycle x xs) ↝⟨ (λ { (p ∷ ps) → p ∷ ps }) ⟩□
□ˢ ∞ P (cycle x xs) □
}
where
from : ∀ {i ys} →
□ˢ ∞ P (x ∷ xs) → □ˢ i P ys → □ˢ i P (ys ++ cycle x xs)
from ps (q ∷ qs) = q ∷ λ { .force → from ps (force qs) }
from (p ∷ ps) [] = ∞→ p ∷ λ { .force →
from (p ∷ ps) (force ps) }