commit
stringlengths
40
40
old_file
stringlengths
2
205
new_file
stringlengths
2
205
old_contents
stringlengths
0
32.9k
new_contents
stringlengths
1
38.9k
subject
stringlengths
3
9.4k
message
stringlengths
6
9.84k
lang
stringlengths
3
13
license
stringclasses
13 values
repos
stringlengths
6
115k
a737167afb8fb406b75d8e0d52fdfb3fa6c50035
Parametric/Change/Specification.agda
Parametric/Change/Specification.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change evaluation (Def 3.6 and Fig. 4h). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) {{validity-structure : Validity.Structure ⟦_⟧Base}} where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Theorem.CongApp open import Postulate.Extensionality module Structure where --------------- -- Interface -- --------------- -- We provide: Derivatives of terms. ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) -- And we provide correctness proofs about the derivatives. correctness : ∀ {τ Γ} (t : Term Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ ⟦ c ⟧Const ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record { apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ) ; correct = λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎ } where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f = ⟦ s ⟧ ρ g = ⟦ s ⟧ (after-env dρ) u = ⟦ t ⟧ ρ v = ⟦ t ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ Δu = ⟦ t ⟧Δ ρ dρ in begin f u ⊞₍ τ ₎ call-change {σ} {τ} Δf u Δu ≡⟨ sym (is-valid {σ} {τ} Δf u Δu) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change evaluation (Def 3.6 and Fig. 4h). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) {{validity-structure : Validity.Structure ⟦_⟧Base}} where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Theorem.CongApp open import Postulate.Extensionality module Structure where --------------- -- Interface -- --------------- -- We provide: Derivatives of terms. ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) -- And we provide correctness proofs about the derivatives. correctness : ∀ {τ Γ} (t : Term Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ ⟦ c ⟧Const ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record { apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ) ; correct = λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎ } where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f₁ = ⟦ s ⟧ ρ f₂ = ⟦ s ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ u₁ = ⟦ t ⟧ ρ u₂ = ⟦ t ⟧ (after-env dρ) Δu = ⟦ t ⟧Δ ρ dρ in begin f₁ u₁ ⊞₍ τ ₎ call-change {σ} {τ} Δf u₁ Δu ≡⟨ sym (is-valid {σ} {τ} Δf u₁ Δu) ⟩ (f₁ ⊞₍ σ ⇒ τ ₎ Δf) (u₁ ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ f₂ u₂ ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
Rename locals in proof for clarity
Rename locals in proof for clarity Was re-reading this proof for the thesis.
Agda
mit
inc-lc/ilc-agda
a06adfefda06e416bc4f111d7085cd3b5ad42568
Parametric/Change/Evaluation.agda
Parametric/Change/Evaluation.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Type as ChangeType import Parametric.Change.Term as ChangeTerm import Parametric.Change.Value as ChangeValue module Parametric.Change.Evaluation {Base : Type.Structure} {Const : Term.Structure Base} (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (apply-base : ChangeTerm.ApplyStructure Const ΔBase) (diff-base : ChangeTerm.DiffStructure Const ΔBase) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open ChangeType.Structure Base ΔBase open ChangeTerm.Structure Const ΔBase diff-base apply-base open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open import Relation.Binary.PropositionalEquality open import Base.Denotation.Notation open import Postulate.Extensionality -- Relating `diff` with `diff-term`, `apply` with `apply-term` ApplyStructure : Set ApplyStructure = ∀ ι {Γ} → {t : Term Γ (base ι)} {Δt : Term Γ (ΔType (base ι))} {ρ : ⟦ Γ ⟧} → ⟦ t ⟧ ρ ⟦⊕₍ base ι ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ base ι ₎ Δt ⟧ ρ DiffStructure : Set DiffStructure = ∀ ι {Γ} → {s : Term Γ (base ι)} {t : Term Γ (base ι)} {ρ : ⟦ Γ ⟧} → ⟦ s ⟧ ρ ⟦⊝₍ base ι ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ base ι ₎ t ⟧ ρ module Structure (meaning-⊕-base : ApplyStructure) (meaning-⊝-base : DiffStructure) where -- unique names with unambiguous types -- to help type inference figure things out private module Disambiguation where infixr 9 _⋆_ _⋆_ : Type → Context → Context _⋆_ = _•_ meaning-⊕ : ∀ {τ Γ} {t : Term Γ τ} {Δt : Term Γ (ΔType τ)} {ρ : ⟦ Γ ⟧} → ⟦ t ⟧ ρ ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ τ ₎ Δt ⟧ ρ meaning-⊝ : ∀ {τ Γ} {s : Term Γ τ} {t : Term Γ τ} {ρ : ⟦ Γ ⟧} → ⟦ s ⟧ ρ ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ τ ₎ t ⟧ ρ meaning-⊕ {base ι} {Γ} {τ} {Δt} {ρ} = meaning-⊕-base ι {Γ} {τ} {Δt} {ρ} meaning-⊕ {σ ⇒ τ} {Γ} {t} {Δt} {ρ} = ext (λ v → let Γ′ = σ ⋆ (σ ⇒ τ) ⋆ ΔType (σ ⇒ τ) ⋆ Γ ρ′ : ⟦ Γ′ ⟧ ρ′ = v • (⟦ t ⟧ ρ) • (⟦ Δt ⟧ ρ) • ρ x : Term Γ′ σ x = var this f : Term Γ′ (σ ⇒ τ) f = var (that this) Δf : Term Γ′ (ΔType (σ ⇒ τ)) Δf = var (that (that this)) y = app f x Δy = app (app Δf x) (x ⊝ x) in begin ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (v ⟦⊝₍ σ ₎⟧ v) ≡⟨ cong (λ hole → ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v hole) (meaning-⊝ {s = x} {x} {ρ′}) ⟩ ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (⟦ x ⊝ x ⟧ ρ′) ≡⟨ meaning-⊕ {t = y} {Δt = Δy} {ρ′} ⟩ ⟦ y ⊕₍ τ ₎ Δy ⟧ ρ′ ∎) where open ≡-Reasoning open Disambiguation meaning-⊝ {base ι} {Γ} {s} {t} {ρ} = meaning-⊝-base ι {Γ} {s} {t} {ρ} meaning-⊝ {σ ⇒ τ} {Γ} {s} {t} {ρ} = ext (λ v → ext (λ Δv → let Γ′ = ΔType σ ⋆ σ ⋆ (σ ⇒ τ) ⋆ (σ ⇒ τ) ⋆ Γ ρ′ : ⟦ Γ′ ⟧Context ρ′ = Δv • v • ⟦ t ⟧Term ρ • ⟦ s ⟧Term ρ • ρ Δx : Term Γ′ (ΔType σ) Δx = var this x : Term Γ′ σ x = var (that this) f : Term Γ′ (σ ⇒ τ) f = var (that (that this)) g : Term Γ′ (σ ⇒ τ) g = var (that (that (that this))) y = app f x y′ = app g (x ⊕₍ σ ₎ Δx) in begin ⟦ s ⟧ ρ (v ⟦⊕₍ σ ₎⟧ Δv) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v ≡⟨ cong (λ hole → ⟦ s ⟧ ρ hole ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v) (meaning-⊕ {t = x} {Δt = Δx} {ρ′}) ⟩ ⟦ s ⟧ ρ (⟦ x ⊕₍ σ ₎ Δx ⟧ ρ′) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v ≡⟨ meaning-⊝ {s = y′} {y} {ρ′} ⟩ ⟦ y′ ⊝ y ⟧ ρ′ ∎)) where open ≡-Reasoning open Disambiguation
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Connecting Parametric.Change.Term and Parametric.Change.Value. ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Type as ChangeType import Parametric.Change.Term as ChangeTerm import Parametric.Change.Value as ChangeValue module Parametric.Change.Evaluation {Base : Type.Structure} {Const : Term.Structure Base} (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (apply-base : ChangeTerm.ApplyStructure Const ΔBase) (diff-base : ChangeTerm.DiffStructure Const ΔBase) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open ChangeType.Structure Base ΔBase open ChangeTerm.Structure Const ΔBase diff-base apply-base open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open import Relation.Binary.PropositionalEquality open import Base.Denotation.Notation open import Postulate.Extensionality -- Extension point 1: Relating ⊝ and its value on base types ApplyStructure : Set ApplyStructure = ∀ ι {Γ} → {t : Term Γ (base ι)} {Δt : Term Γ (ΔType (base ι))} {ρ : ⟦ Γ ⟧} → ⟦ t ⟧ ρ ⟦⊕₍ base ι ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ base ι ₎ Δt ⟧ ρ -- Extension point 2: Relating ⊕ and its value on base types DiffStructure : Set DiffStructure = ∀ ι {Γ} → {s : Term Γ (base ι)} {t : Term Γ (base ι)} {ρ : ⟦ Γ ⟧} → ⟦ s ⟧ ρ ⟦⊝₍ base ι ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ base ι ₎ t ⟧ ρ module Structure (meaning-⊕-base : ApplyStructure) (meaning-⊝-base : DiffStructure) where -- unique names with unambiguous types -- to help type inference figure things out private module Disambiguation where infixr 9 _⋆_ _⋆_ : Type → Context → Context _⋆_ = _•_ -- We provide: Relating ⊕ and ⊝ and their values on arbitrary types. meaning-⊕ : ∀ {τ Γ} {t : Term Γ τ} {Δt : Term Γ (ΔType τ)} {ρ : ⟦ Γ ⟧} → ⟦ t ⟧ ρ ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ τ ₎ Δt ⟧ ρ meaning-⊝ : ∀ {τ Γ} {s : Term Γ τ} {t : Term Γ τ} {ρ : ⟦ Γ ⟧} → ⟦ s ⟧ ρ ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ τ ₎ t ⟧ ρ meaning-⊕ {base ι} {Γ} {τ} {Δt} {ρ} = meaning-⊕-base ι {Γ} {τ} {Δt} {ρ} meaning-⊕ {σ ⇒ τ} {Γ} {t} {Δt} {ρ} = ext (λ v → let Γ′ = σ ⋆ (σ ⇒ τ) ⋆ ΔType (σ ⇒ τ) ⋆ Γ ρ′ : ⟦ Γ′ ⟧ ρ′ = v • (⟦ t ⟧ ρ) • (⟦ Δt ⟧ ρ) • ρ x : Term Γ′ σ x = var this f : Term Γ′ (σ ⇒ τ) f = var (that this) Δf : Term Γ′ (ΔType (σ ⇒ τ)) Δf = var (that (that this)) y = app f x Δy = app (app Δf x) (x ⊝ x) in begin ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (v ⟦⊝₍ σ ₎⟧ v) ≡⟨ cong (λ hole → ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v hole) (meaning-⊝ {s = x} {x} {ρ′}) ⟩ ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (⟦ x ⊝ x ⟧ ρ′) ≡⟨ meaning-⊕ {t = y} {Δt = Δy} {ρ′} ⟩ ⟦ y ⊕₍ τ ₎ Δy ⟧ ρ′ ∎) where open ≡-Reasoning open Disambiguation meaning-⊝ {base ι} {Γ} {s} {t} {ρ} = meaning-⊝-base ι {Γ} {s} {t} {ρ} meaning-⊝ {σ ⇒ τ} {Γ} {s} {t} {ρ} = ext (λ v → ext (λ Δv → let Γ′ = ΔType σ ⋆ σ ⋆ (σ ⇒ τ) ⋆ (σ ⇒ τ) ⋆ Γ ρ′ : ⟦ Γ′ ⟧Context ρ′ = Δv • v • ⟦ t ⟧Term ρ • ⟦ s ⟧Term ρ • ρ Δx : Term Γ′ (ΔType σ) Δx = var this x : Term Γ′ σ x = var (that this) f : Term Γ′ (σ ⇒ τ) f = var (that (that this)) g : Term Γ′ (σ ⇒ τ) g = var (that (that (that this))) y = app f x y′ = app g (x ⊕₍ σ ₎ Δx) in begin ⟦ s ⟧ ρ (v ⟦⊕₍ σ ₎⟧ Δv) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v ≡⟨ cong (λ hole → ⟦ s ⟧ ρ hole ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v) (meaning-⊕ {t = x} {Δt = Δx} {ρ′}) ⟩ ⟦ s ⟧ ρ (⟦ x ⊕₍ σ ₎ Δx ⟧ ρ′) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v ≡⟨ meaning-⊝ {s = y′} {y} {ρ′} ⟩ ⟦ y′ ⊝ y ⟧ ρ′ ∎)) where open ≡-Reasoning open Disambiguation
Document P.C.Evaluation.
Document P.C.Evaluation. Old-commit-hash: d56cb423e28dc729478353d4e2d2c4fb422d2f8f
Agda
mit
inc-lc/ilc-agda
61de78a178ea4a23092e726b277f92787528c970
Denotation/Specification/Canon-Popl14.agda
Denotation/Specification/Canon-Popl14.agda
module Denotation.Specification.Canon-Popl14 where -- Denotation-as-specification for Calculus Popl14 -- -- Contents -- - ⟦_⟧Δ : binding-time-shifted derive -- - Validity and correctness lemmas for ⟦_⟧Δ -- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments -- - `corollary-closed`: binding-time-shifted main theorem open import Popl14.Syntax.Term open import Popl14.Denotation.Value open import Popl14.Change.Validity open import Popl14.Denotation.Evaluation public open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Product open import Data.Integer open import Structure.Bag.Popl14 open import Theorem.Groups-Popl14 open import Theorem.CongApp open import Postulate.Extensionality ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (dρ : ΔEnv Γ) → Change τ -- better name is: ⟦_⟧Δ reacts to future arguments validity : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) → valid {τ} (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) -- better name is: ⟦_⟧Δ reacts to free variables correctness : ∀ {τ Γ} {t : Term Γ τ} {dρ : ΔEnv Γ} → ⟦ t ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ ≡ ⟦ t ⟧ (update dρ) ⟦_⟧ΔVar : ∀ {τ Γ} → Var Γ τ → ΔEnv Γ → Change τ ⟦ this ⟧ΔVar (cons v dv R[v,dv] • dρ) = dv ⟦ that x ⟧ΔVar (cons v dv R[v,dv] • dρ) = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (intlit n) dρ = + 0 ⟦_⟧Δ (add s t) dρ = ⟦ s ⟧Δ dρ + ⟦ t ⟧Δ dρ ⟦_⟧Δ (minus t) dρ = - ⟦ t ⟧Δ dρ ⟦_⟧Δ empty dρ = emptyBag ⟦_⟧Δ (insert s t) dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in (singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b) ⟦_⟧Δ (union s t) dρ = ⟦ s ⟧Δ dρ ++ ⟦ t ⟧Δ dρ ⟦_⟧Δ (negate t) dρ = negateBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (flatmap s t) dρ = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b ⟦_⟧Δ (sum t) dρ = sumBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (var x) dρ = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (app {σ} {τ} s t) dρ = (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) ⟦_⟧Δ (abs t) dρ = λ v → ⟦ t ⟧Δ (v • dρ) validVar : ∀ {τ Γ} (x : Var Γ τ) → (dρ : ΔEnv Γ) → valid {τ} (⟦ x ⟧ (ignore dρ)) (⟦ x ⟧ΔVar dρ) validVar this (cons v Δv R[v,Δv] • _) = R[v,Δv] validVar {τ} (that x) (cons _ _ _ • dρ) = validVar {τ} x dρ validity (intlit n) dρ = tt validity (add s t) dρ = tt validity (minus t) dρ = tt validity (empty) dρ = tt validity (insert s t) dρ = tt validity (union s t) dρ = tt validity (negate t) dρ = tt validity (flatmap s t) dρ = tt validity (sum t) dρ = tt validity {τ} (var x) dρ = validVar {τ} x dρ validity (app s t) dρ = let v = ⟦ t ⟧ (ignore dρ) Δv = ⟦ t ⟧Δ dρ in proj₁ (validity s dρ (cons v Δv (validity t dρ))) validity {σ ⇒ τ} (abs t) dρ = λ v → let v′ = after {σ} v Δv′ = v′ ⊟₍ σ ₎ v′ dρ₁ = v • dρ dρ₂ = nil-valid-change σ v′ • dρ in validity t dρ₁ , (begin ⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂ ≡⟨ correctness {t = t} {dρ₂} ⟩ ⟦ t ⟧ (update dρ₂) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (update dρ₁) ≡⟨ sym (correctness {t = t} {dρ₁}) ⟩ ⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁ ∎) where open ≡-Reasoning correctVar : ∀ {τ Γ} {x : Var Γ τ} {dρ : ΔEnv Γ} → ⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ) correctVar {x = this } {cons v dv R[v,dv] • dρ} = refl correctVar {x = that y} {cons v dv R[v,dv] • dρ} = correctVar {x = y} {dρ} correctness {t = intlit n} = right-id-int n correctness {t = add s t} {dρ} = trans (mn·pq=mp·nq {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _+_ (correctness {t = s}) (correctness {t = t})) correctness {t = minus t} {dρ} = trans (-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong -_ (correctness {t = t})) correctness {t = empty} = right-id-bag emptyBag correctness {t = insert s t} {dρ} = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) n′ = ⟦ s ⟧ (update dρ) b′ = ⟦ t ⟧ (update dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in begin (singletonBag n ++ b) ++ (singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b) ≡⟨ a++[b\\a]=b ⟩ singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb) ≡⟨ cong₂ _++_ (cong singletonBag (correctness {t = s})) (correctness {t = t}) ⟩ singletonBag n′ ++ b′ ∎ where open ≡-Reasoning correctness {t = union s t} {dρ} = trans (ab·cd=ac·bd {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _++_ (correctness {t = s}) (correctness {t = t})) correctness {t = negate t} {dρ} = trans (-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong negateBag (correctness {t = t})) correctness {t = flatmap s t} {dρ} = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in trans (a++[b\\a]=b {flatmapBag f b} {flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf) (b ⊞₍ base base-bag ₎ Δb)}) (cong₂ flatmapBag (correctness {t = s}) (correctness {t = t})) correctness {t = sum t} {dρ} = let b = ⟦ t ⟧ (ignore dρ) Δb = ⟦ t ⟧Δ dρ in trans (sym homo-sum) (cong sumBag (correctness {t = t})) correctness {τ} {t = var x} = correctVar {τ} {x = x} correctness {t = app {σ} {τ} s t} {dρ} = let f = ⟦ s ⟧ (ignore dρ) g = ⟦ s ⟧ (update dρ) u = ⟦ t ⟧ (ignore dρ) v = ⟦ t ⟧ (update dρ) Δf = ⟦ s ⟧Δ dρ Δu = ⟦ t ⟧Δ dρ in begin f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ)) ≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} {t = s} ⟨$⟩ correctness {σ} {t = t} ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} {abs t} {dρ} = ext (λ v → let dρ′ : ΔEnv (σ • Γ) dρ′ = nil-valid-change σ v • dρ in begin ⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′ ≡⟨ correctness {τ} {t = t} {dρ′} ⟩ ⟦ t ⟧ (update dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v • update dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} → (⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ) (⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ) ≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ)) ⊞₍ τ ₎ (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) corollary {σ} {τ} s t {dρ} = proj₂ (validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))) corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)} (v : ValidChange σ) → ⟦ t ⟧ ∅ (after {σ} v) ≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v corollary-closed {σ} {τ} {t = t} v = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ in begin f (after {σ} v) ≡⟨ cong (λ hole → hole (after {σ} v)) (sym (correctness {σ ⇒ τ} {t = t})) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v) ≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩ f (before {σ} v) ⊞₍ τ ₎ Δf v ∎ where open ≡-Reasoning
module Denotation.Specification.Canon-Popl14 where -- Denotation-as-specification for Calculus Popl14 -- -- Contents -- - ⟦_⟧Δ : binding-time-shifted derive -- - Validity and correctness lemmas for ⟦_⟧Δ -- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments -- - `corollary-closed`: binding-time-shifted main theorem open import Popl14.Syntax.Term open import Popl14.Denotation.Value open import Popl14.Change.Validity open import Popl14.Denotation.Evaluation public open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Product open import Data.Integer open import Structure.Bag.Popl14 open import Theorem.Groups-Popl14 open import Theorem.CongApp open import Postulate.Extensionality ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (dρ : ΔEnv Γ) → Change τ -- better name is: ⟦_⟧Δ reacts to future arguments validity : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) → valid {τ} (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) -- better name is: ⟦_⟧Δ reacts to free variables correctness : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) → ⟦ t ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ ≡ ⟦ t ⟧ (update dρ) ⟦_⟧ΔVar : ∀ {τ Γ} → Var Γ τ → ΔEnv Γ → Change τ ⟦ this ⟧ΔVar (cons v dv R[v,dv] • dρ) = dv ⟦ that x ⟧ΔVar (cons v dv R[v,dv] • dρ) = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (intlit n) dρ = + 0 ⟦_⟧Δ (add s t) dρ = ⟦ s ⟧Δ dρ + ⟦ t ⟧Δ dρ ⟦_⟧Δ (minus t) dρ = - ⟦ t ⟧Δ dρ ⟦_⟧Δ empty dρ = emptyBag ⟦_⟧Δ (insert s t) dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in (singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b) ⟦_⟧Δ (union s t) dρ = ⟦ s ⟧Δ dρ ++ ⟦ t ⟧Δ dρ ⟦_⟧Δ (negate t) dρ = negateBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (flatmap s t) dρ = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b ⟦_⟧Δ (sum t) dρ = sumBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (var x) dρ = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (app {σ} {τ} s t) dρ = (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) ⟦_⟧Δ (abs t) dρ = λ v → ⟦ t ⟧Δ (v • dρ) validVar : ∀ {τ Γ} (x : Var Γ τ) → (dρ : ΔEnv Γ) → valid {τ} (⟦ x ⟧ (ignore dρ)) (⟦ x ⟧ΔVar dρ) validVar this (cons v Δv R[v,Δv] • _) = R[v,Δv] validVar {τ} (that x) (cons _ _ _ • dρ) = validVar {τ} x dρ validity (intlit n) dρ = tt validity (add s t) dρ = tt validity (minus t) dρ = tt validity (empty) dρ = tt validity (insert s t) dρ = tt validity (union s t) dρ = tt validity (negate t) dρ = tt validity (flatmap s t) dρ = tt validity (sum t) dρ = tt validity {τ} (var x) dρ = validVar {τ} x dρ validity (app s t) dρ = let v = ⟦ t ⟧ (ignore dρ) Δv = ⟦ t ⟧Δ dρ in proj₁ (validity s dρ (cons v Δv (validity t dρ))) validity {σ ⇒ τ} (abs t) dρ = λ v → let v′ = after {σ} v Δv′ = v′ ⊟₍ σ ₎ v′ dρ₁ = v • dρ dρ₂ = nil-valid-change σ v′ • dρ in validity t dρ₁ , (begin ⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂ ≡⟨ correctness t dρ₂ ⟩ ⟦ t ⟧ (update dρ₂) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (update dρ₁) ≡⟨ sym (correctness t dρ₁) ⟩ ⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁ ∎) where open ≡-Reasoning correctVar : ∀ {τ Γ} {x : Var Γ τ} {dρ : ΔEnv Γ} → ⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ) correctVar {x = this } {cons v dv R[v,dv] • dρ} = refl correctVar {x = that y} {cons v dv R[v,dv] • dρ} = correctVar {x = y} {dρ} correctness (intlit n) dρ = right-id-int n correctness (add s t) dρ = trans (mn·pq=mp·nq {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _+_ (correctness s dρ) (correctness t dρ)) correctness (minus t) dρ = trans (-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong -_ (correctness t dρ)) correctness empty dρ = right-id-bag emptyBag correctness (insert s t) dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) n′ = ⟦ s ⟧ (update dρ) b′ = ⟦ t ⟧ (update dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in begin (singletonBag n ++ b) ++ (singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b) ≡⟨ a++[b\\a]=b ⟩ singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb) ≡⟨ cong₂ _++_ (cong singletonBag (correctness s dρ)) (correctness t dρ) ⟩ singletonBag n′ ++ b′ ∎ where open ≡-Reasoning correctness (union s t) dρ = trans (ab·cd=ac·bd {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _++_ (correctness s dρ) (correctness t dρ)) correctness (negate t) dρ = trans (-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong negateBag (correctness t dρ)) correctness (flatmap s t) dρ = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in trans (a++[b\\a]=b {flatmapBag f b} {flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf) (b ⊞₍ base base-bag ₎ Δb)}) (cong₂ flatmapBag (correctness s dρ) (correctness t dρ)) correctness (sum t) dρ = let b = ⟦ t ⟧ (ignore dρ) Δb = ⟦ t ⟧Δ dρ in trans (sym homo-sum) (cong sumBag (correctness t dρ)) correctness {τ} (var x) dρ = correctVar {τ} {x = x} correctness (app {σ} {τ} s t) dρ = let f = ⟦ s ⟧ (ignore dρ) g = ⟦ s ⟧ (update dρ) u = ⟦ t ⟧ (ignore dρ) v = ⟦ t ⟧ (update dρ) Δf = ⟦ s ⟧Δ dρ Δu = ⟦ t ⟧Δ dρ in begin f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ)) ≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s dρ ⟨$⟩ correctness {σ} t dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) dρ = ext (λ v → let dρ′ : ΔEnv (σ • Γ) dρ′ = nil-valid-change σ v • dρ in begin ⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′ ≡⟨ correctness {τ} t dρ′ ⟩ ⟦ t ⟧ (update dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v • update dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} → (⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ) (⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ) ≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ)) ⊞₍ τ ₎ (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) corollary {σ} {τ} s t {dρ} = proj₂ (validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))) corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)} (v : ValidChange σ) → ⟦ t ⟧ ∅ (after {σ} v) ≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v corollary-closed {σ} {τ} {t = t} v = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ in begin f (after {σ} v) ≡⟨ cong (λ hole → hole (after {σ} v)) (sym (correctness {σ ⇒ τ} t ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v) ≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩ f (before {σ} v) ⊞₍ τ ₎ Δf v ∎ where open ≡-Reasoning
Make some arguments of correctness explicit.
Make some arguments of correctness explicit. Old-commit-hash: b8f4ff3d6b2f0ea72c79a94c10ae3ec1b545c2ca
Agda
mit
inc-lc/ilc-agda
80463c85413df224feab7c464101dbfd5ff7ec77
Syntax/Language/Atlas.agda
Syntax/Language/Atlas.agda
module Syntax.Language.Atlas where -- Base types of the calculus Atlas -- to be used with Plotkin-style language description -- -- Atlas supports maps with neutral elements. The change type to -- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the -- ground type ι. Such a change to maps can support insertions -- and deletions as well: Inserting `k -> v` means mapping `k` to -- the change from the neutral element to `v`, and deleting -- `k -> v` means mapping `k` to the change from `v` to the -- neutral element. open import Syntax.Language.Calculus data Atlas-type : Set where Bool : Atlas-type Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type data Atlas-const : Set where true : Atlas-const false : Atlas-const xor : Atlas-const empty : ∀ {κ ι : Atlas-type} → Atlas-const update : ∀ {κ ι : Atlas-type} → Atlas-const zip : ∀ {κ a b c : Atlas-type} → Atlas-const Atlas-lookup : Atlas-const → Type Atlas-type Atlas-lookup true = base Bool Atlas-lookup false = base Bool Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool Atlas-lookup (empty {κ} {ι}) = base (Map κ ι) -- `update key val my-map` would -- - insert if `key` is not present in `my-map` -- - delete if `val` is the neutral element -- - make an update otherwise Atlas-lookup (update {κ} {ι}) = base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι) -- Model of zip = Haskell Data.List.zipWith -- zipWith :: (a → b → c) → [a] → [b] → [c] Atlas-lookup (zip {κ} {a} {b} {c}) = (base κ ⇒ base a ⇒ base b ⇒ base c) ⇒ base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c) Atlas-Δbase : Atlas-type → Atlas-type -- change to a boolean is a xor-rand Atlas-Δbase Bool = Bool -- change to a map is change to its values Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val)) Atlas-Δtype : Type Atlas-type → Type Atlas-type Atlas-Δtype = lift-Δtype₀ Atlas-Δbase Atlas-context : Set Atlas-context = Context {Type Atlas-type} Atlas-term : Atlas-context → Type Atlas-type → Set Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup} -- Every base type has a known nil-change. -- The nil-change of ι is also the neutral element of Map κ Δι. neutral : ∀ {ι : Atlas-type} → Atlas-const neutral {Bool} = false neutral {Map κ ι} = empty {κ} {ι} neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι) neutral-term {Bool} = const (neutral {Bool}) neutral-term {Map κ ι} = const (neutral {Map κ ι}) nil-const : ∀ {ι : Atlas-type} → Atlas-const nil-const {ι} = neutral {Atlas-Δbase ι} nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι)) nil-term {Bool} = const (nil-const {Bool}) nil-term {Map κ ι} = const (nil-const {Map κ ι}) -- Shorthands of constants -- -- There's probably a uniform way to lift constants -- into term constructors. -- -- TODO: write this and call it Syntax.Term.Plotkin.lift-term zip! : ∀ {κ a b c Γ} → Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) → Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) → Atlas-term Γ (base (Map κ c)) zip! f m₁ m₂ = app (app (app (const zip) f) m₁) m₂ lookup! : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) → Atlas-term Γ (base ι) lookup! = {!!} -- TODO: add constant `lookup` -- diff-term and apply-term -- b₀ ⊝ b₁ = b₀ xor b₁ -- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁ Atlas-diff : ∀ {ι Γ} → Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι)) Atlas-diff {Bool} = const xor Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff) -- b ⊕ Δb = b xor Δb -- m ⊕ Δm = zip _⊕_ m Δm Atlas-apply : ∀ {ι Γ} → Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι) Atlas-apply {Bool} = const xor Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply) -- Shorthands for working with diff-term and apply-term diff : ∀ {τ Γ} → Atlas-term Γ τ → Atlas-term Γ τ → Atlas-term Γ (Atlas-Δtype τ) diff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t apply : ∀ {τ Γ} → Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ → Atlas-term Γ τ apply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t -- Shorthands for creating changes corresponding to -- insertion/deletion. update! : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base ι) → Atlas-term Γ (base (Map κ ι)) → Atlas-term Γ (base (Map κ ι)) update! k v m = app (app (app (const update) k) v) m insert : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base ι) → -- last argument is the change accumulator Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) → Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) delete : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base ι) → Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) → Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) insert k v acc = update! k (diff v neutral-term) acc delete k v acc = update! k (diff neutral-term v) acc -- The binary operator such that -- union t t ≡ t -- To implement union, we need for each non-map base-type -- an operator such that `op v v = v` on all values `v`. -- for Booleans, conjunction is good enough. -- -- TODO (later): support conjunction, probably by Boolean -- elimination form if-then-else postulate and! : ∀ {Γ} → Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) union : ∀ {ι Γ} → Atlas-term Γ (base ι) → Atlas-term Γ (base ι) → Atlas-term Γ (base ι) union {Bool} s t = and! s t union {Map κ ι} s t = let union-term = abs (abs (union (var (that this)) (var this))) in zip! (abs union-term) s t -- Shorthand for 4-way zip zip4! : ∀ {κ a b c d e Γ} → let t:_ = λ ι → Atlas-term Γ (base ι) in Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) → t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e zip4! f m₁ m₂ m₃ m₄ = let v₁ = var (that this) v₂ = var this v₃ = var (that this) v₄ = var this k₁₂ = var (that (that this)) k₃₄ = var (that (that this)) f₁₂ = abs (abs (abs (app (app (app (app (app (weaken₃ f) k₁₂) v₁) v₂) (lookup! k₁₂ (weaken₃ m₃))) (lookup! k₁₂ (weaken₃ m₄))))) f₃₄ = abs (abs (abs (app (app (app (app (app (weaken₃ f) k₃₄) (lookup! k₃₄ (weaken₃ m₁))) (lookup! k₃₄ (weaken₃ m₂))) v₃) v₄))) in -- A correct but inefficient implementation. -- May want to speed it up after constants are finalized. union (zip! f₁₂ m₁ m₂) (zip! f₃₄ m₃ m₄) -- Type signature of Atlas-Δconst is boilerplate. Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) → Atlas-term Γ (Atlas-Δtype (Atlas-lookup c)) Atlas-Δconst true = const false Atlas-Δconst false = const false -- Δxor = λ x Δx y Δy → Δx xor Δy Atlas-Δconst xor = let Δx = var (that (that this)) Δy = var this in abs (abs (abs (abs (app (app (const xor) Δx) Δy)))) Atlas-Δconst empty = const empty -- If k ⊕ Δk ≡ k, then -- Δupdate k Δk v Δv m Δm = update k Δv Δm -- else it is a deletion followed by insertion: -- Δupdate k Δk v Δv m Δm = -- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm) -- -- We implement the else-branch only for the moment. Atlas-Δconst update = let k = var (that (that (that (that (that this))))) Δk = var (that (that (that (that this)))) v = var (that (that (that this))) Δv = var (that (that this)) -- m = var (that this) -- unused parameter Δm = var this in abs (abs (abs (abs (abs (abs (insert (apply Δk k) (apply Δv v) (delete k v Δm))))))) -- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f) -- -- ... | true = -- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂) -- Δm₁ Δm₂ -- -- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂ -- -- we implement the false-branch for the moment. Atlas-Δconst zip = let Δf = var (that (that (that (that this)))) m₁ = var (that (that (that this))) Δm₁ = var (that (that this)) m₂ = var (that this) Δm₂ = var this g = abs (app (app (weaken₁ Δf) (var this)) nil-term) in abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂)))))) Atlas = calculus-with Atlas-type Atlas-const Atlas-lookup Atlas-Δtype Atlas-Δconst
module Syntax.Language.Atlas where -- Base types of the calculus Atlas -- to be used with Plotkin-style language description -- -- Atlas supports maps with neutral elements. The change type to -- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the -- ground type ι. Such a change to maps can support insertions -- and deletions as well: Inserting `k -> v` means mapping `k` to -- the change from the neutral element to `v`, and deleting -- `k -> v` means mapping `k` to the change from `v` to the -- neutral element. open import Syntax.Language.Calculus data Atlas-type : Set where Bool : Atlas-type Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type data Atlas-const : Set where true : Atlas-const false : Atlas-const xor : Atlas-const empty : ∀ {κ ι : Atlas-type} → Atlas-const update : ∀ {κ ι : Atlas-type} → Atlas-const zip : ∀ {κ a b c : Atlas-type} → Atlas-const Atlas-lookup : Atlas-const → Type Atlas-type Atlas-lookup true = base Bool Atlas-lookup false = base Bool Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool Atlas-lookup (empty {κ} {ι}) = base (Map κ ι) -- `update key val my-map` would -- - insert if `key` is not present in `my-map` -- - delete if `val` is the neutral element -- - make an update otherwise Atlas-lookup (update {κ} {ι}) = base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι) -- Model of zip = Haskell Data.List.zipWith -- zipWith :: (a → b → c) → [a] → [b] → [c] Atlas-lookup (zip {κ} {a} {b} {c}) = (base κ ⇒ base a ⇒ base b ⇒ base c) ⇒ base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c) Atlas-Δbase : Atlas-type → Atlas-type -- change to a boolean is a xor-rand Atlas-Δbase Bool = Bool -- change to a map is change to its values Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val)) Atlas-Δtype : Type Atlas-type → Type Atlas-type Atlas-Δtype = lift-Δtype₀ Atlas-Δbase Atlas-context : Set Atlas-context = Context {Type Atlas-type} Atlas-term : Atlas-context → Type Atlas-type → Set Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup} -- Every base type has a known nil-change. -- The nil-change of ι is also the neutral element of Map κ Δι. neutral : ∀ {ι : Atlas-type} → Atlas-const neutral {Bool} = false neutral {Map κ ι} = empty {κ} {ι} neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι) neutral-term {Bool} = const (neutral {Bool}) neutral-term {Map κ ι} = const (neutral {Map κ ι}) nil-const : ∀ {ι : Atlas-type} → Atlas-const nil-const {ι} = neutral {Atlas-Δbase ι} nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι)) nil-term {Bool} = const (nil-const {Bool}) nil-term {Map κ ι} = const (nil-const {Map κ ι}) -- Shorthands of constants -- -- There's probably a uniform way to lift constants -- into term constructors. -- -- TODO: write this and call it Syntax.Term.Plotkin.lift-term zip! : ∀ {κ a b c Γ} → Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) → Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) → Atlas-term Γ (base (Map κ c)) zip! f m₁ m₂ = app (app (app (const zip) f) m₁) m₂ lookup! : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) → Atlas-term Γ (base ι) lookup! = {!!} -- TODO: add constant `lookup` -- diff-term and apply-term -- b₀ ⊝ b₁ = b₀ xor b₁ -- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁ Atlas-diff : ∀ {ι Γ} → Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι)) Atlas-diff {Bool} = const xor Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff) -- b ⊕ Δb = b xor Δb -- m ⊕ Δm = zip _⊕_ m Δm Atlas-apply : ∀ {ι Γ} → Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι) Atlas-apply {Bool} = const xor Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply) -- Shorthands for working with diff-term and apply-term diff : ∀ {τ Γ} → Atlas-term Γ τ → Atlas-term Γ τ → Atlas-term Γ (Atlas-Δtype τ) diff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t apply : ∀ {τ Γ} → Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ → Atlas-term Γ τ apply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t -- Shorthands for creating changes corresponding to -- insertion/deletion. update! : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base ι) → Atlas-term Γ (base (Map κ ι)) → Atlas-term Γ (base (Map κ ι)) update! k v m = app (app (app (const update) k) v) m insert : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base ι) → -- last argument is the change accumulator Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) → Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) delete : ∀ {κ ι Γ} → Atlas-term Γ (base κ) → Atlas-term Γ (base ι) → Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) → Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) insert k v acc = update! k (diff v neutral-term) acc delete k v acc = update! k (diff neutral-term v) acc -- union s t should be: -- 1. equal to t if s is neutral -- 2. equal to s if t is neutral -- 3. equal to s if s == t -- 4. whatever it wants to be otherwise -- -- On Booleans, only conjunction can be union. -- -- TODO (later): support conjunction, probably by Boolean -- elimination form if-then-else postulate and! : ∀ {Γ} → Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) union : ∀ {ι Γ} → Atlas-term Γ (base ι) → Atlas-term Γ (base ι) → Atlas-term Γ (base ι) union {Bool} s t = and! s t union {Map κ ι} s t = let union-term = abs (abs (union (var (that this)) (var this))) in zip! (abs union-term) s t -- Shorthand for 4-way zip zip4! : ∀ {κ a b c d e Γ} → let t:_ = λ ι → Atlas-term Γ (base ι) in Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) → t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e zip4! f m₁ m₂ m₃ m₄ = let v₁ = var (that this) v₂ = var this v₃ = var (that this) v₄ = var this k₁₂ = var (that (that this)) k₃₄ = var (that (that this)) f₁₂ = abs (abs (abs (app (app (app (app (app (weaken₃ f) k₁₂) v₁) v₂) (lookup! k₁₂ (weaken₃ m₃))) (lookup! k₁₂ (weaken₃ m₄))))) f₃₄ = abs (abs (abs (app (app (app (app (app (weaken₃ f) k₃₄) (lookup! k₃₄ (weaken₃ m₁))) (lookup! k₃₄ (weaken₃ m₂))) v₃) v₄))) in -- A correct but inefficient implementation. -- May want to speed it up after constants are finalized. union (zip! f₁₂ m₁ m₂) (zip! f₃₄ m₃ m₄) -- Type signature of Atlas-Δconst is boilerplate. Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) → Atlas-term Γ (Atlas-Δtype (Atlas-lookup c)) Atlas-Δconst true = const false Atlas-Δconst false = const false -- Δxor = λ x Δx y Δy → Δx xor Δy Atlas-Δconst xor = let Δx = var (that (that this)) Δy = var this in abs (abs (abs (abs (app (app (const xor) Δx) Δy)))) Atlas-Δconst empty = const empty -- If k ⊕ Δk ≡ k, then -- Δupdate k Δk v Δv m Δm = update k Δv Δm -- else it is a deletion followed by insertion: -- Δupdate k Δk v Δv m Δm = -- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm) -- -- We implement the else-branch only for the moment. Atlas-Δconst update = let k = var (that (that (that (that (that this))))) Δk = var (that (that (that (that this)))) v = var (that (that (that this))) Δv = var (that (that this)) -- m = var (that this) -- unused parameter Δm = var this in abs (abs (abs (abs (abs (abs (insert (apply Δk k) (apply Δv v) (delete k v Δm))))))) -- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f) -- -- ... | true = -- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂) -- Δm₁ Δm₂ -- -- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂ -- -- we implement the false-branch for the moment. Atlas-Δconst zip = let Δf = var (that (that (that (that this)))) m₁ = var (that (that (that this))) Δm₁ = var (that (that this)) m₂ = var (that this) Δm₂ = var this g = abs (app (app (weaken₁ Δf) (var this)) nil-term) in abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂)))))) Atlas = calculus-with Atlas-type Atlas-const Atlas-lookup Atlas-Δtype Atlas-Δconst
improve specification of union
Atlas: improve specification of union Old-commit-hash: cda40f70859c26a48a9077ad39b93d3b9a0af045
Agda
mit
inc-lc/ilc-agda
2c54443576bbae481639179d8b3bc48566274aad
Syntax/FreeVars/Popl14.agda
Syntax/FreeVars/Popl14.agda
module Syntax.FreeVars.Popl14 where -- Free variables of Calculus Popl14 -- -- Contents -- FV: get the free variables of a term -- closed?: test if a term is closed, producing a witness if yes. open import Popl14.Syntax.Type open import Popl14.Change.Term open import Base.Syntax.Vars Type public open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Sum FV : ∀ {τ Γ} → Term Γ τ → Vars Γ FV {Γ = Γ} (intlit n) = none FV (add s t) = FV s ∪ FV t FV (minus t) = FV t FV {Γ = Γ} empty = none FV (insert s t) = FV s ∪ FV t FV (union s t) = FV s ∪ FV t FV (negate t) = FV t FV (flatmap s t) = FV s ∪ FV t FV (sum t) = FV t FV (var x) = singleton x FV (abs t) = tail (FV t) FV (app s t) = FV s ∪ FV t closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤ closed? t = empty? (FV t)
module Syntax.FreeVars.Popl14 where -- Free variables of Calculus Popl14 -- -- Contents -- FV: get the free variables of a term -- closed?: test if a term is closed, producing a witness if yes. open import Popl14.Syntax.Type open import Popl14.Change.Term open import Base.Syntax.Vars Type public open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Sum FV : ∀ {τ Γ} → Term Γ τ → Vars Γ FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ FV (const ι ts) = FV-terms ts FV (var x) = singleton x FV (abs t) = tail (FV t) FV (app s t) = FV s ∪ FV t FV-terms ∅ = none FV-terms (t • ts) = FV t ∪ FV-terms ts closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤ closed? t = empty? (FV t)
Implement FV more parametrically.
Implement FV more parametrically. Old-commit-hash: 470f2fd7a3e73bb1ac01fce483f8aa80e4ec51e3
Agda
mit
inc-lc/ilc-agda
e71e428c1e63d766e7896b94aa406645ef65a52e
ModalLogic.agda
ModalLogic.agda
module ModalLogic where -- Implement Frank Pfenning's lambda calculus based on modal logic S4, as -- described in "A Modal Analysis of Staged Computation". open import Denotational.Notation data BaseType : Set where Base : BaseType Next : BaseType → BaseType data Type : Set where base : BaseType → Type _⇒_ : Type → Type → Type □_ : Type → Type -- Reuse contexts, variables and weakening from Tillmann's library. open import Syntactic.Contexts Type -- No semantics for these types. data Term : Context → Context → Type → Set where abs : ∀ {τ₁ τ₂ Γ Δ} → (t : Term (τ₁ • Γ) Δ τ₂) → Term Γ Δ (τ₁ ⇒ τ₂) app : ∀ {τ₁ τ₂ Γ Δ} → (t₁ : Term Γ Δ (τ₁ ⇒ τ₂)) (t₂ : Term Γ Δ τ₁) → Term Γ Δ τ₂ ovar : ∀ {Γ Δ τ} → (x : Var Γ τ) → Term Γ Δ τ mvar : ∀ {Γ Δ τ} → (u : Var Δ τ) → Term Γ Δ τ -- Note the builtin weakening box : ∀ {Γ Δ τ} → Term ∅ Δ τ → Term Γ Δ (□ τ) let-box_in-_ : ∀ {τ₁ τ₂ Γ Δ} → (e₁ : Term Γ Δ (□ τ₁)) → (e₂ : Term Γ (τ₁ • Δ) τ₂) → Term Γ Δ τ₂ -- Lemma 1 includes weakening. -- Most of the proof can be generated by C-c C-a, but syntax errors must then be fixed. weaken : ∀ {Γ₁ Γ₂ Δ₁ Δ₂ τ} → (Γ′ : Γ₁ ≼ Γ₂) → (Δ′ : Δ₁ ≼ Δ₂) → Term Γ₁ Δ₁ τ → Term Γ₂ Δ₂ τ weaken Γ′ Δ′ (abs {τ₁} t) = abs (weaken (keep τ₁ • Γ′) Δ′ t) weaken Γ′ Δ′ (app t t₁) = app (weaken Γ′ Δ′ t) (weaken Γ′ Δ′ t₁) weaken Γ′ Δ′ (ovar x) = ovar (lift Γ′ x) weaken Γ′ Δ′ (mvar u) = mvar (lift Δ′ u) weaken Γ′ Δ′ (box t) = box (weaken ∅ Δ′ t) weaken Γ′ Δ′ (let-box_in-_ {τ₁} t t₁) = let-box weaken Γ′ Δ′ t in- weaken Γ′ (keep τ₁ • Δ′) t₁
module ModalLogic where -- Implement Frank Pfenning's lambda calculus based on modal logic S4, as -- described in "A Modal Analysis of Staged Computation". open import Denotational.Notation data BaseType : Set where Base : BaseType Next : BaseType → BaseType data Type : Set where base : BaseType → Type _⇒_ : Type → Type → Type □_ : Type → Type -- Reuse contexts, variables and weakening from Tillmann's library. open import Syntactic.Contexts Type -- No semantics for these types. data Term : Context → Context → Type → Set where abs : ∀ {τ₁ τ₂ Γ Δ} → (t : Term (τ₁ • Γ) Δ τ₂) → Term Γ Δ (τ₁ ⇒ τ₂) app : ∀ {τ₁ τ₂ Γ Δ} → (t₁ : Term Γ Δ (τ₁ ⇒ τ₂)) (t₂ : Term Γ Δ τ₁) → Term Γ Δ τ₂ ovar : ∀ {Γ Δ τ} → (x : Var Γ τ) → Term Γ Δ τ mvar : ∀ {Γ Δ τ} → (u : Var Δ τ) → Term Γ Δ τ -- Note the builtin weakening box : ∀ {Γ Δ τ} → Term ∅ Δ τ → Term Γ Δ (□ τ) let-box_in-_ : ∀ {τ₁ τ₂ Γ Δ} → (e₁ : Term Γ Δ (□ τ₁)) → (e₂ : Term Γ (τ₁ • Δ) τ₂) → Term Γ Δ τ₂ -- Lemma 1 includes weakening. -- Attempt 1 at weakening, subcontext-based. This works, but it is not what we need later. -- -- Most of the proof can be generated by C-c C-a, but syntax errors must then be -- fixed and lift introduced by hand. weaken : ∀ {Γ₁ Γ₂ Δ₁ Δ₂ τ} → (Γ′ : Γ₁ ≼ Γ₂) → (Δ′ : Δ₁ ≼ Δ₂) → Term Γ₁ Δ₁ τ → Term Γ₂ Δ₂ τ weaken Γ′ Δ′ (abs {τ₁} t) = abs (weaken (keep τ₁ • Γ′) Δ′ t) weaken Γ′ Δ′ (app t t₁) = app (weaken Γ′ Δ′ t) (weaken Γ′ Δ′ t₁) weaken Γ′ Δ′ (ovar x) = ovar (lift Γ′ x) weaken Γ′ Δ′ (mvar u) = mvar (lift Δ′ u) weaken Γ′ Δ′ (box t) = box (weaken ∅ Δ′ t) weaken Γ′ Δ′ (let-box_in-_ {τ₁} t t₁) = let-box weaken Γ′ Δ′ t in- weaken Γ′ (keep τ₁ • Δ′) t₁ open import Relation.Binary.PropositionalEquality weaken-≼ : ∀ {Γ₁ Γ₂ Γ₃ Δ τ} → Term (Γ₁ ⋎ Γ₃) Δ τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) Δ τ weaken-≼ {Γ₁} {Γ₂} {Γ₃} (abs {τ₁} t) = abs (weaken-≼ {τ₁ • Γ₁} {Γ₂} t) weaken-≼ {Γ₁} {Γ₂} (app t t₁) = app (weaken-≼ {Γ₁} {Γ₂} t) (weaken-≼ {Γ₁} {Γ₂} t₁) weaken-≼ {Γ₁} {Γ₂} (ovar x) = ovar (Prefixes.lift {Γ₁} {Γ₂} x) weaken-≼ (mvar u) = mvar u weaken-≼ (box t) = box t weaken-≼ {Γ₁} {Γ₂} (let-box t in- t₁) = let-box weaken-≼ {Γ₁} {Γ₂} t in- weaken-≼ {Γ₁} {Γ₂} t₁ substVar : ∀ {Γ Γ′ Δ τ₁ τ₂} → (t : Term Γ′ Δ τ₁) → (v : Var (Γ ⋎ (τ₁ • Γ′)) τ₂) → Term (Γ ⋎ Γ′) Δ τ₂ substVar {∅} t this = t substVar {∅} t (that v) = ovar v substVar {τ • Γ} t this = ovar this substVar {τ • Γ} t (that v) = weaken-≼ {∅} {τ • ∅} (substVar {Γ} t v) substTerm : ∀ {Γ Γ′ Δ τ₁ τ₂} → (t₁ : Term Γ′ Δ τ₁) → (t₂ : Term (Γ ⋎ (τ₁ • Γ′)) Δ τ₂) → Term (Γ ⋎ Γ′) Δ τ₂ substTerm {Γ} {Γ′} t₁ (abs {τ₂} t₂) = abs ( substTerm {τ₂ • Γ} {Γ′} t₁ t₂ ) substTerm {Γ} {Γ′} t₁ (app t₂ t₃) = app (substTerm {Γ} {Γ′} t₁ t₂) (substTerm {Γ} {Γ′} t₁ t₃) substTerm {Γ} {Γ′} t₁ (ovar v) = substVar {Γ} {Γ′} t₁ v substTerm t₁ (mvar u) = mvar u substTerm t₁ (box t₂) = box t₂ substTerm {Γ} {Γ′} t₁ (let-box_in-_ {τ₃} t₂ t₃) = let-box substTerm {Γ} {Γ′} t₁ t₂ in- substTerm {Γ} {Γ′} (weaken ≼-refl (drop τ₃ • ≼-refl) t₁) t₃
implement substitution for modal logic
Agda: implement substitution for modal logic Old-commit-hash: e8580d4ac39e998fdf42c16064112cd2c0326763
Agda
mit
inc-lc/ilc-agda
d02c0c3ef60a8f1e0b8ace3fdea7c8bb38c61281
Syntax/Context.agda
Syntax/Context.agda
module Syntax.Context {Type : Set} where -- CONTEXTS -- -- This module defines the syntax of contexts, prefixes of -- contexts and variables and properties of these notions. -- -- This module is parametric in the syntax of types, so it -- can be reused for different calculi. open import Relation.Binary open import Relation.Binary.PropositionalEquality -- TYPING CONTEXTS -- Syntax data Context : Set where ∅ : Context _•_ : (τ : Type) (Γ : Context) → Context infixr 9 _•_ -- Specialized congruence rules ⟨∅⟩ : ∅ ≡ ∅ ⟨∅⟩ = refl _⟨•⟩_ : ∀ {τ₁ τ₂ Γ₁ Γ₂} → τ₁ ≡ τ₂ → Γ₁ ≡ Γ₂ → τ₁ • Γ₁ ≡ τ₂ • Γ₂ _⟨•⟩_ = cong₂ _•_ -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ -- WEAKENING -- CONTEXT PREFIXES -- -- Useful for making lemmas strong enough to prove by induction. -- -- Consider using the Subcontexts module instead. module Prefixes where -- Prefix of a context data Prefix : Context → Set where ∅ : ∀ {Γ} → Prefix Γ _•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ) -- take only the prefix of a context take : (Γ : Context) → Prefix Γ → Context take Γ ∅ = ∅ take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′ -- drop the prefix of a context drop : (Γ : Context) → Prefix Γ → Context drop Γ ∅ = Γ drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′ -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₂ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂) take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ take-drop ∅ ∅ = refl take-drop (τ • Γ) ∅ = refl take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ or-unit ∅ = refl or-unit (τ • Γ) rewrite or-unit Γ = refl move-prefix : ∀ Γ τ Γ′ → Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′ move-prefix ∅ τ Γ′ = refl move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅))) move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- SUBCONTEXTS -- -- Useful as a reified weakening operation, -- and for making theorems strong enough to prove by induction. -- -- The contents of this module are currently exported at the end -- of this file. module Subcontexts where infix 8 _≼_ data _≼_ : (Γ₁ Γ₂ : Context) → Set where ∅ : ∅ ≼ ∅ keep_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → τ • Γ₁ ≼ τ • Γ₂ drop_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → Γ₁ ≼ τ • Γ₂ -- Properties ∅≼Γ : ∀ {Γ} → ∅ ≼ Γ ∅≼Γ {∅} = ∅ ∅≼Γ {τ • Γ} = drop τ • ∅≼Γ ≼-refl : Reflexive _≼_ ≼-refl {∅} = ∅ ≼-refl {τ • Γ} = keep τ • ≼-refl ≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂ ≼-reflexive refl = ≼-refl ≼-trans : Transitive _≼_ ≼-trans ≼₁ ∅ = ≼₁ ≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂ ≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-isPreorder : IsPreorder _≡_ _≼_ ≼-isPreorder = record { isEquivalence = isEquivalence ; reflexive = ≼-reflexive ; trans = ≼-trans } ≼-preorder : Preorder _ _ _ ≼-preorder = record { Carrier = Context ; _≈_ = _≡_ ; _∼_ = _≼_ ; isPreorder = ≼-isPreorder } module ≼-Reasoning where open import Relation.Binary.PreorderReasoning ≼-preorder public renaming ( _≈⟨_⟩_ to _≡⟨_⟩_ ; _∼⟨_⟩_ to _≼⟨_⟩_ ; _≈⟨⟩_ to _≡⟨⟩_ ) -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ lift (keep τ • ≼₁) this = this lift (keep τ • ≼₁) (that x) = that (lift ≼₁ x) lift (drop τ • ≼₁) x = that (lift ≼₁ x) -- Currently, we export the subcontext relation as well as the -- definition of _⋎_. open Subcontexts public open Prefixes public using (_⋎_)
module Syntax.Context {Type : Set} where -- CONTEXTS -- -- This module defines the syntax of contexts, prefixes of -- contexts and variables and properties of these notions. -- -- This module is parametric in the syntax of types, so it -- can be reused for different calculi. open import Relation.Binary open import Relation.Binary.PropositionalEquality -- TYPING CONTEXTS -- Syntax data Context : Set where ∅ : Context _•_ : (τ : Type) (Γ : Context) → Context infixr 9 _•_ -- Specialized congruence rules ⟨∅⟩ : ∅ ≡ ∅ ⟨∅⟩ = refl _⟨•⟩_ : ∀ {τ₁ τ₂ Γ₁ Γ₂} → τ₁ ≡ τ₂ → Γ₁ ≡ Γ₂ → τ₁ • Γ₁ ≡ τ₂ • Γ₂ _⟨•⟩_ = cong₂ _•_ -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ -- WEAKENING -- CONTEXT PREFIXES -- -- Useful for making lemmas strong enough to prove by induction. -- -- Consider using the Subcontexts module instead. module Prefixes where -- Prefix of a context data Prefix : Context → Set where ∅ : ∀ {Γ} → Prefix Γ _•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ) -- take only the prefix of a context take : (Γ : Context) → Prefix Γ → Context take Γ ∅ = ∅ take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′ -- drop the prefix of a context drop : (Γ : Context) → Prefix Γ → Context drop Γ ∅ = Γ drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′ -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₂ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂) take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ take-drop ∅ ∅ = refl take-drop (τ • Γ) ∅ = refl take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ or-unit ∅ = refl or-unit (τ • Γ) rewrite or-unit Γ = refl move-prefix : ∀ Γ τ Γ′ → Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′ move-prefix ∅ τ Γ′ = refl move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅))) move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- SUBCONTEXTS -- -- Useful as a reified weakening operation, -- and for making theorems strong enough to prove by induction. -- -- The contents of this module are currently exported at the end -- of this file. module Subcontexts where infix 8 _≼_ data _≼_ : (Γ₁ Γ₂ : Context) → Set where ∅ : ∅ ≼ ∅ keep_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → τ • Γ₁ ≼ τ • Γ₂ drop_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → Γ₁ ≼ τ • Γ₂ -- Properties ∅≼Γ : ∀ {Γ} → ∅ ≼ Γ ∅≼Γ {∅} = ∅ ∅≼Γ {τ • Γ} = drop τ • ∅≼Γ ≼-refl : Reflexive _≼_ ≼-refl {∅} = ∅ ≼-refl {τ • Γ} = keep τ • ≼-refl ≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂ ≼-reflexive refl = ≼-refl ≼-trans : Transitive _≼_ ≼-trans ≼₁ ∅ = ≼₁ ≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂ ≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-isPreorder : IsPreorder _≡_ _≼_ ≼-isPreorder = record { isEquivalence = isEquivalence ; reflexive = ≼-reflexive ; trans = ≼-trans } ≼-preorder : Preorder _ _ _ ≼-preorder = record { Carrier = Context ; _≈_ = _≡_ ; _∼_ = _≼_ ; isPreorder = ≼-isPreorder } module ≼-Reasoning where open import Relation.Binary.PreorderReasoning ≼-preorder public renaming ( _≈⟨_⟩_ to _≡⟨_⟩_ ; _∼⟨_⟩_ to _≼⟨_⟩_ ; _≈⟨⟩_ to _≡⟨⟩_ ) -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ lift (keep τ • ≼₁) this = this lift (keep τ • ≼₁) (that x) = that (lift ≼₁ x) lift (drop τ • ≼₁) x = that (lift ≼₁ x) -- Currently, we export the subcontext relation as well as the -- definition of _⋎_. open Subcontexts public open Prefixes public using (_⋎_)
rename type parameter of `that`
Agda: rename type parameter of `that` Old-commit-hash: 0b30e5858214cb3ecab7a1661b7a2a79ac6bc871
Agda
mit
inc-lc/ilc-agda
cd132a2ada356a76c361341940070bcfccc2c571
lib/Explore/Zero.agda
lib/Explore/Zero.agda
open import Type open import Type.Identities open import Level.NP open import Explore.Core open import Explore.Properties open import Explore.Explorable open import Data.Zero open import Function.NP open import Function.Extensionality open import Data.Product open import Relation.Binary.PropositionalEquality.NP using (_≡_; refl; _∙_; !_) open import HoTT open Equivalences import Explore.Monad module Explore.Zero where module _ {ℓ} where 𝟘ᵉ : Explore ℓ 𝟘 𝟘ᵉ = empty-explore {- or 𝟘ᵉ ε _ _ = ε -} 𝟘ⁱ : ∀ {p} → ExploreInd p 𝟘ᵉ 𝟘ⁱ = empty-explore-ind {- or 𝟘ⁱ _ Pε _ _ = Pε -} module _ {ℓ₁ ℓ₂ ℓᵣ} {R : 𝟘 → 𝟘 → ★₀} where ⟦𝟘ᵉ⟧ : ⟦Explore⟧ᵤ ℓ₁ ℓ₂ ℓᵣ R 𝟘ᵉ 𝟘ᵉ ⟦𝟘ᵉ⟧ _ εᵣ _ _ = εᵣ open Explorable₀ 𝟘ⁱ public using () renaming (sum to 𝟘ˢ; product to 𝟘ᵖ) module _ {ℓ} where open Explorableₛ {ℓ} 𝟘ⁱ public using () renaming (reify to 𝟘ʳ) open Explorableₛₛ {ℓ} 𝟘ⁱ public using () renaming (unfocus to 𝟘ᵘ) 𝟘ˡ : Lookup {ℓ} 𝟘ᵉ 𝟘ˡ _ () 𝟘ᶠ : Focus {ℓ} 𝟘ᵉ 𝟘ᶠ ((), _) module _ {{_ : UA}} where Σᵉ𝟘-ok : Adequate-Σᵉ {ℓ} 𝟘ᵉ Σᵉ𝟘-ok _ = ! Σ𝟘-lift∘fst module _ {{_ : UA}}{{_ : FunExt}} where Πᵉ𝟘-ok : Adequate-Πᵉ {ℓ} 𝟘ᵉ Πᵉ𝟘-ok _ = ! Π𝟘-uniq _ module _ {{_ : UA}} where 𝟘ˢ-ok : Adequate-sum 𝟘ˢ 𝟘ˢ-ok _ = Fin0≡𝟘 ∙ ! Σ𝟘-fst module _ {{_ : UA}}{{_ : FunExt}} where 𝟘ᵖ-ok : Adequate-product 𝟘ᵖ 𝟘ᵖ-ok _ = Fin1≡𝟙 ∙ ! (Π𝟘-uniq₀ _) explore𝟘 = 𝟘ᵉ explore𝟘-ind = 𝟘ⁱ lookup𝟘 = 𝟘ˡ reify𝟘 = 𝟘ʳ focus𝟘 = 𝟘ᶠ unfocus𝟘 = 𝟘ᵘ sum𝟘 = 𝟘ˢ adequate-sum𝟘 = 𝟘ˢ-ok product𝟘 = 𝟘ᵖ adequate-product𝟘 = 𝟘ᵖ-ok -- DEPRECATED module _ {{_ : UA}} where open ExplorableRecord μ𝟘 : Explorable 𝟘 μ𝟘 = mk _ 𝟘ⁱ 𝟘ˢ-ok -- -}
open import Type open import Type.Identities open import Level.NP open import Explore.Core open import Explore.Properties open import Explore.Explorable open import Data.Zero open import Function.NP open import Function.Extensionality open import Data.Product open import Relation.Binary.PropositionalEquality.NP using (_≡_; refl; _∙_; !_) open import HoTT open Equivalences import Explore.Monad open import Explore.Isomorphism module Explore.Zero where module _ {ℓ} where 𝟘ᵉ : Explore ℓ 𝟘 𝟘ᵉ = empty-explore {- or 𝟘ᵉ ε _ _ = ε -} 𝟘ⁱ : ∀ {p} → ExploreInd p 𝟘ᵉ 𝟘ⁱ = empty-explore-ind {- or 𝟘ⁱ _ Pε _ _ = Pε -} module _ {ℓ₁ ℓ₂ ℓᵣ} {R : 𝟘 → 𝟘 → ★₀} where ⟦𝟘ᵉ⟧ : ⟦Explore⟧ᵤ ℓ₁ ℓ₂ ℓᵣ R 𝟘ᵉ 𝟘ᵉ ⟦𝟘ᵉ⟧ _ εᵣ _ _ = εᵣ open Explorable₀ 𝟘ⁱ public using () renaming (sum to 𝟘ˢ; product to 𝟘ᵖ) module _ {ℓ} where open Explorableₛ {ℓ} 𝟘ⁱ public using () renaming (reify to 𝟘ʳ) open Explorableₛₛ {ℓ} 𝟘ⁱ public using () renaming (unfocus to 𝟘ᵘ) 𝟘ˡ : Lookup {ℓ} 𝟘ᵉ 𝟘ˡ _ () 𝟘ᶠ : Focus {ℓ} 𝟘ᵉ 𝟘ᶠ ((), _) module _ {{_ : UA}} where Σᵉ𝟘-ok : Adequate-Σᵉ {ℓ} 𝟘ᵉ Σᵉ𝟘-ok _ = ! Σ𝟘-lift∘fst module _ {{_ : UA}}{{_ : FunExt}} where Πᵉ𝟘-ok : Adequate-Πᵉ {ℓ} 𝟘ᵉ Πᵉ𝟘-ok _ = ! Π𝟘-uniq _ module _ {{_ : UA}} where 𝟘ˢ-ok : Adequate-sum 𝟘ˢ 𝟘ˢ-ok _ = Fin0≡𝟘 ∙ ! Σ𝟘-fst module _ {{_ : UA}}{{_ : FunExt}} where 𝟘ᵖ-ok : Adequate-product 𝟘ᵖ 𝟘ᵖ-ok _ = Fin1≡𝟙 ∙ ! (Π𝟘-uniq₀ _) explore𝟘 = 𝟘ᵉ explore𝟘-ind = 𝟘ⁱ lookup𝟘 = 𝟘ˡ reify𝟘 = 𝟘ʳ focus𝟘 = 𝟘ᶠ unfocus𝟘 = 𝟘ᵘ sum𝟘 = 𝟘ˢ adequate-sum𝟘 = 𝟘ˢ-ok product𝟘 = 𝟘ᵖ adequate-product𝟘 = 𝟘ᵖ-ok Lift𝟘ᵉ : ∀ {m} → Explore m (Lift 𝟘) Lift𝟘ᵉ = explore-iso (≃-sym Lift≃id) 𝟘ᵉ ΣᵉLift𝟘-ok : ∀ {{_ : UA}}{{_ : FunExt}}{m} → Adequate-Σᵉ {m} Lift𝟘ᵉ ΣᵉLift𝟘-ok = Σ-iso-ok (≃-sym Lift≃id) {Aᵉ = 𝟘ᵉ} Σᵉ𝟘-ok -- DEPRECATED module _ {{_ : UA}} where open ExplorableRecord μ𝟘 : Explorable 𝟘 μ𝟘 = mk _ 𝟘ⁱ 𝟘ˢ-ok -- -}
add explore function for Lift 𝟘
add explore function for Lift 𝟘
Agda
bsd-3-clause
crypto-agda/explore
8aefca99408c27e4a214ce4b0721a4e4794eeb87
Atlas/Change/Derivation.agda
Atlas/Change/Derivation.agda
module Atlas.Change.Derivation where open import Atlas.Syntax.Type open import Atlas.Syntax.Term open import Atlas.Change.Type open import Atlas.Change.Term open import Base.Syntax.Context Type open import Base.Change.Context ΔType Atlas-Δconst : ∀ {Γ Σ τ} → (c : Const Σ τ) → Term Γ (internalizeContext (ΔContext′ Σ) (ΔType τ)) Atlas-Δconst true = false! Atlas-Δconst false = false! Atlas-Δconst xor = abs₄ (λ x Δx y Δy → xor! Δx Δy) Atlas-Δconst empty = empty! -- If k ⊕ Δk ≡ k, then -- Δupdate k Δk v Δv m Δm = update k Δv Δm -- else it is a deletion followed by insertion: -- Δupdate k Δk v Δv m Δm = -- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm) -- -- We implement the else-branch only for the moment. Atlas-Δconst update = abs₆ (λ k Δk v Δv m Δm → insert (apply Δk k) (apply Δv v) (delete k v Δm)) -- Δlookup k Δk m Δm | true? (k ⊕ Δk ≡ k) -- ... | true = lookup k Δm -- ... | false = -- (lookup (k ⊕ Δk) m ⊕ lookup (k ⊕ Δk) Δm) -- ⊝ lookup k m -- -- Only the false-branch is implemented. Atlas-Δconst lookup = abs₄ (λ k Δk m Δm → let k′ = apply Δk k in (diff (apply {base _} (lookup! k′ Δm) (lookup! k′ m)) (lookup! k m))) -- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f) -- -- ... | true = -- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂) -- Δm₁ Δm₂ -- -- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂ -- -- we implement the false-branch for the moment. Atlas-Δconst zip = abs₆ (λ f Δf m₁ Δm₁ m₂ Δm₂ → let g = abs (app₂ (weaken₁ Δf) (var this) nil-term) in zip4! g m₁ Δm₁ m₂ Δm₂) -- Δfold f Δf z Δz m Δm = proj₂ -- (fold (λ k [a,Δa] [b,Δb] → -- uncurry (λ a Δa → uncurry (λ b Δb → -- pair (f k a b) (Δf k nil a Δa b Δb)) -- [b,Δb]) [a,Δa]) -- (pair z Δz) -- (zip-pair m Δm)) -- -- Δfold is efficient only if evaluation is lazy and Δf is -- self-maintainable: it doesn't look at the argument -- (b = fold f k a b₀) at all. Atlas-Δconst (fold {κ} {α} {β}) = let -- TODO (tedius): write weaken₇ f = weaken₃ (weaken₃ (weaken₁ (var (that (that (that (that (that this)))))))) Δf = weaken₃ (weaken₃ (weaken₁ (var (that (that (that (that this))))))) z = var (that (that (that this))) Δz = var (that (that this)) m = var (that this) Δm = var this k = weaken₃ (weaken₁ (var (that (that this)))) [a,Δa] = var (that this) [b,Δb] = var this a = var (that (that (that this))) Δa = var (that (that this)) b = var (that this) Δb = var this g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs (pair (app₃ f k a b) (app₆ Δf k nil-term a Δa b Δb)))) (weaken₂ [b,Δb])))) [a,Δa]))) proj₂ = uncurry (abs (abs (var this))) in abs (abs (abs (abs (abs (abs (proj₂ (fold! g (pair z Δz) (zip-pair m Δm))))))))
module Atlas.Change.Derivation where open import Atlas.Syntax.Type open import Atlas.Syntax.Term open import Atlas.Change.Type open import Atlas.Change.Term open import Base.Syntax.Context Type open import Base.Change.Context ΔType ΔConst : ∀ {Γ Σ τ} → (c : Const Σ τ) → Term Γ (internalizeContext (ΔContext′ Σ) (ΔType τ)) ΔConst true = false! ΔConst false = false! ΔConst xor = abs₄ (λ x Δx y Δy → xor! Δx Δy) ΔConst empty = empty! -- If k ⊕ Δk ≡ k, then -- Δupdate k Δk v Δv m Δm = update k Δv Δm -- else it is a deletion followed by insertion: -- Δupdate k Δk v Δv m Δm = -- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm) -- -- We implement the else-branch only for the moment. ΔConst update = abs₆ (λ k Δk v Δv m Δm → insert (apply Δk k) (apply Δv v) (delete k v Δm)) -- Δlookup k Δk m Δm | true? (k ⊕ Δk ≡ k) -- ... | true = lookup k Δm -- ... | false = -- (lookup (k ⊕ Δk) m ⊕ lookup (k ⊕ Δk) Δm) -- ⊝ lookup k m -- -- Only the false-branch is implemented. ΔConst lookup = abs₄ (λ k Δk m Δm → let k′ = apply Δk k in (diff (apply {base _} (lookup! k′ Δm) (lookup! k′ m)) (lookup! k m))) -- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f) -- -- ... | true = -- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂) -- Δm₁ Δm₂ -- -- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂ -- -- we implement the false-branch for the moment. ΔConst zip = abs₆ (λ f Δf m₁ Δm₁ m₂ Δm₂ → let g = abs (app₂ (weaken₁ Δf) (var this) nil-term) in zip4! g m₁ Δm₁ m₂ Δm₂) -- Δfold f Δf z Δz m Δm = proj₂ -- (fold (λ k [a,Δa] [b,Δb] → -- uncurry (λ a Δa → uncurry (λ b Δb → -- pair (f k a b) (Δf k nil a Δa b Δb)) -- [b,Δb]) [a,Δa]) -- (pair z Δz) -- (zip-pair m Δm)) -- -- Δfold is efficient only if evaluation is lazy and Δf is -- self-maintainable: it doesn't look at the argument -- (b = fold f k a b₀) at all. ΔConst (fold {κ} {α} {β}) = let -- TODO (tedius): write weaken₇ f = weaken₃ (weaken₃ (weaken₁ (var (that (that (that (that (that this)))))))) Δf = weaken₃ (weaken₃ (weaken₁ (var (that (that (that (that this))))))) z = var (that (that (that this))) Δz = var (that (that this)) m = var (that this) Δm = var this k = weaken₃ (weaken₁ (var (that (that this)))) [a,Δa] = var (that this) [b,Δb] = var this a = var (that (that (that this))) Δa = var (that (that this)) b = var (that this) Δb = var this g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs (pair (app₃ f k a b) (app₆ Δf k nil-term a Δa b Δb)))) (weaken₂ [b,Δb])))) [a,Δa]))) proj₂ = uncurry (abs (abs (var this))) in abs (abs (abs (abs (abs (abs (proj₂ (fold! g (pair z Δz) (zip-pair m Δm))))))))
Rename Atlas-Δconst to ΔConst.
Rename Atlas-Δconst to ΔConst. Old-commit-hash: e7ed19ec0ca363e486fe72ac858facfcd192e057
Agda
mit
inc-lc/ilc-agda
af1d5371ab683fac0cfee48a20cabc40e4da7d41
program-distance.agda
program-distance.agda
module program-distance where open import flipbased-implem open import Data.Bits open import Data.Bool open import Data.Vec.NP using (Vec; count; countᶠ) open import Data.Nat.NP open import Data.Nat.Properties open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP import Data.Fin as Fin record PrgDist : Set₁ where constructor mk field _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-right-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ]-[ g → g ≈⅁ h → f ]-[ h ]-[-cong-right-≈↺ pf pf' = ]-[-sym (]-[-cong-left-≈↺ (sym pf') (]-[-sym pf)) ]-[-cong-≗↺ : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' ]-[-cong-≗↺ {c} {c'} {f} {g} {f'} {g'} f≗g f'≗g' pf = ]-[-cong-left-≈↺ {f = g} {g = f} {h = g'} ((≗⇒≈⅁ (λ x → sym (f≗g x)))) (]-[-cong-right-≈↺ pf (≗⇒≈⅁ f'≗g')) breaks : ∀ {c} (EXP : Bit → ⅁ c) → Set breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b -- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁ _⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ⅁ c₀) (⅁₁ : Bit → ⅁ c₁) → Set ⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁ extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ⅁ c} → ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁ extensional-reduction same-games = ]-[-cong-≗↺ (same-games 0b) (same-games 1b) module HomImplem k where -- | Pr[ f ≡ 1 ] - Pr[ g ≡ 1 ] | ≥ ε [ on reals ] -- dist Pr[ f ≡ 1 ] Pr[ g ≡ 1 ] ≥ ε [ on reals ] -- dist (#1 f / 2^ c) (#1 g / 2^ c) ≥ ε [ on reals ] -- dist (#1 f) (#1 g) ≥ ε * 2^ c where ε = 2^ -k [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(-k) * 2^ c [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c - k) [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c ∸ k) [ on natural ] _]-[_ : ∀ {n} (f g : ⅁ n) → Set _]-[_ {n} f g = dist (count↺ f) (count↺ g) ≥ 2^(n ∸ k) ]-[-sym : ∀ {n} {f g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count↺ f) (count↺ g) = f]-[g ]-[-cong-left-≈↺ : ∀ {n} {f g h : ⅁ n} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {n} {f} {g} f≈g g]-[h rewrite ≈⅁⇒≈⅁′ {n} {f} {g} f≈g = g]-[h -- dist #g #h ≥ 2^(n ∸ k) -- dist #f #h ≥ 2^(n ∸ k) module Implem k where _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set _]-[_ {m} {n} f g = dist (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) ≥ 2^((m + n) ∸ k) ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) | ℕ°.+-comm m n = f]-[g postulate helper : ∀ x y z t → x + ((y + z) ∸ t) ≡ y + ((x + z) ∸ t) ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} f≈g g]-[h with 2^*-mono m g]-[h ... | q rewrite sym (dist-2^* m (⟨2^ o ⟩* (count↺ g)) (⟨2^ n ⟩* (count↺ h))) | 2^-comm m o (count↺ g) | sym f≈g | 2^-comm o n (count↺ f) | 2^-comm m n (count↺ h) | dist-2^* n (⟨2^ o ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ h)) | 2^-+ m (n + o ∸ k) 1 | helper m n o k | sym (2^-+ n (m + o ∸ k) 1) = 2^*-mono′ n q prgDist : PrgDist prgDist = mk _]-[_ (λ {m n f g} x → ]-[-sym {m} {n} {f} {g} x) (λ {m n o f g h} x y → ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} x y)
module program-distance where open import flipbased-implem open import Data.Bits open import Data.Bool open import Data.Vec.NP using (Vec; count; countᶠ) open import Data.Nat.NP open import Data.Nat.Properties open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP import Data.Fin as Fin record PrgDist : Set₁ where constructor mk field _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-right-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ]-[ g → g ≈⅁ h → f ]-[ h ]-[-cong-right-≈↺ pf pf' = ]-[-sym (]-[-cong-left-≈↺ (sym pf') (]-[-sym pf)) ]-[-cong-≗↺ : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' ]-[-cong-≗↺ {c} {c'} {f} {g} {f'} {g'} f≗g f'≗g' pf = ]-[-cong-left-≈↺ {f = g} {g = f} {h = g'} ((≗⇒≈⅁ (λ x → sym (f≗g x)))) (]-[-cong-right-≈↺ pf (≗⇒≈⅁ f'≗g')) breaks : ∀ {c} (EXP : Bit → ⅁ c) → Set breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b -- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁ _⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ⅁ c₀) (⅁₁ : Bit → ⅁ c₁) → Set ⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁ extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ⅁ c} → ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁ extensional-reduction same-games = ]-[-cong-≗↺ (same-games 0b) (same-games 1b) module HomImplem k where -- | Pr[ f ≡ 1 ] - Pr[ g ≡ 1 ] | ≥ ε [ on reals ] -- dist Pr[ f ≡ 1 ] Pr[ g ≡ 1 ] ≥ ε [ on reals ] -- dist (#1 f / 2^ c) (#1 g / 2^ c) ≥ ε [ on reals ] -- dist (#1 f) (#1 g) ≥ ε * 2^ c where ε = 2^ -k [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(-k) * 2^ c [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c - k) [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c ∸ k) [ on natural ] _]-[_ : ∀ {n} (f g : ⅁ n) → Set _]-[_ {n} f g = dist (count↺ f) (count↺ g) ≥ 2^(n ∸ k) ]-[-sym : ∀ {n} {f g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count↺ f) (count↺ g) = f]-[g ]-[-cong-left-≈↺ : ∀ {n} {f g h : ⅁ n} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {n} {f} {g} f≈g g]-[h rewrite ≈⅁⇒≈⅁′ {n} {f} {g} f≈g = g]-[h -- dist #g #h ≥ 2^(n ∸ k) -- dist #f #h ≥ 2^(n ∸ k) module Implem k where _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set _]-[_ {m} {n} f g = dist (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) ≥ 2^((m + n) ∸ k) ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) | ℕ°.+-comm m n = f]-[g postulate helper : ∀ x y z t → x + ((y + z) ∸ t) ≡ y + ((x + z) ∸ t) ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} f≈g g]-[h with 2^*-mono m g]-[h -- 2ᵐ(dist 2ᵒ#g 2ⁿ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ ... | q rewrite sym (dist-2^* m (⟨2^ o ⟩* (count↺ g)) (⟨2^ n ⟩* (count↺ h))) -- dist 2ᵐ2ᵒ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm m o (count↺ g) -- dist 2ᵒ2ᵐ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | sym f≈g -- dist 2ᵒ2ⁿ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm o n (count↺ f) -- dist 2ⁿ2ᵒ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm m n (count↺ h) -- dist 2ⁿ2ᵒ#f 2ⁿ2ᵐ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | dist-2^* n (⟨2^ o ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ h)) -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-+ m (n + o ∸ k) 1 -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ⁺ⁿ⁺ᵒ⁻ᵏ | helper m n o k -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ⁺ᵐ⁺ᵒ⁻ᵏ | sym (2^-+ n (m + o ∸ k) 1) -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ2ᵐ⁺ᵒ⁻ᵏ = 2^*-mono′ n q -- dist 2ᵒ#f 2ᵐ#h ≤ 2ᵐ⁺ᵒ⁻ᵏ prgDist : PrgDist prgDist = mk _]-[_ (λ {m n f g} x → ]-[-sym {m} {n} {f} {g} x) (λ {m n o f g h} x y → ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} x y)
document a broken (so far) proof
document a broken (so far) proof
Agda
bsd-3-clause
crypto-agda/crypto-agda
d0df7845258df353bcf850c309c4a494f9869248
Parametric/Denotation/CachingEvaluation.agda
Parametric/Denotation/CachingEvaluation.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Caching evaluation ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation module Parametric.Denotation.CachingEvaluation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality module Structure where {- -- Prototype here the type-correctness of a simple non-standard semantics. -- This describes a simplified version of the transformation by Liu and -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language -- terms, we produce host language terms to take advantage of the richer type -- system of the host language (in particular, here we need the unit type, -- product types and *existentials*). -- -- As usual, we'll later switch to a real term transformation. -} open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level -- Type semantics for this scenario. ⟦_⟧TypeHidCache : (τ : Type) → Set₁ ⟦ base ι ⟧TypeHidCache = Lift ⟦ base ι ⟧ ⟦ σ ⇒ τ ⟧TypeHidCache = ⟦ σ ⟧TypeHidCache → (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧TypeHidCache × τ₁ ) open import Parametric.Syntax.MType as MType open MType.Structure Base open import Parametric.Syntax.MTerm as MTerm open MTerm.Structure Const open import Function hiding (const) ⟦_⟧ValType : (τ : ValType) → Set ⟦_⟧CompType : (τ : CompType) → Set ⟦ U c ⟧ValType = ⟦ c ⟧CompType ⟦ B ι ⟧ValType = ⟦ base ι ⟧ ⟦ vUnit ⟧ValType = ⊤ ⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType ⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType ⟦ F τ ⟧CompType = ⟦ τ ⟧ValType ⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧CtxHidCache : (Γ : Context) → Set₁ ⟦_⟧CtxHidCache = DependentList ⟦_⟧TypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -} -- It's questionable that this is not polymorphic enough. ⟦_⟧VarHidCache : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦ this ⟧VarHidCache (v • ρ) = v ⟦ that x ⟧VarHidCache (v • ρ) = ⟦ x ⟧VarHidCache ρ -- Now, let us define a caching semantics for terms. -- This proves to be hard, because we need to insert and remove caches where -- we apply constants. -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us. -- -- Inserting and removing caches -- -- -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time. -- Indeed, both functions seem structurally recursive on τ. dropCache : ∀ {τ} → ⟦ τ ⟧TypeHidCache → ⟦ τ ⟧ extend : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧TypeHidCache dropCache {base ι} v = lower v dropCache {σ ⇒ τ} v x = dropCache (proj₁ (proj₂ (v (extend x)))) extend {base ι} v = lift v extend {σ ⇒ τ} v = λ x → , (extend (v (dropCache x)) , tt) -- OK, this version is syntax-directed, luckily enough, except on primitives -- (as expected). This reveals a bug of ours on higher-order primitives. -- -- Moreover, we can somewhat safely assume that each call to extend and to -- dropCache is bad: then we see that the handling of constants is bad. That's -- correct, because constants will not return intermediate results in this -- schema :-(. ⟦_⟧TermCache : ∀ {τ Γ} → Term Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦_⟧TermCache (const c args) ρ = extend (⟦ const c args ⟧ (map dropCache ρ)) ⟦_⟧TermCache (var x) ρ = ⟦ x ⟧VarHidCache ρ -- It seems odd (a probable bug?) that the result of t needn't be stripped of -- its cache. ⟦_⟧TermCache (app s t) ρ = proj₁ (proj₂ ((⟦ s ⟧TermCache ρ) (⟦ t ⟧TermCache ρ))) -- Provide an empty cache! ⟦_⟧TermCache (abs t) ρ x = , (⟦ t ⟧TermCache (x • ρ) , tt) -- The solution is to distinguish among different kinds of constants. Some are -- value constructors (and thus do not return caches), while others are -- computation constructors (and thus should return caches). For products, I -- believe we will only use the products which are values, not computations -- (XXX check CBPV paper for the name). ⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public using () renaming (⟦_⟧Var to ⟦_⟧ValVar) -- This says that the environment does not contain caches... sounds wrong! -- Either we add extra variables for the caches, or we store computations in -- the environment (but that does not make sense), or we store caches in -- values, by acting not on F but on something else (U?). -- I suspect the plan was to use extra variables; that's annoying to model in -- Agda but easier in implementations. ⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ ⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ -- The real deal, finally. open import UNDEFINED -- XXX constants are still a slight mess because I'm abusing CBPV... -- (Actually, I just forgot the difference, and believe I had too little clue -- when I wrote these constructors... but some of them did make sense). ⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED -- Also, where are introduction forms for pairs and sums among values? With -- them, we should see that we can interpret them without adding a cache. -- Thunks keep seeming noops. ⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ -- Here, in an actual implementation, we would return the actual cache with -- all variables. -- -- The effect of F is a writer monad of cached values, where the monoid is -- (isomorphic to) the free monoid over (∃ τ . τ), but we push the -- existentials up when pairing things! -- That's what we're interpreting computations in. XXX maybe consider using -- monads directly. But that doesn't deal with arity. ⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt) -- For this to be enough, a lambda needs to return a produce, not to forward -- the underlying one (unless there are no intermediate results). The correct -- requirements can maybe be enforced through a linear typing discipline. {- -- Here we'd have a problem with the original into from CBPV, because it does -- not require converting expressions to the "CBPV A-normal form". ⟦_⟧CompTermCache (v₁ into v₂) ρ = -- Sequence commands and combine their caches. {- let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in , (r₂ , (c₁ ,′ c₂)) -} -- The code above does not work, because we only guarantee that v₂ is -- a computation, not that it's an F-computation - v₂ could also be function -- type or a computation product. -- We need a restricted CBPV, where these two possibilities are forbidden. If -- you want to save something between different lambdas, you need to add an F -- U to reflect that. (Double-check the papers which show how to encode arity -- using CBPV, it seems that they should find the same problem --- XXX they -- don't). -- Instead, just drop the first cache (XXX WRONG). ⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ) -} -- But if we alter _into_ as described above, composing the caches works! -- However, we should not forget we also need to save the new intermediate -- result, that is the one produced by the first part of the let. ⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ = let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂)) -- Note the compositionality and luck: we don't need to do anything at the -- cReturn time, we just need the nested into to do their job, because as I -- said intermediate results are a writer monad. -- -- Q: But then, do we still need to do all the other stuff? IOW, do we still -- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <- -- f args; z <- g args'; z)? -- -- A: One thing we still need is using the monadic version of into for the -- same reasons - which makes sense, since it has the type of monadic bind. -- -- Maybe not: if we use a monad, which respects left and right identity, the -- two above forms are equivalent. But what about associativity? We don't have -- associativity with nested tuples in the middle. That's why the monad uses -- lists! We can also use nested tuple, as long as in the into case we don't -- do (a, b) but append a b (ahem, where?), which decomposes the first list -- and prepends it to the second. To this end, we need to know the type of the -- first element, or to ensure it's always a pair. Maybe we just want to reuse -- an HList. -- In abstractions, we should start collecting all variables... -- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache, -- that's moved into the handling of cReturn. This makes *the* difference for -- nested lambdas, where we don't need to create caches multiple times! ⟦_⟧CompTermCache (cAbs v) ρ x = ⟦ v ⟧CompTermCache (x • ρ) -- Here we see that we are in a sort of A-normal form, because the argument is -- a value (not quite ANF though, since values can be thunks - that is, -- computations which haven't been run yet, I guess. Do we have an use for -- that? That allows passing lambdas as arguments directly - which is fine, -- because producing a closure indeed does not have intermediate results!). ⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Caching evaluation ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation module Parametric.Denotation.CachingEvaluation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality module Structure where {- -- Prototype here the type-correctness of a simple non-standard semantics. -- This describes a simplified version of the transformation by Liu and -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language -- terms, we produce host language terms to take advantage of the richer type -- system of the host language (in particular, here we need the unit type, -- product types and *existentials*). -- -- As usual, we'll later switch to a real term transformation. -} open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level -- Type semantics for this scenario. ⟦_⟧TypeHidCache : (τ : Type) → Set₁ ⟦ base ι ⟧TypeHidCache = Lift ⟦ base ι ⟧ ⟦ σ ⇒ τ ⟧TypeHidCache = ⟦ σ ⟧TypeHidCache → (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧TypeHidCache × τ₁ ) -- Now, let us define a caching semantics for terms. -- This proves to be hard, because we need to insert and remove caches where -- we apply constants. -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us. -- -- Inserting and removing caches -- -- -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time. -- Indeed, both functions seem structurally recursive on τ. dropCache : ∀ {τ} → ⟦ τ ⟧TypeHidCache → ⟦ τ ⟧ extend : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧TypeHidCache dropCache {base ι} v = lower v dropCache {σ ⇒ τ} v x = dropCache (proj₁ (proj₂ (v (extend x)))) extend {base ι} v = lift v extend {σ ⇒ τ} v = λ x → , (extend (v (dropCache x)) , tt) ⟦_⟧CtxHidCache : (Γ : Context) → Set₁ ⟦_⟧CtxHidCache = DependentList ⟦_⟧TypeHidCache -- It's questionable that this is not polymorphic enough. ⟦_⟧VarHidCache : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦ this ⟧VarHidCache (v • ρ) = v ⟦ that x ⟧VarHidCache (v • ρ) = ⟦ x ⟧VarHidCache ρ -- OK, this version is syntax-directed, luckily enough, except on primitives -- (as expected). This reveals a bug of ours on higher-order primitives. -- -- Moreover, we can somewhat safely assume that each call to extend and to -- dropCache is bad: then we see that the handling of constants is bad. That's -- correct, because constants will not return intermediate results in this -- schema :-(. ⟦_⟧TermCache : ∀ {τ Γ} → Term Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦_⟧TermCache (const c args) ρ = extend (⟦ const c args ⟧ (map dropCache ρ)) ⟦_⟧TermCache (var x) ρ = ⟦ x ⟧VarHidCache ρ -- It seems odd (a probable bug?) that the result of t needn't be stripped of -- its cache. ⟦_⟧TermCache (app s t) ρ = proj₁ (proj₂ ((⟦ s ⟧TermCache ρ) (⟦ t ⟧TermCache ρ))) -- Provide an empty cache! ⟦_⟧TermCache (abs t) ρ x = , (⟦ t ⟧TermCache (x • ρ) , tt) open import Parametric.Syntax.MType as MType open MType.Structure Base open import Parametric.Syntax.MTerm as MTerm open MTerm.Structure Const open import Function hiding (const) ⟦_⟧ValType : (τ : ValType) → Set ⟦_⟧CompType : (τ : CompType) → Set ⟦ U c ⟧ValType = ⟦ c ⟧CompType ⟦ B ι ⟧ValType = ⟦ base ι ⟧ ⟦ vUnit ⟧ValType = ⊤ ⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType ⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType ⟦ F τ ⟧CompType = ⟦ τ ⟧ValType ⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -} -- The solution is to distinguish among different kinds of constants. Some are -- value constructors (and thus do not return caches), while others are -- computation constructors (and thus should return caches). For products, I -- believe we will only use the products which are values, not computations -- (XXX check CBPV paper for the name). ⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public using () renaming (⟦_⟧Var to ⟦_⟧ValVar) -- This says that the environment does not contain caches... sounds wrong! -- Either we add extra variables for the caches, or we store computations in -- the environment (but that does not make sense), or we store caches in -- values, by acting not on F but on something else (U?). -- I suspect the plan was to use extra variables; that's annoying to model in -- Agda but easier in implementations. ⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ ⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ -- The real deal, finally. open import UNDEFINED -- XXX constants are still a slight mess because I'm abusing CBPV... -- (Actually, I just forgot the difference, and believe I had too little clue -- when I wrote these constructors... but some of them did make sense). ⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED -- Also, where are introduction forms for pairs and sums among values? With -- them, we should see that we can interpret them without adding a cache. -- Thunks keep seeming noops. ⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ -- Here, in an actual implementation, we would return the actual cache with -- all variables. -- -- The effect of F is a writer monad of cached values, where the monoid is -- (isomorphic to) the free monoid over (∃ τ . τ), but we push the -- existentials up when pairing things! -- That's what we're interpreting computations in. XXX maybe consider using -- monads directly. But that doesn't deal with arity. ⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt) -- For this to be enough, a lambda needs to return a produce, not to forward -- the underlying one (unless there are no intermediate results). The correct -- requirements can maybe be enforced through a linear typing discipline. {- -- Here we'd have a problem with the original into from CBPV, because it does -- not require converting expressions to the "CBPV A-normal form". ⟦_⟧CompTermCache (v₁ into v₂) ρ = -- Sequence commands and combine their caches. {- let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in , (r₂ , (c₁ ,′ c₂)) -} -- The code above does not work, because we only guarantee that v₂ is -- a computation, not that it's an F-computation - v₂ could also be function -- type or a computation product. -- We need a restricted CBPV, where these two possibilities are forbidden. If -- you want to save something between different lambdas, you need to add an F -- U to reflect that. (Double-check the papers which show how to encode arity -- using CBPV, it seems that they should find the same problem --- XXX they -- don't). -- Instead, just drop the first cache (XXX WRONG). ⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ) -} -- But if we alter _into_ as described above, composing the caches works! -- However, we should not forget we also need to save the new intermediate -- result, that is the one produced by the first part of the let. ⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ = let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂)) -- Note the compositionality and luck: we don't need to do anything at the -- cReturn time, we just need the nested into to do their job, because as I -- said intermediate results are a writer monad. -- -- Q: But then, do we still need to do all the other stuff? IOW, do we still -- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <- -- f args; z <- g args'; z)? -- -- A: One thing we still need is using the monadic version of into for the -- same reasons - which makes sense, since it has the type of monadic bind. -- -- Maybe not: if we use a monad, which respects left and right identity, the -- two above forms are equivalent. But what about associativity? We don't have -- associativity with nested tuples in the middle. That's why the monad uses -- lists! We can also use nested tuple, as long as in the into case we don't -- do (a, b) but append a b (ahem, where?), which decomposes the first list -- and prepends it to the second. To this end, we need to know the type of the -- first element, or to ensure it's always a pair. Maybe we just want to reuse -- an HList. -- In abstractions, we should start collecting all variables... -- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache, -- that's moved into the handling of cReturn. This makes *the* difference for -- nested lambdas, where we don't need to create caches multiple times! ⟦_⟧CompTermCache (cAbs v) ρ x = ⟦ v ⟧CompTermCache (x • ρ) -- Here we see that we are in a sort of A-normal form, because the argument is -- a value (not quite ANF though, since values can be thunks - that is, -- computations which haven't been run yet, I guess. Do we have an use for -- that? That allows passing lambdas as arguments directly - which is fine, -- because producing a closure indeed does not have intermediate results!). ⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
Reorder code
Reorder code Separate non-CBPV and CBPV version of the code
Agda
mit
inc-lc/ilc-agda
9654b48257da5d67093ffa9653ed62f10df0bfa4
composition-sem-sec-reduction.agda
composition-sem-sec-reduction.agda
module otp-sem-sec where import Level as L open import Function open import Data.Nat.NP open import Data.Bits open import Data.Bool open import Data.Bool.Properties open import Data.Vec hiding (_>>=_) open import Data.Product.NP hiding (_⟦×⟧_) open import Relation.Nullary open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP open import flipbased-implem open ≡-Reasoning open import Data.Unit using (⊤) open import composable open import vcomp open import forkable open import flat-funs open import program-distance open import one-time-semantic-security import bit-guessing-game module BitsExtra where splitAt′ : ∀ k {n} → Bits (k + n) → Bits k × Bits n splitAt′ k xs = case splitAt k xs of λ { (ys , zs , _) → (ys , zs) } vnot∘vnot : ∀ {n} → vnot {n} ∘ vnot ≗ id vnot∘vnot [] = refl vnot∘vnot (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot xs) open BitsExtra module CompRed {t} {T : Set t} {♭Funs : FlatFuns T} (♭ops : FlatFunsOps ♭Funs) (ep ep' : EncParams) where open FlatFunsOps ♭ops open AbsSemSec ♭Funs open EncParams ep using (|M|; |C|) open EncParams ep' using () renaming (|M| to |M|'; |C| to |C|') M = `Bits |M| C = `Bits |C| M' = `Bits |M|' C' = `Bits |C|' comp-red : (pre-E : M' `→ M) (post-E : C `→ C') → SemSecReduction ep' ep _ comp-red pre-E post-E (mk A₀ A₁) = mk (A₀ >>> (pre-E *** pre-E) *** idO) (post-E *** idO >>> A₁) module Comp (ep : EncParams) (|M|' |C|' : ℕ) where open EncParams ep ep' : EncParams ep' = EncParams.mk |M|' |C|' |R|e open EncParams ep' using () renaming (Enc to Enc'; M to M'; C to C') Tr = Enc → Enc' open FlatFunsOps fun♭Ops comp : (pre-E : M' → M) (post-E : C → C') → Tr comp pre-E post-E E = pre-E >>> E >>> {-weaken≤ |R|e≤|R|e' ∘-} map↺ post-E module CompSec (prgDist : PrgDist) (ep : EncParams) (|M|' |C|' : ℕ) where open PrgDist prgDist open FlatFunsOps fun♭Ops open FunSemSec prgDist open AbsSemSec fun♭Funs open EncParams ep ep' : EncParams ep' = EncParams.mk |M|' |C|' |R|e open EncParams ep' using () renaming (Enc to Enc'; M to M'; C to C') module CompSec' (pre-E : M' → M) (post-E : C → C') where open SemSecReductions ep ep' open CompRed fun♭Ops ep ep' hiding (M; C) open Comp ep |M|' |C|' comp-pres-sem-sec : SemSecTr id (comp pre-E post-E) comp-pres-sem-sec = comp-red pre-E post-E , λ E A → id open SemSecReductions ep ep' open CompRed fun♭Ops ep ep' hiding (M; C) open CompSec' public module Comp' {x y z} = Comp x y z open Comp' hiding (Tr) comp-pres-sem-sec⁻¹ : ∀ pre-E pre-E⁻¹ (pre-E-right-inv : pre-E ∘ pre-E⁻¹ ≗ id) post-E post-E⁻¹ (post-E-left-inv : post-E⁻¹ ∘ post-E ≗ id) → SemSecTr⁻¹ id (comp pre-E post-E) comp-pres-sem-sec⁻¹ pre-E pre-E⁻¹ pre-E-inv post-E post-E⁻¹ post-E-inv = SemSecTr→SemSecTr⁻¹ (comp pre-E⁻¹ post-E⁻¹) (comp pre-E post-E) (λ E m R → trans (post-E-inv _) (cong (λ x → run↺ (E x) R) (pre-E-inv _))) (comp-pres-sem-sec pre-E⁻¹ post-E⁻¹) module PostNegSec (prgDist : PrgDist) ep where open EncParams ep open Comp ep |M| |C| hiding (Tr) open CompSec prgDist ep |M| |C| open FunSemSec prgDist open SemSecReductions ep ep post-neg : Tr post-neg = comp id vnot post-neg-pres-sem-sec : SemSecTr id post-neg post-neg-pres-sem-sec = comp-pres-sem-sec id vnot post-neg-pres-sem-sec⁻¹ : SemSecTr⁻¹ id post-neg post-neg-pres-sem-sec⁻¹ = comp-pres-sem-sec⁻¹ id id (λ _ → refl) vnot vnot vnot∘vnot module Concrete k where open program-distance.Implem k module Guess' = bit-guessing-game prgDist module FunSemSec' = FunSemSec prgDist module CompSec' = CompSec prgDist
module composition-sem-sec-reduction where import Level as L open import Function open import Data.Nat.NP open import Data.Bits open import Data.Bool open import Data.Vec hiding (_>>=_) open import Data.Product.NP using (_,_) open import Relation.Nullary open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP open import flipbased-implem open ≡-Reasoning open import Data.Unit using (⊤) open import composable open import vcomp open import forkable open import flat-funs open import program-distance open import one-time-semantic-security import bit-guessing-game -- One focuses on a particular kind of transformation -- over a cipher, namely pre and post composing functions -- to a given cipher E. -- This transformation can change the size of input/output -- of the given cipher. module Comp (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where open EncParams²-same-|R|ᵉ ep₀ |M|₁ |C|₁ open FlatFunsOps fun♭Ops comp : (pre-E : M₁ → M₀) (post-E : C₀ → C₁) → Tr comp pre-E post-E E = pre-E >>> E >>> map↺ post-E -- This module shows how to adapt an adversary that is supposed to break -- one time semantic security of some cipher E enhanced by pre and post -- composition to an adversary breaking one time semantic security of the -- original cipher E. -- -- The adversary transformation works for any notion of function space -- (FlatFuns) and the corresponding operations (FlatFunsOps). -- Because of this abstraction the following construction can be safely -- instantiated for at least three uses: -- * When provided with a concrete notion of function like circuits -- or at least functions over bit-vectors, one get a concrete circuit -- transformation process. -- * When provided with a high-level function space with real products -- then proving properties about the code is made easy. -- * When provided with a notion of cost such as time or space then -- one get a the cost of the resulting adversary given the cost of -- the inputs (adversary...). module CompRed {t} {T : Set t} {♭Funs : FlatFuns T} (♭ops : FlatFunsOps ♭Funs) (ep₀ ep₁ : EncParams) where open FlatFuns ♭Funs open FlatFunsOps ♭ops open AbsSemSec ♭Funs open ♭EncParams² ♭Funs ep₀ ep₁ using (`M₀; `C₀; `M₁; `C₁) comp-red : (pre-E : `M₁ `→ `M₀) (post-E : `C₀ `→ `C₁) → SemSecReduction ep₁ ep₀ _ comp-red pre-E post-E (mk A₀ A₁) = mk (A₀ >>> (pre-E *** pre-E) *** idO) (post-E *** idO >>> A₁) module CompSec (prgDist : PrgDist) (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where open PrgDist prgDist open FlatFunsOps fun♭Ops open FunSemSec prgDist open AbsSemSec fun♭Funs open EncParams²-same-|R|ᵉ ep₀ |M|₁ |C|₁ open SemSecReductions ep₀ ep₁ id open CompRed fun♭Ops ep₀ ep₁ private module Comp-implicit {x y z} = Comp x y z open Comp-implicit public comp-pres-sem-sec : ∀ pre-E post-E → SemSecTr (comp pre-E post-E) comp-pres-sem-sec pre-E post-E = comp-red pre-E post-E , λ E A → id comp-pres-sem-sec⁻¹ : ∀ pre-E pre-E⁻¹ (pre-E-right-inv : pre-E ∘ pre-E⁻¹ ≗ id) post-E post-E⁻¹ (post-E-left-inv : post-E⁻¹ ∘ post-E ≗ id) → SemSecTr⁻¹ (comp pre-E post-E) comp-pres-sem-sec⁻¹ pre-E pre-E⁻¹ pre-E-inv post-E post-E⁻¹ post-E-inv = SemSecTr→SemSecTr⁻¹ (comp pre-E⁻¹ post-E⁻¹) (comp pre-E post-E) (λ E m R → trans (post-E-inv _) (cong (λ x → run↺ (E x) R) (pre-E-inv _))) (comp-pres-sem-sec pre-E⁻¹ post-E⁻¹) module PostNegSec (prgDist : PrgDist) ep where open EncParams ep open EncParams² ep ep using (Tr) open CompSec prgDist ep |M| |C| open FunSemSec prgDist open SemSecReductions ep ep id post-neg : Tr post-neg = comp id vnot post-neg-pres-sem-sec : SemSecTr post-neg post-neg-pres-sem-sec = comp-pres-sem-sec id vnot post-neg-pres-sem-sec⁻¹ : SemSecTr⁻¹ post-neg post-neg-pres-sem-sec⁻¹ = comp-pres-sem-sec⁻¹ id id (λ _ → refl) vnot vnot vnot∘vnot≗id module Concrete k where open program-distance.Implem k module Guess-prgDist = bit-guessing-game prgDist module FunSemSec-prgDist = FunSemSec prgDist module CompSec-prgDist = CompSec prgDist
Rework composition-sem-sec-reduction.agda
Rework composition-sem-sec-reduction.agda
Agda
bsd-3-clause
crypto-agda/crypto-agda
be0274dd27e35d690791eb80a7b4b66635414047
Thesis/ANormalDTerm.agda
Thesis/ANormalDTerm.agda
module Thesis.ANormalDTerm where open import Thesis.ANormal {- Try defining an alternative syntax for differential terms. We'll need this for cache terms. Maybe? Options: 1. use standard untyped lambda calculus as a target language. This requires much more weakening in the syntax, which is a nuisance in proofs. Maybe use de Bruijn levels instead? 2. use a specialized syntax as a target language. 3. use separate namespaces, environments, and so on? Also, caching uses quite a bit of sugar on top of lambda calculus, with pairs and what not. -} {- -- To define an alternative syntax for the derivation output, let's remember that: derive (var x) = dvar dx derive (let y = f x in t) = let y = f x ; dy = df x dx in derive t -} -- Attempt 1 leads to function calls in datatype indexes. That's very very bad! module Try1 where -- data DTerm : (Γ : Context) (τ : Type) → Set where -- dvar : ∀ {Γ τ} (x : Var Γ τ) → -- DTerm (ΔΓ Γ) (Δt τ) -- dlett : ∀ {Γ σ τ₁ τ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) → DTerm (Δt τ₁ • τ₁ • ΔΓ Γ) (Δt τ) → DTerm (ΔΓ Γ) (Δt τ) -- derive-dterm : ∀ {Γ τ} → Term Γ τ → DTerm (ΔΓ Γ) (Δt τ) -- derive-dterm (var x) = dvar x -- derive-dterm (lett f x t) = dlett f x (derive-dterm t) -- Case split on dt FAILS! -- ⟦_⟧DTerm : ∀ {Γ τ} → DTerm (ΔΓ Γ) (Δt τ) → ⟦ ΔΓ Γ ⟧Context → ⟦ Δt τ ⟧Type -- ⟦ dt ⟧DTerm ρ = {!dt!} ΔΔ : Context → Context ΔΔ ∅ = ∅ ΔΔ (τ • Γ) = Δt τ • ΔΔ Γ Δτ = Δt ChΔ : ∀ (Δ : Context) → Set ChΔ Δ = ⟦ ΔΔ Δ ⟧Context -- changeStructureΔ : ∀ Δ → ChangeStructure ⟦ Δ ⟧Context -- changeStructureΔ = {!!} -- ch_from_to_ {{changeStructureΔ Δ}} -- [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set -- [ ∅ ]Δ ∅ from ∅ to ∅ = ⊤ -- [ τ • Δ ]Δ dv • dρ from v1 • ρ1 to (v2 • ρ2) = [ τ ]τ dv from v1 to v2 × [ Δ ]Δ dρ from ρ1 to ρ2 -- Δ is bound to the change type! Not good! data [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set where v∅ : [ ∅ ]Δ ∅ from ∅ to ∅ _v•_ : ∀ {τ Δ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ]τ dv from v1 to v2) → (dρρ : [ Δ ]Δ dρ from ρ1 to ρ2) → [ τ • Δ ]Δ (dv • dρ) from (v1 • ρ1) to (v2 • ρ2) module Try3 where derive-dvar : ∀ {Δ σ} → (x : Var Δ σ) → Var (ΔΔ Δ) (Δτ σ) derive-dvar this = this derive-dvar (that x) = that (derive-dvar x) fromtoDeriveDVar : ∀ {Δ τ} → (x : Var Δ τ) → ∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 → [ τ ]τ (⟦ derive-dvar x ⟧Var dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2) fromtoDeriveDVar this (dvv v• dρρ) = dvv fromtoDeriveDVar (that x) (dvv v• dρρ) = fromtoDeriveDVar x dρρ -- A DTerm evaluates in normal context Δ, change context (ΔΔ Δ), and produces -- a result of type (Δt τ). data DTerm : (Δ : Context) (τ : Type) → Set where dvar : ∀ {Δ τ} (x : Var (ΔΔ Δ) (Δt τ)) → DTerm Δ τ dlett : ∀ {Δ τ σ τ₁} → (f : Var Δ (σ ⇒ τ₁)) → (x : Var Δ σ) → (t : Term (τ₁ • Δ) τ) → (df : Var (ΔΔ Δ) (Δτ (σ ⇒ τ₁))) → (dx : Var (ΔΔ Δ) (Δτ σ)) → (dt : DTerm (τ₁ • Δ) τ) → DTerm Δ τ data DFun (Δ : Context) : (τ : Type) → Set where dterm : ∀ {τ} → DTerm Δ τ → DFun Δ τ dabs : ∀ {σ τ} → DFun (σ • Δ) τ → DFun Δ (σ ⇒ τ) derive-dterm : ∀ {Δ σ} → (t : Term Δ σ) → DTerm Δ σ derive-dterm (var x) = dvar (derive-dvar x) derive-dterm (lett f x t) = dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t) ⟦_⟧DTerm : ∀ {Δ τ} → DTerm Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type ⟦ dvar x ⟧DTerm ρ dρ = ⟦ x ⟧Var dρ ⟦ dlett f x t df dx dt ⟧DTerm ρ dρ = let v = (⟦ x ⟧Var ρ) in ⟦ dt ⟧DTerm (⟦ f ⟧Var ρ v • ρ) (⟦ df ⟧Var dρ v (⟦ dx ⟧Var dρ) • dρ) fromtoDeriveDTerm : ∀ {Δ τ} → (t : Term Δ τ) → ∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 → [ τ ]τ (⟦ derive-dterm t ⟧DTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2) fromtoDeriveDTerm (var x) dρρ = fromtoDeriveDVar x dρρ fromtoDeriveDTerm (lett f x t) dρρ = let fromToF = fromtoDeriveDVar f dρρ fromToX = fromtoDeriveDVar x dρρ fromToFX = fromToF _ _ _ fromToX in fromtoDeriveDTerm t (fromToFX v• dρρ) derive-dfun : ∀ {Δ σ} → (t : Fun Δ σ) → DFun Δ σ derive-dfun (term t) = dterm (derive-dterm t) derive-dfun (abs f) = dabs (derive-dfun f) ⟦_⟧DFun : ∀ {Δ τ} → DFun Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type ⟦ dterm t ⟧DFun = ⟦ t ⟧DTerm ⟦ dabs df ⟧DFun ρ dρ = λ v dv → ⟦ df ⟧DFun (v • ρ) (dv • dρ) fromtoDeriveDFun : ∀ {Δ τ} → (f : Fun Δ τ) → ∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 → [ τ ]τ (⟦ derive-dfun f ⟧DFun ρ1 dρ) from (⟦ f ⟧Fun ρ1) to (⟦ f ⟧Fun ρ2) fromtoDeriveDFun (term t) = fromtoDeriveDTerm t fromtoDeriveDFun (abs f) dρρ = λ dv v1 v2 dvv → fromtoDeriveDFun f (dvv v• dρρ) module Try2 where -- This is literally isomorphic to the source type :-| derivation is part of erasure... data DTerm : (Γ : Context) (τ : Type) → Set where dvar : ∀ {Γ τ} (x : Var Γ τ) → DTerm Γ τ -- dlett : ∀ {Γ σ τ₁ τ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) → DTerm (Δt τ₁ • τ₁ • ΔΓ Γ) (Δt τ) → DTerm (ΔΓ Γ) (Δt τ) dlett : ∀ {Γ τ σ τ₁} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) → DTerm (τ₁ • Γ) τ → DTerm Γ τ derive-dterm : ∀ {Γ τ} → Term Γ τ → DTerm Γ τ derive-dterm (var x) = dvar x derive-dterm (lett f x t) = dlett f x (derive-dterm t) -- Incremental semantics over terms. Same results as base semantics over change -- terms. But less weakening. ⟦_⟧DTerm : ∀ {Γ τ} → DTerm Γ τ → ⟦ ΔΓ Γ ⟧Context → ⟦ Δt τ ⟧Type ⟦ dvar x ⟧DTerm dρ = ⟦ derive-var x ⟧Var dρ ⟦ dlett f x dt ⟧DTerm dρ = let ρ = ⟦ Γ≼ΔΓ ⟧≼ dρ in ⟦ dt ⟧DTerm ( ⟦ derive-var f ⟧Var dρ (⟦ x ⟧Var ρ) (⟦ derive-var x ⟧Var dρ) • ⟦ f ⟧Var ρ (⟦ x ⟧Var ρ) • dρ)
module Thesis.ANormalDTerm where open import Thesis.ANormal ΔΔ : Context → Context ΔΔ ∅ = ∅ ΔΔ (τ • Γ) = Δt τ • ΔΔ Γ Δτ = Δt ChΔ : ∀ (Δ : Context) → Set ChΔ Δ = ⟦ ΔΔ Δ ⟧Context -- [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set -- [ ∅ ]Δ ∅ from ∅ to ∅ = ⊤ -- [ τ • Δ ]Δ dv • dρ from v1 • ρ1 to (v2 • ρ2) = [ τ ]τ dv from v1 to v2 × [ Δ ]Δ dρ from ρ1 to ρ2 data [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set where v∅ : [ ∅ ]Δ ∅ from ∅ to ∅ _v•_ : ∀ {τ Δ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ]τ dv from v1 to v2) → (dρρ : [ Δ ]Δ dρ from ρ1 to ρ2) → [ τ • Δ ]Δ (dv • dρ) from (v1 • ρ1) to (v2 • ρ2) derive-dvar : ∀ {Δ σ} → (x : Var Δ σ) → Var (ΔΔ Δ) (Δτ σ) derive-dvar this = this derive-dvar (that x) = that (derive-dvar x) fromtoDeriveDVar : ∀ {Δ τ} → (x : Var Δ τ) → ∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 → [ τ ]τ (⟦ derive-dvar x ⟧Var dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2) fromtoDeriveDVar this (dvv v• dρρ) = dvv fromtoDeriveDVar (that x) (dvv v• dρρ) = fromtoDeriveDVar x dρρ -- A DTerm evaluates in normal context Δ, change context (ΔΔ Δ), and produces -- a result of type (Δt τ). data DTerm : (Δ : Context) (τ : Type) → Set where dvar : ∀ {Δ τ} (x : Var (ΔΔ Δ) (Δt τ)) → DTerm Δ τ dlett : ∀ {Δ τ σ τ₁} → (f : Var Δ (σ ⇒ τ₁)) → (x : Var Δ σ) → (t : Term (τ₁ • Δ) τ) → (df : Var (ΔΔ Δ) (Δτ (σ ⇒ τ₁))) → (dx : Var (ΔΔ Δ) (Δτ σ)) → (dt : DTerm (τ₁ • Δ) τ) → DTerm Δ τ data DFun (Δ : Context) : (τ : Type) → Set where dterm : ∀ {τ} → DTerm Δ τ → DFun Δ τ dabs : ∀ {σ τ} → DFun (σ • Δ) τ → DFun Δ (σ ⇒ τ) derive-dterm : ∀ {Δ σ} → (t : Term Δ σ) → DTerm Δ σ derive-dterm (var x) = dvar (derive-dvar x) derive-dterm (lett f x t) = dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t) ⟦_⟧DTerm : ∀ {Δ τ} → DTerm Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type ⟦ dvar x ⟧DTerm ρ dρ = ⟦ x ⟧Var dρ ⟦ dlett f x t df dx dt ⟧DTerm ρ dρ = let v = (⟦ x ⟧Var ρ) in ⟦ dt ⟧DTerm (⟦ f ⟧Var ρ v • ρ) (⟦ df ⟧Var dρ v (⟦ dx ⟧Var dρ) • dρ) fromtoDeriveDTerm : ∀ {Δ τ} → (t : Term Δ τ) → ∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 → [ τ ]τ (⟦ derive-dterm t ⟧DTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2) fromtoDeriveDTerm (var x) dρρ = fromtoDeriveDVar x dρρ fromtoDeriveDTerm (lett f x t) dρρ = let fromToF = fromtoDeriveDVar f dρρ fromToX = fromtoDeriveDVar x dρρ fromToFX = fromToF _ _ _ fromToX in fromtoDeriveDTerm t (fromToFX v• dρρ) derive-dfun : ∀ {Δ σ} → (t : Fun Δ σ) → DFun Δ σ derive-dfun (term t) = dterm (derive-dterm t) derive-dfun (abs f) = dabs (derive-dfun f) ⟦_⟧DFun : ∀ {Δ τ} → DFun Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type ⟦ dterm t ⟧DFun = ⟦ t ⟧DTerm ⟦ dabs df ⟧DFun ρ dρ = λ v dv → ⟦ df ⟧DFun (v • ρ) (dv • dρ) fromtoDeriveDFun : ∀ {Δ τ} → (f : Fun Δ τ) → ∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 → [ τ ]τ (⟦ derive-dfun f ⟧DFun ρ1 dρ) from (⟦ f ⟧Fun ρ1) to (⟦ f ⟧Fun ρ2) fromtoDeriveDFun (term t) = fromtoDeriveDTerm t fromtoDeriveDFun (abs f) dρρ = λ dv v1 v2 dvv → fromtoDeriveDFun f (dvv v• dρρ)
remove bad experiments
ANormalDTerm: remove bad experiments
Agda
mit
inc-lc/ilc-agda
3ef9b9505385dfad155ad9a494a161385271f576
ZK/GroupHom/ElGamal.agda
ZK/GroupHom/ElGamal.agda
{-# OPTIONS --without-K #-} open import Type using (Type) open import Function using (flip; _∘_) open import Data.Product renaming (proj₁ to fst; proj₂ to snd) open import Data.Two open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP using (_≡_; ap; ap₂; !_; _∙_; module ≡-Reasoning) open import Algebra.Group open import Algebra.Group.Constructions open import Algebra.Group.Homomorphism import ZK.SigmaProtocol.KnownStatement import ZK.GroupHom open import ZK.GroupHom.Types module ZK.GroupHom.ElGamal (G+ G* : Type) (𝔾+ : Group G+) (𝔾* : Group G*) (_==_ : G* → G* → 𝟚) (✓-== : ∀ {x y} → x ≡ y → ✓ (x == y)) (==-✓ : ∀ {x y} → ✓ (x == y) → x ≡ y) (_^_ : G* → G+ → G*) (^-hom : ∀ b → GroupHomomorphism 𝔾+ 𝔾* (_^_ b)) (g : G*) where open Additive-Group 𝔾+ open Multiplicative-Group 𝔾* hiding (_^_) -- TODO: Re-use another module module ElGamal-encryption where record CipherText : Type where constructor _,_ field α β : G* PubKey = G* PrivKey = G+ EncRnd = G+ {- randomness used for encryption of ct -} Message = G* {- plain text message -} enc : PubKey → EncRnd → Message → CipherText enc y r M = α , β where α = g ^ r β = (y ^ r) * M dec : PrivKey → CipherText → Message dec x ct = (α ^ x)⁻¹ * β where open CipherText ct open ElGamal-encryption -- CP : (g₀ g₁ u₀ u₁ : G) (w : ℤq) → Type -- CP g₀ g₁ u₀ u₁ w = (g₀ ^ w ≡ u₀) × (g₁ ^ w ≡ u₁) -- CP : (g₀ g₁ u₀ u₁ : G*) (w : G+) → Type -- CP g₀ g₁ u₀ u₁ w = ✓ (((g₀ ^ w) == u₀) ∧ ((g₁ ^ w) == u₁)) module _ (y : PubKey) (M : Message) (ct : CipherText) where module CT = CipherText ct KnownEncRnd : EncRnd → Type KnownEncRnd r = enc y r M ≡ ct known-enc-rnd : GrpHom _ _ KnownEncRnd known-enc-rnd = record { 𝔾+ = 𝔾+ ; 𝔾* = Product.×-grp 𝔾* 𝔾* ; _==_ = λ x y → fst x == fst y ∧ snd x == snd y ; ✓-== = λ e → ✓∧ (✓-== (ap fst e)) (✓-== (ap snd e)) ; ==-✓ = λ e → let p = ✓∧× e in ap₂ _,_ (==-✓ (fst p)) (==-✓ (snd p)) ; φ = _ ; φ-hom = Pair.pair-hom _ _ _ (_^_ g) (^-hom g) (_^_ y) (^-hom y) ; y = CT.α , CT.β / M ; φ⇒P = λ _ e → ap₂ (λ p q → fst p , q) e (ap (flip _*_ M ∘ snd) e ∙ ! /-*) ; P⇒φ = λ _ e → ap₂ _,_ (ap CipherText.α e) (*-/ ∙ ap (flip _/_ M) (ap CipherText.β e)) } KnownDec : PrivKey → Type KnownDec x = (g ^ x ≡ y) × (dec x ct ≡ M) open ≡-Reasoning /⁻¹-* : ∀ {x y} → (x / y)⁻¹ * x ≡ y /⁻¹-* {x} {y} = (x / y)⁻¹ * x ≡⟨ ap (λ z → z * x) ⁻¹-hom′ ⟩ y ⁻¹ ⁻¹ * x ⁻¹ * x ≡⟨ *-assoc ⟩ y ⁻¹ ⁻¹ * (x ⁻¹ * x) ≡⟨ ap₂ _*_ ⁻¹-involutive (fst ⁻¹-inverse) ⟩ y * 1# ≡⟨ *1-identity ⟩ y ∎ /-⁻¹* : ∀ {x y} → x / (y ⁻¹ * x) ≡ y /-⁻¹* {x} {y} = x * (y ⁻¹ * x) ⁻¹ ≡⟨ ap (_*_ x) ⁻¹-hom′ ⟩ x * (x ⁻¹ * y ⁻¹ ⁻¹) ≡⟨ ! *-assoc ⟩ (x * x ⁻¹) * y ⁻¹ ⁻¹ ≡⟨ *= (snd ⁻¹-inverse) ⁻¹-involutive ⟩ 1# * y ≡⟨ 1*-identity ⟩ y ∎ known-dec : GrpHom _ _ KnownDec known-dec = record { 𝔾+ = 𝔾+ ; 𝔾* = Product.×-grp 𝔾* 𝔾* ; _==_ = λ x y → fst x == fst y ∧ snd x == snd y ; ✓-== = λ e → ✓∧ (✓-== (ap fst e)) (✓-== (ap snd e)) ; ==-✓ = λ e → let p = ✓∧× e in ap₂ _,_ (==-✓ (fst p)) (==-✓ (snd p)) ; φ = _ ; φ-hom = Pair.pair-hom _ _ _ (_^_ g) (^-hom g) (_^_ CT.α) (^-hom CT.α) ; y = y , CT.β / M ; φ⇒P = λ x e → ap fst e , ap (λ z → z ⁻¹ * CT.β) (ap snd e) ∙ /⁻¹-* ; P⇒φ = λ x e → ap₂ _,_ (fst e) (! /-⁻¹* ∙ ap (_/_ CT.β) (snd e)) } -- -} -- -} -- -}
{-# OPTIONS --without-K #-} open import Type using (Type) open import Type.Eq open import Function.NP using (flip; _∘_; it) open import Data.Product.NP open import Data.Two hiding (_²) open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP using (idp; _≡_; ap; ap₂; !_; _∙_; module ≡-Reasoning) open import Algebra.Group open import Algebra.Group.Constructions open import Algebra.Group.Homomorphism import ZK.GroupHom open import ZK.GroupHom.Types module ZK.GroupHom.ElGamal (G+ G* : Type) (𝔾+ : Group G+) (𝔾* : Group G*) (G*-eq? : Eq? G*) (_^_ : G* → G+ → G*) (^-hom : ∀ b → GroupHomomorphism 𝔾+ 𝔾* (_^_ b)) (g : G*) where module 𝔾* = Group 𝔾* open Additive-Group 𝔾+ open module MG = Multiplicative-Group 𝔾* hiding (_^_; _²) module ^ b = GroupHomomorphism (^-hom b) _² : Type → Type A ² = A × A -- TODO: Re-use another module module ElGamal-encryption where PubKey = G* PrivKey = G+ EncRnd = G+ {- randomness used for encryption of ct -} Message = G* {- plain text message -} CipherText = G* × G* module CipherText (ct : CipherText) where α β : G* α = fst ct β = snd ct pub-of : PrivKey → PubKey pub-of sk = g ^ sk enc : PubKey → Message → EncRnd → CipherText enc y M r = α , β module enc where α = g ^ r β = (y ^ r) * M dec : PrivKey → CipherText → Message dec sk (α , β) = (α ^ sk)⁻¹ * β open ElGamal-encryption open Product _²-grp : {A : Type} → Group A → Group (A ²) 𝔸 ²-grp = ×-grp 𝔸 𝔸 𝕄 : Group Message 𝕄 = 𝔾* ℂ𝕋 : Group CipherText ℂ𝕋 = ×-grp 𝔾* 𝔾* -- CP : (g₀ g₁ u₀ u₁ : G) (w : ℤq) → Type -- CP g₀ g₁ u₀ u₁ w = (g₀ ^ w ≡ u₀) × (g₁ ^ w ≡ u₁) -- CP : (g₀ g₁ u₀ u₁ : G*) (w : G+) → Type -- CP g₀ g₁ u₀ u₁ w = ✓ (((g₀ ^ w) == u₀) ∧ ((g₁ ^ w) == u₁)) module Known-enc-rnd (y : PubKey) (M : Message) (ct : CipherText) where module ct = CipherText ct Valid-witness : EncRnd → Type Valid-witness r = enc y M r ≡ ct zk-hom : ZK-hom _ _ Valid-witness zk-hom = record { φ-hom = < ^-hom g , ^-hom y >-hom ; y = ct.α , ct.β / M ; φ⇒P = λ _ e → ap₂ (λ p q → fst p , q) e (ap (flip _*_ M ∘ snd) e ∙ ! /-*) ; P⇒φ = λ _ e → ap₂ _,_ (ap fst e) (*-/ ∙ ap (flip _/_ M) (ap snd e)) } open ≡-Reasoning /⁻¹-* : ∀ {x y} → (x / y)⁻¹ * x ≡ y /⁻¹-* {x} {y} = (x / y)⁻¹ * x ≡⟨ ap (λ z → z * x) ⁻¹-hom′ ⟩ y ⁻¹ ⁻¹ * x ⁻¹ * x ≡⟨ *-assoc ⟩ y ⁻¹ ⁻¹ * (x ⁻¹ * x) ≡⟨ ap₂ _*_ ⁻¹-involutive (fst ⁻¹-inverse) ⟩ y * 1# ≡⟨ *1-identity ⟩ y ∎ /-⁻¹* : ∀ {x y} → x / (y ⁻¹ * x) ≡ y /-⁻¹* {x} {y} = x * (y ⁻¹ * x) ⁻¹ ≡⟨ ap (_*_ x) ⁻¹-hom′ ⟩ x * (x ⁻¹ * y ⁻¹ ⁻¹) ≡⟨ ! *-assoc ⟩ (x * x ⁻¹) * y ⁻¹ ⁻¹ ≡⟨ *= (snd ⁻¹-inverse) ⁻¹-involutive ⟩ 1# * y ≡⟨ 1*-identity ⟩ y ∎ x⁻¹y≡1→x≡y : ∀ {x y} → x ⁻¹ * y ≡ 1# → x ≡ y x⁻¹y≡1→x≡y e = cancels-*-left (fst ⁻¹-inverse ∙ ! e) module Known-dec (y : PubKey) (M : Message) (ct : CipherText) where module ct = CipherText ct Valid-witness : PrivKey → Type Valid-witness sk = (g ^ sk ≡ y) × (dec sk ct ≡ M) zk-hom : ZK-hom _ _ Valid-witness zk-hom = record { φ-hom = < ^-hom g , ^-hom ct.α >-hom ; y = y , ct.β / M ; φ⇒P = λ x e → ap fst e , ap (λ z → z ⁻¹ * ct.β) (ap snd e) ∙ /⁻¹-* ; P⇒φ = λ x e → ap₂ _,_ (fst e) (! /-⁻¹* ∙ ap (_/_ ct.β) (snd e)) } -- Inverse of ciphertexts _⁻¹CT : CipherText → CipherText (α , β)⁻¹CT = α ⁻¹ , β ⁻¹ -- Division of ciphertexts _/CT_ : CipherText → CipherText → CipherText (α₀ , β₀) /CT (α₁ , β₁) = (α₀ / α₁) , (β₀ / β₁) -- TODO: Move this elsewhere (Cipher.ElGamal.Homomorphic) import Algebra.FunctionProperties.Eq as FP open FP.Implicits open import Algebra.Group.Abelian module From-*-comm (*-comm : Commutative _*_) where private module 𝔾*-comm = Abelian-Group-Struct (𝔾*.grp-struct , *-comm) hom-enc : (y : PubKey) → GroupHomomorphism (×-grp 𝕄 𝔾+) ℂ𝕋 (uncurry (enc y)) hom-enc y = mk λ { {M₀ , r₀} {M₁ , r₁} → ap₂ _,_ (^.hom _) (enc.β y (M₀ * M₁) (r₀ + r₁) ≡⟨by-definition⟩ y ^ (r₀ + r₁) * (M₀ * M₁) ≡⟨ *= (^.hom y) idp ⟩ (y ^ r₀ * y ^ r₁) * (M₀ * M₁) ≡⟨ 𝔾*-comm.interchange ⟩ enc.β y M₀ r₀ * enc.β y M₁ r₁ ∎) } module hom-enc y = GroupHomomorphism (hom-enc y) -- The encryption of the inverse is the inverse of the encryption -- (notice that the randomnesses seems to be negated only because we give an additive notation to our G+ group) enc-⁻¹ : ∀ {y M r} → enc y (M ⁻¹) (0− r) ≡ enc y M r ⁻¹CT enc-⁻¹ = hom-enc.pres-inv _ -- The encryption of the division is the division of the encryptions -- (notice that the randomnesses seems to be subtracted only because we give an additive notation to our G+ group) enc-/ : ∀ {y M₀ r₀ M₁ r₁} → enc y (M₀ / M₁) (r₀ − r₁) ≡ enc y M₀ r₀ /CT enc y M₁ r₁ enc-/ = hom-enc.−-/ _ -- Alice wants to prove to the public that she encrypted the -- same message for two (potentially different) recepients -- without revealing the content of the message or the randomness -- used for the encryptions. module Message-equality-enc (y₀ y₁ : PubKey) (ct₀ ct₁ : CipherText) where module ct₀ = CipherText ct₀ module ct₁ = CipherText ct₁ Witness = Message × EncRnd ² Statement = CipherText ² Valid-witness : Witness → Type Valid-witness (M , r₀ , r₁) = enc y₀ M r₀ ≡ ct₀ × enc y₁ M r₁ ≡ ct₁ private θ : Message × EncRnd ² → (Message × EncRnd)² θ (M , r₀ , r₁) = ((M , r₀) , (M , r₁)) θ-hom : GroupHomomorphism (×-grp 𝕄 (𝔾+ ²-grp)) ((×-grp 𝕄 𝔾+)²-grp) θ θ-hom = mk idp φ-hom : GroupHomomorphism _ _ _ φ-hom = < hom-enc y₀ × hom-enc y₁ >-hom ∘-hom θ-hom zk-hom : ZK-hom _ _ Valid-witness zk-hom = record { φ-hom = φ-hom ; y = ct₀ , ct₁ ; φ⇒P = λ _ e → ap fst e , ap snd e ; P⇒φ = λ x e → ap₂ _,_ (fst e) (snd e) } module From-flip-^-hom (flip-^-hom : ∀ x → GroupHomomorphism 𝔾* 𝔾* (flip _^_ x)) where module flip-^ x = GroupHomomorphism (flip-^-hom x) hom-dec : (x : PrivKey) → GroupHomomorphism ℂ𝕋 𝕄 (dec x) hom-dec x = mk λ { {α₀ , β₀} {α₁ , β₁} → dec x (α₀ * α₁ , β₀ * β₁) ≡⟨by-definition⟩ ((α₀ * α₁) ^ x)⁻¹ * (β₀ * β₁) ≡⟨ ap (λ z → _*_ (_⁻¹ z) (_*_ β₀ β₁)) (flip-^.hom x) ⟩ (α₀ ^ x * α₁ ^ x)⁻¹ * (β₀ * β₁) ≡⟨ *= 𝔾*-comm.⁻¹-hom idp ⟩ (α₀ ^ x)⁻¹ * (α₁ ^ x)⁻¹ * (β₀ * β₁) ≡⟨ 𝔾*-comm.interchange ⟩ dec x (α₀ , β₀) * dec x (α₁ , β₁) ∎ } module hom-dec x = GroupHomomorphism (hom-dec x) -- Bob wants to prove to the public that he decrypted two -- ciphertexts which decrypt to the same message, -- without revealing the content of the message or his -- secret key. -- The two ciphertexts are encrypted using the same public key. module Message-equality-dec (y : PubKey) (ct₀ ct₁ : CipherText) where private module ct₀ = CipherText ct₀ module ct₁ = CipherText ct₁ α/ = ct₀.α / ct₁.α β/ = ct₀.β / ct₁.β Valid-witness : PrivKey → Type Valid-witness sk = pub-of sk ≡ y × dec sk ct₀ ≡ dec sk ct₁ zk-hom : ZK-hom _ _ Valid-witness zk-hom = record { φ-hom = < ^-hom g , ^-hom (ct₀.α / ct₁.α) >-hom ; y = y , ct₀.β / ct₁.β ; φ⇒P = λ x e → ap fst e , x/y≡1→x≡y (dec x ct₀ / dec x ct₁ ≡⟨ ! hom-dec.−-/ x ⟩ dec x (ct₀ /CT ct₁) ≡⟨by-definition⟩ dec x (α/ , β/) ≡⟨ ! ap (λ z → dec x (α/ , snd z)) e ⟩ dec x (α/ , (α/)^ x) ≡⟨ fst ⁻¹-inverse ⟩ 1# ∎) ; P⇒φ = λ x e → ap₂ _,_ (fst e) (x⁻¹y≡1→x≡y (dec x (α/ , β/) ≡⟨ hom-dec.−-/ x ⟩ dec x ct₀ / dec x ct₁ ≡⟨ /= (snd e) idp ⟩ dec x ct₁ / dec x ct₁ ≡⟨ snd ⁻¹-inverse ⟩ 1# ∎)) } -- -} -- -} -- -}
add proofs of equality of message (two styles)
ZK.GroupHom.ElGamal: add proofs of equality of message (two styles)
Agda
bsd-3-clause
crypto-agda/crypto-agda
ccb724c55f5701acf132f0a3045f0a1c2d116f96
Denotational/ValidChanges.agda
Denotational/ValidChanges.agda
module Denotational.ValidChanges where open import Data.Product open import Data.Unit open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Denotational.Notation open import Denotational.Values open import Denotational.Equivalence open import Changes -- DEFINITION of valid changes via a logical relation {- What I wanted to write: data ValidΔ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) → (∀ (s : ⟦ S ⟧) ds (valid : ValidΔ s ds) → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) → ValidΔ f df -} -- What I had to write: -- Note: now I could go back to using a datatype, since the datatype is now strictly positive. Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set Valid-Δ {bool} v dv = ⊤ Valid-Δ {S ⇒ T} f df = ∀ (s : ⟦ S ⟧) ds {- (valid-w : Valid-Δ s ds) -} → Valid-Δ (f s) (df s ds) × (apply df f) (apply ds s) ≡ apply (df s ds) (f s) diff-is-valid : ∀ {τ} (v′ v : ⟦ τ ⟧) → Valid-Δ {τ} v (diff v′ v) diff-is-valid {bool} v′ v = tt diff-is-valid {τ ⇒ τ₁} v′ v = λ s ds → diff-is-valid (v′ (apply ds s)) (v s) , ( begin apply (diff v′ v) v (apply ds s) ≡⟨ refl ⟩ apply (diff (v′ (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s))) (v (apply ds s)) ≡⟨ ≡-cong (λ x → apply (diff (v′ x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) ⟩ apply (diff (v′ (apply ds s)) (v (apply ds s))) (v (apply ds s)) ≡⟨ apply-diff (v (apply ds s)) (v′ (apply ds s)) ⟩ v′ (apply ds s) ≡⟨ sym (apply-diff (v s) (v′ (apply ds s))) ⟩ apply ((diff v′ v) s ds) (v s) ∎) where open ≡-Reasoning derive-is-valid : ∀ {τ} (v : ⟦ τ ⟧) → Valid-Δ {τ} v (derive v) derive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v -- This is a postulate elsewhere, but here I provide a proper proof. diff-apply-proof : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) → (Valid-Δ v dv) → diff (apply dv v) v ≡ dv diff-apply-proof {τ₁ ⇒ τ₂} df f df-valid = ext (λ v → ext (λ dv → begin diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v) ≡⟨ ≡-cong (λ x → diff x (f v)) (sym (proj₂ (df-valid (apply dv v) (derive (apply dv v))))) ⟩ diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v) ≡⟨ ≡-cong (λ x → diff (apply df f x) (f v)) (apply-derive (apply dv v)) ⟩ diff ((apply df f) (apply dv v)) (f v) ≡⟨ ≡-cong (λ x → diff x (f v)) (proj₂ (df-valid v dv)) ⟩ diff (apply (df v dv) (f v)) (f v) ≡⟨ diff-apply-proof {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv)) ⟩ df v dv ∎)) where open ≡-Reasoning diff-apply-proof {bool} db b _ = xor-cancellative b db
module Denotational.ValidChanges where open import Data.Product open import Data.Unit open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Denotational.Notation open import Denotational.Values open import Denotational.Equivalence open import Changes -- DEFINITION of valid changes via a logical relation {- What I wanted to write: data Valid-Δ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) → (∀ (s : ⟦ S ⟧) ds → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) → ValidΔ f df -} -- What I had to write: -- Note: now I could go back to using a datatype, since the datatype is now strictly positive. Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set Valid-Δ {bool} v dv = ⊤ Valid-Δ {S ⇒ T} f df = ∀ (s : ⟦ S ⟧) ds {- (valid-w : Valid-Δ s ds) -} → Valid-Δ (f s) (df s ds) × (apply df f) (apply ds s) ≡ apply (df s ds) (f s) diff-is-valid : ∀ {τ} (v′ v : ⟦ τ ⟧) → Valid-Δ {τ} v (diff v′ v) diff-is-valid {bool} v′ v = tt diff-is-valid {τ ⇒ τ₁} v′ v = λ s ds → diff-is-valid (v′ (apply ds s)) (v s) , ( begin apply (diff v′ v) v (apply ds s) ≡⟨ refl ⟩ apply (diff (v′ (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s))) (v (apply ds s)) ≡⟨ ≡-cong (λ x → apply (diff (v′ x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) ⟩ apply (diff (v′ (apply ds s)) (v (apply ds s))) (v (apply ds s)) ≡⟨ apply-diff (v (apply ds s)) (v′ (apply ds s)) ⟩ v′ (apply ds s) ≡⟨ sym (apply-diff (v s) (v′ (apply ds s))) ⟩ apply ((diff v′ v) s ds) (v s) ∎) where open ≡-Reasoning derive-is-valid : ∀ {τ} (v : ⟦ τ ⟧) → Valid-Δ {τ} v (derive v) derive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v -- This is a postulate elsewhere, but here I provide a proper proof. diff-apply-proof : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) → (Valid-Δ v dv) → diff (apply dv v) v ≡ dv diff-apply-proof {τ₁ ⇒ τ₂} df f df-valid = ext (λ v → ext (λ dv → begin diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v) ≡⟨ ≡-cong (λ x → diff x (f v)) (sym (proj₂ (df-valid (apply dv v) (derive (apply dv v))))) ⟩ diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v) ≡⟨ ≡-cong (λ x → diff (apply df f x) (f v)) (apply-derive (apply dv v)) ⟩ diff ((apply df f) (apply dv v)) (f v) ≡⟨ ≡-cong (λ x → diff x (f v)) (proj₂ (df-valid v dv)) ⟩ diff (apply (df v dv) (f v)) (f v) ≡⟨ diff-apply-proof {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv)) ⟩ df v dv ∎)) where open ≡-Reasoning diff-apply-proof {bool} db b _ = xor-cancellative b db
Update alternative definition of Valid-Δ
agda: Update alternative definition of Valid-Δ Rationale for maintaining this alternative definition: If all proofs go through with the current definition of Valid-Δ, which can be expressed as a datatype, we'll switch to defining it with a datatype. Old-commit-hash: a21df87af1c12f4d94d3710d37e6e4eedfcc83b0
Agda
mit
inc-lc/ilc-agda
884a5f0cedea9599476b8420c36f53c0676e8551
arrow.agda
arrow.agda
data Bool : Set where true : Bool false : Bool -------------------- data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false -------------------- data List (A : Set) : Set where ∘ : List A _∷_ : A → List A → List A _∈_ : ℕ → List ℕ → Bool x ∈ ∘ = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys -------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow modusponens : ℕ → Arrow → Arrow modusponens n (⇒ q) = (⇒ q) modusponens n (p ⇒ q) with (n ≡ p) ... | true = modusponens n q ... | false = p ⇒ (modusponens n q) reduce : ℕ → List Arrow → List Arrow reduce n ∘ = ∘ reduce n (arr ∷ rst) = (modusponens n arr) ∷ (reduce n rst) search : List Arrow → List ℕ search ∘ = ∘ search ((⇒ n) ∷ rst) = n ∷ (search (reduce n rst)) search ((n ⇒ q) ∷ rst) with (n ∈ (search rst)) ... | true = search (q ∷ rst) ... | false = search rst
data Bool : Set where true : Bool false : Bool -------------------- data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false -------------------- data List (A : Set) : Set where ∘ : List A _∷_ : A → List A → List A _∈_ : ℕ → List ℕ → Bool x ∈ ∘ = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys -------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow modusponens : ℕ → Arrow → Arrow modusponens n (⇒ q) = (⇒ q) modusponens n (p ⇒ q) with (n ≡ p) ... | true = modusponens n q ... | false = p ⇒ (modusponens n q) reduce : ℕ → List Arrow → List Arrow reduce n ∘ = ∘ reduce n (arr ∷ rst) = (modusponens n arr) ∷ (reduce n rst) search : List Arrow → List ℕ search ∘ = ∘ search ((⇒ n) ∷ rst) = n ∷ (search (reduce n rst)) search ((n ⇒ q) ∷ rst) with (n ∈ (search rst)) ... | true = search (q ∷ rst) ... | false = search rst _⊢_ : List Arrow → Arrow → Bool cs ⊢ (⇒ q) = q ∈ (search cs) cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
Add turnstile
Add turnstile
Agda
mit
louisswarren/hieretikz
b5aa26aa273178564c9e3df4846793f1e96e2e81
Parametric/Change/Validity.agda
Parametric/Change/Validity.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e) ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value open import Base.Change.Algebra as CA using (ChangeAlgebraFamily) module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Level -- Extension Point: Change algebras for base types Structure : Set₁ Structure = ChangeAlgebraFamily ⟦_⟧Base module Structure {{change-algebra-base : Structure}} where -- change algebras open CA public renaming -- Constructors for All′ ( [] to ∅ ; _∷_ to _•_ ) -- We provide: change algebra for every type instance change-algebra : ∀ τ → ChangeAlgebra ⟦ τ ⟧Type change-algebra (base ι) = change-algebra₍ ι ₎ change-algebra (τ₁ ⇒ τ₂) = changeAlgebraFun {{change-algebra τ₁}} {{change-algebra τ₂}} change-algebra-family : ChangeAlgebraFamily ⟦_⟧Type change-algebra-family = family change-algebra -- function changes module _ {τ₁ τ₂ : Type} where open FunctionChange {{change-algebra τ₁}} {{change-algebra τ₂}} public renaming ( correct to is-valid ; apply to call-change ) -- We also provide: change environments (aka. environment changes). open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming ( changeAlgebraListChanges to environment-changes ) after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧ after-env {Γ} = after₍_₎ Γ
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e) ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value open import Base.Change.Algebra as CA using (ChangeAlgebraFamily) module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Level -- Extension Point: Change algebras for base types Structure : Set₁ Structure = ChangeAlgebraFamily ⟦_⟧Base module Structure {{change-algebra-base : Structure}} where -- change algebras open CA public renaming -- Constructors for All′ ( [] to ∅ ; _∷_ to _•_ ) change-algebra : ∀ τ → ChangeAlgebra ⟦ τ ⟧Type change-algebra (base ι) = change-algebra₍ ι ₎ change-algebra (τ₁ ⇒ τ₂) = changeAlgebraFun {{change-algebra τ₁}} {{change-algebra τ₂}} -- We provide: change algebra for every type instance change-algebra-family : ChangeAlgebraFamily ⟦_⟧Type change-algebra-family = family change-algebra -- function changes module _ {τ₁ τ₂ : Type} where open FunctionChange {{change-algebra τ₁}} {{change-algebra τ₂}} public renaming ( correct to is-valid ; apply to call-change ) -- We also provide: change environments (aka. environment changes). open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming ( changeAlgebraListChanges to environment-changes ) after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧ after-env {Γ} = after₍_₎ Γ
Disable unused instance
Disable unused instance Agda will not infer explicit arguments of instance arguments. On the other hand, trying to make this into a proper instance (with an implicit argument) makes typechecking too much slower or not terminating (more precisely, on some modules typechecking took longer than I wanted to wait and I didn't investigate further).
Agda
mit
inc-lc/ilc-agda
e41ee7bc6a42348d7050206b8746f460ef632c69
Parametric/Change/Validity.agda
Parametric/Change/Validity.agda
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base -- Changes for Calculus Popl14 -- -- Contents -- - Mutually recursive concepts: ΔVal, validity. -- Under module Syntax, the corresponding concepts of -- ΔType and ΔContext reside in separate files. -- Now they have to be together due to mutual recursiveness. -- - `diff` and `apply` on semantic values of changes: -- they have to be here as well because they are mutually -- recursive with validity. -- - The lemma diff-is-valid: it has to be here because it is -- mutually recursive with `apply` -- - The lemma apply-diff: it is mutually recursive with `apply` -- and `diff` open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality open import Data.Product hiding (map) import Structure.Tuples as Tuples open Tuples import Base.Data.DependentList as DependentList open DependentList open import Relation.Unary using (_⊆_) record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field Change-base : Base → Set valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v) v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u --------------- -- Interface -- --------------- Change : Type → Set valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ infixl 6 apply-change diff-change -- as with + - in GHC.Num syntax apply-change τ v dv = v ⊞₍ τ ₎ dv syntax diff-change τ u v = u ⊟₍ τ ₎ v -- Lemma diff-is-valid R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v) -- Lemma apply-diff v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u -------------------- -- Implementation -- -------------------- -- (Change τ) is the set of changes of type τ. This set is -- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In -- particular, (Change (σ ⇒ τ)) is a function that accepts only -- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid -- changes. -- -- Change τ is the target of the denotational specification ⟦_⟧Δ. -- Detailed motivation: -- -- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex -- Change : Type → Set Change (base ι) = Change-base ι Change (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : Change σ) → valid v dv → Change τ -- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ n ⊞₍ base ι ₎ Δn = apply-change-base ι n Δn f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ}) -- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ m ⊟₍ base ι ₎ n = diff-change-base ι m n g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v -- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set valid {base ι} n Δn = valid-base {ι} n Δn valid {σ ⇒ τ} f Δf = ∀ (v : ⟦ σ ⟧) (Δv : Change σ) (R[v,Δv] : valid v Δv) → valid {τ} (f v) (Δf v Δv R[v,Δv]) -- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv] × (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv] -- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v} v+[u-v]=u {σ ⇒ τ} {u} {v} = ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w → begin (v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w ≡⟨ refl ⟩ v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w) ≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩ v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w) ≡⟨ v+[u-v]=u {τ} ⟩ u w ∎) where open ≡-Reasoning R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v} R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] → let w′ = w ⊞₍ σ ₎ Δw in R[v,u-v] {τ} , (begin (v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′ ≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩ u w′ ≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩ v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw] ∎) where open ≡-Reasoning -- syntactic sugar for implicit indices infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ _⊞_ {τ} v dv = v ⊞₍ τ ₎ dv _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ _⊟_ {τ} u v = u ⊟₍ τ ₎ v ------------------ -- Environments -- ------------------ open DependentList public using (∅; _•_) open Tuples public using (cons) ValidChange : Type → Set ValidChange τ = Triple ⟦ τ ⟧ (λ _ → Change τ) (λ v dv → valid {τ} v dv) ΔEnv : Context → Set ΔEnv = DependentList ValidChange ignore-valid-change : ValidChange ⊆ ⟦_⟧ ignore-valid-change (cons v _ _) = v update-valid-change : ValidChange ⊆ ⟦_⟧ update-valid-change {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ ignore = map (λ {τ} → ignore-valid-change {τ}) update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ update = map (λ {τ} → update-valid-change {τ})
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base -- Changes for Calculus Popl14 -- -- Contents -- - Mutually recursive concepts: ΔVal, validity. -- Under module Syntax, the corresponding concepts of -- ΔType and ΔContext reside in separate files. -- Now they have to be together due to mutual recursiveness. -- - `diff` and `apply` on semantic values of changes: -- they have to be here as well because they are mutually -- recursive with validity. -- - The lemma diff-is-valid: it has to be here because it is -- mutually recursive with `apply` -- - The lemma apply-diff: it is mutually recursive with `apply` -- and `diff` open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality open import Data.Product hiding (map) import Structure.Tuples as Tuples open Tuples import Base.Data.DependentList as DependentList open DependentList open import Relation.Unary using (_⊆_) record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field Change-base : Base → Set valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v) v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u --------------- -- Interface -- --------------- Change : Type → Set valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ infixl 6 apply-change diff-change -- as with + - in GHC.Num syntax apply-change τ v dv = v ⊞₍ τ ₎ dv syntax diff-change τ u v = u ⊟₍ τ ₎ v -- Lemma diff-is-valid R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v) -- Lemma apply-diff v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u -------------------- -- Implementation -- -------------------- -- (Change τ) is the set of changes of type τ. This set is -- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In -- particular, (Change (σ ⇒ τ)) is a function that accepts only -- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid -- changes. -- -- Change τ is the target of the denotational specification ⟦_⟧Δ. -- Detailed motivation: -- -- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex -- Change : Type → Set ValidChange : Type → Set ValidChange τ = Triple ⟦ τ ⟧ (λ _ → Change τ) (λ v dv → valid {τ} v dv) Change (base ι) = Change-base ι Change (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : Change σ) → valid v dv → Change τ -- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ n ⊞₍ base ι ₎ Δn = apply-change-base ι n Δn f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ}) -- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ m ⊟₍ base ι ₎ n = diff-change-base ι m n g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v -- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set valid {base ι} n Δn = valid-base {ι} n Δn valid {σ ⇒ τ} f Δf = ∀ (v : ⟦ σ ⟧) (Δv : Change σ) (R[v,Δv] : valid v Δv) → valid {τ} (f v) (Δf v Δv R[v,Δv]) -- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv] × (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv] -- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v} v+[u-v]=u {σ ⇒ τ} {u} {v} = ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w → begin (v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w ≡⟨ refl ⟩ v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w) ≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩ v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w) ≡⟨ v+[u-v]=u {τ} ⟩ u w ∎) where open ≡-Reasoning R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v} R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] → let w′ = w ⊞₍ σ ₎ Δw in R[v,u-v] {τ} , (begin (v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′ ≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩ u w′ ≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩ v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw] ∎) where open ≡-Reasoning -- syntactic sugar for implicit indices infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ _⊞_ {τ} v dv = v ⊞₍ τ ₎ dv _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ _⊟_ {τ} u v = u ⊟₍ τ ₎ v ------------------ -- Environments -- ------------------ open DependentList public using (∅; _•_) open Tuples public using (cons) ΔEnv : Context → Set ΔEnv = DependentList ValidChange ignore-valid-change : ValidChange ⊆ ⟦_⟧ ignore-valid-change (cons v _ _) = v update-valid-change : ValidChange ⊆ ⟦_⟧ update-valid-change {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ ignore = map (λ {τ} → ignore-valid-change {τ}) update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ update = map (λ {τ} → update-valid-change {τ})
Move ValidChange next to Change.
Move ValidChange next to Change. This commit prepares for using ValidChange in the definition of Change. Old-commit-hash: a90d85385de56e35cc3aba5821ddc4a2d3448247
Agda
mit
inc-lc/ilc-agda
6156984d33b98f39610ee7244b167632999b9b41
Parametric/Change/Correctness.agda
Parametric/Change/Correctness.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity import Parametric.Change.Specification as Specification import Parametric.Change.Type as ChangeType import Parametric.Change.Term as ChangeTerm import Parametric.Change.Value as ChangeValue import Parametric.Change.Evaluation as ChangeEvaluation import Parametric.Change.Derive as Derive import Parametric.Change.Implementation as Implementation module Parametric.Change.Correctness {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (diff-base : ChangeTerm.DiffStructure Const ΔBase) (apply-base : ChangeTerm.ApplyStructure Const ΔBase) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) (meaning-⊕-base : ChangeEvaluation.ApplyStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (meaning-⊝-base : ChangeEvaluation.DiffStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (validity-structure : Validity.Structure ⟦_⟧Base) (specification-structure : Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure) (derive-const : Derive.Structure Const ΔBase) (implementation-structure : Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure open ChangeType.Structure Base ΔBase open ChangeTerm.Structure Const ΔBase diff-base apply-base open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open ChangeEvaluation.Structure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧ meaning-⊕-base meaning-⊝-base open Derive.Structure Const ΔBase derive-const open Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure -- The denotational properties of the `derive` transformation. -- In particular, the main theorem about it producing the correct -- incremental behavior. open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality Structure : Set Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → (ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) → ⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′) module Structure (derive-const-correct : Structure) where deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′) deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′ deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′ -- That `derive t` implements ⟦ t ⟧Δ derive-correct : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′) derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′)) derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅ derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ = derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′ derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ = derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′ (derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′) derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ = deriveVar-correct x ρ dρ ρ′ dρ≈ρ′ derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′ = subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′) (derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′ (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′)) derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ = λ w dw w′ dw≈w′ → derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′) derive-correct-closed : ∀ {τ} (t : Term ∅ τ) → ⟦ t ⟧Δ ∅ ∅ ≈₍ τ ₎ ⟦ derive t ⟧ ∅ derive-correct-closed t = derive-correct t ∅ ∅ ∅ ∅ main-theorem : ∀ {σ τ} {f : Term ∅ (σ ⇒ τ)} {s : Term ∅ σ} {ds : Term ∅ (ΔType σ)} → {dv : Δ₍ σ ₎ (⟦ s ⟧ ∅)} {erasure : dv ≈₍ σ ₎ (⟦ ds ⟧ ∅)} → ⟦ app f (s ⊕₍ σ ₎ ds) ⟧ ≡ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ main-theorem {σ} {τ} {f} {s} {ds} {dv} {erasure} = let g = ⟦ f ⟧ ∅ Δg = ⟦ f ⟧Δ ∅ ∅ Δg′ = ⟦ derive f ⟧ ∅ v = ⟦ s ⟧ ∅ dv′ = ⟦ ds ⟧ ∅ u = ⟦ s ⊕₍ σ ₎ ds ⟧ ∅ -- Δoutput-term = app (app (derive f) x) (y ⊝ x) in ext {A = ⟦ ∅ ⟧Context} (λ { ∅ → begin g u ≡⟨ cong g (sym (meaning-⊕ {t = s} {Δt = ds})) ⟩ g (v ⟦⊕₍ σ ₎⟧ dv′) ≡⟨ cong g (sym (carry-over {σ} dv erasure)) ⟩ g (v ⊞₍ σ ₎ dv) ≡⟨ corollary-closed {σ} {τ} f v dv ⟩ g v ⊞₍ τ ₎ call-change {σ} {τ} Δg v dv ≡⟨ carry-over {τ} (call-change {σ} {τ} Δg v dv) (derive-correct f ∅ ∅ ∅ ∅ v dv dv′ erasure) ⟩ g v ⟦⊕₍ τ ₎⟧ Δg′ v dv′ ≡⟨ meaning-⊕ {t = app f s} {Δt = app (app (derive f) s) ds} ⟩ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ ∅ ∎}) where open ≡-Reasoning
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity import Parametric.Change.Specification as Specification import Parametric.Change.Type as ChangeType import Parametric.Change.Term as ChangeTerm import Parametric.Change.Value as ChangeValue import Parametric.Change.Evaluation as ChangeEvaluation import Parametric.Change.Derive as Derive import Parametric.Change.Implementation as Implementation module Parametric.Change.Correctness {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (diff-base : ChangeTerm.DiffStructure Const ΔBase) (apply-base : ChangeTerm.ApplyStructure Const ΔBase) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) (meaning-⊕-base : ChangeEvaluation.ApplyStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (meaning-⊝-base : ChangeEvaluation.DiffStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (validity-structure : Validity.Structure ⟦_⟧Base) (specification-structure : Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure) (derive-const : Derive.Structure Const ΔBase) (implementation-structure : Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure open ChangeType.Structure Base ΔBase open ChangeTerm.Structure Const ΔBase diff-base apply-base open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open ChangeEvaluation.Structure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧ meaning-⊕-base meaning-⊝-base open Derive.Structure Const ΔBase derive-const open Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure -- The denotational properties of the `derive` transformation. -- In particular, the main theorem about it producing the correct -- incremental behavior. open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality Structure : Set Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → (ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) → ⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′) module Structure (derive-const-correct : Structure) where deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′) deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′ deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′ -- That `derive t` implements ⟦ t ⟧Δ derive-correct : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′) derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′)) derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅ derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ = derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′ derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ = derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′ (derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′) derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ = deriveVar-correct x ρ dρ ρ′ dρ≈ρ′ derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′ = subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′) (derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′ (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′)) derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ = λ w dw w′ dw≈w′ → derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′) derive-correct-closed : ∀ {τ} (t : Term ∅ τ) → ⟦ t ⟧Δ ∅ ∅ ≈₍ τ ₎ ⟦ derive t ⟧ ∅ derive-correct-closed t = derive-correct t ∅ ∅ ∅ ∅ main-theorem : ∀ {σ τ} {f : Term ∅ (σ ⇒ τ)} {s : Term ∅ σ} {ds : Term ∅ (ΔType σ)} → {dv : Δ₍ σ ₎ (⟦ s ⟧ ∅)} {erasure : dv ≈₍ σ ₎ (⟦ ds ⟧ ∅)} → ⟦ app f (s ⊕₍ σ ₎ ds) ⟧ ≡ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ main-theorem {σ} {τ} {f} {s} {ds} {dv} {erasure} = let g = ⟦ f ⟧ ∅ Δg = ⟦ f ⟧Δ ∅ ∅ Δg′ = ⟦ derive f ⟧ ∅ v = ⟦ s ⟧ ∅ dv′ = ⟦ ds ⟧ ∅ u = ⟦ s ⊕₍ σ ₎ ds ⟧ ∅ -- Δoutput-term = app (app (derive f) x) (y ⊝ x) in ext {A = ⟦ ∅ ⟧Context} (λ { ∅ → begin g u ≡⟨ cong g (sym (meaning-⊕ {t = s} {Δt = ds})) ⟩ g (v ⟦⊕₍ σ ₎⟧ dv′) ≡⟨ cong g (sym (carry-over {σ} dv erasure)) ⟩ g (v ⊞₍ σ ₎ dv) ≡⟨ corollary-closed {σ} {τ} f v dv ⟩ g v ⊞₍ τ ₎ call-change {σ} {τ} Δg v dv ≡⟨ carry-over {τ} (call-change {σ} {τ} Δg v dv) (derive-correct-closed f v dv dv′ erasure) ⟩ g v ⟦⊕₍ τ ₎⟧ Δg′ v dv′ ≡⟨ meaning-⊕ {t = app f s} {Δt = app (app (derive f) s) ds} ⟩ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ ∅ ∎}) where open ≡-Reasoning
call derive-correct-closed instead of derive-correct in thm:main
call derive-correct-closed instead of derive-correct in thm:main Old-commit-hash: 9fa1103b0ac110498ae8ab0d0c0f3320ff4b264f
Agda
mit
inc-lc/ilc-agda
88a46de4b9496c0c6c4f861d964d90e3ae3cfd27
Syntax/Derive/Plotkin.agda
Syntax/Derive/Plotkin.agda
import Parametric.Syntax.Type as Type import Base.Syntax.Context as Context import Syntax.Term.Plotkin as Term import Syntax.DeltaContext as DeltaContext import Parametric.Change.Type as DeltaType module Syntax.Derive.Plotkin {Base : Set {- of base types -}} {Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}} (ΔBase : Base → Base) (deriveConst : ∀ {Γ Σ τ} → Constant Σ τ → Term.Terms Constant (DeltaContext.ΔContext (DeltaType.ΔType ΔBase) Γ) (DeltaContext.ΔContext (DeltaType.ΔType ΔBase) Σ) → Term.Term Constant (DeltaContext.ΔContext (DeltaType.ΔType ΔBase) Γ) (DeltaType.ΔType ΔBase τ)) where -- Terms of languages described in Plotkin style open Type Base open Context Type open import Syntax.Context.Plotkin Base open Term Constant open DeltaType ΔBase open DeltaContext ΔType fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ fit = weaken Γ≼ΔΓ deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (ΔContext Σ) derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ) derive-terms {∅} ∅ = ∅ derive-terms {τ • Σ} (t • ts) = derive t • fit t • derive-terms ts derive (var x) = var (deriveVar x) derive (app s t) = app (app (derive s) (fit t)) (derive t) derive (abs t) = abs (abs (derive t)) derive (const c ts) = deriveConst c (derive-terms ts)
import Parametric.Syntax.Type as Type import Base.Syntax.Context as Context import Syntax.Term.Plotkin as Term import Syntax.DeltaContext as DeltaContext import Parametric.Change.Type as ChangeType module Syntax.Derive.Plotkin {Base : Set {- of base types -}} {Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}} (ΔBase : Base → Base) (deriveConst : ∀ {Γ Σ τ} → Constant Σ τ → Term.Terms Constant (DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Γ) (DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Σ) → Term.Term Constant (DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Γ) (ChangeType.ΔType ΔBase τ)) where -- Terms of languages described in Plotkin style open Type Base open Context Type open import Syntax.Context.Plotkin Base open Term Constant open ChangeType ΔBase open DeltaContext ΔType fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ fit = weaken Γ≼ΔΓ deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (ΔContext Σ) derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ) derive-terms {∅} ∅ = ∅ derive-terms {τ • Σ} (t • ts) = derive t • fit t • derive-terms ts derive (var x) = var (deriveVar x) derive (app s t) = app (app (derive s) (fit t)) (derive t) derive (abs t) = abs (abs (derive t)) derive (const c ts) = deriveConst c (derive-terms ts)
Update qualified import for new module name.
Update qualified import for new module name. Old-commit-hash: 2cb47a8d412768ed582e8af1a042b51c3ca781be
Agda
mit
inc-lc/ilc-agda
92f785054b5dd55703ecbd7d5aec15af5e0d04d2
README.agda
README.agda
module README where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- * Δ e describes how e changes when its free variables or its arguments change -- * denotational semantics including semantics of changes -- -- Note that Δ is *not* the same as the ∂ operator in -- definition/intro.tex. See discussion at: -- -- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325 -- -- Work in Progress: -- * lemmas about behavior of changes -- * lemmas about behavior of Δ -- * correctness proof for symbolic derivation -- The formalization is split across the following files. -- TODO: document them more. -- Note that we postulate function extensionality (in -- `Denotational.ExtensionalityPostulate`), known to be consistent -- with intensional type theory. open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.ChangeTypes.ChangesAreDerivatives open import Syntactic.ChangeContexts open import Syntactic.Terms.Total open import Syntactic.Changes open import Denotational.Notation open import Denotational.Values open import Denotational.ExtensionalityPostulate open import Denotational.EqualityLemmas open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Changes open import Denotational.Equivalence open import Denotational.ValidChanges open import Denotational.WeakValidChanges open import ChangeContexts open import ChangeContextLifting open import PropsDelta open import SymbolicDerivation -- This finishes the correctness proof of the above work. open import total -- EXAMPLES open import Examples.Examples1 -- NATURAL semantics open import Syntactic.Closures open import Denotational.Closures open import Natural.Lookup open import Natural.Evaluation open import Natural.Soundness -- Other calculi open import partial open import lambda
module README where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- -- XXX: fill in again other details. -- 41e0f95d7fda7244a258b67f8a4255e4128a42c3 declares that the main theorems are -- in these files: open import Denotation.Derive.Canon-Popl14 open import Denotation.Derive.Optimized-Popl14 -- TODO: this file is intended to load all maintained Agda code; otherwise, a -- better solution to issue #41 should be found. -- -- Moreover, arguably it would be good if this file would list all relevant -- imports, to highlight the structure of the development, as it did previously. -- But I'm not sure what's the best structure.
Update a bit README.agda (#41)
Update a bit README.agda (#41) README.agda was not up-to-date. Remaining issues are described inline. Old-commit-hash: 93469332bbc849a2a13486a1bfae7d21dc9eb399
Agda
mit
inc-lc/ilc-agda
9cdc324caee9ca0888f5f85d83d0727e04d6e654
incremental.agda
incremental.agda
module incremental where -- SIMPLE TYPES -- Syntax data Type : Set where _⇒_ : (τ₁ τ₂ : Type) → Type infixr 5 _⇒_ -- Semantics Dom⟦_⟧ : Type -> Set Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧ -- TYPING CONTEXTS -- Syntax data Context : Set where ∅ : Context _•_ : (τ : Type) (Γ : Context) → Context infixr 9 _•_ -- Semantics data Empty : Set where ∅ : Empty data Bind A B : Set where _•_ : (v : A) (ρ : B) → Bind A B Env⟦_⟧ : Context → Set Env⟦ ∅ ⟧ = Empty Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧ -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ -- Semantics lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ lookup⟦ this ⟧ (v • ρ) = v lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ -- TERMS -- Syntax data Term : Context → Type → Set where abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂) app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂ var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ -- Semantics eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ) eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ) eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ -- WEAKENING -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₁ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂ -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- Weaken a term to a super context weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t) weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂) weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x) -- CHANGE TYPES Δ-Type : Type → Type Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂ apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply {τ₁ ⇒ τ₂} = abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x ) ( f x ) compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ) compose {τ₁ ⇒ τ₂} = abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λdg. λx. compose ( df x ) ( dg x ) nil : ∀ {τ Γ} → Term Γ (Δ-Type τ) nil {τ₁ ⇒ τ₂} = abs nil -- λx. nil -- Hey, apply is α-equivalent to compose, what's going on? -- Oh, and `Δ-Type` is the identity function? -- CHANGE CONTEXTS Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ -- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ) Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t) Δ-term {Γ} (app t₁ t₂) = {!!} Δ-term {Γ} (var x) = {!!}
module incremental where -- SIMPLE TYPES -- Syntax data Type : Set where _⇒_ : (τ₁ τ₂ : Type) → Type infixr 5 _⇒_ -- Semantics Dom⟦_⟧ : Type -> Set Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧ -- TYPING CONTEXTS -- Syntax data Context : Set where ∅ : Context _•_ : (τ : Type) (Γ : Context) → Context infixr 9 _•_ -- Semantics data Empty : Set where ∅ : Empty data Bind A B : Set where _•_ : (v : A) (ρ : B) → Bind A B Env⟦_⟧ : Context → Set Env⟦ ∅ ⟧ = Empty Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧ -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ -- Semantics lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ lookup⟦ this ⟧ (v • ρ) = v lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ -- TERMS -- Syntax data Term : Context → Type → Set where abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂) app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂ var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ -- Semantics eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ) eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ) eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ -- WEAKENING -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₁ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂ -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- Weaken a term to a super context weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t) weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂) weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x) -- CHANGE TYPES Δ-Type : Type → Type Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂ apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply {τ₁ ⇒ τ₂} = abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x ) ( f x ) compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ) compose {τ₁ ⇒ τ₂} = abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λdg. λx. compose ( df x ) ( dg x ) nil : ∀ {τ Γ} → Term Γ (Δ-Type τ) nil {τ₁ ⇒ τ₂} = abs nil -- λx. nil -- Hey, apply is α-equivalent to compose, what's going on? -- Oh, and `Δ-Type` is the identity function? -- CHANGE CONTEXTS Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ -- CHANGE VARIABLES -- changes 'x' to 'dx' rename : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) rename this = that this rename (that x) = that (that (rename x)) -- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound) Δ-var : ∀ {Γ₁ Γ₂ τ} → Var (Γ₁ ⋎ Γ₂) τ → Term (Δ-Context Γ₂) (Δ-Type τ) Δ-var {∅} x = var (rename x) Δ-var {τ • Γ} this = nil Δ-var {τ • Γ} (that x) = Δ-var {Γ} x -- CHANGE TERMS Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ) Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t) Δ-term {Γ} (app t₁ t₂) = {!!} Δ-term {Γ} (var x) = weaken {∅} {Γ} (Δ-var {Γ} x)
Implement 'Δ-term' for variables.
Implement 'Δ-term' for variables. Old-commit-hash: dd0c2916cc4b7472353b0073fa6538f60e7325f5
Agda
mit
inc-lc/ilc-agda
f9aeb58a90443e04e8fd3afd4951c3d768d88d29
Syntax/Context/Plotkin.agda
Syntax/Context/Plotkin.agda
module Syntax.Context.Plotkin where -- Context for Plotkin-stype language descriptions -- -- Duplicates Syntax.Context to a large extent. -- Consider having Syntax.Context import this module. infixr 9 _•_ data Context {Type : Set} : Set where ∅ : Context {Type} _•_ : (τ : Type) (Γ : Context {Type}) → Context {Type} data Var {Type : Set} : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
module Syntax.Context.Plotkin where -- Context for Plotkin-stype language descriptions -- -- This module will tend to duplicate Syntax.Context to a large -- extent. Consider having Syntax.Context specialize future -- content of this module to maintain its interface. infixr 9 _•_ data Context {Type : Set} : Set where ∅ : Context {Type} _•_ : (τ : Type) (Γ : Context {Type}) → Context {Type} data Var {Type : Set} : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
update description of Plotkin's context
Agda: update description of Plotkin's context Reason: the previous description contains a modularization task that is already partially carried out. Reference: https://github.com/ps-mr/ilc/commit/249e99d807e2382a37fb35aee78c235b98e427aa#commitcomment-3659727 Old-commit-hash: 3c52ba4a07600646db8de764b1460fa6b69c3014
Agda
mit
inc-lc/ilc-agda
63b0b983e562c4195da605874a1e5b1e379fa13c
UNDEFINED.agda
UNDEFINED.agda
-- Allow holes in modules to import, by introducing a single general postulate. module UNDEFINED where open import Data.Unit.NonEta using (Hidden) open import Data.Unit.NonEta public using (reveal) postulate -- If this postulate would produce a T, it could be used as instance argument, triggering many ambiguities. -- This postulate is named in capitals because it introduces an inconsistency. -- Hence, this must be used through `reveal UNDEFINED`. UNDEFINED : ∀ {ℓ} → {T : Set ℓ} → Hidden T
-- Allow holes in modules to import, by introducing a single general postulate. module UNDEFINED where postulate UNDEFINED : ∀ {ℓ} → {T : Set ℓ} → T
Refactor UNDEFINED to simplify it (XXX untested)
Refactor UNDEFINED to simplify it (XXX untested) Instance arguments changed behavior, so not every definition is available anymore. Hence we can avoid the wrapping. It typechecks, but not really tested on clients.
Agda
mit
inc-lc/ilc-agda
9c628f8fc4514a3379c8bb109b925c9a196d5571
Parametric/Syntax/MTerm.agda
Parametric/Syntax/MTerm.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Syntax.MType as MType module Parametric.Syntax.MTerm {Base : Type.Structure} (Const : Term.Structure Base) where open Type.Structure Base open MType.Structure Base open Term.Structure Base Const -- Our extension points are sets of primitives, indexed by the -- types of their arguments and their return type. -- We want different types of constants; some produce values, some produce -- computations. In all cases, arguments are assumed to be values (though -- individual primitives might require thunks). ValConstStructure : Set₁ ValConstStructure = ValContext → ValType → Set CompConstStructure : Set₁ CompConstStructure = ValContext → CompType → Set module Structure (ValConst : ValConstStructure) (CompConst : CompConstStructure) where mutual -- Analogues of Terms Vals : ValContext → ValContext → Set Vals Γ = DependentList (Val Γ) Comps : ValContext → List CompType → Set Comps Γ = DependentList (Comp Γ) data Val Γ : (τ : ValType) → Set where vVar : ∀ {τ} (x : ValVar Γ τ) → Val Γ τ -- XXX Do we need thunks? The draft in the paper doesn't have them. -- However, they will start being useful if we deal with CBN source -- languages. vThunk : ∀ {τ} → Comp Γ τ → Val Γ (U τ) vConst : ∀ {Σ τ} → (c : ValConst Σ τ) → (args : Vals Γ Σ) → Val Γ τ data Comp Γ : (τ : CompType) → Set where cConst : ∀ {Σ τ} → (c : CompConst Σ τ) → (args : Vals Γ Σ) → Comp Γ τ cForce : ∀ {τ} → Val Γ (U τ) → Comp Γ τ cReturn : ∀ {τ} (v : Val Γ τ) → Comp Γ (F τ) {- -- Originally, M to x in N. But here we have no names! _into_ : ∀ {σ τ} → (e₁ : Comp Γ (F σ)) → (e₂ : Comp (σ •• Γ) τ) → Comp Γ τ -} -- The following constructor is the main difference between CBPV and this -- monadic calculus. This is better for the caching transformation. _into_ : ∀ {σ τ} → (e₁ : Comp Γ (F σ)) → (e₂ : Comp (σ •• Γ) (F τ)) → Comp Γ (F τ) cAbs : ∀ {σ τ} → (t : Comp (σ •• Γ) τ) → Comp Γ (σ ⇛ τ) cApp : ∀ {σ τ} → (s : Comp Γ (σ ⇛ τ)) → (t : Val Γ σ) → Comp Γ τ weaken-val : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) → Val Γ₁ τ → Val Γ₂ τ weaken-comp : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) → Comp Γ₁ τ → Comp Γ₂ τ weaken-vals : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) → Vals Γ₁ Σ → Vals Γ₂ Σ weaken-val Γ₁≼Γ₂ (vVar x) = vVar (weaken-val-var Γ₁≼Γ₂ x) weaken-val Γ₁≼Γ₂ (vThunk x) = vThunk (weaken-comp Γ₁≼Γ₂ x) weaken-val Γ₁≼Γ₂ (vConst c args) = vConst c (weaken-vals Γ₁≼Γ₂ args) weaken-comp Γ₁≼Γ₂ (cConst c args) = cConst c (weaken-vals Γ₁≼Γ₂ args) weaken-comp Γ₁≼Γ₂ (cForce x) = cForce (weaken-val Γ₁≼Γ₂ x) weaken-comp Γ₁≼Γ₂ (cReturn v) = cReturn (weaken-val Γ₁≼Γ₂ v) weaken-comp Γ₁≼Γ₂ (_into_ {σ} c c₁) = (weaken-comp Γ₁≼Γ₂ c) into (weaken-comp (keep σ •• Γ₁≼Γ₂) c₁) weaken-comp Γ₁≼Γ₂ (cAbs {σ} c) = cAbs (weaken-comp (keep σ •• Γ₁≼Γ₂) c) weaken-comp Γ₁≼Γ₂ (cApp s t) = cApp (weaken-comp Γ₁≼Γ₂ s) (weaken-val Γ₁≼Γ₂ t) weaken-vals Γ₁≼Γ₂ ∅ = ∅ weaken-vals Γ₁≼Γ₂ (px • ts) = (weaken-val Γ₁≼Γ₂ px) • (weaken-vals Γ₁≼Γ₂ ts) fromCBN : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBNCtx Γ) (cbnToCompType τ) fromCBNTerms : ∀ {Γ Σ} → Terms Γ Σ → Vals (fromCBNCtx Γ) (fromCBNCtx Σ) fromCBNTerms ∅ = ∅ fromCBNTerms (px • ts) = vThunk (fromCBN px) • fromCBNTerms ts open import UNDEFINED -- This is really supposed to be part of the plugin interface. cbnToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBNCtx Σ) (cbnToCompType τ) cbnToCompConst = reveal UNDEFINED fromCBN (const c args) = cConst (cbnToCompConst c) (fromCBNTerms args) fromCBN (var x) = cForce (vVar (fromVar cbnToValType x)) fromCBN (app s t) = cApp (fromCBN s) (vThunk (fromCBN t)) fromCBN (abs t) = cAbs (fromCBN t) -- To satisfy termination checking, we'd need to inline fromCBV and weaken: fromCBV needs to produce a term in a bigger context. -- But let's ignore that. {-# NO_TERMINATION_CHECK #-} fromCBV : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBVCtx Γ) (cbvToCompType τ) -- This is really supposed to be part of the plugin interface. cbvToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBVCtx Σ) (cbvToCompType τ) cbvToCompConst = reveal UNDEFINED cbvTermsToComps : ∀ {Γ Σ} → Terms Γ Σ → Comps (fromCBVCtx Γ) (fromCBVToCompList Σ) cbvTermsToComps ∅ = ∅ cbvTermsToComps (px • ts) = fromCBV px • cbvTermsToComps ts module _ where dequeValContexts : ValContext → ValContext → ValContext dequeValContexts ∅ Γ = Γ dequeValContexts (x • Σ) Γ = dequeValContexts Σ (x • Γ) dequeValContexts≼≼ : ∀ Σ Γ → Γ ≼≼ dequeValContexts Σ Γ dequeValContexts≼≼ ∅ Γ = ≼≼-refl dequeValContexts≼≼ (x • Σ) Γ = ≼≼-trans (drop_••_ x ≼≼-refl) (dequeValContexts≼≼ Σ (x • Γ)) -- dequeContexts : Context → Context → ValContext dequeContexts Σ Γ = dequeValContexts (fromCBVCtx Σ) (fromCBVCtx Γ) fromCBVArg : ∀ {σ Γ τ} → Term Γ τ → (Val (cbvToValType τ • fromCBVCtx Γ) (cbvToValType τ) → Comp (cbvToValType τ • fromCBVCtx Γ) (cbvToCompType σ)) → Comp (fromCBVCtx Γ) (cbvToCompType σ) fromCBVArg t k = (fromCBV t) into k (vVar vThis) fromCBVArgs : ∀ {Σ Γ τ} → Terms Γ Σ → (Vals (dequeContexts Σ Γ) (fromCBVCtx Σ) → Comp (dequeContexts Σ Γ) (cbvToCompType τ)) → Comp (fromCBVCtx Γ) (cbvToCompType τ) fromCBVArgs ∅ k = k ∅ fromCBVArgs {σ • Σ} {Γ} (t • ts) k = fromCBVArg t (λ v → fromCBVArgs (weaken-terms (drop_•_ _ ≼-refl) ts) (λ vs → k (weaken-val (dequeValContexts≼≼ (fromCBVCtx Σ) _) v • vs))) -- Transform a constant application into -- -- arg1 to x1 in (arg2 to x2 in ... (argn to xn in (cConst (cbvToCompConst c) (x1 :: x2 :: ... xn)))). fromCBVConstCPSRoot : ∀ {Σ Γ τ} → Const Σ τ → Terms Γ Σ → Comp (fromCBVCtx Γ) (cbvToCompType τ) -- pass that as a closure to compose with (_•_ x) for each new variable. fromCBVConstCPSRoot c ts = fromCBVArgs ts (λ vs → cConst (cbvToCompConst c) vs) -- fromCBVConstCPSDo ts {!cConst (cbvToCompConst c)!} -- In the beginning, we should get a function that expects a whole set of -- arguments (Vals Γ Σ) for c, in the initial context Γ. -- Later, each call should match: -- - Σ = τΣ • Σ′, then we recurse with Γ′ = τΣ • Γ and Σ′. Terms ought to be -- weakened. The result of f (or the arguments) also ought to be weakened! So f should weaken -- all arguments. -- - Σ = ∅. fromCBV (const c args) = fromCBVConstCPSRoot c args fromCBV (app s t) = (fromCBV s) into ((fromCBV (weaken (drop _ • ≼-refl) t)) into cApp (cForce (vVar (vThat vThis))) (vVar vThis)) -- Values fromCBV (var x) = cReturn (vVar (fromVar cbvToValType x)) fromCBV (abs t) = cReturn (vThunk (cAbs (fromCBV t))) -- This reflects the CBV conversion of values in TLCA '99, but we can't write -- this because it's partial on the Term type. Instead, we duplicate thunking -- at the call site. {- fromCBVToVal : ∀ {Γ τ} (t : Term Γ τ) → Val (fromCBVCtx Γ) (cbvToValType τ) fromCBVToVal (var x) = vVar (fromVar cbvToValType x) fromCBVToVal (abs t) = vThunk (cAbs (fromCBV t)) fromCBVToVal (const c args) = {!!} fromCBVToVal (app t t₁) = {!!} -- Not a value -}
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Syntax.MType as MType module Parametric.Syntax.MTerm {Base : Type.Structure} (Const : Term.Structure Base) where open Type.Structure Base open MType.Structure Base open Term.Structure Base Const -- Our extension points are sets of primitives, indexed by the -- types of their arguments and their return type. -- We want different types of constants; some produce values, some produce -- computations. In all cases, arguments are assumed to be values (though -- individual primitives might require thunks). ValConstStructure : Set₁ ValConstStructure = ValContext → ValType → Set CompConstStructure : Set₁ CompConstStructure = ValContext → CompType → Set module Structure (ValConst : ValConstStructure) (CompConst : CompConstStructure) where mutual -- Analogues of Terms Vals : ValContext → ValContext → Set Vals Γ = DependentList (Val Γ) Comps : ValContext → List CompType → Set Comps Γ = DependentList (Comp Γ) data Val Γ : (τ : ValType) → Set where vVar : ∀ {τ} (x : ValVar Γ τ) → Val Γ τ -- XXX Do we need thunks? The draft in the paper doesn't have them. -- However, they will start being useful if we deal with CBN source -- languages. vThunk : ∀ {τ} → Comp Γ τ → Val Γ (U τ) vConst : ∀ {Σ τ} → (c : ValConst Σ τ) → (args : Vals Γ Σ) → Val Γ τ data Comp Γ : (τ : CompType) → Set where cConst : ∀ {Σ τ} → (c : CompConst Σ τ) → (args : Vals Γ Σ) → Comp Γ τ cForce : ∀ {τ} → Val Γ (U τ) → Comp Γ τ cReturn : ∀ {τ} (v : Val Γ τ) → Comp Γ (F τ) {- -- Originally, M to x in N. But here we have no names! _into_ : ∀ {σ τ} → (e₁ : Comp Γ (F σ)) → (e₂ : Comp (σ •• Γ) τ) → Comp Γ τ -} -- The following constructor is the main difference between CBPV and this -- monadic calculus. This is better for the caching transformation. _into_ : ∀ {σ τ} → (e₁ : Comp Γ (F σ)) → (e₂ : Comp (σ •• Γ) (F τ)) → Comp Γ (F τ) cAbs : ∀ {σ τ} → (t : Comp (σ •• Γ) τ) → Comp Γ (σ ⇛ τ) cApp : ∀ {σ τ} → (s : Comp Γ (σ ⇛ τ)) → (t : Val Γ σ) → Comp Γ τ weaken-val : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) → Val Γ₁ τ → Val Γ₂ τ weaken-comp : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) → Comp Γ₁ τ → Comp Γ₂ τ weaken-vals : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) → Vals Γ₁ Σ → Vals Γ₂ Σ weaken-val Γ₁≼Γ₂ (vVar x) = vVar (weaken-val-var Γ₁≼Γ₂ x) weaken-val Γ₁≼Γ₂ (vThunk x) = vThunk (weaken-comp Γ₁≼Γ₂ x) weaken-val Γ₁≼Γ₂ (vConst c args) = vConst c (weaken-vals Γ₁≼Γ₂ args) weaken-comp Γ₁≼Γ₂ (cConst c args) = cConst c (weaken-vals Γ₁≼Γ₂ args) weaken-comp Γ₁≼Γ₂ (cForce x) = cForce (weaken-val Γ₁≼Γ₂ x) weaken-comp Γ₁≼Γ₂ (cReturn v) = cReturn (weaken-val Γ₁≼Γ₂ v) weaken-comp Γ₁≼Γ₂ (_into_ {σ} c c₁) = (weaken-comp Γ₁≼Γ₂ c) into (weaken-comp (keep σ •• Γ₁≼Γ₂) c₁) weaken-comp Γ₁≼Γ₂ (cAbs {σ} c) = cAbs (weaken-comp (keep σ •• Γ₁≼Γ₂) c) weaken-comp Γ₁≼Γ₂ (cApp s t) = cApp (weaken-comp Γ₁≼Γ₂ s) (weaken-val Γ₁≼Γ₂ t) weaken-vals Γ₁≼Γ₂ ∅ = ∅ weaken-vals Γ₁≼Γ₂ (px • ts) = (weaken-val Γ₁≼Γ₂ px) • (weaken-vals Γ₁≼Γ₂ ts) fromCBN : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBNCtx Γ) (cbnToCompType τ) fromCBNTerms : ∀ {Γ Σ} → Terms Γ Σ → Vals (fromCBNCtx Γ) (fromCBNCtx Σ) fromCBNTerms ∅ = ∅ fromCBNTerms (px • ts) = vThunk (fromCBN px) • fromCBNTerms ts open import UNDEFINED -- This is really supposed to be part of the plugin interface. cbnToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBNCtx Σ) (cbnToCompType τ) cbnToCompConst = reveal UNDEFINED fromCBN (const c args) = cConst (cbnToCompConst c) (fromCBNTerms args) fromCBN (var x) = cForce (vVar (fromVar cbnToValType x)) fromCBN (app s t) = cApp (fromCBN s) (vThunk (fromCBN t)) fromCBN (abs t) = cAbs (fromCBN t) -- To satisfy termination checking, we'd need to inline fromCBV and weaken: fromCBV needs to produce a term in a bigger context. -- But let's ignore that. {-# NO_TERMINATION_CHECK #-} fromCBV : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBVCtx Γ) (cbvToCompType τ) -- This is really supposed to be part of the plugin interface. cbvToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBVCtx Σ) (cbvToCompType τ) cbvToCompConst = reveal UNDEFINED cbvTermsToComps : ∀ {Γ Σ} → Terms Γ Σ → Comps (fromCBVCtx Γ) (fromCBVToCompList Σ) cbvTermsToComps ∅ = ∅ cbvTermsToComps (px • ts) = fromCBV px • cbvTermsToComps ts module _ where dequeValContexts : ValContext → ValContext → ValContext dequeValContexts ∅ Γ = Γ dequeValContexts (x • Σ) Γ = dequeValContexts Σ (x • Γ) dequeValContexts≼≼ : ∀ Σ Γ → Γ ≼≼ dequeValContexts Σ Γ dequeValContexts≼≼ ∅ Γ = ≼≼-refl dequeValContexts≼≼ (x • Σ) Γ = ≼≼-trans (drop_••_ x ≼≼-refl) (dequeValContexts≼≼ Σ (x • Γ)) -- dequeContexts : Context → Context → ValContext dequeContexts Σ Γ = dequeValContexts (fromCBVCtx Σ) (fromCBVCtx Γ) fromCBVArg : ∀ {σ Γ τ} → Term Γ τ → (Val (cbvToValType τ • fromCBVCtx Γ) (cbvToValType τ) → Comp (cbvToValType τ • fromCBVCtx Γ) (cbvToCompType σ)) → Comp (fromCBVCtx Γ) (cbvToCompType σ) fromCBVArg t k = (fromCBV t) into k (vVar vThis) fromCBVArgs : ∀ {Σ Γ τ} → Terms Γ Σ → (Vals (dequeContexts Σ Γ) (fromCBVCtx Σ) → Comp (dequeContexts Σ Γ) (cbvToCompType τ)) → Comp (fromCBVCtx Γ) (cbvToCompType τ) fromCBVArgs ∅ k = k ∅ fromCBVArgs {σ • Σ} {Γ} (t • ts) k = fromCBVArg t (λ v → fromCBVArgs (weaken-terms (drop_•_ _ ≼-refl) ts) (λ vs → k (weaken-val (dequeValContexts≼≼ (fromCBVCtx Σ) _) v • vs))) -- Transform a constant application into -- -- arg1 to x1 in (arg2 to x2 in ... (argn to xn in (cConst (cbvToCompConst c) (x1 :: x2 :: ... xn)))). fromCBVConstCPSRoot : ∀ {Σ Γ τ} → Const Σ τ → Terms Γ Σ → Comp (fromCBVCtx Γ) (cbvToCompType τ) -- pass that as a closure to compose with (_•_ x) for each new variable. fromCBVConstCPSRoot c ts = fromCBVArgs ts (λ vs → cConst (cbvToCompConst c) vs) -- fromCBVConstCPSDo ts {!cConst (cbvToCompConst c)!} -- In the beginning, we should get a function that expects a whole set of -- arguments (Vals Γ Σ) for c, in the initial context Γ. -- Later, each call should match: -- - Σ = τΣ • Σ′, then we recurse with Γ′ = τΣ • Γ and Σ′. Terms ought to be -- weakened. The result of f (or the arguments) also ought to be weakened! So f should weaken -- all arguments. -- - Σ = ∅. fromCBV (const c args) = fromCBVConstCPSRoot c args fromCBV (app s t) = (fromCBV s) into (fromCBV (weaken (drop _ • ≼-refl) t) into cApp (cForce (vVar (vThat vThis))) (vVar vThis)) -- Values fromCBV (var x) = cReturn (vVar (fromVar cbvToValType x)) fromCBV (abs t) = cReturn (vThunk (cAbs (fromCBV t))) -- This reflects the CBV conversion of values in TLCA '99, but we can't write -- this because it's partial on the Term type. Instead, we duplicate thunking -- at the call site. {- fromCBVToVal : ∀ {Γ τ} (t : Term Γ τ) → Val (fromCBVCtx Γ) (cbvToValType τ) fromCBVToVal (var x) = vVar (fromVar cbvToValType x) fromCBVToVal (abs t) = vThunk (cAbs (fromCBV t)) fromCBVToVal (const c args) = {!!} fromCBVToVal (app t t₁) = {!!} -- Not a value -}
Drop extra parens
Drop extra parens
Agda
mit
inc-lc/ilc-agda
bf4c2374eee5aebf6d658c3c557a4233b3ba3172
flipbased.agda
flipbased.agda
module flipbased where open import Algebra import Level as L open L using () renaming (_⊔_ to _L⊔_) open import Function hiding (_⟨_⟩_) open import Data.Nat.NP open import Data.Nat.Properties open import Data.Product using (proj₁; proj₂; _,_; swap; _×_) open import Data.Bits hiding (replicateM) open import Data.Bool open import Data.Vec using (Vec; []; _∷_; take; drop; head; tail) open import Relation.Binary import Relation.Binary.PropositionalEquality as ≡ open ≡ using (_≡_) record M {a} n (A : Set a) : Set a where constructor mk field run : Bits n → A toss′ : M 1 Bit toss′ = mk head return′ : ∀ {a} {A : Set a} → A → M 0 A return′ = mk ∘ const pure′ : ∀ {a} {A : Set a} → A → M 0 A pure′ = return′ comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → M m A → M n A comap f (mk g) = mk (g ∘ f) weaken : ∀ {m n a} {A : Set a} → M n A → M (m + n) A weaken {m} = comap (drop m) weaken′ : ∀ {m n a} {A : Set a} → M n A → M (n + m) A weaken′ = comap (take _) private take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n take≤ z≤n _ = [] take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → M m A → M n A weaken≤ p = comap (take≤ p) coerce : ∀ {m n a} {A : Set a} → m ≡ n → M m A → M n A coerce ≡.refl = id toss : ∀ {n} → M (1 + n) Bit toss = weaken′ toss′ return : ∀ {n a} {A : Set a} → A → M n A return = weaken′ ∘ return′ pure : ∀ {n a} {A : Set a} → A → M n A pure = return _>>=_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ A → (A → M n₂ B) → M (n₁ + n₂) B _>>=_ {n₁} x f = mk (λ bs → M.run (f (M.run x (take _ bs))) (drop n₁ bs)) _>>=′_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ A → (A → M n₂ B) → M (n₂ + n₁) B _>>=′_ {n₁} {n₂} rewrite ℕ°.+-comm n₂ n₁ = _>>=_ _>>_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ A → M n₂ B → M (n₁ + n₂) B _>>_ {n₁} x y = x >>= const y map : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → M n A → M n B map f x = mk (f ∘ M.run x) -- map f x ≗ x >>=′ (return {0} ∘ f) join : ∀ {n₁ n₂ a} {A : Set a} → M n₁ (M n₂ A) → M (n₁ + n₂) A join x = x >>= id infixl 4 _⊛_ _⊛_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ (A → B) → M n₂ A → M (n₁ + n₂) B _⊛_ {n₁} mf mx = mf >>= λ f → map (_$_ f) mx _⟨_⟩_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} → M m A → (A → B → C) → M n B → M (m + n) C x ⟨ f ⟩ y = map f x ⊛ y ⟪_⟫ : ∀ {n} {a} {A : Set a} → A → M n A ⟪_⟫ = pure ⟪_⟫′ : ∀ {a} {A : Set a} → A → M 0 A ⟪_⟫′ = pure′ ⟪_·_⟫ : ∀ {a b} {A : Set a} {B : Set b} {n} → (A → B) → M n A → M n B ⟪ f · x ⟫ = map f x ⟪_·_·_⟫ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} → (A → B → C) → M m A → M n B → M (m + n) C ⟪ f · x · y ⟫ = map f x ⊛ y ⟪_·_·_·_⟫ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} → (A → B → C → D) → M m A → M n B → M o C → M (m + n + o) D ⟪ f · x · y · z ⟫ = map f x ⊛ y ⊛ z _⟨,⟩_ : ∀ {a b} {A : Set a} {B : Set b} {m n} → M m A → M n B → M (m + n) (A × B) x ⟨,⟩ y = ⟪ _,_ · x · y ⟫ _⟨xor⟩_ : ∀ {n₁ n₂} → M n₁ Bit → M n₂ Bit → M (n₁ + n₂) Bit x ⟨xor⟩ y = ⟪ _xor_ · x · y ⟫ _⟨⊕⟩_ : ∀ {n₁ n₂ m} → M n₁ (Bits m) → M n₂ (Bits m) → M (n₁ + n₂) (Bits m) x ⟨⊕⟩ y = ⟪ _⊕_ · x · y ⟫ replicateM : ∀ {n m} {a} {A : Set a} → M m A → M (n * m) (Vec A n) replicateM {zero} _ = ⟪ [] ⟫ replicateM {suc _} x = ⟪ _∷_ · x · replicateM x ⟫ random : ∀ {n} → M n (Bits n) -- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce random {zero} = ⟪ [] ⟫ random {suc _} = ⟪ _∷_ · toss′ · random ⟫ randomTbl : ∀ m n → M (2 ^ m * n) (Vec (Bits n) (2 ^ m)) randomTbl m n = replicateM random randomFun : ∀ m n → M (2 ^ m * n) (Bits m → Bits n) randomFun m n = ⟪ funFromTbl · randomTbl m n ⟫ randomFunExt : ∀ {n k a} {A : Set a} → M k (Bits n → A) → M (k + k) (Bits (suc n) → A) randomFunExt f = ⟪ comb · f · f ⟫ where comb = λ g₁ g₂ xs → (if head xs then g₁ else g₂) (tail xs) 2*_ : ℕ → ℕ 2* x = x + x 2^_ : ℕ → ℕ 2^ 0 = 1 2^ (suc n) = 2* (2^ n) costRndFun : ℕ → ℕ → ℕ costRndFun zero n = n costRndFun (suc m) n = 2* (costRndFun m n) lem : ∀ m n → costRndFun m n ≡ 2 ^ m * n lem zero n rewrite ℕ°.+-comm n 0 = ≡.refl lem (suc m) n rewrite lem m n | ℕ°.*-assoc 2 (2 ^ m) n | ℕ°.+-comm (2 ^ m * n) 0 = ≡.refl randomFun′ : ∀ {m n} → M (costRndFun m n) (Bits m → Bits n) randomFun′ {zero} = ⟪ const · random ⟫ randomFun′ {suc m} = randomFunExt (randomFun′ {m}) record ProgEquiv a ℓ : Set (L.suc ℓ L⊔ L.suc a) where infix 2 _≈_ _≋_ field _≈_ : ∀ {n} {A : Set a} → Rel (M n A) ℓ refl : ∀ {n A} → Reflexive {A = M n A} _≈_ sym : ∀ {n A} → Symmetric {A = M n A} _≈_ -- not strictly transitive reflexive : ∀ {n A} → _≡_ ⇒ _≈_ {n} {A} reflexive ≡.refl = refl _≋_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ _≋_ {n₁} {n₂} p₁ p₂ = _≈_ {n = n₁ ⊔ n₂} (weaken≤ (m≤m⊔n _ _) p₁) (weaken≤ (m≤n⊔m _ n₁) p₂) where m≤n⊔m : ∀ m n → m ≤ n ⊔ m m≤n⊔m m n rewrite ⊔°.+-comm n m = m≤m⊔n m n -- Another name for _≋_ _looks_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ _looks_ = _≋_ module WithEquiv (progEq : ProgEquiv L.zero L.zero) where open ProgEquiv progEq SecPRG : ∀ {k n} (prg : (key : Bits k) → Bits n) → Set SecPRG prg = this looks random where this = ⟪ prg · random ⟫ record PRG k n : Set where constructor _,_ field prg : Bits k → Bits n sec : SecPRG prg OneTimeSecPRF : ∀ {k m n} (prf : (key : Bits k) (msg : Bits m) → Bits n) → Set OneTimeSecPRF prf = ∀ {xs} → let this = ⟪ prf · random · ⟪ xs ⟫′ ⟫ in this looks random record PRF k m n : Set where constructor _,_ field prf : Bits k → Bits m → Bits n sec : OneTimeSecPRF prf OTP : ∀ {n} → Bits n → Bits n → Bits n OTP key msg = key ⊕ msg init : ∀ {k a} {A : Set a} → (Bits k → A) → M k A init f = ⟪ f · random ⟫ module Examples (progEq : ProgEquiv L.zero L.zero) where open ProgEquiv progEq open WithEquiv progEq left-unit-law = ∀ {A B : Set} {n} {x : A} {f : A → M n B} → return′ x >>= f ≈ f x right-unit-law = ∀ {A : Set} {n} {x : M n A} → x >>=′ return′ ≈ x assoc-law = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C} → (x >>= f) >>= g ≋ x >>= (λ x → f x >>= g) assoc-law′ = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C} → (x >>= f) >>= g ≈ coerce (≡.sym (ℕ°.+-assoc n₁ n₂ n₃)) (x >>= (λ x → f x >>= g)) ex₁ = ∀ {x} → toss′ ⟨xor⟩ ⟪ x ⟫′ ≈ ⟪ x ⟫ ex₂ = p ≈ map swap p where p = toss′ ⟨,⟩ toss′ ex₃ = ∀ {n} → OneTimeSecPRF {n} OTP ex₄ = ∀ {k n} (prg : PRG k n) → OneTimeSecPRF (λ key xs → xs ⊕ PRG.prg prg key) ex₅ = ∀ {k n} → PRG k n → PRF k n n
module flipbased where open import Algebra import Level as L open L using () renaming (_⊔_ to _L⊔_) open import Function hiding (_⟨_⟩_) open import Data.Nat.NP open import Data.Bool open import Data.Nat.Properties open import Data.Product using (proj₁; proj₂; _,_; swap; _×_) open import Data.Bits hiding (replicateM) open import Data.Bool open import Data.Vec using (Vec; []; _∷_; take; drop; head; tail) open import Relation.Binary import Relation.Binary.PropositionalEquality as ≡ open ≡ using (_≡_) record M {a} n (A : Set a) : Set a where constructor mk field run : Bits n → A toss′ : M 1 Bit toss′ = mk head return′ : ∀ {a} {A : Set a} → A → M 0 A return′ = mk ∘ const pure′ : ∀ {a} {A : Set a} → A → M 0 A pure′ = return′ comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → M m A → M n A comap f (mk g) = mk (g ∘ f) weaken : ∀ {m n a} {A : Set a} → M n A → M (m + n) A weaken {m} = comap (drop m) weaken′ : ∀ {m n a} {A : Set a} → M n A → M (n + m) A weaken′ = comap (take _) private take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n take≤ z≤n _ = [] take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → M m A → M n A weaken≤ p = comap (take≤ p) coerce : ∀ {m n a} {A : Set a} → m ≡ n → M m A → M n A coerce ≡.refl = id toss : ∀ {n} → M (1 + n) Bit toss = weaken′ toss′ return : ∀ {n a} {A : Set a} → A → M n A return = weaken′ ∘ return′ pure : ∀ {n a} {A : Set a} → A → M n A pure = return _>>=_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ A → (A → M n₂ B) → M (n₁ + n₂) B _>>=_ {n₁} x f = mk (λ bs → M.run (f (M.run x (take _ bs))) (drop n₁ bs)) _>>=′_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ A → (A → M n₂ B) → M (n₂ + n₁) B _>>=′_ {n₁} {n₂} rewrite ℕ°.+-comm n₂ n₁ = _>>=_ _>>_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ A → M n₂ B → M (n₁ + n₂) B _>>_ {n₁} x y = x >>= const y map : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → M n A → M n B map f x = mk (f ∘ M.run x) -- map f x ≗ x >>=′ (return {0} ∘ f) join : ∀ {n₁ n₂ a} {A : Set a} → M n₁ (M n₂ A) → M (n₁ + n₂) A join x = x >>= id infixl 4 _⊛_ _⊛_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} → M n₁ (A → B) → M n₂ A → M (n₁ + n₂) B _⊛_ {n₁} mf mx = mf >>= λ f → map (_$_ f) mx _⟨_⟩_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} → M m A → (A → B → C) → M n B → M (m + n) C x ⟨ f ⟩ y = map f x ⊛ y ⟪_⟫ : ∀ {n} {a} {A : Set a} → A → M n A ⟪_⟫ = pure ⟪_⟫′ : ∀ {a} {A : Set a} → A → M 0 A ⟪_⟫′ = pure′ ⟪_·_⟫ : ∀ {a b} {A : Set a} {B : Set b} {n} → (A → B) → M n A → M n B ⟪ f · x ⟫ = map f x ⟪_·_·_⟫ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} → (A → B → C) → M m A → M n B → M (m + n) C ⟪ f · x · y ⟫ = map f x ⊛ y ⟪_·_·_·_⟫ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} → (A → B → C → D) → M m A → M n B → M o C → M (m + n + o) D ⟪ f · x · y · z ⟫ = map f x ⊛ y ⊛ z choose : ∀ {n a} {A : Set a} → M n A → M n A → M (suc n) A choose x y = ⟪ if_then_else_ · toss · x · y ⟫ _⟨,⟩_ : ∀ {a b} {A : Set a} {B : Set b} {m n} → M m A → M n B → M (m + n) (A × B) x ⟨,⟩ y = ⟪ _,_ · x · y ⟫ _⟨xor⟩_ : ∀ {n₁ n₂} → M n₁ Bit → M n₂ Bit → M (n₁ + n₂) Bit x ⟨xor⟩ y = ⟪ _xor_ · x · y ⟫ _⟨⊕⟩_ : ∀ {n₁ n₂ m} → M n₁ (Bits m) → M n₂ (Bits m) → M (n₁ + n₂) (Bits m) x ⟨⊕⟩ y = ⟪ _⊕_ · x · y ⟫ replicateM : ∀ {n m} {a} {A : Set a} → M m A → M (n * m) (Vec A n) replicateM {zero} _ = ⟪ [] ⟫ replicateM {suc _} x = ⟪ _∷_ · x · replicateM x ⟫ random : ∀ {n} → M n (Bits n) -- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce random {zero} = ⟪ [] ⟫ random {suc _} = ⟪ _∷_ · toss′ · random ⟫ randomTbl : ∀ m n → M (2 ^ m * n) (Vec (Bits n) (2 ^ m)) randomTbl m n = replicateM random randomFun : ∀ m n → M (2 ^ m * n) (Bits m → Bits n) randomFun m n = ⟪ funFromTbl · randomTbl m n ⟫ randomFunExt : ∀ {n k a} {A : Set a} → M k (Bits n → A) → M (k + k) (Bits (suc n) → A) randomFunExt f = ⟪ comb · f · f ⟫ where comb = λ g₁ g₂ xs → (if head xs then g₁ else g₂) (tail xs) 2*_ : ℕ → ℕ 2* x = x + x 2^_ : ℕ → ℕ 2^ 0 = 1 2^ (suc n) = 2* (2^ n) costRndFun : ℕ → ℕ → ℕ costRndFun zero n = n costRndFun (suc m) n = 2* (costRndFun m n) lem : ∀ m n → costRndFun m n ≡ 2 ^ m * n lem zero n rewrite ℕ°.+-comm n 0 = ≡.refl lem (suc m) n rewrite lem m n | ℕ°.*-assoc 2 (2 ^ m) n | ℕ°.+-comm (2 ^ m * n) 0 = ≡.refl randomFun′ : ∀ {m n} → M (costRndFun m n) (Bits m → Bits n) randomFun′ {zero} = ⟪ const · random ⟫ randomFun′ {suc m} = randomFunExt (randomFun′ {m}) record ProgEquiv a ℓ : Set (L.suc ℓ L⊔ L.suc a) where infix 2 _≈_ _≋_ field _≈_ : ∀ {n} {A : Set a} → Rel (M n A) ℓ refl : ∀ {n A} → Reflexive {A = M n A} _≈_ sym : ∀ {n A} → Symmetric {A = M n A} _≈_ -- not strictly transitive reflexive : ∀ {n A} → _≡_ ⇒ _≈_ {n} {A} reflexive ≡.refl = refl _≋_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ _≋_ {n₁} {n₂} p₁ p₂ = _≈_ {n = n₁ ⊔ n₂} (weaken≤ (m≤m⊔n _ _) p₁) (weaken≤ (m≤n⊔m _ n₁) p₂) where m≤n⊔m : ∀ m n → m ≤ n ⊔ m m≤n⊔m m n rewrite ⊔°.+-comm n m = m≤m⊔n m n -- Another name for _≋_ _looks_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ _looks_ = _≋_ module WithEquiv (progEq : ProgEquiv L.zero L.zero) where open ProgEquiv progEq SecPRG : ∀ {k n} (prg : (key : Bits k) → Bits n) → Set SecPRG prg = this looks random where this = ⟪ prg · random ⟫ record PRG k n : Set where constructor _,_ field prg : Bits k → Bits n sec : SecPRG prg OneTimeSecPRF : ∀ {k m n} (prf : (key : Bits k) (msg : Bits m) → Bits n) → Set OneTimeSecPRF prf = ∀ {xs} → let this = ⟪ prf · random · ⟪ xs ⟫′ ⟫ in this looks random record PRF k m n : Set where constructor _,_ field prf : Bits k → Bits m → Bits n sec : OneTimeSecPRF prf OTP : ∀ {n} → Bits n → Bits n → Bits n OTP key msg = key ⊕ msg init : ∀ {k a} {A : Set a} → (Bits k → A) → M k A init f = ⟪ f · random ⟫ module Examples (progEq : ProgEquiv L.zero L.zero) where open ProgEquiv progEq open WithEquiv progEq left-unit-law = ∀ {A B : Set} {n} {x : A} {f : A → M n B} → return′ x >>= f ≈ f x right-unit-law = ∀ {A : Set} {n} {x : M n A} → x >>=′ return′ ≈ x assoc-law = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C} → (x >>= f) >>= g ≋ x >>= (λ x → f x >>= g) assoc-law′ = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C} → (x >>= f) >>= g ≈ coerce (≡.sym (ℕ°.+-assoc n₁ n₂ n₃)) (x >>= (λ x → f x >>= g)) ex₁ = ∀ {x} → toss′ ⟨xor⟩ ⟪ x ⟫′ ≈ ⟪ x ⟫ ex₂ = p ≈ map swap p where p = toss′ ⟨,⟩ toss′ ex₃ = ∀ {n} → OneTimeSecPRF {n} OTP ex₄ = ∀ {k n} (prg : PRG k n) → OneTimeSecPRF (λ key xs → xs ⊕ PRG.prg prg key) ex₅ = ∀ {k n} → PRG k n → PRF k n n
add choose
flipbased: add choose
Agda
bsd-3-clause
crypto-agda/crypto-agda
b2e5536ab47da9ade851c78e2d0e284fdaed9d6e
SymbolicDerivation.agda
SymbolicDerivation.agda
module SymbolicDerivation where open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Syntactic.Changes open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Denotational.ValidChanges open import Natural.Evaluation open import Changes open import ChangeContexts open import ChangeContextLifting open import PropsDelta -- SYMBOLIC DERIVATION derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) derive-var {τ • Γ} this = this derive-var {τ • Γ} (that x) = that (that (derive-var x)) derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ) derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t)) where Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x)) derive-term {{Γ′}} true = false derive-term {{Γ′}} false = false derive-term {{Γ′}} (if c t e) = if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c)) (diff-term (apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e)) (lift-term {{Γ′}} t)) (if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c))) (diff-term (apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t)) (lift-term {{Γ′}} e)) (if (lift-term {{Γ′}} c) (derive-term {{Γ′}} t) (derive-term {{Γ′}} e))) derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t)
module SymbolicDerivation where open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Syntactic.Changes open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Denotational.ValidChanges open import Natural.Evaluation open import Changes open import ChangeContexts open import ChangeContextLifting open import PropsDelta -- SYMBOLIC DERIVATION derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) derive-var {τ • Γ} this = this derive-var {τ • Γ} (that x) = that (that (derive-var x)) derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ) derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t)) where Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x)) derive-term {{Γ′}} true = false derive-term {{Γ′}} false = false derive-term {{Γ′}} (if c t e) = if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c)) (diff-term (apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e)) (lift-term {{Γ′}} t)) (if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c))) (diff-term (apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t)) (lift-term {{Γ′}} e)) (if (lift-term {{Γ′}} c) (derive-term {{Γ′}} t) (derive-term {{Γ′}} e))) derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t)
Break some lines.
Break some lines. Old-commit-hash: f541af3f72f8831d3daba892d4230bda0bdb2cdf
Agda
mit
inc-lc/ilc-agda
789ee03b932a743e1b7f9832f09bd7efc37f4fa8
Popl14/Denotation/Value.agda
Popl14/Denotation/Value.agda
module Popl14.Denotation.Value where open import Popl14.Syntax.Type public open import Popl14.Change.Type public open import Base.Denotation.Notation public open import Structure.Bag.Popl14 open import Data.Integer -- Values of Calculus Popl14 -- -- Contents -- - Domains associated with types -- - `diff` and `apply` in semantic domains ⟦_⟧Type : Type -> Set ⟦ int ⟧Type = ℤ ⟦ bag ⟧Type = Bag ⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type meaningOfType : Meaning Type meaningOfType = meaning ⟦_⟧Type
module Popl14.Denotation.Value where open import Popl14.Syntax.Type public open import Popl14.Change.Type public open import Base.Denotation.Notation public open import Structure.Bag.Popl14 open import Data.Integer -- Values of Calculus Popl14 -- -- Contents -- - Domains associated with types -- - `diff` and `apply` in semantic domains ⟦_⟧Base : Base → Set ⟦ base-int ⟧Base = ℤ ⟦ base-bag ⟧Base = Bag ⟦_⟧Type : Type -> Set ⟦ base ι ⟧Type = ⟦ ι ⟧Base ⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type meaningOfType : Meaning Type meaningOfType = meaning ⟦_⟧Type
Split ⟦_⟧Base from ⟦_⟧Type (WIP).
Split ⟦_⟧Base from ⟦_⟧Type (WIP). Needs fixes to type inference failures! Old-commit-hash: 4f7d5e46e2d2a202dc2c672a08ae4deeebe589c2
Agda
mit
inc-lc/ilc-agda
3ef3b1f40e4b0b9e7308a67388ab552654f5feeb
lib/Data/Bits.agda
lib/Data/Bits.agda
{-# OPTIONS --without-K #-} module Data.Bits where open import Algebra open import Level.NP open import Type hiding (★) open import Data.Nat.NP hiding (_==_) renaming (_<=_ to _ℕ<=_) open import Data.Bit using (Bit) open import Data.Two renaming (_==_ to _==ᵇ_) open import Data.Fin.NP using (Fin; zero; suc; inject₁; inject+; raise; Fin▹ℕ) open import Data.Vec.NP open import Function.NP import Data.List.NP as L -- Re-export some vector functions, maybe they should be given -- less generic types. open Data.Vec.NP public using ([]; _∷_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; onᵢ) Bits : ℕ → ★₀ Bits = Vec Bit _→ᵇ_ : ℕ → ℕ → ★₀ i →ᵇ o = Bits i → Bits o 0ⁿ : ∀ {n} → Bits n 0ⁿ = replicate 0₂ -- Notice that all empty vectors are the same, hence 0ⁿ {0} ≡ 1ⁿ {0} 1ⁿ : ∀ {n} → Bits n 1ⁿ = replicate 1₂ 0∷_ : ∀ {n} → Bits n → Bits (suc n) 0∷ xs = 0₂ ∷ xs -- can't we make these pattern aliases? 1∷_ : ∀ {n} → Bits n → Bits (suc n) 1∷ xs = 1₂ ∷ xs _!_ : ∀ {a n} {A : ★ a} → Vec A n → Fin n → A _!_ = flip lookup -- see Data.Bits.Properties _==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bit [] == [] = 1₂ (b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁ _<=_ : ∀ {n} (xs ys : Bits n) → Bit [] <= [] = 1₂ (1₂ ∷ xs) <= (0₂ ∷ ys) = 0₂ (0₂ ∷ xs) <= (1₂ ∷ ys) = 1₂ (_ ∷ xs) <= (_ ∷ ys) = xs <= ys infixr 5 _⊕_ _⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n _⊕_ = zipWith _xor_ ⊕-group : ℕ → Group ₀ ₀ ⊕-group = LiftGroup.group Xor°.+-group module ⊕-Group (n : ℕ) = Group (⊕-group n) module ⊕-Monoid (n : ℕ) = Monoid (⊕-Group.monoid n) -- Negate all bits, i.e. "xor"ing them by one. vnot : ∀ {n} → Endo (Bits n) vnot = _⊕_ 1ⁿ -- Negate the i-th bit. notᵢ : ∀ {n} (i : Fin n) → Bits n → Bits n notᵢ = onᵢ not msb : ∀ k {n} → Bits (k + n) → Bits k msb = take lsb : ∀ k {n} → Bits (n + k) → Bits k lsb _ {n} = drop n msb₂ : ∀ {n} → Bits (2 + n) → Bits 2 msb₂ = msb 2 lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2 lsb₂ = reverse ∘ msb 2 ∘ reverse #1 : ∀ {n} → Bits n → Fin (suc n) #1 [] = zero #1 (0₂ ∷ bs) = inject₁ (#1 bs) #1 (1₂ ∷ bs) = suc (#1 bs) #0 : ∀ {n} → Bits n → Fin (suc n) #0 = #1 ∘ map not allBitsL : ∀ n → L.List (Bits n) allBitsL _ = replicateM (toList (0₂ ∷ 1₂ ∷ [])) where open L.Monad allBits : ∀ n → Vec (Bits n) (2^ n) allBits zero = [] ∷ [] allBits (suc n) = map 0∷_ bs ++ map 1∷_ bs where bs = allBits n always : ∀ n → Bits n → Bit always _ _ = 1₂ never : ∀ n → Bits n → Bit never _ _ = 0₂ _∨°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit _∨°_ f g x = f x ∨ g x _∧°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit _∧°_ f g x = f x ∧ g x not° : ∀ {n} (f : Bits n → Bit) → Bits n → Bit not° f = not ∘ f view∷ : ∀ {n a b} {A : ★ a} {B : ★ b} → (A → Vec A n → B) → Vec A (suc n) → B view∷ f (x ∷ xs) = f x xs sucBCarry : ∀ {n} → Bits n → Bits (1 + n) sucBCarry [] = 0₂ ∷ [] sucBCarry (0₂ ∷ xs) = 0₂ ∷ sucBCarry xs sucBCarry (1₂ ∷ xs) = view∷ (λ x xs → x ∷ not x ∷ xs) (sucBCarry xs) sucB : ∀ {n} → Bits n → Bits n sucB = tail ∘ sucBCarry --_[mod_] : ℕ → ℕ → ★₀ --a [mod b ] = DivMod' a b module ReversedBits where sucRB : ∀ {n} → Bits n → Bits n sucRB [] = [] sucRB (0₂ ∷ xs) = 1₂ ∷ xs sucRB (1₂ ∷ xs) = 0₂ ∷ sucRB xs toFin : ∀ {n} → Bits n → Fin (2^ n) toFin [] = zero toFin (0₂ ∷ xs) = inject+ _ (toFin xs) toFin {suc n} (1₂ ∷ xs) = raise (2^ n) (toFin xs) Bits▹ℕ : ∀ {n} → Bits n → ℕ Bits▹ℕ [] = zero Bits▹ℕ (0₂ ∷ xs) = Bits▹ℕ xs Bits▹ℕ {suc n} (1₂ ∷ xs) = 2^ n + Bits▹ℕ xs ℕ▹Bits : ∀ {n} → ℕ → Bits n ℕ▹Bits {zero} _ = [] ℕ▹Bits {suc n} x = [0: 0∷ ℕ▹Bits x 1: 1∷ ℕ▹Bits (x ∸ 2^ n) ]′ (2^ n ℕ<= x) ℕ▹Bits′ : ∀ {n} → ℕ → Bits n ℕ▹Bits′ = fold 0ⁿ sucB fromFin : ∀ {n} → Fin (2^ n) → Bits n fromFin = ℕ▹Bits ∘ Fin▹ℕ lookupTbl : ∀ {n a} {A : ★ a} → Bits n → Vec A (2^ n) → A lookupTbl [] (x ∷ []) = x lookupTbl (0₂ ∷ key) tbl = lookupTbl key (take _ tbl) lookupTbl {suc n} (1₂ ∷ key) tbl = lookupTbl key (drop (2^ n) tbl) funFromTbl : ∀ {n a} {A : ★ a} → Vec A (2^ n) → (Bits n → A) funFromTbl = flip lookupTbl tblFromFun : ∀ {n a} {A : ★ a} → (Bits n → A) → Vec A (2^ n) -- tblFromFun f = tabulate (f ∘ fromFin) tblFromFun {zero} f = f [] ∷ [] tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_) ++ tblFromFun {n} (f ∘ 1∷_)
{-# OPTIONS --without-K #-} module Data.Bits where open import Algebra open import Level.NP open import Type hiding (★) open import Data.Nat.NP hiding (_==_) renaming (_<=_ to _ℕ<=_) open import Data.Bit using (Bit) open import Data.Two renaming (_==_ to _==ᵇ_) open import Data.Fin.NP using (Fin; zero; suc; inject₁; inject+; raise; Fin▹ℕ) open import Data.Vec.NP open import Function.NP import Data.List.NP as L -- Re-export some vector functions, maybe they should be given -- less generic types. open Data.Vec.NP public using ([]; _∷_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; onᵢ) Bits : ℕ → ★₀ Bits = Vec Bit _→ᵇ_ : ℕ → ℕ → ★₀ i →ᵇ o = Bits i → Bits o 0ⁿ : ∀ {n} → Bits n 0ⁿ = replicate 0₂ -- Notice that all empty vectors are the same, hence 0ⁿ {0} ≡ 1ⁿ {0} 1ⁿ : ∀ {n} → Bits n 1ⁿ = replicate 1₂ pattern 0∷_ xs = 0₂ ∷ xs pattern 1∷_ xs = 1₂ ∷ xs {- 0∷_ : ∀ {n} → Bits n → Bits (suc n) 0∷ xs = 0₂ ∷ xs -- can't we make these pattern aliases? 1∷_ : ∀ {n} → Bits n → Bits (suc n) 1∷ xs = 1₂ ∷ xs -} _!_ : ∀ {a n} {A : ★ a} → Vec A n → Fin n → A _!_ = flip lookup -- see Data.Bits.Properties _==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bit [] == [] = 1₂ (b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁ _<=_ : ∀ {n} (xs ys : Bits n) → Bit [] <= [] = 1₂ (1₂ ∷ xs) <= (0₂ ∷ ys) = 0₂ (0₂ ∷ xs) <= (1₂ ∷ ys) = 1₂ (_ ∷ xs) <= (_ ∷ ys) = xs <= ys infixr 5 _⊕_ _⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n _⊕_ = zipWith _xor_ ⊕-group : ℕ → Group ₀ ₀ ⊕-group = LiftGroup.group Xor°.+-group module ⊕-Group (n : ℕ) = Group (⊕-group n) module ⊕-Monoid (n : ℕ) = Monoid (⊕-Group.monoid n) -- Negate all bits, i.e. "xor"ing them by one. vnot : ∀ {n} → Endo (Bits n) vnot = _⊕_ 1ⁿ -- Negate the i-th bit. notᵢ : ∀ {n} (i : Fin n) → Bits n → Bits n notᵢ = onᵢ not msb : ∀ k {n} → Bits (k + n) → Bits k msb = take lsb : ∀ k {n} → Bits (n + k) → Bits k lsb _ {n} = drop n msb₂ : ∀ {n} → Bits (2 + n) → Bits 2 msb₂ = msb 2 lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2 lsb₂ = reverse ∘ msb 2 ∘ reverse #1 : ∀ {n} → Bits n → Fin (suc n) #1 [] = zero #1 (0∷ bs) = inject₁ (#1 bs) #1 (1∷ bs) = suc (#1 bs) #0 : ∀ {n} → Bits n → Fin (suc n) #0 = #1 ∘ map not allBitsL : ∀ n → L.List (Bits n) allBitsL _ = replicateM (toList (0∷ 1∷ [])) where open L.Monad allBits : ∀ n → Vec (Bits n) (2^ n) allBits zero = [] ∷ [] allBits (suc n) = map 0∷_ bs ++ map 1∷_ bs where bs = allBits n always : ∀ n → Bits n → Bit always _ _ = 1₂ never : ∀ n → Bits n → Bit never _ _ = 0₂ _∨°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit _∨°_ f g x = f x ∨ g x _∧°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit _∧°_ f g x = f x ∧ g x not° : ∀ {n} (f : Bits n → Bit) → Bits n → Bit not° f = not ∘ f view∷ : ∀ {n a b} {A : ★ a} {B : ★ b} → (A → Vec A n → B) → Vec A (suc n) → B view∷ f (x ∷ xs) = f x xs sucBCarry : ∀ {n} → Bits n → Bits (1 + n) sucBCarry [] = 0∷ [] sucBCarry (0∷ xs) = 0∷ sucBCarry xs sucBCarry (1∷ xs) = view∷ (λ x xs → x ∷ not x ∷ xs) (sucBCarry xs) sucB : ∀ {n} → Bits n → Bits n sucB = tail ∘ sucBCarry --_[mod_] : ℕ → ℕ → ★₀ --a [mod b ] = DivMod' a b module ReversedBits where sucRB : ∀ {n} → Bits n → Bits n sucRB [] = [] sucRB (0∷ xs) = 1∷ xs sucRB (1∷ xs) = 0∷ sucRB xs toFin : ∀ {n} → Bits n → Fin (2^ n) toFin [] = zero toFin (0∷ xs) = inject+ _ (toFin xs) toFin {suc n} (1∷ xs) = raise (2^ n) (toFin xs) Bits▹ℕ : ∀ {n} → Bits n → ℕ Bits▹ℕ [] = zero Bits▹ℕ (0∷ xs) = Bits▹ℕ xs Bits▹ℕ {suc n} (1∷ xs) = 2^ n + Bits▹ℕ xs ℕ▹Bits : ∀ {n} → ℕ → Bits n ℕ▹Bits {zero} _ = [] ℕ▹Bits {suc n} x = [0: 0∷ ℕ▹Bits x 1: 1∷ ℕ▹Bits (x ∸ 2^ n) ]′ (2^ n ℕ<= x) ℕ▹Bits′ : ∀ {n} → ℕ → Bits n ℕ▹Bits′ = fold 0ⁿ sucB fromFin : ∀ {n} → Fin (2^ n) → Bits n fromFin = ℕ▹Bits ∘ Fin▹ℕ lookupTbl : ∀ {n a} {A : ★ a} → Bits n → Vec A (2^ n) → A lookupTbl [] = head lookupTbl (0∷ key) = lookupTbl key ∘ take _ lookupTbl {suc n} (1∷ key) = lookupTbl key ∘ drop (2^ n) funFromTbl : ∀ {n a} {A : ★ a} → Vec A (2^ n) → (Bits n → A) funFromTbl = flip lookupTbl tblFromFun : ∀ {n a} {A : ★ a} → (Bits n → A) → Vec A (2^ n) -- tblFromFun f = tabulate (f ∘ fromFin) tblFromFun {zero} f = f [] ∷ [] tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_) ++ tblFromFun {n} (f ∘ 1∷_)
use patterns 0∷ and 1∷
Bits: use patterns 0∷ and 1∷
Agda
bsd-3-clause
crypto-agda/agda-nplib
4cbed45974f3385d5c182dddefd1bb738b95c793
ChangeContexts.agda
ChangeContexts.agda
module ChangeContexts where open import IlcModel open import Changes open import meaning -- TYPING CONTEXTS, VARIABLES and WEAKENING open import binding Type ⟦_⟧Type -- CHANGE CONTEXTS Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = Δ-Type τ • τ • Δ-Context Γ update : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧ update {∅} ∅ = ∅ update {τ • Γ} (dv • v • ρ) = apply dv v • update ρ ignore : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧ ignore {∅} ∅ = ∅ ignore {τ • Γ} (dv • v • ρ) = v • ignore ρ Δ-Context′ : (Γ : Context) → Prefix Γ → Context Δ-Context′ Γ ∅ = Δ-Context Γ Δ-Context′ (.τ • Γ) (τ • Γ′) = τ • Δ-Context′ Γ Γ′ update′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧ update′ ∅ ρ = update ρ update′ (τ • Γ′) (v • ρ) = v • update′ Γ′ ρ ignore′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧ ignore′ ∅ ρ = ignore ρ ignore′ (τ • Γ′) (v • ρ) = v • ignore′ Γ′ ρ
module ChangeContexts where open import IlcModel open import Changes open import meaning -- TYPING CONTEXTS, VARIABLES and WEAKENING open import binding Type ⟦_⟧Type -- CHANGE CONTEXTS Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = Δ-Type τ • τ • Δ-Context Γ update : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧ update {∅} ∅ = ∅ update {τ • Γ} (dv • v • ρ) = apply dv v • update ρ ignore : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧ ignore {∅} ∅ = ∅ ignore {τ • Γ} (dv • v • ρ) = v • ignore ρ -- Δ-Context′: behaves like Δ-Context, but has an extra argument Γ′, a -- prefix of its first argument which should not be touched. Δ-Context′ : (Γ : Context) → Prefix Γ → Context Δ-Context′ Γ ∅ = Δ-Context Γ Δ-Context′ (.τ • Γ) (τ • Γ′) = τ • Δ-Context′ Γ Γ′ update′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧ update′ ∅ ρ = update ρ update′ (τ • Γ′) (v • ρ) = v • update′ Γ′ ρ ignore′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧ ignore′ ∅ ρ = ignore ρ ignore′ (τ • Γ′) (v • ρ) = v • ignore′ Γ′ ρ
document a function
agda: document a function Old-commit-hash: c0550a6f2e84b20bc63a5c0f91a168988125b117
Agda
mit
inc-lc/ilc-agda
70907b2ea28e2d6559f103705f74a8fefbca6f63
Parametric/Change/Validity.agda
Parametric/Change/Validity.agda
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base -- Changes for Calculus Popl14 -- -- Contents -- - Mutually recursive concepts: ΔVal, validity. -- Under module Syntax, the corresponding concepts of -- ΔType and ΔContext reside in separate files. -- Now they have to be together due to mutual recursiveness. -- - `diff` and `apply` on semantic values of changes: -- they have to be here as well because they are mutually -- recursive with validity. -- - The lemma diff-is-valid: it has to be here because it is -- mutually recursive with `apply` -- - The lemma apply-diff: it is mutually recursive with `apply` -- and `diff` open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality open import Data.Product hiding (map) import Structure.Tuples as Tuples open Tuples import Base.Data.DependentList as DependentList open DependentList open import Relation.Unary using (_⊆_) record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field Change-base : (ι : Base) → ⟦ ι ⟧Base → Set apply-change-base : ∀ ι → (v : ⟦ ι ⟧Base) → Change-base ι v → ⟦ ι ⟧Base diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u --------------- -- Interface -- --------------- Change : (τ : Type) → ⟦ τ ⟧ → Set nil-change : ∀ τ v → Change τ v apply-change : ∀ τ → (v : ⟦ τ ⟧) (dv : Change τ v) → ⟦ τ ⟧ diff-change : ∀ τ → (u v : ⟦ τ ⟧) → Change τ v infixl 6 apply-change diff-change -- as with + - in GHC.Num syntax apply-change τ v dv = v ⊞₍ τ ₎ dv syntax diff-change τ u v = u ⊟₍ τ ₎ v -- Lemma apply-diff v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u -------------------- -- Implementation -- -------------------- -- (Change τ) is the set of changes of type τ. This set is -- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In -- particular, (Change (σ ⇒ τ)) is a function that accepts only -- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid -- changes. -- -- Change τ is the target of the denotational specification ⟦_⟧Δ. -- Detailed motivation: -- -- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex -- Change : Type → Set Change (base ι) v = Change-base ι v Change (σ ⇒ τ) f = Pair (∀ v → Change σ v → Change τ (f v)) (λ Δf → ∀ v dv → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ Δf (v ⊞₍ σ ₎ dv) (nil-change σ (v ⊞₍ σ ₎ dv)) ≡ f v ⊞₍ τ ₎ Δf v dv) before : ∀ {τ v} → Change τ v → ⟦ τ ⟧ before {τ} {v} _ = v after : ∀ {τ v} → Change τ v → ⟦ τ ⟧ after {τ} {v} dv = v ⊞₍ τ ₎ dv open Pair public using () renaming ( cdr to is-valid ; car to call-change ) nil-change τ v = diff-change τ v v -- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ apply-change (base ι) n Δn = apply-change-base ι n Δn apply-change (σ ⇒ τ) f Δf = λ v → f v ⊞₍ τ ₎ call-change Δf v (nil-change σ v) -- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ diff-change (base ι) m n = diff-change-base ι m n diff-change (σ ⇒ τ) g f = cons (λ v dv → g (after {σ} dv) ⊟₍ τ ₎ f v) (λ v dv → begin f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g ((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv)) ≡⟨ cong (λ □ → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g □ ⊟₍ τ ₎ (f (v ⊞₍ σ ₎ dv)))) (v+[u-v]=u {σ} {v ⊞₍ σ ₎ dv} {v ⊞₍ σ ₎ dv}) ⟩ f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv)) ≡⟨ v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f (v ⊞₍ σ ₎ dv)} ⟩ g (v ⊞₍ σ ₎ dv) ≡⟨ sym (v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f v} ) ⟩ f v ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v) ∎) where open ≡-Reasoning -- call this lemma "replace"? -- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v} v+[u-v]=u {σ ⇒ τ} {u} {v} = ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w → begin (apply-change (σ ⇒ τ) v (diff-change (σ ⇒ τ) u v)) w ≡⟨ refl ⟩ v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w) ≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩ v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w) ≡⟨ v+[u-v]=u {τ} ⟩ u w ∎) where open ≡-Reasoning -- syntactic sugar for implicit indices infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num _⊞_ : ∀ {τ} → (v : ⟦ τ ⟧) → Change τ v → ⟦ τ ⟧ _⊞_ {τ} v dv = v ⊞₍ τ ₎ dv _⊟_ : ∀ {τ} → (u v : ⟦ τ ⟧) → Change τ v _⊟_ {τ} u v = u ⊟₍ τ ₎ v ------------------ -- Environments -- ------------------ open DependentList public using (∅; _•_) open Tuples public using (cons) data ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set where ∅ : ΔEnv ∅ ∅ _•_ : ∀ {τ Γ v ρ} → (dv : Change τ v) → (dρ : ΔEnv Γ ρ) → ΔEnv (τ • Γ) (v • ρ) ignore : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧ ignore {Γ} {ρ} _ = ρ update : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧ update ∅ = ∅ update {τ • Γ} (dv • dρ) = after {τ} dv • update dρ
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base -- Changes for Calculus Popl14 -- -- Contents -- - Mutually recursive concepts: ΔVal, validity. -- Under module Syntax, the corresponding concepts of -- ΔType and ΔContext reside in separate files. -- Now they have to be together due to mutual recursiveness. -- - `diff` and `apply` on semantic values of changes: -- they have to be here as well because they are mutually -- recursive with validity. -- - The lemma diff-is-valid: it has to be here because it is -- mutually recursive with `apply` -- - The lemma apply-diff: it is mutually recursive with `apply` -- and `diff` open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality open import Data.Product hiding (map) import Structure.Tuples as Tuples open Tuples import Base.Data.DependentList as DependentList open DependentList open import Relation.Unary using (_⊆_) record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field Change-base : (ι : Base) → ⟦ ι ⟧Base → Set apply-change-base : ∀ ι → (v : ⟦ ι ⟧Base) → Change-base ι v → ⟦ ι ⟧Base diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u --------------- -- Interface -- --------------- Change : (τ : Type) → ⟦ τ ⟧ → Set nil-change : ∀ τ v → Change τ v apply-change : ∀ τ → (v : ⟦ τ ⟧) (dv : Change τ v) → ⟦ τ ⟧ diff-change : ∀ τ → (u v : ⟦ τ ⟧) → Change τ v infixl 6 apply-change diff-change -- as with + - in GHC.Num syntax apply-change τ v dv = v ⊞₍ τ ₎ dv syntax diff-change τ u v = u ⊟₍ τ ₎ v -- Lemma apply-diff v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u -------------------- -- Implementation -- -------------------- -- (Change τ) is the set of changes of type τ. This set is -- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In -- particular, (Change (σ ⇒ τ)) is a function that accepts only -- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid -- changes. -- -- Change τ is the target of the denotational specification ⟦_⟧Δ. -- Detailed motivation: -- -- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex -- Change : Type → Set Change (base ι) v = Change-base ι v Change (σ ⇒ τ) f = Pair (∀ v → Change σ v → Change τ (f v)) (λ Δf → ∀ v dv → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ Δf (v ⊞₍ σ ₎ dv) (nil-change σ (v ⊞₍ σ ₎ dv)) ≡ f v ⊞₍ τ ₎ Δf v dv) open Pair public using () renaming ( cdr to is-valid ; car to call-change ) nil-change τ v = diff-change τ v v -- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ apply-change (base ι) n Δn = apply-change-base ι n Δn apply-change (σ ⇒ τ) f Δf = λ v → f v ⊞₍ τ ₎ call-change Δf v (nil-change σ v) -- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ diff-change (base ι) m n = diff-change-base ι m n diff-change (σ ⇒ τ) g f = cons (λ v dv → g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v) (λ v dv → begin f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g ((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv)) ≡⟨ cong (λ □ → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g □ ⊟₍ τ ₎ (f (v ⊞₍ σ ₎ dv)))) (v+[u-v]=u {σ} {v ⊞₍ σ ₎ dv} {v ⊞₍ σ ₎ dv}) ⟩ f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv)) ≡⟨ v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f (v ⊞₍ σ ₎ dv)} ⟩ g (v ⊞₍ σ ₎ dv) ≡⟨ sym (v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f v} ) ⟩ f v ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v) ∎) where open ≡-Reasoning -- call this lemma "replace"? -- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v} v+[u-v]=u {σ ⇒ τ} {u} {v} = ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w → begin (apply-change (σ ⇒ τ) v (diff-change (σ ⇒ τ) u v)) w ≡⟨ refl ⟩ v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w) ≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩ v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w) ≡⟨ v+[u-v]=u {τ} ⟩ u w ∎) where open ≡-Reasoning -- syntactic sugar for implicit indices infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num _⊞_ : ∀ {τ} → (v : ⟦ τ ⟧) → Change τ v → ⟦ τ ⟧ _⊞_ {τ} v dv = v ⊞₍ τ ₎ dv _⊟_ : ∀ {τ} → (u v : ⟦ τ ⟧) → Change τ v _⊟_ {τ} u v = u ⊟₍ τ ₎ v -- abbrevitations before : ∀ {τ v} → Change τ v → ⟦ τ ⟧ before {τ} {v} _ = v after : ∀ {τ v} → Change τ v → ⟦ τ ⟧ after {τ} {v} dv = v ⊞₍ τ ₎ dv ------------------ -- Environments -- ------------------ open DependentList public using (∅; _•_) open Tuples public using (cons) data ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set where ∅ : ΔEnv ∅ ∅ _•_ : ∀ {τ Γ v ρ} → (dv : Change τ v) → (dρ : ΔEnv Γ ρ) → ΔEnv (τ • Γ) (v • ρ) ignore : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧ ignore {Γ} {ρ} _ = ρ update : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧ update ∅ = ∅ update {τ • Γ} (dv • dρ) = after {τ} dv • update dρ
Move before and after out of the mutually dependent block.
Move before and after out of the mutually dependent block. Old-commit-hash: 1d7a74aa7d62457de4936eae9faaeb605d739bf3
Agda
mit
inc-lc/ilc-agda
6f7fde98554f01770bfdbd43711ab8e45eab8b9b
Control/Protocol.agda
Control/Protocol.agda
{-# OPTIONS --without-K #-} open import Function.NP open import Type open import Level.NP open import Data.Product.NP using (Σ; _×_; _,_) renaming (proj₁ to fst) open import Data.One using (𝟙) open import Relation.Binary.PropositionalEquality.NP using (_≡_; !_; _∙_; refl; ap; coe; coe!) open import Function.Extensionality open import HoTT open import Data.ShapePolymorphism open Equivalences module Control.Protocol where data InOut : ★ where In Out : InOut dualᴵᴼ : InOut → InOut dualᴵᴼ In = Out dualᴵᴼ Out = In dualᴵᴼ-involutive : ∀ io → dualᴵᴼ (dualᴵᴼ io) ≡ io dualᴵᴼ-involutive In = refl dualᴵᴼ-involutive Out = refl dualᴵᴼ-equiv : Is-equiv dualᴵᴼ dualᴵᴼ-equiv = self-inv-is-equiv dualᴵᴼ-involutive dualᴵᴼ-inj : ∀ {x y} → dualᴵᴼ x ≡ dualᴵᴼ y → x ≡ y dualᴵᴼ-inj = Is-equiv.injective dualᴵᴼ-equiv {- module UniversalProtocols ℓ {U : ★_(ₛ ℓ)}(U⟦_⟧ : U → ★_ ℓ) where -} module _ ℓ where U = ★_ ℓ U⟦_⟧ = id data Proto_ : ★_(ₛ ℓ) where end : Proto_ com : (io : InOut){M : U}(P : U⟦ M ⟧ → Proto_) → Proto_ {- module U★ ℓ = UniversalProtocols ℓ {★_ ℓ} id open U★ -} Proto : ★₁ Proto = Proto_ ₀ Proto₀ = Proto Proto₁ = Proto_ ₁ pattern recv P = com In P pattern send P = com Out P module _ {{_ : FunExt}} where com= : ∀ {io₀ io₁}(io= : io₀ ≡ io₁) {M₀ M₁}(M= : M₀ ≡ M₁) {P₀ : M₀ → Proto}{P₁ : M₁ → Proto}(P= : ∀ m₀ → P₀ m₀ ≡ P₁ (coe M= m₀)) → com io₀ P₀ ≡ com io₁ P₁ com= refl refl P= = ap (com _) (λ= P=) module _ {io₀ io₁}(io= : io₀ ≡ io₁) {M₀ M₁}(M≃ : M₀ ≃ M₁) {P₀ : M₀ → Proto}{P₁ : M₁ → Proto} (P= : ∀ m₀ → P₀ m₀ ≡ P₁ (–> M≃ m₀)) {{_ : UA}} where com≃ : com io₀ P₀ ≡ com io₁ P₁ com≃ = com= io= (ua M≃) λ m → P= m ∙ ap P₁ (! coe-β M≃ m) module _ io {M N}(P : M × N → Proto) where com₂ : Proto com₂ = com io λ m → com io λ n → P (m , n) {- Proving this would be awesome... module _ io {M₀ M₁ N₀ N₁ : ★} (M×N≃ : (M₀ × N₀) ≃ (M₁ × N₁)) {P₀ : M₀ × N₀ → Proto}{P₁ : M₁ × N₁ → Proto} (P= : ∀ m,n₀ → P₀ m,n₀ ≡ P₁ (–> M×N≃ m,n₀)) {{_ : UA}} where com₂≃ : com₂ io P₀ ≡ com₂ io P₁ com₂≃ = {!com=!} -} send= = com= {Out} refl send≃ = com≃ {Out} refl recv= = com= {In} refl recv≃ = com≃ {In} refl com=′ : ∀ io {M}{P₀ P₁ : M → Proto}(P= : ∀ m → P₀ m ≡ P₁ m) → com io P₀ ≡ com io P₁ com=′ io = com= refl refl send=′ : ∀ {M}{P₀ P₁ : M → Proto}(P= : ∀ m → P₀ m ≡ P₁ m) → send P₀ ≡ send P₁ send=′ = send= refl recv=′ : ∀ {M}{P₀ P₁ : M → Proto}(P= : ∀ m → P₀ m ≡ P₁ m) → recv P₀ ≡ recv P₁ recv=′ = recv= refl pattern recvE M P = com In {M} P pattern sendE M P = com Out {M} P recv☐ : {M : ★}(P : ..(_ : M) → Proto) → Proto recv☐ P = recv (λ { [ m ] → P m }) send☐ : {M : ★}(P : ..(_ : M) → Proto) → Proto send☐ P = send (λ { [ m ] → P m }) {- dual : Proto → Proto dual end = end dual (send P) = recv λ m → dual (P m) dual (recv P) = send λ m → dual (P m) -} dual : Proto → Proto dual end = end dual (com io P) = com (dualᴵᴼ io) λ m → dual (P m) source-of : Proto → Proto source-of end = end source-of (com _ P) = send λ m → source-of (P m) sink-of : Proto → Proto sink-of = dual ∘ source-of data IsSource : Proto → ★₁ where end : IsSource end send' : ∀ {M P} (PT : (m : M) → IsSource (P m)) → IsSource (send P) data IsSink : Proto → ★₁ where end : IsSink end recv' : ∀ {M P} (PT : (m : M) → IsSink (P m)) → IsSink (recv P) data Proto☐ : Proto → ★₁ where end : Proto☐ end com : ∀ io {M P} (P☐ : ∀ (m : ☐ M) → Proto☐ (P m)) → Proto☐ (com io P) record End_ ℓ : ★_ ℓ where constructor end End : ∀ {ℓ} → ★_ ℓ End = End_ _ End-equiv : End ≃ 𝟙 End-equiv = equiv {₀} _ _ (λ _ → refl) (λ _ → refl) End-uniq : {{_ : UA}} → End ≡ 𝟙 End-uniq = ua End-equiv ⟦_⟧ᴵᴼ : InOut → ∀{ℓ}(M : ★_ ℓ)(P : M → ★_ ℓ) → ★_ ℓ ⟦_⟧ᴵᴼ In = Π ⟦_⟧ᴵᴼ Out = Σ ⟦_⟧ : ∀ {ℓ} → Proto_ ℓ → ★_ ℓ ⟦ end ⟧ = End ⟦ com io P ⟧ = ⟦ io ⟧ᴵᴼ _ λ m → ⟦ P m ⟧ ⟦_⊥⟧ : Proto → ★ ⟦ P ⊥⟧ = ⟦ dual P ⟧ Log : Proto → ★ Log P = ⟦ source-of P ⟧ Sink : Proto → ★ Sink P = ⟦ sink-of P ⟧ sink : ∀ P → Sink P sink end = _ sink (com _ P) x = sink (P x) telecom : ∀ P → ⟦ P ⟧ → ⟦ P ⊥⟧ → Log P telecom end _ _ = _ telecom (recv P) p (m , q) = m , telecom (P m) (p m) q telecom (send P) (m , p) q = m , telecom (P m) p (q m) send′ : ★ → Proto → Proto send′ M P = send λ (_ : M) → P recv′ : ★ → Proto → Proto recv′ M P = recv λ (_ : M) → P module _ {{_ : FunExt}} where dual-involutive : ∀ P → dual (dual P) ≡ P dual-involutive end = refl dual-involutive (com io P) = com= (dualᴵᴼ-involutive io) refl λ m → dual-involutive (P m) dual-equiv : Is-equiv dual dual-equiv = self-inv-is-equiv dual-involutive dual-inj : ∀ {P Q} → dual P ≡ dual Q → P ≡ Q dual-inj = Is-equiv.injective dual-equiv source-of-idempotent : ∀ P → source-of (source-of P) ≡ source-of P source-of-idempotent end = refl source-of-idempotent (com _ P) = com= refl refl λ m → source-of-idempotent (P m) source-of-dual-oblivious : ∀ P → source-of (dual P) ≡ source-of P source-of-dual-oblivious end = refl source-of-dual-oblivious (com _ P) = com= refl refl λ m → source-of-dual-oblivious (P m) module _ {{_ : FunExt}} where sink-contr : ∀ P s → sink P ≡ s sink-contr end s = refl sink-contr (com _ P) s = λ= λ m → sink-contr (P m) (s m) Sink-is-contr : ∀ P → Is-contr (Sink P) Sink-is-contr P = sink P , sink-contr P 𝟙≃Sink : ∀ P → 𝟙 ≃ Sink P 𝟙≃Sink P = Is-contr-to-Is-equiv.𝟙≃ (Sink-is-contr P) dual-Log : ∀ P → Log (dual P) ≡ Log P dual-Log = ap ⟦_⟧ ∘ source-of-dual-oblivious
module Control.Protocol where open import Control.Protocol.InOut public open import Control.Protocol.End public open import Control.Protocol.Core public
Split Control.Protocol
Split Control.Protocol
Agda
bsd-3-clause
crypto-agda/protocols
81eecc922c3dc4fc227770d378988bd4c8881616
ZK/GroupHom.agda
ZK/GroupHom.agda
{-# OPTIONS --without-K #-} open import Type using (Type) open import Type.Eq using (Eq?; _==_; ≡⇒==; ==⇒≡) open import Function using (case_of_) open import Data.Sum renaming (inj₁ to inl; inj₂ to inr) open import Data.Product renaming (proj₁ to fst; proj₂ to snd) open import Relation.Binary.PropositionalEquality.Base using (_≡_; _≢_; idp; ap; ap₂; !_; _∙_; module ≡-Reasoning) open import Algebra.Group open import Algebra.Group.Homomorphism import ZK.SigmaProtocol.KnownStatement module ZK.GroupHom {G+ G* : Type} (grp-G+ : Group G+) (grp-G* : Group G*) {{eq?-G* : Eq? G*}} (open Additive-Group grp-G+ hiding (_⊗_)) (open Multiplicative-Group grp-G* hiding (_^_)) {C : Type} -- e.g. C ⊆ ℕ (_>_ : C → C → Type) (swap? : {c₀ c₁ : C} → c₀ ≢ c₁ → (c₀ > c₁) ⊎ (c₁ > c₀)) (_⊗_ : G+ → C → G+) (_^_ : G* → C → G*) (_−ᶜ_ : C → C → C) (1/ : C → C) (φ : G+ → G*) -- For instance φ(x) = g^x (φ-hom : GroupHomomorphism grp-G+ grp-G* φ) (φ-hom-iterated : ∀ {x c} → φ (x ⊗ c) ≡ φ x ^ c) (Y : G*) (^-−ᶜ : ∀ {c₀ c₁}(c> : c₀ > c₁) → Y ^(c₀ −ᶜ c₁) ≡ (Y ^ c₀) / (Y ^ c₁)) (^-^-1/ : ∀ {c₀ c₁}(c> : c₀ > c₁) → (Y ^(c₀ −ᶜ c₁))^(1/(c₀ −ᶜ c₁)) ≡ Y) where Commitment = G* Challenge = C Response = G+ Witness = G+ Randomness = G+ _∈y : Witness → Type x ∈y = φ x ≡ Y open ZK.SigmaProtocol.KnownStatement Commitment Challenge Response Randomness Witness _∈y public prover : Prover prover a x = φ a , response where response : Challenge → Response response c = (x ⊗ c) + a verifier : Verifier verifier (mk A c s) = φ s == ((Y ^ c) * A) -- We still call it Schnorr since essentially not much changed. -- The scheme abstracts away g^ as an homomorphism called φ -- and one get to distinguish between the types C vs G+ and -- their operations _⊗_ vs _*_. Schnorr : Σ-Protocol Schnorr = (prover , verifier) -- The simulator shows why it is so important for the -- challenge to be picked once the commitment is known. -- To fake a transcript, the challenge and response can -- be arbitrarily chosen. However to be indistinguishable -- from a valid proof they need to be picked at random. simulator : Simulator simulator c s = A where -- Compute A, such that the verifier accepts! A = (Y ^ c)⁻¹ * φ s swap-t²? : Transcript²[ _≢_ ] verifier → Transcript²[ _>_ ] verifier swap-t²? t² = t²' module Swap? where open Transcript² t² public using (verify₀; verify₁; c₀≢c₁) renaming ( get-A to A ; get-f₀ to r₀; get-f₁ to r₁ ; get-c₀ to c₀; get-c₁ to c₁ ) t²' = case swap? c₀≢c₁ of λ { (inl c₀>c₁) → mk A c₀ c₁ c₀>c₁ r₀ r₁ verify₀ verify₁ ; (inr c₀<c₁) → mk A c₁ c₀ c₀<c₁ r₁ r₀ verify₁ verify₀ } -- The extractor shows the importance of never reusing a -- commitment. If the prover answers to two different challenges -- comming from the same commitment then the knowledge of the -- prover (the witness) can be extracted. extractor : Extractor verifier extractor t² = x' module Witness-extractor where open Transcript² (swap-t²? t²) public using (verify₀; verify₁) renaming ( get-A to A ; get-f₀ to r₀; get-f₁ to r₁ ; get-c₀ to c₀; get-c₁ to c₁ ; c₀≢c₁ to c₀>c₁ ) rd = r₀ − r₁ cd = c₀ −ᶜ c₁ 1/cd = 1/ cd x' = rd ⊗ 1/cd open ≡-Reasoning open GroupHomomorphism φ-hom correct : Correct Schnorr correct x a c w = ≡⇒== (φ((x ⊗ c) + a) ≡⟨ hom ⟩ φ(x ⊗ c) * A ≡⟨ *= φ-hom-iterated idp ⟩ (φ x ^ c) * A ≡⟨ ap (λ z → (z ^ c) * A) w ⟩ (Y ^ c) * A ∎) where A = φ a shvzk : Special-Honest-Verifier-Zero-Knowledge Schnorr shvzk = record { simulator = simulator ; correct-simulator = λ _ _ → ≡⇒== (! elim-*-!assoc= (snd ⁻¹-inverse)) } module _ (t² : Transcript² verifier) where open Witness-extractor t² φrd≡ycd : _ φrd≡ycd = φ rd ≡⟨by-definition⟩ φ (r₀ − r₁) ≡⟨ −-/ ⟩ φ r₀ / φ r₁ ≡⟨ /= (==⇒≡ verify₀) (==⇒≡ verify₁) ⟩ (Y ^ c₀ * A) / (Y ^ c₁ * A) ≡⟨ elim-*-right-/ ⟩ Y ^ c₀ / Y ^ c₁ ≡⟨ ! ^-−ᶜ c₀>c₁ ⟩ Y ^(c₀ −ᶜ c₁) ≡⟨by-definition⟩ Y ^ cd ∎ -- The extracted x is correct extractor-ok : φ x' ≡ Y extractor-ok = φ(rd ⊗ 1/cd) ≡⟨ φ-hom-iterated ⟩ φ rd ^ 1/cd ≡⟨ ap₂ _^_ φrd≡ycd idp ⟩ (Y ^ cd)^ 1/cd ≡⟨ ^-^-1/ c₀>c₁ ⟩ Y ∎ special-soundness : Special-Soundness Schnorr special-soundness = record { extractor = extractor ; extract-valid-witness = extractor-ok } special-Σ-protocol : Special-Σ-Protocol special-Σ-protocol = record { Σ-protocol = Schnorr ; correct = correct ; shvzk = shvzk ; ssound = special-soundness } -- -} -- -} -- -} -- -}
{-# OPTIONS --without-K #-} open import Type using (Type) open import Type.Eq using (Eq?; _==_; ≡⇒==; ==⇒≡) open import Function using (case_of_) open import Data.Sum renaming (inj₁ to inl; inj₂ to inr) open import Data.Product renaming (proj₁ to fst; proj₂ to snd) open import Relation.Binary.PropositionalEquality.Base using (_≡_; _≢_; idp; ap; ap₂; !_; _∙_; module ≡-Reasoning) open import Algebra.Group open import Algebra.Group.Homomorphism import ZK.SigmaProtocol.KnownStatement module ZK.GroupHom {G+ G* : Type} (grp-G+ : Group G+) (grp-G* : Group G*) {{eq?-G* : Eq? G*}} (open Additive-Group grp-G+ hiding (_⊗_)) (open Multiplicative-Group grp-G* hiding (_^_)) {C : Type} -- e.g. C ⊆ ℕ (_>_ : C → C → Type) (swap? : {c₀ c₁ : C} → c₀ ≢ c₁ → (c₀ > c₁) ⊎ (c₁ > c₀)) (_⊗_ : G+ → C → G+) (_^_ : G* → C → G*) (_−ᶜ_ : C → C → C) (1/ : C → C) (φ : G+ → G*) -- For instance φ(x) = g^x (φ-hom : GroupHomomorphism grp-G+ grp-G* φ) (φ-hom-iterated : ∀ {x c} → φ (x ⊗ c) ≡ φ x ^ c) (Y : G*) (^-−ᶜ : ∀ {c₀ c₁}(c> : c₀ > c₁) → Y ^(c₀ −ᶜ c₁) ≡ (Y ^ c₀) / (Y ^ c₁)) (^-^-1/ : ∀ {c₀ c₁}(c> : c₀ > c₁) → (Y ^(c₀ −ᶜ c₁))^(1/(c₀ −ᶜ c₁)) ≡ Y) where Commitment = G* Challenge = C Response = G+ Witness = G+ Randomness = G+ _∈y : Witness → Type x ∈y = φ x ≡ Y open ZK.SigmaProtocol.KnownStatement Commitment Challenge Response Randomness Witness _∈y public prover : Prover prover a x = φ a , response where response : Challenge → Response response c = (x ⊗ c) + a verifier' : (A : Commitment)(c : Challenge)(s : Response) → _ verifier' A c s = φ s == ((Y ^ c) * A) verifier : Verifier verifier (mk A c s) = verifier' A c s -- We still call it Schnorr since essentially not much changed. -- The scheme abstracts away g^ as an homomorphism called φ -- and one get to distinguish between the types C vs G+ and -- their operations _⊗_ vs _*_. Schnorr : Σ-Protocol Schnorr = (prover , verifier) -- The simulator shows why it is so important for the -- challenge to be picked once the commitment is known. -- To fake a transcript, the challenge and response can -- be arbitrarily chosen. However to be indistinguishable -- from a valid proof they need to be picked at random. simulator : Simulator simulator c s = A where -- Compute A, such that the verifier accepts! A = (Y ^ c)⁻¹ * φ s swap-t²? : Transcript²[ _≢_ ] verifier → Transcript²[ _>_ ] verifier swap-t²? t² = t²' module Swap? where open Transcript² t² public using (verify₀; verify₁; c₀≢c₁) renaming ( get-A to A ; get-f₀ to r₀; get-f₁ to r₁ ; get-c₀ to c₀; get-c₁ to c₁ ) t²' = case swap? c₀≢c₁ of λ { (inl c₀>c₁) → mk A c₀ c₁ c₀>c₁ r₀ r₁ verify₀ verify₁ ; (inr c₀<c₁) → mk A c₁ c₀ c₀<c₁ r₁ r₀ verify₁ verify₀ } -- The extractor shows the importance of never reusing a -- commitment. If the prover answers to two different challenges -- comming from the same commitment then the knowledge of the -- prover (the witness) can be extracted. extractor : Extractor verifier extractor t² = x' module Witness-extractor where open Transcript² (swap-t²? t²) public using (verify₀; verify₁) renaming ( get-A to A ; get-f₀ to r₀; get-f₁ to r₁ ; get-c₀ to c₀; get-c₁ to c₁ ; c₀≢c₁ to c₀>c₁ ) rd = r₀ − r₁ cd = c₀ −ᶜ c₁ 1/cd = 1/ cd x' = rd ⊗ 1/cd open ≡-Reasoning open GroupHomomorphism φ-hom correct : Correct Schnorr correct x a c w = ≡⇒== (φ((x ⊗ c) + a) ≡⟨ hom ⟩ φ(x ⊗ c) * A ≡⟨ *= φ-hom-iterated idp ⟩ (φ x ^ c) * A ≡⟨ ap (λ z → (z ^ c) * A) w ⟩ (Y ^ c) * A ∎) where A = φ a shvzk : Special-Honest-Verifier-Zero-Knowledge Schnorr shvzk = record { simulator = simulator ; correct-simulator = λ _ _ → ≡⇒== (! elim-*-!assoc= (snd ⁻¹-inverse)) } module _ (t² : Transcript² verifier) where open Witness-extractor t² φrd≡ycd : _ φrd≡ycd = φ rd ≡⟨by-definition⟩ φ (r₀ − r₁) ≡⟨ −-/ ⟩ φ r₀ / φ r₁ ≡⟨ /= (==⇒≡ verify₀) (==⇒≡ verify₁) ⟩ (Y ^ c₀ * A) / (Y ^ c₁ * A) ≡⟨ elim-*-right-/ ⟩ Y ^ c₀ / Y ^ c₁ ≡⟨ ! ^-−ᶜ c₀>c₁ ⟩ Y ^(c₀ −ᶜ c₁) ≡⟨by-definition⟩ Y ^ cd ∎ -- The extracted x is correct extractor-ok : φ x' ≡ Y extractor-ok = φ(rd ⊗ 1/cd) ≡⟨ φ-hom-iterated ⟩ φ rd ^ 1/cd ≡⟨ ap₂ _^_ φrd≡ycd idp ⟩ (Y ^ cd)^ 1/cd ≡⟨ ^-^-1/ c₀>c₁ ⟩ Y ∎ special-soundness : Special-Soundness Schnorr special-soundness = record { extractor = extractor ; extract-valid-witness = extractor-ok } special-Σ-protocol : Special-Σ-Protocol special-Σ-protocol = record { Σ-protocol = Schnorr ; correct = correct ; shvzk = shvzk ; ssound = special-soundness } -- -} -- -} -- -} -- -}
split the verifier function
GroupHom: split the verifier function
Agda
bsd-3-clause
crypto-agda/crypto-agda
90aff21d03b068f50fef74f2c22666b5ea68c76b
Control/Strategy.agda
Control/Strategy.agda
module Control.Strategy where open import Function open import Type using (★) open import Category.Monad open import Relation.Binary.PropositionalEquality.NP data Strategy (Q R A : ★) : ★ where ask : (q? : Q) (cont : R → Strategy Q R A) → Strategy Q R A done : A → Strategy Q R A infix 2 _≈_ data _≈_ {Q R A} : (s₀ s₁ : Strategy Q R A) → ★ where ask-ask : ∀ {k₀ k₁} → (q? : Q) (cont : ∀ r → k₀ r ≈ k₁ r) → ask q? k₀ ≈ ask q? k₁ done-done : ∀ x → done x ≈ done x ≈-refl : ∀ {Q R A} {s : Strategy Q R A} → s ≈ s ≈-refl {s = ask q? cont} = ask-ask q? (λ r → ≈-refl) ≈-refl {s = done x} = done-done x module _ {Q R : ★} where private M : ★ → ★ M = Strategy Q R _=<<_ : ∀ {A B} → (A → M B) → M A → M B f =<< ask q? cont = ask q? (λ r → f =<< cont r) f =<< done x = f x return : ∀ {A} → A → M A return = done map : ∀ {A B} → (A → B) → M A → M B map f s = (done ∘ f) =<< s join : ∀ {A} → M (M A) → M A join s = id =<< s _>>=_ : ∀ {A B} → M A → (A → M B) → M B _>>=_ = flip _=<<_ rawMonad : RawMonad M rawMonad = record { return = return; _>>=_ = _>>=_ } return-=<< : ∀ {A} (m : M A) → return =<< m ≈ m return-=<< (ask q? cont) = ask-ask q? (λ r → return-=<< (cont r)) return-=<< (done x) = done-done x return->>= : ∀ {A B} (x : A) (f : A → M B) → return x >>= f ≡ f x return->>= _ _ = refl >>=-assoc : ∀ {A B C} (mx : M A) (my : A → M B) (f : B → M C) → (mx >>= λ x → (my x >>= f)) ≈ (mx >>= my) >>= f >>=-assoc (ask q? cont) my f = ask-ask q? (λ r → >>=-assoc (cont r) my f) >>=-assoc (done x) my f = ≈-refl map-id : ∀ {A} (s : M A) → map id s ≈ s map-id = return-=<< map-∘ : ∀ {A B C} (f : A → B) (g : B → C) s → map (g ∘ f) s ≈ (map g ∘ map f) s map-∘ f g s = >>=-assoc s (done ∘ f) (done ∘ g) module EffectfulRun (E : ★ → ★) (_>>=E_ : ∀ {A B} → E A → (A → E B) → E B) (returnE : ∀ {A} → A → E A) (Oracle : Q → E R) where runE : ∀ {A} → M A → E A runE (ask q? cont) = Oracle q? >>=E (λ r → runE (cont r)) runE (done x) = returnE x module _ (Oracle : Q → R) where run : ∀ {A} → M A → A run (ask q? cont) = run (cont (Oracle q?)) run (done x) = x module _ {A B : ★} (f : A → M B) where run->>= : (s : M A) → run (s >>= f) ≡ run (f (run s)) run->>= (ask q? cont) = run->>= (cont (Oracle q?)) run->>= (done x) = refl module _ {A B : ★} (f : A → B) where run-map : (s : M A) → run (map f s) ≡ f (run s) run-map = run->>= (return ∘ f) module _ {A B : ★} where run-≈ : {s₀ s₁ : M A} → s₀ ≈ s₁ → run s₀ ≡ run s₁ run-≈ (ask-ask q? cont) = run-≈ (cont (Oracle q?)) run-≈ (done-done x) = refl
module Control.Strategy where open import Function open import Type using (★) open import Category.Monad open import Data.Product hiding (map) open import Relation.Binary.PropositionalEquality.NP data Strategy (Q : ★) (R : Q → ★) (A : ★) : ★ where ask : (q? : Q) (cont : R q? → Strategy Q R A) → Strategy Q R A done : A → Strategy Q R A infix 2 _≈_ data _≈_ {Q R A} : (s₀ s₁ : Strategy Q R A) → ★ where ask-ask : ∀ (q? : Q) {k₀ k₁} (cont : ∀ r → k₀ r ≈ k₁ r) → ask q? k₀ ≈ ask q? k₁ done-done : ∀ x → done x ≈ done x ≈-refl : ∀ {Q R A} {s : Strategy Q R A} → s ≈ s ≈-refl {s = ask q? cont} = ask-ask q? (λ r → ≈-refl) ≈-refl {s = done x} = done-done x module _ {Q : ★} {R : Q → ★} where private M : ★ → ★ M = Strategy Q R _=<<_ : ∀ {A B} → (A → M B) → M A → M B f =<< ask q? cont = ask q? (λ r → f =<< cont r) f =<< done x = f x return : ∀ {A} → A → M A return = done map : ∀ {A B} → (A → B) → M A → M B map f s = (done ∘ f) =<< s join : ∀ {A} → M (M A) → M A join s = id =<< s _>>=_ : ∀ {A B} → M A → (A → M B) → M B _>>=_ = flip _=<<_ rawMonad : RawMonad M rawMonad = record { return = return; _>>=_ = _>>=_ } return-=<< : ∀ {A} (m : M A) → return =<< m ≈ m return-=<< (ask q? cont) = ask-ask q? (λ r → return-=<< (cont r)) return-=<< (done x) = done-done x return->>= : ∀ {A B} (x : A) (f : A → M B) → return x >>= f ≡ f x return->>= _ _ = refl >>=-assoc : ∀ {A B C} (mx : M A) (my : A → M B) (f : B → M C) → (mx >>= λ x → (my x >>= f)) ≈ (mx >>= my) >>= f >>=-assoc (ask q? cont) my f = ask-ask q? (λ r → >>=-assoc (cont r) my f) >>=-assoc (done x) my f = ≈-refl map-id : ∀ {A} (s : M A) → map id s ≈ s map-id = return-=<< map-∘ : ∀ {A B C} (f : A → B) (g : B → C) s → map (g ∘ f) s ≈ (map g ∘ map f) s map-∘ f g s = >>=-assoc s (done ∘ f) (done ∘ g) module EffectfulRun (E : ★ → ★) (_>>=E_ : ∀ {A B} → E A → (A → E B) → E B) (returnE : ∀ {A} → A → E A) (Oracle : (q : Q) → E (R q)) where runE : ∀ {A} → M A → E A runE (ask q? cont) = Oracle q? >>=E (λ r → runE (cont r)) runE (done x) = returnE x module _ (Oracle : (q : Q) → R q) where run : ∀ {A} → M A → A run (ask q? cont) = run (cont (Oracle q?)) run (done x) = x module _ {A B : ★} (f : A → M B) where run->>= : (s : M A) → run (s >>= f) ≡ run (f (run s)) run->>= (ask q? cont) = run->>= (cont (Oracle q?)) run->>= (done x) = refl module _ {A B : ★} (f : A → B) where run-map : (s : M A) → run (map f s) ≡ f (run s) run-map = run->>= (return ∘ f) module _ {A B : ★} where run-≈ : {s₀ s₁ : M A} → s₀ ≈ s₁ → run s₀ ≡ run s₁ run-≈ (ask-ask q? cont) = run-≈ (cont (Oracle q?)) run-≈ (done-done x) = refl private State : (S A : ★) → ★ State S A = S → A × S -- redundant since we have EffectfulRun, but... module StatefulRun {S : ★} (Oracle : (q : Q) → State S (R q)) where runS : ∀ {A} → M A → State S A runS (ask q? cont) s = case Oracle q? s of λ { (x , s') → runS (cont x) s' } runS (done x) s = x , s evalS : ∀ {A} → M A → S → A evalS x s = proj₁ (runS x s) execS : ∀ {A} → M A → S → S execS x s = proj₂ (runS x s) -- -} -- -} -- -} -- -}
Improve Strategy to have a the response type depend on the query type
Improve Strategy to have a the response type depend on the query type
Agda
bsd-3-clause
crypto-agda/crypto-agda
f1f55119854a27617ba6d8786230e7ca527ff616
agda2/Text/Greek/Smyth.agda
agda2/Text/Greek/Smyth.agda
module Text.Greek.Smyth where open import Data.Char open import Data.Maybe open import Relation.Nullary using (¬_) -- Smyth §1 data Letter : Set where α β γ δ ε ζ η θ ι κ λ′ μ ν ξ ο π ρ σ τ υ φ χ ψ ω : Letter data LetterCase : Set where capital small : LetterCase form : Letter → LetterCase → Char form α capital = 'Α' form α small = 'α' form β capital = 'Β' form β small = 'β' form γ capital = 'Γ' form γ small = 'γ' form δ capital = 'Δ' form δ small = 'δ' form ε capital = 'Ε' form ε small = 'ε' form ζ capital = 'Ζ' form ζ small = 'ζ' form η capital = 'Η' form η small = 'η' form θ capital = 'Θ' form θ small = 'θ' form ι capital = 'Ι' form ι small = 'ι' form κ capital = 'Κ' form κ small = 'κ' form λ′ capital = 'Λ' form λ′ small = 'λ' form μ capital = 'Μ' form μ small = 'μ' form ν capital = 'Ν' form ν small = 'ν' form ξ capital = 'Ξ' form ξ small = 'ξ' form ο capital = 'Ο' form ο small = 'ο' form π capital = 'Π' form π small = 'π' form ρ capital = 'Ρ' form ρ small = 'ρ' form σ capital = 'Σ' form σ small = 'σ' form τ capital = 'Τ' form τ small = 'τ' form υ capital = 'Υ' form υ small = 'υ' form φ capital = 'Φ' form φ small = 'φ' form χ capital = 'Χ' form χ small = 'χ' form ψ capital = 'Ψ' form ψ small = 'ψ' form ω capital = 'Ω' form ω small = 'ω' -- Smyth §1 a data PositionInWord : Set where start-of-word : PositionInWord middle-of-word : PositionInWord end-of-word : PositionInWord data FinalForm : Set where final-form not-final-form : FinalForm get-final-form : Letter → LetterCase → PositionInWord → FinalForm get-final-form σ′ small end-of-word = final-form get-final-form _ _ _ = not-final-form -- Smyth §3 data OlderLetter : Set where Ϝ′ : OlderLetter Ϙ′ : OlderLetter ϡ′ : OlderLetter -- Smyth §4 data Vowel : Letter → Set where α : Vowel α ε : Vowel ε η : Vowel η ι : Vowel ι ο : Vowel ο υ : Vowel υ ω : Vowel ω data AlwaysShort : Letter → Set where ε : AlwaysShort ε ο : AlwaysShort ο data AlwaysLong : Letter → Set where η : AlwaysLong η ω : AlwaysLong ω data VowelLength : Set where short long : VowelLength data VowelWithLength : ∀ {v} → Vowel v → VowelLength → Set where always-short : ∀ {ℓ} → (v : Vowel ℓ) → AlwaysShort ℓ → VowelWithLength v short always-long : ∀ {ℓ} → (v : Vowel ℓ) → AlwaysLong ℓ → VowelWithLength v long α-with-length : (vl : VowelLength) → VowelWithLength α vl ι-with-length : (vl : VowelLength) → VowelWithLength ι vl υ-with-length : (vl : VowelLength) → VowelWithLength υ vl alwaysShort-Vowel : ∀ {ℓ} → AlwaysShort ℓ → Vowel ℓ alwaysShort-Vowel ε = ε alwaysShort-Vowel ο = ο alwaysLong-Vowel : ∀ {ℓ} → AlwaysLong ℓ → Vowel ℓ alwaysLong-Vowel η = η alwaysLong-Vowel ω = ω -- Smyth §5 data Diphthong : Letter → Letter → Set where αι : Diphthong α ι ει : Diphthong ε ι οι : Diphthong ο ι ᾱͅ : Diphthong α ι ῃ : Diphthong η ι ῳ : Diphthong ω ι αυ : Diphthong α υ ευ : Diphthong ε υ ου : Diphthong ο υ ηυ : Diphthong η υ υι : Diphthong υ ι -- Smyth §6 data SpuriousDiphthong : ∀ {v₁ v₂} → Diphthong v₁ v₂ → Set where ει-sp : SpuriousDiphthong ει ου-sp : SpuriousDiphthong ου -- Smyth §7 data TonguePosition : Set where closed closed-medium open-medium open′ closing opening : TonguePosition tongue-position-vowel : ∀ {ℓ} → Vowel ℓ → TonguePosition tongue-position-vowel α = open′ tongue-position-vowel ε = closed-medium tongue-position-vowel η = open-medium tongue-position-vowel ι = closed tongue-position-vowel ο = closed-medium tongue-position-vowel υ = closed tongue-position-vowel ω = open-medium tongue-position-diphthong : ∀ {ℓ₁ ℓ₂} → Diphthong ℓ₁ ℓ₂ → TonguePosition tongue-position-diphthong d = closing data RoundedLips : Set where rounded-lips not-rounded-lips : RoundedLips rounded-lips-vowel : ∀ {ℓ} → Vowel ℓ → RoundedLips rounded-lips-vowel ο = rounded-lips rounded-lips-vowel υ = rounded-lips rounded-lips-vowel ω = rounded-lips rounded-lips-vowel _ = not-rounded-lips rounded-lips-diphthong : ∀ {ℓ₁ ℓ₂} → Diphthong ℓ₁ ℓ₂ → RoundedLips rounded-lips-diphthong ου = rounded-lips rounded-lips-diphthong _ = not-rounded-lips -- Smyth §9 data Breathing : Set where ῾ ᾿ : Breathing -- Smyth §15 data Consonant : Letter → Set where β : Consonant β γ : Consonant γ δ : Consonant δ ζ : Consonant ζ θ : Consonant θ κ : Consonant κ λ′ : Consonant λ′ μ : Consonant μ ν : Consonant ν ξ : Consonant ξ π : Consonant π ρ : Consonant ρ σ : Consonant σ τ : Consonant τ φ : Consonant φ χ : Consonant χ ψ : Consonant ψ data LetterCategory : Letter → Set where vowel : ∀ {ℓ} → Vowel ℓ → LetterCategory ℓ consonant : ∀ {ℓ} → Consonant ℓ → LetterCategory ℓ Letter-to-LetterCategory : (ℓ : Letter) → LetterCategory ℓ Letter-to-LetterCategory α = vowel α Letter-to-LetterCategory β = consonant β Letter-to-LetterCategory γ = consonant γ Letter-to-LetterCategory δ = consonant δ Letter-to-LetterCategory ε = vowel ε Letter-to-LetterCategory ζ = consonant ζ Letter-to-LetterCategory η = vowel η Letter-to-LetterCategory θ = consonant θ Letter-to-LetterCategory ι = vowel ι Letter-to-LetterCategory κ = consonant κ Letter-to-LetterCategory λ′ = consonant λ′ Letter-to-LetterCategory μ = consonant μ Letter-to-LetterCategory ν = consonant ν Letter-to-LetterCategory ξ = consonant ξ Letter-to-LetterCategory ο = vowel ο Letter-to-LetterCategory π = consonant π Letter-to-LetterCategory ρ = consonant ρ Letter-to-LetterCategory σ = consonant σ Letter-to-LetterCategory τ = consonant τ Letter-to-LetterCategory υ = vowel υ Letter-to-LetterCategory φ = consonant φ Letter-to-LetterCategory χ = consonant χ Letter-to-LetterCategory ψ = consonant ψ Letter-to-LetterCategory ω = vowel ω LetterCategory-to-Letter : ∀ {ℓ} → LetterCategory ℓ → Letter LetterCategory-to-Letter {ℓ} _ = ℓ data ConsonantSound : Letter → Set where β : ConsonantSound β γ : ConsonantSound γ δ : ConsonantSound δ ζ : ConsonantSound ζ θ : ConsonantSound θ κ : ConsonantSound κ λ′ : ConsonantSound λ′ μ : ConsonantSound μ ν : ConsonantSound ν ξ : ConsonantSound ξ π : ConsonantSound π ρ : ConsonantSound ρ σ : ConsonantSound σ τ : ConsonantSound τ φ : ConsonantSound φ χ : ConsonantSound χ ψ : ConsonantSound ψ γ-nasal : ConsonantSound γ ῥ : ConsonantSound ρ data VocalChords : Set where voiced voiceless : VocalChords -- Smyth §15 a data Voiced : ∀ {ℓ} → ConsonantSound ℓ → Set where β : Voiced β δ : Voiced δ γ : Voiced γ λ′ : Voiced λ′ ρ : Voiced ρ μ : Voiced μ ν : Voiced ν γ-nasal : Voiced γ-nasal ζ : Voiced ζ -- Smyth §15 a, b data Voiceless : ∀ {ℓ} → ConsonantSound ℓ → Set where ῥ : Voiceless ῥ π : Voiceless π τ : Voiceless τ κ : Voiceless κ φ : Voiceless φ θ : Voiceless θ χ : Voiceless χ σ : Voiceless σ ψ : Voiceless ψ ξ : Voiceless ξ -- TODO (needs a unified model) -- Accent -- Smyth §4 All vowels with the circumflex (149) are long. -- Diaeresis -- Smyth §8 Diaeresis.—A double dot, the mark of diaeresis, may be written over ι or υ when these do not form a diphthong with the preceding vowel -- Breathing -- Smyth §9. Every initial vowel or diphthong has either the rough (῾) or the smooth (᾿) breathing. -- Smyth §10. Initial υ (ῠ and ῡ) always has the rough breathing. -- Smyth §11. Diphthongs take the breathing, as the accent (152), over the second vowel: αἱρέω hairéo I seize, αἴρω aíro I lift. -- Smyth §11. But ᾳ, ῃ, ῳ take both the breathing and the accent on the first vowel, even when ι is written in the line (5): ᾄδω = Ἄιδω I sing… The writing ἀίδηλος (Ἀίδηλος) destroying shows that αι does not here form a diphthong -- Smyth §12. In compound words (as in προορᾶν to foresee, from πρό + ὁρᾶν) the rough breathing is not written, though it must often have been pronounced -- Smyth §13. Every initial ρ has the rough breathing -- Smyth §13. Medial ρρ is written ῤῥ in some texts -- Consonants -- Smyth §15 c. ι̯ υ̯
module Text.Greek.Smyth where open import Data.Char open import Data.Maybe open import Relation.Nullary using (¬_) -- Smyth §1 data Letter : Set where α β γ δ ε ζ η θ ι κ λ′ μ ν ξ ο π ρ σ τ υ φ χ ψ ω : Letter data LetterCase : Set where capital small : LetterCase form : Letter → LetterCase → Char form α capital = 'Α' form α small = 'α' form β capital = 'Β' form β small = 'β' form γ capital = 'Γ' form γ small = 'γ' form δ capital = 'Δ' form δ small = 'δ' form ε capital = 'Ε' form ε small = 'ε' form ζ capital = 'Ζ' form ζ small = 'ζ' form η capital = 'Η' form η small = 'η' form θ capital = 'Θ' form θ small = 'θ' form ι capital = 'Ι' form ι small = 'ι' form κ capital = 'Κ' form κ small = 'κ' form λ′ capital = 'Λ' form λ′ small = 'λ' form μ capital = 'Μ' form μ small = 'μ' form ν capital = 'Ν' form ν small = 'ν' form ξ capital = 'Ξ' form ξ small = 'ξ' form ο capital = 'Ο' form ο small = 'ο' form π capital = 'Π' form π small = 'π' form ρ capital = 'Ρ' form ρ small = 'ρ' form σ capital = 'Σ' form σ small = 'σ' form τ capital = 'Τ' form τ small = 'τ' form υ capital = 'Υ' form υ small = 'υ' form φ capital = 'Φ' form φ small = 'φ' form χ capital = 'Χ' form χ small = 'χ' form ψ capital = 'Ψ' form ψ small = 'ψ' form ω capital = 'Ω' form ω small = 'ω' -- Smyth §1 a data PositionInWord : Set where start-of-word : PositionInWord middle-of-word : PositionInWord end-of-word : PositionInWord data FinalForm : Set where final-form not-final-form : FinalForm get-final-form : Letter → LetterCase → PositionInWord → FinalForm get-final-form σ′ small end-of-word = final-form get-final-form _ _ _ = not-final-form -- Smyth §3 data OlderLetter : Set where Ϝ : OlderLetter Ϙ : OlderLetter ϡ : OlderLetter -- Smyth §4 data Vowel : Letter → Set where α : Vowel α ε : Vowel ε η : Vowel η ι : Vowel ι ο : Vowel ο υ : Vowel υ ω : Vowel ω data AlwaysShort : Letter → Set where ε : AlwaysShort ε ο : AlwaysShort ο data AlwaysLong : Letter → Set where η : AlwaysLong η ω : AlwaysLong ω data VowelLength : Set where short long : VowelLength data VowelWithLength : ∀ {v} → Vowel v → VowelLength → Set where always-short : ∀ {ℓ} → (v : Vowel ℓ) → AlwaysShort ℓ → VowelWithLength v short always-long : ∀ {ℓ} → (v : Vowel ℓ) → AlwaysLong ℓ → VowelWithLength v long α-with-length : (vl : VowelLength) → VowelWithLength α vl ι-with-length : (vl : VowelLength) → VowelWithLength ι vl υ-with-length : (vl : VowelLength) → VowelWithLength υ vl alwaysShort-Vowel : ∀ {ℓ} → AlwaysShort ℓ → Vowel ℓ alwaysShort-Vowel ε = ε alwaysShort-Vowel ο = ο alwaysLong-Vowel : ∀ {ℓ} → AlwaysLong ℓ → Vowel ℓ alwaysLong-Vowel η = η alwaysLong-Vowel ω = ω -- Smyth §5 data Diphthong : Letter → Letter → Set where αι : Diphthong α ι ει : Diphthong ε ι οι : Diphthong ο ι ᾱͅ : Diphthong α ι ῃ : Diphthong η ι ῳ : Diphthong ω ι αυ : Diphthong α υ ευ : Diphthong ε υ ου : Diphthong ο υ ηυ : Diphthong η υ υι : Diphthong υ ι -- Smyth §6 data SpuriousDiphthong : ∀ {v₁ v₂} → Diphthong v₁ v₂ → Set where ει-sp : SpuriousDiphthong ει ου-sp : SpuriousDiphthong ου -- Smyth §7 data TonguePosition : Set where closed closed-medium open-medium open′ closing opening : TonguePosition tongue-position-vowel : ∀ {ℓ} → Vowel ℓ → TonguePosition tongue-position-vowel α = open′ tongue-position-vowel ε = closed-medium tongue-position-vowel η = open-medium tongue-position-vowel ι = closed tongue-position-vowel ο = closed-medium tongue-position-vowel υ = closed tongue-position-vowel ω = open-medium tongue-position-diphthong : ∀ {ℓ₁ ℓ₂} → Diphthong ℓ₁ ℓ₂ → TonguePosition tongue-position-diphthong d = closing data RoundedLips : Set where rounded-lips not-rounded-lips : RoundedLips rounded-lips-vowel : ∀ {ℓ} → Vowel ℓ → RoundedLips rounded-lips-vowel ο = rounded-lips rounded-lips-vowel υ = rounded-lips rounded-lips-vowel ω = rounded-lips rounded-lips-vowel _ = not-rounded-lips rounded-lips-diphthong : ∀ {ℓ₁ ℓ₂} → Diphthong ℓ₁ ℓ₂ → RoundedLips rounded-lips-diphthong ου = rounded-lips rounded-lips-diphthong _ = not-rounded-lips -- Smyth §9 data Breathing : Set where ῾ ᾿ : Breathing -- Smyth §15 data Consonant : Letter → Set where β : Consonant β γ : Consonant γ δ : Consonant δ ζ : Consonant ζ θ : Consonant θ κ : Consonant κ λ′ : Consonant λ′ μ : Consonant μ ν : Consonant ν ξ : Consonant ξ π : Consonant π ρ : Consonant ρ σ : Consonant σ τ : Consonant τ φ : Consonant φ χ : Consonant χ ψ : Consonant ψ data LetterCategory : Letter → Set where vowel : ∀ {ℓ} → Vowel ℓ → LetterCategory ℓ consonant : ∀ {ℓ} → Consonant ℓ → LetterCategory ℓ Letter-to-LetterCategory : (ℓ : Letter) → LetterCategory ℓ Letter-to-LetterCategory α = vowel α Letter-to-LetterCategory β = consonant β Letter-to-LetterCategory γ = consonant γ Letter-to-LetterCategory δ = consonant δ Letter-to-LetterCategory ε = vowel ε Letter-to-LetterCategory ζ = consonant ζ Letter-to-LetterCategory η = vowel η Letter-to-LetterCategory θ = consonant θ Letter-to-LetterCategory ι = vowel ι Letter-to-LetterCategory κ = consonant κ Letter-to-LetterCategory λ′ = consonant λ′ Letter-to-LetterCategory μ = consonant μ Letter-to-LetterCategory ν = consonant ν Letter-to-LetterCategory ξ = consonant ξ Letter-to-LetterCategory ο = vowel ο Letter-to-LetterCategory π = consonant π Letter-to-LetterCategory ρ = consonant ρ Letter-to-LetterCategory σ = consonant σ Letter-to-LetterCategory τ = consonant τ Letter-to-LetterCategory υ = vowel υ Letter-to-LetterCategory φ = consonant φ Letter-to-LetterCategory χ = consonant χ Letter-to-LetterCategory ψ = consonant ψ Letter-to-LetterCategory ω = vowel ω LetterCategory-to-Letter : ∀ {ℓ} → LetterCategory ℓ → Letter LetterCategory-to-Letter {ℓ} _ = ℓ data ConsonantSound : Letter → Set where β : ConsonantSound β γ : ConsonantSound γ δ : ConsonantSound δ ζ : ConsonantSound ζ θ : ConsonantSound θ κ : ConsonantSound κ λ′ : ConsonantSound λ′ μ : ConsonantSound μ ν : ConsonantSound ν ξ : ConsonantSound ξ π : ConsonantSound π ρ : ConsonantSound ρ σ : ConsonantSound σ τ : ConsonantSound τ φ : ConsonantSound φ χ : ConsonantSound χ ψ : ConsonantSound ψ γ-nasal : ConsonantSound γ ῥ : ConsonantSound ρ data VocalChords : Set where voiced voiceless : VocalChords -- Smyth §15 a data Voiced : ∀ {ℓ} → ConsonantSound ℓ → Set where β : Voiced β δ : Voiced δ γ : Voiced γ λ′ : Voiced λ′ ρ : Voiced ρ μ : Voiced μ ν : Voiced ν γ-nasal : Voiced γ-nasal ζ : Voiced ζ -- Smyth §15 a, b data Voiceless : ∀ {ℓ} → ConsonantSound ℓ → Set where ῥ : Voiceless ῥ π : Voiceless π τ : Voiceless τ κ : Voiceless κ φ : Voiceless φ θ : Voiceless θ χ : Voiceless χ σ : Voiceless σ ψ : Voiceless ψ ξ : Voiceless ξ -- Smyth §16 data PartOfMouthClass : Set where labial dental palatal : PartOfMouthClass data Labial : ∀ {ℓ} → ConsonantSound ℓ → Set where π : Labial π β : Labial β φ : Labial φ data Dental : ∀ {ℓ} → ConsonantSound ℓ → Set where τ : Dental τ δ : Dental δ θ : Dental θ data Palatal : ∀ {ℓ} → ConsonantSound ℓ → Set where κ : Palatal κ γ : Palatal γ χ : Palatal χ data SmoothStop : ∀ {ℓ} → ConsonantSound ℓ → Set where π : SmoothStop π τ : SmoothStop τ κ : SmoothStop κ data MiddleStop : ∀ {ℓ} → ConsonantSound ℓ → Set where β : MiddleStop β δ : MiddleStop δ γ : MiddleStop γ data RoughStop : ∀ {ℓ} → ConsonantSound ℓ → Set where φ : RoughStop φ θ : RoughStop θ χ : RoughStop χ -- Smyth §17 data Spirant : ∀ {ℓ} → ConsonantSound ℓ → Set where σ : Spirant σ -- Smyth §18 data Liquid : ∀ {ℓ} → ConsonantSound ℓ → Set where λ′ : Liquid λ′ ρ : Liquid ρ -- Smyth §19 data Nasal : ∀ {ℓ} → ConsonantSound ℓ → Set where μ : Nasal μ ν : Nasal ν γ-nasal : Nasal γ-nasal -- Smyth §20 data Semivowel : ∀ {ℓ} → Vowel ℓ → Set where ι̯ : Semivowel ι υ̯ : Semivowel υ -- Smyth §20 b data Sonant : ∀ {ℓ} → ConsonantSound ℓ → Set where λ̥ : Sonant λ′ μ̥ : Sonant μ ν̥ : Sonant γ ρ̥ : Sonant ρ σ̥ : Sonant σ -- Smyth §21 data DoubleConsonant : ∀ {ℓ} → ConsonantSound ℓ → Set where ζ : DoubleConsonant ζ ξ : DoubleConsonant ξ ψ : DoubleConsonant ψ data _+_⇒DoubleConsonant_ : ∀ {ℓ₁ ℓ₂ ℓ₃} → ConsonantSound ℓ₁ → ConsonantSound ℓ₂ → ConsonantSound ℓ₃ → Set where σ+δ⇒DoubleConsonantζ : σ + δ ⇒DoubleConsonant ζ -- Smyth §26 D. Aeolic has σδ for ζ δ+σ⇒DoubleConsonantζ : δ + σ ⇒DoubleConsonant ζ κ+σ⇒DoubleConsonantξ : κ + σ ⇒DoubleConsonant ξ γ+σ⇒DoubleConsonantξ : γ + σ ⇒DoubleConsonant ξ χ+σ⇒DoubleConsonantξ : χ + σ ⇒DoubleConsonant ξ π+σ⇒DoubleConsonantψ : π + σ ⇒DoubleConsonant ψ β+σ⇒DoubleConsonantψ : β + σ ⇒DoubleConsonant ψ φ+σ⇒DoubleConsonantψ : φ + σ ⇒DoubleConsonant ψ -- TODO (needs a unified model) -- Accent -- Smyth §4. All vowels with the circumflex (149) are long. -- Diaeresis -- Smyth §8. Diaeresis.—A double dot, the mark of diaeresis, may be written over ι or υ when these do not form a diphthong with the preceding vowel -- Breathing -- Smyth §9. Every initial vowel or diphthong has either the rough (῾) or the smooth (᾿) breathing. -- Smyth §10. Initial υ (ῠ and ῡ) always has the rough breathing. -- Smyth §11. Diphthongs take the breathing, as the accent (152), over the second vowel: αἱρέω hairéo I seize, αἴρω aíro I lift. -- Smyth §11. But ᾳ, ῃ, ῳ take both the breathing and the accent on the first vowel, even when ι is written in the line (5): ᾄδω = Ἄιδω I sing… The writing ἀίδηλος (Ἀίδηλος) destroying shows that αι does not here form a diphthong -- Smyth §12. In compound words (as in προορᾶν to foresee, from πρό + ὁρᾶν) the rough breathing is not written, though it must often have been pronounced -- Smyth §13. Every initial ρ has the rough breathing -- Smyth §13. Medial ρρ is written ῤῥ in some texts -- Consonants -- Smyth §15 c. ι̯ υ̯ -- Smyth §19 a. Gamma before κ, γ, χ, ξ is called γ-nasal -- Smyth §20 a. When ι and υ correspond to y and w (cp. minion, persuade) they are said to be unsyllabic; and, with a following vowel, make one syllable out of two. -- Smyth §20 a. Initial ι̯ passed into ̔ (h), as in ἧπαρ liver, Lat. jecur; and into ζ in ζυγόν yoke -- Smyth §20 a. Initial υ̯ was written ϝ -- Smyth §20 a. Medial ι̯, υ̯ before vowels were often lost, as in τῑμά-(ι̯)ω I honour, βο (υ̯)-ός, gen. of βοῦ-ς ox, cow -- Smyth §26. σ was usually like our sharp s; but before voiced consonants (15 a) it probably was soft, like z; thus we find both κόζμος and κόσμος on inscriptions.
Add more Smyth encoding.
Add more Smyth encoding.
Agda
mit
scott-fleischman/greek-grammar,scott-fleischman/greek-grammar
f6544cdf2b41aa0730418c4d5d83b346e2b74f2e
Base/Change/Equivalence.agda
Base/Change/Equivalence.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Delta-observational equivalence ------------------------------------------------------------------------ module Base.Change.Equivalence where open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra open import Level open import Data.Unit open import Function open import Base.Change.Equivalence.Base public open import Base.Change.Equivalence.EqReasoning public module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where -- By update-nil, if dx = nil x, then x ⊞ dx ≡ x. -- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x nil-is-⊞-unit dx dx≙nil-x = begin x ⊞ dx ≡⟨ proof dx≙nil-x ⟩ x ⊞ (nil x) ≡⟨ update-nil x ⟩ x ∎ where open ≡-Reasoning -- Here we prove the inverse: ⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x ⊞-unit-is-nil dx x⊞dx≡x = doe $ begin x ⊞ dx ≡⟨ x⊞dx≡x ⟩ x ≡⟨ sym (update-nil x) ⟩ x ⊞ nil x ∎ where open ≡-Reasoning -- Let's show that nil x is d.o.e. to x ⊟ x nil-x-is-x⊟x : nil x ≙ x ⊟ x nil-x-is-x⊟x = ≙-sym (⊞-unit-is-nil (x ⊟ x) (update-diff x x)) -- TODO: we want to show that all functions of interest respect -- delta-observational equivalence, so that two d.o.e. changes can be -- substituted for each other freely. -- -- * That should be be true for -- functions using changes parametrically. -- -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ]; -- this is proved below on both contexts at once by fun-change-respects. -- -- * Finally, change algebra operations should respect d.o.e. But ⊞ respects -- it by definition, and ⊟ doesn't take change arguments - we will only -- need a proof for compose, when we define it. -- -- Stating the general result, though, seems hard, we should -- rather have lemmas proving that certain classes of functions respect this -- equivalence. -- This results pairs with update-diff. diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx diff-update {dx} = doe lemma where lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx lemma = update-diff (x ⊞ dx) x module _ {a} {b} {c} {d} {A : Set a} {B : Set b} {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where module FC = FunctionChanges A B {{CA}} {{CB}} open FC using (changeAlgebra; incrementalization) open FC.FunctionChange fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} → df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂ fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂ -- Informally: use incrementalization on both sides and then apply -- congruence. lemma = begin f x ⊞ apply df₁ x dx₁ ≡⟨ sym (incrementalization f df₁ x dx₁) ⟩ (f ⊞ df₁) (x ⊞ dx₁) ≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩ (f ⊞ df₁) (x ⊞ dx₂) ≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩ (f ⊞ df₂) (x ⊞ dx₂) ≡⟨ incrementalization f df₂ x dx₂ ⟩ f x ⊞ apply df₂ x dx₂ ∎ open import Postulate.Extensionality -- An extensionality principle for delta-observational equivalence: if -- applying two function changes to the same base value and input change gives -- a d.o.e. result, then the two function changes are d.o.e. themselves. delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂ where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx lemma₂ : f ⊞ df ≡ f ⊞ dg lemma₂ = ext (λ x → lemma₁ x (nil x)) -- We know that Derivative f (apply (nil f)) (by nil-is-derivative). -- That is, df = nil f -> Derivative f (apply df). -- Now, we try to prove that if Derivative f (apply df) -> df = nil f. -- But first, we prove that f ⊞ df = f. derivative-is-⊞-unit : ∀ {f : A → B} df → Derivative f (apply df) → f ⊞ df ≡ f derivative-is-⊞-unit {f} df fdf = begin f ⊞ df ≡⟨⟩ (λ x → f x ⊞ apply df x (nil x)) ≡⟨ ext (λ x → fdf x (nil x)) ⟩ (λ x → f (x ⊞ nil x)) ≡⟨ ext (λ x → cong f (update-nil x)) ⟩ (λ x → f x) ≡⟨⟩ f ∎ where open ≡-Reasoning -- We can restate the above as "df is a nil change". derivative-is-nil : ∀ {f : A → B} df → Derivative f (apply df) → df ≙ nil f derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf) -- If we have two derivatives, they're both nil, hence they're equal. derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg derivative-unique {f} {df} {dg} fdf fdg = begin df ≙⟨ derivative-is-nil df fdf ⟩ nil f ≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩ dg ∎ where open ≙-Reasoning -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses -- (incorrectly) normal equality instead of delta-observational equivalence. deriv-zero : (f : A → B) → (df : (a : A) (da : Δ a) → Δ (f a)) → Derivative f df → ∀ v → df v (nil v) ≙ nil (f v) deriv-zero f df proof v = doe lemma where open ≡-Reasoning lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil (f v) lemma = begin f v ⊞ df v (nil v) ≡⟨ proof v (nil v) ⟩ f (v ⊞ (nil v)) ≡⟨ cong f (update-nil v) ⟩ f v ≡⟨ sym (update-nil (f v)) ⟩ f v ⊞ nil (f v) ∎
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Delta-observational equivalence ------------------------------------------------------------------------ module Base.Change.Equivalence where open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra open import Level open import Data.Unit open import Function open import Base.Change.Equivalence.Base public open import Base.Change.Equivalence.EqReasoning public module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where -- By update-nil, if dx = nil x, then x ⊞ dx ≡ x. -- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x nil-is-⊞-unit dx dx≙nil-x = begin x ⊞ dx ≡⟨ proof dx≙nil-x ⟩ x ⊞ (nil x) ≡⟨ update-nil x ⟩ x ∎ where open ≡-Reasoning -- Here we prove the inverse: ⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x ⊞-unit-is-nil dx x⊞dx≡x = doe $ begin x ⊞ dx ≡⟨ x⊞dx≡x ⟩ x ≡⟨ sym (update-nil x) ⟩ x ⊞ nil x ∎ where open ≡-Reasoning -- Let's show that nil x is d.o.e. to x ⊟ x nil-x-is-x⊟x : nil x ≙ x ⊟ x nil-x-is-x⊟x = ≙-sym (⊞-unit-is-nil (x ⊟ x) (update-diff x x)) -- TODO: we want to show that all functions of interest respect -- delta-observational equivalence, so that two d.o.e. changes can be -- substituted for each other freely. -- -- * That should be be true for -- functions using changes parametrically. -- -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ]; -- this is proved below on both contexts at once by fun-change-respects. -- -- * Finally, change algebra operations should respect d.o.e. But ⊞ respects -- it by definition, and ⊟ doesn't take change arguments - we will only -- need a proof for compose, when we define it. -- -- Stating the general result, though, seems hard, we should -- rather have lemmas proving that certain classes of functions respect this -- equivalence. -- This results pairs with update-diff. diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx diff-update {dx} = doe lemma where lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx lemma = update-diff (x ⊞ dx) x module _ {a} {b} {c} {d} {A : Set a} {B : Set b} {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where module FC = FunctionChanges A B {{CA}} {{CB}} open FC using (changeAlgebra; incrementalization) open FC.FunctionChange fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} → df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂ fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂ -- Informally: use incrementalization on both sides and then apply -- congruence. lemma = begin f x ⊞ apply df₁ x dx₁ ≡⟨ sym (incrementalization f df₁ x dx₁) ⟩ (f ⊞ df₁) (x ⊞ dx₁) ≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩ (f ⊞ df₁) (x ⊞ dx₂) ≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩ (f ⊞ df₂) (x ⊞ dx₂) ≡⟨ incrementalization f df₂ x dx₂ ⟩ f x ⊞ apply df₂ x dx₂ ∎ open import Postulate.Extensionality -- An extensionality principle for delta-observational equivalence: if -- applying two function changes to the same base value and input change gives -- a d.o.e. result, then the two function changes are d.o.e. themselves. delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂ where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx lemma₂ : f ⊞ df ≡ f ⊞ dg lemma₂ = ext (λ x → lemma₁ x (nil x)) -- You could think that the function should relate equivalent changes, but -- that's a stronger hypothesis, which doesn't give you extra guarantees. But -- here's the statement and proof, for completeness: delta-ext₂ : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx₁ dx₂ → dx₁ ≙ dx₂ → apply df x dx₁ ≙ apply dg x dx₂) → df ≙ dg delta-ext₂ {f} {df} {dg} df-x-dx≙dg-x-dx = delta-ext (λ x dx → df-x-dx≙dg-x-dx x dx dx ≙-refl) -- We know that Derivative f (apply (nil f)) (by nil-is-derivative). -- That is, df = nil f -> Derivative f (apply df). -- Now, we try to prove that if Derivative f (apply df) -> df = nil f. -- But first, we prove that f ⊞ df = f. derivative-is-⊞-unit : ∀ {f : A → B} df → Derivative f (apply df) → f ⊞ df ≡ f derivative-is-⊞-unit {f} df fdf = begin f ⊞ df ≡⟨⟩ (λ x → f x ⊞ apply df x (nil x)) ≡⟨ ext (λ x → fdf x (nil x)) ⟩ (λ x → f (x ⊞ nil x)) ≡⟨ ext (λ x → cong f (update-nil x)) ⟩ (λ x → f x) ≡⟨⟩ f ∎ where open ≡-Reasoning -- We can restate the above as "df is a nil change". derivative-is-nil : ∀ {f : A → B} df → Derivative f (apply df) → df ≙ nil f derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf) -- If we have two derivatives, they're both nil, hence they're equal. derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg derivative-unique {f} {df} {dg} fdf fdg = begin df ≙⟨ derivative-is-nil df fdf ⟩ nil f ≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩ dg ∎ where open ≙-Reasoning -- This is Lemma 2.5 in the paper. Note that the statement in the paper uses -- (incorrectly) normal equality instead of delta-observational equivalence. deriv-zero : (f : A → B) → (df : (a : A) (da : Δ a) → Δ (f a)) → Derivative f df → ∀ v → df v (nil v) ≙ nil (f v) deriv-zero f df proof v = doe lemma where open ≡-Reasoning lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil (f v) lemma = begin f v ⊞ df v (nil v) ≡⟨ proof v (nil v) ⟩ f (v ⊞ (nil v)) ≡⟨ cong f (update-nil v) ⟩ f v ≡⟨ sym (update-nil (f v)) ⟩ f v ⊞ nil (f v) ∎
add variant of delta-ext
Base.Change.Equivalence: add variant of delta-ext Since I was confused myself for a while on the correct statement for delta-ext, clarify it explicitly.
Agda
mit
inc-lc/ilc-agda
8f017c6136524d1546c71e1484d6011f82180f52
otp-sem-sec.agda
otp-sem-sec.agda
module otp-sem-sec where import Level as L open import Function open import Data.Nat.NP open import Data.Bits open import Data.Bool open import Data.Bool.Properties open import Data.Vec hiding (_>>=_) open import Data.Product.NP hiding (_⟦×⟧_) open import Relation.Nullary open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP open import flipbased-implem open ≡-Reasoning open import Data.Unit using (⊤) open import composable open import vcomp open import forkable proj : ∀ {a} {A : Set a} → A × A → Bit → A proj (x₀ , x₁) 0b = x₀ proj (x₀ , x₁) 1b = x₁ module FunctionExtra where _***_ : ∀ {A B C D : Set} → (A → B) → (C → D) → A × C → B × D (f *** g) (x , y) = (f x , g y) -- Fanout _&&&_ : ∀ {A B C : Set} → (A → B) → (A → C) → A → B × C (f &&& g) x = (f x , g x) _>>>_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} → (A → B) → (B → C) → (A → C) f >>> g = g ∘ f infixr 1 _>>>_ module BitsExtra where splitAt′ : ∀ k {n} → Bits (k + n) → Bits k × Bits n splitAt′ k xs = case splitAt k xs of λ { (ys , zs , _) → (ys , zs) } vnot∘vnot : ∀ {n} → vnot {n} ∘ vnot ≗ id vnot∘vnot [] = refl vnot∘vnot (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot xs) open BitsExtra Coins = ℕ record PrgDist : Set₁ where constructor mk field _]-[_ : ∀ {c} (f g : ↺ c Bit) → Set ]-[-cong : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' breaks : ∀ {c} (EXP : Bit → ↺ c Bit) → Set breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b _≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ↺ c Bit) → Set ⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b ≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c}) ≗⅁-trans p q b R = trans (p b R) (q b R) -- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁ _⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ↺ c₀ Bit) (⅁₁ : Bit → ↺ c₁ Bit) → Set ⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁ extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ↺ c Bit} → ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁ extensional-reduction same-games = ]-[-cong (same-games 0b) (same-games 1b) module Guess (Power : Set) (coins : Power → Coins) (prgDist : PrgDist) where open PrgDist prgDist GuessAdv : Coins → Set GuessAdv c = ↺ c Bit runGuess⅁ : ∀ {ca} (A : GuessAdv ca) (b : Bit) → ↺ ca Bit runGuess⅁ A _ = A -- An oracle: an adversary who can break the guessing game. Oracle : Power → Set Oracle power = ∃ (λ (A : GuessAdv (coins power)) → breaks (runGuess⅁ A)) -- Any adversary cannot do better than a random guess. GuessSec : Power → Set GuessSec power = ∀ (A : GuessAdv (coins power)) → ¬(breaks (runGuess⅁ A)) record FlatFuns (Power : Set) {t} (T : Set t) : Set (L.suc t) where constructor mk field `Bits : ℕ → T `Bit : T _`×_ : T → T → T ⟨_⟩_↝_ : Power → T → T → Set infixr 2 _`×_ infix 0 ⟨_⟩_↝_ record PowerOps (Power : Set) : Set where constructor mk infixr 1 _>>>ᵖ_ infixr 3 _***ᵖ_ field idᵖ : Power _>>>ᵖ_ _***ᵖ_ : Power → Power → Power constPowerOps : PowerOps ⊤ constPowerOps = _ record FlatFunsOps {P t} {T : Set t} (powerOps : PowerOps P) (♭Funs : FlatFuns P T) : Set t where constructor mk open FlatFuns ♭Funs open PowerOps powerOps field idO : ∀ {A p} → ⟨ p ⟩ A ↝ A isIComposable : IComposable {I = ⊤} _>>>ᵖ_ ⟨_⟩_↝_ isVIComposable : VIComposable {I = ⊤} _***ᵖ_ _`×_ ⟨_⟩_↝_ open FlatFuns ♭Funs public open PowerOps powerOps public open IComposable isIComposable public open VIComposable isVIComposable public fun♭Funs : FlatFuns ⊤ Set fun♭Funs = mk Bits Bit _×_ (λ _ A B → A → B) fun♭Ops : FlatFunsOps _ fun♭Funs fun♭Ops = mk id funComp funVComp module AbsSemSec (|M| |C| : ℕ) {FunPower : Set} {t} {T : Set t} (♭Funs : FlatFuns FunPower T) where record Power : Set where constructor mk field p₀ p₁ : FunPower |R| : Coins coins = Power.|R| open FlatFuns ♭Funs M = `Bits |M| C = `Bits |C| record AbsSemSecAdv (p : Power) : Set where constructor mk open Power p field {|S|} : ℕ S = `Bits |S| R = `Bits |R| field step₀ : ⟨ p₀ ⟩ R ↝ (M `× M) `× S step₁ : ⟨ p₁ ⟩ C `× S ↝ `Bit SemSecReduction : ∀ (f : Power → Power) → Set SemSecReduction f = ∀ {p} → AbsSemSecAdv p → AbsSemSecAdv (f p) -- Here we use Agda functions for FlatFuns and ⊤ for power. module FunSemSec (prgDist : PrgDist) (|M| |C| : ℕ) where open PrgDist prgDist open AbsSemSec |M| |C| fun♭Funs open PowerOps constPowerOps open FlatFunsOps fun♭Ops M² = Bit → M Enc : ∀ cc → Set Enc cc = M → ↺ cc C Tr : (cc₀ cc₁ : Coins) → Set Tr cc₀ cc₁ = Enc cc₀ → Enc cc₁ FunSemSecAdv : Coins → Set FunSemSecAdv |R| = AbsSemSecAdv (mk _ _ |R|) module FunSemSecAdv {|R|} (A : FunSemSecAdv |R|) where open AbsSemSecAdv A public step₀F : R → (M² × S) step₀F = step₀ >>> proj *** idO step₀↺ : ↺ |R| (M² × S) step₀↺ = mk step₀F step₁F : S → C → Bit step₁F s c = step₁ (c , s) -- Returing 0 means Chal wins, Adv looses -- 1 means Adv wins, Chal looses runSemSec : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit runSemSec E A b = A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) } where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁) _⇄_ : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit _⇄_ = runSemSec runAdv : ∀ {|R|} → FunSemSecAdv |R| → C → Bits |R| → (M × M) × Bit runAdv (mk A-step₀ A-step₁) C = A-step₀ >>> id *** (const C &&& id >>> A-step₁) where open FunctionExtra using (_&&&_) _≗A_ : ∀ {p} (A₁ A₂ : FunSemSecAdv p) → Set A₀ ≗A A₁ = ∀ C R → runAdv A₀ C R ≡ runAdv A₁ C R change-adv : ∀ {cc ca} {E : Enc cc} {A₁ A₂ : FunSemSecAdv ca} → A₁ ≗A A₂ → (E ⇄ A₁) ≗⅁ (E ⇄ A₂) change-adv {ca = ca} {A₁ = _} {_} pf b R with splitAt ca R change-adv {E = E} {A₁} {A₂} pf b ._ | pre Σ., post , refl = trans (cong proj₂ (helper₀ A₁)) helper₂ where open FunSemSecAdv helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post) pre helper₂ = cong (λ m → step₁ A₂ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₂ pre))) (helper₀ A₂) SafeSemSecReduction : ∀ (f : Power → Power) {cc₀ cc₁} (E₀ : Enc cc₀) (E₁ : Enc cc₁) → Set SafeSemSecReduction f E₀ E₁ = ∃ λ (red : SemSecReduction f) → ∀ {p} A → (E₀ ⇄ A) ⇓ (E₁ ⇄ red {p} A) SemSecTr : ∀ {cc₀ cc₁} (f : Power → Power) (tr : Tr cc₀ cc₁) → Set SemSecTr {cc₀} f tr = ∀ {E : Enc cc₀} → SafeSemSecReduction f (tr E) E module PostCompSec (prgDist : PrgDist) (|M| |C| : ℕ) where module PostCompRed {FunPower : Set} {t} {T : Set t} {♭Funs : FlatFuns FunPower T} {funPowerOps : PowerOps FunPower} (♭ops : FlatFunsOps funPowerOps ♭Funs) where open FlatFunsOps ♭ops open AbsSemSec |M| |C| ♭Funs post-comp-red-power : FunPower → Power → Power post-comp-red-power p (mk p₀ p₁ |R|) = mk p₀ (p ***ᵖ idᵖ >>>ᵖ p₁) |R| post-comp-red : ∀ {p} (post-E : ⟨ p ⟩ C ↝ C) → SemSecReduction (post-comp-red-power p) post-comp-red post-E (mk A₀ A₁) = mk A₀ (post-E *** idO >>> A₁) open PrgDist prgDist open PostCompRed fun♭Ops open FlatFunsOps fun♭Ops open FunSemSec prgDist |M| |C| open AbsSemSec |M| |C| fun♭Funs post-comp : ∀ {cc} (post-E : C → C) → Tr cc cc post-comp post-E E = E >>> map↺ post-E post-comp-pres-sem-sec : ∀ {cc} (post-E : C → C) → SemSecTr id (post-comp {cc} post-E) post-comp-pres-sem-sec post-E = post-comp-red post-E , (λ _ → id) post-comp-pres-sem-sec' : ∀ (post-E post-E⁻¹ : C → C) (post-E-inv : post-E⁻¹ ∘ post-E ≗ id) {cc} {E : Enc cc} → SafeSemSecReduction id E (post-comp post-E E) post-comp-pres-sem-sec' post-E post-E⁻¹ post-E-inv {cc} {E} = red , helper where E' = post-comp post-E E red : SemSecReduction id red = post-comp-red post-E⁻¹ helper : ∀ {p} A → (E ⇄ A) ⇓ (E' ⇄ red {p} A) helper {p} A A-breaks-E = A'-breaks-E' where open FunSemSecAdv A renaming (step₀F to A₀F) A' = red {p} A same-games : (E ⇄ A) ≗⅁ (E' ⇄ A') same-games b R rewrite post-E-inv (run↺ (E (proj₁ (A₀F (take (coins p) R)) b)) (drop (coins p) R)) = refl A'-breaks-E' : breaks (E' ⇄ A') A'-breaks-E' = extensional-reduction same-games A-breaks-E post-neg : ∀ {cc} → Tr cc cc post-neg = post-comp vnot post-neg-pres-sem-sec : ∀ {cc} → SemSecTr id (post-neg {cc}) post-neg-pres-sem-sec {cc} {E} = post-comp-pres-sem-sec vnot {E} post-neg-pres-sem-sec' : ∀ {cc} {E : Enc cc} → SafeSemSecReduction id E (post-neg E) post-neg-pres-sem-sec' {cc} {E} = post-comp-pres-sem-sec' vnot vnot vnot∘vnot {cc} {E} open import diff import Data.Fin as Fin _]-_-[_ : ∀ {c} (f : ↺ c Bit) k (g : ↺ c Bit) → Set _]-_-[_ {c} f k g = diff (Fin.toℕ #⟨ run↺ f ⟩) (Fin.toℕ #⟨ run↺ g ⟩) ≥ 2^(c ∸ k) -- diff (#1 f) (#1 g) ≥ 2^(-k) * 2^ c -- diff (#1 f) (#1 g) ≥ ε * 2^ c -- dist (#1 f) (#1 g) ≥ ε * 2^ c -- where ε = 2^ -k -- {!dist (#1 f / 2^ c) (#1 g / 2^ c) > ε !} open import Data.Vec.NP using (count) ext-count : ∀ {n a} {A : Set a} {f g : A → Bool} → f ≗ g → (xs : Vec A n) → count f xs ≡ count g xs ext-count f≗g [] = refl ext-count f≗g (x ∷ xs) rewrite ext-count f≗g xs | f≗g x = refl ext-# : ∀ {c} {f g : Bits c → Bit} → f ≗ g → #⟨ f ⟩ ≡ #⟨ g ⟩ ext-# f≗g = ext-count f≗g (allBits _) ]-[-cong : ∀ {k c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]- k -[ f' → g ]- k -[ g' ]-[-cong f≗g f'≗g' f]-[f' rewrite ext-# f≗g | ext-# f'≗g' = f]-[f' module Concrete k where _]-[_ : ∀ {c} (f g : ↺ c Bit) → Set _]-[_ f g = f ]- k -[ g cong' : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' cong' = ]-[-cong {k} prgDist : PrgDist prgDist = mk _]-[_ cong' module Guess' = Guess Coins id prgDist module FunSemSec' = FunSemSec prgDist module PostCompSec' = PostCompSec prgDist
module otp-sem-sec where import Level as L open import Function open import Data.Nat.NP open import Data.Bits open import Data.Bool open import Data.Bool.Properties open import Data.Vec hiding (_>>=_) open import Data.Product.NP hiding (_⟦×⟧_) open import Relation.Nullary open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP open import flipbased-implem open ≡-Reasoning open import Data.Unit using (⊤) open import composable open import vcomp open import forkable open import flat-funs proj : ∀ {a} {A : Set a} → A × A → Bit → A proj (x₀ , x₁) 0b = x₀ proj (x₀ , x₁) 1b = x₁ module BitsExtra where splitAt′ : ∀ k {n} → Bits (k + n) → Bits k × Bits n splitAt′ k xs = case splitAt k xs of λ { (ys , zs , _) → (ys , zs) } vnot∘vnot : ∀ {n} → vnot {n} ∘ vnot ≗ id vnot∘vnot [] = refl vnot∘vnot (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot xs) open BitsExtra Coins = ℕ record PrgDist : Set₁ where constructor mk field _]-[_ : ∀ {c} (f g : ↺ c Bit) → Set ]-[-cong : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' breaks : ∀ {c} (EXP : Bit → ↺ c Bit) → Set breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b _≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ↺ c Bit) → Set ⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b ≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c}) ≗⅁-trans p q b R = trans (p b R) (q b R) -- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁ _⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ↺ c₀ Bit) (⅁₁ : Bit → ↺ c₁ Bit) → Set ⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁ extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ↺ c Bit} → ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁ extensional-reduction same-games = ]-[-cong (same-games 0b) (same-games 1b) module Guess (Power : Set) (coins : Power → Coins) (prgDist : PrgDist) where open PrgDist prgDist GuessAdv : Coins → Set GuessAdv c = ↺ c Bit runGuess⅁ : ∀ {ca} (A : GuessAdv ca) (b : Bit) → ↺ ca Bit runGuess⅁ A _ = A -- An oracle: an adversary who can break the guessing game. Oracle : Power → Set Oracle power = ∃ (λ (A : GuessAdv (coins power)) → breaks (runGuess⅁ A)) -- Any adversary cannot do better than a random guess. GuessSec : Power → Set GuessSec power = ∀ (A : GuessAdv (coins power)) → ¬(breaks (runGuess⅁ A)) module AbsSemSec (|M| |C| : ℕ) {t} {T : Set t} (♭Funs : FlatFuns T) where open FlatFuns ♭Funs M = `Bits |M| C = `Bits |C| record AbsSemSecAdv (|R| : Coins) : Set where constructor mk field {|S|} : ℕ S = `Bits |S| R = `Bits |R| field step₀ : R `→ (M `× M) `× S step₁ : C `× S `→ `Bit -- step₀ : ⟨ p₀ ⟩ R ↝ (M `× M) `× S -- step₁ : ⟨ p₁ ⟩ C `× S ↝ `Bit SemSecReduction : ∀ (f : Coins → Coins) → Set SemSecReduction f = ∀ {c} → AbsSemSecAdv c → AbsSemSecAdv (f c) -- Here we use Agda functions for FlatFuns. module FunSemSec (prgDist : PrgDist) (|M| |C| : ℕ) where open PrgDist prgDist open AbsSemSec |M| |C| fun♭Funs open FlatFunsOps fun♭Ops M² = Bit → M Enc : ∀ cc → Set Enc cc = M → ↺ cc C Tr : (cc₀ cc₁ : Coins) → Set Tr cc₀ cc₁ = Enc cc₀ → Enc cc₁ FunSemSecAdv : Coins → Set FunSemSecAdv = AbsSemSecAdv module FunSemSecAdv {|R|} (A : FunSemSecAdv |R|) where open AbsSemSecAdv A public step₀F : R → (M² × S) step₀F = step₀ >>> proj *** idO step₀↺ : ↺ |R| (M² × S) step₀↺ = mk step₀F step₁F : S → C → Bit step₁F s c = step₁ (c , s) -- Returing 0 means Chal wins, Adv looses -- 1 means Adv wins, Chal looses runSemSec : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit runSemSec E A b = A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) } where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁) _⇄_ : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit _⇄_ = runSemSec runAdv : ∀ {|R|} → FunSemSecAdv |R| → C → Bits |R| → (M × M) × Bit runAdv (mk A-step₀ A-step₁) C = A-step₀ >>> id *** (const C &&& id >>> A-step₁) _≗A_ : ∀ {p} (A₁ A₂ : FunSemSecAdv p) → Set A₀ ≗A A₁ = ∀ C R → runAdv A₀ C R ≡ runAdv A₁ C R change-adv : ∀ {cc ca} {E : Enc cc} {A₁ A₂ : FunSemSecAdv ca} → A₁ ≗A A₂ → (E ⇄ A₁) ≗⅁ (E ⇄ A₂) change-adv {ca = ca} {A₁ = _} {_} pf b R with splitAt ca R change-adv {E = E} {A₁} {A₂} pf b ._ | pre Σ., post , refl = trans (cong proj₂ (helper₀ A₁)) helper₂ where open FunSemSecAdv helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post) pre helper₂ = cong (λ m → step₁ A₂ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₂ pre))) (helper₀ A₂) SafeSemSecReduction : ∀ (f : Coins → Coins) {cc₀ cc₁} (E₀ : Enc cc₀) (E₁ : Enc cc₁) → Set SafeSemSecReduction f E₀ E₁ = ∃ λ (red : SemSecReduction f) → ∀ {c} A → (E₀ ⇄ A) ⇓ (E₁ ⇄ red {c} A) SemSecTr : ∀ {cc₀ cc₁} (f : Coins → Coins) (tr : Tr cc₀ cc₁) → Set SemSecTr {cc₀} f tr = ∀ {E : Enc cc₀} → SafeSemSecReduction f (tr E) E module PostCompSec (prgDist : PrgDist) (|M| |C| : ℕ) where module PostCompRed {t} {T : Set t} {♭Funs : FlatFuns T} (♭ops : FlatFunsOps ♭Funs) where open FlatFunsOps ♭ops open AbsSemSec |M| |C| ♭Funs post-comp-red : (post-E : C `→ C) → SemSecReduction _ post-comp-red post-E (mk A₀ A₁) = mk A₀ (post-E *** idO >>> A₁) open PrgDist prgDist open PostCompRed fun♭Ops open FlatFunsOps fun♭Ops open FunSemSec prgDist |M| |C| open AbsSemSec |M| |C| fun♭Funs post-comp : ∀ {cc} (post-E : C → C) → Tr cc cc post-comp post-E E = E >>> map↺ post-E post-comp-pres-sem-sec : ∀ {cc} (post-E : C → C) → SemSecTr id (post-comp {cc} post-E) post-comp-pres-sem-sec post-E = post-comp-red post-E , (λ _ → id) post-comp-pres-sem-sec' : ∀ (post-E post-E⁻¹ : C → C) (post-E-inv : post-E⁻¹ ∘ post-E ≗ id) {cc} {E : Enc cc} → SafeSemSecReduction id E (post-comp post-E E) post-comp-pres-sem-sec' post-E post-E⁻¹ post-E-inv {cc} {E} = red , helper where E' = post-comp post-E E red : SemSecReduction id red = post-comp-red post-E⁻¹ helper : ∀ {p} A → (E ⇄ A) ⇓ (E' ⇄ red {p} A) helper {c} A A-breaks-E = A'-breaks-E' where open FunSemSecAdv A renaming (step₀F to A₀F) A' = red {c} A same-games : (E ⇄ A) ≗⅁ (E' ⇄ A') same-games b R rewrite post-E-inv (run↺ (E (proj₁ (A₀F (take c R)) b)) (drop c R)) = refl A'-breaks-E' : breaks (E' ⇄ A') A'-breaks-E' = extensional-reduction same-games A-breaks-E post-neg : ∀ {cc} → Tr cc cc post-neg = post-comp vnot post-neg-pres-sem-sec : ∀ {cc} → SemSecTr id (post-neg {cc}) post-neg-pres-sem-sec {cc} {E} = post-comp-pres-sem-sec vnot {E} post-neg-pres-sem-sec' : ∀ {cc} {E : Enc cc} → SafeSemSecReduction id E (post-neg E) post-neg-pres-sem-sec' {cc} {E} = post-comp-pres-sem-sec' vnot vnot vnot∘vnot {cc} {E} open import diff import Data.Fin as Fin _]-_-[_ : ∀ {c} (f : ↺ c Bit) k (g : ↺ c Bit) → Set _]-_-[_ {c} f k g = diff (Fin.toℕ #⟨ run↺ f ⟩) (Fin.toℕ #⟨ run↺ g ⟩) ≥ 2^(c ∸ k) -- diff (#1 f) (#1 g) ≥ 2^(-k) * 2^ c -- diff (#1 f) (#1 g) ≥ ε * 2^ c -- dist (#1 f) (#1 g) ≥ ε * 2^ c -- where ε = 2^ -k -- {!dist (#1 f / 2^ c) (#1 g / 2^ c) > ε !} open import Data.Vec.NP using (count) ext-count : ∀ {n a} {A : Set a} {f g : A → Bool} → f ≗ g → (xs : Vec A n) → count f xs ≡ count g xs ext-count f≗g [] = refl ext-count f≗g (x ∷ xs) rewrite ext-count f≗g xs | f≗g x = refl ext-# : ∀ {c} {f g : Bits c → Bit} → f ≗ g → #⟨ f ⟩ ≡ #⟨ g ⟩ ext-# f≗g = ext-count f≗g (allBits _) ]-[-cong : ∀ {k c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]- k -[ f' → g ]- k -[ g' ]-[-cong f≗g f'≗g' f]-[f' rewrite ext-# f≗g | ext-# f'≗g' = f]-[f' module Concrete k where _]-[_ : ∀ {c} (f g : ↺ c Bit) → Set _]-[_ f g = f ]- k -[ g cong' : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' cong' = ]-[-cong {k} prgDist : PrgDist prgDist = mk _]-[_ cong' module Guess' = Guess Coins id prgDist module FunSemSec' = FunSemSec prgDist module PostCompSec' = PostCompSec prgDist
Cut out power
Cut out power
Agda
bsd-3-clause
crypto-agda/crypto-agda
f97ff6f67a4cac4360ae09731c7f0ab22eca83b7
src/Holes/Test/Limited.agda
src/Holes/Test/Limited.agda
module Holes.Test.Limited where open import Holes.Prelude hiding (_⊛_) open import Holes.Util open import Holes.Term open import Holes.Cong.Limited open import Agda.Builtin.Equality using (_≡_; refl) module Contrived where infixl 5 _⊕_ infixl 6 _⊛_ infix 4 _≈_ -- A type of expression trees for natural number calculations data Expr : Set where zero : Expr succ : Expr → Expr _⊕_ _⊛_ : Expr → Expr → Expr -- An unsophisticated 'equality' relation on the expression trees data _≈_ : Expr → Expr → Set where zero-cong : zero ≈ zero succ-cong : ∀ {m n} → m ≈ n → succ m ≈ succ n ⊕-cong : ∀ {a a′ b b′} → a ≈ a′ → b ≈ b′ → a ⊕ b ≈ a′ ⊕ b′ ⊛-cong : ∀ {a a′ b b′} → a ≈ a′ → b ≈ b′ → a ⊛ b ≈ a′ ⊛ b′ ⊕-comm : ∀ a b → a ⊕ b ≈ b ⊕ a -- Some lemmas that are necessary to proceed ≈-refl : ∀ {n} → n ≈ n ≈-refl {zero} = zero-cong ≈-refl {succ n} = succ-cong ≈-refl ≈-refl {m ⊕ n} = ⊕-cong ≈-refl ≈-refl ≈-refl {m ⊛ n} = ⊛-cong ≈-refl ≈-refl {- Each congruence in the database must have a type with the same shape as the below, following these rules: - A congruence `c` is for a single _top-level_ function `f` - `c` may only be a congruence for _one_ of `f`'s arguments - Each explicit argument to `f` must be an explicit argument to `c` - The 'rewritten' version of the changing argument must follow as an implicit argument to `c` - The equation to do the local rewriting must be the next explicit argument -} open CongSplit _≈_ ≈-refl ⊕-cong₁ = two→one₁ ⊕-cong ⊕-cong₂ = two→one₂ ⊕-cong ⊛-cong₁ = two→one₁ ⊛-cong ⊛-cong₂ = two→one₂ ⊛-cong succ-cong′ : ∀ n {n′} → n ≈ n′ → succ n ≈ succ n′ succ-cong′ _ = succ-cong {- Create the database of congruences. - This is a list of (Name × ArgPlace × Congruence) triples. - The `Name` is the (reflected) name of the function to which the congruence applies. - The `ArgPlace` is the index of the argument place that the congruence can rewrite at. - The `Congruence` is a reflected copy of the congruence function, of type `Term` -} database = (quote _⊕_ , 0 , quote ⊕-cong₁) ∷ (quote _⊕_ , 1 , quote ⊕-cong₂) ∷ (quote _⊛_ , 0 , quote ⊛-cong₁) ∷ (quote _⊛_ , 1 , quote ⊛-cong₂) ∷ (quote succ , 0 , quote succ-cong′) ∷ [] open AutoCong database test1 : ∀ a b → succ ⌞ a ⊕ b ⌟ ≈ succ (b ⊕ a) test1 a b = cong! (⊕-comm a b) test2 : ∀ a b c → succ (⌞ a ⊕ b ⌟ ⊕ c) ≈ succ (b ⊕ a ⊕ c) test2 a b c = cong! (⊕-comm a b) test3 : ∀ a b c → succ (a ⊕ ⌞ b ⊕ c ⌟) ≈ succ (a ⊕ (c ⊕ b)) test3 a b c = cong! (⊕-comm b c) -- We can use a proof obtained by pattern matching for `cong!` test5 : ∀ a b c → a ≈ b ⊕ c × b ≈ succ c → succ (⌞ b ⌟ ⊕ c ⊛ a) ≈ succ (succ c ⊕ c ⊛ a) test5 a b c (eq1 , eq2) = cong! eq2 record ∃ {a p} {A : Set a} (P : A → Set p) : Set (a ⊔ p) where constructor _,_ field evidence : A proof : P evidence -- We can use `cong!` to prove some part of the result even when it depends on -- other parts of the result test6 : ∀ a b c → b ⊕ c ≈ a × b ≈ succ c → ∃ λ x → succ (⌞ b ⊕ x ⌟ ⊕ a) ≈ succ (a ⊕ a) test6 a b c (eq1 , eq2) = c , cong! eq1 module Propositional (+-comm : ∀ a b → a + b ≡ b + a) where +-cong₁ : ∀ a b {a′} → a ≡ a′ → a + b ≡ a′ + b +-cong₁ _ _ refl = refl +-cong₂ : ∀ a b {b′} → b ≡ b′ → a + b ≡ a + b′ +-cong₂ _ _ refl = refl suc-cong : ∀ n {n′} → n ≡ n′ → suc n ≡ suc n′ suc-cong _ refl = refl database = (quote _+_ , 0 , quote +-cong₁) ∷ (quote _+_ , 1 , quote +-cong₂) ∷ (quote suc , 0 , quote suc-cong) ∷ [] open AutoCong database test1 : ∀ a b → suc ⌞ a + b ⌟ ≡ suc (b + a) test1 a b = cong! (+-comm a b) test2 : ∀ a b c → suc (⌞ a + b ⌟ + c) ≡ suc (b + a + c) test2 a b c = cong! (+-comm a b) test3 : ∀ a b c → suc (a + ⌞ b + c ⌟) ≡ suc (a + (c + b)) test3 a b c = cong! (+-comm b c) test4 : ∀ a b c a′ → a ≡ a′ → suc (⌞ a ⌟ + b + c) ≡ suc (a′ + b + c) test4 _ _ _ _ eq = cong! eq
module Holes.Test.Limited where open import Holes.Prelude hiding (_⊛_) open import Holes.Util open import Holes.Term open import Holes.Cong.Limited open import Agda.Builtin.Equality using (_≡_; refl) module Contrived where infixl 5 _⊕_ infixl 6 _⊛_ infix 4 _≈_ -- A type of expression trees for natural number calculations data Expr : Set where zero : Expr succ : Expr → Expr _⊕_ _⊛_ : Expr → Expr → Expr -- An unsophisticated 'equality' relation on the expression trees data _≈_ : Expr → Expr → Set where zero-cong : zero ≈ zero succ-cong : ∀ {m n} → m ≈ n → succ m ≈ succ n ⊕-cong : ∀ {a a′ b b′} → a ≈ a′ → b ≈ b′ → a ⊕ b ≈ a′ ⊕ b′ ⊛-cong : ∀ {a a′ b b′} → a ≈ a′ → b ≈ b′ → a ⊛ b ≈ a′ ⊛ b′ ⊕-comm : ∀ a b → a ⊕ b ≈ b ⊕ a -- Some lemmas that are necessary to proceed ≈-refl : ∀ {n} → n ≈ n ≈-refl {zero} = zero-cong ≈-refl {succ n} = succ-cong ≈-refl ≈-refl {m ⊕ n} = ⊕-cong ≈-refl ≈-refl ≈-refl {m ⊛ n} = ⊛-cong ≈-refl ≈-refl {- Each congruence in the database must have a type with the same shape as the below, following these rules: - A congruence `c` is for a single _top-level_ function `f` - `c` may only be a congruence for _one_ of `f`'s arguments - Each explicit argument to `f` must be an explicit argument to `c` - The 'rewritten' version of the changing argument must follow as an implicit argument to `c` - The equation to do the local rewriting must be the next explicit argument -} open CongSplit _≈_ ≈-refl ⊕-cong₁ = two→one₁ ⊕-cong ⊕-cong₂ = two→one₂ ⊕-cong ⊛-cong₁ = two→one₁ ⊛-cong ⊛-cong₂ = two→one₂ ⊛-cong succ-cong′ : ∀ n {n′} → n ≈ n′ → succ n ≈ succ n′ succ-cong′ _ = succ-cong {- Create the database of congruences. - This is a list of (Name × ArgPlace × Congruence) triples. - The `Name` is the (reflected) name of the function to which the congruence applies. - The `ArgPlace` is the index of the argument place that the congruence can rewrite at. - The `Congruence` is a reflected copy of the congruence function, of type `Term` -} database = (quote _⊕_ , 0 , quote ⊕-cong₁) ∷ (quote _⊕_ , 1 , quote ⊕-cong₂) ∷ (quote _⊛_ , 0 , quote ⊛-cong₁) ∷ (quote _⊛_ , 1 , quote ⊛-cong₂) ∷ (quote succ , 0 , quote succ-cong′) ∷ [] open AutoCong database test1 : ∀ a b → succ ⌞ a ⊕ b ⌟ ≈ succ (b ⊕ a) test1 a b = cong! (⊕-comm a b) test2 : ∀ a b c → succ (⌞ a ⊕ b ⌟ ⊕ c) ≈ succ (b ⊕ a ⊕ c) test2 a b c = cong! (⊕-comm a b) test3 : ∀ a b c → succ (a ⊕ ⌞ b ⊕ c ⌟) ≈ succ (a ⊕ (c ⊕ b)) test3 a b c = cong! (⊕-comm b c) -- We can use a proof obtained by pattern matching for `cong!` test5 : ∀ a b c → a ≈ b ⊕ c × b ≈ succ c → succ (⌞ b ⌟ ⊕ c ⊛ a) ≈ succ (succ c ⊕ c ⊛ a) test5 a b c (eq1 , eq2) = cong! eq2 record ∃ {a p} {A : Set a} (P : A → Set p) : Set (a ⊔ p) where constructor _,_ field evidence : A proof : P evidence -- We can use `cong!` to prove some part of the result even when it depends on -- other parts of the result test6 : ∀ a b c → b ⊕ c ≈ a × b ≈ succ c → ∃ λ x → succ (⌞ b ⊕ x ⌟ ⊕ a) ≈ succ (a ⊕ a) test6 a b c (eq1 , eq2) = c , cong! eq1 -- Deep nesting test7 : ∀ a b c → succ (a ⊕ succ (b ⊕ succ (c ⊕ succ (a ⊕ succ ⌞ b ⊕ c ⌟)))) ≈ succ (a ⊕ succ (b ⊕ succ (c ⊕ succ (a ⊕ succ ⌞ c ⊕ b ⌟)))) test7 _ b c = cong! (⊕-comm b c) module Propositional (+-comm : ∀ a b → a + b ≡ b + a) where +-cong₁ : ∀ a b {a′} → a ≡ a′ → a + b ≡ a′ + b +-cong₁ _ _ refl = refl +-cong₂ : ∀ a b {b′} → b ≡ b′ → a + b ≡ a + b′ +-cong₂ _ _ refl = refl suc-cong : ∀ n {n′} → n ≡ n′ → suc n ≡ suc n′ suc-cong _ refl = refl database = (quote _+_ , 0 , quote +-cong₁) ∷ (quote _+_ , 1 , quote +-cong₂) ∷ (quote suc , 0 , quote suc-cong) ∷ [] open AutoCong database test1 : ∀ a b → suc ⌞ a + b ⌟ ≡ suc (b + a) test1 a b = cong! (+-comm a b) test2 : ∀ a b c → suc (⌞ a + b ⌟ + c) ≡ suc (b + a + c) test2 a b c = cong! (+-comm a b) test3 : ∀ a b c → suc (a + ⌞ b + c ⌟) ≡ suc (a + (c + b)) test3 a b c = cong! (+-comm b c) test4 : ∀ a b c a′ → a ≡ a′ → suc (⌞ a ⌟ + b + c) ≡ suc (a′ + b + c) test4 _ _ _ _ eq = cong! eq
Add a new test
Add a new test
Agda
mit
bch29/agda-holes
ee97a3e6749a3dd5f7faa559198900ab683eb47d
README.agda
README.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Machine-checked formalization of the theoretical results presented -- in the paper: -- -- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann. -- A Theory of Changes for Higher-Order Languages: -- Incrementalizing λ-Calculi by Static Differentiation. -- To appear at PLDI, ACM 2014. -- -- We claim that this formalization -- -- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper, -- (2) formally specifies the interface between our incrementalization -- framework and potential plugins, and -- (3) shows that the plugin interface can be instantiated. -- -- The first claim is the main reason for a machine-checked -- proof: We want to be sure that we got the proofs right. -- -- The second claim is about reusability and applicability: Only -- a clearly defined interface allows other researchers to -- provide plugins for our framework. -- -- The third claim is to show that the plugin interface is -- consistent: An inconsistent plugin interface would allow to -- prove arbitrary results in the framework. ------------------------------------------------------------------------ module README where -- We know two good ways to read this code base. You can either -- use Emacs with agda2-mode to interact with the source files, -- or you can use a web browser to view the pretty-printed and -- hyperlinked source files. For Agda power users, we also -- include basic setup information for your own machine. -- IF YOU WANT TO USE A BROWSER -- ============================ -- -- Start with *HTML* version of this readme. On the AEC -- submission website (online or inside the VM), follow the link -- "view the Agda code in their browser" to the file -- agda/README.html. -- -- The source code is syntax highlighted and hyperlinked. You can -- click on module names to open the corresponding files, or you -- can click on identifiers to jump to their definition. In -- general, a Agda file with name `Foo/Bar/Baz.agda` contains a -- module `Foo.Bar.Baz` and is shown in an HTML file -- `Foo.Bar.Baz.html`. -- -- Note that we also include the HTML files generated for our -- transitive dependencies from the Agda standard library. This -- allows you to follow hyperlinks to the Agda standard -- library. It is the default behavior of `agda --html` which we -- used to generate the HTML. -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE EMACS -- ======================== -- -- Open this file in Emacs with agda2-mode installed. See below -- for which Agda version you need to install. On the VM image, -- everything is setup for you and you can just open this file in -- Emacs. -- -- C-c C-l Load code. Type checks and syntax highlights. Use -- this if the code is not syntax highlighted, or -- after you changed something that you want to type -- check. -- -- M-. Jump to definition of identifier at the point. -- M-* Jump back. -- -- Note that README.agda imports Everything.agda which imports -- every Agda file in our formalization. So if README.agda type -- checks successfully, everything does. If you want to type -- check everything from scratch, delete the *.agdai files to -- disable separate compilation. You can use "find . -name -- '*.agdai' | xargs rm" to do that. -- -- More information on the Agda mode is available on the Agda wiki: -- -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE YOUR OWN SETUP -- ================================= -- -- To typecheck this formalization, you need to install the appropriate version -- of Agda, the Agda standard library (version 0.7), generate Everything.agda -- with the attached Haskell helper, and finally run Agda on this file. -- -- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh -- script and following instructions given on output will eventually generate -- Everything.agda and proceed to type check everything on command line. -- -- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but -- has some bugs with serialization of code using some recent syntactic sugar -- which we use (https://code.google.com/p/agda/issues/detail?id=756), so it -- might work or not. When it does not, removing Agda caches (.agdai files) -- appears to often help. You can use "find . -name '*.agdai' | xargs rm" to do -- that. -- -- If you're not an Agda power user, it is probably easier to use -- the VM image or look at the pretty-printed and hyperlinked -- HTML files, see above. -- WHERE TO START READING? -- ======================= -- -- modules.pdf -- The graph of dependencies between Agda modules. -- Good if you want to get a broad overview. -- -- README.agda -- This file. A coarse-grained introduction to the Agda -- formalization. Good if you want to begin at the beginning -- and understand the structure of our code. -- -- PLDI14-List-of-Theorems.agda -- Pointers to the Agda formalizations of all theorems, lemmas -- or definitions in the PLDI paper. Good if you want to read -- the paper and the Agda code side by side. -- -- Here is an import of this file, so you can jump to it -- directly (use M-. in Emacs or click the module name in the -- Browser): import PLDI14-List-of-Theorems -- Everything.agda -- Imports every Agda module in our formalization. Good if you -- want to make sure you don't miss anything. -- -- Again, here's is an import of this file so you can navigate -- there: import Everything -- (This import is also important to ensure that if we typecheck -- README.agda, we also typecheck every other file of our -- formalization). -- THE AGDA CODE -- ============= import Postulate.Extensionality import Base.Data.DependentList -- Variables and contexts import Base.Syntax.Context -- Sets of variables import Base.Syntax.Vars import Base.Denotation.Notation -- Environments import Base.Denotation.Environment -- Change contexts import Base.Change.Context -- # Base, parametric proof. -- -- This is for a parametric calculus where: -- types are parametric in base types -- terms are parametric in constants -- -- -- Modules are ordered and grouped according to what they represent. -- ## Definitions import Parametric.Syntax.Type import Parametric.Syntax.Term import Parametric.Denotation.Value import Parametric.Denotation.Evaluation import Parametric.Change.Type import Parametric.Change.Term import Parametric.Change.Derive import Parametric.Change.Value import Parametric.Change.Evaluation -- ## Proofs import Parametric.Change.Validity import Parametric.Change.Specification import Parametric.Change.Implementation import Parametric.Change.Correctness -- # Nehemiah plugin -- -- The structure is the same as the parametric proof (down to the -- order and the grouping of modules), except for the postulate module. -- Postulate an abstract data type for integer Bags. import Postulate.Bag-Nehemiah -- ## Definitions import Nehemiah.Syntax.Type import Nehemiah.Syntax.Term import Nehemiah.Denotation.Value import Nehemiah.Denotation.Evaluation import Nehemiah.Change.Term import Nehemiah.Change.Type import Nehemiah.Change.Derive import Nehemiah.Change.Value import Nehemiah.Change.Evaluation -- ## Proofs import Nehemiah.Change.Validity import Nehemiah.Change.Specification import Nehemiah.Change.Implementation import Nehemiah.Change.Correctness
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Machine-checked formalization of the theoretical results presented -- in the paper: -- -- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann. -- A Theory of Changes for Higher-Order Languages: -- Incrementalizing λ-Calculi by Static Differentiation. -- To appear at PLDI, ACM 2014. -- -- We claim that this formalization -- -- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper, -- (2) formally specifies the interface between our incrementalization -- framework and potential plugins, and -- (3) shows that the plugin interface can be instantiated. -- -- The first claim is the main reason for a machine-checked -- proof: We want to be sure that we got the proofs right. -- -- The second claim is about reusability and applicability: Only -- a clearly defined interface allows other researchers to -- provide plugins for our framework. -- -- The third claim is to show that the plugin interface is -- consistent: An inconsistent plugin interface would allow to -- prove arbitrary results in the framework. ------------------------------------------------------------------------ module README where -- We know two good ways to read this code base. You can either -- use Emacs with agda2-mode to interact with the source files, -- or you can use a web browser to view the pretty-printed and -- hyperlinked source files. For Agda power users, we also -- include basic setup information for your own machine. -- IF YOU WANT TO USE A BROWSER -- ============================ -- -- Start with *HTML* version of this readme. On the AEC -- submission website (online or inside the VM), follow the link -- "view the Agda code in their browser" to the file -- agda/README.html. -- -- The source code is syntax highlighted and hyperlinked. You can -- click on module names to open the corresponding files, or you -- can click on identifiers to jump to their definition. In -- general, a Agda file with name `Foo/Bar/Baz.agda` contains a -- module `Foo.Bar.Baz` and is shown in an HTML file -- `Foo.Bar.Baz.html`. -- -- Note that we also include the HTML files generated for our -- transitive dependencies from the Agda standard library. This -- allows you to follow hyperlinks to the Agda standard -- library. It is the default behavior of `agda --html` which we -- used to generate the HTML. -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE EMACS -- ======================== -- -- Open this file in Emacs with agda2-mode installed. See below -- for which Agda version you need to install. On the VM image, -- everything is setup for you and you can just open this file in -- Emacs. -- -- C-c C-l Load code. Type checks and syntax highlights. Use -- this if the code is not syntax highlighted, or -- after you changed something that you want to type -- check. -- -- M-. Jump to definition of identifier at the point. -- M-* Jump back. -- -- Note that README.agda imports Everything.agda which imports -- every Agda file in our formalization. So if README.agda type -- checks successfully, everything does. If you want to type -- check everything from scratch, delete the *.agdai files to -- disable separate compilation. You can use "find . -name -- '*.agdai' | xargs rm" to do that. -- -- More information on the Agda mode is available on the Agda wiki: -- -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE YOUR OWN SETUP -- ================================= -- -- To typecheck this formalization, you need to install the appropriate version -- of Agda, the Agda standard library (version 0.7), generate Everything.agda -- with the attached Haskell helper, and finally run Agda on this file. -- -- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh -- script and following instructions given on output will eventually generate -- Everything.agda and proceed to type check everything on command line. -- -- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but -- has some bugs with serialization of code using some recent syntactic sugar -- which we use (https://code.google.com/p/agda/issues/detail?id=756), so it -- might work or not. When it does not, removing Agda caches (.agdai files) -- appears to often help. You can use "find . -name '*.agdai' | xargs rm" to do -- that. -- -- If you're not an Agda power user, it is probably easier to use -- the VM image or look at the pretty-printed and hyperlinked -- HTML files, see above. -- WHERE TO START READING? -- ======================= -- -- modules.pdf -- The graph of dependencies between Agda modules. -- Good if you want to get a broad overview. -- -- README.agda -- This file. A coarse-grained introduction to the Agda -- formalization. Good if you want to begin at the beginning -- and understand the structure of our code. -- -- PLDI14-List-of-Theorems.agda -- Pointers to the Agda formalizations of all theorems, lemmas -- or definitions in the PLDI paper. Good if you want to read -- the paper and the Agda code side by side. -- -- Here is an import of this file, so you can jump to it -- directly (use M-. in Emacs or click the module name in the -- Browser): import PLDI14-List-of-Theorems -- Everything.agda -- Imports every Agda module in our formalization. Good if you -- want to make sure you don't miss anything. -- -- Again, here's is an import of this file so you can navigate -- there: import Everything -- (This import is also important to ensure that if we typecheck -- README.agda, we also typecheck every other file of our -- formalization). -- THE AGDA CODE -- ============= -- -- The formalization has four parts: -- -- 1. A formalization of change structures. This lives in -- Base.Change.Algebra. (What we call "change structure" in the -- paper, we call "change algebra" in the Agda code. We changed -- the name when writing the paper, and never got around to -- updating the name in the Agda code). -- -- 2. Incrementalization framework for first-class functions, -- with extension points for plugging in data types and their -- incrementalization. This lives in the Parametric.* -- hierarchy. -- -- 3. An example plugin that provides integers and bags -- with negative multiplicity. This lives in the Nehemiah.* -- hierarchy. (For some reason, we choose to call this -- particular incarnation of the plugin Nehemiah). -- -- 4. Other material that is unrelated to the framework/plugin -- distinction. This is all other files. -- FORMALIZATION OF CHANGE STRUCTURES -- ================================== -- -- Section 2 of the paper, and Base.Change.Algebra in Agda. import Base.Change.Algebra -- INCREMENTALIZATION FRAMEWORK -- ============================ -- -- Section 3 of the paper, and Parametric.* hierarchy in Agda. -- -- The extension points are implemented as module parameters. See -- detailed explanation in Parametric.Syntax.Type and -- Parametric.Syntax.Term. Some extension points are for types, -- some for values, and some for proof fragments. In Agda, these -- three kinds of entities are unified anyway, so we can encode -- all of them as module parameters. -- -- Every module in the Parametric.* hierarchy adds at least one -- extension point, so the module hierarchy of a plugin will -- typically mirror the Parametric.* hierarchy, defining the -- values for these extension points. -- -- Firstly, we have the syntax of types and terms, the erased -- change structure for function types and incrementalization as -- a term-to-term transformation. The contents of these modules -- formalizes Sec. 3.1 and 3.2 of the paper, except for the -- second and third line of Figure 3, which is formalized further -- below. import Parametric.Syntax.Type -- syntax of types import Parametric.Syntax.Term -- syntax of terms import Parametric.Change.Type -- simply-typed changes import Parametric.Change.Derive -- incrementalization -- Secondly, we define the usual denotational semantics of the -- simply-typed lambda calculus in terms of total functions, and -- the change semantics in terms of a change structure on total -- functions. The contents of these modules formalize Sec. 3.3, -- 3.4 and 3.5 of the paper. import Parametric.Denotation.Value -- standard values import Parametric.Denotation.Evaluation -- standard evaluation import Parametric.Change.Validity -- dependently-typed changes import Parametric.Change.Specification -- change evaluation -- Thirdly, we define terms that operate on simply-typed changes, -- and connect them to their values. The concents of these -- modules formalize the second and third line of Figure 3, as -- well as the semantics of these lines. import Parametric.Change.Term -- terms that operate on simply-typed changes import Parametric.Change.Value -- the values of these terms import Parametric.Change.Evaluation -- connecting the terms and their values -- Finally, we prove correctness by connecting the (syntactic) -- incrementalization to the (semantic) change evaluation by a -- logical relation, and a proof that the values of terms -- produced by the incrementalization transformation are related -- to the change values of the original terms. The contents of -- these modules formalize Sec. 3.6. import Parametric.Change.Implementation -- logical relation import Parametric.Change.Correctness -- main correctness proof -- EXAMPLE PLUGIN -- ============== -- -- Sec. 3.7 in the paper, and the Nehemiah.* hierarchy in Agda. -- -- The module structure of the plugin follows the structure of -- the Parametric.* hierarchy. For example, the extension point -- defined in Parametric.Syntax.Term is instantiated in -- Nehemiah.Syntax.Term. -- -- As discussed in Sec. 3.7 of the paper, the point of this -- plugin is not to speed up any real programs, but to show "that -- the interface for proof plugins can be implemented". As a -- first step towards proving correctness of the more complicated -- plugin with integers, bags and finite maps we implement in -- Scala, we choose to define plugin with integers and bags in -- Agda. Instead of implementing bags (with negative -- multiplicities, like in the paper) in Agda, though, we -- postulate that a group of such bags exist. Note that integer -- bags with integer multiplicities are actually the free group -- given a singleton operation `Integer -> Bag`, so this should -- be easy to formalize in principle. -- Before we start with the plugin, we postulate an abstract data -- type for integer bags. import Postulate.Bag-Nehemiah -- Firstly, we extend the syntax of types and terms, the erased -- change structure for function types, and incrementalization as -- a term-to-term transformation to account for the data types of -- the Nehemiah language. The contents of these modules -- instantiate the extension points in Sec. 3.1 and 3.2 of the -- paper, except for the second and third line of Figure 3, which -- is instantiated further below. import Nehemiah.Syntax.Type import Nehemiah.Syntax.Term import Nehemiah.Change.Type import Nehemiah.Change.Derive -- Secondly, we extend the usual denotational semantics and the -- change semantics to account for the data types of the Nehemiah -- language. The contents of these modules instantiate the -- extension points in Sec. 3.3, 3.4 and 3.5 of the paper. import Nehemiah.Denotation.Value import Nehemiah.Denotation.Evaluation import Nehemiah.Change.Validity import Nehemiah.Change.Specification -- Thirdly, we extend the terms that operate on simply-typed -- changes, and the connection to their values to account for the -- data types of the Nehemiah language. The concents of these -- modules instantiate the extension points in the second and -- third line of Figure 3. import Nehemiah.Change.Term import Nehemiah.Change.Value import Nehemiah.Change.Evaluation -- Finally, we extend the logical relation and the main -- correctness proof to account for the data types in the -- Nehemiah language. The contents of these modules instantiate -- the extension points defined in Sec. 3.6. import Nehemiah.Change.Implementation import Nehemiah.Change.Correctness -- OTHER MATERIAL -- ============== -- -- We postulate extensionality of Agda functions. This postulate -- is well known to be compatible with Agda's type theory. import Postulate.Extensionality -- For historical reasons, we reexport Data.List.All from the -- standard library under the name DependentList. import Base.Data.DependentList -- This module supports overloading the ⟦_⟧ notation based on -- Agda's instance arguments. import Base.Denotation.Notation -- These modules implement contexts including typed de Bruijn -- indices to represent bound variables, sets of bound variables, -- and environments. These modules are parametric in the set of -- types (that are stored in contexts) and the set of values -- (that are stored in environments). So these modules are even -- more parametric than the Parametric.* hierarchy. import Base.Syntax.Context import Base.Syntax.Vars import Base.Denotation.Environment -- This module contains some helper definitions to merge a -- context of values and a context of changes. import Base.Change.Context
Revise walkthrough in README.agda (for #275).
Revise walkthrough in README.agda (for #275). Old-commit-hash: 97b77b8c0bb96fc3d4df29ee914a158019dd3cdd
Agda
mit
inc-lc/ilc-agda
11413469714bba635b972f40607b808e31925650
Syntax/Term/Plotkin.agda
Syntax/Term/Plotkin.agda
import Syntax.Type.Plotkin as Type import Syntax.Context as Context module Syntax.Term.Plotkin {B : Set {- of base types -}} {C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}} where -- Terms of languages described in Plotkin style open import Function using (_∘_) open import Data.Product open Type B open Context {Type} open import Denotation.Environment Type open import Syntax.Context.Plotkin B -- Declarations of Term and Terms to enable mutual recursion data Term (Γ : Context) : (τ : Type) → Set data Terms (Γ : Context) : (Σ : Context) → Set -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where const : ∀ {Σ τ} → (c : C Σ τ) → Terms Γ Σ → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. data Terms Γ where ∅ : Terms Γ ∅ _•_ : ∀ {τ Σ} → Term Γ τ → Terms Γ Σ → Terms Γ (τ • Σ) infixr 9 _•_ -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ) lift-diff-apply diff apply {base ι} = diff , apply lift-diff-apply diff apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) lift-diff diff apply = λ {τ Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) lift-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ) lift-apply diff apply = λ {τ Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weakenAll : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weakenAll Γ₁≼Γ₂ ∅ = ∅ weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant)
import Syntax.Type.Plotkin as Type import Syntax.Context as Context module Syntax.Term.Plotkin {B : Set {- of base types -}} {C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}} where -- Terms of languages described in Plotkin style open import Function using (_∘_) open import Data.Product open Type B open Context {Type} open import Syntax.Context.Plotkin B -- Declarations of Term and Terms to enable mutual recursion data Term (Γ : Context) : (τ : Type) → Set data Terms (Γ : Context) : (Σ : Context) → Set -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where const : ∀ {Σ τ} → (c : C Σ τ) → Terms Γ Σ → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. data Terms Γ where ∅ : Terms Γ ∅ _•_ : ∀ {τ Σ} → Term Γ τ → Terms Γ Σ → Terms Γ (τ • Σ) infixr 9 _•_ -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ) lift-diff-apply diff apply {base ι} = diff , apply lift-diff-apply diff apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) lift-diff diff apply = λ {τ Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) lift-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ) lift-apply diff apply = λ {τ Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weakenAll : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weakenAll Γ₁≼Γ₂ ∅ = ∅ weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant)
Remove spurious imports
Remove spurious imports Old-commit-hash: fc57d2d6ee3ff6f548c16e7073aeb6ae12646d85
Agda
mit
inc-lc/ilc-agda
1832c6af3f90a6b9fc8c06181cccfcb95932c5fc
lib/Relation/Binary/NP.agda
lib/Relation/Binary/NP.agda
{-# OPTIONS --universe-polymorphism #-} module Relation.Binary.NP where open import Level open import Relation.Binary public import Relation.Binary.PropositionalEquality as PropEq -- could this be moved in place of Trans is Relation.Binary.Core Trans' : ∀ {a b c ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : Set b} {C : Set c} → REL A B ℓ₁ → REL B C ℓ₂ → REL A C ℓ₃ → Set _ Trans' P Q R = ∀ {i j k} (x : P i j) (xs : Q j k) → R i k substitutive-to-reflexive : ∀ {a ℓ ℓ'} {A : Set a} {≈ : Rel A ℓ} {≡ : Rel A ℓ'} → Substitutive ≡ ℓ → Reflexive ≈ → ≡ ⇒ ≈ substitutive-to-reflexive {≈ = ≈} ≡-subst ≈-refl xᵣ = ≡-subst (≈ _) xᵣ ≈-refl substitutive⇒≡ : ∀ {a ℓ} {A : Set a} {≡ : Rel A ℓ} → Substitutive ≡ a → ≡ ⇒ PropEq._≡_ substitutive⇒≡ subst = substitutive-to-reflexive {≈ = PropEq._≡_} subst PropEq.refl record Equality {a ℓ} {A : Set a} (_≡_ : Rel A ℓ) : Set (suc a ⊔ ℓ) where field isEquivalence : IsEquivalence _≡_ subst : Substitutive _≡_ a open IsEquivalence isEquivalence public to-reflexive : ∀ {≈} → Reflexive ≈ → _≡_ ⇒ ≈ to-reflexive = substitutive-to-reflexive subst to-propositional : _≡_ ⇒ PropEq._≡_ to-propositional = substitutive⇒≡ subst -- Equational reasoning combinators. module Trans-Reasoning {a ℓ} {A : Set a} (_≈_ : Rel A ℓ) (trans : Transitive _≈_) where infix 2 finally infixr 2 _≈⟨_⟩_ _≈⟨_⟩_ : ∀ x {y z : A} → x ≈ y → y ≈ z → x ≈ z _ ≈⟨ x≈y ⟩ y≈z = trans x≈y y≈z -- When there is no reflexivty available this -- combinator can be used to end the reasoning. finally : ∀ (x y : A) → x ≈ y → x ≈ y finally _ _ x≈y = x≈y syntax finally x y x≈y = x ≈⟨ x≈y ⟩∎ y ∎ module Equivalence-Reasoning {a ℓ} {A : Set a} {_≈_ : Rel A ℓ} (E : IsEquivalence _≈_) where open IsEquivalence E open Trans-Reasoning _≈_ trans public hiding (finally) infix 2 _∎ _∎ : ∀ x → x ≈ x _ ∎ = refl module Preorder-Reasoning {p₁ p₂ p₃} (P : Preorder p₁ p₂ p₃) where open Preorder P open Equivalence-Reasoning isEquivalence public renaming (_≈⟨_⟩_ to _∼⟨_⟩_) module Setoid-Reasoning {a ℓ} (s : Setoid a ℓ) where open Preorder-Reasoning (Setoid.preorder s) public renaming (_∼⟨_⟩_ to _≈⟨_⟩_)
{-# OPTIONS --universe-polymorphism #-} module Relation.Binary.NP where open import Level open import Relation.Binary public import Relation.Binary.PropositionalEquality as PropEq -- could this be moved in place of Trans is Relation.Binary.Core Trans' : ∀ {a b c ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : Set b} {C : Set c} → REL A B ℓ₁ → REL B C ℓ₂ → REL A C ℓ₃ → Set _ Trans' P Q R = ∀ {i j k} (x : P i j) (xs : Q j k) → R i k substitutive-to-reflexive : ∀ {a ℓ ℓ'} {A : Set a} {≈ : Rel A ℓ} {≡ : Rel A ℓ'} → Substitutive ≡ ℓ → Reflexive ≈ → ≡ ⇒ ≈ substitutive-to-reflexive {≈ = ≈} ≡-subst ≈-refl xᵣ = ≡-subst (≈ _) xᵣ ≈-refl substitutive⇒≡ : ∀ {a ℓ} {A : Set a} {≡ : Rel A ℓ} → Substitutive ≡ a → ≡ ⇒ PropEq._≡_ substitutive⇒≡ subst = substitutive-to-reflexive {≈ = PropEq._≡_} subst PropEq.refl record Equality {a ℓ} {A : Set a} (_≡_ : Rel A ℓ) : Set (suc a ⊔ ℓ) where field isEquivalence : IsEquivalence _≡_ subst : Substitutive _≡_ a open IsEquivalence isEquivalence public to-reflexive : ∀ {≈} → Reflexive ≈ → _≡_ ⇒ ≈ to-reflexive = substitutive-to-reflexive subst to-propositional : _≡_ ⇒ PropEq._≡_ to-propositional = substitutive⇒≡ subst -- Equational reasoning combinators. module Trans-Reasoning {a ℓ} {A : Set a} (_≈_ : Rel A ℓ) (trans : Transitive _≈_) where infix 2 finally infixr 2 _≈⟨_⟩_ _≈⟨_⟩_ : ∀ x {y z : A} → x ≈ y → y ≈ z → x ≈ z _ ≈⟨ x≈y ⟩ y≈z = trans x≈y y≈z -- When there is no reflexivty available this -- combinator can be used to end the reasoning. finally : ∀ (x y : A) → x ≈ y → x ≈ y finally _ _ x≈y = x≈y syntax finally x y x≈y = x ≈⟨ x≈y ⟩∎ y ∎ module Equivalence-Reasoning {a ℓ} {A : Set a} {_≈_ : Rel A ℓ} (E : IsEquivalence _≈_) where open IsEquivalence E open Trans-Reasoning _≈_ trans public hiding (finally) infix 2 _∎ _∎ : ∀ x → x ≈ x _ ∎ = refl module Preorder-Reasoning {p₁ p₂ p₃} (P : Preorder p₁ p₂ p₃) where open Preorder P open Trans-Reasoning _∼_ trans public hiding (finally) renaming (_≈⟨_⟩_ to _∼⟨_⟩_) open Equivalence-Reasoning isEquivalence public renaming (_∎ to _☐) infix 2 _∎ _∎ : ∀ x → x ∼ x _ ∎ = refl module Setoid-Reasoning {a ℓ} (s : Setoid a ℓ) where open Equivalence-Reasoning (Setoid.isEquivalence s) public
Fix Preorder-Reasoning and Setoid-Reasoning
Fix Preorder-Reasoning and Setoid-Reasoning
Agda
bsd-3-clause
crypto-agda/agda-nplib
7257505604e7df0478b8f19eb563905418431bcb
Parametric/Change/Specification.agda
Parametric/Change/Specification.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (validity-structure : Validity.Structure ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure -- Denotation-as-specification -- -- Contents -- - ⟦_⟧Δ : binding-time-shifted derive -- - Validity and correctness lemmas for ⟦_⟧Δ -- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments -- - `corollary-closed`: binding-time-shifted main theorem open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality -- open import Data.Integer -- open import Theorem.Groups-Nehemiah open import Theorem.CongApp open import Postulate.Extensionality record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field ⟦_⟧ΔConst : ∀ {Σ τ} → (c : Const Σ τ) (ρ : ⟦ Σ ⟧) → Δ₍ Σ ₎ ρ → Δ₍ τ ₎ (⟦ c ⟧Const ρ) correctness-const : ∀ {Σ τ} (c : Const Σ τ) → Derivative₍ Σ , τ ₎ ⟦ c ⟧Const ⟦ c ⟧ΔConst --------------- -- Interface -- --------------- ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) ⟦_⟧ΔTerms : ∀ {Σ Γ} → (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ Σ ₎ (⟦ ts ⟧Terms ρ) correctness : ∀ {τ Γ} (t : Term Γ τ) → Derivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ correctness-terms : ∀ {Σ Γ} (ts : Terms Γ Σ) → Derivative₍ Γ , Σ ₎ ⟦ ts ⟧Terms ⟦ ts ⟧ΔTerms -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const c ts) ρ dρ = ⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = cons (λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ)) (λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎) where open ≡-Reasoning ⟦ ∅ ⟧ΔTerms ρ dρ = ∅ ⟦ t • ts ⟧ΔTerms ρ dρ = ⟦ t ⟧Δ ρ dρ • ⟦ ts ⟧ΔTerms ρ dρ correctVar : ∀ {τ Γ} (x : Var Γ τ) → Derivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness-terms ∅ ρ dρ = refl correctness-terms (t • ts) ρ dρ = cong₂ _•_ (correctness t ρ dρ) (correctness-terms ts ρ dρ) correctness (const {Σ} {τ} c ts) ρ dρ = begin after₍ τ ₎ (⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ)) ≡⟨ correctness-const c (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ⟩ ⟦ c ⟧Const (after-env (⟦ ts ⟧ΔTerms ρ dρ)) ≡⟨ cong ⟦ c ⟧Const (correctness-terms ts ρ dρ) ⟩ ⟦ c ⟧Const (⟦ ts ⟧Terms (after-env dρ)) ∎ where open ≡-Reasoning correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f = ⟦ s ⟧ ρ g = ⟦ s ⟧ (after-env dρ) u = ⟦ t ⟧ ρ v = ⟦ t ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ Δu = ⟦ t ⟧Δ ρ dρ in begin f u ⊞₍ τ ₎ call-change {σ} {τ} Δf u Δu ≡⟨ sym (is-valid {σ} {τ} Δf u Δu) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change evaluation (Def 3.6 and Fig. 4h). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (validity-structure : Validity.Structure ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Theorem.CongApp open import Postulate.Extensionality -- This module defines two extension points, so to avoid some of -- the boilerplate, we use record notation. record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field -- Extension point 1: Derivatives of constants. ⟦_⟧ΔConst : ∀ {Σ τ} → (c : Const Σ τ) (ρ : ⟦ Σ ⟧) → Δ₍ Σ ₎ ρ → Δ₍ τ ₎ (⟦ c ⟧Const ρ) -- Extension point 2: Correctness of the instantiation of extension point 1. correctness-const : ∀ {Σ τ} (c : Const Σ τ) → Derivative₍ Σ , τ ₎ ⟦ c ⟧Const ⟦ c ⟧ΔConst --------------- -- Interface -- --------------- -- We provide: Derivatives of terms and lists of terms. ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) ⟦_⟧ΔTerms : ∀ {Σ Γ} → (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ Σ ₎ (⟦ ts ⟧Terms ρ) -- And we provide correctness proofs about the derivatives. correctness : ∀ {τ Γ} (t : Term Γ τ) → Derivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ correctness-terms : ∀ {Σ Γ} (ts : Terms Γ Σ) → Derivative₍ Γ , Σ ₎ ⟦ ts ⟧Terms ⟦ ts ⟧ΔTerms -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const c ts) ρ dρ = ⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = cons (λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ)) (λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎) where open ≡-Reasoning ⟦ ∅ ⟧ΔTerms ρ dρ = ∅ ⟦ t • ts ⟧ΔTerms ρ dρ = ⟦ t ⟧Δ ρ dρ • ⟦ ts ⟧ΔTerms ρ dρ correctVar : ∀ {τ Γ} (x : Var Γ τ) → Derivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness-terms ∅ ρ dρ = refl correctness-terms (t • ts) ρ dρ = cong₂ _•_ (correctness t ρ dρ) (correctness-terms ts ρ dρ) correctness (const {Σ} {τ} c ts) ρ dρ = begin after₍ τ ₎ (⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ)) ≡⟨ correctness-const c (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ⟩ ⟦ c ⟧Const (after-env (⟦ ts ⟧ΔTerms ρ dρ)) ≡⟨ cong ⟦ c ⟧Const (correctness-terms ts ρ dρ) ⟩ ⟦ c ⟧Const (⟦ ts ⟧Terms (after-env dρ)) ∎ where open ≡-Reasoning correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f = ⟦ s ⟧ ρ g = ⟦ s ⟧ (after-env dρ) u = ⟦ t ⟧ ρ v = ⟦ t ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ Δu = ⟦ t ⟧Δ ρ dρ in begin f u ⊞₍ τ ₎ call-change {σ} {τ} Δf u Δu ≡⟨ sym (is-valid {σ} {τ} Δf u Δu) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
Document P.C.Specification.
Document P.C.Specification. Old-commit-hash: e6e682df0d2f25bc8c50a5872acd4540f0116289
Agda
mit
inc-lc/ilc-agda
351737b9010e4a14a8b06675017e628dfb7d6269
PTT/Translation.agda
PTT/Translation.agda
open import Function open import Data.Product hiding (zip) renaming (_,_ to ⟨_,_⟩; proj₁ to fst; proj₂ to snd; map to ×map) open import Data.Zero open import Data.One open import Data.Two open import PTT.Dom import PTT.Session as Session import PTT.Map as Map import PTT.Env as Env import PTT.Proto as Proto open Session hiding (Ended) open Env hiding (_/₀_; _/₁_; Ended) open Proto hiding () open import PTT.Term open import PTT.Split open import Relation.Binary.PropositionalEquality.NP hiding (J) S-⅋-inp : ∀ {c c₀ c₁ δI S₀ S₁}{I : Proto δI}(l : [ c ↦ S₀ ⅋ S₁ ]∈ I) → S⟨ I [/] l ,[ c₀ ↦ S₀ ] ,[ c₁ ↦ S₁ ] ⟩ → S⟨ I ⟩ S-⅋-inp l P = {!!} translate : ∀ {δI}{I : Proto δI} → ⟨ I ⟩ → S⟨ I ⟩ translate (end x) = S-T (TC-end x) translate (⅋-inp l P) = S-⅋-inp l (translate (P c₀ c₁)) where postulate c₀ c₁ : URI translate (⊗-out l P) = {!!} translate (split σs A1 P P₁) = {!!} translate (nu D P) = {!!} {- module PTT.Translation where module Translation {t} (T⟨_⟩ : ∀ {δI} → Proto δI → Set t) (T-⊗-out : ∀ {δI I c S₀ S₁} (l : [ c ↦ S₀ ⊗ S₁ …]∈ I) (σs : Selections δI) (σE : Selection ([↦…]∈.δE l)) (A0 : AtMost 0 σs) (P₀ : ∀ c₀ → T⟨ I [/…] l /₀ σs ,[ E/ l Env./₀ σE , c₀ ↦ « S₀ » ] ⟩) (P₁ : ∀ c₁ → T⟨ I [/…] l /₁ σs ,[ E/ l Env./₁ σE , c₁ ↦ « S₁ » ] ⟩) → T⟨ I ⟩) (T-⅋-inp : ∀ {δI}{I : Proto δI}{c S₀ S₁} (l : [ c ↦ S₀ ⅋ S₁ ]∈ I) (P : ∀ c₀ c₁ → T⟨ I [/] l ,[ c₀ ↦ S₀ ] ,[ c₁ ↦ S₁ ] ⟩) → T⟨ I ⟩) (T-end : ∀ {δI}{I : Proto δI} (E : Ended I) → T⟨ I ⟩) (T-split : ∀ {δI}{I : Proto δI} (σs : Selections δI) (A1 : AtMost 1 σs) (P₀ : T⟨ I /₀ σs ⟩) (P₁ : T⟨ I /₁ σs ⟩) → T⟨ I ⟩) (T-cut : ∀ {δI}{I : Proto δI}{S₀ S₁} (D : Dual S₀ S₁) (σs : Selections δI) (A0 : AtMost 0 σs) (P₀ : ∀ c₀ → T⟨ I /₀ σs ,[ c₀ ↦ S₀ ] ⟩) (P₁ : ∀ c₁ → T⟨ I /₁ σs ,[ c₁ ↦ S₁ ] ⟩) → T⟨ I ⟩) (T-⊗-reorg : ∀ {δI δE c c₀ c₁ S₀ S₁}{J : Proto δI}{E : Env δE} (l : [ E ]∈ J) (l₀ : c₀ ↦ « S₀ » ∈ E) (l₁ : c₁ ↦ « S₁ » ∈ E) (P : T⟨ J ⟩) → T⟨ J Proto./ l ,[ (E Env./' l₀ /D (↦∈.lA l₁) , c ↦ « S₀ ⊗ S₁ ») ] ⟩) (T-conv : ∀ {δI δJ}{I : Proto δI}{J : Proto δJ} → I ≈ J → T⟨ I ⟩ → T⟨ J ⟩) where T-fwd : ∀ {S₀ S₁} (S : Dual S₀ S₁) c₀ c₁ → T⟨ · ,[ c₀ ↦ S₀ ] ,[ c₁ ↦ S₁ ] ⟩ T-fwd 𝟙⊥ c₀ c₁ = {!!} T-fwd ⊥𝟙 c₀ c₁ = {!!} T-fwd (act (?! S S₁)) c₀ c₁ = {!!} T-fwd (act (!? S S₁)) c₀ c₁ = {!!} T-fwd (⊗⅋ S₀ S₁ S₂ S₃) c₀ c₁ = T-⅋-inp here[]' λ c₂ c₃ → T-⊗-out (there…' (there…' (there…' here…'))) ((((· ,[ (ε , c₀ ↦ 0₂) ]) ,[ (ε , c₁ ↦ 0₂) ]) ,[ (ε , c₂ ↦ 0₂) ]) ,[ (ε , c₃ ↦ 1₂) ]) (ε , c₀ ↦ 0₂) ((((· ,[ {!!} ]) ,[ {!!} ]) ,[ {!!} ]) ,[ {!!} ]) (λ c₄ → T-conv (≈,[] (≈-! (≈,[swap] ≈-∙ {!≈,[] ≈-refl ?!})) (∼,↦ (∼-! ∼,↦end))) (T-fwd S₁ c₃ c₄)) (λ c₄ → T-conv (≈,[] (≈,[] (≈-! (≈,[end] _ ≈-∙ (≈,[end] _ ≈-∙ ≈,[end] _))) ∼-refl) (∼,↦ (∼-! ∼,↦end))) (T-fwd S₃ c₃ c₄)) T-fwd (⅋⊗ S S₁ S₂ S₃) c₀ c₁ = {!!} {- -} go : ∀ {δI}{I : Proto δI} → ⟨ I ⟩ → T⟨ I ⟩ go (end x) = T-end x go (⅋-inp l P) = T-⅋-inp l (λ c₀ c₁ → go (P c₀ c₁)) go {I = I}(⊗-out {c} {S₀} {S₁} l P) = T-conv e rPP where postulate c0 c1 : URI open [↦…]∈ l F = E Env./' lE , c0 ↦ « S₀ » , c1 ↦ « S₁ » J = I Proto./ lI ,[ F ] G = F /D there here /D here , c ↦ « S₀ ⊗ S₁ » K = J Proto./ heRe[] ,[ G ] rPP : T⟨ K ⟩ rPP = T-⊗-reorg heRe[] (theRe here) heRe (go (P c0 c1)) e : K ≈ I e = ≈,[] (≈,[end] (Ended-/* F)) (∼,↦ (∼,↦end ∼-∙ ∼,↦end)) ≈-∙ (≈-! (≈-/…,[…] l)) go {I = I}(nu {S₀} {S₁} D P) = T-conv {!!} (T-cut {I = I'} D (sel₀ _ ,[ (ε , c ↦ 0₂) ] ,[ (ε , c' ↦ 1₂) ]) {!!} (λ c₀' → {!cPP!}) (λ c₁' → {!T-fwd!})) where postulate c c' c0 c1 : URI E = ε , c0 ↦ « S₀ » , c1 ↦ « S₁ » E/* = ε , c0 ↦ end , c1 ↦ end J = I ,[ E ] -- K = J / here ,[ E/* , c ↦ S₀ ⊗ S₁ ] K = I ,[ E/* ] ,[ E/* , c ↦ « S₀ ⊗ S₁ » ] gP : T⟨ J ⟩ gP = go (P c0 c1) rPP : T⟨ K ⟩ rPP = T-⊗-reorg {c = c} heRe[] (theRe here) heRe gP e : K ≈ I ,[ c ↦ S₀ ⊗ S₁ ] e = ≈,[] (≈,[end] _) (∼,↦ (∼,↦end ∼-∙ ∼,↦end)) cPP : T⟨ I ,[ c ↦ S₀ ⊗ S₁ ] ⟩ cPP = T-conv e rPP I' = I ,[ c ↦ S₀ ⊗ S₁ ] ,[ c' ↦ {!!} ] go (split σs A P₀ P₁) = T-split σs A (go P₀) (go P₁) -} -- -} -- -} -- -} -- -} -- -} -- -}
-- UNFINISHED open import Function open import Data.Product hiding (zip) renaming (_,_ to ⟨_,_⟩; proj₁ to fst; proj₂ to snd; map to ×map) open import Data.Zero open import Data.One open import Data.Two open import PTT.Dom import PTT.Session as Session import PTT.Map as Map import PTT.Env as Env import PTT.Proto as Proto open Session hiding (Ended) open Env hiding (_/₀_; _/₁_; Ended) open Proto hiding () open import PTT.Term open import PTT.Split open import Relation.Binary.PropositionalEquality.NP hiding (J) S-⅋-inp : ∀ {c c₀ c₁ δI S₀ S₁}{I : Proto δI}(l : [ c ↦ S₀ ⅋ S₁ ]∈ I) → S⟨ I [/] l ,[ c₀ ↦ S₀ ] ,[ c₁ ↦ S₁ ] ⟩ → S⟨ I ⟩ S-⅋-inp l P = {!!} translate : ∀ {δI}{I : Proto δI} → ⟨ I ⟩ → S⟨ I ⟩ translate (end x) = S-T (TC-end x) translate (⅋-inp l P) = S-⅋-inp l (translate (P c₀ c₁)) where postulate c₀ c₁ : URI translate (⊗-out l P) = {!!} translate (split σs A1 P P₁) = {!!} translate (nu D P) = {!!} {- module PTT.Translation where module Translation {t} (T⟨_⟩ : ∀ {δI} → Proto δI → Set t) (T-⊗-out : ∀ {δI I c S₀ S₁} (l : [ c ↦ S₀ ⊗ S₁ …]∈ I) (σs : Selections δI) (σE : Selection ([↦…]∈.δE l)) (A0 : AtMost 0 σs) (P₀ : ∀ c₀ → T⟨ I [/…] l /₀ σs ,[ E/ l Env./₀ σE , c₀ ↦ « S₀ » ] ⟩) (P₁ : ∀ c₁ → T⟨ I [/…] l /₁ σs ,[ E/ l Env./₁ σE , c₁ ↦ « S₁ » ] ⟩) → T⟨ I ⟩) (T-⅋-inp : ∀ {δI}{I : Proto δI}{c S₀ S₁} (l : [ c ↦ S₀ ⅋ S₁ ]∈ I) (P : ∀ c₀ c₁ → T⟨ I [/] l ,[ c₀ ↦ S₀ ] ,[ c₁ ↦ S₁ ] ⟩) → T⟨ I ⟩) (T-end : ∀ {δI}{I : Proto δI} (E : Ended I) → T⟨ I ⟩) (T-split : ∀ {δI}{I : Proto δI} (σs : Selections δI) (A1 : AtMost 1 σs) (P₀ : T⟨ I /₀ σs ⟩) (P₁ : T⟨ I /₁ σs ⟩) → T⟨ I ⟩) (T-cut : ∀ {δI}{I : Proto δI}{S₀ S₁} (D : Dual S₀ S₁) (σs : Selections δI) (A0 : AtMost 0 σs) (P₀ : ∀ c₀ → T⟨ I /₀ σs ,[ c₀ ↦ S₀ ] ⟩) (P₁ : ∀ c₁ → T⟨ I /₁ σs ,[ c₁ ↦ S₁ ] ⟩) → T⟨ I ⟩) (T-⊗-reorg : ∀ {δI δE c c₀ c₁ S₀ S₁}{J : Proto δI}{E : Env δE} (l : [ E ]∈ J) (l₀ : c₀ ↦ « S₀ » ∈ E) (l₁ : c₁ ↦ « S₁ » ∈ E) (P : T⟨ J ⟩) → T⟨ J Proto./ l ,[ (E Env./' l₀ /D (↦∈.lA l₁) , c ↦ « S₀ ⊗ S₁ ») ] ⟩) (T-conv : ∀ {δI δJ}{I : Proto δI}{J : Proto δJ} → I ≈ J → T⟨ I ⟩ → T⟨ J ⟩) where T-fwd : ∀ {S₀ S₁} (S : Dual S₀ S₁) c₀ c₁ → T⟨ · ,[ c₀ ↦ S₀ ] ,[ c₁ ↦ S₁ ] ⟩ T-fwd 𝟙⊥ c₀ c₁ = {!!} T-fwd ⊥𝟙 c₀ c₁ = {!!} T-fwd (act (?! S S₁)) c₀ c₁ = {!!} T-fwd (act (!? S S₁)) c₀ c₁ = {!!} T-fwd (⊗⅋ S₀ S₁ S₂ S₃) c₀ c₁ = T-⅋-inp here[]' λ c₂ c₃ → T-⊗-out (there…' (there…' (there…' here…'))) ((((· ,[ (ε , c₀ ↦ 0₂) ]) ,[ (ε , c₁ ↦ 0₂) ]) ,[ (ε , c₂ ↦ 0₂) ]) ,[ (ε , c₃ ↦ 1₂) ]) (ε , c₀ ↦ 0₂) ((((· ,[ {!!} ]) ,[ {!!} ]) ,[ {!!} ]) ,[ {!!} ]) (λ c₄ → T-conv (≈,[] (≈-! (≈,[swap] ≈-∙ {!≈,[] ≈-refl ?!})) (∼,↦ (∼-! ∼,↦end))) (T-fwd S₁ c₃ c₄)) (λ c₄ → T-conv (≈,[] (≈,[] (≈-! (≈,[end] _ ≈-∙ (≈,[end] _ ≈-∙ ≈,[end] _))) ∼-refl) (∼,↦ (∼-! ∼,↦end))) (T-fwd S₃ c₃ c₄)) T-fwd (⅋⊗ S S₁ S₂ S₃) c₀ c₁ = {!!} go : ∀ {δI}{I : Proto δI} → ⟨ I ⟩ → T⟨ I ⟩ go (end x) = T-end x go (⅋-inp l P) = T-⅋-inp l (λ c₀ c₁ → go (P c₀ c₁)) go {I = I}(⊗-out {c} {S₀} {S₁} l P) = T-conv e rPP where postulate c0 c1 : URI open [↦…]∈ l F = E Env./' lE , c0 ↦ « S₀ » , c1 ↦ « S₁ » J = I Proto./ lI ,[ F ] G = F /D there here /D here , c ↦ « S₀ ⊗ S₁ » K = J Proto./ heRe[] ,[ G ] rPP : T⟨ K ⟩ rPP = T-⊗-reorg heRe[] (theRe here) heRe (go (P c0 c1)) e : K ≈ I e = ≈,[] (≈,[end] (Ended-/* F)) (∼,↦ (∼,↦end ∼-∙ ∼,↦end)) ≈-∙ (≈-! (≈-/…,[…] l)) go {I = I}(nu {S₀} {S₁} D P) = T-conv {!!} (T-cut {I = I'} D (sel₀ _ ,[ (ε , c ↦ 0₂) ] ,[ (ε , c' ↦ 1₂) ]) {!!} (λ c₀' → {!cPP!}) (λ c₁' → {!T-fwd!})) where postulate c c' c0 c1 : URI E = ε , c0 ↦ « S₀ » , c1 ↦ « S₁ » E/* = ε , c0 ↦ end , c1 ↦ end J = I ,[ E ] -- K = J / here ,[ E/* , c ↦ S₀ ⊗ S₁ ] K = I ,[ E/* ] ,[ E/* , c ↦ « S₀ ⊗ S₁ » ] gP : T⟨ J ⟩ gP = go (P c0 c1) rPP : T⟨ K ⟩ rPP = T-⊗-reorg {c = c} heRe[] (theRe here) heRe gP e : K ≈ I ,[ c ↦ S₀ ⊗ S₁ ] e = ≈,[] (≈,[end] _) (∼,↦ (∼,↦end ∼-∙ ∼,↦end)) cPP : T⟨ I ,[ c ↦ S₀ ⊗ S₁ ] ⟩ cPP = T-conv e rPP I' = I ,[ c ↦ S₀ ⊗ S₁ ] ,[ c' ↦ {!!} ] go (split σs A P₀ P₁) = T-split σs A (go P₀) (go P₁) -} -- -} -- -} -- -} -- -} -- -} -- -}
Mark the Translation module as unfinished
Mark the Translation module as unfinished
Agda
bsd-3-clause
crypto-agda/protocols
c5b9953cdc3f8a48882f01493714cec1f0d16c61
arrow.agda
arrow.agda
open import Agda.Builtin.Bool _or_ : Bool → Bool → Bool true or _ = true _ or true = true false or false = false _and_ : Bool → Bool → Bool false and _ = false _ and false = false true and true = true ---------------------------------------- data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false ---------------------------------------- infixr 5 _∷_ data List (A : Set) : Set where ∘ : List A _∷_ : A → List A → List A {-# BUILTIN LIST List #-} {-# BUILTIN NIL ∘ #-} {-# BUILTIN CONS _∷_ #-} any : {A : Set} → (A → Bool) → List A → Bool any _ ∘ = false any f (x ∷ xs) = (f x) or (any f xs) _∈_ : ℕ → List ℕ → Bool x ∈ ∘ = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys _∋_ : List ℕ → ℕ → Bool xs ∋ y = y ∈ xs ---------------------------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow _≡≡_ : Arrow → Arrow → Bool (⇒ q) ≡≡ (⇒ s) = q ≡ s (p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s) _ ≡≡ _ = false _∈∈_ : Arrow → List Arrow → Bool x ∈∈ ∘ = false x ∈∈ (y ∷ ys) with x ≡≡ y ... | true = true ... | false = x ∈∈ ys closure : List Arrow → List ℕ → List ℕ closure ∘ found = found closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found)) closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found)) ... | true = closure (q ∷ rest) found ... | false = closure rest found _,_⊢_ : List Arrow → List ℕ → ℕ → Bool cs , ps ⊢ q = q ∈ (closure cs ps) _⊢_ : List Arrow → Arrow → Bool cs ⊢ (⇒ q) = q ∈ (closure cs ∘) cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q ---------------------------------------- data Separation : Set where model : List ℕ → List ℕ → Separation modelsupports : Separation → List Arrow → ℕ → Bool modelsupports (model holds _) cs n = cs , holds ⊢ n modeldenies : Separation → List Arrow → ℕ → Bool modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails _⟪!_⟫_ : List Arrow → Separation → Arrow → Bool cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q) _⟪_⟫_ : List Arrow → List Separation → Arrow → Bool cs ⟪ ∘ ⟫ arr = false cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr) ---------------------------------------- data Relation : Set where Proved : Relation Derivable : Relation Separated : Relation Unknown : Relation consider : List Arrow → List Separation → Arrow → Relation consider cs ms arr with (arr ∈∈ cs) ... | true = Proved ... | false with (cs ⊢ arr) ... | true = Derivable ... | false with (cs ⟪ ms ⟫ arr) ... | true = Separated ... | false = Unknown proofs : List Arrow proofs = (3 ⇒ (⇒ 4)) ∷ -- (5 ⇒ (⇒ 4)) ∷ (6 ⇒ (⇒ 4)) ∷ (3 ⇒ (⇒ 3)) ∷ (3 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (5 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 7)) ∷ (6 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 7)) ∷ (5 ⇒ (⇒ 10)) ∷ (10 ⇒ (⇒ 4)) ∷ (5 ⇒ (⇒ 11)) ∷ (6 ⇒ (⇒ 11)) ∷ (11 ⇒ (⇒ 4)) ∷ (10 ⇒ (⇒ 7)) ∷ (8 ⇒ (⇒ 4)) ∷ (9 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 8)) ∷ (3 ⇒ (⇒ 8)) ∷ (5 ⇒ (3 ⇒ (⇒ 9))) ∷ (6 ⇒ (7 ⇒ (⇒ 10))) ∷ (6 ⇒ (3 ⇒ (⇒ 3))) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (8 ⇒ (⇒ 10))) ∷ (3 ⇒ (10 ⇒ (⇒ 9))) ∷ (5 ⇒ (⇒ 1)) ∷ (3 ⇒ (1 ⇒ (⇒ 9))) ∷ (1 ⇒ (⇒ 2)) ∷ (10 ⇒ (⇒ 2)) ∷ ∘ cms : List Separation cms = (model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷ (model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷ (model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷ (model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷ (model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷ (model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷ (model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷ (model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷ (model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷ (model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷ (model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘ testp : Arrow testp = (5 ⇒ (⇒ 10)) testd : Arrow testd = (5 ⇒ (⇒ 4)) tests : Arrow tests = (5 ⇒ (⇒ 3)) testu : Arrow testu = (6 ⇒ (⇒ 1)) main = (closure proofs (5 ∷ ∘))
data Bool : Set where true : Bool false : Bool _or_ : Bool → Bool → Bool true or _ = true _ or true = true false or false = false _and_ : Bool → Bool → Bool false and _ = false _ and false = false true and true = true ---------------------------------------- data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false ---------------------------------------- infixr 5 _∷_ data List (A : Set) : Set where ∘ : List A _∷_ : A → List A → List A any : {A : Set} → (A → Bool) → List A → Bool any _ ∘ = false any f (x ∷ xs) = (f x) or (any f xs) _∈_ : ℕ → List ℕ → Bool x ∈ ∘ = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys _∋_ : List ℕ → ℕ → Bool xs ∋ y = y ∈ xs ---------------------------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow _≡≡_ : Arrow → Arrow → Bool (⇒ q) ≡≡ (⇒ s) = q ≡ s (p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s) _ ≡≡ _ = false _∈∈_ : Arrow → List Arrow → Bool x ∈∈ ∘ = false x ∈∈ (y ∷ ys) with x ≡≡ y ... | true = true ... | false = x ∈∈ ys closure : List Arrow → List ℕ → List ℕ closure ∘ found = found closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found)) closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found)) ... | true = closure (q ∷ rest) found ... | false = closure rest found _,_⊢_ : List Arrow → List ℕ → ℕ → Bool cs , ps ⊢ q = q ∈ (closure cs ps) _⊢_ : List Arrow → Arrow → Bool cs ⊢ (⇒ q) = q ∈ (closure cs ∘) cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q ---------------------------------------- data Separation : Set where model : List ℕ → List ℕ → Separation modelsupports : Separation → List Arrow → ℕ → Bool modelsupports (model holds _) cs n = cs , holds ⊢ n modeldenies : Separation → List Arrow → ℕ → Bool modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails _⟪!_⟫_ : List Arrow → Separation → Arrow → Bool cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q) _⟪_⟫_ : List Arrow → List Separation → Arrow → Bool cs ⟪ ∘ ⟫ arr = false cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr) ---------------------------------------- data Relation : Set where Proved : Relation Derivable : Relation Separated : Relation Unknown : Relation consider : List Arrow → List Separation → Arrow → Relation consider cs ms arr with (arr ∈∈ cs) ... | true = Proved ... | false with (cs ⊢ arr) ... | true = Derivable ... | false with (cs ⟪ ms ⟫ arr) ... | true = Separated ... | false = Unknown proofs : List Arrow proofs = (3 ⇒ (⇒ 4)) ∷ -- (5 ⇒ (⇒ 4)) ∷ (6 ⇒ (⇒ 4)) ∷ (3 ⇒ (⇒ 3)) ∷ (3 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (5 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 7)) ∷ (6 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 7)) ∷ (5 ⇒ (⇒ 10)) ∷ (10 ⇒ (⇒ 4)) ∷ (5 ⇒ (⇒ 11)) ∷ (6 ⇒ (⇒ 11)) ∷ (11 ⇒ (⇒ 4)) ∷ (10 ⇒ (⇒ 7)) ∷ (8 ⇒ (⇒ 4)) ∷ (9 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 8)) ∷ (3 ⇒ (⇒ 8)) ∷ (5 ⇒ (3 ⇒ (⇒ 9))) ∷ (6 ⇒ (7 ⇒ (⇒ 10))) ∷ (6 ⇒ (3 ⇒ (⇒ 3))) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (8 ⇒ (⇒ 10))) ∷ (3 ⇒ (10 ⇒ (⇒ 9))) ∷ (5 ⇒ (⇒ 1)) ∷ (3 ⇒ (1 ⇒ (⇒ 9))) ∷ (1 ⇒ (⇒ 2)) ∷ (10 ⇒ (⇒ 2)) ∷ ∘ cms : List Separation cms = (model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷ (model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷ (model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷ (model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷ (model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷ (model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷ (model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷ (model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷ (model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷ (model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷ (model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘ testp : Arrow testp = (5 ⇒ (⇒ 10)) testd : Arrow testd = (5 ⇒ (⇒ 4)) tests : Arrow tests = (5 ⇒ (⇒ 3)) testu : Arrow testu = (6 ⇒ (⇒ 1))
Revert "Import bool" - The agda stdlib is too slow to be worthwhile
Revert "Import bool" - The agda stdlib is too slow to be worthwhile This reverts commit e449e00f2c1e586ddf5a78a508185eb30347d0fc.
Agda
mit
louisswarren/hieretikz
c6df8251b159a6d5d0490726b27d486890c82882
Neglible.agda
Neglible.agda
{-# OPTIONS --copatterns #-} open import Algebra open import Function open import Data.Nat.NP open import Data.Nat.Distance open import Data.Nat.Properties open import Data.Two open import Data.Zero open import Data.Product open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP open import HoTT open Equivalences open import Explore.Core open import Explore.Universe.Type {𝟘} open import Explore.Universe.Base module Neglible where module prop = CommutativeSemiring commutativeSemiring module OR = Poset (DecTotalOrder.poset decTotalOrder) ≤-*-cancel : ∀ {x m n} → 1 ≤ x → x * m ≤ x * n → m ≤ n ≤-*-cancel {suc x} {m} {n} (s≤s le) mn rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-≤ _ _ _ mn record ℕ→ℚ : Set where constructor _/_[_] field εN : (n : ℕ) → ℕ εD : (n : ℕ) → ℕ εD-pos : ∀ n → εD n > 0 record Is-Neg (ε : ℕ→ℚ) : Set where constructor mk open ℕ→ℚ ε field cₙ : (c : ℕ) → ℕ prf : ∀(c n : ℕ) → n > cₙ n → n ^ c * εN n ≤ εD n open Is-Neg 0ℕℚ : ℕ→ℚ ℕ→ℚ.εN 0ℕℚ _ = 0 ℕ→ℚ.εD 0ℕℚ _ = 1 ℕ→ℚ.εD-pos 0ℕℚ _ = s≤s z≤n 0ℕℚ-neg : Is-Neg 0ℕℚ cₙ 0ℕℚ-neg _ = 0 prf 0ℕℚ-neg c n x = OR.trans (OR.reflexive (proj₂ prop.zero (n ^ c))) z≤n _+ℕℚ_ : ℕ→ℚ → ℕ→ℚ → ℕ→ℚ ℕ→ℚ.εN ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εN n * μD n + μN n * εD n ℕ→ℚ.εD ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εD n * μD n ℕ→ℚ.εD-pos ((εN / εD [ εD+ ]) +ℕℚ (μN / μD [ μD+ ])) n = εD+ n *-mono μD+ n +ℕℚ-neg : {ε μ : ℕ→ℚ} → Is-Neg ε → Is-Neg μ → Is-Neg (ε +ℕℚ μ) cₙ (+ℕℚ-neg ε μ) n = 1 + cₙ ε n + cₙ μ n prf (+ℕℚ-neg {εM} {μM} ε μ) c n n>nc = ≤-*-cancel {x = n} (OR.trans (s≤s z≤n) n>nc) lemma where open ≤-Reasoning open ℕ→ℚ εM open ℕ→ℚ μM renaming (εN to μN; εD to μD; εD-pos to μD-pos) lemma = n * (n ^ c * (εN n * μD n + μN n * εD n)) ≡⟨ ! prop.*-assoc n (n ^ c) _ ∙ proj₁ prop.distrib (n ^ (1 + c)) (εN n * μD n) (μN n * εD n) ∙ ap₂ _+_ (! prop.*-assoc (n ^ (1 + c)) (εN n) (μD n)) (! (prop.*-assoc (n ^ (1 + c)) (μN n) (εD n))) ⟩ n ^ (1 + c) * εN n * μD n + n ^ (1 + c) * μN n * εD n ≤⟨ (prf ε (1 + c) n (OR.trans (s≤s (≤-step (m≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (μD n ∎)) +-mono (prf μ (1 + c) n (OR.trans (s≤s (≤-step (n≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (εD n ∎)) ⟩ εD n * μD n + μD n * εD n ≡⟨ ap₂ _+_ (refl {x = εD n * μD n}) (prop.*-comm (μD n) (εD n) ∙ ! proj₂ prop.+-identity (εD n * μD n)) ⟩ 2 * (εD n * μD n) ≤⟨ OR.trans (s≤s (s≤s z≤n)) n>nc *-mono (εD n * μD n ∎) ⟩ n * (εD n * μD n) ∎ module _ (Rᵁ : ℕ → U)(let R = λ n → El (Rᵁ n)) where # : ∀ {n} → Count (R n) # {n} = count (Rᵁ n) record _~_ (f g : (x : ℕ) → R x → 𝟚) : Set where constructor mk field ε : ℕ→ℚ open ℕ→ℚ ε field ε-neg : Is-Neg ε bounded : ∀ k → εD k * dist (# (f k)) (# (g k)) ≤ Card (Rᵁ k) * εN k ~-trans : Transitive _~_ _~_.ε (~-trans x x₁) = _ _~_.ε-neg (~-trans x x₁) = +ℕℚ-neg (_~_.ε-neg x) (_~_.ε-neg x₁) _~_.bounded (~-trans {f}{g}{h}(mk ε₀ ε₀-neg fg) (mk ε₁ ε₁-neg gh)) k = (b * d) * dist #f #h ≤⟨ (b * d ∎) *-mono dist-sum #f #g #h ⟩ (b * d) * (dist #f #g + dist #g #h) ≡⟨ proj₁ prop.distrib (b * d) (dist #f #g) (dist #g #h) ∙ ap₂ _+_ (ap₂ _*_ (prop.*-comm b d) refl ∙ prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h)) ⟩ d * (b * dist #f #g) + b * (d * dist #g #h) ≤⟨ ((d ∎) *-mono fg k) +-mono ((b ∎) *-mono gh k) ⟩ d * (|R| * a) + b * (|R| * c) ≡⟨ ap₂ _+_ (rot d |R| a) (rot b |R| c) ∙ ! proj₁ prop.distrib |R| (a * d) (c * b) ⟩ |R| * ℕ→ℚ.εN (ε₀ +ℕℚ ε₁) k ∎ where open ≤-Reasoning rot : ∀ x y z → x * (y * z) ≡ y * (z * x) rot x y z = prop.*-comm x (y * z) ∙ prop.*-assoc y z x |R| = Card (Rᵁ k) #f = # (f k) #g = # (g k) #h = # (h k) a = ℕ→ℚ.εN ε₀ k b = ℕ→ℚ.εD ε₀ k c = ℕ→ℚ.εN ε₁ k d = ℕ→ℚ.εD ε₁ k -- -} -- -} -- -} -- -} -- -} -- -}
{-# OPTIONS --copatterns #-} open import Algebra open import Function open import Data.Nat.NP open import Data.Nat.Distance open import Data.Nat.Properties open import Data.Two open import Data.Zero open import Data.Product open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP open import HoTT open Equivalences open import Explore.Core open import Explore.Universe.Type {𝟘} open import Explore.Universe.Base module Neglible where module prop = CommutativeSemiring commutativeSemiring module OR = Poset (DecTotalOrder.poset decTotalOrder) ≤-*-cancel : ∀ {x m n} → 1 ≤ x → x * m ≤ x * n → m ≤ n ≤-*-cancel {suc x} {m} {n} (s≤s le) mn rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-≤ _ _ _ mn record ℕ→ℚ : Set where constructor _/_[_] field εN : (n : ℕ) → ℕ εD : (n : ℕ) → ℕ εD-pos : ∀ n → εD n > 0 record Is-Neg (ε : ℕ→ℚ) : Set where constructor mk open ℕ→ℚ ε field cₙ : (c : ℕ) → ℕ prf : ∀(c n : ℕ) → n > cₙ n → n ^ c * εN n ≤ εD n open Is-Neg 0ℕℚ : ℕ→ℚ ℕ→ℚ.εN 0ℕℚ _ = 0 ℕ→ℚ.εD 0ℕℚ _ = 1 ℕ→ℚ.εD-pos 0ℕℚ _ = s≤s z≤n 0ℕℚ-neg : Is-Neg 0ℕℚ cₙ 0ℕℚ-neg _ = 0 prf 0ℕℚ-neg c n x = OR.trans (OR.reflexive (proj₂ prop.zero (n ^ c))) z≤n _+ℕℚ_ : ℕ→ℚ → ℕ→ℚ → ℕ→ℚ ℕ→ℚ.εN ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εN n * μD n + μN n * εD n ℕ→ℚ.εD ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εD n * μD n ℕ→ℚ.εD-pos ((εN / εD [ εD+ ]) +ℕℚ (μN / μD [ μD+ ])) n = εD+ n *-mono μD+ n +ℕℚ-neg : {ε μ : ℕ→ℚ} → Is-Neg ε → Is-Neg μ → Is-Neg (ε +ℕℚ μ) cₙ (+ℕℚ-neg ε μ) n = 1 + cₙ ε n + cₙ μ n prf (+ℕℚ-neg {εM} {μM} ε μ) c n n>nc = ≤-*-cancel {x = n} (OR.trans (s≤s z≤n) n>nc) lemma where open ≤-Reasoning open ℕ→ℚ εM open ℕ→ℚ μM renaming (εN to μN; εD to μD; εD-pos to μD-pos) lemma = n * (n ^ c * (εN n * μD n + μN n * εD n)) ≡⟨ ! prop.*-assoc n (n ^ c) _ ∙ proj₁ prop.distrib (n ^ (1 + c)) (εN n * μD n) (μN n * εD n) ∙ ap₂ _+_ (! prop.*-assoc (n ^ (1 + c)) (εN n) (μD n)) (! (prop.*-assoc (n ^ (1 + c)) (μN n) (εD n))) ⟩ n ^ (1 + c) * εN n * μD n + n ^ (1 + c) * μN n * εD n ≤⟨ (prf ε (1 + c) n (OR.trans (s≤s (≤-step (m≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (μD n ∎)) +-mono (prf μ (1 + c) n (OR.trans (s≤s (≤-step (n≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (εD n ∎)) ⟩ εD n * μD n + μD n * εD n ≡⟨ ap₂ _+_ (refl {x = εD n * μD n}) (prop.*-comm (μD n) (εD n) ∙ ! proj₂ prop.+-identity (εD n * μD n)) ⟩ 2 * (εD n * μD n) ≤⟨ OR.trans (s≤s (s≤s z≤n)) n>nc *-mono (εD n * μD n ∎) ⟩ n * (εD n * μD n) ∎ infix 4 _≤→_ record _≤→_ (f g : ℕ→ℚ) : Set where constructor mk open ℕ→ℚ f renaming (εN to fN; εD to fD) open ℕ→ℚ g renaming (εN to gN; εD to gD) field -- fN k / fD k ≤ gN k / gD k ≤→ : ∀ k → fN k * gD k ≤ gN k * fD k ≤→-trans : ∀ {f g h} → f ≤→ g → g ≤→ h → f ≤→ h _≤→_.≤→ (≤→-trans {fN / fD [ fD-pos ]} {gN / gD [ gD-pos ]} {hN / hD [ hD-pos ]} (mk fg) (mk gh)) k = ≤-*-cancel (gD-pos k) lemma where open ≤-Reasoning lemma : gD k * (fN k * hD k) ≤ gD k * (hN k * fD k) lemma = gD k * (fN k * hD k) ≡⟨ ! prop.*-assoc (gD k) (fN k) (hD k) ∙ ap (flip _*_ (hD k)) (prop.*-comm (gD k) (fN k)) ⟩ (fN k * gD k) * hD k ≤⟨ fg k *-mono OR.refl ⟩ (gN k * fD k) * hD k ≡⟨ prop.*-assoc (gN k) (fD k) (hD k) ∙ ap (_*_ (gN k)) (prop.*-comm (fD k) (hD k)) ∙ ! prop.*-assoc (gN k) (hD k) (fD k) ⟩ (gN k * hD k) * fD k ≤⟨ gh k *-mono OR.refl ⟩ (hN k * gD k) * fD k ≡⟨ ap (flip _*_ (fD k)) (prop.*-comm (hN k) (gD k)) ∙ prop.*-assoc (gD k) (hN k) (fD k) ⟩ gD k * (hN k * fD k) ∎ +ℕℚ-mono : ∀ {f f' g g'} → f ≤→ f' → g ≤→ g' → f +ℕℚ g ≤→ f' +ℕℚ g' _≤→_.≤→ (+ℕℚ-mono {fN / fD [ _ ]} {f'N / f'D [ _ ]} {gN / gD [ _ ]} {g'N / g'D [ _ ]} (mk ff) (mk gg)) k = (fN k * gD k + gN k * fD k) * (f'D k * g'D k) ≡⟨ proj₂ prop.distrib (f'D k * g'D k) (fN k * gD k) (gN k * fD k) ⟩ fN k * gD k * (f'D k * g'D k) + gN k * fD k * (f'D k * g'D k) ≡⟨ ap₂ _+_ (*-interchange (fN k) (gD k) (f'D k) (g'D k) ∙ ap (_*_ (fN k * f'D k)) (prop.*-comm (gD k) (g'D k))) (ap (_*_ (gN k * fD k)) (prop.*-comm (f'D k) (g'D k)) ∙ *-interchange (gN k) (fD k) (g'D k) (f'D k)) ⟩ fN k * f'D k * (g'D k * gD k) + gN k * g'D k * (fD k * f'D k) ≤⟨ (ff k *-mono OR.refl) +-mono (gg k *-mono OR.refl) ⟩ f'N k * fD k * (g'D k * gD k) + g'N k * gD k * (fD k * f'D k) ≡⟨ ap₂ _+_ (*-interchange (f'N k) (fD k) (g'D k) (gD k)) (ap (_*_ (g'N k * gD k)) (prop.*-comm (fD k) (f'D k)) ∙ *-interchange (g'N k) (gD k) (f'D k) (fD k) ∙ ap (_*_ (g'N k * f'D k)) (prop.*-comm (gD k) (fD k))) ⟩ f'N k * g'D k * (fD k * gD k) + g'N k * f'D k * (fD k * gD k) ≡⟨ ! proj₂ prop.distrib (fD k * gD k) (f'N k * g'D k) (g'N k * f'D k) ⟩ (f'N k * g'D k + g'N k * f'D k) * (fD k * gD k) ∎ where open ≤-Reasoning record NegBounded (f : ℕ→ℚ) : Set where constructor mk field ε : ℕ→ℚ ε-neg : Is-Neg ε bounded : f ≤→ ε module _ where open NegBounded ≤-NB : {f g : ℕ→ℚ} → f ≤→ g → NegBounded g → NegBounded f ε (≤-NB le nb) = ε nb ε-neg (≤-NB le nb) = ε-neg nb bounded (≤-NB le nb) = ≤→-trans le (bounded nb) _+NB_ : {f g : ℕ→ℚ} → NegBounded f → NegBounded g → NegBounded (f +ℕℚ g) ε (fNB +NB gNB) = ε fNB +ℕℚ ε gNB ε-neg (fNB +NB gNB) = +ℕℚ-neg (ε-neg fNB) (ε-neg gNB) bounded (fNB +NB gNB) = +ℕℚ-mono (bounded fNB) (bounded gNB) module ~-NegBounded (Rᵁ : ℕ → U)(let R = λ n → El (Rᵁ n))(inh : ∀ x → 0 < Card (Rᵁ x)) where # : ∀ {n} → Count (R n) # {n} = count (Rᵁ n) ~dist : (f g : (x : ℕ) → R x → 𝟚) → ℕ→ℚ ℕ→ℚ.εN (~dist f g) n = dist (# (f n)) (# (g n)) ℕ→ℚ.εD (~dist f g) n = Card (Rᵁ n) ℕ→ℚ.εD-pos (~dist f g) n = inh n ~dist-sum : ∀ f g h → ~dist f h ≤→ ~dist f g +ℕℚ ~dist g h _≤→_.≤→ (~dist-sum f g h) k = #fh * (|R| * |R|) ≤⟨ dist-sum #f #g #h *-mono OR.refl ⟩ (#fg + #gh) * (|R| * |R|) ≡⟨ ! prop.*-assoc (#fg + #gh) |R| |R| ∙ ap (flip _*_ |R|) (proj₂ prop.distrib |R| #fg #gh) ⟩ (#fg * |R| + #gh * |R|) * |R| ∎ where open ≤-Reasoning |R| = Card (Rᵁ k) #f = # (f k) #g = # (g k) #h = # (h k) #fh = dist #f #h #fg = dist #f #g #gh = dist #g #h record _~_ (f g : (x : ℕ) → R x → 𝟚) : Set where constructor mk field ~ : NegBounded (~dist f g) ~-trans : Transitive _~_ _~_.~ (~-trans {f}{g}{h} fg gh) = ≤-NB (~dist-sum f g h) (_~_.~ fg +NB _~_.~ gh) module ~-Inlined (Rᵁ : ℕ → U)(let R = λ n → El (Rᵁ n)) where # : ∀ {n} → Count (R n) # {n} = count (Rᵁ n) record _~_ (f g : (x : ℕ) → R x → 𝟚) : Set where constructor mk field ε : ℕ→ℚ open ℕ→ℚ ε field ε-neg : Is-Neg ε bounded : ∀ k → εD k * dist (# (f k)) (# (g k)) ≤ Card (Rᵁ k) * εN k ~-trans : Transitive _~_ _~_.ε (~-trans x x₁) = _ _~_.ε-neg (~-trans x x₁) = +ℕℚ-neg (_~_.ε-neg x) (_~_.ε-neg x₁) _~_.bounded (~-trans {f}{g}{h}(mk ε₀ ε₀-neg fg) (mk ε₁ ε₁-neg gh)) k = (b * d) * dist #f #h ≤⟨ (b * d ∎) *-mono dist-sum #f #g #h ⟩ (b * d) * (dist #f #g + dist #g #h) ≡⟨ proj₁ prop.distrib (b * d) (dist #f #g) (dist #g #h) ∙ ap₂ _+_ (ap₂ _*_ (prop.*-comm b d) refl ∙ prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h)) ⟩ d * (b * dist #f #g) + b * (d * dist #g #h) ≤⟨ ((d ∎) *-mono fg k) +-mono ((b ∎) *-mono gh k) ⟩ d * (|R| * a) + b * (|R| * c) ≡⟨ ap₂ _+_ (rot d |R| a) (rot b |R| c) ∙ ! proj₁ prop.distrib |R| (a * d) (c * b) ⟩ |R| * ℕ→ℚ.εN (ε₀ +ℕℚ ε₁) k ∎ where open ≤-Reasoning rot : ∀ x y z → x * (y * z) ≡ y * (z * x) rot x y z = prop.*-comm x (y * z) ∙ prop.*-assoc y z x |R| = Card (Rᵁ k) #f = # (f k) #g = # (g k) #h = # (h k) a = ℕ→ℚ.εN ε₀ k b = ℕ→ℚ.εD ε₀ k c = ℕ→ℚ.εN ε₁ k d = ℕ→ℚ.εD ε₁ k -- -} -- -} -- -} -- -} -- -} -- -}
Update to Neglible
Update to Neglible
Agda
bsd-3-clause
crypto-agda/crypto-agda
905379564a50f91a3435bb015a582178b9bc39e2
README.agda
README.agda
module README where ---------------------------- -- INCREMENTAL λ-CALCULUS -- ---------------------------- -- -- -- IMPORTANT FILES -- -- modules.pdf -- The graph of dependencies between Agda modules. -- -- README.agda -- This file. A coarse-grained introduction to the Agda formalization. -- -- PLDI14-List-of-Theorems.agda -- For each theorem, lemma or definition in the PLDI 2014 submission, -- it points to the corresponding Agda object. -- -- -- LOCATION OF AGDA MODULES -- -- To find the file containing an Agda module, replace the dots in its -- full name by directory separators. The result is the file's path relative -- to this directory. For example, Parametric.Syntax.Type is defined in -- Parametric/Syntax/Type.agda. -- -- -- HOW TO TYPE CHECK EVERYTHING -- -- To typecheck this formalization, you need to install the appropriate version -- of Agda, the Agda standard library (version 0.7), generate Everything.agda -- with the attached Haskell helper, and finally run Agda on this file. -- -- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh -- script and following instructions given on output will eventually generate -- Everything.agda and proceed to type check everything on command line. -- -- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but -- has some bugs with serialization of code using some recent syntactic sugar -- which we use (https://code.google.com/p/agda/issues/detail?id=756), so it -- might work or not. When it does not, removing Agda caches (.agdai files) -- appears to often help. You can use "find . -name '*.agdai' | xargs rm" to do -- that. import Postulate.Extensionality import Base.Data.DependentList -- Variables and contexts import Base.Syntax.Context -- Sets of variables import Base.Syntax.Vars import Base.Denotation.Notation -- Environments import Base.Denotation.Environment -- Change contexts import Base.Change.Context -- # Base, parametric proof. -- -- This is for a parametric calculus where: -- types are parametric in base types -- terms are parametric in constants -- -- -- Modules are ordered and grouped according to what they represent. -- ## Definitions import Parametric.Syntax.Type import Parametric.Syntax.Term import Parametric.Denotation.Value import Parametric.Denotation.Evaluation import Parametric.Change.Type import Parametric.Change.Term import Parametric.Change.Derive import Parametric.Change.Value import Parametric.Change.Evaluation -- ## Proofs import Parametric.Change.Validity import Parametric.Change.Specification import Parametric.Change.Implementation import Parametric.Change.Correctness -- # Nehemiah plugin -- -- The structure is the same as the parametric proof (down to the -- order and the grouping of modules), except for the postulate module. -- Postulate an abstract data type for integer Bags. import Postulate.Bag-Nehemiah -- ## Definitions import Nehemiah.Syntax.Type import Nehemiah.Syntax.Term import Nehemiah.Denotation.Value import Nehemiah.Denotation.Evaluation import Nehemiah.Change.Term import Nehemiah.Change.Type import Nehemiah.Change.Derive import Nehemiah.Change.Value import Nehemiah.Change.Evaluation -- ## Proofs import Nehemiah.Change.Validity import Nehemiah.Change.Specification import Nehemiah.Change.Implementation import Nehemiah.Change.Correctness -- Import everything else. This ensures that typechecking README.agda typechecks -- the entire codebase, because Everything.agda is auto-generated. import Everything
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Machine-checked formalization of the theoretical results presented -- in the paper: -- -- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann. -- A Theory of Changes for Higher-Order Languages: -- Incrementalizing λ-Calculi by Static Differentiation. -- To appear at PLDI, ACM 2014. -- ------------------------------------------------------------------------ module README where -- We know two good ways to read this code base. You can either -- use Emacs with agda2-mode to interact with the source files, -- or you can use a web browser to view the pretty-printed and -- hyperlinked source files. For Agda power users, we also -- include basic setup information for your own machine. -- IF YOU WANT TO USE A BROWSER -- ============================ -- -- Start with *HTML* version of this readme. On the AEC -- submission website (online or inside the VM), follow the link -- "view the Agda code in their browser" to the file -- agda/README.html. -- -- The source code is syntax highlighted and hyperlinked. You can -- click on module names to open the corresponding files, or you -- can click on identifiers to jump to their definition. In -- general, a Agda file with name `Foo/Bar/Baz.agda` contains a -- module `Foo.Bar.Baz` and is shown in an HTML file -- `Foo.Bar.Baz.html`. -- -- Note that we also include the HTML files generated for our -- transitive dependencies from the Agda standard library. This -- allows you to follow hyperlinks to the Agda standard -- library. It is the default behavior of `agda --html` which we -- used to generate the HTML. -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE EMACS -- ======================== -- -- Open this file in Emacs with agda2-mode installed. See below -- for which Agda version you need to install. On the VM image, -- everything is setup for you and you can just open this file in -- Emacs. -- -- C-c C-l Load code. Type checks and syntax highlights. Use -- this if the code is not syntax highlighted, or -- after you changed something that you want to type -- check. -- -- M-. Jump to definition of identifier at the point. -- M-* Jump back. -- -- Note that README.agda imports Everything.agda which imports -- every Agda file in our formalization. So if README.agda type -- checks successfully, everything does. If you want to type -- check everything from scratch, delete the *.agdai files to -- disable separate compilation. You can use "find . -name -- '*.agdai' | xargs rm" to do that. -- -- More information on the Agda mode is available on the Agda wiki: -- -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE YOUR OWN SETUP -- ================================= -- -- To typecheck this formalization, you need to install the appropriate version -- of Agda, the Agda standard library (version 0.7), generate Everything.agda -- with the attached Haskell helper, and finally run Agda on this file. -- -- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh -- script and following instructions given on output will eventually generate -- Everything.agda and proceed to type check everything on command line. -- -- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but -- has some bugs with serialization of code using some recent syntactic sugar -- which we use (https://code.google.com/p/agda/issues/detail?id=756), so it -- might work or not. When it does not, removing Agda caches (.agdai files) -- appears to often help. You can use "find . -name '*.agdai' | xargs rm" to do -- that. -- -- If you're not an Agda power user, it is probably easier to use -- the VM image or look at the pretty-printed and hyperlinked -- HTML files, see above. -- WHERE TO START READING? -- ======================= -- -- modules.pdf -- The graph of dependencies between Agda modules. -- Good if you want to get a broad overview. -- -- README.agda -- This file. A coarse-grained introduction to the Agda -- formalization. Good if you want to begin at the beginning -- and understand the structure of our code. -- -- PLDI14-List-of-Theorems.agda -- Pointers to the Agda formalizations of all theorems, lemmas -- or definitions in the PLDI paper. Good if you want to read -- the paper and the Agda code side by side. -- -- Here is an import of this file, so you can jump to it -- directly (use M-. in Emacs or click the module name in the -- Browser): import PLDI14-List-of-Theorems -- Everything.agda -- Imports every Agda module in our formalization. Good if you -- want to make sure you don't miss anything. -- -- Again, here's is an import of this file so you can navigate -- there: import Everything -- THE AGDA CODE -- ============= import Postulate.Extensionality import Base.Data.DependentList -- Variables and contexts import Base.Syntax.Context -- Sets of variables import Base.Syntax.Vars import Base.Denotation.Notation -- Environments import Base.Denotation.Environment -- Change contexts import Base.Change.Context -- # Base, parametric proof. -- -- This is for a parametric calculus where: -- types are parametric in base types -- terms are parametric in constants -- -- -- Modules are ordered and grouped according to what they represent. -- ## Definitions import Parametric.Syntax.Type import Parametric.Syntax.Term import Parametric.Denotation.Value import Parametric.Denotation.Evaluation import Parametric.Change.Type import Parametric.Change.Term import Parametric.Change.Derive import Parametric.Change.Value import Parametric.Change.Evaluation -- ## Proofs import Parametric.Change.Validity import Parametric.Change.Specification import Parametric.Change.Implementation import Parametric.Change.Correctness -- # Nehemiah plugin -- -- The structure is the same as the parametric proof (down to the -- order and the grouping of modules), except for the postulate module. -- Postulate an abstract data type for integer Bags. import Postulate.Bag-Nehemiah -- ## Definitions import Nehemiah.Syntax.Type import Nehemiah.Syntax.Term import Nehemiah.Denotation.Value import Nehemiah.Denotation.Evaluation import Nehemiah.Change.Term import Nehemiah.Change.Type import Nehemiah.Change.Derive import Nehemiah.Change.Value import Nehemiah.Change.Evaluation -- ## Proofs import Nehemiah.Change.Validity import Nehemiah.Change.Specification import Nehemiah.Change.Implementation import Nehemiah.Change.Correctness
Revise agda/README.md (for #275).
Revise agda/README.md (for #275). Old-commit-hash: 88f72736101147e9754ea68565ed89d6702452bb
Agda
mit
inc-lc/ilc-agda
5e28e95336edd7da4ef18ee12d86f53112e719a2
Denotational/Evaluation/Total.agda
Denotational/Evaluation/Total.agda
module Denotational.Evaluation.Total where -- EVALUATION with a primitive for TOTAL DERIVATIVES -- -- This module defines the semantics of terms that support a -- primitive (Δ e) for computing the total derivative according -- to all free variables in e and all future arguments of e if e -- is a function. open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Changes open import ChangeContexts -- TERMS -- Denotational Semantics ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧ ⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ) ⟦ app t₁ t₂ ⟧Term ρ = (⟦ t₁ ⟧Term ρ) (⟦ t₂ ⟧Term ρ) ⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ ⟦ true ⟧Term ρ = true ⟦ false ⟧Term ρ = false ⟦ if t₁ t₂ t₃ ⟧Term ρ with ⟦ t₁ ⟧Term ρ ... | true = ⟦ t₂ ⟧Term ρ ... | false = ⟦ t₃ ⟧Term ρ ⟦ Δ {{Γ′}} t ⟧Term ρ = diff (⟦ t ⟧Term (update (⟦ Γ′ ⟧ ρ))) (⟦ t ⟧Term (ignore (⟦ Γ′ ⟧ ρ))) meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ) meaningOfTerm = meaning ⟦_⟧Term -- PROPERTIES of WEAKENING weaken-sound : ∀ {Γ₁ Γ₂ τ} (t : Term Γ₁ τ) {Γ′ : Γ₁ ≼ Γ₂} → ∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ′ ⟧ ρ) weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ)) weaken-sound (app t₁ t₂) ρ = ≡-app (weaken-sound t₁ ρ) (weaken-sound t₂ ρ) weaken-sound (var x) ρ = lift-sound _ x ρ weaken-sound true ρ = refl weaken-sound false ρ = refl weaken-sound (if t₁ t₂ t₃) {Γ′} ρ with weaken-sound t₁ {Γ′} ρ ... | H with ⟦ weaken Γ′ t₁ ⟧ ρ | ⟦ t₁ ⟧ (⟦ Γ′ ⟧ ρ) weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | true | true = weaken-sound t₂ {Γ′} ρ weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | false | false = weaken-sound t₃ {Γ′} ρ weaken-sound (Δ {{Γ′}} t) {Γ″} ρ = cong (λ x → diff (⟦ t ⟧ (update x)) (⟦ t ⟧ (ignore x))) (⟦⟧-≼-trans Γ′ Γ″ ρ)
module Denotational.Evaluation.Total where -- EVALUATION with a primitive for TOTAL DERIVATIVES -- -- This module defines the semantics of terms that support a -- primitive (Δ e) for computing the total derivative according -- to all free variables in e and all future arguments of e if e -- is a function. open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Changes open import ChangeContexts -- TERMS -- Denotational Semantics ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧ ⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ) ⟦ app t₁ t₂ ⟧Term ρ = (⟦ t₁ ⟧Term ρ) (⟦ t₂ ⟧Term ρ) ⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ ⟦ true ⟧Term ρ = true ⟦ false ⟧Term ρ = false ⟦ if t₁ t₂ t₃ ⟧Term ρ = if ⟦ t₁ ⟧Term ρ then ⟦ t₂ ⟧Term ρ else ⟦ t₃ ⟧Term ρ ⟦ Δ {{Γ′}} t ⟧Term ρ = diff (⟦ t ⟧Term (update (⟦ Γ′ ⟧ ρ))) (⟦ t ⟧Term (ignore (⟦ Γ′ ⟧ ρ))) meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ) meaningOfTerm = meaning ⟦_⟧Term -- PROPERTIES of WEAKENING weaken-sound : ∀ {Γ₁ Γ₂ τ} (t : Term Γ₁ τ) {Γ′ : Γ₁ ≼ Γ₂} → ∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ′ ⟧ ρ) weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ)) weaken-sound (app t₁ t₂) ρ = ≡-app (weaken-sound t₁ ρ) (weaken-sound t₂ ρ) weaken-sound (var x) ρ = lift-sound _ x ρ weaken-sound true ρ = refl weaken-sound false ρ = refl weaken-sound (if t₁ t₂ t₃) {Γ′} ρ with weaken-sound t₁ {Γ′} ρ ... | H with ⟦ weaken Γ′ t₁ ⟧ ρ | ⟦ t₁ ⟧ (⟦ Γ′ ⟧ ρ) weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | true | true = weaken-sound t₂ {Γ′} ρ weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | false | false = weaken-sound t₃ {Γ′} ρ weaken-sound (Δ {{Γ′}} t) {Γ″} ρ = cong (λ x → diff (⟦ t ⟧ (update x)) (⟦ t ⟧ (ignore x))) (⟦⟧-≼-trans Γ′ Γ″ ρ)
Use if_then_else in ⟦_⟧Term.
Use if_then_else in ⟦_⟧Term. Old-commit-hash: f469e7f3aa20a7cdade4976d8bea10180b56dedb
Agda
mit
inc-lc/ilc-agda
e8b0a2064d7927030a67be90fa6130d3a7099cf3
Parametric/Denotation/CachingMValue.agda
Parametric/Denotation/CachingMValue.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Values for caching evaluation of MTerm ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.MType as MType import Parametric.Denotation.Value as Value import Parametric.Denotation.MValue as MValue module Parametric.Denotation.CachingMValue (Base : Type.Structure) (⟦_⟧Base : Value.Structure Base) where open import Base.Denotation.Notation open Type.Structure Base open MType.Structure Base open Value.Structure Base ⟦_⟧Base open MValue.Structure Base ⟦_⟧Base open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level open import Function hiding (const) module Structure where -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -}
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Values for caching evaluation of MTerm ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.MType as MType import Parametric.Denotation.Value as Value import Parametric.Denotation.MValue as MValue module Parametric.Denotation.CachingMValue (Base : Type.Structure) (⟦_⟧Base : Value.Structure Base) where open import Base.Denotation.Notation open Type.Structure Base open MType.Structure Base open Value.Structure Base ⟦_⟧Base open MValue.Structure Base ⟦_⟧Base open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level open import Function hiding (const) module Structure where -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCacheWrong × ⟦ τ₁ ⟧ValTypeHidCacheWrong ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -}
Maintain ⟦_⟧CompTypeHidCacheWrong
Maintain ⟦_⟧CompTypeHidCacheWrong This got out of sync with the other version. Probably this belongs to history though.
Agda
mit
inc-lc/ilc-agda
bce471b3be844f987aa2bd3e748f542683de4829
Base/Change/Products.agda
Base/Change/Products.agda
module Base.Change.Products where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Data.Product -- Also try defining sectioned change structures on the positives halves of -- groups? Or on arbitrary subsets? -- Restriction: we pair sets on the same level (because right now everything -- else would risk getting in the way). module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where open ≡-Reasoning -- The simplest possible definition of changes for products. -- The following is probably bullshit: -- Does not handle products of functions - more accurately, writing the -- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p PChange : A × B → Set ℓ PChange (a , b) = Δ a × Δ b _⊕_ : (v : A × B) → PChange v → A × B _⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db _⊝_ : A × B → (v : A × B) → PChange v _⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b p-nil : (v : A × B) → PChange v p-nil (a , b) = (nil a , nil b) p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u p-update-diff (ua , ub) (va , vb) = let u = (ua , ub) v = (va , vb) in begin v ⊕ (u ⊝ v) ≡⟨⟩ (va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb)) --v ⊕ ((ua ⊟ va , ub ⊟ vb)) ≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩ (ua , ub) ≡⟨⟩ u ∎ p-update-nil : (v : A × B) → v ⊕ (p-nil v) ≡ v p-update-nil (a , b) = let v = (a , b) in begin v ⊕ (p-nil v) ≡⟨⟩ (a ⊞ nil a , b ⊞ nil b) ≡⟨ cong₂ _,_ (update-nil a) (update-nil b)⟩ (a , b) ≡⟨⟩ v ∎ changeAlgebra : ChangeAlgebra ℓ (A × B) changeAlgebra = record { Change = PChange ; update = _⊕_ ; diff = _⊝_ ; nil = p-nil ; isChangeAlgebra = record { update-diff = p-update-diff ; update-nil = p-update-nil } } proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v) proj₁′ (a , b) (da , db) = da proj₁′Derivative : Derivative proj₁ proj₁′ -- Implementation note: we do not need to pattern match on v and dv because -- they are records, hence Agda knows that pattern matching on records cannot -- fail. Technically, the required feature is the eta-rule on records. proj₁′Derivative v dv = refl -- An extended explanation. proj₁′Derivative₁ : Derivative proj₁ proj₁′ proj₁′Derivative₁ (a , b) (da , db) = let v = (a , b) dv = (da , db) in begin proj₁ v ⊞ proj₁′ v dv ≡⟨⟩ a ⊞ da ≡⟨⟩ proj₁ (v ⊞ dv) ∎ -- Same for the second extractor. proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v) proj₂′ (a , b) (da , db) = db proj₂′Derivative : Derivative proj₂ proj₂′ proj₂′Derivative v dv = refl -- We should do the same for uncurry instead. -- What one could wrongly expect to be the derivative of the constructor: _,_′ : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b) _,_′ a da b db = da , db -- That has the correct behavior, in a sense, and it would be in the -- subset-based formalization in the paper. -- -- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof -- As a consequence, proving that's a derivative seems too insanely hard. We -- might want to provide a proof schema for curried functions at once, -- starting from the right correctness equation. B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B) A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}} module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}} _,_′-real : Δ _,_ _,_′-real = nil _,_ _,_′-real-Derivative : Derivative {{CA}} {{B→A×B}} _,_ (ΔA→B→A×B.apply _,_′-real) _,_′-real-Derivative = FunctionChanges.nil-is-derivative A (B → A × B) {{CA}} {{B→A×B}} _,_ _,_′′ : (a : A) → Δ a → Δ {{B→A×B}} (λ b → (a , b)) _,_′′ a da = record { apply = _,_′ a da ; correct = λ b db → begin (a , b ⊞ db) ⊞ (_,_′ a da) (b ⊞ db) (nil (b ⊞ db)) ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) ≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩ a ⊞ da , b ⊞ db ≡⟨⟩ (a , b) ⊞ (_,_′ a da) b db ∎ } {- _,_′′′ : Δ {{A→B→A×B}} _,_ _,_′′′ = record { apply = _,_′′ ; correct = λ a da → begin update (_,_ (a ⊞ da)) (_,_′′ (a ⊞ da) (nil (a ⊞ da))) ≡⟨ {!!} ⟩ update (_,_ a) (_,_′′ a da) ∎ } where -- This is needed to use update above. -- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly. open ChangeAlgebra B→A×B hiding (nil) {- {! begin (_,_ (a ⊞ da)) ⊞ _,_′′ (a ⊞ da) (nil (a ⊞ da)) {- ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) -} ≡⟨ ? ⟩ {-a ⊞ da , b ⊞ db ≡⟨⟩-} (_,_ a) ⊞ (_,_′′ a da) ∎!} } -} open import Postulate.Extensionality _,_′Derivative : Derivative {{CA}} {{B→A×B}} _,_ _,_′′ _,_′Derivative a da = begin _⊞_ {{B→A×B}} (_,_ a) (_,_′′ a da) ≡⟨⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) --ext (λ b → cong (λ □ → (a , b) ⊞ □) (update-nil {{?}} b)) ≡⟨ {!!} ⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) ≡⟨ sym {!ΔA→B→A×B.incrementalization _,_ _,_′′′ a da!} ⟩ --FunctionChanges.incrementalization A (B → A × B) {{CA}} {{{!B→A×B!}}} _,_ {!!} {!!} {!!} _,_ (a ⊞ da) ∎ -}
module Base.Change.Products where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Data.Product -- Also try defining sectioned change structures on the positives halves of -- groups? Or on arbitrary subsets? -- Restriction: we pair sets on the same level (because right now everything -- else would risk getting in the way). module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where open ≡-Reasoning -- The simplest possible definition of changes for products. PChange : A × B → Set ℓ PChange (a , b) = Δ a × Δ b -- An interesting alternative definition allows omitting the nil change of a -- component when that nil change can be computed from the type. For instance, the nil change for integers is always the same. -- However, the nil change for function isn't always the same (unless we -- defunctionalize them first), so nil changes for functions can't be omitted. _⊕_ : (v : A × B) → PChange v → A × B _⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db _⊝_ : A × B → (v : A × B) → PChange v _⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b p-nil : (v : A × B) → PChange v p-nil (a , b) = (nil a , nil b) p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u p-update-diff (ua , ub) (va , vb) = let u = (ua , ub) v = (va , vb) in begin v ⊕ (u ⊝ v) ≡⟨⟩ (va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb)) --v ⊕ ((ua ⊟ va , ub ⊟ vb)) ≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩ (ua , ub) ≡⟨⟩ u ∎ p-update-nil : (v : A × B) → v ⊕ (p-nil v) ≡ v p-update-nil (a , b) = let v = (a , b) in begin v ⊕ (p-nil v) ≡⟨⟩ (a ⊞ nil a , b ⊞ nil b) ≡⟨ cong₂ _,_ (update-nil a) (update-nil b)⟩ (a , b) ≡⟨⟩ v ∎ changeAlgebra : ChangeAlgebra ℓ (A × B) changeAlgebra = record { Change = PChange ; update = _⊕_ ; diff = _⊝_ ; nil = p-nil ; isChangeAlgebra = record { update-diff = p-update-diff ; update-nil = p-update-nil } } proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v) proj₁′ (a , b) (da , db) = da proj₁′Derivative : Derivative proj₁ proj₁′ -- Implementation note: we do not need to pattern match on v and dv because -- they are records, hence Agda knows that pattern matching on records cannot -- fail. Technically, the required feature is the eta-rule on records. proj₁′Derivative v dv = refl -- An extended explanation. proj₁′Derivative₁ : Derivative proj₁ proj₁′ proj₁′Derivative₁ (a , b) (da , db) = let v = (a , b) dv = (da , db) in begin proj₁ v ⊞ proj₁′ v dv ≡⟨⟩ a ⊞ da ≡⟨⟩ proj₁ (v ⊞ dv) ∎ -- Same for the second extractor. proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v) proj₂′ (a , b) (da , db) = db proj₂′Derivative : Derivative proj₂ proj₂′ proj₂′Derivative v dv = refl -- We should do the same for uncurry instead. -- What one could wrongly expect to be the derivative of the constructor: _,_′ : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b) _,_′ a da b db = da , db -- That has the correct behavior, in a sense, and it would be in the -- subset-based formalization in the paper. -- -- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof -- As a consequence, proving that's a derivative seems too insanely hard. We -- might want to provide a proof schema for curried functions at once, -- starting from the right correctness equation. B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B) A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}} module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}} _,_′-real : Δ _,_ _,_′-real = nil _,_ _,_′-real-Derivative : Derivative {{CA}} {{B→A×B}} _,_ (ΔA→B→A×B.apply _,_′-real) _,_′-real-Derivative = FunctionChanges.nil-is-derivative A (B → A × B) {{CA}} {{B→A×B}} _,_ _,_′′ : (a : A) → Δ a → Δ {{B→A×B}} (λ b → (a , b)) _,_′′ a da = record { apply = _,_′ a da ; correct = λ b db → begin (a , b ⊞ db) ⊞ (_,_′ a da) (b ⊞ db) (nil (b ⊞ db)) ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) ≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩ a ⊞ da , b ⊞ db ≡⟨⟩ (a , b) ⊞ (_,_′ a da) b db ∎ } {- _,_′′′ : Δ {{A→B→A×B}} _,_ _,_′′′ = record { apply = _,_′′ ; correct = λ a da → begin update (_,_ (a ⊞ da)) (_,_′′ (a ⊞ da) (nil (a ⊞ da))) ≡⟨ {!!} ⟩ update (_,_ a) (_,_′′ a da) ∎ } where -- This is needed to use update above. -- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly. open ChangeAlgebra B→A×B hiding (nil) {- {! begin (_,_ (a ⊞ da)) ⊞ _,_′′ (a ⊞ da) (nil (a ⊞ da)) {- ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) -} ≡⟨ ? ⟩ {-a ⊞ da , b ⊞ db ≡⟨⟩-} (_,_ a) ⊞ (_,_′′ a da) ∎!} } -} open import Postulate.Extensionality _,_′Derivative : Derivative {{CA}} {{B→A×B}} _,_ _,_′′ _,_′Derivative a da = begin _⊞_ {{B→A×B}} (_,_ a) (_,_′′ a da) ≡⟨⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) --ext (λ b → cong (λ □ → (a , b) ⊞ □) (update-nil {{?}} b)) ≡⟨ {!!} ⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) ≡⟨ sym {!ΔA→B→A×B.incrementalization _,_ _,_′′′ a da!} ⟩ --FunctionChanges.incrementalization A (B → A × B) {{CA}} {{{!B→A×B!}}} _,_ {!!} {!!} {!!} _,_ (a ⊞ da) ∎ -}
Correct wrong comment on changes of products
Correct wrong comment on changes of products
Agda
mit
inc-lc/ilc-agda
30936886bb410fbf699a91bd25fbf51d2878e09f
New/NewNew.agda
New/NewNew.agda
module New.NewNew where open import New.Changes open import New.LangChanges open import New.Lang open import New.Derive [_]_from_to_ : ∀ (τ : Type) → (dv : Cht τ) → (v1 v2 : ⟦ τ ⟧Type) → Set [ σ ⇒ τ ] df from f1 to f2 = ∀ (da : Cht σ) (a1 a2 : ⟦ σ ⟧Type) → [ σ ] da from a1 to a2 → [ τ ] df a1 da from f1 a1 to f2 a2 [ int ] dv from v1 to v2 = v2 ≡ v1 + dv [ pair σ τ ] (da , db) from (a1 , b1) to (a2 , b2) = [ σ ] da from a1 to a2 × [ τ ] db from b1 to b2 data [_]Γ_from_to_ : ∀ Γ → eCh Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set where v∅ : [ ∅ ]Γ ∅ from ∅ to ∅ _v•_ : ∀ {τ Γ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ] dv from v1 to v2) → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → [ τ • Γ ]Γ (dv • v1 • dρ) from (v1 • ρ1) to (v2 • ρ2) ⟦Γ≼ΔΓ⟧ : ∀ {Γ ρ1 ρ2 dρ} → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → ρ1 ≡ ⟦ Γ≼ΔΓ ⟧≼ dρ ⟦Γ≼ΔΓ⟧ v∅ = refl ⟦Γ≼ΔΓ⟧ (dvv v• dρρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ dρρ) fit-sound : ∀ {Γ τ} → (t : Term Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → ⟦ t ⟧Term ρ1 ≡ ⟦ fit t ⟧Term dρ fit-sound t dρρ = trans (cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ dρρ)) (sym (weaken-sound t _)) fromtoDeriveVar : ∀ {Γ τ} → (x : Var Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ x ⟧ΔVar ρ1 dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2) fromtoDeriveVar this (dvv v• dρρ) = dvv fromtoDeriveVar (that x) (dvv v• dρρ) = fromtoDeriveVar x dρρ fromtoDeriveConst : ∀ {τ} c → [ τ ] ⟦ deriveConst c ⟧Term ∅ from ⟦ c ⟧Const to ⟦ c ⟧Const fromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn·pq=mp·nq {a1} {da} {b1} {db} fromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m·-n=-mn {b1} {db}) = mn·pq=mp·nq {a1} {da} { - b1} { - db} fromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb fromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa fromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb fromtoDerive : ∀ {Γ} τ → (t : Term Γ τ) → {dρ : eCh Γ} {ρ1 ρ2 : ⟦ Γ ⟧Context} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ t ⟧ΔTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2) fromtoDerive τ (const c) {dρ} {ρ1} dρρ rewrite ⟦ c ⟧ΔConst-rewrite ρ1 dρ = fromtoDeriveConst c fromtoDerive τ (var x) dρρ = fromtoDeriveVar x dρρ fromtoDerive τ (app {σ} s t) dρρ rewrite sym (fit-sound t dρρ) = let fromToF = fromtoDerive (σ ⇒ τ) s dρρ in let fromToB = fromtoDerive σ t dρρ in fromToF _ _ _ fromToB fromtoDerive (σ ⇒ τ) (abs t) dρρ = λ dv v1 v2 dvv → fromtoDerive τ t (dvv v• dρρ) -- Now relate this validity with ⊕. To know that nil and so on are valid, also -- relate it to the other definition. open import Postulate.Extensionality fromto→⊕ : ∀ {τ} dv v1 v2 → [ τ ] dv from v1 to v2 → v1 ⊕ dv ≡ v2 ⊝-fromto : ∀ {τ} (v1 v2 : ⟦ τ ⟧Type) → [ τ ] v2 ⊝ v1 from v1 to v2 ⊝-fromto {σ ⇒ τ} f1 f2 da a1 a2 daa rewrite sym (fromto→⊕ _ _ _ daa) = ⊝-fromto (f1 a1) (f2 (a1 ⊕ da)) ⊝-fromto {int} v1 v2 = sym (update-diff v2 v1) ⊝-fromto {pair σ τ} (a1 , b1) (a2 , b2) = ⊝-fromto a1 a2 , ⊝-fromto b1 b2 nil-fromto : ∀ {τ} (v : ⟦ τ ⟧Type) → [ τ ] nil v from v to v nil-fromto v = ⊝-fromto v v fromto→⊕ {σ ⇒ τ} df f1 f2 dff = ext (λ v → fromto→⊕ {τ} (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v))) fromto→⊕ {int} dn n1 n2 refl = refl fromto→⊕ {pair σ τ} (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = cong₂ _,_ (fromto→⊕ _ _ _ daa) (fromto→⊕ _ _ _ dbb) open ≡-Reasoning -- If df is valid, prove (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da. -- This statement uses a ⊕ da instead of a2, which is not the style of this formalization but fits better with the other one. -- Instead, WellDefinedFunChangeFromTo (without prime) fits this formalization. WellDefinedFunChangeFromTo′ : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo′ f1 df = ∀ da a → [ _ ] da from a to (a ⊕ da) → WellDefinedFunChangePoint f1 df a da fromto→WellDefined′ : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo′ f1 df fromto→WellDefined′ {f1 = f1} {f2} {df} dff da a daa = begin (f1 ⊕ df) (a ⊕ da) ≡⟨⟩ f1 (a ⊕ da) ⊕ df (a ⊕ da) (nil (a ⊕ da)) ≡⟨ (fromto→⊕ (df (a ⊕ da) (nil (a ⊕ da))) _ _ (dff (nil (a ⊕ da)) _ _ (nil-fromto (a ⊕ da)))) ⟩ f2 (a ⊕ da) ≡⟨ sym (fromto→⊕ _ _ _ (dff da _ _ daa)) ⟩ f1 a ⊕ df a da ∎ WellDefinedFunChangeFromTo : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo f1 df = ∀ da a1 a2 → [ _ ] da from a1 to a2 → WellDefinedFunChangePoint f1 df a1 da fromto→WellDefined : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo f1 df fromto→WellDefined {f1 = f1} {f2} {df} dff da a1 a2 daa = fromto→WellDefined′ dff da a1 daa′ where daa′ : [ _ ] da from a1 to (a1 ⊕ da) daa′ rewrite fromto→⊕ da a1 a2 daa = daa -- Recursive isomorphism between the two validities. -- -- Among other things, valid→fromto proves that a validity-preserving function, -- with validity defined via (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da, is also valid -- in the "fromto" sense. -- -- We can't hope for a better statement, since we need the equation to be -- satisfied also by returned or argument functions. fromto→valid : ∀ {τ} → ∀ v1 v2 dv → [ τ ] dv from v1 to v2 → valid v1 dv valid→fromto : ∀ {τ} v (dv : Cht τ) → valid v dv → [ τ ] dv from v to (v ⊕ dv) fromto→valid {int} = λ v1 v2 dv x → tt fromto→valid {pair σ τ} (a1 , b1) (a2 , b2) (da , db) (daa , dbb) = (fromto→valid _ _ _ daa) , (fromto→valid _ _ _ dbb) fromto→valid {σ ⇒ τ} f1 f2 df dff = λ a da ada → fromto→valid _ _ _ (dff da a (a ⊕ da) (valid→fromto a da ada)) , fromto→WellDefined′ dff da a (valid→fromto _ _ ada) valid→fromto {int} v dv tt = refl valid→fromto {pair σ τ} (a , b) (da , db) (ada , bdb) = valid→fromto a da ada , valid→fromto b db bdb valid→fromto {σ ⇒ τ} f df fdf da a1 a2 daa = body where fa1da-valid : valid (f a1) (df a1 da) × WellDefinedFunChangePoint f df a1 da fa1da-valid = fdf a1 da (fromto→valid _ _ _ daa) body : [ τ ] df a1 da from f a1 to (f ⊕ df) a2 body rewrite sym (fromto→⊕ _ _ _ daa) | proj₂ fa1da-valid = valid→fromto (f a1) (df a1 da) (proj₁ fa1da-valid)
module New.NewNew where open import New.Changes open import New.LangChanges open import New.Lang open import New.Derive [_]_from_to_ : ∀ (τ : Type) → (dv : Cht τ) → (v1 v2 : ⟦ τ ⟧Type) → Set [ σ ⇒ τ ] df from f1 to f2 = ∀ (da : Cht σ) (a1 a2 : ⟦ σ ⟧Type) → [ σ ] da from a1 to a2 → [ τ ] df a1 da from f1 a1 to f2 a2 [ int ] dv from v1 to v2 = v2 ≡ v1 + dv [ pair σ τ ] (da , db) from (a1 , b1) to (a2 , b2) = [ σ ] da from a1 to a2 × [ τ ] db from b1 to b2 data [_]Γ_from_to_ : ∀ Γ → eCh Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set where v∅ : [ ∅ ]Γ ∅ from ∅ to ∅ _v•_ : ∀ {τ Γ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ] dv from v1 to v2) → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → [ τ • Γ ]Γ (dv • v1 • dρ) from (v1 • ρ1) to (v2 • ρ2) ⟦Γ≼ΔΓ⟧ : ∀ {Γ ρ1 ρ2 dρ} → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → ρ1 ≡ ⟦ Γ≼ΔΓ ⟧≼ dρ ⟦Γ≼ΔΓ⟧ v∅ = refl ⟦Γ≼ΔΓ⟧ (dvv v• dρρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ dρρ) fit-sound : ∀ {Γ τ} → (t : Term Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → ⟦ t ⟧Term ρ1 ≡ ⟦ fit t ⟧Term dρ fit-sound t dρρ = trans (cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ dρρ)) (sym (weaken-sound t _)) fromtoDeriveVar : ∀ {Γ τ} → (x : Var Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ x ⟧ΔVar ρ1 dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2) fromtoDeriveVar this (dvv v• dρρ) = dvv fromtoDeriveVar (that x) (dvv v• dρρ) = fromtoDeriveVar x dρρ fromtoDeriveConst : ∀ {τ} c → [ τ ] ⟦ deriveConst c ⟧Term ∅ from ⟦ c ⟧Const to ⟦ c ⟧Const fromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn·pq=mp·nq {a1} {da} {b1} {db} fromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m·-n=-mn {b1} {db}) = mn·pq=mp·nq {a1} {da} { - b1} { - db} fromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb fromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa fromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb fromtoDerive : ∀ {Γ} τ → (t : Term Γ τ) → {dρ : eCh Γ} {ρ1 ρ2 : ⟦ Γ ⟧Context} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ t ⟧ΔTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2) fromtoDerive τ (const c) {dρ} {ρ1} dρρ rewrite ⟦ c ⟧ΔConst-rewrite ρ1 dρ = fromtoDeriveConst c fromtoDerive τ (var x) dρρ = fromtoDeriveVar x dρρ fromtoDerive τ (app {σ} s t) dρρ rewrite sym (fit-sound t dρρ) = let fromToF = fromtoDerive (σ ⇒ τ) s dρρ in let fromToB = fromtoDerive σ t dρρ in fromToF _ _ _ fromToB fromtoDerive (σ ⇒ τ) (abs t) dρρ = λ dv v1 v2 dvv → fromtoDerive τ t (dvv v• dρρ) -- Now relate this validity with ⊕. To know that nil and so on are valid, also -- relate it to the other definition. open import Postulate.Extensionality fromto→⊕ : ∀ {τ} dv v1 v2 → [ τ ] dv from v1 to v2 → v1 ⊕ dv ≡ v2 ⊝-fromto : ∀ {τ} (v1 v2 : ⟦ τ ⟧Type) → [ τ ] v2 ⊝ v1 from v1 to v2 ⊝-fromto {σ ⇒ τ} f1 f2 da a1 a2 daa rewrite sym (fromto→⊕ _ _ _ daa) = ⊝-fromto (f1 a1) (f2 (a1 ⊕ da)) ⊝-fromto {int} v1 v2 = sym (update-diff v2 v1) ⊝-fromto {pair σ τ} (a1 , b1) (a2 , b2) = ⊝-fromto a1 a2 , ⊝-fromto b1 b2 nil-fromto : ∀ {τ} (v : ⟦ τ ⟧Type) → [ τ ] nil v from v to v nil-fromto v = ⊝-fromto v v fromto→⊕ {σ ⇒ τ} df f1 f2 dff = ext (λ v → fromto→⊕ {τ} (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v))) fromto→⊕ {int} dn n1 n2 refl = refl fromto→⊕ {pair σ τ} (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = cong₂ _,_ (fromto→⊕ _ _ _ daa) (fromto→⊕ _ _ _ dbb) open ≡-Reasoning -- If df is valid, prove (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da. -- This statement uses a ⊕ da instead of a2, which is not the style of this formalization but fits better with the other one. -- Instead, WellDefinedFunChangeFromTo (without prime) fits this formalization. WellDefinedFunChangeFromTo′ : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo′ f1 df = ∀ da a → [ _ ] da from a to (a ⊕ da) → WellDefinedFunChangePoint f1 df a da fromto→WellDefined′ : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo′ f1 df fromto→WellDefined′ {f1 = f1} {f2} {df} dff da a daa = begin (f1 ⊕ df) (a ⊕ da) ≡⟨⟩ f1 (a ⊕ da) ⊕ df (a ⊕ da) (nil (a ⊕ da)) ≡⟨ (fromto→⊕ (df (a ⊕ da) (nil (a ⊕ da))) _ _ (dff (nil (a ⊕ da)) _ _ (nil-fromto (a ⊕ da)))) ⟩ f2 (a ⊕ da) ≡⟨ sym (fromto→⊕ _ _ _ (dff da _ _ daa)) ⟩ f1 a ⊕ df a da ∎ WellDefinedFunChangeFromTo : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo f1 df = ∀ da a1 a2 → [ _ ] da from a1 to a2 → WellDefinedFunChangePoint f1 df a1 da fromto→WellDefined : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo f1 df fromto→WellDefined {f1 = f1} {f2} {df} dff da a1 a2 daa = fromto→WellDefined′ dff da a1 daa′ where daa′ : [ _ ] da from a1 to (a1 ⊕ da) daa′ rewrite fromto→⊕ da a1 a2 daa = daa -- Recursive isomorphism between the two validities. -- -- Among other things, valid→fromto proves that a validity-preserving function, -- with validity defined via (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da, is also valid -- in the "fromto" sense. -- -- We can't hope for a better statement, since we need the equation to be -- satisfied also by returned or argument functions. fromto→valid : ∀ {τ} → ∀ dv v1 v2 → [ τ ] dv from v1 to v2 → valid v1 dv valid→fromto : ∀ {τ} v (dv : Cht τ) → valid v dv → [ τ ] dv from v to (v ⊕ dv) fromto→valid {int} = λ dv v1 v2 x → tt fromto→valid {pair σ τ} (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = (fromto→valid da _ _ daa) , (fromto→valid db _ _ dbb) fromto→valid {σ ⇒ τ} df f1 f2 dff = λ a da ada → fromto→valid (df a da) (f1 a) (f2 (a ⊕ da)) (dff da a (a ⊕ da) (valid→fromto a da ada)) , fromto→WellDefined′ dff da a (valid→fromto a da ada) valid→fromto {int} v dv tt = refl valid→fromto {pair σ τ} (a , b) (da , db) (ada , bdb) = valid→fromto a da ada , valid→fromto b db bdb valid→fromto {σ ⇒ τ} f df fdf da a1 a2 daa = body where fa1da-valid : valid (f a1) (df a1 da) × WellDefinedFunChangePoint f df a1 da fa1da-valid = fdf a1 da (fromto→valid da _ _ daa) body : [ τ ] df a1 da from f a1 to (f ⊕ df) a2 body rewrite sym (fromto→⊕ da _ _ daa) | proj₂ fa1da-valid = valid→fromto (f a1) (df a1 da) (proj₁ fa1da-valid)
Fix argument order for fromto→valid
Fix argument order for fromto→valid Also make some arguments explicit where it's going to be required soon.
Agda
mit
inc-lc/ilc-agda
9806377d17063141d1be0d07fda712feb086d8b0
Syntactic/Changes.agda
Syntactic/Changes.agda
module Syntactic.Changes where -- TERMS that operate on CHANGES -- -- This module defines terms that correspond to the basic change -- operations, as well as some other helper terms. open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Changes _and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a and b = if a b false !_ : ∀ {Γ} → Term Γ bool → Term Γ bool ! x = if x false true -- Term xor _xor-term_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a xor-term b = if a (! b) b diff-term : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ) -- XXX: to finish this, we just need to call lift-term, like in -- derive-term. Should be easy, just a bit boring. -- Other problem: in fact, Δ t is not the nil change of t, in this context. That's a problem. apply-pure-term : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply-pure-term {bool} = abs (abs (var this xor-term var (that this))) apply-pure-term {τ₁ ⇒ τ₂} {Γ} = abs (abs (abs (app (app apply-pure-term (app (app (var (that (that this))) (var this)) (diff-term (var this) (var this)))) (app (var (that this)) (var this))))) --abs (abs (abs (app (app apply-compose-term (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x (Δx)) ( f x ) apply-term : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ apply-term {τ ⇒ τ₁} = λ df f → app (app apply-pure-term df) f apply-term {bool} = _xor-term_ diff-term {τ ⇒ τ₁} = λ f₁ f₂ → --λx. λdx. diff ( f₁ (apply dx x)) (f₂ x) abs (abs (diff-term {τ₁} (app (weaken ≼-in-body f₁) (apply-term (var this) (var (that this)))) (app (weaken ≼-in-body f₂) (var (that this))))) where ≼-in-body = drop (Δ-Type τ) • (drop τ • ≼-refl) diff-term {bool} = _xor-term_
module Syntactic.Changes where -- TERMS that operate on CHANGES -- -- This module defines terms that correspond to the basic change -- operations, as well as some other helper terms. open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Changes _and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a and b = if a b false !_ : ∀ {Γ} → Term Γ bool → Term Γ bool ! x = if x false true -- Term xor _xor-term_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a xor-term b = if a (! b) b diff-term : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ) -- XXX: to finish this, we just need to call lift-term, like in -- derive-term. Should be easy, just a bit boring. -- Other problem: in fact, Δ t is not the nil change of t, in this context. That's a problem. apply-pure-term : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply-pure-term {bool} = abs (abs (var this xor-term var (that this))) apply-pure-term {τ₁ ⇒ τ₂} {Γ} = abs (abs (abs (app (app apply-pure-term (app (app (var (that (that this))) (var this)) (diff-term (var this) (var this)))) (app (var (that this)) (var this))))) --abs (abs (abs (app (app apply-compose-term (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x (Δx)) ( f x ) apply-term : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ apply-term {τ ⇒ τ₁} = λ df f → app (app apply-pure-term df) f apply-term {bool} = _xor-term_ diff-term {τ ⇒ τ₁} = λ f₁ f₂ → -- The following can be written as: -- * Using Unicode symbols for change operations: -- λ x dx. (f1 (dx ⊝ x)) ⊝ (f2 x) -- * In lambda calculus with names: --λx. λdx. diff ( f₁ (apply dx x)) (f₂ x) -- * As a deBrujin-encoded term in Agda: abs (abs (diff-term {τ₁} (app (weaken ≼-in-body f₁) (apply-term (var this) (var (that this)))) (app (weaken ≼-in-body f₂) (var (that this))))) where ≼-in-body = drop (Δ-Type τ) • (drop τ • ≼-refl) diff-term {bool} = _xor-term_
clarify implementation of diff-term by adding pseudocode
agda: clarify implementation of diff-term by adding pseudocode Old-commit-hash: 531f469c648998b9b89cff66da12e2dbc01a3aea
Agda
mit
inc-lc/ilc-agda
9df1056695e49ba8d199081681450dbf409ca429
Parametric/Change/Term.agda
Parametric/Change/Term.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure (diff-base : DiffStructure) (apply-base : ApplyStructure) where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply {base ι} = diff-base , apply-base lift-diff-apply {σ ⇒ τ} = let -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ}) _⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t _⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t _⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t _⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t in abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x) , abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)) diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) diff-term = λ {τ Γ} → proj₁ (lift-diff-apply {τ} {Γ}) lift-apply : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-apply = λ {τ Γ} → proj₂ (lift-diff-apply {τ} {Γ}) diff : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ) diff = app₂ diff-term apply : ∀ {τ Γ} → Term Γ (ΔType τ) → Term Γ τ → Term Γ τ apply = app₂ lift-apply
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure (diff-base : DiffStructure) (apply-base : ApplyStructure) where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply {base ι} = diff-base , apply-base lift-diff-apply {σ ⇒ τ} = let -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ}) _⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t _⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t _⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t _⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t in abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x) , abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)) diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) diff-term = λ {τ Γ} → proj₁ (lift-diff-apply {τ} {Γ}) apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) apply-term = λ {τ Γ} → proj₂ (lift-diff-apply {τ} {Γ}) diff : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ) diff = app₂ diff-term apply : ∀ {τ Γ} → Term Γ (ΔType τ) → Term Γ τ → Term Γ τ apply = app₂ apply-term
Rename lift-apply to apply-term.
Rename lift-apply to apply-term. Old-commit-hash: b02f9113ba8fab93ce132d5a8abe88b8d4ce471c
Agda
mit
inc-lc/ilc-agda
a1b1e5915214625b81e1399a850d92f16cf11d05
Base/Change/Equivalence.agda
Base/Change/Equivalence.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Delta-observational equivalence ------------------------------------------------------------------------ module Base.Change.Equivalence where open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra open import Level open import Data.Unit module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where -- Delta-observational equivalence: these asserts that two changes -- give the same result when applied to a base value. _≙_ : ∀ dx dy → Set a dx ≙ dy = x ⊞ dx ≡ x ⊞ dy -- Same priority as ≡ infix 4 _≙_ open import Relation.Binary -- _≙_ is indeed an equivalence relation: ≙-refl : ∀ {dx} → dx ≙ dx ≙-refl = refl ≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx ≙-symm ≙ = sym ≙ ≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz ≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂ ≙-isEquivalence : IsEquivalence (_≙_) ≙-isEquivalence = record { refl = ≙-refl ; sym = ≙-symm ; trans = ≙-trans } ≙-setoid : Setoid ℓ a ≙-setoid = record { Carrier = Δ x ; _≈_ = _≙_ ; isEquivalence = ≙-isEquivalence } ------------------------------------------------------------------------ -- Convenient syntax for equational reasoning import Relation.Binary.EqReasoning as EqR -- Relation.Binary.EqReasoning is more convenient to use with _≡_ if -- the combinators take the type argument (a) as a hidden argument, -- instead of being locked to a fixed type at module instantiation -- time. module ≙-Reasoning where open EqR ≙-setoid public renaming (_≈⟨_⟩_ to _≙⟨_⟩_) -- By update-nil, if dx = nil x, then x ⊞ dx ≡ x. -- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x nil-is-⊞-unit dx dx≙nil-x = begin x ⊞ dx ≡⟨ dx≙nil-x ⟩ x ⊞ (nil x) ≡⟨ update-nil x ⟩ x ∎ where open ≡-Reasoning -- Here we prove the inverse: ⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x ⊞-unit-is-nil dx x⊞dx≡x = begin x ⊞ dx ≡⟨ x⊞dx≡x ⟩ x ≡⟨ sym (update-nil x) ⟩ x ⊞ nil x ∎ where open ≡-Reasoning -- TODO: we want to show that all functions of interest respect -- delta-observational equivalence, so that two d.o.e. changes can be -- substituted for each other freely. -- -- * That should be be true for -- functions using changes parametrically. -- -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ]; -- this is proved below on both contexts at once by fun-change-respects. -- -- * Finally, change algebra operations should respect d.o.e. But ⊞ respects -- it by definition, and ⊟ doesn't take change arguments - we will only -- need a proof for compose, when we define it. -- -- Stating the general result, though, seems hard, we should -- rather have lemmas proving that certain classes of functions respect this -- equivalence. -- This results pairs with update-diff. diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx diff-update {dx} = lemma where lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx lemma = update-diff (x ⊞ dx) x module _ {a} {b} {c} {d} {A : Set a} {B : Set b} {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where module FC = FunctionChanges A B {{CA}} {{CB}} open FC using (changeAlgebra; incrementalization) open FC.FunctionChange fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} → df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂ fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂ -- Informally: use incrementalization on both sides and then apply -- congruence. lemma = begin f x ⊞ apply df₁ x dx₁ ≡⟨ sym (incrementalization f df₁ x dx₁) ⟩ (f ⊞ df₁) (x ⊞ dx₁) ≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩ (f ⊞ df₁) (x ⊞ dx₂) ≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩ (f ⊞ df₂) (x ⊞ dx₂) ≡⟨ incrementalization f df₂ x dx₂ ⟩ f x ⊞ apply df₂ x dx₂ ∎ open import Postulate.Extensionality -- An extensionality principle for delta-observational equivalence: if -- applying two function changes to the same base value and input change gives -- a d.o.e. result, then the two function changes are d.o.e. themselves. delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = lemma₂ where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx lemma₁ = df-x-dx≙dg-x-dx lemma₂ : f ⊞ df ≡ f ⊞ dg lemma₂ = ext (λ x → lemma₁ x (nil x)) -- We know that Derivative f (apply (nil f)) (by nil-is-derivative). -- That is, df = nil f -> Derivative f (apply df). -- Now, we try to prove that if Derivative f (apply df) -> df = nil f. -- But first, we prove that f ⊞ df = f. derivative-is-⊞-unit : ∀ {f : A → B} df → Derivative f (apply df) → f ⊞ df ≡ f derivative-is-⊞-unit {f} df fdf = begin f ⊞ df ≡⟨⟩ (λ x → f x ⊞ apply df x (nil x)) ≡⟨ ext (λ x → fdf x (nil x)) ⟩ (λ x → f (x ⊞ nil x)) ≡⟨ ext (λ x → cong f (update-nil x)) ⟩ (λ x → f x) ≡⟨⟩ f ∎ where open ≡-Reasoning -- We can restate the above as "df is a nil change". derivative-is-nil : ∀ {f : A → B} df → Derivative f (apply df) → df ≙ nil f derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf) -- If we have two derivatives, they're both nil, hence they're equal. derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg derivative-unique {f} {df} {dg} fdf fdg = begin df ≙⟨ derivative-is-nil df fdf ⟩ nil f ≙⟨ sym (derivative-is-nil dg fdg) ⟩ dg ∎ where open ≙-Reasoning {- lemma : nil f ≙ dg -- Goes through --lemma = sym (derivative-is-nil dg fdg) -- Generates tons of crazy yellow ambiguities of equality type. Apparently, unifying against ≙ does not work so well. lemma = ≙-symm {{changeAlgebra}} {f} {dg} {nil f} (derivative-is-nil dg fdg) -} -- We could also use derivative-is-⊞-unit, but the proof above matches better -- with the text above.
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Delta-observational equivalence ------------------------------------------------------------------------ module Base.Change.Equivalence where open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra open import Level open import Data.Unit module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where -- Delta-observational equivalence: these asserts that two changes -- give the same result when applied to a base value. _≙_ : ∀ dx dy → Set a dx ≙ dy = x ⊞ dx ≡ x ⊞ dy -- Same priority as ≡ infix 4 _≙_ open import Relation.Binary -- _≙_ is indeed an equivalence relation: ≙-refl : ∀ {dx} → dx ≙ dx ≙-refl = refl ≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx ≙-symm ≙ = sym ≙ ≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz ≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂ ≙-isEquivalence : IsEquivalence (_≙_) ≙-isEquivalence = record { refl = ≙-refl ; sym = ≙-symm ; trans = ≙-trans } ≙-setoid : Setoid ℓ a ≙-setoid = record { Carrier = Δ x ; _≈_ = _≙_ ; isEquivalence = ≙-isEquivalence } ------------------------------------------------------------------------ -- Convenient syntax for equational reasoning import Relation.Binary.EqReasoning as EqR module ≙-Reasoning where open EqR ≙-setoid public renaming (_≈⟨_⟩_ to _≙⟨_⟩_) -- By update-nil, if dx = nil x, then x ⊞ dx ≡ x. -- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x nil-is-⊞-unit dx dx≙nil-x = begin x ⊞ dx ≡⟨ dx≙nil-x ⟩ x ⊞ (nil x) ≡⟨ update-nil x ⟩ x ∎ where open ≡-Reasoning -- Here we prove the inverse: ⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x ⊞-unit-is-nil dx x⊞dx≡x = begin x ⊞ dx ≡⟨ x⊞dx≡x ⟩ x ≡⟨ sym (update-nil x) ⟩ x ⊞ nil x ∎ where open ≡-Reasoning -- TODO: we want to show that all functions of interest respect -- delta-observational equivalence, so that two d.o.e. changes can be -- substituted for each other freely. -- -- * That should be be true for -- functions using changes parametrically. -- -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ]; -- this is proved below on both contexts at once by fun-change-respects. -- -- * Finally, change algebra operations should respect d.o.e. But ⊞ respects -- it by definition, and ⊟ doesn't take change arguments - we will only -- need a proof for compose, when we define it. -- -- Stating the general result, though, seems hard, we should -- rather have lemmas proving that certain classes of functions respect this -- equivalence. -- This results pairs with update-diff. diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx diff-update {dx} = lemma where lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx lemma = update-diff (x ⊞ dx) x module _ {a} {b} {c} {d} {A : Set a} {B : Set b} {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where module FC = FunctionChanges A B {{CA}} {{CB}} open FC using (changeAlgebra; incrementalization) open FC.FunctionChange fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} → df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂ fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂ -- Informally: use incrementalization on both sides and then apply -- congruence. lemma = begin f x ⊞ apply df₁ x dx₁ ≡⟨ sym (incrementalization f df₁ x dx₁) ⟩ (f ⊞ df₁) (x ⊞ dx₁) ≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩ (f ⊞ df₁) (x ⊞ dx₂) ≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩ (f ⊞ df₂) (x ⊞ dx₂) ≡⟨ incrementalization f df₂ x dx₂ ⟩ f x ⊞ apply df₂ x dx₂ ∎ open import Postulate.Extensionality -- An extensionality principle for delta-observational equivalence: if -- applying two function changes to the same base value and input change gives -- a d.o.e. result, then the two function changes are d.o.e. themselves. delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = lemma₂ where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx lemma₁ = df-x-dx≙dg-x-dx lemma₂ : f ⊞ df ≡ f ⊞ dg lemma₂ = ext (λ x → lemma₁ x (nil x)) -- We know that Derivative f (apply (nil f)) (by nil-is-derivative). -- That is, df = nil f -> Derivative f (apply df). -- Now, we try to prove that if Derivative f (apply df) -> df = nil f. -- But first, we prove that f ⊞ df = f. derivative-is-⊞-unit : ∀ {f : A → B} df → Derivative f (apply df) → f ⊞ df ≡ f derivative-is-⊞-unit {f} df fdf = begin f ⊞ df ≡⟨⟩ (λ x → f x ⊞ apply df x (nil x)) ≡⟨ ext (λ x → fdf x (nil x)) ⟩ (λ x → f (x ⊞ nil x)) ≡⟨ ext (λ x → cong f (update-nil x)) ⟩ (λ x → f x) ≡⟨⟩ f ∎ where open ≡-Reasoning -- We can restate the above as "df is a nil change". derivative-is-nil : ∀ {f : A → B} df → Derivative f (apply df) → df ≙ nil f derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf) -- If we have two derivatives, they're both nil, hence they're equal. derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg derivative-unique {f} {df} {dg} fdf fdg = begin df ≙⟨ derivative-is-nil df fdf ⟩ nil f ≙⟨ sym (derivative-is-nil dg fdg) ⟩ dg ∎ where open ≙-Reasoning {- lemma : nil f ≙ dg -- Goes through --lemma = sym (derivative-is-nil dg fdg) -- Generates tons of crazy yellow ambiguities of equality type. Apparently, unifying against ≙ does not work so well. lemma = ≙-symm {{changeAlgebra}} {f} {dg} {nil f} (derivative-is-nil dg fdg) -} -- We could also use derivative-is-⊞-unit, but the proof above matches better -- with the text above.
Delete copy-n-pasted-by-mistake comment
Delete copy-n-pasted-by-mistake comment This comes directly from Relation.Binary.PropositionalEquality, and is totally irrelevant here. Old-commit-hash: d8c5accf1966a913db36fefe149720366811965c
Agda
mit
inc-lc/ilc-agda
71d3cf829a84e65c7c028a11e8b02c35297f9f49
experiments/Coerce.agda
experiments/Coerce.agda
{-# OPTIONS --guardedness-preserving-type-constructors #-} -- Agda specification of `coerce` -- -- Incomplete: too much work just to make type system happy. -- Conversions between morally identical types obscure -- the operational content. open import Data.Product open import Data.Maybe open import Data.Bool open import Data.Sum open import Data.Fin hiding (_+_) open import Data.Vec import Data.Nat as ℕ open ℕ using (ℕ ; suc ; zero) open import Coinduction renaming (Rec to Fix ; fold to roll ; unfold to unroll) data Data (ℓ : ℕ) : Set where base : ℕ → Data ℓ pair : Data ℓ → Data ℓ → Data ℓ plus : Data ℓ → Data ℓ → Data ℓ var : Fin ℓ → Data ℓ -- de Bruijn index fix : Data (suc ℓ) → Data ℓ try : ∀ {ℓ m} {A : Set ℓ} {B : Set m} → B → B → A → A try a b b₂ = b₂ weaken-fin : ∀{n} (m : ℕ) → Fin n → Fin (m ℕ.+ n) weaken-fin zero x = x weaken-fin (suc m) x = suc (weaken-fin m x) -- this terminates for sure {-# NO_TERMINATION_CHECK #-} weaken : ∀ {n} → (m : ℕ) → Data n → Data (m ℕ.+ n) weaken 0 t = t weaken m (base x) = base x weaken m (pair S T) = pair (weaken m S) (weaken m T) weaken m (plus S T) = plus (weaken m S) (weaken m T) weaken m (var x) = var (weaken-fin m x) weaken 1 (fix T) = fix (weaken 1 T) weaken (suc m) (fix T) = weaken 1 (weaken m (fix T)) -- universe construction; always terminating. {-# NO_TERMINATION_CHECK #-} uc : ∀ {ℓ} → Data ℓ → Vec Set ℓ → Set uc (base n) _ = Fin n uc (pair σ τ) ρ = uc σ ρ × uc τ ρ uc (plus σ τ) ρ = uc σ ρ ⊎ uc τ ρ uc (var i) ρ = lookup i ρ uc (fix τ) ρ = set where set = Fix (♯ (uc τ (set ∷ ρ))) my-nat-data : Data 0 my-nat-data = fix (plus (base 1) (var zero)) My-nat : Set My-nat = uc my-nat-data [] my-zero : My-nat my-zero = roll (inj₁ zero) my-one : My-nat my-one = roll (inj₂ (roll (inj₁ zero))) -- nonterminating stuff botData : Data 0 botData = fix (var zero) botType : Set botType = uc botData [] {-# NON_TERMINATING #-} ⋯ : botType ⋯ = roll (roll (roll (roll (roll (roll ⋯))))) -- object language: STLC with fix infixr 3 _⇒_ -- is a coinductive datatype data Type : Set₁ where base : (ι : Set) → Type _⇒_ : (σ : Type) → (τ : Type) → Type _*_ : ∞ Type → ∞ Type → Type _+_ : ∞ Type → ∞ Type → Type -- is generative on coinductive data {-# NO_TERMINATION_CHECK #-} val : Type → Set val (base ι) = ι val (σ ⇒ τ) = val σ → val τ val (σ * τ) = val (♭ σ) × val (♭ τ) val (σ + τ) = val (♭ σ) ⊎ val (♭ τ) Context : ℕ → Set₁ Context = Vec Type data Term {n} (Γ : Context n) : Type → Set₁ where var : (i : Fin n) → Term Γ (lookup i Γ) app : ∀ {σ τ} → (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ abs : ∀ {σ τ} → (t : Term (σ ∷ Γ) τ) → Term Γ (σ ⇒ τ) fix : ∀ {τ} → Term Γ (τ ⇒ τ) → Term Γ τ -- intro/elim products pair : ∀ {σ τ} → Term Γ σ → Term Γ τ → Term Γ (♯ σ * ♯ τ) uncr : ∀ {σ τ β} → Term Γ (σ ⇒ τ ⇒ β) → Term Γ (♯ σ * ♯ τ) → Term Γ β -- intro/elim sums left : ∀ {σ τ} → Term Γ σ → Term Γ (♯ σ + ♯ τ) rite : ∀ {σ τ} → Term Γ τ → Term Γ (♯ σ + ♯ τ) mach : ∀ {σ τ β} → Term Γ (σ ⇒ β) → Term Γ (τ ⇒ β) → Term Γ (♯ σ + ♯ τ) → Term Γ β -- intro/elim base types base : ∀ {S : Set} → S → Term Γ (base S) call : ∀ {S T : Set} → Term Γ (base (S → T)) → Term Γ (base S) → Term Γ (base T) data Env : ∀ {n} (Γ : Context n) → Set₁ where [] : Env [] _∷_ : ∀ {n τ} {Γ : Context n} → val τ → Env Γ → Env (τ ∷ Γ) seek : ∀ {n} {Γ : Context n} → (i : Fin n) → Env Γ → val (lookup i Γ) seek zero (v ∷ _) = v seek (suc i) (_ ∷ ρ) = seek i ρ {-# NON_TERMINATING #-} eval : ∀ {n} {Γ : Context n} {τ} → Term Γ τ → Env Γ → val τ eval (var x) ρ = seek x ρ eval (app s t) ρ = eval s ρ (eval t ρ) eval (abs t) ρ = λ v → eval t (v ∷ ρ) eval (fix f) ρ = eval (app f (fix f)) ρ eval (pair s t) ρ = eval s ρ , eval t ρ eval (uncr f p) ρ = uncurry (eval f ρ) (eval p ρ) eval (left x) ρ = inj₁ (eval x ρ) eval (rite y) ρ = inj₂ (eval y ρ) eval (mach f g t) ρ = [ eval f ρ , eval g ρ ]′ (eval t ρ) eval (base v) ρ = v eval (call f x) ρ = eval f ρ (eval x ρ) nat-eq : ℕ → ℕ → Bool nat-eq zero zero = true nat-eq (suc m) (suc n) = nat-eq m n nat-eq _ _ = false fin-eq : ∀ {n} → Fin n → Fin n → Bool fin-eq m n = nat-eq (toℕ m) (toℕ n) infix 7 _==_ _==_ : ∀ {n} → Data n → Data n → Bool base x == base y = nat-eq x y pair σ₁ τ₁ == pair σ₂ τ₂ = σ₁ == σ₂ ∧ τ₁ == τ₂ plus σ₁ τ₁ == plus σ₂ τ₂ = σ₁ == σ₂ ∧ τ₁ == τ₂ var x == var y = fin-eq x y fix σ == fix τ = σ == τ _ == _ = false -- this is nonterminating for sure (because we use equirecursion) -- but it's generative (as in coinduction) -- we want it to expand a couple times during type checking {-# NO_TERMINATION_CHECK #-} type-of : ∀ {n} → Context n → Data n → Type type-of _ (base n) = base (Fin n) type-of Γ (pair σ τ) = ♯ (type-of Γ σ) * ♯ (type-of Γ τ) type-of Γ (plus σ τ) = ♯ (type-of Γ σ) + ♯ (type-of Γ τ) type-of Γ (var x) = lookup x Γ type-of Γ (fix τ) = type-of (type-of Γ (fix τ) ∷ Γ) τ inject-fin : ∀ {m n} → m ℕ.≤ n → Fin m → Fin n inject-fin {zero} {zero} pf () inject-fin {zero} {suc n} pf () inject-fin {suc m} {zero} () i inject-fin {suc m} {suc n} pf zero = zero inject-fin {suc m} {suc n} (ℕ.s≤s pf) (suc i) = suc (inject-fin pf i) mapMaybe : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → Maybe A → Maybe B mapMaybe f (just x) = just (f x) mapMaybe f nothing = nothing {- have to do weakening properly... weak : ∀ {n} {Γ : Context n} {σ τ} → Term Γ τ → Term (σ ∷ Γ) τ weak (var i) = var (suc i) weak (app t t₁) = app (weak t) (weak t₁) weak {Γ = []} (abs t) = abs (try t {!!} {!!}) weak {Γ = x ∷ Γ} (abs t) = {!!} weak (fix t) = fix (weak t) weak (pair t t₁) = pair (weak t) (weak t₁) weak (uncr t t₁) = uncr (weak t) (weak t₁) weak (left t) = left (weak t) weak (rite t) = rite (weak t) weak (mach t t₁ t₂) = mach (weak t) (weak t₁) (weak t₂) weak (base x) = base x weak (call t t₁) = call (weak t) (weak t₁) -} Src : ∀ {n} → Context n → Data n → Data n → Set₁ Src Γ S T = Term Γ (type-of Γ S ⇒ type-of Γ T) Witness : ∀ {n} → Context n → Set₁ Witness {n} Γ = (S : Data n) → (T : Data n) → Maybe (Src Γ S T) Result : ∀ {n} → Data n → Data n → Set₁ Result {n} S T = Σ ℕ (λ m′ → let m = m′ ℕ.+ n in Σ (Context m) (λ Γ′ → Witness Γ′ × Src Γ′ (weaken m′ S) (weaken m′ T))) coerce-src : ∀ {n} {Γ : Context n} → Witness Γ → (S : Data n) → (T : Data n) → Maybe (Result S T) coerce-src {n} {Γ} A S T = match (A S T) where σ→τ : Type σ→τ = type-of Γ S ⇒ type-of Γ T A₀ : Witness (σ→τ ∷ Γ) A₀ S' T' = if S' == weaken 1 S ∧ T' == weaken 1 T then {!!} -- just (var zero) -- error due to lack of awareness of equality else weaken-witness A S' T' where weaken-witness : Witness Γ → Witness (σ→τ ∷ Γ) weaken-witness = {!!} -- some form of fmap'ed weeakening inspect : (S : Data n) → (T : Data n) → Maybe (Result S T) inspect (base m) (base n) = {!!} {- inj (m ℕ.≤? n) where open import Relation.Nullary.Core inj : Dec (m ℕ.≤ n) → Maybe (Witness Γ × Src Γ (base m) (base n)) inj (yes m≤n) = just ({!!} , abs (call (base (inject-fin m≤n)) (var zero))) inj (no _) = nothing -} inspect (pair S₁ S₂) (pair T₁ T₂) = {!!} inspect (plus S₁ S₂) (plus T₁ T₂) = {!!} inspect (var x) T = {!!} inspect (fix S) T = {!!} inspect _ _ = nothing match : Maybe (Src Γ S T) → Maybe (Result S T) match (just t) = just (0 , Γ , A , t) match nothing = inspect S T
{-# OPTIONS --guardedness-preserving-type-constructors #-} -- Agda specification of `coerce` -- -- Incomplete: too much work just to make type system happy. -- Conversions between morally identical types obscure -- the operational content. open import Function open import Data.Product open import Data.Maybe open import Data.Bool hiding (_≟_) open import Data.Sum open import Data.Nat import Data.Fin as Fin open Fin using (Fin ; zero ; suc) open import Relation.Nullary using (Dec ; yes ; no) open import Coinduction renaming (Rec to Fix ; fold to roll ; unfold to unroll) -- Things fail sometimes postulate fail : ∀ {ℓ} {A : Set ℓ} → A -- I know what I'm doing postulate unsafeCast : ∀ {A B : Set} → A → B data Data : Set where base : ℕ → Data pair : Data → Data → Data plus : Data → Data → Data var : ℕ → Data fix : Data → Data -- infinite environments Env : ∀ {ℓ} → Set ℓ → Set ℓ Env A = ℕ → A infixr 0 decide_then_else_ decide_then_else_ : ∀ {p ℓ} {P : Set p} {A : Set ℓ} → Dec P → A → A → A decide yes p then x else y = x decide no ¬p then x else y = y --update : ∀ {ℓ} {A : Set ℓ} → ℕ → A → Env A → Env A --update n v env = λ m → decide n ≟ m then v else env m prepend : ∀ {ℓ} {A : Set ℓ} → A → Env A → Env A prepend v env zero = v prepend v env (suc m) = env m -- universe construction; always terminating. {-# NO_TERMINATION_CHECK #-} uc : Data → Env Set → Set uc (base x) ρ = Fin x uc (pair S T) ρ = uc S ρ × uc T ρ uc (plus S T) ρ = uc S ρ ⊎ uc T ρ uc (var x) ρ = ρ x uc (fix D) ρ = set where set = Fix (♯ (uc D (prepend set ρ))) ∅ : ∀ {ℓ} {A : Set ℓ} → Env A ∅ = fail my-nat-data : Data my-nat-data = fix (plus (base 1) (var zero)) My-nat : Set My-nat = uc my-nat-data ∅ my-zero : My-nat my-zero = roll (inj₁ zero) my-one : My-nat my-one = roll (inj₂ (roll (inj₁ zero))) -- nonterminating stuff botData : Data botData = fix (var zero) botType : Set botType = uc botData ∅ {-# NON_TERMINATING #-} ⋯ : botType ⋯ = roll (roll (roll (roll (roll (roll ⋯))))) shift : ℕ → ℕ → Data → Data shift c d (base x) = base x shift c d (pair S T) = pair (shift c d S) (shift c d T) shift c d (plus S T) = plus (shift c d S) (shift c d T) shift c d (var k) = decide suc k ≤? c then var k else var (k + d) shift c d (fix D) = fix (shift c d D) _[_↦_] : Data → ℕ → Data → Data base x [ n ↦ S ] = base x pair T T₁ [ n ↦ S ] = pair (T [ n ↦ S ]) (T₁ [ n ↦ S ]) plus T T₁ [ n ↦ S ] = plus (T [ n ↦ S ]) (T₁ [ n ↦ S ]) var k [ n ↦ S ] = decide k ≟ n then S else var k fix T [ n ↦ S ] = fix (T [ n + 1 ↦ shift 0 1 S ]) import Level open import Category.Monad open RawMonad {Level.zero} monad {-# NON_TERMINATING #-} coerce : (S : Data) → (T : Data) → Maybe (uc S ∅ → (uc T ∅)) coerce (base m) (base n) with m ≤? n ... | yes m≤n = just (λ s → Fin.inject≤ s m≤n) ... | no m>n = nothing coerce (pair S₁ S₂) (pair T₁ T₂) = coerce S₁ T₁ >>= (λ f₁ → coerce S₂ T₂ >>= (λ f₂ → return (λ s → f₁ (proj₁ s) , f₂ (proj₂ s)))) coerce (plus S₁ S₂) (plus T₁ T₂) = coerce S₁ T₁ >>= (λ f₁ → coerce S₂ T₂ >>= (λ f₂ → return [ inj₁ ∘ f₁ , inj₂ ∘ f₂ ])) coerce (var j) T = nothing -- should not happen coerce (fix S) T = let S′ = S [ 0 ↦ fix S ] in coerce S′ T >>= (λ f → return (λ { (roll x) → f (unsafeCast x) })) coerce S (fix T) = let T′ = T [ 0 ↦ fix T ] in coerce S T′ >>= (λ f → return (λ x → (roll (unsafeCast (f x))))) coerce _ _ = nothing
make the Agda specification of `coerce` useful for humans
make the Agda specification of `coerce` useful for humans Type-safety is sacrificed to describe `coerce` without prohibitive administrative overhead. Unfortunately, `coerce` calls a postulate on every encounter of a recursive type. It is only good for humans to read right now; it is not executable.
Agda
mit
yfcai/CREG
c4286bde265ec051c1ed0b2dc4b3f95ba028710d
Denotation/Implementation/Popl14.agda
Denotation/Implementation/Popl14.agda
module Denotation.Implementation.Popl14 where -- Notions of programs being implementations of specifications -- for Calculus Popl14 open import Denotation.Specification.Canon-Popl14 public open import Popl14.Syntax.Type open import Popl14.Syntax.Term open import Popl14.Change.Derive open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Product open import Data.Integer open import Structure.Bag.Popl14 open import Postulate.Extensionality open import Popl14.Denotation.WeakenSound -- Better name for v ≈ ⟦ t ⟧: -- t implements v. infix 4 _≈_ _≈_ : ∀ {τ} → ΔVal τ → ⟦ ΔType τ ⟧ → Set _≈_ {int} u v = u ≡ v _≈_ {bag} u v = u ≡ v _≈_ {σ ⇒ τ} u v = (w : ⟦ σ ⟧) (Δw : ΔVal σ) (Δw′ : ⟦ ΔType σ ⟧) (R[w,Δw] : valid w Δw) (Δw≈Δw′ : Δw ≈ Δw′) → u w Δw R[w,Δw] ≈ v w Δw′ module Disambiguation (τ : Type) where _✚_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ _✚_ = _⟦⊕⟧_ {τ} _−_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ _−_ = _⟦⊝⟧_ {τ} infixl 6 _✚_ _−_ _≃_ : ΔVal τ → ⟦ ΔType τ ⟧ → Set _≃_ = _≈_ {τ} infix 4 _≃_ module FunctionDisambiguation (σ : Type) (τ : Type) where open Disambiguation (σ ⇒ τ) public _✚₁_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ _✚₁_ = _⟦⊕⟧_ {τ} _−₁_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ _−₁_ = _⟦⊝⟧_ {τ} infixl 6 _✚₁_ _−₁_ _≃₁_ : ΔVal τ → ⟦ ΔType τ ⟧ → Set _≃₁_ = _≈_ {τ} infix 4 _≃₁_ _✚₀_ : ⟦ σ ⟧ → ⟦ ΔType σ ⟧ → ⟦ σ ⟧ _✚₀_ = _⟦⊕⟧_ {σ} _−₀_ : ⟦ σ ⟧ → ⟦ σ ⟧ → ⟦ ΔType σ ⟧ _−₀_ = _⟦⊝⟧_ {σ} infixl 6 _✚₀_ _−₀_ _≃₀_ : ΔVal σ → ⟦ ΔType σ ⟧ → Set _≃₀_ = _≈_ {σ} infix 4 _≃₀_ compatible : ∀ {Γ} → ΔEnv Γ → ⟦ ΔContext Γ ⟧ → Set compatible {∅} ∅ ∅ = ⊤ compatible {τ • Γ} (cons v Δv _ ρ) (Δv′ • v′ • ρ′) = Triple (v ≡ v′) (λ _ → Δv ≈ Δv′) (λ _ _ → compatible ρ ρ′) -- If a program implements a specification, then certain things -- proven about the specification carry over to the programs. carry-over : ∀ {τ} {v : ⟦ τ ⟧} {Δv : ΔVal τ} {Δv′ : ⟦ ΔType τ ⟧} (R[v,Δv] : valid v Δv) (Δv≈Δv′ : Δv ≈ Δv′) → let open Disambiguation τ in v ⊞ Δv ≡ v ✚ Δv′ u⊟v≈u⊝v : ∀ {τ : Type} {u v : ⟦ τ ⟧} → let open Disambiguation τ in u ⊟ v ≃ u − v u⊟v≈u⊝v {int} = refl u⊟v≈u⊝v {bag} = refl u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where open FunctionDisambiguation σ τ result : (w : ⟦ σ ⟧) (Δw : ΔVal σ) (Δw′ : ⟦ ΔType σ ⟧) → valid w Δw → Δw ≈ Δw′ → g (w ⊞ Δw) ⊟ f w ≃₁ g (w ✚₀ Δw′) −₁ f w result w Δw Δw′ R[w,Δw] Δw≈Δw′ rewrite carry-over {σ} {w} R[w,Δw] Δw≈Δw′ = u⊟v≈u⊝v {τ} {g (w ✚₀ Δw′)} {f w} carry-over {int} {v} tt Δv≈Δv′ = cong (_+_ v) Δv≈Δv′ carry-over {bag} {v} tt Δv≈Δv′ = cong (_++_ v) Δv≈Δv′ carry-over {σ ⇒ τ} {f} {Δf} {Δf′} R[f,Δf] Δf≈Δf′ = ext (λ v → let open FunctionDisambiguation σ τ V = R[v,u-v] {σ} {v} {v} S = u⊟v≈u⊝v {σ} {v} {v} in carry-over {τ} {f v} {Δf v (v ⊟ v) V} {Δf′ v (v −₀ v)} (proj₁ (R[f,Δf] v (v ⊟ v) V)) (Δf≈Δf′ v (v ⊟ v) (v −₀ v) V S)) -- A property relating `ignore` and the subcontext relation Γ≼ΔΓ ⟦Γ≼ΔΓ⟧ : ∀ {Γ} {ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧} (C : compatible ρ ρ′) → ignore ρ ≡ ⟦ Γ≼ΔΓ ⟧ ρ′ ⟦Γ≼ΔΓ⟧ {∅} {∅} {∅} _ = refl ⟦Γ≼ΔΓ⟧ {τ • Γ} {cons v dv _ ρ} {dv′ • v′ • ρ′} (cons v≡v′ _ C) = cong₂ _•_ v≡v′ (⟦Γ≼ΔΓ⟧ C) -- A specialization of the soundness of weakening ⟦fit⟧ : ∀ {τ Γ} (t : Term Γ τ) {ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧} (C : compatible ρ ρ′) → ⟦ t ⟧ (ignore ρ) ≡ ⟦ fit t ⟧ ρ′ ⟦fit⟧ t {ρ} {ρ′} C = trans (cong ⟦ t ⟧ (⟦Γ≼ΔΓ⟧ C)) (sym (weaken-sound t ρ′))
module Denotation.Implementation.Popl14 where -- Notions of programs being implementations of specifications -- for Calculus Popl14 open import Denotation.Specification.Canon-Popl14 public open import Popl14.Syntax.Type open import Popl14.Syntax.Term open import Popl14.Change.Derive open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Product open import Data.Integer open import Structure.Bag.Popl14 open import Postulate.Extensionality open import Popl14.Denotation.WeakenSound infix 4 implements syntax implements τ u v = u ≈₍ τ ₎ v implements : ∀ (τ : Type) → ΔVal τ → ⟦ ΔType τ ⟧ → Set u ≈₍ int ₎ v = u ≡ v u ≈₍ bag ₎ v = u ≡ v u ≈₍ σ ⇒ τ ₎ v = (w : ⟦ σ ⟧) (Δw : ΔVal σ) (Δw′ : ⟦ ΔType σ ⟧) (R[w,Δw] : valid {σ} w Δw) (Δw≈Δw′ : implements σ Δw Δw′) → implements τ (u w Δw R[w,Δw]) (v w Δw′) infix 4 _≈_ _≈_ : ∀ {τ} → ΔVal τ → ⟦ ΔType τ ⟧ → Set _≈_ {τ} = implements τ module Disambiguation (τ : Type) where _✚_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ _✚_ = _⟦⊕⟧_ {τ} _−_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ _−_ = _⟦⊝⟧_ {τ} infixl 6 _✚_ _−_ _≃_ : ΔVal τ → ⟦ ΔType τ ⟧ → Set _≃_ = _≈_ {τ} infix 4 _≃_ module FunctionDisambiguation (σ : Type) (τ : Type) where open Disambiguation (σ ⇒ τ) public _✚₁_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ _✚₁_ = _⟦⊕⟧_ {τ} _−₁_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ _−₁_ = _⟦⊝⟧_ {τ} infixl 6 _✚₁_ _−₁_ _≃₁_ : ΔVal τ → ⟦ ΔType τ ⟧ → Set _≃₁_ = _≈_ {τ} infix 4 _≃₁_ _✚₀_ : ⟦ σ ⟧ → ⟦ ΔType σ ⟧ → ⟦ σ ⟧ _✚₀_ = _⟦⊕⟧_ {σ} _−₀_ : ⟦ σ ⟧ → ⟦ σ ⟧ → ⟦ ΔType σ ⟧ _−₀_ = _⟦⊝⟧_ {σ} infixl 6 _✚₀_ _−₀_ _≃₀_ : ΔVal σ → ⟦ ΔType σ ⟧ → Set _≃₀_ = _≈_ {σ} infix 4 _≃₀_ compatible : ∀ {Γ} → ΔEnv Γ → ⟦ ΔContext Γ ⟧ → Set compatible {∅} ∅ ∅ = ⊤ compatible {τ • Γ} (cons v Δv _ ρ) (Δv′ • v′ • ρ′) = Triple (v ≡ v′) (λ _ → Δv ≈ Δv′) (λ _ _ → compatible ρ ρ′) -- If a program implements a specification, then certain things -- proven about the specification carry over to the programs. carry-over : ∀ {τ} {v : ⟦ τ ⟧} {Δv : ΔVal τ} {Δv′ : ⟦ ΔType τ ⟧} (R[v,Δv] : valid v Δv) (Δv≈Δv′ : Δv ≈ Δv′) → let open Disambiguation τ in v ⊞ Δv ≡ v ✚ Δv′ u⊟v≈u⊝v : ∀ {τ : Type} {u v : ⟦ τ ⟧} → let open Disambiguation τ in u ⊟ v ≃ u − v u⊟v≈u⊝v {int} = refl u⊟v≈u⊝v {bag} = refl u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where open FunctionDisambiguation σ τ result : (w : ⟦ σ ⟧) (Δw : ΔVal σ) (Δw′ : ⟦ ΔType σ ⟧) → valid w Δw → Δw ≈ Δw′ → g (w ⊞ Δw) ⊟ f w ≃₁ g (w ✚₀ Δw′) −₁ f w result w Δw Δw′ R[w,Δw] Δw≈Δw′ rewrite carry-over {σ} {w} R[w,Δw] Δw≈Δw′ = u⊟v≈u⊝v {τ} {g (w ✚₀ Δw′)} {f w} carry-over {int} {v} tt Δv≈Δv′ = cong (_+_ v) Δv≈Δv′ carry-over {bag} {v} tt Δv≈Δv′ = cong (_++_ v) Δv≈Δv′ carry-over {σ ⇒ τ} {f} {Δf} {Δf′} R[f,Δf] Δf≈Δf′ = ext (λ v → let open FunctionDisambiguation σ τ V = R[v,u-v] {σ} {v} {v} S = u⊟v≈u⊝v {σ} {v} {v} in carry-over {τ} {f v} {Δf v (v ⊟ v) V} {Δf′ v (v −₀ v)} (proj₁ (R[f,Δf] v (v ⊟ v) V)) (Δf≈Δf′ v (v ⊟ v) (v −₀ v) V S)) -- A property relating `ignore` and the subcontext relation Γ≼ΔΓ ⟦Γ≼ΔΓ⟧ : ∀ {Γ} {ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧} (C : compatible ρ ρ′) → ignore ρ ≡ ⟦ Γ≼ΔΓ ⟧ ρ′ ⟦Γ≼ΔΓ⟧ {∅} {∅} {∅} _ = refl ⟦Γ≼ΔΓ⟧ {τ • Γ} {cons v dv _ ρ} {dv′ • v′ • ρ′} (cons v≡v′ _ C) = cong₂ _•_ v≡v′ (⟦Γ≼ΔΓ⟧ C) -- A specialization of the soundness of weakening ⟦fit⟧ : ∀ {τ Γ} (t : Term Γ τ) {ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧} (C : compatible ρ ρ′) → ⟦ t ⟧ (ignore ρ) ≡ ⟦ fit t ⟧ ρ′ ⟦fit⟧ t {ρ} {ρ′} C = trans (cong ⟦ t ⟧ (⟦Γ≼ΔΓ⟧ C)) (sym (weaken-sound t ρ′))
Support type arguments for infix ≈.
Support type arguments for infix ≈. Old-commit-hash: e96c855b72f3792e2bc0279596338ed2c82c4c8b
Agda
mit
inc-lc/ilc-agda
be5009e736f000abc308cda35f51ca8c075da427
Parametric/Change/Correctness.agda
Parametric/Change/Correctness.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity import Parametric.Change.Specification as Specification import Parametric.Change.Type as ChangeType import Parametric.Change.Term as ChangeTerm import Parametric.Change.Value as ChangeValue import Parametric.Change.Evaluation as ChangeEvaluation import Parametric.Change.Derive as Derive import Parametric.Change.Implementation as Implementation module Parametric.Change.Correctness {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (diff-base : ChangeTerm.DiffStructure Const ΔBase) (apply-base : ChangeTerm.ApplyStructure Const ΔBase) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) (meaning-⊕-base : ChangeEvaluation.ApplyStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (meaning-⊝-base : ChangeEvaluation.DiffStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (validity-structure : Validity.Structure ⟦_⟧Base) (specification-structure : Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure) (derive-const : Derive.Structure Const ΔBase) (implementation-structure : Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure open ChangeType.Structure Base ΔBase open ChangeTerm.Structure Const ΔBase diff-base apply-base open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open ChangeEvaluation.Structure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧ meaning-⊕-base meaning-⊝-base open Derive.Structure Const ΔBase derive-const open Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure -- The denotational properties of the `derive` transformation. -- In particular, the main theorem about it producing the correct -- incremental behavior. open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality Structure : Set Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → (ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) → ⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′) module Structure (derive-const-correct : Structure) where deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′) deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′ deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′ -- That `derive t` implements ⟦ t ⟧Δ derive-correct : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′) derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′)) derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅ derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ = derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′ derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ = derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′ (derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′) derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ = deriveVar-correct x ρ dρ ρ′ dρ≈ρ′ derive-correct (app s t) ρ dρ ρ′ dρ≈ρ′ rewrite sym (⟦fit⟧ t ρ ρ′) = derive-correct s ρ dρ ρ′ dρ≈ρ′ (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct t ρ dρ ρ′ dρ≈ρ′) derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ = λ w dw w′ dw≈w′ → derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′) main-theorem : ∀ {σ τ} {f : Term ∅ (σ ⇒ τ)} {x : Term ∅ σ} {y : Term ∅ σ} → ⟦ app f y ⟧ ≡ ⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) (y ⊝ x) ⟧ main-theorem {σ} {τ} {f} {x} {y} = let h = ⟦ f ⟧ ∅ Δh = ⟦ f ⟧Δ ∅ ∅ Δh′ = ⟦ derive f ⟧ ∅ v = ⟦ x ⟧ ∅ u = ⟦ y ⟧ ∅ Δoutput-term = app (app (derive f) x) (y ⊝ x) in ext {A = ⟦ ∅ ⟧Context} (λ { ∅ → begin h u ≡⟨ cong h (sym (v+[u-v]=u {σ})) ⟩ h (v ⊞₍ σ ₎ (u ⊟₍ σ ₎ v)) ≡⟨ corollary-closed {σ} {τ} f v (u ⊟₍ σ ₎ v) ⟩ h v ⊞₍ τ ₎ call-change {σ} {τ} Δh v (u ⊟₍ σ ₎ v) ≡⟨ carry-over {τ} (call-change {σ} {τ} Δh v (u ⊟₍ σ ₎ v)) (derive-correct f ∅ ∅ ∅ ∅ v (u ⊟₍ σ ₎ v) (u ⟦⊝₍ σ ₎⟧ v) (u⊟v≈u⊝v {σ} {u} {v})) ⟩ h v ⟦⊕₍ τ ₎⟧ Δh′ v (u ⟦⊝₍ σ ₎⟧ v) ≡⟨ trans (cong (λ hole → h v ⟦⊕₍ τ ₎⟧ Δh′ v hole) (meaning-⊝ {σ} {s = y} {x})) (meaning-⊕ {t = app f x} {Δt = Δoutput-term}) ⟩ ⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) (y ⊝ x) ⟧ ∅ ∎}) where open ≡-Reasoning
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity import Parametric.Change.Specification as Specification import Parametric.Change.Type as ChangeType import Parametric.Change.Term as ChangeTerm import Parametric.Change.Value as ChangeValue import Parametric.Change.Evaluation as ChangeEvaluation import Parametric.Change.Derive as Derive import Parametric.Change.Implementation as Implementation module Parametric.Change.Correctness {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (diff-base : ChangeTerm.DiffStructure Const ΔBase) (apply-base : ChangeTerm.ApplyStructure Const ΔBase) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) (meaning-⊕-base : ChangeEvaluation.ApplyStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (meaning-⊝-base : ChangeEvaluation.DiffStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (validity-structure : Validity.Structure ⟦_⟧Base) (specification-structure : Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure) (derive-const : Derive.Structure Const ΔBase) (implementation-structure : Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure open ChangeType.Structure Base ΔBase open ChangeTerm.Structure Const ΔBase diff-base apply-base open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open ChangeEvaluation.Structure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧ meaning-⊕-base meaning-⊝-base open Derive.Structure Const ΔBase derive-const open Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure -- The denotational properties of the `derive` transformation. -- In particular, the main theorem about it producing the correct -- incremental behavior. open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality Structure : Set Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → (ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) → ⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′) module Structure (derive-const-correct : Structure) where deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′) deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′ deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′ -- That `derive t` implements ⟦ t ⟧Δ derive-correct : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′) derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′)) derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅ derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ = derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′ derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ = derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′ (derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′) derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ = deriveVar-correct x ρ dρ ρ′ dρ≈ρ′ derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′ = subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′) (derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′ (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′)) derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ = λ w dw w′ dw≈w′ → derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′) main-theorem : ∀ {σ τ} {f : Term ∅ (σ ⇒ τ)} {x : Term ∅ σ} {y : Term ∅ σ} → ⟦ app f y ⟧ ≡ ⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) (y ⊝ x) ⟧ main-theorem {σ} {τ} {f} {x} {y} = let h = ⟦ f ⟧ ∅ Δh = ⟦ f ⟧Δ ∅ ∅ Δh′ = ⟦ derive f ⟧ ∅ v = ⟦ x ⟧ ∅ u = ⟦ y ⟧ ∅ Δoutput-term = app (app (derive f) x) (y ⊝ x) in ext {A = ⟦ ∅ ⟧Context} (λ { ∅ → begin h u ≡⟨ cong h (sym (v+[u-v]=u {σ})) ⟩ h (v ⊞₍ σ ₎ (u ⊟₍ σ ₎ v)) ≡⟨ corollary-closed {σ} {τ} f v (u ⊟₍ σ ₎ v) ⟩ h v ⊞₍ τ ₎ call-change {σ} {τ} Δh v (u ⊟₍ σ ₎ v) ≡⟨ carry-over {τ} (call-change {σ} {τ} Δh v (u ⊟₍ σ ₎ v)) (derive-correct f ∅ ∅ ∅ ∅ v (u ⊟₍ σ ₎ v) (u ⟦⊝₍ σ ₎⟧ v) (u⊟v≈u⊝v {σ} {u} {v})) ⟩ h v ⟦⊕₍ τ ₎⟧ Δh′ v (u ⟦⊝₍ σ ₎⟧ v) ≡⟨ trans (cong (λ hole → h v ⟦⊕₍ τ ₎⟧ Δh′ v hole) (meaning-⊝ {σ} {s = y} {x})) (meaning-⊕ {t = app f x} {Δt = Δoutput-term}) ⟩ ⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) (y ⊝ x) ⟧ ∅ ∎}) where open ≡-Reasoning
Use subst instead of rewrite.
Use subst instead of rewrite. For unknown reasons, this seems to play better with type inference. Old-commit-hash: 55d911c0be0ad7f2ae6e77af9d5250bf4e4932b2
Agda
mit
inc-lc/ilc-agda
5c780ccbd5c829ffabcb3aa2ec8905291fcbc87a
Base/Change/Algebra.agda
Base/Change/Algebra.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change Structures. -- -- This module defines the notion of change structures, -- as well as change structures for groups, functions and lists. -- -- This module corresponds to Section 2 of the PLDI paper. ------------------------------------------------------------------------ module Base.Change.Algebra where open import Relation.Binary.PropositionalEquality open import Level -- Change Algebras -- =============== -- -- In the paper, change algebras are called "change structures" -- and they are described in Section 2.1. We follow the design of -- the Agda standard library and define change algebras as two -- records, so Definition 2.1 from the PLDI paper maps to the -- records IsChangeAlgebra and ChangeAlgebra. -- -- A value of type (IsChangeAlgebra Change update diff) proves that -- Change, update and diff together form a change algebra. -- -- A value of type (ChangeAlgebra Carrier) contains the necessary -- ingredients to create a change algebra for a carrier set. -- -- In the paper, Carrier is called V (Def. 2.1a), -- Change is called Δ (Def. 2.1b), -- update is written as infix ⊕ (Def. 2.1c), -- diff is written as infix ⊝ (Def. 2.1d), -- and update-diff is specified in Def. 2.1e. record IsChangeAlgebra {c} {d} {Carrier : Set c} (Change : Carrier → Set d) (update : (v : Carrier) (dv : Change v) → Carrier) (diff : (u v : Carrier) → Change v) (nil : (u : Carrier) → Change u) : Set (c ⊔ d) where field update-diff : ∀ u v → update v (diff u v) ≡ u -- This corresponds to Lemma 2.3 from the paper. update-nil : ∀ v → update v (nil v) ≡ v -- abbreviations before : ∀ {v} → Change v → Carrier before {v} _ = v after : ∀ {v} → Change v → Carrier after {v} dv = update v dv record ChangeAlgebra {c} ℓ (Carrier : Set c) : Set (c ⊔ suc ℓ) where field Change : Carrier → Set ℓ update : (v : Carrier) (dv : Change v) → Carrier diff : (u v : Carrier) → Change v -- This generalizes Def. 2.2. from the paper. nil : (u : Carrier) → Change u isChangeAlgebra : IsChangeAlgebra Change update diff nil infixl 6 update diff open IsChangeAlgebra isChangeAlgebra public -- The following open ... public statement lets us use infix ⊞ -- and ⊟ for update and diff. In the paper, we use infix ⊕ and -- ⊝. open ChangeAlgebra {{...}} public using ( update-diff ; update-nil ; nil ; before ; after ) renaming ( Change to Δ ; update to _⊞_ ; diff to _⊟_ ) -- Families of Change Algebras -- =========================== -- -- This is some Agda machinery to allow subscripting change -- algebra operations to avoid ambiguity. In the paper, -- we simply write (in the paragraph after Def. 2.1): -- -- We overload operators ∆, ⊝ and ⊕ to refer to the -- corresponding operations of different change structures; we -- will subscript these symbols when needed to prevent -- ambiguity. -- -- The following definitions implement this idea formally. record ChangeAlgebraFamily {a p} ℓ {A : Set a} (P : A → Set p): Set (suc ℓ ⊔ p ⊔ a) where constructor family field change-algebra : ∀ x → ChangeAlgebra ℓ (P x) module _ x where open ChangeAlgebra (change-algebra x) public module Family = ChangeAlgebraFamily {{...}} open Family public using ( ) renaming ( Change to Δ₍_₎ ; nil to nil₍_₎ ; update-diff to update-diff₍_₎ ; update-nil to update-nil₍_₎ ; change-algebra to change-algebra₍_₎ ; before to before₍_₎ ; after to after₍_₎ ) infixl 6 update′ diff′ update′ = Family.update syntax update′ x v dv = v ⊞₍ x ₎ dv diff′ = Family.diff syntax diff′ x u v = u ⊟₍ x ₎ v -- Derivatives -- =========== -- -- This corresponds to Def. 2.4 in the paper. Derivative : ∀ {a b c d} {A : Set a} {B : Set b} → {{CA : ChangeAlgebra c A}} → {{CB : ChangeAlgebra d B}} → (f : A → B) → (df : (a : A) (da : Δ a) → Δ (f a)) → Set (a ⊔ b ⊔ c) Derivative f df = ∀ a da → f a ⊞ df a da ≡ f (a ⊞ da) -- This is a variant of Derivative for change algebra families. Derivative₍_,_₎ : ∀ {a b p q c d} {A : Set a} {B : Set b} {P : A → Set p} {Q : B → Set q} → {{CP : ChangeAlgebraFamily c P}} → {{CQ : ChangeAlgebraFamily d Q}} → (x : A) → (y : B) → (f : P x → Q y) → (df : (px : P x) (dpx : Δ₍ x ₎ px) → Δ₍ y ₎ (f px)) → Set (p ⊔ q ⊔ c) Derivative₍ x , y ₎ f df = Derivative f df where CPx = change-algebra₍ x ₎ CQy = change-algebra₍ y ₎ -- Lemma 2.5 appears in Base.Change.Equivalence. -- Abelian Groups Induce Change Algebras -- ===================================== -- -- In the paper, as the first example for change structures after -- Def. 2.1, we mention that "each abelian group ... induces a -- change structure". This is the formalization of this result. -- -- The module GroupChanges below takes a proof that A forms an -- abelian group and provides a changeAlgebra for A. The proof of -- Def 2.1e is by equational reasoning using the group axioms, in -- the definition of changeAlgebra.isChangeAlgebra.update-diff. open import Algebra.Structures open import Data.Product open import Function module GroupChanges {a} (A : Set a) {_⊕_} {ε} {_⁻¹} {{isAbelianGroup : IsAbelianGroup {A = A} _≡_ _⊕_ ε _⁻¹}} where open IsAbelianGroup isAbelianGroup hiding ( refl ) renaming ( _-_ to _⊝_ ; ∙-cong to _⟨⊕⟩_ ) open ≡-Reasoning changeAlgebra : ChangeAlgebra a A changeAlgebra = record { Change = const A ; update = _⊕_ ; diff = _⊝_ ; nil = const ε ; isChangeAlgebra = record { update-diff = λ u v → begin v ⊕ (u ⊝ v) ≡⟨ comm _ _ ⟩ (u ⊝ v ) ⊕ v ≡⟨⟩ (u ⊕ (v ⁻¹)) ⊕ v ≡⟨ assoc _ _ _ ⟩ u ⊕ ((v ⁻¹) ⊕ v) ≡⟨ refl ⟨⊕⟩ proj₁ inverse v ⟩ u ⊕ ε ≡⟨ proj₂ identity u ⟩ u ∎ ; update-nil = proj₂ identity } } -- Function Changes -- ================ -- -- This is one of our most important results: Change structures -- can be lifted to function spaces. We formalize this as a module -- FunctionChanges that takes the change algebras for A and B as -- arguments, and provides a changeAlgebra for (A → B). The proofs -- are by equational reasoning using 2.1e for A and B. module FunctionChanges {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where -- This corresponds to Definition 2.6 in the paper. record FunctionChange (f : A → B) : Set (a ⊔ b ⊔ c ⊔ d) where constructor cons field -- Definition 2.6a apply : (a : A) (da : Δ a) → Δ (f a) -- Definition 2.6b. -- (for some reason, the version in the paper has the arguments of ≡ -- flipped. Since ≡ is symmetric, this doesn't matter). correct : (a : A) (da : Δ a) → f (a ⊞ da) ⊞ apply (a ⊞ da) (nil (a ⊞ da)) ≡ f a ⊞ apply a da open FunctionChange public open ≡-Reasoning open import Postulate.Extensionality funDiff : (g f : A → B) → FunctionChange f funDiff = λ g f → record { apply = λ a da → g (a ⊞ da) ⊟ f a -- the proof of correct is the first half of what we -- have to prove for Theorem 2.7. ; correct = λ a da → begin f (a ⊞ da) ⊞ (g ((a ⊞ da) ⊞ nil (a ⊞ da)) ⊟ f (a ⊞ da)) ≡⟨ cong (λ □ → f (a ⊞ da) ⊞ (g □ ⊟ f (a ⊞ da))) (update-nil (a ⊞ da)) ⟩ f (a ⊞ da) ⊞ (g (a ⊞ da) ⊟ f (a ⊞ da)) ≡⟨ update-diff (g (a ⊞ da)) (f (a ⊞ da)) ⟩ g (a ⊞ da) ≡⟨ sym (update-diff (g (a ⊞ da)) (f a)) ⟩ f a ⊞ (g (a ⊞ da) ⊟ f a) ∎ } funUpdate : ∀ (f : A → B) (df : FunctionChange f) → A → B funUpdate = λ f df a → f a ⊞ apply df a (nil a) funNil = λ f → funDiff f f mutual -- I have to write the type of funUpdateDiff without using changeAlgebra, -- so I just use the underlying implementations. funUpdateDiff : ∀ u v → funUpdate v (funDiff u v) ≡ u changeAlgebra : ChangeAlgebra (a ⊔ b ⊔ c ⊔ d) (A → B) changeAlgebra = record { Change = FunctionChange -- in the paper, update and diff below are in Def. 2.7 ; update = funUpdate ; diff = funDiff ; nil = funNil ; isChangeAlgebra = record -- the proof of update-diff is the second half of what -- we have to prove for Theorem 2.8. { update-diff = funUpdateDiff ; update-nil = λ v → funUpdateDiff v v } } -- XXX remove mutual recursion by inlining the algebra in here? funUpdateDiff = λ g f → ext (λ a → begin f a ⊞ (g (a ⊞ nil a) ⊟ f a) ≡⟨ cong (λ □ → f a ⊞ (g □ ⊟ f a)) (update-nil a) ⟩ f a ⊞ (g a ⊟ f a) ≡⟨ update-diff (g a) (f a) ⟩ g a ∎) -- This is Theorem 2.9 in the paper. incrementalization : ∀ (f : A → B) df a da → (f ⊞ df) (a ⊞ da) ≡ f a ⊞ apply df a da incrementalization f df a da = correct df a da -- This is Theorem 2.10 in the paper. nil-is-derivative : ∀ (f : A → B) → Derivative f (apply (nil f)) nil-is-derivative f a da = begin f a ⊞ apply (nil f) a da ≡⟨ sym (incrementalization f (nil f) a da) ⟩ (f ⊞ nil f) (a ⊞ da) ≡⟨ cong (λ □ → □ (a ⊞ da)) (update-nil f) ⟩ f (a ⊞ da) ∎ -- List (== Environment) Changes -- ============================= -- -- Here, we define a change structure on environments, given a -- change structure on the values in the environments. In the -- paper, we describe this in Definition 3.5. But note that this -- Agda formalization uses de Bruijn indices instead of names, so -- environments are just lists. Therefore, when we use Definition -- 3.5 in the paper, in this formalization, we use the list-like -- change structure defined here. open import Data.List open import Data.List.All data All′ {a p q} {A : Set a} {P : A → Set p} (Q : {x : A} → P x → Set q) : {xs : List A} (pxs : All P xs) → Set (p ⊔ q ⊔ a) where [] : All′ Q [] _∷_ : ∀ {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All′ Q pxs) → All′ Q (px ∷ pxs) module ListChanges {a} {c} {A : Set a} (P : A → Set) {{C : ChangeAlgebraFamily c P}} where update-all : ∀ {xs} → (pxs : All P xs) → All′ (Δ₍ _ ₎) pxs → All P xs update-all {[]} [] [] = [] update-all {x ∷ xs} (px ∷ pxs) (dpx ∷ dpxs) = (px ⊞₍ x ₎ dpx) ∷ update-all pxs dpxs diff-all : ∀ {xs} → (pxs′ pxs : All P xs) → All′ (Δ₍ _ ₎) pxs diff-all [] [] = [] diff-all (px′ ∷ pxs′) (px ∷ pxs) = (px′ ⊟₍ _ ₎ px) ∷ diff-all pxs′ pxs update-diff-all : ∀ {xs} → (pxs′ pxs : All P xs) → update-all pxs (diff-all pxs′ pxs) ≡ pxs′ update-diff-all [] [] = refl update-diff-all (px′ ∷ pxs′) (px ∷ pxs) = cong₂ _∷_ (update-diff₍ _ ₎ px′ px) (update-diff-all pxs′ pxs) changeAlgebra : ChangeAlgebraFamily (c ⊔ a) (All P) changeAlgebra = record { change-algebra = λ xs → record { Change = All′ Δ₍ _ ₎ ; update = update-all ; diff = diff-all ; nil = λ xs → diff-all xs xs ; isChangeAlgebra = record { update-diff = update-diff-all ; update-nil = λ xs₁ → update-diff-all xs₁ xs₁ } } }
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change Structures. -- -- This module defines the notion of change structures, -- as well as change structures for groups, functions and lists. -- -- This module corresponds to Section 2 of the PLDI paper. ------------------------------------------------------------------------ module Base.Change.Algebra where open import Relation.Binary.PropositionalEquality open import Level -- Change Algebras -- =============== -- -- In the paper, change algebras are called "change structures" -- and they are described in Section 2.1. We follow the design of -- the Agda standard library and define change algebras as two -- records, so Definition 2.1 from the PLDI paper maps to the -- records IsChangeAlgebra and ChangeAlgebra. -- -- A value of type (IsChangeAlgebra Change update diff) proves that -- Change, update and diff together form a change algebra. -- -- A value of type (ChangeAlgebra Carrier) contains the necessary -- ingredients to create a change algebra for a carrier set. -- -- In the paper, Carrier is called V (Def. 2.1a), -- Change is called Δ (Def. 2.1b), -- update is written as infix ⊕ (Def. 2.1c), -- diff is written as infix ⊝ (Def. 2.1d), -- and update-diff is specified in Def. 2.1e. record IsChangeAlgebra {c} {d} {Carrier : Set c} (Change : Carrier → Set d) (update : (v : Carrier) (dv : Change v) → Carrier) (diff : (u v : Carrier) → Change v) (nil : (u : Carrier) → Change u) : Set (c ⊔ d) where field update-diff : ∀ u v → update v (diff u v) ≡ u -- This corresponds to Lemma 2.3 from the paper. update-nil : ∀ v → update v (nil v) ≡ v -- abbreviations before : ∀ {v} → Change v → Carrier before {v} _ = v after : ∀ {v} → Change v → Carrier after {v} dv = update v dv record ChangeAlgebra {c} ℓ (Carrier : Set c) : Set (c ⊔ suc ℓ) where field Change : Carrier → Set ℓ update : (v : Carrier) (dv : Change v) → Carrier diff : (u v : Carrier) → Change v -- This generalizes Def. 2.2. from the paper. nil : (u : Carrier) → Change u isChangeAlgebra : IsChangeAlgebra Change update diff nil infixl 6 update diff open IsChangeAlgebra isChangeAlgebra public -- The following open ... public statement lets us use infix ⊞ -- and ⊟ for update and diff. In the paper, we use infix ⊕ and -- ⊝. open ChangeAlgebra {{...}} public using ( update-diff ; update-nil ; nil ; before ; after ) renaming ( Change to Δ ; update to _⊞_ ; diff to _⊟_ ) -- Families of Change Algebras -- =========================== -- -- This is some Agda machinery to allow subscripting change -- algebra operations to avoid ambiguity. In the paper, -- we simply write (in the paragraph after Def. 2.1): -- -- We overload operators ∆, ⊝ and ⊕ to refer to the -- corresponding operations of different change structures; we -- will subscript these symbols when needed to prevent -- ambiguity. -- -- The following definitions implement this idea formally. record ChangeAlgebraFamily {a p} ℓ {A : Set a} (P : A → Set p): Set (suc ℓ ⊔ p ⊔ a) where constructor family field change-algebra : ∀ x → ChangeAlgebra ℓ (P x) {- instance change-algebra-extractor : ∀ {x} → ChangeAlgebra ℓ (P x) change-algebra-extractor {x} = change-algebra x -} module _ x where open ChangeAlgebra (change-algebra x) public module Family = ChangeAlgebraFamily {{...}} open Family public using ( ) renaming ( Change to Δ₍_₎ ; nil to nil₍_₎ ; update-diff to update-diff₍_₎ ; update-nil to update-nil₍_₎ ; change-algebra to change-algebra₍_₎ ; before to before₍_₎ ; after to after₍_₎ ) -- XXX not clear this is ever used instance change-algebra-family-inst = change-algebra₍_₎ infixl 6 update′ diff′ update′ = Family.update syntax update′ x v dv = v ⊞₍ x ₎ dv diff′ = Family.diff syntax diff′ x u v = u ⊟₍ x ₎ v -- Derivatives -- =========== -- -- This corresponds to Def. 2.4 in the paper. Derivative : ∀ {a b c d} {A : Set a} {B : Set b} → {{CA : ChangeAlgebra c A}} → {{CB : ChangeAlgebra d B}} → (f : A → B) → (df : (a : A) (da : Δ a) → Δ (f a)) → Set (a ⊔ b ⊔ c) Derivative f df = ∀ a da → f a ⊞ df a da ≡ f (a ⊞ da) -- This is a variant of Derivative for change algebra families. Derivative₍_,_₎ : ∀ {a b p q c d} {A : Set a} {B : Set b} {P : A → Set p} {Q : B → Set q} → {{CP : ChangeAlgebraFamily c P}} → {{CQ : ChangeAlgebraFamily d Q}} → (x : A) → (y : B) → (f : P x → Q y) → (df : (px : P x) (dpx : Δ₍ x ₎ px) → Δ₍ y ₎ (f px)) → Set (p ⊔ q ⊔ c) Derivative₍ x , y ₎ f df = Derivative {{change-algebra₍ _ ₎}} {{change-algebra₍ _ ₎}} f df where CPx = change-algebra₍ x ₎ CQy = change-algebra₍ y ₎ -- Lemma 2.5 appears in Base.Change.Equivalence. -- Abelian Groups Induce Change Algebras -- ===================================== -- -- In the paper, as the first example for change structures after -- Def. 2.1, we mention that "each abelian group ... induces a -- change structure". This is the formalization of this result. -- -- The module GroupChanges below takes a proof that A forms an -- abelian group and provides a changeAlgebra for A. The proof of -- Def 2.1e is by equational reasoning using the group axioms, in -- the definition of changeAlgebra.isChangeAlgebra.update-diff. open import Algebra.Structures open import Data.Product open import Function module GroupChanges {a} (A : Set a) {_⊕_} {ε} {_⁻¹} {{isAbelianGroup : IsAbelianGroup {A = A} _≡_ _⊕_ ε _⁻¹}} where open IsAbelianGroup isAbelianGroup hiding ( refl ) renaming ( _-_ to _⊝_ ; ∙-cong to _⟨⊕⟩_ ) open ≡-Reasoning changeAlgebra : ChangeAlgebra a A changeAlgebra = record { Change = const A ; update = _⊕_ ; diff = _⊝_ ; nil = const ε ; isChangeAlgebra = record { update-diff = λ u v → begin v ⊕ (u ⊝ v) ≡⟨ comm _ _ ⟩ (u ⊝ v ) ⊕ v ≡⟨⟩ (u ⊕ (v ⁻¹)) ⊕ v ≡⟨ assoc _ _ _ ⟩ u ⊕ ((v ⁻¹) ⊕ v) ≡⟨ refl ⟨⊕⟩ proj₁ inverse v ⟩ u ⊕ ε ≡⟨ proj₂ identity u ⟩ u ∎ ; update-nil = proj₂ identity } } -- Function Changes -- ================ -- -- This is one of our most important results: Change structures -- can be lifted to function spaces. We formalize this as a module -- FunctionChanges that takes the change algebras for A and B as -- arguments, and provides a changeAlgebra for (A → B). The proofs -- are by equational reasoning using 2.1e for A and B. module FunctionChanges {a} {b} {c} {d} (A : Set a) (B : Set b) {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where -- This corresponds to Definition 2.6 in the paper. record FunctionChange (f : A → B) : Set (a ⊔ b ⊔ c ⊔ d) where constructor cons field -- Definition 2.6a apply : (a : A) (da : Δ a) → Δ (f a) -- Definition 2.6b. -- (for some reason, the version in the paper has the arguments of ≡ -- flipped. Since ≡ is symmetric, this doesn't matter). correct : (a : A) (da : Δ a) → f (a ⊞ da) ⊞ apply (a ⊞ da) (nil (a ⊞ da)) ≡ f a ⊞ apply a da open FunctionChange public open ≡-Reasoning open import Postulate.Extensionality funDiff : (g f : A → B) → FunctionChange f funDiff = λ g f → record { apply = λ a da → g (a ⊞ da) ⊟ f a -- the proof of correct is the first half of what we -- have to prove for Theorem 2.7. ; correct = λ a da → begin f (a ⊞ da) ⊞ (g ((a ⊞ da) ⊞ nil (a ⊞ da)) ⊟ f (a ⊞ da)) ≡⟨ cong (λ □ → f (a ⊞ da) ⊞ (g □ ⊟ f (a ⊞ da))) (update-nil (a ⊞ da)) ⟩ f (a ⊞ da) ⊞ (g (a ⊞ da) ⊟ f (a ⊞ da)) ≡⟨ update-diff (g (a ⊞ da)) (f (a ⊞ da)) ⟩ g (a ⊞ da) ≡⟨ sym (update-diff (g (a ⊞ da)) (f a)) ⟩ f a ⊞ (g (a ⊞ da) ⊟ f a) ∎ } funUpdate : ∀ (f : A → B) (df : FunctionChange f) → A → B funUpdate = λ f df a → f a ⊞ apply df a (nil a) funNil = λ f → funDiff f f mutual -- I have to write the type of funUpdateDiff without using changeAlgebra, -- so I just use the underlying implementations. funUpdateDiff : ∀ u v → funUpdate v (funDiff u v) ≡ u instance changeAlgebra : ChangeAlgebra (a ⊔ b ⊔ c ⊔ d) (A → B) changeAlgebra = record { Change = FunctionChange -- in the paper, update and diff below are in Def. 2.7 ; update = funUpdate ; diff = funDiff ; nil = funNil ; isChangeAlgebra = record -- the proof of update-diff is the second half of what -- we have to prove for Theorem 2.8. { update-diff = funUpdateDiff ; update-nil = λ v → funUpdateDiff v v } } -- XXX remove mutual recursion by inlining the algebra in here? funUpdateDiff = λ g f → ext (λ a → begin f a ⊞ (g (a ⊞ nil a) ⊟ f a) ≡⟨ cong (λ □ → f a ⊞ (g □ ⊟ f a)) (update-nil a) ⟩ f a ⊞ (g a ⊟ f a) ≡⟨ update-diff (g a) (f a) ⟩ g a ∎) -- This is Theorem 2.9 in the paper. incrementalization : ∀ (f : A → B) df a da → (f ⊞ df) (a ⊞ da) ≡ f a ⊞ apply df a da incrementalization f df a da = correct df a da -- This is Theorem 2.10 in the paper. nil-is-derivative : ∀ (f : A → B) → Derivative f (apply (nil f)) nil-is-derivative f a da = begin f a ⊞ apply (nil f) a da ≡⟨ sym (incrementalization f (nil f) a da) ⟩ (f ⊞ nil f) (a ⊞ da) ≡⟨ cong (λ □ → □ (a ⊞ da)) (update-nil f) ⟩ f (a ⊞ da) ∎ -- List (== Environment) Changes -- ============================= -- -- Here, we define a change structure on environments, given a -- change structure on the values in the environments. In the -- paper, we describe this in Definition 3.5. But note that this -- Agda formalization uses de Bruijn indices instead of names, so -- environments are just lists. Therefore, when we use Definition -- 3.5 in the paper, in this formalization, we use the list-like -- change structure defined here. open import Data.List open import Data.List.All data All′ {a p q} {A : Set a} {P : A → Set p} (Q : {x : A} → P x → Set q) : {xs : List A} (pxs : All P xs) → Set (p ⊔ q ⊔ a) where [] : All′ Q [] _∷_ : ∀ {x xs} {px : P x} {pxs : All P xs} (qx : Q px) (qxs : All′ Q pxs) → All′ Q (px ∷ pxs) module ListChanges {a} {c} {A : Set a} (P : A → Set) {{C : ChangeAlgebraFamily c P}} where update-all : ∀ {xs} → (pxs : All P xs) → All′ (Δ₍ _ ₎) pxs → All P xs update-all {[]} [] [] = [] update-all {x ∷ xs} (px ∷ pxs) (dpx ∷ dpxs) = (px ⊞₍ x ₎ dpx) ∷ update-all pxs dpxs diff-all : ∀ {xs} → (pxs′ pxs : All P xs) → All′ (Δ₍ _ ₎) pxs diff-all [] [] = [] diff-all (px′ ∷ pxs′) (px ∷ pxs) = (px′ ⊟₍ _ ₎ px) ∷ diff-all pxs′ pxs update-diff-all : ∀ {xs} → (pxs′ pxs : All P xs) → update-all pxs (diff-all pxs′ pxs) ≡ pxs′ update-diff-all [] [] = refl update-diff-all (px′ ∷ pxs′) (px ∷ pxs) = cong₂ _∷_ (update-diff₍ _ ₎ px′ px) (update-diff-all pxs′ pxs) instance changeAlgebra : ChangeAlgebraFamily (c ⊔ a) (All P) changeAlgebra = record { change-algebra = λ xs → record { Change = All′ Δ₍ _ ₎ ; update = update-all ; diff = diff-all ; nil = λ xs → diff-all xs xs ; isChangeAlgebra = record { update-diff = update-diff-all ; update-nil = λ xs₁ → update-diff-all xs₁ xs₁ } } }
Convert Base.Change.Algebra to Agda 2.4.2
Convert Base.Change.Algebra to Agda 2.4.2
Agda
mit
inc-lc/ilc-agda
1fd25d4aff13f116be831b1d538e1df362ba14e5
experimental/Embed62.agda
experimental/Embed62.agda
module Embed62 where open import TaggedDeltaTypes open import ExplicitNil using (ext³) open import Relation.Binary.PropositionalEquality open import Data.Product using (_×_ ; _,_ ; proj₁ ; proj₂) open import Data.Unit using (⊤ ; tt) open import Data.Nat Δ-Type : Type → Type Δ-Type nats = (nats ⇒ nats ⇒ nats) ⇒ nats -- Church pairs Δ-Type bags = bags Δ-Type (σ ⇒ τ) = σ ⇒ Δ-Type σ ⇒ Δ-Type τ Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = Δ-Type τ • τ • Δ-Context Γ Γ≼ΔΓ : ∀ {Γ} → Γ ≼ Δ-Context Γ Γ≼ΔΓ {∅} = ∅≼∅ Γ≼ΔΓ {τ • Γ} = drop Δ-Type τ • keep τ • Γ≼ΔΓ weak : ∀ {τ Γ} → Term Γ τ → Term (Δ-Context Γ) τ weak = weaken Γ≼ΔΓ deriveVar : ∀ {τ Γ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) absurd! : ∀ {A B : Set} → A → A → B → B absurd! _ _ b = b fst : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ τ) fst = abs (abs (var (that this))) snd : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ τ) snd = abs (abs (var this)) difff : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ) apply : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ apply {nats} dt t = app dt snd apply {bags} dt t = union t dt apply {σ ⇒ τ} dt t = abs (apply (app (app (weaken (drop _ • Γ≼Γ) dt) (var this)) (difff (var this) (var this))) (app (weaken (drop _ • Γ≼Γ) t) (var this))) difff {nats} s t = abs (app (app (var this) (weaken (drop _ • Γ≼Γ) s)) (weaken (drop _ • Γ≼Γ) t)) difff {bags} s t = diff s t difff {σ ⇒ τ} s t = abs (abs (difff (app (weaken (drop _ • drop _ • Γ≼Γ) s) (apply (var this) (var (that this)))) (app (weaken (drop _ • drop _ • Γ≼Γ) t) (var (that this))))) embed : ∀ {τ Γ} → ΔTerm Γ τ → Term (Δ-Context Γ) (Δ-Type τ) embed (Δnat old new) = abs (app (app (var this) (nat old)) (nat new)) embed (Δbag db) = bag db embed (Δvar x) = var (deriveVar x) embed (Δabs dt) = abs (abs (embed dt)) embed (Δapp ds t dt) = app (app (embed ds) (weak t)) (embed dt) embed (Δadd ds dt) = abs (app (app (var this) (add (app (weaken (drop _ • Γ≼Γ) (embed ds)) fst) (app (weaken (drop _ • Γ≼Γ) (embed dt)) fst))) (add (app (weaken (drop _ • Γ≼Γ) (embed ds)) snd) (app (weaken (drop _ • Γ≼Γ) (embed dt)) snd))) embed (Δmap₀ s ds t dt) = diff (map (apply (embed ds) (weak s)) (apply (embed dt) (weak t))) (weak (map s t)) embed (Δmap₁ s dt) = map (weak s) (embed dt) embed (Δunion ds dt) = union (embed ds) (embed dt) embed (Δdiff ds dt) = diff (embed ds) (embed dt) ⟦fst⟧ : ∀ {A : Set} → A → A → A ⟦fst⟧ a b = a ⟦snd⟧ : ∀ {A : Set} → A → A → A ⟦snd⟧ a b = b _≈_ : ∀ {τ} → ΔVal τ → ⟦ Δ-Type τ ⟧ → Set _≈_ {nats} (old , new) v = old ≡ v ⟦fst⟧ × new ≡ v ⟦snd⟧ _≈_ {bags} u v = u ≡ v _≈_ {σ ⇒ τ} u v = (w : ⟦ σ ⟧) (dw : ΔVal σ) (dw′ : ⟦ Δ-Type σ ⟧) (R[w,dw] : valid w dw) (eq : dw ≈ dw′) → u w dw R[w,dw] ≈ v w dw′ infix 4 _≈_ compatible : ∀ {Γ} → ΔEnv Γ → ⟦ Δ-Context Γ ⟧ → Set compatible {∅} ∅ ∅ = EmptySet compatible {τ • Γ} (cons v dv _ ρ) (dv′ • v′ • ρ′) = triple (v ≡ v′) (dv ≈ dv′) (compatible ρ ρ′) deriveVar-correct : ∀ {τ Γ} {x : Var Γ τ} {ρ : ΔEnv Γ} {ρ′ : ⟦ Δ-Context Γ ⟧} {C : compatible ρ ρ′} → ⟦ x ⟧ΔVar ρ ≈ ⟦ deriveVar x ⟧ ρ′ deriveVar-correct {x = this} -- pattern-matching on ρ, ρ′ NOT optional {cons _ _ _ _} {_ • _ • _} {cons _ dv≈dv′ _ _} = dv≈dv′ deriveVar-correct {x = that y} {cons _ _ _ ρ} {_ • _ • ρ′} {cons _ _ C _} = deriveVar-correct {x = y} {ρ} {ρ′} {C} weak-id : ∀ {Γ} {ρ : ⟦ Γ ⟧} → ⟦ Γ≼Γ {Γ} ⟧ ρ ≡ ρ weak-id {∅} {∅} = refl weak-id {τ • Γ} {v • ρ} = cong₂ _•_ {x = v} refl (weak-id {Γ} {ρ}) weak-eq : ∀ {Γ} {ρ : ΔEnv Γ} {ρ′ : ⟦ Δ-Context Γ ⟧} (C : compatible ρ ρ′) → ignore ρ ≡ ⟦ Γ≼ΔΓ ⟧ ρ′ weak-eq {∅} {∅} {∅} _ = refl weak-eq {τ • Γ} {cons v dv _ ρ} {dv′ • v′ • ρ′} (cons v≡v′ _ C _) = cong₂ _•_ v≡v′ (weak-eq C) weak-eq-term : ∀ {τ Γ} (t : Term Γ τ) {ρ : ΔEnv Γ} {ρ′ : ⟦ Δ-Context Γ ⟧} (C : compatible ρ ρ′) → ⟦ t ⟧ (ignore ρ) ≡ ⟦ weaken Γ≼ΔΓ t ⟧ ρ′ weak-eq-term t {ρ} {ρ′} C = trans (cong ⟦ t ⟧ (weak-eq C)) (sym (weaken-sound t ρ′)) weaken-once : ∀ {τ Γ σ} {t : Term Γ τ} {v : ⟦ σ ⟧} {ρ : ⟦ Γ ⟧} → ⟦ t ⟧ ρ ≡ ⟦ weaken (drop σ • Γ≼Γ) t ⟧ (v • ρ) weaken-once {t = t} {v} {ρ} = trans (cong ⟦ t ⟧ (sym weak-id)) (sym (weaken-sound t (v • ρ))) embed-correct : ∀ {τ Γ} {dt : ΔTerm Γ τ} {ρ : ΔEnv Γ} {V : dt is-valid-for ρ} {ρ′ : ⟦ Δ-Context Γ ⟧} {C : compatible ρ ρ′} → ⟦ dt ⟧Δ ρ V ≈ ⟦ embed dt ⟧ ρ′ embed-correct {dt = Δnat old new} = refl , refl embed-correct {dt = Δbag db} = refl embed-correct {dt = Δvar x} {ρ} {ρ′ = ρ′} {C} = deriveVar-correct {x = x} {ρ} {ρ′} {C} embed-correct {dt = Δadd ds dt} {ρ} {V} {ρ′} {C} = let s00 , s01 = ⟦ ds ⟧Δ ρ (car V) t00 , t01 = ⟦ dt ⟧Δ ρ (cadr V) s10 = ⟦ embed ds ⟧ ρ′ ⟦fst⟧ s11 = ⟦ embed ds ⟧ ρ′ ⟦snd⟧ t10 = ⟦ embed dt ⟧ ρ′ ⟦fst⟧ t11 = ⟦ embed dt ⟧ ρ′ ⟦snd⟧ rec-s = embed-correct {dt = ds} {ρ} {car V} {ρ′} {C} rec-t = embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C} in (begin s00 + t00 ≡⟨ cong₂ _+_ (proj₁ rec-s) (proj₁ rec-t) ⟩ s10 + t10 ≡⟨ cong₂ _+_ (cong (λ x → ⟦ embed ds ⟧ x ⟦fst⟧) (sym weak-id)) (cong (λ x → ⟦ embed dt ⟧ x ⟦fst⟧) (sym weak-id)) ⟩ ⟦ embed ds ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦fst⟧ {ℕ} • ρ′)) ⟦fst⟧ + ⟦ embed dt ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦fst⟧ {ℕ} • ρ′)) ⟦fst⟧ ≡⟨ cong₂ _+_ (cong (λ f → f ⟦fst⟧) (sym (weaken-sound (embed ds) (⟦fst⟧ • ρ′)))) (cong (λ f → f ⟦fst⟧) (sym (weaken-sound (embed dt) (⟦fst⟧ • ρ′)))) ⟩ ⟦ weaken (drop _ • Γ≼Γ) (embed ds) ⟧ (⟦fst⟧ • ρ′) ⟦fst⟧ + ⟦ weaken (drop _ • Γ≼Γ) (embed dt) ⟧ (⟦fst⟧ • ρ′) ⟦fst⟧ ∎) , (begin s01 + t01 ≡⟨ cong₂ _+_ (proj₂ rec-s) (proj₂ rec-t) ⟩ s11 + t11 ≡⟨ cong₂ _+_ (cong (λ x → ⟦ embed ds ⟧ x ⟦snd⟧) (sym weak-id)) (cong (λ x → ⟦ embed dt ⟧ x ⟦snd⟧) (sym weak-id)) ⟩ ⟦ embed ds ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦snd⟧ {ℕ} • ρ′)) ⟦snd⟧ + ⟦ embed dt ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦snd⟧ {ℕ} • ρ′)) ⟦snd⟧ ≡⟨ cong₂ _+_ (cong (λ f → f ⟦snd⟧) (sym (weaken-sound (embed ds) (⟦snd⟧ • ρ′)))) (cong (λ f → f ⟦snd⟧) (sym (weaken-sound (embed dt) (⟦snd⟧ • ρ′)))) ⟩ ⟦ weaken (drop _ • Γ≼Γ) (embed ds) ⟧ (⟦snd⟧ • ρ′) ⟦snd⟧ + ⟦ weaken (drop _ • Γ≼Γ) (embed dt) ⟧ (⟦snd⟧ • ρ′) ⟦snd⟧ ∎) where open ≡-Reasoning embed-correct {dt = Δunion ds dt} {ρ} {V} {ρ′} {C} = cong₂ _++_ (embed-correct {dt = ds} {ρ} {car V} {ρ′} {C}) (embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C}) embed-correct {dt = Δdiff ds dt} {ρ} {V} {ρ′} {C} = cong₂ _\\_ (embed-correct {dt = ds} {ρ} {car V} {ρ′} {C}) (embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C}) embed-correct {dt = Δmap₁ s dt} {ρ} {V} {ρ′} {C} = cong₂ mapBag (weak-eq-term s C) (embed-correct {dt = dt} {ρ} {car V} {ρ′} {C}) embed-correct {dt = Δmap₀ s ds t dt} {ρ} {V} {ρ′} {C} = cong₂ _\\_ (cong₂ mapBag (extensionality (λ v → begin proj₂ (⟦ ds ⟧Δ ρ (car V) v (v , v) refl) ≡⟨ proj₂ (embed-correct {dt = ds} {ρ} {car V} {ρ′} {C} v (v , v) (λ f → f v v) refl (refl , refl)) ⟩ ⟦ embed ds ⟧ ρ′ v (λ f → f v v) ⟦snd⟧ ≡⟨ cong (λ hole → hole v (λ f → f v v) ⟦snd⟧) (weaken-once {t = embed ds} {v} {ρ′}) ⟩ ⟦ weaken (drop _ • Γ≼Γ) (embed ds) ⟧ (v • ρ′) v (λ f → f v v) ⟦snd⟧ ∎)) (cong₂ _++_ (weak-eq-term t C) (embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C}))) (cong₂ mapBag (weak-eq-term s C) (weak-eq-term t C)) where open ≡-Reasoning embed-correct {dt = Δapp ds t dt} {ρ} {cons vds vdt R[t,dt] _} {ρ′} {C} rewrite sym (weak-eq-term t C) = embed-correct {dt = ds} {ρ} {vds} {ρ′} {C} (⟦ t ⟧Term (ignore ρ)) (⟦ dt ⟧Δ ρ vdt) (⟦ embed dt ⟧Term ρ′) R[t,dt] (embed-correct {dt = dt} {ρ} {vdt} {ρ′} {C}) embed-correct {dt = Δabs dt} {ρ} {V} {ρ′} {C} = λ w dw dw′ R[w,dw] dw≈dw′ → embed-correct {dt = dt} {cons w dw R[w,dw] ρ} {V w dw R[w,dw]} {dw′ • w • ρ′} {cons refl dw≈dw′ C tt}
module Embed62 where open import TaggedDeltaTypes open import ExplicitNil using (ext³) open import Relation.Binary.PropositionalEquality open import Data.Product using (_×_ ; _,_ ; proj₁ ; proj₂) open import Data.Unit using (⊤ ; tt) open import Data.Nat natPairVisitor : Type natPairVisitor = nats ⇒ nats ⇒ nats Δ-Type : Type → Type Δ-Type nats = natPairVisitor ⇒ nats -- Church pairs Δ-Type bags = bags Δ-Type (σ ⇒ τ) = σ ⇒ Δ-Type σ ⇒ Δ-Type τ Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = Δ-Type τ • τ • Δ-Context Γ Γ≼ΔΓ : ∀ {Γ} → Γ ≼ Δ-Context Γ Γ≼ΔΓ {∅} = ∅≼∅ Γ≼ΔΓ {τ • Γ} = drop Δ-Type τ • keep τ • Γ≼ΔΓ weak : ∀ {τ Γ} → Term Γ τ → Term (Δ-Context Γ) τ weak = weaken Γ≼ΔΓ deriveVar : ∀ {τ Γ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) absurd! : ∀ {A B : Set} → A → A → B → B absurd! _ _ b = b fst : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ τ) fst = abs (abs (var (that this))) snd : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ τ) snd = abs (abs (var this)) oldFrom newFrom : ∀ {Γ} → Term Γ (Δ-Type nats) → Term Γ nats oldFrom d = app d fst newFrom d = app d snd difff : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ) apply : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ apply {nats} dt t = app dt snd apply {bags} dt t = union t dt apply {σ ⇒ τ} dt t = abs (apply (app (app (weaken (drop _ • Γ≼Γ) dt) (var this)) (difff (var this) (var this))) (app (weaken (drop _ • Γ≼Γ) t) (var this))) difff {nats} s t = abs (app (app (var this) (weaken (drop _ • Γ≼Γ) s)) (weaken (drop _ • Γ≼Γ) t)) difff {bags} s t = diff s t difff {σ ⇒ τ} s t = abs (abs (difff (app (weaken (drop _ • drop _ • Γ≼Γ) s) (apply (var this) (var (that this)))) (app (weaken (drop _ • drop _ • Γ≼Γ) t) (var (that this))))) natPair : ∀ {Γ} → (old new : Term (natPairVisitor • Γ) nats) → Term Γ (Δ-Type nats) natPair old new = abs (app (app (var this) old) new) embed : ∀ {τ Γ} → ΔTerm Γ τ → Term (Δ-Context Γ) (Δ-Type τ) embed (Δnat old new) = natPair (nat old) (nat new) embed (Δbag db) = bag db embed (Δvar x) = var (deriveVar x) embed (Δabs dt) = abs (abs (embed dt)) embed (Δapp ds t dt) = app (app (embed ds) (weak t)) (embed dt) embed (Δadd ds dt) = natPair (add (oldFrom (embedWeaken ds)) (oldFrom (embedWeaken dt))) (add (newFrom (embedWeaken ds)) (newFrom (embedWeaken dt))) where embedWeaken : ∀ {Γ τ} → (d : ΔTerm Γ nats) → Term (τ • Δ-Context Γ) (natPairVisitor ⇒ nats) embedWeaken d = (weaken (drop _ • Γ≼Γ) (embed d)) embed (Δmap₀ s ds t dt) = diff (map (apply (embed ds) (weak s)) (apply (embed dt) (weak t))) (weak (map s t)) embed (Δmap₁ s dt) = map (weak s) (embed dt) embed (Δunion ds dt) = union (embed ds) (embed dt) embed (Δdiff ds dt) = diff (embed ds) (embed dt) ⟦fst⟧ : ∀ {A : Set} → A → A → A ⟦fst⟧ a b = a ⟦snd⟧ : ∀ {A : Set} → A → A → A ⟦snd⟧ a b = b _≈_ : ∀ {τ} → ΔVal τ → ⟦ Δ-Type τ ⟧ → Set _≈_ {nats} (old , new) v = old ≡ v ⟦fst⟧ × new ≡ v ⟦snd⟧ _≈_ {bags} u v = u ≡ v _≈_ {σ ⇒ τ} u v = (w : ⟦ σ ⟧) (dw : ΔVal σ) (dw′ : ⟦ Δ-Type σ ⟧) (R[w,dw] : valid w dw) (eq : dw ≈ dw′) → u w dw R[w,dw] ≈ v w dw′ infix 4 _≈_ compatible : ∀ {Γ} → ΔEnv Γ → ⟦ Δ-Context Γ ⟧ → Set compatible {∅} ∅ ∅ = EmptySet compatible {τ • Γ} (cons v dv _ ρ) (dv′ • v′ • ρ′) = triple (v ≡ v′) (dv ≈ dv′) (compatible ρ ρ′) deriveVar-correct : ∀ {τ Γ} {x : Var Γ τ} {ρ : ΔEnv Γ} {ρ′ : ⟦ Δ-Context Γ ⟧} {C : compatible ρ ρ′} → ⟦ x ⟧ΔVar ρ ≈ ⟦ deriveVar x ⟧ ρ′ deriveVar-correct {x = this} -- pattern-matching on ρ, ρ′ NOT optional {cons _ _ _ _} {_ • _ • _} {cons _ dv≈dv′ _ _} = dv≈dv′ deriveVar-correct {x = that y} {cons _ _ _ ρ} {_ • _ • ρ′} {cons _ _ C _} = deriveVar-correct {x = y} {ρ} {ρ′} {C} weak-id : ∀ {Γ} {ρ : ⟦ Γ ⟧} → ⟦ Γ≼Γ {Γ} ⟧ ρ ≡ ρ weak-id {∅} {∅} = refl weak-id {τ • Γ} {v • ρ} = cong₂ _•_ {x = v} refl (weak-id {Γ} {ρ}) weak-eq : ∀ {Γ} {ρ : ΔEnv Γ} {ρ′ : ⟦ Δ-Context Γ ⟧} (C : compatible ρ ρ′) → ignore ρ ≡ ⟦ Γ≼ΔΓ ⟧ ρ′ weak-eq {∅} {∅} {∅} _ = refl weak-eq {τ • Γ} {cons v dv _ ρ} {dv′ • v′ • ρ′} (cons v≡v′ _ C _) = cong₂ _•_ v≡v′ (weak-eq C) weak-eq-term : ∀ {τ Γ} (t : Term Γ τ) {ρ : ΔEnv Γ} {ρ′ : ⟦ Δ-Context Γ ⟧} (C : compatible ρ ρ′) → ⟦ t ⟧ (ignore ρ) ≡ ⟦ weaken Γ≼ΔΓ t ⟧ ρ′ weak-eq-term t {ρ} {ρ′} C = trans (cong ⟦ t ⟧ (weak-eq C)) (sym (weaken-sound t ρ′)) weaken-once : ∀ {τ Γ σ} {t : Term Γ τ} {v : ⟦ σ ⟧} {ρ : ⟦ Γ ⟧} → ⟦ t ⟧ ρ ≡ ⟦ weaken (drop σ • Γ≼Γ) t ⟧ (v • ρ) weaken-once {t = t} {v} {ρ} = trans (cong ⟦ t ⟧ (sym weak-id)) (sym (weaken-sound t (v • ρ))) embed-correct : ∀ {τ Γ} {dt : ΔTerm Γ τ} {ρ : ΔEnv Γ} {V : dt is-valid-for ρ} {ρ′ : ⟦ Δ-Context Γ ⟧} {C : compatible ρ ρ′} → ⟦ dt ⟧Δ ρ V ≈ ⟦ embed dt ⟧ ρ′ embed-correct {dt = Δnat old new} = refl , refl embed-correct {dt = Δbag db} = refl embed-correct {dt = Δvar x} {ρ} {ρ′ = ρ′} {C} = deriveVar-correct {x = x} {ρ} {ρ′} {C} embed-correct {dt = Δadd ds dt} {ρ} {V} {ρ′} {C} = let s00 , s01 = ⟦ ds ⟧Δ ρ (car V) t00 , t01 = ⟦ dt ⟧Δ ρ (cadr V) s10 = ⟦ embed ds ⟧ ρ′ ⟦fst⟧ s11 = ⟦ embed ds ⟧ ρ′ ⟦snd⟧ t10 = ⟦ embed dt ⟧ ρ′ ⟦fst⟧ t11 = ⟦ embed dt ⟧ ρ′ ⟦snd⟧ rec-s = embed-correct {dt = ds} {ρ} {car V} {ρ′} {C} rec-t = embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C} in (begin s00 + t00 ≡⟨ cong₂ _+_ (proj₁ rec-s) (proj₁ rec-t) ⟩ s10 + t10 ≡⟨ cong₂ _+_ (cong (λ x → ⟦ embed ds ⟧ x ⟦fst⟧) (sym weak-id)) (cong (λ x → ⟦ embed dt ⟧ x ⟦fst⟧) (sym weak-id)) ⟩ ⟦ embed ds ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦fst⟧ {ℕ} • ρ′)) ⟦fst⟧ + ⟦ embed dt ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦fst⟧ {ℕ} • ρ′)) ⟦fst⟧ ≡⟨ cong₂ _+_ (cong (λ f → f ⟦fst⟧) (sym (weaken-sound (embed ds) (⟦fst⟧ • ρ′)))) (cong (λ f → f ⟦fst⟧) (sym (weaken-sound (embed dt) (⟦fst⟧ • ρ′)))) ⟩ ⟦ weaken (drop _ • Γ≼Γ) (embed ds) ⟧ (⟦fst⟧ • ρ′) ⟦fst⟧ + ⟦ weaken (drop _ • Γ≼Γ) (embed dt) ⟧ (⟦fst⟧ • ρ′) ⟦fst⟧ ∎) , (begin s01 + t01 ≡⟨ cong₂ _+_ (proj₂ rec-s) (proj₂ rec-t) ⟩ s11 + t11 ≡⟨ cong₂ _+_ (cong (λ x → ⟦ embed ds ⟧ x ⟦snd⟧) (sym weak-id)) (cong (λ x → ⟦ embed dt ⟧ x ⟦snd⟧) (sym weak-id)) ⟩ ⟦ embed ds ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦snd⟧ {ℕ} • ρ′)) ⟦snd⟧ + ⟦ embed dt ⟧ (⟦ drop _ • Γ≼Γ ⟧ (⟦snd⟧ {ℕ} • ρ′)) ⟦snd⟧ ≡⟨ cong₂ _+_ (cong (λ f → f ⟦snd⟧) (sym (weaken-sound (embed ds) (⟦snd⟧ • ρ′)))) (cong (λ f → f ⟦snd⟧) (sym (weaken-sound (embed dt) (⟦snd⟧ • ρ′)))) ⟩ ⟦ weaken (drop _ • Γ≼Γ) (embed ds) ⟧ (⟦snd⟧ • ρ′) ⟦snd⟧ + ⟦ weaken (drop _ • Γ≼Γ) (embed dt) ⟧ (⟦snd⟧ • ρ′) ⟦snd⟧ ∎) where open ≡-Reasoning embed-correct {dt = Δunion ds dt} {ρ} {V} {ρ′} {C} = cong₂ _++_ (embed-correct {dt = ds} {ρ} {car V} {ρ′} {C}) (embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C}) embed-correct {dt = Δdiff ds dt} {ρ} {V} {ρ′} {C} = cong₂ _\\_ (embed-correct {dt = ds} {ρ} {car V} {ρ′} {C}) (embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C}) embed-correct {dt = Δmap₁ s dt} {ρ} {V} {ρ′} {C} = cong₂ mapBag (weak-eq-term s C) (embed-correct {dt = dt} {ρ} {car V} {ρ′} {C}) embed-correct {dt = Δmap₀ s ds t dt} {ρ} {V} {ρ′} {C} = cong₂ _\\_ (cong₂ mapBag (extensionality (λ v → begin proj₂ (⟦ ds ⟧Δ ρ (car V) v (v , v) refl) ≡⟨ proj₂ (embed-correct {dt = ds} {ρ} {car V} {ρ′} {C} v (v , v) (λ f → f v v) refl (refl , refl)) ⟩ ⟦ embed ds ⟧ ρ′ v (λ f → f v v) ⟦snd⟧ ≡⟨ cong (λ hole → hole v (λ f → f v v) ⟦snd⟧) (weaken-once {t = embed ds} {v} {ρ′}) ⟩ ⟦ weaken (drop _ • Γ≼Γ) (embed ds) ⟧ (v • ρ′) v (λ f → f v v) ⟦snd⟧ ∎)) (cong₂ _++_ (weak-eq-term t C) (embed-correct {dt = dt} {ρ} {cadr V} {ρ′} {C}))) (cong₂ mapBag (weak-eq-term s C) (weak-eq-term t C)) where open ≡-Reasoning embed-correct {dt = Δapp ds t dt} {ρ} {cons vds vdt R[t,dt] _} {ρ′} {C} rewrite sym (weak-eq-term t C) = embed-correct {dt = ds} {ρ} {vds} {ρ′} {C} (⟦ t ⟧Term (ignore ρ)) (⟦ dt ⟧Δ ρ vdt) (⟦ embed dt ⟧Term ρ′) R[t,dt] (embed-correct {dt = dt} {ρ} {vdt} {ρ′} {C}) embed-correct {dt = Δabs dt} {ρ} {V} {ρ′} {C} = λ w dw dw′ R[w,dw] dw≈dw′ → embed-correct {dt = dt} {cons w dw R[w,dw] ρ} {V w dw R[w,dw]} {dw′ • w • ρ′} {cons refl dw≈dw′ C tt}
refactor proof of embed (#62)
Agda: refactor proof of embed (#62) This refactoring makes the code of embed readable to humans (say, me). (One should remember that programming should be about communicating with humans). Old-commit-hash: bf8e0402fab99cc5be02c0d14d407dd6c8123e06
Agda
mit
inc-lc/ilc-agda
46ea55a1cff1a55f5e77fd87b7af72362e358405
Syntax/Derive/Plotkin.agda
Syntax/Derive/Plotkin.agda
import Parametric.Syntax.Type as Type import Base.Syntax.Context as Context import Parametric.Syntax.Term as Term import Base.Change.Context as DeltaContext import Parametric.Change.Type as ChangeType module Syntax.Derive.Plotkin {Base : Set {- of base types -}} {Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}} (ΔBase : Base → Base) (deriveConst : ∀ {Γ Σ τ} → Constant Σ τ → Term.Terms Constant (DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Γ) (DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Σ) → Term.Term Constant (DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Γ) (ChangeType.ΔType ΔBase τ)) where -- Terms of languages described in Plotkin style open Type Base open Context Type open Term Constant open ChangeType ΔBase open DeltaContext ΔType fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ fit = weaken Γ≼ΔΓ deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (ΔContext Σ) derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ) derive-terms {∅} ∅ = ∅ derive-terms {τ • Σ} (t • ts) = derive t • fit t • derive-terms ts derive (var x) = var (deriveVar x) derive (app s t) = app (app (derive s) (fit t)) (derive t) derive (abs t) = abs (abs (derive t)) derive (const c ts) = deriveConst c (derive-terms ts)
import Parametric.Syntax.Type as Type import Base.Syntax.Context as Context import Parametric.Syntax.Term as Term import Base.Change.Context as ChangeContext import Parametric.Change.Type as ChangeType module Syntax.Derive.Plotkin {Base : Set {- of base types -}} {Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}} (ΔBase : Base → Base) (deriveConst : ∀ {Γ Σ τ} → Constant Σ τ → Term.Terms Constant (ChangeContext.ΔContext (ChangeType.ΔType ΔBase) Γ) (ChangeContext.ΔContext (ChangeType.ΔType ΔBase) Σ) → Term.Term Constant (ChangeContext.ΔContext (ChangeType.ΔType ΔBase) Γ) (ChangeType.ΔType ΔBase τ)) where -- Terms of languages described in Plotkin style open Type Base open Context Type open Term Constant open ChangeType ΔBase open ChangeContext ΔType fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ fit = weaken Γ≼ΔΓ deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (ΔContext Σ) derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ) derive-terms {∅} ∅ = ∅ derive-terms {τ • Σ} (t • ts) = derive t • fit t • derive-terms ts derive (var x) = var (deriveVar x) derive (app s t) = app (app (derive s) (fit t)) (derive t) derive (abs t) = abs (abs (derive t)) derive (const c ts) = deriveConst c (derive-terms ts)
Update qualified import for new module name.
Update qualified import for new module name. Old-commit-hash: d14bfe564331d1fd643bdd11a759bb2a5c9338dc
Agda
mit
inc-lc/ilc-agda
f0143fea39cd9ef10ecc18c39c4bcb147cf3dc0f
total.agda
total.agda
module total where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- * Δ e describes how e changes when its free variables or its arguments change -- * denotational semantics including semantics of changes -- -- Note that Δ is *not* the same as the ∂ operator in -- definition/intro.tex. See discussion at: -- -- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325 -- -- Work in Progress: -- * lemmas about behavior of changes -- * lemmas about behavior of Δ -- * correctness proof for symbolic derivation open import Data.Product open import Data.Unit open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Changes open import ChangeContexts -- DEFINITION of valid changes via a logical relation {- What I wanted to write: data ValidΔ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) → (∀ (s : ⟦ S ⟧) ds (valid : ValidΔ s ds) → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) → ValidΔ f df -} -- What I had to write: valid-Δ : {T : Type} → ⟦ T ⟧ → ⟦ Δ-Type T ⟧ → Set valid-Δ {bool} v dv = ⊤ valid-Δ {S ⇒ T} f df = ∀ (s : ⟦ S ⟧) ds (valid-w : valid-Δ s ds) → valid-Δ (f s) (df s ds) × (apply df f) (apply ds s) ≡ apply (df s ds) (f s) -- LIFTING terms into Δ-Contexts lift-term : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ τ lift-term {Γ₁} {Γ₂} {{Γ′}} = weaken (≼-trans ≼-Δ-Context Γ′) -- PROPERTIES of lift-term lift-term-ignore : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} {ρ : ⟦ Γ₂ ⟧} (t : Term Γ₁ τ) → ⟦ lift-term {{Γ′}} t ⟧ ρ ≡ ⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ)) lift-term-ignore {{Γ′}} {ρ} t = let Γ″ = ≼-trans ≼-Δ-Context Γ′ in begin ⟦ lift-term {{Γ′}} t ⟧ ρ ≡⟨⟩ ⟦ weaken Γ″ t ⟧ ρ ≡⟨ weaken-sound t ρ ⟩ ⟦ t ⟧ (⟦ ≼-trans ≼-Δ-Context Γ′ ⟧ ρ) ≡⟨ cong (λ x → ⟦ t ⟧ x) (⟦⟧-≼-trans ≼-Δ-Context Γ′ ρ) ⟩ ⟦ t ⟧Term (⟦ ≼-Δ-Context ⟧≼ (⟦ Γ′ ⟧≼ ρ)) ≡⟨⟩ ⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ)) ∎ where open ≡-Reasoning -- PROPERTIES of Δ Δ-abs : ∀ {τ₁ τ₂ Γ₁ Γ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t : Term (τ₁ • Γ₁) τ₂) → let Γ″ = keep Δ-Type τ₁ • keep τ₁ • Γ′ in Δ {{Γ′}} (abs t) ≈ abs (abs (Δ {τ₁ • Γ₁} t)) Δ-abs t = ext-t (λ ρ → refl) Δ-app : ∀ {Γ₁ Γ₂ τ₁ τ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t₁ : Term Γ₁ (τ₁ ⇒ τ₂)) (t₂ : Term Γ₁ τ₁) → Δ {{Γ′}} (app t₁ t₂) ≈ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) Δ-app {{Γ′}} t₁ t₂ = ≈-sym (ext-t (λ ρ′ → let ρ = ⟦ Γ′ ⟧ ρ′ in begin ⟦ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ⟧ ρ′ ≡⟨⟩ diff (⟦ t₁ ⟧ (update ρ) (apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) (⟦ lift-term {{Γ′}} t₂ ⟧ ρ′))) (⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term {{Γ′}} t₂ ⟧ ρ′)) ≡⟨ ≡-cong (λ x → diff (⟦ t₁ ⟧ (update ρ) (apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x)) (⟦ t₁ ⟧ (ignore ρ) x)) (lift-term-ignore {{Γ′}} t₂) ⟩ diff (⟦ t₁ ⟧ (update ρ) (apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) (⟦ t₂ ⟧ (ignore ρ)))) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))) ≡⟨ ≡-cong (λ x → diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))) (apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩ diff (⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ))) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))) ≡⟨⟩ ⟦ Δ {{Γ′}} (app t₁ t₂) ⟧ ρ′ ∎)) where open ≡-Reasoning -- SYMBOLIC DERIVATION derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) derive-var {τ • Γ} this = this derive-var {τ • Γ} (that x) = that (that (derive-var x)) _and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a and b = if a b false !_ : ∀ {Γ} → Term Γ bool → Term Γ bool ! x = if x false true -- Term xor _xor-term_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a xor-term b = if a (! b) b -- XXX: to finish this, we just need to call lift-term, like in -- derive-term. Should be easy, just a bit boring. -- Other problem: in fact, Δ t is not the nil change of t, in this context. That's a problem. apply-pure-term : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply-pure-term {bool} = abs (abs (var this xor-term var (that this))) apply-pure-term {τ₁ ⇒ τ₂} {Γ} = abs (abs (abs (app (app apply-pure-term (app (app (var (that (that this))) (var this)) ({!Δ {Γ} (var this)!}))) (app (var (that this)) (var this))))) --abs (abs (abs (app (app apply-compose-term (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x (Δx)) ( f x ) apply-term : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ apply-term {τ ⇒ τ₁} = λ df f → app (app apply-pure-term df) f apply-term {bool} = _xor-term_ diff-term : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ) diff-term {τ ⇒ τ₁} = λ f₁ f₂ → --λx. λdx. diff ( f₁ (apply dx x)) (f₂ x) abs (abs (diff-term {τ₁} (app (weaken ≼-in-body f₁) (apply-term (var this) (var (that this)))) (app (weaken ≼-in-body f₂) (var (that this))))) where ≼-in-body = drop (Δ-Type τ) • (drop τ • ≼-refl) diff-term {bool} = _xor-term_ derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ) derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t)) where Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x)) derive-term {{Γ′}} true = false derive-term {{Γ′}} false = false derive-term {{Γ′}} (if c t e) = if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c)) (diff-term (apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e)) (lift-term {{Γ′}} t)) (if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c))) (diff-term (apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t)) (lift-term {{Γ′}} e)) (if (lift-term {{Γ′}} c) (derive-term {{Γ′}} t) (derive-term {{Γ′}} e))) derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t) -- CORRECTNESS of derivation derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) → diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡ ⟦ derive-var x ⟧ ρ derive-var-correct (dv • v • ρ) this = diff-apply dv v derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) → Δ {{Γ′}} t ≈ derive-term {{Γ′}} t derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) = begin Δ (abs t) ≈⟨ Δ-abs t ⟩ abs (abs (Δ {τ • Γ₁} t)) ≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} t)) ⟩ abs (abs (derive-term {τ • Γ₁} t)) ≡⟨⟩ derive-term (abs t) ∎ where open ≈-Reasoning Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) = begin Δ (app t₁ t₂) ≈⟨ Δ-app t₁ t₂ ⟩ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩ app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) ≡⟨⟩ derive-term {{Γ′}} (app t₁ t₂) ∎ where open ≈-Reasoning derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ → begin ⟦ Δ {{Γ′}} (var x) ⟧ ρ ≡⟨⟩ diff (⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ))) (⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ))) ≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩ ⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ) ≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩ ⟦ lift Γ′ (derive-var x) ⟧Var ρ ∎) where open ≡-Reasoning derive-term-correct true = ext-t (λ ρ → ≡-refl) derive-term-correct false = ext-t (λ ρ → ≡-refl) derive-term-correct (if t₁ t₂ t₃) = {!!} derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
module total where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- * Δ e describes how e changes when its free variables or its arguments change -- * denotational semantics including semantics of changes -- -- Note that Δ is *not* the same as the ∂ operator in -- definition/intro.tex. See discussion at: -- -- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325 -- -- Work in Progress: -- * lemmas about behavior of changes -- * lemmas about behavior of Δ -- * correctness proof for symbolic derivation open import Data.Product open import Data.Unit open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Changes open import ChangeContexts -- DEFINITION of valid changes via a logical relation {- What I wanted to write: data ValidΔ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) → (∀ (s : ⟦ S ⟧) ds (valid : ValidΔ s ds) → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) → ValidΔ f df -} -- What I had to write: valid-Δ : {T : Type} → ⟦ T ⟧ → ⟦ Δ-Type T ⟧ → Set valid-Δ {bool} v dv = ⊤ valid-Δ {S ⇒ T} f df = ∀ (s : ⟦ S ⟧) ds (valid-w : valid-Δ s ds) → valid-Δ (f s) (df s ds) × (apply df f) (apply ds s) ≡ apply (df s ds) (f s) -- LIFTING terms into Δ-Contexts lift-term : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ τ lift-term {Γ₁} {Γ₂} {{Γ′}} = weaken (≼-trans ≼-Δ-Context Γ′) -- PROPERTIES of lift-term lift-term-ignore : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} {ρ : ⟦ Γ₂ ⟧} (t : Term Γ₁ τ) → ⟦ lift-term {{Γ′}} t ⟧ ρ ≡ ⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ)) lift-term-ignore {{Γ′}} {ρ} t = let Γ″ = ≼-trans ≼-Δ-Context Γ′ in begin ⟦ lift-term {{Γ′}} t ⟧ ρ ≡⟨⟩ ⟦ weaken Γ″ t ⟧ ρ ≡⟨ weaken-sound t ρ ⟩ ⟦ t ⟧ (⟦ ≼-trans ≼-Δ-Context Γ′ ⟧ ρ) ≡⟨ cong (λ x → ⟦ t ⟧ x) (⟦⟧-≼-trans ≼-Δ-Context Γ′ ρ) ⟩ ⟦ t ⟧Term (⟦ ≼-Δ-Context ⟧≼ (⟦ Γ′ ⟧≼ ρ)) ≡⟨⟩ ⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ)) ∎ where open ≡-Reasoning -- PROPERTIES of Δ Δ-abs : ∀ {τ₁ τ₂ Γ₁ Γ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t : Term (τ₁ • Γ₁) τ₂) → let Γ″ = keep Δ-Type τ₁ • keep τ₁ • Γ′ in Δ {{Γ′}} (abs t) ≈ abs (abs (Δ {τ₁ • Γ₁} t)) Δ-abs t = ext-t (λ ρ → refl) Δ-app : ∀ {Γ₁ Γ₂ τ₁ τ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t₁ : Term Γ₁ (τ₁ ⇒ τ₂)) (t₂ : Term Γ₁ τ₁) → Δ {{Γ′}} (app t₁ t₂) ≈ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) Δ-app {{Γ′}} t₁ t₂ = ≈-sym (ext-t (λ ρ′ → let ρ = ⟦ Γ′ ⟧ ρ′ in begin ⟦ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ⟧ ρ′ ≡⟨⟩ diff (⟦ t₁ ⟧ (update ρ) (apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) (⟦ lift-term {{Γ′}} t₂ ⟧ ρ′))) (⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term {{Γ′}} t₂ ⟧ ρ′)) ≡⟨ ≡-cong (λ x → diff (⟦ t₁ ⟧ (update ρ) (apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x)) (⟦ t₁ ⟧ (ignore ρ) x)) (lift-term-ignore {{Γ′}} t₂) ⟩ diff (⟦ t₁ ⟧ (update ρ) (apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) (⟦ t₂ ⟧ (ignore ρ)))) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))) ≡⟨ ≡-cong (λ x → diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))) (apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩ diff (⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ))) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))) ≡⟨⟩ ⟦ Δ {{Γ′}} (app t₁ t₂) ⟧ ρ′ ∎)) where open ≡-Reasoning -- SYMBOLIC DERIVATION derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) derive-var {τ • Γ} this = this derive-var {τ • Γ} (that x) = that (that (derive-var x)) _and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a and b = if a b false !_ : ∀ {Γ} → Term Γ bool → Term Γ bool ! x = if x false true -- Term xor _xor-term_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool a xor-term b = if a (! b) b diff-term : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ) -- XXX: to finish this, we just need to call lift-term, like in -- derive-term. Should be easy, just a bit boring. -- Other problem: in fact, Δ t is not the nil change of t, in this context. That's a problem. apply-pure-term : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply-pure-term {bool} = abs (abs (var this xor-term var (that this))) apply-pure-term {τ₁ ⇒ τ₂} {Γ} = abs (abs (abs (app (app apply-pure-term (app (app (var (that (that this))) (var this)) (diff-term (var this) (var this)))) (app (var (that this)) (var this))))) --abs (abs (abs (app (app apply-compose-term (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x (Δx)) ( f x ) apply-term : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ apply-term {τ ⇒ τ₁} = λ df f → app (app apply-pure-term df) f apply-term {bool} = _xor-term_ diff-term {τ ⇒ τ₁} = λ f₁ f₂ → --λx. λdx. diff ( f₁ (apply dx x)) (f₂ x) abs (abs (diff-term {τ₁} (app (weaken ≼-in-body f₁) (apply-term (var this) (var (that this)))) (app (weaken ≼-in-body f₂) (var (that this))))) where ≼-in-body = drop (Δ-Type τ) • (drop τ • ≼-refl) diff-term {bool} = _xor-term_ derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ) derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t)) where Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x)) derive-term {{Γ′}} true = false derive-term {{Γ′}} false = false derive-term {{Γ′}} (if c t e) = if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c)) (diff-term (apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e)) (lift-term {{Γ′}} t)) (if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c))) (diff-term (apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t)) (lift-term {{Γ′}} e)) (if (lift-term {{Γ′}} c) (derive-term {{Γ′}} t) (derive-term {{Γ′}} e))) derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t) -- CORRECTNESS of derivation derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) → diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡ ⟦ derive-var x ⟧ ρ derive-var-correct (dv • v • ρ) this = diff-apply dv v derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) → Δ {{Γ′}} t ≈ derive-term {{Γ′}} t derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) = begin Δ (abs t) ≈⟨ Δ-abs t ⟩ abs (abs (Δ {τ • Γ₁} t)) ≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} t)) ⟩ abs (abs (derive-term {τ • Γ₁} t)) ≡⟨⟩ derive-term (abs t) ∎ where open ≈-Reasoning Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) = begin Δ (app t₁ t₂) ≈⟨ Δ-app t₁ t₂ ⟩ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩ app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) ≡⟨⟩ derive-term {{Γ′}} (app t₁ t₂) ∎ where open ≈-Reasoning derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ → begin ⟦ Δ {{Γ′}} (var x) ⟧ ρ ≡⟨⟩ diff (⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ))) (⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ))) ≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩ ⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ) ≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩ ⟦ lift Γ′ (derive-var x) ⟧Var ρ ∎) where open ≡-Reasoning derive-term-correct true = ext-t (λ ρ → ≡-refl) derive-term-correct false = ext-t (λ ρ → ≡-refl) derive-term-correct (if t₁ t₂ t₃) = {!!} derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
implement apply-term (see #25)
agda: implement apply-term (see #25) The mutual recursion is accepted by Agda. Old-commit-hash: 22f8cd2f280a41e9e8e4578726267f3f2a199d3f
Agda
mit
inc-lc/ilc-agda
cbe1ca6e8fe8ab815052c5862ecc57d1c11fd7f7
Parametric/Change/Validity.agda
Parametric/Change/Validity.agda
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra as CA using (ChangeAlgebraFamily) open import Level Structure : Set₁ Structure = ChangeAlgebraFamily zero ⟦_⟧Base module Structure (change-algebra-base : Structure) where -- change algebras open CA public renaming ( [] to ∅ ; _∷_ to _•_ ) -- change algebra for every type change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type change-algebra (base ι) = change-algebra₍ ι ₎ change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type change-algebra-family = family change-algebra -- function changes open FunctionChanges public using (cons) module _ {τ₁ τ₂ : Type} where open FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public renaming ( correct to is-valid ; apply to call-change ) -- environment changes open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming ( changeAlgebra to environment-changes ) after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧ after-env {Γ} = after₍ Γ ₎
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e) ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra as CA using (ChangeAlgebraFamily) open import Level -- Extension Point: Change algebras for base types Structure : Set₁ Structure = ChangeAlgebraFamily zero ⟦_⟧Base module Structure (change-algebra-base : Structure) where -- change algebras open CA public renaming ( [] to ∅ ; _∷_ to _•_ ) -- We provide: change algebra for every type change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type change-algebra (base ι) = change-algebra₍ ι ₎ change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type change-algebra-family = family change-algebra -- function changes open FunctionChanges public using (cons) module _ {τ₁ τ₂ : Type} where open FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public renaming ( correct to is-valid ; apply to call-change ) -- We also provide: change environments (aka. environment changes). open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming ( changeAlgebra to environment-changes ) after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧ after-env {Γ} = after₍ Γ ₎
Document P.C.Validity.
Document P.C.Validity. Old-commit-hash: 35c83b38382aa41cf8d8162e7cb4e14ec2cd75f0
Agda
mit
inc-lc/ilc-agda
50819fad2a9bd8093c7722e7d88f99deb1ca7fc2
Parametric/Change/Term.agda
Parametric/Change/Term.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply diff-base apply-base {base ι} = diff-base , apply-base lift-diff-apply diff-base apply-base {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff-base apply-base {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff-base apply-base {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff-base apply-base {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff-base apply-base {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) lift-diff diff-base apply-base = λ {τ Γ} → proj₁ (lift-diff-apply diff-base apply-base {τ} {Γ}) lift-apply : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-apply diff-base apply-base = λ {τ Γ} → proj₂ (lift-diff-apply diff-base apply-base {τ} {Γ})
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure (diff-base : DiffStructure) (apply-base : ApplyStructure) where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply {base ι} = diff-base , apply-base lift-diff-apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) lift-diff = λ {τ Γ} → proj₁ (lift-diff-apply {τ} {Γ}) lift-apply : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-apply = λ {τ Γ} → proj₂ (lift-diff-apply {τ} {Γ})
Move parameter to module level.
Move parameter to module level. Old-commit-hash: 46e0ad74c26c053c33e0317365ac3c7047809529
Agda
mit
inc-lc/ilc-agda
d109d6a26c2ede68c53dd2c7f83ec7f6e6508725
Syntax/Context/Popl14.agda
Syntax/Context/Popl14.agda
module Syntax.Context.Popl14 where -- Context specific to Popl14 version of the calculus -- -- This module exports Syntax.Context specialized to -- Syntax.Type.Popl14.Type and declares ΔContext appropriate -- to Popl14 version of the incrementalization system. -- This ΔContext may not make sense for other systems. open import Syntax.Type.Popl14 public open import Syntax.Context Type as Ctx open Ctx public hiding (lift) ΔContext : Context → Context ΔContext ∅ = ∅ ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ -- Aliasing of lemmas in Calculus Popl14 Γ≼Γ : ∀ {Γ} → Γ ≼ Γ Γ≼Γ = ≼-refl Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ Γ≼ΔΓ {∅} = ∅ Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ -- Aliasing of weakening of variables weakenVar : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ weakenVar = Ctx.lift
module Syntax.Context.Popl14 where -- Context specific to Popl14 version of the calculus -- -- This module exports Syntax.Context specialized to -- Syntax.Type.Popl14.Type and declares ΔContext appropriate -- to Popl14 version of the incrementalization system. -- This ΔContext may not make sense for other systems. open import Syntax.Type.Popl14 public import Syntax.Context Type as Ctx open Ctx public hiding (lift) ΔContext : Context → Context ΔContext ∅ = ∅ ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ -- Aliasing of lemmas in Calculus Popl14 Γ≼Γ : ∀ {Γ} → Γ ≼ Γ Γ≼Γ = ≼-refl Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ Γ≼ΔΓ {∅} = ∅ Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ -- Aliasing of weakening of variables weakenVar : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ weakenVar = Ctx.lift
remove redundant 'open' in Syntax.Context.Popl14
Agda: remove redundant 'open' in Syntax.Context.Popl14 https://github.com/ps-mr/ilc/commit/02bb5ef8bf31b80d9b640a440b4ec1801aa500fe#commitcomment-3645919 Old-commit-hash: 97db5813bea3cc8a973f051efb3b6d3f075681d0
Agda
mit
inc-lc/ilc-agda
ac5970524f01ba1d4dfda134c9486fe83efe0e35
lib/Data/Bool/NP.agda
lib/Data/Bool/NP.agda
{-# OPTIONS --universe-polymorphism #-} module Data.Bool.NP where open import Data.Bool using (Bool; true; false; T; if_then_else_; not) import Algebra import Data.Bool.Properties open import Data.Unit using (⊤) open import Data.Sum open import Function open import Relation.Binary.NP open import Relation.Binary.Logical open import Relation.Nullary open import Relation.Nullary.Decidable import Relation.Binary.PropositionalEquality as ≡ open ≡ using (_≡_) module Xor° = Algebra.CommutativeRing Data.Bool.Properties.commutativeRing-xor-∧ module Bool° = Algebra.CommutativeSemiring Data.Bool.Properties.commutativeSemiring-∧-∨ check : ∀ b → {pf : T b} → ⊤ check = _ If_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → (T b → A) → (T (not b) → B) → if b then A else B If_then_else_ true x _ = x _ If_then_else_ false _ x = x _ If′_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → A → B → if b then A else B If′_then_else_ true x _ = x If′_then_else_ false _ x = x If-map : ∀ {A B C D : Set} b (f : T b → A → C) (g : T (not b) → B → D) → if b then A else B → if b then C else D If-map true f _ = f _ If-map false _ f = f _ If-elim : ∀ {A B : Set} {P : Bool → Set} b (f : T b → A → P true) (g : T (not b) → B → P false) → if b then A else B → P b If-elim true f _ = f _ If-elim false _ f = f _ If-true : ∀ {A B : Set} {b} → T b → (if b then A else B) ≡ A If-true {b = true} _ = ≡.refl If-true {b = false} () If-false : ∀ {A B : Set} {b} → T (not b) → (if b then A else B) ≡ B If-false {b = true} () If-false {b = false} _ = ≡.refl cong-if : ∀ {A B : Set} b {t₀ t₁} (f : A → B) → (if b then f t₀ else f t₁) ≡ f (if b then t₀ else t₁) cong-if true _ = ≡.refl cong-if false _ = ≡.refl data ⟦Bool⟧ : (b₁ b₂ : Bool) → Set where ⟦true⟧ : ⟦Bool⟧ true true ⟦false⟧ : ⟦Bool⟧ false false private module ⟦Bool⟧-Internals where refl : Reflexive ⟦Bool⟧ refl {true} = ⟦true⟧ refl {false} = ⟦false⟧ sym : Symmetric ⟦Bool⟧ sym ⟦true⟧ = ⟦true⟧ sym ⟦false⟧ = ⟦false⟧ trans : Transitive ⟦Bool⟧ trans ⟦true⟧ = id trans ⟦false⟧ = id subst : ∀ {ℓ} → Substitutive ⟦Bool⟧ ℓ subst _ ⟦true⟧ = id subst _ ⟦false⟧ = id _≟_ : Decidable ⟦Bool⟧ true ≟ true = yes ⟦true⟧ false ≟ false = yes ⟦false⟧ true ≟ false = no (λ()) false ≟ true = no (λ()) isEquivalence : IsEquivalence ⟦Bool⟧ isEquivalence = record { refl = refl; sym = sym; trans = trans } isDecEquivalence : IsDecEquivalence ⟦Bool⟧ isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ } setoid : Setoid _ _ setoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isEquivalence = isEquivalence } decSetoid : DecSetoid _ _ decSetoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isDecEquivalence = isDecEquivalence } equality : Equality ⟦Bool⟧ equality = record { isEquivalence = isEquivalence; subst = subst } module ⟦Bool⟧-Props where open ⟦Bool⟧-Internals public using (subst; decSetoid; equality) open DecSetoid decSetoid public open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans) ⟦if⟨_⟩_then_else_⟧ : ∀ {a₁ a₂ aᵣ} → (∀⟨ Aᵣ ∶ ⟦Set⟧ {a₁} {a₂} aᵣ ⟩⟦→⟧ ⟦Bool⟧ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ) if_then_else_ if_then_else_ ⟦if⟨_⟩_then_else_⟧ _ ⟦true⟧ xᵣ _ = xᵣ ⟦if⟨_⟩_then_else_⟧ _ ⟦false⟧ _ xᵣ = xᵣ ⟦If′⟨_,_⟩_then_else_⟧ : ∀ {ℓ₁ ℓ₂ ℓᵣ} → (∀⟨ Aᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧ ∀⟨ Bᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧ ⟨ bᵣ ∶ ⟦Bool⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Bᵣ ⟦→⟧ ⟦if⟨ ⟦Set⟧ _ ⟩ bᵣ then Aᵣ else Bᵣ ⟧) If′_then_else_ If′_then_else_ ⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦true⟧ xᵣ _ = xᵣ ⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦false⟧ _ xᵣ = xᵣ _==_ : (x y : Bool) → Bool true == true = true true == false = false false == true = false false == false = true module == where _≈_ : (x y : Bool) → Set x ≈ y = T (x == y) refl : Reflexive _≈_ refl {true} = _ refl {false} = _ subst : ∀ {ℓ} → Substitutive _≈_ ℓ subst _ {true} {true} _ = id subst _ {false} {false} _ = id subst _ {true} {false} () subst _ {false} {true} () sym : Symmetric _≈_ sym {x} {y} eq = subst (λ y → y ≈ x) {x} {y} eq (refl {x}) trans : Transitive _≈_ trans {x} {y} {z} x≈y y≈z = subst (_≈_ x) {y} {z} y≈z x≈y _≟_ : Decidable _≈_ true ≟ true = yes _ false ≟ false = yes _ true ≟ false = no (λ()) false ≟ true = no (λ()) isEquivalence : IsEquivalence _≈_ isEquivalence = record { refl = λ {x} → refl {x} ; sym = λ {x} {y} → sym {x} {y} ; trans = λ {x} {y} {z} → trans {x} {y} {z} } isDecEquivalence : IsDecEquivalence _≈_ isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ } setoid : Setoid _ _ setoid = record { Carrier = Bool; _≈_ = _≈_ ; isEquivalence = isEquivalence } decSetoid : DecSetoid _ _ decSetoid = record { Carrier = Bool; _≈_ = _≈_; isDecEquivalence = isDecEquivalence } module ⟦Bool⟧-Reasoning = Setoid-Reasoning ⟦Bool⟧-Props.setoid open Data.Bool public ⟦true⟧′ : ∀ {b} → T b → ⟦Bool⟧ true b ⟦true⟧′ {true} _ = ⟦true⟧ ⟦true⟧′ {false} () ⟦false⟧′ : ∀ {b} → T (not b) → ⟦Bool⟧ false b ⟦false⟧′ {true} () ⟦false⟧′ {false} _ = ⟦false⟧ T∧ : ∀ {b₁ b₂} → T b₁ → T b₂ → T (b₁ ∧ b₂) T∧ {true} {true} _ _ = _ T∧ {false} {_} () _ T∧ {true} {false} _ () T∧₁ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₁ T∧₁ {true} {true} _ = _ T∧₁ {false} {_} () T∧₁ {true} {false} () T∧₂ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₂ T∧₂ {true} {true} _ = _ T∧₂ {false} {_} () T∧₂ {true} {false} () T∨'⊎ : ∀ {b₁ b₂} → T (b₁ ∨ b₂) → T b₁ ⊎ T b₂ T∨'⊎ {true} _ = inj₁ _ T∨'⊎ {false} {true} _ = inj₂ _ T∨'⊎ {false} {false} () T∨₁ : ∀ {b₁ b₂} → T b₁ → T (b₁ ∨ b₂) T∨₁ {true} _ = _ T∨₁ {false} {true} _ = _ T∨₁ {false} {false} () T∨₂ : ∀ {b₁ b₂} → T b₂ → T (b₁ ∨ b₂) T∨₂ {true} _ = _ T∨₂ {false} {true} _ = _ T∨₂ {false} {false} () T'not'¬ : ∀ {b} → T (not b) → ¬ (T b) T'not'¬ {false} _ = λ() T'not'¬ {true} () T'¬'not : ∀ {b} → ¬ (T b) → T (not b) T'¬'not {true} f = f _ T'¬'not {false} _ = _ Tdec : ∀ b → Dec (T b) Tdec true = yes _ Tdec false = no λ()
{-# OPTIONS --universe-polymorphism #-} module Data.Bool.NP where open import Data.Bool using (Bool; true; false; T; if_then_else_; not) import Algebra import Data.Bool.Properties as B open import Data.Unit using (⊤) open import Data.Product open import Data.Sum open import Function open import Relation.Binary.NP open import Relation.Binary.Logical open import Relation.Nullary open import Relation.Nullary.Decidable import Relation.Binary.PropositionalEquality as ≡ import Function.Equivalence as E open E.Equivalence using (to; from) open import Function.Equality using (_⟨$⟩_) open ≡ using (_≡_) module Xor° = Algebra.CommutativeRing B.commutativeRing-xor-∧ module Bool° = Algebra.CommutativeSemiring B.commutativeSemiring-∧-∨ check : ∀ b → {pf : T b} → ⊤ check = _ If_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → (T b → A) → (T (not b) → B) → if b then A else B If_then_else_ true x _ = x _ If_then_else_ false _ x = x _ If′_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → A → B → if b then A else B If′_then_else_ true x _ = x If′_then_else_ false _ x = x If-map : ∀ {A B C D : Set} b (f : T b → A → C) (g : T (not b) → B → D) → if b then A else B → if b then C else D If-map true f _ = f _ If-map false _ f = f _ If-elim : ∀ {A B : Set} {P : Bool → Set} b (f : T b → A → P true) (g : T (not b) → B → P false) → if b then A else B → P b If-elim true f _ = f _ If-elim false _ f = f _ If-true : ∀ {A B : Set} {b} → T b → (if b then A else B) ≡ A If-true {b = true} _ = ≡.refl If-true {b = false} () If-false : ∀ {A B : Set} {b} → T (not b) → (if b then A else B) ≡ B If-false {b = true} () If-false {b = false} _ = ≡.refl cong-if : ∀ {A B : Set} b {t₀ t₁} (f : A → B) → (if b then f t₀ else f t₁) ≡ f (if b then t₀ else t₁) cong-if true _ = ≡.refl cong-if false _ = ≡.refl data ⟦Bool⟧ : (b₁ b₂ : Bool) → Set where ⟦true⟧ : ⟦Bool⟧ true true ⟦false⟧ : ⟦Bool⟧ false false private module ⟦Bool⟧-Internals where refl : Reflexive ⟦Bool⟧ refl {true} = ⟦true⟧ refl {false} = ⟦false⟧ sym : Symmetric ⟦Bool⟧ sym ⟦true⟧ = ⟦true⟧ sym ⟦false⟧ = ⟦false⟧ trans : Transitive ⟦Bool⟧ trans ⟦true⟧ = id trans ⟦false⟧ = id subst : ∀ {ℓ} → Substitutive ⟦Bool⟧ ℓ subst _ ⟦true⟧ = id subst _ ⟦false⟧ = id _≟_ : Decidable ⟦Bool⟧ true ≟ true = yes ⟦true⟧ false ≟ false = yes ⟦false⟧ true ≟ false = no (λ()) false ≟ true = no (λ()) isEquivalence : IsEquivalence ⟦Bool⟧ isEquivalence = record { refl = refl; sym = sym; trans = trans } isDecEquivalence : IsDecEquivalence ⟦Bool⟧ isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ } setoid : Setoid _ _ setoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isEquivalence = isEquivalence } decSetoid : DecSetoid _ _ decSetoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isDecEquivalence = isDecEquivalence } equality : Equality ⟦Bool⟧ equality = record { isEquivalence = isEquivalence; subst = subst } module ⟦Bool⟧-Props where open ⟦Bool⟧-Internals public using (subst; decSetoid; equality) open DecSetoid decSetoid public open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans) ⟦if⟨_⟩_then_else_⟧ : ∀ {a₁ a₂ aᵣ} → (∀⟨ Aᵣ ∶ ⟦Set⟧ {a₁} {a₂} aᵣ ⟩⟦→⟧ ⟦Bool⟧ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ) if_then_else_ if_then_else_ ⟦if⟨_⟩_then_else_⟧ _ ⟦true⟧ xᵣ _ = xᵣ ⟦if⟨_⟩_then_else_⟧ _ ⟦false⟧ _ xᵣ = xᵣ ⟦If′⟨_,_⟩_then_else_⟧ : ∀ {ℓ₁ ℓ₂ ℓᵣ} → (∀⟨ Aᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧ ∀⟨ Bᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧ ⟨ bᵣ ∶ ⟦Bool⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Bᵣ ⟦→⟧ ⟦if⟨ ⟦Set⟧ _ ⟩ bᵣ then Aᵣ else Bᵣ ⟧) If′_then_else_ If′_then_else_ ⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦true⟧ xᵣ _ = xᵣ ⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦false⟧ _ xᵣ = xᵣ _==_ : (x y : Bool) → Bool true == true = true true == false = false false == true = false false == false = true module == where _≈_ : (x y : Bool) → Set x ≈ y = T (x == y) refl : Reflexive _≈_ refl {true} = _ refl {false} = _ subst : ∀ {ℓ} → Substitutive _≈_ ℓ subst _ {true} {true} _ = id subst _ {false} {false} _ = id subst _ {true} {false} () subst _ {false} {true} () sym : Symmetric _≈_ sym {x} {y} eq = subst (λ y → y ≈ x) {x} {y} eq (refl {x}) trans : Transitive _≈_ trans {x} {y} {z} x≈y y≈z = subst (_≈_ x) {y} {z} y≈z x≈y _≟_ : Decidable _≈_ true ≟ true = yes _ false ≟ false = yes _ true ≟ false = no (λ()) false ≟ true = no (λ()) isEquivalence : IsEquivalence _≈_ isEquivalence = record { refl = λ {x} → refl {x} ; sym = λ {x} {y} → sym {x} {y} ; trans = λ {x} {y} {z} → trans {x} {y} {z} } isDecEquivalence : IsDecEquivalence _≈_ isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ } setoid : Setoid _ _ setoid = record { Carrier = Bool; _≈_ = _≈_ ; isEquivalence = isEquivalence } decSetoid : DecSetoid _ _ decSetoid = record { Carrier = Bool; _≈_ = _≈_; isDecEquivalence = isDecEquivalence } module ⟦Bool⟧-Reasoning = Setoid-Reasoning ⟦Bool⟧-Props.setoid open Data.Bool public ⟦true⟧′ : ∀ {b} → T b → ⟦Bool⟧ true b ⟦true⟧′ {true} _ = ⟦true⟧ ⟦true⟧′ {false} () ⟦false⟧′ : ∀ {b} → T (not b) → ⟦Bool⟧ false b ⟦false⟧′ {true} () ⟦false⟧′ {false} _ = ⟦false⟧ T∧ : ∀ {b₁ b₂} → T b₁ → T b₂ → T (b₁ ∧ b₂) T∧ p q = _⟨$⟩_ (from B.T-∧) (p , q) T∧₁ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₁ T∧₁ = proj₁ ∘ _⟨$⟩_ (to B.T-∧) T∧₂ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₂ T∧₂ {b₁} = proj₂ ∘ _⟨$⟩_ (to (B.T-∧ {b₁})) T∨'⊎ : ∀ {b₁ b₂} → T (b₁ ∨ b₂) → T b₁ ⊎ T b₂ T∨'⊎ {b₁} = _⟨$⟩_ (to (B.T-∨ {b₁})) T∨₁ : ∀ {b₁ b₂} → T b₁ → T (b₁ ∨ b₂) T∨₁ = _⟨$⟩_ (from B.T-∨) ∘ inj₁ T∨₂ : ∀ {b₁ b₂} → T b₂ → T (b₁ ∨ b₂) T∨₂ {b₁} = _⟨$⟩_ (from (B.T-∨ {b₁})) ∘ inj₂ T'not'¬ : ∀ {b} → T (not b) → ¬ (T b) T'not'¬ {false} _ = λ() T'not'¬ {true} () T'¬'not : ∀ {b} → ¬ (T b) → T (not b) T'¬'not {true} f = f _ T'¬'not {false} _ = _ Tdec : ∀ b → Dec (T b) Tdec true = yes _ Tdec false = no λ()
Use more the stdlib
Use more the stdlib
Agda
bsd-3-clause
crypto-agda/agda-nplib
23b3e5376bd66a7bc1239a94dff9d2749e9a1aee
lib/Function/Extensionality.agda
lib/Function/Extensionality.agda
{-# OPTIONS --without-K #-} module Function.Extensionality where open import Relation.Binary.PropositionalEquality renaming (subst to tr) postulate FunExt : Set λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f₀ f₁ : (x : A) → B x}(f= : ∀ x → f₀ x ≡ f₁ x){{fe : FunExt}} → f₀ ≡ f₁ -- This should be derivable if I had a proper proof of λ= tr-λ= : ∀ {a b p}{A : Set a}{B : A → Set b}{x}(P : B x → Set p) {f g : (x : A) → B x}(fg : (x : A) → f x ≡ g x){{fe : FunExt}} → tr (λ f → P (f x)) (λ= fg) ≡ tr P (fg x)
{-# OPTIONS --without-K #-} module Function.Extensionality where open import Relation.Binary.PropositionalEquality renaming (subst to tr) postulate FunExt : Set λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f₀ f₁ : (x : A) → B x}{{fe : FunExt}} (f= : ∀ x → f₀ x ≡ f₁ x) → f₀ ≡ f₁ -- This should be derivable if I had a proper proof of λ= tr-λ= : ∀ {a b p}{A : Set a}{B : A → Set b}{x}(P : B x → Set p) {f g : (x : A) → B x}{{fe : FunExt}}(fg : (x : A) → f x ≡ g x) → tr (λ f → P (f x)) (λ= fg) ≡ tr P (fg x)
change argument order
FunExt: change argument order
Agda
bsd-3-clause
crypto-agda/agda-nplib
89f99238ec73533604712e27553bd981c818d86a
Parametric/Syntax/Term.agda
Parametric/Syntax/Term.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- The syntax of terms (Fig. 1a and 1b). ------------------------------------------------------------------------ -- The syntax of terms depends on the syntax of simple types -- (because terms are indexed by types in order to rule out -- ill-typed terms). But we are in the Parametric.* hierarchy, so -- we don't know the full syntax of types, only how to lift the -- syntax of base types into the syntax of simple types. This -- means that we have to be parametric in the syntax of base -- types, too. -- -- In such parametric modules that depend on other parametric -- modules, we first import our dependencies under a more -- convenient name. import Parametric.Syntax.Type as Type -- Then we start the module proper, with parameters for all -- extension points of our dependencies. Note that here, the -- "Structure" naming convenion makes some sense, because we can -- say that we need some "Type.Structure" in order to define the -- "Term.Structure". module Parametric.Syntax.Term (Base : Type.Structure) where -- Now inside the module, we can open our dependencies with the -- parameters for their extension points. Again, here the name -- "Structure" makes some sense, because we can say that we want -- to access the "Type.Structure" that is induced by Base. open Type.Structure Base -- At this point, we have dealt with the extension points of our -- dependencies, and we have all the definitions about simple -- types, contexts, variables, and variable sets in scope that we -- provided in Parametric.Syntax.Type. Now we can proceed to -- define our own extension point, following the pattern -- explained in Parametric.Syntax.Type. open import Relation.Binary.PropositionalEquality open import Function using (_∘_) open import Data.Unit open import Data.Sum -- Our extension point is a set of primitives, indexed by the -- types of their arguments and their return type. In general, if -- you're confused about what an extension point means, you might -- want to open the corresponding module in the Nehemiah -- hierarchy to see how it is implemented in the example -- plugin. In this case, that would be the Nehemiah.Syntax.Term -- module. Structure : Set₁ Structure = Context → Type → Set module Structure (Const : Structure) where import Base.Data.DependentList as DependentList open DependentList public using (∅ ; _•_) open DependentList -- Declarations of Term and Terms to enable mutual recursion. -- -- Note that terms are indexed by contexts and types. In the -- paper, we define the abstract syntax of terms in Fig 1a and -- then define a type system in Fig 1b. All lemmas and theorems -- then explicitly specify that they only hold for well-typed -- terms. Here, we use the indices to define a type that can -- only hold well-typed terms in the first place. data Term (Γ : Context) : (τ : Type) → Set -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. Terms : Context → Context → Set Terms Γ = DependentList (Term Γ) -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where -- constants aka. primitives can only occur fully applied. const : ∀ {Σ τ} → (c : Const Σ τ) → (args : Terms Γ Σ) → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ -- we use de Bruijn indicies, so we don't need binding occurrences. abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- Free variables FV : ∀ {τ Γ} → Term Γ τ → Vars Γ FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ FV (const ι ts) = FV-terms ts FV (var x) = singleton x FV (abs t) = tail (FV t) FV (app s t) = FV s ∪ FV t FV-terms ∅ = none FV-terms (t • ts) = FV t ∪ FV-terms ts closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤ closed? t = empty? (FV t) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weaken-terms : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weaken-terms Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weaken-terms Γ₁≼Γ₂ ∅ = ∅ weaken-terms Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weaken-terms Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant) -- HOAS-like smart constructors for lambdas, for different arities. abs₁ : ∀ {Γ τ₁ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → (x : Term Γ′ τ₁) → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ)) abs₁ {Γ} {τ₁} = λ f → abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) abs₂ : ∀ {Γ τ₁ τ₂ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ)) abs₂ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₁ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₃ : ∀ {Γ τ₁ τ₂ τ₃ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ)) abs₃ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₂ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₄ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ)) abs₄ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₃ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₅ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ)) abs₅ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₄ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₆ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ₆ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ₆ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ₆ ⇒ τ)) abs₆ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₅ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- The syntax of terms (Fig. 1a and 1b). ------------------------------------------------------------------------ -- The syntax of terms depends on the syntax of simple types -- (because terms are indexed by types in order to rule out -- ill-typed terms). But we are in the Parametric.* hierarchy, so -- we don't know the full syntax of types, only how to lift the -- syntax of base types into the syntax of simple types. This -- means that we have to be parametric in the syntax of base -- types, too. -- -- In such parametric modules that depend on other parametric -- modules, we first import our dependencies under a more -- convenient name. import Parametric.Syntax.Type as Type -- Then we start the module proper, with parameters for all -- extension points of our dependencies. Note that here, the -- "Structure" naming convenion makes some sense, because we can -- say that we need some "Type.Structure" in order to define the -- "Term.Structure". module Parametric.Syntax.Term (Base : Type.Structure) where -- Now inside the module, we can open our dependencies with the -- parameters for their extension points. Again, here the name -- "Structure" makes some sense, because we can say that we want -- to access the "Type.Structure" that is induced by Base. open Type.Structure Base -- At this point, we have dealt with the extension points of our -- dependencies, and we have all the definitions about simple -- types, contexts, variables, and variable sets in scope that we -- provided in Parametric.Syntax.Type. Now we can proceed to -- define our own extension point, following the pattern -- explained in Parametric.Syntax.Type. open import Relation.Binary.PropositionalEquality open import Function using (_∘_) open import Data.Unit open import Data.Sum -- Our extension point is a set of primitives, indexed by the -- types of their arguments and their return type. In general, if -- you're confused about what an extension point means, you might -- want to open the corresponding module in the Nehemiah -- hierarchy to see how it is implemented in the example -- plugin. In this case, that would be the Nehemiah.Syntax.Term -- module. Structure : Set₁ Structure = Context → Type → Set module Structure (Const : Structure) where import Base.Data.DependentList as DependentList open DependentList public using (∅ ; _•_) open DependentList -- Declarations of Term and Terms to enable mutual recursion. -- -- Note that terms are indexed by contexts and types. In the -- paper, we define the abstract syntax of terms in Fig 1a and -- then define a type system in Fig 1b. All lemmas and theorems -- then explicitly specify that they only hold for well-typed -- terms. Here, we use the indices to define a type that can -- only hold well-typed terms in the first place. data Term (Γ : Context) : (τ : Type) → Set -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. Terms : Context → Context → Set Terms Γ = DependentList (Term Γ) -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where -- constants aka. primitives can only occur fully applied. const : ∀ {Σ τ} → (c : Const Σ τ) → (args : Terms Γ Σ) → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ -- we use de Bruijn indicies, so we don't need binding occurrences. abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- Free variables FV : ∀ {τ Γ} → Term Γ τ → Vars Γ FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ FV (const ι ts) = FV-terms ts FV (var x) = singleton x FV (abs t) = tail (FV t) FV (app s t) = FV s ∪ FV t FV-terms ∅ = none FV-terms (t • ts) = FV t ∪ FV-terms ts closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤ closed? t = empty? (FV t) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weaken-terms : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weaken-terms Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weaken-terms Γ₁≼Γ₂ ∅ = ∅ weaken-terms Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weaken-terms Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant) -- HOAS-like smart constructors for lambdas, for different arities. -- We could also write this: module NamespaceForBadAbs₁ where abs₁′ : ∀ {Γ τ₁ τ} → (Term (τ₁ • Γ) τ₁ → Term (τ₁ • Γ) τ) → (Term Γ (τ₁ ⇒ τ)) abs₁′ {Γ} {τ₁} = λ f → abs (f (var this)) -- However, this is less general, and it is harder to reuse. In particular, -- this cannot be used inside abs₂, ..., abs₆. -- Now, let's write other variants with a loop! open import Data.Vec using (_∷_; []; Vec; foldr) open import Data.Nat module AbsNHelpers where open import Function hoasArgType : ∀ {n} → Context → Type → Vec Type n → Set hoasArgType Γ τ = foldr _ (λ a b → a → b) (Term Γ τ) ∘ Data.Vec.map (Term Γ) -- That is, --hoasArgType Γ τ [] = Term Γ τ --hoasArgType Γ τ (τ₀ ∷ τs) = Term Γ τ₀ → hoasArgType Γ τ τs hoasResType : ∀ {n} → Type → Vec Type n → Type hoasResType τ = foldr _ _⇒_ τ absNType : {n : ℕ} → Vec _ n → Set absNType τs = ∀ {Γ τ} → (f : ∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → hoasArgType Γ′ τ τs) → Term Γ (hoasResType τ τs) -- A better type for absN but a mess to use due to the proofs (which aren't synthesized, even though maybe they should be?) -- absN : (n : ℕ) → {_ : n > 0} → absNType n -- XXX See "how to keep your neighbours in order" for tricks. -- Please the termination checker by keeping this case separate. absNBase : ∀ {τ₁} → absNType (τ₁ ∷ []) absNBase {τ₁} f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) -- Otherwise, the recursive step of absN would invoke absN twice, and the -- termination checker does not figure out that the calls are in fact -- terminating. open AbsNHelpers using (absNType; absNBase) -- XXX: could we be using the same trick as above to take the N implicit -- arguments individually, rather than in a vector? I can't figure out how, -- at least not without trying it. But it seems that's what's shown in Agda 2.4.0 release notes! absN : {n : ℕ} → (τs : Vec _ (suc n)) → absNType τs absN {zero} (τ₁ ∷ []) = absNBase absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f = absNBase (λ {_} {Γ≼Γ′} x₁ → absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) -- What I'd like to write, avoiding the need for absNBase, but can't because of the termination checker. {- absN {zero} (τ₁ ∷ []) f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f = absN (τ₁ ∷ []) (λ {_} {Γ≼Γ′} x₁ → absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) -} -- Declare abs₁ .. abs₆ wrappers for more convenient use, allowing implicit -- type arguments to be synthesized. Somehow, Agda does not manage to -- synthesize τs by unification. -- Implicit arguments are reversed when assembling the list, but that's no real problem. module _ {τ₁ : Type} where τs₁ = τ₁ ∷ [] abs₁ : absNType τs₁ abs₁ = absN τs₁ module _ {τ₂ : Type} where τs₂ = τ₂ ∷ τs₁ abs₂ : absNType τs₂ abs₂ = absN τs₂ module _ {τ₃ : Type} where τs₃ = τ₃ ∷ τs₂ abs₃ : absNType τs₃ abs₃ = absN τs₃ module _ {τ₄ : Type} where τs₄ = τ₄ ∷ τs₃ abs₄ : absNType τs₄ abs₄ = absN τs₄ module _ {τ₅ : Type} where τs₅ = τ₅ ∷ τs₄ abs₅ : absNType τs₅ abs₅ = absN τs₅ module _ {τ₆ : Type} where τs₆ = τ₆ ∷ τs₅ abs₆ : absNType τs₆ abs₆ = absN τs₆
Remove lots of duplication across abs₁ .. abs₆
Remove lots of duplication across abs₁ .. abs₆ The new code is much more clever. Still not perfect, with varying arities we can probably do better.
Agda
mit
inc-lc/ilc-agda
30e1935a87b6ee45bd8266999805c80b80dd4bd0
lib/Relation/Nullary/NP.agda
lib/Relation/Nullary/NP.agda
module Relation.Nullary.NP where open import Type open import Data.Sum open import Relation.Nullary public module _ {a b} {A : ★_ a} {B : ★_ b} where Dec-⊎ : Dec A → Dec B → Dec (A ⊎ B) Dec-⊎ (yes p) _ = yes (inj₁ p) Dec-⊎ (no ¬p) (yes p) = yes (inj₂ p) Dec-⊎ (no ¬p) (no ¬q) = no [ ¬p , ¬q ]
module Relation.Nullary.NP where open import Type open import Data.Sum open import Relation.Nullary public module _ {a b} {A : ★_ a} {B : ★_ b} where Dec-⊎ : Dec A → Dec B → Dec (A ⊎ B) Dec-⊎ (yes p) _ = yes (inj₁ p) Dec-⊎ (no ¬p) (yes p) = yes (inj₂ p) Dec-⊎ (no ¬p) (no ¬q) = no [ ¬p , ¬q ] module _ (to : A → B)(from : B → A) where map-Dec : Dec A → Dec B map-Dec (yes p) = yes (to p) map-Dec (no ¬p) = no (λ z → ¬p (from z))
add map-Dec
add map-Dec
Agda
bsd-3-clause
crypto-agda/agda-nplib
bd3c7e9ede5e1077663b478e2d90ba337c971567
lib/FFI/JS/BigI.agda
lib/FFI/JS/BigI.agda
{-# OPTIONS --without-K #-} open import FFI.JS hiding (toString) module FFI.JS.BigI where postulate BigI : Set postulate BigI▹JSValue : BigI → JSValue {-# COMPILED_JS BigI▹JSValue function(x) { return x; } #-} postulate unsafe-JSValue▹BigI : JSValue → BigI {-# COMPILED_JS unsafe-JSValue▹BigI function(x) { return x; } #-} postulate bigI : (x base : String) → BigI {-# COMPILED_JS bigI function(x) { return function (y) { return require("bigi")(x,y); }; } #-} postulate negate : (x : BigI) → BigI {-# COMPILED_JS negate function(x) { return x.negate(); } #-} postulate add : (x y : BigI) → BigI {-# COMPILED_JS add function(x) { return function (y) { return x.add(y); }; } #-} postulate subtract : (x y : BigI) → BigI {-# COMPILED_JS subtract function(x) { return function (y) { return x.subtract(y); }; } #-} postulate multiply : (x y : BigI) → BigI {-# COMPILED_JS multiply function(x) { return function (y) { return x.multiply(y); }; } #-} postulate divide : (x y : BigI) → BigI {-# COMPILED_JS divide function(x) { return function (y) { return x.divide(y); }; } #-} postulate remainder : (x y : BigI) → BigI {-# COMPILED_JS remainder function(x) { return function (y) { return x.remainder(y); }; } #-} postulate mod : (x y : BigI) → BigI {-# COMPILED_JS mod function(x) { return function (y) { return x.mod(y); }; } #-} postulate modPow : (this e m : BigI) → BigI {-# COMPILED_JS modPow function(x) { return function (y) { return function (z) { return x.modPow(y,z); }; }; } #-} postulate modInv : (this m : BigI) → BigI {-# COMPILED_JS modInv function(x) { return function (y) { return x.modInverse(y); }; } #-} postulate gcd : (this m : BigI) → BigI {-# COMPILED_JS gcd function(x) { return function (y) { return x.gcd(y); }; } #-} -- test primality with certainty >= 1-.5^t postulate isProbablePrime : (this : BigI)(t : Number) → Bool {-# COMPILED_JS isProbablePrime function(x) { return function (y) { return x.isProbablePrime(y); }; } #-} postulate signum : (this : BigI) → Number {-# COMPILED_JS signum function(x) { return x.signum(); } #-} postulate equals : (x y : BigI) → Bool {-# COMPILED_JS equals function(x) { return function (y) { return x.equals(y); }; } #-} postulate compareTo : (this x : BigI) → Number {-# COMPILED_JS compareTo function(x) { return function (y) { return x.compareTo(y); }; } #-} postulate toString : (x : BigI) → String {-# COMPILED_JS toString function(x) { return x.toString(); } #-} postulate fromHex : String → BigI {-# COMPILED_JS fromHex require("bigi").fromHex #-} postulate toHex : BigI → String {-# COMPILED_JS toHex function(x) { return x.toHex(); } #-} postulate byteLength : BigI → Number {-# COMPILED_JS byteLength function(x) { return x.byteLength(); } #-} 0I 1I 2I : BigI 0I = bigI "0" "10" 1I = bigI "1" "10" 2I = bigI "2" "10" _≤I_ : BigI → BigI → Bool x ≤I y = compareTo x y ≤Number 0N _<I_ : BigI → BigI → Bool x <I y = compareTo x y <Number 0N _>I_ : BigI → BigI → Bool x >I y = compareTo x y >Number 0N _≥I_ : BigI → BigI → Bool x ≥I y = compareTo x y ≥Number 0N
{-# OPTIONS --without-K #-} open import FFI.JS hiding (toString) open import Type.Eq using (Eq?) open import Data.Bool.Base using (Bool; true; false) renaming (T to ✓) open import Relation.Binary.Core using (_≡_; refl) open import Relation.Binary.PropositionalEquality.TrustMe using (trustMe) module FFI.JS.BigI where postulate BigI : Set postulate BigI▹JSValue : BigI → JSValue {-# COMPILED_JS BigI▹JSValue function(x) { return x; } #-} postulate unsafe-JSValue▹BigI : JSValue → BigI {-# COMPILED_JS unsafe-JSValue▹BigI function(x) { return x; } #-} postulate bigI : (x base : String) → BigI {-# COMPILED_JS bigI function(x) { return function (y) { return require("bigi")(x,y); }; } #-} postulate negate : (x : BigI) → BigI {-# COMPILED_JS negate function(x) { return x.negate(); } #-} postulate add : (x y : BigI) → BigI {-# COMPILED_JS add function(x) { return function (y) { return x.add(y); }; } #-} postulate subtract : (x y : BigI) → BigI {-# COMPILED_JS subtract function(x) { return function (y) { return x.subtract(y); }; } #-} postulate multiply : (x y : BigI) → BigI {-# COMPILED_JS multiply function(x) { return function (y) { return x.multiply(y); }; } #-} postulate divide : (x y : BigI) → BigI {-# COMPILED_JS divide function(x) { return function (y) { return x.divide(y); }; } #-} postulate remainder : (x y : BigI) → BigI {-# COMPILED_JS remainder function(x) { return function (y) { return x.remainder(y); }; } #-} postulate mod : (x y : BigI) → BigI {-# COMPILED_JS mod function(x) { return function (y) { return x.mod(y); }; } #-} postulate modPow : (this e m : BigI) → BigI {-# COMPILED_JS modPow function(x) { return function (y) { return function (z) { return x.modPow(y,z); }; }; } #-} postulate modInv : (this m : BigI) → BigI {-# COMPILED_JS modInv function(x) { return function (y) { return x.modInverse(y); }; } #-} postulate gcd : (this m : BigI) → BigI {-# COMPILED_JS gcd function(x) { return function (y) { return x.gcd(y); }; } #-} -- test primality with certainty >= 1-.5^t postulate isProbablePrime : (this : BigI)(t : Number) → Bool {-# COMPILED_JS isProbablePrime function(x) { return function (y) { return x.isProbablePrime(y); }; } #-} postulate signum : (this : BigI) → Number {-# COMPILED_JS signum function(x) { return x.signum(); } #-} postulate equals : (x y : BigI) → Bool {-# COMPILED_JS equals function(x) { return function (y) { return x.equals(y); }; } #-} postulate compareTo : (this x : BigI) → Number {-# COMPILED_JS compareTo function(x) { return function (y) { return x.compareTo(y); }; } #-} postulate toString : (x : BigI) → String {-# COMPILED_JS toString function(x) { return x.toString(); } #-} postulate fromHex : String → BigI {-# COMPILED_JS fromHex require("bigi").fromHex #-} postulate toHex : BigI → String {-# COMPILED_JS toHex function(x) { return x.toHex(); } #-} postulate byteLength : BigI → Number {-# COMPILED_JS byteLength function(x) { return x.byteLength(); } #-} 0I 1I 2I : BigI 0I = bigI "0" "10" 1I = bigI "1" "10" 2I = bigI "2" "10" _≤I_ : BigI → BigI → Bool x ≤I y = compareTo x y ≤Number 0N _<I_ : BigI → BigI → Bool x <I y = compareTo x y <Number 0N _>I_ : BigI → BigI → Bool x >I y = compareTo x y >Number 0N _≥I_ : BigI → BigI → Bool x ≥I y = compareTo x y ≥Number 0N -- This is a postulates which computes as much as it needs equals-refl : ∀ {x : BigI} → ✓ (equals x x) equals-refl {x} with equals x x ... | true = _ ... | false = STUCK where postulate STUCK : _ instance BigI-Eq? : Eq? BigI BigI-Eq? = record { _==_ = _==_ ; ≡⇒== = ≡⇒== ; ==⇒≡ = ==⇒≡ } module BigI-Eq? where _==_ : BigI → BigI → Bool _==_ = equals ≡⇒== : ∀ {x y} → x ≡ y → ✓ (x == y) ≡⇒== refl = equals-refl ==⇒≡ : ∀ {x y} → ✓ (x == y) → x ≡ y ==⇒≡ _ = trustMe
add Eq? instance, thus depends on nplib
FFI.JS.BigI: add Eq? instance, thus depends on nplib
Agda
bsd-3-clause
crypto-agda/agda-libjs
68af677d56d4f0a4c1aaa95134ae06f0813500c1
arrow.agda
arrow.agda
open import Agda.Builtin.Bool _or_ : Bool → Bool → Bool true or _ = true _ or true = true false or false = false _and_ : Bool → Bool → Bool false and _ = false _ and false = false true and true = true ---------------------------------------- data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false ---------------------------------------- infixr 5 _∷_ data List (A : Set) : Set where ∘ : List A _∷_ : A → List A → List A {-# BUILTIN LIST List #-} {-# BUILTIN NIL ∘ #-} {-# BUILTIN CONS _∷_ #-} any : {A : Set} → (A → Bool) → List A → Bool any _ ∘ = false any f (x ∷ xs) = (f x) or (any f xs) _∈_ : ℕ → List ℕ → Bool x ∈ ∘ = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys _∋_ : List ℕ → ℕ → Bool xs ∋ y = y ∈ xs ---------------------------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow _≡≡_ : Arrow → Arrow → Bool (⇒ q) ≡≡ (⇒ s) = q ≡ s (p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s) _ ≡≡ _ = false _∈∈_ : Arrow → List Arrow → Bool x ∈∈ ∘ = false x ∈∈ (y ∷ ys) with x ≡≡ y ... | true = true ... | false = x ∈∈ ys closure : List Arrow → List ℕ → List ℕ closure ∘ found = found closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found)) closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found)) ... | true = closure (q ∷ rest) found ... | false = closure rest found _,_⊢_ : List Arrow → List ℕ → ℕ → Bool cs , ps ⊢ q = q ∈ (closure cs ps) _⊢_ : List Arrow → Arrow → Bool cs ⊢ (⇒ q) = q ∈ (closure cs ∘) cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q ---------------------------------------- data Separation : Set where model : List ℕ → List ℕ → Separation modelsupports : Separation → List Arrow → ℕ → Bool modelsupports (model holds _) cs n = cs , holds ⊢ n modeldenies : Separation → List Arrow → ℕ → Bool modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails _⟪!_⟫_ : List Arrow → Separation → Arrow → Bool cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q) _⟪_⟫_ : List Arrow → List Separation → Arrow → Bool cs ⟪ ∘ ⟫ arr = false cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr) ---------------------------------------- data Relation : Set where Proved : Relation Derivable : Relation Separated : Relation Unknown : Relation consider : List Arrow → List Separation → Arrow → Relation consider cs ms arr with (arr ∈∈ cs) ... | true = Proved ... | false with (cs ⊢ arr) ... | true = Derivable ... | false with (cs ⟪ ms ⟫ arr) ... | true = Separated ... | false = Unknown proofs : List Arrow proofs = (3 ⇒ (⇒ 4)) ∷ -- (5 ⇒ (⇒ 4)) ∷ (6 ⇒ (⇒ 4)) ∷ (3 ⇒ (⇒ 3)) ∷ (3 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (5 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 7)) ∷ (6 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 7)) ∷ (5 ⇒ (⇒ 10)) ∷ (10 ⇒ (⇒ 4)) ∷ (5 ⇒ (⇒ 11)) ∷ (6 ⇒ (⇒ 11)) ∷ (11 ⇒ (⇒ 4)) ∷ (10 ⇒ (⇒ 7)) ∷ (8 ⇒ (⇒ 4)) ∷ (9 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 8)) ∷ (3 ⇒ (⇒ 8)) ∷ (5 ⇒ (3 ⇒ (⇒ 9))) ∷ (6 ⇒ (7 ⇒ (⇒ 10))) ∷ (6 ⇒ (3 ⇒ (⇒ 3))) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (8 ⇒ (⇒ 10))) ∷ (3 ⇒ (10 ⇒ (⇒ 9))) ∷ (5 ⇒ (⇒ 1)) ∷ (3 ⇒ (1 ⇒ (⇒ 9))) ∷ (1 ⇒ (⇒ 2)) ∷ (10 ⇒ (⇒ 2)) ∷ ∘ cms : List Separation cms = (model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷ (model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷ (model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷ (model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷ (model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷ (model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷ (model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷ (model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷ (model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷ (model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷ (model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷ (model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘ testp : Arrow testp = (5 ⇒ (⇒ 10)) testd : Arrow testd = (5 ⇒ (⇒ 4)) tests : Arrow tests = (5 ⇒ (⇒ 3)) testu : Arrow testu = (6 ⇒ (⇒ 1)) main = (closure proofs (5 ∷ ∘))
open import Data.Bool open import Data.List open import Data.Nat ---------------------------------------- _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false ---------------------------------------- _∈_ : ℕ → List ℕ → Bool x ∈ [] = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys _∋_ : List ℕ → ℕ → Bool xs ∋ y = y ∈ xs ---------------------------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow _≡≡_ : Arrow → Arrow → Bool (⇒ q) ≡≡ (⇒ s) = q ≡ s (p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) ∧ (q ≡≡ s) _ ≡≡ _ = false _∈∈_ : Arrow → List Arrow → Bool x ∈∈ [] = false x ∈∈ (y ∷ ys) with x ≡≡ y ... | true = true ... | false = x ∈∈ ys closure : List Arrow → List ℕ → List ℕ closure [] found = found closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found)) closure ((n ⇒ q) ∷ rest) found with (n ∈ found) ∨ (n ∈ (closure rest found)) ... | true = closure (q ∷ rest) found ... | false = closure rest found _,_⊢_ : List Arrow → List ℕ → ℕ → Bool cs , ps ⊢ q = q ∈ (closure cs ps) _⊢_ : List Arrow → Arrow → Bool cs ⊢ (⇒ q) = q ∈ (closure cs []) cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q ---------------------------------------- data Separation : Set where model : List ℕ → List ℕ → Separation modelsupports : Separation → List Arrow → ℕ → Bool modelsupports (model holds _) cs n = cs , holds ⊢ n modeldenies : Separation → List Arrow → ℕ → Bool modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ []))) fails _⟪!_⟫_ : List Arrow → Separation → Arrow → Bool cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) ∧ (cs ⟪! m ⟫ q) _⟪_⟫_ : List Arrow → List Separation → Arrow → Bool cs ⟪ [] ⟫ arr = false cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) ∨ (cs ⟪ ms ⟫ arr) ---------------------------------------- data Relation : Set where Proved : Relation Derivable : Relation Separated : Relation Unknown : Relation consider : List Arrow → List Separation → Arrow → Relation consider cs ms arr with (arr ∈∈ cs) ... | true = Proved ... | false with (cs ⊢ arr) ... | true = Derivable ... | false with (cs ⟪ ms ⟫ arr) ... | true = Separated ... | false = Unknown proofs : List Arrow proofs = (3 ⇒ (⇒ 4)) ∷ -- (5 ⇒ (⇒ 4)) ∷ (6 ⇒ (⇒ 4)) ∷ (3 ⇒ (⇒ 3)) ∷ (3 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (9 ⇒ (⇒ 3)) ∷ (5 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 7)) ∷ (6 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 8)) ∷ (10 ⇒ (⇒ 7)) ∷ (5 ⇒ (⇒ 10)) ∷ (10 ⇒ (⇒ 4)) ∷ (5 ⇒ (⇒ 11)) ∷ (6 ⇒ (⇒ 11)) ∷ (11 ⇒ (⇒ 4)) ∷ (10 ⇒ (⇒ 7)) ∷ (8 ⇒ (⇒ 4)) ∷ (9 ⇒ (⇒ 7)) ∷ (9 ⇒ (⇒ 8)) ∷ (3 ⇒ (⇒ 8)) ∷ (5 ⇒ (3 ⇒ (⇒ 9))) ∷ (6 ⇒ (7 ⇒ (⇒ 10))) ∷ (6 ⇒ (3 ⇒ (⇒ 3))) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (⇒ 7)) ∷ (7 ⇒ (8 ⇒ (⇒ 10))) ∷ (3 ⇒ (10 ⇒ (⇒ 9))) ∷ (5 ⇒ (⇒ 1)) ∷ (3 ⇒ (1 ⇒ (⇒ 9))) ∷ (1 ⇒ (⇒ 2)) ∷ (10 ⇒ (⇒ 2)) ∷ [] cms : List Separation cms = (model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ []) (5 ∷ 3 ∷ 7 ∷ 7 ∷ [])) ∷ (model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ []) (5 ∷ [])) ∷ (model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ []) (6 ∷ 3 ∷ [])) ∷ (model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ []) (6 ∷ [])) ∷ (model (12 ∷ 4 ∷ 11 ∷ []) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ [])) ∷ (model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ []) (3 ∷ [])) ∷ (model (12 ∷ 4 ∷ 11 ∷ 7 ∷ []) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ [])) ∷ (model (10 ∷ 9 ∷ []) (1 ∷ [])) ∷ (model (3 ∷ 4 ∷ 11 ∷ []) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ [])) ∷ (model (12 ∷ 7 ∷ 1 ∷ []) (4 ∷ 11 ∷ 8 ∷ [])) ∷ (model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ []) (11 ∷ [])) ∷ (model (12 ∷ 4 ∷ 10 ∷ 1 ∷ []) (11 ∷ 3 ∷ [])) ∷ (model (3 ∷ 6 ∷ 5 ∷ []) ([])) ∷ (model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ []) (12 ∷ [])) ∷ [] testp : Arrow testp = (5 ⇒ (⇒ 10)) testd : Arrow testd = (5 ⇒ (⇒ 4)) tests : Arrow tests = (5 ⇒ (⇒ 3)) testu : Arrow testu = (6 ⇒ (⇒ 1)) main = (closure proofs (5 ∷ []))
Convert to stdlib
Convert to stdlib
Agda
mit
louisswarren/hieretikz
fed88a10935d042bd79da68f2e7233104f1a0536
program-distance.agda
program-distance.agda
module program-distance where open import flipbased-implem open import Data.Bits open import Data.Bool open import Data.Vec.NP using (Vec; count; countᶠ) open import Data.Nat.NP open import Data.Nat.Properties open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP import Data.Fin as Fin record PrgDist : Set₁ where constructor mk field _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-right-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ]-[ g → g ≈⅁ h → f ]-[ h ]-[-cong-right-≈↺ pf pf' = ]-[-sym (]-[-cong-left-≈↺ (sym pf') (]-[-sym pf)) ]-[-cong-≗↺ : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' ]-[-cong-≗↺ {c} {c'} {f} {g} {f'} {g'} f≗g f'≗g' pf = ]-[-cong-left-≈↺ {f = g} {g = f} {h = g'} ((≗⇒≈⅁ (λ x → sym (f≗g x)))) (]-[-cong-right-≈↺ pf (≗⇒≈⅁ f'≗g')) breaks : ∀ {c} (EXP : Bit → ⅁ c) → Set breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b -- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁ _⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ⅁ c₀) (⅁₁ : Bit → ⅁ c₁) → Set ⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁ extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ⅁ c} → ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁ extensional-reduction same-games = ]-[-cong-≗↺ (same-games 0b) (same-games 1b) module HomImplem k where -- | Pr[ f ≡ 1 ] - Pr[ g ≡ 1 ] | ≥ ε [ on reals ] -- dist Pr[ f ≡ 1 ] Pr[ g ≡ 1 ] ≥ ε [ on reals ] -- dist (#1 f / 2^ c) (#1 g / 2^ c) ≥ ε [ on reals ] -- dist (#1 f) (#1 g) ≥ ε * 2^ c where ε = 2^ -k [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(-k) * 2^ c [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c - k) [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c ∸ k) [ on natural ] _]-[_ : ∀ {n} (f g : ⅁ n) → Set _]-[_ {n} f g = dist (count↺ f) (count↺ g) ≥ 2^(n ∸ k) ]-[-sym : ∀ {n} {f g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count↺ f) (count↺ g) = f]-[g ]-[-cong-left-≈↺ : ∀ {n} {f g h : ⅁ n} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {n} {f} {g} f≈g g]-[h rewrite ≈⅁⇒≈⅁′ {n} {f} {g} f≈g = g]-[h -- dist #g #h ≥ 2^(n ∸ k) -- dist #f #h ≥ 2^(n ∸ k) module Implem k where _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set _]-[_ {m} {n} f g = dist (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) ≥ 2^((m + n) ∸ k) ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) | ℕ°.+-comm m n = f]-[g postulate helper : ∀ x y z t → x + ((y + z) ∸ t) ≡ y + ((x + z) ∸ t) ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} f≈g g]-[h with 2^*-mono m g]-[h -- 2ᵐ(dist 2ᵒ#g 2ⁿ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ ... | q rewrite sym (dist-2^* m (⟨2^ o ⟩* (count↺ g)) (⟨2^ n ⟩* (count↺ h))) -- dist 2ᵐ2ᵒ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm m o (count↺ g) -- dist 2ᵒ2ᵐ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | sym f≈g -- dist 2ᵒ2ⁿ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm o n (count↺ f) -- dist 2ⁿ2ᵒ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm m n (count↺ h) -- dist 2ⁿ2ᵒ#f 2ⁿ2ᵐ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | dist-2^* n (⟨2^ o ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ h)) -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-+ m (n + o ∸ k) 1 -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ⁺ⁿ⁺ᵒ⁻ᵏ | helper m n o k -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ⁺ᵐ⁺ᵒ⁻ᵏ | sym (2^-+ n (m + o ∸ k) 1) -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ2ᵐ⁺ᵒ⁻ᵏ = 2^*-mono′ n q -- dist 2ᵒ#f 2ᵐ#h ≤ 2ᵐ⁺ᵒ⁻ᵏ prgDist : PrgDist prgDist = mk _]-[_ (λ {m n f g} x → ]-[-sym {m} {n} {f} {g} x) (λ {m n o f g h} x y → ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} x y)
module program-distance where open import flipbased-implem open import Data.Bits open import Data.Bool open import Data.Vec.NP using (Vec; count; countᶠ) open import Data.Nat.NP open import Data.Nat.Properties open import Relation.Nullary open import Relation.Binary open import Relation.Binary.PropositionalEquality.NP import Data.Fin as Fin record PrgDist : Set₁ where constructor mk field _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set ]-[-antisym : ∀ {n} (f : ⅁ n) → ¬ (f ]-[ f) ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-right-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ]-[ g → g ≈⅁ h → f ]-[ h ]-[-cong-right-≈↺ pf pf' = ]-[-sym (]-[-cong-left-≈↺ (sym pf') (]-[-sym pf)) ]-[-cong-≗↺ : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g' ]-[-cong-≗↺ {c} {c'} {f} {g} {f'} {g'} f≗g f'≗g' pf = ]-[-cong-left-≈↺ {f = g} {g = f} {h = g'} ((≗⇒≈⅁ (λ x → sym (f≗g x)))) (]-[-cong-right-≈↺ pf (≗⇒≈⅁ f'≗g')) breaks : ∀ {c} (EXP : Bit → ⅁ c) → Set breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b -- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁ _⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ⅁ c₀) (⅁₁ : Bit → ⅁ c₁) → Set ⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁ extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ⅁ c} → ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁ extensional-reduction same-games = ]-[-cong-≗↺ (same-games 0b) (same-games 1b) private 2^_≥1 : ∀ n → 2^ n ≥ 1 2^ zero ≥1 = s≤s z≤n 2^ suc n ≥1 = z≤n {2^ n} +-mono 2^ n ≥1 module HomImplem k where -- | Pr[ f ≡ 1 ] - Pr[ g ≡ 1 ] | ≥ ε [ on reals ] -- dist Pr[ f ≡ 1 ] Pr[ g ≡ 1 ] ≥ ε [ on reals ] -- dist (#1 f / 2^ c) (#1 g / 2^ c) ≥ ε [ on reals ] -- dist (#1 f) (#1 g) ≥ ε * 2^ c where ε = 2^ -k [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(-k) * 2^ c [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c - k) [ on rationals ] -- dist (#1 f) (#1 g) ≥ 2^(c ∸ k) [ on natural ] _]-[_ : ∀ {n} (f g : ⅁ n) → Set _]-[_ {n} f g = dist (count↺ f) (count↺ g) ≥ 2^(n ∸ k) ]-[-antisym : ∀ {n} (f : ⅁ n) → ¬ (f ]-[ f) ]-[-antisym {n} f f]-[g rewrite dist-refl (count↺ f) with ℕ≤.trans (2^ (n ∸ k) ≥1) f]-[g ... | () ]-[-sym : ∀ {n} {f g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count↺ f) (count↺ g) = f]-[g ]-[-cong-left-≈↺ : ∀ {n} {f g h : ⅁ n} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {n} {f} {g} f≈g g]-[h rewrite ≈⅁⇒≈⅁′ {n} {f} {g} f≈g = g]-[h -- dist #g #h ≥ 2^(n ∸ k) -- dist #f #h ≥ 2^(n ∸ k) module Implem k where _]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set _]-[_ {m} {n} f g = dist ⟨2^ n * count↺ f ⟩ ⟨2^ m * count↺ g ⟩ ≥ 2^((m + n) ∸ k) ]-[-antisym : ∀ {n} (f : ⅁ n) → ¬ (f ]-[ f) ]-[-antisym {n} f f]-[g rewrite dist-refl ⟨2^ n * count↺ f ⟩ with ℕ≤.trans (2^ (n + n ∸ k) ≥1) f]-[g ... | () ]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f ]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym ⟨2^ n * count↺ f ⟩ ⟨2^ m * count↺ g ⟩ | ℕ°.+-comm m n = f]-[g postulate helper : ∀ m n o k → m + ((n + o) ∸ k) ≡ n + ((m + o) ∸ k) helper′ : ∀ m n o k → ⟨2^ m * (2^((n + o) ∸ k))⟩ ≡ ⟨2^ n * (2^((m + o) ∸ k))⟩ ]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} f≈g g]-[h with 2^*-mono m g]-[h -- 2ᵐ(dist 2ᵒ#g 2ⁿ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ ... | q rewrite sym (dist-2^* m ⟨2^ o * count↺ g ⟩ ⟨2^ n * count↺ h ⟩) -- dist 2ᵐ2ᵒ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm m o (count↺ g) -- dist 2ᵒ2ᵐ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | sym f≈g -- dist 2ᵒ2ⁿ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm o n (count↺ f) -- dist 2ⁿ2ᵒ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-comm m n (count↺ h) -- dist 2ⁿ2ᵒ#f 2ⁿ2ᵐ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | dist-2^* n ⟨2^ o * count↺ f ⟩ ⟨2^ m * count↺ h ⟩ -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ | 2^-+ m (n + o ∸ k) 1 -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ⁺ⁿ⁺ᵒ⁻ᵏ | helper m n o k -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ⁺ᵐ⁺ᵒ⁻ᵏ | sym (2^-+ n (m + o ∸ k) 1) -- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ2ᵐ⁺ᵒ⁻ᵏ = 2^*-mono′ n q -- dist 2ᵒ#f 2ᵐ#h ≤ 2ᵐ⁺ᵒ⁻ᵏ prgDist : PrgDist prgDist = mk _]-[_ ]-[-antisym (λ {m n f g} → ]-[-sym {f = f} {g}) (λ {m n o f g h} → ]-[-cong-left-≈↺ {f = f} {g} {h})
upgrade count, and add antisym
distance: upgrade count, and add antisym
Agda
bsd-3-clause
crypto-agda/crypto-agda
74ad4a9a6900f8062300b3ca1cc4b318038c4af5
Popl14/Change/Term.agda
Popl14/Change/Term.agda
module Popl14.Change.Term where -- Terms Calculus Popl14 -- -- Contents -- - Term constructors -- - Weakening on terms -- - `fit`: weaken a term to its ΔContext -- - diff-term, apply-term and their syntactic sugars open import Data.Integer open import Popl14.Syntax.Type public open import Popl14.Syntax.Term public open import Popl14.Change.Type public import Parametric.Change.Term Const ΔBase as ChangeTerm diff-base : ChangeTerm.DiffStructure diff-base {base-int} = abs₂ (λ x y → add x (minus y)) diff-base {base-bag} = abs₂ (λ x y → union x (negate y)) apply-base : ChangeTerm.ApplyStructure apply-base {base-int} = abs₂ (λ Δx x → add x Δx) apply-base {base-bag} = abs₂ (λ Δx x → union x Δx) diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) -- Sugars for diff-term and apply-term infixl 6 _⊕_ _⊝_ _⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ _⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ) t ⊕ Δt = app (app apply-term Δt) t s ⊝ t = app (app diff-term s) t apply-term {base ι} = apply-base {ι} apply-term {σ ⇒ τ} = let Δf = var (that (that this)) f = var (that this) x = var this in -- Δf f x abs (abs (abs (app f x ⊕ app (app Δf x) (x ⊝ x)))) diff-term {base ι} = diff-base {ι} diff-term {σ ⇒ τ} = let g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this in -- g f x Δx abs (abs (abs (abs (app g (x ⊕ Δx) ⊝ app f x))))
module Popl14.Change.Term where -- Terms Calculus Popl14 -- -- Contents -- - Term constructors -- - Weakening on terms -- - `fit`: weaken a term to its ΔContext -- - diff-term, apply-term and their syntactic sugars open import Data.Integer open import Popl14.Syntax.Type public open import Popl14.Syntax.Term public open import Popl14.Change.Type public import Parametric.Change.Term Const ΔBase as ChangeTerm diff-base : ChangeTerm.DiffStructure diff-base {base-int} = abs₂ (λ x y → add x (minus y)) diff-base {base-bag} = abs₂ (λ x y → union x (negate y)) apply-base : ChangeTerm.ApplyStructure apply-base {base-int} = abs₂ (λ Δx x → add x Δx) apply-base {base-bag} = abs₂ (λ Δx x → union x Δx) diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) -- Sugars for diff-term and apply-term infixl 6 _⊕_ _⊝_ _⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ _⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ) t ⊕ Δt = app (app apply-term Δt) t s ⊝ t = app (app diff-term s) t apply-term {base ι} = apply-base {ι} apply-term {σ ⇒ τ} = abs₃ (λ Δf f x → app f x ⊕ app (app Δf x) (x ⊝ x)) diff-term {base ι} = diff-base {ι} diff-term {σ ⇒ τ} = abs₄ (λ g f x Δx → app g (x ⊕ Δx) ⊝ app f x)
Use HOAS-enabled helpers for nested abstractions.
Use HOAS-enabled helpers for nested abstractions. Old-commit-hash: 4efbb3abc5e02e14285a2e9c6998ee6f663cbc2d
Agda
mit
inc-lc/ilc-agda
b1fdb10a5e323a77a174af8dfdcaab062c73405f
flat-funs-implem.agda
flat-funs-implem.agda
module flat-funs-implem where open import Data.Unit using (⊤) open import Data.Fin using (Fin) open import Data.Nat.NP using (ℕ) open import Data.Bool using (if_then_else_) import Data.Vec.NP as V import Function as F import Data.Product as × open F using (const; _∘′_) open V using (Vec; []; _∷_; _++_; [_]) open × using (_×_; _,_; proj₁; proj₂; uncurry) open import Data.Bits using (_→ᵇ_; 0b; 1b) open import data-universe open import flat-funs -→- : Set → Set → Set -→- A B = A → B _→ᶠ_ : ℕ → ℕ → Set _→ᶠ_ i o = Fin i → Fin o fun♭Funs : FlatFuns Set fun♭Funs = mk Set-U -→- module FunTypes = FlatFuns fun♭Funs bitsFun♭Funs : FlatFuns ℕ bitsFun♭Funs = mk Bits-U _→ᵇ_ module BitsFunTypes = FlatFuns bitsFun♭Funs finFun♭Funs : FlatFuns ℕ finFun♭Funs = mk Fin-U _→ᶠ_ module FinFunTypes = FlatFuns finFun♭Funs fun♭Ops : FlatFunsOps fun♭Funs fun♭Ops = mk F.id _∘′_ (F.const 0b) (F.const 1b) (λ { (b , x , y) → if b then x else y }) (λ { f g (b , x) → (if b then f else g) x }) _ ×.<_,_> proj₁ proj₂ (λ x → x , x) (λ f → ×.map f F.id) ×.swap (λ {((x , y) , z) → x , (y , z) }) (λ x → _ , x) proj₂ (λ f g → ×.map f g) (λ f → ×.map F.id f) (F.const []) (uncurry _∷_) V.uncons module FunOps = FlatFunsOps fun♭Ops bitsFun♭Ops : FlatFunsOps bitsFun♭Funs bitsFun♭Ops = mk id _∘_ (const [ 0b ]) (const [ 1b ]) cond forkᵇ (const []) <_,_>ᵇ fstᵇ (λ {x} → sndᵇ x) dup first (λ {x} → swap {x}) (λ {x} → assoc {x}) id id <_×_> (λ {x} → second {x}) (const []) id id where open BitsFunTypes open FunOps using (id; _∘_) fstᵇ : ∀ {A B} → A `× B `→ A fstᵇ {A} = V.take A sndᵇ : ∀ {B} A → A `× B `→ B sndᵇ A = V.drop A <_,_>ᵇ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C <_,_>ᵇ f g x = f x ++ g x forkᵇ : ∀ {A B} (f g : A `→ B) → `Bit `× A `→ B forkᵇ f g (b ∷ xs) = (if b then f else g) xs open Defaults bitsFun♭Funs open DefaultsGroup2 id _∘_ (const []) <_,_>ᵇ fstᵇ (λ {x} → sndᵇ x) open DefaultCond forkᵇ fstᵇ (λ {x} → sndᵇ x) module BitsFunOps = FlatFunsOps bitsFun♭Ops constFuns : Set → FlatFuns ⊤ constFuns A = mk ⊤-U (λ _ _ → A) module ConstFunTypes A = FlatFuns (constFuns A)
module flat-funs-implem where open import Data.Unit using (⊤) open import Data.Fin using (Fin) open import Data.Nat.NP using (ℕ) open import Data.Bool using (if_then_else_) import Data.Vec.NP as V import Function as F import Data.Product as × open F using (const; _∘′_) open V using (Vec; []; _∷_; _++_; [_]) open × using (_×_; _,_; proj₁; proj₂; uncurry) open import Data.Bits using (_→ᵇ_; 0b; 1b) open import data-universe open import flat-funs -→- : Set → Set → Set -→- A B = A → B _→ᶠ_ : ℕ → ℕ → Set _→ᶠ_ i o = Fin i → Fin o fun♭Funs : FlatFuns Set fun♭Funs = mk Set-U -→- module FunTypes = FlatFuns fun♭Funs bitsFun♭Funs : FlatFuns ℕ bitsFun♭Funs = mk Bits-U _→ᵇ_ module BitsFunTypes = FlatFuns bitsFun♭Funs finFun♭Funs : FlatFuns ℕ finFun♭Funs = mk Fin-U _→ᶠ_ module FinFunTypes = FlatFuns finFun♭Funs funLin : LinRewiring fun♭Funs funLin = mk F.id _∘′_ (λ f → ×.map f F.id) ×.swap (λ {((x , y) , z) → x , (y , z) }) (λ x → _ , x) proj₂ (λ f g → ×.map f g) (λ f → ×.map F.id f) (F.const []) _ (uncurry _∷_) V.uncons funRewiring : Rewiring fun♭Funs funRewiring = mk funLin _ (λ x → x , x) (F.const []) ×.<_,_> proj₁ proj₂ fun♭Ops : FlatFunsOps fun♭Funs fun♭Ops = mk funRewiring (F.const 0b) (F.const 1b) (λ { (b , x , y) → if b then x else y }) (λ { f g (b , x) → (if b then f else g) x }) module FunOps = FlatFunsOps fun♭Ops bitsFun♭Ops : FlatFunsOps bitsFun♭Funs bitsFun♭Ops = mk rewiring (const [ 0b ]) (const [ 1b ]) cond forkᵇ where open BitsFunTypes open FunOps using (id; _∘_) fstᵇ : ∀ {A B} → A `× B `→ A fstᵇ {A} = V.take A sndᵇ : ∀ {B} A → A `× B `→ B sndᵇ A = V.drop A <_,_>ᵇ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C <_,_>ᵇ f g x = f x ++ g x forkᵇ : ∀ {A B} (f g : A `→ B) → `Bit `× A `→ B forkᵇ f g (b ∷ xs) = (if b then f else g) xs open Defaults bitsFun♭Funs open DefaultsGroup2 id _∘_ (const []) <_,_>ᵇ fstᵇ (λ {x} → sndᵇ x) open DefaultCond forkᵇ fstᵇ (λ {x} → sndᵇ x) lin : LinRewiring bitsFun♭Funs lin = mk id _∘_ first (λ {x} → swap {x}) (λ {x} → assoc {x}) id id <_×_> (λ {x} → second {x}) id id id id rewiring : Rewiring bitsFun♭Funs rewiring = mk lin (const []) dup (const []) <_,_>ᵇ fstᵇ (λ {x} → sndᵇ x) bitsFunRewiring : Rewiring bitsFun♭Funs bitsFunRewiring = FlatFunsOps.rewiring bitsFun♭Ops bitsFunLin : LinRewiring bitsFun♭Funs bitsFunLin = Rewiring.linRewiring bitsFunRewiring module BitsFunOps = FlatFunsOps bitsFun♭Ops constFuns : Set → FlatFuns ⊤ constFuns A = mk ⊤-U (λ _ _ → A) module ConstFunTypes A = FlatFuns (constFuns A) ⊤-FunOps : FlatFunsOps (constFuns ⊤) ⊤-FunOps = _
Upgrade flat-funs-implem
Upgrade flat-funs-implem
Agda
bsd-3-clause
crypto-agda/crypto-agda
9b763493d067a9d574644c9fdbeec64fc16872f0
ZK/JSChecker/Proof.agda
ZK/JSChecker/Proof.agda
{-# OPTIONS --without-K #-} open import ZK.JSChecker using (verify-PoK-CP-EG-rnd; PoK-CP-EG-rnd; module PoK-CP-EG-rnd) open import FFI.JS.BigI using (BigI) open import Data.Product using (_,_) open import Crypto.JS.BigI.FiniteField open import Crypto.JS.BigI.CyclicGroup open import Relation.Binary.PropositionalEquality.Base using (_≡_; refl) import ZK.GroupHom.ElGamal.JS module ZK.JSChecker.Proof (p q : BigI) (pok : PoK-CP-EG-rnd ℤ[ q ] ℤ[ p ]★) where open module ^-h = ^-hom p {q} using (_^_) open module pok = PoK-CP-EG-rnd pok pf : ZK.GroupHom.ElGamal.JS.known-enc-rnd.verify-transcript p q g y (g ^ m) (α , β) (A , B) (ℤq▹BigI q c) s ≡ verify-PoK-CP-EG-rnd p q pok pf = refl
{-# OPTIONS --without-K #-} open import ZK.Types using (PoK-CP-EG-rnd; module PoK-CP-EG-rnd) open import FFI.JS.BigI using (BigI) open import Data.Product using (_,_) open import Crypto.JS.BigI.FiniteField open import Crypto.JS.BigI.CyclicGroup open import Relation.Binary.PropositionalEquality.Base using (_≡_; refl) import ZK.JSChecker import ZK.GroupHom.ElGamal.JS module ZK.JSChecker.Proof (p q : BigI) (pok : PoK-CP-EG-rnd ℤ[ q ] ℤ[ p ]★) where open module ^-h = ^-hom p {q} using (_^_) open module pok = PoK-CP-EG-rnd pok pf : ZK.GroupHom.ElGamal.JS.known-enc-rnd.verify-transcript p q g y (g ^ m) (α , β) (A , B) (ℤq▹BigI q c) s ≡ ZK.JSChecker.verify-PoK-CP-EG-rnd p q pok pf = refl
Fix imports
ZK.JSChecker.Proof: Fix imports
Agda
bsd-3-clause
crypto-agda/crypto-agda
7e41bea4c8fdf5515c5d163b86c4b01ce7e0e399
arrow.agda
arrow.agda
data Bool : Set where true : Bool false : Bool _or_ : Bool → Bool → Bool true or true = true true or false = true false or true = true false or false = false ---------------------------------------- data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false ---------------------------------------- data List (A : Set) : Set where ∘ : List A _∷_ : A → List A → List A any : {A : Set} → (A → Bool) → List A → Bool any _ ∘ = false any f (x ∷ xs) = (f x) or (any f xs) apply : {A B : Set} → (A → B) → List A → List B apply _ ∘ = ∘ apply f (x ∷ xs) = (f x) ∷ (apply f xs) _∈_ : ℕ → List ℕ → Bool x ∈ ∘ = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys _∋_ : List ℕ → ℕ → Bool xs ∋ y = y ∈ xs ---------------------------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow closure : List Arrow → List ℕ → List ℕ closure ∘ found = found closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found)) closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found)) ... | true = closure (q ∷ rest) found ... | false = closure rest found _,_⊢_ : List Arrow → List ℕ → ℕ → Bool cs , ps ⊢ q = q ∈ (closure cs ps) _⊢_ : List Arrow → Arrow → Bool cs ⊢ (⇒ q) = q ∈ (closure cs ∘) cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q ---------------------------------------- data Separation : Set where model : List ℕ → List ℕ → Separation modelsupports : Separation → List Arrow → ℕ → Bool modelsupports (model holds _) cs n = cs , holds ⊢ n modeldenies : Separation → List Arrow → ℕ → Bool modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails _,_,_¬⊨_ : List Separation → List Arrow → List ℕ → ℕ → Bool --(m ∷ ms) , cs , ps ¬⊨ n with (modelsupports m cs _,_¬⊨_ : List Separation → List Arrow → Arrow → Bool
data Bool : Set where true : Bool false : Bool _or_ : Bool → Bool → Bool true or _ = true _ or true = true false or false = false _and_ : Bool → Bool → Bool false and _ = false _ and false = false true and true = true ---------------------------------------- data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} _≡_ : ℕ → ℕ → Bool zero ≡ zero = true suc n ≡ suc m = n ≡ m _ ≡ _ = false ---------------------------------------- data List (A : Set) : Set where ∘ : List A _∷_ : A → List A → List A any : {A : Set} → (A → Bool) → List A → Bool any _ ∘ = false any f (x ∷ xs) = (f x) or (any f xs) apply : {A B : Set} → (A → B) → List A → List B apply _ ∘ = ∘ apply f (x ∷ xs) = (f x) ∷ (apply f xs) _∈_ : ℕ → List ℕ → Bool x ∈ ∘ = false x ∈ (y ∷ ys) with x ≡ y ... | true = true ... | false = x ∈ ys _∋_ : List ℕ → ℕ → Bool xs ∋ y = y ∈ xs ---------------------------------------- data Arrow : Set where ⇒_ : ℕ → Arrow _⇒_ : ℕ → Arrow → Arrow _≡≡_ : Arrow → Arrow → Bool (⇒ q) ≡≡ (⇒ s) = q ≡ s (p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s) _ ≡≡ _ = false _∈∈_ : Arrow → List Arrow → Bool x ∈∈ ∘ = false x ∈∈ (y ∷ ys) with x ≡≡ y ... | true = true ... | false = x ∈∈ ys closure : List Arrow → List ℕ → List ℕ closure ∘ found = found closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found)) closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found)) ... | true = closure (q ∷ rest) found ... | false = closure rest found _,_⊢_ : List Arrow → List ℕ → ℕ → Bool cs , ps ⊢ q = q ∈ (closure cs ps) _⊢_ : List Arrow → Arrow → Bool cs ⊢ (⇒ q) = q ∈ (closure cs ∘) cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q ---------------------------------------- data Separation : Set where model : List ℕ → List ℕ → Separation modelsupports : Separation → List Arrow → ℕ → Bool modelsupports (model holds _) cs n = cs , holds ⊢ n modeldenies : Separation → List Arrow → ℕ → Bool modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails _⟪!_⟫_ : List Arrow → Separation → Arrow → Bool cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q) _⟪_⟫_ : List Arrow → List Separation → Arrow → Bool cs ⟪ ∘ ⟫ arr = false cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr) ---------------------------------------- data Relation : Set where Proved : Relation Derivable : Relation Separated : Relation Unknown : Relation consider : List Arrow → List Separation → Arrow → Relation consider cs ms arr with (arr ∈∈ cs) ... | true = Proved ... | false with (cs ⊢ arr) ... | true = Derivable ... | false with (cs ⟪ ms ⟫ arr) ... | true = Separated ... | false = Unknown
Add classification
Add classification
Agda
mit
louisswarren/hieretikz
5e583450a3104dc6cb3ffb9b3876a297c6a9baf8
Parametric/Change/Value.agda
Parametric/Change/Value.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Change.Type as ChangeType module Parametric.Change.Value {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (ΔBase : ChangeType.Structure Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open ChangeType.Structure Base ΔBase open import Base.Denotation.Notation -- `diff` and `apply`, without validity proofs ApplyStructure : Set ApplyStructure = ∀ ι → ⟦ ι ⟧Base → ⟦ ΔBase ι ⟧Base → ⟦ ι ⟧Base DiffStructure : Set DiffStructure = ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → ⟦ ΔBase ι ⟧Base module Structure (⟦apply-base⟧ : ApplyStructure) (⟦diff-base⟧ : DiffStructure) where ⟦apply⟧ : ∀ τ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ ⟦diff⟧ : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ infixl 6 ⟦apply⟧ ⟦diff⟧ syntax ⟦apply⟧ τ v dv = v ⟦⊕₍ τ ₎⟧ dv syntax ⟦diff⟧ τ u v = u ⟦⊝₍ τ ₎⟧ v v ⟦⊕₍ base ι ₎⟧ Δv = ⟦apply-base⟧ ι v Δv f ⟦⊕₍ σ ⇒ τ ₎⟧ Δf = λ v → f v ⟦⊕₍ τ ₎⟧ Δf v (v ⟦⊝₍ σ ₎⟧ v) u ⟦⊝₍ base ι ₎⟧ v = ⟦diff-base⟧ ι u v g ⟦⊝₍ σ ⇒ τ ₎⟧ f = λ v Δv → (g (v ⟦⊕₍ σ ₎⟧ Δv)) ⟦⊝₍ τ ₎⟧ (f v) _⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ _⟦⊕⟧_ {τ} = ⟦apply⟧ τ _⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ _⟦⊝⟧_ {τ} = ⟦diff⟧ τ alternate : ∀ {Γ} → ⟦ Γ ⟧ → ⟦ mapContext ΔType Γ ⟧ → ⟦ ΔContext Γ ⟧ alternate {∅} ∅ ∅ = ∅ alternate {τ • Γ} (v • ρ) (dv • dρ) = dv • v • alternate ρ dρ
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- The values of terms in Parametric.Change.Term. ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Change.Type as ChangeType module Parametric.Change.Value {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (ΔBase : ChangeType.Structure Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open ChangeType.Structure Base ΔBase open import Base.Denotation.Notation -- Extension point 1: The value of ⊕ for base types. ApplyStructure : Set ApplyStructure = ∀ ι → ⟦ ι ⟧Base → ⟦ ΔBase ι ⟧Base → ⟦ ι ⟧Base -- Extension point 2: The value of ⊝ for base types. DiffStructure : Set DiffStructure = ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → ⟦ ΔBase ι ⟧Base module Structure (⟦apply-base⟧ : ApplyStructure) (⟦diff-base⟧ : DiffStructure) where -- We provide: The value of ⊕ and ⊝ for arbitrary types. ⟦apply⟧ : ∀ τ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ ⟦diff⟧ : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ infixl 6 ⟦apply⟧ ⟦diff⟧ syntax ⟦apply⟧ τ v dv = v ⟦⊕₍ τ ₎⟧ dv syntax ⟦diff⟧ τ u v = u ⟦⊝₍ τ ₎⟧ v v ⟦⊕₍ base ι ₎⟧ Δv = ⟦apply-base⟧ ι v Δv f ⟦⊕₍ σ ⇒ τ ₎⟧ Δf = λ v → f v ⟦⊕₍ τ ₎⟧ Δf v (v ⟦⊝₍ σ ₎⟧ v) u ⟦⊝₍ base ι ₎⟧ v = ⟦diff-base⟧ ι u v g ⟦⊝₍ σ ⇒ τ ₎⟧ f = λ v Δv → (g (v ⟦⊕₍ σ ₎⟧ Δv)) ⟦⊝₍ τ ₎⟧ (f v) _⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧ _⟦⊕⟧_ {τ} = ⟦apply⟧ τ _⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ _⟦⊝⟧_ {τ} = ⟦diff⟧ τ alternate : ∀ {Γ} → ⟦ Γ ⟧ → ⟦ mapContext ΔType Γ ⟧ → ⟦ ΔContext Γ ⟧ alternate {∅} ∅ ∅ = ∅ alternate {τ • Γ} (v • ρ) (dv • dρ) = dv • v • alternate ρ dρ
Document P.C.Value.
Document P.C.Value. Old-commit-hash: 1a3643a6990f5380e0a80e97aa83d76b6a2e2244
Agda
mit
inc-lc/ilc-agda
1d37d5dc7cf104062e0fa00000b3b1486d05ea34
Parametric/Syntax/Term.agda
Parametric/Syntax/Term.agda
import Parametric.Syntax.Type as Type import Base.Syntax.Context as Context module Parametric.Syntax.Term {Base : Set} (C : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}) where -- Terms of languages described in Plotkin style open import Function using (_∘_) open import Data.Product open Type Base open Context Type open import Syntax.Context.Plotkin Base -- Declarations of Term and Terms to enable mutual recursion data Term (Γ : Context) : (τ : Type) → Set data Terms (Γ : Context) : (Σ : Context) → Set -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where const : ∀ {Σ τ} → (c : C Σ τ) → Terms Γ Σ → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. data Terms Γ where ∅ : Terms Γ ∅ _•_ : ∀ {τ Σ} → Term Γ τ → Terms Γ Σ → Terms Γ (τ • Σ) infixr 9 _•_ -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {Δbase : Base → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ) lift-diff-apply diff apply {base ι} = diff , apply lift-diff-apply diff apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : ∀ {Δbase : Base → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) lift-diff diff apply = λ {τ Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) lift-apply : ∀ {Δbase : Base → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ) lift-apply diff apply = λ {τ Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weakenAll : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weakenAll Γ₁≼Γ₂ ∅ = ∅ weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant) -- HOAS-like smart constructors for lambdas, for different arities. abs₁ : ∀ {Γ τ₁ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → (x : Term Γ′ τ₁) → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ)) abs₁ {Γ} {τ₁} = λ f → abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) abs₂ : ∀ {Γ τ₁ τ₂ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ)) abs₂ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₁ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₃ : ∀ {Γ τ₁ τ₂ τ₃ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ)) abs₃ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₂ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₄ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ)) abs₄ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₃ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₅ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ)) abs₅ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₄ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₆ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ₆ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ₆ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ₆ ⇒ τ)) abs₆ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₅ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
import Parametric.Syntax.Type as Type import Base.Syntax.Context as Context module Parametric.Syntax.Term {Base : Set} (Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}) where -- Terms of languages described in Plotkin style open import Function using (_∘_) open import Data.Product open Type Base open Context Type open import Syntax.Context.Plotkin Base -- Declarations of Term and Terms to enable mutual recursion data Term (Γ : Context) : (τ : Type) → Set data Terms (Γ : Context) : (Σ : Context) → Set -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where const : ∀ {Σ τ} → (c : Constant Σ τ) → Terms Γ Σ → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. data Terms Γ where ∅ : Terms Γ ∅ _•_ : ∀ {τ Σ} → Term Γ τ → Terms Γ Σ → Terms Γ (τ • Σ) infixr 9 _•_ -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {Δbase : Base → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ) lift-diff-apply diff apply {base ι} = diff , apply lift-diff-apply diff apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : ∀ {Δbase : Base → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) lift-diff diff apply = λ {τ Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) lift-apply : ∀ {Δbase : Base → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ) lift-apply diff apply = λ {τ Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weakenAll : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weakenAll Γ₁≼Γ₂ ∅ = ∅ weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → Constant Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → Constant Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant) -- HOAS-like smart constructors for lambdas, for different arities. abs₁ : ∀ {Γ τ₁ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → (x : Term Γ′ τ₁) → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ)) abs₁ {Γ} {τ₁} = λ f → abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) abs₂ : ∀ {Γ τ₁ τ₂ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ)) abs₂ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₁ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₃ : ∀ {Γ τ₁ τ₂ τ₃ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ)) abs₃ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₂ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₄ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ)) abs₄ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₃ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₅ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ)) abs₅ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₄ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₆ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ₆ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ₆ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ₆ ⇒ τ)) abs₆ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₅ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
Rename C to Const.
Rename C to Const. Old-commit-hash: ed6d806588b71d4c3272db594c241e23d0c12a79
Agda
mit
inc-lc/ilc-agda
3d721fa8ec4e3e2ec1bf00e4dc87ad1cc5a00225
lib/Data/Bits.agda
lib/Data/Bits.agda
module Data.Bits where -- cleanup open import Category.Applicative open import Category.Monad open import Data.Nat.NP hiding (_≤_; _==_) open import Data.Nat.DivMod open import Data.Bool.NP hiding (_==_) open import Data.Maybe import Data.Fin as Fin open Fin using (Fin; zero; suc; #_; inject₁; inject+; raise) open import Data.Vec.NP hiding (_⊛_) renaming (map to vmap) open import Data.Vec.N-ary.NP open import Data.Unit using (⊤) open import Data.Empty using (⊥) open import Data.Product using (_×_; _,_; uncurry; proj₁; proj₂) open import Function.NP hiding (_→⟨_⟩_) import Relation.Binary.PropositionalEquality.NP as ≡ open ≡ open import Algebra.FunctionProperties import Data.List as L open import Data.Bool.NP public using (_xor_) open import Data.Vec.NP public using ([]; _∷_; head; tail; replicate) Bit : Set Bit = Bool pattern 0b = false pattern 1b = true Bits : ℕ → Set Bits = Vec Bit 0ⁿ : ∀ {n} → Bits n 0ⁿ = replicate 0b -- Warning: 0ⁿ {0} ≡ 1ⁿ {0} 1ⁿ : ∀ {n} → Bits n 1ⁿ = replicate 1b 0∷_ : ∀ {n} → Bits n → Bits (suc n) 0∷ xs = 0b ∷ xs -- can't we make these pattern aliases? 1∷_ : ∀ {n} → Bits n → Bits (suc n) 1∷ xs = 1b ∷ xs _!_ : ∀ {a n} {A : Set a} → Vec A n → Fin n → A _!_ = flip lookup _==ᵇ_ : (b₀ b₁ : Bit) → Bool b₀ ==ᵇ b₁ = not (b₀ xor b₁) _==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bool [] == [] = true (b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁ infixr 5 _⊕_ _⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n _⊕_ = zipWith _xor_ vnot : ∀ {n} → Endo (Bits n) vnot = _⊕_ 1ⁿ ⊕-assoc : ∀ {n} → Associative _≡_ (_⊕_ {n}) ⊕-assoc [] [] [] = refl ⊕-assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) rewrite ⊕-assoc xs ys zs | Xor°.+-assoc x y z = refl ⊕-comm : ∀ {n} → Commutative _≡_ (_⊕_ {n}) ⊕-comm [] [] = refl ⊕-comm (x ∷ xs) (y ∷ ys) rewrite ⊕-comm xs ys | Xor°.+-comm x y = refl ⊕-left-identity : ∀ {n} → LeftIdentity _≡_ 0ⁿ (_⊕_ {n}) ⊕-left-identity [] = refl ⊕-left-identity (x ∷ xs) rewrite ⊕-left-identity xs = refl ⊕-right-identity : ∀ {n} → RightIdentity _≡_ 0ⁿ (_⊕_ {n}) ⊕-right-identity [] = refl ⊕-right-identity (x ∷ xs) rewrite ⊕-right-identity xs | proj₂ Xor°.+-identity x = refl ⊕-≡ : ∀ {n} (x : Bits n) → x ⊕ x ≡ 0ⁿ ⊕-≡ [] = refl ⊕-≡ (x ∷ xs) rewrite ⊕-≡ xs | proj₂ Xor°.-‿inverse x = refl ⊕-≢ : ∀ {n} (x : Bits n) → x ⊕ vnot x ≡ 1ⁿ ⊕-≢ x = x ⊕ vnot x ≡⟨ refl ⟩ x ⊕ (1ⁿ ⊕ x) ≡⟨ cong (_⊕_ x) (⊕-comm 1ⁿ x) ⟩ x ⊕ (x ⊕ 1ⁿ) ≡⟨ sym (⊕-assoc x x 1ⁿ) ⟩ (x ⊕ x) ⊕ 1ⁿ ≡⟨ cong (flip _⊕_ 1ⁿ) (⊕-≡ x) ⟩ 0ⁿ ⊕ 1ⁿ ≡⟨ ⊕-left-identity 1ⁿ ⟩ 1ⁿ ∎ where open ≡-Reasoning msb : ∀ k {n} → Bits (k + n) → Bits k msb = take lsb : ∀ {n} k → Bits (n + k) → Bits k lsb {n} k rewrite ℕ°.+-comm n k = reverse ∘ msb k ∘ reverse msb₂ : ∀ {n} → Bits (2 + n) → Bits 2 msb₂ = msb 2 lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2 lsb₂ = reverse ∘ msb 2 ∘ reverse #1 : ∀ {n} → Bits n → Fin (suc n) #1 [] = zero #1 (0b ∷ bs) = inject₁ (#1 bs) #1 (1b ∷ bs) = suc (#1 bs) #0 : ∀ {n} → Bits n → Fin (suc n) #0 = #1 ∘ vmap not private module M {a} {A : Set a} {M} (appl : RawApplicative M) where open RawApplicative appl replicateM : ∀ {n} → M A → M (Vec A n) replicateM {n = zero} _ = pure [] replicateM {n = suc n} x = pure _∷_ ⊛ x ⊛ replicateM x open M public allBitsL : ∀ n → L.List (Bits n) allBitsL _ = replicateM rawIApplicative (toList (0b ∷ 1b ∷ [])) where open RawMonad L.monad allBits : ∀ n → Vec (Bits n) (2 ^ n) allBits zero = [] ∷ [] allBits (suc n) rewrite ℕ°.+-comm (2 ^ n) 0 = vmap 0∷_ bs ++ vmap 1∷_ bs where bs = allBits n #⟨_⟩ : ∀ {n} → (Bits n → Bool) → Fin (suc (2 ^ n)) #⟨ pred ⟩ = count pred (allBits _) sucBCarry : ∀ {n} → Bits n → Bits (1 + n) sucBCarry [] = 0b ∷ [] sucBCarry (0b ∷ xs) = 0b ∷ sucBCarry xs sucBCarry (1b ∷ xs) with sucBCarry xs ... | 0b ∷ bs = 0b ∷ 1b ∷ bs ... | 1b ∷ bs = 1b ∷ 0b ∷ bs sucB : ∀ {n} → Bits n → Bits n sucB = tail ∘ sucBCarry _[mod_] : ℕ → ℕ → Set a [mod b ] = DivMod' a b module ReversedBits where sucRB : ∀ {n} → Bits n → Bits n sucRB [] = [] sucRB (0b ∷ xs) = 1b ∷ xs sucRB (1b ∷ xs) = 0b ∷ sucRB xs toFin : ∀ {n} → Bits n → Fin (2 ^ n) toFin [] = zero toFin (0b ∷ xs) = inject+ _ (toFin xs) toFin {suc n} (1b ∷ xs) = raise (2 ^ n) (inject+ 0 (toFin xs)) {- toℕ : ∀ {n} → Bits n → ℕ toℕ = Fin.toℕ ∘ toFin -} toℕ : ∀ {n} → Bits n → ℕ toℕ [] = zero toℕ (0b ∷ xs) = toℕ xs toℕ {suc n} (1b ∷ xs) = 2 ^ n + toℕ xs fromℕ : ∀ {n} → ℕ → Bits n fromℕ = fold 0ⁿ sucB fromFin : ∀ {n} → Fin (2 ^ n) → Bits n fromFin = fromℕ ∘ Fin.toℕ lookupTbl : ∀ {n a} {A : Set a} → Bits n → Vec A (2 ^ n) → A lookupTbl [] (x ∷ []) = x lookupTbl (0b ∷ key) tbl = lookupTbl key (take _ tbl) lookupTbl {suc n} (1b ∷ key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl)) funFromTbl : ∀ {n a} {A : Set a} → Vec A (2 ^ n) → (Bits n → A) funFromTbl = flip lookupTbl tblFromFun : ∀ {n a} {A : Set a} → (Bits n → A) → Vec A (2 ^ n) -- tblFromFun f = tabulate (f ∘ fromFin) tblFromFun {zero} f = f [] ∷ [] tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_) ++ tblFromFun {n} (f ∘ 1∷_) ++ [] funFromTbl∘tblFromFun : ∀ {n a} {A : Set a} (fun : Bits n → A) → funFromTbl (tblFromFun fun) ≗ fun funFromTbl∘tblFromFun {zero} f [] = refl funFromTbl∘tblFromFun {suc n} f (0b ∷ xs) rewrite take-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ []) = funFromTbl∘tblFromFun {n} (f ∘ 0∷_) xs funFromTbl∘tblFromFun {suc n} f (1b ∷ xs) rewrite drop-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ []) | take-++ (2 ^ n) (tblFromFun {n} (f ∘ 1∷_)) [] = funFromTbl∘tblFromFun {n} (f ∘ 1∷_) xs tblFromFun∘funFromTbl : ∀ {n a} {A : Set a} (tbl : Vec A (2 ^ n)) → tblFromFun {n} (funFromTbl tbl) ≡ tbl tblFromFun∘funFromTbl {zero} (x ∷ []) = refl tblFromFun∘funFromTbl {suc n} tbl rewrite tblFromFun∘funFromTbl {n} (take _ tbl) | tblFromFun∘funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl)) | take-them-all (2 ^ n) (drop (2 ^ n) tbl) | take-drop-lem (2 ^ n) tbl = refl {- sucB-lem : ∀ {n} x → toℕ {2 ^ n} (sucB x) [mod 2 ^ n ] ≡ (suc (toℕ x)) [mod 2 ^ n ] sucB-lem x = {!!} -- sucB-lem : ∀ {n} x → (sucB (fromℕ x)) [mod 2 ^ n ] ≡ fromℕ ((suc x) [mod 2 ^ n ]) toℕ∘fromℕ : ∀ {n} x → toℕ (fromℕ {n} x) ≡ x toℕ∘fromℕ zero = {!!} toℕ∘fromℕ (suc x) = {!toℕ∘fromℕ x!} toℕ∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toℕ (fromFin x) ≡ Fin.toℕ x toℕ∘fromFin x = {!!} toFin∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toFin (fromFin x) ≡ x toFin∘fromFin x = {!!} -- _ᴮ : (s : String) {pf : IsBitString s} → Bits (length s) -- _ᴮ = -}
module Data.Bits where -- cleanup open import Category.Applicative open import Category.Monad open import Data.Nat.NP hiding (_≤_; _==_) open import Data.Nat.DivMod open import Data.Bool.NP hiding (_==_) open import Data.Maybe import Data.Fin as Fin open Fin using (Fin; zero; suc; #_; inject₁; inject+; raise) open import Data.Vec.NP hiding (_⊛_) renaming (map to vmap) open import Data.Vec.N-ary.NP open import Data.Unit using (⊤) open import Data.Empty using (⊥) open import Data.Product using (_×_; _,_; uncurry; proj₁; proj₂) open import Function.NP hiding (_→⟨_⟩_) import Relation.Binary.PropositionalEquality.NP as ≡ open ≡ open import Algebra.FunctionProperties import Data.List as L open import Data.Bool.NP public using (_xor_) open import Data.Vec.NP public using ([]; _∷_; head; tail; replicate) Bit : Set Bit = Bool pattern 0b = false pattern 1b = true Bits : ℕ → Set Bits = Vec Bit 0ⁿ : ∀ {n} → Bits n 0ⁿ = replicate 0b -- Warning: 0ⁿ {0} ≡ 1ⁿ {0} 1ⁿ : ∀ {n} → Bits n 1ⁿ = replicate 1b 0∷_ : ∀ {n} → Bits n → Bits (suc n) 0∷ xs = 0b ∷ xs -- can't we make these pattern aliases? 1∷_ : ∀ {n} → Bits n → Bits (suc n) 1∷ xs = 1b ∷ xs _!_ : ∀ {a n} {A : Set a} → Vec A n → Fin n → A _!_ = flip lookup _==ᵇ_ : (b₀ b₁ : Bit) → Bool b₀ ==ᵇ b₁ = not (b₀ xor b₁) _==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bool [] == [] = true (b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁ infixr 5 _⊕_ _⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n _⊕_ = zipWith _xor_ vnot : ∀ {n} → Endo (Bits n) vnot = _⊕_ 1ⁿ ⊕-assoc : ∀ {n} → Associative _≡_ (_⊕_ {n}) ⊕-assoc [] [] [] = refl ⊕-assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) rewrite ⊕-assoc xs ys zs | Xor°.+-assoc x y z = refl ⊕-comm : ∀ {n} → Commutative _≡_ (_⊕_ {n}) ⊕-comm [] [] = refl ⊕-comm (x ∷ xs) (y ∷ ys) rewrite ⊕-comm xs ys | Xor°.+-comm x y = refl ⊕-left-identity : ∀ {n} → LeftIdentity _≡_ 0ⁿ (_⊕_ {n}) ⊕-left-identity [] = refl ⊕-left-identity (x ∷ xs) rewrite ⊕-left-identity xs = refl ⊕-right-identity : ∀ {n} → RightIdentity _≡_ 0ⁿ (_⊕_ {n}) ⊕-right-identity [] = refl ⊕-right-identity (x ∷ xs) rewrite ⊕-right-identity xs | proj₂ Xor°.+-identity x = refl ⊕-≡ : ∀ {n} (x : Bits n) → x ⊕ x ≡ 0ⁿ ⊕-≡ [] = refl ⊕-≡ (x ∷ xs) rewrite ⊕-≡ xs | proj₂ Xor°.-‿inverse x = refl ⊕-≢ : ∀ {n} (x : Bits n) → x ⊕ vnot x ≡ 1ⁿ ⊕-≢ x = x ⊕ vnot x ≡⟨ refl ⟩ x ⊕ (1ⁿ ⊕ x) ≡⟨ cong (_⊕_ x) (⊕-comm 1ⁿ x) ⟩ x ⊕ (x ⊕ 1ⁿ) ≡⟨ sym (⊕-assoc x x 1ⁿ) ⟩ (x ⊕ x) ⊕ 1ⁿ ≡⟨ cong (flip _⊕_ 1ⁿ) (⊕-≡ x) ⟩ 0ⁿ ⊕ 1ⁿ ≡⟨ ⊕-left-identity 1ⁿ ⟩ 1ⁿ ∎ where open ≡-Reasoning msb : ∀ k {n} → Bits (k + n) → Bits k msb = take lsb : ∀ {n} k → Bits (n + k) → Bits k lsb {n} k rewrite ℕ°.+-comm n k = reverse ∘ msb k ∘ reverse msb₂ : ∀ {n} → Bits (2 + n) → Bits 2 msb₂ = msb 2 lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2 lsb₂ = reverse ∘ msb 2 ∘ reverse #1 : ∀ {n} → Bits n → Fin (suc n) #1 [] = zero #1 (0b ∷ bs) = inject₁ (#1 bs) #1 (1b ∷ bs) = suc (#1 bs) #0 : ∀ {n} → Bits n → Fin (suc n) #0 = #1 ∘ vmap not private module M {a} {A : Set a} {M : Set a → Set a} (appl : RawApplicative M) where open RawApplicative appl replicateM : ∀ {n} → M A → M (Vec A n) replicateM {n = zero} _ = pure [] replicateM {n = suc n} x = pure _∷_ ⊛ x ⊛ replicateM x open M public allBitsL : ∀ n → L.List (Bits n) allBitsL _ = replicateM rawIApplicative (toList (0b ∷ 1b ∷ [])) where open RawMonad L.monad allBits : ∀ n → Vec (Bits n) (2 ^ n) allBits zero = [] ∷ [] allBits (suc n) rewrite ℕ°.+-comm (2 ^ n) 0 = vmap 0∷_ bs ++ vmap 1∷_ bs where bs = allBits n #⟨_⟩ : ∀ {n} → (Bits n → Bool) → Fin (suc (2 ^ n)) #⟨ pred ⟩ = count pred (allBits _) sucBCarry : ∀ {n} → Bits n → Bits (1 + n) sucBCarry [] = 0b ∷ [] sucBCarry (0b ∷ xs) = 0b ∷ sucBCarry xs sucBCarry (1b ∷ xs) with sucBCarry xs ... | 0b ∷ bs = 0b ∷ 1b ∷ bs ... | 1b ∷ bs = 1b ∷ 0b ∷ bs sucB : ∀ {n} → Bits n → Bits n sucB = tail ∘ sucBCarry _[mod_] : ℕ → ℕ → Set a [mod b ] = DivMod' a b module ReversedBits where sucRB : ∀ {n} → Bits n → Bits n sucRB [] = [] sucRB (0b ∷ xs) = 1b ∷ xs sucRB (1b ∷ xs) = 0b ∷ sucRB xs toFin : ∀ {n} → Bits n → Fin (2 ^ n) toFin [] = zero toFin (0b ∷ xs) = inject+ _ (toFin xs) toFin {suc n} (1b ∷ xs) = raise (2 ^ n) (inject+ 0 (toFin xs)) {- toℕ : ∀ {n} → Bits n → ℕ toℕ = Fin.toℕ ∘ toFin -} toℕ : ∀ {n} → Bits n → ℕ toℕ [] = zero toℕ (0b ∷ xs) = toℕ xs toℕ {suc n} (1b ∷ xs) = 2 ^ n + toℕ xs fromℕ : ∀ {n} → ℕ → Bits n fromℕ = fold 0ⁿ sucB fromFin : ∀ {n} → Fin (2 ^ n) → Bits n fromFin = fromℕ ∘ Fin.toℕ lookupTbl : ∀ {n a} {A : Set a} → Bits n → Vec A (2 ^ n) → A lookupTbl [] (x ∷ []) = x lookupTbl (0b ∷ key) tbl = lookupTbl key (take _ tbl) lookupTbl {suc n} (1b ∷ key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl)) funFromTbl : ∀ {n a} {A : Set a} → Vec A (2 ^ n) → (Bits n → A) funFromTbl = flip lookupTbl tblFromFun : ∀ {n a} {A : Set a} → (Bits n → A) → Vec A (2 ^ n) -- tblFromFun f = tabulate (f ∘ fromFin) tblFromFun {zero} f = f [] ∷ [] tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_) ++ tblFromFun {n} (f ∘ 1∷_) ++ [] funFromTbl∘tblFromFun : ∀ {n a} {A : Set a} (fun : Bits n → A) → funFromTbl (tblFromFun fun) ≗ fun funFromTbl∘tblFromFun {zero} f [] = refl funFromTbl∘tblFromFun {suc n} f (0b ∷ xs) rewrite take-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ []) = funFromTbl∘tblFromFun {n} (f ∘ 0∷_) xs funFromTbl∘tblFromFun {suc n} f (1b ∷ xs) rewrite drop-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ []) | take-++ (2 ^ n) (tblFromFun {n} (f ∘ 1∷_)) [] = funFromTbl∘tblFromFun {n} (f ∘ 1∷_) xs tblFromFun∘funFromTbl : ∀ {n a} {A : Set a} (tbl : Vec A (2 ^ n)) → tblFromFun {n} (funFromTbl tbl) ≡ tbl tblFromFun∘funFromTbl {zero} (x ∷ []) = refl tblFromFun∘funFromTbl {suc n} tbl rewrite tblFromFun∘funFromTbl {n} (take _ tbl) | tblFromFun∘funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl)) | take-them-all (2 ^ n) (drop (2 ^ n) tbl) | take-drop-lem (2 ^ n) tbl = refl {- sucB-lem : ∀ {n} x → toℕ {2 ^ n} (sucB x) [mod 2 ^ n ] ≡ (suc (toℕ x)) [mod 2 ^ n ] sucB-lem x = {!!} -- sucB-lem : ∀ {n} x → (sucB (fromℕ x)) [mod 2 ^ n ] ≡ fromℕ ((suc x) [mod 2 ^ n ]) toℕ∘fromℕ : ∀ {n} x → toℕ (fromℕ {n} x) ≡ x toℕ∘fromℕ zero = {!!} toℕ∘fromℕ (suc x) = {!toℕ∘fromℕ x!} toℕ∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toℕ (fromFin x) ≡ Fin.toℕ x toℕ∘fromFin x = {!!} toFin∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toFin (fromFin x) ≡ x toFin∘fromFin x = {!!} -- _ᴮ : (s : String) {pf : IsBitString s} → Bits (length s) -- _ᴮ = -}
Update to stdlib
Update to stdlib
Agda
bsd-3-clause
crypto-agda/agda-nplib
4c58b009165ce674a87c1d45177df0c30d306d41
lib/Explore/Summable.agda
lib/Explore/Summable.agda
module Explore.Summable where open import Type open import Function.NP import Relation.Binary.PropositionalEquality.NP as ≡ open ≡ using (_≡_ ; _≗_ ; _≗₂_) open import Explore.Type open import Explore.Product open import Data.Product open import Data.Nat.NP open import Data.Nat.Properties open import Data.Bool.NP renaming (Bool to 𝟚; true to 1b; false to 0b; toℕ to 𝟚▹ℕ) open Data.Bool.NP.Indexed module FromSum {A : ★} (sum : Sum A) where Card : ℕ Card = sum (const 1) count : Count A count f = sum (𝟚▹ℕ ∘ f) module FromSumInd {A : ★} {sum : Sum A} (sum-ind : SumInd sum) where open FromSum sum public sum-ext : SumExt sum sum-ext = sum-ind (λ s → s _ ≡ s _) (≡.cong₂ _+_) sum-zero : SumZero sum sum-zero = sum-ind (λ s → s (const 0) ≡ 0) (≡.cong₂ _+_) (λ _ → ≡.refl) sum-hom : SumHom sum sum-hom f g = sum-ind (λ s → s (f +° g) ≡ s f + s g) (λ {s₀} {s₁} p₀ p₁ → ≡.trans (≡.cong₂ _+_ p₀ p₁) (+-interchange (s₀ _) (s₀ _) _ _)) (λ _ → ≡.refl) sum-mono : SumMono sum sum-mono = sum-ind (λ s → s _ ≤ s _) _+-mono_ sum-lin : SumLin sum sum-lin f zero = sum-zero sum-lin f (suc k) = ≡.trans (sum-hom f (λ x → k * f x)) (≡.cong₂ _+_ (≡.refl {x = sum f}) (sum-lin f k)) module _ (f g : A → ℕ) where open ≡.≡-Reasoning sum-⊓-∸ : sum f ≡ sum (f ⊓° g) + sum (f ∸° g) sum-⊓-∸ = sum f ≡⟨ sum-ext (f ⟨ a≡a⊓b+a∸b ⟩° g) ⟩ sum ((f ⊓° g) +° (f ∸° g)) ≡⟨ sum-hom (f ⊓° g) (f ∸° g) ⟩ sum (f ⊓° g) + sum (f ∸° g) ∎ sum-⊔-⊓ : sum f + sum g ≡ sum (f ⊔° g) + sum (f ⊓° g) sum-⊔-⊓ = sum f + sum g ≡⟨ ≡.sym (sum-hom f g) ⟩ sum (f +° g) ≡⟨ sum-ext (f ⟨ a+b≡a⊔b+a⊓b ⟩° g) ⟩ sum (f ⊔° g +° f ⊓° g) ≡⟨ sum-hom (f ⊔° g) (f ⊓° g) ⟩ sum (f ⊔° g) + sum (f ⊓° g) ∎ sum-⊔ : sum (f ⊔° g) ≤ sum f + sum g sum-⊔ = ℕ≤.trans (sum-mono (f ⟨ ⊔≤+ ⟩° g)) (ℕ≤.reflexive (sum-hom f g)) count-ext : CountExt count count-ext f≗g = sum-ext (≡.cong 𝟚▹ℕ ∘ f≗g) sum-const : ∀ k → sum (const k) ≡ Card * k sum-const k rewrite ℕ°.*-comm Card k | ≡.sym (sum-lin (const 1) k) | proj₂ ℕ°.*-identity k = ≡.refl module _ f g where count-∧-not : count f ≡ count (f ∧° g) + count (f ∧° not° g) count-∧-not rewrite sum-⊓-∸ (𝟚▹ℕ ∘ f) (𝟚▹ℕ ∘ g) | sum-ext (f ⟨ toℕ-⊓ ⟩° g) | sum-ext (f ⟨ toℕ-∸ ⟩° g) = ≡.refl count-∨-∧ : count f + count g ≡ count (f ∨° g) + count (f ∧° g) count-∨-∧ rewrite sum-⊔-⊓ (𝟚▹ℕ ∘ f) (𝟚▹ℕ ∘ g) | sum-ext (f ⟨ toℕ-⊔ ⟩° g) | sum-ext (f ⟨ toℕ-⊓ ⟩° g) = ≡.refl count-∨≤+ : count (f ∨° g) ≤ count f + count g count-∨≤+ = ℕ≤.trans (ℕ≤.reflexive (sum-ext (≡.sym ∘ (f ⟨ toℕ-⊔ ⟩° g)))) (sum-⊔ (𝟚▹ℕ ∘ f) (𝟚▹ℕ ∘ g)) module FromSum× {A B} {sumᴬ : Sum A} (sum-indᴬ : SumInd sumᴬ) {sumᴮ : Sum B} (sum-indᴮ : SumInd sumᴮ) where module |A| = FromSumInd sum-indᴬ module |B| = FromSumInd sum-indᴮ sumᴬᴮ = sumᴬ ×-sum sumᴮ sum-∘proj₁≡Card* : ∀ f → sumᴬᴮ (f ∘ proj₁) ≡ |B|.Card * sumᴬ f sum-∘proj₁≡Card* f rewrite |A|.sum-ext (|B|.sum-const ∘ f) = |A|.sum-lin f |B|.Card sum-∘proj₂≡Card* : ∀ f → sumᴬᴮ (f ∘ proj₂) ≡ |A|.Card * sumᴮ f sum-∘proj₂≡Card* = |A|.sum-const ∘ sumᴮ sum-∘proj₁ : ∀ {f} {g} → sumᴬ f ≡ sumᴬ g → sumᴬᴮ (f ∘ proj₁) ≡ sumᴬᴮ (g ∘ proj₁) sum-∘proj₁ {f} {g} sumf≡sumg rewrite sum-∘proj₁≡Card* f | sum-∘proj₁≡Card* g | sumf≡sumg = ≡.refl sum-∘proj₂ : ∀ {f} {g} → sumᴮ f ≡ sumᴮ g → sumᴬᴮ (f ∘ proj₂) ≡ sumᴬᴮ (g ∘ proj₂) sum-∘proj₂ sumf≡sumg = |A|.sum-ext (const sumf≡sumg) -- -} -- -} -- -} -- -}
module Explore.Summable where open import Type open import Function.NP import Relation.Binary.PropositionalEquality.NP as ≡ open ≡ using (_≡_ ; _≗_ ; _≗₂_) open import Explore.Type open import Explore.Product open import Data.Product open import Data.Nat.NP open import Data.Nat.Properties open import Data.Bool.NP renaming (Bool to 𝟚; true to 1b; false to 0b; toℕ to 𝟚▹ℕ) open Data.Bool.NP.Indexed module FromSum {A : ★} (sum : Sum A) where Card : ℕ Card = sum (const 1) count : Count A count f = sum (𝟚▹ℕ ∘ f) module FromSumInd {A : ★} {sum : Sum A} (sum-ind : SumInd sum) where open FromSum sum public sum-ext : SumExt sum sum-ext = sum-ind (λ s → s _ ≡ s _) (≡.cong₂ _+_) sum-zero : SumZero sum sum-zero = sum-ind (λ s → s (const 0) ≡ 0) (≡.cong₂ _+_) (λ _ → ≡.refl) sum-hom : SumHom sum sum-hom f g = sum-ind (λ s → s (f +° g) ≡ s f + s g) (λ {s₀} {s₁} p₀ p₁ → ≡.trans (≡.cong₂ _+_ p₀ p₁) (+-interchange (s₀ _) (s₀ _) _ _)) (λ _ → ≡.refl) sum-mono : SumMono sum sum-mono = sum-ind (λ s → s _ ≤ s _) _+-mono_ sum-lin : SumLin sum sum-lin f zero = sum-zero sum-lin f (suc k) = ≡.trans (sum-hom f (λ x → k * f x)) (≡.cong₂ _+_ (≡.refl {x = sum f}) (sum-lin f k)) module _ (f g : A → ℕ) where open ≡.≡-Reasoning sum-⊓-∸ : sum f ≡ sum (f ⊓° g) + sum (f ∸° g) sum-⊓-∸ = sum f ≡⟨ sum-ext (f ⟨ a≡a⊓b+a∸b ⟩° g) ⟩ sum ((f ⊓° g) +° (f ∸° g)) ≡⟨ sum-hom (f ⊓° g) (f ∸° g) ⟩ sum (f ⊓° g) + sum (f ∸° g) ∎ sum-⊔-⊓ : sum f + sum g ≡ sum (f ⊔° g) + sum (f ⊓° g) sum-⊔-⊓ = sum f + sum g ≡⟨ ≡.sym (sum-hom f g) ⟩ sum (f +° g) ≡⟨ sum-ext (f ⟨ a+b≡a⊔b+a⊓b ⟩° g) ⟩ sum (f ⊔° g +° f ⊓° g) ≡⟨ sum-hom (f ⊔° g) (f ⊓° g) ⟩ sum (f ⊔° g) + sum (f ⊓° g) ∎ sum-⊔ : sum (f ⊔° g) ≤ sum f + sum g sum-⊔ = ℕ≤.trans (sum-mono (f ⟨ ⊔≤+ ⟩° g)) (ℕ≤.reflexive (sum-hom f g)) count-ext : CountExt count count-ext f≗g = sum-ext (≡.cong 𝟚▹ℕ ∘ f≗g) sum-const : ∀ k → sum (const k) ≡ Card * k sum-const k rewrite ℕ°.*-comm Card k | ≡.sym (sum-lin (const 1) k) | proj₂ ℕ°.*-identity k = ≡.refl module _ f g where count-∧-not : count f ≡ count (f ∧° g) + count (f ∧° not° g) count-∧-not rewrite sum-⊓-∸ (𝟚▹ℕ ∘ f) (𝟚▹ℕ ∘ g) | sum-ext (f ⟨ toℕ-⊓ ⟩° g) | sum-ext (f ⟨ toℕ-∸ ⟩° g) = ≡.refl count-∨-∧ : count f + count g ≡ count (f ∨° g) + count (f ∧° g) count-∨-∧ rewrite sum-⊔-⊓ (𝟚▹ℕ ∘ f) (𝟚▹ℕ ∘ g) | sum-ext (f ⟨ toℕ-⊔ ⟩° g) | sum-ext (f ⟨ toℕ-⊓ ⟩° g) = ≡.refl count-∨≤+ : count (f ∨° g) ≤ count f + count g count-∨≤+ = ℕ≤.trans (ℕ≤.reflexive (sum-ext (≡.sym ∘ (f ⟨ toℕ-⊔ ⟩° g)))) (sum-⊔ (𝟚▹ℕ ∘ f) (𝟚▹ℕ ∘ g)) module FromSum× {A B} {sumᴬ : Sum A} (sum-indᴬ : SumInd sumᴬ) {sumᴮ : Sum B} (sum-indᴮ : SumInd sumᴮ) where module |A| = FromSumInd sum-indᴬ module |B| = FromSumInd sum-indᴮ open Operators sumᴬᴮ = sumᴬ ×ˢ sumᴮ sum-∘proj₁≡Card* : ∀ f → sumᴬᴮ (f ∘ proj₁) ≡ |B|.Card * sumᴬ f sum-∘proj₁≡Card* f rewrite |A|.sum-ext (|B|.sum-const ∘ f) = |A|.sum-lin f |B|.Card sum-∘proj₂≡Card* : ∀ f → sumᴬᴮ (f ∘ proj₂) ≡ |A|.Card * sumᴮ f sum-∘proj₂≡Card* = |A|.sum-const ∘ sumᴮ sum-∘proj₁ : ∀ {f} {g} → sumᴬ f ≡ sumᴬ g → sumᴬᴮ (f ∘ proj₁) ≡ sumᴬᴮ (g ∘ proj₁) sum-∘proj₁ {f} {g} sumf≡sumg rewrite sum-∘proj₁≡Card* f | sum-∘proj₁≡Card* g | sumf≡sumg = ≡.refl sum-∘proj₂ : ∀ {f} {g} → sumᴮ f ≡ sumᴮ g → sumᴬᴮ (f ∘ proj₂) ≡ sumᴬᴮ (g ∘ proj₂) sum-∘proj₂ sumf≡sumg = |A|.sum-ext (const sumf≡sumg) -- -} -- -} -- -} -- -}
Make Explore.Summable typecheck
Make Explore.Summable typecheck
Agda
bsd-3-clause
crypto-agda/explore
818dfccaa5b7e626b635720fbf80318263d4c8d9
New/Changes.agda
New/Changes.agda
module New.Changes where open import Relation.Binary.PropositionalEquality open import Level open import Data.Unit open import Data.Product record IsChAlg {ℓ : Level} (A : Set ℓ) (Ch : Set ℓ) : Set (suc ℓ) where field _⊕_ : A → Ch → A _⊝_ : A → A → Ch valid : A → Ch → Set ℓ ⊝-valid : ∀ (a b : A) → valid a (b ⊝ a) ⊕-⊝ : (b a : A) → a ⊕ (b ⊝ a) ≡ b infixl 6 _⊕_ _⊝_ nil : A → Ch nil a = a ⊝ a nil-valid : (a : A) → valid a (nil a) nil-valid a = ⊝-valid a a Δ : A → Set ℓ Δ a = Σ[ da ∈ Ch ] (valid a da) update-diff = ⊕-⊝ update-nil : (a : A) → a ⊕ nil a ≡ a update-nil a = update-diff a a record ChAlg {ℓ : Level} (A : Set ℓ) : Set (suc ℓ) where field Ch : Set ℓ isChAlg : IsChAlg A Ch open IsChAlg isChAlg public open ChAlg {{...}} public hiding (Ch) Ch : ∀ {ℓ} (A : Set ℓ) → {{CA : ChAlg A}} → Set ℓ Ch A {{CA}} = ChAlg.Ch CA -- Huge hack, but it does gives the output you want to see in all cases I've seen. {-# DISPLAY IsChAlg.valid x = valid #-} {-# DISPLAY ChAlg.valid x = valid #-} {-# DISPLAY IsChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY ChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY IsChAlg.nil x = nil #-} {-# DISPLAY ChAlg.nil x = nil #-} {-# DISPLAY IsChAlg._⊝_ x = _⊝_ #-} {-# DISPLAY ChAlg._⊝_ x = _⊝_ #-} module _ {ℓ₁} {ℓ₂} {A : Set ℓ₁} {B : Set ℓ₂} {{CA : ChAlg A}} {{CB : ChAlg B}} where private fCh = A → Ch A → Ch B _f⊕_ : (A → B) → fCh → A → B _f⊕_ = λ f df a → f a ⊕ df a (nil a) _f⊝_ : (g f : A → B) → fCh _f⊝_ = λ g f a da → g (a ⊕ da) ⊝ f a open ≡-Reasoning open import Postulate.Extensionality IsDerivative : ∀ (f : A → B) → (df : fCh) → Set (ℓ₁ ⊔ ℓ₂) IsDerivative f df = ∀ a da (v : valid a da) → f (a ⊕ da) ≡ f a ⊕ df a da instance funCA : ChAlg (A → B) private funUpdateDiff : ∀ g f a → (f f⊕ (g f⊝ f)) a ≡ g a funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a) funCA = record { Ch = A → Ch A → Ch B ; isChAlg = record { _⊕_ = _f⊕_ ; _⊝_ = _f⊝_ ; valid = λ f df → ∀ a da (v : valid a da) → valid (f a) (df a da) × (f f⊕ df) (a ⊕ da) ≡ f a ⊕ df a da ; ⊝-valid = λ f g a da (v : valid a da) → ⊝-valid (f a) (g (a ⊕ da)) , ( begin f (a ⊕ da) ⊕ (g (a ⊕ da ⊕ nil (a ⊕ da)) ⊝ f (a ⊕ da)) ≡⟨ cong (λ □ → f (a ⊕ da) ⊕ (g □ ⊝ f (a ⊕ da))) (update-nil (a ⊕ da)) ⟩ f (a ⊕ da) ⊕ (g (a ⊕ da) ⊝ f (a ⊕ da)) ≡⟨ update-diff (g (a ⊕ da)) (f (a ⊕ da)) ⟩ g (a ⊕ da) ≡⟨ sym (update-diff (g (a ⊕ da)) (f a)) ⟩ f a ⊕ (g (a ⊕ da) ⊝ f a) ∎) ; ⊕-⊝ = λ g f → ext (funUpdateDiff g f) } } nil-is-derivative : ∀ (f : A → B) → IsDerivative f (nil f) nil-is-derivative f a da v = begin f (a ⊕ da) ≡⟨ sym (cong (λ □ → □ (_⊕_ a da)) (update-nil f)) ⟩ (f ⊕ nil f) (a ⊕ da) ≡⟨ proj₂ (nil-valid f a da v) ⟩ f a ⊕ (nil f a da) ∎ -- instance -- pairCA : ChAlg (A × B) -- pairCA = record -- { Ch = Ch CA × Ch CB ; isChAlg = record { _⊕_ = {!!} ; _⊝_ = {!!} ; valid = {!!} ; ⊝-valid = {!!} ; ⊕-⊝ = {!!} } } open import Data.Integer open import Theorem.Groups-Nehemiah instance intCA : ChAlg ℤ intCA = record { Ch = ℤ ; isChAlg = record { _⊕_ = _+_ ; _⊝_ = _-_ ; valid = λ a b → ⊤ ; ⊝-valid = λ a b → tt ; ⊕-⊝ = λ b a → n+[m-n]=m {a} {b} } }
module New.Changes where open import Relation.Binary.PropositionalEquality open import Level open import Data.Unit open import Data.Product record IsChAlg {ℓ : Level} (A : Set ℓ) (Ch : Set ℓ) : Set (suc ℓ) where field _⊕_ : A → Ch → A _⊝_ : A → A → Ch valid : A → Ch → Set ℓ ⊝-valid : ∀ (a b : A) → valid a (b ⊝ a) ⊕-⊝ : (b a : A) → a ⊕ (b ⊝ a) ≡ b infixl 6 _⊕_ _⊝_ nil : A → Ch nil a = a ⊝ a nil-valid : (a : A) → valid a (nil a) nil-valid a = ⊝-valid a a Δ : A → Set ℓ Δ a = Σ[ da ∈ Ch ] (valid a da) update-diff = ⊕-⊝ update-nil : (a : A) → a ⊕ nil a ≡ a update-nil a = update-diff a a record ChAlg {ℓ : Level} (A : Set ℓ) : Set (suc ℓ) where field Ch : Set ℓ isChAlg : IsChAlg A Ch open IsChAlg isChAlg public open ChAlg {{...}} public hiding (Ch) Ch : ∀ {ℓ} (A : Set ℓ) → {{CA : ChAlg A}} → Set ℓ Ch A {{CA}} = ChAlg.Ch CA -- Huge hack, but it does gives the output you want to see in all cases I've seen. {-# DISPLAY IsChAlg.valid x = valid #-} {-# DISPLAY ChAlg.valid x = valid #-} {-# DISPLAY IsChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY ChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY IsChAlg.nil x = nil #-} {-# DISPLAY ChAlg.nil x = nil #-} {-# DISPLAY IsChAlg._⊝_ x = _⊝_ #-} {-# DISPLAY ChAlg._⊝_ x = _⊝_ #-} module _ {ℓ₁} {ℓ₂} {A : Set ℓ₁} {B : Set ℓ₂} {{CA : ChAlg A}} {{CB : ChAlg B}} where private fCh = A → Ch A → Ch B _f⊕_ : (A → B) → fCh → A → B _f⊕_ = λ f df a → f a ⊕ df a (nil a) _f⊝_ : (g f : A → B) → fCh _f⊝_ = λ g f a da → g (a ⊕ da) ⊝ f a open ≡-Reasoning open import Postulate.Extensionality IsDerivative : ∀ (f : A → B) → (df : fCh) → Set (ℓ₁ ⊔ ℓ₂) IsDerivative f df = ∀ a da (v : valid a da) → f (a ⊕ da) ≡ f a ⊕ df a da instance funCA : ChAlg (A → B) private funUpdateDiff : ∀ g f a → (f f⊕ (g f⊝ f)) a ≡ g a funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a) funCA = record { Ch = A → Ch A → Ch B ; isChAlg = record { _⊕_ = _f⊕_ ; _⊝_ = _f⊝_ ; valid = λ f df → ∀ a da (v : valid a da) → valid (f a) (df a da) × (f f⊕ df) (a ⊕ da) ≡ f a ⊕ df a da ; ⊝-valid = λ f g a da (v : valid a da) → ⊝-valid (f a) (g (a ⊕ da)) , ( begin f (a ⊕ da) ⊕ (g (a ⊕ da ⊕ nil (a ⊕ da)) ⊝ f (a ⊕ da)) ≡⟨ cong (λ □ → f (a ⊕ da) ⊕ (g □ ⊝ f (a ⊕ da))) (update-nil (a ⊕ da)) ⟩ f (a ⊕ da) ⊕ (g (a ⊕ da) ⊝ f (a ⊕ da)) ≡⟨ update-diff (g (a ⊕ da)) (f (a ⊕ da)) ⟩ g (a ⊕ da) ≡⟨ sym (update-diff (g (a ⊕ da)) (f a)) ⟩ f a ⊕ (g (a ⊕ da) ⊝ f a) ∎) ; ⊕-⊝ = λ g f → ext (funUpdateDiff g f) } } nil-is-derivative : ∀ (f : A → B) → IsDerivative f (nil f) nil-is-derivative f a da v = begin f (a ⊕ da) ≡⟨ sym (cong (λ □ → □ (_⊕_ a da)) (update-nil f)) ⟩ (f ⊕ nil f) (a ⊕ da) ≡⟨ proj₂ (nil-valid f a da v) ⟩ f a ⊕ (nil f a da) ∎ private _p⊕_ : A × B → Ch A × Ch B → A × B _p⊕_ (a , b) (da , db) = a ⊕ da , b ⊕ db _p⊝_ : A × B → A × B → Ch A × Ch B _p⊝_ (a2 , b2) (a1 , b1) = a2 ⊝ a1 , b2 ⊝ b1 pvalid : A × B → Ch A × Ch B → Set (ℓ₂ ⊔ ℓ₁) pvalid (a , b) (da , db) = valid a da × valid b db p⊝-valid : (p1 p2 : A × B) → pvalid p1 (p2 p⊝ p1) p⊝-valid (a1 , b1) (a2 , b2) = ⊝-valid a1 a2 , ⊝-valid b1 b2 p⊕-⊝ : (p2 p1 : A × B) → p1 p⊕ (p2 p⊝ p1) ≡ p2 p⊕-⊝ (a2 , b2) (a1 , b1) = cong₂ _,_ (⊕-⊝ a2 a1) (⊕-⊝ b2 b1) instance pairCA : ChAlg (A × B) pairCA = record { Ch = Ch A × Ch B ; isChAlg = record { _⊕_ = _p⊕_ ; _⊝_ = _p⊝_ ; valid = pvalid ; ⊝-valid = p⊝-valid ; ⊕-⊝ = p⊕-⊝ } } open import Data.Integer open import Theorem.Groups-Nehemiah instance intCA : ChAlg ℤ intCA = record { Ch = ℤ ; isChAlg = record { _⊕_ = _+_ ; _⊝_ = _-_ ; valid = λ a b → ⊤ ; ⊝-valid = λ a b → tt ; ⊕-⊝ = λ b a → n+[m-n]=m {a} {b} } }
Change strucure for products
Change strucure for products
Agda
mit
inc-lc/ilc-agda
da063111bdead7bc9476b14520d957b8057cba93
formalization/agda/Spire/Operational.agda
formalization/agda/Spire/Operational.agda
module Spire.Operational where ---------------------------------------------------------------------- data Level : Set where zero : Level suc : Level → Level ---------------------------------------------------------------------- data Context : Set data Type (Γ : Context) : Set data Value (Γ : Context) : Type Γ → Set data Neutral (Γ : Context) : Type Γ → Set ---------------------------------------------------------------------- data Context where ∅ : Context _,_ : (Γ : Context) → Type Γ → Context data Type Γ where `⊥ `⊤ `Bool : Type Γ `Desc `Type : (ℓ : Level) → Type Γ `Π `Σ : (A : Type Γ) (B : Type (Γ , A)) → Type Γ `⟦_⟧ : ∀{ℓ} → Neutral Γ (`Type ℓ) → Type Γ ---------------------------------------------------------------------- ⟦_⟧ : ∀{Γ ℓ} → Value Γ (`Type ℓ) → Type Γ postulate wknT : ∀{Γ A} → Type Γ → Type (Γ , A) subT : ∀{Γ A} → Type (Γ , A) → Value Γ A → Type Γ subV : ∀{Γ A B} → Value (Γ , A) B → (x : Value Γ A) → Value Γ (subT B x) data Var : (Γ : Context) (A : Type Γ) → Set where here : ∀{Γ A} → Var (Γ , A) (wknT A) there : ∀{Γ A B} → Var Γ A → Var (Γ , B) (wknT A) ---------------------------------------------------------------------- data Value Γ where {- Type introduction -} `⊥ `⊤ `Bool `Desc `Type : ∀{ℓ} → Value Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (suc ℓ)) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Value Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Desc (suc ℓ))) → Value Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Value Γ `⊤ `true `false : Value Γ `Bool _`,_ : ∀{A B} (a : Value Γ A) (b : Value Γ (subT B a)) → Value Γ (`Σ A B) `λ : ∀{A B} → Value (Γ , A) B → Value Γ (`Π A B) `neut : ∀{A} → Neutral Γ A → Value Γ A ---------------------------------------------------------------------- data Neutral Γ where {- Value elimination -} `var : ∀{A} → Var Γ A → Neutral Γ A `if_`then_`else_ : ∀{C} (b : Neutral Γ `Bool) (c₁ c₂ : Value Γ C) → Neutral Γ C `elim⊥ : ∀{A} → Neutral Γ `⊥ → Neutral Γ A `elimBool : ∀{ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Neutral Γ `Bool) → Neutral Γ (subT ⟦ P ⟧ (`neut b)) `proj₁ : ∀{A B} → Neutral Γ (`Σ A B) → Neutral Γ A `proj₂ : ∀{A B} (ab : Neutral Γ (`Σ A B)) → Neutral Γ (subT B (`neut (`proj₁ ab))) _`$_ : ∀{A B} (f : Neutral Γ (`Π A B)) (a : Value Γ A) → Neutral Γ (subT B a) ---------------------------------------------------------------------- ⟦ `Π A B ⟧ = `Π ⟦ A ⟧ ⟦ B ⟧ ⟦ `Σ A B ⟧ = `Σ ⟦ A ⟧ ⟦ B ⟧ ⟦ `⊥ ⟧ = `⊥ ⟦ `⊤ ⟧ = `⊤ ⟦ `Bool ⟧ = `Bool ⟦ `Type {zero} ⟧ = `⊥ ⟦ `Type {suc ℓ} ⟧ = `Type ℓ ⟦ `⟦ A ⟧ ⟧ = ⟦ A ⟧ ⟦ `Desc {ℓ} ⟧ = `Desc ℓ ⟦ `neut A ⟧ = `⟦ A ⟧ ---------------------------------------------------------------------- elim⊥ : ∀{Γ A} → Value Γ `⊥ → Value Γ A elim⊥ (`neut bot) = `neut (`elim⊥ bot) ---------------------------------------------------------------------- if_then_else_ : ∀{Γ C} (b : Value Γ `Bool) (c₁ c₂ : Value Γ C) → Value Γ C if `true then c₁ else c₂ = c₁ if `false then c₁ else c₂ = c₂ if `neut b then c₁ else c₂ = `neut (`if b `then c₁ `else c₂) elimBool : ∀{Γ ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Value Γ `Bool) → Value Γ (subT ⟦ P ⟧ b) elimBool P pt pf `true = pt elimBool P pt pf `false = pf elimBool P pt pf (`neut b) = `neut (`elimBool P pt pf b) ---------------------------------------------------------------------- proj₁ : ∀{Γ A B} → Value Γ (`Σ A B) → Value Γ A proj₁ (a `, b) = a proj₁ (`neut ab) = `neut (`proj₁ ab) proj₂ : ∀{Γ A B} (ab : Value Γ (`Σ A B)) → Value Γ (subT B (proj₁ ab)) proj₂ (a `, b) = b proj₂ (`neut ab) = `neut (`proj₂ ab) ---------------------------------------------------------------------- _$_ : ∀{Γ A B} → Value Γ (`Π A B) → (a : Value Γ A) → Value Γ (subT B a) `λ b $ a = subV b a `neut f $ a = `neut (f `$ a) ---------------------------------------------------------------------- data Term (Γ : Context) : Type Γ → Set eval : ∀{Γ A} → Term Γ A → Value Γ A data Term Γ where {- Type introduction -} `⊥ `⊤ `Bool `Type : ∀{ℓ} → Term Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (B : Term (Γ , ⟦ eval A ⟧) (`Type ℓ)) → Term Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Term Γ (`Type ℓ) → Term Γ (`Type (suc ℓ)) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Term Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (D : Term (Γ , ⟦ eval A ⟧) (`Desc (suc ℓ))) → Term Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Term Γ `⊤ `true `false : Term Γ `Bool _`,_ : ∀{A B} (a : Term Γ A) (b : Term Γ (subT B (eval a))) → Term Γ (`Σ A B) `λ : ∀{A B} (b : Term (Γ , A) B) → Term Γ (`Π A B) {- Value elimination -} `var : ∀{A} → Var Γ A → Term Γ A `if_`then_`else_ : ∀{C} (b : Term Γ `Bool) (c₁ c₂ : Term Γ C) → Term Γ C _`$_ : ∀{A B} (f : Term Γ (`Π A B)) (a : Term Γ A) → Term Γ (subT B (eval a)) `proj₁ : ∀{A B} → Term Γ (`Σ A B) → Term Γ A `proj₂ : ∀{A B} (ab : Term Γ (`Σ A B)) → Term Γ (subT B (proj₁ (eval ab))) `elim⊥ : ∀{A} → Term Γ `⊥ → Term Γ A `elimBool : ∀{ℓ} (P : Term (Γ , `Bool) (`Type ℓ)) (pt : Term Γ (subT ⟦ eval P ⟧ `true)) (pf : Term Γ (subT ⟦ eval P ⟧ `false)) (b : Term Γ `Bool) → Term Γ (subT ⟦ eval P ⟧ (eval b)) ---------------------------------------------------------------------- {- Type introduction -} eval `⊥ = `⊥ eval `⊤ = `⊤ eval `Bool = `Bool eval `Type = `Type eval (`Π A B) = `Π (eval A) (eval B) eval (`Σ A B) = `Σ (eval A) (eval B) eval `⟦ A ⟧ = `⟦ eval A ⟧ {- Desc introduction -} eval `⊤ᵈ = `⊤ᵈ eval `Xᵈ = `Xᵈ eval (`Πᵈ A D) = `Πᵈ (eval A) (eval D) eval (`Σᵈ A D) = `Σᵈ (eval A) (eval D) {- Value introduction -} eval `tt = `tt eval `true = `true eval `false = `false eval (a `, b) = eval a `, eval b eval (`λ b) = `λ (eval b) {- Value elimination -} eval (`var i) = `neut (`var i) eval (`if b `then c₁ `else c₂) = if eval b then eval c₁ else eval c₂ eval (f `$ a) = eval f $ eval a eval (`proj₁ ab) = proj₁ (eval ab) eval (`proj₂ ab) = proj₂ (eval ab) eval (`elim⊥ bot) = elim⊥ (eval bot) eval (`elimBool P pt pf b) = elimBool (eval P) (eval pt) (eval pf) (eval b) ----------------------------------------------------------------------
module Spire.Operational where ---------------------------------------------------------------------- data Level : Set where zero : Level suc : Level → Level ---------------------------------------------------------------------- data Context : Set data Type (Γ : Context) : Set data Value (Γ : Context) : Type Γ → Set data Neutral (Γ : Context) : Type Γ → Set ---------------------------------------------------------------------- data Context where ∅ : Context _,_ : (Γ : Context) → Type Γ → Context data Type Γ where `⊥ `⊤ `Bool : Type Γ `Desc `Type : (ℓ : Level) → Type Γ `Π `Σ : (A : Type Γ) (B : Type (Γ , A)) → Type Γ `μ : ∀{ℓ} → Value Γ (`Desc ℓ) → Type Γ `⟦_⟧ : ∀{ℓ} → Neutral Γ (`Type ℓ) → Type Γ `⟦_⟧ᵈ : ∀{ℓ} → Neutral Γ (`Desc ℓ) → Type Γ → Type Γ ---------------------------------------------------------------------- ⟦_⟧ : ∀{Γ ℓ} → Value Γ (`Type ℓ) → Type Γ ⟦_⟧ᵈ : ∀{Γ ℓ} → Value Γ (`Desc ℓ) → Type Γ → Type Γ postulate wknT : ∀{Γ A} → Type Γ → Type (Γ , A) subT : ∀{Γ A} → Type (Γ , A) → Value Γ A → Type Γ subV : ∀{Γ A B} → Value (Γ , A) B → (x : Value Γ A) → Value Γ (subT B x) data Var : (Γ : Context) (A : Type Γ) → Set where here : ∀{Γ A} → Var (Γ , A) (wknT A) there : ∀{Γ A B} → Var Γ A → Var (Γ , B) (wknT A) ---------------------------------------------------------------------- data Value Γ where {- Type introduction -} `⊥ `⊤ `Bool `Desc `Type : ∀{ℓ} → Value Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ) `μ : ∀{ℓ} → Value Γ (`Desc ℓ) → Value Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (suc ℓ)) `⟦_⟧ᵈ : ∀{ℓ} → Value Γ (`Desc ℓ) → Value Γ (`Type ℓ) → Value Γ (`Type ℓ) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Value Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Desc (suc ℓ))) → Value Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Value Γ `⊤ `true `false : Value Γ `Bool _`,_ : ∀{A B} (a : Value Γ A) (b : Value Γ (subT B a)) → Value Γ (`Σ A B) `λ : ∀{A B} → Value (Γ , A) B → Value Γ (`Π A B) `con : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Value Γ (⟦ D ⟧ᵈ (`μ D)) → Value Γ (`μ D) `neut : ∀{A} → Neutral Γ A → Value Γ A ---------------------------------------------------------------------- data Neutral Γ where {- Value elimination -} `var : ∀{A} → Var Γ A → Neutral Γ A `if_`then_`else_ : ∀{C} (b : Neutral Γ `Bool) (c₁ c₂ : Value Γ C) → Neutral Γ C `elim⊥ : ∀{A} → Neutral Γ `⊥ → Neutral Γ A `elimBool : ∀{ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Neutral Γ `Bool) → Neutral Γ (subT ⟦ P ⟧ (`neut b)) `proj₁ : ∀{A B} → Neutral Γ (`Σ A B) → Neutral Γ A `proj₂ : ∀{A B} (ab : Neutral Γ (`Σ A B)) → Neutral Γ (subT B (`neut (`proj₁ ab))) _`$_ : ∀{A B} (f : Neutral Γ (`Π A B)) (a : Value Γ A) → Neutral Γ (subT B a) ---------------------------------------------------------------------- ⟦ `Π A B ⟧ = `Π ⟦ A ⟧ ⟦ B ⟧ ⟦ `Σ A B ⟧ = `Σ ⟦ A ⟧ ⟦ B ⟧ ⟦ `⊥ ⟧ = `⊥ ⟦ `⊤ ⟧ = `⊤ ⟦ `Bool ⟧ = `Bool ⟦ `μ D ⟧ = `μ D ⟦ `Type {zero} ⟧ = `⊥ ⟦ `Type {suc ℓ} ⟧ = `Type ℓ ⟦ `⟦ A ⟧ ⟧ = ⟦ A ⟧ ⟦ `Desc {ℓ} ⟧ = `Desc ℓ ⟦ `⟦ D ⟧ᵈ X ⟧ = ⟦ D ⟧ᵈ ⟦ X ⟧ ⟦ `neut A ⟧ = `⟦ A ⟧ ---------------------------------------------------------------------- ⟦ `⊤ᵈ ⟧ᵈ X = `⊤ ⟦ `Xᵈ ⟧ᵈ X = X ⟦ `Πᵈ A D ⟧ᵈ X = `Π ⟦ A ⟧ (⟦ D ⟧ᵈ (wknT X)) ⟦ `Σᵈ A D ⟧ᵈ X = `Σ ⟦ A ⟧ (⟦ D ⟧ᵈ (wknT X)) ⟦ `neut D ⟧ᵈ X = `⟦ D ⟧ᵈ X ---------------------------------------------------------------------- elim⊥ : ∀{Γ A} → Value Γ `⊥ → Value Γ A elim⊥ (`neut bot) = `neut (`elim⊥ bot) ---------------------------------------------------------------------- if_then_else_ : ∀{Γ C} (b : Value Γ `Bool) (c₁ c₂ : Value Γ C) → Value Γ C if `true then c₁ else c₂ = c₁ if `false then c₁ else c₂ = c₂ if `neut b then c₁ else c₂ = `neut (`if b `then c₁ `else c₂) elimBool : ∀{Γ ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Value Γ `Bool) → Value Γ (subT ⟦ P ⟧ b) elimBool P pt pf `true = pt elimBool P pt pf `false = pf elimBool P pt pf (`neut b) = `neut (`elimBool P pt pf b) ---------------------------------------------------------------------- proj₁ : ∀{Γ A B} → Value Γ (`Σ A B) → Value Γ A proj₁ (a `, b) = a proj₁ (`neut ab) = `neut (`proj₁ ab) proj₂ : ∀{Γ A B} (ab : Value Γ (`Σ A B)) → Value Γ (subT B (proj₁ ab)) proj₂ (a `, b) = b proj₂ (`neut ab) = `neut (`proj₂ ab) ---------------------------------------------------------------------- _$_ : ∀{Γ A B} → Value Γ (`Π A B) → (a : Value Γ A) → Value Γ (subT B a) `λ b $ a = subV b a `neut f $ a = `neut (f `$ a) ---------------------------------------------------------------------- data Term (Γ : Context) : Type Γ → Set eval : ∀{Γ A} → Term Γ A → Value Γ A data Term Γ where {- Type introduction -} `⊥ `⊤ `Bool `Type : ∀{ℓ} → Term Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (B : Term (Γ , ⟦ eval A ⟧) (`Type ℓ)) → Term Γ (`Type ℓ) `μ : ∀{ℓ} → Term Γ (`Desc ℓ) → Term Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Term Γ (`Type ℓ) → Term Γ (`Type (suc ℓ)) `⟦_⟧ᵈ : ∀{ℓ} → Term Γ (`Desc ℓ) → Term Γ (`Type ℓ) → Term Γ (`Type ℓ) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Term Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (D : Term (Γ , ⟦ eval A ⟧) (`Desc (suc ℓ))) → Term Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Term Γ `⊤ `true `false : Term Γ `Bool _`,_ : ∀{A B} (a : Term Γ A) (b : Term Γ (subT B (eval a))) → Term Γ (`Σ A B) `λ : ∀{A B} (b : Term (Γ , A) B) → Term Γ (`Π A B) `con : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Term Γ (⟦ D ⟧ᵈ (`μ D)) → Term Γ (`μ D) {- Value elimination -} `var : ∀{A} → Var Γ A → Term Γ A `if_`then_`else_ : ∀{C} (b : Term Γ `Bool) (c₁ c₂ : Term Γ C) → Term Γ C _`$_ : ∀{A B} (f : Term Γ (`Π A B)) (a : Term Γ A) → Term Γ (subT B (eval a)) `proj₁ : ∀{A B} → Term Γ (`Σ A B) → Term Γ A `proj₂ : ∀{A B} (ab : Term Γ (`Σ A B)) → Term Γ (subT B (proj₁ (eval ab))) `elim⊥ : ∀{A} → Term Γ `⊥ → Term Γ A `elimBool : ∀{ℓ} (P : Term (Γ , `Bool) (`Type ℓ)) (pt : Term Γ (subT ⟦ eval P ⟧ `true)) (pf : Term Γ (subT ⟦ eval P ⟧ `false)) (b : Term Γ `Bool) → Term Γ (subT ⟦ eval P ⟧ (eval b)) ---------------------------------------------------------------------- {- Type introduction -} eval `⊥ = `⊥ eval `⊤ = `⊤ eval `Bool = `Bool eval `Type = `Type eval (`Π A B) = `Π (eval A) (eval B) eval (`Σ A B) = `Σ (eval A) (eval B) eval (`μ D) = `μ (eval D) eval `⟦ A ⟧ = `⟦ eval A ⟧ eval (`⟦ D ⟧ᵈ X) = `⟦ eval D ⟧ᵈ (eval X) {- Desc introduction -} eval `⊤ᵈ = `⊤ᵈ eval `Xᵈ = `Xᵈ eval (`Πᵈ A D) = `Πᵈ (eval A) (eval D) eval (`Σᵈ A D) = `Σᵈ (eval A) (eval D) {- Value introduction -} eval `tt = `tt eval `true = `true eval `false = `false eval (a `, b) = eval a `, eval b eval (`λ b) = `λ (eval b) eval (`con x) = `con (eval x) {- Value elimination -} eval (`var i) = `neut (`var i) eval (`if b `then c₁ `else c₂) = if eval b then eval c₁ else eval c₂ eval (f `$ a) = eval f $ eval a eval (`proj₁ ab) = proj₁ (eval ab) eval (`proj₂ ab) = proj₂ (eval ab) eval (`elim⊥ bot) = elim⊥ (eval bot) eval (`elimBool P pt pf b) = elimBool (eval P) (eval pt) (eval pf) (eval b) ----------------------------------------------------------------------
Add Description introductions to operational semantics.
Add Description introductions to operational semantics.
Agda
bsd-3-clause
spire/spire
5d7647a9747da1890100da0dfd670ed16f37961a
Parametric/Change/Term.agda
Parametric/Change/Term.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply diff-base apply {base ι} = diff-base , apply lift-diff-apply diff-base apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff-base apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff-base apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff-base apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff-base apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) lift-diff diff-base apply = λ {τ Γ} → proj₁ (lift-diff-apply diff-base apply {τ} {Γ}) lift-apply : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-apply diff-base apply = λ {τ Γ} → proj₂ (lift-diff-apply diff-base apply {τ} {Γ})
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply diff-base apply-base {base ι} = diff-base , apply-base lift-diff-apply diff-base apply-base {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff-base apply-base {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff-base apply-base {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff-base apply-base {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff-base apply-base {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) lift-diff diff-base apply-base = λ {τ Γ} → proj₁ (lift-diff-apply diff-base apply-base {τ} {Γ}) lift-apply : DiffStructure → ApplyStructure → ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-apply diff-base apply-base = λ {τ Γ} → proj₂ (lift-diff-apply diff-base apply-base {τ} {Γ})
Rename apply to apply-base.
Rename apply to apply-base. Old-commit-hash: d8eae9079296e08a6b1db1db97ac5c9722947b28
Agda
mit
inc-lc/ilc-agda
abeda4b342c437d506e6e55cef2e20b34d09e7dd
Atlas/Change/Derive.agda
Atlas/Change/Derive.agda
module Atlas.Change.Derive where open import Atlas.Syntax.Type open import Atlas.Syntax.Term open import Atlas.Change.Type open import Atlas.Change.Term ΔConst : ∀ {Γ Σ τ} → (c : Const Σ τ) → Term Γ (internalizeContext (ΔContext′ Σ) (ΔType τ)) ΔConst true = false! ΔConst false = false! ΔConst xor = abs₄ (λ x Δx y Δy → xor! Δx Δy) ΔConst empty = empty! -- If k ⊕ Δk ≡ k, then -- Δupdate k Δk v Δv m Δm = update k Δv Δm -- else it is a deletion followed by insertion: -- Δupdate k Δk v Δv m Δm = -- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm) -- -- We implement the else-branch only for the moment. ΔConst update = abs₆ (λ k Δk v Δv m Δm → insert (apply Δk k) (apply Δv v) (delete k v Δm)) -- Δlookup k Δk m Δm | true? (k ⊕ Δk ≡ k) -- ... | true = lookup k Δm -- ... | false = -- (lookup (k ⊕ Δk) m ⊕ lookup (k ⊕ Δk) Δm) -- ⊝ lookup k m -- -- Only the false-branch is implemented. ΔConst lookup = abs₄ (λ k Δk m Δm → let k′ = apply Δk k in (diff (apply {base _} (lookup! k′ Δm) (lookup! k′ m)) (lookup! k m))) -- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f) -- -- ... | true = -- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂) -- Δm₁ Δm₂ -- -- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂ -- -- we implement the false-branch for the moment. ΔConst zip = abs₆ (λ f Δf m₁ Δm₁ m₂ Δm₂ → let g = abs (app₂ (weaken₁ Δf) (var this) nil-term) in zip4! g m₁ Δm₁ m₂ Δm₂) -- Δfold f Δf z Δz m Δm = proj₂ -- (fold (λ k [a,Δa] [b,Δb] → -- uncurry (λ a Δa → uncurry (λ b Δb → -- pair (f k a b) (Δf k nil a Δa b Δb)) -- [b,Δb]) [a,Δa]) -- (pair z Δz) -- (zip-pair m Δm)) -- -- Δfold is efficient only if evaluation is lazy and Δf is -- self-maintainable: it doesn't look at the argument -- (b = fold f k a b₀) at all. ΔConst (fold {κ} {α} {β}) = let -- TODO (tedius): write weaken₇ f = weaken₃ (weaken₃ (weaken₁ (var (that (that (that (that (that this)))))))) Δf = weaken₃ (weaken₃ (weaken₁ (var (that (that (that (that this))))))) z = var (that (that (that this))) Δz = var (that (that this)) m = var (that this) Δm = var this k = weaken₃ (weaken₁ (var (that (that this)))) [a,Δa] = var (that this) [b,Δb] = var this a = var (that (that (that this))) Δa = var (that (that this)) b = var (that this) Δb = var this g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs (pair (app₃ f k a b) (app₆ Δf k nil-term a Δa b Δb)))) (weaken₂ [b,Δb])))) [a,Δa]))) proj₂ = uncurry (abs (abs (var this))) in abs (abs (abs (abs (abs (abs (proj₂ (fold! g (pair z Δz) (zip-pair m Δm))))))))
module Atlas.Change.Derive where open import Atlas.Syntax.Type open import Atlas.Syntax.Term open import Atlas.Change.Type open import Atlas.Change.Term ΔConst : ∀ {Γ Σ τ} → Const Σ τ → Terms (ΔContext Γ) (ΔContext Σ) → Term (ΔContext Γ) (ΔType τ) ΔConst true ∅ = false! ΔConst false ∅ = false! ΔConst xor (Δx • x • Δy • y • ∅) = xor! Δx Δy ΔConst empty ∅ = empty! -- If k ⊕ Δk ≡ k, then -- Δupdate k Δk v Δv m Δm = update k Δv Δm -- else it is a deletion followed by insertion: -- Δupdate k Δk v Δv m Δm = -- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm) -- -- We implement the else-branch only for the moment. ΔConst (update {κ} {ι}) (Δk • k • Δv • v • Δm • m • ∅) = insert (apply {base κ} Δk k) (apply {base ι} Δv v) (delete k v Δm) -- Δlookup k Δk m Δm | true? (k ⊕ Δk ≡ k) -- ... | true = lookup k Δm -- ... | false = -- (lookup (k ⊕ Δk) m ⊕ lookup (k ⊕ Δk) Δm) -- ⊝ lookup k m -- -- Only the false-branch is implemented. ΔConst (lookup {κ} {ι}) (Δk • k • Δm • m • ∅) = let k′ = apply {base κ} Δk k in (diff (apply {base _} (lookup! k′ Δm) (lookup! k′ m)) (lookup! k m)) -- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f) -- -- ... | true = -- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂) -- Δm₁ Δm₂ -- -- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂ -- -- we implement the false-branch for the moment. ΔConst zip (Δf • f • Δm₁ • m₁ • Δm₂ • m₂ • ∅) = let g = abs (app₂ (weaken₁ Δf) (var this) nil-term) in zip4! g m₁ Δm₁ m₂ Δm₂ -- Δfold f Δf z Δz m Δm = proj₂ -- (fold (λ k [a,Δa] [b,Δb] → -- uncurry (λ a Δa → uncurry (λ b Δb → -- pair (f k a b) (Δf k nil a Δa b Δb)) -- [b,Δb]) [a,Δa]) -- (pair z Δz) -- (zip-pair m Δm)) -- -- Δfold is efficient only if evaluation is lazy and Δf is -- self-maintainable: it doesn't look at the argument -- (b = fold f k a b₀) at all. ΔConst (fold {κ} {α} {β}) (Δf′ • f′ • Δz • z • Δm • m • ∅) = let -- TODO (tedius): write weaken₇ f = weaken₃ (weaken₃ (weaken₁ f′)) Δf = weaken₃ (weaken₃ (weaken₁ Δf′)) k = weaken₃ (weaken₁ (var (that (that this)))) [a,Δa] = var (that this) [b,Δb] = var this a = var (that (that (that this))) Δa = var (that (that this)) b = var (that this) Δb = var this g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs (pair (app₃ f k a b) (app₆ Δf k nil-term a Δa b Δb)))) (weaken₂ [b,Δb])))) [a,Δa]))) proj₂ = uncurry (abs (abs (var this))) in proj₂ (fold! g (pair z Δz) (zip-pair m Δm))
Convert ΔConst to new derivation interface.
Convert ΔConst to new derivation interface. Old-commit-hash: 7b59661ce496b2c2659e20c6f9c4f528ddbd6102
Agda
mit
inc-lc/ilc-agda
6d4ce38ab949f2a79c7caa46d5283b91888d5b61
FunUniverse/Fin.agda
FunUniverse/Fin.agda
{-# OPTIONS --without-K #-} module FunUniverse.Fin where open import Type open import Data.Nat using (ℕ) open import Data.Fin using (Fin) open import FunUniverse.Data open import FunUniverse.Core _→ᶠ_ : ℕ → ℕ → ★ _→ᶠ_ i o = Fin i → Fin o finFunU : FunUniverse ℕ finFunU = Fin-U , _→ᶠ_ module FinFunUniverse = FunUniverse finFunU
{-# OPTIONS --without-K #-} module FunUniverse.Fin where open import Type open import Function open import Data.Nat using (ℕ) open import Data.Fin using (Fin) open import FunUniverse.Data open import FunUniverse.Core open import FunUniverse.Category open import FunUniverse.Rewiring.Linear _→ᶠ_ : ℕ → ℕ → ★ _→ᶠ_ i o = Fin i → Fin o finFunU : FunUniverse ℕ finFunU = Fin-U , _→ᶠ_ module FinFunUniverse = FunUniverse finFunU finCat : Category _→ᶠ_ finCat = id , _∘′_ {- finLin : LinRewiring finFunU finLin = mk finCat {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} -}
add finCat
FunUniverse.Fin: add finCat
Agda
bsd-3-clause
crypto-agda/crypto-agda
f53c070dcba6e92a79746f5762bb453a510cf059
README.agda
README.agda
module README where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- * Δ e describes how e changes when its free variables or its arguments change -- * denotational semantics including semantics of changes -- -- Note that Δ is *not* the same as the ∂ operator in -- definition/intro.tex. See discussion at: -- -- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325 -- -- Work in Progress: -- * lemmas about behavior of changes -- * lemmas about behavior of Δ -- * correctness proof for symbolic derivation -- The formalization is split across the following files. -- Note that we use two postulates currently: -- 1. function extensionality (in -- `Denotational.ExtensionalityPostulate`), known to be consistent -- with intensional type theory. -- -- 2. Temporarily, we use `diff-apply` as a postulate, which is only -- true in a slightly weaker form, `diff-apply-proof` - we are -- adapting the proofs to use the latter. -- TODO: document them more. open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Syntactic.Changes open import Denotational.Notation open import Denotational.Values open import Denotational.ExtensionalityPostulate open import Denotational.EqualityLemmas open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Denotational.ValidChanges open import Changes open import ChangeContexts open import ChangeContextLifting open import PropsDelta open import SymbolicDerivation -- This finishes the correctness proof of the above work. open import total -- EXAMPLES open import Examples.Examples1 -- NATURAL semantics open import Syntactic.Closures open import Denotational.Closures open import Natural.Lookup open import Natural.Evaluation open import Natural.Soundness -- Other calculi open import partial open import lambda
module README where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- * Δ e describes how e changes when its free variables or its arguments change -- * denotational semantics including semantics of changes -- -- Note that Δ is *not* the same as the ∂ operator in -- definition/intro.tex. See discussion at: -- -- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325 -- -- Work in Progress: -- * lemmas about behavior of changes -- * lemmas about behavior of Δ -- * correctness proof for symbolic derivation -- The formalization is split across the following files. -- TODO: document them more. -- Note that we use two postulates currently: -- 1. function extensionality (in -- `Denotational.ExtensionalityPostulate`), known to be consistent -- with intensional type theory. -- -- 2. Temporarily, we use `diff-apply` as a postulate, which is only -- true in a slightly weaker form, `diff-apply-proof` - we are -- adapting the proofs to use the latter. open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Syntactic.Changes open import Denotational.Notation open import Denotational.Values open import Denotational.ExtensionalityPostulate open import Denotational.EqualityLemmas open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Denotational.ValidChanges open import Changes open import ChangeContexts open import ChangeContextLifting open import PropsDelta open import SymbolicDerivation -- This finishes the correctness proof of the above work. open import total -- EXAMPLES open import Examples.Examples1 -- NATURAL semantics open import Syntactic.Closures open import Denotational.Closures open import Natural.Lookup open import Natural.Evaluation open import Natural.Soundness -- Other calculi open import partial open import lambda
comment fix
agda: comment fix Old-commit-hash: d5ff611d7aaf67a4b7821fedb255e43d07c53504
Agda
mit
inc-lc/ilc-agda
2cf425c915364c989a28b50e75f8d096d98a2b30
total.agda
total.agda
module total where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- * Δ e describes how e changes when its free variables or its arguments change -- * denotational semantics including semantics of changes -- -- Note that Δ is *not* the same as the ∂ operator in -- definition/intro.tex. See discussion at: -- -- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325 -- -- Work in Progress: -- * lemmas about behavior of changes -- * lemmas about behavior of Δ -- * correctness proof for symbolic derivation open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Syntactic.Changes open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Denotational.ValidChanges open import Changes open import ChangeContexts open import ChangeContextLifting open import PropsDelta open import SymbolicDerivation -- CORRECTNESS of derivation derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) → diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡ ⟦ derive-var x ⟧ ρ derive-var-correct (dv • v • ρ) this = diff-apply dv v derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) → Δ {{Γ′}} t ≈ derive-term {{Γ′}} t derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) = begin Δ (abs t) ≈⟨ Δ-abs t ⟩ abs (abs (Δ {τ • Γ₁} t)) ≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} t)) ⟩ abs (abs (derive-term {τ • Γ₁} t)) ≡⟨⟩ derive-term (abs t) ∎ where open ≈-Reasoning Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) = begin Δ (app t₁ t₂) ≈⟨ Δ-app t₁ t₂ ⟩ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩ app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) ≡⟨⟩ derive-term {{Γ′}} (app t₁ t₂) ∎ where open ≈-Reasoning derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ → begin ⟦ Δ {{Γ′}} (var x) ⟧ ρ ≡⟨⟩ diff (⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ))) (⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ))) ≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩ ⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ) ≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩ ⟦ lift Γ′ (derive-var x) ⟧Var ρ ∎) where open ≡-Reasoning derive-term-correct true = ext-t (λ ρ → ≡-refl) derive-term-correct false = ext-t (λ ρ → ≡-refl) derive-term-correct {{Γ′}} (if t₁ t₂ t₃) = ext-t (λ ρ → begin ⟦ Δ (if t₁ t₂ t₃) ⟧Term ρ ≡⟨⟩ diff (⟦ if t₁ t₂ t₃ ⟧Term (update (⟦ Γ′ ⟧ ρ))) (⟦ if t₁ t₂ t₃ ⟧Term (ignore (⟦ Γ′ ⟧ ρ))) ≡⟨ {!!} ⟩ ⟦ derive-term (if t₁ t₂ t₃) ⟧ ρ ∎) where open ≡-Reasoning derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
module total where -- INCREMENTAL λ-CALCULUS -- with total derivatives -- -- Features: -- * changes and derivatives are unified (following Cai) -- * Δ e describes how e changes when its free variables or its arguments change -- * denotational semantics including semantics of changes -- -- Note that Δ is *not* the same as the ∂ operator in -- definition/intro.tex. See discussion at: -- -- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325 -- -- Work in Progress: -- * lemmas about behavior of changes -- * lemmas about behavior of Δ -- * correctness proof for symbolic derivation open import Relation.Binary.PropositionalEquality open import Syntactic.Types open import Syntactic.Contexts Type open import Syntactic.Terms.Total open import Syntactic.Changes open import Denotational.Notation open import Denotational.Values open import Denotational.Environments Type ⟦_⟧Type open import Denotational.Evaluation.Total open import Denotational.Equivalence open import Denotational.ValidChanges open import Changes open import ChangeContexts open import ChangeContextLifting open import PropsDelta open import SymbolicDerivation -- CORRECTNESS of derivation derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) → diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡ ⟦ derive-var x ⟧ ρ derive-var-correct (dv • v • ρ) this = diff-apply dv v derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) → Δ {{Γ′}} t ≈ derive-term {{Γ′}} t derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) = begin Δ (abs t) ≈⟨ Δ-abs t ⟩ abs (abs (Δ {τ • Γ₁} t)) ≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} t)) ⟩ abs (abs (derive-term {τ • Γ₁} t)) ≡⟨⟩ derive-term (abs t) ∎ where open ≈-Reasoning Γ″ = keep Δ-Type τ • keep τ • Γ′ derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) = begin Δ (app t₁ t₂) ≈⟨ Δ-app t₁ t₂ ⟩ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩ app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂) ≡⟨⟩ derive-term {{Γ′}} (app t₁ t₂) ∎ where open ≈-Reasoning derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ → begin ⟦ Δ {{Γ′}} (var x) ⟧ ρ ≡⟨⟩ diff (⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ))) (⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ))) ≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩ ⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ) ≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩ ⟦ lift Γ′ (derive-var x) ⟧Var ρ ∎) where open ≡-Reasoning derive-term-correct true = ext-t (λ ρ → ≡-refl) derive-term-correct false = ext-t (λ ρ → ≡-refl) derive-term-correct {{Γ′}} (if t₁ t₂ t₃) = begin Δ (if t₁ t₂ t₃) ≈⟨ {!!} ⟩ if (derive-term t₁ and lift-term {{Γ′}} t₁) (diff-term (apply-term (derive-term t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂)) (if (derive-term t₁ and (lift-term {{Γ′}} (! t₁))) (diff-term (apply-term (derive-term t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)) (if (lift-term {{Γ′}} t₁) (derive-term t₂) (derive-term t₃))) ≡⟨⟩ derive-term {{Γ′}} (if t₁ t₂ t₃) ∎ where open ≈-Reasoning derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
Prepare for using ≈-Reasoning to prove derive-term-correct (see #25).
Prepare for using ≈-Reasoning to prove derive-term-correct (see #25). Old-commit-hash: b0ca315e1dcaa44e39fdfa67bb29e1da512641a6
Agda
mit
inc-lc/ilc-agda
5084db0b3e01eca253b247da48daf55234039968
derivation.agda
derivation.agda
module derivation where open import lambda -- KO: Is it correct that this file is supposed to contain the syntactic -- variants of the operations in Changes.agda? -- If so, why does it have a different (and rather strange) Δ-Type definition? -- Why are the base cases (bool) all missing? The inductive cases look -- boring since they don't actually do anything (which explains why compose and apply are the same) -- CHANGE TYPES Δ-Type : Type → Type Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂ Δ-Type bool = {!!} apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply {τ₁ ⇒ τ₂} = abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x ) ( f x ) apply {bool} = {!!} compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ) compose {τ₁ ⇒ τ₂} = abs (abs (abs (app (app (compose {τ₂}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λdg. λx. compose ( df x ) ( dg x ) compose {bool} = {!!} nil : ∀ {τ Γ} → Term Γ (Δ-Type τ) nil {τ₁ ⇒ τ₂} = abs (nil {τ₂}) -- λx. nil nil {bool} = {!!} -- Hey, apply is α-equivalent to compose, what's going on? -- Oh, and `Δ-Type` is the identity function? -- CHANGE CONTEXTS Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ -- CHANGE VARIABLES -- changes 'x' to 'dx' rename : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) rename this = that this rename (that x) = that (that (rename x)) -- Weakening of a term needed during derivation - change x to x in a context which also includes dx. -- The actual specification is more complicated, study the type signature. adaptVar : ∀ {τ} Γ₁ Γ₂ → Var (Γ₁ ⋎ Γ₂) τ → Var (Γ₁ ⋎ Δ-Context Γ₂) τ adaptVar ∅ (τ₂ • Γ₂) this = this adaptVar ∅ (τ₂ • Γ₂) (that x) = that (that (adaptVar ∅ Γ₂ x)) adaptVar (τ₁ • Γ₁) Γ₂ this = this adaptVar (τ₁ • Γ₁) Γ₂ (that x) = that (adaptVar Γ₁ Γ₂ x) adapt : ∀ {τ} Γ₁ Γ₂ → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) τ adapt {τ₁ ⇒ τ₂} Γ₁ Γ₂ (abs t) = abs (adapt (τ₁ • Γ₁) Γ₂ t) adapt Γ₁ Γ₂ (app t₁ t₂) = app (adapt Γ₁ Γ₂ t₁) (adapt Γ₁ Γ₂ t₂) adapt Γ₁ Γ₂ (var t) = var (adaptVar Γ₁ Γ₂ t) adapt Γ₁ Γ₂ true = true adapt Γ₁ Γ₂ false = false adapt Γ₁ Γ₂ (cond tc t₁ t₂) = cond (adapt Γ₁ Γ₂ tc) (adapt Γ₁ Γ₂ t₁) (adapt Γ₁ Γ₂ t₂) -- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound) Δ-var : ∀ {Γ₁ Γ₂ τ} → Var (Γ₁ ⋎ Γ₂) τ → Term (Δ-Context Γ₂) (Δ-Type τ) Δ-var {∅} x = var (rename x) Δ-var {τ • Γ} this = nil {τ} Δ-var {τ • Γ} (that x) = Δ-var {Γ} x -- Note: this should be the derivative with respect to the first variable. nabla : ∀ {Γ τ₁ τ₂} → (t₀ : Term Γ (τ₁ ⇒ τ₂)) → Term Γ (τ₁ ⇒ Δ-Type τ₁ ⇒ Δ-Type τ₂) nabla = {!!} -- CHANGE TERMS Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ) Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t) -- To recheck: which is the order in which to apply the changes? When I did my live proof to Klaus, I came up with this order: -- (Δ-term t₁) (t₂ ⊕ (Δ-term t₂)) ∘ (∇ t₁) t₂ (Δ-term t₂) -- corresponding to: Δ-term {Γ₁} {Γ₂} {τ} (app t₁ t₂) = app (app (compose {τ}) (app (Δ-term {Γ₁} t₁) (app (app apply (Δ-term {Γ₁} {Γ₂} t₂)) (adapt Γ₁ Γ₂ t₂)))) (app (adapt Γ₁ Γ₂ (app (nabla {Γ₁ ⋎ Γ₂} t₁) t₂)) (Δ-term {Γ₁} {Γ₂} t₂)) Δ-term {Γ} (var x) = weaken {∅} {Γ} (Δ-var {Γ} x) Δ-term {Γ} true = {!!} Δ-term {Γ} false = {!!} Δ-term {Γ} (cond t₁ t₂ t₃) = {!!}
module derivation where open import lambda -- KO: Is it correct that this file is supposed to contain the syntactic -- variants of the operations in Syntactic.Changes.agda? -- If so, why does it have a different (and rather strange) Δ-Type definition? -- Why are the base cases (bool) all missing? The inductive cases look -- boring since they don't actually do anything (which explains why compose and apply are the same) -- CHANGE TYPES Δ-Type : Type → Type Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂ Δ-Type bool = {!!} apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply {τ₁ ⇒ τ₂} = abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x ) ( f x ) apply {bool} = {!!} compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ) compose {τ₁ ⇒ τ₂} = abs (abs (abs (app (app (compose {τ₂}) (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λdg. λx. compose ( df x ) ( dg x ) compose {bool} = {!!} nil : ∀ {τ Γ} → Term Γ (Δ-Type τ) nil {τ₁ ⇒ τ₂} = abs (nil {τ₂}) -- λx. nil nil {bool} = {!!} -- Hey, apply is α-equivalent to compose, what's going on? -- Oh, and `Δ-Type` is the identity function? -- CHANGE CONTEXTS Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ -- CHANGE VARIABLES -- changes 'x' to 'dx' rename : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ) rename this = that this rename (that x) = that (that (rename x)) -- Weakening of a term needed during derivation - change x to x in a context which also includes dx. -- The actual specification is more complicated, study the type signature. adaptVar : ∀ {τ} Γ₁ Γ₂ → Var (Γ₁ ⋎ Γ₂) τ → Var (Γ₁ ⋎ Δ-Context Γ₂) τ adaptVar ∅ (τ₂ • Γ₂) this = this adaptVar ∅ (τ₂ • Γ₂) (that x) = that (that (adaptVar ∅ Γ₂ x)) adaptVar (τ₁ • Γ₁) Γ₂ this = this adaptVar (τ₁ • Γ₁) Γ₂ (that x) = that (adaptVar Γ₁ Γ₂ x) adapt : ∀ {τ} Γ₁ Γ₂ → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) τ adapt {τ₁ ⇒ τ₂} Γ₁ Γ₂ (abs t) = abs (adapt (τ₁ • Γ₁) Γ₂ t) adapt Γ₁ Γ₂ (app t₁ t₂) = app (adapt Γ₁ Γ₂ t₁) (adapt Γ₁ Γ₂ t₂) adapt Γ₁ Γ₂ (var t) = var (adaptVar Γ₁ Γ₂ t) adapt Γ₁ Γ₂ true = true adapt Γ₁ Γ₂ false = false adapt Γ₁ Γ₂ (cond tc t₁ t₂) = cond (adapt Γ₁ Γ₂ tc) (adapt Γ₁ Γ₂ t₁) (adapt Γ₁ Γ₂ t₂) -- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound) Δ-var : ∀ {Γ₁ Γ₂ τ} → Var (Γ₁ ⋎ Γ₂) τ → Term (Δ-Context Γ₂) (Δ-Type τ) Δ-var {∅} x = var (rename x) Δ-var {τ • Γ} this = nil {τ} Δ-var {τ • Γ} (that x) = Δ-var {Γ} x -- Note: this should be the derivative with respect to the first variable. nabla : ∀ {Γ τ₁ τ₂} → (t₀ : Term Γ (τ₁ ⇒ τ₂)) → Term Γ (τ₁ ⇒ Δ-Type τ₁ ⇒ Δ-Type τ₂) nabla = {!!} -- CHANGE TERMS Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ) Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t) -- To recheck: which is the order in which to apply the changes? When I did my live proof to Klaus, I came up with this order: -- (Δ-term t₁) (t₂ ⊕ (Δ-term t₂)) ∘ (∇ t₁) t₂ (Δ-term t₂) -- corresponding to: Δ-term {Γ₁} {Γ₂} {τ} (app t₁ t₂) = app (app (compose {τ}) (app (Δ-term {Γ₁} t₁) (app (app apply (Δ-term {Γ₁} {Γ₂} t₂)) (adapt Γ₁ Γ₂ t₂)))) (app (adapt Γ₁ Γ₂ (app (nabla {Γ₁ ⋎ Γ₂} t₁) t₂)) (Δ-term {Γ₁} {Γ₂} t₂)) Δ-term {Γ} (var x) = weaken {∅} {Γ} (Δ-var {Γ} x) Δ-term {Γ} true = {!!} Δ-term {Γ} false = {!!} Δ-term {Γ} (cond t₁ t₂ t₃) = {!!}
Update derivation.agda
Update derivation.agda typo Old-commit-hash: 719b7f355a6ddb5cacf3f0d3a90f563748f921a0
Agda
mit
inc-lc/ilc-agda
0ea931fffedcc73acbb0013981dff55d1c0d69ce
Thesis/LangChanges.agda
Thesis/LangChanges.agda
module Thesis.LangChanges where open import Thesis.Changes open import Thesis.IntChanges open import Thesis.Lang open import Relation.Binary.PropositionalEquality Chτ : (τ : Type) → Set Chτ τ = ⟦ Δt τ ⟧Type ΔΓ : Context → Context ΔΓ ∅ = ∅ ΔΓ (τ • Γ) = Δt τ • τ • ΔΓ Γ ChΓ : ∀ (Γ : Context) → Set ChΓ Γ = ⟦ ΔΓ Γ ⟧Context [_]τ_from_to_ : ∀ (τ : Type) → (dv : Chτ τ) → (v1 v2 : ⟦ τ ⟧Type) → Set isCompChangeStructureτ : ∀ τ → IsCompChangeStructure ⟦ τ ⟧Type ⟦ Δt τ ⟧Type [ τ ]τ_from_to_ changeStructureτ : ∀ τ → ChangeStructure ⟦ τ ⟧Type changeStructureτ τ = record { isCompChangeStructure = isCompChangeStructureτ τ } instance ichangeStructureτ : ∀ {τ} → ChangeStructure ⟦ τ ⟧Type ichangeStructureτ {τ} = changeStructureτ τ [ σ ⇒ τ ]τ df from f1 to f2 = ∀ (da : Chτ σ) (a1 a2 : ⟦ σ ⟧Type) → [ σ ]τ da from a1 to a2 → [ τ ]τ df a1 da from f1 a1 to f2 a2 [ int ]τ dv from v1 to v2 = v1 + dv ≡ v2 [ pair σ τ ]τ (da , db) from (a1 , b1) to (a2 , b2) = [ σ ]τ da from a1 to a2 × [ τ ]τ db from b1 to b2 [ sum σ τ ]τ dv from v1 to v2 = sch_from_to_ dv v1 v2 isCompChangeStructureτ (σ ⇒ τ) = ChangeStructure.isCompChangeStructure (funCS {{changeStructureτ σ}} {{changeStructureτ τ}}) isCompChangeStructureτ int = ChangeStructure.isCompChangeStructure intCS isCompChangeStructureτ (pair σ τ) = ChangeStructure.isCompChangeStructure (pairCS {{changeStructureτ σ}} {{changeStructureτ τ}}) isCompChangeStructureτ (sum σ τ) = ChangeStructure.isCompChangeStructure ((sumCS {{changeStructureτ σ}} {{changeStructureτ τ}})) open import Data.Unit -- We can define an alternative semantics for contexts, that defines environments as nested products: ⟦_⟧Context-prod : Context → Set ⟦ ∅ ⟧Context-prod = ⊤ ⟦ τ • Γ ⟧Context-prod = ⟦ τ ⟧Type × ⟦ Γ ⟧Context-prod -- And define a change structure for such environments, reusing the change -- structure constructor for pairs. However, this change structure does not -- duplicate base values in the environment. We probably *could* define a -- different change structure for pairs that gives the right result. changeStructureΓ-old : ∀ Γ → ChangeStructure ⟦ Γ ⟧Context-prod changeStructureΓ-old ∅ = unitCS changeStructureΓ-old (τ • Γ) = pairCS {{changeStructureτ τ}} {{changeStructureΓ-old Γ}} data [_]Γ_from_to_ : ∀ Γ → ChΓ Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set where v∅ : [ ∅ ]Γ ∅ from ∅ to ∅ _v•_ : ∀ {τ Γ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ]τ dv from v1 to v2) → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → [ τ • Γ ]Γ (dv • v1 • dρ) from (v1 • ρ1) to (v2 • ρ2) module _ where _e⊕_ : ∀ {Γ} → ⟦ Γ ⟧Context → ChΓ Γ → ⟦ Γ ⟧Context _e⊕_ ∅ ∅ = ∅ _e⊕_ (_ • ρ) (dv • v • dρ) = v ⊕ dv • ρ e⊕ dρ _e⊝_ : ∀ {Γ} → ⟦ Γ ⟧Context → ⟦ Γ ⟧Context → ChΓ Γ _e⊝_ ∅ ∅ = ∅ _e⊝_ (v₂ • ρ₂) (v₁ • ρ₁) = v₂ ⊝ v₁ • v₁ • ρ₂ e⊝ ρ₁ isEnvCompCS : ∀ Γ → IsCompChangeStructure ⟦ Γ ⟧Context (ChΓ Γ) [ Γ ]Γ_from_to_ efromto→⊕ : ∀ {Γ} (dρ : ChΓ Γ) (ρ1 ρ2 : ⟦ Γ ⟧Context) → [ Γ ]Γ dρ from ρ1 to ρ2 → (ρ1 e⊕ dρ) ≡ ρ2 efromto→⊕ .∅ .∅ .∅ v∅ = refl efromto→⊕ .(_ • _ • _) .(_ • _) .(_ • _) (dvv v• dρρ) = cong₂ _•_ (fromto→⊕ _ _ _ dvv) (efromto→⊕ _ _ _ dρρ) e⊝-fromto : ∀ {Γ} → (ρ1 ρ2 : ⟦ Γ ⟧Context) → [ Γ ]Γ ρ2 e⊝ ρ1 from ρ1 to ρ2 e⊝-fromto ∅ ∅ = v∅ e⊝-fromto (v1 • ρ1) (v2 • ρ2) = ⊝-fromto v1 v2 v• e⊝-fromto ρ1 ρ2 _e⊚[_]_ : ∀ {Γ} → ChΓ Γ → ⟦ Γ ⟧Context → ChΓ Γ → ChΓ Γ _e⊚[_]_ {∅} ∅ ∅ ∅ = ∅ _e⊚[_]_ {τ • Γ} (dv1 • _ • dρ1) (v1 • ρ1) (dv2 • _ • dρ2) = (dv1 ⊚[ v1 ] dv2) • v1 • (dρ1 e⊚[ ρ1 ] dρ2) e⊚-fromto : ∀ Γ → (ρ1 ρ2 ρ3 : ⟦ Γ ⟧Context) (dρ1 dρ2 : ChΓ Γ) → [ Γ ]Γ dρ1 from ρ1 to ρ2 → [ Γ ]Γ dρ2 from ρ2 to ρ3 → [ Γ ]Γ (dρ1 e⊚[ ρ1 ] dρ2) from ρ1 to ρ3 e⊚-fromto ∅ ∅ ∅ ∅ ∅ ∅ v∅ v∅ = v∅ e⊚-fromto (τ • Γ) (v1 • ρ1) (v2 • ρ2) (v3 • ρ3) (dv1 • (.v1 • dρ1)) (dv2 • (.v2 • dρ2)) (dvv1 v• dρρ1) (dvv2 v• dρρ2) = ⊚-fromto v1 v2 v3 dv1 dv2 dvv1 dvv2 v• e⊚-fromto Γ ρ1 ρ2 ρ3 dρ1 dρ2 dρρ1 dρρ2 isEnvCompCS Γ = record { isChangeStructure = record { _⊕_ = _e⊕_ ; fromto→⊕ = efromto→⊕ ; _⊝_ = _e⊝_ ; ⊝-fromto = e⊝-fromto } ; _⊚[_]_ = _e⊚[_]_ ; ⊚-fromto = e⊚-fromto Γ } changeStructureΓ : ∀ Γ → ChangeStructure ⟦ Γ ⟧Context changeStructureΓ Γ = record { isCompChangeStructure = isEnvCompCS Γ } instance ichangeStructureΓ : ∀ {Γ} → ChangeStructure ⟦ Γ ⟧Context ichangeStructureΓ {Γ} = changeStructureΓ Γ changeStructureΓτ : ∀ Γ τ → ChangeStructure (⟦ Γ ⟧Context → ⟦ τ ⟧Type) changeStructureΓτ Γ τ = funCS instance ichangeStructureΓτ : ∀ {Γ τ} → ChangeStructure (⟦ Γ ⟧Context → ⟦ τ ⟧Type) ichangeStructureΓτ {Γ} {τ} = changeStructureΓτ Γ τ
module Thesis.LangChanges where open import Thesis.Changes open import Thesis.IntChanges open import Thesis.Lang open import Relation.Binary.PropositionalEquality Chτ : (τ : Type) → Set Chτ τ = ⟦ Δt τ ⟧Type ΔΓ : Context → Context ΔΓ ∅ = ∅ ΔΓ (τ • Γ) = Δt τ • τ • ΔΓ Γ ChΓ : ∀ (Γ : Context) → Set ChΓ Γ = ⟦ ΔΓ Γ ⟧Context [_]τ_from_to_ : ∀ (τ : Type) → (dv : Chτ τ) → (v1 v2 : ⟦ τ ⟧Type) → Set isCompChangeStructureτ : ∀ τ → IsCompChangeStructure ⟦ τ ⟧Type ⟦ Δt τ ⟧Type [ τ ]τ_from_to_ changeStructureτ : ∀ τ → ChangeStructure ⟦ τ ⟧Type changeStructureτ τ = record { isCompChangeStructure = isCompChangeStructureτ τ } instance ichangeStructureτ : ∀ {τ} → ChangeStructure ⟦ τ ⟧Type ichangeStructureτ {τ} = changeStructureτ τ [ σ ⇒ τ ]τ df from f1 to f2 = ∀ (da : Chτ σ) (a1 a2 : ⟦ σ ⟧Type) → [ σ ]τ da from a1 to a2 → [ τ ]τ df a1 da from f1 a1 to f2 a2 [ int ]τ dv from v1 to v2 = v1 + dv ≡ v2 [ pair σ τ ]τ (da , db) from (a1 , b1) to (a2 , b2) = [ σ ]τ da from a1 to a2 × [ τ ]τ db from b1 to b2 [ sum σ τ ]τ dv from v1 to v2 = sch_from_to_ dv v1 v2 isCompChangeStructureτ (σ ⇒ τ) = ChangeStructure.isCompChangeStructure (funCS {{changeStructureτ σ}} {{changeStructureτ τ}}) isCompChangeStructureτ int = ChangeStructure.isCompChangeStructure intCS isCompChangeStructureτ (pair σ τ) = ChangeStructure.isCompChangeStructure (pairCS {{changeStructureτ σ}} {{changeStructureτ τ}}) isCompChangeStructureτ (sum σ τ) = ChangeStructure.isCompChangeStructure ((sumCS {{changeStructureτ σ}} {{changeStructureτ τ}})) open import Data.Unit -- We can define an alternative semantics for contexts, that defines environments as nested products: ⟦_⟧Context-prod : Context → Set ⟦ ∅ ⟧Context-prod = ⊤ ⟦ τ • Γ ⟧Context-prod = ⟦ τ ⟧Type × ⟦ Γ ⟧Context-prod -- And define a change structure for such environments, reusing the change -- structure constructor for pairs. However, this change structure does not -- duplicate base values in the environment. We probably *could* define a -- different change structure for pairs that gives the right result. changeStructureΓ-old : ∀ Γ → ChangeStructure ⟦ Γ ⟧Context-prod changeStructureΓ-old ∅ = unitCS changeStructureΓ-old (τ • Γ) = pairCS {{changeStructureτ τ}} {{changeStructureΓ-old Γ}} data [_]Γ_from_to_ : ∀ Γ → ChΓ Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set where v∅ : [ ∅ ]Γ ∅ from ∅ to ∅ _v•_ : ∀ {τ Γ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ]τ dv from v1 to v2) → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → [ τ • Γ ]Γ (dv • v1 • dρ) from (v1 • ρ1) to (v2 • ρ2) module _ where _e⊕_ : ∀ {Γ} → ⟦ Γ ⟧Context → ChΓ Γ → ⟦ Γ ⟧Context _e⊕_ ∅ ∅ = ∅ _e⊕_ (v • ρ) (dv • _ • dρ) = v ⊕ dv • ρ e⊕ dρ _e⊝_ : ∀ {Γ} → ⟦ Γ ⟧Context → ⟦ Γ ⟧Context → ChΓ Γ _e⊝_ ∅ ∅ = ∅ _e⊝_ (v₂ • ρ₂) (v₁ • ρ₁) = v₂ ⊝ v₁ • v₁ • ρ₂ e⊝ ρ₁ isEnvCompCS : ∀ Γ → IsCompChangeStructure ⟦ Γ ⟧Context (ChΓ Γ) [ Γ ]Γ_from_to_ efromto→⊕ : ∀ {Γ} (dρ : ChΓ Γ) (ρ1 ρ2 : ⟦ Γ ⟧Context) → [ Γ ]Γ dρ from ρ1 to ρ2 → (ρ1 e⊕ dρ) ≡ ρ2 efromto→⊕ .∅ .∅ .∅ v∅ = refl efromto→⊕ .(_ • _ • _) .(_ • _) .(_ • _) (dvv v• dρρ) = cong₂ _•_ (fromto→⊕ _ _ _ dvv) (efromto→⊕ _ _ _ dρρ) e⊝-fromto : ∀ {Γ} → (ρ1 ρ2 : ⟦ Γ ⟧Context) → [ Γ ]Γ ρ2 e⊝ ρ1 from ρ1 to ρ2 e⊝-fromto ∅ ∅ = v∅ e⊝-fromto (v1 • ρ1) (v2 • ρ2) = ⊝-fromto v1 v2 v• e⊝-fromto ρ1 ρ2 _e⊚[_]_ : ∀ {Γ} → ChΓ Γ → ⟦ Γ ⟧Context → ChΓ Γ → ChΓ Γ _e⊚[_]_ {∅} ∅ ∅ ∅ = ∅ _e⊚[_]_ {τ • Γ} (dv1 • _ • dρ1) (v1 • ρ1) (dv2 • _ • dρ2) = (dv1 ⊚[ v1 ] dv2) • v1 • (dρ1 e⊚[ ρ1 ] dρ2) e⊚-fromto : ∀ Γ → (ρ1 ρ2 ρ3 : ⟦ Γ ⟧Context) (dρ1 dρ2 : ChΓ Γ) → [ Γ ]Γ dρ1 from ρ1 to ρ2 → [ Γ ]Γ dρ2 from ρ2 to ρ3 → [ Γ ]Γ (dρ1 e⊚[ ρ1 ] dρ2) from ρ1 to ρ3 e⊚-fromto ∅ ∅ ∅ ∅ ∅ ∅ v∅ v∅ = v∅ e⊚-fromto (τ • Γ) (v1 • ρ1) (v2 • ρ2) (v3 • ρ3) (dv1 • (.v1 • dρ1)) (dv2 • (.v2 • dρ2)) (dvv1 v• dρρ1) (dvv2 v• dρρ2) = ⊚-fromto v1 v2 v3 dv1 dv2 dvv1 dvv2 v• e⊚-fromto Γ ρ1 ρ2 ρ3 dρ1 dρ2 dρρ1 dρρ2 isEnvCompCS Γ = record { isChangeStructure = record { _⊕_ = _e⊕_ ; fromto→⊕ = efromto→⊕ ; _⊝_ = _e⊝_ ; ⊝-fromto = e⊝-fromto } ; _⊚[_]_ = _e⊚[_]_ ; ⊚-fromto = e⊚-fromto Γ } changeStructureΓ : ∀ Γ → ChangeStructure ⟦ Γ ⟧Context changeStructureΓ Γ = record { isCompChangeStructure = isEnvCompCS Γ } instance ichangeStructureΓ : ∀ {Γ} → ChangeStructure ⟦ Γ ⟧Context ichangeStructureΓ {Γ} = changeStructureΓ Γ changeStructureΓτ : ∀ Γ τ → ChangeStructure (⟦ Γ ⟧Context → ⟦ τ ⟧Type) changeStructureΓτ Γ τ = funCS instance ichangeStructureΓτ : ∀ {Γ τ} → ChangeStructure (⟦ Γ ⟧Context → ⟦ τ ⟧Type) ichangeStructureΓτ {Γ} {τ} = changeStructureΓτ Γ τ
Revert "Show we can define e⊕ using base values in changes"
Revert "Show we can define e⊕ using base values in changes" That was a demonstration, but the old definition is the one I prefer. This reverts commit 47190645cc58ddea24ef59e6877fda0473ae4818.
Agda
mit
inc-lc/ilc-agda
4c8990f1323496415af84ce8c49e276f0c366745
Base/Syntax/Context.agda
Base/Syntax/Context.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Variables and contexts -- -- This module defines the syntax of contexts, prefixes of -- contexts and variables and properties of these notions. -- -- This module is parametric in the syntax of types, so it -- can be reused for different calculi. -- ------------------------------------------------------------------------ module Base.Syntax.Context (Type : Set) where open import Relation.Binary open import Relation.Binary.PropositionalEquality -- TYPING CONTEXTS -- Syntax import Data.List as List open List public using () renaming ( [] to ∅ ; _∷_ to _•_ ; map to mapContext ) Context : Set Context = List.List Type -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ -- WEAKENING -- CONTEXT PREFIXES -- -- Useful for making lemmas strong enough to prove by induction. -- -- Consider using the Subcontexts module instead. module Prefixes where -- Prefix of a context data Prefix : Context → Set where ∅ : ∀ {Γ} → Prefix Γ _•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ) -- take only the prefix of a context take : (Γ : Context) → Prefix Γ → Context take Γ ∅ = ∅ take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′ -- drop the prefix of a context drop : (Γ : Context) → Prefix Γ → Context drop Γ ∅ = Γ drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′ -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₂ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂) take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ take-drop ∅ ∅ = refl take-drop (τ • Γ) ∅ = refl take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ or-unit ∅ = refl or-unit (τ • Γ) rewrite or-unit Γ = refl move-prefix : ∀ Γ τ Γ′ → Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′ move-prefix ∅ τ Γ′ = refl move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅))) move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- SUBCONTEXTS -- -- Useful as a reified weakening operation, -- and for making theorems strong enough to prove by induction. -- -- The contents of this module are currently exported at the end -- of this file. module Subcontexts where infix 4 _≼_ data _≼_ : (Γ₁ Γ₂ : Context) → Set where ∅ : ∅ ≼ ∅ keep_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → τ • Γ₁ ≼ τ • Γ₂ drop_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → Γ₁ ≼ τ • Γ₂ -- Properties ∅≼Γ : ∀ {Γ} → ∅ ≼ Γ ∅≼Γ {∅} = ∅ ∅≼Γ {τ • Γ} = drop τ • ∅≼Γ ≼-refl : Reflexive _≼_ ≼-refl {∅} = ∅ ≼-refl {τ • Γ} = keep τ • ≼-refl ≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂ ≼-reflexive refl = ≼-refl ≼-trans : Transitive _≼_ ≼-trans ≼₁ ∅ = ≼₁ ≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂ ≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-isPreorder : IsPreorder _≡_ _≼_ ≼-isPreorder = record { isEquivalence = isEquivalence ; reflexive = ≼-reflexive ; trans = ≼-trans } ≼-preorder : Preorder _ _ _ ≼-preorder = record { Carrier = Context ; _≈_ = _≡_ ; _∼_ = _≼_ ; isPreorder = ≼-isPreorder } module ≼-Reasoning where open import Relation.Binary.PreorderReasoning ≼-preorder public renaming ( _≈⟨_⟩_ to _≡⟨_⟩_ ; _∼⟨_⟩_ to _≼⟨_⟩_ ; _≈⟨⟩_ to _≡⟨⟩_ ) -- Lift a variable to a super context weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ weaken-var (keep τ • ≼₁) this = this weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x) weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x) -- Currently, we export the subcontext relation as well as the -- definition of _⋎_. open Subcontexts public open Prefixes public using (_⋎_)
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Variables and contexts -- -- This module defines the syntax of contexts, prefixes of -- contexts and variables and properties of these notions. -- -- This module is parametric in the syntax of types, so it -- can be reused for different calculi. ------------------------------------------------------------------------ module Base.Syntax.Context (Type : Set) where open import Relation.Binary open import Relation.Binary.PropositionalEquality -- Typing Contexts -- =============== import Data.List as List open List public using () renaming ( [] to ∅ ; _∷_ to _•_ ; map to mapContext ) Context : Set Context = List.List Type -- Variables -- ========= -- -- Here it is clear that we are using de Bruijn indices, -- encoded as natural numbers, more or less. data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ -- Weakening -- ========= -- -- We provide two approaches for weakening: context prefixes and -- and subcontext relationship. In this formalization, we mostly -- (maybe exclusively?) use the latter. -- Context Prefixes -- ---------------- -- -- Useful for making lemmas strong enough to prove by induction. -- -- Consider using the Subcontexts module instead. module Prefixes where -- Prefix of a context data Prefix : Context → Set where ∅ : ∀ {Γ} → Prefix Γ _•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ) -- take only the prefix of a context take : (Γ : Context) → Prefix Γ → Context take Γ ∅ = ∅ take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′ -- drop the prefix of a context drop : (Γ : Context) → Prefix Γ → Context drop Γ ∅ = Γ drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′ -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₂ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂) take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ take-drop ∅ ∅ = refl take-drop (τ • Γ) ∅ = refl take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ or-unit ∅ = refl or-unit (τ • Γ) rewrite or-unit Γ = refl move-prefix : ∀ Γ τ Γ′ → Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′ move-prefix ∅ τ Γ′ = refl move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅))) move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- Subcontexts -- ----------- -- -- Useful as a reified weakening operation, -- and for making theorems strong enough to prove by induction. -- -- The contents of this module are currently exported at the end -- of this file. module Subcontexts where infix 4 _≼_ data _≼_ : (Γ₁ Γ₂ : Context) → Set where ∅ : ∅ ≼ ∅ keep_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → τ • Γ₁ ≼ τ • Γ₂ drop_•_ : ∀ {Γ₁ Γ₂} → (τ : Type) → Γ₁ ≼ Γ₂ → Γ₁ ≼ τ • Γ₂ -- Properties ∅≼Γ : ∀ {Γ} → ∅ ≼ Γ ∅≼Γ {∅} = ∅ ∅≼Γ {τ • Γ} = drop τ • ∅≼Γ ≼-refl : Reflexive _≼_ ≼-refl {∅} = ∅ ≼-refl {τ • Γ} = keep τ • ≼-refl ≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂ ≼-reflexive refl = ≼-refl ≼-trans : Transitive _≼_ ≼-trans ≼₁ ∅ = ≼₁ ≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂ ≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂ ≼-isPreorder : IsPreorder _≡_ _≼_ ≼-isPreorder = record { isEquivalence = isEquivalence ; reflexive = ≼-reflexive ; trans = ≼-trans } ≼-preorder : Preorder _ _ _ ≼-preorder = record { Carrier = Context ; _≈_ = _≡_ ; _∼_ = _≼_ ; isPreorder = ≼-isPreorder } module ≼-Reasoning where open import Relation.Binary.PreorderReasoning ≼-preorder public renaming ( _≈⟨_⟩_ to _≡⟨_⟩_ ; _∼⟨_⟩_ to _≼⟨_⟩_ ; _≈⟨⟩_ to _≡⟨⟩_ ) -- Lift a variable to a super context weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ weaken-var (keep τ • ≼₁) this = this weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x) weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x) -- Currently, we export the subcontext relation as well as the -- definition of _⋎_. open Subcontexts public open Prefixes public using (_⋎_)
Improve commentary in Base.Syntax.Context.
Improve commentary in Base.Syntax.Context. Old-commit-hash: 4886ab0c5f6e2cdc47142a8ae0c717890f449f93
Agda
mit
inc-lc/ilc-agda
a3b1db76fbdbf97e5638eca08c0475817ff1afca
lib/Algebra/Monoid.agda
lib/Algebra/Monoid.agda
{-# OPTIONS --without-K #-} open import Type using () renaming (Type_ to Type) open import Level.NP open import Function.NP open import Data.Product.NP open import Data.Nat using (ℕ; zero) renaming (_+_ to _+ℕ_; _*_ to _*ℕ_; suc to 1+_) import Data.Nat.Properties.Simple as ℕ° open import Data.Integer.NP using (ℤ; +_; -[1+_]; _⊖_; -_; module ℤ°) renaming ( _+_ to _+ℤ_; _-_ to _−ℤ_; _*_ to _*ℤ_ ; suc to sucℤ; pred to predℤ ) open import Relation.Binary.PropositionalEquality.NP renaming (_∙_ to _♦_) import Algebra.FunctionProperties.Eq open Algebra.FunctionProperties.Eq.Implicits open ≡-Reasoning module Algebra.Monoid where record Monoid-Ops {ℓ} (M : Type ℓ) : Type ℓ where constructor _,_ infixl 7 _∙_ field _∙_ : M → M → M ε : M open From-Op₂ _∙_ public renaming (op= to ∙=) open From-Monoid-Ops ε _∙_ public record Monoid-Struct {ℓ} {M : Type ℓ} (mon-ops : Monoid-Ops M) : Type ℓ where constructor _,_ open Monoid-Ops mon-ops -- laws field assocs : Associative _∙_ × Associative (flip _∙_) identity : Identity ε _∙_ ε∙-identity : LeftIdentity ε _∙_ ε∙-identity = fst identity ∙ε-identity : RightIdentity ε _∙_ ∙ε-identity = snd identity assoc = fst assocs !assoc = snd assocs open From-Assoc assoc public hiding (assocs) open From-Assoc-LeftIdentity assoc ε∙-identity public open From-Assoc-RightIdentity assoc ∙ε-identity public record Monoid {ℓ}(M : Type ℓ) : Type ℓ where constructor _,_ field mon-ops : Monoid-Ops M mon-struct : Monoid-Struct mon-ops open Monoid-Ops mon-ops public open Monoid-Struct mon-struct public -- A renaming of Monoid-Ops with additive notation module Additive-Monoid-Ops {ℓ}{M : Set ℓ} (mon : Monoid-Ops M) where private module M = Monoid-Ops mon using () renaming ( _∙_ to _+_ ; ε to 0# ; _² to 2⊗_ ; _³ to 3⊗_ ; _⁴ to 4⊗_ ; _^¹⁺_ to _⊗¹⁺_ ; _^⁺_ to _⊗⁺_ ; ∙= to += ) open M public using (0#; +=) infixl 6 _+_ infixl 7 _⊗¹⁺_ _⊗⁺_ 2⊗_ 3⊗_ 4⊗_ _+_ = M._+_ _⊗¹⁺_ = M._⊗¹⁺_ _⊗⁺_ = M._⊗⁺_ 2⊗_ = M.2⊗_ 3⊗_ = M.3⊗_ 4⊗_ = M.4⊗_ -- A renaming of Monoid-Struct with additive notation module Additive-Monoid-Struct {ℓ}{M : Type ℓ}{mon-ops : Monoid-Ops M} (mon-struct : Monoid-Struct mon-ops) = Monoid-Struct mon-struct renaming ( identity to +-identity ; ε∙-identity to 0+-identity ; ∙ε-identity to +0-identity ; assocs to +-assocs ; assoc to +-assoc ; !assoc to +-!assoc ; assoc= to +-assoc= ; !assoc= to +-!assoc= ; inner= to +-inner= ) -- A renaming of Monoid with additive notation module Additive-Monoid {ℓ}{M : Type ℓ} (mon : Monoid M) where open Additive-Monoid-Ops (Monoid.mon-ops mon) public open Additive-Monoid-Struct (Monoid.mon-struct mon) public -- A renaming of Monoid-Ops with multiplicative notation module Multiplicative-Monoid-Ops {ℓ}{M : Type ℓ} (mon-ops : Monoid-Ops M) = Monoid-Ops mon-ops renaming ( _∙_ to _*_ ; ε to 1# ; ∙= to *= ) -- A renaming of Monoid-Struct with multiplicative notation module Multiplicative-Monoid-Struct {ℓ}{M : Type ℓ}{mon-ops : Monoid-Ops M} (mon-struct : Monoid-Struct mon-ops) = Monoid-Struct mon-struct renaming ( identity to *-identity ; ε∙-identity to 1*-identity ; ∙ε-identity to *1-identity ; assocs to *-assocs ; assoc to *-assoc ; !assoc to *-!assoc ; assoc= to *-assoc= ; !assoc= to *-!assoc= ; inner= to *-inner= ) -- A renaming of Monoid with multiplicative notation module Multiplicative-Monoid {ℓ}{M : Type ℓ} (mon : Monoid M) where open Multiplicative-Monoid-Ops (Monoid.mon-ops mon) public open Multiplicative-Monoid-Struct (Monoid.mon-struct mon) public module Monoidᵒᵖ {ℓ}{M : Type ℓ} where _ᵒᵖ-ops : Monoid-Ops M → Monoid-Ops M (_∙_ , ε) ᵒᵖ-ops = flip _∙_ , ε _ᵒᵖ-struct : {mon : Monoid-Ops M} → Monoid-Struct mon → Monoid-Struct (mon ᵒᵖ-ops) (assocs , identities) ᵒᵖ-struct = (swap assocs , swap identities) _ᵒᵖ : Monoid M → Monoid M (ops , struct)ᵒᵖ = _ , struct ᵒᵖ-struct ᵒᵖ∘ᵒᵖ-id : ∀ {mon} → (mon ᵒᵖ) ᵒᵖ ≡ mon ᵒᵖ∘ᵒᵖ-id = idp module _ {a}{A : Type a}(_∙_ : Op₂ A)(assoc : Associative _∙_) where from-assoc = From-Op₂.From-Assoc.assocs _∙_ assoc --import Data.Vec.NP as V {- module _ {A B : Type} where (f : Vec : Vec M n) → f ⊛ x (∀ i → ) module VecMonoid {M : Type} (mon : Monoid M) where open V open Monoid mon module _ n where ×-mon-ops : Monoid-Ops (Vec M n) ×-mon-ops = zipWith _∙_ , replicate ε ×-mon-struct : Monoid-Struct ×-mon-ops ×-mon-struct = (λ {x}{y}{z} → {!replicate ? ⊛ ?!}) , {!!} , {!!} -} module MonoidProduct {a}{A : Type a}{b}{B : Type b} (monA0+ : Monoid A)(monB1* : Monoid B) where open Additive-Monoid monA0+ open Multiplicative-Monoid monB1* ×-mon-ops : Monoid-Ops (A × B) ×-mon-ops = zip _+_ _*_ , 0# , 1# ×-mon-struct : Monoid-Struct ×-mon-ops ×-mon-struct = (ap₂ _,_ +-assoc *-assoc , ap₂ _,_ +-!assoc *-!assoc) , ap₂ _,_ (fst +-identity) (fst *-identity) , ap₂ _,_ (snd +-identity) (snd *-identity) ×-mon : Monoid (A × B) ×-mon = ×-mon-ops , ×-mon-struct open Monoid ×-mon public -- -} -- -} -- -} -- -}
{-# OPTIONS --without-K #-} open import Type using (Type_) open import Function.NP open import Function.Extensionality open import Data.Product.NP open import Relation.Binary.PropositionalEquality.NP using (_≡_; idp; ap; ap₂) import Algebra.FunctionProperties.Eq open Algebra.FunctionProperties.Eq.Implicits module Algebra.Monoid where record Monoid-Ops {ℓ} (M : Type ℓ) : Type ℓ where constructor _,_ infixl 7 _∙_ field _∙_ : M → M → M ε : M open From-Op₂ _∙_ public renaming (op= to ∙=) open From-Monoid-Ops ε _∙_ public record Monoid-Struct {ℓ} {M : Type ℓ} (mon-ops : Monoid-Ops M) : Type ℓ where constructor _,_ open Monoid-Ops mon-ops -- laws field assocs : Associative _∙_ × Associative (flip _∙_) identity : Identity ε _∙_ ε∙-identity : LeftIdentity ε _∙_ ε∙-identity = fst identity ∙ε-identity : RightIdentity ε _∙_ ∙ε-identity = snd identity assoc = fst assocs !assoc = snd assocs open From-Assoc assoc public hiding (assocs) open From-Assoc-LeftIdentity assoc ε∙-identity public open From-Assoc-RightIdentity assoc ∙ε-identity public record Monoid {ℓ}(M : Type ℓ) : Type ℓ where constructor _,_ field mon-ops : Monoid-Ops M mon-struct : Monoid-Struct mon-ops open Monoid-Ops mon-ops public open Monoid-Struct mon-struct public -- A renaming of Monoid-Ops with additive notation module Additive-Monoid-Ops {ℓ}{M : Set ℓ} (mon : Monoid-Ops M) where private module M = Monoid-Ops mon using () renaming ( _∙_ to _+_ ; ε to 0# ; _² to 2⊗_ ; _³ to 3⊗_ ; _⁴ to 4⊗_ ; _^¹⁺_ to _⊗¹⁺_ ; _^⁺_ to _⊗⁺_ ; ∙= to += ) open M public using (0#; +=) infixl 6 _+_ infixl 7 _⊗¹⁺_ _⊗⁺_ 2⊗_ 3⊗_ 4⊗_ _+_ = M._+_ _⊗¹⁺_ = M._⊗¹⁺_ _⊗⁺_ = M._⊗⁺_ 2⊗_ = M.2⊗_ 3⊗_ = M.3⊗_ 4⊗_ = M.4⊗_ -- A renaming of Monoid-Struct with additive notation module Additive-Monoid-Struct {ℓ}{M : Type ℓ}{mon-ops : Monoid-Ops M} (mon-struct : Monoid-Struct mon-ops) = Monoid-Struct mon-struct renaming ( identity to +-identity ; ε∙-identity to 0+-identity ; ∙ε-identity to +0-identity ; assocs to +-assocs ; assoc to +-assoc ; !assoc to +-!assoc ; assoc= to +-assoc= ; !assoc= to +-!assoc= ; inner= to +-inner= ) -- A renaming of Monoid with additive notation module Additive-Monoid {ℓ}{M : Type ℓ} (mon : Monoid M) where open Additive-Monoid-Ops (Monoid.mon-ops mon) public open Additive-Monoid-Struct (Monoid.mon-struct mon) public -- A renaming of Monoid-Ops with multiplicative notation module Multiplicative-Monoid-Ops {ℓ}{M : Type ℓ} (mon-ops : Monoid-Ops M) = Monoid-Ops mon-ops renaming ( _∙_ to _*_ ; ε to 1# ; ∙= to *= ) -- A renaming of Monoid-Struct with multiplicative notation module Multiplicative-Monoid-Struct {ℓ}{M : Type ℓ}{mon-ops : Monoid-Ops M} (mon-struct : Monoid-Struct mon-ops) = Monoid-Struct mon-struct renaming ( identity to *-identity ; ε∙-identity to 1*-identity ; ∙ε-identity to *1-identity ; assocs to *-assocs ; assoc to *-assoc ; !assoc to *-!assoc ; assoc= to *-assoc= ; !assoc= to *-!assoc= ; inner= to *-inner= ) -- A renaming of Monoid with multiplicative notation module Multiplicative-Monoid {ℓ}{M : Type ℓ} (mon : Monoid M) where open Multiplicative-Monoid-Ops (Monoid.mon-ops mon) public open Multiplicative-Monoid-Struct (Monoid.mon-struct mon) public module Monoidᵒᵖ {ℓ}{M : Type ℓ} where _ᵒᵖ-ops : Monoid-Ops M → Monoid-Ops M (_∙_ , ε) ᵒᵖ-ops = flip _∙_ , ε _ᵒᵖ-struct : {mon : Monoid-Ops M} → Monoid-Struct mon → Monoid-Struct (mon ᵒᵖ-ops) (assocs , identities) ᵒᵖ-struct = (swap assocs , swap identities) _ᵒᵖ : Monoid M → Monoid M (ops , struct)ᵒᵖ = _ , struct ᵒᵖ-struct ᵒᵖ∘ᵒᵖ-id : ∀ {mon} → (mon ᵒᵖ) ᵒᵖ ≡ mon ᵒᵖ∘ᵒᵖ-id = idp module _ {a}{A : Type a}(_∙_ : Op₂ A)(assoc : Associative _∙_) where from-assoc = From-Op₂.From-Assoc.assocs _∙_ assoc --import Data.Vec.NP as V {- module _ {A B : Type} where (f : Vec : Vec M n) → f ⊛ x (∀ i → ) module VecMonoid {M : Type} (mon : Monoid M) where open V open Monoid mon module _ n where ×-mon-ops : Monoid-Ops (Vec M n) ×-mon-ops = zipWith _∙_ , replicate ε ×-mon-struct : Monoid-Struct ×-mon-ops ×-mon-struct = (λ {x}{y}{z} → {!replicate ? ⊛ ?!}) , {!!} , {!!} -} -- The monoidal structure of endomorphisms module _ {a}(A : Type a) where ∘-mon-ops : Monoid-Ops (Endo A) ∘-mon-ops = _∘′_ , id ∘-mon-struct : Monoid-Struct ∘-mon-ops ∘-mon-struct = (idp , idp) , (idp , idp) ∘-mon : Monoid (Endo A) ∘-mon = ∘-mon-ops , ∘-mon-struct module Product {a}{A : Type a}{b}{B : Type b} (𝔸 : Monoid A)(𝔹 : Monoid B) where open Additive-Monoid 𝔸 open Multiplicative-Monoid 𝔹 ×-mon-ops : Monoid-Ops (A × B) ×-mon-ops = zip _+_ _*_ , 0# , 1# ×-mon-struct : Monoid-Struct ×-mon-ops ×-mon-struct = (ap₂ _,_ +-assoc *-assoc , ap₂ _,_ +-!assoc *-!assoc) , ap₂ _,_ (fst +-identity) (fst *-identity) , ap₂ _,_ (snd +-identity) (snd *-identity) ×-mon : Monoid (A × B) ×-mon = ×-mon-ops , ×-mon-struct open Monoid ×-mon public {- This module shows how properties of a monoid 𝕄 are carried on functions from any type A to 𝕄. However since the function type can be dependent, this also generalises the product of monoids (since Π 𝟚 [ A , B ] ≃ A × B). -} module Pointwise {{_ : FunExt}}{a}(A : Type a){m}{M : A → Type m} (𝕄 : (x : A) → Monoid (M x)) where private module 𝕄 {x} = Monoid (𝕄 x) open 𝕄 hiding (mon-ops; mon-struct) ⟨ε⟩ : Π A M ⟨ε⟩ = λ _ → ε _⟨∙⟩_ : Op₂ (Π A M) (f ⟨∙⟩ g) x = f x ∙ g x mon-ops : Monoid-Ops (Π A M) mon-ops = _⟨∙⟩_ , ⟨ε⟩ mon-struct : Monoid-Struct mon-ops mon-struct = (λ=ⁱ assoc , λ=ⁱ !assoc) , λ=ⁱ ε∙-identity , λ=ⁱ ∙ε-identity mon : Monoid (Π A M) mon = mon-ops , mon-struct open Monoid mon public hiding (mon-ops; mon-struct) -- Non-dependent version of Pointwise′ module Pointwise′ {{_ : FunExt}}{a}(A : Type a){m}{M : Type m}(𝕄 : Monoid M) = Pointwise A (λ _ → 𝕄) {- OR open Monoid 𝕄 hiding (mon-ops; mon-struct) ⟨ε⟩ : A → M ⟨ε⟩ = λ _ → ε _⟨∙⟩_ : Op₂ (A → M) (f ⟨∙⟩ g) x = f x ∙ g x mon-ops : Monoid-Ops (A → M) mon-ops = _⟨∙⟩_ , ⟨ε⟩ mon-struct : Monoid-Struct mon-ops mon-struct = (λ=ⁱ assoc , λ=ⁱ !assoc) , λ=ⁱ ε∙-identity , λ=ⁱ ∙ε-identity mon : Monoid (A → M) mon = mon-ops , mon-struct open Monoid mon public hiding (mon-ops; mon-struct) -} -- -} -- -} -- -} -- -}
add the Pointwise construction
Monoid: add the Pointwise construction
Agda
bsd-3-clause
crypto-agda/agda-nplib
b6915db12b7c450d87def9967c9830d36ca626b4
Parametric/Change/Specification.agda
Parametric/Change/Specification.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change evaluation (Def 3.6 and Fig. 4h). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) {{validity-structure : Validity.Structure ⟦_⟧Base}} where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Theorem.CongApp open import Postulate.Extensionality module Structure where --------------- -- Interface -- --------------- -- We provide: Derivatives of terms. ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) -- And we provide correctness proofs about the derivatives. correctness : ∀ {τ Γ} (t : Term Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ (⟦ const c ⟧Term ρ) ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record { apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ) ; correct = λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎ } where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f₁ = ⟦ s ⟧ ρ f₂ = ⟦ s ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ u₁ = ⟦ t ⟧ ρ u₂ = ⟦ t ⟧ (after-env dρ) Δu = ⟦ t ⟧Δ ρ dρ in begin f₁ u₁ ⊞₍ τ ₎ call-change {σ} {τ} Δf u₁ Δu ≡⟨ sym (is-valid {σ} {τ} Δf u₁ Δu) ⟩ (f₁ ⊞₍ σ ⇒ τ ₎ Δf) (u₁ ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ f₂ u₂ ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change evaluation (Def 3.6 and Fig. 4h). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) {{validity-structure : Validity.Structure ⟦_⟧Base}} where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Theorem.CongApp open import Postulate.Extensionality module Structure where --------------- -- Interface -- --------------- -- We provide: Derivatives of terms. ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) -- And we provide correctness proofs about the derivatives. correctness : ∀ {τ Γ} (t : Term Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ (⟦ c ⟧Const) ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record { apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ) ; correct = λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎ } where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f₁ = ⟦ s ⟧ ρ f₂ = ⟦ s ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ u₁ = ⟦ t ⟧ ρ u₂ = ⟦ t ⟧ (after-env dρ) Δu = ⟦ t ⟧Δ ρ dρ in begin f₁ u₁ ⊞₍ τ ₎ call-change {σ} {τ} Δf u₁ Δu ≡⟨ sym (is-valid {σ} {τ} Δf u₁ Δu) ⟩ (f₁ ⊞₍ σ ⇒ τ ₎ Δf) (u₁ ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ f₂ u₂ ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
Simplify change semantics
Simplify change semantics
Agda
mit
inc-lc/ilc-agda
5c76b2f48299a010ec3ccf23921c0a130d17d754
New/NewNew.agda
New/NewNew.agda
module New.NewNew where open import New.Changes open import New.LangChanges open import New.Lang open import New.Types open import New.Derive [_]_from_to_ : ∀ (τ : Type) → (dv : Cht τ) → (v1 v2 : ⟦ τ ⟧Type) → Set [ σ ⇒ τ ] df from f1 to f2 = ∀ (da : Cht σ) (a1 a2 : ⟦ σ ⟧Type) → [ σ ] da from a1 to a2 → [ τ ] df a1 da from f1 a1 to f2 a2 [ int ] dv from v1 to v2 = v2 ≡ v1 + dv [ pair σ τ ] (da , db) from (a1 , b1) to (a2 , b2) = [ σ ] da from a1 to a2 × [ τ ] db from b1 to b2 data [_]Γ_from_to_ : ∀ Γ → eCh Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set where v∅ : [ ∅ ]Γ ∅ from ∅ to ∅ _v•_ : ∀ {τ Γ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ] dv from v1 to v2) → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → [ τ • Γ ]Γ (dv • v1 • dρ) from (v1 • ρ1) to (v2 • ρ2) ⟦Γ≼ΔΓ⟧ : ∀ {Γ ρ1 ρ2 dρ} → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → ρ1 ≡ ⟦ Γ≼ΔΓ ⟧≼ dρ ⟦Γ≼ΔΓ⟧ v∅ = refl ⟦Γ≼ΔΓ⟧ (dvv v• dρρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ dρρ) fit-sound : ∀ {Γ τ} → (t : Term Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → ⟦ t ⟧Term ρ1 ≡ ⟦ fit t ⟧Term dρ fit-sound t dρρ = trans (cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ dρρ)) (sym (weaken-sound t _)) fromtoDeriveVar : ∀ {Γ τ} → (x : Var Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ x ⟧ΔVar ρ1 dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2) fromtoDeriveVar this (dvv v• dρρ) = dvv fromtoDeriveVar (that x) (dvv v• dρρ) = fromtoDeriveVar x dρρ fromtoDeriveConst : ∀ {τ} c → [ τ ] ⟦ deriveConst c ⟧Term ∅ from ⟦ c ⟧Const to ⟦ c ⟧Const fromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn·pq=mp·nq {a1} {da} {b1} {db} fromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m·-n=-mn {b1} {db}) = mn·pq=mp·nq {a1} {da} { - b1} { - db} fromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb fromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa fromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb fromtoDerive : ∀ {Γ} τ → (t : Term Γ τ) → {dρ : eCh Γ} {ρ1 ρ2 : ⟦ Γ ⟧Context} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ t ⟧ΔTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2) fromtoDerive τ (const c) {dρ} {ρ1} dρρ rewrite ⟦ c ⟧ΔConst-rewrite ρ1 dρ = fromtoDeriveConst c fromtoDerive τ (var x) dρρ = fromtoDeriveVar x dρρ fromtoDerive τ (app {σ} s t) dρρ rewrite sym (fit-sound t dρρ) = let fromToF = fromtoDerive (σ ⇒ τ) s dρρ in let fromToB = fromtoDerive σ t dρρ in fromToF _ _ _ fromToB fromtoDerive (σ ⇒ τ) (abs t) dρρ = λ dv v1 v2 dvv → fromtoDerive τ t (dvv v• dρρ) -- Now relate this validity with ⊕. To know that nil and so on are valid, also -- relate it to the other definition. open import Postulate.Extensionality fromto→⊕ : ∀ {τ} dv v1 v2 → [ τ ] dv from v1 to v2 → v1 ⊕ dv ≡ v2 ⊝-fromto : ∀ {τ} (v1 v2 : ⟦ τ ⟧Type) → [ τ ] v2 ⊝ v1 from v1 to v2 ⊝-fromto {σ ⇒ τ} f1 f2 da a1 a2 daa rewrite sym (fromto→⊕ _ _ _ daa) = ⊝-fromto (f1 a1) (f2 (a1 ⊕ da)) ⊝-fromto {int} v1 v2 = sym (update-diff v2 v1) ⊝-fromto {pair σ τ} (a1 , b1) (a2 , b2) = ⊝-fromto a1 a2 , ⊝-fromto b1 b2 nil-fromto : ∀ {τ} (v : ⟦ τ ⟧Type) → [ τ ] nil v from v to v nil-fromto v = ⊝-fromto v v fromto→⊕ {σ ⇒ τ} df f1 f2 dff = ext (λ v → fromto→⊕ {τ} (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v))) fromto→⊕ {int} dn n1 n2 refl = refl fromto→⊕ {pair σ τ} (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = cong₂ _,_ (fromto→⊕ _ _ _ daa) (fromto→⊕ _ _ _ dbb) open ≡-Reasoning -- If df is valid, prove (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da. -- This statement uses a ⊕ da instead of a2, which is not the style of this formalization but fits better with the other one. -- Instead, WellDefinedFunChangeFromTo (without prime) fits this formalization. WellDefinedFunChangeFromTo′ : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo′ f1 df = ∀ da a → [ _ ] da from a to (a ⊕ da) → WellDefinedFunChangePoint f1 df a da fromto→WellDefined′ : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo′ f1 df fromto→WellDefined′ {f1 = f1} {f2} {df} dff da a daa = begin (f1 ⊕ df) (a ⊕ da) ≡⟨⟩ f1 (a ⊕ da) ⊕ df (a ⊕ da) (nil (a ⊕ da)) ≡⟨ (fromto→⊕ (df (a ⊕ da) (nil (a ⊕ da))) _ _ (dff (nil (a ⊕ da)) _ _ (nil-fromto (a ⊕ da)))) ⟩ f2 (a ⊕ da) ≡⟨ sym (fromto→⊕ _ _ _ (dff da _ _ daa)) ⟩ f1 a ⊕ df a da ∎ WellDefinedFunChangeFromTo : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo f1 df = ∀ da a1 a2 → [ _ ] da from a1 to a2 → WellDefinedFunChangePoint f1 df a1 da fromto→WellDefined : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo f1 df fromto→WellDefined {f1 = f1} {f2} {df} dff da a1 a2 daa = fromto→WellDefined′ dff da a1 daa′ where daa′ : [ _ ] da from a1 to (a1 ⊕ da) daa′ rewrite fromto→⊕ da a1 a2 daa = daa -- Recursive isomorphism between the two validities. -- -- Among other things, valid→fromto proves that a validity-preserving function, -- with validity defined via (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da, is also valid -- in the "fromto" sense. -- -- We can't hope for a better statement, since we need the equation to be -- satisfied also by returned or argument functions. fromto→valid : ∀ {τ} → ∀ v1 v2 dv → [ τ ] dv from v1 to v2 → valid v1 dv valid→fromto : ∀ {τ} v (dv : Cht τ) → valid v dv → [ τ ] dv from v to (v ⊕ dv) fromto→valid {int} = λ v1 v2 dv x → tt fromto→valid {pair σ τ} (a1 , b1) (a2 , b2) (da , db) (daa , dbb) = (fromto→valid _ _ _ daa) , (fromto→valid _ _ _ dbb) fromto→valid {σ ⇒ τ} f1 f2 df dff = λ a da ada → fromto→valid _ _ _ (dff da a (a ⊕ da) (valid→fromto a da ada)) , fromto→WellDefined′ dff da a (valid→fromto _ _ ada) valid→fromto {int} v dv tt = refl valid→fromto {pair σ τ} (a , b) (da , db) (ada , bdb) = valid→fromto a da ada , valid→fromto b db bdb valid→fromto {σ ⇒ τ} f df fdf da a1 a2 daa = body where fa1da-valid : valid (f a1) (df a1 da) × WellDefinedFunChangePoint f df a1 da fa1da-valid = fdf a1 da (fromto→valid _ _ _ daa) body : [ τ ] df a1 da from f a1 to (f ⊕ df) a2 body rewrite sym (fromto→⊕ _ _ _ daa) | proj₂ fa1da-valid = valid→fromto (f a1) (df a1 da) (proj₁ fa1da-valid)
module New.NewNew where open import New.Changes open import New.LangChanges open import New.Lang open import New.Derive [_]_from_to_ : ∀ (τ : Type) → (dv : Cht τ) → (v1 v2 : ⟦ τ ⟧Type) → Set [ σ ⇒ τ ] df from f1 to f2 = ∀ (da : Cht σ) (a1 a2 : ⟦ σ ⟧Type) → [ σ ] da from a1 to a2 → [ τ ] df a1 da from f1 a1 to f2 a2 [ int ] dv from v1 to v2 = v2 ≡ v1 + dv [ pair σ τ ] (da , db) from (a1 , b1) to (a2 , b2) = [ σ ] da from a1 to a2 × [ τ ] db from b1 to b2 data [_]Γ_from_to_ : ∀ Γ → eCh Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set where v∅ : [ ∅ ]Γ ∅ from ∅ to ∅ _v•_ : ∀ {τ Γ dv v1 v2 dρ ρ1 ρ2} → (dvv : [ τ ] dv from v1 to v2) → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → [ τ • Γ ]Γ (dv • v1 • dρ) from (v1 • ρ1) to (v2 • ρ2) ⟦Γ≼ΔΓ⟧ : ∀ {Γ ρ1 ρ2 dρ} → (dρρ : [ Γ ]Γ dρ from ρ1 to ρ2) → ρ1 ≡ ⟦ Γ≼ΔΓ ⟧≼ dρ ⟦Γ≼ΔΓ⟧ v∅ = refl ⟦Γ≼ΔΓ⟧ (dvv v• dρρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ dρρ) fit-sound : ∀ {Γ τ} → (t : Term Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → ⟦ t ⟧Term ρ1 ≡ ⟦ fit t ⟧Term dρ fit-sound t dρρ = trans (cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ dρρ)) (sym (weaken-sound t _)) fromtoDeriveVar : ∀ {Γ τ} → (x : Var Γ τ) → ∀ {dρ ρ1 ρ2} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ x ⟧ΔVar ρ1 dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2) fromtoDeriveVar this (dvv v• dρρ) = dvv fromtoDeriveVar (that x) (dvv v• dρρ) = fromtoDeriveVar x dρρ fromtoDeriveConst : ∀ {τ} c → [ τ ] ⟦ deriveConst c ⟧Term ∅ from ⟦ c ⟧Const to ⟦ c ⟧Const fromtoDeriveConst plus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb = mn·pq=mp·nq {a1} {da} {b1} {db} fromtoDeriveConst minus da a1 a2 daa db b1 b2 dbb rewrite daa | dbb | sym (-m·-n=-mn {b1} {db}) = mn·pq=mp·nq {a1} {da} { - b1} { - db} fromtoDeriveConst cons da a1 a2 daa db b1 b2 dbb = daa , dbb fromtoDeriveConst fst (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = daa fromtoDeriveConst snd (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = dbb fromtoDerive : ∀ {Γ} τ → (t : Term Γ τ) → {dρ : eCh Γ} {ρ1 ρ2 : ⟦ Γ ⟧Context} → [ Γ ]Γ dρ from ρ1 to ρ2 → [ τ ] (⟦ t ⟧ΔTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2) fromtoDerive τ (const c) {dρ} {ρ1} dρρ rewrite ⟦ c ⟧ΔConst-rewrite ρ1 dρ = fromtoDeriveConst c fromtoDerive τ (var x) dρρ = fromtoDeriveVar x dρρ fromtoDerive τ (app {σ} s t) dρρ rewrite sym (fit-sound t dρρ) = let fromToF = fromtoDerive (σ ⇒ τ) s dρρ in let fromToB = fromtoDerive σ t dρρ in fromToF _ _ _ fromToB fromtoDerive (σ ⇒ τ) (abs t) dρρ = λ dv v1 v2 dvv → fromtoDerive τ t (dvv v• dρρ) -- Now relate this validity with ⊕. To know that nil and so on are valid, also -- relate it to the other definition. open import Postulate.Extensionality fromto→⊕ : ∀ {τ} dv v1 v2 → [ τ ] dv from v1 to v2 → v1 ⊕ dv ≡ v2 ⊝-fromto : ∀ {τ} (v1 v2 : ⟦ τ ⟧Type) → [ τ ] v2 ⊝ v1 from v1 to v2 ⊝-fromto {σ ⇒ τ} f1 f2 da a1 a2 daa rewrite sym (fromto→⊕ _ _ _ daa) = ⊝-fromto (f1 a1) (f2 (a1 ⊕ da)) ⊝-fromto {int} v1 v2 = sym (update-diff v2 v1) ⊝-fromto {pair σ τ} (a1 , b1) (a2 , b2) = ⊝-fromto a1 a2 , ⊝-fromto b1 b2 nil-fromto : ∀ {τ} (v : ⟦ τ ⟧Type) → [ τ ] nil v from v to v nil-fromto v = ⊝-fromto v v fromto→⊕ {σ ⇒ τ} df f1 f2 dff = ext (λ v → fromto→⊕ {τ} (df v (nil v)) (f1 v) (f2 v) (dff (nil v) v v (nil-fromto v))) fromto→⊕ {int} dn n1 n2 refl = refl fromto→⊕ {pair σ τ} (da , db) (a1 , b1) (a2 , b2) (daa , dbb) = cong₂ _,_ (fromto→⊕ _ _ _ daa) (fromto→⊕ _ _ _ dbb) open ≡-Reasoning -- If df is valid, prove (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da. -- This statement uses a ⊕ da instead of a2, which is not the style of this formalization but fits better with the other one. -- Instead, WellDefinedFunChangeFromTo (without prime) fits this formalization. WellDefinedFunChangeFromTo′ : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo′ f1 df = ∀ da a → [ _ ] da from a to (a ⊕ da) → WellDefinedFunChangePoint f1 df a da fromto→WellDefined′ : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo′ f1 df fromto→WellDefined′ {f1 = f1} {f2} {df} dff da a daa = begin (f1 ⊕ df) (a ⊕ da) ≡⟨⟩ f1 (a ⊕ da) ⊕ df (a ⊕ da) (nil (a ⊕ da)) ≡⟨ (fromto→⊕ (df (a ⊕ da) (nil (a ⊕ da))) _ _ (dff (nil (a ⊕ da)) _ _ (nil-fromto (a ⊕ da)))) ⟩ f2 (a ⊕ da) ≡⟨ sym (fromto→⊕ _ _ _ (dff da _ _ daa)) ⟩ f1 a ⊕ df a da ∎ WellDefinedFunChangeFromTo : ∀ {σ τ} (f1 : ⟦ σ ⇒ τ ⟧Type) → (df : Cht (σ ⇒ τ)) → Set WellDefinedFunChangeFromTo f1 df = ∀ da a1 a2 → [ _ ] da from a1 to a2 → WellDefinedFunChangePoint f1 df a1 da fromto→WellDefined : ∀ {σ τ f1 f2 df} → [ σ ⇒ τ ] df from f1 to f2 → WellDefinedFunChangeFromTo f1 df fromto→WellDefined {f1 = f1} {f2} {df} dff da a1 a2 daa = fromto→WellDefined′ dff da a1 daa′ where daa′ : [ _ ] da from a1 to (a1 ⊕ da) daa′ rewrite fromto→⊕ da a1 a2 daa = daa -- Recursive isomorphism between the two validities. -- -- Among other things, valid→fromto proves that a validity-preserving function, -- with validity defined via (f1 ⊕ df) (a ⊕ da) ≡ f1 a ⊕ df a da, is also valid -- in the "fromto" sense. -- -- We can't hope for a better statement, since we need the equation to be -- satisfied also by returned or argument functions. fromto→valid : ∀ {τ} → ∀ v1 v2 dv → [ τ ] dv from v1 to v2 → valid v1 dv valid→fromto : ∀ {τ} v (dv : Cht τ) → valid v dv → [ τ ] dv from v to (v ⊕ dv) fromto→valid {int} = λ v1 v2 dv x → tt fromto→valid {pair σ τ} (a1 , b1) (a2 , b2) (da , db) (daa , dbb) = (fromto→valid _ _ _ daa) , (fromto→valid _ _ _ dbb) fromto→valid {σ ⇒ τ} f1 f2 df dff = λ a da ada → fromto→valid _ _ _ (dff da a (a ⊕ da) (valid→fromto a da ada)) , fromto→WellDefined′ dff da a (valid→fromto _ _ ada) valid→fromto {int} v dv tt = refl valid→fromto {pair σ τ} (a , b) (da , db) (ada , bdb) = valid→fromto a da ada , valid→fromto b db bdb valid→fromto {σ ⇒ τ} f df fdf da a1 a2 daa = body where fa1da-valid : valid (f a1) (df a1 da) × WellDefinedFunChangePoint f df a1 da fa1da-valid = fdf a1 da (fromto→valid _ _ _ daa) body : [ τ ] df a1 da from f a1 to (f ⊕ df) a2 body rewrite sym (fromto→⊕ _ _ _ daa) | proj₂ fa1da-valid = valid→fromto (f a1) (df a1 da) (proj₁ fa1da-valid)
Drop redundant import
Drop redundant import
Agda
mit
inc-lc/ilc-agda
e6b366592e727138ab845e5190ed6c35fa3d28d4
js-experiment/prelude.agda
js-experiment/prelude.agda
module _ where open import Agda.Primitive public using (Level; _⊔_) renaming (lzero to ₀; lsuc to ₛ) -- open import Function.NP id : ∀ {a} {A : Set a} (x : A) → A id x = x infixr 9 _∘_ _∘_ : ∀ {a b c} {A : Set a} {B : A → Set b} {C : {x : A} → B x → Set c} → (∀ {x} (y : B x) → C y) → (g : (x : A) → B x) → ((x : A) → C (g x)) f ∘ g = λ x → f (g x) infixr 0 _$_ _$_ : ∀ {a b} {A : Set a} {B : A → Set b} → ((x : A) → B x) → ((x : A) → B x) f $ x = f x _$′_ : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → (A → B) f $′ x = f x data 𝟘 : Set where -- open import Data.One record 𝟙 : Set₀ where constructor <> open 𝟙 data Bool : Set where true false : Bool [true:_,false:_] : ∀ {c} {C : Bool → Set c} → C true → C false → ((x : Bool) → C x) [true: f ,false: g ] true = f [true: f ,false: g ] false = g data LR : Set where L R : LR [L:_,R:_] : ∀ {c}{C : LR → Set c} → C L → C R → (x : LR) → C x [L: f ,R: g ] L = f [L: f ,R: g ] R = g -- open import Data.Product.NP record Σ {a b}(A : Set a)(B : A → Set b) : Set(a ⊔ b) where constructor _,_ field fst : A snd : B fst open Σ public _×_ : (A B : Set) → Set A × B = Σ A (λ _ → B) -- open import Data.Sum.NP data _⊎_ (A B : Set) : Set where inl : A → A ⊎ B inr : B → A ⊎ B [inl:_,inr:_] : ∀ {c} {A B : Set} {C : A ⊎ B → Set c} → ((x : A) → C (inl x)) → ((x : B) → C (inr x)) → ((x : A ⊎ B) → C x) [inl: f ,inr: g ] (inl x) = f x [inl: f ,inr: g ] (inr y) = g y -- open import Data.List using (List; []; _∷_) infixr 5 _∷_ data List {a} (A : Set a) : Set a where [] : List A _∷_ : (x : A) (xs : List A) → List A [_] : ∀ {a}{A : Set a} → A → List A [ x ] = x ∷ [] {-# BUILTIN LIST List #-} {-# BUILTIN NIL [] #-} {-# BUILTIN CONS _∷_ #-} reverse : {A : Set} → List A → List A reverse {A} = go [] where go : List A → List A → List A go acc [] = acc go acc (x ∷ xs) = go (x ∷ acc) xs module _ {A : Set} (_≤_ : A → A → Bool) where merge-sort-list : (l₀ l₁ : List A) → List A merge-sort-list [] l₁ = l₁ merge-sort-list l₀ [] = l₀ merge-sort-list (x₀ ∷ l₀) (x₁ ∷ l₁) with x₀ ≤ x₁ ... | true = x₀ ∷ merge-sort-list l₀ (x₁ ∷ l₁) ... | false = x₁ ∷ merge-sort-list (x₀ ∷ l₀) l₁ -- open import Relation.Binary.PropositionalEquality infix 0 _≡_ data _≡_ {a}{A : Set a} (x : A) : A → Set a where refl : x ≡ x {-# BUILTIN EQUALITY _≡_ #-} {-# BUILTIN REFL refl #-} _≢_ : ∀ {a}{A : Set a}(x y : A) → Set a x ≢ y = x ≡ y → 𝟘 ap : ∀{a b}{A : Set a}{B : Set b} (f : A → B) {x y : A} (p : x ≡ y) → f x ≡ f y ap f refl = refl sym : ∀{a}{A : Set a}{x y : A} → x ≡ y → y ≡ x sym refl = refl ! : ∀{a}{A : Set a}{x y : A} → x ≡ y → y ≡ x ! refl = refl subst : ∀{a p}{A : Set a}(P : A → Set p){x y : A} → x ≡ y → P x → P y subst P refl px = px tr = subst ap₂ : ∀ {a b c}{A : Set a}{B : Set b}{C : Set c}(f : A → B → C){x x' y y'} → x ≡ x' → y ≡ y' → f x y ≡ f x' y' ap₂ f refl refl = refl infixr 6 _∙_ _∙_ : ∀ {a}{A : Set a}{x y z : A} → x ≡ y → y ≡ z → x ≡ z refl ∙ p = p postulate String : Set Char : Set {-# BUILTIN STRING String #-} {-# BUILTIN CHAR Char #-} data Error (A : Set) : Set where succeed : A → Error A fail : (err : String) → Error A [succeed:_,fail:_] : ∀ {c} {A : Set} {C : Error A → Set c} → ((x : A) → C (succeed x)) → ((x : String) → C (fail x)) → ((x : Error A) → C x) [succeed: f ,fail: g ] (succeed x) = f x [succeed: f ,fail: g ] (fail y) = g y mapE : {A B : Set} → (A → B) → Error A → Error B mapE f (fail err) = fail err mapE f (succeed x) = succeed (f x) data All {A : Set} (P : A → Set) : Error A → Set where fail : ∀ s → All P (fail s) succeed : ∀ {x} → P x → All P (succeed x) record _≃?_ (A B : Set) : Set where field serialize : A → B parse : B → Error A linv : ∀ x → All (_≡_ x ∘ serialize) (parse x) rinv : ∀ x → parse (serialize x) ≡ succeed x open _≃?_ {{...}} public {- open import Category.Profunctor Prism : (S T A B : Set) → Set₁ Prism S T A B = ∀ {_↝_}{{_ : Choice {₀} _↝_}} → (A ↝ B) → (S ↝ T) Prism' : (S A : Set) → Set₁ Prism' S A = Prism S S A A module _ {S T A B : Set} where prism : (B → T) → (S → T ⊎ A) → Prism S T A B prism bt sta = dimap sta [inl: id ,inr: bt ] ∘ right where open Choice {{...}} -} Prism : (S T A B : Set) → Set Prism S T A B = (B → T) × (S → T ⊎ A) Prism' : (S A : Set) → Set Prism' S A = Prism S S A A module _ {S T A B : Set} where prism : (B → T) → (S → T ⊎ A) → Prism S T A B prism = _,_ -- This is particular case of lens' _#_ _#_ : Prism S T A B → B → T _#_ = fst -- This is particular case of lens' review review = _#_ -- _^._ : Prism S T A B → S → ... module _ {S A : Set} where prism' : (A → S) → (S → S ⊎ A) → Prism' S A prism' = prism module _ {a b}{A : Set a}{B : A → Set b} where case_of_ : (x : A) → ((y : A) → B y) → B x case x of f = f x module _ {a b}{A : Set a}{B : Set b} where case_of′_ : A → (A → B) → B case_of′_ = case_of_ … : {A : Set}{{x : A}} → A … {{x}} = x
module _ where open import Agda.Primitive public using (Level; _⊔_) renaming (lzero to ₀; lsuc to ₛ) -- open import Function.NP id : ∀ {a} {A : Set a} (x : A) → A id x = x infixr 9 _∘_ _∘_ : ∀ {a b c} {A : Set a} {B : A → Set b} {C : {x : A} → B x → Set c} → (∀ {x} (y : B x) → C y) → (g : (x : A) → B x) → ((x : A) → C (g x)) f ∘ g = λ x → f (g x) infixr 0 _$_ _$_ : ∀ {a b} {A : Set a} {B : A → Set b} → ((x : A) → B x) → ((x : A) → B x) f $ x = f x _$′_ : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → (A → B) f $′ x = f x data 𝟘 : Set where -- open import Data.One record 𝟙 : Set₀ where constructor <> open 𝟙 data Bool : Set where true false : Bool [true:_,false:_] : ∀ {c} {C : Bool → Set c} → C true → C false → ((x : Bool) → C x) [true: f ,false: g ] true = f [true: f ,false: g ] false = g data LR : Set where L R : LR [L:_,R:_] : ∀ {c}{C : LR → Set c} → C L → C R → (x : LR) → C x [L: f ,R: g ] L = f [L: f ,R: g ] R = g -- open import Data.Product.NP record Σ {a b}(A : Set a)(B : A → Set b) : Set(a ⊔ b) where constructor _,_ field fst : A snd : B fst open Σ public _×_ : ∀{a b}(A : Set a)(B : Set b) → Set _ A × B = Σ A (λ _ → B) -- open import Data.Sum.NP data _⊎_ (A B : Set) : Set where inl : A → A ⊎ B inr : B → A ⊎ B [inl:_,inr:_] : ∀ {c} {A B : Set} {C : A ⊎ B → Set c} → ((x : A) → C (inl x)) → ((x : B) → C (inr x)) → ((x : A ⊎ B) → C x) [inl: f ,inr: g ] (inl x) = f x [inl: f ,inr: g ] (inr y) = g y -- open import Data.List using (List; []; _∷_) infixr 5 _∷_ data List {a} (A : Set a) : Set a where [] : List A _∷_ : (x : A) (xs : List A) → List A [_] : ∀ {a}{A : Set a} → A → List A [ x ] = x ∷ [] {-# BUILTIN LIST List #-} {-# BUILTIN NIL [] #-} {-# BUILTIN CONS _∷_ #-} reverse : {A : Set} → List A → List A reverse {A} = go [] where go : List A → List A → List A go acc [] = acc go acc (x ∷ xs) = go (x ∷ acc) xs module _ {A : Set} (_≤_ : A → A → Bool) where merge-sort-list : (l₀ l₁ : List A) → List A merge-sort-list [] l₁ = l₁ merge-sort-list l₀ [] = l₀ merge-sort-list (x₀ ∷ l₀) (x₁ ∷ l₁) with x₀ ≤ x₁ ... | true = x₀ ∷ merge-sort-list l₀ (x₁ ∷ l₁) ... | false = x₁ ∷ merge-sort-list (x₀ ∷ l₀) l₁ -- open import Relation.Binary.PropositionalEquality infix 0 _≡_ data _≡_ {a}{A : Set a} (x : A) : A → Set a where refl : x ≡ x {-# BUILTIN EQUALITY _≡_ #-} {-# BUILTIN REFL refl #-} _≢_ : ∀ {a}{A : Set a}(x y : A) → Set a x ≢ y = x ≡ y → 𝟘 ap : ∀{a b}{A : Set a}{B : Set b} (f : A → B) {x y : A} (p : x ≡ y) → f x ≡ f y ap f refl = refl sym : ∀{a}{A : Set a}{x y : A} → x ≡ y → y ≡ x sym refl = refl ! : ∀{a}{A : Set a}{x y : A} → x ≡ y → y ≡ x ! refl = refl subst : ∀{a p}{A : Set a}(P : A → Set p){x y : A} → x ≡ y → P x → P y subst P refl px = px tr = subst ap₂ : ∀ {a b c}{A : Set a}{B : Set b}{C : Set c}(f : A → B → C){x x' y y'} → x ≡ x' → y ≡ y' → f x y ≡ f x' y' ap₂ f refl refl = refl infixr 6 _∙_ _∙_ : ∀ {a}{A : Set a}{x y z : A} → x ≡ y → y ≡ z → x ≡ z refl ∙ p = p postulate String : Set Char : Set {-# BUILTIN STRING String #-} {-# BUILTIN CHAR Char #-} data Error (A : Set) : Set where succeed : A → Error A fail : (err : String) → Error A [succeed:_,fail:_] : ∀ {c} {A : Set} {C : Error A → Set c} → ((x : A) → C (succeed x)) → ((x : String) → C (fail x)) → ((x : Error A) → C x) [succeed: f ,fail: g ] (succeed x) = f x [succeed: f ,fail: g ] (fail y) = g y mapE : {A B : Set} → (A → B) → Error A → Error B mapE f (fail err) = fail err mapE f (succeed x) = succeed (f x) data All {A : Set} (P : A → Set) : Error A → Set where fail : ∀ s → All P (fail s) succeed : ∀ {x} → P x → All P (succeed x) record _≃?_ (A B : Set) : Set where field serialize : A → B parse : B → Error A linv : ∀ x → All (_≡_ x ∘ serialize) (parse x) rinv : ∀ x → parse (serialize x) ≡ succeed x open _≃?_ {{...}} public {- open import Category.Profunctor Prism : (S T A B : Set) → Set₁ Prism S T A B = ∀ {_↝_}{{_ : Choice {₀} _↝_}} → (A ↝ B) → (S ↝ T) Prism' : (S A : Set) → Set₁ Prism' S A = Prism S S A A module _ {S T A B : Set} where prism : (B → T) → (S → T ⊎ A) → Prism S T A B prism bt sta = dimap sta [inl: id ,inr: bt ] ∘ right where open Choice {{...}} -} Prism : (S T A B : Set) → Set Prism S T A B = (B → T) × (S → T ⊎ A) Prism' : (S A : Set) → Set Prism' S A = Prism S S A A module _ {S T A B : Set} where prism : (B → T) → (S → T ⊎ A) → Prism S T A B prism = _,_ -- This is particular case of lens' _#_ _#_ : Prism S T A B → B → T _#_ = fst -- This is particular case of lens' review review = _#_ -- _^._ : Prism S T A B → S → ... module _ {S A : Set} where prism' : (A → S) → (S → S ⊎ A) → Prism' S A prism' = prism module _ {a b}{A : Set a}{B : A → Set b} where case_of_ : (x : A) → ((y : A) → B y) → B x case x of f = f x module _ {a b}{A : Set a}{B : Set b} where case_of′_ : A → (A → B) → B case_of′_ = case_of_ … : {A : Set}{{x : A}} → A … {{x}} = x
make × universe polymorphic
make × universe polymorphic
Agda
bsd-3-clause
crypto-agda/protocols
5760ecf94ca99d0cf810abe16aba48c31d177ad8
Structure/Bag/Nehemiah.agda
Structure/Bag/Nehemiah.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Bags of integers, for Nehemiah plugin. -- -- This module imports postulates about bags of integers -- with negative multiplicities as a group under additive union. ------------------------------------------------------------------------ module Structure.Bag.Nehemiah where open import Postulate.Bag-Nehemiah public open import Relation.Binary.PropositionalEquality open import Algebra using (CommutativeRing) open import Algebra.Structures open import Data.Integer open import Data.Integer.Properties using () renaming (commutativeRing to ℤ-is-commutativeRing) open import Data.Product infixl 9 _\\_ -- same as Data.Map.(\\) _\\_ : Bag → Bag → Bag d \\ b = d ++ (negateBag b) -- Useful properties of abelian groups commutative : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (m n : A) → f m n ≡ f n m commutative = IsAbelianGroup.comm associative : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (k m n : A) → f (f k m) n ≡ f k (f m n) associative abelian = IsAbelianGroup.assoc abelian left-inverse : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f (neg n) n ≡ z left-inverse abelian = proj₁ (IsAbelianGroup.inverse abelian) right-inverse : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f n (neg n) ≡ z right-inverse abelian = proj₂ (IsAbelianGroup.inverse abelian) left-identity : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f z n ≡ n left-identity abelian = proj₁ (IsMonoid.identity (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian))) right-identity : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f n z ≡ n right-identity abelian = proj₂ (IsMonoid.identity (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian))) instance abelian-int : IsAbelianGroup _≡_ _+_ (+ 0) (-_) abelian-int = IsRing.+-isAbelianGroup (IsCommutativeRing.isRing (CommutativeRing.isCommutativeRing ℤ-is-commutativeRing)) commutative-int : (m n : ℤ) → m + n ≡ n + m commutative-int = commutative abelian-int associative-int : (k m n : ℤ) → (k + m) + n ≡ k + (m + n) associative-int = associative abelian-int right-inv-int : (n : ℤ) → n - n ≡ + 0 right-inv-int = right-inverse abelian-int left-id-int : (n : ℤ) → (+ 0) + n ≡ n left-id-int = left-identity abelian-int right-id-int : (n : ℤ) → n + (+ 0) ≡ n right-id-int = right-identity abelian-int commutative-bag : (a b : Bag) → a ++ b ≡ b ++ a commutative-bag = commutative abelian-bag associative-bag : (a b c : Bag) → (a ++ b) ++ c ≡ a ++ (b ++ c) associative-bag = associative abelian-bag right-inv-bag : (b : Bag) → b \\ b ≡ emptyBag right-inv-bag = right-inverse abelian-bag left-id-bag : (b : Bag) → emptyBag ++ b ≡ b left-id-bag = left-identity abelian-bag right-id-bag : (b : Bag) → b ++ emptyBag ≡ b right-id-bag = right-identity abelian-bag
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Bags of integers, for Nehemiah plugin. -- -- This module imports postulates about bags of integers -- with negative multiplicities as a group under additive union. ------------------------------------------------------------------------ module Structure.Bag.Nehemiah where open import Postulate.Bag-Nehemiah public open import Relation.Binary.PropositionalEquality open import Algebra using (CommutativeRing) open import Algebra.Structures open import Data.Integer open import Data.Integer.Properties using () renaming (commutativeRing to ℤ-is-commutativeRing) open import Data.Product infixl 9 _\\_ -- same as Data.Map.(\\) _\\_ : Bag → Bag → Bag d \\ b = d ++ (negateBag b) -- Useful properties of abelian groups commutative : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (m n : A) → f m n ≡ f n m commutative = IsAbelianGroup.comm associative : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (k m n : A) → f (f k m) n ≡ f k (f m n) associative abelian = IsAbelianGroup.assoc abelian left-inverse : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f (neg n) n ≡ z left-inverse abelian = proj₁ (IsAbelianGroup.inverse abelian) right-inverse : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f n (neg n) ≡ z right-inverse abelian = proj₂ (IsAbelianGroup.inverse abelian) left-identity : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f z n ≡ n left-identity abelian = proj₁ (IsMonoid.identity (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian))) right-identity : ∀ {A : Set} {f : A → A → A} {z neg} → IsAbelianGroup _≡_ f z neg → (n : A) → f n z ≡ n right-identity abelian = proj₂ (IsMonoid.identity (IsGroup.isMonoid (IsAbelianGroup.isGroup abelian))) instance abelian-int : IsAbelianGroup _≡_ _+_ (+ 0) (-_) abelian-int = CommutativeRing.+-isAbelianGroup ℤ-is-commutativeRing commutative-int : (m n : ℤ) → m + n ≡ n + m commutative-int = commutative abelian-int associative-int : (k m n : ℤ) → (k + m) + n ≡ k + (m + n) associative-int = associative abelian-int right-inv-int : (n : ℤ) → n - n ≡ + 0 right-inv-int = right-inverse abelian-int left-id-int : (n : ℤ) → (+ 0) + n ≡ n left-id-int = left-identity abelian-int right-id-int : (n : ℤ) → n + (+ 0) ≡ n right-id-int = right-identity abelian-int commutative-bag : (a b : Bag) → a ++ b ≡ b ++ a commutative-bag = commutative abelian-bag associative-bag : (a b c : Bag) → (a ++ b) ++ c ≡ a ++ (b ++ c) associative-bag = associative abelian-bag right-inv-bag : (b : Bag) → b \\ b ≡ emptyBag right-inv-bag = right-inverse abelian-bag left-id-bag : (b : Bag) → emptyBag ++ b ≡ b left-id-bag = left-identity abelian-bag right-id-bag : (b : Bag) → b ++ emptyBag ≡ b right-id-bag = right-identity abelian-bag
Simplify code
Simplify code
Agda
mit
inc-lc/ilc-agda
5013dd25be51aea4374492d685bcc066ae526e82
Base/Change/Equivalence.agda
Base/Change/Equivalence.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Delta-observational equivalence ------------------------------------------------------------------------ module Base.Change.Equivalence where open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra open import Level open import Data.Unit module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where -- Delta-observational equivalence: these asserts that two changes -- give the same result when applied to a base value. _≙_ : ∀ dx dy → Set a dx ≙ dy = x ⊞ dx ≡ x ⊞ dy -- _≙_ is indeed an equivalence relation: ≙-refl : ∀ {dx} → dx ≙ dx ≙-refl = refl ≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx ≙-symm ≙ = sym ≙ ≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz ≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂ -- By update-nil, if dx = nil x, then x ⊞ dx ≡ x. -- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x nil-is-⊞-unit dx dx≙nil-x = begin x ⊞ dx ≡⟨ dx≙nil-x ⟩ x ⊞ (nil x) ≡⟨ update-nil x ⟩ x ∎ where open ≡-Reasoning -- Here we prove the inverse: ⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x ⊞-unit-is-nil dx x⊞dx≡x = begin x ⊞ dx ≡⟨ x⊞dx≡x ⟩ x ≡⟨ sym (update-nil x) ⟩ x ⊞ nil x ∎ where open ≡-Reasoning -- TODO: we want to show that all functions of interest respect -- delta-observational equivalence, so that two d.o.e. changes can be -- substituted for each other freely. -- -- * That should be be true for -- functions using changes parametrically. -- -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ]; -- this is proved below on both contexts at once by fun-change-respects. -- -- * Finally, change algebra operations should respect d.o.e. But ⊞ respects -- it by definition, and ⊟ doesn't take change arguments - we will only -- need a proof for compose, when we define it. -- -- Stating the general result, though, seems hard, we should -- rather have lemmas proving that certain classes of functions respect this -- equivalence. module _ {a} {b} {c} {d} {A : Set a} {B : Set b} {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where module FC = FunctionChanges A B {{CA}} {{CB}} open FC using (changeAlgebra; incrementalization) open FC.FunctionChange fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} → df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂ fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂ -- Informally: use incrementalization on both sides and then apply -- congruence. lemma = begin f x ⊞ apply df₁ x dx₁ ≡⟨ sym (incrementalization f df₁ x dx₁) ⟩ (f ⊞ df₁) (x ⊞ dx₁) ≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩ (f ⊞ df₁) (x ⊞ dx₂) ≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩ (f ⊞ df₂) (x ⊞ dx₂) ≡⟨ incrementalization f df₂ x dx₂ ⟩ f x ⊞ apply df₂ x dx₂ ∎ open import Postulate.Extensionality -- An extensionality principle for delta-observational equivalence: if -- applying two function changes to the same base value and input change gives -- a d.o.e. result, then the two function changes are d.o.e. themselves. delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = lemma₂ where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx lemma₁ = df-x-dx≙dg-x-dx lemma₂ : f ⊞ df ≡ f ⊞ dg lemma₂ = ext (λ x → lemma₁ x (nil x)) -- We know that Derivative f (apply (nil f)) (by nil-is-derivative). -- That is, df = nil f -> Derivative f (apply df). -- Now, we try to prove that if Derivative f (apply df) -> df = nil f. -- But first, we prove that f ⊞ df = f. derivative-is-⊞-unit : ∀ {f : A → B} df → Derivative f (apply df) → f ⊞ df ≡ f derivative-is-⊞-unit {f} df fdf = begin f ⊞ df ≡⟨⟩ (λ x → f x ⊞ apply df x (nil x)) ≡⟨ ext (λ x → fdf x (nil x)) ⟩ (λ x → f (x ⊞ nil x)) ≡⟨ ext (λ x → cong f (update-nil x)) ⟩ (λ x → f x) ≡⟨⟩ f ∎ where open ≡-Reasoning -- We can restate the above as "df is a nil change". derivative-is-nil : ∀ {f : A → B} df → Derivative f (apply df) → df ≙ nil f derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf) -- If we have two derivatives, they're both nil, hence they're equal. derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg derivative-unique {f} {df} {dg} fdf fdg = begin f ⊞ df ≡⟨ derivative-is-nil df fdf ⟩ f ⊞ nil f ≡⟨ sym (derivative-is-nil dg fdg) ⟩ f ⊞ dg ∎ where open ≡-Reasoning -- We could also use derivative-is-⊞-unit, but the proof above matches better -- with the text above. -- -- Small TODO: It could be even more elegant to define a setoid for ≙ and -- use equational reasoning on it directly.
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Delta-observational equivalence ------------------------------------------------------------------------ module Base.Change.Equivalence where open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra open import Level open import Data.Unit module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where -- Delta-observational equivalence: these asserts that two changes -- give the same result when applied to a base value. _≙_ : ∀ dx dy → Set a dx ≙ dy = x ⊞ dx ≡ x ⊞ dy open import Relation.Binary -- _≙_ is indeed an equivalence relation: ≙-refl : ∀ {dx} → dx ≙ dx ≙-refl = refl ≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx ≙-symm ≙ = sym ≙ ≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz ≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂ ≙-isEquivalence : IsEquivalence (_≙_) ≙-isEquivalence = record { refl = ≙-refl ; sym = ≙-symm ; trans = ≙-trans } ≙-setoid : Setoid ℓ a ≙-setoid = record { Carrier = Δ x ; _≈_ = _≙_ ; isEquivalence = ≙-isEquivalence } ------------------------------------------------------------------------ -- Convenient syntax for equational reasoning import Relation.Binary.EqReasoning as EqR -- Relation.Binary.EqReasoning is more convenient to use with _≡_ if -- the combinators take the type argument (a) as a hidden argument, -- instead of being locked to a fixed type at module instantiation -- time. module ≙-Reasoning where open EqR ≙-setoid public renaming (_≈⟨_⟩_ to _≙⟨_⟩_) -- By update-nil, if dx = nil x, then x ⊞ dx ≡ x. -- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x nil-is-⊞-unit dx dx≙nil-x = begin x ⊞ dx ≡⟨ dx≙nil-x ⟩ x ⊞ (nil x) ≡⟨ update-nil x ⟩ x ∎ where open ≡-Reasoning -- Here we prove the inverse: ⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x ⊞-unit-is-nil dx x⊞dx≡x = begin x ⊞ dx ≡⟨ x⊞dx≡x ⟩ x ≡⟨ sym (update-nil x) ⟩ x ⊞ nil x ∎ where open ≡-Reasoning -- TODO: we want to show that all functions of interest respect -- delta-observational equivalence, so that two d.o.e. changes can be -- substituted for each other freely. -- -- * That should be be true for -- functions using changes parametrically. -- -- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ]; -- this is proved below on both contexts at once by fun-change-respects. -- -- * Finally, change algebra operations should respect d.o.e. But ⊞ respects -- it by definition, and ⊟ doesn't take change arguments - we will only -- need a proof for compose, when we define it. -- -- Stating the general result, though, seems hard, we should -- rather have lemmas proving that certain classes of functions respect this -- equivalence. module _ {a} {b} {c} {d} {A : Set a} {B : Set b} {{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where module FC = FunctionChanges A B {{CA}} {{CB}} open FC using (changeAlgebra; incrementalization) open FC.FunctionChange fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} → df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂ fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂ -- Informally: use incrementalization on both sides and then apply -- congruence. lemma = begin f x ⊞ apply df₁ x dx₁ ≡⟨ sym (incrementalization f df₁ x dx₁) ⟩ (f ⊞ df₁) (x ⊞ dx₁) ≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩ (f ⊞ df₁) (x ⊞ dx₂) ≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩ (f ⊞ df₂) (x ⊞ dx₂) ≡⟨ incrementalization f df₂ x dx₂ ⟩ f x ⊞ apply df₂ x dx₂ ∎ open import Postulate.Extensionality -- An extensionality principle for delta-observational equivalence: if -- applying two function changes to the same base value and input change gives -- a d.o.e. result, then the two function changes are d.o.e. themselves. delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = lemma₂ where open ≡-Reasoning -- This type signature just expands the goal manually a bit. lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx lemma₁ = df-x-dx≙dg-x-dx lemma₂ : f ⊞ df ≡ f ⊞ dg lemma₂ = ext (λ x → lemma₁ x (nil x)) -- We know that Derivative f (apply (nil f)) (by nil-is-derivative). -- That is, df = nil f -> Derivative f (apply df). -- Now, we try to prove that if Derivative f (apply df) -> df = nil f. -- But first, we prove that f ⊞ df = f. derivative-is-⊞-unit : ∀ {f : A → B} df → Derivative f (apply df) → f ⊞ df ≡ f derivative-is-⊞-unit {f} df fdf = begin f ⊞ df ≡⟨⟩ (λ x → f x ⊞ apply df x (nil x)) ≡⟨ ext (λ x → fdf x (nil x)) ⟩ (λ x → f (x ⊞ nil x)) ≡⟨ ext (λ x → cong f (update-nil x)) ⟩ (λ x → f x) ≡⟨⟩ f ∎ where open ≡-Reasoning -- We can restate the above as "df is a nil change". derivative-is-nil : ∀ {f : A → B} df → Derivative f (apply df) → df ≙ nil f derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf) -- If we have two derivatives, they're both nil, hence they're equal. derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg derivative-unique {f} {df} {dg} fdf fdg = begin df ≙⟨ derivative-is-nil df fdf ⟩ nil f ≙⟨ sym (derivative-is-nil dg fdg) ⟩ dg ∎ where open ≙-Reasoning {- lemma : nil f ≙ dg -- Goes through --lemma = sym (derivative-is-nil dg fdg) -- Generates tons of crazy yellow ambiguities of equality type. Apparently, unifying against ≙ does not work so well. lemma = ≙-symm {{changeAlgebra}} {f} {dg} {nil f} (derivative-is-nil dg fdg) -} -- We could also use derivative-is-⊞-unit, but the proof above matches better -- with the text above.
Define setoid for d.o.e. (not very succesfully)
Agda: Define setoid for d.o.e. (not very succesfully) This setoid can be defined, but using it for equational reasoning seems not so convenient. I guess a reason is that unifying against _≙_ might be harder (see comments at the end). The other, bigger problem is that most reasonings here need to rewrite dx₁ ≙ dx₂ into x ⊞ dx₁ ≡ x ⊞ dx₂ to perform other transformations, so this setoid is not powerful enough. Maybe, however, other files can make more use of this setoid. Old-commit-hash: a37b3b7baeb5c99c881dcf6dc7dced39f9311ed3
Agda
mit
inc-lc/ilc-agda
57da8ab233fdca3b992d585fa703f2c305a61295
sha1.agda
sha1.agda
{-# OPTIONS --without-K #-} -- https://upload.wikimedia.org/wikipedia/commons/e/e2/SHA-1.svg -- http://www.faqs.org/rfcs/rfc3174.html open import Type open import Data.Nat.NP open import Data.Bool using (if_then_else_) import Data.Vec as V open V using (Vec; []; _∷_) --open import Data.Product open import Function.NP hiding (id) open import FunUniverse.Core hiding (_,_) open import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (toℕ to Fin▹ℕ) module sha1 where module FunSHA1 {t} {T : ★_ t} {funU : FunUniverse T} (funOps : FunOps funU) where open FunUniverse funU open FunOps funOps renaming (_∘_ to _`∘_) Word : T Word = `Bits 32 map²ʷ : (`𝟚 `× `𝟚 `→ `𝟚) → Word `× Word `→ Word map²ʷ = zipWith mapʷ : (`𝟚 `→ `𝟚) → Word `→ Word mapʷ = map lift : ∀ {Γ A B} → (A `→ B) → Γ `→ A → Γ `→ B lift f g = g ⁏ f lift₂ : ∀ {Γ A B C} → (A `× B `→ C) → Γ `→ A → Γ `→ B → Γ `→ C lift₂ op₂ f₀ f₁ = < f₀ , f₁ > ⁏ op₂ `not : ∀ {Γ} (f : Γ `→ Word) → Γ `→ Word `not = lift (mapʷ not) infixr 3 _`⊕_ _`⊕_ : ∀ {Γ} (f₀ f₁ : Γ `→ Word) → Γ `→ Word _`⊕_ = lift₂ (map²ʷ <xor>) infixr 3 _`∧_ _`∧_ : ∀ {Γ} (f₀ f₁ : Γ `→ Word) → Γ `→ Word _`∧_ = lift₂ (map²ʷ <and>) infixr 2 _`∨_ _`∨_ : ∀ {Γ} (f₀ f₁ : Γ `→ Word) → Γ `→ Word _`∨_ = lift₂ (map²ʷ <or>) open import Solver.Linear module LinSolver = Syntaxᶠ linRewiring --iter : ∀ {n A B S} → (S `× A `→ S `× B) → S `× `Vec A n `→ `Vec B n iter : ∀ {n A B C D} → (D `× A `× B `→ D `× C) → D `× `Vec A n `× `Vec B n `→ `Vec C n iter {zero} F = <[]> iter {suc n} F = < id × < uncons × uncons > > ⁏ (helper ⁏ < F × id > ⁏ < swap × id > ⁏ assoc ⁏ < id × iter F >) ⁏ <∷> where open LinSolver helper = λ {A} {B} {D} {VA} {VB} → rewireᶠ (A ∷ B ∷ D ∷ VA ∷ VB ∷ []) (λ a b d va vb → (d , (a , va) , (b , vb)) ↦ (d , a , b) , (va , vb)) <⊞> adder : Word `× Word `→ Word adder = <tt⁏ <0₂> , id > ⁏ iter full-adder <⊞> = adder infixl 4 _`⊞_ _`⊞_ : ∀ {A} (f g : A `→ Word) → A `→ Word _`⊞_ = lift₂ <⊞> <_,_,_> : ∀ {Γ A B C} → Γ `→ A → Γ `→ B → Γ `→ C → Γ `→ (A `× B `× C) < f₀ , f₁ , f₂ > = < f₀ , < f₁ , f₂ > > <_,_,_,_,_> : ∀ {Γ A B C D E} → Γ `→ A → Γ `→ B → Γ `→ C → Γ `→ D → Γ `→ E → Γ `→ (A `× B `× C `× D `× E) < f₀ , f₁ , f₂ , f₃ , f₄ > = < f₀ , < f₁ , < f₂ , f₃ , f₄ > > > <<<₅ : `Endo Word <<<₅ = rot-left 5 <<<₃₀ : `Endo Word <<<₃₀ = rot-left 30 Word² = Word `× Word Word³ = Word `× Word² Word⁴ = Word `× Word³ Word⁵ = Word `× Word⁴ open import Data.Digit bits : ∀ ℓ → ℕ → `𝟙 `→ `Bits ℓ bits ℓ n₀ = constBits (L▹V (L.map F▹𝟚 (proj₁ (toDigits 2 n₀)))) where open import Data.List as L open import Data.Product open import Data.Two L▹V : ∀ {n} → List 𝟚 → Vec 𝟚 n L▹V {zero} xs = [] L▹V {suc n} [] = V.replicate 0₂ L▹V {suc n} (x ∷ xs) = x ∷ L▹V xs F▹𝟚 : Fin 2 → 𝟚 F▹𝟚 zero = 0₂ F▹𝟚 (suc _) = 1₂ {- [_-_mod_] : ℕ → ℕ → ℕ → ℕ [ m - n mod p ] = {!!} [_+_mod_] : ℕ → ℕ → ℕ → ℕ [ m + n mod p ] = {!!} -} #ʷ = bits 32 <⊞⁵> : Word⁵ `× Word⁵ `→ Word⁵ <⊞⁵> = < <⊞> `zip` < <⊞> `zip` < <⊞> `zip` < <⊞> `zip` <⊞> > > > > iterateⁿ : ∀ {A} n → (Fin n → `Endo A) → `Endo A iterateⁿ zero f = id iterateⁿ (suc n) f = f zero ⁏ iterateⁿ n (f ∘ suc) _²⁰ : ∀ {A} → (Fin 20 → `Endo A) → `Endo A _²⁰ = iterateⁿ 20 module _ where K₀ = #ʷ 0x5A827999 K₂ = #ʷ 0x6ED9EBA1 K₄ = #ʷ 0x8F1BBCDC K₆ = #ʷ 0xCA62C1D6 H0 = #ʷ 0x67452301 H1 = #ʷ 0xEFCDAB89 H2 = #ʷ 0x98BADCFE H3 = #ʷ 0x10325476 H4 = #ʷ 0xC3D2E1F0 A B C D E : Word⁵ `→ Word A = fst B = snd ⁏ fst C = snd ⁏ snd ⁏ fst D = snd ⁏ snd ⁏ snd ⁏ fst E = snd ⁏ snd ⁏ snd ⁏ snd F₀ = B `∧ C `∨ `not B `∧ D F₂ = B `⊕ C `⊕ D F₄ = B `∧ C `∨ B `∧ D `∨ C `∧ D F₆ = F₂ module _ (F : Word⁵ `→ Word) (K : `𝟙 `→ Word) (W : `𝟙 `→ Word) where Iteration = < A' , A , (B ⁏ <<<₃₀) , C , D > where A' = F `⊞ E `⊞ (A ⁏ <<<₅) `⊞ (tt ⁏ W) `⊞ (tt ⁏ K) module _ (W : Fin 80 → `𝟙 `→ Word) where W₀ W₂ W₄ W₆ : Fin 20 → `𝟙 `→ Word W₀ = W ∘ inject+ 60 ∘ raise 0 W₂ = W ∘ inject+ 40 ∘ raise 20 W₄ = W ∘ inject+ 20 ∘ raise 40 W₆ = W ∘ inject+ 0 ∘ raise 60 Iteration⁸⁰ : `Endo Word⁵ Iteration⁸⁰ = (Iteration F₀ K₀ ∘ W₀)²⁰ ⁏ (Iteration F₂ K₂ ∘ W₂)²⁰ ⁏ (Iteration F₄ K₄ ∘ W₄)²⁰ ⁏ (Iteration F₆ K₆ ∘ W₆)²⁰ pad0s : ℕ → ℕ pad0s zero = 512 ∸ 65 pad0s (suc _) = STUCK where postulate STUCK : ℕ -- pad0s n = [ 512 - [ n + 65 mod 512 ] mod 512 ] paddedLength : ℕ → ℕ paddedLength n = n + (1 + pad0s n + 64) padding : ∀ {n} → `Bits n `→ `Bits (paddedLength n) padding {n} = < id ,tt⁏ <1∷ < <0ⁿ> {pad0s n} ++ bits 64 n > > > ⁏ append ite : Endo (`Endo Word⁵) ite f = dup ⁏ first f ⁏ <⊞⁵> hash-block : `Endo Word⁵ hash-block = ite Iteration⁸⁰ ite' : ∀ n (W : Fin n → Fin 80 → `𝟙 `→ Word) → ((Fin 80 → `𝟙 `→ Word) → `Endo Word⁵) → `Endo Word⁵ ite' zero W f = id ite' (suc n) W f = f (W zero) ⁏ ite' n (W ∘ suc) f SHA1 : ∀ n (W : Fin n → Fin 80 → `𝟙 `→ Word) → `𝟙 `→ Word⁵ SHA1 n W = < H0 , H1 , H2 , H3 , H4 > ⁏ ite' n W hash-block SHA1-on-0s : `𝟙 `→ Word⁵ SHA1-on-0s = SHA1 1 (λ _ _ → <0ⁿ>) module AgdaSHA1 where open import FunUniverse.Agda open FunSHA1 agdaFunOps open import Data.Two open import IO import IO.Primitive open import Data.One open import Data.Two open import Data.Product open import Coinduction putBit : 𝟚 → IO 𝟙 putBit 1₂ = putStr "1" putBit 0₂ = putStr "0" putBits : ∀ {n} → Vec 𝟚 n → IO 𝟙 putBits [] = return _ putBits (x ∷ bs) = ♯ putBit x >> ♯ putBits bs put× : ∀ {A B : Set} → (A → IO 𝟙) → (B → IO 𝟙) → (A × B) → IO 𝟙 put× pA pB (x , y) = ♯ pA x >> ♯ pB y {- main : IO.Primitive.IO 𝟙 main = IO.run (put× putBits (put× putBits (put× putBits (put× putBits putBits))) AgdaSHA1.SHA1-on-0s) -} firstBit : ∀ {A : Set} → (V.Vec 𝟚 32 × A) → 𝟚 firstBit ((b ∷ _) , _) = b import FunUniverse.Cost as Cost open import Data.Nat.Show sha1-cost : ℕ sha1-cost = FunSHA1.SHA1-on-0s Cost.timeOps main : IO.Primitive.IO 𝟙 --main = IO.run (putBit (firstBit (AgdaSHA1.SHA1-on-0s _))) main = IO.run (putStrLn (show sha1-cost)) -- -}
{-# OPTIONS --without-K #-} -- https://upload.wikimedia.org/wikipedia/commons/e/e2/SHA-1.svg -- http://www.faqs.org/rfcs/rfc3174.html open import Data.Nat.NP using (ℕ; zero; suc; _+_; _∸_) import Data.Vec as V open V using (Vec; []; _∷_) open import Function.NP using (Endo; _∘_) open import FunUniverse.Core hiding (_,_) open import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (toℕ to Fin▹ℕ) open import Solver.Linear module sha1 where module FunSHA1 {t} {T : Set t} {funU : FunUniverse T} (funOps : FunOps funU) where open FunUniverse funU open FunOps funOps renaming (_∘_ to _`∘_) module LinSolver = Syntaxᶠ linRewiring Word : T Word = `Bits 32 map²ʷ : (`𝟚 `× `𝟚 `→ `𝟚) → Word `× Word `→ Word map²ʷ = zipWith mapʷ : (`𝟚 `→ `𝟚) → Word `→ Word mapʷ = map lift : ∀ {Γ A B} → (A `→ B) → Γ `→ A → Γ `→ B lift f g = g ⁏ f lift₂ : ∀ {Γ A B C} → (A `× B `→ C) → Γ `→ A → Γ `→ B → Γ `→ C lift₂ op₂ f₀ f₁ = < f₀ , f₁ > ⁏ op₂ `not : ∀ {Γ} (f : Γ `→ Word) → Γ `→ Word `not = lift (mapʷ not) infixr 3 _`⊕_ _`⊕_ : ∀ {Γ} (f₀ f₁ : Γ `→ Word) → Γ `→ Word _`⊕_ = lift₂ (map²ʷ <xor>) infixr 3 _`∧_ _`∧_ : ∀ {Γ} (f₀ f₁ : Γ `→ Word) → Γ `→ Word _`∧_ = lift₂ (map²ʷ <and>) infixr 2 _`∨_ _`∨_ : ∀ {Γ} (f₀ f₁ : Γ `→ Word) → Γ `→ Word _`∨_ = lift₂ (map²ʷ <or>) --iter : ∀ {n A B S} → (S `× A `→ S `× B) → S `× `Vec A n `→ `Vec B n iter : ∀ {n A B C D} → (D `× A `× B `→ D `× C) → D `× `Vec A n `× `Vec B n `→ `Vec C n iter {zero} F = <[]> iter {suc n} F = < id × < uncons × uncons > > ⁏ (helper ⁏ < F × id > ⁏ < swap × id > ⁏ assoc ⁏ < id × iter F >) ⁏ <∷> where open LinSolver helper = λ {A} {B} {D} {VA} {VB} → rewireᶠ (A ∷ B ∷ D ∷ VA ∷ VB ∷ []) (λ a b d va vb → (d , (a , va) , (b , vb)) ↦ (d , a , b) , (va , vb)) <⊞> adder : Word `× Word `→ Word adder = <tt⁏ <0₂> , id > ⁏ iter full-adder <⊞> = adder infixl 4 _`⊞_ _`⊞_ : ∀ {A} (f g : A `→ Word) → A `→ Word _`⊞_ = lift₂ <⊞> <_,_,_> : ∀ {Γ A B C} → Γ `→ A → Γ `→ B → Γ `→ C → Γ `→ (A `× B `× C) < f₀ , f₁ , f₂ > = < f₀ , < f₁ , f₂ > > <_,_,_,_,_> : ∀ {Γ A B C D E} → Γ `→ A → Γ `→ B → Γ `→ C → Γ `→ D → Γ `→ E → Γ `→ (A `× B `× C `× D `× E) < f₀ , f₁ , f₂ , f₃ , f₄ > = < f₀ , < f₁ , < f₂ , f₃ , f₄ > > > <<<₅ : `Endo Word <<<₅ = rot-left 5 <<<₃₀ : `Endo Word <<<₃₀ = rot-left 30 Word² = Word `× Word Word³ = Word `× Word² Word⁴ = Word `× Word³ Word⁵ = Word `× Word⁴ open import Data.Digit bits : ∀ ℓ → ℕ → `𝟙 `→ `Bits ℓ bits ℓ n₀ = constBits (L▹V (L.map F▹𝟚 (proj₁ (toDigits 2 n₀)))) where open import Data.List as L open import Data.Product open import Data.Two L▹V : ∀ {n} → List 𝟚 → Vec 𝟚 n L▹V {zero} xs = [] L▹V {suc n} [] = V.replicate 0₂ L▹V {suc n} (x ∷ xs) = x ∷ L▹V xs F▹𝟚 : Fin 2 → 𝟚 F▹𝟚 zero = 0₂ F▹𝟚 (suc _) = 1₂ {- [_-_mod_] : ℕ → ℕ → ℕ → ℕ [ m - n mod p ] = {!!} [_+_mod_] : ℕ → ℕ → ℕ → ℕ [ m + n mod p ] = {!!} -} #ʷ = bits 32 <⊞⁵> : Word⁵ `× Word⁵ `→ Word⁵ <⊞⁵> = < <⊞> `zip` < <⊞> `zip` < <⊞> `zip` < <⊞> `zip` <⊞> > > > > iterateⁿ : ∀ {A} n → (Fin n → `Endo A) → `Endo A iterateⁿ zero f = id iterateⁿ (suc n) f = f zero ⁏ iterateⁿ n (f ∘ suc) _²⁰ : ∀ {A} → (Fin 20 → `Endo A) → `Endo A _²⁰ = iterateⁿ 20 module _ where K₀ = #ʷ 0x5A827999 K₂ = #ʷ 0x6ED9EBA1 K₄ = #ʷ 0x8F1BBCDC K₆ = #ʷ 0xCA62C1D6 H0 = #ʷ 0x67452301 H1 = #ʷ 0xEFCDAB89 H2 = #ʷ 0x98BADCFE H3 = #ʷ 0x10325476 H4 = #ʷ 0xC3D2E1F0 A B C D E : Word⁵ `→ Word A = fst B = snd ⁏ fst C = snd ⁏ snd ⁏ fst D = snd ⁏ snd ⁏ snd ⁏ fst E = snd ⁏ snd ⁏ snd ⁏ snd F₀ = B `∧ C `∨ `not B `∧ D F₂ = B `⊕ C `⊕ D F₄ = B `∧ C `∨ B `∧ D `∨ C `∧ D F₆ = F₂ module _ (F : Word⁵ `→ Word) (K : `𝟙 `→ Word) (W : `𝟙 `→ Word) where Iteration = < A' , A , (B ⁏ <<<₃₀) , C , D > where A' = F `⊞ E `⊞ (A ⁏ <<<₅) `⊞ (tt ⁏ W) `⊞ (tt ⁏ K) module _ (W : Fin 80 → `𝟙 `→ Word) where W₀ W₂ W₄ W₆ : Fin 20 → `𝟙 `→ Word W₀ = W ∘ inject+ 60 ∘ raise 0 W₂ = W ∘ inject+ 40 ∘ raise 20 W₄ = W ∘ inject+ 20 ∘ raise 40 W₆ = W ∘ inject+ 0 ∘ raise 60 Iteration⁸⁰ : `Endo Word⁵ Iteration⁸⁰ = (Iteration F₀ K₀ ∘ W₀)²⁰ ⁏ (Iteration F₂ K₂ ∘ W₂)²⁰ ⁏ (Iteration F₄ K₄ ∘ W₄)²⁰ ⁏ (Iteration F₆ K₆ ∘ W₆)²⁰ pad0s : ℕ → ℕ pad0s zero = 512 ∸ 65 pad0s (suc _) = STUCK where postulate STUCK : ℕ -- pad0s n = [ 512 - [ n + 65 mod 512 ] mod 512 ] paddedLength : ℕ → ℕ paddedLength n = n + (1 + pad0s n + 64) padding : ∀ {n} → `Bits n `→ `Bits (paddedLength n) padding {n} = < id ,tt⁏ <1∷ < <0ⁿ> {pad0s n} ++ bits 64 n > > > ⁏ append ite : Endo (`Endo Word⁵) ite f = dup ⁏ first f ⁏ <⊞⁵> hash-block : `Endo Word⁵ hash-block = ite Iteration⁸⁰ ite' : ∀ n (W : Fin n → Fin 80 → `𝟙 `→ Word) → ((Fin 80 → `𝟙 `→ Word) → `Endo Word⁵) → `Endo Word⁵ ite' zero W f = id ite' (suc n) W f = f (W zero) ⁏ ite' n (W ∘ suc) f SHA1 : ∀ n (W : Fin n → Fin 80 → `𝟙 `→ Word) → `𝟙 `→ Word⁵ SHA1 n W = < H0 , H1 , H2 , H3 , H4 > ⁏ ite' n W hash-block SHA1-on-0s : `𝟙 `→ Word⁵ SHA1-on-0s = SHA1 1 (λ _ _ → <0ⁿ>) module AgdaSHA1 where open import FunUniverse.Agda open FunSHA1 agdaFunOps open import Data.Two open import IO import IO.Primitive open import Data.One open import Data.Two open import Data.Product open import Coinduction putBit : 𝟚 → IO 𝟙 putBit 1₂ = putStr "1" putBit 0₂ = putStr "0" putBits : ∀ {n} → Vec 𝟚 n → IO 𝟙 putBits [] = return _ putBits (x ∷ bs) = ♯ putBit x >> ♯ putBits bs put× : ∀ {A B : Set} → (A → IO 𝟙) → (B → IO 𝟙) → (A × B) → IO 𝟙 put× pA pB (x , y) = ♯ pA x >> ♯ pB y {- main : IO.Primitive.IO 𝟙 main = IO.run (put× putBits (put× putBits (put× putBits (put× putBits putBits))) AgdaSHA1.SHA1-on-0s) -} firstBit : ∀ {A : Set} → (V.Vec 𝟚 32 × A) → 𝟚 firstBit ((b ∷ _) , _) = b import FunUniverse.Cost as Cost open import Data.Nat.Show sha1-cost : ℕ sha1-cost = FunSHA1.SHA1-on-0s Cost.timeOps main : IO.Primitive.IO 𝟙 --main = IO.run (putBit (firstBit (AgdaSHA1.SHA1-on-0s _))) main = IO.run (putStrLn (show sha1-cost)) -- -}
Fix sha1...
Fix sha1...
Agda
bsd-3-clause
crypto-agda/crypto-agda
bfcd2eb5d4fab7bcb698719c3f02fc2aea768cf3
lib/Data/Tree/Binary.agda
lib/Data/Tree/Binary.agda
{-# OPTIONS --without-K #-} open import Type hiding (★) module Data.Tree.Binary where data BinTree {a} (A : ★ a) : ★ a where empty : BinTree A leaf : A → BinTree A fork : (ℓ r : BinTree A) → BinTree A
{-# OPTIONS --without-K #-} open import Type hiding (★) open import Level open import Data.Zero open import Data.Sum module Data.Tree.Binary where data BinTree {a} (A : ★ a) : ★ a where empty : BinTree A leaf : A → BinTree A fork : (ℓ r : BinTree A) → BinTree A Any : ∀ {a p}{A : ★ a}(P : A → ★ p) → BinTree A → ★ p Any P empty = Lift 𝟘 Any P (leaf x) = P x Any P (fork ts ts₁) = Any P ts ⊎ Any P ts₁
Add Any predicate for binary tree
Add Any predicate for binary tree
Agda
bsd-3-clause
crypto-agda/agda-nplib
5c1ef9d73912d2e59b329ff1866bf20c6dddefc2
Crypto/JS/BigI/CyclicGroup.agda
Crypto/JS/BigI/CyclicGroup.agda
{-# OPTIONS --without-K #-} open import Type.Eq open import FFI.JS using (Bool; trace-call; _++_) open import FFI.JS.Check renaming (check to check?) --renaming (warn-check to check?) open import FFI.JS.BigI open import Data.List.Base using (List; foldr) open import Data.Two hiding (_==_) open import Relation.Binary.PropositionalEquality open import Algebra.Raw open import Algebra.Group -- TODO carry on a primality proof of p module Crypto.JS.BigI.CyclicGroup (p : BigI) where abstract ℤ[_]★ : Set ℤ[_]★ = BigI private ℤp★ : Set ℤp★ = BigI mod-p : BigI → ℤp★ mod-p x = mod x p -- There are two ways to go from BigI to ℤp★: check and mod-p -- Use check for untrusted input data and mod-p for internal -- computation. BigI▹ℤ[_]★ : BigI → ℤp★ BigI▹ℤ[_]★ = -- trace-call "BigI▹ℤ[_]★ " λ x → (check? (x <I p) (λ _ → "Not below the modulus: p:" ++ toString p ++ " is less than x:" ++ toString x) (check? (x >I 0I) (λ _ → "Should be strictly positive: " ++ toString x ++ " <= 0") x)) check-non-zero : ℤp★ → BigI check-non-zero = -- trace-call "check-non-zero " λ x → check? (x >I 0I) (λ _ → "Should be non zero") x repr : ℤp★ → BigI repr x = x 1# : ℤp★ 1# = 1I 1/_ : ℤp★ → ℤp★ 1/ x = modInv (check-non-zero x) p _^_ : ℤp★ → BigI → ℤp★ x ^ y = modPow x y p _*_ _/_ : ℤp★ → ℤp★ → ℤp★ x * y = mod-p (multiply (repr x) (repr y)) x / y = x * 1/ y instance ℤ[_]★-Eq? : Eq? ℤp★ ℤ[_]★-Eq? = record { _==_ = _=='_ ; ≡⇒== = ≡⇒==' ; ==⇒≡ = ==⇒≡' } where _=='_ : ℤp★ → ℤp★ → 𝟚 x ==' y = equals (repr x) (repr y) postulate ≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y) ==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y prod : List ℤp★ → ℤp★ prod = foldr _*_ 1# mon-ops : Monoid-Ops ℤp★ mon-ops = _*_ , 1# grp-ops : Group-Ops ℤp★ grp-ops = mon-ops , 1/_ postulate grp-struct : Group-Struct grp-ops grp : Group ℤp★ grp = grp-ops , grp-struct module grp = Group grp -- -} -- -} -- -} -- -} -- -}
{-# OPTIONS --without-K #-} open import Type.Eq open import FFI.JS using (Bool; trace-call; _++_) open import FFI.JS.Check renaming (check to check?) --renaming (warn-check to check?) open import FFI.JS.BigI open import Data.List.Base using (List; foldr) open import Data.Two hiding (_==_) open import Relation.Binary.PropositionalEquality open import Algebra.Raw open import Algebra.Group -- TODO carry on a primality proof of p module Crypto.JS.BigI.CyclicGroup (p : BigI) where abstract ℤ[_]★ : Set ℤ[_]★ = BigI private ℤp★ : Set ℤp★ = BigI mod-p : BigI → ℤp★ mod-p x = mod x p -- There are two ways to go from BigI to ℤp★: check and mod-p -- Use check for untrusted input data and mod-p for internal -- computation. BigI▹ℤ[_]★ : BigI → ℤp★ BigI▹ℤ[_]★ = -- trace-call "BigI▹ℤ[_]★ " λ x → (check? (x <I p) (λ _ → "Not below the modulus: p:" ++ toString p ++ " is less than x:" ++ toString x) (check? (x >I 0I) (λ _ → "Should be strictly positive: " ++ toString x ++ " <= 0") x)) repr : ℤp★ → BigI repr x = x 1# : ℤp★ 1# = 1I 1/_ : ℤp★ → ℤp★ 1/ x = modInv x p _^_ : ℤp★ → BigI → ℤp★ x ^ y = modPow x y p _*_ _/_ : ℤp★ → ℤp★ → ℤp★ x * y = mod-p (multiply (repr x) (repr y)) x / y = x * 1/ y instance ℤ[_]★-Eq? : Eq? ℤp★ ℤ[_]★-Eq? = record { _==_ = _=='_ ; ≡⇒== = ≡⇒==' ; ==⇒≡ = ==⇒≡' } where _=='_ : ℤp★ → ℤp★ → 𝟚 x ==' y = equals (repr x) (repr y) postulate ≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y) ==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y prod : List ℤp★ → ℤp★ prod = foldr _*_ 1# mon-ops : Monoid-Ops ℤp★ mon-ops = _*_ , 1# grp-ops : Group-Ops ℤp★ grp-ops = mon-ops , 1/_ postulate grp-struct : Group-Struct grp-ops grp : Group ℤp★ grp = grp-ops , grp-struct module grp = Group grp -- -} -- -} -- -} -- -} -- -}
Remove a useless check: the point of ℤp★ is to not have 0
Remove a useless check: the point of ℤp★ is to not have 0
Agda
bsd-3-clause
crypto-agda/crypto-agda
e6dc4b68e44d8d340a5227f0ad0a4240a5f086a9
Attack/Compression.agda
Attack/Compression.agda
{-# OPTIONS --copatterns #-} -- Compression can be used an an Oracle to defeat encryption. -- Here we show how compressing before encrypting lead to a -- NOT semantically secure construction (IND-CPA). module Attack.Compression where open import Type using (★) open import Function open import Data.Nat.NP open import Data.Two renaming (_==_ to _==ᵇ_) open import Data.Product open import Data.Zero open import Relation.Binary.PropositionalEquality.NP import Game.IND-CPA record Sized (A : ★) : ★ where field size : A → ℕ open Sized {{...}} module _ {A B} {{_ : Sized A}} {{_ : Sized B}} where -- Same size _==ˢ_ : A → B → 𝟚 x ==ˢ y = size x == size y -- Same size _≡ˢ_ : A → B → ★ x ≡ˢ y = size x ≡ size y ≡ˢ→==ˢ : ∀ {x y} → x ≡ˢ y → (x ==ˢ y) ≡ 1₂ ≡ˢ→==ˢ {x} {y} x≡ˢy rewrite x≡ˢy = ✓→≡ (==.refl {size y}) ==ˢ→≡ˢ : ∀ {x y} → (x ==ˢ y) ≡ 1₂ → x ≡ˢ y ==ˢ→≡ˢ p = ==.sound _ _ (≡→✓ p) module _ {PubKey Message CipherText Rₑ : ★} (Enc : PubKey → Message → Rₑ → CipherText) {{_ : Sized Message}} {{_ : Sized CipherText}} where -- Encryption size is independant of the randomness EncSizeRndInd = ∀ {pk m r₀ r₁} → Enc pk m r₀ ≡ˢ Enc pk m r₁ -- Encrypted ciphertexts of the same size, will lead to messages of the same size EncLeakSize = ∀ {pk m₀ m₁ r₀ r₁} → Enc pk m₀ r₀ ≡ˢ Enc pk m₁ r₁ → m₀ ≡ˢ m₁ module M {Message CompressedMessage : ★} {{_ : Sized CompressedMessage}} (compress : Message → CompressedMessage) -- 2 messages which have different size after compression (m₀ m₁ : Message) (different-compression : size (compress m₀) ≢ size (compress m₁)) (PubKey : ★) (SecKey : ★) (CipherText : ★) {{_ : Sized CipherText}} (Rₑ Rₖ Rₓ : ★) (KeyGen : Rₖ → PubKey × SecKey) (Enc : PubKey → CompressedMessage → Rₑ → CipherText) (EncSizeRndInd : EncSizeRndInd Enc) (EncLeakSize : EncLeakSize Enc) where -- Our adversary runs one encryption Rₐ = Rₑ CEnc : PubKey → Message → Rₑ → CipherText CEnc pk m rₑ = Enc pk (compress m) rₑ module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText Rₑ Rₖ Rₐ Rₓ KeyGen CEnc open IND-CPA.Adversary A : IND-CPA.Adversary m A = λ _ _ → [0: m₀ 1: m₁ ] b′ A = λ rₑ pk c → c ==ˢ CEnc pk m₁ rₑ -- The adversary A is always winning. A-always-wins : ∀ b r → IND-CPA.EXP b A r ≡ b A-always-wins 0₂ _ = ≢1→≡0 (different-compression ∘ EncLeakSize ∘ ==ˢ→≡ˢ) A-always-wins 1₂ _ = ≡ˢ→==ˢ EncSizeRndInd lem : ∀ x y → (x ==ᵇ y) ≡ 0₂ → not (x ==ᵇ y) ≡ 1₂ lem 1₂ 1₂ = λ () lem 1₂ 0₂ = λ _ → refl lem 0₂ 1₂ = λ _ → refl lem 0₂ 0₂ = λ () {- A-always-wins' : ∀ r → IND-CPA.game A r ≡ 1₂ A-always-wins' (0₂ , r) = {!lem (not (IND-CPA.EXP 0₂ {!A!} r)) (IND-CPA.EXP 1₂ A r) (A-always-wins 0₂ r)!} A-always-wins' (1₂ , r) = A-always-wins 1₂ r -}
{-# OPTIONS --copatterns #-} -- Compression can be used an an Oracle to defeat encryption. -- Here we show how compressing before encrypting lead to a -- NOT semantically secure construction (IND-CPA). module Attack.Compression where open import Type using (★) open import Function.NP open import Data.Nat.NP open import Data.Two renaming (_==_ to _==ᵇ_) open import Data.Product open import Data.Zero open import Relation.Binary.PropositionalEquality.NP import Game.IND-CPA record Sized (A : ★) : ★ where field size : A → ℕ open Sized {{...}} module EqSized {A B : ★} {{_ : Sized A}} {{_ : Sized B}} where -- Same size _==ˢ_ : A → B → 𝟚 x ==ˢ y = size x == size y -- Same size _≡ˢ_ : A → B → ★ x ≡ˢ y = size x ≡ size y ≡ˢ→==ˢ : ∀ {x y} → x ≡ˢ y → (x ==ˢ y) ≡ 1₂ ≡ˢ→==ˢ {x} {y} x≡ˢy rewrite x≡ˢy = ✓→≡ (==.refl {size y}) ==ˢ→≡ˢ : ∀ {x y} → (x ==ˢ y) ≡ 1₂ → x ≡ˢ y ==ˢ→≡ˢ p = ==.sound _ _ (≡→✓ p) module EncSized {PubKey Message CipherText Rₑ : ★} (enc : PubKey → Message → Rₑ → CipherText) {{_ : Sized Message}} {{_ : Sized CipherText}} where open EqSized -- Encryption size is independant of the randomness EncSizeRndInd = ∀ {pk m r₀ r₁} → enc pk m r₀ ≡ˢ enc pk m r₁ -- Encrypted ciphertexts of the same size, will lead to messages of the same size EncLeakSize = ∀ {pk m₀ m₁ r₀ r₁} → enc pk m₀ r₀ ≡ˢ enc pk m₁ r₁ → m₀ ≡ˢ m₁ module M {Message CompressedMessage : ★} {{_ : Sized CompressedMessage}} (compress : Message → CompressedMessage) -- 2 messages which have different size after compression (m₀ m₁ : Message) (different-compression : size (compress m₀) ≢ size (compress m₁)) (PubKey : ★) (SecKey : ★) (CipherText : ★) {{_ : Sized CipherText}} (Rₑ Rₖ Rₓ : ★) (KeyGen : Rₖ → PubKey × SecKey) (enc : PubKey → CompressedMessage → Rₑ → CipherText) (open EncSized enc) (encSizeRndInd : EncSizeRndInd) (encLeakSize : EncLeakSize) where -- Our adversary runs one encryption Rₐ = Rₑ CEnc : PubKey → Message → Rₑ → CipherText CEnc pk m rₑ = enc pk (compress m) rₑ module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText Rₑ Rₖ Rₐ Rₓ KeyGen CEnc open IND-CPA.Adversary open EqSized {CipherText}{CipherText} {{it}} {{it}} A : IND-CPA.Adversary m A = λ _ _ → [0: m₀ 1: m₁ ] b′ A = λ rₑ pk c → c ==ˢ CEnc pk m₁ rₑ -- The adversary A is always winning. A-always-wins : ∀ b r → IND-CPA.EXP b A r ≡ b A-always-wins 0₂ _ = ≢1→≡0 (different-compression ∘′ encLeakSize ∘′ ==ˢ→≡ˢ) A-always-wins 1₂ _ = ≡ˢ→==ˢ encSizeRndInd -- One should be able to derive this one from A-always-wins and the game-flipping general lemma in the exploration lib {- A-always-wins' : ∀ r → IND-CPA.game A r ≡ 1₂ A-always-wins' (0₂ , r) = {!lem (not (IND-CPA.EXP 0₂ {!A!} r)) (IND-CPA.EXP 1₂ A r) (A-always-wins 0₂ r)!} where lem : ∀ x y → (x ==ᵇ y) ≡ 0₂ → not (x ==ᵇ y) ≡ 1₂ lem 1₂ 1₂ = λ () lem 1₂ 0₂ = λ _ → refl lem 0₂ 1₂ = λ _ → refl lem 0₂ 0₂ = λ () A-always-wins' (1₂ , r) = A-always-wins 1₂ r -}
work around the instance argument becoming weaker...
Attack.Compression: work around the instance argument becoming weaker...
Agda
bsd-3-clause
crypto-agda/crypto-agda
eb332321cb55523e6068bacced76b6f0bc2f6f7b
Parametric/Change/Validity.agda
Parametric/Change/Validity.agda
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra as CA using ( ChangeAlgebra ; ChangeAlgebraFamily ; change-algebra₍_₎ ; family ; Δ₍_₎ ) open import Level Structure : Set₁ Structure = ChangeAlgebraFamily zero ⟦_⟧Base module Structure (change-algebra-base : Structure) where change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type change-algebra (base ι) = change-algebra₍ ι ₎ change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type change-algebra-family = family change-algebra --------------- -- Interface -- --------------- open CA public using ( Δ₍_₎ ; update′ ; diff′ ; nil₍_₎ ; update-diff₍_₎ ; update-nil₍_₎ ) -------------------- -- Implementation -- -------------------- module _ {τ₁ τ₂ : Type} where open CA.FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public using ( ) renaming ( correct to is-valid ; apply to call-change ) open CA public using ( before ; after ; before₍_₎ ; after₍_₎ ) ------------------ -- Environments -- ------------------ open CA.FunctionChanges public using (cons) open CA public using () renaming ( [] to ∅ ; _∷_ to _•_ ) open CA.ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming ( changeAlgebra to environment-changes ) after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧ after-env {Γ} = after₍ Γ ₎
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Base.Change.Algebra as CA using (ChangeAlgebraFamily) open import Level Structure : Set₁ Structure = ChangeAlgebraFamily zero ⟦_⟧Base module Structure (change-algebra-base : Structure) where -- change algebras open CA public renaming ( [] to ∅ ; _∷_ to _•_ ) -- change algebra for every type change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type change-algebra (base ι) = change-algebra₍ ι ₎ change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type change-algebra-family = family change-algebra -- function changes open FunctionChanges public using (cons) module _ {τ₁ τ₂ : Type} where open FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public renaming ( correct to is-valid ; apply to call-change ) -- environment changes open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming ( changeAlgebra to environment-changes ) after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧ after-env {Γ} = after₍ Γ ₎
Simplify reexports.
Simplify reexports. Merge open declarations, reexport everything. Old-commit-hash: 69a4b0415ae43f3db65282495914ddf7a6fe06fd
Agda
mit
inc-lc/ilc-agda
9e4e42995f871b15f24591bd95cac30bfecb1bda
Game/Transformation/CPAd-CPA.agda
Game/Transformation/CPAd-CPA.agda
{-# OPTIONS --without-K --copatterns #-} open import Type open import Data.Bit open import Data.Maybe open import Data.Product open import Data.One open import Control.Strategy renaming (run to runStrategy; map to mapStrategy) open import Function open import Relation.Binary.PropositionalEquality import Game.IND-CPA-dagger import Game.IND-CPA module Game.Transformation.CPAd-CPA (PubKey : ★) (SecKey : ★) (Message : ★) (CipherText : ★) -- randomness supply for, encryption, key-generation, adversary, adversary state (Rₑ Rₖ Rₐ : ★) (KeyGen : Rₖ → PubKey × SecKey) (Enc : PubKey → Message → Rₑ → CipherText) (Dec : SecKey → CipherText → Message) where module CPA† = Game.IND-CPA-dagger PubKey SecKey Message CipherText Rₑ Rₖ Rₐ 𝟙 KeyGen Enc module CPA = Game.IND-CPA PubKey SecKey Message CipherText Rₑ Rₖ Rₐ 𝟙 KeyGen Enc {- f : (Message × Message) × (CipherText → DecRound Bit) → (Message × Message) × (CipherText → CipherText → DecRound Bit) f (m , g) = m , λ c _ → g c -} R-transform : CPA†.R → CPA.R R-transform (rₐ , rₖ , rₑ , _ , _) = rₐ , rₖ , rₑ , _ module _ (A : CPA.Adversary) where open CPA.Adversary A† : CPA†.Adversary m A† = m A b′ A† rₐ pk c₀ c₁ = b′ A rₐ pk c₀ lemma : ∀ b t r → CPA.EXP b A (R-transform r) ≡ CPA†.EXP b t A† r lemma _ _ _ = refl -- If we are able to do the transformation, then we get the same advantage correct : ∀ b r → CPA.EXP b A (R-transform r) ≡ CPA†.EXP b (not b) A† r correct _ _ = refl -- -}
{-# OPTIONS --without-K --copatterns #-} open import Type open import Data.Bit open import Data.Maybe open import Data.Product open import Data.One open import Control.Strategy renaming (run to runStrategy; map to mapStrategy) open import Function open import Relation.Binary.PropositionalEquality import Game.IND-CPA-dagger import Game.IND-CPA module Game.Transformation.CPAd-CPA (PubKey : ★) (SecKey : ★) (Message : ★) (CipherText : ★) -- randomness supply for, encryption, key-generation, adversary, adversary state (Rₑ Rₖ Rₐ : ★) (KeyGen : Rₖ → PubKey × SecKey) (Enc : PubKey → Message → Rₑ → CipherText) (Dec : SecKey → CipherText → Message) where module CPA† = Game.IND-CPA-dagger PubKey SecKey Message CipherText Rₑ Rₖ Rₐ 𝟙 KeyGen Enc module CPA = Game.IND-CPA PubKey SecKey Message CipherText Rₑ Rₖ Rₐ 𝟙 KeyGen Enc R-transform : CPA†.R → CPA.R R-transform (rₐ , rₖ , rₑ , _ , _) = rₐ , rₖ , rₑ , _ module _ (A : CPA.Adversary) where open CPA†.Adversary module A = CPA.Adversary A A† : CPA†.Adversary m A† = A.m b′ A† rₐ pk c₀ c₁ = A.b′ rₐ pk c₀ lemma : ∀ b t r → CPA.EXP b A (R-transform r) ≡ CPA†.EXP b t A† r lemma _ _ _ = refl -- If we are able to do the transformation, then we get the same advantage correct : ∀ b r → CPA.EXP b A (R-transform r) ≡ CPA†.EXP b (not b) A† r correct _ _ = refl -- -}
fix copattern error in CPAd-CPA
fix copattern error in CPAd-CPA
Agda
bsd-3-clause
crypto-agda/crypto-agda