text
stringlengths
4
690k
-- Decidability in the context of types means that it is possible to decide whether a type is empty or have inhabitants. -- In the case of it having inhabitants, it is possible to extract a witness of the type. -- Note: When using the types as propositions interpretation of types as a logic system, decidability is equivalent to excluded middle. module Type.Properties.Decidable where open import Data open import Data.Tuple.RaiseTypeᵣ import Lvl import Lvl.MultiFunctions as Lvl open import Data.Boolean using (Bool ; 𝑇 ; 𝐹) open import Function.Multi open import Logic.Predicate open import Numeral.Natural open import Type private variable ℓ ℓₚ : Lvl.Level private variable A B C P Q : Type{ℓ} -- Decider₀(P)(b) states that the boolean value b decides whether the type P is empty or inhabited. -- When interpreting P as a proposition, the boolean value b decides the truth value of P. -- Base case of Decider. data Decider₀ {ℓ} (P : Type{ℓ}) : Bool → Type{Lvl.𝐒(ℓ)} where true : P → Decider₀(P)(𝑇) false : (P → Empty{Lvl.𝟎}) → Decider₀(P)(𝐹) -- Elimination rule of Decider₀. elim : ∀{P} → (Proof : ∀{b} → Decider₀{ℓₚ}(P)(b) → Type{ℓ}) → ((p : P) → Proof(true p)) → ((np : P → Empty) → Proof(false np)) → (∀{b} → (d : Decider₀(P)(b)) → Proof(d)) elim _ fp fnp (true p) = fp p elim _ fp fnp (false np) = fnp np step : ∀{f : Bool → Bool} → (P → Decider₀(Q)(f(𝑇))) → ((P → Empty) → Decider₀(Q)(f(𝐹))) → (∀{b} → Decider₀(P)(b) → Decider₀(Q)(f(b))) step{f = f} = elim (\{b} _ → Decider₀(_)(f(b))) -- Decider(n)(P)(f) states that the values of the n-ary function f decides whether the values of P is empty or inhabited for a given number of arguments. -- When interpreting P as an n-ary predicate (proposition), the n-ary function f decides the truth values of P. Decider : (n : ℕ) → ∀{ℓ}{ℓ𝓈}{As : Types{n}(ℓ𝓈)} → (As ⇉ Type{ℓ}) → (As ⇉ Bool) → Type{Lvl.𝐒(ℓ) Lvl.⊔ Lvl.⨆(ℓ𝓈)} Decider(0) = Decider₀ Decider(1) P f = ∀{x} → Decider₀(P(x))(f(x)) Decider(𝐒(𝐒(n))) P f = ∀{x} → Decider(𝐒(n))(P(x))(f(x)) -- Whether there is a boolean decider for the inhabitedness of a type function. Decidable : (n : ℕ) → ∀{ℓ}{ℓ𝓈}{As : Types{n}(ℓ𝓈)} → (As ⇉ Type{ℓ}) → Type Decidable(n)(P) = ∃(Decider(n)(P)) -- The boolean n-ary inhabitedness decider function for a decidable n-ary type function. decide : (n : ℕ) → ∀{ℓ}{ℓ𝓈}{As : Types{n}(ℓ𝓈)} → (P : As ⇉ Type{ℓ}) → ⦃ dec : Decidable(n)(P) ⦄ → (As ⇉ Bool) decide(_)(_) ⦃ dec ⦄ = [∃]-witness dec
module logic where open import empty public open import eq public open import level public open import negation public open import neq public open import product public open import sum public open import unit public
module EtaBug where record ⊤ : Set where data ⊥ : Set where data Bool : Set where true : Bool false : Bool T : Bool → Set T true = ⊤ T false = ⊥ record ∃ {A : Set} (B : A → Set) : Set where constructor _,_ field proj₁ : A proj₂ : B proj₁ postulate G : Set → Set D : ∀ {A} → G A → A → Set P : {A : Set} → G A → Set P g = ∀ x → D g x postulate f : ∀ {A} {g : G A} → P g → P g g : (p : ⊥ → Bool) → G (∃ λ t → T (p t)) h : ∀ {p : ⊥ → Bool} {t pt} → D (g p) (t , pt) p : ⊥ → Bool works : P (g p) works = f (λ _ → h {p = p}) fails : P (g p) fails = f (λ _ → h)
------------------------------------------------------------------------ -- Isomorphisms and equalities relating an arbitrary "equality with J" -- to path equality, along with a proof of extensionality for the -- "equality with J" ------------------------------------------------------------------------ {-# OPTIONS --erased-cubical --safe #-} import Equality.Path as P module Equality.Path.Isomorphisms {e⁺} (eq : ∀ {a p} → P.Equality-with-paths a p e⁺) where open P.Derived-definitions-and-properties eq open import Prelude import Bijection import Embedding import Equivalence import Equivalence.Contractible-preimages import Equivalence.Half-adjoint import Function-universe import H-level import Surjection import Univalence-axiom private module B = Bijection equality-with-J module CP = Equivalence.Contractible-preimages equality-with-J module HA = Equivalence.Half-adjoint equality-with-J module Eq = Equivalence equality-with-J module F = Function-universe equality-with-J module PB = Bijection P.equality-with-J module PM = Embedding P.equality-with-J module PE = Equivalence P.equality-with-J module PCP = Equivalence.Contractible-preimages P.equality-with-J module PHA = Equivalence.Half-adjoint P.equality-with-J module PF = Function-universe P.equality-with-J module PH = H-level P.equality-with-J module PS = Surjection P.equality-with-J module PU = Univalence-axiom P.equality-with-J open B using (_↔_) open Embedding equality-with-J hiding (id; _∘_) open Eq using (_≃_; Is-equivalence) open F hiding (id; _∘_) open H-level equality-with-J open Surjection equality-with-J using (_↠_) open Univalence-axiom equality-with-J private variable a b c p q ℓ : Level A : Type a B : A → Type b u v x y z : A f g h : (x : A) → B x n : ℕ ------------------------------------------------------------------------ -- Extensionality -- The proof bad-ext is perhaps not less "good" than ext (I don't -- know), it is called this simply because it is not defined using -- good-ext. bad-ext : Extensionality a b apply-ext bad-ext {f = f} {g = g} = (∀ x → f x ≡ g x) ↝⟨ (λ p x → _↔_.to ≡↔≡ (p x)) ⟩ (∀ x → f x P.≡ g x) ↝⟨ P.apply-ext P.ext ⟩ f P.≡ g ↔⟨ inverse ≡↔≡ ⟩□ f ≡ g □ -- Extensionality. ext : Extensionality a b ext = Eq.good-ext bad-ext ⟨ext⟩ : Extensionality′ A B ⟨ext⟩ = apply-ext ext abstract -- The function ⟨ext⟩ is an equivalence. ext-is-equivalence : Is-equivalence {A = ∀ x → f x ≡ g x} ⟨ext⟩ ext-is-equivalence = Eq.good-ext-is-equivalence bad-ext -- Equality rearrangement lemmas for ⟨ext⟩. ext-refl : ⟨ext⟩ (λ x → refl (f x)) ≡ refl f ext-refl = Eq.good-ext-refl bad-ext _ ext-const : (x≡y : x ≡ y) → ⟨ext⟩ (const {B = A} x≡y) ≡ cong const x≡y ext-const = Eq.good-ext-const bad-ext cong-ext : ∀ (f≡g : ∀ x → f x ≡ g x) {x} → cong (_$ x) (⟨ext⟩ f≡g) ≡ f≡g x cong-ext = Eq.cong-good-ext bad-ext ext-cong : {B : Type b} {C : B → Type c} {f : A → (x : B) → C x} {x≡y : x ≡ y} → ⟨ext⟩ (λ z → cong (flip f z) x≡y) ≡ cong f x≡y ext-cong = Eq.good-ext-cong bad-ext subst-ext : ∀ {f g : (x : A) → B x} (P : B x → Type p) {p} (f≡g : ∀ x → f x ≡ g x) → subst (λ f → P (f x)) (⟨ext⟩ f≡g) p ≡ subst P (f≡g x) p subst-ext = Eq.subst-good-ext bad-ext elim-ext : (P : B x → B x → Type p) (p : (y : B x) → P y y) {f g : (x : A) → B x} (f≡g : ∀ x → f x ≡ g x) → elim (λ {f g} _ → P (f x) (g x)) (p ∘ (_$ x)) (⟨ext⟩ f≡g) ≡ elim (λ {x y} _ → P x y) p (f≡g x) elim-ext = Eq.elim-good-ext bad-ext -- I based the statements of the following three lemmas on code in -- the Lean Homotopy Type Theory Library with Jakob von Raumer and -- Floris van Doorn listed as authors. The file was claimed to have -- been ported from the Coq HoTT library. (The third lemma has later -- been generalised.) ext-sym : (f≡g : ∀ x → f x ≡ g x) → ⟨ext⟩ (sym ∘ f≡g) ≡ sym (⟨ext⟩ f≡g) ext-sym = Eq.good-ext-sym bad-ext ext-trans : (f≡g : ∀ x → f x ≡ g x) (g≡h : ∀ x → g x ≡ h x) → ⟨ext⟩ (λ x → trans (f≡g x) (g≡h x)) ≡ trans (⟨ext⟩ f≡g) (⟨ext⟩ g≡h) ext-trans = Eq.good-ext-trans bad-ext cong-post-∘-ext : (f≡g : ∀ x → f x ≡ g x) → cong (h ∘_) (⟨ext⟩ f≡g) ≡ ⟨ext⟩ (cong h ∘ f≡g) cong-post-∘-ext = Eq.cong-post-∘-good-ext bad-ext bad-ext cong-pre-∘-ext : (f≡g : ∀ x → f x ≡ g x) → cong (_∘ h) (⟨ext⟩ f≡g) ≡ ⟨ext⟩ (f≡g ∘ h) cong-pre-∘-ext = Eq.cong-pre-∘-good-ext bad-ext bad-ext cong-∘-ext : {A : Type a} {B : Type b} {C : Type c} {f g : B → C} (f≡g : ∀ x → f x ≡ g x) → cong {B = (A → B) → (A → C)} (λ f → f ∘_) (⟨ext⟩ f≡g) ≡ ⟨ext⟩ λ h → ⟨ext⟩ λ x → f≡g (h x) cong-∘-ext = Eq.cong-∘-good-ext bad-ext bad-ext bad-ext ------------------------------------------------------------------------ -- More isomorphisms and related properties -- Split surjections expressed using equality are equivalent to split -- surjections expressed using paths. ↠≃↠ : {A : Type a} {B : Type b} → (A ↠ B) ≃ (A PS.↠ B) ↠≃↠ = Eq.↔→≃ (λ A↠B → record { logical-equivalence = _↠_.logical-equivalence A↠B ; right-inverse-of = _↔_.to ≡↔≡ ∘ _↠_.right-inverse-of A↠B }) (λ A↠B → record { logical-equivalence = PS._↠_.logical-equivalence A↠B ; right-inverse-of = _↔_.from ≡↔≡ ∘ PS._↠_.right-inverse-of A↠B }) (λ A↠B → cong (λ r → record { logical-equivalence = PS._↠_.logical-equivalence A↠B ; right-inverse-of = r }) $ ⟨ext⟩ λ _ → _↔_.right-inverse-of ≡↔≡ _) (λ A↠B → cong (λ r → record { logical-equivalence = _↠_.logical-equivalence A↠B ; right-inverse-of = r }) $ ⟨ext⟩ λ _ → _↔_.left-inverse-of ≡↔≡ _) private -- Bijections expressed using paths can be converted into bijections -- expressed using equality. ↔→↔ : {A B : Type ℓ} → A PB.↔ B → A ↔ B ↔→↔ A↔B = record { surjection = _≃_.from ↠≃↠ $ PB._↔_.surjection A↔B ; left-inverse-of = _↔_.from ≡↔≡ ∘ PB._↔_.left-inverse-of A↔B } -- Bijections expressed using equality are equivalent to bijections -- expressed using paths. ↔≃↔ : {A : Type a} {B : Type b} → (A ↔ B) ≃ (A PB.↔ B) ↔≃↔ {A = A} {B = B} = A ↔ B ↔⟨ B.↔-as-Σ ⟩ (∃ λ (f : A → B) → ∃ λ (f⁻¹ : B → A) → (∀ x → f (f⁻¹ x) ≡ x) × (∀ x → f⁻¹ (f x) ≡ x)) ↔⟨ (∃-cong λ _ → ∃-cong λ _ → (∀-cong ext λ _ → ≡↔≡) ×-cong (∀-cong ext λ _ → ≡↔≡)) ⟩ (∃ λ (f : A → B) → ∃ λ (f⁻¹ : B → A) → (∀ x → f (f⁻¹ x) P.≡ x) × (∀ x → f⁻¹ (f x) P.≡ x)) ↔⟨ inverse $ ↔→↔ $ PB.↔-as-Σ ⟩□ A PB.↔ B □ -- H-level expressed using equality is isomorphic to H-level expressed -- using paths. H-level↔H-level : ∀ n → H-level n A ↔ PH.H-level n A H-level↔H-level {A = A} zero = H-level 0 A ↔⟨⟩ (∃ λ (x : A) → ∀ y → x ≡ y) ↝⟨ (∃-cong λ _ → ∀-cong ext λ _ → ≡↔≡) ⟩ (∃ λ (x : A) → ∀ y → x P.≡ y) ↔⟨⟩ PH.H-level 0 A □ H-level↔H-level {A = A} (suc n) = H-level (suc n) A ↝⟨ inverse $ ≡↔+ _ ext ⟩ (∀ x y → H-level n (x ≡ y)) ↝⟨ (∀-cong ext λ _ → ∀-cong ext λ _ → H-level-cong ext _ ≡↔≡) ⟩ (∀ x y → H-level n (x P.≡ y)) ↝⟨ (∀-cong ext λ _ → ∀-cong ext λ _ → H-level↔H-level n) ⟩ (∀ x y → PH.H-level n (x P.≡ y)) ↝⟨ ↔→↔ $ PF.≡↔+ _ P.ext ⟩ PH.H-level (suc n) A □ -- CP.Is-equivalence is isomorphic to PCP.Is-equivalence. Is-equivalence-CP↔Is-equivalence-CP : CP.Is-equivalence f ↔ PCP.Is-equivalence f Is-equivalence-CP↔Is-equivalence-CP {f = f} = CP.Is-equivalence f ↔⟨⟩ (∀ y → Contractible (∃ λ x → f x ≡ y)) ↝⟨ (∀-cong ext λ _ → H-level-cong ext _ $ ∃-cong λ _ → ≡↔≡) ⟩ (∀ y → Contractible (∃ λ x → f x P.≡ y)) ↝⟨ (∀-cong ext λ _ → H-level↔H-level _) ⟩ (∀ y → P.Contractible (∃ λ x → f x P.≡ y)) ↔⟨⟩ PCP.Is-equivalence f □ -- Is-equivalence expressed using equality is isomorphic to -- Is-equivalence expressed using paths. Is-equivalence↔Is-equivalence : Is-equivalence f ↔ PE.Is-equivalence f Is-equivalence↔Is-equivalence {f = f} = Is-equivalence f ↝⟨ HA.Is-equivalence↔Is-equivalence-CP ext ⟩ CP.Is-equivalence f ↝⟨ Is-equivalence-CP↔Is-equivalence-CP ⟩ PCP.Is-equivalence f ↝⟨ inverse $ ↔→↔ $ PHA.Is-equivalence↔Is-equivalence-CP P.ext ⟩□ PE.Is-equivalence f □ -- The type of equivalences, expressed using equality, is isomorphic -- to the type of equivalences, expressed using paths. ≃↔≃ : {A : Type a} {B : Type b} → A ≃ B ↔ A PE.≃ B ≃↔≃ {A = A} {B = B} = A ≃ B ↝⟨ Eq.≃-as-Σ ⟩ ∃ Is-equivalence ↝⟨ (∃-cong λ _ → Is-equivalence↔Is-equivalence) ⟩ ∃ PE.Is-equivalence ↝⟨ inverse $ ↔→↔ PE.≃-as-Σ ⟩□ A PE.≃ B □ private -- ≃↔≃ computes in the "right" way. to-≃↔≃ : {A : Type a} {B : Type b} {A≃B : A ≃ B} → PE._≃_.logical-equivalence (_↔_.to ≃↔≃ A≃B) ≡ _≃_.logical-equivalence A≃B to-≃↔≃ = refl _ from-≃↔≃ : {A : Type a} {B : Type b} {A≃B : A PE.≃ B} → _≃_.logical-equivalence (_↔_.from ≃↔≃ A≃B) ≡ PE._≃_.logical-equivalence A≃B from-≃↔≃ = refl _ -- The type of equivalences, expressed using "contractible preimages" -- and equality, is isomorphic to the type of equivalences, expressed -- using contractible preimages and paths. ≃-CP↔≃-CP : {A : Type a} {B : Type b} → A CP.≃ B ↔ A PCP.≃ B ≃-CP↔≃-CP {A = A} {B = B} = ∃ CP.Is-equivalence ↝⟨ (∃-cong λ _ → Is-equivalence-CP↔Is-equivalence-CP) ⟩□ ∃ PCP.Is-equivalence □ -- The cong function for paths can be expressed in terms of the cong -- function for equality. cong≡cong : {A : Type a} {B : Type b} {f : A → B} {x y : A} {x≡y : x P.≡ y} → cong f (_↔_.from ≡↔≡ x≡y) ≡ _↔_.from ≡↔≡ (P.cong f x≡y) cong≡cong {f = f} {x≡y = x≡y} = P.elim (λ x≡y → cong f (_↔_.from ≡↔≡ x≡y) ≡ _↔_.from ≡↔≡ (P.cong f x≡y)) (λ x → cong f (_↔_.from ≡↔≡ P.refl) ≡⟨ cong (cong f) from-≡↔≡-refl ⟩ cong f (refl x) ≡⟨ cong-refl _ ⟩ refl (f x) ≡⟨ sym $ from-≡↔≡-refl ⟩ _↔_.from ≡↔≡ P.refl ≡⟨ cong (_↔_.from ≡↔≡) $ sym $ _↔_.from ≡↔≡ $ P.cong-refl f ⟩∎ _↔_.from ≡↔≡ (P.cong f P.refl) ∎) x≡y -- The sym function for paths can be expressed in terms of the sym -- function for equality. sym≡sym : {x≡y : x P.≡ y} → sym (_↔_.from ≡↔≡ x≡y) ≡ _↔_.from ≡↔≡ (P.sym x≡y) sym≡sym {x≡y = x≡y} = P.elim₁ (λ x≡y → sym (_↔_.from ≡↔≡ x≡y) ≡ _↔_.from ≡↔≡ (P.sym x≡y)) (sym (_↔_.from ≡↔≡ P.refl) ≡⟨ cong sym from-≡↔≡-refl ⟩ sym (refl _) ≡⟨ sym-refl ⟩ refl _ ≡⟨ sym from-≡↔≡-refl ⟩ _↔_.from ≡↔≡ P.refl ≡⟨ cong (_↔_.from ≡↔≡) $ sym $ _↔_.from ≡↔≡ P.sym-refl ⟩∎ _↔_.from ≡↔≡ (P.sym P.refl) ∎) x≡y -- The trans function for paths can be expressed in terms of the trans -- function for equality. trans≡trans : {x≡y : x P.≡ y} {y≡z : y P.≡ z} → trans (_↔_.from ≡↔≡ x≡y) (_↔_.from ≡↔≡ y≡z) ≡ _↔_.from ≡↔≡ (P.trans x≡y y≡z) trans≡trans {x≡y = x≡y} {y≡z = y≡z} = P.elim₁ (λ x≡y → trans (_↔_.from ≡↔≡ x≡y) (_↔_.from ≡↔≡ y≡z) ≡ _↔_.from ≡↔≡ (P.trans x≡y y≡z)) (trans (_↔_.from ≡↔≡ P.refl) (_↔_.from ≡↔≡ y≡z) ≡⟨ cong (flip trans _) from-≡↔≡-refl ⟩ trans (refl _) (_↔_.from ≡↔≡ y≡z) ≡⟨ trans-reflˡ _ ⟩ _↔_.from ≡↔≡ y≡z ≡⟨ cong (_↔_.from ≡↔≡) $ sym $ _↔_.from ≡↔≡ $ P.trans-reflˡ _ ⟩∎ _↔_.from ≡↔≡ (P.trans P.refl y≡z) ∎) x≡y -- The type of embeddings, expressed using equality, is isomorphic to -- the type of embeddings, expressed using paths. Embedding↔Embedding : {A : Type a} {B : Type b} → Embedding A B ↔ PM.Embedding A B Embedding↔Embedding {A = A} {B = B} = Embedding A B ↝⟨ Embedding-as-Σ ⟩ (∃ λ f → ∀ x y → Is-equivalence (cong f)) ↔⟨ (∃-cong λ f → ∀-cong ext λ x → ∀-cong ext λ y → Eq.⇔→≃ (Eq.propositional ext _) (Eq.propositional ext _) (λ is → _≃_.is-equivalence $ Eq.with-other-function ( x P.≡ y ↔⟨ inverse ≡↔≡ ⟩ x ≡ y ↝⟨ Eq.⟨ _ , is ⟩ ⟩ f x ≡ f y ↔⟨ ≡↔≡ ⟩□ f x P.≡ f y □) (P.cong f) (λ eq → _↔_.to ≡↔≡ (cong f (_↔_.from ≡↔≡ eq)) ≡⟨ cong (_↔_.to ≡↔≡) cong≡cong ⟩ _↔_.to ≡↔≡ (_↔_.from ≡↔≡ (P.cong f eq)) ≡⟨ _↔_.right-inverse-of ≡↔≡ _ ⟩∎ P.cong f eq ∎)) (λ is → _≃_.is-equivalence $ Eq.with-other-function ( x ≡ y ↔⟨ ≡↔≡ ⟩ x P.≡ y ↝⟨ Eq.⟨ _ , is ⟩ ⟩ f x P.≡ f y ↔⟨ inverse ≡↔≡ ⟩□ f x ≡ f y □) (cong f) (λ eq → _↔_.from ≡↔≡ (P.cong f (_↔_.to ≡↔≡ eq)) ≡⟨ sym cong≡cong ⟩ cong f (_↔_.from ≡↔≡ (_↔_.to ≡↔≡ eq)) ≡⟨ cong (cong f) $ _↔_.left-inverse-of ≡↔≡ _ ⟩∎ cong f eq ∎))) ⟩ (∃ λ f → ∀ x y → Is-equivalence (P.cong f)) ↝⟨ (∃-cong λ _ → ∀-cong ext λ _ → ∀-cong ext λ _ → Is-equivalence↔Is-equivalence) ⟩ (∃ λ f → ∀ x y → PE.Is-equivalence (P.cong f)) ↝⟨ inverse $ ↔→↔ PM.Embedding-as-Σ ⟩□ PM.Embedding A B □ -- The subst function for paths can be expressed in terms of the subst -- function for equality. subst≡subst : ∀ {P : A → Type p} {x≡y : x P.≡ y} {p} → subst P (_↔_.from ≡↔≡ x≡y) p ≡ P.subst P x≡y p subst≡subst {P = P} {x≡y} = P.elim (λ x≡y → ∀ {p} → subst P (_↔_.from ≡↔≡ x≡y) p ≡ P.subst P x≡y p) (λ x {p} → subst P (_↔_.from ≡↔≡ P.refl) p ≡⟨ cong (λ eq → subst P eq p) from-≡↔≡-refl ⟩ subst P (refl x) p ≡⟨ subst-refl _ _ ⟩ p ≡⟨ sym $ _↔_.from ≡↔≡ $ P.subst-refl P _ ⟩∎ P.subst P P.refl p ∎) x≡y -- A "computation" rule for subst≡subst. subst≡subst-refl : ∀ {P : A → Type p} {p : P x} → subst≡subst {x≡y = P.refl} ≡ trans (cong (λ eq → subst P eq p) from-≡↔≡-refl) (trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl P _)) subst≡subst-refl {P = P} = cong (λ f → f {p = _}) $ _↔_.from ≡↔≡ $ P.elim-refl (λ x≡y → ∀ {p} → subst P (_↔_.from ≡↔≡ x≡y) p ≡ P.subst P x≡y p) (λ _ → trans (cong (λ eq → subst P eq _) from-≡↔≡-refl) (trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl P _))) -- Some corollaries. subst≡↔subst≡ : ∀ {eq : x P.≡ y} → subst B (_↔_.from ≡↔≡ eq) u ≡ v ↔ P.subst B eq u P.≡ v subst≡↔subst≡ {B = B} {u = u} {v = v} {eq = eq} = subst B (_↔_.from ≡↔≡ eq) u ≡ v ↝⟨ ≡⇒↝ _ $ cong (_≡ _) subst≡subst ⟩ P.subst B eq u ≡ v ↝⟨ ≡↔≡ ⟩□ P.subst B eq u P.≡ v □ subst≡↔[]≡ : {eq : x P.≡ y} → subst B (_↔_.from ≡↔≡ eq) u ≡ v ↔ P.[ (λ i → B (eq i)) ] u ≡ v subst≡↔[]≡ {B = B} {u = u} {v = v} {eq = eq} = subst B (_↔_.from ≡↔≡ eq) u ≡ v ↝⟨ subst≡↔subst≡ ⟩ P.subst B eq u P.≡ v ↝⟨ ↔→↔ $ PB.inverse $ P.heterogeneous↔homogeneous _ ⟩□ P.[ (λ i → B (eq i)) ] u ≡ v □ -- The dcong function for paths can be expressed using dcong for -- equality (up to pointwise equality). dcong≡dcong : {f : (x : A) → B x} {x≡y : x P.≡ y} → _↔_.to subst≡↔subst≡ (dcong f (_↔_.from ≡↔≡ x≡y)) ≡ P.dcong f x≡y dcong≡dcong {B = B} {f = f} {x≡y} = P.elim (λ x≡y → _↔_.to subst≡↔subst≡ (dcong f (_↔_.from ≡↔≡ x≡y)) ≡ P.dcong f x≡y) (λ x → _↔_.to subst≡↔subst≡ (dcong f (_↔_.from ≡↔≡ P.refl)) ≡⟨⟩ _↔_.to ≡↔≡ (_↔_.to (≡⇒↝ _ $ cong (_≡ _) subst≡subst) $ dcong f (_↔_.from ≡↔≡ P.refl)) ≡⟨ cong (_↔_.to ≡↔≡) $ lemma x ⟩ _↔_.to ≡↔≡ (_↔_.from ≡↔≡ $ P.subst-refl B (f x)) ≡⟨ _↔_.right-inverse-of ≡↔≡ _ ⟩ P.subst-refl B (f x) ≡⟨ sym $ _↔_.from ≡↔≡ $ P.dcong-refl f ⟩∎ P.dcong f P.refl ∎) x≡y where lemma : ∀ _ → _ lemma _ = _↔_.to (≡⇒↝ _ $ cong (_≡ _) subst≡subst) (dcong f (_↔_.from ≡↔≡ P.refl)) ≡⟨ sym $ subst-in-terms-of-≡⇒↝ bijection _ _ _ ⟩ subst (_≡ _) subst≡subst (dcong f (_↔_.from ≡↔≡ P.refl)) ≡⟨ cong (λ p → subst (_≡ _) p $ dcong f _) $ sym $ sym-sym _ ⟩ subst (_≡ _) (sym $ sym subst≡subst) (dcong f (_↔_.from ≡↔≡ P.refl)) ≡⟨ subst-trans _ ⟩ trans (sym (subst≡subst {x≡y = P.refl})) (dcong f (_↔_.from ≡↔≡ P.refl)) ≡⟨ cong (λ p → trans (sym p) (dcong f (_↔_.from ≡↔≡ P.refl))) subst≡subst-refl ⟩ trans (sym $ trans (cong (λ eq → subst B eq _) from-≡↔≡-refl) (trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl B _))) (dcong f (_↔_.from ≡↔≡ P.refl)) ≡⟨ elim₁ (λ {x} p → trans (sym $ trans (cong (λ eq → subst B eq _) p) (trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl B _))) (dcong f x) ≡ trans (sym $ trans (cong (λ eq → subst B eq _) (refl _)) (trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl B _))) (dcong f (refl _))) (refl _) from-≡↔≡-refl ⟩ trans (sym $ trans (cong (λ eq → subst B eq _) (refl _)) (trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl B _))) (dcong f (refl _)) ≡⟨ cong₂ (λ p → trans $ sym $ trans p $ trans (subst-refl _ _) $ sym $ _↔_.from ≡↔≡ $ P.subst-refl B _) (cong-refl _) (dcong-refl _) ⟩ trans (sym $ trans (refl _) (trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl B _))) (subst-refl B _) ≡⟨ cong (λ p → trans (sym p) (subst-refl _ _)) $ trans-reflˡ _ ⟩ trans (sym $ trans (subst-refl _ _) (sym $ _↔_.from ≡↔≡ $ P.subst-refl B _)) (subst-refl B _) ≡⟨ cong (λ p → trans p (subst-refl _ _)) $ sym-trans _ _ ⟩ trans (trans (sym $ sym $ _↔_.from ≡↔≡ $ P.subst-refl B _) (sym $ subst-refl _ _)) (subst-refl B _) ≡⟨ trans-[trans-sym]- _ _ ⟩ sym $ sym $ _↔_.from ≡↔≡ $ P.subst-refl B _ ≡⟨ sym-sym _ ⟩∎ _↔_.from ≡↔≡ $ P.subst-refl B _ ∎ -- A lemma relating dcong and hcong (for paths). dcong≡hcong : {x≡y : x P.≡ y} (f : (x : A) → B x) → dcong f (_↔_.from ≡↔≡ x≡y) ≡ _↔_.from subst≡↔[]≡ (P.hcong f x≡y) dcong≡hcong {x≡y = x≡y} f = dcong f (_↔_.from ≡↔≡ x≡y) ≡⟨ sym $ _↔_.from-to (inverse subst≡↔subst≡) dcong≡dcong ⟩ _↔_.from subst≡↔subst≡ (P.dcong f x≡y) ≡⟨ cong (_↔_.from subst≡↔subst≡) $ _↔_.from ≡↔≡ $ P.dcong≡hcong f ⟩ _↔_.from subst≡↔subst≡ (PB._↔_.to (P.heterogeneous↔homogeneous _) (P.hcong f x≡y)) ≡⟨⟩ _↔_.from subst≡↔[]≡ (P.hcong f x≡y) ∎ -- Three corollaries, intended to be used in the implementation of -- eliminators for HITs. For some examples, see Interval and -- Quotient.HIT. subst≡→[]≡ : {eq : x P.≡ y} → subst B (_↔_.from ≡↔≡ eq) u ≡ v → P.[ (λ i → B (eq i)) ] u ≡ v subst≡→[]≡ = _↔_.to subst≡↔[]≡ dcong-subst≡→[]≡ : {eq₁ : x P.≡ y} {eq₂ : subst B (_↔_.from ≡↔≡ eq₁) (f x) ≡ f y} → P.hcong f eq₁ ≡ subst≡→[]≡ eq₂ → dcong f (_↔_.from ≡↔≡ eq₁) ≡ eq₂ dcong-subst≡→[]≡ {B = B} {f = f} {eq₁} {eq₂} hyp = dcong f (_↔_.from ≡↔≡ eq₁) ≡⟨ dcong≡hcong f ⟩ _↔_.from subst≡↔[]≡ (P.hcong f eq₁) ≡⟨ cong (_↔_.from subst≡↔[]≡) hyp ⟩ _↔_.from subst≡↔[]≡ (_↔_.to subst≡↔[]≡ eq₂) ≡⟨ _↔_.left-inverse-of subst≡↔[]≡ _ ⟩∎ eq₂ ∎ cong-≡↔≡ : {eq₁ : x P.≡ y} {eq₂ : f x ≡ f y} → P.cong f eq₁ ≡ _↔_.to ≡↔≡ eq₂ → cong f (_↔_.from ≡↔≡ eq₁) ≡ eq₂ cong-≡↔≡ {f = f} {eq₁ = eq₁} {eq₂} hyp = cong f (_↔_.from ≡↔≡ eq₁) ≡⟨ cong≡cong ⟩ _↔_.from ≡↔≡ $ P.cong f eq₁ ≡⟨ cong (_↔_.from ≡↔≡) hyp ⟩ _↔_.from ≡↔≡ $ _↔_.to ≡↔≡ eq₂ ≡⟨ _↔_.left-inverse-of ≡↔≡ _ ⟩∎ eq₂ ∎ ------------------------------------------------------------------------ -- Univalence -- CP.Univalence′ is pointwise equivalent to PCP.Univalence′. Univalence′-CP≃Univalence′-CP : {A B : Type ℓ} → CP.Univalence′ A B ≃ PCP.Univalence′ A B Univalence′-CP≃Univalence′-CP {A = A} {B = B} = ((A≃B : A CP.≃ B) → ∃ λ (x : ∃ λ A≡B → CP.≡⇒≃ A≡B ≡ A≃B) → ∀ y → x ≡ y) ↔⟨⟩ ((A≃B : ∃ λ (f : A → B) → CP.Is-equivalence f) → ∃ λ (x : ∃ λ A≡B → CP.≡⇒≃ A≡B ≡ A≃B) → ∀ y → x ≡ y) ↝⟨ (Π-cong ext (∃-cong λ _ → Is-equivalence-CP↔Is-equivalence-CP) λ A≃B → Σ-cong (lemma₁ A≃B) λ _ → Π-cong ext (lemma₁ A≃B) λ _ → inverse $ Eq.≃-≡ (lemma₁ A≃B)) ⟩ ((A≃B : ∃ λ (f : A → B) → PCP.Is-equivalence f) → ∃ λ (x : ∃ λ A≡B → PCP.≡⇒≃ A≡B ≡ A≃B) → ∀ y → x ≡ y) ↔⟨⟩ ((A≃B : A PCP.≃ B) → ∃ λ (x : ∃ λ A≡B → PCP.≡⇒≃ A≡B ≡ A≃B) → ∀ y → x ≡ y) ↔⟨ Is-equivalence-CP↔Is-equivalence-CP ⟩□ ((A≃B : A PCP.≃ B) → ∃ λ (x : ∃ λ A≡B → PCP.≡⇒≃ A≡B P.≡ A≃B) → ∀ y → x P.≡ y) □ where lemma₃ : (A≡B : A ≡ B) → _↔_.to ≃-CP↔≃-CP (CP.≡⇒≃ A≡B) ≡ PCP.≡⇒≃ (_↔_.to ≡↔≡ A≡B) lemma₃ = elim¹ (λ A≡B → _↔_.to ≃-CP↔≃-CP (CP.≡⇒≃ A≡B) ≡ PCP.≡⇒≃ (_↔_.to ≡↔≡ A≡B)) (_↔_.to ≃-CP↔≃-CP (CP.≡⇒≃ (refl _)) ≡⟨ cong (_↔_.to ≃-CP↔≃-CP) CP.≡⇒≃-refl ⟩ _↔_.to ≃-CP↔≃-CP CP.id ≡⟨ _↔_.from ≡↔≡ $ P.Σ-≡,≡→≡ P.refl (PCP.propositional P.ext _ _ _) ⟩ PCP.id ≡⟨ sym $ _↔_.from ≡↔≡ PCP.≡⇒≃-refl ⟩ PCP.≡⇒≃ P.refl ≡⟨ sym $ cong PCP.≡⇒≃ to-≡↔≡-refl ⟩∎ PCP.≡⇒≃ (_↔_.to ≡↔≡ (refl _)) ∎) lemma₂ : ∀ _ _ → _ ≃ _ lemma₂ A≡B (f , f-eq) = CP.≡⇒≃ A≡B ≡ (f , f-eq) ↝⟨ inverse $ Eq.≃-≡ (Eq.↔⇒≃ ≃-CP↔≃-CP) ⟩ _↔_.to ≃-CP↔≃-CP (CP.≡⇒≃ A≡B) ≡ (f , _↔_.to Is-equivalence-CP↔Is-equivalence-CP f-eq) ↝⟨ ≡⇒≃ $ cong (_≡ (f , _↔_.to Is-equivalence-CP↔Is-equivalence-CP f-eq)) $ lemma₃ A≡B ⟩□ PCP.≡⇒≃ (_↔_.to ≡↔≡ A≡B) ≡ (f , _↔_.to Is-equivalence-CP↔Is-equivalence-CP f-eq) □ lemma₁ : ∀ A≃B → (∃ λ A≡B → CP.≡⇒≃ A≡B ≡ A≃B) ≃ (∃ λ A≡B → PCP.≡⇒≃ A≡B ≡ ( proj₁ A≃B , _↔_.to Is-equivalence-CP↔Is-equivalence-CP (proj₂ A≃B) )) lemma₁ A≃B = Σ-cong ≡↔≡ λ A≡B → lemma₂ A≡B A≃B -- Univalence′ expressed using equality is equivalent to Univalence′ -- expressed using paths. Univalence′≃Univalence′ : {A B : Type ℓ} → Univalence′ A B ≃ PU.Univalence′ A B Univalence′≃Univalence′ {A = A} {B = B} = Univalence′ A B ↝⟨ Univalence′≃Univalence′-CP ext ⟩ CP.Univalence′ A B ↝⟨ Univalence′-CP≃Univalence′-CP ⟩ PCP.Univalence′ A B ↝⟨ inverse $ _↔_.from ≃↔≃ $ PU.Univalence′≃Univalence′-CP P.ext ⟩□ PU.Univalence′ A B □ -- Univalence expressed using equality is equivalent to univalence -- expressed using paths. Univalence≃Univalence : Univalence ℓ ≃ PU.Univalence ℓ Univalence≃Univalence {ℓ = ℓ} = ({A B : Type ℓ} → Univalence′ A B) ↝⟨ implicit-∀-cong ext $ implicit-∀-cong ext Univalence′≃Univalence′ ⟩□ ({A B : Type ℓ} → PU.Univalence′ A B) □
-- We define what it means to satisfy the universal property -- of localisation and show that the localisation in Base satisfies -- it. We will also show that the localisation is uniquely determined -- by the universal property, and give sufficient criteria for -- satisfying the universal property. {-# OPTIONS --cubical --no-import-sorts --safe #-} module Cubical.Algebra.CommRing.Localisation.UniversalProperty where open import Cubical.Foundations.Prelude open import Cubical.Foundations.Function open import Cubical.Foundations.Equiv open import Cubical.Foundations.Isomorphism open import Cubical.Foundations.Univalence open import Cubical.Foundations.HLevels open import Cubical.Foundations.Powerset open import Cubical.Foundations.Transport open import Cubical.Functions.FunExtEquiv open import Cubical.Functions.Surjection open import Cubical.Functions.Embedding import Cubical.Data.Empty as ⊥ open import Cubical.Data.Bool open import Cubical.Data.Nat renaming ( _+_ to _+ℕ_ ; _·_ to _·ℕ_ ; +-comm to +ℕ-comm ; +-assoc to +ℕ-assoc ; ·-assoc to ·ℕ-assoc ; ·-comm to ·ℕ-comm) open import Cubical.Data.Vec open import Cubical.Data.Sigma.Base open import Cubical.Data.Sigma.Properties open import Cubical.Data.FinData open import Cubical.Relation.Nullary open import Cubical.Relation.Binary open import Cubical.Algebra.Group open import Cubical.Algebra.AbGroup open import Cubical.Algebra.Monoid open import Cubical.Algebra.Ring open import Cubical.Algebra.RingSolver.ReflectionSolving open import Cubical.Algebra.CommRing open import Cubical.Algebra.CommRing.Localisation.Base open import Cubical.HITs.SetQuotients as SQ open import Cubical.HITs.PropositionalTruncation as PT open Iso private variable ℓ ℓ' : Level module _ (R' : CommRing {ℓ}) (S' : ℙ (fst R')) (SMultClosedSubset : isMultClosedSubset R' S') where open isMultClosedSubset private R = fst R' open CommRingStr (snd R') hiding (is-set) open Theory (CommRing→Ring R') open RingHom hasLocUniversalProp : (A : CommRing {ℓ}) (φ : CommRingHom R' A) → (∀ s → s ∈ S' → f φ s ∈ A ˣ) → Type (ℓ-suc ℓ) hasLocUniversalProp A φ _ = (B : CommRing {ℓ}) (ψ : CommRingHom R' B) → (∀ s → s ∈ S' → f ψ s ∈ B ˣ) → ∃![ χ ∈ CommRingHom A B ] (f χ) ∘ (f φ) ≡ (f ψ) UniversalPropIsProp : (A : CommRing {ℓ}) (φ : CommRingHom R' A) → (φS⊆Aˣ : ∀ s → s ∈ S' → f φ s ∈ A ˣ) → isProp (hasLocUniversalProp A φ φS⊆Aˣ) UniversalPropIsProp A φ φS⊆Aˣ = isPropΠ3 (λ _ _ _ → isPropIsContr) -- S⁻¹R has the universal property module S⁻¹RUniversalProp where open Loc R' S' SMultClosedSubset _/1 : R → S⁻¹R r /1 = [ r , 1r , SMultClosedSubset .containsOne ] /1AsCommRingHom : CommRingHom R' S⁻¹RAsCommRing f /1AsCommRingHom = _/1 pres1 /1AsCommRingHom = refl isHom+ /1AsCommRingHom r r' = cong [_] (≡-× (cong₂ (_+_) (sym (·Rid r)) (sym (·Rid r'))) (Σ≡Prop (λ x → S' x .snd) (sym (·Lid 1r)))) isHom· /1AsCommRingHom r r' = cong [_] (≡-× refl (Σ≡Prop (λ x → S' x .snd) (sym (·Lid 1r)))) S⁻¹Rˣ = S⁻¹RAsCommRing ˣ S/1⊆S⁻¹Rˣ : ∀ s → s ∈ S' → (s /1) ∈ S⁻¹Rˣ S/1⊆S⁻¹Rˣ s s∈S' = [ 1r , s , s∈S' ] , eq/ _ _ ((1r , SMultClosedSubset .containsOne) , path s) where path : ∀ s → 1r · (s · 1r) · 1r ≡ 1r · 1r · (1r · s) path = solve R' S⁻¹RHasUniversalProp : hasLocUniversalProp S⁻¹RAsCommRing /1AsCommRingHom S/1⊆S⁻¹Rˣ S⁻¹RHasUniversalProp B' ψ ψS⊆Bˣ = (χ , funExt χcomp) , χunique where B = fst B' open CommRingStr (snd B') renaming ( is-set to Bset ; _·_ to _·B_ ; 1r to 1b ; _+_ to _+B_ ; ·Assoc to ·B-assoc ; ·-comm to ·B-comm ; ·Lid to ·B-lid ; ·Rid to ·B-rid ; ·Ldist+ to ·B-ldist-+) open Units B' renaming (Rˣ to Bˣ ; RˣMultClosed to BˣMultClosed ; RˣContainsOne to BˣContainsOne) open Theory (CommRing→Ring B') renaming (·-assoc2 to ·B-assoc2) open CommTheory B' renaming (·-commAssocl to ·B-commAssocl ; ·-commAssocSwap to ·B-commAssocSwap) χ : CommRingHom S⁻¹RAsCommRing B' f χ = SQ.rec Bset fχ fχcoh where fχ : R × S → B fχ (r , s , s∈S') = (f ψ r) ·B ((f ψ s) ⁻¹) ⦃ ψS⊆Bˣ s s∈S' ⦄ fχcoh : (a b : R × S) → a ≈ b → fχ a ≡ fχ b fχcoh (r , s , s∈S') (r' , s' , s'∈S') ((u , u∈S') , p) = instancepath ⦃ ψS⊆Bˣ s s∈S' ⦄ ⦃ ψS⊆Bˣ s' s'∈S' ⦄ ⦃ ψS⊆Bˣ (u · s · s') (SMultClosedSubset .multClosed (SMultClosedSubset .multClosed u∈S' s∈S') s'∈S') ⦄ ⦃ BˣMultClosed _ _ ⦃ ψS⊆Bˣ (u · s) (SMultClosedSubset .multClosed u∈S' s∈S') ⦄ ⦃ ψS⊆Bˣ s' s'∈S' ⦄ ⦄ ⦃ ψS⊆Bˣ (u · s) (SMultClosedSubset .multClosed u∈S' s∈S') ⦄ where -- only a few indidividual steps can be solved by the ring solver yet instancepath : ⦃ _ : f ψ s ∈ Bˣ ⦄ ⦃ _ : f ψ s' ∈ Bˣ ⦄ ⦃ _ : f ψ (u · s · s') ∈ Bˣ ⦄ ⦃ _ : f ψ (u · s) ·B f ψ s' ∈ Bˣ ⦄ ⦃ _ : f ψ (u · s) ∈ Bˣ ⦄ → f ψ r ·B f ψ s ⁻¹ ≡ f ψ r' ·B f ψ s' ⁻¹ instancepath = f ψ r ·B f ψ s ⁻¹ ≡⟨ sym (·B-rid _) ⟩ f ψ r ·B f ψ s ⁻¹ ·B 1b ≡⟨ cong (f ψ r ·B f ψ s ⁻¹ ·B_) (sym (·-rinv _)) ⟩ f ψ r ·B f ψ s ⁻¹ ·B (f ψ (u · s · s') ·B f ψ (u · s · s') ⁻¹) ≡⟨ ·B-assoc _ _ _ ⟩ f ψ r ·B f ψ s ⁻¹ ·B f ψ (u · s · s') ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (λ x → f ψ r ·B f ψ s ⁻¹ ·B x ·B f ψ (u · s · s') ⁻¹) (isHom· ψ _ _) ⟩ f ψ r ·B f ψ s ⁻¹ ·B (f ψ (u · s) ·B f ψ s') ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (_·B f ψ (u · s · s') ⁻¹) (·B-assoc _ _ _) ⟩ f ψ r ·B f ψ s ⁻¹ ·B f ψ (u · s) ·B f ψ s' ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (λ x → f ψ r ·B f ψ s ⁻¹ ·B x ·B f ψ s' ·B f ψ (u · s · s') ⁻¹) (isHom· ψ _ _) ⟩ f ψ r ·B f ψ s ⁻¹ ·B (f ψ u ·B f ψ s) ·B f ψ s' ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (λ x → x ·B f ψ s' ·B f ψ (u · s · s') ⁻¹) (·B-commAssocSwap _ _ _ _) ⟩ f ψ r ·B f ψ u ·B (f ψ s ⁻¹ ·B f ψ s) ·B f ψ s' ·B f ψ (u · s · s') ⁻¹ ≡⟨ (λ i → ·B-comm (f ψ r) (f ψ u) i ·B (·-linv (f ψ s) i) ·B f ψ s' ·B f ψ (u · s · s') ⁻¹) ⟩ f ψ u ·B f ψ r ·B 1b ·B f ψ s' ·B f ψ (u · s · s') ⁻¹ ≡⟨ (λ i → (·B-rid (sym (isHom· ψ u r) i) i) ·B f ψ s' ·B f ψ (u · s · s') ⁻¹) ⟩ f ψ (u · r) ·B f ψ s' ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (_·B f ψ (u · s · s') ⁻¹) (sym (isHom· ψ _ _)) ⟩ f ψ (u · r · s') ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (λ x → f ψ x ·B f ψ (u · s · s') ⁻¹) p ⟩ f ψ (u · r' · s) ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (_·B f ψ (u · s · s') ⁻¹) (isHom· ψ _ _) ⟩ f ψ (u · r') ·B f ψ s ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (λ x → x ·B f ψ s ·B f ψ (u · s · s') ⁻¹) (isHom· ψ _ _) ⟩ f ψ u ·B f ψ r' ·B f ψ s ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (_·B f ψ (u · s · s') ⁻¹) (sym (·B-assoc _ _ _)) ⟩ f ψ u ·B (f ψ r' ·B f ψ s) ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (_·B f ψ (u · s · s') ⁻¹) (·B-commAssocl _ _ _) ⟩ f ψ r' ·B (f ψ u ·B f ψ s) ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (λ x → f ψ r' ·B x ·B f ψ (u · s · s') ⁻¹) (sym (isHom· ψ _ _)) ⟩ f ψ r' ·B f ψ (u · s) ·B f ψ (u · s · s') ⁻¹ ≡⟨ cong (f ψ r' ·B f ψ (u · s) ·B_) (unitCong (isHom· ψ _ _)) ⟩ f ψ r' ·B f ψ (u · s) ·B (f ψ (u · s) ·B f ψ s') ⁻¹ ≡⟨ cong (f ψ r' ·B f ψ (u · s) ·B_) (⁻¹-dist-· _ _) ⟩ f ψ r' ·B f ψ (u · s) ·B (f ψ (u · s) ⁻¹ ·B f ψ s' ⁻¹) ≡⟨ ·B-assoc2 _ _ _ _ ⟩ f ψ r' ·B (f ψ (u · s) ·B f ψ (u · s) ⁻¹) ·B f ψ s' ⁻¹ ≡⟨ cong (λ x → f ψ r' ·B x ·B f ψ s' ⁻¹) (·-rinv _) ⟩ f ψ r' ·B 1b ·B f ψ s' ⁻¹ ≡⟨ cong (_·B f ψ s' ⁻¹) (·B-rid _) ⟩ f ψ r' ·B f ψ s' ⁻¹ ∎ pres1 χ = instancepres1χ ⦃ ψS⊆Bˣ 1r (SMultClosedSubset .containsOne) ⦄ ⦃ BˣContainsOne ⦄ where instancepres1χ : ⦃ _ : f ψ 1r ∈ Bˣ ⦄ ⦃ _ : 1b ∈ Bˣ ⦄ → f ψ 1r ·B (f ψ 1r) ⁻¹ ≡ 1b instancepres1χ = (λ i → (pres1 ψ i) ·B (unitCong (pres1 ψ) i)) ∙ (λ i → ·B-lid (1⁻¹≡1 i) i) isHom+ χ = elimProp2 (λ _ _ _ _ → Bset _ _ _ _) isHom+[] where isHom+[] : (a b : R × S) → f χ ([ a ] +ₗ [ b ]) ≡ (f χ [ a ]) +B (f χ [ b ]) isHom+[] (r , s , s∈S') (r' , s' , s'∈S') = instancepath ⦃ ψS⊆Bˣ s s∈S' ⦄ ⦃ ψS⊆Bˣ s' s'∈S' ⦄ ⦃ ψS⊆Bˣ (s · s') (SMultClosedSubset .multClosed s∈S' s'∈S') ⦄ ⦃ BˣMultClosed _ _ ⦃ ψS⊆Bˣ s s∈S' ⦄ ⦃ ψS⊆Bˣ s' s'∈S' ⦄ ⦄ where -- only a few indidividual steps can be solved by the ring solver yet instancepath : ⦃ _ : f ψ s ∈ Bˣ ⦄ ⦃ _ : f ψ s' ∈ Bˣ ⦄ ⦃ _ : f ψ (s · s') ∈ Bˣ ⦄ ⦃ _ : f ψ s ·B f ψ s' ∈ Bˣ ⦄ → f ψ (r · s' + r' · s) ·B f ψ (s · s') ⁻¹ ≡ f ψ r ·B f ψ s ⁻¹ +B f ψ r' ·B f ψ s' ⁻¹ instancepath = f ψ (r · s' + r' · s) ·B f ψ (s · s') ⁻¹ ≡⟨ (λ i → isHom+ ψ (r · s') (r' · s) i ·B unitCong (isHom· ψ s s') i) ⟩ (f ψ (r · s') +B f ψ (r' · s)) ·B (f ψ s ·B f ψ s') ⁻¹ ≡⟨ (λ i → (isHom· ψ r s' i +B isHom· ψ r' s i) ·B ⁻¹-dist-· (f ψ s) (f ψ s') i) ⟩ (f ψ r ·B f ψ s' +B f ψ r' ·B f ψ s) ·B (f ψ s ⁻¹ ·B f ψ s' ⁻¹) ≡⟨ ·B-ldist-+ _ _ _ ⟩ f ψ r ·B f ψ s' ·B (f ψ s ⁻¹ ·B f ψ s' ⁻¹) +B f ψ r' ·B f ψ s ·B (f ψ s ⁻¹ ·B f ψ s' ⁻¹) ≡⟨ (λ i → ·B-commAssocSwap (f ψ r) (f ψ s') (f ψ s ⁻¹) (f ψ s' ⁻¹) i +B ·B-assoc2 (f ψ r') (f ψ s) (f ψ s ⁻¹) (f ψ s' ⁻¹) i) ⟩ f ψ r ·B f ψ s ⁻¹ ·B (f ψ s' ·B f ψ s' ⁻¹) +B f ψ r' ·B (f ψ s ·B f ψ s ⁻¹) ·B f ψ s' ⁻¹ ≡⟨ (λ i → f ψ r ·B f ψ s ⁻¹ ·B (·-rinv (f ψ s') i) +B f ψ r' ·B (·-rinv (f ψ s) i) ·B f ψ s' ⁻¹) ⟩ f ψ r ·B f ψ s ⁻¹ ·B 1b +B f ψ r' ·B 1b ·B f ψ s' ⁻¹ ≡⟨ (λ i → ·B-rid (f ψ r ·B f ψ s ⁻¹) i +B ·B-rid (f ψ r') i ·B f ψ s' ⁻¹) ⟩ f ψ r ·B f ψ s ⁻¹ +B f ψ r' ·B f ψ s' ⁻¹ ∎ isHom· χ = elimProp2 (λ _ _ _ _ → Bset _ _ _ _) isHom·[] where isHom·[] : (a b : R × S) → f χ ([ a ] ·ₗ [ b ]) ≡ (f χ [ a ]) ·B (f χ [ b ]) isHom·[] (r , s , s∈S') (r' , s' , s'∈S') = instancepath ⦃ ψS⊆Bˣ s s∈S' ⦄ ⦃ ψS⊆Bˣ s' s'∈S' ⦄ ⦃ ψS⊆Bˣ (s · s') (SMultClosedSubset .multClosed s∈S' s'∈S') ⦄ ⦃ BˣMultClosed _ _ ⦃ ψS⊆Bˣ s s∈S' ⦄ ⦃ ψS⊆Bˣ s' s'∈S' ⦄ ⦄ where -- only a indidividual steps can be solved by the ring solver yet instancepath : ⦃ _ : f ψ s ∈ Bˣ ⦄ ⦃ _ : f ψ s' ∈ Bˣ ⦄ ⦃ _ : f ψ (s · s') ∈ Bˣ ⦄ ⦃ _ : f ψ s ·B f ψ s' ∈ Bˣ ⦄ → f ψ (r · r') ·B f ψ (s · s') ⁻¹ ≡ (f ψ r ·B f ψ s ⁻¹) ·B (f ψ r' ·B f ψ s' ⁻¹) instancepath = f ψ (r · r') ·B f ψ (s · s') ⁻¹ ≡⟨ (λ i → isHom· ψ r r' i ·B unitCong (isHom· ψ s s') i) ⟩ f ψ r ·B f ψ r' ·B (f ψ s ·B f ψ s') ⁻¹ ≡⟨ cong (f ψ r ·B f ψ r' ·B_) (⁻¹-dist-· _ _) ⟩ f ψ r ·B f ψ r' ·B (f ψ s ⁻¹ ·B f ψ s' ⁻¹) ≡⟨ ·B-commAssocSwap _ _ _ _ ⟩ f ψ r ·B f ψ s ⁻¹ ·B (f ψ r' ·B f ψ s' ⁻¹) ∎ χcomp : (r : R) → f χ (r /1) ≡ f ψ r χcomp = instanceχcomp ⦃ ψS⊆Bˣ 1r (SMultClosedSubset .containsOne) ⦄ ⦃ Units.RˣContainsOne B' ⦄ where instanceχcomp : ⦃ _ : f ψ 1r ∈ Bˣ ⦄ ⦃ _ : 1b ∈ Bˣ ⦄ (r : R) → f ψ r ·B (f ψ 1r) ⁻¹ ≡ f ψ r instanceχcomp r = f ψ r ·B (f ψ 1r) ⁻¹ ≡⟨ cong (f ψ r ·B_) (unitCong (pres1 ψ)) ⟩ f ψ r ·B 1b ⁻¹ ≡⟨ cong (f ψ r ·B_) 1⁻¹≡1 ⟩ f ψ r ·B 1b ≡⟨ ·B-rid _ ⟩ f ψ r ∎ χunique : (y : Σ[ χ' ∈ CommRingHom S⁻¹RAsCommRing B' ] f χ' ∘ _/1 ≡ f ψ) → (χ , funExt χcomp) ≡ y χunique (χ' , χ'/1≡ψ) = Σ≡Prop (λ x → isSetΠ (λ _ → Bset) _ _) (RingHom≡f _ _ fχ≡fχ') where open RingHomRespUnits {A' = S⁻¹RAsCommRing} {B' = B'} χ' renaming (φ[x⁻¹]≡φ[x]⁻¹ to χ'[x⁻¹]≡χ'[x]⁻¹) []-path : (a : R × S) → f χ [ a ] ≡ f χ' [ a ] []-path (r , s , s∈S') = instancepath ⦃ ψS⊆Bˣ s s∈S' ⦄ ⦃ S/1⊆S⁻¹Rˣ s s∈S' ⦄ ⦃ RingHomRespInv _ ⦃ S/1⊆S⁻¹Rˣ s s∈S' ⦄ ⦄ where open Units S⁻¹RAsCommRing renaming (_⁻¹ to _⁻¹ˡ ; inverseUniqueness to S⁻¹RInverseUniqueness) hiding (unitCong) s-inv : ⦃ s/1∈S⁻¹Rˣ : s /1 ∈ S⁻¹Rˣ ⦄ → s /1 ⁻¹ˡ ≡ [ 1r , s , s∈S' ] s-inv ⦃ s/1∈S⁻¹Rˣ ⦄ = PathPΣ (S⁻¹RInverseUniqueness (s /1) s/1∈S⁻¹Rˣ (_ , eq/ _ _ ((1r , SMultClosedSubset .containsOne) , path s))) .fst where path : ∀ s → 1r · (s · 1r) · 1r ≡ 1r · 1r · (1r · s) path = solve R' ·ₗ-path : [ r , s , s∈S' ] ≡ [ r , 1r , SMultClosedSubset .containsOne ] ·ₗ [ 1r , s , s∈S' ] ·ₗ-path = cong [_] (≡-× (sym (·Rid r)) (Σ≡Prop (λ x → S' x .snd) (sym (·Lid s)))) instancepath : ⦃ _ : f ψ s ∈ Bˣ ⦄ ⦃ _ : s /1 ∈ S⁻¹Rˣ ⦄ ⦃ _ : f χ' (s /1) ∈ Bˣ ⦄ → f ψ r ·B f ψ s ⁻¹ ≡ f χ' [ r , s , s∈S' ] instancepath = f ψ r ·B f ψ s ⁻¹ ≡⟨ cong (f ψ r ·B_) (unitCong (cong (λ φ → φ s) (sym χ'/1≡ψ))) ⟩ f ψ r ·B f χ' (s /1) ⁻¹ ≡⟨ cong (f ψ r ·B_) (sym (χ'[x⁻¹]≡χ'[x]⁻¹ _)) ⟩ f ψ r ·B f χ' (s /1 ⁻¹ˡ) ≡⟨ cong (λ x → f ψ r ·B f χ' x) s-inv ⟩ f ψ r ·B f χ' [ 1r , s , s∈S' ] ≡⟨ cong (_·B f χ' [ 1r , s , s∈S' ]) (cong (λ φ → φ r) (sym χ'/1≡ψ)) ⟩ f χ' [ r , 1r , SMultClosedSubset .containsOne ] ·B f χ' [ 1r , s , s∈S' ] ≡⟨ sym (isHom· χ' _ _) ⟩ f χ' ([ r , 1r , SMultClosedSubset .containsOne ] ·ₗ [ 1r , s , s∈S' ]) ≡⟨ cong (f χ') (sym ·ₗ-path) ⟩ f χ' [ r , s , s∈S' ] ∎ fχ≡fχ' : f χ ≡ f χ' fχ≡fχ' = funExt (SQ.elimProp (λ _ → Bset _ _) []-path) -- sufficient conditions for having the universal property -- used as API in the leanprover-community/mathlib -- Corollary 3.2 in Atiyah-McDonald open S⁻¹RUniversalProp open Loc R' S' SMultClosedSubset record PathToS⁻¹R (A' : CommRing {ℓ}) (φ : CommRingHom R' A') : Type ℓ where constructor pathtoS⁻¹R open Units A' renaming (Rˣ to Aˣ) open CommRingStr (snd A') renaming (is-set to Aset ; 0r to 0a ; _·_ to _·A_) field φS⊆Aˣ : ∀ s → s ∈ S' → f φ s ∈ Aˣ kerφ⊆annS : ∀ r → f φ r ≡ 0a → ∃[ s ∈ S ] (s .fst) · r ≡ 0r surχ : ∀ a → ∃[ x ∈ R × S ] f φ (x .fst) ·A (f φ (x .snd .fst) ⁻¹) ⦃ φS⊆Aˣ _ (x .snd .snd) ⦄ ≡ a S⁻¹RChar : (A' : CommRing {ℓ}) (φ : CommRingHom R' A') → PathToS⁻¹R A' φ → S⁻¹RAsCommRing ≡ A' S⁻¹RChar A' φ cond = CommRingPath S⁻¹RAsCommRing A' .fst (S⁻¹R≃A , record { pres1 = pres1 χ ; isHom+ = isHom+ χ ; isHom· = isHom· χ }) where open CommRingStr (snd A') renaming ( is-set to Aset ; 0r to 0a ; _·_ to _·A_ ; 1r to 1a ; ·Rid to ·A-rid) open Units A' renaming (Rˣ to Aˣ ; RˣInvClosed to AˣInvClosed) open PathToS⁻¹R ⦃...⦄ private A = fst A' instance _ = cond χ = (S⁻¹RHasUniversalProp A' φ φS⊆Aˣ .fst .fst) open HomTheory χ S⁻¹R≃A : S⁻¹R ≃ A S⁻¹R≃A = f χ , isEmbedding×isSurjection→isEquiv (Embχ , Surχ) where Embχ : isEmbedding (f χ) Embχ = injEmbedding squash/ Aset (ker≡0→inj λ {x} → kerχ≡0 x) where kerχ≡0 : (r/s : S⁻¹R) → f χ r/s ≡ 0a → r/s ≡ 0ₗ kerχ≡0 = SQ.elimProp (λ _ → isPropΠ λ _ → squash/ _ _) kerχ≡[] where kerχ≡[] : (a : R × S) → f χ [ a ] ≡ 0a → [ a ] ≡ 0ₗ kerχ≡[] (r , s , s∈S') p = PT.rec (squash/ _ _) Σhelper (kerφ⊆annS r (UnitsAreNotZeroDivisors _ _ p)) where instance _ : f φ s ∈ Aˣ _ = φS⊆Aˣ s s∈S' _ : f φ s ⁻¹ ∈ Aˣ _ = AˣInvClosed _ Σhelper : Σ[ s ∈ S ] (s .fst) · r ≡ 0r → [ r , s , s∈S' ] ≡ 0ₗ Σhelper ((u , u∈S') , q) = eq/ _ _ ((u , u∈S') , path) where path : u · r · 1r ≡ u · 0r · s path = (λ i → ·Rid (q i) i) ∙∙ sym (0LeftAnnihilates _) ∙∙ cong (_· s) (sym (0RightAnnihilates _)) Surχ : isSurjection (f χ) Surχ a = PT.rec propTruncIsProp (λ x → PT.∣ [ x .fst ] , x .snd ∣) (surχ a)
{-# OPTIONS --omega-in-omega --no-termination-check --overlapping-instances #-} import Light.Library.Relation.Decidable as Decidable open import Light.Package using (Package) module Light.Library.Relation.Binary.Decidable where open import Light.Library.Relation.Binary using (Binary) open import Light.Level using (Setω) module _ ⦃ decidable‐package : Package record {Decidable} ⦄ where DecidableBinary : Setω DecidableBinary = ∀ {aℓ bℓ} (𝕒 : Set aℓ) (𝕓 : Set bℓ) → Binary 𝕒 𝕓 DecidableSelfBinary : Setω DecidableSelfBinary = ∀ {aℓ} (𝕒 : Set aℓ) → Binary 𝕒 𝕒
{-# OPTIONS --cubical --no-import-sorts #-} module Number.Instances.QuoQ.Instance where open import Agda.Primitive renaming (_⊔_ to ℓ-max; lsuc to ℓ-suc; lzero to ℓ-zero) open import Cubical.Foundations.Everything renaming (_⁻¹ to _⁻¹ᵖ; assoc to ∙-assoc) open import Cubical.Foundations.Logic renaming (inl to inlᵖ; inr to inrᵖ) open import Cubical.Relation.Nullary.Base renaming (¬_ to ¬ᵗ_) open import Cubical.Relation.Binary.Base open import Cubical.Data.Sum.Base renaming (_⊎_ to infixr 4 _⊎_) open import Cubical.Data.Sigma.Base renaming (_×_ to infixr 4 _×_) open import Cubical.Data.Sigma open import Cubical.Data.Bool as Bool using (Bool; not; true; false) open import Cubical.Data.Empty renaming (elim to ⊥-elim; ⊥ to ⊥⊥) -- `⊥` and `elim` open import Cubical.Foundations.Logic renaming (¬_ to ¬ᵖ_; inl to inlᵖ; inr to inrᵖ) open import Function.Base using (it; _∋_; _$_) open import Cubical.Foundations.Isomorphism open import Cubical.HITs.PropositionalTruncation renaming (elim to ∣∣-elim) open import Utils using (!_; !!_) open import MoreLogic.Reasoning open import MoreLogic.Definitions open import MoreLogic.Properties open import MorePropAlgebra.Definitions hiding (_≤''_) open import MorePropAlgebra.Structures open import MorePropAlgebra.Bundles open import MorePropAlgebra.Consequences open import Number.Structures2 open import Number.Bundles2 open import Cubical.Data.NatPlusOne using (HasFromNat; 1+_; ℕ₊₁; ℕ₊₁→ℕ) renaming ( _*₊₁_ to _·₊₁_ ; *₊₁-comm to ·₊₁-comm ; *₊₁-assoc to ·₊₁-assoc ; *₊₁-identityˡ to ·₊₁-identityˡ ; *₊₁-identityʳ to ·₊₁-identityʳ ) open import Cubical.HITs.SetQuotients as SetQuotient using () renaming (_/_ to _//_) open import Cubical.Data.Nat.Literals open import Cubical.Data.Bool open import Number.Prelude.Nat open import Number.Prelude.QuoInt open import Cubical.HITs.Ints.QuoInt using (HasFromNat) open import Number.Instances.QuoQ.Definitions open import Cubical.HITs.Rationals.QuoQ using ( ℚ ; HasFromNat ; isSetℚ ; onCommonDenom ; onCommonDenomSym ; eq/ ; eq/⁻¹ ; _//_ ; _∼_ ; [_/_] ) renaming ( [_] to [_]ᶠ ; ℕ₊₁→ℤ to [1+_ⁿ]ᶻ ; _*_ to _·_ ; *-comm to ·-comm ; *-assoc to ·-assoc ) {-# DISPLAY [_/_] (pos 1) (1+ 0) = 1 #-} {-# DISPLAY [_/_] (pos 0) (1+ 0) = 0 #-} private ·ᶻ-commʳ : ∀ a b c → (a ·ᶻ b) ·ᶻ c ≡ (a ·ᶻ c) ·ᶻ b ·ᶻ-commʳ a b c = (a ·ᶻ b) ·ᶻ c ≡⟨ sym $ ·ᶻ-assoc a b c ⟩ a ·ᶻ (b ·ᶻ c) ≡⟨ (λ i → a ·ᶻ ·ᶻ-comm b c i) ⟩ a ·ᶻ (c ·ᶻ b) ≡⟨ ·ᶻ-assoc a c b ⟩ (a ·ᶻ c) ·ᶻ b ∎ ·ᶻ-commˡ : ∀ a b c → a ·ᶻ (b ·ᶻ c) ≡ b ·ᶻ (a ·ᶻ c) ·ᶻ-commˡ a b c = ·ᶻ-comm a (b ·ᶻ c) ∙ sym (·ᶻ-commʳ b a c) ∙ sym (·ᶻ-assoc b a c) is-0<ⁿᶻ : ∀{aⁿ} → [ 0 <ᶻ [1+ aⁿ ⁿ]ᶻ ] is-0<ⁿᶻ {aⁿ} = ℕ₊₁.n aⁿ , +ⁿ-comm (ℕ₊₁.n aⁿ) 1 is-0≤ⁿᶻ : ∀{aⁿ} → [ 0 ≤ᶻ [1+ aⁿ ⁿ]ᶻ ] is-0≤ⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p <-irrefl : ∀ a → [ ¬(a < a) ] <-irrefl = SetQuotient.elimProp {R = _∼_} (λ a → isProp[] (¬(a < a))) γ where γ : (a : ℤ × ℕ₊₁) → [ ¬([ a ]ᶠ < [ a ]ᶠ) ] γ a@(aᶻ , aⁿ) = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ κ : [ ¬((aᶻ ·ᶻ aⁿᶻ) <ᶻ (aᶻ ·ᶻ aⁿᶻ)) ] κ = <ᶻ-irrefl (aᶻ ·ᶻ aⁿᶻ) <-trans : (a b c : ℚ) → [ a < b ] → [ b < c ] → [ a < c ] <-trans = SetQuotient.elimProp3 {R = _∼_} (λ a b c → isProp[] ((a < b) ⇒ (b < c) ⇒ (a < c))) γ where γ : (a b c : ℤ × ℕ₊₁) → [ [ a ]ᶠ < [ b ]ᶠ ] → [ [ b ]ᶠ < [ c ]ᶠ ] → [ [ a ]ᶠ < [ c ]ᶠ ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ cⁿᶻ = [1+ cⁿ ⁿ]ᶻ 0<aⁿᶻ = [ 0 <ᶻ aⁿᶻ ] ∋ is-0<ⁿᶻ 0<bⁿᶻ = [ 0 <ᶻ bⁿᶻ ] ∋ is-0<ⁿᶻ 0<cⁿᶻ = [ 0 <ᶻ cⁿᶻ ] ∋ is-0<ⁿᶻ κ : [ (aᶻ ·ᶻ bⁿᶻ) <ᶻ (bᶻ ·ᶻ aⁿᶻ) ] → [ (bᶻ ·ᶻ cⁿᶻ) <ᶻ (cᶻ ·ᶻ bⁿᶻ) ] → [ (aᶻ ·ᶻ cⁿᶻ) <ᶻ (cᶻ ·ᶻ aⁿᶻ) ] -- strategy: multiply with xⁿᶻ and then use <ᶻ-trans κ p₁ p₂ = ·ᶻ-reflects-<ᶻ (aᶻ ·ᶻ cⁿᶻ) (cᶻ ·ᶻ aⁿᶻ) bⁿᶻ 0<bⁿᶻ φ₃ where φ₁ = ( aᶻ ·ᶻ bⁿᶻ <ᶻ bᶻ ·ᶻ aⁿᶻ ⇒ᵖ⟨ ·ᶻ-preserves-<ᶻ (aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) cⁿᶻ 0<cⁿᶻ ⟩ aᶻ ·ᶻ bⁿᶻ ·ᶻ cⁿᶻ <ᶻ bᶻ ·ᶻ aⁿᶻ ·ᶻ cⁿᶻ ⇒ᵖ⟨ transport (λ i → [ ·ᶻ-commʳ aᶻ bⁿᶻ cⁿᶻ i <ᶻ ·ᶻ-commʳ bᶻ aⁿᶻ cⁿᶻ i ]) ⟩ aᶻ ·ᶻ cⁿᶻ ·ᶻ bⁿᶻ <ᶻ bᶻ ·ᶻ cⁿᶻ ·ᶻ aⁿᶻ ◼ᵖ) .snd p₁ φ₂ = ( bᶻ ·ᶻ cⁿᶻ <ᶻ cᶻ ·ᶻ bⁿᶻ ⇒ᵖ⟨ ·ᶻ-preserves-<ᶻ (bᶻ ·ᶻ cⁿᶻ) (cᶻ ·ᶻ bⁿᶻ) aⁿᶻ 0<aⁿᶻ ⟩ bᶻ ·ᶻ cⁿᶻ ·ᶻ aⁿᶻ <ᶻ cᶻ ·ᶻ bⁿᶻ ·ᶻ aⁿᶻ ⇒ᵖ⟨ transport (λ i → [ (bᶻ ·ᶻ cⁿᶻ ·ᶻ aⁿᶻ) <ᶻ ·ᶻ-commʳ cᶻ bⁿᶻ aⁿᶻ i ]) ⟩ bᶻ ·ᶻ cⁿᶻ ·ᶻ aⁿᶻ <ᶻ cᶻ ·ᶻ aⁿᶻ ·ᶻ bⁿᶻ ◼ᵖ) .snd p₂ φ₃ : [ aᶻ ·ᶻ cⁿᶻ ·ᶻ bⁿᶻ <ᶻ cᶻ ·ᶻ aⁿᶻ ·ᶻ bⁿᶻ ] φ₃ = <ᶻ-trans (aᶻ ·ᶻ cⁿᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ cⁿᶻ ·ᶻ aⁿᶻ) (cᶻ ·ᶻ aⁿᶻ ·ᶻ bⁿᶻ) φ₁ φ₂ <-asym : ∀ a b → [ a < b ] → [ ¬(b < a) ] <-asym a b a<b b<a = <-irrefl a (<-trans a b a a<b b<a) <-irrefl'' : ∀ a b → [ a < b ] ⊎ [ b < a ] → [ ¬(a ≡ₚ b) ] <-irrefl'' a b (inl a<b) a≡b = <-irrefl b (substₚ (λ p → p < b) a≡b a<b) <-irrefl'' a b (inr b<a) a≡b = <-irrefl b (substₚ (λ p → b < p) a≡b b<a) <-tricho : (a b : ℚ) → ([ a < b ] ⊎ [ b < a ]) ⊎ [ a ≡ₚ b ] -- TODO: insert trichotomy definition here <-tricho = SetQuotient.elimProp2 {R = _∼_} (λ a b → isProp[] ([ <-irrefl'' a b ] ([ <-asym a b ] (a < b) ⊎ᵖ (b < a)) ⊎ᵖ (a ≡ₚ b))) γ where γ : (a b : ℤ × ℕ₊₁) → ([ [ a ]ᶠ < [ b ]ᶠ ] ⊎ [ [ b ]ᶠ < [ a ]ᶠ ]) ⊎ [ [ a ]ᶠ ≡ₚ [ b ]ᶠ ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ κ : ([ (aᶻ ·ᶻ bⁿᶻ) <ᶻ (bᶻ ·ᶻ aⁿᶻ) ] ⊎ [ (bᶻ ·ᶻ aⁿᶻ) <ᶻ (aᶻ ·ᶻ bⁿᶻ) ]) ⊎ [ [ aᶻ , aⁿ ]ᶠ ≡ₚ [ bᶻ , bⁿ ]ᶠ ] κ with <ᶻ-tricho (aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) ... | inl p = inl p ... | inr p = inr ∣ eq/ {R = _∼_} a b p ∣ <-StrictLinearOrder : [ isStrictLinearOrder _<_ ] <-StrictLinearOrder .IsStrictLinearOrder.is-irrefl = <-irrefl <-StrictLinearOrder .IsStrictLinearOrder.is-trans = <-trans <-StrictLinearOrder .IsStrictLinearOrder.is-tricho = <-tricho <-StrictPartialOrder : [ isStrictPartialOrder _<_ ] <-StrictPartialOrder = strictlinearorder⇒strictpartialorder _<_ <-StrictLinearOrder _#_ : hPropRel ℚ ℚ ℓ-zero x # y = [ <-asym x y ] (x < y) ⊎ᵖ (y < x) #-ApartnessRel : [ isApartnessRel _#_ ] #-ApartnessRel = #''-isApartnessRel <-StrictPartialOrder <-asym open IsApartnessRel #-ApartnessRel public renaming ( is-irrefl to #-irrefl ; is-sym to #-sym ; is-cotrans to #-cotrans ) #-split' : ∀ z n → [ [ z , n ]ᶠ # 0 ] → [ z <ᶻ 0 ] ⊎ [ 0 <ᶻ z ] #-split' (pos zero) _ p = p #-split' (neg zero) _ p = p #-split' (posneg i) _ p = p #-split' (pos (suc n)) _ p = transport (λ i → [ suc (·ⁿ-identityʳ n i) <ⁿ 0 ] ⊎ [ 0 <ⁿ suc (·ⁿ-identityʳ n i) ]) p #-split' (neg (suc n)) _ p = p _⁻¹' : (x : ℤ × ℕ₊₁) → [ [ x ]ᶠ # 0 ] → ℚ (xᶻ , xⁿ) ⁻¹' = λ _ → [ signed (signᶻ xᶻ) (ℕ₊₁→ℕ xⁿ) , absᶻ⁺¹ xᶻ ]ᶠ ⁻¹'-respects-∼ : (a b : ℤ × ℕ₊₁) (p : a ∼ b) → PathP (λ i → [ eq/ a b p i # 0 ] → ℚ) (a ⁻¹') (b ⁻¹') ⁻¹'-respects-∼ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) p = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ; 0<aⁿᶻ : [ 0 <ᶻ aⁿᶻ ]; 0<aⁿᶻ = is-0<ⁿᶻ -- ℕ₊₁.n aⁿ , +ⁿ-comm (ℕ₊₁.n aⁿ) 1 bⁿᶻ = [1+ bⁿ ⁿ]ᶻ; 0<bⁿᶻ : [ 0 <ᶻ bⁿᶻ ]; 0<bⁿᶻ = is-0<ⁿᶻ -- ℕ₊₁.n bⁿ , +ⁿ-comm (ℕ₊₁.n bⁿ) 1 η : signᶻ aᶻ ≡ signᶻ bᶻ η i = sign (eq/ a b p i) s : aᶻ ·ᶻ bⁿᶻ ≡ bᶻ ·ᶻ aⁿᶻ s = p r : [ [ aᶻ , aⁿ ]ᶠ # 0 ] → (signed (signᶻ aᶻ) (ℕ₊₁→ℕ aⁿ) , absᶻ⁺¹ aᶻ) ∼ (signed (signᶻ bᶻ) (ℕ₊₁→ℕ bⁿ) , absᶻ⁺¹ bᶻ) r q = φ where φ : signed (signᶻ aᶻ) (ℕ₊₁→ℕ aⁿ) ·ᶻ [1+ absᶻ⁺¹ bᶻ ⁿ]ᶻ ≡ signed (signᶻ bᶻ) (ℕ₊₁→ℕ bⁿ) ·ᶻ [1+ absᶻ⁺¹ aᶻ ⁿ]ᶻ φ with #-split' aᶻ aⁿ q -- proof for aᶻ < 0 ... | inl aᶻ<0 = signed (signᶻ aᶻ) (ℕ₊₁→ℕ aⁿ) ·ᶻ [1+ absᶻ⁺¹ bᶻ ⁿ]ᶻ ≡⟨ (λ i → signed (<ᶻ0-signᶻ aᶻ aᶻ<0 i) (ℕ₊₁→ℕ aⁿ) ·ᶻ absᶻ⁺¹-identity bᶻ (inl bᶻ<0) i) ⟩ signed sneg (ℕ₊₁→ℕ aⁿ) ·ᶻ pos (absᶻ bᶻ) ≡⟨ cong₂ signed (λ i → sneg ·ˢ q₁ i) q₂ ⟩ signed sneg (ℕ₊₁→ℕ bⁿ) ·ᶻ pos (absᶻ aᶻ) ≡⟨ (λ i → signed (<ᶻ0-signᶻ bᶻ bᶻ<0 (~ i)) (ℕ₊₁→ℕ bⁿ) ·ᶻ absᶻ⁺¹-identity aᶻ (inl aᶻ<0) (~ i)) ⟩ signed (signᶻ bᶻ) (ℕ₊₁→ℕ bⁿ) ·ᶻ [1+ absᶻ⁺¹ aᶻ ⁿ]ᶻ ∎ where bᶻ<0 : [ bᶻ <ᶻ 0 ] bᶻ<0 = ∼-preserves-<ᶻ aᶻ aⁿ bᶻ bⁿ p .fst aᶻ<0 abstract c = suc (<ᶻ-split-neg aᶻ aᶻ<0 .fst) d = suc (<ᶻ-split-neg bᶻ bᶻ<0 .fst) aᶻ≡-c : aᶻ ≡ neg c aᶻ≡-c = <ᶻ-split-neg aᶻ aᶻ<0 .snd bᶻ≡-d : bᶻ ≡ neg d bᶻ≡-d = <ᶻ-split-neg bᶻ bᶻ<0 .snd absa≡c : absᶻ aᶻ ≡ c absa≡c i = absᶻ (aᶻ≡-c i) absb≡d : absᶻ bᶻ ≡ d absb≡d i = absᶻ (bᶻ≡-d i) q₁ : signᶻ (pos (absᶻ bᶻ)) ≡ signᶻ (pos (absᶻ aᶻ)) q₁ = transport (λ i → signᶻ-pos (absᶻ bᶻ) (~ i) ≡ signᶻ-pos (absᶻ aᶻ) (~ i)) refl s' = bᶻ ·ᶻ aⁿᶻ ≡ aᶻ ·ᶻ bⁿᶻ ≡⟨ (λ i → ·ᶻ-comm bᶻ aⁿᶻ i ≡ ·ᶻ-comm aᶻ bⁿᶻ i) ⟩ aⁿᶻ ·ᶻ bᶻ ≡ bⁿᶻ ·ᶻ aᶻ ≡⟨ (λ i → aⁿᶻ ·ᶻ bᶻ≡-d i ≡ bⁿᶻ ·ᶻ aᶻ≡-c i) ⟩ aⁿᶻ ·ᶻ neg d ≡ bⁿᶻ ·ᶻ neg c ≡⟨ refl ⟩ signed (signᶻ (neg d)) (suc (ℕ₊₁.n aⁿ) ·ⁿ d) ≡ signed (signᶻ (neg c)) (suc (ℕ₊₁.n bⁿ) ·ⁿ c) ∎ q₂ = (suc (ℕ₊₁.n aⁿ) ·ⁿ d ≡ suc (ℕ₊₁.n bⁿ) ·ⁿ c ⇒⟨ transport (λ i → suc (ℕ₊₁.n aⁿ) ·ⁿ absb≡d (~ i) ≡ suc (ℕ₊₁.n bⁿ) ·ⁿ absa≡c (~ i)) ⟩ suc (ℕ₊₁.n aⁿ) ·ⁿ absᶻ bᶻ ≡ suc (ℕ₊₁.n bⁿ) ·ⁿ absᶻ aᶻ ◼) (λ i → absᶻ (transport s' (sym s) i)) -- proof for 0 < aᶻ ... | inr 0<aᶻ = signed (signᶻ aᶻ ⊕ spos) (suc (ℕ₊₁.n aⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ bᶻ))) ≡⟨ (λ i → signed (0<ᶻ-signᶻ aᶻ 0<aᶻ i ⊕ spos) (suc (ℕ₊₁.n aⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ bᶻ)))) ⟩ signed spos (suc (ℕ₊₁.n aⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ bᶻ))) ≡⟨ transport s' (sym s) ⟩ signed spos (suc (ℕ₊₁.n bⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ aᶻ))) ≡⟨ (λ i → signed (0<ᶻ-signᶻ bᶻ 0<bᶻ (~ i) ⊕ spos) (suc (ℕ₊₁.n bⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ aᶻ)))) ⟩ signed (signᶻ bᶻ ⊕ spos) (suc (ℕ₊₁.n bⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ aᶻ))) ∎ where 0<bᶻ : [ 0 <ᶻ bᶻ ] 0<bᶻ = ∼-preserves-<ᶻ aᶻ aⁿ bᶻ bⁿ p .snd 0<aᶻ abstract c = <ᶻ-split-pos aᶻ 0<aᶻ .fst d = <ᶻ-split-pos bᶻ 0<bᶻ .fst aᶻ≡sc : aᶻ ≡ pos (suc c) bᶻ≡sd : bᶻ ≡ pos (suc d) absa≡sc : absᶻ aᶻ ≡ suc c absb≡sd : absᶻ bᶻ ≡ suc d aᶻ≡sc = <ᶻ-split-pos aᶻ 0<aᶻ .snd bᶻ≡sd = <ᶻ-split-pos bᶻ 0<bᶻ .snd absa≡sc i = absᶻ (aᶻ≡sc i) absb≡sd i = absᶻ (bᶻ≡sd i) s' = bᶻ ·ᶻ aⁿᶻ ≡ aᶻ ·ᶻ bⁿᶻ ≡⟨ (λ i → ·ᶻ-comm bᶻ aⁿᶻ i ≡ ·ᶻ-comm aᶻ bⁿᶻ i) ⟩ aⁿᶻ ·ᶻ bᶻ ≡ bⁿᶻ ·ᶻ aᶻ ≡⟨ (λ i → aⁿᶻ ·ᶻ bᶻ≡sd i ≡ bⁿᶻ ·ᶻ aᶻ≡sc i) ⟩ aⁿᶻ ·ᶻ pos (suc d) ≡ bⁿᶻ ·ᶻ pos (suc c) ≡⟨ refl ⟩ pos (suc (ℕ₊₁.n aⁿ) ·ⁿ suc d) ≡ pos (suc (ℕ₊₁.n bⁿ) ·ⁿ suc c) ≡⟨ (λ i → pos (suc (ℕ₊₁.n aⁿ) ·ⁿ absb≡sd (~ i)) ≡ pos (suc (ℕ₊₁.n bⁿ) ·ⁿ absa≡sc (~ i))) ⟩ pos (suc (ℕ₊₁.n aⁿ) ·ⁿ absᶻ bᶻ) ≡ pos (suc (ℕ₊₁.n bⁿ) ·ⁿ absᶻ aᶻ) ≡⟨ (λ i → pos (suc (ℕ₊₁.n aⁿ) ·ⁿ absᶻ⁺¹-identityⁿ bᶻ (inr 0<bᶻ) (~ i)) ≡ pos (suc (ℕ₊₁.n bⁿ) ·ⁿ absᶻ⁺¹-identityⁿ aᶻ (inr 0<aᶻ) (~ i))) ⟩ pos (suc (ℕ₊₁.n aⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ bᶻ))) ≡ pos (suc (ℕ₊₁.n bⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ aᶻ))) ∎ -- eq/ a b r : [ a ]ᶠ ≡ [ b ]ᶠ γ : [ [ a ]ᶠ # 0 ] ≡ [ [ b ]ᶠ # 0 ] γ i = [ eq/ a b p i # 0 ] κ : PathP _ (a ⁻¹') (b ⁻¹') κ i = λ(q : [ eq/ a b p i # 0 ]) → eq/ (signed (signᶻ aᶻ) (ℕ₊₁→ℕ aⁿ) , absᶻ⁺¹ aᶻ) (signed (signᶻ bᶻ) (ℕ₊₁→ℕ bⁿ) , absᶻ⁺¹ bᶻ) (r (ψ q)) i where ψ : [ eq/ a b p i # 0 ] → [ eq/ a b p i0 # 0 ] ψ p = transport (λ j → γ (i ∧ ~ j)) p _⁻¹ : (x : ℚ) → {{ _ : [ x # 0 ]}} → ℚ (x ⁻¹) {{p}} = SetQuotient.elim {R = _∼_} {B = λ x → [ x # 0 ] → ℚ} φ _⁻¹' ⁻¹'-respects-∼ x p where φ : ∀ x → isSet ([ x # 0 ] → ℚ) φ x = isSetΠ (λ _ → isSetℚ) -- φ' : ∀ x → isSet ({{_ : [ x # 0 ]}} → ℚ) -- φ' x = transport (λ i → ∀ x → isSet (instance≡ {A = [ x # 0 ]} {B = λ _ → ℚ} (~ i))) φ x ⊕-diagonal : ∀ s → s ⊕ s ≡ spos ⊕-diagonal spos = refl ⊕-diagonal sneg = refl zeroᶠ : ∀ x → [ 0 / x ] ≡ 0 zeroᶠ x = eq/ (0 , x) (0 , 1) refl #⇒#ᶻ : ∀ xᶻ xⁿ → [ [ xᶻ / xⁿ ] # 0 ] → [ xᶻ #ᶻ 0 ] #⇒#ᶻ (pos zero) xⁿ p = p #⇒#ᶻ (neg zero) xⁿ p = p #⇒#ᶻ (posneg i) xⁿ p = p #⇒#ᶻ (pos (suc n)) xⁿ (inl p) = inl (transport (λ i → [ suc (·ⁿ-identityʳ n i) <ⁿ 0 ]) p) #⇒#ᶻ (pos (suc n)) xⁿ (inr p) = inr (transport (λ i → [ 0 <ⁿ suc (·ⁿ-identityʳ n i) ]) p) #⇒#ᶻ (neg (suc n)) xⁿ p = inl tt #ᶻ⇒# : ∀ xᶻ xⁿ → [ xᶻ #ᶻ 0 ] → [ [ xᶻ / xⁿ ] # 0 ] #ᶻ⇒# (pos zero) xⁿ p = p #ᶻ⇒# (neg zero) xⁿ p = p #ᶻ⇒# (posneg i) xⁿ p = p #ᶻ⇒# (pos (suc n)) xⁿ (inl p) = inl (transport (λ i → [ suc (·ⁿ-identityʳ n (~ i)) <ⁿ 0 ]) p) #ᶻ⇒# (pos (suc n)) xⁿ (inr p) = inr (transport (λ i → [ 0 <ⁿ suc (·ⁿ-identityʳ n (~ i)) ]) p) #ᶻ⇒# (neg (suc n)) xⁿ p = inl tt ·-invʳ' : ∀ x → (p : [ [ x ]ᶠ # 0 ]) → [ x ]ᶠ · ([ x ]ᶠ ⁻¹) {{p}} ≡ 1 ·-invʳ' x@(xᶻ , xⁿ) p = γ where aᶻ : ℤ aⁿ : ℕ₊₁ aᶻ = signed (signᶻ xᶻ ⊕ signᶻ xᶻ) (absᶻ xᶻ ·ⁿ suc (ℕ₊₁.n xⁿ)) aⁿ = 1+ (ℕ₊₁.n (absᶻ⁺¹ xᶻ) +ⁿ ℕ₊₁.n xⁿ ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ xᶻ))) aⁿᶻ = [1+ aⁿ ⁿ]ᶻ η = absᶻ xᶻ ·ⁿ suc (ℕ₊₁.n xⁿ) ≡⟨ ·ⁿ-comm (absᶻ xᶻ) (suc (ℕ₊₁.n xⁿ)) ⟩ suc (ℕ₊₁.n xⁿ) ·ⁿ absᶻ xᶻ ≡⟨ (λ i → suc (ℕ₊₁.n xⁿ) ·ⁿ absᶻ⁺¹-identityⁿ xᶻ (#⇒#ᶻ xᶻ xⁿ p) (~ i)) ⟩ suc (ℕ₊₁.n xⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ xᶻ)) ∎ ψ : aᶻ ≡ aⁿᶻ ψ = signed (signᶻ xᶻ ⊕ signᶻ xᶻ) (absᶻ xᶻ ·ⁿ suc (ℕ₊₁.n xⁿ)) ≡⟨ cong₂ signed (⊕-diagonal (signᶻ xᶻ)) refl ⟩ signed spos (absᶻ xᶻ ·ⁿ suc (ℕ₊₁.n xⁿ)) ≡⟨ cong pos η ⟩ pos (suc (ℕ₊₁.n xⁿ) ·ⁿ suc (ℕ₊₁.n (absᶻ⁺¹ xᶻ))) ∎ φ : aᶻ ·ᶻ 1 ≡ 1 ·ᶻ aⁿᶻ φ = ·ᶻ-identity aᶻ .fst ∙ ψ ∙ sym (·ᶻ-identity aⁿᶻ .snd) κ : (aᶻ , aⁿ) ∼ (pos 1 , (1+ 0)) κ = φ γ : [ aᶻ , aⁿ ]ᶠ ≡ 1 γ = eq/ (aᶻ , aⁿ) (pos 1 , (1+ 0)) κ ·-invʳ : ∀ x → (p : [ x # 0 ]) → x · (x ⁻¹) {{p}} ≡ 1 ·-invʳ = let P : ℚ → hProp ℓ-zero P x = ∀ᵖ[ p ∶ x # 0 ] ([ isSetℚ ] x · (x ⁻¹) {{p}} ≡ˢ 1) in SetQuotient.elimProp {R = _∼_} (λ x → isProp[] (P x)) ·-invʳ' ·-invˡ : ∀ x → (p : [ x # 0 ]) → (x ⁻¹) {{p}} · x ≡ 1 ·-invˡ x p = ·-comm _ x ∙ ·-invʳ x p -- NOTE: we already have -- ℚ-cancelˡ : ∀ {a b} (c : ℕ₊₁) → [ ℕ₊₁→ℤ c ℤ.* a / c *₊₁ b ] ≡ [ a / b ] -- ℚ-cancelʳ : ∀ {a b} (c : ℕ₊₁) → [ a ℤ.* ℕ₊₁→ℤ c / b *₊₁ c ] ≡ [ a / b ] module _ (aᶻ : ℤ) (aⁿ bⁿ : ℕ₊₁) (let aⁿᶻ = [1+ aⁿ ⁿ]ᶻ) (let bⁿᶻ = [1+ bⁿ ⁿ]ᶻ) where private lem : ∀ a b → signᶻ a ≡ signᶻ (signed (signᶻ a ⊕ spos) (absᶻ a ·ⁿ (ℕ₊₁→ℕ b))) lem (pos zero) b j = signᶻ (posneg (i0 ∧ ~ j)) lem (neg zero) b j = signᶻ (posneg (i1 ∧ ~ j)) lem (posneg i) b j = signᶻ (posneg (i ∧ ~ j)) lem (pos (suc n)) b = refl lem (neg (suc n)) b = refl -- one could write this as a one-liner, even without mentioning cᶻ and cⁿ, but this reduces the overall proof-readability expand-fraction' : [ aᶻ / aⁿ ] ≡ [ aᶻ ·ᶻ bⁿᶻ / aⁿ ·₊₁ bⁿ ] expand-fraction' = eq/ _ _ $ (λ i → signed (lem aᶻ bⁿ i ⊕ spos) (((λ i → absᶻ aᶻ ·ⁿ ·ⁿ-comm (ℕ₊₁→ℕ aⁿ) (ℕ₊₁→ℕ bⁿ) i) ∙ ·ⁿ-assoc (absᶻ aᶻ) (ℕ₊₁→ℕ bⁿ) (ℕ₊₁→ℕ aⁿ)) i)) expand-fraction : [ aᶻ / aⁿ ] ≡ [ aᶻ ·ᶻ bⁿᶻ / aⁿ ·₊₁ bⁿ ] expand-fraction = eq/ _ _ γ where cᶻ : ℤ cᶻ = signed (signᶻ aᶻ ⊕ spos) (absᶻ aᶻ ·ⁿ (ℕ₊₁→ℕ bⁿ)) cⁿ : ℕ₊₁ cⁿ = 1+ (ℕ₊₁.n bⁿ +ⁿ ℕ₊₁.n aⁿ ·ⁿ (ℕ₊₁→ℕ bⁿ)) κ = absᶻ aᶻ ·ⁿ ((ℕ₊₁→ℕ aⁿ) ·ⁿ (ℕ₊₁→ℕ bⁿ)) ≡[ i ]⟨ absᶻ aᶻ ·ⁿ ·ⁿ-comm (ℕ₊₁→ℕ aⁿ) (ℕ₊₁→ℕ bⁿ) i ⟩ absᶻ aᶻ ·ⁿ ((ℕ₊₁→ℕ bⁿ) ·ⁿ (ℕ₊₁→ℕ aⁿ)) ≡⟨ ·ⁿ-assoc (absᶻ aᶻ) (ℕ₊₁→ℕ bⁿ) (ℕ₊₁→ℕ aⁿ) ⟩ (absᶻ aᶻ ·ⁿ (ℕ₊₁→ℕ bⁿ)) ·ⁿ (ℕ₊₁→ℕ aⁿ) ∎ γ : (aᶻ , aⁿ) ∼ (cᶻ , cⁿ) γ i = signed (lem aᶻ bⁿ i ⊕ spos) (κ i) ·-≡' : ∀ aᶻ aⁿ bᶻ bⁿ → [ aᶻ / aⁿ ] · [ bᶻ / bⁿ ] ≡ [ aᶻ ·ᶻ bᶻ / aⁿ ·₊₁ bⁿ ] ·-≡' aᶻ aⁿ bᶻ bⁿ = refl ∼⁻¹ : ∀ aᶻ aⁿ bᶻ bⁿ → [ aᶻ / aⁿ ] ≡ [ bᶻ / bⁿ ] → (aᶻ , aⁿ) ∼ (bᶻ , bⁿ) ∼⁻¹ aᶻ aⁿ bᶻ bⁿ p = eq/⁻¹ _ _ p -- already have this in ᶻ signᶻ-absᶻ-identity : ∀ a → signed (signᶻ a) (absᶻ a) ≡ a signᶻ-absᶻ-identity (pos zero) j = posneg (i0 ∧ j) signᶻ-absᶻ-identity (neg zero) j = posneg (i1 ∧ j) signᶻ-absᶻ-identity (posneg i) j = posneg (i ∧ j) signᶻ-absᶻ-identity (pos (suc n)) = refl signᶻ-absᶻ-identity (neg (suc n)) = refl -- already have this in ᶻ absᶻ-preserves-·ᶻ : ∀ a b → absᶻ (a ·ᶻ b) ≡ absᶻ a ·ⁿ absᶻ b absᶻ-preserves-·ᶻ a b = refl -- already have this in ᶻ signᶻ-absᶻ-≡ : ∀ a b → signᶻ a ≡ signᶻ b → absᶻ a ≡ absᶻ b → a ≡ b signᶻ-absᶻ-≡ a b p q = transport (λ i → signᶻ-absᶻ-identity a i ≡ signᶻ-absᶻ-identity b i) λ i → signed (p i) (q i) ℚ-reflects-nom : ∀ aᶻ bᶻ n → [ aᶻ / n ] ≡ [ bᶻ / n ] → aᶻ ≡ bᶻ ℚ-reflects-nom aᶻ bᶻ n p = signᶻ-absᶻ-≡ aᶻ bᶻ φ η where n' = suc (ℕ₊₁.n n) 0<n' : [ 0 <ⁿ n' ] 0<n' = 0<ⁿsuc (ℕ₊₁.n n) s = signed (signᶻ aᶻ ) (absᶻ aᶻ ·ⁿ n') ≡⟨ cong₂ signed (sym (⊕-identityʳ (signᶻ aᶻ))) refl ⟩ signed (signᶻ aᶻ ⊕ spos) (absᶻ aᶻ ·ⁿ n') ≡⟨ eq/⁻¹ _ _ p ⟩ signed (signᶻ bᶻ ⊕ spos) (absᶻ bᶻ ·ⁿ n') ≡⟨ cong₂ signed (⊕-identityʳ (signᶻ bᶻ)) refl ⟩ signed (signᶻ bᶻ ) (absᶻ bᶻ ·ⁿ n') ∎ φ : signᶻ aᶻ ≡ signᶻ bᶻ φ i = sign (p i) κ : absᶻ aᶻ ·ⁿ n' ≡ absᶻ bᶻ ·ⁿ n' κ i = absᶻ (s i) η : absᶻ aᶻ ≡ absᶻ bᶻ η = ·ⁿ-reflects-≡ʳ (absᶻ aᶻ) (absᶻ bᶻ) n' 0<n' κ ℕ₊₁→ℕ-reflects-≡ : ∀ a b → (ℕ₊₁→ℕ a) ≡ (ℕ₊₁→ℕ b) → a ≡ b ℕ₊₁→ℕ-reflects-≡ (1+ a) (1+ b) p i = 1+ +ⁿ-preserves-≡ˡ {1} {a} {b} p i ℚ-reflects-denom : ∀ z aⁿ bⁿ → [ z #ᶻ 0 ] → [ z / aⁿ ] ≡ [ z / bⁿ ] → aⁿ ≡ bⁿ ℚ-reflects-denom z aⁿ bⁿ z#0 p = ℕ₊₁→ℕ-reflects-≡ aⁿ bⁿ (sym γ) where κ : absᶻ z ·ⁿ (ℕ₊₁→ℕ bⁿ) ≡ absᶻ z ·ⁿ (ℕ₊₁→ℕ aⁿ) κ i = absᶻ (eq/⁻¹ _ _ p i) φ : (z#0 : [ z #ᶻ 0 ]) → [ 0 <ⁿ absᶻ z ] φ (inl z<0) = let (n , z≡-sn) = <ᶻ-split-neg z z<0 in transport (λ i → [ 0 <ⁿ absᶻ (z≡-sn (~ i)) ]) (0<ⁿsuc n) φ (inr 0<z) = let (n , z≡+sn) = <ᶻ-split-pos z 0<z in transport (λ i → [ 0 <ⁿ absᶻ (z≡+sn (~ i)) ]) (0<ⁿsuc n) γ : ℕ₊₁→ℕ bⁿ ≡ ℕ₊₁→ℕ aⁿ γ = ·ⁿ-reflects-≡ˡ (ℕ₊₁→ℕ bⁿ) (ℕ₊₁→ℕ aⁿ) (absᶻ z) (φ z#0) κ -- already have this in QuoInt pos-reflects-≡ : ∀ a b → pos a ≡ pos b → a ≡ b pos-reflects-≡ a b p i = absᶻ (p i) snotz' : ∀ n → ¬ᵗ (suc n ≡ 0) snotz' n p = let caseNat = λ{ 0 → ⊥⊥ ; (suc n) → ℕ } in subst caseNat p 0 ¬0≡1ⁿ : ¬ᵗ _≡_ {A = ℕ} 0 1 ¬0≡1ⁿ p = snotzⁿ {0} (sym p) ¬0≡1ᶻ : ¬ᵗ _≡_ {A = ℤ} 0 1 ¬0≡1ᶻ p = ¬0≡1ⁿ $ pos-reflects-≡ 0 1 p ¬0≡1ᶠ : ¬ᵗ _≡_ {A = ℚ} 0 1 ¬0≡1ᶠ p = ¬0≡1ᶻ $ ℚ-reflects-nom 0 1 1 p -- already have this in ᶻ signed0≡0 : ∀ s → signed s 0 ≡ 0 signed0≡0 spos = refl signed0≡0 sneg i = posneg (~ i) ·-nullifiesˡ' : ∀ bᶻ bⁿ → 0 · [ bᶻ / bⁿ ] ≡ 0 ·-nullifiesˡ' bᶻ bⁿ = [ signed (signᶻ bᶻ) 0 / (1+ (ℕ₊₁.n bⁿ +ⁿ 0)) ] ≡⟨ cong₂ [_/_] (signed0≡0 (signᶻ bᶻ)) refl ⟩ [ 0 / (1+ (ℕ₊₁.n bⁿ +ⁿ 0)) ] ≡⟨ zeroᶠ (1+ (ℕ₊₁.n bⁿ +ⁿ 0)) ⟩ [ 0 / (1+ 0) ] ∎ ·-nullifiesʳ' : ∀ bᶻ bⁿ → [ bᶻ / bⁿ ] · 0 ≡ 0 ·-nullifiesʳ' bᶻ bⁿ = ·-comm [ bᶻ / bⁿ ] 0 ∙ ·-nullifiesˡ' bᶻ bⁿ ·-inv-<ᶻ' : (a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) : ℤ × ℕ₊₁) → ([ a ]ᶠ · [ b ]ᶠ ≡ 1) → [ (0 <ᶻ aᶻ) ⊓ (0 <ᶻ bᶻ) ] ⊎ [ (aᶻ <ᶻ 0) ⊓ (bᶻ <ᶻ 0) ] ·-inv-<ᶻ' a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) p = γ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ κ = ([ aᶻ / aⁿ ] · [ bᶻ / bⁿ ] ≡ [ 1 / (1+ 0) ] ⇒⟨ transport (cong₂ _≡_ refl (transport (λ i → 1 ≡ [ ·ᶻ-identity aⁿᶻ .snd i / ·₊₁-identityˡ aⁿ i ]) (expand-fraction 1 1 aⁿ) ∙ expand-fraction aⁿᶻ aⁿ bⁿ)) ⟩ [ aᶻ ·ᶻ bᶻ / aⁿ ·₊₁ bⁿ ] ≡ [ aⁿᶻ ·ᶻ bⁿᶻ / aⁿ ·₊₁ bⁿ ] ⇒⟨ ℚ-reflects-nom _ _ (aⁿ ·₊₁ bⁿ) ⟩ aᶻ ·ᶻ bᶻ ≡ aⁿᶻ ·ᶻ bⁿᶻ ◼) p φ₀ : [ 0 <ⁿ ((ℕ₊₁→ℕ aⁿ) ·ⁿ (ℕ₊₁→ℕ bⁿ)) ] φ₀ = 0<ⁿsuc _ φ₁ : [ 0 <ᶻ aⁿᶻ ·ᶻ bⁿᶻ ] φ₁ = φ₀ φ₂ : [ 0 <ᶻ aᶻ ·ᶻ bᶻ ] φ₂ = subst (λ p → [ 0 <ᶻ p ]) (sym κ) φ₁ γ : [ (0 <ᶻ aᶻ) ⊓ (0 <ᶻ bᶻ) ] ⊎ [ (aᶻ <ᶻ 0) ⊓ (bᶻ <ᶻ 0) ] γ with <ᶻ-tricho 0 aᶻ ... | inl (inl 0<aᶻ) = inl (0<aᶻ , ·ᶻ-reflects-0<ᶻ aᶻ bᶻ φ₂ .fst .fst 0<aᶻ) ... | inl (inr aᶻ<0) = inr (aᶻ<0 , ·ᶻ-reflects-0<ᶻ aᶻ bᶻ φ₂ .snd .fst aᶻ<0) ... | inr 0≡aᶻ = ⊥-elim {A = λ _ → [ (0 <ᶻ aᶻ) ⊓ (0 <ᶻ bᶻ) ] ⊎ [ (aᶻ <ᶻ 0) ⊓ (bᶻ <ᶻ 0) ]} (¬0≡1ᶠ η) where η = 0 ≡⟨ sym $ zeroᶠ (aⁿ ·₊₁ bⁿ) ⟩ [ 0 / aⁿ ·₊₁ bⁿ ] ≡⟨ (λ i → [ ·ᶻ-nullifiesˡ bᶻ (~ i) / aⁿ ·₊₁ bⁿ ]) ⟩ [ 0 ·ᶻ bᶻ / aⁿ ·₊₁ bⁿ ] ≡⟨ (λ i → [ 0≡aᶻ i ·ᶻ bᶻ / aⁿ ·₊₁ bⁿ ]) ⟩ [ aᶻ ·ᶻ bᶻ / aⁿ ·₊₁ bⁿ ] ≡⟨ p ⟩ 1 ∎ private lem0<ᶻ : ∀ aᶻ → [ 0 <ᶻ aᶻ ] → aᶻ ≡ signed (signᶻ aᶻ ⊕ spos) (absᶻ aᶻ ·ⁿ 1) lem0<ᶻ aᶻ 0<aᶻ = aᶻ ≡⟨ γ ⟩ pos (suc n) ≡⟨ (λ i → pos (suc (·ⁿ-identityʳ n (~ i)))) ⟩ pos (suc (n ·ⁿ 1)) ≡⟨ refl ⟩ signed (signᶻ (pos (suc n)) ⊕ spos) (absᶻ (pos (suc n)) ·ⁿ 1) ≡⟨ (λ i → signed (signᶻ (γ (~ i)) ⊕ spos) (absᶻ (γ (~ i)) ·ⁿ 1)) ⟩ signed (signᶻ aᶻ ⊕ spos) (absᶻ aᶻ ·ⁿ 1) ∎ where abstract n = <ᶻ-split-pos aᶻ 0<aᶻ .fst γ : aᶻ ≡ pos (suc n) γ = <ᶻ-split-pos aᶻ 0<aᶻ .snd lem<ᶻ0 : ∀ aᶻ → [ aᶻ <ᶻ 0 ] → aᶻ ≡ signed (signᶻ aᶻ ⊕ spos) (absᶻ aᶻ ·ⁿ 1) lem<ᶻ0 aᶻ aᶻ<0 = aᶻ ≡⟨ γ ⟩ neg (suc n) ≡⟨ (λ i → neg (suc (·ⁿ-identityʳ n (~ i)))) ⟩ neg (suc (n ·ⁿ 1)) ≡⟨ refl ⟩ signed (signᶻ (neg (suc n)) ⊕ spos) (absᶻ (neg (suc n)) ·ⁿ 1) ≡⟨ (λ i → signed (signᶻ (γ (~ i)) ⊕ spos) (absᶻ (γ (~ i)) ·ⁿ 1)) ⟩ signed (signᶻ aᶻ ⊕ spos) (absᶻ aᶻ ·ⁿ 1) ∎ where abstract n = <ᶻ-split-neg aᶻ aᶻ<0 .fst γ : aᶻ ≡ neg (suc n) γ = <ᶻ-split-neg aᶻ aᶻ<0 .snd ·-inv#0' : (a b : ℤ × ℕ₊₁) → ([ a ]ᶠ · [ b ]ᶠ ≡ 1) → [ [ a ]ᶠ # 0 ⊓ [ b ]ᶠ # 0 ] ·-inv#0' a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) p with ·-inv-<ᶻ' a b p ... | inl (0<aᶻ , 0<bᶻ) = inr (subst (λ p → [ 0 <ᶻ p ]) (lem0<ᶻ aᶻ 0<aᶻ) 0<aᶻ) , inr (subst (λ p → [ 0 <ᶻ p ]) (lem0<ᶻ bᶻ 0<bᶻ) 0<bᶻ) ... | inr (aᶻ<0 , bᶻ<0) = inl (subst (λ p → [ p <ᶻ 0 ]) (lem<ᶻ0 aᶻ aᶻ<0) aᶻ<0) , inl (subst (λ p → [ p <ᶻ 0 ]) (lem<ᶻ0 bᶻ bᶻ<0) bᶻ<0) ·-inv#0 : ∀ x y → x · y ≡ 1 → [(x # 0) ⊓ (y # 0)] ·-inv#0 = let P : ℚ → ℚ → hProp ℓ-zero P x y = ([ isSetℚ ] (x · y) ≡ˢ 1) ⇒ ((x # 0) ⊓ (y # 0)) in SetQuotient.elimProp2 {R = _∼_} {C = λ x y → [ P x y ]} (λ x y → isProp[] (P x y)) ·-inv#0' ·-inv#0ˡ' : (a b : ℤ × ℕ₊₁) → ([ a ]ᶠ · [ b ]ᶠ ≡ 1) → [ [ a ]ᶠ # 0 ] ·-inv#0ˡ' a b p = ·-inv#0' a b p .fst ·-inv#0ˡ : ∀ x y → x · y ≡ 1 → [ x # 0 ] ·-inv#0ˡ x y p = ·-inv#0 x y p .fst -- ·-reflects-signʳ : ∀ a b c → [ 0 < c ] → a · b ≡ c → [ ((0 < b) ⇒ (0 < a)) ⊓ ((b < 0) ⇒ (a < 0)) ] -- ·-reflects-signʳ a b c p q .fst 0<b = {! !} -- ·-reflects-signʳ a b c p q .snd b<0 = {! !} -- ⁻¹-preserves-sign : ∀ z z⁻¹ → [ 0f < z ] → z · z⁻¹ ≡ 1f → [ 0f < z⁻¹ ] -- ⁻¹-preserves-sign z z⁻¹ -- -- TODO: this is a plain copy from `MorePropAlgebra.Properties.AlmostPartiallyOrderedField` -- -- we might put it into `MorePropAlgebra.Consequences` -- -- uniqueness of inverses from `·-assoc` + `·-comm` + `·-lid` + `·-rid` -- ·-rinv-unique'' : (x y z : F) → [ x · y ≡ˢ 1f ] → [ x · z ≡ˢ 1f ] → [ y ≡ˢ z ] -- ·-rinv-unique'' x y z x·y≡1 x·z≡1 = -- ( x · y ≡ˢ 1f ⇒ᵖ⟨ (λ x·y≡1 i → z · x·y≡1 i) ⟩ -- z · (x · y) ≡ˢ z · 1f ⇒ᵖ⟨ pathTo⇒ (λ i → ·-assoc z x y i ≡ˢ ·-rid z i) ⟩ -- (z · x) · y ≡ˢ z ⇒ᵖ⟨ pathTo⇒ (λ i → (·-comm z x i) · y ≡ˢ z) ⟩ -- (x · z) · y ≡ˢ z ⇒ᵖ⟨ pathTo⇒ (λ i → x·z≡1 i · y ≡ˢ z) ⟩ -- 1f · y ≡ˢ z ⇒ᵖ⟨ pathTo⇒ (λ i → ·-lid y i ≡ˢ z) ⟩ -- y ≡ˢ z ◼ᵖ) .snd x·y≡1 ·-inv'' : ∀ x → [ (∃[ y ] ([ isSetℚ ] (x · y) ≡ˢ 1)) ⇔ (x # 0) ] ·-inv'' x .fst p = ∣∣-elim (λ _ → φ') (λ{ (y , q) → γ y q } ) p where φ' = isProp[] (x # 0) γ : ∀ y → [ [ isSetℚ ] (x · y) ≡ˢ 1 ] → [ x # 0 ] γ y q = ·-inv#0ˡ x y q ·-inv'' x .snd p = ∣ (x ⁻¹) {{p}} , ·-invʳ x p ∣ -- -- note, that the following holds definitionally (TODO: put this at the definition of `min`) -- _ = min [ aᶻ , aⁿ ]ᶠ [ bᶻ , bⁿ ]ᶠ ≡⟨ refl ⟩ -- [ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) , aⁿ *₊₁ bⁿ) ]ᶠ ∎ -- -- and we also have definitionally -- _ : [1+ aⁿ *₊₁ bⁿ ⁿ]ᶻ ≡ aⁿᶻ *ᶻ bⁿᶻ -- _ = refl -- -- therefore, we have for the LHS: -- _ = ([ cᶻ , cⁿ ]ᶠ ≤ min [ aᶻ , aⁿ ]ᶠ [ bᶻ , bⁿ ]ᶠ) ≡⟨ refl ⟩ -- ([ cᶻ , cⁿ ]ᶠ ≤ [ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) , aⁿ *₊₁ bⁿ) ]ᶠ) ≡⟨ refl ⟩ -- (¬([ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) , aⁿ *₊₁ bⁿ) ]ᶠ < [ cᶻ , cⁿ ]ᶠ)) ≡⟨ refl ⟩ -- ¬((minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ))) ≡⟨ refl ⟩ -- ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ)) ∎ -- -- similar for the RHS: -- _ = ([ cᶻ , cⁿ ]ᶠ ≤ [ aᶻ , aⁿ ]ᶠ) ⊓ ([ cᶻ , cⁿ ]ᶠ ≤ [ bᶻ , bⁿ ]ᶠ) ≡⟨ refl ⟩ -- ¬([ aᶻ , aⁿ ]ᶠ < [ cᶻ , cⁿ ]ᶠ) ⊓ ¬([ bᶻ , bⁿ ]ᶠ < [ cᶻ , cⁿ ]ᶠ) ≡⟨ refl ⟩ -- ¬((aᶻ *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ aⁿᶻ)) ⊓ ¬((bᶻ *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ bⁿᶻ)) ≡⟨ refl ⟩ -- ((cᶻ *ᶻ aⁿᶻ) ≤ᶻ (aᶻ *ᶻ cⁿᶻ)) ⊓ ((cᶻ *ᶻ bⁿᶻ) ≤ᶻ (bᶻ *ᶻ cⁿᶻ)) ∎ -- -- therfore, only properties on ℤ remain -- RHS = [ ((cᶻ *ᶻ aⁿᶻ) ≤ᶻ (aᶻ *ᶻ cⁿᶻ)) ⊓ ((cᶻ *ᶻ bⁿᶻ) ≤ᶻ (bᶻ *ᶻ cⁿᶻ)) ] -- LHS = [ ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ)) ] -- strategy: multiply everything with aⁿᶻ, bⁿᶻ, cⁿᶻ is-min : (x y z : ℚ) → [ (z ≤ min x y) ⇔ (z ≤ x) ⊓ (z ≤ y) ] is-min = SetQuotient.elimProp3 {R = _∼_} (λ x y z → isProp[] ((z ≤ min x y) ⇔ (z ≤ x) ⊓ (z ≤ y))) γ where γ : (a b c : ℤ × ℕ₊₁) → [ ([ c ]ᶠ ≤ min [ a ]ᶠ [ b ]ᶠ) ⇔ ([ c ]ᶠ ≤ [ a ]ᶠ) ⊓ ([ c ]ᶠ ≤ [ b ]ᶠ) ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) = pathTo⇔ (sym κ) where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ; 0≤aⁿᶻ : [ 0 ≤ᶻ aⁿᶻ ]; 0≤aⁿᶻ = is-0≤ⁿᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ; 0≤bⁿᶻ : [ 0 ≤ᶻ bⁿᶻ ]; 0≤bⁿᶻ = is-0≤ⁿᶻ cⁿᶻ = [1+ cⁿ ⁿ]ᶻ; 0≤cⁿᶻ : [ 0 ≤ᶻ cⁿᶻ ]; 0≤cⁿᶻ = is-0≤ⁿᶻ κ = ( ((cᶻ ·ᶻ aⁿᶻ) ≤ᶻ (aᶻ ·ᶻ cⁿᶻ) ) ⊓ ((cᶻ ·ᶻ bⁿᶻ) ≤ᶻ (bᶻ ·ᶻ cⁿᶻ) ) ≡⟨ ⊓≡⊓ (·ᶻ-creates-≤ᶻ-≡ (cᶻ ·ᶻ aⁿᶻ) (aᶻ ·ᶻ cⁿᶻ) bⁿᶻ 0≤bⁿᶻ) (·ᶻ-creates-≤ᶻ-≡ (cᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ cⁿᶻ) aⁿᶻ 0≤aⁿᶻ) ⟩ ((cᶻ ·ᶻ aⁿᶻ) ·ᶻ bⁿᶻ ≤ᶻ (aᶻ ·ᶻ cⁿᶻ) ·ᶻ bⁿᶻ ) ⊓ ((cᶻ ·ᶻ bⁿᶻ) ·ᶻ aⁿᶻ ≤ᶻ (bᶻ ·ᶻ cⁿᶻ) ·ᶻ aⁿᶻ ) ≡⟨ ⊓≡⊓ (λ i → ·ᶻ-assoc cᶻ aⁿᶻ bⁿᶻ (~ i) ≤ᶻ ·ᶻ-assoc aᶻ cⁿᶻ bⁿᶻ (~ i)) (λ i → ·ᶻ-assoc cᶻ bⁿᶻ aⁿᶻ (~ i) ≤ᶻ ·ᶻ-assoc bᶻ cⁿᶻ aⁿᶻ (~ i)) ⟩ ( cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ) ≤ᶻ aᶻ ·ᶻ (cⁿᶻ ·ᶻ bⁿᶻ)) ⊓ ( cᶻ ·ᶻ (bⁿᶻ ·ᶻ aⁿᶻ) ≤ᶻ bᶻ ·ᶻ (cⁿᶻ ·ᶻ aⁿᶻ)) ≡⟨ ⊓≡⊓ (λ i → cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ) ≤ᶻ aᶻ ·ᶻ (·ᶻ-comm cⁿᶻ bⁿᶻ i)) (λ i → cᶻ ·ᶻ (·ᶻ-comm bⁿᶻ aⁿᶻ i) ≤ᶻ bᶻ ·ᶻ (·ᶻ-comm cⁿᶻ aⁿᶻ i)) ⟩ ( cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ) ≤ᶻ aᶻ ·ᶻ (bⁿᶻ ·ᶻ cⁿᶻ)) ⊓ ( cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ) ≤ᶻ bᶻ ·ᶻ (aⁿᶻ ·ᶻ cⁿᶻ)) ≡⟨ sym $ ⇔toPath' $ is-minᶻ (aᶻ ·ᶻ (bⁿᶻ ·ᶻ cⁿᶻ)) (bᶻ ·ᶻ (aⁿᶻ ·ᶻ cⁿᶻ)) (cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ⟩ ((cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ≤ᶻ minᶻ ( aᶻ ·ᶻ (bⁿᶻ ·ᶻ cⁿᶻ)) (bᶻ ·ᶻ (aⁿᶻ ·ᶻ cⁿᶻ))) ≡⟨ (λ i → ((cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ≤ᶻ minᶻ (·ᶻ-assoc aᶻ bⁿᶻ cⁿᶻ i) (·ᶻ-assoc bᶻ aⁿᶻ cⁿᶻ i))) ⟩ ((cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ≤ᶻ minᶻ ((aᶻ ·ᶻ bⁿᶻ) ·ᶻ cⁿᶻ) ((bᶻ ·ᶻ aⁿᶻ) ·ᶻ cⁿᶻ)) ≡⟨ (λ i → (cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ≤ᶻ ·ᶻ-minᶻ-distribʳ (aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) cⁿᶻ 0≤cⁿᶻ (~ i)) ⟩ ((cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ≤ᶻ (minᶻ ( aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) ·ᶻ cⁿᶻ)) ∎) is-max : (x y z : ℚ) → [ (max x y ≤ z) ⇔ (x ≤ z) ⊓ (y ≤ z) ] is-max = SetQuotient.elimProp3 {R = _∼_} (λ x y z → isProp[] ((max x y ≤ z) ⇔ (x ≤ z) ⊓ (y ≤ z))) γ where γ : (a b c : ℤ × ℕ₊₁) → [ (max [ a ]ᶠ [ b ]ᶠ ≤ [ c ]ᶠ) ⇔ ([ a ]ᶠ ≤ [ c ]ᶠ) ⊓ ([ b ]ᶠ ≤ [ c ]ᶠ) ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) = pathTo⇔ (sym κ) where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ; 0≤aⁿᶻ : [ 0 ≤ᶻ aⁿᶻ ]; 0≤aⁿᶻ = is-0≤ⁿᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ; 0≤bⁿᶻ : [ 0 ≤ᶻ bⁿᶻ ]; 0≤bⁿᶻ = is-0≤ⁿᶻ cⁿᶻ = [1+ cⁿ ⁿ]ᶻ; 0≤cⁿᶻ : [ 0 ≤ᶻ cⁿᶻ ]; 0≤cⁿᶻ = is-0≤ⁿᶻ κ = ( aᶻ ·ᶻ cⁿᶻ ≤ᶻ cᶻ ·ᶻ aⁿᶻ ) ⊓ ( bᶻ ·ᶻ cⁿᶻ ≤ᶻ cᶻ ·ᶻ bⁿᶻ ) ≡⟨ ⊓≡⊓ (·ᶻ-creates-≤ᶻ-≡ (aᶻ ·ᶻ cⁿᶻ) (cᶻ ·ᶻ aⁿᶻ) bⁿᶻ 0≤bⁿᶻ) (·ᶻ-creates-≤ᶻ-≡ (bᶻ ·ᶻ cⁿᶻ) (cᶻ ·ᶻ bⁿᶻ) aⁿᶻ 0≤aⁿᶻ) ⟩ ((aᶻ ·ᶻ cⁿᶻ) ·ᶻ bⁿᶻ ≤ᶻ (cᶻ ·ᶻ aⁿᶻ) ·ᶻ bⁿᶻ) ⊓ ((bᶻ ·ᶻ cⁿᶻ) ·ᶻ aⁿᶻ ≤ᶻ (cᶻ ·ᶻ bⁿᶻ) ·ᶻ aⁿᶻ) ≡⟨ ⊓≡⊓ (λ i → ·ᶻ-assoc aᶻ cⁿᶻ bⁿᶻ (~ i) ≤ᶻ ·ᶻ-assoc cᶻ aⁿᶻ bⁿᶻ (~ i)) (λ i → ·ᶻ-assoc bᶻ cⁿᶻ aⁿᶻ (~ i) ≤ᶻ ·ᶻ-assoc cᶻ bⁿᶻ aⁿᶻ (~ i)) ⟩ (aᶻ ·ᶻ (cⁿᶻ ·ᶻ bⁿᶻ) ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ⊓ (bᶻ ·ᶻ (cⁿᶻ ·ᶻ aⁿᶻ) ≤ᶻ cᶻ ·ᶻ (bⁿᶻ ·ᶻ aⁿᶻ)) ≡⟨ ⊓≡⊓ (λ i → aᶻ ·ᶻ ·ᶻ-comm cⁿᶻ bⁿᶻ i ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) (λ i → bᶻ ·ᶻ ·ᶻ-comm cⁿᶻ aⁿᶻ i ≤ᶻ cᶻ ·ᶻ ·ᶻ-comm bⁿᶻ aⁿᶻ i) ⟩ (aᶻ ·ᶻ (bⁿᶻ ·ᶻ cⁿᶻ) ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ⊓ (bᶻ ·ᶻ (aⁿᶻ ·ᶻ cⁿᶻ) ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ≡⟨ sym $ ⇔toPath' $ is-maxᶻ (aᶻ ·ᶻ (bⁿᶻ ·ᶻ cⁿᶻ)) (bᶻ ·ᶻ (aⁿᶻ ·ᶻ cⁿᶻ)) (cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ⟩ maxᶻ (aᶻ ·ᶻ (bⁿᶻ ·ᶻ cⁿᶻ)) (bᶻ ·ᶻ (aⁿᶻ ·ᶻ cⁿᶻ)) ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ) ≡⟨ (λ i → maxᶻ (·ᶻ-assoc aᶻ bⁿᶻ cⁿᶻ i) (·ᶻ-assoc bᶻ aⁿᶻ cⁿᶻ i) ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ⟩ maxᶻ ((aᶻ ·ᶻ bⁿᶻ) ·ᶻ cⁿᶻ) ((bᶻ ·ᶻ aⁿᶻ) ·ᶻ cⁿᶻ) ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ) ≡⟨ (λ i → ·ᶻ-maxᶻ-distribʳ (aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) cⁿᶻ 0≤cⁿᶻ (~ i) ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ)) ⟩ maxᶻ (aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) ·ᶻ cⁿᶻ ≤ᶻ cᶻ ·ᶻ (aⁿᶻ ·ᶻ bⁿᶻ) ∎ private lem2 : ∀ a b c d → (a ·ᶻ c) ·ᶻ (b ·ᶻ d) ≡ (a ·ᶻ b) ·ᶻ (c ·ᶻ d) lem2' : ∀ a b c d → (a ·ᶻ c) ·ᶻ (b ·ᶻ d) ≡ (a ·ᶻ b) ·ᶻ (d ·ᶻ c) lem3 : ∀ a b c d → (a ·ᶻ c) ·ᶻ (b ·ᶻ d) ≡ (a ·ᶻ d) ·ᶻ (c ·ᶻ b) lem3' : ∀ a b c d → (a ·ᶻ c) ·ᶻ (b ·ᶻ d) ≡ (a ·ᶻ d) ·ᶻ (b ·ᶻ c) lem2 a b c d = ·ᶻ-commʳ a c (b ·ᶻ d) ∙ (λ i → ·ᶻ-assoc a b d i ·ᶻ c) ∙ sym (·ᶻ-assoc (a ·ᶻ b) d c) ∙ (λ i → (a ·ᶻ b) ·ᶻ ·ᶻ-comm d c i) lem2' a b c d = lem2 a b c d ∙ λ i → (a ·ᶻ b) ·ᶻ ·ᶻ-comm c d i lem3 a b c d = (λ i → a ·ᶻ c ·ᶻ ·ᶻ-comm b d i) ∙ sym (·ᶻ-assoc a c (d ·ᶻ b)) ∙ (λ i → a ·ᶻ ·ᶻ-commˡ c d b i) ∙ ·ᶻ-assoc a d (c ·ᶻ b) lem3' a b c d = lem3 a b c d ∙ λ i → (a ·ᶻ d) ·ᶻ ·ᶻ-comm c b i ·-preserves-< : (x y z : ℚ) → [ 0 < z ] → [ x < y ] → [ x · z < y · z ] ·-preserves-< = SetQuotient.elimProp3 {R = _∼_} (λ x y z → isProp[] ((0 < z) ⇒ (x < y) ⇒ (x · z < y · z))) γ where γ : (a b c : ℤ × ℕ₊₁) → [ 0 < [ c ]ᶠ ] → [ [ a ]ᶠ < [ b ]ᶠ ] → [ [ a ]ᶠ · [ c ]ᶠ < [ b ]ᶠ · [ c ]ᶠ ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ cⁿᶻ = [1+ cⁿ ⁿ]ᶻ; 0<cⁿᶻ : [ 0 <ᶻ cⁿᶻ ]; 0<cⁿᶻ = is-0<ⁿᶻ κ : [ 0 ·ᶻ cⁿᶻ <ᶻ cᶻ ·ᶻ 1 ] → [ aᶻ ·ᶻ bⁿᶻ <ᶻ bᶻ ·ᶻ aⁿᶻ ] → [ (aᶻ ·ᶻ cᶻ) ·ᶻ (bⁿᶻ ·ᶻ cⁿᶻ) <ᶻ (bᶻ ·ᶻ cᶻ) ·ᶻ (aⁿᶻ ·ᶻ cⁿᶻ) ] κ p q = transport (λ i → [ lem2 aᶻ bⁿᶻ cᶻ cⁿᶻ (~ i) <ᶻ lem2 bᶻ aⁿᶻ cᶻ cⁿᶻ (~ i) ]) $ ·ᶻ-preserves-<ᶻ (aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) (cᶻ ·ᶻ cⁿᶻ) 0<cᶻ·cⁿᶻ q where 0<cᶻ : [ 0 <ᶻ cᶻ ] 0<cᶻ = transport (λ i → [ ·ᶻ-nullifiesˡ cⁿᶻ i <ᶻ ·ᶻ-identity cᶻ .fst i ]) p 0<cᶻ·cⁿᶻ : [ 0 <ᶻ (cᶻ ·ᶻ cⁿᶻ) ] 0<cᶻ·cⁿᶻ = ·ᶻ-preserves-0<ᶻ cᶻ cⁿᶻ 0<cᶻ 0<cⁿᶻ private -- continuing the pattern... elimProp4 : ∀{ℓ} {A : Type ℓ} → {R : A → A → Type ℓ} → {E : A SetQuotient./ R → A SetQuotient./ R → A SetQuotient./ R → A SetQuotient./ R → Type ℓ} → ((x y z w : A SetQuotient./ R ) → isProp (E x y z w)) → ((a b c d : A) → E SetQuotient.[ a ] SetQuotient.[ b ] SetQuotient.[ c ] SetQuotient.[ d ]) → (x y z w : A SetQuotient./ R) → E x y z w elimProp4 Eprop f = SetQuotient.elimProp (λ x → isPropΠ3 (λ y z w → Eprop x y z w)) (λ x → SetQuotient.elimProp3 (λ y z w → Eprop SetQuotient.[ x ] y z w) (f x)) open import Cubical.HITs.Rationals.QuoQ using ( _+_ ; -_ ; +-assoc ; +-comm ; +-identityˡ ; +-identityʳ ; +-inverseˡ ; +-inverseʳ ) renaming ( *-identityˡ to ·-identityˡ ; *-identityʳ to ·-identityʳ ; *-distribˡ to ·-distribˡ ; *-distribʳ to ·-distribʳ ) -- the following hold definitionally: -- [ aᶻ , aⁿ ]ᶠ · [ bᶻ , bⁿ ]ᶠ ≡ [ aᶻ · bᶻ , aⁿ ·₊₁ bⁿ ]ᶠ -- [ aᶻ , aⁿ ]ᶠ < [ bᶻ , bⁿ ]ᶠ ≡ aᶻ ·ᶻ bⁿᶻ <ᶻ bᶻ ·ᶻ aⁿᶻ -- min [ aᶻ , aⁿ ]ᶠ [ bᶻ , bⁿ ]ᶠ ≡ [ (minᶻ (aᶻ ·ᶻ bⁿᶻ) (bᶻ ·ᶻ aⁿᶻ) , aⁿ ·₊₁ bⁿ) ]ᶠ -- [ aᶻ , aⁿ ]ᶠ + [ bᶻ , bⁿ ]ᶠ ≡ [ aᶻ ·ᶻ bⁿᶻ +ᶻ bᶻ ·ᶻ aⁿᶻ , aⁿ ·₊₁ bⁿ ]ᶠ +-<-ext : (w x y z : ℚ) → [ w + x < y + z ] → [ (w < y) ⊔ (x < z) ] +-<-ext = elimProp4 {R = _∼_} (λ w x y z → isProp[] ((w + x < y + z) ⇒ ((w < y) ⊔ (x < z)))) γ where γ : (w x y z : ℤ × ℕ₊₁) → [ [ w ]ᶠ + [ x ]ᶠ < [ y ]ᶠ + [ z ]ᶠ ] → [ ([ w ]ᶠ < [ y ]ᶠ) ⊔ ([ x ]ᶠ < [ z ]ᶠ) ] γ w@(wᶻ , wⁿ) x@(xᶻ , xⁿ) y@(yᶻ , yⁿ) z@(zᶻ , zⁿ) = κ where wⁿᶻ = [1+ wⁿ ⁿ]ᶻ; 0<wⁿᶻ : [ 0 <ᶻ wⁿᶻ ]; 0<wⁿᶻ = is-0<ⁿᶻ xⁿᶻ = [1+ xⁿ ⁿ]ᶻ; 0<xⁿᶻ : [ 0 <ᶻ xⁿᶻ ]; 0<xⁿᶻ = is-0<ⁿᶻ yⁿᶻ = [1+ yⁿ ⁿ]ᶻ; 0<yⁿᶻ : [ 0 <ᶻ yⁿᶻ ]; 0<yⁿᶻ = is-0<ⁿᶻ zⁿᶻ = [1+ zⁿ ⁿ]ᶻ; 0<zⁿᶻ : [ 0 <ᶻ zⁿᶻ ]; 0<zⁿᶻ = is-0<ⁿᶻ 0<xzⁿᶻ : [ 0 <ᶻ xⁿᶻ ·ᶻ zⁿᶻ ]; 0<xzⁿᶻ = ·ᶻ-preserves-0<ᶻ xⁿᶻ zⁿᶻ 0<xⁿᶻ 0<zⁿᶻ 0<wyⁿᶻ : [ 0 <ᶻ wⁿᶻ ·ᶻ yⁿᶻ ]; 0<wyⁿᶻ = ·ᶻ-preserves-0<ᶻ wⁿᶻ yⁿᶻ 0<wⁿᶻ 0<yⁿᶻ φ₁ = wᶻ ·ᶻ xⁿᶻ ·ᶻ (yⁿᶻ ·ᶻ zⁿᶻ) ≡⟨ lem2 wᶻ yⁿᶻ xⁿᶻ zⁿᶻ ⟩ wᶻ ·ᶻ yⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ) ∎ φ₂ = xᶻ ·ᶻ wⁿᶻ ·ᶻ (yⁿᶻ ·ᶻ zⁿᶻ) ≡⟨ lem3 xᶻ yⁿᶻ wⁿᶻ zⁿᶻ ⟩ xᶻ ·ᶻ zⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ) ∎ φ₃ = yᶻ ·ᶻ zⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ xⁿᶻ) ≡⟨ lem2' yᶻ wⁿᶻ zⁿᶻ xⁿᶻ ⟩ yᶻ ·ᶻ wⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ) ∎ φ₄ = zᶻ ·ᶻ yⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ xⁿᶻ) ≡⟨ lem3' zᶻ wⁿᶻ yⁿᶻ xⁿᶻ ⟩ zᶻ ·ᶻ xⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ) ∎ φ = ( (wᶻ ·ᶻ xⁿᶻ +ᶻ xᶻ ·ᶻ wⁿᶻ) ·ᶻ (yⁿᶻ ·ᶻ zⁿᶻ) <ᶻ (yᶻ ·ᶻ zⁿᶻ +ᶻ zᶻ ·ᶻ yⁿᶻ) ·ᶻ (wⁿᶻ ·ᶻ xⁿᶻ) ⇒ᵖ⟨ transport (λ i → [ is-distᶻ (wᶻ ·ᶻ xⁿᶻ) (xᶻ ·ᶻ wⁿᶻ) (yⁿᶻ ·ᶻ zⁿᶻ) .snd i <ᶻ is-distᶻ (yᶻ ·ᶻ zⁿᶻ) (zᶻ ·ᶻ yⁿᶻ) (wⁿᶻ ·ᶻ xⁿᶻ) .snd i ]) ⟩ wᶻ ·ᶻ xⁿᶻ ·ᶻ (yⁿᶻ ·ᶻ zⁿᶻ) +ᶻ xᶻ ·ᶻ wⁿᶻ ·ᶻ (yⁿᶻ ·ᶻ zⁿᶻ) <ᶻ yᶻ ·ᶻ zⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ xⁿᶻ) +ᶻ zᶻ ·ᶻ yⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ xⁿᶻ) ⇒ᵖ⟨ transport (λ i → [ φ₁ i +ᶻ φ₂ i <ᶻ φ₃ i +ᶻ φ₄ i ]) ⟩ wᶻ ·ᶻ yⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ) +ᶻ xᶻ ·ᶻ zⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ) <ᶻ yᶻ ·ᶻ wⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ) +ᶻ zᶻ ·ᶻ xⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ) ◼ᵖ) .snd P₁ = wᶻ ·ᶻ yⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ) <ᶻ yᶻ ·ᶻ wⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ) P₂ = xᶻ ·ᶻ zⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ) <ᶻ zᶻ ·ᶻ xⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ) Q₁ = wᶻ ·ᶻ yⁿᶻ <ᶻ yᶻ ·ᶻ wⁿᶻ Q₂ = xᶻ ·ᶻ zⁿᶻ <ᶻ zᶻ ·ᶻ xⁿᶻ ψ = +ᶻ-<ᶻ-ext (wᶻ ·ᶻ yⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ)) (xᶻ ·ᶻ zⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ)) (yᶻ ·ᶻ wⁿᶻ ·ᶻ (xⁿᶻ ·ᶻ zⁿᶻ)) (zᶻ ·ᶻ xⁿᶻ ·ᶻ (wⁿᶻ ·ᶻ yⁿᶻ)) κ : [ (wᶻ ·ᶻ xⁿᶻ +ᶻ xᶻ ·ᶻ wⁿᶻ) ·ᶻ (yⁿᶻ ·ᶻ zⁿᶻ) <ᶻ (yᶻ ·ᶻ zⁿᶻ +ᶻ zᶻ ·ᶻ yⁿᶻ) ·ᶻ (wⁿᶻ ·ᶻ xⁿᶻ) ] → [ Q₁ ⊔ Q₂ ] κ p = case ψ (φ p) as P₁ ⊔ P₂ ⇒ (Q₁ ⊔ Q₂) of λ { (inl q) → inlᵖ (·ᶻ-reflects-<ᶻ (wᶻ ·ᶻ yⁿᶻ) (yᶻ ·ᶻ wⁿᶻ) (xⁿᶻ ·ᶻ zⁿᶻ) 0<xzⁿᶻ q) ; (inr q) → inrᵖ (·ᶻ-reflects-<ᶻ (xᶻ ·ᶻ zⁿᶻ) (zᶻ ·ᶻ xⁿᶻ) (wⁿᶻ ·ᶻ yⁿᶻ) 0<wyⁿᶻ q) } +-Semigroup : [ isSemigroup _+_ ] +-Semigroup .IsSemigroup.is-set = isSetℚ +-Semigroup .IsSemigroup.is-assoc = +-assoc ·-Semigroup : [ isSemigroup _·_ ] ·-Semigroup .IsSemigroup.is-set = isSetℚ ·-Semigroup .IsSemigroup.is-assoc = ·-assoc +-Monoid : [ isMonoid 0 _+_ ] +-Monoid .IsMonoid.is-Semigroup = +-Semigroup +-Monoid .IsMonoid.is-identity x = +-identityʳ x , +-identityˡ x ·-Monoid : [ isMonoid 1 _·_ ] ·-Monoid .IsMonoid.is-Semigroup = ·-Semigroup ·-Monoid .IsMonoid.is-identity x = ·-identityʳ x , ·-identityˡ x is-Semiring : [ isSemiring 0 1 _+_ _·_ ] is-Semiring .IsSemiring.+-Monoid = +-Monoid is-Semiring .IsSemiring.·-Monoid = ·-Monoid is-Semiring .IsSemiring.+-comm = +-comm is-Semiring .IsSemiring.is-dist x y z = sym (·-distribˡ x y z) , sym (·-distribʳ x y z) is-CommSemiring : [ isCommSemiring 0 1 _+_ _·_ ] is-CommSemiring .IsCommSemiring.is-Semiring = is-Semiring is-CommSemiring .IsCommSemiring.·-comm = ·-comm ≤-Lattice : [ isLattice _≤_ min max ] ≤-Lattice .IsLattice.≤-PartialOrder = linearorder⇒partialorder _ (≤'-isLinearOrder <-StrictLinearOrder) ≤-Lattice .IsLattice.is-min = is-min ≤-Lattice .IsLattice.is-max = is-max is-LinearlyOrderedCommSemiring : [ isLinearlyOrderedCommSemiring 0 1 _+_ _·_ _<_ min max ] is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.is-CommSemiring = is-CommSemiring is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.<-StrictLinearOrder = <-StrictLinearOrder is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.≤-Lattice = ≤-Lattice is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.+-<-ext = +-<-ext is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.·-preserves-< = ·-preserves-< +-inverse : (x : ℚ) → (x + (- x) ≡ 0) × ((- x) + x ≡ 0) +-inverse x .fst = +-inverseʳ x +-inverse x .snd = +-inverseˡ x is-LinearlyOrderedCommRing : [ isLinearlyOrderedCommRing 0 1 _+_ _·_ -_ _<_ min max ] is-LinearlyOrderedCommRing. IsLinearlyOrderedCommRing.is-LinearlyOrderedCommSemiring = is-LinearlyOrderedCommSemiring is-LinearlyOrderedCommRing. IsLinearlyOrderedCommRing.+-inverse = +-inverse is-LinearlyOrderedField : [ isLinearlyOrderedField 0 1 _+_ _·_ -_ _<_ min max ] is-LinearlyOrderedField .IsLinearlyOrderedField.is-LinearlyOrderedCommRing = is-LinearlyOrderedCommRing is-LinearlyOrderedField .IsLinearlyOrderedField.·-inv'' = ·-inv'' ℚbundle : LinearlyOrderedField {ℓ-zero} {ℓ-zero} ℚbundle .LinearlyOrderedField.Carrier = ℚ ℚbundle .LinearlyOrderedField.0f = 0 ℚbundle .LinearlyOrderedField.1f = 1 ℚbundle .LinearlyOrderedField._+_ = _+_ ℚbundle .LinearlyOrderedField.-_ = -_ ℚbundle .LinearlyOrderedField._·_ = _·_ ℚbundle .LinearlyOrderedField.min = min ℚbundle .LinearlyOrderedField.max = max ℚbundle .LinearlyOrderedField._<_ = _<_ ℚbundle .LinearlyOrderedField.is-LinearlyOrderedField = is-LinearlyOrderedField
{-# OPTIONS --cubical #-} module ExerciseSession2 where open import Part1 open import Part2 open import Part3 open import Part4 -- (* Construct a point if isContr A → Π (x y : A) . isContr (x = y) *) isProp→isSet : isProp A → isSet A isProp→isSet h a b p q j i = hcomp (λ k → λ { (i = i0) → h a a k ; (i = i1) → h a b k ; (j = i0) → h a (p i) k ; (j = i1) → h a (q i) k }) a foo : isProp A → isProp' A foo h = λ x y → (h x y) , λ q → isProp→isSet h _ _ (h x y) q -- hProp : {ℓ : Level} → Type (ℓ-suc ℓ) -- hProp {ℓ = ℓ} = Σ[ A ∈ Type ℓ ] isProp A -- foo : isContr (Σ[ A ∈ hProp {ℓ = ℓ} ] (fst A)) -- foo = ({!!} , {!!}) , {!!} -- Σ≡Prop : ((x : A) → isProp (B x)) → {u v : Σ A B} -- → (p : u .fst ≡ v .fst) → u ≡ v
module #5 where open import Relation.Binary.PropositionalEquality open import Function open import Level ind₌ : ∀{a b}{A : Set a} → (C : (x y : A) → (x ≡ y) → Set b) → ((x : A) → C x x refl) → {x y : A} → (p : x ≡ y) → C x y p ind₌ C c {x}{y} p rewrite p = c y transport : ∀ {i} {A : Set i}{P : A → Set i}{x y : A} → (p : x ≡ y) → (P x → P y) transport {i} {A}{P} {x}{y} p = ind₌ D d p where D : (x y : A) → (p : x ≡ y) → Set i D x y p = P x → P y d : (x : A) → D x x refl d = λ x → id record IsEquiv {i j}{A : Set i}{B : Set j}(to : A → B) : Set (i ⊔ j) where field from : B → A iso₁ : (x : A) → from (to x) ≡ x iso₂ : (y : B) → to (from y) ≡ y {- Exercise 2.5. Prove that the functions (2.3.6) and (2.3.7) are inverse equivalences. -} lem2-3-6 : ∀ {i} {A B : Set i}{x y : A} → (p : x ≡ y) → (f : A → B) → (f x ≡ f y) → (transport p (f x) ≡ f y) lem2-3-6 p f q rewrite p = refl lem2-3-7 : ∀ {i} {A B : Set i}{x y : A} → (p : x ≡ y) → (f : A → B) → (transport p (f x) ≡ f y) → (f x ≡ f y) lem2-3-7 p f q rewrite p = refl lems-is-equiv : ∀ {i} {A B : Set i}{x y : A} → (p : x ≡ y) → (f : A → B) → IsEquiv (lem2-3-6 p f) lems-is-equiv p f = record { from = lem2-3-7 p f ; iso₁ = λ x → {!!} ; iso₂ = λ y → {!!} }
module Parity where data ℕ : Set where zero : ℕ suc : ℕ -> ℕ infixl 60 _+_ infixl 70 _*_ _+_ : ℕ -> ℕ -> ℕ n + zero = n n + suc m = suc (n + m) _*_ : ℕ -> ℕ -> ℕ n * zero = zero n * suc m = n * m + n {-# BUILTIN NATURAL ℕ #-} {-# BUILTIN ZERO zero #-} {-# BUILTIN SUC suc #-} {-# BUILTIN NATPLUS _+_ #-} {-# BUILTIN NATTIMES _*_ #-} data Parity : ℕ -> Set where itsEven : (k : ℕ) -> Parity (2 * k) itsOdd : (k : ℕ) -> Parity (2 * k + 1) parity : (n : ℕ) -> Parity n parity zero = itsEven zero parity (suc n) with parity n parity (suc .(2 * k)) | itsEven k = itsOdd k parity (suc .(2 * k + 1)) | itsOdd k = itsEven (k + 1) half : ℕ -> ℕ half n with parity n half .(2 * k) | itsEven k = k half .(2 * k + 1) | itsOdd k = k
{-# OPTIONS --sized-types #-} open import FRP.JS.Array using ( ⟨⟩ ; ⟨_ ; _,_ ; _⟩ ) open import FRP.JS.Bool using ( Bool ; true ; false ; not ) open import FRP.JS.JSON using ( JSON ; float ; bool ; string ; object ; array ; null ; parse ; _≟_ ) open import FRP.JS.Maybe using ( Maybe ; just ; nothing ; _≟[_]_ ) open import FRP.JS.Object using ( ⟪_ ; _↦_⟫ ; _↦_,_ ) open import FRP.JS.QUnit using ( TestSuite ; test ; ok ; ok! ; _,_ ) module FRP.JS.Test.JSON where ⟨1⟩ = array (⟨ float 1.0 ⟩) ⟨n⟩ = array (⟨ null ⟩) ⟨1,n⟩ = array (⟨ float 1.0 , null ⟩) ⟨n,1⟩ = array (⟨ null , float 1.0 ⟩) ⟨⟨1⟩⟩ = array (⟨ ⟨1⟩ ⟩) ⟪a↦1⟫ = object (⟪ "a" ↦ float 1.0 ⟫) ⟪b↦n⟫ = object (⟪ "b" ↦ null ⟫) ⟪a↦1,b↦n⟫ = object (⟪ "a" ↦ float 1.0 , "b" ↦ null ⟫) ⟪a↦⟪a↦1⟫⟫ = object (⟪ "a" ↦ ⟪a↦1⟫ ⟫) ⟪a↦⟨1⟩⟫ = object (⟪ "a" ↦ ⟨1⟩ ⟫) ⟨⟪a↦1⟫⟩ = array (⟨ ⟪a↦1⟫ ⟩) _≟?_ : Maybe JSON → Maybe JSON → Bool j ≟? k = j ≟[ _≟_ ] k tests : TestSuite tests = ( test "≟" ( ok "n ≟ n" (null ≟ null) , ok "a ≟ a" (string "a" ≟ string "a") , ok "t ≟ t" (bool true ≟ bool true) , ok "f ≟ f" (bool false ≟ bool false) , ok "1 ≟ 1" (float 1.0 ≟ float 1.0) , ok "2 ≟ 2" (float 2.0 ≟ float 2.0) , ok "⟨1⟩ ≟ ⟨1⟩" (⟨1⟩ ≟ ⟨1⟩) , ok "⟨n⟩ ≟ ⟨n⟩" (⟨n⟩ ≟ ⟨n⟩) , ok "⟨1,n⟩ ≟ ⟨1,n⟩" (⟨1,n⟩ ≟ ⟨1,n⟩) , ok "⟪a↦1⟫ ≟ ⟪a↦1⟫" (⟪a↦1⟫ ≟ ⟪a↦1⟫) , ok "⟪b↦n⟫ ≟ ⟪b↦n⟫" (⟪b↦n⟫ ≟ ⟪b↦n⟫) , ok "⟪a↦1,b↦n⟫ ≟ ⟪a↦1,b↦n⟫" (⟪a↦1,b↦n⟫ ≟ ⟪a↦1,b↦n⟫) , ok "⟪a↦⟪a↦1⟫⟫ ≟ ⟪a↦⟪a↦1⟫⟫" (⟪a↦⟪a↦1⟫⟫ ≟ ⟪a↦⟪a↦1⟫⟫) , ok "⟪a↦⟨1⟩⟫ ≟ ⟪a↦⟨1⟩⟫" (⟪a↦⟨1⟩⟫ ≟ ⟪a↦⟨1⟩⟫) , ok "⟨⟪a↦1⟫⟩ ≟ ⟨⟪a↦1⟫⟩" (⟨⟪a↦1⟫⟩ ≟ ⟨⟪a↦1⟫⟩) , ok "n ≟ a" (not (null ≟ string "a")) , ok "n ≟ 'n'" (not (null ≟ string "null")) , ok "n ≟ f" (not (null ≟ bool false)) , ok "n ≟ 0" (not (null ≟ float 0.0)) , ok "a ≟ b" (not (string "a" ≟ string "b")) , ok "a ≟ A" (not (string "a" ≟ string "A")) , ok "t ≟ 't'" (not (string "true" ≟ bool true)) , ok "1 ≟ '1'" (not (string "1.0" ≟ float 1.0)) , ok "t ≟ f" (not (bool true ≟ bool false)) , ok "f ≟ 0" (not (bool false ≟ float 0.0)) , ok "1 ≟ 2" (not (float 1.0 ≟ float 2.0)) , ok "⟨1⟩ ≟ ⟨n⟩" (not (⟨1⟩ ≟ ⟨n⟩)) , ok "⟨1⟩ ≟ ⟨1,n⟩" (not (⟨1⟩ ≟ ⟨1,n⟩)) , ok "⟨1,n⟩ ≟ ⟨n,1⟩" (not (⟨1,n⟩ ≟ ⟨n,1⟩)) , ok "⟪a↦1⟫ ≟ ⟪b↦n⟫" (not (⟪a↦1⟫ ≟ ⟪b↦n⟫)) , ok "⟪a↦1⟫ ≟ ⟪a↦1,b↦n⟫" (not (⟪a↦1⟫ ≟ ⟪a↦1,b↦n⟫)) , ok "⟪a↦⟪a↦1⟫⟫ ≟ ⟪a↦1⟫" (not (⟪a↦⟪a↦1⟫⟫ ≟ ⟪a↦1⟫)) , ok "⟪a↦⟨1⟩⟫ ≟ ⟪a↦1⟫" (not (⟪a↦⟨1⟩⟫ ≟ ⟪a↦1⟫)) , ok "⟨⟪a↦1⟫⟩ ≟ ⟪a↦1⟫" (not (⟨⟪a↦1⟫⟩ ≟ ⟪a↦1⟫)) ) , test "parse" ( ok! "parse n" (parse "null" ≟? just null) , ok! "parse a" (parse "\"a\"" ≟? just (string "a")) , ok! "parse a" (parse "true" ≟? just (bool true)) , ok! "parse a" (parse "false" ≟? just (bool false)) , ok! "parse a" (parse "1" ≟? just (float 1.0)) , ok! "parse a" (parse "1.0" ≟? just (float 1.0)) , ok! "parse ⟨1⟩" (parse "[1]" ≟? just ⟨1⟩) , ok! "parse ⟨n⟩" (parse "[null]" ≟? just ⟨n⟩) , ok! "parse ⟨1,n⟩" (parse "[1,null]" ≟? just ⟨1,n⟩) , ok! "parse ⟨n,1⟩" (parse "[null,1]" ≟? just ⟨n,1⟩) , ok! "parse ⟨⟨1⟩⟩" (parse "[[1]]" ≟? just ⟨⟨1⟩⟩) , ok! "parse ⟪a↦1⟫" (parse "{\"a\":1}" ≟? just ⟪a↦1⟫) , ok! "parse ⟪b↦n⟫" (parse "{\"b\":null}" ≟? just ⟪b↦n⟫) , ok! "parse ⟪a↦1,b↦n⟫" (parse "{\"a\":1,\"b\":null}" ≟? just ⟪a↦1,b↦n⟫) , ok! "parse ⟪a↦⟪a↦1⟫⟫" (parse "{\"a\":{\"a\":1}}" ≟? just ⟪a↦⟪a↦1⟫⟫) , ok! "parse ⟪a↦⟨1⟩⟫" (parse "{\"a\":[1]}" ≟? just ⟪a↦⟨1⟩⟫) , ok! "parse ⟨⟪a↦1⟫⟩" (parse "[{\"a\":1}]" ≟? just ⟨⟪a↦1⟫⟩) , ok! "parse ⟨⟪a↦1⟫⟩" (parse "][" ≟? nothing ) ) )
------------------------------------------------------------------------ -- A variant of the lens type in -- Lens.Non-dependent.Higher.Capriotti.Variant.Erased -- -- This variant uses ∥_∥ᴱ instead of ∥_∥. ------------------------------------------------------------------------ import Equality.Path as P module Lens.Non-dependent.Higher.Capriotti.Variant.Erased.Variant {e⁺} (eq : ∀ {a p} → P.Equality-with-paths a p e⁺) where open P.Derived-definitions-and-properties eq open import Logical-equivalence using (_⇔_) open import Prelude open import Bijection equality-with-J as B using (_↔_) open import Equality.Path.Isomorphisms eq open import Equivalence equality-with-J as Eq using (_≃_; Is-equivalence) open import Equivalence.Erased.Cubical eq as EEq using (_≃ᴱ_) open import Equivalence.Erased.Contractible-preimages.Cubical eq as ECP using (_⁻¹ᴱ_; Contractibleᴱ) open import Erased.Cubical eq open import Function-universe equality-with-J as F hiding (id; _∘_) open import H-level equality-with-J open import H-level.Closure equality-with-J open import H-level.Truncation.Propositional eq as PT using (∥_∥; ∣_∣) open import H-level.Truncation.Propositional.Erased eq as T using (∥_∥ᴱ; ∣_∣) open import Preimage equality-with-J using (_⁻¹_) open import Univalence-axiom equality-with-J open import Lens.Non-dependent eq import Lens.Non-dependent.Higher.Erased eq as Higher import Lens.Non-dependent.Higher.Capriotti.Variant eq as V private variable a b p q : Level A B : Type a P Q : A → Type p b₁ b₂ b₃ : A f : (x : A) → P x ------------------------------------------------------------------------ -- Coherently-constant -- Coherently constant type-valued functions. Coherently-constant : {A : Type a} → (A → Type p) → Type (a ⊔ lsuc p) Coherently-constant {p = p} {A = A} P = ∃ λ (Q : ∥ A ∥ᴱ → Type p) → (∀ x → P x ≃ᴱ Q ∣ x ∣) × ∃ λ (Q→Q : ∀ x y → Q x → Q y) → Erased (∀ x y → Q→Q x y ≡ subst Q (T.truncation-is-proposition x y)) -- The "last part" of Coherently-constant is contractible (in erased -- contexts). @0 Contractible-last-part-of-Coherently-constant : Contractible (∃ λ (Q→Q : ∀ x y → Q x → Q y) → Erased (∀ x y → Q→Q x y ≡ subst Q (T.truncation-is-proposition x y))) Contractible-last-part-of-Coherently-constant {Q = Q} = $⟨ Contractibleᴱ-Erased-singleton ⟩ Contractibleᴱ (∃ λ (Q→Q : ∀ x y → Q x → Q y) → Erased (Q→Q ≡ λ x y → subst Q (T.truncation-is-proposition x y))) ↝⟨ (ECP.Contractibleᴱ-cong _ $ ∃-cong λ _ → Erased-cong (inverse $ Eq.extensionality-isomorphism ext F.∘ ∀-cong ext λ _ → Eq.extensionality-isomorphism ext)) ⦂ (_ → _) ⟩ Contractibleᴱ (∃ λ (Q→Q : ∀ x y → Q x → Q y) → Erased (∀ x y → Q→Q x y ≡ subst Q (T.truncation-is-proposition x y))) ↝⟨ ECP.Contractibleᴱ→Contractible ⟩□ Contractible (∃ λ (Q→Q : ∀ x y → Q x → Q y) → Erased (∀ x y → Q→Q x y ≡ subst Q (T.truncation-is-proposition x y))) □ -- In erased contexts Coherently-constant P is equivalent to -- V.Coherently-constant P. @0 Coherently-constant≃Variant-coherently-constant : {P : A → Type p} → Coherently-constant P ≃ V.Coherently-constant P Coherently-constant≃Variant-coherently-constant {A = A} {P = P} = (∃ λ (Q : ∥ A ∥ᴱ → _) → (∀ x → P x ≃ᴱ Q ∣ x ∣) × _) ↔⟨ (∃-cong λ _ → drop-⊤-right λ _ → _⇔_.to contractible⇔↔⊤ Contractible-last-part-of-Coherently-constant) ⟩ (∃ λ (Q : ∥ A ∥ᴱ → _) → ∀ x → P x ≃ᴱ Q ∣ x ∣) ↝⟨ (∃-cong λ _ → ∀-cong ext λ _ → inverse EEq.≃≃≃ᴱ) ⟩ (∃ λ (Q : ∥ A ∥ᴱ → _) → ∀ x → P x ≃ Q ∣ x ∣) ↝⟨ (Σ-cong {k₁ = equivalence} (→-cong₁ ext PT.∥∥ᴱ≃∥∥) λ _ → F.id) ⟩□ (∃ λ (Q : ∥ A ∥ → _) → ∀ x → P x ≃ Q ∣ x ∣) □ -- In erased contexts Coherently-constant (f ⁻¹ᴱ_) is equivalent to -- V.Coherently-constant (f ⁻¹_). @0 Coherently-constant-⁻¹ᴱ≃Variant-coherently-constant-⁻¹ : Coherently-constant (f ⁻¹ᴱ_) ≃ V.Coherently-constant (f ⁻¹_) Coherently-constant-⁻¹ᴱ≃Variant-coherently-constant-⁻¹ {f = f} = Coherently-constant (f ⁻¹ᴱ_) ↝⟨ Coherently-constant≃Variant-coherently-constant ⟩ V.Coherently-constant (f ⁻¹ᴱ_) ↔⟨⟩ (∃ λ Q → ∀ x → f ⁻¹ᴱ x ≃ Q ∣ x ∣) ↝⟨ (∃-cong λ _ → ∀-cong ext λ _ → Eq.≃-preserves ext (inverse ECP.⁻¹≃⁻¹ᴱ) F.id) ⟩ (∃ λ Q → ∀ x → f ⁻¹ x ≃ Q ∣ x ∣) ↔⟨⟩ V.Coherently-constant (f ⁻¹_) □ -- A variant of Coherently-constant. Coherently-constant′ : {A : Type a} → (A → Type p) → Type (a ⊔ lsuc p) Coherently-constant′ {p = p} {A = A} P = ∃ λ (P→P : ∀ x y → P x → P y) → ∃ λ (Q : ∥ A ∥ᴱ → Type p) → ∃ λ (P≃Q : ∀ x → P x ≃ᴱ Q ∣ x ∣) → Erased (P→P ≡ λ x y → _≃ᴱ_.from (P≃Q y) ∘ subst Q (T.truncation-is-proposition ∣ x ∣ ∣ y ∣) ∘ _≃ᴱ_.to (P≃Q x)) -- Coherently-constant and Coherently-constant′ are pointwise -- equivalent (with erased proofs). Coherently-constant≃ᴱCoherently-constant′ : Coherently-constant P ≃ᴱ Coherently-constant′ P Coherently-constant≃ᴱCoherently-constant′ {P = P} = (∃ λ Q → (∀ x → P x ≃ᴱ Q ∣ x ∣) × ∃ λ (Q→Q : ∀ x y → Q x → Q y) → Erased (∀ x y → Q→Q x y ≡ subst Q (T.truncation-is-proposition x y))) ↝⟨ lemma ⟩ (∃ λ Q → ∃ λ (P≃Q : ∀ x → P x ≃ᴱ Q ∣ x ∣) → ∃ λ (P→P : ∀ x y → P x → P y) → Erased (P→P ≡ λ x y → _≃ᴱ_.from (P≃Q y) ∘ subst Q (T.truncation-is-proposition ∣ x ∣ ∣ y ∣) ∘ _≃ᴱ_.to (P≃Q x))) ↔⟨ ∃-comm F.∘ (∃-cong λ _ → ∃-comm) ⟩□ (∃ λ (P→P : ∀ x y → P x → P y) → ∃ λ Q → ∃ λ (P≃Q : ∀ x → P x ≃ᴱ Q ∣ x ∣) → Erased (P→P ≡ λ x y → _≃ᴱ_.from (P≃Q y) ∘ subst Q (T.truncation-is-proposition ∣ x ∣ ∣ y ∣) ∘ _≃ᴱ_.to (P≃Q x))) □ where lemma = ∃-cong λ Q → ∃-cong λ P≃Q → (∃ λ (Q→Q : ∀ x y → Q x → Q y) → Erased (∀ x y → Q→Q x y ≡ subst Q (T.truncation-is-proposition x y))) ↔⟨ (∀-cong ext λ _ → ∃-cong λ _ → Erased-cong (from-equivalence $ Eq.extensionality-isomorphism bad-ext)) F.∘ inverse ΠΣ-comm F.∘ (∃-cong λ _ → Erased-Π↔Π) ⟩ (∀ x → ∃ λ (Q→Q : ∀ y → Q x → Q y) → Erased (Q→Q ≡ λ y → subst Q (T.truncation-is-proposition x y))) ↔⟨ (∀-cong ext λ _ → T.Σ-Π-∥∥ᴱ-Erased-≡-≃) ⟩ (∀ x → ∃ λ (Q→Q : ∀ y → Q x → Q ∣ y ∣) → Erased (Q→Q ≡ λ y → subst Q (T.truncation-is-proposition x ∣ y ∣))) ↔⟨ (∃-cong λ _ → (Erased-cong (from-equivalence $ Eq.extensionality-isomorphism bad-ext)) F.∘ inverse Erased-Π↔Π) F.∘ ΠΣ-comm ⟩ (∃ λ (Q→Q : ∀ x y → Q x → Q ∣ y ∣) → Erased (Q→Q ≡ λ x y → subst Q (T.truncation-is-proposition x ∣ y ∣))) ↔⟨ T.Σ-Π-∥∥ᴱ-Erased-≡-≃ ⟩ (∃ λ (Q→Q : ∀ x y → Q ∣ x ∣ → Q ∣ y ∣) → Erased (Q→Q ≡ λ x y → subst Q (T.truncation-is-proposition ∣ x ∣ ∣ y ∣))) ↔⟨ (inverse $ ΠΣ-comm F.∘ (∀-cong ext λ _ → ΠΣ-comm)) F.∘ (∃-cong λ _ → ((∀-cong ext λ _ → Erased-Π↔Π) F.∘ Erased-Π↔Π) F.∘ Erased-cong (from-equivalence $ inverse $ Eq.extensionality-isomorphism bad-ext F.∘ (∀-cong ext λ _ → Eq.extensionality-isomorphism bad-ext))) ⟩ (∀ x y → ∃ λ (Q→Q : Q ∣ x ∣ → Q ∣ y ∣) → Erased (Q→Q ≡ subst Q (T.truncation-is-proposition ∣ x ∣ ∣ y ∣))) ↝⟨ (∀-cong ext λ _ → ∀-cong ext λ _ → let lemma = inverse $ →-cong ext (P≃Q _) (P≃Q _) in EEq.Σ-cong-≃ᴱ-Erased lemma λ _ → Erased-cong (from-equivalence $ inverse $ Eq.≃-≡ $ EEq.≃ᴱ→≃ lemma)) ⟩ (∀ x y → ∃ λ (P→P : P x → P y) → Erased (P→P ≡ _≃ᴱ_.from (P≃Q y) ∘ subst Q (T.truncation-is-proposition ∣ x ∣ ∣ y ∣) ∘ _≃ᴱ_.to (P≃Q x))) ↔⟨ (∃-cong λ _ → Erased-cong (from-equivalence $ Eq.extensionality-isomorphism bad-ext F.∘ (∀-cong ext λ _ → Eq.extensionality-isomorphism bad-ext))) F.∘ (∃-cong λ _ → inverse $ (∀-cong ext λ _ → Erased-Π↔Π) F.∘ Erased-Π↔Π) F.∘ ΠΣ-comm F.∘ (∀-cong ext λ _ → ΠΣ-comm) ⟩□ (∃ λ (P→P : ∀ x y → P x → P y) → Erased (P→P ≡ λ x y → _≃ᴱ_.from (P≃Q y) ∘ subst Q (T.truncation-is-proposition ∣ x ∣ ∣ y ∣) ∘ _≃ᴱ_.to (P≃Q x))) □ ------------------------------------------------------------------------ -- The lens type family -- Higher lenses with erased "proofs". Lens : Type a → Type b → Type (lsuc (a ⊔ b)) Lens A B = ∃ λ (get : A → B) → Coherently-constant (get ⁻¹ᴱ_) -- In erased contexts Lens A B is equivalent to V.Lens A B. @0 Lens≃Variant-lens : Lens A B ≃ V.Lens A B Lens≃Variant-lens {B = B} = (∃ λ get → Coherently-constant (get ⁻¹ᴱ_)) ↝⟨ (∃-cong λ _ → Coherently-constant-⁻¹ᴱ≃Variant-coherently-constant-⁻¹) ⟩□ (∃ λ get → V.Coherently-constant (get ⁻¹_)) □ -- Some derived definitions. module Lens {A : Type a} {B : Type b} (l : Lens A B) where -- A getter. get : A → B get = proj₁ l -- A predicate. H : Pow a ∥ B ∥ᴱ H = proj₁ (proj₂ l) -- An equivalence (with erased proofs). get⁻¹ᴱ-≃ᴱ : ∀ b → get ⁻¹ᴱ b ≃ᴱ H ∣ b ∣ get⁻¹ᴱ-≃ᴱ = proj₁ (proj₂ (proj₂ l)) -- The predicate H is, in a certain sense, constant. H→H : ∀ b₁ b₂ → H b₁ → H b₂ H→H = proj₁ (proj₂ (proj₂ (proj₂ l))) -- In erased contexts H→H can be expressed using -- T.truncation-is-proposition. @0 H→H≡ : H→H b₁ b₂ ≡ subst H (T.truncation-is-proposition b₁ b₂) H→H≡ = erased (proj₂ (proj₂ (proj₂ (proj₂ l)))) _ _ -- In erased contexts H→H b₁ b₂ is an equivalence. @0 H→H-equivalence : ∀ b₁ b₂ → Is-equivalence (H→H b₁ b₂) H→H-equivalence b₁ b₂ = $⟨ _≃_.is-equivalence $ Eq.subst-as-equivalence _ _ ⟩ Is-equivalence (subst H (T.truncation-is-proposition b₁ b₂)) ↝⟨ subst Is-equivalence $ sym H→H≡ ⟩□ Is-equivalence (H→H b₁ b₂) □ -- One can convert from any "preimage" (with erased proofs) of the -- getter to any other. get⁻¹ᴱ-const : (b₁ b₂ : B) → get ⁻¹ᴱ b₁ → get ⁻¹ᴱ b₂ get⁻¹ᴱ-const b₁ b₂ = get ⁻¹ᴱ b₁ ↝⟨ _≃ᴱ_.to $ get⁻¹ᴱ-≃ᴱ b₁ ⟩ H ∣ b₁ ∣ ↝⟨ H→H ∣ b₁ ∣ ∣ b₂ ∣ ⟩ H ∣ b₂ ∣ ↝⟨ _≃ᴱ_.from $ get⁻¹ᴱ-≃ᴱ b₂ ⟩□ get ⁻¹ᴱ b₂ □ -- The function get⁻¹ᴱ-const b₁ b₂ is an equivalence (in erased -- contexts). @0 get⁻¹ᴱ-const-equivalence : (b₁ b₂ : B) → Is-equivalence (get⁻¹ᴱ-const b₁ b₂) get⁻¹ᴱ-const-equivalence b₁ b₂ = _≃_.is-equivalence ( get ⁻¹ᴱ b₁ ↝⟨ EEq.≃ᴱ→≃ $ get⁻¹ᴱ-≃ᴱ b₁ ⟩ H ∣ b₁ ∣ ↝⟨ Eq.⟨ _ , H→H-equivalence ∣ b₁ ∣ ∣ b₂ ∣ ⟩ ⟩ H ∣ b₂ ∣ ↝⟨ EEq.≃ᴱ→≃ $ inverse $ get⁻¹ᴱ-≃ᴱ b₂ ⟩□ get ⁻¹ᴱ b₂ □) -- Some coherence properties. @0 H→H-∘ : H→H b₂ b₃ ∘ H→H b₁ b₂ ≡ H→H b₁ b₃ H→H-∘ {b₂ = b₂} {b₃ = b₃} {b₁ = b₁} = H→H b₂ b₃ ∘ H→H b₁ b₂ ≡⟨ cong₂ (λ f g → f ∘ g) H→H≡ H→H≡ ⟩ subst H (T.truncation-is-proposition b₂ b₃) ∘ subst H (T.truncation-is-proposition b₁ b₂) ≡⟨ (⟨ext⟩ λ _ → subst-subst _ _ _ _) ⟩ subst H (trans (T.truncation-is-proposition b₁ b₂) (T.truncation-is-proposition b₂ b₃)) ≡⟨ cong (subst H) $ mono₁ 1 T.truncation-is-proposition _ _ ⟩ subst H (T.truncation-is-proposition b₁ b₃) ≡⟨ sym H→H≡ ⟩∎ H→H b₁ b₃ ∎ @0 H→H-id : ∀ {b} → H→H b b ≡ id H→H-id {b = b} = H→H b b ≡⟨ H→H≡ ⟩ subst H (T.truncation-is-proposition b b) ≡⟨ cong (subst H) $ mono₁ 1 T.truncation-is-proposition _ _ ⟩ subst H (refl b) ≡⟨ (⟨ext⟩ λ _ → subst-refl _ _) ⟩∎ id ∎ @0 H→H-inverse : H→H b₁ b₂ ∘ H→H b₂ b₁ ≡ id H→H-inverse {b₁ = b₁} {b₂ = b₂} = H→H b₁ b₂ ∘ H→H b₂ b₁ ≡⟨ H→H-∘ ⟩ H→H b₂ b₂ ≡⟨ H→H-id ⟩∎ id ∎ @0 get⁻¹ᴱ-const-∘ : get⁻¹ᴱ-const b₂ b₃ ∘ get⁻¹ᴱ-const b₁ b₂ ≡ get⁻¹ᴱ-const b₁ b₃ get⁻¹ᴱ-const-∘ {b₂ = b₂} {b₃ = b₃} {b₁ = b₁} = ⟨ext⟩ λ p → _≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ b₃) (H→H _ _ (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b₂) (_≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ b₂) (H→H _ _ (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b₁) p))))) ≡⟨ cong (_≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ _) ∘ H→H _ _) $ _≃ᴱ_.right-inverse-of (get⁻¹ᴱ-≃ᴱ _) _ ⟩ _≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ b₃) (H→H _ _ (H→H _ _ (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b₁) p))) ≡⟨ cong (λ f → _≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ _) (f (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ _) _))) H→H-∘ ⟩∎ _≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ b₃) (H→H _ _ (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b₁) p)) ∎ @0 get⁻¹ᴱ-const-id : ∀ {b} → get⁻¹ᴱ-const b b ≡ id get⁻¹ᴱ-const-id {b = b} = ⟨ext⟩ λ p → _≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ b) (H→H _ _ (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b) p)) ≡⟨ cong (_≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ b)) $ cong (_$ _) H→H-id ⟩ _≃ᴱ_.from (get⁻¹ᴱ-≃ᴱ b) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b) p) ≡⟨ _≃ᴱ_.left-inverse-of (get⁻¹ᴱ-≃ᴱ b) _ ⟩∎ p ∎ @0 get⁻¹ᴱ-const-inverse : get⁻¹ᴱ-const b₁ b₂ ∘ get⁻¹ᴱ-const b₂ b₁ ≡ id get⁻¹ᴱ-const-inverse {b₁ = b₁} {b₂ = b₂} = get⁻¹ᴱ-const b₁ b₂ ∘ get⁻¹ᴱ-const b₂ b₁ ≡⟨ get⁻¹ᴱ-const-∘ ⟩ get⁻¹ᴱ-const b₂ b₂ ≡⟨ get⁻¹ᴱ-const-id ⟩∎ id ∎ -- A setter. set : A → B → A set a b = $⟨ get⁻¹ᴱ-const _ _ ⟩ (get ⁻¹ᴱ get a → get ⁻¹ᴱ b) ↝⟨ _$ (a , [ refl _ ]) ⟩ get ⁻¹ᴱ b ↝⟨ proj₁ ⟩□ A □ -- Lens laws. @0 get-set : ∀ a b → get (set a b) ≡ b get-set a b = get (proj₁ (get⁻¹ᴱ-const (get a) b (a , [ refl _ ]))) ≡⟨ erased (proj₂ (get⁻¹ᴱ-const (get a) b (a , [ refl _ ]))) ⟩∎ b ∎ @0 set-get : ∀ a → set a (get a) ≡ a set-get a = proj₁ (get⁻¹ᴱ-const (get a) (get a) (a , [ refl _ ])) ≡⟨ cong proj₁ $ cong (_$ a , [ refl _ ]) get⁻¹ᴱ-const-id ⟩∎ a ∎ @0 set-set : ∀ a b₁ b₂ → set (set a b₁) b₂ ≡ set a b₂ set-set a b₁ b₂ = proj₁ (get⁻¹ᴱ-const (get (set a b₁)) b₂ (set a b₁ , [ refl _ ])) ≡⟨ elim¹ (λ {b} eq → proj₁ (get⁻¹ᴱ-const (get (set a b₁)) b₂ (set a b₁ , [ refl _ ])) ≡ proj₁ (get⁻¹ᴱ-const b b₂ (set a b₁ , [ eq ]))) (refl _) (get-set a b₁) ⟩ proj₁ (get⁻¹ᴱ-const b₁ b₂ (set a b₁ , [ get-set a b₁ ])) ≡⟨⟩ proj₁ (get⁻¹ᴱ-const b₁ b₂ (get⁻¹ᴱ-const (get a) b₁ (a , [ refl _ ]))) ≡⟨ cong proj₁ $ cong (_$ a , [ refl _ ]) get⁻¹ᴱ-const-∘ ⟩∎ proj₁ (get⁻¹ᴱ-const (get a) b₂ (a , [ refl _ ])) ∎ -- TODO: Can get-set-get and get-set-set be proved for the lens laws -- given above? instance -- The lenses defined above have getters and setters. has-getter-and-setter : Has-getter-and-setter (Lens {a = a} {b = b}) has-getter-and-setter = record { get = Lens.get ; set = Lens.set } ------------------------------------------------------------------------ -- Equality characterisation lemmas -- An equality characterisation lemma. @0 equality-characterisation₁ : {A : Type a} {B : Type b} {l₁ l₂ : Lens A B} → Block "equality-characterisation" → let open Lens in (l₁ ≡ l₂) ≃ (∃ λ (g : get l₁ ≡ get l₂) → ∃ λ (h : H l₁ ≡ H l₂) → ∀ b p → subst (_$ ∣ b ∣) h (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym g) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) equality-characterisation₁ {a = a} {b = b} {A = A} {B = B} {l₁ = l₁} {l₂ = l₂} ⊠ = l₁ ≡ l₂ ↝⟨ inverse $ Eq.≃-≡ $ Eq.↔⇒≃ (Σ-assoc F.∘ ∃-cong λ _ → Σ-assoc) ⟩ ((get l₁ , H l₁ , get⁻¹ᴱ-≃ᴱ l₁) , _) ≡ ((get l₂ , H l₂ , get⁻¹ᴱ-≃ᴱ l₂) , _) ↔⟨ inverse $ ignore-propositional-component $ mono₁ 0 Contractible-last-part-of-Coherently-constant ⟩ (get l₁ , H l₁ , get⁻¹ᴱ-≃ᴱ l₁) ≡ (get l₂ , H l₂ , get⁻¹ᴱ-≃ᴱ l₂) ↝⟨ inverse $ Eq.≃-≡ $ Eq.↔⇒≃ Σ-assoc ⟩ ((get l₁ , H l₁) , get⁻¹ᴱ-≃ᴱ l₁) ≡ ((get l₂ , H l₂) , get⁻¹ᴱ-≃ᴱ l₂) ↔⟨ inverse B.Σ-≡,≡↔≡ ⟩ (∃ λ (eq : (get l₁ , H l₁) ≡ (get l₂ , H l₂)) → subst (λ (g , H) → ∀ b → g ⁻¹ᴱ b ≃ᴱ H ∣ b ∣) eq (get⁻¹ᴱ-≃ᴱ l₁) ≡ get⁻¹ᴱ-≃ᴱ l₂) ↝⟨ (Σ-cong-contra ≡×≡↔≡ λ _ → F.id) ⟩ (∃ λ ((g , h) : get l₁ ≡ get l₂ × H l₁ ≡ H l₂) → subst (λ (g , H) → ∀ b → g ⁻¹ᴱ b ≃ᴱ H ∣ b ∣) (cong₂ _,_ g h) (get⁻¹ᴱ-≃ᴱ l₁) ≡ get⁻¹ᴱ-≃ᴱ l₂) ↔⟨ inverse Σ-assoc ⟩ (∃ λ (g : get l₁ ≡ get l₂) → ∃ λ (h : H l₁ ≡ H l₂) → subst (λ (g , H) → ∀ b → g ⁻¹ᴱ b ≃ᴱ H ∣ b ∣) (cong₂ _,_ g h) (get⁻¹ᴱ-≃ᴱ l₁) ≡ get⁻¹ᴱ-≃ᴱ l₂) ↝⟨ (∃-cong λ _ → ∃-cong λ _ → inverse $ Eq.extensionality-isomorphism ext) ⟩ (∃ λ (g : get l₁ ≡ get l₂) → ∃ λ (h : H l₁ ≡ H l₂) → ∀ b → subst (λ (g , H) → ∀ b → g ⁻¹ᴱ b ≃ᴱ H ∣ b ∣) (cong₂ _,_ g h) (get⁻¹ᴱ-≃ᴱ l₁) b ≡ get⁻¹ᴱ-≃ᴱ l₂ b) ↝⟨ (∃-cong λ _ → ∃-cong λ _ → ∀-cong ext λ _ → ≡⇒↝ _ $ cong (_≡ _) $ sym $ push-subst-application _ _) ⟩ (∃ λ (g : get l₁ ≡ get l₂) → ∃ λ (h : H l₁ ≡ H l₂) → ∀ b → subst (λ (g , H) → g ⁻¹ᴱ b ≃ᴱ H ∣ b ∣) (cong₂ _,_ g h) (get⁻¹ᴱ-≃ᴱ l₁ b) ≡ get⁻¹ᴱ-≃ᴱ l₂ b) ↔⟨ (∃-cong λ _ → ∃-cong λ _ → ∀-cong ext λ _ → inverse $ EEq.to≡to≃≡ ext) ⟩ (∃ λ (g : get l₁ ≡ get l₂) → ∃ λ (h : H l₁ ≡ H l₂) → ∀ b p → _≃ᴱ_.to (subst (λ (g , H) → g ⁻¹ᴱ b ≃ᴱ H ∣ b ∣) (cong₂ _,_ g h) (get⁻¹ᴱ-≃ᴱ l₁ b)) p ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) ↝⟨ (∃-cong λ _ → ∃-cong λ _ → ∀-cong ext λ _ → ∀-cong ext λ _ → ≡⇒↝ _ $ cong (_≡ _) $ lemma _ _ _ _) ⟩□ (∃ λ (g : get l₁ ≡ get l₂) → ∃ λ (h : H l₁ ≡ H l₂) → ∀ b p → subst (_$ ∣ b ∣) h (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym g) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) □ where open Lens lemma : ∀ _ _ _ _ → _ ≡ _ lemma g h b p = _≃ᴱ_.to (subst (λ (g , H) → g ⁻¹ᴱ b ≃ᴱ H ∣ b ∣) (cong₂ _,_ g h) (get⁻¹ᴱ-≃ᴱ l₁ b)) p ≡⟨ cong (_$ p) EEq.to-subst ⟩ subst (λ (g , H) → g ⁻¹ᴱ b → H ∣ b ∣) (cong₂ _,_ g h) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b)) p ≡⟨ subst-→ ⟩ subst (λ (g , H) → H ∣ b ∣) (cong₂ _,_ g h) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (λ (g , H) → g ⁻¹ᴱ b) (sym $ cong₂ _,_ g h) p)) ≡⟨ subst-∘ _ _ _ ⟩ subst (_$ ∣ b ∣) (cong proj₂ $ cong₂ _,_ g h) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (λ (g , H) → g ⁻¹ᴱ b) (sym $ cong₂ _,_ g h) p)) ≡⟨ cong₂ (λ p q → subst (λ (H : Pow a ∥ _ ∥ᴱ) → H ∣ b ∣) p (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) q)) (cong-proj₂-cong₂-, _ _) (subst-∘ _ _ _) ⟩ subst (_$ ∣ b ∣) h (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (cong proj₁ $ sym $ cong₂ _,_ g h) p)) ≡⟨ cong (λ eq → subst (_$ ∣ b ∣) h (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) eq p))) $ trans (cong-sym _ _) $ cong sym $ cong-proj₁-cong₂-, _ _ ⟩∎ subst (_$ ∣ b ∣) h (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym g) p)) ∎ -- Another equality characterisation lemma. @0 equality-characterisation₂ : {l₁ l₂ : Lens A B} → Block "equality-characterisation" → let open Lens in (l₁ ≡ l₂) ≃ (∃ λ (g : ∀ a → get l₁ a ≡ get l₂ a) → ∃ λ (h : ∀ b → H l₁ b ≡ H l₂ b) → ∀ b p → subst id (h ∣ b ∣) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ g)) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) equality-characterisation₂ {l₁ = l₁} {l₂ = l₂} ⊠ = l₁ ≡ l₂ ↝⟨ equality-characterisation₁ ⊠ ⟩ (∃ λ (g : get l₁ ≡ get l₂) → ∃ λ (h : H l₁ ≡ H l₂) → ∀ b p → subst (_$ ∣ b ∣) h (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym g) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) ↝⟨ (Σ-cong-contra (Eq.extensionality-isomorphism bad-ext) λ _ → Σ-cong-contra (Eq.extensionality-isomorphism bad-ext) λ _ → F.id) ⟩ (∃ λ (g : ∀ a → get l₁ a ≡ get l₂ a) → ∃ λ (h : ∀ b → H l₁ b ≡ H l₂ b) → ∀ b p → subst (_$ ∣ b ∣) (⟨ext⟩ h) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ g)) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) ↝⟨ (∃-cong λ _ → ∃-cong λ _ → ∀-cong ext λ _ → ∀-cong ext λ _ → ≡⇒↝ _ $ cong (_≡ _) $ subst-ext _ _) ⟩□ (∃ λ (g : ∀ a → get l₁ a ≡ get l₂ a) → ∃ λ (h : ∀ b → H l₁ b ≡ H l₂ b) → ∀ b p → subst id (h ∣ b ∣) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ g)) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) □ where open Lens -- Yet another equality characterisation lemma. @0 equality-characterisation₃ : {A : Type a} {B : Type b} {l₁ l₂ : Lens A B} → Block "equality-characterisation" → Univalence (a ⊔ b) → let open Lens in (l₁ ≡ l₂) ≃ (∃ λ (g : ∀ a → get l₁ a ≡ get l₂ a) → ∃ λ (h : ∀ b → H l₁ b ≃ H l₂ b) → ∀ b p → _≃_.to (h ∣ b ∣) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ g)) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) equality-characterisation₃ {l₁ = l₁} {l₂ = l₂} ⊠ univ = l₁ ≡ l₂ ↝⟨ equality-characterisation₂ ⊠ ⟩ (∃ λ (g : ∀ a → get l₁ a ≡ get l₂ a) → ∃ λ (h : ∀ b → H l₁ b ≡ H l₂ b) → ∀ b p → subst id (h ∣ b ∣) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ g)) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) ↝⟨ (∃-cong λ _ → Σ-cong-contra (∀-cong ext λ _ → inverse $ ≡≃≃ univ) λ _ → F.id) ⟩ (∃ λ (g : ∀ a → get l₁ a ≡ get l₂ a) → ∃ λ (h : ∀ b → H l₁ b ≃ H l₂ b) → ∀ b p → subst id (≃⇒≡ univ (h ∣ b ∣)) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ g)) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) ↝⟨ (∃-cong λ _ → ∃-cong λ _ → ∀-cong ext λ _ → ∀-cong ext λ _ → ≡⇒↝ _ $ cong (_≡ _) $ trans (subst-id-in-terms-of-≡⇒↝ equivalence) $ cong (λ eq → _≃_.to eq _) $ _≃_.right-inverse-of (≡≃≃ univ) _) ⟩□ (∃ λ (g : ∀ a → get l₁ a ≡ get l₂ a) → ∃ λ (h : ∀ b → H l₁ b ≃ H l₂ b) → ∀ b p → _≃_.to (h ∣ b ∣) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₁ b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ g)) p)) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ l₂ b) p) □ where open Lens ------------------------------------------------------------------------ -- Conversion functions -- The lenses defined above can be converted to and from the lenses -- defined in Higher. Lens⇔Higher-lens : Block "conversion" → Lens A B ⇔ Higher.Lens A B Lens⇔Higher-lens {A = A} {B = B} ⊠ = record { to = λ l → let open Lens l in record { R = Σ ∥ B ∥ᴱ H ; equiv = A ↝⟨ (inverse $ drop-⊤-right λ _ → Erased-other-singleton≃ᴱ⊤) ⟩ (∃ λ (a : A) → ∃ λ (b : B) → Erased (get a ≡ b)) ↔⟨ ∃-comm ⟩ (∃ λ (b : B) → get ⁻¹ᴱ b) ↝⟨ ∃-cong get⁻¹ᴱ-≃ᴱ ⟩ (∃ λ (b : B) → H ∣ b ∣) ↝⟨ EEq.↔→≃ᴱ (λ (b , h) → (∣ b ∣ , b) , h) (λ ((∥b∥ , b) , h) → b , H→H ∥b∥ ∣ b ∣ h) (λ ((∥b∥ , b) , h) → Σ-≡,≡→≡ (cong (_, _) (T.truncation-is-proposition _ _)) ( subst (H ∘ proj₁) (cong (_, _) (T.truncation-is-proposition _ _)) (H→H _ _ h) ≡⟨ trans (subst-∘ _ _ _) $ trans (cong (flip (subst H) _) $ cong-∘ _ _ _) $ cong (flip (subst H) _) $ sym $ cong-id _ ⟩ subst H (T.truncation-is-proposition _ _) (H→H _ _ h) ≡⟨ cong (_$ _) $ sym H→H≡ ⟩ H→H _ _ (H→H _ _ h) ≡⟨ cong (_$ h) H→H-∘ ⟩ H→H _ _ h ≡⟨ cong (_$ h) H→H-id ⟩∎ h ∎)) (λ (b , h) → cong (_ ,_) ( H→H _ _ h ≡⟨ cong (_$ h) H→H-id ⟩∎ h ∎)) ⟩ (∃ λ ((b , _) : ∥ B ∥ᴱ × B) → H b) ↔⟨ Σ-assoc F.∘ (∃-cong λ _ → ×-comm) F.∘ inverse Σ-assoc ⟩□ Σ ∥ B ∥ᴱ H × B □ ; inhabited = _≃_.to PT.∥∥ᴱ≃∥∥ ∘ proj₁ } ; from = λ l → Higher.Lens.get l , (λ _ → Higher.Lens.R l) , (λ b → inverse (Higher.remainder≃ᴱget⁻¹ᴱ l b)) , (λ _ _ → id) , [ (λ _ _ → ⟨ext⟩ λ r → r ≡⟨ sym $ subst-const _ ⟩∎ subst (const (Higher.Lens.R l)) _ r ∎) ] } -- The conversion preserves getters and setters. Lens⇔Higher-lens-preserves-getters-and-setters : (bl : Block "conversion") → Preserves-getters-and-setters-⇔ A B (Lens⇔Higher-lens bl) Lens⇔Higher-lens-preserves-getters-and-setters ⊠ = (λ _ → refl _ , refl _) , (λ _ → refl _ , refl _) -- Lens A B is equivalent to Higher.Lens A B (with erased proofs, -- assuming univalence). Lens≃ᴱHigher-lens : {A : Type a} {B : Type b} → Block "conversion" → @0 Univalence (a ⊔ b) → Lens A B ≃ᴱ Higher.Lens A B Lens≃ᴱHigher-lens {A = A} {B = B} bl univ = EEq.↔→≃ᴱ (_⇔_.to (Lens⇔Higher-lens bl)) (_⇔_.from (Lens⇔Higher-lens bl)) (to∘from bl) (from∘to bl) where @0 to∘from : (bl : Block "conversion") → ∀ l → _⇔_.to (Lens⇔Higher-lens bl) (_⇔_.from (Lens⇔Higher-lens bl) l) ≡ l to∘from ⊠ l = _↔_.from (Higher.equality-characterisation₁ univ) ( (∥ B ∥ᴱ × R ↔⟨ (drop-⊤-left-× λ r → _⇔_.to contractible⇔↔⊤ $ propositional⇒inhabited⇒contractible T.truncation-is-proposition (_≃_.from PT.∥∥ᴱ≃∥∥ (inhabited r))) ⟩□ R □) , (λ _ → refl _) ) where open Higher.Lens l @0 from∘to : (bl : Block "conversion") → ∀ l → _⇔_.from (Lens⇔Higher-lens bl) (_⇔_.to (Lens⇔Higher-lens bl) l) ≡ l from∘to bl@⊠ l = _≃_.from (equality-characterisation₃ ⊠ univ) ( (λ _ → refl _) , Σ∥B∥ᴱH≃H , (λ b p@(a , [ get-a≡b ]) → _≃_.to (Σ∥B∥ᴱH≃H ∣ b ∣) (_≃ᴱ_.from (Higher.remainder≃ᴱget⁻¹ᴱ (_⇔_.to (Lens⇔Higher-lens bl) l) b) (subst (_⁻¹ᴱ b) (sym (⟨ext⟩ λ _ → refl _)) p)) ≡⟨ cong (_≃_.to (Σ∥B∥ᴱH≃H ∣ b ∣) ∘ _≃ᴱ_.from (Higher.remainder≃ᴱget⁻¹ᴱ (_⇔_.to (Lens⇔Higher-lens bl) l) b)) $ trans (cong (flip (subst (_⁻¹ᴱ b)) p) $ trans (cong sym ext-refl) $ sym-refl) $ subst-refl _ _ ⟩ _≃_.to (Σ∥B∥ᴱH≃H ∣ b ∣) (_≃ᴱ_.from (Higher.remainder≃ᴱget⁻¹ᴱ (_⇔_.to (Lens⇔Higher-lens bl) l) b) p) ≡⟨⟩ _≃_.to (Σ∥B∥ᴱH≃H ∣ b ∣) (Higher.Lens.remainder (_⇔_.to (Lens⇔Higher-lens bl) l) a) ≡⟨⟩ subst H _ (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ (get a)) (a , [ refl _ ])) ≡⟨ cong (λ eq → subst H eq (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ (get a)) (a , [ refl _ ]))) $ mono₁ 1 T.truncation-is-proposition _ _ ⟩ subst H (cong ∣_∣ get-a≡b) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ (get a)) (a , [ refl _ ])) ≡⟨ elim¹ (λ {b} eq → subst H (cong ∣_∣ eq) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ (get a)) (a , [ refl _ ])) ≡ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b) (a , [ eq ])) ( subst H (cong ∣_∣ (refl _)) (_≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ (get a)) (a , [ refl _ ])) ≡⟨ trans (cong (flip (subst H) _) $ cong-refl _) $ subst-refl _ _ ⟩ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ (get a)) (a , [ refl _ ]) ∎) _ ⟩∎ _≃ᴱ_.to (get⁻¹ᴱ-≃ᴱ b) p ∎) ) where open Lens l Σ∥B∥ᴱH≃H = λ b → Σ ∥ B ∥ᴱ H ↔⟨ (drop-⊤-left-Σ $ _⇔_.to contractible⇔↔⊤ $ propositional⇒inhabited⇒contractible T.truncation-is-proposition b) ⟩□ H b □ -- The equivalence preserves getters and setters. Lens≃ᴱHigher-lens-preserves-getters-and-setters : {A : Type a} {B : Type b} (bl : Block "conversion") (@0 univ : Univalence (a ⊔ b)) → Preserves-getters-and-setters-⇔ A B (_≃ᴱ_.logical-equivalence (Lens≃ᴱHigher-lens bl univ)) Lens≃ᴱHigher-lens-preserves-getters-and-setters bl univ = Lens⇔Higher-lens-preserves-getters-and-setters bl
------------------------------------------------------------------------ -- The Agda standard library -- -- Greatest Common Divisor for integers ------------------------------------------------------------------------ {-# OPTIONS --without-K --safe #-} module Data.Integer.GCD where open import Data.Integer.Base open import Data.Integer.Divisibility open import Data.Integer.Properties open import Data.Nat.Base import Data.Nat.GCD as ℕ open import Relation.Binary.PropositionalEquality ------------------------------------------------------------------------ -- Definition ------------------------------------------------------------------------ gcd : ℤ → ℤ → ℤ gcd i j = + ℕ.gcd ∣ i ∣ ∣ j ∣ ------------------------------------------------------------------------ -- Properties ------------------------------------------------------------------------ gcd[i,j]∣i : ∀ i j → gcd i j ∣ i gcd[i,j]∣i i j = ℕ.gcd[m,n]∣m ∣ i ∣ ∣ j ∣ gcd[i,j]∣j : ∀ i j → gcd i j ∣ j gcd[i,j]∣j i j = ℕ.gcd[m,n]∣n ∣ i ∣ ∣ j ∣ gcd-greatest : ∀ {i j c} → c ∣ i → c ∣ j → c ∣ gcd i j gcd-greatest c∣i c∣j = ℕ.gcd-greatest c∣i c∣j gcd[0,0]≡0 : gcd 0ℤ 0ℤ ≡ 0ℤ gcd[0,0]≡0 = cong (+_) ℕ.gcd[0,0]≡0 gcd[i,j]≡0⇒i≡0 : ∀ i j → gcd i j ≡ 0ℤ → i ≡ 0ℤ gcd[i,j]≡0⇒i≡0 i j eq = ∣n∣≡0⇒n≡0 (ℕ.gcd[m,n]≡0⇒m≡0 (+-injective eq)) gcd[i,j]≡0⇒j≡0 : ∀ {i j} → gcd i j ≡ 0ℤ → j ≡ 0ℤ gcd[i,j]≡0⇒j≡0 {i} eq = ∣n∣≡0⇒n≡0 (ℕ.gcd[m,n]≡0⇒n≡0 ∣ i ∣ (+-injective eq)) gcd-comm : ∀ i j → gcd i j ≡ gcd j i gcd-comm i j = cong (+_) (ℕ.gcd-comm ∣ i ∣ ∣ j ∣)
{- Part 1: The interval and path types • The interval in Cubical Agda • Path and PathP types • Function extensionality • Equality in Σ-types -} -- To make Agda cubical add the following options {-# OPTIONS --cubical #-} module Part1 where -- To load an Agda file type "C-c C-l" in emacs (the notation "C-c" -- means that one should hold "CTRL" and press "c", for general -- documentation about emacs keybindings see: -- https://www.gnu.org/software/emacs/manual/html_node/efaq/Basic-keys.html) -- The "Core" package of the agda/cubical library sets things for us open import Cubical.Core.Primitives public -- The "Foundations" package of agda/cubical contains a lot of -- important results (in particular the univalence theorem). As we -- will develop many things from scratch we don't import it here, but -- a typical file in the library would import the relevant files from -- Foundations which it uses. To get everything in Foundations write: -- -- open import Cubical.Foundations.Everything -- To search for something among the imported files press C-s C-z and -- then write what you want to search for. -- Documentation of the Cubical Agda mode can be found at: -- https://agda.readthedocs.io/en/v2.6.1.3/language/cubical.html ------------------------------------------------------------------------------ -- Agda Basic -- ------------------------------------------------------------------------------ -- We parametrize everything by some universe levels (as opposed to -- Coq we always have to give these explicitly unless we work with the -- lowest universe) variable ℓ ℓ' ℓ'' : Level -- Universes in Agda are called "Set", but in order to avoid confusion -- with h-sets we rename them to "Type". -- Functions in Agda are written using equations: id : {A : Type ℓ} → A → A id x = x -- The {A : Type} notation says that A is an implicit argument of Type ℓ. -- The notation (x : A) → B and {x : A} → B introduces a dependent -- function (so B might mention B), in other words an element of a -- Π-type. -- We could also write this using a λ-abstraction: id' : {A : Type ℓ} → A → A id' = λ x → x -- To input a nice symbol for the lambda write "\lambda". Agda support -- Unicode symbols natively: -- https://agda.readthedocs.io/en/latest/tools/emacs-mode.html#unicode-input -- To input the "ℓ" write "\ell" -- We can build Agda terms interactively in emacs by writing a ? as RHS: -- -- id'' : {A : Type ℓ} → A → A -- id'' = ? -- -- Try uncommenting this and pressing "C-c C-l". This will give us a -- hole and by entering it with the cursor we can get information -- about what Agda expects us to provide and get help from Agda in -- providing this. -- -- By pressing "C-c C-," while having the cursor in the goal Agda -- shows us the current context and goal. As we're trying to write a -- function we can press "C-c C-r" (for refine) to have Agda write the -- λ-abstraction "λ x → ?" automatically for us. If one presses "C-c C-," -- in the hole again "x : A" will now be in the context. If we type -- "x" in the hole and press "C-c C-." Agda will show us that we have -- an A, which is exactly what we want to provide to fill the goal. By -- pressing "C-c C-SPACE" Agda will then fill the hole with "x" for us. -- -- Agda has lots of handy commands like this for manipulating goals: -- https://agda.readthedocs.io/en/latest/tools/emacs-mode.html#commands-in-context-of-a-goal -- A good resource to get start with Agda is the documentation: -- https://agda.readthedocs.io/en/latest/getting-started/index.html ------------------------------------------------------------------------------ -- The interval and path types -- ------------------------------------------------------------------------------ -- The interval in Cubical Agda is written I and the endpoints are -- -- i0 : I -- i1 : I -- -- These stand for "interval 0" and "interval 1". -- We can apply a function out of the interval to an endpoint just -- like we would apply any Agda function: apply0 : (A : Type ℓ) (p : I → A) → A apply0 A p = p i0 -- The equality type _≡_ is not inductively defined in Cubical Agda, -- instead it's a builtin primitive. An element of x ≡ y consists of a -- function p : I → A such that p i0 is definitionally x and p i1 is -- definitionally y. The check that the endpoints are correct when we -- provide a p : I → A is automatically checked by Agda during -- typechecking, so introducing an element of x ≡ y is done just like -- how we introduce elements of I → A but Agda will check the side -- conditions. -- -- So we can write paths using λ-abstraction: path1 : {A : Type ℓ} (x : A) → x ≡ x path1 x = λ i → x -- -- As explained above Agda checks that whatever we write as definition -- matches the path that we have written (so the endpoints have to be -- correct). In this case everything is fine and path1 can be thought -- of as a proof reflexivity. Let's give it a nicer name and more -- implicit arguments: -- refl : {A : Type ℓ} {x : A} → x ≡ x refl {x = x} = λ i → x -- -- The notation {x = x} lets us access the implicit argument x (the x -- in the LHS) and rename it to x (the x in the RHS) in the body of refl. -- We could just as well have written: -- -- refl : {A : Type ℓ} {x : A} → x ≡ x -- refl {x = y} = λ i → y -- Note that we cannot pattern-match on interval variables as I is not -- inductively defined: -- -- foo : {A : Type} → I → A -- foo r = {!r!} -- Try typing C-c C-c -- It often gets tiring to write {A : Type ℓ} everywhere, so let's -- assume that we have some types: variable A B : Type ℓ -- This will make A and B elements of different universes (all -- arguments is maximally generalized) and all definitions that use -- them will have them as implicit arguments. -- We can now implement some basic operations on _≡_. Let's start with -- cong (called "ap" in the HoTT book): cong : (f : A → B) {x y : A} → x ≡ y → f x ≡ f y cong f p i = f (p i) -- Note that the definition differs from the HoTT definition in that -- it is not defined by J or pattern-matching on p, but rather it's -- just a direct definition as a composition fo functions. Agda treats -- p : x ≡ y as a function, so we can just apply it to i to get an -- element of A which at i0 is x and at i1 is y. By applying f to this -- element we hence get an element of B which at i0 is f x and at i1 -- is f y. -- As this is just function composition it satisfies lots of nice -- definitional equalities, see the Warmup.agda file. Some of these -- are not satisfied by the HoTT definition of cong/ap. -- In HoTT function extensionality is proved as a consequence of -- univalence using a rather ingenious proof due to Voevodsky, but in -- cubical systems it has a much more direct proof. As paths are just -- functions we can get it by just swapping the arguments to p: funExt : {f g : A → B} (p : (x : A) → f x ≡ g x) → f ≡ g funExt p i x = p x i -- To see that this has the correct type not that when i is i0 we have -- "p x i0 = f x" and when i is i1 we have "p x i1 = g x", so by η for -- functions we have a path f ≡ g as desired. -- The interval has additional operations: -- -- Minimum: _∧_ : I → I → I -- Maximum: _∨_ : I → I → I -- Symmetry: ~_ : I → I -- These satisfy the equations of a De Morgan algebra (i.e. a -- distributive lattice (_∧_ , _∨_ , i0 , i1) with an involution -- ~). So we have the following kinds of equations definitionally: -- -- i0 ∨ i = i -- i ∨ i1 = i1 -- i ∨ j = j ∨ i -- i0 ∧ i = i0 -- i1 ∧ i = i -- i ∧ j = j ∧ i -- ~ (~ i) = i -- i0 = ~ i1 -- ~ (i ∨ j) = ~ i ∧ ~ j -- ~ (i ∧ j) = ~ i ∨ ~ j -- These operations are very useful as they let us define even more -- things directly. For example symmetry of paths is easily defined -- using ~: sym : {x y : A} → x ≡ y → y ≡ x sym p i = p (~ i) -- The operations _∧_ and _∨_ are called "connections" and lets us -- build higher dimensional cubes from lower dimensional ones, for -- example if we have a path p : x ≡ y then -- -- sq i j = p (i ∧ j) -- -- is a square (as we've parametrized by i and j) with the following -- boundary: -- -- sq i0 j = p (i0 ∧ j) = p i0 = x -- sq i1 j = p (i1 ∧ j) = p j -- sq i i0 = p (i ∧ i0) = p i0 = x -- sq i i1 = p (i ∧ i1) = p i -- -- So if we draw this we get: -- -- p -- x --------> y -- ^ ^ -- ¦ ¦ -- refl ¦ sq ¦ p -- ¦ ¦ -- ¦ ¦ -- x --------> x -- refl -- -- These operations are very useful, for example let's prove that -- singletons are contractible (aka based path induction). -- -- We first need the notion of contractible types. For this we need -- to use a Σ-type: isContr : Type ℓ → Type ℓ isContr A = Σ[ x ∈ A ] ((y : A) → x ≡ y) -- Σ-types are introduced in the file Agda.Builtin.Sigma as the record -- (modulo some renaming): -- -- record Σ {ℓ ℓ'} (A : Type ℓ) (B : A → Type ℓ') : Type (ℓ-max ℓ ℓ') where -- constructor _,_ -- field -- fst : A -- snd : B fst -- -- So the projections are fst/snd and the constructor is _,_. We -- also define non-dependent product as a special case of Σ-types in -- Cubical.Data.Sigma.Base as: -- -- _×_ : ∀ {ℓ ℓ'} (A : Type ℓ) (B : Type ℓ') → Type (ℓ-max ℓ ℓ') -- A × B = Σ A (λ _ → B) -- -- The notation ∀ {ℓ ℓ'} lets us omit the type of ℓ and ℓ' in the -- definition. -- We define the type of singletons as follows singl : {A : Type ℓ} (a : A) → Type ℓ singl {A = A} a = Σ[ x ∈ A ] a ≡ x -- To show that this type is contractible we need to provide a center -- of contraction and the fact that any element of it is path-equal to -- the center isContrSingl : (x : A) → isContr (singl x) isContrSingl x = ctr , prf where -- The center is just a pair with x and refl ctr : singl x ctr = x , refl -- We then need to prove that ctr is equal to any element s : singl x. -- This is an equality in a Σ-type, so the first component is a path -- and the second is a path over the path we pick as first argument, -- so the second component is a square. In fact, we need a square -- relating refl and pax, so we can use an _∧_ connection. prf : (s : singl x) → ctr ≡ s prf (y , pax) i = (pax i) , λ j → pax (i ∧ j) -- As we saw in the second component of prf we often need squares when -- proving things. In fact, pax (i ∧ j) is a path relating refl to pax -- *over* another path "λ j → x ≡ pax j". This notion of path over a -- path is very useful when working in HoTT as well as cubically. In -- HoTT these are called path-overs and are defined using transport, -- but in Cubical Agda they are a primitive notion called PathP ("Path -- of a Path"). In general PathP A x y has -- -- A : I → Type ℓ -- x : A i0 -- y : A i1 -- -- So PathP lets us natively define heteorgeneous paths, i.e. paths -- where the endpoints lie in different types. This lets us specify -- the type of the second component of prf: prf' : (x : A) (s : singl x) → (x , refl) ≡ s prf' x (y , pax) i = (pax i) , λ j → goal i j where goal : PathP (λ j → x ≡ pax j) refl pax goal i j = pax (i ∧ j) -- Just like _×_ is a special case of Σ-types we have that _≡_ is a -- special case of PathP. In fact, x ≡ y is just short for PathP (λ _ → A) x y: reflP : {x : A} → PathP (λ _ → A) x x reflP {x = x} = λ _ → x -- Having the primitive notion of equality be heterogeneous is an -- easily overlooked, but very important, strength of cubical type -- theory. This allows us to work directly with equality in Σ-types -- without using transport: module _ {A : Type ℓ} {B : A → Type ℓ'} {x y : Σ A B} where ΣPathP : Σ[ p ∈ fst x ≡ fst y ] PathP (λ i → B (p i)) (snd x) (snd y) → x ≡ y ΣPathP eq i = fst eq i , snd eq i PathPΣ : x ≡ y → Σ[ p ∈ fst x ≡ fst y ] PathP (λ i → B (p i)) (snd x) (snd y) PathPΣ eq = (λ i → fst (eq i)) , (λ i → snd (eq i)) -- The fact that these cancel is proved by refl -- If one looks carefully the proof of prf uses ΣPathP inlined, in -- fact this is used all over the place when working cubically and -- simplifies many proofs which in HoTT requires long complicated -- reasoning about transport. isContrΠ : {B : A → Type ℓ'} (h : (x : A) → isContr (B x)) → isContr ((x : A) → B x) isContrΠ h = (λ x → fst (h x)) , (λ f i x → snd (h x) (f x) i) -- Let us end this session with defining propositions and sets isProp : Type ℓ → Type ℓ isProp A = (x y : A) → x ≡ y isSet : Type ℓ → Type ℓ isSet A = (x y : A) → isProp (x ≡ y) -- In the agda/cubical library we call these h-levels following -- Voevodsky instead of n-types and index by natural numbers instead -- of ℕ₋₂. So isContr is h-level 0, isProp is h-level 1, isSet is -- h-level 2, etc. For details see Cubical/Foundations/HLevels.agda
{-# OPTIONS --show-implicit #-} -- {-# OPTIONS -v tc.polarity:10 -v tc.inj:40 -v tc.conv.irr:20 #-} -- -v tc.mod.apply:100 #-} module Issue168 where postulate X : Set open import Issue168b open Membership X postulate P : Nat → Set lemma : ∀ n → P (id n) foo : P zero foo = lemma _
------------------------------------------------------------------------ -- A definitional interpreter ------------------------------------------------------------------------ open import Prelude import Lambda.Syntax module Lambda.Interpreter {Name : Type} (open Lambda.Syntax Name) (def : Name → Tm 1) where import Equality.Propositional as E open import Monad E.equality-with-J open import Vec.Data E.equality-with-J open import Delay-monad open import Delay-monad.Bisimilarity open import Lambda.Delay-crash open Closure Tm ------------------------------------------------------------------------ -- The interpreter infix 10 _∙_ mutual ⟦_⟧ : ∀ {i n} → Tm n → Env n → Delay-crash Value i ⟦ var x ⟧ ρ = return (index ρ x) ⟦ lam t ⟧ ρ = return (lam t ρ) ⟦ t₁ · t₂ ⟧ ρ = do v₁ ← ⟦ t₁ ⟧ ρ v₂ ← ⟦ t₂ ⟧ ρ v₁ ∙ v₂ ⟦ call f t ⟧ ρ = do v ← ⟦ t ⟧ ρ lam (def f) [] ∙ v ⟦ con b ⟧ ρ = return (con b) ⟦ if t₁ t₂ t₃ ⟧ ρ = do v₁ ← ⟦ t₁ ⟧ ρ ⟦if⟧ v₁ t₂ t₃ ρ _∙_ : ∀ {i} → Value → Value → Delay-crash Value i lam t₁ ρ ∙ v₂ = later λ { .force → ⟦ t₁ ⟧ (v₂ ∷ ρ) } con _ ∙ _ = crash ⟦if⟧ : ∀ {i n} → Value → Tm n → Tm n → Env n → Delay-crash Value i ⟦if⟧ (lam _ _) _ _ _ = crash ⟦if⟧ (con true) t₂ t₃ ρ = ⟦ t₂ ⟧ ρ ⟦if⟧ (con false) t₂ t₃ ρ = ⟦ t₃ ⟧ ρ ------------------------------------------------------------------------ -- An example -- The semantics of Ω is the non-terminating computation never. Ω-loops : ∀ {i} → [ i ] ⟦ Ω ⟧ [] ∼ never Ω-loops = ⟦ Ω ⟧ [] ∼⟨⟩ ⟦ ω · ω ⟧ [] ∼⟨⟩ lam ω-body [] ∙ lam ω-body [] ∼⟨ later (λ { .force → ⟦ ω-body ⟧ (lam ω-body [] ∷ []) ∼⟨⟩ lam ω-body [] ∙ lam ω-body [] ∼⟨⟩ ⟦ Ω ⟧ [] ∼⟨ Ω-loops ⟩∼ never ∎ }) ⟩∼ never ∎ -- A more direct proof. Ω-loops′ : ∀ {i} → [ i ] ⟦ Ω ⟧ [] ∼ never Ω-loops′ = later λ { .force → Ω-loops′ }
open import Agda.Builtin.Nat open import Agda.Builtin.Equality -- Should not be accepted f : (@0 n : Nat) (m : Nat) → n + 0 ≡ m → Nat f n m refl = m
{-# OPTIONS --without-K --safe #-} open import Categories.Category open import Categories.Functor module Categories.Adjoint.AFT.SolutionSet {o ℓ e o′ ℓ′ e′} {C : Category o ℓ e} {D : Category o′ ℓ′ e′} (F : Functor C D) where open import Level private module C = Category C module D = Category D module F = Functor F open D open F record SolutionSet i : Set (o ⊔ ℓ ⊔ o′ ⊔ ℓ′ ⊔ e′ ⊔ (suc i)) where field I : Set i S : I → C.Obj S₀ : ∀ {A X} → X ⇒ F₀ A → I S₁ : ∀ {A X} (f : X ⇒ F₀ A) → S (S₀ f) C.⇒ A ϕ : ∀ {A X} (f : X ⇒ F₀ A) → X ⇒ F₀ (S (S₀ f)) commute : ∀ {A X} (f : X ⇒ F₀ A) → F₁ (S₁ f) ∘ ϕ f ≈ f record SolutionSet′ : Set (o ⊔ ℓ ⊔ o′ ⊔ ℓ′ ⊔ e′) where field S₀ : ∀ {A X} → X ⇒ F₀ A → C.Obj S₁ : ∀ {A X} (f : X ⇒ F₀ A) → S₀ f C.⇒ A ϕ : ∀ {A X} (f : X ⇒ F₀ A) → X ⇒ F₀ (S₀ f) commute : ∀ {A X} (f : X ⇒ F₀ A) → F₁ (S₁ f) ∘ ϕ f ≈ f SolutionSet⇒SolutionSet′ : ∀ {i} → SolutionSet i → SolutionSet′ SolutionSet⇒SolutionSet′ s = record { S₀ = λ f → S (S₀ f) ; S₁ = S₁ ; ϕ = ϕ ; commute = commute } where open SolutionSet s SolutionSet′⇒SolutionSet : ∀ i → SolutionSet′ → SolutionSet (o ⊔ i) SolutionSet′⇒SolutionSet i s = record { I = Lift i C.Obj ; S = lower ; S₀ = λ f → lift (S₀ f) ; S₁ = S₁ ; ϕ = ϕ ; commute = commute } where open SolutionSet′ s
postulate Typ : Set ⊢ : Typ → Set N : Typ t : ⊢ N record TopPred : Set where constructor tp field nf : Typ postulate inv-t[σ] : (T : Typ) → ⊢ T → TopPred su-I′ : TopPred → Typ su-I′ krip = let tp _ = inv-t[σ] _ t open TopPred krip in N
{-# OPTIONS --cubical --no-import-sorts --safe #-} module Cubical.Algebra.Semigroup where open import Cubical.Algebra.Semigroup.Base public
module Function.Equals.Multi where open import Data.Tuple.Raise open import Data.Tuple.RaiseTypeᵣ open import Functional open import Function.Multi.Functions open import Function.Multi open import Logic open import Logic.Predicate open import Logic.Predicate.Multi import Lvl import Lvl.MultiFunctions as Lvl open import Numeral.Natural open import Structure.Setoid open import Syntax.Function open import Type private variable ℓ ℓₑ : Lvl.Level private variable n : ℕ private variable ℓ𝓈 : Lvl.Level ^ n module Names where -- Pointwise function equality for a pair of multivariate functions. -- Example: -- (f ⦗ ⊜₊(3) ⦘ g) = (∀{x}{y}{z} → (f(x)(y)(z) ≡ g(x)(y)(z))) ⊜₊ : (n : ℕ) → ∀{ℓ𝓈}{As : Types{n}(ℓ𝓈)}{B : Type{ℓ}} ⦃ equiv-B : Equiv{ℓₑ}(B) ⦄ → (As ⇉ B) → (As ⇉ B) → Stmt{ℓₑ Lvl.⊔ (Lvl.⨆(ℓ𝓈))} ⊜₊(n) = ∀₊(n) ∘₂ pointwise(n)(2) (_≡_) record ⊜₊ (n : ℕ) {ℓ𝓈}{As : Types{n}(ℓ𝓈)}{B : Type{ℓ}} ⦃ equiv-B : Equiv{ℓₑ}(B) ⦄ (f : As ⇉ B) (g : As ⇉ B) : Stmt{ℓₑ Lvl.⊔ (Lvl.⨆(ℓ𝓈))} where constructor intro field proof : f ⦗ Names.⊜₊(n) ⦘ g
{-# OPTIONS --cubical --no-import-sorts --safe #-} module Cubical.Data.Nat.Omniscience where open import Cubical.Foundations.Function open import Cubical.Foundations.HLevels open import Cubical.Foundations.Prelude open import Cubical.Foundations.Univalence open import Cubical.Data.Bool renaming (Bool to 𝟚; Bool→Type to ⟨_⟩) open import Cubical.Data.Nat open import Cubical.Data.Nat.Order.Recursive open import Cubical.Relation.Nullary open import Cubical.Axiom.Omniscience variable ℓ : Level A : Type ℓ F : A → Type ℓ module _ where open Minimal never-least→never : (∀ m → ¬ Least F m) → (∀ m → ¬ F m) never-least→never ¬LF = wf-elim (flip ∘ curry ∘ ¬LF) never→never-least : (∀ m → ¬ F m) → (∀ m → ¬ Least F m) never→never-least ¬F m (Fm , _) = ¬F m Fm ¬least-wlpo : (∀(P : ℕ → 𝟚) → Dec (∀ x → ¬ Least (⟨_⟩ ∘ P) x)) → WLPO ℕ ¬least-wlpo lwlpo P = mapDec never-least→never (_∘ never→never-least) (lwlpo P)
{- Category of categories -} module CategoryTheory.Instances.Cat where open import CategoryTheory.Categories open import CategoryTheory.Functor open import CategoryTheory.NatTrans -- Category of categories ℂat : ∀{n} -> Category (lsuc n) ℂat {n} = record { obj = Category n ; _~>_ = Functor ; id = I ; _∘_ = _◯_ ; _≈_ = _⟺_ ; id-left = id-left-ℂat ; id-right = id-right-ℂat ; ∘-assoc = λ {𝔸} {𝔹} {ℂ} {𝔻} {H} {G} {F} -> ∘-assoc-ℂat {𝔸} {𝔹} {ℂ} {𝔻} {F} {G} {H} ; ≈-equiv = ⟺-equiv ; ≈-cong = ≈-cong-ℂat } where id-left-ℂat : {ℂ 𝔻 : Category n} {F : Functor ℂ 𝔻} -> (I ◯ F) ⟺ F id-left-ℂat {ℂ} {𝔻} {F} = record { to = record { at = λ A -> F.fmap ℂ.id ; nat-cond =  λ {A} {B} {f} -> begin F.fmap f ∘ F.fmap ℂ.id ≈⟨ ≈-cong-right (F.fmap-id) ⟩ F.fmap f ∘ id ≈⟨ id-comm ⟩ id ∘ Functor.fmap (I ◯ F) f ≈⟨ ≈-cong-left (F.fmap-id [sym]) ⟩ F.fmap ℂ.id ∘ Functor.fmap (I ◯ F) f ∎ } ; from = record { at = λ A -> F.fmap ℂ.id ; nat-cond = λ {A} {B} {f} -> begin Functor.fmap (I ◯ F) f ∘ F.fmap ℂ.id ≈⟨ ≈-cong-right (F.fmap-id) ⟩ Functor.fmap (I ◯ F) f ∘ id ≈⟨ id-comm ⟩ id ∘ F.fmap f ≈⟨ ≈-cong-left (F.fmap-id [sym]) ⟩ F.fmap ℂ.id ∘ F.fmap f ∎ } ; iso1 = iso-proof ; iso2 = iso-proof } where open import Relation.Binary using (IsEquivalence) private module F = Functor F private module ℂ = Category ℂ open Category 𝔻 iso-proof : ∀{A : ℂ.obj} -> F.fmap (ℂ.id {A}) ∘ F.fmap ℂ.id ≈ id iso-proof = begin F.fmap ℂ.id ∘ F.fmap ℂ.id ≈⟨ ≈-cong F.fmap-id F.fmap-id ⟩ id ∘ id ≈⟨ id-left ⟩ id ∎ id-right-ℂat : {ℂ 𝔻 : Category n} {F : Functor ℂ 𝔻} -> (F ◯ I) ⟺ F id-right-ℂat {ℂ} {𝔻} {F} = record { to = record { at = λ A -> F.fmap ℂ.id ; nat-cond =  λ {A} {B} {f} -> begin F.fmap f ∘ F.fmap ℂ.id ≈⟨ ≈-cong-right (F.fmap-id) ⟩ F.fmap f ∘ id ≈⟨ id-comm ⟩ id ∘ Functor.fmap (F ◯ I) f ≈⟨ ≈-cong-left (F.fmap-id [sym]) ⟩ F.fmap ℂ.id ∘ Functor.fmap (F ◯ I) f ∎ } ; from = record { at = λ A -> F.fmap ℂ.id ; nat-cond = λ {A} {B} {f} -> begin Functor.fmap (F ◯ I) f ∘ F.fmap ℂ.id ≈⟨ ≈-cong-right (F.fmap-id) ⟩ Functor.fmap (F ◯ I) f ∘ id ≈⟨ id-right ⟩ Functor.fmap (F ◯ I) f ≈⟨ id-left [sym] ⟩ id ∘ F.fmap f ≈⟨ ≈-cong-left (F.fmap-id [sym]) ⟩ F.fmap ℂ.id ∘ F.fmap f ∎ } ; iso1 = iso-proof ; iso2 = iso-proof } where open import Relation.Binary using (IsEquivalence) private module F = Functor F private module ℂ = Category ℂ open Category 𝔻 iso-proof : ∀{A : ℂ.obj} -> F.fmap (ℂ.id {A}) ∘ F.fmap ℂ.id ≈ id iso-proof = begin F.fmap ℂ.id ∘ F.fmap ℂ.id ≈⟨ ≈-cong F.fmap-id F.fmap-id ⟩ id ∘ id ≈⟨ id-left ⟩ id ∎ ∘-assoc-ℂat : {𝔸 𝔹 ℂ 𝔻 : Category n} {F : Functor 𝔸 𝔹} {G : Functor 𝔹 ℂ} {H : Functor ℂ 𝔻} -> (H ◯ G) ◯ F ⟺ H ◯ (G ◯ F) ∘-assoc-ℂat {𝔸} {𝔹} {ℂ} {𝔻} {F} {G} {H} = record { to = record { at = λ A -> Functor.fmap (H ◯ (G ◯ F)) 𝔸.id ; nat-cond = λ {A} {B} {f} -> begin Functor.fmap (H ◯ (G ◯ F)) f ∘ Functor.fmap (H ◯ (G ◯ F)) 𝔸.id ≈⟨ ≈-cong-right (Functor.fmap-id (H ◯ (G ◯ F))) ⟩ Functor.fmap (H ◯ (G ◯ F)) f ∘ id ≈⟨ id-comm ⟩ id ∘ Functor.fmap (H ◯ G ◯ F) f ≈⟨ ≈-cong-left (Functor.fmap-id ((H ◯ G) ◯ F) [sym]) ⟩ Functor.fmap (H ◯ G ◯ F) 𝔸.id ∘ Functor.fmap (H ◯ G ◯ F) f ∎ } ; from = record { at = λ A -> Functor.fmap ((H ◯ G) ◯ F) 𝔸.id ; nat-cond = λ {A} {B} {f} -> begin Functor.fmap (H ◯ (G ◯ F)) f ∘ Functor.fmap ((H ◯ G) ◯ F) 𝔸.id ≈⟨ ≈-cong-right (Functor.fmap-id ((H ◯ G) ◯ F)) ⟩ Functor.fmap (H ◯ (G ◯ F)) f ∘ id ≈⟨ id-comm ⟩ id ∘ Functor.fmap (H ◯ G ◯ F) f ≈⟨ ≈-cong-left (Functor.fmap-id ((H ◯ G) ◯ F) [sym]) ⟩ Functor.fmap (H ◯ G ◯ F) 𝔸.id ∘ Functor.fmap (H ◯ G ◯ F) f ∎ } ; iso1 = iso-proof ; iso2 = iso-proof } where open import Relation.Binary using (IsEquivalence) private module F = Functor F private module G = Functor G private module H = Functor H private module 𝔸 = Category 𝔸 private module 𝔹 = Category 𝔹 private module ℂ = Category ℂ open Category 𝔻 iso-proof : ∀{A : 𝔸.obj} -> Functor.fmap ((H ◯ G) ◯ F) (𝔸.id {A}) ∘ Functor.fmap (H ◯ (G ◯ F)) 𝔸.id ≈ id iso-proof = begin Functor.fmap ((H ◯ G) ◯ F) 𝔸.id ∘ Functor.fmap (H ◯ (G ◯ F)) 𝔸.id ≈⟨ ≈-cong (Functor.fmap-id ((H ◯ G) ◯ F)) (Functor.fmap-id (H ◯ (G ◯ F))) ⟩ id ∘ id ≈⟨ id-left ⟩ id ∎ ≈-cong-ℂat : {𝔸 𝔹 ℂ : Category n} {F F′ : Functor 𝔸 𝔹} {G G′ : Functor 𝔹 ℂ} -> F ⟺ F′ -> G ⟺ G′ -> (G ◯ F) ⟺ (G′ ◯ F′) ≈-cong-ℂat {𝔸}{𝔹}{ℂ}{F}{F′}{G}{G′} F⟺F′ G⟺G′ = record { to = record { at = λ A -> at G⟺G′.to (F′.omap A) ∘ G.fmap (at F⟺F′.to A) ; nat-cond = λ {A} {B} {f} -> begin Functor.fmap (G′ ◯ F′) f ∘ (at G⟺G′.to (F′.omap A) ∘ G.fmap (at F⟺F′.to A)) ≈⟨ ∘-assoc [sym] ≈> ≈-cong-left (nat-cond (G⟺G′.to))⟩ (at G⟺G′.to (F′.omap B) ∘ Functor.fmap (G ◯ F′) f) ∘ G.fmap (at F⟺F′.to A) ≈⟨ ∘-assoc ≈> ≈-cong-right (G.fmap-∘ [sym])⟩ at G⟺G′.to (F′.omap B) ∘ G.fmap (F′.fmap f 𝔹.∘ at F⟺F′.to A) ≈⟨ ≈-cong-right (G.fmap-cong (nat-cond (F⟺F′.to))) ⟩ at G⟺G′.to (F′.omap B) ∘ G.fmap (at F⟺F′.to B 𝔹.∘ F.fmap f) ≈⟨ ≈-cong-right (G.fmap-∘) ⟩ at G⟺G′.to (F′.omap B) ∘ (G.fmap (at F⟺F′.to B) ∘ Functor.fmap (G ◯ F) f) ≈⟨ ∘-assoc [sym] ⟩ (at G⟺G′.to (F′.omap B) ∘ G.fmap (at F⟺F′.to B)) ∘ Functor.fmap (G ◯ F) f ∎ } ; from = record { at = λ A -> at G⟺G′.from (F.omap A) ∘ G′.fmap (at F⟺F′.from A) ; nat-cond = λ {A} {B} {f} -> begin Functor.fmap (G ◯ F) f ∘ (at G⟺G′.from (F.omap A) ∘ G′.fmap (at F⟺F′.from A)) ≈⟨ ∘-assoc [sym] ≈> ≈-cong-left (nat-cond (G⟺G′.from)) ⟩ (at G⟺G′.from (F.omap B) ∘ Functor.fmap (G′ ◯ F) f) ∘ G′.fmap (at F⟺F′.from A) ≈⟨ ∘-assoc ≈> ≈-cong-right (G′.fmap-∘ [sym])⟩ at G⟺G′.from (F.omap B) ∘ G′.fmap (F.fmap f 𝔹.∘ at F⟺F′.from A) ≈⟨ ≈-cong-right (G′.fmap-cong (nat-cond (F⟺F′.from))) ⟩ at G⟺G′.from (F.omap B) ∘ G′.fmap (at F⟺F′.from B 𝔹.∘ F′.fmap f) ≈⟨ ≈-cong-right (G′.fmap-∘) ⟩ at G⟺G′.from (F.omap B) ∘ (G′.fmap (at F⟺F′.from B) ∘ Functor.fmap (G′ ◯ F′) f) ≈⟨ ∘-assoc [sym] ⟩ (at G⟺G′.from (F.omap B) ∘ G′.fmap (at F⟺F′.from B)) ∘ Functor.fmap (G′ ◯ F′) f ∎ } ; iso1 = λ {A} -> begin (at G⟺G′.from (F.omap A) ∘ G′.fmap (at F⟺F′.from A)) ∘ (at G⟺G′.to (F′.omap A) ∘ G.fmap (at F⟺F′.to A)) ≈⟨ ≈-cong-left (nat-cond G⟺G′.from [sym]) ⟩ (G.fmap (at F⟺F′.from A) ∘ at G⟺G′.from ((F′.omap A))) ∘ (at G⟺G′.to (F′.omap A) ∘ G.fmap (at F⟺F′.to A)) ≈⟨ ∘-assoc [sym] ≈> ≈-cong-left (∘-assoc) ⟩ (G.fmap (at F⟺F′.from A) ∘ (at G⟺G′.from ((F′.omap A)) ∘ at G⟺G′.to (F′.omap A))) ∘ G.fmap (at F⟺F′.to A) ≈⟨ ≈-cong-left (≈-cong-right (G⟺G′.iso1)) ⟩ (G.fmap (at F⟺F′.from A) ∘ id) ∘ G.fmap (at F⟺F′.to A) ≈⟨ ≈-cong-left (id-right) ⟩ G.fmap (at F⟺F′.from A) ∘ G.fmap (at F⟺F′.to A) ≈⟨ G.fmap-∘ [sym] ⟩ G.fmap (at F⟺F′.from A 𝔹.∘ at F⟺F′.to A) ≈⟨ G.fmap-cong (F⟺F′.iso1) ⟩ G.fmap 𝔹.id ≈⟨ G.fmap-id ⟩ id ∎ ; iso2 = λ {A} -> begin (at G⟺G′.to (F′.omap A) ∘ G.fmap (at F⟺F′.to A)) ∘ (at G⟺G′.from (F.omap A) ∘ G′.fmap (at F⟺F′.from A)) ≈⟨ ≈-cong-left (nat-cond G⟺G′.to [sym]) ⟩ (G′.fmap (at F⟺F′.to A) ∘ at G⟺G′.to ((F.omap A))) ∘ (at G⟺G′.from (F.omap A) ∘ G′.fmap (at F⟺F′.from A)) ≈⟨ ∘-assoc [sym] ≈> ≈-cong-left (∘-assoc) ⟩ (G′.fmap (at F⟺F′.to A) ∘ (at G⟺G′.to ((F.omap A)) ∘ at G⟺G′.from (F.omap A))) ∘ G′.fmap (at F⟺F′.from A) ≈⟨ ≈-cong-left (≈-cong-right (G⟺G′.iso2)) ⟩ (G′.fmap (at F⟺F′.to A) ∘ id) ∘ G′.fmap (at F⟺F′.from A) ≈⟨ ≈-cong-left (id-right) ⟩ G′.fmap (at F⟺F′.to A) ∘ G′.fmap (at F⟺F′.from A) ≈⟨ G′.fmap-∘ [sym] ⟩ G′.fmap (at F⟺F′.to A 𝔹.∘ at F⟺F′.from A) ≈⟨ G′.fmap-cong (F⟺F′.iso2) ⟩ G′.fmap 𝔹.id ≈⟨ G′.fmap-id ⟩ id ∎ } where private module F = Functor F private module F′ = Functor F′ private module G = Functor G private module G′ = Functor G′ private module 𝔸 = Category 𝔸 private module 𝔹 = Category 𝔹 open Category ℂ open _⟹_ open _⟺_ private module F⟺F′ = _⟺_ F⟺F′ private module G⟺G′ = _⟺_ G⟺G′
module PartiallyAppliedConstructorInIndex where data Nat : Set where zero : Nat suc : Nat -> Nat plus : Nat -> Nat -> Nat data D : (Nat -> Nat) -> Set where c : D suc d : (x : Nat) -> D (plus x) e : D (\ x -> suc x) f : D suc -> Nat f c = zero f e = suc zero
{-# OPTIONS --without-K #-} module Agda.Builtin.Bool where data Bool : Set where false true : Bool {-# BUILTIN BOOL Bool #-} {-# BUILTIN FALSE false #-} {-# BUILTIN TRUE true #-} {-# COMPILE UHC Bool = data __BOOL__ (__FALSE__ | __TRUE__) #-} {-# COMPILE JS Bool = function (x,v) { return ((x)? v["true"]() : v["false"]()); } #-} {-# COMPILE JS false = false #-} {-# COMPILE JS true = true #-}
module _ where q : ? q = Set
module Relator.Equals.Category where import Data.Tuple as Tuple open import Functional as Fn using (_$_) open import Functional.Dependent using () renaming (_∘_ to _∘ᶠ_) open import Logic.Predicate import Lvl open import Relator.Equals open import Relator.Equals.Proofs open import Structure.Categorical.Properties import Structure.Category.Functor as Category open import Structure.Category.Monad open import Structure.Category.NaturalTransformation open import Structure.Category open import Structure.Function open import Structure.Groupoid import Structure.Groupoid.Functor as Groupoid open import Structure.Operator open import Structure.Relator.Properties open import Structure.Relator open import Syntax.Transitivity open import Type.Category.IntensionalFunctionsCategory open import Type.Identity open import Type private variable ℓ : Lvl.Level private variable T A B Obj : Type{ℓ} private variable f g : A → B private variable P : T → Type{ℓ} private variable x y z : T private variable p : Id x y private variable _⟶_ : Obj → Obj → Type{ℓ} -- The inhabitants of a type together with the the inhabitants of the identity type forms a groupoid. -- An object is an inhabitant of a certain type, and a morphism is a proof of identity between two objects. identityTypeGroupoid : Groupoid{Obj = T} (Id) Groupoid._∘_ identityTypeGroupoid = Fn.swap(transitivity(Id)) Groupoid.id identityTypeGroupoid = reflexivity(Id) Groupoid.inv identityTypeGroupoid = symmetry(Id) Morphism.Associativity.proof (Groupoid.associativity identityTypeGroupoid) {x = _} {x = intro} {y = intro} {z = intro} = intro Morphism.Identityₗ.proof (Tuple.left (Groupoid.identity identityTypeGroupoid)) {f = intro} = intro Morphism.Identityᵣ.proof (Tuple.right (Groupoid.identity identityTypeGroupoid)) {f = intro} = intro Polymorphism.Inverterₗ.proof (Tuple.left (Groupoid.inverter identityTypeGroupoid)) {f = intro} = intro Polymorphism.Inverterᵣ.proof (Tuple.right (Groupoid.inverter identityTypeGroupoid)) {f = intro} = intro identityTypeCategory : Category{Obj = T} (Id) identityTypeCategory = Groupoid.category identityTypeGroupoid -- TODO: Generalize and move this to Structure.Categorical.Proofs inverse-of-identity : ∀{grp : Groupoid(_⟶_)}{x} → (Groupoid.inv grp (Groupoid.id grp {x}) ≡ Groupoid.id grp) inverse-of-identity {grp = grp} = inv(id) 🝖[ Id ]-[ Morphism.identityᵣ(_∘_)(id) ]-sym inv(id) ∘ id 🝖[ Id ]-[ Morphism.inverseₗ(_∘_)(id)(id)(inv id) ] id 🝖[ Id ]-end where open Structure.Groupoid.Groupoid(grp) idTransportFunctor : ∀{grp : Groupoid(_⟶_)} → Groupoid.Functor(identityTypeGroupoid)(grp) Fn.id Groupoid.Functor.map (idTransportFunctor{_⟶_ = _⟶_}{grp = grp}) = sub₂(Id)(_⟶_) where instance _ = Groupoid.morphism-reflexivity grp Groupoid.Functor.op-preserving (idTransportFunctor{grp = grp}) {f = intro} {g = intro} = symmetry(Id) (Morphism.Identityₗ.proof (Tuple.left (Groupoid.identity grp))) Groupoid.Functor.inv-preserving (idTransportFunctor{_⟶_ = _⟶_}{grp = grp}) {f = intro} = sub₂(Id)(_⟶_) (symmetry(Id) (reflexivity(Id))) 🝖[ Id ]-[] sub₂(Id)(_⟶_) (reflexivity(Id)) 🝖[ Id ]-[] id 🝖[ Id ]-[ inverse-of-identity{grp = grp} ]-sym inv(id) 🝖[ Id ]-[] inv(sub₂(Id)(_⟶_) (reflexivity(Id))) 🝖-end where instance _ = Groupoid.morphism-reflexivity grp open Structure.Groupoid.Groupoid(grp) Groupoid.Functor.id-preserving idTransportFunctor = intro -- A functor in the identity type groupoid is a function and a proof of it being a function (compatibility with the identity relation, or in other words, respecting the congruence property). -- Note: It does not neccessarily have to be an endofunctor because different objects (types) can be chosen for the respective groupoids. functionFunctor : Groupoid.Functor(identityTypeGroupoid)(identityTypeGroupoid) f Groupoid.Functor.map (functionFunctor {f = f}) = congruence₁(f) Groupoid.Functor.op-preserving functionFunctor {f = intro} {g = intro} = intro Groupoid.Functor.inv-preserving functionFunctor {f = intro} = intro Groupoid.Functor.id-preserving functionFunctor = intro -- A functor to the category of types is a predicate and a proof of it being a relation (having the substitution property). predicateFunctor : Category.Functor(identityTypeCategory)(typeIntensionalFnCategory) P -- TODO: Is it possible to generalize so that the target (now `typeIntensionalFnCategory`) is more general? `idTransportFunctor` seems to be similar. Maybe on the on₂-category to the right? Category.Functor.map (predicateFunctor{P = P}) = substitute₁(P) Category.Functor.op-preserving predicateFunctor {x} {.x} {.x} {intro} {intro} = intro Category.Functor.id-preserving predicateFunctor = intro -- A natural transformation in the identity type groupoid between two functions (functors of the identity type groupoid) is a proof of them being extensionally identical. extensionalFnNaturalTransformation : (p : ∀(x) → (f(x) ≡ g(x))) → NaturalTransformation([∃]-intro f ⦃ Groupoid.Functor.categoryFunctor functionFunctor ⦄) ([∃]-intro g ⦃ Groupoid.Functor.categoryFunctor functionFunctor ⦄) p NaturalTransformation.natural (extensionalFnNaturalTransformation {f = f} {g = g} p) {x} {.x} {intro} = congruence₁(f) intro 🝖 p(x) 🝖-[ Morphism.Identityᵣ.proof (Tuple.right (Category.identity identityTypeCategory)) ] p(x) 🝖-[ Morphism.Identityₗ.proof (Tuple.left (Category.identity identityTypeCategory)) ]-sym p(x) 🝖 congruence₁(g) intro 🝖-end open import Function.Equals open import Function.Proofs open import Structure.Function.Domain open import Structure.Function.Multi open import Structure.Setoid using (Equiv) -- A monad in the identity type groupoid is an identity function and a proof of it being that and it being idempotent. -- The proof that it behaves the same extensionally as an identity function should also preserve congruence. -- TODO: Instead of (∀{x} → (p(f(x))) ≡ congruence₁(f) (p(x))) , use something like ⦃ preserv : Preserving₁(p)(f)(congruence₁(f)) ⦄ , but it does not work at the moment. Maybe something is dependent here? identityFunctionMonad : ∀{T : Type{ℓ}}{f : T → T} → (p : ∀(x) → (x ≡ f(x))) → (∀{x} → (p(f(x))) ≡ congruence₁(f) (p(x))) → Monad(f) ⦃ Groupoid.Functor.categoryFunctor functionFunctor ⦄ ∃.witness (Monad.Η (identityFunctionMonad p _)) = p NaturalTransformation.natural (∃.proof (Monad.Η (identityFunctionMonad {f = f} p _))) {x}{.x}{intro} = Fn.id intro 🝖 p(x) 🝖[ Id ]-[] intro 🝖 p(x) 🝖[ Id ]-[] p(x) 🝖[ Id ]-[ Morphism.Identityₗ.proof (Tuple.left (Category.identity identityTypeCategory)) ]-sym p(x) 🝖 intro 🝖[ Id ]-[] p(x) 🝖 congruence₁(f) intro 🝖-end ∃.witness (Monad.Μ (identityFunctionMonad {f = f} p _)) x = symmetry(Id) (congruence₁(f) (p(x))) NaturalTransformation.natural (∃.proof (Monad.Μ (identityFunctionMonad {f = f} p _))) {x}{.x}{intro} = (congruence₁(f) Fn.∘ congruence₁(f)) intro 🝖 symmetry(Id) (congruence₁(f) (p(x))) 🝖[ Id ]-[] congruence₁(f) (congruence₁(f) intro) 🝖 symmetry(Id) (congruence₁(f) (p(x))) 🝖[ Id ]-[] congruence₁(f) intro 🝖 symmetry(Id) (congruence₁(f) (p(x))) 🝖[ Id ]-[] intro 🝖 symmetry(Id) (congruence₁(f) (p(x))) 🝖[ Id ]-[] symmetry(Id) (congruence₁(f) (p(x))) 🝖[ Id ]-[ Morphism.Identityₗ.proof (Tuple.left (Category.identity identityTypeCategory)) ]-sym symmetry(Id) (congruence₁(f) (p(x))) 🝖 intro 🝖[ Id ]-[] symmetry(Id) (congruence₁(f) (p(x))) 🝖 congruence₁(f) intro 🝖-end _⊜_.proof (Monad.μ-functor-[∘]-commutativity (identityFunctionMonad {f = f} p preserv)) {x} = congruence₂ₗ(_🝖_)(symmetry(Id) (congruence₁(f) (p(x)))) $ congruence₁(f) (symmetry(Id) (congruence₁(f) (p(x)))) 🝖[ Id ]-[ Groupoid.Functor.inv-preserving (functionFunctor{f = f}) {f = congruence₁(f) (p x)} ] symmetry(Id) (congruence₁(f) (congruence₁(f) (p(x)))) 🝖[ Id ]-[ congruence₁(symmetry(Id)) (congruence₁(congruence₁(f)) preserv) ]-sym symmetry(Id) (congruence₁(f) (p(f(x)))) 🝖-end _⊜_.proof (Monad.μ-functor-[∘]-identityₗ (identityFunctionMonad {f = f} p preserv)) {x} = congruence₁(f) (p(x)) 🝖 symmetry(Id) (congruence₁(f) (p(x))) 🝖[ Id ]-[ congruence₂(Groupoid._∘_ identityTypeGroupoid) (congruence₁(symmetry(Id)) preserv) preserv ]-sym p(f(x)) 🝖 symmetry(Id) (p(f(x))) 🝖[ Id ]-[ Morphism.Inverseₗ.proof (Tuple.left(Groupoid.inverse identityTypeGroupoid {f = p(f x)})) ] intro{x = f(x)} 🝖-end _⊜_.proof (Monad.μ-functor-[∘]-identityᵣ (identityFunctionMonad {f = f} p preserv)) {x} = p(f(x)) 🝖 symmetry(Id) (congruence₁(f) (p(x))) 🝖[ Id ]-[ congruence₂ᵣ(_🝖_)(p(f(x))) (congruence₁(symmetry(Id)) preserv) ]-sym p(f(x)) 🝖 symmetry(Id) (p(f(x))) 🝖[ Id ]-[ Morphism.Inverseₗ.proof (Tuple.left(Groupoid.inverse identityTypeGroupoid {f = p(f x)})) ] intro{x = f(x)} 🝖-end
module _ where module M where infixr 3 _!_ data D : Set₁ where _!_ : D → D → D infixl 3 _!_ data E : Set₁ where _!_ : E → E → E open M postulate [_]E : E → Set [_]D : D → Set fail : ∀ {d e} → [ (d ! d) ! d ]D → [ e ! (e ! e) ]E fail x = x -- should use the right fixity for the overloaded constructor
{-# OPTIONS --cubical --no-import-sorts --prop #-} module Instances where open import Cubical.Foundations.Everything renaming (_⁻¹ to _⁻¹ᵖ) open import Agda.Primitive renaming (_⊔_ to ℓ-max; lsuc to ℓ-suc; lzero to ℓ-zero) open import Cubical.Relation.Nullary.Base -- ¬_ open import Cubical.Relation.Binary.Base open import Cubical.Data.Sum.Base renaming (_⊎_ to infixr 4 _⊎_) open import Cubical.Data.Sigma.Base renaming (_×_ to infixr 4 _×_) open import Cubical.Foundations.Logic open import Agda.Builtin.Equality renaming (_≡_ to _≡ᵢ_; refl to reflₚ) variable ℓ ℓ' ℓ'' : Level -- hProp is just the propertie's target type A with ≡ for all inhabitants attached _ : (hProp ℓ) ≡ (Σ[ A ∈ Type ℓ ] (∀(x y : A) → x ≡ y)) _ = refl _ : (hProp ℓ) ≡ (Σ[ A ∈ Type ℓ ] isProp A) _ = refl -- PropRel is just the relation R with ≡ for all inhabitants of all target types R a b attached PropRel' : ∀ {ℓ} (A B : Type ℓ) (ℓ' : Level) → Type (ℓ-max ℓ (ℓ-suc ℓ')) PropRel' A B ℓ' = Σ[ R ∈ Rel A B ℓ' ] ∀ a b → isProp (R a b) #-coerceₚ' : ∀{ℓ'} → {P Q : hProp ℓ'} → [ P ] → {{[ P ] ≡ [ Q ]}} → [ Q ] #-coerceₚ' {ℓ} {[p] , p-isProp} {[q] , q-isProp} p {{ p≡q }} = transport p≡q p path-to-id : ∀{ℓ} {A : Type ℓ} {x y : A} → x ≡ y → x ≡ᵢ y path-to-id p = {!!} -- just for the explanation record PoorField : Type (ℓ-suc (ℓ-max ℓ ℓ')) where field F : Type ℓ 0f 1f : F _#_ : Rel F F ℓ' #-isprop : ∀ x y → isProp (x # y) instance #-isprop' : ∀{x y} {p q : x # y} → p ≡ q -- could we use this in some way? -- #-rel : PropRel F F ℓ' _#ₚ_ : F → F → hProp ℓ' x #ₚ y = (x # y) , #-isprop x y -- NOTE: this creates a `Goal: fst #-rel ...` everywhere -- we might just separate the relation from the isprop -- i.e. directly define _#_ and #-isprop -- _#_ = fst #-rel -- #-isprop = snd #-rel -- NOTE: there is an email on the agda mailing list 08.11.18, 21:16 by Martin Escardo "I want implicit coercions in Agda" -- although this email thread is about more general coercions which are not straight forward, where hProp should be less of an issue #-coerce : ∀{ℓ x y} → ∀{p q} {R : x # y → Type ℓ} → R p → R q #-coerce {ℓ} {x} {y} {p} {q} {R} rp = transport (cong R (#-isprop x y p q))rp #-coerceₚ : ∀{ℓ x y} → ∀{p q} {R : [ x #ₚ y ] → Type ℓ} → R p → R q #-coerceₚ {ℓ} {x} {y} {p} {q} {R} rp = transport (cong R (#-isprop x y p q)) rp field _+_ _·_ : F → F → F _⁻¹ᶠ : (x : F) → {{ x # 0f }} → F ·-rinv : (x : F) → (p : x # 0f) → x · (_⁻¹ᶠ x {{p}}) ≡ 1f -- for the purposes of explanation we just assume two different proofs of `1 # 0` 1#0 : 1f # 0f 1#0' : 1f # 0f -- maybe there is some clever way to define _⁻¹ᶠ in a way where it accepts different proofs _⁻¹ᶠ' : (x : F) → {{ [ x #ₚ 0f ] }} → F ·-rinv' : (x : F) → (p : [ x #ₚ 0f ]) → x · (_⁻¹ᶠ' x {{p}}) ≡ 1f 1#0ₚ : [ 1f #ₚ 0f ] 1#0ₚ' : [ 1f #ₚ 0f ] _⁻¹ᶠ'' : (x : F) → {{ [ x #ₚ 0f ] }} → F -- infix 9 _⁻¹ᶠ -- infixl 7 _·_ -- infixl 5 _+_ -- infixl 4 _#_ module test-hProp (PF : PoorField {ℓ} {ℓ'}) where open PoorField PF -- of course, this works test0 : let instance _ = 1#0 in 1f · (1f ⁻¹ᶠ) ≡ 1f test0 = ·-rinv 1f 1#0 -- now, we try passing in 1#0' test1 : let instance _ = 1#0 in 1f · (1f ⁻¹ᶠ) ≡ 1f -- ERROR: -- PoorField.1#0' /= PoorField.1#0 -- when checking that the expression 1#0' has type fst #-rel 1f 0f test1 = ·-rinv 1f ( #-coerceₚ {_} {1f} {0f} {_} {_} {_} 1#0') -- #-coerce seems to have troubles resolving the R test2 : let instance _ = 1#0 in 1f · (1f ⁻¹ᶠ) ≡ 1f -- ERROR: -- Failed to solve the following constraints: -- _R_161 _q_160 =< (1f · (1f ⁻¹ᶠ)) ≡ 1f -- (1f · (1f ⁻¹ᶠ)) ≡ 1f =< _R_161 _p_159 test2 = #-coerce (·-rinv 1f 1#0') -- this line is yellow in emacs -- it works when we give R explicitly test3 : let instance _ = 1#0 in 1f · (1f ⁻¹ᶠ) ≡ 1f test3 = #-coerce {R = λ r → 1f · (_⁻¹ᶠ 1f {{r}}) ≡ 1f} (·-rinv 1f 1#0') -- a different "result" of this consideration might be that Goals involving hProp-instances need to be formulated in a different way test4 : let instance _ = 1#0ₚ in 1f · (1f ⁻¹ᶠ') ≡ 1f test4 = ·-rinv' 1f ( #-coerceₚ' 1#0ₚ' {{{!!}}} ) test5 : let instance _ = 1#0ₚ in 1f · (1f ⁻¹ᶠ') ≡ 1f test5 with 1#0ₚ' | path-to-id {ℓ'} {x = 1#0ₚ} {y = 1#0ₚ'} (#-isprop 1f 0f 1#0ₚ 1#0ₚ') ... | .(PoorField.1#0ₚ PF) | reflₚ = {! ·-rinv' 1f 1#0ₚ' !} -- when using Prop this would be less of an issue -- but how does it interact with the hProp based cubical library? record ImpredicativePoorField : Type (ℓ-suc (ℓ-max ℓ ℓ')) where field F : Type ℓ 0f 1f : F _#_ : F → F → Prop ℓ' _+_ _·_ : F → F → F _⁻¹ᶠ : (x : F) → {{ x # 0f }} → F ·-rinv : (x : F) → (p : x # 0f) → x · (_⁻¹ᶠ x {{p}}) ≡ 1f -- for the purposes of explanation we just assume two different proofs of `1 # 0` 1#0 : 1f # 0f 1#0' : 1f # 0f module test-impredicative (PF : ImpredicativePoorField {ℓ} {ℓ'}) where open ImpredicativePoorField PF -- now, we try passing in 1#0' test1 : let instance _ = 1#0 in 1f · (1f ⁻¹ᶠ) ≡ 1f test1 = ·-rinv 1f 1#0' -- just works
module Container.List where open import Prelude infixr 5 _∷_ data All {a b} {A : Set a} (P : A → Set b) : List A → Set (a ⊔ b) where [] : All P [] _∷_ : ∀ {x xs} (p : P x) (ps : All P xs) → All P (x ∷ xs) data Any {a b} {A : Set a} (P : A → Set b) : List A → Set (a ⊔ b) where instance zero : ∀ {x xs} (p : P x) → Any P (x ∷ xs) suc : ∀ {x xs} (i : Any P xs) → Any P (x ∷ xs) pattern zero! = zero refl -- Literal overloading for Any module _ {a b} {A : Set a} {P : A → Set b} where private AnyConstraint : List A → Nat → Set (a ⊔ b) AnyConstraint [] _ = ⊥′ AnyConstraint (x ∷ _) zero = ⊤′ {a} × P x -- hack to line up levels AnyConstraint (_ ∷ xs) (suc i) = AnyConstraint xs i natToIx : ∀ (xs : List A) n → {{_ : AnyConstraint xs n}} → Any P xs natToIx [] n {{}} natToIx (x ∷ xs) zero {{_ , px}} = zero px natToIx (x ∷ xs) (suc n) = suc (natToIx xs n) instance NumberAny : ∀ {xs} → Number (Any P xs) Number.Constraint (NumberAny {xs}) = AnyConstraint xs fromNat {{NumberAny {xs}}} = natToIx xs infix 3 _∈_ _∈_ : ∀ {a} {A : Set a} → A → List A → Set a x ∈ xs = Any (_≡_ x) xs forgetAny : ∀ {a p} {A : Set a} {P : A → Set p} {xs : List A} → Any P xs → Nat forgetAny (zero _) = zero forgetAny (suc i) = suc (forgetAny i) lookupAny : ∀ {a b} {A : Set a} {P Q : A → Set b} {xs} → All P xs → Any Q xs → Σ A (λ x → P x × Q x) lookupAny (p ∷ ps) (zero q) = _ , p , q lookupAny (p ∷ ps) (suc i) = lookupAny ps i lookup∈ : ∀ {a b} {A : Set a} {P : A → Set b} {xs x} → All P xs → x ∈ xs → P x lookup∈ (p ∷ ps) (zero refl) = p lookup∈ (p ∷ ps) (suc i) = lookup∈ ps i module _ {a b} {A : Set a} {P Q : A → Set b} (f : ∀ {x} → P x → Q x) where mapAll : ∀ {xs} → All P xs → All Q xs mapAll [] = [] mapAll (x ∷ xs) = f x ∷ mapAll xs mapAny : ∀ {xs} → Any P xs → Any Q xs mapAny (zero x) = zero (f x) mapAny (suc i) = suc (mapAny i) traverseAll : ∀ {a b} {A : Set a} {B : A → Set a} {F : Set a → Set b} {{AppF : Applicative F}} → (∀ x → F (B x)) → (xs : List A) → F (All B xs) traverseAll f [] = pure [] traverseAll f (x ∷ xs) = ⦇ f x ∷ traverseAll f xs ⦈ module _ {a b} {A : Set a} {P : A → Set b} where -- Append infixr 5 _++All_ _++All_ : ∀ {xs ys} → All P xs → All P ys → All P (xs ++ ys) [] ++All qs = qs (p ∷ ps) ++All qs = p ∷ ps ++All qs -- Delete deleteIx : ∀ xs → Any P xs → List A deleteIx (_ ∷ xs) (zero _) = xs deleteIx (x ∷ xs) (suc i) = x ∷ deleteIx xs i deleteAllIx : ∀ {c} {Q : A → Set c} {xs} → All Q xs → (i : Any P xs) → All Q (deleteIx xs i) deleteAllIx (q ∷ qs) (zero _) = qs deleteAllIx (q ∷ qs) (suc i) = q ∷ deleteAllIx qs i -- Equality -- module _ {a b} {A : Set a} {P : A → Set b} {{EqP : ∀ {x} → Eq (P x)}} where private z : ∀ {x xs} → P x → Any P (x ∷ xs) z = zero zero-inj : ∀ {x} {xs : List A} {p q : P x} → Any.zero {xs = xs} p ≡ z q → p ≡ q zero-inj refl = refl sucAny-inj : ∀ {x xs} {i j : Any P xs} → Any.suc {x = x} i ≡ Any.suc {x = x} j → i ≡ j sucAny-inj refl = refl cons-inj₁ : ∀ {x xs} {p q : P x} {ps qs : All P xs} → p All.∷ ps ≡ q ∷ qs → p ≡ q cons-inj₁ refl = refl cons-inj₂ : ∀ {x xs} {p q : P x} {ps qs : All P xs} → p All.∷ ps ≡ q ∷ qs → ps ≡ qs cons-inj₂ refl = refl instance EqAny : ∀ {xs} → Eq (Any P xs) _==_ {{EqAny}} (zero p) (zero q) = decEq₁ zero-inj (p == q) _==_ {{EqAny}} (suc i) (suc j) = decEq₁ sucAny-inj (i == j) _==_ {{EqAny}} (zero _) (suc _) = no λ () _==_ {{EqAny}} (suc _) (zero _) = no λ () EqAll : ∀ {xs} → Eq (All P xs) _==_ {{EqAll}} [] [] = yes refl _==_ {{EqAll}} (x ∷ xs) (y ∷ ys) = decEq₂ cons-inj₁ cons-inj₂ (x == y) (xs == ys)
-- 2013-02-21 Andreas -- ensure that constructor-headedness works also for abstract things module Issue796 where data U : Set where a b : U data A : Set where data B : Set where abstract A' B' : Set A' = A B' = B -- fails if changed to A. [_] : U → Set [_] a = A' [_] b = B' f : ∀ u → [ u ] → U f u _ = u postulate x : A' zzz = f _ x -- succeeds since [_] is constructor headed
module Helper.CodeGeneration where open import Agda.Primitive open import Data.Nat open import Data.Fin open import Data.List open import Function using (_∘_ ; _$_ ; _∋_) open import Reflection a : {A : Set} -> (x : A) -> Arg A a x = arg (arg-info visible relevant) x a1 : {A : Set} -> (x : A) -> Arg A a1 x = arg (arg-info hidden relevant) x t0 : Term -> Type t0 t = el (lit 0) t fin_term : (n : ℕ) -> Term fin_term zero = con (quote Fin.zero) [] fin_term (suc fin) = con (quote Fin.suc) [ a (fin_term fin) ] fin_pat : (n : ℕ) -> (l : Pattern) -> Pattern fin_pat zero l = l fin_pat (suc n) l = con (quote Fin.suc) [ a (fin_pat n l) ] fin_type : (n : ℕ) -> Type fin_type n = t0 (def (quote Fin) [ a (lit (nat n)) ]) -- Annotate terms with a type _∋-t_ : Term -> Term -> Term _∋-t_ ty term = def (quote _∋_) (a ty ∷ a term ∷ []) cons : (n : Name) -> List Name cons n with (definition n) cons n | data-type d = reverse (constructors d) cons n | _ = [] infer_type = el (lit 0) unknown
import Lvl open import Structure.Operator.Vector open import Structure.Setoid open import Type module Structure.Operator.Vector.Subspace.Proofs {ℓᵥ ℓₛ ℓᵥₑ ℓₛₑ} {V : Type{ℓᵥ}} ⦃ equiv-V : Equiv{ℓᵥₑ}(V) ⦄ {S : Type{ℓₛ}} ⦃ equiv-S : Equiv{ℓₛₑ}(S) ⦄ {_+ᵥ_ : V → V → V} {_⋅ₛᵥ_ : S → V → V} {_+ₛ_ _⋅ₛ_ : S → S → S} ⦃ vectorSpace : VectorSpace(_+ᵥ_)(_⋅ₛᵥ_)(_+ₛ_)(_⋅ₛ_) ⦄ where open VectorSpace(vectorSpace) open import Logic.Predicate open import Logic.Propositional open import Numeral.CoordinateVector as Vec using () renaming (Vector to Vec) open import Numeral.Finite open import Numeral.Natural open import Sets.ExtensionalPredicateSet as PredSet using (PredSet ; _∈_ ; _∪_ ; ⋂ᵢ ; _⊇_ ; _⊆_) open import Structure.Container.SetLike using (SetElement) private open module SetLikeFunctionProperties{ℓ} = Structure.Container.SetLike.FunctionProperties{C = PredSet{ℓ}(V)}{E = V}(_∈_) open import Structure.Function.Multi open import Structure.Operator open import Structure.Operator.Vector.LinearCombination ⦃ vectorSpace = vectorSpace ⦄ open import Structure.Operator.Vector.LinearCombination.Proofs open import Structure.Operator.Vector.Subspace ⦃ vectorSpace = vectorSpace ⦄ open import Structure.Relator.Properties open import Syntax.Function open import Syntax.Transitivity private variable ℓ ℓ₁ ℓ₂ : Lvl.Level private variable I : Type{ℓ} private variable n : ℕ private variable vf : Vec(n)(V) private variable sf : Vec(n)(S) -- TODO [⋂ᵢ]-subspace : ∀{Vsf : I → PredSet{ℓ}(V)} → (∀{i} → Subspace(Vsf(i))) → Subspace(⋂ᵢ Vsf) [∪]-subspace : ∀{Vₛ₁ Vₛ₂} → Subspace{ℓ₁}(Vₛ₁) → Subspace{ℓ₂}(Vₛ₂) → (((Vₛ₁ ⊇ Vₛ₂) ∨ (Vₛ₁ ⊆ Vₛ₂)) ↔ Subspace(Vₛ₁ ∪ Vₛ₂))
module _ where module Inner where private variable A : Set open Inner fail : A → A fail x = x
{-# OPTIONS --cubical --safe #-} open import Prelude open import Categories module Categories.Coequalizer {ℓ₁ ℓ₂} (C : Category ℓ₁ ℓ₂) where open Category C private variable h i : X ⟶ Y record Coequalizer (f g : X ⟶ Y) : Type (ℓ₁ ℓ⊔ ℓ₂) where field {obj} : Ob arr : Y ⟶ obj equality : arr · f ≡ arr · g coequalize : ∀ {H} {h : Y ⟶ H} → h · f ≡ h · g → obj ⟶ H universal : ∀ {H} {h : Y ⟶ H} {eq : h · f ≡ h · g} → h ≡ coequalize eq · arr unique : ∀ {H} {h : Y ⟶ H} {i : obj ⟶ H} {eq : h · f ≡ h · g} → h ≡ i · arr → i ≡ coequalize eq
------------------------------------------------------------------------ -- The Agda standard library -- -- The universe polymorphic unit type and ordering relation ------------------------------------------------------------------------ {-# OPTIONS --without-K --safe #-} module Data.Unit.Polymorphic.Base where open import Level ------------------------------------------------------------------------ -- A unit type defined as a record type record ⊤ {ℓ : Level} : Set ℓ where constructor tt
{-# OPTIONS --safe --warning=error --without-K --guardedness #-} open import Functions.Definition open import Functions.Lemmas open import LogicalFormulae open import Numbers.Naturals.Definition open import Numbers.Naturals.Order open import Sets.FinSet.Definition open import Sets.FinSet.Lemmas open import Sets.Cardinality.Infinite.Definition open import Sets.Cardinality.Finite.Lemmas open import Numbers.Reals.Definition open import Numbers.Rationals.Definition open import Numbers.Integers.Definition open import Sets.Cardinality.Infinite.Lemmas open import Setoids.Setoids module Sets.Cardinality.Infinite.Examples where ℕIsInfinite : InfiniteSet ℕ ℕIsInfinite n f bij = pigeonhole (le 0 refl) badInj where inv : ℕ → FinSet n inv = Invertible.inverse (bijectionImpliesInvertible bij) invInj : Injection inv invInj = Bijection.inj (invertibleImpliesBijection (inverseIsInvertible (bijectionImpliesInvertible bij))) bumpUp : FinSet n → FinSet (succ n) bumpUp = intoSmaller (le 0 refl) bumpUpInj : Injection bumpUp bumpUpInj = intoSmallerInj (le 0 refl) nextInj : Injection (toNat {succ n}) nextInj = finsetInjectIntoℕ {succ n} bad : FinSet (succ n) → FinSet n bad a = (inv (toNat a)) badInj : Injection bad badInj = injComp nextInj invInj ℝIsInfinite : DedekindInfiniteSet ℝ DedekindInfiniteSet.inj ℝIsInfinite n = injectionR (injectionQ (nonneg n)) DedekindInfiniteSet.isInjection ℝIsInfinite {x} {y} pr = nonnegInjective (injectionQInjective (injectionRInjective pr))
module DeBruijn where open import Prelude -- using (_∘_) -- composition, identity open import Data.Maybe open import Logic.Identity renaming (subst to subst≡) import Logic.ChainReasoning module Chain = Logic.ChainReasoning.Poly.Homogenous _≡_ (\x -> refl) (\x y z -> trans) open Chain -- untyped de Bruijn terms data Lam (A : Set) : Set where var : A -> Lam A app : Lam A -> Lam A -> Lam A abs : Lam (Maybe A) -> Lam A -- functoriality of Lam lam : {A B : Set} -> (A -> B) -> Lam A -> Lam B lam f (var a) = var (f a) lam f (app t1 t2) = app (lam f t1) (lam f t2) lam f (abs r) = abs (lam (fmap f) r) -- lifting a substitution A -> Lam B under a binder lift : {A B : Set} -> (A -> Lam B) -> Maybe A -> Lam (Maybe B) lift f nothing = var nothing lift f (just a) = lam just (f a) -- extensionality of lifting liftExt : {A B : Set}(f g : A -> Lam B) -> ((a : A) -> f a ≡ g a) -> (t : Maybe A) -> lift f t ≡ lift g t liftExt f g H nothing = refl liftExt f g H (just a) = cong (lam just) $ H a -- simultaneous substitution subst : {A B : Set} -> (A -> Lam B) -> Lam A -> Lam B subst f (var a) = f a subst f (app t1 t2) = app (subst f t1) (subst f t2) subst f (abs r) = abs (subst (lift f) r) -- extensionality of subst substExt : {A B : Set}(f g : A -> Lam B) -> ((a : A) -> f a ≡ g a) -> (t : Lam A) -> subst f t ≡ subst g t substExt f g H (var a) = H a substExt f g H (app t1 t2) = chain> subst f (app t1 t2) === app (subst f t1) (subst f t2) by refl === app (subst g t1) (subst f t2) by cong (\ x -> app x (subst f t2)) (substExt f g H t1) === app (subst g t1) (subst g t2) by cong (\ x -> app (subst g t1) x) (substExt f g H t2) substExt f g H (abs r) = chain> subst f (abs r) === abs (subst (lift f) r) by refl === abs (subst (lift g) r) by cong abs (substExt (lift f) (lift g) (liftExt f g H) r) === subst g (abs r) by refl -- Lemma: lift g ∘ fmap f = lift (g ∘ f) liftLaw1 : {A B C : Set}(f : A -> B)(g : B -> Lam C)(t : Maybe A) -> lift g (fmap f t) ≡ lift (g ∘ f) t liftLaw1 f g nothing = chain> lift g (fmap f nothing) === lift g nothing by refl === var nothing by refl === lift (g ∘ f) nothing by refl liftLaw1 f g (just a) = chain> lift g (fmap f (just a)) === lift g (just (f a)) by refl === lam just (g (f a)) by refl === lift (g ∘ f) (just a) by refl -- Lemma: subst g (lam f t) = subst (g ∘ f) t substLaw1 : {A B C : Set}(f : A -> B)(g : B -> Lam C)(t : Lam A) -> subst g (lam f t) ≡ subst (g ∘ f) t substLaw1 f g (var a) = refl substLaw1 f g (app t1 t2) = chain> subst g (lam f (app t1 t2)) === subst g (app (lam f t1) (lam f t2)) by refl === app (subst g (lam f t1)) (subst g (lam f t2)) by refl === app (subst (g ∘ f) t1) (subst g (lam f t2)) by cong (\ x -> app x (subst g (lam f t2))) (substLaw1 f g t1) === app (subst (g ∘ f) t1) (subst (g ∘ f) t2) by cong (\ x -> app (subst (g ∘ f) t1) x) (substLaw1 f g t2) substLaw1 f g (abs r) = chain> subst g (lam f (abs r)) === subst g (abs (lam (fmap f) r)) by refl === abs (subst (lift g) (lam (fmap f) r)) by refl === abs (subst (lift g ∘ fmap f) r) by cong abs (substLaw1 (fmap f) (lift g) r) === abs (subst (lift (g ∘ f)) r) by cong abs (substExt (lift g ∘ fmap f) (lift (g ∘ f)) (liftLaw1 f g) r) -- Lemma: subst (lam f ∘ g) = lam f ∘ subst g substLaw2 : {A B C : Set}(f : B -> C)(g : A -> Lam B)(t : Lam A) -> subst (lam f ∘ g) t ≡ lam f (subst g t) substLaw2 f g (var a) = refl
------------------------------------------------------------------------ -- The Agda standard library -- -- Some examples showing where the natural numbers and some related -- operations and properties are defined, and how they can be used ------------------------------------------------------------------------ module README.Nat where -- The natural numbers and various arithmetic operations are defined -- in Data.Nat. open import Data.Nat ex₁ : ℕ ex₁ = 1 + 3 -- Propositional equality and some related properties can be found -- in Relation.Binary.PropositionalEquality. open import Relation.Binary.PropositionalEquality ex₂ : 3 + 5 ≡ 2 * 4 ex₂ = refl -- Data.Nat.Properties contains a number of properties about natural -- numbers. Algebra defines what a commutative semiring is, among -- other things. open import Algebra import Data.Nat.Properties as Nat private module CS = CommutativeSemiring Nat.commutativeSemiring ex₃ : ∀ m n → m * n ≡ n * m ex₃ m n = CS.*-comm m n -- The module ≡-Reasoning in Relation.Binary.PropositionalEquality -- provides some combinators for equational reasoning. open ≡-Reasoning open import Data.Product ex₄ : ∀ m n → m * (n + 0) ≡ n * m ex₄ m n = begin m * (n + 0) ≡⟨ cong (_*_ m) (proj₂ CS.+-identity n) ⟩ m * n ≡⟨ CS.*-comm m n ⟩ n * m ∎ -- The module SemiringSolver in Data.Nat.Properties contains a solver -- for natural number equalities involving variables, constants, _+_ -- and _*_. open Nat.SemiringSolver ex₅ : ∀ m n → m * (n + 0) ≡ n * m ex₅ = solve 2 (λ m n → m :* (n :+ con 0) := n :* m) refl
{-# OPTIONS --cubical --safe #-} module Data.Nat.Base where open import Agda.Builtin.Nat public using (_+_; _*_; zero; suc) renaming (Nat to ℕ; _-_ to _∸_) import Agda.Builtin.Nat as Nat open import Level open import Data.Bool data Ordering : ℕ → ℕ → Type₀ where less : ∀ m k → Ordering m (suc (m + k)) equal : ∀ m → Ordering m m greater : ∀ m k → Ordering (suc (m + k)) m compare : ∀ m n → Ordering m n compare zero zero = equal zero compare (suc m) zero = greater zero m compare zero (suc n) = less zero n compare (suc m) (suc n) with compare m n ... | less m k = less (suc m) k ... | equal m = equal (suc m) ... | greater n k = greater (suc n) k nonZero : ℕ → Bool nonZero (suc _) = true nonZero zero = false _÷_ : (n m : ℕ) → { m≢0 : T (nonZero m) } → ℕ _÷_ n (suc m) = Nat.div-helper 0 m n m rem : (n m : ℕ) → { m≢0 : T (nonZero m) } → ℕ rem n (suc m) = Nat.mod-helper 0 m n m
{-# OPTIONS --warning=error --safe --without-K #-} open import LogicalFormulae open import Agda.Primitive using (Level; lzero; lsuc; _⊔_) open import Functions.Definition open import Setoids.Setoids open import Setoids.Subset open import Graphs.Definition open import Sets.FinSet.Definition open import Numbers.Naturals.Semiring open import Sets.EquivalenceRelations module Graphs.CompleteGraph where CompleteGraph : {a b : _} {V' : Set a} (V : Setoid {a} {b} V') → Graph b V Graph._<->_ (CompleteGraph V) x y = ((Setoid._∼_ V x y) → False) Graph.noSelfRelation (CompleteGraph V) x x!=x = x!=x (Equivalence.reflexive (Setoid.eq V)) Graph.symmetric (CompleteGraph V) x!=y y=x = x!=y ((Equivalence.symmetric (Setoid.eq V)) y=x) Graph.wellDefined (CompleteGraph V) {x} {y} {r} {s} x=y r=s x!=r y=s = x!=r (transitive x=y (transitive y=s (symmetric r=s))) where open Setoid V open Equivalence eq Kn : (n : ℕ) → Graph _ (reflSetoid (FinSet n)) Kn n = CompleteGraph (reflSetoid (FinSet n)) triangle : Graph _ (reflSetoid (FinSet 3)) triangle = Kn 3
module Nat1 where data ℕ : Set where zero : ℕ succ : ℕ → ℕ _+_ : ℕ → ℕ → ℕ zero + b = b succ a + b = succ (a + b) open import Equality one = succ zero two = succ one three = succ two 0-is-id : ∀ (n : ℕ) → (n + zero) ≡ n 0-is-id zero = begin (zero + zero) ≈ zero by definition ∎ 0-is-id (succ y) = begin (succ y + zero) ≈ succ (y + zero) by definition ≈ succ y by cong succ (0-is-id y) ∎ +-assoc : ∀ (a b c : ℕ) → (a + b) + c ≡ a + (b + c) +-assoc zero b c = definition +-assoc (succ a) b c = begin ((succ a + b) + c) ≈ succ (a + b) + c by definition ≈ succ ((a + b) + c) by definition ≈ succ (a + (b + c)) by cong succ (+-assoc a b c) ≈ succ a + (b + c) by definition ∎ {- +-assoc zero b c = definition +-assoc (succ a) b c = begin ((succ a + b) + c) ≈ succ (a + b) + c by definition ≈ succ ((a + b) + c) by definition ≈ succ (a + (b + c)) by cong succ (+-assoc a b c) ≈ succ a + (b + c) by definition ∎ -}
module Functor where record IsEquivalence {A : Set} (_≈_ : A → A → Set) : Set where field refl : ∀ {x} → x ≈ x sym : ∀ {i j} → i ≈ j → j ≈ i trans : ∀ {i j k} → i ≈ j → j ≈ k → i ≈ k record Setoid : Set₁ where infix 4 _≈_ field Carrier : Set _≈_ : Carrier → Carrier → Set isEquivalence : IsEquivalence _≈_ open IsEquivalence isEquivalence public infixr 0 _⟶_ record _⟶_ (From To : Setoid) : Set where infixl 5 _⟨$⟩_ field _⟨$⟩_ : Setoid.Carrier From → Setoid.Carrier To cong : ∀ {x y} → Setoid._≈_ From x y → Setoid._≈_ To (_⟨$⟩_ x) (_⟨$⟩_ y) open _⟶_ public id : ∀ {A} → A ⟶ A id = record { _⟨$⟩_ = λ x → x; cong = λ x≈y → x≈y } infixr 9 _∘_ _∘_ : ∀ {A B C} → B ⟶ C → A ⟶ B → A ⟶ C f ∘ g = record { _⟨$⟩_ = λ x → f ⟨$⟩ (g ⟨$⟩ x) ; cong = λ x≈y → cong f (cong g x≈y) } _⇨_ : (To From : Setoid) → Setoid From ⇨ To = record { Carrier = From ⟶ To ; _≈_ = λ f g → ∀ {x y} → x ≈₁ y → f ⟨$⟩ x ≈₂ g ⟨$⟩ y ; isEquivalence = record { refl = λ {f} → cong f ; sym = λ f∼g x∼y → To.sym (f∼g (From.sym x∼y)) ; trans = λ f∼g g∼h x∼y → To.trans (f∼g From.refl) (g∼h x∼y) } } where open module From = Setoid From using () renaming (_≈_ to _≈₁_) open module To = Setoid To using () renaming (_≈_ to _≈₂_) record Functor (F : Setoid → Setoid) : Set₁ where field map : ∀ {A B} → (A ⇨ B) ⟶ (F A ⇨ F B) identity : ∀ {A} → let open Setoid (F A ⇨ F A) in map ⟨$⟩ id ≈ id composition : ∀ {A B C} (f : B ⟶ C) (g : A ⟶ B) → let open Setoid (F A ⇨ F C) in map ⟨$⟩ (f ∘ g) ≈ (map ⟨$⟩ f) ∘ (map ⟨$⟩ g)
{-# OPTIONS --without-K #-} open import HoTT open import cohomology.FunctionOver open import cohomology.Theory {- Useful lemmas concerning the functorial action of C -} module cohomology.Functor {i} (CT : CohomologyTheory i) where open CohomologyTheory CT open import cohomology.Unit CT open import cohomology.BaseIndependence CT CF-cst : (n : ℤ) {X Y : Ptd i} → CF-hom n (⊙cst {X = X} {Y = Y}) == cst-hom CF-cst n {X} {Y} = CF-hom n (⊙cst {X = ⊙LU} ⊙∘ ⊙cst {X = X}) =⟨ CF-comp n ⊙cst ⊙cst ⟩ (CF-hom n (⊙cst {X = X})) ∘ᴳ (CF-hom n (⊙cst {X = ⊙LU})) =⟨ hom= (CF-hom n (⊙cst {X = ⊙LU})) cst-hom (contr-has-all-paths (→-level (C-Unit-is-contr n)) _ _) |in-ctx (λ w → CF-hom n (⊙cst {X = X} {Y = ⊙LU}) ∘ᴳ w) ⟩ (CF-hom n (⊙cst {X = X} {Y = ⊙LU})) ∘ᴳ cst-hom =⟨ pre∘-cst-hom (CF-hom n (⊙cst {X = X} {Y = ⊙LU})) ⟩ cst-hom ∎ where ⊙LU = ⊙Lift {j = i} ⊙Unit CF-inverse : (n : ℤ) {X Y : Ptd i} (f : fst (X ⊙→ Y)) (g : fst (Y ⊙→ X)) → (∀ x → fst g (fst f x) == x) → (CF-hom n f) ∘ᴳ (CF-hom n g) == idhom _ CF-inverse n f g p = ! (CF-comp n g f) ∙ CF-λ= n p ∙ CF-ident n CF-iso : (n : ℤ) {X Y : Ptd i} → X ⊙≃ Y → C n Y ≃ᴳ C n X CF-iso n (f , ie) = (CF-hom n f , is-eq _ (GroupHom.f (CF-hom n (⊙<– (f , ie)))) (app= $ ap GroupHom.f $ CF-inverse n f _ $ <–-inv-l (fst f , ie)) (app= $ ap GroupHom.f $ CF-inverse n _ f $ <–-inv-r (fst f , ie)))
{-# OPTIONS --without-K --rewriting #-} open import HoTT open import homotopy.elims.CofPushoutSection {- If f : X → Y is a section, then ΣY ≃ ΣX ∨ ΣCof(f) -} module homotopy.SuspSectionDecomp {i j} {X : Ptd i} {Y : Ptd j} (⊙f : X ⊙→ Y) (g : de⊙ Y → de⊙ X) (inv : ∀ x → g (fst ⊙f x) == x) where module SuspSectionDecomp where private f = fst ⊙f module Into = SuspRec {C = de⊙ (⊙Susp X ⊙∨ ⊙Susp (⊙Cofiber ⊙f))} (winl south) (winr south) (λ y → ! (ap winl (merid (g y))) ∙ wglue ∙ ap winr (merid (cfcod y))) into = Into.f module OutWinl = SuspRec south south (λ x → ! (merid (f x)) ∙ merid (pt Y)) out-winr-glue : de⊙ (⊙Cofiber ⊙f) → south' (de⊙ Y) == south out-winr-glue = CofiberRec.f idp (λ y → ! (merid (f (g y))) ∙ merid y) (λ x → ! $ ap (λ w → ! (merid (f w)) ∙ merid (f x)) (inv x) ∙ !-inv-l (merid (f x))) module OutWinr = SuspRec south south out-winr-glue out-winl = OutWinl.f out-winr = OutWinr.f module Out = WedgeRec {X = ⊙Susp X} {Y = ⊙Susp (⊙Cofiber ⊙f)} out-winl out-winr idp out = Out.f out-into : ∀ σy → out (into σy) == σy out-into = Susp-elim (! (merid (pt Y))) idp (↓-∘=idf-from-square out into ∘ λ y → (ap (ap out) (Into.merid-β y) ∙ ap-∙ out (! (ap winl (merid (g y)))) (wglue ∙ ap winr (merid (cfcod y))) ∙ part₁ y ∙2 (ap-∙ out wglue (ap winr (merid (cfcod y))) ∙ Out.glue-β ∙2 part₂ y)) ∙v⊡ square-lemma (merid (pt Y)) (merid (f (g y))) (merid y)) where part₁ : (y : de⊙ Y) → ap out (! (ap winl (merid (g y)))) == ! (merid (pt Y)) ∙ merid (f (g y)) part₁ y = ap-! out (ap winl (merid (g y))) ∙ ap ! (∘-ap out winl (merid (g y))) ∙ ap ! (OutWinl.merid-β (g y)) ∙ !-∙ (! (merid (f (g y)))) (merid (pt Y)) ∙ ap (λ w → ! (merid (pt Y)) ∙ w) (!-! (merid (f (g y)))) part₂ : (y : de⊙ Y) → ap out (ap winr (merid (cfcod y))) == ! (merid (f (g y))) ∙ merid y part₂ y = ∘-ap out winr (merid (cfcod y)) ∙ OutWinr.merid-β (cfcod y) square-lemma : ∀ {i} {A : Type i} {x y z w : A} (p : x == y) (q : x == z) (r : x == w) → Square (! p) ((! p ∙ q) ∙ ! q ∙ r) r idp square-lemma idp idp idp = ids into-out : ∀ w → into (out w) == w into-out = Wedge-elim into-out-winl into-out-winr (↓-∘=idf-from-square into out $ ap (ap into) Out.glue-β ∙v⊡ glue-square-lemma (! (ap winr (merid cfbase))) wglue) where {- Three path induction lemmas to simply some squares -} winl-square-lemma : ∀ {i} {A : Type i} {x y z w : A} (p q : x == y) (s : x == z) (r t u : z == w) (v : x == y) → p == q → r == t → r == u → Square (! r ∙ ! s) (! (! p ∙ s ∙ t) ∙ ! v ∙ s ∙ u) q (! r ∙ ! s ∙ v) winl-square-lemma idp .idp idp idp .idp .idp v idp idp idp = ∙-unit-r _ ∙v⊡ rt-square v winr-square-lemma : ∀ {i} {A : Type i} {x y z w : A} (p q u : x == y) (r t : z == w) (s : z == x) → p == q → r == t → Square (! p) (! (! r ∙ s ∙ q) ∙ ! t ∙ s ∙ u) u idp winr-square-lemma idp .idp u idp .idp idp idp idp = vid-square glue-square-lemma : ∀ {i} {A : Type i} {x y z : A} (p : x == y) (q : z == y) → Square (p ∙ ! q) idp q p glue-square-lemma idp idp = ids into-out-winl : ∀ σx → into (out (winl σx)) == winl σx into-out-winl = Susp-elim (! (ap winr (merid cfbase)) ∙ ! wglue) (! (ap winr (merid cfbase)) ∙ ! wglue ∙ ap winl (merid (g (pt Y)))) (↓-='-from-square ∘ λ x → (ap-∘ into out-winl (merid x) ∙ ap (ap into) (OutWinl.merid-β x) ∙ ap-∙ into (! (merid (f x))) (merid (pt Y)) ∙ (ap-! into (merid (f x)) ∙ ap ! (Into.merid-β (f x))) ∙2 Into.merid-β (pt Y)) ∙v⊡ winl-square-lemma (ap winl (merid (g (f x)))) (ap winl (merid x)) wglue (ap winr (merid cfbase)) (ap winr (merid (cfcod (f x)))) (ap winr (merid (cfcod (pt Y)))) (ap winl (merid (g (pt Y)))) (ap (ap winl ∘ merid) (inv x)) (ap (ap winr ∘ merid) (cfglue x)) (ap (ap winr ∘ merid) (cfglue (pt X) ∙ ap cfcod (snd ⊙f)))) into-out-winr : ∀ σκ → into (out (winr σκ)) == winr σκ into-out-winr = CofPushoutSection.elim {h = λ _ → unit} g inv (! (ap winr (merid cfbase))) (λ tt → idp) (λ tt → transport (λ κ → ! (ap winr (merid cfbase)) == idp [ (λ σκ → into (out (winr σκ)) == winr σκ) ↓ merid κ ]) (! (cfglue (pt X))) (into-out-winr-coh (f (pt X)))) into-out-winr-coh where into-out-winr-coh : (y : de⊙ Y) → ! (ap winr (merid cfbase)) == idp [ (λ σκ → into (out (winr σκ)) == winr σκ) ↓ merid (cfcod y) ] into-out-winr-coh y = ↓-='-from-square $ (ap-∘ into out-winr (merid (cfcod y)) ∙ ap (ap into) (OutWinr.merid-β (cfcod y)) ∙ ap-∙ into (! (merid (f (g y)))) (merid y) ∙ (ap-! into (merid (f (g y))) ∙ ap ! (Into.merid-β (f (g y)))) ∙2 Into.merid-β y) ∙v⊡ winr-square-lemma (ap winr (merid cfbase)) (ap winr (merid (cfcod (f (g y))))) (ap winr (merid (cfcod y))) (ap winl (merid (g (f (g y))))) (ap winl (merid (g y))) wglue (ap (ap winr ∘ merid) (cfglue (g y))) (ap (ap winl ∘ merid) (inv (g y))) eq : Susp (de⊙ Y) ≃ ⊙Susp X ∨ ⊙Susp (⊙Cofiber ⊙f) eq = equiv into out into-out out-into
module Numeral.Natural.Oper.FlooredDivision.Proofs where import Lvl open import Data open import Data.Boolean.Stmt open import Numeral.Natural open import Numeral.Natural.Oper.Comparisons open import Numeral.Natural.Oper.Comparisons.Proofs open import Numeral.Natural.Oper.FlooredDivision open import Numeral.Natural.Oper.Proofs open import Numeral.Natural.Oper open import Numeral.Natural.Relation open import Numeral.Natural.Relation.Order open import Relator.Equals open import Relator.Equals.Proofs private variable d d₁ d₂ b a' b' : ℕ inddiv-result-𝐒 : [ 𝐒 d , b ] a' div b' ≡ 𝐒([ d , b ] a' div b') inddiv-result-𝐒 {d} {b} {𝟎} {b'} = [≡]-intro inddiv-result-𝐒 {d} {b} {𝐒 a'} {𝟎} = inddiv-result-𝐒 {𝐒 d} {b} {a'} {b} inddiv-result-𝐒 {d} {b} {𝐒 a'} {𝐒 b'} = inddiv-result-𝐒 {d}{b}{a'}{b'} {-# REWRITE inddiv-result-𝐒 #-} inddiv-result : [ d , b ] a' div b' ≡ d + ([ 𝟎 , b ] a' div b') inddiv-result {𝟎} = [≡]-intro inddiv-result {𝐒 d}{b}{a'}{b'} = [≡]-with(𝐒) (inddiv-result {d}{b}{a'}{b'}) inddiv-of-denominator : [ d , b ] b' div b' ≡ d inddiv-of-denominator {d} {b} {𝟎} = [≡]-intro inddiv-of-denominator {d} {b} {𝐒 b'} = inddiv-of-denominator{d}{b}{b'} inddiv-of-denominator-successor : [ d , b ] 𝐒 b' div b' ≡ 𝐒 d inddiv-of-denominator-successor {d} {b} {𝟎} = [≡]-intro inddiv-of-denominator-successor {d} {b} {𝐒 b'} = inddiv-of-denominator-successor{d}{b}{b'} inddiv-step-denominator : [ d , b ] (a' + b') div b' ≡ [ d , b ] a' div 𝟎 inddiv-step-denominator {_} {_} {_} {𝟎} = [≡]-intro inddiv-step-denominator {d} {b} {a'} {𝐒 b'} = inddiv-step-denominator {d} {b} {a'} {b'} inddiv-smaller : (a' ≤ b') → ([ d , b ] a' div b' ≡ d) inddiv-smaller min = [≡]-intro inddiv-smaller {d = d}{b} (succ {𝟎} {𝟎} p) = [≡]-intro inddiv-smaller {d = d}{b} (succ {𝟎} {𝐒 b'} p) = [≡]-intro inddiv-smaller {d = d}{b} (succ {𝐒 a'}{𝐒 b'} p) = inddiv-smaller {d = d}{b} p [⌊/⌋][⌊/⌋₀]-equality : ∀{a b} → ⦃ _ : IsTrue(positive?(b))⦄ → (a ⌊/⌋₀ b ≡ a ⌊/⌋ b) [⌊/⌋][⌊/⌋₀]-equality {b = 𝐒 b} = [≡]-intro [⌊/⌋]-of-0ₗ : ∀{n} → ⦃ _ : IsTrue(positive?(n))⦄ → (𝟎 ⌊/⌋ n ≡ 𝟎) [⌊/⌋]-of-0ₗ {𝐒 n} = [≡]-intro [⌊/⌋]-of-1ₗ : ∀{n} → ⦃ _ : IsTrue(positive?(n))⦄ → ⦃ _ : IsTrue(n ≢? 1)⦄ → (1 ⌊/⌋ n ≡ 𝟎) [⌊/⌋]-of-1ₗ {𝐒 (𝐒 n)} = [≡]-intro [⌊/⌋]-of-1ᵣ : ∀{m} → (m ⌊/⌋ 1 ≡ m) [⌊/⌋]-of-1ᵣ {𝟎} = [≡]-intro [⌊/⌋]-of-1ᵣ {𝐒 m} = [≡]-with(𝐒) ([⌊/⌋]-of-1ᵣ {m}) [⌊/⌋]-of-same : ∀{n} → ⦃ _ : IsTrue(positive?(n))⦄ → (n ⌊/⌋ n ≡ 1) [⌊/⌋]-of-same {𝐒 n} = inddiv-of-denominator-successor {b' = n} postulate [⌊/⌋]-positive : ∀{a b} ⦃ _ : Positive(a) ⦄ ⦃ _ : Positive(b) ⦄ → Positive(a ⌊/⌋ b) {- [⌊/⌋]-of-[+]ₗ : ∀{m n} → ⦃ _ : IsTrue(n ≢? 𝟎)⦄ → ((m + n) ⌊/⌋ n ≡ 𝐒(m ⌊/⌋ n)) [⌊/⌋]-of-[+]ₗ {_} {𝐒 𝟎} = [≡]-intro [⌊/⌋]-of-[+]ₗ {𝟎} {𝐒 (𝐒 n)} = [≡]-intro [⌊/⌋]-of-[+]ₗ {𝐒 m} {𝐒 (𝐒 n)} = {![⌊/⌋]-of-[+]ₗ {m} {𝐒 (𝐒 n)}!} [⌊/⌋]-is-0 : ∀{m n} → ⦃ _ : IsTrue(n ≢? 𝟎)⦄ → (m ⌊/⌋ n ≡ 𝟎) → (m ≡ 𝟎) [⌊/⌋]-is-0 {𝟎} {𝐒 n} _ = [≡]-intro [⌊/⌋]-is-0 {𝐒 m} {𝐒(𝐒 n)} p with [⌊/⌋]-is-0 {𝐒 m} {𝐒 n} {!!} ... | pp = {!!} -} postulate [⌊/⌋]-leₗ : ∀{a b} ⦃ _ : IsTrue(positive?(b))⦄ → (a ⌊/⌋ b ≤ a) postulate [⌊/⌋]-ltₗ : ∀{a} ⦃ _ : IsTrue(positive?(a))⦄ {b} ⦃ b-proof : IsTrue(b >? 1)⦄ → ((a ⌊/⌋ b) ⦃ [<?]-positive-any {1}{b} ⦄ < a) postulate [⌊/⌋]-zero : ∀{a b} ⦃ _ : IsTrue(positive?(b))⦄ → (a < b) → (a ⌊/⌋ b ≡ 𝟎) postulate [⌊/⌋]-preserve-[<]ₗ : ∀{a b d} ⦃ _ : IsTrue(positive?(b))⦄ ⦃ _ : IsTrue(positive?(d))⦄ → (a < b) → (a ⌊/⌋ d < b) postulate [⌊/⌋][+]-distributivityᵣ : ∀{a b c} ⦃ _ : IsTrue(positive?(c))⦄ → ((a + b) ⌊/⌋ c ≡ (a ⌊/⌋ c) + (b ⌊/⌋ c))
{-# OPTIONS --without-K --rewriting --no-sized-types --no-guardedness --no-subtyping #-} module Agda.Builtin.Equality.Rewrite where open import Agda.Builtin.Equality {-# BUILTIN REWRITE _≡_ #-}
open import Oscar.Prelude open import Oscar.Class open import Oscar.Class.Transitivity module Oscar.Class.Transextensionality where module Transextensionality {𝔬} {𝔒 : Ø 𝔬} {𝔯} (_∼_ : 𝔒 → 𝔒 → Ø 𝔯) {ℓ} (_∼̇_ : ∀ {x y} → x ∼ y → x ∼ y → Ø ℓ) (let infix 4 _∼̇_ ; _∼̇_ = _∼̇_) (transitivity : Transitivity.type _∼_) (let _∙_ : FlipTransitivity.type _∼_ _∙_ g f = transitivity f g) = ℭLASS ((λ {x y} → _∼̇_ {x} {y}) , λ {x y z} → transitivity {x} {y} {z}) -- FIXME what other possibilities work here? (∀ {x y z} {f₁ f₂ : x ∼ y} {g₁ g₂ : y ∼ z} → f₁ ∼̇ f₂ → g₁ ∼̇ g₂ → g₁ ∙ f₁ ∼̇ g₂ ∙ f₂) module Transextensionality! {𝔬} {𝔒 : Ø 𝔬} {𝔯} (_∼_ : 𝔒 → 𝔒 → Ø 𝔯) {ℓ} (_∼̇_ : ∀ {x y} → x ∼ y → x ∼ y → Ø ℓ) (let infix 4 _∼̇_ ; _∼̇_ = _∼̇_) ⦃ tr : Transitivity.class _∼_ ⦄ = Transextensionality (_∼_) (λ {x y} → _∼̇_ {x} {y}) transitivity module _ {𝔬} {𝔒 : Ø 𝔬} {𝔯} {_∼_ : 𝔒 → 𝔒 → Ø 𝔯} {ℓ} {_∼̇_ : ∀ {x y} → x ∼ y → x ∼ y → Ø ℓ} {transitivity : Transitivity.type _∼_} where transextensionality = Transextensionality.method _∼_ _∼̇_ transitivity
module AocIO where open import IO.Primitive public open import Data.String as String open import Data.List as List postulate getLine : IO Costring getArgs : IO (List String) getProgName : IO String {-# COMPILE GHC getLine = getLine #-} {-# FOREIGN GHC import qualified Data.Text as Text #-} {-# FOREIGN GHC import qualified Data.Text.IO as Text #-} {-# FOREIGN GHC import System.Environment (getArgs, getProgName) #-} {-# COMPILE GHC getArgs = fmap (map Text.pack) getArgs #-} {-# COMPILE GHC getProgName = fmap Text.pack getProgName #-}
module Structure.Operator.Proofs where import Lvl open import Data open import Data.Tuple open import Functional hiding (id) open import Function.Equals import Function.Names as Names import Lang.Vars.Structure.Operator open Lang.Vars.Structure.Operator.Select open import Logic.IntroInstances open import Logic.Propositional open import Logic.Predicate open import Structure.Setoid open import Structure.Setoid.Uniqueness open import Structure.Function.Domain open import Structure.Function.Multi open import Structure.Function import Structure.Operator.Names as Names open import Structure.Operator.Properties open import Structure.Operator open import Structure.Relator.Properties open import Syntax.Transitivity open import Syntax.Type open import Type module One {ℓ ℓₑ} {T : Type{ℓ}} ⦃ equiv : Equiv{ℓₑ}(T) ⦄ {_▫_ : T → T → T} where open Lang.Vars.Structure.Operator.One ⦃ equiv = equiv ⦄ {_▫_ = _▫_} -- When an identity element exists and is the same for both sides, it is unique. unique-identity : Unique(Identity(_▫_)) unique-identity{x₁}{x₂} (intro ⦃ intro identityₗ₁ ⦄ ⦃ intro identityᵣ₁ ⦄) (intro ⦃ intro identityₗ₂ ⦄ ⦃ intro identityᵣ₂ ⦄) = x₁ 🝖-[ symmetry(_≡_) (identityₗ₂{x₁}) ] x₂ ▫ x₁ 🝖-[ identityᵣ₁{x₂} ] x₂ 🝖-end -- An existing left identity is unique when the operator is commutative unique-identityₗ-by-commutativity : let _ = comm in Unique(Identityₗ(_▫_)) unique-identityₗ-by-commutativity {x₁}{x₂} (intro identity₁) (intro identity₂) = x₁ 🝖-[ symmetry(_≡_) (identity₂{x₁}) ] x₂ ▫ x₁ 🝖-[ commutativity(_▫_) {x₂}{x₁} ] x₁ ▫ x₂ 🝖-[ identity₁{x₂} ] x₂ 🝖-end -- An existing right identity is unique when the operator is commutative unique-identityᵣ-by-commutativity : let _ = comm in Unique(Identityᵣ(_▫_)) unique-identityᵣ-by-commutativity {x₁}{x₂} (intro identity₁) (intro identity₂) = x₁ 🝖-[ symmetry(_≡_) (identity₂{x₁}) ] x₁ ▫ x₂ 🝖-[ commutativity(_▫_) {x₁}{x₂} ] x₂ ▫ x₁ 🝖-[ identity₁{x₂} ] x₂ 🝖-end -- An existing left identity is unique when the operator is cancellable unique-identityₗ-by-cancellationᵣ : let _ = cancᵣ in Unique(Identityₗ(_▫_)) unique-identityₗ-by-cancellationᵣ {x₁}{x₂} (intro identity₁) (intro identity₂) = cancellationᵣ(_▫_) {x₁}{x₁}{x₂} ( x₁ ▫ x₁ 🝖-[ identity₁{x₁} ] x₁ 🝖-[ symmetry(_≡_) (identity₂{x₁}) ] x₂ ▫ x₁ 🝖-end ) :of: (x₁ ≡ x₂) -- An existing left identity is unique when the operator is cancellable unique-identityᵣ-by-cancellationₗ : let _ = cancₗ in Unique(Identityᵣ(_▫_)) unique-identityᵣ-by-cancellationₗ {x₁}{x₂} (intro identity₁) (intro identity₂) = cancellationₗ(_▫_) {x₂}{x₁}{x₂} ( x₂ ▫ x₁ 🝖-[ identity₁{x₂} ] x₂ 🝖-[ symmetry(_≡_) (identity₂{x₂}) ] x₂ ▫ x₂ 🝖-end ) :of: (x₁ ≡ x₂) -- When identities for both sides exists, they are the same unique-identities : ⦃ _ : Identityₗ(_▫_)(idₗ) ⦄ → ⦃ _ : Identityᵣ(_▫_)(idᵣ) ⦄ → (idₗ ≡ idᵣ) unique-identities {idₗ}{idᵣ} = idₗ 🝖-[ symmetry(_≡_) (identityᵣ(_▫_)(idᵣ)) ] idₗ ▫ idᵣ 🝖-[ identityₗ(_▫_)(idₗ) ] idᵣ 🝖-end -- When identities for both sides exists, they are the same identity-equivalence-by-commutativity : let _ = comm in Identityₗ(_▫_)(id) ↔ Identityᵣ(_▫_)(id) identity-equivalence-by-commutativity {id} = [↔]-intro l r where l : Identityₗ(_▫_)(id) ← Identityᵣ(_▫_)(id) Identityₗ.proof (l identᵣ) {x} = commutativity(_▫_) 🝖 identityᵣ(_▫_)(id) ⦃ identᵣ ⦄ r : Identityₗ(_▫_)(id) → Identityᵣ(_▫_)(id) Identityᵣ.proof (r identₗ) {x} = commutativity(_▫_) 🝖 identityₗ(_▫_)(id) ⦃ identₗ ⦄ -- Cancellation is possible when the operator is associative and have an inverse cancellationₗ-by-associativity-inverse : let _ = op , assoc , inverₗ in Cancellationₗ(_▫_) Cancellationₗ.proof(cancellationₗ-by-associativity-inverse {idₗ} {invₗ} ) {x}{a}{b} (xa≡xb) = a 🝖-[ symmetry(_≡_) (identityₗ(_▫_)(idₗ) {a}) ] idₗ ▫ a 🝖-[ congruence₂ₗ(_▫_)(a) (symmetry(_≡_) (inverseFunctionₗ(_▫_)(invₗ) {x})) ] (invₗ x ▫ x) ▫ a 🝖-[ associativity(_▫_) {invₗ(x)}{x}{a} ] invₗ x ▫ (x ▫ a) 🝖-[ congruence₂ᵣ(_▫_)(invₗ(x)) (xa≡xb) ] invₗ x ▫ (x ▫ b) 🝖-[ symmetry(_≡_) (associativity(_▫_) {invₗ(x)}{x}{b}) ] (invₗ x ▫ x) ▫ b 🝖-[ congruence₂ₗ(_▫_)(b) (inverseFunctionₗ(_▫_)(invₗ) {x}) ] idₗ ▫ b 🝖-[ identityₗ(_▫_)(idₗ){b} ] b 🝖-end -- TODO: This pattern of applying symmetric transitivity rules, make it a function -- Cancellation is possible when the operator is associative and have an inverse cancellationᵣ-by-associativity-inverse : let _ = op , assoc , inverᵣ in Cancellationᵣ(_▫_) Cancellationᵣ.proof(cancellationᵣ-by-associativity-inverse {idᵣ} {invᵣ} ) {x}{a}{b} (xa≡xb) = a 🝖-[ symmetry(_≡_) (identityᵣ(_▫_)(idᵣ)) ] a ▫ idᵣ 🝖-[ congruence₂ᵣ(_▫_)(_) (symmetry(_≡_) (inverseFunctionᵣ(_▫_)(invᵣ))) ] a ▫ (x ▫ invᵣ x) 🝖-[ symmetry(_≡_) (associativity(_▫_)) ] (a ▫ x) ▫ invᵣ x 🝖-[ congruence₂ₗ(_▫_)(_) (xa≡xb) ] (b ▫ x) ▫ invᵣ x 🝖-[ associativity(_▫_) ] b ▫ (x ▫ invᵣ x) 🝖-[ congruence₂ᵣ(_▫_)(_) (inverseFunctionᵣ(_▫_)(invᵣ)) ] b ▫ idᵣ 🝖-[ identityᵣ(_▫_)(idᵣ) ] b 🝖-end -- When something and something else's inverse is the identity element, they are equal equality-zeroₗ : let _ = op , assoc , select-inv(id)(ident)(inv)(inver) in ∀{x y} → (x ≡ y) ← (x ▫ inv(y) ≡ id) equality-zeroₗ {id}{inv} {x}{y} (proof) = x 🝖-[ symmetry(_≡_) (identity-right(_▫_)(id)) ] x ▫ id 🝖-[ symmetry(_≡_) (congruence₂ᵣ(_▫_)(x) (inverseFunction-left(_▫_)(inv))) ] x ▫ (inv(y) ▫ y) 🝖-[ symmetry(_≡_) (associativity(_▫_)) ] (x ▫ inv(y)) ▫ y 🝖-[ congruence₂ₗ(_▫_)(y) (proof) ] id ▫ y 🝖-[ identity-left(_▫_)(id) ] y 🝖-end equality-zeroᵣ : let _ = op , assoc , select-inv(id)(ident)(inv)(inver) in ∀{x y} → (x ≡ y) → (x ▫ inv(y) ≡ id) equality-zeroᵣ {id}{inv} {x}{y} (proof) = x ▫ inv(y) 🝖-[ congruence₂ₗ(_▫_)(inv(y)) proof ] y ▫ inv(y) 🝖-[ inverseFunctionᵣ(_▫_)(inv) ] id 🝖-end commuting-id : let _ = select-id(id)(ident) in ∀{x} → (id ▫ x ≡ x ▫ id) commuting-id {id} {x} = id ▫ x 🝖-[ identity-left(_▫_)(id) ] x 🝖-[ symmetry(_≡_) (identity-right(_▫_)(id)) ] x ▫ id 🝖-end squeezed-inverse : let _ = op , select-id(id)(ident) in ∀{a b x y} → (a ▫ b ≡ id) → ((x ▫ (a ▫ b)) ▫ y ≡ x ▫ y) squeezed-inverse {id} {a}{b}{x}{y} abid = (x ▫ (a ▫ b)) ▫ y 🝖-[ (congruence₂ₗ(_▫_)(_) ∘ congruence₂ᵣ(_▫_)(_)) abid ] (x ▫ id) ▫ y 🝖-[ congruence₂ₗ(_▫_)(_) (identity-right(_▫_)(id)) ] x ▫ y 🝖-end double-inverse : let _ = cancᵣ , select-inv(id)(ident)(inv)(inver) in ∀{x} → (inv(inv x) ≡ x) double-inverse {id}{inv} {x} = (cancellationᵣ(_▫_) $ ( inv(inv x) ▫ inv(x) 🝖-[ inverseFunction-left(_▫_)(inv) ] id 🝖-[ inverseFunction-right(_▫_)(inv) ]-sym x ▫ inv(x) 🝖-end ) :of: (inv(inv x) ▫ inv(x) ≡ x ▫ inv(x)) ) :of: (inv(inv x) ≡ x) double-inverseₗ-by-id : let _ = op , assoc , select-id(id)(ident) , select-invₗ(id)(Identity.left(ident))(invₗ)(inverₗ) in ∀{x} → (invₗ(invₗ x) ≡ x) double-inverseₗ-by-id {id}{inv} {x} = inv(inv(x)) 🝖-[ symmetry(_≡_) (identityᵣ(_▫_)(id)) ] inv(inv(x)) ▫ id 🝖-[ congruence₂ᵣ(_▫_)(_) (symmetry(_≡_) (inverseFunctionₗ(_▫_)(inv))) ] inv(inv(x)) ▫ (inv(x) ▫ x) 🝖-[ symmetry(_≡_) (associativity(_▫_)) ] (inv(inv(x)) ▫ inv(x)) ▫ x 🝖-[ congruence₂ₗ(_▫_)(_) (inverseFunctionₗ(_▫_)(inv)) ] id ▫ x 🝖-[ identityₗ(_▫_)(id) ] x 🝖-end double-inverseᵣ-by-id : let _ = op , assoc , select-id(id)(ident) , select-invᵣ(id)(Identity.right(ident))(invᵣ)(inverᵣ) in ∀{x} → (invᵣ(invᵣ x) ≡ x) double-inverseᵣ-by-id {id}{inv} {x} = inv(inv(x)) 🝖-[ identityₗ(_▫_)(id) ]-sym id ▫ inv(inv(x)) 🝖-[ congruence₂ₗ(_▫_)(_) (inverseFunctionᵣ(_▫_)(inv)) ]-sym (x ▫ inv(x)) ▫ inv(inv(x)) 🝖-[ associativity(_▫_) ] x ▫ (inv(x) ▫ inv(inv(x))) 🝖-[ congruence₂ᵣ(_▫_)(_) (inverseFunctionᵣ(_▫_)(inv)) ] x ▫ id 🝖-[ identityᵣ(_▫_)(id) ] x 🝖-end {- double-complement-by-? inv(inv(x)) ▫ inv(x) ab inv(x) ▫ x -} inverse-equivalence-by-id : let _ = op , assoc , select-id(id)(ident) in InverseFunctionₗ(_▫_)(inv) ↔ InverseFunctionᵣ(_▫_)(inv) inverse-equivalence-by-id {id}{inv} = [↔]-intro l r where l : InverseFunctionₗ(_▫_)(inv) ← InverseFunctionᵣ(_▫_)(inv) InverseFunctionₗ.proof (l inverᵣ) {x} = inv(x) ▫ x 🝖-[ congruence₂ᵣ(_▫_)(_) (symmetry(_≡_) (double-inverseᵣ-by-id ⦃ inverᵣ = inverᵣ ⦄)) ] inv(x) ▫ inv(inv(x)) 🝖-[ inverseFunctionᵣ(_▫_)(inv) ⦃ inverᵣ ⦄ ] id 🝖-end r : InverseFunctionₗ(_▫_)(inv) → InverseFunctionᵣ(_▫_)(inv) InverseFunctionᵣ.proof (r inverₗ) {x} = x ▫ inv(x) 🝖-[ congruence₂ₗ(_▫_)(_) (symmetry(_≡_) (double-inverseₗ-by-id ⦃ inverₗ = inverₗ ⦄)) ] inv(inv(x)) ▫ inv(x) 🝖-[ inverseFunctionₗ(_▫_)(inv) ⦃ inverₗ ⦄ ] id 🝖-end {- complement-equivalence-by-id : let _ = op , assoc , select-abs(ab)(absorb) in ComplementFunctionₗ(_▫_)(inv) ↔ ComplementFunctionᵣ(_▫_)(inv) complement-equivalence-by-id {ab}{inv} = [↔]-intro l r where l : ComplementFunctionₗ(_▫_)(inv) ← ComplementFunctionᵣ(_▫_)(inv) ComplementFunctionₗ.proof (l absorbᵣ) {x} = inv(x) ▫ x 🝖-[ {!!} ] inv(x) ▫ inv(inv(x)) 🝖-[ {!!} ] ab 🝖-end r : ComplementFunctionₗ(_▫_)(inv) → ComplementFunctionᵣ(_▫_)(inv) ComplementFunctionᵣ.proof (r absorbₗ) {x} = x ▫ inv(x) 🝖-[ {!!} ] inv(inv(x)) ▫ inv(x) 🝖-[ {!!} ] ab 🝖-end -} cancellationₗ-by-group : let _ = op , assoc , select-invₗ(idₗ)(identₗ)(invₗ)(inverₗ) in Cancellationₗ(_▫_) Cancellationₗ.proof (cancellationₗ-by-group {id}{inv}) {a}{b}{c} abac = b 🝖-[ symmetry(_≡_) (identityₗ(_▫_)(id)) ] id ▫ b 🝖-[ congruence₂ₗ(_▫_)(_) (symmetry(_≡_) (inverseFunctionₗ(_▫_)(inv))) ] (inv(a) ▫ a) ▫ b 🝖-[ associativity(_▫_) ] inv(a) ▫ (a ▫ b) 🝖-[ congruence₂ᵣ(_▫_)(_) abac ] inv(a) ▫ (a ▫ c) 🝖-[ symmetry(_≡_) (associativity(_▫_)) ] (inv(a) ▫ a) ▫ c 🝖-[ congruence₂ₗ(_▫_)(_) (inverseFunctionₗ(_▫_)(inv)) ] id ▫ c 🝖-[ identityₗ(_▫_)(id) ] c 🝖-end cancellationᵣ-by-group : let _ = op , assoc , select-invᵣ(idᵣ)(identᵣ)(invᵣ)(inverᵣ) in Cancellationᵣ(_▫_) Cancellationᵣ.proof (cancellationᵣ-by-group {id}{inv}) {c}{a}{b} acbc = a 🝖-[ symmetry(_≡_) (identityᵣ(_▫_)(id)) ] a ▫ id 🝖-[ congruence₂ᵣ(_▫_)(_) (symmetry(_≡_) (inverseFunctionᵣ(_▫_)(inv))) ] a ▫ (c ▫ inv(c)) 🝖-[ symmetry(_≡_) (associativity(_▫_)) ] (a ▫ c) ▫ inv(c) 🝖-[ congruence₂ₗ(_▫_)(_) acbc ] (b ▫ c) ▫ inv(c) 🝖-[ associativity(_▫_) ] b ▫ (c ▫ inv(c)) 🝖-[ congruence₂ᵣ(_▫_)(_) (inverseFunctionᵣ(_▫_)(inv)) ] b ▫ id 🝖-[ identityᵣ(_▫_)(id) ] b 🝖-end inverse-distribution : let _ = op , assoc , select-inv(id)(ident)(inv)(inver) in ∀{x y} → (inv(x ▫ y) ≡ inv(y) ▫ inv(x)) inverse-distribution {id}{inv} {x}{y} = (cancellationᵣ(_▫_) ⦃ cancellationᵣ-by-group ⦄ (( inv(x ▫ y) ▫ (x ▫ y) 🝖-[ inverseFunction-left(_▫_)(inv) ] id 🝖-[ symmetry(_≡_) (inverseFunction-left(_▫_)(inv)) ] inv(y) ▫ y 🝖-[ symmetry(_≡_) (squeezed-inverse (inverseFunction-left(_▫_)(inv))) ] (inv(y) ▫ (inv(x) ▫ x)) ▫ y 🝖-[ congruence₂ₗ(_▫_)(_) (symmetry(_≡_) (associativity(_▫_))) ] ((inv(y) ▫ inv(x)) ▫ x) ▫ y 🝖-[ associativity(_▫_) ] (inv(y) ▫ inv(x)) ▫ (x ▫ y) 🝖-end ) :of: (inv(x ▫ y) ▫ (x ▫ y) ≡ (inv(y) ▫ inv(x)) ▫ (x ▫ y))) ) :of: (inv(x ▫ y) ≡ inv(y) ▫ inv(x)) inverse-preserving : let _ = op , assoc , comm , select-inv(id)(ident)(inv)(inver) in Preserving₂(inv)(_▫_)(_▫_) Preserving.proof (inverse-preserving {id}{inv}) {x}{y} = inverse-distribution {id}{inv} {x}{y} 🝖 commutativity(_▫_) inverse-distribute-to-inverse : let _ = op , assoc , select-inv(id)(ident)(inv)(inver) in ∀{x y} → inv(inv x ▫ inv y) ≡ y ▫ x inverse-distribute-to-inverse {id}{inv} {x}{y} = inv(inv x ▫ inv y) 🝖-[ inverse-distribution ] inv(inv y) ▫ inv(inv x) 🝖-[ congruence₂ᵣ(_▫_)(_) (double-inverse ⦃ cancᵣ = cancellationᵣ-by-group ⦄) ] inv(inv y) ▫ x 🝖-[ congruence₂ₗ(_▫_)(_) (double-inverse ⦃ cancᵣ = cancellationᵣ-by-group ⦄) ] y ▫ x 🝖-end inverse-preserving-to-inverse : let _ = op , assoc , comm , select-inv(id)(ident)(inv)(inver) in ∀{x y} → inv(inv x ▫ inv y) ≡ x ▫ y inverse-preserving-to-inverse {id}{inv} {x}{y} = inverse-distribute-to-inverse {id}{inv} {x}{y} 🝖 commutativity(_▫_) unique-inverseₗ-by-id : let _ = op , assoc , select-id(id)(ident) , select-invₗ(id)(Identity.left ident)(inv)(inverₗ) in ∀{x x⁻¹} → (x⁻¹ ▫ x ≡ id) → (x⁻¹ ≡ inv(x)) unique-inverseₗ-by-id {id = id} {inv = inv} {x}{x⁻¹} inver-elem = x⁻¹ 🝖-[ identityᵣ(_▫_)(id) ]-sym x⁻¹ ▫ id 🝖-[ congruence₂ᵣ(_▫_)(_) (inverseFunctionₗ(_▫_)(inv)) ]-sym x⁻¹ ▫ (inv(inv(x)) ▫ inv(x)) 🝖-[ associativity(_▫_) ]-sym (x⁻¹ ▫ inv(inv(x))) ▫ inv(x) 🝖-[ congruence₂ₗ(_▫_)(_) (congruence₂ᵣ(_▫_)(_) (double-inverseₗ-by-id)) ] (x⁻¹ ▫ x) ▫ inv(x) 🝖-[ congruence₂ₗ(_▫_)(_) inver-elem ] id ▫ inv(x) 🝖-[ identityₗ(_▫_)(id) ] inv(x) 🝖-end unique-inverseᵣ-by-id : let _ = op , assoc , select-id(id)(ident) , select-invᵣ(id)(Identity.right ident)(inv)(inverᵣ) in ∀{x x⁻¹} → (x ▫ x⁻¹ ≡ id) → (x⁻¹ ≡ inv(x)) unique-inverseᵣ-by-id {id = id} {inv = inv} {x}{x⁻¹} inver-elem = x⁻¹ 🝖-[ identityₗ(_▫_)(id) ]-sym id ▫ x⁻¹ 🝖-[ congruence₂ₗ(_▫_)(_) (inverseFunctionᵣ(_▫_)(inv)) ]-sym (inv(x) ▫ inv(inv(x))) ▫ x⁻¹ 🝖-[ associativity(_▫_) ] inv(x) ▫ (inv(inv(x)) ▫ x⁻¹) 🝖-[ congruence₂ᵣ(_▫_)(_) (congruence₂ₗ(_▫_)(_) double-inverseᵣ-by-id) ] inv(x) ▫ (x ▫ x⁻¹) 🝖-[ congruence₂ᵣ(_▫_)(_) inver-elem ] inv(x) ▫ id 🝖-[ identityᵣ(_▫_)(id) ] inv(x) 🝖-end unique-inverseFunctionₗ-by-id : let _ = op , assoc , select-id(id)(ident) in Unique(InverseFunctionₗ(_▫_)) unique-inverseFunctionₗ-by-id {id = id} {x = inv₁} {inv₂} inverse₁ inverse₂ = intro \{x} → unique-inverseₗ-by-id ⦃ inverₗ = inverse₂ ⦄ (inverseFunctionₗ(_▫_)(inv₁) ⦃ inverse₁ ⦄) unique-inverseFunctionᵣ-by-id : let _ = op , assoc , select-id(id)(ident) in Unique(InverseFunctionᵣ(_▫_)) unique-inverseFunctionᵣ-by-id {id = id} {x = inv₁} {inv₂} inverse₁ inverse₂ = intro \{x} → unique-inverseᵣ-by-id ⦃ inverᵣ = inverse₂ ⦄ (inverseFunctionᵣ(_▫_)(inv₁) ⦃ inverse₁ ⦄) unique-inverses : let _ = op , assoc , select-id(id)(ident) in ⦃ _ : InverseFunctionₗ(_▫_)(invₗ) ⦄ → ⦃ _ : InverseFunctionᵣ(_▫_)(invᵣ) ⦄ → (invₗ ≡ invᵣ) unique-inverses {id} {invₗ} {invᵣ} = intro \{x} → ( invₗ(x) 🝖-[ symmetry(_≡_) (identityᵣ(_▫_)(id)) ] invₗ(x) ▫ id 🝖-[ congruence₂ᵣ(_▫_)(_) (symmetry(_≡_) (inverseFunctionᵣ(_▫_)(invᵣ))) ] invₗ(x) ▫ (x ▫ invᵣ(x)) 🝖-[ symmetry(_≡_) (associativity(_▫_)) ] (invₗ(x) ▫ x) ▫ invᵣ(x) 🝖-[ congruence₂ₗ(_▫_)(_) (inverseFunctionₗ(_▫_)(invₗ)) ] id ▫ invᵣ(x) 🝖-[ identityₗ(_▫_)(id) ] invᵣ(x) 🝖-end ) absorber-equivalence-by-commutativity : let _ = comm in Absorberₗ(_▫_)(ab) ↔ Absorberᵣ(_▫_)(ab) absorber-equivalence-by-commutativity {ab} = [↔]-intro l r where r : Absorberₗ(_▫_)(ab) → Absorberᵣ(_▫_)(ab) Absorberᵣ.proof (r absoₗ) {x} = x ▫ ab 🝖-[ commutativity(_▫_) ] ab ▫ x 🝖-[ absorberₗ(_▫_)(ab) ⦃ absoₗ ⦄ ] ab 🝖-end l : Absorberₗ(_▫_)(ab) ← Absorberᵣ(_▫_)(ab) Absorberₗ.proof (l absoᵣ) {x} = ab ▫ x 🝖-[ commutativity(_▫_) ] x ▫ ab 🝖-[ absorberᵣ(_▫_)(ab) ⦃ absoᵣ ⦄ ] ab 🝖-end inverse-propertyₗ-by-groupₗ : let _ = op , assoc , select-invₗ(id)(identₗ)(inv)(inverₗ) in InversePropertyₗ(_▫_)(inv) InverseOperatorₗ.proof (inverse-propertyₗ-by-groupₗ {id = id}{inv = inv}) {x} {y} = inv(x) ▫ (x ▫ y) 🝖-[ associativity(_▫_) ]-sym (inv(x) ▫ x) ▫ y 🝖-[ congruence₂ₗ(_▫_)(y) (inverseFunctionₗ(_▫_)(inv)) ] id ▫ y 🝖-[ identityₗ(_▫_)(id) ] y 🝖-end inverse-propertyᵣ-by-groupᵣ : let _ = op , assoc , select-invᵣ(id)(identᵣ)(inv)(inverᵣ) in InversePropertyᵣ(_▫_)(inv) InverseOperatorᵣ.proof (inverse-propertyᵣ-by-groupᵣ {id = id}{inv = inv}) {x} {y} = (x ▫ y) ▫ inv(y) 🝖-[ associativity(_▫_) ] x ▫ (y ▫ inv(y)) 🝖-[ congruence₂ᵣ(_▫_)(x) (inverseFunctionᵣ(_▫_)(inv)) ] x ▫ id 🝖-[ identityᵣ(_▫_)(id) ] x 🝖-end standard-inverse-operatorₗ-by-involuting-inverse-propₗ : let _ = op , select-invol(inv)(invol) , select-invPropₗ(inv)(inverPropₗ) in InverseOperatorₗ(x ↦ y ↦ inv(x) ▫ y)(_▫_) InverseOperatorₗ.proof (standard-inverse-operatorₗ-by-involuting-inverse-propₗ {inv = inv}) {x} {y} = x ▫ (inv x ▫ y) 🝖-[ congruence₂ₗ(_▫_)((inv x ▫ y)) (involution(inv)) ]-sym inv(inv(x)) ▫ (inv x ▫ y) 🝖-[ inversePropₗ(_▫_)(inv) ] y 🝖-end standard-inverse-inverse-operatorₗ-by-inverse-propₗ : let _ = select-invPropₗ(inv)(inverPropₗ) in InverseOperatorₗ(_▫_)(x ↦ y ↦ inv(x) ▫ y) InverseOperatorₗ.proof (standard-inverse-inverse-operatorₗ-by-inverse-propₗ {inv = inv}) {x} {y} = inversePropₗ(_▫_)(inv) standard-inverse-operatorᵣ-by-involuting-inverse-propᵣ : let _ = op , select-invol(inv)(invol) , select-invPropᵣ(inv)(inverPropᵣ) in InverseOperatorᵣ(x ↦ y ↦ x ▫ inv(y))(_▫_) InverseOperatorᵣ.proof (standard-inverse-operatorᵣ-by-involuting-inverse-propᵣ {inv = inv}) {x} {y} = (x ▫ inv y) ▫ y 🝖-[ congruence₂ᵣ(_▫_)((x ▫ inv y)) (involution(inv)) ]-sym (x ▫ inv y) ▫ inv(inv(y)) 🝖-[ inversePropᵣ(_▫_)(inv) ] x 🝖-end standard-inverse-inverse-operatorᵣ-by-inverse-propᵣ : let _ = select-invPropᵣ(inv)(inverPropᵣ) in InverseOperatorᵣ(_▫_)(x ↦ y ↦ x ▫ inv(y)) InverseOperatorᵣ.proof (standard-inverse-inverse-operatorᵣ-by-inverse-propᵣ {inv = inv}) {x} {y} = inversePropᵣ(_▫_)(inv) inverseᵣ-by-assoc-inv-propᵣ : let _ = op , assoc , select-idₗ(id)(identₗ) , select-invPropᵣ(inv)(inverPropᵣ) in InverseFunctionᵣ(_▫_) ⦃ [∃]-intro(id) ⦃ identᵣ ⦄ ⦄ (inv) InverseFunctionᵣ.proof (inverseᵣ-by-assoc-inv-propᵣ {id = id} {inv = inv}) {x} = x ▫ inv x 🝖-[ identityₗ(_▫_)(id) ]-sym id ▫ (x ▫ inv x) 🝖-[ associativity(_▫_) ]-sym (id ▫ x) ▫ inv x 🝖-[ inversePropᵣ(_▫_)(inv) ] id 🝖-end zero-when-redundant-addition : let _ = select-idₗ(id)(identₗ) , cancᵣ in ∀{x} → (x ≡ x ▫ x) → (x ≡ id) zero-when-redundant-addition {id = id} {x} p = cancellationᵣ(_▫_) $ symmetry(_≡_) $ id ▫ x 🝖-[ identityₗ(_▫_)(id) ] x 🝖-[ p ] x ▫ x 🝖-end module OneTypeTwoOp {ℓ ℓₑ} {T : Type{ℓ}} ⦃ equiv : Equiv{ℓₑ}(T) ⦄ {_▫₁_ _▫₂_ : T → T → T} where open Lang.Vars.Structure.Operator.OneTypeTwoOp ⦃ equiv = equiv ⦄ {_▫₁_ = _▫₁_} {_▫₂_ = _▫₂_} absorptionₗ-by-abs-com-dist-id : let _ = op₁ , op₂ , distriₗ , select-absₗ(id)(absorbₗ₂) , select-idᵣ(id)(identᵣ₁) in Absorptionₗ(_▫₂_)(_▫₁_) Absorptionₗ.proof (absorptionₗ-by-abs-com-dist-id {id = id}) {x}{y} = x ▫₂ (x ▫₁ y) 🝖-[ congruence₂ₗ(_▫₂_)(_) (identityᵣ(_▫₁_)(id)) ]-sym (x ▫₁ id) ▫₂ (x ▫₁ y) 🝖-[ distributivityₗ(_▫₁_)(_▫₂_) ]-sym x ▫₁ (id ▫₂ y) 🝖-[ congruence₂ᵣ(_▫₁_)(_) (absorberₗ(_▫₂_)(id)) ] x ▫₁ id 🝖-[ identityᵣ(_▫₁_)(id) ] x 🝖-end absorptionᵣ-by-abs-com-dist-id : let _ = op₁ , op₂ , distriᵣ , select-absᵣ(id)(absorbᵣ₂) , select-idₗ(id)(identₗ₁) in Absorptionᵣ(_▫₂_)(_▫₁_) Absorptionᵣ.proof (absorptionᵣ-by-abs-com-dist-id {id = id}) {x}{y} = (x ▫₁ y) ▫₂ y 🝖-[ congruence₂ᵣ(_▫₂_)(_) (identityₗ(_▫₁_)(id)) ]-sym (x ▫₁ y) ▫₂ (id ▫₁ y) 🝖-[ distributivityᵣ(_▫₁_)(_▫₂_) ]-sym (x ▫₂ id) ▫₁ y 🝖-[ congruence₂ₗ(_▫₁_)(_) (absorberᵣ(_▫₂_)(id)) ] id ▫₁ y 🝖-[ identityₗ(_▫₁_)(id) ] y 🝖-end distributivity-equivalence-by-commutativity : let _ = op₂ , comm₁ in Distributivityₗ(_▫₁_)(_▫₂_) ↔ Distributivityᵣ(_▫₁_)(_▫₂_) distributivity-equivalence-by-commutativity = [↔]-intro l r where l : Distributivityₗ(_▫₁_)(_▫₂_) ← Distributivityᵣ(_▫₁_)(_▫₂_) Distributivityₗ.proof (l distriᵣ) {x}{y}{z} = x ▫₁ (y ▫₂ z) 🝖-[ commutativity(_▫₁_) ] (y ▫₂ z) ▫₁ x 🝖-[ distributivityᵣ(_▫₁_)(_▫₂_) ⦃ distriᵣ ⦄ ] (y ▫₁ x) ▫₂ (z ▫₁ x) 🝖-[ congruence₂ₗ(_▫₂_)(_) (commutativity(_▫₁_)) ] (x ▫₁ y) ▫₂ (z ▫₁ x) 🝖-[ congruence₂ᵣ(_▫₂_)(_) (commutativity(_▫₁_)) ] (x ▫₁ y) ▫₂ (x ▫₁ z) 🝖-end r : Distributivityₗ(_▫₁_)(_▫₂_) → Distributivityᵣ(_▫₁_)(_▫₂_) Distributivityᵣ.proof (r distriₗ) {x}{y}{z} = (x ▫₂ y) ▫₁ z 🝖-[ commutativity(_▫₁_) ] z ▫₁ (x ▫₂ y) 🝖-[ distributivityₗ(_▫₁_)(_▫₂_) ⦃ distriₗ ⦄ ] (z ▫₁ x) ▫₂ (z ▫₁ y) 🝖-[ congruence₂ₗ(_▫₂_)(_) (commutativity(_▫₁_)) ] (x ▫₁ z) ▫₂ (z ▫₁ y) 🝖-[ congruence₂ᵣ(_▫₂_)(_) (commutativity(_▫₁_)) ] (x ▫₁ z) ▫₂ (y ▫₁ z) 🝖-end absorption-equivalence-by-commutativity : let _ = op₁ , comm₁ , comm₂ in Absorptionₗ(_▫₁_)(_▫₂_) ↔ Absorptionᵣ(_▫₁_)(_▫₂_) absorption-equivalence-by-commutativity = [↔]-intro l r where r : Absorptionₗ(_▫₁_)(_▫₂_) → Absorptionᵣ(_▫₁_)(_▫₂_) Absorptionᵣ.proof (r absorpₗ) {x}{y} = (x ▫₂ y) ▫₁ y 🝖-[ commutativity(_▫₁_) ] y ▫₁ (x ▫₂ y) 🝖-[ congruence₂ᵣ(_▫₁_)(_) (commutativity(_▫₂_)) ] y ▫₁ (y ▫₂ x) 🝖-[ absorptionₗ(_▫₁_)(_▫₂_) ⦃ absorpₗ ⦄ {y}{x} ] y 🝖-end l : Absorptionₗ(_▫₁_)(_▫₂_) ← Absorptionᵣ(_▫₁_)(_▫₂_) Absorptionₗ.proof (l absorpᵣ) {x}{y} = x ▫₁ (x ▫₂ y) 🝖-[ commutativity(_▫₁_) ] (x ▫₂ y) ▫₁ x 🝖-[ congruence₂ₗ(_▫₁_)(_) (commutativity(_▫₂_)) ] (y ▫₂ x) ▫₁ x 🝖-[ absorptionᵣ(_▫₁_)(_▫₂_) ⦃ absorpᵣ ⦄ {y}{x} ] x 🝖-end absorberₗ-by-absorptionₗ-identityₗ : let _ = absorpₗ , select-idₗ(id)(identₗ₁) in Absorberₗ(_▫₂_)(id) Absorberₗ.proof (absorberₗ-by-absorptionₗ-identityₗ {id}) {x} = id ▫₂ x 🝖-[ identityₗ(_▫₁_)(id) ]-sym id ▫₁ (id ▫₂ x) 🝖-[ absorptionₗ(_▫₁_)(_▫₂_) ] id 🝖-end absorberᵣ-by-absorptionᵣ-identityᵣ : let _ = absorpᵣ , select-idᵣ(id)(identᵣ₁) in Absorberᵣ(_▫₂_)(id) Absorberᵣ.proof (absorberᵣ-by-absorptionᵣ-identityᵣ {id}) {x} = x ▫₂ id 🝖-[ identityᵣ(_▫₁_)(id) ]-sym (x ▫₂ id) ▫₁ id 🝖-[ absorptionᵣ(_▫₁_)(_▫₂_) ] id 🝖-end module Two {ℓ₁ ℓ₂ ℓₑ₁ ℓₑ₂} {A : Type{ℓ₁}} ⦃ equiv-A : Equiv{ℓₑ₁}(A) ⦄ {_▫₁_ : A → A → A} {B : Type{ℓ₂}} ⦃ equiv-B : Equiv{ℓₑ₂}(B) ⦄ {_▫₂_ : B → B → B} where open Lang.Vars.Structure.Operator.Two ⦃ equiv-A = equiv-A ⦄ {_▫₁_ = _▫₁_} ⦃ equiv-B = equiv-B ⦄ {_▫₂_ = _▫₂_} module _ {θ : A → B} ⦃ func : Function ⦃ equiv-A ⦄ ⦃ equiv-B ⦄ (θ) ⦄ ⦃ preserv : Preserving₂ ⦃ equiv-B ⦄ (θ)(_▫₁_)(_▫₂_) ⦄ where preserving-identityₗ : let _ = cancᵣ₂ , select-idₗ(id₁)(identₗ₁) , select-idₗ(id₂)(identₗ₂) in (θ(id₁) ≡ id₂) preserving-identityₗ {id₁}{id₂} = cancellationᵣ(_▫₂_) $ θ(id₁) ▫₂ θ(id₁) 🝖-[ preserving₂(θ)(_▫₁_)(_▫₂_) ]-sym θ(id₁ ▫₁ id₁) 🝖-[ congruence₁(θ) (identityₗ(_▫₁_)(id₁)) ] θ(id₁) 🝖-[ identityₗ(_▫₂_)(id₂) ]-sym id₂ ▫₂ θ(id₁) 🝖-end preserving-inverseₗ : let _ = cancᵣ₂ , select-invₗ(id₁)(identₗ₁)(inv₁)(inverₗ₁) , select-invₗ(id₂)(identₗ₂)(inv₂)(inverₗ₂) in ∀{x} → (θ(inv₁(x)) ≡ inv₂(θ(x))) preserving-inverseₗ {id₁}{inv₁}{id₂}{inv₂} {x} = cancellationᵣ(_▫₂_) $ θ(inv₁ x) ▫₂ θ(x) 🝖-[ preserving₂(θ)(_▫₁_)(_▫₂_) ]-sym θ(inv₁ x ▫₁ x) 🝖-[ congruence₁(θ) (inverseFunctionₗ(_▫₁_)(inv₁)) ] θ(id₁) 🝖-[ preserving-identityₗ ] id₂ 🝖-[ inverseFunctionₗ(_▫₂_)(inv₂) ]-sym inv₂(θ(x)) ▫₂ θ(x) 🝖-end preserving-identityᵣ : let _ = cancₗ₂ , select-idᵣ(id₁)(identᵣ₁) , select-idᵣ(id₂)(identᵣ₂) in (θ(id₁) ≡ id₂) preserving-identityᵣ {id₁}{id₂} = cancellationₗ(_▫₂_) $ θ(id₁) ▫₂ θ(id₁) 🝖-[ preserving₂(θ)(_▫₁_)(_▫₂_) ]-sym θ(id₁ ▫₁ id₁) 🝖-[ congruence₁(θ) (identityᵣ(_▫₁_)(id₁)) ] θ(id₁) 🝖-[ identityᵣ(_▫₂_)(id₂) ]-sym θ(id₁) ▫₂ id₂ 🝖-end preserving-inverseᵣ : let _ = cancₗ₂ , select-invᵣ(id₁)(identᵣ₁)(inv₁)(inverᵣ₁) , select-invᵣ(id₂)(identᵣ₂)(inv₂)(inverᵣ₂) in ∀{x} → (θ(inv₁(x)) ≡ inv₂(θ(x))) preserving-inverseᵣ {id₁}{inv₁}{id₂}{inv₂} {x} = cancellationₗ(_▫₂_) $ θ(x) ▫₂ θ(inv₁(x)) 🝖-[ preserving₂(θ)(_▫₁_)(_▫₂_) ]-sym θ(x ▫₁ inv₁(x)) 🝖-[ congruence₁(θ) (inverseFunctionᵣ(_▫₁_)(inv₁)) ] θ(id₁) 🝖-[ preserving-identityᵣ ] id₂ 🝖-[ inverseFunctionᵣ(_▫₂_)(inv₂) ]-sym θ(x) ▫₂ inv₂(θ(x)) 🝖-end injective-kernel : let _ = op₁ , op₂ , assoc₁ , assoc₂ , cancₗ₂ , select-inv(id₁)(ident₁)(inv₁)(inver₁) , select-inv(id₂)(ident₂)(inv₂)(inver₂) in Injective(θ) ↔ (∀{a} → (θ(a) ≡ id₂) → (a ≡ id₁)) injective-kernel {id₁}{inv₁}{id₂}{inv₂} = [↔]-intro l (\inj → r ⦃ inj ⦄) where l : Injective(θ) ← (∀{a} → (θ(a) ≡ id₂) → (a ≡ id₁)) Injective.proof(l(proof)) {a}{b} (θa≡θb) = One.equality-zeroₗ( proof( θ (a ▫₁ inv₁(b)) 🝖-[ preserving₂(θ)(_▫₁_)(_▫₂_) ] θ(a) ▫₂ θ(inv₁(b)) 🝖-[ congruence₂ᵣ(_▫₂_)(θ(a)) preserving-inverseᵣ ] θ(a) ▫₂ inv₂(θ(b)) 🝖-[ One.equality-zeroᵣ(θa≡θb) ] id₂ 🝖-end ) :of: (a ▫₁ inv₁(b) ≡ id₁) ) :of: (a ≡ b) r : ⦃ _ : Injective(θ) ⦄ → (∀{a} → (θ(a) ≡ id₂) → (a ≡ id₁)) r {a} (θa≡id) = injective(θ) $ θ(a) 🝖-[ θa≡id ] id₂ 🝖-[ preserving-identityᵣ ]-sym θ(id₁) 🝖-end
{-# OPTIONS --cubical --no-import-sorts --safe #-} module Cubical.Data.Unit.Properties where open import Cubical.Core.Everything open import Cubical.Foundations.Prelude open import Cubical.Foundations.HLevels open import Cubical.Data.Nat open import Cubical.Data.Unit.Base open import Cubical.Data.Prod.Base open import Cubical.Foundations.Isomorphism open import Cubical.Foundations.Equiv isContr⊤ : isContr ⊤ isContr⊤ = tt , λ {tt → refl} isProp⊤ : isProp ⊤ isProp⊤ _ _ i = tt -- definitionally equal to: isContr→isProp isContrUnit isSet⊤ : isSet ⊤ isSet⊤ = isProp→isSet isProp⊤ isOfHLevel⊤ : (n : HLevel) → isOfHLevel n ⊤ isOfHLevel⊤ n = isContr→isOfHLevel n isContr⊤ diagonal-unit : ⊤ ≡ ⊤ × ⊤ diagonal-unit = isoToPath (iso (λ x → tt , tt) (λ x → tt) (λ {(tt , tt) i → tt , tt}) λ {tt i → tt}) fibId : ∀ {ℓ} (A : Type ℓ) → (fiber (λ (x : A) → tt) tt) ≡ A fibId A = isoToPath (iso fst (λ a → a , refl) (λ _ → refl) (λ a i → fst a , isOfHLevelSuc 1 isProp⊤ _ _ (snd a) refl i))
{-# OPTIONS --cubical #-} open import Agda.Builtin.Cubical.Path open import Agda.Primitive open import Agda.Primitive.Cubical open import Agda.Builtin.Bool variable a p : Level A : Set a P : A → Set p x y : A refl : x ≡ x refl {x = x} = λ _ → x data 𝕊¹ : Set where base : 𝕊¹ loop : base ≡ base g : Bool → I → 𝕊¹ g true i = base g false i = loop i _ : g false i0 ≡ base _ = refl _ : g true i0 ≡ base _ = refl -- non-unique solutions, should not solve either of the metas. _ : g _ _ ≡ base _ = refl h : Bool → base ≡ base h true = \ _ → base h false = loop _ : h false i0 ≡ base _ = refl _ : h true i0 ≡ base _ = refl -- non-unique solutions, should not solve either of the metas. _ : h _ _ ≡ base _ = refl
{-# OPTIONS --cubical --safe #-} open import Agda.Builtin.Cubical.Path data Circle1 : Set where base1 : Circle1 loop : base1 ≡ base1 data Circle2 : Set where base2 : Circle2 loop : base2 ≡ base2 test : base2 ≡ base2 test = loop
{-# OPTIONS --without-K #-} {- This file lists some basic facts about equivalences that have to be put in a separate file due to dependency. -} open import Types open import Functions open import Paths open import HLevel open import Equivalences open import Univalence open import HLevelBis module EquivalenceLemmas where equiv-eq : ∀ {i} {A B : Set i} {f g : A ≃ B} → π₁ f ≡ π₁ g → f ≡ g equiv-eq p = Σ-eq p $ prop-has-all-paths (is-equiv-is-prop _) _ _
{-# OPTIONS --copatterns --allow-unsolved-metas #-} module Issue942 where record Sigma (A : Set)(P : A → Set) : Set where constructor _,_ field fst : A snd : P fst open Sigma postulate A : Set x : A P Q : A → Set Px : P x f : ∀ {x} → P x → Q x ex : Sigma A Q ex = record { fst = x ; snd = f {!!} -- goal: P x } ex' : Sigma A Q ex' = x , f {!!} -- goal: P x ex'' : Sigma A Q fst ex'' = x snd ex'' = f {!!} -- goal: P (fst ex'') -- should termination check
open import Relation.Binary.Core module InsertSort.Impl2 {A : Set} (_≤_ : A → A → Set) (tot≤ : Total _≤_) where open import Bound.Lower A open import Bound.Lower.Order _≤_ open import Data.List open import Data.Sum open import OList _≤_ insert : {b : Bound}{x : A} → LeB b (val x) → OList b → OList b insert {b} {x} b≤x onil = :< b≤x onil insert {b} {x} b≤x (:< {x = y} b≤y ys) with tot≤ x y ... | inj₁ x≤y = :< b≤x (:< (lexy x≤y) ys) ... | inj₂ y≤x = :< b≤y (insert (lexy y≤x) ys) insertSort : List A → OList bot insertSort [] = onil insertSort (x ∷ xs) = insert {bot} {x} lebx (insertSort xs)
------------------------------------------------------------------------ -- Inductive axiomatisation of subtyping ------------------------------------------------------------------------ module RecursiveTypes.Subtyping.Axiomatic.Inductive where open import Codata.Musical.Notation open import Data.Nat using (ℕ; zero; suc) open import Data.List using (List; []; _∷_; _++_) open import Data.List.Relation.Unary.Any as Any using (Any) open import Data.List.Relation.Unary.Any.Properties open import Data.List.Relation.Unary.All as All using (All; []; _∷_) open import Data.List.Membership.Propositional using (_∈_) open import Data.Product open import Data.Sum as Sum using (_⊎_; inj₁; inj₂; [_,_]′) open import Function.Base open import Function.Equality using (_⟨$⟩_) open import Function.Inverse using (module Inverse) open import Relation.Nullary using (¬_; Dec; yes; no) open import Relation.Binary.PropositionalEquality as PropEq using (_≡_; refl) open import RecursiveTypes.Syntax open import RecursiveTypes.Syntax.UnfoldedOrFixpoint open import RecursiveTypes.Substitution using (unfold[μ_⟶_]) import RecursiveTypes.Subterm as ST import RecursiveTypes.Subterm.RestrictedHypothesis as Restricted open import RecursiveTypes.Subtyping.Semantic.Coinductive as Sem using (_≤Coind_) open import RecursiveTypes.Subtyping.Axiomatic.Coinductive as Ax using (_≤_; ⊥; ⊤; _⟶_; unfold; fold; _∎; _≤⟨_⟩_) ------------------------------------------------------------------------ -- Definition infixr 10 _⟶_ infix 4 _⊢_≤_ infix 3 _∎ infixr 2 _≤⟨_⟩_ -- This inductive subtyping relation is parameterised on a list of -- hypotheses. Note Brandt and Henglein's unusual definition of _⟶_. data _⊢_≤_ {n} (A : List (Hyp n)) : Ty n → Ty n → Set where -- Structural rules. ⊥ : ∀ {τ} → A ⊢ ⊥ ≤ τ ⊤ : ∀ {σ} → A ⊢ σ ≤ ⊤ _⟶_ : ∀ {σ₁ σ₂ τ₁ τ₂} → let H = σ₁ ⟶ σ₂ ≲ τ₁ ⟶ τ₂ in (τ₁≤σ₁ : H ∷ A ⊢ τ₁ ≤ σ₁) (σ₂≤τ₂ : H ∷ A ⊢ σ₂ ≤ τ₂) → A ⊢ σ₁ ⟶ σ₂ ≤ τ₁ ⟶ τ₂ -- Rules for folding and unfolding μ. unfold : ∀ {τ₁ τ₂} → A ⊢ μ τ₁ ⟶ τ₂ ≤ unfold[μ τ₁ ⟶ τ₂ ] fold : ∀ {τ₁ τ₂} → A ⊢ unfold[μ τ₁ ⟶ τ₂ ] ≤ μ τ₁ ⟶ τ₂ -- Reflexivity. _∎ : ∀ τ → A ⊢ τ ≤ τ -- Transitivity. _≤⟨_⟩_ : ∀ τ₁ {τ₂ τ₃} (τ₁≤τ₂ : A ⊢ τ₁ ≤ τ₂) (τ₂≤τ₃ : A ⊢ τ₂ ≤ τ₃) → A ⊢ τ₁ ≤ τ₃ -- Hypothesis. hyp : ∀ {σ τ} (σ≤τ : (σ ≲ τ) ∈ A) → A ⊢ σ ≤ τ ------------------------------------------------------------------------ -- Soundness -- A hypothesis is valid if there is a corresponding proof. Valid : ∀ {n} → (Ty n → Ty n → Set) → Hyp n → Set Valid _≤_ (σ₁ ≲ σ₂) = σ₁ ≤ σ₂ module Soundness where -- The soundness proof uses my trick to show that the code is -- productive. infixr 10 _⟶_ infix 4 _≤P_ _≤W_ infixr 2 _≤⟨_⟩_ mutual -- Soundness proof programs. data _≤P_ {n} : Ty n → Ty n → Set where sound : ∀ {A σ τ} → (valid : All (Valid _≤W_) A) (σ≤τ : A ⊢ σ ≤ τ) → σ ≤P τ -- Weak head normal forms of soundness proof programs. Note that -- _⟶_ takes (suspended) /programs/ as arguments, while _≤⟨_⟩_ -- takes /WHNFs/. data _≤W_ {n} : Ty n → Ty n → Set where done : ∀ {σ τ} (σ≤τ : σ ≤ τ) → σ ≤W τ _⟶_ : ∀ {σ₁ σ₂ τ₁ τ₂} (τ₁≤σ₁ : ∞ (τ₁ ≤P σ₁)) (σ₂≤τ₂ : ∞ (σ₂ ≤P τ₂)) → σ₁ ⟶ σ₂ ≤W τ₁ ⟶ τ₂ _≤⟨_⟩_ : ∀ τ₁ {τ₂ τ₃} (τ₁≤τ₂ : τ₁ ≤W τ₂) (τ₂≤τ₃ : τ₂ ≤W τ₃) → τ₁ ≤W τ₃ -- The following two functions compute the WHNF of a soundness -- program. Note the circular, but productive, definition of proof -- below. soundW : ∀ {n} {σ τ : Ty n} {A} → All (Valid _≤W_) A → A ⊢ σ ≤ τ → σ ≤W τ soundW valid ⊥ = done ⊥ soundW valid ⊤ = done ⊤ soundW valid unfold = done unfold soundW valid fold = done fold soundW valid (τ ∎) = done (τ ∎) soundW valid (τ₁ ≤⟨ τ₁≤τ₂ ⟩ τ₂≤τ₃) = τ₁ ≤⟨ soundW valid τ₁≤τ₂ ⟩ soundW valid τ₂≤τ₃ soundW valid (hyp σ≤τ) = All.lookup valid σ≤τ soundW valid (τ₁≤σ₁ ⟶ σ₂≤τ₂) = proof where proof : _ ≤W _ proof = ♯ sound (proof ∷ valid) τ₁≤σ₁ ⟶ ♯ sound (proof ∷ valid) σ₂≤τ₂ whnf : ∀ {n} {σ τ : Ty n} → σ ≤P τ → σ ≤W τ whnf (sound valid σ≤τ) = soundW valid σ≤τ -- Computes actual proofs. mutual ⟦_⟧W : ∀ {n} {σ τ : Ty n} → σ ≤W τ → σ ≤ τ ⟦ done σ≤τ ⟧W = σ≤τ ⟦ τ₁≤σ₁ ⟶ σ₂≤τ₂ ⟧W = ♯ ⟦ ♭ τ₁≤σ₁ ⟧P ⟶ ♯ ⟦ ♭ σ₂≤τ₂ ⟧P ⟦ τ₁ ≤⟨ τ₁≤τ₂ ⟩ τ₂≤τ₃ ⟧W = τ₁ ≤⟨ ⟦ τ₁≤τ₂ ⟧W ⟩ ⟦ τ₂≤τ₃ ⟧W ⟦_⟧P : ∀ {n} {σ τ : Ty n} → σ ≤P τ → σ ≤ τ ⟦ σ≤τ ⟧P = ⟦ whnf σ≤τ ⟧W -- The subtyping relation defined above is sound with respect to the -- others. sound : ∀ {n A} {σ τ : Ty n} → All (Valid _≤_) A → A ⊢ σ ≤ τ → σ ≤ τ sound {n} valid σ≤τ = ⟦ S.sound (All.map (λ {h} → done′ h) valid) σ≤τ ⟧P where open module S = Soundness done′ : (σ₁≲σ₂ : Hyp n) → Valid _≤_ σ₁≲σ₂ → Valid _≤W_ σ₁≲σ₂ done′ (_ ≲ _) = done ------------------------------------------------------------------------ -- The subtyping relation is decidable module Decidable {n} (χ₁ χ₂ : Ty n) where open Restricted χ₁ χ₂ -- The proof below is not optimised for speed. (See Gapeyev, Levin -- and Pierce's "Recursive Subtyping Revealed" from ICFP '00 for -- more information about the complexity of subtyping algorithms.) mutual -- A wrapper which applies the U∨Μ view. _⊢_,_≤?_,_ : ∀ {A ℓ} → A ⊕ ℓ → ∀ σ → Subterm σ → ∀ τ → Subterm τ → ⟨ A ⟩⋆ ⊢ σ ≤ τ ⊎ (¬ σ ≤Coind τ) T ⊢ σ , σ⊑ ≤? τ , τ⊑ = T ⊩ u∨μ σ , σ⊑ ≤? u∨μ τ , τ⊑ -- _⊩_,_≤?_,_ unfolds fixpoints. Note that at least one U∨Μ -- argument becomes smaller in each recursive call, while ℓ, an -- upper bound on the number of pairs of types yet to be -- inspected, is preserved. _⊩_,_≤?_,_ : ∀ {A ℓ} → A ⊕ ℓ → ∀ {σ τ} → U∨Μ σ → Subterm σ → U∨Μ τ → Subterm τ → ⟨ A ⟩⋆ ⊢ σ ≤ τ ⊎ (¬ σ ≤Coind τ) T ⊩ fixpoint {σ₁} {σ₂} u , σ⊑ ≤? τ , τ⊑ = Sum.map (λ ≤τ → μ σ₁ ⟶ σ₂ ≤⟨ unfold ⟩ unfold[μ σ₁ ⟶ σ₂ ] ≤⟨ ≤τ ⟩ u∨μ⁻¹ τ ∎) (λ ≰τ ≤τ → ≰τ (Sem.trans Sem.fold ≤τ)) (T ⊩ u , anti-mono ST.unfold′ σ⊑ ≤? τ , τ⊑) T ⊩ σ , σ⊑ ≤? fixpoint {τ₁} {τ₂} u , τ⊑ = Sum.map (λ σ≤ → u∨μ⁻¹ σ ≤⟨ σ≤ ⟩ unfold[μ τ₁ ⟶ τ₂ ] ≤⟨ fold ⟩ μ τ₁ ⟶ τ₂ ∎) (λ σ≰ σ≤ → σ≰ (Sem.trans σ≤ Sem.unfold)) (T ⊩ σ , σ⊑ ≤? u , anti-mono ST.unfold′ τ⊑) T ⊩ unfolded σ , σ⊑ ≤? unfolded τ , τ⊑ = T ⊪ σ , σ⊑ ≤? τ , τ⊑ -- _⊪_,_≤?_,_ handles the structural cases. Note that ℓ becomes -- smaller in each recursive call. _⊪_,_≤?_,_ : ∀ {A ℓ} → A ⊕ ℓ → ∀ {σ} → Unfolded σ → Subterm σ → ∀ {τ} → Unfolded τ → Subterm τ → ⟨ A ⟩⋆ ⊢ σ ≤ τ ⊎ (¬ σ ≤Coind τ) T ⊪ ⊥ , _ ≤? τ , _ = inj₁ ⊥ T ⊪ σ , _ ≤? ⊤ , _ = inj₁ ⊤ T ⊪ var x , _ ≤? var y , _ = helper (var x ≡? var y) where helper : ∀ {A x y} → Dec ((Ty n ∋ var x) ≡ var y) → ⟨ A ⟩⋆ ⊢ var x ≤ var y ⊎ ¬ var x ≤Coind var y helper (yes refl) = inj₁ (var _ ∎) helper (no x≠y) = inj₂ (x≠y ∘ Sem.var:≤∞⟶≡) _⊪_,_≤?_,_ {A} T (σ₁ ⟶ σ₂) σ⊑ (τ₁ ⟶ τ₂) τ⊑ = helper (lookupOrInsert T H) where H = (-, σ⊑) ≲ (-, τ⊑) helper₂ : ⟨ H ∷ A ⟩⋆ ⊢ τ₁ ≤ σ₁ ⊎ ¬ τ₁ ≤Coind σ₁ → ⟨ H ∷ A ⟩⋆ ⊢ σ₂ ≤ τ₂ ⊎ ¬ σ₂ ≤Coind τ₂ → ⟨ A ⟩⋆ ⊢ σ₁ ⟶ σ₂ ≤ τ₁ ⟶ τ₂ ⊎ ¬ σ₁ ⟶ σ₂ ≤Coind τ₁ ⟶ τ₂ helper₂ (inj₁ ≤₁) (inj₁ ≤₂) = inj₁ (≤₁ ⟶ ≤₂) helper₂ (inj₂ ≰ ) (_ ) = inj₂ (≰ ∘ Sem.left-proj) helper₂ (_ ) (inj₂ ≰ ) = inj₂ (≰ ∘ Sem.right-proj) helper : ∀ {ℓ} → Any (_≈_ H) A ⊎ (∃ λ n → ℓ ≡ suc n × H ∷ A ⊕ n) → ⟨ A ⟩⋆ ⊢ σ₁ ⟶ σ₂ ≤ τ₁ ⟶ τ₂ ⊎ ¬ σ₁ ⟶ σ₂ ≤Coind τ₁ ⟶ τ₂ helper (inj₁ σ≲τ) = inj₁ $ hyp $ Inverse.to map↔ ⟨$⟩ σ≲τ helper (inj₂ (_ , refl , T′)) = helper₂ (T′ ⊢ τ₁ , anti-mono ST.⟶ˡ′ τ⊑ ≤? σ₁ , anti-mono ST.⟶ˡ′ σ⊑) (T′ ⊢ σ₂ , anti-mono ST.⟶ʳ′ σ⊑ ≤? τ₂ , anti-mono ST.⟶ʳ′ τ⊑) T ⊪ ⊤ , _ ≤? ⊥ , _ = inj₂ (λ ()) T ⊪ ⊤ , _ ≤? var x , _ = inj₂ (λ ()) T ⊪ ⊤ , _ ≤? τ₁ ⟶ τ₂ , _ = inj₂ (λ ()) T ⊪ var x , _ ≤? ⊥ , _ = inj₂ (λ ()) T ⊪ var x , _ ≤? τ₁ ⟶ τ₂ , _ = inj₂ (λ ()) T ⊪ σ₁ ⟶ σ₂ , _ ≤? ⊥ , _ = inj₂ (λ ()) T ⊪ σ₁ ⟶ σ₂ , _ ≤? var x , _ = inj₂ (λ ()) -- Finally we have to prove that an upper bound ℓ actually exists. dec : [] ⊢ χ₁ ≤ χ₂ ⊎ (¬ χ₁ ≤ χ₂) dec = Sum.map id (λ σ≰τ → σ≰τ ∘ Ax.sound) (empty ⊢ χ₁ , inj₁ ST.refl ≤? χ₂ , inj₂ ST.refl) infix 4 []⊢_≤?_ _≤?_ -- The subtyping relation defined above is decidable (when the set of -- assumptions is empty). []⊢_≤?_ : ∀ {n} (σ τ : Ty n) → Dec ([] ⊢ σ ≤ τ) []⊢ σ ≤? τ with Decidable.dec σ τ ... | inj₁ σ≤τ = yes σ≤τ ... | inj₂ σ≰τ = no (σ≰τ ∘ sound []) -- The other subtyping relations can also be decided. _≤?_ : ∀ {n} (σ τ : Ty n) → Dec (σ ≤ τ) σ ≤? τ with Decidable.dec σ τ ... | inj₁ σ≤τ = yes (sound [] σ≤τ) ... | inj₂ σ≰τ = no σ≰τ ------------------------------------------------------------------------ -- Completeness -- Weakening (at the end of the list of assumptions). weaken : ∀ {n A A′} {σ τ : Ty n} → A ⊢ σ ≤ τ → A ++ A′ ⊢ σ ≤ τ weaken ⊥ = ⊥ weaken ⊤ = ⊤ weaken unfold = unfold weaken fold = fold weaken (τ ∎) = τ ∎ weaken (τ₁ ≤⟨ τ₁≤τ₂ ⟩ τ₂≤τ₃) = τ₁ ≤⟨ weaken τ₁≤τ₂ ⟩ weaken τ₂≤τ₃ weaken (hyp h) = hyp (Inverse.to ++↔ ⟨$⟩ inj₁ h) weaken (τ₁≤σ₁ ⟶ σ₂≤τ₂) = weaken τ₁≤σ₁ ⟶ weaken σ₂≤τ₂ -- The subtyping relation defined above is complete with respect to -- the others. complete : ∀ {n A} {σ τ : Ty n} → σ ≤ τ → A ⊢ σ ≤ τ complete {σ = σ} {τ} σ≤τ with Decidable.dec σ τ ... | inj₁ []⊢σ≤τ = weaken []⊢σ≤τ ... | inj₂ σ≰τ with σ≰τ σ≤τ ... | ()
open import Prelude module Implicits.Semantics.Context where open import Implicits.Syntax open import Implicits.Semantics.Type open import SystemF.Everything as F using () open import Data.Vec ⟦_⟧ctx→ : ∀ {ν n} → Ctx ν n → F.Ctx ν n ⟦ Γ ⟧ctx→ = map ⟦_⟧tp→ Γ
{-# OPTIONS --without-K --rewriting #-} open import lib.Basics open import lib.types.Pi open import lib.types.Sigma open import lib.types.CommutingSquare module lib.types.Span where record Span {i j k : ULevel} : Type (lsucc (lmax (lmax i j) k)) where constructor span field A : Type i B : Type j C : Type k f : C → A g : C → B private span=-raw : ∀ {i j k} {A A' : Type i} (p : A == A') {B B' : Type j} (q : B == B') {C C' : Type k} (r : C == C') {f : C → A} {f' : C' → A'} (s : f == f' [ (λ CA → fst CA → snd CA) ↓ pair×= r p ]) {g : C → B} {g' : C' → B'} (t : g == g' [ (λ CB → fst CB → snd CB) ↓ pair×= r q ]) → (span A B C f g) == (span A' B' C' f' g') span=-raw idp idp idp idp idp = idp abstract span= : ∀ {i j k} {A A' : Type i} (p : A ≃ A') {B B' : Type j} (q : B ≃ B') {C C' : Type k} (r : C ≃ C') {f : C → A} {f' : C' → A'} (s : (a : C) → (–> p) (f a) == f' (–> r a)) {g : C → B} {g' : C' → B'} (t : (b : C) → (–> q) (g b) == g' (–> r b)) → (span A B C f g) == (span A' B' C' f' g') span= p q r {f} {f'} s {g} {g'} t = span=-raw (ua p) (ua q) (ua r) (↓-→-in (λ α → ↓-snd×-in (ua r) (ua p) (↓-idf-ua-in p ( s _ ∙ ap f' (↓-idf-ua-out r (↓-fst×-out (ua r) (ua p) α)))))) (↓-→-in (λ β → ↓-snd×-in (ua r) (ua q) (↓-idf-ua-in q ( t _ ∙ ap g' (↓-idf-ua-out r (↓-fst×-out (ua r) (ua q) β)))))) record ⊙Span {i j k : ULevel} : Type (lsucc (lmax (lmax i j) k)) where constructor ⊙span field X : Ptd i Y : Ptd j Z : Ptd k f : Z ⊙→ X g : Z ⊙→ Y ⊙Span-to-Span : ∀ {i j k} → ⊙Span {i} {j} {k} → Span {i} {j} {k} ⊙Span-to-Span (⊙span X Y Z f g) = span (de⊙ X) (de⊙ Y) (de⊙ Z) (fst f) (fst g) {- Helper for path induction on pointed spans -} ⊙span-J : ∀ {i j k l} (P : ⊙Span {i} {j} {k} → Type l) → ({A : Type i} {B : Type j} {Z : Ptd k} (f : de⊙ Z → A) (g : de⊙ Z → B) → P (⊙span ⊙[ A , f (pt Z) ] ⊙[ B , g (pt Z) ] Z (f , idp) (g , idp))) → Π ⊙Span P ⊙span-J P t (⊙span ⊙[ A , ._ ] ⊙[ B , ._ ] Z (f , idp) (g , idp)) = t f g {- Span-flipping functions -} Span-flip : ∀ {i j k} → Span {i} {j} {k} → Span {j} {i} {k} Span-flip (span A B C f g) = span B A C g f ⊙Span-flip : ∀ {i j k} → ⊙Span {i} {j} {k} → ⊙Span {j} {i} {k} ⊙Span-flip (⊙span X Y Z f g) = ⊙span Y X Z g f record SpanMap {i₀ j₀ k₀ i₁ j₁ k₁} (span₀ : Span {i₀} {j₀} {k₀}) (span₁ : Span {i₁} {j₁} {k₁}) : Type (lmax (lmax (lmax i₀ j₀) k₀) (lmax (lmax i₁ j₁) k₁)) where constructor span-map module span₀ = Span span₀ module span₁ = Span span₁ field hA : span₀.A → span₁.A hB : span₀.B → span₁.B hC : span₀.C → span₁.C f-commutes : CommSquare span₀.f span₁.f hC hA g-commutes : CommSquare span₀.g span₁.g hC hB SpanMap-∘ : ∀ {i₀ j₀ k₀ i₁ j₁ k₁ i₂ j₂ k₂} {span₀ : Span {i₀} {j₀} {k₀}} {span₁ : Span {i₁} {j₁} {k₁}} {span₂ : Span {i₂} {j₂} {k₂}} → SpanMap span₁ span₂ → SpanMap span₀ span₁ → SpanMap span₀ span₂ SpanMap-∘ span-map₁₂ span-map₀₁ = record { hA = span-map₁₂.hA ∘ span-map₀₁.hA; hB = span-map₁₂.hB ∘ span-map₀₁.hB; hC = span-map₁₂.hC ∘ span-map₀₁.hC; f-commutes = CommSquare-∘v span-map₁₂.f-commutes span-map₀₁.f-commutes; g-commutes = CommSquare-∘v span-map₁₂.g-commutes span-map₀₁.g-commutes} where module span-map₀₁ = SpanMap span-map₀₁ module span-map₁₂ = SpanMap span-map₁₂ SpanEquiv : ∀ {i₀ j₀ k₀ i₁ j₁ k₁} (span₀ : Span {i₀} {j₀} {k₀}) (span₁ : Span {i₁} {j₁} {k₁}) → Type (lmax (lmax (lmax i₀ j₀) k₀) (lmax (lmax i₁ j₁) k₁)) SpanEquiv span₀ span₁ = Σ (SpanMap span₀ span₁) (λ span-map → is-equiv (SpanMap.hA span-map) × is-equiv (SpanMap.hB span-map) × is-equiv (SpanMap.hC span-map)) SpanEquiv-inverse : ∀ {i₀ j₀ k₀ i₁ j₁ k₁} {span₀ : Span {i₀} {j₀} {k₀}} {span₁ : Span {i₁} {j₁} {k₁}} → SpanEquiv span₀ span₁ → SpanEquiv span₁ span₀ SpanEquiv-inverse (span-map hA hB hC f-commutes g-commutes , hA-ise , hB-ise , hC-ise) = ( span-map hA.g hB.g hC.g (CommSquare-inverse-v f-commutes hC-ise hA-ise) (CommSquare-inverse-v g-commutes hC-ise hB-ise) , ( is-equiv-inverse hA-ise , is-equiv-inverse hB-ise , is-equiv-inverse hC-ise)) where module hA = is-equiv hA-ise module hB = is-equiv hB-ise module hC = is-equiv hC-ise
{-# OPTIONS --without-K --rewriting #-} open import HoTT module homotopy.AnyUniversalCoverIsPathSet {i} (X : Ptd i) where private a₁ = pt X path-set-is-universal : is-universal (path-set-cover X) path-set-is-universal = has-level-in ([ pt X , idp₀ ] , Trunc-elim {P = λ xp₀ → [ pt X , idp₀ ] == xp₀} (λ{(x , p₀) → Trunc-elim {P = λ p₀ → [ pt X , idp₀ ] == [ x , p₀ ]} (λ p → ap [_] $ pair= p (lemma p)) p₀ })) where lemma : ∀ {x} (p : pt X == x) → idp₀ == [ p ] [ (pt X =₀_) ↓ p ] lemma idp = idp module _ {j} (univ-cov : ⊙UniversalCover X j) where private module univ-cov = ⊙UniversalCover univ-cov instance _ = univ-cov.is-univ -- One-to-one mapping between the universal cover -- and the truncated path spaces from one point. [path] : ∀ (a⇑₁ a⇑₂ : univ-cov.TotalSpace) → a⇑₁ =₀ a⇑₂ [path] a⇑₁ a⇑₂ = –> (Trunc=-equiv [ a⇑₁ ] [ a⇑₂ ]) (contr-has-all-paths [ a⇑₁ ] [ a⇑₂ ]) abstract [path]-has-all-paths : ∀ {a⇑₁ a⇑₂ : univ-cov.TotalSpace} → has-all-paths (a⇑₁ =₀ a⇑₂) [path]-has-all-paths {a⇑₁} {a⇑₂} = transport has-all-paths (ua (Trunc=-equiv [ a⇑₁ ] [ a⇑₂ ])) $ contr-has-all-paths {{has-level-apply (raise-level -2 univ-cov.is-univ) [ a⇑₁ ] [ a⇑₂ ]}} to : ∀ {a₂} → univ-cov.Fiber a₂ → a₁ =₀ a₂ to {a₂} a⇑₂ = ap₀ fst ([path] (a₁ , univ-cov.pt) (a₂ , a⇑₂)) from : ∀ {a₂} → a₁ =₀ a₂ → univ-cov.Fiber a₂ from p = cover-trace univ-cov.cov univ-cov.pt p abstract to-from : ∀ {a₂} (p : a₁ =₀ a₂) → to (from p) == p to-from = Trunc-elim (λ p → lemma p) where lemma : ∀ {a₂} (p : a₁ == a₂) → to (from [ p ]) == [ p ] lemma idp = ap₀ fst ([path] (a₁ , univ-cov.pt) (a₁ , univ-cov.pt)) =⟨ ap (ap₀ fst) $ [path]-has-all-paths ([path] (a₁ , univ-cov.pt) (a₁ , univ-cov.pt)) (idp₀ :> ((a₁ , univ-cov.pt) =₀ (a₁ , univ-cov.pt))) ⟩ (idp₀ :> (a₁ =₀ a₁)) ∎ from-to : ∀ {a₂} (a⇑₂ : univ-cov.Fiber a₂) → from (to a⇑₂) == a⇑₂ from-to {a₂} a⇑₂ = Trunc-elim {{λ p → =-preserves-level {x = from (ap₀ fst p)} {y = a⇑₂} univ-cov.Fiber-is-set}} (λ p → to-transp $ snd= p) ([path] (a₁ , univ-cov.pt) (a₂ , a⇑₂)) theorem : ∀ a₂ → univ-cov.Fiber a₂ ≃ (a₁ =₀ a₂) theorem a₂ = to , is-eq _ from to-from from-to
------------------------------------------------------------------------ -- Heterogeneous equality ------------------------------------------------------------------------ module Relation.Binary.HeterogeneousEquality where open import Relation.Nullary open import Relation.Binary open import Relation.Binary.Consequences open import Relation.Binary.PropositionalEquality as PropEq using (_≡_; refl) open import Data.Function open import Data.Product ------------------------------------------------------------------------ -- Heterogeneous equality infix 4 _≅_ _≇_ _≅₁_ _≇₁_ data _≅_ {a : Set} (x : a) : {b : Set} → b → Set where refl : x ≅ x data _≅₁_ {a : Set₁} (x : a) : {b : Set₁} → b → Set where refl : x ≅₁ x -- Nonequality. _≇_ : {a : Set} → a → {b : Set} → b → Set x ≇ y = ¬ x ≅ y _≇₁_ : {a : Set₁} → a → {b : Set₁} → b → Set x ≇₁ y = ¬ x ≅₁ y ------------------------------------------------------------------------ -- Conversion ≡-to-≅ : ∀ {a} {x y : a} → x ≡ y → x ≅ y ≡-to-≅ refl = refl ≅-to-≡ : ∀ {a} {x y : a} → x ≅ y → x ≡ y ≅-to-≡ refl = refl ------------------------------------------------------------------------ -- Some properties reflexive : ∀ {a} → _⇒_ {a} _≡_ (λ x y → x ≅ y) reflexive refl = refl sym : ∀ {a b} {x : a} {y : b} → x ≅ y → y ≅ x sym refl = refl trans : ∀ {a b c} {x : a} {y : b} {z : c} → x ≅ y → y ≅ z → x ≅ z trans refl refl = refl subst : ∀ {a} → Substitutive {a} (λ x y → x ≅ y) subst P refl p = p subst₂ : ∀ {A B} (P : A → B → Set) → ∀ {x₁ x₂ y₁ y₂} → x₁ ≅ x₂ → y₁ ≅ y₂ → P x₁ y₁ → P x₂ y₂ subst₂ P refl refl p = p subst₁ : ∀ {a} (P : a → Set₁) → ∀ {x y} → x ≅ y → P x → P y subst₁ P refl p = p subst-removable : ∀ {a} (P : a → Set) {x y} (eq : x ≅ y) z → subst P eq z ≅ z subst-removable P refl z = refl ≡-subst-removable : ∀ {a} (P : a → Set) {x y} (eq : x ≡ y) z → PropEq.subst P eq z ≅ z ≡-subst-removable P refl z = refl cong : ∀ {A : Set} {B : A → Set} {x y} (f : (x : A) → B x) → x ≅ y → f x ≅ f y cong f refl = refl cong₂ : ∀ {A : Set} {B : A → Set} {C : ∀ x → B x → Set} {x y u v} (f : (x : A) (y : B x) → C x y) → x ≅ y → u ≅ v → f x u ≅ f y v cong₂ f refl refl = refl resp₂ : ∀ {a} (∼ : Rel a) → ∼ Respects₂ (λ x y → x ≅ y) resp₂ _∼_ = subst⟶resp₂ _∼_ subst isEquivalence : ∀ {a} → IsEquivalence {a} (λ x y → x ≅ y) isEquivalence = record { refl = refl ; sym = sym ; trans = trans } setoid : Set → Setoid setoid a = record { carrier = a ; _≈_ = λ x y → x ≅ y ; isEquivalence = isEquivalence } decSetoid : ∀ {a} → Decidable (λ x y → _≅_ {a} x y) → DecSetoid decSetoid dec = record { _≈_ = λ x y → x ≅ y ; isDecEquivalence = record { isEquivalence = isEquivalence ; _≟_ = dec } } isPreorder : ∀ {a} → IsPreorder {a} (λ x y → x ≅ y) (λ x y → x ≅ y) isPreorder = record { isEquivalence = isEquivalence ; reflexive = id ; trans = trans ; ∼-resp-≈ = resp₂ (λ x y → x ≅ y) } isPreorder-≡ : ∀ {a} → IsPreorder {a} _≡_ (λ x y → x ≅ y) isPreorder-≡ = record { isEquivalence = PropEq.isEquivalence ; reflexive = reflexive ; trans = trans ; ∼-resp-≈ = PropEq.resp₂ (λ x y → x ≅ y) } preorder : Set → Preorder preorder a = record { carrier = a ; _≈_ = _≡_ ; _∼_ = λ x y → x ≅ y ; isPreorder = isPreorder-≡ } ------------------------------------------------------------------------ -- The inspect idiom -- See Relation.Binary.PropositionalEquality.Inspect. data Inspect {a : Set} (x : a) : Set where _with-≅_ : (y : a) (eq : y ≅ x) → Inspect x inspect : ∀ {a} (x : a) → Inspect x inspect x = x with-≅ refl ------------------------------------------------------------------------ -- Convenient syntax for equational reasoning module ≅-Reasoning where -- The code in Relation.Binary.EqReasoning cannot handle -- heterogeneous equalities, hence the code duplication here. infix 4 _IsRelatedTo_ infix 2 _∎ infixr 2 _≅⟨_⟩_ _≡⟨_⟩_ infix 1 begin_ data _IsRelatedTo_ {A} (x : A) {B} (y : B) : Set where relTo : (x≅y : x ≅ y) → x IsRelatedTo y begin_ : ∀ {A} {x : A} {B} {y : B} → x IsRelatedTo y → x ≅ y begin relTo x≅y = x≅y _≅⟨_⟩_ : ∀ {A} (x : A) {B} {y : B} {C} {z : C} → x ≅ y → y IsRelatedTo z → x IsRelatedTo z _ ≅⟨ x≅y ⟩ relTo y≅z = relTo (trans x≅y y≅z) _≡⟨_⟩_ : ∀ {A} (x : A) {y} {C} {z : C} → x ≡ y → y IsRelatedTo z → x IsRelatedTo z _ ≡⟨ x≡y ⟩ relTo y≅z = relTo (trans (reflexive x≡y) y≅z) _∎ : ∀ {A} (x : A) → x IsRelatedTo x _∎ _ = relTo refl
{-# OPTIONS --type-in-type #-} -- yes, there will be some cheating in this lecture module Lec3 where open import Lec1Done open import Lec2Done postulate extensionality : {S : Set}{T : S -> Set} {f g : (x : S) -> T x} -> ((x : S) -> f x == g x) -> f == g record Category : Set where field -- two types of thing Obj : Set -- "objects" _~>_ : Obj -> Obj -> Set -- "arrows" or "morphisms" -- or "homomorphisms" -- two operations id~> : {T : Obj} -> T ~> T _>~>_ : {R S T : Obj} -> R ~> S -> S ~> T -> R ~> T -- three laws law-id~>>~> : {S T : Obj} (f : S ~> T) -> (id~> >~> f) == f law->~>id~> : {S T : Obj} (f : S ~> T) -> (f >~> id~>) == f law->~>>~> : {Q R S T : Obj} (f : Q ~> R)(g : R ~> S)(h : S ~> T) -> ((f >~> g) >~> h) == (f >~> (g >~> h)) -- Sets and functions are the classic example of a category. {-(-} SET : Category SET = record { Obj = Set ; _~>_ = \ S T -> S -> T ; id~> = id ; _>~>_ = _>>_ ; law-id~>>~> = \ f -> refl f ; law->~>id~> = \ f -> refl f ; law->~>>~> = \ f g h -> refl (\ x -> h (g (f x))) } {-)-} unique->= : (m n : Nat)(p q : m >= n) -> p == q unique->= m zero p q = refl <> unique->= zero (suc n) () () unique->= (suc m) (suc n) p q = unique->= m n p q -- A PREORDER is a category where there is at most one arrow between -- any two objects. (So arrows are unique.) {-(-} NAT->= : Category NAT->= = record { Obj = Nat ; _~>_ = _>=_ ; id~> = \ {n} -> refl->= n ; _>~>_ = \ {m}{n}{p} m>=n n>=p -> trans->= m n p m>=n n>=p ; law-id~>>~> = \ {S} {T} f -> unique->= S T (trans->= S S T (refl->= S) f) f ; law->~>id~> = \ {S} {T} f -> unique->= S T (trans->= S T T f (refl->= T)) f ; law->~>>~> = \ {Q} {R} {S} {T} f g h -> unique->= Q T _ _ {-(trans->= Q S T (trans->= Q R S f g) h) (trans->= Q R T f (trans->= R S T g h)) -} } where {-)-} -- A MONOID is a category with Obj = One. -- The values in the monoid are the *arrows*. {-(-} ONE-Nat : Category ONE-Nat = record { Obj = One ; _~>_ = \ _ _ -> Nat ; id~> = zero ; _>~>_ = _+N_ ; law-id~>>~> = zero-+N ; law->~>id~> = +N-zero ; law->~>>~> = assocLR-+N } {-)-} {-(-} eqUnique : {X : Set}{x y : X}{p q : x == y} -> p == q eqUnique {p = refl x} {q = refl .x} = refl (refl x) -- A DISCRETE category is one where the only arrows are the identities. DISCRETE : (X : Set) -> Category DISCRETE X = record { Obj = X ; _~>_ = _==_ ; id~> = \ {x} -> refl x ; _>~>_ = \ { (refl x) (refl .x) -> refl x } ; law-id~>>~> = \ {S} {T} f -> eqUnique ; law->~>id~> = \ {S} {T} f -> eqUnique ; law->~>>~> = \ {Q} {R} {S} {T} f g h -> eqUnique } {-)-} module FUNCTOR where open Category record _=>_ (C D : Category) : Set where -- "Functor from C to D" field -- two actions F-Obj : Obj C -> Obj D F-map : {S T : Obj C} -> _~>_ C S T -> _~>_ D (F-Obj S) (F-Obj T) -- two laws F-map-id~> : {T : Obj C} -> F-map (id~> C {T}) == id~> D {F-Obj T} F-map->~> : {R S T : Obj C}(f : _~>_ C R S)(g : _~>_ C S T) -> F-map (_>~>_ C f g) == _>~>_ D (F-map f) (F-map g) open FUNCTOR module FOO (C : Category) where open Category C X : Set X = Obj postulate vmap : {n : Nat}{S T : Set} → (S → T) → Vec S n → Vec T n {-+} VEC : Nat -> SET => SET VEC n = record { F-Obj = \ X -> Vec X n ; F-map = vmap ; F-map-id~> = extensionality {!!} ; F-map->~> = {!!} } {+-} {-(-} VTAKE : Set -> NAT->= => SET VTAKE X = record { F-Obj = Vec X ; F-map = \ {m}{n} m>=n xs -> vTake m n m>=n xs ; F-map-id~> = \ {n} -> extensionality (vTakeIdFact n) ; F-map->~> = \ {m}{n}{p} m>=n n>=p -> extensionality (vTakeCpFact m n p m>=n n>=p) } {-)-} {-(-} ADD : Nat -> NAT->= => NAT->= ADD d = record { F-Obj = (d +N_) -- \ x -> d +N x ; F-map = \ {m}{n} -> help d m n ; F-map-id~> = \ {T} -> unique->= (d +N T) (d +N T) (help d T T (refl->= T)) (refl->= (d +N T)) ; F-map->~> = \ {R} {S} {T} f g -> unique->= (d +N R) (d +N T) (help d R T (trans->= R S T f g)) (trans->= (d +N R) (d +N S) (d +N T) (help d R S f) (help d S T g)) } where help : forall d m n -> m >= n -> (d +N m) >= (d +N n) help zero m n m>=n = m>=n help (suc d) m n m>=n = help d m n m>=n {-)-} {-(-} CATEGORY : Category CATEGORY = record { Obj = Category ; _~>_ = _=>_ ; id~> = record { F-Obj = id ; F-map = id ; F-map-id~> = {!!} ; F-map->~> = {!!} } ; _>~>_ = \ F G -> record { F-Obj = F-Obj F >> F-Obj G ; F-map = F-map F >> F-map G ; F-map-id~> = {!!} ; F-map->~> = {!!} } ; law-id~>>~> = {!!} ; law->~>id~> = {!!} ; law->~>>~> = {!!} } where open _=>_ {-)-}
{- Based on Nicolai Kraus' blog post: The Truncation Map |_| : ℕ -> ‖ℕ‖ is nearly Invertible https://homotopytypetheory.org/2013/10/28/the-truncation-map-_-ℕ-‖ℕ‖-is-nearly-invertible/ Defines [recover], which definitionally satisfies `recover ∣ x ∣ ≡ x` ([recover∣∣]) for homogeneous types Also see the follow-up post by Jason Gross: Composition is not what you think it is! Why “nearly invertible” isn’t. https://homotopytypetheory.org/2014/02/24/composition-is-not-what-you-think-it-is-why-nearly-invertible-isnt/ -} {-# OPTIONS --cubical --safe #-} module Cubical.HITs.PropositionalTruncation.MagicTrick where open import Cubical.Foundations.Prelude open import Cubical.Foundations.Function open import Cubical.Foundations.Path open import Cubical.Foundations.Pointed open import Cubical.Foundations.Pointed.Homogeneous open import Cubical.HITs.PropositionalTruncation.Base open import Cubical.HITs.PropositionalTruncation.Properties module Recover {ℓ} (A∙ : Pointed ℓ) (h : isHomogeneous A∙) where private A = typ A∙ a = pt A∙ toEquivPtd : ∥ A ∥ → Σ[ B∙ ∈ Pointed ℓ ] (A , a) ≡ B∙ toEquivPtd = recPropTrunc (isContr→isProp (_ , λ p → contrSingl (snd p))) (λ x → (A , x) , h x) private B∙ : ∥ A ∥ → Pointed ℓ B∙ tx = fst (toEquivPtd tx) -- the key observation is that B∙ ∣ x ∣ is definitionally equal to (A , x) private obvs : ∀ x → B∙ ∣ x ∣ ≡ (A , x) obvs x = refl -- try it: `C-c C-n B∙ ∣ x ∣` gives `(A , x)` -- thus any truncated element (of a homogeneous type) can be recovered by agda's normalizer! recover : ∀ (tx : ∥ A ∥) → typ (B∙ tx) recover tx = pt (B∙ tx) recover∣∣ : ∀ (x : A) → recover ∣ x ∣ ≡ x recover∣∣ x = refl -- try it: `C-c C-n recover ∣ x ∣` gives `x` private -- notice that the following typechecks because typ (B∙ ∣ x ∣) is definitionally equal to to A, but -- `recover : ∥ A ∥ → A` does not because typ (B∙ tx) is not definitionally equal to A (though it is -- judegmentally equal to A by cong typ (snd (toEquivPtd tx)) : A ≡ typ (B∙ tx)) obvs2 : A → A obvs2 = recover ∘ ∣_∣ -- one might wonder if (cong recover (squash ∣ x ∣ ∣ y ∣)) therefore has type x ≡ y, but thankfully -- typ (B∙ (squash ∣ x ∣ ∣ y ∣ i)) is *not* A (it's a messy hcomp involving h x and h y) recover-squash : ∀ x y → -- x ≡ y -- this raises an error PathP (λ i → typ (B∙ (squash ∣ x ∣ ∣ y ∣ i))) x y recover-squash x y = cong recover (squash ∣ x ∣ ∣ y ∣) -- Demo, adapted from: -- https://bitbucket.org/nicolaikraus/agda/src/e30d70c72c6af8e62b72eefabcc57623dd921f04/trunc-inverse.lagda private open import Cubical.Data.Nat open Recover (ℕ , zero) (isHomogeneousDiscrete discreteℕ) -- only `∣hidden∣` is exported, `hidden` is no longer in scope module _ where private hidden : ℕ hidden = 17 ∣hidden∣ : ∥ ℕ ∥ ∣hidden∣ = ∣ hidden ∣ -- we can still recover the value, even though agda can no longer see `hidden`! test : recover ∣hidden∣ ≡ 17 test = refl -- try it: `C-c C-n recover ∣hidden∣` gives `17` -- `C-c C-n hidden` gives an error -- Finally, note that the definition of recover is independent of the proof that A is homogeneous. Thus we -- still can definitionally recover information hidden by ∣_∣ as long as we permit holes. Try replacing -- `isHomogeneousDiscrete discreteℕ` above with a hole (`?`) and notice that everything still works
open import Oscar.Prelude module Oscar.Data.List where open import Agda.Builtin.List public using () renaming ([] to ∅) renaming (_∷_ to _,_) ⟨_⟩¶ = Agda.Builtin.List.List List⟨_⟩ = ⟨_⟩¶
module Oscar.Class.IsSemigroupoid where open import Oscar.Class.Associativity open import Oscar.Class.Equivalence open import Oscar.Class.Extensionality₂ open import Oscar.Function open import Oscar.Level open import Oscar.Relation record Semigroup {𝔞} {𝔄 : Set 𝔞} {𝔰} {_►_ : 𝔄 → 𝔄 → Set 𝔰} (_◅_ : ∀ {m n} → m ► n → ∀ {l} → m ⟨ l ►_ ⟩→ n) {ℓ} (_≋_ : ∀ {m n} → m ► n → m ► n → Set ℓ) : Set (𝔞 ⊔ 𝔰 ⊔ ℓ) where field ⦃ ′equivalence ⦄ : ∀ {m n} → Equivalence (_≋_ {m} {n}) ⦃ ′associativity ⦄ : Associativity _◅_ _≋_ ⦃ ′extensionality₂ ⦄ : ∀ {l m n} → Extensionality₂ (_≋_ {m} {n}) (_≋_ {l}) (λ ⋆ → _≋_ ⋆) (λ ⋆ → _◅_ ⋆) (λ ⋆ → _◅_ ⋆) open Semigroup ⦃ … ⦄ public hiding (′equivalence; ′associativity) -- instance -- Semigroup⋆ : ∀ -- {𝔞} {𝔄 : Set 𝔞} {𝔰} {_►_ : 𝔄 → 𝔄 → Set 𝔰} -- {_◅_ : ∀ {m n} → m ► n → ∀ {l} → m ⟨ l ►_ ⟩→ n} -- {ℓ} -- {_≋_ : ∀ {m n} → m ► n → m ► n → Set ℓ} -- ⦃ _ : ∀ {m n} → Equivalence (_≋_ {m} {n}) ⦄ -- ⦃ _ : Associativity _◅_ _≋_ ⦄ -- → Semigroup _◅_ _≋_ -- Semigroup.′equivalence Semigroup⋆ = it -- Semigroup.′associativity Semigroup⋆ = it -- -- -- record Semigroup -- -- -- {𝔞} {𝔄 : Set 𝔞} {𝔰} {_►_ : 𝔄 → 𝔄 → Set 𝔰} -- -- -- (_◅_ : ∀ {m n} → m ► n → ∀ {l} → l ► m → l ► n) -- -- -- {𝔱} {▸ : 𝔄 → Set 𝔱} -- -- -- (_◃_ : ∀ {m n} → m ► n → m ⟨ ▸ ⟩→ n) -- -- -- {ℓ} -- -- -- (_≋_ : ∀ {n} → ▸ n → ▸ n → Set ℓ) -- -- -- : Set (𝔞 ⊔ 𝔰 ⊔ 𝔱 ⊔ ℓ) where -- -- -- field -- -- -- ⦃ ′equivalence ⦄ : ∀ {n} → Equivalence (_≋_ {n}) -- -- -- ⦃ ′associativity ⦄ : Associativity _◅_ _◃_ _≋_ -- -- -- open Semigroup ⦃ … ⦄ public hiding (′equivalence; ′associativity) -- -- -- instance -- -- -- Semigroup⋆ : ∀ -- -- -- {𝔞} {𝔄 : Set 𝔞} {𝔰} {_►_ : 𝔄 → 𝔄 → Set 𝔰} -- -- -- {_◅_ : ∀ {m n} → m ► n → ∀ {l} → l ► m → l ► n} -- -- -- {𝔱} {▸ : 𝔄 → Set 𝔱} -- -- -- {_◃_ : ∀ {m n} → m ► n → m ⟨ ▸ ⟩→ n} -- -- -- {ℓ} -- -- -- {_≋_ : ∀ {n} → ▸ n → ▸ n → Set ℓ} -- -- -- ⦃ _ : ∀ {n} → Equivalence (_≋_ {n}) ⦄ -- -- -- ⦃ _ : Associativity _◅_ _◃_ _≋_ ⦄ -- -- -- → Semigroup _◅_ _◃_ _≋_ -- -- -- Semigroup.′equivalence Semigroup⋆ = it -- -- -- Semigroup.′associativity Semigroup⋆ = it
import cedille-options module communication-util (options : cedille-options.options) where open import general-util open import toplevel-state options {IO} logRopeh : filepath → rope → IO ⊤ logRopeh logFilePath r with cedille-options.options.generate-logs options ...| ff = return triv ...| tt = getCurrentTime >>= λ time → withFile logFilePath AppendMode λ hdl → hPutRope hdl ([[ "([" ^ utcToString time ^ "] " ]] ⊹⊹ r ⊹⊹ [[ ")\n" ]]) logRope : toplevel-state → rope → IO ⊤ logRope s = logRopeh (toplevel-state.logFilePath s) logMsg : toplevel-state → (message : string) → IO ⊤ logMsg s msg = logRope s [[ msg ]] logMsg' : filepath → (message : string) → IO ⊤ logMsg' logFilePath msg = logRopeh logFilePath [[ msg ]] sendProgressUpdate : string → IO ⊤ sendProgressUpdate msg = putStr "progress: " >> putStr msg >> putStr "\n" progressUpdate : (filename : string) → {-(do-check : 𝔹) → -} IO ⊤ progressUpdate filename {-do-check-} = if cedille-options.options.show-progress-updates options then sendProgressUpdate ((if {-do-check-} tt then "Checking " else "Skipping ") ^ filename) else return triv
------------------------------------------------------------------------ -- The Agda standard library -- -- A categorical view of Delay ------------------------------------------------------------------------ {-# OPTIONS --without-K --safe --sized-types #-} module Codata.Delay.Categorical where open import Codata.Delay open import Function open import Category.Functor open import Category.Applicative open import Category.Monad open import Data.These using (leftMost) functor : ∀ {i ℓ} → RawFunctor {ℓ} (λ A → Delay A i) functor = record { _<$>_ = λ f → map f } module Sequential where applicative : ∀ {i ℓ} → RawApplicative {ℓ} (λ A → Delay A i) applicative = record { pure = now ; _⊛_ = λ df da → bind df (λ f → map f da) } applicativeZero : ∀ {i ℓ} → RawApplicativeZero {ℓ} (λ A → Delay A i) applicativeZero = record { applicative = applicative ; ∅ = never } monad : ∀ {i ℓ} → RawMonad {ℓ} (λ A → Delay A i) monad = record { return = now ; _>>=_ = bind } monadZero : ∀ {i ℓ} → RawMonadZero {ℓ} (λ A → Delay A i) monadZero = record { monad = monad ; applicativeZero = applicativeZero } module Zippy where applicative : ∀ {i ℓ} → RawApplicative {ℓ} (λ A → Delay A i) applicative = record { pure = now ; _⊛_ = zipWith id } applicativeZero : ∀ {i ℓ} → RawApplicativeZero {ℓ} (λ A → Delay A i) applicativeZero = record { applicative = applicative ; ∅ = never } alternative : ∀ {i ℓ} → RawAlternative {ℓ} (λ A → Delay A i) alternative = record { applicativeZero = applicativeZero ; _∣_ = alignWith leftMost }
open import Data.Product using ( _×_ ; _,_ ) open import Data.Sum using ( _⊎_ ; inj₁ ; inj₂ ) open import FRP.LTL.RSet.Core using ( RSet ) open import FRP.LTL.Time.Bound using ( Time∞ ; fin ; _≼_ ; _≺_ ; ≼-refl ; _≼-trans_ ; _≼-asym_ ; _≼-total_ ; _≺-transˡ_ ; ≺-impl-≼ ; ≡-impl-≼ ; ≡-impl-≽ ; ≺-impl-⋡ ; src ) open import FRP.LTL.Time.Interval using ( Interval ; [_⟩ ; _⊑_ ; _~_ ; _,_ ; lb ; ub ; lb≼ ; ≺ub ; Int ; _⌢_∵_ ; ⊑-impl-≼ ; ⊑-impl-≽ ; lb≺ub ; ⊑-refl ; _⊑-trans_ ; ⌢-inj₁ ; ⌢-inj₂ ; sing ) open import FRP.LTL.Util using ( ≡-relevant ; ⊥-elim ) open import Relation.Binary.PropositionalEquality using ( _≡_ ; refl ) open import Relation.Unary using ( _∈_ ) module FRP.LTL.ISet.Core where infixr 4 _,_ -- Monotone interval types -- An MSet is a pair (A , split , subsum) where -- A : Interval → Set -- if σ : A [ s≼t≼u ⟩ then (split s≼t t≼u σ) : (A [ s≼t ⟩ × A [ t≼u ⟩) -- if i ⊑ j and σ : A j then subsum i⊑j σ : A i -- Note that subsum and split are interderivable, but -- we provide both for efficiency. -- In previous versions of MSet, we also included comp (an inverse of split) -- and id (a unit for comp) but &&& and loop have been rewritten to avoid needing comp. data MSet : Set₁ where _,_ : (A : Interval → Set) → ((∀ i j i~j → A (i ⌢ j ∵ i~j) → (A i × A j)) × (∀ i j → (i ⊑ j) → A j → A i)) → MSet -- The underlying "unboxed" elements of an MSet U⟦_⟧ : MSet → Interval → Set U⟦ A , _ ⟧ = A -- A "boxed" element of an MSet. This is used rather than the unboxed -- representation because it's invertable, and so plays much better -- with the type inference algorithm. data B⟦_⟧ (A : MSet) (i : Interval) : Set where [_] : U⟦ A ⟧ i → B⟦ A ⟧ i unbox : ∀ {A i} → B⟦ A ⟧ i → U⟦ A ⟧ i unbox [ a ] = a splitB⟦_⟧ : ∀ A i j i~j → B⟦ A ⟧ (i ⌢ j ∵ i~j) → (B⟦ A ⟧ i × B⟦ A ⟧ j) splitB⟦ A , split , subsum ⟧ i j i~j [ σ ] with split i j i~j σ ... | (σ₁ , σ₂) = ([ σ₁ ] , [ σ₂ ]) subsumB⟦_⟧ : ∀ A i j → (i ⊑ j) → B⟦ A ⟧ j → B⟦ A ⟧ i subsumB⟦ A , split , subsum ⟧ i j i⊑j [ σ ] = [ subsum i j i⊑j σ ] -- Interval types data ISet : Set₁ where [_] : MSet → ISet _⇛_ : MSet → ISet → ISet -- Non-monotone semantics of ISets I⟦_⟧ : ISet → Interval → Set I⟦ [ A ] ⟧ i = B⟦ A ⟧ i I⟦ A ⇛ B ⟧ i = B⟦ A ⟧ i → I⟦ B ⟧ i -- Monotone semantics of ISets M⟦_⟧ : ISet → Interval → Set M⟦ [ A ] ⟧ i = B⟦ A ⟧ i M⟦ A ⇛ B ⟧ i = ∀ j → (j ⊑ i) → I⟦ A ⇛ B ⟧ j -- User-level semantics ⟦_⟧ : ISet → Set ⟦ A ⟧ = ∀ {i} → I⟦ A ⟧ i -- Translation of ISet into MSet splitM⟦_⟧ : ∀ A i j i~j → M⟦ A ⟧ (i ⌢ j ∵ i~j) → (M⟦ A ⟧ i × M⟦ A ⟧ j) splitM⟦ [ A ] ⟧ i j i~j σ = splitB⟦ A ⟧ i j i~j σ splitM⟦ A ⇛ B ⟧ i j i~j f = (f₁ , f₂) where f₁ : ∀ k → (k ⊑ i) → B⟦ A ⟧ k → I⟦ B ⟧ k f₁ k k⊑i = f k (⊑-impl-≽ k⊑i , ⊑-impl-≼ k⊑i ≼-trans ≡-impl-≼ i~j ≼-trans ≺-impl-≼ (lb≺ub j)) f₂ : ∀ k → (k ⊑ j) → B⟦ A ⟧ k → I⟦ B ⟧ k f₂ k k⊑j = f k (≺-impl-≼ (lb≺ub i) ≼-trans ≡-impl-≼ i~j ≼-trans ⊑-impl-≽ k⊑j , ⊑-impl-≼ k⊑j) subsumM⟦_⟧ : ∀ A i j → (i ⊑ j) → M⟦ A ⟧ j → M⟦ A ⟧ i subsumM⟦ [ A ] ⟧ i j i⊑j σ = subsumB⟦ A ⟧ i j i⊑j σ subsumM⟦ A ⇛ B ⟧ i j i⊑j f = λ h h⊑i → f h (h⊑i ⊑-trans i⊑j) mset : ISet → MSet mset A = ( M⟦ A ⟧ , splitM⟦ A ⟧ , subsumM⟦ A ⟧ ) -- Translations back and forth between M⟦ A ⟧ and I⟦ A ⟧ m→i : ∀ {A i} → M⟦ A ⟧ i → I⟦ A ⟧ i m→i {[ A ]} {i} σ = σ m→i {A ⇛ B} {i} f = f i ⊑-refl i→m : ∀ {A i} → (∀ j → (j ⊑ i) → I⟦ A ⟧ j) → M⟦ A ⟧ i i→m {[ A ]} {i} σ = σ i ⊑-refl i→m {A ⇛ B} {i} f = f -- Embedding of RSet into ISet ⌈_⌉ : RSet → ISet ⌈ A ⌉ = [ (λ i → ∀ t → .(t ∈ Int i) → A t) , split , subsum ] where split : ∀ i j i~j → (∀ t → .(t ∈ Int (i ⌢ j ∵ i~j)) → A t) → ((∀ t → .(t ∈ Int i) → A t) × (∀ t → .(t ∈ Int j) → A t)) split i j i~j σ = ( (λ t t∈i → σ t (lb≼ t∈i , ≺ub t∈i ≺-transˡ ≡-impl-≼ i~j ≼-trans ≺-impl-≼ (lb≺ub j))) , (λ t t∈j → σ t (≺-impl-≼ (lb≺ub i) ≼-trans ≡-impl-≼ i~j ≼-trans lb≼ t∈j , ≺ub t∈j)) ) subsum : ∀ i j → (i ⊑ j) → (∀ t → .(t ∈ Int j) → A t) → (∀ t → .(t ∈ Int i) → A t) subsum i j i⊑j σ t t∈i = σ t (⊑-impl-≽ i⊑j ≼-trans lb≼ t∈i , ≺ub t∈i ≺-transˡ ⊑-impl-≼ i⊑j) -- Embedding of ISet into RSet ⌊_⌋ : ISet → RSet ⌊ A ⌋ t = M⟦ A ⟧ (sing t) -- Embedding of Set into ISet data Always (A : Set) (i : Interval) : Set where const : A → Always A i var : ∀ j → .(i ⊑ j) → (∀ t → .(t ∈ Int j) → A) → Always A i ⟨_⟩ : Set → ISet ⟨ A ⟩ = [ Always A , split , subsum ] where split : ∀ i j i~j → Always A (i ⌢ j ∵ i~j) → (Always A i × Always A j) split i j i~j (const a) = (const a , const a) split i j i~j (var k i⌢j⊑k f) = ( var k (⌢-inj₁ i j i~j ⊑-trans i⌢j⊑k) f , var k (⌢-inj₂ i j i~j ⊑-trans i⌢j⊑k) f ) subsum : ∀ i j → (i ⊑ j) → Always A j → Always A i subsum i j i⊑j (const a) = const a subsum i j i⊑j (var k j⊑k f) = var k (i⊑j ⊑-trans j⊑k) f
{- Byzantine Fault Tolerant Consensus Verification in Agda, version 0.9. Copyright (c) 2021, Oracle and/or its affiliates. Licensed under the Universal Permissive License v 1.0 as shown at https://opensource.oracle.com/licenses/upl -} open import LibraBFT.ImplShared.Consensus.Types open import Optics.All import Util.KVMap as Map open import Util.Prelude module LibraBFT.Impl.Consensus.TestUtils.MockSharedStorage where new : ValidatorSet → MockSharedStorage new = mkMockSharedStorage Map.empty Map.empty Map.empty nothing nothing newObmWithLIWS : ValidatorSet → LedgerInfoWithSignatures → MockSharedStorage newObmWithLIWS vs obmLIWS = new vs & mssLis ∙~ Map.singleton (obmLIWS ^∙ liwsVersion) obmLIWS
------------------------------------------------------------------------ -- The Agda standard library -- -- Pointwise lifting of binary relations to sigma types ------------------------------------------------------------------------ module Relation.Binary.Sigma.Pointwise where open import Data.Product as Prod open import Level open import Function open import Function.Equality as F using (_⟶_; _⟨$⟩_) open import Function.Equivalence as Eq using (Equivalence; _⇔_; module Equivalence) open import Function.Injection as Inj using (Injection; _↣_; module Injection; Injective) open import Function.Inverse as Inv using (Inverse; _↔_; module Inverse) open import Function.LeftInverse as LeftInv using (LeftInverse; _↞_; module LeftInverse; _LeftInverseOf_; _RightInverseOf_) open import Function.Related as Related using (_∼[_]_; lam; app-←; app-↢) open import Function.Surjection as Surj using (Surjection; _↠_; module Surjection) import Relation.Binary as B open import Relation.Binary.Indexed as I using (_at_) import Relation.Binary.HeterogeneousEquality as H import Relation.Binary.PropositionalEquality as P ------------------------------------------------------------------------ -- Pointwise lifting infixr 4 _,_ data REL {a₁ a₂ b₁ b₂ ℓ₁ ℓ₂} {A₁ : Set a₁} (B₁ : A₁ → Set b₁) {A₂ : Set a₂} (B₂ : A₂ → Set b₂) (_R₁_ : B.REL A₁ A₂ ℓ₁) (_R₂_ : I.REL B₁ B₂ ℓ₂) : B.REL (Σ A₁ B₁) (Σ A₂ B₂) (a₁ ⊔ a₂ ⊔ b₁ ⊔ b₂ ⊔ ℓ₁ ⊔ ℓ₂) where _,_ : {x₁ : A₁} {y₁ : B₁ x₁} {x₂ : A₂} {y₂ : B₂ x₂} (x₁Rx₂ : x₁ R₁ x₂) (y₁Ry₂ : y₁ R₂ y₂) → REL B₁ B₂ _R₁_ _R₂_ (x₁ , y₁) (x₂ , y₂) Rel : ∀ {a b ℓ₁ ℓ₂} {A : Set a} (B : A → Set b) (_R₁_ : B.Rel A ℓ₁) (_R₂_ : I.Rel B ℓ₂) → B.Rel (Σ A B) _ Rel B = REL B B ------------------------------------------------------------------------ -- Rel preserves many properties module _ {a b ℓ₁ ℓ₂} {A : Set a} {B : A → Set b} {R₁ : B.Rel A ℓ₁} {R₂ : I.Rel B ℓ₂} where refl : B.Reflexive R₁ → I.Reflexive B R₂ → B.Reflexive (Rel B R₁ R₂) refl refl₁ refl₂ {x = (x , y)} = (refl₁ , refl₂) symmetric : B.Symmetric R₁ → I.Symmetric B R₂ → B.Symmetric (Rel B R₁ R₂) symmetric sym₁ sym₂ (x₁Rx₂ , y₁Ry₂) = (sym₁ x₁Rx₂ , sym₂ y₁Ry₂) transitive : B.Transitive R₁ → I.Transitive B R₂ → B.Transitive (Rel B R₁ R₂) transitive trans₁ trans₂ (x₁Rx₂ , y₁Ry₂) (x₂Rx₃ , y₂Ry₃) = (trans₁ x₁Rx₂ x₂Rx₃ , trans₂ y₁Ry₂ y₂Ry₃) isEquivalence : B.IsEquivalence R₁ → I.IsEquivalence B R₂ → B.IsEquivalence (Rel B R₁ R₂) isEquivalence eq₁ eq₂ = record { refl = refl (B.IsEquivalence.refl eq₁) (I.IsEquivalence.refl eq₂) ; sym = symmetric (B.IsEquivalence.sym eq₁) (I.IsEquivalence.sym eq₂) ; trans = transitive (B.IsEquivalence.trans eq₁) (I.IsEquivalence.trans eq₂) } setoid : ∀ {b₁ b₂ i₁ i₂} → (A : B.Setoid b₁ b₂) → I.Setoid (B.Setoid.Carrier A) i₁ i₂ → B.Setoid _ _ setoid s₁ s₂ = record { isEquivalence = isEquivalence (B.Setoid.isEquivalence s₁) (I.Setoid.isEquivalence s₂) } ------------------------------------------------------------------------ -- The propositional equality setoid over sigma types can be -- decomposed using Rel Rel↔≡ : ∀ {a b} {A : Set a} {B : A → Set b} → Inverse (setoid (P.setoid A) (H.indexedSetoid B)) (P.setoid (Σ A B)) Rel↔≡ {a} {b} {A} {B} = record { to = record { _⟨$⟩_ = id; cong = to-cong } ; from = record { _⟨$⟩_ = id; cong = from-cong } ; inverse-of = record { left-inverse-of = uncurry (λ _ _ → (P.refl , H.refl)) ; right-inverse-of = λ _ → P.refl } } where open I using (_=[_]⇒_) to-cong : Rel B P._≡_ (λ x y → H._≅_ x y) =[ id {a = a ⊔ b} ]⇒ P._≡_ to-cong (P.refl , H.refl) = P.refl from-cong : P._≡_ =[ id {a = a ⊔ b} ]⇒ Rel B P._≡_ (λ x y → H._≅_ x y) from-cong {i = (x , y)} P.refl = (P.refl , H.refl) ------------------------------------------------------------------------ -- Some properties related to "relatedness" ⟶ : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : I.Setoid A₁ b₁ b₁′} (B₂ : I.Setoid A₂ b₂ b₂′) (f : A₁ → A₂) → (∀ {x} → (B₁ at x) ⟶ (B₂ at f x)) → setoid (P.setoid A₁) B₁ ⟶ setoid (P.setoid A₂) B₂ ⟶ {A₁ = A₁} {A₂} {B₁} B₂ f g = record { _⟨$⟩_ = fg ; cong = fg-cong } where open B.Setoid (setoid (P.setoid A₁) B₁) using () renaming (_≈_ to _≈₁_) open B.Setoid (setoid (P.setoid A₂) B₂) using () renaming (_≈_ to _≈₂_) open B using (_=[_]⇒_) fg = Prod.map f (_⟨$⟩_ g) fg-cong : _≈₁_ =[ fg ]⇒ _≈₂_ fg-cong (P.refl , ∼) = (P.refl , F.cong g ∼) equivalence : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : I.Setoid A₁ b₁ b₁′} {B₂ : I.Setoid A₂ b₂ b₂′} (A₁⇔A₂ : A₁ ⇔ A₂) → (∀ {x} → _⟶_ (B₁ at x) (B₂ at (Equivalence.to A₁⇔A₂ ⟨$⟩ x))) → (∀ {y} → _⟶_ (B₂ at y) (B₁ at (Equivalence.from A₁⇔A₂ ⟨$⟩ y))) → Equivalence (setoid (P.setoid A₁) B₁) (setoid (P.setoid A₂) B₂) equivalence {B₁ = B₁} {B₂} A₁⇔A₂ B-to B-from = record { to = ⟶ B₂ (_⟨$⟩_ (to A₁⇔A₂)) B-to ; from = ⟶ B₁ (_⟨$⟩_ (from A₁⇔A₂)) B-from } where open Equivalence private subst-cong : ∀ {i a p} {I : Set i} {A : I → Set a} (P : ∀ {i} → A i → A i → Set p) {i i′} {x y : A i} (i≡i′ : P._≡_ i i′) → P x y → P (P.subst A i≡i′ x) (P.subst A i≡i′ y) subst-cong P P.refl p = p equivalence-↞ : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} (B₁ : I.Setoid A₁ b₁ b₁′) {B₂ : I.Setoid A₂ b₂ b₂′} (A₁↞A₂ : A₁ ↞ A₂) → (∀ {x} → Equivalence (B₁ at (LeftInverse.from A₁↞A₂ ⟨$⟩ x)) (B₂ at x)) → Equivalence (setoid (P.setoid A₁) B₁) (setoid (P.setoid A₂) B₂) equivalence-↞ B₁ {B₂} A₁↞A₂ B₁⇔B₂ = equivalence (LeftInverse.equivalence A₁↞A₂) B-to B-from where B-to : ∀ {x} → _⟶_ (B₁ at x) (B₂ at (LeftInverse.to A₁↞A₂ ⟨$⟩ x)) B-to = record { _⟨$⟩_ = λ x → Equivalence.to B₁⇔B₂ ⟨$⟩ P.subst (I.Setoid.Carrier B₁) (P.sym $ LeftInverse.left-inverse-of A₁↞A₂ _) x ; cong = F.cong (Equivalence.to B₁⇔B₂) ∘ subst-cong (λ {x} → I.Setoid._≈_ B₁ {x} {x}) (P.sym (LeftInverse.left-inverse-of A₁↞A₂ _)) } B-from : ∀ {y} → _⟶_ (B₂ at y) (B₁ at (LeftInverse.from A₁↞A₂ ⟨$⟩ y)) B-from = Equivalence.from B₁⇔B₂ equivalence-↠ : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : I.Setoid A₁ b₁ b₁′} (B₂ : I.Setoid A₂ b₂ b₂′) (A₁↠A₂ : A₁ ↠ A₂) → (∀ {x} → Equivalence (B₁ at x) (B₂ at (Surjection.to A₁↠A₂ ⟨$⟩ x))) → Equivalence (setoid (P.setoid A₁) B₁) (setoid (P.setoid A₂) B₂) equivalence-↠ {B₁ = B₁} B₂ A₁↠A₂ B₁⇔B₂ = equivalence (Surjection.equivalence A₁↠A₂) B-to B-from where B-to : ∀ {x} → B₁ at x ⟶ B₂ at (Surjection.to A₁↠A₂ ⟨$⟩ x) B-to = Equivalence.to B₁⇔B₂ B-from : ∀ {y} → B₂ at y ⟶ B₁ at (Surjection.from A₁↠A₂ ⟨$⟩ y) B-from = record { _⟨$⟩_ = λ x → Equivalence.from B₁⇔B₂ ⟨$⟩ P.subst (I.Setoid.Carrier B₂) (P.sym $ Surjection.right-inverse-of A₁↠A₂ _) x ; cong = F.cong (Equivalence.from B₁⇔B₂) ∘ subst-cong (λ {x} → I.Setoid._≈_ B₂ {x} {x}) (P.sym (Surjection.right-inverse-of A₁↠A₂ _)) } ⇔ : ∀ {a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (A₁⇔A₂ : A₁ ⇔ A₂) → (∀ {x} → B₁ x → B₂ (Equivalence.to A₁⇔A₂ ⟨$⟩ x)) → (∀ {y} → B₂ y → B₁ (Equivalence.from A₁⇔A₂ ⟨$⟩ y)) → Σ A₁ B₁ ⇔ Σ A₂ B₂ ⇔ {B₁ = B₁} {B₂} A₁⇔A₂ B-to B-from = Inverse.equivalence (Rel↔≡ {B = B₂}) ⟨∘⟩ equivalence A₁⇔A₂ (Inverse.to (H.≡↔≅ B₂) ⊚ P.→-to-⟶ B-to ⊚ Inverse.from (H.≡↔≅ B₁)) (Inverse.to (H.≡↔≅ B₁) ⊚ P.→-to-⟶ B-from ⊚ Inverse.from (H.≡↔≅ B₂)) ⟨∘⟩ Eq.sym (Inverse.equivalence (Rel↔≡ {B = B₁})) where open Eq using () renaming (_∘_ to _⟨∘⟩_) open F using () renaming (_∘_ to _⊚_) ⇔-↠ : ∀ {a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (A₁↠A₂ : A₁ ↠ A₂) → (∀ {x} → _⇔_ (B₁ x) (B₂ (Surjection.to A₁↠A₂ ⟨$⟩ x))) → _⇔_ (Σ A₁ B₁) (Σ A₂ B₂) ⇔-↠ {B₁ = B₁} {B₂} A₁↠A₂ B₁⇔B₂ = Inverse.equivalence (Rel↔≡ {B = B₂}) ⟨∘⟩ equivalence-↠ (H.indexedSetoid B₂) A₁↠A₂ (λ {x} → Inverse.equivalence (H.≡↔≅ B₂) ⟨∘⟩ B₁⇔B₂ {x} ⟨∘⟩ Inverse.equivalence (Inv.sym (H.≡↔≅ B₁))) ⟨∘⟩ Eq.sym (Inverse.equivalence (Rel↔≡ {B = B₁})) where open Eq using () renaming (_∘_ to _⟨∘⟩_) injection : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : I.Setoid A₁ b₁ b₁′} (B₂ : I.Setoid A₂ b₂ b₂′) → (A₁↣A₂ : A₁ ↣ A₂) → (∀ {x} → Injection (B₁ at x) (B₂ at (Injection.to A₁↣A₂ ⟨$⟩ x))) → Injection (setoid (P.setoid A₁) B₁) (setoid (P.setoid A₂) B₂) injection {B₁ = B₁} B₂ A₁↣A₂ B₁↣B₂ = record { to = to ; injective = inj } where to = ⟶ B₂ (_⟨$⟩_ (Injection.to A₁↣A₂)) (Injection.to B₁↣B₂) inj : Injective to inj (x , y) = Injection.injective A₁↣A₂ x , lemma (Injection.injective A₁↣A₂ x) y where lemma : ∀ {x x′} {y : I.Setoid.Carrier B₁ x} {y′ : I.Setoid.Carrier B₁ x′} → P._≡_ x x′ → (eq : I.Setoid._≈_ B₂ (Injection.to B₁↣B₂ ⟨$⟩ y) (Injection.to B₁↣B₂ ⟨$⟩ y′)) → I.Setoid._≈_ B₁ y y′ lemma P.refl = Injection.injective B₁↣B₂ ↣ : ∀ {a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (A₁↣A₂ : A₁ ↣ A₂) → (∀ {x} → B₁ x ↣ B₂ (Injection.to A₁↣A₂ ⟨$⟩ x)) → Σ A₁ B₁ ↣ Σ A₂ B₂ ↣ {B₁ = B₁} {B₂} A₁↣A₂ B₁↣B₂ = Inverse.injection (Rel↔≡ {B = B₂}) ⟨∘⟩ injection (H.indexedSetoid B₂) A₁↣A₂ (λ {x} → Inverse.injection (H.≡↔≅ B₂) ⟨∘⟩ B₁↣B₂ {x} ⟨∘⟩ Inverse.injection (Inv.sym (H.≡↔≅ B₁))) ⟨∘⟩ Inverse.injection (Inv.sym (Rel↔≡ {B = B₁})) where open Inj using () renaming (_∘_ to _⟨∘⟩_) left-inverse : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} (B₁ : I.Setoid A₁ b₁ b₁′) {B₂ : I.Setoid A₂ b₂ b₂′} → (A₁↞A₂ : A₁ ↞ A₂) → (∀ {x} → LeftInverse (B₁ at (LeftInverse.from A₁↞A₂ ⟨$⟩ x)) (B₂ at x)) → LeftInverse (setoid (P.setoid A₁) B₁) (setoid (P.setoid A₂) B₂) left-inverse B₁ {B₂} A₁↞A₂ B₁↞B₂ = record { to = Equivalence.to eq ; from = Equivalence.from eq ; left-inverse-of = left } where eq = equivalence-↞ B₁ A₁↞A₂ (LeftInverse.equivalence B₁↞B₂) left : Equivalence.from eq LeftInverseOf Equivalence.to eq left (x , y) = LeftInverse.left-inverse-of A₁↞A₂ x , I.Setoid.trans B₁ (LeftInverse.left-inverse-of B₁↞B₂ _) (lemma (P.sym (LeftInverse.left-inverse-of A₁↞A₂ x))) where lemma : ∀ {x x′ y} (eq : P._≡_ x x′) → I.Setoid._≈_ B₁ (P.subst (I.Setoid.Carrier B₁) eq y) y lemma P.refl = I.Setoid.refl B₁ ↞ : ∀ {a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (A₁↞A₂ : A₁ ↞ A₂) → (∀ {x} → B₁ (LeftInverse.from A₁↞A₂ ⟨$⟩ x) ↞ B₂ x) → Σ A₁ B₁ ↞ Σ A₂ B₂ ↞ {B₁ = B₁} {B₂} A₁↞A₂ B₁↞B₂ = Inverse.left-inverse (Rel↔≡ {B = B₂}) ⟨∘⟩ left-inverse (H.indexedSetoid B₁) A₁↞A₂ (λ {x} → Inverse.left-inverse (H.≡↔≅ B₂) ⟨∘⟩ B₁↞B₂ {x} ⟨∘⟩ Inverse.left-inverse (Inv.sym (H.≡↔≅ B₁))) ⟨∘⟩ Inverse.left-inverse (Inv.sym (Rel↔≡ {B = B₁})) where open LeftInv using () renaming (_∘_ to _⟨∘⟩_) surjection : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : I.Setoid A₁ b₁ b₁′} (B₂ : I.Setoid A₂ b₂ b₂′) → (A₁↠A₂ : A₁ ↠ A₂) → (∀ {x} → Surjection (B₁ at x) (B₂ at (Surjection.to A₁↠A₂ ⟨$⟩ x))) → Surjection (setoid (P.setoid A₁) B₁) (setoid (P.setoid A₂) B₂) surjection {B₁} B₂ A₁↠A₂ B₁↠B₂ = record { to = Equivalence.to eq ; surjective = record { from = Equivalence.from eq ; right-inverse-of = right } } where eq = equivalence-↠ B₂ A₁↠A₂ (Surjection.equivalence B₁↠B₂) right : Equivalence.from eq RightInverseOf Equivalence.to eq right (x , y) = Surjection.right-inverse-of A₁↠A₂ x , I.Setoid.trans B₂ (Surjection.right-inverse-of B₁↠B₂ _) (lemma (P.sym $ Surjection.right-inverse-of A₁↠A₂ x)) where lemma : ∀ {x x′ y} (eq : P._≡_ x x′) → I.Setoid._≈_ B₂ (P.subst (I.Setoid.Carrier B₂) eq y) y lemma P.refl = I.Setoid.refl B₂ ↠ : ∀ {a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (A₁↠A₂ : A₁ ↠ A₂) → (∀ {x} → B₁ x ↠ B₂ (Surjection.to A₁↠A₂ ⟨$⟩ x)) → Σ A₁ B₁ ↠ Σ A₂ B₂ ↠ {B₁ = B₁} {B₂} A₁↠A₂ B₁↠B₂ = Inverse.surjection (Rel↔≡ {B = B₂}) ⟨∘⟩ surjection (H.indexedSetoid B₂) A₁↠A₂ (λ {x} → Inverse.surjection (H.≡↔≅ B₂) ⟨∘⟩ B₁↠B₂ {x} ⟨∘⟩ Inverse.surjection (Inv.sym (H.≡↔≅ B₁))) ⟨∘⟩ Inverse.surjection (Inv.sym (Rel↔≡ {B = B₁})) where open Surj using () renaming (_∘_ to _⟨∘⟩_) inverse : ∀ {a₁ a₂ b₁ b₁′ b₂ b₂′} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : I.Setoid A₁ b₁ b₁′} (B₂ : I.Setoid A₂ b₂ b₂′) → (A₁↔A₂ : A₁ ↔ A₂) → (∀ {x} → Inverse (B₁ at x) (B₂ at (Inverse.to A₁↔A₂ ⟨$⟩ x))) → Inverse (setoid (P.setoid A₁) B₁) (setoid (P.setoid A₂) B₂) inverse {B₁ = B₁} B₂ A₁↔A₂ B₁↔B₂ = record { to = Surjection.to surj ; from = Surjection.from surj ; inverse-of = record { left-inverse-of = left ; right-inverse-of = Surjection.right-inverse-of surj } } where surj = surjection B₂ (Inverse.surjection A₁↔A₂) (Inverse.surjection B₁↔B₂) left : Surjection.from surj LeftInverseOf Surjection.to surj left (x , y) = Inverse.left-inverse-of A₁↔A₂ x , I.Setoid.trans B₁ (lemma (P.sym (Inverse.left-inverse-of A₁↔A₂ x)) (P.sym (Inverse.right-inverse-of A₁↔A₂ (Inverse.to A₁↔A₂ ⟨$⟩ x)))) (Inverse.left-inverse-of B₁↔B₂ y) where lemma : ∀ {x x′ y} → P._≡_ x x′ → (eq : P._≡_ (Inverse.to A₁↔A₂ ⟨$⟩ x) (Inverse.to A₁↔A₂ ⟨$⟩ x′)) → I.Setoid._≈_ B₁ (Inverse.from B₁↔B₂ ⟨$⟩ P.subst (I.Setoid.Carrier B₂) eq y) (Inverse.from B₁↔B₂ ⟨$⟩ y) lemma P.refl P.refl = I.Setoid.refl B₁ ↔ : ∀ {a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (A₁↔A₂ : A₁ ↔ A₂) → (∀ {x} → B₁ x ↔ B₂ (Inverse.to A₁↔A₂ ⟨$⟩ x)) → Σ A₁ B₁ ↔ Σ A₂ B₂ ↔ {B₁ = B₁} {B₂} A₁↔A₂ B₁↔B₂ = Rel↔≡ {B = B₂} ⟨∘⟩ inverse (H.indexedSetoid B₂) A₁↔A₂ (λ {x} → H.≡↔≅ B₂ ⟨∘⟩ B₁↔B₂ {x} ⟨∘⟩ Inv.sym (H.≡↔≅ B₁)) ⟨∘⟩ Inv.sym (Rel↔≡ {B = B₁}) where open Inv using () renaming (_∘_ to _⟨∘⟩_) private swap-coercions : ∀ {k a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} (B₂ : A₂ → Set b₂) (A₁↔A₂ : _↔_ A₁ A₂) → (∀ {x} → B₁ x ∼[ k ] B₂ (Inverse.to A₁↔A₂ ⟨$⟩ x)) → ∀ {x} → B₁ (Inverse.from A₁↔A₂ ⟨$⟩ x) ∼[ k ] B₂ x swap-coercions {k} {B₁ = B₁} B₂ A₁↔A₂ eq {x} = B₁ (Inverse.from A₁↔A₂ ⟨$⟩ x) ∼⟨ eq ⟩ B₂ (Inverse.to A₁↔A₂ ⟨$⟩ (Inverse.from A₁↔A₂ ⟨$⟩ x)) ↔⟨ B.Setoid.reflexive (Related.setoid Related.bijection _) (P.cong B₂ $ Inverse.right-inverse-of A₁↔A₂ x) ⟩ B₂ x ∎ where open Related.EquationalReasoning cong : ∀ {k a₁ a₂ b₁ b₂} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (A₁↔A₂ : _↔_ A₁ A₂) → (∀ {x} → B₁ x ∼[ k ] B₂ (Inverse.to A₁↔A₂ ⟨$⟩ x)) → Σ A₁ B₁ ∼[ k ] Σ A₂ B₂ cong {Related.implication} = λ A₁↔A₂ → Prod.map (_⟨$⟩_ (Inverse.to A₁↔A₂)) cong {Related.reverse-implication} {B₂ = B₂} = λ A₁↔A₂ B₁←B₂ → lam (Prod.map (_⟨$⟩_ (Inverse.from A₁↔A₂)) (app-← (swap-coercions B₂ A₁↔A₂ B₁←B₂))) cong {Related.equivalence} = ⇔-↠ ∘ Inverse.surjection cong {Related.injection} = ↣ ∘ Inverse.injection cong {Related.reverse-injection} {B₂ = B₂} = λ A₁↔A₂ B₁↢B₂ → lam (↣ (Inverse.injection (Inv.sym A₁↔A₂)) (app-↢ (swap-coercions B₂ A₁↔A₂ B₁↢B₂))) cong {Related.left-inverse} = λ A₁↔A₂ → ↞ (Inverse.left-inverse A₁↔A₂) ∘ swap-coercions _ A₁↔A₂ cong {Related.surjection} = ↠ ∘ Inverse.surjection cong {Related.bijection} = ↔
open import FRP.JS.QUnit using ( TestSuites ; suite ; _,_ ) import FRP.JS.Test.Bool import FRP.JS.Test.Nat import FRP.JS.Test.Int import FRP.JS.Test.Float import FRP.JS.Test.String import FRP.JS.Test.Maybe import FRP.JS.Test.List import FRP.JS.Test.Array import FRP.JS.Test.Object import FRP.JS.Test.JSON import FRP.JS.Test.Behaviour import FRP.JS.Test.DOM import FRP.JS.Test.Compiler module FRP.JS.Test where tests : TestSuites tests = ( suite "Bool" FRP.JS.Test.Bool.tests , suite "Nat" FRP.JS.Test.Nat.tests , suite "Int" FRP.JS.Test.Int.tests , suite "Float" FRP.JS.Test.Float.tests , suite "String" FRP.JS.Test.String.tests , suite "Maybe" FRP.JS.Test.Maybe.tests , suite "List" FRP.JS.Test.List.tests , suite "Array" FRP.JS.Test.Array.tests , suite "Object" FRP.JS.Test.Object.tests , suite "JSON" FRP.JS.Test.JSON.tests , suite "Behaviour" FRP.JS.Test.Behaviour.tests , suite "DOM" FRP.JS.Test.DOM.tests , suite "Compiler" FRP.JS.Test.Compiler.tests )
{-# OPTIONS --safe #-} module STLC.Operational.Base where open import STLC.Syntax open import Data.Empty using (⊥-elim) open import Data.Nat using (ℕ; suc) open import Data.Fin using (Fin; _≟_; punchOut; punchIn) renaming (zero to fzero; suc to fsuc) open import Relation.Binary.PropositionalEquality using (_≢_; refl; sym) open import Relation.Nullary using (yes; no) open import Function.Base using (_∘_) private variable n : ℕ a : Type fin : Fin n -- | Increments the variables that are free relative to the inserted "pivot" variable. lift : Term n → Fin (suc n) → Term (suc n) lift (abs ty body) v = abs ty (lift body (fsuc v)) lift (app f x) v = app (lift f v) (lift x v) lift (var n) v = var (punchIn v n) lift ⋆ v = ⋆ lift (pair x y) v = pair (lift x v) (lift y v) lift (projₗ p) v = projₗ (lift p v) lift (projᵣ p) v = projᵣ (lift p v) -- | Inserts a binding in the middle of the context. liftΓ : Ctx n → Fin (suc n) → Type → Ctx (suc n) liftΓ Γ fzero t = Γ ,- t liftΓ (Γ ,- A) (fsuc v) t = (liftΓ Γ v t) ,- A infix 7 _[_≔_] _[_≔_] : Term (suc n) → Fin (suc n) → Term n → Term n (abs ty body) [ v ≔ def ] = abs ty (body [ fsuc v ≔ lift def fzero ]) (app f x) [ v ≔ def ] = app (f [ v ≔ def ]) (x [ v ≔ def ]) (var n) [ v ≔ def ] with v ≟ n ... | yes refl = def ... | no neq = var (punchOut {i = v} {j = n} λ { refl → neq (sym refl)}) ⋆ [ v ≔ def ] = ⋆ (pair x y) [ v ≔ def ] = pair (x [ v ≔ def ]) (y [ v ≔ def ]) (projₗ p) [ v ≔ def ] = projₗ (p [ v ≔ def ]) (projᵣ p) [ v ≔ def ] = projᵣ (p [ v ≔ def ])
{-# OPTIONS --safe --without-K #-} module CF.Types where open import Data.Unit using (⊤; tt) open import Data.Empty using (⊥) open import Data.Product open import Data.List as L open import Data.String open import Relation.Binary open import Relation.Binary.PropositionalEquality open import Relation.Nullary.Decidable open import Relation.Nullary data Ty : Set where void : Ty int : Ty bool : Ty variable a b c r s t : Ty as bs cs : List Ty
{-# OPTIONS --cumulativity #-} open import Agda.Primitive open import Agda.Builtin.Nat open import Agda.Builtin.Equality module _ where variable ℓ ℓ′ ℓ₁ ℓ₂ : Level A B C : Set ℓ k l m n : Nat lone ltwo lthree : Level lone = lsuc lzero ltwo = lsuc lone lthree = lsuc ltwo set0 : Set₂ set0 = Set₀ set1 : Set₂ set1 = Set₁ lift : Set ℓ → Set (ℓ ⊔ ℓ′) lift x = x data List (A : Set ℓ) : Set ℓ where [] : List A _∷_ : (x : A) (xs : List A) → List A List-test₁ : (A : Set) → List {lone} A List-test₁ A = [] lower-List : ∀ ℓ′ {A : Set ℓ} → List {ℓ ⊔ ℓ′} A → List A lower-List ℓ [] = [] lower-List ℓ (x ∷ xs) = x ∷ lower-List ℓ xs lower-List-test₁ : {A : Set} → List {lthree} A → List {lone} A lower-List-test₁ = lower-List lthree map : (f : A → B) → List A → List B map f [] = [] map f (x ∷ xs) = f x ∷ map f xs map00 : {A B : Set} (f : A → B) → List A → List B map00 = map map01 : {A : Set} {B : Set₁} (f : A → B) → List A → List B map01 = map map10 : {A : Set₁} {B : Set} (f : A → B) → List A → List B map10 = map map20 : {A : Set₂} {B : Set} (f : A → B) → List A → List B map20 = map data Vec (A : Set ℓ) : Nat → Set ℓ where [] : Vec A 0 _∷_ : A → Vec A n → Vec A (suc n) module Without-Cumulativity where N-ary-level : Level → Level → Nat → Level N-ary-level ℓ₁ ℓ₂ zero = ℓ₂ N-ary-level ℓ₁ ℓ₂ (suc n) = ℓ₁ ⊔ N-ary-level ℓ₁ ℓ₂ n N-ary : ∀ n → Set ℓ₁ → Set ℓ₂ → Set (N-ary-level ℓ₁ ℓ₂ n) N-ary zero A B = B N-ary (suc n) A B = A → N-ary n A B module With-Cumulativity where N-ary : Nat → Set ℓ₁ → Set ℓ₂ → Set (ℓ₁ ⊔ ℓ₂) N-ary zero A B = B N-ary (suc n) A B = A → N-ary n A B curryⁿ : (Vec A n → B) → N-ary n A B curryⁿ {n = zero} f = f [] curryⁿ {n = suc n} f = λ x → curryⁿ λ xs → f (x ∷ xs) _$ⁿ_ : N-ary n A B → (Vec A n → B) f $ⁿ [] = f f $ⁿ (x ∷ xs) = f x $ⁿ xs ∀ⁿ : ∀ {A : Set ℓ} n → N-ary n A (Set ℓ′) → Set (ℓ ⊔ ℓ′) ∀ⁿ zero P = P ∀ⁿ (suc n) P = ∀ x → ∀ⁿ n (P x)
------------------------------------------------------------------------ -- The Agda standard library -- -- Container Morphisms ------------------------------------------------------------------------ {-# OPTIONS --without-K --safe #-} module Data.Container.Morphism where open import Data.Container.Core import Function as F module _ {s p} (C : Container s p) where id : C ⇒ C id .shape = F.id id .position = F.id module _ {s₁ s₂ s₃ p₁ p₂ p₃} {C₁ : Container s₁ p₁} {C₂ : Container s₂ p₂} {C₃ : Container s₃ p₃} where infixr 9 _∘_ _∘_ : C₂ ⇒ C₃ → C₁ ⇒ C₂ → C₁ ⇒ C₃ (f ∘ g) .shape = shape f F.∘′ shape g (f ∘ g) .position = position g F.∘′ position f
module AbstractInterfaceExample where open import Function open import Data.Bool open import Data.String -- * One-parameter interface for the type `a' with only one method. record VoiceInterface (a : Set) : Set where constructor voice-interface field say-method-of : a → String open VoiceInterface -- * An overloaded method. say : {a : Set} → ⦃ _ : VoiceInterface a ⦄ → a → String say ⦃ instance ⦄ = say-method-of instance -- * Some data types. data Cat : Set where cat : Bool → Cat crazy! = true plain-cat = false -- | This cat is crazy? crazy? : Cat → Bool crazy? (cat x) = x -- | A 'plain' dog. data Dog : Set where dog : Dog -- * Implementation of the interface (and method). instance-for-cat : VoiceInterface Cat instance-for-cat = voice-interface case where case : Cat → String case x with crazy? x ... | true = "meeeoooowwwww!!!" ... | false = "meow!" instance-for-dog : VoiceInterface Dog instance-for-dog = voice-interface $ const "woof!" -- * and then: -- -- say dog => "woof!" -- say (cat crazy!) => "meeeoooowwwww!!!" -- say (cat plain-cat) => "meow!" --
module Dave.Isomorphism where open import Dave.Equality open import Dave.Functions infix 0 _≃_ record _≃_ (A B : Set) : Set where field to : A → B from : B → A from∘to : ∀ (x : A) → from (to x) ≡ x to∘from : ∀ (x : B) → to (from x) ≡ x open _≃_ {- Another way to define Isomporphism throug data -} data _≃′_ (A B : Set) : Set where mk-≃′ : ∀ (to : A → B) → ∀ (from : B → A) → ∀ (from∘to : (∀ (x : A) → from (to x) ≡ x)) → ∀ (to∘from : (∀ (y : B) → to (from y) ≡ y)) → A ≃′ B to′ : ∀ {A B : Set} → (A ≃′ B) → (A → B) to′ (mk-≃′ f g g∘f f∘g) = f from′ : ∀ {A B : Set} → (A ≃′ B) → (B → A) from′ (mk-≃′ f g g∘f f∘g) = g from∘to′ : ∀ {A B : Set} → (A≃B : A ≃′ B) → (∀ (x : A) → from′ A≃B (to′ A≃B x) ≡ x) from∘to′ (mk-≃′ f g g∘f f∘g) = g∘f to∘from′ : ∀ {A B : Set} → (A≃B : A ≃′ B) → (∀ (y : B) → to′ A≃B (from′ A≃B y) ≡ y) to∘from′ (mk-≃′ f g g∘f f∘g) = f∘g ≃-refl : ∀ {A : Set} → A ≃ A ≃-refl = record { to = λ x → x; from = λ y → y; from∘to = λ x → refl; to∘from = λ y → refl } ≃-sym : ∀ {A B : Set} → A ≃ B → B ≃ A ≃-sym A≃B = record { to = from A≃B ; from = to A≃B ; from∘to = to∘from A≃B ; to∘from = from∘to A≃B } ≃-trans : ∀ {A B C : Set} → A ≃ B → B ≃ C → A ≃ C ≃-trans A≃B B≃C = record { to = (to B≃C) ∘ (to A≃B); from = (from A≃B) ∘ (from B≃C); from∘to = λ x → begin (from A≃B ∘ from B≃C) ((to B≃C ∘ to A≃B) x) ≡⟨⟩ from A≃B (from B≃C (to B≃C (to A≃B x))) ≡⟨ cong (from A≃B) (from∘to B≃C (to A≃B x)) ⟩ from A≃B (to A≃B x) ≡⟨ from∘to A≃B x ⟩ x ∎; to∘from = λ x → begin (to B≃C ∘ to A≃B) ((from A≃B ∘ from B≃C) x) ≡⟨⟩ to B≃C (to A≃B (from A≃B (from B≃C x))) ≡⟨ cong (to B≃C) (to∘from A≃B (from B≃C x)) ⟩ to B≃C (from B≃C x) ≡⟨ to∘from B≃C x ⟩ x ∎ } module ≃-Reasoning where infix 1 ≃-begin_ infixr 2 _≃⟨_⟩_ infix 3 _≃-∎ ≃-begin_ : ∀ {A B : Set} → A ≃ B ----- → A ≃ B ≃-begin A≃B = A≃B _≃⟨_⟩_ : ∀ (A : Set) {B C : Set} → A ≃ B → B ≃ C ----- → A ≃ C A ≃⟨ A≃B ⟩ B≃C = ≃-trans A≃B B≃C _≃-∎ : ∀ (A : Set) ----- → A ≃ A A ≃-∎ = ≃-refl open ≃-Reasoning public
open import Relation.Binary.Core module Mergesort.Impl2.Correctness.Permutation {A : Set} (_≤_ : A → A → Set) (tot≤ : Total _≤_) where open import Bound.Lower A open import Bound.Lower.Order _≤_ open import Data.List open import Data.Sum open import List.Permutation.Base A open import List.Permutation.Base.Concatenation A open import List.Permutation.Base.Equivalence A open import List.Properties A open import LRTree {A} open import Mergesort.Impl2 _≤_ tot≤ open import OList _≤_ lemma-merge'-empty : {b : Bound}(xs : OList b) → merge' xs (onil {b}) ≡ xs lemma-merge'-empty onil = refl lemma-merge'-empty (:< b≤x xs) = refl mutual lemma-merge' : {b : Bound}(xs ys : OList b) → (forget xs ++ forget ys) ∼ forget (merge' xs ys) lemma-merge' onil ys = refl∼ lemma-merge' xs onil rewrite ++id (forget xs) | lemma-merge'-empty xs = refl∼ lemma-merge' (:< {x = x} b≤x xs) (:< {x = y} b≤y ys) with tot≤ x y ... | inj₁ x≤y = ∼x /head /head (lemma-merge' xs (:< (lexy x≤y) ys)) ... | inj₂ y≤x = let f'xxsyf'ys∼yf'xxsf'ys = ∼x (lemma++/ {y} {forget (:< b≤x xs)}) /head refl∼ ; yf'xxsf'ys∼yf'm'xxsys = ∼x /head /head (lemma-merge'xs (lexy y≤x) xs ys) in trans∼ f'xxsyf'ys∼yf'xxsf'ys yf'xxsf'ys∼yf'm'xxsys lemma-merge'xs : {b : Bound}{x : A} → (b≤x : LeB b (val x))(xs : OList (val x))(ys : OList b) → (forget (:< b≤x xs) ++ forget ys) ∼ forget (merge'xs b≤x xs ys) lemma-merge'xs b≤x xs onil rewrite ++id (forget xs) | lemma-merge'-empty xs = refl∼ lemma-merge'xs {x = x} b≤x xs (:< {x = y} b≤y ys) with tot≤ x y ... | inj₁ x≤y = ∼x /head /head (lemma-merge' xs (:< (lexy x≤y) ys)) ... | inj₂ y≤x = let f'xxsyf'ys∼yf'xxsf'ys = ∼x (lemma++/ {y} {forget (:< b≤x xs)}) /head refl∼ ; yf'xxsf'ys∼yf'm'xxsys = ∼x /head /head (lemma-merge'xs (lexy y≤x) xs ys) in trans∼ f'xxsyf'ys∼yf'xxsf'ys yf'xxsf'ys∼yf'm'xxsys lemma-insert : (x : A)(t : LRTree) → (x ∷ flatten t) ∼ flatten (insert x t) lemma-insert x empty = ∼x /head /head ∼[] lemma-insert x (leaf y) = ∼x (/tail /head) /head (∼x /head /head ∼[]) lemma-insert x (node left l r) = lemma++∼r (lemma-insert x l) lemma-insert x (node right l r) = let xlr∼lxr = ∼x /head (lemma++/ {x} {flatten l}) refl∼ ; lxr∼lrᵢ = lemma++∼l {flatten l} (lemma-insert x r) in trans∼ xlr∼lxr lxr∼lrᵢ lemma-deal : (xs : List A) → xs ∼ flatten (deal xs) lemma-deal [] = ∼[] lemma-deal (x ∷ xs) = trans∼ (∼x /head /head (lemma-deal xs)) (lemma-insert x (deal xs)) lemma-merge : (t : LRTree) → flatten t ∼ forget (merge t) lemma-merge empty = ∼[] lemma-merge (leaf x) = ∼x /head /head ∼[] lemma-merge (node t l r) = let flfr∼f'm'lf'm'r = lemma++∼ (lemma-merge l) (lemma-merge r) ; f'm'lf'm'r∼m''m''lm''r = lemma-merge' (merge l) (merge r) in trans∼ flfr∼f'm'lf'm'r f'm'lf'm'r∼m''m''lm''r theorem-mergesort-∼ : (xs : List A) → xs ∼ (forget (mergesort xs)) theorem-mergesort-∼ xs = trans∼ (lemma-deal xs) (lemma-merge (deal xs))
{-# OPTIONS --cubical --safe #-} module Data.Integer where open import Level open import Data.Nat using (ℕ; suc; zero) import Data.Nat as ℕ import Data.Nat.Properties as ℕ open import Data.Bool data ℤ : Type where ⁺ : ℕ → ℤ ⁻suc : ℕ → ℤ ⁻ : ℕ → ℤ ⁻ zero = ⁺ zero ⁻ (suc n) = ⁻suc n {-# DISPLAY ⁻suc n = ⁻ suc n #-} negate : ℤ → ℤ negate (⁺ x) = ⁻ x negate (⁻suc x) = ⁺ (suc x) {-# DISPLAY negate x = ⁻ x #-} infixl 6 _+_ _-suc_ : ℕ → ℕ → ℤ x -suc y = if y ℕ.<ᴮ x then ⁺ (x ℕ.∸ (suc y)) else ⁻suc (y ℕ.∸ x) _+_ : ℤ → ℤ → ℤ ⁺ x + ⁺ y = ⁺ (x ℕ.+ y) ⁺ x + ⁻suc y = x -suc y ⁻suc x + ⁺ y = y -suc x ⁻suc x + ⁻suc y = ⁻suc (suc x ℕ.+ y) _*-suc_ : ℕ → ℕ → ℤ zero *-suc m = ⁺ zero suc n *-suc m = ⁻suc (n ℕ.+ m ℕ.+ n ℕ.* m) infixl 7 _*_ _*_ : ℤ → ℤ → ℤ ⁺ x * ⁺ y = ⁺ (x ℕ.* y) ⁺ x * ⁻suc y = x *-suc y ⁻suc x * ⁺ y = y *-suc x ⁻suc x * ⁻suc y = ⁺ (suc x ℕ.* suc y)
------------------------------------------------------------------------ -- Some definitions related to and properties of natural numbers ------------------------------------------------------------------------ {-# OPTIONS --without-K --safe #-} open import Equality module Nat {reflexive} (eq : ∀ {a p} → Equality-with-J a p reflexive) where import Agda.Builtin.Bool import Agda.Builtin.Nat open import Logical-equivalence using (_⇔_) open import Prelude open Derived-definitions-and-properties eq ------------------------------------------------------------------------ -- A helper function private -- Converts builtin booleans to Bool. Bool→Bool : Agda.Builtin.Bool.Bool → Bool Bool→Bool Agda.Builtin.Bool.true = true Bool→Bool Agda.Builtin.Bool.false = false ------------------------------------------------------------------------ -- Equality of natural numbers is decidable -- Inhabited only for zero. Zero : ℕ → Type Zero zero = ⊤ Zero (suc n) = ⊥ -- Predecessor (except if the argument is zero). pred : ℕ → ℕ pred zero = zero pred (suc n) = n abstract -- Zero is not equal to the successor of any number. 0≢+ : {n : ℕ} → zero ≢ suc n 0≢+ 0≡+ = subst Zero 0≡+ tt -- The suc constructor is cancellative. cancel-suc : {m n : ℕ} → suc m ≡ suc n → m ≡ n cancel-suc = cong pred -- Equality of natural numbers is decidable. infix 4 _≟_ _≟_ : Decidable-equality ℕ zero ≟ zero = yes (refl _) suc m ≟ suc n = ⊎-map (cong suc) (λ m≢n → m≢n ∘ cancel-suc) (m ≟ n) zero ≟ suc n = no 0≢+ suc m ≟ zero = no (0≢+ ∘ sym) -- An equality test that is likely to be more efficient. infix 4 _==_ _==_ : ℕ → ℕ → Bool m == n = Bool→Bool (m Agda.Builtin.Nat.== n) ------------------------------------------------------------------------ -- Inequality -- Inequality. Distinct : ℕ → ℕ → Type Distinct zero zero = ⊥ Distinct zero (suc _) = ⊤ Distinct (suc _) zero = ⊤ Distinct (suc m) (suc n) = Distinct m n -- Distinct is pointwise logically equivalent to _≢_. Distinct⇔≢ : {m n : ℕ} → Distinct m n ⇔ m ≢ n Distinct⇔≢ = record { to = to _ _; from = from _ _ } where to : ∀ m n → Distinct m n → m ≢ n to zero zero () to zero (suc n) _ = 0≢+ to (suc m) zero _ = 0≢+ ∘ sym to (suc m) (suc n) m≢n = to m n m≢n ∘ cancel-suc from : ∀ m n → m ≢ n → Distinct m n from zero zero 0≢0 = 0≢0 (refl 0) from zero (suc n) _ = _ from (suc m) zero _ = _ from (suc m) (suc n) 1+m≢1+n = from m n (1+m≢1+n ∘ cong suc) -- Two natural numbers are either equal or distinct. ≡⊎Distinct : ∀ m n → m ≡ n ⊎ Distinct m n ≡⊎Distinct m n = ⊎-map id (_⇔_.from Distinct⇔≢) (m ≟ n) ------------------------------------------------------------------------ -- Properties related to _+_ -- Addition is associative. +-assoc : ∀ m {n o} → m + (n + o) ≡ (m + n) + o +-assoc zero = refl _ +-assoc (suc m) = cong suc (+-assoc m) -- Zero is a right additive unit. +-right-identity : ∀ {n} → n + 0 ≡ n +-right-identity {zero} = refl 0 +-right-identity {suc _} = cong suc +-right-identity -- The successor constructor can be moved from one side of _+_ to the -- other. suc+≡+suc : ∀ m {n} → suc m + n ≡ m + suc n suc+≡+suc zero = refl _ suc+≡+suc (suc m) = cong suc (suc+≡+suc m) -- Addition is commutative. +-comm : ∀ m {n} → m + n ≡ n + m +-comm zero = sym +-right-identity +-comm (suc m) {n} = suc (m + n) ≡⟨ cong suc (+-comm m) ⟩ suc (n + m) ≡⟨ suc+≡+suc n ⟩∎ n + suc m ∎ -- A number is not equal to a strictly larger number. ≢1+ : ∀ m {n} → ¬ m ≡ suc (m + n) ≢1+ zero p = 0≢+ p ≢1+ (suc m) p = ≢1+ m (cancel-suc p) -- Addition is left cancellative. +-cancellativeˡ : ∀ {m n₁ n₂} → m + n₁ ≡ m + n₂ → n₁ ≡ n₂ +-cancellativeˡ {zero} = id +-cancellativeˡ {suc m} = +-cancellativeˡ ∘ cancel-suc -- Addition is right cancellative. +-cancellativeʳ : ∀ {m₁ m₂ n} → m₁ + n ≡ m₂ + n → m₁ ≡ m₂ +-cancellativeʳ {m₁} {m₂} {n} hyp = +-cancellativeˡ ( n + m₁ ≡⟨ +-comm n ⟩ m₁ + n ≡⟨ hyp ⟩ m₂ + n ≡⟨ +-comm m₂ ⟩∎ n + m₂ ∎) ------------------------------------------------------------------------ -- Properties related to _*_ -- Zero is a right zero for multiplication. *-right-zero : ∀ n → n * 0 ≡ 0 *-right-zero zero = refl 0 *-right-zero (suc n) = *-right-zero n -- Multiplication is commutative. *-comm : ∀ m {n} → m * n ≡ n * m *-comm zero {n} = sym (*-right-zero n) *-comm (suc m) {zero} = *-right-zero m *-comm (suc m) {suc n} = suc (n + m * suc n) ≡⟨ cong (suc ∘ (n +_)) (*-comm m) ⟩ suc (n + suc n * m) ≡⟨⟩ suc (n + (m + n * m)) ≡⟨ cong suc (+-assoc n) ⟩ suc (n + m + n * m) ≡⟨ cong₂ (λ x y → suc (x + y)) (+-comm n) (sym (*-comm m)) ⟩ suc (m + n + m * n) ≡⟨ cong suc (sym (+-assoc m)) ⟩ suc (m + (n + m * n)) ≡⟨⟩ suc (m + suc m * n) ≡⟨ cong (suc ∘ (m +_)) (*-comm (suc m) {n}) ⟩∎ suc (m + n * suc m) ∎ -- One is a left identity for multiplication. *-left-identity : ∀ {n} → 1 * n ≡ n *-left-identity {n} = 1 * n ≡⟨⟩ n + zero ≡⟨ +-right-identity ⟩∎ n ∎ -- One is a right identity for multiplication. *-right-identity : ∀ n → n * 1 ≡ n *-right-identity n = n * 1 ≡⟨ *-comm n ⟩ 1 * n ≡⟨ *-left-identity ⟩∎ n ∎ -- Multiplication distributes from the left over addition. *-+-distribˡ : ∀ m {n o} → m * (n + o) ≡ m * n + m * o *-+-distribˡ zero = refl _ *-+-distribˡ (suc m) {n} {o} = n + o + m * (n + o) ≡⟨ cong ((n + o) +_) (*-+-distribˡ m) ⟩ n + o + (m * n + m * o) ≡⟨ sym (+-assoc n) ⟩ n + (o + (m * n + m * o)) ≡⟨ cong (n +_) (+-assoc o) ⟩ n + (o + m * n + m * o) ≡⟨ cong ((n +_) ∘ (_+ m * o)) (+-comm o) ⟩ n + (m * n + o + m * o) ≡⟨ cong (n +_) (sym (+-assoc (m * _))) ⟩ n + (m * n + (o + m * o)) ≡⟨ +-assoc n ⟩∎ n + m * n + (o + m * o) ∎ -- Multiplication distributes from the right over addition. *-+-distribʳ : ∀ m {n o} → (m + n) * o ≡ m * o + n * o *-+-distribʳ m {n} {o} = (m + n) * o ≡⟨ *-comm (m + _) ⟩ o * (m + n) ≡⟨ *-+-distribˡ o ⟩ o * m + o * n ≡⟨ cong₂ _+_ (*-comm o) (*-comm o) ⟩∎ m * o + n * o ∎ -- Multiplication is associative. *-assoc : ∀ m {n k} → m * (n * k) ≡ (m * n) * k *-assoc zero = refl _ *-assoc (suc m) {n = n} {k = k} = n * k + m * (n * k) ≡⟨ cong (n * k +_) $ *-assoc m ⟩ n * k + (m * n) * k ≡⟨ sym $ *-+-distribʳ n ⟩∎ (n + m * n) * k ∎ -- Multiplication is right cancellative for positive numbers. *-cancellativeʳ : ∀ {m₁ m₂ n} → m₁ * suc n ≡ m₂ * suc n → m₁ ≡ m₂ *-cancellativeʳ {zero} {zero} = λ _ → zero ∎ *-cancellativeʳ {zero} {suc _} = ⊥-elim ∘ 0≢+ *-cancellativeʳ {suc _} {zero} = ⊥-elim ∘ 0≢+ ∘ sym *-cancellativeʳ {suc m₁} {suc m₂} = cong suc ∘ *-cancellativeʳ ∘ +-cancellativeˡ -- Multiplication is left cancellative for positive numbers. *-cancellativeˡ : ∀ m {n₁ n₂} → suc m * n₁ ≡ suc m * n₂ → n₁ ≡ n₂ *-cancellativeˡ m {n₁} {n₂} hyp = *-cancellativeʳ ( n₁ * suc m ≡⟨ *-comm n₁ ⟩ suc m * n₁ ≡⟨ hyp ⟩ suc m * n₂ ≡⟨ *-comm (suc m) ⟩∎ n₂ * suc m ∎) -- Even numbers are not equal to odd numbers. even≢odd : ∀ m n → 2 * m ≢ 1 + 2 * n even≢odd zero n eq = 0≢+ eq even≢odd (suc m) n eq = even≢odd n m ( 2 * n ≡⟨ sym $ cancel-suc eq ⟩ m + 1 * (1 + m) ≡⟨ sym $ suc+≡+suc m ⟩∎ 1 + 2 * m ∎) ------------------------------------------------------------------------ -- Properties related to _^_ -- One is a right identity for exponentiation. ^-right-identity : ∀ {n} → n ^ 1 ≡ n ^-right-identity {n} = n ^ 1 ≡⟨⟩ n * 1 ≡⟨ *-right-identity _ ⟩∎ n ∎ -- One is a left zero for exponentiation. ^-left-zero : ∀ n → 1 ^ n ≡ 1 ^-left-zero zero = refl _ ^-left-zero (suc n) = 1 ^ suc n ≡⟨⟩ 1 * 1 ^ n ≡⟨ *-left-identity ⟩ 1 ^ n ≡⟨ ^-left-zero n ⟩∎ 1 ∎ -- Some rearrangement lemmas for exponentiation. ^+≡^*^ : ∀ {m} n {k} → m ^ (n + k) ≡ m ^ n * m ^ k ^+≡^*^ {m} zero {k} = m ^ k ≡⟨ sym *-left-identity ⟩ 1 * m ^ k ≡⟨⟩ m ^ 0 * m ^ k ∎ ^+≡^*^ {m} (suc n) {k} = m ^ (1 + n + k) ≡⟨⟩ m * m ^ (n + k) ≡⟨ cong (m *_) $ ^+≡^*^ n ⟩ m * (m ^ n * m ^ k) ≡⟨ *-assoc m ⟩ m * m ^ n * m ^ k ≡⟨⟩ m ^ (1 + n) * m ^ k ∎ *^≡^*^ : ∀ {m n} k → (m * n) ^ k ≡ m ^ k * n ^ k *^≡^*^ {m} {n} zero = refl _ *^≡^*^ {m} {n} (suc k) = (m * n) ^ suc k ≡⟨⟩ m * n * (m * n) ^ k ≡⟨ cong (m * n *_) $ *^≡^*^ k ⟩ m * n * (m ^ k * n ^ k) ≡⟨ sym $ *-assoc m ⟩ m * (n * (m ^ k * n ^ k)) ≡⟨ cong (m *_) $ *-assoc n ⟩ m * (n * m ^ k * n ^ k) ≡⟨ cong (λ x → m * (x * n ^ k)) $ *-comm n ⟩ m * (m ^ k * n * n ^ k) ≡⟨ cong (m *_) $ sym $ *-assoc (m ^ k) ⟩ m * (m ^ k * (n * n ^ k)) ≡⟨ *-assoc m ⟩ m * m ^ k * (n * n ^ k) ≡⟨⟩ m ^ suc k * n ^ suc k ∎ ^^≡^* : ∀ {m} n {k} → (m ^ n) ^ k ≡ m ^ (n * k) ^^≡^* {m} zero {k} = ^-left-zero k ^^≡^* {m} (suc n) {k} = (m ^ (1 + n)) ^ k ≡⟨⟩ (m * m ^ n) ^ k ≡⟨ *^≡^*^ k ⟩ m ^ k * (m ^ n) ^ k ≡⟨ cong (m ^ k *_) $ ^^≡^* n ⟩ m ^ k * m ^ (n * k) ≡⟨ sym $ ^+≡^*^ k ⟩ m ^ (k + n * k) ≡⟨⟩ m ^ ((1 + n) * k) ∎ ------------------------------------------------------------------------ -- The usual ordering of the natural numbers, along with some -- properties -- The ordering. infix 4 _≤_ _<_ data _≤_ (m n : ℕ) : Type where ≤-refl′ : m ≡ n → m ≤ n ≤-step′ : ∀ {k} → m ≤ k → suc k ≡ n → m ≤ n -- Strict inequality. _<_ : ℕ → ℕ → Type m < n = suc m ≤ n -- Some abbreviations. ≤-refl : ∀ {n} → n ≤ n ≤-refl = ≤-refl′ (refl _) ≤-step : ∀ {m n} → m ≤ n → m ≤ suc n ≤-step m≤n = ≤-step′ m≤n (refl _) -- _≤_ is transitive. ≤-trans : ∀ {m n o} → m ≤ n → n ≤ o → m ≤ o ≤-trans p (≤-refl′ eq) = subst (_ ≤_) eq p ≤-trans p (≤-step′ q eq) = ≤-step′ (≤-trans p q) eq -- If m is strictly less than n, then m is less than or equal to n. <→≤ : ∀ {m n} → m < n → m ≤ n <→≤ m<n = ≤-trans (≤-step ≤-refl) m<n -- "Equational" reasoning combinators. infix -1 finally-≤ _∎≤ infixr -2 step-≤ step-≡≤ _≡⟨⟩≤_ step-< _<⟨⟩_ _∎≤ : ∀ n → n ≤ n _ ∎≤ = ≤-refl -- For an explanation of why step-≤, step-≡≤ and step-< are defined in -- this way, see Equality.step-≡. step-≤ : ∀ m {n o} → n ≤ o → m ≤ n → m ≤ o step-≤ _ n≤o m≤n = ≤-trans m≤n n≤o syntax step-≤ m n≤o m≤n = m ≤⟨ m≤n ⟩ n≤o step-≡≤ : ∀ m {n o} → n ≤ o → m ≡ n → m ≤ o step-≡≤ _ n≤o m≡n = subst (_≤ _) (sym m≡n) n≤o syntax step-≡≤ m n≤o m≡n = m ≡⟨ m≡n ⟩≤ n≤o _≡⟨⟩≤_ : ∀ m {n} → m ≤ n → m ≤ n _ ≡⟨⟩≤ m≤n = m≤n finally-≤ : ∀ m n → m ≤ n → m ≤ n finally-≤ _ _ m≤n = m≤n syntax finally-≤ m n m≤n = m ≤⟨ m≤n ⟩∎ n ∎≤ step-< : ∀ m {n o} → n ≤ o → m < n → m ≤ o step-< m {n} {o} n≤o m<n = m ≤⟨ <→≤ m<n ⟩ n ≤⟨ n≤o ⟩∎ o ∎≤ syntax step-< m n≤o m<n = m <⟨ m<n ⟩ n≤o _<⟨⟩_ : ∀ m {n} → m < n → m ≤ n _<⟨⟩_ _ = <→≤ -- Some simple lemmas. zero≤ : ∀ n → zero ≤ n zero≤ zero = ≤-refl zero≤ (suc n) = ≤-step (zero≤ n) suc≤suc : ∀ {m n} → m ≤ n → suc m ≤ suc n suc≤suc (≤-refl′ eq) = ≤-refl′ (cong suc eq) suc≤suc (≤-step′ m≤n eq) = ≤-step′ (suc≤suc m≤n) (cong suc eq) suc≤suc⁻¹ : ∀ {m n} → suc m ≤ suc n → m ≤ n suc≤suc⁻¹ (≤-refl′ eq) = ≤-refl′ (cancel-suc eq) suc≤suc⁻¹ (≤-step′ p eq) = ≤-trans (≤-step ≤-refl) (subst (_ ≤_) (cancel-suc eq) p) m≤m+n : ∀ m n → m ≤ m + n m≤m+n zero n = zero≤ n m≤m+n (suc m) n = suc≤suc (m≤m+n m n) m≤n+m : ∀ m n → m ≤ n + m m≤n+m m zero = ≤-refl m≤n+m m (suc n) = ≤-step (m≤n+m m n) pred≤ : ∀ n → pred n ≤ n pred≤ zero = ≤-refl pred≤ (suc _) = ≤-step ≤-refl -- A decision procedure for _≤_. infix 4 _≤?_ _≤?_ : Decidable _≤_ zero ≤? n = inj₁ (zero≤ n) suc m ≤? zero = inj₂ λ { (≤-refl′ eq) → 0≢+ (sym eq) ; (≤-step′ _ eq) → 0≢+ (sym eq) } suc m ≤? suc n = ⊎-map suc≤suc (λ m≰n → m≰n ∘ suc≤suc⁻¹) (m ≤? n) -- A comparison operator that is likely to be more efficient than -- _≤?_. infix 4 _<=_ _<=_ : ℕ → ℕ → Bool m <= n = Bool→Bool (m Agda.Builtin.Nat.< suc n) -- If m is not less than or equal to n, then n is strictly less -- than m. ≰→> : ∀ {m n} → ¬ m ≤ n → n < m ≰→> {zero} {n} p = ⊥-elim (p (zero≤ n)) ≰→> {suc m} {zero} p = suc≤suc (zero≤ m) ≰→> {suc m} {suc n} p = suc≤suc (≰→> (p ∘ suc≤suc)) -- If m is not less than or equal to n, then n is less than or -- equal to m. ≰→≥ : ∀ {m n} → ¬ m ≤ n → n ≤ m ≰→≥ p = ≤-trans (≤-step ≤-refl) (≰→> p) -- If m is less than or equal to n, but not equal to n, then m is -- strictly less than n. ≤≢→< : ∀ {m n} → m ≤ n → m ≢ n → m < n ≤≢→< (≤-refl′ eq) m≢m = ⊥-elim (m≢m eq) ≤≢→< (≤-step′ m≤n eq) m≢1+n = subst (_ <_) eq (suc≤suc m≤n) -- If m is less than or equal to n, then m is strictly less than n or -- equal to n. ≤→<⊎≡ : ∀ {m n} → m ≤ n → m < n ⊎ m ≡ n ≤→<⊎≡ (≤-refl′ m≡n) = inj₂ m≡n ≤→<⊎≡ {m} {n} (≤-step′ {k = k} m≤k 1+k≡n) = inj₁ ( suc m ≤⟨ suc≤suc m≤k ⟩ suc k ≡⟨ 1+k≡n ⟩≤ n ∎≤) -- _≤_ is total. total : ∀ m n → m ≤ n ⊎ n ≤ m total m n = ⊎-map id ≰→≥ (m ≤? n) -- A variant of total. infix 4 _≤⊎>_ _≤⊎>_ : ∀ m n → m ≤ n ⊎ n < m m ≤⊎> n = ⊎-map id ≰→> (m ≤? n) -- Another variant of total. infix 4 _<⊎≡⊎>_ _<⊎≡⊎>_ : ∀ m n → m < n ⊎ m ≡ n ⊎ n < m m <⊎≡⊎> n = case m ≤⊎> n of λ where (inj₂ n<m) → inj₂ (inj₂ n<m) (inj₁ m≤n) → ⊎-map id inj₁ (≤→<⊎≡ m≤n) -- A number is not strictly greater than a smaller (strictly or not) -- number. +≮ : ∀ m {n} → ¬ m + n < n +≮ m {n} (≤-refl′ q) = ≢1+ n (n ≡⟨ sym q ⟩ suc (m + n) ≡⟨ cong suc (+-comm m) ⟩∎ suc (n + m) ∎) +≮ m {zero} (≤-step′ {k = k} p q) = 0≢+ (0 ≡⟨ sym q ⟩∎ suc k ∎) +≮ m {suc n} (≤-step′ {k = k} p q) = +≮ m {n} (suc m + n ≡⟨ suc+≡+suc m ⟩≤ m + suc n <⟨ p ⟩ k ≡⟨ cancel-suc q ⟩≤ n ∎≤) -- No number is strictly less than zero. ≮0 : ∀ n → ¬ n < zero ≮0 n = +≮ n ∘ subst (_< 0) (sym +-right-identity) -- _<_ is irreflexive. <-irreflexive : ∀ {n} → ¬ n < n <-irreflexive = +≮ 0 -- If m is strictly less than n, then m is not equal to n. <→≢ : ∀ {m n} → m < n → m ≢ n <→≢ m<n m≡n = <-irreflexive (subst (_< _) m≡n m<n) -- Antisymmetry. ≤-antisymmetric : ∀ {m n} → m ≤ n → n ≤ m → m ≡ n ≤-antisymmetric (≤-refl′ q₁) _ = q₁ ≤-antisymmetric _ (≤-refl′ q₂) = sym q₂ ≤-antisymmetric {m} {n} (≤-step′ {k = k₁} p₁ q₁) (≤-step′ {k = k₂} p₂ q₂) = ⊥-elim (<-irreflexive ( suc k₁ ≡⟨ q₁ ⟩≤ n ≤⟨ p₂ ⟩ k₂ <⟨⟩ suc k₂ ≡⟨ q₂ ⟩≤ m ≤⟨ p₁ ⟩ k₁ ∎≤)) -- If n is less than or equal to pred n, then n is equal to zero. ≤pred→≡zero : ∀ {n} → n ≤ pred n → n ≡ zero ≤pred→≡zero {zero} _ = refl _ ≤pred→≡zero {suc n} n<n = ⊥-elim (+≮ 0 n<n) -- The pred function is monotone. pred-mono : ∀ {m n} → m ≤ n → pred m ≤ pred n pred-mono (≤-refl′ m≡n) = ≤-refl′ (cong pred m≡n) pred-mono {m} {n} (≤-step′ {k = k} m≤k 1+k≡n) = pred m ≤⟨ pred-mono m≤k ⟩ pred k ≤⟨ pred≤ _ ⟩ k ≡⟨ cong pred 1+k≡n ⟩≤ pred n ∎≤ -- _+_ is monotone. infixl 6 _+-mono_ _+-mono_ : ∀ {m₁ m₂ n₁ n₂} → m₁ ≤ m₂ → n₁ ≤ n₂ → m₁ + n₁ ≤ m₂ + n₂ _+-mono_ {m₁} {m₂} {n₁} {n₂} (≤-refl′ eq) q = m₁ + n₁ ≡⟨ cong (_+ _) eq ⟩≤ m₂ + n₁ ≤⟨ lemma m₂ q ⟩∎ m₂ + n₂ ∎≤ where lemma : ∀ m {n k} → n ≤ k → m + n ≤ m + k lemma zero p = p lemma (suc m) p = suc≤suc (lemma m p) _+-mono_ {m₁} {m₂} {n₁} {n₂} (≤-step′ {k = k} p eq) q = m₁ + n₁ ≤⟨ p +-mono q ⟩ k + n₂ ≤⟨ ≤-step (_ ∎≤) ⟩ suc k + n₂ ≡⟨ cong (_+ _) eq ⟩≤ m₂ + n₂ ∎≤ -- _*_ is monotone. infixl 7 _*-mono_ _*-mono_ : ∀ {m₁ m₂ n₁ n₂} → m₁ ≤ m₂ → n₁ ≤ n₂ → m₁ * n₁ ≤ m₂ * n₂ _*-mono_ {zero} _ _ = zero≤ _ _*-mono_ {suc _} {zero} p q = ⊥-elim (≮0 _ p) _*-mono_ {suc _} {suc _} p q = q +-mono suc≤suc⁻¹ p *-mono q -- 1 is less than or equal to positive natural numbers raised to any -- power. 1≤+^ : ∀ {m} n → 1 ≤ suc m ^ n 1≤+^ {m} zero = ≤-refl 1≤+^ {m} (suc n) = 1 ≡⟨⟩≤ 1 + 0 ≤⟨ 1≤+^ n +-mono zero≤ _ ⟩ suc m ^ n + m * suc m ^ n ∎≤ private -- _^_ is not monotone. ¬-^-mono : ¬ (∀ {m₁ m₂ n₁ n₂} → m₁ ≤ m₂ → n₁ ≤ n₂ → m₁ ^ n₁ ≤ m₂ ^ n₂) ¬-^-mono mono = ≮0 _ ( 1 ≡⟨⟩≤ 0 ^ 0 ≤⟨ mono (≤-refl {n = 0}) (zero≤ 1) ⟩ 0 ^ 1 ≡⟨⟩≤ 0 ∎≤) -- _^_ is monotone for positive bases. infixr 8 _⁺^-mono_ _⁺^-mono_ : ∀ {m₁ m₂ n₁ n₂} → suc m₁ ≤ m₂ → n₁ ≤ n₂ → suc m₁ ^ n₁ ≤ m₂ ^ n₂ _⁺^-mono_ {m₁} {m₂ = suc m₂} {n₁ = zero} {n₂} _ _ = suc m₁ ^ 0 ≡⟨⟩≤ 1 ≤⟨ 1≤+^ n₂ ⟩ suc m₂ ^ n₂ ∎≤ _⁺^-mono_ {m₁} {m₂} {suc n₁} {suc n₂} p q = suc m₁ * suc m₁ ^ n₁ ≤⟨ p *-mono p ⁺^-mono suc≤suc⁻¹ q ⟩ m₂ * m₂ ^ n₂ ∎≤ _⁺^-mono_ {m₂ = zero} p _ = ⊥-elim (≮0 _ p) _⁺^-mono_ {n₁ = suc _} {n₂ = zero} _ q = ⊥-elim (≮0 _ q) -- _^_ is monotone for positive exponents. infixr 8 _^⁺-mono_ _^⁺-mono_ : ∀ {m₁ m₂ n₁ n₂} → m₁ ≤ m₂ → suc n₁ ≤ n₂ → m₁ ^ suc n₁ ≤ m₂ ^ n₂ _^⁺-mono_ {zero} _ _ = zero≤ _ _^⁺-mono_ {suc _} p q = p ⁺^-mono q -- A natural number raised to a positive power is greater than or -- equal to the number. ≤^+ : ∀ {m} n → m ≤ m ^ suc n ≤^+ {m} n = m ≡⟨ sym ^-right-identity ⟩≤ m ^ 1 ≤⟨ ≤-refl {n = m} ^⁺-mono suc≤suc (zero≤ n) ⟩ m ^ suc n ∎≤ -- The result of _! is always positive. 1≤! : ∀ n → 1 ≤ n ! 1≤! zero = ≤-refl 1≤! (suc n) = 1 ≡⟨⟩≤ 1 * 1 ≤⟨ suc≤suc (zero≤ n) *-mono 1≤! n ⟩ suc n * n ! ≡⟨⟩≤ suc n ! ∎≤ -- _! is monotone. infix 9 _!-mono _!-mono : ∀ {m n} → m ≤ n → m ! ≤ n ! _!-mono {zero} {n} _ = 1≤! n _!-mono {suc m} {zero} p = ⊥-elim (≮0 _ p) _!-mono {suc m} {suc n} p = suc m * m ! ≤⟨ p *-mono suc≤suc⁻¹ p !-mono ⟩ suc n * n ! ∎≤ -- An eliminator corresponding to well-founded induction. well-founded-elim : ∀ {p} (P : ℕ → Type p) → (∀ m → (∀ {n} → n < m → P n) → P m) → ∀ m → P m well-founded-elim P p m = helper (suc m) ≤-refl where helper : ∀ m {n} → n < m → P n helper zero n<0 = ⊥-elim (≮0 _ n<0) helper (suc m) (≤-step′ {k = k} n<k 1+k≡1+m) = helper m (subst (_ <_) (cancel-suc 1+k≡1+m) n<k) helper (suc m) (≤-refl′ 1+n≡1+m) = subst P (sym (cancel-suc 1+n≡1+m)) (p m (helper m)) -- The accessibility relation for _<_. data Acc (n : ℕ) : Type where acc : (∀ {m} → m < n → Acc m) → Acc n -- Every natural number is accessible. accessible : ∀ n → Acc n accessible = well-founded-elim _ λ _ → acc ------------------------------------------------------------------------ -- An alternative definition of the ordering -- The following ordering can be used to define a function by -- recursion up to some bound (see the example below). infix 4 _≤↑_ data _≤↑_ (m n : ℕ) : Type where ≤↑-refl : m ≡ n → m ≤↑ n ≤↑-step : suc m ≤↑ n → m ≤↑ n -- The successor function preserves _≤↑_. suc≤↑suc : ∀ {m n} → m ≤↑ n → suc m ≤↑ suc n suc≤↑suc (≤↑-refl m≡n) = ≤↑-refl (cong suc m≡n) suc≤↑suc (≤↑-step 1+m≤↑n) = ≤↑-step (suc≤↑suc 1+m≤↑n) -- Functions that convert between _≤_ and _≤↑_. ≤→≤↑ : ∀ {m n} → m ≤ n → m ≤↑ n ≤→≤↑ (≤-refl′ m≡n) = ≤↑-refl m≡n ≤→≤↑ (≤-step′ {k = k} m≤k 1+k≡n) = subst (_ ≤↑_) 1+k≡n (≤↑-step (suc≤↑suc (≤→≤↑ m≤k))) ≤↑→≤ : ∀ {m n} → m ≤↑ n → m ≤ n ≤↑→≤ (≤↑-refl m≡n) = ≤-refl′ m≡n ≤↑→≤ (≤↑-step 1+m≤↑n) = <→≤ (≤↑→≤ 1+m≤↑n) private -- An example: The list up-to n consists of all natural numbers from -- 0 to n, inclusive. up-to : ℕ → List ℕ up-to bound = helper 0 (≤→≤↑ (zero≤ bound)) where helper : ∀ n → n ≤↑ bound → List ℕ helper n (≤↑-refl _) = n ∷ [] helper n (≤↑-step p) = n ∷ helper _ p -- An eliminator wrapping up the technique used in the example above. up-to-elim : ∀ {p} (P : ℕ → Type p) → ∀ n → P n → (∀ m → m < n → P (suc m) → P m) → P 0 up-to-elim P n Pn P<n = helper 0 (≤→≤↑ (zero≤ n)) where helper : ∀ m → m ≤↑ n → P m helper m (≤↑-refl m≡n) = subst P (sym m≡n) Pn helper m (≤↑-step m<n) = P<n m (≤↑→≤ m<n) (helper (suc m) m<n) private -- The example, expressed using the eliminator. up-to′ : ℕ → List ℕ up-to′ bound = up-to-elim (const _) bound (bound ∷ []) (λ n _ → n ∷_) ------------------------------------------------------------------------ -- Another alternative definition of the ordering -- This ordering relation is defined by recursion on the numbers. infix 4 _≤→_ _≤→_ : ℕ → ℕ → Type zero ≤→ _ = ⊤ suc m ≤→ zero = ⊥ suc m ≤→ suc n = m ≤→ n -- Functions that convert between _≤_ and _≤→_. ≤→≤→ : ∀ m n → m ≤ n → m ≤→ n ≤→≤→ zero _ _ = _ ≤→≤→ (suc m) zero p = ≮0 _ p ≤→≤→ (suc m) (suc n) p = ≤→≤→ m n (suc≤suc⁻¹ p) ≤→→≤ : ∀ m n → m ≤→ n → m ≤ n ≤→→≤ zero n _ = zero≤ n ≤→→≤ (suc m) zero () ≤→→≤ (suc m) (suc n) p = suc≤suc (≤→→≤ m n p) ------------------------------------------------------------------------ -- Properties related to _∸_ -- Zero is a left zero of truncated subtraction. ∸-left-zero : ∀ n → 0 ∸ n ≡ 0 ∸-left-zero zero = refl _ ∸-left-zero (suc _) = refl _ -- A form of associativity for _∸_. ∸-∸-assoc : ∀ {m} n {k} → (m ∸ n) ∸ k ≡ m ∸ (n + k) ∸-∸-assoc zero = refl _ ∸-∸-assoc {zero} (suc _) {zero} = refl _ ∸-∸-assoc {zero} (suc _) {suc _} = refl _ ∸-∸-assoc {suc _} (suc n) = ∸-∸-assoc n -- A limited form of associativity for _+_ and _∸_. +-∸-assoc : ∀ {m n k} → k ≤ n → (m + n) ∸ k ≡ m + (n ∸ k) +-∸-assoc {m} {n} {zero} _ = m + n ∸ 0 ≡⟨⟩ m + n ≡⟨⟩ m + (n ∸ 0) ∎ +-∸-assoc {m} {zero} {suc k} <0 = ⊥-elim (≮0 _ <0) +-∸-assoc {m} {suc n} {suc k} 1+k≤1+n = m + suc n ∸ suc k ≡⟨ cong (_∸ suc k) (sym (suc+≡+suc m)) ⟩ suc m + n ∸ suc k ≡⟨⟩ m + n ∸ k ≡⟨ +-∸-assoc (suc≤suc⁻¹ 1+k≤1+n) ⟩ m + (n ∸ k) ≡⟨⟩ m + (suc n ∸ suc k) ∎ -- A special case of +-∸-assoc. ∸≡suc∸suc : ∀ {m n} → n < m → m ∸ n ≡ suc (m ∸ suc n) ∸≡suc∸suc = +-∸-assoc -- If you add a number and then subtract it again, then you get back -- what you started with. +∸≡ : ∀ {m} n → (m + n) ∸ n ≡ m +∸≡ {m} zero = m + 0 ≡⟨ +-right-identity ⟩∎ m ∎ +∸≡ {zero} (suc n) = +∸≡ n +∸≡ {suc m} (suc n) = m + suc n ∸ n ≡⟨ cong (_∸ n) (sym (suc+≡+suc m)) ⟩ suc m + n ∸ n ≡⟨ +∸≡ n ⟩∎ suc m ∎ -- If you subtract a number from itself, then the answer is zero. ∸≡0 : ∀ n → n ∸ n ≡ 0 ∸≡0 = +∸≡ -- If you subtract a number and then add it again, then you get back -- what you started with if the number is less than or equal to the -- number that you started with. ∸+≡ : ∀ {m n} → n ≤ m → (m ∸ n) + n ≡ m ∸+≡ {m} {n} (≤-refl′ n≡m) = (m ∸ n) + n ≡⟨ cong (λ n → m ∸ n + n) n≡m ⟩ (m ∸ m) + m ≡⟨ cong (_+ m) (∸≡0 m) ⟩ 0 + m ≡⟨⟩ m ∎ ∸+≡ {m} {n} (≤-step′ {k = k} n≤k 1+k≡m) = (m ∸ n) + n ≡⟨ cong (λ m → m ∸ n + n) (sym 1+k≡m) ⟩ (1 + k ∸ n) + n ≡⟨ cong (_+ n) (∸≡suc∸suc (suc≤suc n≤k)) ⟩ 1 + ((k ∸ n) + n) ≡⟨ cong (1 +_) (∸+≡ n≤k) ⟩ 1 + k ≡⟨ 1+k≡m ⟩∎ m ∎ -- If you subtract a number and then add it again, then you get -- something that is greater than or equal to what you started with. ≤∸+ : ∀ m n → m ≤ (m ∸ n) + n ≤∸+ m zero = m ≡⟨ sym +-right-identity ⟩≤ m + 0 ∎≤ ≤∸+ zero (suc n) = zero≤ _ ≤∸+ (suc m) (suc n) = suc m ≤⟨ suc≤suc (≤∸+ m n) ⟩ suc (m ∸ n + n) ≡⟨ suc+≡+suc _ ⟩≤ m ∸ n + suc n ∎≤ -- If you subtract something from a number you get a number that is -- less than or equal to the one you started with. ∸≤ : ∀ m n → m ∸ n ≤ m ∸≤ _ zero = ≤-refl ∸≤ zero (suc _) = ≤-refl ∸≤ (suc m) (suc n) = ≤-step (∸≤ m n) -- A kind of commutativity for _+ n and _∸ k. +-∸-comm : ∀ {m n k} → k ≤ m → (m + n) ∸ k ≡ (m ∸ k) + n +-∸-comm {m} {n} {k = zero} = const (m + n ∎) +-∸-comm {zero} {k = suc k} = ⊥-elim ∘ ≮0 k +-∸-comm {suc _} {k = suc _} = +-∸-comm ∘ suc≤suc⁻¹ -- _∸_ is monotone in its first argument and antitone in its second -- argument. infixl 6 _∸-mono_ _∸-mono_ : ∀ {m₁ m₂ n₁ n₂} → m₁ ≤ m₂ → n₂ ≤ n₁ → m₁ ∸ n₁ ≤ m₂ ∸ n₂ _∸-mono_ {n₁ = zero} {zero} p _ = p _∸-mono_ {n₁ = zero} {suc _} _ q = ⊥-elim (≮0 _ q) _∸-mono_ {zero} {n₁ = suc n₁} _ _ = zero≤ _ _∸-mono_ {suc _} {zero} {suc _} p _ = ⊥-elim (≮0 _ p) _∸-mono_ {suc m₁} {suc m₂} {suc n₁} {zero} p _ = m₁ ∸ n₁ ≤⟨ ∸≤ _ n₁ ⟩ m₁ ≤⟨ pred≤ _ ⟩ suc m₁ ≤⟨ p ⟩∎ suc m₂ ∎≤ _∸-mono_ {suc _} {suc _} {suc _} {suc _} p q = suc≤suc⁻¹ p ∸-mono suc≤suc⁻¹ q -- The predecessor function can be expressed using _∸_. pred≡∸1 : ∀ n → pred n ≡ n ∸ 1 pred≡∸1 zero = refl _ pred≡∸1 (suc _) = refl _ ------------------------------------------------------------------------ -- Minimum and maximum -- Minimum. min : ℕ → ℕ → ℕ min zero n = zero min m zero = zero min (suc m) (suc n) = suc (min m n) -- Maximum. max : ℕ → ℕ → ℕ max zero n = n max m zero = m max (suc m) (suc n) = suc (max m n) -- The min operation can be expressed using repeated truncated -- subtraction. min≡∸∸ : ∀ m n → min m n ≡ m ∸ (m ∸ n) min≡∸∸ zero n = 0 ≡⟨ sym (∸-left-zero (0 ∸ n)) ⟩∎ 0 ∸ (0 ∸ n) ∎ min≡∸∸ (suc m) zero = 0 ≡⟨ sym (∸≡0 m) ⟩∎ m ∸ m ∎ min≡∸∸ (suc m) (suc n) = suc (min m n) ≡⟨ cong suc (min≡∸∸ m n) ⟩ suc (m ∸ (m ∸ n)) ≡⟨ sym (+-∸-assoc (∸≤ _ n)) ⟩∎ suc m ∸ (m ∸ n) ∎ -- The max operation can be expressed using addition and truncated -- subtraction. max≡+∸ : ∀ m n → max m n ≡ m + (n ∸ m) max≡+∸ zero _ = refl _ max≡+∸ (suc m) zero = suc m ≡⟨ cong suc (sym +-right-identity) ⟩∎ suc (m + 0) ∎ max≡+∸ (suc m) (suc n) = cong suc (max≡+∸ m n) -- The _≤_ relation can be expressed in terms of the min function. ≤⇔min≡ : ∀ {m n} → m ≤ n ⇔ min m n ≡ m ≤⇔min≡ = record { to = to _ _ ∘ ≤→≤→ _ _; from = from _ _ } where to : ∀ m n → m ≤→ n → min m n ≡ m to zero n = const (refl zero) to (suc m) zero = λ () to (suc m) (suc n) = cong suc ∘ to m n from : ∀ m n → min m n ≡ m → m ≤ n from zero n = const (zero≤ n) from (suc m) zero = ⊥-elim ∘ 0≢+ from (suc m) (suc n) = suc≤suc ∘ from m n ∘ cancel-suc -- The _≤_ relation can be expressed in terms of the max function. ≤⇔max≡ : ∀ {m n} → m ≤ n ⇔ max m n ≡ n ≤⇔max≡ {m} = record { to = to m _ ∘ ≤→≤→ _ _; from = from _ _ } where to : ∀ m n → m ≤→ n → max m n ≡ n to zero n = const (refl n) to (suc m) zero = λ () to (suc m) (suc n) = cong suc ∘ to m n from : ∀ m n → max m n ≡ n → m ≤ n from zero n = const (zero≤ n) from (suc m) zero = ⊥-elim ∘ 0≢+ ∘ sym from (suc m) (suc n) = suc≤suc ∘ from m n ∘ cancel-suc -- Given two numbers one is the minimum and the other is the maximum. min-and-max : ∀ m n → min m n ≡ m × max m n ≡ n ⊎ min m n ≡ n × max m n ≡ m min-and-max zero _ = inj₁ (refl _ , refl _) min-and-max (suc _) zero = inj₂ (refl _ , refl _) min-and-max (suc m) (suc n) = ⊎-map (Σ-map (cong suc) (cong suc)) (Σ-map (cong suc) (cong suc)) (min-and-max m n) -- The min operation is idempotent. min-idempotent : ∀ n → min n n ≡ n min-idempotent n = case min-and-max n n of [ proj₁ , proj₁ ] -- The max operation is idempotent. max-idempotent : ∀ n → max n n ≡ n max-idempotent n = case min-and-max n n of [ proj₂ , proj₂ ] -- The min operation is commutative. min-comm : ∀ m n → min m n ≡ min n m min-comm zero zero = refl _ min-comm zero (suc _) = refl _ min-comm (suc _) zero = refl _ min-comm (suc m) (suc n) = cong suc (min-comm m n) -- The max operation is commutative. max-comm : ∀ m n → max m n ≡ max n m max-comm zero zero = refl _ max-comm zero (suc _) = refl _ max-comm (suc _) zero = refl _ max-comm (suc m) (suc n) = cong suc (max-comm m n) -- The min operation is associative. min-assoc : ∀ m {n o} → min m (min n o) ≡ min (min m n) o min-assoc zero = refl _ min-assoc (suc m) {n = zero} = refl _ min-assoc (suc m) {n = suc n} {o = zero} = refl _ min-assoc (suc m) {n = suc n} {o = suc o} = cong suc (min-assoc m) -- The max operation is associative. max-assoc : ∀ m {n o} → max m (max n o) ≡ max (max m n) o max-assoc zero = refl _ max-assoc (suc m) {n = zero} = refl _ max-assoc (suc m) {n = suc n} {o = zero} = refl _ max-assoc (suc m) {n = suc n} {o = suc o} = cong suc (max-assoc m) -- The minimum is less than or equal to both arguments. min≤ˡ : ∀ m n → min m n ≤ m min≤ˡ zero _ = ≤-refl min≤ˡ (suc _) zero = zero≤ _ min≤ˡ (suc m) (suc n) = suc≤suc (min≤ˡ m n) min≤ʳ : ∀ m n → min m n ≤ n min≤ʳ m n = min m n ≡⟨ min-comm _ _ ⟩≤ min n m ≤⟨ min≤ˡ _ _ ⟩∎ n ∎≤ -- The maximum is greater than or equal to both arguments. ˡ≤max : ∀ m n → m ≤ max m n ˡ≤max zero _ = zero≤ _ ˡ≤max (suc _) zero = ≤-refl ˡ≤max (suc m) (suc n) = suc≤suc (ˡ≤max m n) ʳ≤max : ∀ m n → n ≤ max m n ʳ≤max m n = n ≤⟨ ˡ≤max _ _ ⟩ max n m ≡⟨ max-comm n _ ⟩≤ max m n ∎≤ -- A kind of distributivity property for max and _∸_. max-∸ : ∀ m n o → max (m ∸ o) (n ∸ o) ≡ max m n ∸ o max-∸ m n zero = refl _ max-∸ zero n (suc o) = refl _ max-∸ (suc m) (suc n) (suc o) = max-∸ m n o max-∸ (suc m) zero (suc o) = max (m ∸ o) 0 ≡⟨ max-comm _ 0 ⟩ m ∸ o ∎ ------------------------------------------------------------------------ -- Division by two -- Division by two, rounded upwards. ⌈_/2⌉ : ℕ → ℕ ⌈ zero /2⌉ = zero ⌈ suc zero /2⌉ = suc zero ⌈ suc (suc n) /2⌉ = suc ⌈ n /2⌉ -- Division by two, rounded downwards. ⌊_/2⌋ : ℕ → ℕ ⌊ zero /2⌋ = zero ⌊ suc zero /2⌋ = zero ⌊ suc (suc n) /2⌋ = suc ⌊ n /2⌋ -- Some simple lemmas related to division by two. ⌈/2⌉+⌊/2⌋ : ∀ n → ⌈ n /2⌉ + ⌊ n /2⌋ ≡ n ⌈/2⌉+⌊/2⌋ zero = refl _ ⌈/2⌉+⌊/2⌋ (suc zero) = refl _ ⌈/2⌉+⌊/2⌋ (suc (suc n)) = suc ⌈ n /2⌉ + suc ⌊ n /2⌋ ≡⟨ cong suc (sym (suc+≡+suc _)) ⟩ suc (suc (⌈ n /2⌉ + ⌊ n /2⌋)) ≡⟨ cong (suc ∘ suc) (⌈/2⌉+⌊/2⌋ n) ⟩∎ suc (suc n) ∎ ⌊/2⌋≤⌈/2⌉ : ∀ n → ⌊ n /2⌋ ≤ ⌈ n /2⌉ ⌊/2⌋≤⌈/2⌉ zero = ≤-refl ⌊/2⌋≤⌈/2⌉ (suc zero) = zero≤ 1 ⌊/2⌋≤⌈/2⌉ (suc (suc n)) = suc≤suc (⌊/2⌋≤⌈/2⌉ n) ⌈/2⌉≤ : ∀ n → ⌈ n /2⌉ ≤ n ⌈/2⌉≤ zero = ≤-refl ⌈/2⌉≤ (suc zero) = ≤-refl ⌈/2⌉≤ (suc (suc n)) = suc ⌈ n /2⌉ ≤⟨ suc≤suc (⌈/2⌉≤ n) ⟩ suc n ≤⟨ m≤n+m _ 1 ⟩∎ suc (suc n) ∎≤ ⌈/2⌉< : ∀ n → ⌈ 2 + n /2⌉ < 2 + n ⌈/2⌉< n = 2 + ⌈ n /2⌉ ≤⟨ ≤-refl +-mono ⌈/2⌉≤ n ⟩∎ 2 + n ∎≤ ⌊/2⌋< : ∀ n → ⌊ 1 + n /2⌋ < 1 + n ⌊/2⌋< zero = ≤-refl ⌊/2⌋< (suc n) = 2 + ⌊ n /2⌋ ≤⟨ ≤-refl +-mono ⌊/2⌋≤⌈/2⌉ n ⟩ 2 + ⌈ n /2⌉ ≤⟨ ⌈/2⌉< n ⟩∎ 2 + n ∎≤ ⌈2*+/2⌉≡ : ∀ m {n} → ⌈ 2 * m + n /2⌉ ≡ m + ⌈ n /2⌉ ⌈2*+/2⌉≡ zero = refl _ ⌈2*+/2⌉≡ (suc m) {n = n} = ⌈ 2 * (1 + m) + n /2⌉ ≡⟨ cong (λ m → ⌈ m + n /2⌉) (*-+-distribˡ 2 {n = 1}) ⟩ ⌈ 2 + 2 * m + n /2⌉ ≡⟨⟩ 1 + ⌈ 2 * m + n /2⌉ ≡⟨ cong suc (⌈2*+/2⌉≡ m) ⟩∎ 1 + m + ⌈ n /2⌉ ∎ ⌊2*+/2⌋≡ : ∀ m {n} → ⌊ 2 * m + n /2⌋ ≡ m + ⌊ n /2⌋ ⌊2*+/2⌋≡ zero = refl _ ⌊2*+/2⌋≡ (suc m) {n = n} = ⌊ 2 * (1 + m) + n /2⌋ ≡⟨ cong (λ m → ⌊ m + n /2⌋) (*-+-distribˡ 2 {n = 1}) ⟩ ⌊ 2 + 2 * m + n /2⌋ ≡⟨⟩ 1 + ⌊ 2 * m + n /2⌋ ≡⟨ cong suc (⌊2*+/2⌋≡ m) ⟩∎ 1 + m + ⌊ n /2⌋ ∎ ⌈2*/2⌉≡ : ∀ n → ⌈ 2 * n /2⌉ ≡ n ⌈2*/2⌉≡ n = ⌈ 2 * n /2⌉ ≡⟨ cong ⌈_/2⌉ $ sym +-right-identity ⟩ ⌈ 2 * n + 0 /2⌉ ≡⟨ ⌈2*+/2⌉≡ n ⟩ n + ⌈ 0 /2⌉ ≡⟨⟩ n + 0 ≡⟨ +-right-identity ⟩∎ n ∎ ⌈1+2*/2⌉≡ : ∀ n → ⌈ 1 + 2 * n /2⌉ ≡ 1 + n ⌈1+2*/2⌉≡ n = ⌈ 1 + 2 * n /2⌉ ≡⟨ cong ⌈_/2⌉ $ +-comm 1 ⟩ ⌈ 2 * n + 1 /2⌉ ≡⟨ ⌈2*+/2⌉≡ n ⟩ n + ⌈ 1 /2⌉ ≡⟨⟩ n + 1 ≡⟨ +-comm n ⟩∎ 1 + n ∎ ⌊2*/2⌋≡ : ∀ n → ⌊ 2 * n /2⌋ ≡ n ⌊2*/2⌋≡ n = ⌊ 2 * n /2⌋ ≡⟨ cong ⌊_/2⌋ $ sym +-right-identity ⟩ ⌊ 2 * n + 0 /2⌋ ≡⟨ ⌊2*+/2⌋≡ n ⟩ n + ⌊ 0 /2⌋ ≡⟨⟩ n + 0 ≡⟨ +-right-identity ⟩∎ n ∎ ⌊1+2*/2⌋≡ : ∀ n → ⌊ 1 + 2 * n /2⌋ ≡ n ⌊1+2*/2⌋≡ n = ⌊ 1 + 2 * n /2⌋ ≡⟨ cong ⌊_/2⌋ $ +-comm 1 ⟩ ⌊ 2 * n + 1 /2⌋ ≡⟨ ⌊2*+/2⌋≡ n ⟩ n + ⌊ 1 /2⌋ ≡⟨⟩ n + 0 ≡⟨ +-right-identity ⟩∎ n ∎
{-# OPTIONS --safe --experimental-lossy-unification #-} module Cubical.Algebra.GradedRing.Instances.TrivialGradedRing where open import Cubical.Foundations.Prelude open import Cubical.Foundations.Isomorphism open import Cubical.Foundations.Equiv open import Cubical.Data.Unit open import Cubical.Data.Nat using (ℕ ; zero ; suc ; +-assoc ; +-zero) open import Cubical.Data.Sigma open import Cubical.Algebra.Monoid open import Cubical.Algebra.Monoid.Instances.Nat open import Cubical.Algebra.AbGroup open import Cubical.Algebra.AbGroup.Instances.Unit open import Cubical.Algebra.DirectSum.DirectSumHIT.Base open import Cubical.Algebra.Ring open import Cubical.Algebra.GradedRing.Base open import Cubical.Algebra.GradedRing.DirectSumHIT private variable ℓ : Level open Iso open GradedRing-⊕HIT-index open GradedRing-⊕HIT-⋆ module _ (ARing@(A , Astr) : Ring ℓ) where open RingStr Astr open RingTheory ARing private G : ℕ → Type ℓ G zero = A G (suc k) = Unit* Gstr : (k : ℕ) → AbGroupStr (G k) Gstr zero = snd (Ring→AbGroup ARing) Gstr (suc k) = snd UnitAbGroup ⋆ : {k l : ℕ} → G k → G l → G (k Cubical.Data.Nat.+ l) ⋆ {zero} {zero} x y = x · y ⋆ {zero} {suc l} x y = tt* ⋆ {suc k} {l} x y = tt* 0-⋆ : {k l : ℕ} (b : G l) → ⋆ (AbGroupStr.0g (Gstr k)) b ≡ AbGroupStr.0g (Gstr (k Cubical.Data.Nat.+ l)) 0-⋆ {zero} {zero} b = 0LeftAnnihilates _ 0-⋆ {zero} {suc l} b = refl 0-⋆ {suc k} {l} b = refl ⋆-0 : {k l : ℕ} (a : G k) → ⋆ a (AbGroupStr.0g (Gstr l)) ≡ AbGroupStr.0g (Gstr (k Cubical.Data.Nat.+ l)) ⋆-0 {zero} {zero} a = 0RightAnnihilates _ ⋆-0 {zero} {suc l} a = refl ⋆-0 {suc k} {l} a = refl ⋆Assoc : {k l m : ℕ} (a : G k) (b : G l) (c : G m) → _≡_ {A = Σ[ k ∈ ℕ ] G k} (k Cubical.Data.Nat.+ (l Cubical.Data.Nat.+ m) , ⋆ a (⋆ b c)) (k Cubical.Data.Nat.+ l Cubical.Data.Nat.+ m , ⋆ (⋆ a b) c) ⋆Assoc {zero} {zero} {zero} a b c = ΣPathTransport→PathΣ _ _ (+-assoc _ _ _ , transportRefl _ ∙ ·Assoc _ _ _) ⋆Assoc {zero} {zero} {suc m} a b c = ΣPathTransport→PathΣ _ _ (+-assoc _ _ _ , transportRefl _) ⋆Assoc {zero} {suc l} {m} a b c = ΣPathTransport→PathΣ _ _ (+-assoc _ _ _ , transportRefl _) ⋆Assoc {suc k} {l} {m} a b c = ΣPathTransport→PathΣ _ _ (+-assoc _ _ _ , transportRefl _) ⋆IdR : {k : ℕ} (a : G k) → _≡_ {A = Σ[ k ∈ ℕ ] G k} (k Cubical.Data.Nat.+ 0 , ⋆ a 1r) (k , a) ⋆IdR {zero} a = ΣPathTransport→PathΣ _ _ (refl , (transportRefl _ ∙ ·IdR _)) ⋆IdR {suc k} a = ΣPathTransport→PathΣ _ _ ((+-zero _) , (transportRefl _)) ⋆IdL : {l : ℕ} (b : G l) → _≡_ {A = Σ[ k ∈ ℕ ] G k} (l , ⋆ 1r b) (l , b) ⋆IdL {zero} b = ΣPathTransport→PathΣ _ _ (refl , (transportRefl _ ∙ ·IdL _)) ⋆IdL {suc l} b = ΣPathTransport→PathΣ _ _ (refl , (transportRefl _)) ⋆DistR+ : {k l : ℕ} (a : G k) (b c : G l) → ⋆ a (Gstr l .AbGroupStr._+_ b c) ≡ Gstr (k Cubical.Data.Nat.+ l) .AbGroupStr._+_ (⋆ a b) (⋆ a c) ⋆DistR+ {zero} {zero} a b c = ·DistR+ _ _ _ ⋆DistR+ {zero} {suc l} a b c = refl ⋆DistR+ {suc k} {l} a b c = refl ⋆DistL+ : {k l : ℕ} (a b : G k) (c : G l) → ⋆ (Gstr k .AbGroupStr._+_ a b) c ≡ Gstr (k Cubical.Data.Nat.+ l) .AbGroupStr._+_ (⋆ a c) (⋆ b c) ⋆DistL+ {zero} {zero} a b c = ·DistL+ _ _ _ ⋆DistL+ {zero} {suc l} a b c = refl ⋆DistL+ {suc k} {l} a b c = refl trivialGradedRing : GradedRing ℓ-zero ℓ trivialGradedRing = makeGradedRing ARing NatMonoid G Gstr 1r ⋆ 0-⋆ ⋆-0 ⋆Assoc ⋆IdR ⋆IdL ⋆DistR+ ⋆DistL+ equivRing where equivRing : RingEquiv ARing (⊕HITgradedRing-Ring NatMonoid G Gstr 1r ⋆ 0-⋆ ⋆-0 ⋆Assoc ⋆IdR ⋆IdL ⋆DistR+ ⋆DistL+) fst equivRing = isoToEquiv is where is : Iso A (⊕HIT ℕ G Gstr) fun is a = base 0 a inv is = DS-Rec-Set.f _ _ _ _ is-set 0r (λ {zero a → a ; (suc k) a → 0r }) _+_ +Assoc +IdR +Comm (λ { zero → refl ; (suc k) → refl}) (λ {zero a b → refl ; (suc n) a b → +IdR _}) rightInv is = DS-Ind-Prop.f _ _ _ _ (λ _ → trunc _ _) (base-neutral _) (λ { zero a → refl ; (suc k) a → base-neutral _ ∙ sym (base-neutral _)} ) λ {U V} ind-U ind-V → sym (base-add _ _ _) ∙ cong₂ _add_ ind-U ind-V leftInv is = λ _ → refl snd equivRing = makeIsRingHom -- issue agda have trouble infering the Idx, G, Gstr {R = ARing} {S = ⊕HITgradedRing-Ring NatMonoid G Gstr 1r ⋆ 0-⋆ ⋆-0 ⋆Assoc ⋆IdR ⋆IdL ⋆DistR+ ⋆DistL+} {f = λ a → base 0 a} refl (λ _ _ → sym (base-add _ _ _)) λ _ _ → refl
{-# OPTIONS --allow-unsolved-metas #-} open import Oscar.Class open import Oscar.Class.Reflexivity open import Oscar.Class.Symmetry open import Oscar.Class.Transitivity open import Oscar.Class.Transleftidentity open import Oscar.Prelude module Test.ProblemWithDerivation-5 where module Map {𝔵₁} {𝔛₁ : Ø 𝔵₁} {𝔵₂} {𝔛₂ : Ø 𝔵₂} {𝔯₁₂} {𝔯₁₂'} (ℜ₁₂ : 𝔛₁ → 𝔛₂ → Ø 𝔯₁₂) (ℜ₁₂' : 𝔛₁ → 𝔛₂ → Ø 𝔯₁₂') = ℭLASS (ℜ₁₂ , ℜ₁₂') (∀ P₁ P₂ → ℜ₁₂ P₁ P₂ → ℜ₁₂' P₁ P₂) module _ {𝔵₁} {𝔛₁ : Ø 𝔵₁} {𝔵₂} {𝔛₂ : Ø 𝔵₂} {𝔯₁₂} {ℜ₁₂ : 𝔛₁ → 𝔛₂ → Ø 𝔯₁₂} {𝔯₁₂'} {ℜ₁₂' : 𝔛₁ → 𝔛₂ → Ø 𝔯₁₂'} where map = Map.method ℜ₁₂ ℜ₁₂' module _ {𝔵₁} {𝔛₁ : Ø 𝔵₁} {𝔯₁₂} {ℜ₁₂ : 𝔛₁ → 𝔛₁ → Ø 𝔯₁₂} {𝔯₁₂'} {ℜ₁₂' : 𝔛₁ → 𝔛₁ → Ø 𝔯₁₂'} where smap = Map.method ℜ₁₂ ℜ₁₂' -- FIXME this looks like a viable solution; but what if `-MapFromTransleftidentity` builds something addressable by smap (i.e., where 𝔛₁ ≡ 𝔛₂)? module _ {𝔵₁} {𝔛₁ : Ø 𝔵₁} {𝔵₂} {𝔛₂ : Ø 𝔵₂} {𝔯₁₂} {ℜ₁₂ : 𝔛₁ → 𝔛₂ → Ø 𝔯₁₂} where hmap = Map.method ℜ₁₂ ℜ₁₂ postulate A : Set B : Set _~A~_ : A → A → Set _~B~_ : B → B → Set s1 : A → B f1 : ∀ {x y} → x ~A~ y → s1 x ~B~ s1 y instance 𝓢urjectivity1 : Map.class {𝔛₁ = A} {𝔛₂ = A} _~A~_ (λ x y → s1 x ~B~ s1 y) 𝓢urjectivity1 .⋆ _ _ = f1 -- Oscar.Class.Hmap.Transleftidentity instance postulate -MapFromTransleftidentity : ∀ {𝔵} {𝔛 : Ø 𝔵} {𝔞} {𝔄 : 𝔛 → Ø 𝔞} {𝔟} {𝔅 : 𝔛 → Ø 𝔟} (let _∼_ = Arrow 𝔄 𝔅) {ℓ̇} {_∼̇_ : ∀ {x y} → x ∼ y → x ∼ y → Ø ℓ̇} {transitivity : Transitivity.type _∼_} {reflexivity : Reflexivity.type _∼_} {ℓ} ⦃ _ : Transleftidentity.class _∼_ _∼̇_ reflexivity transitivity ⦄ → ∀ {m n} → Map.class {𝔛₁ = Arrow 𝔄 𝔅 m n} {𝔛₂ = LeftExtensionṖroperty ℓ (Arrow 𝔄 𝔅) _∼̇_ m} (λ f P → π₀ (π₀ P) f) (λ f P → π₀ (π₀ P) (transitivity f reflexivity)) test-before : ∀ {x y} → x ~A~ y → s1 x ~B~ s1 y test-before = map _ _ postulate XX : Set BB : XX → Set EQ : ∀ {x y} → Arrow BB BB x y → Arrow BB BB x y → Set instance postulate -transleftidentity : Transleftidentity.class (Arrow BB BB) EQ (λ x₁ → magic) (λ x₁ x₂ x₃ → magic) test-after : ∀ {x y} → x ~A~ y → s1 x ~B~ s1 y test-after {x} {y} = map _ _ -- FIXME yellow test-after-s : ∀ {x y} → x ~A~ y → s1 x ~B~ s1 y test-after-s {x} {y} = smap _ _ testr : ∀ {m n ℓ} (P₁ : Arrow BB BB m n) (P₂ : LeftExtensionṖroperty ℓ (Arrow BB BB) EQ m) → π₀ (π₀ P₂) P₁ → π₀ (π₀ P₂) (flip {∅̂} {∅̂} {∅̂} {Arrow (λ z → BB z) (λ z → BB z) m n} {Arrow (λ z → BB z) (λ z → BB z) n n} {λ _ _ → Arrow BB BB m n} (λ _ _ _ → magic) (λ _ → magic) P₁) testr = map
module CS410-Vec where open import CS410-Prelude open import CS410-Nat data Vec (X : Set) : Nat -> Set where [] : Vec X 0 _::_ : forall {n} -> X -> Vec X n -> Vec X (suc n) infixr 3 _::_ _+V_ : forall {X m n} -> Vec X m -> Vec X n -> Vec X (m +N n) [] +V ys = ys (x :: xs) +V ys = x :: xs +V ys infixr 3 _+V_ data Choppable {X : Set}(m n : Nat) : Vec X (m +N n) -> Set where chopTo : (xs : Vec X m)(ys : Vec X n) -> Choppable m n (xs +V ys) chop : {X : Set}(m n : Nat)(xs : Vec X (m +N n)) -> Choppable m n xs chop zero n xs = chopTo [] xs chop (suc m) n (x :: xs) with chop m n xs chop (suc m) n (x :: .(xs +V ys)) | chopTo xs ys = chopTo (x :: xs) ys chopParts : {X : Set}(m n : Nat)(xs : Vec X (m +N n)) -> Vec X m * Vec X n chopParts m n xs with chop m n xs chopParts m n .(xs +V ys) | chopTo xs ys = xs , ys vec : forall {n X} -> X -> Vec X n vec {zero} x = [] vec {suc n} x = x :: vec x vapp : forall {n X Y} -> Vec (X -> Y) n -> Vec X n -> Vec Y n vapp [] [] = [] vapp (f :: fs) (x :: xs) = f x :: vapp fs xs
-- Andreas, issue 2349 -- Andreas, 2016-12-20, issue #2350 -- {-# OPTIONS -v tc.term.con:50 #-} postulate A : Set data D {{a : A}} : Set where c : D test : {{a b : A}} → D test {{a}} = c {{a}} -- WAS: complaint about unsolvable instance -- Should succeed
------------------------------------------------------------------------------ -- Discussion about the inductive approach ------------------------------------------------------------------------------ {-# OPTIONS --exact-split #-} {-# OPTIONS --no-sized-types #-} {-# OPTIONS --no-universe-polymorphism #-} {-# OPTIONS --without-K #-} -- Peter: If you take away the proof objects (as you do when you go to -- predicate logic) the K axiom doesn't give you any extra power. module FOT.FOTC.InductiveApproach.ProofTerm where open import FOTC.Base ------------------------------------------------------------------------------ foo : (∃ λ x → ∃ λ y → x ≡ y) → (∃ λ x → ∃ λ y → y ≡ x) foo (x , .x , refl) = x , x , refl
open import Agda.Builtin.Equality postulate Ty Cxt : Set Var Tm : Ty → Cxt → Set _≤_ : (Γ Δ : Cxt) → Set variable Γ Δ Φ : Cxt A B C : Ty x : Var A Γ Mon : (P : Cxt → Set) → Set Mon P = ∀{Δ Γ} (ρ : Δ ≤ Γ) → P Γ → P Δ postulate _•_ : Mon (_≤ Φ) monVar : Mon (Var A) monTm : Mon (Tm A) postulate refl-ish : {A : Set} {x y : A} → x ≡ y variable ρ ρ' : Δ ≤ Γ monVar-comp : monVar ρ (monVar ρ' x) ≡ monVar (ρ • ρ') x monVar-comp {ρ = ρ} {ρ' = ρ'} {x = x} = refl-ish variable t : Tm A Γ monTm-comp : monTm ρ (monTm ρ' t) ≡ monTm (ρ • ρ') t monTm-comp {ρ = ρ} {ρ' = ρ'} {t = t} = refl-ish
module Prelude.List.Properties where open import Prelude.Function open import Prelude.Bool open import Prelude.Bool.Properties open import Prelude.Nat open import Prelude.Nat.Properties open import Prelude.Semiring open import Prelude.List.Base open import Prelude.Decidable open import Prelude.Monoid open import Prelude.Semigroup open import Prelude.Equality open import Prelude.Equality.Inspect open import Prelude.Variables open import Prelude.Strict foldr-map-fusion : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} (f : B → C → C) (g : A → B) (z : C) (xs : List A) → foldr f z (map g xs) ≡ foldr (f ∘ g) z xs foldr-map-fusion f g z [] = refl foldr-map-fusion f g z (x ∷ xs) rewrite foldr-map-fusion f g z xs = refl foldl-map-fusion : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} (f : C → B → C) (g : A → B) (z : C) (xs : List A) → foldl f z (map g xs) ≡ foldl (λ x y → f x (g y)) z xs foldl-map-fusion f g z [] = refl foldl-map-fusion f g z (x ∷ xs) rewrite foldl-map-fusion f g (f z (g x)) xs = refl foldl-assoc : ∀ {a} {A : Set a} (f : A → A → A) → (∀ x y z → f x (f y z) ≡ f (f x y) z) → ∀ y z xs → foldl f (f y z) xs ≡ f y (foldl f z xs) foldl-assoc f assoc y z [] = refl foldl-assoc f assoc y z (x ∷ xs) rewrite sym (assoc y z x) = foldl-assoc f assoc y (f z x) xs foldl-foldr : ∀ {a} {A : Set a} (f : A → A → A) (z : A) → (∀ x y z → f x (f y z) ≡ f (f x y) z) → (∀ x → f z x ≡ x) → (∀ x → f x z ≡ x) → ∀ xs → foldl f z xs ≡ foldr f z xs foldl-foldr f z assoc idl idr [] = refl foldl-foldr f z assoc idl idr (x ∷ xs) rewrite sym (foldl-foldr f z assoc idl idr xs) | idl x ⟨≡⟩ʳ idr x = foldl-assoc f assoc x z xs foldl!-foldl : ∀ {a b} {A : Set a} {B : Set b} (f : B → A → B) z (xs : List A) → foldl! f z xs ≡ foldl f z xs foldl!-foldl f z [] = refl foldl!-foldl f z (x ∷ xs) = forceLemma (f z x) (λ z′ → foldl! f z′ xs) ⟨≡⟩ foldl!-foldl f (f z x) xs -- Properties of _++_ map-++ : ∀ {a b} {A : Set a} {B : Set b} (f : A → B) (xs ys : List A) → map f (xs ++ ys) ≡ map f xs ++ map f ys map-++ f [] ys = refl map-++ f (x ∷ xs) ys = f x ∷_ $≡ map-++ f xs ys product-++ : (xs ys : List Nat) → productR (xs ++ ys) ≡ productR xs * productR ys product-++ [] ys = sym (add-zero-r _) product-++ (x ∷ xs) ys = x *_ $≡ product-++ xs ys ⟨≡⟩ mul-assoc x _ _
------------------------------------------------------------------------ -- The Agda standard library -- -- M-types (the dual of W-types) ------------------------------------------------------------------------ module Data.M where open import Level open import Coinduction -- The family of M-types. data M {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where inf : (x : A) (f : B x → ∞ (M A B)) → M A B -- Projections. head : ∀ {a b} {A : Set a} {B : A → Set b} → M A B → A head (inf x f) = x tail : ∀ {a b} {A : Set a} {B : A → Set b} → (x : M A B) → B (head x) → M A B tail (inf x f) b = ♭ (f b)
module UnifyMguPair where open import UnifyTerm open import UnifyMgu open import UnifyMguCorrect open import Data.Fin using (Fin; suc; zero) open import Data.Nat hiding (_≤_) open import Relation.Binary.PropositionalEquality renaming ([_] to [[_]]) open import Function using (_∘_; id; case_of_; _$_) open import Relation.Nullary open import Data.Product open import Data.Empty open import Data.Maybe open import Data.Sum l1 : ∀ m → Data.Fin.toℕ (Data.Fin.fromℕ m) ≡ m l1 zero = refl l1 (suc m) = cong suc (l1 m) l2 : ∀ m → m ≡ m + 0 l2 zero = refl l2 (suc m) = cong suc (l2 m) fixup : ∀ m → Fin (Data.Fin.toℕ (Data.Fin.fromℕ m) + m) → Fin (m + (m + 0)) fixup m x rewrite l1 m | sym (l2 m) = x revise-down : ∀ {m} → Fin m → Fin (2 * m) revise-down {m} x = Data.Fin.inject+ (m + 0) x revise-up : ∀ {m} → Fin m → Fin (2 * m) revise-up {m} x = fixup m (Data.Fin.fromℕ m Data.Fin.+ x) write-variable-down : ∀ {m} → Term m → Term (2 * m) write-variable-down {m} (i l) = i $ revise-down l write-variable-down {m} leaf = leaf write-variable-down {m} (s fork t) = write-variable-down s fork write-variable-down t write-variable-up : ∀ {m} → Term m → Term (2 * m) write-variable-up {m} (i r) = i (revise-up r) write-variable-up {m} leaf = leaf write-variable-up {m} (s fork t) = write-variable-up s fork write-variable-up t write-variables-apart : ∀ {m} (s t : Term m) → Term (2 * m) × Term (2 * m) write-variables-apart s t = write-variable-down s , write-variable-up t separate-substitutions-down : ∀ {m n} → (Fin (2 * m) → Term n) → Fin m → Term n separate-substitutions-down {m} f x = f $ revise-down x separate-substitutions-up : ∀ {m n} → (Fin (2 * m) → Term n) → Fin m → Term n separate-substitutions-up {m} f x = f $ revise-up x separate-substitutions : ∀ {m n} → (Fin (2 * m) → Term n) → (Fin m → Term n) × (Fin m → Term n) separate-substitutions {m} x = separate-substitutions-down {m} x , separate-substitutions-up {m} x write≡separate : ∀ {m n} (σ : AList (2 * m) n) (t : Term m) → (sub σ ◃_) (write-variable-down t) ≡ ((separate-substitutions-down $ sub σ) ◃_) t write≡separate {zero} {.0} anil (i x) = refl write≡separate {suc m} {.(suc (m + suc (m + 0)))} anil (i x) = refl write≡separate {suc m} {n} (σ asnoc t' / x) (i x₁) = refl write≡separate σ leaf = refl write≡separate σ (t₁ fork t₂) = cong₂ _fork_ (write≡separate σ t₁) (write≡separate σ t₂) write≡separate' : ∀ {m n} (σ : AList (2 * m) n) (t : Term m) → (sub σ ◃_) (write-variable-down t) ≡ ((separate-substitutions-down $ sub σ) ◃_) t write≡separate' {zero} {.0} anil (i x) = refl write≡separate' {suc m} {.(suc (m + suc (m + 0)))} anil (i x) = refl write≡separate' {suc m} {n} (σ asnoc t' / x) (i x₁) = refl write≡separate' σ leaf = refl write≡separate' σ (t₁ fork t₂) = cong₂ _fork_ (write≡separate σ t₁) (write≡separate σ t₂) Property'2 : (m : ℕ) -> Set1 Property'2 m = ∀ {n} -> (Fin (2 * m) -> Term n) -> Set Nothing'2 : ∀{m} -> (P : Property'2 m) -> Set Nothing'2 P = ∀{n} f -> P {n} f -> ⊥ Unifies'2 : ∀ {m} (s t : Term m) -> Property'2 m Unifies'2 s t f = let --s' , t' = write-variables-apart s t f₁ , f₂ = separate-substitutions f in f₁ ◃ s ≡ f₂ ◃ t pair-mgu' : ∀ {m} -> (s t : Term m) -> Maybe (∃ (AList (2 * m))) pair-mgu' {m} s t = let s' , t' = write-variables-apart s t mgu' = mgu s' t' in mgu' up-equality : ∀ {m n} {f : (2 * m) ~> n} (t : Term m) → (f ∘ revise-up) ◃ t ≡ f ◃ write-variable-up t up-equality (i x) = refl up-equality leaf = refl up-equality (t₁ fork t₂) = cong₂ _fork_ (up-equality t₁) (up-equality t₂) down-equality : ∀ {m n} {f : (2 * m) ~> n} (t : Term m) → (f ∘ revise-down) ◃ t ≡ f ◃ write-variable-down t down-equality (i x) = refl down-equality leaf = refl down-equality (t₁ fork t₂) = cong₂ _fork_ (down-equality t₁) (down-equality t₂) revise-to-write : ∀ {m n} {f : (2 * m) ~> n} (s t : Term m) → (f ∘ revise-down) ◃ s ≡ (f ∘ revise-up) ◃ t → f ◃ write-variable-down s ≡ f ◃ write-variable-up t revise-to-write (i x) (i x₁) x₂ = x₂ revise-to-write (i x) leaf x₁ = x₁ revise-to-write (i x) (t fork t₁) x₁ = trans x₁ (up-equality (t fork t₁)) revise-to-write leaf (i x) x₁ = x₁ revise-to-write leaf leaf x = refl revise-to-write leaf (t₁ fork t₂) x = trans x (up-equality (t₁ fork t₂)) revise-to-write (s₁ fork s₂) (i x) x₁ = trans (sym (down-equality (s₁ fork s₂))) x₁ revise-to-write (s₁ fork s₂) leaf x = trans (sym (down-equality (s₁ fork s₂))) x revise-to-write (s₁ fork s₂) (t₁ fork t₂) x = trans (trans (sym (down-equality (s₁ fork s₂))) x) (up-equality (t₁ fork t₂)) write-to-revise : ∀ {m n} {f : (2 * m) ~> n} (s t : Term m) → f ◃ write-variable-down s ≡ f ◃ write-variable-up t → (f ∘ revise-down) ◃ s ≡ (f ∘ revise-up) ◃ t write-to-revise (i x) (i x₁) x₂ = x₂ write-to-revise (i x) leaf x₁ = x₁ write-to-revise (i x) (t fork t₁) x₁ = trans x₁ (sym (up-equality (t fork t₁))) write-to-revise leaf (i x) x₁ = x₁ write-to-revise leaf leaf x = refl write-to-revise leaf (t fork t₁) x = trans x (sym (up-equality (t fork t₁))) write-to-revise (s₁ fork s₂) (i x) x₁ = trans ((down-equality (s₁ fork s₂))) x₁ write-to-revise (s₁ fork s₂) leaf x = trans ((down-equality (s₁ fork s₂))) x write-to-revise (s₁ fork s₂) (t fork t₁) x = trans (trans ((down-equality (s₁ fork s₂))) x) (sym (up-equality (t fork t₁))) pair-mgu-c' : ∀ {m} (s t : Term m) -> (∃ λ n → ∃ λ σ → (Max⋆ (Unifies'2 s t)) (sub σ) × pair-mgu' s t ≡ just (n , σ)) ⊎ (Nothing⋆ (Unifies'2 s t) × pair-mgu' s t ≡ nothing) pair-mgu-c' {m} s t with write-variable-down s | write-variable-up t | inspect write-variable-down s | inspect write-variable-up t … | s' | t' | [[ refl ]] | [[ refl ]] with mgu-c s' t' … | (inj₁ (n , σ , (σ◃s'=σ◃t' , max-σ) , amgu=just)) = inj₁ $ n , σ , ( write-to-revise s t σ◃s'=σ◃t' , ( λ {n'} f f◃s≡f◃t → (proj₁ $ max-σ f (revise-to-write s t f◃s≡f◃t)) , (λ x → proj₂ (max-σ f (revise-to-write s t f◃s≡f◃t)) x) ) ) , amgu=just … | (inj₂ (notunified , amgu=nothing)) = inj₂ ((λ {n} f x → notunified f (revise-to-write s t x)) , amgu=nothing)
open import TakeDropDec
{-# OPTIONS --omega-in-omega --no-termination-check #-} module Light.Variable.Sets where open import Light.Variable.Levels variable 𝕒 𝕒₀ 𝕒₁ 𝕒₂ 𝕒₃ 𝕒₄ 𝕒₅ : Set aℓ 𝕓 𝕓₀ 𝕓₁ 𝕓₂ 𝕓₃ 𝕓₄ 𝕓₅ : Set bℓ 𝕔 𝕔₀ 𝕔₁ 𝕔₂ 𝕔₃ 𝕔₄ 𝕔₅ : Set cℓ 𝕕 𝕕₀ 𝕕₁ 𝕕₂ 𝕕₃ 𝕕₄ 𝕕₅ : Set dℓ 𝕖 𝕖₀ 𝕖₁ 𝕖₂ 𝕖₃ 𝕖₄ 𝕖₅ : Set eℓ 𝕗 𝕗₀ 𝕗₁ 𝕗₂ 𝕗₃ 𝕗₄ 𝕗₅ : Set fℓ 𝕘 𝕘₀ 𝕘₁ 𝕘₂ 𝕘₃ 𝕘₄ 𝕘₅ : Set gℓ 𝕙 𝕙₀ 𝕙₁ 𝕙₂ 𝕙₃ 𝕙₄ 𝕙₅ : Set hℓ
------------------------------------------------------------------------ -- Some Vec-related properties ------------------------------------------------------------------------ module Data.Vec.Properties where open import Algebra open import Data.Vec open import Data.Nat import Data.Nat.Properties as Nat open import Data.Fin using (Fin; zero; suc) open import Data.Function open import Relation.Binary module UsingVectorEquality (S : Setoid) where private module SS = Setoid S open SS using () renaming (carrier to A) import Data.Vec.Equality as VecEq open VecEq.Equality S replicate-lemma : ∀ {m} n x (xs : Vec A m) → replicate {n = n} x ++ (x ∷ xs) ≈ replicate {n = 1 + n} x ++ xs replicate-lemma zero x xs = refl (x ∷ xs) replicate-lemma (suc n) x xs = SS.refl ∷-cong replicate-lemma n x xs xs++[]=xs : ∀ {n} (xs : Vec A n) → xs ++ [] ≈ xs xs++[]=xs [] = []-cong xs++[]=xs (x ∷ xs) = SS.refl ∷-cong xs++[]=xs xs map-++-commute : ∀ {B m n} (f : B → A) (xs : Vec B m) {ys : Vec B n} → map f (xs ++ ys) ≈ map f xs ++ map f ys map-++-commute f [] = refl _ map-++-commute f (x ∷ xs) = SS.refl ∷-cong map-++-commute f xs open import Relation.Binary.PropositionalEquality as PropEq open import Relation.Binary.HeterogeneousEquality as HetEq -- lookup is natural. lookup-natural : ∀ {A B n} (f : A → B) (i : Fin n) → lookup i ∘ map f ≗ f ∘ lookup i lookup-natural f zero (x ∷ xs) = refl lookup-natural f (suc i) (x ∷ xs) = lookup-natural f i xs -- map is a congruence. map-cong : ∀ {A B n} {f g : A → B} → f ≗ g → _≗_ {Vec A n} (map f) (map g) map-cong f≗g [] = refl map-cong f≗g (x ∷ xs) = PropEq.cong₂ _∷_ (f≗g x) (map-cong f≗g xs) -- map is functorial. map-id : ∀ {A n} → _≗_ {Vec A n} (map id) id map-id [] = refl map-id (x ∷ xs) = PropEq.cong (_∷_ x) (map-id xs) map-∘ : ∀ {A B C n} (f : B → C) (g : A → B) → _≗_ {Vec A n} (map (f ∘ g)) (map f ∘ map g) map-∘ f g [] = refl map-∘ f g (x ∷ xs) = PropEq.cong (_∷_ (f (g x))) (map-∘ f g xs) -- sum commutes with _++_. sum-++-commute : ∀ {m n} (xs : Vec ℕ m) {ys : Vec ℕ n} → sum (xs ++ ys) ≡ sum xs + sum ys sum-++-commute [] = refl sum-++-commute (x ∷ xs) {ys} = begin x + sum (xs ++ ys) ≡⟨ PropEq.cong (λ p → x + p) (sum-++-commute xs) ⟩ x + (sum xs + sum ys) ≡⟨ PropEq.sym (+-assoc x (sum xs) (sum ys)) ⟩ sum (x ∷ xs) + sum ys ∎ where open ≡-Reasoning open CommutativeSemiring Nat.commutativeSemiring hiding (_+_; sym) -- foldr is a congruence. foldr-cong : ∀ {A} {B₁} {f₁ : ∀ {n} → A → B₁ n → B₁ (suc n)} {e₁} {B₂} {f₂ : ∀ {n} → A → B₂ n → B₂ (suc n)} {e₂} → (∀ {n x} {y₁ : B₁ n} {y₂ : B₂ n} → y₁ ≅ y₂ → f₁ x y₁ ≅ f₂ x y₂) → e₁ ≅ e₂ → ∀ {n} (xs : Vec A n) → foldr B₁ f₁ e₁ xs ≅ foldr B₂ f₂ e₂ xs foldr-cong _ e₁=e₂ [] = e₁=e₂ foldr-cong {B₁ = B₁} f₁=f₂ e₁=e₂ (x ∷ xs) = f₁=f₂ (foldr-cong {B₁ = B₁} f₁=f₂ e₁=e₂ xs) -- foldl is a congruence. foldl-cong : ∀ {A} {B₁} {f₁ : ∀ {n} → B₁ n → A → B₁ (suc n)} {e₁} {B₂} {f₂ : ∀ {n} → B₂ n → A → B₂ (suc n)} {e₂} → (∀ {n x} {y₁ : B₁ n} {y₂ : B₂ n} → y₁ ≅ y₂ → f₁ y₁ x ≅ f₂ y₂ x) → e₁ ≅ e₂ → ∀ {n} (xs : Vec A n) → foldl B₁ f₁ e₁ xs ≅ foldl B₂ f₂ e₂ xs foldl-cong _ e₁=e₂ [] = e₁=e₂ foldl-cong {B₁ = B₁} f₁=f₂ e₁=e₂ (x ∷ xs) = foldl-cong {B₁ = B₁ ∘₀ suc} f₁=f₂ (f₁=f₂ e₁=e₂) xs -- The (uniqueness part of the) universality property for foldr. foldr-universal : ∀ {A} (B : ℕ → Set) (f : ∀ {n} → A → B n → B (suc n)) {e} (h : ∀ {n} → Vec A n → B n) → h [] ≡ e → (∀ {n} x → h ∘ _∷_ x ≗ f {n} x ∘ h) → ∀ {n} → h ≗ foldr B {n} f e foldr-universal B f h base step [] = base foldr-universal B f {e} h base step (x ∷ xs) = begin h (x ∷ xs) ≡⟨ step x xs ⟩ f x (h xs) ≡⟨ PropEq.cong (f x) (foldr-universal B f h base step xs) ⟩ f x (foldr B f e xs) ∎ where open ≡-Reasoning -- A fusion law for foldr. foldr-fusion : ∀ {A} {B : ℕ → Set} {f : ∀ {n} → A → B n → B (suc n)} e {C : ℕ → Set} {g : ∀ {n} → A → C n → C (suc n)} (h : ∀ {n} → B n → C n) → (∀ {n} x → h ∘ f {n} x ≗ g x ∘ h) → ∀ {n} → h ∘ foldr B {n} f e ≗ foldr C g (h e) foldr-fusion {B = B} {f} e {C} h fuse = foldr-universal C _ _ refl (λ x xs → fuse x (foldr B f e xs)) -- The identity function is a fold. idIsFold : ∀ {A n} → id ≗ foldr (Vec A) {n} _∷_ [] idIsFold = foldr-universal _ _ id refl (λ _ _ → refl)
module Issue1691 where open import Common.Equality open import Issue1691.Nat -- works if we inline this module ∸1≗pred : ∀ n → n ∸ suc 0 ≡ pred n -- works if zero is replaced by 0 ∸1≗pred zero = refl ∸1≗pred (suc _) = refl
-- Agda supports full unicode everywhere. An Agda file should be written using -- the UTF-8 encoding. module Introduction.Unicode where module ユーニコード where data _∧_ (P Q : Prop) : Prop where ∧-intro : P -> Q -> P ∧ Q ∧-elim₁ : {P Q : Prop} -> P ∧ Q -> P ∧-elim₁ (∧-intro p _) = p ∧-elim₂ : {P Q : Prop} -> P ∧ Q -> Q ∧-elim₂ (∧-intro _ q) = q data _∨_ (P Q : Prop) : Prop where ∨-intro₁ : P -> P ∨ Q ∨-intro₂ : Q -> P ∨ Q ∨-elim : {P Q R : Prop} -> (P -> R) -> (Q -> R) -> P ∨ Q -> R ∨-elim f g (∨-intro₁ p) = f p ∨-elim f g (∨-intro₂ q) = g q data ⊥ : Prop where data ⊤ : Prop where ⊤-intro : ⊤ data ¬_ (P : Prop) : Prop where ¬-intro : (P -> ⊥) -> ¬ P data ∏ {A : Set}(P : A -> Prop) : Prop where ∏-intro : ((x : A) -> P x) -> ∏ P
------------------------------------------------------------------------ -- Abstract grammars ------------------------------------------------------------------------ {-# OPTIONS --without-K --safe #-} module Grammar.Abstract where open import Data.Bool open import Data.Char open import Data.Empty open import Data.List open import Data.Product open import Data.Sum open import Function open import Relation.Binary.PropositionalEquality using (_≡_) ------------------------------------------------------------------------ -- Grammars -- I use an abstract and general definition of grammars: a grammar is -- a predicate that relates strings with parse results. Grammar : Set → Set₁ Grammar A = A → List Char → Set ------------------------------------------------------------------------ -- Grammar combinators -- A grammar for no strings. fail : ∀ {A} → Grammar A fail = λ _ _ → ⊥ -- Symmetric choice. infixl 10 _∣_ _∣_ : ∀ {A} → Grammar A → Grammar A → Grammar A g₁ ∣ g₂ = λ x s → g₁ x s ⊎ g₂ x s -- Grammars for the empty string. return : ∀ {A} → A → Grammar A return x = λ y s → y ≡ x × s ≡ [] -- Map. infixl 20 _<$>_ _<$_ _<$>_ : ∀ {A B} → (A → B) → Grammar A → Grammar B f <$> g = λ x s → ∃ λ y → g y s × x ≡ f y _<$_ : ∀ {A B} → A → Grammar B → Grammar A x <$ g = const x <$> g -- A sequencing combinator for partially applied grammars. seq : (List Char → Set) → (List Char → Set) → (List Char → Set) seq p₁ p₂ = λ s → ∃₂ λ s₁ s₂ → p₁ s₁ × p₂ s₂ × s ≡ s₁ ++ s₂ -- Monadic bind. infixl 15 _>>=_ _>>_ _>>=_ : ∀ {A B} → Grammar A → (A → Grammar B) → Grammar B g₁ >>= g₂ = λ y s → ∃ λ x → seq (g₁ x) (g₂ x y) s _>>_ : ∀ {A B} → Grammar A → Grammar B → Grammar B g₁ >> g₂ = g₁ >>= λ _ → g₂ -- "Applicative" sequencing. infixl 20 _⊛_ _<⊛_ _⊛>_ _⊛_ : ∀ {A B} → Grammar (A → B) → Grammar A → Grammar B g₁ ⊛ g₂ = g₁ >>= λ f → f <$> g₂ _<⊛_ : ∀ {A B} → Grammar A → Grammar B → Grammar A g₁ <⊛ g₂ = λ x s → ∃ λ y → seq (g₁ x) (g₂ y) s _⊛>_ : ∀ {A B} → Grammar A → Grammar B → Grammar B _⊛>_ = _>>_ -- Kleene star. infix 30 _⋆ _+ _⋆ : ∀ {A} → Grammar A → Grammar (List A) (g ⋆) [] s = s ≡ [] (g ⋆) (x ∷ xs) s = seq (g x) ((g ⋆) xs) s _+ : ∀ {A} → Grammar A → Grammar (List A) (g +) [] s = ⊥ (g +) (x ∷ xs) s = (g ⋆) (x ∷ xs) s -- Elements separated by something. infixl 18 _sep-by_ _sep-by_ : ∀ {A B} → Grammar A → Grammar B → Grammar (List A) g sep-by sep = _∷_ <$> g ⊛ (sep >> g) ⋆ -- A grammar for an arbitrary token. token : Grammar Char token = λ c s → s ≡ [ c ] -- A grammar for a given token. tok : Char → Grammar Char tok c = λ c′ s → c′ ≡ c × token c′ s -- A grammar for tokens satisfying a given predicate. sat : (p : Char → Bool) → Grammar (∃ λ t → T (p t)) sat _ = λ { (c , _) s → token c s } -- A grammar for whitespace. whitespace : Grammar Char whitespace = tok ' ' ∣ tok '\n' -- A grammar for a given string. string : List Char → Grammar (List Char) string s = λ s′ s″ → s′ ≡ s × s″ ≡ s -- A grammar for the given string, possibly followed by some -- whitespace. symbol : List Char → Grammar (List Char) symbol s = string s <⊛ whitespace ⋆
{-# OPTIONS --cubical --no-import-sorts #-} module Number.Instances.QuoQ where open import Agda.Primitive renaming (_⊔_ to ℓ-max; lsuc to ℓ-suc; lzero to ℓ-zero) open import Cubical.Foundations.Everything renaming (_⁻¹ to _⁻¹ᵖ; assoc to ∙-assoc) open import Cubical.Foundations.Logic renaming (inl to inlᵖ; inr to inrᵖ) open import Cubical.Relation.Nullary.Base renaming (¬_ to ¬ᵗ_) open import Cubical.Relation.Binary.Base open import Cubical.Data.Sum.Base renaming (_⊎_ to infixr 4 _⊎_) open import Cubical.Data.Sigma.Base renaming (_×_ to infixr 4 _×_) open import Cubical.Data.Sigma open import Cubical.Data.Bool as Bool using (Bool; not; true; false) open import Cubical.Data.Empty renaming (elim to ⊥-elim; ⊥ to ⊥⊥) -- `⊥` and `elim` open import Cubical.Foundations.Logic renaming (¬_ to ¬ᵖ_; inl to inlᵖ; inr to inrᵖ) open import Function.Base using (it; _∋_; _$_) open import Cubical.Foundations.Isomorphism open import Cubical.HITs.PropositionalTruncation --.Properties open import Utils using (!_; !!_) open import MoreLogic.Reasoning open import MoreLogic.Definitions open import MoreLogic.Properties open import MorePropAlgebra.Definitions hiding (_≤''_) open import MorePropAlgebra.Structures open import MorePropAlgebra.Bundles open import MorePropAlgebra.Consequences open import Number.Structures2 open import Number.Bundles2 open import Cubical.Data.Nat using (suc; zero; ℕ) renaming ( _*_ to _*ⁿ_ ; +-comm to +ⁿ-comm ; +-assoc to +ⁿ-assoc ; *-comm to *ⁿ-comm ; *-suc to *ⁿ-suc ; *-assoc to *ⁿ-assoc ; +-suc to +ⁿ-suc ; *-distribˡ to *ⁿ-distribˡ ; *-distribʳ to *ⁿ-distribʳ ; *-identityʳ to *ⁿ-identityʳ ; snotz to snotzⁿ ; injSuc to injSucⁿ ) open import Cubical.Data.NatPlusOne using (HasFromNat; 1+_; ℕ₊₁; ℕ₊₁→ℕ) open import Cubical.HITs.Ints.QuoInt using (HasFromNat; signed) renaming ( abs to absᶻ ; pos to pos ; neg to neg ) open import Number.Instances.QuoInt using (ℤbundle) renaming ( _<ᶠ_ to _<ᶻ_ ; ·-reflects-<ᶠ to ·ᶻ-reflects-<ᶻ ) open import Cubical.Data.Nat.Order using () renaming ( ¬-<-zero to ¬-<ⁿ-zero ) module Definitions where open import Cubical.HITs.Ints.QuoInt hiding (_+_; -_; +-assoc; +-comm) open LinearlyOrderedCommRing ℤbundle -- open IsLinearlyOrderedCommRing is-LinearlyOrderedCommRing open import Cubical.HITs.Rationals.QuoQ using ( ℚ ; onCommonDenom ; onCommonDenomSym ; eq/ ; _//_ ; _∼_ ) renaming ( [_] to [_]ᶠ ; ℕ₊₁→ℤ to [1+_ⁿ]ᶻ ) abstract 0<1 : [ 0 < 1 ] 0<1 = 0 , refl -- TODO: import these properties from somewhere else +-reflects-< : ∀ x y z → [ (x + z < y + z) ⇒ ( x < y ) ] +-preserves-< : ∀ x y z → [ ( x < y ) ⇒ (x + z < y + z) ] +-creates-< : ∀ x y z → [ ( x < y ) ⇔ (x + z < y + z) ] +-preserves-< a b x = snd ( ( a < b ) ⇒ᵖ⟨ transport (λ i → [ sym (fst (+-identity a)) i < sym (fst (+-identity b)) i ]) ⟩ ( a + 0f < b + 0f ) ⇒ᵖ⟨ transport (λ i → [ a + sym (+-rinv x) i < b + sym (+-rinv x) i ]) ⟩ ( a + (x - x) < b + (x - x)) ⇒ᵖ⟨ transport (λ i → [ +-assoc a x (- x) i < +-assoc b x (- x) i ]) ⟩ ((a + x) - x < (b + x) - x ) ⇒ᵖ⟨ +-<-ext (a + x) (- x) (b + x) (- x) ⟩ ((a + x < b + x) ⊔ (- x < - x)) ⇒ᵖ⟨ (λ q → case q as (a + x < b + x) ⊔ (- x < - x) ⇒ a + x < b + x of λ { (inl a+x<b+x) → a+x<b+x -- somehow ⊥-elim needs a hint in the next line ; (inr -x<-x ) → ⊥-elim {A = λ _ → [ a + x < b + x ]} (<-irrefl (- x) -x<-x) }) ⟩ a + x < b + x ◼ᵖ) +-reflects-< x y z = snd ( x + z < y + z ⇒ᵖ⟨ +-preserves-< (x + z) (y + z) (- z) ⟩ (x + z) - z < (y + z) - z ⇒ᵖ⟨ transport (λ i → [ +-assoc x z (- z) (~ i) < +-assoc y z (- z) (~ i) ]) ⟩ x + (z - z) < y + (z - z) ⇒ᵖ⟨ transport (λ i → [ x + +-rinv z i < y + +-rinv z i ]) ⟩ x + 0f < y + 0f ⇒ᵖ⟨ transport (λ i → [ fst (+-identity x) i < fst (+-identity y) i ]) ⟩ x < y ◼ᵖ) +-creates-< x y z .fst = +-preserves-< x y z +-creates-< x y z .snd = +-reflects-< x y z suc-creates-< : ∀ x y → [ (x < y) ⇔ (sucℤ x < sucℤ y) ] suc-creates-< x y .fst p = substₚ (λ p → sucℤ x < p) (∣ +-comm y (pos 1) ∣) $ substₚ (λ p → p < y + pos 1) (∣ +-comm x (pos 1) ∣) (+-preserves-< x y (pos 1) p) suc-creates-< x y .snd p = +-reflects-< x y (pos 1) $ substₚ (λ p → p < y + pos 1) (∣ +-comm (pos 1) x ∣) $ substₚ (λ p → sucℤ x < p) (∣ +-comm (pos 1) y ∣) p +-creates-≤ : ∀ a b x → [ (a ≤ b) ⇔ ((a + x) ≤ (b + x)) ] +-creates-≤ a b x = {! !} ·-creates-< : ∀ a b x → [ 0 < x ] → [ (a < b) ⇔ ((a * x) < (b * x)) ] ·-creates-< a b x p .fst q = ·-preserves-< a b x p q ·-creates-< a b x p .snd q = ·ᶻ-reflects-<ᶻ a b x p q ·-creates-≤ : ∀ a b x → [ 0f ≤ x ] → [ (a ≤ b) ⇔ ((a · x) ≤ (b · x)) ] ·-creates-≤ a b x 0≤x .fst p = {! !} ·-creates-≤ a b x 0≤x .snd p = {! !} ·-creates-≤-≡ : ∀ a b x → [ 0f ≤ x ] → (a ≤ b) ≡ ((a · x) ≤ (b · x)) ·-creates-≤-≡ a b x 0≤x = uncurry ⇔toPath $ ·-creates-≤ a b x 0≤x ℤlattice : Lattice {ℓ-zero} {ℓ-zero} ℤlattice = record { LinearlyOrderedCommRing ℤbundle renaming (≤-Lattice to is-Lattice) } open import MorePropAlgebra.Properties.Lattice ℤlattice open OnSet is-set hiding (+-min-distribʳ; ·-min-distribʳ; +-max-distribʳ; ·-max-distribʳ) abstract ≤-dicho : ∀ x y → [ (x ≤ y) ⊔ (y ≤ x) ] ≤-dicho x y with <-tricho x y ... | inl (inl x<y) = inlᵖ $ <-asym x y x<y ... | inl (inr y<x) = inrᵖ $ <-asym y x y<x ... | inr x≡y = inlᵖ $ subst (λ p → [ ¬(p <ᶻ x) ]) x≡y (<-irrefl x) ≤-min-+ : ∀ a b c w → [ w ≤ (a + c) ] → [ w ≤ (b + c) ] → [ w ≤ (min a b + c) ] ≤-max-+ : ∀ a b c w → [ (a + c) ≤ w ] → [ (b + c) ≤ w ] → [ (max a b + c) ≤ w ] ≤-min-· : ∀ a b c w → [ w ≤ (a · c) ] → [ w ≤ (b · c) ] → [ w ≤ (min a b · c) ] ≤-max-· : ∀ a b c w → [ (a · c) ≤ w ] → [ (b · c) ≤ w ] → [ (max a b · c) ≤ w ] ≤-min-+ = OnType.≤-dicho⇒+.≤-min-+ _+_ ≤-dicho ≤-max-+ = OnType.≤-dicho⇒+.≤-max-+ _+_ ≤-dicho ≤-min-· = OnType.≤-dicho⇒·.≤-min-· _·_ ≤-dicho ≤-max-· = OnType.≤-dicho⇒·.≤-max-· _·_ ≤-dicho +-min-distribʳ : ∀ x y z → (min x y + z) ≡ min (x + z) (y + z) ·-min-distribʳ : ∀ x y z → [ 0f ≤ z ] → (min x y · z) ≡ min (x · z) (y · z) +-max-distribʳ : ∀ x y z → (max x y + z) ≡ max (x + z) (y + z) ·-max-distribʳ : ∀ x y z → [ 0f ≤ z ] → (max x y · z) ≡ max (x · z) (y · z) +-min-distribˡ : ∀ x y z → (z + min x y) ≡ min (z + x) (z + y) ·-min-distribˡ : ∀ x y z → [ 0f ≤ z ] → (z · min x y) ≡ min (z · x) (z · y) +-max-distribˡ : ∀ x y z → (z + max x y) ≡ max (z + x) (z + y) ·-max-distribˡ : ∀ x y z → [ 0f ≤ z ] → (z · max x y) ≡ max (z · x) (z · y) +-min-distribʳ = OnSet.+-min-distribʳ is-set _+_ +-creates-≤ ≤-min-+ ·-min-distribʳ = OnSet.·-min-distribʳ is-set 0f _·_ ·-creates-≤ ≤-min-· +-max-distribʳ = OnSet.+-max-distribʳ is-set _+_ +-creates-≤ ≤-max-+ ·-max-distribʳ = OnSet.·-max-distribʳ is-set 0f _·_ ·-creates-≤ ≤-max-· +-min-distribˡ x y z = +-comm z (min x y) ∙ +-min-distribʳ x y z ∙ (λ i → min (+-comm x z i) (+-comm y z i)) ·-min-distribˡ x y z p = ·-comm z (min x y) ∙ ·-min-distribʳ x y z p ∙ (λ i → min (·-comm x z i) (·-comm y z i)) +-max-distribˡ x y z = +-comm z (max x y) ∙ +-max-distribʳ x y z ∙ (λ i → max (+-comm x z i) (+-comm y z i)) ·-max-distribˡ x y z p = ·-comm z (max x y) ∙ ·-max-distribʳ x y z p ∙ (λ i → max (·-comm x z i) (·-comm y z i)) pos<pos[suc] : ∀ x → [ pos x < pos (suc x) ] pos<pos[suc] 0 = 0<1 pos<pos[suc] (suc x) = suc-creates-< (pos x) (pos (suc x)) .fst (pos<pos[suc] x) 0<ᶻpos[suc] : ∀ x → [ 0 < pos (suc x) ] 0<ᶻpos[suc] 0 = 0<1 0<ᶻpos[suc] (suc x) = <-trans 0 (pos (suc x)) (pos (suc (suc x))) (0<ᶻpos[suc] x) (suc-creates-< (pos x) (pos (suc x)) .fst (pos<pos[suc] x)) -- *-nullifiesʳ : ∀(x : ℤ) → x * 0 ≡ 0 -- *-nullifiesʳ (pos zero) = refl -- *-nullifiesʳ (pos (suc n)) = {! *-nullifiesʳ (pos n) !} -- *-nullifiesʳ (neg zero) = refl -- *-nullifiesʳ (neg (suc n)) = {! *-nullifiesʳ (neg n) !} -- *-nullifiesʳ (posneg i) = refl *-nullifiesˡ : ∀(x : ℤ) → 0 * x ≡ 0 *-nullifiesˡ x = {! !} *-preserves-<0 : ∀ a b → [ 0 < a ] → [ 0 < b ] → [ 0 < a * b ] *-preserves-<0 a b p q = subst (λ p → [ p < a * b ]) (*-nullifiesˡ b) (·-preserves-< 0 a b q p) -- data Trichotomy ·-creates-<-≡ : ∀ a b x → [ 0 < x ] → (a < b) ≡ ((a * x) < (b * x)) ·-creates-<-≡ a b x p = ⇔toPath (·-creates-< a b x p .fst) (·-creates-< a b x p .snd) open import Cubical.HITs.SetQuotients as SetQuotient using () renaming (_/_ to _//_) abstract lemma1 : ∀ a b₁ b₂ c → (a * b₁) * (b₂ * c) ≡ (a * c) * (b₂ * b₁) lemma1 a b₁ b₂ c = (a * b₁) * (b₂ * c) ≡⟨ sym $ ·-assoc a b₁ (b₂ * c) ⟩ a * (b₁ * (b₂ * c)) ≡⟨ (λ i → a * ·-assoc b₁ b₂ c i) ⟩ a * ((b₁ * b₂) * c) ≡⟨ (λ i → a * ·-comm (b₁ * b₂) c i) ⟩ a * (c * (b₁ * b₂)) ≡⟨ ·-assoc a c (b₁ * b₂) ⟩ (a * c) * (b₁ * b₂) ≡⟨ (λ i → (a * c) * ·-comm b₁ b₂ i) ⟩ (a * c) * (b₂ * b₁) ∎ -- TODO: we might extract definition and properties in the where clauses upfront infixl 4 _<ᶠ_ _<ᶠ_ : hPropRel ℚ ℚ ℓ-zero a <ᶠ b = SetQuotient.rec2 {R = _∼_} {B = hProp ℓ-zero} isSetHProp _<'_ <'-respects-∼ˡ <'-respects-∼ʳ a b where _<'_ : ℤ × ℕ₊₁ → ℤ × ℕ₊₁ → hProp ℓ-zero (aᶻ , aⁿ) <' (bᶻ , bⁿ) = let aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ in (aᶻ * bⁿᶻ) < (bᶻ * aⁿᶻ) abstract <'-respects-∼ˡ : ∀ a b x → a ∼ b → a <' x ≡ b <' x <'-respects-∼ˡ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) x@(xᶻ , xⁿ) a~b = γ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ xⁿᶻ = [1+ xⁿ ⁿ]ᶻ 0<aⁿᶻ : [ 0 < aⁿᶻ ] 0<aⁿᶻ = 0<ᶻpos[suc] _ 0<bⁿᶻ : [ 0 < bⁿᶻ ] 0<bⁿᶻ = 0<ᶻpos[suc] _ p : aᶻ * bⁿᶻ ≡ bᶻ * aⁿᶻ p = a~b γ : ((aᶻ * xⁿᶻ) < (xᶻ * aⁿᶻ)) ≡ ((bᶻ * xⁿᶻ) < (xᶻ * bⁿᶻ)) γ with <-tricho 0 aᶻ ... | inl (inl 0<aᶻ) = (aᶻ * xⁿᶻ) < (xᶻ * aⁿᶻ) ≡⟨ ·-creates-<-≡ (aᶻ * xⁿᶻ) (xᶻ * aⁿᶻ) (aᶻ * bⁿᶻ) (*-preserves-<0 aᶻ bⁿᶻ 0<aᶻ 0<bⁿᶻ) ⟩ (aᶻ * xⁿᶻ) * (aᶻ * bⁿᶻ) < (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ) ≡⟨ (λ i → (aᶻ * xⁿᶻ) * p i < (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ)) ⟩ (aᶻ * xⁿᶻ) * (bᶻ * aⁿᶻ) < (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ) ≡⟨ (λ i → ·-comm (aᶻ * xⁿᶻ) (bᶻ * aⁿᶻ) i < (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ)) ⟩ (bᶻ * aⁿᶻ) * (aᶻ * xⁿᶻ) < (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ) ≡⟨ (λ i → lemma1 bᶻ aⁿᶻ aᶻ xⁿᶻ i < lemma1 xᶻ aⁿᶻ aᶻ bⁿᶻ i) ⟩ (bᶻ * xⁿᶻ) * (aᶻ * aⁿᶻ) < (xᶻ * bⁿᶻ) * (aᶻ * aⁿᶻ) ≡⟨ sym $ ·-creates-<-≡ (bᶻ * xⁿᶻ) (xᶻ * bⁿᶻ) (aᶻ * aⁿᶻ) (*-preserves-<0 aᶻ aⁿᶻ 0<aᶻ 0<aⁿᶻ) ⟩ (bᶻ * xⁿᶻ) < (xᶻ * bⁿᶻ) ∎ ... | inl (inr aᶻ<0) = {! !} ... | inr 0≡aᶻ = (aᶻ * xⁿᶻ) < (xᶻ * aⁿᶻ) ≡⟨ {! !} ⟩ ( 0 * xⁿᶻ) < (xᶻ * aⁿᶻ) ≡⟨ {! !} ⟩ 0 < (xᶻ * aⁿᶻ) ≡⟨ {! κ !} ⟩ 0 < (xᶻ * bⁿᶻ) ≡⟨ {! !} ⟩ ( 0 * xⁿᶻ) < (xᶻ * bⁿᶻ) ≡⟨ {! !} ⟩ (bᶻ * xⁿᶻ) < (xᶻ * bⁿᶻ) ∎ where bᶻ≡0 : bᶻ ≡ 0 bᶻ≡0 = {! !} κ : ∀ x y z → [ 0 < y ] → [ 0 < z ] → (0 < (x * y)) ≡ (0 < (x * z)) κ x y z p q = 0 < (x * y) ≡⟨ {! !} ⟩ (0 * y) < (x * y) ≡⟨ {! !} ⟩ 0 < x ≡⟨ {! !} ⟩ (0 * z) < (x * z) ≡⟨ {! !} ⟩ 0 < (x * z) ∎ -- in (aᶻ * xⁿᶻ) < (xᶻ * aⁿᶻ) ≡⟨ {! !} ⟩ -- (aᶻ * xⁿᶻ) / (aᶻ * bⁿᶻ) < (xᶻ * aⁿᶻ) / (bᶻ * aⁿᶻ) ≡⟨ {! !} ⟩ -- xⁿᶻ / bⁿᶻ < xᶻ / bᶻ ≡⟨ {! !} ⟩ -- (bᶻ * xⁿᶻ) < (xᶻ * bⁿᶻ) ∎ -- aᶻ > 0: abstract <'-respects-∼ʳ : ∀ x a b → a ∼ b → x <' a ≡ x <' b <'-respects-∼ʳ x@(xᶻ , xⁿ) a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) a~b = let aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ xⁿᶻ = [1+ xⁿ ⁿ]ᶻ p : aᶻ * bⁿᶻ ≡ bᶻ * aⁿᶻ p = a~b in (xᶻ * aⁿᶻ) < (aᶻ * xⁿᶻ) ≡⟨ {! !} ⟩ (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ) < (aᶻ * xⁿᶻ) * (aᶻ * bⁿᶻ) ≡⟨ {! !} ⟩ (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ) < (aᶻ * xⁿᶻ) * (bᶻ * aⁿᶻ) ≡⟨ {! !} ⟩ (xᶻ * aⁿᶻ) * (aᶻ * bⁿᶻ) < (bᶻ * aⁿᶻ) * (aᶻ * xⁿᶻ) ≡⟨ {! !} ⟩ (xᶻ * bⁿᶻ) * (aᶻ * aⁿᶻ) < (bᶻ * xⁿᶻ) * (aᶻ * aⁿᶻ) ≡⟨ {! !} ⟩ (xᶻ * bⁿᶻ) < (bᶻ * xⁿᶻ) ∎ _≤ᶠ_ : hPropRel ℚ ℚ ℓ-zero x ≤ᶠ y = ¬ᵖ (y <ᶠ x) minᶠ : ℚ → ℚ → ℚ minᶠ a b = onCommonDenomSym min' min'-sym min'-respects-∼ a b where min' : ℤ × ℕ₊₁ → ℤ × ℕ₊₁ → ℤ min' (aᶻ , aⁿ) (bᶻ , bⁿ) = let aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ in min (aᶻ * bⁿᶻ) (bᶻ * aⁿᶻ) abstract min'-sym : ∀ x y → min' x y ≡ min' y x min'-sym (aᶻ , aⁿ) (bᶻ , bⁿ) = min-comm (aᶻ * [1+ bⁿ ⁿ]ᶻ) (bᶻ * [1+ aⁿ ⁿ]ᶻ) min'-respects-∼ : (a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) x@(xᶻ , xⁿ) : ℤ × ℕ₊₁) → a ∼ b → [1+ bⁿ ⁿ]ᶻ * min' a x ≡ [1+ aⁿ ⁿ]ᶻ * min' b x min'-respects-∼ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) x@(xᶻ , xⁿ) a~b = bⁿᶻ * min (aᶻ * xⁿᶻ) (xᶻ * aⁿᶻ) ≡⟨ ·-min-distribˡ (aᶻ * xⁿᶻ) (xᶻ * aⁿᶻ) bⁿᶻ 0≤bⁿᶻ ⟩ min (bⁿᶻ * (aᶻ * xⁿᶻ)) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → min (·-assoc bⁿᶻ aᶻ xⁿᶻ i) (bⁿᶻ * (xᶻ * aⁿᶻ))) ⟩ min ((bⁿᶻ * aᶻ) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → min ((·-comm bⁿᶻ aᶻ i) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ))) ⟩ min ((aᶻ * bⁿᶻ) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → min (p i * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ))) ⟩ min ((bᶻ * aⁿᶻ) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → min (·-comm bᶻ aⁿᶻ i * xⁿᶻ) (·-assoc bⁿᶻ xᶻ aⁿᶻ i)) ⟩ min ((aⁿᶻ * bᶻ) * xⁿᶻ) ((bⁿᶻ * xᶻ) * aⁿᶻ) ≡⟨ (λ i → min (·-assoc aⁿᶻ bᶻ xⁿᶻ (~ i)) (·-comm (bⁿᶻ * xᶻ) aⁿᶻ i)) ⟩ min (aⁿᶻ * (bᶻ * xⁿᶻ)) (aⁿᶻ * (bⁿᶻ * xᶻ)) ≡⟨ sym $ ·-min-distribˡ (bᶻ * xⁿᶻ) (bⁿᶻ * xᶻ) aⁿᶻ 0≤aⁿᶻ ⟩ aⁿᶻ * min (bᶻ * xⁿᶻ) (bⁿᶻ * xᶻ) ≡⟨ (λ i → aⁿᶻ * min (bᶻ * xⁿᶻ) (·-comm bⁿᶻ xᶻ i)) ⟩ aⁿᶻ * min (bᶻ * xⁿᶻ) (xᶻ * bⁿᶻ) ∎ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ xⁿᶻ = [1+ xⁿ ⁿ]ᶻ p : aᶻ * bⁿᶻ ≡ bᶻ * aⁿᶻ p = a~b 0≤aⁿᶻ : [ 0 ≤ aⁿᶻ ] 0≤aⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p 0≤bⁿᶻ : [ 0 ≤ bⁿᶻ ] 0≤bⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p -- same proof as for min maxᶠ : ℚ → ℚ → ℚ maxᶠ a b = onCommonDenomSym max' max'-sym max'-respects-∼ a b where max' : ℤ × ℕ₊₁ → ℤ × ℕ₊₁ → ℤ max' (aᶻ , aⁿ) (bᶻ , bⁿ) = let aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ in max (aᶻ * bⁿᶻ) (bᶻ * aⁿᶻ) abstract max'-sym : ∀ x y → max' x y ≡ max' y x max'-sym (aᶻ , aⁿ) (bᶻ , bⁿ) = max-comm (aᶻ * [1+ bⁿ ⁿ]ᶻ) (bᶻ * [1+ aⁿ ⁿ]ᶻ) max'-respects-∼ : (a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) x@(xᶻ , xⁿ) : ℤ × ℕ₊₁) → a ∼ b → [1+ bⁿ ⁿ]ᶻ * max' a x ≡ [1+ aⁿ ⁿ]ᶻ * max' b x max'-respects-∼ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) x@(xᶻ , xⁿ) a~b = bⁿᶻ * max (aᶻ * xⁿᶻ) (xᶻ * aⁿᶻ) ≡⟨ ·-max-distribˡ (aᶻ * xⁿᶻ) (xᶻ * aⁿᶻ) bⁿᶻ 0≤bⁿᶻ ⟩ max (bⁿᶻ * (aᶻ * xⁿᶻ)) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → max (·-assoc bⁿᶻ aᶻ xⁿᶻ i) (bⁿᶻ * (xᶻ * aⁿᶻ))) ⟩ max ((bⁿᶻ * aᶻ) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → max ((·-comm bⁿᶻ aᶻ i) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ))) ⟩ max ((aᶻ * bⁿᶻ) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → max (p i * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ))) ⟩ max ((bᶻ * aⁿᶻ) * xⁿᶻ) (bⁿᶻ * (xᶻ * aⁿᶻ)) ≡⟨ (λ i → max (·-comm bᶻ aⁿᶻ i * xⁿᶻ) (·-assoc bⁿᶻ xᶻ aⁿᶻ i)) ⟩ max ((aⁿᶻ * bᶻ) * xⁿᶻ) ((bⁿᶻ * xᶻ) * aⁿᶻ) ≡⟨ (λ i → max (·-assoc aⁿᶻ bᶻ xⁿᶻ (~ i)) (·-comm (bⁿᶻ * xᶻ) aⁿᶻ i)) ⟩ max (aⁿᶻ * (bᶻ * xⁿᶻ)) (aⁿᶻ * (bⁿᶻ * xᶻ)) ≡⟨ sym $ ·-max-distribˡ (bᶻ * xⁿᶻ) (bⁿᶻ * xᶻ) aⁿᶻ 0≤aⁿᶻ ⟩ aⁿᶻ * max (bᶻ * xⁿᶻ) (bⁿᶻ * xᶻ) ≡⟨ (λ i → aⁿᶻ * max (bᶻ * xⁿᶻ) (·-comm bⁿᶻ xᶻ i)) ⟩ aⁿᶻ * max (bᶻ * xⁿᶻ) (xᶻ * bⁿᶻ) ∎ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ xⁿᶻ = [1+ xⁿ ⁿ]ᶻ p : aᶻ * bⁿᶻ ≡ bᶻ * aⁿᶻ p = a~b 0≤aⁿᶻ : [ 0 ≤ aⁿᶻ ] 0≤aⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p 0≤bⁿᶻ : [ 0 ≤ bⁿᶻ ] 0≤bⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p -- maxᶠ : ℚ → ℚ → ℚ -- maxᶠ a b = onCommonDenom f g h a b where -- f : ℤ × ℕ₊₁ → ℤ × ℕ₊₁ → ℤ -- f (aᶻ , aⁿ) (bᶻ , bⁿ) = max (aᶻ * [1+ bⁿ ⁿ]ᶻ) (bᶻ * [1+ aⁿ ⁿ]ᶻ) -- g : (a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) : ℤ × ℕ₊₁) -- → aᶻ * [1+ bⁿ ⁿ]ᶻ ≡ bᶻ * [1+ aⁿ ⁿ]ᶻ -- → [1+ bⁿ ⁿ]ᶻ * f a c ≡ [1+ aⁿ ⁿ]ᶻ * f b c -- g a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) aᶻ*bⁿ'≡bᶻ*aⁿ' = let -- aⁿ' = [1+ aⁿ ⁿ]ᶻ -- bⁿ' = [1+ bⁿ ⁿ]ᶻ -- cⁿ' = [1+ cⁿ ⁿ]ᶻ -- 0<aⁿ' : [ 0 < aⁿ' ] -- 0<aⁿ' = 0<ᶻpos[suc] _ -- 0<bⁿ' : [ 0 < bⁿ' ] -- 0<bⁿ' = 0<ᶻpos[suc] _ -- 0<cⁿ' : [ 0 < cⁿ' ] -- 0<cⁿ' = 0<ᶻpos[suc] _ -- γ : bⁿ' * max (aᶻ * cⁿ') (cᶻ * aⁿ') -- ≡ aⁿ' * max (bᶻ * cⁿ') (cᶻ * bⁿ') -- γ = {! !} -- in γ -- h : (a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) : ℤ × ℕ₊₁) -- → bᶻ * [1+ cⁿ ⁿ]ᶻ ≡ cᶻ * [1+ bⁿ ⁿ]ᶻ -- → f a b * [1+ cⁿ ⁿ]ᶻ ≡ f a c * [1+ bⁿ ⁿ]ᶻ -- h a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) bᶻ*cⁿ≡cᶻ*bⁿ = {! !} open Definitions public renaming ( _<ᶠ_ to _<_ ; _≤ᶠ_ to _≤_ ; minᶠ to min ; maxᶠ to max ) open LinearlyOrderedCommRing ℤbundle using () renaming ( min to minᶻ ; max to maxᶻ -- ; _<_ to _<ᶻ_ ; _≤_ to _≤ᶻ_ ; <-irrefl to <ᶻ-irrefl ; <-trans to <ᶻ-trans ; is-min to is-minᶻ ; is-max to is-maxᶻ ; ·-assoc to ·ᶻ-assoc ; ·-comm to ·ᶻ-comm ; <-tricho to <ᶻ-tricho ; -_ to -ᶻ_ ) open import Cubical.HITs.Rationals.QuoQ renaming ( [_] to [_]ᶠ ; eq/ to eq/ᶠ ; ℕ₊₁→ℤ to [1+_ⁿ]ᶻ ) open import Cubical.HITs.SetQuotients as SetQuotient using () renaming (_/_ to _//_) open import Cubical.HITs.Ints.QuoInt using (ℤ) renaming (_*_ to _*ᶻ_; sign to signᶻ) open import Cubical.HITs.PropositionalTruncation renaming (elim to ∣∣-elim) abstract <-irrefl : ∀ a → [ ¬(a < a) ] <-irrefl = SetQuotient.elimProp {R = _∼_} (λ a → isProp[] (¬(a < a))) γ where γ : (a : ℤ × ℕ₊₁) → [ ¬([ a ]ᶠ < [ a ]ᶠ) ] γ a@(aᶻ , aⁿ) = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ κ : [ ¬((aᶻ *ᶻ aⁿᶻ) <ᶻ (aᶻ *ᶻ aⁿᶻ)) ] κ = <ᶻ-irrefl (aᶻ *ᶻ aⁿᶻ) <-trans : (a b c : ℚ) → [ a < b ] → [ b < c ] → [ a < c ] <-trans = SetQuotient.elimProp3 {R = _∼_} (λ a b c → isProp[] ((a < b) ⇒ (b < c) ⇒ (a < c))) γ where γ : (a b c : ℤ × ℕ₊₁) → [ [ a ]ᶠ < [ b ]ᶠ ] → [ [ b ]ᶠ < [ c ]ᶠ ] → [ [ a ]ᶠ < [ c ]ᶠ ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ cⁿᶻ = [1+ cⁿ ⁿ]ᶻ κ : [ (aᶻ *ᶻ bⁿᶻ) <ᶻ (bᶻ *ᶻ aⁿᶻ) ] → [ (bᶻ *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ bⁿᶻ) ] → [ (aᶻ *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ aⁿᶻ) ] -- strategy: multiply with xⁿᶻ and then use <ᶻ-trans κ = {!!} <-asym : ∀ a b → [ a < b ] → [ ¬(b < a) ] <-asym a b a<b b<a = <-irrefl a (<-trans a b a a<b b<a) <-irrefl'' : ∀ a b → [ a < b ] ⊎ [ b < a ] → [ ¬(a ≡ₚ b) ] <-irrefl'' a b (inl a<b) a≡b = <-irrefl b (substₚ (λ p → p < b) a≡b a<b) <-irrefl'' a b (inr b<a) a≡b = <-irrefl b (substₚ (λ p → b < p) a≡b b<a) <-tricho : (a b : ℚ) → ([ a < b ] ⊎ [ b < a ]) ⊎ [ a ≡ₚ b ] -- TODO: insert trichotomy definition here <-tricho = SetQuotient.elimProp2 {R = _∼_} (λ a b → isProp[] ([ <-irrefl'' a b ] ([ <-asym a b ] (a < b) ⊎ᵖ (b < a)) ⊎ᵖ (a ≡ₚ b))) γ where γ : (a b : ℤ × ℕ₊₁) → ([ [ a ]ᶠ < [ b ]ᶠ ] ⊎ [ [ b ]ᶠ < [ a ]ᶠ ]) ⊎ [ [ a ]ᶠ ≡ₚ [ b ]ᶠ ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ κ : ([ (aᶻ *ᶻ bⁿᶻ) <ᶻ (bᶻ *ᶻ aⁿᶻ) ] ⊎ [ (bᶻ *ᶻ aⁿᶻ) <ᶻ (aᶻ *ᶻ bⁿᶻ) ]) ⊎ [ [ aᶻ , aⁿ ]ᶠ ≡ₚ [ bᶻ , bⁿ ]ᶠ ] κ with <ᶻ-tricho (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) ... | inl p = inl p ... | inr p = inr ∣ eq/ᶠ {R = _∼_} a b p ∣ <ᶻ-asym : ∀ a b → [ a <ᶻ b ] → [ ¬(b <ᶻ a) ] <ᶻ-asym a b a<b b<a = <ᶻ-irrefl a (<ᶻ-trans a b a a<b b<a) _#ᶻ_ : hPropRel ℤ ℤ ℓ-zero x #ᶻ y = [ <ᶻ-asym x y ] (x <ᶻ y) ⊎ᵖ (y <ᶻ x) _#_ : hPropRel ℚ ℚ ℓ-zero x # y = [ <-asym x y ] (x < y) ⊎ᵖ (y < x) injᶻⁿ⁺¹ : ∀ x → [ 0 <ᶻ x ] → Σ[ y ∈ ℕ₊₁ ] x ≡ [1+ y ⁿ]ᶻ injᶻⁿ⁺¹ (signed false zero) p = ⊥-elim {A = λ _ → Σ[ y ∈ ℕ₊₁ ] ℤ.posneg i0 ≡ [1+ y ⁿ]ᶻ} (¬-<ⁿ-zero p) injᶻⁿ⁺¹ (signed true zero) p = ⊥-elim {A = λ _ → Σ[ y ∈ ℕ₊₁ ] ℤ.posneg i1 ≡ [1+ y ⁿ]ᶻ} (¬-<ⁿ-zero p) injᶻⁿ⁺¹ (ℤ.posneg i) p = ⊥-elim {A = λ _ → Σ[ y ∈ ℕ₊₁ ] ℤ.posneg i ≡ [1+ y ⁿ]ᶻ} (¬-<ⁿ-zero p) injᶻⁿ⁺¹ (signed false (suc n)) p = 1+ n , refl abstract -flips-<ᶻ0 : ∀ x → [ (x <ᶻ 0) ⇔ (0 <ᶻ (-ᶻ x)) ] -flips-<ᶻ0 (signed false zero) = (λ x → x) , (λ x → x) -flips-<ᶻ0 (signed true zero) = (λ x → x) , (λ x → x) -flips-<ᶻ0 (ℤ.posneg i) = (λ x → x) , (λ x → x) -flips-<ᶻ0 (signed false (suc n)) .fst p = ¬-<ⁿ-zero p -flips-<ᶻ0 (signed true (suc n)) .fst tt = n , +ⁿ-comm n 1 -flips-<ᶻ0 (signed true (suc n)) .snd p = tt -- -flips-< : ∀ x y → [ x <ᶻ y ⇔ - y <ᶻ - x ] -- #-Dichotomyˢ : {A : Type ℓ} (is-set : isSet A) (_#_ : hPropRel A A ℓ) )(#-tight : ∀ x y → [ x # y ⇔ ¬([ is-set ] x ≡ˢ y) ] ) (x : A) → hProp ℓ -- #-Dichotomyˢ x = [ #-tight x 0 .fst ] x # 0 ⊎ᵖ (x ≡ 0) #ᶻ-dicho : ∀ x → [ x #ᶻ 0 ] ⊎ (x ≡ 0) #ᶻ-dicho x = <ᶻ-tricho x 0 ⊕-identityʳ : ∀ s → (s Bool.⊕ false) ≡ s ⊕-identityʳ false = refl ⊕-identityʳ true = refl abstract *ᶻ-preserves-signˡ : ∀ x y → [ 0 <ᶻ y ] → signᶻ (x *ᶻ y) ≡ signᶻ x *ᶻ-preserves-signˡ x (signed false zero) p = ⊥-elim {A = λ _ → signᶻ (x *ᶻ ℤ.posneg i0) ≡ signᶻ x} (¬-<ⁿ-zero p) *ᶻ-preserves-signˡ x (signed true zero) p = ⊥-elim {A = λ _ → signᶻ (x *ᶻ ℤ.posneg i1) ≡ signᶻ x} (¬-<ⁿ-zero p) *ᶻ-preserves-signˡ x (ℤ.posneg i) p = ⊥-elim {A = λ _ → signᶻ (x *ᶻ ℤ.posneg i ) ≡ signᶻ x} (¬-<ⁿ-zero p) *ᶻ-preserves-signˡ (signed false zero) (signed false (suc n)) p = refl *ᶻ-preserves-signˡ (signed true zero) (signed false (suc n)) p = refl *ᶻ-preserves-signˡ (ℤ.posneg i) (signed false (suc n)) p = refl *ᶻ-preserves-signˡ (signed s (suc n₁)) (signed false (suc n)) p = ⊕-identityʳ s absⁿ⁺¹' : ℤ → ℕ₊₁ absⁿ⁺¹' xᶻ with <ᶻ-tricho 0 xᶻ ... | inl (inl 0<xᶻ) = injᶻⁿ⁺¹ xᶻ 0<xᶻ .fst ... | inl (inr xᶻ<0) = injᶻⁿ⁺¹ (-ᶻ xᶻ) ( -flips-<ᶻ0 xᶻ .fst xᶻ<0) .fst ... | inr 0≡xᶻ = 1+ 0 -- this case will be excluded lateron, but we keep it here to omit the definition of a nonzero ℚ -- absⁿ⁺¹'-identity⁺ : ∀ x → signed false (ℕ₊₁→ℕ x) ≡ [1+ absⁿ⁺¹' x ⁿ]ᶻ -- absⁿ⁺¹'-identity⁺ x = ? absⁿ⁺¹'-identity⁺ : ∀ x → [1+ absⁿ⁺¹' (pos x) ⁿ]ᶻ ≡ pos x absⁿ⁺¹'-identity⁺ x with <ᶻ-tricho 0 (pos x) ... | inl (inl 0<xᶻ) = {!!} ... | inl (inr xᶻ<0) = {!!} ... | inr 0≡xᶻ = {!!} _⁻¹''' : (x : ℚ) → [ x # 0 ] → ℚ _⁻¹''' = {! SetQuotient.elim {R = _∼_} {B = λ x → [ x # 0 ] → ℚ} φ _⁻¹'' ⁻¹''-respects-∼ !} where φ : ∀ x → isSet ([ x # 0 ] → ℚ) φ x = isSetΠ (λ _ → isSetℚ) _⁻¹'' : (a : ℤ × ℕ₊₁) → [ [ a ]ᶠ # 0 ] → ℚ x ⁻¹'' = {!!} ⁻¹''-respects-∼ : (a b : ℤ × ℕ₊₁) (r : a ∼ b) → PathP (λ i → [ eq/ᶠ a b r i # 0 ] → ℚ) (a ⁻¹'') (b ⁻¹'') ⁻¹''-respects-∼ a b r = {!!} isSet→ : ∀{ℓ ℓ'} {A : Type ℓ} {B : Type ℓ'} → (isSet B) → isSet (A → B) isSet→ {B = B} isset = isSetΠ λ _ → isset _⁻¹' : ℚ → ℚ _⁻¹' = SetQuotient.rec {R = _∼_} isSetℚ _⁻¹'' ⁻¹''-respects-∼ where _⁻¹'' : ℤ × ℕ₊₁ → ℚ (xᶻ , xⁿ) ⁻¹'' = [ signed (signᶻ xᶻ) (ℕ₊₁→ℕ xⁿ) , absⁿ⁺¹' xᶻ ]ᶠ ⁻¹''-respects-∼ : (a b : ℤ × ℕ₊₁) → a ∼ b → (a ⁻¹'') ≡ (b ⁻¹'') ⁻¹''-respects-∼ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) p = κ where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ 0<aⁿᶻ : [ 0 <ᶻ aⁿᶻ ] 0<aⁿᶻ = ℕ₊₁.n aⁿ , +ⁿ-comm (ℕ₊₁.n aⁿ) 1 0<bⁿᶻ : [ 0 <ᶻ bⁿᶻ ] 0<bⁿᶻ = ℕ₊₁.n bⁿ , +ⁿ-comm (ℕ₊₁.n bⁿ) 1 q : aᶻ *ᶻ bⁿᶻ ≡ bᶻ *ᶻ aⁿᶻ q = p sign≡ = signᶻ aᶻ ≡⟨ sym $ *ᶻ-preserves-signˡ aᶻ bⁿᶻ 0<bⁿᶻ ⟩ signᶻ (aᶻ *ᶻ bⁿᶻ) ≡⟨ (λ i → signᶻ (q i)) ⟩ signᶻ (bᶻ *ᶻ aⁿᶻ) ≡⟨ *ᶻ-preserves-signˡ bᶻ aⁿᶻ 0<aⁿᶻ ⟩ signᶻ bᶻ ∎ cᶻ dᶻ : ℤ cᶻ = signed (signᶻ aᶻ) (ℕ₊₁→ℕ aⁿ) dᶻ = signed (signᶻ bᶻ) (ℕ₊₁→ℕ bⁿ) cⁿ dⁿ : ℕ₊₁ cⁿ = absⁿ⁺¹' aᶻ dⁿ = absⁿ⁺¹' bᶻ t : ∀ s → (signed s (ℕ₊₁→ℕ aⁿ) *ᶻ [1+ absⁿ⁺¹' bᶻ ⁿ]ᶻ) ≡ (signed s (ℕ₊₁→ℕ bⁿ) *ᶻ [1+ absⁿ⁺¹' aᶻ ⁿ]ᶻ) t false = {!!} t true = {!!} r : (signed (signᶻ aᶻ) (ℕ₊₁→ℕ aⁿ) *ᶻ [1+ absⁿ⁺¹' bᶻ ⁿ]ᶻ) ≡ (signed (signᶻ bᶻ) (ℕ₊₁→ℕ bⁿ) *ᶻ [1+ absⁿ⁺¹' aᶻ ⁿ]ᶻ) r = {!!} κ : ([ cᶻ , cⁿ ]ᶠ) ≡ ([ dᶻ , dⁿ ]ᶠ) κ = eq/ᶠ {R = _∼_} (cᶻ , cⁿ) (dᶻ , dⁿ) r ·-inv'' : ∀ x → [ (∃[ y ] ([ isSetℚ ] (x * y) ≡ˢ 1)) ⇔ (x # 0) ] -- ·-inv'' x .fst p = {! !} -- ·-inv'' x .snd p = {! !} ·-inv'' = SetQuotient.elimProp {R = _∼_} φ {! !} where φ : (x : ℚ) → _ φ x = isProp[] ((∃[ y ] ([ isSetℚ ] (x * y) ≡ˢ 1)) ⇔ (x # 0)) κ₁ : ∀ x → [ ∃[ y ] ([ isSetℚ ] (x * y) ≡ˢ 1) ] → [ x # 0 ] κ₁ x p = ∣∣-elim (λ _ → φ') (λ{ (y , q) → γ y q } ) p where φ' = isProp[] (x # 0) γ : ∀ y → [ [ isSetℚ ] (x * y) ≡ˢ 1 ] → [ x # 0 ] γ y q = {!!} κ₂ : ∀ x → [ x # 0 ] → [ ∃[ y ] ([ isSetℚ ] (x * y) ≡ˢ 1) ] κ₂ x (inl x<0) = {! !} -- well.. we would need ℚ⁺ here κ₂ x (inr 0<x) = ∣ SetQuotient.rec {R = _∼_} isSetℚ {!!} {!!} x , {!!} ∣ where _⁻¹⁺ : ℤ × ℕ₊₁ → ℚ _⁻¹⁺ (aᶻ , aⁿ) = {!!} +-Semigroup : [ isSemigroup _+_ ] +-Semigroup .IsSemigroup.is-set = isSetℚ +-Semigroup .IsSemigroup.is-assoc = +-assoc ·-Semigroup : [ isSemigroup _*_ ] ·-Semigroup .IsSemigroup.is-set = isSetℚ ·-Semigroup .IsSemigroup.is-assoc = *-assoc +-Monoid : [ isMonoid 0 _+_ ] +-Monoid .IsMonoid.is-Semigroup = +-Semigroup +-Monoid .IsMonoid.is-identity x = +-identityʳ x , +-identityˡ x ·-Monoid : [ isMonoid 1 _*_ ] ·-Monoid .IsMonoid.is-Semigroup = ·-Semigroup ·-Monoid .IsMonoid.is-identity x = *-identityʳ x , *-identityˡ x is-Semiring : [ isSemiring 0 1 _+_ _*_ ] is-Semiring .IsSemiring.+-Monoid = +-Monoid is-Semiring .IsSemiring.·-Monoid = ·-Monoid is-Semiring .IsSemiring.+-comm = +-comm is-Semiring .IsSemiring.is-dist x y z = sym (*-distribˡ x y z) , sym (*-distribʳ x y z) is-CommSemiring : [ isCommSemiring 0 1 _+_ _*_ ] is-CommSemiring .IsCommSemiring.is-Semiring = is-Semiring is-CommSemiring .IsCommSemiring.·-comm = *-comm <-StrictLinearOrder : [ isStrictLinearOrder _<_ ] <-StrictLinearOrder .IsStrictLinearOrder.is-irrefl = <-irrefl <-StrictLinearOrder .IsStrictLinearOrder.is-trans = <-trans <-StrictLinearOrder .IsStrictLinearOrder.is-tricho = <-tricho open import Cubical.Data.NatPlusOne using (_*₊₁_) ⇔toPath' : ∀{ℓ} {P Q : hProp ℓ} → [ P ⇔ Q ] → P ≡ Q ⇔toPath' = uncurry ⇔toPath pathTo⇔ : ∀{ℓ} {P Q : hProp ℓ} → P ≡ Q → [ P ⇔ Q ] pathTo⇔ p≡q = (pathTo⇒ p≡q , pathTo⇐ p≡q) ⊓⇔⊓ : ∀{ℓ ℓ' ℓ'' ℓ'''} {P : hProp ℓ} {Q : hProp ℓ'} {R : hProp ℓ''} {S : hProp ℓ'''} → [ (P ⇔ R) ⊓ (Q ⇔ S) ] → [ (P ⊓ Q) ⇔ (R ⊓ S) ] ⊓⇔⊓ (p⇔r , q⇔s) .fst (p , q) = p⇔r .fst p , q⇔s .fst q ⊓⇔⊓ (p⇔r , q⇔s) .snd (r , s) = p⇔r .snd r , q⇔s .snd s ⊓≡⊓ : ∀{ℓ} {P Q R S : hProp ℓ} → P ≡ R → Q ≡ S → (P ⊓ Q) ≡ (R ⊓ S) ⊓≡⊓ p≡r q≡s i = p≡r i ⊓ q≡s i abstract is-min : (x y z : ℚ) → [ (z ≤ min x y) ⇔ (z ≤ x) ⊓ (z ≤ y) ] is-min = SetQuotient.elimProp3 {R = _∼_} (λ x y z → isProp[] ((z ≤ min x y) ⇔ (z ≤ x) ⊓ (z ≤ y))) γ where γ : (a b c : ℤ × ℕ₊₁) → [ ([ c ]ᶠ ≤ min [ a ]ᶠ [ b ]ᶠ) ⇔ ([ c ]ᶠ ≤ [ a ]ᶠ) ⊓ ([ c ]ᶠ ≤ [ b ]ᶠ) ] γ a@(aᶻ , aⁿ) b@(bᶻ , bⁿ) c@(cᶻ , cⁿ) = pathTo⇔ (sym κ) where aⁿᶻ = [1+ aⁿ ⁿ]ᶻ bⁿᶻ = [1+ bⁿ ⁿ]ᶻ cⁿᶻ = [1+ cⁿ ⁿ]ᶻ 0≤aⁿᶻ : [ 0 ≤ᶻ aⁿᶻ ] 0≤aⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p 0≤bⁿᶻ : [ 0 ≤ᶻ bⁿᶻ ] 0≤bⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p 0≤cⁿᶻ : [ 0 ≤ᶻ cⁿᶻ ] 0≤cⁿᶻ (k , p) = snotzⁿ $ sym (+ⁿ-suc k _) ∙ p -- -- note, that the following holds definitionally (TODO: put this at the definition of `min`) -- _ = min [ aᶻ , aⁿ ]ᶠ [ bᶻ , bⁿ ]ᶠ ≡⟨ refl ⟩ -- [ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) , aⁿ *₊₁ bⁿ) ]ᶠ ∎ -- -- and we also have definitionally -- _ : [1+ aⁿ *₊₁ bⁿ ⁿ]ᶻ ≡ aⁿᶻ *ᶻ bⁿᶻ -- _ = refl -- -- therefore, we have for the LHS: -- _ = ([ cᶻ , cⁿ ]ᶠ ≤ min [ aᶻ , aⁿ ]ᶠ [ bᶻ , bⁿ ]ᶠ) ≡⟨ refl ⟩ -- ([ cᶻ , cⁿ ]ᶠ ≤ [ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) , aⁿ *₊₁ bⁿ) ]ᶠ) ≡⟨ refl ⟩ -- (¬([ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) , aⁿ *₊₁ bⁿ) ]ᶠ < [ cᶻ , cⁿ ]ᶠ)) ≡⟨ refl ⟩ -- ¬((minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ))) ≡⟨ refl ⟩ -- ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ)) ∎ -- -- similar for the RHS: -- _ = ([ cᶻ , cⁿ ]ᶠ ≤ [ aᶻ , aⁿ ]ᶠ) ⊓ ([ cᶻ , cⁿ ]ᶠ ≤ [ bᶻ , bⁿ ]ᶠ) ≡⟨ refl ⟩ -- ¬([ aᶻ , aⁿ ]ᶠ < [ cᶻ , cⁿ ]ᶠ) ⊓ ¬([ bᶻ , bⁿ ]ᶠ < [ cᶻ , cⁿ ]ᶠ) ≡⟨ refl ⟩ -- ¬((aᶻ *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ aⁿᶻ)) ⊓ ¬((bᶻ *ᶻ cⁿᶻ) <ᶻ (cᶻ *ᶻ bⁿᶻ)) ≡⟨ refl ⟩ -- ((cᶻ *ᶻ aⁿᶻ) ≤ᶻ (aᶻ *ᶻ cⁿᶻ)) ⊓ ((cᶻ *ᶻ bⁿᶻ) ≤ᶻ (bᶻ *ᶻ cⁿᶻ)) ∎ -- -- therfore, only properties on ℤ remain -- RHS = [ ((cᶻ *ᶻ aⁿᶻ) ≤ᶻ (aᶻ *ᶻ cⁿᶻ)) ⊓ ((cᶻ *ᶻ bⁿᶻ) ≤ᶻ (bᶻ *ᶻ cⁿᶻ)) ] -- LHS = [ ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ)) ] -- strategy: multiply everything with aⁿᶻ, bⁿᶻ, cⁿᶻ κ = ( ((cᶻ *ᶻ aⁿᶻ) ≤ᶻ (aᶻ *ᶻ cⁿᶻ) ) ⊓ ((cᶻ *ᶻ bⁿᶻ) ≤ᶻ (bᶻ *ᶻ cⁿᶻ) ) ≡⟨ ⊓≡⊓ (Definitions.·-creates-≤-≡ (cᶻ *ᶻ aⁿᶻ) (aᶻ *ᶻ cⁿᶻ) bⁿᶻ 0≤bⁿᶻ) (Definitions.·-creates-≤-≡ (cᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ cⁿᶻ) aⁿᶻ 0≤aⁿᶻ) ⟩ ((cᶻ *ᶻ aⁿᶻ) *ᶻ bⁿᶻ ≤ᶻ (aᶻ *ᶻ cⁿᶻ) *ᶻ bⁿᶻ ) ⊓ ((cᶻ *ᶻ bⁿᶻ) *ᶻ aⁿᶻ ≤ᶻ (bᶻ *ᶻ cⁿᶻ) *ᶻ aⁿᶻ ) ≡⟨ ⊓≡⊓ (λ i → ·ᶻ-assoc cᶻ aⁿᶻ bⁿᶻ (~ i) ≤ᶻ ·ᶻ-assoc aᶻ cⁿᶻ bⁿᶻ (~ i)) (λ i → ·ᶻ-assoc cᶻ bⁿᶻ aⁿᶻ (~ i) ≤ᶻ ·ᶻ-assoc bᶻ cⁿᶻ aⁿᶻ (~ i)) ⟩ ( cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ) ≤ᶻ aᶻ *ᶻ (cⁿᶻ *ᶻ bⁿᶻ)) ⊓ ( cᶻ *ᶻ (bⁿᶻ *ᶻ aⁿᶻ) ≤ᶻ bᶻ *ᶻ (cⁿᶻ *ᶻ aⁿᶻ)) ≡⟨ ⊓≡⊓ (λ i → cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ) ≤ᶻ aᶻ *ᶻ (·ᶻ-comm cⁿᶻ bⁿᶻ i)) (λ i → cᶻ *ᶻ (·ᶻ-comm bⁿᶻ aⁿᶻ i) ≤ᶻ bᶻ *ᶻ (·ᶻ-comm cⁿᶻ aⁿᶻ i)) ⟩ ( cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ) ≤ᶻ aᶻ *ᶻ (bⁿᶻ *ᶻ cⁿᶻ)) ⊓ ( cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ) ≤ᶻ bᶻ *ᶻ (aⁿᶻ *ᶻ cⁿᶻ)) ≡⟨ sym $ ⇔toPath' $ is-minᶻ (aᶻ *ᶻ (bⁿᶻ *ᶻ cⁿᶻ)) (bᶻ *ᶻ (aⁿᶻ *ᶻ cⁿᶻ)) (cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ⟩ ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ minᶻ (aᶻ *ᶻ (bⁿᶻ *ᶻ cⁿᶻ)) (bᶻ *ᶻ (aⁿᶻ *ᶻ cⁿᶻ))) ≡⟨ (λ i → ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ minᶻ (·ᶻ-assoc aᶻ bⁿᶻ cⁿᶻ i) (·ᶻ-assoc bᶻ aⁿᶻ cⁿᶻ i))) ⟩ ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ minᶻ ((aᶻ *ᶻ bⁿᶻ) *ᶻ cⁿᶻ) ((bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ)) ≡⟨ (λ i → (cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ Definitions.·-min-distribʳ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) cⁿᶻ 0≤cⁿᶻ (~ i)) ⟩ ((cᶻ *ᶻ (aⁿᶻ *ᶻ bⁿᶻ)) ≤ᶻ (minᶻ (aᶻ *ᶻ bⁿᶻ) (bᶻ *ᶻ aⁿᶻ) *ᶻ cⁿᶻ)) ∎) ≤-Lattice : [ isLattice _≤_ min max ] ≤-Lattice .IsLattice.≤-PartialOrder = linearorder⇒partialorder _ (≤'-isLinearOrder <-StrictLinearOrder) ≤-Lattice .IsLattice.is-min = is-min ≤-Lattice .IsLattice.is-max = {! !} is-LinearlyOrderedCommSemiring : [ isLinearlyOrderedCommSemiring 0 1 _+_ _*_ _<_ min max ] is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.is-CommSemiring = is-CommSemiring is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.<-StrictLinearOrder = <-StrictLinearOrder is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.≤-Lattice = ≤-Lattice is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.+-<-ext = {! !} is-LinearlyOrderedCommSemiring .IsLinearlyOrderedCommSemiring.·-preserves-< = {! !} +-inverse : (x : ℚ) → (x + (- x) ≡ 0) × ((- x) + x ≡ 0) +-inverse x .fst = +-inverseʳ x +-inverse x .snd = +-inverseˡ x is-LinearlyOrderedCommRing : [ isLinearlyOrderedCommRing 0 1 _+_ _*_ -_ _<_ min max ] is-LinearlyOrderedCommRing. IsLinearlyOrderedCommRing.is-LinearlyOrderedCommSemiring = is-LinearlyOrderedCommSemiring is-LinearlyOrderedCommRing. IsLinearlyOrderedCommRing.+-inverse = +-inverse is-LinearlyOrderedField : [ isLinearlyOrderedField 0 1 _+_ _*_ -_ _<_ min max ] is-LinearlyOrderedField .IsLinearlyOrderedField.is-LinearlyOrderedCommRing = is-LinearlyOrderedCommRing is-LinearlyOrderedField .IsLinearlyOrderedField.·-inv'' = ·-inv'' ℚbundle : LinearlyOrderedField {ℓ-zero} {ℓ-zero} ℚbundle .LinearlyOrderedField.Carrier = ℚ ℚbundle .LinearlyOrderedField.0f = 0 ℚbundle .LinearlyOrderedField.1f = 1 ℚbundle .LinearlyOrderedField._+_ = _+_ ℚbundle .LinearlyOrderedField.-_ = -_ ℚbundle .LinearlyOrderedField._·_ = _*_ ℚbundle .LinearlyOrderedField.min = min ℚbundle .LinearlyOrderedField.max = max ℚbundle .LinearlyOrderedField._<_ = _<_ ℚbundle .LinearlyOrderedField.is-LinearlyOrderedField = is-LinearlyOrderedField
postulate id : {t : Set} → t → t _≡_ : {t : Set} → t → t → Set ⊤ : Set record FunctorOp (f : Set → Set) : Set₁ where record FunctorLaws (f : Set → Set) {{op : FunctorOp f}} : Set₁ where -- demand functor laws to access <*>, but promise we won't use them in our definition record ApplyOp (A : Set → Set) {{_ : FunctorOp A}} .{{_ : FunctorLaws A}} : Set₁ where field _<*>_ : ∀ {t₁ t₂} → A (t₁ → t₂) → A t₁ → A t₂ open ApplyOp {{...}} record ApplyLaws₂ (A : Set → Set) {{_ : FunctorOp A}} .{{_ : FunctorLaws A}} {{i : ApplyOp A}} : Set₁ where -- but if we try to do anything in here... -- resolution fails, even though our instance `i` is already resolved and in scope! field blah : ∀ (f : A (⊤ → ⊤)) → (x : A ⊤) → (f <*> x) ≡ x
module SystemF.Substitutions.Types where open import Prelude open import SystemF.Syntax.Type open import Data.Fin.Substitution open import Data.Star hiding (map) open import Data.Vec module TypeSubst where module TypeApp {T} (l : Lift T Type) where open Lift l hiding (var) infixl 8 _/_ _/_ : ∀ {m n} → Type m → Sub T m n → Type n tc c / σ = tc c tvar x / σ = lift (lookup x σ) (a →' b) / σ = (a / σ) →' (b / σ) (a ⟶ b) / σ = (a / σ) ⟶ (b / σ) ∀' a / σ = ∀' (a / σ ↑) open Application (record { _/_ = _/_ }) using (_/✶_) →'-/✶-↑✶ : ∀ k {m n a b} (ρs : Subs T m n) → (a →' b) /✶ ρs ↑✶ k ≡ (a /✶ ρs ↑✶ k) →' (b /✶ ρs ↑✶ k) →'-/✶-↑✶ k ε = refl →'-/✶-↑✶ k (ρ ◅ ρs) = cong₂ _/_ (→'-/✶-↑✶ k ρs) refl ⟶-/✶-↑✶ : ∀ k {m n a b} (ρs : Subs T m n) → (a ⟶ b) /✶ ρs ↑✶ k ≡ (a /✶ ρs ↑✶ k) ⟶ (b /✶ ρs ↑✶ k) ⟶-/✶-↑✶ k ε = refl ⟶-/✶-↑✶ k (ρ ◅ ρs) = cong₂ _/_ (⟶-/✶-↑✶ k ρs) refl ∀'-/✶-↑✶ : ∀ k {m n a} (ρs : Subs T m n) → (∀' a) /✶ ρs ↑✶ k ≡ ∀' (a /✶ ρs ↑✶ (1 + k)) ∀'-/✶-↑✶ k ε = refl ∀'-/✶-↑✶ k (ρ ◅ ρs) = cong₂ _/_ (∀'-/✶-↑✶ k ρs) refl tc-/✶-↑✶ : ∀ k {c m n} (ρs : Subs T m n) → (tc c) /✶ ρs ↑✶ k ≡ tc c tc-/✶-↑✶ k ε = refl tc-/✶-↑✶ k (r ◅ ρs) = cong₂ _/_ (tc-/✶-↑✶ k ρs) refl typeSubst : TermSubst Type typeSubst = record { var = tvar; app = TypeApp._/_ } open TermSubst typeSubst public hiding (var) infix 8 _[/_] -- Shorthand for single-variable type substitutions _[/_] : ∀ {n} → Type (suc n) → Type n → Type n a [/ b ] = a / sub b open TypeSubst public using () renaming (_/_ to _tp/tp_; _[/_] to _tp[/tp_]; weaken to tp-weaken)
open import SOAS.Common open import SOAS.Families.Core open import Categories.Object.Initial open import SOAS.Coalgebraic.Strength import SOAS.Metatheory.MetaAlgebra -- Renaming structure by initiality module SOAS.Metatheory.Renaming {T : Set} (⅀F : Functor 𝔽amiliesₛ 𝔽amiliesₛ) (⅀:Str : Strength ⅀F) (𝔛 : Familyₛ) (open SOAS.Metatheory.MetaAlgebra ⅀F 𝔛) (𝕋:Init : Initial 𝕄etaAlgebras) where open import SOAS.Context open import SOAS.Variable open import SOAS.Abstract.Hom import SOAS.Abstract.Coalgebra as →□ ; open →□.Sorted import SOAS.Abstract.Box as □ ; open □.Sorted open import SOAS.Coalgebraic.Map open import SOAS.Metatheory.Algebra {T} ⅀F open import SOAS.Metatheory.Semantics ⅀F ⅀:Str 𝔛 𝕋:Init open import SOAS.Metatheory.Traversal ⅀F ⅀:Str 𝔛 𝕋:Init open Strength ⅀:Str -- Renaming is a ℐ-parametrised traversal into 𝕋 module Renaming = □Traversal 𝕋ᵃ 𝕣𝕖𝕟 : 𝕋 ⇾̣ □ 𝕋 𝕣𝕖𝕟 = Renaming.𝕥𝕣𝕒𝕧 𝕨𝕜 : {α τ : T}{Γ : Ctx} → 𝕋 α Γ → 𝕋 α (τ ∙ Γ) 𝕨𝕜 t = 𝕣𝕖𝕟 t old -- Comultiplication law 𝕣𝕖𝕟-comp : MapEq₂ ℐᴮ ℐᴮ 𝕒𝕝𝕘 (λ t ρ ϱ → 𝕣𝕖𝕟 t (ϱ ∘ ρ)) (λ t ρ ϱ → 𝕣𝕖𝕟 (𝕣𝕖𝕟 t ρ) ϱ) 𝕣𝕖𝕟-comp = record { φ = 𝕧𝕒𝕣 ; ϕ = λ x ρ → 𝕧𝕒𝕣 (ρ x) ; χ = 𝕞𝕧𝕒𝕣 ; f⟨𝑣⟩ = 𝕥⟨𝕧⟩ ; f⟨𝑚⟩ = 𝕥⟨𝕞⟩ ; f⟨𝑎⟩ = λ{ {σ = ρ}{ϱ}{t} → begin 𝕣𝕖𝕟 (𝕒𝕝𝕘 t) (ϱ ∘ ρ) ≡⟨ 𝕥⟨𝕒⟩ ⟩ 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (⅀₁ 𝕣𝕖𝕟 t) (ϱ ∘ ρ)) ≡⟨ cong 𝕒𝕝𝕘 (str-dist 𝕋 (jᶜ ℐᴮ) (⅀₁ 𝕣𝕖𝕟 t) ρ ϱ) ⟩ 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (str ℐᴮ (□ 𝕋) (⅀₁ (precomp 𝕋 (j ℐ)) (⅀₁ 𝕣𝕖𝕟 t)) ρ) ϱ) ≡˘⟨ congr ⅀.homomorphism (λ - → 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (str ℐᴮ (□ 𝕋) - ρ) ϱ)) ⟩ 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (str ℐᴮ (□ 𝕋) (⅀₁ (λ{ t ρ ϱ → 𝕣𝕖𝕟 t (ϱ ∘ ρ)}) t) ρ) ϱ) ∎ } ; g⟨𝑣⟩ = trans (𝕥≈₁ 𝕥⟨𝕧⟩) 𝕥⟨𝕧⟩ ; g⟨𝑚⟩ = trans (𝕥≈₁ 𝕥⟨𝕞⟩) 𝕥⟨𝕞⟩ ; g⟨𝑎⟩ = λ{ {σ = ρ}{ϱ}{t} → begin 𝕣𝕖𝕟 (𝕣𝕖𝕟 (𝕒𝕝𝕘 t) ρ) ϱ ≡⟨ 𝕥≈₁ 𝕥⟨𝕒⟩ ⟩ 𝕣𝕖𝕟 (𝕒𝕝𝕘 (str ℐᴮ 𝕋 (⅀₁ 𝕣𝕖𝕟 t) ρ)) ϱ ≡⟨ 𝕥⟨𝕒⟩ ⟩ 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (⅀₁ 𝕣𝕖𝕟 (str ℐᴮ 𝕋 (⅀₁ 𝕣𝕖𝕟 t) ρ)) ϱ) ≡˘⟨ congr (str-nat₂ 𝕣𝕖𝕟 (⅀₁ 𝕣𝕖𝕟 t) ρ) (λ - → 𝕒𝕝𝕘 (str ℐᴮ 𝕋 - ϱ)) ⟩ 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (str ℐᴮ (□ 𝕋) (⅀.F₁ (λ { h′ ς → 𝕣𝕖𝕟 (h′ ς) }) (⅀₁ 𝕣𝕖𝕟 t)) ρ) ϱ) ≡˘⟨ congr ⅀.homomorphism (λ - → 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (str ℐᴮ (□ 𝕋) - ρ) ϱ)) ⟩ 𝕒𝕝𝕘 (str ℐᴮ 𝕋 (str ℐᴮ (□ 𝕋) (⅀₁ (λ{ t ρ ϱ → 𝕣𝕖𝕟 (𝕣𝕖𝕟 t ρ) ϱ}) t) ρ) ϱ) ∎ } } where open ≡-Reasoning open Renaming -- Pointed □-coalgebra structure for 𝕋 𝕋ᵇ : Coalg 𝕋 𝕋ᵇ = record { r = 𝕣𝕖𝕟 ; counit = □𝕥𝕣𝕒𝕧-id≈id ; comult = λ{ {t = t} → MapEq₂.≈ 𝕣𝕖𝕟-comp t } } 𝕋ᴮ : Coalgₚ 𝕋 𝕋ᴮ = record { ᵇ = 𝕋ᵇ ; η = 𝕧𝕒𝕣 ; r∘η = Renaming.𝕥⟨𝕧⟩ }
open import Common.IO open import Agda.Builtin.String open import Agda.Builtin.Unit open import Agda.Builtin.Nat open import Agda.Builtin.Sigma open import Agda.Builtin.Equality open import Agda.Builtin.Bool open import Agda.Builtin.Strict data ∋⋆ : Set where Z : ∋⋆ data ⊢⋆ : Set where size⋆ : Nat → ⊢⋆ ⋆Sub : Set ⋆Sub = ∋⋆ → ⊢⋆ data TermCon : ⊢⋆ → Set where integer : (s : Nat) → TermCon (size⋆ s) data ⊢_ : ⊢⋆ → Set where con : ∀ {s} → TermCon (size⋆ s) → ⊢ size⋆ s data Value : {A : ⊢⋆} → ⊢ A → Set where V-con : ∀ {n} → (cn : TermCon (size⋆ n)) → Value (con cn) notErased : Nat → Bool notErased n = primForce n λ _ → true BUILTIN : (σ : ⋆Sub) (vtel : Σ (⊢ σ Z) Value) → Bool BUILTIN σ vtel with σ Z BUILTIN σ (_ , V-con (integer s)) | _ = notErased s -- Either of these work: -- BUILTIN σ (_ , V-con (integer s)) | size⋆ s = notErased s -- BUILTIN σ (con (integer s) , V-con (integer s)) | _ = notErased s con2 : ⊢ size⋆ 8 con2 = con (integer 8) vcon2 : Value con2 vcon2 = V-con (integer 8) builtin2plus2 : Bool builtin2plus2 = BUILTIN (λ _ → size⋆ 8) (con2 , vcon2) help : Bool → String help true = "4" help false = "something went wrong" check : "4" ≡ help builtin2plus2 check = refl main : IO ⊤ main = putStrLn (help builtin2plus2)