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
|
---|---|---|---|---|---|---|---|---|---|
785dddce82b3dc294f8d52a3a8a96b64366b1ac4
|
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
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
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 ≙₁ ≙₂
-- 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
open import Postulate.Extensionality
-- 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₂
∎
|
------------------------------------------------------------------------
-- 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 Structure where
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 ≙₁ ≙₂
-- 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₂
∎
|
Remove more
|
Remove more
No Structure in Base.
Old-commit-hash: 81cee315c1ade9b842f0400fbc2bd215a189e8dc
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2aa331ecc397f402c621e7b4b53886eb5ca53e91
|
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
-- 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₂ (diffσ {Γ}) s t
_⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t
_⊕τ_ = λ {Γ} t Δt → 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 {τ} {Γ})
|
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))
lift-diff :
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
lift-diff = λ {τ Γ} →
proj₁ (lift-diff-apply {τ} {Γ})
lift-apply :
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply = λ {τ Γ} →
proj₂ (lift-diff-apply {τ} {Γ})
|
Use HOAS-enabled helpers for nested abstractions.
|
Use HOAS-enabled helpers for nested abstractions.
Old-commit-hash: f5006b448dcc30a8ff103cf5de501e945e14faa1
|
Agda
|
mit
|
inc-lc/ilc-agda
|
24ce7b62d29582982f0f0c2694ebd10a473113aa
|
Game/IND-CCA2.agda
|
Game/IND-CCA2.agda
|
{-# OPTIONS --without-K #-}
open import Type
open import Data.Bit
open import Data.Maybe
open import Data.Product
open import Data.Unit
open import Data.Nat.NP
open import Rat
open import Explore.Type
open import Explore.Explorable
open import Explore.Product
open Operators
open import Relation.Binary.PropositionalEquality
module Game.IND-CCA2
(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
open import Game.CCA-Common Message CipherText
Adv : ★
Adv = Rₐ → PubKey → Strategy ((Message × Message) × (CipherText → Strategy Bit))
{-
Valid-Adv : Adv → Set
Valid-Adv adv = ∀ {rₐ rₓ pk c} → Valid (λ x → x ≢ c) {!!}
-}
R : ★
R = Rₐ × Rₖ × Rₑ
Game : ★
Game = Adv → R → Bit
⅁ : Bit → Game
⅁ b m (rₐ , rₖ , rₑ) with KeyGen rₖ
... | pk , sk = b′ where
open Eval Dec sk
ev = eval (m rₐ pk)
mb = proj (proj₁ ev) b
d = proj₂ ev
c = Enc pk mb rₑ
b′ = eval (d c)
module Advantage
(μₑ : Explore₀ Rₑ)
(μₖ : Explore₀ Rₖ)
(μₐ : Explore₀ Rₐ)
where
μR : Explore₀ R
μR = μₐ ×ᵉ μₖ ×ᵉ μₑ
module μR = FromExplore₀ μR
run : Bit → Adv → ℕ
run b adv = μR.count (⅁ b adv)
Advantage : Adv → ℕ
Advantage adv = dist (run 0b adv) (run 1b adv)
Advantageℚ : Adv → ℚ
Advantageℚ adv = Advantage adv / μR.Card
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type
open import Data.Bit
open import Data.Maybe
open import Data.Product
open import Data.Unit
open import Data.Nat.NP
--open import Rat
open import Explore.Type
open import Explore.Explorable
open import Explore.Product
open Operators
open import Relation.Binary.PropositionalEquality
module Game.IND-CCA2
(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
open import Game.CCA-Common Message CipherText
Adv : ★
Adv = Rₐ → PubKey → Strategy ((Message × Message) × (CipherText → Strategy Bit))
{-
Valid-Adv : Adv → Set
Valid-Adv adv = ∀ {rₐ rₓ pk c} → Valid (λ x → x ≢ c) {!!}
-}
R : ★
R = Rₐ × Rₖ × Rₑ
Game : ★
Game = Adv → R → Bit
⅁ : Bit → Game
⅁ b m (rₐ , rₖ , rₑ) with KeyGen rₖ
... | pk , sk = b′ where
open Eval Dec sk
ev = eval (m rₐ pk)
mb = proj (proj₁ ev) b
d = proj₂ ev
c = Enc pk mb rₑ
b′ = eval (d c)
module Advantage
(μₑ : Explore₀ Rₑ)
(μₖ : Explore₀ Rₖ)
(μₐ : Explore₀ Rₐ)
where
μR : Explore₀ R
μR = μₐ ×ᵉ μₖ ×ᵉ μₑ
module μR = FromExplore₀ μR
run : Bit → Adv → ℕ
run b adv = μR.count (⅁ b adv)
Advantage : Adv → ℕ
Advantage adv = dist (run 0b adv) (run 1b adv)
--Advantageℚ : Adv → ℚ
--Advantageℚ adv = Advantage adv / μR.Card
-- -}
-- -}
-- -}
-- -}
|
Hide the Rat code
|
IND-CCA2: Hide the Rat code
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
16f8a95227efdacdb93599ca7b8ddfd847c2e47a
|
Parametric/Denotation/Value.agda
|
Parametric/Denotation/Value.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Value domains for languages described in Plotkin style
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
module Parametric.Denotation.Value
(Base : Type.Structure)
where
open import Base.Denotation.Notation
open Type.Structure Base
Structure : Set₁
Structure = Base → Set
module Structure (⟦_⟧Base : Structure) where
⟦_⟧Type : Type -> Set
⟦ base ι ⟧Type = ⟦ ι ⟧Base
⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
open import Base.Denotation.Environment Type ⟦_⟧Type public
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Values for standard evaluation (Def. 3.1 and 3.2, Fig. 4c and 4f).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
module Parametric.Denotation.Value
(Base : Type.Structure)
where
open import Base.Denotation.Notation
open Type.Structure Base
-- Extension point: Values for base types.
Structure : Set₁
Structure = Base → Set
module Structure (⟦_⟧Base : Structure) where
-- We provide: Values for arbitrary types.
⟦_⟧Type : Type -> Set
⟦ base ι ⟧Type = ⟦ ι ⟧Base
⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type
-- This means: Overload ⟦_⟧ to mean ⟦_⟧Type.
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- We also provide: Environments of such values.
open import Base.Denotation.Environment Type ⟦_⟧Type public
|
Document P.D.Value.
|
Document P.D.Value.
Old-commit-hash: bfaab88fbd6365ff0b9046dd999ccd1362c526d6
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a2f7958b6a6e7245442d1e150092aacdc7c8707d
|
examples/Propositional.agda
|
examples/Propositional.agda
|
module Propositional where
open import Data.Nat
open import Relation.Binary.PropositionalEquality
open import Holes.Term using (⌞_⌟)
open import Holes.Cong.Propositional
open ≡-Reasoning
--------------------------------------------------------------------------------
-- Some easy lemmas
--------------------------------------------------------------------------------
+-zero : ∀ a → a + zero ≡ a
+-zero zero = refl
+-zero (suc a) rewrite +-zero a = refl
+-suc : ∀ a b → a + suc b ≡ suc (a + b)
+-suc zero b = refl
+-suc (suc a) b rewrite +-suc a b = refl
+-assoc : ∀ a b c → a + b + c ≡ a + (b + c)
+-assoc zero b c = refl
+-assoc (suc a) b c rewrite +-assoc a b c = refl
--------------------------------------------------------------------------------
-- Proofs by equational reasoning
--------------------------------------------------------------------------------
-- commutativity of addition proved traditionally
+-comm₁ : ∀ a b → a + b ≡ b + a
+-comm₁ zero b = sym (+-zero b)
+-comm₁ (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong suc (+-comm₁ a b) ⟩
suc (b + a) ≡⟨ sym (+-suc b a) ⟩
b + suc a ∎
-- commutativity of addition proved with holes
+-comm : ∀ a b → a + b ≡ b + a
+-comm zero b = sym (+-zero b)
+-comm (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong! (+-comm a b) ⟩
suc (b + a) ≡⟨ cong! (+-suc b a) ⟩
b + suc a ∎
-- distributivity of multiplication over addition proved traditionally
*-distrib-+₁ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+₁ zero b c = refl
*-distrib-+₁ (suc a) b c =
b + c + a * (b + c) ≡⟨ cong (λ h → b + c + h) (*-distrib-+₁ a b c) ⟩
(b + c) + (a * b + a * c) ≡⟨ sym (+-assoc (b + c) (a * b) (a * c)) ⟩
((b + c) + a * b) + a * c ≡⟨ cong (λ h → h + a * c) (+-assoc b c (a * b)) ⟩
(b + (c + a * b)) + a * c ≡⟨ cong (λ h → (b + h) + a * c) (+-comm c (a * b)) ⟩
(b + (a * b + c)) + a * c ≡⟨ cong (λ h → h + a * c) (sym (+-assoc b (a * b) c)) ⟩
((b + a * b) + c) + a * c ≡⟨ +-assoc (b + a * b) c (a * c) ⟩
(b + a * b) + (c + a * c)
∎
-- distributivity of multiplication over addition proved with holes
*-distrib-+ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+ zero b c = refl
*-distrib-+ (suc a) b c =
b + c + ⌞ a * (b + c) ⌟ ≡⟨ cong! (*-distrib-+ a b c) ⟩
(b + c) + (a * b + a * c) ≡⟨ cong! (+-assoc (b + c) (a * b) (a * c)) ⟩
⌞ (b + c) + a * b ⌟ + a * c ≡⟨ cong! (+-assoc b c (a * b)) ⟩
(b + ⌞ c + a * b ⌟) + a * c ≡⟨ cong! (+-comm c (a * b)) ⟩
⌞ b + (a * b + c) ⌟ + a * c ≡⟨ cong! (+-assoc b (a * b) c) ⟩
⌞ ((b + a * b) + c) + a * c ⌟ ≡⟨ cong! (+-assoc (b + a * b) c (a * c)) ⟩
(b + a * b) + (c + a * c)
∎
|
module Propositional where
open import Data.Nat
open import Relation.Binary.PropositionalEquality
open import Holes.Term using (⌞_⌟)
open import Holes.Cong.Propositional
open ≡-Reasoning
--------------------------------------------------------------------------------
-- Some easy lemmas
--------------------------------------------------------------------------------
+-zero : ∀ a → a + zero ≡ a
+-zero zero = refl
+-zero (suc a) rewrite +-zero a = refl
+-suc : ∀ a b → a + suc b ≡ suc (a + b)
+-suc zero b = refl
+-suc (suc a) b rewrite +-suc a b = refl
+-assoc : ∀ a b c → a + b + c ≡ a + (b + c)
+-assoc zero b c = refl
+-assoc (suc a) b c rewrite +-assoc a b c = refl
--------------------------------------------------------------------------------
-- Proofs by equational reasoning
--------------------------------------------------------------------------------
-- commutativity of addition proved traditionally
+-comm₁ : ∀ a b → a + b ≡ b + a
+-comm₁ zero b = sym (+-zero b)
+-comm₁ (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong suc (+-comm₁ a b) ⟩
suc (b + a) ≡⟨ sym (+-suc b a) ⟩
b + suc a ∎
-- commutativity of addition proved with holes
+-comm : ∀ a b → a + b ≡ b + a
+-comm zero b = sym (+-zero b)
+-comm (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong! (+-comm a b) ⟩
suc (b + a) ≡⟨ cong! (+-suc b a) ⟩
b + suc a ∎
-- distributivity of multiplication over addition proved traditionally
*-distrib-+₁ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+₁ zero b c = refl
*-distrib-+₁ (suc a) b c =
b + c + a * (b + c) ≡⟨ cong (λ h → b + c + h) (*-distrib-+₁ a b c) ⟩
(b + c) + (a * b + a * c) ≡⟨ sym (+-assoc (b + c) (a * b) (a * c)) ⟩
((b + c) + a * b) + a * c ≡⟨ cong (λ h → h + a * c) (+-assoc b c (a * b)) ⟩
(b + (c + a * b)) + a * c ≡⟨ cong (λ h → (b + h) + a * c) (+-comm c (a * b)) ⟩
(b + (a * b + c)) + a * c ≡⟨ cong (λ h → h + a * c) (sym (+-assoc b (a * b) c)) ⟩
((b + a * b) + c) + a * c ≡⟨ +-assoc (b + a * b) c (a * c) ⟩
(b + a * b) + (c + a * c)
∎
-- distributivity of multiplication over addition proved with holes
*-distrib-+ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+ zero b c = refl
*-distrib-+ (suc a) b c =
b + c + ⌞ a * (b + c) ⌟ ≡⟨ cong! (*-distrib-+ a b c) ⟩
⌞ (b + c) + (a * b + a * c) ⌟ ≡⟨ cong! (+-assoc (b + c) (a * b) (a * c)) ⟩
⌞ (b + c) + a * b ⌟ + a * c ≡⟨ cong! (+-assoc b c (a * b)) ⟩
(b + ⌞ c + a * b ⌟) + a * c ≡⟨ cong! (+-comm c (a * b)) ⟩
⌞ b + (a * b + c) ⌟ + a * c ≡⟨ cong! (+-assoc b (a * b) c) ⟩
⌞ ((b + a * b) + c) + a * c ⌟ ≡⟨ cong! (+-assoc (b + a * b) c (a * c)) ⟩
(b + a * b) + (c + a * c)
∎
|
Add a hole in the examples that's strictly unnecessary
|
Add a hole in the examples that's strictly unnecessary
... but potentially confusing if it isn't there
|
Agda
|
mit
|
bch29/agda-holes
|
48d1be2bb4bb61634b15fbd0cff064c02be1f4f9
|
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
-- 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.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
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 (_⋎_)
|
Comment on handling of contexts
|
Comment on handling of contexts
Old-commit-hash: 0bfc0dfdc024921016f8df0d8252aa20f5f0f8d6
|
Agda
|
mit
|
inc-lc/ilc-agda
|
7ec8bed818d248b395b4b0b2c2d2f21361f940bc
|
Thesis/LangOps.agda
|
Thesis/LangOps.agda
|
module Thesis.LangOps where
open import Thesis.Lang
open import Thesis.Changes
open import Thesis.LangChanges
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
oplusτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ ⇒ τ)
ominusτo : ∀ {Γ} τ → Term Γ (τ ⇒ τ ⇒ Δt τ)
onilτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ)
onilτo τ = abs (app₂ (ominusτo τ) (var this) (var this))
-- Do NOT try to read this, such terms are write-only. But the behavior is
-- specified to be oplusτ-equiv and ominusτ-equiv.
oplusτo (σ ⇒ τ) = abs (abs (abs
(app₂ (oplusτo τ)
(app (var (that (that this))) (var this))
(app₂ (var (that this)) (var this) (app (onilτo σ) (var this))))))
oplusτo unit = abs (abs (const unit))
oplusτo int = const plus
oplusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (oplusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (oplusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
oplusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const linj) (app₂ (oplusτo σ) (var (that (that this))) (var this))))
(abs (app (const linj) (var (that (that this)))))))
(abs (var this))))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const rinj) (var (that (that this)))))
(abs (app (const rinj) (app₂ (oplusτo τ) (var (that (that this))) (var this))))))
(abs (var this))))))
ominusτo (σ ⇒ τ) = abs (abs (abs (abs (app₂ (ominusτo τ)
(app (var (that (that (that this)))) (app₂ (oplusτo σ) (var (that this)) (var this)))
(app (var (that (that this))) (var (that this)))))))
ominusτo unit = abs (abs (const unit))
ominusτo int = const minus
ominusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (ominusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (ominusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
ominusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app (const linj) (app (const linj) (app₂ (ominusτo σ) (var (that this)) (var this)))))
(abs (app (const rinj) (var (that (that (that this))))))))
(abs (app₃ (const match) (var (that this))
(abs (app (const rinj) (var (that (that (that this))))))
(abs (app (const linj) (app (const rinj) (app₂ (ominusτo τ) (var (that this)) (var this)))))))))
oplusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ a da → ⟦ oplusτo τ ⟧Term ρ a da ≡ a ⊕ da
ominusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ b a → ⟦ ominusτo τ ⟧Term ρ b a ≡ b ⊝ a
oplusτ-equiv-ext : ∀ τ Γ → ⟦ oplusτo {Γ} τ ⟧Term ≡ λ ρ → _⊕_
oplusτ-equiv-ext τ _ = ext³ (λ ρ a da → oplusτ-equiv _ ρ τ a da)
ominusτ-equiv-ext : ∀ τ Γ → ⟦ ominusτo {Γ} τ ⟧Term ≡ λ ρ → _⊝_
ominusτ-equiv-ext τ _ = ext³ (λ ρ a da → ominusτ-equiv _ ρ τ a da)
oplusτ-equiv Γ ρ (σ ⇒ τ) f df = ext (λ a → lemma a)
where
module _ (a : ⟦ σ ⟧Type) where
ρ′ = a • df • f • ρ
ρ′′ = a • ρ′
lemma : ⟦ oplusτo τ ⟧Term ρ′ (f a)
(df a (⟦ ominusτo σ ⟧Term ρ′′ a a))
≡ f a ⊕ df a (nil a)
lemma
rewrite ominusτ-equiv _ ρ′′ σ a a
| oplusτ-equiv _ ρ′ τ (f a) (df a (nil a))
= refl
oplusτ-equiv Γ ρ unit tt tt = refl
oplusτ-equiv Γ ρ int a da = refl
oplusτ-equiv Γ ρ (pair σ τ) (a , b) (da , db)
rewrite oplusτ-equiv _ ((da , db) • (a , b) • ρ) σ a da
| oplusτ-equiv _ ((da , db) • (a , b) • ρ) τ b db
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₁ dx))
rewrite oplusτ-equiv-ext σ (Δt σ • sum (Δt σ) (Δt τ) • σ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₂ dy)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₁ dx)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₂ dy))
rewrite oplusτ-equiv-ext τ (Δt τ • sum (Δt σ) (Δt τ) • τ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁) = refl
ominusτ-equiv Γ ρ (σ ⇒ τ) g f = ext (λ a → ext (lemma a))
where
module _ (a : ⟦ σ ⟧Type) (da : Chτ σ) where
ρ′ = da • a • f • g • ρ
lemma : ⟦ ominusτo τ ⟧Term (da • a • f • g • ρ)
(g (⟦ oplusτo σ ⟧Term (da • a • f • g • ρ) a da)) (f a)
≡ g (a ⊕ da) ⊝ f a
lemma
rewrite oplusτ-equiv _ ρ′ σ a da
| ominusτ-equiv _ ρ′ τ (g (a ⊕ da)) (f a) = refl
ominusτ-equiv Γ ρ unit tt tt = refl
ominusτ-equiv Γ ρ int b a = refl
ominusτ-equiv Γ ρ (pair σ τ) (a2 , b2) (a1 , b1)
rewrite ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) σ a2 a1
| ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) τ b2 b1
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ x₁)
rewrite ominusτ-equiv-ext σ (σ • σ • sum σ τ • sum σ τ • Γ)
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ x) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁)
rewrite ominusτ-equiv-ext τ (τ • τ • sum σ τ • sum σ τ • Γ)
= refl
|
module Thesis.LangOps where
open import Thesis.Lang
open import Thesis.Changes
open import Thesis.LangChanges
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
oplusτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ ⇒ τ)
ominusτo : ∀ {Γ} τ → Term Γ (τ ⇒ τ ⇒ Δt τ)
onilτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ)
onilτo τ = abs (app₂ (ominusτo τ) (var this) (var this))
-- Do NOT try to read this, such terms are write-only. But the behavior is
-- specified to be oplusτ-equiv and ominusτ-equiv.
oplusτo (σ ⇒ τ) = abs (abs (abs
(app₂ (oplusτo τ)
(app (var (that (that this))) (var this))
(app₂ (var (that this)) (var this) (app (onilτo σ) (var this))))))
oplusτo unit = abs (abs (const unit))
oplusτo int = const plus
oplusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (oplusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (oplusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
oplusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const linj) (app₂ (oplusτo σ) (var (that (that this))) (var this))))
(abs (app (const linj) (var (that (that this)))))))
(abs (var this))))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const rinj) (var (that (that this)))))
(abs (app (const rinj) (app₂ (oplusτo τ) (var (that (that this))) (var this))))))
(abs (var this))))))
ominusτo (σ ⇒ τ) = abs (abs (abs (abs (app₂ (ominusτo τ)
(app (var (that (that (that this)))) (app₂ (oplusτo σ) (var (that this)) (var this)))
(app (var (that (that this))) (var (that this)))))))
ominusτo unit = abs (abs (const unit))
ominusτo int = const minus
ominusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (ominusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (ominusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
ominusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app (const linj) (app (const linj) (app₂ (ominusτo σ) (var (that this)) (var this)))))
(abs (app (const rinj) (var (that (that (that this))))))))
(abs (app₃ (const match) (var (that this))
(abs (app (const rinj) (var (that (that (that this))))))
(abs (app (const linj) (app (const rinj) (app₂ (ominusτo τ) (var (that this)) (var this)))))))))
oplusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ a da → ⟦ oplusτo τ ⟧Term ρ a da ≡ a ⊕ da
ominusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ b a → ⟦ ominusτo τ ⟧Term ρ b a ≡ b ⊝ a
oplusτ-equiv-ext : ∀ τ Γ → ⟦ oplusτo {Γ} τ ⟧Term ≡ λ ρ → _⊕_
oplusτ-equiv-ext τ _ = ext³ (λ ρ a da → oplusτ-equiv _ ρ τ a da)
ominusτ-equiv-ext : ∀ τ Γ → ⟦ ominusτo {Γ} τ ⟧Term ≡ λ ρ → _⊝_
ominusτ-equiv-ext τ _ = ext³ (λ ρ a da → ominusτ-equiv _ ρ τ a da)
oplusτ-equiv Γ ρ (σ ⇒ τ) f df = ext (λ a → lemma a)
where
module _ (a : ⟦ σ ⟧Type) where
ρ′ = a • df • f • ρ
ρ′′ = a • ρ′
lemma : ⟦ oplusτo τ ⟧Term ρ′ (f a)
(df a (⟦ ominusτo σ ⟧Term ρ′′ a a))
≡ f a ⊕ df a (nil a)
lemma
rewrite ominusτ-equiv _ ρ′′ σ a a
| oplusτ-equiv _ ρ′ τ (f a) (df a (nil a))
= refl
oplusτ-equiv Γ ρ unit tt tt = refl
oplusτ-equiv Γ ρ int a da = refl
oplusτ-equiv Γ ρ (pair σ τ) (a , b) (da , db)
rewrite oplusτ-equiv _ ((da , db) • (a , b) • ρ) σ a da
| oplusτ-equiv _ ((da , db) • (a , b) • ρ) τ b db
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₁ dx))
rewrite oplusτ-equiv-ext σ (Δt σ • sum (Δt σ) (Δt τ) • σ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₂ dy)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₁ dx)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₂ dy))
rewrite oplusτ-equiv-ext τ (Δt τ • sum (Δt σ) (Δt τ) • τ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁) = refl
ominusτ-equiv Γ ρ (σ ⇒ τ) g f = ext (λ a → ext (lemma a))
where
module _ (a : ⟦ σ ⟧Type) (da : Chτ σ) where
ρ′ = da • a • f • g • ρ
lemma : ⟦ ominusτo τ ⟧Term (da • a • f • g • ρ)
(g (⟦ oplusτo σ ⟧Term (da • a • f • g • ρ) a da)) (f a)
≡ g (a ⊕ da) ⊝ f a
lemma
rewrite oplusτ-equiv _ ρ′ σ a da
| ominusτ-equiv _ ρ′ τ (g (a ⊕ da)) (f a) = refl
ominusτ-equiv Γ ρ unit tt tt = refl
ominusτ-equiv Γ ρ int b a = refl
ominusτ-equiv Γ ρ (pair σ τ) (a2 , b2) (a1 , b1)
rewrite ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) σ a2 a1
| ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) τ b2 b1
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ x₁)
rewrite ominusτ-equiv-ext σ (σ • σ • sum σ τ • sum σ τ • Γ)
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ x) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁)
rewrite ominusτ-equiv-ext τ (τ • τ • sum σ τ • sum σ τ • Γ)
= refl
-- Proved these lemmas to show them in thesis.
oplusτ-equiv-term : ∀ dΓ (dρ : ⟦ dΓ ⟧Context) τ t dt →
⟦ app₂ (oplusτo τ) t dt ⟧Term dρ ≡ ⟦ t ⟧Term dρ ⊕ ⟦ dt ⟧Term dρ
oplusτ-equiv-term dΓ dρ τ t dt = oplusτ-equiv dΓ dρ τ (⟦ t ⟧Term dρ) (⟦ dt ⟧Term dρ)
ominusτ-equiv-term : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ t2 t1 →
⟦ app₂ (ominusτo τ) t2 t1 ⟧Term ρ ≡ ⟦ t2 ⟧Term ρ ⊝ ⟦ t1 ⟧Term ρ
ominusτ-equiv-term Γ ρ τ t2 t1 = ominusτ-equiv Γ ρ τ (⟦ t2 ⟧Term ρ) (⟦ t1 ⟧Term ρ)
|
Add lemmas I present in thesis
|
Add lemmas I present in thesis
|
Agda
|
mit
|
inc-lc/ilc-agda
|
965133f11d3863769cb8e636eff326a49bc7321b
|
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
-- ℤp*
𝔾 : Set
𝔾 = BigI
private
mod-p : BigI → 𝔾
mod-p x = mod x p
-- There are two ways to go from BigI to 𝔾: check and mod-p
-- Use check for untrusted input data and mod-p for internal
-- computation.
BigI▹𝔾 : BigI → 𝔾
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 : 𝔾 → BigI
check-non-zero = -- trace-call "check-non-zero "
λ x → check? (x >I 0I) (λ _ → "Should be non zero") x
repr : 𝔾 → BigI
repr x = x
1# : 𝔾
1# = 1I
1/_ : 𝔾 → 𝔾
1/ x = modInv (check-non-zero x) p
_^_ : 𝔾 → BigI → 𝔾
x ^ y = modPow x y p
_*_ _/_ : 𝔾 → 𝔾 → 𝔾
x * y = mod-p (multiply (repr x) (repr y))
x / y = x * 1/ y
instance
𝔾-Eq? : Eq? 𝔾
𝔾-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : 𝔾 → 𝔾 → 𝟚
x ==' y = equals (repr x) (repr y)
postulate
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
prod : List 𝔾 → 𝔾
prod = foldr _*_ 1#
mon-ops : Monoid-Ops 𝔾
mon-ops = _*_ , 1#
grp-ops : Group-Ops 𝔾
grp-ops = mon-ops , 1/_
postulate
grp-struct : Group-Struct grp-ops
grp : Group 𝔾
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))
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
-- -}
-- -}
-- -}
-- -}
-- -}
|
rename 𝔾 as ℤ[ p ]★
|
CyclicGroup: rename 𝔾 as ℤ[ p ]★
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
da7ed128a0220380c3109913af5f58760ad241d3
|
Base/Denotation/Notation.agda
|
Base/Denotation/Notation.agda
|
module Base.Denotation.Notation where
-- OVERLOADING ⟦_⟧ NOTATION
--
-- This module defines a general mechanism for overloading the
-- ⟦_⟧ notation, using Agda’s instance arguments.
open import Level
record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where
constructor
meaning
field
{Semantics} : Set ℓ
⟨_⟩⟦_⟧ : Syntax → Semantics
open Meaning {{...}} public
renaming (⟨_⟩⟦_⟧ to ⟦_⟧)
open Meaning public
using (⟨_⟩⟦_⟧)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Overloading ⟦_⟧ notation
--
-- This module defines a general mechanism for overloading the
-- ⟦_⟧ notation, using Agda’s instance arguments.
------------------------------------------------------------------------
module Base.Denotation.Notation where
open import Level
record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where
constructor
meaning
field
{Semantics} : Set ℓ
⟨_⟩⟦_⟧ : Syntax → Semantics
open Meaning {{...}} public
renaming (⟨_⟩⟦_⟧ to ⟦_⟧)
open Meaning public
using (⟨_⟩⟦_⟧)
|
Improve commentary in Base.Denotation.Notation.
|
Improve commentary in Base.Denotation.Notation.
Old-commit-hash: f51d5958c73259a9120fc8a2ad20854bb12f8870
|
Agda
|
mit
|
inc-lc/ilc-agda
|
66a643a5678be6f131477803e19ed0cb52159556
|
agda/Equality.agda
|
agda/Equality.agda
|
-- This is an implementation of the equality type for Sets. Agda's
-- standard equality is more powerful. The main idea here is to
-- illustrate the equality type.
module Equality where
open import Level
-- The equality of two elements of type A. The type a ≡ b is a family
-- of types which captures the statement of equality in A. Not all of
-- the types are inhabited as in general not all elements are equal
-- here. The only type that are inhabited are a ≡ a by the element
-- that we call definition. This is called refl (for reflection in
-- agda).
data _≡_ {ℓ} {A : Set ℓ} : (a b : A) → Set ℓ where
definition : {x : A} → x ≡ x
-- ≡ is a symmetric relation.
sym : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → b ≡ a
sym definition = definition
-- ≡ is a transitive relation.
trans : ∀ {ℓ} {A : Set ℓ} {a b c : A} → a ≡ b → b ≡ c → a ≡ c
trans pAB definition = pAB
-- trans definition pBC = pBC -- alternate proof of transitivity.
-- Congruence. If we apply f to equals the result are also equal.
cong : ∀{ℓ₀ ℓ₁} {A : Set ℓ₀} {B : Set ℓ₁}
{a₀ a₁ : A} → (f : A → B) → a₀ ≡ a₁ → f a₀ ≡ f a₁
cong f definition = definition
-- Pretty way of doing equational reasoning. If we want to prove a₀ ≡
-- b through an intermediate set of equations use this. The general
-- form will look like.
--
-- begin a ≈ a₀ by p₀
-- ≈ a₁ by p₁
-- ...
-- ≈ b by p
-- ∎
begin : ∀{ℓ} {A : Set ℓ} (a : A) → a ≡ a
_≈_by_ : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → (c : A) → b ≡ c → a ≡ c
_∎ : ∀{ℓ} {A : Set ℓ} (a : A) → A
begin a = definition
aEb ≈ c by bEc = trans aEb bEc
x ∎ = x
infixl 1 _≈_by_
infixl 1 _≡_
|
-- This is an implementation of the equality type for Sets. Agda's
-- standard equality is more powerful. The main idea here is to
-- illustrate the equality type.
module Equality where
open import Level
-- The equality of two elements of type A. The type a ≡ b is a family
-- of types which captures the statement of equality in A. Not all of
-- the types are inhabited as in general not all elements are equal
-- here. The only type that are inhabited are a ≡ a by the element
-- that we call definition. This is called refl (for reflection in
-- agda).
data _≡_ {ℓ} {A : Set ℓ} : (a b : A) → Set ℓ where
refl : {x : A} → x ≡ x
-- ≡ is a symmetric relation.
sym : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → b ≡ a
sym refl = refl
-- ≡ is a transitive relation.
trans : ∀ {ℓ} {A : Set ℓ} {a b c : A} → a ≡ b → b ≡ c → a ≡ c
trans pAB refl = pAB
-- trans refl pBC = pBC -- alternate proof of transitivity.
-- Congruence. If we apply f to equals the result are also equal.
cong : ∀{ℓ₀ ℓ₁} {A : Set ℓ₀} {B : Set ℓ₁}
{a₀ a₁ : A} → (f : A → B) → a₀ ≡ a₁ → f a₀ ≡ f a₁
cong f refl = refl
-- Pretty way of doing equational reasoning. If we want to prove a₀ ≡
-- b through an intermediate set of equations use this. The general
-- form will look like.
--
-- begin a ≈ a₀ by p₀
-- ≈ a₁ by p₁
-- ...
-- ≈ b by p
-- ∎
begin : ∀{ℓ} {A : Set ℓ} (a : A) → a ≡ a
_≈_by_ : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → (c : A) → b ≡ c → a ≡ c
_∎ : ∀{ℓ} {A : Set ℓ} (a : A) → A
begin a = refl
aEb ≈ c by bEc = trans aEb bEc
x ∎ = x
infixl 1 _≈_by_
infixl 1 _≡_
|
use the standard terminology refl.
|
use the standard terminology refl.
|
Agda
|
unlicense
|
piyush-kurur/sample-code
|
5303d7901af55944be2ec4695ee17bbd01624291
|
Theorem/CongApp.agda
|
Theorem/CongApp.agda
|
module Theorem.CongApp where
-- Theorem CongApp.
-- If f ≡ g and x ≡ y, then (f x) ≡ (g y).
open import Relation.Binary.PropositionalEquality public
infixl 0 _⟨$⟩_
_⟨$⟩_ : ∀ {a b} {A : Set a} {B : Set b}
{f g : A → B} {x y : A} →
f ≡ g → x ≡ y → f x ≡ g y
_⟨$⟩_ = cong₂ (λ x y → x y)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Congruence of application.
--
-- If f ≡ g and x ≡ y, then (f x) ≡ (g y).
------------------------------------------------------------------------
module Theorem.CongApp where
open import Relation.Binary.PropositionalEquality public
infixl 0 _⟨$⟩_
_⟨$⟩_ : ∀ {a b} {A : Set a} {B : Set b}
{f g : A → B} {x y : A} →
f ≡ g → x ≡ y → f x ≡ g y
_⟨$⟩_ = cong₂ (λ x y → x y)
|
Document Theorem.CongApp regularly.
|
Document Theorem.CongApp regularly.
Old-commit-hash: f16f3e3d0b8e59230bd86a0c223af263a8db75e8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b87eb75ad51669de831d389ef1a78b415b31a18d
|
lib/Data/Nat/Distance.agda
|
lib/Data/Nat/Distance.agda
|
open import Data.Nat.NP
open import Data.Nat.Properties as Props
open import Data.Product
open import Relation.Binary.PropositionalEquality.NP
module Data.Nat.Distance where
dist : ℕ → ℕ → ℕ
dist zero y = y
dist x zero = x
dist (suc x) (suc y) = dist x y
dist-refl : ∀ x → dist x x ≡ 0
dist-refl zero = idp
dist-refl (suc x) rewrite dist-refl x = idp
dist-0≡id : ∀ x → dist 0 x ≡ x
dist-0≡id _ = idp
dist-x-x+y≡y : ∀ x y → dist x (x + y) ≡ y
dist-x-x+y≡y zero y = idp
dist-x-x+y≡y (suc x) y = dist-x-x+y≡y x y
dist-comm : ∀ x y → dist x y ≡ dist y x
dist-comm zero zero = idp
dist-comm zero (suc y) = idp
dist-comm (suc x) zero = idp
dist-comm (suc x) (suc y) = dist-comm x y
dist-x+ : ∀ x y z → dist (x + y) (x + z) ≡ dist y z
dist-x+ zero y z = idp
dist-x+ (suc x) y z = dist-x+ x y z
dist-2* : ∀ x y → dist (2* x) (2* y) ≡ 2* dist x y
dist-2* zero y = idp
dist-2* (suc x) zero = idp
dist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x | +-assoc-comm y 1 y = dist-2* x y
dist-asym-def : ∀ {x y} → x ≤ y → x + dist x y ≡ y
dist-asym-def z≤n = idp
dist-asym-def (s≤s pf) = ap suc (dist-asym-def pf)
dist-sym-wlog : ∀ (f : ℕ → ℕ) → (∀ x k → dist (f x) (f (x + k)) ≡ f k) → ∀ x y → dist (f x) (f y) ≡ f (dist x y)
dist-sym-wlog f pf x y with compare x y
dist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)
... | q rewrite +-assoc-comm x 1 k | q | ! +-assoc-comm x 1 k | dist-x-x+y≡y x (suc k) = idp
dist-sym-wlog f pf .y y | equal .y with pf y 0
... | q rewrite ℕ°.+-comm y 0 | dist-refl y = q
dist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)
... | q rewrite +-assoc-comm 1 y k | dist-comm (y + suc k) y | dist-x-x+y≡y y (suc k) | dist-comm (f (y + suc k)) (f y) = q
dist-x* : ∀ x y z → dist (x * y) (x * z) ≡ x * dist y z
dist-x* x = dist-sym-wlog (_*_ x) pf
where pf : ∀ a k → dist (x * a) (x * (a + k)) ≡ x * k
pf a k rewrite proj₁ ℕ°.distrib x a k = dist-x-x+y≡y (x * a) _
dist-2^* : ∀ x y z → dist ⟨2^ x * y ⟩ ⟨2^ x * z ⟩ ≡ ⟨2^ x * dist y z ⟩
dist-2^* x = dist-sym-wlog (2^⟨ x ⟩*) pf
where pf : ∀ a k → dist ⟨2^ x * a ⟩ ⟨2^ x * (a + k) ⟩ ≡ ⟨2^ x * k ⟩
pf a k rewrite 2^*-distrib x a k = dist-x-x+y≡y ⟨2^ x * a ⟩ ⟨2^ x * k ⟩
dist-bounded : ∀ {x y f} → x ≤ f → y ≤ f → dist x y ≤ f
dist-bounded z≤n y≤f = y≤f
dist-bounded (s≤s x≤f) z≤n = s≤s x≤f
dist-bounded (s≤s x≤f) (s≤s y≤f) = ≤-step (dist-bounded x≤f y≤f)
-- Triangular inequality
dist-sum : ∀ x y z → dist x z ≤ dist x y + dist y z
dist-sum zero zero z = ℕ≤.refl
dist-sum zero (suc y) zero = z≤n
dist-sum zero (suc y) (suc z) = s≤s (dist-sum zero y z)
dist-sum (suc x) zero zero = s≤s (ℕ≤.reflexive (ℕ°.+-comm 0 x))
dist-sum (suc x) (suc y) zero
rewrite ℕ°.+-comm (dist x y) (suc y)
| dist-comm x y = s≤s (dist-sum zero y x)
dist-sum (suc x) zero (suc z) = dist-sum x zero z
∙≤ ℕ≤.reflexive (ap₂ _+_ (dist-comm x 0) idp)
∙≤ ≤-step (ℕ≤.refl {x} +-mono ≤-step ℕ≤.refl)
dist-sum (suc x) (suc y) (suc z) = dist-sum x y z
dist≡0 : ∀ x y → dist x y ≡ 0 → x ≡ y
dist≡0 zero zero d≡0 = refl
dist≡0 zero (suc y) ()
dist≡0 (suc x) zero ()
dist≡0 (suc x) (suc y) d≡0 = ap suc (dist≡0 x y d≡0)
∸-dist : ∀ x y → y ≤ x → x ∸ y ≡ dist x y
∸-dist x .0 z≤n = ! dist-comm x 0
∸-dist ._ ._ (s≤s y≤x) = ∸-dist _ _ y≤x
{-
post--ulate
dist-≤ : ∀ x y → dist x y ≤ x
dist-mono₁ : ∀ x y z t → x ≤ y → dist z t ≤ dist (x + z) (y + t)
-}
-- Haskell
-- let dist x y = abs (x - y)
-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))
|
open import Function.NP
open import Data.Nat.NP
open import Data.Nat.Properties as Props
open import Data.Nat.Properties.Simple
open import Data.Product.NP
open import Relation.Binary.PropositionalEquality.NP
open import Relation.Binary
open import Relation.Nullary
module Data.Nat.Distance where
{-
dist : ℕ → ℕ → ℕ
dist zero y = y
dist x zero = x
dist (suc x) (suc y) = dist x y
-}
-- `symIter` from Conor McBride
-- Since symIter is essentially `dist` with callbacks, I
-- include it here.
module Generic {a}{A : Set a}(z : ℕ → A)(s : A → A) where
dist : ℕ → ℕ → A
dist zero y = z y
dist x zero = z x
dist (suc x) (suc y) = s (dist x y)
dist-comm : ∀ x y → dist x y ≡ dist y x
dist-comm zero zero = idp
dist-comm zero (suc y) = idp
dist-comm (suc x) zero = idp
dist-comm (suc x) (suc y) = ap s (dist-comm x y)
dist-0≡id : ∀ x → dist 0 x ≡ z x
dist-0≡id _ = idp
dist-0≡id′ : ∀ x → dist x 0 ≡ z x
dist-0≡id′ zero = idp
dist-0≡id′ (suc _) = idp
open Generic id id public
module Add where
open Generic id (suc ∘ suc)
public
renaming ( dist to _+'_
; dist-0≡id to +'0-identity
; dist-0≡id′ to 0+'-identity
; dist-comm to +'-comm
)
+-spec : ∀ x y → x +' y ≡ x + y
+-spec zero _ = idp
+-spec (suc x) zero = ap suc (ℕ°.+-comm 0 x)
+-spec (suc x) (suc y) = ap suc (ap suc (+-spec x y) ∙ ! +-suc x y)
module AddMult where
open Generic (λ x → x , 0)
(λ { (s , p) → (2 + s , 1 + s + p) })
public
renaming ( dist to _+*_
; dist-0≡id to +*0-identity
; dist-0≡id′ to 0+*-identity
; dist-comm to +*-comm
)
_+'_ : ℕ → ℕ → ℕ
x +' y = fst (x +* y)
_*'_ : ℕ → ℕ → ℕ
x *' y = snd (x +* y)
+*-spec : ∀ x y → (x +' y ≡ x + y) × (x *' y ≡ x * y)
+*-spec zero _ = idp , idp
+*-spec (suc x) zero = ap suc (ℕ°.+-comm 0 x) , ℕ°.*-comm 0 x
+*-spec (suc x) (suc y) = ap suc p+ , ap suc p*
where p = +*-spec x y
p+ = ap suc (fst p) ∙ ! +-suc x y
p* = ap₂ _+_ (fst p) (snd p)
∙ +-assoc x y (x * y)
∙ ap (λ z → x + (y + z)) (ℕ°.*-comm x y)
∙ ! +-assoc-comm y x (y * x)
∙ ap (_+_ y) (ℕ°.*-comm (suc y) x)
module Min where
open Generic (const zero) suc
public
renaming ( dist to _⊓'_
; dist-0≡id to ⊓'0-zero
; dist-0≡id′ to 0⊓'-zero
; dist-comm to ⊓'-comm
)
⊓'-spec : ∀ x y → x ⊓' y ≡ x ⊓ y
⊓'-spec zero zero = idp
⊓'-spec zero (suc y) = idp
⊓'-spec (suc x) zero = idp
⊓'-spec (suc x) (suc y) = ap suc (⊓'-spec x y)
module Max where
open Generic id suc
public
renaming ( dist to _⊔'_
; dist-0≡id to ⊔'0-identity
; dist-0≡id′ to 0⊔'-identity
; dist-comm to ⊔'-comm
)
⊔'-spec : ∀ x y → x ⊔' y ≡ x ⊔ y
⊔'-spec zero zero = idp
⊔'-spec zero (suc y) = idp
⊔'-spec (suc x) zero = idp
⊔'-spec (suc x) (suc y) = ap suc (⊔'-spec x y)
dist-refl : ∀ x → dist x x ≡ 0
dist-refl zero = idp
dist-refl (suc x) rewrite dist-refl x = idp
dist-x-x+y≡y : ∀ x y → dist x (x + y) ≡ y
dist-x-x+y≡y zero y = idp
dist-x-x+y≡y (suc x) y = dist-x-x+y≡y x y
dist-x+ : ∀ x y z → dist (x + y) (x + z) ≡ dist y z
dist-x+ zero y z = idp
dist-x+ (suc x) y z = dist-x+ x y z
dist-2* : ∀ x y → dist (2* x) (2* y) ≡ 2* dist x y
dist-2* zero y = idp
dist-2* (suc x) zero = idp
dist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x | +-assoc-comm y 1 y = dist-2* x y
dist-asym-def : ∀ {x y} → x ≤ y → x + dist x y ≡ y
dist-asym-def z≤n = idp
dist-asym-def (s≤s pf) = ap suc (dist-asym-def pf)
dist-sym-wlog : ∀ (f : ℕ → ℕ) → (∀ x k → dist (f x) (f (x + k)) ≡ f k) → ∀ x y → dist (f x) (f y) ≡ f (dist x y)
dist-sym-wlog f pf x y with compare x y
dist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)
... | q rewrite +-assoc-comm x 1 k | q | ! +-assoc-comm x 1 k | dist-x-x+y≡y x (suc k) = idp
dist-sym-wlog f pf .y y | equal .y with pf y 0
... | q rewrite ℕ°.+-comm y 0 | dist-refl y = q
dist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)
... | q rewrite +-assoc-comm 1 y k | dist-comm (y + suc k) y | dist-x-x+y≡y y (suc k) | dist-comm (f (y + suc k)) (f y) = q
dist-x* : ∀ x y z → dist (x * y) (x * z) ≡ x * dist y z
dist-x* x = dist-sym-wlog (_*_ x) pf
where pf : ∀ a k → dist (x * a) (x * (a + k)) ≡ x * k
pf a k rewrite fst ℕ°.distrib x a k = dist-x-x+y≡y (x * a) _
dist-2^* : ∀ x y z → dist ⟨2^ x * y ⟩ ⟨2^ x * z ⟩ ≡ ⟨2^ x * dist y z ⟩
dist-2^* x = dist-sym-wlog (2^⟨ x ⟩*) pf
where pf : ∀ a k → dist ⟨2^ x * a ⟩ ⟨2^ x * (a + k) ⟩ ≡ ⟨2^ x * k ⟩
pf a k rewrite 2^*-distrib x a k = dist-x-x+y≡y ⟨2^ x * a ⟩ ⟨2^ x * k ⟩
dist-bounded : ∀ {x y f} → x ≤ f → y ≤ f → dist x y ≤ f
dist-bounded z≤n y≤f = y≤f
dist-bounded (s≤s x≤f) z≤n = s≤s x≤f
dist-bounded (s≤s x≤f) (s≤s y≤f) = ≤-step (dist-bounded x≤f y≤f)
-- Triangular inequality
dist-sum : ∀ x y z → dist x z ≤ dist x y + dist y z
dist-sum zero zero z = ℕ≤.refl
dist-sum zero (suc y) zero = z≤n
dist-sum zero (suc y) (suc z) = s≤s (dist-sum zero y z)
dist-sum (suc x) zero zero = s≤s (ℕ≤.reflexive (ℕ°.+-comm 0 x))
dist-sum (suc x) (suc y) zero
rewrite ℕ°.+-comm (dist x y) (suc y)
| dist-comm x y = s≤s (dist-sum zero y x)
dist-sum (suc x) zero (suc z) = dist-sum x zero z
∙≤ ℕ≤.reflexive (ap₂ _+_ (dist-comm x 0) idp)
∙≤ ≤-step (ℕ≤.refl {x} +-mono ≤-step ℕ≤.refl)
dist-sum (suc x) (suc y) (suc z) = dist-sum x y z
dist≡0 : ∀ x y → dist x y ≡ 0 → x ≡ y
dist≡0 zero zero d≡0 = idp
dist≡0 zero (suc y) ()
dist≡0 (suc x) zero ()
dist≡0 (suc x) (suc y) d≡0 = ap suc (dist≡0 x y d≡0)
∸-≤-dist : ∀ x y → x ∸ y ≤ dist x y
∸-≤-dist zero zero = z≤n
∸-≤-dist zero (suc y) = z≤n
∸-≤-dist (suc x) zero = ℕ≤.refl
∸-≤-dist (suc x) (suc y) = ∸-≤-dist x y
∸-dist : ∀ x y → y ≤ x → x ∸ y ≡ dist x y
∸-dist x .0 z≤n = ! dist-comm x 0
∸-dist ._ ._ (s≤s y≤x) = ∸-dist _ _ y≤x
dist-min-max : ∀ x y → dist x y ≡ (x ⊔ y) ∸ (x ⊓ y)
dist-min-max zero zero = idp
dist-min-max zero (suc y) = idp
dist-min-max (suc x) zero = idp
dist-min-max (suc x) (suc y) = dist-min-max x y
-- -}
-- -}
-- -}
-- -}
-- -}
|
add the `symIter` from Conor and some lemmas
|
Dist: add the `symIter` from Conor and some lemmas
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
1dd4fe7a8a920498ea83e46ad2707edca7f6f4bf
|
Syntax/Type/Plotkin.agda
|
Syntax/Type/Plotkin.agda
|
module Syntax.Type.Plotkin where
-- Types for language description à la Plotkin (LCF as PL)
--
-- Given base types, we build function types.
infixr 5 _⇒_
data Type (B : Set {- of base types -}) : Set where
base : (ι : B) → Type B
_⇒_ : (σ : Type B) → (τ : Type B) → Type B
-- Lift (Δ : B → Type B) to (Δtype : Type B → Type B)
-- according to Δ (σ ⇒ τ) = σ ⇒ Δ σ ⇒ Δ τ
lift-Δtype : ∀ {B} → (B → Type B) → (Type B → Type B)
lift-Δtype f (base ι) = f ι
lift-Δtype f (σ ⇒ τ) = let Δ = lift-Δtype f in σ ⇒ Δ σ ⇒ Δ τ
-- Note: the above is *not* a monadic bind.
--
-- Proof. base` is the most straightforward `return` of the
-- functor `Type`.
--
-- return : B → Type B
-- return = base
--
-- Let
--
-- m : Type B
-- m = base κ ⇒ base ι
--
-- then
--
-- m >>= return = lift-Δtype return m
-- = base κ ⇒ base κ ⇒ base ι
--
-- violating the second monadic law, m >>= return ≡ m. ∎
open import Function
-- Variant of lift-Δtype for (Δ : B → B).
lift-Δtype₀ : ∀ {B} → (B → B) → (Type B → Type B)
lift-Δtype₀ f = lift-Δtype $ base ∘ f
-- This has a similar type to the type of `fmap`,
-- and `base` has a similar type to the type of `return`.
--
-- Similarly, for collections map can be defined from flatMap,
-- like lift-Δtype₀ can be defined in terms of lift-Δtype.
|
module Syntax.Type.Plotkin where
-- Types for language description à la Plotkin (LCF as PL)
--
-- Given base types, we build function types.
open import Function
infixr 5 _⇒_
data Type (B : Set {- of base types -}) : Set where
base : (ι : B) → Type B
_⇒_ : (σ : Type B) → (τ : Type B) → Type B
-- Lift (Δ : B → Type B) to (Δtype : Type B → Type B)
-- according to Δ (σ ⇒ τ) = σ ⇒ Δ σ ⇒ Δ τ
lift-Δtype : ∀ {B} → (B → Type B) → (Type B → Type B)
lift-Δtype f (base ι) = f ι
lift-Δtype f (σ ⇒ τ) = let Δ = lift-Δtype f in σ ⇒ Δ σ ⇒ Δ τ
-- Note: the above is *not* a monadic bind.
--
-- Proof. base` is the most straightforward `return` of the
-- functor `Type`.
--
-- return : B → Type B
-- return = base
--
-- Let
--
-- m : Type B
-- m = base κ ⇒ base ι
--
-- then
--
-- m >>= return = lift-Δtype return m
-- = base κ ⇒ base κ ⇒ base ι
--
-- violating the second monadic law, m >>= return ≡ m. ∎
-- Variant of lift-Δtype for (Δ : B → B).
lift-Δtype₀ : ∀ {B} → (B → B) → (Type B → Type B)
lift-Δtype₀ f = lift-Δtype $ base ∘ f
-- This has a similar type to the type of `fmap`,
-- and `base` has a similar type to the type of `return`.
--
-- Similarly, for collections map can be defined from flatMap,
-- like lift-Δtype₀ can be defined in terms of lift-Δtype.
|
move import to top of file in Syntax.Type.Plotkin
|
Agda: move import to top of file in Syntax.Type.Plotkin
Old-commit-hash: 0d789d2318a5215ac4ba2f68286e7db5826593ec
|
Agda
|
mit
|
inc-lc/ilc-agda
|
fa1e5e6ba14f3f7038a0a46052c8f81d12ed2e9d
|
Base/Change/Equivalence/Base.agda
|
Base/Change/Equivalence/Base.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence - base definitions
------------------------------------------------------------------------
module Base.Change.Equivalence.Base where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
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.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence - base definitions
------------------------------------------------------------------------
module Base.Change.Equivalence.Base where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
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.
-- To avoid unification problems, use a one-field record (a Haskell "newtype")
-- instead of a "type synonym".
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
|
Clarify Agda comment
|
Clarify Agda comment
Old-commit-hash: e00f9f0bb80cb86372669d674900a9abd43b7ef4
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e2d49c81f464ab9449818f964e0078660f4315da
|
Syntactic/Contexts.agda
|
Syntactic/Contexts.agda
|
module Syntactic.Contexts
(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
≼-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 Syntactic.Contexts
(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 (_⋎_)
|
add lemma about syntactic contexts
|
Agda: add lemma about syntactic contexts
Old-commit-hash: 3613bba9a32778eaaa8f9d22419c23a8505be346
|
Agda
|
mit
|
inc-lc/ilc-agda
|
0ecf1536814a880e8cdbe60b6eef1ff1106504bf
|
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ρ≈ρ′)
-- Our main theorem, as we used to state it in the paper.
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 (update-diff₍ σ ₎ u v)) ⟩
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
-- A corollary, closer to what we state in the paper.
main-theorem-coroll : ∀ {σ τ}
{f : Term ∅ (σ ⇒ τ)} {x : Term ∅ σ} {dx : Term ∅ (ΔType σ)}
→ ⟦ app f (x ⊕₍ σ ₎ dx) ⟧
≡ ⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) ((x ⊕₍ σ ₎ dx) ⊝ x) ⟧
main-theorem-coroll {σ} {τ} {f} {x} {dx} = main-theorem {σ} {τ} {f} {x} {x ⊕₍ σ ₎ dx}
-- For the statement in the paper, we'd need to talk about valid changes in
-- the lambda calculus. In fact we can, thanks to the `implements` relation;
-- but I guess the required proof must be done directly from derive-correct,
-- not from main-theorem.
|
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
|
make main theorem correspond to the one stated in the paper
|
make main theorem correspond to the one stated in the paper
Old-commit-hash: 84979b2067c54a685110f1bad744ba106a962a0f
|
Agda
|
mit
|
inc-lc/ilc-agda
|
272abae5605c8fd8c5385376ffe4f4d4c26d2a9d
|
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
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.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; 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
≡⟨ 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
-- 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₁) $ proof dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ cong (λ f → f (x ⊞ dx₂)) $ proof 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
-- Unused, but just to test that inference works.
lemma : nil f ≙ dg
lemma = ≙-sym (derivative-is-nil dg fdg)
|
------------------------------------------------------------------------
-- 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
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.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; 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
≡⟨ 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
-- 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
-- Unused, but just to test that inference works.
lemma : nil f ≙ dg
lemma = ≙-sym (derivative-is-nil dg fdg)
|
Add and use a congruence lemma
|
Add and use a congruence lemma
To reduce explicit wrapping/unwrapping of proofs of ≙.
Old-commit-hash: 2bc64b5b9f3b074a70918e11f66512232fb6796b
|
Agda
|
mit
|
inc-lc/ilc-agda
|
57dd579828aa8a7c7ff8b0546dd51520584eb18e
|
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 ρ = 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 Γ′ Γ″ ρ)
|
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 (⟦ Γ′ ⟧ ρ)))
{-
Here is an example to understand the semantics of Δ. I will use a
named variable representation for the task.
Consider the typing judgment:
x: T |- x: T
Thus, we have that:
dx : Δ T, x: T |- Δ x : Δ T
Thanks to weakening, we also have:
y : S, dx : Δ T, x: T |- Δ x : Δ T
In the formalization, we need a proof Γ′ that the context Γ₁ = dx : Δ
T, x: T is a subcontext of Γ₂ = y : S, dx : Δ T, x: T. Thus, Γ′ has
type Γ₁ ≼ Γ₂.
Now take the environment:
ρ = y ↦ w, dx ↦ dv, x ↦ v
Since the semantics of Γ′ : Γ₁ ≼ Γ₂ is a function from environments
for Γ₂ to environments for Γ₁, we have that:
⟦ Γ′ ⟧ ρ = dx ↦ dv, x ↦ v
From the definitions of update and ignore, it follows that:
update (⟦ Γ′ ⟧ ρ) = x ↦ dv ⊕ v
ignore (⟦ Γ′ ⟧ ρ) = x ↦ v
Hence, finally, we have that:
diff (⟦ t ⟧Term (update (⟦ Γ′ ⟧ ρ))) (⟦ t ⟧Term (ignore (⟦ Γ′ ⟧ ρ)))
is simply diff (dv ⊕ v) v (or (dv ⊕ v) ⊝ v). If dv is a valid change,
that's just dv, that is ⟦ dx ⟧ ρ. In other words
-}
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 Γ′ Γ″ ρ)
|
add extended comment on semantics of Δ
|
agda: add extended comment on semantics of Δ
During my walk-throughs on the code, explaining this line was a bit
hard and required a small example. After writing it here during the
walkthrough, I decided to extend it and save it.
Old-commit-hash: 279b6047512b8c934cae49bda5977fa104077e12
|
Agda
|
mit
|
inc-lc/ilc-agda
|
beadc4c6bf5a0654b270878f150131cb18ada078
|
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)
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 (_≡_)
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
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 λ()
|
add ring modules Xor°, Bool°
|
Data.Bool.NP: add ring modules Xor°, Bool°
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
09aa7263bcfccc0537afdaba6875c6d31a8fa0e1
|
lib/Algebra/Group.agda
|
lib/Algebra/Group.agda
|
{-# OPTIONS --without-K #-}
-- TODO
-- If you are looking for a proof of:
-- f (Σ(xᵢ∈A) g(x₁)) ≡ Π(xᵢ∈A) (f(g(xᵢ)))
-- Have a look to:
-- https://github.com/crypto-agda/explore/blob/master/lib/Explore/GroupHomomorphism.agda
open import Type using (Type_)
open import Data.Product.NP using (_,_)
import Algebra.FunctionProperties.Eq
open Algebra.FunctionProperties.Eq.Implicits
open import Algebra.Monoid
module Algebra.Group where
record Group-Ops {ℓ} (G : Type ℓ) : Type ℓ where
constructor _,_
field
mon-ops : Monoid-Ops G
_⁻¹ : G → G
open Monoid-Ops mon-ops public
open From-Group-Ops ε _∙_ _⁻¹ public
record Group-Struct {ℓ} {G : Type ℓ} (grp-ops : Group-Ops G) : Type ℓ where
constructor _,_
open Group-Ops grp-ops
-- laws
field
mon-struct : Monoid-Struct mon-ops
inverse : Inverse ε _⁻¹ _∙_
mon : Monoid G
mon = mon-ops , mon-struct
open Monoid-Struct mon-struct public
open From-Assoc-Identities-Inverse assoc identity inverse public
-- TODO Monoid+LeftInverse → Group
record Group {ℓ}(G : Type ℓ) : Type ℓ where
constructor _,_
field
grp-ops : Group-Ops G
grp-struct : Group-Struct grp-ops
open Group-Ops grp-ops public
open Group-Struct grp-struct public
-- A renaming of Group-Ops with additive notation
module Additive-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) where
private
module M = Group-Ops grp
using ()
renaming ( _⁻¹ to 0−_
; _/_ to _−_
; _^⁻_ to _⊗⁻_
; _^_ to _⊗_
; mon-ops to +-mon-ops
; /= to −=)
open M public using (0−_; +-mon-ops; −=)
open Additive-Monoid-Ops +-mon-ops public
infixl 6 _−_
infixl 7 _⊗⁻_ _⊗_
_−_ = M._−_
_⊗⁻_ = M._⊗⁻_
_⊗_ = M._⊗_
-- A renaming of Group-Struct with additive notation
module Additive-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ()
renaming ( mon-struct to +-mon-struct
; mon to +-mon
; assoc to +-assoc
; identity to +-identity
; ε∙-identity to 0+-identity
; ∙ε-identity to +0-identity
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; inverse to 0−-inverse
; ∙-/ to +-−; /-∙ to −-+
; unique-ε-left to unique-0-left
; unique-ε-right to unique-0-right
; x/y≡ε→x≡y to x−y≡0→x≡y
; x/y≢ε to x−y≢0
; is-ε-left to is-0-left
; is-ε-right to is-0-right
; unique-⁻¹ to unique-0−
; cancels-∙-left to cancels-+-left
; cancels-∙-right to cancels-+-right
; elim-∙-right-/ to elim-+-right-−
; elim-assoc= to elim-+-assoc=
; elim-!assoc= to elim-+-!assoc=
; elim-inner= to elim-+-inner=
; ⁻¹-hom′ to 0−-hom′
; ⁻¹-inj to 0−-inj
; ⁻¹-involutive to 0−-involutive
; ε⁻¹≡ε to 0−0≡0
)
-- A renaming of Group with additive notation
module Additive-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Additive-Group-Ops (Group.grp-ops mon) public
open Additive-Group-Struct (Group.grp-struct mon) public
-- A renaming of Group-Ops with multiplicative notation
module Multiplicative-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) = Group-Ops grp
using ( _⁻¹; _/_; /=; _^⁺_ ; _^⁻_; _^_; _²; _³; _⁴ )
renaming ( _∙_ to _*_; ε to 1#; mon-ops to *-mon-ops; ∙= to *= )
-- A renaming of Group-Struct with multiplicative notation
module Multiplicative-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ( unique-⁻¹
; ⁻¹-hom′
; ⁻¹-inj
; ⁻¹-involutive
)
renaming ( assoc to *-assoc
; identity to *-identity
; ε∙-identity to 1*-identity
; ∙ε-identity to *1-identity
; inverse to ⁻¹-inverse
; ∙-/ to *-/; /-∙ to /-*
; mon-struct to *-mon-struct
; mon to *-mon
; unique-ε-left to unique-1-left
; unique-ε-right to unique-1-right
; x/y≡ε→x≡y to x/y≡1→x≡y
; x/y≢ε to x/y≢1
; is-ε-left to is-1-left
; is-ε-right to is-1-right
; cancels-∙-left to cancels-*-left
; cancels-∙-right to cancels-*-right
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; elim-∙-right-/ to elim-*-right-/
; elim-assoc= to elim-*-assoc=
; elim-!assoc= to elim-*-!assoc=
; elim-inner= to elim-*-inner=
; ε⁻¹≡ε to 1⁻¹≡1
)
-- A renaming of Group with multiplicative notation
module Multiplicative-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Multiplicative-Group-Ops (Group.grp-ops mon) public
open Multiplicative-Group-Struct (Group.grp-struct mon) public
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
-- TODO
-- If you are looking for a proof of:
-- f (Σ(xᵢ∈A) g(x₁)) ≡ Π(xᵢ∈A) (f(g(xᵢ)))
-- Have a look to:
-- https://github.com/crypto-agda/explore/blob/master/lib/Explore/GroupHomomorphism.agda
open import Type using (Type_)
open import Data.Product.NP using (_,_;fst;snd)
import Algebra.FunctionProperties.Eq
open Algebra.FunctionProperties.Eq.Implicits
open import Algebra.Monoid
module Algebra.Group where
record Group-Ops {ℓ} (G : Type ℓ) : Type ℓ where
constructor _,_
field
mon-ops : Monoid-Ops G
_⁻¹ : G → G
open Monoid-Ops mon-ops public
open From-Group-Ops ε _∙_ _⁻¹ public
record Group-Struct {ℓ} {G : Type ℓ} (grp-ops : Group-Ops G) : Type ℓ where
constructor _,_
open Group-Ops grp-ops
-- laws
field
mon-struct : Monoid-Struct mon-ops
inverse : Inverse ε _⁻¹ _∙_
mon : Monoid G
mon = mon-ops , mon-struct
⁻¹∙-inverse : LeftInverse ε _⁻¹ _∙_
⁻¹∙-inverse = fst inverse
∙⁻¹-inverse : RightInverse ε _⁻¹ _∙_
∙⁻¹-inverse = snd inverse
open Monoid-Struct mon-struct public
open From-Assoc-Identities-Inverse assoc identity inverse public
-- TODO Monoid+LeftInverse → Group
record Group {ℓ}(G : Type ℓ) : Type ℓ where
constructor _,_
field
grp-ops : Group-Ops G
grp-struct : Group-Struct grp-ops
open Group-Ops grp-ops public
open Group-Struct grp-struct public
-- A renaming of Group-Ops with additive notation
module Additive-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) where
private
module M = Group-Ops grp
using ()
renaming ( _⁻¹ to 0−_
; _/_ to _−_
; _^⁻_ to _⊗⁻_
; _^_ to _⊗_
; mon-ops to +-mon-ops
; /= to −=)
open M public using (0−_; +-mon-ops; −=)
open Additive-Monoid-Ops +-mon-ops public
infixl 6 _−_
infixl 7 _⊗⁻_ _⊗_
_−_ = M._−_
_⊗⁻_ = M._⊗⁻_
_⊗_ = M._⊗_
-- A renaming of Group-Struct with additive notation
module Additive-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ()
renaming ( mon-struct to +-mon-struct
; mon to +-mon
; assoc to +-assoc
; identity to +-identity
; ε∙-identity to 0+-identity
; ∙ε-identity to +0-identity
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; inverse to 0−-inverse
; ∙-/ to +-−; /-∙ to −-+
; unique-ε-left to unique-0-left
; unique-ε-right to unique-0-right
; x/y≡ε→x≡y to x−y≡0→x≡y
; x/y≢ε to x−y≢0
; is-ε-left to is-0-left
; is-ε-right to is-0-right
; unique-⁻¹ to unique-0−
; cancels-∙-left to cancels-+-left
; cancels-∙-right to cancels-+-right
; elim-∙-right-/ to elim-+-right-−
; elim-assoc= to elim-+-assoc=
; elim-!assoc= to elim-+-!assoc=
; elim-inner= to elim-+-inner=
; ⁻¹-hom′ to 0−-hom′
; ⁻¹-inj to 0−-inj
; ⁻¹-involutive to 0−-involutive
; ε⁻¹≡ε to 0−0≡0
)
-- A renaming of Group with additive notation
module Additive-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Additive-Group-Ops (Group.grp-ops mon) public
open Additive-Group-Struct (Group.grp-struct mon) public
-- A renaming of Group-Ops with multiplicative notation
module Multiplicative-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) = Group-Ops grp
using ( _⁻¹; _/_; /=; _^⁺_ ; _^⁻_; _^_; _²; _³; _⁴ )
renaming ( _∙_ to _*_; ε to 1#; mon-ops to *-mon-ops; ∙= to *= )
-- A renaming of Group-Struct with multiplicative notation
module Multiplicative-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ( unique-⁻¹
; ⁻¹-hom′
; ⁻¹-inj
; ⁻¹-involutive
)
renaming ( assoc to *-assoc
; identity to *-identity
; ε∙-identity to 1*-identity
; ∙ε-identity to *1-identity
; inverse to ⁻¹-inverse
; ∙-/ to *-/; /-∙ to /-*
; mon-struct to *-mon-struct
; mon to *-mon
; unique-ε-left to unique-1-left
; unique-ε-right to unique-1-right
; x/y≡ε→x≡y to x/y≡1→x≡y
; x/y≢ε to x/y≢1
; is-ε-left to is-1-left
; is-ε-right to is-1-right
; cancels-∙-left to cancels-*-left
; cancels-∙-right to cancels-*-right
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; elim-∙-right-/ to elim-*-right-/
; elim-assoc= to elim-*-assoc=
; elim-!assoc= to elim-*-!assoc=
; elim-inner= to elim-*-inner=
; ε⁻¹≡ε to 1⁻¹≡1
)
-- A renaming of Group with multiplicative notation
module Multiplicative-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Multiplicative-Group-Ops (Group.grp-ops mon) public
open Multiplicative-Group-Struct (Group.grp-struct mon) public
-- -}
-- -}
-- -}
-- -}
|
update Group to include left and right inverse
|
update Group to include left and right inverse
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
bd4721cbfeecd02bd82f2eb2bb19570e836dac3a
|
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.Denotation.Value
open import Popl14.Change.Derive
open import Popl14.Change.Value
open import Popl14.Change.Validity
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Tuples
open import Structure.Bag.Popl14
open import Postulate.Extensionality
infix 4 implements
syntax implements τ u v = u ≈₍ τ ₎ v
implements : ∀ (τ : Type) → Change τ → ⟦ ΔType τ ⟧ → Set
u ≈₍ int ₎ v = u ≡ v
u ≈₍ bag ₎ v = u ≡ v
u ≈₍ σ ⇒ τ ₎ v =
(w : ⟦ σ ⟧) (Δw : Change σ) (R[w,Δw] : valid {σ} w Δw)
(Δw′ : ⟦ ΔType σ ⟧) (Δw≈Δw′ : implements σ Δw Δw′) →
implements τ (u (cons w Δw R[w,Δw])) (v w Δw′)
infix 4 _≈_
_≈_ : ∀ {τ} → Change τ → ⟦ ΔType τ ⟧ → Set
_≈_ {τ} = implements τ
module Disambiguation (τ : Type) where
_✚_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚_ = _⟦⊕⟧_ {τ}
_−_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−_ = _⟦⊝⟧_ {τ}
infixl 6 _✚_ _−_
_≃_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃_ = _≈_ {τ}
infix 4 _≃_
module FunctionDisambiguation (σ : Type) (τ : Type) where
open Disambiguation (σ ⇒ τ) public
_✚₁_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚₁_ = _⟦⊕⟧_ {τ}
_−₁_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−₁_ = _⟦⊝⟧_ {τ}
infixl 6 _✚₁_ _−₁_
_≃₁_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃₁_ = _≈_ {τ}
infix 4 _≃₁_
_✚₀_ : ⟦ σ ⟧ → ⟦ ΔType σ ⟧ → ⟦ σ ⟧
_✚₀_ = _⟦⊕⟧_ {σ}
_−₀_ : ⟦ σ ⟧ → ⟦ σ ⟧ → ⟦ ΔType σ ⟧
_−₀_ = _⟦⊝⟧_ {σ}
infixl 6 _✚₀_ _−₀_
_≃₀_ : Change σ → ⟦ Δ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 : Change τ} {Δ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 {base base-int} = refl
u⊟v≈u⊝v {base base-bag} = refl
u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where
open FunctionDisambiguation σ τ
result : (w : ⟦ σ ⟧) (Δw : Change σ) → valid {σ} w Δw →
(Δw′ : ⟦ ΔType σ ⟧) → Δw ≈₍ σ ₎ Δw′ →
g (w ⊞₍ σ ₎ Δw) ⊟₍ τ ₎ f w ≃₁ g (w ✚₀ Δw′) −₁ f w
result w Δw R[w,Δw] Δw′ Δw≈Δw′
rewrite carry-over {σ} {w} R[w,Δw] Δw≈Δw′ =
u⊟v≈u⊝v {τ} {g (w ✚₀ Δw′)} {f w}
carry-over {base base-int} {v} _ Δv≈Δv′ = cong (_+_ v) Δv≈Δv′
carry-over {base base-bag} {v} _ Δ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 (nil-valid-change σ v)} {Δf′ v (v −₀ v)}
(proj₁ (R[f,Δf] (nil-valid-change σ 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.Denotation.Value
open import Popl14.Change.Derive
open import Popl14.Change.Value
open import Popl14.Change.Validity
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Tuples
open import Structure.Bag.Popl14
open import Postulate.Extensionality
infix 4 implements
syntax implements τ u v = u ≈₍ τ ₎ v
implements : ∀ (τ : Type) → Change τ → ⟦ ΔType τ ⟧ → Set
u ≈₍ int ₎ v = u ≡ v
u ≈₍ bag ₎ v = u ≡ v
u ≈₍ σ ⇒ τ ₎ v =
(w : ⟦ σ ⟧) (Δw : Change σ) (R[w,Δw] : valid {σ} w Δw)
(Δw′ : ⟦ ΔType σ ⟧) (Δw≈Δw′ : implements σ Δw Δw′) →
implements τ (u (cons w Δw R[w,Δw])) (v w Δw′)
infix 4 _≈_
_≈_ : ∀ {τ} → Change τ → ⟦ ΔType τ ⟧ → Set
_≈_ {τ} = implements τ
module Disambiguation (τ : Type) where
_✚_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚_ = _⟦⊕⟧_ {τ}
_−_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−_ = _⟦⊝⟧_ {τ}
infixl 6 _✚_ _−_
_≃_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃_ = _≈_ {τ}
infix 4 _≃_
module FunctionDisambiguation (σ : Type) (τ : Type) where
open Disambiguation (σ ⇒ τ) public
_✚₁_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚₁_ = _⟦⊕⟧_ {τ}
_−₁_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−₁_ = _⟦⊝⟧_ {τ}
infixl 6 _✚₁_ _−₁_
_≃₁_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃₁_ = _≈_ {τ}
infix 4 _≃₁_
_✚₀_ : ⟦ σ ⟧ → ⟦ ΔType σ ⟧ → ⟦ σ ⟧
_✚₀_ = _⟦⊕⟧_ {σ}
_−₀_ : ⟦ σ ⟧ → ⟦ σ ⟧ → ⟦ ΔType σ ⟧
_−₀_ = _⟦⊝⟧_ {σ}
infixl 6 _✚₀_ _−₀_
_≃₀_ : Change σ → ⟦ Δ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 : Change τ} (R[v,Δv] : valid {τ} v Δv)
{Δv′ : ⟦ ΔType τ ⟧} (Δ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 {base base-int} = refl
u⊟v≈u⊝v {base base-bag} = refl
u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where
open FunctionDisambiguation σ τ
result : (w : ⟦ σ ⟧) (Δw : Change σ) → valid {σ} w Δw →
(Δw′ : ⟦ ΔType σ ⟧) → Δw ≈₍ σ ₎ Δw′ →
g (w ⊞₍ σ ₎ Δw) ⊟₍ τ ₎ f w ≃₁ g (w ✚₀ Δw′) −₁ f w
result w Δw R[w,Δw] Δw′ Δw≈Δw′
rewrite carry-over {σ} {w} R[w,Δw] Δw≈Δw′ =
u⊟v≈u⊝v {τ} {g (w ✚₀ Δw′)} {f w}
carry-over {base base-int} {v} _ Δv≈Δv′ = cong (_+_ v) Δv≈Δv′
carry-over {base base-bag} {v} _ Δv≈Δv′ = cong (_++_ v) Δv≈Δv′
carry-over {σ ⇒ τ} {f} {Δf} R[f,Δ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 (nil-valid-change σ v)} (proj₁ (R[f,Δf] (nil-valid-change σ v)))
{Δf′ 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 ρ′))
|
Change argument order of carry-over.
|
Change argument order of carry-over.
This commit prepares for uncurrying cary-over.
Old-commit-hash: e1a21bdcbe7ee2b217917bd6ac9dfc576487102a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9028d1ebd71caa01d4b8a57cbb884b32e787ebe1
|
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
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import meaning
open import IlcModel
open import Changes
open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
-- LIFTING terms into Δ-Contexts
lift-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) τ
lift-var this = that this
lift-var (that x) = that (that (lift-var x))
lift-var′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Var Γ τ → Var (Δ-Context′ Γ Γ′) τ
lift-var′ ∅ x = lift-var x
lift-var′ (τ • Γ′) this = this
lift-var′ (τ • Γ′) (that x) = that (lift-var′ Γ′ x)
lift-var-3 : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context (Δ-Context Γ)) τ
lift-var-3 this = that (that (that this))
lift-var-3 (that x) = that (that (that (that (lift-var-3 x))))
lift-term′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Term Γ τ → Term (Δ-Context′ Γ Γ′) τ
lift-term′ Γ′ (abs t) = abs (lift-term′ (_ • Γ′) t)
lift-term′ Γ′ (app t₁ t₂) = app (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂)
lift-term′ Γ′ (var x) = var (lift-var′ Γ′ x)
lift-term′ Γ′ true = true
lift-term′ Γ′ false = false
lift-term′ Γ′ (if t₁ t₂ t₃) = if (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂) (lift-term′ Γ′ t₃)
lift-term′ {.(Δ-Context Γ)} {.(Δ-Type τ)} Γ′ (Δ {Γ} {τ} t) = weakenMore t
where
open import Relation.Binary.PropositionalEquality using (sym)
doWeakenMore : ∀ Γprefix Γrest {τ} →
Term (Γprefix ⋎ Γrest) (Δ-Type τ) →
Term (Γprefix ⋎ Δ-Context Γrest) (Δ-Type τ)
doWeakenMore Γprefix ∅ t₁ = t₁
doWeakenMore ∅ (τ₂ • Γrest) t₁ =
weakenOne ∅ (Δ-Type τ₂) (doWeakenMore (τ₂ • ∅) Γrest t₁)
doWeakenMore Γprefix (τ₂ • Γrest) {τ} t₁ =
weakenOne Γprefix (Δ-Type τ₂)
(substTerm (sym (move-prefix Γprefix τ₂ (Δ-Context Γrest)))
(doWeakenMore (Γprefix ⋎ (τ₂ • ∅)) Γrest
(substTerm (move-prefix Γprefix τ₂ Γrest) t₁)))
prefix : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
prefix Γ Γ′ = take (Δ-Context Γ) Γ′
rest : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
rest Γ Γ′ = drop (Δ-Context Γ) Γ′
weakenMore2 : ∀ Γ Γ′ {τ} →
Term Γ τ →
Term (prefix Γ Γ′ ⋎ Δ-Context (rest Γ Γ′)) (Δ-Type τ)
weakenMore2 Γ Γ′ t =
doWeakenMore (prefix Γ Γ′) (rest Γ Γ′) (
substTerm (sym (take-drop (Δ-Context Γ) Γ′)) (Δ t))
weakenMore : --∀ {Γ τ} Γ′ →
Term Γ τ → Term (Δ-Context′ (Δ-Context Γ) Γ′) (Δ-Type τ)
weakenMore t =
substTerm
(sym (take-⋎-Δ-Context-drop-Δ-Context′ (Δ-Context Γ) Γ′))
(weakenMore2 Γ Γ′ t)
{-
weakenMore2 : ∀ Γ Γ′ {τ} →
Term Γ τ →
Term (prefix Γ Γ′ ⋎ Δ-Context (rest Γ Γ′)) (Δ-Type τ)
weakenMore2 ∅ ∅ t₁ = Δ t₁
weakenMore2 (τ₁ • Γ₁) ∅ t₁ =
weakenOne ∅ (Δ-Type (Δ-Type τ₁))
(weakenOne (Δ-Type τ₁ • ∅) (Δ-Type τ₁) {!
weakenMore2
(Δ {τ₁ • Γ₁} {τ} t₁)!})
--(weakenMore2 {τ₁ • Γ₁} (Δ-Type τ₁ • τ₁ • ∅) t₁ ))
-- (Δ {τ₁ • Γ₁} {τ} t₁)))
weakenMore2 (τ₁ • Γ₁) (.(Δ-Type τ₁) • Γ′₁) t₁ = {!!}
-}
lift-term′ {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) τ
lift-term = lift-term′ ∅
-- PROPERTIES of lift-term
lift-var-ignore : ∀ {Γ τ} (ρ : ⟦ Δ-Context Γ ⟧) (x : Var Γ τ) →
⟦ lift-var x ⟧ ρ ≡ ⟦ x ⟧ (ignore ρ)
lift-var-ignore (v • dv • ρ) this = ≡-refl
lift-var-ignore (v • dv • ρ) (that x) = lift-var-ignore ρ x
lift-var-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) (ρ : ⟦ Δ-Context′ Γ Γ′ ⟧) (x : Var Γ τ) →
⟦ lift-var′ Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (ignore′ Γ′ ρ)
lift-var-ignore′ ∅ ρ x = lift-var-ignore ρ x
lift-var-ignore′ (τ • Γ′) (v • ρ) this = ≡-refl
lift-var-ignore′ (τ • Γ′) (v • ρ) (that x) = lift-var-ignore′ Γ′ ρ x
lift-term-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) {ρ : ⟦ Δ-Context′ Γ Γ′ ⟧} (t : Term Γ τ) →
⟦ lift-term′ Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (ignore′ Γ′ ρ)
lift-term-ignore′ Γ′ (abs t) =
ext (λ v → lift-term-ignore′ (_ • Γ′) t)
lift-term-ignore′ Γ′ (app t₁ t₂) =
≡-app (lift-term-ignore′ Γ′ t₁) (lift-term-ignore′ Γ′ t₂)
lift-term-ignore′ Γ′ (var x) = lift-var-ignore′ Γ′ _ x
lift-term-ignore′ Γ′ true = ≡-refl
lift-term-ignore′ Γ′ false = ≡-refl
lift-term-ignore′ Γ′ {ρ} (if t₁ t₂ t₃)
with ⟦ lift-term′ Γ′ t₁ ⟧ ρ
| ⟦ t₁ ⟧ (ignore′ Γ′ ρ)
| lift-term-ignore′ Γ′ {ρ} t₁
... | true | true | bool = lift-term-ignore′ Γ′ t₂
... | false | false | bool = lift-term-ignore′ Γ′ t₃
lift-term-ignore′ Γ′ (Δ t) = {!!}
lift-term-ignore′ _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term-ignore : ∀ {Γ τ} {ρ : ⟦ Δ-Context Γ ⟧} (t : Term Γ τ) →
⟦ lift-term t ⟧ ρ ≡ ⟦ t ⟧ (ignore ρ)
lift-term-ignore = lift-term-ignore′ ∅
-- PROPERTIES of Δ
Δ-abs : ∀ {Γ τ₁ τ₂} (t : Term (τ₁ • Γ) τ₂) →
Δ (abs t) ≈ abs (abs (Δ t))
Δ-abs t = ext (λ ρ → ≡-refl)
Δ-app : ∀ {Γ τ₁ τ₂} (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) →
Δ (app t₁ t₂) ≈ app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
Δ-app t₁ t₂ = ≈-sym (ext (λ ρ →
begin
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 ρ)))
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var this = this
derive-var (that x) = that (that (derive-var x))
diff-term : ∀ {Γ τ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term = {!!}
apply-term : ∀ {Γ τ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term = {!!}
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = {!!}
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = {!!}
derive-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) (Δ-Type τ)
derive-term (abs t) = abs (abs (derive-term t))
derive-term (app t₁ t₂) = app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
derive-term (var x) = var (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)
derive-term (weakenOne Γ₁ τ₂ {Γ₃} t) =
substTerm (Δ-Context-⋎-expanded Γ₁ τ₂ Γ₃)
(weakenOne (Δ-Context Γ₁) (Δ-Type τ₂)
(weakenOne (Δ-Context Γ₁) τ₂
(substTerm (Δ-Context-⋎ Γ₁ Γ₃)
(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 : ∀ {Γ τ} → (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))
≈⟨ ≈-refl ⟩
derive-term (abs t)
∎ where open ≈-Reasoning
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₂)
≈⟨ ≈-refl ⟩
derive-term (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct (var x) = ext (λ ρ → derive-var-correct ρ x)
derive-term-correct true = ext (λ ρ → ≡-refl)
derive-term-correct false = ext (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
derive-term-correct (weakenOne _ _ 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
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import meaning
open import IlcModel
open import Changes
open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
-- LIFTING terms into Δ-Contexts
lift-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) τ
lift-var this = that this
lift-var (that x) = that (that (lift-var x))
lift-var′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Var Γ τ → Var (Δ-Context′ Γ Γ′) τ
lift-var′ ∅ x = lift-var x
lift-var′ (τ • Γ′) this = this
lift-var′ (τ • Γ′) (that x) = that (lift-var′ Γ′ x)
lift-term′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Term Γ τ → Term (Δ-Context′ Γ Γ′) τ
lift-term′ Γ′ (abs t) = abs (lift-term′ (_ • Γ′) t)
lift-term′ Γ′ (app t₁ t₂) = app (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂)
lift-term′ Γ′ (var x) = var (lift-var′ Γ′ x)
lift-term′ Γ′ true = true
lift-term′ Γ′ false = false
lift-term′ Γ′ (if t₁ t₂ t₃) = if (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂) (lift-term′ Γ′ t₃)
lift-term′ {.(Δ-Context Γ)} {.(Δ-Type τ)} Γ′ (Δ {Γ} {τ} t) = weakenMore t
where
open import Relation.Binary.PropositionalEquality using (sym)
doWeakenMore : ∀ Γprefix Γrest {τ} →
Term (Γprefix ⋎ Γrest) (Δ-Type τ) →
Term (Γprefix ⋎ Δ-Context Γrest) (Δ-Type τ)
doWeakenMore Γprefix ∅ t₁ = t₁
doWeakenMore ∅ (τ₂ • Γrest) t₁ =
weakenOne ∅ (Δ-Type τ₂) (doWeakenMore (τ₂ • ∅) Γrest t₁)
doWeakenMore Γprefix (τ₂ • Γrest) {τ} t₁ =
weakenOne Γprefix (Δ-Type τ₂)
(substTerm (sym (move-prefix Γprefix τ₂ (Δ-Context Γrest)))
(doWeakenMore (Γprefix ⋎ (τ₂ • ∅)) Γrest
(substTerm (move-prefix Γprefix τ₂ Γrest) t₁)))
prefix : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
prefix Γ Γ′ = take (Δ-Context Γ) Γ′
rest : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
rest Γ Γ′ = drop (Δ-Context Γ) Γ′
weakenMore2 : ∀ Γ Γ′ {τ} →
Term Γ τ →
Term (prefix Γ Γ′ ⋎ Δ-Context (rest Γ Γ′)) (Δ-Type τ)
weakenMore2 Γ Γ′ t =
doWeakenMore (prefix Γ Γ′) (rest Γ Γ′) (
substTerm (sym (take-drop (Δ-Context Γ) Γ′)) (Δ t))
weakenMore : --∀ {Γ τ} Γ′ →
Term Γ τ → Term (Δ-Context′ (Δ-Context Γ) Γ′) (Δ-Type τ)
weakenMore t =
substTerm
(sym (take-⋎-Δ-Context-drop-Δ-Context′ (Δ-Context Γ) Γ′))
(weakenMore2 Γ Γ′ t)
lift-term′ {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) τ
lift-term = lift-term′ ∅
-- PROPERTIES of lift-term
lift-var-ignore : ∀ {Γ τ} (ρ : ⟦ Δ-Context Γ ⟧) (x : Var Γ τ) →
⟦ lift-var x ⟧ ρ ≡ ⟦ x ⟧ (ignore ρ)
lift-var-ignore (v • dv • ρ) this = ≡-refl
lift-var-ignore (v • dv • ρ) (that x) = lift-var-ignore ρ x
lift-var-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) (ρ : ⟦ Δ-Context′ Γ Γ′ ⟧) (x : Var Γ τ) →
⟦ lift-var′ Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (ignore′ Γ′ ρ)
lift-var-ignore′ ∅ ρ x = lift-var-ignore ρ x
lift-var-ignore′ (τ • Γ′) (v • ρ) this = ≡-refl
lift-var-ignore′ (τ • Γ′) (v • ρ) (that x) = lift-var-ignore′ Γ′ ρ x
lift-term-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) {ρ : ⟦ Δ-Context′ Γ Γ′ ⟧} (t : Term Γ τ) →
⟦ lift-term′ Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (ignore′ Γ′ ρ)
lift-term-ignore′ Γ′ (abs t) =
ext (λ v → lift-term-ignore′ (_ • Γ′) t)
lift-term-ignore′ Γ′ (app t₁ t₂) =
≡-app (lift-term-ignore′ Γ′ t₁) (lift-term-ignore′ Γ′ t₂)
lift-term-ignore′ Γ′ (var x) = lift-var-ignore′ Γ′ _ x
lift-term-ignore′ Γ′ true = ≡-refl
lift-term-ignore′ Γ′ false = ≡-refl
lift-term-ignore′ Γ′ {ρ} (if t₁ t₂ t₃)
with ⟦ lift-term′ Γ′ t₁ ⟧ ρ
| ⟦ t₁ ⟧ (ignore′ Γ′ ρ)
| lift-term-ignore′ Γ′ {ρ} t₁
... | true | true | bool = lift-term-ignore′ Γ′ t₂
... | false | false | bool = lift-term-ignore′ Γ′ t₃
lift-term-ignore′ Γ′ (Δ t) = {!!}
lift-term-ignore′ _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term-ignore : ∀ {Γ τ} {ρ : ⟦ Δ-Context Γ ⟧} (t : Term Γ τ) →
⟦ lift-term t ⟧ ρ ≡ ⟦ t ⟧ (ignore ρ)
lift-term-ignore = lift-term-ignore′ ∅
-- PROPERTIES of Δ
Δ-abs : ∀ {Γ τ₁ τ₂} (t : Term (τ₁ • Γ) τ₂) →
Δ (abs t) ≈ abs (abs (Δ t))
Δ-abs t = ext (λ ρ → ≡-refl)
Δ-app : ∀ {Γ τ₁ τ₂} (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) →
Δ (app t₁ t₂) ≈ app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
Δ-app t₁ t₂ = ≈-sym (ext (λ ρ →
begin
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 ρ)))
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var this = this
derive-var (that x) = that (that (derive-var x))
diff-term : ∀ {Γ τ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term = {!!}
apply-term : ∀ {Γ τ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term = {!!}
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = {!!}
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = {!!}
derive-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) (Δ-Type τ)
derive-term (abs t) = abs (abs (derive-term t))
derive-term (app t₁ t₂) = app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
derive-term (var x) = var (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)
derive-term (weakenOne Γ₁ τ₂ {Γ₃} t) =
substTerm (Δ-Context-⋎-expanded Γ₁ τ₂ Γ₃)
(weakenOne (Δ-Context Γ₁) (Δ-Type τ₂)
(weakenOne (Δ-Context Γ₁) τ₂
(substTerm (Δ-Context-⋎ Γ₁ Γ₃)
(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 : ∀ {Γ τ} → (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))
≈⟨ ≈-refl ⟩
derive-term (abs t)
∎ where open ≈-Reasoning
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₂)
≈⟨ ≈-refl ⟩
derive-term (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct (var x) = ext (λ ρ → derive-var-correct ρ x)
derive-term-correct true = ext (λ ρ → ≡-refl)
derive-term-correct false = ext (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
derive-term-correct (weakenOne _ _ t) = {!!}
|
Revert "agda/...: misc (to revert)"
|
Revert "agda/...: misc (to revert)"
This reverts commit ae3a9db6fb87ddc01573ae236f8850b322bec8db.
Old-commit-hash: fa2ae9742c4282275b8f242a197b35ef1acf5932
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2e98871b50826699f6f16a2718f8f2361a559c4d
|
Thesis/ANormalUntyped.agda
|
Thesis/ANormalUntyped.agda
|
module Thesis.ANormalUntyped where
open import Data.Nat.Base
open import Data.Integer.Base
open import Relation.Binary.PropositionalEquality
{- Typed deBruijn indexes for untyped languages. -}
-- Using a record gives an eta rule saying that all types are equal.
record Type : Set where
constructor Uni
open import Base.Syntax.Context Type public
data Term (Γ : Context) : Set where
var : (x : Var Γ Uni) →
Term Γ
lett : (f : Var Γ Uni) → (x : Var Γ Uni) → Term (Uni • Γ) → Term Γ
{-
deriveC Δ (lett f x t) = dlett df x dx
-}
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- cvar : (x : Var Γ τ) Δ →
-- ΔCTerm Γ τ Δ
-- clett : ∀ {σ τ₁ κ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) →
-- ΔCTerm (τ₁ • Γ) τ (? • Δ) →
-- ΔCTerm Γ τ Δ
weaken-term : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ →
Term Γ₂
weaken-term Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x)
weaken-term Γ₁≼Γ₂ (lett f x t) = lett (weaken-var Γ₁≼Γ₂ f) (weaken-var Γ₁≼Γ₂ x) (weaken-term (keep _ • Γ₁≼Γ₂) t)
data Fun (Γ : Context) : Set where
term : Term Γ → Fun Γ
abs : ∀ {σ} → Fun (σ • Γ) → Fun Γ
weaken-fun : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Fun Γ₁ →
Fun Γ₂
weaken-fun Γ₁≼Γ₂ (term x) = term (weaken-term Γ₁≼Γ₂ x)
weaken-fun Γ₁≼Γ₂ (abs f) = abs (weaken-fun (keep _ • Γ₁≼Γ₂) f)
data Val : Type → Set
-- data Val (τ : Type) : Set
open import Base.Denotation.Environment Type Val public
open import Base.Data.DependentList public
-- data Val (τ : Type) where
data Val where
closure : ∀ {Γ} → (t : Fun (Uni • Γ)) → (ρ : ⟦ Γ ⟧Context) → Val Uni
intV : ∀ (n : ℤ) → Val Uni
-- ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧Context → ⟦ τ ⟧Type
-- ⟦ var x ⟧Term ρ = ⟦ x ⟧Var ρ
-- ⟦ lett f x t ⟧Term ρ = ⟦ t ⟧Term (⟦ f ⟧Var ρ (⟦ x ⟧Var ρ) • ρ)
data ErrVal : Set where
Done : (v : Val Uni) → ErrVal
Error : ErrVal
TimeOut : ErrVal
_>>=_ : ErrVal → (Val Uni → ErrVal) → ErrVal
Done v >>= f = f v
Error >>= f = Error
TimeOut >>= f = TimeOut
Res : Set
Res = ℕ → ErrVal
eval-fun : ∀ {Γ} → Fun Γ → ⟦ Γ ⟧Context → Res
eval-term : ∀ {Γ} → Term Γ → ⟦ Γ ⟧Context → Res
apply : Val Uni → Val Uni → Res
apply (closure f ρ) a n = eval-fun f (a • ρ) n
apply (intV _) a n = Error
eval-term t ρ zero = TimeOut
eval-term (var x) ρ (suc n) = Done (⟦ x ⟧Var ρ)
eval-term (lett f x t) ρ (suc n) = apply (⟦ f ⟧Var ρ) (⟦ x ⟧Var ρ) n >>= (λ v → eval-term t (v • ρ) n)
eval-fun (term t) ρ n = eval-term t ρ n
eval-fun (abs f) ρ n = Done (closure f ρ)
-- Erasure from typed to untyped values.
import Thesis.ANormalBigStep as T
erase-type : T.Type → Type
erase-type _ = Uni
erase-val : ∀ {τ} → T.Val τ → Val (erase-type τ)
erase-errVal : ∀ {τ} → T.ErrVal τ → ErrVal
erase-errVal (T.Done v) = Done (erase-val v)
erase-errVal T.Error = Error
erase-errVal T.TimeOut = TimeOut
erase-res : ∀ {τ} → T.Res τ → Res
erase-res r n = erase-errVal (r n)
erase-ctx : T.Context → Context
erase-ctx ∅ = ∅
erase-ctx (τ • Γ) = erase-type τ • (erase-ctx Γ)
erase-env : ∀ {Γ} → T.Op.⟦ Γ ⟧Context → ⟦ erase-ctx Γ ⟧Context
erase-env ∅ = ∅
erase-env (v • ρ) = erase-val v • erase-env ρ
erase-var : ∀ {Γ τ} → T.Var Γ τ → Var (erase-ctx Γ) (erase-type τ)
erase-var T.this = this
erase-var (T.that x) = that (erase-var x)
erase-term : ∀ {Γ τ} → T.Term Γ τ → Term (erase-ctx Γ)
erase-term (T.var x) = var (erase-var x)
erase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)
erase-fun : ∀ {Γ τ} → T.Fun Γ τ → Fun (erase-ctx Γ)
erase-fun (T.term x) = term (erase-term x)
erase-fun (T.abs f) = abs (erase-fun f)
erase-val (T.closure t ρ) = closure (erase-fun t) (erase-env ρ)
erase-val (T.intV n) = intV n
-- Different erasures commute.
erasure-commute-var : ∀ {Γ τ} (x : T.Var Γ τ) ρ →
erase-val (T.Op.⟦ x ⟧Var ρ) ≡ ⟦ erase-var x ⟧Var (erase-env ρ)
erasure-commute-var T.this (v • ρ) = refl
erasure-commute-var (T.that x) (v • ρ) = erasure-commute-var x ρ
erase-bind : ∀ {σ τ Γ} a (t : T.Term (σ • Γ) τ) ρ n → erase-errVal (a T.>>= (λ v → T.eval-term t (v • ρ) n)) ≡ erase-errVal a >>= (λ v → eval-term (erase-term t) (v • erase-env ρ) n)
erasure-commute-fun : ∀ {Γ τ} (t : T.Fun Γ τ) ρ n →
erase-errVal (T.eval-fun t ρ n) ≡ eval-fun (erase-fun t) (erase-env ρ) n
erasure-commute-apply : ∀ {σ τ} (f : T.Val (σ T.⇒ τ)) a n → erase-errVal (T.apply f a n) ≡ apply (erase-val f) (erase-val a) n
erasure-commute-apply {σ} (T.closure t ρ) a n = erasure-commute-fun t (a • ρ) n
erasure-commute-term : ∀ {Γ τ} (t : T.Term Γ τ) ρ n →
erase-errVal (T.eval-term t ρ n) ≡ eval-term (erase-term t) (erase-env ρ) n
erasure-commute-fun (T.term t) ρ n = erasure-commute-term t ρ n
erasure-commute-fun (T.abs t) ρ n = refl
erasure-commute-term t ρ zero = refl
erasure-commute-term (T.var x) ρ (ℕ.suc n) = cong Done (erasure-commute-var x ρ)
erasure-commute-term (T.lett f x t) ρ (ℕ.suc n) rewrite erase-bind (T.apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n) t ρ n | erasure-commute-apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n | erasure-commute-var f ρ | erasure-commute-var x ρ = refl
erase-bind (T.Done v) t ρ n = erasure-commute-term t (v • ρ) n
erase-bind T.Error t ρ n = refl
erase-bind T.TimeOut t ρ n = refl
|
module Thesis.ANormalUntyped where
open import Data.Empty
open import Data.Product
open import Data.Nat.Base
import Data.Integer.Base as I
open I using (ℤ)
open import Data.Integer.Base using (ℤ)
open import Relation.Binary.PropositionalEquality
{- Typed deBruijn indexes for untyped languages. -}
-- Using a record gives an eta rule saying that all types are equal.
record Type : Set where
constructor Uni
record DType : Set where
constructor DUni
open import Base.Syntax.Context Type public
import Base.Syntax.Context DType as DC
data Term (Γ : Context) : Set where
var : (x : Var Γ Uni) →
Term Γ
lett : (f : Var Γ Uni) → (x : Var Γ Uni) → Term (Uni • Γ) → Term Γ
ΔΔ : Context → DC.Context
ΔΔ ∅ = ∅
ΔΔ (τ • Γ) = DUni • ΔΔ Γ
derive-dvar : ∀ {Δ} → (x : Var Δ Uni) → DC.Var (ΔΔ Δ) DUni
derive-dvar this = DC.this
derive-dvar (that x) = DC.that (derive-dvar x)
data DTerm : (Δ : Context) → Set where
dvar : ∀ {Δ} (x : DC.Var (ΔΔ Δ) DUni) →
DTerm Δ
dlett : ∀ {Δ} →
(f : Var Δ Uni) →
(x : Var Δ Uni) →
(t : Term (Uni • Δ)) →
(df : DC.Var (ΔΔ Δ) DUni) →
(dx : DC.Var (ΔΔ Δ) DUni) →
(dt : DTerm (Uni • Δ)) →
DTerm Δ
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)
{-
deriveC Δ (lett f x t) = dlett df x dx
-}
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- cvar : (x : Var Γ τ) Δ →
-- ΔCTerm Γ τ Δ
-- clett : ∀ {σ τ₁ κ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) →
-- ΔCTerm (τ₁ • Γ) τ (? • Δ) →
-- ΔCTerm Γ τ Δ
weaken-term : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ →
Term Γ₂
weaken-term Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x)
weaken-term Γ₁≼Γ₂ (lett f x t) = lett (weaken-var Γ₁≼Γ₂ f) (weaken-var Γ₁≼Γ₂ x) (weaken-term (keep _ • Γ₁≼Γ₂) t)
-- I don't necessarily recommend having a separate syntactic category for
-- functions, but we should prove a fundamental lemma for them too, somehow.
-- I'll probably end up with some ANF allowing lambdas to do the semantics.
data Fun (Γ : Context) : Set where
term : Term Γ → Fun Γ
abs : ∀ {σ} → Fun (σ • Γ) → Fun Γ
data DFun (Δ : Context) : Set where
dterm : DTerm Δ → DFun Δ
dabs : DFun (Uni • Δ) → DFun Δ
derive-dfun : ∀ {Δ} → (t : Fun Δ) → DFun Δ
derive-dfun (term t) = dterm (derive-dterm t)
derive-dfun (abs f) = dabs (derive-dfun f)
weaken-fun : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Fun Γ₁ →
Fun Γ₂
weaken-fun Γ₁≼Γ₂ (term x) = term (weaken-term Γ₁≼Γ₂ x)
weaken-fun Γ₁≼Γ₂ (abs f) = abs (weaken-fun (keep _ • Γ₁≼Γ₂) f)
data Val : Type → Set
data DVal : DType → Set
-- data Val (τ : Type) : Set
open import Base.Denotation.Environment Type Val public
open import Base.Data.DependentList public
import Base.Denotation.Environment DType DVal as D
-- data Val (τ : Type) where
data Val where
closure : ∀ {Γ} → (t : Fun (Uni • Γ)) → (ρ : ⟦ Γ ⟧Context) → Val Uni
intV : ∀ (n : ℕ) → Val Uni
data DVal where
dclosure : ∀ {Γ} → (dt : DFun (Uni • Γ)) → (ρ : ⟦ Γ ⟧Context) → (dρ : D.⟦ ΔΔ Γ ⟧Context) → DVal DUni
dintV : ∀ (n : ℤ) → DVal DUni
ChΔ : ∀ (Δ : Context) → Set
ChΔ Δ = D.⟦ ΔΔ Δ ⟧Context
-- ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧Context → ⟦ τ ⟧Type
-- ⟦ var x ⟧Term ρ = ⟦ x ⟧Var ρ
-- ⟦ lett f x t ⟧Term ρ = ⟦ t ⟧Term (⟦ f ⟧Var ρ (⟦ x ⟧Var ρ) • ρ)
-- XXX separate syntax is a bit dangerous. Also, do I want to be so accurate relative to the original model?
data _F⊢_↓[_]_ {Γ} (ρ : ⟦ Γ ⟧Context) : Fun Γ → ℕ → Val Uni → Set where
abs : ∀ {t : Fun (Uni • Γ)} →
ρ F⊢ abs t ↓[ 0 ] closure t ρ
data _⊢_↓[_]_ {Γ} (ρ : ⟦ Γ ⟧Context) : Term Γ → ℕ → Val Uni → Set where
var : ∀ (x : Var Γ Uni) →
ρ ⊢ var x ↓[ 0 ] (⟦ x ⟧Var ρ)
lett : ∀ n1 n2 {Γ' ρ′ v1 v2 v3} {f x t t'} →
ρ ⊢ var f ↓[ 0 ] closure {Γ'} t' ρ′ →
ρ ⊢ var x ↓[ 0 ] v1 →
(v1 • ρ′) F⊢ t' ↓[ n1 ] v2 →
(v2 • ρ) ⊢ t ↓[ n2 ] v3 →
ρ ⊢ lett f x t ↓[ suc (suc (n1 + n2)) ] v3
-- lit : ∀ n →
-- ρ ⊢ const (lit n) ↓[ 0 ] intV n
-- data _D_⊢_↓[_]_ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : D.⟦ ΔΔ Γ ⟧Context) : DTerm Γ → ℕ → DVal DUni → Set where
-- dvar : ∀ (x : DC.Var (ΔΔ Γ) DUni) →
-- ρ D dρ ⊢ dvar x ↓[ 0 ] (D.⟦ x ⟧Var dρ)
-- dlett : ∀ n1 n2 n3 n4 {Γ' ρ′ ρ'' dρ' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} →
-- ρ ⊢ var f ↓[ 0 ] closure {Γ'} t' ρ′ →
-- ρ ⊢ var x ↓[ 0 ] v1 →
-- (v1 • ρ′) F⊢ t' ↓[ n1 ] v2 →
-- (v2 • ρ) ⊢ t ↓[ n2 ] v3 →
-- -- With a valid input ρ' and ρ'' coincide? Varies among plausible validity
-- -- definitions.
-- ρ D dρ ⊢ dvar df ↓[ 0 ] dclosure {Γ'} dt' ρ'' dρ' →
-- ρ D dρ ⊢ dvar dx ↓[ 0 ] dv1 →
-- (v1 • ρ'') D (dv1 • dρ') F⊢ dt' ↓[ n3 ] dv2 →
-- (v2 • ρ) D (dv2 • dρ) ⊢ dt ↓[ n4 ] dv3 →
-- ρ D dρ ⊢ dlett f x t df dx dt ↓[ suc (suc (n1 + n2)) ] dv3
-- Do I need to damn count steps here? No.
data _D_F⊢_↓_ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) : DFun Γ → DVal DUni → Set where
dabs : ∀ {t : DFun (Uni • Γ)} →
ρ D dρ F⊢ dabs t ↓ dclosure t ρ dρ
data _D_⊢_↓_ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) : DTerm Γ → DVal DUni → Set where
dvar : ∀ (x : DC.Var (ΔΔ Γ) DUni) →
ρ D dρ ⊢ dvar x ↓ (D.⟦ x ⟧Var dρ)
dlett : ∀ n1 n2 {Γ' ρ′ ρ'' dρ' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} →
ρ ⊢ var f ↓[ 0 ] closure {Γ'} t' ρ′ →
ρ ⊢ var x ↓[ 0 ] v1 →
(v1 • ρ′) F⊢ t' ↓[ n1 ] v2 →
(v2 • ρ) ⊢ t ↓[ n2 ] v3 →
-- With a valid input ρ' and ρ'' coincide? Varies among plausible validity
-- definitions.
ρ D dρ ⊢ dvar df ↓ dclosure {Γ'} dt' ρ'' dρ' →
ρ D dρ ⊢ dvar dx ↓ dv1 →
(v1 • ρ'') D (dv1 • dρ') F⊢ dt' ↓ dv2 →
(v2 • ρ) D (dv2 • dρ) ⊢ dt ↓ dv3 →
ρ D dρ ⊢ dlett f x t df dx dt ↓ dv3
{-# TERMINATING #-} -- Dear lord. Why on Earth.
mutual
-- Single context? Yeah, good, that's what we want in the end...
-- Though we might want more flexibility till when we have replacement
-- changes.
rrelT3 : ∀ {Γ} (t1 : Fun Γ) (dt : DFun Γ) (t2 : Fun Γ) (ρ1 : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) (ρ2 : ⟦ Γ ⟧Context) → ℕ → Set
rrelT3 t1 dt t2 ρ1 dρ ρ2 k =
(v1 v2 : Val Uni) →
∀ j n2 (j<k : j < k) →
(ρ1⊢t1↓[j]v1 : ρ1 F⊢ t1 ↓[ j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 F⊢ t2 ↓[ n2 ] v2) →
Σ[ dv ∈ DVal DUni ] Σ[ dn ∈ ℕ ]
ρ1 D dρ F⊢ dt ↓ dv ×
rrelV3 v1 dv v2 (k ∸ j)
rrelV3 : ∀ (v1 : Val Uni) (dv : DVal DUni) (v2 : Val Uni) → ℕ → Set
rrelV3 (intV v1) (dintV dv) (intV v2) n = dv I.+ (I.+ v1) ≡ (I.+ v2)
rrelV3 (closure {Γ1} t1 ρ1) (dclosure {ΔΓ} dt ρ' dρ) (closure {Γ2} t2 ρ2) n =
-- Require a proof that the two contexts match:
Σ ((Γ1 ≡ Γ2) × (Γ1 ≡ ΔΓ)) λ { (refl , refl) →
∀ (k : ℕ) (k<n : k < n) v1 dv v2 →
rrelV3 v1 dv v2 k →
rrelT3
t1
dt
t2
(v1 • ρ1)
(dv • dρ)
(v2 • ρ2)
k
}
rrelV3 _ _ _ n = ⊥
-- -- [_]τ from v1 to v2) :
-- -- [ τ ]τ dv from v1 to v2) =
-- -- 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)
-- data ErrVal : Set where
-- Done : (v : Val Uni) → ErrVal
-- Error : ErrVal
-- TimeOut : ErrVal
-- _>>=_ : ErrVal → (Val Uni → ErrVal) → ErrVal
-- Done v >>= f = f v
-- Error >>= f = Error
-- TimeOut >>= f = TimeOut
-- Res : Set
-- Res = ℕ → ErrVal
-- -- eval-fun : ∀ {Γ} → Fun Γ → ⟦ Γ ⟧Context → Res
-- -- eval-term : ∀ {Γ} → Term Γ → ⟦ Γ ⟧Context → Res
-- -- apply : Val Uni → Val Uni → Res
-- -- apply (closure f ρ) a n = eval-fun f (a • ρ) n
-- -- apply (intV _) a n = Error
-- -- eval-term t ρ zero = TimeOut
-- -- eval-term (var x) ρ (suc n) = Done (⟦ x ⟧Var ρ)
-- -- eval-term (lett f x t) ρ (suc n) = apply (⟦ f ⟧Var ρ) (⟦ x ⟧Var ρ) n >>= (λ v → eval-term t (v • ρ) n)
-- -- eval-fun (term t) ρ n = eval-term t ρ n
-- -- eval-fun (abs f) ρ n = Done (closure f ρ)
-- -- -- Erasure from typed to untyped values.
-- -- import Thesis.ANormalBigStep as T
-- -- erase-type : T.Type → Type
-- -- erase-type _ = Uni
-- -- erase-val : ∀ {τ} → T.Val τ → Val (erase-type τ)
-- -- erase-errVal : ∀ {τ} → T.ErrVal τ → ErrVal
-- -- erase-errVal (T.Done v) = Done (erase-val v)
-- -- erase-errVal T.Error = Error
-- -- erase-errVal T.TimeOut = TimeOut
-- -- erase-res : ∀ {τ} → T.Res τ → Res
-- -- erase-res r n = erase-errVal (r n)
-- -- erase-ctx : T.Context → Context
-- -- erase-ctx ∅ = ∅
-- -- erase-ctx (τ • Γ) = erase-type τ • (erase-ctx Γ)
-- -- erase-env : ∀ {Γ} → T.Op.⟦ Γ ⟧Context → ⟦ erase-ctx Γ ⟧Context
-- -- erase-env ∅ = ∅
-- -- erase-env (v • ρ) = erase-val v • erase-env ρ
-- -- erase-var : ∀ {Γ τ} → T.Var Γ τ → Var (erase-ctx Γ) (erase-type τ)
-- -- erase-var T.this = this
-- -- erase-var (T.that x) = that (erase-var x)
-- -- erase-term : ∀ {Γ τ} → T.Term Γ τ → Term (erase-ctx Γ)
-- -- erase-term (T.var x) = var (erase-var x)
-- -- erase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)
-- -- erase-fun : ∀ {Γ τ} → T.Fun Γ τ → Fun (erase-ctx Γ)
-- -- erase-fun (T.term x) = term (erase-term x)
-- -- erase-fun (T.abs f) = abs (erase-fun f)
-- -- erase-val (T.closure t ρ) = closure (erase-fun t) (erase-env ρ)
-- -- erase-val (T.intV n) = intV n
-- -- -- Different erasures commute.
-- -- erasure-commute-var : ∀ {Γ τ} (x : T.Var Γ τ) ρ →
-- -- erase-val (T.Op.⟦ x ⟧Var ρ) ≡ ⟦ erase-var x ⟧Var (erase-env ρ)
-- -- erasure-commute-var T.this (v • ρ) = refl
-- -- erasure-commute-var (T.that x) (v • ρ) = erasure-commute-var x ρ
-- -- erase-bind : ∀ {σ τ Γ} a (t : T.Term (σ • Γ) τ) ρ n → erase-errVal (a T.>>= (λ v → T.eval-term t (v • ρ) n)) ≡ erase-errVal a >>= (λ v → eval-term (erase-term t) (v • erase-env ρ) n)
-- -- erasure-commute-fun : ∀ {Γ τ} (t : T.Fun Γ τ) ρ n →
-- -- erase-errVal (T.eval-fun t ρ n) ≡ eval-fun (erase-fun t) (erase-env ρ) n
-- -- erasure-commute-apply : ∀ {σ τ} (f : T.Val (σ T.⇒ τ)) a n → erase-errVal (T.apply f a n) ≡ apply (erase-val f) (erase-val a) n
-- -- erasure-commute-apply {σ} (T.closure t ρ) a n = erasure-commute-fun t (a • ρ) n
-- -- erasure-commute-term : ∀ {Γ τ} (t : T.Term Γ τ) ρ n →
-- -- erase-errVal (T.eval-term t ρ n) ≡ eval-term (erase-term t) (erase-env ρ) n
-- -- erasure-commute-fun (T.term t) ρ n = erasure-commute-term t ρ n
-- -- erasure-commute-fun (T.abs t) ρ n = refl
-- -- erasure-commute-term t ρ zero = refl
-- -- erasure-commute-term (T.var x) ρ (ℕ.suc n) = cong Done (erasure-commute-var x ρ)
-- -- erasure-commute-term (T.lett f x t) ρ (ℕ.suc n) rewrite erase-bind (T.apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n) t ρ n | erasure-commute-apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n | erasure-commute-var f ρ | erasure-commute-var x ρ = refl
-- -- erase-bind (T.Done v) t ρ n = erasure-commute-term t (v • ρ) n
-- -- erase-bind T.Error t ρ n = refl
-- -- erase-bind T.TimeOut t ρ n = refl
|
Write down real step-indexed logical relation for untyped terms
|
Write down real step-indexed logical relation for untyped terms
Sadly, Agda does not recognize this is terminating, because recursing on k with
k < n is not a structurally recursive call on a subterm. SAD!
|
Agda
|
mit
|
inc-lc/ilc-agda
|
7849a93dd727b32c2606d958173b08212260367d
|
Game/Transformation/ReceiptFreeness-CCA2d/Valid.agda
|
Game/Transformation/ReceiptFreeness-CCA2d/Valid.agda
|
{-# OPTIONS --copatterns #-}
open import Type
open import Function
open import Data.One
open import Data.Two
open import Data.Maybe
open import Data.Product
open import Data.Nat
open import Data.Vec hiding (_>>=_ ; _∈_)
open import Data.List as L
open import Data.Fin as Fin using (Fin)
open import Relation.Binary.PropositionalEquality.NP as ≡
open import Control.Strategy renaming (map to mapS)
open import Game.Challenge
import Game.ReceiptFreeness
import Game.IND-CCA2-dagger
import Game.IND-CPA-utils
import Data.List.Any
open Data.List.Any using (here; there)
open Data.List.Any.Membership-≡ using (_∈_ ; _∉_)
module Game.Transformation.ReceiptFreeness-CCA2d.Valid
(PubKey : ★)
(SecKey : ★)
-- Message = 𝟚
(CipherText : ★)
(SerialNumber : ★)
-- randomness supply for, encryption, key-generation, adversary, adversary state
(Rₑ Rₖ Rₐ : ★)
(#q : ℕ) (max#q : Fin #q)
(KeyGen : Rₖ → PubKey × SecKey)
(Enc : let Message = 𝟚 in
PubKey → Message → Rₑ → CipherText)
(Dec : let Message = 𝟚 in
SecKey → CipherText → Message)
(Check : let open Game.ReceiptFreeness PubKey SecKey CipherText SerialNumber Rₑ Rₖ Rₐ #q max#q KeyGen Enc Dec
in BB → Receipt → 𝟚)
(CheckMem : ∀ bb r → ✓ (Check bb r) → proj₁ (proj₂ r) ∉ L.map (proj₁ ∘ proj₂) bb)
-- (CheckEnc : ∀ pk m rₑ → Check (Enc pk m rₑ) ≡ 1₂)
where
_²' : ★ → ★
X ²' = X × X
r-sn : Receipt → SerialNumber
r-sn (_ , sn , _) = sn
module Simulator-Valid (RFA : RF.Adversary)(RFA-Valid : RF.Valid-Adversary RFA)
(WRONG-HYP : ∀ r r' → r-sn r ≡ r-sn r' → enc-co r ≡ enc-co r')
where
valid : CCA2†.Valid-Adversary (Simulator.A† RFA)
valid (rₐ , rgb) pk = Phase1 _ (RFA-Valid rₐ pk) where
open CCA2†.Valid-Adversary (rₐ , rgb) pk
module RFA = RF.Valid-Adversary rₐ pk
open Simulator RFA
open AdversaryParts rgb pk rₐ
-- could refine r more
-- {-
Phase2 : ∀ RF {bb i taA taB r} → r-sn (r 0₂) ∈ L.map r-sn bb → r-sn (r 1₂) ∈ L.map r-sn bb → RFA.Phase2-Valid r RF
→ Phase2-Valid (proj₂ ∘ proj₂ ∘ r) (mapS proj₁ (mitm-to-client-trans (MITM-phase 1₂ i bb (taA , taB)) RF))
Phase2 (ask REB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RBB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RTally cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask (RCO x) cont) r0 r1 ((r₀ , r₁) , RF-valid) = r₀ , r₁ , (λ r → Phase2 (cont _) r0 r1 (RF-valid _))
Phase2 (ask (Vote x) cont) {bb} r0 r1 RF-valid with Check bb x | CheckMem bb x
... | 0₂ | _ = Phase2 (cont _) r0 r1 (RF-valid _)
... | 1₂ | not-in-bb = (λ eq → not-in-bb _ {!subst (λ x → x ∈ L.map r-sn bb) eq!})
, {!!},
, λ r → Phase2 (cont _) (there r0) (there r1) (RF-valid _)
Phase2 (done x) r0 r1 RF-valid = RF-valid
Phase1 : ∀ RF {sn i bb taA taB} → RFA.Phase1-Valid sn RF
→ Phase1-Valid (mapS A†2 (mitm-to-client-trans (MITM-phase 0₂ i bb (taA , taB)) RF))
Phase1 (ask REB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RBB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RTally cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask (RCO x) cont) RF-valid r = Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) {bb = bb} RF-valid with Check bb x
Phase1 (ask (Vote x) cont) RF-valid | 1₂ = λ r → Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) RF-valid | 0₂ = Phase1 _ (RF-valid _)
Phase1 (done x) (sn₀∉sn , sn₁∉sn , RF-valid) cs = Phase2 (put-resp x (proj₂ (put-resp (hack-challenge x) cs) ))
(here refl) (there (here refl)) (RF-valid _)
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --copatterns #-}
open import Type
open import Function
open import Data.One
open import Data.Two
open import Data.Maybe
open import Data.Product
open import Data.Nat
open import Data.Vec hiding (_>>=_ ; _∈_)
open import Data.List as L
open import Data.Fin as Fin using (Fin)
open import Relation.Binary.PropositionalEquality.NP as ≡
open import Control.Strategy renaming (map to mapS)
open import Control.Strategy.Utils
open import Game.Challenge
import Game.ReceiptFreeness
import Game.IND-CCA2-dagger
import Game.IND-CCA2-dagger.Valid
import Game.IND-CPA-utils
import Data.List.Any
open Data.List.Any using (here; there)
open Data.List.Any.Membership-≡ using (_∈_ ; _∉_)
import Game.ReceiptFreeness.Adversary
import Game.ReceiptFreeness.Valid
import Game.Transformation.ReceiptFreeness-CCA2d.Simulator
module Game.Transformation.ReceiptFreeness-CCA2d.Valid
(PubKey : ★)
(CipherText : ★)
(SerialNumber : ★)
(Receipt : ★)
(MarkedReceipt? : ★)
(Ballot : ★)
(Tally : ★)
-- (BB : ★)
-- ([] : BB)
-- (_∷_ : Receipt → BB → BB)
(Rgb : ★)
(genBallot : PubKey → Rgb → Ballot)
(tallyMarkedReceipt? : let CO = 𝟚 in CO → MarkedReceipt? → Tally)
(0,0 : Tally)
(1,1 : Tally)
(_+,+_ : Tally → Tally → Tally)
(receipts : SerialNumber ² → CipherText ² → Receipt ²)
(enc-co : Receipt → CipherText)
(r-sn : Receipt → SerialNumber)
(m? : Receipt → MarkedReceipt?)
(b-sn : Ballot → SerialNumber)
-- randomness supply for, encryption, key-generation, adversary, adversary state
(Rₐ : ★)
(#q : ℕ) (max#q : Fin #q)
(Check : let BB = List Receipt in BB → Receipt → 𝟚)
where
_²' : ★ → ★
X ²' = X × X
CO = 𝟚
BB = List Receipt
all-sn : BB → List SerialNumber
all-sn = L.map r-sn
module RF = Game.ReceiptFreeness.Adversary PubKey (SerialNumber ²) Rₐ Receipt Ballot Tally CO BB
module RFV = Game.ReceiptFreeness.Valid PubKey SerialNumber Rₐ Receipt Ballot Tally CO BB
CipherText enc-co r-sn b-sn
open Game.Transformation.ReceiptFreeness-CCA2d.Simulator
PubKey CipherText (SerialNumber ²) Receipt MarkedReceipt? Ballot Tally BB [] _∷_ Rgb genBallot
tallyMarkedReceipt? 0,0 1,1 _+,+_ receipts enc-co m? Rₐ #q max#q Check
module CCA2†V = Game.IND-CCA2-dagger.Valid PubKey Message CipherText Rₐ†
module Simulator-Valid (RFA : RF.Adversary)(RFA-Valid : RFV.Valid-Adversary RFA)
(WRONG-HYP : ∀ r r' → enc-co r ≡ enc-co r' → r-sn r ≡ r-sn r')
(CheckMem : ∀ bb r → ✓ (Check bb r) → r-sn r ∉ all-sn bb)
where
valid : CCA2†V.Valid-Adversary (Simulator.A† RFA)
valid (rₐ , rgb) pk = Phase1 _ (RFA-Valid rₐ pk) where
open CCA2†V.Valid-Adversary (rₐ , rgb) pk
module RFVA = RFV.Valid-Adversary rₐ pk
open RF
open Simulator RFA
open AdversaryParts rgb pk rₐ
-- could refine r more
-- {-
Phase2 : ∀ RF {bb i ta r} → r-sn (r 0₂) ∈ all-sn bb → r-sn (r 1₂) ∈ all-sn bb → RFVA.Phase2-Valid r RF
→ Phase2-Valid (enc-co ∘ r) (mapS proj₁ (mitm-to-client-trans (MITM-phase 1₂ i bb ta) RF))
Phase2 (ask REB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RBB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RTally cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask (RCO x) cont) r0 r1 ((r₀ , r₁) , RF-valid) = r₀ , r₁ , (λ r → Phase2 (cont _) r0 r1 (RF-valid _))
Phase2 (ask (Vote x) cont) {bb} r0 r1 RF-valid with Check bb x | CheckMem bb x
... | 0₂ | _ = Phase2 (cont _) r0 r1 (RF-valid _)
... | 1₂ | not-in-bb = (λ eq → not-in-bb _ (subst (λ x₁ → x₁ ∈ all-sn bb) (WRONG-HYP _ _ eq) r0))
, (λ eq → not-in-bb _ (subst (λ x₁ → x₁ ∈ all-sn bb) (WRONG-HYP _ _ eq) r1))
, λ r → Phase2 (cont _) (there r0) (there r1) (RF-valid _)
--Phase2 (cont _) (there r0) (there r1) (RF-valid _)
Phase2 (done x) r0 r1 RF-valid = RF-valid
Phase1 : ∀ RF {sn i bb ta} → RFVA.Phase1-Valid sn RF
→ Phase1-Valid (mapS A†2 (mitm-to-client-trans (MITM-phase 0₂ i bb ta) RF))
Phase1 (ask REB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RBB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RTally cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask (RCO x) cont) RF-valid r = Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) {bb = bb} RF-valid with Check bb x
Phase1 (ask (Vote x) cont) RF-valid | 1₂ = λ r → Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) RF-valid | 0₂ = Phase1 _ (RF-valid _)
Phase1 (done x) {bb = bb'}{ta} (sn₀∉sn , sn₁∉sn , RF-valid) cs
= {!Phase2 (put-resp x (proj₂ (put-resp (hack-challenge x) cs))) (here refl) (there (here refl)) (RF-valid _)!}
-- Phase2 (put-resp x (proj₂ (put-resp (hack-challenge x) cs) ))
-- ? ? (RF-valid _)
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
|
update the validity for the simulator
|
update the validity for the simulator
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
41898751fdcc62758217e161ad60e93a20db6f51
|
lib/Algebra/Monoid.agda
|
lib/Algebra/Monoid.agda
|
open import Function.NP
open import Data.Product.NP
open import Data.Nat
using (ℕ; fold; zero)
renaming (_+_ to _+ℕ_; _*_ to _*ℕ_; suc to 1+_)
open import Data.Integer
using (ℤ; +_; -[1+_]; _⊖_)
renaming (_+_ to _+ℤ_; _*_ to _*ℤ_)
open import Relation.Binary.PropositionalEquality.NP renaming (_∙_ to _♦_)
open import Algebra.FunctionProperties.Eq
open ≡-Reasoning
module Algebra.Monoid where
record Monoid-Ops {ℓ} (M : Set ℓ) : Set ℓ where
constructor mk
infixl 7 _∙_
field
_∙_ : M → M → M
ε : M
_^⁺_ : M → ℕ → M
x ^⁺ n = fold ε (_∙_ x) n
module FromInverseOp
(_⁻¹ : Op₁ M)
where
infixl 7 _/_
_/_ : M → M → M
x / y = x ∙ y ⁻¹
open FromOp₂ _/_ public renaming (op= to /=)
_^⁻_ _^⁻′_ : M → ℕ → M
x ^⁻ n = (x ^⁺ n)⁻¹
x ^⁻′ n = (x ⁻¹)^⁺ n
_^_ : M → ℤ → M
x ^ -[1+ n ] = x ^⁻(1+ n)
x ^ (+ n) = x ^⁺ n
record Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
-- laws
field
assoc : Associative _∙_
identity : Identity ε _∙_
open FromOp₂ _∙_ public renaming (op= to ∙=)
open FromAssoc _∙_ assoc public
module _ {b} where
^⁺0-ε : b ^⁺ 0 ≡ ε
^⁺0-ε = idp
^⁺1-id : b ^⁺ 1 ≡ b
^⁺1-id = snd identity
^⁺2-∙ : b ^⁺ 2 ≡ b ∙ b
^⁺2-∙ = ap (_∙_ _) ^⁺1-id
^⁺-+ : ∀ m {n} → b ^⁺ (m +ℕ n) ≡ b ^⁺ m ∙ b ^⁺ n
^⁺-+ 0 = ! fst identity
^⁺-+ (1+ m) = ap (_∙_ b) (^⁺-+ m) ♦ ! assoc
^⁺-* : ∀ m {n} → b ^⁺(m *ℕ n) ≡ (b ^⁺ n)^⁺ m
^⁺-* 0 = idp
^⁺-* (1+ m) {n}
= b ^⁺ (n +ℕ m *ℕ n) ≡⟨ ^⁺-+ n ⟩
b ^⁺ n ∙ b ^⁺(m *ℕ n) ≡⟨ ap (_∙_ (b ^⁺ n)) (^⁺-* m) ⟩
b ^⁺ n ∙ (b ^⁺ n)^⁺ m ∎
comm-ε : ∀ {x} → x ∙ ε ≡ ε ∙ x
comm-ε = snd identity ♦ ! fst identity
module _ {c x y} (e : (x ∙ y) ≡ ε) where
elim-assoc= : (c ∙ x) ∙ y ≡ c
elim-assoc= = assoc ♦ ∙= idp e ♦ snd identity
elim-!assoc= : x ∙ (y ∙ c) ≡ c
elim-!assoc= = ! assoc ♦ ∙= e idp ♦ fst identity
module _ {c d x y} (e : (x ∙ y) ≡ ε) where
elim-inner= : (c ∙ x) ∙ (y ∙ d) ≡ c ∙ d
elim-inner= = assoc ♦ ap (_∙_ c) (elim-!assoc= e)
module FromLeftInverse
(_⁻¹ : Op₁ M)
(inv-l : LeftInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-left : LeftCancel _∙_
cancels-∙-left {c} {x} {y} e
= x ≡⟨ ! fst identity ⟩
ε ∙ x ≡⟨ ∙= (! inv-l) idp ⟩
c ⁻¹ ∙ c ∙ x ≡⟨ !assoc= e ⟩
c ⁻¹ ∙ c ∙ y ≡⟨ ∙= inv-l idp ⟩
ε ∙ y ≡⟨ fst identity ⟩
y ∎
inv-r : RightInverse ε _⁻¹ _∙_
inv-r = cancels-∙-left (! assoc ♦ ∙= inv-l idp ♦ ! comm-ε)
/-∙ : ∀ {x y} → x ≡ (x / y) ∙ y
/-∙ {x} {y}
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-l) ⟩
x ∙ (y ⁻¹ ∙ y) ≡⟨ ! assoc ⟩
(x / y) ∙ y ∎
module FromRightInverse
(_⁻¹ : Op₁ M)
(inv-r : RightInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-right : RightCancel _∙_
cancels-∙-right {c} {x} {y} e
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ∙= idp (! inv-r) ⟩
x ∙ (c ∙ c ⁻¹) ≡⟨ assoc= e ⟩
y ∙ (c ∙ c ⁻¹) ≡⟨ ∙= idp inv-r ⟩
y ∙ ε ≡⟨ snd identity ⟩
y ∎
inv-l : LeftInverse ε _⁻¹ _∙_
inv-l = cancels-∙-right (assoc ♦ ∙= idp inv-r ♦ comm-ε)
module _ {x y} where
is-ε-left : x ≡ ε → x ∙ y ≡ y
is-ε-left e = ap (λ z → z ∙ _) e ♦ fst identity
is-ε-right : y ≡ ε → x ∙ y ≡ x
is-ε-right e = ap (λ z → _ ∙ z) e ♦ snd identity
∙-/ : x ≡ (x ∙ y) / y
∙-/
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-r) ⟩
x ∙ (y / y) ≡⟨ ! assoc ⟩
(x ∙ y) / y ∎
module _ {x y} where
unique-ε-left : x ∙ y ≡ y → x ≡ ε
unique-ε-left eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
y / y ≡⟨ inv-r ⟩
ε ∎
unique-ε-right : x ∙ y ≡ x → y ≡ ε
unique-ε-right eq
= y ≡⟨ ! is-ε-left inv-l ⟩
x ⁻¹ ∙ x ∙ y ≡⟨ assoc ⟩
x ⁻¹ ∙ (x ∙ y) ≡⟨ ∙= idp eq ⟩
x ⁻¹ ∙ x ≡⟨ inv-l ⟩
ε ∎
unique-⁻¹ : x ∙ y ≡ ε → x ≡ y ⁻¹
unique-⁻¹ eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
ε / y ≡⟨ fst identity ⟩
y ⁻¹ ∎
open FromLeftInverse _⁻¹ inv-l hiding (inv-r)
ε⁻¹-ε : ε ⁻¹ ≡ ε
ε⁻¹-ε = unique-ε-left inv-l
involutive : Involutive _⁻¹
involutive {x}
= cancels-∙-right
(x ⁻¹ ⁻¹ ∙ x ⁻¹ ≡⟨ inv-l ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ∎)
⁻¹-hom′ : ∀ {x y} → (x ∙ y)⁻¹ ≡ y ⁻¹ ∙ x ⁻¹
⁻¹-hom′ {x} {y} = cancels-∙-left {x ∙ y}
((x ∙ y) ∙ (x ∙ y)⁻¹ ≡⟨ inv-r ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ≡⟨ ap (_∙_ x) (! fst identity) ⟩
x ∙ (ε ∙ x ⁻¹) ≡⟨ ∙= idp (∙= (! inv-r) idp) ⟩
x ∙ ((y ∙ y ⁻¹) ∙ x ⁻¹) ≡⟨ ap (_∙_ x) assoc ⟩
x ∙ (y ∙ (y ⁻¹ ∙ x ⁻¹)) ≡⟨ ! assoc ⟩
(x ∙ y) ∙ (y ⁻¹ ∙ x ⁻¹) ∎)
elim-∙-left-⁻¹∙ : ∀ {c x y} → (c ∙ x)⁻¹ ∙ (c ∙ y) ≡ x ⁻¹ ∙ y
elim-∙-left-⁻¹∙ {c} {x} {y}
= (c ∙ x)⁻¹ ∙ (c ∙ y) ≡⟨ ∙= ⁻¹-hom′ idp ⟩
x ⁻¹ ∙ c ⁻¹ ∙ (c ∙ y) ≡⟨ elim-inner= inv-l ⟩
x ⁻¹ ∙ y ∎
elim-∙-right-/ : ∀ {c x y} → (x ∙ c) / (y ∙ c) ≡ x / y
elim-∙-right-/ {c} {x} {y}
= x ∙ c ∙ (y ∙ c)⁻¹ ≡⟨ ap (_∙_ _) ⁻¹-hom′ ⟩
x ∙ c ∙ (c ⁻¹ / y) ≡⟨ elim-inner= inv-r ⟩
x / y ∎
module _ {b} where
^⁺suc : ∀ n → b ^⁺(1+ n) ≡ b ^⁺ n ∙ b
^⁺suc 0 = comm-ε
^⁺suc (1+ n) = ap (_∙_ b) (^⁺suc n) ♦ ! assoc
^⁺-comm : ∀ n → b ∙ b ^⁺ n ≡ b ^⁺ n ∙ b
^⁺-comm = ^⁺suc
^⁻suc : ∀ n → b ^⁻(1+ n) ≡ b ⁻¹ ∙ b ^⁻ n
^⁻suc n = ap _⁻¹ (^⁺suc n) ♦ ⁻¹-hom′
^⁻′-spec : ∀ n → b ^⁻′ n ≡ b ^⁻ n
^⁻′-spec 0 = ! ε⁻¹-ε
^⁻′-spec (1+ n) = ap (_∙_ (b ⁻¹)) (^⁻′-spec n)
♦ ! ⁻¹-hom′
♦ ap _⁻¹ (! ^⁺suc n)
^⁻′1-id : b ^⁻′ 1 ≡ b ⁻¹
^⁻′1-id = snd identity
^⁻1-id : b ^⁻ 1 ≡ b ⁻¹
^⁻1-id = ! ^⁻′-spec 1 ♦ ^⁻′1-id
^⁻′2-∙ : b ^⁻′ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻′2-∙ = ap (_∙_ _) ^⁻′1-id
^⁻2-∙ : b ^⁻ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻2-∙ = ! ^⁻′-spec 2 ♦ ^⁻′2-∙
record Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-struct : Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Monoid-Struct mon-struct public
record Commutative-Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
field
mon-struct : Monoid-Struct mon-ops
comm : Commutative _∙_
open Monoid-Struct mon-struct public
open FromAssocComm _∙_ assoc comm public
hiding (!assoc=; assoc=; inner=)
record Commutative-Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-comm : Commutative-Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Commutative-Monoid-Struct mon-comm public
mon : Monoid M
mon = record { mon-struct = mon-struct }
-- A renaming of Monoid with additive notation
module Additive-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
)
-- A renaming of Monoid with multiplicative notation
module Multiplicative-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
)
module Additive-Commutative-Monoid {M} (mon-comm : Commutative-Monoid M)
= Commutative-Monoid mon-comm
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; assoc-comm to +-assoc-comm
; interchange to +-interchange
; outer= to +-outer=
)
module Multiplicative-Commutative-Monoid {M} (mon : Commutative-Monoid M) = Commutative-Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; assoc-comm to *-assoc-comm
; interchange to *-interchange
; outer= to *-outer=
)
record MonoidHomomorphism {A B : Set}
(monA0+ : Monoid A)
(monB1* : Monoid B)
(f : A → B) : Set where
open Additive-Monoid monA0+
open Multiplicative-Monoid monB1*
field
0-hom-1 : f 0ᵐ ≡ 1ᵐ
+-hom-* : ∀ {x y} → f (x + y) ≡ f x * f y
-- -}
-- -}
-- -}
-- -}
|
open import Function.NP
open import Data.Product.NP
open import Data.Nat
using (ℕ; zero)
renaming (_+_ to _+ℕ_; _*_ to _*ℕ_; suc to 1+_)
open import Data.Integer
using (ℤ; +_; -[1+_]; _⊖_)
renaming (_+_ to _+ℤ_; _*_ to _*ℤ_)
open import Relation.Binary.PropositionalEquality.NP renaming (_∙_ to _♦_)
open import Algebra.FunctionProperties.Eq
open ≡-Reasoning
module Algebra.Monoid where
record Monoid-Ops {ℓ} (M : Set ℓ) : Set ℓ where
constructor mk
infixl 7 _∙_
field
_∙_ : M → M → M
ε : M
_^⁺_ : M → ℕ → M
x ^⁺ n = nest n (_∙_ x) ε
module FromInverseOp
(_⁻¹ : Op₁ M)
where
infixl 7 _/_
_/_ : M → M → M
x / y = x ∙ y ⁻¹
open FromOp₂ _/_ public renaming (op= to /=)
_^⁻_ _^⁻′_ : M → ℕ → M
x ^⁻ n = (x ^⁺ n)⁻¹
x ^⁻′ n = (x ⁻¹)^⁺ n
_^_ : M → ℤ → M
x ^ -[1+ n ] = x ^⁻(1+ n)
x ^ (+ n) = x ^⁺ n
record Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
-- laws
field
assoc : Associative _∙_
identity : Identity ε _∙_
open FromOp₂ _∙_ public renaming (op= to ∙=)
open FromAssoc _∙_ assoc public
module _ {b} where
^⁺0-ε : b ^⁺ 0 ≡ ε
^⁺0-ε = idp
^⁺1-id : b ^⁺ 1 ≡ b
^⁺1-id = snd identity
^⁺2-∙ : b ^⁺ 2 ≡ b ∙ b
^⁺2-∙ = ap (_∙_ _) ^⁺1-id
^⁺-+ : ∀ m {n} → b ^⁺ (m +ℕ n) ≡ b ^⁺ m ∙ b ^⁺ n
^⁺-+ 0 = ! fst identity
^⁺-+ (1+ m) = ap (_∙_ b) (^⁺-+ m) ♦ ! assoc
^⁺-* : ∀ m {n} → b ^⁺(m *ℕ n) ≡ (b ^⁺ n)^⁺ m
^⁺-* 0 = idp
^⁺-* (1+ m) {n}
= b ^⁺ (n +ℕ m *ℕ n) ≡⟨ ^⁺-+ n ⟩
b ^⁺ n ∙ b ^⁺(m *ℕ n) ≡⟨ ap (_∙_ (b ^⁺ n)) (^⁺-* m) ⟩
b ^⁺ n ∙ (b ^⁺ n)^⁺ m ∎
comm-ε : ∀ {x} → x ∙ ε ≡ ε ∙ x
comm-ε = snd identity ♦ ! fst identity
module _ {c x y} (e : (x ∙ y) ≡ ε) where
elim-assoc= : (c ∙ x) ∙ y ≡ c
elim-assoc= = assoc ♦ ∙= idp e ♦ snd identity
elim-!assoc= : x ∙ (y ∙ c) ≡ c
elim-!assoc= = ! assoc ♦ ∙= e idp ♦ fst identity
module _ {c d x y} (e : (x ∙ y) ≡ ε) where
elim-inner= : (c ∙ x) ∙ (y ∙ d) ≡ c ∙ d
elim-inner= = assoc ♦ ap (_∙_ c) (elim-!assoc= e)
module FromLeftInverse
(_⁻¹ : Op₁ M)
(inv-l : LeftInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-left : LeftCancel _∙_
cancels-∙-left {c} {x} {y} e
= x ≡⟨ ! fst identity ⟩
ε ∙ x ≡⟨ ∙= (! inv-l) idp ⟩
c ⁻¹ ∙ c ∙ x ≡⟨ !assoc= e ⟩
c ⁻¹ ∙ c ∙ y ≡⟨ ∙= inv-l idp ⟩
ε ∙ y ≡⟨ fst identity ⟩
y ∎
inv-r : RightInverse ε _⁻¹ _∙_
inv-r = cancels-∙-left (! assoc ♦ ∙= inv-l idp ♦ ! comm-ε)
/-∙ : ∀ {x y} → x ≡ (x / y) ∙ y
/-∙ {x} {y}
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-l) ⟩
x ∙ (y ⁻¹ ∙ y) ≡⟨ ! assoc ⟩
(x / y) ∙ y ∎
module FromRightInverse
(_⁻¹ : Op₁ M)
(inv-r : RightInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-right : RightCancel _∙_
cancels-∙-right {c} {x} {y} e
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ∙= idp (! inv-r) ⟩
x ∙ (c ∙ c ⁻¹) ≡⟨ assoc= e ⟩
y ∙ (c ∙ c ⁻¹) ≡⟨ ∙= idp inv-r ⟩
y ∙ ε ≡⟨ snd identity ⟩
y ∎
inv-l : LeftInverse ε _⁻¹ _∙_
inv-l = cancels-∙-right (assoc ♦ ∙= idp inv-r ♦ comm-ε)
module _ {x y} where
is-ε-left : x ≡ ε → x ∙ y ≡ y
is-ε-left e = ap (λ z → z ∙ _) e ♦ fst identity
is-ε-right : y ≡ ε → x ∙ y ≡ x
is-ε-right e = ap (λ z → _ ∙ z) e ♦ snd identity
∙-/ : x ≡ (x ∙ y) / y
∙-/
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-r) ⟩
x ∙ (y / y) ≡⟨ ! assoc ⟩
(x ∙ y) / y ∎
module _ {x y} where
unique-ε-left : x ∙ y ≡ y → x ≡ ε
unique-ε-left eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
y / y ≡⟨ inv-r ⟩
ε ∎
unique-ε-right : x ∙ y ≡ x → y ≡ ε
unique-ε-right eq
= y ≡⟨ ! is-ε-left inv-l ⟩
x ⁻¹ ∙ x ∙ y ≡⟨ assoc ⟩
x ⁻¹ ∙ (x ∙ y) ≡⟨ ∙= idp eq ⟩
x ⁻¹ ∙ x ≡⟨ inv-l ⟩
ε ∎
unique-⁻¹ : x ∙ y ≡ ε → x ≡ y ⁻¹
unique-⁻¹ eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
ε / y ≡⟨ fst identity ⟩
y ⁻¹ ∎
open FromLeftInverse _⁻¹ inv-l hiding (inv-r)
ε⁻¹-ε : ε ⁻¹ ≡ ε
ε⁻¹-ε = unique-ε-left inv-l
involutive : Involutive _⁻¹
involutive {x}
= cancels-∙-right
(x ⁻¹ ⁻¹ ∙ x ⁻¹ ≡⟨ inv-l ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ∎)
⁻¹-hom′ : ∀ {x y} → (x ∙ y)⁻¹ ≡ y ⁻¹ ∙ x ⁻¹
⁻¹-hom′ {x} {y} = cancels-∙-left {x ∙ y}
((x ∙ y) ∙ (x ∙ y)⁻¹ ≡⟨ inv-r ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ≡⟨ ap (_∙_ x) (! fst identity) ⟩
x ∙ (ε ∙ x ⁻¹) ≡⟨ ∙= idp (∙= (! inv-r) idp) ⟩
x ∙ ((y ∙ y ⁻¹) ∙ x ⁻¹) ≡⟨ ap (_∙_ x) assoc ⟩
x ∙ (y ∙ (y ⁻¹ ∙ x ⁻¹)) ≡⟨ ! assoc ⟩
(x ∙ y) ∙ (y ⁻¹ ∙ x ⁻¹) ∎)
elim-∙-left-⁻¹∙ : ∀ {c x y} → (c ∙ x)⁻¹ ∙ (c ∙ y) ≡ x ⁻¹ ∙ y
elim-∙-left-⁻¹∙ {c} {x} {y}
= (c ∙ x)⁻¹ ∙ (c ∙ y) ≡⟨ ∙= ⁻¹-hom′ idp ⟩
x ⁻¹ ∙ c ⁻¹ ∙ (c ∙ y) ≡⟨ elim-inner= inv-l ⟩
x ⁻¹ ∙ y ∎
elim-∙-right-/ : ∀ {c x y} → (x ∙ c) / (y ∙ c) ≡ x / y
elim-∙-right-/ {c} {x} {y}
= x ∙ c ∙ (y ∙ c)⁻¹ ≡⟨ ap (_∙_ _) ⁻¹-hom′ ⟩
x ∙ c ∙ (c ⁻¹ / y) ≡⟨ elim-inner= inv-r ⟩
x / y ∎
module _ {b} where
^⁺suc : ∀ n → b ^⁺(1+ n) ≡ b ^⁺ n ∙ b
^⁺suc 0 = comm-ε
^⁺suc (1+ n) = ap (_∙_ b) (^⁺suc n) ♦ ! assoc
^⁺-comm : ∀ n → b ∙ b ^⁺ n ≡ b ^⁺ n ∙ b
^⁺-comm = ^⁺suc
^⁻suc : ∀ n → b ^⁻(1+ n) ≡ b ⁻¹ ∙ b ^⁻ n
^⁻suc n = ap _⁻¹ (^⁺suc n) ♦ ⁻¹-hom′
^⁻′-spec : ∀ n → b ^⁻′ n ≡ b ^⁻ n
^⁻′-spec 0 = ! ε⁻¹-ε
^⁻′-spec (1+ n) = ap (_∙_ (b ⁻¹)) (^⁻′-spec n)
♦ ! ⁻¹-hom′
♦ ap _⁻¹ (! ^⁺suc n)
^⁻′1-id : b ^⁻′ 1 ≡ b ⁻¹
^⁻′1-id = snd identity
^⁻1-id : b ^⁻ 1 ≡ b ⁻¹
^⁻1-id = ! ^⁻′-spec 1 ♦ ^⁻′1-id
^⁻′2-∙ : b ^⁻′ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻′2-∙ = ap (_∙_ _) ^⁻′1-id
^⁻2-∙ : b ^⁻ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻2-∙ = ! ^⁻′-spec 2 ♦ ^⁻′2-∙
record Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-struct : Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Monoid-Struct mon-struct public
record Commutative-Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
field
mon-struct : Monoid-Struct mon-ops
comm : Commutative _∙_
open Monoid-Struct mon-struct public
open FromAssocComm _∙_ assoc comm public
hiding (!assoc=; assoc=; inner=)
record Commutative-Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-comm : Commutative-Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Commutative-Monoid-Struct mon-comm public
mon : Monoid M
mon = record { mon-struct = mon-struct }
-- A renaming of Monoid with additive notation
module Additive-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
)
-- A renaming of Monoid with multiplicative notation
module Multiplicative-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
)
module Additive-Commutative-Monoid {M} (mon-comm : Commutative-Monoid M)
= Commutative-Monoid mon-comm
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; assoc-comm to +-assoc-comm
; interchange to +-interchange
; outer= to +-outer=
)
module Multiplicative-Commutative-Monoid {M} (mon : Commutative-Monoid M) = Commutative-Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; assoc-comm to *-assoc-comm
; interchange to *-interchange
; outer= to *-outer=
)
record MonoidHomomorphism {A B : Set}
(monA0+ : Monoid A)
(monB1* : Monoid B)
(f : A → B) : Set where
open Additive-Monoid monA0+
open Multiplicative-Monoid monB1*
field
0-hom-1 : f 0ᵐ ≡ 1ᵐ
+-hom-* : ∀ {x y} → f (x + y) ≡ f x * f y
-- -}
-- -}
-- -}
-- -}
|
use nest and not fold
|
Monoid: use nest and not fold
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
7826abee6b67c7b55aa70968ca7fd6b637d30857
|
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
-- ===============
import Data.List as List
open import Base.Data.ContextList public
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
_⋎_ = List._++_
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.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
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 and subcontexts,
-- together with 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 import Base.Data.ContextList public
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 define weakening based on subcontext relationship.
-- 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.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
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.
open Subcontexts public
|
Drop Prefixes
|
Drop Prefixes
|
Agda
|
mit
|
inc-lc/ilc-agda
|
d4d74883fdfb5330e27bb48434b046f8291b5f3d
|
Denotation/Change/Popl14.agda
|
Denotation/Change/Popl14.agda
|
module Denotation.Change.Popl14 where
-- 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 Popl14.Denotation.Value
open import Theorem.Groups-Popl14
open import Postulate.Extensionality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
-- apply Δv v ::= v ⊞ Δv
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
-- diff u v ::= u ⊟ v
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
-- 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 : ⟦ τ ⟧} →
let
_+_ = _⊞_ {τ}
_-_ = _⊟_ {τ}
in
v + (u - v) ≡ u
--------------------
-- Implementation --
--------------------
-- ΔVal τ is intended to be the semantic domain for changes of values
-- of type τ.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
--
-- ΔVal : Type → Set
ΔVal (base base-int) = ℤ
ΔVal (base base-bag) = Bag
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {base base-int} n Δn = n + Δn
_⊞_ {base base-bag} b Δb = b ++ Δb
_⊞_ {σ ⇒ τ} f Δf = λ v → f v ⊞ Δf v (v ⊟ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {base base-int} m n = m - n
_⊟_ {base base-bag} d b = d \\ b
_⊟_ {σ ⇒ τ} g f = λ v Δv R[v,Δv] → g (v ⊞ Δv) ⊟ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base base-int} n Δn = ⊤
valid {base base-bag} b Δb = ⊤
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (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 base-int} {u} {v} = n+[m-n]=m {v} {u}
v+[u-v]=u {base base-bag} {u} {v} = a++[b\\a]=b {v} {u}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
let
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₀_ = _⊞_ {σ}
_-₀_ = _⊟_ {σ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
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 base-int} {u} {v} = tt
R[v,u-v] {base base-bag} {u} {v} = tt
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
-- `diff` and `apply`, without validity proofs
infixl 6 _⟦⊕⟧_ _⟦⊝⟧_
_⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_⟦⊕⟧_ {base base-int} n Δn = n + Δn
_⟦⊕⟧_ {base base-bag} b Δb = b ++ Δb
_⟦⊕⟧_ {σ ⇒ τ} f Δf = λ v →
let
_-₀_ = _⟦⊝⟧_ {σ}
_+₁_ = _⟦⊕⟧_ {τ}
in
f v +₁ Δf v (v -₀ v)
_⟦⊝⟧_ {base base-int} m n = m - n
_⟦⊝⟧_ {base base-bag} a b = a \\ b
_⟦⊝⟧_ {σ ⇒ τ} g f = λ v Δv → _⟦⊝⟧_ {τ} (g (_⟦⊕⟧_ {σ} v Δv)) (f v)
|
module Denotation.Change.Popl14 where
-- 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 Popl14.Denotation.Value
open import Theorem.Groups-Popl14
open import Postulate.Extensionality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
-- apply Δv v ::= v ⊞ Δv
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
-- diff u v ::= u ⊟ v
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
-- 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 : ⟦ τ ⟧} →
let
_+_ = _⊞_ {τ}
_-_ = _⊟_ {τ}
in
v + (u - v) ≡ u
--------------------
-- Implementation --
--------------------
-- (ΔVal τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (ΔVal (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
--
-- ΔVal : Type → Set
ΔVal (base base-int) = ℤ
ΔVal (base base-bag) = Bag
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {base base-int} n Δn = n + Δn
_⊞_ {base base-bag} b Δb = b ++ Δb
_⊞_ {σ ⇒ τ} f Δf = λ v → f v ⊞ Δf v (v ⊟ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {base base-int} m n = m - n
_⊟_ {base base-bag} d b = d \\ b
_⊟_ {σ ⇒ τ} g f = λ v Δv R[v,Δv] → g (v ⊞ Δv) ⊟ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base base-int} n Δn = ⊤
valid {base base-bag} b Δb = ⊤
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (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 base-int} {u} {v} = n+[m-n]=m {v} {u}
v+[u-v]=u {base base-bag} {u} {v} = a++[b\\a]=b {v} {u}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
let
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₀_ = _⊞_ {σ}
_-₀_ = _⊟_ {σ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
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 base-int} {u} {v} = tt
R[v,u-v] {base base-bag} {u} {v} = tt
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
-- `diff` and `apply`, without validity proofs
infixl 6 _⟦⊕⟧_ _⟦⊝⟧_
_⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_⟦⊕⟧_ {base base-int} n Δn = n + Δn
_⟦⊕⟧_ {base base-bag} b Δb = b ++ Δb
_⟦⊕⟧_ {σ ⇒ τ} f Δf = λ v →
let
_-₀_ = _⟦⊝⟧_ {σ}
_+₁_ = _⟦⊕⟧_ {τ}
in
f v +₁ Δf v (v -₀ v)
_⟦⊝⟧_ {base base-int} m n = m - n
_⟦⊝⟧_ {base base-bag} a b = a \\ b
_⟦⊝⟧_ {σ ⇒ τ} g f = λ v Δv → _⟦⊝⟧_ {τ} (g (_⟦⊕⟧_ {σ} v Δv)) (f v)
|
Improve documentation of ΔVal.
|
Improve documentation of ΔVal.
Old-commit-hash: fb71a0ea6af97c62b01a9d8b7d3089e5cd3139ed
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a0c0e331a5839a6f7cd2921fe50b2acfdf977244
|
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₃) =
begin
Δ (if t₁ t₂ t₃)
≈⟨ Δ-if {{Γ′}} t₁ t₂ t₃ ⟩
if (Δ t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (Δ t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (Δ t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(Δ t₂)
(Δ t₃))
≈⟨ ≈-if (derive-term-correct t₁)
(≈-if (≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct t₃) ≈-refl) ≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct t₂) ≈-refl) ≈-refl))
(≈-if (≈-refl)
(derive-term-correct t₂)
(derive-term-correct t₃)) ⟩
if (derive-term t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (derive-term t₃) (lift-term {{Γ′}} t₃)) (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)
|
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 {{Γ′}} t₁ t₂ t₃ ⟩
if (Δ t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (Δ {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (Δ {{Γ′}} t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(Δ {{Γ′}} t₂)
(Δ {{Γ′}} t₃))
≈⟨ ≈-if (derive-term-correct {{Γ′}} t₁)
(≈-if (≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₃) ≈-refl) ≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₂) ≈-refl) ≈-refl))
(≈-if (≈-refl)
(derive-term-correct {{Γ′}} t₂)
(derive-term-correct {{Γ′}} t₃)) ⟩
if (derive-term {{Γ′}} t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (derive-term {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (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)
|
Resolve implicit arguments.
|
Resolve implicit arguments.
When I filled the last hole in total.agda in the previous commit,
Agda told me to 'resolve implicit argument', so I added explicit
implicit arguments until Agda was satisfied. I don't know whether
or why it matters to do this.
Old-commit-hash: ed138bd06659ab2caac47a670b7d3d60c4d97f3a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
db5307718bfbf262aa80ac0a0dd4b8da3799363b
|
Denotational/WeakValidChanges.agda
|
Denotational/WeakValidChanges.agda
|
module Denotational.WeakValidChanges where
open import Data.Product
open import Data.Unit
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.ChangeTypes.ChangesAreDerivatives
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Changes
open import Denotational.EqualityLemmas
-- DEFINITION of weakly valid changes via a logical relation
-- Strong validity:
open import Denotational.ValidChanges
-- Weak validity, defined through a function producing a type.
--
-- Since this is a logical relation, this couldn't be defined as a
-- datatype, since it is not strictly positive.
Weak-Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set
Weak-Valid-Δ {bool} v dv = ⊤
Weak-Valid-Δ {S ⇒ T} f df =
∀ (s : ⟦ S ⟧) ds (valid-w : Weak-Valid-Δ s ds) →
Weak-Valid-Δ (f s) (df s ds) ×
df is-correct-for f on s and ds
strong-to-weak-validity : ∀ {τ : Type} {v : ⟦ τ ⟧} {dv} → Valid-Δ v dv → Weak-Valid-Δ v dv
strong-to-weak-validity {bool} _ = tt
strong-to-weak-validity {τ ⇒ τ₁} {v} {dv} s-valid-v-dv s ds _ = strong-to-weak-validity dv-s-ds-valid-on-v-s , dv-is-correct-for-v-on-s-and-ds
where
proofs = s-valid-v-dv s ds
dv-s-ds-valid-on-v-s = proj₁ proofs
dv-is-correct-for-v-on-s-and-ds = proj₂ proofs
{-
-- This proof doesn't go through: the desired equivalence is too
-- strong, and we can't fill the holes because dv is arbitrary.
diff-apply-proof-weak : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≡ dv
diff-apply-proof-weak {τ₁ ⇒ τ₂} 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)) (strong-to-weak-validity (derive-is-valid (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-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv {!!})) ⟩
df v dv
∎)) where open ≡-Reasoning
diff-apply-proof-weak {bool} db b _ = xor-cancellative b db
-- That'd be the core of the proof of diff-apply-weak, if we relax the
-- equivalence in the goal to something more correct. We need a proof
-- of validity only for the first argument, somehow: maybe because
-- diff itself produces strongly valid changes?
--
-- Ahah! Not really: below we use the unprovable
-- diff-apply-proof-weak. To be able to use induction, we need to use
-- this weaker lemma again. Hence, we'll only be able to prove the
-- same equivalence, which will needs another proof of validity to
-- unfold further until the base case.
diff-apply-proof-weak-f : ∀ {τ₁ τ₂} (df : ⟦ Δ-Type (τ₁ ⇒ τ₂) ⟧) (f : ⟦ τ₁ ⇒ τ₂ ⟧) →
(Weak-Valid-Δ f df) → ∀ dv v → (Weak-Valid-Δ v dv) → (diff (apply df f) f) v dv ≡ df v dv
diff-apply-proof-weak-f {τ₁} {τ₂} df f df-valid dv v dv-valid =
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)) (strong-to-weak-validity (derive-is-valid (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 dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid)) ⟩
df v dv
∎ where open ≡-Reasoning
-}
-- A stronger delta equivalence. Note this isn't a congruence; it
-- should be possible to define some congruence rules, but those will
-- be more complex.
data _≈_ : {τ : Type} → ⟦ Δ-Type τ ⟧ → ⟦ Δ-Type τ ⟧ → Set where
--base : ∀ (v : ⟦ bool ⟧) → v ≈ v
base : ∀ (v1 v2 : ⟦ bool ⟧) → (≡ : v1 ≡ v2) → v1 ≈ v2
fun : ∀ {σ τ} (f1 f2 : ⟦ Δ-Type (σ ⇒ τ) ⟧) →
(t≈ : ∀ s ds (valid : Weak-Valid-Δ s ds) → f1 s ds ≈ f2 s ds) →
f1 ≈ f2
≡→≈ : ∀ {τ} {v1 v2 : ⟦ Δ-Type τ ⟧} → v1 ≡ v2 → v1 ≈ v2
≡→≈ {bool} {v1} {.v1} refl = base v1 v1 refl
≡→≈ {τ₁ ⇒ τ₂} {v1} {.v1} refl = fun v1 v1 (λ s ds _ → ≡→≈ refl)
open import Relation.Binary hiding (_⇒_)
≈-refl : ∀ {τ} → Reflexive (_≈_ {τ})
≈-refl = ≡→≈ refl
≈-sym : ∀ {τ} → Symmetric (_≈_ {τ})
≈-sym (base v1 v2 ≡) = base _ _ (sym ≡)
≈-sym {(σ ⇒ τ)} (fun f1 f2 t≈) = fun _ _ (λ s ds valid → ≈-sym (t≈ s ds valid))
≈-trans : ∀ {τ} → Transitive (_≈_ {τ})
≈-trans {.bool} {.k} {.k} {k} (base .k .k refl) (base .k .k refl) = base k k refl
≈-trans {σ ⇒ τ} {.f1} {.f2} {k} (fun f1 .f2 ≈₁) (fun f2 .k ≈₂) = fun f1 k (λ s ds valid → ≈-trans (≈₁ _ _ valid) (≈₂ _ _ valid))
≈-isEquivalence : ∀ {τ} → IsEquivalence (_≈_ {τ})
≈-isEquivalence = record
{ refl = ≈-refl
; sym = ≈-sym
; trans = ≈-trans
}
≈-setoid : Type → Setoid _ _
≈-setoid τ = record
{ Carrier = ⟦ Δ-Type τ ⟧
; _≈_ = _≈_
; isEquivalence = ≈-isEquivalence
}
module ≈-Reasoning where
module _ {τ : Type} where
open import Relation.Binary.EqReasoning (≈-setoid τ) public
hiding (_≡⟨_⟩_)
-- Correct proof, to refactor using ≈-Reasoning
diff-apply-proof-weak-real : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≈ dv
diff-apply-proof-weak-real {τ₁ ⇒ τ₂} df f df-valid = fun _ _ (λ v dv dv-valid → ≈-trans (≡→≈ (
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)) (strong-to-weak-validity (derive-is-valid (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 dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
∎)) (diff-apply-proof-weak-real {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid))))
--≈⟨ fill in the above proof ⟩
--df v dv
where open ≡-Reasoning
diff-apply-proof-weak-real {bool} db b _ = ≡→≈ (xor-cancellative b db)
|
module Denotational.WeakValidChanges where
open import Data.Product
open import Data.Unit
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.ChangeTypes.ChangesAreDerivatives
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Changes
open import Denotational.EqualityLemmas
-- DEFINITION of weakly valid changes via a logical relation
-- Strong validity:
open import Denotational.ValidChanges
-- Weak validity, defined through a function producing a type.
--
-- Since this is a logical relation, this couldn't be defined as a
-- datatype, since it is not strictly positive.
Weak-Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set
Weak-Valid-Δ {bool} v dv = ⊤
Weak-Valid-Δ {S ⇒ T} f df =
∀ (s : ⟦ S ⟧) ds (valid-w : Weak-Valid-Δ s ds) →
Weak-Valid-Δ (f s) (df s ds) ×
df is-correct-for f on s and ds
strong-to-weak-validity : ∀ {τ : Type} {v : ⟦ τ ⟧} {dv} → Valid-Δ v dv → Weak-Valid-Δ v dv
strong-to-weak-validity {bool} _ = tt
strong-to-weak-validity {τ ⇒ τ₁} {v} {dv} s-valid-v-dv = λ s ds _ →
let
proofs = s-valid-v-dv s ds
dv-s-ds-valid-on-v-s = proj₁ proofs
dv-is-correct-for-v-on-s-and-ds = proj₂ proofs
in
strong-to-weak-validity dv-s-ds-valid-on-v-s , dv-is-correct-for-v-on-s-and-ds
{-
-- This proof doesn't go through: the desired equivalence is too
-- strong, and we can't fill the holes because dv is arbitrary.
diff-apply-proof-weak : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≡ dv
diff-apply-proof-weak {τ₁ ⇒ τ₂} 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)) (strong-to-weak-validity (derive-is-valid (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-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv {!!})) ⟩
df v dv
∎)) where open ≡-Reasoning
diff-apply-proof-weak {bool} db b _ = xor-cancellative b db
-- That'd be the core of the proof of diff-apply-weak, if we relax the
-- equivalence in the goal to something more correct. We need a proof
-- of validity only for the first argument, somehow: maybe because
-- diff itself produces strongly valid changes?
--
-- Ahah! Not really: below we use the unprovable
-- diff-apply-proof-weak. To be able to use induction, we need to use
-- this weaker lemma again. Hence, we'll only be able to prove the
-- same equivalence, which will needs another proof of validity to
-- unfold further until the base case.
diff-apply-proof-weak-f : ∀ {τ₁ τ₂} (df : ⟦ Δ-Type (τ₁ ⇒ τ₂) ⟧) (f : ⟦ τ₁ ⇒ τ₂ ⟧) →
(Weak-Valid-Δ f df) → ∀ dv v → (Weak-Valid-Δ v dv) → (diff (apply df f) f) v dv ≡ df v dv
diff-apply-proof-weak-f {τ₁} {τ₂} df f df-valid dv v dv-valid =
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)) (strong-to-weak-validity (derive-is-valid (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 dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid)) ⟩
df v dv
∎ where open ≡-Reasoning
-}
-- A stronger delta equivalence. Note this isn't a congruence; it
-- should be possible to define some congruence rules, but those will
-- be more complex.
data _≈_ : {τ : Type} → ⟦ Δ-Type τ ⟧ → ⟦ Δ-Type τ ⟧ → Set where
--base : ∀ (v : ⟦ bool ⟧) → v ≈ v
base : ∀ (v1 v2 : ⟦ bool ⟧) → (≡ : v1 ≡ v2) → v1 ≈ v2
fun : ∀ {σ τ} (f1 f2 : ⟦ Δ-Type (σ ⇒ τ) ⟧) →
(t≈ : ∀ s ds (valid : Weak-Valid-Δ s ds) → f1 s ds ≈ f2 s ds) →
f1 ≈ f2
≡→≈ : ∀ {τ} {v1 v2 : ⟦ Δ-Type τ ⟧} → v1 ≡ v2 → v1 ≈ v2
≡→≈ {bool} {v1} {.v1} refl = base v1 v1 refl
≡→≈ {τ₁ ⇒ τ₂} {v1} {.v1} refl = fun v1 v1 (λ s ds _ → ≡→≈ refl)
open import Relation.Binary hiding (_⇒_)
≈-refl : ∀ {τ} → Reflexive (_≈_ {τ})
≈-refl = ≡→≈ refl
≈-sym : ∀ {τ} → Symmetric (_≈_ {τ})
≈-sym (base v1 v2 ≡) = base _ _ (sym ≡)
≈-sym {(σ ⇒ τ)} (fun f1 f2 t≈) = fun _ _ (λ s ds valid → ≈-sym (t≈ s ds valid))
≈-trans : ∀ {τ} → Transitive (_≈_ {τ})
≈-trans {.bool} {.k} {.k} {k} (base .k .k refl) (base .k .k refl) = base k k refl
≈-trans {σ ⇒ τ} {.f1} {.f2} {k} (fun f1 .f2 ≈₁) (fun f2 .k ≈₂) = fun f1 k (λ s ds valid → ≈-trans (≈₁ _ _ valid) (≈₂ _ _ valid))
≈-isEquivalence : ∀ {τ} → IsEquivalence (_≈_ {τ})
≈-isEquivalence = record
{ refl = ≈-refl
; sym = ≈-sym
; trans = ≈-trans
}
≈-setoid : Type → Setoid _ _
≈-setoid τ = record
{ Carrier = ⟦ Δ-Type τ ⟧
; _≈_ = _≈_
; isEquivalence = ≈-isEquivalence
}
module ≈-Reasoning where
module _ {τ : Type} where
open import Relation.Binary.EqReasoning (≈-setoid τ) public
hiding (_≡⟨_⟩_)
-- Correct proof, to refactor using ≈-Reasoning
diff-apply-proof-weak-real : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≈ dv
diff-apply-proof-weak-real {τ₁ ⇒ τ₂} df f df-valid = fun _ _ (λ v dv dv-valid → ≈-trans (≡→≈ (
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)) (strong-to-weak-validity (derive-is-valid (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 dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
∎)) (diff-apply-proof-weak-real {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid))))
--≈⟨ fill in the above proof ⟩
--df v dv
where open ≡-Reasoning
diff-apply-proof-weak-real {bool} db b _ = ≡→≈ (xor-cancellative b db)
|
Change where-clause to let-expr so that agda 2.3.2 typechecks README
|
Change where-clause to let-expr so that agda 2.3.2 typechecks README
Old-commit-hash: 6ffa74c100a7ee9d51e15d960db14c801f06e27c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
cad090cf1face3e9ee7c8e4575440a427c74e66a
|
lib/Explore/BinTree.agda
|
lib/Explore/BinTree.agda
|
{-# OPTIONS --without-K #-}
module Explore.BinTree where
open import Data.Tree.Binary
open import Explore.Core
open import Explore.Properties
fromBinTree : ∀ {m} {A} → BinTree A → Explore m A
fromBinTree empty = empty-explore
fromBinTree (leaf x) = point-explore x
fromBinTree (fork ℓ r) = merge-explore (fromBinTree ℓ) (fromBinTree r)
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind empty = empty-explore-ind
fromBinTree-ind (leaf x) = point-explore-ind x
fromBinTree-ind (fork ℓ r) = merge-explore-ind (fromBinTree-ind ℓ)
(fromBinTree-ind r)
{-
fromBinTree : ∀ {m A} → BinTree A → Explore m A
fromBinTree (leaf x) _ _ f = f x
fromBinTree (fork ℓ r) ε _∙_ f = fromBinTree ℓ ε _∙_ f ∙ fromBinTree r ε _∙_ f
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind (leaf x) P _ P∙ Pf = Pf x
fromBinTree-ind (fork ℓ r) P Pε P∙ Pf = P∙ (fromBinTree-ind ℓ P Pε P∙ Pf)
(fromBinTree-ind r P Pε P∙ Pf)
-}
|
{-# OPTIONS --without-K #-}
module Explore.BinTree where
open import Level.NP
open import Type hiding (★)
open import Data.Tree.Binary
open import Data.Zero
open import Data.Product
open import Relation.Binary.PropositionalEquality.NP
open import HoTT
open Equivalences
open import Type.Identities
open import Function.Extensionality
open import Explore.Core
open import Explore.Properties
open import Explore.Zero
open import Explore.Sum
open import Explore.Isomorphism
fromBinTree : ∀ {m} {A} → BinTree A → Explore m A
fromBinTree empty = empty-explore
fromBinTree (leaf x) = point-explore x
fromBinTree (fork ℓ r) = merge-explore (fromBinTree ℓ) (fromBinTree r)
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind empty = empty-explore-ind
fromBinTree-ind (leaf x) = point-explore-ind x
fromBinTree-ind (fork ℓ r) = merge-explore-ind (fromBinTree-ind ℓ)
(fromBinTree-ind r)
{-
fromBinTree : ∀ {m A} → BinTree A → Explore m A
fromBinTree (leaf x) _ _ f = f x
fromBinTree (fork ℓ r) ε _∙_ f = fromBinTree ℓ ε _∙_ f ∙ fromBinTree r ε _∙_ f
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind (leaf x) P _ P∙ Pf = Pf x
fromBinTree-ind (fork ℓ r) P Pε P∙ Pf = P∙ (fromBinTree-ind ℓ P Pε P∙ Pf)
(fromBinTree-ind r P Pε P∙ Pf)
-}
AnyP≡ΣfromBinTree : ∀ {p}{A : ★ _}{P : A → ★ p}(xs : BinTree A) → Any P xs ≡ Σᵉ (fromBinTree xs) P
AnyP≡ΣfromBinTree empty = idp
AnyP≡ΣfromBinTree (leaf x) = idp
AnyP≡ΣfromBinTree (fork xs xs₁) = ⊎= (AnyP≡ΣfromBinTree xs) (AnyP≡ΣfromBinTree xs₁)
module _ {{_ : UA}}{{_ : FunExt}}{A : ★ ₀} where
exploreΣ∈ : ∀ {m} xs → Explore m (Σ A λ x → Any (_≡_ x) xs)
exploreΣ∈ empty = explore-iso (coe-equiv (Lift≡id ∙ ! ×𝟘-snd ∙ ×= idp (! Lift≡id))) Lift𝟘ᵉ
exploreΣ∈ (leaf x) = point-explore (x , idp)
exploreΣ∈ (fork xs xs₁) = explore-iso (coe-equiv (! Σ⊎-split)) (exploreΣ∈ xs ⊎ᵉ exploreΣ∈ xs₁)
Σᵉ-adq-exploreΣ∈ : ∀ {m} xs → Adequate-Σᵉ {m} (exploreΣ∈ xs)
Σᵉ-adq-exploreΣ∈ empty = Σ-iso-ok (coe-equiv (Lift≡id ∙ ! ×𝟘-snd ∙ ×= idp (! Lift≡id)))
{Aᵉ = Lift𝟘ᵉ} ΣᵉLift𝟘-ok
Σᵉ-adq-exploreΣ∈ (leaf x₁) F = ! Σ𝟙-snd ∙ Σ-fst≃ (≃-sym (Σx≡≃𝟙 x₁)) F
Σᵉ-adq-exploreΣ∈ (fork xs xs₁) = Σ-iso-ok (coe-equiv (! Σ⊎-split)) {Aᵉ = exploreΣ∈ xs ⊎ᵉ exploreΣ∈ xs₁}
(Σᵉ⊎-ok {eᴬ = exploreΣ∈ xs}{eᴮ = exploreΣ∈ xs₁} (Σᵉ-adq-exploreΣ∈ xs) (Σᵉ-adq-exploreΣ∈ xs₁))
module _ {{_ : UA}}{{_ : FunExt}}{A : ★ ₀}{P : A → ★ _}(explore-P : ∀ {m} x → Explore m (P x)) where
open import Explore.Zero
open import Explore.Sum
open import Explore.Isomorphism
exploreAny : ∀ {m} xs → Explore m (Any P xs)
exploreAny empty = Lift𝟘ᵉ
exploreAny (leaf x) = explore-P x
exploreAny (fork xs xs₁) = exploreAny xs ⊎ᵉ exploreAny xs₁
module _ (Σᵉ-adq-explore-P : ∀ {m} x → Adequate-Σᵉ {m} (explore-P x)) where
Σᵉ-adq-exploreAny : ∀ {m} xs → Adequate-Σᵉ {m} (exploreAny xs)
Σᵉ-adq-exploreAny empty F = ! Σ𝟘-lift∘fst ∙ Σ-fst= (! Lift≡id) _
Σᵉ-adq-exploreAny (leaf x₁) F = Σᵉ-adq-explore-P x₁ F
Σᵉ-adq-exploreAny (fork xs xs₁) F = ⊎= (Σᵉ-adq-exploreAny xs _) (Σᵉ-adq-exploreAny xs₁ _) ∙ ! dist-⊎-Σ
exploreΣᵉ : ∀ {m} xs → Explore m (Σᵉ (fromBinTree xs) P)
exploreΣᵉ {m} xs = fromBinTree-ind xs (λ e → Explore m (Σᵉ e P)) Lift𝟘ᵉ _⊎ᵉ_ explore-P
module _ (Σᵉ-adq-explore-P : ∀ {m} x → Adequate-Σᵉ {m} (explore-P x)) where
Σᵉ-adq-exploreΣᵉ : ∀ {m} xs → Adequate-Σᵉ {m} (exploreΣᵉ xs)
Σᵉ-adq-exploreΣᵉ empty F = ! Σ𝟘-lift∘fst ∙ Σ-fst= (! Lift≡id) _
Σᵉ-adq-exploreΣᵉ (leaf x₁) F = Σᵉ-adq-explore-P x₁ F
Σᵉ-adq-exploreΣᵉ (fork xs xs₁) F = ⊎= (Σᵉ-adq-exploreΣᵉ xs _) (Σᵉ-adq-exploreΣᵉ xs₁ _) ∙ ! dist-⊎-Σ
-- -}
-- -}
-- -}
-- -}
|
Add more exploration functions about binary trees
|
Add more exploration functions about binary trees
|
Agda
|
bsd-3-clause
|
crypto-agda/explore
|
15cd0eafd613a991286818c276496c93db0ada5d
|
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 = (Σ[ τ₁ ∈ 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
-}
|
------------------------------------------------------------------------
-- 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
{-# 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
--
-- 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.
--
-- XXX: This line is the only change, up to now, for the caching semantics,
-- the rest is copied. Inheritance would handle this precisely; without
-- inheritance, we might want to use one of the standard encodings of related
-- features (delegation?).
⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache )
⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set
⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache
{-
⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁
⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache
-}
|
Drop ⟦_⟧*TypeHidCacheWrong
|
Drop ⟦_⟧*TypeHidCacheWrong
This wrong definition should not be counted among the failures before
CBPV, since it was created after and it is due to a separate limitation.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e7b75259e34cf9de11a2696f75d1384a4dc62f8d
|
agda/Nat.agda
|
agda/Nat.agda
|
module Nat where
data ℕ : Set where
zero : ℕ
succ : ℕ → ℕ
_+_ : ℕ → ℕ → ℕ
zero + b = b
succ a + b = succ (a + b)
_×_ : ℕ → ℕ → ℕ
zero × b = zero
succ a × b = (a × b) + b
open import Relation.Binary.PropositionalEquality
0-is-right-identity-of-+ : ∀ (n : ℕ) → n + zero ≡ n
0-is-right-identity-of-+ zero = refl
0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)
+-is-associative : ∀ (a b c : ℕ) → a + (b + c) ≡ (a + b) + c
+-is-associative zero b c = refl
+-is-associative (succ a) b c = cong succ (+-is-associative a b c)
lemma : ∀ (a b : ℕ) → a + succ b ≡ succ (a + b)
lemma zero b = refl
lemma (succ a) b = cong succ (lemma a b)
import Relation.Binary.EqReasoning as EqR
open module EqNat = EqR (setoid ℕ)
+-is-commutative : ∀ (a b : ℕ) → a + b ≡ b + a
+-is-commutative a zero = 0-is-right-identity-of-+ a
+-is-commutative a (succ b) =
begin
a + succ b
≈⟨ lemma a b ⟩
succ (a + b)
≈⟨ cong succ (+-is-commutative a b) ⟩
succ (b + a)
≈⟨ refl ⟩
succ b + a
∎
|
module Nat where
data ℕ : Set where
zero : ℕ
succ : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
{-# BUILTIN ZERO zero #-}
{-# BUILTIN SUC succ #-}
_+_ : ℕ → ℕ → ℕ
zero + b = b
succ a + b = succ (a + b)
_×_ : ℕ → ℕ → ℕ
zero × b = zero
succ a × b = (a × b) + b
open import Relation.Binary.PropositionalEquality
0-is-right-identity-of-+ : ∀ (n : ℕ) → n + zero ≡ n
0-is-right-identity-of-+ zero = refl
0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)
+-is-associative : ∀ (a b c : ℕ) → a + (b + c) ≡ (a + b) + c
+-is-associative zero b c = refl
+-is-associative (succ a) b c = cong succ (+-is-associative a b c)
lemma : ∀ (a b : ℕ) → a + succ b ≡ succ (a + b)
lemma zero b = refl
lemma (succ a) b = cong succ (lemma a b)
import Relation.Binary.EqReasoning as EqR
open module EqNat = EqR (setoid ℕ)
+-is-commutative : ∀ (a b : ℕ) → a + b ≡ b + a
+-is-commutative a zero = 0-is-right-identity-of-+ a
+-is-commutative a (succ b) =
begin
a + succ b
≈⟨ lemma a b ⟩
succ (a + b)
≈⟨ cong succ (+-is-commutative a b) ⟩
succ (b + a)
≈⟨ refl ⟩
succ b + a
∎
|
allow the use of more natural notation in Nat
|
allow the use of more natural notation in Nat
|
Agda
|
unlicense
|
piyush-kurur/sample-code
|
8e1932027422fbfd5254b377ea26f80df138163c
|
Parametric/Change/Term.agda
|
Parametric/Change/Term.agda
|
import Parametric.Syntax.Type as Type
import Base.Syntax.Context as Context
module Parametric.Change.Term
{Base : Set}
(Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -})
where
-- Terms that operate on changes
open Type Base
open Context Type
open import Parametric.Change.Type Δbase
open import Parametric.Syntax.Term Constant
open import Data.Product
-- 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 {τ} {Γ})
|
import Parametric.Syntax.Type as Type
import Base.Syntax.Context as Context
module Parametric.Change.Term
{Base : Set}
(Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -})
{Δbase : Base → Type.Type Base}
where
-- Terms that operate on changes
open Type Base
open Context Type
open import Parametric.Change.Type Δbase
open import Parametric.Syntax.Term Constant
open import Data.Product
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
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 :
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 :
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 {τ} {Γ})
|
Move parameter to module level.
|
Move parameter to module level.
All definitions in this module take the same parameter, so moving that
parameter to the module level makes it easier to instantiate all of the
definitions at once.
Old-commit-hash: c9a73b24cd02aed1ee75b46416c1893c8c37b130
|
Agda
|
mit
|
inc-lc/ilc-agda
|
bdfd1c903bebb00f241b98797e6544d8970bea49
|
Crypto/JS/BigI/FiniteField.agda
|
Crypto/JS/BigI/FiniteField.agda
|
{-# OPTIONS --without-K #-}
open import Type.Eq using (_==_; Eq?; ≡⇒==; ==⇒≡)
open import Data.Two.Base using (𝟚; ✓)
open import Relation.Binary.PropositionalEquality.Base using (_≡_; refl; ap)
open import FFI.JS using (JS[_]; _++_; return; _>>_)
open import FFI.JS.Check
using (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 Algebra.Raw
open import Algebra.Group
open import Algebra.Group.Homomorphism
open import Algebra.Field
-- TODO carry on a primality proof of q
module Crypto.JS.BigI.FiniteField (q : BigI) where
-- The constructor mk is not exported.
private
module Internals where
data ℤ[_] : Set where
mk : BigI → ℤ[_]
mk-inj : ∀ {x y : BigI} → ℤ[_].mk x ≡ mk y → x ≡ y
mk-inj refl = refl
open Internals public using (ℤ[_])
open Internals
ℤq : Set
ℤq = ℤ[_]
private
mod-q : BigI → ℤq
mod-q x = mk (mod x q)
-- There are two ways to go from BigI to ℤq: BigI▹ℤ[ q ] and mod-q
-- Use BigI▹ℤ[ q ] for untrusted input data and mod-q for internal
-- computation.
BigI▹ℤ[_] : BigI → JS[ ℤq ]
BigI▹ℤ[_] x =
-- Console.log "BigI▹ℤ[_]"
check! "below the modulus" (x <I q)
(λ _ → "Not below the modulus: q:" ++ toString q ++
" is less than x:" ++ toString x) >>
check! "positivity" (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") >>
return (mk x)
check-non-zero : ℤq → BigI
check-non-zero (mk x) = -- trace-call "check-non-zero "
check? (x >I 0I) (λ _ → "Should be non zero") x
ℤ[_]▹BigI : ℤq → BigI
ℤ[_]▹BigI (mk x) = x
0# 1# : ℤq
0# = mk 0I
1# = mk 1I
-- One could choose here to return a dummy value when 0 is given.
-- Instead we throw an exception which in some circumstances could
-- be bad if the runtime semantics is too eager.
1/_ : ℤq → ℤq
1/ x = mk (modInv (check-non-zero x) q)
_^I_ : ℤq → BigI → ℤq
mk x ^I y = mk (modPow x y q)
ℤq▹BigI = ℤ[_]▹BigI
BigI▹ℤq = BigI▹ℤ[_]
_⊗I_ : ℤq → BigI → ℤq
x ⊗I y = mod-q (multiply (ℤq▹BigI x) y)
_+_ _−_ _*_ _/_ : ℤq → ℤq → ℤq
x + y = mod-q (add (ℤq▹BigI x) (ℤq▹BigI y))
x − y = mod-q (subtract (ℤq▹BigI x) (ℤq▹BigI y))
x * y = x ⊗I ℤq▹BigI y
x / y = x * 1/ y
*-def : _*_ ≡ (λ x y → x ⊗I ℤq▹BigI y)
*-def = refl
0−_ : ℤq → ℤq
0− x = mod-q (negate (ℤq▹BigI x))
sum prod : List ℤq → ℤq
sum = foldr _+_ 0#
prod = foldr _*_ 1#
instance
ℤ[_]-Eq? : Eq? ℤq
ℤ[_]-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤq → ℤq → 𝟚
mk x ==' mk y = x == y
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
≡⇒==' {mk x} {mk y} p = ≡⇒== (mk-inj p)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
==⇒≡' {mk x} {mk y} p = ap mk (==⇒≡ p)
ℤq-Eq? = ℤ[_]-Eq?
+-mon-ops : Monoid-Ops ℤq
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops ℤq
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops ℤq
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops ℤq
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops ℤq
fld-ops = +-grp-ops , *-grp-ops
postulate fld-struct : Field-Struct fld-ops
fld : Field ℤq
fld = fld-ops , fld-struct
module fld = Field fld
-- open fld using (+-grp) public
ℤ[_]+-grp : Group ℤq
ℤ[_]+-grp = fld.+-grp
ℤq+-grp : Group ℤq
ℤq+-grp = fld.+-grp
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type.Eq using (_==_; Eq?; ≡⇒==; ==⇒≡)
open import Data.Two.Base using (𝟚; ✓)
open import Relation.Binary.PropositionalEquality.Base using (_≡_; refl; ap)
open import FFI.JS using (JS[_]; _++_; return; _>>_)
open import FFI.JS.Check
using (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 Algebra.Raw
open import Algebra.Group
open import Algebra.Group.Homomorphism
open import Algebra.Field
-- TODO carry on a primality proof of q
module Crypto.JS.BigI.FiniteField (q : BigI) where
-- The constructor mk is not exported.
private
module Internals where
data ℤ[_] : Set where
mk : BigI → ℤ[_]
mk-inj : ∀ {x y : BigI} → ℤ[_].mk x ≡ mk y → x ≡ y
mk-inj refl = refl
open Internals public using (ℤ[_])
open Internals
ℤq : Set
ℤq = ℤ[_]
private
mod-q : BigI → ℤq
mod-q x = mk (mod x q)
-- There are two ways to go from BigI to ℤq: BigI▹ℤ[ q ] and mod-q
-- Use BigI▹ℤ[ q ] for untrusted input data and mod-q for internal
-- computation.
BigI▹ℤ[_] : BigI → JS[ ℤq ]
BigI▹ℤ[_] x =
-- Console.log "BigI▹ℤ[_]"
check! "below the modulus" (x <I q)
(λ _ → "Not below the modulus: q:" ++ toString q ++
" is less than x:" ++ toString x) >>
check! "positivity" (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") >>
return (mk x)
check-non-zero : ℤq → BigI
check-non-zero (mk x) = -- trace-call "check-non-zero "
check? (x >I 0I) (λ _ → "Should be non zero") x
ℤ[_]▹BigI : ℤq → BigI
ℤ[_]▹BigI (mk x) = x
0# 1# : ℤq
0# = mk 0I
1# = mk 1I
-- One could choose here to return a dummy value when 0 is given.
-- Instead we throw an exception which in some circumstances could
-- be bad if the runtime semantics is too eager.
1/_ : ℤq → ℤq
1/ x = mk (modInv (check-non-zero x) q)
_^I_ : ℤq → BigI → ℤq
mk x ^I y = mk (modPow x y q)
ℤq▹BigI = ℤ[_]▹BigI
BigI▹ℤq = BigI▹ℤ[_]
_⊗I_ : ℤq → BigI → ℤq
x ⊗I y = mod-q (multiply (ℤq▹BigI x) y)
_+_ _−_ _*_ _/_ : ℤq → ℤq → ℤq
x + y = mod-q (add (ℤq▹BigI x) (ℤq▹BigI y))
x − y = mod-q (subtract (ℤq▹BigI x) (ℤq▹BigI y))
x * y = x ⊗I ℤq▹BigI y
x / y = x * 1/ y
*-def : _*_ ≡ (λ x y → x ⊗I ℤq▹BigI y)
*-def = refl
0−_ : ℤq → ℤq
0− x = mod-q (negate (ℤq▹BigI x))
sum prod : List ℤq → ℤq
sum = foldr _+_ 0#
prod = foldr _*_ 1#
instance
ℤ[_]-Eq? : Eq? ℤq
ℤ[_]-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤq → ℤq → 𝟚
mk x ==' mk y = x == y
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
≡⇒==' {mk x} {mk y} p = ≡⇒== (mk-inj p)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
==⇒≡' {mk x} {mk y} p = ap mk (==⇒≡ p)
ℤq-Eq? = ℤ[_]-Eq?
+-mon-ops : Monoid-Ops ℤq
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops ℤq
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops ℤq
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops ℤq
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops ℤq
fld-ops = +-grp-ops , *-grp-ops
postulate fld-struct : Field-Struct fld-ops
fld : Field ℤq
fld = fld-ops , fld-struct
module fld = Field fld
-- open fld using (+-grp) public
ℤ[_]+-grp : Group ℤq
ℤ[_]+-grp = fld.+-grp
ℤq+-grp : Group ℤq
ℤq+-grp = fld.+-grp
-- -}
-- -}
-- -}
-- -}
|
Use warn-check since check is monadic now...
|
ℤq: Use warn-check since check is monadic now...
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
3be2477097d1bfe6056a5d1fbcef0fb70636e4dc
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
{-# OPTIONS --universe-polymorphism #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≡≡_ : ∀ {a} {A : Set a} {i j : A}
(i≡j₁ : i ≡ j) (i≡j₂ : i ≡ j) → i≡j₁ ≡ i≡j₂
_≡≡_ refl refl = refl
_≟≡_ : ∀ {a} {A : Set a} {i j : A} → Decidable {A = i ≡ j} _≡_
_≟≡_ refl refl = yes refl
injective : ∀ {a} {A : Set a} → InjectiveRel A _≡_
injective refl refl = refl
surjective : ∀ {a} {A : Set a} → SurjectiveRel A _≡_
surjective refl refl = refl
bijective : ∀ {a} {A : Set a} → BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
module ≡-Reasoning {a} {A : Set a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : Set a} {B : Set b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦Set⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦Set⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → Set
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦Set₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
{-# OPTIONS --universe-polymorphism #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≡≡_ : ∀ {a} {A : Set a} {i j : A}
(i≡j₁ : i ≡ j) (i≡j₂ : i ≡ j) → i≡j₁ ≡ i≡j₂
_≡≡_ refl refl = refl
_≟≡_ : ∀ {a} {A : Set a} {i j : A} → Decidable {A = i ≡ j} _≡_
_≟≡_ refl refl = yes refl
_≗₂_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} (f g : A → B → C) → Set _
f ≗₂ g = ∀ x y → f x y ≡ g x y
injective : ∀ {a} {A : Set a} → InjectiveRel A _≡_
injective refl refl = refl
surjective : ∀ {a} {A : Set a} → SurjectiveRel A _≡_
surjective refl refl = refl
bijective : ∀ {a} {A : Set a} → BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
module ≡-Reasoning {a} {A : Set a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : Set a} {B : Set b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦Set⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦Set⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → Set
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦Set₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
add ≗₂
|
add ≗₂
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
89da077b4d098d6e0939d860c48061f66c5d3369
|
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 Γ
`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 `Type : ∀{ℓ} → Value Γ (`Type ℓ)
`Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ)
`⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (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
`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 ⟧
⟦ `neut A ⟧ = `⟦ A ⟧
----------------------------------------------------------------------
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₂)
----------------------------------------------------------------------
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 ℓ))
{- Value introduction -}
`tt : Term Γ `⊤
`true `false : Term Γ `Bool
_`,_ : ∀{A B}
(a : Term Γ A) (b : Term Γ (subT B (eval a)))
→ 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)))
----------------------------------------------------------------------
{- 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 ⟧
{- Value introduction -}
eval `tt = `tt
eval `true = `true
eval `false = `false
eval (a `, b) = eval a `, 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)
----------------------------------------------------------------------
|
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 Γ
`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 `Type : ∀{ℓ} → Value Γ (`Type ℓ)
`Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ)
`⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (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
`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 ⟧
⟦ `neut A ⟧ = `⟦ A ⟧
----------------------------------------------------------------------
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 ℓ))
{- Value introduction -}
`tt : Term Γ `⊤
`true `false : Term Γ `Bool
_`,_ : ∀{A B}
(a : Term Γ A) (b : Term Γ (subT B (eval a)))
→ 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)))
`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 ⟧
{- Value introduction -}
eval `tt = `tt
eval `true = `true
eval `false = `false
eval (a `, b) = eval a `, 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 (`elimBool P pt pf b) = elimBool (eval P) (eval pt) (eval pf) (eval b)
----------------------------------------------------------------------
|
Add elimBool to operational semantics.
|
Add elimBool to operational semantics.
|
Agda
|
bsd-3-clause
|
spire/spire
|
d28029e6a6b72c675115285102a2d423b1c0b3f9
|
prg.agda
|
prg.agda
|
module prg where
open import fun-universe
open import Function
open import Data.Nat.NP hiding (_==_)
open import Data.Bool.NP hiding (_==_)
open import Data.Nat.Properties
open import Data.Bool.Properties
open import Data.Bits
open import Data.Fin using (Fin; zero; suc)
open import Data.Product
open import Data.Empty
open import Data.Vec
open import Relation.Binary.PropositionalEquality.NP
PRGFun : (k n : ℕ) → Set
PRGFun k n = Bits k → Bits n
module PRG⅁₁ {k n} (PRG : PRGFun k n) where
-- TODO: give the adv some rand
Adv : Set
Adv = Bits n → Bit
chal : Bit → Bits (n + k) → Bits n
chal b R = if b then take n R else PRG (drop n R)
prg⅁ : Adv → Bit → Bits (n + k) → Bit
prg⅁ adv b R = adv (chal b R)
-- ``Looks pseudo-random if the message is the output of the PRG some key''
brute′ : Adv
brute′ m = search {k} _∨_ ((_==_ m) ∘ PRG)
-- ``Looks random if the message is not a possible output of the PRG''
brute : Adv
brute m = search {k} _∧_ (not ∘ (_==_ m) ∘ PRG)
private
brute-PRG-K≡0b-aux : ∀ {k n} (PRG : Bits k → Bits n) K
→ PRG⅁₁.brute PRG (PRG K) ≡ 0b
brute-PRG-K≡0b-aux {zero} {n} PRG [] = cong not (==-refl (PRG []))
brute-PRG-K≡0b-aux {suc k} {n} PRG (true ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 1∷_) K
= Bool°.+-comm (search _∧_ (not ∘ _==_ (PRG (1∷ K)) ∘ PRG ∘ 0∷_)) 0b
brute-PRG-K≡0b-aux {suc k} {n} PRG (false ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 0∷_) K = refl
brute′-bound-aux : ∀ {k n} (PRG : Bits k → Bits n)
→ let open PRG⅁₁ PRG in
#⟨ brute′ ⟩ ≤ 2^ k
brute′-bound-aux {zero} PRG = ℕ≤.reflexive #⟨ PRG [] ==⟩
brute′-bound-aux {suc k} PRG = #∨ {f = λ x → search _∨_ (_==_ x ∘ PRG ∘ 0∷_)}
(brute′-bound-aux (PRG ∘ 0∷_))
(brute′-bound-aux (PRG ∘ 1∷_))
module PRG⅁ {k n} (PRG : PRGFun k n) where
open PRG⅁₁ PRG public
brute-PRG-K≡0b : ∀ K → brute (PRG K) ≡ 0b
brute-PRG-K≡0b = brute-PRG-K≡0b-aux PRG
prg⅁-brute-0 : prg⅁ brute 0b ≗ (const 0b)
prg⅁-brute-0 R = brute-PRG-K≡0b (drop n R)
brute′-bound : #⟨ brute′ ⟩ ≤ 2^ k
brute′-bound = brute′-bound-aux PRG
not∘brute′≗brute : not ∘ brute′ ≗ brute
not∘brute′≗brute x = search-comm _∨_ _∧_ not (_==_ x ∘ PRG) de-morgan
brute≗not∘brute′ : brute′ ≗ not ∘ brute
brute≗not∘brute′ x = trans (sym (not-involutive _)) (cong not (not∘brute′≗brute x))
|
module prg where
open import fun-universe
open import Function
open import Data.Nat.NP hiding (_==_)
open import Data.Bool.NP hiding (_==_)
open import Data.Nat.Properties
open import Data.Bool.Properties
open import Data.Bits
open import Data.Fin using (Fin; zero; suc)
open import Data.Product
open import Data.Empty
open import Data.Vec
open import Relation.Binary.PropositionalEquality.NP
PRGFun : (k n : ℕ) → Set
PRGFun k n = Bits k → Bits n
module PRG⅁₁ {k n} (PRG : PRGFun k n) where
-- TODO: give the adv some rand
Adv : Set
Adv = Bits n → Bit
chal : Bit → Bits (n + k) → Bits n
chal b R = if b then take n R else PRG (drop n R)
prg⅁ : Adv → Bit → Bits (n + k) → Bit
prg⅁ adv b R = adv (chal b R)
-- ``Looks pseudo-random if the message is the output of the PRG some key''
brute′ : Adv
brute′ m = search _∨_ {k} ((_==_ m) ∘ PRG)
-- ``Looks random if the message is not a possible output of the PRG''
brute : Adv
brute m = search _∧_ {k} (not ∘ (_==_ m) ∘ PRG)
private
brute-PRG-K≡0b-aux : ∀ {k n} (PRG : Bits k → Bits n) K
→ PRG⅁₁.brute PRG (PRG K) ≡ 0b
brute-PRG-K≡0b-aux {zero} {n} PRG [] = cong not (==-refl (PRG []))
brute-PRG-K≡0b-aux {suc k} {n} PRG (true ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 1∷_) K
= Bool°.+-comm (search _∧_ (not ∘ _==_ (PRG (1∷ K)) ∘ PRG ∘ 0∷_)) 0b
brute-PRG-K≡0b-aux {suc k} {n} PRG (false ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 0∷_) K = refl
brute′-bound-aux : ∀ {k n} (PRG : Bits k → Bits n)
→ let open PRG⅁₁ PRG in
#⟨ brute′ ⟩ ≤ 2^ k
brute′-bound-aux {zero} PRG = ℕ≤.reflexive #⟨ PRG [] ==⟩
brute′-bound-aux {suc k} PRG = #∨ {f = λ x → search _∨_ (_==_ x ∘ PRG ∘ 0∷_)}
(brute′-bound-aux (PRG ∘ 0∷_))
(brute′-bound-aux (PRG ∘ 1∷_))
module PRG⅁ {k n} (PRG : PRGFun k n) where
open PRG⅁₁ PRG public
brute-PRG-K≡0b : ∀ K → brute (PRG K) ≡ 0b
brute-PRG-K≡0b = brute-PRG-K≡0b-aux PRG
prg⅁-brute-0 : prg⅁ brute 0b ≗ (const 0b)
prg⅁-brute-0 R = brute-PRG-K≡0b (drop n R)
brute′-bound : #⟨ brute′ ⟩ ≤ 2^ k
brute′-bound = brute′-bound-aux PRG
not∘brute′≗brute : not ∘ brute′ ≗ brute
not∘brute′≗brute x = search-hom _∨_ _∧_ not (_==_ x ∘ PRG) de-morgan
brute≗not∘brute′ : brute′ ≗ not ∘ brute
brute≗not∘brute′ x = trans (sym (not-involutive _)) (cong not (not∘brute′≗brute x))
|
fix argument order
|
prg: fix argument order
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
f274de0be6b9f871bb446ee6381969cac1a7ad8a
|
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.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
open import Base.Denotation.Notation
-- Extension Point: Evaluation of fully applied constants.
ValStructure : Set
ValStructure = ∀ {Σ τ} → ValConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
CompStructure : Set
CompStructure = ∀ {Σ τ} → CompConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
module Structure
(⟦_⟧ValBaseTermCache : ValStructure)
(⟦_⟧CompBaseTermCache : CompStructure)
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.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- 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
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- 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?).
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- No caching, because the arguments are values, so evaluating them does not
-- produce intermediate results.
⟦ vConst c args ⟧ValTermCache ρ = ⟦ c ⟧ValBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- The only caching is done by the interpretation of the constant (because the
-- arguments are values so need no caching).
⟦_⟧CompTermCache (cConst c args) ρ = ⟦ c ⟧CompBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- 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 ρ
-- 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 ρ)
⟦_⟧TermCacheCBV : ∀ {τ Γ} → Term Γ τ → ⟦ fromCBVCtx Γ ⟧ValCtxHidCache → ⟦ cbvToCompType τ ⟧CompTypeHidCache
⟦ t ⟧TermCacheCBV = ⟦ fromCBV t ⟧CompTermCache
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Caching evaluation
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
open import Base.Denotation.Notation
-- Extension Point: Evaluation of fully applied constants.
ValStructure : Set
ValStructure = ∀ {Σ τ} → ValConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
CompStructure : Set
CompStructure = ∀ {Σ τ} → CompConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
module Structure
(⟦_⟧ValBaseTermCache : ValStructure)
(⟦_⟧CompBaseTermCache : CompStructure)
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.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- 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
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- 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?).
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- No caching, because the arguments are values, so evaluating them does not
-- produce intermediate results.
⟦ vConst c args ⟧ValTermCache ρ = ⟦ c ⟧ValBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- The only caching is done by the interpretation of the constant (because the
-- arguments are values so need no caching).
⟦_⟧CompTermCache (cConst c args) ρ = ⟦ c ⟧CompBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- 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 ρ
-- 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₂ ,′ (c₁ ,′ c₂ ,′ r₁))
-- 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 ρ)
⟦_⟧TermCacheCBV : ∀ {τ Γ} → Term Γ τ → ⟦ fromCBVCtx Γ ⟧ValCtxHidCache → ⟦ cbvToCompType τ ⟧CompTypeHidCache
⟦ t ⟧TermCacheCBV = ⟦ fromCBV t ⟧CompTermCache
|
Fix order of returned caches
|
Fix order of returned caches
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a127cbe0afe5735ec22de57b718586c54a38c33b
|
arrow.agda
|
arrow.agda
|
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 ⇒ (⇒ 7))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
|
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))
|
Fix testd
|
Fix testd
|
Agda
|
mit
|
louisswarren/hieretikz
|
3682d42d35e0734a6a4d6a4ee62cbad3965309b9
|
Parametric/Change/Term.agda
|
Parametric/Change/Term.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Terms that operate on changes (Fig. 3).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Const
open ChangeType.Structure Base ΔBase
open import Data.Product
-- Extension point 1: A term for ⊝ on base types.
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
-- Extension point 2: A term for ⊕ on base types.
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)
-- We provide: terms for ⊕ and ⊝ on arbitrary types.
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
absV 4 (λ g f x Δx → app g (x ⊕σ Δx) ⊝τ app f x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
absV 3 (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ τ {Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff _ = app₂ diff-term
apply : ∀ τ {Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply _ = app₂ apply-term
infixl 6 apply diff
syntax apply τ x Δx = Δx ⊕₍ τ ₎ x
syntax diff τ x y = x ⊝₍ τ ₎ y
infixl 6 _⊕_ _⊝_
_⊝_ : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
_⊝_ {τ} = diff τ
_⊕_ : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
_⊕_ {τ} = app₂ apply-term
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Terms that operate on changes (Fig. 3).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Const
open ChangeType.Structure Base ΔBase
open import Data.Product
-- Extension point 1: A term for ⊝ on base types.
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
-- Extension point 2: A term for ⊕ on base types.
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)
-- We provide: terms for ⊕ and ⊝ on arbitrary types.
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
absV 4 (λ g f x Δx → app g (x ⊕σ Δx) ⊝τ app f x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
absV 3 (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ τ {Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff _ = app₂ diff-term
apply : ∀ τ {Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply _ = app₂ apply-term
infixl 6 apply diff
syntax apply τ x Δx = Δx ⊕₍ τ ₎ x
syntax diff τ x y = x ⊝₍ τ ₎ y
infixl 6 _⊕_ _⊝_
_⊝_ : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
_⊝_ {τ} = diff τ
_⊕_ : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
_⊕_ {τ} = apply τ
|
Make _⊕_ more coherent with _⊝_
|
Make _⊕_ more coherent with _⊝_
|
Agda
|
mit
|
inc-lc/ilc-agda
|
5cd94161fed3259ba24dc7c2099fc439a5fdadb1
|
New/Derive.agda
|
New/Derive.agda
|
module New.Derive where
open import New.Lang
open import New.Changes
open import New.LangOps
ΔΓ : Context → Context
ΔΓ ∅ = ∅
ΔΓ (τ • Γ) = Δt τ • τ • ΔΓ Γ
Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔΓ Γ
Γ≼ΔΓ {∅} = ∅
Γ≼ΔΓ {τ • Γ} = drop Δt τ • keep τ • Γ≼ΔΓ
deriveConst : ∀ {τ} →
Const τ →
Term ∅ (Δt τ)
-- dplus = λ m dm n dn → (m + dm) + (n + dn) - (m + n) = dm + dn
deriveConst plus = abs (abs (abs (abs (app₂ (const plus) (var (that (that this))) (var this)))))
-- minus = λ m n → m - n
-- dminus = λ m dm n dn → (m + dm) - (n + dn) - (m - n) = dm - dn
deriveConst minus = abs (abs (abs (abs (app₂ (const minus) (var (that (that this))) (var this)))))
deriveConst cons = abs (abs (abs (abs (app (app (const cons) (var (that (that this)))) (var this)))))
deriveConst fst = abs (abs (app (const fst) (var this)))
deriveConst snd = abs (abs (app (const snd) (var this)))
-- deriveConst linj = abs (abs (app (const linj) (app (const linj) (var this))))
-- deriveConst rinj = abs (abs (app (const linj) (app (const rinj) (var this))))
-- deriveConst (match {t1} {t2} {t3}) =
-- -- λ s ds f df g dg →
-- abs (abs (abs (abs (abs (abs
-- -- match ds
-- (app₃ (const match) (var (that (that (that (that this)))))
-- -- λ ds₁ → match ds₁
-- (abs (app₃ (const match) (var this)
-- -- case inj₁ da → absV 1 (λ da → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- λ a → app₂ df a da
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (var (that this))))
-- -- absurd: λ b → dg b (nil b)
-- (abs (app₂ (var (that (that (that this)))) (var this) (app (onilτo t2) (var this))))))
-- -- case inj₂ db → absV 1 (λ db → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- absurd: λ a → df a (nil a)
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (app (onilτo t1) (var this))))
-- -- λ b → app₂ dg b db
-- (abs (app₂ (var (that (that (that this)))) (var this) (var (that this))))))))
-- -- recomputation branch:
-- -- λ s2 → ominus (match s2 (f ⊕ df) (g ⊕ dg)) (match s f g)
-- (abs (app₂ (ominusτo t3)
-- -- (match s2 (f ⊕ df) (g ⊕ dg))
-- (app₃ (const match)
-- (var this)
-- (app₂ (oplusτo (t1 ⇒ t3))
-- (var (that (that (that (that this)))))
-- (var (that (that (that this)))))
-- (app₂ (oplusτo (t2 ⇒ t3))
-- (var (that (that this)))
-- (var (that this))))
-- -- (match s f g)
-- (app₃ (const match)
-- (var (that (that (that (that (that (that this)))))))
-- (var (that (that (that (that this)))))
-- (var (that (that this))))))))))))
-- {-
-- derive (λ s f g → match s f g) =
-- λ s ds f df g dg →
-- case ds of
-- ch1 da →
-- case s of
-- inj1 a → df a da
-- inj2 b → {- absurd -} dg b (nil b)
-- ch2 db →
-- case s of
-- inj2 b → dg b db
-- inj1 a → {- absurd -} df a (nil a)
-- rp s2 →
-- match (f ⊕ df) (g ⊕ dg) s2 ⊝
-- match f g s
-- -}
deriveVar : ∀ {Γ τ} → Var Γ τ → Var (ΔΓ Γ) (Δt τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔΓ Γ) τ
fit = weaken Γ≼ΔΓ
derive : ∀ {Γ τ} → Term Γ τ → Term (ΔΓ Γ) (Δt τ)
derive (const c) = weaken (∅≼Γ {ΔΓ _}) (deriveConst c)
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))
open import New.LangChanges
-- Lemmas needed to reason about derivation, for any correctness proof
alternate : ∀ {Γ} → ⟦ Γ ⟧Context → eCh Γ → ⟦ ΔΓ Γ ⟧Context
alternate {∅} ∅ ∅ = ∅
alternate {τ • Γ} (v • ρ) (dv • dρ) = dv • v • alternate ρ dρ
⟦Γ≼ΔΓ⟧ : ∀ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
ρ ≡ ⟦ Γ≼ΔΓ ⟧≼ (alternate ρ dρ)
⟦Γ≼ΔΓ⟧ ∅ ∅ = refl
⟦Γ≼ΔΓ⟧ (v • ρ) (dv • dρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ ρ dρ)
fit-sound : ∀ {Γ τ} → (t : Term Γ τ) →
(ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
⟦ t ⟧Term ρ ≡ ⟦ fit t ⟧Term (alternate ρ dρ)
fit-sound t ρ dρ = trans
(cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ ρ dρ))
(sym (weaken-sound t _))
-- The change semantics is just the semantics composed with derivation!
⟦_⟧ΔVar : ∀ {Γ τ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ x ⟧ΔVar ρ dρ = ⟦ deriveVar x ⟧Var (alternate ρ dρ)
⟦_⟧ΔTerm : ∀ {Γ τ} → (t : Term Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ t ⟧ΔTerm ρ dρ = ⟦ derive t ⟧Term (alternate ρ dρ)
⟦_⟧ΔConst : ∀ {τ} (c : Const τ) → Cht τ
⟦ c ⟧ΔConst = ⟦ deriveConst c ⟧Term ∅
⟦_⟧ΔConst-rewrite : ∀ {τ Γ} (c : Const τ) (ρ : ⟦ Γ ⟧Context) dρ → ⟦_⟧ΔTerm (const c) ρ dρ ≡ ⟦ c ⟧ΔConst
⟦ c ⟧ΔConst-rewrite ρ dρ rewrite weaken-sound {Γ₁≼Γ₂ = ∅≼Γ} (deriveConst c) (alternate ρ dρ) | ⟦∅≼Γ⟧-∅ (alternate ρ dρ) = refl
|
module New.Derive where
open import New.Lang
open import New.Changes
open import New.LangOps
deriveConst : ∀ {τ} →
Const τ →
Term ∅ (Δt τ)
-- dplus = λ m dm n dn → (m + dm) + (n + dn) - (m + n) = dm + dn
deriveConst plus = abs (abs (abs (abs (app₂ (const plus) (var (that (that this))) (var this)))))
-- minus = λ m n → m - n
-- dminus = λ m dm n dn → (m + dm) - (n + dn) - (m - n) = dm - dn
deriveConst minus = abs (abs (abs (abs (app₂ (const minus) (var (that (that this))) (var this)))))
deriveConst cons = abs (abs (abs (abs (app (app (const cons) (var (that (that this)))) (var this)))))
deriveConst fst = abs (abs (app (const fst) (var this)))
deriveConst snd = abs (abs (app (const snd) (var this)))
-- deriveConst linj = abs (abs (app (const linj) (app (const linj) (var this))))
-- deriveConst rinj = abs (abs (app (const linj) (app (const rinj) (var this))))
-- deriveConst (match {t1} {t2} {t3}) =
-- -- λ s ds f df g dg →
-- abs (abs (abs (abs (abs (abs
-- -- match ds
-- (app₃ (const match) (var (that (that (that (that this)))))
-- -- λ ds₁ → match ds₁
-- (abs (app₃ (const match) (var this)
-- -- case inj₁ da → absV 1 (λ da → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- λ a → app₂ df a da
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (var (that this))))
-- -- absurd: λ b → dg b (nil b)
-- (abs (app₂ (var (that (that (that this)))) (var this) (app (onilτo t2) (var this))))))
-- -- case inj₂ db → absV 1 (λ db → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- absurd: λ a → df a (nil a)
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (app (onilτo t1) (var this))))
-- -- λ b → app₂ dg b db
-- (abs (app₂ (var (that (that (that this)))) (var this) (var (that this))))))))
-- -- recomputation branch:
-- -- λ s2 → ominus (match s2 (f ⊕ df) (g ⊕ dg)) (match s f g)
-- (abs (app₂ (ominusτo t3)
-- -- (match s2 (f ⊕ df) (g ⊕ dg))
-- (app₃ (const match)
-- (var this)
-- (app₂ (oplusτo (t1 ⇒ t3))
-- (var (that (that (that (that this)))))
-- (var (that (that (that this)))))
-- (app₂ (oplusτo (t2 ⇒ t3))
-- (var (that (that this)))
-- (var (that this))))
-- -- (match s f g)
-- (app₃ (const match)
-- (var (that (that (that (that (that (that this)))))))
-- (var (that (that (that (that this)))))
-- (var (that (that this))))))))))))
-- {-
-- derive (λ s f g → match s f g) =
-- λ s ds f df g dg →
-- case ds of
-- ch1 da →
-- case s of
-- inj1 a → df a da
-- inj2 b → {- absurd -} dg b (nil b)
-- ch2 db →
-- case s of
-- inj2 b → dg b db
-- inj1 a → {- absurd -} df a (nil a)
-- rp s2 →
-- match (f ⊕ df) (g ⊕ dg) s2 ⊝
-- match f g s
-- -}
ΔΓ : Context → Context
ΔΓ ∅ = ∅
ΔΓ (τ • Γ) = Δt τ • τ • ΔΓ Γ
Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔΓ Γ
Γ≼ΔΓ {∅} = ∅
Γ≼ΔΓ {τ • Γ} = drop Δt τ • keep τ • Γ≼ΔΓ
deriveVar : ∀ {Γ τ} → Var Γ τ → Var (ΔΓ Γ) (Δt τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔΓ Γ) τ
fit = weaken Γ≼ΔΓ
derive : ∀ {Γ τ} → Term Γ τ → Term (ΔΓ Γ) (Δt τ)
derive (const c) = weaken (∅≼Γ {ΔΓ _}) (deriveConst c)
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))
open import New.LangChanges
-- Lemmas needed to reason about derivation, for any correctness proof
alternate : ∀ {Γ} → ⟦ Γ ⟧Context → eCh Γ → ⟦ ΔΓ Γ ⟧Context
alternate {∅} ∅ ∅ = ∅
alternate {τ • Γ} (v • ρ) (dv • dρ) = dv • v • alternate ρ dρ
⟦Γ≼ΔΓ⟧ : ∀ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
ρ ≡ ⟦ Γ≼ΔΓ ⟧≼ (alternate ρ dρ)
⟦Γ≼ΔΓ⟧ ∅ ∅ = refl
⟦Γ≼ΔΓ⟧ (v • ρ) (dv • dρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ ρ dρ)
fit-sound : ∀ {Γ τ} → (t : Term Γ τ) →
(ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
⟦ t ⟧Term ρ ≡ ⟦ fit t ⟧Term (alternate ρ dρ)
fit-sound t ρ dρ = trans
(cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ ρ dρ))
(sym (weaken-sound t _))
-- The change semantics is just the semantics composed with derivation!
⟦_⟧ΔVar : ∀ {Γ τ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ x ⟧ΔVar ρ dρ = ⟦ deriveVar x ⟧Var (alternate ρ dρ)
⟦_⟧ΔTerm : ∀ {Γ τ} → (t : Term Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ t ⟧ΔTerm ρ dρ = ⟦ derive t ⟧Term (alternate ρ dρ)
⟦_⟧ΔConst : ∀ {τ} (c : Const τ) → Cht τ
⟦ c ⟧ΔConst = ⟦ deriveConst c ⟧Term ∅
⟦_⟧ΔConst-rewrite : ∀ {τ Γ} (c : Const τ) (ρ : ⟦ Γ ⟧Context) dρ → ⟦_⟧ΔTerm (const c) ρ dρ ≡ ⟦ c ⟧ΔConst
⟦ c ⟧ΔConst-rewrite ρ dρ rewrite weaken-sound {Γ₁≼Γ₂ = ∅≼Γ} (deriveConst c) (alternate ρ dρ) | ⟦∅≼Γ⟧-∅ (alternate ρ dρ) = refl
|
Reorder definitions
|
Reorder definitions
Move definitions about change contexts next to their uses.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
42316d32e1b90cea16ece1ad29627cb678610bc6
|
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
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
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 ≙₁ ≙₂
-- 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.
--
-- 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
open import Postulate.Extensionality
-- 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₂
∎
|
------------------------------------------------------------------------
-- 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
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
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 ≙₁ ≙₂
-- 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
open import Postulate.Extensionality
-- 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₂
∎
|
Clarify that change algebra operations trivially preserve d.o.e.
|
Clarify that change algebra operations trivially preserve d.o.e.
Old-commit-hash: c11fb0c1abae938f0c7ec3dacff96f112f6bbee4
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b3c29af8a0e31056b3efe8dd76723fe0d834058a
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
{-# OPTIONS --without-K #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Type hiding (★)
open import Data.One using (𝟙)
open import Data.Product using (Σ; _,_)
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
private
module Dummy {a} {A : ★ a} where
open IsEquivalence (isEquivalence {a} {A}) public hiding (refl; sym; trans)
open Dummy public
PathOver : ∀ {i j} {A : ★ i} (B : A → ★ j)
{x y : A} (p : x ≡ y) (u : B x) (v : B y) → ★ j
PathOver B refl u v = (u ≡ v)
syntax PathOver B p u v =
u ≡ v [ B ↓ p ]
-- Some definitions with the names from Agda-HoTT
-- this is only a temporary state of affairs...
module _ {a} {A : ★ a} where
idp : {x : A} → x ≡ x
idp = refl
infixr 8 _∙_ _∙'_
_∙_ _∙'_ : {x y z : A} → (x ≡ y → y ≡ z → x ≡ z)
refl ∙ q = q
q ∙' refl = q
! : {x y : A} → (x ≡ y → y ≡ x)
! refl = refl
ap↓ : ∀ {i j k} {A : ★ i} {B : A → ★ j} {C : A → ★ k}
(g : {a : A} → B a → C a) {x y : A} {p : x ≡ y}
{u : B x} {v : B y}
→ (u ≡ v [ B ↓ p ] → g u ≡ g v [ C ↓ p ])
ap↓ g {p = refl} refl = refl
ap : ∀ {i j} {A : ★ i} {B : ★ j} (f : A → B) {x y : A}
→ (x ≡ y → f x ≡ f y)
ap f p = ap↓ {A = 𝟙} f {p = refl} p
apd : ∀ {i j} {A : ★ i} {B : A → ★ j} (f : (a : A) → B a) {x y : A}
→ (p : x ≡ y) → f x ≡ f y [ B ↓ p ]
apd f refl = refl
coe : ∀ {i} {A B : ★ i} (p : A ≡ B) → A → B
coe refl x = x
coe! : ∀ {i} {A B : ★ i} (p : A ≡ B) → B → A
coe! refl x = x
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≗₂_ : ∀ {a b c} {A : ★ a} {B : ★ b} {C : ★ c} (f g : A → B → C) → ★ _
f ≗₂ g = ∀ x y → f x y ≡ g x y
module _ {a} {A : ★ a} where
injective : InjectiveRel A _≡_
injective p q = p ∙ ! q
surjective : SurjectiveRel A _≡_
surjective p q = ! p ∙ q
bijective : BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
Equalizer : ∀ {b} {B : A → ★ b} (f g : (x : A) → B x) → ★ _
Equalizer f g = Σ A (λ x → f x ≡ g x)
{- In a category theory context this type would the object 'E'
and 'proj₁' would be the morphism 'eq : E → A' such that
given any object O, and morphism 'm : O → A', there exists
a unique morphism 'u : O → E' such that 'eq ∘ u ≡ m'.
-}
module ≡-Reasoning {a} {A : ★ a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : ★ a} {B : ★ b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦★⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦★⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → ★
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦★₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
{-# OPTIONS --without-K #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Type hiding (★)
open import Data.One using (𝟙)
open import Data.Product using (Σ; _,_)
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
private
module Dummy {a} {A : ★ a} where
open IsEquivalence (isEquivalence {a} {A}) public hiding (refl; sym; trans)
open Dummy public
PathOver : ∀ {i j} {A : ★ i} (B : A → ★ j)
{x y : A} (p : x ≡ y) (u : B x) (v : B y) → ★ j
PathOver B refl u v = (u ≡ v)
syntax PathOver B p u v =
u ≡ v [ B ↓ p ]
-- Some definitions with the names from Agda-HoTT
-- this is only a temporary state of affairs...
module _ {a} {A : ★ a} where
idp : {x : A} → x ≡ x
idp = refl
infixr 8 _∙_ _∙'_
_∙_ _∙'_ : {x y z : A} → (x ≡ y → y ≡ z → x ≡ z)
refl ∙ q = q
q ∙' refl = q
! : {x y : A} → (x ≡ y → y ≡ x)
! refl = refl
ap↓ : ∀ {i j k} {A : ★ i} {B : A → ★ j} {C : A → ★ k}
(g : {a : A} → B a → C a) {x y : A} {p : x ≡ y}
{u : B x} {v : B y}
→ (u ≡ v [ B ↓ p ] → g u ≡ g v [ C ↓ p ])
ap↓ g {p = refl} refl = refl
ap : ∀ {i j} {A : ★ i} {B : ★ j} (f : A → B) {x y : A}
→ (x ≡ y → f x ≡ f y)
ap f p = ap↓ {A = 𝟙} f {p = refl} p
apd : ∀ {i j} {A : ★ i} {B : A → ★ j} (f : (a : A) → B a) {x y : A}
→ (p : x ≡ y) → f x ≡ f y [ B ↓ p ]
apd f refl = refl
coe : ∀ {i} {A B : ★ i} (p : A ≡ B) → A → B
coe refl x = x
coe! : ∀ {i} {A B : ★ i} (p : A ≡ B) → B → A
coe! refl x = x
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≗₂_ : ∀ {a b c} {A : ★ a} {B : ★ b} {C : ★ c} (f g : A → B → C) → ★ _
f ≗₂ g = ∀ x y → f x y ≡ g x y
module _ {a} {A : ★ a} where
injective : InjectiveRel A _≡_
injective p q = p ∙ ! q
surjective : SurjectiveRel A _≡_
surjective p q = ! p ∙ q
bijective : BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
Equalizer : ∀ {b} {B : A → ★ b} (f g : (x : A) → B x) → ★ _
Equalizer f g = Σ A (λ x → f x ≡ g x)
{- In a category theory context this type would the object 'E'
and 'proj₁' would be the morphism 'eq : E → A' such that
given any object O, and morphism 'm : O → A', there exists
a unique morphism 'u : O → E' such that 'eq ∘ u ≡ m'.
-}
Pullback : ∀ {b c} {B : ★ b} {C : ★ c} (f : A → C) (g : B → C) → ★ _
Pullback {B = B} f g = Σ A (λ x → Σ B (λ y → f x ≡ g y))
module ≡-Reasoning {a} {A : ★ a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : ★ a} {B : ★ b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦★⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦★⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → ★
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦★₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
Add Pullback
|
Add Pullback
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
e58fdcf2bd3f99745f653c3ee049cb1244bcb092
|
lib/Relation/Binary/NP.agda
|
lib/Relation/Binary/NP.agda
|
{-# OPTIONS --without-K #-}
{-# 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 Refl-Trans-Reasoning
{a ℓ} {A : Set a} (_≈_ : Rel A ℓ)
(refl : Reflexive _≈_)
(trans : Transitive _≈_) where
open Trans-Reasoning _≈_ trans public hiding (finally)
infix 2 _∎
_∎ : ∀ x → x ≈ x
_ ∎ = refl
infixr 2 _≈⟨-by-definition-⟩_
_≈⟨-by-definition-⟩_ : ∀ x {y : A} → x ≈ y → x ≈ y
_ ≈⟨-by-definition-⟩ x≈y = x≈y
module Equivalence-Reasoning
{a ℓ} {A : Set a} {_≈_ : Rel A ℓ}
(E : IsEquivalence _≈_) where
open IsEquivalence E
open Refl-Trans-Reasoning _≈_ refl trans public
module Preorder-Reasoning
{p₁ p₂ p₃} (P : Preorder p₁ p₂ p₃) where
open Preorder P
open Refl-Trans-Reasoning _∼_ refl trans public
renaming (_≈⟨_⟩_ to _∼⟨_⟩_; _≈⟨-by-definition-⟩_ to _∼⟨-by-definition-⟩_)
open Equivalence-Reasoning isEquivalence public renaming (_∎ to _☐)
module Setoid-Reasoning {a ℓ} (s : Setoid a ℓ) where
open Equivalence-Reasoning (Setoid.isEquivalence s) public
|
{-# OPTIONS --without-K #-}
{-# 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 Refl-Trans-Reasoning
{a ℓ} {A : Set a} (_≈_ : Rel A ℓ)
(refl : Reflexive _≈_)
(trans : Transitive _≈_) where
open Trans-Reasoning _≈_ trans public hiding (finally)
infix 2 _∎
_∎ : ∀ x → x ≈ x
_ ∎ = refl
infixr 2 _≈⟨by-definition⟩_
_≈⟨by-definition⟩_ : ∀ x {y : A} → x ≈ y → x ≈ y
_ ≈⟨by-definition⟩ x≈y = x≈y
module Equivalence-Reasoning
{a ℓ} {A : Set a} {_≈_ : Rel A ℓ}
(E : IsEquivalence _≈_) where
open IsEquivalence E
open Refl-Trans-Reasoning _≈_ refl trans public
module Preorder-Reasoning
{p₁ p₂ p₃} (P : Preorder p₁ p₂ p₃) where
open Preorder P
open Refl-Trans-Reasoning _∼_ refl trans public
renaming (_≈⟨_⟩_ to _∼⟨_⟩_; _≈⟨by-definition⟩_ to _∼⟨by-definition⟩_)
open Equivalence-Reasoning isEquivalence public renaming (_∎ to _☐)
module Setoid-Reasoning {a ℓ} (s : Setoid a ℓ) where
open Equivalence-Reasoning (Setoid.isEquivalence s) public
|
Rename by-definition combinator for eq-reasoning
|
Rename by-definition combinator for eq-reasoning
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
a0cb2129d6925e76e7c69250fbe510b3a711b245
|
Parametric/Denotation/Evaluation.agda
|
Parametric/Denotation/Evaluation.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Standard evaluation (Def. 3.3 and Fig. 4i)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
-- Extension Point: Evaluation of fully applied constants.
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
-- We provide: Evaluation of arbitrary terms.
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
-- this is what we'd like to write.
-- unfortunately termination checker complains.
--
-- ⟦ terms ⟧Terms ρ = map-IVT (λ t → ⟦ t ⟧Term ρ) terms
--
-- so we do explicit pattern matching instead.
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ₁≼Γ₂ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound : ∀ {Γ₁ Γ₂ Σ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(terms : Terms Γ₁ Σ) (ρ : ⟦ Γ₂ ⟧) →
⟦ weaken-terms Γ₁≼Γ₂ terms ⟧Terms ρ ≡ ⟦ terms ⟧Terms (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound ∅ ρ = refl
weaken-terms-sound (t • terms) ρ =
cong₂ _•_ (weaken-sound t ρ) (weaken-terms-sound terms ρ)
weaken-sound {Γ₁≼Γ₂ = Γ₁≼Γ₂} (var x) ρ = weaken-var-sound Γ₁≼Γ₂ x ρ
weaken-sound (app s t) ρ = weaken-sound s ρ ⟨$⟩ weaken-sound t ρ
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound {Γ₁} {Γ₂} {Γ₁≼Γ₂ = Γ₁≼Γ₂} (const {Σ} {τ} c args) ρ =
cong ⟦ c ⟧Const (weaken-terms-sound args ρ)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Standard evaluation (Def. 3.3 and Fig. 4i)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
-- Extension Point: Evaluation of fully applied constants.
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
-- We provide: Evaluation of arbitrary terms.
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
-- this is what we'd like to write.
-- unfortunately termination checker complains.
--
-- ⟦ terms ⟧Terms ρ = map (λ t → ⟦ t ⟧Term ρ) terms
--
-- so we do explicit pattern matching instead.
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ₁≼Γ₂ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound : ∀ {Γ₁ Γ₂ Σ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(terms : Terms Γ₁ Σ) (ρ : ⟦ Γ₂ ⟧) →
⟦ weaken-terms Γ₁≼Γ₂ terms ⟧Terms ρ ≡ ⟦ terms ⟧Terms (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound ∅ ρ = refl
weaken-terms-sound (t • terms) ρ =
cong₂ _•_ (weaken-sound t ρ) (weaken-terms-sound terms ρ)
weaken-sound {Γ₁≼Γ₂ = Γ₁≼Γ₂} (var x) ρ = weaken-var-sound Γ₁≼Γ₂ x ρ
weaken-sound (app s t) ρ = weaken-sound s ρ ⟨$⟩ weaken-sound t ρ
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound {Γ₁} {Γ₂} {Γ₁≼Γ₂ = Γ₁≼Γ₂} (const {Σ} {τ} c args) ρ =
cong ⟦ c ⟧Const (weaken-terms-sound args ρ)
|
Correct code in comment
|
Correct code in comment
map-IVT does not exist currently (not sure it ever did, I did not
investigate); what we want and does not work is simply map. Hence, fix
the comment to clarify it.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
caab0f0a9ebc53432d697d8dfef61fb6cae09b67
|
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
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.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; 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
≡⟨ 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
-- 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
|
------------------------------------------------------------------------
-- 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
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.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR (≙-setoid {x = x}) public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
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
-- 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
|
Prepare for splitting
|
Equivalence: Prepare for splitting
The file has become too big.
Splitting the anonymous modules requires more work out of type inference
and introduces one type inference failure, which is fixed in this commmit.
Old-commit-hash: 32f42f80b60cd3dd45e879d7a367b4fe933bb9d8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
26feb31d8f07b57839d5ad020a366816d0ae21fb
|
Parametric/Denotation/Evaluation.agda
|
Parametric/Denotation/Evaluation.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Syntax.Context Type
open import Base.Denotation.Environment Type ⟦_⟧Type
open import Base.Denotation.Notation
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure(⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Syntax.Context Type
open import Base.Denotation.Environment Type ⟦_⟧Type
open import Base.Denotation.Notation
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
|
insert missing space (cosmetic)
|
insert missing space (cosmetic)
Old-commit-hash: 4d559497f68fabe2674c6d523508f715c78256c8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
321249014d14436b6f3f84e01fdeb73f0d5faf7b
|
crypto-agda.agda
|
crypto-agda.agda
|
module crypto-agda where
import Cipher.ElGamal.Generic
import Composition.Forkable
import Composition.Horizontal
import Composition.Vertical
import Crypto.Schemes
--import FiniteField.FinImplem
import FunUniverse.Agda
import FunUniverse.BinTree
import FunUniverse.Bits
import FunUniverse.Category
import FunUniverse.Category.Op
import FunUniverse.Circuit
import FunUniverse.Const
import FunUniverse.Core
import FunUniverse.Cost
import FunUniverse.Data
import FunUniverse.Defaults.FirstPart
--import FunUniverse.ExnArrow
import FunUniverse.Fin
import FunUniverse.Fin.Op
import FunUniverse.Fin.Op.Abstract
--import FunUniverse.FlatFunsProd
--import FunUniverse.Interface.Bits
import FunUniverse.Interface.Two
import FunUniverse.Interface.Vec
import FunUniverse.Inverse
--import FunUniverse.Loop
import FunUniverse.Nand
import FunUniverse.Nand.Function
import FunUniverse.Nand.Properties
import FunUniverse.README
import FunUniverse.Rewiring.Linear
--import FunUniverse.State
import FunUniverse.Syntax
import FunUniverse.Types
import Game.DDH
import Game.EntropySmoothing
import Game.EntropySmoothing.WithKey
import Game.IND-CPA
import Game.Transformation.InternalExternal
import Language.Simple.Abstract
import Language.Simple.Interface
import Language.Simple.Two.Mux
import Language.Simple.Two.Mux.Normalise
import Language.Simple.Two.Nand
import README
import Solver.AddMax
import Solver.Linear
--import Solver.Linear.Examples
import Solver.Linear.Parser
import Solver.Linear.Syntax
import adder
import alea.cpo
import bijection-syntax.Bijection-Fin
import bijection-syntax.Bijection
import bijection-syntax.README
--import circuits.bytecode
import circuits.circuit
--import elgamal
--import sha1
|
module crypto-agda where
import Attack.Compression
import Attack.Reencryption
import Attack.Reencryption.OneBitMessage
import Cipher.ElGamal.Generic
import Cipher.ElGamal.Homomorphic
import Composition.Forkable
import Composition.Horizontal
import Composition.Vertical
import Crypto.Schemes
import FiniteField.FinImplem
import FunUniverse.Agda
import FunUniverse.BinTree
import FunUniverse.Bits
import FunUniverse.Category
import FunUniverse.Category.Op
import FunUniverse.Circuit
import FunUniverse.Const
import FunUniverse.Core
import FunUniverse.Cost
import FunUniverse.Data
import FunUniverse.Defaults.FirstPart
import FunUniverse.ExnArrow
import FunUniverse.Fin
import FunUniverse.Fin.Op
import FunUniverse.Fin.Op.Abstract
import FunUniverse.FlatFunsProd
import FunUniverse.Interface.Bits
import FunUniverse.Interface.Two
import FunUniverse.Interface.Vec
import FunUniverse.Inverse
import FunUniverse.Loop
import FunUniverse.Nand
import FunUniverse.Nand.Function
import FunUniverse.Nand.Properties
import FunUniverse.README
import FunUniverse.Rewiring.Linear
import FunUniverse.State
import FunUniverse.Syntax
import FunUniverse.Types
import Game.DDH
import Game.EntropySmoothing
import Game.EntropySmoothing.WithKey
import Game.IND-CCA
import Game.IND-CCA2-dagger
import Game.IND-CCA2
import Game.IND-CPA
import Game.Transformation.CCA-CPA
import Game.Transformation.CCA2-CCA
import Game.Transformation.CCA2-CCA2d
import Game.Transformation.CCA2d-CCA2
import Game.Transformation.InternalExternal
import Language.Simple.Abstract
import Language.Simple.Interface
import Language.Simple.Two.Mux
import Language.Simple.Two.Mux.Normalise
import Language.Simple.Two.Nand
import README
import Solver.AddMax
import Solver.Linear
import Solver.Linear.Examples
import Solver.Linear.Parser
import Solver.Linear.Syntax
import adder
import alea.cpo
import bijection-syntax.Bijection-Fin
import bijection-syntax.Bijection
import bijection-syntax.README
import circuits.bytecode
import circuits.circuit
import elgamal
import sha1
|
update crypto-agda.agda
|
update crypto-agda.agda
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
bc0f697e345522fad65844eed07cba984158e3b7
|
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 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₍ Γ ₎
|
------------------------------------------------------------------------
-- 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 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₍ Γ ₎
-}
|
Comment out bug-triggering code
|
Parametric.Change.Validity: Comment out bug-triggering code
This code is exported and used elsewhere :-(
|
Agda
|
mit
|
inc-lc/ilc-agda
|
ac25a046e34905a5fd630275f7f19cf585a2f9ab
|
ZK/JSChecker.agda
|
ZK/JSChecker.agda
|
{-# OPTIONS --without-K #-}
module ZK.JSChecker where
open import Function using (id; _∘′_; case_of_)
open import Data.Bool.Base using (Bool; true; false; _∧_)
open import Data.List.Base using (List; []; _∷_; and; foldr)
open import Data.String.Base using (String)
open import FFI.JS
open import FFI.JS.Check
-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)
-- open import Control.Process.Type
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
import FFI.JS.BigI as BigI
open BigI using (BigI; bigI)
import Crypto.JS.BigI.ZqZp as ZqZp
-- TODO dynamise me
primality-test-probability-bound : Number
primality-test-probability-bound = readNumber "10"
-- TODO: check if this is large enough
min-bits-q : Number
min-bits-q = 256N
min-bits-p : Number
min-bits-p = 2048N
-- TODO check with undefined
bigdec : JSValue → BigI
bigdec v = bigI (castString v) "10"
-- TODO bug (undefined)!
record ZK-chaum-pedersen-pok-elgamal-rnd {--(ℤq ℤp★ : Set)--} : Set where
field
m c s : BigI {--ℤq--}
g p q y α β A B : BigI --ℤp★
zk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} → JS!
zk-check-chaum-pedersen-pok-elgamal-rnd! pf
= trace "g=" g λ _ →
trace "p=" I.p λ _ →
trace "q=" I.q λ _ →
trace "y=" y λ _ →
trace "α=" α λ _ →
trace "β=" β λ _ →
trace "m=" m λ _ →
trace "M=" M λ _ →
trace "A=" A λ _ →
trace "B=" B λ _ →
trace "c=" c λ _ →
trace "s=" s λ _ →
checks!
>> check! "g^s==A·α^c" ((g ^ s) == (A · (α ^ c))) (λ _ → "")
>> check! "y^s==B·(β/M)^c" ((y ^ s) == (B · ((β ·/ M) ^ c))) (λ _ → "")
module ZK-check-chaum-pedersen-pok-elgamal-rnd where
module I = ZK-chaum-pedersen-pok-elgamal-rnd pf
params = record
{ primality-test-probability-bound = primality-test-probability-bound
; min-bits-q = min-bits-q
; min-bits-p = min-bits-p
; qI = I.q
; pI = I.p
; gI = I.g
}
open module [ℤq]ℤp★ = ZqZp params
A = BigI▹ℤp★ I.A
B = BigI▹ℤp★ I.B
α = BigI▹ℤp★ I.α
β = BigI▹ℤp★ I.β
y = BigI▹ℤp★ I.y
s = BigI▹ℤq I.s
c = BigI▹ℤq I.c
m = BigI▹ℤq I.m
M = g ^ m
zk-check! : JSValue → JS!
zk-check! arg =
check! "type of statement" (typ === fromString cpt)
(λ _ → "Expected type of statement: " ++ cpt ++ " not " ++ toString typ)
>> zk-check-chaum-pedersen-pok-elgamal-rnd! pok
module Zk-check where
cpt = "chaum-pedersen-pok-elgamal-rnd"
stm = arg ·« "statement" »
typ = stm ·« "type" »
dat = stm ·« "data" »
g = bigdec (dat ·« "g" »)
p = bigdec (dat ·« "p" »)
q = bigdec (dat ·« "q" »)
y = bigdec (dat ·« "y" »)
m = bigdec (dat ·« "plain" »)
enc = dat ·« "enc" »
α = bigdec (enc ·« "alpha" »)
β = bigdec (enc ·« "beta" »)
prf = arg ·« "proof" »
com = prf ·« "commitment" »
A = bigdec (com ·« "A" »)
B = bigdec (com ·« "B" »)
c = bigdec (prf ·« "challenge" »)
s = bigdec (prf ·« "response" »)
pok = record { g = g; p = p; q = q; y = y; α = α; β = β; A = A; B = B; c = c; s = s; m = m }
{-
srv : URI → JSProc
srv d =
recv d λ q →
send d (fromBool (zk-check q))
end
-}
-- Working around Agda.Primitive.lsuc being undefined
-- case_of_ : {A : Set} {B : Set} → A → (A → B) → B
-- case x of f = f x
main : JS!
main =
Process.argv !₁ λ args →
case JSArray▹ListString args of λ {
(_node ∷ _run ∷ _test ∷ args') →
case args' of λ {
[] →
Console.log "usage: No arguments"
{- server "127.0.0.1" "1337" srv !₁ λ uri →
Console.log (showURI uri)
-}
; (arg ∷ args'') →
case args'' of λ {
[] →
Console.log ("Reading input file: " ++ arg) >>
FS.readFile arg nullJS !₂ λ err dat →
check! "reading input file" (is-null err)
(λ _ → "readFile error: " ++ toString err) >>
zk-check! (JSON-parse (toString dat))
; _ →
Console.log "usage: Too many arguments"
}
}
; _ →
Console.log "usage"
}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
module ZK.JSChecker where
open import Function using (id; _∘′_; case_of_)
open import Data.Bool.Base using (Bool; true; false; _∧_)
open import Data.List.Base using (List; []; _∷_; and; foldr)
open import Data.String.Base using (String)
open import FFI.JS
open import FFI.JS.Check
-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)
-- open import Control.Process.Type
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
import FFI.JS.BigI as BigI
open BigI using (BigI; bigI)
import Crypto.JS.BigI.ZqZp as ZqZp
-- TODO dynamise me
primality-test-probability-bound : Number
primality-test-probability-bound = readNumber "10"
-- TODO: check if this is large enough
min-bits-q : Number
min-bits-q = 256N
min-bits-p : Number
min-bits-p = 2048N
bigdec : JSValue → BigI
bigdec v = bigI (castString v) "10"
-- TODO bug (undefined)!
record ZK-chaum-pedersen-pok-elgamal-rnd {--(ℤq ℤp★ : Set)--} : Set where
field
m c s : BigI {--ℤq--}
g p q y α β A B : BigI --ℤp★
zk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} → JS!
zk-check-chaum-pedersen-pok-elgamal-rnd! pf
= trace "g=" g λ _ →
trace "p=" I.p λ _ →
trace "q=" I.q λ _ →
trace "y=" y λ _ →
trace "α=" α λ _ →
trace "β=" β λ _ →
trace "m=" m λ _ →
trace "M=" M λ _ →
trace "A=" A λ _ →
trace "B=" B λ _ →
trace "c=" c λ _ →
trace "s=" s λ _ →
checks!
>> check! "g^s==A·α^c" ((g ^ s) == (A · (α ^ c))) (λ _ → "")
>> check! "y^s==B·(β/M)^c" ((y ^ s) == (B · ((β ·/ M) ^ c))) (λ _ → "")
module ZK-check-chaum-pedersen-pok-elgamal-rnd where
module I = ZK-chaum-pedersen-pok-elgamal-rnd pf
params = record
{ primality-test-probability-bound = primality-test-probability-bound
; min-bits-q = min-bits-q
; min-bits-p = min-bits-p
; qI = I.q
; pI = I.p
; gI = I.g
}
open module [ℤq]ℤp★ = ZqZp params
A = BigI▹ℤp★ I.A
B = BigI▹ℤp★ I.B
α = BigI▹ℤp★ I.α
β = BigI▹ℤp★ I.β
y = BigI▹ℤp★ I.y
s = BigI▹ℤq I.s
c = BigI▹ℤq I.c
m = BigI▹ℤq I.m
M = g ^ m
zk-check! : JSValue → JS!
zk-check! arg =
check! "type of statement" (typ === fromString cpt)
(λ _ → "Expected type of statement: " ++ cpt ++ " not " ++ toString typ)
>> zk-check-chaum-pedersen-pok-elgamal-rnd! pok
module Zk-check where
cpt = "chaum-pedersen-pok-elgamal-rnd"
stm = arg ·« "statement" »
typ = stm ·« "type" »
dat = stm ·« "data" »
g = bigdec (dat ·« "g" »)
p = bigdec (dat ·« "p" »)
q = bigdec (dat ·« "q" »)
y = bigdec (dat ·« "y" »)
m = bigdec (dat ·« "plain" »)
enc = dat ·« "enc" »
α = bigdec (enc ·« "alpha" »)
β = bigdec (enc ·« "beta" »)
prf = arg ·« "proof" »
com = prf ·« "commitment" »
A = bigdec (com ·« "A" »)
B = bigdec (com ·« "B" »)
c = bigdec (prf ·« "challenge" »)
s = bigdec (prf ·« "response" »)
pok = record { g = g; p = p; q = q; y = y; α = α; β = β; A = A; B = B; c = c; s = s; m = m }
{-
srv : URI → JSProc
srv d =
recv d λ q →
send d (fromBool (zk-check q))
end
-}
-- Working around Agda.Primitive.lsuc being undefined
-- case_of_ : {A : Set} {B : Set} → A → (A → B) → B
-- case x of f = f x
main : JS!
main =
Process.argv !₁ λ args →
case JSArray▹ListString args of λ {
(_node ∷ _run ∷ _test ∷ args') →
case args' of λ {
[] →
Console.log "usage: No arguments"
{- server "127.0.0.1" "1337" srv !₁ λ uri →
Console.log (showURI uri)
-}
; (arg ∷ args'') →
case args'' of λ {
[] →
Console.log ("Reading input file: " ++ arg) >>
FS.readFile arg nullJS !₂ λ err dat →
check! "reading input file" (is-null err)
(λ _ → "readFile error: " ++ toString err) >>
zk-check! (JSON-parse (toString dat))
; _ →
Console.log "usage: Too many arguments"
}
}
; _ →
Console.log "usage"
}
-- -}
-- -}
-- -}
-- -}
|
Remove no longer relevant TODO
|
Remove no longer relevant TODO
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
a6dfe1f553ba79964ad12503ce1a9095fe79ffe2
|
arrow.agda
|
arrow.agda
|
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
|
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 ⇒ (⇒ 7))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
|
Add test
|
Add test
|
Agda
|
mit
|
louisswarren/hieretikz
|
51432b971511d6e3b28904b1d33fc55d32888906
|
lib/Function/Extensionality.agda
|
lib/Function/Extensionality.agda
|
{-# OPTIONS --without-K #-}
module Function.Extensionality where
open import Function.NP
open import Relation.Binary.PropositionalEquality.NP -- renaming (subst to tr)
happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}
→ f ≡ g → (x : A) → f x ≡ g x
happly p x = ap (λ f → f x) p
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₁
happly-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → happly (λ= fg) ≡ fg
λ=-happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → λ= (happly α) ≡ α
-- 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)
!-α-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → ! α ≡ λ= (!_ ∘ happly α)
!-α-λ= refl = ! λ=-happly refl
!-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → ! (λ= fg) ≡ λ= (!_ ∘ fg)
!-λ= fg = !-α-λ= (λ= fg) ∙ ap λ= (λ= (λ x → ap !_ (happly (happly-λ= fg) x)))
|
{-# OPTIONS --without-K #-}
module Function.Extensionality where
open import Function.NP
open import Relation.Binary.PropositionalEquality.NP -- renaming (subst to tr)
happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}
→ f ≡ g → (x : A) → f x ≡ g x
happly p x = ap (λ f → f x) p
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₁
happly-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → happly (λ= fg) ≡ fg
λ=-happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → λ= (happly α) ≡ α
-- 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)
!-α-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → ! α ≡ λ= (!_ ∘ happly α)
!-α-λ= refl = ! λ=-happly refl
!-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → ! (λ= fg) ≡ λ= (!_ ∘ fg)
!-λ= fg = !-α-λ= (λ= fg) ∙ ap λ= (λ= (λ x → ap !_ (happly (happly-λ= fg) x)))
module _ {a b}{A : Set a}{B : A → Set b}{f₀ f₁ : (x : A) → B x}{{fe : FunExt}} where
λ=ⁱ : (f= : ∀ {x} → f₀ x ≡ f₁ x) → f₀ ≡ f₁
λ=ⁱ f= = λ= λ x → f= {x}
|
add a version with implicit arguments
|
FunExt: add a version with implicit arguments
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
702fcd37b6983eaf128ea29fca185751c2444c69
|
lib/Relation/Binary/Permutation.agda
|
lib/Relation/Binary/Permutation.agda
|
--TODO {-# OPTIONS --without-K #-}
module Relation.Binary.Permutation where
open import Level
open import Data.Product.NP
open import Data.Zero
open import Data.One
open import Data.Sum
open import Data.List
open import Relation.Nullary
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_;_≢_)
private
_⇔_ : ∀ {a b ℓ₁ ℓ₂} {A : Set a} {B : Set b} (R₁ : REL A B ℓ₁) (R₂ : REL A B ℓ₂) → Set _
R₁ ⇔ R₂ = R₁ ⇒ R₂ × R₂ ⇒ R₁
infix 2 _⇔_
⟨_,_⟩∈_ : ∀ {ℓ a b} {A : Set a} {B : Set b} (x : A) (y : B) → REL A B ℓ → Set ℓ
⟨_,_⟩∈_ x y R = R x y
data _[_↔_] {a} {A : Set a} (R : Rel A a) (x y : A) : Rel A a where
here₁ : ∀ {j}
(yRj : ⟨ y , j ⟩∈ R )
→ ----------------------
⟨ x , j ⟩∈ R [ x ↔ y ]
here₂ : ∀ {j}
(xRj : ⟨ x , j ⟩∈ R )
→ ----------------------
⟨ y , j ⟩∈ R [ x ↔ y ]
there : ∀ {i j}
(x≢i : x ≢ i )
(y≢i : y ≢ i )
(iRj : ⟨ i , j ⟩∈ R )
→ -----------------------
⟨ i , j ⟩∈ R [ x ↔ y ]
module PermComm {a} {A : Set a} {R : Rel A a} where
⟹ : ∀ {x y} → R [ x ↔ y ] ⇒ R [ y ↔ x ]
⟹ (here₁ yRj) = here₂ yRj
⟹ (here₂ xRj) = here₁ xRj
⟹ (there x≢i x≢j iRj) = there x≢j x≢i iRj
lem : ∀ {x y} → R [ x ↔ y ] ⇔ R [ y ↔ x ]
lem = (λ {_} → ⟹) , (λ {_} → ⟹)
module PermIdem {a} {A : Set a} (_≟_ : Decidable {A = A} _≡_) {x y : A} {R : Rel A a} where
⇐ : ∀ {x y} → R [ x ↔ y ] [ x ↔ y ] ⇒ R
⇐ (here₁ (here₁ yRj)) = yRj
⇐ (here₁ (here₂ xRj)) = xRj
⇐ (here₁ (there _ y≢y _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (here₂ (here₁ yRj)) = yRj
⇐ (here₂ (here₂ xRj)) = xRj
⇐ (here₂ (there x≢x _ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there x≢x _ (here₁ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there _ y≢y (here₂ _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (there _ _ (there _ _ iRj)) = iRj
⟹ : R ⇒ R [ x ↔ y ] [ x ↔ y ]
⟹ {i} {j} R
with x ≟ i | y ≟ i
... | yes x≡i | _ rewrite x≡i = here₁ (here₂ R)
... | _ | yes y≡i rewrite y≡i = here₂ (here₁ R)
... | no x≢i | no y≢i = there x≢i y≢i (there x≢i y≢i R)
lem : R ⇔ R [ x ↔ y ] [ x ↔ y ]
lem = (λ {_} → ⟹) , λ {_} → ⇐
Permutation : ∀ {a} → Set a → Set a
Permutation A = List (A × A)
permRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a → Rel A a
permRel π R = foldr (λ p r → r [ fst p ↔ snd p ]) R π
toRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a
toRel π = permRel π (λ _ _ → Lift 𝟙)
{-
_⟨$⟩₁_ : ∀ {a} {A : Set a} → Permutation A → A → Maybe A
[] ⟨$⟩₁ y = nothing
(x ∷ xs) ⟨$⟩₁ y = if ⌊ x ≟A y ⌋ then ? else
-}
|
--TODO {-# OPTIONS --without-K #-}
module Relation.Binary.Permutation where
open import Level
open import Data.Product.NP
open import Data.Zero
open import Data.One
open import Data.Sum
open import Data.List
open import Relation.Nullary
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_;_≢_)
private
_⇔_ : ∀ {a b ℓ₁ ℓ₂} {A : Set a} {B : Set b} (R₁ : REL A B ℓ₁) (R₂ : REL A B ℓ₂) → Set _
R₁ ⇔ R₂ = R₁ ⇒ R₂ × R₂ ⇒ R₁
infix 2 _⇔_
⟨_,_⟩∈_ : ∀ {ℓ a b} {A : Set a} {B : Set b} (x : A) (y : B) → REL A B ℓ → Set ℓ
⟨_,_⟩∈_ x y R = R x y
data _[_↔_] {a} {A : Set a} (R : Rel A a) (x y : A) : Rel A a where
here₁ : ∀ {j}
(yRj : ⟨ y , j ⟩∈ R )
→ ----------------------
⟨ x , j ⟩∈ R [ x ↔ y ]
here₂ : ∀ {j}
(xRj : ⟨ x , j ⟩∈ R )
→ ----------------------
⟨ y , j ⟩∈ R [ x ↔ y ]
there : ∀ {i j}
(x≢i : x ≢ i )
(y≢i : y ≢ i )
(iRj : ⟨ i , j ⟩∈ R )
→ -----------------------
⟨ i , j ⟩∈ R [ x ↔ y ]
module PermComm {a} {A : Set a} {R : Rel A a} where
⟹ : ∀ {x y} → R [ x ↔ y ] ⇒ R [ y ↔ x ]
⟹ (here₁ yRj) = here₂ yRj
⟹ (here₂ xRj) = here₁ xRj
⟹ (there x≢i x≢j iRj) = there x≢j x≢i iRj
lem : ∀ {x y} → R [ x ↔ y ] ⇔ R [ y ↔ x ]
lem = (λ {_} → ⟹) , (λ {_} → ⟹)
module PermIdem {a} {A : Set a} (_≟_ : Decidable {A = A} _≡_) {R : Rel A a} where
⇐ : ∀ {x y} → R [ x ↔ y ] [ x ↔ y ] ⇒ R
⇐ (here₁ (here₁ yRj)) = yRj
⇐ (here₁ (here₂ xRj)) = xRj
⇐ (here₁ (there _ y≢y _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (here₂ (here₁ yRj)) = yRj
⇐ (here₂ (here₂ xRj)) = xRj
⇐ (here₂ (there x≢x _ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there x≢x _ (here₁ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there _ y≢y (here₂ _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (there _ _ (there _ _ iRj)) = iRj
⟹ : ∀ {x y} → R ⇒ R [ x ↔ y ] [ x ↔ y ]
⟹ {x} {y} {i} {j} R
with x ≟ i | y ≟ i
... | yes x≡i | _ rewrite x≡i = here₁ (here₂ R)
... | _ | yes y≡i rewrite y≡i = here₂ (here₁ R)
... | no x≢i | no y≢i = there x≢i y≢i (there x≢i y≢i R)
lem : ∀ {x y} → R ⇔ R [ x ↔ y ] [ x ↔ y ]
lem = (λ {_} → ⟹) , λ {_} → ⇐
Permutation : ∀ {a} → Set a → Set a
Permutation A = List (A × A)
permRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a → Rel A a
permRel π R = foldr (λ p r → r [ fst p ↔ snd p ]) R π
toRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a
toRel π = permRel π (λ _ _ → Lift 𝟙)
{-
_⟨$⟩₁_ : ∀ {a} {A : Set a} → Permutation A → A → Maybe A
[] ⟨$⟩₁ y = nothing
(x ∷ xs) ⟨$⟩₁ y = if ⌊ x ≟A y ⌋ then ? else
-}
|
Fix in Relation.Binary.Permutation
|
Fix in Relation.Binary.Permutation
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
3822d110c060a75d914e9d0e4fc105f00e99980a
|
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 with which all base-type values
-- form a group
union : ∀ {ι Γ} →
Atlas-term Γ (base ι) → Atlas-term Γ (base ι) →
Atlas-term Γ (base ι)
union {Bool} s t = app (app (const xor) 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
-- 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
|
fix bug in `union` by postulating conjunction
|
Atlas: fix bug in `union` by postulating conjunction
Old-commit-hash: 69ed9a83b2b162709e5dfcf541f9d0bf18c7e970
|
Agda
|
mit
|
inc-lc/ilc-agda
|
080a08a480530a0bfbe7ee42fb486407caea4a5e
|
Parametric/Change/Equivalence.agda
|
Parametric/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Equivalence
{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
open import Level
open import Data.Unit
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
module _ {ℓ} {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
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 ≙₁ ≙₂
-- 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 true for functions
-- using changes parametrically, for derivatives and function changes, and
-- for functions using only the interface to changes (including the fact
-- that function changes are functions). Stating the general result, though,
-- seems hard, we should rather have lemmas proving that certain classes of
-- functions respect this equivalence.
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Parametric.Change.Equivalence where
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
module _ {ℓ} {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
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 ≙₁ ≙₂
-- 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 true for functions
-- using changes parametrically, for derivatives and function changes, and
-- for functions using only the interface to changes (including the fact
-- that function changes are functions). Stating the general result, though,
-- seems hard, we should rather have lemmas proving that certain classes of
-- functions respect this equivalence.
|
Remove extra imports
|
Remove extra imports
Old-commit-hash: 3a9775f6afd97585f6b425390b5431b43807b03e
|
Agda
|
mit
|
inc-lc/ilc-agda
|
3409935502fb668aa913b86adee0b90b81efb211
|
arrow.agda
|
arrow.agda
|
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))
|
data Bool : Set where
true : Bool
false : Bool
_∨_ : Bool → Bool → Bool
true ∨ _ = true
false ∨ b = b
_∧_ : Bool → Bool → Bool
false ∧ _ = false
true ∧ b = b
----------------------------------------
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
infixr 5 _∷_
[_] : {A : Set} → A → List A
[ x ] = x ∷ []
any : {A : Set} → (A → Bool) → List A → Bool
any _ [] = false
any f (x ∷ xs) = (f x) ∨ (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) ∧ (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))
|
Use standard syntax
|
Use standard syntax
|
Agda
|
mit
|
louisswarren/hieretikz
|
20a1abbafdf38b144be117be78c688be2786fbee
|
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} (mk fg) (mk 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
|
f7a0824e23aa553c512f0fbff1879941ee29f6d0
|
lib/FFI/JS.agda
|
lib/FFI/JS.agda
|
module FFI.JS where
open import Data.Empty public renaming (⊥ to 𝟘)
open import Data.Unit.Base public renaming (⊤ to 𝟙)
open import Data.Char.Base public using (Char)
open import Data.String.Base public using (String)
open import Data.Bool.Base public using (Bool; true; false)
open import Data.List.Base using (List; []; _∷_)
open import Data.Product using (_×_) renaming (proj₁ to fst; proj₂ to snd)
open import Function using (id; _∘_)
open import Control.Process.Type
{-# COMPILED_JS Bool function (x,v) { return ((x)? v["true"]() : v["false"]()); } #-}
{-# COMPILED_JS true true #-}
{-# COMPILED_JS false false #-}
postulate
Number : Set
JSArray : Set → Set
JSObject : Set
JSValue : Set
postulate readNumber : String → Number
{-# COMPILED_JS readNumber Number #-}
postulate 0N : Number
{-# COMPILED_JS 0N 0 #-}
postulate 1N : Number
{-# COMPILED_JS 1N 1 #-}
postulate 2N : Number
{-# COMPILED_JS 2N 2 #-}
postulate 4N : Number
{-# COMPILED_JS 4N 4 #-}
postulate 8N : Number
{-# COMPILED_JS 8N 8 #-}
postulate 16N : Number
{-# COMPILED_JS 16N 16 #-}
postulate 32N : Number
{-# COMPILED_JS 32N 32 #-}
postulate 64N : Number
{-# COMPILED_JS 64N 64 #-}
postulate 128N : Number
{-# COMPILED_JS 128N 128 #-}
postulate 256N : Number
{-# COMPILED_JS 256N 256 #-}
postulate 512N : Number
{-# COMPILED_JS 512N 512 #-}
postulate 1024N : Number
{-# COMPILED_JS 1024N 1024 #-}
postulate 2048N : Number
{-# COMPILED_JS 2048N 2048 #-}
postulate 4096N : Number
{-# COMPILED_JS 4096N 4096 #-}
postulate _+_ : Number → Number → Number
{-# COMPILED_JS _+_ function(x) { return function(y) { return x + y; }; } #-}
postulate _−_ : Number → Number → Number
{-# COMPILED_JS _−_ function(x) { return function(y) { return x - y; }; } #-}
postulate _*_ : Number → Number → Number
{-# COMPILED_JS _*_ function(x) { return function(y) { return x * y; }; } #-}
postulate _/_ : Number → Number → Number
{-# COMPILED_JS _/_ function(x) { return function(y) { return x / y; }; } #-}
infixr 5 _++_
postulate _++_ : String → String → String
{-# COMPILED_JS _++_ function(x) { return function(y) { return x + y; }; } #-}
postulate _+JS_ : JSValue → JSValue → JSValue
{-# COMPILED_JS _+JS_ function(x) { return function(y) { return x + y; }; } #-}
postulate _≤JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≤JS_ function(x) { return function(y) { return x <= y; }; } #-}
postulate _<JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _<JS_ function(x) { return function(y) { return x < y; }; } #-}
postulate _>JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _>JS_ function(x) { return function(y) { return x > y; }; } #-}
postulate _≥JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≥JS_ function(x) { return function(y) { return x >= y; }; } #-}
postulate _===_ : JSValue → JSValue → Bool
{-# COMPILED_JS _===_ function(x) { return function(y) { return x === y; }; } #-}
postulate reverse : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS reverse function(ty) { return function(x) { return x.reverse(); }; } #-}
postulate sort : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS sort function(ty) { return function(x) { return x.sort(); }; } #-}
postulate split : (sep target : String) → JSArray String
{-# COMPILED_JS split function(sep) { return function(target) { return target.split(sep); }; } #-}
postulate join : (sep : String)(target : JSArray String) → String
{-# COMPILED_JS join function(sep) { return function(target) { return target.join(sep); }; } #-}
postulate fromList : {A B : Set}(xs : List A)(fromElt : A → B) → JSArray B
{-# COMPILED_JS fromList require("libagda").fromList #-}
postulate length : String → Number
{-# COMPILED_JS length function(s) { return s.length; } #-}
postulate JSON-stringify : JSValue → String
{-# COMPILED_JS JSON-stringify JSON.stringify #-}
postulate JSON-parse : String → JSValue
{-# COMPILED_JS JSON-parse JSON.parse #-}
postulate toString : JSValue → String
{-# COMPILED_JS toString function(x) { return x.toString(); } #-}
postulate fromBool : Bool → JSValue
{-# COMPILED_JS fromBool function(x) { return x; } #-}
postulate fromString : String → JSValue
{-# COMPILED_JS fromString function(x) { return x; } #-}
postulate fromChar : Char → JSValue
{-# COMPILED_JS fromChar String #-}
postulate Char▹String : Char → String
{-# COMPILED_JS Char▹String String #-}
postulate fromNumber : Number → JSValue
{-# COMPILED_JS fromNumber function(x) { return x; } #-}
postulate fromJSArray : {A : Set} → JSArray A → JSValue
{-# COMPILED_JS fromJSArray function(ty) { return function(x) { return x; }; } #-}
postulate fromJSObject : JSObject → JSValue
{-# COMPILED_JS fromJSObject function(x) { return x; } #-}
postulate objectFromList : {A : Set}(xs : List A)(fromKey : A → String)(fromVal : A → JSValue) → JSObject
{-# COMPILED_JS objectFromList require("libagda").objectFromList #-}
postulate decodeJSArray : {A B : Set}(arr : JSArray A)(fromElt : Number → A → B) → List B
{-# COMPILED_JS decodeJSArray require("libagda").decodeJSArray #-}
postulate castNumber : JSValue → Number
{-# COMPILED_JS castNumber Number #-}
postulate castString : JSValue → String
{-# COMPILED_JS castString String #-}
-- TODO dyn check of length 1?
postulate castChar : JSValue → Char
{-# COMPILED_JS castChar String #-}
-- TODO dyn check of length 1?
postulate String▹Char : String → Char
{-# COMPILED_JS String▹Char String #-}
-- TODO dyn check?
postulate castJSArray : JSValue → JSArray JSValue
{-# COMPILED_JS castJSArray function(x) { return x; } #-}
-- TODO dyn check?
postulate castJSObject : JSValue → JSObject
{-# COMPILED_JS castJSObject function(x) { return x; } #-}
postulate nullJS : JSValue
{-# COMPILED_JS nullJS null #-}
postulate _·[_] : JSValue → JSValue → JSValue
{-# COMPILED_JS _·[_] require("libagda").readProp #-}
postulate _Array[_] : {A : Set} → JSArray A → Number → A
{-# COMPILED_JS _Array[_] function(ty) { return require("libagda").readProp; } #-}
postulate onJSArray : {A : Set} (f : JSArray JSValue → A) → JSValue → A
{-# COMPILED_JS onJSArray require("libagda").onJSArray #-}
postulate onString : {A : Set} (f : String → A) → JSValue → A
{-# COMPILED_JS onString require("libagda").onString #-}
-- Writes 'msg' and 'inp' to the console and then returns `f inp`
postulate trace : {A B : Set}(msg : String)(inp : A)(f : A → B) → B
{-# COMPILED_JS trace require("libagda").trace #-}
postulate throw : {A : Set} → String → A → A
{-# COMPILED_JS throw require("libagda").throw #-}
data Value : Set₀ where
array : List Value → Value
object : List (String × Value) → Value
string : String → Value
number : Number → Value
bool : Bool → Value
null : Value
Object = List (String × JSValue)
postulate fromValue : Value → JSValue
{-# COMPILED_JS fromValue require("libagda").fromValue #-}
-- TODO we could make it a COMPILED type and remove the encoding by using JSValue as the internal repr.
data ValueView : Set₀ where
array : JSArray JSValue → ValueView
object : JSObject → ValueView
string : String → ValueView
number : Number → ValueView
bool : Bool → ValueView
null : ValueView
-- TODO not yet tested
postulate viewJSValue : JSValue → ValueView
{-# COMPILED_JS viewJSValue require("libagda").viewJSValue #-}
Bool▹String : Bool → String
Bool▹String true = "true"
Bool▹String false = "false"
List▹String : List Char → String
List▹String xs = join "" (fromList xs Char▹String)
String▹List : String → List Char
String▹List s = decodeJSArray (split "" s) (λ _ → String▹Char)
Number▹String : Number → String
Number▹String = castString ∘ fromNumber
JSArray▹ListString : {A : Set} → JSArray A → List A
JSArray▹ListString a = decodeJSArray a (λ _ → id)
fromObject : Object → JSObject
fromObject o = objectFromList o fst snd
_≤Char_ : Char → Char → Bool
x ≤Char y = fromChar x ≤JS fromChar y
_<Char_ : Char → Char → Bool
x <Char y = fromChar x <JS fromChar y
_>Char_ : Char → Char → Bool
x >Char y = fromChar x >JS fromChar y
_≥Char_ : Char → Char → Bool
x ≥Char y = fromChar x ≥JS fromChar y
_≤String_ : String → String → Bool
x ≤String y = fromString x ≤JS fromString y
_<String_ : String → String → Bool
x <String y = fromString x <JS fromString y
_>String_ : String → String → Bool
x >String y = fromString x >JS fromString y
_≥String_ : String → String → Bool
x ≥String y = fromString x ≥JS fromString y
_≤Number_ : Number → Number → Bool
x ≤Number y = fromNumber x ≤JS fromNumber y
_<Number_ : Number → Number → Bool
x <Number y = fromNumber x <JS fromNumber y
_>Number_ : Number → Number → Bool
x >Number y = fromNumber x >JS fromNumber y
_≥Number_ : Number → Number → Bool
x ≥Number y = fromNumber x ≥JS fromNumber y
_·«_» : JSValue → String → JSValue
v ·« s » = v ·[ fromString s ]
_·«_»A : JSValue → String → JSArray JSValue
v ·« s »A = castJSArray (v ·« s »)
trace-call : {A B : Set} → String → (A → B) → A → B
trace-call s f x = trace s (f x) id
postulate JSCmd : Set → Set
Callback1 : Set → Set
Callback1 A = JSCmd ((A → 𝟘) → 𝟘)
Callback0 : Set
Callback0 = Callback1 𝟙
Callback2 : Set → Set → Set
Callback2 A B = JSCmd ((A → B → 𝟘) → 𝟘)
postulate assert : Bool → Callback0
{-# COMPILED_JS assert require("libagda").assert #-}
check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
check true errmsg x = x
check false errmsg x = throw (errmsg _) x
warn-check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
warn-check true errmsg x = x
warn-check false errmsg x = trace ("Warning: " ++ errmsg _) x id
infixr 0 _>>_ _!₁_ _!₂_
data JS! : Set₁ where
end : JS!
_!₁_ : {A : Set}(cmd : Callback1 A)(cb : A → JS!) → JS!
_!₂_ : {A B : Set}(cmd : JSCmd ((A → B → 𝟘) → 𝟘))(cb : A → B → JS!) → JS!
_>>_ : Callback0 → JS! → JS!
cmd >> cont = cmd !₁ λ _ → cont
-- -}
-- -}
-- -}
-- -}
-- -}
|
module FFI.JS where
open import Data.Empty public renaming (⊥ to 𝟘)
open import Data.Unit.Base public renaming (⊤ to 𝟙)
open import Data.Char.Base public using (Char)
open import Data.String.Base public using (String)
open import Data.Bool.Base public using (Bool; true; false)
open import Data.List.Base using (List; []; _∷_)
open import Data.Product using (_×_) renaming (proj₁ to fst; proj₂ to snd)
open import Function using (id; _∘_)
open import Control.Process.Type
{-# COMPILED_JS Bool function (x,v) { return ((x)? v["true"]() : v["false"]()); } #-}
{-# COMPILED_JS true true #-}
{-# COMPILED_JS false false #-}
postulate
Number : Set
JSArray : Set → Set
JSObject : Set
JSValue : Set
postulate readNumber : String → Number
{-# COMPILED_JS readNumber Number #-}
postulate 0N : Number
{-# COMPILED_JS 0N 0 #-}
postulate 1N : Number
{-# COMPILED_JS 1N 1 #-}
postulate 2N : Number
{-# COMPILED_JS 2N 2 #-}
postulate 4N : Number
{-# COMPILED_JS 4N 4 #-}
postulate 8N : Number
{-# COMPILED_JS 8N 8 #-}
postulate 16N : Number
{-# COMPILED_JS 16N 16 #-}
postulate 32N : Number
{-# COMPILED_JS 32N 32 #-}
postulate 64N : Number
{-# COMPILED_JS 64N 64 #-}
postulate 128N : Number
{-# COMPILED_JS 128N 128 #-}
postulate 256N : Number
{-# COMPILED_JS 256N 256 #-}
postulate 512N : Number
{-# COMPILED_JS 512N 512 #-}
postulate 1024N : Number
{-# COMPILED_JS 1024N 1024 #-}
postulate 2048N : Number
{-# COMPILED_JS 2048N 2048 #-}
postulate 4096N : Number
{-# COMPILED_JS 4096N 4096 #-}
postulate _+_ : Number → Number → Number
{-# COMPILED_JS _+_ function(x) { return function(y) { return x + y; }; } #-}
postulate _−_ : Number → Number → Number
{-# COMPILED_JS _−_ function(x) { return function(y) { return x - y; }; } #-}
postulate _*_ : Number → Number → Number
{-# COMPILED_JS _*_ function(x) { return function(y) { return x * y; }; } #-}
postulate _/_ : Number → Number → Number
{-# COMPILED_JS _/_ function(x) { return function(y) { return x / y; }; } #-}
infixr 5 _++_
postulate _++_ : String → String → String
{-# COMPILED_JS _++_ function(x) { return function(y) { return x + y; }; } #-}
postulate _+JS_ : JSValue → JSValue → JSValue
{-# COMPILED_JS _+JS_ function(x) { return function(y) { return x + y; }; } #-}
postulate _≤JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≤JS_ function(x) { return function(y) { return x <= y; }; } #-}
postulate _<JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _<JS_ function(x) { return function(y) { return x < y; }; } #-}
postulate _>JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _>JS_ function(x) { return function(y) { return x > y; }; } #-}
postulate _≥JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≥JS_ function(x) { return function(y) { return x >= y; }; } #-}
postulate _===_ : JSValue → JSValue → Bool
{-# COMPILED_JS _===_ function(x) { return function(y) { return x === y; }; } #-}
postulate reverse : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS reverse function(ty) { return function(x) { return x.reverse(); }; } #-}
postulate sort : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS sort function(ty) { return function(x) { return x.sort(); }; } #-}
postulate split : (sep target : String) → JSArray String
{-# COMPILED_JS split function(sep) { return function(target) { return target.split(sep); }; } #-}
postulate join : (sep : String)(target : JSArray String) → String
{-# COMPILED_JS join function(sep) { return function(target) { return target.join(sep); }; } #-}
postulate fromList : {A B : Set}(xs : List A)(fromElt : A → B) → JSArray B
{-# COMPILED_JS fromList require("libagda").fromList #-}
postulate length : String → Number
{-# COMPILED_JS length function(s) { return s.length; } #-}
postulate JSON-stringify : JSValue → String
{-# COMPILED_JS JSON-stringify JSON.stringify #-}
postulate JSON-parse : String → JSValue
{-# COMPILED_JS JSON-parse JSON.parse #-}
postulate toString : JSValue → String
{-# COMPILED_JS toString function(x) { return x.toString(); } #-}
postulate fromBool : Bool → JSValue
{-# COMPILED_JS fromBool function(x) { return x; } #-}
postulate fromString : String → JSValue
{-# COMPILED_JS fromString function(x) { return x; } #-}
postulate fromChar : Char → JSValue
{-# COMPILED_JS fromChar String #-}
postulate Char▹String : Char → String
{-# COMPILED_JS Char▹String String #-}
postulate fromNumber : Number → JSValue
{-# COMPILED_JS fromNumber function(x) { return x; } #-}
postulate fromJSArray : {A : Set} → JSArray A → JSValue
{-# COMPILED_JS fromJSArray function(ty) { return function(x) { return x; }; } #-}
postulate fromJSObject : JSObject → JSValue
{-# COMPILED_JS fromJSObject function(x) { return x; } #-}
postulate objectFromList : {A : Set}(xs : List A)(fromKey : A → String)(fromVal : A → JSValue) → JSObject
{-# COMPILED_JS objectFromList require("libagda").objectFromList #-}
postulate decodeJSArray : {A B : Set}(arr : JSArray A)(fromElt : Number → A → B) → List B
{-# COMPILED_JS decodeJSArray require("libagda").decodeJSArray #-}
postulate castNumber : JSValue → Number
{-# COMPILED_JS castNumber Number #-}
postulate castString : JSValue → String
{-# COMPILED_JS castString String #-}
-- TODO dyn check of length 1?
postulate castChar : JSValue → Char
{-# COMPILED_JS castChar String #-}
-- TODO dyn check of length 1?
postulate String▹Char : String → Char
{-# COMPILED_JS String▹Char String #-}
-- TODO dyn check?
postulate castJSArray : JSValue → JSArray JSValue
{-# COMPILED_JS castJSArray function(x) { return x; } #-}
-- TODO dyn check?
postulate castJSObject : JSValue → JSObject
{-# COMPILED_JS castJSObject function(x) { return x; } #-}
postulate nullJS : JSValue
{-# COMPILED_JS nullJS null #-}
postulate _·[_] : JSValue → JSValue → JSValue
{-# COMPILED_JS _·[_] require("libagda").readProp #-}
postulate _Array[_] : {A : Set} → JSArray A → Number → A
{-# COMPILED_JS _Array[_] function(ty) { return require("libagda").readProp; } #-}
postulate onJSArray : {A : Set} (f : JSArray JSValue → A) → JSValue → A
{-# COMPILED_JS onJSArray require("libagda").onJSArray #-}
postulate onString : {A : Set} (f : String → A) → JSValue → A
{-# COMPILED_JS onString require("libagda").onString #-}
-- Writes 'msg' and 'inp' to the console and then returns `f inp`
postulate trace : {A B : Set}(msg : String)(inp : A)(f : A → B) → B
{-# COMPILED_JS trace require("libagda").trace #-}
postulate throw : {A : Set} → String → A → A
{-# COMPILED_JS throw require("libagda").throw #-}
postulate is-null : JSValue → Bool
{-# COMPILED_JS is-null function(x) { return (x == null); } #-}
data Value : Set₀ where
array : List Value → Value
object : List (String × Value) → Value
string : String → Value
number : Number → Value
bool : Bool → Value
null : Value
Object = List (String × JSValue)
postulate fromValue : Value → JSValue
{-# COMPILED_JS fromValue require("libagda").fromValue #-}
-- TODO we could make it a COMPILED type and remove the encoding by using JSValue as the internal repr.
data ValueView : Set₀ where
array : JSArray JSValue → ValueView
object : JSObject → ValueView
string : String → ValueView
number : Number → ValueView
bool : Bool → ValueView
null : ValueView
-- TODO not yet tested
postulate viewJSValue : JSValue → ValueView
{-# COMPILED_JS viewJSValue require("libagda").viewJSValue #-}
Bool▹String : Bool → String
Bool▹String true = "true"
Bool▹String false = "false"
List▹String : List Char → String
List▹String xs = join "" (fromList xs Char▹String)
String▹List : String → List Char
String▹List s = decodeJSArray (split "" s) (λ _ → String▹Char)
Number▹String : Number → String
Number▹String = castString ∘ fromNumber
JSArray▹ListString : {A : Set} → JSArray A → List A
JSArray▹ListString a = decodeJSArray a (λ _ → id)
fromObject : Object → JSObject
fromObject o = objectFromList o fst snd
_≤Char_ : Char → Char → Bool
x ≤Char y = fromChar x ≤JS fromChar y
_<Char_ : Char → Char → Bool
x <Char y = fromChar x <JS fromChar y
_>Char_ : Char → Char → Bool
x >Char y = fromChar x >JS fromChar y
_≥Char_ : Char → Char → Bool
x ≥Char y = fromChar x ≥JS fromChar y
_≤String_ : String → String → Bool
x ≤String y = fromString x ≤JS fromString y
_<String_ : String → String → Bool
x <String y = fromString x <JS fromString y
_>String_ : String → String → Bool
x >String y = fromString x >JS fromString y
_≥String_ : String → String → Bool
x ≥String y = fromString x ≥JS fromString y
_≤Number_ : Number → Number → Bool
x ≤Number y = fromNumber x ≤JS fromNumber y
_<Number_ : Number → Number → Bool
x <Number y = fromNumber x <JS fromNumber y
_>Number_ : Number → Number → Bool
x >Number y = fromNumber x >JS fromNumber y
_≥Number_ : Number → Number → Bool
x ≥Number y = fromNumber x ≥JS fromNumber y
_·«_» : JSValue → String → JSValue
v ·« s » = v ·[ fromString s ]
_·«_»A : JSValue → String → JSArray JSValue
v ·« s »A = castJSArray (v ·« s »)
trace-call : {A B : Set} → String → (A → B) → A → B
trace-call s f x = trace s (f x) id
postulate JSCmd : Set → Set
Callback1 : Set → Set
Callback1 A = JSCmd ((A → 𝟘) → 𝟘)
Callback0 : Set
Callback0 = Callback1 𝟙
Callback2 : Set → Set → Set
Callback2 A B = JSCmd ((A → B → 𝟘) → 𝟘)
postulate assert : Bool → Callback0
{-# COMPILED_JS assert require("libagda").assert #-}
check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
check true errmsg x = x
check false errmsg x = throw (errmsg _) x
warn-check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
warn-check true errmsg x = x
warn-check false errmsg x = trace ("Warning: " ++ errmsg _) x id
infixr 0 _>>_ _!₁_ _!₂_
data JS! : Set₁ where
end : JS!
_!₁_ : {A : Set}(cmd : Callback1 A)(cb : A → JS!) → JS!
_!₂_ : {A B : Set}(cmd : JSCmd ((A → B → 𝟘) → 𝟘))(cb : A → B → JS!) → JS!
_>>_ : Callback0 → JS! → JS!
cmd >> cont = cmd !₁ λ _ → cont
-- -}
-- -}
-- -}
-- -}
-- -}
|
add is-null
|
JS: add is-null
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-libjs
|
7b2c31fcd7415cecc448653df3d386001cbebf1b
|
Syntax/Term/Plotkin.agda
|
Syntax/Term/Plotkin.agda
|
import Syntax.Type.Plotkin as Type
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : 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 import Syntax.Context {Type}
data Term
(Γ : Context) :
(τ : Type) → Set
where
const : ∀ {τ} → (c : C τ) → Term Γ τ
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ))
(t : Term Γ σ) → Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ)
-- 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 Γ₂ τ
weaken Γ₁≼Γ₂ (const c) = const c
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
-- 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)
|
import Syntax.Type.Plotkin as Type
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : 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 import Syntax.Context {Type}
data Term
(Γ : Context) :
(τ : Type) → Set
where
const : ∀ {τ} →
(c : C τ) →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- 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 Γ₂ τ
weaken Γ₁≼Γ₂ (const c) = const c
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
-- 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)
|
Put constructor arguments on different lines.
|
Put constructor arguments on different lines.
Old-commit-hash: 02f9c59c59ddef7050c2c035457c98ecb00464dc
|
Agda
|
mit
|
inc-lc/ilc-agda
|
fbbd955ba2c82259764f220c075ee5ab09f0a3c1
|
models/Desc.agda
|
models/Desc.agda
|
{-# OPTIONS --type-in-type
--no-termination-check
--no-positivity-check #-}
module Desc where
--********************************************
-- Prelude
--********************************************
-- Some preliminary stuffs, to avoid relying on the stdlib
--****************
-- Sigma and friends
--****************
data Sigma (A : Set) (B : A -> Set) : Set where
_,_ : (x : A) (y : B x) -> Sigma A B
_*_ : (A B : Set) -> Set
A * B = Sigma A \_ -> B
fst : {A : Set}{B : A -> Set} -> Sigma A B -> A
fst (a , _) = a
snd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)
snd (a , b) = b
data Zero : Set where
record One : Set where
--****************
-- Sum and friends
--****************
data _+_ (A B : Set) : Set where
l : A -> A + B
r : B -> A + B
--********************************************
-- Desc code
--********************************************
-- Inductive types are implemented as a Universe. Hence, in this
-- section, we implement their code.
-- We can read this code as follow (see Conor's "Ornamental
-- algebras"): a description in |Desc| is a program to read one node
-- of the described tree.
data Desc : Set where
Arg : (X : Set) -> (X -> Desc) -> Desc
-- Read a field in |X|; continue, given its value
-- Often, |X| is an |EnumT|, hence allowing to choose a constructor
-- among a finite set of constructors
Ind : (H : Set) -> Desc -> Desc
-- Read a field in H; read a recursive subnode given the field,
-- continue regarless of the subnode
-- Often, |H| is |1|, hence |Ind| simplifies to |Inf : Desc -> Desc|,
-- meaning: read a recursive subnode and continue regardless
Done : Desc
-- Stop reading
--********************************************
-- Desc decoder
--********************************************
-- Provided the type of the recursive subnodes |R|, we decode a
-- description as a record describing the node.
[|_|]_ : Desc -> Set -> Set
[| Arg A D |] R = Sigma A (\ a -> [| D a |] R)
[| Ind H D |] R = (H -> R) * [| D |] R
[| Done |] R = One
--********************************************
-- Functions on codes
--********************************************
-- Saying that a "predicate" |p| holds everywhere in |v| amounts to
-- write something of the following type:
Everywhere : (d : Desc) (D : Set) (bp : D -> Set) (V : [| d |] D) -> Set
Everywhere (Arg A f) d p v = Everywhere (f (fst v)) d p (snd v)
-- It must hold for this constructor
Everywhere (Ind H x) d p v = ((y : H) -> p (fst v y)) * Everywhere x d p (snd v)
-- It must hold for the subtrees
Everywhere Done _ _ _ = _
-- It trivially holds at endpoints
-- Then, we can build terms that inhabits this type. That is, a
-- function that takes a "predicate" |bp| and makes it hold everywhere
-- in the data-structure. It is the equivalent of a "map", but in a
-- dependently-typed setting.
everywhere : (d : Desc) (D : Set) (bp : D -> Set) ->
((y : D) -> bp y) -> (v : [| d |] D) ->
Everywhere d D bp v
everywhere (Arg a f) d bp p v = everywhere (f (fst v)) d bp p (snd v)
-- It holds everywhere on this constructor
everywhere (Ind H x) d bp p v = (\y -> p (fst v y)) , everywhere x d bp p (snd v)
-- It holds here, and down in the recursive subtrees
everywhere Done _ _ _ _ = One
-- Nothing needs to be done on endpoints
-- Looking at the decoder, a natural thing to do is to define its
-- fixpoint |Mu D|, hence instantiating |R| with |Mu D| itself.
data Mu (D : Desc) : Set where
Con : [| D |] (Mu D) -> Mu D
-- Using the "map" defined by |everywhere|, we can implement a "fold"
-- over the |Mu| fixpoint:
foldDesc : (D : Desc) (bp : Mu D -> Set) ->
((x : [| D |] (Mu D)) -> Everywhere D (Mu D) bp x -> bp (Con x)) ->
(v : Mu D) ->
bp v
foldDesc D bp p (Con v) = p v (everywhere D (Mu D) bp (\x -> foldDesc D bp p x) v)
--********************************************
-- Nat
--********************************************
data NatConst : Set where
ZE : NatConst
SU : NatConst
natc : NatConst -> Desc
natc ZE = Done
natc SU = Ind One Done
natd : Desc
natd = Arg NatConst natc
nat : Set
nat = Mu natd
zero : nat
zero = Con ( ZE , _ )
suc : nat -> nat
suc n = Con ( SU , ( (\_ -> n) , _ ) )
two : nat
two = suc (suc zero)
four : nat
four = suc (suc (suc (suc zero)))
sum : nat ->
((x : Sigma NatConst (\ a -> [| natc a |] Mu (Arg NatConst natc))) ->
Everywhere (natc (fst x)) (Mu (Arg NatConst natc)) (\ _ -> Mu (Arg NatConst natc)) (snd x) ->
Mu (Arg NatConst natc))
sum n2 (ZE , _) p = n2
sum n2 (SU , f) p = suc ( fst p _)
plus : nat -> nat -> nat
plus n1 n2 = foldDesc natd (\_ -> nat) (sum n2) n1
x : nat
x = plus two two
|
{-# OPTIONS --type-in-type #-}
module Desc where
--********************************************
-- Prelude
--********************************************
-- Some preliminary stuffs, to avoid relying on the stdlib
--****************
-- Sigma and friends
--****************
data Sigma (A : Set) (B : A -> Set) : Set where
_,_ : (x : A) (y : B x) -> Sigma A B
_*_ : (A : Set)(B : Set) -> Set
A * B = Sigma A \_ -> B
fst : {A : Set}{B : A -> Set} -> Sigma A B -> A
fst (a , _) = a
snd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)
snd (a , b) = b
data Zero : Set where
data Unit : Set where
Void : Unit
--****************
-- Sum and friends
--****************
data _+_ (A : Set)(B : Set) : Set where
l : A -> A + B
r : B -> A + B
--****************
-- Equality
--****************
data _==_ {A : Set}(x : A) : A -> Set where
refl : x == x
cong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y
cong f refl = refl
cong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} ->
x == y -> z == t -> f x z == f y t
cong2 f refl refl = refl
postulate
reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g
--********************************************
-- Desc code
--********************************************
data Desc : Set where
id : Desc
const : Set -> Desc
prod : Desc -> Desc -> Desc
sigma : (S : Set) -> (S -> Desc) -> Desc
pi : (S : Set) -> (S -> Desc) -> Desc
--********************************************
-- Desc interpretation
--********************************************
[|_|]_ : Desc -> Set -> Set
[| id |] Z = Z
[| const X |] Z = X
[| prod D D' |] Z = [| D |] Z * [| D' |] Z
[| sigma S T |] Z = Sigma S (\s -> [| T s |] Z)
[| pi S T |] Z = (s : S) -> [| T s |] Z
--********************************************
-- Fixpoint construction
--********************************************
data Mu (D : Desc) : Set where
con : [| D |] (Mu D) -> Mu D
--********************************************
-- Predicate: All
--********************************************
All : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set
All id X P x = P x
All (const Z) X P x = Z
All (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')
All (sigma S T) X P (a , b) = All (T a) X P b
All (pi S T) X P f = (s : S) -> All (T s) X P (f s)
all : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x
all id X P R x = R x
all (const Z) X P R z = z
all (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'
all (sigma S T) X P R (a , b) = all (T a) X P R b
all (pi S T) X P R f = \ s -> all (T s) X P R (f s)
--********************************************
-- Elimination principle: induction
--********************************************
{-
induction : (D : Desc)
(P : Mu D -> Set) ->
( (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x)) ->
(v : Mu D) ->
P v
induction D P ms (con xs) = ms xs (all D (Mu D) P (\x -> induction D P ms x) xs)
-}
module Elim (D : Desc)
(P : Mu D -> Set)
(ms : (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x))
where
mutual
induction : (x : Mu D) -> P x
induction (con xs) = ms xs (hyps D xs)
hyps : (D' : Desc)
(xs : [| D' |] (Mu D)) ->
All D' (Mu D) P xs
hyps id x = induction x
hyps (const Z) z = z
hyps (prod D D') (d , d') = hyps D d , hyps D' d'
hyps (sigma S T) (a , b) = hyps (T a) b
hyps (pi S T) f = \s -> hyps (T s) (f s)
induction : (D : Desc)
(P : Mu D -> Set) ->
( (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x)) ->
(v : Mu D) ->
P v
induction D P ms x = Elim.induction D P ms x
--********************************************
-- Examples
--********************************************
--****************
-- Nat
--****************
data NatConst : Set where
Ze : NatConst
Suc : NatConst
natCases : NatConst -> Desc
natCases Ze = const Unit
natCases Suc = id
NatD : Desc
NatD = sigma NatConst natCases
Nat : Set
Nat = Mu NatD
ze : Nat
ze = con (Ze , Void)
suc : Nat -> Nat
suc n = con (Suc , n)
--****************
-- List
--****************
data ListConst : Set where
Nil : ListConst
Cons : ListConst
listCases : Set -> ListConst -> Desc
listCases X Nil = const Unit
listCases X Cons = sigma X (\_ -> id)
ListD : Set -> Desc
ListD X = sigma ListConst (listCases X)
List : Set -> Set
List X = Mu (ListD X)
nil : {X : Set} -> List X
nil = con ( Nil , Void )
cons : {X : Set} -> X -> List X -> List X
cons x t = con ( Cons , ( x , t ))
--****************
-- Tree
--****************
data TreeConst : Set where
Leaf : TreeConst
Node : TreeConst
treeCases : Set -> TreeConst -> Desc
treeCases X Leaf = const Unit
treeCases X Node = sigma X (\_ -> prod id id)
TreeD : Set -> Desc
TreeD X = sigma TreeConst (treeCases X)
Tree : Set -> Set
Tree X = Mu (TreeD X)
leaf : {X : Set} -> Tree X
leaf = con (Leaf , Void)
node : {X : Set} -> X -> Tree X -> Tree X -> Tree X
node x le ri = con (Node , (x , (le , ri)))
--********************************************
-- Finite sets
--********************************************
EnumU : Set
EnumU = Nat
nilE : EnumU
nilE = ze
consE : EnumU -> EnumU
consE e = suc e
{-
data EnumU : Set where
nilE : EnumU
consE : EnumU -> EnumU
-}
data EnumT : (e : EnumU) -> Set where
EZe : {e : EnumU} -> EnumT (consE e)
ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)
casesSpi : (xs : [| NatD |] Nat) ->
All NatD Nat (\e -> (P' : EnumT e -> Set) -> Set) xs ->
(P' : EnumT (con xs) -> Set) -> Set
casesSpi (Ze , Void) hs P' = Unit
casesSpi (Suc , n) hs P' = P' EZe * hs (\e -> P' (ESu e))
spi : (e : EnumU)(P : EnumT e -> Set) -> Set
spi e P = induction NatD (\e -> (P : EnumT e -> Set) -> Set) casesSpi e P
{-
spi : (e : EnumU)(P : EnumT e -> Set) -> Set
spi nilE P = Unit
spi (consE e) P = P EZe * spi e (\e -> P (ESu e))
-}
casesSwitch : (xs : [| NatD |] Nat) ->
All NatD Nat (\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->
(P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'
casesSwitch (Ze , Void) hs P' b' ()
casesSwitch (Suc , n) hs P' b' EZe = fst b'
casesSwitch (Suc , n) hs P' b' (ESu e') = hs (\e -> P' (ESu e)) (snd b') e'
switch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x
switch e P b x = induction NatD (\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x
{-
switch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x
switch nilE P b ()
switch (consE e) P b EZe = fst b
switch (consE e) P b (ESu n) = switch e (\e -> P (ESu e)) (snd b) n
-}
--********************************************
-- Tagged description
--********************************************
TagDesc : Set
TagDesc = Sigma EnumU (\e -> spi e (\_ -> Desc))
toDesc : TagDesc -> Desc
toDesc (B , F) = sigma (EnumT B) (\e -> switch B (\_ -> Desc) F e)
--********************************************
-- Catamorphism
--********************************************
cata : (D : Desc)
(T : Set) ->
([| D |] T -> T) ->
(Mu D) -> T
cata D T phi x = induction D (\_ -> T) (\x ms -> phi (replace D T x ms )) x
where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\_ -> T) xs) -> [| D' |] T
replace id T x y = y
replace (const Z) T z z' = z'
replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'
replace (sigma A B) T (a , b) t = a , replace (B a) T b t
replace (pi A B) T f t = \s -> replace (B s) T (f s) (t s)
--********************************************
-- Free monad construction
--********************************************
_**_ : TagDesc -> (X : Set) -> TagDesc
(e , D) ** X = consE e , (const X , D)
--********************************************
-- Substitution
--********************************************
apply : (D : TagDesc)(X Y : Set) ->
(X -> Mu (toDesc (D ** Y))) ->
[| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->
Mu (toDesc (D ** Y))
apply (E , B) X Y sig (EZe , x) = sig x
apply (E , B) X Y sig (ESu n , t) = con (ESu n , t)
subst : (D : TagDesc)(X Y : Set) ->
Mu (toDesc (D ** X)) ->
(X -> Mu (toDesc (D ** Y))) ->
Mu (toDesc (D ** Y))
subst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x
|
Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.
|
Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.
|
Agda
|
mit
|
mietek/epigram2,larrytheliquid/pigit,mietek/epigram2
|
bb08669e1bdc2f7d2df9bb9289bda1b42f3dece8
|
lib/Algebra/FunctionProperties/Eq.agda
|
lib/Algebra/FunctionProperties/Eq.agda
|
{-# OPTIONS --without-K #-}
-- Like Algebra.FunctionProperties but specialized to _≡_ and using implict arguments
-- Moreover there is some extensions such as:
-- * interchange
-- * non-zero inversions
-- * cancelation and non-zero cancelation
-- These properties can (for instance) be used to define algebraic
-- structures.
open import Level
open import Function using (flip)
open import Data.Product
open import Relation.Nullary
open import Relation.Binary.NP
open import Relation.Binary.PropositionalEquality.NP
-- The properties are specified using the following relation as
-- "equality".
module Algebra.FunctionProperties.Eq {a} {A : Set a} where
------------------------------------------------------------------------
-- Unary and binary operations
open import Algebra.FunctionProperties.Core public
------------------------------------------------------------------------
-- Properties of operations
Associative : Op₂ A → Set _
Associative _·_ = ∀ {x y z} → ((x · y) · z) ≡ (x · (y · z))
Commutative : Op₂ A → Set _
Commutative _·_ = ∀ {x y} → (x · y) ≡ (y · x)
LeftIdentity : A → Op₂ A → Set _
LeftIdentity e _·_ = ∀ {x} → (e · x) ≡ x
RightIdentity : A → Op₂ A → Set _
RightIdentity e _·_ = ∀ {x} → (x · e) ≡ x
Identity : A → Op₂ A → Set _
Identity e · = LeftIdentity e · × RightIdentity e ·
LeftZero : A → Op₂ A → Set _
LeftZero z _·_ = ∀ {x} → (z · x) ≡ z
RightZero : A → Op₂ A → Set _
RightZero z _·_ = ∀ {x} → (x · z) ≡ z
Zero : A → Op₂ A → Set _
Zero z · = LeftZero z · × RightZero z ·
LeftInverse : A → Op₁ A → Op₂ A → Set _
LeftInverse e _⁻¹ _·_ = ∀ {x} → (x ⁻¹ · x) ≡ e
LeftInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
LeftInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x ⁻¹ · x) ≡ e
RightInverse : A → Op₁ A → Op₂ A → Set _
RightInverse e _⁻¹ _·_ = ∀ {x} → (x · (x ⁻¹)) ≡ e
RightInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
RightInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x · (x ⁻¹)) ≡ e
Inverse : A → Op₁ A → Op₂ A → Set _
Inverse e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
InverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
InverseNonZero zero e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
_DistributesOverˡ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverˡ _+_ =
∀ {x y z} → (x * (y + z)) ≡ ((x * y) + (x * z))
_DistributesOverʳ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverʳ _+_ =
∀ {x y z} → ((y + z) * x) ≡ ((y * x) + (z * x))
_DistributesOver_ : Op₂ A → Op₂ A → Set _
* DistributesOver + = (* DistributesOverˡ +) × (* DistributesOverʳ +)
_IdempotentOn_ : Op₂ A → A → Set _
_·_ IdempotentOn x = (x · x) ≡ x
Idempotent : Op₂ A → Set _
Idempotent · = ∀ {x} → · IdempotentOn x
IdempotentFun : Op₁ A → Set _
IdempotentFun f = ∀ {x} → f (f x) ≡ f x
_Absorbs_ : Op₂ A → Op₂ A → Set _
_·_ Absorbs _∘_ = ∀ {x y} → (x · (x ∘ y)) ≡ x
Absorptive : Op₂ A → Op₂ A → Set _
Absorptive · ∘ = (· Absorbs ∘) × (∘ Absorbs ·)
Involutive : Op₁ A → Set _
Involutive f = ∀ {x} → f (f x) ≡ x
Interchange : Op₂ A → Op₂ A → Set _
Interchange _·_ _∘_ = ∀ {x y z t} → ((x · y) ∘ (z · t)) ≡ ((x ∘ z) · (y ∘ t))
LeftCancel : Op₂ A → Set _
LeftCancel _·_ = ∀ {c x y} → c · x ≡ c · y → x ≡ y
RightCancel : Op₂ A → Set _
RightCancel _·_ = ∀ {c x y} → x · c ≡ y · c → x ≡ y
LeftCancelNonZero : A → Op₂ A → Set _
LeftCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → c · x ≡ c · y → x ≡ y
RightCancelNonZero : A → Op₂ A → Set _
RightCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → x · c ≡ y · c → x ≡ y
module InterchangeFromAssocComm
(_·_ : Op₂ A)
(·-assoc : Associative _·_)
(·-comm : Commutative _·_)
where
open ≡-Reasoning
·= : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → (x · y) ≡ (x' · y')
·= refl refl = refl
·-interchange : Interchange _·_ _·_
·-interchange {x} {y} {z} {t}
= (x · y) · (z · t)
≡⟨ ·-assoc ⟩
x · (y · (z · t))
≡⟨ ·= refl (! ·-assoc) ⟩
x · ((y · z) · t)
≡⟨ ·= refl (·= ·-comm refl) ⟩
x · ((z · y) · t)
≡⟨ ·= refl ·-assoc ⟩
x · (z · (y · t))
≡⟨ ! ·-assoc ⟩
(x · z) · (y · t)
∎
module _ {b} {B : Set b} (f : A → B) where
Injective : Set (b ⊔ a)
Injective = ∀ {x y} → f x ≡ f y → x ≡ y
Conflict : Set (b ⊔ a)
Conflict = ∃ λ x → ∃ λ y → (x ≢ y) × f x ≡ f y
module _ {b} {B : Set b} {f : A → B} where
Injective-¬Conflict : Injective f → ¬ (Conflict f)
Injective-¬Conflict inj (x , y , x≢y , fx≡fy) = x≢y (inj fx≡fy)
Conflict-¬Injective : Conflict f → ¬ (Injective f)
Conflict-¬Injective = flip Injective-¬Conflict
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
-- Like Algebra.FunctionProperties but specialized to _≡_ and using implict arguments
-- Moreover there is some extensions such as:
-- * interchange
-- * non-zero inversions
-- * cancelation and non-zero cancelation
-- These properties can (for instance) be used to define algebraic
-- structures.
open import Level
open import Function.NP using (_$⟨_⟩_; flip)
open import Data.Nat.Base using (ℕ)
open import Data.Product
open import Relation.Nullary
open import Relation.Binary.NP
open import Relation.Binary.PropositionalEquality.NP
-- The properties are specified using the following relation as
-- "equality".
module Algebra.FunctionProperties.Eq {a} {A : Set a} where
------------------------------------------------------------------------
-- Unary and binary operations
open import Algebra.FunctionProperties.Core public
------------------------------------------------------------------------
-- Properties of operations
Associative : Op₂ A → Set _
Associative _·_ = ∀ {x y z} → ((x · y) · z) ≡ (x · (y · z))
Commutative : Op₂ A → Set _
Commutative _·_ = ∀ {x y} → (x · y) ≡ (y · x)
LeftIdentity : A → Op₂ A → Set _
LeftIdentity e _·_ = ∀ {x} → (e · x) ≡ x
RightIdentity : A → Op₂ A → Set _
RightIdentity e _·_ = ∀ {x} → (x · e) ≡ x
Identity : A → Op₂ A → Set _
Identity e · = LeftIdentity e · × RightIdentity e ·
LeftZero : A → Op₂ A → Set _
LeftZero z _·_ = ∀ {x} → (z · x) ≡ z
RightZero : A → Op₂ A → Set _
RightZero z _·_ = ∀ {x} → (x · z) ≡ z
Zero : A → Op₂ A → Set _
Zero z · = LeftZero z · × RightZero z ·
LeftInverse : A → Op₁ A → Op₂ A → Set _
LeftInverse e _⁻¹ _·_ = ∀ {x} → (x ⁻¹ · x) ≡ e
LeftInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
LeftInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x ⁻¹ · x) ≡ e
RightInverse : A → Op₁ A → Op₂ A → Set _
RightInverse e _⁻¹ _·_ = ∀ {x} → (x · (x ⁻¹)) ≡ e
RightInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
RightInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x · (x ⁻¹)) ≡ e
Inverse : A → Op₁ A → Op₂ A → Set _
Inverse e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
InverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
InverseNonZero zero e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
_DistributesOverˡ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverˡ _+_ =
∀ {x y z} → (x * (y + z)) ≡ ((x * y) + (x * z))
_DistributesOverʳ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverʳ _+_ =
∀ {x y z} → ((y + z) * x) ≡ ((y * x) + (z * x))
_DistributesOver_ : Op₂ A → Op₂ A → Set _
* DistributesOver + = (* DistributesOverˡ +) × (* DistributesOverʳ +)
_IdempotentOn_ : Op₂ A → A → Set _
_·_ IdempotentOn x = (x · x) ≡ x
Idempotent : Op₂ A → Set _
Idempotent · = ∀ {x} → · IdempotentOn x
IdempotentFun : Op₁ A → Set _
IdempotentFun f = ∀ {x} → f (f x) ≡ f x
_Absorbs_ : Op₂ A → Op₂ A → Set _
_·_ Absorbs _∘_ = ∀ {x y} → (x · (x ∘ y)) ≡ x
Absorptive : Op₂ A → Op₂ A → Set _
Absorptive · ∘ = (· Absorbs ∘) × (∘ Absorbs ·)
Involutive : Op₁ A → Set _
Involutive f = ∀ {x} → f (f x) ≡ x
Interchange : Op₂ A → Op₂ A → Set _
Interchange _·_ _∘_ = ∀ {x y z t} → ((x · y) ∘ (z · t)) ≡ ((x ∘ z) · (y ∘ t))
LeftCancel : Op₂ A → Set _
LeftCancel _·_ = ∀ {c x y} → c · x ≡ c · y → x ≡ y
RightCancel : Op₂ A → Set _
RightCancel _·_ = ∀ {c x y} → x · c ≡ y · c → x ≡ y
LeftCancelNonZero : A → Op₂ A → Set _
LeftCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → c · x ≡ c · y → x ≡ y
RightCancelNonZero : A → Op₂ A → Set _
RightCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → x · c ≡ y · c → x ≡ y
module InterchangeFromAssocComm
(_·_ : Op₂ A)
(·-assoc : Associative _·_)
(·-comm : Commutative _·_)
where
open ≡-Reasoning
·= : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → (x · y) ≡ (x' · y')
·= refl refl = refl
·-interchange : Interchange _·_ _·_
·-interchange {x} {y} {z} {t}
= (x · y) · (z · t)
≡⟨ ·-assoc ⟩
x · (y · (z · t))
≡⟨ ·= refl (! ·-assoc) ⟩
x · ((y · z) · t)
≡⟨ ·= refl (·= ·-comm refl) ⟩
x · ((z · y) · t)
≡⟨ ·= refl ·-assoc ⟩
x · (z · (y · t))
≡⟨ ! ·-assoc ⟩
(x · z) · (y · t)
∎
module _ {b} {B : Set b} (f : A → B) where
Injective : Set (b ⊔ a)
Injective = ∀ {x y} → f x ≡ f y → x ≡ y
Conflict : Set (b ⊔ a)
Conflict = ∃ λ x → ∃ λ y → (x ≢ y) × f x ≡ f y
module _ {b} {B : Set b} {f : A → B} where
Injective-¬Conflict : Injective f → ¬ (Conflict f)
Injective-¬Conflict inj (x , y , x≢y , fx≡fy) = x≢y (inj fx≡fy)
Conflict-¬Injective : Conflict f → ¬ (Injective f)
Conflict-¬Injective = flip Injective-¬Conflict
module Endo {f : A → A} where
Cycle^ : ℕ → Set _
Cycle^ n = ∃ λ x → f $⟨ n ⟩ x ≡ x
Cycle = ∃ Cycle^
-- -}
-- -}
-- -}
-- -}
|
add Endo.Cycle
|
Alegbra.FunctionProperties.Eq: add Endo.Cycle
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
675459709ff3563cde300754c35f229c6b963f10
|
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
ΔVal-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → ΔVal-base ι → Set
apply-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ΔVal-base ι → ⟦ ι ⟧Base
diff-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → ΔVal-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-ΔVal-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-ΔVal-base ι v (diff-ΔVal-base ι u v) ≡ u
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
apply-ΔVal : ∀ τ → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
diff-ΔVal : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
infixl 6 apply-ΔVal diff-ΔVal -- as with + - in GHC.Num
syntax apply-ΔVal τ v dv = v ⊞₍ τ ₎ dv
syntax diff-ΔVal τ 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 --
--------------------
-- (ΔVal τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (ΔVal (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- ΔVal : Type → Set
ΔVal (base ι) = ΔVal-base ι
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
n ⊞₍ base ι ₎ Δn = apply-ΔVal-base ι n Δn
f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
m ⊟₍ base ι ₎ n = diff-ΔVal-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (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
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ΔEnv-Entry : Type → Set
ΔEnv-Entry τ = Triple
⟦ τ ⟧
(λ _ → ΔVal τ)
(λ v dv → valid {τ} v dv)
ΔEnv : Context → Set
ΔEnv = DependentList ΔEnv-Entry
ignore-entry : ΔEnv-Entry ⊆ ⟦_⟧
ignore-entry (cons v _ _) = v
update-entry : ΔEnv-Entry ⊆ ⟦_⟧
update-entry {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv
ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
ignore = map (λ {τ} → ignore-entry {τ})
update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
update = map (λ {τ} → update-entry {τ})
|
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
ΔVal-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → ΔVal-base ι → Set
apply-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ΔVal-base ι → ⟦ ι ⟧Base
diff-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → ΔVal-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-ΔVal-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-ΔVal-base ι v (diff-ΔVal-base ι u v) ≡ u
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
apply-ΔVal : ∀ τ → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
diff-ΔVal : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
infixl 6 apply-ΔVal diff-ΔVal -- as with + - in GHC.Num
syntax apply-ΔVal τ v dv = v ⊞₍ τ ₎ dv
syntax diff-ΔVal τ 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 --
--------------------
-- (ΔVal τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (ΔVal (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- ΔVal : Type → Set
ΔVal (base ι) = ΔVal-base ι
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
n ⊞₍ base ι ₎ Δn = apply-ΔVal-base ι n Δn
f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
m ⊟₍ base ι ₎ n = diff-ΔVal-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (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
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ValidChange : Type → Set
ValidChange τ = Triple
⟦ τ ⟧
(λ _ → ΔVal τ)
(λ 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 {τ})
|
Rename ΔEnv-Entry to ValidChange.
|
Rename ΔEnv-Entry to ValidChange.
Also rename operations related to ΔEnv-Entry.
Old-commit-hash: 35ab981b89bab6eced9a7b20753bb140f6caf7a8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
0e5b51a126b354cb6a2c34c080deb1fdcc75c7cb
|
Denotational/Environments.agda
|
Denotational/Environments.agda
|
module Denotational.Environments
(Type : Set)
(⟦_⟧Type : Type → Set)
where
-- ENVIRONMENTS
--
-- This module defines the meaning of contexts, that is,
-- the type of environments that fit a context, together
-- with operations and properties of these operations.
--
-- This module is parametric in the syntax and semantics
-- of types, so it can be reused for different calculi
-- and models.
open import Relation.Binary.PropositionalEquality
open import Syntactic.Contexts Type
open import Denotational.Notation
private
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- TYPING CONTEXTS
-- Denotational Semantics : Contexts Represent Environments
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
⟦_⟧Context : Context → Set
⟦ ∅ ⟧Context = Empty
⟦ τ • Γ ⟧Context = Bind ⟦ τ ⟧ ⟦ Γ ⟧Context
meaningOfContext : Meaning Context
meaningOfContext = meaning ⟦_⟧Context
-- VARIABLES
-- Denotational Semantics
⟦_⟧Var : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ this ⟧Var (v • ρ) = v
⟦ that x ⟧Var (v • ρ) = ⟦ x ⟧Var ρ
meaningOfVar : ∀ {Γ τ} → Meaning (Var Γ τ)
meaningOfVar = meaning ⟦_⟧Var
-- WEAKENING
-- Remove a variable from an environment
⟦_⟧≼ : ∀ {Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) → ⟦ Γ₂ ⟧ → ⟦ Γ₁ ⟧
⟦ ∅ ⟧≼ ∅ = ∅
⟦ keep τ • Γ′ ⟧≼ (v • ρ) = v • ⟦ Γ′ ⟧≼ ρ
⟦ drop τ • Γ′ ⟧≼ (v • ρ) = ⟦ Γ′ ⟧≼ ρ
meaningOf≼ : ∀ {Γ₁ Γ₂} → Meaning (Γ₁ ≼ Γ₂)
meaningOf≼ = meaning ⟦_⟧≼
-- Properties
⟦⟧-≼-trans : ∀ {Γ₃ Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) (Γ″ : Γ₂ ≼ Γ₃) →
∀ (ρ : ⟦ Γ₃ ⟧) → ⟦ ≼-trans Γ′ Γ″ ⟧ ρ ≡ ⟦ Γ′ ⟧ (⟦ Γ″ ⟧ ρ)
⟦⟧-≼-trans Γ′ ∅ ∅ = refl
⟦⟧-≼-trans (keep τ • Γ′) (keep .τ • Γ″) (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-trans Γ′ Γ″ ρ)
⟦⟧-≼-trans (drop τ • Γ′) (keep .τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-trans Γ′ (drop τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-refl : ∀ {Γ : Context} →
∀ (ρ : ⟦ Γ ⟧) → ⟦ ≼-refl ⟧ ρ ≡ ρ
⟦⟧-≼-refl {∅} ∅ = refl
⟦⟧-≼-refl {τ • Γ} (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-refl ρ)
-- SOUNDNESS of variable lifting
lift-sound : ∀ {Γ₁ Γ₂ τ} (Γ′ : Γ₁ ≼ Γ₂) (x : Var Γ₁ τ) →
∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ lift Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (⟦ Γ′ ⟧ ρ)
lift-sound ∅ () ρ
lift-sound (keep τ • Γ′) this (v • ρ) = refl
lift-sound (keep τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ x ρ
lift-sound (drop τ • Γ′) this (v • ρ) = lift-sound Γ′ this ρ
lift-sound (drop τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ (that x) ρ
|
module Denotational.Environments
(Type : Set)
{ℓ}
(⟦_⟧Type : Type → Set ℓ)
where
-- ENVIRONMENTS
--
-- This module defines the meaning of contexts, that is,
-- the type of environments that fit a context, together
-- with operations and properties of these operations.
--
-- This module is parametric in the syntax and semantics
-- of types, so it can be reused for different calculi
-- and models.
open import Relation.Binary.PropositionalEquality
open import Syntactic.Contexts Type
open import Denotational.Notation
private
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- TYPING CONTEXTS
-- Denotational Semantics : Contexts Represent Environments
data Empty : Set ℓ where
∅ : Empty
data Bind A B : Set ℓ where
_•_ : (v : A) (ρ : B) → Bind A B
⟦_⟧Context : Context → Set ℓ
⟦ ∅ ⟧Context = Empty
⟦ τ • Γ ⟧Context = Bind ⟦ τ ⟧ ⟦ Γ ⟧Context
meaningOfContext : Meaning Context
meaningOfContext = meaning ⟦_⟧Context
-- VARIABLES
-- Denotational Semantics
⟦_⟧Var : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ this ⟧Var (v • ρ) = v
⟦ that x ⟧Var (v • ρ) = ⟦ x ⟧Var ρ
meaningOfVar : ∀ {Γ τ} → Meaning (Var Γ τ)
meaningOfVar = meaning ⟦_⟧Var
-- WEAKENING
-- Remove a variable from an environment
⟦_⟧≼ : ∀ {Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) → ⟦ Γ₂ ⟧ → ⟦ Γ₁ ⟧
⟦ ∅ ⟧≼ ∅ = ∅
⟦ keep τ • Γ′ ⟧≼ (v • ρ) = v • ⟦ Γ′ ⟧≼ ρ
⟦ drop τ • Γ′ ⟧≼ (v • ρ) = ⟦ Γ′ ⟧≼ ρ
meaningOf≼ : ∀ {Γ₁ Γ₂} → Meaning (Γ₁ ≼ Γ₂)
meaningOf≼ = meaning ⟦_⟧≼
-- Properties
⟦⟧-≼-trans : ∀ {Γ₃ Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) (Γ″ : Γ₂ ≼ Γ₃) →
∀ (ρ : ⟦ Γ₃ ⟧) → ⟦ ≼-trans Γ′ Γ″ ⟧ ρ ≡ ⟦ Γ′ ⟧ (⟦ Γ″ ⟧ ρ)
⟦⟧-≼-trans Γ′ ∅ ∅ = refl
⟦⟧-≼-trans (keep τ • Γ′) (keep .τ • Γ″) (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-trans Γ′ Γ″ ρ)
⟦⟧-≼-trans (drop τ • Γ′) (keep .τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-trans Γ′ (drop τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-refl : ∀ {Γ : Context} →
∀ (ρ : ⟦ Γ ⟧) → ⟦ ≼-refl ⟧ ρ ≡ ρ
⟦⟧-≼-refl {∅} ∅ = refl
⟦⟧-≼-refl {τ • Γ} (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-refl ρ)
-- SOUNDNESS of variable lifting
lift-sound : ∀ {Γ₁ Γ₂ τ} (Γ′ : Γ₁ ≼ Γ₂) (x : Var Γ₁ τ) →
∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ lift Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (⟦ Γ′ ⟧ ρ)
lift-sound ∅ () ρ
lift-sound (keep τ • Γ′) this (v • ρ) = refl
lift-sound (keep τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ x ρ
lift-sound (drop τ • Γ′) this (v • ρ) = lift-sound Γ′ this ρ
lift-sound (drop τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ (that x) ρ
|
Generalize environments to values in higher universes.
|
Generalize environments to values in higher universes.
Old-commit-hash: 4ff4102742306da9be77151d785aa608f8558977
|
Agda
|
mit
|
inc-lc/ilc-agda
|
0ab0b0012d2db6e26ec2716c2d95f3743111df4d
|
Parametric/Syntax/MType.agda
|
Parametric/Syntax/MType.agda
|
import Parametric.Syntax.Type as Type
module Parametric.Syntax.MType where
module Structure (Base : Type.Structure) where
open Type.Structure Base
mutual
-- Derived from CBPV
data ValType : Set where
U : (c : CompType) → ValType
B : (ι : Base) → ValType
vUnit : ValType
_v×_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
_v+_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
data CompType : Set where
F : ValType → CompType
_⇛_ : ValType → CompType → CompType
-- We did not use this in CBPV, so dropped.
-- _Π_ : CompType → CompType → CompType
cbnToCompType : Type → CompType
cbnToCompType (base ι) = F (B ι)
cbnToCompType (σ ⇒ τ) = U (cbnToCompType σ) ⇛ cbnToCompType τ
cbvToValType : Type → ValType
cbvToValType (base ι) = B ι
cbvToValType (σ ⇒ τ) = U (cbvToValType σ ⇛ F (cbvToValType τ))
open import Base.Syntax.Context ValType public
using ()
renaming
( ∅ to ∅∅
; _•_ to _••_
; mapContext to mapValCtx
; Var to ValVar
; Context to ValContext
; this to vThis; that to vThat)
cbnToValType : Type → ValType
cbnToValType τ = U (cbnToCompType τ)
cbvToCompType : Type → CompType
cbvToCompType τ = F (cbvToValType τ)
fromCBNCtx : Context → ValContext
fromCBNCtx Γ = mapValCtx cbnToValType Γ
fromCBVCtx : Context → ValContext
fromCBVCtx Γ = mapValCtx cbvToValType Γ
open import Data.List
open Data.List using (List) public
fromCBVToCompList : Context → List CompType
fromCBVToCompList Γ = mapValCtx cbvToCompType Γ
fromVar : ∀ {Γ τ} → (f : Type → ValType) → Var Γ τ → ValVar (mapValCtx f Γ) (f τ)
fromVar {x • Γ} f this = vThis
fromVar {x • Γ} f (that v) = vThat (fromVar f v)
|
import Parametric.Syntax.Type as Type
module Parametric.Syntax.MType where
module Structure (Base : Type.Structure) where
open Type.Structure Base
mutual
-- Derived from CBPV
data ValType : Set where
U : (c : CompType) → ValType
B : (ι : Base) → ValType
vUnit : ValType
_v×_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
_v+_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
-- Same associativity as the standard _×_
infixr 2 _v×_
data CompType : Set where
F : ValType → CompType
_⇛_ : ValType → CompType → CompType
-- We did not use this in CBPV, so dropped.
-- _Π_ : CompType → CompType → CompType
cbnToCompType : Type → CompType
cbnToCompType (base ι) = F (B ι)
cbnToCompType (σ ⇒ τ) = U (cbnToCompType σ) ⇛ cbnToCompType τ
cbvToValType : Type → ValType
cbvToValType (base ι) = B ι
cbvToValType (σ ⇒ τ) = U (cbvToValType σ ⇛ F (cbvToValType τ))
open import Base.Syntax.Context ValType public
using ()
renaming
( ∅ to ∅∅
; _•_ to _••_
; mapContext to mapValCtx
; Var to ValVar
; Context to ValContext
; this to vThis; that to vThat)
cbnToValType : Type → ValType
cbnToValType τ = U (cbnToCompType τ)
cbvToCompType : Type → CompType
cbvToCompType τ = F (cbvToValType τ)
fromCBNCtx : Context → ValContext
fromCBNCtx Γ = mapValCtx cbnToValType Γ
fromCBVCtx : Context → ValContext
fromCBVCtx Γ = mapValCtx cbvToValType Γ
open import Data.List
open Data.List using (List) public
fromCBVToCompList : Context → List CompType
fromCBVToCompList Γ = mapValCtx cbvToCompType Γ
fromVar : ∀ {Γ τ} → (f : Type → ValType) → Var Γ τ → ValVar (mapValCtx f Γ) (f τ)
fromVar {x • Γ} f this = vThis
fromVar {x • Γ} f (that v) = vThat (fromVar f v)
|
Fix arity of _v×_
|
MType: Fix arity of _v×_
|
Agda
|
mit
|
inc-lc/ilc-agda
|
dd73b5a36235448b400cd9d02d7983b1155617bc
|
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
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- 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)
TermConstructor : (Γ Σ : Context) (τ : Type) → Set
TermConstructor Γ ∅ τ′ = Term Γ τ′
TermConstructor Γ (τ • Σ) τ′ = Term Γ τ → TermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → TermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → TermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const 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 Denotation.Environment Type
open import Syntax.Context.Plotkin B
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
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)
TermConstructor : (Γ Σ : Context) (τ : Type) → Set
TermConstructor Γ ∅ τ′ = Term Γ τ′
TermConstructor Γ (τ • Σ) τ′ = Term Γ τ → TermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → TermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → TermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const constant)
|
Reorder definitions of Term and Terms.
|
Reorder definitions of Term and Terms.
The order of definitions should follow the order of declaration,
and this code seems easier to understand when one looks at Term
first.
Old-commit-hash: 517547b8eebe1b8803d9f30fa3945f02685b6cc5
|
Agda
|
mit
|
inc-lc/ilc-agda
|
8d03eb08a3d8dc237fc96cbdf430c73571ead581
|
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 : Δ {{ca}} x) → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil {{ca}} x)
≡⟨ update-nil {{ca}} x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ (dx : Δ {{ca}} x) → x ⊞ dx ≡ x → _≙_ {{ca}} dx (nil {{ca}} x)
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil {{ca}} x) ⟩
x ⊞ nil {{ca}} 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 {{ca}} 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
-- equiv-fun-changes-respect, and its corollaries fun-change-respects and
-- equiv-fun-changes-funs.
--
-- * 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 : Δ {{ca}} x} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : _⊞_ {{ca}} x (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff {{ca}} (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; DerivativeAsChange)
open FC.FunctionChange
equiv-fun-changes-respect : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
_≙_ {{changeAlgebra}} df₁ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
equiv-fun-changes-respect {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₂
∎
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} (df : Δ f) →
dx₁ ≙ dx₂ → apply df x dx₁ ≙ apply df x dx₂
fun-change-respects df dx₁≙dx₂ = equiv-fun-changes-respect (≙-refl {dx = df}) dx₁≙dx₂
-- D.o.e. function changes behave like the same function (up to d.o.e.).
equiv-fun-changes-funs : ∀ {x : A} {dx : Δ x} {f : A → B} {df₁ df₂ : Δ f} →
_≙_ {{changeAlgebra}} df₁ df₂ → apply df₁ x dx ≙ apply df₂ x dx
equiv-fun-changes-funs {dx = dx} df₁≙df₂ = equiv-fun-changes-respect df₁≙df₂ (≙-refl {dx = 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₂ → _≙_ {{CA}} 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 IsDerivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> IsDerivative f (apply df).
-- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
IsDerivative 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 {{CA}} x))
≡⟨ ext (λ x → cong f (update-nil {{CA}} 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 →
IsDerivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
derivative-is-nil-alternative : ∀ {f : A → B} df →
(IsDerivative-f-df : IsDerivative f df) → DerivativeAsChange IsDerivative-f-df ≙ nil f
derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → IsDerivative f (apply df) → IsDerivative 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 → IsDerivative f df →
∀ v → df v (nil {{CA}} v) ≙ nil {{CB}} (f v)
deriv-zero f df proof v = doe lemma
where
open ≡-Reasoning
lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil {{CB}} (f v)
lemma =
begin
f v ⊞ df v (nil {{CA}} v)
≡⟨ proof v (nil {{CA}} v) ⟩
f (v ⊞ (nil {{CA}} v))
≡⟨ cong f (update-nil {{CA}} v) ⟩
f v
≡⟨ sym (update-nil {{CB}} (f v)) ⟩
f v ⊞ nil {{CB}} (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 : Δ {{ca}} x) → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil {{ca}} x)
≡⟨ update-nil {{ca}} x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ (dx : Δ {{ca}} x) → x ⊞ dx ≡ x → _≙_ {{ca}} dx (nil {{ca}} x)
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil {{ca}} x) ⟩
x ⊞ nil {{ca}} 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 {{ca}} 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
-- equiv-fun-changes-respect, and its corollaries fun-change-respects and
-- equiv-fun-changes-funs.
--
-- * 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 : Δ {{ca}} x} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : _⊞_ {{ca}} x (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff {{ca}} (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; DerivativeAsChange)
open FC.FunctionChange
equiv-fun-changes-respect : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
_≙_ {{changeAlgebra}} df₁ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
equiv-fun-changes-respect {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₂
∎
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} (df : Δ f) →
dx₁ ≙ dx₂ → apply df x dx₁ ≙ apply df x dx₂
fun-change-respects df dx₁≙dx₂ = equiv-fun-changes-respect (≙-refl {dx = df}) dx₁≙dx₂
-- D.o.e. function changes behave like the same function (up to d.o.e.).
equiv-fun-changes-funs : ∀ {x : A} {dx : Δ x} {f : A → B} {df₁ df₂ : Δ f} →
_≙_ {{changeAlgebra}} df₁ df₂ → apply df₁ x dx ≙ apply df₂ x dx
equiv-fun-changes-funs {dx = dx} df₁≙df₂ = equiv-fun-changes-respect df₁≙df₂ (≙-refl {dx = dx})
derivative-doe-characterization : ∀ {a : A} {da : Δ a}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
_≙_ {{CB}} (df a da) (f (a ⊞ da) ⊟ f a)
derivative-doe-characterization {a} {da} {f} {df} is-derivative = doe lemma
where
open ≡-Reasoning
lemma : f a ⊞ df a da ≡ f a ⊞ (f (a ⊞ da) ⊟ f a)
lemma =
begin
(f a ⊞ df a da)
≡⟨ is-derivative a da ⟩
(f (a ⊞ da))
≡⟨ sym (update-diff (f (a ⊞ da)) (f a)) ⟩
(f a ⊞ (f (a ⊞ da) ⊟ f a))
∎
derivative-respects-doe : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
_≙_ {{CA}} dx₁ dx₂ → _≙_ {{CB}} (df x dx₁) (df x dx₂)
derivative-respects-doe {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
begin
df x dx₁
≙⟨ derivative-doe-characterization is-derivative ⟩
f (x ⊞ dx₁) ⊟ f x
≡⟨ cong (λ v → f v ⊟ f x) (proof dx₁≙dx₂) ⟩
f (x ⊞ dx₂) ⊟ f x
≙⟨ ≙-sym (derivative-doe-characterization is-derivative) ⟩
df x dx₂
∎
where
open ≙-Reasoning
-- This is also a corollary of fun-changes-respect
derivative-respects-doe-alt : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
_≙_ {{CA}} dx₁ dx₂ → _≙_ {{CB}} (df x dx₁) (df x dx₂)
derivative-respects-doe-alt {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
fun-change-respects (DerivativeAsChange is-derivative) dx₁≙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₂ → _≙_ {{CA}} 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 IsDerivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> IsDerivative f (apply df).
-- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
IsDerivative 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 {{CA}} x))
≡⟨ ext (λ x → cong f (update-nil {{CA}} 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 →
IsDerivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
derivative-is-nil-alternative : ∀ {f : A → B} df →
(IsDerivative-f-df : IsDerivative f df) → DerivativeAsChange IsDerivative-f-df ≙ nil f
derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → IsDerivative f (apply df) → IsDerivative 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 → IsDerivative f df →
∀ v → df v (nil {{CA}} v) ≙ nil {{CB}} (f v)
deriv-zero f df proof v = doe lemma
where
open ≡-Reasoning
lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil {{CB}} (f v)
lemma =
begin
f v ⊞ df v (nil {{CA}} v)
≡⟨ proof v (nil {{CA}} v) ⟩
f (v ⊞ (nil {{CA}} v))
≡⟨ cong f (update-nil {{CA}} v) ⟩
f v
≡⟨ sym (update-nil {{CB}} (f v)) ⟩
f v ⊞ nil {{CB}} (f v)
∎
|
Add new lemmas on change equivalence
|
Add new lemmas on change equivalence
|
Agda
|
mit
|
inc-lc/ilc-agda
|
ca0490d3e647551767333fe1b8af760d98b01dfa
|
Syntax/Term/Popl14.agda
|
Syntax/Term/Popl14.agda
|
module Syntax.Term.Popl14 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 Syntax.Context.Popl14 public
open import Data.Integer
data Term (Γ : Context) : Type -> Set where
int : (n : ℤ) → Term Γ int
add : (s : Term Γ int) (t : Term Γ int) → Term Γ int
minus : (t : Term Γ int) → Term Γ int
empty : Term Γ bag
insert : (s : Term Γ int) (t : Term Γ bag) → Term Γ bag
union : (s : Term Γ bag) → (t : Term Γ bag) → Term Γ bag
negate : (t : Term Γ bag) → Term Γ bag
flatmap : (s : Term Γ (int ⇒ bag)) (t : Term Γ bag) → Term Γ bag
sum : (t : Term Γ bag) → Term Γ int
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ} → (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) → Term Γ τ
abs : ∀ {σ τ} → (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ)
weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ
weaken Γ₁≼Γ₂ (int x) = int x
weaken Γ₁≼Γ₂ (add s t) = add (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (minus t) = minus (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ empty = empty
weaken Γ₁≼Γ₂ (insert s t) = insert (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (union s t) = union (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (negate t) = negate (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (flatmap s t) = flatmap (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (sum t) = sum (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (var x) = var (weakenVar Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {τ} t) = abs (weaken (keep τ • Γ₁≼Γ₂) t)
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
apply-term {int} =
let Δx = var (that this)
x = var this
in abs (abs (add x Δx))
apply-term {bag} =
let Δx = var (that this)
x = var this
in abs (abs (union x Δx))
apply-term {σ ⇒ τ} =
let
Δf = var (that (that this))
f = var (that this)
x = var this
in
-- Δf f x
abs (abs (abs
(app (app apply-term
(app (app Δf x) (app (app diff-term x) x)))
(app f x))))
diff-term {int} =
let x = var (that this)
y = var this
in abs (abs (add x (minus y)))
diff-term {bag} =
let x = var (that this)
y = var this
in abs (abs (union x (negate y)))
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 (app diff-term
(app g (app (app apply-term Δx) x)))
(app f x)))))
-- 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
|
module Syntax.Term.Popl14 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 Syntax.Context.Popl14 public
open import Data.Integer
data Term (Γ : Context) : Type -> Set where
int : (n : ℤ) → Term Γ int
add : (s : Term Γ int) (t : Term Γ int) → Term Γ int
minus : (t : Term Γ int) → Term Γ int
empty : Term Γ bag
insert : (s : Term Γ int) (t : Term Γ bag) → Term Γ bag
union : (s : Term Γ bag) → (t : Term Γ bag) → Term Γ bag
negate : (t : Term Γ bag) → Term Γ bag
flatmap : (s : Term Γ (int ⇒ bag)) (t : Term Γ bag) → Term Γ bag
sum : (t : Term Γ bag) → Term Γ int
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ} → (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) → Term Γ τ
abs : ∀ {σ τ} → (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ)
weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ
weaken Γ₁≼Γ₂ (int x) = int x
weaken Γ₁≼Γ₂ (add s t) = add (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (minus t) = minus (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ empty = empty
weaken Γ₁≼Γ₂ (insert s t) = insert (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (union s t) = union (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (negate t) = negate (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (flatmap s t) = flatmap (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (sum t) = sum (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (var x) = var (weakenVar Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {τ} t) = abs (weaken (keep τ • Γ₁≼Γ₂) t)
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
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 {int} =
let Δx = var (that this)
x = var this
in abs (abs (add x Δx))
apply-term {bag} =
let Δx = var (that this)
x = var this
in abs (abs (union x Δx))
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 {int} =
let x = var (that this)
y = var this
in abs (abs (add x (minus y)))
diff-term {bag} =
let x = var (that this)
y = var this
in abs (abs (union x (negate y)))
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))))
|
use ⊕ and ⊝ also when defining diff-term and apply-term
|
agda: use ⊕ and ⊝ also when defining diff-term and apply-term
Goal: make diff-term and apply-term themselves more readable.
Old-commit-hash: fc206b02d6057172e7f49c02eb521abaaafcb1e8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
f994df78d5187cf478e9bd11ea4ef31f16894f95
|
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
[_]Γ_from_to_ : ∀ Γ → eCh Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set
[ ∅ ]Γ ∅ from ∅ to ∅ = ⊤
[ τ • Γ ]Γ (dv • v1' • dρ) from (v1 • ρ1) to (v2 • ρ2) =
[ τ ] dv from v1 to v2 × v1 ≡ v1' × [ Γ ]Γ dρ from ρ1 to ρ2
⟦Γ≼ΔΓ⟧ : ∀ {Γ} (ρ1 ρ2 : ⟦ Γ ⟧Context) (dρ : eCh Γ) → [ Γ ]Γ dρ from ρ1 to ρ2 →
ρ1 ≡ ⟦ Γ≼ΔΓ ⟧≼ dρ
⟦Γ≼ΔΓ⟧ ∅ ∅ ∅ tt = refl
⟦Γ≼ΔΓ⟧ (v1 • ρ1) (v2 • ρ2) (dv • .v1 • dρ) (dvv , refl , dρρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ ρ1 ρ2 dρ dρρ)
fit-sound : ∀ {Γ τ} → (t : Term Γ τ) →
(ρ1 ρ2 : ⟦ Γ ⟧Context) (dρ : eCh Γ) → [ Γ ]Γ dρ from ρ1 to ρ2 →
⟦ t ⟧Term ρ1 ≡ ⟦ fit t ⟧Term dρ
fit-sound t ρ1 ρ2 dρ dρρ = trans
(cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ ρ1 ρ2 dρ dρρ))
(sym (weaken-sound t _))
fromtoDeriveVar : ∀ {Γ τ} → (x : Var Γ τ) →
(dρ : eCh Γ) (ρ1 ρ2 : ⟦ Γ ⟧Context) → [ Γ ]Γ dρ from ρ1 to ρ2 →
[ τ ] (⟦ x ⟧ΔVar ρ1 dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2)
fromtoDeriveVar this (dv • .v1 • dρ) (v1 • ρ1) (v2 • ρ2) (dvv , refl , dρρ) = dvv
fromtoDeriveVar (that x) (dv • .v1 • dρ) (v1 • ρ1) (v2 • ρ2) (dvv , refl , dρρ) = fromtoDeriveVar x dρ ρ1 ρ2 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ρ} {ρ1} {ρ2} dρρ rewrite sym (fit-sound t ρ1 ρ2 dρ dρρ) =
let fromToF = fromtoDerive (σ ⇒ τ) s dρρ
in let fromToB = fromtoDerive σ t dρρ in fromToF _ _ _ fromToB
fromtoDerive (σ ⇒ τ) (abs t) dρρ = λ da a1 a2 daa →
fromtoDerive τ t (daa , refl , dρρ)
|
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
-- XXX This would be more elegant as a datatype — that would avoid the need for
-- an equality proof.
[_]Γ_from_to_ : ∀ Γ → eCh Γ → (ρ1 ρ2 : ⟦ Γ ⟧Context) → Set
[ ∅ ]Γ ∅ from ∅ to ∅ = ⊤
[ τ • Γ ]Γ (dv • v1' • dρ) from (v1 • ρ1) to (v2 • ρ2) =
[ τ ] dv from v1 to v2 × v1 ≡ v1' × [ Γ ]Γ dρ from ρ1 to ρ2
⟦Γ≼ΔΓ⟧ : ∀ {Γ ρ1 ρ2 dρ} → [ Γ ]Γ dρ from ρ1 to ρ2 →
ρ1 ≡ ⟦ Γ≼ΔΓ ⟧≼ dρ
⟦Γ≼ΔΓ⟧ {∅} {∅} {∅} {∅} tt = refl
⟦Γ≼ΔΓ⟧ {_ • _} {v1 • ρ1} {v2 • ρ2} {dv • .v1 • dρ} (dvv , refl , dρρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ dρρ)
fit-sound : ∀ {Γ τ} → (t : Term Γ τ) →
∀ {ρ1 ρ2 dρ} → [ Γ ]Γ 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ρ : eCh Γ) (ρ1 ρ2 : ⟦ Γ ⟧Context) → [ Γ ]Γ dρ from ρ1 to ρ2 →
[ τ ] (⟦ x ⟧ΔVar ρ1 dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2)
fromtoDeriveVar this (dv • .v1 • dρ) (v1 • ρ1) (v2 • ρ2) (dvv , refl , dρρ) = dvv
fromtoDeriveVar (that x) (dv • .v1 • dρ) (v1 • ρ1) (v2 • ρ2) (dvv , refl , dρρ) = fromtoDeriveVar x dρ ρ1 ρ2 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ρρ = λ da a1 a2 daa →
fromtoDerive τ t (daa , refl , dρρ)
|
Hide deducible arguments
|
NewNew.agda: Hide deducible arguments
|
Agda
|
mit
|
inc-lc/ilc-agda
|
1ae2c016233ad8fa076a9b194e880387b889b8b0
|
Search/Searchable/Sum.agda
|
Search/Searchable/Sum.agda
|
open import Function.NP
open import Data.Nat using (_+_)
import Level as L
import Function.Inverse.NP as FI
import Function.Related as FR
open FI using (_↔_; inverses; module Inverse) renaming (_$₁_ to to; _$₂_ to from)
open import Function.Related.TypeIsomorphisms.NP
open import Data.Product.NP
open import Data.Sum
open import Data.Fin using (Fin)
open import Search.Type
open import Search.Searchable
open import Relation.Binary.Sum
open import Relation.Binary.PropositionalEquality.NP using (_≡_ ; module ≡-Reasoning; cong)
module Search.Searchable.Sum where
infixr 4 _⊎-search_
_⊎-search_ : ∀ {m A B} → Search m A → Search m B → Search m (A ⊎ B)
(searchᴬ ⊎-search searchᴮ) _∙_ f = searchᴬ _∙_ (f ∘ inj₁) ∙ searchᴮ _∙_ (f ∘ inj₂)
_⊎-search-ind_ : ∀ {m p A B} {sᴬ : Search m A} {sᴮ : Search m B}
→ SearchInd p sᴬ → SearchInd p sᴮ → SearchInd p (sᴬ ⊎-search sᴮ)
(Psᴬ ⊎-search-ind Psᴮ) P P∙ Pf
= P∙ (Psᴬ (λ s → P (λ _ f → s _ (f ∘ inj₁))) P∙ (Pf ∘ inj₁))
(Psᴮ (λ s → P (λ _ f → s _ (f ∘ inj₂))) P∙ (Pf ∘ inj₂))
infixr 4 _⊎-sum_
_⊎-sum_ : ∀ {A B} → Sum A → Sum B → Sum (A ⊎ B)
(sumᴬ ⊎-sum sumᴮ) f = sumᴬ (f ∘ inj₁) + sumᴮ (f ∘ inj₂)
module _ {A B} {sumᴬ : Sum A} {sumᴮ : Sum B} where
sumᴬᴮ = sumᴬ ⊎-sum sumᴮ
_⊎-adequate-sum_ : AdequateSum sumᴬ → AdequateSum sumᴮ → AdequateSum sumᴬᴮ
(asumᴬ ⊎-adequate-sum asumᴮ) f = (Fin (sumᴬ (f ∘ inj₁) + sumᴮ (f ∘ inj₂)))
↔⟨ FI.sym (Fin-⊎-+ _ _) ⟩
(Fin (sumᴬ (f ∘ inj₁)) ⊎ Fin (sumᴮ (f ∘ inj₂)))
↔⟨ asumᴬ (f ∘ inj₁) ⊎-cong asumᴮ (f ∘ inj₂) ⟩
(Σ A (Fin ∘ f ∘ inj₁) ⊎ Σ B (Fin ∘ f ∘ inj₂))
↔⟨ FI.sym Σ⊎-distrib ⟩
Σ (A ⊎ B) (Fin ∘ f)
∎
where open FR.EquationalReasoning
_⊎-μ_ : ∀ {A B} → Searchable A → Searchable B → Searchable (A ⊎ B)
μA ⊎-μ μB = mk _ (search-ind μA ⊎-search-ind search-ind μB)
(adequate-sum μA ⊎-adequate-sum adequate-sum μB)
module _ {A B} {sᴬ : Search₁ A} {sᴮ : Search₁ B} where
sᴬ⁺ᴮ = sᴬ ⊎-search sᴮ
_⊎-focus_ : Focus sᴬ → Focus sᴮ → Focus sᴬ⁺ᴮ
(fᴬ ⊎-focus fᴮ) (inj₁ x , y) = inj₁ (fᴬ (x , y))
(fᴬ ⊎-focus fᴮ) (inj₂ x , y) = inj₂ (fᴮ (x , y))
_⊎-unfocus_ : Unfocus sᴬ → Unfocus sᴮ → Unfocus sᴬ⁺ᴮ
_⊎-unfocus_ fᴬ fᴮ (inj₁ x) = first inj₁ (fᴬ x)
_⊎-unfocus_ fᴬ fᴮ (inj₂ y) = first inj₂ (fᴮ y)
{-
_⊎-focused_ : Focused sᴬ → Focused sᴮ → Focused {L.zero} sᴬ⁺ᴮ
_⊎-focused_ fᴬ fᴮ {B} = inverses (to fᴬ ⊎-focus to fᴮ) (from fᴬ ⊎-unfocus from fᴮ) (⇒) (⇐)
where
⇒ : (x : Σ (A ⊎ {!!}) {!!}) → _
⇒ (x , y) = {!!}
⇐ : (x : sᴬ _⊎_ (B ∘ inj₁) ⊎ sᴮ _⊎_ (B ∘ inj₂)) → _
⇐ (inj₁ x) = cong inj₁ {!!}
⇐ (inj₂ x) = cong inj₂ {!!}
-}
_⊎-lookup_ : Lookup sᴬ → Lookup sᴮ → Lookup (sᴬ ⊎-search sᴮ)
(lookupᴬ ⊎-lookup lookupᴮ) (x , y) = [ lookupᴬ x , lookupᴮ y ]
_⊎-reify_ : Reify sᴬ → Reify sᴮ → Reify (sᴬ ⊎-search sᴮ)
(reifyᴬ ⊎-reify reifyᴮ) f = (reifyᴬ (f ∘ inj₁)) , (reifyᴮ (f ∘ inj₂))
-- -}
-- -}
-- -}
|
open import Function.NP
open import Data.Nat using (_+_)
import Level as L
import Function.Inverse.NP as FI
import Function.Related as FR
open FI using (_↔_; inverses; module Inverse) renaming (_$₁_ to to; _$₂_ to from)
open import Function.Related.TypeIsomorphisms.NP
open import Data.Product.NP
open import Data.Sum
open import Data.Bits
open import Data.Fin using (Fin)
open import Search.Type
open import Search.Searchable
open import Relation.Binary.Sum
import Relation.Binary.PropositionalEquality.NP as ≡
open ≡ using (_≡_ ; module ≡-Reasoning; cong)
module Search.Searchable.Sum where
infixr 4 _⊎-search_
_⊎-search_ : ∀ {m A B} → Search m A → Search m B → Search m (A ⊎ B)
(searchᴬ ⊎-search searchᴮ) _∙_ f = searchᴬ _∙_ (f ∘ inj₁) ∙ searchᴮ _∙_ (f ∘ inj₂)
_⊎-search-ind_ : ∀ {m p A B} {sᴬ : Search m A} {sᴮ : Search m B}
→ SearchInd p sᴬ → SearchInd p sᴮ → SearchInd p (sᴬ ⊎-search sᴮ)
(Psᴬ ⊎-search-ind Psᴮ) P P∙ Pf
= P∙ (Psᴬ (λ s → P (λ _ f → s _ (f ∘ inj₁))) P∙ (Pf ∘ inj₁))
(Psᴮ (λ s → P (λ _ f → s _ (f ∘ inj₂))) P∙ (Pf ∘ inj₂))
infixr 4 _⊎-sum_
_⊎-sum_ : ∀ {A B} → Sum A → Sum B → Sum (A ⊎ B)
(sumᴬ ⊎-sum sumᴮ) f = sumᴬ (f ∘ inj₁) + sumᴮ (f ∘ inj₂)
module _ {A B} {sumᴬ : Sum A} {sumᴮ : Sum B} where
sumᴬᴮ = sumᴬ ⊎-sum sumᴮ
_⊎-adequate-sum_ : AdequateSum sumᴬ → AdequateSum sumᴮ → AdequateSum sumᴬᴮ
(asumᴬ ⊎-adequate-sum asumᴮ) f = (Fin (sumᴬ (f ∘ inj₁) + sumᴮ (f ∘ inj₂)))
↔⟨ FI.sym (Fin-⊎-+ _ _) ⟩
(Fin (sumᴬ (f ∘ inj₁)) ⊎ Fin (sumᴮ (f ∘ inj₂)))
↔⟨ asumᴬ (f ∘ inj₁) ⊎-cong asumᴮ (f ∘ inj₂) ⟩
(Σ A (Fin ∘ f ∘ inj₁) ⊎ Σ B (Fin ∘ f ∘ inj₂))
↔⟨ FI.sym Σ⊎-distrib ⟩
Σ (A ⊎ B) (Fin ∘ f)
∎
where open FR.EquationalReasoning
_⊎-μ_ : ∀ {A B} → Searchable A → Searchable B → Searchable (A ⊎ B)
μA ⊎-μ μB = mk _ (search-ind μA ⊎-search-ind search-ind μB)
(adequate-sum μA ⊎-adequate-sum adequate-sum μB)
module _ {A B} {sᴬ : Search₁ A} {sᴮ : Search₁ B} where
sᴬ⁺ᴮ = sᴬ ⊎-search sᴮ
_⊎-focus_ : Focus sᴬ → Focus sᴮ → Focus sᴬ⁺ᴮ
(fᴬ ⊎-focus fᴮ) (inj₁ x , y) = inj₁ (fᴬ (x , y))
(fᴬ ⊎-focus fᴮ) (inj₂ x , y) = inj₂ (fᴮ (x , y))
_⊎-unfocus_ : Unfocus sᴬ → Unfocus sᴮ → Unfocus sᴬ⁺ᴮ
_⊎-unfocus_ fᴬ fᴮ (inj₁ x) = first inj₁ (fᴬ x)
_⊎-unfocus_ fᴬ fᴮ (inj₂ y) = first inj₂ (fᴮ y)
{-
_⊎-focused_ : Focused sᴬ → Focused sᴮ → Focused {L.zero} sᴬ⁺ᴮ
_⊎-focused_ fᴬ fᴮ {B} = inverses (to fᴬ ⊎-focus to fᴮ) (from fᴬ ⊎-unfocus from fᴮ) (⇒) (⇐)
where
⇒ : (x : Σ (A ⊎ {!!}) {!!}) → _
⇒ (x , y) = {!!}
⇐ : (x : sᴬ _⊎_ (B ∘ inj₁) ⊎ sᴮ _⊎_ (B ∘ inj₂)) → _
⇐ (inj₁ x) = cong inj₁ {!!}
⇐ (inj₂ x) = cong inj₂ {!!}
-}
_⊎-lookup_ : Lookup sᴬ → Lookup sᴮ → Lookup (sᴬ ⊎-search sᴮ)
(lookupᴬ ⊎-lookup lookupᴮ) (x , y) = [ lookupᴬ x , lookupᴮ y ]
_⊎-reify_ : Reify sᴬ → Reify sᴮ → Reify (sᴬ ⊎-search sᴮ)
(reifyᴬ ⊎-reify reifyᴮ) f = (reifyᴬ (f ∘ inj₁)) , (reifyᴮ (f ∘ inj₂))
searchBit : ∀ {m} → Search m Bit
searchBit _∙_ f = f 0b ∙ f 1b
searchBit-ind : ∀ {m p} → SearchInd p {m} searchBit
searchBit-ind _ _P∙_ Pf = Pf 0b P∙ Pf 1b
μBit : Searchable Bit
μBit = μ-iso (FI.sym Bit↔⊤⊎⊤) (μ⊤ ⊎-μ μ⊤)
focusBit : ∀ {a} → Focus {a} searchBit
focusBit (false , x) = inj₁ x
focusBit (true , x) = inj₂ x
focusedBit : Focused {L.zero} searchBit
focusedBit {B} = inverses focusBit unfocus (⇒) (⇐)
where open Searchable₁₁ searchBit-ind
⇒ : (x : Σ Bit B) → _
⇒ (false , x) = ≡.refl
⇒ (true , x) = ≡.refl
⇐ : (x : B 0b ⊎ B 1b) → _
⇐ (inj₁ x) = ≡.refl
⇐ (inj₂ x) = ≡.refl
lookupBit : ∀ {a} → Lookup {a} searchBit
lookupBit = proj
-- -}
-- -}
-- -}
|
add bit
|
Searchable.Sum: add bit
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
7540b79d7380f6d1c7ca7db983d990a2aab84b00
|
lib/Explore/GroupHomomorphism.agda
|
lib/Explore/GroupHomomorphism.agda
|
{-# OPTIONS --without-K #-}
module Explore.GroupHomomorphism where
open import Level
open import Algebra.FunctionProperties
open import Data.Product
open import Function using (_∘_ ; flip)
open import Function.Inverse as Inv using (_↔_; module Inverse)
open import Relation.Binary.PropositionalEquality.NP hiding (_∙_)
open import Explore.Core
open import Explore.Properties
open import Explore.Sum
record Group (G : Set) : Set where
field
_∙_ : G → G → G
ε : G
-_ : G → G
-- laws
field
assoc : Associative _≡_ _∙_
identity : Identity _≡_ ε _∙_
inverse : Inverse _≡_ ε -_ _∙_
GroupHomomorphism : ∀ {A B : Set} → Group A → Group B →(A → B) → Set
GroupHomomorphism GA GB f = ∀ x y → f (x + y) ≡ f x * f y
where
open Group GA renaming (_∙_ to _+_)
open Group GB renaming (_∙_ to _*_)
module _ {A B}(GA : Group A)(GB : Group B)
(f : A → B)
(exploreA : Explore zero A)(f-homo : GroupHomomorphism GA GB f)
([f] : B → A)(f-sur : ∀ b → f ([f] b) ≡ b)
(explore-ext : ExploreExt exploreA)
where
open Group GA using (-_) renaming (_∙_ to _+_ ; ε to 0g)
open Group GB using () renaming (_∙_ to _*_ ; ε to 1g ; -_ to 1/_)
{- How all this is related to elgamal
the Group GA is ℤq with modular addition as operation
the Group GB is the cyclic group with order q
f is g^, the proof only need that it is a group homomorphism
and that it has a right inverse
we require that the explore (for type A) function (should work with only summation)
is Stable under addition of GA (notice that we have flip in there that is so that
we don't need commutativity
finally we require that the explore function respects extensionality
-}
{-
I had some problems with using the standard library definiton of Groups
so I rolled my own, therefor I need some boring proofs first
-}
help : ∀ x y → x ≡ (x * y) * 1/ y
help x y = x
≡⟨ sym (proj₂ identity x) ⟩
x * 1g
≡⟨ cong (_*_ x) (sym (proj₂ inverse y)) ⟩
x * (y * 1/ y)
≡⟨ sym (assoc x y (1/ y)) ⟩
(x * y) * (1/ y)
∎
where open ≡-Reasoning
open Group GB
unique-1g : ∀ x y → x * y ≡ y → x ≡ 1g
unique-1g x y eq = x
≡⟨ help x y ⟩
(x * y) * 1/ y
≡⟨ cong (flip _*_ (1/ y)) eq ⟩
y * 1/ y
≡⟨ proj₂ inverse y ⟩
1g
∎
where open ≡-Reasoning
open Group GB
unique-/ : ∀ x y → x * y ≡ 1g → x ≡ 1/ y
unique-/ x y eq = x
≡⟨ help x y ⟩
(x * y) * 1/ y
≡⟨ cong (flip _*_ (1/ y)) eq ⟩
1g * 1/ y
≡⟨ proj₁ identity (1/ y) ⟩
1/ y
∎
where open ≡-Reasoning
open Group GB
f-pres-ε : f 0g ≡ 1g
f-pres-ε = unique-1g (f 0g) (f 0g) part
where open ≡-Reasoning
open Group GA
part = f 0g * f 0g
≡⟨ sym (f-homo 0g 0g) ⟩
f (0g + 0g)
≡⟨ cong f (proj₁ identity 0g) ⟩
f 0g
∎
f-pres-inv : ∀ x → f (- x) ≡ 1/ f x
f-pres-inv x = unique-/ (f (- x)) (f x) part
where open ≡-Reasoning
open Group GA hiding (-_)
part = f (- x) * f x
≡⟨ sym (f-homo (- x) x) ⟩
f (- x + x)
≡⟨ cong f (proj₁ inverse x) ⟩
f 0g
≡⟨ f-pres-ε ⟩
1g
∎
{-
While this proof looks complicated it basically just adds inverse of m₀ and then adds m₁ (from image of f)
we need the homomorphic property to pull out the values.
-}
module _ {X}(z : X)(op : X → X → X)
(sui : ∀ k → StableUnder' exploreA z op (flip (Group._∙_ GA) k))
where
-- this proof isn't actually any hard..
thm : ∀ (O : B → X) m₀ m₁ → exploreA z op (λ x → O (f x * m₀)) ≡ exploreA z op (λ x → O (f x * m₁))
thm O m₀ m₁ = explore (λ x → O (f x * m₀))
≡⟨ sui (- [f] m₀) (λ x → O (f x * m₀)) ⟩
explore (λ x → O (f (x + - [f] m₀) * m₀))
≡⟨ explore-ext z op (λ x → cong O (lemma1 x)) ⟩
explore (λ x → O (f x ))
≡⟨ sui ([f] m₁) (O ∘ f) ⟩
explore (λ x → O (f (x + [f] m₁)))
≡⟨ explore-ext z op (λ x → cong O (lemma2 x)) ⟩
explore (λ x → O (f x * m₁))
∎
where
open ≡-Reasoning
explore = exploreA z op
lemma1 : ∀ x → f (x + - [f] m₀) * m₀ ≡ f x
lemma1 x rewrite f-homo x (- [f] m₀)
| f-pres-inv ([f] m₀)
| f-sur m₀
| Group.assoc GB (f x) (1/ m₀) m₀
| proj₁ (Group.inverse GB) m₀
| proj₂ (Group.identity GB) (f x) = refl
lemma2 : ∀ x → f (x + [f] m₁) ≡ f x * m₁
lemma2 x rewrite f-homo x ([f] m₁)
| f-sur m₁ = refl
|
{-# OPTIONS --without-K #-}
module Explore.GroupHomomorphism where
open import Level
open import Algebra.FunctionProperties
open import Data.Product
open import Function using (_∘_ ; flip)
open import Function.Inverse as Inv using (_↔_; module Inverse)
open import Relation.Binary.PropositionalEquality.NP hiding (_∙_)
open import Explore.Core
open import Explore.Properties
open import Explore.Sum
{-
I had some problems with using the standard library definiton of Groups
so I rolled my own, therefor I need some boring proofs first
-}
record Group (G : Set) : Set where
field
_∙_ : G → G → G
ε : G
-_ : G → G
-- laws
field
assoc : Associative _≡_ _∙_
identity : Identity _≡_ ε _∙_
inverse : Inverse _≡_ ε -_ _∙_
-- derived property
help : ∀ x y → x ≡ (x ∙ y) ∙ - y
help x y = x
≡⟨ sym (proj₂ identity x) ⟩
x ∙ ε
≡⟨ cong (_∙_ x) (sym (proj₂ inverse y)) ⟩
x ∙ (y ∙ - y)
≡⟨ sym (assoc x y (- y)) ⟩
(x ∙ y) ∙ (- y)
∎
where open ≡-Reasoning
unique-1g : ∀ x y → x ∙ y ≡ y → x ≡ ε
unique-1g x y eq = x
≡⟨ help x y ⟩
(x ∙ y) ∙ - y
≡⟨ cong (flip _∙_ (- y)) eq ⟩
y ∙ - y
≡⟨ proj₂ inverse y ⟩
ε
∎
where open ≡-Reasoning
unique-/ : ∀ x y → x ∙ y ≡ ε → x ≡ - y
unique-/ x y eq = x
≡⟨ help x y ⟩
(x ∙ y) ∙ - y
≡⟨ cong (flip _∙_ (- y)) eq ⟩
ε ∙ - y
≡⟨ proj₁ identity (- y) ⟩
- y
∎
where open ≡-Reasoning
module _ {A B : Set}(GA : Group A)(GB : Group B) where
open Group GA using (-_) renaming (_∙_ to _+_; ε to 0g)
open Group GB using (unique-1g ; unique-/) renaming (_∙_ to _*_; ε to 1g; -_ to 1/_)
GroupHomomorphism : (A → B) → Set
GroupHomomorphism f = ∀ x y → f (x + y) ≡ f x * f y
module GroupHomomorphismProp {f}(f-homo : GroupHomomorphism f) where
f-pres-ε : f 0g ≡ 1g
f-pres-ε = unique-1g (f 0g) (f 0g) part
where open ≡-Reasoning
open Group GA using (identity)
part = f 0g * f 0g
≡⟨ sym (f-homo 0g 0g) ⟩
f (0g + 0g)
≡⟨ cong f (proj₁ identity 0g) ⟩
f 0g
∎
f-pres-inv : ∀ x → f (- x) ≡ 1/ f x
f-pres-inv x = unique-/ (f (- x)) (f x) part
where open ≡-Reasoning
open Group GA using (inverse)
part = f (- x) * f x
≡⟨ sym (f-homo (- x) x) ⟩
f (- x + x)
≡⟨ cong f (proj₁ inverse x) ⟩
f 0g
≡⟨ f-pres-ε ⟩
1g
∎
module _ {A B}(GA : Group A)(GB : Group B)
(f : A → B)
(exploreA : Explore zero A)(f-homo : GroupHomomorphism GA GB f)
([f] : B → A)(f-sur : ∀ b → f ([f] b) ≡ b)
(explore-ext : ExploreExt exploreA)
where
open Group GA using (-_) renaming (_∙_ to _+_ ; ε to 0g)
open Group GB using () renaming (_∙_ to _*_ ; ε to 1g ; -_ to 1/_)
open GroupHomomorphismProp GA GB f-homo
{- How all this is related to elgamal
the Group GA is ℤq with modular addition as operation
the Group GB is the cyclic group with order q
f is g^, the proof only need that it is a group homomorphism
and that it has a right inverse
we require that the explore (for type A) function (should work with only summation)
is Stable under addition of GA (notice that we have flip in there that is so that
we don't need commutativity
finally we require that the explore function respects extensionality
-}
{-
While this proof looks complicated it basically just adds inverse of m₀ and then adds m₁ (from image of f)
we need the homomorphic property to pull out the values.
-}
module _ {X}(z : X)(op : X → X → X)
(sui : ∀ k → StableUnder' exploreA z op (flip (Group._∙_ GA) k))
where
-- this proof isn't actually any hard..
thm : ∀ (O : B → X) m₀ m₁ → exploreA z op (λ x → O (f x * m₀)) ≡ exploreA z op (λ x → O (f x * m₁))
thm O m₀ m₁ = explore (λ x → O (f x * m₀))
≡⟨ sui (- [f] m₀) (λ x → O (f x * m₀)) ⟩
explore (λ x → O (f (x + - [f] m₀) * m₀))
≡⟨ explore-ext z op (λ x → cong O (lemma1 x)) ⟩
explore (λ x → O (f x ))
≡⟨ sui ([f] m₁) (O ∘ f) ⟩
explore (λ x → O (f (x + [f] m₁)))
≡⟨ explore-ext z op (λ x → cong O (lemma2 x)) ⟩
explore (λ x → O (f x * m₁))
∎
where
open ≡-Reasoning
explore = exploreA z op
lemma1 : ∀ x → f (x + - [f] m₀) * m₀ ≡ f x
lemma1 x rewrite f-homo x (- [f] m₀)
| f-pres-inv ([f] m₀)
| f-sur m₀
| Group.assoc GB (f x) (1/ m₀) m₀
| proj₁ (Group.inverse GB) m₀
| proj₂ (Group.identity GB) (f x) = refl
lemma2 : ∀ x → f (x + [f] m₁) ≡ f x * m₁
lemma2 x rewrite f-homo x ([f] m₁)
| f-sur m₁ = refl
|
refactor grouphomomorphism
|
refactor grouphomomorphism
|
Agda
|
bsd-3-clause
|
crypto-agda/explore
|
e4762e8af8591526fcb5395b9c63888ed89cadd2
|
Syntax/Type/Plotkin.agda
|
Syntax/Type/Plotkin.agda
|
module Syntax.Type.Plotkin where
-- Types for language description à la Plotkin (LCF as PL)
--
-- Given base types, we build function types.
infixr 5 _⇒_
data Type (B : Set {- of base types -}) : Set where
base : (ι : B) → Type B
_⇒_ : (σ : Type B) → (τ : Type B) → Type B
-- Lift (Δ : B → B) to (Δtype : Type B → Type B)
-- according to Δ (σ ⇒ τ) = σ ⇒ Δ σ ⇒ Δ τ
lift₀ : ∀ {B} → (B → B) → (Type B → Type B)
lift₀ f (base ι) = base (f ι)
lift₀ f (σ ⇒ τ) = let Δ = lift₀ f in σ ⇒ Δ σ ⇒ Δ τ
-- Lift (Δ : B → Type B) to (Δtype : Type B → Type B)
-- according to Δ (σ ⇒ τ) = σ ⇒ Δ σ ⇒ Δ τ
lift₁ : ∀ {B} → (B → Type B) → (Type B → Type B)
lift₁ f (base ι) = f ι
lift₁ f (σ ⇒ τ) = let Δ = lift₁ f in σ ⇒ Δ σ ⇒ Δ τ
|
module Syntax.Type.Plotkin where
-- Types for language description à la Plotkin (LCF as PL)
--
-- Given base types, we build function types.
infixr 5 _⇒_
data Type (B : Set {- of base types -}) : Set where
base : (ι : B) → Type B
_⇒_ : (σ : Type B) → (τ : Type B) → Type B
-- Lift (Δ : B → Type B) to (Δtype : Type B → Type B)
-- according to Δ (σ ⇒ τ) = σ ⇒ Δ σ ⇒ Δ τ
lift₁ : ∀ {B} → (B → Type B) → (Type B → Type B)
lift₁ f (base ι) = f ι
lift₁ f (σ ⇒ τ) = let Δ = lift₁ f in σ ⇒ Δ σ ⇒ Δ τ
-- Note: the above is monadic bind with a different argument order.
open import Function
-- Variant of lift₁ for (Δ : B → B).
lift₀ : ∀ {B} → (B → B) → (Type B → Type B)
lift₀ f = lift₁ $ base ∘ f
-- If lift₁ is a monadic bind, this is fmap,
-- and base is return.
--
-- Similarly, for collections map can be defined from flatMap, like lift₀ can be
-- defined in terms of lift₁.
|
Remove code duplication
|
agda: Remove code duplication
The current code defines two overloads of the same function independently, with
duplicated documentation. However, one can be defined in terms of the other.
Note: I could not fully test this code, since it is not yet clear which file is
the main one. I typechecked Syntax.Language.Atlas, I hope this was enough.
I couldn't test this change because of #41: without fixing it, either I risk
breaking code, or I can't fix issues I spot.
Old-commit-hash: b481a293cbc77ca4b2981761c15e7771d95216af
|
Agda
|
mit
|
inc-lc/ilc-agda
|
23339dd5a9e56c3c31d42252c4020b716ec77725
|
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) : Set (c ⊔ d) where
field
update-diff : ∀ u v → update v (diff u v) ≡ u
-- In the paper, this is Def. 2.2.
nil : ∀ v → Change v
nil v = diff v v
-- In the paper, this is Lemma 2.3.
update-nil : ∀ v → update v (nil v) ≡ v
update-nil v = update-diff v v
-- abbrevitations
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
isChangeAlgebra : IsChangeAlgebra Change update diff
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 ₎
-- 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 = _⊝_
; 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
∎
}
}
-- 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.5 in the paper.
record FunctionChange (f : A → B) : Set (a ⊔ b ⊔ c ⊔ d) where
constructor
cons
field
-- Definition 2.5a
apply : (a : A) (da : Δ a) →
Δ (f a)
-- Definition 2.5b.
-- (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
changeAlgebra : ChangeAlgebra (a ⊔ b ⊔ c ⊔ d) (A → B)
changeAlgebra = record
{ Change = FunctionChange
-- in the paper, update and diff below are in Def. 2.6
; update = λ f df a → f a ⊞ apply df a (nil a)
; diff = λ 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)
∎
}
; isChangeAlgebra = record
-- the proof of update-diff is the second half of what
-- we have to prove for Theorem 2.7.
{ update-diff = λ 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 Lemma 2.8 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.9 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
; isChangeAlgebra = record
{ update-diff = update-diff-all
}
}
}
|
------------------------------------------------------------------------
-- 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) : Set (c ⊔ d) where
field
update-diff : ∀ u v → update v (diff u v) ≡ u
-- In the paper, this is Def. 2.2.
nil : ∀ v → Change v
nil v = diff v v
-- In the paper, this is Lemma 2.3.
update-nil : ∀ v → update v (nil v) ≡ v
update-nil v = update-diff 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
isChangeAlgebra : IsChangeAlgebra Change update diff
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 ₎
-- 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 = _⊝_
; 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
∎
}
}
-- 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
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 = λ f df a → f a ⊞ apply df a (nil a)
; diff = λ 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)
∎
}
; isChangeAlgebra = record
-- the proof of update-diff is the second half of what
-- we have to prove for Theorem 2.8.
{ update-diff = λ 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
; isChangeAlgebra = record
{ update-diff = update-diff-all
}
}
}
|
Update references to paper
|
Update references to paper
During revision, I added Lemma 2.5 to the paper, changing the numbering.
This commits updates the numbering in the formalization.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2f337fa6c912b4b71f8a6539911608241ebc8524
|
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
-- 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 {τ} {Γ})
|
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₂ diffσ s t
_⊝τ_ = λ s t → app₂ diffτ s t
_⊕σ_ = λ t Δt → app₂ applyσ Δt t
_⊕τ_ = λ t Δt → 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 {τ} {Γ})
|
Use helpers for nested applications.
|
Use helpers for nested applications.
Old-commit-hash: e4c7562ec14ff04d00f64f84b95725baa11a85ca
|
Agda
|
mit
|
inc-lc/ilc-agda
|
561e54d6fa752c3e41e0261fcb95b5fe132461b5
|
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 Γ τ
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const 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 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 Γ τ
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ
curryTermConstructor {∅} k = k ∅
curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
lift-η-const constant = curryTermConstructor (const constant)
|
Rename lift-η-const-rec to curryTermConstructor.
|
Rename lift-η-const-rec to curryTermConstructor.
Also delete the frivolous comment, which is no longer necessary
now that curryTermConstructor has a descriptive name and the
matching type signature.
Old-commit-hash: bcd82c9b32ab89ebe5c1badbec2d5714d0fce9bf
|
Agda
|
mit
|
inc-lc/ilc-agda
|
6e6a81d4cd5e6747138da920db56cf5c044c974c
|
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 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 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))
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 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 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-Δ : {τ : 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)
-- 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))
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)
|
rename parameter
|
agda: rename parameter
As suggested by Tillmann.
Old-commit-hash: 3b2f1506d9fc2595f06fb0d8e57a526bc30b03ac
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b50b0f77764d63042aa5a88075d4320129b3681a
|
Syntactic/Terms/Total.agda
|
Syntactic/Terms/Total.agda
|
module Syntactic.Terms.Total where
-- TERMS with a primitive for TOTAL DERIVATIVES
--
-- This module defines the syntax 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 Syntactic.Types
open import Syntactic.Contexts Type
open import Changes
open import ChangeContexts
open import Relation.Binary.PropositionalEquality
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
true false : ∀ {Γ} → Term Γ bool
if : ∀ {Γ τ} → (t₁ : Term Γ bool) (t₂ t₃ : Term Γ τ) → Term Γ τ
-- `Δ t` describes how t changes if its free variables or arguments change
Δ : ∀ {Γ τ} → (t : Term Γ τ) → Term (Δ-Context Γ) (Δ-Type τ)
weakenOne : ∀ Γ₁ τ₂ {Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ (τ₂ • Γ₃)) τ
substTerm : ∀ {τ Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Term Γ₁ τ → Term Γ₂ τ
substTerm {τ} {Γ₁} {Γ₂} ≡₁ t = subst (λ Γ → Term Γ τ) ≡₁ t
|
module Syntactic.Terms.Total where
-- TERMS with a primitive for TOTAL DERIVATIVES
--
-- This module defines the syntax 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 Syntactic.Types
open import Syntactic.Contexts Type
open import Changes
open import ChangeContexts
open import Relation.Binary.PropositionalEquality
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {τ₁ τ₂ Γ} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {τ₁ τ₂ Γ} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
true false : ∀ {Γ} → Term Γ bool
if : ∀ {Γ τ} → (t₁ : Term Γ bool) (t₂ t₃ : Term Γ τ) → Term Γ τ
-- `Δ t` describes how t changes if its free variables or arguments change
Δ : ∀ {Γ τ} → (t : Term Γ τ) → Term (Δ-Context Γ) (Δ-Type τ)
weakenOne : ∀ Γ₁ τ₂ {Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ (τ₂ • Γ₃)) τ
substTerm : ∀ {τ Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Term Γ₁ τ → Term Γ₂ τ
substTerm {τ} {Γ₁} {Γ₂} ≡₁ t = subst (λ Γ → Term Γ τ) ≡₁ t
|
Change order of implicit arguments of term constructors.
|
Change order of implicit arguments of term constructors.
This simplifies extracting the argument type from a term
of function type.
Old-commit-hash: 2742a31a130e9d01c2283f3e7cb7fae6102a4062
|
Agda
|
mit
|
inc-lc/ilc-agda
|
53ffa41592e8de74ad057626e193d26cc8e3b78b
|
Theorem/Groups-Nehemiah.agda
|
Theorem/Groups-Nehemiah.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- About the group structure of integers and bags for Nehemiah plugin.
------------------------------------------------------------------------
module Theorem.Groups-Nehemiah where
open import Structure.Bag.Nehemiah public
open import Relation.Binary.PropositionalEquality
open import Data.Integer
open import Algebra.Structures
n+[m-n]=m : ∀ {n m} → n + (m - n) ≡ m
n+[m-n]=m {n} {m} =
begin
n + (m - n)
≡⟨ cong (λ hole → n + hole) (commutative-int m (- n)) ⟩
n + (- n + m)
≡⟨ sym (associative-int n (- n) m) ⟩
(n - n) + m
≡⟨ cong (λ hole → hole + m) (right-inv-int n) ⟩
(+ 0) + m
≡⟨ left-id-int m ⟩
m
∎ where open ≡-Reasoning
a++[b\\a]=b : ∀ {a b} → a ++ (b \\ a) ≡ b
a++[b\\a]=b {b} {d} = trans
(cong (λ hole → b ++ hole) (commutative-bag d (negateBag b))) (trans
(sym (associative-bag b (negateBag b) d)) (trans
(cong (λ hole → hole ++ d) (right-inv-bag b))
(left-id-bag d)))
4-way-shuffle : ∀ {A : Set} {f} {z a b c d : A}
{{comm-monoid : IsCommutativeMonoid _≡_ f z}} →
f (f a b) (f c d) ≡ f (f a c) (f b d)
4-way-shuffle {f = f} {z = z} {a} {b} {c} {d} {{comm-monoid}} =
let
assoc = associative comm-monoid
cmute = commutative comm-monoid
in
begin
f (f a b) (f c d)
≡⟨ assoc a b (f c d) ⟩
f a (f b (f c d))
≡⟨ cong (f a) (sym (assoc b c d)) ⟩
f a (f (f b c) d)
≡⟨ cong (λ hole → f a (f hole d)) (cmute b c) ⟩
f a (f (f c b) d)
≡⟨ cong (f a) (assoc c b d) ⟩
f a (f c (f b d))
≡⟨ sym (assoc a c (f b d)) ⟩
f (f a c) (f b d)
∎ where open ≡-Reasoning
ab·cd=ac·bd : ∀ {a b c d : Bag} →
(a ++ b) ++ (c ++ d) ≡ (a ++ c) ++ (b ++ d)
ab·cd=ac·bd {a} {b} {c} {d} =
4-way-shuffle {a = a} {b} {c} {d} {{comm-monoid-bag}}
mn·pq=mp·nq : ∀ {m n p q : ℤ} →
(m + n) + (p + q) ≡ (m + p) + (n + q)
mn·pq=mp·nq {m} {n} {p} {q} =
4-way-shuffle {a = m} {n} {p} {q} {{comm-monoid-int}}
inverse-unique : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f a b ≡ z → b ≡ neg a
inverse-unique {f = f} {neg} {z} {a} {b} {{abelian}} ab=z =
let
assoc = associative (IsAbelianGroup.isCommutativeMonoid abelian)
cmute = commutative (IsAbelianGroup.isCommutativeMonoid abelian)
in
begin
b
≡⟨ sym (left-identity abelian b) ⟩
f z b
≡⟨ cong (λ hole → f hole b) (sym (left-inverse abelian a)) ⟩
f (f (neg a) a) b
≡⟨ assoc (neg a) a b ⟩
f (neg a) (f a b)
≡⟨ cong (f (neg a)) ab=z ⟩
f (neg a) z
≡⟨ right-identity abelian (neg a) ⟩
neg a
∎ where
open ≡-Reasoning
eq1 : f (neg a) (f a b) ≡ f (neg a) z
eq1 rewrite ab=z = cong (f (neg a)) refl
distribute-neg : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f (neg a) (neg b) ≡ neg (f a b)
distribute-neg {f = f} {neg} {z} {a} {b} {{abelian}} = inverse-unique
{{abelian}}
(begin
f (f a b) (f (neg a) (neg b))
≡⟨ 4-way-shuffle {{IsAbelianGroup.isCommutativeMonoid abelian}} ⟩
f (f a (neg a)) (f b (neg b))
≡⟨ cong₂ f (inverse a) (inverse b) ⟩
f z z
≡⟨ left-identity abelian z ⟩
z
∎) where
open ≡-Reasoning
inverse = right-inverse abelian
-a·-b=-ab : ∀ {a b : Bag} →
negateBag a ++ negateBag b ≡ negateBag (a ++ b)
-a·-b=-ab {a} {b} = distribute-neg {a = a} {b} {{abelian-bag}}
-m·-n=-mn : ∀ {m n : ℤ} →
(- m) + (- n) ≡ - (m + n)
-m·-n=-mn {m} {n} = distribute-neg {a = m} {n} {{abelian-int}}
[a++b]\\a=b : ∀ {a b} → (a ++ b) \\ a ≡ b
[a++b]\\a=b {b} {d} =
begin
(b ++ d) \\ b
≡⟨ cong (λ hole → hole \\ b) (commutative-bag b d) ⟩
(d ++ b) \\ b
≡⟨ associative-bag d b (negateBag b) ⟩
d ++ (b \\ b)
≡⟨ cong (_++_ d) (right-inv-bag b) ⟩
d ++ emptyBag
≡⟨ right-id-bag d ⟩
d
∎ where open ≡-Reasoning
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- About the group structure of integers and bags for Nehemiah plugin.
------------------------------------------------------------------------
module Theorem.Groups-Nehemiah where
open import Structure.Bag.Nehemiah public
open import Relation.Binary.PropositionalEquality
open import Algebra.Structures
4-way-shuffle : ∀ {A : Set} {f} {z a b c d : A}
{{comm-monoid : IsCommutativeMonoid _≡_ f z}} →
f (f a b) (f c d) ≡ f (f a c) (f b d)
4-way-shuffle {f = f} {z = z} {a} {b} {c} {d} {{comm-monoid}} =
let
assoc = associative comm-monoid
cmute = commutative comm-monoid
in
begin
f (f a b) (f c d)
≡⟨ assoc a b (f c d) ⟩
f a (f b (f c d))
≡⟨ cong (f a) (sym (assoc b c d)) ⟩
f a (f (f b c) d)
≡⟨ cong (λ hole → f a (f hole d)) (cmute b c) ⟩
f a (f (f c b) d)
≡⟨ cong (f a) (assoc c b d) ⟩
f a (f c (f b d))
≡⟨ sym (assoc a c (f b d)) ⟩
f (f a c) (f b d)
∎ where open ≡-Reasoning
open import Data.Integer
n+[m-n]=m : ∀ {n m} → n + (m - n) ≡ m
n+[m-n]=m {n} {m} =
begin
n + (m - n)
≡⟨ cong (λ hole → n + hole) (commutative-int m (- n)) ⟩
n + (- n + m)
≡⟨ sym (associative-int n (- n) m) ⟩
(n - n) + m
≡⟨ cong (λ hole → hole + m) (right-inv-int n) ⟩
(+ 0) + m
≡⟨ left-id-int m ⟩
m
∎ where open ≡-Reasoning
a++[b\\a]=b : ∀ {a b} → a ++ (b \\ a) ≡ b
a++[b\\a]=b {b} {d} = trans
(cong (λ hole → b ++ hole) (commutative-bag d (negateBag b))) (trans
(sym (associative-bag b (negateBag b) d)) (trans
(cong (λ hole → hole ++ d) (right-inv-bag b))
(left-id-bag d)))
ab·cd=ac·bd : ∀ {a b c d : Bag} →
(a ++ b) ++ (c ++ d) ≡ (a ++ c) ++ (b ++ d)
ab·cd=ac·bd {a} {b} {c} {d} =
4-way-shuffle {a = a} {b} {c} {d} {{comm-monoid-bag}}
mn·pq=mp·nq : ∀ {m n p q : ℤ} →
(m + n) + (p + q) ≡ (m + p) + (n + q)
mn·pq=mp·nq {m} {n} {p} {q} =
4-way-shuffle {a = m} {n} {p} {q} {{comm-monoid-int}}
inverse-unique : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f a b ≡ z → b ≡ neg a
inverse-unique {f = f} {neg} {z} {a} {b} {{abelian}} ab=z =
let
assoc = associative (IsAbelianGroup.isCommutativeMonoid abelian)
cmute = commutative (IsAbelianGroup.isCommutativeMonoid abelian)
in
begin
b
≡⟨ sym (left-identity abelian b) ⟩
f z b
≡⟨ cong (λ hole → f hole b) (sym (left-inverse abelian a)) ⟩
f (f (neg a) a) b
≡⟨ assoc (neg a) a b ⟩
f (neg a) (f a b)
≡⟨ cong (f (neg a)) ab=z ⟩
f (neg a) z
≡⟨ right-identity abelian (neg a) ⟩
neg a
∎ where
open ≡-Reasoning
eq1 : f (neg a) (f a b) ≡ f (neg a) z
eq1 rewrite ab=z = cong (f (neg a)) refl
distribute-neg : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f (neg a) (neg b) ≡ neg (f a b)
distribute-neg {f = f} {neg} {z} {a} {b} {{abelian}} = inverse-unique
{{abelian}}
(begin
f (f a b) (f (neg a) (neg b))
≡⟨ 4-way-shuffle {{IsAbelianGroup.isCommutativeMonoid abelian}} ⟩
f (f a (neg a)) (f b (neg b))
≡⟨ cong₂ f (inverse a) (inverse b) ⟩
f z z
≡⟨ left-identity abelian z ⟩
z
∎) where
open ≡-Reasoning
inverse = right-inverse abelian
-a·-b=-ab : ∀ {a b : Bag} →
negateBag a ++ negateBag b ≡ negateBag (a ++ b)
-a·-b=-ab {a} {b} = distribute-neg {a = a} {b} {{abelian-bag}}
-m·-n=-mn : ∀ {m n : ℤ} →
(- m) + (- n) ≡ - (m + n)
-m·-n=-mn {m} {n} = distribute-neg {a = m} {n} {{abelian-int}}
[a++b]\\a=b : ∀ {a b} → (a ++ b) \\ a ≡ b
[a++b]\\a=b {b} {d} =
begin
(b ++ d) \\ b
≡⟨ cong (λ hole → hole \\ b) (commutative-bag b d) ⟩
(d ++ b) \\ b
≡⟨ associative-bag d b (negateBag b) ⟩
d ++ (b \\ b)
≡⟨ cong (_++_ d) (right-inv-bag b) ⟩
d ++ emptyBag
≡⟨ right-id-bag d ⟩
d
∎ where open ≡-Reasoning
|
reorder theorems
|
Groups-Nehemiah: reorder theorems
|
Agda
|
mit
|
inc-lc/ilc-agda
|
8bb712cd1a8c9c9cc0885ef29918d9cc47588189
|
Parametric/Denotation/Evaluation.agda
|
Parametric/Denotation/Evaluation.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Evaluation for languages described in Plotkin style
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
-- this is what we'd like to write.
-- unfortunately termination checker complains.
--
-- ⟦ terms ⟧Terms ρ = map-IVT (λ t → ⟦ t ⟧Term ρ) terms
--
-- so we do explicit pattern matching instead.
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ₁≼Γ₂ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound : ∀ {Γ₁ Γ₂ Σ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(terms : Terms Γ₁ Σ) (ρ : ⟦ Γ₂ ⟧) →
⟦ weaken-terms Γ₁≼Γ₂ terms ⟧Terms ρ ≡ ⟦ terms ⟧Terms (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound ∅ ρ = refl
weaken-terms-sound (t • terms) ρ =
cong₂ _•_ (weaken-sound t ρ) (weaken-terms-sound terms ρ)
weaken-sound {Γ₁≼Γ₂ = Γ₁≼Γ₂} (var x) ρ = weaken-var-sound Γ₁≼Γ₂ x ρ
weaken-sound (app s t) ρ = weaken-sound s ρ ⟨$⟩ weaken-sound t ρ
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound {Γ₁} {Γ₂} {Γ₁≼Γ₂ = Γ₁≼Γ₂} (const {Σ} {τ} c args) ρ =
cong ⟦ c ⟧Const (weaken-terms-sound args ρ)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Standard evaluation (Def. 3.3 and Fig. 4i)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
-- Extension Point: Evaluation of fully applied constants.
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
-- We provide: Evaluation of arbitrary terms.
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
-- this is what we'd like to write.
-- unfortunately termination checker complains.
--
-- ⟦ terms ⟧Terms ρ = map-IVT (λ t → ⟦ t ⟧Term ρ) terms
--
-- so we do explicit pattern matching instead.
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ₁≼Γ₂ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound : ∀ {Γ₁ Γ₂ Σ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(terms : Terms Γ₁ Σ) (ρ : ⟦ Γ₂ ⟧) →
⟦ weaken-terms Γ₁≼Γ₂ terms ⟧Terms ρ ≡ ⟦ terms ⟧Terms (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound ∅ ρ = refl
weaken-terms-sound (t • terms) ρ =
cong₂ _•_ (weaken-sound t ρ) (weaken-terms-sound terms ρ)
weaken-sound {Γ₁≼Γ₂ = Γ₁≼Γ₂} (var x) ρ = weaken-var-sound Γ₁≼Γ₂ x ρ
weaken-sound (app s t) ρ = weaken-sound s ρ ⟨$⟩ weaken-sound t ρ
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound {Γ₁} {Γ₂} {Γ₁≼Γ₂ = Γ₁≼Γ₂} (const {Σ} {τ} c args) ρ =
cong ⟦ c ⟧Const (weaken-terms-sound args ρ)
|
Document P.D.Evaluation.
|
Document P.D.Evaluation.
Old-commit-hash: a346d6ffd091c3041ecb8d087a4171aeffa63576
|
Agda
|
mit
|
inc-lc/ilc-agda
|
7b8de230698933472e580b5ece63a74cf55998a4
|
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}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Const
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)
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
abs₄ (λ g f x Δx → app g (x ⊕σ Δx) ⊝τ app f x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ τ {Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff _ = app₂ diff-term
apply : ∀ τ {Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply _ = app₂ apply-term
infixl 6 apply diff
syntax apply τ x Δx = Δx ⊕₍ τ ₎ x
syntax diff τ x y = x ⊝₍ τ ₎ y
infixl 6 _⊕_ _⊝_
_⊝_ : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
_⊝_ {τ} = diff τ
_⊕_ : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
_⊕_ {τ} = app₂ apply-term
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Terms that operate on changes (Fig. 3).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Const
open ChangeType.Structure Base ΔBase
open import Data.Product
-- Extension point 1: A term for ⊝ on base types.
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
-- Extension point 2: A term for ⊕ on base types.
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)
-- We provide: terms for ⊕ and ⊝ on arbitrary types.
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
abs₄ (λ g f x Δx → app g (x ⊕σ Δx) ⊝τ app f x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ τ {Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff _ = app₂ diff-term
apply : ∀ τ {Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply _ = app₂ apply-term
infixl 6 apply diff
syntax apply τ x Δx = Δx ⊕₍ τ ₎ x
syntax diff τ x y = x ⊝₍ τ ₎ y
infixl 6 _⊕_ _⊝_
_⊝_ : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
_⊝_ {τ} = diff τ
_⊕_ : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
_⊕_ {τ} = app₂ apply-term
|
Document P.C.Term.
|
Document P.C.Term.
Old-commit-hash: 0263f48a6674e209dc10d253b3ffab2671e313cd
|
Agda
|
mit
|
inc-lc/ilc-agda
|
1da8d765d3b45cbc385435bbd6c113c680b6f896
|
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
-- 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₂ diffσ s t
_⊝τ_ = λ s t → app₂ diffτ s t
_⊕σ_ = λ t Δt → app₂ applyσ Δt t
_⊕τ_ = λ t Δt → 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 {τ} {Γ})
|
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₂ (diffσ {Γ}) s t
_⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t
_⊕τ_ = λ {Γ} t Δt → 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 {τ} {Γ})
|
Generalize helper methods.
|
Generalize helper methods.
Before this commit, the helper methods worked for one particular context
that was inferred from the helper's use sites. I want to change the
helper's use suite in the next commit so that the context would no
longer get inferred automatically, so I have to generalize the helper
methods for arbitrary contexts first.
Old-commit-hash: 331ecddb578d8491bb6b65d1312b789d9260ff31
|
Agda
|
mit
|
inc-lc/ilc-agda
|
ef0b3de030e69c13f6abe2f657976fd6056b39d2
|
example1.agda
|
example1.agda
|
module example1 where
open import Data.String.Base using (String)
open import Data.Product
open import Data.List.Base using (List; []; _∷_)
open import Data.Bool.Base
open import Function
open import Control.Process.Type
open import FFI.JS as JS
open import FFI.JS.Proc
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
postulate take-half : String → String
{-# COMPILED_JS take-half function(x) { return x.substring(0,x.length/2); } #-}
postulate drop-half : String → String
{-# COMPILED_JS drop-half function(x) { return x.substring(x.length/2); } #-}
test-value : Value
test-value = object (("array" , array (array [] ∷ array (array [] ∷ []) ∷ [])) ∷
("object" , array (object [] ∷ object (("a", string "b") ∷ []) ∷ [])) ∷
("string" , array (string "" ∷ string "a" ∷ [])) ∷
("number" , array (number zero ∷ number one ∷ [])) ∷
("bool" , array (bool true ∷ bool false ∷ [])) ∷
("null" , array (null ∷ [])) ∷ [])
test =
fromString (JSON-stringify (fromValue test-value))
===
fromString "{\"array\":[[],[[]]],\"object\":[{},{\"a\":\"b\"}],\"string\":[\"\",\"a\"],\"number\":[0,1],\"bool\":[true,false],\"null\":[null]}"
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₁
merge-sort-string : String → String → String
merge-sort-string s₀ s₁ = List▹String (merge-sort-list _≤Char_ (String▹List s₀) (String▹List s₁))
mapJSArray : (JSArray String → JSArray String) → JSValue → JSValue
mapJSArray f v = fromString (onString (join "" ∘ f ∘ split "") v)
reverser : URI → JSProc
reverser d = recv d λ s → send d (mapJSArray JS.reverse s) end
adder : URI → JSProc
adder d = recv d λ s₀ → recv d λ s₁ → send d (s₀ +JS s₁) end
adder-client : URI → JSValue → JSValue → JSProc
adder-client d s₀ s₁ = send d s₀ (send d s₁ (recv d λ _ → end))
module _ (adder-addr reverser-addr : URI)(s : JSValue) where
adder-reverser-client : JSProc
adder-reverser-client =
send reverser-addr s $
send adder-addr s $
recv reverser-addr λ rs →
send adder-addr rs $
recv adder-addr λ res →
end
str-sorter₀ : URI → JSProc
str-sorter₀ d = recv d λ s → send d (mapJSArray sort s) end
str-sorter-client : ∀ {D} → D → JSValue → Proc D JSValue
str-sorter-client d s = send d s $ recv d λ _ → end
module _ (upstream helper₀ helper₁ : URI) where
str-merger : JSProc
str-merger =
recv upstream λ s →
send helper₀ (fromString (onString take-half s)) $
send helper₁ (fromString (onString drop-half s)) $
recv helper₀ λ ss₀ →
recv helper₁ λ ss₁ →
send upstream (fromString (onString (onString merge-sort-string ss₀) ss₁))
end
dyn-merger : URI → (URI → JSProc) → JSProc
dyn-merger upstream helper =
spawn helper λ helper₀ →
spawn helper λ helper₁ →
str-merger upstream helper₀ helper₁
str-sorter₁ : URI → JSProc
str-sorter₁ upstream = dyn-merger upstream str-sorter₀
str-sorter₂ : URI → JSProc
str-sorter₂ upstream = dyn-merger upstream str-sorter₁
stringifier : URI → JSProc
stringifier d = recv d λ v → send d (fromString (JSON-stringify v)) end
stringifier-client : ∀ {D} → D → JSValue → Proc D JSValue
stringifier-client d v = send d v $ recv d λ _ → end
main : JS!
main =
Console.log "Hey!" >> assert test >>
Process.argv !₁ λ argv → Console.log ("argv=" ++ join " " argv) >>
Console.log "server(adder):" >> server "127.0.0.1" "1337" adder !₁ λ adder-uri →
Console.log "client(adderclient):" >>
client (adder-client adder-uri (fromString "Hello ") (fromString "World!")) >>
client (adder-client adder-uri (fromString "Bonjour ") (fromString "monde!")) >>
Console.log "server(reverser):" >>
server "127.0.0.1" "1338" reverser !₁ λ reverser-uri →
Console.log "client(adder-reverser-client):" >>
client (adder-reverser-client adder-uri reverser-uri (fromString "red")) >>
server "127.0.0.1" "1342" str-sorter₂ !₁ λ str-sorter₂-uri →
Console.log "str-sorter-client:" >>
client (str-sorter-client str-sorter₂-uri (fromString "Something to be sorted!")) >>
server "127.0.0.1" "1343" stringifier !₁ λ stringifier-uri →
client (stringifier-client stringifier-uri (fromValue test-value)) >>
FS.readFile "README.md" nullJS !₂ λ err dat →
Console.log ("README.md, length is " ++ Number▹String (length (castString dat))) >>
Console.log "Bye!" >>
end
-- -}
-- -}
-- -}
-- -}
|
module example1 where
open import Data.String.Base using (String)
open import Data.Product
open import Data.List.Base using (List; []; _∷_)
open import Data.Bool.Base
open import Function
open import Control.Process.Type
open import FFI.JS as JS
open import FFI.JS.Proc
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
take-half : String → String
take-half s = substring s 0N (length s / 2N)
drop-half : String → String
drop-half s = substring1 s (length s / 2N)
test-value : Value
test-value = object (("array" , array (array [] ∷ array (array [] ∷ []) ∷ [])) ∷
("object" , array (object [] ∷ object (("a", string "b") ∷ []) ∷ [])) ∷
("string" , array (string "" ∷ string "a" ∷ [])) ∷
("number" , array (number 0N ∷ number 1N ∷ [])) ∷
("bool" , array (bool true ∷ bool false ∷ [])) ∷
("null" , array (null ∷ [])) ∷ [])
test =
fromString (JSON-stringify (fromValue test-value))
===
fromString "{\"array\":[[],[[]]],\"object\":[{},{\"a\":\"b\"}],\"string\":[\"\",\"a\"],\"number\":[0,1],\"bool\":[true,false],\"null\":[null]}"
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₁
merge-sort-string : String → String → String
merge-sort-string s₀ s₁ = List▹String (merge-sort-list _≤Char_ (String▹List s₀) (String▹List s₁))
mapJSArray : (JSArray String → JSArray String) → JSValue → JSValue
mapJSArray f v = fromString (join "" ∘ f ∘ split "" ∘ castString $ v)
reverser : URI → JSProc
reverser d = recv d λ s → send d (mapJSArray JS.reverse s) end
adder : URI → JSProc
adder d = recv d λ s₀ → recv d λ s₁ → send d (s₀ +JS s₁) end
adder-client : URI → JSValue → JSValue → JSProc
adder-client d s₀ s₁ = send d s₀ (send d s₁ (recv d λ _ → end))
module _ (adder-addr reverser-addr : URI)(s : JSValue) where
adder-reverser-client : JSProc
adder-reverser-client =
send reverser-addr s $
send adder-addr s $
recv reverser-addr λ rs →
send adder-addr rs $
recv adder-addr λ res →
end
str-sorter₀ : URI → JSProc
str-sorter₀ d = recv d λ s → send d (mapJSArray sort s) end
str-sorter-client : ∀ {D} → D → JSValue → Proc D JSValue
str-sorter-client d s = send d s $ recv d λ _ → end
module _ (upstream helper₀ helper₁ : URI) where
str-merger : JSProc
str-merger =
recv upstream λ s →
send helper₀ (fromString (take-half (castString s))) $
send helper₁ (fromString (drop-half (castString s))) $
recv helper₀ λ ss₀ →
recv helper₁ λ ss₁ →
send upstream (fromString (merge-sort-string (castString ss₀) (castString ss₁)))
end
dyn-merger : URI → (URI → JSProc) → JSProc
dyn-merger upstream helper =
spawn helper λ helper₀ →
spawn helper λ helper₁ →
str-merger upstream helper₀ helper₁
str-sorter₁ : URI → JSProc
str-sorter₁ upstream = dyn-merger upstream str-sorter₀
str-sorter₂ : URI → JSProc
str-sorter₂ upstream = dyn-merger upstream str-sorter₁
stringifier : URI → JSProc
stringifier d = recv d λ v → send d (fromString (JSON-stringify v)) end
stringifier-client : ∀ {D} → D → JSValue → Proc D JSValue
stringifier-client d v = send d v $ recv d λ _ → end
main : JS!
main =
Console.log "Hey!" >> assert test >>
Process.argv !₁ λ argv → Console.log ("argv=" ++ join " " argv) >>
Console.log "server(adder):" >> server "127.0.0.1" "1337" adder !₁ λ adder-uri →
Console.log "client(adderclient):" >>
client (adder-client adder-uri (fromString "Hello ") (fromString "World!")) >>
client (adder-client adder-uri (fromString "Bonjour ") (fromString "monde!")) >>
Console.log "server(reverser):" >>
server "127.0.0.1" "1338" reverser !₁ λ reverser-uri →
Console.log "client(adder-reverser-client):" >>
client (adder-reverser-client adder-uri reverser-uri (fromString "red")) >>
server "127.0.0.1" "1339" str-sorter₀ !₁ λ str-sorter₀-uri →
Console.log "str-sorter-client for str-sorter₀:" >>
client (str-sorter-client str-sorter₀-uri (fromString "Something to be sorted!")) >>
server "127.0.0.1" "1342" str-sorter₂ !₁ λ str-sorter₂-uri →
Console.log "str-sorter-client:" >>
client (str-sorter-client str-sorter₂-uri (fromString "Something to be sorted!")) >>
server "127.0.0.1" "1343" stringifier !₁ λ stringifier-uri →
client (stringifier-client stringifier-uri (fromValue test-value)) >>
FS.readFile "README.md" nullJS !₂ λ err dat →
Console.log ("README.md, length is " ++ Number▹String (length (toString dat))) >>
Console.log "Bye!"
-- -}
-- -}
-- -}
-- -}
|
Update example1
|
Update example1
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-libjs
|
b3651f102b65a31ef5dd4d541002915b3e5cc9fe
|
Base/Data/DependentList.agda
|
Base/Data/DependentList.agda
|
module Base.Data.DependentList where
open import Data.List.All public
using
( head
; tail
; map
; tabulate
)
renaming
( All to DependentList
; _∷_ to _•_
; [] to ∅
)
zipWith : ∀ {a p q r} {A : Set a} {P : A → Set p} {Q : A → Set q} {R : A → Set r} →
(f : {a : A} → P a → Q a → R a) →
∀ {xs} → DependentList P xs → DependentList Q xs → DependentList R xs
zipWith f ∅ ∅ = ∅
zipWith f (p • ps) (q • qs) = f p q • zipWith f ps qs
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Reexport Data.List.All from the standard library.
--
-- At one point, we reinvented Data.List.All from the Agda
-- standard library, under the name dependent list. We later
-- replaced our reinvention by this adapter module that just
-- exports the standard library's version with partly different
-- names.
------------------------------------------------------------------------
module Base.Data.DependentList where
open import Data.List.All public
using
( head
; tail
; map
; tabulate
)
renaming
( All to DependentList
; _∷_ to _•_
; [] to ∅
)
-- Maps a binary function over two dependent lists.
-- Should this be in the Agda standard library?
zipWith : ∀ {a p q r} {A : Set a} {P : A → Set p} {Q : A → Set q} {R : A → Set r} →
(f : {a : A} → P a → Q a → R a) →
∀ {xs} → DependentList P xs → DependentList Q xs → DependentList R xs
zipWith f ∅ ∅ = ∅
zipWith f (p • ps) (q • qs) = f p q • zipWith f ps qs
|
Improve commentary in Base.Data.DependentList.
|
Improve commentary in Base.Data.DependentList.
Old-commit-hash: 7a4d9aea2f3574b1b3f21022bced5f9ef112264c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
eea966329ff9f953b7330cd87ef1188ffe40f0f0
|
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.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
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.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- 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
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- 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?).
open import UNDEFINED
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- TODO No caching. Uh??? What about the intermediate results of the
-- arguments? Ah, they're values, so they aren't interesting (or they're
-- already cached). Yu-uh!
-- XXX We need a caching ValBase, so the caching type semantics needs to move.
⟦ vConst c args ⟧ValTermCache ρ = reveal UNDEFINED -- {!⟦ c ⟧ValBase !}
-- The real deal, finally.
-- TODO Do caching. No! Delegate to caching version of the base semantics!
⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED -- {!⟦ c ⟧CompBase (⟦ args ⟧Vals ?)!}
-- 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 (cConstB c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstVB c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstVB2 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.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
open import Base.Denotation.Notation
-- Extension Point: Evaluation of fully applied constants.
ValStructure : Set
ValStructure = ∀ {Σ τ} → ValConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
CompStructure : Set
CompStructure = ∀ {Σ τ} → CompConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
module Structure
(⟦_⟧ValBaseTermCache : ValStructure)
(⟦_⟧CompBaseTermCache : CompStructure)
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.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- 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
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- 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?).
open import UNDEFINED
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- No caching, because the arguments are values, so evaluating them does not
-- produce intermediate results.
⟦ vConst c args ⟧ValTermCache ρ = ⟦ c ⟧ValBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- The only caching is done by the interpretation of the constant (because the
-- arguments are values so need no caching).
⟦_⟧CompTermCache (cConst c args) ρ = ⟦ c ⟧CompBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- 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 (cConstB c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstVB c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstVB2 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 ρ)
|
Correct caching for constants (by delegation)
|
Correct caching for constants (by delegation)
This defines the plugin interface for constants (with a type that makes
semantic sense), and delegates to that, while stipulating:
* what are the appropriate types;
* that constant arguments needn't be cached because they are values.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
308d57f744384b21a2811f278b455dfb952dd324
|
Syntax/Language/Popl14.agda
|
Syntax/Language/Popl14.agda
|
module Syntax.Language.Popl14 where
open import Syntax.Term.Popl14
open import Syntax.Context.Plotkin Popl14-type
open import Data.Integer
|
module Syntax.Language.Popl14 where
open import Syntax.Term.Popl14
open import Syntax.Context.Plotkin Popl14-type
open import Data.Integer
import Syntax.Language.Calculus as Calc
ΔConst : ∀ {Γ Σ τ} →
Const Σ τ →
Term Γ
(internalizeContext
(Calc.ΔContext′ (Calc.Type Popl14-type) ΔType Σ) (ΔType τ))
-- These helpers hide deBrujin indexes, providing an interface which is as
-- comfortable as HOAS. This should be generalized and moved to Syntax.Term.Plotkin
ΔConst (intlit-c n) = intlit (+ 0)
ΔConst add-c = abs₄ (λ x Δx y Δy → add Δx Δy)
ΔConst minus-c = abs₂ (λ x Δx → minus Δx)
ΔConst empty-c = empty
ΔConst insert-c = abs₄ (λ x Δx y Δy → insert (x ⊕ Δx) (y ⊕ Δy) ⊝ insert x y)
ΔConst union-c = abs₄ (λ x Δx y Δy → union Δx Δy)
ΔConst negate-c = abs₂ (λ x Δx → negate Δx)
ΔConst flatmap-c = abs₄ (λ x Δx y Δy → flatmap (x ⊕ Δx) (y ⊕ Δy) ⊝ flatmap x y)
ΔConst sum-c = abs₂ (λ x Δx → sum Δx)
Popl14 = Calc.calculus-with
Popl14-type
Const
ΔType
ΔConst
|
Define ΔConst for Popl14 and fill in calculus-with
|
Define ΔConst for Popl14 and fill in calculus-with
Old-commit-hash: c1cdbc71d45afdb85da9ac22b3207d1cb03e0bf7
|
Agda
|
mit
|
inc-lc/ilc-agda
|
5ea0220b999f1d1ef9f072ca4bedfb82dc747ca1
|
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
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
open import Function
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.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-symm ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
≙-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
≡⟨ 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
-- 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₁) $ proof dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ cong (λ f → f (x ⊞ dx₂)) $ proof 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
≙⟨ ≙-symm (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- Unused, but just to test that inference works.
lemma : nil f ≙ dg
lemma = ≙-symm (derivative-is-nil dg fdg)
|
Use a record for doe, as suggested by Tillmann
|
Use a record for doe, as suggested by Tillmann
Old-commit-hash: f1a0e47b6fd1c171f165e2a9ba96fd258042a8ae
|
Agda
|
mit
|
inc-lc/ilc-agda
|
de617d2ad76260052f9d6ecbf3c054fc7eb183ef
|
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
open import Data.Integer
open import Structure.Bag.Popl14
-- `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ρ
|
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ρ
|
Remove unused imports violating architectural constraints
|
Agda: Remove unused imports violating architectural constraints
The parametric formalization shouldn't import stuff specific to the
POPL14 calculus.
Old-commit-hash: 30c3071e33a99422b2c878f7bca7937e8b1c216f
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b0acfde8e76ff27fb9d7b8609c3d7acf1ccf1ef2
|
PLDI14-List-of-Theorems.agda
|
PLDI14-List-of-Theorems.agda
|
module PLDI14-List-of-Theorems where
-- List of theorems in PLDI submission
--
-- For hints about installation and execution, please refer
-- to README.agda.
--
-- Agda modules corresponding to definitions, lemmas and theorems
-- are listed here with the most important names. For example,
-- after this file type checks (C-C C-L), placing the cursor
-- on the purple "Base.Change.Algebra" and pressing M-. will
-- bring you to the file where change structures are defined.
-- The name for change structures in that file is
-- "ChangeAlgebra", given in the using-clause.
-- Definition 2.1 (Change structures)
open import Base.Change.Algebra using (ChangeAlgebra)
---- Carrier in record ChangeAlgebra --(a)
open Base.Change.Algebra.ChangeAlgebra using (Change) --(b)
open Base.Change.Algebra.ChangeAlgebra using (update) --(c)
open Base.Change.Algebra.ChangeAlgebra using (diff) --(d)
open Base.Change.Algebra.IsChangeAlgebra using (update-diff)--(e)
-- Definition 2.2 (Nil change)
-- IsChangeAlgebra.nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Lemma 2.3 (Behavior of nil)
-- IsChangeAlgebra.update-nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Definition 2.4 (Derivatives)
open Base.Change.Algebra using (Derivative)
-- Definition 2.5 (Carrier set of function changes)
open Base.Change.Algebra.FunctionChanges
-- Definition 2.6 (Operations on function changes)
-- ChangeAlgebra.update FunctionChanges.changeAlgebra
-- ChangeAlgebra.diff FunctionChanges.changeAlgebra
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Theorem 2.7 (Function changes form a change structure)
-- (In Agda, the proof of Theorem 2.7 has to be included in the
-- definition of function changes, here
-- FunctionChanges.changeAlgebra.)
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Lemma 2.8 (Incrementalization)
open Base.Change.Algebra.FunctionChanges using (incrementalization)
-- Theorem 2.9 (Nil changes are derivatives)
open Base.Change.Algebra.FunctionChanges using (nil-is-derivative)
-- Definition 3.1 (Domains)
import Parametric.Denotation.Value
open Parametric.Denotation.Value.Structure using (⟦_⟧Type)
-- Definition 3.2 (Environments)
open import Base.Denotation.Environment using (⟦_⟧Context)
-- Definition 3.3 (Evaluation)
import Parametric.Denotation.Evaluation
open Parametric.Denotation.Evaluation.Structure using (⟦_⟧Term)
-- Definition 3.4 (Changes)
-- Definition 3.5 (Change environments)
import Parametric.Change.Validity
open Parametric.Change.Validity.Structure using (change-algebra)
open Parametric.Change.Validity.Structure using (environment-changes)
-- Definition 3.6 (Change semantics)
-- Lemma 3.7 (Change semantics is the derivative of semantics)
import Parametric.Change.Specification
open Parametric.Change.Specification.Structure using (⟦_⟧Δ)
open Parametric.Change.Specification.Structure using (correctness)
-- Definition 3.8 (Erasure)
-- Lemma 3.9 (The erased version of a change is almost the same)
import Parametric.Change.Implementation
open Parametric.Change.Implementation.Structure using (_≈_)
open Parametric.Change.Implementation.Structure using (carry-over)
-- Lemma 3.10 (⟦ t ⟧Δ erases to Derive(t))
-- Theorem 3.11 (Correctness of differentiation)
import Parametric.Change.Correctness
open Parametric.Change.Correctness.Structure using (derive-correct-closed)
open Parametric.Change.Correctness.Structure using (main-theorem)
|
module PLDI14-List-of-Theorems where
-- List of theorems in PLDI submission
--
-- For hints about installation and execution, please refer
-- to README.agda.
--
-- Agda modules corresponding to definitions, lemmas and theorems
-- are listed here with the most important names. For example,
-- after this file type checks (C-C C-L), placing the cursor
-- on the purple "Base.Change.Algebra" and pressing M-. will
-- bring you to the file where change structures are defined.
-- The name for change structures in that file is
-- "ChangeAlgebra", given in the using-clause.
-- Definition 2.1 (Change structures)
open import Base.Change.Algebra using (ChangeAlgebra)
---- Carrier in record ChangeAlgebra --(a)
open Base.Change.Algebra.ChangeAlgebra using (Change) --(b)
open Base.Change.Algebra.ChangeAlgebra using (update) --(c)
open Base.Change.Algebra.ChangeAlgebra using (diff) --(d)
open Base.Change.Algebra.IsChangeAlgebra using (update-diff)--(e)
-- Definition 2.2 (Nil change)
-- IsChangeAlgebra.nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Lemma 2.3 (Behavior of nil)
-- IsChangeAlgebra.update-nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Lemma 2.5 (Behavior of derivatives on nil)
open import Base.Change.Equivalence using (deriv-zero)
-- Definition 2.4 (Derivatives)
open Base.Change.Algebra using (Derivative)
-- Definition 2.6 (Carrier set of function changes)
open Base.Change.Algebra.FunctionChanges
-- Definition 2.7 (Operations on function changes)
-- ChangeAlgebra.update FunctionChanges.changeAlgebra
-- ChangeAlgebra.diff FunctionChanges.changeAlgebra
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Theorem 2.8 (Function changes form a change structure)
-- (In Agda, the proof of Theorem 2.8 has to be included in the
-- definition of function changes, here
-- FunctionChanges.changeAlgebra.)
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Theorem 2.9 (Incrementalization)
open Base.Change.Algebra.FunctionChanges using (incrementalization)
-- Theorem 2.10 (Nil changes are derivatives)
open Base.Change.Algebra.FunctionChanges using (nil-is-derivative)
-- Definition 3.1 (Domains)
import Parametric.Denotation.Value
open Parametric.Denotation.Value.Structure using (⟦_⟧Type)
-- Definition 3.2 (Environments)
open import Base.Denotation.Environment using (⟦_⟧Context)
-- Definition 3.3 (Evaluation)
import Parametric.Denotation.Evaluation
open Parametric.Denotation.Evaluation.Structure using (⟦_⟧Term)
-- Definition 3.4 (Changes)
-- Definition 3.5 (Change environments)
import Parametric.Change.Validity
open Parametric.Change.Validity.Structure using (change-algebra)
open Parametric.Change.Validity.Structure using (environment-changes)
-- Definition 3.6 (Change semantics)
-- Lemma 3.7 (Change semantics is the derivative of semantics)
import Parametric.Change.Specification
open Parametric.Change.Specification.Structure using (⟦_⟧Δ)
open Parametric.Change.Specification.Structure using (correctness)
-- Definition 3.8 (Erasure)
-- Lemma 3.9 (The erased version of a change is almost the same)
import Parametric.Change.Implementation
open Parametric.Change.Implementation.Structure using (_≈_)
open Parametric.Change.Implementation.Structure using (carry-over)
-- Lemma 3.10 (⟦ t ⟧Δ erases to Derive(t))
-- Theorem 3.11 (Correctness of differentiation)
import Parametric.Change.Correctness
open Parametric.Change.Correctness.Structure using (derive-correct-closed)
open Parametric.Change.Correctness.Structure using (main-theorem)
|
update PLDI14-List-of-Theorems.agda for consistency with #4
|
update PLDI14-List-of-Theorems.agda for consistency with #4
Camera-ready version inserts a "lemma 2.5", which means math items
2.5--2.9 should be renumbered 2.6--2.10.
Relevant commits:
23339dd5a9e56c3c31d42252c4020b716ec77725
0c1e97fbf3b62bddf23559545a4c801b4189cb29
|
Agda
|
mit
|
inc-lc/ilc-agda
|
8fbacc398d3d884274c0a5d0564e3802b8ff356b
|
lib/Explore/Dice.agda
|
lib/Explore/Dice.agda
|
{-# OPTIONS --without-K #-}
open import Type
open import Data.Fin using (Fin; zero; suc; #_)
import Function.Inverse.NP as Inv
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
open Inv using (_↔_)
open import Explore.Core
open import Explore.Properties
open import Explore.Explorable
open import Explore.Universe
module Explore.Dice where
data Dice : ★₀ where
⚀ ⚁ ⚂ ⚃ ⚄ ⚅ : Dice
module ByHand where
exploreDice : ∀ {m} → Explore m Dice
exploreDice ε _∙_ f = f ⚀ ∙ (f ⚁ ∙ (f ⚂ ∙ (f ⚃ ∙ (f ⚄ ∙ f ⚅))))
exploreDice-ind : ∀ {m p} → ExploreInd p (exploreDice {m})
exploreDice-ind P ε _∙_ f = f ⚀ ∙ (f ⚁ ∙ (f ⚂ ∙ (f ⚃ ∙ (f ⚄ ∙ f ⚅))))
open Explorable₀ exploreDice-ind public using () renaming (sum to sumDice; product to productDice)
open Explorable₁₀ exploreDice-ind public using () renaming (reify to reifyDice)
open Explorable₁₁ exploreDice-ind public using () renaming (unfocus to unfocusDice)
Dice↔Fin6 : Dice ↔ Fin 6
Dice↔Fin6 = Inv.inverses (⇒) (⇐) ⇐⇒ ⇒⇐
module Dice↔Fin6 where
S = Dice
T = Fin 6
⇒ : S → T
⇒ ⚀ = # 0
⇒ ⚁ = # 1
⇒ ⚂ = # 2
⇒ ⚃ = # 3
⇒ ⚄ = # 4
⇒ ⚅ = # 5
⇐ : T → S
⇐ zero = ⚀
⇐ (suc zero) = ⚁
⇐ (suc (suc zero)) = ⚂
⇐ (suc (suc (suc zero))) = ⚃
⇐ (suc (suc (suc (suc zero)))) = ⚄
⇐ (suc (suc (suc (suc (suc zero))))) = ⚅
⇐ (suc (suc (suc (suc (suc (suc ()))))))
⇐⇒ : ∀ x → ⇐ (⇒ x) ≡ x
⇐⇒ ⚀ = refl
⇐⇒ ⚁ = refl
⇐⇒ ⚂ = refl
⇐⇒ ⚃ = refl
⇐⇒ ⚄ = refl
⇐⇒ ⚅ = refl
⇒⇐ : ∀ x → ⇒ (⇐ x) ≡ x
⇒⇐ zero = refl
⇒⇐ (suc zero) = refl
⇒⇐ (suc (suc zero)) = refl
⇒⇐ (suc (suc (suc zero))) = refl
⇒⇐ (suc (suc (suc (suc zero)))) = refl
⇒⇐ (suc (suc (suc (suc (suc zero))))) = refl
⇒⇐ (suc (suc (suc (suc (suc (suc ()))))))
open Explore.Universe.Isomorphism (FinU 6) (Inv.sym Dice↔Fin6 Inv.∘ FinU↔Fin 6)
public
renaming ( isoᵉ to Diceᵉ
; isoⁱ to Diceⁱ
; isoˡ to Diceˡ
; isoᶠ to Diceᶠ
; isoˢ to Diceˢ
; isoᵖ to Diceᵖ
; isoʳ to Diceʳ
; isoᵘ to Diceᵘ
; isoˢ-ok to Diceˢ-ok
; isoˢ-stableUnder to Diceˢ-stableUnder
; μiso to μDice
)
|
{-# OPTIONS --without-K #-}
open import Type
open import Data.Fin using (Fin; zero; suc; #_)
import Function.Inverse.NP as Inv
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
open Inv using (_↔_)
open import Explore.Core
open import Explore.Properties
open import Explore.Explorable
open import Explore.Universe
module Explore.Dice where
data Dice : ★₀ where
⚀ ⚁ ⚂ ⚃ ⚄ ⚅ : Dice
module ByHand where
exploreDice : ∀ {m} → Explore m Dice
exploreDice ε _∙_ f = f ⚀ ∙ (f ⚁ ∙ (f ⚂ ∙ (f ⚃ ∙ (f ⚄ ∙ f ⚅))))
exploreDice-ind : ∀ {m p} → ExploreInd p (exploreDice {m})
exploreDice-ind P ε _∙_ f = f ⚀ ∙ (f ⚁ ∙ (f ⚂ ∙ (f ⚃ ∙ (f ⚄ ∙ f ⚅))))
open Explorable₀ exploreDice-ind public using () renaming (sum to sumDice; product to productDice)
open Explorable₁₀ exploreDice-ind public using () renaming (reify to reifyDice)
open Explorable₁₁ exploreDice-ind public using () renaming (unfocus to unfocusDice)
Dice↔Fin6 : Dice ↔ Fin 6
Dice↔Fin6 = Inv.inverses (⇒) (⇐) ⇐⇒ ⇒⇐
module Dice↔Fin6 where
S = Dice
T = Fin 6
⇒ : S → T
⇒ ⚀ = # 0
⇒ ⚁ = # 1
⇒ ⚂ = # 2
⇒ ⚃ = # 3
⇒ ⚄ = # 4
⇒ ⚅ = # 5
⇐ : T → S
⇐ zero = ⚀
⇐ (suc zero) = ⚁
⇐ (suc (suc zero)) = ⚂
⇐ (suc (suc (suc zero))) = ⚃
⇐ (suc (suc (suc (suc zero)))) = ⚄
⇐ (suc (suc (suc (suc (suc zero))))) = ⚅
⇐ (suc (suc (suc (suc (suc (suc ()))))))
⇐⇒ : ∀ x → ⇐ (⇒ x) ≡ x
⇐⇒ ⚀ = refl
⇐⇒ ⚁ = refl
⇐⇒ ⚂ = refl
⇐⇒ ⚃ = refl
⇐⇒ ⚄ = refl
⇐⇒ ⚅ = refl
⇒⇐ : ∀ x → ⇒ (⇐ x) ≡ x
⇒⇐ zero = refl
⇒⇐ (suc zero) = refl
⇒⇐ (suc (suc zero)) = refl
⇒⇐ (suc (suc (suc zero))) = refl
⇒⇐ (suc (suc (suc (suc zero)))) = refl
⇒⇐ (suc (suc (suc (suc (suc zero))))) = refl
⇒⇐ (suc (suc (suc (suc (suc (suc ()))))))
-- By using FinU' instead of FinU one get a special case for Fin 1 thus avoiding
-- a final ε in the exploration function.
open Explore.Universe.Isomorphism (FinU' 6) (Inv.sym Dice↔Fin6 Inv.∘ FinU'↔Fin 6)
public
renaming ( isoᵉ to Diceᵉ
; isoⁱ to Diceⁱ
; isoˡ to Diceˡ
; isoᶠ to Diceᶠ
; isoˢ to Diceˢ
; isoᵖ to Diceᵖ
; isoʳ to Diceʳ
; isoᵘ to Diceᵘ
; isoˢ-ok to Diceˢ-ok
; isoˢ-stableUnder to Diceˢ-stableUnder
; μiso to μDice
)
module _ {m} where
open ByHand
_≡ᵉ_ : (e₀ e₁ : Explore m Dice) → ★_ _
_≡ᵉ_ = _≡_
same-as-by-hand : exploreDice ≡ᵉ Diceᵉ
same-as-by-hand = refl
|
use the second flavor of Fin exploration to show that it behave as expected
|
Dice: use the second flavor of Fin exploration to show that it behave as expected
|
Agda
|
bsd-3-clause
|
crypto-agda/explore
|
c691b85f8bfd7f9b4a97533c4013b53d694c2eb0
|
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)
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 more dead comments
|
Drop more dead comments
|
Agda
|
mit
|
inc-lc/ilc-agda
|
c1960f9f07f5b25dbb7225149d30534a5c8f3464
|
Theorem/Groups-Nehemiah.agda
|
Theorem/Groups-Nehemiah.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- About the group structure of integers and bags for Nehemiah plugin.
------------------------------------------------------------------------
module Theorem.Groups-Nehemiah where
open import Structure.Bag.Nehemiah public
open import Relation.Binary.PropositionalEquality
open import Algebra.Structures
4-way-shuffle : ∀ {A : Set} {f} {z a b c d : A}
{{comm-monoid : IsCommutativeMonoid _≡_ f z}} →
f (f a b) (f c d) ≡ f (f a c) (f b d)
4-way-shuffle {f = f} {z = z} {a} {b} {c} {d} {{comm-monoid}} =
let
assoc = associative comm-monoid
cmute = commutative comm-monoid
in
begin
f (f a b) (f c d)
≡⟨ assoc a b (f c d) ⟩
f a (f b (f c d))
≡⟨ cong (f a) (sym (assoc b c d)) ⟩
f a (f (f b c) d)
≡⟨ cong (λ hole → f a (f hole d)) (cmute b c) ⟩
f a (f (f c b) d)
≡⟨ cong (f a) (assoc c b d) ⟩
f a (f c (f b d))
≡⟨ sym (assoc a c (f b d)) ⟩
f (f a c) (f b d)
∎ where open ≡-Reasoning
open import Data.Integer
n+[m-n]=m : ∀ {n m} → n + (m - n) ≡ m
n+[m-n]=m {n} {m} =
begin
n + (m - n)
≡⟨ cong (λ hole → n + hole) (commutative-int m (- n)) ⟩
n + (- n + m)
≡⟨ sym (associative-int n (- n) m) ⟩
(n - n) + m
≡⟨ cong (λ hole → hole + m) (right-inv-int n) ⟩
(+ 0) + m
≡⟨ left-id-int m ⟩
m
∎ where open ≡-Reasoning
a++[b\\a]=b : ∀ {a b} → a ++ (b \\ a) ≡ b
a++[b\\a]=b {b} {d} = trans
(cong (λ hole → b ++ hole) (commutative-bag d (negateBag b))) (trans
(sym (associative-bag b (negateBag b) d)) (trans
(cong (λ hole → hole ++ d) (right-inv-bag b))
(left-id-bag d)))
ab·cd=ac·bd : ∀ {a b c d : Bag} →
(a ++ b) ++ (c ++ d) ≡ (a ++ c) ++ (b ++ d)
ab·cd=ac·bd {a} {b} {c} {d} =
4-way-shuffle {a = a} {b} {c} {d} {{comm-monoid-bag}}
mn·pq=mp·nq : ∀ {m n p q : ℤ} →
(m + n) + (p + q) ≡ (m + p) + (n + q)
mn·pq=mp·nq {m} {n} {p} {q} =
4-way-shuffle {a = m} {n} {p} {q} {{comm-monoid-int}}
inverse-unique : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f a b ≡ z → b ≡ neg a
inverse-unique {f = f} {neg} {z} {a} {b} {{abelian}} ab=z =
let
assoc = associative (IsAbelianGroup.isCommutativeMonoid abelian)
cmute = commutative (IsAbelianGroup.isCommutativeMonoid abelian)
in
begin
b
≡⟨ sym (left-identity abelian b) ⟩
f z b
≡⟨ cong (λ hole → f hole b) (sym (left-inverse abelian a)) ⟩
f (f (neg a) a) b
≡⟨ assoc (neg a) a b ⟩
f (neg a) (f a b)
≡⟨ cong (f (neg a)) ab=z ⟩
f (neg a) z
≡⟨ right-identity abelian (neg a) ⟩
neg a
∎ where
open ≡-Reasoning
eq1 : f (neg a) (f a b) ≡ f (neg a) z
eq1 rewrite ab=z = cong (f (neg a)) refl
distribute-neg : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f (neg a) (neg b) ≡ neg (f a b)
distribute-neg {f = f} {neg} {z} {a} {b} {{abelian}} = inverse-unique
{{abelian}}
(begin
f (f a b) (f (neg a) (neg b))
≡⟨ 4-way-shuffle {{IsAbelianGroup.isCommutativeMonoid abelian}} ⟩
f (f a (neg a)) (f b (neg b))
≡⟨ cong₂ f (inverse a) (inverse b) ⟩
f z z
≡⟨ left-identity abelian z ⟩
z
∎) where
open ≡-Reasoning
inverse = right-inverse abelian
-a·-b=-ab : ∀ {a b : Bag} →
negateBag a ++ negateBag b ≡ negateBag (a ++ b)
-a·-b=-ab {a} {b} = distribute-neg {a = a} {b} {{abelian-bag}}
-m·-n=-mn : ∀ {m n : ℤ} →
(- m) + (- n) ≡ - (m + n)
-m·-n=-mn {m} {n} = distribute-neg {a = m} {n} {{abelian-int}}
[a++b]\\a=b : ∀ {a b} → (a ++ b) \\ a ≡ b
[a++b]\\a=b {b} {d} =
begin
(b ++ d) \\ b
≡⟨ cong (λ hole → hole \\ b) (commutative-bag b d) ⟩
(d ++ b) \\ b
≡⟨ associative-bag d b (negateBag b) ⟩
d ++ (b \\ b)
≡⟨ cong (_++_ d) (right-inv-bag b) ⟩
d ++ emptyBag
≡⟨ right-id-bag d ⟩
d
∎ where open ≡-Reasoning
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- About the group structure of integers and bags for Nehemiah plugin.
------------------------------------------------------------------------
module Theorem.Groups-Nehemiah where
open import Structure.Bag.Nehemiah public
open import Relation.Binary.PropositionalEquality
open import Algebra.Structures
4-way-shuffle : ∀ {A : Set} {f} {z a b c d : A}
{{comm-monoid : IsCommutativeMonoid _≡_ f z}} →
f (f a b) (f c d) ≡ f (f a c) (f b d)
4-way-shuffle {f = f} {z = z} {a} {b} {c} {d} {{comm-monoid}} =
let
assoc = associative comm-monoid
cmute = commutative comm-monoid
in
begin
f (f a b) (f c d)
≡⟨ assoc a b (f c d) ⟩
f a (f b (f c d))
≡⟨ cong (f a) (sym (assoc b c d)) ⟩
f a (f (f b c) d)
≡⟨ cong (λ hole → f a (f hole d)) (cmute b c) ⟩
f a (f (f c b) d)
≡⟨ cong (f a) (assoc c b d) ⟩
f a (f c (f b d))
≡⟨ sym (assoc a c (f b d)) ⟩
f (f a c) (f b d)
∎ where open ≡-Reasoning
module _ where
open import Data.Nat
-- Apologies for irregular name.
ℕ-mn·pq=mp·nq : ∀ {m n p q : ℕ} →
(m + n) + (p + q) ≡ (m + p) + (n + q)
ℕ-mn·pq=mp·nq {m} {n} {p} {q} =
4-way-shuffle {a = m} {n} {p} {q} {{comm-monoid-nat}}
open import Data.Integer
n+[m-n]=m : ∀ {n m} → n + (m - n) ≡ m
n+[m-n]=m {n} {m} =
begin
n + (m - n)
≡⟨ cong (λ hole → n + hole) (commutative-int m (- n)) ⟩
n + (- n + m)
≡⟨ sym (associative-int n (- n) m) ⟩
(n - n) + m
≡⟨ cong (λ hole → hole + m) (right-inv-int n) ⟩
(+ 0) + m
≡⟨ left-id-int m ⟩
m
∎ where open ≡-Reasoning
a++[b\\a]=b : ∀ {a b} → a ++ (b \\ a) ≡ b
a++[b\\a]=b {b} {d} = trans
(cong (λ hole → b ++ hole) (commutative-bag d (negateBag b))) (trans
(sym (associative-bag b (negateBag b) d)) (trans
(cong (λ hole → hole ++ d) (right-inv-bag b))
(left-id-bag d)))
ab·cd=ac·bd : ∀ {a b c d : Bag} →
(a ++ b) ++ (c ++ d) ≡ (a ++ c) ++ (b ++ d)
ab·cd=ac·bd {a} {b} {c} {d} =
4-way-shuffle {a = a} {b} {c} {d} {{comm-monoid-bag}}
mn·pq=mp·nq : ∀ {m n p q : ℤ} →
(m + n) + (p + q) ≡ (m + p) + (n + q)
mn·pq=mp·nq {m} {n} {p} {q} =
4-way-shuffle {a = m} {n} {p} {q} {{comm-monoid-int}}
inverse-unique : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f a b ≡ z → b ≡ neg a
inverse-unique {f = f} {neg} {z} {a} {b} {{abelian}} ab=z =
let
assoc = associative (IsAbelianGroup.isCommutativeMonoid abelian)
cmute = commutative (IsAbelianGroup.isCommutativeMonoid abelian)
in
begin
b
≡⟨ sym (left-identity abelian b) ⟩
f z b
≡⟨ cong (λ hole → f hole b) (sym (left-inverse abelian a)) ⟩
f (f (neg a) a) b
≡⟨ assoc (neg a) a b ⟩
f (neg a) (f a b)
≡⟨ cong (f (neg a)) ab=z ⟩
f (neg a) z
≡⟨ right-identity abelian (neg a) ⟩
neg a
∎ where
open ≡-Reasoning
eq1 : f (neg a) (f a b) ≡ f (neg a) z
eq1 rewrite ab=z = cong (f (neg a)) refl
distribute-neg : ∀ {A : Set} {f neg} {z a b : A}
{{abelian : IsAbelianGroup _≡_ f z neg}} →
f (neg a) (neg b) ≡ neg (f a b)
distribute-neg {f = f} {neg} {z} {a} {b} {{abelian}} = inverse-unique
{{abelian}}
(begin
f (f a b) (f (neg a) (neg b))
≡⟨ 4-way-shuffle {{IsAbelianGroup.isCommutativeMonoid abelian}} ⟩
f (f a (neg a)) (f b (neg b))
≡⟨ cong₂ f (inverse a) (inverse b) ⟩
f z z
≡⟨ left-identity abelian z ⟩
z
∎) where
open ≡-Reasoning
inverse = right-inverse abelian
-a·-b=-ab : ∀ {a b : Bag} →
negateBag a ++ negateBag b ≡ negateBag (a ++ b)
-a·-b=-ab {a} {b} = distribute-neg {a = a} {b} {{abelian-bag}}
-m·-n=-mn : ∀ {m n : ℤ} →
(- m) + (- n) ≡ - (m + n)
-m·-n=-mn {m} {n} = distribute-neg {a = m} {n} {{abelian-int}}
[a++b]\\a=b : ∀ {a b} → (a ++ b) \\ a ≡ b
[a++b]\\a=b {b} {d} =
begin
(b ++ d) \\ b
≡⟨ cong (λ hole → hole \\ b) (commutative-bag b d) ⟩
(d ++ b) \\ b
≡⟨ associative-bag d b (negateBag b) ⟩
d ++ (b \\ b)
≡⟨ cong (_++_ d) (right-inv-bag b) ⟩
d ++ emptyBag
≡⟨ right-id-bag d ⟩
d
∎ where open ≡-Reasoning
|
add variant of 4-way-shuffle for naturals
|
Groups-Nehemiah: add variant of 4-way-shuffle for naturals
|
Agda
|
mit
|
inc-lc/ilc-agda
|
f8bd15f415762af0fce2117302889b7e97a25936
|
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 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 :
DiffStructure →
ApplyStructure →
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply :
DiffStructure →
ApplyStructure →
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff 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 ι} = 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 {τ} {Γ})
|
Rename diff to diff-base.
|
Rename diff to diff-base.
I want to move code between Parametric.Syntax.Term and
Atlas.Syntax.Term, but I have to unify the naming conventions first.
Old-commit-hash: 73a829a379ce12d2c01dbaf184ae2d380ad9abb1
|
Agda
|
mit
|
inc-lc/ilc-agda
|
be6f1fa3a0adbb680ac349286c28d45c17722184
|
Prob.agda
|
Prob.agda
|
module Prob where
open import Data.Bool
open import Data.Nat
open import Data.Vec
open import Function
open import Relation.Binary.PropositionalEquality.NP
open import Relation.Nullary
record ]0,1]-ops (]0,1] : Set) : Set where
field
1E : ]0,1]
_+E_ : ]0,1] → ]0,1] → ]0,1]
_·E_ : ]0,1] → ]0,1] → ]0,1]
1/E_ : ]0,1] → ]0,1]
module [0,1] {]0,1]}(]0,1]R : ]0,1]-ops ]0,1]) where
open ]0,1]-ops ]0,1]R public
infixl 6 _+I_
infix 4 _≤I_
data [0,1] : Set where
0I : [0,1]
_I : ]0,1] → [0,1]
1I : [0,1]
1I = 1E I
Pos : [0,1] → Set
Pos x = ¬ (x ≡ 0I)
_<_> : (x : [0,1]) → Pos x → ]0,1]
0I < pos > with pos refl
... | ()
(x I) < pos > = x
_+I_ : [0,1] → [0,1] → [0,1]
0I +I x = x
x I +I 0I = x I
x I +I x₁ I = (x +E x₁) I
_·I_ : [0,1] → [0,1] → [0,1]
0I ·I y = 0I
(x I) ·I 0I = 0I
(x I) ·I (x₁ I) = (x ·E x₁) I
1/I_ : ]0,1] → [0,1]
1/I x = (1/E x) I
_/I_ : [0,1] → ]0,1] → [0,1]
x /I y = x ·I (1/I y)
+I-identity : (x : [0,1]) → x ≡ 0I +I x
+I-identity x = refl
postulate
+I-sym : (x y : [0,1]) → x +I y ≡ y +I x
+I-assoc : (x y z : [0,1]) → x +I y +I z ≡ x +I (y +I z)
_≤I_ : [0,1] → [0,1] → Set
≤I-refl : {x : [0,1]} → x ≤I x
≤I-trans : {x y z : [0,1]} → x ≤I y → y ≤I z → x ≤I z
≤I-mono : (x : [0,1]){y z : [0,1]} → y ≤I z → y ≤I z +I x
≤I-pres : (x : [0,1]){y z : [0,1]} → y ≤I z → x +I y ≤I x +I z
module Univ ( ]0,1] : Set)(]0,1]R : ]0,1]-ops ]0,1])
(U : Set)
(size-1 : ℕ)
(allU : Vec U (suc size-1))
(x∈allU : (x : U) → x ∈ allU) where
open [0,1] ]0,1]R
sumP : {n : ℕ} → (U → [0,1]) → Vec U n → [0,1]
sumP P [] = 0I
sumP P (x ∷ xs) = (P x) +I (sumP P xs)
module Prob (P : U → [0,1])
(sumP≡1 : sumP P allU ≡ 1I) where
Event : Set
Event = U → Bool
pr_∋_ : Event → U → [0,1]
pr A ∋ x = if A x then P x else 0I
_∪_ : Event → Event → Event
A₁ ∪ A₂ = λ x → A₁ x ∨ A₂ x
_∩_ : Event → Event → Event
A₁ ∩ A₂ = λ x → A₁ x ∧ A₂ x
ℂ[_] : Event → Event
ℂ[ A ] = not ∘ A
Pr[_] : Event → [0,1]
Pr[ A ] = sumP (pr_∋_ A) allU
Pr[_∣_]<_> : (A B : Event)(pf : Pos Pr[ B ]) → [0,1]
Pr[ A ∣ B ]< pr > = Pr[ A ∩ B ] /I Pr[ B ] < pr >
_ind_ : (A B : Event) → Set
A ind B = Pr[ A ] ·I Pr[ B ] ≡ Pr[ A ∩ B ]
union-bound : (A₁ A₂ : Event) → Pr[ (A₁ ∪ A₂) ] ≤I Pr[ A₁ ] +I Pr[ A₂ ]
union-bound A₁ A₂ = go allU where
sA₁ : {n : ℕ} → Vec U n → [0,1]
sA₁ = λ xs → sumP (pr_∋_ A₁) xs
sA₂ : {n : ℕ} → Vec U n → [0,1]
sA₂ = sumP (pr_∋_ A₂)
go : {n : ℕ}(xs : Vec U n) → sumP (pr_∋_ (A₁ ∪ A₂)) xs ≤I sumP (pr_∋_ A₁) xs +I sumP (pr_∋_ A₂) xs
go [] = ≤I-refl
go (x ∷ xs) with A₁ x | A₂ x
... | true | true rewrite +I-assoc (P x) (sA₁ xs) (P x +I sA₂ xs)
| +I-sym (P x) (sA₂ xs)
| sym (+I-assoc (sA₁ xs) (sA₂ xs) (P x))
= ≤I-pres (P x) (≤I-mono (P x) (go xs))
... | true | false rewrite +I-assoc (P x) (sA₁ xs) (sA₂ xs)
= ≤I-pres (P x) (go xs)
... | false | true rewrite sym (+I-assoc (sA₁ xs) (P x) (sA₂ xs))
| +I-sym (sA₁ xs) (P x)
| +I-assoc (P x) (sA₁ xs) (sA₂ xs)
= ≤I-pres (P x) (go xs)
... | false | false = go xs
module RandomVar (V : Set) (_==V_ : V → V → Bool) where
RV : Set
RV = U → V
_^-1_ : RV → V → Event
RV ^-1 v = λ x → RV x ==V v
_≡r_ : RV → V → Event
RV ≡r v = RV ^-1 v
-- sugar, socker et sucre
Pr[_≡_] : RV → V → [0,1]
Pr[ X ≡ v ] = Pr[ X ≡r v ]
-- ∀ (a b) . Pr[X = a and Y = b] = Pr[X = a] · Pr[Y = b]
_indRV_ : RV → RV → Set
X indRV Y = (a b : V) → (X ^-1 a) ind (Y ^-1 b)
|
module Prob where
open import Data.Bool
open import Data.Nat
open import Data.Vec
open import Data.Unit using (⊤)
open import Data.Empty using (⊥)
open import Function
open import Relation.Binary.PropositionalEquality.NP
open import Relation.Nullary
record ]0,1]-ops (]0,1] : Set) (_≤E_ : ]0,1] → ]0,1] → Set) : Set where
constructor mk
field
1E : ]0,1]
_+E_ : ]0,1] → ]0,1] → ]0,1]
_·E_ : ]0,1] → ]0,1] → ]0,1]
_/E_<_> : (x y : ]0,1]) → x ≤E y → ]0,1]
module [0,1] {]0,1] _≤E_} (]0,1]R : ]0,1]-ops ]0,1] _≤E_) where
open ]0,1]-ops ]0,1]R public
infixl 6 _+I_
infix 4 _≤I_
data [0,1] : Set where
0I : [0,1]
_I : ]0,1] → [0,1]
data _≤I_ : [0,1] → [0,1] → Set where
z≤n : ∀ {n} → 0I ≤I n
E≤E : ∀ {x y} → x ≤E y → (x I) ≤I (y I)
1I : [0,1]
1I = 1E I
Pos : [0,1] → Set
Pos 0I = ⊥
Pos (_ I) = ⊤
_<_> : (x : [0,1]) → Pos x → ]0,1]
0I < () >
(x I) < pos > = x
_+I_ : [0,1] → [0,1] → [0,1]
0I +I x = x
x I +I 0I = x I
x I +I x₁ I = (x +E x₁) I
_·I_ : [0,1] → [0,1] → [0,1]
0I ·I y = 0I
(x I) ·I 0I = 0I
(x I) ·I (x₁ I) = (x ·E x₁) I
-- 1/I1+_ : ℕ → [0,1]
_/I_<_,_> : (x y : [0,1]) → x ≤I y → Pos y → [0,1]
_ /I 0I < _ , () >
0I /I _ < _ , _ > = 0I
(x I) /I (y I) < E≤E pf , _ > = (x /E y < pf >)I
+I-identity : (x : [0,1]) → x ≡ 0I +I x
+I-identity x = refl
postulate
+I-sym : (x y : [0,1]) → x +I y ≡ y +I x
+I-assoc : (x y z : [0,1]) → x +I y +I z ≡ x +I (y +I z)
≤I-refl : {x : [0,1]} → x ≤I x
≤I-trans : {x y z : [0,1]} → x ≤I y → y ≤I z → x ≤I z
≤I-mono : (x : [0,1]){y z : [0,1]} → y ≤I z → y ≤I z +I x
≤I-pres : (x : [0,1]){y z : [0,1]} → y ≤I z → x +I y ≤I x +I z
module Univ {]0,1] _≤E_} (]0,1]R : ]0,1]-ops ]0,1] _≤E_)
(U : Set)
(size-1 : ℕ)
(allU : Vec U (suc size-1))
(x∈allU : (x : U) → x ∈ allU) where
open [0,1] ]0,1]R
sumP : {n : ℕ} → (U → [0,1]) → Vec U n → [0,1]
sumP P [] = 0I
sumP P (x ∷ xs) = (P x) +I (sumP P xs)
module Prob (P : U → [0,1])
(sumP≡1 : sumP P allU ≡ 1I) where
Event : Set
Event = U → Bool
pr_∋_ : Event → U → [0,1]
pr A ∋ x = if A x then P x else 0I
_∪_ : Event → Event → Event
A₁ ∪ A₂ = λ x → A₁ x ∨ A₂ x
_∩_ : Event → Event → Event
A₁ ∩ A₂ = λ x → A₁ x ∧ A₂ x
_⊆_ : Event → Event → Set
A ⊆ B = ∀ x → T(A x) → T(B x)
ℂ[_] : Event → Event
ℂ[ A ] = not ∘ A
Pr[_] : Event → [0,1]
Pr[ A ] = sumP (pr_∋_ A) allU
postulate
Pr-mono : ∀ {A B} → A ⊆ B → Pr[ A ] ≤I Pr[ B ]
∪-lem : ∀ {A} B → A ⊆ (A ∪ B)
∪-lem {A} _ x with A x
... | true = id
... | false = λ()
∩-lem : ∀ A {B} → (A ∩ B) ⊆ B
∩-lem A x with A x
... | true = id
... | false = λ()
Pr[_∣_]<_> : (A B : Event)(pf : Pos Pr[ B ]) → [0,1]
Pr[ A ∣ B ]< pr > = Pr[ A ∩ B ] /I Pr[ B ] < Pr-mono (∩-lem A) , pr >
_ind_ : (A B : Event) → Set
A ind B = Pr[ A ] ·I Pr[ B ] ≡ Pr[ A ∩ B ]
union-bound : (A₁ A₂ : Event) → Pr[ (A₁ ∪ A₂) ] ≤I Pr[ A₁ ] +I Pr[ A₂ ]
union-bound A₁ A₂ = go allU where
sA₁ : {n : ℕ} → Vec U n → [0,1]
sA₁ = λ xs → sumP (pr_∋_ A₁) xs
sA₂ : {n : ℕ} → Vec U n → [0,1]
sA₂ = sumP (pr_∋_ A₂)
go : {n : ℕ}(xs : Vec U n) → sumP (pr_∋_ (A₁ ∪ A₂)) xs ≤I sumP (pr_∋_ A₁) xs +I sumP (pr_∋_ A₂) xs
go [] = ≤I-refl
go (x ∷ xs) with A₁ x | A₂ x
... | true | true rewrite +I-assoc (P x) (sA₁ xs) (P x +I sA₂ xs)
| +I-sym (P x) (sA₂ xs)
| sym (+I-assoc (sA₁ xs) (sA₂ xs) (P x))
= ≤I-pres (P x) (≤I-mono (P x) (go xs))
... | true | false rewrite +I-assoc (P x) (sA₁ xs) (sA₂ xs)
= ≤I-pres (P x) (go xs)
... | false | true rewrite sym (+I-assoc (sA₁ xs) (P x) (sA₂ xs))
| +I-sym (sA₁ xs) (P x)
| +I-assoc (P x) (sA₁ xs) (sA₂ xs)
= ≤I-pres (P x) (go xs)
... | false | false = go xs
module RandomVar (V : Set) (_==V_ : V → V → Bool) where
RV : Set
RV = U → V
_^-1_ : RV → V → Event
RV ^-1 v = λ x → RV x ==V v
_≡r_ : RV → V → Event
RV ≡r v = RV ^-1 v
-- sugar, socker et sucre
Pr[_≡_] : RV → V → [0,1]
Pr[ X ≡ v ] = Pr[ X ≡r v ]
-- ∀ (a b) . Pr[X = a and Y = b] = Pr[X = a] · Pr[Y = b]
_indRV_ : RV → RV → Set
X indRV Y = (a b : V) → (X ^-1 a) ind (Y ^-1 b)
|
Fix some concrete non-sense about the division
|
Fix some concrete non-sense about the division
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
a0c26d1d2731a3ccca7b00c77068c7bb3491ecab
|
FiniteField/JS.agda
|
FiniteField/JS.agda
|
{-# OPTIONS --without-K #-}
open import Function.NP
open import FFI.JS using (Number; Bool; true; false; String; warn-check; check; trace-call; _++_)
open import FFI.JS.BigI
open import Data.List.Base hiding (sum; _++_)
{-
open import Algebra.Raw
open import Algebra.Field
-}
module FiniteField.JS (q : BigI) where
abstract
ℤq : Set
ℤq = BigI
private
mod-q : BigI → ℤq
mod-q x = mod x q
-- There is two ways to go from BigI to ℤq: check and mod-q
-- Use check for untrusted input data and mod-q for internal
-- computation.
BigI▹ℤq : BigI → ℤq
BigI▹ℤq = -- trace-call "BigI▹ℤq "
λ x →
mod-q
(warn-check (x <I q)
(λ _ → "Not below the modulus: " ++ toString q ++ " < " ++ toString x)
(warn-check (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") x))
check-non-zero : ℤq → BigI
check-non-zero = -- trace-call "check-non-zero "
λ x → check (x >I 0I) (λ _ → "Should be non zero") x
repr : ℤq → BigI
repr x = x
0# 1# : ℤq
0# = 0I
1# = 1I
1/_ : Op₁ ℤq
1/ x = modInv (check-non-zero x) q
_^_ : Op₂ ℤq
x ^ y = modPow (repr x) (repr y) q
_+_ _−_ _*_ _/_ : Op₂ ℤq
x + y = mod-q (add (repr x) (repr y))
x − y = mod-q (subtract (repr x) (repr y))
x * y = mod-q (multiply (repr x) (repr y))
x / y = x * 1/ y
0−_ : Op₁ ℤq
0− x = mod-q (negate (repr x))
_==_ : (x y : ℤq) → Bool
x == y = equals (repr x) (repr y)
sum prod : List ℤq → ℤq
sum = foldr _+_ 0#
prod = foldr _*_ 1#
{-
+-mon-ops : Monoid-Ops ℤq
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops ℤq
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops ℤq
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops ℤq
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops ℤq
fld-ops = +-grp-ops , *-grp-ops
postulate
fld-struct : Field-Struct fld-ops
fld : Field ℤq
fld = fld-ops , fld-struct
-- -}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Function.NP
open import FFI.JS using (𝟙; Number; Bool; true; false; String; warn-check; check; trace-call; _++_)
open import FFI.JS.BigI
open import Data.List.Base hiding (sum; _++_)
{-
open import Algebra.Raw
open import Algebra.Field
-}
module FiniteField.JS (q : BigI) where
abstract
𝔽 : Set
𝔽 = BigI
private
mod-q : BigI → 𝔽
mod-q x = mod x q
check' : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
-- check' = warn-check
check' = check
-- There is two ways to go from BigI to ℤq: check and mod-q
-- Use check for untrusted input data and mod-q for internal
-- computation.
fromBigI : BigI → 𝔽
fromBigI = -- trace-call "BigI▹ℤq "
λ x →
(check' (x <I q)
(λ _ → "Not below the modulus: q:" ++ toString q ++ " is less than x:" ++ toString x)
(check' (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") x))
check-non-zero : 𝔽 → BigI
check-non-zero = -- trace-call "check-non-zero "
λ x → check (x >I 0I) (λ _ → "Should be non zero") x
repr : 𝔽 → BigI
repr x = x
0# 1# : 𝔽
0# = 0I
1# = 1I
1/_ : Op₁ 𝔽
1/ x = modInv (check-non-zero x) q
_^_ : Op₂ 𝔽
x ^ y = modPow (repr x) (repr y) q
_+_ _−_ _*_ _/_ : Op₂ 𝔽
x + y = mod-q (add (repr x) (repr y))
x − y = mod-q (subtract (repr x) (repr y))
x * y = mod-q (multiply (repr x) (repr y))
x / y = x * 1/ y
0−_ : Op₁ 𝔽
0− x = mod-q (negate (repr x))
_==_ : (x y : 𝔽) → Bool
x == y = equals (repr x) (repr y)
sum prod : List 𝔽 → 𝔽
sum = foldr _+_ 0#
prod = foldr _*_ 1#
{-
+-mon-ops : Monoid-Ops 𝔽
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops 𝔽
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops 𝔽
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops 𝔽
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops 𝔽
fld-ops = +-grp-ops , *-grp-ops
postulate
fld-struct : Field-Struct fld-ops
fld : Field 𝔽
fld = fld-ops , fld-struct
-- -}
-- -}
-- -}
-- -}
-- -}
|
rename ℤq to 𝔽
|
FiniteField.JS: rename ℤq to 𝔽
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
0f09d9f4ac44a07ba808622a5206a04b696a6f76
|
derivation.agda
|
derivation.agda
|
module derivation where
open import lambda
-- 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))
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)
Δ-term {Γ₁} {Γ₂} {τ} (app t₁ t₂) = app (app (compose {τ}) (app (Δ-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
-- 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₃) = {!!}
|
correct Δ-term, more comments
|
agda/derivation.agda: correct Δ-term, more comments
In the previous commit I forgot some comments and I forgot applying some
changes, something the typechecker unfortunately would not detect. Fix that.
Old-commit-hash: 9a72bde5058103929f5d3c0eda97b9850a5d7c9c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
8f895d5906dc2d743ba0dc39c4f907e17931b4f9
|
Denotational/ValidChanges.agda
|
Denotational/ValidChanges.agda
|
module Denotational.ValidChanges where
open import Data.Product
open import Data.Unit
open import Relation.Nullary
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)
invalid-changes-exist : ¬ (∀ {τ} v dv → Valid-Δ {τ} v dv)
invalid-changes-exist k with k (λ x → x) (λ x dx → false) false true
... | _ , ()
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.Nullary
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)
invalid-changes-exist : ¬ (∀ {τ} v dv → Valid-Δ {τ} v dv)
invalid-changes-exist k with k (λ x → x) (λ x dx → false) false true
... | _ , ()
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
open import Syntactic.Contexts Type
open import Denotational.Environments Type ⟦_⟧Type
open import Changes
open import ChangeContexts
ProofVal : Type → Set
ProofVal τ = Σ[ v ∈ ⟦ τ ⟧ ] (Σ[ dv ∈ ⟦ Δ-Type τ ⟧ ] Valid-Δ v dv)
import Denotational.Environments Type ProofVal as ProofEnv
eraseVal : ∀ {τ} → ProofVal τ → ⟦ τ ⟧
eraseVal (v , dv , dv-valid) = v
-- Specification: eraseEnv = map eraseVal
eraseEnv : ∀ {Γ} → ProofEnv.⟦ Γ ⟧Context → ⟦ Γ ⟧
eraseEnv {∅} ∅ = ∅
eraseEnv {τ • Γ} (v • ρ) = eraseVal v • eraseEnv ρ
eraseProofs : ∀ {Γ} → ProofEnv.⟦ Γ ⟧Context → ⟦ Δ-Context Γ ⟧
eraseProofs {∅} ∅ = ∅
eraseProofs {τ • Γ} ((v , dv , dv-valid) • ρ) = dv • v • eraseProofs ρ
|
implement proof-decorated environments (#33)
|
agda: implement proof-decorated environments (#33)
Old-commit-hash: eb0097a135a39c572cfb62c967a388632b42f839
|
Agda
|
mit
|
inc-lc/ilc-agda
|
7c497fbc194526d5d3f1a4335a6f74574050c633
|
TotalNaturalSemantics.agda
|
TotalNaturalSemantics.agda
|
module TotalNaturalSemantics where
open import Relation.Binary.PropositionalEquality
open import meaning
open import Model
--open import Changes
--open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
-- NATURAL SEMANTICS
-- (without support for Δ for now)
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv = meaning ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal = meaning ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = ≡-refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = ≡-refl
↓-sound (app ↓₁ ↓₂ ↓′) =
≡-trans
(≡-cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂))
(↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
|
module TotalNaturalSemantics where
open import Relation.Binary.PropositionalEquality
open import meaning
open import Model
--open import Changes
--open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
open import Relation.Binary.PropositionalEquality
-- NATURAL SEMANTICS
-- (without support for Δ for now)
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv = meaning ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal = meaning ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = ≡-refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = ≡-refl
↓-sound (app ↓₁ ↓₂ ↓′) =
≡-trans
(≡-cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂))
(↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
|
Fix TotalNaturalSemantics.agda (fix #32).
|
Fix TotalNaturalSemantics.agda (fix #32).
Old-commit-hash: 3665667d355c2d049d92115cd27f98cc9dc4f3a2
|
Agda
|
mit
|
inc-lc/ilc-agda
|
3ecae75aaafb233597792507020b6d74bee0ecfe
|
Data/NatBag.agda
|
Data/NatBag.agda
|
-- An agda type isomorphic to bags of natural numbers
-- How to import this file from anywhere in the world
--
-- 1. M-x customize-group RET agda2 RET
-- 2. Look for the option `Agda2 Include Dirs`
-- 3. Insert "$ILC/agda", where $ILC is path to ILC repo on your computer
-- 4. "Save for future sessions"
-- 5. open import Data.NatBag
module Data.NatBag where
open import Data.Nat hiding (zero) renaming (_+_ to plus)
open import Data.Integer renaming (suc to +1 ; pred to -1)
open import Data.Maybe.Core
open import Data.Sum
-----------
-- Types --
-----------
data Nonzero : ℤ → Set where
negative : (n : ℕ) → Nonzero -[1+ n ]
positive : (n : ℕ) → Nonzero (+ (suc n))
data EmptyBag : Set where
∅ : EmptyBag
data NonemptyBag : Set where
singleton : (i : ℤ) → (i≠0 : Nonzero i) → NonemptyBag
_∷_ : (i : ℤ) → (bag : NonemptyBag) → NonemptyBag
infixr 5 _∷_
Bag : Set
Bag = EmptyBag ⊎ NonemptyBag
-------------------
-- Bag interface --
-------------------
empty : Bag
insert : ℕ → Bag → Bag
delete : ℕ → Bag → Bag
lookup : ℕ → Bag → ℤ
update : ℕ → ℤ → Bag → Bag
updateWith : ℕ → (ℤ → ℤ) → Bag → Bag
map₁ : (ℕ → ℕ) → Bag → Bag -- map on bags, mapKeys on maps
map₂ : (ℤ → ℤ) → Bag → Bag -- mapValues on maps
{- TODO: Wait for map.
zipWith : (ℤ → ℤ → ℤ) → Bag → Bag → Bag
_++_ : Bag → Bag → Bag
infixr 5 _++_ -- right-associativity follows Haskell, for efficiency
_\\_ : Bag → Bag → Bag
infixl 9 _\\_ -- fixity follows Haskell's Data.Map.(\\)
-}
--------------------
-- Implementation --
--------------------
insert n bag = updateWith n +1 bag
delete n bag = updateWith n -1 bag
update n i bag = updateWith n (λ _ → i) bag
zero : ℤ
zero = + 0
-- It is very easy to compute a proof that an integer is
-- nonzero whenever such a thing exists.
nonzero? : (i : ℤ) → Maybe (Nonzero i)
nonzero? (+ 0) = nothing
nonzero? (+ (suc n)) = just (positive n)
nonzero? -[1+ n ] = just (negative n)
empty = inj₁ ∅
makeSingleton : ℕ → (i : ℤ) → Nonzero i → NonemptyBag
makeSingleton 0 i i≠0 = singleton i i≠0
makeSingleton (suc n) i i≠0 = (+ 0) ∷ makeSingleton n i i≠0
updateWith n f (inj₁ ∅) with nonzero? (f zero)
... | nothing = empty
... | just i≠0 = inj₂ (makeSingleton n (f zero) i≠0)
updateWith 0 f (inj₂ (singleton i i≠0)) with nonzero? (f i)
... | nothing = empty
... | just j≠0 = inj₂ (singleton (f i) j≠0)
updateWith (suc n) f (inj₂ (singleton i i≠0)) with nonzero? (f zero)
... | nothing = inj₂ (singleton i i≠0)
... | just j≠0 = inj₂ (i ∷ makeSingleton n (f zero) j≠0)
updateWith 0 f (inj₂ (i ∷ bag)) = inj₂ (f i ∷ bag)
updateWith (suc n) f (inj₂ (i ∷ y))
with updateWith n f (inj₂ y) | nonzero? i
... | inj₁ ∅ | nothing = empty
... | inj₁ ∅ | just i≠0 = inj₂ (singleton i i≠0)
... | inj₂ bag | _ = inj₂ (i ∷ bag)
lookupNonempty : ℕ → NonemptyBag → ℤ
lookupNonempty 0 (singleton i i≠0) = i
lookupNonempty (suc n) (singleton i i≠0) = zero
lookupNonempty 0 (i ∷ bag) = i
lookupNonempty (suc n) (i ∷ bag) = lookupNonempty n bag
lookup n (inj₁ ∅) = zero
lookup n (inj₂ bag) = lookupNonempty n bag
-- It is possible to get empty bags by mapping over a nonempty bag:
-- map₁ (λ _ → 3) { 1 ⇒ 5 , 2 ⇒ -5 }
mapKeysFrom : ℕ → (ℕ → ℕ) → NonemptyBag → Bag
mapKeysFrom n f (singleton i i≠0) = inj₂ (makeSingleton (f n) i i≠0)
mapKeysFrom n f (i ∷ bag) with nonzero? i
... | nothing = mapKeysFrom (suc n) f bag
... | just i≠0 = updateWith (f n) (λ j → j + i) (mapKeysFrom (suc n) f bag)
map₁ f (inj₁ ∅) = empty
map₁ f (inj₂ bag) = mapKeysFrom 0 f bag
map₂ f (inj₁ ∅) = empty
map₂ f (inj₂ (singleton i i≠0)) = {!!}
map₂ f (inj₂ (i ∷ bag₀)) with map₂ f (inj₂ bag₀) | nonzero? i | nonzero? (f i)
-- If an element has multiplicity 0, it should not be mapped over.
-- Conceptually, we are mapping over the values of this infinite
-- map not by f, but by {case 0 => 0}.orElse(f).
... | inj₁ ∅ | nothing | _ = empty
... | inj₂ bag | nothing | _ = inj₂ (zero ∷ bag)
... | inj₁ ∅ | just i≠0 | nothing = empty
... | inj₁ ∅ | just i≠0 | just j≠0 = inj₂ (singleton (f i) j≠0)
... | inj₂ bag | just i≠0 | _ = inj₂ (f i ∷ bag)
{- TODO: Redefine in terms of zipWith, which will be defined
in terms of map.
-- The union of nonempty bags (with possibly negative
-- multiplicities) can be empty.
unionNonempty : NonemptyBag → NonemptyBag → Bag
unionNonempty (singleton i i≠0) (singleton j j≠0) with nonzero? (i + j)
... | nothing = empty
... | just k≠0 = inj₂ (singleton (i + j) k≠0)
unionNonempty (singleton i i≠0) (j ∷ b₂) = inj₂ (i + j ∷ b₂)
inj₁ ∅ ++ b₂ = b₂
b₁ ++ inj₁ ∅ = b₁
(inj₂ b₁) ++ (inj₂ b₂) = (unionNonempty b₁ b₂)
-}
|
-- An agda type isomorphic to bags of natural numbers
-- How to import this file from anywhere in the world
--
-- 1. M-x customize-group RET agda2 RET
-- 2. Look for the option `Agda2 Include Dirs`
-- 3. Insert "$ILC/agda", where $ILC is path to ILC repo on your computer
-- 4. "Save for future sessions"
-- 5. open import Data.NatBag
module Data.NatBag where
open import Data.Nat hiding (zero) renaming (_+_ to plus)
open import Data.Integer renaming (suc to +1 ; pred to -1)
open import Data.Maybe.Core
open import Data.Sum hiding (map)
-----------
-- Types --
-----------
data Nonzero : ℤ → Set where
negative : (n : ℕ) → Nonzero -[1+ n ]
positive : (n : ℕ) → Nonzero (+ (suc n))
data EmptyBag : Set where
∅ : EmptyBag
data NonemptyBag : Set where
singleton : (i : ℤ) → (i≠0 : Nonzero i) → NonemptyBag
_∷_ : (i : ℤ) → (bag : NonemptyBag) → NonemptyBag
infixr 5 _∷_
Bag : Set
Bag = EmptyBag ⊎ NonemptyBag
-------------------
-- Bag interface --
-------------------
empty : Bag
insert : ℕ → Bag → Bag
delete : ℕ → Bag → Bag
lookup : ℕ → Bag → ℤ
update : ℕ → ℤ → Bag → Bag
updateWith : ℕ → (ℤ → ℤ) → Bag → Bag
map : (ℕ → ℕ) → Bag → Bag -- map on bags, mapKeys on maps
map₂ : (ℤ → ℤ) → Bag → Bag -- mapValues on maps
zipWith : (ℤ → ℤ → ℤ) → Bag → Bag → Bag
_++_ : Bag → Bag → Bag
infixr 5 _++_ -- right-associativity follows Haskell, for efficiency
_\\_ : Bag → Bag → Bag
infixl 9 _\\_ -- fixity follows Haskell's Data.Map.(\\)
--------------------
-- Implementation --
--------------------
insert n bag = updateWith n +1 bag
delete n bag = updateWith n -1 bag
update n i bag = updateWith n (λ _ → i) bag
_++_ = zipWith _+_
_\\_ = zipWith _-_
zero : ℤ
zero = + 0
-- It is very easy to compute a proof that an integer is
-- nonzero whenever such a thing exists.
nonzero? : (i : ℤ) → Maybe (Nonzero i)
nonzero? (+ 0) = nothing
nonzero? (+ (suc n)) = just (positive n)
nonzero? -[1+ n ] = just (negative n)
empty = inj₁ ∅
makeSingleton : ℕ → (i : ℤ) → Nonzero i → NonemptyBag
makeSingleton 0 i i≠0 = singleton i i≠0
makeSingleton (suc n) i i≠0 = (+ 0) ∷ makeSingleton n i i≠0
updateWith n f (inj₁ ∅) with nonzero? (f zero)
... | nothing = empty
... | just i≠0 = inj₂ (makeSingleton n (f zero) i≠0)
updateWith 0 f (inj₂ (singleton i i≠0)) with nonzero? (f i)
... | nothing = empty
... | just j≠0 = inj₂ (singleton (f i) j≠0)
updateWith (suc n) f (inj₂ (singleton i i≠0)) with nonzero? (f zero)
... | nothing = inj₂ (singleton i i≠0)
... | just j≠0 = inj₂ (i ∷ makeSingleton n (f zero) j≠0)
updateWith 0 f (inj₂ (i ∷ bag)) = inj₂ (f i ∷ bag)
updateWith (suc n) f (inj₂ (i ∷ y))
with updateWith n f (inj₂ y) | nonzero? i
... | inj₁ ∅ | nothing = empty
... | inj₁ ∅ | just i≠0 = inj₂ (singleton i i≠0)
... | inj₂ bag | _ = inj₂ (i ∷ bag)
lookupNonempty : ℕ → NonemptyBag → ℤ
lookupNonempty 0 (singleton i i≠0) = i
lookupNonempty (suc n) (singleton i i≠0) = zero
lookupNonempty 0 (i ∷ bag) = i
lookupNonempty (suc n) (i ∷ bag) = lookupNonempty n bag
lookup n (inj₁ ∅) = zero
lookup n (inj₂ bag) = lookupNonempty n bag
-- It is possible to get empty bags by mapping over a nonempty bag:
-- map (λ _ → 3) { 1 ⇒ 5 , 2 ⇒ -5 }
mapKeysFrom : ℕ → (ℕ → ℕ) → NonemptyBag → Bag
mapKeysFrom n f (singleton i i≠0) = inj₂ (makeSingleton (f n) i i≠0)
mapKeysFrom n f (i ∷ bag) with nonzero? i
... | nothing = mapKeysFrom (suc n) f bag
... | just i≠0 = updateWith (f n) (λ j → j + i) (mapKeysFrom (suc n) f bag)
map f (inj₁ ∅) = empty
map f (inj₂ bag) = mapKeysFrom 0 f bag
mapNonempty₂ : (ℤ → ℤ) → NonemptyBag → Bag
mapNonempty₂ f (singleton i i≠0) with nonzero? (f i)
... | nothing = empty
... | just j≠0 = inj₂ (singleton (f i) j≠0)
mapNonempty₂ f (i ∷ bag₀)
with mapNonempty₂ f bag₀ | nonzero? (f i)
... | inj₁ ∅ | nothing = empty
... | inj₁ ∅ | just j≠0 = inj₂ (singleton (f i) j≠0)
... | inj₂ bag | _ = inj₂ (f i ∷ bag)
map₂ f (inj₁ ∅) = empty
map₂ f (inj₂ b) = mapNonempty₂ f b
zipLeft : (ℤ → ℤ → ℤ) → NonemptyBag → Bag
zipLeft f b₁ = mapNonempty₂ (λ x → f x zero) b₁
zipRight : (ℤ → ℤ → ℤ) → NonemptyBag → Bag
zipRight f b₂ = mapNonempty₂ (λ y → f zero y) b₂
zipNonempty : (ℤ → ℤ → ℤ) → NonemptyBag → NonemptyBag → Bag
zipNonempty f (singleton i i≠0) (singleton j j≠0) with nonzero? (f i j)
... | nothing = empty
... | just k≠0 = inj₂ (singleton (f i j) k≠0)
zipNonempty f (singleton i i≠0) (j ∷ b₂)
with zipRight f b₂ | nonzero? (f i j)
... | inj₁ ∅ | nothing = empty
... | inj₁ ∅ | just k≠0 = inj₂ (singleton (f i j) k≠0)
... | inj₂ bag | _ = inj₂ (f i j ∷ bag)
zipNonempty f (i ∷ b₁) (singleton j j≠0)
with zipLeft f b₁ | nonzero? (f i j)
... | inj₁ ∅ | nothing = empty
... | inj₁ ∅ | just k≠0 = inj₂ (singleton (f i j) k≠0)
... | inj₂ bag | _ = inj₂ (f i j ∷ bag)
zipNonempty f (i ∷ b₁) (j ∷ b₂)
with zipNonempty f b₁ b₂ | nonzero? (f i j)
... | inj₁ ∅ | nothing = empty
... | inj₁ ∅ | just k≠0 = inj₂ (singleton (f i j) k≠0)
... | inj₂ bag | _ = inj₂ (f i j ∷ bag)
zipWith f (inj₁ ∅) (inj₁ ∅) = empty
zipWith f (inj₁ ∅) (inj₂ b₂) = zipRight f b₂
zipWith f (inj₂ b₁) (inj₁ ∅) = zipLeft f b₁
zipWith f (inj₂ b₁) (inj₂ b₂) = zipNonempty f b₁ b₂
|
Implement map, union, difference on bags of natural numbers (#51)
|
Implement map, union, difference on bags of natural numbers (#51)
TODO before closing the issue:
- Introduce bags and prove the stupid derivative of map correct.
Old-commit-hash: 76c8ff03739579a6839431ba637557395ccbf8b2
|
Agda
|
mit
|
inc-lc/ilc-agda
|
f44ff9c8dc76e1365c69137588490cc7705c8477
|
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 : 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
|
rename datatype valid-Δ |-> Valid-Δ
|
agda: rename datatype valid-Δ |-> Valid-Δ
Old-commit-hash: 37b8db1e2601a886b5bcd31b170ad1560352b8a7
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2c0998148ca4d0f531c98144173c0d27ec548b8a
|
ZK/Schnorr.agda
|
ZK/Schnorr.agda
|
module ZK.Schnorr where
open import Type
open import Data.Two
open import Relation.Binary.PropositionalEquality.NP
import ZK.Sigma-Protocol
module Shnorr-protocol
(G ℤq : ★)
(g : G)
(_^_ : G → ℤq → G)
(_·_ : G → G → G)
(_/_ : G → G → G)
(_+_ : ℤq → ℤq → ℤq)
(_*_ : ℤq → ℤq → ℤq)
(_==_ : (x y : G) → 𝟚)
where
Commitment = G
Challenge = ℤq
Response = ℤq
open ZK.Sigma-Protocol Commitment Challenge Response
module _ (x w : ℤq) where
prover-commitment : Commitment
prover-commitment = g ^ w
prover-response : Challenge → Response
prover-response c = w + (x * c)
prover : Prover
prover = prover-commitment , prover-response
module _ (y : G) where
verifier : Verifier
verifier (mk A c s) = (g ^ s) == (A · (y ^ c))
-- This 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 it they need to be picked at random.
module _ (c : Challenge) (s : Response) where
-- Compute A, such that the verifier accepts!
private
A = (g ^ s) / (y ^ c)
simulate-commitment : Commitment
simulate-commitment = A
simulate : Transcript
simulate = mk simulate-commitment c s
module Correct-simulation
(✓-== : ∀ {x y} → x ≡ y → ✓ (x == y))
(/-· : ∀ {P Q} → P ≡ (P / Q) · Q)
where
correct-simulation : ✓(verifier simulate)
correct-simulation = ✓-== /-·
module Correctness-proof
(✓-== : ∀ {x y} → x ≡ y → ✓ (x == y))
(^-+ : ∀ {b x y} → b ^(x + y) ≡ (b ^ x) · (b ^ y))
(^-* : ∀ {b x y} → b ^(x * y) ≡ (b ^ x) ^ y)
(x r : ℤq) where
open ≡-Reasoning
gʳ = g ^ r
correctness : Correctness (prover x r) (verifier (g ^ x))
correctness c = ✓-== (g ^(r + (x * c))
≡⟨ ^-+ ⟩
gʳ · (g ^(x * c))
≡⟨ ap (λ z → gʳ · z) ^-* ⟩
gʳ · ((g ^ x) ^ c)
∎)
-- -}
-- -}
-- -}
-- -}
|
open import Type using (Type)
open import Data.Bool.Minimal using (Bool) renaming (T to ✓)
open import Relation.Binary.PropositionalEquality.NP using (_≡_; idp; ap; ap₂; !_; module ≡-Reasoning)
open import ZK.Types using (Cyclic-group; module Cyclic-group
; Cyclic-group-properties; module Cyclic-group-properties)
import ZK.SigmaProtocol
module ZK.Schnorr
{G ℤq : Type}
(cg : Cyclic-group G ℤq)
where
open Cyclic-group cg
Commitment = G
Challenge = ℤq
Response = ℤq
open ZK.SigmaProtocol Commitment Challenge Response
module _ (x a : ℤq) where
prover-commitment : Commitment
prover-commitment = g ^ a
prover-response : Challenge → Response
prover-response c = a + (x * c)
prover : Prover
prover = prover-commitment , prover-response
module _ (y : G) where
verifier : Verifier
verifier (mk A c s) = (g ^ s) == (A · (y ^ c))
-- This 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 it they need to be picked at random.
simulator : Simulator
simulator c s = A
where
-- Compute A, such that the verifier accepts!
A = (g ^ s) / (y ^ c)
witness-extractor : Transcript² verifier → ℤq
witness-extractor t² = x
module Witness-extractor where
open Transcript² t²
fd = get-f₀ - get-f₁
cd = get-c₀ - get-c₁
x = fd * modinv cd
extractor : ∀ a → Extractor verifier
extractor a t² = prover (witness-extractor t²) a
module Proofs (cg-props : Cyclic-group-properties cg) where
open Cyclic-group-properties cg-props
correct : ∀ x a → Correct (prover x a) (let y = g ^ x in verifier y)
correct x a c
= ✓-== (g ^(a + (x * c))
≡⟨ ^-+ ⟩
gʷ · (g ^(x * c))
≡⟨ ap (λ z → gʷ · z) ^-* ⟩
gʷ · (y ^ c)
∎)
where
open ≡-Reasoning
gʷ = g ^ a
y = g ^ x
module _ (y : G) where
shvzk : Special-Honest-Verifier-Zero-Knowledge (verifier y)
shvzk = record { simulator = simulator y
; correct-simulator = λ _ _ → ✓-== /-· }
module _ (x : ℤq) (t² : Transcript² (verifier (g ^ x))) where
private
y = g ^ x
x' = witness-extractor y t²
open Transcript² t² renaming (get-A to A; get-c₀ to c₀; get-c₁ to c₁
;get-f₀ to f₀; get-f₁ to f₁)
open Witness-extractor y t² hiding (x)
open ≡-Reasoning
g^xcd≡g^fd = g ^(x * cd)
≡⟨ ^-* ⟩
y ^ (c₀ - c₁)
≡⟨ ^-- ⟩
(y ^ c₀) / (y ^ c₁)
≡⟨ ! cancels-/ ⟩
(A · (y ^ c₀)) / (A · (y ^ c₁))
≡⟨ ap₂ _/_ (! ==-✓ verify₀) (! ==-✓ verify₁) ⟩
(g ^ f₀) / (g ^ f₁)
≡⟨ ! ^-- ⟩
g ^ fd
∎
-- The extracted x is correct
x≡x' : x ≡ x'
x≡x' = left-*-to-right-/ (^-inj g^xcd≡g^fd)
extractor-exact : ∀ a → EqProver (extractor y a t²) (prover x a)
extractor-exact a = idp , (λ c → ap (λ z → _+_ a (_*_ z c)) (! x≡x'))
special-soundness : Special-Soundness (verifier y)
special-soundness = record { extractor = extractor y a
; extractor-correct = extractor-correct }
-- -}
-- -}
-- -}
-- -}
|
Update and extend ZK/Schnorr
|
Update and extend ZK/Schnorr
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
70def950c8925dc369db199a79e13cb59ff30970
|
adder.agda
|
adder.agda
|
{-# OPTIONS --without-K #-}
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 Function.NP hiding (id)
open import FunUniverse.Core hiding (_,_)
open import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (toℕ to Fin▹ℕ)
module adder where
module FunAdder
{t}
{T : ★_ t}
{funU : FunUniverse T}
(funOps : FunOps funU)
where
open FunUniverse funU
open FunOps funOps renaming (_∘_ to _`∘_)
--iter : ∀ {n A B S} → (S `× A `→ S `× B) → S `× `Vec A n `→ `Vec B n
iter : ∀ {n A B S} → (S `× A `→ S `× B) → S `× `Vec A n `→ S `× `Vec B n
iter {zero} F = second <[]>
iter {suc n} F = second uncons
⁏ assoc-first F ⁏ around (iter F)
⁏ second <∷>
msb-adder : ∀ {n} → `Bits n `× `Bits n `→ `Bits n
msb-adder = <tt⁏ <0b> , zip > ⁏ iter full-adder ⁏ snd
-- TODO reverses all over the places... switch to lsb first?
-- lsb
adder : ∀ {n} → `Bits n `× `Bits n `→ `Bits n
adder = < reverse × reverse > ⁏ msb-adder ⁏ reverse
open import Data.Digit
ℕ▹`Bits : ∀ ℓ → ℕ → `𝟙 `→ `Bits ℓ
ℕ▹`Bits ℓ n₀ = constBits (V.reverse (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₂
{-
open import IO
import IO.Primitive
-}
open import Data.One
open import Data.Two
open import Data.Product
--open import Coinduction
open import FunUniverse.Agda
--open import Data.Nat.Show
open FunAdder agdaFunOps
open FunOps agdaFunOps
import FunUniverse.Cost as Cost
module TimeCost = FunOps Cost.timeOps
{-
putBit : 𝟚 → IO 𝟙
putBit 1₂ = putStr "1"
putBit 0₂ = putStr "0"
putBits : ∀ {n} → Vec 𝟚 n → IO 𝟙
putBits [] = return _
putBits (x ∷ bs) = ♯ putBit x >> ♯ putBits bs
-}
arg1 = ℕ▹`Bits 8 0x0b _
arg2 = ℕ▹`Bits 8 0x1f _
result = adder (arg1 , arg2)
adder-cost : ℕ → ℕ
adder-cost n = FunAdder.adder Cost.timeOps {n}
{-
mainIO : IO 𝟙
mainIO = ♯ putBits arg1 >>
♯ (♯ putStr " + " >>
♯ (♯ putBits arg2 >>
♯ (♯ putStr " = " >>
♯ (♯ putBits result >>
♯ (♯ putStr " cost:" >>
♯ putStr (show (adder-cost 8)))))))
main : IO.Primitive.IO 𝟙
main = IO.run mainIO
-}
|
{-# OPTIONS --without-K #-}
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 Function.NP hiding (id)
open import FunUniverse.Core hiding (_,_)
open import Data.Fin using (Fin; zero; suc; #_; inject+; raise) renaming (toℕ to Fin▹ℕ)
module adder where
module FunAdder
{t}
{T : ★_ t}
{funU : FunUniverse T}
(funOps : FunOps funU)
where
open FunUniverse funU
open FunOps funOps renaming (_∘_ to _`∘_)
--iter : ∀ {n A B S} → (S `× A `→ S `× B) → S `× `Vec A n `→ `Vec B n
msb-adder : ∀ {n} → `Bits n `× `Bits n `→ `Bits n
msb-adder = <tt⁏ <0₂> , zip > ⁏ mapAccum full-adder ⁏ snd
-- TODO reverses all over the places... switch to lsb first?
-- lsb
adder : ∀ {n} → `Bits n `× `Bits n `→ `Bits n
adder = < reverse × reverse > ⁏ msb-adder ⁏ reverse
open import Data.Digit
ℕ▹`Bits : ∀ ℓ → ℕ → `𝟙 `→ `Bits ℓ
ℕ▹`Bits ℓ n₀ = constBits (V.reverse (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₂
{-
open import IO
import IO.Primitive
-}
open import Data.One
open import Data.Two
open import Data.Product
--open import Coinduction
open import FunUniverse.Agda
--open import Data.Nat.Show
open FunAdder agdaFunOps
open FunOps agdaFunOps
import FunUniverse.Cost as Cost
module TimeCost = FunOps Cost.timeOps
{-
putBit : 𝟚 → IO 𝟙
putBit 1₂ = putStr "1"
putBit 0₂ = putStr "0"
putBits : ∀ {n} → Vec 𝟚 n → IO 𝟙
putBits [] = return _
putBits (x ∷ bs) = ♯ putBit x >> ♯ putBits bs
-}
arg1 = ℕ▹`Bits 8 0x0b _
arg2 = ℕ▹`Bits 8 0x1f _
result = adder (arg1 , arg2)
adder-cost : ℕ → ℕ
adder-cost n = FunAdder.adder Cost.timeOps {n}
{-
mainIO : IO 𝟙
mainIO = ♯ putBits arg1 >>
♯ (♯ putStr " + " >>
♯ (♯ putBits arg2 >>
♯ (♯ putStr " = " >>
♯ (♯ putBits result >>
♯ (♯ putStr " cost:" >>
♯ putStr (show (adder-cost 8)))))))
main : IO.Primitive.IO 𝟙
main = IO.run mainIO
-}
|
use mapAccum
|
adder: use mapAccum
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.