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
0b848c7f1bc49d5ae50193b8abe25a13694ce804
experiments/Coerce.agda
experiments/Coerce.agda
{-# OPTIONS --guardedness-preserving-type-constructors #-} -- Agda specification of `coerce` -- -- Incomplete: too much work just to make type system happy. -- Conversions between morally identical types obscure -- the operational content. open import Function open import Data.Product open import Data.Maybe open import Data.Bool hiding (_≟_) open import Data.Sum open import Data.Nat import Data.Fin as Fin open Fin using (Fin ; zero ; suc) open import Relation.Nullary using (Dec ; yes ; no) open import Coinduction renaming (Rec to Fix ; fold to roll ; unfold to unroll) -- Things fail sometimes postulate fail : ∀ {ℓ} {A : Set ℓ} → A -- I know what I'm doing postulate unsafeCast : ∀ {A B : Set} → A → B data Data : Set where base : ℕ → Data pair : Data → Data → Data plus : Data → Data → Data var : ℕ → Data fix : Data → Data -- infinite environments Env : ∀ {ℓ} → Set ℓ → Set ℓ Env A = ℕ → A infixr 0 decide_then_else_ decide_then_else_ : ∀ {p ℓ} {P : Set p} {A : Set ℓ} → Dec P → A → A → A decide yes p then x else y = x decide no ¬p then x else y = y --update : ∀ {ℓ} {A : Set ℓ} → ℕ → A → Env A → Env A --update n v env = λ m → decide n ≟ m then v else env m prepend : ∀ {ℓ} {A : Set ℓ} → A → Env A → Env A prepend v env zero = v prepend v env (suc m) = env m -- universe construction; always terminating. {-# NO_TERMINATION_CHECK #-} uc : Data → Env Set → Set uc (base x) ρ = Fin x uc (pair S T) ρ = uc S ρ × uc T ρ uc (plus S T) ρ = uc S ρ ⊎ uc T ρ uc (var x) ρ = ρ x uc (fix D) ρ = set where set = Fix (♯ (uc D (prepend set ρ))) ∅ : ∀ {ℓ} {A : Set ℓ} → Env A ∅ = fail my-nat-data : Data my-nat-data = fix (plus (base 1) (var zero)) My-nat : Set My-nat = uc my-nat-data ∅ my-zero : My-nat my-zero = roll (inj₁ zero) my-one : My-nat my-one = roll (inj₂ (roll (inj₁ zero))) -- nonterminating stuff botData : Data botData = fix (var zero) botType : Set botType = uc botData ∅ {-# NON_TERMINATING #-} ⋯ : botType ⋯ = roll (roll (roll (roll (roll (roll ⋯))))) shift : ℕ → ℕ → Data → Data shift c d (base x) = base x shift c d (pair S T) = pair (shift c d S) (shift c d T) shift c d (plus S T) = plus (shift c d S) (shift c d T) shift c d (var k) = decide suc k ≤? c then var k else var (k + d) shift c d (fix D) = fix (shift c d D) _[_↦_] : Data → ℕ → Data → Data base x [ n ↦ S ] = base x pair T T₁ [ n ↦ S ] = pair (T [ n ↦ S ]) (T₁ [ n ↦ S ]) plus T T₁ [ n ↦ S ] = plus (T [ n ↦ S ]) (T₁ [ n ↦ S ]) var k [ n ↦ S ] = decide k ≟ n then S else var k fix T [ n ↦ S ] = fix (T [ n + 1 ↦ shift 0 1 S ]) import Level open import Category.Monad open RawMonad {Level.zero} monad {-# NON_TERMINATING #-} coerce : (S : Data) → (T : Data) → Maybe (uc S ∅ → (uc T ∅)) coerce (base m) (base n) with m ≤? n ... | yes m≤n = just (λ s → Fin.inject≤ s m≤n) ... | no m>n = nothing coerce (pair S₁ S₂) (pair T₁ T₂) = coerce S₁ T₁ >>= (λ f₁ → coerce S₂ T₂ >>= (λ f₂ → return (λ s → f₁ (proj₁ s) , f₂ (proj₂ s)))) coerce (plus S₁ S₂) (plus T₁ T₂) = coerce S₁ T₁ >>= (λ f₁ → coerce S₂ T₂ >>= (λ f₂ → return [ inj₁ ∘ f₁ , inj₂ ∘ f₂ ])) coerce (var j) T = nothing -- should not happen coerce (fix S) T = let S′ = S [ 0 ↦ fix S ] in coerce S′ T >>= (λ f → return (λ { (roll x) → f (unsafeCast x) })) coerce S (fix T) = let T′ = T [ 0 ↦ fix T ] in coerce S T′ >>= (λ f → return (λ x → (roll (unsafeCast (f x))))) coerce _ _ = nothing
{-# OPTIONS --guardedness-preserving-type-constructors #-} -- Agda specification of `coerce` -- -- Incomplete: too much work just to make type system happy. -- Conversions between morally identical types obscure -- the operational content. open import Function open import Data.Product open import Data.Maybe open import Data.Bool hiding (_≟_) open import Data.Sum open import Data.Nat import Data.Fin as Fin open Fin using (Fin ; zero ; suc) open import Relation.Nullary using (Dec ; yes ; no) open import Coinduction renaming (Rec to Fix ; fold to roll ; unfold to unroll) -- Things fail sometimes postulate fail : ∀ {ℓ} {A : Set ℓ} → A -- I know what I'm doing postulate unsafeCast : ∀ {A B : Set} → A → B data Data : Set where base : ℕ → Data pair : Data → Data → Data plus : Data → Data → Data var : ℕ → Data fix : Data → Data -- infinite environments Env : ∀ {ℓ} → Set ℓ → Set ℓ Env A = ℕ → A infixr 0 decide_then_else_ decide_then_else_ : ∀ {p ℓ} {P : Set p} {A : Set ℓ} → Dec P → A → A → A decide yes p then x else y = x decide no ¬p then x else y = y --update : ∀ {ℓ} {A : Set ℓ} → ℕ → A → Env A → Env A --update n v env = λ m → decide n ≟ m then v else env m prepend : ∀ {ℓ} {A : Set ℓ} → A → Env A → Env A prepend v env zero = v prepend v env (suc m) = env m -- universe construction; always terminating. {-# NO_TERMINATION_CHECK #-} uc : Data → Env Set → Set uc (base x) ρ = Fin x uc (pair S T) ρ = uc S ρ × uc T ρ uc (plus S T) ρ = uc S ρ ⊎ uc T ρ uc (var x) ρ = ρ x uc (fix D) ρ = set where set = Fix (♯ (uc D (prepend set ρ))) ∅ : ∀ {ℓ} {A : Set ℓ} → Env A ∅ = fail my-nat-data : Data my-nat-data = fix (plus (base 1) (var zero)) My-nat : Set My-nat = uc my-nat-data ∅ my-zero : My-nat my-zero = roll (inj₁ zero) my-one : My-nat my-one = roll (inj₂ (roll (inj₁ zero))) -- nonterminating stuff botData : Data botData = fix (var zero) botType : Set botType = uc botData ∅ {-# NON_TERMINATING #-} ⋯ : botType ⋯ = roll (roll (roll (roll (roll (roll ⋯))))) shift : ℕ → ℕ → Data → Data shift c d (base x) = base x shift c d (pair S T) = pair (shift c d S) (shift c d T) shift c d (plus S T) = plus (shift c d S) (shift c d T) shift c d (var k) = decide suc k ≤? c then var k else var (k + d) shift c d (fix D) = fix (shift c d D) _[_↦_] : Data → ℕ → Data → Data base x [ n ↦ S ] = base x pair T T₁ [ n ↦ S ] = pair (T [ n ↦ S ]) (T₁ [ n ↦ S ]) plus T T₁ [ n ↦ S ] = plus (T [ n ↦ S ]) (T₁ [ n ↦ S ]) var k [ n ↦ S ] = decide k ≟ n then S else var k fix T [ n ↦ S ] = fix (T [ n + 1 ↦ shift 0 1 S ]) import Level open import Category.Monad open RawMonad {Level.zero} monad -- terminating specification of `coerce` -- it is terminating because: -- -- 1. if `fix T` is contractive (i. e., fix T != μX. X), -- then the `fix T` case will trigger one of the other cases. -- -- 2. in every other case, the input data `s` is stripped of -- a constructor. -- -- This `coerce` is an interpreter. -- The Scala `coerce` is the corresponding compiler. -- {-# NON_TERMINATING #-} coerce : (S : Data) → (T : Data) → uc S ∅ → Maybe (uc T ∅) coerce (base m) (base n) s with m ≤? n ... | yes m≤n = just (Fin.inject≤ s m≤n) ... | no m>n = nothing coerce (pair S₁ S₂) (pair T₁ T₂) (s₁ , s₂) = coerce S₁ T₁ s₁ >>= (λ t₁ → coerce S₂ T₂ s₂ >>= (λ t₂ → return (t₁ , t₂))) coerce (plus S₁ S₂) (plus T₁ T₂) (inj₁ s₁) = coerce S₁ T₁ s₁ >>= (λ t₁ → just (inj₁ t₁)) coerce (plus S₁ S₂) (plus T₁ T₂) (inj₂ s₂) = coerce S₂ T₂ s₂ >>= (λ t₂ → just (inj₂ t₂)) coerce (var j) T s = nothing -- should not happen coerce (fix S) T (roll x) = let S′ = S [ 0 ↦ fix S ] in coerce S′ T (unsafeCast x) coerce S (fix T) s = let T′ = T [ 0 ↦ fix T ] in coerce S T′ s >>= (λ t → just (roll (unsafeCast t))) coerce _ _ _ = nothing
make agda spec of `coerce` terminating for contractive types
make agda spec of `coerce` terminating for contractive types
Agda
mit
yfcai/CREG
634f458ffffacc3e5bc977bed4de2be936703233
New/Lang.agda
New/Lang.agda
module New.Lang where open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Sum open import New.Types public open import Base.Syntax.Context Type public open import Base.Syntax.Vars Type public open import Base.Data.DependentList public module _ where data Const : (τ : Type) → Set where data Term (Γ : Context) : (τ : Type) → Set where -- constants aka. primitives const : ∀ {τ} → (c : Const τ) → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ -- we use de Bruijn indicies, so we don't need binding occurrences. abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weaken Γ₁≼Γ₂ (const c) = const c weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) open import Base.Denotation.Environment Type ⟦_⟧Type public ⟦_⟧Const : ∀ {τ} → Const τ → ⟦ τ ⟧Type ⟦_⟧Const () ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧Context → ⟦ τ ⟧Type ⟦ const c ⟧Term ρ = ⟦ c ⟧Const ⟦ var x ⟧Term ρ = ⟦ x ⟧Var ρ ⟦ app s t ⟧Term ρ = ⟦ s ⟧Term ρ (⟦ t ⟧Term ρ) ⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ) open import Theorem.CongApp open import Postulate.Extensionality weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂} (t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧Context) → ⟦ weaken Γ₁≼Γ₂ t ⟧Term ρ ≡ ⟦ t ⟧Term (⟦ Γ₁≼Γ₂ ⟧≼ ρ) 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) ρ = refl
module New.Lang where open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Sum open import New.Types public open import Base.Syntax.Context Type public open import Base.Syntax.Vars Type public open import Base.Data.DependentList public module _ where data Const : (τ : Type) → Set where data Term (Γ : Context) : (τ : Type) → Set where -- constants aka. primitives const : ∀ {τ} → (c : Const τ) → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ -- we use de Bruijn indicies, so we don't need binding occurrences. abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weaken Γ₁≼Γ₂ (const c) = const c weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) 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) open import Base.Denotation.Environment Type ⟦_⟧Type public ⟦_⟧Const : ∀ {τ} → Const τ → ⟦ τ ⟧Type ⟦_⟧Const () ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧Context → ⟦ τ ⟧Type ⟦ const c ⟧Term ρ = ⟦ c ⟧Const ⟦ var x ⟧Term ρ = ⟦ x ⟧Var ρ ⟦ app s t ⟧Term ρ = ⟦ s ⟧Term ρ (⟦ t ⟧Term ρ) ⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ) open import Theorem.CongApp open import Postulate.Extensionality weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂} (t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧Context) → ⟦ weaken Γ₁≼Γ₂ t ⟧Term ρ ≡ ⟦ t ⟧Term (⟦ Γ₁≼Γ₂ ⟧≼ ρ) 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) ρ = refl
Add helpers
Add helpers
Agda
mit
inc-lc/ilc-agda
be5ee7eec0dd2371c546256f7625b36c21943d03
Popl14/Change/Specification.agda
Popl14/Change/Specification.agda
module Popl14.Change.Specification where -- Denotation-as-specification for Calculus Popl14 -- -- Contents -- - ⟦_⟧Δ : binding-time-shifted derive -- - Validity and correctness lemmas for ⟦_⟧Δ -- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments -- - `corollary-closed`: binding-time-shifted main theorem open import Popl14.Syntax.Term open import Popl14.Denotation.Value open import Popl14.Change.Validity open import Popl14.Denotation.Evaluation public open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Product open import Data.Integer open import Structure.Bag.Popl14 open import Theorem.Groups-Popl14 open import Theorem.CongApp open import Postulate.Extensionality ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → Change τ (⟦ t ⟧ ρ) correctness : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → after {τ} (⟦ t ⟧Δ ρ dρ) ≡ ⟦ t ⟧ (update dρ) ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → ΔEnv Γ ρ → Change τ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (intlit n) ρ dρ = + 0 ⟦_⟧Δ (add s t) ρ dρ = ⟦ s ⟧Δ ρ dρ + ⟦ t ⟧Δ ρ dρ ⟦_⟧Δ (minus t) ρ dρ = - ⟦ t ⟧Δ ρ dρ ⟦_⟧Δ empty ρ dρ = emptyBag ⟦_⟧Δ (insert s t) ρ dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δn = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in (singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b) ⟦_⟧Δ (union s t) ρ dρ = ⟦ s ⟧Δ ρ dρ ++ ⟦ t ⟧Δ ρ dρ ⟦_⟧Δ (negate t) ρ dρ = negateBag (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (flatmap s t) ρ dρ = let f = ⟦ s ⟧ ρ b = ⟦ t ⟧ ρ Δf : Change (int ⇒ bag) f Δf = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b ⟦_⟧Δ (sum t) ρ dρ = sumBag (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = cons (λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ)) (λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (update ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) • update dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • update dρ) ≡⟨⟩ ⟦ t ⟧ (update (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎) where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → ⟦ x ⟧ ρ ⊞₍ τ ₎ ⟦ x ⟧ΔVar ρ dρ ≡ ⟦ x ⟧ (update dρ) correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (intlit n) ρ dρ = right-id-int n correctness (add s t) ρ dρ = trans (mn·pq=mp·nq {⟦ s ⟧ ρ} {⟦ t ⟧ ρ} {⟦ s ⟧Δ ρ dρ} {⟦ t ⟧Δ ρ dρ}) (cong₂ _+_ (correctness s ρ dρ) (correctness t ρ dρ)) correctness (minus t) ρ dρ = trans (-m·-n=-mn {⟦ t ⟧ ρ} {⟦ t ⟧Δ ρ dρ}) (cong -_ (correctness t ρ dρ)) correctness empty ρ dρ = right-id-bag emptyBag correctness (insert s t) ρ dρ = let n = ⟦ s ⟧ ρ b = ⟦ t ⟧ ρ n′ = ⟦ s ⟧ (update dρ) b′ = ⟦ t ⟧ (update dρ) Δn = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in begin (singletonBag n ++ b) ++ (singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b) ≡⟨ a++[b\\a]=b ⟩ singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb) ≡⟨ cong₂ _++_ (cong singletonBag (correctness s ρ dρ)) (correctness t ρ dρ) ⟩ singletonBag n′ ++ b′ ∎ where open ≡-Reasoning correctness (union s t) ρ dρ = trans (ab·cd=ac·bd {⟦ s ⟧ ρ} {⟦ t ⟧ ρ} {⟦ s ⟧Δ ρ dρ} {⟦ t ⟧Δ ρ dρ}) (cong₂ _++_ (correctness s ρ dρ) (correctness t ρ dρ)) correctness (negate t) ρ dρ = trans (-a·-b=-ab {⟦ t ⟧ ρ} {⟦ t ⟧Δ ρ dρ}) (cong negateBag (correctness t ρ dρ)) correctness (flatmap s t) ρ dρ = let f = ⟦ s ⟧ ρ b = ⟦ t ⟧ ρ Δf = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in trans (a++[b\\a]=b {flatmapBag f b} {flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf) (b ⊞₍ base base-bag ₎ Δb)}) (cong₂ flatmapBag (correctness s ρ dρ) (correctness t ρ dρ)) correctness (sum t) ρ dρ = let b = ⟦ t ⟧ ρ Δb = ⟦ t ⟧Δ ρ dρ in trans (sym homo-sum) (cong sumBag (correctness t ρ dρ)) correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f = ⟦ s ⟧ ρ g = ⟦ s ⟧ (update dρ) u = ⟦ t ⟧ ρ v = ⟦ t ⟧ (update dρ) Δf = ⟦ s ⟧Δ ρ dρ Δu = ⟦ t ⟧Δ ρ dρ in begin f u ⊞₍ τ ₎ call-change Δf u Δu ≡⟨ sym (is-valid Δf u Δu) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil-change σ v • dρ in begin ⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (update dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v • update dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Change σ v) → ⟦ t ⟧ ∅ (after {σ} dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after {σ} dv) ≡⟨ cong (λ hole → hole (after {σ} dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} dv) ≡⟨ is-valid (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before {σ} dv) ⊞₍ τ ₎ call-change Δf v dv ∎ where open ≡-Reasoning
module Popl14.Change.Specification where -- Denotation-as-specification for Calculus Popl14 -- -- Contents -- - ⟦_⟧Δ : binding-time-shifted derive -- - Validity and correctness lemmas for ⟦_⟧Δ -- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments -- - `corollary-closed`: binding-time-shifted main theorem open import Popl14.Syntax.Term open import Popl14.Denotation.Value open import Popl14.Change.Validity open import Popl14.Denotation.Evaluation public open import Relation.Binary.PropositionalEquality open import Data.Integer open import Theorem.Groups-Popl14 open import Theorem.CongApp open import Postulate.Extensionality ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → Change τ (⟦ t ⟧ ρ) correctness : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → after {τ} (⟦ t ⟧Δ ρ dρ) ≡ ⟦ t ⟧ (update dρ) ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → ΔEnv Γ ρ → Change τ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (intlit n) ρ dρ = + 0 ⟦_⟧Δ (add s t) ρ dρ = ⟦ s ⟧Δ ρ dρ + ⟦ t ⟧Δ ρ dρ ⟦_⟧Δ (minus t) ρ dρ = - ⟦ t ⟧Δ ρ dρ ⟦_⟧Δ empty ρ dρ = emptyBag ⟦_⟧Δ (insert s t) ρ dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δn = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in (singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b) ⟦_⟧Δ (union s t) ρ dρ = ⟦ s ⟧Δ ρ dρ ++ ⟦ t ⟧Δ ρ dρ ⟦_⟧Δ (negate t) ρ dρ = negateBag (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (flatmap s t) ρ dρ = let f = ⟦ s ⟧ ρ b = ⟦ t ⟧ ρ Δf : Change (int ⇒ bag) f Δf = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b ⟦_⟧Δ (sum t) ρ dρ = sumBag (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = cons (λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ)) (λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (update ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) • update dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • update dρ) ≡⟨⟩ ⟦ t ⟧ (update (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎) where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → ⟦ x ⟧ ρ ⊞₍ τ ₎ ⟦ x ⟧ΔVar ρ dρ ≡ ⟦ x ⟧ (update dρ) correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (intlit n) ρ dρ = right-id-int n correctness (add s t) ρ dρ = trans (mn·pq=mp·nq {⟦ s ⟧ ρ} {⟦ t ⟧ ρ} {⟦ s ⟧Δ ρ dρ} {⟦ t ⟧Δ ρ dρ}) (cong₂ _+_ (correctness s ρ dρ) (correctness t ρ dρ)) correctness (minus t) ρ dρ = trans (-m·-n=-mn {⟦ t ⟧ ρ} {⟦ t ⟧Δ ρ dρ}) (cong -_ (correctness t ρ dρ)) correctness empty ρ dρ = right-id-bag emptyBag correctness (insert s t) ρ dρ = let n = ⟦ s ⟧ ρ b = ⟦ t ⟧ ρ n′ = ⟦ s ⟧ (update dρ) b′ = ⟦ t ⟧ (update dρ) Δn = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in begin (singletonBag n ++ b) ++ (singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b) ≡⟨ a++[b\\a]=b ⟩ singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb) ≡⟨ cong₂ _++_ (cong singletonBag (correctness s ρ dρ)) (correctness t ρ dρ) ⟩ singletonBag n′ ++ b′ ∎ where open ≡-Reasoning correctness (union s t) ρ dρ = trans (ab·cd=ac·bd {⟦ s ⟧ ρ} {⟦ t ⟧ ρ} {⟦ s ⟧Δ ρ dρ} {⟦ t ⟧Δ ρ dρ}) (cong₂ _++_ (correctness s ρ dρ) (correctness t ρ dρ)) correctness (negate t) ρ dρ = trans (-a·-b=-ab {⟦ t ⟧ ρ} {⟦ t ⟧Δ ρ dρ}) (cong negateBag (correctness t ρ dρ)) correctness (flatmap s t) ρ dρ = let f = ⟦ s ⟧ ρ b = ⟦ t ⟧ ρ Δf = ⟦ s ⟧Δ ρ dρ Δb = ⟦ t ⟧Δ ρ dρ in trans (a++[b\\a]=b {flatmapBag f b} {flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf) (b ⊞₍ base base-bag ₎ Δb)}) (cong₂ flatmapBag (correctness s ρ dρ) (correctness t ρ dρ)) correctness (sum t) ρ dρ = let b = ⟦ t ⟧ ρ Δb = ⟦ t ⟧Δ ρ dρ in trans (sym homo-sum) (cong sumBag (correctness t ρ dρ)) correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f = ⟦ s ⟧ ρ g = ⟦ s ⟧ (update dρ) u = ⟦ t ⟧ ρ v = ⟦ t ⟧ (update dρ) Δf = ⟦ s ⟧Δ ρ dρ Δu = ⟦ t ⟧Δ ρ dρ in begin f u ⊞₍ τ ₎ call-change Δf u Δu ≡⟨ sym (is-valid Δf u Δu) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil-change σ v • dρ in begin ⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (update dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v • update dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : ΔEnv Γ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Change σ v) → ⟦ t ⟧ ∅ (after {σ} dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after {σ} dv) ≡⟨ cong (λ hole → hole (after {σ} dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} dv) ≡⟨ is-valid (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before {σ} dv) ⊞₍ τ ₎ call-change Δf v dv ∎ where open ≡-Reasoning
Remove unnecessary imports.
Remove unnecessary imports. Old-commit-hash: 01c6cb718906e9a41b8156076fe9e837257c7a1f
Agda
mit
inc-lc/ilc-agda
584a95def9b81558cd9bc660db3fc2eb687dd915
PLDI14-List-of-Theorems.agda
PLDI14-List-of-Theorems.agda
module PLDI14-List-of-Theorems where open import Function -- 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 name. 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) -- (d) ChangeAlgebra.diff -- (e) IsChangeAlgebra.update-diff 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) -- For each plugin requirement, we include its definition and -- a concrete instantiation called "Popl14" with integers and -- bags of integers as base types. -- Plugin Requirement 3.1 (Domains of base types) open import Parametric.Denotation.Value using (Structure) open import Popl14.Denotation.Value using (⟦_⟧Base) -- Definition 3.2 (Domains) open Parametric.Denotation.Value.Structure using (⟦_⟧Type) -- Plugin Requirement 3.3 (Evaluation of constants) open import Parametric.Denotation.Evaluation using (Structure) open import Popl14.Denotation.Evaluation using (⟦_⟧Const) -- Definition 3.4 (Environments) open import Base.Denotation.Environment using (⟦_⟧Context) -- Definition 3.5 (Evaluation) open Parametric.Denotation.Evaluation.Structure using (⟦_⟧Term) -- Plugin Requirement 3.6 (Changes on base types) open import Parametric.Change.Validity using (Structure) open import Popl14.Change.Validity using (change-algebra-base-family) -- Definition 3.7 (Changes) open Parametric.Change.Validity.Structure using (change-algebra) -- Definition 3.8 (Change environments) open Parametric.Change.Validity.Structure using (environment-changes) -- Plugin Requirement 3.9 (Change semantics for constants) open import Parametric.Change.Specification using (Structure) open import Popl14.Change.Specification using (specification-structure) -- Definition 3.10 (Change semantics) open Parametric.Change.Specification.Structure using (⟦_⟧Δ) -- Lemma 3.11 (Change semantics is the derivative of semantics) open Parametric.Change.Specification.Structure using (correctness) -- Definition 3.12 (Erasure) import Parametric.Change.Implementation open Parametric.Change.Implementation.Structure using (_≈_) open import Popl14.Change.Implementation using (implements-base) -- Lemma 3.13 (The erased version of a change is almost the same) open Parametric.Change.Implementation.Structure using (carry-over) -- Lemma 3.14 (⟦ t ⟧Δ erases to Derive(t)) import Parametric.Change.Correctness open Parametric.Change.Correctness.Structure using (main-theorem)
module PLDI14-List-of-Theorems where open import Function -- 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 name. 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) -- (d) ChangeAlgebra.diff -- (e) IsChangeAlgebra.update-diff 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) -- For each plugin requirement, we include its definition and -- a concrete instantiation called "Popl14" with integers and -- bags of integers as base types. -- Plugin Requirement 3.1 (Domains of base types) open import Parametric.Denotation.Value using (Structure) open import Popl14.Denotation.Value using (⟦_⟧Base) -- Definition 3.2 (Domains) open Parametric.Denotation.Value.Structure using (⟦_⟧Type) -- Plugin Requirement 3.3 (Evaluation of constants) open import Parametric.Denotation.Evaluation using (Structure) open import Popl14.Denotation.Evaluation using (⟦_⟧Const) -- Definition 3.4 (Environments) open import Base.Denotation.Environment using (⟦_⟧Context) -- Definition 3.5 (Evaluation) open Parametric.Denotation.Evaluation.Structure using (⟦_⟧Term) -- Plugin Requirement 3.6 (Changes on base types) open import Parametric.Change.Validity using (Structure) open import Popl14.Change.Validity using (change-algebra-base-family) -- Definition 3.7 (Changes) open Parametric.Change.Validity.Structure using (change-algebra) -- Definition 3.8 (Change environments) open Parametric.Change.Validity.Structure using (environment-changes) -- Plugin Requirement 3.9 (Change semantics for constants) open import Parametric.Change.Specification using (Structure) open import Popl14.Change.Specification using (specification-structure) -- Definition 3.10 (Change semantics) open Parametric.Change.Specification.Structure using (⟦_⟧Δ) -- Lemma 3.11 (Change semantics is the derivative of semantics) open Parametric.Change.Specification.Structure using (correctness) -- Definition 3.12 (Erasure) import Parametric.Change.Implementation open Parametric.Change.Implementation.Structure using (_≈_) open import Popl14.Change.Implementation using (implements-base) -- Lemma 3.13 (The erased version of a change is almost the same) open Parametric.Change.Implementation.Structure using (carry-over) -- Lemma 3.14 (⟦ t ⟧Δ erases to Derive(t)) import Parametric.Change.Correctness open Parametric.Change.Correctness.Structure using (derive-correct-closed) -- Theorem 3.15 (Correctness of differentiation) open Parametric.Change.Correctness.Structure using (main-theorem)
correct list of theorems
correct list of theorems Old-commit-hash: c915c2bba4a8fd18c2b4d5af429fdedf6042ca83
Agda
mit
inc-lc/ilc-agda
d0bb4f7bbbfa2acbfe952a24f1f347e8b7bac989
lib/Algebra/Field.agda
lib/Algebra/Field.agda
module Algebra.Field where open import Relation.Binary.PropositionalEquality.NP import Algebra.FunctionProperties.Eq as FP open import Data.Nat.NP using (ℕ; zero; suc; fold) import Data.Integer as ℤ open ℤ using (ℤ) record Field-Ops {ℓ} (A : Set ℓ) : Set ℓ where infixl 6 _+_ _−_ infixl 7 _*_ _/_ field _+_ _*_ : A → A → A 0ᶠ 1ᶠ : A 0-_ : A → A _⁻¹ : A → A _−_ _/_ : A → A → A a − b = a + 0- b a / b = a * b ⁻¹ -0ᶠ -1ᶠ : A -0ᶠ = 0- 0ᶠ -1ᶠ = 0- 1ᶠ sucᶠ : A → A sucᶠ = _+_ 1ᶠ predᶠ : A → A predᶠ x = x − 1ᶠ ℕ[_] : ℕ → A ℕ[_] = fold 0ᶠ sucᶠ ℤ[_] : ℤ → A ℤ[ ℤ.+ x ] = ℕ[ x ] ℤ[ ℤ.-[1+ x ] ] = 0- ℕ[ suc x ] _^ℕ_ : A → ℕ → A b ^ℕ e = fold 1ᶠ (_*_ b) e _^ℤ_ : A → ℤ → A b ^ℤ (ℤ.+ x) = b ^ℕ x b ^ℤ ℤ.-[1+ x ] = (b ^ℕ (suc x))⁻¹ record Field-Struct {ℓ} {A : Set ℓ} (field-ops : Field-Ops A) : Set ℓ where open FP {ℓ} {A} open Field-Ops field-ops field 0≢1 : 0ᶠ ≢ 1ᶠ +-assoc : Associative _+_ *-assoc : Associative _*_ +-comm : Commutative _+_ *-comm : Commutative _*_ 0+-identity : LeftIdentity 0ᶠ _+_ 1*-identity : LeftIdentity 1ᶠ _*_ 0--inverse : LeftInverse 0ᶠ 0-_ _+_ ⁻¹-inverse : ∀ {x} → x ≢ 0ᶠ → x ⁻¹ * x ≡ 1ᶠ *-+-distr : _*_ DistributesOverˡ _+_ 0--right-inverse : RightInverse 0ᶠ 0-_ _+_ 0--right-inverse = +-comm ∙ 0--inverse ⁻¹-right-inverse : ∀ {x} → x ≢ 0ᶠ → x * x ⁻¹ ≡ 1ᶠ ⁻¹-right-inverse p = *-comm ∙ ⁻¹-inverse p +0-identity : RightIdentity 0ᶠ _+_ +0-identity = +-comm ∙ 0+-identity *1-identity : RightIdentity 1ᶠ _*_ *1-identity = *-comm ∙ 1*-identity += : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → x + y ≡ x' + y' += {x} {y' = y'} p q = ap (_+_ x) q ∙ ap (λ z → z + y') p *= : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → x * y ≡ x' * y' *= {x} {y' = y'} p q = ap (_*_ x) q ∙ ap (λ z → z * y') p 0-c+c+x : ∀ {c x} → 0- c + c + x ≡ x 0-c+c+x = += 0--inverse refl ∙ 0+-identity +-left-cancel : LeftCancel _+_ +-left-cancel p = ! 0-c+c+x ∙ +-assoc ∙ ap (λ z → 0- _ + z) p ∙ ! +-assoc ∙ 0-c+c+x +-right-cancel : RightCancel _+_ +-right-cancel p = +-left-cancel (+-comm ∙ p ∙ +-comm) 0--involutive : Involutive 0-_ 0--involutive = +-left-cancel (0--right-inverse ∙ ! 0--inverse) -0≡0 : -0ᶠ ≡ 0ᶠ -0≡0 = +-left-cancel (0--right-inverse ∙ ! 0+-identity) open Field-Ops field-ops public record Field {ℓ} (A : Set ℓ) : Set ℓ where field field-ops : Field-Ops A field-struct : Field-Struct field-ops open Field-Struct field-struct public open import Reflection.NP open import Data.List open import Data.Maybe.NP module TermField {a} {A : Set a} (F : Field A) where open Field F pattern _`+_ t u = con (quote _+_) (argᵛʳ t ∷ argᵛʳ u ∷ []) pattern _`*_ t u = con (quote _*_) (argᵛʳ t ∷ argᵛʳ u ∷ []) pattern `0-_ t = conᵛʳ (quote 0-_) t pattern _`⁻¹ t = conᵛʳ (quote _⁻¹) t pattern `0ᶠ = con (quote 0ᶠ) [] pattern `1ᶠ = con (quote 1ᶠ) [] module Decode-RawField {b}{B : Set b}(G : Field-Ops B)(default : Decode-Term B) where module G = Field-Ops G decode-Field : Decode-Term B decode-Field `0ᶠ = just G.0ᶠ decode-Field `1ᶠ = just G.1ᶠ decode-Field (t `⁻¹) = map? G._⁻¹ (decode-Field t) decode-Field (`0- t) = map? G.0-_ (decode-Field t) decode-Field (t `+ u) = ⟪ G._+_ · decode-Field t · decode-Field u ⟫? decode-Field (t `* u) = ⟪ G._*_ · decode-Field t · decode-Field u ⟫? decode-Field t = default t open ≡-Reasoning open import Data.List infixl 6 _+'_ infixl 7 _*'_ data Tm (A : Set) : Set where lit : (n : ℤ) → Tm A var : (x : A) → Tm A _+'_ _*'_ : Tm A → Tm A → Tm A pattern 0' = lit (ℤ.+ 0) pattern 1' = lit (ℤ.+ 1) pattern 2' = lit (ℤ.+ 1) pattern 3' = lit (ℤ.+ 1) pattern -1' = lit ℤ.-[1+ 0 ] pattern -2' = lit ℤ.-[1+ 1 ] pattern -3' = lit ℤ.-[1+ 2 ] record Var (A : Set) : Set where constructor _^'_ field v : A e : ℤ data Nf' (A : Set) : Set where _*'_ : (n : ℤ) (vs : List (Var A)) → Nf' A data Nf (A : Set) : Set where _+'_ : (n : ℤ) (ts' : List (Nf' A)) → Nf A module _ {A : Set} where infixl 6 _+ᴺ_ litᴺ : ℤ → Nf A litᴺ n = n +' [] varᴺ : (x : A) → Nf A varᴺ x = ℤ.+ 0 +' (ℤ.+ 1 *' (x ^' (ℤ.+ 1) ∷ []) ∷ []) _+ᴺ_ : Nf A → Nf A → Nf A (n +' ts) +ᴺ (m +' us) = n ℤ.+ m +' (ts ++ us) infixl 7 _**_ _***_ _*ᴺ_ _**_ : ℤ → List (Nf' A) → Nf A z ** [] = z +' [] m ** (n *' vs ∷ ts) with m ** ts ... | z +' us = z +' ((m ℤ.* n) *' vs ∷ us) _***_ : List (Nf' A) → List (Nf' A) → Nf A ts *** us = ℤ.+ 0 +' (ts ++ us) _*ᴺ_ : Nf A → Nf A → Nf A (n +' ts) *ᴺ (m +' us) = n ℤ.* m +' [] +ᴺ n ** us +ᴺ m ** ts +ᴺ ts *** us module Normalizer {F : Set} (F-field : Field F) where open Field F-field renaming (0-_ to -_) module _ {A : Set} where eval : (A → F) → Tm A → F eval ρ (lit x) = ℤ[ x ] eval ρ (t +' t₁) = eval ρ t + eval ρ t₁ eval ρ (t *' t₁) = eval ρ t * eval ρ t₁ eval ρ (var x) = ρ x norm™ : Tm A → Nf A norm™ (lit x) = litᴺ x norm™ (var x) = varᴺ x norm™ (t +' t₁) = norm™ t +ᴺ norm™ t₁ norm™ (t *' t₁) = norm™ t *ᴺ norm™ t₁ _+''_ : Tm A → Tm A → Tm A -- lit (ℤ.+ 0) +'' u = u t +'' lit (ℤ.+ 0) = t t +'' u = t +' u _*''_ : Tm A → Tm A → Tm A -- lit (ℤ.+ 0) *'' u = lit (ℤ.+ 0) -- lit (ℤ.-[1+_] 0) *'' u = lit (ℤ.+ 0) t *'' lit (ℤ.+ 0) = lit (ℤ.+ 0) t *'' lit (ℤ.-[1+_] 0) = lit (ℤ.+ 0) -- lit (ℤ.+ 1) *'' u = u t *'' lit (ℤ.+ 1) = t t *'' u = t *' u reifyVar : Var A → Tm A reifyVar (v ^' (ℤ.+ n)) = fold (lit (ℤ.+ 1)) (λ x → var v *'' x) n reifyVar (v ^' ℤ.-[1+ n ]) = fold (lit (ℤ.-[1+_] 1)) (λ x → var v *'' x) n reifyNf' : Nf' A → Tm A reifyNf' (n *' vs) = foldr (λ v t → reifyVar v *'' t) (lit n) vs reifyNf : Nf A → Tm A reifyNf (n +' ts') = foldr (λ n t → reifyNf' n +'' t) (lit n) ts' module _ ρ where _∼_ : (t u : Tm A) → Set t ∼ u = eval ρ t ≡ eval ρ u +'∼+'' : ∀ t u → (t +' u) ∼ (t +'' u) +'∼+'' t (lit (ℤ.+ zero)) = +0-identity +'∼+'' t (lit (ℤ.+ (suc x))) = refl +'∼+'' t (lit ℤ.-[1+ x ]) = refl +'∼+'' t (var x) = refl +'∼+'' t (u +' u₁) = refl +'∼+'' t (u *' u₁) = refl {- +-reifyNf : ∀ t u → reifyNf (t +ᴺ u) ∼ (reifyNf t +' reifyNf u) +-reifyNf (n +' ts') (n₁ +' ts'') = {!!} *-reifyNf : ∀ t u → reifyNf (t *ᴺ u) ∼ (reifyNf t *' reifyNf u) *-reifyNf t u = {!!} pf : ∀ t → eval ρ (reifyNf (norm™ t)) ≡ eval ρ t pf (lit n) = refl pf (var x) = refl pf (t +' t₁) rewrite +-reifyNf (norm™ t) (norm™ t₁) | pf t | pf t₁ = refl pf (t *' t₁) rewrite *-reifyNf (norm™ t) (norm™ t₁) | pf t | pf t₁ = refl -} -- -} -- -} -- -} -- -} -- -}
module Algebra.Field where open import Relation.Binary.PropositionalEquality.NP import Algebra.FunctionProperties.Eq as FP open import Data.Nat.NP using (ℕ; zero; suc; fold) import Data.Integer as ℤ open ℤ using (ℤ) record Field-Ops {ℓ} (A : Set ℓ) : Set ℓ where infixl 6 _+_ _−_ infixl 7 _*_ _/_ field _+_ _*_ : A → A → A 0ᶠ 1ᶠ : A 0-_ : A → A _⁻¹ : A → A _−_ _/_ : A → A → A a − b = a + 0- b a / b = a * b ⁻¹ -0ᶠ -1ᶠ : A -0ᶠ = 0- 0ᶠ -1ᶠ = 0- 1ᶠ sucᶠ : A → A sucᶠ = _+_ 1ᶠ predᶠ : A → A predᶠ x = x − 1ᶠ ℕ[_] : ℕ → A ℕ[_] = fold 0ᶠ sucᶠ ℤ[_] : ℤ → A ℤ[ ℤ.+ x ] = ℕ[ x ] ℤ[ ℤ.-[1+ x ] ] = 0- ℕ[ suc x ] _^ℕ_ : A → ℕ → A b ^ℕ e = fold 1ᶠ (_*_ b) e _^ℤ_ : A → ℤ → A b ^ℤ (ℤ.+ x) = b ^ℕ x b ^ℤ ℤ.-[1+ x ] = (b ^ℕ (suc x))⁻¹ record Field-Struct {ℓ} {A : Set ℓ} (field-ops : Field-Ops A) : Set ℓ where open FP {ℓ} {A} open Field-Ops field-ops field 0≢1 : 0ᶠ ≢ 1ᶠ +-assoc : Associative _+_ *-assoc : Associative _*_ +-comm : Commutative _+_ *-comm : Commutative _*_ 0+-identity : LeftIdentity 0ᶠ _+_ 1*-identity : LeftIdentity 1ᶠ _*_ 0--inverse : LeftInverse 0ᶠ 0-_ _+_ ⁻¹-inverse : ∀ {x} → x ≢ 0ᶠ → x ⁻¹ * x ≡ 1ᶠ *-+-distr : _*_ DistributesOverˡ _+_ 0--right-inverse : RightInverse 0ᶠ 0-_ _+_ 0--right-inverse = +-comm ∙ 0--inverse ⁻¹-right-inverse : ∀ {x} → x ≢ 0ᶠ → x * x ⁻¹ ≡ 1ᶠ ⁻¹-right-inverse p = *-comm ∙ ⁻¹-inverse p +0-identity : RightIdentity 0ᶠ _+_ +0-identity = +-comm ∙ 0+-identity *1-identity : RightIdentity 1ᶠ _*_ *1-identity = *-comm ∙ 1*-identity += : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → x + y ≡ x' + y' += {x} {y' = y'} p q = ap (_+_ x) q ∙ ap (λ z → z + y') p *= : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → x * y ≡ x' * y' *= {x} {y' = y'} p q = ap (_*_ x) q ∙ ap (λ z → z * y') p 0-= : ∀ {x x'} → x ≡ x' → 0- x ≡ 0- x' 0-= = ap 0-_ -- Correct naming scheme? +-*-distr : _*_ DistributesOverʳ _+_ +-*-distr = *-comm ∙ *-+-distr ∙ += *-comm *-comm 0-c+c+x : ∀ {c x} → 0- c + c + x ≡ x 0-c+c+x = += 0--inverse refl ∙ 0+-identity +-left-cancel : LeftCancel _+_ +-left-cancel p = ! 0-c+c+x ∙ +-assoc ∙ ap (λ z → 0- _ + z) p ∙ ! +-assoc ∙ 0-c+c+x +-right-cancel : RightCancel _+_ +-right-cancel p = +-left-cancel (+-comm ∙ p ∙ +-comm) *0-zero : RightZero 0ᶠ _*_ *0-zero = +-right-cancel (+= refl (! *1-identity) ∙ ! *-+-distr ∙ *= refl 0+-identity ∙ *1-identity ∙ ! 0+-identity) 0*-zero : LeftZero 0ᶠ _*_ 0*-zero = *-comm ∙ *0-zero -- name ? *-0- : ∀ {x y} → 0- (x * y) ≡ (0- x) * y *-0- = +-right-cancel (0--inverse ∙ ! 0*-zero ∙ *= (! 0--inverse) refl ∙ +-*-distr) -1*-neg : ∀ {x} → -1ᶠ * x ≡ 0- x -1*-neg = ! *-0- ∙ 0-= 1*-identity 0--involutive : Involutive 0-_ 0--involutive = +-left-cancel (0--right-inverse ∙ ! 0--inverse) -0≡0 : -0ᶠ ≡ 0ᶠ -0≡0 = +-left-cancel (0--right-inverse ∙ ! 0+-identity) noZeroDivisor : ∀ {x y} → x ≢ 0ᶠ → y ≢ 0ᶠ → x * y ≢ 0ᶠ noZeroDivisor nx ny x*y≡0ᶠ = ny (! *1-identity ∙ *= refl (! ⁻¹-inverse nx) ∙ *= refl *-comm ∙ ! *-assoc ∙ *= (*-comm ∙ x*y≡0ᶠ) refl ∙ 0*-zero ) open Field-Ops field-ops public record Field {ℓ} (A : Set ℓ) : Set ℓ where field field-ops : Field-Ops A field-struct : Field-Struct field-ops open Field-Struct field-struct public open import Reflection.NP open import Data.List open import Data.Maybe.NP module TermField {a} {A : Set a} (F : Field A) where open Field F pattern _`+_ t u = con (quote _+_) (argᵛʳ t ∷ argᵛʳ u ∷ []) pattern _`*_ t u = con (quote _*_) (argᵛʳ t ∷ argᵛʳ u ∷ []) pattern `0-_ t = conᵛʳ (quote 0-_) t pattern _`⁻¹ t = conᵛʳ (quote _⁻¹) t pattern `0ᶠ = con (quote 0ᶠ) [] pattern `1ᶠ = con (quote 1ᶠ) [] module Decode-RawField {b}{B : Set b}(G : Field-Ops B)(default : Decode-Term B) where module G = Field-Ops G decode-Field : Decode-Term B decode-Field `0ᶠ = just G.0ᶠ decode-Field `1ᶠ = just G.1ᶠ decode-Field (t `⁻¹) = map? G._⁻¹ (decode-Field t) decode-Field (`0- t) = map? G.0-_ (decode-Field t) decode-Field (t `+ u) = ⟪ G._+_ · decode-Field t · decode-Field u ⟫? decode-Field (t `* u) = ⟪ G._*_ · decode-Field t · decode-Field u ⟫? decode-Field t = default t open ≡-Reasoning open import Data.List infixl 6 _+'_ infixl 7 _*'_ data Tm (A : Set) : Set where lit : (n : ℤ) → Tm A var : (x : A) → Tm A _+'_ _*'_ : Tm A → Tm A → Tm A pattern 0' = lit (ℤ.+ 0) pattern 1' = lit (ℤ.+ 1) pattern 2' = lit (ℤ.+ 1) pattern 3' = lit (ℤ.+ 1) pattern -1' = lit ℤ.-[1+ 0 ] pattern -2' = lit ℤ.-[1+ 1 ] pattern -3' = lit ℤ.-[1+ 2 ] record Var (A : Set) : Set where constructor _^'_ field v : A e : ℤ data Nf' (A : Set) : Set where _*'_ : (n : ℤ) (vs : List (Var A)) → Nf' A data Nf (A : Set) : Set where _+'_ : (n : ℤ) (ts' : List (Nf' A)) → Nf A module _ {A : Set} where infixl 6 _+ᴺ_ litᴺ : ℤ → Nf A litᴺ n = n +' [] varᴺ : (x : A) → Nf A varᴺ x = ℤ.+ 0 +' (ℤ.+ 1 *' (x ^' (ℤ.+ 1) ∷ []) ∷ []) _+ᴺ_ : Nf A → Nf A → Nf A (n +' ts) +ᴺ (m +' us) = n ℤ.+ m +' (ts ++ us) infixl 7 _**_ _***_ _*ᴺ_ _**_ : ℤ → List (Nf' A) → Nf A z ** [] = z +' [] m ** (n *' vs ∷ ts) with m ** ts ... | z +' us = z +' ((m ℤ.* n) *' vs ∷ us) _***_ : List (Nf' A) → List (Nf' A) → Nf A ts *** us = ℤ.+ 0 +' (ts ++ us) _*ᴺ_ : Nf A → Nf A → Nf A (n +' ts) *ᴺ (m +' us) = n ℤ.* m +' [] +ᴺ n ** us +ᴺ m ** ts +ᴺ ts *** us module Normalizer {F : Set} (F-field : Field F) where open Field F-field renaming (0-_ to -_) module _ {A : Set} where eval : (A → F) → Tm A → F eval ρ (lit x) = ℤ[ x ] eval ρ (t +' t₁) = eval ρ t + eval ρ t₁ eval ρ (t *' t₁) = eval ρ t * eval ρ t₁ eval ρ (var x) = ρ x norm™ : Tm A → Nf A norm™ (lit x) = litᴺ x norm™ (var x) = varᴺ x norm™ (t +' t₁) = norm™ t +ᴺ norm™ t₁ norm™ (t *' t₁) = norm™ t *ᴺ norm™ t₁ _+''_ : Tm A → Tm A → Tm A -- lit (ℤ.+ 0) +'' u = u t +'' lit (ℤ.+ 0) = t t +'' u = t +' u _*''_ : Tm A → Tm A → Tm A -- lit (ℤ.+ 0) *'' u = lit (ℤ.+ 0) -- lit (ℤ.-[1+_] 0) *'' u = lit (ℤ.+ 0) t *'' lit (ℤ.+ 0) = lit (ℤ.+ 0) t *'' lit (ℤ.-[1+_] 0) = lit (ℤ.+ 0) -- lit (ℤ.+ 1) *'' u = u t *'' lit (ℤ.+ 1) = t t *'' u = t *' u reifyVar : Var A → Tm A reifyVar (v ^' (ℤ.+ n)) = fold (lit (ℤ.+ 1)) (λ x → var v *'' x) n reifyVar (v ^' ℤ.-[1+ n ]) = fold (lit (ℤ.-[1+_] 1)) (λ x → var v *'' x) n reifyNf' : Nf' A → Tm A reifyNf' (n *' vs) = foldr (λ v t → reifyVar v *'' t) (lit n) vs reifyNf : Nf A → Tm A reifyNf (n +' ts') = foldr (λ n t → reifyNf' n +'' t) (lit n) ts' module _ ρ where _∼_ : (t u : Tm A) → Set t ∼ u = eval ρ t ≡ eval ρ u +'∼+'' : ∀ t u → (t +' u) ∼ (t +'' u) +'∼+'' t (lit (ℤ.+ zero)) = +0-identity +'∼+'' t (lit (ℤ.+ (suc x))) = refl +'∼+'' t (lit ℤ.-[1+ x ]) = refl +'∼+'' t (var x) = refl +'∼+'' t (u +' u₁) = refl +'∼+'' t (u *' u₁) = refl {- +-reifyNf : ∀ t u → reifyNf (t +ᴺ u) ∼ (reifyNf t +' reifyNf u) +-reifyNf (n +' ts') (n₁ +' ts'') = {!!} *-reifyNf : ∀ t u → reifyNf (t *ᴺ u) ∼ (reifyNf t *' reifyNf u) *-reifyNf t u = {!!} pf : ∀ t → eval ρ (reifyNf (norm™ t)) ≡ eval ρ t pf (lit n) = refl pf (var x) = refl pf (t +' t₁) rewrite +-reifyNf (norm™ t) (norm™ t₁) | pf t | pf t₁ = refl pf (t *' t₁) rewrite *-reifyNf (norm™ t) (norm™ t₁) | pf t | pf t₁ = refl -} -- -} -- -} -- -} -- -} -- -}
Add more derived Field properties
Add more derived Field properties
Agda
bsd-3-clause
crypto-agda/agda-nplib
e295b6ccc86d810b0a1b3567f66adcdaf0bb065f
arrow.agda
arrow.agda
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))
data Bool : Set where true : Bool false : Bool _∨_ : Bool → Bool → Bool true ∨ _ = true false ∨ b = b _∧_ : Bool → Bool → Bool false ∧ _ = false true ∧ b = b if_then_else_ : {A : Set} → Bool → A → A → A if true then a else _ = a if false then _ else 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 : Relation testp = consider proofs cms (5 ⇒ (⇒ 10)) testd : Relation testd = consider proofs cms (5 ⇒ (⇒ 4)) tests : Relation tests = consider proofs cms (5 ⇒ (⇒ 3)) testu : Relation testu = consider proofs cms (6 ⇒ (⇒ 1)) testcl : List ℕ testcl = closure proofs (5 ∷ [])
Add tests
Add tests
Agda
mit
louisswarren/hieretikz
fab1df4c820031858a8111d3a016a96fcf1bd2e4
Parametric/Denotation/CachingEvaluation.agda
Parametric/Denotation/CachingEvaluation.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Caching evaluation ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation module Parametric.Denotation.CachingEvaluation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality module Structure where {- -- Prototype here the type-correctness of a simple non-standard semantics. -- This describes a simplified version of the transformation by Liu and -- Tanenbaum, PEPM 1995 - but 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*). -} open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level -- Type semantics for this scenario. ⟦_⟧TypeHidCache : (τ : Type) → Set₁ ⟦ base ι ⟧TypeHidCache = Lift ⟦ base ι ⟧ ⟦ σ ⇒ τ ⟧TypeHidCache = ⟦ σ ⟧TypeHidCache → (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧TypeHidCache × τ₁ ) open import Parametric.Syntax.MType as MType open MType.Structure Base open import Parametric.Syntax.MTerm as MTerm open MTerm.Structure Const open import Function hiding (const) ⟦_⟧ValType : (τ : ValType) → Set ⟦_⟧CompType : (τ : CompType) → Set ⟦ U c ⟧ValType = ⟦ c ⟧CompType ⟦ B ι ⟧ValType = ⟦ base ι ⟧ ⟦ vUnit ⟧ValType = ⊤ ⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType ⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType ⟦ F τ ⟧CompType = ⟦ τ ⟧ValType ⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧CtxHidCache : (Γ : Context) → Set₁ ⟦_⟧CtxHidCache = DependentList ⟦_⟧TypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -} -- It's questionable that this is not polymorphic enough. ⟦_⟧VarHidCache : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦ this ⟧VarHidCache (v • ρ) = v ⟦ that x ⟧VarHidCache (v • ρ) = ⟦ x ⟧VarHidCache ρ -- Now, let us define a caching semantics for terms. -- This proves to be hard, because we need to insert and remove caches where -- we apply constants. -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us. -- -- Inserting and removing caches -- -- -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time. -- Indeed, both functions seem structurally recursive on τ. dropCache : ∀ {τ} → ⟦ τ ⟧TypeHidCache → ⟦ τ ⟧ extend : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧TypeHidCache dropCache {base ι} v = lower v dropCache {σ ⇒ τ} v x = dropCache (proj₁ (proj₂ (v (extend x)))) extend {base ι} v = lift v extend {σ ⇒ τ} v = λ x → , (extend (v (dropCache x)) , tt) -- OK, this version is syntax-directed, luckily enough, except on primitives -- (as expected). This reveals a bug of ours on higher-order primitives. -- -- Moreover, we can somewhat safely assume that each call to extend and to -- dropCache is bad: then we see that the handling of constants is bad. That's -- correct, because constants will not return intermediate results in this -- schema :-(. ⟦_⟧TermCache2 : ∀ {τ Γ} → Term Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦_⟧TermCache2 (const c args) ρ = extend (⟦ const c args ⟧ (map dropCache ρ)) ⟦_⟧TermCache2 (var x) ρ = ⟦ x ⟧VarHidCache ρ -- It seems odd (a probable bug?) that the result of t needn't be stripped of -- its cache. ⟦_⟧TermCache2 (app s t) ρ = proj₁ (proj₂ ((⟦ s ⟧TermCache2 ρ) (⟦ t ⟧TermCache2 ρ))) -- Provide an empty cache! ⟦_⟧TermCache2 (abs t) ρ x = , (⟦ t ⟧TermCache2 (x • ρ) , tt) -- The solution is to distinguish among different kinds of constants. Some are -- value constructors (and thus do not return caches), while others are -- computation constructors (and thus should return caches). For products, I -- believe we will only use the products which are values, not computations -- (XXX check CBPV paper for the name). ⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public using () renaming (⟦_⟧Var to ⟦_⟧ValVar) -- This says that the environment does not contain caches... sounds wrong! -- Either we add extra variables for the caches, or we store computations in -- the environment (but that does not make sense), or we store caches in -- values, by acting not on F but on something else (U?). -- I suspect the plan was to use extra variables; that's annoying to model in -- Agda but easier in implementations. ⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ ⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ -- The real deal, finally. open import UNDEFINED -- XXX constants are still a slight mess because I'm abusing CBPV... -- (Actually, I just forgot the difference, and believe I had too little clue -- when I wrote these constructors... but some of them did make sense). ⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED -- Also, where are introduction forms for pairs and sums among values? With -- them, we should see that we can interpret them without adding a cache. -- Thunks keep seeming noops. ⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ -- Here, in an actual implementation, we would return the actual cache with -- all variables. -- -- The effect of F is a writer monad of cached values, where the monoid is -- (isomorphic to) the free monoid over (∃ τ . τ), but we push the -- existentials up when pairing things! -- That's what we're interpreting computations in. XXX maybe consider using -- monads directly. But that doesn't deal with arity. ⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt) -- For this to be enough, a lambda needs to return a produce, not to forward -- the underlying one (unless there are no intermediate results). The correct -- requirements can maybe be enforced through a linear typing discipline. {- -- Here we'd have a problem with the original into from CBPV, because it does -- not require converting expressions to the "CBPV A-normal form". ⟦_⟧CompTermCache (v₁ into v₂) ρ = -- Sequence commands and combine their caches. {- let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in , (r₂ , (c₁ ,′ c₂)) -} -- The code above does not work, because we only guarantee that v₂ is -- a computation, not that it's an F-computation - v₂ could also be function -- type or a computation product. -- We need a restricted CBPV, where these two possibilities are forbidden. If -- you want to save something between different lambdas, you need to add an F -- U to reflect that. (Double-check the papers which show how to encode arity -- using CBPV, it seems that they should find the same problem --- XXX they -- don't). -- Instead, just drop the first cache (XXX WRONG). ⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ) -} -- But if we alter _into_ as described above, composing the caches works! -- However, we should not forget we also need to save the new intermediate -- result, that is the one produced by the first part of the let. ⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ = let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂)) -- Note the compositionality and luck: we don't need to do anything at the -- cReturn time, we just need the nested into to do their job, because as I -- said intermediate results are a writer monad. -- -- Q: But then, do we still need to do all the other stuff? IOW, do we still -- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <- -- f args; z <- g args'; z)? -- -- A: One thing we still need is using the monadic version of into for the -- same reasons - which makes sense, since it has the type of monadic bind. -- -- Maybe not: if we use a monad, which respects left and right identity, the -- two above forms are equivalent. But what about associativity? We don't have -- associativity with nested tuples in the middle. That's why the monad uses -- lists! We can also use nested tuple, as long as in the into case we don't -- do (a, b) but append a b (ahem, where?), which decomposes the first list -- and prepends it to the second. To this end, we need to know the type of the -- first element, or to ensure it's always a pair. Maybe we just want to reuse -- an HList. -- In abstractions, we should start collecting all variables... -- Here, unlike in ⟦_⟧TermCache2, we don't need to invent an empty cache, -- that's moved into the handling of cReturn. This makes *the* difference for -- nested lambdas, where we don't need to create caches multiple times! ⟦_⟧CompTermCache (cAbs v) ρ x = ⟦ v ⟧CompTermCache (x • ρ) -- Here we see that we are in a sort of A-normal form, because the argument is -- a value (not quite ANF though, since values can be thunks - that is, -- computations which haven't been run yet, I guess. Do we have an use for -- that? That allows passing lambdas as arguments directly - which is fine, -- because producing a closure indeed does not have intermediate results!). ⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Caching evaluation ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation module Parametric.Denotation.CachingEvaluation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality module Structure where {- -- Prototype here the type-correctness of a simple non-standard semantics. -- This describes a simplified version of the transformation by Liu and -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language -- terms, we produce host language terms to take advantage of the richer type -- system of the host language (in particular, here we need the unit type, -- product types and *existentials*). -- -- As usual, we'll later switch to a real term transformation. -} open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level -- Type semantics for this scenario. ⟦_⟧TypeHidCache : (τ : Type) → Set₁ ⟦ base ι ⟧TypeHidCache = Lift ⟦ base ι ⟧ ⟦ σ ⇒ τ ⟧TypeHidCache = ⟦ σ ⟧TypeHidCache → (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧TypeHidCache × τ₁ ) open import Parametric.Syntax.MType as MType open MType.Structure Base open import Parametric.Syntax.MTerm as MTerm open MTerm.Structure Const open import Function hiding (const) ⟦_⟧ValType : (τ : ValType) → Set ⟦_⟧CompType : (τ : CompType) → Set ⟦ U c ⟧ValType = ⟦ c ⟧CompType ⟦ B ι ⟧ValType = ⟦ base ι ⟧ ⟦ vUnit ⟧ValType = ⊤ ⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType ⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType ⟦ F τ ⟧CompType = ⟦ τ ⟧ValType ⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧CtxHidCache : (Γ : Context) → Set₁ ⟦_⟧CtxHidCache = DependentList ⟦_⟧TypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -} -- It's questionable that this is not polymorphic enough. ⟦_⟧VarHidCache : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦ this ⟧VarHidCache (v • ρ) = v ⟦ that x ⟧VarHidCache (v • ρ) = ⟦ x ⟧VarHidCache ρ -- Now, let us define a caching semantics for terms. -- This proves to be hard, because we need to insert and remove caches where -- we apply constants. -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us. -- -- Inserting and removing caches -- -- -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time. -- Indeed, both functions seem structurally recursive on τ. dropCache : ∀ {τ} → ⟦ τ ⟧TypeHidCache → ⟦ τ ⟧ extend : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧TypeHidCache dropCache {base ι} v = lower v dropCache {σ ⇒ τ} v x = dropCache (proj₁ (proj₂ (v (extend x)))) extend {base ι} v = lift v extend {σ ⇒ τ} v = λ x → , (extend (v (dropCache x)) , tt) -- OK, this version is syntax-directed, luckily enough, except on primitives -- (as expected). This reveals a bug of ours on higher-order primitives. -- -- Moreover, we can somewhat safely assume that each call to extend and to -- dropCache is bad: then we see that the handling of constants is bad. That's -- correct, because constants will not return intermediate results in this -- schema :-(. ⟦_⟧TermCache2 : ∀ {τ Γ} → Term Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦_⟧TermCache2 (const c args) ρ = extend (⟦ const c args ⟧ (map dropCache ρ)) ⟦_⟧TermCache2 (var x) ρ = ⟦ x ⟧VarHidCache ρ -- It seems odd (a probable bug?) that the result of t needn't be stripped of -- its cache. ⟦_⟧TermCache2 (app s t) ρ = proj₁ (proj₂ ((⟦ s ⟧TermCache2 ρ) (⟦ t ⟧TermCache2 ρ))) -- Provide an empty cache! ⟦_⟧TermCache2 (abs t) ρ x = , (⟦ t ⟧TermCache2 (x • ρ) , tt) -- The solution is to distinguish among different kinds of constants. Some are -- value constructors (and thus do not return caches), while others are -- computation constructors (and thus should return caches). For products, I -- believe we will only use the products which are values, not computations -- (XXX check CBPV paper for the name). ⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public using () renaming (⟦_⟧Var to ⟦_⟧ValVar) -- This says that the environment does not contain caches... sounds wrong! -- Either we add extra variables for the caches, or we store computations in -- the environment (but that does not make sense), or we store caches in -- values, by acting not on F but on something else (U?). -- I suspect the plan was to use extra variables; that's annoying to model in -- Agda but easier in implementations. ⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ ⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ -- The real deal, finally. open import UNDEFINED -- XXX constants are still a slight mess because I'm abusing CBPV... -- (Actually, I just forgot the difference, and believe I had too little clue -- when I wrote these constructors... but some of them did make sense). ⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED -- Also, where are introduction forms for pairs and sums among values? With -- them, we should see that we can interpret them without adding a cache. -- Thunks keep seeming noops. ⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ -- Here, in an actual implementation, we would return the actual cache with -- all variables. -- -- The effect of F is a writer monad of cached values, where the monoid is -- (isomorphic to) the free monoid over (∃ τ . τ), but we push the -- existentials up when pairing things! -- That's what we're interpreting computations in. XXX maybe consider using -- monads directly. But that doesn't deal with arity. ⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt) -- For this to be enough, a lambda needs to return a produce, not to forward -- the underlying one (unless there are no intermediate results). The correct -- requirements can maybe be enforced through a linear typing discipline. {- -- Here we'd have a problem with the original into from CBPV, because it does -- not require converting expressions to the "CBPV A-normal form". ⟦_⟧CompTermCache (v₁ into v₂) ρ = -- Sequence commands and combine their caches. {- let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in , (r₂ , (c₁ ,′ c₂)) -} -- The code above does not work, because we only guarantee that v₂ is -- a computation, not that it's an F-computation - v₂ could also be function -- type or a computation product. -- We need a restricted CBPV, where these two possibilities are forbidden. If -- you want to save something between different lambdas, you need to add an F -- U to reflect that. (Double-check the papers which show how to encode arity -- using CBPV, it seems that they should find the same problem --- XXX they -- don't). -- Instead, just drop the first cache (XXX WRONG). ⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ) -} -- But if we alter _into_ as described above, composing the caches works! -- However, we should not forget we also need to save the new intermediate -- result, that is the one produced by the first part of the let. ⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ = let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂)) -- Note the compositionality and luck: we don't need to do anything at the -- cReturn time, we just need the nested into to do their job, because as I -- said intermediate results are a writer monad. -- -- Q: But then, do we still need to do all the other stuff? IOW, do we still -- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <- -- f args; z <- g args'; z)? -- -- A: One thing we still need is using the monadic version of into for the -- same reasons - which makes sense, since it has the type of monadic bind. -- -- Maybe not: if we use a monad, which respects left and right identity, the -- two above forms are equivalent. But what about associativity? We don't have -- associativity with nested tuples in the middle. That's why the monad uses -- lists! We can also use nested tuple, as long as in the into case we don't -- do (a, b) but append a b (ahem, where?), which decomposes the first list -- and prepends it to the second. To this end, we need to know the type of the -- first element, or to ensure it's always a pair. Maybe we just want to reuse -- an HList. -- In abstractions, we should start collecting all variables... -- Here, unlike in ⟦_⟧TermCache2, 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 ρ)
Clarify comment
Clarify comment
Agda
mit
inc-lc/ilc-agda
07cae51cc16edefb7e8225f94559a896323c1dd2
Popl14/Syntax/Term.agda
Popl14/Syntax/Term.agda
module Popl14.Syntax.Term where open import Popl14.Syntax.Type open import Data.Integer import Parametric.Syntax.Term Base as Term -- Popl14-Const ? No. data Const : Term.Structure where intlit-const : (n : ℤ) → Const ∅ int add-const : Const (int • int • ∅) int minus-const : Const (int • ∅) (int) empty-const : Const ∅ (bag) insert-const : Const (int • bag • ∅) (bag) union-const : Const (bag • bag • ∅) (bag) negate-const : Const (bag • ∅) (bag) flatmap-const : Const ((int ⇒ bag) • bag • ∅) (bag) sum-const : Const (bag • ∅) (int) open Term.Structure Const public -- Shorthands of constants pattern intlit n = const (intlit-const n) ∅ pattern add s t = const add-const (s • t • ∅) pattern minus t = const minus-const (t • ∅) pattern empty = const empty-const ∅ pattern insert s t = const insert-const (s • t • ∅) pattern union s t = const union-const (s • t • ∅) pattern negate t = const negate-const (t • ∅) pattern flatmap s t = const flatmap-const (s • t • ∅) pattern sum t = const sum-const (t • ∅)
module Popl14.Syntax.Term where open import Popl14.Syntax.Type open import Data.Integer import Parametric.Syntax.Term Base as Term data Const : Term.Structure where intlit-const : (n : ℤ) → Const ∅ int add-const : Const (int • int • ∅) int minus-const : Const (int • ∅) (int) empty-const : Const ∅ (bag) insert-const : Const (int • bag • ∅) (bag) union-const : Const (bag • bag • ∅) (bag) negate-const : Const (bag • ∅) (bag) flatmap-const : Const ((int ⇒ bag) • bag • ∅) (bag) sum-const : Const (bag • ∅) (int) open Term.Structure Const public -- Shorthands of constants pattern intlit n = const (intlit-const n) ∅ pattern add s t = const add-const (s • t • ∅) pattern minus t = const minus-const (t • ∅) pattern empty = const empty-const ∅ pattern insert s t = const insert-const (s • t • ∅) pattern union s t = const union-const (s • t • ∅) pattern negate t = const negate-const (t • ∅) pattern flatmap s t = const flatmap-const (s • t • ∅) pattern sum t = const sum-const (t • ∅)
Remove comment.
Remove comment. Old-commit-hash: aabb08e2144e33b92062ac6a93d3722869d7ba2f
Agda
mit
inc-lc/ilc-agda
ce0860eb54642cc4fd246103f18313790c9d29f4
lib/Data/Vec/NP.agda
lib/Data/Vec/NP.agda
module Data.Vec.NP where open import Data.Vec public open import Data.Nat using (ℕ; suc; zero; _+_) open import Data.Fin hiding (_+_) open import Data.Fin.Props open import Data.Bool open import Data.Product open import Function import Relation.Binary.PropositionalEquality.NP as ≡ open ≡ count : ∀ {n a} {A : Set a} → (A → Bool) → Vec A n → Fin (suc n) count pred [] = zero count pred (x ∷ xs) = (if pred x then suc else inject₁) (count pred xs) filter : ∀ {n a} {A : Set a} (pred : A → Bool) (xs : Vec A n) → Vec A (toℕ (count pred xs)) filter pred [] = [] filter pred (x ∷ xs) with pred x ... | true = x ∷ filter pred xs ... | false rewrite inject₁-lemma (count pred xs) = filter pred xs η : ∀ {n a} {A : Set a} → Vec A n → Vec A n η = tabulate ∘ flip lookup η′ : ∀ {n a} {A : Set a} → Vec A n → Vec A n η′ {zero} = λ _ → [] η′ {suc n} = λ xs → head xs ∷ η (tail xs) ≡-splitAt : ∀ {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n} → (xs ++ ys) ≡ (zs ++ ts) → (xs ≡ zs × ys ≡ ts) ≡-splitAt {zero} {xs = []} {[]} p = refl , p ≡-splitAt {suc m} {xs = x ∷ xs} {z ∷ zs} eq with ≡-splitAt {m} {xs = xs} {zs} (cong tail eq) ... | (q₁ , q₂) = (cong₂ _∷_ (cong head eq) q₁) , q₂ {- open import Data.Vec.Equality module Here {a} {A : Set a} where open Equality (≡.setoid A) ≈-splitAt : ∀ {m n} {xs zs : Vec A m} {ys ts : Vec A n} → (xs ++ ys) ≈ (zs ++ ts) → (xs ≈ zs × ys ≈ ts) ≈-splitAt {zero} {xs = []} {[]} p = []-cong , p ≈-splitAt {suc m} {xs = x ∷ xs} {z ∷ zs} (x¹≈x² ∷-cong p) with ≈-splitAt {m} {xs = xs} {zs} p ... | (q₁ , q₂) = x¹≈x² ∷-cong q₁ , q₂ -} ++-inj₁ : ∀ {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} → xs ++ zs ≡ ys ++ ts → xs ≡ ys ++-inj₁ xs ys eq = proj₁ (≡-splitAt eq) ++-inj₂ : ∀ {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} → xs ++ zs ≡ ys ++ ts → zs ≡ ts ++-inj₂ xs ys eq = proj₂ (≡-splitAt {xs = xs} {ys} eq) take-++ : ∀ m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) → take m (xs ++ ys) ≡ xs take-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys ... | zs | eq with splitAt m zs take-++ m xs₁ ys₁ | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj₁ xs₁ xs eq) drop-++ : ∀ m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) → drop m (xs ++ ys) ≡ ys drop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys ... | zs | eq with splitAt m zs drop-++ m xs₁ ys₁ | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj₂ xs₁ xs eq) take-drop-lem : ∀ m {n} {a} {A : Set a} (xs : Vec A (m + n)) → take m xs ++ drop m xs ≡ xs take-drop-lem m xs with splitAt m xs take-drop-lem m .(ys ++ zs) | ys , zs , refl = refl take-them-all : ∀ n {a} {A : Set a} (xs : Vec A (n + 0)) → take n xs ++ [] ≡ xs take-them-all n xs with splitAt n xs take-them-all n .(ys ++ []) | ys , [] , refl = refl
module Data.Vec.NP where open import Data.Vec public open import Data.Nat using (ℕ; suc; zero; _+_) open import Data.Fin hiding (_+_) open import Data.Fin.Props open import Data.Bool open import Data.Product open import Function import Relation.Binary.PropositionalEquality.NP as ≡ open ≡ count : ∀ {n a} {A : Set a} → (A → Bool) → Vec A n → Fin (suc n) count pred [] = zero count pred (x ∷ xs) = (if pred x then suc else inject₁) (count pred xs) filter : ∀ {n a} {A : Set a} (pred : A → Bool) (xs : Vec A n) → Vec A (toℕ (count pred xs)) filter pred [] = [] filter pred (x ∷ xs) with pred x ... | true = x ∷ filter pred xs ... | false rewrite inject₁-lemma (count pred xs) = filter pred xs η : ∀ {n a} {A : Set a} → Vec A n → Vec A n η = tabulate ∘ flip lookup η′ : ∀ {n a} {A : Set a} → Vec A n → Vec A n η′ {zero} = λ _ → [] η′ {suc n} = λ xs → head xs ∷ η (tail xs) ++-decomp : ∀ {m n a} {A : Set a} {xs zs : Vec A m} {ys ts : Vec A n} → (xs ++ ys) ≡ (zs ++ ts) → (xs ≡ zs × ys ≡ ts) ++-decomp {zero} {xs = []} {[]} p = refl , p ++-decomp {suc m} {xs = x ∷ xs} {z ∷ zs} eq with ++-decomp {m} {xs = xs} {zs} (cong tail eq) ... | (q₁ , q₂) = (cong₂ _∷_ (cong head eq) q₁) , q₂ {- open import Data.Vec.Equality module Here {a} {A : Set a} where open Equality (≡.setoid A) ≈-splitAt : ∀ {m n} {xs zs : Vec A m} {ys ts : Vec A n} → (xs ++ ys) ≈ (zs ++ ts) → (xs ≈ zs × ys ≈ ts) ≈-splitAt {zero} {xs = []} {[]} p = []-cong , p ≈-splitAt {suc m} {xs = x ∷ xs} {z ∷ zs} (x¹≈x² ∷-cong p) with ≈-splitAt {m} {xs = xs} {zs} p ... | (q₁ , q₂) = x¹≈x² ∷-cong q₁ , q₂ -} ++-inj₁ : ∀ {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} → xs ++ zs ≡ ys ++ ts → xs ≡ ys ++-inj₁ xs ys eq = proj₁ (++-decomp eq) ++-inj₂ : ∀ {m n} {a} {A : Set a} (xs ys : Vec A m) {zs ts : Vec A n} → xs ++ zs ≡ ys ++ ts → zs ≡ ts ++-inj₂ xs ys eq = proj₂ (++-decomp {xs = xs} {ys} eq) take-++ : ∀ m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) → take m (xs ++ ys) ≡ xs take-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys ... | zs | eq with splitAt m zs take-++ m xs₁ ys₁ | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj₁ xs₁ xs eq) drop-++ : ∀ m {n} {a} {A : Set a} (xs : Vec A m) (ys : Vec A n) → drop m (xs ++ ys) ≡ ys drop-++ m xs ys with xs ++ ys | inspect (_++_ xs) ys ... | zs | eq with splitAt m zs drop-++ m xs₁ ys₁ | .(xs ++ ys) | Reveal_is_.[_] eq | xs , ys , refl = sym (++-inj₂ xs₁ xs eq) take-drop-lem : ∀ m {n} {a} {A : Set a} (xs : Vec A (m + n)) → take m xs ++ drop m xs ≡ xs take-drop-lem m xs with splitAt m xs take-drop-lem m .(ys ++ zs) | ys , zs , refl = refl take-them-all : ∀ n {a} {A : Set a} (xs : Vec A (n + 0)) → take n xs ++ [] ≡ xs take-them-all n xs with splitAt n xs take-them-all n .(ys ++ []) | ys , [] , refl = refl
rename ≡-splitAt into ++-decomp
rename ≡-splitAt into ++-decomp
Agda
bsd-3-clause
crypto-agda/agda-nplib
9a173b770f197c6b30d41ce679f7a2b1864026a1
ZK/JSChecker.agda
ZK/JSChecker.agda
{-# OPTIONS --without-K #-} module ZK.JSChecker where open import Type.Eq open import Function using (case_of_) open import Data.Two.Base using (_∧_) open import Data.List.Base using ([]; _∷_) 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.FiniteField as Zq import Crypto.JS.BigI.CyclicGroup as Zp open import Crypto.JS.BigI.Params using (Params; module Params) open import Crypto.JS.BigI.Checks using (check-params!) -- 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" -- PoK: (Zero-Knowledge) Proof of Knowledge -- CP: Chaum-Pedersen -- EG: ElGamal -- rnd: Knowledge of the secret, random exponent used in ElGamal encryption record PoK-CP-EG-rnd (ℤq ℤp★ : Set) : Set where inductive -- NO_ETA field g y α β A B : ℤp★ m c s : ℤq verify-PoK-CP-EG-rnd : (p q : BigI) (pok : PoK-CP-EG-rnd Zq.ℤ[ q ] Zp.ℤ[ p ]★) → Bool verify-PoK-CP-EG-rnd p q pok = gˢ==αᶜ·A ∧ yˢ==⟨β/M⟩ᶜ·B module verify-PoK-CP-EG-rnd where open module ℤq = Zq q open module ℤp★ = Zp p open module ^-h = ^-hom {q} open module pok = PoK-CP-EG-rnd pok M = g ^ m gˢ==αᶜ·A = g ^ s == (α ^ c) ** A yˢ==⟨β/M⟩ᶜ·B = y ^ s == ((β // M) ^ c) ** B zk-check! : JSValue → JS! zk-check! arg = check! "type of statement" (typ === fromString cpt) (λ _ → "Expected type of statement: " ++ cpt ++ " not " ++ toString typ) >> BigI▹ℤp★ gI >>= λ g → BigI▹ℤp★ (bigdec (dat ·« "y" »)) >>= λ y → BigI▹ℤp★ (bigdec (enc ·« "alpha" »)) >>= λ α → BigI▹ℤp★ (bigdec (enc ·« "beta" »)) >>= λ β → BigI▹ℤp★ (bigdec (com ·« "A" »)) >>= λ A → BigI▹ℤp★ (bigdec (com ·« "B" »)) >>= λ B → BigI▹ℤq (bigdec (prf ·« "challenge" »)) >>= λ c → BigI▹ℤq (bigdec (prf ·« "response" »)) >>= λ s → BigI▹ℤq (bigdec (dat ·« "plain" »)) >>= λ m → check-params! gpq >> -- Console.log ("pok=" ++ JSON-stringify (fromAny pok)) >> check! "PoK-CP-EG-rnd" (verify-PoK-CP-EG-rnd pI qI (record { g = g ; y = y ; α = α ; β = β ; A = A ; B = B ; c = c ; s = s ; m = m })) (λ _ → "Invalid transcript") module zk-check where cpt = "chaum-pedersen-pok-elgamal-rnd" stm = arg ·« "statement" » typ = stm ·« "type" » dat = stm ·« "data" » enc = dat ·« "enc" » prf = arg ·« "proof" » com = prf ·« "commitment" » gI = bigdec (dat ·« "g" ») pI = bigdec (dat ·« "p" ») qI = bigdec (dat ·« "q" ») gpq = record { primality-test-probability-bound = primality-test-probability-bound ; min-bits-q = min-bits-q ; min-bits-p = min-bits-p ; qI = qI ; pI = pI ; gI = gI } open module ℤq = Zq qI using (BigI▹ℤq) open module ℤp★ = Zp pI using (BigI▹ℤp★) -- open module [ℤq]ℤp★ = ZqZp gpq {- 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 Type.Eq open import Function using (case_of_) open import Data.Two.Base using (_∧_) open import Data.List.Base using ([]; _∷_) 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.FiniteField as Zq import Crypto.JS.BigI.CyclicGroup as Zp open import Crypto.JS.BigI.Params using (Params; module Params) open import Crypto.JS.BigI.Checks using (check-params!) open import ZK.Types verify-PoK-CP-EG-rnd : (p q : BigI) (pok : PoK-CP-EG-rnd Zq.ℤ[ q ] Zp.ℤ[ p ]★) → Bool verify-PoK-CP-EG-rnd p q pok = gˢ==αᶜ·A ∧ yˢ==⟨β/M⟩ᶜ·B module verify-PoK-CP-EG-rnd where open module ℤq = Zq q open module ℤp★ = Zp p open module ^-h = ^-hom {q} open module pok = PoK-CP-EG-rnd pok M = g ^ m gˢ==αᶜ·A = g ^ s == (α ^ c) ** A yˢ==⟨β/M⟩ᶜ·B = y ^ s == ((β // M) ^ c) ** B -- 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 → JS[ BigI ] bigdec v = bigI <$> castString v <*> return "10" zk-check! : JSValue → JS! zk-check! arg = arg ·« "statement" » >>= λ stm → stm ·« "type" » >>= λ typ → let cpt = "chaum-pedersen-pok-elgamal-rnd" in check! "type of statement" (typ === fromString cpt) (λ _ → "Expected type of statement: " ++ cpt ++ " not " ++ toString typ) >> stm ·« "data" » >>= λ dat → dat ·« "enc" » >>= λ enc → arg ·« "proof" » >>= λ prf → prf ·« "commitment" » >>= λ com → (bigdec =<< dat ·« "g" ») >>= λ gI → (bigdec =<< dat ·« "p" ») >>= λ pI → (bigdec =<< dat ·« "q" ») >>= λ qI → let open module ℤq = Zq qI using (BigI▹ℤq) open module ℤp★ = Zp pI using (BigI▹ℤp★) gpq = record { primality-test-probability-bound = primality-test-probability-bound ; min-bits-q = min-bits-q ; min-bits-p = min-bits-p ; qI = qI ; pI = pI ; gI = gI } in BigI▹ℤp★ gI >>= λ g → (BigI▹ℤp★ =<< bigdec =<< dat ·« "y" ») >>= λ y → (BigI▹ℤp★ =<< bigdec =<< enc ·« "alpha" ») >>= λ α → (BigI▹ℤp★ =<< bigdec =<< enc ·« "beta" ») >>= λ β → (BigI▹ℤp★ =<< bigdec =<< com ·« "A" ») >>= λ A → (BigI▹ℤp★ =<< bigdec =<< com ·« "B" ») >>= λ B → (BigI▹ℤq =<< bigdec =<< prf ·« "challenge" ») >>= λ c → (BigI▹ℤq =<< bigdec =<< prf ·« "response" ») >>= λ s → (BigI▹ℤq =<< bigdec =<< dat ·« "plain" ») >>= λ m → check-params! gpq >> -- Console.log ("pok=" ++ JSON-stringify (fromAny pok)) >> check! "PoK-CP-EG-rnd" (verify-PoK-CP-EG-rnd pI qI (record { g = g ; y = y ; α = α ; β = β ; A = A ; B = B ; c = c ; s = s ; m = m })) (λ _ → "Invalid transcript") {- 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▹List 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" } -- -} -- -} -- -} -- -}
Update ZK.JSChecker to monadic JS operations
Update ZK.JSChecker to monadic JS operations
Agda
bsd-3-clause
crypto-agda/crypto-agda
13ef6fc79c73003cf82b179fbf73127b0e3772b8
Parametric/Denotation/CachingEvaluation.agda
Parametric/Denotation/CachingEvaluation.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Caching evaluation ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation module Parametric.Denotation.CachingEvaluation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality module Structure where {- -- Prototype here the type-correctness of a simple non-standard semantics. -- This describes a simplified version of the transformation by Liu and -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language -- terms, we produce host language terms to take advantage of the richer type -- system of the host language (in particular, here we need the unit type, -- product types and *existentials*). -- -- As usual, we'll later switch to a real term transformation. -} open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level -- Type semantics for this scenario. ⟦_⟧TypeHidCache : (τ : Type) → Set₁ ⟦ base ι ⟧TypeHidCache = Lift ⟦ base ι ⟧ ⟦ σ ⇒ τ ⟧TypeHidCache = ⟦ σ ⟧TypeHidCache → (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧TypeHidCache × τ₁ ) -- Now, let us define a caching semantics for terms. -- This proves to be hard, because we need to insert and remove caches where -- we apply constants. -- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us. -- -- Inserting and removing caches -- -- -- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time. -- Indeed, both functions seem structurally recursive on τ. dropCache : ∀ {τ} → ⟦ τ ⟧TypeHidCache → ⟦ τ ⟧ extend : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧TypeHidCache dropCache {base ι} v = lower v dropCache {σ ⇒ τ} v x = dropCache (proj₁ (proj₂ (v (extend x)))) extend {base ι} v = lift v extend {σ ⇒ τ} v = λ x → , (extend (v (dropCache x)) , tt) ⟦_⟧CtxHidCache : (Γ : Context) → Set₁ ⟦_⟧CtxHidCache = DependentList ⟦_⟧TypeHidCache -- It's questionable that this is not polymorphic enough. ⟦_⟧VarHidCache : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦ this ⟧VarHidCache (v • ρ) = v ⟦ that x ⟧VarHidCache (v • ρ) = ⟦ x ⟧VarHidCache ρ -- OK, this version is syntax-directed, luckily enough, except on primitives -- (as expected). This reveals a bug of ours on higher-order primitives. -- -- Moreover, we can somewhat safely assume that each call to extend and to -- dropCache is bad: then we see that the handling of constants is bad. That's -- correct, because constants will not return intermediate results in this -- schema :-(. ⟦_⟧TermCache : ∀ {τ Γ} → Term Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache ⟦_⟧TermCache (const c args) ρ = extend (⟦ const c args ⟧ (map dropCache ρ)) ⟦_⟧TermCache (var x) ρ = ⟦ x ⟧VarHidCache ρ -- It seems odd (a probable bug?) that the result of t needn't be stripped of -- its cache. ⟦_⟧TermCache (app s t) ρ = proj₁ (proj₂ ((⟦ s ⟧TermCache ρ) (⟦ t ⟧TermCache ρ))) -- Provide an empty cache! ⟦_⟧TermCache (abs t) ρ x = , (⟦ t ⟧TermCache (x • ρ) , tt) open import Parametric.Syntax.MType as MType open MType.Structure Base open import Parametric.Syntax.MTerm as MTerm open MTerm.Structure Const open import Function hiding (const) ⟦_⟧ValType : (τ : ValType) → Set ⟦_⟧CompType : (τ : CompType) → Set ⟦ U c ⟧ValType = ⟦ c ⟧CompType ⟦ B ι ⟧ValType = ⟦ base ι ⟧ ⟦ vUnit ⟧ValType = ⊤ ⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType ⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType ⟦ F τ ⟧CompType = ⟦ τ ⟧ValType ⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -} -- The solution is to distinguish among different kinds of constants. Some are -- value constructors (and thus do not return caches), while others are -- computation constructors (and thus should return caches). For products, I -- believe we will only use the products which are values, not computations -- (XXX check CBPV paper for the name). ⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public using () renaming (⟦_⟧Var to ⟦_⟧ValVar) -- This says that the environment does not contain caches... sounds wrong! -- Either we add extra variables for the caches, or we store computations in -- the environment (but that does not make sense), or we store caches in -- values, by acting not on F but on something else (U?). -- I suspect the plan was to use extra variables; that's annoying to model in -- Agda but easier in implementations. ⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ ⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ -- The real deal, finally. open import UNDEFINED -- XXX constants are still a slight mess because I'm abusing CBPV... -- (Actually, I just forgot the difference, and believe I had too little clue -- when I wrote these constructors... but some of them did make sense). ⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED -- Also, where are introduction forms for pairs and sums among values? With -- them, we should see that we can interpret them without adding a cache. -- Thunks keep seeming noops. ⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ -- Here, in an actual implementation, we would return the actual cache with -- all variables. -- -- The effect of F is a writer monad of cached values, where the monoid is -- (isomorphic to) the free monoid over (∃ τ . τ), but we push the -- existentials up when pairing things! -- That's what we're interpreting computations in. XXX maybe consider using -- monads directly. But that doesn't deal with arity. ⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt) -- For this to be enough, a lambda needs to return a produce, not to forward -- the underlying one (unless there are no intermediate results). The correct -- requirements can maybe be enforced through a linear typing discipline. {- -- Here we'd have a problem with the original into from CBPV, because it does -- not require converting expressions to the "CBPV A-normal form". ⟦_⟧CompTermCache (v₁ into v₂) ρ = -- Sequence commands and combine their caches. {- let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in , (r₂ , (c₁ ,′ c₂)) -} -- The code above does not work, because we only guarantee that v₂ is -- a computation, not that it's an F-computation - v₂ could also be function -- type or a computation product. -- We need a restricted CBPV, where these two possibilities are forbidden. If -- you want to save something between different lambdas, you need to add an F -- U to reflect that. (Double-check the papers which show how to encode arity -- using CBPV, it seems that they should find the same problem --- XXX they -- don't). -- Instead, just drop the first cache (XXX WRONG). ⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ) -} -- But if we alter _into_ as described above, composing the caches works! -- However, we should not forget we also need to save the new intermediate -- result, that is the one produced by the first part of the let. ⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ = let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂)) -- Note the compositionality and luck: we don't need to do anything at the -- cReturn time, we just need the nested into to do their job, because as I -- said intermediate results are a writer monad. -- -- Q: But then, do we still need to do all the other stuff? IOW, do we still -- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <- -- f args; z <- g args'; z)? -- -- A: One thing we still need is using the monadic version of into for the -- same reasons - which makes sense, since it has the type of monadic bind. -- -- Maybe not: if we use a monad, which respects left and right identity, the -- two above forms are equivalent. But what about associativity? We don't have -- associativity with nested tuples in the middle. That's why the monad uses -- lists! We can also use nested tuple, as long as in the into case we don't -- do (a, b) but append a b (ahem, where?), which decomposes the first list -- and prepends it to the second. To this end, we need to know the type of the -- first element, or to ensure it's always a pair. Maybe we just want to reuse -- an HList. -- In abstractions, we should start collecting all variables... -- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache, -- that's moved into the handling of cReturn. This makes *the* difference for -- nested lambdas, where we don't need to create caches multiple times! ⟦_⟧CompTermCache (cAbs v) ρ x = ⟦ v ⟧CompTermCache (x • ρ) -- Here we see that we are in a sort of A-normal form, because the argument is -- a value (not quite ANF though, since values can be thunks - that is, -- computations which haven't been run yet, I guess. Do we have an use for -- that? That allows passing lambdas as arguments directly - which is fine, -- because producing a closure indeed does not have intermediate results!). ⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Caching evaluation ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation module Parametric.Denotation.CachingEvaluation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality module Structure where {- -- Prototype here the type-correctness of a simple non-standard semantics. -- This describes a simplified version of the transformation by Liu and -- Tanenbaum, PEPM 1995 - but for now, instead of producing object language -- terms, we produce host language terms to take advantage of the richer type -- system of the host language (in particular, here we need the unit type, -- product types and *existentials*). -- -- As usual, we'll later switch to a real term transformation. -} open import Data.Product hiding (map) open import Data.Sum hiding (map) open import Data.Unit open import Level -- 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. open import Parametric.Syntax.MType as MType open MType.Structure Base open import Parametric.Syntax.MTerm as MTerm open MTerm.Structure Const open import Function hiding (const) ⟦_⟧ValType : (τ : ValType) → Set ⟦_⟧CompType : (τ : CompType) → Set ⟦ U c ⟧ValType = ⟦ c ⟧CompType ⟦ B ι ⟧ValType = ⟦ base ι ⟧ ⟦ vUnit ⟧ValType = ⊤ ⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType ⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType ⟦ F τ ⟧CompType = ⟦ τ ⟧ValType ⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType -- XXX: below we need to override just a few cases. Inheritance would handle -- this precisely; without inheritance, we might want to use one of the -- standard encodings of related features (delegation?). ⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁ ⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁ -- This line is the only change, up to now, for the caching semantics starting from CBPV. ⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ ) -- Delegation upward. ⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType ⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType -- The above does not override what happens in recursive occurrences. {-# NO_TERMINATION_CHECK #-} ⟦_⟧ValTypeHidCache : (τ : ValType) → Set ⟦_⟧CompTypeHidCache : (τ : CompType) → Set ⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache ⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧ ⟦ vUnit ⟧ValTypeHidCache = ⊤ ⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache ⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache -- This line is the only change, up to now, for the caching semantics. -- -- XXX The termination checker isn't happy with it, and it may be right ─ if -- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big. -- I think we don't do that unless we are caching a non-terminating -- computation to begin with, but I'm not entirely sure. -- -- However, the termination checker can't prove that the function is -- terminating because it's not structurally recursive, but one call of the -- function will produce another call of the function stuck on a neutral term: -- So the computation will have terminated, just in an unusual way! -- -- Anyway, I need not mechanize this part of the proof for my goals. ⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache ) ⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set ⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache {- ⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁ ⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache -} -- The solution is to distinguish among different kinds of constants. Some are -- value constructors (and thus do not return caches), while others are -- computation constructors (and thus should return caches). For products, I -- believe we will only use the products which are values, not computations -- (XXX check CBPV paper for the name). ⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache ⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public using () renaming (⟦_⟧Var to ⟦_⟧ValVar) -- This says that the environment does not contain caches... sounds wrong! -- Either we add extra variables for the caches, or we store computations in -- the environment (but that does not make sense), or we store caches in -- values, by acting not on F but on something else (U?). -- I suspect the plan was to use extra variables; that's annoying to model in -- Agda but easier in implementations. ⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ ⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ -- The real deal, finally. open import UNDEFINED -- XXX constants are still a slight mess because I'm abusing CBPV... -- (Actually, I just forgot the difference, and believe I had too little clue -- when I wrote these constructors... but some of them did make sense). ⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED ⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED -- Also, where are introduction forms for pairs and sums among values? With -- them, we should see that we can interpret them without adding a cache. -- Thunks keep seeming noops. ⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ -- Here, in an actual implementation, we would return the actual cache with -- all variables. -- -- The effect of F is a writer monad of cached values, where the monoid is -- (isomorphic to) the free monoid over (∃ τ . τ), but we push the -- existentials up when pairing things! -- That's what we're interpreting computations in. XXX maybe consider using -- monads directly. But that doesn't deal with arity. ⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt) -- For this to be enough, a lambda needs to return a produce, not to forward -- the underlying one (unless there are no intermediate results). The correct -- requirements can maybe be enforced through a linear typing discipline. {- -- Here we'd have a problem with the original into from CBPV, because it does -- not require converting expressions to the "CBPV A-normal form". ⟦_⟧CompTermCache (v₁ into v₂) ρ = -- Sequence commands and combine their caches. {- let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in , (r₂ , (c₁ ,′ c₂)) -} -- The code above does not work, because we only guarantee that v₂ is -- a computation, not that it's an F-computation - v₂ could also be function -- type or a computation product. -- We need a restricted CBPV, where these two possibilities are forbidden. If -- you want to save something between different lambdas, you need to add an F -- U to reflect that. (Double-check the papers which show how to encode arity -- using CBPV, it seems that they should find the same problem --- XXX they -- don't). -- Instead, just drop the first cache (XXX WRONG). ⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ) -} -- But if we alter _into_ as described above, composing the caches works! -- However, we should not forget we also need to save the new intermediate -- result, that is the one produced by the first part of the let. ⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ = let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ (τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ) in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂)) -- Note the compositionality and luck: we don't need to do anything at the -- cReturn time, we just need the nested into to do their job, because as I -- said intermediate results are a writer monad. -- -- Q: But then, do we still need to do all the other stuff? IOW, do we still -- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <- -- f args; z <- g args'; z)? -- -- A: One thing we still need is using the monadic version of into for the -- same reasons - which makes sense, since it has the type of monadic bind. -- -- Maybe not: if we use a monad, which respects left and right identity, the -- two above forms are equivalent. But what about associativity? We don't have -- associativity with nested tuples in the middle. That's why the monad uses -- lists! We can also use nested tuple, as long as in the into case we don't -- do (a, b) but append a b (ahem, where?), which decomposes the first list -- and prepends it to the second. To this end, we need to know the type of the -- first element, or to ensure it's always a pair. Maybe we just want to reuse -- an HList. -- In abstractions, we should start collecting all variables... -- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache, -- that's moved into the handling of cReturn. This makes *the* difference for -- nested lambdas, where we don't need to create caches multiple times! ⟦_⟧CompTermCache (cAbs v) ρ x = ⟦ v ⟧CompTermCache (x • ρ) -- Here we see that we are in a sort of A-normal form, because the argument is -- a value (not quite ANF though, since values can be thunks - that is, -- computations which haven't been run yet, I guess. Do we have an use for -- that? That allows passing lambdas as arguments directly - which is fine, -- because producing a closure indeed does not have intermediate results!). ⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
Drop non-CBPV caching semantics & update comments
Drop non-CBPV caching semantics & update comments
Agda
mit
inc-lc/ilc-agda
27a4ba88511dda3572cdb1d98a77ac8d6af2e8e1
Base/Change/Context.agda
Base/Change/Context.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change contexts ------------------------------------------------------------------------ module Base.Change.Context {Type : Set} (ΔType : Type → Type) where open import Base.Syntax.Context Type -- Transform a context of values into a context of values and -- changes. ΔContext : Context → Context ΔContext ∅ = ∅ ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ -- like ΔContext, but ΔType τ and τ are swapped ΔContext′ : Context → Context ΔContext′ ∅ = ∅ ΔContext′ (τ • Γ) = τ • ΔType τ • ΔContext′ Γ Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ Γ≼ΔΓ {∅} = ∅ Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change contexts -- -- This module specifies how a context of values is merged -- together with the corresponding context of changes. -- -- In the PLDI paper, instead of merging the contexts together, we just -- concatenate them. For example, in the "typing rule" for Derive -- in Sec. 3.2 of the paper, we write in the conclusion of the rule: -- "Γ, ΔΓ ⊢ Derive(t) : Δτ". Simple concatenation is possible because -- the paper uses named variables and assumes that no user variables -- start with "d". In this formalization, we use de Bruijn indices, so -- it is easier to alternate values and their changes in the context. ------------------------------------------------------------------------ module Base.Change.Context {Type : Set} (ΔType : Type → Type) where open import Base.Syntax.Context Type -- Transform a context of values into a context of values and -- changes. ΔContext : Context → Context ΔContext ∅ = ∅ ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ -- like ΔContext, but ΔType τ and τ are swapped ΔContext′ : Context → Context ΔContext′ ∅ = ∅ ΔContext′ (τ • Γ) = τ • ΔType τ • ΔContext′ Γ -- This sub-context relationship explains how to go back from -- ΔContext Γ to Γ: You have to drop every other binding. Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ Γ≼ΔΓ {∅} = ∅ Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ
Improve commentary on Base.Change.Context.
Improve commentary on Base.Change.Context. Old-commit-hash: ad5f3d68627e46f68376b1c994a9ce3787870847
Agda
mit
inc-lc/ilc-agda
670c3d1576725334e48ff0039610f28d8afc13f2
Parametric/Change/Correctness.agda
Parametric/Change/Correctness.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity import Parametric.Change.Specification as Specification import Parametric.Change.Type as ChangeType import Parametric.Change.Term as ChangeTerm import Parametric.Change.Value as ChangeValue import Parametric.Change.Evaluation as ChangeEvaluation import Parametric.Change.Derive as Derive import Parametric.Change.Implementation as Implementation module Parametric.Change.Correctness {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (diff-base : ChangeTerm.DiffStructure Const ΔBase) (apply-base : ChangeTerm.ApplyStructure Const ΔBase) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) (meaning-⊕-base : ChangeEvaluation.ApplyStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (meaning-⊝-base : ChangeEvaluation.DiffStructure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧) (validity-structure : Validity.Structure ⟦_⟧Base) (specification-structure : Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure) (derive-const : Derive.Structure Const ΔBase) (implementation-structure : Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure open ChangeType.Structure Base ΔBase open ChangeTerm.Structure Const ΔBase diff-base apply-base open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open ChangeEvaluation.Structure ⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧ meaning-⊕-base meaning-⊝-base open Derive.Structure Const ΔBase derive-const open Implementation.Structure Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure ⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure -- The denotational properties of the `derive` transformation. -- In particular, the main theorem about it producing the correct -- incremental behavior. open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality Structure : Set Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → (ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) → ⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′) module Structure (derive-const-correct : Structure) where deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′) deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′ deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′ -- That `derive t` implements ⟦ t ⟧Δ derive-correct : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → ⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′) derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) → implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′)) derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅ derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ = derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′ derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ = derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′ (derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′) derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ = deriveVar-correct x ρ dρ ρ′ dρ≈ρ′ derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′ = subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′) (derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′ (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′)) derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ = λ w dw w′ dw≈w′ → derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′) derive-correct-closed : ∀ {τ} (t : Term ∅ τ) → ⟦ t ⟧Δ ∅ ∅ ≈₍ τ ₎ ⟦ derive t ⟧ ∅ derive-correct-closed t = derive-correct t ∅ ∅ ∅ ∅ main-theorem : ∀ {σ τ} {f : Term ∅ (σ ⇒ τ)} {s : Term ∅ σ} {ds : Term ∅ (ΔType σ)} → {dv : Δ₍ σ ₎ (⟦ s ⟧ ∅)} {erasure : dv ≈₍ σ ₎ (⟦ ds ⟧ ∅)} → ⟦ app f (s ⊕₍ σ ₎ ds) ⟧ ≡ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ main-theorem {σ} {τ} {f} {s} {ds} {dv} {erasure} = let g = ⟦ f ⟧ ∅ Δg = ⟦ f ⟧Δ ∅ ∅ Δg′ = ⟦ derive f ⟧ ∅ v = ⟦ s ⟧ ∅ dv′ = ⟦ ds ⟧ ∅ u = ⟦ s ⊕₍ σ ₎ ds ⟧ ∅ -- Δoutput-term = app (app (derive f) x) (y ⊝ x) in ext {A = ⟦ ∅ ⟧Context} (λ { ∅ → begin g u ≡⟨ cong g (sym (meaning-⊕ {t = s} {Δt = ds})) ⟩ g (v ⟦⊕₍ σ ₎⟧ dv′) ≡⟨ cong g (sym (carry-over {σ} dv erasure)) ⟩ g (v ⊞₍ σ ₎ dv) ≡⟨ corollary-closed {σ} {τ} f v dv ⟩ g v ⊞₍ τ ₎ call-change {σ} {τ} Δg v dv ≡⟨ carry-over {τ} (call-change {σ} {τ} Δg v dv) (derive-correct-closed f v dv dv′ erasure) ⟩ g v ⟦⊕₍ τ ₎⟧ Δg′ v dv′ ≡⟨ meaning-⊕ {t = app f s} {Δt = app (app (derive f) s) ds} ⟩ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ ∅ ∎}) where open ≡-Reasoning
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Correctness of differentiation (Lemma 3.10 and Theorem 3.11). ------------------------------------------------------------------------ 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 -- Extension point: A proof that change evaluation for a fully -- applied primitive is related to the value of incrementalizing -- this primitive, if their arguments are so related. 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ρ′ -- We provide: A variant of Lemma 3.10 for arbitrary contexts. 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 ∅ ∅ ∅ ∅ -- And we proof Theorem 3.11, finally. main-theorem : ∀ {σ τ} {f : Term ∅ (σ ⇒ τ)} {s : Term ∅ σ} {ds : Term ∅ (ΔType σ)} → {dv : Δ₍ σ ₎ (⟦ s ⟧ ∅)} {erasure : dv ≈₍ σ ₎ (⟦ ds ⟧ ∅)} → ⟦ app f (s ⊕₍ σ ₎ ds) ⟧ ≡ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ main-theorem {σ} {τ} {f} {s} {ds} {dv} {erasure} = let g = ⟦ f ⟧ ∅ Δg = ⟦ f ⟧Δ ∅ ∅ Δg′ = ⟦ derive f ⟧ ∅ v = ⟦ s ⟧ ∅ dv′ = ⟦ ds ⟧ ∅ u = ⟦ s ⊕₍ σ ₎ ds ⟧ ∅ -- Δoutput-term = app (app (derive f) x) (y ⊝ x) in ext {A = ⟦ ∅ ⟧Context} (λ { ∅ → begin g u ≡⟨ cong g (sym (meaning-⊕ {t = s} {Δt = ds})) ⟩ g (v ⟦⊕₍ σ ₎⟧ dv′) ≡⟨ cong g (sym (carry-over {σ} dv erasure)) ⟩ g (v ⊞₍ σ ₎ dv) ≡⟨ corollary-closed {σ} {τ} f v dv ⟩ g v ⊞₍ τ ₎ call-change {σ} {τ} Δg v dv ≡⟨ carry-over {τ} (call-change {σ} {τ} Δg v dv) (derive-correct-closed f v dv dv′ erasure) ⟩ g v ⟦⊕₍ τ ₎⟧ Δg′ v dv′ ≡⟨ meaning-⊕ {t = app f s} {Δt = app (app (derive f) s) ds} ⟩ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ ∅ ∎}) where open ≡-Reasoning
Document P.C.Correctness.
Document P.C.Correctness. Old-commit-hash: 688c41e30d805aa8b9ac8494f9ec53ddf0b1eb5a
Agda
mit
inc-lc/ilc-agda
2fbf9654c1cd556317dc4eaee7c81283b276d720
lib/Explore/Function/Fin.agda
lib/Explore/Function/Fin.agda
{-# OPTIONS --without-K #-} open import Type open import Function open import Data.Fin using (Fin) open import Level.NP open import Algebra open import Algebra.FunctionProperties.NP open import Data.Fin using (Fin; zero; suc) open import Data.Nat open import Data.Product open import Relation.Binary.NP open import Explore.Core open import Explore.Properties open import Explore.Explorable open import Explore.Fin module Explore.Function.Fin where postulate Postulate-FinFunˢ-ok : ★ module _ {A : ★}(μA : Explorable A) where eᴬ = explore μA extend : ∀ {n} → A → (Fin n → A) → Fin (suc n) → A extend x g zero = x extend x g (suc i) = g i ¬Fin0 : Fin 0 → A ¬Fin0 () -- There is one function Fin 0 → A (called abs) so this should be fine -- if not there is a version below that forces the domain to be non-empty FinFunᵉ : ∀ n → Explore _ (Fin n → A) FinFunᵉ zero z op f = f ¬Fin0 FinFunᵉ (suc n) z op f = eᴬ z op (λ x → FinFunᵉ n z op (f ∘ extend x)) FinFunⁱ : ∀ n → ExploreInd _ (FinFunᵉ n) FinFunⁱ zero P Pz P∙ Pf = Pf _ FinFunⁱ (suc n) P Pz P∙ Pf = explore-ind μA (λ sa → P (λ z op f → sa z op (λ x → FinFunᵉ n z op (f ∘ extend x)))) Pz P∙ (λ x → FinFunⁱ n (λ sf → P (λ z op f → sf z op (f ∘ extend x))) Pz P∙ (Pf ∘ extend x)) FinFunˢ : ∀ n → Sum (Fin n → A) FinFunˢ n = FinFunᵉ n 0 _+_ postulate FinFunˢ-ok : ∀ {{_ : Postulate-FinFunˢ-ok}} n → AdequateSum (FinFunˢ n) μFinFun : ∀ {{_ : Postulate-FinFunˢ-ok}} {n} → Explorable (Fin n → A) μFinFun = mk _ (FinFunⁱ _) (FinFunˢ-ok _) module BigDistr {{_ : Postulate-FinFunˢ-ok}} {A : ★}(μA : Explorable A) (cm : CommutativeMonoid ₀ ₀) -- we want (open CMon cm) !!! (_◎_ : let open CMon cm in C → C → C) (ε-◎ : let open CMon cm in Zero _≈_ ε _◎_) (distrib : let open CMon cm in _DistributesOver_ _≈_ _◎_ _∙_) (_◎-cong_ : let open CMon cm in _◎_ Preserves₂ _≈_ ⟶ _≈_ ⟶ _≈_) where open CMon cm renaming (sym to ≈-sym) μF→A = μFinFun μA -- Sum over A Σᴬ = explore μA ε _∙_ -- Sum over (Fin(1+I)→A) functions Σ' : ∀ {I} → ((Fin I → A) → C) → C Σ' = explore μF→A ε _∙_ -- Product over Fin(1+I) values Π' = λ I → explore (μFin I) ε _◎_ bigDistr : ∀ I F → Π' I (Σᴬ ∘ F) ≈ Σ' (Π' I ∘ _ˢ_ F) bigDistr zero _ = refl bigDistr (suc I) F = Σᴬ (F zero) ◎ Π' I (Σᴬ ∘ F ∘ suc) ≈⟨ refl ◎-cong bigDistr I (F ∘ suc) ⟩ Σᴬ (F zero) ◎ Σ' (Π' I ∘ _ˢ_ (F ∘ suc)) ≈⟨ ≈-sym (explore-linʳ μA monoid _◎_ (F zero) (Σ' (Π' I ∘ _ˢ_ (F ∘ suc))) (proj₁ ε-◎ _) (proj₂ distrib)) ⟩ Σᴬ (λ j → F zero j ◎ Σ' (Π' I ∘ _ˢ_ (F ∘ suc))) ≈⟨ explore-mon-ext μA monoid (λ j → ≈-sym (explore-linˡ μF→A monoid _◎_ (Π' I ∘ _ˢ_ (F ∘ suc)) (F zero j) (proj₂ ε-◎ _) (proj₁ distrib))) ⟩ (Σᴬ λ j → Σ' λ f → F zero j ◎ Π' I ((F ∘ suc) ˢ f)) ∎ module _ {{_ : Postulate-FinFunˢ-ok}} where FinDist : ∀ {n} → DistFun (μFin n) (λ μX → μFinFun μX) FinDist μB c ◎ distrib ◎-cong f = BigDistr.bigDistr μB c ◎ distrib ◎-cong f _
{-# OPTIONS --without-K #-} open import Type open import Function open import Data.Fin using (Fin) open import Level.NP open import Algebra open import Algebra.FunctionProperties.NP open import Data.Fin using (Fin; zero; suc) open import Data.Nat open import Data.Product open import Relation.Binary.NP open import Explore.Core open import Explore.Properties open import Explore.Explorable import Explore.Fin open Explore.Fin.Regular module Explore.Function.Fin where postulate Postulate-FinFunˢ-ok : ★ module _ {A : ★}(μA : Explorable A) where eᴬ = explore μA extend : ∀ {n} → A → (Fin n → A) → Fin (suc n) → A extend x g zero = x extend x g (suc i) = g i ¬Fin0 : Fin 0 → A ¬Fin0 () -- There is one function Fin 0 → A (called abs) so this should be fine -- if not there is a version below that forces the domain to be non-empty FinFunᵉ : ∀ n → Explore _ (Fin n → A) FinFunᵉ zero z op f = f ¬Fin0 FinFunᵉ (suc n) z op f = eᴬ z op (λ x → FinFunᵉ n z op (f ∘ extend x)) FinFunⁱ : ∀ n → ExploreInd _ (FinFunᵉ n) FinFunⁱ zero P Pz P∙ Pf = Pf _ FinFunⁱ (suc n) P Pz P∙ Pf = explore-ind μA (λ sa → P (λ z op f → sa z op (λ x → FinFunᵉ n z op (f ∘ extend x)))) Pz P∙ (λ x → FinFunⁱ n (λ sf → P (λ z op f → sf z op (f ∘ extend x))) Pz P∙ (Pf ∘ extend x)) FinFunˢ : ∀ n → Sum (Fin n → A) FinFunˢ n = FinFunᵉ n 0 _+_ postulate FinFunˢ-ok : ∀ {{_ : Postulate-FinFunˢ-ok}} n → AdequateSum (FinFunˢ n) μFinFun : ∀ {{_ : Postulate-FinFunˢ-ok}} {n} → Explorable (Fin n → A) μFinFun = mk _ (FinFunⁱ _) (FinFunˢ-ok _) module BigDistr {{_ : Postulate-FinFunˢ-ok}} {A : ★}(μA : Explorable A) (cm : CommutativeMonoid ₀ ₀) -- we want (open CMon cm) !!! (_◎_ : let open CMon cm in C → C → C) (ε-◎ : let open CMon cm in Zero _≈_ ε _◎_) (distrib : let open CMon cm in _DistributesOver_ _≈_ _◎_ _∙_) (_◎-cong_ : let open CMon cm in _◎_ Preserves₂ _≈_ ⟶ _≈_ ⟶ _≈_) where open CMon cm renaming (sym to ≈-sym) μF→A = μFinFun μA -- Sum over A Σᴬ = explore μA ε _∙_ -- Sum over (Fin(1+I)→A) functions Σ' : ∀ {I} → ((Fin I → A) → C) → C Σ' = explore μF→A ε _∙_ -- Product over Fin(1+I) values Π' = λ I → explore (μFin I) ε _◎_ bigDistr : ∀ I F → Π' I (Σᴬ ∘ F) ≈ Σ' (Π' I ∘ _ˢ_ F) bigDistr zero _ = refl bigDistr (suc I) F = Σᴬ (F zero) ◎ Π' I (Σᴬ ∘ F ∘ suc) ≈⟨ refl ◎-cong bigDistr I (F ∘ suc) ⟩ Σᴬ (F zero) ◎ Σ' (Π' I ∘ _ˢ_ (F ∘ suc)) ≈⟨ ≈-sym (explore-linʳ μA monoid _◎_ (F zero) (Σ' (Π' I ∘ _ˢ_ (F ∘ suc))) (proj₁ ε-◎ _) (proj₂ distrib)) ⟩ Σᴬ (λ j → F zero j ◎ Σ' (Π' I ∘ _ˢ_ (F ∘ suc))) ≈⟨ explore-mon-ext μA monoid (λ j → ≈-sym (explore-linˡ μF→A monoid _◎_ (Π' I ∘ _ˢ_ (F ∘ suc)) (F zero j) (proj₂ ε-◎ _) (proj₁ distrib))) ⟩ (Σᴬ λ j → Σ' λ f → F zero j ◎ Π' I ((F ∘ suc) ˢ f)) ∎ module _ {{_ : Postulate-FinFunˢ-ok}} where FinDist : ∀ {n} → DistFun (μFin n) (λ μX → μFinFun μX) FinDist μB c ◎ distrib ◎-cong f = BigDistr.bigDistr μB c ◎ distrib ◎-cong f _
use the Regular version
Fun...Fun: use the Regular version
Agda
bsd-3-clause
crypto-agda/explore
18cdc5c3e0989baa42c6669ff2552c5f95ed8867
Postulate/Bag-Nehemiah.agda
Postulate/Bag-Nehemiah.agda
module Postulate.Bag-Nehemiah where -- Postulates about bags of integers, version Nehemiah -- -- This module postulates bags of integers with negative -- multiplicities as a group under additive union. open import Relation.Binary.PropositionalEquality open import Algebra.Structures open import Data.Integer -- postulate Bag as an abelion group -- [Argue that no inconsistency can be introduced] postulate Bag : Set -- singleton postulate singletonBag : ℤ → Bag -- union postulate _++_ : Bag → Bag → Bag infixr 5 _++_ -- negate = mapMultiplicities (λ z → - z) postulate negateBag : Bag → Bag postulate emptyBag : Bag postulate abelian-bag : IsAbelianGroup _≡_ _++_ emptyBag negateBag -- Naming convention follows Algebra.Morphism -- Homomorphic₁ : morphism preserves negation Homomorphic₁ : {A B : Set} (f : A → B) (negA : A → A) (negB : B → B) → Set Homomorphic₁ {A} {B} f negA negB = ∀ {x} → f (negA x) ≡ negB (f x) -- Homomorphic₂ : morphism preserves binary operation. Homomorphic₂ : {A B : Set} (f : A → B) (_+_ : A → A → A) (_*_ : B → B → B) → Set Homomorphic₂ {A} {B} f _+_ _*_ = ∀ {x y} → f (x + y) ≡ f x * f y -- postulate map, flatmap and sum to be homomorphisms postulate mapBag : (f : ℤ → ℤ) (b : Bag) → Bag postulate flatmapBag : (f : ℤ → Bag) (b : Bag) → Bag postulate sumBag : Bag → ℤ postulate homo-map : ∀ {f} → Homomorphic₂ (mapBag f) _++_ _++_ postulate homo-flatmap : ∀ {f} → Homomorphic₂ (flatmapBag f) _++_ _++_ postulate homo-sum : Homomorphic₂ sumBag _++_ _+_ postulate neg-map : ∀ {f} → Homomorphic₁ (mapBag f) negateBag negateBag postulate neg-flatmap : ∀ {f} → Homomorphic₁ (flatmapBag f) negateBag negateBag
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Bags with negative multiplicities, for Nehemiah. -- -- Instead of implementing bags (with negative multiplicities, -- like in the paper) in Agda, we postulate that a group of such -- bags exist. Note that integer bags with integer multiplicities -- are actually the free group given a singleton operation -- `Integer -> Bag`, so this should be easy to formalize in -- principle. ------------------------------------------------------------------------ module Postulate.Bag-Nehemiah where -- Postulates about bags of integers, version Nehemiah -- -- This module postulates bags of integers with negative -- multiplicities as a group under additive union. open import Relation.Binary.PropositionalEquality open import Algebra.Structures open import Data.Integer -- postulate Bag as an abelion group postulate Bag : Set -- singleton postulate singletonBag : ℤ → Bag -- union postulate _++_ : Bag → Bag → Bag infixr 5 _++_ -- negate = mapMultiplicities (λ z → - z) postulate negateBag : Bag → Bag postulate emptyBag : Bag postulate abelian-bag : IsAbelianGroup _≡_ _++_ emptyBag negateBag -- Naming convention follows Algebra.Morphism -- Homomorphic₁ : morphism preserves negation Homomorphic₁ : {A B : Set} (f : A → B) (negA : A → A) (negB : B → B) → Set Homomorphic₁ {A} {B} f negA negB = ∀ {x} → f (negA x) ≡ negB (f x) -- Homomorphic₂ : morphism preserves binary operation. Homomorphic₂ : {A B : Set} (f : A → B) (_+_ : A → A → A) (_*_ : B → B → B) → Set Homomorphic₂ {A} {B} f _+_ _*_ = ∀ {x y} → f (x + y) ≡ f x * f y -- postulate map, flatmap and sum to be homomorphisms postulate mapBag : (f : ℤ → ℤ) (b : Bag) → Bag postulate flatmapBag : (f : ℤ → Bag) (b : Bag) → Bag postulate sumBag : Bag → ℤ postulate homo-map : ∀ {f} → Homomorphic₂ (mapBag f) _++_ _++_ postulate homo-flatmap : ∀ {f} → Homomorphic₂ (flatmapBag f) _++_ _++_ postulate homo-sum : Homomorphic₂ sumBag _++_ _+_ postulate neg-map : ∀ {f} → Homomorphic₁ (mapBag f) negateBag negateBag postulate neg-flatmap : ∀ {f} → Homomorphic₁ (flatmapBag f) negateBag negateBag
Document Postulate.Bag-Nehemiah (for #275).
Document Postulate.Bag-Nehemiah (for #275). Old-commit-hash: f22f7ab7d307f61d4bb165003c6eb917869d49a5
Agda
mit
inc-lc/ilc-agda
e047957caacbba3c00568183dc07c1230b362d8e
Denotation/Specification/Canon-Popl14.agda
Denotation/Specification/Canon-Popl14.agda
module Denotation.Specification.Canon-Popl14 where -- Denotation-as-specification for Calculus Popl14 -- -- Contents -- - ⟦_⟧Δ : binding-time-shifted derive -- - Validity and correctness lemmas for ⟦_⟧Δ -- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments -- - `corollary-closed`: binding-time-shifted main theorem open import Popl14.Syntax.Term open import Popl14.Denotation.Value open import Popl14.Change.Validity open import Popl14.Denotation.Evaluation public open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Product open import Data.Integer open import Structure.Bag.Popl14 open import Theorem.Groups-Popl14 open import Theorem.CongApp open import Postulate.Extensionality ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (dρ : ΔEnv Γ) → Change τ -- better name is: ⟦_⟧Δ reacts to future arguments validity : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) → valid {τ} (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) -- better name is: ⟦_⟧Δ reacts to free variables correctness : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) → ⟦ t ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ ≡ ⟦ t ⟧ (update dρ) ⟦_⟧ΔVar : ∀ {τ Γ} → Var Γ τ → ΔEnv Γ → Change τ ⟦ this ⟧ΔVar (cons v dv R[v,dv] • dρ) = dv ⟦ that x ⟧ΔVar (cons v dv R[v,dv] • dρ) = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (intlit n) dρ = + 0 ⟦_⟧Δ (add s t) dρ = ⟦ s ⟧Δ dρ + ⟦ t ⟧Δ dρ ⟦_⟧Δ (minus t) dρ = - ⟦ t ⟧Δ dρ ⟦_⟧Δ empty dρ = emptyBag ⟦_⟧Δ (insert s t) dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in (singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b) ⟦_⟧Δ (union s t) dρ = ⟦ s ⟧Δ dρ ++ ⟦ t ⟧Δ dρ ⟦_⟧Δ (negate t) dρ = negateBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (flatmap s t) dρ = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b ⟦_⟧Δ (sum t) dρ = sumBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (var x) dρ = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (app {σ} {τ} s t) dρ = (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) ⟦_⟧Δ (abs t) dρ = λ v → ⟦ t ⟧Δ (v • dρ) validVar : ∀ {τ Γ} (x : Var Γ τ) → (dρ : ΔEnv Γ) → valid {τ} (⟦ x ⟧ (ignore dρ)) (⟦ x ⟧ΔVar dρ) validVar this (cons v Δv R[v,Δv] • _) = R[v,Δv] validVar {τ} (that x) (cons _ _ _ • dρ) = validVar {τ} x dρ validity (intlit n) dρ = tt validity (add s t) dρ = tt validity (minus t) dρ = tt validity (empty) dρ = tt validity (insert s t) dρ = tt validity (union s t) dρ = tt validity (negate t) dρ = tt validity (flatmap s t) dρ = tt validity (sum t) dρ = tt validity {τ} (var x) dρ = validVar {τ} x dρ validity (app s t) dρ = let v = ⟦ t ⟧ (ignore dρ) Δv = ⟦ t ⟧Δ dρ in proj₁ (validity s dρ (cons v Δv (validity t dρ))) validity {σ ⇒ τ} (abs t) dρ = λ v → let v′ = after {σ} v Δv′ = v′ ⊟₍ σ ₎ v′ dρ₁ = v • dρ dρ₂ = nil-valid-change σ v′ • dρ in validity t dρ₁ , (begin ⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂ ≡⟨ correctness t dρ₂ ⟩ ⟦ t ⟧ (update dρ₂) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (update dρ₁) ≡⟨ sym (correctness t dρ₁) ⟩ ⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁ ∎) where open ≡-Reasoning correctVar : ∀ {τ Γ} {x : Var Γ τ} {dρ : ΔEnv Γ} → ⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ) correctVar {x = this } {cons v dv R[v,dv] • dρ} = refl correctVar {x = that y} {cons v dv R[v,dv] • dρ} = correctVar {x = y} {dρ} correctness (intlit n) dρ = right-id-int n correctness (add s t) dρ = trans (mn·pq=mp·nq {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _+_ (correctness s dρ) (correctness t dρ)) correctness (minus t) dρ = trans (-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong -_ (correctness t dρ)) correctness empty dρ = right-id-bag emptyBag correctness (insert s t) dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) n′ = ⟦ s ⟧ (update dρ) b′ = ⟦ t ⟧ (update dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in begin (singletonBag n ++ b) ++ (singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b) ≡⟨ a++[b\\a]=b ⟩ singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb) ≡⟨ cong₂ _++_ (cong singletonBag (correctness s dρ)) (correctness t dρ) ⟩ singletonBag n′ ++ b′ ∎ where open ≡-Reasoning correctness (union s t) dρ = trans (ab·cd=ac·bd {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _++_ (correctness s dρ) (correctness t dρ)) correctness (negate t) dρ = trans (-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong negateBag (correctness t dρ)) correctness (flatmap s t) dρ = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in trans (a++[b\\a]=b {flatmapBag f b} {flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf) (b ⊞₍ base base-bag ₎ Δb)}) (cong₂ flatmapBag (correctness s dρ) (correctness t dρ)) correctness (sum t) dρ = let b = ⟦ t ⟧ (ignore dρ) Δb = ⟦ t ⟧Δ dρ in trans (sym homo-sum) (cong sumBag (correctness t dρ)) correctness {τ} (var x) dρ = correctVar {τ} {x = x} correctness (app {σ} {τ} s t) dρ = let f = ⟦ s ⟧ (ignore dρ) g = ⟦ s ⟧ (update dρ) u = ⟦ t ⟧ (ignore dρ) v = ⟦ t ⟧ (update dρ) Δf = ⟦ s ⟧Δ dρ Δu = ⟦ t ⟧Δ dρ in begin f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ)) ≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s dρ ⟨$⟩ correctness {σ} t dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) dρ = ext (λ v → let dρ′ : ΔEnv (σ • Γ) dρ′ = nil-valid-change σ v • dρ in begin ⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′ ≡⟨ correctness {τ} t dρ′ ⟩ ⟦ t ⟧ (update dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v • update dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} → (⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ) (⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ) ≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ)) ⊞₍ τ ₎ (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) corollary {σ} {τ} s t {dρ} = proj₂ (validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))) corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)} (v : ValidChange σ) → ⟦ t ⟧ ∅ (after {σ} v) ≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v corollary-closed {σ} {τ} {t = t} v = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ in begin f (after {σ} v) ≡⟨ cong (λ hole → hole (after {σ} v)) (sym (correctness {σ ⇒ τ} t ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v) ≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩ f (before {σ} v) ⊞₍ τ ₎ Δf v ∎ where open ≡-Reasoning
module Denotation.Specification.Canon-Popl14 where -- Denotation-as-specification for Calculus Popl14 -- -- Contents -- - ⟦_⟧Δ : binding-time-shifted derive -- - Validity and correctness lemmas for ⟦_⟧Δ -- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments -- - `corollary-closed`: binding-time-shifted main theorem open import Popl14.Syntax.Term open import Popl14.Denotation.Value open import Popl14.Change.Validity open import Popl14.Denotation.Evaluation public open import Relation.Binary.PropositionalEquality open import Data.Unit open import Data.Product open import Data.Integer open import Structure.Bag.Popl14 open import Theorem.Groups-Popl14 open import Theorem.CongApp open import Postulate.Extensionality ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (dρ : ΔEnv Γ) → Change τ -- better name is: ⟦_⟧Δ reacts to future arguments validity : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) → valid {τ} (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) -- better name is: ⟦_⟧Δ reacts to free variables correctness : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) → ⟦ t ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ ≡ ⟦ t ⟧ (update dρ) ⟦_⟧ΔVar : ∀ {τ Γ} → Var Γ τ → ΔEnv Γ → Change τ ⟦ this ⟧ΔVar (cons v dv R[v,dv] • dρ) = dv ⟦ that x ⟧ΔVar (cons v dv R[v,dv] • dρ) = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (intlit n) dρ = + 0 ⟦_⟧Δ (add s t) dρ = ⟦ s ⟧Δ dρ + ⟦ t ⟧Δ dρ ⟦_⟧Δ (minus t) dρ = - ⟦ t ⟧Δ dρ ⟦_⟧Δ empty dρ = emptyBag ⟦_⟧Δ (insert s t) dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in (singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b) ⟦_⟧Δ (union s t) dρ = ⟦ s ⟧Δ dρ ++ ⟦ t ⟧Δ dρ ⟦_⟧Δ (negate t) dρ = negateBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (flatmap s t) dρ = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b ⟦_⟧Δ (sum t) dρ = sumBag (⟦ t ⟧Δ dρ) ⟦_⟧Δ (var x) dρ = ⟦ x ⟧ΔVar dρ ⟦_⟧Δ (app {σ} {τ} s t) dρ = (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) ⟦_⟧Δ (abs t) dρ = λ v → ⟦ t ⟧Δ (v • dρ) validVar : ∀ {τ Γ} (x : Var Γ τ) → (dρ : ΔEnv Γ) → valid {τ} (⟦ x ⟧ (ignore dρ)) (⟦ x ⟧ΔVar dρ) validVar this (cons v Δv R[v,Δv] • _) = R[v,Δv] validVar {τ} (that x) (cons _ _ _ • dρ) = validVar {τ} x dρ validity (intlit n) dρ = tt validity (add s t) dρ = tt validity (minus t) dρ = tt validity (empty) dρ = tt validity (insert s t) dρ = tt validity (union s t) dρ = tt validity (negate t) dρ = tt validity (flatmap s t) dρ = tt validity (sum t) dρ = tt validity {τ} (var x) dρ = validVar {τ} x dρ validity (app s t) dρ = let v = ⟦ t ⟧ (ignore dρ) Δv = ⟦ t ⟧Δ dρ in proj₁ (validity s dρ (cons v Δv (validity t dρ))) validity {σ ⇒ τ} (abs t) dρ = λ v → let v′ = after {σ} v Δv′ = v′ ⊟₍ σ ₎ v′ dρ₁ = v • dρ dρ₂ = nil-valid-change σ v′ • dρ in validity t dρ₁ , (begin ⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂ ≡⟨ correctness t dρ₂ ⟩ ⟦ t ⟧ (update dρ₂) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (update dρ₁) ≡⟨ sym (correctness t dρ₁) ⟩ ⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁ ∎) where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) (dρ : ΔEnv Γ) → ⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ) correctVar (this) (cons v dv R[v,dv] • dρ) = refl correctVar (that y) (cons v dv R[v,dv] • dρ) = correctVar y dρ correctness (intlit n) dρ = right-id-int n correctness (add s t) dρ = trans (mn·pq=mp·nq {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _+_ (correctness s dρ) (correctness t dρ)) correctness (minus t) dρ = trans (-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong -_ (correctness t dρ)) correctness empty dρ = right-id-bag emptyBag correctness (insert s t) dρ = let n = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) n′ = ⟦ s ⟧ (update dρ) b′ = ⟦ t ⟧ (update dρ) Δn = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in begin (singletonBag n ++ b) ++ (singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b) ≡⟨ a++[b\\a]=b ⟩ singletonBag (n ⊞₍ base base-int ₎ Δn) ++ (b ⊞₍ base base-bag ₎ Δb) ≡⟨ cong₂ _++_ (cong singletonBag (correctness s dρ)) (correctness t dρ) ⟩ singletonBag n′ ++ b′ ∎ where open ≡-Reasoning correctness (union s t) dρ = trans (ab·cd=ac·bd {⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ}) (cong₂ _++_ (correctness s dρ) (correctness t dρ)) correctness (negate t) dρ = trans (-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ}) (cong negateBag (correctness t dρ)) correctness (flatmap s t) dρ = let f = ⟦ s ⟧ (ignore dρ) b = ⟦ t ⟧ (ignore dρ) Δf = ⟦ s ⟧Δ dρ Δb = ⟦ t ⟧Δ dρ in trans (a++[b\\a]=b {flatmapBag f b} {flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf) (b ⊞₍ base base-bag ₎ Δb)}) (cong₂ flatmapBag (correctness s dρ) (correctness t dρ)) correctness (sum t) dρ = let b = ⟦ t ⟧ (ignore dρ) Δb = ⟦ t ⟧Δ dρ in trans (sym homo-sum) (cong sumBag (correctness t dρ)) correctness {τ} (var x) dρ = correctVar {τ} x dρ correctness (app {σ} {τ} s t) dρ = let f = ⟦ s ⟧ (ignore dρ) g = ⟦ s ⟧ (update dρ) u = ⟦ t ⟧ (ignore dρ) v = ⟦ t ⟧ (update dρ) Δf = ⟦ s ⟧Δ dρ Δu = ⟦ t ⟧Δ dρ in begin f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ)) ≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s dρ ⟨$⟩ correctness {σ} t dρ ⟩ g v ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) dρ = ext (λ v → let dρ′ : ΔEnv (σ • Γ) dρ′ = nil-valid-change σ v • dρ in begin ⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′ ≡⟨ correctness {τ} t dρ′ ⟩ ⟦ t ⟧ (update dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩ ⟦ t ⟧ (v • update dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} → (⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ) (⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ) ≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ)) ⊞₍ τ ₎ (⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ)) corollary {σ} {τ} s t {dρ} = proj₂ (validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))) corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)} (v : ValidChange σ) → ⟦ t ⟧ ∅ (after {σ} v) ≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v corollary-closed {σ} {τ} {t = t} v = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ in begin f (after {σ} v) ≡⟨ cong (λ hole → hole (after {σ} v)) (sym (correctness {σ ⇒ τ} t ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v) ≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩ f (before {σ} v) ⊞₍ τ ₎ Δf v ∎ where open ≡-Reasoning
Make some arguments of correctVar explicit.
Make some arguments of correctVar explicit. Old-commit-hash: 2596b6835434e84c2b902574a029b3b1644baa1b
Agda
mit
inc-lc/ilc-agda
9b2d54a0d452488646b4ad893d98b6b7c51edbc6
lib/Function/Extensionality.agda
lib/Function/Extensionality.agda
{-# OPTIONS --without-K #-} module Function.Extensionality where open import Relation.Binary.PropositionalEquality renaming (subst to tr) postulate FunExt : Set λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f₀ f₁ : (x : A) → B x}{{fe : FunExt}} (f= : ∀ x → f₀ x ≡ f₁ x) → f₀ ≡ f₁ -- This should be derivable if I had a proper proof of λ= tr-λ= : ∀ {a b p}{A : Set a}{B : A → Set b}{x}(P : B x → Set p) {f g : (x : A) → B x}{{fe : FunExt}}(fg : (x : A) → f x ≡ g x) → tr (λ f → P (f x)) (λ= fg) ≡ tr P (fg x)
{-# 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)))
Add happly, and the univalence axioms
Add happly, and the univalence axioms
Agda
bsd-3-clause
crypto-agda/agda-nplib
0087d65feddda54b6f2fa46d87ca1d54d8f1779a
Parametric/Change/Specification.agda
Parametric/Change/Specification.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change evaluation (Def 3.6 and Fig. 4h). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) {{validity-structure : Validity.Structure ⟦_⟧Base}} where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Theorem.CongApp open import Postulate.Extensionality module Structure where --------------- -- Interface -- --------------- -- We provide: Derivatives of terms. ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) -- And we provide correctness proofs about the derivatives. correctness : ∀ {τ Γ} (t : Term Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ ⟦ c ⟧Const ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record { apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ) ; correct = λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎ } where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f₁ = ⟦ s ⟧ ρ f₂ = ⟦ s ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ u₁ = ⟦ t ⟧ ρ u₂ = ⟦ t ⟧ (after-env dρ) Δu = ⟦ t ⟧Δ ρ dρ in begin f₁ u₁ ⊞₍ τ ₎ call-change {σ} {τ} Δf u₁ Δu ≡⟨ sym (is-valid {σ} {τ} Δf u₁ Δu) ⟩ (f₁ ⊞₍ σ ⇒ τ ₎ Δf) (u₁ ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ f₂ u₂ ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Change evaluation (Def 3.6 and Fig. 4h). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Denotation.Value as Value import Parametric.Denotation.Evaluation as Evaluation import Parametric.Change.Validity as Validity module Parametric.Change.Specification {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) {{validity-structure : Validity.Structure ⟦_⟧Base}} where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Theorem.CongApp open import Postulate.Extensionality module Structure where --------------- -- Interface -- --------------- -- We provide: Derivatives of terms. ⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ) -- And we provide correctness proofs about the derivatives. correctness : ∀ {τ Γ} (t : Term Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ -------------------- -- Implementation -- -------------------- ⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ) ⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv ⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ (⟦ const c ⟧Term ρ) ⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ ⟦_⟧Δ (app {σ} {τ} s t) ρ dρ = call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) ⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record { apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ) ; correct = λ v dv → begin ⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩ ⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)) ≡⟨⟩ ⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩ ⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ) ≡⟨⟩ ⟦ t ⟧ (after-env (dv • dρ)) ≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩ ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ) ∎ } where open ≡-Reasoning correctVar : ∀ {τ Γ} (x : Var Γ τ) → IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar correctVar (this) (v • ρ) (dv • dρ) = refl correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ correctness (app {σ} {τ} s t) ρ dρ = let f₁ = ⟦ s ⟧ ρ f₂ = ⟦ s ⟧ (after-env dρ) Δf = ⟦ s ⟧Δ ρ dρ u₁ = ⟦ t ⟧ ρ u₂ = ⟦ t ⟧ (after-env dρ) Δu = ⟦ t ⟧Δ ρ dρ in begin f₁ u₁ ⊞₍ τ ₎ call-change {σ} {τ} Δf u₁ Δu ≡⟨ sym (is-valid {σ} {τ} Δf u₁ Δu) ⟩ (f₁ ⊞₍ σ ⇒ τ ₎ Δf) (u₁ ⊞₍ σ ₎ Δu) ≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩ f₂ u₂ ∎ where open ≡-Reasoning correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v → let -- dρ′ : ΔEnv (σ • Γ) (v • ρ) dρ′ = nil₍ σ ₎ v • dρ in begin ⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′ ≡⟨ correctness {τ} t _ dρ′ ⟩ ⟦ t ⟧ (after-env dρ′) ≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩ ⟦ t ⟧ (v • after-env dρ) ∎ ) where open ≡-Reasoning -- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv corollary : ∀ {σ τ Γ} (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → (⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ) ≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ) ⊞₍ τ ₎ (call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary {σ} {τ} s t ρ dρ = is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ)) (v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v) → ⟦ t ⟧ ∅ (after₍ σ ₎ dv) ≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv corollary-closed {σ} {τ} t v dv = let f = ⟦ t ⟧ ∅ Δf = ⟦ t ⟧Δ ∅ ∅ in begin f (after₍ σ ₎ dv) ≡⟨ cong (λ hole → hole (after₍ σ ₎ dv)) (sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩ (f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv) ≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩ f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv ∎ where open ≡-Reasoning
Simplify definition
Simplify definition This variant is simpler to think about and do proofs about on paper, and the two variants are definitionally equal for Agda.
Agda
mit
inc-lc/ilc-agda
df3e9873d4d0c9dac1df76aa623fc13494cecc2d
lib/NomPa/All.agda
lib/NomPa/All.agda
module NomPa.All where import NomPa.Record import NomPa.Derived import NomPa.Derived.NaPa import NomPa.Traverse import NomPa.Implem import NomPa.Examples.LC.DataTypes import NomPa.Examples.LC.Contexts import NomPa.Examples.LC.Equiv -- import NomPa.Examples.LC.Monadic -- import NomPa.Examples.LC.Monadic.Tests import NomPa.Examples.LC.DbLevels import NomPa.Examples.NaPa.LC import NomPa.Examples.LC.Combined import NomPa.Examples.LC import NomPa.Examples.Records -- import NomPa.Examples.Records.TypedVec -- import NomPa.Examples.Records.Typed import NomPa.Laws import NomPa.Link import NomPa.NomT import NomPa.NomT.Maybe -- Normalization By Evaluation import NomPa.Examples.NBE import NomPa.Examples.NBE.Can import NomPa.Examples.NBE.Env import NomPa.Examples.NBE.Neu import NomPa.Examples.NBE.Tests -- bare, raw, non-fancy… import NomPa.Examples.BareDB import NomPa.Examples.Raw import NomPa.Examples.Raw.Parser import NomPa.Examples.Raw.Printer import NomPa.Examples.LC.WellScopedJudgment -- Encodings: Emebedding of nominal types import NomPa.Encodings.NominalTypes import NomPa.Encodings.NominalTypes.Generic import NomPa.Encodings.NominalTypes.Generic.Subst import NomPa.Encodings.NominalTypes.Generic.Combined import NomPa.Encodings.NominalTypes.MultiSorts import NomPa.Encodings.NominalTypes.MultiSorts.Test import NomPa.Encodings.AlphaCaml import NomPa.Encodings.AlphaCaml.Test import NomPa.Encodings.BindersUnbound import NomPa.Encodings.BindersUnbound.Generic -- Algorithms import NomPa.Examples.LC.CC import NomPa.Examples.LC.Monadic import NomPa.Examples.LC.Monadic.Tests -- hybrid import NomPa.Examples.LN import NomPa.Examples.LL import NomPa.Examples.LocallyNamed -- More examples import NomPa.Examples.LC.Maybe import NomPa.Examples.LC.SymanticsTy import NomPa.Examples.LC.Tests import NomPa.Examples.LocallyClosed import NomPa.Examples.PTS import NomPa.Examples.PTS.F import NomPa.Examples.Reg -- Tests import NomPa.Examples.Tests -- Logical relations import NomPa.Record.LogicalRelation import NomPa.Implem.LogicalRelation import NomPa.Examples.LC.DataTypes.Logical -- Universal types import NomPa.Universal.RegularTree import NomPa.Universal.Sexp import NomPa.Derived.WorldInclusion
module NomPa.All where import NomPa.Record import NomPa.Derived import NomPa.Derived.NaPa import NomPa.Traverse import NomPa.Implem import NomPa.Examples.LC.DataTypes import NomPa.Examples.LC.Contexts import NomPa.Examples.LC.Equiv -- import NomPa.Examples.LC.Monadic -- import NomPa.Examples.LC.Monadic.Tests import NomPa.Examples.LC.DbLevels import NomPa.Examples.NaPa.LC import NomPa.Examples.LC.Combined import NomPa.Examples.LC import NomPa.Examples.Records -- import NomPa.Examples.Records.TypedVec -- import NomPa.Examples.Records.Typed import NomPa.Laws import NomPa.Link import NomPa.NomT import NomPa.NomT.Maybe -- Normalization By Evaluation import NomPa.Examples.NBE import NomPa.Examples.NBE.Can import NomPa.Examples.NBE.Env import NomPa.Examples.NBE.Neu import NomPa.Examples.NBE.Tests -- bare, raw, non-fancy… import NomPa.Examples.BareDB import NomPa.Examples.Raw import NomPa.Examples.Raw.Parser import NomPa.Examples.Raw.Printer import NomPa.Examples.LC.WellScopedJudgment -- Encodings: Emebedding of nominal types import NomPa.Encodings.NominalTypes import NomPa.Encodings.NominalTypes.Generic import NomPa.Encodings.NominalTypes.Generic.Subst import NomPa.Encodings.NominalTypes.Generic.Combined import NomPa.Encodings.NominalTypes.MultiSorts import NomPa.Encodings.NominalTypes.MultiSorts.Test import NomPa.Encodings.AlphaCaml import NomPa.Encodings.AlphaCaml.Test import NomPa.Encodings.BindersUnbound import NomPa.Encodings.BindersUnbound.Generic -- Algorithms import NomPa.Examples.LC.CC import NomPa.Examples.LC.Monadic import NomPa.Examples.LC.Monadic.Tests -- hybrid import NomPa.Examples.LN import NomPa.Examples.LL import NomPa.Examples.LocallyNamed -- More examples import NomPa.Examples.LC.Maybe --import NomPa.Examples.LC.SymanticsTy import NomPa.Examples.LC.Tests import NomPa.Examples.LocallyClosed import NomPa.Examples.PTS import NomPa.Examples.PTS.F import NomPa.Examples.Reg -- Tests import NomPa.Examples.Tests -- Logical relations import NomPa.Record.LogicalRelation import NomPa.Implem.LogicalRelation import NomPa.Examples.LC.DataTypes.Logical -- Universal types import NomPa.Universal.RegularTree import NomPa.Universal.Sexp import NomPa.Derived.WorldInclusion
Disable broken import
NomPa: Disable broken import
Agda
bsd-3-clause
np/NomPa
95e2bfa97f5f84f48cf5fe9cd6a123b8e8ddb4bb
Game/ReceiptFreeness/Definitions.agda
Game/ReceiptFreeness/Definitions.agda
{-# OPTIONS --without-K #-} open import Function open import Type open import Data.Product renaming (zip to zip-×) open import Data.Two open import Data.List as L open import Data.Nat.NP hiding (_==_) module Game.ReceiptFreeness.Definitions (PubKey : ★) (SecKey : ★) -- Message = 𝟚 (CipherText : ★) (SerialNumber : ★) -- randomness supply for, encryption, key-generation, adversary, adversary state (Rₑ Rₐ : ★) (Enc : let Message = 𝟚 in PubKey → Message → Rₑ → CipherText) (Dec : let Message = 𝟚 in SecKey → CipherText → Message) where Candidate : ★ Candidate = 𝟚 -- as in the paper: "for simplicity" alice bob : Candidate alice = 0₂ bob = 1₂ -- candidate order -- also known as LHS or Message -- represented by who is the first candidate CO = 𝟚 alice-then-bob bob-then-alice : CO alice-then-bob = alice bob-then-alice = bob data CO-spec : CO → Candidate → Candidate → ★ where alice-then-bob-spec : CO-spec alice-then-bob alice bob bob-then-alice-spec : CO-spec bob-then-alice bob alice MarkedReceipt : ★ MarkedReceipt = 𝟚 marked-on-first-cell marked-on-second-cell : MarkedReceipt marked-on-first-cell = 0₂ marked-on-second-cell = 1₂ data MarkedReceipt-spec : CO → MarkedReceipt → Candidate → ★ where m1 : MarkedReceipt-spec alice-then-bob marked-on-first-cell alice m2 : MarkedReceipt-spec alice-then-bob marked-on-second-cell bob m3 : MarkedReceipt-spec bob-then-alice marked-on-first-cell bob m4 : MarkedReceipt-spec bob-then-alice marked-on-second-cell alice data MarkedReceipt? : ★ where not-marked : MarkedReceipt? marked : MarkedReceipt → MarkedReceipt? -- Receipt or also called RHS -- Made of a potential mark, a serial number, and an encrypted candidate order Receipt : ★ Receipt = MarkedReceipt? × SerialNumber × CipherText markedReceipt? : Receipt → MarkedReceipt? markedReceipt? = proj₁ -- Marked when there is a 1 marked? : MarkedReceipt? → 𝟚 marked? not-marked = 0₂ marked? (marked _) = 1₂ marked-on-first-cell? : MarkedReceipt? → 𝟚 marked-on-first-cell? not-marked = 0₂ marked-on-first-cell? (marked x) = x == 0₂ marked-on-second-cell? : MarkedReceipt? → 𝟚 marked-on-second-cell? not-marked = 0₂ marked-on-second-cell? (marked x) = x == 1₂ enc-co : Receipt → CipherText enc-co = proj₂ ∘ proj₂ Ballot : ★ Ballot = CO × Receipt -- co or also called LHS co : Ballot → CO co = proj₁ -- receipt or also called RHS receipt : Ballot → Receipt receipt = proj₂ -- randomness for genBallot Rgb : ★ Rgb = CO × SerialNumber × Rₑ genBallot : PubKey → Rgb → Ballot genBallot pk (r-co , sn , rₑ) = r-co , not-marked , sn , Enc pk r-co rₑ mark : CO → Candidate → MarkedReceipt mark co c = co xor c mark-ok : ∀ co c → MarkedReceipt-spec co (mark co c) c mark-ok 1₂ 1₂ = m3 mark-ok 1₂ 0₂ = m4 mark-ok 0₂ 1₂ = m2 mark-ok 0₂ 0₂ = m1 fillBallot : Candidate → Ballot → Ballot fillBallot c (co , _ , sn , enc-co) = co , marked (mark co c) , sn , enc-co -- TODO Ballot-spec c (fillBallot b) ClearReceipt : ★ ClearReceipt = CO × MarkedReceipt? ClearBB : ★ ClearBB = List ClearReceipt Tally : ★ Tally = ℕ × ℕ alice-score : Tally → ℕ alice-score = proj₁ bob-score : Tally → ℕ bob-score = proj₂ 1:alice-0:bob 0:alice-1:bob : Tally 1:alice-0:bob = 1 , 0 0:alice-1:bob = 0 , 1 data TallyMarkedReceipt-spec : CO → MarkedReceipt → Tally → ★ where c1 : TallyMarkedReceipt-spec alice-then-bob marked-on-first-cell 1:alice-0:bob c2 : TallyMarkedReceipt-spec alice-then-bob marked-on-second-cell 0:alice-1:bob c3 : TallyMarkedReceipt-spec bob-then-alice marked-on-first-cell 0:alice-1:bob c4 : TallyMarkedReceipt-spec bob-then-alice marked-on-second-cell 1:alice-0:bob marked-for-alice? : CO → MarkedReceipt → 𝟚 marked-for-alice? co m = co == m marked-for-bob? : CO → MarkedReceipt → 𝟚 marked-for-bob? co m = not (marked-for-alice? co m) tallyMarkedReceipt : CO → MarkedReceipt → Tally tallyMarkedReceipt co m = 𝟚▹ℕ for-alice , 𝟚▹ℕ (not for-alice) where for-alice = marked-for-alice? co m tallyMarkedReceipt-ok : ∀ co m → TallyMarkedReceipt-spec co m (tallyMarkedReceipt co m) tallyMarkedReceipt-ok 1₂ 1₂ = c4 tallyMarkedReceipt-ok 1₂ 0₂ = c3 tallyMarkedReceipt-ok 0₂ 1₂ = c2 tallyMarkedReceipt-ok 0₂ 0₂ = c1 tallyMarkedReceipt? : CO → MarkedReceipt? → Tally tallyMarkedReceipt? co not-marked = 0 , 0 tallyMarkedReceipt? co (marked mark) = tallyMarkedReceipt co mark _+,+_ : Tally → Tally → Tally _+,+_ = zip-× _+_ _+_ -- Not taking advantage of any homomorphic encryption tallyClearBB : ClearBB → Tally tallyClearBB = L.foldr _+,+_ (0 , 0) ∘ L.map (uncurry tallyMarkedReceipt?) BB : ★ BB = List Receipt DecReceipt : SecKey → Receipt → CO × MarkedReceipt? DecReceipt sk (m? , sn , enc-co) = Dec sk enc-co , m? DecBB : SecKey → BB → ClearBB DecBB = L.map ∘ DecReceipt -- Not taking advantage of any homomorphic encryption tally : SecKey → BB → Tally tally sk bb = tallyClearBB (DecBB sk bb) open import Game.ReceiptFreeness.Adversary PubKey (SerialNumber ²) Rₐ Receipt Ballot Tally CO BB public -- -} -- -} -- -} -- -} -- -} -- -} -- -}
{-# OPTIONS --without-K #-} open import Function open import Type open import Data.Product renaming (zip to zip-×) open import Data.Two open import Data.List as L open import Data.Nat.NP hiding (_==_) module Game.ReceiptFreeness.Definitions (PubKey : ★) (SecKey : ★) -- Message = 𝟚 (CipherText : ★) (SerialNumber : ★) -- randomness supply for, encryption, key-generation, adversary, adversary state (Rₑ Rₐ : ★) (Enc : let Message = 𝟚 in PubKey → Message → Rₑ → CipherText) (Dec : let Message = 𝟚 in SecKey → CipherText → Message) where Candidate : ★ Candidate = 𝟚 -- as in the paper: "for simplicity" alice bob : Candidate alice = 0₂ bob = 1₂ -- candidate order -- also known as LHS or Message -- represented by who is the first candidate CO = 𝟚 alice-then-bob bob-then-alice : CO alice-then-bob = alice bob-then-alice = bob data CO-spec : CO → Candidate → Candidate → ★ where alice-then-bob-spec : CO-spec alice-then-bob alice bob bob-then-alice-spec : CO-spec bob-then-alice bob alice MarkedReceipt : ★ MarkedReceipt = 𝟚 marked-on-first-cell marked-on-second-cell : MarkedReceipt marked-on-first-cell = 0₂ marked-on-second-cell = 1₂ data MarkedReceipt-spec : CO → MarkedReceipt → Candidate → ★ where m1 : MarkedReceipt-spec alice-then-bob marked-on-first-cell alice m2 : MarkedReceipt-spec alice-then-bob marked-on-second-cell bob m3 : MarkedReceipt-spec bob-then-alice marked-on-first-cell bob m4 : MarkedReceipt-spec bob-then-alice marked-on-second-cell alice data MarkedReceipt? : ★ where not-marked : MarkedReceipt? marked : MarkedReceipt → MarkedReceipt? -- Receipt or also called RHS -- Made of a potential mark, a serial number, and an encrypted candidate order Receipt : ★ Receipt = MarkedReceipt? × SerialNumber × CipherText markedReceipt? : Receipt → MarkedReceipt? markedReceipt? = proj₁ -- Marked when there is a 1 marked? : MarkedReceipt? → 𝟚 marked? not-marked = 0₂ marked? (marked _) = 1₂ marked-on-first-cell? : MarkedReceipt? → 𝟚 marked-on-first-cell? not-marked = 0₂ marked-on-first-cell? (marked x) = x == 0₂ marked-on-second-cell? : MarkedReceipt? → 𝟚 marked-on-second-cell? not-marked = 0₂ marked-on-second-cell? (marked x) = x == 1₂ enc-co : Receipt → CipherText enc-co = proj₂ ∘ proj₂ m? : Receipt → MarkedReceipt? m? = proj₁ Ballot : ★ Ballot = CO × Receipt -- co or also called LHS co : Ballot → CO co = proj₁ -- receipt or also called RHS receipt : Ballot → Receipt receipt = proj₂ -- randomness for genBallot Rgb : ★ Rgb = CO × SerialNumber × Rₑ genBallot : PubKey → Rgb → Ballot genBallot pk (r-co , sn , rₑ) = r-co , not-marked , sn , Enc pk r-co rₑ mark : CO → Candidate → MarkedReceipt mark co c = co xor c mark-ok : ∀ co c → MarkedReceipt-spec co (mark co c) c mark-ok 1₂ 1₂ = m3 mark-ok 1₂ 0₂ = m4 mark-ok 0₂ 1₂ = m2 mark-ok 0₂ 0₂ = m1 fillBallot : Candidate → Ballot → Ballot fillBallot c (co , _ , sn , enc-co) = co , marked (mark co c) , sn , enc-co -- TODO Ballot-spec c (fillBallot b) ClearReceipt : ★ ClearReceipt = CO × MarkedReceipt? ClearBB : ★ ClearBB = List ClearReceipt Tally : ★ Tally = ℕ × ℕ alice-score : Tally → ℕ alice-score = proj₁ bob-score : Tally → ℕ bob-score = proj₂ 1:alice-0:bob 0:alice-1:bob : Tally 1:alice-0:bob = 1 , 0 0:alice-1:bob = 0 , 1 data TallyMarkedReceipt-spec : CO → MarkedReceipt → Tally → ★ where c1 : TallyMarkedReceipt-spec alice-then-bob marked-on-first-cell 1:alice-0:bob c2 : TallyMarkedReceipt-spec alice-then-bob marked-on-second-cell 0:alice-1:bob c3 : TallyMarkedReceipt-spec bob-then-alice marked-on-first-cell 0:alice-1:bob c4 : TallyMarkedReceipt-spec bob-then-alice marked-on-second-cell 1:alice-0:bob marked-for-alice? : CO → MarkedReceipt → 𝟚 marked-for-alice? co m = co == m marked-for-bob? : CO → MarkedReceipt → 𝟚 marked-for-bob? co m = not (marked-for-alice? co m) tallyMarkedReceipt : CO → MarkedReceipt → Tally tallyMarkedReceipt co m = 𝟚▹ℕ for-alice , 𝟚▹ℕ (not for-alice) where for-alice = marked-for-alice? co m tallyMarkedReceipt-ok : ∀ co m → TallyMarkedReceipt-spec co m (tallyMarkedReceipt co m) tallyMarkedReceipt-ok 1₂ 1₂ = c4 tallyMarkedReceipt-ok 1₂ 0₂ = c3 tallyMarkedReceipt-ok 0₂ 1₂ = c2 tallyMarkedReceipt-ok 0₂ 0₂ = c1 tallyMarkedReceipt? : CO → MarkedReceipt? → Tally tallyMarkedReceipt? co not-marked = 0 , 0 tallyMarkedReceipt? co (marked mark) = tallyMarkedReceipt co mark _+,+_ : Tally → Tally → Tally _+,+_ = zip-× _+_ _+_ -- Not taking advantage of any homomorphic encryption tallyClearBB : ClearBB → Tally tallyClearBB = L.foldr _+,+_ (0 , 0) ∘ L.map (uncurry tallyMarkedReceipt?) BB : ★ BB = List Receipt DecReceipt : SecKey → Receipt → CO × MarkedReceipt? DecReceipt sk (m? , sn , enc-co) = Dec sk enc-co , m? DecBB : SecKey → BB → ClearBB DecBB = L.map ∘ DecReceipt -- Not taking advantage of any homomorphic encryption tally : SecKey → BB → Tally tally sk bb = tallyClearBB (DecBB sk bb) open import Game.ReceiptFreeness.Adversary PubKey (SerialNumber ²) Rₐ Receipt Ballot Tally CO BB public -- -} -- -} -- -} -- -} -- -} -- -} -- -}
Add missing projection for Receipts
Add missing projection for Receipts
Agda
bsd-3-clause
crypto-agda/crypto-agda
a74d5f4d0654808362f2583716c758bb12b35288
Parametric/Change/Implementation.agda
Parametric/Change/Implementation.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.Value as ChangeValue import Parametric.Change.Derive as Derive module Parametric.Change.Implementation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (validity-structure : Validity.Structure ⟦_⟧Base) (specification-structure : Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) (derive-const : Derive.Structure Const ΔBase) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure open ChangeType.Structure Base ΔBase open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open Derive.Structure Const ΔBase derive-const open import Base.Denotation.Notation -- Notions of programs being implementations of specifications open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field implements-base : ∀ ι {v : ⟦ ι ⟧Base} → Δ₍ ι ₎ v → ⟦ ΔBase ι ⟧Base → Set u⊟v≈u⊝v-base : ∀ ι {u v : ⟦ ι ⟧Base} → implements-base ι (u ⊟₍ ι ₎ v) (⟦diff-base⟧ ι u v) carry-over-base : ∀ {ι} {v : ⟦ ι ⟧Base} (Δv : Δ₍ ι ₎ v) {Δv′ : ⟦ ΔBase ι ⟧Base} (Δv≈Δv′ : implements-base ι Δv Δv′) → v ⊞₍ base ι ₎ Δv ≡ v ⟦⊕₍ base ι ₎⟧ Δv′ ------------------------ -- Logical relation ≈ -- ------------------------ infix 4 implements syntax implements τ u v = u ≈₍ τ ₎ v implements : ∀ τ {v} → Δ₍ τ ₎ v → ⟦ ΔType τ ⟧ → Set implements (base ι) Δf Δf′ = implements-base ι Δf Δf′ implements (σ ⇒ τ) {f} Δf Δf′ = (w : ⟦ σ ⟧) (Δw : Δ₍ σ ₎ w) (Δw′ : ⟦ ΔType σ ⟧) (Δw≈Δw′ : implements σ {w} Δw Δw′) → implements τ {f w} (call-change {σ} {τ} Δf w Δw) (Δf′ w Δw′) infix 4 _≈_ _≈_ : ∀ {τ v} → Δ₍ τ ₎ v → ⟦ ΔType τ ⟧ → Set _≈_ {τ} {v} = implements τ {v} data implements-env : ∀ Γ → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ mapContext ΔType Γ ⟧ → Set where ∅ : implements-env ∅ {∅} ∅ ∅ _•_ : ∀ {τ Γ v ρ dv dρ v′ ρ′} → (dv≈v′ : implements τ {v} dv v′) → (dρ≈ρ′ : implements-env Γ {ρ} dρ ρ′) → implements-env (τ • Γ) {v • ρ} (dv • dρ) (v′ • ρ′) ---------------- -- carry-over -- ---------------- -- If a program implements a specification, then certain things -- proven about the specification carry over to the programs. carry-over : ∀ {τ} {v : ⟦ τ ⟧} (Δv : Δ₍ τ ₎ v) {Δv′ : ⟦ ΔType τ ⟧} (Δv≈Δv′ : Δv ≈₍ τ ₎ Δv′) → after₍ τ ₎ Δv ≡ before₍ τ ₎ Δv ⟦⊕₍ τ ₎⟧ Δv′ u⊟v≈u⊝v : ∀ {τ : Type} {u v : ⟦ τ ⟧} → u ⊟₍ τ ₎ v ≈₍ τ ₎ u ⟦⊝₍ τ ₎⟧ v u⊟v≈u⊝v {base ι} {u} {v} = u⊟v≈u⊝v-base ι {u} {v} u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where result : (w : ⟦ σ ⟧) (Δw : Δ₍ σ ₎ w) → (Δw′ : ⟦ ΔType σ ⟧) → Δw ≈₍ σ ₎ Δw′ → (g (after₍ σ ₎ Δw) ⊟₍ τ ₎ f (before₍ σ ₎ Δw)) ≈₍ τ ₎ g (before₍ σ ₎ Δw ⟦⊕₍ σ ₎⟧ Δw′) ⟦⊝₍ τ ₎⟧ f (before₍ σ ₎ Δw) result w Δw Δw′ Δw≈Δw′ rewrite carry-over {σ} Δw Δw≈Δw′ = u⊟v≈u⊝v {τ} {g (before₍ σ ₎ Δw ⟦⊕₍ σ ₎⟧ Δw′)} {f (before₍ σ ₎ Δw)} carry-over {base ι} Δv Δv≈Δv′ = carry-over-base Δv Δv≈Δv′ carry-over {σ ⇒ τ} {f} Δf {Δf′} Δf≈Δf′ = ext (λ v → carry-over {τ} {f v} (call-change {σ} {τ} Δf v (nil₍ σ ₎ v)) {Δf′ v (v ⟦⊝₍ σ ₎⟧ v)} (Δf≈Δf′ v (nil₍ σ ₎ v) (v ⟦⊝₍ σ ₎⟧ v) ( u⊟v≈u⊝v {σ} {v} {v}))) -- A property relating `alternate` and the subcontext relation Γ≼ΔΓ ⟦Γ≼ΔΓ⟧ : ∀ {Γ} (ρ : ⟦ Γ ⟧) (dρ : ⟦ mapContext ΔType Γ ⟧) → ρ ≡ ⟦ Γ≼ΔΓ ⟧ (alternate ρ dρ) ⟦Γ≼ΔΓ⟧ ∅ ∅ = refl ⟦Γ≼ΔΓ⟧ (v • ρ) (dv • dρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ ρ dρ) -- A specialization of the soundness of weakening ⟦fit⟧ : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ⟦ mapContext ΔType Γ ⟧) → ⟦ t ⟧ ρ ≡ ⟦ fit t ⟧ (alternate ρ dρ) ⟦fit⟧ t ρ dρ = trans (cong ⟦ t ⟧ (⟦Γ≼ΔΓ⟧ ρ dρ)) (sym (weaken-sound t _))
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Logical relation for erasure (Def. 3.8 and Lemma 3.9) ------------------------------------------------------------------------ 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.Value as ChangeValue import Parametric.Change.Derive as Derive module Parametric.Change.Implementation {Base : Type.Structure} (Const : Term.Structure Base) (⟦_⟧Base : Value.Structure Base) (⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base) (ΔBase : ChangeType.Structure Base) (validity-structure : Validity.Structure ⟦_⟧Base) (specification-structure : Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure) (⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase) (⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase) (derive-const : Derive.Structure Const ΔBase) where open Type.Structure Base open Term.Structure Base Const open Value.Structure Base ⟦_⟧Base open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const open Validity.Structure ⟦_⟧Base validity-structure open Specification.Structure Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure open ChangeType.Structure Base ΔBase open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧ open Derive.Structure Const ΔBase derive-const open import Base.Denotation.Notation open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field -- Extension point 1: Logical relation on base types. -- -- In the paper, we assume that the logical relation is equality on base types -- (see Def. 3.8a). Here, we only require that plugins define what the logical -- relation is on base types, and provide proofs for the two extension points -- below. implements-base : ∀ ι {v : ⟦ ι ⟧Base} → Δ₍ ι ₎ v → ⟦ ΔBase ι ⟧Base → Set -- Extension point 2: Differences on base types are logically related. u⊟v≈u⊝v-base : ∀ ι {u v : ⟦ ι ⟧Base} → implements-base ι (u ⊟₍ ι ₎ v) (⟦diff-base⟧ ι u v) -- Extension point 3: Lemma 3.1 for base types. carry-over-base : ∀ {ι} {v : ⟦ ι ⟧Base} (Δv : Δ₍ ι ₎ v) {Δv′ : ⟦ ΔBase ι ⟧Base} (Δv≈Δv′ : implements-base ι Δv Δv′) → v ⊞₍ base ι ₎ Δv ≡ v ⟦⊕₍ base ι ₎⟧ Δv′ ------------------------ -- Logical relation ≈ -- ------------------------ infix 4 implements syntax implements τ u v = u ≈₍ τ ₎ v implements : ∀ τ {v} → Δ₍ τ ₎ v → ⟦ ΔType τ ⟧ → Set implements (base ι) Δf Δf′ = implements-base ι Δf Δf′ implements (σ ⇒ τ) {f} Δf Δf′ = (w : ⟦ σ ⟧) (Δw : Δ₍ σ ₎ w) (Δw′ : ⟦ ΔType σ ⟧) (Δw≈Δw′ : implements σ {w} Δw Δw′) → implements τ {f w} (call-change {σ} {τ} Δf w Δw) (Δf′ w Δw′) infix 4 _≈_ _≈_ : ∀ {τ v} → Δ₍ τ ₎ v → ⟦ ΔType τ ⟧ → Set _≈_ {τ} {v} = implements τ {v} data implements-env : ∀ Γ → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ mapContext ΔType Γ ⟧ → Set where ∅ : implements-env ∅ {∅} ∅ ∅ _•_ : ∀ {τ Γ v ρ dv dρ v′ ρ′} → (dv≈v′ : implements τ {v} dv v′) → (dρ≈ρ′ : implements-env Γ {ρ} dρ ρ′) → implements-env (τ • Γ) {v • ρ} (dv • dρ) (v′ • ρ′) ---------------- -- carry-over -- ---------------- -- This is lemma 3.10. carry-over : ∀ {τ} {v : ⟦ τ ⟧} (Δv : Δ₍ τ ₎ v) {Δv′ : ⟦ ΔType τ ⟧} (Δv≈Δv′ : Δv ≈₍ τ ₎ Δv′) → after₍ τ ₎ Δv ≡ before₍ τ ₎ Δv ⟦⊕₍ τ ₎⟧ Δv′ u⊟v≈u⊝v : ∀ {τ : Type} {u v : ⟦ τ ⟧} → u ⊟₍ τ ₎ v ≈₍ τ ₎ u ⟦⊝₍ τ ₎⟧ v u⊟v≈u⊝v {base ι} {u} {v} = u⊟v≈u⊝v-base ι {u} {v} u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where result : (w : ⟦ σ ⟧) (Δw : Δ₍ σ ₎ w) → (Δw′ : ⟦ ΔType σ ⟧) → Δw ≈₍ σ ₎ Δw′ → (g (after₍ σ ₎ Δw) ⊟₍ τ ₎ f (before₍ σ ₎ Δw)) ≈₍ τ ₎ g (before₍ σ ₎ Δw ⟦⊕₍ σ ₎⟧ Δw′) ⟦⊝₍ τ ₎⟧ f (before₍ σ ₎ Δw) result w Δw Δw′ Δw≈Δw′ rewrite carry-over {σ} Δw Δw≈Δw′ = u⊟v≈u⊝v {τ} {g (before₍ σ ₎ Δw ⟦⊕₍ σ ₎⟧ Δw′)} {f (before₍ σ ₎ Δw)} carry-over {base ι} Δv Δv≈Δv′ = carry-over-base Δv Δv≈Δv′ carry-over {σ ⇒ τ} {f} Δf {Δf′} Δf≈Δf′ = ext (λ v → carry-over {τ} {f v} (call-change {σ} {τ} Δf v (nil₍ σ ₎ v)) {Δf′ v (v ⟦⊝₍ σ ₎⟧ v)} (Δf≈Δf′ v (nil₍ σ ₎ v) (v ⟦⊝₍ σ ₎⟧ v) ( u⊟v≈u⊝v {σ} {v} {v}))) -- A property relating `alternate` and the subcontext relation Γ≼ΔΓ ⟦Γ≼ΔΓ⟧ : ∀ {Γ} (ρ : ⟦ Γ ⟧) (dρ : ⟦ mapContext ΔType Γ ⟧) → ρ ≡ ⟦ Γ≼ΔΓ ⟧ (alternate ρ dρ) ⟦Γ≼ΔΓ⟧ ∅ ∅ = refl ⟦Γ≼ΔΓ⟧ (v • ρ) (dv • dρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ ρ dρ) -- A specialization of the soundness of weakening ⟦fit⟧ : ∀ {τ Γ} (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : ⟦ mapContext ΔType Γ ⟧) → ⟦ t ⟧ ρ ≡ ⟦ fit t ⟧ (alternate ρ dρ) ⟦fit⟧ t ρ dρ = trans (cong ⟦ t ⟧ (⟦Γ≼ΔΓ⟧ ρ dρ)) (sym (weaken-sound t _))
Document P.C.Implementation.
Document P.C.Implementation. Old-commit-hash: 62c4109c756f345ba501b4ab6a99669cec1f3a21
Agda
mit
inc-lc/ilc-agda
14cfac67791f75bdf456a7774da62935891a6640
Control/Strategy.agda
Control/Strategy.agda
module Control.Strategy where open import Function.NP open import Type using (★) open import Category.Monad open import Data.Product hiding (map) open import Data.List using (List; []; _∷_) open import Relation.Binary.PropositionalEquality.NP data Strategy (Q : ★) (R : Q → ★) (A : ★) : ★ where ask : (q? : Q) (cont : R q? → Strategy Q R A) → Strategy Q R A done : A → Strategy Q R A infix 2 _≈_ data _≈_ {Q R A} : (s₀ s₁ : Strategy Q R A) → ★ where ask-ask : ∀ (q? : Q) {k₀ k₁} (cont : ∀ r → k₀ r ≈ k₁ r) → ask q? k₀ ≈ ask q? k₁ done-done : ∀ x → done x ≈ done x ≈-refl : ∀ {Q R A} {s : Strategy Q R A} → s ≈ s ≈-refl {s = ask q? cont} = ask-ask q? (λ r → ≈-refl) ≈-refl {s = done x} = done-done x module _ {Q : ★} {R : Q → ★} where private M : ★ → ★ M = Strategy Q R _=<<_ : ∀ {A B} → (A → M B) → M A → M B f =<< ask q? cont = ask q? (λ r → f =<< cont r) f =<< done x = f x return : ∀ {A} → A → M A return = done map : ∀ {A B} → (A → B) → M A → M B map f s = (done ∘ f) =<< s join : ∀ {A} → M (M A) → M A join s = id =<< s _>>=_ : ∀ {A B} → M A → (A → M B) → M B _>>=_ = flip _=<<_ rawMonad : RawMonad M rawMonad = record { return = return; _>>=_ = _>>=_ } return-=<< : ∀ {A} (m : M A) → return =<< m ≈ m return-=<< (ask q? cont) = ask-ask q? (λ r → return-=<< (cont r)) return-=<< (done x) = done-done x return->>= : ∀ {A B} (x : A) (f : A → M B) → return x >>= f ≡ f x return->>= _ _ = refl >>=-assoc : ∀ {A B C} (mx : M A) (my : A → M B) (f : B → M C) → (mx >>= λ x → (my x >>= f)) ≈ (mx >>= my) >>= f >>=-assoc (ask q? cont) my f = ask-ask q? (λ r → >>=-assoc (cont r) my f) >>=-assoc (done x) my f = ≈-refl map-id : ∀ {A} (s : M A) → map id s ≈ s map-id = return-=<< map-∘ : ∀ {A B C} (f : A → B) (g : B → C) s → map (g ∘ f) s ≈ (map g ∘ map f) s map-∘ f g s = >>=-assoc s (done ∘ f) (done ∘ g) module EffectfulRun (E : ★ → ★) (_>>=E_ : ∀ {A B} → E A → (A → E B) → E B) (returnE : ∀ {A} → A → E A) (Oracle : (q : Q) → E (R q)) where runE : ∀ {A} → M A → E A runE (ask q? cont) = Oracle q? >>=E (λ r → runE (cont r)) runE (done x) = returnE x module _ (Oracle : (q : Q) → R q) where run : ∀ {A} → M A → A run (ask q? cont) = run (cont (Oracle q?)) run (done x) = x module _ {A B : ★} (f : A → M B) where run->>= : (s : M A) → run (s >>= f) ≡ run (f (run s)) run->>= (ask q? cont) = run->>= (cont (Oracle q?)) run->>= (done x) = refl module _ {A B : ★} (f : A → B) where run-map : (s : M A) → run (map f s) ≡ f (run s) run-map = run->>= (return ∘ f) module _ {A B : ★} where run-≈ : {s₀ s₁ : M A} → s₀ ≈ s₁ → run s₀ ≡ run s₁ run-≈ (ask-ask q? cont) = run-≈ (cont (Oracle q?)) run-≈ (done-done x) = refl private State : (S A : ★) → ★ State S A = S → A × S -- redundant since we have EffectfulRun, but... module StatefulRun {S : ★} (Oracle : (q : Q) → State S (R q)) where runS : ∀ {A} → M A → State S A runS (ask q? cont) s = case Oracle q? s of λ { (x , s') → runS (cont x) s' } runS (done x) s = x , s evalS : ∀ {A} → M A → S → A evalS x s = proj₁ (runS x s) execS : ∀ {A} → M A → S → S execS x s = proj₂ (runS x s) private Transcript = List (Σ Q R) module TranscriptRun (Oracle : Transcript → Π Q R){A} where runT : M A → Transcript → A × Transcript runT (ask q? cont) t = let r = Oracle t q? in runT (cont r) ((q? , r) ∷ t) runT (done x) t = x , t open StatefulRun -- runT is runS where the state is the transcript and the Oracle cannot change it runT-runS : ∀ (str : M A) t → runT str t ≡ runS (λ q s → let r = Oracle s q in r , (q , r) ∷ s) str t runT-runS (ask q? cont) t = let r = Oracle t q? in runT-runS (cont r) ((q? , r) ∷ t) runT-runS (done x) t = refl -- -} -- -} -- -} -- -}
module Control.Strategy where open import Data.Nat open import Function.NP open import Type using (★) open import Category.Monad open import Data.Product hiding (map) open import Data.List as List using (List; []; _∷_) open import Relation.Binary.PropositionalEquality.NP data Strategy (Q : ★) (R : Q → ★) (A : ★) : ★ where ask : (q? : Q) (cont : R q? → Strategy Q R A) → Strategy Q R A done : A → Strategy Q R A infix 2 _≈_ data _≈_ {Q R A} : (s₀ s₁ : Strategy Q R A) → ★ where ask-ask : ∀ (q? : Q) {k₀ k₁} (cont : ∀ r → k₀ r ≈ k₁ r) → ask q? k₀ ≈ ask q? k₁ done-done : ∀ x → done x ≈ done x ≈-refl : ∀ {Q R A} {s : Strategy Q R A} → s ≈ s ≈-refl {s = ask q? cont} = ask-ask q? (λ r → ≈-refl) ≈-refl {s = done x} = done-done x module _ {Q : ★} {R : Q → ★} where private M : ★ → ★ M = Strategy Q R _=<<_ : ∀ {A B} → (A → M B) → M A → M B f =<< ask q? cont = ask q? (λ r → f =<< cont r) f =<< done x = f x return : ∀ {A} → A → M A return = done map : ∀ {A B} → (A → B) → M A → M B map f s = (done ∘ f) =<< s join : ∀ {A} → M (M A) → M A join s = id =<< s _>>=_ : ∀ {A B} → M A → (A → M B) → M B _>>=_ = flip _=<<_ rawMonad : RawMonad M rawMonad = record { return = return; _>>=_ = _>>=_ } return-=<< : ∀ {A} (m : M A) → return =<< m ≈ m return-=<< (ask q? cont) = ask-ask q? (λ r → return-=<< (cont r)) return-=<< (done x) = done-done x return->>= : ∀ {A B} (x : A) (f : A → M B) → return x >>= f ≡ f x return->>= _ _ = refl >>=-assoc : ∀ {A B C} (mx : M A) (my : A → M B) (f : B → M C) → (mx >>= λ x → (my x >>= f)) ≈ (mx >>= my) >>= f >>=-assoc (ask q? cont) my f = ask-ask q? (λ r → >>=-assoc (cont r) my f) >>=-assoc (done x) my f = ≈-refl map-id : ∀ {A} (s : M A) → map id s ≈ s map-id = return-=<< map-∘ : ∀ {A B C} (f : A → B) (g : B → C) s → map (g ∘ f) s ≈ (map g ∘ map f) s map-∘ f g s = >>=-assoc s (done ∘ f) (done ∘ g) module EffectfulRun (E : ★ → ★) (_>>=E_ : ∀ {A B} → E A → (A → E B) → E B) (returnE : ∀ {A} → A → E A) (Oracle : (q : Q) → E (R q)) where runE : ∀ {A} → M A → E A runE (ask q? cont) = Oracle q? >>=E (λ r → runE (cont r)) runE (done x) = returnE x module _ (Oracle : (q : Q) → R q) where run : ∀ {A} → M A → A run (ask q? cont) = run (cont (Oracle q?)) run (done x) = x module _ {A B : ★} (f : A → M B) where run->>= : (s : M A) → run (s >>= f) ≡ run (f (run s)) run->>= (ask q? cont) = run->>= (cont (Oracle q?)) run->>= (done x) = refl module _ {A B : ★} (f : A → B) where run-map : (s : M A) → run (map f s) ≡ f (run s) run-map = run->>= (return ∘ f) module _ {A B : ★} where run-≈ : {s₀ s₁ : M A} → s₀ ≈ s₁ → run s₀ ≡ run s₁ run-≈ (ask-ask q? cont) = run-≈ (cont (Oracle q?)) run-≈ (done-done x) = refl module Repeat {I O : ★} where ind : (I → M O) → List I → M (List O) ind g [] = done [] ind g (x ∷ xs) = g x >>= (λ o → map (_∷_ o) (ind g xs)) module _ (Oracle : (q : Q) → R q)(g : I → M O) where run-ind : ∀ xs → run Oracle (ind g xs) ≡ List.map (run Oracle ∘ g) xs run-ind [] = refl run-ind (x ∷ xs) = run->>= Oracle (λ o → map (_∷_ o) (ind g xs)) (g x) ∙ run-map Oracle (_∷_ (run Oracle (g x))) (ind g xs) ∙ ap (_∷_ (run Oracle (g x))) (run-ind xs) module RepeatIndex {O : ★} where map-list : (ℕ → (q : Q) → R q → O) → List Q → M (List O) map-list _ [] = done [] map-list f (x ∷ xs) = ask x λ r → map (_∷_ (f 0 x r)) (map-list (f ∘ suc) xs) mapIndex : {A B : Set} → (ℕ → A → B) → List A → List B mapIndex f [] = [] mapIndex f (x ∷ xs) = f 0 x ∷ mapIndex (f ∘ suc) xs module _ (Oracle : (q : Q) → R q) where run-map-list : ∀ xs (g : ℕ → (q : Q) → R q → O) → run Oracle (map-list g xs) ≡ mapIndex (λ i q → g i q (Oracle q)) xs run-map-list [] _ = refl run-map-list (x ∷ xs) g = run-map Oracle (_∷_ (g 0 x (Oracle x))) (map-list (g ∘ suc) xs) ∙ ap (_∷_ (g 0 x (Oracle x))) (run-map-list xs (g ∘ suc)) private State : (S A : ★) → ★ State S A = S → A × S -- redundant since we have EffectfulRun, but... module StatefulRun {S : ★} (Oracle : (q : Q) → State S (R q)) where runS : ∀ {A} → M A → State S A runS (ask q? cont) s = case Oracle q? s of λ { (x , s') → runS (cont x) s' } runS (done x) s = x , s evalS : ∀ {A} → M A → S → A evalS x s = proj₁ (runS x s) execS : ∀ {A} → M A → S → S execS x s = proj₂ (runS x s) private Transcript = List (Σ Q R) module TranscriptRun (Oracle : Transcript → Π Q R){A} where runT : M A → Transcript → A × Transcript runT (ask q? cont) t = let r = Oracle t q? in runT (cont r) ((q? , r) ∷ t) runT (done x) t = x , t open StatefulRun -- runT is runS where the state is the transcript and the Oracle cannot change it runT-runS : ∀ (str : M A) t → runT str t ≡ runS (λ q s → let r = Oracle s q in r , (q , r) ∷ s) str t runT-runS (ask q? cont) t = let r = Oracle t q? in runT-runS (cont r) ((q? , r) ∷ t) runT-runS (done x) t = refl module TranscriptConstRun (Oracle : Π Q R){A} where open TranscriptRun runT-run : ∀ (str : M A) {t} → proj₁ (runT (const Oracle) str t) ≡ run Oracle str runT-run (ask q? cont) = let r = Oracle q? in runT-run (cont r) runT-run (done x) = refl -- -} -- -} -- -} -- -}
Add some more ways of computing Strategy
Add some more ways of computing Strategy
Agda
bsd-3-clause
crypto-agda/crypto-agda
00bee87e47150d5062c2e99e9711f35f367885f2
formalization/agda/Spire/Operational.agda
formalization/agda/Spire/Operational.agda
module Spire.Operational where ---------------------------------------------------------------------- data Level : Set where zero : Level suc : Level → Level ---------------------------------------------------------------------- data Context : Set data Type (Γ : Context) : Set data Value (Γ : Context) : Type Γ → Set data Neutral (Γ : Context) : Type Γ → Set ---------------------------------------------------------------------- data Context where ∅ : Context _,_ : (Γ : Context) → Type Γ → Context data Type Γ where `⊥ `⊤ `Bool : Type Γ `Desc `Type : (ℓ : Level) → Type Γ `Π `Σ : (A : Type Γ) (B : Type (Γ , A)) → Type Γ `μ : ∀{ℓ} → Value Γ (`Desc ℓ) → Type Γ `⟦_⟧ : ∀{ℓ} → Neutral Γ (`Type ℓ) → Type Γ `⟦_⟧ᵈ : ∀{ℓ} → Neutral Γ (`Desc ℓ) → Type Γ → Type Γ ---------------------------------------------------------------------- ⟦_⟧ : ∀{Γ ℓ} → Value Γ (`Type ℓ) → Type Γ ⟦_⟧ᵈ : ∀{Γ ℓ} → Value Γ (`Desc ℓ) → Type Γ → Type Γ postulate wknT : ∀{Γ A} → Type Γ → Type (Γ , A) subT : ∀{Γ A} → Type (Γ , A) → Value Γ A → Type Γ subV : ∀{Γ A B} → Value (Γ , A) B → (x : Value Γ A) → Value Γ (subT B x) data Var : (Γ : Context) (A : Type Γ) → Set where here : ∀{Γ A} → Var (Γ , A) (wknT A) there : ∀{Γ A B} → Var Γ A → Var (Γ , B) (wknT A) ---------------------------------------------------------------------- data Value Γ where {- Type introduction -} `⊥ `⊤ `Bool `Desc `Type : ∀{ℓ} → Value Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ) `μ : ∀{ℓ} → Value Γ (`Desc ℓ) → Value Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (suc ℓ)) `⟦_⟧ᵈ : ∀{ℓ} → Value Γ (`Desc ℓ) → Value Γ (`Type ℓ) → Value Γ (`Type ℓ) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Value Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Desc (suc ℓ))) → Value Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Value Γ `⊤ `true `false : Value Γ `Bool _`,_ : ∀{A B} (a : Value Γ A) (b : Value Γ (subT B a)) → Value Γ (`Σ A B) `λ : ∀{A B} → Value (Γ , A) B → Value Γ (`Π A B) `con : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Value Γ (⟦ D ⟧ᵈ (`μ D)) → Value Γ (`μ D) `neut : ∀{A} → Neutral Γ A → Value Γ A ---------------------------------------------------------------------- data Neutral Γ where {- Value elimination -} `var : ∀{A} → Var Γ A → Neutral Γ A `if_`then_`else_ : ∀{C} (b : Neutral Γ `Bool) (c₁ c₂ : Value Γ C) → Neutral Γ C `elim⊥ : ∀{A} → Neutral Γ `⊥ → Neutral Γ A `elimBool : ∀{ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Neutral Γ `Bool) → Neutral Γ (subT ⟦ P ⟧ (`neut b)) `proj₁ : ∀{A B} → Neutral Γ (`Σ A B) → Neutral Γ A `proj₂ : ∀{A B} (ab : Neutral Γ (`Σ A B)) → Neutral Γ (subT B (`neut (`proj₁ ab))) _`$_ : ∀{A B} (f : Neutral Γ (`Π A B)) (a : Value Γ A) → Neutral Γ (subT B a) ---------------------------------------------------------------------- ⟦ `Π A B ⟧ = `Π ⟦ A ⟧ ⟦ B ⟧ ⟦ `Σ A B ⟧ = `Σ ⟦ A ⟧ ⟦ B ⟧ ⟦ `⊥ ⟧ = `⊥ ⟦ `⊤ ⟧ = `⊤ ⟦ `Bool ⟧ = `Bool ⟦ `μ D ⟧ = `μ D ⟦ `Type {zero} ⟧ = `⊥ ⟦ `Type {suc ℓ} ⟧ = `Type ℓ ⟦ `⟦ A ⟧ ⟧ = ⟦ A ⟧ ⟦ `Desc {ℓ} ⟧ = `Desc ℓ ⟦ `⟦ D ⟧ᵈ X ⟧ = ⟦ D ⟧ᵈ ⟦ X ⟧ ⟦ `neut A ⟧ = `⟦ A ⟧ ---------------------------------------------------------------------- ⟦ `⊤ᵈ ⟧ᵈ X = `⊤ ⟦ `Xᵈ ⟧ᵈ X = X ⟦ `Πᵈ A D ⟧ᵈ X = `Π ⟦ A ⟧ (⟦ D ⟧ᵈ (wknT X)) ⟦ `Σᵈ A D ⟧ᵈ X = `Σ ⟦ A ⟧ (⟦ D ⟧ᵈ (wknT X)) ⟦ `neut D ⟧ᵈ X = `⟦ D ⟧ᵈ X ---------------------------------------------------------------------- elim⊥ : ∀{Γ A} → Value Γ `⊥ → Value Γ A elim⊥ (`neut bot) = `neut (`elim⊥ bot) ---------------------------------------------------------------------- if_then_else_ : ∀{Γ C} (b : Value Γ `Bool) (c₁ c₂ : Value Γ C) → Value Γ C if `true then c₁ else c₂ = c₁ if `false then c₁ else c₂ = c₂ if `neut b then c₁ else c₂ = `neut (`if b `then c₁ `else c₂) elimBool : ∀{Γ ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Value Γ `Bool) → Value Γ (subT ⟦ P ⟧ b) elimBool P pt pf `true = pt elimBool P pt pf `false = pf elimBool P pt pf (`neut b) = `neut (`elimBool P pt pf b) ---------------------------------------------------------------------- proj₁ : ∀{Γ A B} → Value Γ (`Σ A B) → Value Γ A proj₁ (a `, b) = a proj₁ (`neut ab) = `neut (`proj₁ ab) proj₂ : ∀{Γ A B} (ab : Value Γ (`Σ A B)) → Value Γ (subT B (proj₁ ab)) proj₂ (a `, b) = b proj₂ (`neut ab) = `neut (`proj₂ ab) ---------------------------------------------------------------------- _$_ : ∀{Γ A B} → Value Γ (`Π A B) → (a : Value Γ A) → Value Γ (subT B a) `λ b $ a = subV b a `neut f $ a = `neut (f `$ a) ---------------------------------------------------------------------- data Term (Γ : Context) : Type Γ → Set eval : ∀{Γ A} → Term Γ A → Value Γ A data Term Γ where {- Type introduction -} `⊥ `⊤ `Bool `Type : ∀{ℓ} → Term Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (B : Term (Γ , ⟦ eval A ⟧) (`Type ℓ)) → Term Γ (`Type ℓ) `μ : ∀{ℓ} → Term Γ (`Desc ℓ) → Term Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Term Γ (`Type ℓ) → Term Γ (`Type (suc ℓ)) `⟦_⟧ᵈ : ∀{ℓ} → Term Γ (`Desc ℓ) → Term Γ (`Type ℓ) → Term Γ (`Type ℓ) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Term Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (D : Term (Γ , ⟦ eval A ⟧) (`Desc (suc ℓ))) → Term Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Term Γ `⊤ `true `false : Term Γ `Bool _`,_ : ∀{A B} (a : Term Γ A) (b : Term Γ (subT B (eval a))) → Term Γ (`Σ A B) `λ : ∀{A B} (b : Term (Γ , A) B) → Term Γ (`Π A B) `con : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Term Γ (⟦ D ⟧ᵈ (`μ D)) → Term Γ (`μ D) {- Value elimination -} `var : ∀{A} → Var Γ A → Term Γ A `if_`then_`else_ : ∀{C} (b : Term Γ `Bool) (c₁ c₂ : Term Γ C) → Term Γ C _`$_ : ∀{A B} (f : Term Γ (`Π A B)) (a : Term Γ A) → Term Γ (subT B (eval a)) `proj₁ : ∀{A B} → Term Γ (`Σ A B) → Term Γ A `proj₂ : ∀{A B} (ab : Term Γ (`Σ A B)) → Term Γ (subT B (proj₁ (eval ab))) `elim⊥ : ∀{A} → Term Γ `⊥ → Term Γ A `elimBool : ∀{ℓ} (P : Term (Γ , `Bool) (`Type ℓ)) (pt : Term Γ (subT ⟦ eval P ⟧ `true)) (pf : Term Γ (subT ⟦ eval P ⟧ `false)) (b : Term Γ `Bool) → Term Γ (subT ⟦ eval P ⟧ (eval b)) ---------------------------------------------------------------------- {- Type introduction -} eval `⊥ = `⊥ eval `⊤ = `⊤ eval `Bool = `Bool eval `Type = `Type eval (`Π A B) = `Π (eval A) (eval B) eval (`Σ A B) = `Σ (eval A) (eval B) eval (`μ D) = `μ (eval D) eval `⟦ A ⟧ = `⟦ eval A ⟧ eval (`⟦ D ⟧ᵈ X) = `⟦ eval D ⟧ᵈ (eval X) {- Desc introduction -} eval `⊤ᵈ = `⊤ᵈ eval `Xᵈ = `Xᵈ eval (`Πᵈ A D) = `Πᵈ (eval A) (eval D) eval (`Σᵈ A D) = `Σᵈ (eval A) (eval D) {- Value introduction -} eval `tt = `tt eval `true = `true eval `false = `false eval (a `, b) = eval a `, eval b eval (`λ b) = `λ (eval b) eval (`con x) = `con (eval x) {- Value elimination -} eval (`var i) = `neut (`var i) eval (`if b `then c₁ `else c₂) = if eval b then eval c₁ else eval c₂ eval (f `$ a) = eval f $ eval a eval (`proj₁ ab) = proj₁ (eval ab) eval (`proj₂ ab) = proj₂ (eval ab) eval (`elim⊥ bot) = elim⊥ (eval bot) eval (`elimBool P pt pf b) = elimBool (eval P) (eval pt) (eval pf) (eval b) ----------------------------------------------------------------------
module Spire.Operational where ---------------------------------------------------------------------- data Level : Set where zero : Level suc : Level → Level ---------------------------------------------------------------------- data Context : Set data Type (Γ : Context) : Set data Value (Γ : Context) : Type Γ → Set data Neutral (Γ : Context) : Type Γ → Set ---------------------------------------------------------------------- data Context where ∅ : Context _,_ : (Γ : Context) → Type Γ → Context data Type Γ where `⊥ `⊤ `Bool : Type Γ `Desc `Type : (ℓ : Level) → Type Γ `Π `Σ : (A : Type Γ) (B : Type (Γ , A)) → Type Γ `μ : ∀{ℓ} → Value Γ (`Desc ℓ) → Type Γ `⟦_⟧ : ∀{ℓ} → Neutral Γ (`Type ℓ) → Type Γ `⟦_⟧ᵈ : ∀{ℓ} → Neutral Γ (`Desc ℓ) → Type Γ → Type Γ ---------------------------------------------------------------------- ⟦_⟧ : ∀{Γ ℓ} → Value Γ (`Type ℓ) → Type Γ ⟦_⟧ᵈ : ∀{Γ ℓ} → Value Γ (`Desc ℓ) → Type Γ → Type Γ postulate wknT : ∀{Γ A} → Type Γ → Type (Γ , A) subT : ∀{Γ A} → Type (Γ , A) → Value Γ A → Type Γ subV : ∀{Γ A B} → Value (Γ , A) B → (x : Value Γ A) → Value Γ (subT B x) data Var : (Γ : Context) (A : Type Γ) → Set where here : ∀{Γ A} → Var (Γ , A) (wknT A) there : ∀{Γ A B} → Var Γ A → Var (Γ , B) (wknT A) ---------------------------------------------------------------------- data Value Γ where {- Type introduction -} `⊥ `⊤ `Bool `Desc `Type : ∀{ℓ} → Value Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ) `μ : ∀{ℓ} → Value Γ (`Desc ℓ) → Value Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (suc ℓ)) `⟦_⟧ᵈ : ∀{ℓ} → Value Γ (`Desc ℓ) → Value Γ (`Type ℓ) → Value Γ (`Type ℓ) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Value Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Desc (suc ℓ))) → Value Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Value Γ `⊤ `true `false : Value Γ `Bool _`,_ : ∀{A B} (a : Value Γ A) (b : Value Γ (subT B a)) → Value Γ (`Σ A B) `λ : ∀{A B} → Value (Γ , A) B → Value Γ (`Π A B) `con : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Value Γ (⟦ D ⟧ᵈ (`μ D)) → Value Γ (`μ D) `neut : ∀{A} → Neutral Γ A → Value Γ A ---------------------------------------------------------------------- data Neutral Γ where {- Value elimination -} `var : ∀{A} → Var Γ A → Neutral Γ A `if_`then_`else_ : ∀{C} (b : Neutral Γ `Bool) (c₁ c₂ : Value Γ C) → Neutral Γ C `elim⊥ : ∀{A} → Neutral Γ `⊥ → Neutral Γ A `elimBool : ∀{ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Neutral Γ `Bool) → Neutral Γ (subT ⟦ P ⟧ (`neut b)) `proj₁ : ∀{A B} → Neutral Γ (`Σ A B) → Neutral Γ A `proj₂ : ∀{A B} (ab : Neutral Γ (`Σ A B)) → Neutral Γ (subT B (`neut (`proj₁ ab))) _`$_ : ∀{A B} (f : Neutral Γ (`Π A B)) (a : Value Γ A) → Neutral Γ (subT B a) `des : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Neutral Γ (`μ D) → Neutral Γ (⟦ D ⟧ᵈ (`μ D)) ---------------------------------------------------------------------- ⟦ `Π A B ⟧ = `Π ⟦ A ⟧ ⟦ B ⟧ ⟦ `Σ A B ⟧ = `Σ ⟦ A ⟧ ⟦ B ⟧ ⟦ `⊥ ⟧ = `⊥ ⟦ `⊤ ⟧ = `⊤ ⟦ `Bool ⟧ = `Bool ⟦ `μ D ⟧ = `μ D ⟦ `Type {zero} ⟧ = `⊥ ⟦ `Type {suc ℓ} ⟧ = `Type ℓ ⟦ `⟦ A ⟧ ⟧ = ⟦ A ⟧ ⟦ `Desc {ℓ} ⟧ = `Desc ℓ ⟦ `⟦ D ⟧ᵈ X ⟧ = ⟦ D ⟧ᵈ ⟦ X ⟧ ⟦ `neut A ⟧ = `⟦ A ⟧ ---------------------------------------------------------------------- ⟦ `⊤ᵈ ⟧ᵈ X = `⊤ ⟦ `Xᵈ ⟧ᵈ X = X ⟦ `Πᵈ A D ⟧ᵈ X = `Π ⟦ A ⟧ (⟦ D ⟧ᵈ (wknT X)) ⟦ `Σᵈ A D ⟧ᵈ X = `Σ ⟦ A ⟧ (⟦ D ⟧ᵈ (wknT X)) ⟦ `neut D ⟧ᵈ X = `⟦ D ⟧ᵈ X ---------------------------------------------------------------------- elim⊥ : ∀{Γ A} → Value Γ `⊥ → Value Γ A elim⊥ (`neut bot) = `neut (`elim⊥ bot) ---------------------------------------------------------------------- if_then_else_ : ∀{Γ C} (b : Value Γ `Bool) (c₁ c₂ : Value Γ C) → Value Γ C if `true then c₁ else c₂ = c₁ if `false then c₁ else c₂ = c₂ if `neut b then c₁ else c₂ = `neut (`if b `then c₁ `else c₂) elimBool : ∀{Γ ℓ} (P : Value (Γ , `Bool) (`Type ℓ)) (pt : Value Γ (subT ⟦ P ⟧ `true)) (pf : Value Γ (subT ⟦ P ⟧ `false)) (b : Value Γ `Bool) → Value Γ (subT ⟦ P ⟧ b) elimBool P pt pf `true = pt elimBool P pt pf `false = pf elimBool P pt pf (`neut b) = `neut (`elimBool P pt pf b) ---------------------------------------------------------------------- proj₁ : ∀{Γ A B} → Value Γ (`Σ A B) → Value Γ A proj₁ (a `, b) = a proj₁ (`neut ab) = `neut (`proj₁ ab) proj₂ : ∀{Γ A B} (ab : Value Γ (`Σ A B)) → Value Γ (subT B (proj₁ ab)) proj₂ (a `, b) = b proj₂ (`neut ab) = `neut (`proj₂ ab) ---------------------------------------------------------------------- des : ∀{Γ ℓ} {D : Value Γ (`Desc ℓ)} → Value Γ (`μ D) → Value Γ (⟦ D ⟧ᵈ (`μ D)) des (`con x) = x des (`neut x) = `neut (`des x) ---------------------------------------------------------------------- _$_ : ∀{Γ A B} → Value Γ (`Π A B) → (a : Value Γ A) → Value Γ (subT B a) `λ b $ a = subV b a `neut f $ a = `neut (f `$ a) ---------------------------------------------------------------------- data Term (Γ : Context) : Type Γ → Set eval : ∀{Γ A} → Term Γ A → Value Γ A data Term Γ where {- Type introduction -} `⊥ `⊤ `Bool `Type : ∀{ℓ} → Term Γ (`Type ℓ) `Π `Σ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (B : Term (Γ , ⟦ eval A ⟧) (`Type ℓ)) → Term Γ (`Type ℓ) `μ : ∀{ℓ} → Term Γ (`Desc ℓ) → Term Γ (`Type ℓ) `⟦_⟧ : ∀{ℓ} → Term Γ (`Type ℓ) → Term Γ (`Type (suc ℓ)) `⟦_⟧ᵈ : ∀{ℓ} → Term Γ (`Desc ℓ) → Term Γ (`Type ℓ) → Term Γ (`Type ℓ) {- Desc introduction -} `⊤ᵈ `Xᵈ : ∀{ℓ} → Term Γ (`Desc ℓ) `Πᵈ `Σᵈ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (D : Term (Γ , ⟦ eval A ⟧) (`Desc (suc ℓ))) → Term Γ (`Desc (suc ℓ)) {- Value introduction -} `tt : Term Γ `⊤ `true `false : Term Γ `Bool _`,_ : ∀{A B} (a : Term Γ A) (b : Term Γ (subT B (eval a))) → Term Γ (`Σ A B) `λ : ∀{A B} (b : Term (Γ , A) B) → Term Γ (`Π A B) `con : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Term Γ (⟦ D ⟧ᵈ (`μ D)) → Term Γ (`μ D) {- Value elimination -} `var : ∀{A} → Var Γ A → Term Γ A `if_`then_`else_ : ∀{C} (b : Term Γ `Bool) (c₁ c₂ : Term Γ C) → Term Γ C _`$_ : ∀{A B} (f : Term Γ (`Π A B)) (a : Term Γ A) → Term Γ (subT B (eval a)) `proj₁ : ∀{A B} → Term Γ (`Σ A B) → Term Γ A `proj₂ : ∀{A B} (ab : Term Γ (`Σ A B)) → Term Γ (subT B (proj₁ (eval ab))) `elim⊥ : ∀{A} → Term Γ `⊥ → Term Γ A `elimBool : ∀{ℓ} (P : Term (Γ , `Bool) (`Type ℓ)) (pt : Term Γ (subT ⟦ eval P ⟧ `true)) (pf : Term Γ (subT ⟦ eval P ⟧ `false)) (b : Term Γ `Bool) → Term Γ (subT ⟦ eval P ⟧ (eval b)) `des : ∀{ℓ} {D : Value Γ (`Desc ℓ)} → Term Γ (`μ D) → Term Γ (⟦ D ⟧ᵈ (`μ D)) ---------------------------------------------------------------------- {- Type introduction -} eval `⊥ = `⊥ eval `⊤ = `⊤ eval `Bool = `Bool eval `Type = `Type eval (`Π A B) = `Π (eval A) (eval B) eval (`Σ A B) = `Σ (eval A) (eval B) eval (`μ D) = `μ (eval D) eval `⟦ A ⟧ = `⟦ eval A ⟧ eval (`⟦ D ⟧ᵈ X) = `⟦ eval D ⟧ᵈ (eval X) {- Desc introduction -} eval `⊤ᵈ = `⊤ᵈ eval `Xᵈ = `Xᵈ eval (`Πᵈ A D) = `Πᵈ (eval A) (eval D) eval (`Σᵈ A D) = `Σᵈ (eval A) (eval D) {- Value introduction -} eval `tt = `tt eval `true = `true eval `false = `false eval (a `, b) = eval a `, eval b eval (`λ b) = `λ (eval b) eval (`con x) = `con (eval x) {- Value elimination -} eval (`var i) = `neut (`var i) eval (`if b `then c₁ `else c₂) = if eval b then eval c₁ else eval c₂ eval (f `$ a) = eval f $ eval a eval (`proj₁ ab) = proj₁ (eval ab) eval (`proj₂ ab) = proj₂ (eval ab) eval (`elim⊥ bot) = elim⊥ (eval bot) eval (`elimBool P pt pf b) = elimBool (eval P) (eval pt) (eval pf) (eval b) eval (`des x) = des (eval x) ----------------------------------------------------------------------
Add Mu eliminator to operational semantics.
Add Mu eliminator to operational semantics.
Agda
bsd-3-clause
spire/spire
c9a0e322742287bc9c2a9d3e057a06d23971c82c
Syntax/Language/Calculus.agda
Syntax/Language/Calculus.agda
module Syntax.Language.Calculus where -- Full description of a calculus in Plotkin style -- -- Users should supply -- - base types -- - constants -- - Δtype of base types -- - derivatives of constants open import Syntax.Type.Plotkin public open import Syntax.Term.Plotkin public open import Syntax.Context public open import Syntax.Context.Plotkin public open import Syntax.DeltaContext public record Calculus : Set₁ where constructor calculus-with field basetype : Set constant : Context {Type basetype} → Type basetype → Set Δtype : Type basetype → Type basetype Δconst : ∀ {Γ Σ τ} → (c : constant Σ τ) → Term {basetype} {constant} Γ (internalizeContext basetype (ΔContext′ (Type basetype) Δtype Σ) (Δtype τ)) open Calculus public type : Calculus → Set type L = Type (basetype L) context : Calculus → Set context L = Context {type L} term : (L : Calculus) → context L → type L → Set term L = Term {basetype L} {constant L}
module Syntax.Language.Calculus where -- Full description of a calculus in Plotkin style -- -- Users should supply -- - base types -- - constants -- - Δtype of base types -- - derivatives of constants open import Syntax.Type.Plotkin public open import Syntax.Term.Plotkin public open import Syntax.Context public open import Syntax.Context.Plotkin public open import Syntax.DeltaContext public record Calculus : Set₁ where constructor calculus-with field basetype : Set constant : Context {Type basetype} → Type basetype → Set Δtype : Type basetype → Type basetype Δconst : ∀ {Γ Σ τ} → (c : constant Σ τ) → Term {basetype} {constant} Γ (internalizeContext basetype (ΔContext′ (Type basetype) Δtype Σ) (Δtype τ)) type : Set type = Type basetype context : Set context = Context {type} term : context → type → Set term = Term {basetype} {constant} open Calculus public
Refactor Syntax/Language/Calculus.agda
Refactor Syntax/Language/Calculus.agda Move helpers inside record, so that if we open the record module with a record value: open Calculus some-specific-calculus We get all of the helper functions specialized to `some-specific-calculus`, like the fields. Old-commit-hash: dafe67297ca04cb5ab0369e8d1d7a8e2109a6c6d
Agda
mit
inc-lc/ilc-agda
456887b7b811ebbfcf89f5846339b3778163d905
Parametric/Change/Validity.agda
Parametric/Change/Validity.agda
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base -- Changes for Calculus Popl14 -- -- Contents -- - Mutually recursive concepts: ΔVal, validity. -- Under module Syntax, the corresponding concepts of -- ΔType and ΔContext reside in separate files. -- Now they have to be together due to mutual recursiveness. -- - `diff` and `apply` on semantic values of changes: -- they have to be here as well because they are mutually -- recursive with validity. -- - The lemma diff-is-valid: it has to be here because it is -- mutually recursive with `apply` -- - The lemma apply-diff: it is mutually recursive with `apply` -- and `diff` open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality open import Data.Product hiding (map) import Structure.Tuples as Tuples open Tuples import Base.Data.DependentList as DependentList open DependentList open import Relation.Unary using (_⊆_) record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field Change-base : Base → Set valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v) v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u --------------- -- Interface -- --------------- Change : Type → Set valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ infixl 6 apply-change diff-change -- as with + - in GHC.Num syntax apply-change τ v dv = v ⊞₍ τ ₎ dv syntax diff-change τ u v = u ⊟₍ τ ₎ v -- Lemma diff-is-valid R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v) -- Lemma apply-diff v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u -------------------- -- Implementation -- -------------------- -- (Change τ) is the set of changes of type τ. This set is -- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In -- particular, (Change (σ ⇒ τ)) is a function that accepts only -- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid -- changes. -- -- Change τ is the target of the denotational specification ⟦_⟧Δ. -- Detailed motivation: -- -- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex ValidChange : Type → Set ValidChange τ = Triple ⟦ τ ⟧ (λ _ → Change τ) (λ v dv → valid {τ} v dv) -- Change : Type → Set Change (base ι) = Change-base ι Change (σ ⇒ τ) = ValidChange σ → Change τ before : ValidChange ⊆ ⟦_⟧ before (cons v _ _) = v after : ValidChange ⊆ ⟦_⟧ after {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv -- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ n ⊞₍ base ι ₎ Δn = apply-change-base ι n Δn f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf (cons v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})) -- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ m ⊟₍ base ι ₎ n = diff-change-base ι m n g ⊟₍ σ ⇒ τ ₎ f = λ v → g (after v) ⊟₍ τ ₎ f (before v) -- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set valid {base ι} n Δn = valid-base {ι} n Δn valid {σ ⇒ τ} f Δf = ∀ (v : ValidChange σ) → valid {τ} (f (before v)) (Δf v) -- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv] × (f ⊞₍ σ ⇒ τ ₎ Δf) (after v) ≡ f (before v) ⊞₍ τ ₎ Δf 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 → let w′ = after {σ} 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 (before {σ} w)}) ⟩ v (before {σ} w) ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w ∎) where open ≡-Reasoning -- helpers nil-valid-change : ∀ τ → ⟦ τ ⟧ → ValidChange τ nil-valid-change τ v = cons v (v ⊟₍ τ ₎ v) (R[v,u-v] {τ} {v} {v}) -- syntactic sugar for implicit indices infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ _⊞_ {τ} v dv = v ⊞₍ τ ₎ dv _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ _⊟_ {τ} u v = u ⊟₍ τ ₎ v ------------------ -- Environments -- ------------------ open DependentList public using (∅; _•_) open Tuples public using (cons) ΔEnv : Context → Set ΔEnv = DependentList ValidChange ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ ignore = map (λ {τ} → before {τ}) update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ update = map (λ {τ} → after {τ})
import Parametric.Syntax.Type as Type import Parametric.Denotation.Value as Value module Parametric.Change.Validity {Base : Type.Structure} (⟦_⟧Base : Value.Structure Base) where open Type.Structure Base open Value.Structure Base ⟦_⟧Base -- Changes for Calculus Popl14 -- -- Contents -- - Mutually recursive concepts: ΔVal, validity. -- Under module Syntax, the corresponding concepts of -- ΔType and ΔContext reside in separate files. -- Now they have to be together due to mutual recursiveness. -- - `diff` and `apply` on semantic values of changes: -- they have to be here as well because they are mutually -- recursive with validity. -- - The lemma diff-is-valid: it has to be here because it is -- mutually recursive with `apply` -- - The lemma apply-diff: it is mutually recursive with `apply` -- and `diff` open import Base.Denotation.Notation public open import Relation.Binary.PropositionalEquality open import Postulate.Extensionality open import Data.Product hiding (map) import Structure.Tuples as Tuples open Tuples import Base.Data.DependentList as DependentList open DependentList open import Relation.Unary using (_⊆_) record Structure : Set₁ where ---------------- -- Parameters -- ---------------- field Change-base : Base → Set valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v) v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u --------------- -- Interface -- --------------- Change : Type → Set valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ infixl 6 apply-change diff-change -- as with + - in GHC.Num syntax apply-change τ v dv = v ⊞₍ τ ₎ dv syntax diff-change τ u v = u ⊟₍ τ ₎ v -- Lemma diff-is-valid R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v) -- Lemma apply-diff v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u -------------------- -- Implementation -- -------------------- -- (Change τ) is the set of changes of type τ. This set is -- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In -- particular, (Change (σ ⇒ τ)) is a function that accepts only -- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid -- changes. -- -- Change τ is the target of the denotational specification ⟦_⟧Δ. -- Detailed motivation: -- -- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex ValidChange : Type → Set ValidChange τ = Triple ⟦ τ ⟧ (λ _ → Change τ) (λ v dv → valid {τ} v dv) -- Change : Type → Set Change (base ι) = Change-base ι Change (σ ⇒ τ) = ValidChange σ → Change τ before : ValidChange ⊆ ⟦_⟧ before (cons v _ _) = v after : ValidChange ⊆ ⟦_⟧ after {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv nil-valid-change : ∀ τ → ⟦ τ ⟧ → ValidChange τ nil-valid-change τ v = cons v (v ⊟₍ τ ₎ v) (R[v,u-v] {τ} {v} {v}) -- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ n ⊞₍ base ι ₎ Δn = apply-change-base ι n Δn f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf (nil-valid-change σ v) -- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ m ⊟₍ base ι ₎ n = diff-change-base ι m n g ⊟₍ σ ⇒ τ ₎ f = λ v → g (after v) ⊟₍ τ ₎ f (before v) -- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set valid {base ι} n Δn = valid-base {ι} n Δn valid {σ ⇒ τ} f Δf = ∀ (v : ValidChange σ) → valid {τ} (f (before v)) (Δf v) -- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv] × (f ⊞₍ σ ⇒ τ ₎ Δf) (after v) ≡ f (before v) ⊞₍ τ ₎ Δf 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 → let w′ = after {σ} 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 (before {σ} w)}) ⟩ v (before {σ} w) ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w ∎) where open ≡-Reasoning -- syntactic sugar for implicit indices infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧ _⊞_ {τ} v dv = v ⊞₍ τ ₎ dv _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ _⊟_ {τ} u v = u ⊟₍ τ ₎ v ------------------ -- Environments -- ------------------ open DependentList public using (∅; _•_) open Tuples public using (cons) ΔEnv : Context → Set ΔEnv = DependentList ValidChange ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ ignore = map (λ {τ} → before {τ}) update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧ update = map (λ {τ} → after {τ})
Move nil-valid-change to use it in ⊞.
Move nil-valid-change to use it in ⊞. What I learned from this project: To implement apply, you need nil. Old-commit-hash: 3af743a00cc7e7cad69e57ec30364639c1b5e928
Agda
mit
inc-lc/ilc-agda
5d95a3734811ffcaf8c9508aca53f1f46a9739ee
FunUniverse/Fin/Op.agda
FunUniverse/Fin/Op.agda
open import Type open import Function open import Data.Nat.NP using (ℕ; module ℕ°) open import Data.Fin.NP as Fin using (Fin; inject+; raise; bound; free) open import Data.Vec.NP using (lookup) open import FunUniverse.Data open import FunUniverse.Core open import FunUniverse.Rewiring.Linear open import FunUniverse.Category open import FunUniverse.Category.Op open import FunUniverse.Fin module FunUniverse.Fin.Op where _ᶠ→_ : (i o : ℕ) → ★ i ᶠ→ o = Fin o → Fin i finOpFunU : FunUniverse ℕ finOpFunU = Bits-U , _ᶠ→_ finOpCat : Category _ᶠ→_ finOpCat = op finCat open FunUniverse finOpFunU open Cong-*1 _`→_ finOpLinRewiring : LinRewiring finOpFunU finOpLinRewiring = mk finOpCat first (λ {x} → swap {x}) (λ {x} → assoc {x}) id id <_×_> (λ {x} → second {x}) id id id id where -- NOTE that doing: -- swap {A} {B} rewrite ℕ°.+-comm A B = id -- would be totally wrong. swap : ∀ {A B} → (A `× B) `→ (B `× A) swap {A} {B} x with Fin.cmp B A x swap {A} {B} ._ | Fin.bound y = raise A y swap {A} {B} ._ | Fin.free y = inject+ B y -- Since the order of elements is preserved this is OK to rewrite assoc : ∀ {A B C} → ((A `× B) `× C) `→ (A `× (B `× C)) assoc {A} {B} {C} rewrite ℕ°.+-assoc A B C = id <_×_> : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D) <_×_> {A} {B} {C} {D} f g x with Fin.cmp C D x <_×_> {A} {B} {C} {D} f g ._ | Fin.bound y = inject+ B (f y) <_×_> {A} {B} {C} {D} f g ._ | Fin.free y = raise A (g y) idᶠ : ∀ {A} → A `→ A idᶠ = id first : ∀ {A B C} → (A `→ B) → (A `× C) `→ (B `× C) first f = < f × id > second : ∀ {A B C} → (B `→ C) → (A `× B) `→ (A `× C) second {A} f = < idᶠ {A} × f > finOpRewiring : Rewiring finOpFunU finOpRewiring = mk finOpLinRewiring (λ ()) dup (λ()) (λ f g → dup ⁏ < f × g >) (inject+ _) (λ {x} → raise x) cong-*1 (cong-*1 ∘ flip lookup) where open LinRewiring finOpLinRewiring using (<_×_>; _⁏_) dup : ∀ {A} → A `→ (A `× A) dup {A} x with Fin.cmp A A x dup ._ | Fin.bound y = y dup ._ | Fin.free y = y
Add FunUniverse/Fin/Op.agda
Add FunUniverse/Fin/Op.agda
Agda
bsd-3-clause
crypto-agda/crypto-agda
13c4b4c064a0a4ebee4104eb40461152750f44d5
NormalizationByEvaluation.agda
NormalizationByEvaluation.agda
module NormalizationByEvaluation where -- NORMALIZATION BY EVALUATION -- -- for the simply-typed λ calculus with base types open import Level hiding (Lift; lift) open import Data.Bool open import Relation.Binary hiding (_⇒_) -- The rest of this file is parametric in the set of base types. module Parametric (Base : Set) where -- SYNTAX OF TYPES -- -- We support function types and base types. data Type : Set where _⇒_ : (τ₁ τ₂ : Type) → Type base : (b : Base) → Type open import Syntactic.Contexts Type public -- DOMAIN CONSTRUCTION -- -- The meaning of types is defined using Kripke structures. record IsKripke {w ℓ₁ ℓ₂} {World : Set w} (_≈_ : Rel World ℓ₁) (_≤_ : Rel World ℓ₂) (_⊩⟦_⟧Base : World → Base → Set (ℓ₂ ⊔ w)) : Set (w ⊔ ℓ₁ ⊔ ℓ₂) where Lift : ∀ {A : Set} (_⊩⟦_⟧ : World → A → Set (ℓ₂ ⊔ w)) → Set (ℓ₂ ⊔ w) Lift _⊩⟦_⟧ = ∀ {a w₁ w₂} → w₁ ≤ w₂ → w₁ ⊩⟦ a ⟧ → w₂ ⊩⟦ a ⟧ field isPreorder : IsPreorder _≈_ _≤_ liftBase : Lift _⊩⟦_⟧Base open IsPreorder isPreorder public _⊩⟦_⟧Type : World → Type → Set (ℓ₂ ⊔ w) w₁ ⊩⟦ τ₁ ⇒ τ₂ ⟧Type = ∀ {w₂} → w₁ ≤ w₂ → w₂ ⊩⟦ τ₁ ⟧Type → w₂ ⊩⟦ τ₂ ⟧Type w₁ ⊩⟦ base b ⟧Type = w₁ ⊩⟦ b ⟧Base liftType : Lift _⊩⟦_⟧Type liftType {τ₁ ⇒ τ₂} w₁≤w₂ w₁⊩τ₁⇒τ₂ = λ w₂≤w₃ w₃⊩τ₁ → w₁⊩τ₁⇒τ₂ (trans w₁≤w₂ w₂≤w₃) w₃⊩τ₁ liftType {base b} w₁≤w₂ w₁⊩b = liftBase w₁≤w₂ w₁⊩b module _ (w : World) where open import Denotational.Environments Type (λ τ → w ⊩⟦ τ ⟧Type) public renaming (⟦_⟧Context to _⊩⟦_⟧Context; ⟦_⟧Var to _⊩⟦_⟧Var) liftContext : Lift {Context} _⊩⟦_⟧Context liftContext {∅} w₁≤w₂ w₁⊩Γ = ∅ liftContext {τ • Γ} w₁≤w₂ (w₁⊩τ • w₁⊩Γ) = liftType {τ} w₁≤w₂ w₁⊩τ • liftContext w₁≤w₂ w₁⊩Γ record Kripke w ℓ₁ ℓ₂ : Set (suc (w ⊔ ℓ₁ ⊔ ℓ₂)) where field World : Set w _≈_ : Rel World ℓ₁ _≤_ : Rel World ℓ₂ _⊩⟦_⟧Base : World → Base → Set (ℓ₂ ⊔ w) isKripke : IsKripke _≈_ _≤_ _⊩⟦_⟧Base open IsKripke isKripke public -- TERMS -- -- The syntax of terms is parametric in the set of constants. module Terms (Constant : Type → Set) where data Term : Context → Type → Set where abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂) app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂ var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ con : ∀ {Γ τ} → (c : Constant τ) → Term Γ τ weaken : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Term Γ₁ τ → Term Γ₂ τ weaken Γ₁≼Γ₂ (abs t) = abs (weaken (keep _ • Γ₁≼Γ₂) t) weaken Γ₁≼Γ₂ (app t₁ t₂) = app (weaken Γ₁≼Γ₂ t₁) (weaken Γ₁≼Γ₂ t₂) weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (con c) = con c -- SYMBOLIC EXECUTION and REIFICATION -- -- The Kripke structure of symbolic computation, using typing -- contexts as worlds. Base types are interpreted as -- terms. This is parametric in the set of available constants. module Symbolic (Constant : Type → Set) where open import Relation.Binary.PropositionalEquality open Terms Constant BaseTerm : Context → Base → Set BaseTerm Γ b = Term Γ (base b) isKripke : IsKripke _≡_ _≼_ BaseTerm isKripke = record { isPreorder = ≼-isPreorder; liftBase = weaken } symbolic : Kripke zero zero zero symbolic = record { World = Context; _≈_ = _≡_; _≤_ = _≼_; _⊩⟦_⟧Base = BaseTerm; isKripke = isKripke } open Kripke symbolic ↓ : ∀ {Γ} τ → Γ ⊩⟦ τ ⟧Type → Term Γ τ ↑ : ∀ {Γ} τ → Term Γ τ → Γ ⊩⟦ τ ⟧Type ↓ (τ₁ ⇒ τ₂) v = abs (↓ τ₂ (v (drop τ₁ • ≼-refl) (↑ τ₁ (var this)))) ↓ (base b) v = v ↑ (τ₁ ⇒ τ₂) t = λ Γ₁≼Γ₂ v → ↑ τ₂ (app (weaken Γ₁≼Γ₂ t) (↓ τ₁ v)) ↑ (base b) t = t Γ⊩Γ : ∀ {Γ} → Γ ⊩⟦ Γ ⟧Context Γ⊩Γ {∅} = ∅ Γ⊩Γ {τ • Γ} = ↑ τ (var this) • liftContext (drop τ • ≼-refl) Γ⊩Γ -- EVALUATION -- -- Evaluation is defined for every Kripke structure and every -- set of constants. We have to provide the interpretation of -- the constants in the choosen Kripke structure, though. module _ {w ℓ₁ ℓ₂} (K : Kripke w ℓ₁ ℓ₂) where open Kripke K module Evaluation (Constant : Type → Set) (_⊩⟦_⟧Constant : ∀ w {τ} → Constant τ → w ⊩⟦ τ ⟧Type) where open Terms Constant _⊩⟦_⟧Term_ : ∀ w {Γ τ} → Term Γ τ → w ⊩⟦ Γ ⟧Context → w ⊩⟦ τ ⟧Type w₁ ⊩⟦ abs t ⟧Term w₁⊩Γ = λ {w₂} w₁≤w₂ w₂⊩τ₁ → w₂ ⊩⟦ t ⟧Term (w₂⊩τ₁ • liftContext w₁≤w₂ w₁⊩Γ) w₁ ⊩⟦ app t₁ t₂ ⟧Term w₁⊩Γ = (w₁ ⊩⟦ t₁ ⟧Term w₁⊩Γ) refl (w₁ ⊩⟦ t₂ ⟧Term w₁⊩Γ) w₁ ⊩⟦ var x ⟧Term w₁⊩Γ = (w₁ ⊩⟦ x ⟧Var) w₁⊩Γ w₁ ⊩⟦ con c ⟧Term w₁⊩Γ = w₁ ⊩⟦ c ⟧Constant -- NORMALIZATION -- -- Given a set of constants and their interpretation in the -- Kripke structure for symbolic computation, we can normalize -- terms. module _ (Constant : Type → Set) where open Terms Constant open Symbolic Constant open Kripke symbolic module Normalization (_⊩⟦_⟧Constant : ∀ Γ {τ} → Constant τ → Γ ⊩⟦ τ ⟧Type) where open Evaluation symbolic Constant _⊩⟦_⟧Constant norm : ∀ {Γ τ} → Term Γ τ → Term Γ τ norm {Γ} {τ} t = ↓ τ (Γ ⊩⟦ t ⟧Term Γ⊩Γ)
Implement NbE for lambda calculus with base types (see #50).
Implement NbE for lambda calculus with base types (see #50). Old-commit-hash: 26dd0dee4e73552fe51a52999922812354a03188
Agda
mit
inc-lc/ilc-agda
7733c00a89f89d8d2ab941d498d88e4b5f59d2ef
adder.agda
adder.agda
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 renaming (`⊤ to `𝟙; `Bit to `𝟚) open FunOps funOps renaming (_∘_ to _`∘_) open import Solver.Linear module LinSolver = Syntaxᶠ linRewiring --iter : ∀ {n A B S} → (S `× A `→ S `× B) → S `× `Vec A n `→ `Vec B n iter : ∀ {n A B C D} → (D `× A `× B `→ D `× C) → D `× `Vec A n `× `Vec B n `→ `Vec C n iter {zero} F = <[]> iter {suc n} F = < id × < uncons × uncons > > ⁏ (helper ⁏ < F ⁏ swap × id > ⁏ assoc ⁏ < id × iter F >) ⁏ <∷> where open LinSolver helper = λ {A} {B} {D} {VA} {VB} → rewireᶠ (A ∷ B ∷ D ∷ VA ∷ VB ∷ []) (λ a b d va vb → (d , (a , va) , (b , vb)) ↦ (d , a , b) , (va , vb)) -- TODO reverses all over the places... switch to lsb first? adder : ∀ {n} → `Bits n `× `Bits n `→ `Bits n adder = <tt⁏ <0b> , < reverse × reverse > > ⁏ iter full-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 FunAdder agdaFunOps open FunOps agdaFunOps 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) mainIO : IO 𝟙 mainIO = ♯ putBits arg1 >> ♯ (♯ putStr " + " >> ♯ (♯ putBits arg2 >> ♯ (♯ putStr " = " >> ♯ putBits result))) main : IO.Primitive.IO 𝟙 main = IO.run mainIO
add a "simple" circuit adder capable of 11+31=42
add a "simple" circuit adder capable of 11+31=42
Agda
bsd-3-clause
crypto-agda/crypto-agda
72c4baef0b4762d0e68731f5561c74025613aeca
lib/Data/Nat/Primality/NP.agda
lib/Data/Nat/Primality/NP.agda
{-# OPTIONS --without-K #-} module Data.Nat.Primality.NP where open import Data.Nat.NP open import Data.Nat.Properties open import Data.Nat.Divisibility.NP open import Data.Nat.Primality public hiding (prime?) -- was slow open import Data.Fin.Dec open import Relation.Unary open import Relation.Binary.PropositionalEquality open import Relation.Nullary.Negation open import Relation.Nullary -- same code but imports the fast _∣?_ prime? : Decidable Prime prime? 0 = no λ() prime? 1 = no λ() prime? (suc (suc n)) = all? λ _ → ¬? (_ ∣? _) prime-≥2 : ∀ {n} → Prime n → n ≥ 2 prime-≥2 {0} () prime-≥2 {1} () prime-≥2 {2+ _} _ = s≤s (s≤s z≤n) prime-≢0 : ∀ {n} → Prime n → n ≢ 0 prime-≢0 {0} () _ prime-≢0 {1+ _} _ () prime-≢1 : ∀ {n} → Prime n → n ≢ 1 prime-≢1 {0} () _ prime-≢1 {1} () _ prime-≢1 {2+ _} _ ()
Add Nat.Primality.NP
Add Nat.Primality.NP
Agda
bsd-3-clause
crypto-agda/agda-nplib
a22358b5e658d944b72f4ecb719b036e857e9f95
lib/Data/One/Param/Binary.agda
lib/Data/One/Param/Binary.agda
{-# OPTIONS --without-K #-} open import Type.Param.Binary open import Data.Unit renaming (⊤ to 𝟙; tt to 0₁) module Data.One.Param.Binary where record ⟦𝟙⟧ (x₁ x₂ : 𝟙) : Set₀ where constructor ⟦0₁⟧
Add Data.One.Param.Binary
Add Data.One.Param.Binary
Agda
bsd-3-clause
np/agda-parametricity
c838b5d3d047273d8b46736db7c5e6353d903cbd
Crypto/Sig/Lamport/OneBit/Abstract.agda
Crypto/Sig/Lamport/OneBit/Abstract.agda
{-# OPTIONS --without-K #-} open import Type using (Type) open import Type.Eq using (Eq?; _==_; ≡⇒==; ==⇒≡) open import Data.Product.NP using (_×_; _,_; fst; snd; map) open import Data.Bit using (Bit; 0b; 1b; proj′; ✓→≡; ≡→✓) open import Relation.Binary.PropositionalEquality.NP open ≡-Reasoning module Crypto.Sig.Lamport.OneBit.Abstract (Secret : Type) (Digest : Type) (hash : Secret → Digest) (eq? : Eq? Digest) where SignKey = Secret × Secret Signature = Secret VerifKey = Digest × Digest module signkey (sk : SignKey) where skL = fst sk skH = snd sk vkL = hash skL vkH = hash skH -- Derive the public key by hashing each secret verif-key : SignKey → VerifKey verif-key sk = vkL , vkH where open signkey sk module verifkey (vk : VerifKey) where vkL = fst vk vkH = snd vk -- Key generation key-gen : SignKey → VerifKey × SignKey key-gen sk = vk , sk module key-gen where vk = verif-key sk -- Sign a single bit message sign : SignKey → Bit → Signature sign = proj′ -- Verify the signature of a single bit message verify : VerifKey → Bit → Signature → Bit verify vk b sig = proj′ vk b == hash sig verify-correct-sig : ∀ sk b → verify (verif-key sk) b (sign sk b) ≡ 1b verify-correct-sig sk 0b = ✓→≡ (≡⇒== (hash (fst sk) ∎)) verify-correct-sig sk 1b = ✓→≡ (≡⇒== (hash (snd sk) ∎)) import Algebra.FunctionProperties.Eq open Algebra.FunctionProperties.Eq.Implicits module Assuming-injectivity (hash-inj : Injective hash) where -- If one considers the hash function injective, then, so is verif-key verif-key-inj : ∀ {sk1 sk2} → verif-key sk1 ≡ verif-key sk2 → sk1 ≡ sk2 verif-key-inj {sk1} {sk2} e = ap₂ _,_ (hash-inj (ap fst e)) (hash-inj (ap snd e)) where module sk1 = signkey sk1 module sk2 = signkey sk2 -- Therefor under this assumption, different secret keys means different public keys verif-key-corrolary : ∀ {sk1 sk2} → sk1 ≢ sk2 → verif-key sk1 ≢ verif-key sk2 verif-key-corrolary {sk1} {sk2} sk≢ vk= = sk≢ (verif-key-inj vk=) -- If one considers the hash function injective then there is -- only one signing key which can sign correctly all (0 and 1) -- the messages signkey-uniqness : ∀ sk1 sk2 → (∀ b → verify (verif-key sk1) b (sign sk2 b) ≡ 1b) → sk1 ≡ sk2 signkey-uniqness sk1 sk2 e = ap₂ _,_ (hash-inj (==⇒≡ (≡→✓ (e 0b)))) (hash-inj (==⇒≡ (≡→✓ (e 1b)))) module Assuming-invertibility (unhash : Digest → Secret) (unhash-hash : ∀ x → unhash (hash x) ≡ x) (hash-unhash : ∀ x → hash (unhash x) ≡ x) where -- EXERCISES {- recover-signkey : VerifKey → SignKey recover-signkey = {!!} recover-signkey-correct : ∀ sk → recover-signkey (verif-key sk) ≡ sk recover-signkey-correct = {!!} forgesig : VerifKey → Bit → Signature forgesig = {!!} forgesig-correct : ∀ vk b → verify vk b (forgesig vk b) ≡ 1b forgesig-correct = {!!} -} -- -}
Add Crypto.Sig.Lamport.OneBit.Abstract
Add Crypto.Sig.Lamport.OneBit.Abstract
Agda
bsd-3-clause
crypto-agda/crypto-agda
d245f73509a8c7b6ddee9bdcd869c6144bf6d4c8
FunUniverse/Nand/Function.agda
FunUniverse/Nand/Function.agda
open import Data.Product module FunUniverse.Nand.Function where module FromNand {A} (nand : A × A → A) where open import FunUniverse.Agda open import FunUniverse.Nand (Abstract𝟚.funRewiring A) open FromNand nand public open import Data.Two module Nand𝟚 = FromNand (uncurry nand) open import Data.Tree.Binary module NandTree {A} = FromNand {BinTree A} (uncurry fork)
Add FunUniverse/Nand/Function.agda
Add FunUniverse/Nand/Function.agda
Agda
bsd-3-clause
crypto-agda/crypto-agda
cf87a127259d824c11c3fb4c235421022c1fc846
FLABloM/SemiRingRecord.agda
FLABloM/SemiRingRecord.agda
module SemiRingRecord where import Algebra.FunctionProperties using (LeftIdentity; RightIdentity) import Function using (_on_) import Relation.Binary.EqReasoning as EqReasoning import Relation.Binary.On using (isEquivalence) import Algebra.Structures using (module IsCommutativeMonoid; IsCommutativeMonoid) open import Relation.Binary using (module IsEquivalence; IsEquivalence; _Preserves₂_⟶_⟶_ ; Setoid) open import Data.Product renaming (_,_ to _,,_) -- just to avoid clash with other commas open import Preliminaries open import SemiNearRingRecord record SemiRing : Set₁ where field snr : SemiNearRing open SemiNearRing snr field 1s : s open Algebra.FunctionProperties _≃s_ using (LeftIdentity; RightIdentity) field ∙-identityl : LeftIdentity 1s _∙s_ ∙-identityr : RightIdentity 1s _∙s_
Add SemiRing
Add SemiRing
Agda
bsd-3-clause
DSLsofMath/DSLsofMath
ddc36dae24c43d057f2b8f67f617631d371130bf
lib/Control/Process/Type.agda
lib/Control/Process/Type.agda
{- On the one hand this module has nothing to do with JS, on the other hand our JS binding relies on this particular type definition. -} module Control.Process.Type where open import Data.String.Base using (String) data Proc (C {-channels-} : Set₀) (M {-messages-} : Set₀) : Set₀ where end : Proc C M send : (d : C) (m : M) (p : Proc C M) → Proc C M recv : (d : C) (p : M → Proc C M) → Proc C M spawn : (p : C → Proc C M) (q : C → Proc C M) → Proc C M error : (err : String) → Proc C M
Add Control.Process.Type
Add Control.Process.Type
Agda
bsd-3-clause
audreyt/agda-libjs,crypto-agda/agda-libjs
e0c6a8e5b17986de3b0562105b17b44fb570453b
FiniteField/FinImplem.agda
FiniteField/FinImplem.agda
-- Implements ℤq with Data.Fin open import Type open import Data.Nat open import Data.Fin.NP as Fin open import Relation.Binary.PropositionalEquality open import Explore.Type open import Explore.Explorable open Fin.Modulo renaming (sucmod to [suc]; sucmod-inj to [suc]-inj) module FiniteField.FinImplem (q-1 : ℕ) ([0]' [1]' : Fin (suc q-1)) where -- open Sum q : ℕ q = suc q-1 ℤq : ★ ℤq = Fin q μℤq : Explorable ℤq μℤq = {!μFinSuc q-1!} sumℤq : Sum ℤq sumℤq = sum μℤq [0] : ℤq [0] = [0]' [1] : ℤq [1] = [1]' [suc]-stable : SumStableUnder (sum μℤq) [suc] [suc]-stable = {!μFinSUI [suc] [suc]-inj!} _ℕ⊞_ : ℕ → ℤq → ℤq zero ℕ⊞ n = n suc m ℕ⊞ n = m ℕ⊞ ([suc] n) ℕ⊞-inj : ∀ n {x y} → n ℕ⊞ x ≡ n ℕ⊞ y → x ≡ y ℕ⊞-inj zero eq = eq ℕ⊞-inj (suc n) eq = [suc]-inj (ℕ⊞-inj n eq) ℕ⊞-stable : ∀ m → SumStableUnder (sum μℤq) (_ℕ⊞_ m) ℕ⊞-stable m = {!μFinSUI (_ℕ⊞_ m) (ℕ⊞-inj m)!} _⊞_ : ℤq → ℤq → ℤq m ⊞ n = Fin▹ℕ m ℕ⊞ n ⊞-inj : ∀ m {x y} → m ⊞ x ≡ m ⊞ y → x ≡ y ⊞-inj m = ℕ⊞-inj (Fin▹ℕ m) ⊞-stable : ∀ m → SumStableUnder (sum μℤq) (_⊞_ m) ⊞-stable m = {!μFinSUI (_⊞_ m) (⊞-inj m)!} _ℕ⊠_ : ℕ → ℤq → ℤq zero ℕ⊠ n = [0] suc m ℕ⊠ n = n ⊞ (m ℕ⊠ n) _⊠_ : ℤq → ℤq → ℤq m ⊠ n = Fin▹ℕ m ℕ⊠ n _[^]ℕ_ : ℤq → ℕ → ℤq m [^]ℕ zero = [1] m [^]ℕ suc n = m ⊠ (m [^]ℕ n) _[^]_ : ℤq → ℤq → ℤq m [^] n = m [^]ℕ (Fin▹ℕ n)
Add FiniteField/FinImplem.agda (unfinished)
Add FiniteField/FinImplem.agda (unfinished)
Agda
bsd-3-clause
crypto-agda/crypto-agda
80516389760b8a704422d85e8bf6d5b682422d9d
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.
Implement delta-observational equivalence (#244, #299)
Implement delta-observational equivalence (#244, #299) This proof shows that delta-observational equivalence is an equivalence relation. Known limitations of this commit: * it does not show that any function respects this relation; * it does not rephrase existing results; * this might better belong in Base.Change.Equivalence. Old-commit-hash: 46789e08b7a8bbd95738f9bf14b3ff8644151772
Agda
mit
inc-lc/ilc-agda
f58fb12a605924ada6a2cc1645dc725167a301ea
meaning.agda
meaning.agda
module meaning where open import Level record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where constructor meaning field Semantics : Set ℓ ⟦_⟧ : Syntax → Semantics open Meaning {{...}} public
Add missing file.
Add missing file. Forgot to add this file in commit ba1d8a8be0778b4d917c4d98c7db8f78f8f3cbbc. Old-commit-hash: 02fcec1035aa2e0190d1ba3fc335cecbade97c9b
Agda
mit
inc-lc/ilc-agda
14b8f096b525d8a615d01965c700b05596b5d603
New/Equivalence.agda
New/Equivalence.agda
module New.Equivalence where open import Relation.Binary.PropositionalEquality open import Function open import Data.Product open import Postulate.Extensionality open import New.Changes module _ {a} {A : Set a} {{CA : ChAlg 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 : Ch A) : 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 a ≙-setoid = record { Carrier = Ch A ; _≈_ = _≙_ ; isEquivalence = ≙-isEquivalence } ≙-syntax : ∀ {a} {A : Set a} {{CA : ChAlg A}} (x : A) (dx₁ dx₂ : Ch A) → Set a ≙-syntax x dx₁ dx₂ = _≙_ {x = x} dx₁ dx₂ syntax ≙-syntax x dx₁ dx₂ = dx₁ ≙[ x ] dx₂ module BinaryValid {A : Set} {{CA : ChAlg A}} {B : Set} {{CB : ChAlg B}} {C : Set} {{CC : ChAlg C}} (f : A → B → C) (df : A → Ch A → B → Ch B → Ch C) where open import Data.Product binary-valid-preserve-hp = ∀ a da (ada : valid a da) b db (bdb : valid b db) → valid (f a b) (df a da b db) binary-valid-eq-hp = ∀ a da (ada : valid a da) b db (bdb : valid b db) → (f ⊕ df) (a ⊕ da) (b ⊕ db) ≡ f a b ⊕ df a da b db binary-valid : binary-valid-preserve-hp → binary-valid-eq-hp → valid f df binary-valid ext-valid proof a da ada = (λ b db bdb → ext-valid a da ada b db bdb , lem2 b db bdb) , ext lem1 where lem1 : ∀ b → f (a ⊕ da) b ⊕ df (a ⊕ da) (nil (a ⊕ da)) b (nil b) ≡ f a b ⊕ df a da b (nil b) lem1 b rewrite sym (update-nil b) | proof a da ada b (nil b) (nil-valid b) | update-nil b = refl lem2 : ∀ b (db : Ch B) (bdb : valid b db) → f a (b ⊕ db) ⊕ df a da (b ⊕ db) (nil (b ⊕ db)) ≡ f a b ⊕ df a da b db lem2 b db bdb rewrite sym (proof a da ada (b ⊕ db) (nil (b ⊕ db)) (nil-valid (b ⊕ db))) | update-nil (b ⊕ db) = proof a da ada b db bdb module TernaryValid {A : Set} {{CA : ChAlg A}} {B : Set} {{CB : ChAlg B}} {C : Set} {{CC : ChAlg C}} {D : Set} {{CD : ChAlg D}} (f : A → B → C → D) (df : A → Ch A → B → Ch B → C → Ch C → Ch D) where ternary-valid-preserve-hp = ∀ a da (ada : valid a da) b db (bdb : valid b db) c dc (cdc : valid c dc) → valid (f a b c) (df a da b db c dc) -- These are explicit definitions only to speed up typechecking. CA→B→C→D : ChAlg (A → B → C → D) CA→B→C→D = funCA f⊕df = (_⊕_ {{CA→B→C→D}} f df) -- Already this definition takes a while to typecheck. ternary-valid-eq-hp = ∀ a (da : Ch A {{CA}}) (ada : valid {{CA}} a da) b (db : Ch B {{CB}}) (bdb : valid {{CB}} b db) c (dc : Ch C {{CC}}) (cdc : valid {{CC}} c dc) → f⊕df (a ⊕ da) (b ⊕ db) (c ⊕ dc) ≡ f a b c ⊕ df a da b db c dc ternary-valid : ternary-valid-preserve-hp → ternary-valid-eq-hp → valid f df ternary-valid ext-valid proof a da ada = binary-valid (λ b db bdb c dc cdc → ext-valid a da ada b db bdb c dc cdc) lem2 , ext (λ b → ext (lem1 b)) where open BinaryValid (f a) (df a da) lem1 : ∀ b c → f⊕df (a ⊕ da) b c ≡ (f a ⊕ df a da) b c lem1 b c rewrite sym (update-nil b) | sym (update-nil c) | proof a da ada b (nil b) (nil-valid b) c (nil c) (nil-valid c) | update-nil b | update-nil c = refl -- rewrite -- sym -- (proof -- (a ⊕ da) (nil (a ⊕ da)) (nil-valid (a ⊕ da)) -- b (nil b) (nil-valid b) -- c (nil c) (nil-valid c)) -- | update-nil (a ⊕ da) -- | update-nil b -- | update-nil c = {! !} lem2 : ∀ b db (bdb : valid b db) c dc (cdc : valid c dc) → (f a ⊕ df a da) (b ⊕ db) (c ⊕ dc) ≡ f a b c ⊕ df a da b db c dc lem2 b db bdb c dc cdc rewrite sym (proof a da ada (b ⊕ db) (nil (b ⊕ db)) (nil-valid (b ⊕ db)) (c ⊕ dc) (nil (c ⊕ dc)) (nil-valid (c ⊕ dc)) ) | update-nil (b ⊕ db) | update-nil (c ⊕ dc) = proof a da ada b db bdb c dc cdc
Add lemmas to prove function changes valid
Add lemmas to prove function changes valid
Agda
mit
inc-lc/ilc-agda
02b7dc1fcde2d5ea5944dc426ae4920689962b5d
experiments/NonregularTraversable.agda
experiments/NonregularTraversable.agda
-- A traversable functor that is not regular. open import Data.Nat open import Data.Vec open import Data.Product record Applicative : Set₁ where constructor appl field Map : Set → Set pure : ∀ {A} → A → Map A call : ∀ {A B} → Map (A → B) → Map A → Map B Traversable : (Set → Set) → Set₁ Traversable F = (G : Applicative) → ∀ {A B} → (A → Map G B) → F A → Map G (F B) where open Applicative infixr 10 _^_ -- exponents for naturals _^_ : ℕ → ℕ → ℕ m ^ zero = 1 m ^ suc n = m * (m ^ n) -- a regular container RegCon : Set → Set RegCon A = Σ ℕ (Vec A) -- a regular traversal regTrav : Traversable RegCon regTrav G f (0 , []) = pure G ((0 , [])) where open Applicative regTrav G f (suc n , x ∷ xs) = call G (call G (pure G (λ y nys → (suc (proj₁ nys) , y ∷ proj₂ nys))) (f x)) (regTrav G f (n , xs)) where open Applicative -- a nonregular language: 0, 10, 1100, 111000, .... nonreg : ℕ → ℕ nonreg n = let 2n = 2 ^ n in 2n * pred 2n -- a nonregular finitary container NonregCon : Set → Set NonregCon A = Σ ℕ (λ n → Vec A (nonreg n)) fmap : ∀ {A B} (G : Applicative) → (A → B) → let open Applicative in Map G A → Map G B fmap G f gx = call G (pure G f) gx where open Applicative -- a nonregular traversable functor nonregTrav : Traversable NonregCon nonregTrav G f (n , xs) = let gnnys = regTrav G f ((nonreg n , xs)) in fmap G (λ{(nn , ys) → (n , {!ys!})}) gnnys -- should be well-typed but is not
add example of non-regular traversable functor
add example of non-regular traversable functor
Agda
mit
yfcai/CREG
ccc91a1d485cba9d42a78ea4ff80597f0f7e7849
bytecode.agda
bytecode.agda
-- This module is an example of the use of the circuit building library. -- The point is to start with a tiny bytecode evaluator for bit operations. module bytecode where open import Function open import Data.Nat open import Data.Vec open import Data.Product.NP open import Data.Bits open import Data.Bool open import Algebra.FunctionProperties data I : ℕ → Set where `[] : I 1 `op₀ : ∀ {i} (b : Bit) (is : I (1 + i)) → I i `op₁ : ∀ {i} (op₁ : Op₁ Bit) (is : I (1 + i)) → I (1 + i) `op₂ : ∀ {i} (op₂ : Op₂ Bit) (is : I (1 + i)) → I (2 + i) eval : ∀ {i} → I i → Bits i → Bit eval `[] (x ∷ []) = x eval (`op₀ b is) st = eval is (b ∷ st) eval (`op₁ op₁ is) (b ∷ st) = eval is (op₁ b ∷ st) eval (`op₂ op₂ is) (b₀ ∷ b₁ ∷ st) = eval is (op₂ b₀ b₁ ∷ st) eval₀ : I 0 → Bit eval₀ i₀ = eval i₀ [] open import circuit module Ck {C} (cb : CircuitBuilder C) where open CircuitBuilder cb data IC : ℕ → Set where `[] : IC 1 `op : ∀ {i ki ko} (op : C ki ko) (is : IC (ko + i)) → IC (ki + i) module Eval (runC : RunCircuit C) where module CF = CircuitBuilder bitsFunCircuitBuilder ckIC : ∀ {i} → IC i → C i 1 ckIC `[] = idC ckIC (`op op is) = op *** idC >>> ckIC is evalIC : ∀ {i} → IC i → Bits i → Bit evalIC is bs = head (runC (ckIC is) bs) ck : ∀ {i} → I i → C i 1 ck `[] = idC ck (`op₀ b is) = bit b *** idC >>> ck is ck (`op₁ op₁ is) = unOp op₁ *** idC >>> ck is ck (`op₂ op₂ is) = binOp op₂ *** idC >>> ck is I2IC : ∀ {i} → I i → IC i I2IC `[] = `[] I2IC (`op₀ b is) = `op (bit b) (I2IC is) I2IC (`op₁ op₁ is) = `op (unOp op₁) (I2IC is) I2IC (`op₂ op₂ is) = `op (binOp op₂) (I2IC is) ck₀ : I 0 → C 0 1 ck₀ = ck module CheckCk where open Ck bitsFunCircuitBuilder open CircuitBuilder bitsFunCircuitBuilder open import Relation.Binary.PropositionalEquality ck-eval : ∀ {i} (is : I i) bs → ck is bs ≡ eval is bs ∷ [] ck-eval `[] (x ∷ []) = refl ck-eval (`op₀ b is) bs = ck-eval is (b ∷ bs) ck-eval (`op₁ op₁ is) (b ∷ bs) = ck-eval is (op₁ b ∷ bs) ck-eval (`op₂ op₂ is) (b₀ ∷ b₁ ∷ bs) = ck-eval is (op₂ b₀ b₁ ∷ bs)
Add bytecode.agda to experiment with circuits
Add bytecode.agda to experiment with circuits
Agda
bsd-3-clause
crypto-agda/crypto-agda
095f53c09c64b3abe9688067512e48f0db0bf52b
ZK/SigmaProtocol/KnownStatement.agda
ZK/SigmaProtocol/KnownStatement.agda
open import Type using (Type) open import Data.Bool.Base using (Bool; true) open import Relation.Binary.Core using (_≡_; _≢_) -- This module assumes the exact statement of the Zero-Knowledge proof to be -- known by all parties. module ZK.SigmaProtocol.KnownStatement (Commitment : Type) -- Prover commitments (Challenge : Type) -- Verifier challenges, picked at random (Response : Type) -- Prover responses/proofs to challenges (Randomness : Type) -- Prover's randomness (Witness : Type) -- Prover's witness (ValidWitness : Witness → Type) -- Valid witness for the statement where -- The prover is made of two steps. -- * First, send the commitment. -- * Second, receive a challenge and send back the response. -- -- Therefor one represents a prover as a pair (a record) made -- of a commitment (get-A) and a function (get-f) from challenges -- to responses. -- -- Note that one might wish the function get-f to be partial. -- -- This covers any kind of prover, either honest or dishonest -- Notice as well that such should depend on some randomness. -- So such a prover as type: SomeRandomness → Prover record Prover-Interaction : Type where constructor _,_ field get-A : Commitment get-f : Challenge → Response Prover : Type Prover = (r : Randomness)(w : Witness) → Prover-Interaction -- A transcript of the interaction between the prover and the -- verifier. -- -- Note that in the case of an interactive zero-knowledge -- proof the transcript alone does not prove anything. It can usually -- be simulated if one knows the challenge before the commitment. -- -- The only way to be convinced by an interactive prover is to be/trust the verifier, -- namely to receive the commitment first and send a randomly picked challenge -- thereafter. So the proof is not transferable. -- -- The other way is by making the zero-knowledge proof non-interactive, which forces the -- challenge to be a cryptographic hash of the commitment and the statement. -- See the StrongFiatShamir module and the corresponding paper for more details. record Transcript : Type where constructor mk field get-A : Commitment get-c : Challenge get-f : Response -- The actual behavior of an interactive verifier is as follows: -- * Given a statement -- * Receive a commitment from the prover -- * Send a randomly chosen challenge to the prover -- * Receive a response to that challenge -- Since the first two points are common to all Σ-protocols we describe -- the verifier behavior as a computable test on the transcript. Verifier : Type Verifier = (t : Transcript) → Bool -- To run the interaction, one only needs the prover and a randomly -- chosen challenge. The returned transcript is then checked by the -- verifier afterwards. run : Prover-Interaction → Challenge → Transcript run (A , f) c = mk A c (f c) -- A Σ-protocol is made of the code for honest prover -- and honest verifier. record Σ-Protocol : Type where constructor _,_ field prover : Prover verifier : Verifier Verified : (t : Transcript) → Type Verified t = verifier t ≡ true -- Correctness (also called completeness in some papers): a Σ-protocol is said -- to be correct if for any challenge, the (honest) verifier accepts what -- the (honest) prover returns. Correct : Σ-Protocol → Type Correct (prover , verifier) = ∀ r {w} c → ValidWitness w → verifier (run (prover r w) c) ≡ true -- A simulator takes a challenge and a response and returns a commitment. -- -- As defined next, a correct simulator picks the commitment such that -- the transcript is accepted by the verifier. -- -- Notice that generally, to make a valid looking transcript one should -- randomly pick the challenge and the response. Simulator : Type Simulator = (c : Challenge) (s : Response) → Commitment -- A correct simulator always convinces the honest verifier. Correct-simulator : Verifier → Simulator → Type Correct-simulator verifier simulator = ∀ c s → let A = simulator c s in verifier (mk A c s) ≡ true {- A Σ-protocol, more specifically a verifier which is equipped with a correct simulator is said to be Special Honest Verifier Zero Knowledge. This property is one of the condition to apply the Strong Fiat Shamir transformation. The Special part of Special-Honest-Verifier-Zero-Knowledge is covered by the simulator being correct. The Honest part is not covered yet, the definition is informally adapted from the paper "How not to prove yourself": Furthermore, if the challenge c and response s where chosen uniformly at random from their respective domains then the triple (A, c, s) is distributed identically to that of an execution between the (honest) prover and the (honest) verifier (run prover c). where A = simulator c s -} record Special-Honest-Verifier-Zero-Knowledge (Σ-proto : Σ-Protocol) : Type where open Σ-Protocol Σ-proto field simulator : Simulator correct-simulator : Correct-simulator verifier simulator -- A pair of "Transcript"s such that the commitment is shared -- and the challenges are different. record Transcript² (verifier : Verifier) : Type where constructor mk field -- The commitment is shared get-A : Commitment -- The challenges... get-c₀ get-c₁ : Challenge -- ...are different c₀≢c₁ : get-c₀ ≢ get-c₁ -- The responses/proofs are arbitrary get-f₀ get-f₁ : Response -- The two transcripts t₀ : Transcript t₀ = mk get-A get-c₀ get-f₀ t₁ : Transcript t₁ = mk get-A get-c₁ get-f₁ field -- The transcripts verify verify₀ : verifier t₀ ≡ true verify₁ : verifier t₁ ≡ true -- Remark: What if the underlying witnesses are different? Nothing is enforced here. -- At least in the case of the Schnorr protocol it does not matter and yield a unique -- witness. Extractor : Verifier → Type Extractor verifier = Transcript² verifier → Witness Extract-Valid-Witness : (verifier : Verifier) → Extractor verifier → Type Extract-Valid-Witness verifier extractor = ∀ t² → ValidWitness (extractor t²) -- A Σ-protocol, more specifically a verifier which is equipped with -- a correct extractor is said to have the Special Soundness property. -- This property is one of the condition to apply the Strong Fiat Shamir -- transformation. record Special-Soundness Σ-proto : Type where open Σ-Protocol Σ-proto field -- TODO Challenge should exp large wrt the security param extractor : Extractor verifier extract-valid-witness : Extract-Valid-Witness verifier extractor record Special-Σ-Protocol : Type where field Σ-protocol : Σ-Protocol correct : Correct Σ-protocol shvzk : Special-Honest-Verifier-Zero-Knowledge Σ-protocol ssound : Special-Soundness Σ-protocol open Σ-Protocol Σ-protocol public open Special-Honest-Verifier-Zero-Knowledge shvzk public open Special-Soundness ssound public
Add ZK.SigmaProtocol.KnowStatement
Add ZK.SigmaProtocol.KnowStatement
Agda
bsd-3-clause
crypto-agda/crypto-agda
6419e6a499edf075accabe01063360bc7c867ef8
formalization/agda/Spire/Examples/CompLev.agda
formalization/agda/Spire/Examples/CompLev.agda
{-# OPTIONS --type-in-type #-} open import Data.Unit open import Data.Product hiding ( curry ; uncurry ) open import Data.List hiding ( concat ) open import Data.String open import Relation.Binary.PropositionalEquality open import Function module Spire.Examples.CompLev where ---------------------------------------------------------------------- Label : Set Label = String Enum : Set Enum = List Label data Tag : Enum → Set where here : ∀{l E} → Tag (l ∷ E) there : ∀{l E} → Tag E → Tag (l ∷ E) Branches : (E : Enum) (P : Tag E → Set) → Set Branches [] P = ⊤ Branches (l ∷ E) P = P here × Branches E (λ t → P (there t)) case : {E : Enum} (P : Tag E → Set) (cs : Branches E P) (t : Tag E) → P t case P (c , cs) here = c case P (c , cs) (there t) = case (λ t → P (there t)) cs t ---------------------------------------------------------------------- data Tel : Set where End : Tel Arg : (A : Set) (B : A → Tel) → Tel Elᵀ : Tel → Set Elᵀ End = ⊤ Elᵀ (Arg A B) = Σ A (λ a → Elᵀ (B a)) ---------------------------------------------------------------------- data Desc (I : Set) : Set where End : Desc I Rec : (i : I) (D : Desc I) → Desc I RecFun : (A : Set) (B : A → I) (D : Desc I) → Desc I Arg : (A : Set) (B : A → Desc I) → Desc I ---------------------------------------------------------------------- ISet : Set → Set ISet I = I → Set Elᴰ : {I : Set} (D : Desc I) → ISet I → Set Elᴰ End X = ⊤ Elᴰ (Rec j D) X = X j × Elᴰ D X Elᴰ (RecFun A B D) X = ((a : A) → X (B a)) × Elᴰ D X Elᴰ (Arg A B) X = Σ A (λ a → Elᴰ (B a) X) Hyps : {I : Set} (D : Desc I) (X : ISet I) (P : (i : I) → X i → Set) (xs : Elᴰ D X) → Set Hyps End X P tt = ⊤ Hyps (Rec i D) X P (x , xs) = P i x × Hyps D X P xs Hyps (RecFun A B D) X P (f , xs) = ((a : A) → P (B a) (f a)) × Hyps D X P xs Hyps (Arg A B) X P (a , xs) = Hyps (B a) X P xs ---------------------------------------------------------------------- data μ {I : Set} (R : I → Desc I) (i : I) : Set where init : Elᴰ (R i) (μ R) → μ R i ---------------------------------------------------------------------- ind : {I : Set} (R : I → Desc I) (M : (i : I) → μ R i → Set) (α : ∀ i (xs : Elᴰ (R i) (μ R)) (ihs : Hyps (R i) (μ R) M xs) → M i (init xs)) (i : I) (x : μ R i) → M i x prove : {I : Set} (D : Desc I) (R : I → Desc I) (M : (i : I) → μ R i → Set) (α : ∀ i (xs : Elᴰ (R i) (μ R)) (ihs : Hyps (R i) (μ R) M xs) → M i (init xs)) (xs : Elᴰ D (μ R)) → Hyps D (μ R) M xs ind R M α i (init xs) = α i xs (prove (R i) R M α xs) prove End R M α tt = tt prove (Rec j D) R M α (x , xs) = ind R M α j x , prove D R M α xs prove (RecFun A B D) R M α (f , xs) = (λ a → ind R M α (B a) (f a)) , prove D R M α xs prove (Arg A B) R M α (a , xs) = prove (B a) R M α xs ---------------------------------------------------------------------- DescE : Enum DescE = "End" ∷ "Rec" ∷ "Arg" ∷ [] DescT : Set DescT = Tag DescE -- EndT : DescT pattern EndT = here -- RecT : DescT pattern RecT = there here -- ArgT : DescT pattern ArgT = there (there here) DescR : Set → ⊤ → Desc ⊤ DescR I tt = Arg (Tag DescE) (case (λ _ → Desc ⊤) ( (Arg I λ i → End) , (Arg I λ i → Rec tt End) , (Arg Set λ A → RecFun A (λ a → tt) End) , tt )) `Desc : (I : Set) → Set `Desc I = μ (DescR I) tt -- `End : {I : Set} (i : I) → `Desc I pattern `End i = init (EndT , i , tt) -- `Rec : {I : Set} (i : I) (D : `Desc I) → `Desc I pattern `Rec i D = init (RecT , i , D , tt) -- `Arg : {I : Set} (A : Set) (B : A → `Desc I) → `Desc I pattern `Arg A B = init (ArgT , A , B , tt) ---------------------------------------------------------------------- FixI : (I : Set) → Set FixI I = I × `Desc I FixR' : (I : Set) (D : `Desc I) → I → `Desc I → Desc (FixI I) FixR' I D i (`End j) = Arg (j ≡ i) λ q → End FixR' I D i (`Rec j E) = Rec (j , D) (FixR' I D i E) FixR' I D i (`Arg A B) = Arg A λ a → FixR' I D i (B a) FixR' I D i (init (there (there (there ())) , xs)) FixR : (I : Set) (D : `Desc I) → FixI I → Desc (FixI I) FixR I D E,i = FixR' I D (proj₁ E,i) (proj₂ E,i) `Fix : (I : Set) (D : `Desc I) (i : I) → Set `Fix I D i = μ (FixR I D) (i , D) ----------------------------------------------------------------------
Fix internalized as a computational description.
Fix internalized as a computational description. The idea is that the core could have computatial descriptions, which do not mention things like which arguments are implicit, and Desc can be internalized as a description like the current primitive (non-computational) description that also mentions which arguments are implicit. Then you can define the fixpoint of that description as a computational description, parameterized by (I : Set) (D : DescI) and indexed by (i : I) (E : Desc I). It's cool that you can do this but I think it's one step too far in the direction of making the core type theory simple in exchange for more complicated derivable programs. If anything, defining `Desc, making an interpretation function for it, along with a toDesc function might be okay. But, again I think defining `Fix is probably not worthwhile.
Agda
bsd-3-clause
spire/spire
3be44d190ddb71b95417f9028965e046e7938081
agda/Language/Greek.agda
agda/Language/Greek.agda
module Language.Greek where data Maybe A : Set where none : Maybe A just : A → Maybe A data Unicode : Set where Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω α β γ δ ε ζ η θ ι κ λ′ μ ν ξ ο π ρ σ ς τ υ φ χ ψ ω : Unicode grave acute diaeresis smooth rough circumflex iotaSubscript : Unicode postulate Char : Set {-# BUILTIN CHAR Char #-} {-# COMPILED_TYPE Char Char #-} charToUnicode : Char -> Maybe Unicode charToUnicode 'Α' = just Α charToUnicode 'Β' = just Β charToUnicode 'Γ' = just Γ charToUnicode 'Δ' = just Δ charToUnicode 'Ε' = just Ε charToUnicode 'Ζ' = just Ζ charToUnicode 'Η' = just Η charToUnicode 'Θ' = just Θ charToUnicode 'Ι' = just Ι charToUnicode 'Κ' = just Κ charToUnicode 'Λ' = just Λ charToUnicode 'Μ' = just Μ charToUnicode 'Ν' = just Ν charToUnicode 'Ξ' = just Ξ charToUnicode 'Ο' = just Ο charToUnicode 'Π' = just Π charToUnicode 'Ρ' = just Ρ charToUnicode 'Σ' = just Σ charToUnicode 'Τ' = just Τ charToUnicode 'Υ' = just Υ charToUnicode 'Φ' = just Φ charToUnicode 'Χ' = just Χ charToUnicode 'Ψ' = just Ψ charToUnicode 'Ω' = just Ω charToUnicode 'α' = just α charToUnicode 'β' = just β charToUnicode 'γ' = just γ charToUnicode 'δ' = just δ charToUnicode 'ε' = just ε charToUnicode 'ζ' = just ζ charToUnicode 'η' = just η charToUnicode 'θ' = just θ charToUnicode 'ι' = just ι charToUnicode 'κ' = just κ charToUnicode 'λ' = just λ′ charToUnicode 'μ' = just μ charToUnicode 'ν' = just ν charToUnicode 'ξ' = just ξ charToUnicode 'ο' = just ο charToUnicode 'π' = just π charToUnicode 'ρ' = just ρ charToUnicode 'ς' = just ς charToUnicode 'σ' = just σ charToUnicode 'τ' = just τ charToUnicode 'υ' = just υ charToUnicode 'φ' = just φ charToUnicode 'χ' = just χ charToUnicode 'ψ' = just ψ charToUnicode 'ω' = just ω charToUnicode '\x0300' = just grave -- COMBINING GRAVE ACCENT charToUnicode '\x0301' = just acute -- COMBINING ACUTE ACCENT charToUnicode '\x0308' = just diaeresis -- COMBINING DIAERESIS charToUnicode '\x0313' = just smooth -- COMBINING COMMA ABOVE charToUnicode '\x0314' = just rough -- COMBINING REVERSED COMMA ABOVE charToUnicode '\x0342' = just circumflex -- COMBINING GREEK PERISPOMENI charToUnicode '\x0345' = just iotaSubscript -- COMBINING GREEK YPOGEGRAMMENI charToUnicode _ = none data UnicodeCategory : Set where letter mark : UnicodeCategory unicodeToCategory : Unicode → UnicodeCategory unicodeToCategory Α = letter unicodeToCategory Β = letter unicodeToCategory Γ = letter unicodeToCategory Δ = letter unicodeToCategory Ε = letter unicodeToCategory Ζ = letter unicodeToCategory Η = letter unicodeToCategory Θ = letter unicodeToCategory Ι = letter unicodeToCategory Κ = letter unicodeToCategory Λ = letter unicodeToCategory Μ = letter unicodeToCategory Ν = letter unicodeToCategory Ξ = letter unicodeToCategory Ο = letter unicodeToCategory Π = letter unicodeToCategory Ρ = letter unicodeToCategory Σ = letter unicodeToCategory Τ = letter unicodeToCategory Υ = letter unicodeToCategory Φ = letter unicodeToCategory Χ = letter unicodeToCategory Ψ = letter unicodeToCategory Ω = letter unicodeToCategory α = letter unicodeToCategory β = letter unicodeToCategory γ = letter unicodeToCategory δ = letter unicodeToCategory ε = letter unicodeToCategory ζ = letter unicodeToCategory η = letter unicodeToCategory θ = letter unicodeToCategory ι = letter unicodeToCategory κ = letter unicodeToCategory λ′ = letter unicodeToCategory μ = letter unicodeToCategory ν = letter unicodeToCategory ξ = letter unicodeToCategory ο = letter unicodeToCategory π = letter unicodeToCategory ρ = letter unicodeToCategory σ = letter unicodeToCategory ς = letter unicodeToCategory τ = letter unicodeToCategory υ = letter unicodeToCategory φ = letter unicodeToCategory χ = letter unicodeToCategory ψ = letter unicodeToCategory ω = letter unicodeToCategory grave = mark unicodeToCategory acute = mark unicodeToCategory diaeresis = mark unicodeToCategory smooth = mark unicodeToCategory rough = mark unicodeToCategory circumflex = mark unicodeToCategory iotaSubscript = mark data List A : Set where [] : List A _∷_ : A → List A -> List A infixr 6 _∷_ foldr : {A B : Set} → (A → B → B) → B → List A → B foldr f y [] = y foldr f y (x ∷ xs) = f x (foldr f y xs) data _And_ : (A : Set) → (B : Set) → Set₁ where _and_ : {A B : Set} → A → B → A And B data ConcreteLetter : Set where Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω α β γ δ ε ζ η θ ι κ λ′ μ ν ξ ο π ρ σ ς τ υ φ χ ψ ω : ConcreteLetter data ConcreteMark : Set where grave acute diaeresis smooth rough circumflex iotaSubscript : ConcreteMark
Add an initial start for an Agda model.
Add an initial start for an Agda model.
Agda
mit
scott-fleischman/greek-grammar,scott-fleischman/greek-grammar
8eb3bf726fb7d12e74f6c4c5fd3b4b9b641cfefd
lib/Algebra/Nearring.agda
lib/Algebra/Nearring.agda
{-# OPTIONS --without-K #-} open import Relation.Binary.PropositionalEquality.NP import Algebra.FunctionProperties.Eq open Algebra.FunctionProperties.Eq.Implicits open import Function.Extensionality open import Data.Product.NP open import Data.Nat.NP using (ℕ; zero; fold) renaming (suc to 1+) open import Data.Integer using (ℤ; +_; -[1+_]) open import Algebra.Raw open import Algebra.Monoid open import Algebra.Monoid.Commutative open import Algebra.Group open import Algebra.Group.Abelian open import HoTT module Algebra.Nearring where record Nearring+1-Struct {ℓ} {A : Set ℓ} (rng-ops : Ring-Ops A) : Set ℓ where open Ring-Ops rng-ops open ≡-Reasoning field +-grp-struct : Group-Struct +-grp-ops *-mon-struct : Monoid-Struct *-mon-ops *-+-distrʳ : _*_ DistributesOverʳ _+_ open Additive-Group-Struct +-grp-struct public -- 0−-involutive : Involutive 0−_ -- cancels-+-left : LeftCancel _+_ -- cancels-+-right : RightCancel _+_ -- ... open Multiplicative-Monoid-Struct *-mon-struct public open From-Ring-Ops rng-ops open From-+Group-1*Identity-DistributesOverʳ +-assoc 0+-identity +0-identity (snd 0−-inverse) 1*-identity *-+-distrʳ public +-grp : Group A +-grp = +-grp-ops , +-grp-struct module +-Grp = Group +-grp *-mon : Monoid A *-mon = *-mon-ops , *-mon-struct module *-Mon = Monoid *-mon record Nearring+1 {ℓ} (A : Set ℓ) : Set ℓ where field rng-ops : Ring-Ops A nearring+1-struct : Nearring+1-Struct rng-ops open Ring-Ops rng-ops public open Nearring+1-Struct nearring+1-struct public -- -} -- -} -- -} -- -} -- -}
Add Nearring+1
Add Nearring+1
Agda
bsd-3-clause
crypto-agda/agda-nplib
b5129ee5c0b5ffae12387ee403ca85c9cabd2fb1
lib/FFI/JS/BigI.agda
lib/FFI/JS/BigI.agda
open import FFI.JS hiding (toString) module FFI.JS.BigI where abstract BigI : Set BigI = JSValue BigI▹JSValue : BigI → JSValue BigI▹JSValue x = x unsafe-JSValue▹BigI : BigI → JSValue unsafe-JSValue▹BigI x = x postulate bigI : (x base : String) → BigI add : (x y : BigI) → BigI multiply : (x y : BigI) → BigI mod : (x y : BigI) → BigI modPow : (this e m : BigI) → BigI modInv : (this m : BigI) → BigI equals : (x y : BigI) → Bool toString : (x : BigI) → String fromHex : String → BigI toHex : BigI → String {-# COMPILED_JS bigI function(x) { return function (y) { return require("bigi")(x,y); }; } #-} {-# COMPILED_JS add function(x) { return function (y) { return x.add(y); }; } #-} {-# COMPILED_JS multiply function(x) { return function (y) { return x.multiply(y); }; } #-} {-# COMPILED_JS mod function(x) { return function (y) { return x.mod(y); }; } #-} {-# COMPILED_JS modPow function(x) { return function (y) { return function (z) { return x.modPow(y,z); }; }; } #-} {-# COMPILED_JS modInv function(x) { return function (y) { return x.modInverse(y); }; } #-} {-# COMPILED_JS equals function(x) { return function (y) { return x.equals(y); }; } #-} {-# COMPILED_JS toString function(x) { return x.toString(); } #-} {-# COMPILED_JS fromHex require("bigi").fromHex #-} {-# COMPILED_JS toHex function(x) { return x.toHex(); } #-} 0I 1I 2I : BigI 0I = bigI "0" "10" 1I = bigI "1" "10" 2I = bigI "2" "10"
Add FFI.JS.BigI
Add FFI.JS.BigI
Agda
bsd-3-clause
crypto-agda/agda-libjs,audreyt/agda-libjs
27acbf899d88340ad1d03042fa744d1a3b783e0c
Base/Change/Sums.agda
Base/Change/Sums.agda
module Base.Change.Sums where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Base.Change.Equivalence open import Postulate.Extensionality open import Data.Sum module SumChanges ℓ (X Y : Set ℓ) {{CX : ChangeAlgebra ℓ X}} {{CY : ChangeAlgebra ℓ Y}} where open ≡-Reasoning data SumChange : X ⊎ Y → Set ℓ where ch₁ : ∀ {x} → (dx : Δ x) → SumChange (inj₁ x) rp₁₂ : ∀ {x} → (y : Y) → SumChange (inj₁ x) ch₂ : ∀ {y} → (dy : Δ y) → SumChange (inj₂ y) rp₂₁ : ∀ {y} → (x : X) → SumChange (inj₂ y) _⊕_ : (v : X ⊎ Y) → SumChange v → X ⊎ Y inj₁ x ⊕ ch₁ dx = inj₁ (x ⊞ dx) inj₂ y ⊕ ch₂ dy = inj₂ (y ⊞ dy) inj₁ x ⊕ rp₁₂ y = inj₂ y inj₂ y ⊕ rp₂₁ x = inj₁ x _⊝_ : ∀ (v₂ v₁ : X ⊎ Y) → SumChange v₁ inj₁ x₂ ⊝ inj₁ x₁ = ch₁ (x₂ ⊟ x₁) inj₂ y₂ ⊝ inj₂ y₁ = ch₂ (y₂ ⊟ y₁) inj₂ y₂ ⊝ inj₁ x₁ = rp₁₂ y₂ inj₁ x₂ ⊝ inj₂ y₁ = rp₂₁ x₂ s-nil : (v : X ⊎ Y) → SumChange v s-nil (inj₁ x) = ch₁ (nil x) s-nil (inj₂ y) = ch₂ (nil y) s-update-diff : ∀ (u v : X ⊎ Y) → v ⊕ (u ⊝ v) ≡ u s-update-diff (inj₁ x₂) (inj₁ x₁) = cong inj₁ (update-diff x₂ x₁) s-update-diff (inj₂ y₂) (inj₂ y₁) = cong inj₂ (update-diff y₂ y₁) s-update-diff (inj₁ x₂) (inj₂ y₁) = refl s-update-diff (inj₂ y₂) (inj₁ x₁) = refl s-update-nil : ∀ v → v ⊕ (s-nil v) ≡ v s-update-nil (inj₁ x) = cong inj₁ (update-nil x) s-update-nil (inj₂ y) = cong inj₂ (update-nil y) changeAlgebra : ChangeAlgebra ℓ (X ⊎ Y) changeAlgebra = record { Change = SumChange ; update = _⊕_ ; diff = _⊝_ ; nil = s-nil ; isChangeAlgebra = record { update-diff = s-update-diff ; update-nil = s-update-nil } } inj₁′ : (x : X) → (dx : Δ x) → Δ (inj₁ x) inj₁′ x dx = ch₁ dx inj₁′Derivative : Derivative inj₁ inj₁′ inj₁′Derivative x dx = refl inj₂′ : (y : Y) → (dy : Δ y) → Δ (inj₂ y) inj₂′ y dy = ch₂ dy inj₂′Derivative : Derivative inj₂ inj₂′ inj₂′Derivative y dy = refl -- Elimination form for sums. This is a less dependently-typed version of -- [_,_]. match : ∀ {Z : Set ℓ} → (X → Z) → (Y → Z) → X ⊎ Y → Z match f g (inj₁ x) = f x match f g (inj₂ y) = g y module _ {Z : Set ℓ} {{CZ : ChangeAlgebra ℓ Z}} where X→Z = FunctionChanges.changeAlgebra X Z --module ΔX→Z = FunctionChanges X Z {{CX}} {{CZ}} Y→Z = FunctionChanges.changeAlgebra Y Z --module ΔY→Z = FunctionChanges Y Z {{CY}} {{CZ}} X⊎Y→Z = FunctionChanges.changeAlgebra (X ⊎ Y) Z Y→Z→X⊎Y→Z = FunctionChanges.changeAlgebra (Y → Z) (X ⊎ Y → Z) open FunctionChanges using (apply; correct) match′₀-realizer : (f : X → Z) → Δ f → (g : Y → Z) → Δ g → (s : X ⊎ Y) → Δ s → Δ (match f g s) match′₀-realizer f df g dg (inj₁ x) (ch₁ dx) = apply df x dx match′₀-realizer f df g dg (inj₁ x) (rp₁₂ y) = ((g ⊞ dg) y) ⊟ (f x) match′₀-realizer f df g dg (inj₂ y) (rp₂₁ x) = ((f ⊞ df) x) ⊟ (g y) match′₀-realizer f df g dg (inj₂ y) (ch₂ dy) = apply dg y dy match′₀-realizer-correct : (f : X → Z) → (df : Δ f) → (g : Y → Z) → (dg : Δ g) → (s : X ⊎ Y) → (ds : Δ s) → match f g (s ⊕ ds) ⊞ match′₀-realizer f df g dg (s ⊕ ds) (nil (s ⊕ ds)) ≡ match f g s ⊞ match′₀-realizer f df g dg s ds match′₀-realizer-correct f df g dg (inj₁ x) (ch₁ dx) = correct df x dx match′₀-realizer-correct f df g dg (inj₂ y) (ch₂ dy) = correct dg y dy match′₀-realizer-correct f df g dg (inj₁ x) (rp₁₂ y) rewrite update-diff ((g ⊞ dg) y) (f x) = refl match′₀-realizer-correct f df g dg (inj₂ y) (rp₂₁ x) rewrite update-diff ((f ⊞ df) x) (g y) = refl match′₀ : (f : X → Z) → Δ f → (g : Y → Z) → Δ g → Δ (match f g) match′₀ f df g dg = record { apply = match′₀-realizer f df g dg ; correct = match′₀-realizer-correct f df g dg } match′-realizer-correct-body : (f : X → Z) → (df : Δ f) → (g : Y → Z) → (dg : Δ g) → (s : X ⊎ Y) → (match f (g ⊞ dg) ⊞ match′₀ f df (g ⊞ dg) (nil (g ⊞ dg))) s ≡ (match f g ⊞ match′₀ f df g dg) s match′-realizer-correct-body f df g dg (inj₁ x) = refl -- refl doesn't work here. That seems a *huge* bad smell. However, that's simply because we're only updating g, not f match′-realizer-correct-body f df g dg (inj₂ y) rewrite update-nil y = update-diff (g y ⊞ apply dg y (nil y)) (g y ⊞ apply dg y (nil y)) match′-realizer-correct : (f : X → Z) → (df : Δ f) → (g : Y → Z) → (dg : Δ g) → (match f (g ⊞ dg)) ⊞ match′₀ f df (g ⊞ dg) (nil (g ⊞ dg)) ≡ match f g ⊞ match′₀ f df g dg match′-realizer-correct f df g dg = ext (match′-realizer-correct-body f df g dg) match′ : (f : X → Z) → Δ f → Δ (match f) match′ f df = record { apply = λ g dg → match′₀ f df g dg ; correct = match′-realizer-correct f df }
Implement change structure for sum types
Implement change structure for sum types
Agda
mit
inc-lc/ilc-agda
98776944ea4983b6faeeced4116bc7798c5e3ebc
Crypto/Sig/Lamport.agda
Crypto/Sig/Lamport.agda
{-# OPTIONS --without-K #-} open import Function using (_∘_) open import Data.Nat.NP hiding (_==_) open import Data.Product.NP hiding (map) open import Data.Bit hiding (_==_) open import Data.Bits open import Data.Bits.Properties open import Data.Vec.NP open import Relation.Binary.PropositionalEquality.NP import Crypto.Sig.LamportOneBit module Crypto.Sig.Lamport (#digest : ℕ) (#seed : ℕ) (#secret : ℕ) (#message : ℕ) (hash-secret : Bits #secret → Bits #digest) (seed-expansion : Bits #seed → Bits (#message * 2* #secret)) where module OTS1 = Crypto.Sig.LamportOneBit #secret #digest hash-secret H = hash-secret #seckey1 = 2* #secret #pubkey1 = 2* #digest #signature1 = #secret #seckey = #message * #seckey1 #pubkey = #message * #pubkey1 #signature = #message * #signature1 Digest = Bits #digest Seed = Bits #seed Secret = Bits #secret SignKey = Bits #seckey Message = Bits #message Signature = Bits #signature VerifKey = Bits #pubkey verif-key : SignKey → VerifKey verif-key = map* #message OTS1.verif-key module verifkey (vk : VerifKey) where vk1s = group #message #pubkey1 vk module signkey (sk : SignKey) where sk1s = group #message #seckey1 sk vk = verif-key sk open verifkey vk public module signature (sig : Signature) where sig1s = group #message #signature1 sig key-gen : Seed → VerifKey × SignKey key-gen s = vk , sk module key-gen where sk = seed-expansion s vk = verif-key sk sign : SignKey → Message → Signature sign sk m = sig module sign where open signkey sk public sig1s = map OTS1.sign sk1s ⊛ m sig = concat sig1s verify : VerifKey → Message → Signature → Bit verify vk m sig = and (map OTS1.verify vk1s ⊛ m ⊛ sig1s) module verify where open verifkey vk public open signature sig public verify-correct-sig : ∀ sk m → verify (verif-key sk) m (sign sk m) ≡ 1b verify-correct-sig = lemma where module lemma {#m} sk b m where skL = take #seckey1 sk skH = drop #seckey1 sk vkL = OTS1.verif-key skL vkH = map* #m OTS1.verif-key skH sigL = OTS1.sign skL b sigH = concat (map OTS1.sign (group #m #seckey1 skH) ⊛ m) lemma : ∀ {#m} sk m → and (map OTS1.verify (group #m #pubkey1 (map* #m OTS1.verif-key sk)) ⊛ m ⊛ group #m #signature1 (concat (map OTS1.sign (group #m #seckey1 sk) ⊛ m))) ≡ 1b lemma sk [] = refl lemma sk (b ∷ m) rewrite (let open lemma sk b m in take-++ #pubkey1 vkL vkH) | (let open lemma sk b m in drop-++ #pubkey1 vkL vkH) | (let open lemma sk b m in take-++ #signature1 sigL sigH) | (let open lemma sk b m in drop-++ #signature1 sigL sigH) | (let open lemma sk b m in OTS1.verify-correct-sig skL b) | (let open lemma sk b m in lemma skH m) = refl -- -} -- -} -- -} -- -}
Add Crypto.Sig.Lamport
Add Crypto.Sig.Lamport
Agda
bsd-3-clause
crypto-agda/crypto-agda
128af4af6ee42b630e845312b9e05e5b42880cae
Control/Strategy/Utils.agda
Control/Strategy/Utils.agda
{-# OPTIONS --copatterns #-} open import Type open import Function open import Data.Product open import Control.Strategy renaming (map to mapS) module Control.Strategy.Utils where record Proto : ★₁ where constructor P[_,_] field Q : ★ R : Q → ★ Client : Proto → ★ → ★ Client P[ Q , R ] A = Strategy Q R A {- data Bisim {P P' A A'} (RA : A → A' → ★) : Client P A → Client P' A' → ★ where ask-nop : ∀ {q? cont clt} r → Bisim RA (cont r) clt → Bisim RA (ask q? cont) clt ask-ask : ∀ q₀ q₁ cont₀ cont₁ r₀ r₁ → ({!!} → Bisim RA (cont₀ r₀) (cont₁ r₁)) → Bisim RA (ask q₀ cont₀) (ask q₁ cont₁) -} module Unused where mapS' : ∀ {A B Q Q' R} (f : A → B) (g : Q → Q') → Strategy Q (R ∘ g) A → Strategy Q' R B mapS' f g (ask q cont) = ask (g q) (λ r → mapS' f g (cont r)) mapS' f g (done x) = done (f x) [_,_]=<<_ : ∀ {A B Q Q' R} (f : A → Strategy Q' R B) (g : Q → Q') → Strategy Q (R ∘ g) A → Strategy Q' R B [ f , g ]=<< ask q? cont = ask (g q?) (λ r → [ f , g ]=<< cont r) [ f , g ]=<< done x = f x module Rec {A B Q : ★} {R : Q → ★} {M : ★ → ★} (runAsk : ∀ {A} → (q : Q) → (R q → M A) → M A) (runDone : A → M B) where runMM : Strategy Q R A → M B runMM (ask q? cont) = runAsk q? (λ r → runMM (cont r)) runMM (done x) = runDone x module MM {A B Q : ★} {R : Q → ★} {M : ★ → ★} (_>>=M_ : ∀ {A B : ★} → M A → (A → M B) → M B) (runAsk : (q : Q) → M (R q)) (runDone : A → M B) where runMM : Strategy Q R A → M B runMM (ask q? cont) = runAsk q? >>=M (λ r → runMM (cont r)) runMM (done x) = runDone x module _ {A B Q Q' : ★} {R : Q → ★} {R'} (f : (q : Q) → Strategy Q' R' (R q)) (g : A → Strategy Q' R' B) where [_,_]=<<'_ : Strategy Q R A → Strategy Q' R' B [_,_]=<<'_ (ask q? cont) = f q? >>= λ r → [_,_]=<<'_ (cont r) [_,_]=<<'_ (done x) = g x record ServerResp (P : Proto) (q : Proto.Q P) (A : ★₀) : ★₀ where coinductive open Proto P field srv-resp : R q srv-cont : ∀ q → ServerResp P q A open ServerResp Server : Proto → ★₀ → ★₀ Server P A = ∀ q → ServerResp P q A OracleServer : Proto → ★₀ OracleServer P[ Q , R ] = (q : Q) → R q module _ {P : Proto} {A : ★} where open Proto P com : Server P A → Client P A → A com srv (ask q κc) = com (srv-cont r) (κc (srv-resp r)) where r = srv q com srv (done x) = x module _ {P P' : Proto} {A A' : ★} where module P = Proto P module P' = Proto P' record MITM : ★ where coinductive field hack-query : (q' : P'.Q) → Client P (P'.R q' × MITM) hack-result : A' → Client P A open MITM module WithOutBind where hacked-com-client : Server P A → MITM → Client P' A' → A hacked-com-mitm : ∀ {q'} → Server P A → Client P (P'.R q' × MITM) → (P'.R q' → Client P' A') → A hacked-com-srv-resp : ∀ {q q'} → ServerResp P q A → (P.R q → Client P (P'.R q' × MITM)) → (P'.R q' → Client P' A') → A hacked-com-srv-resp r mitm clt = hacked-com-mitm (srv-cont r) (mitm (srv-resp r)) clt hacked-com-mitm srv (ask q? mitm) clt = hacked-com-srv-resp (srv q?) mitm clt hacked-com-mitm srv (done (r' , mitm)) clt = hacked-com-client srv mitm (clt r') hacked-com-client srv mitm (ask q' κc) = hacked-com-mitm srv (hack-query mitm q') κc hacked-com-client srv mitm (done x) = com srv (hack-result mitm x) mitm-to-client-trans : MITM → Client P' A' → Client P A mitm-to-client-trans mitm (ask q? cont) = hack-query mitm q? >>= λ { (r' , mitm') → mitm-to-client-trans mitm' (cont r') } mitm-to-client-trans mitm (done x) = hack-result mitm x hacked-com : Server P A → MITM → Client P' A' → A hacked-com srv mitm clt = com srv (mitm-to-client-trans mitm clt) module _ (P : Proto) (A : ★) where open Proto P open MITM honest : MITM {P} {P} {A} {A} hack-query honest q = ask q λ r → done (r , honest) hack-result honest a = done a module _ (P : Proto) (A : ★) (Oracle : OracleServer P) where oracle-server : Server P A srv-resp (oracle-server q) = Oracle q srv-cont (oracle-server q) = oracle-server -- -} -- -} -- -} -- -} -- -} -- -} -- -} -- -} -- -}
Add Control.Strategy.Utils
Add Control.Strategy.Utils
Agda
bsd-3-clause
crypto-agda/crypto-agda
f4f6e7a3cad61d0a276de2905272032cf125b5eb
lib/Data/Product/Param/Binary.agda
lib/Data/Product/Param/Binary.agda
{-# OPTIONS --without-K #-} open import Level open import Data.Product renaming (proj₁ to fst; proj₂ to snd) open import Function.Param.Binary module Data.Product.Param.Binary where record ⟦Σ⟧ {a₁ a₂ b₂ b₁ aᵣ bᵣ} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (Aᵣ : A₁ → A₂ → Set aᵣ) (Bᵣ : {x₁ : A₁} {x₂ : A₂} (xᵣ : Aᵣ x₁ x₂) → B₁ x₁ → B₂ x₂ → Set bᵣ) (p₁ : Σ A₁ B₁) (p₂ : Σ A₂ B₂) : Set (aᵣ ⊔ bᵣ) where constructor _⟦,⟧_ field ⟦fst⟧ : Aᵣ (fst p₁) (fst p₂) ⟦snd⟧ : Bᵣ ⟦fst⟧ (snd p₁) (snd p₂) open ⟦Σ⟧ public infixr 4 _⟦,⟧_ syntax ⟦Σ⟧ Aᵣ (λ xᵣ → e) = [ xᵣ ∶ Aᵣ ]⟦×⟧[ e ] ⟦∃⟧ : ∀ {a₁ a₂ b₂ b₁ aᵣ bᵣ} {A₁ : Set a₁} {A₂ : Set a₂} {Aᵣ : A₁ → A₂ → Set aᵣ} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} (Bᵣ : ⟦Pred⟧ bᵣ Aᵣ B₁ B₂) (p₁ : Σ A₁ B₁) (p₂ : Σ A₂ B₂) → Set _ ⟦∃⟧ = ⟦Σ⟧ _ syntax ⟦∃⟧ (λ xᵣ → e) = ⟦∃⟧[ xᵣ ] e _⟦×⟧_ : ∀ {a₁ a₂ b₂ b₁ aᵣ bᵣ} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : Set b₁} {B₂ : Set b₂} (Aᵣ : A₁ → A₂ → Set aᵣ) (Bᵣ : B₁ → B₂ → Set bᵣ) (p₁ : A₁ × B₁) (p₂ : A₂ × B₂) → Set (aᵣ ⊔ bᵣ) _⟦×⟧_ Aᵣ Bᵣ = ⟦Σ⟧ Aᵣ (λ _ → Bᵣ) {- _⟦×⟧_ : ∀ {a₁ a₂ aᵣ} {A₁ : Set a₁} {A₂ : Set a₂} (Aᵣ : A₁ → A₂ → Set aᵣ) {b₁ b₂ bᵣ} {B₁ : Set b₁} {B₂ : Set b₂} (Bᵣ : B₁ → B₂ → Set bᵣ) (p₁ : A₁ × B₁) (p₂ : A₂ × B₂) → Set _ _⟦×⟧_ Aᵣ Bᵣ = λ p₁ p₂ → Aᵣ (fst p₁) (fst p₂) × Bᵣ (snd p₁) (snd p₂) -} {- One can give these two types to ⟦_,_⟧: ⟦_,_⟧' : ∀ {a₁ a₂ b₁ b₂ aᵣ bᵣ} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} {Aᵣ : ⟦Set⟧ aᵣ A₁ A₂} {Bᵣ : ⟦Pred⟧ Aᵣ bᵣ B₁ B₂} {x₁ x₂ y₁ y₂} (xᵣ : Aᵣ x₁ x₂) (yᵣ : Bᵣ xᵣ y₁ y₂) → ⟦Σ⟧ Aᵣ Bᵣ (x₁ , y₁) (x₂ , y₂) ⟦_,_⟧' = ⟦_,_⟧ ⟦_,_⟧'' : ∀ {a₁ a₂ b₁ b₂ aᵣ bᵣ} {A₁ : Set a₁} {A₂ : Set a₂} {B₁ : A₁ → Set b₁} {B₂ : A₂ → Set b₂} {Aᵣ : ⟦Set⟧ aᵣ A₁ A₂} {Bᵣ : ⟦Pred⟧ Aᵣ bᵣ B₁ B₂} {p₁ p₂} (⟦fst⟧ : Aᵣ (fst p₁) (fst p₂)) (⟦snd⟧ : Bᵣ ⟦fst⟧ (snd p₁) (snd p₂)) → ⟦Σ⟧ Aᵣ Bᵣ p₁ p₂ ⟦_,_⟧'' = ⟦_,_⟧ -}
add Data.Product.Param.Binary
add Data.Product.Param.Binary
Agda
bsd-3-clause
np/agda-parametricity
8fd23013cafc4153d1b4a63b41e95bc6ecbbac7e
Crypto/Sig/LamportOneBit.agda
Crypto/Sig/LamportOneBit.agda
{-# OPTIONS --without-K #-} open import Data.Nat.NP hiding (_==_) open import Data.Product.NP hiding (map) open import Data.Bit hiding (_==_) open import Data.Bits open import Data.Bits.Properties open import Data.Vec.NP open import Relation.Binary.PropositionalEquality.NP module Crypto.Sig.LamportOneBit (#secret : ℕ) (#digest : ℕ) (hash : Bits #secret → Bits #digest) where #signkey = 2* #secret #verifkey = 2* #digest #signature = #secret Digest = Bits #digest Secret = Bits #secret SignKey = Bits #signkey Signature = Bits #signature VerifKey = Bits #verifkey module signkey (sk : SignKey) where skL = take #secret sk skH = drop #secret sk vkL = hash skL vkH = hash skH -- Derive the public key by hashing each secret verif-key : SignKey → VerifKey verif-key = map2* #secret #digest hash module verifkey (vk : VerifKey) where vkL = take #digest vk vkH = drop #digest vk -- Key generation key-gen : SignKey → VerifKey × SignKey key-gen sk = vk , sk module key-gen where vk = verif-key sk -- Sign a single bit message sign : SignKey → Bit → Signature sign sk b = take2* _ b sk -- Verify the signature of a single bit message verify : VerifKey → Bit → Signature → Bit verify vk b sig = take2* _ b vk == hash sig verify-correct-sig : ∀ sk b → verify (verif-key sk) b (sign sk b) ≡ 1b verify-correct-sig sk 0b = ==-reflexive (take-++ #digest (hash (take #secret sk)) _) verify-correct-sig sk 1b = ==-reflexive (drop-++ #digest (hash (take #secret sk)) _) import Algebra.FunctionProperties.Eq open Algebra.FunctionProperties.Eq.Implicits module Assuming-injectivity (hash-inj : Injective hash) where -- If one considers the hash function injective, then, so is verif-key verif-key-inj : ∀ {sk1 sk2} → verif-key sk1 ≡ verif-key sk2 → sk1 ≡ sk2 verif-key-inj {sk1} {sk2} e = take-drop= #secret sk1 sk2 (hash-inj (++-inj₁ e)) (hash-inj (++-inj₂ sk1.vkL sk2.vkL e)) where module sk1 = signkey sk1 module sk2 = signkey sk2 -- Therefor under this assumption, different secret keys means different public keys verif-key-corrolary : ∀ {sk1 sk2} → sk1 ≢ sk2 → verif-key sk1 ≢ verif-key sk2 verif-key-corrolary {sk1} {sk2} sk≢ vk= = sk≢ (verif-key-inj vk=) -- If one considers the hash function injective then there is -- only one signing key which can sign correctly all (0 and 1) -- the messages signkey-uniqness : ∀ sk1 sk2 → (∀ b → verify (verif-key sk1) b (sign sk2 b) ≡ 1b) → sk1 ≡ sk2 signkey-uniqness sk1 sk2 e = take-drop= #secret sk1 sk2 (lemmaL (e 0b)) (lemmaH (e 1b)) where module sk1 = signkey sk1 module sk2 = signkey sk2 lemmaL : verify (verif-key sk1) 0b (sign sk2 0b) ≡ 1b → sk1.skL ≡ sk2.skL lemmaL e0 rewrite take-++ #digest sk1.vkL sk1.vkH | hash-inj (==⇒≡ e0) = refl lemmaH : verify (verif-key sk1) 1b (sign sk2 1b) ≡ 1b → sk1.skH ≡ sk2.skH lemmaH e1 rewrite drop-++ #digest sk1.vkL sk1.vkH | hash-inj (==⇒≡ e1) = refl module Assuming-invertibility (unhash : Digest → Secret) (unhash-hash : ∀ x → unhash (hash x) ≡ x) (hash-unhash : ∀ x → hash (unhash x) ≡ x) where -- EXERCISES {- recover-signkey : VerifKey → SignKey recover-signkey = {!!} recover-signkey-correct : ∀ sk → recover-signkey (verif-key sk) ≡ sk recover-signkey-correct = {!!} forgesig : VerifKey → Bit → Signature forgesig = {!!} forgesig-correct : ∀ vk b → verify vk b (forgesig vk b) ≡ 1b forgesig-correct = {!!} -} -- -}
Add Crypto.Sig.LamportOneBit
Add Crypto.Sig.LamportOneBit
Agda
bsd-3-clause
crypto-agda/crypto-agda
fefd4590ac23e7cdf91cbc2277dcce1be57d736e
Control/Strategy.agda
Control/Strategy.agda
module Control.Strategy where open import Function open import Type using (★) open import Category.Monad open import Relation.Binary.PropositionalEquality.NP data Strategy (Q R A : ★) : ★ where ask : (q? : Q) (cont : R → Strategy Q R A) → Strategy Q R A done : A → Strategy Q R A infix 2 _≈_ data _≈_ {Q R A} : (s₀ s₁ : Strategy Q R A) → ★ where ask-ask : ∀ {k₀ k₁} → (q? : Q) (cont : ∀ r → k₀ r ≈ k₁ r) → ask q? k₀ ≈ ask q? k₁ done-done : ∀ x → done x ≈ done x ≈-refl : ∀ {Q R A} {s : Strategy Q R A} → s ≈ s ≈-refl {s = ask q? cont} = ask-ask q? (λ r → ≈-refl) ≈-refl {s = done x} = done-done x module _ {Q R : ★} where private M : ★ → ★ M = Strategy Q R _=<<_ : ∀ {A B} → (A → M B) → M A → M B f =<< ask q? cont = ask q? (λ r → f =<< cont r) f =<< done x = f x return : ∀ {A} → A → M A return = done map : ∀ {A B} → (A → B) → M A → M B map f s = (done ∘ f) =<< s join : ∀ {A} → M (M A) → M A join s = id =<< s _>>=_ : ∀ {A B} → M A → (A → M B) → M B _>>=_ = flip _=<<_ rawMonad : RawMonad M rawMonad = record { return = return; _>>=_ = _>>=_ } return-=<< : ∀ {A} (m : M A) → return =<< m ≈ m return-=<< (ask q? cont) = ask-ask q? (λ r → return-=<< (cont r)) return-=<< (done x) = done-done x return->>= : ∀ {A B} (x : A) (f : A → M B) → return x >>= f ≡ f x return->>= _ _ = refl >>=-assoc : ∀ {A B C} (mx : M A) (my : A → M B) (f : B → M C) → (mx >>= λ x → (my x >>= f)) ≈ (mx >>= my) >>= f >>=-assoc (ask q? cont) my f = ask-ask q? (λ r → >>=-assoc (cont r) my f) >>=-assoc (done x) my f = ≈-refl map-id : ∀ {A} (s : M A) → map id s ≈ s map-id = return-=<< map-∘ : ∀ {A B C} (f : A → B) (g : B → C) s → map (g ∘ f) s ≈ (map g ∘ map f) s map-∘ f g s = >>=-assoc s (done ∘ f) (done ∘ g) module EffectfulRun (E : ★ → ★) (_>>=E_ : ∀ {A B} → E A → (A → E B) → E B) (returnE : ∀ {A} → A → E A) (Oracle : Q → E R) where runE : ∀ {A} → M A → E A runE (ask q? cont) = Oracle q? >>=E (λ r → runE (cont r)) runE (done x) = returnE x module _ (Oracle : Q → R) where run : ∀ {A} → M A → A run (ask q? cont) = run (cont (Oracle q?)) run (done x) = x module _ {A B : ★} (f : A → M B) where run->>= : (s : M A) → run (s >>= f) ≡ run (f (run s)) run->>= (ask q? cont) = run->>= (cont (Oracle q?)) run->>= (done x) = refl module _ {A B : ★} (f : A → B) where run-map : (s : M A) → run (map f s) ≡ f (run s) run-map = run->>= (return ∘ f) module _ {A B : ★} where run-≈ : {s₀ s₁ : M A} → s₀ ≈ s₁ → run s₀ ≡ run s₁ run-≈ (ask-ask q? cont) = run-≈ (cont (Oracle q?)) run-≈ (done-done x) = refl
Add Control.Strategy
Add Control.Strategy
Agda
bsd-3-clause
crypto-agda/crypto-agda
962dded37d6c55fa91fe570fbfe0821e7c37241a
ttfp1.agda
ttfp1.agda
-- some excises for learning ttfp (type theory for functional programming). -- most of the code is *borrowed* from learn you an agda shamelessly -- Function composition _∘_ : {A : Set} { B : A -> Set} { C : (x : A) -> B x -> Set} (f : {x : A}(y : B x) -> C x y) (g : (x : A) -> B x) (x : A) -> C x (g x) (f ∘ g) x = f (g x) -- Conjuntion, ∏-type in haskell data _∧_ (α : Set) (β : Set) : Set where ∧-intro : α → β → (α ∧ β) ∧-elim₁ : {α β : Set} → (α ∧ β) → α ∧-elim₁ (∧-intro p q) = p ∧-elim₂ : {α β : Set} → (α ∧ β) → β ∧-elim₂ (∧-intro p q) = q _⇔_ : (α : Set) → (β : Set) → Set a ⇔ b = (a → b) ∧ (b → a) ∧-comm′ : {α β : Set} → (α ∧ β) → (β ∧ α) ∧-comm′ (∧-intro a b) = ∧-intro b a ∧-comm : {α β : Set} → (α ∧ β) ⇔ (β ∧ α) ∧-comm = ∧-intro ∧-comm′ ∧-comm′ -- type signature cannot be eliminated, was expecting type could be infered but it wasn't the case. e₁e₁ : { P Q R : Set} → ( (P ∧ Q) ∧ R ) → P e₁e₁ = ∧-elim₁ ∘ ∧-elim₁ e₂e₁ : { P Q R : Set} → ( (P ∧ Q) ∧ R) → Q e₂e₁ = ∧-elim₂ ∘ ∧-elim₁ ∧-assoc₁ : { P Q R : Set } → ((P ∧ Q) ∧ R) → (P ∧ (Q ∧ R)) ∧-assoc₁ = λ x → ∧-intro (∧-elim₁ (∧-elim₁ x)) (∧-intro (∧-elim₂ (∧-elim₁ x)) (∧-elim₂ x)) ∧-assoc₂ : {P Q R : Set} → (P ∧ (Q ∧ R)) → ((P ∧ Q) ∧ R) ∧-assoc₂ = λ x → ∧-intro (∧-intro (∧-elim₁ x) (∧-elim₁ (∧-elim₂ x))) (∧-elim₂ (∧-elim₂ x)) ∧-assoc : {P Q R : Set} → ((P ∧ Q) ∧ R) ⇔ (P ∧ (Q ∧ R)) ∧-assoc = ∧-intro ∧-assoc₁ ∧-assoc₂ -- Disjunctions, ∑-type in haskell data _∨_ (P Q : Set) : Set where ∨-intro₁ : P → P ∨ Q ∨-intro₂ : Q → P ∨ Q ∨-elim : {A B C : Set} → (A ∨ B) → (A → C) → (B → C) → C ∨-elim (∨-intro₁ a) ac bc = ac a ∨-elim (∨-intro₂ b) ac bc = bc b ∨-comm′ : {P Q : Set} → (P ∨ Q) → (Q ∨ P) ∨-comm′ (∨-intro₁ p) = ∨-intro₂ p ∨-comm′ (∨-intro₂ q) = ∨-intro₁ q ∨-comm : {P Q : Set} → (P ∨ Q) ⇔ (Q ∨ P) ∨-comm = ∧-intro ∨-comm′ ∨-comm′
Create ttfp1.agda
Create ttfp1.agda notes while learning ttfp
Agda
bsd-3-clause
wangbj/excises,wangbj/excises,wangbj/excises
38dc0e74c8050099800069c05d5282fdff28dced
incremental.agda
incremental.agda
module incremental where -- SIMPLE TYPES -- Syntax data Type : Set where _⇒_ : (τ₁ τ₂ : Type) → Type infixr 5 _⇒_ -- Semantics Dom⟦_⟧ : Type -> Set Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧ -- TYPING CONTEXTS -- Syntax data Context : Set where ∅ : Context _•_ : (τ : Type) (Γ : Context) → Context infixr 9 _•_ -- Semantics data Empty : Set where ∅ : Empty data Bind A B : Set where _•_ : (v : A) (ρ : B) → Bind A B Env⟦_⟧ : Context → Set Env⟦ ∅ ⟧ = Empty Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧ -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ -- Semantics lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ lookup⟦ this ⟧ (v • ρ) = v lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ -- TERMS -- Syntax data Term : Context → Type → Set where abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂) app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂ var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ -- Semantics eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ) eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ) eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ -- WEAKENING -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₁ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂ -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- Weaken a term to a super context weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t) weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂) weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x) -- CHANGE TYPES Δ-Type : Type → Type Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂ apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply {τ₁ ⇒ τ₂} = abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x ) ( f x ) compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ) compose {τ₁ ⇒ τ₂} = abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λdg. λx. compose ( df x ) ( dg x ) -- 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 τ • Γ -- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ) Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t) Δ-term {Γ} (app t₁ t₂) = {!!} Δ-term {Γ} (var x) = {!!}
Implement STLC in AGDA (fix #13).
Implement STLC in AGDA (fix #13). Old-commit-hash: 8c8d0d1e3a7f67f1a6640d7526542321b3a5a424
Agda
mit
inc-lc/ilc-agda
2824f8185e103bd7e2500937eff9fb9af1077e07
lib/Data/Nat/GCD/NP.agda
lib/Data/Nat/GCD/NP.agda
module Data.Nat.GCD.NP where open import Data.Nat open import Data.Nat.Properties open import Data.Nat.Divisibility as Div open import Relation.Binary private module P = Poset Div.poset open import Data.Product open import Relation.Binary.PropositionalEquality as PropEq using (_≡_) open import Induction open import Induction.Nat open import Induction.Lexicographic open import Function open import Data.Nat.GCD.Lemmas ------------------------------------------------------------------------ -- Greatest common divisor module GCD where -- Specification of the greatest common divisor (gcd) of two natural -- numbers. record GCD (m n gcd : ℕ) : Set where constructor is field -- The gcd is a common divisor. commonDivisor : gcd ∣ m × gcd ∣ n -- All common divisors divide the gcd, i.e. the gcd is the -- greatest common divisor according to the partial order _∣_. greatest : ∀ {d} → d ∣ m × d ∣ n → d ∣ gcd open GCD public -- The gcd is unique. unique : ∀ {d₁ d₂ m n} → GCD m n d₁ → GCD m n d₂ → d₁ ≡ d₂ unique d₁ d₂ = P.antisym (GCD.greatest d₂ (GCD.commonDivisor d₁)) (GCD.greatest d₁ (GCD.commonDivisor d₂)) -- The gcd relation is "symmetric". sym : ∀ {d m n} → GCD m n d → GCD n m d sym g = is (swap $ GCD.commonDivisor g) (GCD.greatest g ∘ swap) -- The gcd relation is "reflexive". refl : ∀ {n} → GCD n n n refl = is (P.refl , P.refl) proj₁ -- The GCD of 0 and n is n. base : ∀ {n} → GCD 0 n n base {n} = is (n ∣0 , P.refl) proj₂ -- If d is the gcd of n and k, then it is also the gcd of n and -- n + k. step : ∀ {n k d} → GCD n k d → GCD n (n + k) d step g with GCD.commonDivisor g step {n} {k} {d} g | (d₁ , d₂) = is (d₁ , ∣-+ d₁ d₂) greatest′ where greatest′ : ∀ {d′} → d′ ∣ n × d′ ∣ n + k → d′ ∣ d greatest′ (d₁ , d₂) = GCD.greatest g (d₁ , ∣-∸ d₂ d₁) open GCD public using (GCD) ------------------------------------------------------------------------ -- Calculating the gcd -- The calculation also proves Bézout's lemma. module Bézout where module Identity where -- If m and n have greatest common divisor d, then one of the -- following two equations is satisfied, for some numbers x and y. -- The proof is "lemma" below (Bézout's lemma). -- -- (If this identity was stated using integers instead of natural -- numbers, then it would not be necessary to have two equations.) data Identity (d m n : ℕ) : Set where -- +- : (x y : ℕ) (eq : d + y * n ≡ x * m) → Identity d m n -+ : (x y : ℕ) (eq : d + x * m ≡ y * n) → Identity d m n -- Various properties about Identity. {- sym : ∀ {d} → Symmetric (Identity d) sym (+- x y eq) = -+ y x eq sym (-+ x y eq) = +- y x eq -} refl : ∀ {d} → Identity d d d refl = -+ 0 1 PropEq.refl base : ∀ {d} → Identity d 0 d base = -+ 0 1 PropEq.refl private infixl 7 _⊕_ _⊕_ : ℕ → ℕ → ℕ m ⊕ n = 1 + m + n step : ∀ {d n k} → Identity d n k → Identity d n (n + k) {- step {d} (+- x y eq) with compare x y step {d} (+- .x .x eq) | equal x = +- (2 * x) x (lem₂ d x eq) step {d} (+- .x .(x ⊕ i) eq) | less x i = +- (2 * x ⊕ i) (x ⊕ i) (lem₃ d x eq) step {d} {n} (+- .(y ⊕ i) .y eq) | greater y i = +- (2 * y ⊕ i) y (lem₄ d y n eq) -} step {d} (-+ x y eq) with compare x y step {d} (-+ .x .x eq) | equal x = -+ (2 * x) x (lem₅ d x eq) step {d} (-+ .x .(x ⊕ i) eq) | less x i = -+ (2 * x ⊕ i) (x ⊕ i) (lem₆ d x eq) step {d} {n} (-+ .(y ⊕ i) .y eq) | greater y i = -+ (2 * y ⊕ i) y (lem₇ d y n eq) open Identity public using (Identity) -- ; +-; -+) module Lemma where -- This type packs up the gcd, the proof that it is a gcd, and the -- proof that it satisfies Bézout's identity. data Lemma (m n : ℕ) : Set where result : (d : ℕ) (g : GCD m n d) (b : Identity d m n) → Lemma m n -- Various properties about Lemma. {- sym : Symmetric Lemma sym (result d g b) = result d (GCD.sym g) (Identity.sym b) -} base : ∀ d → Lemma 0 d base d = result d GCD.base Identity.base refl : ∀ d → Lemma d d refl d = result d GCD.refl Identity.refl stepˡ : ∀ {n k} → Lemma n (suc k) → Lemma n (suc (n + k)) stepˡ {n} {k} (result d g b) = PropEq.subst (Lemma n) (lem₀ n k) $ result d (GCD.step g) (Identity.step b) {- stepʳ : ∀ {n k} → Lemma (suc k) n → Lemma (suc (n + k)) n stepʳ = sym ∘ stepˡ ∘ sym -} open Lemma public using (Lemma; result) -- Bézout's lemma proved using some variant of the extended -- Euclidean algorithm. lemma : (m n : ℕ) → Lemma m n lemma zero n = Lemma.base n lemma m zero = {!Lemma.sym (Lemma.base m)!} lemma (suc m) (suc n) = build [ <-rec-builder ⊗ <-rec-builder ] P gcd (m , n) where P : ℕ × ℕ → Set P (m , n) = Lemma (suc m) (suc n) gcd : ∀ p → (<-Rec ⊗ <-Rec) P p → P p gcd (m , n ) rec with compare m n gcd (m , .m ) rec | equal .m = Lemma.refl (suc m) gcd (m , .(suc m + k)) rec | less .m k = -- "gcd m k" -- k < n -- m < n -- n = suc m + k -- dist m n = suc k -- m + k < m + suc m + k -- k < suc m + k -- 0 < suc m Lemma.stepˡ $ proj₁ rec k (≤⇒≤′ (s≤s (≤-steps {k} {k} m (≤′⇒≤ ≤′-refl)))) gcd (.(suc n + k) , n) rec | greater .n k = -- "gcd k n" -- m > n -- m ≡ suc n + k -- dist m n = suc k -- k + n < suc n + k + n -- k < suc n + k -- 0 < suc n -- gcd n k {!Lemma.stepʳ $ proj₂ rec k (≤⇒≤′ (s≤s (≤-steps {k} {k} n (≤′⇒≤ ≤′-refl)))) n!} -- Bézout's identity can be recovered from the GCD. identity : ∀ {m n d} → GCD m n d → Identity d m n identity {m} {n} g with lemma m n identity g | result d g′ b with GCD.unique g g′ identity g | result d g′ b | PropEq.refl = b -- Calculates the gcd of the arguments. gcd : (m n : ℕ) → ∃ λ d → GCD m n d gcd m n with Bézout.lemma m n gcd m n | Bézout.result d g _ = (d , g) -- -} -- -} -- -} -- -} -- -}
Add a broken attempt at simplifying Nat.GCD: Nat.GCD.NP
Add a broken attempt at simplifying Nat.GCD: Nat.GCD.NP
Agda
bsd-3-clause
crypto-agda/agda-nplib
4995bc2a3bd988bcf90ed2efe5b614e17b5554a0
Game/IND-CPA-alt.agda
Game/IND-CPA-alt.agda
{-# OPTIONS --without-K #-} open import Type open import Data.Product open import Data.Bit module Game.IND-CPA-alt (PubKey : ★) (SecKey : ★) (Message : ★) (CipherText : ★) -- randomness supply for: encryption, key-generation, adversary, extensions (Rₑ Rₖ Rₐ : ★) (KeyGen : Rₖ → PubKey × SecKey) (Enc : PubKey → Message → Rₑ → CipherText) where M² = Bit → Message -- IND-CPA adversary in two parts Adv : ★ Adv = Rₐ → PubKey → (M² × (CipherText → Bit)) -- IND-CPA randomness supply R : ★ R = (Rₐ × Rₖ × Rₑ) -- IND-CPA games: -- * input: adversary and randomness supply -- * output b: adversary claims we are in game ⅁ b Game : ★ Game = Adv → R → Bit -- The game step by step: -- (pk) key-generation, only the public-key is needed -- (mb) send randomness, public-key and bit -- receive which message to encrypt -- (c) encrypt the message -- (b′) send randomness, public-key and ciphertext -- receive the guess from the adversary ⅁ : Bit → Game ⅁ b m (rₐ , rₖ , rₑ) = b′ where pk = proj₁ (KeyGen rₖ) ad = m rₐ pk mb = proj₁ ad b c = Enc pk mb rₑ b′ = proj₂ ad c ⅁₀ ⅁₁ : Game ⅁₀ = ⅁ 0b ⅁₁ = ⅁ 1b
Add alternative defintion for IND CPA
Add alternative defintion for IND CPA
Agda
bsd-3-clause
crypto-agda/crypto-agda
f9510280666609fdd9b8b35645bab6c0e17092c4
lib/FFI/JS.agda
lib/FFI/JS.agda
module FFI.JS where 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 Control.Process.Type {-# COMPILED_JS Bool function (x,v) { return ((x)? v["true"]() : v["false"]()); } #-} {-# COMPILED_JS true true #-} {-# COMPILED_JS false false #-} data JSType : Set where array object number string bool null : JSType infixr 5 _++_ postulate Number : Set readNumber : String → Number zero : Number one : Number _+_ : Number → Number → Number _++_ : String → String → String reverse : String → String sort : String → String take-half : String → String drop-half : String → String String▹List : String → List Char List▹String : List Char → String Number▹String : Number → String JSValue : Set _+JS_ : JSValue → JSValue → JSValue _≤JS_ : JSValue → JSValue → Bool _===_ : JSValue → JSValue → Bool JSON-stringify : JSValue → String JSON-parse : String → JSValue toString : JSValue → String fromString : String → JSValue fromChar : Char → JSValue fromNumber : Number → JSValue objectFromList : {A : Set} → List A → (A → String) → (A → JSValue) → JSValue fromJSArray : {A : Set} → JSValue → (Number → JSValue → A) → List A fromJSArrayString : JSValue → List String castNumber : JSValue → Number castString : JSValue → String nullJS : JSValue trueJS : JSValue falseJS : JSValue readJSType : String → JSType showJSType : JSType → String typeof : JSValue → JSType _·[_] : JSValue → JSValue → JSValue onString : {A : Set} (f : String → A) → JSValue → A trace : {A B : Set} → String → A → (A → B) → B {-# COMPILED_JS zero 0 #-} {-# COMPILED_JS one 1 #-} {-# COMPILED_JS readNumber Number #-} {-# COMPILED_JS _+_ function(x) { return function(y) { return x + y; }; } #-} {-# COMPILED_JS _++_ function(x) { return function(y) { return x + y; }; } #-} {-# COMPILED_JS _+JS_ function(x) { return function(y) { return x + y; }; } #-} {-# COMPILED_JS reverse function(x) { return x.split("").reverse().join(""); } #-} {-# COMPILED_JS sort function(x) { return x.split("").sort().join(""); } #-} {-# COMPILED_JS take-half function(x) { return x.substring(0,x.length/2); } #-} {-# COMPILED_JS drop-half function(x) { return x.substring(x.length/2); } #-} {-# COMPILED_JS List▹String function(x) { return (require("libagda").fromList(x, function(y) { return y; })).join(""); } #-} {-# COMPILED_JS String▹List function(x) { return require("libagda").fromJSArrayString(x.split("")); } #-} {-# COMPILED_JS fromJSArray function (ty) { return require("libagda").fromJSArray; } #-} {-# COMPILED_JS fromJSArrayString require("libagda").fromJSArrayString #-} {-# COMPILED_JS _≤JS_ function(x) { return function(y) { return x <= y; }; } #-} {-# COMPILED_JS _===_ function(x) { return function(y) { return x === y; }; } #-} {-# COMPILED_JS JSON-stringify JSON.stringify #-} {-# COMPILED_JS JSON-parse JSON.parse #-} {-# COMPILED_JS toString function(x) { return x.toString(); } #-} {-# COMPILED_JS fromString function(x) { return x; } #-} {-# COMPILED_JS fromChar function(x) { return x; } #-} {-# COMPILED_JS fromNumber function(x) { return x; } #-} {-# COMPILED_JS Number▹String String #-} {-# COMPILED_JS castNumber Number #-} {-# COMPILED_JS castString String #-} {-# COMPILED_JS nullJS null #-} {-# COMPILED_JS trueJS true #-} {-# COMPILED_JS falseJS false #-} {-# COMPILED_JS typeof function(x) { return typeof(x); } #-} {-# COMPILED_JS _·[_] require("libagda").readProp #-} {-# COMPILED_JS onString function(t) { return require("libagda").onString; } #-} {-# COMPILED_JS trace require("libagda").trace #-} data Value : Set₀ where array : List Value → Value object : List (String × Value) → Value string : String → Value number : Number → Value bool : Bool → Value null : Value postulate fromValue : Value → JSValue {-# COMPILED_JS fromValue require("libagda").fromValue #-} data ValueView : Set₀ where array : List JSValue → ValueView object : List (String × JSValue) → ValueView string : String → ValueView number : Number → ValueView bool : Bool → ValueView null : ValueView {- postulate fromJSValue : JSValue → ValueView -} fromBool : Bool → JSValue fromBool true = trueJS fromBool false = falseJS Object = List (String × JSValue) fromObject : Object → JSValue fromObject o = objectFromList o fst snd _≤Char_ : Char → Char → Bool x ≤Char y = fromChar x ≤JS fromChar 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 _·«_» : JSValue → String → JSValue v ·« s » = v ·[ fromString s ] abstract URI = String showURI : URI → String showURI x = x readURI : String → URI readURI x = x JSProc = Proc URI JSValue data JSCmd : Set where server : (ip port : String) (proc : URI → JSProc) (callback : URI → JSCmd) → JSCmd client : JSProc → JSCmd → JSCmd end : JSCmd assert : Bool → JSCmd → JSCmd console_log : String → JSCmd → JSCmd process_argv : (List String → JSCmd) → JSCmd
Add FFI.JS
Add FFI.JS
Agda
bsd-3-clause
audreyt/agda-libjs,crypto-agda/agda-libjs
9236c772d09686e999993e80d77ce8bd225d16f5
lib/FFI/JS/SHA1.agda
lib/FFI/JS/SHA1.agda
module FFI.JS.SHA1 where open import Data.String.Base using (String) postulate SHA1 : String → String {-# COMPILED_JS SHA1 function (x) { return require("sha1")(x); } #-}
Add FFI.JS.SHA1
Add FFI.JS.SHA1
Agda
bsd-3-clause
audreyt/agda-libjs,crypto-agda/agda-libjs
d5f6beafc85ff125a91c2f035d2d29dd6409f54e
ZK/GroupHom/ElGamal/JS.agda
ZK/GroupHom/ElGamal/JS.agda
{-# OPTIONS --without-K #-} open import FFI.JS.BigI open import Data.Bool.Base using (Bool) open import SynGrp open import ZK.GroupHom.Types import ZK.GroupHom.ElGamal import ZK.GroupHom.JS module ZK.GroupHom.ElGamal.JS (p q : BigI)(g : ℤ[ p ]★) where module EG = ZK.GroupHom.ElGamal `ℤ[ q ]+ `ℤ[ p ]★ _`^_ g module known-enc-rnd (pk : EG.PubKey) (M : EG.Message) (ct : EG.CipherText) where module ker = EG.Known-enc-rnd pk M ct module zkh = `ZK-hom ker.zk-hom module zkp = ZK.GroupHom.JS q zkh.`φ zkh.y prover-commitment : (r : zkp.Randomness)(w : zkp.Witness) → zkp.Commitment prover-commitment r w = zkp.Prover-Interaction.get-A (zkp.prover r w) prover-response : (r : zkp.Randomness)(w : zkp.Witness)(c : zkp.Challenge) → zkp.Response prover-response r w c = zkp.Prover-Interaction.get-f (zkp.prover r w) c verify-transcript : (A : zkp.Commitment)(c : zkp.Challenge)(r : zkp.Response) → Bool verify-transcript A c r = zkp.verifier (zkp.Transcript.mk A c r) -- -} -- -} -- -} -- -}
Add ZK.GroupHom.ElGamal.JS, so far only known-enc-rnd
Add ZK.GroupHom.ElGamal.JS, so far only known-enc-rnd
Agda
bsd-3-clause
crypto-agda/crypto-agda
1ffd391240cdf755b930cf4216a8231b81aee07d
FunUniverse/Fin/Op/Abstract.agda
FunUniverse/Fin/Op/Abstract.agda
open import Type open import Function open import Data.Product using (proj₂) open import Data.Two renaming (mux to mux₂) open import Data.Nat.NP using (ℕ; zero; suc; _+_; _*_; module ℕ°) open import Data.Fin.NP as Fin using (Fin; inject+; raise; bound; free; zero; suc) open import Data.Vec.NP using (Vec; []; _∷_; lookup; tabulate) open import Data.Vec.Properties using (lookup∘tabulate) open import Data.Bits using (Bits; _→ᵇ_; RewireTbl{-; 0ⁿ; 1ⁿ-}) open import Relation.Binary.PropositionalEquality open import FunUniverse.Data open import FunUniverse.Core open import FunUniverse.Fin.Op open import FunUniverse.Rewiring.Linear open import FunUniverse.Category open import Language.Simple.Interface module FunUniverse.Fin.Op.Abstract where private module BF = Rewiring finOpRewiring module _ {E : ★ → ★} {Op} (lang : Lang Op 𝟚 E) where open Lang lang {- bits : ∀ {o} → Bits o → 0 →ᵉ o bits xs = 𝟚▹E ∘ flip Vec.lookup xs -} efinFunU : FunUniverse ℕ efinFunU = Bits-U , _→ᵉ_ module EFinFunUniverse = FunUniverse efinFunU efinCat : Category _→ᵉ_ efinCat = input , _∘ᵉ_ private open EFinFunUniverse module _ {A B C D} (f : A `→ C) (g : B `→ D) where <_×_> : (A `× B) `→ (C `× D) <_×_> x with Fin.cmp C D x <_×_> ._ | Fin.bound y = inject+ B <$> f y <_×_> ._ | Fin.free y = raise A <$> g y efinLin : LinRewiring efinFunU efinLin = mk efinCat (flip <_×_> input) (λ {A} → input ∘ BF.swap {A}) (λ {A} → input ∘ BF.assoc {A}) input input <_×_> (λ {A} → <_×_> {A} input) input input input input efinRewiring : Rewiring efinFunU efinRewiring = mk efinLin (λ()) (input ∘ BF.dup) (λ()) (λ f g → < f × g > ∘ᵉ (input ∘ BF.dup)) (input ∘ inject+ _) (λ {A} → input ∘ raise A) (λ f → input ∘ BF.rewire f) (λ f → input ∘ BF.rewireTbl f) {- efinFork : HasFork efinFunU efinFork = cond , λ f g → second {1} < f , g > ⁏ cond where open Rewiring efinRewiring cond : ∀ {A} → `Bit `× A `× A `→ A cond {A} x = {!mux (return zero) (input (raise 1 (inject+ _ x))) (input (raise 1 (raise A x)))!} 𝟚▹E = {!!} efinOps : FunOps efinFunU efinOps = mk efinRewiring efinFork (const (𝟚▹E 0₂)) (const (𝟚▹E 1₂)) open Cong-*1 _→ᵉ_ reify : ∀ {i o} → i →ᵇ o → i →ᵉ o reify = cong-*1′ ∘ FunOps.fromBitsFun efinOps -} {- eval-reify : eval (reify f) eval-reify = ? -} -- -}
Add FunUniverse/Fin/Op/Abstract.agda
Add FunUniverse/Fin/Op/Abstract.agda
Agda
bsd-3-clause
crypto-agda/crypto-agda
3887acc776498fc936c85a42b1ba8ed98dfe1593
Everything.agda
Everything.agda
module Everything where import Holes.Prelude import Holes.Term import Holes.Cong.Limited import Holes.Cong.General import Holes.Cong.Propositional
Add Everything file
Add Everything file
Agda
mit
bch29/agda-holes
22eab6635a40915cfce9e29a45fe31de8280bcbf
PLDI14-List-of-Theorems.agda
PLDI14-List-of-Theorems.agda
module PLDI14-List-of-Theorems where open import Function -- 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 name. 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) -- (d) ChangeAlgebra.diff -- (e) IsChangeAlgebra.update-diff 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) -- For each plugin requirement, we include its definition and -- a concrete instantiation called "Popl14" with integers and -- bags of integers as base types. -- Plugin Requirement 3.1 (Domains of base types) open import Parametric.Denotation.Value using (Structure) open import Popl14.Denotation.Value using (⟦_⟧Base) -- Definition 3.2 (Domains) open Parametric.Denotation.Value.Structure using (⟦_⟧Type) -- Plugin Requirement 3.3 (Evaluation of constants) open import Parametric.Denotation.Evaluation using (Structure) open import Popl14.Denotation.Evaluation using (⟦_⟧Const) -- Definition 3.4 (Environments) open import Base.Denotation.Environment using (⟦_⟧Context) -- Definition 3.5 (Evaluation) open Parametric.Denotation.Evaluation.Structure using (⟦_⟧Term) -- Plugin Requirement 3.6 (Changes on base types) open import Parametric.Change.Validity using (Structure) open import Popl14.Change.Validity using (change-algebra-base-family) -- Definition 3.7 (Changes) open Parametric.Change.Validity.Structure using (change-algebra) -- Definition 3.8 (Change environments) open Parametric.Change.Validity.Structure using (environment-changes) -- Plugin Requirement 3.9 (Change semantics for constants) open import Parametric.Change.Specification using (Structure) open import Popl14.Change.Specification using (specification-structure) -- Definition 3.10 (Change semantics) open Parametric.Change.Specification.Structure using (⟦_⟧Δ) -- Lemma 3.11 (Change semantics is the derivative of semantics) open Parametric.Change.Specification.Structure using (correctness) -- Definition 3.12 (Erasure) import Parametric.Change.Implementation open Parametric.Change.Implementation.Structure using (_≈_) open import Popl14.Change.Implementation using (implements-base) -- Lemma 3.13 (The erased version of a change is almost the same) open Parametric.Change.Implementation.Structure using (carry-over) -- Lemma 3.14 (⟦ t ⟧Δ erases to Derive(t)) import Parametric.Change.Correctness open Parametric.Change.Correctness.Structure using (main-theorem)
add list of theorems
add list of theorems Old-commit-hash: ae20c5bded5fecd55d9723e7870ba03979cd0880
Agda
mit
inc-lc/ilc-agda
dd18fcc5ba9cbb6d8cbf62d171887cf3a22893f5
Base/Change/Products.agda
Base/Change/Products.agda
module Base.Change.Products where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Data.Product -- Also try defining sectioned change structures on the positives halves of -- groups? Or on arbitrary subsets? -- Restriction: we pair sets on the same level (because right now everything -- else would risk getting in the way). module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where open ≡-Reasoning -- The simplest possible definition of changes for products. -- The following is probably bullshit: -- Does not handle products of functions - more accurately, writing the -- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p PChange : A × B → Set ℓ PChange (a , b) = Δ a × Δ b _⊕_ : (v : A × B) → PChange v → A × B _⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db _⊝_ : A × B → (v : A × B) → PChange v _⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u p-update-diff (ua , ub) (va , vb) = let u = (ua , ub) v = (va , vb) in begin v ⊕ (u ⊝ v) ≡⟨⟩ (va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb)) --v ⊕ ((ua ⊟ va , ub ⊟ vb)) ≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩ (ua , ub) ≡⟨⟩ u ∎ changeAlgebra : ChangeAlgebra ℓ (A × B) changeAlgebra = record { Change = PChange ; update = _⊕_ ; diff = _⊝_ ; isChangeAlgebra = record { update-diff = p-update-diff } } proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v) proj₁′ (a , b) (da , db) = da proj₁′Derivative : Derivative proj₁ proj₁′ -- Implementation note: we do not need to pattern match on v and dv because -- they are records, hence Agda knows that pattern matching on records cannot -- fail. Technically, the required feature is the eta-rule on records. proj₁′Derivative v dv = refl -- An extended explanation. proj₁′Derivative₁ : Derivative proj₁ proj₁′ proj₁′Derivative₁ (a , b) (da , db) = let v = (a , b) dv = (da , db) in begin proj₁ v ⊞ proj₁′ v dv ≡⟨⟩ a ⊞ da ≡⟨⟩ proj₁ (v ⊞ dv) ∎ -- Same for the second extractor. proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v) proj₂′ (a , b) (da , db) = db proj₂′Derivative : Derivative proj₂ proj₂′ proj₂′Derivative v dv = refl -- We should do the same for uncurry instead. -- What one could wrongly expect to be the derivative of the constructor: _,_′ : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b) _,_′ a da b db = da , db -- That has the correct behavior, in a sense, and it would be in the -- subset-based formalization in the paper. -- -- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof -- As a consequence, proving that's a derivative seems too insanely hard. We -- might want to provide a proof schema for curried functions at once, -- starting from the right correctness equation. B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B) A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}} module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}} _,_′-real : Δ _,_ _,_′-real = nil _,_ module FCΔA→B→A×B = ΔA→B→A×B.FunctionChange _,_′-real-Derivative : Derivative {{CA}} {{B→A×B}} _,_ (FCΔA→B→A×B.apply _,_′-real) _,_′-real-Derivative = FunctionChanges.nil-is-derivative A (B → A × B) {{CA}} {{B→A×B}} _,_ {- _,_′′ : (a : A) → Δ a → Δ {{B→A×B}} (λ b → (a , b)) _,_′′ a da = record { apply = _,_′ a da ; correct = λ b db → begin (a , b ⊞ db) ⊞ (_,_′ a da) (b ⊞ db) (nil (b ⊞ db)) ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) ≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩ a ⊞ da , b ⊞ db ≡⟨⟩ (a , b) ⊞ (_,_′ a da) b db ∎ } _,_′′′ : Δ {{A→B→A×B}} _,_ _,_′′′ = record { apply = _,_′′ ; correct = λ a da → begin update (_,_ (a ⊞ da)) (_,_′′ (a ⊞ da) (nil (a ⊞ da))) ≡⟨ {!!} ⟩ update (_,_ a) (_,_′′ a da) --ChangeAlgebra.update B→A×B (_,_ a) (a , da ′′) ∎ } where open ChangeAlgebra B→A×B hiding (nil) {- {! begin (_,_ (a ⊞ da)) ⊞ _,_′′ (a ⊞ da) (nil (a ⊞ da)) {- ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) -} ≡⟨ ? ⟩ {-a ⊞ da , b ⊞ db ≡⟨⟩-} (_,_ a) ⊞ (_,_′′ a da) ∎!} } -} open import Postulate.Extensionality _,_′Derivative : Derivative {{CA}} {{B→A×B}} _,_ _,_′′ _,_′Derivative a da = begin _⊞_ {{B→A×B}} (_,_ a) (_,_′′ a da) ≡⟨⟩ (λ b → (a , b) ⊞ ΔBA×B.FunctionChange.apply (_,_′′ a da) b (nil b)) --ext (λ b → cong (λ □ → (a , b) ⊞ □) (update-nil {{?}} b)) ≡⟨ {!!} ⟩ (λ b → (a , b) ⊞ ΔBA×B.FunctionChange.apply (_,_′′ a da) b (nil b)) ≡⟨ sym {!ΔA→B→A×B.incrementalization _,_ _,_′′′ a da!} ⟩ --FunctionChanges.incrementalization A (B → A × B) {{CA}} {{{!B→A×B!}}} _,_ {!!} {!!} {!!} _,_ (a ⊞ da) ∎ where --open FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}} -}
Define a change structure for products
Define a change structure for products Derivatives of curried functions seem challenging. Accessing nested records too, but seems easy to fix. Old-commit-hash: d89e6171d447aae6292cc025dc6db95ce2370b88
Agda
mit
inc-lc/ilc-agda
16e3c79c254b8edd7f64b46df9c7c746fa4194c2
Language/Simple/Free.agda
Language/Simple/Free.agda
{-# OPTIONS --without-K --copatterns #-} open import Level.NP open import Function hiding (_$_) open import Type open import Algebra open import Algebra.FunctionProperties open import Algebra.Structures open import Data.Nat using (ℕ; zero; suc; _+_; _≤?_) open import Data.Fin using (Fin; zero; suc) open import Data.Vec using (Vec; []; _∷_; _++_) open import Data.One open import Data.Two open import Data.Product.NP open import Relation.Nullary.Decidable open import Relation.Binary.NP open import Relation.Binary.PropositionalEquality.NP as ≡ using (_≡_; !_; _≗_; module ≡-Reasoning) open import Category.Monad.NP import Language.Simple.Abstract module Language.Simple.Free where module _ {Op : ℕ → ★} where open Language.Simple.Abstract Op renaming (E to T) data Ctx (X : ★) : ★ where hole : Ctx X op : ∀ {a b} → Op (a + (1 + b)) → Vec (T X) a → Ctx X → Vec (T X) b → Ctx X module _ {X : ★} where η : ∀ {X} → X → T X η = var _·_ : Ctx X → T X → T X hole · e = e op o ts E us · e = op o (ts ++ E · e ∷ us) open WithEvalOp open Monad monad public renaming (liftM to map-T) module Unused {X R : ★} (⊢_≡_ : T X → T X → ★) (eval-Op : ∀ {n} → Op n → Vec R n → R) (eval-X : X → R) (t u : T X) (⊢t≡u : ⊢ t ≡ u) where ev = eval eval-Op eval-X ≈ = ev t ≡ ev u module ℛ {X : ★} (⊢_≡_ : T X → T X → ★) where {- data _≋*_ : ∀ {a} (ts us : Vec (T X) a) → ★ data _≋_ : (t u : T X) → ★ data _≋*_ where [] : [] ≋* [] _∷_ : ∀ {t u a} {ts us : Vec _ a} → t ≋ u → ts ≋* us → (t ∷ ts) ≋* (u ∷ us) data _≋_ where η : ∀ {t u} → ⊢ t ≡ u → t ≋ u !η : ∀ {t u} → ⊢ t ≡ u → u ≋ t var : ∀ x → var x ≋ var x op : ∀ {a} (o : Op a) {ts us} → ts ≋* us → op o ts ≋ op o us -} module Unused2 where data Succ (X : ★) : ★ where old : X → Succ X new : Succ X infix 9 _$_ _$_ : T (Succ X) → T X → T X E $ t = E >>= (λ { new → t ; (old x) → var x }) infix 0 _≈_ data _≈_ : (t u : T X) → ★ where refl : Reflexive _≈_ sym : Symmetric _≈_ trans : Transitive _≈_ η≡ : ∀ {t u} → ⊢ t ≡ u → t ≈ u congCtx : ∀ E {t u} → t ≈ u → E · t ≈ E · u ≈-isEquivalence : IsEquivalence _≈_ ≈-isEquivalence = record { refl = refl ; sym = sym ; trans = trans } module FreeSemigroup where U = Semigroup.Carrier record _→ₛ_ (X Y : Semigroup ₀ ₀) : ★ where open Semigroup X renaming (_∙_ to _∙x_) open Semigroup Y renaming (_∙_ to _∙y_) field to : U X → U Y to-∙-cong : ∀ t u → to (t ∙x u) ≡ to t ∙y to u open _→ₛ_ public idₛ : ∀ {S} → S →ₛ S to idₛ = id to-∙-cong idₛ _ _ = ≡.refl module _ {X Y Z} where _∘ₛ_ : (Y →ₛ Z) → (X →ₛ Y) → X →ₛ Z to (f ∘ₛ g) = to f ∘ to g to-∙-cong (f ∘ₛ g) t u = to (f ∘ₛ g) (t ∙x u) ≡⟨ ≡.cong (to f) (to-∙-cong g _ _) ⟩ to f (to g t ∙y to g u) ≡⟨ to-∙-cong f _ _ ⟩ to (f ∘ₛ g) t ∙z to (f ∘ₛ g) u ∎ where open ≡-Reasoning open Semigroup X renaming (_∙_ to _∙x_) open Semigroup Y renaming (_∙_ to _∙y_) open Semigroup Z renaming (_∙_ to _∙z_) data Op : ℕ → ★ where `μ : Op 2 open Language.Simple.Abstract Op renaming (E to T) open Monad monad module _ {X : ★} where _∙_ : T X → T X → T X t ∙ u = op `μ (t ∷ u ∷ []) module _ (X : ★) where infix 0 ⊢_≡_ data ⊢_≡_ : T X → T X → ★ where ⊢-∙-assoc : ∀ x y z → ⊢ ((x ∙ y) ∙ z) ≡ (x ∙ (y ∙ z)) open ℛ ⊢_≡_ ∙-assoc : Associative _≈_ _∙_ ∙-assoc x y z = η≡ (⊢-∙-assoc x y z) open Equivalence-Reasoning ≈-isEquivalence ∙-cong : _∙_ Preserves₂ _≈_ ⟶ _≈_ ⟶ _≈_ ∙-cong {x} {y} {u} {v} p q = x ∙ u ≈⟨ congCtx (op `μ (x ∷ []) hole []) q ⟩ x ∙ v ≈⟨ congCtx (op `μ [] hole (v ∷ [])) p ⟩ y ∙ v ∎ T-IsSemigroup : IsSemigroup _≈_ _∙_ T-IsSemigroup = record { isEquivalence = ≈-isEquivalence ; assoc = ∙-assoc ; ∙-cong = ∙-cong } T-Semigroup : Semigroup ₀ ₀ T-Semigroup = record { Carrier = T X ; _≈_ = _≈_ ; _∙_ = _∙_ ; isSemigroup = T-IsSemigroup } map-T-∙-hom : ∀ {X Y : ★} (f : X → Y) t u → map-T f (t ∙ u) ≡ map-T f t ∙ map-T f u map-T-∙-hom _ _ _ = ≡.refl Tₛ = T-Semigroup Tₛ-hom : ∀ {X Y} (f : X → Y) → Tₛ X →ₛ Tₛ Y to (Tₛ-hom f) = map-T f to-∙-cong (Tₛ-hom f) = map-T-∙-hom f module WithSemigroup (S : Semigroup ₀ ₀) where module S = Semigroup S open S renaming (Carrier to S₀; _∙_ to _∙ₛ_) eval-Op : ∀ {n} → Op n → Vec S₀ n → S₀ eval-Op `μ (x ∷ y ∷ []) = x ∙ₛ y eval-T : T S₀ → S₀ eval-T = WithEvalOp.eval eval-Op id eval-T-var : ∀ x → eval-T (var x) ≡ x eval-T-var x = ≡.refl eval-T-∙ : ∀ t u → eval-T (t ∙ u) ≡ eval-T t ∙ₛ eval-T u eval-T-∙ t u = ≡.refl eval-Tₛ : Tₛ S₀ →ₛ S to eval-Tₛ = eval-T to-∙-cong eval-Tₛ t u = ≡.refl {- Tₛ : Sets ⟶ Semigroups U : Semigroups ⟶ Sets Tₛ ⊣ U Semigroups(Tₛ X, Y) ≅ Sets(X, U Y) -} module _ {X : ★} {Y : Semigroup ₀ ₀} where open WithSemigroup Y open Semigroup Y renaming (Carrier to Y₀; _∙_ to _∙Y_) φ : (Tₛ X →ₛ Y) → X → U Y φ f = to f ∘ var -- var is the unit (η) ψ : (f : X → U Y) → Tₛ X →ₛ Y ψ f = eval-Tₛ ∘ₛ Tₛ-hom f -- eval-Tₛ is the co-unit (ε) φ-ψ : ∀ f → φ (ψ f) ≡ f φ-ψ f = ≡.refl ψ-φ : ∀ f t → to (ψ (φ f)) t ≡ to f t ψ-φ f (var x) = ≡.refl ψ-φ f (op `μ (t ∷ u ∷ [])) = to (ψ (φ f)) (t ∙ u) ≡⟨ ≡.refl ⟩ eval-T (map-T (φ f) (t ∙ u)) ≡⟨ ≡.refl ⟩ eval-T (map-T (φ f) t ∙ map-T (φ f) u) ≡⟨ ≡.refl ⟩ eval-T (map-T (φ f) t) ∙Y eval-T (map-T (φ f) u) ≡⟨ ≡.refl ⟩ to (ψ (φ f)) t ∙Y to (ψ (φ f)) u ≡⟨ ≡.cong₂ _∙Y_ (ψ-φ f t) (ψ-φ f u) ⟩ to f t ∙Y to f u ≡⟨ ! (to-∙-cong f t u) ⟩ to f (t ∙ u) ∎ where open ≡-Reasoning module FreeMonoid where data Op : ℕ → ★ where `ε : Op 0 `μ : Op 2 open Language.Simple.Abstract Op renaming (E to T) ε : ∀ {X} → T X ε = op `ε [] _∙_ : ∀ {X} → T X → T X → T X t ∙ u = op `μ (t ∷ u ∷ []) infix 0 _⊢_≡_ data _⊢_≡_ (X : ★) : T X → T X → ★ where ⊢-ε∙x : ∀ x → X ⊢ ε ∙ x ≡ x ⊢-x∙ε : ∀ x → X ⊢ x ∙ ε ≡ x ⊢-∙-assoc : ∀ x y z → X ⊢ ((x ∙ y) ∙ z) ≡ (x ∙ (y ∙ z)) module _ (X : ★) where open ℛ (_⊢_≡_ X) {- TODO, share the proofs with FreeSemigroup above -} ∙-assoc : Associative _≈_ _∙_ ∙-assoc x y z = η≡ (⊢-∙-assoc x y z) open Equivalence-Reasoning ≈-isEquivalence ∙-cong : _∙_ Preserves₂ _≈_ ⟶ _≈_ ⟶ _≈_ ∙-cong {x} {y} {u} {v} p q = x ∙ u ≈⟨ congCtx (op `μ (x ∷ []) hole []) q ⟩ x ∙ v ≈⟨ congCtx (op `μ [] hole (v ∷ [])) p ⟩ y ∙ v ∎ monoid : Monoid ₀ ₀ monoid = record { Carrier = T X ; _≈_ = _≈_ ; _∙_ = _∙_ ; ε = ε ; isMonoid = record { isSemigroup = record { isEquivalence = ≈-isEquivalence ; assoc = ∙-assoc ; ∙-cong = ∙-cong } ; identity = (λ x → η≡ (⊢-ε∙x x)) , (λ x → η≡ (⊢-x∙ε x)) } } {- Free monoid as a functor F : Sets → Mon map-[] : map f [] ≡ [] map-++ : map f xs ++ map f ys = map f (xs ++ ys) F X = (List X , [] , _++_) F (f : Sets(X , Y)) : Mon(F X , F Y) = (map f , map-[] , map-++) -} module FreeGroup where data Op : ℕ → ★ where `ε : Op 0 `i : Op 1 `μ : Op 2 open Language.Simple.Abstract Op renaming (E to T) ε : ∀ {X} → T X ε = op `ε [] infix 9 −_ −_ : ∀ {X} → T X → T X − t = op `i (t ∷ []) infixr 6 _∙_ _∙_ : ∀ {X} → T X → T X → T X t ∙ u = op `μ (t ∷ u ∷ []) infix 0 _⊢_≡_ data _⊢_≡_ (X : ★) : T X → T X → ★ where ε∙x : ∀ x → X ⊢ ε ∙ x ≡ x x∙ε : ∀ x → X ⊢ x ∙ ε ≡ x ∙-assoc : ∀ x y z → X ⊢ (x ∙ (y ∙ z)) ≡ ((x ∙ y) ∙ z) −∙-inv : ∀ x → X ⊢ − x ∙ x ≡ ε ∙−-inv : ∀ x → X ⊢ x ∙ − x ≡ ε -- − (− t) ≡ t -- − t ∙ − (− t) ≡ ε ≡ − t ∙ t -- -} -- -} -- -} -- -}
Add Language.Simple.Free
Add Language.Simple.Free
Agda
bsd-3-clause
crypto-agda/crypto-agda
a2284fe9263eadf1deb8ce96940c9f0cc72949bb
ModalLogic.agda
ModalLogic.agda
module ModalLogic where -- Implement Frank Pfenning's lambda calculus based on modal logic S4, as -- described in "A Modal Analysis of Staged Computation". open import Denotational.Notation data BaseType : Set where Base : BaseType Next : BaseType → BaseType data Type : Set where base : BaseType → Type _⇒_ : Type → Type → Type □_ : Type → Type -- Reuse contexts, variables and weakening from Tillmann's library. open import Syntactic.Contexts Type -- No semantics for these types. data Term : Context → Context → Type → Set where abs : ∀ {τ₁ τ₂ Γ Δ} → (t : Term (τ₁ • Γ) Δ τ₂) → Term Γ Δ (τ₁ ⇒ τ₂) app : ∀ {τ₁ τ₂ Γ Δ} → (t₁ : Term Γ Δ (τ₁ ⇒ τ₂)) (t₂ : Term Γ Δ τ₁) → Term Γ Δ τ₂ ovar : ∀ {Γ Δ τ} → (x : Var Γ τ) → Term Γ Δ τ mvar : ∀ {Γ Δ τ} → (u : Var Δ τ) → Term Γ Δ τ -- Note the builtin weakening box : ∀ {Γ Δ τ} → Term ∅ Δ τ → Term Γ Δ (□ τ) let-box_in-_ : ∀ {τ₁ τ₂ Γ Δ} → (e₁ : Term Γ Δ (□ τ₁)) → (e₂ : Term Γ (τ₁ • Δ) τ₂) → Term Γ Δ τ₂ -- Lemma 1 includes weakening. -- Most of the proof can be generated by C-c C-a, but syntax errors must then be fixed. weaken : ∀ {Γ₁ Γ₂ Δ₁ Δ₂ τ} → (Γ′ : Γ₁ ≼ Γ₂) → (Δ′ : Δ₁ ≼ Δ₂) → Term Γ₁ Δ₁ τ → Term Γ₂ Δ₂ τ weaken Γ′ Δ′ (abs {τ₁} t) = abs (weaken (keep τ₁ • Γ′) Δ′ t) weaken Γ′ Δ′ (app t t₁) = app (weaken Γ′ Δ′ t) (weaken Γ′ Δ′ t₁) weaken Γ′ Δ′ (ovar x) = ovar (lift Γ′ x) weaken Γ′ Δ′ (mvar u) = mvar (lift Δ′ u) weaken Γ′ Δ′ (box t) = box (weaken ∅ Δ′ t) weaken Γ′ Δ′ (let-box_in-_ {τ₁} t t₁) = let-box weaken Γ′ Δ′ t in- weaken Γ′ (keep τ₁ • Δ′) t₁
Implement modal logic
Implement modal logic This doesn't belong here, but it reuses @Toxaris modular library. Old-commit-hash: 2f12ca949c8cb58e2b65b773607282bafbccb5c9
Agda
mit
inc-lc/ilc-agda
006fd9c825ad2011f95bb01a40c7f000d4fd4f30
bugs/Future.agda
bugs/Future.agda
module Future where open import Denotational.Notation -- Uses too much memory. -- open import Data.Integer open import Data.Product open import Data.Sum open import Data.Unit postulate ⟦Int⟧ : Set postulate ⟦Bag⟧ : Set → Set --mutual -- First, define a language of types, their denotations, and then the whole algebra of changes in the semantic domain data Type : Set ⟦_⟧Type : Type → Set data Type where Int : Type --Bag : Type → Type _⇒_ {- _⊠_ _⊞_ -} : Type → Type → Type Δ : (τ : Type) → (old : ⟦ τ ⟧Type) → Type -- meaningOfType : Meaning Type -- meaningOfType = meaning ⟦_⟧Type -- Try out Δ old here Change : (τ : Type) → ⟦ τ ⟧Type → Set Change Int old₁ = {!!} -- Change (Bag τ₁) old₁ = ⟦ Bag τ₁ ⟧Type ⊎ (Σ[ old ∈ (⟦ τ₁ ⟧Type) ] (⟦Bag⟧ ⟦ Δ τ₁ old ⟧Type)) Change (τ₁ ⇒ τ₂) oldF = ⟦ Δ τ₂ (oldF ?) ⟧Type -- oldF ? -- oldF ? -- (oldInput : ⟦ τ₁ ⟧Type) → ⟦ Δ τ₁ oldInput ⟧Type → ⟦ Δ τ₂ (oldF oldInput) ⟧Type -- Change (τ₁ ⊠ τ₂) old₁ = {!⟦ τ₁ ⟧Type × ⟦ τ₂ ⟧Type!} -- Change (τ₁ ⊞ τ₂) old₁ = {!!} Change (Δ τ₁ old₁) old₂ = {!!} ⟦ Int ⟧Type = ⟦Int⟧ -- ⟦ Bag τ ⟧Type = ⟦Bag⟧ ⟦ τ ⟧Type ⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type -- ⟦ σ ⊠ τ ⟧Type = ⟦ σ ⟧Type × ⟦ τ ⟧Type -- ⟦ σ ⊞ τ ⟧Type = ⟦ σ ⟧Type ⊎ ⟦ τ ⟧Type ⟦ Δ τ old ⟧Type = {- ⊤ ⊎ ⟦ τ ⟧Type ⊎ -} Change τ old
add another bug example
Agda: add another bug example Old-commit-hash: 82f681165ed59ccc36685cbf9c376026811e127f
Agda
mit
inc-lc/ilc-agda
9bb44c3e31a11725d740b2f59d40ebbe7e0917a2
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 -- -} -- -} -- -} -- -} -- -}
Add FiniteField/JS
Add FiniteField/JS
Agda
bsd-3-clause
crypto-agda/crypto-agda
a48ff25823603032db6b1a26e4605512aefd0561
notes/FOT/FOTC/Data/Conat/ConatSL.agda
notes/FOT/FOTC/Data/Conat/ConatSL.agda
------------------------------------------------------------------------------ -- Definition of FOTC Conat using Agda's co-inductive combinators ------------------------------------------------------------------------------ {-# OPTIONS --allow-unsolved-metas #-} {-# OPTIONS --no-universe-polymorphism #-} {-# OPTIONS --without-K #-} module FOT.FOTC.Data.Conat.ConatSL where open import FOTC.Base open import Coinduction ------------------------------------------------------------------------------ data Conat : D → Set where cozero : Conat zero cosucc : ∀ {n} → (∞ (Conat n)) → Conat (succ₁ n) Conat-unf : ∀ {n} → Conat n → n ≡ zero ∨ (∃[ n' ] Conat n' ∧ n ≡ succ₁ n') Conat-unf cozero = inj₁ refl Conat-unf (cosucc {n} Cn) = inj₂ (n , ♭ Cn , refl) Conat-pre-fixed : ∀ {n} → (n ≡ zero ∨ (∃[ n' ] Conat n' ∧ n ≡ succ₁ n')) → Conat n Conat-pre-fixed (inj₁ h) = subst Conat (sym h) cozero Conat-pre-fixed (inj₂ (n , Cn , h)) = subst Conat (sym h) (cosucc (♯ Cn)) Conat-coind : ∀ (A : D → Set) {n} → (A n → n ≡ zero ∨ (∃[ n' ] A n' ∧ n ≡ succ₁ n')) → A n → Conat n Conat-coind A h An = {!!}
Add incomplete note on co-inductive natural numbers.
Add incomplete note on co-inductive natural numbers.
Agda
mit
asr/fotc,asr/fotc
07045b7aa32a14de23ec233029b6fc6134b24685
experiment/liftVec.agda
experiment/liftVec.agda
open import Function open import Data.Nat using (ℕ ; suc ; zero) open import Data.Fin using (Fin) open import Data.Vec open import Data.Vec.N-ary.NP open import Relation.Binary.PropositionalEquality module liftVec where module single (A : Set)(_+_ : A → A → A)where data Tm : Set where var : Tm cst : A → Tm fun : Tm → Tm → Tm eval : Tm → A → A eval var x = x eval (cst c) x = c eval (fun tm tm') x = eval tm x + eval tm' x veval : {m : ℕ} → Tm → Vec A m → Vec A m veval var xs = xs veval (cst x) xs = replicate x veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs) record Fm : Set where constructor _=='_ field lhs : Tm rhs : Tm s[_] : Fm → Set s[ lhs ==' rhs ] = (x : A) → eval lhs x ≡ eval rhs x v[_] : Fm → Set v[ lhs ==' rhs ] = {m : ℕ}(xs : Vec A m) → veval lhs xs ≡ veval rhs xs lemma1 : (t : Tm) → veval t [] ≡ [] lemma1 var = refl lemma1 (cst x) = refl lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl lemma2 : {m : ℕ}(x : A)(xs : Vec A m)(t : Tm) → veval t (x ∷ xs) ≡ eval t x ∷ veval t xs lemma2 _ _ var = refl lemma2 _ _ (cst c) = refl lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl prf : (t : Fm) → s[ t ] → v[ t ] prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl prf (l ==' r) pr (x ∷ xs) rewrite lemma2 x xs l | lemma2 x xs r = cong₂ (_∷_) (pr x) (prf (l ==' r) pr xs) module MultiDataType {A : Set} {nrVar : ℕ} where data Tm : Set where var : Fin nrVar → Tm cst : A → Tm fun : Tm → Tm → Tm infixl 6 _+'_ _+'_ : Tm → Tm → Tm _+'_ = fun infix 4 _≡'_ record TmEq : Set where constructor _≡'_ field lhs rhs : Tm module multi (A : Set)(_+_ : A → A → A)(nrVar : ℕ) where open MultiDataType {A} {nrVar} public sEnv : Set sEnv = Vec A nrVar vEnv : ℕ → Set vEnv m = Vec (Vec A m) nrVar Var : Set Var = Fin nrVar _!_ : {X : Set} → Vec X nrVar → Var → X E ! v = lookup v E eval : Tm → sEnv → A eval (var x) G = G ! x eval (cst c) G = c eval (fun t t') G = eval t G + eval t' G veval : {m : ℕ} → Tm → vEnv m → Vec A m veval (var x) G = G ! x veval (cst c) G = replicate c veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G) lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) → veval t xs ≡ [] lemma1 {xs} (var x) with xs ! x ... | [] = refl lemma1 (cst x) = refl lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl lemVar : {m n : ℕ}(G : Vec (Vec A (suc m)) n)(i : Fin n) → lookup i G ≡ lookup i (map head G) ∷ lookup i (map tail G) lemVar [] () lemVar ((x ∷ G) ∷ G₁) Data.Fin.zero = refl lemVar (G ∷ G') (Data.Fin.suc i) = lemVar G' i lemma2 : {n : ℕ}(xs : vEnv (suc n))(t : Tm) → veval t xs ≡ eval t (map head xs) ∷ veval t (map tail xs) lemma2 G (var x) = lemVar G x lemma2 _ (cst x) = refl lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl s[_==_] : Tm → Tm → Set s[ l == r ] = (G : Vec A nrVar) → eval l G ≡ eval r G v[_==_] : Tm → Tm → Set v[ l == r ] = {m : ℕ}(G : Vec (Vec A m) nrVar) → veval l G ≡ veval r G prf : (l r : Tm) → s[ l == r ] → v[ l == r ] prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r = cong₂ _∷_ (pr (replicate head ⊛ G)) (prf l r pr (map tail G)) module Full (A : Set)(_+_ : A → A → A) (m : ℕ) n where open multi A _+_ n public solve : ∀ (l r : Tm) → ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G)) → ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G)) solve l r x = curryⁿ {n} {A = Vec A m} {B = curryⁿ′ f} (λ xs → subst id (sym (curry-$ⁿ′ f xs)) (prf l r (λ G → subst id (curry-$ⁿ′ g G) (x $ⁿ G)) xs)) where f = λ G → veval {m} l G ≡ veval r G g = λ G → eval l G ≡ eval r G mkTm : N-ary n Tm TmEq → TmEq mkTm = go (tabulate id) where go : {m : ℕ} -> Vec (Fin n) m → N-ary m Tm TmEq → TmEq go [] f = f go (x ∷ args) f = go args (f (var x)) prove : ∀ (t : N-ary n Tm TmEq) → let l = TmEq.lhs (mkTm t) r = TmEq.rhs (mkTm t) in ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G)) → ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G)) prove t x = solve (TmEq.lhs (mkTm t)) (TmEq.rhs (mkTm t)) x open import Data.Bool module example where open import Data.Fin open import Data.Bool.NP open import Data.Product using (Σ ; proj₁) module VecBoolXorProps {m} = Full Bool _xor_ m open MultiDataType coolTheorem : {m : ℕ} → (xs ys : Vec Bool m) → zipWith _xor_ xs ys ≡ zipWith _xor_ ys xs coolTheorem = VecBoolXorProps.prove 2 (λ x y → x +' y ≡' y +' x) Xor°.+-comm coolTheorem2 : {m : ℕ} → (xs : Vec Bool m) → _ coolTheorem2 = VecBoolXorProps.prove 1 (λ x → (x +' x) ≡' (cst false)) (proj₁ Xor°.-‿inverse) test = example.coolTheorem (true ∷ false ∷ []) (false ∷ false ∷ [])
open import Function open import Data.Nat using (ℕ ; suc ; zero) open import Data.Fin using (Fin) open import Data.Vec open import Data.Vec.N-ary.NP open import Relation.Binary.PropositionalEquality module liftVec where module single (A : Set)(_+_ : A → A → A)where data Tm : Set where var : Tm cst : A → Tm fun : Tm → Tm → Tm eval : Tm → A → A eval var x = x eval (cst c) x = c eval (fun tm tm') x = eval tm x + eval tm' x veval : {m : ℕ} → Tm → Vec A m → Vec A m veval var xs = xs veval (cst x) xs = replicate x veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs) record Fm : Set where constructor _=='_ field lhs : Tm rhs : Tm s[_] : Fm → Set s[ lhs ==' rhs ] = (x : A) → eval lhs x ≡ eval rhs x v[_] : Fm → Set v[ lhs ==' rhs ] = {m : ℕ}(xs : Vec A m) → veval lhs xs ≡ veval rhs xs lemma1 : (t : Tm) → veval t [] ≡ [] lemma1 var = refl lemma1 (cst x) = refl lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl lemma2 : {m : ℕ}(x : A)(xs : Vec A m)(t : Tm) → veval t (x ∷ xs) ≡ eval t x ∷ veval t xs lemma2 _ _ var = refl lemma2 _ _ (cst c) = refl lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl prf : (t : Fm) → s[ t ] → v[ t ] prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl prf (l ==' r) pr (x ∷ xs) rewrite lemma2 x xs l | lemma2 x xs r = cong₂ (_∷_) (pr x) (prf (l ==' r) pr xs) module MultiDataType {A : Set} {nrVar : ℕ} where data Tm : Set where var : Fin nrVar → Tm cst : A → Tm fun : Tm → Tm → Tm infixl 6 _+'_ _+'_ : Tm → Tm → Tm _+'_ = fun infix 4 _≡'_ record TmEq : Set where constructor _≡'_ field lhs rhs : Tm module multi (A : Set)(_+_ : A → A → A)(nrVar : ℕ) where open MultiDataType {A} {nrVar} public sEnv : Set sEnv = Vec A nrVar vEnv : ℕ → Set vEnv m = Vec (Vec A m) nrVar Var : Set Var = Fin nrVar _!_ : {X : Set} → Vec X nrVar → Var → X E ! v = lookup v E eval : Tm → sEnv → A eval (var x) G = G ! x eval (cst c) G = c eval (fun t t') G = eval t G + eval t' G veval : {m : ℕ} → Tm → vEnv m → Vec A m veval (var x) G = G ! x veval (cst c) G = replicate c veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G) lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) → veval t xs ≡ [] lemma1 {xs} (var x) with xs ! x ... | [] = refl lemma1 (cst x) = refl lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl lemVar : {m n : ℕ}(G : Vec (Vec A (suc m)) n)(i : Fin n) → lookup i G ≡ lookup i (map head G) ∷ lookup i (map tail G) lemVar [] () lemVar ((x ∷ G) ∷ G₁) Data.Fin.zero = refl lemVar (G ∷ G') (Data.Fin.suc i) = lemVar G' i lemma2 : {n : ℕ}(xs : vEnv (suc n))(t : Tm) → veval t xs ≡ eval t (map head xs) ∷ veval t (map tail xs) lemma2 G (var x) = lemVar G x lemma2 _ (cst x) = refl lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl s[_==_] : Tm → Tm → Set s[ l == r ] = (G : Vec A nrVar) → eval l G ≡ eval r G v[_==_] : Tm → Tm → Set v[ l == r ] = {m : ℕ}(G : Vec (Vec A m) nrVar) → veval l G ≡ veval r G prf : (l r : Tm) → s[ l == r ] → v[ l == r ] prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r = cong₂ _∷_ (pr (replicate head ⊛ G)) (prf l r pr (map tail G)) module Full (A : Set)(_+_ : A → A → A) (m : ℕ) n where open multi A _+_ n public solve : ∀ (l r : Tm) → ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G)) → ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G)) solve l r x = curryⁿ {n} {A = Vec A m} {B = curryⁿ′ f} (λ xs → subst id (sym (curry-$ⁿ′ f xs)) (prf l r (λ G → subst id (curry-$ⁿ′ g G) (x $ⁿ G)) xs)) where f = λ G → veval {m} l G ≡ veval r G g = λ G → eval l G ≡ eval r G mkTm : N-ary n Tm TmEq → TmEq mkTm = go (tabulate id) where go : {m : ℕ} -> Vec (Fin n) m → N-ary m Tm TmEq → TmEq go [] f = f go (x ∷ args) f = go args (f (var x)) prove : ∀ (t : N-ary n Tm TmEq) → let l = TmEq.lhs (mkTm t) r = TmEq.rhs (mkTm t) in ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G)) → ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G)) prove t x = solve (TmEq.lhs (mkTm t)) (TmEq.rhs (mkTm t)) x open import Data.Two module example where open import Data.Fin open import Data.Product using (Σ ; proj₁) module VecBoolXorProps {m} = Full 𝟚 _xor_ m open MultiDataType coolTheorem : {m : ℕ} → (xs ys : Vec 𝟚 m) → zipWith _xor_ xs ys ≡ zipWith _xor_ ys xs coolTheorem = VecBoolXorProps.prove 2 (λ x y → x +' y ≡' y +' x) Xor°.+-comm coolTheorem2 : {m : ℕ} → (xs : Vec 𝟚 m) → _ coolTheorem2 = VecBoolXorProps.prove 1 (λ x → (x +' x) ≡' (cst 0₂)) (proj₁ Xor°.-‿inverse) test = example.coolTheorem (1₂ ∷ 0₂ ∷ []) (0₂ ∷ 0₂ ∷ [])
Upgrade to Data.Two
Upgrade to Data.Two
Agda
bsd-3-clause
crypto-agda/agda-nplib
a79e9939c6512db4827fc831f2689871d99cfc26
Base/Change/Context.agda
Base/Change/Context.agda
module Base.Change.Context {Type : Set} (ΔType : Type → Type) where -- Transform a context of values into a context of values and -- changes. open import Base.Syntax.Context Type ΔContext : Context → Context ΔContext ∅ = ∅ ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ -- like ΔContext, but ΔType τ and τ are swapped ΔContext′ : Context → Context ΔContext′ ∅ = ∅ ΔContext′ (τ • Γ) = τ • ΔType τ • ΔContext′ Γ Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ Γ≼ΔΓ {∅} = ∅ Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ
module Base.Change.Context {Type : Set} (ΔType : Type → Type) where open import Base.Syntax.Context Type -- Transform a context of values into a context of values and -- changes. ΔContext : Context → Context ΔContext ∅ = ∅ ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ -- like ΔContext, but ΔType τ and τ are swapped ΔContext′ : Context → Context ΔContext′ ∅ = ∅ ΔContext′ (τ • Γ) = τ • ΔType τ • ΔContext′ Γ Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ Γ≼ΔΓ {∅} = ∅ Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ
Move function-specific comment right before function
Move function-specific comment right before function Currently, this seems a comment about the whole module. Old-commit-hash: c25e918e712496981adfe4c74f94a8d80e8e5052
Agda
mit
inc-lc/ilc-agda
06d988ce4bc3692e4e60e39bdb8f2e1daddb660e
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 : ⟦ τ ⟧} → 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 int = ℤ ΔVal bag = Bag ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ -- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧ _⊞_ {int} n Δn = n + Δn _⊞_ {bag} b Δb = b ++ Δb _⊞_ {σ ⇒ τ} f Δf = λ v → f v ⊞ Δf v (v ⊟ v) (R[v,u-v] {σ}) -- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ _⊟_ {int} m n = m - n _⊟_ {bag} d b = d \\ b _⊟_ {σ ⇒ τ} g f = λ v Δv R[v,Δv] → g (v ⊞ Δv) ⊟ f v -- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set valid {int} n Δn = ⊤ valid {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] -- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u v+[u-v]=u {int} {u} {v} = n+[m-n]=m {v} {u} v+[u-v]=u {bag} {u} {v} = a++[b\\a]=b {v} {u} 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] {int} {u} {v} = tt R[v,u-v] {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 = u} {v}) ⟩ u w′ ≡⟨ sym (v+[u-v]=u {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 τ ⟧ _⟦⊕⟧_ {int} n Δn = n + Δn _⟦⊕⟧_ {bag} b Δb = b ++ Δb _⟦⊕⟧_ {σ ⇒ τ} f Δf = λ v → f v ⟦⊕⟧ Δf v (v ⟦⊝⟧ v) _⟦⊝⟧_ {int} m n = m - n _⟦⊝⟧_ {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 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)
add type annotations to Denotation.Change.Popl14
[WIP] add type annotations to Denotation.Change.Popl14 Old-commit-hash: 90b06722aecf9d0b5c6c69547217cb2c522912be
Agda
mit
inc-lc/ilc-agda
d9a3d68c58bd5c8ae49738e7a8f68fd5e89b4bce
lambda.agda
lambda.agda
module lambda where open import Relation.Binary.PropositionalEquality open import Denotational.Notation -- SIMPLE TYPES -- Syntax data Type : Set where _⇒_ : (τ₁ τ₂ : Type) → Type bool : Type infixr 5 _⇒_ -- Denotational Semantics data Bool : Set where true false : Bool ⟦_⟧Type : Type -> Set ⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type ⟦ bool ⟧Type = Bool meaningOfType : Meaning Type meaningOfType = meaning ⟦_⟧Type -- TYPING CONTEXTS, VARIABLES and WEAKENING open import Syntactic.Contexts Type public open import Denotational.Environments Type ⟦_⟧Type public -- 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 cond : ∀ {Γ τ} → (e₁ : Term Γ bool) (e₂ e₃ : Term Γ τ) → Term Γ τ -- 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 ⟦ cond t₁ t₂ t₃ ⟧Term ρ with ⟦ t₁ ⟧Term ρ ... | true = ⟦ t₂ ⟧Term ρ ... | false = ⟦ t₃ ⟧Term ρ meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ) meaningOfTerm = meaning ⟦_⟧Term -- NATURAL SEMANTICS -- Syntax data Env : Context → Set data Val : Type → Set data Val where ⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂) true : Val bool false : Val bool 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 cond-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} → ρ ⊢ t₁ ↓ true → ρ ⊢ t₂ ↓ v₂ → ρ ⊢ cond t₁ t₂ t₃ ↓ v₂ cond-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} → ρ ⊢ t₁ ↓ false → ρ ⊢ t₃ ↓ v₃ → ρ ⊢ cond t₁ t₂ t₃ ↓ v₃ -- SOUNDNESS of natural semantics ⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧ ⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧ ⟦ ∅ ⟧Env = ∅ ⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env ⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env) ⟦ true ⟧Val = true ⟦ false ⟧Val = false 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 ↦ ↓-sound (cond-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂ ↓-sound (cond-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃ -- WEAKENING -- Weaken a term to a super context weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t) weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂) weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x) weaken {Γ₁} {Γ₂} true = true weaken {Γ₁} {Γ₂} false = false weaken {Γ₁} {Γ₂} (cond e₁ e₂ e₃) = cond (weaken {Γ₁} {Γ₂} e₁) (weaken {Γ₁} {Γ₂} e₂) (weaken {Γ₁} {Γ₂} e₃)
module lambda where open import Relation.Binary.PropositionalEquality open import Syntactic.Types public open import Syntactic.Contexts Type public open import Denotational.Notation open import Denotational.Values public open import Denotational.Environments Type ⟦_⟧Type public -- 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 cond : ∀ {Γ τ} → (e₁ : Term Γ bool) (e₂ e₃ : Term Γ τ) → Term Γ τ -- 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 ⟦ cond t₁ t₂ t₃ ⟧Term ρ with ⟦ t₁ ⟧Term ρ ... | true = ⟦ t₂ ⟧Term ρ ... | false = ⟦ t₃ ⟧Term ρ meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ) meaningOfTerm = meaning ⟦_⟧Term -- NATURAL SEMANTICS -- Syntax data Env : Context → Set data Val : Type → Set data Val where ⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂) true : Val bool false : Val bool 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 cond-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} → ρ ⊢ t₁ ↓ true → ρ ⊢ t₂ ↓ v₂ → ρ ⊢ cond t₁ t₂ t₃ ↓ v₂ cond-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} → ρ ⊢ t₁ ↓ false → ρ ⊢ t₃ ↓ v₃ → ρ ⊢ cond t₁ t₂ t₃ ↓ v₃ -- SOUNDNESS of natural semantics ⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧ ⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧ ⟦ ∅ ⟧Env = ∅ ⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env ⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env) ⟦ true ⟧Val = true ⟦ false ⟧Val = false 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 ↦ ↓-sound (cond-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂ ↓-sound (cond-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃ -- WEAKENING -- Weaken a term to a super context weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t) weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂) weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x) weaken {Γ₁} {Γ₂} true = true weaken {Γ₁} {Γ₂} false = false weaken {Γ₁} {Γ₂} (cond e₁ e₂ e₃) = cond (weaken {Γ₁} {Γ₂} e₁) (weaken {Γ₁} {Γ₂} e₂) (weaken {Γ₁} {Γ₂} e₃)
Improve reuse (see #28).
Improve reuse (see #28). Old-commit-hash: 7da11aabf971e192ab71542164b98c80cbe26325
Agda
mit
inc-lc/ilc-agda
a883bca1e4dcf18cbd172d5af888116a00518e15
Parametric/Change/Term.agda
Parametric/Change/Term.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure (diff-base : DiffStructure) (apply-base : ApplyStructure) where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply {base ι} = diff-base , apply-base lift-diff-apply {σ ⇒ τ} = let -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ}) _⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t _⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t _⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t _⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t in abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x) , abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)) lift-diff : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) lift-diff = λ {τ Γ} → proj₁ (lift-diff-apply {τ} {Γ}) lift-apply : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-apply = λ {τ Γ} → proj₂ (lift-diff-apply {τ} {Γ}) diff : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ) diff = app₂ lift-diff apply : ∀ {τ Γ} → Term Γ (ΔType τ) → Term Γ τ → Term Γ τ apply = app₂ lift-apply
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Term {Base : Set} (Constant : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where -- Terms that operate on changes open Type.Structure Base open Term.Structure Base Constant open ChangeType.Structure Base ΔBase open import Data.Product DiffStructure : Set DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι)) ApplyStructure : Set ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι) module Structure (diff-base : DiffStructure) (apply-base : ApplyStructure) where -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-diff-apply {base ι} = diff-base , apply-base lift-diff-apply {σ ⇒ τ} = let -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ}) _⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t _⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t _⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t _⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t in abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x) , abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)) diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) diff-term = λ {τ Γ} → proj₁ (lift-diff-apply {τ} {Γ}) lift-apply : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) lift-apply = λ {τ Γ} → proj₂ (lift-diff-apply {τ} {Γ}) diff : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ) diff = app₂ diff-term apply : ∀ {τ Γ} → Term Γ (ΔType τ) → Term Γ τ → Term Γ τ apply = app₂ lift-apply
Rename lift-diff to diff-term.
Rename lift-diff to diff-term. Old-commit-hash: 64cb73fbed07060d460e4d9ee85fcec48281d13a
Agda
mit
inc-lc/ilc-agda
b01d8abafe916c9d2d8af12b12d2adb5f2ae8340
Syntax/Term/Plotkin.agda
Syntax/Term/Plotkin.agda
module Syntax.Term.Plotkin where -- Terms of languages described in Plotkin style open import Function using (_∘_) open import Data.Product open import Syntax.Type.Plotkin open import Syntax.Context data Term {B : Set {- of base types -}} {C : Set {- of constants -}} {type-of : C → Type B} (Γ : Context {Type B}) : (τ : Type B) → Set where const : (c : C) → Term Γ (type-of c) var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term {B} {C} {type-of} Γ (σ ⇒ τ)) (t : Term {B} {C} {type-of} Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term {B} {C} {type-of} (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {B C type-of} {Δbase : B → Type B} → let Δtype = lift-Δtype Δbase term = Term {B} {C} {type-of} 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 : ∀ {B C type-of} {Δbase : B → Type B} → let Δtype = lift-Δtype Δbase term = Term {B} {C} {type-of} 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 : ∀ {B C type-of} {Δbase : B → Type B} → let Δtype = lift-Δtype Δbase term = Term {B} {C} {type-of} 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 : ∀ {B C ⊢ Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term {B} {C} {⊢} Γ₁ τ → Term {B} {C} {⊢} Γ₂ τ 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₁ : ∀ {B C ⊢ Γ σ τ} → Term {B} {C} {⊢} Γ τ → Term {B} {C} {⊢} (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {B C ⊢ Γ α β τ} → Term {B} {C} {⊢} Γ τ → Term {B} {C} {⊢} (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {B C ⊢ Γ α β γ τ} → Term {B} {C} {⊢} Γ τ → Term {B} {C} {⊢} (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {B C ⊢ Γ α β γ} → Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {B C ⊢ Γ α β γ δ} → Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {B C ⊢ Γ α β γ δ ε} → Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {B C ⊢ Γ α β γ δ ε ζ} → Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {B C ⊢ Γ α β γ δ ε ζ η} → Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x)
module Syntax.Term.Plotkin {B : Set {- of base types -}} {C : Set {- of constants -}} where -- Terms of languages described in Plotkin style open import Function using (_∘_) open import Data.Product open import Syntax.Type.Plotkin open import Syntax.Context data Term {type-of : C → Type B} (Γ : Context {Type B}) : (τ : Type B) → Set where const : (c : C) → Term Γ (type-of c) var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term {type-of} Γ (σ ⇒ τ)) (t : Term {type-of} Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term {type-of} (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {type-of} {Δbase : B → Type B} → let Δtype = lift-Δtype Δbase term = Term {type-of} 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 : ∀ {type-of} {Δbase : B → Type B} → let Δtype = lift-Δtype Δbase term = Term {type-of} 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 : ∀ {type-of} {Δbase : B → Type B} → let Δtype = lift-Δtype Δbase term = Term {type-of} 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)
Move parameters to module level.
Move parameters to module level. All definitions in this module depend on the parameters {B} and {C} which never change inside this module. So it is easier to parameterize the whole module instead of each separate definition. Old-commit-hash: 1303320f48d486d11152713c604275c2c7bcdc18
Agda
mit
inc-lc/ilc-agda
9755ab9f86f1e00b28c36e99421e4e9be389a95f
Base/Change/Products.agda
Base/Change/Products.agda
module Base.Change.Products where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Data.Product -- Also try defining sectioned change structures on the positives halves of -- groups? Or on arbitrary subsets? -- Restriction: we pair sets on the same level (because right now everything -- else would risk getting in the way). module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where open ≡-Reasoning -- The simplest possible definition of changes for products. -- The following is probably bullshit: -- Does not handle products of functions - more accurately, writing the -- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p PChange : A × B → Set ℓ PChange (a , b) = Δ a × Δ b _⊕_ : (v : A × B) → PChange v → A × B _⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db _⊝_ : A × B → (v : A × B) → PChange v _⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u p-update-diff (ua , ub) (va , vb) = let u = (ua , ub) v = (va , vb) in begin v ⊕ (u ⊝ v) ≡⟨⟩ (va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb)) --v ⊕ ((ua ⊟ va , ub ⊟ vb)) ≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩ (ua , ub) ≡⟨⟩ u ∎ changeAlgebra : ChangeAlgebra ℓ (A × B) changeAlgebra = record { Change = PChange ; update = _⊕_ ; diff = _⊝_ ; isChangeAlgebra = record { update-diff = p-update-diff } } proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v) proj₁′ (a , b) (da , db) = da proj₁′Derivative : Derivative proj₁ proj₁′ -- Implementation note: we do not need to pattern match on v and dv because -- they are records, hence Agda knows that pattern matching on records cannot -- fail. Technically, the required feature is the eta-rule on records. proj₁′Derivative v dv = refl -- An extended explanation. proj₁′Derivative₁ : Derivative proj₁ proj₁′ proj₁′Derivative₁ (a , b) (da , db) = let v = (a , b) dv = (da , db) in begin proj₁ v ⊞ proj₁′ v dv ≡⟨⟩ a ⊞ da ≡⟨⟩ proj₁ (v ⊞ dv) ∎ -- Same for the second extractor. proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v) proj₂′ (a , b) (da , db) = db proj₂′Derivative : Derivative proj₂ proj₂′ proj₂′Derivative v dv = refl -- We should do the same for uncurry instead. -- What one could wrongly expect to be the derivative of the constructor: _,_′ : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b) _,_′ a da b db = da , db -- That has the correct behavior, in a sense, and it would be in the -- subset-based formalization in the paper. -- -- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof -- As a consequence, proving that's a derivative seems too insanely hard. We -- might want to provide a proof schema for curried functions at once, -- starting from the right correctness equation. B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B) A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}} module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}} _,_′-real : Δ _,_ _,_′-real = nil _,_ _,_′-real-Derivative : Derivative {{CA}} {{B→A×B}} _,_ (ΔA→B→A×B.apply _,_′-real) _,_′-real-Derivative = FunctionChanges.nil-is-derivative A (B → A × B) {{CA}} {{B→A×B}} _,_ _,_′′ : (a : A) → Δ a → Δ {{B→A×B}} (λ b → (a , b)) _,_′′ a da = record { apply = _,_′ a da ; correct = λ b db → begin (a , b ⊞ db) ⊞ (_,_′ a da) (b ⊞ db) (nil (b ⊞ db)) ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) ≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩ a ⊞ da , b ⊞ db ≡⟨⟩ (a , b) ⊞ (_,_′ a da) b db ∎ } _,_′′′ : Δ {{A→B→A×B}} _,_ _,_′′′ = record { apply = _,_′′ ; correct = λ a da → begin update (_,_ (a ⊞ da)) (_,_′′ (a ⊞ da) (nil (a ⊞ da))) ≡⟨ {!!} ⟩ update (_,_ a) (_,_′′ a da) ∎ } where -- This is needed to use update above. -- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly. open ChangeAlgebra B→A×B hiding (nil) {- {! begin (_,_ (a ⊞ da)) ⊞ _,_′′ (a ⊞ da) (nil (a ⊞ da)) {- ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) -} ≡⟨ ? ⟩ {-a ⊞ da , b ⊞ db ≡⟨⟩-} (_,_ a) ⊞ (_,_′′ a da) ∎!} } -} open import Postulate.Extensionality _,_′Derivative : Derivative {{CA}} {{B→A×B}} _,_ _,_′′ _,_′Derivative a da = begin _⊞_ {{B→A×B}} (_,_ a) (_,_′′ a da) ≡⟨⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) --ext (λ b → cong (λ □ → (a , b) ⊞ □) (update-nil {{?}} b)) ≡⟨ {!!} ⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) ≡⟨ sym {!ΔA→B→A×B.incrementalization _,_ _,_′′′ a da!} ⟩ --FunctionChanges.incrementalization A (B → A × B) {{CA}} {{{!B→A×B!}}} _,_ {!!} {!!} {!!} _,_ (a ⊞ da) ∎
module Base.Change.Products where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Data.Product -- Also try defining sectioned change structures on the positives halves of -- groups? Or on arbitrary subsets? -- Restriction: we pair sets on the same level (because right now everything -- else would risk getting in the way). module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where open ≡-Reasoning -- The simplest possible definition of changes for products. -- The following is probably bullshit: -- Does not handle products of functions - more accurately, writing the -- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p PChange : A × B → Set ℓ PChange (a , b) = Δ a × Δ b _⊕_ : (v : A × B) → PChange v → A × B _⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db _⊝_ : A × B → (v : A × B) → PChange v _⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u p-update-diff (ua , ub) (va , vb) = let u = (ua , ub) v = (va , vb) in begin v ⊕ (u ⊝ v) ≡⟨⟩ (va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb)) --v ⊕ ((ua ⊟ va , ub ⊟ vb)) ≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩ (ua , ub) ≡⟨⟩ u ∎ changeAlgebra : ChangeAlgebra ℓ (A × B) changeAlgebra = record { Change = PChange ; update = _⊕_ ; diff = _⊝_ ; isChangeAlgebra = record { update-diff = p-update-diff } } proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v) proj₁′ (a , b) (da , db) = da proj₁′Derivative : Derivative proj₁ proj₁′ -- Implementation note: we do not need to pattern match on v and dv because -- they are records, hence Agda knows that pattern matching on records cannot -- fail. Technically, the required feature is the eta-rule on records. proj₁′Derivative v dv = refl -- An extended explanation. proj₁′Derivative₁ : Derivative proj₁ proj₁′ proj₁′Derivative₁ (a , b) (da , db) = let v = (a , b) dv = (da , db) in begin proj₁ v ⊞ proj₁′ v dv ≡⟨⟩ a ⊞ da ≡⟨⟩ proj₁ (v ⊞ dv) ∎ -- Same for the second extractor. proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v) proj₂′ (a , b) (da , db) = db proj₂′Derivative : Derivative proj₂ proj₂′ proj₂′Derivative v dv = refl -- We should do the same for uncurry instead. -- What one could wrongly expect to be the derivative of the constructor: _,_′ : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b) _,_′ a da b db = da , db -- That has the correct behavior, in a sense, and it would be in the -- subset-based formalization in the paper. -- -- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof -- As a consequence, proving that's a derivative seems too insanely hard. We -- might want to provide a proof schema for curried functions at once, -- starting from the right correctness equation. B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B) A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}} module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}} _,_′-real : Δ _,_ _,_′-real = nil _,_ _,_′-real-Derivative : Derivative {{CA}} {{B→A×B}} _,_ (ΔA→B→A×B.apply _,_′-real) _,_′-real-Derivative = FunctionChanges.nil-is-derivative A (B → A × B) {{CA}} {{B→A×B}} _,_ _,_′′ : (a : A) → Δ a → Δ {{B→A×B}} (λ b → (a , b)) _,_′′ a da = record { apply = _,_′ a da ; correct = λ b db → begin (a , b ⊞ db) ⊞ (_,_′ a da) (b ⊞ db) (nil (b ⊞ db)) ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) ≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩ a ⊞ da , b ⊞ db ≡⟨⟩ (a , b) ⊞ (_,_′ a da) b db ∎ } {- _,_′′′ : Δ {{A→B→A×B}} _,_ _,_′′′ = record { apply = _,_′′ ; correct = λ a da → begin update (_,_ (a ⊞ da)) (_,_′′ (a ⊞ da) (nil (a ⊞ da))) ≡⟨ {!!} ⟩ update (_,_ a) (_,_′′ a da) ∎ } where -- This is needed to use update above. -- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly. open ChangeAlgebra B→A×B hiding (nil) {- {! begin (_,_ (a ⊞ da)) ⊞ _,_′′ (a ⊞ da) (nil (a ⊞ da)) {- ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) -} ≡⟨ ? ⟩ {-a ⊞ da , b ⊞ db ≡⟨⟩-} (_,_ a) ⊞ (_,_′′ a da) ∎!} } -} open import Postulate.Extensionality _,_′Derivative : Derivative {{CA}} {{B→A×B}} _,_ _,_′′ _,_′Derivative a da = begin _⊞_ {{B→A×B}} (_,_ a) (_,_′′ a da) ≡⟨⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) --ext (λ b → cong (λ □ → (a , b) ⊞ □) (update-nil {{?}} b)) ≡⟨ {!!} ⟩ (λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b)) ≡⟨ sym {!ΔA→B→A×B.incrementalization _,_ _,_′′′ a da!} ⟩ --FunctionChanges.incrementalization A (B → A × B) {{CA}} {{{!B→A×B!}}} _,_ {!!} {!!} {!!} _,_ (a ⊞ da) ∎ -}
Comment out holes to ensure Everything compiles again
Comment out holes to ensure Everything compiles again Old-commit-hash: dfc28a00f9301015066e19d4be2c4179f8e0849b
Agda
mit
inc-lc/ilc-agda
07b3b64ff9795f6941b64231e7b2a0dd630e0de4
lib/Explore/Examples.agda
lib/Explore/Examples.agda
{-# OPTIONS --without-K #-} module Explore.Examples where open import Type open import Level.NP open import Data.Maybe.NP open import Data.List open import Data.Two open import Data.Product open import Data.Sum.NP open import Data.One open import HoTT using (UA) open import Function.Extensionality using (FunExt) open import Relation.Binary.PropositionalEquality hiding ([_]) open import Explore.Core open import Explore.Explorable open import Explore.Universe.Base open import Explore.Monad {₀} ₀ public renaming (map to map-explore) open import Explore.Two open import Explore.Product open Explore.Product.Operators module E where open FromExplore public module M {Msg Digest : ★} (_==_ : Digest → Digest → 𝟚) (H : Msg → Digest) (exploreMsg : ∀ {ℓ} → Explore ℓ Msg) (d : Digest) where module V1 where list-H⁻¹ : List Msg list-H⁻¹ = exploreMsg [] _++_ (λ m → [0: [] 1: [ m ] ] (H m == d)) module ExploreMsg = FromExplore {A = Msg} exploreMsg module V2 where first-H⁻¹ : Maybe Msg first-H⁻¹ = ExploreMsg.findKey (λ m → H m == d) module V3 where explore-H⁻¹ : Explore ₀ Msg explore-H⁻¹ ε _⊕_ f = exploreMsg ε _⊕_ (λ m → [0: ε 1: f m ] (H m == d)) module V4 where explore-H⁻¹ : Explore ₀ Msg explore-H⁻¹ = exploreMsg >>= λ m → [0: empty-explore 1: point-explore m ] (H m == d) module V5 where explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg explore-H⁻¹ = filter-explore (λ m → H m == d) exploreMsg list-H⁻¹ : List Msg list-H⁻¹ = E.list explore-H⁻¹ first-H⁻¹ : Maybe Msg first-H⁻¹ = E.first explore-H⁻¹ module V6 where explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg explore-H⁻¹ = explore-endo (filter-explore (λ m → H m == d) exploreMsg) list-H⁻¹ : List Msg list-H⁻¹ = E.list explore-H⁻¹ first-H⁻¹ : Maybe Msg first-H⁻¹ = E.first explore-H⁻¹ last-H⁻¹ : Maybe Msg last-H⁻¹ = E.last explore-H⁻¹ Msg = 𝟚 × 𝟚 Digest = 𝟚 -- _==_ : Digest → Digest → 𝟚 H : Msg → Digest H (x , y) = x xor y Msgᵉ : ∀ {ℓ} → Explore ℓ Msg Msgᵉ = 𝟚ᵉ ×ᵉ 𝟚ᵉ module N5 = M.V5 _==_ H Msgᵉ module N6 = M.V6 _==_ H Msgᵉ test5 = N5.list-H⁻¹ test6-list : N6.list-H⁻¹ 0₂ ≡ (0₂ , 0₂) ∷ (1₂ , 1₂) ∷ [] test6-list = refl test6-rev-list : E.list (E.backward (N6.explore-H⁻¹ 0₂)) ≡ (1₂ , 1₂) ∷ (0₂ , 0₂) ∷ [] test6-rev-list = refl test6-first : N6.first-H⁻¹ 0₂ ≡ just (0₂ , 0₂) test6-first = refl test6-last : N6.last-H⁻¹ 0₂ ≡ just (1₂ , 1₂) test6-last = refl -- -} {- 𝟛ᵁ : U 𝟛ᵁ = 𝟙ᵁ ⊎ᵁ 𝟚ᵁ list22 = list (𝟚ᵁ →ᵁ 𝟚ᵁ) list33 = list (𝟛ᵁ →ᵁ 𝟛ᵁ) -} {- module _ {{_ : UA}}{{_ : FunExt}} where check22 : ∀ (f : 𝟚 → 𝟚) x → ✓ (f x == f (f (f x))) check22 f x = {!check! ((𝟚ᵁ →ᵁ 𝟚ᵁ) ×ᵁ 𝟚ᵁ) (λ { (f , x) → let f' = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0₂ , f 1₂) , x)!} -}
{-# OPTIONS --without-K #-} module Explore.Examples where open import Type open import Level.NP open import Data.Maybe.NP open import Data.List open import Data.Zero open import Data.One open import Data.Two open import Data.Product open import Data.Sum.NP open import HoTT using (UA) open import Function.NP open import Function.Extensionality using (FunExt) open import Relation.Binary.PropositionalEquality hiding ([_]) open import Explore.Core open import Explore.Explorable open import Explore.Universe.Type {𝟘} open import Explore.Universe.Base open import Explore.Monad {₀} ₀ public renaming (map to map-explore) open import Explore.Two open import Explore.Product open Explore.Product.Operators module E where open FromExplore public module M {Msg Digest : ★} (_==_ : Digest → Digest → 𝟚) (H : Msg → Digest) (exploreMsg : ∀ {ℓ} → Explore ℓ Msg) (d : Digest) where module V1 where list-H⁻¹ : List Msg list-H⁻¹ = exploreMsg [] _++_ (λ m → [0: [] 1: [ m ] ] (H m == d)) module ExploreMsg = FromExplore {A = Msg} exploreMsg module V2 where first-H⁻¹ : Maybe Msg first-H⁻¹ = ExploreMsg.findKey (λ m → H m == d) module V3 where explore-H⁻¹ : Explore ₀ Msg explore-H⁻¹ ε _⊕_ f = exploreMsg ε _⊕_ (λ m → [0: ε 1: f m ] (H m == d)) module V4 where explore-H⁻¹ : Explore ₀ Msg explore-H⁻¹ = exploreMsg >>= λ m → [0: empty-explore 1: point-explore m ] (H m == d) module V5 where explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg explore-H⁻¹ = filter-explore (λ m → H m == d) exploreMsg list-H⁻¹ : List Msg list-H⁻¹ = E.list explore-H⁻¹ first-H⁻¹ : Maybe Msg first-H⁻¹ = E.first explore-H⁻¹ module V6 where explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg explore-H⁻¹ = explore-endo (filter-explore (λ m → H m == d) exploreMsg) list-H⁻¹ : List Msg list-H⁻¹ = E.list explore-H⁻¹ first-H⁻¹ : Maybe Msg first-H⁻¹ = E.first explore-H⁻¹ last-H⁻¹ : Maybe Msg last-H⁻¹ = E.last explore-H⁻¹ Msg = 𝟚 × 𝟚 Digest = 𝟚 -- _==_ : Digest → Digest → 𝟚 H : Msg → Digest H (x , y) = x xor y Msgᵉ : ∀ {ℓ} → Explore ℓ Msg Msgᵉ = 𝟚ᵉ ×ᵉ 𝟚ᵉ module N5 = M.V5 _==_ H Msgᵉ module N6 = M.V6 _==_ H Msgᵉ test5 = N5.list-H⁻¹ test6-list : N6.list-H⁻¹ 0₂ ≡ (0₂ , 0₂) ∷ (1₂ , 1₂) ∷ [] test6-list = refl test6-rev-list : E.list (E.backward (N6.explore-H⁻¹ 0₂)) ≡ (1₂ , 1₂) ∷ (0₂ , 0₂) ∷ [] test6-rev-list = refl test6-first : N6.first-H⁻¹ 0₂ ≡ just (0₂ , 0₂) test6-first = refl test6-last : N6.last-H⁻¹ 0₂ ≡ just (1₂ , 1₂) test6-last = refl -- -} 𝟛ᵁ : U 𝟛ᵁ = 𝟙ᵁ ⊎ᵁ 𝟚ᵁ prop-∧-comm : 𝟚 × 𝟚 → 𝟚 prop-∧-comm (x , y) = x ∧ y == y ∧ x module _ {{_ : UA}}{{_ : FunExt}} where check-∧-comm : ∀ x y → ✓ (x ∧ y == y ∧ x) check-∧-comm x y = check! (𝟚ᵁ ×ᵁ 𝟚ᵁ) prop-∧-comm (x , y) prop-∧-∨-distr : 𝟚 × 𝟚 × 𝟚 → 𝟚 prop-∧-∨-distr (x , y , z) = x ∧ (y ∨ z) == x ∧ y ∨ x ∧ z module _ {{_ : UA}}{{_ : FunExt}} where check-∧-∨-distr : ∀ x y z → ✓ (x ∧ (y ∨ z) == x ∧ y ∨ x ∧ z) check-∧-∨-distr x y z = check! (𝟚ᵁ ×ᵁ 𝟚ᵁ ×ᵁ 𝟚ᵁ) prop-∧-∨-distr (x , y , z) list22 = list (𝟚ᵁ →ᵁ 𝟚ᵁ) list33 = list (𝟛ᵁ →ᵁ 𝟛ᵁ) {- module _ {{_ : UA}}{{_ : FunExt}} where module _ (fᵁ : El (𝟚ᵁ →ᵁ 𝟚ᵁ)) x where f = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ fᵁ check22 : ✓ (f x == f (f (f x))) check22 = check! ((𝟚ᵁ →ᵁ 𝟚ᵁ) ×ᵁ 𝟚ᵁ) (λ { (f , x) → let f' = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0₂ , f 1₂) , x) {- check22 : ∀ (f : 𝟚 → 𝟚) x → ✓ (f x == f (f (f x))) check22 f x = let k = check! ((𝟚ᵁ →ᵁ 𝟚ᵁ) ×ᵁ 𝟚ᵁ) (λ { (f , x) → let f' = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0₂ , f 1₂) , x) in {!k!} -- -} -- -} -- -} -- -}
Update the Examples
Update the Examples
Agda
bsd-3-clause
crypto-agda/explore
b4c39231ae725a5b759926a08d76642ed2df680d
Base/Change/Products.agda
Base/Change/Products.agda
module Base.Change.Products where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Data.Product -- Also try defining sectioned change structures on the positives halves of -- groups? Or on arbitrary subsets? -- Restriction: we pair sets on the same level (because right now everything -- else would risk getting in the way). module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where open ≡-Reasoning -- The simplest possible definition of changes for products. PChange : A × B → Set ℓ PChange (a , b) = Δ a × Δ b -- An interesting alternative definition allows omitting the nil change of a -- component when that nil change can be computed from the type. For instance, the nil change for integers is always the same. -- However, the nil change for function isn't always the same (unless we -- defunctionalize them first), so nil changes for functions can't be omitted. _⊕_ : (v : A × B) → PChange v → A × B _⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db _⊝_ : A × B → (v : A × B) → PChange v _⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b p-nil : (v : A × B) → PChange v p-nil (a , b) = (nil a , nil b) p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u p-update-diff (ua , ub) (va , vb) = let u = (ua , ub) v = (va , vb) in begin v ⊕ (u ⊝ v) ≡⟨⟩ (va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb)) --v ⊕ ((ua ⊟ va , ub ⊟ vb)) ≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩ (ua , ub) ≡⟨⟩ u ∎ p-update-nil : (v : A × B) → v ⊕ (p-nil v) ≡ v p-update-nil (a , b) = let v = (a , b) in begin v ⊕ (p-nil v) ≡⟨⟩ (a ⊞ nil a , b ⊞ nil b) ≡⟨ cong₂ _,_ (update-nil a) (update-nil b)⟩ (a , b) ≡⟨⟩ v ∎ changeAlgebra : ChangeAlgebra ℓ (A × B) changeAlgebra = record { Change = PChange ; update = _⊕_ ; diff = _⊝_ ; nil = p-nil ; isChangeAlgebra = record { update-diff = p-update-diff ; update-nil = p-update-nil } } proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v) proj₁′ (a , b) (da , db) = da proj₁′Derivative : Derivative proj₁ proj₁′ -- Implementation note: we do not need to pattern match on v and dv because -- they are records, hence Agda knows that pattern matching on records cannot -- fail. Technically, the required feature is the eta-rule on records. proj₁′Derivative v dv = refl -- An extended explanation. proj₁′Derivative₁ : Derivative proj₁ proj₁′ proj₁′Derivative₁ (a , b) (da , db) = let v = (a , b) dv = (da , db) in begin proj₁ v ⊞ proj₁′ v dv ≡⟨⟩ a ⊞ da ≡⟨⟩ proj₁ (v ⊞ dv) ∎ -- Same for the second extractor. proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v) proj₂′ (a , b) (da , db) = db proj₂′Derivative : Derivative proj₂ proj₂′ proj₂′Derivative v dv = refl B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B) A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}} module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}} module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}} -- Morally, the following is a change: -- What one could wrongly expect to be the derivative of the constructor: _,_′-realizer : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b) _,_′-realizer a da b db = da , db -- That has the correct behavior, in a sense, and it would be in the -- subset-based formalization in the paper. -- -- But the above is not even a change, because it does not contain a proof of -- its own validity, and because after application it does not contain a -- proof. -- -- However, the above is (morally) a "realizer" of the actual change, since it -- only represents its computational behavior, not its proof manipulation. -- Hence, we need to do some additional work. _,_′-realizer-correct _,_′-realizer-correct-detailed : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → (a , b ⊞ db) ⊞ (_,_′-realizer a da (b ⊞ db) (nil (b ⊞ db))) ≡ (a , b) ⊞ (_,_′-realizer a da b db) _,_′-realizer-correct a da b db rewrite update-nil (b ⊞ db) = refl _,_′-realizer-correct-detailed a da b db = begin (a , b ⊞ db) ⊞ (_,_′-realizer a da) (b ⊞ db) (nil (b ⊞ db)) ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) ≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩ a ⊞ da , b ⊞ db ≡⟨⟩ (a , b) ⊞ (_,_′-realizer a da) b db ∎ _,_′ : (a : A) → (da : Δ a) → Δ (_,_ a) _,_′ a da = record { apply = _,_′-realizer a da ; correct = λ b db → _,_′-realizer-correct a da b db } _,_′-Derivative : Derivative _,_ _,_′ _,_′-Derivative a da = ext (λ b → cong (_,_ (a ⊞ da)) (update-nil b)) where open import Postulate.Extensionality -- Define specialized variant of uncurry, and derive it. uncurry₀ : ∀ {C : Set ℓ} → (A → B → C) → A × B → C uncurry₀ f (a , b) = f a b module _ {C : Set ℓ} {{CC : ChangeAlgebra ℓ C}} where B→C : ChangeAlgebra ℓ (B → C) B→C = FunctionChanges.changeAlgebra B C A→B→C : ChangeAlgebra ℓ (A → B → C) A→B→C = FunctionChanges.changeAlgebra A (B → C) A×B→C : ChangeAlgebra ℓ (A × B → C) A×B→C = FunctionChanges.changeAlgebra (A × B) C module ΔB→C = FunctionChanges B C {{CB}} {{CC}} module ΔA→B→C = FunctionChanges A (B → C) {{CA}} {{B→C}} module ΔA×B→C = FunctionChanges (A × B) C {{changeAlgebra}} {{CC}} uncurry₀′-realizer : (f : A → B → C) → Δ f → (p : A × B) → Δ p → Δ (uncurry₀ f p) uncurry₀′-realizer f df (a , b) (da , db) = ΔB→C.apply (ΔA→B→C.apply df a da) b db uncurry₀′-realizer-correct uncurry₀′-realizer-correct-detailed : ∀ (f : A → B → C) (df : Δ f) (p : A × B) (dp : Δ p) → uncurry₀ f (p ⊕ dp) ⊞ uncurry₀′-realizer f df (p ⊕ dp) (nil (p ⊞ dp)) ≡ uncurry₀ f p ⊞ uncurry₀′-realizer f df p dp -- Hard to read uncurry₀′-realizer-correct f df (a , b) (da , db) rewrite sym (ΔB→C.incrementalization (f (a ⊞ da)) (ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))) | update-nil (b ⊞ db) | {- cong (λ □ → □ (b ⊞ db)) -} (sym (ΔA→B→C.incrementalization f df (a ⊞ da) (nil (a ⊞ da)))) | update-nil (a ⊞ da) | cong (λ □ → □ (b ⊞ db)) (ΔA→B→C.incrementalization f df a da) | ΔB→C.incrementalization (f a) (ΔA→B→C.apply df a da) b db = refl -- Verbose, but it shows all the intermediate steps. uncurry₀′-realizer-correct-detailed f df (a , b) (da , db) = begin uncurry₀ f (a ⊞ da , b ⊞ db) ⊞ uncurry₀′-realizer f df (a ⊞ da , b ⊞ db) (nil (a ⊞ da , b ⊞ db)) ≡⟨⟩ f (a ⊞ da) (b ⊞ db) ⊞ ΔB→C.apply (ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db)) ≡⟨ sym (ΔB→C.incrementalization (f (a ⊞ da)) (ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))) ⟩ (f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db))) ≡⟨ cong-lem₀ ⟩ (f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) ≡⟨ sym cong-lem₂ ⟩ ((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db) ≡⟨ cong-lem₁ ⟩ (f ⊞ df) (a ⊞ da) (b ⊞ db) ≡⟨ cong (λ □ → □ (b ⊞ db)) (ΔA→B→C.incrementalization f df a da) ⟩ (f a ⊞ ΔA→B→C.apply df a da) (b ⊞ db) ≡⟨ ΔB→C.incrementalization (f a) (ΔA→B→C.apply df a da) b db ⟩ f a b ⊞ ΔB→C.apply (ΔA→B→C.apply df a da) b db ≡⟨⟩ uncurry₀ f (a , b) ⊞ uncurry₀′-realizer f df (a , b) (da , db) ∎ where cong-lem₀ : (f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db))) ≡ (f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) cong-lem₀ rewrite update-nil (b ⊞ db) = refl cong-lem₁ : ((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db) ≡ (f ⊞ df) (a ⊞ da) (b ⊞ db) cong-lem₁ rewrite update-nil (a ⊞ da) = refl cong-lem₂ : ((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db) ≡ (f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) cong-lem₂ = cong (λ □ → □ (b ⊞ db)) (ΔA→B→C.incrementalization f df (a ⊞ da) (nil (a ⊞ da))) uncurry₀′ : (f : A → B → C) → Δ f → Δ (uncurry f) uncurry₀′ f df = record { apply = uncurry₀′-realizer f df ; correct = uncurry₀′-realizer-correct f df } -- Now proving that uncurry₀′ is a derivative is trivial! uncurry₀′Derivative₀ : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′ uncurry₀′Derivative₀ f df = refl -- If you wonder what's going on, here's the step-by-step proof, going purely by definitional equality. uncurry₀′Derivative : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′ uncurry₀′Derivative f df = begin uncurry₀ f ⊞ uncurry₀′ f df ≡⟨⟩ (λ {(a , b) → uncurry₀ f (a , b) ⊞ ΔA×B→C.apply (uncurry₀′ f df) (a , b) (nil (a , b))}) ≡⟨⟩ (λ {(a , b) → f a b ⊞ ΔB→C.apply (ΔA→B→C.apply df a (nil a)) b (nil b)}) ≡⟨⟩ (λ {(a , b) → (f a ⊞ ΔA→B→C.apply df a (nil a)) b}) ≡⟨⟩ (λ {(a , b) → (f ⊞ df) a b}) ≡⟨⟩ (λ {(a , b) → uncurry₀ (f ⊞ df) (a , b)}) ≡⟨⟩ uncurry₀ (f ⊞ df) ∎
module Base.Change.Products where open import Relation.Binary.PropositionalEquality open import Level open import Base.Change.Algebra open import Data.Product -- Also try defining sectioned change structures on the positives halves of -- groups? Or on arbitrary subsets? -- Restriction: we pair sets on the same level (because right now everything -- else would risk getting in the way). module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where open ≡-Reasoning -- The simplest possible definition of changes for products. PChange : A × B → Set ℓ PChange (a , b) = Δ a × Δ b -- An interesting alternative definition allows omitting the nil change of a -- component when that nil change can be computed from the type. For instance, the nil change for integers is always the same. -- However, the nil change for function isn't always the same (unless we -- defunctionalize them first), so nil changes for functions can't be omitted. _⊕_ : (v : A × B) → PChange v → A × B _⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db _⊝_ : A × B → (v : A × B) → PChange v _⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b p-nil : (v : A × B) → PChange v p-nil (a , b) = (nil a , nil b) p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u p-update-diff (ua , ub) (va , vb) = let u = (ua , ub) v = (va , vb) in begin v ⊕ (u ⊝ v) ≡⟨⟩ (va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb)) --v ⊕ ((ua ⊟ va , ub ⊟ vb)) ≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩ (ua , ub) ≡⟨⟩ u ∎ p-update-nil : (v : A × B) → v ⊕ (p-nil v) ≡ v p-update-nil (a , b) = let v = (a , b) in begin v ⊕ (p-nil v) ≡⟨⟩ (a ⊞ nil a , b ⊞ nil b) ≡⟨ cong₂ _,_ (update-nil a) (update-nil b)⟩ (a , b) ≡⟨⟩ v ∎ changeAlgebra : ChangeAlgebra ℓ (A × B) changeAlgebra = record { Change = PChange ; update = _⊕_ ; diff = _⊝_ ; nil = p-nil ; isChangeAlgebra = record { update-diff = p-update-diff ; update-nil = p-update-nil } } proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v) proj₁′ (a , b) (da , db) = da proj₁′Derivative : Derivative proj₁ proj₁′ -- Implementation note: we do not need to pattern match on v and dv because -- they are records, hence Agda knows that pattern matching on records cannot -- fail. Technically, the required feature is the eta-rule on records. proj₁′Derivative v dv = refl -- An extended explanation. proj₁′Derivative₁ : Derivative proj₁ proj₁′ proj₁′Derivative₁ (a , b) (da , db) = let v = (a , b) dv = (da , db) in begin proj₁ v ⊞ proj₁′ v dv ≡⟨⟩ a ⊞ da ≡⟨⟩ proj₁ (v ⊞ dv) ∎ -- Same for the second extractor. proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v) proj₂′ (a , b) (da , db) = db proj₂′Derivative : Derivative proj₂ proj₂′ proj₂′Derivative v dv = refl B→A×B = FunctionChanges.changeAlgebra B (A × B) A→B→A×B = FunctionChanges.changeAlgebra A (B → A × B) {{CA}} {{B→A×B}} -- Morally, the following is a change: -- What one could wrongly expect to be the derivative of the constructor: _,_′-realizer : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b) _,_′-realizer a da b db = da , db -- That has the correct behavior, in a sense, and it would be in the -- subset-based formalization in the paper. -- -- But the above is not even a change, because it does not contain a proof of -- its own validity, and because after application it does not contain a -- proof. -- -- However, the above is (morally) a "realizer" of the actual change, since it -- only represents its computational behavior, not its proof manipulation. -- Hence, we need to do some additional work. _,_′-realizer-correct _,_′-realizer-correct-detailed : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → (a , b ⊞ db) ⊞ (_,_′-realizer a da (b ⊞ db) (nil (b ⊞ db))) ≡ (a , b) ⊞ (_,_′-realizer a da b db) _,_′-realizer-correct a da b db rewrite update-nil (b ⊞ db) = refl _,_′-realizer-correct-detailed a da b db = begin (a , b ⊞ db) ⊞ (_,_′-realizer a da) (b ⊞ db) (nil (b ⊞ db)) ≡⟨⟩ a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) ≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩ a ⊞ da , b ⊞ db ≡⟨⟩ (a , b) ⊞ (_,_′-realizer a da) b db ∎ _,_′ : (a : A) → (da : Δ a) → Δ (_,_ a) _,_′ a da = record { apply = _,_′-realizer a da ; correct = λ b db → _,_′-realizer-correct a da b db } _,_′-Derivative : Derivative _,_ _,_′ _,_′-Derivative a da = ext (λ b → cong (_,_ (a ⊞ da)) (update-nil b)) where open import Postulate.Extensionality -- Define specialized variant of uncurry, and derive it. uncurry₀ : ∀ {C : Set ℓ} → (A → B → C) → A × B → C uncurry₀ f (a , b) = f a b module _ {C : Set ℓ} {{CC : ChangeAlgebra ℓ C}} where B→C : ChangeAlgebra ℓ (B → C) B→C = FunctionChanges.changeAlgebra B C A→B→C : ChangeAlgebra ℓ (A → B → C) A→B→C = FunctionChanges.changeAlgebra A (B → C) A×B→C : ChangeAlgebra ℓ (A × B → C) A×B→C = FunctionChanges.changeAlgebra (A × B) C open FunctionChanges using (apply; correct) uncurry₀′-realizer : (f : A → B → C) → Δ f → (p : A × B) → Δ p → Δ (uncurry₀ f p) uncurry₀′-realizer f df (a , b) (da , db) = apply (apply df a da) b db uncurry₀′-realizer-correct uncurry₀′-realizer-correct-detailed : ∀ (f : A → B → C) (df : Δ f) (p : A × B) (dp : Δ p) → uncurry₀ f (p ⊕ dp) ⊞ uncurry₀′-realizer f df (p ⊕ dp) (nil (p ⊞ dp)) ≡ uncurry₀ f p ⊞ uncurry₀′-realizer f df p dp -- Hard to read uncurry₀′-realizer-correct f df (a , b) (da , db) rewrite sym (correct (apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))) | update-nil (b ⊞ db) | {- cong (λ □ → □ (b ⊞ db)) -} (sym (correct df (a ⊞ da) (nil (a ⊞ da)))) | update-nil (a ⊞ da) | cong (λ □ → □ (b ⊞ db)) (correct df a da) | correct (apply df a da) b db = refl -- Verbose, but it shows all the intermediate steps. uncurry₀′-realizer-correct-detailed f df (a , b) (da , db) = begin uncurry₀ f (a ⊞ da , b ⊞ db) ⊞ uncurry₀′-realizer f df (a ⊞ da , b ⊞ db) (nil (a ⊞ da , b ⊞ db)) ≡⟨⟩ f (a ⊞ da) (b ⊞ db) ⊞ apply (apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db)) ≡⟨ sym (correct (apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))) ⟩ (f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db))) ≡⟨ cong-lem₀ ⟩ (f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) ≡⟨ sym cong-lem₂ ⟩ ((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db) ≡⟨ cong-lem₁ ⟩ (f ⊞ df) (a ⊞ da) (b ⊞ db) ≡⟨ cong (λ □ → □ (b ⊞ db)) (correct df a da) ⟩ (f a ⊞ apply df a da) (b ⊞ db) ≡⟨ correct (apply df a da) b db ⟩ f a b ⊞ apply (apply df a da) b db ≡⟨⟩ uncurry₀ f (a , b) ⊞ uncurry₀′-realizer f df (a , b) (da , db) ∎ where cong-lem₀ : (f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db))) ≡ (f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) cong-lem₀ rewrite update-nil (b ⊞ db) = refl cong-lem₁ : ((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db) ≡ (f ⊞ df) (a ⊞ da) (b ⊞ db) cong-lem₁ rewrite update-nil (a ⊞ da) = refl cong-lem₂ : ((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db) ≡ (f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) cong-lem₂ = cong (λ □ → □ (b ⊞ db)) (correct df (a ⊞ da) (nil (a ⊞ da))) uncurry₀′ : (f : A → B → C) → Δ f → Δ (uncurry f) uncurry₀′ f df = record { apply = uncurry₀′-realizer f df ; correct = uncurry₀′-realizer-correct f df } -- Now proving that uncurry₀′ is a derivative is trivial! uncurry₀′Derivative₀ : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′ uncurry₀′Derivative₀ f df = refl -- If you wonder what's going on, here's the step-by-step proof, going purely by definitional equality. uncurry₀′Derivative : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′ uncurry₀′Derivative f df = begin uncurry₀ f ⊞ uncurry₀′ f df ≡⟨⟩ (λ {(a , b) → uncurry₀ f (a , b) ⊞ apply (uncurry₀′ f df) (a , b) (nil (a , b))}) ≡⟨⟩ (λ {(a , b) → f a b ⊞ apply (apply df a (nil a)) b (nil b)}) ≡⟨⟩ (λ {(a , b) → (f a ⊞ apply df a (nil a)) b}) ≡⟨⟩ (λ {(a , b) → (f ⊞ df) a b}) ≡⟨⟩ (λ {(a , b) → uncurry₀ (f ⊞ df) (a , b)}) ≡⟨⟩ uncurry₀ (f ⊞ df) ∎
Simplify code
Base.Change.Products: Simplify code These simplifications are inspired by Base.Change.Sums. But they're still modest.
Agda
mit
inc-lc/ilc-agda
5957a0a5f21ebc9ad88ea001c4afa6b381372c04
Parametric/Change/Type.agda
Parametric/Change/Type.agda
import Parametric.Syntax.Type as Type module Parametric.Change.Type (Base : Type.Structure) where open Type.Structure Base Structure : Set Structure = Base → Base module Structure (ΔBase : Structure) where ΔType : Type → Type ΔType (base ι) = base (ΔBase ι) ΔType (σ ⇒ τ) = σ ⇒ ΔType σ ⇒ ΔType τ open import Base.Change.Context ΔType public
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Simply-typed changes (Fig. 3 and Fig. 4d) ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type module Parametric.Change.Type (Base : Type.Structure) where open Type.Structure Base -- Extension point: Simply-typed changes of base types. Structure : Set Structure = Base → Base module Structure (ΔBase : Structure) where -- We provide: Simply-typed changes on simple types. ΔType : Type → Type ΔType (base ι) = base (ΔBase ι) ΔType (σ ⇒ τ) = σ ⇒ ΔType σ ⇒ ΔType τ -- And we also provide context merging. open import Base.Change.Context ΔType public
Document P.C.Type.
Document P.C.Type. Old-commit-hash: 11e38d018843f21411d014020ce42181e1f77d32
Agda
mit
inc-lc/ilc-agda
27037592477c7d9112554d56337e06c5a2c6f0b9
Parametric/Syntax/Term.agda
Parametric/Syntax/Term.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- The syntax of terms (Fig. 1a and 1b). ------------------------------------------------------------------------ -- The syntax of terms depends on the syntax of simple types -- (because terms are indexed by types in order to rule out -- ill-typed terms). But we are in the Parametric.* hierarchy, so -- we don't know the full syntax of types, only how to lift the -- syntax of base types into the syntax of simple types. This -- means that we have to be parametric in the syntax of base -- types, too. -- -- In such parametric modules that depend on other parametric -- modules, we first import our dependencies under a more -- convenient name. import Parametric.Syntax.Type as Type -- Then we start the module proper, with parameters for all -- extension points of our dependencies. Note that here, the -- "Structure" naming convenion makes some sense, because we can -- say that we need some "Type.Structure" in order to define the -- "Term.Structure". module Parametric.Syntax.Term (Base : Type.Structure) where -- Now inside the module, we can open our dependencies with the -- parameters for their extension points. Again, here the name -- "Structure" makes some sense, because we can say that we want -- to access the "Type.Structure" that is induced by Base. open Type.Structure Base -- At this point, we have dealt with the extension points of our -- dependencies, and we have all the definitions about simple -- types, contexts, variables, and variable sets in scope that we -- provided in Parametric.Syntax.Type. Now we can proceed to -- define our own extension point, following the pattern -- explained in Parametric.Syntax.Type. open import Relation.Binary.PropositionalEquality open import Function using (_∘_) open import Data.Unit open import Data.Sum -- Our extension point is a set of primitives, indexed by the -- types of their arguments and their return type. In general, if -- you're confused about what an extension point means, you might -- want to open the corresponding module in the Nehemiah -- hierarchy to see how it is implemented in the example -- plugin. In this case, that would be the Nehemiah.Syntax.Term -- module. Structure : Set₁ Structure = Context → Type → Set module Structure (Const : Structure) where import Base.Data.DependentList as DependentList open DependentList public using (∅ ; _•_) open DependentList -- Declarations of Term and Terms to enable mutual recursion. -- -- Note that terms are indexed by contexts and types. In the -- paper, we define the abstract syntax of terms in Fig 1a and -- then define a type system in Fig 1b. All lemmas and theorems -- then explicitly specify that they only hold for well-typed -- terms. Here, we use the indices to define a type that can -- only hold well-typed terms in the first place. data Term (Γ : Context) : (τ : Type) → Set -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. Terms : Context → Context → Set Terms Γ = DependentList (Term Γ) -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where -- constants aka. primitives can only occur fully applied. const : ∀ {Σ τ} → (c : Const Σ τ) → (args : Terms Γ Σ) → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ -- we use de Bruijn indicies, so we don't need binding occurrences. abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- Free variables FV : ∀ {τ Γ} → Term Γ τ → Vars Γ FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ FV (const ι ts) = FV-terms ts FV (var x) = singleton x FV (abs t) = tail (FV t) FV (app s t) = FV s ∪ FV t FV-terms ∅ = none FV-terms (t • ts) = FV t ∪ FV-terms ts closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤ closed? t = empty? (FV t) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weaken-terms : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weaken-terms Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weaken-terms Γ₁≼Γ₂ ∅ = ∅ weaken-terms Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weaken-terms Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant) -- HOAS-like smart constructors for lambdas, for different arities. -- We could also write this: module NamespaceForBadAbs₁ where abs₁′ : ∀ {Γ τ₁ τ} → (Term (τ₁ • Γ) τ₁ → Term (τ₁ • Γ) τ) → (Term Γ (τ₁ ⇒ τ)) abs₁′ {Γ} {τ₁} = λ f → abs (f (var this)) -- However, this is less general, and it is harder to reuse. In particular, -- this cannot be used inside abs₂, ..., abs₆. -- Now, let's write other variants with a loop! open import Data.Vec using (_∷_; []; Vec; foldr) open import Data.Nat module AbsNHelpers where open import Function hoasArgType : ∀ {n} → Context → Type → Vec Type n → Set hoasArgType Γ τ = foldr _ (λ a b → a → b) (Term Γ τ) ∘ Data.Vec.map (Term Γ) -- That is, --hoasArgType Γ τ [] = Term Γ τ --hoasArgType Γ τ (τ₀ ∷ τs) = Term Γ τ₀ → hoasArgType Γ τ τs hoasResType : ∀ {n} → Type → Vec Type n → Type hoasResType τ = foldr _ _⇒_ τ absNType : {n : ℕ} → Vec _ n → Set absNType τs = ∀ {Γ τ} → (f : ∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → hoasArgType Γ′ τ τs) → Term Γ (hoasResType τ τs) -- A better type for absN but a mess to use due to the proofs (which aren't synthesized, even though maybe they should be?) -- absN : (n : ℕ) → {_ : n > 0} → absNType n -- XXX See "how to keep your neighbours in order" for tricks. -- Please the termination checker by keeping this case separate. absNBase : ∀ {τ₁} → absNType (τ₁ ∷ []) absNBase {τ₁} f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) -- Otherwise, the recursive step of absN would invoke absN twice, and the -- termination checker does not figure out that the calls are in fact -- terminating. open AbsNHelpers using (absNType; absNBase) -- XXX: could we be using the same trick as above to take the N implicit -- arguments individually, rather than in a vector? I can't figure out how, -- at least not without trying it. But it seems that's what's shown in Agda 2.4.0 release notes! absN : {n : ℕ} → (τs : Vec _ (suc n)) → absNType τs absN {zero} (τ₁ ∷ []) = absNBase absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f = absNBase (λ {_} {Γ≼Γ′} x₁ → absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) -- What I'd like to write, avoiding the need for absNBase, but can't because of the termination checker. {- absN {zero} (τ₁ ∷ []) f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f = absN (τ₁ ∷ []) (λ {_} {Γ≼Γ′} x₁ → absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) -} -- Declare abs₁ .. abs₆ wrappers for more convenient use, allowing implicit -- type arguments to be synthesized. Somehow, Agda does not manage to -- synthesize τs by unification. -- Implicit arguments are reversed when assembling the list, but that's no real problem. module _ {τ₁ : Type} where τs₁ = τ₁ ∷ [] abs₁ : absNType τs₁ abs₁ = absN τs₁ module _ {τ₂ : Type} where τs₂ = τ₂ ∷ τs₁ abs₂ : absNType τs₂ abs₂ = absN τs₂ module _ {τ₃ : Type} where τs₃ = τ₃ ∷ τs₂ abs₃ : absNType τs₃ abs₃ = absN τs₃ module _ {τ₄ : Type} where τs₄ = τ₄ ∷ τs₃ abs₄ : absNType τs₄ abs₄ = absN τs₄ module _ {τ₅ : Type} where τs₅ = τ₅ ∷ τs₄ abs₅ : absNType τs₅ abs₅ = absN τs₅ module _ {τ₆ : Type} where τs₆ = τ₆ ∷ τs₅ abs₆ : absNType τs₆ abs₆ = absN τs₆
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- The syntax of terms (Fig. 1a and 1b). ------------------------------------------------------------------------ -- The syntax of terms depends on the syntax of simple types -- (because terms are indexed by types in order to rule out -- ill-typed terms). But we are in the Parametric.* hierarchy, so -- we don't know the full syntax of types, only how to lift the -- syntax of base types into the syntax of simple types. This -- means that we have to be parametric in the syntax of base -- types, too. -- -- In such parametric modules that depend on other parametric -- modules, we first import our dependencies under a more -- convenient name. import Parametric.Syntax.Type as Type -- Then we start the module proper, with parameters for all -- extension points of our dependencies. Note that here, the -- "Structure" naming convenion makes some sense, because we can -- say that we need some "Type.Structure" in order to define the -- "Term.Structure". module Parametric.Syntax.Term (Base : Type.Structure) where -- Now inside the module, we can open our dependencies with the -- parameters for their extension points. Again, here the name -- "Structure" makes some sense, because we can say that we want -- to access the "Type.Structure" that is induced by Base. open Type.Structure Base -- At this point, we have dealt with the extension points of our -- dependencies, and we have all the definitions about simple -- types, contexts, variables, and variable sets in scope that we -- provided in Parametric.Syntax.Type. Now we can proceed to -- define our own extension point, following the pattern -- explained in Parametric.Syntax.Type. open import Relation.Binary.PropositionalEquality hiding ([_]) open import Function using (_∘_) open import Data.Unit open import Data.Sum -- Our extension point is a set of primitives, indexed by the -- types of their arguments and their return type. In general, if -- you're confused about what an extension point means, you might -- want to open the corresponding module in the Nehemiah -- hierarchy to see how it is implemented in the example -- plugin. In this case, that would be the Nehemiah.Syntax.Term -- module. Structure : Set₁ Structure = Context → Type → Set module Structure (Const : Structure) where import Base.Data.DependentList as DependentList open DependentList public using (∅ ; _•_) open DependentList -- Declarations of Term and Terms to enable mutual recursion. -- -- Note that terms are indexed by contexts and types. In the -- paper, we define the abstract syntax of terms in Fig 1a and -- then define a type system in Fig 1b. All lemmas and theorems -- then explicitly specify that they only hold for well-typed -- terms. Here, we use the indices to define a type that can -- only hold well-typed terms in the first place. data Term (Γ : Context) : (τ : Type) → Set -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. Terms : Context → Context → Set Terms Γ = DependentList (Term Γ) -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where -- constants aka. primitives can only occur fully applied. const : ∀ {Σ τ} → (c : Const Σ τ) → (args : Terms Γ Σ) → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ -- we use de Bruijn indicies, so we don't need binding occurrences. abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- Free variables FV : ∀ {τ Γ} → Term Γ τ → Vars Γ FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ FV (const ι ts) = FV-terms ts FV (var x) = singleton x FV (abs t) = tail (FV t) FV (app s t) = FV s ∪ FV t FV-terms ∅ = none FV-terms (t • ts) = FV t ∪ FV-terms ts closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤ closed? t = empty? (FV t) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weaken-terms : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weaken-terms Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weaken-terms Γ₁≼Γ₂ ∅ = ∅ weaken-terms Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weaken-terms Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant) -- HOAS-like smart constructors for lambdas, for different arities. -- We could also write this: module NamespaceForBadAbs₁ where abs₁′ : ∀ {Γ τ₁ τ} → (Term (τ₁ • Γ) τ₁ → Term (τ₁ • Γ) τ) → (Term Γ (τ₁ ⇒ τ)) abs₁′ {Γ} {τ₁} = λ f → abs (f (var this)) -- However, this is less general, and it is harder to reuse. In particular, -- this cannot be used inside abs₂, ..., abs₆. -- Now, let's write other variants with a loop! open import Data.Vec using (_∷_; []; Vec; foldr; [_]) open import Data.Nat module AbsNHelpers where open import Function hoasArgType : ∀ {n} → Context → Type → Vec Type n → Set hoasArgType Γ τ = foldr _ (λ a b → a → b) (Term Γ τ) ∘ Data.Vec.map (Term Γ) -- That is, --hoasArgType Γ τ [] = Term Γ τ --hoasArgType Γ τ (τ₀ ∷ τs) = Term Γ τ₀ → hoasArgType Γ τ τs hoasResType : ∀ {n} → Type → Vec Type n → Type hoasResType τ = foldr _ _⇒_ τ absNType : {n : ℕ} → Vec _ n → Set absNType τs = ∀ {Γ τ} → (f : ∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → hoasArgType Γ′ τ τs) → Term Γ (hoasResType τ τs) -- A better type for absN but a mess to use due to the proofs (which aren't synthesized, even though maybe they should be?) -- absN : (n : ℕ) → {_ : n > 0} → absNType n -- XXX See "how to keep your neighbours in order" for tricks. -- Please the termination checker by keeping this case separate. absNBase : ∀ {τ₁} → absNType [ τ₁ ] absNBase {τ₁} f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) -- Otherwise, the recursive step of absN would invoke absN twice, and the -- termination checker does not figure out that the calls are in fact -- terminating. -- What I'd like to write, avoiding the need for absNBase, but can't because of the termination checker. {- absN {zero} (τ₁ ∷ []) f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f = absN (τ₁ ∷ []) (λ {_} {Γ≼Γ′} x₁ → absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) -} --What I have to write instead: absN : {n : ℕ} → (τs : Vec _ (suc n)) → absNType τs absN {zero} (τ₁ ∷ []) = absNBase absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f = absNBase (λ {_} {Γ≼Γ′} x₁ → absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) -- Using a similar trick, we can declare absV which takes the N implicit -- type arguments individually, collects them and passes them on to absN. -- This is inspired by what's shown in the Agda 2.4.0 release notes, and -- relies critically on support for varying arity. To collect them, we need -- to use an accumulator argument. absVType : ∀ n {m} (τs : Vec Type m) → Set absVType 0 τs = absNType τs absVType (suc n) τs = {τᵢ : Type} → absVType n (τᵢ ∷ τs) -- XXX absVAux : ∀ {m} → (τs : Vec Type m) → ∀ n → absVType (suc n) τs absVAux τs zero {τᵢ} = absN (τᵢ ∷ τs) absVAux τs (suc n) {τᵢ} = absVAux (τᵢ ∷ τs) n absV = absVAux [] open AbsNHelpers using (absV) public -- Declare abs₁ .. abs₆ wrappers for more convenient use, allowing implicit -- type arguments to be synthesized. Somehow, Agda does not manage to -- synthesize τs by unification. -- Implicit arguments are reversed when assembling the list, but that's no real problem. abs₁ = absV 0 abs₂ = absV 1 abs₃ = absV 2 abs₄ = absV 3 abs₅ = absV 4 abs₆ = absV 5 {- abs₁ Have: {τ₁ : Type} {Γ : Context} {τ : Type} → ({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ) → Term Γ (τ₁ Type.Structure.⇒ τ) abs₂ Have: {τ₁ : Type} {τ₁ = τ₂ : Type} {Γ : Context} {τ : Type} → ({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₂ → Term Γ′ τ₁ → Term Γ′ τ) → Term Γ (τ₂ Type.Structure.⇒ τ₁ Type.Structure.⇒ τ) -}
Remove remaining duplication between abs₁ ... abs₆
Remove remaining duplication between abs₁ ... abs₆ This relies on Agda 2.4.0's varying arity in a more essential way.
Agda
mit
inc-lc/ilc-agda
05536450804504640dcab4d2a372088bbc7ef7b3
src/fot/FOTC/Program/McCarthy91/PropertiesATP.agda
src/fot/FOTC/Program/McCarthy91/PropertiesATP.agda
------------------------------------------------------------------------------ -- Main properties of the McCarthy 91 function ------------------------------------------------------------------------------ {-# OPTIONS --no-universe-polymorphism #-} {-# OPTIONS --without-K #-} -- The main properties proved of the McCarthy 91 function (called -- f₉₁) are -- 1. The function always terminates. -- 2. For all n, n < f₉₁ n + 11. -- 3. For all n > 100, then f₉₁ n = n - 10. -- 4. For all n <= 100, then f₉₁ n = 91. -- N.B This module does not contain combined proofs, but it imports -- modules which contain combined proofs. module FOTC.Program.McCarthy91.PropertiesATP where open import FOTC.Base open import FOTC.Data.Nat open import FOTC.Data.Nat.Inequalities open import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP using ( x>y→x≤y→⊥ ) open import FOTC.Data.Nat.Inequalities.PropertiesATP using ( x>y∨x≯y ; x≯y→x≤y ; x≯Sy→x≯y∨x≡Sy ; x+k<y+k→x<y ; <-trans ) open import FOTC.Data.Nat.PropertiesATP using ( ∸-N ; +-N ) open import FOTC.Data.Nat.UnaryNumbers open import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP using ( x<x+11 ) open import FOTC.Data.Nat.UnaryNumbers.TotalityATP using ( 10-N ; 11-N ; 89-N ; 90-N ; 91-N ; 92-N ; 93-N ; 94-N ; 95-N ; 96-N ; 97-N ; 98-N ; 99-N ; 100-N ) open import FOTC.Program.McCarthy91.ArithmeticATP open import FOTC.Program.McCarthy91.AuxiliaryPropertiesATP open import FOTC.Program.McCarthy91.McCarthy91 open import FOTC.Program.McCarthy91.WF-Relation open import FOTC.Program.McCarthy91.WF-Relation.LT2WF-RelationATP open import FOTC.Program.McCarthy91.WF-Relation.Induction.Acc.WF-ATP ------------------------------------------------------------------------------ -- For all n > 100, then f₉₁ n = n - 10. -- -- N.B. (21 November 2013). The hypothesis N n is not necessary. postulate f₉₁-x>100 : ∀ n → N n → n > 100' → f₉₁ n ≡ n ∸ 10' {-# ATP prove f₉₁-x>100 #-} -- For all n <= 100, then f₉₁ n = 91. f₉₁-x≯100 : ∀ {n} → N n → n ≯ 100' → f₉₁ n ≡ 91' f₉₁-x≯100 = ◁-wfind A h where A : D → Set A d = d ≯ 100' → f₉₁ d ≡ 91' h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m h {m} Nm f with x>y∨x≯y Nm 100-N ... | inj₁ m>100 = λ m≯100 → ⊥-elim (x>y→x≤y→⊥ Nm 100-N m>100 (x≯y→x≤y Nm 100-N m≯100)) ... | inj₂ m≯100 with x≯Sy→x≯y∨x≡Sy Nm 99-N m≯100 ... | inj₂ m≡100 = λ _ → f₉₁-x≡y f₉₁-100 m≡100 ... | inj₁ m≯99 with x≯Sy→x≯y∨x≡Sy Nm 98-N m≯99 ... | inj₂ m≡99 = λ _ → f₉₁-x≡y f₉₁-99 m≡99 ... | inj₁ m≯98 with x≯Sy→x≯y∨x≡Sy Nm 97-N m≯98 ... | inj₂ m≡98 = λ _ → f₉₁-x≡y f₉₁-98 m≡98 ... | inj₁ m≯97 with x≯Sy→x≯y∨x≡Sy Nm 96-N m≯97 ... | inj₂ m≡97 = λ _ → f₉₁-x≡y f₉₁-97 m≡97 ... | inj₁ m≯96 with x≯Sy→x≯y∨x≡Sy Nm 95-N m≯96 ... | inj₂ m≡96 = λ _ → f₉₁-x≡y f₉₁-96 m≡96 ... | inj₁ m≯95 with x≯Sy→x≯y∨x≡Sy Nm 94-N m≯95 ... | inj₂ m≡95 = λ _ → f₉₁-x≡y f₉₁-95 m≡95 ... | inj₁ m≯94 with x≯Sy→x≯y∨x≡Sy Nm 93-N m≯94 ... | inj₂ m≡94 = λ _ → f₉₁-x≡y f₉₁-94 m≡94 ... | inj₁ m≯93 with x≯Sy→x≯y∨x≡Sy Nm 92-N m≯93 ... | inj₂ m≡93 = λ _ → f₉₁-x≡y f₉₁-93 m≡93 ... | inj₁ m≯92 with x≯Sy→x≯y∨x≡Sy Nm 91-N m≯92 ... | inj₂ m≡92 = λ _ → f₉₁-x≡y f₉₁-92 m≡92 ... | inj₁ m≯91 with x≯Sy→x≯y∨x≡Sy Nm 90-N m≯91 ... | inj₂ m≡91 = λ _ → f₉₁-x≡y f₉₁-91 m≡91 ... | inj₁ m≯90 with x≯Sy→x≯y∨x≡Sy Nm 89-N m≯90 ... | inj₂ m≡90 = λ _ → f₉₁-x≡y f₉₁-90 m≡90 ... | inj₁ m≯89 = λ _ → f₉₁-m≯89 where m≤89 : m ≤ 89' m≤89 = x≯y→x≤y Nm 89-N m≯89 f₉₁-m+11 : m + 11' ≯ 100' → f₉₁ (m + 11') ≡ 91' f₉₁-m+11 = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm)) f₉₁-m≯89 : f₉₁ m ≡ 91' f₉₁-m≯89 = f₉₁-x≯100-helper m 91' m≯100 (f₉₁-m+11 (x≤89→x+11≯100 Nm m≤89)) f₉₁-91 -- The function always terminates. f₉₁-N : ∀ {n} → N n → N (f₉₁ n) f₉₁-N {n} Nn with x>y∨x≯y Nn 100-N ... | inj₁ n>100 = subst N (sym (f₉₁-x>100 n Nn n>100)) (∸-N Nn 10-N) ... | inj₂ n≯100 = subst N (sym (f₉₁-x≯100 Nn n≯100)) 91-N -- For all n, n < f₉₁ n + 11. f₉₁-ineq : ∀ {n} → N n → n < f₉₁ n + 11' f₉₁-ineq = ◁-wfind A h where A : D → Set A d = d < f₉₁ d + 11' h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m h {m} Nm f with x>y∨x≯y Nm 100-N ... | inj₁ m>100 = x>100→x<f₉₁-x+11 Nm m>100 ... | inj₂ m≯100 = let f₉₁-m+11-N : N (f₉₁ (m + 11')) f₉₁-m+11-N = f₉₁-N (+-N Nm 11-N) h₁ : A (m + 11') h₁ = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm)) m<f₉₁m+11 : m < f₉₁ (m + 11') m<f₉₁m+11 = x+k<y+k→x<y Nm f₉₁-m+11-N 11-N h₁ h₂ : A (f₉₁ (m + 11')) h₂ = f f₉₁-m+11-N (<→◁ f₉₁-m+11-N Nm m≯100 m<f₉₁m+11) in (<-trans Nm f₉₁-m+11-N (x+11-N (f₉₁-N Nm)) m<f₉₁m+11 (f₉₁x+11<f₉₁x+11 m m≯100 h₂))
------------------------------------------------------------------------------ -- Main properties of the McCarthy 91 function ------------------------------------------------------------------------------ {-# OPTIONS --no-universe-polymorphism #-} {-# OPTIONS --without-K #-} -- The main properties proved of the McCarthy 91 function (called -- f₉₁) are -- 1. The function always terminates. -- 2. For all n, n < f₉₁ n + 11. -- 3. For all n > 100, then f₉₁ n = n - 10. -- 4. For all n <= 100, then f₉₁ n = 91. -- N.B This module does not contain combined proofs, but it imports -- modules which contain combined proofs. module FOTC.Program.McCarthy91.PropertiesATP where open import FOTC.Base open import FOTC.Data.Nat open import FOTC.Data.Nat.Inequalities open import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP using ( x>y→x≤y→⊥ ) open import FOTC.Data.Nat.Inequalities.PropertiesATP using ( x>y∨x≯y ; x≯y→x≤y ; x≯Sy→x≯y∨x≡Sy ; x+k<y+k→x<y ; <-trans ) open import FOTC.Data.Nat.PropertiesATP using ( ∸-N ; +-N ) open import FOTC.Data.Nat.UnaryNumbers open import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP using ( x<x+11 ) open import FOTC.Data.Nat.UnaryNumbers.TotalityATP using ( 10-N ; 11-N ; 89-N ; 90-N ; 91-N ; 92-N ; 93-N ; 94-N ; 95-N ; 96-N ; 97-N ; 98-N ; 99-N ; 100-N ) open import FOTC.Program.McCarthy91.ArithmeticATP open import FOTC.Program.McCarthy91.AuxiliaryPropertiesATP open import FOTC.Program.McCarthy91.McCarthy91 open import FOTC.Program.McCarthy91.WF-Relation open import FOTC.Program.McCarthy91.WF-Relation.LT2WF-RelationATP open import FOTC.Program.McCarthy91.WF-Relation.Induction.Acc.WF-ATP ------------------------------------------------------------------------------ -- For all n > 100, then f₉₁ n = n - 10. postulate f₉₁-x>100 : ∀ n → n > 100' → f₉₁ n ≡ n ∸ 10' {-# ATP prove f₉₁-x>100 #-} -- For all n <= 100, then f₉₁ n = 91. f₉₁-x≯100 : ∀ {n} → N n → n ≯ 100' → f₉₁ n ≡ 91' f₉₁-x≯100 = ◁-wfind A h where A : D → Set A d = d ≯ 100' → f₉₁ d ≡ 91' h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m h {m} Nm f with x>y∨x≯y Nm 100-N ... | inj₁ m>100 = λ m≯100 → ⊥-elim (x>y→x≤y→⊥ Nm 100-N m>100 (x≯y→x≤y Nm 100-N m≯100)) ... | inj₂ m≯100 with x≯Sy→x≯y∨x≡Sy Nm 99-N m≯100 ... | inj₂ m≡100 = λ _ → f₉₁-x≡y f₉₁-100 m≡100 ... | inj₁ m≯99 with x≯Sy→x≯y∨x≡Sy Nm 98-N m≯99 ... | inj₂ m≡99 = λ _ → f₉₁-x≡y f₉₁-99 m≡99 ... | inj₁ m≯98 with x≯Sy→x≯y∨x≡Sy Nm 97-N m≯98 ... | inj₂ m≡98 = λ _ → f₉₁-x≡y f₉₁-98 m≡98 ... | inj₁ m≯97 with x≯Sy→x≯y∨x≡Sy Nm 96-N m≯97 ... | inj₂ m≡97 = λ _ → f₉₁-x≡y f₉₁-97 m≡97 ... | inj₁ m≯96 with x≯Sy→x≯y∨x≡Sy Nm 95-N m≯96 ... | inj₂ m≡96 = λ _ → f₉₁-x≡y f₉₁-96 m≡96 ... | inj₁ m≯95 with x≯Sy→x≯y∨x≡Sy Nm 94-N m≯95 ... | inj₂ m≡95 = λ _ → f₉₁-x≡y f₉₁-95 m≡95 ... | inj₁ m≯94 with x≯Sy→x≯y∨x≡Sy Nm 93-N m≯94 ... | inj₂ m≡94 = λ _ → f₉₁-x≡y f₉₁-94 m≡94 ... | inj₁ m≯93 with x≯Sy→x≯y∨x≡Sy Nm 92-N m≯93 ... | inj₂ m≡93 = λ _ → f₉₁-x≡y f₉₁-93 m≡93 ... | inj₁ m≯92 with x≯Sy→x≯y∨x≡Sy Nm 91-N m≯92 ... | inj₂ m≡92 = λ _ → f₉₁-x≡y f₉₁-92 m≡92 ... | inj₁ m≯91 with x≯Sy→x≯y∨x≡Sy Nm 90-N m≯91 ... | inj₂ m≡91 = λ _ → f₉₁-x≡y f₉₁-91 m≡91 ... | inj₁ m≯90 with x≯Sy→x≯y∨x≡Sy Nm 89-N m≯90 ... | inj₂ m≡90 = λ _ → f₉₁-x≡y f₉₁-90 m≡90 ... | inj₁ m≯89 = λ _ → f₉₁-m≯89 where m≤89 : m ≤ 89' m≤89 = x≯y→x≤y Nm 89-N m≯89 f₉₁-m+11 : m + 11' ≯ 100' → f₉₁ (m + 11') ≡ 91' f₉₁-m+11 = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm)) f₉₁-m≯89 : f₉₁ m ≡ 91' f₉₁-m≯89 = f₉₁-x≯100-helper m 91' m≯100 (f₉₁-m+11 (x≤89→x+11≯100 Nm m≤89)) f₉₁-91 -- The function always terminates. f₉₁-N : ∀ {n} → N n → N (f₉₁ n) f₉₁-N {n} Nn with x>y∨x≯y Nn 100-N ... | inj₁ n>100 = subst N (sym (f₉₁-x>100 n n>100)) (∸-N Nn 10-N) ... | inj₂ n≯100 = subst N (sym (f₉₁-x≯100 Nn n≯100)) 91-N -- For all n, n < f₉₁ n + 11. f₉₁-ineq : ∀ {n} → N n → n < f₉₁ n + 11' f₉₁-ineq = ◁-wfind A h where A : D → Set A d = d < f₉₁ d + 11' h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m h {m} Nm f with x>y∨x≯y Nm 100-N ... | inj₁ m>100 = x>100→x<f₉₁-x+11 Nm m>100 ... | inj₂ m≯100 = let f₉₁-m+11-N : N (f₉₁ (m + 11')) f₉₁-m+11-N = f₉₁-N (+-N Nm 11-N) h₁ : A (m + 11') h₁ = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm)) m<f₉₁m+11 : m < f₉₁ (m + 11') m<f₉₁m+11 = x+k<y+k→x<y Nm f₉₁-m+11-N 11-N h₁ h₂ : A (f₉₁ (m + 11')) h₂ = f f₉₁-m+11-N (<→◁ f₉₁-m+11-N Nm m≯100 m<f₉₁m+11) in (<-trans Nm f₉₁-m+11-N (x+11-N (f₉₁-N Nm)) m<f₉₁m+11 (f₉₁x+11<f₉₁x+11 m m≯100 h₂))
Revert "Added a non-required totality hypothesis."
Revert "Added a non-required totality hypothesis." This reverts commit 57708b770c00d1ba6276cda0f02752e147f2f533.
Agda
mit
asr/fotc,asr/fotc
f04e665aed5f41deea97dd4b7160ad5ee957e41a
Nehemiah/Change/Validity.agda
Nehemiah/Change/Validity.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Dependently typed changes with the Nehemiah plugin. ------------------------------------------------------------------------ module Nehemiah.Change.Validity where open import Nehemiah.Syntax.Type open import Nehemiah.Denotation.Value import Parametric.Change.Validity ⟦_⟧Base as Validity open import Nehemiah.Change.Type open import Nehemiah.Change.Value open import Data.Integer open import Structure.Bag.Nehemiah open import Base.Change.Algebra open import Level change-algebra-base : ∀ ι → ChangeAlgebra zero ⟦ ι ⟧Base change-algebra-base base-int = GroupChanges.changeAlgebra ℤ change-algebra-base base-bag = GroupChanges.changeAlgebra Bag change-algebra-base-family : ChangeAlgebraFamily zero ⟦_⟧Base change-algebra-base-family = family change-algebra-base open Validity.Structure change-algebra-base-family public
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Dependently typed changes with the Nehemiah plugin. ------------------------------------------------------------------------ module Nehemiah.Change.Validity where open import Nehemiah.Syntax.Type open import Nehemiah.Denotation.Value import Parametric.Change.Validity ⟦_⟧Base as Validity open import Nehemiah.Change.Type open import Nehemiah.Change.Value open import Data.Integer open import Structure.Bag.Nehemiah open import Base.Change.Algebra open import Level change-algebra-base : ∀ ι → ChangeAlgebra zero ⟦ ι ⟧Base change-algebra-base base-int = GroupChanges.changeAlgebra ℤ {{abelian-int}} change-algebra-base base-bag = GroupChanges.changeAlgebra Bag {{abelian-bag}} instance change-algebra-base-family : ChangeAlgebraFamily zero ⟦_⟧Base change-algebra-base-family = family change-algebra-base --open Validity.Structure change-algebra-base-family public --XXX agda/agda#1985. open Validity.Structure public
Update Nehemiah.Change.Validity
Update Nehemiah.Change.Validity * Adapt to weaker instance resolution. * Omit module application that runs into agda/agda#1985.
Agda
mit
inc-lc/ilc-agda
94c667f3d542af3d0d5f3f603fe4d1aeccaf6e95
New/Changes.agda
New/Changes.agda
module New.Changes where open import Relation.Binary.PropositionalEquality open import Level open import Data.Unit open import Data.Product record IsChAlg {ℓ : Level} (A : Set ℓ) (Ch : Set ℓ) : Set (suc ℓ) where field _⊕_ : A → Ch → A _⊝_ : A → A → Ch valid : A → Ch → Set ℓ ⊝-valid : ∀ (a b : A) → valid a (b ⊝ a) ⊕-⊝ : (b a : A) → a ⊕ (b ⊝ a) ≡ b infixl 6 _⊕_ _⊝_ Δ : A → Set ℓ Δ a = Σ[ da ∈ Ch ] (valid a da) update-diff = ⊕-⊝ nil : A → Ch nil a = a ⊝ a nil-valid : (a : A) → valid a (nil a) nil-valid a = ⊝-valid a a update-nil : (a : A) → a ⊕ nil a ≡ a update-nil a = update-diff a a record ChAlg {ℓ : Level} (A : Set ℓ) : Set (suc ℓ) where field Ch : Set ℓ isChAlg : IsChAlg A Ch open IsChAlg isChAlg public open ChAlg {{...}} public hiding (Ch) Ch : ∀ {ℓ} (A : Set ℓ) → {{CA : ChAlg A}} → Set ℓ Ch A {{CA}} = ChAlg.Ch CA -- Huge hack, but it does gives the output you want to see in all cases I've seen. {-# DISPLAY IsChAlg.valid x = valid #-} {-# DISPLAY ChAlg.valid x = valid #-} {-# DISPLAY IsChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY ChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY IsChAlg.nil x = nil #-} {-# DISPLAY ChAlg.nil x = nil #-} {-# DISPLAY IsChAlg._⊝_ x = _⊝_ #-} {-# DISPLAY ChAlg._⊝_ x = _⊝_ #-} module _ {ℓ₁} {ℓ₂} {A : Set ℓ₁} {B : Set ℓ₂} {{CA : ChAlg A}} {{CB : ChAlg B}} where private fCh = A → Ch A → Ch B _f⊕_ : (A → B) → fCh → A → B _f⊕_ = λ f df a → f a ⊕ df a (nil a) _f⊝_ : (g f : A → B) → fCh _f⊝_ = λ g f a da → g (a ⊕ da) ⊝ f a open ≡-Reasoning open import Postulate.Extensionality IsDerivative : ∀ (f : A → B) → (df : fCh) → Set (ℓ₁ ⊔ ℓ₂) IsDerivative f df = ∀ a da (v : valid a da) → f (a ⊕ da) ≡ f a ⊕ df a da instance funCA : ChAlg (A → B) private funUpdateDiff : ∀ g f a → (f f⊕ (g f⊝ f)) a ≡ g a funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a) funCA = record { Ch = A → Ch A → Ch B ; isChAlg = record { _⊕_ = _f⊕_ ; _⊝_ = _f⊝_ ; valid = λ f df → ∀ a da (v : valid a da) → valid (f a) (df a da) × (f f⊕ df) (a ⊕ da) ≡ f a ⊕ df a da ; ⊝-valid = λ f g a da (v : valid a da) → ⊝-valid (f a) (g (a ⊕ da)) , ( begin f (a ⊕ da) ⊕ (g (a ⊕ da ⊕ nil (a ⊕ da)) ⊝ f (a ⊕ da)) ≡⟨ cong (λ □ → f (a ⊕ da) ⊕ (g □ ⊝ f (a ⊕ da))) (update-nil (a ⊕ da)) ⟩ f (a ⊕ da) ⊕ (g (a ⊕ da) ⊝ f (a ⊕ da)) ≡⟨ update-diff (g (a ⊕ da)) (f (a ⊕ da)) ⟩ g (a ⊕ da) ≡⟨ sym (update-diff (g (a ⊕ da)) (f a)) ⟩ f a ⊕ (g (a ⊕ da) ⊝ f a) ∎) ; ⊕-⊝ = λ g f → ext (funUpdateDiff g f) } } nil-is-derivative : ∀ (f : A → B) → IsDerivative f (nil f) nil-is-derivative f a da v = begin f (a ⊕ da) ≡⟨ sym (cong (λ □ → □ (_⊕_ a da)) (update-nil f)) ⟩ (f ⊕ nil f) (a ⊕ da) ≡⟨ proj₂ (nil-valid f a da v) ⟩ f a ⊕ (nil f a da) ∎ private _p⊕_ : A × B → Ch A × Ch B → A × B _p⊕_ (a , b) (da , db) = a ⊕ da , b ⊕ db _p⊝_ : A × B → A × B → Ch A × Ch B _p⊝_ (a2 , b2) (a1 , b1) = a2 ⊝ a1 , b2 ⊝ b1 pvalid : A × B → Ch A × Ch B → Set (ℓ₂ ⊔ ℓ₁) pvalid (a , b) (da , db) = valid a da × valid b db p⊝-valid : (p1 p2 : A × B) → pvalid p1 (p2 p⊝ p1) p⊝-valid (a1 , b1) (a2 , b2) = ⊝-valid a1 a2 , ⊝-valid b1 b2 p⊕-⊝ : (p2 p1 : A × B) → p1 p⊕ (p2 p⊝ p1) ≡ p2 p⊕-⊝ (a2 , b2) (a1 , b1) = cong₂ _,_ (⊕-⊝ a2 a1) (⊕-⊝ b2 b1) instance pairCA : ChAlg (A × B) pairCA = record { Ch = Ch A × Ch B ; isChAlg = record { _⊕_ = _p⊕_ ; _⊝_ = _p⊝_ ; valid = pvalid ; ⊝-valid = p⊝-valid ; ⊕-⊝ = p⊕-⊝ } } open import Data.Integer open import Theorem.Groups-Nehemiah instance intCA : ChAlg ℤ intCA = record { Ch = ℤ ; isChAlg = record { _⊕_ = _+_ ; _⊝_ = _-_ ; valid = λ a b → ⊤ ; ⊝-valid = λ a b → tt ; ⊕-⊝ = λ b a → n+[m-n]=m {a} {b} } }
module New.Changes where open import Relation.Binary.PropositionalEquality open import Level open import Data.Unit open import Data.Product open import Data.Sum record IsChAlg {ℓ : Level} (A : Set ℓ) (Ch : Set ℓ) : Set (suc ℓ) where field _⊕_ : A → Ch → A _⊝_ : A → A → Ch valid : A → Ch → Set ℓ ⊝-valid : ∀ (a b : A) → valid a (b ⊝ a) ⊕-⊝ : (b a : A) → a ⊕ (b ⊝ a) ≡ b infixl 6 _⊕_ _⊝_ Δ : A → Set ℓ Δ a = Σ[ da ∈ Ch ] (valid a da) update-diff = ⊕-⊝ nil : A → Ch nil a = a ⊝ a nil-valid : (a : A) → valid a (nil a) nil-valid a = ⊝-valid a a update-nil : (a : A) → a ⊕ nil a ≡ a update-nil a = update-diff a a record ChAlg {ℓ : Level} (A : Set ℓ) : Set (suc ℓ) where field Ch : Set ℓ isChAlg : IsChAlg A Ch open IsChAlg isChAlg public open ChAlg {{...}} public hiding (Ch) Ch : ∀ {ℓ} (A : Set ℓ) → {{CA : ChAlg A}} → Set ℓ Ch A {{CA}} = ChAlg.Ch CA -- Huge hack, but it does gives the output you want to see in all cases I've seen. {-# DISPLAY IsChAlg.valid x = valid #-} {-# DISPLAY ChAlg.valid x = valid #-} {-# DISPLAY IsChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY ChAlg._⊕_ x = _⊕_ #-} {-# DISPLAY IsChAlg.nil x = nil #-} {-# DISPLAY ChAlg.nil x = nil #-} {-# DISPLAY IsChAlg._⊝_ x = _⊝_ #-} {-# DISPLAY ChAlg._⊝_ x = _⊝_ #-} module _ {ℓ₁} {ℓ₂} {A : Set ℓ₁} {B : Set ℓ₂} {{CA : ChAlg A}} {{CB : ChAlg B}} where private fCh = A → Ch A → Ch B _f⊕_ : (A → B) → fCh → A → B _f⊕_ = λ f df a → f a ⊕ df a (nil a) _f⊝_ : (g f : A → B) → fCh _f⊝_ = λ g f a da → g (a ⊕ da) ⊝ f a open ≡-Reasoning open import Postulate.Extensionality IsDerivative : ∀ (f : A → B) → (df : fCh) → Set (ℓ₁ ⊔ ℓ₂) IsDerivative f df = ∀ a da (v : valid a da) → f (a ⊕ da) ≡ f a ⊕ df a da instance funCA : ChAlg (A → B) private funUpdateDiff : ∀ g f a → (f f⊕ (g f⊝ f)) a ≡ g a funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a) funCA = record { Ch = A → Ch A → Ch B ; isChAlg = record { _⊕_ = _f⊕_ ; _⊝_ = _f⊝_ ; valid = λ f df → ∀ a da (v : valid a da) → valid (f a) (df a da) × (f f⊕ df) (a ⊕ da) ≡ f a ⊕ df a da ; ⊝-valid = λ f g a da (v : valid a da) → ⊝-valid (f a) (g (a ⊕ da)) , ( begin f (a ⊕ da) ⊕ (g (a ⊕ da ⊕ nil (a ⊕ da)) ⊝ f (a ⊕ da)) ≡⟨ cong (λ □ → f (a ⊕ da) ⊕ (g □ ⊝ f (a ⊕ da))) (update-nil (a ⊕ da)) ⟩ f (a ⊕ da) ⊕ (g (a ⊕ da) ⊝ f (a ⊕ da)) ≡⟨ update-diff (g (a ⊕ da)) (f (a ⊕ da)) ⟩ g (a ⊕ da) ≡⟨ sym (update-diff (g (a ⊕ da)) (f a)) ⟩ f a ⊕ (g (a ⊕ da) ⊝ f a) ∎) ; ⊕-⊝ = λ g f → ext (funUpdateDiff g f) } } nil-is-derivative : ∀ (f : A → B) → IsDerivative f (nil f) nil-is-derivative f a da v = begin f (a ⊕ da) ≡⟨ sym (cong (λ □ → □ (_⊕_ a da)) (update-nil f)) ⟩ (f ⊕ nil f) (a ⊕ da) ≡⟨ proj₂ (nil-valid f a da v) ⟩ f a ⊕ (nil f a da) ∎ private _p⊕_ : A × B → Ch A × Ch B → A × B _p⊕_ (a , b) (da , db) = a ⊕ da , b ⊕ db _p⊝_ : A × B → A × B → Ch A × Ch B _p⊝_ (a2 , b2) (a1 , b1) = a2 ⊝ a1 , b2 ⊝ b1 pvalid : A × B → Ch A × Ch B → Set (ℓ₂ ⊔ ℓ₁) pvalid (a , b) (da , db) = valid a da × valid b db p⊝-valid : (p1 p2 : A × B) → pvalid p1 (p2 p⊝ p1) p⊝-valid (a1 , b1) (a2 , b2) = ⊝-valid a1 a2 , ⊝-valid b1 b2 p⊕-⊝ : (p2 p1 : A × B) → p1 p⊕ (p2 p⊝ p1) ≡ p2 p⊕-⊝ (a2 , b2) (a1 , b1) = cong₂ _,_ (⊕-⊝ a2 a1) (⊕-⊝ b2 b1) instance pairCA : ChAlg (A × B) pairCA = record { Ch = Ch A × Ch B ; isChAlg = record { _⊕_ = _p⊕_ ; _⊝_ = _p⊝_ ; valid = pvalid ; ⊝-valid = p⊝-valid ; ⊕-⊝ = p⊕-⊝ } } private SumChange = (Ch A ⊎ Ch B) ⊎ (A ⊎ B) data SumChange2 : Set (ℓ₁ ⊔ ℓ₂) where ch₁ : (da : Ch A) → SumChange2 ch₂ : (db : Ch B) → SumChange2 rp : (s : A ⊎ B) → SumChange2 convert : SumChange → SumChange2 convert (inj₁ (inj₁ da)) = ch₁ da convert (inj₁ (inj₂ db)) = ch₂ db convert (inj₂ s) = rp s convert₁ : SumChange2 → SumChange convert₁ (ch₁ da) = inj₁ (inj₁ da) convert₁ (ch₂ db) = inj₁ (inj₂ db) convert₁ (rp s) = inj₂ s data SValid : A ⊎ B → SumChange → Set (ℓ₁ ⊔ ℓ₂) where sv₁ : ∀ (a : A) (da : Ch A) (ada : valid a da) → SValid (inj₁ a) (convert₁ (ch₁ da)) sv₂ : ∀ (b : B) (db : Ch B) (bdb : valid b db) → SValid (inj₂ b) (convert₁ (ch₂ db)) svrp : ∀ (s₁ s₂ : A ⊎ B) → SValid s₁ (convert₁ (rp s₂)) inv1 : ∀ ds → convert₁ (convert ds) ≡ ds inv1 (inj₁ (inj₁ da)) = refl inv1 (inj₁ (inj₂ db)) = refl inv1 (inj₂ s) = refl inv2 : ∀ ds → convert (convert₁ ds) ≡ ds inv2 (ch₁ da) = refl inv2 (ch₂ db) = refl inv2 (rp s) = refl private s⊕2 : A ⊎ B → SumChange2 → A ⊎ B s⊕2 (inj₁ a) (ch₁ da) = inj₁ (a ⊕ da) s⊕2 (inj₂ b) (ch₂ db) = inj₂ (b ⊕ db) s⊕2 (inj₂ b) (ch₁ da) = inj₂ b -- invalid s⊕2 (inj₁ a) (ch₂ db) = inj₁ a -- invalid s⊕2 s (rp s₁) = s₁ s⊕ : A ⊎ B → SumChange → A ⊎ B s⊕ s ds = s⊕2 s (convert ds) s⊝2 : A ⊎ B → A ⊎ B → SumChange2 s⊝2 (inj₁ x2) (inj₁ x1) = ch₁ (x2 ⊝ x1) s⊝2 (inj₂ y2) (inj₂ y1) = ch₂ (y2 ⊝ y1) s⊝2 s2 s1 = rp s2 s⊝ : A ⊎ B → A ⊎ B → SumChange s⊝ s2 s1 = convert₁ (s⊝2 s2 s1) s⊝-valid : (a b : A ⊎ B) → SValid a (s⊝ b a) s⊝-valid (inj₁ x1) (inj₁ x2) = sv₁ x1 (x2 ⊝ x1) (⊝-valid x1 x2) s⊝-valid (inj₂ y1) (inj₂ y2) = sv₂ y1 (y2 ⊝ y1) (⊝-valid y1 y2) s⊝-valid s1@(inj₁ x) s2@(inj₂ y) = svrp s1 s2 s⊝-valid s1@(inj₂ y) s2@(inj₁ x) = svrp s1 s2 s⊕-⊝ : (b a : A ⊎ B) → s⊕ a (s⊝ b a) ≡ b s⊕-⊝ (inj₁ x2) (inj₁ x1) rewrite ⊕-⊝ x2 x1 = refl s⊕-⊝ (inj₁ x2) (inj₂ y1) = refl s⊕-⊝ (inj₂ y2) (inj₁ x1) = refl s⊕-⊝ (inj₂ y2) (inj₂ y1) rewrite ⊕-⊝ y2 y1 = refl sumCA = record { Ch = SumChange ; isChAlg = record { _⊕_ = s⊕ ; _⊝_ = s⊝ ; valid = SValid ; ⊝-valid = s⊝-valid ; ⊕-⊝ = s⊕-⊝ } } open import Data.Integer open import Theorem.Groups-Nehemiah instance intCA : ChAlg ℤ intCA = record { Ch = ℤ ; isChAlg = record { _⊕_ = _+_ ; _⊝_ = _-_ ; valid = λ a b → ⊤ ; ⊝-valid = λ a b → tt ; ⊕-⊝ = λ b a → n+[m-n]=m {a} {b} } }
Add change structure for changes
Add change structure for changes
Agda
mit
inc-lc/ilc-agda
f1c2df98ed89a3de0f4b42407cb950eded1c5ed4
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 ⟧ (⟦ Γ′ ⟧ ρ) ≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩ ⟦ lift Γ′ (derive-var x) ⟧ ρ ∎) 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)
Increase overloading and confusion.
Increase overloading and confusion. Old-commit-hash: df682306832e5b833f837c92dec32b3c428a7ab9
Agda
mit
inc-lc/ilc-agda
757995f811b789374410d560566e8717a68f1e24
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 = trans (proof (let open ≙-Reasoning in begin dx ≙⟨ dx≙nil-x ⟩ nil x ∎)) (let open ≡-Reasoning in begin x ⊞ (nil x) ≡⟨ update-nil x ⟩ x ∎) -- 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)
------------------------------------------------------------------------ -- 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)
Revert "Show how badly reasoning for ≙ and ≡ combine"
Revert "Show how badly reasoning for ≙ and ≡ combine" This reverts commit 24faa2c5edd91d1db64977d5124ef40e01de433f. The commit showed how badly something worked, so let's leave in the old version for now. Old-commit-hash: 9f5f49cf0319ba809356655a204ec83725dfceb4
Agda
mit
inc-lc/ilc-agda
c1d53d1c793d91e002e6c8b4ffbbcc8dca4c6eda
README.agda
README.agda
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Machine-checked formalization of the theoretical results presented -- in the paper: -- -- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann. -- A Theory of Changes for Higher-Order Languages: -- Incrementalizing λ-Calculi by Static Differentiation. -- To appear at PLDI, ACM 2014. -- -- We claim that this formalization -- -- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper, -- (2) formally specifies the interface between our incrementalization -- framework and potential plugins, and -- (3) shows that the plugin interface can be instantiated. -- -- The first claim is the main reason for a machine-checked -- proof: We want to be sure that we got the proofs right. -- -- The second claim is about reusability and applicability: Only -- a clearly defined interface allows other researchers to -- provide plugins for our framework. -- -- The third claim is to show that the plugin interface is -- consistent: An inconsistent plugin interface would allow to -- prove arbitrary results in the framework. ------------------------------------------------------------------------ module README where -- We know two good ways to read this code base. You can either -- use Emacs with agda2-mode to interact with the source files, -- or you can use a web browser to view the pretty-printed and -- hyperlinked source files. For Agda power users, we also -- include basic setup information for your own machine. -- IF YOU WANT TO USE A BROWSER -- ============================ -- -- Start with *HTML* version of this readme. On the AEC -- submission website (online or inside the VM), follow the link -- "view the Agda code in their browser" to the file -- agda/README.html. -- -- The source code is syntax highlighted and hyperlinked. You can -- click on module names to open the corresponding files, or you -- can click on identifiers to jump to their definition. In -- general, a Agda file with name `Foo/Bar/Baz.agda` contains a -- module `Foo.Bar.Baz` and is shown in an HTML file -- `Foo.Bar.Baz.html`. -- -- Note that we also include the HTML files generated for our -- transitive dependencies from the Agda standard library. This -- allows you to follow hyperlinks to the Agda standard -- library. It is the default behavior of `agda --html` which we -- used to generate the HTML. -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE EMACS -- ======================== -- -- Open this file in Emacs with agda2-mode installed. See below -- for which Agda version you need to install. On the VM image, -- everything is setup for you and you can just open this file in -- Emacs. -- -- C-c C-l Load code. Type checks and syntax highlights. Use -- this if the code is not syntax highlighted, or -- after you changed something that you want to type -- check. -- -- M-. Jump to definition of identifier at the point. -- M-* Jump back. -- -- Note that README.agda imports Everything.agda which imports -- every Agda file in our formalization. So if README.agda type -- checks successfully, everything does. If you want to type -- check everything from scratch, delete the *.agdai files to -- disable separate compilation. You can use "find . -name -- '*.agdai' | xargs rm" to do that. -- -- More information on the Agda mode is available on the Agda wiki: -- -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE YOUR OWN SETUP -- ================================= -- -- To typecheck this formalization, you need to install the appropriate version -- of Agda (2.4.0), the Agda standard library (version 0.8), generate -- Everything.agda with the attached Haskell helper, and finally run Agda on -- this file. -- -- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh -- script and following instructions given on output will eventually generate -- Everything.agda and proceed to type check everything on command line. -- -- If you're not an Agda power user, it is probably easier to use -- the VM image or look at the pretty-printed and hyperlinked -- HTML files, see above. -- WHERE TO START READING? -- ======================= -- -- modules.pdf -- The graph of dependencies between Agda modules. -- Good if you want to get a broad overview. -- -- README.agda -- This file. A coarse-grained introduction to the Agda -- formalization. Good if you want to begin at the beginning -- and understand the structure of our code. -- -- PLDI14-List-of-Theorems.agda -- Pointers to the Agda formalizations of all theorems, lemmas -- or definitions in the PLDI paper. Good if you want to read -- the paper and the Agda code side by side. -- -- Here is an import of this file, so you can jump to it -- directly (use M-. in Emacs or click the module name in the -- Browser): import PLDI14-List-of-Theorems -- Everything.agda -- Imports every Agda module in our formalization. Good if you -- want to make sure you don't miss anything. -- -- Again, here's is an import of this file so you can navigate -- there: import Everything -- (This import is also important to ensure that if we typecheck -- README.agda, we also typecheck every other file of our -- formalization). -- THE AGDA CODE -- ============= -- -- The formalization has four parts: -- -- 1. A formalization of change structures. This lives in -- Base.Change.Algebra. (What we call "change structure" in the -- paper, we call "change algebra" in the Agda code. We changed -- the name when writing the paper, and never got around to -- updating the name in the Agda code). -- -- 2. Incrementalization framework for first-class functions, -- with extension points for plugging in data types and their -- incrementalization. This lives in the Parametric.* -- hierarchy. -- -- 3. An example plugin that provides integers and bags -- with negative multiplicity. This lives in the Nehemiah.* -- hierarchy. (For some reason, we choose to call this -- particular incarnation of the plugin Nehemiah). -- -- 4. Other material that is unrelated to the framework/plugin -- distinction. This is all other files. -- FORMALIZATION OF CHANGE STRUCTURES -- ================================== -- -- Section 2 of the paper, and Base.Change.Algebra in Agda. import Base.Change.Algebra -- INCREMENTALIZATION FRAMEWORK -- ============================ -- -- Section 3 of the paper, and Parametric.* hierarchy in Agda. -- -- The extension points are implemented as module parameters. See -- detailed explanation in Parametric.Syntax.Type and -- Parametric.Syntax.Term. Some extension points are for types, -- some for values, and some for proof fragments. In Agda, these -- three kinds of entities are unified anyway, so we can encode -- all of them as module parameters. -- -- Every module in the Parametric.* hierarchy adds at least one -- extension point, so the module hierarchy of a plugin will -- typically mirror the Parametric.* hierarchy, defining the -- values for these extension points. -- -- Firstly, we have the syntax of types and terms, the erased -- change structure for function types and incrementalization as -- a term-to-term transformation. The contents of these modules -- formalizes Sec. 3.1 and 3.2 of the paper, except for the -- second and third line of Figure 3, which is formalized further -- below. import Parametric.Syntax.Type -- syntax of types import Parametric.Syntax.Term -- syntax of terms import Parametric.Change.Type -- simply-typed changes import Parametric.Change.Derive -- incrementalization -- Secondly, we define the usual denotational semantics of the -- simply-typed lambda calculus in terms of total functions, and -- the change semantics in terms of a change structure on total -- functions. The contents of these modules formalize Sec. 3.3, -- 3.4 and 3.5 of the paper. import Parametric.Denotation.Value -- standard values import Parametric.Denotation.Evaluation -- standard evaluation import Parametric.Change.Validity -- dependently-typed changes import Parametric.Change.Specification -- change evaluation -- Thirdly, we define terms that operate on simply-typed changes, -- and connect them to their values. The concents of these -- modules formalize the second and third line of Figure 3, as -- well as the semantics of these lines. import Parametric.Change.Term -- terms that operate on simply-typed changes import Parametric.Change.Value -- the values of these terms import Parametric.Change.Evaluation -- connecting the terms and their values -- Finally, we prove correctness by connecting the (syntactic) -- incrementalization to the (semantic) change evaluation by a -- logical relation, and a proof that the values of terms -- produced by the incrementalization transformation are related -- to the change values of the original terms. The contents of -- these modules formalize Sec. 3.6. import Parametric.Change.Implementation -- logical relation import Parametric.Change.Correctness -- main correctness proof -- EXAMPLE PLUGIN -- ============== -- -- Sec. 3.7 in the paper, and the Nehemiah.* hierarchy in Agda. -- -- The module structure of the plugin follows the structure of -- the Parametric.* hierarchy. For example, the extension point -- defined in Parametric.Syntax.Term is instantiated in -- Nehemiah.Syntax.Term. -- -- As discussed in Sec. 3.7 of the paper, the point of this -- plugin is not to speed up any real programs, but to show "that -- the interface for proof plugins can be implemented". As a -- first step towards proving correctness of the more complicated -- plugin with integers, bags and finite maps we implement in -- Scala, we choose to define plugin with integers and bags in -- Agda. Instead of implementing bags (with negative -- multiplicities, like in the paper) in Agda, though, we -- postulate that a group of such bags exist. Note that integer -- bags with integer multiplicities are actually the free group -- given a singleton operation `Integer -> Bag`, so this should -- be easy to formalize in principle. -- Before we start with the plugin, we postulate an abstract data -- type for integer bags. import Postulate.Bag-Nehemiah -- Firstly, we extend the syntax of types and terms, the erased -- change structure for function types, and incrementalization as -- a term-to-term transformation to account for the data types of -- the Nehemiah language. The contents of these modules -- instantiate the extension points in Sec. 3.1 and 3.2 of the -- paper, except for the second and third line of Figure 3, which -- is instantiated further below. import Nehemiah.Syntax.Type import Nehemiah.Syntax.Term import Nehemiah.Change.Type import Nehemiah.Change.Derive -- Secondly, we extend the usual denotational semantics and the -- change semantics to account for the data types of the Nehemiah -- language. The contents of these modules instantiate the -- extension points in Sec. 3.3, 3.4 and 3.5 of the paper. import Nehemiah.Denotation.Value import Nehemiah.Denotation.Evaluation import Nehemiah.Change.Validity import Nehemiah.Change.Specification -- Thirdly, we extend the terms that operate on simply-typed -- changes, and the connection to their values to account for the -- data types of the Nehemiah language. The concents of these -- modules instantiate the extension points in the second and -- third line of Figure 3. import Nehemiah.Change.Term import Nehemiah.Change.Value import Nehemiah.Change.Evaluation -- Finally, we extend the logical relation and the main -- correctness proof to account for the data types in the -- Nehemiah language. The contents of these modules instantiate -- the extension points defined in Sec. 3.6. import Nehemiah.Change.Implementation import Nehemiah.Change.Correctness -- OTHER MATERIAL -- ============== -- -- We postulate extensionality of Agda functions. This postulate -- is well known to be compatible with Agda's type theory. import Postulate.Extensionality -- For historical reasons, we reexport Data.List.All from the -- standard library under the name DependentList. import Base.Data.DependentList -- This module supports overloading the ⟦_⟧ notation based on -- Agda's instance arguments. import Base.Denotation.Notation -- These modules implement contexts including typed de Bruijn -- indices to represent bound variables, sets of bound variables, -- and environments. These modules are parametric in the set of -- types (that are stored in contexts) and the set of values -- (that are stored in environments). So these modules are even -- more parametric than the Parametric.* hierarchy. import Base.Syntax.Context import Base.Syntax.Vars import Base.Denotation.Environment -- This module contains some helper definitions to merge a -- context of values and a context of changes. import Base.Change.Context
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Machine-checked formalization of the theoretical results presented -- in the paper: -- -- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann. -- A Theory of Changes for Higher-Order Languages: -- Incrementalizing λ-Calculi by Static Differentiation. -- To appear at PLDI, ACM 2014. -- -- We claim that this formalization -- -- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper, -- (2) formally specifies the interface between our incrementalization -- framework and potential plugins, and -- (3) shows that the plugin interface can be instantiated. -- -- The first claim is the main reason for a machine-checked -- proof: We want to be sure that we got the proofs right. -- -- The second claim is about reusability and applicability: Only -- a clearly defined interface allows other researchers to -- provide plugins for our framework. -- -- The third claim is to show that the plugin interface is -- consistent: An inconsistent plugin interface would allow to -- prove arbitrary results in the framework. ------------------------------------------------------------------------ module README where -- We know two good ways to read this code base. You can either -- use Emacs with agda2-mode to interact with the source files, -- or you can use a web browser to view the pretty-printed and -- hyperlinked source files. For Agda power users, we also -- include basic setup information for your own machine. -- IF YOU WANT TO USE A BROWSER -- ============================ -- -- Start with *HTML* version of this readme. On the AEC -- submission website (online or inside the VM), follow the link -- "view the Agda code in their browser" to the file -- agda/README.html. -- -- The source code is syntax highlighted and hyperlinked. You can -- click on module names to open the corresponding files, or you -- can click on identifiers to jump to their definition. In -- general, a Agda file with name `Foo/Bar/Baz.agda` contains a -- module `Foo.Bar.Baz` and is shown in an HTML file -- `Foo.Bar.Baz.html`. -- -- Note that we also include the HTML files generated for our -- transitive dependencies from the Agda standard library. This -- allows you to follow hyperlinks to the Agda standard -- library. It is the default behavior of `agda --html` which we -- used to generate the HTML. -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE EMACS -- ======================== -- -- Open this file in Emacs with agda2-mode installed. See below -- for which Agda version you need to install. On the VM image, -- everything is setup for you and you can just open this file in -- Emacs. -- -- C-c C-l Load code. Type checks and syntax highlights. Use -- this if the code is not syntax highlighted, or -- after you changed something that you want to type -- check. -- -- M-. Jump to definition of identifier at the point. -- M-* Jump back. -- -- Note that README.agda imports Everything.agda which imports -- every Agda file in our formalization. So if README.agda type -- checks successfully, everything does. If you want to type -- check everything from scratch, delete the *.agdai files to -- disable separate compilation. You can use "find . -name -- '*.agdai' | xargs rm" to do that. -- -- More information on the Agda mode is available on the Agda wiki: -- -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode -- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations -- -- To get started, continue below on "Where to start reading?". -- IF YOU WANT TO USE YOUR OWN SETUP -- ================================= -- -- To typecheck this formalization, you need to install the appropriate version -- of Agda (2.4.0.*), the Agda standard library (version 0.8), generate -- Everything.agda with the attached Haskell helper, and finally run Agda on -- this file. -- -- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh -- script and following instructions given on output will eventually generate -- Everything.agda and proceed to type check everything on command line. -- -- If you're not an Agda power user, it is probably easier to use -- the VM image or look at the pretty-printed and hyperlinked -- HTML files, see above. -- WHERE TO START READING? -- ======================= -- -- modules.pdf -- The graph of dependencies between Agda modules. -- Good if you want to get a broad overview. -- -- README.agda -- This file. A coarse-grained introduction to the Agda -- formalization. Good if you want to begin at the beginning -- and understand the structure of our code. -- -- PLDI14-List-of-Theorems.agda -- Pointers to the Agda formalizations of all theorems, lemmas -- or definitions in the PLDI paper. Good if you want to read -- the paper and the Agda code side by side. -- -- Here is an import of this file, so you can jump to it -- directly (use M-. in Emacs or click the module name in the -- Browser): import PLDI14-List-of-Theorems -- Everything.agda -- Imports every Agda module in our formalization. Good if you -- want to make sure you don't miss anything. -- -- Again, here's is an import of this file so you can navigate -- there: import Everything -- (This import is also important to ensure that if we typecheck -- README.agda, we also typecheck every other file of our -- formalization). -- THE AGDA CODE -- ============= -- -- The formalization has four parts: -- -- 1. A formalization of change structures. This lives in -- Base.Change.Algebra. (What we call "change structure" in the -- paper, we call "change algebra" in the Agda code. We changed -- the name when writing the paper, and never got around to -- updating the name in the Agda code). -- -- 2. Incrementalization framework for first-class functions, -- with extension points for plugging in data types and their -- incrementalization. This lives in the Parametric.* -- hierarchy. -- -- 3. An example plugin that provides integers and bags -- with negative multiplicity. This lives in the Nehemiah.* -- hierarchy. (For some reason, we choose to call this -- particular incarnation of the plugin Nehemiah). -- -- 4. Other material that is unrelated to the framework/plugin -- distinction. This is all other files. -- FORMALIZATION OF CHANGE STRUCTURES -- ================================== -- -- Section 2 of the paper, and Base.Change.Algebra in Agda. import Base.Change.Algebra -- INCREMENTALIZATION FRAMEWORK -- ============================ -- -- Section 3 of the paper, and Parametric.* hierarchy in Agda. -- -- The extension points are implemented as module parameters. See -- detailed explanation in Parametric.Syntax.Type and -- Parametric.Syntax.Term. Some extension points are for types, -- some for values, and some for proof fragments. In Agda, these -- three kinds of entities are unified anyway, so we can encode -- all of them as module parameters. -- -- Every module in the Parametric.* hierarchy adds at least one -- extension point, so the module hierarchy of a plugin will -- typically mirror the Parametric.* hierarchy, defining the -- values for these extension points. -- -- Firstly, we have the syntax of types and terms, the erased -- change structure for function types and incrementalization as -- a term-to-term transformation. The contents of these modules -- formalizes Sec. 3.1 and 3.2 of the paper, except for the -- second and third line of Figure 3, which is formalized further -- below. import Parametric.Syntax.Type -- syntax of types import Parametric.Syntax.Term -- syntax of terms import Parametric.Change.Type -- simply-typed changes import Parametric.Change.Derive -- incrementalization -- Secondly, we define the usual denotational semantics of the -- simply-typed lambda calculus in terms of total functions, and -- the change semantics in terms of a change structure on total -- functions. The contents of these modules formalize Sec. 3.3, -- 3.4 and 3.5 of the paper. import Parametric.Denotation.Value -- standard values import Parametric.Denotation.Evaluation -- standard evaluation import Parametric.Change.Validity -- dependently-typed changes import Parametric.Change.Specification -- change evaluation -- Thirdly, we define terms that operate on simply-typed changes, -- and connect them to their values. The concents of these -- modules formalize the second and third line of Figure 3, as -- well as the semantics of these lines. import Parametric.Change.Term -- terms that operate on simply-typed changes import Parametric.Change.Value -- the values of these terms import Parametric.Change.Evaluation -- connecting the terms and their values -- Finally, we prove correctness by connecting the (syntactic) -- incrementalization to the (semantic) change evaluation by a -- logical relation, and a proof that the values of terms -- produced by the incrementalization transformation are related -- to the change values of the original terms. The contents of -- these modules formalize Sec. 3.6. import Parametric.Change.Implementation -- logical relation import Parametric.Change.Correctness -- main correctness proof -- EXAMPLE PLUGIN -- ============== -- -- Sec. 3.7 in the paper, and the Nehemiah.* hierarchy in Agda. -- -- The module structure of the plugin follows the structure of -- the Parametric.* hierarchy. For example, the extension point -- defined in Parametric.Syntax.Term is instantiated in -- Nehemiah.Syntax.Term. -- -- As discussed in Sec. 3.7 of the paper, the point of this -- plugin is not to speed up any real programs, but to show "that -- the interface for proof plugins can be implemented". As a -- first step towards proving correctness of the more complicated -- plugin with integers, bags and finite maps we implement in -- Scala, we choose to define plugin with integers and bags in -- Agda. Instead of implementing bags (with negative -- multiplicities, like in the paper) in Agda, though, we -- postulate that a group of such bags exist. Note that integer -- bags with integer multiplicities are actually the free group -- given a singleton operation `Integer -> Bag`, so this should -- be easy to formalize in principle. -- Before we start with the plugin, we postulate an abstract data -- type for integer bags. import Postulate.Bag-Nehemiah -- Firstly, we extend the syntax of types and terms, the erased -- change structure for function types, and incrementalization as -- a term-to-term transformation to account for the data types of -- the Nehemiah language. The contents of these modules -- instantiate the extension points in Sec. 3.1 and 3.2 of the -- paper, except for the second and third line of Figure 3, which -- is instantiated further below. import Nehemiah.Syntax.Type import Nehemiah.Syntax.Term import Nehemiah.Change.Type import Nehemiah.Change.Derive -- Secondly, we extend the usual denotational semantics and the -- change semantics to account for the data types of the Nehemiah -- language. The contents of these modules instantiate the -- extension points in Sec. 3.3, 3.4 and 3.5 of the paper. import Nehemiah.Denotation.Value import Nehemiah.Denotation.Evaluation import Nehemiah.Change.Validity import Nehemiah.Change.Specification -- Thirdly, we extend the terms that operate on simply-typed -- changes, and the connection to their values to account for the -- data types of the Nehemiah language. The concents of these -- modules instantiate the extension points in the second and -- third line of Figure 3. import Nehemiah.Change.Term import Nehemiah.Change.Value import Nehemiah.Change.Evaluation -- Finally, we extend the logical relation and the main -- correctness proof to account for the data types in the -- Nehemiah language. The contents of these modules instantiate -- the extension points defined in Sec. 3.6. import Nehemiah.Change.Implementation import Nehemiah.Change.Correctness -- OTHER MATERIAL -- ============== -- -- We postulate extensionality of Agda functions. This postulate -- is well known to be compatible with Agda's type theory. import Postulate.Extensionality -- For historical reasons, we reexport Data.List.All from the -- standard library under the name DependentList. import Base.Data.DependentList -- This module supports overloading the ⟦_⟧ notation based on -- Agda's instance arguments. import Base.Denotation.Notation -- These modules implement contexts including typed de Bruijn -- indices to represent bound variables, sets of bound variables, -- and environments. These modules are parametric in the set of -- types (that are stored in contexts) and the set of values -- (that are stored in environments). So these modules are even -- more parametric than the Parametric.* hierarchy. import Base.Syntax.Context import Base.Syntax.Vars import Base.Denotation.Environment -- This module contains some helper definitions to merge a -- context of values and a context of changes. import Base.Change.Context
Update recommended version of Agda
README.agda: Update recommended version of Agda Rationale: 2.4.0 doesn't install any more.
Agda
mit
inc-lc/ilc-agda
464a985aa70ecceb7a7917d62fc7cb3e05dd59c2
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
kwangkim/pigment,kwangkim/pigment,brixen/Epigram,kwangkim/pigment
7f1a4e72fc0081b46fe06904e4627bb80b3240ac
proposal/examples/TacticsMatchingFunctionCalls.agda
proposal/examples/TacticsMatchingFunctionCalls.agda
{- This file demonstrates a technique that simulates the ability to pattern match against function calls rather than just variables or constructors. This technique is necessary to write certain kinds of tactics as generic functions. -} module TacticsMatchingFunctionCalls where open import Data.Bool hiding ( _≟_ ) open import Data.Nat open import Data.Fin hiding ( _+_ ) open import Data.Product hiding ( map ) open import Data.List open import Relation.Nullary open import Relation.Binary.PropositionalEquality open import Function ---------------------------------------------------------------------- {- Zero is the right identity of addition on the natural numbers. -} plusrident : (n : ℕ) → n ≡ n + 0 plusrident zero = refl plusrident (suc n) = cong suc (plusrident n) ---------------------------------------------------------------------- {- Arith is a universe of codes representing the natural numbers along with a limited set of functions over them. We would like to write tactics that pattern match against types indexed by applications of these functions. -} data Arith : Set where `zero : Arith `suc : (a : Arith) → Arith _`+_ _`*_ : (a₁ a₂ : Arith) → Arith eval : Arith → ℕ eval `zero = zero eval (`suc a) = suc (eval a) eval (a₁ `+ a₂) = eval a₁ + eval a₂ eval (a₁ `* a₂) = eval a₁ * eval a₂ ---------------------------------------------------------------------- {- Φ (standing for *function*-indexed types) represents an indexed type, but we *can* pattern match on function calls in its index position. A - The type of codes, including constructors and functions. i.e. Arith A′ - The model underlying the codes. i.e. ℕ el - The evaluation function from code values to model values. i.e. eval B - The genuine indexed datatype that Φ represents. i.e. Fin a - The index value as a code, which may be a function call and hence supports pattern matching. i.e. (a `+ `zero) -} data Φ (A A′ : Set) (el : A → A′) (B : A′ → Set) (a : A) : Set where φ : (b : B (el a)) → Φ A A′ el B a ---------------------------------------------------------------------- {- Alternatively, we can specialize Φ to our Arith universe. This may be a good use of our ability to cheaply define new datatypes via Desc. -} data ΦArith (B : ℕ → Set) (a : Arith) : Set where φ : (b : B (eval a)) → ΦArith B a ---------------------------------------------------------------------- {- Going further, we could even specialize B in ΦArith to Fin. -} data Fin₂ (a : Arith) : Set where fin : (i : Fin (eval a)) → Fin₂ a ---------------------------------------------------------------------- {- A small universe of dependent types that is sufficient for the examples given below. -} data Type : Set ⟦_⟧ : Type → Set data Type where `Bool `ℕ `Arith : Type `Fin : (n : ℕ) → Type `Φ : (A A′ : Type) (el : ⟦ A ⟧ → ⟦ A′ ⟧) (B : ⟦ A′ ⟧ → Type) (a : ⟦ A ⟧) → Type `ΦArith : (B : ℕ → Type) (a : Arith) → Type `Fin₂ : (a : Arith) → Type `Π `Σ : (A : Type) (B : ⟦ A ⟧ → Type) → Type `Id : (A : Type) (x y : ⟦ A ⟧) → Type ⟦ `Bool ⟧ = Bool ⟦ `ℕ ⟧ = ℕ ⟦ `Arith ⟧ = Arith ⟦ `Fin n ⟧ = Fin n ⟦ `Φ A A′ el B a ⟧ = Φ ⟦ A ⟧ ⟦ A′ ⟧ el (λ a → ⟦ B a ⟧) a ⟦ `ΦArith B a ⟧ = ΦArith (λ x → ⟦ B x ⟧) a ⟦ `Fin₂ a ⟧ = Fin₂ a ⟦ `Π A B ⟧ = (a : ⟦ A ⟧) → ⟦ B a ⟧ ⟦ `Σ A B ⟧ = Σ ⟦ A ⟧ (λ a → ⟦ B a ⟧) ⟦ `Id A x y ⟧ = x ≡ y _`→_ : (A B : Type) → Type A `→ B = `Π A (const B) _`×_ : (A B : Type) → Type A `× B = `Σ A (const B) ---------------------------------------------------------------------- {- Tactics are represented as generic functions taking a Dynamic value (of any type), and returning a Dynamic value (at a possibly different type). The convention is to behave like the identity function if the tactic doesn't match the current value or fails. In practice, the context will be a List of Dynamic values, and a tactic will map a Context to a Context. For simplicity, the tactics I give below only map a Dynamic to a Dynamic. -} Dynamic : Set Dynamic = Σ Type ⟦_⟧ Tactic : Set Tactic = Dynamic → Dynamic ---------------------------------------------------------------------- {- Here is an example of a tactic that only needs to pattern match on a variable or constructor, but not a function. We don't encounter any problems when writing this kind of a tactic. The tactic below changes a `Fin n` value into a `Fin (n + 0)` value. -} add-plus0-Fin : Tactic add-plus0-Fin (`Fin n , i) = `Fin (n + 0) , subst Fin (plusrident n) i add-plus0-Fin x = x ---------------------------------------------------------------------- {- In contrast, it is not straightforward to write tactics that need to pattern match on function calls. For example, below is ideally what we want to write to change a value of `Fin (n + 0)` to a value of `Fin n`. -} -- rm-plus0-Fin : Tactic -- rm-plus0-Fin (`Fin (n + 0) , i) = `Fin n , subst Fin (sym (plusrident n)) i -- rm-plus0-Fin x = x ---------------------------------------------------------------------- {- Instead of matching directly on `Fin, we can match on a `Φ that represents `Fin. This allows us to match on the the `+ function call in the index position. Because we cannot match against the function `el` (we would like to match it against `eval`), we add to our return type that proves the extentional equality between `el` and `eval`. Also note that the tactic below can be used for any type indexed by ℕ, not just Fin. -} rm-plus0 : Tactic rm-plus0 (`Φ `Arith `ℕ el B (a `+ `zero) , φ b) = `Π `Arith (λ x → `Id `ℕ (el x) (eval x)) `→ `Φ `Arith `ℕ el B a , λ f → φ (subst (λ x → ⟦ B x ⟧) (sym (f a)) (subst (λ x → ⟦ B x ⟧) (sym (plusrident (eval a))) (subst (λ x → ⟦ B x ⟧) (f (a `+ `zero)) b))) rm-plus0 x = x ---------------------------------------------------------------------- {- Now it is possible to apply the generic tactic to a specific value and have the desired behavior occur. Notice that the generated extentional equality premise is trivially provable because eval is always equal to eval. -} eg-rm-plus0 : (a : Arith) → ⟦ `Φ `Arith `ℕ eval `Fin (a `+ `zero) ⟧ → ⟦ `Φ `Arith `ℕ eval `Fin a ⟧ eg-rm-plus0 a (φ i) = proj₂ (rm-plus0 (`Φ `Arith `ℕ eval `Fin (a `+ `zero) , φ i)) (λ _ → refl) ---------------------------------------------------------------------- {- Here is the same tactic but using the specialized ΦArith type. -} rm-plus0-Arith : Tactic rm-plus0-Arith (`ΦArith B (a `+ `zero) , φ i) = `ΦArith B a , φ (subst (λ x → ⟦ B x ⟧) (sym (plusrident (eval a))) i) rm-plus0-Arith x = x ---------------------------------------------------------------------- {- And the same example usage, this time without needing to prove a premise. -} eg-rm-plus0-Arith : (a : Arith) → ⟦ `ΦArith `Fin (a `+ `zero) ⟧ → ⟦ `ΦArith `Fin a ⟧ eg-rm-plus0-Arith a (φ i) = proj₂ (rm-plus0-Arith (`ΦArith `Fin (a `+ `zero) , φ i)) ---------------------------------------------------------------------- {- Finally, here is the same tactic but using the even more specialized Fin₂ type. -} rm-plus0-Fin : Tactic rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i) = `Fin₂ a , fin (subst (λ x → ⟦ `Fin x ⟧) (sym (plusrident (eval a))) i) rm-plus0-Fin x = x ---------------------------------------------------------------------- eg-rm-plus0-Fin : (a : Arith) → ⟦ `Fin₂ (a `+ `zero) ⟧ → ⟦ `Fin₂ a ⟧ eg-rm-plus0-Fin a (fin i) = proj₂ (rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i)) ----------------------------------------------------------------------
{- This file demonstrates a technique that simulates the ability to pattern match against function calls rather than just variables or constructors. This technique is necessary to write certain kinds of tactics as generic functions. -} module TacticsMatchingFunctionCalls where open import Data.Bool hiding ( _≟_ ) open import Data.Nat open import Data.Fin hiding ( _+_ ) open import Data.Product hiding ( map ) open import Data.List open import Relation.Nullary open import Relation.Binary.PropositionalEquality open import Function ---------------------------------------------------------------------- {- Zero is the right identity of addition on the natural numbers. -} plusrident : (n : ℕ) → n ≡ n + 0 plusrident zero = refl plusrident (suc n) = cong suc (plusrident n) ---------------------------------------------------------------------- {- Arith is a universe of codes representing the natural numbers along with a limited set of functions over them. We would like to write tactics that pattern match against types indexed by applications of these functions. -} data Arith : Set where `zero : Arith `suc : (a : Arith) → Arith _`+_ _`*_ : (a₁ a₂ : Arith) → Arith eval : Arith → ℕ eval `zero = zero eval (`suc a) = suc (eval a) eval (a₁ `+ a₂) = eval a₁ + eval a₂ eval (a₁ `* a₂) = eval a₁ * eval a₂ ---------------------------------------------------------------------- {- Φ (standing for *function*-indexed types) represents an indexed type, but we *can* pattern match on function calls in its index position. A - The type of codes, including constructors and functions. i.e. Arith A′ - The model underlying the codes. i.e. ℕ el - The evaluation function from code values to model values. i.e. eval B - The genuine indexed datatype that Φ represents. i.e. Fin a - The index value as a code, which may be a function call and hence supports pattern matching. i.e. (a `+ `zero) -} data Φ (A A′ : Set) (el : A → A′) (B : A′ → Set) (a : A) : Set where φ : (b : B (el a)) → Φ A A′ el B a ---------------------------------------------------------------------- {- Alternatively, we can specialize Φ to our Arith universe. This may be a good use of our ability to cheaply define new datatypes via Desc. -} data ΦArith (B : ℕ → Set) (a : Arith) : Set where φ : (b : B (eval a)) → ΦArith B a ---------------------------------------------------------------------- {- Going further, we could even specialize B in ΦArith to Fin. -} data Fin₂ (a : Arith) : Set where fin : (i : Fin (eval a)) → Fin₂ a ---------------------------------------------------------------------- {- A small universe of dependent types that is sufficient for the examples given below. -} data Type : Set ⟦_⟧ : Type → Set data Type where `Bool `ℕ `Arith : Type `Fin : (n : ℕ) → Type `Φ : (A A′ : Type) (el : ⟦ A ⟧ → ⟦ A′ ⟧) (B : ⟦ A′ ⟧ → Type) (a : ⟦ A ⟧) → Type `ΦArith : (B : ℕ → Type) (a : Arith) → Type `Fin₂ : (a : Arith) → Type `Π `Σ : (A : Type) (B : ⟦ A ⟧ → Type) → Type `Id : (A : Type) (x y : ⟦ A ⟧) → Type ⟦ `Bool ⟧ = Bool ⟦ `ℕ ⟧ = ℕ ⟦ `Arith ⟧ = Arith ⟦ `Fin n ⟧ = Fin n ⟦ `Φ A A′ el B a ⟧ = Φ ⟦ A ⟧ ⟦ A′ ⟧ el (λ a → ⟦ B a ⟧) a ⟦ `ΦArith B a ⟧ = ΦArith (λ x → ⟦ B x ⟧) a ⟦ `Fin₂ a ⟧ = Fin₂ a ⟦ `Π A B ⟧ = (a : ⟦ A ⟧) → ⟦ B a ⟧ ⟦ `Σ A B ⟧ = Σ ⟦ A ⟧ (λ a → ⟦ B a ⟧) ⟦ `Id A x y ⟧ = x ≡ y _`→_ : (A B : Type) → Type A `→ B = `Π A (const B) _`×_ : (A B : Type) → Type A `× B = `Σ A (const B) ---------------------------------------------------------------------- {- Tactics are represented as generic functions taking a Dynamic value (of any type), and returning a Dynamic value (at a possibly different type). The convention is to behave like the identity function if the tactic doesn't match the current value or fails. In practice, the context will be a List of Dynamic values, and a tactic will map a Context to a Context. For simplicity, the tactics I give below only map a Dynamic to a Dynamic. -} Dynamic : Set Dynamic = Σ Type ⟦_⟧ Tactic : Set Tactic = Dynamic → Dynamic ---------------------------------------------------------------------- {- Here is an example of a tactic that only needs to pattern match on a variable or constructor, but not a function. We don't encounter any problems when writing this kind of a tactic. The tactic below changes a `Fin n` value into a `Fin (n + 0)` value. -} add-plus0-Fin : Tactic add-plus0-Fin (`Fin n , i) = `Fin (n + 0) , subst Fin (plusrident n) i add-plus0-Fin x = x ---------------------------------------------------------------------- {- In contrast, it is not straightforward to write tactics that need to pattern match on function calls. For example, below is ideally what we want to write to change a value of `Fin (n + 0)` to a value of `Fin n`. -} -- rm-plus0-Fin : Tactic -- rm-plus0-Fin (`Fin (n + 0) , i) = `Fin n , subst Fin (sym (plusrident n)) i -- rm-plus0-Fin x = x ---------------------------------------------------------------------- {- Instead of matching directly on `Fin, we can match on a `Φ that represents `Fin. This allows us to match on the the `+ function call in the index position. Because we cannot match against the function `el` (we would like to match it against `eval`), we add to our return type that proves the extentional equality between `el` and `eval`. Also note that the tactic below can be used for any type indexed by ℕ, not just Fin. -} rm-plus0 : Tactic rm-plus0 (`Φ `Arith `ℕ el B (a `+ `zero) , φ b) = `Π `Arith (λ x → `Id `ℕ (el x) (eval x)) `→ `Φ `Arith `ℕ el B a , λ f → φ (subst (λ x → ⟦ B x ⟧) (sym (f a)) (subst (λ x → ⟦ B x ⟧) (sym (plusrident (eval a))) (subst (λ x → ⟦ B x ⟧) (f (a `+ `zero)) b))) rm-plus0 x = x ---------------------------------------------------------------------- {- Now it is possible to apply the generic tactic to a specific value and have the desired behavior occur. Notice that the generated extentional equality premise is trivially provable because eval is always equal to eval. -} eg-rm-plus0 : (a : Arith) → ⟦ `Φ `Arith `ℕ eval `Fin (a `+ `zero) ⟧ → ⟦ `Φ `Arith `ℕ eval `Fin a ⟧ eg-rm-plus0 a (φ i) = proj₂ (rm-plus0 (`Φ `Arith `ℕ eval `Fin (a `+ `zero) , φ i)) (λ _ → refl) ---------------------------------------------------------------------- {- Here is the same tactic but using the specialized ΦArith type. -} rm-plus0-Arith : Tactic rm-plus0-Arith (`ΦArith B (a `+ `zero) , φ i) = `ΦArith B a , φ (subst (λ x → ⟦ B x ⟧) (sym (plusrident (eval a))) i) rm-plus0-Arith x = x ---------------------------------------------------------------------- {- And the same example usage, this time without needing to prove a premise. -} eg-rm-plus0-Arith : (a : Arith) → ⟦ `ΦArith `Fin (a `+ `zero) ⟧ → ⟦ `ΦArith `Fin a ⟧ eg-rm-plus0-Arith a (φ i) = proj₂ (rm-plus0-Arith (`ΦArith `Fin (a `+ `zero) , φ i)) ---------------------------------------------------------------------- {- Finally, here is the same tactic but using the even more specialized Fin₂ type. -} rm-plus0-Fin : Tactic rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i) = `Fin₂ a , fin (subst (λ x → ⟦ `Fin x ⟧) (sym (plusrident (eval a))) i) rm-plus0-Fin x = x ---------------------------------------------------------------------- eg-rm-plus0-Fin : (a : Arith) → ⟦ `Fin₂ (a `+ `zero) ⟧ → ⟦ `Fin₂ a ⟧ eg-rm-plus0-Fin a (fin i) = proj₂ (rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i)) ---------------------------------------------------------------------- {- A tactic to simplify by one step of `+. -} simp-step-plus0-Fin : Tactic simp-step-plus0-Fin (`Fin₂ (`zero `+ a) , fin i) = `Fin₂ a , fin i simp-step-plus0-Fin (`Fin₂ (`suc a₁ `+ a₂) , fin i) = `Fin₂ (`suc (a₁ `+ a₂)) , fin i simp-step-plus0-Fin x = x ---------------------------------------------------------------------- {- A semantics-preserving definitional evaluator, and a tactic to simplify as far as possible using definitional equality. This is like the "simp" tactic in Coq. -} eval₂ : (a₁ : Arith) → Σ Arith (λ a₂ → eval a₁ ≡ eval a₂) eval₂ `zero = `zero , refl eval₂ (`suc a) with eval₂ a ... | a′ , p rewrite p = `suc a′ , refl eval₂ (`zero `+ a₂) = eval₂ a₂ eval₂ (`suc a₁ `+ a₂) with eval₂ a₁ | eval₂ a₂ ... | a₁′ , p | a₂′ , q rewrite p | q = `suc (a₁′ `+ a₂′) , refl eval₂ (a₁ `+ a₂) with eval₂ a₁ | eval₂ a₂ ... | a₁′ , p | a₂′ , q rewrite p | q = (a₁′ `+ a₂′) , refl eval₂ (`zero `* a₂) = `zero , refl eval₂ (`suc a₁ `* a₂) with eval₂ a₁ | eval₂ a₂ ... | a₁′ , p | a₂′ , q rewrite p | q = a₂′ `+ (a₁′ `* a₂′) , refl eval₂ (a₁ `* a₂) with eval₂ a₁ | eval₂ a₂ ... | a₁′ , p | a₂′ , q rewrite p | q = (a₁′ `* a₂′) , refl simp-Fin : Tactic simp-Fin (`Fin₂ a , fin i) with eval₂ a ... | a′ , p rewrite p = `Fin₂ a′ , fin i simp-Fin x = x ----------------------------------------------------------------------
Add Coq's "simp" tactic.
Add Coq's "simp" tactic.
Agda
bsd-3-clause
spire/spire
71b3a14edcb512975548e8df67858a30b1f62d68
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) 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 -- 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) CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ -- helper for lift-η-const, don't try to understand at home lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → 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)
Rename TermConstructor to CurriedTermConstructor.
Rename TermConstructor to CurriedTermConstructor. This rename prepares for introducing UncurriedTermConstructor which will allow to clarify the role of lift-η-const-rec. Old-commit-hash: b9de3c413f5a2a2015d3114bfb8483ab3078cc06
Agda
mit
inc-lc/ilc-agda
6538c1e3a868c0dc095139fc2243a8683b77c995
Data/NatBag/Properties.agda
Data/NatBag/Properties.agda
module Data.NatBag.Properties where import Data.Nat as ℕ open import Relation.Binary.PropositionalEquality open import Data.NatBag open import Data.Integer open import Data.Sum hiding (map) open import Data.Product hiding (map) -- This import is too slow. -- It causes Agda 2.3.2 to use so much memory that cai's -- computer with 4GB RAM begins to thresh. -- -- open import Data.Integer.Properties using (n⊖n≡0) n⊖n≡0 : ∀ n → n ⊖ n ≡ + 0 n⊖n≡0 ℕ.zero = refl n⊖n≡0 (ℕ.suc n) = n⊖n≡0 n ---------------- -- Statements -- ---------------- b\\b=∅ : ∀ {b : Bag} → b \\ b ≡ empty ∅++b=b : ∀ {b : Bag} → empty ++ b ≡ b b\\∅=b : ∀ {b : Bag} → b \\ empty ≡ b ∅\\b=-b : ∀ {b : Bag} → empty \\ b ≡ map₂ -_ b -- ++d=\\-d : ∀ {b d : Bag} → b ++ d ≡ b \\ map₂ -_ d b++[d\\b]=d : ∀ {b d : Bag} → b ++ (d \\ b) ≡ d ------------ -- Proofs -- ------------ i-i=0 : ∀ {i : ℤ} → (i - i) ≡ (+ 0) i-i=0 {+ ℕ.zero} = refl i-i=0 {+ ℕ.suc n} = n⊖n≡0 n i-i=0 { -[1+ n ]} = n⊖n≡0 n -- Debug tool -- Lets you try out inhabitance of any type anywhere absurd! : ∀ {B C : Set} → 0 ≡ 1 → B → {x : B} → C absurd! () -- Specialized absurdity needed to type check. -- λ () hasn't enough information sometimes. absurd : Nonzero (+ 0) → ∀ {A : Set} → A absurd () -- Here to please the termination checker. neb\\neb=∅ : ∀ {neb : NonemptyBag} → zipNonempty _-_ neb neb ≡ empty neb\\neb=∅ {singleton i i≠0} with nonzero? (i - i) ... | inj₁ _ = refl ... | inj₂ 0≠0 rewrite i-i=0 {i} = absurd 0≠0 neb\\neb=∅ {i ∷ neb} with nonzero? (i - i) ... | inj₁ _ rewrite neb\\neb=∅ {neb} = refl ... | inj₂ 0≠0 rewrite neb\\neb=∅ {neb} | i-i=0 {i} = absurd 0≠0 {- ++d=\\-d {inj₁ ∅} {inj₁ ∅} = refl ++d=\\-d {inj₁ ∅} {inj₂ (i ∷ y)} = {!!} ++d=\\-d {inj₂ y} {d} = {!!} ++d=\\-d {inj₁ ∅} {inj₂ (singleton i i≠0)} rewrite ∅++b=b {inj₂ (singleton i i≠0)} with nonzero? i | nonzero? (+ 0 - i) ... | inj₂ _ | inj₂ 0-i≠0 = begin {!!} ≡⟨ {!!} ⟩ {!inj₂ (singleton (- i) ?)!} ∎ where open ≡-Reasoning ... | inj₁ i=0 | _ rewrite i=0 = absurd i≠0 ++d=\\-d {inj₁ ∅} {inj₂ (singleton (+ 0) i≠0)} | _ | _ = absurd i≠0 ++d=\\-d {inj₁ ∅} {inj₂ (singleton (+ (ℕ.suc n)) i≠0)} | inj₂ (positive .n) | inj₁ () ++d=\\-d {inj₁ ∅} {inj₂ (singleton -[1+ n ] i≠0)} | inj₂ (negative .n) | inj₁ () -} b\\b=∅ {inj₁ ∅} = refl b\\b=∅ {inj₂ neb} = neb\\neb=∅ {neb} ∅++b=b {b} = {!!} b\\∅=b {b} = {!!} ∅\\b=-b {b} = {!!} negate : ∀ {i} → Nonzero i → Nonzero (- i) negate (negative n) = positive n negate (positive n) = negative n negate′ : ∀ {i} → (i≠0 : Nonzero i) → Nonzero (+ 0 - i) negate′ { -[1+ n ]} (negative .n) = positive n negate′ {+ .(ℕ.suc n)} (positive n) = negative n 0-i=-i : ∀ {i} → + 0 - i ≡ - i 0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to 0-i=-i {+ ℕ.zero} = refl -- refl are different. 0-i=-i {+ ℕ.suc n} = refl rewrite-singleton : ∀ (i : ℤ) (0-i≠0 : Nonzero (+ 0 - i)) ( -i≠0 : Nonzero (- i)) → singleton (+ 0 - i) 0-i≠0 ≡ singleton (- i) -i≠0 rewrite-singleton (+ ℕ.zero) () () rewrite-singleton (+ ℕ.suc n) (negative .n) (negative .n) = refl rewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl negateSingleton : ∀ {i i≠0} → mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0) ≡ inj₂ (singleton (- i) (negate i≠0)) -- Fun fact: -- Pattern-match on implicit parameters in the first two -- cases results in rejection by Agda. negateSingleton {i} {i≠0} with nonzero? i | nonzero? (+ 0 - i) negateSingleton | inj₂ (negative n) | inj₁ () negateSingleton | inj₂ (positive n) | inj₁ () negateSingleton {_} {i≠0} | inj₁ i=0 | _ rewrite i=0 = absurd i≠0 negateSingleton {i} {i≠0} | inj₂ _ | inj₂ 0-i≠0 = begin -- Reasoning done in 1 step. Included for clarity only. inj₂ (singleton (+ 0 + - i) 0-i≠0) ≡⟨ cong inj₂ (rewrite-singleton i 0-i≠0 (negate i≠0)) ⟩ inj₂ (singleton (- i) (negate i≠0)) ∎ where open ≡-Reasoning absurd[i-i≠0] : ∀ {i} → Nonzero (i - i) → ∀ {A : Set} → A absurd[i-i≠0] {+ ℕ.zero} = absurd absurd[i-i≠0] {+ ℕ.suc n} = absurd[i-i≠0] { -[1+ n ]} absurd[i-i≠0] { -[1+ ℕ.zero ]} = absurd absurd[i-i≠0] { -[1+ ℕ.suc n ]} = absurd[i-i≠0] { -[1+ n ]} annihilate : ∀ {i i≠0} → inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0)) ≡ inj₁ ∅ annihilate {i} with nonzero? (i - i) ... | inj₁ i-i=0 = λ {i≠0} → refl ... | inj₂ i-i≠0 = absurd[i-i≠0] {i} i-i≠0 {- left-is-not-right : ∀ {A B : Set} {a : A} {b : B} → inj₁ a ≡ inj₂ b → ∀ {X : Set} → X left-is-not-right = λ {A} {B} {a} {b} → λ () never-both : ∀ {A B : Set} {sum : A ⊎ B} {a b} → sum ≡ inj₁ a → sum ≡ inj₂ b → ∀ {X : Set} → X never-both s=a s=b = left-is-not-right (trans (sym s=a) s=b) empty-bag? : ∀ (b : Bag) → (b ≡ inj₁ ∅) ⊎ Σ NonemptyBag (λ neb → b ≡ inj₂ neb) empty-bag? (inj₁ ∅) = inj₁ refl empty-bag? (inj₂ neb) = inj₂ (neb , refl) -} b++[∅\\b]=∅ : ∀ {b} → b ++ (empty \\ b) ≡ empty b++[∅\\b]=∅ {inj₁ ∅} = refl b++[∅\\b]=∅ {inj₂ (singleton i i≠0)} = begin inj₂ (singleton i i≠0) ++ mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0) ≡⟨ cong₂ _++_ {x = inj₂ (singleton i i≠0)} refl (negateSingleton {i} {i≠0}) ⟩ inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0)) ≡⟨ annihilate {i} {i≠0} ⟩ inj₁ ∅ ∎ where open ≡-Reasoning b++[∅\\b]=∅ {inj₂ (i ∷ y)} = {!!} b++[d\\b]=d {inj₁ ∅} {d} rewrite b\\∅=b {d} | ∅++b=b {d} = refl b++[d\\b]=d {b} {inj₁ ∅} = b++[∅\\b]=∅ {b} b++[d\\b]=d {inj₂ b} {inj₂ d} = {!!}
module Data.NatBag.Properties where import Data.Nat as ℕ open import Relation.Binary.PropositionalEquality open import Data.NatBag open import Data.Integer open import Data.Sum hiding (map) open import Data.Product hiding (map) -- This import is too slow. -- It causes Agda 2.3.2 to use so much memory that cai's -- computer with 4GB RAM begins to thresh. -- -- open import Data.Integer.Properties using (n⊖n≡0) n⊖n≡0 : ∀ n → n ⊖ n ≡ + 0 n⊖n≡0 ℕ.zero = refl n⊖n≡0 (ℕ.suc n) = n⊖n≡0 n ---------------- -- Statements -- ---------------- b\\b=∅ : ∀ {b : Bag} → b \\ b ≡ empty ∅++b=b : ∀ {b : Bag} → empty ++ b ≡ b b\\∅=b : ∀ {b : Bag} → b \\ empty ≡ b ∅\\b=-b : ∀ {b : Bag} → empty \\ b ≡ map₂ -_ b b++[d\\b]=d : ∀ {b d : Bag} → b ++ (d \\ b) ≡ d ------------ -- Proofs -- ------------ i-i=0 : ∀ {i : ℤ} → (i - i) ≡ (+ 0) i-i=0 {+ ℕ.zero} = refl i-i=0 {+ ℕ.suc n} = n⊖n≡0 n i-i=0 { -[1+ n ]} = n⊖n≡0 n -- Debug tool -- Lets you try out inhabitance of any type anywhere absurd! : ∀ {B C : Set} → 0 ≡ 1 → B → {x : B} → C absurd! () -- Specialized absurdity needed to type check. -- λ () hasn't enough information sometimes. absurd : Nonzero (+ 0) → ∀ {A : Set} → A absurd () -- Here to please the termination checker. neb\\neb=∅ : ∀ {neb : NonemptyBag} → zipNonempty _-_ neb neb ≡ empty neb\\neb=∅ {singleton i i≠0} with nonzero? (i - i) ... | inj₁ _ = refl ... | inj₂ 0≠0 rewrite i-i=0 {i} = absurd 0≠0 neb\\neb=∅ {i ∷ neb} with nonzero? (i - i) ... | inj₁ _ rewrite neb\\neb=∅ {neb} = refl ... | inj₂ 0≠0 rewrite neb\\neb=∅ {neb} | i-i=0 {i} = absurd 0≠0 b\\b=∅ {inj₁ ∅} = refl b\\b=∅ {inj₂ neb} = neb\\neb=∅ {neb} ∅++b=b {b} = {!!} b\\∅=b {b} = {!!} ∅\\b=-b {b} = {!!} negate : ∀ {i} → Nonzero i → Nonzero (- i) negate (negative n) = positive n negate (positive n) = negative n negate′ : ∀ {i} → (i≠0 : Nonzero i) → Nonzero (+ 0 - i) negate′ { -[1+ n ]} (negative .n) = positive n negate′ {+ .(ℕ.suc n)} (positive n) = negative n 0-i=-i : ∀ {i} → + 0 - i ≡ - i 0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to 0-i=-i {+ ℕ.zero} = refl -- refl are different. 0-i=-i {+ ℕ.suc n} = refl rewrite-singleton : ∀ (i : ℤ) (0-i≠0 : Nonzero (+ 0 - i)) ( -i≠0 : Nonzero (- i)) → singleton (+ 0 - i) 0-i≠0 ≡ singleton (- i) -i≠0 rewrite-singleton (+ ℕ.zero) () () rewrite-singleton (+ ℕ.suc n) (negative .n) (negative .n) = refl rewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl negateSingleton : ∀ {i i≠0} → mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0) ≡ inj₂ (singleton (- i) (negate i≠0)) -- Fun fact: -- Pattern-match on implicit parameters in the first two -- cases results in rejection by Agda. negateSingleton {i} {i≠0} with nonzero? i | nonzero? (+ 0 - i) negateSingleton | inj₂ (negative n) | inj₁ () negateSingleton | inj₂ (positive n) | inj₁ () negateSingleton {_} {i≠0} | inj₁ i=0 | _ rewrite i=0 = absurd i≠0 negateSingleton {i} {i≠0} | inj₂ _ | inj₂ 0-i≠0 = begin -- Reasoning done in 1 step. Included for clarity only. inj₂ (singleton (+ 0 + - i) 0-i≠0) ≡⟨ cong inj₂ (rewrite-singleton i 0-i≠0 (negate i≠0)) ⟩ inj₂ (singleton (- i) (negate i≠0)) ∎ where open ≡-Reasoning absurd[i-i≠0] : ∀ {i} → Nonzero (i - i) → ∀ {A : Set} → A absurd[i-i≠0] {+ ℕ.zero} = absurd absurd[i-i≠0] {+ ℕ.suc n} = absurd[i-i≠0] { -[1+ n ]} absurd[i-i≠0] { -[1+ ℕ.zero ]} = absurd absurd[i-i≠0] { -[1+ ℕ.suc n ]} = absurd[i-i≠0] { -[1+ n ]} annihilate : ∀ {i i≠0} → inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0)) ≡ inj₁ ∅ annihilate {i} with nonzero? (i - i) ... | inj₁ i-i=0 = λ {i≠0} → refl ... | inj₂ i-i≠0 = absurd[i-i≠0] {i} i-i≠0 b++[∅\\b]=∅ : ∀ {b} → b ++ (empty \\ b) ≡ empty b++[∅\\b]=∅ {inj₁ ∅} = refl b++[∅\\b]=∅ {inj₂ (singleton i i≠0)} = begin inj₂ (singleton i i≠0) ++ mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0) ≡⟨ cong₂ _++_ {x = inj₂ (singleton i i≠0)} refl (negateSingleton {i} {i≠0}) ⟩ inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0)) ≡⟨ annihilate {i} {i≠0} ⟩ inj₁ ∅ ∎ where open ≡-Reasoning b++[∅\\b]=∅ {inj₂ (i ∷ y)} = {!!} b++[d\\b]=d {inj₁ ∅} {d} rewrite b\\∅=b {d} | ∅++b=b {d} = refl b++[d\\b]=d {b} {inj₁ ∅} = b++[∅\\b]=∅ {b} b++[d\\b]=d {inj₂ b} {inj₂ d} = {!!}
Remove dead code of Data.NatBag.Properties Checkpoint: Recast property proofs using set theoretic methods (#55)
Remove dead code of Data.NatBag.Properties Checkpoint: Recast property proofs using set theoretic methods (#55) Old-commit-hash: 04b32e5d92152cdab089a25c0d8f9f4c39279fb0
Agda
mit
inc-lc/ilc-agda
2971c097d16d13188929ef857808539310db0b8e
meaning.agda
meaning.agda
module meaning where open import Level record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where constructor meaning field {Semantics} : Set ℓ ⟦_⟧ : Syntax → Semantics open Meaning {{...}} public
module meaning 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 printing of resolved overloading.
Improve printing of resolved overloading. After this change, the semantic brackets will contain the syntactic thing even if Agda displays explicitly resolved overloaded notation. Old-commit-hash: c8cbc43ed8e715342e0cc3bccc98e0be2dd23560
Agda
mit
inc-lc/ilc-agda
b5190556d5bc58a768c39b9fa871dea8b2d6f0f1
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) 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 f (x ⊕σ Δx) ⊝τ app g 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
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) 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
Fix bug in diff-term.
Fix bug in diff-term. Old-commit-hash: 01ada999b676d2a8a2ab289b3a0c79f1524620e5
Agda
mit
inc-lc/ilc-agda
011116b87218eb84d5010b97f93c495b4f090fae
incremental.agda
incremental.agda
module incremental where -- SIMPLE TYPES -- Syntax data Type : Set where _⇒_ : (τ₁ τ₂ : Type) → Type infixr 5 _⇒_ -- Semantics Dom⟦_⟧ : Type -> Set Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧ -- TYPING CONTEXTS -- Syntax data Context : Set where ∅ : Context _•_ : (τ : Type) (Γ : Context) → Context infixr 9 _•_ -- Semantics data Empty : Set where ∅ : Empty data Bind A B : Set where _•_ : (v : A) (ρ : B) → Bind A B Env⟦_⟧ : Context → Set Env⟦ ∅ ⟧ = Empty Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧ -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ -- Semantics lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ lookup⟦ this ⟧ (v • ρ) = v lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ -- TERMS -- Syntax data Term : Context → Type → Set where abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂) app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂ var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ -- Semantics eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ) eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ) eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ -- WEAKENING -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₁ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂ -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- Weaken a term to a super context weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t) weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂) weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x) -- CHANGE TYPES Δ-Type : Type → Type Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂ apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply {τ₁ ⇒ τ₂} = abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x ) ( f x ) compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ) compose {τ₁ ⇒ τ₂} = abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λdg. λx. compose ( df x ) ( dg x ) -- 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 τ • Γ -- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ) Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t) Δ-term {Γ} (app t₁ t₂) = {!!} Δ-term {Γ} (var x) = {!!}
module incremental where -- SIMPLE TYPES -- Syntax data Type : Set where _⇒_ : (τ₁ τ₂ : Type) → Type infixr 5 _⇒_ -- Semantics Dom⟦_⟧ : Type -> Set Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧ -- TYPING CONTEXTS -- Syntax data Context : Set where ∅ : Context _•_ : (τ : Type) (Γ : Context) → Context infixr 9 _•_ -- Semantics data Empty : Set where ∅ : Empty data Bind A B : Set where _•_ : (v : A) (ρ : B) → Bind A B Env⟦_⟧ : Context → Set Env⟦ ∅ ⟧ = Empty Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧ -- VARIABLES -- Syntax data Var : Context → Type → Set where this : ∀ {Γ τ} → Var (τ • Γ) τ that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ -- Semantics lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ lookup⟦ this ⟧ (v • ρ) = v lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ -- TERMS -- Syntax data Term : Context → Type → Set where abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂) app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂ var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ -- Semantics eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧ eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ) eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ) eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ -- WEAKENING -- Extend a context to a super context infixr 10 _⋎_ _⋎_ : (Γ₁ Γ₁ : Context) → Context ∅ ⋎ Γ₂ = Γ₂ (τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂ -- Lift a variable to a super context lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ lift {∅} {∅} x = x lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x) lift {τ • Γ₁} {Γ₂} this = this lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x) -- Weaken a term to a super context weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t) weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂) weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x) -- CHANGE TYPES Δ-Type : Type → Type Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂ apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ) apply {τ₁ ⇒ τ₂} = abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λf. λx. apply ( df x ) ( f x ) compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ) compose {τ₁ ⇒ τ₂} = abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this))))) -- λdf. λdg. λx. compose ( df x ) ( dg x ) nil : ∀ {τ Γ} → Term Γ (Δ-Type τ) nil {τ₁ ⇒ τ₂} = abs nil -- λx. nil -- Hey, apply is α-equivalent to compose, what's going on? -- Oh, and `Δ-Type` is the identity function? -- CHANGE CONTEXTS Δ-Context : Context → Context Δ-Context ∅ = ∅ Δ-Context (τ • Γ) = τ • Δ-Type τ • Γ -- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ) Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t) Δ-term {Γ} (app t₁ t₂) = {!!} Δ-term {Γ} (var x) = {!!}
Implement nil changes.
Implement nil changes. Old-commit-hash: da546d60ec48fe5f6f8b261aac1fdbb71b18fa69
Agda
mit
inc-lc/ilc-agda
f2fde70dc52c3050e537b787efd3c7efa9778118
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 Syntax.Context.Plotkin B -- Declarations of Term and Terms to enable mutual recursion data Term (Γ : Context) : (τ : Type) → Set data Terms (Γ : Context) : (Σ : Context) → Set -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where const : ∀ {Σ τ} → (c : C Σ τ) → Terms Γ Σ → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. data Terms Γ where ∅ : Terms Γ ∅ _•_ : ∀ {τ Σ} → Term Γ τ → Terms Γ Σ → Terms Γ (τ • Σ) infixr 9 _•_ -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ) lift-diff-apply diff apply {base ι} = diff , apply lift-diff-apply diff apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) lift-diff diff apply = λ {τ Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) lift-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ) lift-apply diff apply = λ {τ Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weakenAll : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weakenAll Γ₁≼Γ₂ ∅ = ∅ weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant)
import Syntax.Type.Plotkin as Type import Syntax.Context as Context module Syntax.Term.Plotkin {B : Set {- of base types -}} {C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}} where -- Terms of languages described in Plotkin style open import Function using (_∘_) open import Data.Product open Type B open Context {Type} open import Syntax.Context.Plotkin B -- Declarations of Term and Terms to enable mutual recursion data Term (Γ : Context) : (τ : Type) → Set data Terms (Γ : Context) : (Σ : Context) → Set -- (Term Γ τ) represents a term of type τ -- with free variables bound in Γ. data Term Γ where const : ∀ {Σ τ} → (c : C Σ τ) → Terms Γ Σ → Term Γ τ var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ app : ∀ {σ τ} (s : Term Γ (σ ⇒ τ)) → (t : Term Γ σ) → Term Γ τ abs : ∀ {σ τ} (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ) -- (Terms Γ Σ) represents a list of terms with types from Σ -- with free variables bound in Γ. data Terms Γ where ∅ : Terms Γ ∅ _•_ : ∀ {τ Σ} → Term Γ τ → Terms Γ Σ → Terms Γ (τ • Σ) infixr 9 _•_ -- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x -- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x) lift-diff-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ) lift-diff-apply diff apply {base ι} = diff , apply lift-diff-apply diff apply {σ ⇒ τ} = let -- for diff g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this -- for apply Δh = var (that (that this)) h = var (that this) y = var this -- syntactic sugars diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ}) diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ}) applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) _⊝σ_ = λ s t → app (app diffσ s) t _⊝τ_ = λ s t → app (app diffτ s) t _⊕σ_ = λ t Δt → app (app applyσ Δt) t _⊕τ_ = λ t Δt → app (app applyτ Δt) t in abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x)))) , abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y)))) lift-diff : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ) lift-diff diff apply = λ {τ Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ}) lift-apply : ∀ {Δbase : B → Type} → let Δtype = lift-Δtype Δbase term = Term in (∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) → (∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) → ∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ) lift-apply diff apply = λ {τ Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ}) -- Weakening weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ weakenAll : ∀ {Γ₁ Γ₂ Σ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Terms Γ₁ Σ → Terms Γ₂ Σ weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts) weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x) weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t) weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t) weakenAll Γ₁≼Γ₂ ∅ = ∅ weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts -- Specialized weakening weaken₁ : ∀ {Γ σ τ} → Term Γ τ → Term (σ • Γ) τ weaken₁ t = weaken (drop _ • ≼-refl) t weaken₂ : ∀ {Γ α β τ} → Term Γ τ → Term (α • β • Γ) τ weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t weaken₃ : ∀ {Γ α β γ τ} → Term Γ τ → Term (α • β • γ • Γ) τ weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t -- Shorthands for nested applications app₂ : ∀ {Γ α β γ} → Term Γ (α ⇒ β ⇒ γ) → Term Γ α → Term Γ β → Term Γ γ app₂ f x = app (app f x) app₃ : ∀ {Γ α β γ δ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ app₃ f x = app₂ (app f x) app₄ : ∀ {Γ α β γ δ ε} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε app₄ f x = app₃ (app f x) app₅ : ∀ {Γ α β γ δ ε ζ} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ app₅ f x = app₄ (app f x) app₆ : ∀ {Γ α β γ δ ε ζ η} → Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) → Term Γ α → Term Γ β → Term Γ γ → Term Γ δ → Term Γ ε → Term Γ ζ → Term Γ η app₆ f x = app₅ (app f x) UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ uncurriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ uncurriedConst constant = const constant CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′ CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′ curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ curryTermConstructor {∅} k = k ∅ curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts)) curriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ curriedConst constant = curryTermConstructor (uncurriedConst constant) -- HOAS-like smart constructors for lambdas, for different arities. abs₁ : ∀ {Γ τ₁ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → (x : Term Γ′ τ₁) → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ)) abs₁ {Γ} {τ₁} = λ f → abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this)) abs₂ : ∀ {Γ τ₁ τ₂ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ)) abs₂ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₁ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₃ : ∀ {Γ τ₁ τ₂ τ₃ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ)) abs₃ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₂ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₄ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ)) abs₄ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₃ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₅ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ)) abs₅ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₄ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁))) abs₆ : ∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ₆ τ} → (∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ₆ → Term Γ′ τ) → (Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ₆ ⇒ τ)) abs₆ f = abs₁ (λ {_} {Γ≼Γ′} x₁ → abs₅ (λ {_} {Γ′≼Γ′₁} → f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
Introduce HOAS-like smart constructors for lambdas
Introduce HOAS-like smart constructors for lambdas Old-commit-hash: 6bf4939f8ec84c611d98f6f2395332943573dfe5
Agda
mit
inc-lc/ilc-agda
8514aefef6973b813691fb922e6d44e072cfc75d
generic-zero-knowledge-interactive.agda
generic-zero-knowledge-interactive.agda
open import Type open import Data.Bool.NP as Bool hiding (check) open import Data.Nat open import Data.Maybe open import Data.Product.NP open import Data.Bits open import Function.NP open import Relation.Binary.PropositionalEquality.NP open import sum module generic-zero-knowledge-interactive where -- A random argument, this is only a formal notation to -- indicate that the argument is supposed to be picked -- at random uniformly. (do not confuse with our randomness -- monad). record ↺ (A : ★) : ★ where constructor rand field get : A module M (Permutation : ★) (_⁻¹ : Endo Permutation) (sumπ : Sum Permutation) (μπ : SumProp sumπ) (Rₚ-xtra : ★) -- extra prover/adversary randomness (sumRₚ-xtra : Sum Rₚ-xtra) (μRₚ-xtra : SumProp sumRₚ-xtra) (Problem : ★) (_==_ : Problem → Problem → Bit) (==-refl : ∀ {pb} → (pb == pb) ≡ true) (_∙P_ : Permutation → Endo Problem) (⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x) (Solution : ★) (_∙S_ : Permutation → Endo Solution) (⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x) (check : Problem → Solution → Bit) (check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s) (easy-pb : Permutation → Problem) (easy-sol : Permutation → Solution) (check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true) where -- prover/adversary randomness Rₚ : ★ Rₚ = Permutation × Rₚ-xtra sumRₚ : Sum Rₚ sumRₚ = sumπ ×Sum sumRₚ-xtra μRₚ : SumProp sumRₚ μRₚ = μπ ×μ μRₚ-xtra R = Bit × Rₚ sumR : Sum R sumR = sumBit ×Sum sumRₚ μR : SumProp sumR μR = μBit ×μ μRₚ check-π : Problem → Solution → Rₚ → Bit check-π p s (π , _) = check (π ∙P p) (π ∙S s) otp-∙-check : let #_ = count μRₚ in ∀ p₀ s₀ p₁ s₁ → check p₀ s₀ ≡ check p₁ s₁ → #(check-π p₀ s₀) ≡ #(check-π p₁ s₁) otp-∙-check p₀ s₀ p₁ s₁ check-pf = count-ext μRₚ {f = check-π p₀ s₀} {check-π p₁ s₁} (λ π,r → check-π p₀ s₀ π,r ≡⟨ check-∙ p₀ s₀ (proj₁ π,r) ⟩ check p₀ s₀ ≡⟨ check-pf ⟩ check p₁ s₁ ≡⟨ sym (check-∙ p₁ s₁ (proj₁ π,r)) ⟩ check-π p₁ s₁ π,r ∎) where open ≡-Reasoning #_ : (↺ (Bit × Permutation × Rₚ-xtra) → Bit) → ℕ # f = count μR (f ∘ rand) _≡#_ : (f g : ↺ (Bit × Rₚ) → Bit) → ★ f ≡# g = # f ≡ # g {- otp-∙ : let otp = λ O pb s → count μRₚ (λ { (π , _) → O (π ∙P pb) (π ∙S s) }) in ∀ pb₀ s₀ pb₁ s₁ → check pb₀ s₀ ≡ check pb₁ s₁ → (O : _ → _ → Bit) → otp O pb₀ s₀ ≡ otp O pb₁ s₁ otp-∙ pb₀ s₀ pb₁ s₁ check-pf O = {!(μπ ×Sum-proj₂ μRₚ-xtra ?!} -} Answer : Bit → ★ Answer false{-0b-} = Permutation Answer true {-1b-} = Solution answer : Permutation → Solution → ∀ b → Answer b answer π _ false = π answer _ s true = s -- The prover is the advesary in the generic terminology, -- and the verifier is the challenger. DepProver : ★ DepProver = Problem → ↺ Rₚ → (b : Bit) → Problem × Answer b Prover₀ : ★ Prover₀ = Problem → ↺ Rₚ → Problem × Permutation Prover₁ : ★ Prover₁ = Problem → ↺ Rₚ → Problem × Solution Prover : ★ Prover = Prover₀ × Prover₁ prover : DepProver → Prover prover dpr = (λ pb r → dpr pb r 0b) , (λ pb r → dpr pb r 1b) depProver : Prover → DepProver depProver (pr₀ , pr₁) pb r false = pr₀ pb r depProver (pr₀ , pr₁) pb r true = pr₁ pb r -- Here we show that the explicit commitment step seems useless given -- the formalization. The verifier can "trust" the prover on the fact -- that any choice is going to be govern only by the problem and the -- randomness. module WithCommitment (Commitment : ★) (AnswerWC : Bit → ★) (reveal : ∀ b → Commitment → AnswerWC b → Problem × Answer b) where ProverWC = (Problem → Rₚ → Commitment) × (Problem → Rₚ → (b : Bit) → AnswerWC b) depProver' : ProverWC → DepProver depProver' (pr₀ , pr₁) pb (rand rₚ) b = reveal b (pr₀ pb rₚ) (pr₁ pb rₚ b) Verif : Problem → ∀ b → Problem × Answer b → Bit Verif pb false{-0b-} (π∙pb , π) = (π ∙P pb) == π∙pb Verif pb true {-1b-} (π∙pb , π∙s) = check π∙pb π∙s _⇄′_ : Problem → DepProver → Bit → ↺ Rₚ → Bit (pb ⇄′ pr) b (rand rₚ) = Verif pb b (pr pb (rand rₚ) b) _⇄_ : Problem → DepProver → ↺ (Bit × Rₚ) → Bit (pb ⇄ pr) (rand (b , rₚ)) = (pb ⇄′ pr) b (rand rₚ) _⇄''_ : Problem → Prover → ↺ (Bit × Rₚ) → Bit pb ⇄'' pr = pb ⇄ depProver pr honest : (Problem → Maybe Solution) → DepProver honest solve pb (rand (π , rₚ)) b = (π ∙P pb , answer π sol b) module Honest where sol : Solution sol with solve pb ... | just sol = π ∙S sol ... | nothing = π ∙S easy-sol π module WithCorrectSolver (pb : Problem) (s : Solution) (check-s : check pb s ≡ true) where -- When the honest prover has a solution, he gets accepted -- unconditionally by the verifier. honest-accepted : ∀ r → (pb ⇄ honest (const (just s))) r ≡ 1b honest-accepted (rand (true , π , rₚ)) rewrite check-∙ pb s π = check-s honest-accepted (rand (false , π , rₚ)) = ==-refl honest-⅁ = λ pb s → (pb ⇄ honest (const (just s))) module HonestLeakZeroKnowledge (pb₀ pb₁ : Problem) (s₀ s₁ : Solution) (check-pf : check pb₀ s₀ ≡ check pb₁ s₁) where helper : ∀ rₚ → Bool.toℕ ((pb₀ ⇄′ honest (const (just s₀))) 0b (rand rₚ)) ≡ Bool.toℕ ((pb₁ ⇄′ honest (const (just s₁))) 0b (rand rₚ)) helper (π , rₚ) rewrite ==-refl {π ∙P pb₀} | ==-refl {π ∙P pb₁} = refl honest-leak : honest-⅁ pb₀ s₀ ≡# honest-⅁ pb₁ s₁ honest-leak rewrite otp-∙-check pb₀ s₀ pb₁ s₁ check-pf | sum-ext μRₚ helper = refl module HonestLeakZeroKnowledge' (pb : Problem) (s₀ s₁ : Solution) (check-pf : check pb s₀ ≡ check pb s₁) where honest-leak : honest-⅁ pb s₀ ≡# honest-⅁ pb s₁ honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s₀ s₁ check-pf -- Predicts b=b′ cheater : ∀ b′ → DepProver cheater b′ pb (rand (π , _)) b = π ∙P (case b′ 0→ pb 1→ easy-pb π) , answer π (π ∙S easy-sol π) b -- If cheater predicts correctly, verifer accepts him cheater-accepted : ∀ b pb rₚ → (pb ⇄′ cheater b) b rₚ ≡ 1b cheater-accepted true pb (rand (π , rₚ)) = check-easy π cheater-accepted false pb (rand (π , rₚ)) = ==-refl -- If cheater predicts incorrecty, verifier rejects him module CheaterRejected (pb : Problem) (not-easy-sol : ∀ π → check (π ∙P pb) (π ∙S easy-sol π) ≡ false) (not-easy-pb : ∀ π → ((π ∙P pb) == (π ∙P easy-pb π)) ≡ false) where cheater-rejected : ∀ b rₚ → (pb ⇄′ cheater (not b)) b rₚ ≡ 0b cheater-rejected true (rand (π , rₚ)) = not-easy-sol π cheater-rejected false (rand (π , rₚ)) = not-easy-pb π module DLog (ℤq : ★) (_⊞_ : ℤq → ℤq → ℤq) (⊟_ : ℤq → ℤq) (G : ★) (g : G) (_^_ : G → ℤq → G) (_∙_ : G → G → G) (⊟-⊞ : ∀ π x → (⊟ π) ⊞ (π ⊞ x) ≡ x) (^⊟-∙ : ∀ α β x → ((α ^ (⊟ x)) ∙ ((α ^ x) ∙ β)) ≡ β) -- (∙-assoc : ∀ α β γ → α ∙ (β ∙ γ) ≡ (α ∙ β) ∙ γ) (dist-^-⊞ : ∀ α x y → α ^ (x ⊞ y) ≡ (α ^ x) ∙ (α ^ y)) (_==_ : G → G → Bool) (==-refl : ∀ {α} → (α == α) ≡ true) (==-cong-∙ : ∀ {α β b} γ → α == β ≡ b → (γ ∙ α) == (γ ∙ β) ≡ b) (==-true : ∀ {α β} → α == β ≡ true → α ≡ β) (sumℤq : Sum ℤq) (μℤq : SumProp sumℤq) (Rₚ-xtra : ★) -- extra prover/adversary randomness (sumRₚ-xtra : Sum Rₚ-xtra) (μRₚ-xtra : SumProp sumRₚ-xtra) (some-ℤq : ℤq) where Permutation = ℤq Problem = G Solution = ℤq _⁻¹ : Endo Permutation π ⁻¹ = ⊟ π g^_ : ℤq → G g^ x = g ^ x _∙P_ : Permutation → Endo Problem π ∙P p = g^ π ∙ p ⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x ⁻¹-inverseP π x rewrite ^⊟-∙ g x π = refl _∙S_ : Permutation → Endo Solution π ∙S s = π ⊞ s ⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x ⁻¹-inverseS = ⊟-⊞ check : Problem → Solution → Bit check p s = p == g^ s check-∙' : ∀ p s π b → check p s ≡ b → check (π ∙P p) (π ∙S s) ≡ b check-∙' p s π true check-p-s rewrite dist-^-⊞ g π s | ==-true check-p-s = ==-refl check-∙' p s π false check-p-s rewrite dist-^-⊞ g π s = ==-cong-∙ (g^ π) check-p-s check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s check-∙ p s π = check-∙' p s π (check p s) refl easy-sol : Permutation → Solution easy-sol π = some-ℤq easy-pb : Permutation → Problem easy-pb π = g^(easy-sol π) check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true check-easy π rewrite dist-^-⊞ g π (easy-sol π) = ==-refl open M Permutation _⁻¹ sumℤq μℤq Rₚ-xtra sumRₚ-xtra μRₚ-xtra Problem _==_ ==-refl _∙P_ ⁻¹-inverseP Solution _∙S_ ⁻¹-inverseS check check-∙ easy-pb easy-sol check-easy -- -} -- -} -- -} -- -} -- -}
open import Type open import Data.Bool.NP as Bool hiding (check) open import Data.Nat open import Data.Maybe open import Data.Product.NP open import Data.Bits open import Function.NP open import Relation.Binary.PropositionalEquality.NP open import sum module generic-zero-knowledge-interactive where -- A random argument, this is only a formal notation to -- indicate that the argument is supposed to be picked -- at random uniformly. (do not confuse with our randomness -- monad). record ↺ (A : ★) : ★ where constructor rand field get : A module M (Permutation : ★) (_⁻¹ : Endo Permutation) (μπ : SumProp Permutation) (Rₚ-xtra : ★) -- extra prover/adversary randomness (μRₚ-xtra : SumProp Rₚ-xtra) (Problem : ★) (_==_ : Problem → Problem → Bit) (==-refl : ∀ {pb} → (pb == pb) ≡ true) (_∙P_ : Permutation → Endo Problem) (⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x) (Solution : ★) (_∙S_ : Permutation → Endo Solution) (⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x) (check : Problem → Solution → Bit) (check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s) (easy-pb : Permutation → Problem) (easy-sol : Permutation → Solution) (check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true) where -- prover/adversary randomness Rₚ : ★ Rₚ = Permutation × Rₚ-xtra μRₚ : SumProp Rₚ μRₚ = μπ ×μ μRₚ-xtra R = Bit × Rₚ μR : SumProp R μR = μBit ×μ μRₚ check-π : Problem → Solution → Rₚ → Bit check-π p s (π , _) = check (π ∙P p) (π ∙S s) otp-∙-check : let #_ = count μRₚ in ∀ p₀ s₀ p₁ s₁ → check p₀ s₀ ≡ check p₁ s₁ → #(check-π p₀ s₀) ≡ #(check-π p₁ s₁) otp-∙-check p₀ s₀ p₁ s₁ check-pf = count-ext μRₚ {f = check-π p₀ s₀} {check-π p₁ s₁} (λ π,r → check-π p₀ s₀ π,r ≡⟨ check-∙ p₀ s₀ (proj₁ π,r) ⟩ check p₀ s₀ ≡⟨ check-pf ⟩ check p₁ s₁ ≡⟨ sym (check-∙ p₁ s₁ (proj₁ π,r)) ⟩ check-π p₁ s₁ π,r ∎) where open ≡-Reasoning #_ : (↺ (Bit × Permutation × Rₚ-xtra) → Bit) → ℕ # f = count μR (f ∘ rand) _≡#_ : (f g : ↺ (Bit × Rₚ) → Bit) → ★ f ≡# g = # f ≡ # g {- otp-∙ : let otp = λ O pb s → count μRₚ (λ { (π , _) → O (π ∙P pb) (π ∙S s) }) in ∀ pb₀ s₀ pb₁ s₁ → check pb₀ s₀ ≡ check pb₁ s₁ → (O : _ → _ → Bit) → otp O pb₀ s₀ ≡ otp O pb₁ s₁ otp-∙ pb₀ s₀ pb₁ s₁ check-pf O = {!(μπ ×Sum-proj₂ μRₚ-xtra ?!} -} Answer : Bit → ★ Answer false{-0b-} = Permutation Answer true {-1b-} = Solution answer : Permutation → Solution → ∀ b → Answer b answer π _ false = π answer _ s true = s -- The prover is the advesary in the generic terminology, -- and the verifier is the challenger. DepProver : ★ DepProver = Problem → ↺ Rₚ → (b : Bit) → Problem × Answer b Prover₀ : ★ Prover₀ = Problem → ↺ Rₚ → Problem × Permutation Prover₁ : ★ Prover₁ = Problem → ↺ Rₚ → Problem × Solution Prover : ★ Prover = Prover₀ × Prover₁ prover : DepProver → Prover prover dpr = (λ pb r → dpr pb r 0b) , (λ pb r → dpr pb r 1b) depProver : Prover → DepProver depProver (pr₀ , pr₁) pb r false = pr₀ pb r depProver (pr₀ , pr₁) pb r true = pr₁ pb r -- Here we show that the explicit commitment step seems useless given -- the formalization. The verifier can "trust" the prover on the fact -- that any choice is going to be govern only by the problem and the -- randomness. module WithCommitment (Commitment : ★) (AnswerWC : Bit → ★) (reveal : ∀ b → Commitment → AnswerWC b → Problem × Answer b) where ProverWC = (Problem → Rₚ → Commitment) × (Problem → Rₚ → (b : Bit) → AnswerWC b) depProver' : ProverWC → DepProver depProver' (pr₀ , pr₁) pb (rand rₚ) b = reveal b (pr₀ pb rₚ) (pr₁ pb rₚ b) Verif : Problem → ∀ b → Problem × Answer b → Bit Verif pb false{-0b-} (π∙pb , π) = (π ∙P pb) == π∙pb Verif pb true {-1b-} (π∙pb , π∙s) = check π∙pb π∙s _⇄′_ : Problem → DepProver → Bit → ↺ Rₚ → Bit (pb ⇄′ pr) b (rand rₚ) = Verif pb b (pr pb (rand rₚ) b) _⇄_ : Problem → DepProver → ↺ (Bit × Rₚ) → Bit (pb ⇄ pr) (rand (b , rₚ)) = (pb ⇄′ pr) b (rand rₚ) _⇄''_ : Problem → Prover → ↺ (Bit × Rₚ) → Bit pb ⇄'' pr = pb ⇄ depProver pr honest : (Problem → Maybe Solution) → DepProver honest solve pb (rand (π , rₚ)) b = (π ∙P pb , answer π sol b) module Honest where sol : Solution sol with solve pb ... | just sol = π ∙S sol ... | nothing = π ∙S easy-sol π module WithCorrectSolver (pb : Problem) (s : Solution) (check-s : check pb s ≡ true) where -- When the honest prover has a solution, he gets accepted -- unconditionally by the verifier. honest-accepted : ∀ r → (pb ⇄ honest (const (just s))) r ≡ 1b honest-accepted (rand (true , π , rₚ)) rewrite check-∙ pb s π = check-s honest-accepted (rand (false , π , rₚ)) = ==-refl honest-⅁ = λ pb s → (pb ⇄ honest (const (just s))) module HonestLeakZeroKnowledge (pb₀ pb₁ : Problem) (s₀ s₁ : Solution) (check-pf : check pb₀ s₀ ≡ check pb₁ s₁) where helper : ∀ rₚ → Bool.toℕ ((pb₀ ⇄′ honest (const (just s₀))) 0b (rand rₚ)) ≡ Bool.toℕ ((pb₁ ⇄′ honest (const (just s₁))) 0b (rand rₚ)) helper (π , rₚ) rewrite ==-refl {π ∙P pb₀} | ==-refl {π ∙P pb₁} = refl honest-leak : honest-⅁ pb₀ s₀ ≡# honest-⅁ pb₁ s₁ honest-leak rewrite otp-∙-check pb₀ s₀ pb₁ s₁ check-pf | sum-ext μRₚ helper = refl module HonestLeakZeroKnowledge' (pb : Problem) (s₀ s₁ : Solution) (check-pf : check pb s₀ ≡ check pb s₁) where honest-leak : honest-⅁ pb s₀ ≡# honest-⅁ pb s₁ honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s₀ s₁ check-pf -- Predicts b=b′ cheater : ∀ b′ → DepProver cheater b′ pb (rand (π , _)) b = π ∙P (case b′ 0→ pb 1→ easy-pb π) , answer π (π ∙S easy-sol π) b -- If cheater predicts correctly, verifer accepts him cheater-accepted : ∀ b pb rₚ → (pb ⇄′ cheater b) b rₚ ≡ 1b cheater-accepted true pb (rand (π , rₚ)) = check-easy π cheater-accepted false pb (rand (π , rₚ)) = ==-refl -- If cheater predicts incorrecty, verifier rejects him module CheaterRejected (pb : Problem) (not-easy-sol : ∀ π → check (π ∙P pb) (π ∙S easy-sol π) ≡ false) (not-easy-pb : ∀ π → ((π ∙P pb) == (π ∙P easy-pb π)) ≡ false) where cheater-rejected : ∀ b rₚ → (pb ⇄′ cheater (not b)) b rₚ ≡ 0b cheater-rejected true (rand (π , rₚ)) = not-easy-sol π cheater-rejected false (rand (π , rₚ)) = not-easy-pb π module DLog (ℤq : ★) (_⊞_ : ℤq → ℤq → ℤq) (⊟_ : ℤq → ℤq) (G : ★) (g : G) (_^_ : G → ℤq → G) (_∙_ : G → G → G) (⊟-⊞ : ∀ π x → (⊟ π) ⊞ (π ⊞ x) ≡ x) (^⊟-∙ : ∀ α β x → ((α ^ (⊟ x)) ∙ ((α ^ x) ∙ β)) ≡ β) -- (∙-assoc : ∀ α β γ → α ∙ (β ∙ γ) ≡ (α ∙ β) ∙ γ) (dist-^-⊞ : ∀ α x y → α ^ (x ⊞ y) ≡ (α ^ x) ∙ (α ^ y)) (_==_ : G → G → Bool) (==-refl : ∀ {α} → (α == α) ≡ true) (==-cong-∙ : ∀ {α β b} γ → α == β ≡ b → (γ ∙ α) == (γ ∙ β) ≡ b) (==-true : ∀ {α β} → α == β ≡ true → α ≡ β) (μℤq : SumProp ℤq) (Rₚ-xtra : ★) -- extra prover/adversary randomness (μRₚ-xtra : SumProp Rₚ-xtra) (some-ℤq : ℤq) where Permutation = ℤq Problem = G Solution = ℤq _⁻¹ : Endo Permutation π ⁻¹ = ⊟ π g^_ : ℤq → G g^ x = g ^ x _∙P_ : Permutation → Endo Problem π ∙P p = g^ π ∙ p ⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x ⁻¹-inverseP π x rewrite ^⊟-∙ g x π = refl _∙S_ : Permutation → Endo Solution π ∙S s = π ⊞ s ⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x ⁻¹-inverseS = ⊟-⊞ check : Problem → Solution → Bit check p s = p == g^ s check-∙' : ∀ p s π b → check p s ≡ b → check (π ∙P p) (π ∙S s) ≡ b check-∙' p s π true check-p-s rewrite dist-^-⊞ g π s | ==-true check-p-s = ==-refl check-∙' p s π false check-p-s rewrite dist-^-⊞ g π s = ==-cong-∙ (g^ π) check-p-s check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s check-∙ p s π = check-∙' p s π (check p s) refl easy-sol : Permutation → Solution easy-sol π = some-ℤq easy-pb : Permutation → Problem easy-pb π = g^(easy-sol π) check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true check-easy π rewrite dist-^-⊞ g π (easy-sol π) = ==-refl open M Permutation _⁻¹ μℤq Rₚ-xtra μRₚ-xtra Problem _==_ ==-refl _∙P_ ⁻¹-inverseP Solution _∙S_ ⁻¹-inverseS check check-∙ easy-pb easy-sol check-easy -- -} -- -} -- -} -- -} -- -}
update generic ZK
update generic ZK
Agda
bsd-3-clause
crypto-agda/crypto-agda
569e98b5b6d8a84329f54249ad3f83477357f717
flipbased-implem.agda
flipbased-implem.agda
module flipbased-implem where open import Function open import Data.Bits open import Data.Nat.NP open import Data.Vec open import Relation.Binary import Relation.Binary.PropositionalEquality as ≡ import Data.Fin as Fin open ≡ using (_≗_; _≡_) open Fin using (Fin; suc) import flipbased -- “↺ n A” reads like: “toss n coins and then return a value of type A” record ↺ {a} n (A : Set a) : Set a where constructor mk field run↺ : Bits n → A open ↺ public private -- If you are not allowed to toss any coin, then you are deterministic. Det : ∀ {a} → Set a → Set a Det = ↺ 0 runDet : ∀ {a} {A : Set a} → Det A → A runDet f = run↺ f [] toss : ↺ 1 Bit toss = mk head return↺ : ∀ {n a} {A : Set a} → A → ↺ n A return↺ = mk ∘ const map↺ : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → ↺ n A → ↺ n B map↺ f x = mk (f ∘ run↺ x) -- map↺ f x ≗ x >>=′ (return {0} ∘ f) join↺ : ∀ {n₁ n₂ a} {A : Set a} → ↺ n₁ (↺ n₂ A) → ↺ (n₁ + n₂) A join↺ {n₁} x = mk (λ bs → run↺ (run↺ x (take _ bs)) (drop n₁ bs)) -- join↺ x = x >>= id comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → ↺ m A → ↺ n A comap f (mk g) = mk (g ∘ f) private take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n take≤ z≤n _ = [] take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → ↺ m A → ↺ n A weaken≤ p = comap (take≤ p) open flipbased ↺ toss weaken≤ return↺ map↺ join↺ public _≗↺_ : ∀ {c a} {A : Set a} (f g : ↺ c A) → Set a f ≗↺ g = run↺ f ≗ run↺ g ⅁ : ℕ → Set ⅁ n = ↺ n Bit _≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ⅁ c) → Set ⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b ≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c}) ≗⅁-trans p q b R = ≡.trans (p b R) (q b R) count↺ᶠ : ∀ {c} → ⅁ c → Fin (suc (2^ c)) count↺ᶠ f = #⟨ run↺ f ⟩ᶠ count↺ : ∀ {c} → ⅁ c → ℕ count↺ = Fin.toℕ ∘ count↺ᶠ _∼[_]⅁_ : ∀ {m n} → ⅁ m → (ℕ → ℕ → Set) → ⅁ n → Set _∼[_]⅁_ {m} {n} f _∼_ g = ⟨2^ n * count↺ f ⟩ ∼ ⟨2^ m * count↺ g ⟩ _∼[_]⅁′_ : ∀ {n} → ⅁ n → (ℕ → ℕ → Set) → ⅁ n → Set _∼[_]⅁′_ {n} f _∼_ g = count↺ f ∼ count↺ g _≈⅁_ : ∀ {m n} → ⅁ m → ⅁ n → Set f ≈⅁ g = f ∼[ _≡_ ]⅁ g _≈⅁′_ : ∀ {n} (f g : ⅁ n) → Set f ≈⅁′ g = f ∼[ _≡_ ]⅁′ g ≈⅁-refl : ∀ {n} {f : ⅁ n} → f ≈⅁ f ≈⅁-refl = ≡.refl ≈⅁-sym : ∀ {n} → Symmetric {A = ⅁ n} _≈⅁_ ≈⅁-sym = ≡.sym ≈⅁-trans : ∀ {n} → Transitive {A = ⅁ n} _≈⅁_ ≈⅁-trans = ≡.trans ≗⇒≈⅁ : ∀ {c} {f g : ⅁ c} → f ≗↺ g → f ≈⅁ g ≗⇒≈⅁ pf rewrite ext-# pf = ≡.refl ≈⅁′⇒≈⅁ : ∀ {n} {f g : ⅁ n} → f ≈⅁′ g → f ≈⅁ g ≈⅁′⇒≈⅁ eq rewrite eq = ≡.refl ≈⅁⇒≈⅁′ : ∀ {n} {f g : ⅁ n} → f ≈⅁ g → f ≈⅁′ g ≈⅁⇒≈⅁′ {n} = 2^-inj n ≈⅁-cong : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ≈⅁ f' → g ≈⅁ g' ≈⅁-cong f≗g f'≗g' f≈f' rewrite ext-# f≗g | ext-# f'≗g' = f≈f' ≈⅁′-cong : ∀ {c} {f g f' g' : ⅁ c} → f ≗↺ g → f' ≗↺ g' → f ≈⅁′ f' → g ≈⅁′ g' ≈⅁′-cong f≗g f'≗g' f≈f' rewrite ext-# f≗g | ext-# f'≗g' = f≈f'
module flipbased-implem where open import Function open import Data.Bits open import Data.Nat.NP open import Data.Vec open import Relation.Binary import Relation.Binary.PropositionalEquality as ≡ import Data.Fin as Fin open ≡ using (_≗_; _≡_) open Fin using (Fin; suc) import flipbased -- “↺ n A” reads like: “toss n coins and then return a value of type A” record ↺ {a} n (A : Set a) : Set a where constructor mk field run↺ : Bits n → A open ↺ public private -- If you are not allowed to toss any coin, then you are deterministic. Det : ∀ {a} → Set a → Set a Det = ↺ 0 runDet : ∀ {a} {A : Set a} → Det A → A runDet f = run↺ f [] toss : ↺ 1 Bit toss = mk head return↺ : ∀ {n a} {A : Set a} → A → ↺ n A return↺ = mk ∘ const map↺ : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → ↺ n A → ↺ n B map↺ f x = mk (f ∘ run↺ x) -- map↺ f x ≗ x >>=′ (return {0} ∘ f) join↺ : ∀ {n₁ n₂ a} {A : Set a} → ↺ n₁ (↺ n₂ A) → ↺ (n₁ + n₂) A join↺ {n₁} x = mk (λ bs → run↺ (run↺ x (take _ bs)) (drop n₁ bs)) -- join↺ x = x >>= id comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → ↺ m A → ↺ n A comap f (mk g) = mk (g ∘ f) private take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n take≤ z≤n _ = [] take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → ↺ m A → ↺ n A weaken≤ p = comap (take≤ p) open flipbased ↺ toss weaken≤ return↺ map↺ join↺ public _≗↺_ : ∀ {c a} {A : Set a} (f g : ↺ c A) → Set a f ≗↺ g = run↺ f ≗ run↺ g ⅁ : ℕ → Set ⅁ n = ↺ n Bit _≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ⅁ c) → Set ⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b ≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c}) ≗⅁-trans p q b R = ≡.trans (p b R) (q b R) count↺ᶠ : ∀ {c} → ⅁ c → Fin (suc (2^ c)) count↺ᶠ f = #⟨ run↺ f ⟩ᶠ count↺ : ∀ {c} → ⅁ c → ℕ count↺ f = #⟨ run↺ f ⟩ ≗↺-cong-# : ∀ {n} {f g : ⅁ n} → f ≗↺ g → count↺ f ≡ count↺ g ≗↺-cong-# {f = f} {g} = ≗-cong-# (run↺ f) (run↺ g) _∼[_]⅁_ : ∀ {m n} → ⅁ m → (ℕ → ℕ → Set) → ⅁ n → Set _∼[_]⅁_ {m} {n} f _∼_ g = ⟨2^ n * count↺ f ⟩ ∼ ⟨2^ m * count↺ g ⟩ _∼[_]⅁′_ : ∀ {n} → ⅁ n → (ℕ → ℕ → Set) → ⅁ n → Set _∼[_]⅁′_ {n} f _∼_ g = count↺ f ∼ count↺ g _≈⅁_ : ∀ {m n} → ⅁ m → ⅁ n → Set f ≈⅁ g = f ∼[ _≡_ ]⅁ g _≈⅁′_ : ∀ {n} (f g : ⅁ n) → Set f ≈⅁′ g = f ∼[ _≡_ ]⅁′ g ≈⅁-refl : ∀ {n} {f : ⅁ n} → f ≈⅁ f ≈⅁-refl = ≡.refl ≈⅁-sym : ∀ {n} → Symmetric {A = ⅁ n} _≈⅁_ ≈⅁-sym = ≡.sym ≈⅁-trans : ∀ {n} → Transitive {A = ⅁ n} _≈⅁_ ≈⅁-trans = ≡.trans ≗⇒≈⅁ : ∀ {c} {f g : ⅁ c} → f ≗↺ g → f ≈⅁ g ≗⇒≈⅁ pf rewrite ≗↺-cong-# pf = ≡.refl ≈⅁′⇒≈⅁ : ∀ {n} {f g : ⅁ n} → f ≈⅁′ g → f ≈⅁ g ≈⅁′⇒≈⅁ eq rewrite eq = ≡.refl ≈⅁⇒≈⅁′ : ∀ {n} {f g : ⅁ n} → f ≈⅁ g → f ≈⅁′ g ≈⅁⇒≈⅁′ {n} = 2^-inj n ≈⅁-cong : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ≈⅁ f' → g ≈⅁ g' ≈⅁-cong f≗g f'≗g' f≈f' rewrite ≗↺-cong-# f≗g | ≗↺-cong-# f'≗g' = f≈f' ≈⅁′-cong : ∀ {c} {f g f' g' : ⅁ c} → f ≗↺ g → f' ≗↺ g' → f ≈⅁′ f' → g ≈⅁′ g' ≈⅁′-cong f≗g f'≗g' f≈f' rewrite ≗↺-cong-# f≗g | ≗↺-cong-# f'≗g' = f≈f' data Rat : Set where _/_ : (num denom : ℕ) → Rat Pr[_≡1] : ∀ {n} (f : ⅁ n) → Rat Pr[_≡1] {n} f = count↺ f / 2^ n
Switch count↺ to be based on search
Switch count↺ to be based on search
Agda
bsd-3-clause
crypto-agda/crypto-agda
b590f8a3ed001a65516e5f2211013e1a46683692
ZK/PartialHeliosVerifier.agda
ZK/PartialHeliosVerifier.agda
{-# OPTIONS --without-K #-} module ZK.PartialHeliosVerifier where open import Type open import Function hiding (case_of_) open import Data.Bool.Base open import Data.Product open import Data.List.Base using (List; []; _∷_; and; foldr) open import Data.String.Base using (String) open import FFI.JS as JS hiding (_+_; _/_; _*_; join) open import FFI.JS.BigI as BigI import FFI.JS.Console as Console import FFI.JS.Process as Process import FFI.JS.FS as FS open import FFI.JS.SHA1 open import FFI.JS.Proc open import Control.Process.Type join : String → List String → String join sep [] = "" join sep (x ∷ xs) = x ++ foldr (λ y z → sep ++ y ++ z) "" xs {- instance showBigI = mk λ _ → showString ∘ BigI.toString -} G = BigI ℤq = BigI BigI▹G : BigI → G BigI▹G = id BigI▹ℤq : BigI → ℤq BigI▹ℤq = id non0I : BigI → BigI non0I x with equals x 0I ... | true = throw "Should be non zero!" 0I ... | false = x module BigICG (p q : BigI) where _^_ : G → ℤq → G _·_ : G → G → G _/_ : G → G → G _+_ : ℤq → ℤq → ℤq _*_ : ℤq → ℤq → ℤq _==_ : (x y : G) → Bool _^_ = λ x y → modPow x y p _·_ = λ x y → mod (multiply x y) p _/_ = λ x y → mod (multiply x (modInv (non0I y) p)) p _+_ = λ x y → mod (add x y) q _*_ = λ x y → mod (multiply x y) q _==_ = equals sumI : List BigI → BigI sumI = foldr _+_ 0I bignum : Number → BigI bignum n = bigI (Number▹String n) "10" -- TODO check with undefined bigdec : JSValue → BigI bigdec v = bigI (castString v) "10" {- print : {A : Set}{{_ : Show A}} → A → Callback0 print = Console.log ∘ show -} PubKey = G EncRnd = ℤq {- randomness used for encryption of ct -} Message = G {- plain text message -} Challenge = ℤq Response = ℤq CommitmentPart = G CipherTextPart = G module HeliosVerifyV3 (g : G) p q (y : PubKey) where open BigICG p q verify-chaum-pedersen : (α β : CipherTextPart)(M : Message)(A B : G)(c : Challenge)(s : Response) → Bool verify-chaum-pedersen α β M A B c s = trace "α=" α λ _ → trace "β=" β λ _ → trace "M=" M λ _ → trace "A=" A λ _ → trace "B=" B λ _ → trace "c=" c λ _ → trace "s=" s λ _ → (g ^ s) == (A · (α ^ c)) ∧ (y ^ s) == (B · ((β / M) ^ c)) verify-individual-proof : (α β : CipherTextPart)(ix : Number)(π : JSValue) → Bool verify-individual-proof α β ix π = trace "m=" m λ _ → trace "M=" M λ _ → trace "commitmentA=" A λ _ → trace "commitmentB=" B λ _ → trace "challenge=" c λ _ → trace "response=" s λ _ → res where m = bignum ix M = g ^ m A = bigdec (π ·« "commitment" » ·« "A" ») B = bigdec (π ·« "commitment" » ·« "B" ») c = bigdec (π ·« "challenge" ») s = bigdec (π ·« "response" ») res = verify-chaum-pedersen α β M A B c s -- Conform to HeliosV3 but it is too weak. -- One should follow a "Strong Fiat Shamir" transformation -- from interactive ZK proof to non-interactive ZK proofs. -- Namely one should hash the statement as well. -- -- SHA1(A0 + "," + B0 + "," + A1 + "," + B1 + ... + "Amax" + "," + Bmax) hash-commitments : JSArray JSValue → BigI hash-commitments πs = fromHex $ trace-call "SHA1(commitments)=" SHA1 $ trace-call "commitments=" (join ",") $ decodeJSArray πs λ _ π → (castString (π ·« "commitment" » ·« "A" »)) ++ "," ++ (castString (π ·« "commitment" » ·« "B" »)) sum-challenges : JSArray JSValue → BigI sum-challenges πs = sumI $ decodeJSArray πs λ _ π → bigdec (π ·« "challenge" ») verify-challenges : JSArray JSValue → Bool verify-challenges πs = trace "hash(commitments)=" h λ _ → trace "sum(challenge)=" c λ _ → h == c where h = hash-commitments πs c = sum-challenges πs verify-choice : (v : JSValue)(πs : JSArray JSValue) → Bool verify-choice v πs = trace "α=" α λ _ → trace "β=" β λ _ → verify-challenges πs ∧ and (decodeJSArray πs $ verify-individual-proof α β) where α = bigdec (v ·« "alpha" ») β = bigdec (v ·« "beta" ») verify-choices : (choices πs : JSArray JSValue) → Bool verify-choices choices πs = trace "TODO: check the array size of choices πs together election data" "" λ _ → and $ decodeJSArray choices λ ix choice → trace "verify choice " ix λ _ → verify-choice choice (castJSArray (πs Array[ ix ])) verify-answer : (answer : JSValue) → Bool verify-answer answer = trace "TODO: CHECK the overall_proof" "" λ _ → verify-choices choices individual-proofs where choices = answer ·« "choices" »A individual-proofs = answer ·« "individual_proofs" »A overall-proof = answer ·« "overall_proof" »A verify-answers : (answers : JSArray JSValue) → Bool verify-answers answers = and $ decodeJSArray answers λ ix a → trace "verify answer " ix λ _ → verify-answer a {- verify-election-hash = "" -- notice the whitespaces and the alphabetical order of keys -- {"email": ["[email protected]", "[email protected]"], "first_name": "Ben", "last_name": "Adida"} computed_hash = base64.b64encode(hash.new(election.toJSON()).digest())[:-1] computed_hash == vote.election_hash: "" -} -- module _ {-(election : JSValue)-} where verify-ballot : (ballot : JSValue) → Bool verify-ballot ballot = trace "TODO: CHECK the election_hash: " election_hash λ _ → trace "TODO: CHECK the election_uuid: " election_uuid λ _ → verify-answers answers where answers = ballot ·« "answers" »A election_hash = ballot ·« "election_hash" » election_uuid = ballot ·« "election_uuid" » verify-ballots : (ballots : JSArray JSValue) → Bool verify-ballots ballots = and $ decodeJSArray ballots λ ix ballot → trace "verify ballot " ix λ _ → verify-ballot ballot -- Many checks are still missing! verify-helios-election : (arg : JSValue) → Bool verify-helios-election arg = trace "res=" res id where ed = arg ·« "election_data" » bs = arg ·« "ballots" »A pk = ed ·« "public_key" » g = bigdec (pk ·« "g" ») p = bigdec (pk ·« "p" ») q = bigdec (pk ·« "q" ») y = bigdec (pk ·« "y" ») res = trace "g=" g λ _ → trace "p=" p λ _ → trace "q=" q λ _ → trace "y=" y λ _ → HeliosVerifyV3.verify-ballots g p q y bs srv : URI → JSProc srv d = recv d λ q → send d (fromBool (verify-helios-election 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 λ { [] → server "127.0.0.1" "1337" srv !₁ λ uri → Console.log (showURI uri) ; (arg ∷ args'') → case args'' of λ { [] → let opts = -- fromJSObject (fromObject (("encoding" , fromString "utf8") ∷ [])) -- nullJS JSON-parse "{\"encoding\":\"utf8\"}" in Console.log ("readFile=" ++ arg) >> FS.readFile arg opts !₂ λ err dat → Console.log ("readFile: err=" ++ JS.toString err) >> Console.log (Bool▹String (verify-helios-election (JSON-parse (castString dat)))) ; _ → Console.log "usage" } } ; _ → Console.log "usage" } -- -} -- -} -- -} -- -} -- -}
{-# OPTIONS --without-K #-} module ZK.PartialHeliosVerifier where open import Type open import Function hiding (case_of_) open import Data.Bool.Base open import Data.Product open import Data.List.Base using (List; []; _∷_; and; foldr) open import Data.String.Base using (String) open import FFI.JS as JS hiding (_+_; _/_; _*_; join) open import FFI.JS.BigI as BigI import FFI.JS.Console as Console import FFI.JS.Process as Process import FFI.JS.FS as FS open import FFI.JS.SHA1 open import FFI.JS.Proc open import Control.Process.Type join : String → List String → String join sep [] = "" join sep (x ∷ xs) = x ++ foldr (λ y z → sep ++ y ++ z) "" xs {- instance showBigI = mk λ _ → showString ∘ BigI.toString -} G = BigI ℤq = BigI BigI▹G : BigI → G BigI▹G = id BigI▹ℤq : BigI → ℤq BigI▹ℤq = id non0I : BigI → BigI non0I x with equals x 0I ... | true = throw "Should be non zero!" 0I ... | false = x module BigICG (p q : BigI) where _^_ : G → ℤq → G _·_ : G → G → G _/_ : G → G → G _+_ : ℤq → ℤq → ℤq _*_ : ℤq → ℤq → ℤq _==_ : (x y : G) → Bool _^_ = λ x y → modPow x y p _·_ = λ x y → mod (multiply x y) p _/_ = λ x y → mod (multiply x (modInv (non0I y) p)) p _+_ = λ x y → mod (add x y) q _*_ = λ x y → mod (multiply x y) q _==_ = equals sumI : List BigI → BigI sumI = foldr _+_ 0I bignum : Number → BigI bignum n = bigI (Number▹String n) "10" -- TODO check with undefined bigdec : JSValue → BigI bigdec v = bigI (castString v) "10" {- print : {A : Set}{{_ : Show A}} → A → Callback0 print = Console.log ∘ show -} PubKey = G EncRnd = ℤq {- randomness used for encryption of ct -} Message = G {- plain text message -} Challenge = ℤq Response = ℤq CommitmentPart = G CipherTextPart = G module HeliosVerifyV3 (g : G) p q (y : PubKey) where open BigICG p q verify-chaum-pedersen : (α β : CipherTextPart)(M : Message)(A B : G)(c : Challenge)(s : Response) → Bool verify-chaum-pedersen α β M A B c s = trace "α=" α λ _ → trace "β=" β λ _ → trace "M=" M λ _ → trace "A=" A λ _ → trace "B=" B λ _ → trace "c=" c λ _ → trace "s=" s λ _ → (g ^ s) == (A · (α ^ c)) ∧ (y ^ s) == (B · ((β / M) ^ c)) verify-individual-proof : (α β : CipherTextPart)(ix : Number)(π : JSValue) → Bool verify-individual-proof α β ix π = trace "m=" m λ _ → trace "M=" M λ _ → trace "commitmentA=" A λ _ → trace "commitmentB=" B λ _ → trace "challenge=" c λ _ → trace "response=" s λ _ → res where m = bignum ix M = g ^ m A = bigdec (π ·« "commitment" » ·« "A" ») B = bigdec (π ·« "commitment" » ·« "B" ») c = bigdec (π ·« "challenge" ») s = bigdec (π ·« "response" ») res = verify-chaum-pedersen α β M A B c s -- Conform to HeliosV3 but it is too weak. -- One should follow a "Strong Fiat Shamir" transformation -- from interactive ZK proof to non-interactive ZK proofs. -- Namely one should hash the statement as well. -- -- SHA1(A0 + "," + B0 + "," + A1 + "," + B1 + ... + "Amax" + "," + Bmax) hash-commitments : JSArray JSValue → BigI hash-commitments πs = fromHex $ trace-call "SHA1(commitments)=" SHA1 $ trace-call "commitments=" (join ",") $ decodeJSArray πs λ _ π → (castString (π ·« "commitment" » ·« "A" »)) ++ "," ++ (castString (π ·« "commitment" » ·« "B" »)) sum-challenges : JSArray JSValue → BigI sum-challenges πs = sumI $ decodeJSArray πs λ _ π → bigdec (π ·« "challenge" ») verify-challenges : JSArray JSValue → Bool verify-challenges πs = trace "hash(commitments)=" h λ _ → trace "sum(challenge)=" c λ _ → h == c where h = hash-commitments πs c = sum-challenges πs verify-choice : (v : JSValue)(πs : JSArray JSValue) → Bool verify-choice v πs = trace "α=" α λ _ → trace "β=" β λ _ → verify-challenges πs ∧ and (decodeJSArray πs $ verify-individual-proof α β) where α = bigdec (v ·« "alpha" ») β = bigdec (v ·« "beta" ») verify-choices : (choices πs : JSArray JSValue) → Bool verify-choices choices πs = trace "TODO: check the array size of choices πs together election data" "" λ _ → and $ decodeJSArray choices λ ix choice → trace "verify choice " ix λ _ → verify-choice choice (castJSArray (πs Array[ ix ])) verify-answer : (answer : JSValue) → Bool verify-answer answer = trace "TODO: CHECK the overall_proof" "" λ _ → verify-choices choices individual-proofs where choices = answer ·« "choices" »A individual-proofs = answer ·« "individual_proofs" »A overall-proof = answer ·« "overall_proof" »A verify-answers : (answers : JSArray JSValue) → Bool verify-answers answers = and $ decodeJSArray answers λ ix a → trace "verify answer " ix λ _ → verify-answer a {- verify-election-hash = "" -- notice the whitespaces and the alphabetical order of keys -- {"email": ["[email protected]", "[email protected]"], "first_name": "Ben", "last_name": "Adida"} computed_hash = base64.b64encode(hash.new(election.toJSON()).digest())[:-1] computed_hash == vote.election_hash: "" -} -- module _ {-(election : JSValue)-} where verify-ballot : (ballot : JSValue) → Bool verify-ballot ballot = trace "TODO: CHECK the election_hash: " election_hash λ _ → trace "TODO: CHECK the election_uuid: " election_uuid λ _ → verify-answers answers where answers = ballot ·« "answers" »A election_hash = ballot ·« "election_hash" » election_uuid = ballot ·« "election_uuid" » verify-ballots : (ballots : JSArray JSValue) → Bool verify-ballots ballots = and $ decodeJSArray ballots λ ix ballot → trace "verify ballot " ix λ _ → verify-ballot ballot -- Many checks are still missing! verify-helios-election : (arg : JSValue) → Bool verify-helios-election arg = trace "res=" res id where ed = arg ·« "election_data" » bs = arg ·« "ballots" »A pk = ed ·« "public_key" » g = bigdec (pk ·« "g" ») p = bigdec (pk ·« "p" ») q = bigdec (pk ·« "q" ») y = bigdec (pk ·« "y" ») res = trace "g=" g λ _ → trace "p=" p λ _ → trace "q=" q λ _ → trace "y=" y λ _ → HeliosVerifyV3.verify-ballots g p q y bs srv : URI → JSProc srv d = recv d λ q → send d (fromBool (verify-helios-election 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 λ { [] → server "127.0.0.1" "1337" srv >>= λ uri → Console.log (showURI uri) ; (arg ∷ args'') → case args'' of λ { [] → let opts = -- fromJSObject (fromObject (("encoding" , fromString "utf8") ∷ [])) -- nullJS JSON-parse "{\"encoding\":\"utf8\"}" in Console.log ("readFile=" ++ arg) >> FS.readFile arg opts >>== λ err dat → Console.log ("readFile: err=" ++ JS.toString err) >> Console.log (Bool▹String (verify-helios-election (JSON-parse (castString dat)))) ; _ → Console.log "usage" } } ; _ → Console.log "usage" } -- -} -- -} -- -} -- -} -- -}
Update binds
Update binds
Agda
bsd-3-clause
crypto-agda/crypto-agda
24e0a0189105b8f3f2766a604a1ad5423b51b77e
Parametric/Change/Derive.agda
Parametric/Change/Derive.agda
import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Derive {Base : Type.Structure} (Const : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where open Type.Structure Base open Term.Structure Base Const open ChangeType.Structure Base ΔBase Structure : Set Structure = ∀ {Γ Σ τ} → Const Σ τ → Terms (ΔContext Γ) Σ → Terms (ΔContext Γ) (mapContext ΔType Σ) → Term (ΔContext Γ) (ΔType τ) module Structure (deriveConst : Structure) where fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ fit = weaken Γ≼ΔΓ fit-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) Σ fit-terms = weaken-terms Γ≼ΔΓ deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (mapContext ΔType Σ) derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ) derive-terms {∅} ∅ = ∅ derive-terms {τ • Σ} (t • ts) = derive t • derive-terms ts derive (var x) = var (deriveVar x) derive (app s t) = app (app (derive s) (fit t)) (derive t) derive (abs t) = abs (abs (derive t)) derive (const c ts) = deriveConst c (fit-terms ts) (derive-terms ts)
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Incrementalization as term-to-term transformation (Fig. 4g). ------------------------------------------------------------------------ import Parametric.Syntax.Type as Type import Parametric.Syntax.Term as Term import Parametric.Change.Type as ChangeType module Parametric.Change.Derive {Base : Type.Structure} (Const : Term.Structure Base) (ΔBase : ChangeType.Structure Base) where open Type.Structure Base open Term.Structure Base Const open ChangeType.Structure Base ΔBase -- Extension point: Incrementalization of fully applied primitives. Structure : Set Structure = ∀ {Γ Σ τ} → Const Σ τ → Terms (ΔContext Γ) Σ → Terms (ΔContext Γ) (mapContext ΔType Σ) → Term (ΔContext Γ) (ΔType τ) module Structure (deriveConst : Structure) where fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ fit = weaken Γ≼ΔΓ fit-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) Σ fit-terms = weaken-terms Γ≼ΔΓ -- In the paper, we transform "x" to "dx". Here, we work with -- de Bruijn indices, so we have to manipulate the indices to -- account for a bigger context after transformation. deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ) deriveVar this = this deriveVar (that x) = that (that (deriveVar x)) derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (mapContext ΔType Σ) derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ) derive-terms {∅} ∅ = ∅ derive-terms {τ • Σ} (t • ts) = derive t • derive-terms ts -- We provide: Incrementalization of arbitrary terms. derive (var x) = var (deriveVar x) derive (app s t) = app (app (derive s) (fit t)) (derive t) derive (abs t) = abs (abs (derive t)) derive (const c ts) = deriveConst c (fit-terms ts) (derive-terms ts)
Document P.C.Derive.
Document P.C.Derive. Old-commit-hash: 962425cf9bb190e1b8fb2c400bd9765180081dd0
Agda
mit
inc-lc/ilc-agda
8d4599155dc64db203782bdd39087df1a8014331
Popl14/Change/Term.agda
Popl14/Change/Term.agda
module Popl14.Change.Term where -- Terms Calculus Popl14 -- -- Contents -- - Term constructors -- - Weakening on terms -- - `fit`: weaken a term to its ΔContext -- - diff-term, apply-term and their syntactic sugars open import Data.Integer open import Popl14.Syntax.Type public open import Popl14.Syntax.Term public open import Popl14.Change.Type public import Parametric.Change.Term Const ΔBase as ChangeTerm diff-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-base : ChangeTerm.ApplyStructure apply-base {base-int} = abs₂ (λ Δx x → add x Δx) apply-base {base-bag} = abs₂ (λ Δx x → union x Δx) apply-term {base ι} = apply-base {ι} apply-term {σ ⇒ τ} = let Δf = var (that (that this)) f = var (that this) x = var this in -- Δf f x abs (abs (abs (app f x ⊕ app (app Δf x) (x ⊝ x)))) diff-base : ChangeTerm.DiffStructure diff-base {base-int} = abs₂ (λ x y → add x (minus y)) diff-base {base-bag} = abs₂ (λ x y → union x (negate y)) diff-term {base ι} = diff-base {ι} diff-term {σ ⇒ τ} = let g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this in -- g f x Δx abs (abs (abs (abs (app g (x ⊕ Δx) ⊝ app f x))))
module Popl14.Change.Term where -- Terms Calculus Popl14 -- -- Contents -- - Term constructors -- - Weakening on terms -- - `fit`: weaken a term to its ΔContext -- - diff-term, apply-term and their syntactic sugars open import Data.Integer open import Popl14.Syntax.Type public open import Popl14.Syntax.Term public open import Popl14.Change.Type public import Parametric.Change.Term Const ΔBase as ChangeTerm diff-base : ChangeTerm.DiffStructure diff-base {base-int} = abs₂ (λ x y → add x (minus y)) diff-base {base-bag} = abs₂ (λ x y → union x (negate y)) apply-base : ChangeTerm.ApplyStructure apply-base {base-int} = abs₂ (λ Δx x → add x Δx) apply-base {base-bag} = abs₂ (λ Δx x → union x Δx) diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ) apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ) -- Sugars for diff-term and apply-term infixl 6 _⊕_ _⊝_ _⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ _⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ) t ⊕ Δt = app (app apply-term Δt) t s ⊝ t = app (app diff-term s) t apply-term {base ι} = apply-base {ι} apply-term {σ ⇒ τ} = let Δf = var (that (that this)) f = var (that this) x = var this in -- Δf f x abs (abs (abs (app f x ⊕ app (app Δf x) (x ⊝ x)))) diff-term {base ι} = diff-base {ι} diff-term {σ ⇒ τ} = let g = var (that (that (that this))) f = var (that (that this)) x = var (that this) Δx = var this in -- g f x Δx abs (abs (abs (abs (app g (x ⊕ Δx) ⊝ app f x))))
Move (apply|diff)-base to the top of the module.
Move (apply|diff)-base to the top of the module. This commit separates the POPL14-specific code from the language-independent code in this module. The following commits will remove the language-independent code. Old-commit-hash: 245a7b618b2ac811d28d702089482e9aed8b8425
Agda
mit
inc-lc/ilc-agda
c68a9b8870e9eaca8c1a746f2ec67e2c84d97fd5
Crypto/JS/BigI/CyclicGroup.agda
Crypto/JS/BigI/CyclicGroup.agda
{-# OPTIONS --without-K #-} open import Type.Eq open import FFI.JS using (JS[_]; return; Bool; _++_; _>>_) open import FFI.JS.Check using (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 → JS[ ℤp★ ] BigI▹ℤ[_]★ x = -- Console.log "BigI▹ℤ[_]★" >> check! "below modulus" (x <I p) (λ _ → "Not below the modulus: p:" ++ toString p ++ " is less than x:" ++ toString x) >> check! "strictcly positive" (x >I 0I) (λ _ → "Should be strictly positive: " ++ toString x ++ " <= 0") >> return x repr : ℤp★ → BigI repr x = x 1# : ℤp★ 1# = 1I 1/_ : ℤp★ → ℤp★ 1/ x = modInv x p _^_ : ℤp★ → BigI → ℤp★ x ^ y = modPow x y p _*_ _/_ : ℤp★ → ℤp★ → ℤp★ x * y = mod-p (multiply (repr x) (repr y)) x / y = x * 1/ y instance ℤ[_]★-Eq? : Eq? ℤp★ ℤ[_]★-Eq? = record { _==_ = _=='_ ; ≡⇒== = ≡⇒==' ; ==⇒≡ = ==⇒≡' } where _=='_ : ℤp★ → ℤp★ → 𝟚 x ==' y = equals (repr x) (repr y) postulate ≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y) ==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y prod : List ℤp★ → ℤp★ prod = foldr _*_ 1# mon-ops : Monoid-Ops ℤp★ mon-ops = _*_ , 1# grp-ops : Group-Ops ℤp★ grp-ops = mon-ops , 1/_ postulate grp-struct : Group-Struct grp-ops grp : Group ℤp★ grp = grp-ops , grp-struct module grp = Group grp -- -} -- -} -- -} -- -} -- -}
{-# OPTIONS --without-K #-} open import Type.Eq open import FFI.JS using (JS[_]; return; _++_; _>>_) open import FFI.JS.Check using (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 → JS[ ℤp★ ] BigI▹ℤ[_]★ x = -- Console.log "BigI▹ℤ[_]★" >> check! "below modulus" (x <I p) (λ _ → "Not below the modulus: p:" ++ toString p ++ " is less than x:" ++ toString x) >> check! "strictcly positive" (x >I 0I) (λ _ → "Should be strictly positive: " ++ toString x ++ " <= 0") >> return x repr : ℤp★ → BigI repr x = x 1# : ℤp★ 1# = 1I 1/_ : ℤp★ → ℤp★ 1/ x = modInv x p _^_ : ℤp★ → BigI → ℤp★ x ^ y = modPow x y p _*_ _/_ : ℤp★ → ℤp★ → ℤp★ x * y = mod-p (multiply (repr x) (repr y)) x / y = x * 1/ y instance ℤ[_]★-Eq? : Eq? ℤp★ ℤ[_]★-Eq? = record { _==_ = _=='_ ; ≡⇒== = ≡⇒==' ; ==⇒≡ = ==⇒≡' } where _=='_ : ℤp★ → ℤp★ → 𝟚 x ==' y = equals (repr x) (repr y) postulate ≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y) ==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y prod : List ℤp★ → ℤp★ prod = foldr _*_ 1# mon-ops : Monoid-Ops ℤp★ mon-ops = _*_ , 1# grp-ops : Group-Ops ℤp★ grp-ops = mon-ops , 1/_ postulate grp-struct : Group-Struct grp-ops grp : Group ℤp★ grp = grp-ops , grp-struct module grp = Group grp -- -} -- -} -- -} -- -} -- -}
remove useless import
CyclicGroup: remove useless import
Agda
bsd-3-clause
crypto-agda/crypto-agda
3a1143b9db99b9ec7e1431f08aab91f27cf9bd7a
Postulate/Extensionality.agda
Postulate/Extensionality.agda
module Postulate.Extensionality where -- POSTULATE EXTENSIONALITY -- -- Justification on Agda mailing list: -- http://permalink.gmane.org/gmane.comp.lang.agda/2343 open import Relation.Binary.PropositionalEquality postulate ext : ∀ {a b} → Extensionality a b -- Convenience of using extensionality 3 times in a row -- (using it twice in a row is moderately tolerable) ext³ : ∀ {A : Set} {B : A → Set} {C : (a : A) → B a → Set } {D : (a : A) → (b : B a) → C a b → Set} {f g : (a : A) → (b : B a) → (c : C a b) → D a b c} → ((a : A) (b : B a) (c : C a b) → f a b c ≡ g a b c) → f ≡ g ext³ fabc=gabc = ext (λ a → ext (λ b → ext (λ c → fabc=gabc a b c)))
------------------------------------------------------------------------ -- INCREMENTAL λ-CALCULUS -- -- Postulate extensionality of functions. -- -- Justification on Agda mailing list: -- http://permalink.gmane.org/gmane.comp.lang.agda/2343 ------------------------------------------------------------------------ module Postulate.Extensionality where open import Relation.Binary.PropositionalEquality postulate ext : ∀ {a b} → Extensionality a b -- Convenience of using extensionality 3 times in a row -- (using it twice in a row is moderately tolerable) ext³ : ∀ {A : Set} {B : A → Set} {C : (a : A) → B a → Set } {D : (a : A) → (b : B a) → C a b → Set} {f g : (a : A) → (b : B a) → (c : C a b) → D a b c} → ((a : A) (b : B a) (c : C a b) → f a b c ≡ g a b c) → f ≡ g ext³ fabc=gabc = ext (λ a → ext (λ b → ext (λ c → fabc=gabc a b c)))
Document Postulate.Extensionality regularly.
Document Postulate.Extensionality regularly. Old-commit-hash: 35222e92088dbd2313ec7252e50cd68e85b340bf
Agda
mit
inc-lc/ilc-agda
e7d0ffc6976dc3270e58e80326cf78dc7e9dcb2e
flat-funs.agda
flat-funs.agda
module flat-funs where open import Data.Nat import Level as L open import composable open import vcomp record FlatFuns {t} (T : Set t) : Set (L.suc t) where constructor mk field `⊤ : T `Bit : T _`×_ : T → T → T _`^_ : T → ℕ → T _`→_ : T → T → Set `Vec : T → ℕ → T `Vec A n = A `^ n `Bits : ℕ → T `Bits n = `Bit `^ n infixr 2 _`×_ infixl 2 _`^_ infix 0 _`→_ record FlatFunsOps {t} {T : Set t} (♭Funs : FlatFuns T) : Set t where constructor mk open FlatFuns ♭Funs field idO : ∀ {A} → A `→ A isComposable : Composable _`→_ isVComposable : VComposable _`×_ _`→_ -- Fanout _&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C fst : ∀ {A B} → A `× B `→ A snd : ∀ {A B} → A `× B `→ B open Composable isComposable open VComposable isVComposable <_,_> : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C < f , g > = f &&& g <_×_> : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D) < f × g > = f *** g open Composable isComposable public open VComposable isVComposable public open import Data.Unit using (⊤) open import Data.Vec open import Data.Bits open import Data.Product open import Function fun♭Funs : FlatFuns Set fun♭Funs = mk ⊤ Bit _×_ Vec (λ A B → A → B) bitsFun♭Funs : FlatFuns ℕ bitsFun♭Funs = mk 0 1 _+_ _*_ (λ i o → Bits i → Bits o) fun♭Ops : FlatFunsOps fun♭Funs fun♭Ops = mk id funComp funVComp _&&&_ proj₁ proj₂ where _&&&_ : ∀ {A B C : Set} → (A → B) → (A → C) → A → B × C (f &&& g) x = (f x , g x) bitsFun♭Ops : FlatFunsOps bitsFun♭Funs bitsFun♭Ops = mk id bitsFunComp bitsFunVComp _&&&_ (λ {A} → take A) (λ {A} → drop A) where open FlatFuns bitsFun♭Funs _&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C (f &&& g) x = (f x ++ g x) ×-♭Funs : ∀ {s t} {S : Set s} {T : Set t} → FlatFuns S → FlatFuns T → FlatFuns (S × T) ×-♭Funs funs-S funs-T = ? where module S = FlatFuns funs-S module T = FlatFuns funs-T ×-♭Ops : ∀ {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T} → FlatFunsOps funs-S → FlatFunsOps funs-T → FlatFunsOps (×-♭Funs funs-S funs-T) ×-♭Ops ops-S ops-T = ? where module S = FlatFunsOps ops-S module T = FlatFunsOps ops-T
module flat-funs where open import Data.Nat open import Data.Bits using (Bits) import Level as L import Function as F open import composable open import vcomp open import universe record FlatFuns {t} (T : Set t) : Set (L.suc t) where constructor mk field universe : Universe T _`→_ : T → T → Set infix 0 _`→_ open Universe universe public record FlatFunsOps {t} {T : Set t} (♭Funs : FlatFuns T) : Set t where constructor mk open FlatFuns ♭Funs field idO : ∀ {A} → A `→ A _>>>_ : ∀ {A B C} → (A `→ B) → (B `→ C) → (A `→ C) _***_ : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D) -- Fanout _&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C fst : ∀ {A B} → A `× B `→ A snd : ∀ {A B} → A `× B `→ B constBits : ∀ {n} → Bits n → `⊤ `→ `Bits n <_,_> : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C < f , g > = f &&& g <_×_> : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D) < f × g > = f *** g open import Data.Unit using (⊤) open import Data.Vec open import Data.Bits open import Data.Fin using (Fin) renaming (_+_ to _+ᶠ_) open import Data.Product renaming (zip to ×-zip; map to ×-map) open import Function -→- : Set → Set → Set -→- A B = A → B _→ᶠ_ : ℕ → ℕ → Set _→ᶠ_ i o = Fin i → Fin o mapArr : ∀ {s t} {S : Set s} {T : Set t} (F G : T → S) → (S → S → Set) → (T → T → Set) mapArr F G _`→_ A B = F A `→ G B fun♭Funs : FlatFuns Set fun♭Funs = mk Set-U -→- bitsFun♭Funs : FlatFuns ℕ bitsFun♭Funs = mk Bits-U _→ᵇ_ finFun♭Funs : FlatFuns ℕ finFun♭Funs = mk Fin-U _→ᶠ_ fun♭Ops : FlatFunsOps fun♭Funs fun♭Ops = mk id (λ f g x → g (f x)) (λ f g → ×-map f g) _&&&_ proj₁ proj₂ const where _&&&_ : ∀ {A B C : Set} → (A → B) → (A → C) → A → B × C (f &&& g) x = (f x , g x) bitsFun♭Ops : FlatFunsOps bitsFun♭Funs bitsFun♭Ops = mk id (λ f g → g ∘ f) (VComposable._***_ bitsFunVComp) _&&&_ (λ {A} → take A) (λ {A} → drop A) (λ xs _ → xs ++ [] {- ugly? -}) where open FlatFuns bitsFun♭Funs _&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C (f &&& g) x = (f x ++ g x) ×-♭Funs : ∀ {s t} {S : Set s} {T : Set t} → FlatFuns S → FlatFuns T → FlatFuns (S × T) ×-♭Funs funs-S funs-T = mk (×-U S.universe T.universe) (λ { (A₀ , A₁) (B₀ , B₁) → (A₀ S.`→ B₀) × (A₁ T.`→ B₁) }) where module S = FlatFuns funs-S module T = FlatFuns funs-T ×⊤-♭Funs : ∀ {s} {S : Set s} → FlatFuns S → FlatFuns ⊤ → FlatFuns S ×⊤-♭Funs funs-S funs-T = mk S.universe (λ A B → (A S.`→ B) × (_ T.`→ _)) where module S = FlatFuns funs-S module T = FlatFuns funs-T ×-♭Ops : ∀ {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T} → FlatFunsOps funs-S → FlatFunsOps funs-T → FlatFunsOps (×-♭Funs funs-S funs-T) ×-♭Ops ops-S ops-T = mk (S.idO , T.idO) (×-zip S._>>>_ T._>>>_) (×-zip S._***_ T._***_) (×-zip S._&&&_ T._&&&_) (S.fst , T.fst) (S.snd , T.snd) (S.constBits &&& T.constBits) where module S = FlatFunsOps ops-S module T = FlatFunsOps ops-T open FlatFunsOps fun♭Ops ×⊤-♭Ops : ∀ {s} {S : Set s} {funs-S : FlatFuns S} {funs-⊤ : FlatFuns ⊤} → FlatFunsOps funs-S → FlatFunsOps funs-⊤ → FlatFunsOps (×⊤-♭Funs funs-S funs-⊤) ×⊤-♭Ops ops-S ops-⊤ = mk (S.idO , T.idO) (×-zip S._>>>_ T._>>>_) (×-zip S._***_ T._***_) (×-zip S._&&&_ T._&&&_) (S.fst , T.fst) (S.snd , T.snd) (S.constBits &&& T.constBits) where module S = FlatFunsOps ops-S module T = FlatFunsOps ops-⊤ open FlatFunsOps fun♭Ops constFuns : Set → FlatFuns ⊤ constFuns A = mk ⊤-U (λ _ _ → A) timeOps : FlatFunsOps (constFuns ℕ) timeOps = mk 0 _+_ _⊔_ _⊔_ 0 0 (const 0) spaceOps : FlatFunsOps (constFuns ℕ) spaceOps = mk 0 _+_ _+_ _+_ 0 0 (λ {n} _ → n) time×spaceOps : FlatFunsOps (constFuns (ℕ × ℕ)) time×spaceOps = ×⊤-♭Ops timeOps spaceOps
put the type interface in the universe module
flat-funs: put the type interface in the universe module
Agda
bsd-3-clause
crypto-agda/crypto-agda