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
|
---|---|---|---|---|---|---|---|---|---|
1b5c3931a31ac1d019f7a68f56a036201f92a46e
|
lambda.agda
|
lambda.agda
|
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
if : ∀ {Γ τ} → (t₁ : Term Γ bool) (t₂ t₃ : 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
⟦ if 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
if-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} →
ρ ⊢ t₁ ↓ true →
ρ ⊢ t₂ ↓ v₂ →
ρ ⊢ if t₁ t₂ t₃ ↓ v₂
if-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} →
ρ ⊢ t₁ ↓ false →
ρ ⊢ t₃ ↓ v₃ →
ρ ⊢ if 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 (if-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂
↓-sound (if-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃
-- WEAKENING
-- Weaken a term to a super context
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 ≼₁ true = true
weaken ≼₁ false = false
weaken ≼₁ (if e₁ e₂ e₃) = if (weaken ≼₁ e₁) (weaken ≼₁ e₂) (weaken ≼₁ e₃)
-- SYMBOLIC EXECUTION
--
-- Naming Convention:
-- Γ ⟪_⟫X is like ⟦_⟧X but for symbolic execution in context Γ.
_⟪_⟫Type : Context → Type → Set
Γ₁ ⟪ τ₁ ⇒ τ₂ ⟫Type = ∀ {Γ₂} → Γ₁ ≼ Γ₂ → Γ₂ ⟪ τ₁ ⟫Type → Γ₂ ⟪ τ₂ ⟫Type
Γ₁ ⟪ bool ⟫Type = Term Γ₁ bool
module _ (Γ : Context) where
import Denotational.Environments
module SymEnv = Denotational.Environments Type (λ τ → Γ ⟪ τ ⟫Type)
open SymEnv public using ()
renaming (⟦_⟧Context to _⟪_⟫Context; ⟦_⟧Var to _⟪_⟫Var_)
liftVal : ∀ {τ Γ₁ Γ₂} → Γ₁ ≼ Γ₂ → Γ₁ ⟪ τ ⟫Type → Γ₂ ⟪ τ ⟫Type
liftVal {τ₁ ⇒ τ₂} ≼₁ v₁ = λ ≼₂ v₂ → v₁ (≼-trans ≼₁ ≼₂) v₂
liftVal {bool} ≼₁ v = weaken ≼₁ v
liftEnv : ∀ {Γ Γ₁ Γ₂} → Γ₁ ≼ Γ₂ → Γ₁ ⟪ Γ ⟫Context → Γ₂ ⟪ Γ ⟫Context
liftEnv {∅} ≼₁ ∅ = SymEnv.∅
liftEnv {τ • Γ} ≼₁ (v • ρ) = liftVal ≼₁ v SymEnv.• liftEnv ≼₁ ρ
mixed-if : ∀ {Γ₁} τ → (t₁ : Term Γ₁ bool) (v₂ v₃ : Γ₁ ⟪ τ ⟫Type) → Γ₁ ⟪ τ ⟫Type
mixed-if (τ₁ ⇒ τ₂) t₁ v₂ v₃ = λ ≼₁ v → mixed-if τ₂ (weaken ≼₁ t₁) (v₃ ≼₁ v) (v₃ ≼₁ v)
mixed-if bool t₁ t₂ t₃ = if t₁ t₂ t₃
_⟪_⟫Term_ : ∀ Γ₁ {Γ τ} → Term Γ τ → Γ₁ ⟪ Γ ⟫Context → Γ₁ ⟪ τ ⟫Type
Γ₁ ⟪ abs t ⟫Term ρ = λ {Γ₂} ≼₁ v → Γ₂ ⟪ t ⟫Term (v SymEnv.• liftEnv ≼₁ ρ)
Γ₁ ⟪ app t₁ t₂ ⟫Term ρ = (Γ₁ ⟪ t₁ ⟫Term ρ) ≼-refl (Γ₁ ⟪ t₂ ⟫Term ρ)
Γ₁ ⟪ var x ⟫Term ρ = Γ₁ ⟪ x ⟫Var ρ
Γ₁ ⟪ true ⟫Term ρ = true
Γ₁ ⟪ false ⟫Term ρ = false
Γ₁ ⟪ if t₁ t₂ t₃ ⟫Term ρ = mixed-if _ (Γ₁ ⟪ t₁ ⟫Term ρ) (Γ₁ ⟪ t₂ ⟫Term ρ) (Γ₁ ⟪ t₂ ⟫Term ρ)
↓ : ∀ {Γ} τ → Γ ⟪ τ ⟫Type → Term Γ τ
↑ : ∀ {Γ} τ → Term Γ τ → Γ ⟪ τ ⟫Type
↓ (τ₁ ⇒ τ₂) v = abs (↓ τ₂ (v (drop τ₁ • ≼-refl) (↑ τ₁ (var this))))
↓ bool v = v
↑ (τ₁ ⇒ τ₂) t = λ ≼₁ v → ↑ τ₂ (app (weaken ≼₁ t) (↓ τ₁ v))
↑ bool t = t
↑-Context : ∀ {Γ} → Γ ⟪ Γ ⟫Context
↑-Context {∅} = SymEnv.∅
↑-Context {τ • Γ} = ↑ τ (var this) SymEnv.• liftEnv (drop τ • ≼-refl) ↑-Context
norm : ∀ {Γ τ} → Term Γ τ → Term Γ τ
norm {Γ} {τ} t = ↓ τ (Γ ⟪ t ⟫Term ↑-Context)
|
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
if : ∀ {Γ τ} → (t₁ : Term Γ bool) (t₂ t₃ : 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
⟦ if 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
if-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} →
ρ ⊢ t₁ ↓ true →
ρ ⊢ t₂ ↓ v₂ →
ρ ⊢ if t₁ t₂ t₃ ↓ v₂
if-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} →
ρ ⊢ t₁ ↓ false →
ρ ⊢ t₃ ↓ v₃ →
ρ ⊢ if 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 (if-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂
↓-sound (if-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃
-- WEAKENING
-- Weaken a term to a super context
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 ≼₁ true = true
weaken ≼₁ false = false
weaken ≼₁ (if e₁ e₂ e₃) = if (weaken ≼₁ e₁) (weaken ≼₁ e₂) (weaken ≼₁ e₃)
-- SYMBOLIC EXECUTION
--
-- Naming Convention:
-- Γ ⟪_⟫X is like ⟦_⟧X but for symbolic execution in context Γ.
_⟪_⟫Type : Context → Type → Set
Γ₁ ⟪ τ₁ ⇒ τ₂ ⟫Type = ∀ {Γ₂} → Γ₁ ≼ Γ₂ → Γ₂ ⟪ τ₁ ⟫Type → Γ₂ ⟪ τ₂ ⟫Type
Γ₁ ⟪ bool ⟫Type = Term Γ₁ bool
module _ (Γ : Context) where
import Denotational.Environments
module SymEnv = Denotational.Environments Type (λ τ → Γ ⟪ τ ⟫Type)
open SymEnv public using ()
renaming (⟦_⟧Context to _⟪_⟫Context; ⟦_⟧Var to _⟪_⟫Var_)
liftVal : ∀ {τ Γ₁ Γ₂} → Γ₁ ≼ Γ₂ → Γ₁ ⟪ τ ⟫Type → Γ₂ ⟪ τ ⟫Type
liftVal {τ₁ ⇒ τ₂} ≼₁ v₁ = λ ≼₂ v₂ → v₁ (≼-trans ≼₁ ≼₂) v₂
liftVal {bool} ≼₁ v = weaken ≼₁ v
liftEnv : ∀ {Γ Γ₁ Γ₂} → Γ₁ ≼ Γ₂ → Γ₁ ⟪ Γ ⟫Context → Γ₂ ⟪ Γ ⟫Context
liftEnv {∅} ≼₁ ∅ = SymEnv.∅
liftEnv {τ • Γ} ≼₁ (v • ρ) = liftVal ≼₁ v SymEnv.• liftEnv ≼₁ ρ
mixed-if : ∀ {Γ₁} τ → (t₁ : Term Γ₁ bool) (v₂ v₃ : Γ₁ ⟪ τ ⟫Type) → Γ₁ ⟪ τ ⟫Type
mixed-if (τ₁ ⇒ τ₂) t₁ v₂ v₃ = λ ≼₁ v → mixed-if τ₂ (weaken ≼₁ t₁) (v₂ ≼₁ v) (v₃ ≼₁ v)
mixed-if bool t₁ t₂ t₃ = if t₁ t₂ t₃
_⟪_⟫Term_ : ∀ Γ₁ {Γ τ} → Term Γ τ → Γ₁ ⟪ Γ ⟫Context → Γ₁ ⟪ τ ⟫Type
Γ₁ ⟪ abs t ⟫Term ρ = λ {Γ₂} ≼₁ v → Γ₂ ⟪ t ⟫Term (v SymEnv.• liftEnv ≼₁ ρ)
Γ₁ ⟪ app t₁ t₂ ⟫Term ρ = (Γ₁ ⟪ t₁ ⟫Term ρ) ≼-refl (Γ₁ ⟪ t₂ ⟫Term ρ)
Γ₁ ⟪ var x ⟫Term ρ = Γ₁ ⟪ x ⟫Var ρ
Γ₁ ⟪ true ⟫Term ρ = true
Γ₁ ⟪ false ⟫Term ρ = false
Γ₁ ⟪ if t₁ t₂ t₃ ⟫Term ρ = mixed-if _ (Γ₁ ⟪ t₁ ⟫Term ρ) (Γ₁ ⟪ t₂ ⟫Term ρ) (Γ₁ ⟪ t₂ ⟫Term ρ)
↓ : ∀ {Γ} τ → Γ ⟪ τ ⟫Type → Term Γ τ
↑ : ∀ {Γ} τ → Term Γ τ → Γ ⟪ τ ⟫Type
↓ (τ₁ ⇒ τ₂) v = abs (↓ τ₂ (v (drop τ₁ • ≼-refl) (↑ τ₁ (var this))))
↓ bool v = v
↑ (τ₁ ⇒ τ₂) t = λ ≼₁ v → ↑ τ₂ (app (weaken ≼₁ t) (↓ τ₁ v))
↑ bool t = t
↑-Context : ∀ {Γ} → Γ ⟪ Γ ⟫Context
↑-Context {∅} = SymEnv.∅
↑-Context {τ • Γ} = ↑ τ (var this) SymEnv.• liftEnv (drop τ • ≼-refl) ↑-Context
norm : ∀ {Γ τ} → Term Γ τ → Term Γ τ
norm {Γ} {τ} t = ↓ τ (Γ ⟪ t ⟫Term ↑-Context)
|
Fix mixed-if.
|
Fix mixed-if.
Old-commit-hash: 18ad0d9a08607bece81abf4bedc81a7763b56b9a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
998682f0d09f06ad7918732abe550b386afe02e2
|
Syntax/Type/Popl14.agda
|
Syntax/Type/Popl14.agda
|
module Syntax.Type.Popl14 where
-- Types of Calculus Popl14
infixr 5 _⇒_
data Type : Set where
int : Type
bag : Type
_⇒_ : (σ : Type) → (τ : Type) → Type
ΔType : Type → Type
ΔType int = int
ΔType bag = bag
ΔType (σ ⇒ τ) = σ ⇒ ΔType σ ⇒ ΔType τ
|
module Syntax.Type.Popl14 where
-- Types of Calculus Popl14
data Popl14-type : Set where
base-int : Popl14-type
base-bag : Popl14-type
open import Syntax.Type.Plotkin Popl14-type public
pattern int = base base-int
pattern bag = base base-bag
Popl14-Δbase : Popl14-type → Popl14-type
Popl14-Δbase base-int = base-int
Popl14-Δbase base-bag = base-bag
ΔType : Type → Type
ΔType = lift-Δtype₀ Popl14-Δbase
|
Make Syntax.Type.Popl14 use Syntax.Type.Plotkin
|
Make Syntax.Type.Popl14 use Syntax.Type.Plotkin
Old-commit-hash: 031c97aee2022a85ee855ec3149f0f8cd160d5af
|
Agda
|
mit
|
inc-lc/ilc-agda
|
47b55ae0449921322131bd6dbdab41837ccb68c8
|
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
`Bits : ℕ → T
`Bit : T
_`×_ : T → T → T
_`→_ : T → T → Set
infixr 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
open FlatFuns ♭Funs public
open Composable isComposable public
open VComposable isVComposable public
open import Data.Bits
open import Data.Product
open import Function
fun♭Funs : FlatFuns Set
fun♭Funs = mk Bits Bit _×_ (λ A B → A → B)
bitsfun♭Funs : FlatFuns ℕ
bitsfun♭Funs = mk id 1 _+_ (λ i o → Bits i → Bits o)
fun♭Ops : FlatFunsOps fun♭Funs
fun♭Ops = mk id funComp funVComp _&&&_
where
_&&&_ : ∀ {A B C : Set} → (A → B) → (A → C) → A → B × C
(f &&& g) x = (f x , g x)
|
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 FlatFuns ♭Funs public
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)
|
Add support for general vector to our flat-funs abstraction
|
Add support for general vector to our flat-funs abstraction
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
fce4641e9e8aec601f2ab48e128ad529e5ac3a98
|
Thesis/SIRelBigStep/DeriveCorrect.agda
|
Thesis/SIRelBigStep/DeriveCorrect.agda
|
--
-- In this module we conclude our proof: we have shown that t, derive-dterm t
-- and produce related values (in the sense of the fundamental property,
-- rfundamental3), and for related values v1, dv and v2 we have v1 ⊕ dv ≡
-- v2 (because ⊕ agrees with validity, rrelV3→⊕).
--
-- We now put these facts together via derive-correct-si and derive-correct.
-- This is immediate, even though I spend so much code on it:
-- Indeed, all theorems are immediate corollaries of what we established (as
-- explained above); only the statements are longer, especially because I bother
-- expanding them.
module Thesis.SIRelBigStep.DeriveCorrect where
open import Data.Product
open import Relation.Binary.PropositionalEquality
open import Thesis.SIRelBigStep.IlcSILR
open import Thesis.SIRelBigStep.FundamentalProperty
-- Theorem statement. This theorem still mentions step-indexes explicitly.
derive-correct-si-type =
∀ {τ Γ k} (t : Term Γ τ) ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
rrelT3-skeleton (λ v1 dv v2 _ → v1 ⊕ dv ≡ v2) t (derive-dterm t) t ρ1 dρ ρ2 k
-- A verified expansion of the theorem statement.
derive-correct-si-type-means :
derive-correct-si-type ≡
∀ {τ Γ k} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
(v1 v2 : Val τ) →
∀ j (j<k : j < k) →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct-si-type-means = refl
derive-correct-si : derive-correct-si-type
derive-correct-si t ρ1 dρ ρ2 ρρ = rrelT3→⊕ t (derive-dterm t) t ρ1 dρ ρ2 (rfundamental3 _ t ρ1 dρ ρ2 ρρ)
-- Infinitely related environments:
rrelρ3-inf : ∀ Γ (ρ1 : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) (ρ2 : ⟦ Γ ⟧Context) → Set
rrelρ3-inf Γ ρ1 dρ ρ2 = ∀ k → rrelρ3 Γ ρ1 dρ ρ2 k
-- A theorem statement for infinitely-related environments. On paper, if we informally
-- omit the arbitrary derivation step-index as usual, we get a statement without
-- step-indexes.
derive-correct :
∀ {τ Γ} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3-inf Γ ρ1 dρ ρ2) →
(v1 v2 : Val τ) →
∀ j →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct t ρ1 dρ ρ2 ρρ v1 v2 j = derive-correct-si t ρ1 dρ ρ2 (ρρ (suc j)) v1 v2 j ≤-refl
open import Data.Unit
nilρ : ∀ {Γ n} (ρ : ⟦ Γ ⟧Context) → Σ[ dρ ∈ ChΔ Γ ] rrelρ3 Γ ρ dρ ρ n
nilV : ∀ {τ n} (v : Val τ) → Σ[ dv ∈ DVal τ ] rrelV3 τ v dv v n
nilV (closure t ρ) = let dρ , ρρ = nilρ ρ in dclosure (derive-dterm t) ρ dρ , rfundamental3svv _ (abs t) ρ dρ ρ ρρ
nilV (natV n₁) = dnatV zero , refl
nilV (pairV a b) = let 0a , aa = nilV a; 0b , bb = nilV b in dpairV 0a 0b , aa , bb
nilρ ∅ = ∅ , tt
nilρ (v • ρ) = let dv , vv = nilV v ; dρ , ρρ = nilρ ρ in dv • dρ , vv , ρρ
-- Try to prove to define validity-preserving change composition.
-- Sadly, if we don't store proofs that environment changes in closures are valid, we can't finish the proof for the closure case :-(.
-- Moreover, the proof is annoying to do unless we build a datatype to invert
-- validity proofs (as suggested by Ezyang in
-- http://blog.ezyang.com/2013/09/induction-and-logical-relations/).
-- ocomposeρ : ∀ {Γ} n ρ1 dρ1 ρ2 dρ2 ρ3 → rrelρ3 Γ ρ1 dρ1 ρ2 n → rrelρ3 Γ ρ2 dρ2 ρ3 n → Σ[ dρ ∈ ChΔ Γ ] rrelρ3 Γ ρ1 dρ ρ3 n
-- ocomposev : ∀ {τ} n v1 dv1 v2 dv2 v3 → rrelV3 τ v1 dv1 v2 n → rrelV3 τ v2 dv2 v3 n → Σ[ dv ∈ DVal τ ] rrelV3 τ v1 dv v3 n
-- ocomposev n v1 dv1 v2 (bang v3) .v3 vv1 refl = bang v3 , refl
-- ocomposev n v1 (bang x) v2 (dclosure dt ρ dρ) v3 vv1 vv2 = {!!}
-- ocomposev n (closure t ρ) (dclosure .(derive-dterm t) .ρ dρ1) (closure .t .(ρ ⊕ρ dρ1)) (dclosure .(derive-dterm t) .(ρ ⊕ρ dρ1) dρ2) (closure .t .((ρ ⊕ρ dρ1) ⊕ρ dρ2)) ((refl , refl) , refl , refl , refl , refl , p5) ((refl , refl) , refl , refl , refl , refl , p5q) =
-- let dρ , ρρ = ocomposeρ n ρ dρ1 (ρ ⊕ρ dρ1) dρ2 ((ρ ⊕ρ dρ1) ⊕ρ dρ2) {!!} {!!}
-- in dclosure (derive-dterm t) ρ dρ , (refl , refl) , refl , {!!} , refl , refl ,
-- λ { k (s≤s k<n) v1 dv v2 vv →
-- rfundamental3 k t (v1 • ρ) (dv • dρ) (v2 • ((ρ ⊕ρ dρ1) ⊕ρ dρ2)) (vv , rrelρ3-mono k n (≤-step k<n) _ ρ dρ ((ρ ⊕ρ dρ1) ⊕ρ dρ2) ρρ)}
-- -- p1 , p2 , p3 , p4 , p5
-- ocomposev n v1 dv1 v2 (dnatV n₁) v3 vv1 vv2 = {!!}
-- ocomposev n v1 dv1 v2 (dpairV dv2 dv3) v3 vv1 vv2 = {!!}
-- -- ocomposev n v1 (bang v2) .v2 dv2 v3 refl vv2 = bang (v2 ⊕ dv2) , rrelV3→⊕ v2 dv2 v3 vv2
-- -- ocomposev n v1 (dclosure dt ρ dρ) v2 dv2 v3 vv1 vv2 = {!!}
-- -- ocomposev n v1 (dnatV n₁) v2 dv2 v3 vv1 vv2 = {!!}
-- -- ocomposev n v1 (dpairV dv1 dv2) v2 dv3 v3 vv1 vv2 = {!!}
-- -- ocomposeV : ∀ {τ n} (v : Val τ) → Σ[ dv ∈ DVal τ ] rrelV3 τ v dv v n
-- ocomposeρ {∅} n ∅ dρ1 ρ2 dρ2 ∅ ρρ1 ρρ2 = ∅ , tt
-- ocomposeρ {τ • Γ} n (v1 • ρ1) (dv1 • dρ1) (v2 • ρ2) (dv2 • dρ2) (v3 • ρ3) (vv1 , ρρ1) (vv2 , ρρ2) =
-- let dv , vv = ocomposev n v1 dv1 v2 dv2 v3 vv1 vv2
-- dρ , ρρ = ocomposeρ n ρ1 dρ1 ρ2 dρ2 ρ3 ρρ1 ρρ2
-- in dv • dρ , vv , ρρ
-- But we sort of know how to store environments validity proofs in closures, you just need to use well-founded inductions, even if you're using types. Sad, yes, that's insanely tedious. Let's leave that to Coq.
-- What is also interesting: if that were fixed, could we prove correct
-- composition for change *terms* and rrelT3? And for *open expressions*? The
-- last is the main problem Ahmed run into.
|
--
-- In this module we conclude our proof: we have shown that t, derive-dterm t
-- and produce related values (in the sense of the fundamental property,
-- rfundamental3), and for related values v1, dv and v2 we have v1 ⊕ dv ≡
-- v2 (because ⊕ agrees with validity, rrelV3→⊕).
--
-- We now put these facts together via derive-correct-si and derive-correct.
-- This is immediate, even though I spend so much code on it:
-- Indeed, all theorems are immediate corollaries of what we established (as
-- explained above); only the statements are longer, especially because I bother
-- expanding them.
module Thesis.SIRelBigStep.DeriveCorrect where
open import Data.Product
open import Relation.Binary.PropositionalEquality
open import Thesis.SIRelBigStep.IlcSILR
open import Thesis.SIRelBigStep.FundamentalProperty
-- Theorem statement. This theorem still mentions step-indexes explicitly.
derive-correct-si-type =
∀ {τ Γ k} (t : Term Γ τ) ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
rrelT3-skeleton (λ v1 dv v2 _ → v1 ⊕ dv ≡ v2) t (derive-dterm t) t ρ1 dρ ρ2 k
-- A verified expansion of the theorem statement.
derive-correct-si-type-means :
derive-correct-si-type ≡
∀ {τ Γ k} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
(v1 v2 : Val τ) →
∀ j (j<k : j < k) →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct-si-type-means = refl
derive-correct-si : derive-correct-si-type
derive-correct-si t ρ1 dρ ρ2 ρρ = rrelT3→⊕ t (derive-dterm t) t ρ1 dρ ρ2 (rfundamental3 _ t ρ1 dρ ρ2 ρρ)
-- Infinitely related environments:
rrelρ3-inf : ∀ Γ (ρ1 : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) (ρ2 : ⟦ Γ ⟧Context) → Set
rrelρ3-inf Γ ρ1 dρ ρ2 = ∀ k → rrelρ3 Γ ρ1 dρ ρ2 k
-- A theorem statement for infinitely-related environments. On paper, if we informally
-- omit the arbitrary derivation step-index as usual, we get a statement without
-- step-indexes.
derive-correct :
∀ {τ Γ} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3-inf Γ ρ1 dρ ρ2) →
(v1 v2 : Val τ) →
∀ j →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct t ρ1 dρ ρ2 ρρ v1 v2 j = derive-correct-si t ρ1 dρ ρ2 (ρρ (suc j)) v1 v2 j ≤-refl
open import Data.Unit
nilρ : ∀ {Γ n} (ρ : ⟦ Γ ⟧Context) → Σ[ dρ ∈ ChΔ Γ ] rrelρ3 Γ ρ dρ ρ n
nilV : ∀ {τ n} (v : Val τ) → Σ[ dv ∈ DVal τ ] rrelV3 τ v dv v n
nilV (closure t ρ) = let dρ , ρρ = nilρ ρ in dclosure (derive-dterm t) ρ dρ , rfundamental3svv _ (abs t) ρ dρ ρ ρρ
nilV (natV n₁) = dnatV zero , refl
nilV (pairV a b) = let 0a , aa = nilV a; 0b , bb = nilV b in dpairV 0a 0b , aa , bb
nilρ ∅ = ∅ , tt
nilρ (v • ρ) = let dv , vv = nilV v ; dρ , ρρ = nilρ ρ in dv • dρ , vv , ρρ
-- -- Try to prove to define validity-preserving change composition.
-- -- If we don't store proofs that environment changes in closures are valid, we
-- -- can't finish the proof for the closure case using the Fundamental property.
-- -- But it seems that's not the approach we need to use: we might need
-- -- environment validity elsewhere.
-- -- Moreover, the proof is annoying to do unless we build a datatype to invert
-- -- validity proofs (as suggested by Ezyang in
-- -- http://blog.ezyang.com/2013/09/induction-and-logical-relations/).
-- ocomposeρ : ∀ {Γ} n ρ1 dρ1 ρ2 dρ2 ρ3 → rrelρ3 Γ ρ1 dρ1 ρ2 n → rrelρ3 Γ ρ2 dρ2 ρ3 n → Σ[ dρ ∈ ChΔ Γ ] rrelρ3 Γ ρ1 dρ ρ3 n
-- ocomposev : ∀ {τ} n v1 dv1 v2 dv2 v3 → rrelV3 τ v1 dv1 v2 n → rrelV3 τ v2 dv2 v3 n → Σ[ dv ∈ DVal τ ] rrelV3 τ v1 dv v3 n
-- ocomposev n v1 dv1 v2 (bang v3) .v3 vv1 refl = bang v3 , refl
-- ocomposev n v1 (bang x) v2 (dclosure dt ρ dρ) v3 vv1 vv2 = {!!}
-- ocomposev n (closure t ρ) (dclosure .(derive-dterm t) .ρ dρ1) (closure .t .(ρ ⊕ρ dρ1)) (dclosure .(derive-dterm t) .(ρ ⊕ρ dρ1) dρ2) (closure .t .((ρ ⊕ρ dρ1) ⊕ρ dρ2)) ((refl , refl) , refl , refl , refl , refl , vv1) ((refl , refl) , refl , refl , refl , refl , vv2) =
-- let dρ , ρρ = ocomposeρ n ρ dρ1 (ρ ⊕ρ dρ1) dρ2 ((ρ ⊕ρ dρ1) ⊕ρ dρ2) {!!} {!!}
-- in dclosure (derive-dterm t) ρ dρ , (refl , refl) , refl , {!!} , refl , refl ,
-- λ { k (s≤s k≤n) v1 dv v2 vv →
-- let nilv2 , nilv2v = nilV v2
-- -- vv1 k ? v1 dv v2 vv
-- -- vv2 k ? v2 nilv2 v2 nilv2v
-- in {!!}
-- }
-- -- rfundamental3 k t (v1 • ρ) (dv • dρ) (v2 • ((ρ ⊕ρ dρ1) ⊕ρ dρ2)) (vv , rrelρ3-mono k n (≤-step k<n) _ ρ dρ ((ρ ⊕ρ dρ1) ⊕ρ dρ2) ρρ)}
-- -- p1 , p2 , p3 , p4 , p5
-- ocomposev n v1 dv1 v2 (dnatV n₁) v3 vv1 vv2 = {!!}
-- ocomposev n v1 dv1 v2 (dpairV dv2 dv3) v3 vv1 vv2 = {!!}
-- -- ocomposev n v1 (bang v2) .v2 dv2 v3 refl vv2 = bang (v2 ⊕ dv2) , rrelV3→⊕ v2 dv2 v3 vv2
-- -- ocomposev n v1 (dclosure dt ρ dρ) v2 dv2 v3 vv1 vv2 = {!!}
-- -- ocomposev n v1 (dnatV n₁) v2 dv2 v3 vv1 vv2 = {!!}
-- -- ocomposev n v1 (dpairV dv1 dv2) v2 dv3 v3 vv1 vv2 = {!!}
-- -- ocomposeV : ∀ {τ n} (v : Val τ) → Σ[ dv ∈ DVal τ ] rrelV3 τ v dv v n
-- ocomposeρ {∅} n ∅ dρ1 ρ2 dρ2 ∅ ρρ1 ρρ2 = ∅ , tt
-- ocomposeρ {τ • Γ} n (v1 • ρ1) (dv1 • dρ1) (v2 • ρ2) (dv2 • dρ2) (v3 • ρ3) (vv1 , ρρ1) (vv2 , ρρ2) =
-- let dv , vv = ocomposev n v1 dv1 v2 dv2 v3 vv1 vv2
-- dρ , ρρ = ocomposeρ n ρ1 dρ1 ρ2 dρ2 ρ3 ρρ1 ρρ2
-- in dv • dρ , vv , ρρ
-- -- But we sort of know how to store environments validity proofs in closures, you just need to use well-founded inductions, even if you're using types. Sad, yes, that's insanely tedious. Let's leave that to Coq.
-- -- What is also interesting: if that were fixed, could we prove correct
-- -- composition for change *terms* and rrelT3? And for *open expressions*? The
-- -- last is the main problem Ahmed run into.
-- 1: the proof of transitivity of rrelT by Ahmed is for the case where the second rrelT holds at arbitrary step counts :-)
-- 2: that proof doesn't go through for us. If de1 : e1 -> e2 and de2 : e2 ->
-- e3, we must show that if e1 and e3 evaluate something happens, but to use our
-- hypothesis we need that e2 also terminates, which in our case does not
-- follow. For Ahmed 2006, instead, e1 terminates and e1 ≤ e2 implies that e2
-- terminates.
--
-- Indeed, it seems that we can have a change from e1 to looping e2 and a change
-- from looping e2 to e3, and I don't expect the composition of such changes to
-- satisfy anything.
--
-- Indeed, "Imperative self-adjusting computation" does not mention any
-- transitivity result (even in the technical report).
|
Clarify the problem even more
|
Clarify the problem even more
|
Agda
|
mit
|
inc-lc/ilc-agda
|
6ecbb2d5d6f0f6024afa19dfbd12fdcad3b23439
|
incremental.agda
|
incremental.agda
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Semantics
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
Env⟦_⟧ : Context → Set
Env⟦ ∅ ⟧ = Empty
Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- Semantics
lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
lookup⟦ this ⟧ (v • ρ) = v
lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- Semantics
eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ)
eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ)
eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
nil : ∀ {τ Γ} → Term Γ (Δ-Type τ)
nil {τ₁ ⇒ τ₂} =
abs nil
-- λx. nil
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ
-- CHANGE VARIABLES
-- changes 'x' to 'dx'
rename : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
rename this = that this
rename (that x) = that (that (rename x))
-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)
Δ-var : ∀ {Γ₁ Γ₂ τ} → Var (Γ₁ ⋎ Γ₂) τ → Term (Δ-Context Γ₂) (Δ-Type τ)
Δ-var {∅} x = var (rename x)
Δ-var {τ • Γ} this = nil
Δ-var {τ • Γ} (that x) = Δ-var {Γ} x
-- CHANGE TERMS
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = weaken {∅} {Γ} (Δ-var {Γ} x)
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Denotational Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Denotational 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 (τ′ • Γ) τ
-- Denotational 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 Γ τ
-- Denotational 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 ⟧ ρ
-- NATURAL SEMANTICS
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
nil : ∀ {τ Γ} → Term Γ (Δ-Type τ)
nil {τ₁ ⇒ τ₂} =
abs nil
-- λx. nil
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ
-- CHANGE VARIABLES
-- changes 'x' to 'dx'
rename : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
rename this = that this
rename (that x) = that (that (rename x))
-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)
Δ-var : ∀ {Γ₁ Γ₂ τ} → Var (Γ₁ ⋎ Γ₂) τ → Term (Δ-Context Γ₂) (Δ-Type τ)
Δ-var {∅} x = var (rename x)
Δ-var {τ • Γ} this = nil
Δ-var {τ • Γ} (that x) = Δ-var {Γ} x
-- CHANGE TERMS
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = weaken {∅} {Γ} (Δ-var {Γ} x)
|
Implement natural semantics.
|
Implement natural semantics.
Natural semantics (= big-step operational semantics) came up in
a Skype chat with Paolo.
Old-commit-hash: 40138450230bdc6326bbd4d47b011921433466b1
|
Agda
|
mit
|
inc-lc/ilc-agda
|
64e621c3d2a135c31988f71656671c97b59d8eca
|
arrow.agda
|
arrow.agda
|
data Bool : Set where
true : Bool
false : Bool
_or_ : Bool → Bool → Bool
true or true = true
true or false = true
false or true = true
false or false = false
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs ∘)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
|
data Bool : Set where
true : Bool
false : Bool
_or_ : Bool → Bool → Bool
true or true = true
true or false = true
false or true = true
false or false = false
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
any : {A : Set} → (A → Bool) → List A → Bool
any _ ∘ = false
any f (x ∷ xs) = (f x) or (any f xs)
apply : {A B : Set} → (A → B) → List A → List B
apply _ ∘ = ∘
apply f (x ∷ xs) = (f x) ∷ (apply f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
_,_⊨_ : Separation → List Arrow → ℕ → Bool
((model holds _) , cs ⊨ n) = (cs , holds ⊢ n)
_,_¬⊨_ : Separation → List Arrow → ℕ → Bool
((model _ fails) , cs ¬⊨ n) = any (_∋_ (closure cs (n ∷ ∘))) fails
--_,_,_¬⊨_ : List Separation → List Arrow → List ℕ → ℕ → Bool
|
Add separations
|
Add separations
|
Agda
|
mit
|
louisswarren/hieretikz
|
af9d23c116a68842dcbe0843b111aba0220b3560
|
Atlas/Change/Term.agda
|
Atlas/Change/Term.agda
|
module Atlas.Change.Term where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
-- nil-changes
nil-const : ∀ {ι : Base} → Const ∅ (base (ΔBase ι))
nil-const {ι} = neutral {ΔBase ι}
nil-term : ∀ {ι Γ} → Term Γ (base (ΔBase ι))
nil-term {Bool} = curriedConst (nil-const {Bool})
nil-term {Map κ ι} = curriedConst (nil-const {Map κ ι})
-- diff-term and apply-term
open import Parametric.Change.Term Const ΔBase
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
diff-base : ∀ {ι Γ} →
Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))
diff-base {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
diff-base {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs diff-base) m₁ m₂)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
Atlas-apply {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs Atlas-apply) m₁ m₂)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ (lift-diff diff-base Atlas-apply)
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ (lift-apply diff-base Atlas-apply)
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
insert : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
-- last argument is the change accumulator
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
|
module Atlas.Change.Term where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
-- nil-changes
nil-const : ∀ {ι : Base} → Const ∅ (base (ΔBase ι))
nil-const {ι} = neutral {ΔBase ι}
nil-term : ∀ {ι Γ} → Term Γ (base (ΔBase ι))
nil-term {Bool} = curriedConst (nil-const {Bool})
nil-term {Map κ ι} = curriedConst (nil-const {Map κ ι})
-- diff-term and apply-term
open import Parametric.Change.Term Const ΔBase
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
diff-base : ∀ {ι Γ} →
Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))
diff-base {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
diff-base {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs diff-base) m₁ m₂)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
apply-base : ∀ {ι Γ} →
Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
apply-base {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
apply-base {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs apply-base) m₁ m₂)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ (lift-diff diff-base apply-base)
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ (lift-apply diff-base apply-base)
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
insert : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
-- last argument is the change accumulator
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
|
Rename Atlas-apply to apply-base.
|
Rename Atlas-apply to apply-base.
Old-commit-hash: aa571d5b9c41b6b4aa16886b55721dc3dbd9e033
|
Agda
|
mit
|
inc-lc/ilc-agda
|
aa15436d10416e76d1a5f6494f6bf9dfde53c404
|
Thesis/SIRelBigStep/DSyntax.agda
|
Thesis/SIRelBigStep/DSyntax.agda
|
module Thesis.SIRelBigStep.DSyntax where
open import Thesis.SIRelBigStep.Syntax public
-- data DType : Set where
-- _⇒_ : (σ τ : DType) → DType
-- int : DType
DType = Type
import Base.Syntax.Context
module DC = Base.Syntax.Context DType
Δτ : Type → DType
Δτ (σ ⇒ τ) = σ ⇒ Δτ σ ⇒ Δτ τ
Δτ (pair τ1 τ2) = pair (Δτ τ1) (Δτ τ2)
Δτ nat = nat
ΔΔ : Context → DC.Context
ΔΔ ∅ = ∅
ΔΔ (τ • Γ) = Δτ τ • ΔΔ Γ
--ΔΔ Γ = Γ
-- A DTerm evaluates in normal context Δ, change context (ΔΔ Δ), and produces
-- a result of type (Δt τ).
data DTerm (Δ : Context) (τ : DType) : Set
data DSVal (Δ : Context) : (τ : DType) → Set where
dvar : ∀ {τ} →
(x : Var Δ τ) →
DSVal Δ τ
dabs : ∀ {σ τ}
(dt : DTerm (σ • Δ) τ) →
DSVal Δ (σ ⇒ τ)
dcons : ∀ {τ1 τ2}
(dsv1 : DSVal Δ τ1)
(dsv2 : DSVal Δ τ2) →
DSVal Δ (pair τ1 τ2)
dconst : ∀ {τ} → (dc : Const (Δτ τ)) → DSVal Δ τ
data DTerm (Δ : Context) (τ : DType) where
dval :
DSVal Δ τ →
DTerm Δ τ
dprimapp : ∀ {σ}
(p : Primitive (σ ⇒ τ)) →
(sv : SVal Δ σ) →
(dsv : DSVal Δ σ) →
DTerm Δ τ
dconst :
(c : Const τ) →
DTerm Δ τ
dapp : ∀ {σ}
(dvs : DSVal Δ (σ ⇒ τ)) →
(vt : SVal Δ σ) →
(dvt : DSVal Δ σ) →
DTerm Δ τ
dlett : ∀ {σ}
(s : Term Δ σ) →
(ds : DTerm Δ σ) →
(dt : DTerm (σ • Δ) τ) →
DTerm Δ τ
|
module Thesis.SIRelBigStep.DSyntax where
open import Thesis.SIRelBigStep.Syntax public
-- data DType : Set where
-- _⇒_ : (σ τ : DType) → DType
-- int : DType
DType = Type
import Base.Syntax.Context
module DC = Base.Syntax.Context DType
Δτ : Type → DType
Δτ (σ ⇒ τ) = σ ⇒ Δτ σ ⇒ Δτ τ
Δτ (pair τ1 τ2) = pair (Δτ τ1) (Δτ τ2)
Δτ nat = nat
ΔΔ : Context → DC.Context
ΔΔ ∅ = ∅
ΔΔ (τ • Γ) = Δτ τ • ΔΔ Γ
--ΔΔ Γ = Γ
-- A DTerm evaluates in normal context Δ, change context (ΔΔ Δ), and produces
-- a result of type (Δt τ).
data DTerm (Δ : Context) (τ : DType) : Set
data DSVal (Δ : Context) : (τ : DType) → Set where
dvar : ∀ {τ} →
(x : Var Δ τ) →
DSVal Δ τ
dabs : ∀ {σ τ}
(dt : DTerm (σ • Δ) τ) →
DSVal Δ (σ ⇒ τ)
dcons : ∀ {τ1 τ2}
(dsv1 : DSVal Δ τ1)
(dsv2 : DSVal Δ τ2) →
DSVal Δ (pair τ1 τ2)
dconst : ∀ {τ} → (dc : Const (Δτ τ)) → DSVal Δ τ
data DTerm (Δ : Context) (τ : DType) where
dval :
DSVal Δ τ →
DTerm Δ τ
dprimapp : ∀ {σ}
(p : Primitive (σ ⇒ τ)) →
(sv : SVal Δ σ) →
(dsv : DSVal Δ σ) →
DTerm Δ τ
dapp : ∀ {σ}
(dvs : DSVal Δ (σ ⇒ τ)) →
(vt : SVal Δ σ) →
(dvt : DSVal Δ σ) →
DTerm Δ τ
dlett : ∀ {σ}
(s : Term Δ σ) →
(ds : DTerm Δ σ) →
(dt : DTerm (σ • Δ) τ) →
DTerm Δ τ
|
Drop unused redundant case
|
Drop unused redundant case
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9e3f211e88b705e21001d83b692c5504a15a4f1a
|
lib/Data/Bits.agda
|
lib/Data/Bits.agda
|
module Data.Bits where
-- cleanup
open import Category.Applicative
open import Category.Monad
open import Data.Nat.NP hiding (_≤_; _==_)
open import Data.Nat.DivMod
open import Data.Bool.NP hiding (_==_)
open import Data.Bool.Properties using (not-involutive)
open import Data.Maybe
import Data.Fin as Fin
open Fin using (Fin; zero; suc; #_; inject₁; inject+; raise)
open import Data.Vec.NP hiding (_⊛_) renaming (map to vmap)
open import Data.Vec.N-ary.NP
open import Data.Unit using (⊤)
open import Data.Empty using (⊥)
open import Data.Product using (_×_; _,_; uncurry; proj₁; proj₂)
open import Function.NP hiding (_→⟨_⟩_)
import Relation.Binary.PropositionalEquality.NP as ≡
open ≡
open import Algebra.FunctionProperties
import Data.List as L
open import Data.Bool.NP public using (_xor_)
open import Data.Vec.NP public using ([]; _∷_; head; tail; replicate)
Bit : Set
Bit = Bool
module Defs where
0b = false
1b = true
module Patterns where
pattern 0b = false
pattern 1b = true
open Patterns
Bits : ℕ → Set
Bits = Vec Bit
_→ᵇ_ : ℕ → ℕ → Set
i →ᵇ o = Bits i → Bits o
0ⁿ : ∀ {n} → Bits n
0ⁿ = replicate 0b
-- Warning: 0ⁿ {0} ≡ 1ⁿ {0}
1ⁿ : ∀ {n} → Bits n
1ⁿ = replicate 1b
0∷_ : ∀ {n} → Bits n → Bits (suc n)
0∷ xs = 0b ∷ xs
-- can't we make these pattern aliases?
1∷_ : ∀ {n} → Bits n → Bits (suc n)
1∷ xs = 1b ∷ xs
_!_ : ∀ {a n} {A : Set a} → Vec A n → Fin n → A
_!_ = flip lookup
_==ᵇ_ : (b₀ b₁ : Bit) → Bool
b₀ ==ᵇ b₁ = not (b₀ xor b₁)
_==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bool
[] == [] = true
(b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁
infixr 5 _⊕_
_⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n
_⊕_ = zipWith _xor_
vnot : ∀ {n} → Endo (Bits n)
vnot = _⊕_ 1ⁿ
vnot∘vnot≗id : ∀ {n} → vnot {n} ∘ vnot ≗ id
vnot∘vnot≗id [] = refl
vnot∘vnot≗id (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot≗id xs)
⊕-assoc : ∀ {n} → Associative _≡_ (_⊕_ {n})
⊕-assoc [] [] [] = refl
⊕-assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) rewrite ⊕-assoc xs ys zs | Xor°.+-assoc x y z = refl
⊕-comm : ∀ {n} → Commutative _≡_ (_⊕_ {n})
⊕-comm [] [] = refl
⊕-comm (x ∷ xs) (y ∷ ys) rewrite ⊕-comm xs ys | Xor°.+-comm x y = refl
⊕-left-identity : ∀ {n} → LeftIdentity _≡_ 0ⁿ (_⊕_ {n})
⊕-left-identity [] = refl
⊕-left-identity (x ∷ xs) rewrite ⊕-left-identity xs = refl
⊕-right-identity : ∀ {n} → RightIdentity _≡_ 0ⁿ (_⊕_ {n})
⊕-right-identity [] = refl
⊕-right-identity (x ∷ xs) rewrite ⊕-right-identity xs | proj₂ Xor°.+-identity x = refl
⊕-≡ : ∀ {n} (x : Bits n) → x ⊕ x ≡ 0ⁿ
⊕-≡ [] = refl
⊕-≡ (x ∷ xs) rewrite ⊕-≡ xs | proj₂ Xor°.-‿inverse x = refl
⊕-≢ : ∀ {n} (x : Bits n) → x ⊕ vnot x ≡ 1ⁿ
⊕-≢ x = x ⊕ vnot x ≡⟨ refl ⟩
x ⊕ (1ⁿ ⊕ x) ≡⟨ cong (_⊕_ x) (⊕-comm 1ⁿ x) ⟩
x ⊕ (x ⊕ 1ⁿ) ≡⟨ sym (⊕-assoc x x 1ⁿ) ⟩
(x ⊕ x) ⊕ 1ⁿ ≡⟨ cong (flip _⊕_ 1ⁿ) (⊕-≡ x) ⟩
0ⁿ ⊕ 1ⁿ ≡⟨ ⊕-left-identity 1ⁿ ⟩
1ⁿ ∎ where open ≡-Reasoning
msb : ∀ k {n} → Bits (k + n) → Bits k
msb = take
lsb : ∀ {n} k → Bits (n + k) → Bits k
lsb {n} k rewrite ℕ°.+-comm n k = reverse ∘ msb k ∘ reverse
msb₂ : ∀ {n} → Bits (2 + n) → Bits 2
msb₂ = msb 2
lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2
lsb₂ = reverse ∘ msb 2 ∘ reverse
#1 : ∀ {n} → Bits n → Fin (suc n)
#1 [] = zero
#1 (0b ∷ bs) = inject₁ (#1 bs)
#1 (1b ∷ bs) = suc (#1 bs)
#0 : ∀ {n} → Bits n → Fin (suc n)
#0 = #1 ∘ vmap not
private
module M {a} {A : Set a} {M : Set a → Set a} (appl : RawApplicative M) where
open RawApplicative appl
replicateM : ∀ {n} → M A → M (Vec A n)
replicateM {n = zero} _ = pure []
replicateM {n = suc n} x = pure _∷_ ⊛ x ⊛ replicateM x
open M public
allBitsL : ∀ n → L.List (Bits n)
allBitsL _ = replicateM rawIApplicative (toList (0b ∷ 1b ∷ []))
where open RawMonad L.monad
allBits : ∀ n → Vec (Bits n) (2 ^ n)
allBits zero = [] ∷ []
allBits (suc n) rewrite ℕ°.+-comm (2 ^ n) 0 = vmap 0∷_ bs ++ vmap 1∷_ bs
where bs = allBits n
#⟨_⟩ : ∀ {n} → (Bits n → Bool) → Fin (suc (2 ^ n))
#⟨ pred ⟩ = count pred (allBits _)
sucBCarry : ∀ {n} → Bits n → Bits (1 + n)
sucBCarry [] = 0b ∷ []
sucBCarry (0b ∷ xs) = 0b ∷ sucBCarry xs
sucBCarry (1b ∷ xs) with sucBCarry xs
... | 0b ∷ bs = 0b ∷ 1b ∷ bs
... | 1b ∷ bs = 1b ∷ 0b ∷ bs
sucB : ∀ {n} → Bits n → Bits n
sucB = tail ∘ sucBCarry
_[mod_] : ℕ → ℕ → Set
a [mod b ] = DivMod' a b
proj : ∀ {a} {A : Set a} → A × A → Bit → A
proj (x₀ , x₁) 0b = x₀
proj (x₀ , x₁) 1b = x₁
module ReversedBits where
sucRB : ∀ {n} → Bits n → Bits n
sucRB [] = []
sucRB (0b ∷ xs) = 1b ∷ xs
sucRB (1b ∷ xs) = 0b ∷ sucRB xs
toFin : ∀ {n} → Bits n → Fin (2 ^ n)
toFin [] = zero
toFin (0b ∷ xs) = inject+ _ (toFin xs)
toFin {suc n} (1b ∷ xs) = raise (2 ^ n) (inject+ 0 (toFin xs))
{-
toℕ : ∀ {n} → Bits n → ℕ
toℕ = Fin.toℕ ∘ toFin
-}
toℕ : ∀ {n} → Bits n → ℕ
toℕ [] = zero
toℕ (0b ∷ xs) = toℕ xs
toℕ {suc n} (1b ∷ xs) = 2 ^ n + toℕ xs
fromℕ : ∀ {n} → ℕ → Bits n
fromℕ = fold 0ⁿ sucB
fromFin : ∀ {n} → Fin (2 ^ n) → Bits n
fromFin = fromℕ ∘ Fin.toℕ
lookupTbl : ∀ {n a} {A : Set a} → Bits n → Vec A (2 ^ n) → A
lookupTbl [] (x ∷ []) = x
lookupTbl (0b ∷ key) tbl = lookupTbl key (take _ tbl)
lookupTbl {suc n} (1b ∷ key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))
funFromTbl : ∀ {n a} {A : Set a} → Vec A (2 ^ n) → (Bits n → A)
funFromTbl = flip lookupTbl
tblFromFun : ∀ {n a} {A : Set a} → (Bits n → A) → Vec A (2 ^ n)
-- tblFromFun f = tabulate (f ∘ fromFin)
tblFromFun {zero} f = f [] ∷ []
tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_) ++ tblFromFun {n} (f ∘ 1∷_) ++ []
funFromTbl∘tblFromFun : ∀ {n a} {A : Set a} (fun : Bits n → A) → funFromTbl (tblFromFun fun) ≗ fun
funFromTbl∘tblFromFun {zero} f [] = refl
funFromTbl∘tblFromFun {suc n} f (0b ∷ xs)
rewrite take-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ []) =
funFromTbl∘tblFromFun {n} (f ∘ 0∷_) xs
funFromTbl∘tblFromFun {suc n} f (1b ∷ xs)
rewrite drop-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ [])
| take-++ (2 ^ n) (tblFromFun {n} (f ∘ 1∷_)) [] =
funFromTbl∘tblFromFun {n} (f ∘ 1∷_) xs
tblFromFun∘funFromTbl : ∀ {n a} {A : Set a} (tbl : Vec A (2 ^ n)) → tblFromFun {n} (funFromTbl tbl) ≡ tbl
tblFromFun∘funFromTbl {zero} (x ∷ []) = refl
tblFromFun∘funFromTbl {suc n} tbl
rewrite tblFromFun∘funFromTbl {n} (take _ tbl)
| tblFromFun∘funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))
| take-them-all (2 ^ n) (drop (2 ^ n) tbl)
| take-drop-lem (2 ^ n) tbl
= refl
{-
sucB-lem : ∀ {n} x → toℕ {2 ^ n} (sucB x) [mod 2 ^ n ] ≡ (suc (toℕ x)) [mod 2 ^ n ]
sucB-lem x = {!!}
-- sucB-lem : ∀ {n} x → (sucB (fromℕ x)) [mod 2 ^ n ] ≡ fromℕ ((suc x) [mod 2 ^ n ])
toℕ∘fromℕ : ∀ {n} x → toℕ (fromℕ {n} x) ≡ x
toℕ∘fromℕ zero = {!!}
toℕ∘fromℕ (suc x) = {!toℕ∘fromℕ x!}
toℕ∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toℕ (fromFin x) ≡ Fin.toℕ x
toℕ∘fromFin x = {!!}
toFin∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toFin (fromFin x) ≡ x
toFin∘fromFin x = {!!}
-- _ᴮ : (s : String) {pf : IsBitString s} → Bits (length s)
-- _ᴮ =
-}
open Defs public
|
module Data.Bits where
-- cleanup
open import Category.Applicative
open import Category.Monad
open import Data.Nat.NP hiding (_≤_; _==_)
open import Data.Nat.DivMod
open import Data.Bool.NP hiding (_==_)
open import Data.Bool.Properties using (not-involutive)
open import Data.Maybe
import Data.Fin as Fin
open Fin using (Fin; zero; suc; #_; inject₁; inject+; raise)
open import Data.Vec.NP hiding (_⊛_) renaming (map to vmap)
open import Data.Vec.N-ary.NP
open import Data.Unit using (⊤)
open import Data.Empty using (⊥)
open import Data.Product using (_×_; _,_; uncurry; proj₁; proj₂)
open import Function.NP hiding (_→⟨_⟩_)
import Relation.Binary.PropositionalEquality.NP as ≡
open ≡
open import Algebra.FunctionProperties
import Data.List as L
open import Data.Bool.NP public using (_xor_)
open import Data.Vec.NP public using ([]; _∷_; head; tail; replicate)
Bit : Set
Bit = Bool
module Defs where
0b = false
1b = true
module Patterns where
pattern 0b = false
pattern 1b = true
open Patterns
Bits : ℕ → Set
Bits = Vec Bit
_→ᵇ_ : ℕ → ℕ → Set
i →ᵇ o = Bits i → Bits o
0ⁿ : ∀ {n} → Bits n
0ⁿ = replicate 0b
-- Warning: 0ⁿ {0} ≡ 1ⁿ {0}
1ⁿ : ∀ {n} → Bits n
1ⁿ = replicate 1b
0∷_ : ∀ {n} → Bits n → Bits (suc n)
0∷ xs = 0b ∷ xs
-- can't we make these pattern aliases?
1∷_ : ∀ {n} → Bits n → Bits (suc n)
1∷ xs = 1b ∷ xs
_!_ : ∀ {a n} {A : Set a} → Vec A n → Fin n → A
_!_ = flip lookup
_==ᵇ_ : (b₀ b₁ : Bit) → Bool
b₀ ==ᵇ b₁ = not (b₀ xor b₁)
_==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bool
[] == [] = true
(b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁
infixr 5 _⊕_
_⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n
_⊕_ = zipWith _xor_
vnot : ∀ {n} → Endo (Bits n)
vnot = _⊕_ 1ⁿ
vnot∘vnot≗id : ∀ {n} → vnot {n} ∘ vnot ≗ id
vnot∘vnot≗id [] = refl
vnot∘vnot≗id (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot≗id xs)
⊕-assoc : ∀ {n} → Associative _≡_ (_⊕_ {n})
⊕-assoc [] [] [] = refl
⊕-assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) rewrite ⊕-assoc xs ys zs | Xor°.+-assoc x y z = refl
⊕-comm : ∀ {n} → Commutative _≡_ (_⊕_ {n})
⊕-comm [] [] = refl
⊕-comm (x ∷ xs) (y ∷ ys) rewrite ⊕-comm xs ys | Xor°.+-comm x y = refl
⊕-left-identity : ∀ {n} → LeftIdentity _≡_ 0ⁿ (_⊕_ {n})
⊕-left-identity [] = refl
⊕-left-identity (x ∷ xs) rewrite ⊕-left-identity xs = refl
⊕-right-identity : ∀ {n} → RightIdentity _≡_ 0ⁿ (_⊕_ {n})
⊕-right-identity [] = refl
⊕-right-identity (x ∷ xs) rewrite ⊕-right-identity xs | proj₂ Xor°.+-identity x = refl
⊕-≡ : ∀ {n} (x : Bits n) → x ⊕ x ≡ 0ⁿ
⊕-≡ [] = refl
⊕-≡ (x ∷ xs) rewrite ⊕-≡ xs | proj₂ Xor°.-‿inverse x = refl
⊕-≢ : ∀ {n} (x : Bits n) → x ⊕ vnot x ≡ 1ⁿ
⊕-≢ x = x ⊕ vnot x ≡⟨ refl ⟩
x ⊕ (1ⁿ ⊕ x) ≡⟨ cong (_⊕_ x) (⊕-comm 1ⁿ x) ⟩
x ⊕ (x ⊕ 1ⁿ) ≡⟨ sym (⊕-assoc x x 1ⁿ) ⟩
(x ⊕ x) ⊕ 1ⁿ ≡⟨ cong (flip _⊕_ 1ⁿ) (⊕-≡ x) ⟩
0ⁿ ⊕ 1ⁿ ≡⟨ ⊕-left-identity 1ⁿ ⟩
1ⁿ ∎ where open ≡-Reasoning
msb : ∀ k {n} → Bits (k + n) → Bits k
msb = take
lsb : ∀ {n} k → Bits (n + k) → Bits k
lsb {n} k rewrite ℕ°.+-comm n k = reverse ∘ msb k ∘ reverse
msb₂ : ∀ {n} → Bits (2 + n) → Bits 2
msb₂ = msb 2
lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2
lsb₂ = reverse ∘ msb 2 ∘ reverse
#1 : ∀ {n} → Bits n → Fin (suc n)
#1 [] = zero
#1 (0b ∷ bs) = inject₁ (#1 bs)
#1 (1b ∷ bs) = suc (#1 bs)
#0 : ∀ {n} → Bits n → Fin (suc n)
#0 = #1 ∘ vmap not
private
module M {a} {A : Set a} {M : Set a → Set a} (appl : RawApplicative M) where
open RawApplicative appl
replicateM : ∀ {n} → M A → M (Vec A n)
replicateM {n = zero} _ = pure []
replicateM {n = suc n} x = pure _∷_ ⊛ x ⊛ replicateM x
open M public
allBitsL : ∀ n → L.List (Bits n)
allBitsL _ = replicateM rawIApplicative (toList (0b ∷ 1b ∷ []))
where open RawMonad L.monad
allBits : ∀ n → Vec (Bits n) (2^ n)
allBits zero = [] ∷ []
allBits (suc n) = vmap 0∷_ bs ++ vmap 1∷_ bs
where bs = allBits n
#⟨_⟩ : ∀ {n} → (Bits n → Bool) → Fin (suc (2^ n))
#⟨ pred ⟩ = count pred (allBits _)
sucBCarry : ∀ {n} → Bits n → Bits (1 + n)
sucBCarry [] = 0b ∷ []
sucBCarry (0b ∷ xs) = 0b ∷ sucBCarry xs
sucBCarry (1b ∷ xs) with sucBCarry xs
... | 0b ∷ bs = 0b ∷ 1b ∷ bs
... | 1b ∷ bs = 1b ∷ 0b ∷ bs
sucB : ∀ {n} → Bits n → Bits n
sucB = tail ∘ sucBCarry
_[mod_] : ℕ → ℕ → Set
a [mod b ] = DivMod' a b
proj : ∀ {a} {A : Set a} → A × A → Bit → A
proj (x₀ , x₁) 0b = x₀
proj (x₀ , x₁) 1b = x₁
module ReversedBits where
sucRB : ∀ {n} → Bits n → Bits n
sucRB [] = []
sucRB (0b ∷ xs) = 1b ∷ xs
sucRB (1b ∷ xs) = 0b ∷ sucRB xs
toFin : ∀ {n} → Bits n → Fin (2 ^ n)
toFin [] = zero
toFin (0b ∷ xs) = inject+ _ (toFin xs)
toFin {suc n} (1b ∷ xs) = raise (2 ^ n) (inject+ 0 (toFin xs))
{-
toℕ : ∀ {n} → Bits n → ℕ
toℕ = Fin.toℕ ∘ toFin
-}
toℕ : ∀ {n} → Bits n → ℕ
toℕ [] = zero
toℕ (0b ∷ xs) = toℕ xs
toℕ {suc n} (1b ∷ xs) = 2 ^ n + toℕ xs
fromℕ : ∀ {n} → ℕ → Bits n
fromℕ = fold 0ⁿ sucB
fromFin : ∀ {n} → Fin (2 ^ n) → Bits n
fromFin = fromℕ ∘ Fin.toℕ
lookupTbl : ∀ {n a} {A : Set a} → Bits n → Vec A (2 ^ n) → A
lookupTbl [] (x ∷ []) = x
lookupTbl (0b ∷ key) tbl = lookupTbl key (take _ tbl)
lookupTbl {suc n} (1b ∷ key) tbl = lookupTbl key (take (2 ^ n) (drop (2 ^ n) tbl))
funFromTbl : ∀ {n a} {A : Set a} → Vec A (2 ^ n) → (Bits n → A)
funFromTbl = flip lookupTbl
tblFromFun : ∀ {n a} {A : Set a} → (Bits n → A) → Vec A (2 ^ n)
-- tblFromFun f = tabulate (f ∘ fromFin)
tblFromFun {zero} f = f [] ∷ []
tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_) ++ tblFromFun {n} (f ∘ 1∷_) ++ []
funFromTbl∘tblFromFun : ∀ {n a} {A : Set a} (fun : Bits n → A) → funFromTbl (tblFromFun fun) ≗ fun
funFromTbl∘tblFromFun {zero} f [] = refl
funFromTbl∘tblFromFun {suc n} f (0b ∷ xs)
rewrite take-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ []) =
funFromTbl∘tblFromFun {n} (f ∘ 0∷_) xs
funFromTbl∘tblFromFun {suc n} f (1b ∷ xs)
rewrite drop-++ (2 ^ n) (tblFromFun {n} (f ∘ 0∷_)) (tblFromFun {n} (f ∘ 1∷_) ++ [])
| take-++ (2 ^ n) (tblFromFun {n} (f ∘ 1∷_)) [] =
funFromTbl∘tblFromFun {n} (f ∘ 1∷_) xs
tblFromFun∘funFromTbl : ∀ {n a} {A : Set a} (tbl : Vec A (2 ^ n)) → tblFromFun {n} (funFromTbl tbl) ≡ tbl
tblFromFun∘funFromTbl {zero} (x ∷ []) = refl
tblFromFun∘funFromTbl {suc n} tbl
rewrite tblFromFun∘funFromTbl {n} (take _ tbl)
| tblFromFun∘funFromTbl {n} (take (2 ^ n) (drop (2 ^ n) tbl))
| take-them-all (2 ^ n) (drop (2 ^ n) tbl)
| take-drop-lem (2 ^ n) tbl
= refl
{-
sucB-lem : ∀ {n} x → toℕ {2 ^ n} (sucB x) [mod 2 ^ n ] ≡ (suc (toℕ x)) [mod 2 ^ n ]
sucB-lem x = {!!}
-- sucB-lem : ∀ {n} x → (sucB (fromℕ x)) [mod 2 ^ n ] ≡ fromℕ ((suc x) [mod 2 ^ n ])
toℕ∘fromℕ : ∀ {n} x → toℕ (fromℕ {n} x) ≡ x
toℕ∘fromℕ zero = {!!}
toℕ∘fromℕ (suc x) = {!toℕ∘fromℕ x!}
toℕ∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toℕ (fromFin x) ≡ Fin.toℕ x
toℕ∘fromFin x = {!!}
toFin∘fromFin : ∀ {n} (x : Fin (2 ^ n)) → toFin (fromFin x) ≡ x
toFin∘fromFin x = {!!}
-- _ᴮ : (s : String) {pf : IsBitString s} → Bits (length s)
-- _ᴮ =
-}
open Defs public
|
use 2^ instead of _^_ 2 in allBits and #⟨_⟩
|
Data.Bits: use 2^ instead of _^_ 2 in allBits and #⟨_⟩
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
00065eb75ec8eb897e66dfd3bfb4c202d976fd57
|
Denotational/Values.agda
|
Denotational/Values.agda
|
module Denotational.Values where
-- VALUES
--
-- This module defines the model theory of simple types, that is,
-- it defines for every type, the set of values of that type.
--
-- In fact, we only describe a single model here.
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import Denotational.Notation
open import Syntactic.Types
-- Denotational Semantics
data Bool : Set where
true false : Bool
not : Bool → Bool
not true = false
not false = true
_xor_ : Bool → Bool → Bool
true xor b = not b
false xor b = b
⟦_⟧Type : Type -> Set
⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type
⟦ bool ⟧Type = Bool
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- Value Equivalence
open import Level using (zero)
open import Relation.Binary.PropositionalEquality
postulate ext : Extensionality zero zero
not-not : ∀ a → a ≡ not (not a)
not-not true = refl
not-not false = refl
a-xor-a-false : ∀ a → (a xor a) ≡ false
a-xor-a-false true = refl
a-xor-a-false false = refl
a-xor-false-a : ∀ a → (false xor a) ≡ a
a-xor-false-a b = refl
xor-associative : ∀ a b c → ((b xor c) xor a) ≡ (b xor (c xor a))
xor-associative a true true = not-not a
xor-associative a true false = refl
xor-associative a false c = refl
a-xor-false : ∀ a → a xor false ≡ a
a-xor-false true = refl
a-xor-false false = refl
a-xor-true : ∀ a → a xor true ≡ not a
a-xor-true true = refl
a-xor-true false = refl
xor-commutative : ∀ a b → a xor b ≡ b xor a
xor-commutative true b rewrite a-xor-true b = refl
xor-commutative false b rewrite a-xor-false b = refl
xor-cancellative-2 : ∀ a b → (b xor (a xor a)) ≡ b
xor-cancellative-2 a b rewrite a-xor-a-false a = a-xor-false b
xor-cancellative : ∀ a b → ((b xor a) xor a) ≡ b
xor-cancellative a b rewrite xor-associative a b a = xor-cancellative-2 a b
≡-refl : ∀ {τ} {v : ⟦ τ ⟧} →
v ≡ v
≡-refl = refl
≡-sym : ∀ {τ} {v₁ v₂ : ⟦ τ ⟧} →
v₁ ≡ v₂ → v₂ ≡ v₁
≡-sym = sym
≡-trans : ∀ {τ} {v₁ v₂ v₃ : ⟦ τ ⟧} →
v₁ ≡ v₂ → v₂ ≡ v₃ → v₁ ≡ v₃
≡-trans = trans
≡-cong : ∀ {τ₂ τ₁ v₁ v₂} (f : ⟦ τ₁ ⟧ → ⟦ τ₂ ⟧) →
v₁ ≡ v₂ → f v₁ ≡ f v₂
≡-cong f = cong f
≡-cong₂ : ∀ {τ₃ τ₁ τ₂ v₁ v₂ v₃ v₄} (f : ⟦ τ₁ ⟧ → ⟦ τ₂ ⟧ → ⟦ τ₃ ⟧) →
v₁ ≡ v₂ → v₃ ≡ v₄ → f v₁ v₃ ≡ f v₂ v₄
≡-cong₂ f = cong₂ f
≡-app : ∀ {τ₁ τ₂} {v₁ v₂ : ⟦ τ₁ ⇒ τ₂ ⟧} {v₃ v₄ : ⟦ τ₁ ⟧} →
v₁ ≡ v₂ → v₃ ≡ v₄ → v₁ v₃ ≡ v₂ v₄
≡-app = ≡-cong₂ (λ x y → x y)
≡-isEquivalence : ∀ {τ : Set} → IsEquivalence (_≡_ {A = τ})
≡-isEquivalence = isEquivalence
≡-setoid : Type → Setoid _ _
≡-setoid τ = record
{ Carrier = ⟦ τ ⟧
; _≈_ = _≡_
; isEquivalence = ≡-isEquivalence
}
≡-consistent : ¬ (∀ (τ : Type) → (v₁ v₂ : ⟦ τ ⟧) → v₁ ≡ v₂)
≡-consistent H with H bool true false
... | ()
|
module Denotational.Values where
-- VALUES
--
-- This module defines the model theory of simple types, that is,
-- it defines for every type, the set of values of that type.
--
-- In fact, we only describe a single model here.
open import Data.Bool public
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import Denotational.Notation
open import Syntactic.Types
-- Denotational Semantics
⟦_⟧Type : Type -> Set
⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type
⟦ bool ⟧Type = Bool
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- Value Equivalence
open import Level using (zero)
open import Relation.Binary.PropositionalEquality
postulate ext : Extensionality zero zero
not-not : ∀ a → a ≡ not (not a)
not-not true = refl
not-not false = refl
a-xor-a-false : ∀ a → (a xor a) ≡ false
a-xor-a-false true = refl
a-xor-a-false false = refl
a-xor-false-a : ∀ a → (false xor a) ≡ a
a-xor-false-a b = refl
xor-associative : ∀ a b c → ((b xor c) xor a) ≡ (b xor (c xor a))
xor-associative a true true = not-not a
xor-associative a true false = refl
xor-associative a false c = refl
a-xor-false : ∀ a → a xor false ≡ a
a-xor-false true = refl
a-xor-false false = refl
a-xor-true : ∀ a → a xor true ≡ not a
a-xor-true true = refl
a-xor-true false = refl
xor-commutative : ∀ a b → a xor b ≡ b xor a
xor-commutative true b rewrite a-xor-true b = refl
xor-commutative false b rewrite a-xor-false b = refl
xor-cancellative-2 : ∀ a b → (b xor (a xor a)) ≡ b
xor-cancellative-2 a b rewrite a-xor-a-false a = a-xor-false b
xor-cancellative : ∀ a b → ((b xor a) xor a) ≡ b
xor-cancellative a b rewrite xor-associative a b a = xor-cancellative-2 a b
≡-refl : ∀ {τ} {v : ⟦ τ ⟧} →
v ≡ v
≡-refl = refl
≡-sym : ∀ {τ} {v₁ v₂ : ⟦ τ ⟧} →
v₁ ≡ v₂ → v₂ ≡ v₁
≡-sym = sym
≡-trans : ∀ {τ} {v₁ v₂ v₃ : ⟦ τ ⟧} →
v₁ ≡ v₂ → v₂ ≡ v₃ → v₁ ≡ v₃
≡-trans = trans
≡-cong : ∀ {τ₂ τ₁ v₁ v₂} (f : ⟦ τ₁ ⟧ → ⟦ τ₂ ⟧) →
v₁ ≡ v₂ → f v₁ ≡ f v₂
≡-cong f = cong f
≡-cong₂ : ∀ {τ₃ τ₁ τ₂ v₁ v₂ v₃ v₄} (f : ⟦ τ₁ ⟧ → ⟦ τ₂ ⟧ → ⟦ τ₃ ⟧) →
v₁ ≡ v₂ → v₃ ≡ v₄ → f v₁ v₃ ≡ f v₂ v₄
≡-cong₂ f = cong₂ f
≡-app : ∀ {τ₁ τ₂} {v₁ v₂ : ⟦ τ₁ ⇒ τ₂ ⟧} {v₃ v₄ : ⟦ τ₁ ⟧} →
v₁ ≡ v₂ → v₃ ≡ v₄ → v₁ v₃ ≡ v₂ v₄
≡-app = ≡-cong₂ (λ x y → x y)
≡-isEquivalence : ∀ {τ : Set} → IsEquivalence (_≡_ {A = τ})
≡-isEquivalence = isEquivalence
≡-setoid : Type → Setoid _ _
≡-setoid τ = record
{ Carrier = ⟦ τ ⟧
; _≈_ = _≡_
; isEquivalence = ≡-isEquivalence
}
≡-consistent : ¬ (∀ (τ : Type) → (v₁ v₂ : ⟦ τ ⟧) → v₁ ≡ v₂)
≡-consistent H with H bool true false
... | ()
|
Use booleans from Data.Bool (fix #31).
|
Use booleans from Data.Bool (fix #31).
Old-commit-hash: c2de96202cc68d0b06b45d3c977bdf935e14eec1
|
Agda
|
mit
|
inc-lc/ilc-agda
|
64a0d4678c9a3cfe69e473c9b5f335cd071c1e13
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra as CA
using (ChangeAlgebraFamily)
open import Level
-- Extension Point: Change algebras for base types
Structure : Set₁
Structure = ChangeAlgebraFamily zero ⟦_⟧Base
module Structure (change-algebra-base : Structure) where
-- change algebras
open CA public renaming
( [] to ∅
; _∷_ to _•_
)
-- We provide: change algebra for every type
change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}}
change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type
change-algebra-family = family change-algebra
-- function changes
open FunctionChanges public using (cons)
module _ {τ₁ τ₂ : Type} where
open FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public
renaming
( correct to is-valid
; apply to call-change
)
-- We also provide: change environments (aka. environment changes).
open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebra to environment-changes
)
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍ Γ ₎
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
open import Base.Change.Algebra as CA
using (ChangeAlgebraFamily)
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Level
-- Extension Point: Change algebras for base types
Structure : Set₁
Structure = ChangeAlgebraFamily zero ⟦_⟧Base
module Structure (change-algebra-base : Structure) where
-- change algebras
open CA public renaming
( [] to ∅
; _∷_ to _•_
)
-- We provide: change algebra for every type
change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}}
change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type
change-algebra-family = family change-algebra
-- function changes
open FunctionChanges public using (cons)
module _ {τ₁ τ₂ : Type} where
open FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public
renaming
( correct to is-valid
; apply to call-change
)
-- We also provide: change environments (aka. environment changes).
open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebra to environment-changes
)
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍ Γ ₎
|
Move import to more sensible place
|
Move import to more sensible place
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a6bbe5d2b2206e1090e8303580a73956ec707683
|
formalization/agda/Spire/Examples/DarkwingDuck/Primitive.agda
|
formalization/agda/Spire/Examples/DarkwingDuck/Primitive.agda
|
module Spire.Examples.DarkwingDuck.Primitive where
----------------------------------------------------------------------
infixr 4 _,_
infixr 5 _∷_
----------------------------------------------------------------------
postulate String : Set
{-# BUILTIN STRING String #-}
----------------------------------------------------------------------
data ⊤ : Set where
tt : ⊤
elimUnit : (P : ⊤ → Set)
(ptt : P tt)
(u : ⊤) → P u
elimUnit P ptt tt = ptt
----------------------------------------------------------------------
data Σ (A : Set) (B : A → Set) : Set where
_,_ : (a : A) (b : B a) → Σ A B
elimPair : {A : Set} {B : A → Set}
(P : Σ A B → Set)
(ppair : (a : A) (b : B a) → P (a , b))
(ab : Σ A B) → P ab
elimPair P ppair (a , b) = ppair a b
----------------------------------------------------------------------
data _≡_ {A : Set} (x : A) : A → Set where
refl : x ≡ x
elimEq : {A : Set} {x : A} (P : (y : A) → x ≡ y → Set)
(prefl : P x refl)
(y : A) (q : x ≡ y) → P y q
elimEq P prefl x refl = prefl
----------------------------------------------------------------------
data List (A : Set) : Set where
[] : List A
_∷_ : (x : A) (xs : List A) → List A
elimList : {A : Set} (P : List A → Set)
(pnil : P [])
(pcons : (x : A) (xs : List A) → P xs → P (x ∷ xs))
(xs : List A) → P xs
elimList P pnil pcons [] = pnil
elimList P pnil pcons (x ∷ xs) = pcons x xs (elimList P pnil pcons xs)
----------------------------------------------------------------------
data Point (A : Set) : List A → Set where
here : ∀{x xs} → Point A (x ∷ xs)
there : ∀{x xs} → Point A xs → Point A (x ∷ xs)
elimPoint : {A : Set} (P : (xs : List A) → Point A xs → Set)
(phere : (x : A) (xs : List A) → P (x ∷ xs) here)
(pthere : (x : A) (xs : List A) (t : Point A xs) → P xs t → P (x ∷ xs) (there t))
(xs : List A) (t : Point A xs) → P xs t
elimPoint P phere pthere (x ∷ xs) here = phere x xs
elimPoint P phere pthere (x ∷ xs) (there t) = pthere x xs t (elimPoint P phere pthere xs t)
----------------------------------------------------------------------
data Tel : Set₁ where
End : Tel
Arg : (A : Set) (B : A → Tel) → Tel
elimTel : (P : Tel → Set)
(pend : P End)
(parg : (A : Set) (B : A → Tel) (pb : (a : A) → P (B a)) → P (Arg A B))
(T : Tel) → P T
elimTel P pend parg End = pend
elimTel P pend parg (Arg A B) = parg A B (λ a → elimTel P pend parg (B a))
----------------------------------------------------------------------
data Desc (I : Set) : Set₁ where
End : (i : I) → Desc I
Rec : (i : I) (D : Desc I) → Desc I
Arg : (A : Set) (B : A → Desc I) → Desc I
elimDesc : {I : Set} (P : Desc I → Set)
(pend : (i : I) → P (End i))
(prec : (i : I) (D : Desc I) (pd : P D) → P (Rec i D))
(parg : (A : Set) (B : A → Desc I) (pb : (a : A) → P (B a)) → P (Arg A B))
(D : Desc I) → P D
elimDesc P pend prec parg (End i) = pend i
elimDesc P pend prec parg (Rec i D) = prec i D (elimDesc P pend prec parg D)
elimDesc P pend prec parg (Arg A B) = parg A B (λ a → elimDesc P pend prec parg (B a))
Elᴰ : {I : Set} (D : Desc I) → (I → Set) → I → Set
Elᴰ (End j) X i = j ≡ i
Elᴰ (Rec j D) X i = Σ (X j) (λ _ → Elᴰ D X i)
Elᴰ (Arg A B) X i = Σ A (λ a → Elᴰ (B a) X i)
Hyps : {I : Set} (D : Desc I) (X : I → Set) (P : (i : I) → X i → Set) (i : I) (xs : Elᴰ D X i) → Set
Hyps (End j) X P i q = ⊤
Hyps (Rec j D) X P i (x , xs) = Σ (P j x) (λ _ → Hyps D X P i xs)
Hyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b
----------------------------------------------------------------------
data μ {I : Set} (D : Desc I) (i : I) : Set where
init : Elᴰ D (μ D) i → μ D i
ind : {I : Set} (D : Desc I)
(M : (i : I) → μ D i → Set)
(α : ∀ i (xs : Elᴰ D (μ D) i) (ihs : Hyps D (μ D) M i xs) → M i (init xs))
(i : I)
(x : μ D i)
→ M i x
prove : {I : Set} (D E : Desc I)
(M : (i : I) → μ E i → Set)
(α : ∀ i (xs : Elᴰ E (μ E) i) (ihs : Hyps E (μ E) M i xs) → M i (init xs))
(i : I) (xs : Elᴰ D (μ E) i) → Hyps D (μ E) M i xs
ind D M α i (init xs) = α i xs (prove D D M α i xs)
prove (End j) E M α i q = tt
prove (Rec j D) E M α i (x , xs) = ind E M α j x , prove D E M α i xs
prove (Arg A B) E M α i (a , xs) = prove (B a) E M α i xs
----------------------------------------------------------------------
|
module Spire.Examples.DarkwingDuck.Primitive where
----------------------------------------------------------------------
infixr 4 _,_
infixr 5 _∷_
----------------------------------------------------------------------
postulate String : Set
{-# BUILTIN STRING String #-}
----------------------------------------------------------------------
data ⊥ : Set where
elimBot : (P : ⊥ → Set)
(v : ⊥) → P v
elimBot P ()
----------------------------------------------------------------------
data ⊤ : Set where
tt : ⊤
elimUnit : (P : ⊤ → Set)
(ptt : P tt)
(u : ⊤) → P u
elimUnit P ptt tt = ptt
----------------------------------------------------------------------
data Σ (A : Set) (B : A → Set) : Set where
_,_ : (a : A) (b : B a) → Σ A B
elimPair : {A : Set} {B : A → Set}
(P : Σ A B → Set)
(ppair : (a : A) (b : B a) → P (a , b))
(ab : Σ A B) → P ab
elimPair P ppair (a , b) = ppair a b
----------------------------------------------------------------------
data _≡_ {A : Set} (x : A) : A → Set where
refl : x ≡ x
elimEq : {A : Set} {x : A} (P : (y : A) → x ≡ y → Set)
(prefl : P x refl)
(y : A) (q : x ≡ y) → P y q
elimEq P prefl x refl = prefl
----------------------------------------------------------------------
data List (A : Set) : Set where
[] : List A
_∷_ : (x : A) (xs : List A) → List A
elimList : {A : Set} (P : List A → Set)
(pnil : P [])
(pcons : (x : A) (xs : List A) → P xs → P (x ∷ xs))
(xs : List A) → P xs
elimList P pnil pcons [] = pnil
elimList P pnil pcons (x ∷ xs) = pcons x xs (elimList P pnil pcons xs)
----------------------------------------------------------------------
data Point (A : Set) : List A → Set where
here : ∀{x xs} → Point A (x ∷ xs)
there : ∀{x xs} → Point A xs → Point A (x ∷ xs)
elimPoint : {A : Set} (P : (xs : List A) → Point A xs → Set)
(phere : (x : A) (xs : List A) → P (x ∷ xs) here)
(pthere : (x : A) (xs : List A) (t : Point A xs) → P xs t → P (x ∷ xs) (there t))
(xs : List A) (t : Point A xs) → P xs t
elimPoint P phere pthere (x ∷ xs) here = phere x xs
elimPoint P phere pthere (x ∷ xs) (there t) = pthere x xs t (elimPoint P phere pthere xs t)
----------------------------------------------------------------------
data Tel : Set₁ where
End : Tel
Arg : (A : Set) (B : A → Tel) → Tel
elimTel : (P : Tel → Set)
(pend : P End)
(parg : (A : Set) (B : A → Tel) (pb : (a : A) → P (B a)) → P (Arg A B))
(T : Tel) → P T
elimTel P pend parg End = pend
elimTel P pend parg (Arg A B) = parg A B (λ a → elimTel P pend parg (B a))
----------------------------------------------------------------------
data Desc (I : Set) : Set₁ where
End : (i : I) → Desc I
Rec : (i : I) (D : Desc I) → Desc I
Arg : (A : Set) (B : A → Desc I) → Desc I
elimDesc : {I : Set} (P : Desc I → Set)
(pend : (i : I) → P (End i))
(prec : (i : I) (D : Desc I) (pd : P D) → P (Rec i D))
(parg : (A : Set) (B : A → Desc I) (pb : (a : A) → P (B a)) → P (Arg A B))
(D : Desc I) → P D
elimDesc P pend prec parg (End i) = pend i
elimDesc P pend prec parg (Rec i D) = prec i D (elimDesc P pend prec parg D)
elimDesc P pend prec parg (Arg A B) = parg A B (λ a → elimDesc P pend prec parg (B a))
Elᴰ : {I : Set} (D : Desc I) → (I → Set) → I → Set
Elᴰ (End j) X i = j ≡ i
Elᴰ (Rec j D) X i = Σ (X j) (λ _ → Elᴰ D X i)
Elᴰ (Arg A B) X i = Σ A (λ a → Elᴰ (B a) X i)
Hyps : {I : Set} (D : Desc I) (X : I → Set) (P : (i : I) → X i → Set) (i : I) (xs : Elᴰ D X i) → Set
Hyps (End j) X P i q = ⊤
Hyps (Rec j D) X P i (x , xs) = Σ (P j x) (λ _ → Hyps D X P i xs)
Hyps (Arg A B) X P i (a , b) = Hyps (B a) X P i b
----------------------------------------------------------------------
data μ {I : Set} (D : Desc I) (i : I) : Set where
init : Elᴰ D (μ D) i → μ D i
ind : {I : Set} (D : Desc I)
(M : (i : I) → μ D i → Set)
(α : ∀ i (xs : Elᴰ D (μ D) i) (ihs : Hyps D (μ D) M i xs) → M i (init xs))
(i : I)
(x : μ D i)
→ M i x
prove : {I : Set} (D E : Desc I)
(M : (i : I) → μ E i → Set)
(α : ∀ i (xs : Elᴰ E (μ E) i) (ihs : Hyps E (μ E) M i xs) → M i (init xs))
(i : I) (xs : Elᴰ D (μ E) i) → Hyps D (μ E) M i xs
ind D M α i (init xs) = α i xs (prove D D M α i xs)
prove (End j) E M α i q = tt
prove (Rec j D) E M α i (x , xs) = ind E M α j x , prove D E M α i xs
prove (Arg A B) E M α i (a , xs) = prove (B a) E M α i xs
----------------------------------------------------------------------
|
Add bot to primitives.
|
Add bot to primitives.
Consistency at last.
|
Agda
|
bsd-3-clause
|
spire/spire
|
6dbde1e52f3b14199a1e9ee7097d079561b3923e
|
Thesis/SIRelBigStep/FundamentalProperty.agda
|
Thesis/SIRelBigStep/FundamentalProperty.agda
|
module Thesis.SIRelBigStep.FundamentalProperty where
open import Data.Product
open import Relation.Binary.PropositionalEquality
open import Thesis.SIRelBigStep.IlcSILR
rfundamentalV3v : ∀ {Γ τ} (x : Var Γ τ) → (n : ℕ) → ∀ ρ1 dρ ρ2 (ρρ : rrelρ3 Γ ρ1 dρ ρ2 n) → rrelV3 τ (⟦ x ⟧Var ρ1) (D.⟦ x ⟧Var dρ) (⟦ x ⟧Var ρ2) n
rfundamentalV3v x n ρ1 dρ ρ2 ρρ = ⟦ x ⟧RelVar3 ρρ
rfundamental3constV : ∀ {τ} k (c : Const τ) →
rrelV3 τ (eval-const c) (deval (derive-const c) ∅ ∅) (eval-const c) k
rfundamental3constV k (lit n) = refl
rfundamental3 : ∀ {τ Γ} k (t : Term Γ τ) → ∀ ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
rrelT3 t (derive-dterm t) t ρ1 dρ ρ2 k
rfundamental3svv : ∀ {τ Γ} k (sv : SVal Γ τ) →
∀ ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) → rrelV3 τ (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) (eval sv ρ2) k
rfundamental3svv k (var x) ρ1 dρ ρ2 ρρ = rfundamentalV3v x k ρ1 dρ ρ2 ρρ
rfundamental3svv k (cons sv1 sv2) ρ1 dρ ρ2 ρρ = rfundamental3svv k sv1 ρ1 dρ ρ2 ρρ , rfundamental3svv k sv2 ρ1 dρ ρ2 ρρ
rfundamental3svv k (const c) ρ1 dρ ρ2 ρρ rewrite deval-derive-const-inv c ρ1 dρ = rfundamental3constV k c
rfundamental3svv k (abs t) ρ1 dρ ρ2 ρρ = (refl , refl) , refl , rrelρ3→⊕ ρ1 dρ ρ2 ρρ , refl , refl ,
λ k₁ k<n v1 dv v2 vv →
rfundamental3 k₁ t (v1 • ρ1) (dv • dρ) (v2 • ρ2) (vv , rrelρ3-mono k₁ k (lt1 k<n) _ _ _ _ ρρ)
rfundamental3sv : ∀ {τ Γ} k (sv : SVal Γ τ) →
∀ ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) → rrelT3 (val sv) (dval (derive-dsval sv)) (val sv) ρ1 dρ ρ2 k
rfundamental3sv k sv ρ1 dρ ρ2 ρρ .(eval sv ρ1) .(eval sv ρ2) .0 j<k (val .sv) (val .sv) = deval (derive-dsval sv) ρ1 dρ , dval (derive-dsval sv) , rfundamental3svv k sv ρ1 dρ ρ2 ρρ
open import Theorem.Groups-Nehemiah
rfundamental3primv : ∀ {σ τ} k p →
∀ v1 dv v2 → (vv : rrelV3 σ v1 dv v2 k) →
rrelV3 τ (eval-primitive p v1) (deval-primitive p v1 dv) (eval-primitive p v2) k
rfundamental3primv k succ (natV n₁) (bang .(natV n)) (natV n) refl = refl
rfundamental3primv k succ (natV n) (dnatV dn) (natV .(dn + n)) refl = +-suc dn n
rfundamental3primv k add (pairV (natV a1) (natV b1))
(dpairV (dnatV da) (dnatV db))
(pairV (natV .(da + a1)) (natV .(db + b1)))
(refl , refl) = ℕ-mn·pq=mp·nq {da} {db} {a1} {b1}
rfundamental3primv k add (pairV a1 b1)
(dpairV (dnatV da) (bang b2))
(pairV a2 .b2) (aa , refl) rewrite rrelV3→⊕ a1 (dnatV da) a2 aa = refl
rfundamental3primv k add (pairV a1 b1)
(dpairV (bang a2) db)
(pairV .a2 b2) (refl , bb) rewrite rrelV3→⊕ b1 db b2 bb = refl
rfundamental3primv k add (pairV a1 b1) (bang p2) .p2 refl = refl
-- Warning: names like ρ1⊢t1↓[j]v1 are all broken, sorry for not fixing them.
rfundamental3 k (val sv) = rfundamental3sv k sv
rfundamental3 (suc k) (primapp p sv) ρ1 dρ ρ2 ρρ
.(eval-primitive p (eval sv ρ1)) .(eval-primitive p (eval sv ρ2)) .1 (s≤s j<k) (primapp .p .sv) (primapp .p .sv) =
deval-primitive p (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) , dprimapp p sv (derive-dsval sv) ,
rfundamental3primv k p (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) (eval sv ρ2)
(rfundamental3svv k sv ρ1 dρ ρ2 (rrelρ3-mono k (suc k) (≤-step ≤-refl) _ ρ1 dρ ρ2 ρρ))
-- (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) (eval sv ρ2) k
rfundamental3 (suc (suc k)) (app vs vt) ρ1 dρ ρ2 ρρ v1 v2 .(suc j) (s≤s (s≤s j<k))
(app j vtv1 ρ1⊢t1↓[j]v1 ρ1⊢t1↓[j]v2 ρ1⊢t1↓[j]v3)
(app n₁ vtv2 ρ2⊢t2↓[n2]v2 ρ2⊢t2↓[n2]v3 ρ2⊢t2↓[n2]v4)
with rfundamental3 (suc (suc k)) (val vs) ρ1 dρ ρ2 ρρ _ _ zero (s≤s z≤n) ρ1⊢t1↓[j]v1 ρ2⊢t2↓[n2]v2
| rfundamental3 (suc (suc k)) (val vt) ρ1 dρ ρ2 ρρ _ _ zero (s≤s z≤n) ρ1⊢t1↓[j]v2 ρ2⊢t2↓[n2]v3
... | bang f2 , vs↓dsv , refl | dtv , vt↓dvv , dtvv
rewrite sym (rrelV3→⊕ vtv1 dtv vtv2 dtvv) =
bang v2 , bangapp vs↓dsv ρ1⊢t1↓[j]v2 vt↓dvv ρ2⊢t2↓[n2]v4 , refl
... | dclosure dt ρ dρ₁ , vs↓dsv , (refl , refl) , refl , refl , refl , refl , dsvv | dtv , vt↓dvv , dtvv
with dsvv (suc k) ≤-refl vtv1 dtv vtv2
(rrelV3-mono (suc k) (suc (suc k)) (s≤s (≤-step ≤-refl)) _ vtv1 dtv vtv2 dtvv)
v1 v2 j (s≤s j<k) ρ1⊢t1↓[j]v3 ρ2⊢t2↓[n2]v4
... | dv , ↓dv , dvv =
dv , dapp vs↓dsv ρ1⊢t1↓[j]v2 vt↓dvv ↓dv , dvv
rfundamental3 (suc (suc k)) (lett s t) ρ1 dρ ρ2 ρρ v1 v2 .(suc (n1 + n2)) (s≤s (s≤s n1+n2≤k))
(lett n1 n2 vs1 .s .t ρ1⊢t1↓[j]v1 ρ1⊢t1↓[j]v2) (lett _ _ vs2 .s .t ρ2⊢t2↓[n2]v2 ρ2⊢t2↓[n2]v3)
with rfundamental3 (suc (suc k)) s ρ1 dρ ρ2 ρρ vs1 vs2 n1
(s≤s (≤-trans (≤-trans (m≤m+n n1 n2) n1+n2≤k) (≤-step ≤-refl)))
ρ1⊢t1↓[j]v1 ρ2⊢t2↓[n2]v2
... | dsv , ↓dsv , vsv
with rfundamental3 (suc (suc k) ∸ n1) t (vs1 • ρ1) (dsv • dρ) (vs2 • ρ2) (vsv , rrelρ3-mono (suc (suc k) ∸ n1) (suc (suc k)) (m∸n≤m (suc (suc k)) n1) _ _ _ _ ρρ) v1 v2 n2 (sub∸ n1 (suc n2) (suc (suc k)) n1+[1+n2]≤2+k) ρ1⊢t1↓[j]v2 ρ2⊢t2↓[n2]v3
where
n1+[1+n2]≤2+k : n1 + suc n2 ≤ suc (suc k)
n1+[1+n2]≤2+k rewrite +-suc n1 n2 = ≤-step (s≤s n1+n2≤k)
... | dv , ↓dv , dvv = dv , dlett ρ1⊢t1↓[j]v1 ↓dsv ↓dv , rrelV3-mono (suc k ∸ (n1 + n2)) (suc (suc k) ∸ n1 ∸ n2) 1+k-[n1+n2]≤2+k-n1-n2 _ v1 dv v2 dvv
where
1+k-[n1+n2]≤2+k-n1-n2 : suc k ∸ (n1 + n2) ≤ suc (suc k) ∸ n1 ∸ n2
1+k-[n1+n2]≤2+k-n1-n2 rewrite ∸-+-assoc (suc (suc k)) n1 n2 = ∸-mono {suc k} {suc (suc k)} {n1 + n2} {n1 + n2} (≤-step ≤-refl) ≤-refl
|
module Thesis.SIRelBigStep.FundamentalProperty where
open import Data.Product
open import Relation.Binary.PropositionalEquality
open import Thesis.SIRelBigStep.IlcSILR
rfundamentalV3v : ∀ {Γ τ} (x : Var Γ τ) → (n : ℕ) → ∀ ρ1 dρ ρ2 (ρρ : rrelρ3 Γ ρ1 dρ ρ2 n) → rrelV3 τ (⟦ x ⟧Var ρ1) (D.⟦ x ⟧Var dρ) (⟦ x ⟧Var ρ2) n
rfundamentalV3v x n ρ1 dρ ρ2 ρρ = ⟦ x ⟧RelVar3 ρρ
rfundamental3constV : ∀ {τ} k (c : Const τ) →
rrelV3 τ (eval-const c) (deval (derive-const c) ∅ ∅) (eval-const c) k
rfundamental3constV k (lit n) = refl
rfundamental3 : ∀ {τ Γ} k (t : Term Γ τ) → ∀ ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
rrelT3 t (derive-dterm t) t ρ1 dρ ρ2 k
rfundamental3svv : ∀ {τ Γ} k (sv : SVal Γ τ) →
∀ ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) → rrelV3 τ (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) (eval sv ρ2) k
rfundamental3svv k (var x) ρ1 dρ ρ2 ρρ = rfundamentalV3v x k ρ1 dρ ρ2 ρρ
rfundamental3svv k (cons sv1 sv2) ρ1 dρ ρ2 ρρ = rfundamental3svv k sv1 ρ1 dρ ρ2 ρρ , rfundamental3svv k sv2 ρ1 dρ ρ2 ρρ
rfundamental3svv k (const c) ρ1 dρ ρ2 ρρ rewrite deval-derive-const-inv c ρ1 dρ = rfundamental3constV k c
rfundamental3svv k (abs t) ρ1 dρ ρ2 ρρ = (refl , refl) , refl , rrelρ3→⊕ ρ1 dρ ρ2 ρρ , refl , refl ,
λ j j<k v1 dv v2 vv →
rfundamental3 j t (v1 • ρ1) (dv • dρ) (v2 • ρ2) (vv , rrelρ3-mono j k (lt1 j<k) _ _ _ _ ρρ)
rfundamental3sv : ∀ {τ Γ} k (sv : SVal Γ τ) →
∀ ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) → rrelT3 (val sv) (dval (derive-dsval sv)) (val sv) ρ1 dρ ρ2 k
rfundamental3sv k sv ρ1 dρ ρ2 ρρ .(eval sv ρ1) .(eval sv ρ2) .0 j<k (val .sv) (val .sv) = deval (derive-dsval sv) ρ1 dρ , dval (derive-dsval sv) , rfundamental3svv k sv ρ1 dρ ρ2 ρρ
open import Theorem.Groups-Nehemiah
rfundamental3primv : ∀ {σ τ} k p →
∀ v1 dv v2 → (vv : rrelV3 σ v1 dv v2 k) →
rrelV3 τ (eval-primitive p v1) (deval-primitive p v1 dv) (eval-primitive p v2) k
rfundamental3primv k succ (natV n₁) (bang .(natV n)) (natV n) refl = refl
rfundamental3primv k succ (natV n) (dnatV dn) (natV .(dn + n)) refl = +-suc dn n
rfundamental3primv k add (pairV (natV a1) (natV b1))
(dpairV (dnatV da) (dnatV db))
(pairV (natV .(da + a1)) (natV .(db + b1)))
(refl , refl) = ℕ-mn·pq=mp·nq {da} {db} {a1} {b1}
rfundamental3primv k add (pairV a1 b1)
(dpairV (dnatV da) (bang b2))
(pairV a2 .b2) (aa , refl) rewrite rrelV3→⊕ a1 (dnatV da) a2 aa = refl
rfundamental3primv k add (pairV a1 b1)
(dpairV (bang a2) db)
(pairV .a2 b2) (refl , bb) rewrite rrelV3→⊕ b1 db b2 bb = refl
rfundamental3primv k add (pairV a1 b1) (bang p2) .p2 refl = refl
-- Warning: names like ρ1⊢t1↓[j]v1 are all broken, sorry for not fixing them.
rfundamental3 k (val sv) = rfundamental3sv k sv
rfundamental3 (suc k) (primapp p sv) ρ1 dρ ρ2 ρρ
.(eval-primitive p (eval sv ρ1)) .(eval-primitive p (eval sv ρ2)) .1 (s≤s j<k) (primapp .p .sv) (primapp .p .sv) =
deval-primitive p (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) , dprimapp p sv (derive-dsval sv) ,
rfundamental3primv k p (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) (eval sv ρ2)
(rfundamental3svv k sv ρ1 dρ ρ2 (rrelρ3-mono k (suc k) (≤-step ≤-refl) _ ρ1 dρ ρ2 ρρ))
-- (eval sv ρ1) (deval (derive-dsval sv) ρ1 dρ) (eval sv ρ2) k
rfundamental3 (suc (suc k)) (app vs vt) ρ1 dρ ρ2 ρρ v1 v2 .(suc j) (s≤s (s≤s j<k))
(app j vtv1 ρ1⊢t1↓[j]v1 ρ1⊢t1↓[j]v2 ρ1⊢t1↓[j]v3)
(app n₁ vtv2 ρ2⊢t2↓[n2]v2 ρ2⊢t2↓[n2]v3 ρ2⊢t2↓[n2]v4)
with rfundamental3 (suc (suc k)) (val vs) ρ1 dρ ρ2 ρρ _ _ zero (s≤s z≤n) ρ1⊢t1↓[j]v1 ρ2⊢t2↓[n2]v2
| rfundamental3 (suc (suc k)) (val vt) ρ1 dρ ρ2 ρρ _ _ zero (s≤s z≤n) ρ1⊢t1↓[j]v2 ρ2⊢t2↓[n2]v3
... | bang f2 , vs↓dsv , refl | dtv , vt↓dvv , dtvv
rewrite sym (rrelV3→⊕ vtv1 dtv vtv2 dtvv) =
bang v2 , bangapp vs↓dsv ρ1⊢t1↓[j]v2 vt↓dvv ρ2⊢t2↓[n2]v4 , refl
... | dclosure dt ρ dρ₁ , vs↓dsv , (refl , refl) , refl , refl , refl , refl , dsvv | dtv , vt↓dvv , dtvv
with dsvv (suc k) ≤-refl vtv1 dtv vtv2
(rrelV3-mono (suc k) (suc (suc k)) (s≤s (≤-step ≤-refl)) _ vtv1 dtv vtv2 dtvv)
v1 v2 j (s≤s j<k) ρ1⊢t1↓[j]v3 ρ2⊢t2↓[n2]v4
... | dv , ↓dv , dvv =
dv , dapp vs↓dsv ρ1⊢t1↓[j]v2 vt↓dvv ↓dv , dvv
rfundamental3 (suc (suc k)) (lett s t) ρ1 dρ ρ2 ρρ v1 v2 .(suc (n1 + n2)) (s≤s (s≤s n1+n2≤k))
(lett n1 n2 vs1 .s .t ρ1⊢t1↓[j]v1 ρ1⊢t1↓[j]v2) (lett _ _ vs2 .s .t ρ2⊢t2↓[n2]v2 ρ2⊢t2↓[n2]v3)
with rfundamental3 (suc (suc k)) s ρ1 dρ ρ2 ρρ vs1 vs2 n1
(s≤s (≤-trans (≤-trans (m≤m+n n1 n2) n1+n2≤k) (≤-step ≤-refl)))
ρ1⊢t1↓[j]v1 ρ2⊢t2↓[n2]v2
... | dsv , ↓dsv , vsv
with rfundamental3 (suc (suc k) ∸ n1) t (vs1 • ρ1) (dsv • dρ) (vs2 • ρ2) (vsv , rrelρ3-mono (suc (suc k) ∸ n1) (suc (suc k)) (m∸n≤m (suc (suc k)) n1) _ _ _ _ ρρ) v1 v2 n2 (sub∸ n1 (suc n2) (suc (suc k)) n1+[1+n2]≤2+k) ρ1⊢t1↓[j]v2 ρ2⊢t2↓[n2]v3
where
n1+[1+n2]≤2+k : n1 + suc n2 ≤ suc (suc k)
n1+[1+n2]≤2+k rewrite +-suc n1 n2 = ≤-step (s≤s n1+n2≤k)
... | dv , ↓dv , dvv = dv , dlett ρ1⊢t1↓[j]v1 ↓dsv ↓dv , rrelV3-mono (suc k ∸ (n1 + n2)) (suc (suc k) ∸ n1 ∸ n2) 1+k-[n1+n2]≤2+k-n1-n2 _ v1 dv v2 dvv
where
1+k-[n1+n2]≤2+k-n1-n2 : suc k ∸ (n1 + n2) ≤ suc (suc k) ∸ n1 ∸ n2
1+k-[n1+n2]≤2+k-n1-n2 rewrite ∸-+-assoc (suc (suc k)) n1 n2 = ∸-mono {suc k} {suc (suc k)} {n1 + n2} {n1 + n2} (≤-step ≤-refl) ≤-refl
|
Correct local variable name
|
Correct local variable name
|
Agda
|
mit
|
inc-lc/ilc-agda
|
fb73c4c68d0d9e00dac3fd35d9b93d3d01182522
|
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 Syntax.Context.Popl14 public
open import Data.Integer
open import Popl14.Syntax.Term public
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
-- Sugars for diff-term and apply-term
infixl 6 _⊕_ _⊝_
_⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ
_⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ)
t ⊕ Δt = app (app apply-term Δt) t
s ⊝ t = app (app diff-term s) t
apply-term {int} =
let Δx = var (that this)
x = var this
in abs (abs (add x Δx))
apply-term {bag} =
let Δx = var (that this)
x = var this
in abs (abs (union x Δx))
apply-term {σ ⇒ τ} =
let
Δf = var (that (that this))
f = var (that this)
x = var this
in
-- Δf f x
abs (abs (abs
(app f x ⊕ app (app Δf x) (x ⊝ x))))
diff-term {int} =
let x = var (that this)
y = var this
in abs (abs (add x (minus y)))
diff-term {bag} =
let x = var (that this)
y = var this
in abs (abs (union x (negate y)))
diff-term {σ ⇒ τ} =
let
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
in
-- g f x Δx
abs (abs (abs (abs
(app g (x ⊕ Δx) ⊝ app f x))))
|
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 Syntax.Context.Popl14 public
open import Data.Integer
open import Popl14.Syntax.Term public
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
-- Sugars for diff-term and apply-term
infixl 6 _⊕_ _⊝_
_⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ
_⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ)
t ⊕ Δt = app (app apply-term Δt) t
s ⊝ t = app (app diff-term s) t
apply-term {int} =
abs₂ (λ Δx x → add x Δx)
apply-term {bag} =
abs₂ (λ Δx x → union x Δx)
apply-term {σ ⇒ τ} =
let
Δf = var (that (that this))
f = var (that this)
x = var this
in
-- Δf f x
abs (abs (abs
(app f x ⊕ app (app Δf x) (x ⊝ x))))
diff-term {int} =
abs₂ (λ x y → add x (minus y))
diff-term {bag} =
abs₂ (λ x y → union x (negate y))
diff-term {σ ⇒ τ} =
let
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
in
-- g f x Δx
abs (abs (abs (abs
(app g (x ⊕ Δx) ⊝ app f x))))
|
Use HOAS-enabled helpers for nested abstractions.
|
Use HOAS-enabled helpers for nested abstractions.
Old-commit-hash: cffcfea69d7b1877dc5d63b49ee0ed9f24dce0d2
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9d1e02a049e0eac1e796dc1d74d47064871effb3
|
otp-sem-sec.agda
|
otp-sem-sec.agda
|
module otp-sem-sec where
import Level as L
open import Function
open import Data.Nat.NP
open import Data.Bits
open import Data.Bool
open import Data.Bool.Properties
open import Data.Vec hiding (_>>=_)
open import Data.Product.NP hiding (_⟦×⟧_)
open import Relation.Nullary
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
open import flipbased-implem
open ≡-Reasoning
open import Data.Unit using (⊤)
open import composable
open import vcomp
open import forkable
open import flat-funs
proj : ∀ {a} {A : Set a} → A × A → Bit → A
proj (x₀ , x₁) 0b = x₀
proj (x₀ , x₁) 1b = x₁
module BitsExtra where
splitAt′ : ∀ k {n} → Bits (k + n) → Bits k × Bits n
splitAt′ k xs = case splitAt k xs of λ { (ys , zs , _) → (ys , zs) }
vnot∘vnot : ∀ {n} → vnot {n} ∘ vnot ≗ id
vnot∘vnot [] = refl
vnot∘vnot (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot xs)
open BitsExtra
Coins = ℕ
record PrgDist : Set₁ where
constructor mk
field
_]-[_ : ∀ {c} (f g : ↺ c Bit) → Set
]-[-cong : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g'
breaks : ∀ {c} (EXP : Bit → ↺ c Bit) → Set
breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b
_≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ↺ c Bit) → Set
⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b
≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c})
≗⅁-trans p q b R = trans (p b R) (q b R)
-- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁
_⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ↺ c₀ Bit) (⅁₁ : Bit → ↺ c₁ Bit) → Set
⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁
extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ↺ c Bit}
→ ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁
extensional-reduction same-games = ]-[-cong (same-games 0b) (same-games 1b)
module Guess (Power : Set) (coins : Power → Coins) (prgDist : PrgDist) where
open PrgDist prgDist
GuessAdv : Coins → Set
GuessAdv c = ↺ c Bit
runGuess⅁ : ∀ {ca} (A : GuessAdv ca) (b : Bit) → ↺ ca Bit
runGuess⅁ A _ = A
-- An oracle: an adversary who can break the guessing game.
Oracle : Power → Set
Oracle power = ∃ (λ (A : GuessAdv (coins power)) → breaks (runGuess⅁ A))
-- Any adversary cannot do better than a random guess.
GuessSec : Power → Set
GuessSec power = ∀ (A : GuessAdv (coins power)) → ¬(breaks (runGuess⅁ A))
module AbsSemSec (|M| |C| : ℕ) {t} {T : Set t}
(♭Funs : FlatFuns T) where
open FlatFuns ♭Funs
M = `Bits |M|
C = `Bits |C|
record AbsSemSecAdv (|R| : Coins) : Set where
constructor mk
field
{|S|} : ℕ
S = `Bits |S|
R = `Bits |R|
field
step₀ : R `→ (M `× M) `× S
step₁ : C `× S `→ `Bit
-- step₀ : ⟨ p₀ ⟩ R ↝ (M `× M) `× S
-- step₁ : ⟨ p₁ ⟩ C `× S ↝ `Bit
SemSecReduction : ∀ (f : Coins → Coins) → Set
SemSecReduction f = ∀ {c} → AbsSemSecAdv c → AbsSemSecAdv (f c)
-- Here we use Agda functions for FlatFuns.
module FunSemSec (prgDist : PrgDist) (|M| |C| : ℕ) where
open PrgDist prgDist
open AbsSemSec |M| |C| fun♭Funs
open FlatFunsOps fun♭Ops
M² = Bit → M
Enc : ∀ cc → Set
Enc cc = M → ↺ cc C
Tr : (cc₀ cc₁ : Coins) → Set
Tr cc₀ cc₁ = Enc cc₀ → Enc cc₁
FunSemSecAdv : Coins → Set
FunSemSecAdv = AbsSemSecAdv
module FunSemSecAdv {|R|} (A : FunSemSecAdv |R|) where
open AbsSemSecAdv A public
step₀F : R → (M² × S)
step₀F = step₀ >>> proj *** idO
step₀↺ : ↺ |R| (M² × S)
step₀↺ = mk step₀F
step₁F : S → C → Bit
step₁F s c = step₁ (c , s)
-- Returing 0 means Chal wins, Adv looses
-- 1 means Adv wins, Chal looses
runSemSec : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit
runSemSec E A b
= A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) }
where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁)
_⇄_ : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit
_⇄_ = runSemSec
runAdv : ∀ {|R|} → FunSemSecAdv |R| → C → Bits |R| → (M × M) × Bit
runAdv (mk A-step₀ A-step₁) C = A-step₀ >>> id *** (const C &&& id >>> A-step₁)
_≗A_ : ∀ {p} (A₁ A₂ : FunSemSecAdv p) → Set
A₀ ≗A A₁ = ∀ C R → runAdv A₀ C R ≡ runAdv A₁ C R
change-adv : ∀ {cc ca} {E : Enc cc} {A₁ A₂ : FunSemSecAdv ca} → A₁ ≗A A₂ → (E ⇄ A₁) ≗⅁ (E ⇄ A₂)
change-adv {ca = ca} {A₁ = _} {_} pf b R with splitAt ca R
change-adv {E = E} {A₁} {A₂} pf b ._ | pre Σ., post , refl = trans (cong proj₂ (helper₀ A₁)) helper₂
where open FunSemSecAdv
helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post) pre
helper₂ = cong (λ m → step₁ A₂ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₂ pre)))
(helper₀ A₂)
SafeSemSecReduction : ∀ (f : Coins → Coins) {cc₀ cc₁} (E₀ : Enc cc₀) (E₁ : Enc cc₁) → Set
SafeSemSecReduction f E₀ E₁ =
∃ λ (red : SemSecReduction f) →
∀ {c} A → (E₀ ⇄ A) ⇓ (E₁ ⇄ red {c} A)
SemSecTr : ∀ {cc₀ cc₁} (f : Coins → Coins) (tr : Tr cc₀ cc₁) → Set
SemSecTr {cc₀} f tr = ∀ {E : Enc cc₀} → SafeSemSecReduction f (tr E) E
module PostCompSec (prgDist : PrgDist) (|M| |C| : ℕ) where
module PostCompRed {t} {T : Set t}
{♭Funs : FlatFuns T}
(♭ops : FlatFunsOps ♭Funs) where
open FlatFunsOps ♭ops
open AbsSemSec |M| |C| ♭Funs
post-comp-red : (post-E : C `→ C) → SemSecReduction _
post-comp-red post-E (mk A₀ A₁) = mk A₀ (post-E *** idO >>> A₁)
open PrgDist prgDist
open PostCompRed fun♭Ops
open FlatFunsOps fun♭Ops
open FunSemSec prgDist |M| |C|
open AbsSemSec |M| |C| fun♭Funs
post-comp : ∀ {cc} (post-E : C → C) → Tr cc cc
post-comp post-E E = E >>> map↺ post-E
post-comp-pres-sem-sec : ∀ {cc} (post-E : C → C)
→ SemSecTr id (post-comp {cc} post-E)
post-comp-pres-sem-sec post-E = post-comp-red post-E , (λ _ → id)
post-comp-pres-sem-sec' : ∀ (post-E post-E⁻¹ : C → C)
(post-E-inv : post-E⁻¹ ∘ post-E ≗ id)
{cc} {E : Enc cc}
→ SafeSemSecReduction id E (post-comp post-E E)
post-comp-pres-sem-sec' post-E post-E⁻¹ post-E-inv {cc} {E} = red , helper where
E' = post-comp post-E E
red : SemSecReduction id
red = post-comp-red post-E⁻¹
helper : ∀ {p} A → (E ⇄ A) ⇓ (E' ⇄ red {p} A)
helper {c} A A-breaks-E = A'-breaks-E'
where open FunSemSecAdv A renaming (step₀F to A₀F)
A' = red {c} A
same-games : (E ⇄ A) ≗⅁ (E' ⇄ A')
same-games b R
rewrite post-E-inv (run↺ (E (proj₁ (A₀F (take c R)) b))
(drop c R)) = refl
A'-breaks-E' : breaks (E' ⇄ A')
A'-breaks-E' = extensional-reduction same-games A-breaks-E
post-neg : ∀ {cc} → Tr cc cc
post-neg = post-comp vnot
post-neg-pres-sem-sec : ∀ {cc} → SemSecTr id (post-neg {cc})
post-neg-pres-sem-sec {cc} {E} = post-comp-pres-sem-sec vnot {E}
post-neg-pres-sem-sec' : ∀ {cc} {E : Enc cc}
→ SafeSemSecReduction id E (post-neg E)
post-neg-pres-sem-sec' {cc} {E} = post-comp-pres-sem-sec' vnot vnot vnot∘vnot {cc} {E}
open import diff
import Data.Fin as Fin
_]-_-[_ : ∀ {c} (f : ↺ c Bit) k (g : ↺ c Bit) → Set
_]-_-[_ {c} f k g = diff (Fin.toℕ #⟨ run↺ f ⟩) (Fin.toℕ #⟨ run↺ g ⟩) ≥ 2^(c ∸ k)
-- diff (#1 f) (#1 g) ≥ 2^(-k) * 2^ c
-- diff (#1 f) (#1 g) ≥ ε * 2^ c
-- dist (#1 f) (#1 g) ≥ ε * 2^ c
-- where ε = 2^ -k
-- {!dist (#1 f / 2^ c) (#1 g / 2^ c) > ε !}
open import Data.Vec.NP using (count)
ext-count : ∀ {n a} {A : Set a} {f g : A → Bool} → f ≗ g → (xs : Vec A n) → count f xs ≡ count g xs
ext-count f≗g [] = refl
ext-count f≗g (x ∷ xs) rewrite ext-count f≗g xs | f≗g x = refl
ext-# : ∀ {c} {f g : Bits c → Bit} → f ≗ g → #⟨ f ⟩ ≡ #⟨ g ⟩
ext-# f≗g = ext-count f≗g (allBits _)
]-[-cong : ∀ {k c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]- k -[ f' → g ]- k -[ g'
]-[-cong f≗g f'≗g' f]-[f' rewrite ext-# f≗g | ext-# f'≗g' = f]-[f'
module Concrete k where
_]-[_ : ∀ {c} (f g : ↺ c Bit) → Set
_]-[_ f g = f ]- k -[ g
cong' : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g'
cong' = ]-[-cong {k}
prgDist : PrgDist
prgDist = mk _]-[_ cong'
module Guess' = Guess Coins id prgDist
module FunSemSec' = FunSemSec prgDist
module PostCompSec' = PostCompSec prgDist
|
module otp-sem-sec where
import Level as L
open import Function
open import Data.Nat.NP
open import Data.Bits
open import Data.Bool
open import Data.Bool.Properties
open import Data.Vec hiding (_>>=_)
open import Data.Product.NP hiding (_⟦×⟧_)
open import Relation.Nullary
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
open import flipbased-implem
open ≡-Reasoning
open import Data.Unit using (⊤)
open import composable
open import vcomp
open import forkable
open import flat-funs
proj : ∀ {a} {A : Set a} → A × A → Bit → A
proj (x₀ , x₁) 0b = x₀
proj (x₀ , x₁) 1b = x₁
module BitsExtra where
splitAt′ : ∀ k {n} → Bits (k + n) → Bits k × Bits n
splitAt′ k xs = case splitAt k xs of λ { (ys , zs , _) → (ys , zs) }
vnot∘vnot : ∀ {n} → vnot {n} ∘ vnot ≗ id
vnot∘vnot [] = refl
vnot∘vnot (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot xs)
open BitsExtra
Coins = ℕ
record PrgDist : Set₁ where
constructor mk
field
_]-[_ : ∀ {c} (f g : ↺ c Bit) → Set
]-[-cong : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g'
breaks : ∀ {c} (EXP : Bit → ↺ c Bit) → Set
breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b
_≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ↺ c Bit) → Set
⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b
≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c})
≗⅁-trans p q b R = trans (p b R) (q b R)
-- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁
_⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ↺ c₀ Bit) (⅁₁ : Bit → ↺ c₁ Bit) → Set
⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁
extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ↺ c Bit}
→ ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁
extensional-reduction same-games = ]-[-cong (same-games 0b) (same-games 1b)
module Guess (prgDist : PrgDist) where
open PrgDist prgDist
GuessAdv : Coins → Set
GuessAdv c = ↺ c Bit
runGuess⅁ : ∀ {ca} (A : GuessAdv ca) (b : Bit) → ↺ ca Bit
runGuess⅁ A _ = A
-- An oracle: an adversary who can break the guessing game.
Oracle : Coins → Set
Oracle ca = ∃ (λ (A : GuessAdv ca) → breaks (runGuess⅁ A))
-- Any adversary cannot do better than a random guess.
GuessSec : Coins → Set
GuessSec ca = ∀ (A : GuessAdv ca) → ¬(breaks (runGuess⅁ A))
module AbsSemSec (|M| |C| : ℕ) {t} {T : Set t}
(♭Funs : FlatFuns T) where
open FlatFuns ♭Funs
M = `Bits |M|
C = `Bits |C|
record AbsSemSecAdv (|R| : Coins) : Set where
constructor mk
field
{|S|} : ℕ
S = `Bits |S|
R = `Bits |R|
field
step₀ : R `→ (M `× M) `× S
step₁ : C `× S `→ `Bit
-- step₀ : ⟨ p₀ ⟩ R ↝ (M `× M) `× S
-- step₁ : ⟨ p₁ ⟩ C `× S ↝ `Bit
SemSecReduction : ∀ (f : Coins → Coins) → Set
SemSecReduction f = ∀ {c} → AbsSemSecAdv c → AbsSemSecAdv (f c)
-- Here we use Agda functions for FlatFuns.
module FunSemSec (prgDist : PrgDist) (|M| |C| : ℕ) where
open PrgDist prgDist
open AbsSemSec |M| |C| fun♭Funs
open FlatFunsOps fun♭Ops
M² = Bit → M
Enc : ∀ cc → Set
Enc cc = M → ↺ cc C
Tr : (cc₀ cc₁ : Coins) → Set
Tr cc₀ cc₁ = Enc cc₀ → Enc cc₁
FunSemSecAdv : Coins → Set
FunSemSecAdv = AbsSemSecAdv
module FunSemSecAdv {|R|} (A : FunSemSecAdv |R|) where
open AbsSemSecAdv A public
step₀F : R → (M² × S)
step₀F = step₀ >>> proj *** idO
step₀↺ : ↺ |R| (M² × S)
step₀↺ = mk step₀F
step₁F : S → C → Bit
step₁F s c = step₁ (c , s)
-- Returing 0 means Chal wins, Adv looses
-- 1 means Adv wins, Chal looses
runSemSec : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit
runSemSec E A b
= A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) }
where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁)
_⇄_ : ∀ {cc ca} (E : Enc cc) (A : FunSemSecAdv ca) b → ↺ (ca + cc) Bit
_⇄_ = runSemSec
runAdv : ∀ {|R|} → FunSemSecAdv |R| → C → Bits |R| → (M × M) × Bit
runAdv (mk A-step₀ A-step₁) C = A-step₀ >>> id *** (const C &&& id >>> A-step₁)
_≗A_ : ∀ {p} (A₁ A₂ : FunSemSecAdv p) → Set
A₀ ≗A A₁ = ∀ C R → runAdv A₀ C R ≡ runAdv A₁ C R
change-adv : ∀ {cc ca} {E : Enc cc} {A₁ A₂ : FunSemSecAdv ca} → A₁ ≗A A₂ → (E ⇄ A₁) ≗⅁ (E ⇄ A₂)
change-adv {ca = ca} {A₁ = _} {_} pf b R with splitAt ca R
change-adv {E = E} {A₁} {A₂} pf b ._ | pre Σ., post , refl = trans (cong proj₂ (helper₀ A₁)) helper₂
where open FunSemSecAdv
helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post) pre
helper₂ = cong (λ m → step₁ A₂ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₂ pre)))
(helper₀ A₂)
SafeSemSecReduction : ∀ (f : Coins → Coins) {cc₀ cc₁} (E₀ : Enc cc₀) (E₁ : Enc cc₁) → Set
SafeSemSecReduction f E₀ E₁ =
∃ λ (red : SemSecReduction f) →
∀ {c} A → (E₀ ⇄ A) ⇓ (E₁ ⇄ red {c} A)
SemSecTr : ∀ {cc₀ cc₁} (f : Coins → Coins) (tr : Tr cc₀ cc₁) → Set
SemSecTr {cc₀} f tr = ∀ {E : Enc cc₀} → SafeSemSecReduction f (tr E) E
module PostCompSec (prgDist : PrgDist) (|M| |C| : ℕ) where
module PostCompRed {t} {T : Set t}
{♭Funs : FlatFuns T}
(♭ops : FlatFunsOps ♭Funs) where
open FlatFunsOps ♭ops
open AbsSemSec |M| |C| ♭Funs
post-comp-red : (post-E : C `→ C) → SemSecReduction _
post-comp-red post-E (mk A₀ A₁) = mk A₀ (post-E *** idO >>> A₁)
open PrgDist prgDist
open PostCompRed fun♭Ops
open FlatFunsOps fun♭Ops
open FunSemSec prgDist |M| |C|
open AbsSemSec |M| |C| fun♭Funs
post-comp : ∀ {cc} (post-E : C → C) → Tr cc cc
post-comp post-E E = E >>> map↺ post-E
post-comp-pres-sem-sec : ∀ {cc} (post-E : C → C)
→ SemSecTr id (post-comp {cc} post-E)
post-comp-pres-sem-sec post-E = post-comp-red post-E , (λ _ → id)
post-comp-pres-sem-sec' : ∀ (post-E post-E⁻¹ : C → C)
(post-E-inv : post-E⁻¹ ∘ post-E ≗ id)
{cc} {E : Enc cc}
→ SafeSemSecReduction id E (post-comp post-E E)
post-comp-pres-sem-sec' post-E post-E⁻¹ post-E-inv {cc} {E} = red , helper where
E' = post-comp post-E E
red : SemSecReduction id
red = post-comp-red post-E⁻¹
helper : ∀ {p} A → (E ⇄ A) ⇓ (E' ⇄ red {p} A)
helper {c} A A-breaks-E = A'-breaks-E'
where open FunSemSecAdv A renaming (step₀F to A₀F)
A' = red {c} A
same-games : (E ⇄ A) ≗⅁ (E' ⇄ A')
same-games b R
rewrite post-E-inv (run↺ (E (proj₁ (A₀F (take c R)) b))
(drop c R)) = refl
A'-breaks-E' : breaks (E' ⇄ A')
A'-breaks-E' = extensional-reduction same-games A-breaks-E
post-neg : ∀ {cc} → Tr cc cc
post-neg = post-comp vnot
post-neg-pres-sem-sec : ∀ {cc} → SemSecTr id (post-neg {cc})
post-neg-pres-sem-sec {cc} {E} = post-comp-pres-sem-sec vnot {E}
post-neg-pres-sem-sec' : ∀ {cc} {E : Enc cc}
→ SafeSemSecReduction id E (post-neg E)
post-neg-pres-sem-sec' {cc} {E} = post-comp-pres-sem-sec' vnot vnot vnot∘vnot {cc} {E}
open import diff
import Data.Fin as Fin
_]-_-[_ : ∀ {c} (f : ↺ c Bit) k (g : ↺ c Bit) → Set
_]-_-[_ {c} f k g = diff (Fin.toℕ #⟨ run↺ f ⟩) (Fin.toℕ #⟨ run↺ g ⟩) ≥ 2^(c ∸ k)
-- diff (#1 f) (#1 g) ≥ 2^(-k) * 2^ c
-- diff (#1 f) (#1 g) ≥ ε * 2^ c
-- dist (#1 f) (#1 g) ≥ ε * 2^ c
-- where ε = 2^ -k
-- {!dist (#1 f / 2^ c) (#1 g / 2^ c) > ε !}
open import Data.Vec.NP using (count)
ext-count : ∀ {n a} {A : Set a} {f g : A → Bool} → f ≗ g → (xs : Vec A n) → count f xs ≡ count g xs
ext-count f≗g [] = refl
ext-count f≗g (x ∷ xs) rewrite ext-count f≗g xs | f≗g x = refl
ext-# : ∀ {c} {f g : Bits c → Bit} → f ≗ g → #⟨ f ⟩ ≡ #⟨ g ⟩
ext-# f≗g = ext-count f≗g (allBits _)
]-[-cong : ∀ {k c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]- k -[ f' → g ]- k -[ g'
]-[-cong f≗g f'≗g' f]-[f' rewrite ext-# f≗g | ext-# f'≗g' = f]-[f'
module Concrete k where
_]-[_ : ∀ {c} (f g : ↺ c Bit) → Set
_]-[_ f g = f ]- k -[ g
cong' : ∀ {c} {f f' g g' : ↺ c Bit} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g'
cong' = ]-[-cong {k}
prgDist : PrgDist
prgDist = mk _]-[_ cong'
module Guess' = Guess prgDist
module FunSemSec' = FunSemSec prgDist
module PostCompSec' = PostCompSec prgDist
|
Remove power from the guessing game
|
Remove power from the guessing game
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
242a602d070c3965f3dedd346bff306ec822c452
|
lib/Algebra/Group/Homomorphism.agda
|
lib/Algebra/Group/Homomorphism.agda
|
{-# OPTIONS --without-K #-}
module Algebra.Group.Homomorphism where
open import Type using (Type_)
open import Function.NP using (Op₂; _∘_)
import Algebra.FunctionProperties.Eq
open Algebra.FunctionProperties.Eq.Implicits
open import Algebra.Monoid
open import Algebra.Monoid.Homomorphism
open import Algebra.Raw
open import Algebra.Group
open import Algebra.Group.Constructions
open import Level.NP
open import Data.Product.NP
open import Data.Nat.NP using (1+_)
open import Data.Integer.NP using (ℤ; -[1+_]; +_; -_; module ℤ°)
open import Relation.Binary.PropositionalEquality.NP
open ≡-Reasoning
record GroupHomomorphism {a}{A : Type a}{b}{B : Type b}
(grpA0+ : Group A)(grpB1* : Group B)
(f : A → B) : Type (a ⊔ b) where
constructor mk
open Additive-Group grpA0+
open Multiplicative-Group grpB1*
field
hom : Homomorphic₂ f _+_ _*_
pres-unit : f 0# ≡ 1#
pres-unit = unique-1-left part
where part = f 0# * f 0# ≡⟨ ! hom ⟩
f (0# + 0#) ≡⟨ ap f (fst +-identity) ⟩
f 0# ∎
mon-hom : MonoidHomomorphism +-mon *-mon f
mon-hom = pres-unit , hom
open MonoidHomomorphism mon-hom public
pres-inv : ∀ {x} → f (0− x) ≡ (f x)⁻¹
pres-inv {x} = unique-⁻¹ part
where part = f (0− x) * f x ≡⟨ ! hom ⟩
f (0− x + x) ≡⟨ ap f (fst 0−-inverse) ⟩
f 0# ≡⟨ pres-unit ⟩
1# ∎
0−-⁻¹ = pres-inv
−-/ : ∀ {x y} → f (x − y) ≡ f x / f y
−-/ {x} {y} = f (x − y) ≡⟨ hom ⟩
f x * f (0− y) ≡⟨ ap (_*_ (f x)) pres-inv ⟩
f x / f y ∎
hom-iterated⁻ : ∀ {x} n → f (x ⊗⁻ n) ≡ f x ^⁻ n
hom-iterated⁻ {x} n =
f (x ⊗⁻ n) ≡⟨by-definition⟩
f (0−(x ⊗⁺ n)) ≡⟨ pres-inv ⟩
f(x ⊗⁺ n)⁻¹ ≡⟨ ap _⁻¹ (hom-iterated⁺ n) ⟩
(f x ^⁺ n)⁻¹ ≡⟨by-definition⟩
f x ^⁻ n ∎
hom-iterated : ∀ {x} i → f (x ⊗ i) ≡ f x ^ i
hom-iterated -[1+ n ] = hom-iterated⁻ (1+ n)
hom-iterated (+ n) = hom-iterated⁺ n
ℤ+-grp-ops : Group-Ops ℤ
ℤ+-grp-ops = ℤ+-mon-ops , -_
ℤ+-grp-struct : Group-Struct ℤ+-grp-ops
ℤ+-grp-struct = ℤ+-mon-struct
, (λ{x} → fst ℤ°.-‿inverse x)
, (λ{x} → snd ℤ°.-‿inverse x)
ℤ+-grp : Group ℤ
ℤ+-grp = _ , ℤ+-grp-struct
module ℤ+ = Additive-Group ℤ+-grp
module _ {ℓ}{G : Type ℓ}(𝔾 : Group G) where
open Groupᵒᵖ
open Group 𝔾
module ⁻¹-Hom where
-- The proper type for ⁻¹-hom′
⁻¹-hom' : GroupHomomorphism 𝔾 (𝔾 ᵒᵖ) _⁻¹
⁻¹-hom' = mk ⁻¹-hom′
open GroupHomomorphism ⁻¹-hom' public
module ℤ+-^-Hom {b} where
^-+-hom : GroupHomomorphism ℤ+-grp 𝔾 (_^_ b)
^-+-hom = mk (λ {i} {j} → ^-+ i j)
open GroupHomomorphism ^-+-hom public
module Stability-Minimal
{a}{A : Type a}
{b}{B : Type b}
(φ : A → B)
(_+_ : Op₂ A)
(_*_ : Op₂ B)
(φ-+-* : ∀ {x y} → φ (x + y) ≡ φ x * φ y)
{c}{C : Type c}
(F : (A → B) → C)
(F= : ∀ {f g : A → B} → f ≗ g → F f ≡ F g)
(Fφ* : ∀ {k} → F φ ≡ F (_*_ k ∘ φ))
where
+-stable : ∀ {k} → F φ ≡ F (φ ∘ _+_ k)
+-stable {k} =
F φ ≡⟨ Fφ* ⟩
F (_*_ (φ k) ∘ φ) ≡⟨ F= (λ x → ! φ-+-*) ⟩
F (φ ∘ _+_ k) ∎
module Stability
{a}{A : Type a}
{b}{B : Type b}
(G+ : Group A)
(G* : Group B)
(φ : A → B)
(φ-hom : GroupHomomorphism G+ G* φ)
where
open Additive-Group G+
open Multiplicative-Group G*
open GroupHomomorphism φ-hom
open Stability-Minimal φ _+_ _*_ hom public
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
module Algebra.Group.Homomorphism where
open import Type using (Type_)
open import Function.NP using (Op₂; _∘_; id)
import Algebra.FunctionProperties.Eq
open Algebra.FunctionProperties.Eq.Implicits
open import Algebra.Monoid
open import Algebra.Monoid.Homomorphism
open import Algebra.Raw
open import Algebra.Group
open import Algebra.Group.Constructions
open import Level.NP
open import Data.Product.NP
open import Data.Nat.NP using (1+_)
open import Data.Integer.NP using (ℤ; -[1+_]; +_; -_; module ℤ°)
open import Relation.Binary.PropositionalEquality.NP
open ≡-Reasoning
record GroupHomomorphism {a}{A : Type a}{b}{B : Type b}
(grpA0+ : Group A)(grpB1* : Group B)
(f : A → B) : Type (a ⊔ b) where
constructor mk
open Additive-Group grpA0+
open Multiplicative-Group grpB1*
field
hom : Homomorphic₂ f _+_ _*_
pres-unit : f 0# ≡ 1#
pres-unit = unique-1-left part
where part = f 0# * f 0# ≡⟨ ! hom ⟩
f (0# + 0#) ≡⟨ ap f (fst +-identity) ⟩
f 0# ∎
mon-hom : MonoidHomomorphism +-mon *-mon f
mon-hom = pres-unit , hom
open MonoidHomomorphism mon-hom public
pres-inv : ∀ {x} → f (0− x) ≡ (f x)⁻¹
pres-inv {x} = unique-⁻¹ part
where part = f (0− x) * f x ≡⟨ ! hom ⟩
f (0− x + x) ≡⟨ ap f (fst 0−-inverse) ⟩
f 0# ≡⟨ pres-unit ⟩
1# ∎
0−-⁻¹ = pres-inv
−-/ : ∀ {x y} → f (x − y) ≡ f x / f y
−-/ {x} {y} = f (x − y) ≡⟨ hom ⟩
f x * f (0− y) ≡⟨ ap (_*_ (f x)) pres-inv ⟩
f x / f y ∎
hom-iterated⁻ : ∀ {x} n → f (x ⊗⁻ n) ≡ f x ^⁻ n
hom-iterated⁻ {x} n =
f (x ⊗⁻ n) ≡⟨by-definition⟩
f (0−(x ⊗⁺ n)) ≡⟨ pres-inv ⟩
f(x ⊗⁺ n)⁻¹ ≡⟨ ap _⁻¹ (hom-iterated⁺ n) ⟩
(f x ^⁺ n)⁻¹ ≡⟨by-definition⟩
f x ^⁻ n ∎
hom-iterated : ∀ {x} i → f (x ⊗ i) ≡ f x ^ i
hom-iterated -[1+ n ] = hom-iterated⁻ (1+ n)
hom-iterated (+ n) = hom-iterated⁺ n
ℤ+-grp-ops : Group-Ops ℤ
ℤ+-grp-ops = ℤ+-mon-ops , -_
ℤ+-grp-struct : Group-Struct ℤ+-grp-ops
ℤ+-grp-struct = ℤ+-mon-struct
, (λ{x} → fst ℤ°.-‿inverse x)
, (λ{x} → snd ℤ°.-‿inverse x)
ℤ+-grp : Group ℤ
ℤ+-grp = _ , ℤ+-grp-struct
module ℤ+ = Additive-Group ℤ+-grp
module _ {ℓ}{G : Type ℓ}(𝔾 : Group G) where
open Groupᵒᵖ
open Group 𝔾
module ⁻¹-Hom where
-- The proper type for ⁻¹-hom′
⁻¹-hom' : GroupHomomorphism 𝔾 (𝔾 ᵒᵖ) _⁻¹
⁻¹-hom' = mk ⁻¹-hom′
open GroupHomomorphism ⁻¹-hom' public
module ℤ+-^-Hom {b} where
^-+-hom : GroupHomomorphism ℤ+-grp 𝔾 (_^_ b)
^-+-hom = mk (λ {i} {j} → ^-+ i j)
open GroupHomomorphism ^-+-hom public
module Stability-Minimal
{a}{A : Type a}
{b}{B : Type b}
(φ : A → B)
(_+_ : Op₂ A)
(_*_ : Op₂ B)
(φ-+-* : ∀ {x y} → φ (x + y) ≡ φ x * φ y)
{c}{C : Type c}
(F : (A → B) → C)
(F= : ∀ {f g : A → B} → f ≗ g → F f ≡ F g)
(Fφ* : ∀ {k} → F φ ≡ F (_*_ k ∘ φ))
where
+-stable : ∀ {k} → F φ ≡ F (φ ∘ _+_ k)
+-stable {k} =
F φ ≡⟨ Fφ* ⟩
F (_*_ (φ k) ∘ φ) ≡⟨ F= (λ x → ! φ-+-*) ⟩
F (φ ∘ _+_ k) ∎
module Stability
{a}{A : Type a}
{b}{B : Type b}
(G+ : Group A)
(G* : Group B)
(φ : A → B)
(φ-hom : GroupHomomorphism G+ G* φ)
where
open Additive-Group G+
open Multiplicative-Group G*
open GroupHomomorphism φ-hom
open Stability-Minimal φ _+_ _*_ hom public
open GroupHomomorphism
module Identity
{a}{A : Type a}
(𝔸 : Group A)
where
id-hom : GroupHomomorphism 𝔸 𝔸 id
id-hom = mk refl
module Compose
{a}{A : Type a}
{b}{B : Type b}
{c}{C : Type c}
(𝔸 : Group A)
(𝔹 : Group B)
(ℂ : Group C)
(ψ : A → B)
(ψ-hom : GroupHomomorphism 𝔸 𝔹 ψ)
(φ : B → C)
(φ-hom : GroupHomomorphism 𝔹 ℂ φ)
where
∘-hom : GroupHomomorphism 𝔸 ℂ (φ ∘ ψ)
∘-hom = mk (ap φ (hom ψ-hom) ∙ hom φ-hom)
module Delta
{a}{A : Type a}
(𝔸 : Group A)
where
open Algebra.Group.Constructions.Product
Δ-hom : GroupHomomorphism 𝔸 (×-grp 𝔸 𝔸) (λ x → x , x)
Δ-hom = mk refl
module Zip
{a₀}{A₀ : Type a₀}
{a₁}{A₁ : Type a₁}
{b₀}{B₀ : Type b₀}
{b₁}{B₁ : Type b₁}
(𝔸₀ : Group A₀)
(𝔸₁ : Group A₁)
(𝔹₀ : Group B₀)
(𝔹₁ : Group B₁)
(φ₀ : A₀ → B₀)
(φ₀-hom : GroupHomomorphism 𝔸₀ 𝔹₀ φ₀)
(φ₁ : A₁ → B₁)
(φ₁-hom : GroupHomomorphism 𝔸₁ 𝔹₁ φ₁)
where
open Algebra.Group.Constructions.Product
zip-hom : GroupHomomorphism (×-grp 𝔸₀ 𝔸₁) (×-grp 𝔹₀ 𝔹₁) (map φ₀ φ₁)
zip-hom = mk (ap₂ _,_ (hom φ₀-hom) (hom φ₁-hom))
module Pair
{a}{A : Type a}
{b₀}{B₀ : Type b₀}
{b₁}{B₁ : Type b₁}
(𝔸 : Group A)
(𝔹₀ : Group B₀)
(𝔹₁ : Group B₁)
(φ₀ : A → B₀)
(φ₀-hom : GroupHomomorphism 𝔸 𝔹₀ φ₀)
(φ₁ : A → B₁)
(φ₁-hom : GroupHomomorphism 𝔸 𝔹₁ φ₁)
where
-- pair = zip ∘ Δ
pair-hom : GroupHomomorphism 𝔸 (Product.×-grp 𝔹₀ 𝔹₁) < φ₀ , φ₁ >
pair-hom = Compose.∘-hom _ _ _
_ (Delta.Δ-hom 𝔸)
_ (Zip.zip-hom _ _ _ _ _ φ₀-hom _ φ₁-hom)
-- OR:
pair-hom = mk (ap₂ _,_ (hom φ₀-hom) (hom φ₁-hom))
-- -}
-- -}
-- -}
-- -}
|
add id, ∘, Δ, zip, pair
|
Group.Homomorphism: add id, ∘, Δ, zip, pair
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
39d9e13ae25672af17181a0c3d1fee2118ac33c9
|
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)
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)
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Denotation.Environment Type
open import Syntax.Context.Plotkin B
-- Declarations of Term and Terms to enable mutual recursion
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
-- (Term Γ τ) represents a term of type τ
-- with free variables bound in Γ.
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- (Terms Γ Σ) represents a list of terms with types from Σ
-- with free variables bound in Γ.
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const constant)
|
Introduce and use UncurriedTermConstructor.
|
Introduce and use UncurriedTermConstructor.
Old-commit-hash: d36f33b097ddb2444e6409de52048ef9bf94b001
|
Agda
|
mit
|
inc-lc/ilc-agda
|
81c758c5545a825b9152e8e2ab898c688bf0d4a9
|
Solver/Linear/Parser.agda
|
Solver/Linear/Parser.agda
|
open import Data.Vec as Vec using ([]; _∷_)
open import Text.Parser
open import Data.One
open import Data.Char
open import Data.Nat
open import Data.List
open import Data.Product hiding (map)
open import Data.String as String renaming (toList to S▹L; fromList to L▹S)
open import Solver.Linear.Syntax
open import Data.Maybe.NP hiding (Eq)
open import Relation.Binary.PropositionalEquality
module Solver.Linear.Parser where
spaces : Parser 𝟙
spaces = manyOneOfˢ " \t\n\r" *> pure _
pIdent : Parser String
pIdent = someNoneOfˢ "(,)↦_ \t\n\r" <* spaces
tokᶜ : Char → Parser 𝟙
tokᶜ c = char c <* spaces
tokˢ : String → Parser 𝟙
tokˢ s = string s <* spaces
--pPair : ∀ {A B} → Parser A → Parser B → Parser (A × B)
--pPair pA pB = tokᶜ '(' *> ⟪ _,_ · pA <* tokᶜ ',' · pB ⟫ <* tokᶜ ')'
module _ {A} (pA : Parser A) where
pSimpleSyn pSyn : ℕ → Parser (Syn A)
pSimpleSyn n = pure tt <* oneOfˢ "_𝟙"
⟨|⟩ tokᶜ '(' *> pSyn n <* tokᶜ ')'
⟨|⟩ ⟪ var · pA ⟫
pSyn zero = empty
pSyn (suc n) = ⟪ tuple _ · pSimpleSyn n · many n (tokᶜ ',' *> pSyn n) ⟫
pEq : ℕ → Parser (Eq A)
pEq n = ⟪ _↦_ · pSyn n <* tokᶜ '↦' · pSyn n ⟫
pSynˢ : ℕ → Parser (Syn String)
pSynˢ = pSyn pIdent
pEqˢ : ℕ → Parser (Eq String)
pEqˢ = pEq pIdent
parseSynˢ? : String →? Syn String
parseSynˢ? s = runParser (pSynˢ n <* eof) ℓ
where ℓ = S▹L s
n = length ℓ
parseSynˢ : T[ parseSynˢ? ]
parseSynˢ = F[ parseSynˢ? ]
parseEqˢ? : String →? Eq String
parseEqˢ? s = runParser (spaces *> pEqˢ (2 * n) <* eof) ℓ
where ℓ = S▹L s
n = length ℓ
parseEqˢ : T[ parseEqˢ? ]
parseEqˢ = F[ parseEqˢ? ]
ex-A⇒B : Eq String
ex-A⇒B = parseEqˢ "A↦B"
ex-𝟙⇒𝟙 : Eq String
ex-𝟙⇒𝟙 = parseEqˢ "𝟙↦𝟙"
ex₁ : Eq String
ex₁ = parseEqˢ " ( A , B,(𝟙,C),(D))↦ B,C , A"
test-ex₁ : ex₁ ≡ ((var"A" , (var"B", (tt , var"C") , var"D"))
↦ (var"B" , var"C" , var"A"))
test-ex₁ = refl
-- -}
|
open import Data.Vec as Vec using ([]; _∷_)
open import Text.Parser
open import Data.One
open import Data.Char
open import Data.Nat
open import Data.List
open import Data.Product hiding (map)
open import Data.String as String renaming (toList to S▹L; fromList to L▹S)
open import Solver.Linear.Syntax
open import Data.Maybe.NP hiding (Eq)
open import Relation.Binary.PropositionalEquality
module Solver.Linear.Parser where
spaces : Parser 𝟙
spaces = manyOneOfˢ " \t\n\r" *> pure _
pIdent : Parser String
pIdent = someNoneOfˢ "(,)↦_ \t\n\r" <* spaces
tokᶜ : Char → Parser 𝟙
tokᶜ c = char c <* spaces
tokˢ : String → Parser 𝟙
tokˢ s = string s <* spaces
--pPair : ∀ {A B} → Parser A → Parser B → Parser (A × B)
--pPair pA pB = tokᶜ '(' *> ⟪ _,_ · pA <* tokᶜ ',' · pB ⟫ <* tokᶜ ')'
module _ {A} (pA : Parser A) where
pSimpleSyn pSyn : ℕ → Parser (Syn A)
pSimpleSyn n = pure tt <* oneOfˢ "_𝟙"
⟨|⟩ tokᶜ '(' *> pSyn n <* tokᶜ ')'
⟨|⟩ ⟪ var · pA ⟫
pSyn zero = empty
pSyn (suc n) = ⟪ tuple _ · pSimpleSyn n · many n (tokᶜ ',' *> pSyn n) ⟫
pEq : ℕ → Parser (Eq A)
pEq n = ⟪ _↦_ · pSyn n <* tokᶜ '↦' · pSyn n ⟫
pSynˢ : ℕ → Parser (Syn String)
pSynˢ = pSyn pIdent
pEqˢ : ℕ → Parser (Eq String)
pEqˢ = pEq pIdent
parseSynˢ? : String →? Syn String
parseSynˢ? s = runParser (pSynˢ n <* eof) ℓ
where ℓ = S▹L s
n = length ℓ
parseSynˢ : T[ parseSynˢ? ]
parseSynˢ = F[ parseSynˢ? ]
parseEqˢ? : String →? Eq String
parseEqˢ? s = runParser (spaces *> pEqˢ (2 * n) <* eof) ℓ
where ℓ = S▹L s
n = length ℓ
parseEqˢ : T[ parseEqˢ? ]
parseEqˢ = F[ parseEqˢ? ]
ex-A⇒B : Eq String
ex-A⇒B = parseEqˢ "A↦B"
ex-𝟙⇒𝟙 : Eq String
ex-𝟙⇒𝟙 = parseEqˢ "𝟙↦𝟙"
ex₁ : Eq String
ex₁ = parseEqˢ " ( A , B,(𝟙,C),(D))↦ B,C , A"
{-
test-ex₁ : ex₁ ≡ ((var"A" , (var"B", (tt , var"C") , var"D"))
↦ (var"B" , var"C" , var"A"))
test-ex₁ = refl
-}
-- -}
|
comment out the example (long to check)
|
Solver.Linear.Parser: comment out the example (long to check)
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
32b4e15a9837ed6e6276af1fd200e4a3dffc2223
|
flipbased.agda
|
flipbased.agda
|
module flipbased where
open import Algebra
import Level as L
open L using () renaming (_⊔_ to _L⊔_)
open import Function hiding (_⟨_⟩_)
open import Data.Nat.NP
open import Data.Bool
open import Data.Nat.Properties
open import Data.Product using (proj₁; proj₂; _,_; swap; _×_)
open import Data.Bits hiding (replicateM)
open import Data.Bool
open import Data.Vec using (Vec; []; _∷_; take; drop; head; tail)
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_)
record M {a} n (A : Set a) : Set a where
constructor mk
field
run : Bits n → A
toss′ : M 1 Bit
toss′ = mk head
return′ : ∀ {a} {A : Set a} → A → M 0 A
return′ = mk ∘ const
pure′ : ∀ {a} {A : Set a} → A → M 0 A
pure′ = return′
comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → M m A → M n A
comap f (mk g) = mk (g ∘ f)
weaken : ∀ {m n a} {A : Set a} → M n A → M (m + n) A
weaken {m} = comap (drop m)
weaken′ : ∀ {m n a} {A : Set a} → M n A → M (n + m) A
weaken′ = comap (take _)
private
take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n
take≤ z≤n _ = []
take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs
weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → M m A → M n A
weaken≤ p = comap (take≤ p)
coerce : ∀ {m n a} {A : Set a} → m ≡ n → M m A → M n A
coerce ≡.refl = id
toss : ∀ {n} → M (1 + n) Bit
toss = weaken′ toss′
return : ∀ {n a} {A : Set a} → A → M n A
return = weaken′ ∘ return′
pure : ∀ {n a} {A : Set a} → A → M n A
pure = return
_>>=_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₁ + n₂) B
_>>=_ {n₁} x f = mk (λ bs → M.run (f (M.run x (take _ bs))) (drop n₁ bs))
_>>=′_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₂ + n₁) B
_>>=′_ {n₁} {n₂} rewrite ℕ°.+-comm n₂ n₁ = _>>=_
_>>_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → M n₂ B → M (n₁ + n₂) B
_>>_ {n₁} x y = x >>= const y
map : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → M n A → M n B
map f x = mk (f ∘ M.run x)
-- map f x ≗ x >>=′ (return {0} ∘ f)
join : ∀ {n₁ n₂ a} {A : Set a} →
M n₁ (M n₂ A) → M (n₁ + n₂) A
join x = x >>= id
infixl 4 _⊛_
_⊛_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ (A → B) → M n₂ A → M (n₁ + n₂) B
_⊛_ {n₁} mf mx = mf >>= λ f → map (_$_ f) mx
_⟨_⟩_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
M m A → (A → B → C) → M n B → M (m + n) C
x ⟨ f ⟩ y = map f x ⊛ y
⟪_⟫ : ∀ {n} {a} {A : Set a} → A → M n A
⟪_⟫ = pure
⟪_⟫′ : ∀ {a} {A : Set a} → A → M 0 A
⟪_⟫′ = pure′
⟪_·_⟫ : ∀ {a b} {A : Set a} {B : Set b} {n} → (A → B) → M n A → M n B
⟪ f · x ⟫ = map f x
⟪_·_·_⟫ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
(A → B → C) → M m A → M n B → M (m + n) C
⟪ f · x · y ⟫ = map f x ⊛ y
⟪_·_·_·_⟫ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} →
(A → B → C → D) → M m A → M n B → M o C → M (m + n + o) D
⟪ f · x · y · z ⟫ = map f x ⊛ y ⊛ z
choose : ∀ {n a} {A : Set a} → M n A → M n A → M (suc n) A
choose x y = ⟪ if_then_else_ · toss · x · y ⟫
_⟨,⟩_ : ∀ {a b} {A : Set a} {B : Set b} {m n} → M m A → M n B → M (m + n) (A × B)
x ⟨,⟩ y = ⟪ _,_ · x · y ⟫
_⟨xor⟩_ : ∀ {n₁ n₂} → M n₁ Bit → M n₂ Bit → M (n₁ + n₂) Bit
x ⟨xor⟩ y = ⟪ _xor_ · x · y ⟫
_⟨⊕⟩_ : ∀ {n₁ n₂ m} → M n₁ (Bits m) → M n₂ (Bits m) → M (n₁ + n₂) (Bits m)
x ⟨⊕⟩ y = ⟪ _⊕_ · x · y ⟫
replicateM : ∀ {n m} {a} {A : Set a} → M m A → M (n * m) (Vec A n)
replicateM {zero} _ = ⟪ [] ⟫
replicateM {suc _} x = ⟪ _∷_ · x · replicateM x ⟫
random : ∀ {n} → M n (Bits n)
-- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce
random {zero} = ⟪ [] ⟫
random {suc _} = ⟪ _∷_ · toss′ · random ⟫
randomTbl : ∀ m n → M (2 ^ m * n) (Vec (Bits n) (2 ^ m))
randomTbl m n = replicateM random
randomFun : ∀ m n → M (2 ^ m * n) (Bits m → Bits n)
randomFun m n = ⟪ funFromTbl · randomTbl m n ⟫
randomFunExt : ∀ {n k a} {A : Set a} → M k (Bits n → A) → M (k + k) (Bits (suc n) → A)
randomFunExt f = ⟪ comb · f · f ⟫ where comb = λ g₁ g₂ xs → (if head xs then g₁ else g₂) (tail xs)
2*_ : ℕ → ℕ
2* x = x + x
2^_ : ℕ → ℕ
2^ 0 = 1
2^ (suc n) = 2* (2^ n)
costRndFun : ℕ → ℕ → ℕ
costRndFun zero n = n
costRndFun (suc m) n = 2* (costRndFun m n)
lem : ∀ m n → costRndFun m n ≡ 2 ^ m * n
lem zero n rewrite ℕ°.+-comm n 0 = ≡.refl
lem (suc m) n rewrite lem m n | ℕ°.*-assoc 2 (2 ^ m) n | ℕ°.+-comm (2 ^ m * n) 0 = ≡.refl
randomFun′ : ∀ {m n} → M (costRndFun m n) (Bits m → Bits n)
randomFun′ {zero} = ⟪ const · random ⟫
randomFun′ {suc m} = randomFunExt (randomFun′ {m})
record ProgEquiv a ℓ : Set (L.suc ℓ L⊔ L.suc a) where
infix 2 _≈_ _≋_
field
_≈_ : ∀ {n} {A : Set a} → Rel (M n A) ℓ
refl : ∀ {n A} → Reflexive {A = M n A} _≈_
sym : ∀ {n A} → Symmetric {A = M n A} _≈_
-- not strictly transitive
reflexive : ∀ {n A} → _≡_ ⇒ _≈_ {n} {A}
reflexive ≡.refl = refl
_≋_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_≋_ {n₁} {n₂} p₁ p₂ = _≈_ {n = n₁ ⊔ n₂} (weaken≤ (m≤m⊔n _ _) p₁) (weaken≤ (m≤n⊔m _ n₁) p₂)
where m≤n⊔m : ∀ m n → m ≤ n ⊔ m
m≤n⊔m m n rewrite ⊔°.+-comm n m = m≤m⊔n m n
-- Another name for _≋_
_looks_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_looks_ = _≋_
module WithEquiv (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
SecPRG : ∀ {k n} (prg : (key : Bits k) → Bits n) → Set
SecPRG prg = this looks random where this = ⟪ prg · random ⟫
record PRG k n : Set where
constructor _,_
field
prg : Bits k → Bits n
sec : SecPRG prg
OneTimeSecPRF : ∀ {k m n} (prf : (key : Bits k) (msg : Bits m) → Bits n) → Set
OneTimeSecPRF prf = ∀ {xs} → let this = ⟪ prf · random · ⟪ xs ⟫′ ⟫ in
this looks random
record PRF k m n : Set where
constructor _,_
field
prf : Bits k → Bits m → Bits n
sec : OneTimeSecPRF prf
OTP : ∀ {n} → Bits n → Bits n → Bits n
OTP key msg = key ⊕ msg
init : ∀ {k a} {A : Set a} → (Bits k → A) → M k A
init f = ⟪ f · random ⟫
module Examples (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
open WithEquiv progEq
left-unit-law = ∀ {A B : Set} {n} {x : A} {f : A → M n B} → return′ x >>= f ≈ f x
right-unit-law = ∀ {A : Set} {n} {x : M n A} → x >>=′ return′ ≈ x
assoc-law = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≋ x >>= (λ x → f x >>= g)
assoc-law′ = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≈ coerce (≡.sym (ℕ°.+-assoc n₁ n₂ n₃)) (x >>= (λ x → f x >>= g))
ex₁ = ∀ {x} → toss′ ⟨xor⟩ ⟪ x ⟫′ ≈ ⟪ x ⟫
ex₂ = p ≈ map swap p where p = toss′ ⟨,⟩ toss′
ex₃ = ∀ {n} → OneTimeSecPRF {n} OTP
ex₄ = ∀ {k n} (prg : PRG k n) → OneTimeSecPRF (λ key xs → xs ⊕ PRG.prg prg key)
ex₅ = ∀ {k n} → PRG k n → PRF k n n
|
module flipbased where
open import Algebra
import Level as L
open L using () renaming (_⊔_ to _L⊔_)
open import Function hiding (_⟨_⟩_)
open import Data.Nat.NP
open import Data.Bool
open import Data.Nat.Properties
open import Data.Product using (proj₁; proj₂; _,_; swap; _×_)
open import Data.Bits hiding (replicateM)
open import Data.Bool
open import Data.Vec using (Vec; []; _∷_; take; drop; head; tail)
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_)
record M {a} n (A : Set a) : Set a where
constructor mk
field
run : Bits n → A
toss′ : M 1 Bit
toss′ = mk head
return′ : ∀ {a} {A : Set a} → A → M 0 A
return′ = mk ∘ const
pure′ : ∀ {a} {A : Set a} → A → M 0 A
pure′ = return′
comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → M m A → M n A
comap f (mk g) = mk (g ∘ f)
weaken : ∀ {m n a} {A : Set a} → M n A → M (m + n) A
weaken {m} = comap (drop m)
weaken′ : ∀ {m n a} {A : Set a} → M n A → M (n + m) A
weaken′ = comap (take _)
private
take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n
take≤ z≤n _ = []
take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs
weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → M m A → M n A
weaken≤ p = comap (take≤ p)
coerce : ∀ {m n a} {A : Set a} → m ≡ n → M m A → M n A
coerce ≡.refl = id
toss : ∀ {n} → M (1 + n) Bit
toss = weaken′ toss′
return : ∀ {n a} {A : Set a} → A → M n A
return = weaken′ ∘ return′
pure : ∀ {n a} {A : Set a} → A → M n A
pure = return
_>>=_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₁ + n₂) B
_>>=_ {n₁} x f = mk (λ bs → M.run (f (M.run x (take _ bs))) (drop n₁ bs))
_>>=′_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₂ + n₁) B
_>>=′_ {n₁} {n₂} rewrite ℕ°.+-comm n₂ n₁ = _>>=_
_>>_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → M n₂ B → M (n₁ + n₂) B
_>>_ {n₁} x y = x >>= const y
map : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → M n A → M n B
map f x = mk (f ∘ M.run x)
-- map f x ≗ x >>=′ (return {0} ∘ f)
join : ∀ {n₁ n₂ a} {A : Set a} →
M n₁ (M n₂ A) → M (n₁ + n₂) A
join x = x >>= id
infixl 4 _⊛_
_⊛_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ (A → B) → M n₂ A → M (n₁ + n₂) B
_⊛_ {n₁} mf mx = mf >>= λ f → map (_$_ f) mx
_⟨_⟩_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
M m A → (A → B → C) → M n B → M (m + n) C
x ⟨ f ⟩ y = map f x ⊛ y
⟪_⟫ : ∀ {n} {a} {A : Set a} → A → M n A
⟪_⟫ = pure
⟪_⟫′ : ∀ {a} {A : Set a} → A → M 0 A
⟪_⟫′ = pure′
⟪_·_⟫ : ∀ {a b} {A : Set a} {B : Set b} {n} → (A → B) → M n A → M n B
⟪ f · x ⟫ = map f x
⟪_·_·_⟫ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
(A → B → C) → M m A → M n B → M (m + n) C
⟪ f · x · y ⟫ = map f x ⊛ y
⟪_·_·_·_⟫ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} →
(A → B → C → D) → M m A → M n B → M o C → M (m + n + o) D
⟪ f · x · y · z ⟫ = map f x ⊛ y ⊛ z
choose : ∀ {n a} {A : Set a} → M n A → M n A → M (suc n) A
choose x y = toss′ >>= λ b → if b then x else y
_⟨,⟩_ : ∀ {a b} {A : Set a} {B : Set b} {m n} → M m A → M n B → M (m + n) (A × B)
x ⟨,⟩ y = ⟪ _,_ · x · y ⟫
_⟨xor⟩_ : ∀ {n₁ n₂} → M n₁ Bit → M n₂ Bit → M (n₁ + n₂) Bit
x ⟨xor⟩ y = ⟪ _xor_ · x · y ⟫
_⟨⊕⟩_ : ∀ {n₁ n₂ m} → M n₁ (Bits m) → M n₂ (Bits m) → M (n₁ + n₂) (Bits m)
x ⟨⊕⟩ y = ⟪ _⊕_ · x · y ⟫
replicateM : ∀ {n m} {a} {A : Set a} → M m A → M (n * m) (Vec A n)
replicateM {zero} _ = ⟪ [] ⟫
replicateM {suc _} x = ⟪ _∷_ · x · replicateM x ⟫
random : ∀ {n} → M n (Bits n)
-- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce
random {zero} = ⟪ [] ⟫
random {suc _} = ⟪ _∷_ · toss′ · random ⟫
randomTbl : ∀ m n → M (2 ^ m * n) (Vec (Bits n) (2 ^ m))
randomTbl m n = replicateM random
randomFun : ∀ m n → M (2 ^ m * n) (Bits m → Bits n)
randomFun m n = ⟪ funFromTbl · randomTbl m n ⟫
randomFunExt : ∀ {n k a} {A : Set a} → M k (Bits n → A) → M (k + k) (Bits (suc n) → A)
randomFunExt f = ⟪ comb · f · f ⟫ where comb = λ g₁ g₂ xs → (if head xs then g₁ else g₂) (tail xs)
2*_ : ℕ → ℕ
2* x = x + x
2^_ : ℕ → ℕ
2^ 0 = 1
2^ (suc n) = 2* (2^ n)
costRndFun : ℕ → ℕ → ℕ
costRndFun zero n = n
costRndFun (suc m) n = 2* (costRndFun m n)
lem : ∀ m n → costRndFun m n ≡ 2 ^ m * n
lem zero n rewrite ℕ°.+-comm n 0 = ≡.refl
lem (suc m) n rewrite lem m n | ℕ°.*-assoc 2 (2 ^ m) n | ℕ°.+-comm (2 ^ m * n) 0 = ≡.refl
randomFun′ : ∀ {m n} → M (costRndFun m n) (Bits m → Bits n)
randomFun′ {zero} = ⟪ const · random ⟫
randomFun′ {suc m} = randomFunExt (randomFun′ {m})
record ProgEquiv a ℓ : Set (L.suc ℓ L⊔ L.suc a) where
infix 2 _≈_ _≋_
field
_≈_ : ∀ {n} {A : Set a} → Rel (M n A) ℓ
refl : ∀ {n A} → Reflexive {A = M n A} _≈_
sym : ∀ {n A} → Symmetric {A = M n A} _≈_
-- not strictly transitive
reflexive : ∀ {n A} → _≡_ ⇒ _≈_ {n} {A}
reflexive ≡.refl = refl
_≋_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_≋_ {n₁} {n₂} p₁ p₂ = _≈_ {n = n₁ ⊔ n₂} (weaken≤ (m≤m⊔n _ _) p₁) (weaken≤ (m≤n⊔m _ n₁) p₂)
where m≤n⊔m : ∀ m n → m ≤ n ⊔ m
m≤n⊔m m n rewrite ⊔°.+-comm n m = m≤m⊔n m n
-- Another name for _≋_
_looks_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_looks_ = _≋_
module WithEquiv (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
SecPRG : ∀ {k n} (prg : (key : Bits k) → Bits n) → Set
SecPRG prg = this looks random where this = ⟪ prg · random ⟫
record PRG k n : Set where
constructor _,_
field
prg : Bits k → Bits n
sec : SecPRG prg
OneTimeSecPRF : ∀ {k m n} (prf : (key : Bits k) (msg : Bits m) → Bits n) → Set
OneTimeSecPRF prf = ∀ {xs} → let this = ⟪ prf · random · ⟪ xs ⟫′ ⟫ in
this looks random
record PRF k m n : Set where
constructor _,_
field
prf : Bits k → Bits m → Bits n
sec : OneTimeSecPRF prf
OTP : ∀ {n} → Bits n → Bits n → Bits n
OTP key msg = key ⊕ msg
init : ∀ {k a} {A : Set a} → (Bits k → A) → M k A
init f = ⟪ f · random ⟫
module Examples (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
open WithEquiv progEq
left-unit-law = ∀ {A B : Set} {n} {x : A} {f : A → M n B} → return′ x >>= f ≈ f x
right-unit-law = ∀ {A : Set} {n} {x : M n A} → x >>=′ return′ ≈ x
assoc-law = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≋ x >>= (λ x → f x >>= g)
assoc-law′ = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≈ coerce (≡.sym (ℕ°.+-assoc n₁ n₂ n₃)) (x >>= (λ x → f x >>= g))
ex₁ = ∀ {x} → toss′ ⟨xor⟩ ⟪ x ⟫′ ≈ ⟪ x ⟫
ex₂ = p ≈ map swap p where p = toss′ ⟨,⟩ toss′
ex₃ = ∀ {n} → OneTimeSecPRF {n} OTP
ex₄ = ∀ {k n} (prg : PRG k n) → OneTimeSecPRF (λ key xs → xs ⊕ PRG.prg prg key)
ex₅ = ∀ {k n} → PRG k n → PRF k n n
|
fix choose
|
flipbased: fix choose
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
df41c005d15604256ca7d340975689955ae6729f
|
protocols.agda
|
protocols.agda
|
module protocols where
import Control.Protocol
import Control.Protocol.Additive
import Control.Protocol.CLL
import Control.Protocol.ClientServer
import Control.Protocol.Examples
import Control.Protocol.MultiParty
import Control.Protocol.Multiplicative
import Control.Protocol.Relation
import Control.Protocol.Sequence
|
module protocols where
import Control.Protocol
import Control.Protocol.Additive
import Control.Protocol.CLL
import Control.Protocol.ClientServer
import Control.Protocol.Examples
import Control.Protocol.Extend
import Control.Protocol.Lift
import Control.Protocol.MultiParty
import Control.Protocol.Multiplicative
import Control.Protocol.Relation
import Control.Protocol.Sequence
|
update protocols.agda
|
update protocols.agda
|
Agda
|
bsd-3-clause
|
crypto-agda/protocols
|
a3a6c0d7b42940b151354af420f8d4469955d5c8
|
Atlas/Change/Term.agda
|
Atlas/Change/Term.agda
|
module Atlas.Change.Term where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
-- nil-changes
nil-const : ∀ {ι : Base} → Const ∅ (base (ΔBase ι))
nil-const {ι} = neutral {ΔBase ι}
nil-term : ∀ {ι Γ} → Term Γ (base (ΔBase ι))
nil-term {Bool} = curriedConst (nil-const {Bool})
nil-term {Map κ ι} = curriedConst (nil-const {Map κ ι})
-- diff-term and apply-term
open import Parametric.Change.Term Const ΔBase
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))
Atlas-diff {Bool} = abs (abs (curriedConst xor (var (that this)) (var this)))
Atlas-diff {Map κ ι} = abs (abs (curriedConst zip (abs Atlas-diff) (var (that this)) (var this)))
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = abs (abs (curriedConst xor (var (that this)) (var this)))
Atlas-apply {Map κ ι} = abs (abs (curriedConst zip (abs Atlas-apply) (var (that this)) (var this)))
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ (lift-diff Atlas-diff Atlas-apply)
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ (lift-apply Atlas-diff Atlas-apply)
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
insert : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
-- last argument is the change accumulator
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
|
module Atlas.Change.Term where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
-- nil-changes
nil-const : ∀ {ι : Base} → Const ∅ (base (ΔBase ι))
nil-const {ι} = neutral {ΔBase ι}
nil-term : ∀ {ι Γ} → Term Γ (base (ΔBase ι))
nil-term {Bool} = curriedConst (nil-const {Bool})
nil-term {Map κ ι} = curriedConst (nil-const {Map κ ι})
-- diff-term and apply-term
open import Parametric.Change.Term Const ΔBase
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))
Atlas-diff {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
Atlas-diff {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs Atlas-diff) m₁ m₂)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
Atlas-apply {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs Atlas-apply) m₁ m₂)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ (lift-diff Atlas-diff Atlas-apply)
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ (lift-apply Atlas-diff Atlas-apply)
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
insert : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
-- last argument is the change accumulator
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
|
Simplify Atlas-diff and Atlas-apply.
|
Simplify Atlas-diff and Atlas-apply.
Old-commit-hash: ada22ebb7416f8c4fb19882d4080166628b1b292
|
Agda
|
mit
|
inc-lc/ilc-agda
|
d41e35b9fb769cc937f3fa2787fe655ac719f13a
|
Syntax/Language/Atlas.agda
|
Syntax/Language/Atlas.agda
|
module Syntax.Language.Atlas where
-- Base types of the calculus Atlas
-- to be used with Plotkin-style language description
--
-- Atlas supports maps with neutral elements. The change type to
-- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the
-- ground type ι. Such a change to maps can support insertions
-- and deletions as well: Inserting `k -> v` means mapping `k` to
-- the change from the neutral element to `v`, and deleting
-- `k -> v` means mapping `k` to the change from `v` to the
-- neutral element.
open import Syntax.Language.Calculus
data Atlas-type : Set where
Bool : Atlas-type
Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type
data Atlas-const : Set where
true : Atlas-const
false : Atlas-const
xor : Atlas-const
empty : ∀ {κ ι : Atlas-type} → Atlas-const
update : ∀ {κ ι : Atlas-type} → Atlas-const
zip : ∀ {κ a b c : Atlas-type} → Atlas-const
Atlas-lookup : Atlas-const → Type Atlas-type
Atlas-lookup true = base Bool
Atlas-lookup false = base Bool
Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool
Atlas-lookup (empty {κ} {ι}) = base (Map κ ι)
-- `update key val my-map` would
-- - insert if `key` is not present in `my-map`
-- - delete if `val` is the neutral element
-- - make an update otherwise
Atlas-lookup (update {κ} {ι}) =
base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι)
-- Model of zip = Haskell Data.List.zipWith
-- zipWith :: (a → b → c) → [a] → [b] → [c]
Atlas-lookup (zip {κ} {a} {b} {c}) =
(base κ ⇒ base a ⇒ base b ⇒ base c) ⇒
base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c)
Atlas-Δbase : Atlas-type → Atlas-type
-- change to a boolean is a xor-rand
Atlas-Δbase Bool = Bool
-- change to a map is change to its values
Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val))
Atlas-Δtype : Type Atlas-type → Type Atlas-type
Atlas-Δtype = lift-Δtype₀ Atlas-Δbase
Atlas-context : Set
Atlas-context = Context {Type Atlas-type}
Atlas-term : Atlas-context → Type Atlas-type → Set
Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}
-- Every base type has a known nil-change.
-- The nil-change of ι is also the neutral element of Map κ Δι.
neutral : ∀ {ι : Atlas-type} → Atlas-const
neutral {Bool} = false
neutral {Map κ ι} = empty {κ} {ι}
neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι)
neutral-term {Bool} = const (neutral {Bool})
neutral-term {Map κ ι} = const (neutral {Map κ ι})
nil-const : ∀ {ι : Atlas-type} → Atlas-const
nil-const {ι} = neutral {Atlas-Δbase ι}
nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι))
nil-term {Bool} = const (nil-const {Bool})
nil-term {Map κ ι} = const (nil-const {Map κ ι})
-- Shorthands of constants
--
-- There's probably a uniform way to lift constants
-- into term constructors.
--
-- TODO: write this and call it Syntax.Term.Plotkin.lift-term
zip! : ∀ {κ a b c Γ} →
Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) →
Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) →
Atlas-term Γ (base (Map κ c))
zip! f m₁ m₂ = app (app (app (const zip) f) m₁) m₂
lookup! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base ι)
lookup! = {!!} -- TODO: add constant `lookup`
-- diff-term and apply-term
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι))
Atlas-diff {Bool} = const xor
Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = const xor
Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Atlas-term Γ τ → Atlas-term Γ τ →
Atlas-term Γ (Atlas-Δtype τ)
diff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t
apply : ∀ {τ Γ} →
Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ →
Atlas-term Γ τ
apply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
update! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base (Map κ ι))
update! k v m = app (app (app (const update) k) v) m
insert : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
-- last argument is the change accumulator
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
-- union s t should be:
-- 1. equal to t if s is neutral
-- 2. equal to s if t is neutral
-- 3. equal to s if s == t
-- 4. whatever it wants to be otherwise
--
-- On Booleans, only conjunction can be union.
--
-- TODO (later): support conjunction, probably by Boolean
-- elimination form if-then-else
postulate
and! : ∀ {Γ} →
Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) →
Atlas-term Γ (base Bool)
union : ∀ {ι Γ} →
Atlas-term Γ (base ι) → Atlas-term Γ (base ι) →
Atlas-term Γ (base ι)
union {Bool} s t = and! s t
union {Map κ ι} s t =
let
union-term = abs (abs (union (var (that this)) (var this)))
in
zip! (abs union-term) s t
-- Shorthand for 4-way zip
zip4! : ∀ {κ a b c d e Γ} →
let
t:_ = λ ι → Atlas-term Γ (base ι)
in
Atlas-term Γ
(base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) →
t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e
zip4! f m₁ m₂ m₃ m₄ =
let
v₁ = var (that this)
v₂ = var this
v₃ = var (that this)
v₄ = var this
k₁₂ = var (that (that this))
k₃₄ = var (that (that this))
f₁₂ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₁₂) v₁) v₂)
(lookup! k₁₂ (weaken₃ m₃))) (lookup! k₁₂ (weaken₃ m₄)))))
f₃₄ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₃₄)
(lookup! k₃₄ (weaken₃ m₁)))
(lookup! k₃₄ (weaken₃ m₂))) v₃) v₄)))
in
-- A correct but inefficient implementation.
-- May want to speed it up after constants are finalized.
union (zip! f₁₂ m₁ m₂) (zip! f₃₄ m₃ m₄)
-- Type signature of Atlas-Δconst is boilerplate.
Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) →
Atlas-term Γ (Atlas-Δtype (Atlas-lookup c))
Atlas-Δconst true = const false
Atlas-Δconst false = const false
-- Δxor = λ x Δx y Δy → Δx xor Δy
Atlas-Δconst xor =
let
Δx = var (that (that this))
Δy = var this
in abs (abs (abs (abs (app (app (const xor) Δx) Δy))))
Atlas-Δconst empty = const empty
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
Atlas-Δconst update =
let
k = var (that (that (that (that (that this)))))
Δk = var (that (that (that (that this))))
v = var (that (that (that this)))
Δv = var (that (that this))
-- m = var (that this) -- unused parameter
Δm = var this
in
abs (abs (abs (abs (abs (abs
(insert (apply Δk k) (apply Δv v)
(delete k v Δm)))))))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
Atlas-Δconst zip =
let
Δf = var (that (that (that (that this))))
m₁ = var (that (that (that this)))
Δm₁ = var (that (that this))
m₂ = var (that this)
Δm₂ = var this
g = abs (app (app (weaken₁ Δf) (var this)) nil-term)
in
abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂))))))
Atlas = calculus-with
Atlas-type
Atlas-const
Atlas-lookup
Atlas-Δtype
Atlas-Δconst
|
module Syntax.Language.Atlas where
-- Base types of the calculus Atlas
-- to be used with Plotkin-style language description
--
-- Atlas supports maps with neutral elements. The change type to
-- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the
-- ground type ι. Such a change to maps can support insertions
-- and deletions as well: Inserting `k -> v` means mapping `k` to
-- the change from the neutral element to `v`, and deleting
-- `k -> v` means mapping `k` to the change from `v` to the
-- neutral element.
open import Syntax.Language.Calculus
data Atlas-type : Set where
Bool : Atlas-type
Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type
data Atlas-const : Set where
true : Atlas-const
false : Atlas-const
xor : Atlas-const
empty : ∀ {κ ι : Atlas-type} → Atlas-const
update : ∀ {κ ι : Atlas-type} → Atlas-const
lookup : ∀ {κ ι : Atlas-type} → Atlas-const
zip : ∀ {κ a b c : Atlas-type} → Atlas-const
fold : ∀ {κ a b : Atlas-type} → Atlas-const
Atlas-lookup : Atlas-const → Type Atlas-type
Atlas-lookup true = base Bool
Atlas-lookup false = base Bool
Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool
Atlas-lookup (empty {κ} {ι}) = base (Map κ ι)
-- `update key val my-map` would
-- - insert if `key` is not present in `my-map`
-- - delete if `val` is the neutral element
-- - make an update otherwise
Atlas-lookup (update {κ} {ι}) =
base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι)
Atlas-lookup (lookup {κ} {ι}) =
base κ ⇒ base (Map κ ι) ⇒ base ι
-- Model of zip = Haskell Data.List.zipWith
--
-- zipWith :: (a → b → c) → [a] → [b] → [c]
--
-- Behavioral difference: all key-value pairs present
-- in *either* (m₁ : Map κ a) *or* (m₂ : Map κ b) will
-- be iterated over. Neutral element of type `a` or `b`
-- will be supplied if the key is missing in the
-- corresponding map.
Atlas-lookup (zip {κ} {a} {b} {c}) =
(base κ ⇒ base a ⇒ base b ⇒ base c) ⇒
base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c)
-- Model of fold = Haskell Data.Map.foldWithKey
--
-- foldWithKey :: (k → a → b → b) → b → Map k a → b
--
Atlas-lookup (fold {κ} {a} {b}) =
(base κ ⇒ base a ⇒ base b ⇒ base b) ⇒
base b ⇒ base (Map κ a) ⇒ base b
Atlas-Δbase : Atlas-type → Atlas-type
-- change to a boolean is a xor-rand
Atlas-Δbase Bool = Bool
-- change to a map is change to its values
Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val))
Atlas-Δtype : Type Atlas-type → Type Atlas-type
Atlas-Δtype = lift-Δtype₀ Atlas-Δbase
Atlas-context : Set
Atlas-context = Context {Type Atlas-type}
Atlas-term : Atlas-context → Type Atlas-type → Set
Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}
-- Shorthands of constants
--
-- There's probably a uniform way to lift constants
-- into term constructors.
update! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base (Map κ ι))
update! = app₃ (const update)
lookup! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base ι)
lookup! = app₂ (const lookup)
zip! : ∀ {κ a b c Γ} →
Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) →
Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) →
Atlas-term Γ (base (Map κ c))
zip! = app₃ (const zip)
fold! : ∀ {κ a b Γ} →
Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base b) →
Atlas-term Γ (base b) → Atlas-term Γ (base (Map κ a)) →
Atlas-term Γ (base b)
fold! = app₃ (const fold)
-- Every base type has a known nil-change.
-- The nil-change of ι is also the neutral element of Map κ Δι.
neutral : ∀ {ι : Atlas-type} → Atlas-const
neutral {Bool} = false
neutral {Map κ ι} = empty {κ} {ι}
neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι)
neutral-term {Bool} = const (neutral {Bool})
neutral-term {Map κ ι} = const (neutral {Map κ ι})
nil-const : ∀ {ι : Atlas-type} → Atlas-const
nil-const {ι} = neutral {Atlas-Δbase ι}
nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι))
nil-term {Bool} = const (nil-const {Bool})
nil-term {Map κ ι} = const (nil-const {Map κ ι})
-- Nonfunctional products can be encoded.
-- The incremental behavior of products thus encoded is weird:
-- Δ(α × β) = α × Δβ
Pair : Atlas-type → Atlas-type → Atlas-type
Pair α β = Map α β
pair : ∀ {α β Γ} →
Atlas-term Γ (base α) → Atlas-term Γ (base β) →
Atlas-term Γ (base (Pair α β))
pair s t = update! s t (const empty)
pair-term : ∀ {α β Γ} →
Atlas-term Γ (base α ⇒ base β ⇒ base (Pair α β))
pair-term = abs (abs (pair (var (that this)) (var this)))
uncurry : ∀ {α β γ Γ} →
Atlas-term Γ (base α ⇒ base β ⇒ base γ) →
Atlas-term Γ (base (Pair α β)) →
Atlas-term Γ (base γ)
uncurry f p =
let
a = var (that (that this))
b = var (that this)
g = abs (abs (abs (app₂ (weaken₃ f) a b)))
in
fold! g neutral-term p
zip-pair : ∀ {κ a b Γ} →
Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) →
Atlas-term Γ (base (Map κ (Pair a b)))
zip-pair = zip! (abs pair-term)
-- diff-term and apply-term
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι))
Atlas-diff {Bool} = const xor
Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = const xor
Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Atlas-term Γ τ → Atlas-term Γ τ →
Atlas-term Γ (Atlas-Δtype τ)
diff = app₂ (lift-diff Atlas-diff Atlas-apply)
apply : ∀ {τ Γ} →
Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ →
Atlas-term Γ τ
apply = app₂ (lift-apply Atlas-diff Atlas-apply)
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
insert : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
-- last argument is the change accumulator
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
-- Shorthand for 4-way zip
zip4! : ∀ {κ a b c d e Γ} →
let
t:_ = λ ι → Atlas-term Γ (base ι)
in
Atlas-term Γ
(base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) →
t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e
-- zip₄ f m₁ m₂ m₃ m₄ =
-- zip (λ k p₁₂ p₃₄ → uncurry (λ v₁ v₂ → uncurry (λ v₃ v₄ →
-- f k v₁ v₂ v₃ v₄)
-- p₃₄) p₁₂)
-- (zip-pair m₁ m₂) (zip-pair m₃ m₄)
zip4! f m₁ m₂ m₃ m₄ =
let
f′ = weaken₃ (weaken₃ (weaken₁ f))
k = var (that (that (that (that (that (that this))))))
p₁₂ = var (that this)
p₃₄ = var (that (that this))
v₁ = var (that (that (that this)))
v₂ = var (that (that this))
v₃ = var (that this)
v₄ = var this
g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs
(app₅ f′ k v₁ v₂ v₃ v₄)))
p₃₄))) p₁₂)))
in
zip! g (zip-pair m₁ m₂) (zip-pair m₃ m₄)
-- Type signature of Atlas-Δconst is boilerplate.
Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) →
Atlas-term Γ (Atlas-Δtype (Atlas-lookup c))
Atlas-Δconst true = const false
Atlas-Δconst false = const false
-- Δxor = λ x Δx y Δy → Δx xor Δy
Atlas-Δconst xor =
let
Δx = var (that (that this))
Δy = var this
in abs (abs (abs (abs (app₂ (const xor) Δx Δy))))
Atlas-Δconst empty = const empty
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
Atlas-Δconst update =
let
k = var (that (that (that (that (that this)))))
Δk = var (that (that (that (that this))))
v = var (that (that (that this)))
Δv = var (that (that this))
-- m = var (that this) -- unused parameter
Δm = var this
in
abs (abs (abs (abs (abs (abs
(insert (apply Δk k) (apply Δv v)
(delete k v Δm)))))))
-- Δlookup k Δk m Δm | true? (k ⊕ Δk ≡ k)
-- ... | true = lookup k Δm
-- ... | false =
-- (lookup (k ⊕ Δk) m ⊕ lookup (k ⊕ Δk) Δm)
-- ⊝ lookup k m
--
-- Only the false-branch is implemented.
Atlas-Δconst lookup =
let
k = var (that (that (that this)))
Δk = var (that (that this))
m = var (that this)
Δm = var this
k′ = apply Δk k
in
abs (abs (abs (abs
(diff (apply (lookup! k′ Δm) (lookup! k′ m))
(lookup! k m)))))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
Atlas-Δconst zip =
let
Δf = var (that (that (that (that this))))
m₁ = var (that (that (that this)))
Δm₁ = var (that (that this))
m₂ = var (that this)
Δm₂ = var this
g = abs (app₂ (weaken₁ Δf) (var this) nil-term)
in
abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂))))))
-- Δfold f Δf z Δz m Δm = proj₂
-- (fold (λ k [a,Δa] [b,Δb] →
-- uncurry (λ a Δa → uncurry (λ b Δb →
-- pair (f k a b) (Δf k nil a Δa b Δb))
-- [b,Δb]) [a,Δa])
-- (pair z Δz)
-- (zip-pair m Δm))
--
-- Δfold is efficient only if evaluation is lazy and Δf is
-- self-maintainable: it doesn't look at the argument
-- (b = fold f k a b₀) at all.
Atlas-Δconst (fold {κ} {α} {β}) =
let -- TODO (tedius): write weaken₇
f = weaken₃ (weaken₃ (weaken₁
(var (that (that (that (that (that this))))))))
Δf = weaken₃ (weaken₃ (weaken₁
(var (that (that (that (that this)))))))
z = var (that (that (that this)))
Δz = var (that (that this))
m = var (that this)
Δm = var this
k = weaken₃ (weaken₁ (var (that (that this))))
[a,Δa] = var (that this)
[b,Δb] = var this
a = var (that (that (that this)))
Δa = var (that (that this))
b = var (that this)
Δb = var this
g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs
(pair (app₃ f k a b)
(app₆ Δf k nil-term a Δa b Δb))))
(weaken₂ [b,Δb])))) [a,Δa])))
proj₂ = uncurry (abs (abs (var this)))
in
abs (abs (abs (abs (abs (abs
(proj₂ (fold! g (pair z Δz) (zip-pair m Δm))))))))
Atlas = calculus-with
Atlas-type
Atlas-const
Atlas-lookup
Atlas-Δtype
Atlas-Δconst
|
add primitives `lookup`, `fold` and various improvements
|
Atlas: add primitives `lookup`, `fold` and various improvements
Improvements:
- speed up derivative of zip
- less verbose term constructors for constants
- remove need to support conjunction and union directly
Future work:
- elimination forms: if-then-else, if-nonempty
Old-commit-hash: 9aafd0c2835ff027b57e44ed2930f4f57147e0de
|
Agda
|
mit
|
inc-lc/ilc-agda
|
233ce4867a6fc1f8c29c642b2516cc11c7edd483
|
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
diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ})
_⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t
in
abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x))
,
(let
diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ})
_⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t
in
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff-term :
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
diff-term = λ {τ Γ} →
proj₁ (lift-diff-apply {τ} {Γ})
apply-term :
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
apply-term = λ {τ Γ} →
proj₂ (lift-diff-apply {τ} {Γ})
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ diff-term
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ apply-term
|
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
|
Implement apply-term and diff-term directly.
|
Implement apply-term and diff-term directly.
Before this commit: Mutual recursion of apply-term and diff-term encoded
explicitly via a product type. After this commit: Mutual recursion
expressed with Agda mutual recursion.
Old-commit-hash: 9a7571f8ef5cf6765629a001c46870e002f54d7e
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b51a3256a3c1252ef33def3ad7127f444c50c44e
|
incremental.agda
|
incremental.agda
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Semantics
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
Env⟦_⟧ : Context → Set
Env⟦ ∅ ⟧ = Empty
Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- Semantics
lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
lookup⟦ this ⟧ (v • ρ) = v
lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- Semantics
eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ)
eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ)
eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
nil : ∀ {τ Γ} → Term Γ (Δ-Type τ)
nil {τ₁ ⇒ τ₂} =
abs nil
-- λx. nil
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Γ
-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = {!!}
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Semantics
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
Env⟦_⟧ : Context → Set
Env⟦ ∅ ⟧ = Empty
Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- Semantics
lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
lookup⟦ this ⟧ (v • ρ) = v
lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- Semantics
eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ)
eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ)
eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
nil : ∀ {τ Γ} → Term Γ (Δ-Type τ)
nil {τ₁ ⇒ τ₂} =
abs nil
-- λx. nil
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ
-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = {!!}
|
Fix Δ-Context.
|
Fix Δ-Context.
Old-commit-hash: ce2c7324266f9f7511dd78f63accb2c3ae8c54fb
|
Agda
|
mit
|
inc-lc/ilc-agda
|
c1ff9f6bce16be7d52bc424c1c012656dec37587
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : (ι : Base) → ⟦ ι ⟧Base → Set
apply-change-base : ∀ ι → (v : ⟦ ι ⟧Base) → Change-base ι v → ⟦ ι ⟧Base
diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : (τ : Type) → ⟦ τ ⟧ → Set
nil-change : ∀ τ v → Change τ v
apply-change : ∀ τ → (v : ⟦ τ ⟧) (dv : Change τ v) → ⟦ τ ⟧
diff-change : ∀ τ → (u v : ⟦ τ ⟧) → Change τ v
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
Change (base ι) v = Change-base ι v
Change (σ ⇒ τ) f = Pair
(∀ v → Change σ v → Change τ (f v))
(λ Δf → ∀ v dv →
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ Δf (v ⊞₍ σ ₎ dv) (nil-change σ (v ⊞₍ σ ₎ dv)) ≡ f v ⊞₍ τ ₎ Δf v dv)
open Pair public using () renaming
( cdr to is-valid
; car to call-change
)
nil-change τ v = diff-change τ v v
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
apply-change (base ι) n Δn = apply-change-base ι n Δn
apply-change (σ ⇒ τ) f Δf = λ v → f v ⊞₍ τ ₎ call-change Δf v (nil-change σ v)
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
diff-change (base ι) m n = diff-change-base ι m n
diff-change (σ ⇒ τ) g f = cons (λ v dv → g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v)
(λ v dv →
begin
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g ((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ cong (λ □ → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g □ ⊟₍ τ ₎ (f (v ⊞₍ σ ₎ dv))))
(v+[u-v]=u {σ} {v ⊞₍ σ ₎ dv} {v ⊞₍ σ ₎ dv}) ⟩
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f (v ⊞₍ σ ₎ dv)} ⟩
g (v ⊞₍ σ ₎ dv)
≡⟨ sym (v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f v} ) ⟩
f v ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v)
∎) where open ≡-Reasoning
-- call this lemma "replace"?
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(apply-change (σ ⇒ τ) v (diff-change (σ ⇒ τ) u v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → (v : ⟦ τ ⟧) → Change τ v → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → (u v : ⟦ τ ⟧) → Change τ v
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
-- abbrevitations
before : ∀ {τ v} → Change τ v → ⟦ τ ⟧
before {τ} {v} _ = v
after : ∀ {τ v} → Change τ v → ⟦ τ ⟧
after {τ} {v} dv = v ⊞₍ τ ₎ dv
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
data ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set where
∅ : ΔEnv ∅ ∅
_•_ : ∀ {τ Γ v ρ} →
(dv : Change τ v) →
(dρ : ΔEnv Γ ρ) →
ΔEnv (τ • Γ) (v • ρ)
ignore : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
ignore {Γ} {ρ} _ = ρ
update : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
update ∅ = ∅
update {τ • Γ} (dv • dρ) = after {τ} dv • update dρ
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : (ι : Base) → ⟦ ι ⟧Base → Set
apply-change-base : ∀ ι → (v : ⟦ ι ⟧Base) → Change-base ι v → ⟦ ι ⟧Base
diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : (τ : Type) → ⟦ τ ⟧ → Set
nil-change : ∀ τ v → Change τ v
apply-change : ∀ τ → (v : ⟦ τ ⟧) (dv : Change τ v) → ⟦ τ ⟧
diff-change : ∀ τ → (u v : ⟦ τ ⟧) → Change τ v
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
Change (base ι) v = Change-base ι v
Change (σ ⇒ τ) f = Pair
(∀ v → Change σ v → Change τ (f v))
(λ Δf → ∀ v dv →
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ Δf (v ⊞₍ σ ₎ dv) (nil-change σ (v ⊞₍ σ ₎ dv)) ≡ f v ⊞₍ τ ₎ Δf v dv)
open Pair public using () renaming
( cdr to is-valid
; car to call-change
)
nil-change τ v = diff-change τ v v
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
apply-change (base ι) n Δn = apply-change-base ι n Δn
apply-change (σ ⇒ τ) f Δf = λ v → f v ⊞₍ τ ₎ call-change Δf v (nil-change σ v)
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
diff-change (base ι) m n = diff-change-base ι m n
diff-change (σ ⇒ τ) g f = cons (λ v dv → g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v)
(λ v dv →
begin
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g ((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ cong (λ □ → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g □ ⊟₍ τ ₎ (f (v ⊞₍ σ ₎ dv))))
(v+[u-v]=u {σ} {v ⊞₍ σ ₎ dv} {v ⊞₍ σ ₎ dv}) ⟩
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f (v ⊞₍ σ ₎ dv)} ⟩
g (v ⊞₍ σ ₎ dv)
≡⟨ sym (v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f v} ) ⟩
f v ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v)
∎) where open ≡-Reasoning
-- call this lemma "replace"?
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(apply-change (σ ⇒ τ) v (diff-change (σ ⇒ τ) u v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → (v : ⟦ τ ⟧) → Change τ v → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → (u v : ⟦ τ ⟧) → Change τ v
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
-- abbrevitations
before : ∀ {τ v} → Change τ v → ⟦ τ ⟧
before {τ} {v} _ = v
after : ∀ {τ v} → Change τ v → ⟦ τ ⟧
after {τ} {v} dv = v ⊞₍ τ ₎ dv
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
data ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set where
∅ : ΔEnv ∅ ∅
_•_ : ∀ {τ Γ v ρ} →
(dv : Change τ v) →
(dρ : ΔEnv Γ ρ) →
ΔEnv (τ • Γ) (v • ρ)
ignore : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
ignore {Γ} {ρ} _ = ρ
update : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
update ∅ = ∅
update {τ • Γ} (dv • dρ) = after {τ} dv • update dρ
apply-env : ∀ Γ → (ρ : ⟦ Γ ⟧) → (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
apply-env Γ ρ dρ = update {Γ} dρ
diff-env : ∀ Γ → (π ρ : ⟦ Γ ⟧) → ΔEnv Γ ρ
diff-env ∅ ∅ ∅ = ∅
diff-env (τ • Γ) (u • ρ) (v • π) = diff-change τ u v • diff-env Γ ρ π
|
Implement apply-env and diff-env.
|
Implement apply-env and diff-env.
Old-commit-hash: 0a4d0485631257cbfb9ee522e8ac6facb88f0721
|
Agda
|
mit
|
inc-lc/ilc-agda
|
79eb1bff6fe6f19057332ff8737b10e85457ff5f
|
Parametric/Denotation/CachingEvaluation.agda
|
Parametric/Denotation/CachingEvaluation.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Caching evaluation
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
open import Base.Denotation.Notation
-- Extension Point: Evaluation of fully applied constants.
ValStructure : Set
ValStructure = ∀ {Σ τ} → ValConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
CompStructure : Set
CompStructure = ∀ {Σ τ} → CompConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
module Structure
(⟦_⟧ValBaseTermCache : ValStructure)
(⟦_⟧CompBaseTermCache : CompStructure)
where
{-
-- Prototype here the type-correctness of a simple non-standard semantics.
-- This describes a simplified version of the transformation by Liu and
-- Tanenbaum, PEPM 1995 - but for now, instead of producing object language
-- terms, we produce host language terms to take advantage of the richer type
-- system of the host language (in particular, here we need the unit type,
-- product types and *existentials*).
--
-- As usual, we'll later switch to a real term transformation.
-}
open import Data.Product hiding (map)
open import Data.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- The solution is to distinguish among different kinds of constants. Some are
-- value constructors (and thus do not return caches), while others are
-- computation constructors (and thus should return caches). For products, I
-- believe we will only use the products which are values, not computations
-- (XXX check CBPV paper for the name).
⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- This says that the environment does not contain caches... sounds wrong!
-- Either we add extra variables for the caches, or we store computations in
-- the environment (but that does not make sense), or we store caches in
-- values, by acting not on F but on something else (U?).
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- No caching, because the arguments are values, so evaluating them does not
-- produce intermediate results.
⟦ vConst c args ⟧ValTermCache ρ = ⟦ c ⟧ValBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- The only caching is done by the interpretation of the constant (because the
-- arguments are values so need no caching).
⟦_⟧CompTermCache (cConst c args) ρ = ⟦ c ⟧CompBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- Also, where are introduction forms for pairs and sums among values? With
-- them, we should see that we can interpret them without adding a cache.
-- Thunks keep seeming noops.
⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ
-- 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 ρ)
⟦_⟧TermCacheCBV : ∀ {τ Γ} → Term Γ τ → ⟦ fromCBVCtx Γ ⟧ValCtxHidCache → ⟦ cbvToCompType τ ⟧CompTypeHidCache
⟦ t ⟧TermCacheCBV = ⟦ fromCBV t ⟧CompTermCache
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Caching evaluation
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
open import Base.Denotation.Notation
-- Extension Point: Evaluation of fully applied constants.
ValStructure : Set
ValStructure = ∀ {Σ τ} → ValConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
CompStructure : Set
CompStructure = ∀ {Σ τ} → CompConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
module Structure
(⟦_⟧ValBaseTermCache : ValStructure)
(⟦_⟧CompBaseTermCache : CompStructure)
where
{-
-- Prototype here the type-correctness of a simple non-standard semantics.
-- This describes a simplified version of the transformation by Liu and
-- Tanenbaum, PEPM 1995 - but for now, instead of producing object language
-- terms, we produce host language terms to take advantage of the richer type
-- system of the host language (in particular, here we need the unit type,
-- product types and *existentials*).
--
-- As usual, we'll later switch to a real term transformation.
-}
open import Data.Product hiding (map)
open import Data.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- The solution is to distinguish among different kinds of constants. Some are
-- value constructors (and thus do not return caches), while others are
-- computation constructors (and thus should return caches). For products, I
-- believe we will only use the products which are values, not computations
-- (XXX check CBPV paper for the name).
⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- This says that the environment does not contain caches... sounds wrong!
-- Either we add extra variables for the caches, or we store computations in
-- the environment (but that does not make sense), or we store caches in
-- values, by acting not on F but on something else (U?).
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- No caching, because the arguments are values, so evaluating them does not
-- produce intermediate results.
⟦ vConst c args ⟧ValTermCache ρ = ⟦ c ⟧ValBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- The only caching is done by the interpretation of the constant (because the
-- arguments are values so need no caching).
⟦_⟧CompTermCache (cConst c args) ρ = ⟦ c ⟧CompBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- Also, where are introduction forms for pairs and sums among values? With
-- them, we should see that we can interpret them without adding a cache.
-- Thunks keep seeming noops.
⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ
-- The effect of F is a writer monad of cached values, where the monoid is
-- (isomorphic to) the free monoid over (∃ τ . τ), but we push the
-- existentials up when pairing things!
-- That's what we're interpreting computations in. XXX maybe consider using
-- monads directly. But that doesn't deal with arity.
⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt)
-- For this to be enough, a lambda needs to return a produce, not to forward
-- the underlying one (unless there are no intermediate results). The correct
-- requirements can maybe be enforced through a linear typing discipline.
{-
-- Here we'd have a problem with the original into from CBPV, because it does
-- not require converting expressions to the "CBPV A-normal form".
⟦_⟧CompTermCache (v₁ into v₂) ρ =
-- Sequence commands and combine their caches.
{-
let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in , (r₂ , (c₁ ,′ c₂))
-}
-- The code above does not work, because we only guarantee that v₂ is
-- a computation, not that it's an F-computation - v₂ could also be function
-- type or a computation product.
-- We need a restricted CBPV, where these two possibilities are forbidden. If
-- you want to save something between different lambdas, you need to add an F
-- U to reflect that. (Double-check the papers which show how to encode arity
-- using CBPV, it seems that they should find the same problem --- XXX they
-- don't).
-- Instead, just drop the first cache (XXX WRONG).
⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ)
-}
-- But if we alter _into_ as described above, composing the caches works!
-- However, we should not forget we also need to save the new intermediate
-- result, that is the one produced by the first part of the let.
⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ =
let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂))
-- Note the compositionality and luck: we don't need to do anything at the
-- cReturn time, we just need the nested into to do their job, because as I
-- said intermediate results are a writer monad.
--
-- Q: But then, do we still need to do all the other stuff? IOW, do we still
-- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <-
-- f args; z <- g args'; z)?
--
-- A: One thing we still need is using the monadic version of into for the
-- same reasons - which makes sense, since it has the type of monadic bind.
--
-- Maybe not: if we use a monad, which respects left and right identity, the
-- two above forms are equivalent. But what about associativity? We don't have
-- associativity with nested tuples in the middle. That's why the monad uses
-- lists! We can also use nested tuple, as long as in the into case we don't
-- do (a, b) but append a b (ahem, where?), which decomposes the first list
-- and prepends it to the second. To this end, we need to know the type of the
-- first element, or to ensure it's always a pair. Maybe we just want to reuse
-- an HList.
-- In abstractions, we should start collecting all variables...
-- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache,
-- that's moved into the handling of cReturn. This makes *the* difference for
-- nested lambdas, where we don't need to create caches multiple times!
⟦_⟧CompTermCache (cAbs v) ρ = λ x → ⟦ v ⟧CompTermCache (x • ρ)
-- Here we see that we are in a sort of A-normal form, because the argument is
-- a value (not quite ANF though, since values can be thunks - that is,
-- computations which haven't been run yet, I guess. Do we have an use for
-- that? That allows passing lambdas as arguments directly - which is fine,
-- because producing a closure indeed does not have intermediate results!).
⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
⟦_⟧TermCacheCBV : ∀ {τ Γ} → Term Γ τ → ⟦ fromCBVCtx Γ ⟧ValCtxHidCache → ⟦ cbvToCompType τ ⟧CompTypeHidCache
⟦ t ⟧TermCacheCBV = ⟦ fromCBV t ⟧CompTermCache
|
Drop dead comment
|
Drop dead comment
|
Agda
|
mit
|
inc-lc/ilc-agda
|
1dd4f0f861df9af4f01a33af2bb834b72be708cc
|
Base/Data/DependentList.agda
|
Base/Data/DependentList.agda
|
module Base.Data.DependentList where
open import Level
open import Data.Unit
open import Data.Product using (Σ ; _×_ ; _,_)
import Data.List as List
open List using (List ; [] ; _∷_)
data DependentList {a b} {A : Set a}
(F : A → Set b) : (type-args : List A) → Set (a ⊔ b)
where
∅ : DependentList F []
_•_ : ∀ {type-arg} {other-type-args}
(head : F type-arg)
(tail : DependentList F other-type-args) →
DependentList F (type-arg ∷ other-type-args)
infixr 5 _•_
|
module Base.Data.DependentList where
open import Data.List.All public
renaming
( All to DependentList
; _∷_ to _•_
; [] to ∅
)
|
Implement DependentList on top of Data.List.All.
|
Implement DependentList on top of Data.List.All.
Old-commit-hash: 32c7c7edadf9940f3c013ca3d4ebfdc8b3cbc4f4
|
Agda
|
mit
|
inc-lc/ilc-agda
|
816cffb53d3bae1af15f0f477ac1a68ccfa88b05
|
Control/Protocol/Sequence.agda
|
Control/Protocol/Sequence.agda
|
{-# OPTIONS --without-K #-}
open import Function.NP
open import Type
open import Data.Product.NP using (Σ; _×_; _,_; first) renaming (proj₁ to fst; proj₂ to snd)
open import Data.Two hiding (_≟_)
open import Data.Nat hiding (_⊔_)
open import Relation.Binary.PropositionalEquality.NP using (_≡_; _∙_; refl; ap; coe; coe!; !_ ; tr)-- renaming (subst to tr)
open import Function.Extensionality
open import HoTT
open Equivalences
open import Type.Identities
open import Control.Protocol.Core
module Control.Protocol.Sequence where
infixl 1 _>>=_ _>>_
_>>=_ : (P : Proto) → (Log P → Proto) → Proto
end >>= Q = Q _
com q P >>= Q = com q λ m → P m >>= λ ms → Q (m , ms)
_>>_ : Proto → Proto → Proto
P >> Q = P >>= λ _ → Q
>>=-fst : ∀ P {Q} → ⟦ P >>= Q ⟧ → ⟦ P ⟧
>>=-fst end q = end
>>=-fst (recv P) pq = λ m → >>=-fst (P m) (pq m)
>>=-fst (send P) (m , pq) = m , >>=-fst (P m) pq
>>=-snd : ∀ P {Q}(pq : ⟦ P >>= Q ⟧)(p : ⟦ P ⊥⟧) → ⟦ Q (telecom P (>>=-fst P pq) p) ⟧
>>=-snd end q end = q
>>=-snd (recv P) pq (m , p) = >>=-snd (P m) (pq m) p
>>=-snd (send P) (m , pq) p = >>=-snd (P m) pq (p m)
[_]_>>>=_ : ∀ P {Q} → ⟦ P ⟧ → ((log : Log P) → ⟦ Q log ⟧) → ⟦ P >>= Q ⟧
[ end ] p >>>= q = q _
[ recv P ] p >>>= q = λ m → [ P m ] p m >>>= λ log → q (m , log)
[ send P ] (m , p) >>>= q = m , [ P m ] p >>>= λ log → q (m , log)
[_]_>>>_ : ∀ P {Q} → ⟦ P ⟧ → ⟦ Q ⟧ → ⟦ P >> Q ⟧
[ P ] p >>> q = [ P ] p >>>= λ _ → q
module _ {{_ : FunExt}} where
>>=-fst-inv : ∀ P {Q}(p : ⟦ P ⟧)(q : ((log : Log P) → ⟦ Q log ⟧)) → >>=-fst P {Q} ([ P ] p >>>= q) ≡ p
>>=-fst-inv end end q = refl
>>=-fst-inv (recv P) p q = λ= λ m → >>=-fst-inv (P m) (p m) λ log → q (m , log)
>>=-fst-inv (send P) (m , p) q = snd= (>>=-fst-inv (P m) p λ log → q (m , log))
>>=-snd-inv : ∀ P {Q}(p : ⟦ P ⟧)(q : ((log : Log P) → ⟦ Q log ⟧))(p' : ⟦ P ⊥⟧)
→ tr (λ x → ⟦ Q (telecom P x p') ⟧) (>>=-fst-inv P p q)
(>>=-snd P {Q} ([ P ] p >>>= q) p') ≡ q (telecom P p p')
>>=-snd-inv end end q p' = refl
>>=-snd-inv (recv P) {Q} p q (m , p') =
ap (flip _$_ (>>=-snd (P m) ([ P m ] p m >>>= (λ log → q (m , log))) p'))
(tr-λ= (λ z → ⟦ Q (m , telecom (P m) z p') ⟧)
(λ m → >>=-fst-inv (P m) {Q ∘ _,_ m} (p m) (q ∘ _,_ m)))
∙ >>=-snd-inv (P m) {Q ∘ _,_ m} (p m) (q ∘ _,_ m) p'
>>=-snd-inv (send P) {Q} (m , p) q p' = tr-snd= (λ { (m , p) → ⟦ Q (m , telecom (P m) p (p' m)) ⟧ })
(>>=-fst-inv (P m) p (q ∘ _,_ m))
(>>=-snd (P m) {Q ∘ _,_ m} ([ P m ] p >>>= (q ∘ _,_ m)) (p' m))
∙ >>=-snd-inv (P m) {Q ∘ _,_ m} p (λ log → q (m , log)) (p' m)
{- hmmm...
>>=-uniq : ∀ P {Q} (pq : ⟦ P >>= Q ⟧)(p' : ⟦ P ⊥⟧) → pq ≡ [ P ] (>>=-fst P {Q} pq) >>>= (λ log → {!>>=-snd P {Q} pq p'!})
>>=-uniq = {!!}
-}
replicateᴾ : ℕ → Proto → Proto
replicateᴾ 0 P = end
replicateᴾ (suc n) P = P >> replicateᴾ n P
replicate-proc : ∀ n P → ⟦ P ⟧ → ⟦ replicateᴾ n P ⟧
replicate-proc zero P p = end
replicate-proc (suc n) P p = [ P ] p >>> replicate-proc n P p
module _ {{_ : FunExt}}{{_ : UA}} where
Log->>=-Σ : ∀ (P : Proto){Q} → Log (P >>= Q) ≡ Σ (Log P) (Log ∘ Q)
Log->>=-Σ end = ! (×= End-uniq refl ∙ 𝟙×-snd)
Log->>=-Σ (com _ P) = Σ=′ _ (λ m → Log->>=-Σ (P m)) ∙ Σ-assoc
Log->>-× : ∀ (P : Proto){Q} → Log (P >> Q) ≡ (Log P × Log Q)
Log->>-× P = Log->>=-Σ P
++Log' : ∀ (P : Proto){Q : Log P → Proto} (xs : Log P) → Log (Q xs) → Log (P >>= Q)
++Log' P p q = coe! (Log->>=-Σ P) (p , q)
-- Same as ++Log' but computes better
++Log : ∀ (P : Proto){Q : Log P → Proto} (xs : Log P) → Log (Q xs) → Log (P >>= Q)
++Log end _ ys = ys
++Log (com q P) (x , xs) ys = x , ++Log (P x) xs ys
module _ {{_ : FunExt}}{{_ : UA}} where
module _ {{_ : FunExt}} where
>>-right-unit : ∀ P → (P >> end) ≡ P
>>-right-unit end = refl
>>-right-unit (com q P) = com= refl refl λ m → >>-right-unit (P m)
>>=-assoc : ∀ (P : Proto)(Q : Log P → Proto)(R : Log (P >>= Q) → Proto)
→ (P >>= (λ x → Q x >>= (λ y → R (++Log P x y)))) ≡ ((P >>= Q) >>= R)
>>=-assoc end Q R = refl
>>=-assoc (com q P) Q R = com= refl refl λ m → >>=-assoc (P m) (λ ms → Q (m , ms)) (λ ms → R (m , ms))
ap->>= : ∀ P {Q₀ Q₁} → (∀ {log} → ⟦ Q₀ log ⟧ ≡ ⟦ Q₁ log ⟧) → ⟦ P >>= Q₀ ⟧ ≡ ⟦ P >>= Q₁ ⟧
ap->>= end Q= = Q=
ap->>= (send P) Q= = Σ=′ _ λ m → ap->>= (P m) Q=
ap->>= (recv P) Q= = Π=′ _ λ m → ap->>= (P m) Q=
dual->> : ∀ P Q → dual (P >> Q) ≡ (dual P >> dual Q)
dual->> end Q = refl
dual->> (com io P) Q = com= refl refl λ m → dual->> (P m) Q
{- My coe-ap-fu is lacking...
dual->>= : ∀ P (Q : Log P → Proto) → dual (P >>= Q) ≡ dual P >>= (dual ∘ Q ∘ coe (dual-Log P))
dual->>= end Q = refl
dual->>= (com io P) Q = com= refl refl λ m → dual->>= (P m) (Q ∘ _,_ m) ∙ ap (_>>=_ (dual (P m))) (λ= λ ms → ap (λ x → dual (Q x)) (pair= {!!} {!!}))
-}
dual-replicateᴾ : ∀ n P → dual (replicateᴾ n P) ≡ replicateᴾ n (dual P)
dual-replicateᴾ zero P = refl
dual-replicateᴾ (suc n) P = dual->> P (replicateᴾ n P) ∙ ap (_>>_ (dual P)) (dual-replicateᴾ n P)
{- An incremental telecom function which makes processes communicate
during a matching initial protocol. -}
>>=-telecom : (P : Proto){Q : Log P → Proto}{R : Log P → Proto}
→ ⟦ P >>= Q ⟧
→ ⟦ P >>= R ⊥⟧
→ Σ (Log P) (λ t → ⟦ Q t ⟧ × ⟦ R t ⊥⟧)
>>=-telecom end p0 p1 = _ , p0 , p1
>>=-telecom (send P) (m , p0) p1 = first (_,_ m) (>>=-telecom (P m) p0 (p1 m))
>>=-telecom (recv P) p0 (m , p1) = first (_,_ m) (>>=-telecom (P m) (p0 m) p1)
>>-telecom : (P : Proto){Q R : Proto}
→ ⟦ P >> Q ⟧
→ ⟦ P >> R ⊥⟧
→ Log P × ⟦ Q ⟧ × ⟦ R ⊥⟧
>>-telecom P p q = >>=-telecom P p q
|
{-# OPTIONS --without-K #-}
open import Function.NP
open import Type
open import Data.Product.NP using (Σ; _×_; _,_; first) renaming (proj₁ to fst; proj₂ to snd)
open import Data.Two hiding (_≟_)
open import Data.Nat hiding (_⊔_)
open import Relation.Binary.PropositionalEquality.NP using (_≡_; _∙_; refl; ap; coe; coe!; !_ ; tr)-- renaming (subst to tr)
open import Function.Extensionality
open import HoTT
open Equivalences
open import Type.Identities
open import Control.Protocol.Core
module Control.Protocol.Sequence where
infixl 1 _>>=_ _>>_
_>>=_ : (P : Proto) → (Log P → Proto) → Proto
end >>= Q = Q _
com q P >>= Q = com q λ m → P m >>= λ ms → Q (m , ms)
_>>_ : Proto → Proto → Proto
P >> Q = P >>= λ _ → Q
>>=-fst : ∀ P {Q} → ⟦ P >>= Q ⟧ → ⟦ P ⟧
>>=-fst end q = end
>>=-fst (recv P) pq = λ m → >>=-fst (P m) (pq m)
>>=-fst (send P) (m , pq) = m , >>=-fst (P m) pq
>>=-snd : ∀ P {Q}(pq : ⟦ P >>= Q ⟧)(p : ⟦ P ⊥⟧) → ⟦ Q (telecom P (>>=-fst P pq) p) ⟧
>>=-snd end q end = q
>>=-snd (recv P) pq (m , p) = >>=-snd (P m) (pq m) p
>>=-snd (send P) (m , pq) p = >>=-snd (P m) pq (p m)
>>-snd : ∀ P {Q} (pq : ⟦ P >> Q ⟧)(p : ⟦ P ⊥⟧) → ⟦ Q ⟧
>>-snd P = >>=-snd P
[_]_>>>=_ : ∀ P {Q} → ⟦ P ⟧ → ((log : Log P) → ⟦ Q log ⟧) → ⟦ P >>= Q ⟧
[ end ] p >>>= q = q _
[ recv P ] p >>>= q = λ m → [ P m ] p m >>>= λ log → q (m , log)
[ send P ] (m , p) >>>= q = m , [ P m ] p >>>= λ log → q (m , log)
[_]_>>>ᴸ_ : ∀ P {Q} → ⟦ P ⟧ → (Log P → ⟦ Q ⟧) → ⟦ P >> Q ⟧
[ P ] p >>>ᴸ q = [ P ] p >>>= q
[_]_>>>_ : ∀ P {Q} → ⟦ P ⟧ → ⟦ Q ⟧ → ⟦ P >> Q ⟧
[ P ] p >>> q = [ P ] p >>>ᴸ (const q)
module _ {{_ : FunExt}} where
>>=-fst-inv : ∀ P {Q}(p : ⟦ P ⟧)(q : ((log : Log P) → ⟦ Q log ⟧)) → >>=-fst P {Q} ([ P ] p >>>= q) ≡ p
>>=-fst-inv end end q = refl
>>=-fst-inv (recv P) p q = λ= λ m → >>=-fst-inv (P m) (p m) λ log → q (m , log)
>>=-fst-inv (send P) (m , p) q = snd= (>>=-fst-inv (P m) p λ log → q (m , log))
>>=-snd-inv : ∀ P {Q}(p : ⟦ P ⟧)(q : ((log : Log P) → ⟦ Q log ⟧))(p' : ⟦ P ⊥⟧)
→ tr (λ x → ⟦ Q (telecom P x p') ⟧) (>>=-fst-inv P p q)
(>>=-snd P {Q} ([ P ] p >>>= q) p') ≡ q (telecom P p p')
>>=-snd-inv end end q p' = refl
>>=-snd-inv (recv P) {Q} p q (m , p') =
ap (flip _$_ (>>=-snd (P m) ([ P m ] p m >>>= (λ log → q (m , log))) p'))
(tr-λ= (λ z → ⟦ Q (m , telecom (P m) z p') ⟧)
(λ m → >>=-fst-inv (P m) {Q ∘ _,_ m} (p m) (q ∘ _,_ m)))
∙ >>=-snd-inv (P m) {Q ∘ _,_ m} (p m) (q ∘ _,_ m) p'
>>=-snd-inv (send P) {Q} (m , p) q p' = tr-snd= (λ { (m , p) → ⟦ Q (m , telecom (P m) p (p' m)) ⟧ })
(>>=-fst-inv (P m) p (q ∘ _,_ m))
(>>=-snd (P m) {Q ∘ _,_ m} ([ P m ] p >>>= (q ∘ _,_ m)) (p' m))
∙ >>=-snd-inv (P m) {Q ∘ _,_ m} p (λ log → q (m , log)) (p' m)
{- hmmm...
>>=-uniq : ∀ P {Q} (pq : ⟦ P >>= Q ⟧)(p' : ⟦ P ⊥⟧) → pq ≡ [ P ] (>>=-fst P {Q} pq) >>>= (λ log → {!>>=-snd P {Q} pq p'!})
>>=-uniq = {!!}
-}
replicateᴾ : ℕ → Proto → Proto
replicateᴾ 0 P = end
replicateᴾ (suc n) P = P >> replicateᴾ n P
replicate-proc : ∀ n P → ⟦ P ⟧ → ⟦ replicateᴾ n P ⟧
replicate-proc zero P p = end
replicate-proc (suc n) P p = [ P ] p >>> replicate-proc n P p
module _ {{_ : FunExt}}{{_ : UA}} where
Log->>=-Σ : ∀ (P : Proto){Q} → Log (P >>= Q) ≡ Σ (Log P) (Log ∘ Q)
Log->>=-Σ end = ! (×= End-uniq refl ∙ 𝟙×-snd)
Log->>=-Σ (com _ P) = Σ=′ _ (λ m → Log->>=-Σ (P m)) ∙ Σ-assoc
Log->>-× : ∀ (P : Proto){Q} → Log (P >> Q) ≡ (Log P × Log Q)
Log->>-× P = Log->>=-Σ P
++Log' : ∀ (P : Proto){Q : Log P → Proto} (xs : Log P) → Log (Q xs) → Log (P >>= Q)
++Log' P p q = coe! (Log->>=-Σ P) (p , q)
-- Same as ++Log' but computes better
++Log : ∀ (P : Proto){Q : Log P → Proto} (xs : Log P) → Log (Q xs) → Log (P >>= Q)
++Log end _ ys = ys
++Log (com q P) (x , xs) ys = x , ++Log (P x) xs ys
module _ {{_ : FunExt}}{{_ : UA}} where
++Log≡++Log' : ∀ P {Q} xs ys → ++Log P {Q} xs ys ≡ ++Log' P xs ys
++Log≡++Log' end xs ys
= ! coe-β _ _
∙ ap (coe 𝟙×-snd) (! coe×= End-uniq refl)
∙ coe∘coe 𝟙×-snd (×= End-uniq refl) (xs , ys)
∙ coe-same (! (!-inv (×= End-uniq refl ∙ 𝟙×-snd))) (xs , ys)
++Log≡++Log' (com io P) (m , xs) ys
= ap (_,_ m) (++Log≡++Log' (P m) xs ys)
∙ ! coe!Σ=′ _ (λ m → Log->>=-Σ (P m))
∙ ap (coe (! Σ=′ _ (λ m₁ → Log->>=-Σ (P m₁)))) (! coe!-β _ _)
∙ coe∘coe (! Σ=′ _ (λ m → Log->>=-Σ (P m))) (! Σ-assoc) ((m , xs) , ys)
∙ coe-same (! (hom-!-∙ (Σ=′ _ (λ m → Log->>=-Σ (P m))) Σ-assoc)) ((m , xs) , ys)
module _ {{_ : FunExt}} where
>>-right-unit : ∀ P → (P >> end) ≡ P
>>-right-unit end = refl
>>-right-unit (com q P) = com= refl refl λ m → >>-right-unit (P m)
>>=-assoc : ∀ (P : Proto)(Q : Log P → Proto)(R : Log (P >>= Q) → Proto)
→ (P >>= (λ x → Q x >>= (λ y → R (++Log P x y)))) ≡ ((P >>= Q) >>= R)
>>=-assoc end Q R = refl
>>=-assoc (com q P) Q R = com= refl refl λ m → >>=-assoc (P m) (λ ms → Q (m , ms)) (λ ms → R (m , ms))
ap->>= : ∀ P {Q₀ Q₁} → (∀ {log} → ⟦ Q₀ log ⟧ ≡ ⟦ Q₁ log ⟧) → ⟦ P >>= Q₀ ⟧ ≡ ⟦ P >>= Q₁ ⟧
ap->>= end Q= = Q=
ap->>= (send P) Q= = Σ=′ _ λ m → ap->>= (P m) Q=
ap->>= (recv P) Q= = Π=′ _ λ m → ap->>= (P m) Q=
dual->> : ∀ P Q → dual (P >> Q) ≡ (dual P >> dual Q)
dual->> end Q = refl
dual->> (com io P) Q = com= refl refl λ m → dual->> (P m) Q
{- My coe-ap-fu is lacking...
dual->>= : ∀ P (Q : Log P → Proto) → dual (P >>= Q) ≡ dual P >>= (dual ∘ Q ∘ coe (dual-Log P))
dual->>= end Q = refl
dual->>= (com io P) Q = com= refl refl λ m → dual->>= (P m) (Q ∘ _,_ m) ∙ ap (_>>=_ (dual (P m))) (λ= λ ms → ap (λ x → dual (Q x)) (pair= {!!} {!!}))
-}
dual-replicateᴾ : ∀ n P → dual (replicateᴾ n P) ≡ replicateᴾ n (dual P)
dual-replicateᴾ zero P = refl
dual-replicateᴾ (suc n) P = dual->> P (replicateᴾ n P) ∙ ap (_>>_ (dual P)) (dual-replicateᴾ n P)
{- An incremental telecom function which makes processes communicate
during a matching initial protocol. -}
>>=-telecom : (P : Proto){Q : Log P → Proto}{R : Log P → Proto}
→ ⟦ P >>= Q ⟧
→ ⟦ P >>= R ⊥⟧
→ Σ (Log P) (λ t → ⟦ Q t ⟧ × ⟦ R t ⊥⟧)
>>=-telecom end p0 p1 = _ , p0 , p1
>>=-telecom (send P) (m , p0) p1 = first (_,_ m) (>>=-telecom (P m) p0 (p1 m))
>>=-telecom (recv P) p0 (m , p1) = first (_,_ m) (>>=-telecom (P m) (p0 m) p1)
>>-telecom : (P : Proto){Q R : Proto}
→ ⟦ P >> Q ⟧
→ ⟦ P >> R ⊥⟧
→ Log P × ⟦ Q ⟧ × ⟦ R ⊥⟧
>>-telecom P p q = >>=-telecom P p q
>>-compose : (P Q : Proto){R : Proto}
→ ⟦ P >> Q ⟧
→ ⟦ dual Q >> R ⟧
→ ⟦ P >> R ⟧
>>-compose end end p>>q q>>r = q>>r
>>-compose end (send P) (m , p>>q) q>>r = >>-compose end (P m) p>>q (q>>r m)
>>-compose end (recv P) p>>q (m , q>>r) = >>-compose end (P m) (p>>q m) q>>r
>>-compose (send P) Q (m , p>>q) q>>r = m , >>-compose (P m) Q p>>q q>>r
>>-compose (recv P) Q p>>q q>>r m = >>-compose (P m) Q (p>>q m) q>>r
|
add more composision with >>
|
add more composision with >>
|
Agda
|
bsd-3-clause
|
crypto-agda/protocols
|
11dd97138b62b839e6eff70a2897426660d4fd10
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set
apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base
diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
ValidChange : Type → Set
ValidChange τ = Triple
⟦ τ ⟧
(λ _ → Change τ)
(λ v dv → valid {τ} v dv)
Change (base ι) = Change-base ι
Change (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : Change σ) → valid v dv → Change τ
ignore-valid-change : ValidChange ⊆ ⟦_⟧
ignore-valid-change (cons v _ _) = v
update-valid-change : ValidChange ⊆ ⟦_⟧
update-valid-change {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
n ⊞₍ base ι ₎ Δn = apply-change-base ι n Δn
f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
m ⊟₍ base ι ₎ n = diff-change-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : Change σ) (R[v,Δv] : valid v Δv)
→ valid {τ} (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v}
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞₍ σ ₎ Δw
in
R[v,u-v] {τ}
,
(begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ΔEnv : Context → Set
ΔEnv = DependentList ValidChange
ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
ignore = map (λ {τ} → ignore-valid-change {τ})
update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
update = map (λ {τ} → update-valid-change {τ})
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set
apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base
diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
ValidChange : Type → Set
ValidChange τ = Triple
⟦ τ ⟧
(λ _ → Change τ)
(λ v dv → valid {τ} v dv)
Change (base ι) = Change-base ι
Change (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : Change σ) → valid v dv → Change τ
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 v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
m ⊟₍ base ι ₎ n = diff-change-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : Change σ) (R[v,Δv] : valid v Δv)
→ valid {τ} (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v}
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞₍ σ ₎ Δw
in
R[v,u-v] {τ}
,
(begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ΔEnv : Context → Set
ΔEnv = DependentList ValidChange
ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
ignore = map (λ {τ} → before {τ})
update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
update = map (λ {τ} → after {τ})
|
Rename (ignore|update)-valid-change to (before|after).
|
Rename (ignore|update)-valid-change to (before|after).
Old-commit-hash: 16288a40704567fce8f03420af8050326b9b9b34
|
Agda
|
mit
|
inc-lc/ilc-agda
|
75429bb69a022fe800c050d4e53fdc2a9ccc9261
|
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 names. For example,
-- after this file type checks (C-C C-L), placing the cursor
-- on the purple "Base.Change.Algebra" and pressing M-. will
-- bring you to the file where change structures are defined.
-- The name for change structures in that file is
-- "ChangeAlgebra", given in the using-clause.
-- Definition 2.1 (Change structures)
open import Base.Change.Algebra using (ChangeAlgebra)
---- Carrier in record ChangeAlgebra --(a)
open Base.Change.Algebra.ChangeAlgebra using (Change) --(b)
open Base.Change.Algebra.ChangeAlgebra using (update) --(c)
open Base.Change.Algebra.ChangeAlgebra using (diff) --(d)
open Base.Change.Algebra.IsChangeAlgebra using (update-diff)--(e)
-- Definition 2.2 (Nil change)
-- IsChangeAlgebra.nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Lemma 2.3 (Behavior of nil)
-- IsChangeAlgebra.update-nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Definition 2.4 (Derivatives)
open Base.Change.Algebra using (Derivative)
-- Definition 2.5 (Carrier set of function changes)
open Base.Change.Algebra.FunctionChanges
-- Definition 2.6 (Operations on function changes)
-- ChangeAlgebra.update FunctionChanges.changeAlgebra
-- ChangeAlgebra.diff FunctionChanges.changeAlgebra
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Theorem 2.7 (Function changes form a change structure)
-- (In Agda, the proof of Theorem 2.7 has to be included in the
-- definition of function changes, here
-- FunctionChanges.changeAlgebra.)
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Lemma 2.8 (Incrementalization)
open Base.Change.Algebra.FunctionChanges using (incrementalization)
-- Theorem 2.9 (Nil changes are derivatives)
open Base.Change.Algebra.FunctionChanges using (nil-is-derivative)
-- For each plugin requirement, we include its definition and
-- a concrete instantiation called "Nehemiah" 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 Nehemiah.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 Nehemiah.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 Nehemiah.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 Nehemiah.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 Nehemiah.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)
|
module PLDI14-List-of-Theorems where
-- List of theorems in PLDI submission
--
-- For hints about installation and execution, please refer
-- to README.agda.
--
-- Agda modules corresponding to definitions, lemmas and theorems
-- are listed here with the most important names. For example,
-- after this file type checks (C-C C-L), placing the cursor
-- on the purple "Base.Change.Algebra" and pressing M-. will
-- bring you to the file where change structures are defined.
-- The name for change structures in that file is
-- "ChangeAlgebra", given in the using-clause.
-- Definition 2.1 (Change structures)
open import Base.Change.Algebra using (ChangeAlgebra)
---- Carrier in record ChangeAlgebra --(a)
open Base.Change.Algebra.ChangeAlgebra using (Change) --(b)
open Base.Change.Algebra.ChangeAlgebra using (update) --(c)
open Base.Change.Algebra.ChangeAlgebra using (diff) --(d)
open Base.Change.Algebra.IsChangeAlgebra using (update-diff)--(e)
-- Definition 2.2 (Nil change)
-- IsChangeAlgebra.nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Lemma 2.3 (Behavior of nil)
-- IsChangeAlgebra.update-nil
open Base.Change.Algebra using (IsChangeAlgebra)
-- Definition 2.4 (Derivatives)
open Base.Change.Algebra using (Derivative)
-- Definition 2.5 (Carrier set of function changes)
open Base.Change.Algebra.FunctionChanges
-- Definition 2.6 (Operations on function changes)
-- ChangeAlgebra.update FunctionChanges.changeAlgebra
-- ChangeAlgebra.diff FunctionChanges.changeAlgebra
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Theorem 2.7 (Function changes form a change structure)
-- (In Agda, the proof of Theorem 2.7 has to be included in the
-- definition of function changes, here
-- FunctionChanges.changeAlgebra.)
open Base.Change.Algebra.FunctionChanges using (changeAlgebra)
-- Lemma 2.8 (Incrementalization)
open Base.Change.Algebra.FunctionChanges using (incrementalization)
-- Theorem 2.9 (Nil changes are derivatives)
open Base.Change.Algebra.FunctionChanges using (nil-is-derivative)
-- Definition 3.1 (Domains)
import Parametric.Denotation.Value
open Parametric.Denotation.Value.Structure using (⟦_⟧Type)
-- Definition 3.2 (Environments)
open import Base.Denotation.Environment using (⟦_⟧Context)
-- Definition 3.3 (Evaluation)
import Parametric.Denotation.Evaluation
open Parametric.Denotation.Evaluation.Structure using (⟦_⟧Term)
-- Definition 3.4 (Changes)
-- Definition 3.5 (Change environments)
import Parametric.Change.Validity
open Parametric.Change.Validity.Structure using (change-algebra)
open Parametric.Change.Validity.Structure using (environment-changes)
-- Definition 3.6 (Change semantics)
-- Lemma 3.7 (Change semantics is the derivative of semantics)
import Parametric.Change.Specification
open Parametric.Change.Specification.Structure using (⟦_⟧Δ)
open Parametric.Change.Specification.Structure using (correctness)
-- Definition 3.8 (Erasure)
-- Lemma 3.9 (The erased version of a change is almost the same)
import Parametric.Change.Implementation
open Parametric.Change.Implementation.Structure using (_≈_)
open Parametric.Change.Implementation.Structure using (carry-over)
-- Lemma 3.10 (⟦ t ⟧Δ erases to Derive(t))
-- Theorem 3.11 (Correctness of differentiation)
import Parametric.Change.Correctness
open Parametric.Change.Correctness.Structure using (derive-correct-closed)
open Parametric.Change.Correctness.Structure using (main-theorem)
|
Update PLDI14-List-of-Theorems.agda (for #275).
|
Update PLDI14-List-of-Theorems.agda (for #275).
The list included plugin requirements, but we left them out of the
paper, so the theorem numbers were off.
Old-commit-hash: d4ea643c7474f10c798fcb0d494db9f941e3deb5
|
Agda
|
mit
|
inc-lc/ilc-agda
|
ef3f46045d85fef7b038b4703fc7d2fee8d29a8a
|
Denotational/Equivalence.agda
|
Denotational/Equivalence.agda
|
module Denotational.Equivalence where
-- TERM EQUIVALENCE
--
-- This module defines term equivalence as the relation that
-- identifies terms with the same meaning.
open import Relation.Nullary using (¬_)
open import Relation.Binary hiding (_⇒_)
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Changes
open import ChangeContexts
-- TERMS
-- Term Equivalence
module _ {Γ} {τ} where
data _≈_ (t₁ t₂ : Term Γ τ) : Set where
ext-t :
(∀ ρ → ⟦ t₁ ⟧ ρ ≡ ⟦ t₂ ⟧ ρ) →
t₁ ≈ t₂
≈-refl : Reflexive _≈_
≈-refl = ext-t (λ ρ → ≡-refl)
≈-sym : Symmetric _≈_
≈-sym (ext-t ≈) = ext-t (λ ρ → ≡-sym (≈ ρ))
≈-trans : Transitive _≈_
≈-trans (ext-t ≈₁) (ext-t ≈₂) = ext-t (λ ρ → ≡-trans (≈₁ ρ) (≈₂ ρ))
≈-isEquivalence : IsEquivalence _≈_
≈-isEquivalence = record
{ refl = ≈-refl
; sym = ≈-sym
; trans = ≈-trans
}
≈-setoid : Context → Type → Setoid _ _
≈-setoid Γ τ = record
{ Carrier = Term Γ τ
; _≈_ = _≈_
; isEquivalence = ≈-isEquivalence
}
≈-app : ∀ {Γ τ₁ τ₂} {t₁ t₂ : Term Γ (τ₁ ⇒ τ₂)} {t₃ t₄ : Term Γ τ₁} →
t₁ ≈ t₂ → t₃ ≈ t₄ → app t₁ t₃ ≈ app t₂ t₄
≈-app (ext-t ≈₁) (ext-t ≈₂) = ext-t (λ ρ →
≡-cong₂ (λ x y → x y) (≈₁ ρ) (≈₂ ρ))
≈-abs : ∀ {Γ τ₁ τ₂} {t₁ t₂ : Term (τ₁ • Γ) τ₂} →
t₁ ≈ t₂ → abs t₁ ≈ abs t₂
≈-abs (ext-t ≈) = ext-t (λ ρ →
ext (λ v → ≈ (v • ρ)))
≈-Δ : ∀ {τ Γ} {t₁ t₂ : Term Γ τ} →
t₁ ≈ t₂ → Δ t₁ ≈ Δ t₂
≈-Δ (ext-t ≈) = ext-t (λ ρ → ≡-diff (≈ (update ρ)) (≈ (ignore ρ)))
module ≈-Reasoning where
module _ {Γ : Context} {τ : Type} where
open import Relation.Binary.EqReasoning (≈-setoid Γ τ) public
hiding (_≡⟨_⟩_)
≈-consistent : ¬ (∀ {Γ τ} (t₁ t₂ : Term Γ τ) → t₁ ≈ t₂)
≈-consistent H with H {∅} true false
... | ext-t x with x ∅
... | ()
|
module Denotational.Equivalence where
-- TERM EQUIVALENCE
--
-- This module defines term equivalence as the relation that
-- identifies terms with the same meaning.
open import Relation.Nullary using (¬_)
open import Relation.Binary hiding (_⇒_)
open import Relation.Binary.PropositionalEquality
open import Denotational.Notation
-- TERMS
-- Term Equivalence
--
-- This module is parametric in the syntax and semantics of types
-- and terms to make it work for different calculi.
module _ where
open import Syntactic.Contexts
open import Denotational.Environments
module MakeEquivalence
(Type : Set)
(⟦_⟧Type : Type → Set)
(Term : Context Type → Type → Set)
(⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦_⟧Context Type ⟦_⟧Type Γ → ⟦ τ ⟧Type) where
module _ {Γ} {τ} where
data _≈_ (t₁ t₂ : Term Γ τ) : Set where
ext-t :
(∀ ρ → ⟦ t₁ ⟧Term ρ ≡ ⟦ t₂ ⟧Term ρ) →
t₁ ≈ t₂
≈-refl : Reflexive _≈_
≈-refl = ext-t (λ ρ → refl)
≈-sym : Symmetric _≈_
≈-sym (ext-t ≈) = ext-t (λ ρ → sym (≈ ρ))
≈-trans : Transitive _≈_
≈-trans (ext-t ≈₁) (ext-t ≈₂) = ext-t (λ ρ → trans (≈₁ ρ) (≈₂ ρ))
≈-isEquivalence : IsEquivalence _≈_
≈-isEquivalence = record
{ refl = ≈-refl
; sym = ≈-sym
; trans = ≈-trans
}
≈-setoid : Context Type → Type → Setoid _ _
≈-setoid Γ τ = record
{ Carrier = Term Γ τ
; _≈_ = _≈_
; isEquivalence = ≈-isEquivalence
}
module ≈-Reasoning where
module _ {Γ : Context Type} {τ : Type} where
open import Relation.Binary.EqReasoning (≈-setoid Γ τ) public
hiding (_≡⟨_⟩_)
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Changes
open import ChangeContexts
-- Export a version of the equivalence for terms with total
-- derivatives
open MakeEquivalence Type ⟦_⟧ Term ⟦_⟧ public
-- Specialized congruence rules
≈-app : ∀ {Γ τ₁ τ₂} {t₁ t₂ : Term Γ (τ₁ ⇒ τ₂)} {t₃ t₄ : Term Γ τ₁} →
t₁ ≈ t₂ → t₃ ≈ t₄ → app t₁ t₃ ≈ app t₂ t₄
≈-app (ext-t ≈₁) (ext-t ≈₂) = ext-t (λ ρ →
≡-cong₂ (λ x y → x y) (≈₁ ρ) (≈₂ ρ))
≈-abs : ∀ {Γ τ₁ τ₂} {t₁ t₂ : Term (τ₁ • Γ) τ₂} →
t₁ ≈ t₂ → abs t₁ ≈ abs t₂
≈-abs (ext-t ≈) = ext-t (λ ρ →
ext (λ v → ≈ (v • ρ)))
≈-Δ : ∀ {τ Γ} {t₁ t₂ : Term Γ τ} →
t₁ ≈ t₂ → Δ t₁ ≈ Δ t₂
≈-Δ (ext-t ≈) = ext-t (λ ρ → ≡-diff (≈ (update ρ)) (≈ (ignore ρ)))
-- Consistency
≈-consistent : ¬ (∀ {Γ τ} (t₁ t₂ : Term Γ τ) → t₁ ≈ t₂)
≈-consistent H with H {∅} true false
... | ext-t x with x ∅
... | ()
|
Generalize term equivalence to arbitrary calculi.
|
Generalize term equivalence to arbitrary calculi.
Old-commit-hash: 239a4be82a8af884669ca62db624782bee407ca1
|
Agda
|
mit
|
inc-lc/ilc-agda
|
90e62144b22dcc8d6181e110e5f313f406aff57f
|
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₂
----------------------------------------------------------------------
{-
An alternative to the Arith codes is to create a *view* of the
natural numbers along with a collection of functions over them.
-}
data ArithView : ℕ → Set where
`nat : (n : ℕ) → ArithView n
_`+_ : (n₁ n₂ : ℕ) → ArithView (n₁ + n₂)
_`*_ : (n₁ n₂ : ℕ) → ArithView (n₁ * n₂)
----------------------------------------------------------------------
{-
Φ (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
----------------------------------------------------------------------
{-
Here is the ArithView specialized into the Fin type.
-}
data Fin₃ : ℕ → Set where
fin : (n : ℕ) (i : Fin n) → Fin₃ n
fin+ : (n₁ n₂ : ℕ) (i : Fin (n₁ + n₂)) → Fin₃ (n₁ + n₂)
fin* : (n₁ n₂ : ℕ) (i : Fin (n₁ * n₂)) → Fin₃ (n₁ * n₂)
----------------------------------------------------------------------
{-
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
`ArithView `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
`Fin₃ : (n : ℕ) → Type
`Π `Σ : (A : Type) (B : ⟦ A ⟧ → Type) → Type
`Id : (A : Type) (x y : ⟦ A ⟧) → Type
⟦ `Bool ⟧ = Bool
⟦ `ℕ ⟧ = ℕ
⟦ `Arith ⟧ = Arith
⟦ `ArithView n ⟧ = ArithView n
⟦ `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
⟦ `Fin₃ n ⟧ = Fin₃ n
⟦ `Π 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 that simplifies an ArithView index.
To target Fin, ideally you want to match on something like:
`Σ `ℕ (λ n → `ArithView n `× Fin n)
But this requires matching on a λ, and a non-linear occurrence
of n.
The example after this one accomplishes the same thing via the
specialized Fin₃ type instead.
-}
rm-plus0-ArithView : Tactic
rm-plus0-ArithView (`ArithView .(n + 0) , (n `+ 0)) =
`ArithView n , `nat n
rm-plus0-ArithView x = x
----------------------------------------------------------------------
{-
A tactic to simplify to simplify the "view"-based Fin type Fin₃.
This is most similar to rm-plus0-Fin₂, except Fin₃ is view-based
while Fin₂ is universe-based (where the universe is the codes of
possible operations).
On one hand this Fin₃ approach is simpler than Fin₂, because you do
not need to write an eval function or tell it to reduce definitional
equalities explicitly (like you would to get Arith to compute).
On the other hand, the Fin₂ approach is more powerful because it
allows you to control when things reduce. This is more like Coq
tactics, where you might not want something to reduce and you use
"simpl" explicitly when you do. The "simpl" tactic is defined below
via the eval₂ function.
-}
rm-plus0-Fin₃ : Tactic
rm-plus0-Fin₃ (`Fin₃ .(n + 0) , fin+ n 0 i) =
`Fin₃ n
,
fin n (subst Fin (sym (plusrident n)) i)
rm-plus0-Fin₃ x = x
----------------------------------------------------------------------
{-
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
simp-step-n-plus0-Fin : ℕ → Tactic
simp-step-n-plus0-Fin zero x = x
simp-step-n-plus0-Fin (suc n) x =
simp-step-n-plus0-Fin n (simp-step-plus0-Fin 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
----------------------------------------------------------------------
|
{-
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₂
----------------------------------------------------------------------
{-
An alternative to the Arith codes is to create a *view* of the
natural numbers along with a collection of functions over them.
-}
data ArithView : ℕ → Set where
`nat : (n : ℕ) → ArithView n
_`+_ : (n₁ n₂ : ℕ) → ArithView (n₁ + n₂)
_`*_ : (n₁ n₂ : ℕ) → ArithView (n₁ * n₂)
----------------------------------------------------------------------
{-
Φ (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 with Fin.
-}
data Fin₂ (a : Arith) : Set where
fin : (i : Fin (eval a)) → Fin₂ a
----------------------------------------------------------------------
{-
Here is the ArithView specialized with the Fin type.
-}
data Fin₃ : ℕ → Set where
fin : (n : ℕ) (i : Fin n) → Fin₃ n
fin+ : (n₁ n₂ : ℕ) (i : Fin (n₁ + n₂)) → Fin₃ (n₁ + n₂)
fin* : (n₁ n₂ : ℕ) (i : Fin (n₁ * n₂)) → Fin₃ (n₁ * n₂)
----------------------------------------------------------------------
{-
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
`ArithView `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
`Fin₃ : (n : ℕ) → Type
`Π `Σ : (A : Type) (B : ⟦ A ⟧ → Type) → Type
`Id : (A : Type) (x y : ⟦ A ⟧) → Type
⟦ `Bool ⟧ = Bool
⟦ `ℕ ⟧ = ℕ
⟦ `Arith ⟧ = Arith
⟦ `ArithView n ⟧ = ArithView n
⟦ `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
⟦ `Fin₃ n ⟧ = Fin₃ n
⟦ `Π 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 that simplifies an ArithView index.
To target Fin, ideally you want to match on something like:
`Σ `ℕ (λ n → `ArithView n `× Fin n)
But this requires matching on a λ, and a non-linear occurrence
of n.
The example after this one accomplishes the same thing via the
specialized Fin₃ type instead.
-}
rm-plus0-ArithView : Tactic
rm-plus0-ArithView (`ArithView .(n + 0) , (n `+ 0)) =
`ArithView n , `nat n
rm-plus0-ArithView x = x
----------------------------------------------------------------------
{-
A tactic to simplify to simplify the "view"-based Fin type Fin₃.
This is most similar to rm-plus0-Fin₂, except Fin₃ is view-based
while Fin₂ is universe-based (where the universe is the codes of
possible operations).
On one hand this Fin₃ approach is simpler than Fin₂, because you do
not need to write an eval function or tell it to reduce definitional
equalities explicitly (like you would to get Arith to compute).
On the other hand, the Fin₂ approach is more powerful because it
allows you to control when things reduce. This is more like Coq
tactics, where you might not want something to reduce and you use
"simpl" explicitly when you do. The "simpl" tactic is defined below
via the eval₂ function.
-}
rm-plus0-Fin₃ : Tactic
rm-plus0-Fin₃ (`Fin₃ .(n + 0) , fin+ n 0 i) =
`Fin₃ n
,
fin n (subst Fin (sym (plusrident n)) i)
rm-plus0-Fin₃ x = x
----------------------------------------------------------------------
{-
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
simp-step-n-plus0-Fin : ℕ → Tactic
simp-step-n-plus0-Fin zero x = x
simp-step-n-plus0-Fin (suc n) x =
simp-step-n-plus0-Fin n (simp-step-plus0-Fin 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
----------------------------------------------------------------------
|
change wording
|
change wording
|
Agda
|
bsd-3-clause
|
spire/spire
|
224bef483393cb10b22b2f51c63865cc5ddf471f
|
partial.agda
|
partial.agda
|
module partial where
-- INCREMENTAL λ-CALCULUS
-- with (one-variable) partial derivatives
--
-- Features:
-- * Changes and derivatives are different.
-- * Δ x e maps changes of x to changes of e.
-- * a full denotational semantics is given.
-- * the domain of change types `⟦Δ τ⟧` is uniformly defined as `⟦τ⟧ → ⟦τ⟧`.
open import Relation.Binary.PropositionalEquality
open import meaning
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
-- `Δ τ` is the type of changes to `τ`
Δ : (τ : Type) → Type
infixr 5 _⇒_
-- Denotational Semantics
⟦_⟧Type : Type -> Set
⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type
⟦ Δ τ ⟧Type = ⟦ τ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning Set ⟦_⟧Type
-- TYPING CONTEXTS, VARIABLES and WEAKENING
open import binding Type ⟦_⟧Type
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- `Δ x dx t` maps changes `dx` in `x` to changes in `t`
Δ : ∀ {Γ τ₁ τ₂} → (x : Var Γ τ₁) (dx : Var Γ (Δ τ₁)) → (t : 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 ⟧ ρ
⟦ Δ x dx t ⟧Term ρ = λ _ → ⟦ t ⟧Term (update x (⟦ dx ⟧ ρ) ρ)
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm {Γ} {τ} = meaning (⟦ Γ ⟧ → ⟦ τ ⟧) ⟦_⟧Term
-- NATURAL SEMANTICS
-- (without support for Δ for now)
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv {Γ} = meaning ⟦ Γ ⟧ ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal {τ} = meaning ⟦ τ ⟧ ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = refl
↓-sound (app ↓₁ ↓₂ ↓′) = trans (cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂)) (↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
-- 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 {Γ₁} {Γ₂} (Δ x dx t) = Δ (lift {Γ₁} {Γ₂} x) (lift {Γ₁} {Γ₂} dx) (weaken {Γ₁} {Γ₂} t)
|
module partial where
-- INCREMENTAL λ-CALCULUS
-- with (one-variable) partial derivatives
--
-- Features:
-- * Changes and derivatives are different.
-- * Δ x dx e describes how e changes if x changes by dx.
-- * a full denotational semantics is given.
-- * the domain of change types `⟦Δ τ⟧` is uniformly defined as `⟦τ⟧ → ⟦τ⟧`.
open import Relation.Binary.PropositionalEquality
open import meaning
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
-- `Δ τ` is the type of changes to `τ`
Δ : (τ : Type) → Type
infixr 5 _⇒_
-- Denotational Semantics
⟦_⟧Type : Type -> Set
⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type
⟦ Δ τ ⟧Type = ⟦ τ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning Set ⟦_⟧Type
-- TYPING CONTEXTS, VARIABLES and WEAKENING
open import binding Type ⟦_⟧Type
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- `Δ x dx t` maps changes `dx` in `x` to changes in `t`
Δ : ∀ {Γ τ₁ τ₂} → (x : Var Γ τ₁) (dx : Var Γ (Δ τ₁)) → (t : 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 ⟧ ρ
⟦ Δ x dx t ⟧Term ρ = λ _ → ⟦ t ⟧Term (update x (⟦ dx ⟧ ρ) ρ)
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm {Γ} {τ} = meaning (⟦ Γ ⟧ → ⟦ τ ⟧) ⟦_⟧Term
-- NATURAL SEMANTICS
-- (without support for Δ for now)
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv {Γ} = meaning ⟦ Γ ⟧ ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal {τ} = meaning ⟦ τ ⟧ ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = refl
↓-sound (app ↓₁ ↓₂ ↓′) = trans (cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂)) (↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
-- 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 {Γ₁} {Γ₂} (Δ x dx t) = Δ (lift {Γ₁} {Γ₂} x) (lift {Γ₁} {Γ₂} dx) (weaken {Γ₁} {Γ₂} t)
|
Fix comment (fix #20).
|
Fix comment (fix #20).
Old-commit-hash: d5a7ef44c7c38bf82aa00466ee0bac803dd860d3
|
Agda
|
mit
|
inc-lc/ilc-agda
|
64c7aa44ab0d86c66f744241c812cc27ab7fb2c6
|
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)
instance
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
}
}
-- Encode infix ascription.
as' : ∀ {ℓ} (A : Set ℓ) (a : A) → A
as' _ a = a
syntax as' A a = a as A
inj₁′ : RawChange (inj₁ as (X → (X ⊎ Y)))
inj₁′ x dx = ch₁ dx
inj₁′Derivative : IsDerivative (inj₁ as (X → (X ⊎ Y))) inj₁′
inj₁′Derivative x dx = refl
inj₂′ : RawChange (inj₂ as (Y → (X ⊎ Y)))
inj₂′ y dy = ch₂ dy
inj₂′Derivative : IsDerivative (inj₂ as (Y → (X ⊎ Y))) 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
instance
X→Z = FunctionChanges.changeAlgebra X Z {{CX}} {{CZ}}
--module ΔX→Z = FunctionChanges X Z {{CX}} {{CZ}}
Y→Z = FunctionChanges.changeAlgebra Y Z {{CY}} {{CZ}}
--module ΔY→Z = FunctionChanges Y Z {{CY}} {{CZ}}
X⊎Y→Z = FunctionChanges.changeAlgebra (X ⊎ Y) Z {{changeAlgebra}} {{CZ}}
Y→Z→X⊎Y→Z = FunctionChanges.changeAlgebra (Y → Z) (X ⊎ Y → Z) {{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 }
|
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
-- This is an indexed datatype, so it has two constructors per argument. But
-- erasure would probably not be smart enough to notice.
-- Should we rewrite this as two separate datatypes?
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)
instance
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
}
}
-- Encode infix ascription.
as' : ∀ {ℓ} (A : Set ℓ) (a : A) → A
as' _ a = a
syntax as' A a = a as A
inj₁′ : RawChange (inj₁ as (X → (X ⊎ Y)))
inj₁′ x dx = ch₁ dx
inj₁′Derivative : IsDerivative (inj₁ as (X → (X ⊎ Y))) inj₁′
inj₁′Derivative x dx = refl
inj₂′ : RawChange (inj₂ as (Y → (X ⊎ Y)))
inj₂′ y dy = ch₂ dy
inj₂′Derivative : IsDerivative (inj₂ as (Y → (X ⊎ Y))) 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
instance
X→Z = FunctionChanges.changeAlgebra X Z {{CX}} {{CZ}}
--module ΔX→Z = FunctionChanges X Z {{CX}} {{CZ}}
Y→Z = FunctionChanges.changeAlgebra Y Z {{CY}} {{CZ}}
--module ΔY→Z = FunctionChanges Y Z {{CY}} {{CZ}}
X⊎Y→Z = FunctionChanges.changeAlgebra (X ⊎ Y) Z {{changeAlgebra}} {{CZ}}
Y→Z→X⊎Y→Z = FunctionChanges.changeAlgebra (Y → Z) (X ⊎ Y → Z) {{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 }
|
Add TODO
|
Add TODO
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e0b2a2036942b7b9010a4d1e45e94ed00a26742d
|
Atlas/Change/Term.agda
|
Atlas/Change/Term.agda
|
module Atlas.Change.Term where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
-- nil-changes
nil-const : ∀ {ι : Base} → Const ∅ (base (ΔBase ι))
nil-const {ι} = neutral {ΔBase ι}
nil-term : ∀ {ι Γ} → Term Γ (base (ΔBase ι))
nil-term {Bool} = curriedConst (nil-const {Bool})
nil-term {Map κ ι} = curriedConst (nil-const {Map κ ι})
-- diff-term and apply-term
open import Parametric.Change.Term Const ΔBase
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))
Atlas-diff {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
Atlas-diff {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs Atlas-diff) m₁ m₂)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
Atlas-apply {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs Atlas-apply) m₁ m₂)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ (lift-diff Atlas-diff Atlas-apply)
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ (lift-apply Atlas-diff Atlas-apply)
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
insert : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
-- last argument is the change accumulator
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
|
module Atlas.Change.Term where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
-- nil-changes
nil-const : ∀ {ι : Base} → Const ∅ (base (ΔBase ι))
nil-const {ι} = neutral {ΔBase ι}
nil-term : ∀ {ι Γ} → Term Γ (base (ΔBase ι))
nil-term {Bool} = curriedConst (nil-const {Bool})
nil-term {Map κ ι} = curriedConst (nil-const {Map κ ι})
-- diff-term and apply-term
open import Parametric.Change.Term Const ΔBase
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
diff-base : ∀ {ι Γ} →
Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))
diff-base {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
diff-base {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs diff-base) m₁ m₂)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = abs₂ (λ b₁ b₂ → xor! b₁ b₂)
Atlas-apply {Map κ ι} = abs₂ (λ m₁ m₂ → zip! (abs Atlas-apply) m₁ m₂)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ (lift-diff diff-base Atlas-apply)
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ (lift-apply diff-base Atlas-apply)
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
insert : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
-- last argument is the change accumulator
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Term Γ (base κ) → Term Γ (base ι) →
Term Γ (ΔType (base (Map κ ι))) →
Term Γ (ΔType (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
|
Rename Atlas-diff to diff-base.
|
Rename Atlas-diff to diff-base.
Old-commit-hash: 8940b78431520f22d625a114d12ce0faee05542c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
5a499d733fe3968065e0ae2b47d1393cf5980209
|
lib/Data/Two.agda
|
lib/Data/Two.agda
|
{-# OPTIONS --without-K #-}
module Data.Two where
open import Data.Bool public hiding (if_then_else_) renaming (Bool to 𝟚; false to 0₂; true to 1₂; T to ✓)
open import Data.Bool.Properties
public
using (isCommutativeSemiring-∨-∧
;commutativeSemiring-∨-∧
;module RingSolver
;isCommutativeSemiring-∧-∨
;commutativeSemiring-∧-∨
;isBooleanAlgebra
;booleanAlgebra
;commutativeRing-xor-∧
;module XorRingSolver
;not-involutive
;not-¬
;¬-not
;⇔→≡
;proof-irrelevance)
renaming
(T-≡ to ✓-≡
;T-∧ to ✓-∧
;T-∨ to ✓-∨)
open import Type using (★_)
open import Algebra using (module CommutativeRing; module CommutativeSemiring)
open import Algebra.FunctionProperties using (Op₁; Op₂)
open import Data.Nat using (ℕ; _≤_; z≤n; s≤s; _⊓_; _⊔_; _∸_)
open import Data.Zero using (𝟘-elim)
open import Data.One using (𝟙)
open import Data.Product using (proj₁; proj₂; uncurry; _×_; _,_)
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Function.Equivalence using (module Equivalence)
open import Function.Equality using (_⟨$⟩_)
open import Function.NP using (id; _∘_; _⟨_⟩°_)
open import Relation.Binary.PropositionalEquality.NP using (_≡_; _≢_; refl; idp; _∙_; !_)
open import Relation.Nullary using (¬_; Dec; yes; no)
open Equivalence using (to; from)
module Xor° = CommutativeRing commutativeRing-xor-∧
module 𝟚° = CommutativeSemiring commutativeSemiring-∧-∨
module _ {p} {P : 𝟚 → ★ p} where
[0:_1:_] : P 0₂ → P 1₂ → (b : 𝟚) → P b
[0: e₀ 1: e₁ ] 0₂ = e₀
[0: e₀ 1: e₁ ] 1₂ = e₁
tabulate₂ : ((b : 𝟚) → P b) → P 0₂ × P 1₂
tabulate₂ f = f 0₂ , f 1₂
η-[0:1:] : ∀ (f : (b : 𝟚) → P b) b → [0: f 0₂ 1: f 1₂ ] b ≡ f b
η-[0:1:] f 0₂ = refl
η-[0:1:] f 1₂ = refl
proj : P 0₂ × P 1₂ → (b : 𝟚) → P b
proj = uncurry [0:_1:_]
proj-tabulate₂ : ∀ (f : (b : 𝟚) → P b) b → proj (tabulate₂ f) b ≡ f b
proj-tabulate₂ = η-[0:1:]
module _ {a} {A : ★ a} where
[0:_1:_]′ : A → A → 𝟚 → A
[0:_1:_]′ = [0:_1:_]
case_0:_1:_ : 𝟚 → A → A → A
case b 0: e₀ 1: e₁ = [0: e₀
1: e₁ ] b
proj′ : A × A → 𝟚 → A
proj′ = proj
proj[_] : 𝟚 → A × A → A
proj[_] = [0: proj₁ 1: proj₂ ]
mux : 𝟚 × (A × A) → A
mux (s , eᵢ) = proj eᵢ s
nor : (b₀ b₁ : 𝟚) → 𝟚
nor b₀ b₁ = not (b₀ ∨ b₁)
nand : (b₀ b₁ : 𝟚) → 𝟚
nand b₀ b₁ = not (b₀ ∧ b₁)
-- For properties about _==_ see Data.Two.Equality
_==_ : (b₀ b₁ : 𝟚) → 𝟚
b₀ == b₁ = (not b₀) xor b₁
≡→✓ : ∀ {b} → b ≡ 1₂ → ✓ b
≡→✓ refl = _
≡→✓not : ∀ {b} → b ≡ 0₂ → ✓ (not b)
≡→✓not refl = _
✓→≡ : ∀ {b} → ✓ b → b ≡ 1₂
✓→≡ {1₂} _ = refl
✓→≡ {0₂} ()
✓not→≡ : ∀ {b} → ✓ (not b) → b ≡ 0₂
✓not→≡ {0₂} _ = refl
✓not→≡ {1₂} ()
✓∧ : ∀ {b₁ b₂} → ✓ b₁ → ✓ b₂ → ✓ (b₁ ∧ b₂)
✓∧ p q = _⟨$⟩_ (from ✓-∧) (p , q)
✓∧₁ : ∀ {b₁ b₂} → ✓ (b₁ ∧ b₂) → ✓ b₁
✓∧₁ = proj₁ ∘ _⟨$⟩_ (to ✓-∧)
✓∧₂ : ∀ {b₁ b₂} → ✓ (b₁ ∧ b₂) → ✓ b₂
✓∧₂ {b₁} = proj₂ ∘ _⟨$⟩_ (to (✓-∧ {b₁}))
✓∨-⊎ : ∀ {b₁ b₂} → ✓ (b₁ ∨ b₂) → ✓ b₁ ⊎ ✓ b₂
✓∨-⊎ {b₁} = _⟨$⟩_ (to (✓-∨ {b₁}))
✓∨₁ : ∀ {b₁ b₂} → ✓ b₁ → ✓ (b₁ ∨ b₂)
✓∨₁ = _⟨$⟩_ (from ✓-∨) ∘ inj₁
✓∨₂ : ∀ {b₁ b₂} → ✓ b₂ → ✓ (b₁ ∨ b₂)
✓∨₂ {b₁} = _⟨$⟩_ (from (✓-∨ {b₁})) ∘ inj₂
✓-not-¬ : ∀ {b} → ✓ (not b) → ¬ (✓ b)
✓-not-¬ {0₂} _ = λ()
✓-not-¬ {1₂} ()
✓-¬-not : ∀ {b} → ¬ (✓ b) → ✓ (not b)
✓-¬-not {0₂} _ = _
✓-¬-not {1₂} f = f _
∧⇒∨ : ∀ x y → ✓ (x ∧ y) → ✓ (x ∨ y)
∧⇒∨ 0₂ _ = λ ()
∧⇒∨ 1₂ _ = _
✓dec : ∀ b → Dec (✓ b)
✓dec = [0: no (λ())
1: yes _ ]
de-morgan : ∀ x y → not (x ∨ y) ≡ not x ∧ not y
de-morgan 0₂ _ = refl
de-morgan 1₂ _ = refl
≢0→≡1 : ∀ {x} → x ≢ 0₂ → x ≡ 1₂
≢0→≡1 {1₂} p = refl
≢0→≡1 {0₂} p = 𝟘-elim (p refl)
≢1→≡0 : ∀ {x} → x ≢ 1₂ → x ≡ 0₂
≢1→≡0 {0₂} p = refl
≢1→≡0 {1₂} p = 𝟘-elim (p refl)
-- 0₂ is 0 and 1₂ is 1
𝟚▹ℕ : 𝟚 → ℕ
𝟚▹ℕ = [0: 0
1: 1 ]
𝟚▹ℕ≤1 : ∀ b → 𝟚▹ℕ b ≤ 1
𝟚▹ℕ≤1 = [0: z≤n
1: s≤s z≤n ]
𝟚▹ℕ-⊓ : ∀ a b → 𝟚▹ℕ a ⊓ 𝟚▹ℕ b ≡ 𝟚▹ℕ (a ∧ b)
𝟚▹ℕ-⊓ 1₂ 0₂ = refl
𝟚▹ℕ-⊓ 1₂ 1₂ = refl
𝟚▹ℕ-⊓ 0₂ _ = refl
𝟚▹ℕ-⊔ : ∀ a b → 𝟚▹ℕ a ⊔ 𝟚▹ℕ b ≡ 𝟚▹ℕ (a ∨ b)
𝟚▹ℕ-⊔ 1₂ 0₂ = refl
𝟚▹ℕ-⊔ 1₂ 1₂ = refl
𝟚▹ℕ-⊔ 0₂ _ = refl
𝟚▹ℕ-∸ : ∀ a b → 𝟚▹ℕ a ∸ 𝟚▹ℕ b ≡ 𝟚▹ℕ (a ∧ not b)
𝟚▹ℕ-∸ 0₂ 0₂ = refl
𝟚▹ℕ-∸ 0₂ 1₂ = refl
𝟚▹ℕ-∸ 1₂ 0₂ = refl
𝟚▹ℕ-∸ 1₂ 1₂ = refl
xor-not-not : ∀ x y → (not x) xor (not y) ≡ x xor y
xor-not-not 0₂ y = not-involutive y
xor-not-not 1₂ _ = refl
not-inj : ∀ {x y} → not x ≡ not y → x ≡ y
not-inj {0₂} {0₂} _ = refl
not-inj {1₂} {1₂} _ = refl
not-inj {0₂} {1₂} ()
not-inj {1₂} {0₂} ()
xor-inj₁ : ∀ x {y z} → (x xor y) ≡ (x xor z) → y ≡ z
xor-inj₁ 0₂ = id
xor-inj₁ 1₂ = not-inj
xor-inj₂ : ∀ x {y z} → (y xor x) ≡ (z xor x) → y ≡ z
xor-inj₂ x {y} {z} p = xor-inj₁ x (Xor°.+-comm x y ∙ p ∙ Xor°.+-comm z x)
check : ∀ b → {pf : ✓ b} → 𝟙
check = _
module Indexed {a} {A : ★ a} where
_∧°_ : Op₂ (A → 𝟚)
x ∧° y = x ⟨ _∧_ ⟩° y
_∨°_ : Op₂ (A → 𝟚)
x ∨° y = x ⟨ _∨_ ⟩° y
_xor°_ : Op₂ (A → 𝟚)
x xor° y = x ⟨ _xor_ ⟩° y
_==°_ : Op₂ (A → 𝟚)
x ==° y = x ⟨ _==_ ⟩° y
not° : Op₁ (A → 𝟚)
not° f = not ∘ f
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
module Data.Two where
open import Data.Bool public hiding (if_then_else_) renaming (Bool to 𝟚; false to 0₂; true to 1₂; T to ✓)
open import Data.Bool.Properties
public
using (isCommutativeSemiring-∨-∧
;commutativeSemiring-∨-∧
;module RingSolver
;isCommutativeSemiring-∧-∨
;commutativeSemiring-∧-∨
;isBooleanAlgebra
;booleanAlgebra
;commutativeRing-xor-∧
;module XorRingSolver
;not-involutive
;not-¬
;¬-not
;⇔→≡
;proof-irrelevance)
renaming
(T-≡ to ✓-≡
;T-∧ to ✓-∧
;T-∨ to ✓-∨)
open import Type using (★_)
open import Algebra using (module CommutativeRing; module CommutativeSemiring)
open import Algebra.FunctionProperties using (Op₁; Op₂)
open import Data.Nat using (ℕ; _≤_; z≤n; s≤s; _⊓_; _⊔_; _∸_)
open import Data.Zero using (𝟘-elim)
open import Data.One using (𝟙)
open import Data.Product using (proj₁; proj₂; uncurry; _×_; _,_)
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Function.Equivalence using (module Equivalence)
open import Function.Equality using (_⟨$⟩_)
open import Function.NP using (id; _∘_; _⟨_⟩°_)
open import Relation.Binary.PropositionalEquality.NP using (_≡_; _≢_; refl; idp; _∙_; !_)
open import Relation.Nullary using (¬_; Dec; yes; no)
open Equivalence using (to; from)
module Xor° = CommutativeRing commutativeRing-xor-∧
module 𝟚° = CommutativeSemiring commutativeSemiring-∧-∨
_² : ∀ {a} → ★ a → ★ a
A ² = 𝟚 → A
module _ {p} {P : 𝟚 → ★ p} where
[0:_1:_] : P 0₂ → P 1₂ → (b : 𝟚) → P b
[0: e₀ 1: e₁ ] 0₂ = e₀
[0: e₀ 1: e₁ ] 1₂ = e₁
tabulate₂ : ((b : 𝟚) → P b) → P 0₂ × P 1₂
tabulate₂ f = f 0₂ , f 1₂
η-[0:1:] : ∀ (f : (b : 𝟚) → P b) b → [0: f 0₂ 1: f 1₂ ] b ≡ f b
η-[0:1:] f 0₂ = refl
η-[0:1:] f 1₂ = refl
proj : P 0₂ × P 1₂ → (b : 𝟚) → P b
proj = uncurry [0:_1:_]
proj-tabulate₂ : ∀ (f : (b : 𝟚) → P b) b → proj (tabulate₂ f) b ≡ f b
proj-tabulate₂ = η-[0:1:]
module _ {a} {A : ★ a} where
[0:_1:_]′ : A → A → A ²
[0:_1:_]′ = [0:_1:_]
case_0:_1:_ : 𝟚 → A → A → A
case b 0: e₀ 1: e₁ = [0: e₀
1: e₁ ] b
proj′ : A × A → A ²
proj′ = proj
proj[_] : 𝟚 → A × A → A
proj[_] = [0: proj₁ 1: proj₂ ]
mux : 𝟚 × (A × A) → A
mux (s , eᵢ) = proj eᵢ s
nor : (b₀ b₁ : 𝟚) → 𝟚
nor b₀ b₁ = not (b₀ ∨ b₁)
nand : (b₀ b₁ : 𝟚) → 𝟚
nand b₀ b₁ = not (b₀ ∧ b₁)
-- For properties about _==_ see Data.Two.Equality
_==_ : (b₀ b₁ : 𝟚) → 𝟚
b₀ == b₁ = (not b₀) xor b₁
≡→✓ : ∀ {b} → b ≡ 1₂ → ✓ b
≡→✓ refl = _
≡→✓not : ∀ {b} → b ≡ 0₂ → ✓ (not b)
≡→✓not refl = _
✓→≡ : ∀ {b} → ✓ b → b ≡ 1₂
✓→≡ {1₂} _ = refl
✓→≡ {0₂} ()
✓not→≡ : ∀ {b} → ✓ (not b) → b ≡ 0₂
✓not→≡ {0₂} _ = refl
✓not→≡ {1₂} ()
✓∧ : ∀ {b₁ b₂} → ✓ b₁ → ✓ b₂ → ✓ (b₁ ∧ b₂)
✓∧ p q = _⟨$⟩_ (from ✓-∧) (p , q)
✓∧₁ : ∀ {b₁ b₂} → ✓ (b₁ ∧ b₂) → ✓ b₁
✓∧₁ = proj₁ ∘ _⟨$⟩_ (to ✓-∧)
✓∧₂ : ∀ {b₁ b₂} → ✓ (b₁ ∧ b₂) → ✓ b₂
✓∧₂ {b₁} = proj₂ ∘ _⟨$⟩_ (to (✓-∧ {b₁}))
✓∨-⊎ : ∀ {b₁ b₂} → ✓ (b₁ ∨ b₂) → ✓ b₁ ⊎ ✓ b₂
✓∨-⊎ {b₁} = _⟨$⟩_ (to (✓-∨ {b₁}))
✓∨₁ : ∀ {b₁ b₂} → ✓ b₁ → ✓ (b₁ ∨ b₂)
✓∨₁ = _⟨$⟩_ (from ✓-∨) ∘ inj₁
✓∨₂ : ∀ {b₁ b₂} → ✓ b₂ → ✓ (b₁ ∨ b₂)
✓∨₂ {b₁} = _⟨$⟩_ (from (✓-∨ {b₁})) ∘ inj₂
✓-not-¬ : ∀ {b} → ✓ (not b) → ¬ (✓ b)
✓-not-¬ {0₂} _ = λ()
✓-not-¬ {1₂} ()
✓-¬-not : ∀ {b} → ¬ (✓ b) → ✓ (not b)
✓-¬-not {0₂} _ = _
✓-¬-not {1₂} f = f _
∧⇒∨ : ∀ x y → ✓ (x ∧ y) → ✓ (x ∨ y)
∧⇒∨ 0₂ _ = λ ()
∧⇒∨ 1₂ _ = _
✓dec : ∀ b → Dec (✓ b)
✓dec = [0: no (λ())
1: yes _ ]
de-morgan : ∀ x y → not (x ∨ y) ≡ not x ∧ not y
de-morgan 0₂ _ = refl
de-morgan 1₂ _ = refl
≢0→≡1 : ∀ {x} → x ≢ 0₂ → x ≡ 1₂
≢0→≡1 {1₂} p = refl
≢0→≡1 {0₂} p = 𝟘-elim (p refl)
≢1→≡0 : ∀ {x} → x ≢ 1₂ → x ≡ 0₂
≢1→≡0 {0₂} p = refl
≢1→≡0 {1₂} p = 𝟘-elim (p refl)
-- 0₂ is 0 and 1₂ is 1
𝟚▹ℕ : 𝟚 → ℕ
𝟚▹ℕ = [0: 0
1: 1 ]
𝟚▹ℕ≤1 : ∀ b → 𝟚▹ℕ b ≤ 1
𝟚▹ℕ≤1 = [0: z≤n
1: s≤s z≤n ]
𝟚▹ℕ-⊓ : ∀ a b → 𝟚▹ℕ a ⊓ 𝟚▹ℕ b ≡ 𝟚▹ℕ (a ∧ b)
𝟚▹ℕ-⊓ 1₂ 0₂ = refl
𝟚▹ℕ-⊓ 1₂ 1₂ = refl
𝟚▹ℕ-⊓ 0₂ _ = refl
𝟚▹ℕ-⊔ : ∀ a b → 𝟚▹ℕ a ⊔ 𝟚▹ℕ b ≡ 𝟚▹ℕ (a ∨ b)
𝟚▹ℕ-⊔ 1₂ 0₂ = refl
𝟚▹ℕ-⊔ 1₂ 1₂ = refl
𝟚▹ℕ-⊔ 0₂ _ = refl
𝟚▹ℕ-∸ : ∀ a b → 𝟚▹ℕ a ∸ 𝟚▹ℕ b ≡ 𝟚▹ℕ (a ∧ not b)
𝟚▹ℕ-∸ 0₂ 0₂ = refl
𝟚▹ℕ-∸ 0₂ 1₂ = refl
𝟚▹ℕ-∸ 1₂ 0₂ = refl
𝟚▹ℕ-∸ 1₂ 1₂ = refl
xor-not-not : ∀ x y → (not x) xor (not y) ≡ x xor y
xor-not-not 0₂ y = not-involutive y
xor-not-not 1₂ _ = refl
not-inj : ∀ {x y} → not x ≡ not y → x ≡ y
not-inj {0₂} {0₂} _ = refl
not-inj {1₂} {1₂} _ = refl
not-inj {0₂} {1₂} ()
not-inj {1₂} {0₂} ()
xor-inj₁ : ∀ x {y z} → (x xor y) ≡ (x xor z) → y ≡ z
xor-inj₁ 0₂ = id
xor-inj₁ 1₂ = not-inj
xor-inj₂ : ∀ x {y z} → (y xor x) ≡ (z xor x) → y ≡ z
xor-inj₂ x {y} {z} p = xor-inj₁ x (Xor°.+-comm x y ∙ p ∙ Xor°.+-comm z x)
check : ∀ b → {pf : ✓ b} → 𝟙
check = _
module Indexed {a} {A : ★ a} where
_∧°_ : Op₂ (A → 𝟚)
x ∧° y = x ⟨ _∧_ ⟩° y
_∨°_ : Op₂ (A → 𝟚)
x ∨° y = x ⟨ _∨_ ⟩° y
_xor°_ : Op₂ (A → 𝟚)
x xor° y = x ⟨ _xor_ ⟩° y
_==°_ : Op₂ (A → 𝟚)
x ==° y = x ⟨ _==_ ⟩° y
not° : Op₁ (A → 𝟚)
not° f = not ∘ f
-- -}
-- -}
-- -}
|
add _²
|
Data.Two: add _²
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
6ad96baab8d36671b146b356a5a446ae10e1b832
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra as CA using
( ChangeAlgebra
; ChangeAlgebraFamily
; change-algebra₍_₎
; family
)
open import Level
Structure : Set₁
Structure = ChangeAlgebraFamily zero ⟦_⟧Base
module Structure (change-algebra-base : Structure) where
change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}}
change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type
change-algebra-family = family change-algebra
----------------
-- Parameters --
----------------
Change-base : (ι : Base) → ⟦ ι ⟧Base → Set
Change-base = CA.Δ₍_₎
diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v
diff-change-base = CA.diff′
---------------
-- Interface --
---------------
open CA public
using
(
)
renaming
( Δ₍_₎ to Change
; update′ to apply-change
; diff′ to diff-change
; nil₍_₎ to nil-change
)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
v+[u-v]=u {τ} {u} {v} = CA.update-diff₍ τ ₎ u v
--------------------
-- Implementation --
--------------------
module _ {τ₁ τ₂ : Type} where
open CA.FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public
using
(
)
renaming
( correct to is-valid
; apply to call-change
)
open CA public using
( before
; after
; before₍_₎
; after₍_₎
)
------------------
-- Environments --
------------------
open CA.FunctionChanges public using (cons)
open CA public using () renaming
( [] to ∅
; _∷_ to _•_
)
open CA.ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebra to environment-changes
)
ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set
ΔEnv Γ ρ = CA.Δ₍ Γ ₎ ρ
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍ Γ ₎
apply-env : ∀ Γ → (ρ : ⟦ Γ ⟧) → (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
apply-env = CA.update′
diff-env : ∀ Γ → (π ρ : ⟦ Γ ⟧) → ΔEnv Γ ρ
diff-env = CA.diff′
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra as CA using
( ChangeAlgebra
; ChangeAlgebraFamily
; change-algebra₍_₎
; family
)
open import Level
Structure : Set₁
Structure = ChangeAlgebraFamily zero ⟦_⟧Base
module Structure (change-algebra-base : Structure) where
change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}}
change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type
change-algebra-family = family change-algebra
----------------
-- Parameters --
----------------
Change-base : (ι : Base) → ⟦ ι ⟧Base → Set
Change-base = CA.Δ₍_₎
diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v
diff-change-base = CA.diff′
---------------
-- Interface --
---------------
open CA public
using
(
)
renaming
( Δ₍_₎ to Change
; update′ to apply-change
; diff′ to diff-change
; nil₍_₎ to nil-change
)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
v+[u-v]=u {τ} {u} {v} = CA.update-diff₍ τ ₎ u v
--------------------
-- Implementation --
--------------------
module _ {τ₁ τ₂ : Type} where
open CA.FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public
using
(
)
renaming
( correct to is-valid
; apply to call-change
)
open CA public using
( before
; after
; before₍_₎
; after₍_₎
)
------------------
-- Environments --
------------------
open CA.FunctionChanges public using (cons)
open CA public using () renaming
( [] to ∅
; _∷_ to _•_
)
open CA.ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebra to environment-changes
)
ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set
ΔEnv Γ ρ = CA.Δ₍ Γ ₎ ρ
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍ Γ ₎
|
Remove apply-env and diff-env.
|
Remove apply-env and diff-env.
Old-commit-hash: 0901643e49a0e4ff70db8f16278c25bd28348d3a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
973a194f1e408a138980b8d9e4156302e223bc75
|
Thesis/SIRelBigStep/DeriveCorrect.agda
|
Thesis/SIRelBigStep/DeriveCorrect.agda
|
--
-- In this module we conclude our proof: we have shown that t, derive-dterm t
-- and produce related values (in the sense of the fundamental property,
-- rfundamental3), and for related values v1, dv and v2 we have v1 ⊕ dv ≡
-- v2 (because ⊕ agrees with validity, rrelV3→⊕).
--
-- We now put these facts together via derive-correct-si and derive-correct.
-- This is immediate, even though I spend so much code on it:
-- Indeed, all theorems are immediate corollaries of what we established (as
-- explained above); only the statements are longer, especially because I bother
-- expanding them.
module Thesis.SIRelBigStep.DeriveCorrect where
open import Data.Product
open import Relation.Binary.PropositionalEquality
open import Thesis.SIRelBigStep.IlcSILR
open import Thesis.SIRelBigStep.FundamentalProperty
-- Theorem statement. This theorem still mentions step-indexes explicitly.
derive-correct-si-type =
∀ {τ Γ k} (t : Term Γ τ) ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
rrelT3-skeleton (λ v1 dv v2 _ → v1 ⊕ dv ≡ v2) t (derive-dterm t) t ρ1 dρ ρ2 k
-- A verified expansion of the theorem statement.
derive-correct-si-type-means :
derive-correct-si-type ≡
∀ {τ Γ k} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
(v1 v2 : Val τ) →
∀ j (j<k : j < k) →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct-si-type-means = refl
derive-correct-si : derive-correct-si-type
derive-correct-si t ρ1 dρ ρ2 ρρ = rrelT3→⊕ t (derive-dterm t) t ρ1 dρ ρ2 (rfundamental3 _ t ρ1 dρ ρ2 ρρ)
-- Infinitely related environments:
rrelρ3-inf : ∀ Γ (ρ1 : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) (ρ2 : ⟦ Γ ⟧Context) → Set
rrelρ3-inf Γ ρ1 dρ ρ2 = ∀ k → rrelρ3 Γ ρ1 dρ ρ2 k
-- A theorem statement for infinitely-related environments. On paper, if we informally
-- omit the arbitrary derivation step-index as usual, we get a statement without
-- step-indexes.
derive-correct :
∀ {τ Γ} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3-inf Γ ρ1 dρ ρ2) →
(v1 v2 : Val τ) →
∀ j →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct t ρ1 dρ ρ2 ρρ v1 v2 j = derive-correct-si t ρ1 dρ ρ2 (ρρ (suc j)) v1 v2 j ≤-refl
|
--
-- In this module we conclude our proof: we have shown that t, derive-dterm t
-- and produce related values (in the sense of the fundamental property,
-- rfundamental3), and for related values v1, dv and v2 we have v1 ⊕ dv ≡
-- v2 (because ⊕ agrees with validity, rrelV3→⊕).
--
-- We now put these facts together via derive-correct-si and derive-correct.
-- This is immediate, even though I spend so much code on it:
-- Indeed, all theorems are immediate corollaries of what we established (as
-- explained above); only the statements are longer, especially because I bother
-- expanding them.
module Thesis.SIRelBigStep.DeriveCorrect where
open import Data.Product
open import Relation.Binary.PropositionalEquality
open import Thesis.SIRelBigStep.IlcSILR
open import Thesis.SIRelBigStep.FundamentalProperty
-- Theorem statement. This theorem still mentions step-indexes explicitly.
derive-correct-si-type =
∀ {τ Γ k} (t : Term Γ τ) ρ1 dρ ρ2 → (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
rrelT3-skeleton (λ v1 dv v2 _ → v1 ⊕ dv ≡ v2) t (derive-dterm t) t ρ1 dρ ρ2 k
-- A verified expansion of the theorem statement.
derive-correct-si-type-means :
derive-correct-si-type ≡
∀ {τ Γ k} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3 Γ ρ1 dρ ρ2 k) →
(v1 v2 : Val τ) →
∀ j (j<k : j < k) →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct-si-type-means = refl
derive-correct-si : derive-correct-si-type
derive-correct-si t ρ1 dρ ρ2 ρρ = rrelT3→⊕ t (derive-dterm t) t ρ1 dρ ρ2 (rfundamental3 _ t ρ1 dρ ρ2 ρρ)
-- Infinitely related environments:
rrelρ3-inf : ∀ Γ (ρ1 : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) (ρ2 : ⟦ Γ ⟧Context) → Set
rrelρ3-inf Γ ρ1 dρ ρ2 = ∀ k → rrelρ3 Γ ρ1 dρ ρ2 k
-- A theorem statement for infinitely-related environments. On paper, if we informally
-- omit the arbitrary derivation step-index as usual, we get a statement without
-- step-indexes.
derive-correct :
∀ {τ Γ} (t : Term Γ τ)
ρ1 dρ ρ2 (ρρ : rrelρ3-inf Γ ρ1 dρ ρ2) →
(v1 v2 : Val τ) →
∀ j →
(ρ1⊢t1↓[j]v1 : ρ1 ⊢ t ↓[ i' j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 ⊢ t ↓[ no ] v2) →
Σ[ dv ∈ DVal τ ]
ρ1 D dρ ⊢ derive-dterm t ↓ dv ×
v1 ⊕ dv ≡ v2
derive-correct t ρ1 dρ ρ2 ρρ v1 v2 j = derive-correct-si t ρ1 dρ ρ2 (ρρ (suc j)) v1 v2 j ≤-refl
open import Data.Unit
nilρ : ∀ {Γ n} (ρ : ⟦ Γ ⟧Context) → Σ[ dρ ∈ ChΔ Γ ] rrelρ3 Γ ρ dρ ρ n
nilV : ∀ {τ n} (v : Val τ) → Σ[ dv ∈ DVal τ ] rrelV3 τ v dv v n
nilV (closure t ρ) = let dρ , ρρ = nilρ ρ in dclosure (derive-dterm t) ρ dρ , rfundamental3svv _ (abs t) ρ dρ ρ ρρ
nilV (natV n₁) = dnatV zero , refl
nilV (pairV a b) = let 0a , aa = nilV a; 0b , bb = nilV b in dpairV 0a 0b , aa , bb
nilρ ∅ = ∅ , tt
nilρ (v • ρ) = let dv , vv = nilV v ; dρ , ρρ = nilρ ρ in dv • dρ , vv , ρρ
|
Define a correct nil on values and environments
|
Define a correct nil on values and environments
Compose is trickier because I must match on two validity proofs, need an
inversion lemma for that.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
1da0c206672cf5880079e99b71f1940b6df8932e
|
lambda.agda
|
lambda.agda
|
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
if : ∀ {Γ τ} → (t₁ : Term Γ bool) (t₂ t₃ : 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
⟦ if 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
if-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} →
ρ ⊢ t₁ ↓ true →
ρ ⊢ t₂ ↓ v₂ →
ρ ⊢ if t₁ t₂ t₃ ↓ v₂
if-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} →
ρ ⊢ t₁ ↓ false →
ρ ⊢ t₃ ↓ v₃ →
ρ ⊢ if 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 (if-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂
↓-sound (if-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃
-- WEAKENING
-- Weaken a term to a super context
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 ≼₁ true = true
weaken ≼₁ false = false
weaken ≼₁ (if e₁ e₂ e₃) = if (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
if : ∀ {Γ τ} → (t₁ : Term Γ bool) (t₂ t₃ : 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
⟦ if 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
if-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} →
ρ ⊢ t₁ ↓ true →
ρ ⊢ t₂ ↓ v₂ →
ρ ⊢ if t₁ t₂ t₃ ↓ v₂
if-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} →
ρ ⊢ t₁ ↓ false →
ρ ⊢ t₃ ↓ v₃ →
ρ ⊢ if 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 (if-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂
↓-sound (if-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃
-- WEAKENING
-- Weaken a term to a super context
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 ≼₁ true = true
weaken ≼₁ false = false
weaken ≼₁ (if e₁ e₂ e₃) = if (weaken ≼₁ e₁) (weaken ≼₁ e₂) (weaken ≼₁ e₃)
-- SYMBOLIC EXECUTION
--
-- Naming Convention:
-- Γ ⟪_⟫X is like ⟦_⟧X but for symbolic execution in context Γ.
_⟪_⟫Type : Context → Type → Set
Γ₁ ⟪ τ₁ ⇒ τ₂ ⟫Type = ∀ {Γ₂} → Γ₁ ≼ Γ₂ → Γ₂ ⟪ τ₁ ⟫Type → Γ₂ ⟪ τ₂ ⟫Type
Γ₁ ⟪ bool ⟫Type = Term Γ₁ bool
module _ (Γ : Context) where
import Denotational.Environments
module SymEnv = Denotational.Environments Type (λ τ → Γ ⟪ τ ⟫Type)
open SymEnv public using ()
renaming (⟦_⟧Context to _⟪_⟫Context; ⟦_⟧Var to _⟪_⟫Var_)
liftVal : ∀ {τ Γ₁ Γ₂} → Γ₁ ≼ Γ₂ → Γ₁ ⟪ τ ⟫Type → Γ₂ ⟪ τ ⟫Type
liftVal {τ₁ ⇒ τ₂} ≼₁ v₁ = λ ≼₂ v₂ → v₁ (≼-trans ≼₁ ≼₂) v₂
liftVal {bool} ≼₁ v = weaken ≼₁ v
liftEnv : ∀ {Γ Γ₁ Γ₂} → Γ₁ ≼ Γ₂ → Γ₁ ⟪ Γ ⟫Context → Γ₂ ⟪ Γ ⟫Context
liftEnv {∅} ≼₁ ∅ = SymEnv.∅
liftEnv {τ • Γ} ≼₁ (v • ρ) = liftVal ≼₁ v SymEnv.• liftEnv ≼₁ ρ
mixed-if : ∀ {Γ₁} τ → (t₁ : Term Γ₁ bool) (v₂ v₃ : Γ₁ ⟪ τ ⟫Type) → Γ₁ ⟪ τ ⟫Type
mixed-if (τ₁ ⇒ τ₂) t₁ v₂ v₃ = λ ≼₁ v → mixed-if τ₂ (weaken ≼₁ t₁) (v₃ ≼₁ v) (v₃ ≼₁ v)
mixed-if bool t₁ t₂ t₃ = if t₁ t₂ t₃
_⟪_⟫Term_ : ∀ Γ₁ {Γ τ} → Term Γ τ → Γ₁ ⟪ Γ ⟫Context → Γ₁ ⟪ τ ⟫Type
Γ₁ ⟪ abs t ⟫Term ρ = λ {Γ₂} ≼₁ v → Γ₂ ⟪ t ⟫Term (v SymEnv.• liftEnv ≼₁ ρ)
Γ₁ ⟪ app t₁ t₂ ⟫Term ρ = (Γ₁ ⟪ t₁ ⟫Term ρ) ≼-refl (Γ₁ ⟪ t₂ ⟫Term ρ)
Γ₁ ⟪ var x ⟫Term ρ = Γ₁ ⟪ x ⟫Var ρ
Γ₁ ⟪ true ⟫Term ρ = true
Γ₁ ⟪ false ⟫Term ρ = false
Γ₁ ⟪ if t₁ t₂ t₃ ⟫Term ρ = mixed-if _ (Γ₁ ⟪ t₁ ⟫Term ρ) (Γ₁ ⟪ t₂ ⟫Term ρ) (Γ₁ ⟪ t₂ ⟫Term ρ)
↓ : ∀ {Γ} τ → Γ ⟪ τ ⟫Type → Term Γ τ
↑ : ∀ {Γ} τ → Term Γ τ → Γ ⟪ τ ⟫Type
↓ (τ₁ ⇒ τ₂) v = abs (↓ τ₂ (v (drop τ₁ • ≼-refl) (↑ τ₁ (var this))))
↓ bool v = v
↑ (τ₁ ⇒ τ₂) t = λ ≼₁ v → ↑ τ₂ (app (weaken ≼₁ t) (↓ τ₁ v))
↑ bool t = t
↑-Context : ∀ {Γ} → Γ ⟪ Γ ⟫Context
↑-Context {∅} = SymEnv.∅
↑-Context {τ • Γ} = ↑ τ (var this) SymEnv.• liftEnv (drop τ • ≼-refl) ↑-Context
norm : ∀ {Γ τ} → Term Γ τ → Term Γ τ
norm {Γ} {τ} t = ↓ τ (Γ ⟪ t ⟫Term ↑-Context)
|
Implement NbE for lambda calculus with conditionals (see #50).
|
Implement NbE for lambda calculus with conditionals (see #50).
This version is simpler than the code in NormalizationByEvaluation
in the following ways:
- Instead of supporting arbitrary base types and constants,
bool, true, false, and if are hard-coded.
- Instead of supporting arbitrary Kripke structures,
metacircular evaluation and symbolic evaluation are hard-coded.
Old-commit-hash: d25aa0871f3efecd4932029a007775f4c22568fb
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2bc86b16750cda8c1189ff286ba5405a47076a56
|
README.agda
|
README.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Machine-checked formalization of the theoretical results presented
-- in the paper:
--
-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.
-- A Theory of Changes for Higher-Order Languages:
-- Incrementalizing λ-Calculi by Static Differentiation.
-- To appear at PLDI, ACM 2014.
--
-- We claim that this formalization
--
-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,
-- (2) formally specifies the interface between our incrementalization
-- framework and potential plugins, and
-- (3) shows that the plugin interface can be instantiated.
--
-- The first claim is the main reason for a machine-checked
-- proof: We want to be sure that we got the proofs right.
--
-- The second claim is about reusability and applicability: Only
-- a clearly defined interface allows other researchers to
-- provide plugins for our framework.
--
-- The third claim is to show that the plugin interface is
-- consistent: An inconsistent plugin interface would allow to
-- prove arbitrary results in the framework.
------------------------------------------------------------------------
module README where
-- We know two good ways to read this code base. You can either
-- use Emacs with agda2-mode to interact with the source files,
-- or you can use a web browser to view the pretty-printed and
-- hyperlinked source files. For Agda power users, we also
-- include basic setup information for your own machine.
-- IF YOU WANT TO USE A BROWSER
-- ============================
--
-- Start with *HTML* version of this readme. On the AEC
-- submission website (online or inside the VM), follow the link
-- "view the Agda code in their browser" to the file
-- agda/README.html.
--
-- The source code is syntax highlighted and hyperlinked. You can
-- click on module names to open the corresponding files, or you
-- can click on identifiers to jump to their definition. In
-- general, a Agda file with name `Foo/Bar/Baz.agda` contains a
-- module `Foo.Bar.Baz` and is shown in an HTML file
-- `Foo.Bar.Baz.html`.
--
-- Note that we also include the HTML files generated for our
-- transitive dependencies from the Agda standard library. This
-- allows you to follow hyperlinks to the Agda standard
-- library. It is the default behavior of `agda --html` which we
-- used to generate the HTML.
--
-- To get started, continue below on "Where to start reading?".
-- IF YOU WANT TO USE EMACS
-- ========================
--
-- Open this file in Emacs with agda2-mode installed. See below
-- for which Agda version you need to install. On the VM image,
-- everything is setup for you and you can just open this file in
-- Emacs.
--
-- C-c C-l Load code. Type checks and syntax highlights. Use
-- this if the code is not syntax highlighted, or
-- after you changed something that you want to type
-- check.
--
-- M-. Jump to definition of identifier at the point.
-- M-* Jump back.
--
-- Note that README.agda imports Everything.agda which imports
-- every Agda file in our formalization. So if README.agda type
-- checks successfully, everything does. If you want to type
-- check everything from scratch, delete the *.agdai files to
-- disable separate compilation. You can use "find . -name
-- '*.agdai' | xargs rm" to do that.
--
-- More information on the Agda mode is available on the Agda wiki:
--
-- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode
-- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations
--
-- To get started, continue below on "Where to start reading?".
-- IF YOU WANT TO USE YOUR OWN SETUP
-- =================================
--
-- To typecheck this formalization, you need to install the appropriate version
-- of Agda, the Agda standard library (version 0.7), generate Everything.agda
-- with the attached Haskell helper, and finally run Agda on this file.
--
-- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh
-- script and following instructions given on output will eventually generate
-- Everything.agda and proceed to type check everything on command line.
--
-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but
-- has some bugs with serialization of code using some recent syntactic sugar
-- which we use (https://code.google.com/p/agda/issues/detail?id=756), so it
-- might work or not. When it does not, removing Agda caches (.agdai files)
-- appears to often help. You can use "find . -name '*.agdai' | xargs rm" to do
-- that.
--
-- If you're not an Agda power user, it is probably easier to use
-- the VM image or look at the pretty-printed and hyperlinked
-- HTML files, see above.
-- WHERE TO START READING?
-- =======================
--
-- modules.pdf
-- The graph of dependencies between Agda modules.
-- Good if you want to get a broad overview.
--
-- README.agda
-- This file. A coarse-grained introduction to the Agda
-- formalization. Good if you want to begin at the beginning
-- and understand the structure of our code.
--
-- PLDI14-List-of-Theorems.agda
-- Pointers to the Agda formalizations of all theorems, lemmas
-- or definitions in the PLDI paper. Good if you want to read
-- the paper and the Agda code side by side.
--
-- Here is an import of this file, so you can jump to it
-- directly (use M-. in Emacs or click the module name in the
-- Browser):
import PLDI14-List-of-Theorems
-- Everything.agda
-- Imports every Agda module in our formalization. Good if you
-- want to make sure you don't miss anything.
--
-- Again, here's is an import of this file so you can navigate
-- there:
import Everything
-- (This import is also important to ensure that if we typecheck
-- README.agda, we also typecheck every other file of our
-- formalization).
-- THE AGDA CODE
-- =============
--
-- 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 Agda version in README.agda
|
Update Agda version in README.agda
|
Agda
|
mit
|
inc-lc/ilc-agda
|
1fd2e308e6cbee61ce0c527c7a1c8fa86ae11694
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR ≙-setoid public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil x)
≡⟨ update-nil x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- We know that Derivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> Derivative f (apply df).
-- Now, we try to prove that if Derivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
Derivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil x))
≡⟨ ext (λ x → cong f (update-nil x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
Derivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- 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
|
Remove more dead code
|
Remove more dead code
Old-commit-hash: 20e82287ead4c99b0bce90e095ed1f7711119d2a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e87a9fc0946ded53de5e1775b46c12ba9a8ba6dc
|
ZK/GroupHom/Types.agda
|
ZK/GroupHom/Types.agda
|
{-# OPTIONS --without-K #-}
open import Type using (Type)
open import Data.Bool.Base using (Bool) renaming (T to ✓)
open import Relation.Binary.PropositionalEquality.NP using (_≡_)
open import Algebra.Group
open import Algebra.Group.Homomorphism
module ZK.GroupHom.Types where
record GrpHom (G+ G* : Type)(P : G+ → Type) : Type where
field
𝔾+ : Group G+
𝔾* : Group G*
_==_ : G* → G* → Bool
✓-== : ∀ {x y} → x ≡ y → ✓ (x == y)
==-✓ : ∀ {x y} → ✓ (x == y) → x ≡ y
open Additive-Group 𝔾+ hiding (_⊗_) public
open Multiplicative-Group 𝔾* hiding (_^_) public
field
φ : G+ → G*
φ-hom : GroupHomomorphism 𝔾+ 𝔾* φ
y : G*
φ⇒P : ∀ x → φ x ≡ y → P x
P⇒φ : ∀ x → P x → φ x ≡ y
|
{-# OPTIONS --without-K #-}
open import Type using (Type)
open import Type.Eq
open import Relation.Binary.PropositionalEquality.NP using (_≡_)
open import Algebra.Group
open import Algebra.Group.Homomorphism
module ZK.GroupHom.Types where
-- P is the "valid witness" predicate.
record ZK-hom (G+ G* : Type)(P : G+ → Type) : Type where
field
𝔾+ : Group G+
𝔾* : Group G*
{{G*-eq?}} : Eq? G*
open Eq? G*-eq?
open Additive-Group 𝔾+ hiding (_⊗_) public
open Multiplicative-Group 𝔾* hiding (_^_) public
field
φ : G+ → G*
φ-hom : GroupHomomorphism 𝔾+ 𝔾* φ
y : G*
φ⇒P : ∀ x → φ x ≡ y → P x
P⇒φ : ∀ x → P x → φ x ≡ y
-- -}
-- -}
-- -}
-- -}
|
use Eq?
|
ZK.GroupHom.Types: use Eq?
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
61adc205859b99f216096bf10da4b6ffe56adfc7
|
Base/Syntax/Context.agda
|
Base/Syntax/Context.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Variables and contexts
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
------------------------------------------------------------------------
module Base.Syntax.Context
(Type : Set)
where
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- Typing Contexts
-- ===============
import Data.List as List
open List public
using ()
renaming
( [] to ∅ ; _∷_ to _•_
; map to mapContext
)
Context : Set
Context = List.List Type
-- Variables
-- =========
--
-- Here it is clear that we are using de Bruijn indices,
-- encoded as natural numbers, more or less.
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ
-- Weakening
-- =========
--
-- We provide two approaches for weakening: context prefixes and
-- and subcontext relationship. In this formalization, we mostly
-- (maybe exclusively?) use the latter.
-- Context Prefixes
-- ----------------
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂)
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Subcontexts
-- -----------
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
module Subcontexts where
infix 4 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
weaken-var (keep τ • ≼₁) this = this
weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x)
weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Variables and contexts
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
------------------------------------------------------------------------
module Base.Syntax.Context
(Type : Set)
where
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- Typing Contexts
-- ===============
import Data.List as List
open List public
using ()
renaming
( [] to ∅ ; _∷_ to _•_
; map to mapContext
)
Context : Set
Context = List.List Type
-- Variables
-- =========
--
-- Here it is clear that we are using de Bruijn indices,
-- encoded as natural numbers, more or less.
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ
-- Weakening
-- =========
--
-- We provide two approaches for weakening: context prefixes and
-- and subcontext relationship. In this formalization, we mostly
-- (maybe exclusively?) use the latter.
-- Context Prefixes
-- ----------------
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
_⋎_ = List._++_
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Subcontexts
-- -----------
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
module Subcontexts where
infix 4 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
weaken-var (keep τ • ≼₁) this = this
weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x)
weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
Simplify _⋎_
|
Simplify _⋎_
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b93733dc8edf5e1dc3e416dfa0bca1405b5ae331
|
total.agda
|
total.agda
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Note that Δ is *not* the same as the ∂ operator in
-- definition/intro.tex. See discussion at:
--
-- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
open import Data.Product
open import Data.Unit
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Changes
open import ChangeContexts
-- DEFINITION of valid changes via a logical relation
{-
What I wanted to write:
data ValidΔ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where
base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv
fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) →
(∀ (s : ⟦ S ⟧) ds (valid : ValidΔ s ds) → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) →
ValidΔ f df
-}
-- What I had to write:
valid-Δ : {T : Type} → ⟦ T ⟧ → ⟦ Δ-Type T ⟧ → Set
valid-Δ {bool} v dv = ⊤
valid-Δ {S ⇒ T} f df = ∀ (s : ⟦ S ⟧) ds (valid-w : valid-Δ s ds) → (valid-Δ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))
-- LIFTING terms into Δ-Contexts
lift-term : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ τ
lift-term {Γ₁} {Γ₂} {{Γ′}} = weaken (≼-trans ≼-Δ-Context Γ′)
-- PROPERTIES of lift-term
lift-term-ignore : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} {ρ : ⟦ Γ₂ ⟧} (t : Term Γ₁ τ) →
⟦ lift-term {{Γ′}} t ⟧ ρ ≡ ⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ))
lift-term-ignore {{Γ′}} {ρ} t = let Γ″ = ≼-trans ≼-Δ-Context Γ′ in
begin
⟦ lift-term {{Γ′}} t ⟧ ρ
≡⟨⟩
⟦ weaken Γ″ t ⟧ ρ
≡⟨ weaken-sound t ρ ⟩
⟦ t ⟧ (⟦ ≼-trans ≼-Δ-Context Γ′ ⟧ ρ)
≡⟨ cong (λ x → ⟦ t ⟧ x) (⟦⟧-≼-trans ≼-Δ-Context Γ′ ρ) ⟩
⟦ t ⟧Term (⟦ ≼-Δ-Context ⟧≼ (⟦ Γ′ ⟧≼ ρ))
≡⟨⟩
⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ))
∎ where open ≡-Reasoning
-- PROPERTIES of Δ
Δ-abs : ∀ {τ₁ τ₂ Γ₁ Γ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t : Term (τ₁ • Γ₁) τ₂) →
let Γ″ = keep Δ-Type τ₁ • keep τ₁ • Γ′ in
Δ {{Γ′}} (abs t) ≈ abs (abs (Δ {τ₁ • Γ₁} t))
Δ-abs t = ext-t (λ ρ → refl)
Δ-app : ∀ {Γ₁ Γ₂ τ₁ τ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t₁ : Term Γ₁ (τ₁ ⇒ τ₂)) (t₂ : Term Γ₁ τ₁) →
Δ {{Γ′}} (app t₁ t₂) ≈ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
Δ-app {{Γ′}} t₁ t₂ = ≈-sym (ext-t (λ ρ′ → let ρ = ⟦ Γ′ ⟧ ρ′ in
begin
⟦ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ⟧ ρ′
≡⟨⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ lift-term {{Γ′}} t₂ ⟧ ρ′)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term {{Γ′}} t₂ ⟧ ρ′))
≡⟨ ≡-cong
(λ x →
diff
(⟦ t₁ ⟧ (update ρ)
(apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x))
(⟦ t₁ ⟧ (ignore ρ) x))
(lift-term-ignore {{Γ′}} t₂) ⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ t₂ ⟧ (ignore ρ))))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨ ≡-cong
(λ x →
diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))))
(apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩
diff
(⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨⟩
⟦ Δ {{Γ′}} (app t₁ t₂) ⟧ ρ′
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var {τ • Γ} this = this
derive-var {τ • Γ} (that x) = that (that (derive-var x))
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = if a b false
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = if x false true
-- XXX: to finish this, we just need to call lift-term, like in
-- derive-term. Should be easy, just a bit boring.
apply-pure-term : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply-pure-term {bool} = abs {!!}
apply-pure-term {τ₁ ⇒ τ₂} {Γ} = abs (abs (abs (app (app apply-pure-term (app (app (var (that (that this))) (var this)) ({!Δ {Γ} (var this)!}))) (app (var (that this)) (var this)))))
--abs (abs (abs (app (app apply-compose-term (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x (Δx)) ( f x )
-- Term xor
_xor-term_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a xor-term b = if a (! b) b
diff-term : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term {τ ⇒ τ₁} = λ f₁ f₂ → {!diff-term (f₁ x) (f₂ x)!}
diff-term {bool} = _xor-term_
apply-term : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term {τ ⇒ τ₁} = {!!}
apply-term {bool} = _xor-term_
derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ)
derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t))
where Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x))
derive-term {{Γ′}} true = false
derive-term {{Γ′}} false = false
derive-term {{Γ′}} (if c t e) =
if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c))
(diff-term (apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e)) (lift-term {{Γ′}} t))
(if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c)))
(diff-term (apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t)) (lift-term {{Γ′}} e))
(if (lift-term {{Γ′}} c)
(derive-term {{Γ′}} t)
(derive-term {{Γ′}} e)))
derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t)
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) →
Δ {{Γ′}} t ≈ derive-term {{Γ′}} t
derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) =
begin
Δ (abs t)
≈⟨ Δ-abs t ⟩
abs (abs (Δ {τ • Γ₁} t))
≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} t)) ⟩
abs (abs (derive-term {τ • Γ₁} t))
≡⟨⟩
derive-term (abs t)
∎ where
open ≈-Reasoning
Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) =
begin
Δ (app t₁ t₂)
≈⟨ Δ-app t₁ t₂ ⟩
app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩
app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
≡⟨⟩
derive-term {{Γ′}} (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ →
begin
⟦ Δ {{Γ′}} (var x) ⟧ ρ
≡⟨⟩
diff
(⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ)))
(⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ)))
≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩
⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ)
≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩
⟦ lift Γ′ (derive-var x) ⟧Var ρ
∎) where open ≡-Reasoning
derive-term-correct true = ext-t (λ ρ → ≡-refl)
derive-term-correct false = ext-t (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Note that Δ is *not* the same as the ∂ operator in
-- definition/intro.tex. See discussion at:
--
-- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
open import Data.Product
open import Data.Unit
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Changes
open import ChangeContexts
-- DEFINITION of valid changes via a logical relation
{-
What I wanted to write:
data ValidΔ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where
base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv
fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) →
(∀ (s : ⟦ S ⟧) ds (valid : ValidΔ s ds) → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) →
ValidΔ f df
-}
-- What I had to write:
valid-Δ : {T : Type} → ⟦ T ⟧ → ⟦ Δ-Type T ⟧ → Set
valid-Δ {bool} v dv = ⊤
valid-Δ {S ⇒ T} f df =
∀ (s : ⟦ S ⟧) ds (valid-w : valid-Δ s ds) →
valid-Δ (f s) (df s ds) ×
(apply df f) (apply ds s) ≡ apply (df s ds) (f s)
-- LIFTING terms into Δ-Contexts
lift-term : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ τ
lift-term {Γ₁} {Γ₂} {{Γ′}} = weaken (≼-trans ≼-Δ-Context Γ′)
-- PROPERTIES of lift-term
lift-term-ignore : ∀ {Γ₁ Γ₂ τ} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} {ρ : ⟦ Γ₂ ⟧} (t : Term Γ₁ τ) →
⟦ lift-term {{Γ′}} t ⟧ ρ ≡ ⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ))
lift-term-ignore {{Γ′}} {ρ} t = let Γ″ = ≼-trans ≼-Δ-Context Γ′ in
begin
⟦ lift-term {{Γ′}} t ⟧ ρ
≡⟨⟩
⟦ weaken Γ″ t ⟧ ρ
≡⟨ weaken-sound t ρ ⟩
⟦ t ⟧ (⟦ ≼-trans ≼-Δ-Context Γ′ ⟧ ρ)
≡⟨ cong (λ x → ⟦ t ⟧ x) (⟦⟧-≼-trans ≼-Δ-Context Γ′ ρ) ⟩
⟦ t ⟧Term (⟦ ≼-Δ-Context ⟧≼ (⟦ Γ′ ⟧≼ ρ))
≡⟨⟩
⟦ t ⟧ (ignore (⟦ Γ′ ⟧ ρ))
∎ where open ≡-Reasoning
-- PROPERTIES of Δ
Δ-abs : ∀ {τ₁ τ₂ Γ₁ Γ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t : Term (τ₁ • Γ₁) τ₂) →
let Γ″ = keep Δ-Type τ₁ • keep τ₁ • Γ′ in
Δ {{Γ′}} (abs t) ≈ abs (abs (Δ {τ₁ • Γ₁} t))
Δ-abs t = ext-t (λ ρ → refl)
Δ-app : ∀ {Γ₁ Γ₂ τ₁ τ₂} {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} (t₁ : Term Γ₁ (τ₁ ⇒ τ₂)) (t₂ : Term Γ₁ τ₁) →
Δ {{Γ′}} (app t₁ t₂) ≈ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
Δ-app {{Γ′}} t₁ t₂ = ≈-sym (ext-t (λ ρ′ → let ρ = ⟦ Γ′ ⟧ ρ′ in
begin
⟦ app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂) ⟧ ρ′
≡⟨⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ lift-term {{Γ′}} t₂ ⟧ ρ′)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term {{Γ′}} t₂ ⟧ ρ′))
≡⟨ ≡-cong
(λ x →
diff
(⟦ t₁ ⟧ (update ρ)
(apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x))
(⟦ t₁ ⟧ (ignore ρ) x))
(lift-term-ignore {{Γ′}} t₂) ⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ t₂ ⟧ (ignore ρ))))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨ ≡-cong
(λ x →
diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))))
(apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩
diff
(⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨⟩
⟦ Δ {{Γ′}} (app t₁ t₂) ⟧ ρ′
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var {τ • Γ} this = this
derive-var {τ • Γ} (that x) = that (that (derive-var x))
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = if a b false
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = if x false true
-- XXX: to finish this, we just need to call lift-term, like in
-- derive-term. Should be easy, just a bit boring.
apply-pure-term : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply-pure-term {bool} = abs {!!}
apply-pure-term {τ₁ ⇒ τ₂} {Γ} = abs (abs (abs (app (app apply-pure-term (app (app (var (that (that this))) (var this)) ({!Δ {Γ} (var this)!}))) (app (var (that this)) (var this)))))
--abs (abs (abs (app (app apply-compose-term (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x (Δx)) ( f x )
-- Term xor
_xor-term_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a xor-term b = if a (! b) b
diff-term : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term {τ ⇒ τ₁} = λ f₁ f₂ → {!diff-term (f₁ x) (f₂ x)!}
diff-term {bool} = _xor-term_
apply-term : ∀ {τ Γ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term {τ ⇒ τ₁} = {!!}
apply-term {bool} = _xor-term_
derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ)
derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t))
where Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x))
derive-term {{Γ′}} true = false
derive-term {{Γ′}} false = false
derive-term {{Γ′}} (if c t e) =
if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c))
(diff-term (apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e)) (lift-term {{Γ′}} t))
(if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c)))
(diff-term (apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t)) (lift-term {{Γ′}} e))
(if (lift-term {{Γ′}} c)
(derive-term {{Γ′}} t)
(derive-term {{Γ′}} e)))
derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t)
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) →
Δ {{Γ′}} t ≈ derive-term {{Γ′}} t
derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) =
begin
Δ (abs t)
≈⟨ Δ-abs t ⟩
abs (abs (Δ {τ • Γ₁} t))
≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} t)) ⟩
abs (abs (derive-term {τ • Γ₁} t))
≡⟨⟩
derive-term (abs t)
∎ where
open ≈-Reasoning
Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) =
begin
Δ (app t₁ t₂)
≈⟨ Δ-app t₁ t₂ ⟩
app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩
app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
≡⟨⟩
derive-term {{Γ′}} (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ →
begin
⟦ Δ {{Γ′}} (var x) ⟧ ρ
≡⟨⟩
diff
(⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ)))
(⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ)))
≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩
⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ)
≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩
⟦ lift Γ′ (derive-var x) ⟧Var ρ
∎) where open ≡-Reasoning
derive-term-correct true = ext-t (λ ρ → ≡-refl)
derive-term-correct false = ext-t (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
|
Break lines.
|
Break lines.
Old-commit-hash: c7c69f51c35066147017459aa226bebe26d68df2
|
Agda
|
mit
|
inc-lc/ilc-agda
|
fdb5d8f0195c9849f6e208db228bb650fe5eba97
|
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
module Structure where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
(∀ {ι Γ} → 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 :
(∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))) →
(∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply :
(∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))) →
(∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
|
import Parametric.Syntax.Type as Type
import 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
module Structure where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
(∀ {ι Γ} → 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 :
(∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))) →
(∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply :
(∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ ΔType (base ι))) →
(∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
|
Rename Δbase to ΔBase.
|
Rename Δbase to ΔBase.
Old-commit-hash: e9efdc6e4dd5d76003e901e1ef557ec049d5e59c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
7ed997a9f08476266fb59fb488eac08ad40048de
|
parametricity.agda
|
parametricity.agda
|
module parametricity where
import Data.Bool.Param.NatBool
import Data.Dec.Param.Binary
import Data.Dec.Param.Unary
import Data.Fin.Param.Binary
import Data.Maybe.Param.Binary
import Data.Maybe.Param.Unary
import Data.Nat.Param.Binary
import Data.One.Param.Binary
import Data.One.Param.Unary
import Data.Product.Param.Binary
import Data.Product.Param.Unary
import Data.Sum.Param.Binary
import Data.Sum.Param.Unary
import Data.Two.Param.Binary
import Data.Two.Param.Unary
import Data.Zero.Param.Binary
import Data.Zero.Param.Unary
import Function.Param.Binary
import Function.Param.Unary
import Reflection.NP
import Reflection.Param
import Reflection.Param.Env
import Reflection.Param.Tests
import Type.Param.Binary
import Type.Param.Unary
|
module parametricity where
import Data.Bool.Param.NatBool
import Data.Dec.Param.Binary
import Data.Dec.Param.Unary
import Data.Fin.Param.Binary
import Data.Maybe.Param.Binary
import Data.Maybe.Param.Unary
import Data.Nat.Param.Binary
import Data.One.Param.Binary
import Data.One.Param.Unary
import Data.Product.Param.Binary
import Data.Product.Param.Unary
import Data.Sum.Param.Binary
import Data.Sum.Param.Unary
import Data.Two.Param.Binary
import Data.Two.Param.Unary
import Data.Zero.Param.Binary
import Data.Zero.Param.Unary
import Function.Param.Binary
import Function.Param.Unary
{-
import Reflection.NP
import Reflection.Param
import Reflection.Param.Env
import Reflection.Param.Tests
-}
import Type.Param.Binary
import Type.Param.Unary
|
Disable reflection for now
|
Disable reflection for now
|
Agda
|
bsd-3-clause
|
np/agda-parametricity
|
5bd1cb31b6ff25934b1212da8538587845a850b9
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
{-# OPTIONS --without-K #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Type hiding (★)
open import Data.One using (𝟙)
open import Data.Product using (Σ; _,_)
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
private
module Dummy {a} {A : ★ a} where
open IsEquivalence (isEquivalence {a} {A}) public hiding (refl; sym; trans)
open Dummy public
PathOver : ∀ {i j} {A : ★ i} (B : A → ★ j)
{x y : A} (p : x ≡ y) (u : B x) (v : B y) → ★ j
PathOver B refl u v = (u ≡ v)
syntax PathOver B p u v =
u ≡ v [ B ↓ p ]
{- one day this will work -}
-- pattern idp = refl
-- Some definitions with the names from Agda-HoTT
-- this is only a temporary state of affairs...
module _ {a} {A : ★ a} where
idp : {x : A} → x ≡ x
idp = refl
infixr 8 _∙_ _∙'_
_∙_ _∙'_ : {x y z : A} → (x ≡ y → y ≡ z → x ≡ z)
refl ∙ q = q
q ∙' refl = q
!_ : {x y : A} → (x ≡ y → y ≡ x)
!_ refl = refl
ap↓ : ∀ {i j k} {A : ★ i} {B : A → ★ j} {C : A → ★ k}
(g : {a : A} → B a → C a) {x y : A} {p : x ≡ y}
{u : B x} {v : B y}
→ (u ≡ v [ B ↓ p ] → g u ≡ g v [ C ↓ p ])
ap↓ g {p = refl} refl = refl
ap : ∀ {i j} {A : ★ i} {B : ★ j} (f : A → B) {x y : A}
→ (x ≡ y → f x ≡ f y)
ap f p = ap↓ {A = 𝟙} f {p = refl} p
apd : ∀ {i j} {A : ★ i} {B : A → ★ j} (f : (a : A) → B a) {x y : A}
→ (p : x ≡ y) → f x ≡ f y [ B ↓ p ]
apd f refl = refl
coe : ∀ {i} {A B : ★ i} (p : A ≡ B) → A → B
coe refl x = x
coe! : ∀ {i} {A B : ★ i} (p : A ≡ B) → B → A
coe! p x = coe (! p) x
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≗₂_ : ∀ {a b c} {A : ★ a} {B : ★ b} {C : ★ c} (f g : A → B → C) → ★ _
f ≗₂ g = ∀ x y → f x y ≡ g x y
module _ {a} {A : ★ a} where
J-orig : ∀ {b} (P : {x y : A} → x ≡ y → ★_ b) → (∀ {x} → P {x} {x} refl) → ∀ {x y} (p : x ≡ y) → P p
J-orig P p refl = p
-- This version is better suited to our identity type which has the first argument as a parameter.
-- (due to Paulin-Mohring)
J : ∀ {b} {x : A} (P : {y : A} → x ≡ y → ★_ b) → P {x} refl → ∀ {y} (p : x ≡ y) → P p
J P p refl = p
injective : InjectiveRel A _≡_
injective p q = p ∙ ! q
surjective : SurjectiveRel A _≡_
surjective p q = ! p ∙ q
bijective : BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
Equalizer : ∀ {b} {B : A → ★ b} (f g : (x : A) → B x) → ★ _
Equalizer f g = Σ A (λ x → f x ≡ g x)
{- In a category theory context this type would the object 'E'
and 'proj₁' would be the morphism 'eq : E → A' such that
given any object O, and morphism 'm : O → A', there exists
a unique morphism 'u : O → E' such that 'eq ∘ u ≡ m'.
-}
Pullback : ∀ {b c} {B : ★ b} {C : ★ c} (f : A → C) (g : B → C) → ★ _
Pullback {B = B} f g = Σ A (λ x → Σ B (λ y → f x ≡ g y))
module ≡-Reasoning {a} {A : ★ a} where
open Setoid-Reasoning (setoid A) public
renaming (_≈⟨_⟩_ to _≡⟨_⟩_;
_≈⟨-by-definition-⟩_ to _≡⟨-by-definition-⟩_)
module ≗-Reasoning {a b} {A : ★ a} {B : ★ b} where
open Setoid-Reasoning (A →-setoid B) public
renaming (_≈⟨_⟩_ to _≗⟨_⟩_;
_≈⟨-by-definition-⟩_ to _≗⟨-by-definition-⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦★⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦★⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → ★
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦★₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
{-# OPTIONS --without-K #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Type hiding (★)
open import Data.One using (𝟙)
open import Data.Product using (Σ; _,_)
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning; subst)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
private
module Dummy {a} {A : ★ a} where
open IsEquivalence (isEquivalence {a} {A}) public hiding (refl; sym; trans)
open Dummy public
PathOver : ∀ {i j} {A : ★ i} (B : A → ★ j)
{x y : A} (p : x ≡ y) (u : B x) (v : B y) → ★ j
PathOver B refl u v = (u ≡ v)
syntax PathOver B p u v =
u ≡ v [ B ↓ p ]
{- one day this will work -}
-- pattern idp = refl
-- Some definitions with the names from Agda-HoTT
-- this is only a temporary state of affairs...
module _ {a} {A : ★ a} where
idp : {x : A} → x ≡ x
idp = refl
infixr 8 _∙_ _∙'_
_∙_ _∙'_ : {x y z : A} → (x ≡ y → y ≡ z → x ≡ z)
refl ∙ q = q
q ∙' refl = q
!_ : {x y : A} → (x ≡ y → y ≡ x)
!_ refl = refl
ap↓ : ∀ {i j k} {A : ★ i} {B : A → ★ j} {C : A → ★ k}
(g : {a : A} → B a → C a) {x y : A} {p : x ≡ y}
{u : B x} {v : B y}
→ (u ≡ v [ B ↓ p ] → g u ≡ g v [ C ↓ p ])
ap↓ g {p = refl} refl = refl
ap : ∀ {i j} {A : ★ i} {B : ★ j} (f : A → B) {x y : A}
→ (x ≡ y → f x ≡ f y)
ap f p = ap↓ {A = 𝟙} f {p = refl} p
apd : ∀ {i j} {A : ★ i} {B : A → ★ j} (f : (a : A) → B a) {x y : A}
→ (p : x ≡ y) → f x ≡ f y [ B ↓ p ]
apd f refl = refl
coe : ∀ {i} {A B : ★ i} (p : A ≡ B) → A → B
coe refl x = x
coe! : ∀ {i} {A B : ★ i} (p : A ≡ B) → B → A
coe! p = coe (! p)
module _ {ℓ ℓp}
{A : Set ℓ}
(P : A → Set ℓp)
{x y : A}
(p : x ≡ y)
where
tr : P x → P y
tr = coe (ap P p)
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₀ a₁ b₀ b₁ c₀ c₁}
→ a₀ ≡ a₁ → b₀ ≡ b₁ → c₀ ≡ c₁ → f a₀ b₀ c₀ ≡ f a₁ b₁ c₁
cong₃ f refl refl refl = refl
_≗₂_ : ∀ {a b c} {A : ★ a} {B : ★ b} {C : ★ c} (f g : A → B → C) → ★ _
f ≗₂ g = ∀ x y → f x y ≡ g x y
module _ {a} {A : ★ a} where
J-orig : ∀ {b} (P : (x y : A) → x ≡ y → ★_ b) → (∀ x → P x x refl) → ∀ {x y} (p : x ≡ y) → P x y p
J-orig P p refl = p _
-- This version is better suited to our identity type which has the first argument as a parameter.
-- (due to Paulin-Mohring)
J : ∀ {b} {x : A} (P : (y : A) → x ≡ y → ★_ b) → P x refl → ∀ {y} (p : x ≡ y) → P y p
J P p refl = p
injective : InjectiveRel A _≡_
injective p q = p ∙ ! q
surjective : SurjectiveRel A _≡_
surjective p q = ! p ∙ q
bijective : BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
Equalizer : ∀ {b} {B : A → ★ b} (f g : (x : A) → B x) → ★ _
Equalizer f g = Σ A (λ x → f x ≡ g x)
{- In a category theory context this type would the object 'E'
and 'proj₁' would be the morphism 'eq : E → A' such that
given any object O, and morphism 'm : O → A', there exists
a unique morphism 'u : O → E' such that 'eq ∘ u ≡ m'.
-}
Pullback : ∀ {b c} {B : ★ b} {C : ★ c} (f : A → C) (g : B → C) → ★ _
Pullback {B = B} f g = Σ A (λ x → Σ B (λ y → f x ≡ g y))
module ≡-Reasoning {a} {A : ★ a} where
open Setoid-Reasoning (setoid A) public
renaming (_≈⟨_⟩_ to _≡⟨_⟩_;
_≈⟨by-definition⟩_ to _≡⟨by-definition⟩_)
module ≗-Reasoning {a b} {A : ★ a} {B : ★ b} where
open Setoid-Reasoning (A →-setoid B) public
renaming (_≈⟨_⟩_ to _≗⟨_⟩_;
_≈⟨by-definition⟩_ to _≗⟨by-definition⟩_)
data ⟦≡⟧ {a₀ a₁ aᵣ}
{A₀ A₁} (Aᵣ : ⟦★⟧ {a₀} {a₁} aᵣ A₀ A₁)
{x₀ x₁} (xᵣ : Aᵣ x₀ x₁)
: (Aᵣ ⟦→⟧ ⟦★⟧ aᵣ) (_≡_ x₀) (_≡_ x₁) where
-- : ∀ {y₀ y₁} (yᵣ : Aᵣ y₀ y₁) → x₀ ≡ y₀ → x₁ ≡ y₁ → ★
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦★₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
Hide subst, expose tr...
|
≡: Hide subst, expose tr...
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
690f2df38e24ddcd80d24056254c61364cc071fd
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
open import Base.Change.Equivalence.Base public
open import Base.Change.Equivalence.EqReasoning public
module _ {a} {A : Set a} {{ca : ChangeAlgebra A}} {x : A} where
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ (dx : Δ x) → 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) → x ⊞ dx ≡ x → dx ≙ (nil x)
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- Let's show that nil x is d.o.e. to x ⊟ x
nil-x-is-x⊟x : nil x ≙ x ⊟ x
nil-x-is-x⊟x = ≙-sym (⊞-unit-is-nil (x ⊟ x) (update-diff x x))
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by
-- equiv-fun-changes-respect, and its corollaries fun-change-respects and
-- equiv-fun-changes-funs.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx : Δ x} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
-- \begin{lemma}[Equivalence cancellation]
-- |v2 `ominus` v1 `doe` dv| holds if and only if |v2 = v1 `oplus`
-- dv|, for any |v1, v2 `elem` V| and |dv `elem` Dt ^ v1|.
-- \end{lemma}
equiv-cancel-1 : ∀ x' dx → (x' ⊟ x) ≙ dx → x' ≡ x ⊞ dx
equiv-cancel-1 x' dx (doe x'⊟x≙dx) = trans (sym (update-diff x' x)) x'⊟x≙dx
equiv-cancel-2 : ∀ x' dx → x' ≡ x ⊞ dx → x' ⊟ x ≙ dx
equiv-cancel-2 _ dx refl = diff-update
module _ {a} {b} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}} where
equiv-fun-changes-respect : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂ : Δ f} →
df₁ ≙₍ f ₎ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
equiv-fun-changes-respect {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
fun-change-respects : ∀ {x : A} {dx₁ dx₂} {f : A → B} (df : Δ f) →
dx₁ ≙ dx₂ → apply df x dx₁ ≙ apply df x dx₂
fun-change-respects df dx₁≙dx₂ = equiv-fun-changes-respect (≙-refl {dx = df}) dx₁≙dx₂
-- D.o.e. function changes behave like the same function (up to d.o.e.).
equiv-fun-changes-funs : ∀ {x : A} {dx} {f : A → B} {df₁ df₂} →
df₁ ≙₍ f ₎ df₂ → apply df₁ x dx ≙ apply df₂ x dx
equiv-fun-changes-funs {dx = dx} df₁≙df₂ = equiv-fun-changes-respect df₁≙df₂ (≙-refl {dx = dx})
derivative-doe-characterization : ∀ {a : A} {da : Δ a}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
df a da ≙ f (a ⊞ da) ⊟ f a
derivative-doe-characterization {a} {da} {f} {df} is-derivative = doe lemma
where
open ≡-Reasoning
lemma : f a ⊞ df a da ≡ f a ⊞ (f (a ⊞ da) ⊟ f a)
lemma =
begin
(f a ⊞ df a da)
≡⟨ is-derivative a da ⟩
(f (a ⊞ da))
≡⟨ sym (update-diff (f (a ⊞ da)) (f a)) ⟩
(f a ⊞ (f (a ⊞ da) ⊟ f a))
∎
derivative-respects-doe : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
dx₁ ≙ dx₂ → df x dx₁ ≙ df x dx₂
derivative-respects-doe {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
begin
df x dx₁
≙⟨ derivative-doe-characterization is-derivative ⟩
f (x ⊞ dx₁) ⊟ f x
≡⟨ cong (λ v → f v ⊟ f x) (proof dx₁≙dx₂) ⟩
f (x ⊞ dx₂) ⊟ f x
≙⟨ ≙-sym (derivative-doe-characterization is-derivative) ⟩
df x dx₂
∎
where
open ≙-Reasoning
-- This is also a corollary of fun-changes-respect
derivative-respects-doe-alt : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
dx₁ ≙ dx₂ → df x dx₁ ≙ df x dx₂
derivative-respects-doe-alt {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
fun-change-respects (DerivativeAsChange is-derivative) dx₁≙dx₂
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- You could think that the function should relate equivalent changes, but
-- that's a stronger hypothesis, which doesn't give you extra guarantees. But
-- here's the statement and proof, for completeness:
delta-ext₂ : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x (dx₁ dx₂ : Δ x) → dx₁ ≙₍ x ₎ dx₂ → apply df x dx₁ ≙ apply dg x dx₂) → df ≙ dg
delta-ext₂ {f} {df} {dg} df-x-dx≙dg-x-dx = delta-ext (λ x dx → df-x-dx≙dg-x-dx x dx dx ≙-refl)
-- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> IsDerivative f (apply df).
-- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
IsDerivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil 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 →
IsDerivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
derivative-is-nil-alternative : ∀ {f : A → B} df →
(IsDerivative-f-df : IsDerivative f df) → DerivativeAsChange IsDerivative-f-df ≙ nil f
derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df
nil-is-derivative : ∀ (f : A → B) → IsDerivative f (apply (nil f))
nil-is-derivative f a da =
begin
f a ⊞ apply (nil f) a da
≡⟨ sym (incrementalization f (nil f) a da) ⟩
(f ⊞ nil f) (a ⊞ da)
≡⟨ cong (λ □ → □ (a ⊞ da)) (update-nil f) ⟩
f (a ⊞ da)
∎
where
open ≡-Reasoning
-- If we have two derivatives, they're both nil, hence they're equivalent.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → IsDerivative f (apply df) → IsDerivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
equiv-derivative-is-derivative : ∀ {f : A → B} df₁ df₂ → IsDerivative f (apply df₁) → df₁ ≙₍ f ₎ df₂ → IsDerivative f (apply df₂)
equiv-derivative-is-derivative {f} df₁ df₂ IsDerivative-f-df₁ df₁≙df₂ a da =
begin
f a ⊞ apply df₂ a da
≡⟨ sym (incrementalization f df₂ a da) ⟩
(f ⊞ df₂) (a ⊞ da)
≡⟨ cong (λ □ → □ (a ⊞ da)) lemma ⟩
f (a ⊞ da)
∎
where
open ≡-Reasoning
lemma : f ⊞ df₂ ≡ f
lemma =
begin
f ⊞ df₂
≡⟨ proof (≙-sym df₁≙df₂) ⟩
f ⊞ df₁
≡⟨ derivative-is-⊞-unit df₁ IsDerivative-f-df₁ ⟩
f
∎
-- This is Lemma 2.5 in the paper. Note that the statement in the paper uses
-- (incorrectly) normal equality instead of delta-observational equivalence.
deriv-zero :
∀ (f : A → B) df → IsDerivative f df →
∀ v → df v (nil v) ≙ nil (f v)
deriv-zero f df proof v = doe lemma
where
open ≡-Reasoning
lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil (f v)
lemma =
begin
f v ⊞ df v (nil v)
≡⟨ proof v (nil v) ⟩
f (v ⊞ (nil v))
≡⟨ cong f (update-nil v) ⟩
f v
≡⟨ sym (update-nil (f v)) ⟩
f v ⊞ nil (f v)
∎
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
open import Base.Change.Equivalence.Base public
open import Base.Change.Equivalence.EqReasoning public
module _ {a} {A : Set a} {{ca : ChangeAlgebra A}} {x : A} where
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ (dx : Δ x) → 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) → x ⊞ dx ≡ x → dx ≙ (nil x)
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- Let's show that nil x is d.o.e. to x ⊟ x
nil-x-is-x⊟x : nil x ≙ x ⊟ x
nil-x-is-x⊟x = ≙-sym (⊞-unit-is-nil (x ⊟ x) (update-diff x x))
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by
-- equiv-fun-changes-respect, and its corollaries fun-change-respects and
-- equiv-fun-changes-funs.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx : Δ x} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
-- \begin{lemma}[Equivalence cancellation]
-- |v2 `ominus` v1 `doe` dv| holds if and only if |v2 = v1 `oplus`
-- dv|, for any |v1, v2 `elem` V| and |dv `elem` Dt ^ v1|.
-- \end{lemma}
equiv-cancel-1 : ∀ x' dx → (x' ⊟ x) ≙ dx → x' ≡ x ⊞ dx
equiv-cancel-1 x' dx (doe x'⊟x≙dx) = trans (sym (update-diff x' x)) x'⊟x≙dx
equiv-cancel-2 : ∀ x' dx → x' ≡ x ⊞ dx → x' ⊟ x ≙ dx
equiv-cancel-2 _ dx refl = diff-update
module _ {a} {b} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra A}} {{CB : ChangeAlgebra B}} where
≙-cong₂ : ∀ {c} {C : Set c} {x y}
(f : A → B → C) {dx1 dx2 dy1 dy2} → dx1 ≙ dx2 → dy1 ≙ dy2 → f (x ⊞ dx1) (y ⊞ dy1) ≡ f (x ⊞ dx2) (y ⊞ dy2)
≙-cong₂ f dx1≙dx2 dy1≙dy2 = cong₂ f (proof dx1≙dx2) (proof dy1≙dy2)
equiv-fun-changes-respect : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂ : Δ f} →
df₁ ≙₍ f ₎ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
equiv-fun-changes-respect {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
fun-change-respects : ∀ {x : A} {dx₁ dx₂} {f : A → B} (df : Δ f) →
dx₁ ≙ dx₂ → apply df x dx₁ ≙ apply df x dx₂
fun-change-respects df dx₁≙dx₂ = equiv-fun-changes-respect (≙-refl {dx = df}) dx₁≙dx₂
-- D.o.e. function changes behave like the same function (up to d.o.e.).
equiv-fun-changes-funs : ∀ {x : A} {dx} {f : A → B} {df₁ df₂} →
df₁ ≙₍ f ₎ df₂ → apply df₁ x dx ≙ apply df₂ x dx
equiv-fun-changes-funs {dx = dx} df₁≙df₂ = equiv-fun-changes-respect df₁≙df₂ (≙-refl {dx = dx})
derivative-doe-characterization : ∀ {a : A} {da : Δ a}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
df a da ≙ f (a ⊞ da) ⊟ f a
derivative-doe-characterization {a} {da} {f} {df} is-derivative = doe lemma
where
open ≡-Reasoning
lemma : f a ⊞ df a da ≡ f a ⊞ (f (a ⊞ da) ⊟ f a)
lemma =
begin
(f a ⊞ df a da)
≡⟨ is-derivative a da ⟩
(f (a ⊞ da))
≡⟨ sym (update-diff (f (a ⊞ da)) (f a)) ⟩
(f a ⊞ (f (a ⊞ da) ⊟ f a))
∎
derivative-respects-doe : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
dx₁ ≙ dx₂ → df x dx₁ ≙ df x dx₂
derivative-respects-doe {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
begin
df x dx₁
≙⟨ derivative-doe-characterization is-derivative ⟩
f (x ⊞ dx₁) ⊟ f x
≡⟨ cong (λ v → f v ⊟ f x) (proof dx₁≙dx₂) ⟩
f (x ⊞ dx₂) ⊟ f x
≙⟨ ≙-sym (derivative-doe-characterization is-derivative) ⟩
df x dx₂
∎
where
open ≙-Reasoning
-- This is also a corollary of fun-changes-respect
derivative-respects-doe-alt : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
dx₁ ≙ dx₂ → df x dx₁ ≙ df x dx₂
derivative-respects-doe-alt {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
fun-change-respects (DerivativeAsChange is-derivative) dx₁≙dx₂
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- You could think that the function should relate equivalent changes, but
-- that's a stronger hypothesis, which doesn't give you extra guarantees. But
-- here's the statement and proof, for completeness:
delta-ext₂ : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x (dx₁ dx₂ : Δ x) → dx₁ ≙₍ x ₎ dx₂ → apply df x dx₁ ≙ apply dg x dx₂) → df ≙ dg
delta-ext₂ {f} {df} {dg} df-x-dx≙dg-x-dx = delta-ext (λ x dx → df-x-dx≙dg-x-dx x dx dx ≙-refl)
-- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> IsDerivative f (apply df).
-- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
IsDerivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil 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 →
IsDerivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
derivative-is-nil-alternative : ∀ {f : A → B} df →
(IsDerivative-f-df : IsDerivative f df) → DerivativeAsChange IsDerivative-f-df ≙ nil f
derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df
nil-is-derivative : ∀ (f : A → B) → IsDerivative f (apply (nil f))
nil-is-derivative f a da =
begin
f a ⊞ apply (nil f) a da
≡⟨ sym (incrementalization f (nil f) a da) ⟩
(f ⊞ nil f) (a ⊞ da)
≡⟨ cong (λ □ → □ (a ⊞ da)) (update-nil f) ⟩
f (a ⊞ da)
∎
where
open ≡-Reasoning
-- If we have two derivatives, they're both nil, hence they're equivalent.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → IsDerivative f (apply df) → IsDerivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
equiv-derivative-is-derivative : ∀ {f : A → B} df₁ df₂ → IsDerivative f (apply df₁) → df₁ ≙₍ f ₎ df₂ → IsDerivative f (apply df₂)
equiv-derivative-is-derivative {f} df₁ df₂ IsDerivative-f-df₁ df₁≙df₂ a da =
begin
f a ⊞ apply df₂ a da
≡⟨ sym (incrementalization f df₂ a da) ⟩
(f ⊞ df₂) (a ⊞ da)
≡⟨ cong (λ □ → □ (a ⊞ da)) lemma ⟩
f (a ⊞ da)
∎
where
open ≡-Reasoning
lemma : f ⊞ df₂ ≡ f
lemma =
begin
f ⊞ df₂
≡⟨ proof (≙-sym df₁≙df₂) ⟩
f ⊞ df₁
≡⟨ derivative-is-⊞-unit df₁ IsDerivative-f-df₁ ⟩
f
∎
-- This is Lemma 2.5 in the paper. Note that the statement in the paper uses
-- (incorrectly) normal equality instead of delta-observational equivalence.
deriv-zero :
∀ (f : A → B) df → IsDerivative f df →
∀ v → df v (nil v) ≙ nil (f v)
deriv-zero f df proof v = doe lemma
where
open ≡-Reasoning
lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil (f v)
lemma =
begin
f v ⊞ df v (nil v)
≡⟨ proof v (nil v) ⟩
f (v ⊞ (nil v))
≡⟨ cong f (update-nil v) ⟩
f v
≡⟨ sym (update-nil (f v)) ⟩
f v ⊞ nil (f v)
∎
|
Add ≙-cong₂
|
Add ≙-cong₂
Useful for next steps.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
c68ae3394af059da44a270c4286cea18d3987019
|
Syntax/Term/Plotkin.agda
|
Syntax/Term/Plotkin.agda
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Denotation.Environment Type
open import Syntax.Context.Plotkin B
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
TermConstructor : (Γ Σ : Context) (τ : Type) → Set
TermConstructor Γ ∅ τ′ = Term Γ τ′
TermConstructor Γ (τ • Σ) τ′ = Term Γ τ → TermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → TermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → TermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const constant)
|
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)
|
Document Term and Terms.
|
Document Term and Terms.
Old-commit-hash: 7f9cbb4cffc6d9343c93f495fd39e0d6b1bcd855
|
Agda
|
mit
|
inc-lc/ilc-agda
|
f5b981ea7905d4deb015eced08bb201e6b26490f
|
Crypto/JS/BigI/FiniteField.agda
|
Crypto/JS/BigI/FiniteField.agda
|
{-# OPTIONS --without-K #-}
open import Type.Eq
open import Data.Two hiding (_==_)
open import Relation.Binary.PropositionalEquality
open import FFI.JS using (Bool; trace-call; _++_)
open import FFI.JS.Check
renaming (check to check?)
--renaming (warn-check to check?)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Algebra.Raw
open import Algebra.Field
-- TODO carry on a primality proof of q
module Crypto.JS.BigI.FiniteField (q : BigI) where
abstract
-- ℤq
𝔽 : Set
𝔽 = BigI
private
mod-q : BigI → 𝔽
mod-q x = mod x q
-- There are two ways to go from BigI to 𝔽: BigI▹𝔽 and mod-q
-- Use BigI▹𝔽 for untrusted input data and mod-q for internal
-- computation.
BigI▹𝔽 : BigI → 𝔽
BigI▹𝔽 = -- trace-call "BigI▹𝔽 "
λ x →
(check? (x <I q)
(λ _ → "Not below the modulus: q:" ++ toString q ++ " is less than x:" ++ toString x)
(check? (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") x))
check-non-zero : 𝔽 → BigI
check-non-zero = -- trace-call "check-non-zero "
λ x → check? (x >I 0I) (λ _ → "Should be non zero") x
repr : 𝔽 → BigI
repr x = x
0# 1# : 𝔽
0# = 0I
1# = 1I
1/_ : 𝔽 → 𝔽
1/ x = modInv (check-non-zero x) q
_^_ : 𝔽 → BigI → 𝔽
x ^ y = modPow x y q
_⊗_ : 𝔽 → BigI → 𝔽
x ⊗ y = mod-q (multiply (repr x) y)
_+_ _−_ _*_ _/_ : 𝔽 → 𝔽 → 𝔽
x + y = mod-q (add (repr x) (repr y))
x − y = mod-q (subtract (repr x) (repr y))
x * y = x ⊗ repr y
x / y = x * 1/ y
0−_ : 𝔽 → 𝔽
0− x = mod-q (negate (repr x))
sum prod : List 𝔽 → 𝔽
sum = foldr _+_ 0#
prod = foldr _*_ 1#
instance
𝔽-Eq? : Eq? 𝔽
𝔽-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : 𝔽 → 𝔽 → 𝟚
x ==' y = equals (repr x) (repr y)
postulate
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
+-mon-ops : Monoid-Ops 𝔽
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops 𝔽
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops 𝔽
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops 𝔽
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops 𝔽
fld-ops = +-grp-ops , *-grp-ops
postulate
fld-struct : Field-Struct fld-ops
fld : Field 𝔽
fld = fld-ops , fld-struct
module fld = Field fld
open fld using (+-grp) public
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type.Eq
open import Data.Two hiding (_==_)
open import Relation.Binary.PropositionalEquality
open import FFI.JS using (JS[_]; _++_; return; _>>_)
open import FFI.JS.Check
using (check!)
renaming (check to check?)
--renaming (warn-check to check?)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Algebra.Raw
open import Algebra.Field
-- TODO carry on a primality proof of q
module Crypto.JS.BigI.FiniteField (q : BigI) where
abstract
ℤ[_] : Set
ℤ[_] = BigI
private
ℤq : Set
ℤq = ℤ[_]
mod-q : BigI → ℤq
mod-q x = mod x q
-- There are two ways to go from BigI to ℤq: BigI▹ℤ[ q ] and mod-q
-- Use BigI▹ℤ[ q ] for untrusted input data and mod-q for internal
-- computation.
BigI▹ℤ[_] : BigI → JS[ ℤq ]
BigI▹ℤ[_] x =
-- Console.log "BigI▹ℤ[_]"
check! "below the modulus" (x <I q)
(λ _ → "Not below the modulus: q:" ++ toString q ++
" is less than x:" ++ toString x) >>
check! "positivity" (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") >>
return 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
-- One could choose here to return a dummy value when 0 is given.
-- Instead we throw an exception which in some circumstances could
-- be bad if the runtime semantics is too eager.
1/_ : ℤq → ℤq
1/ x = modInv (check-non-zero x) q
_^_ : ℤq → BigI → ℤq
x ^ y = modPow x y q
_⊗_ : ℤq → BigI → ℤq
x ⊗ y = mod-q (multiply (repr x) y)
_+_ _−_ _*_ _/_ : ℤq → ℤq → ℤq
x + y = mod-q (add (repr x) (repr y))
x − y = mod-q (subtract (repr x) (repr y))
x * y = x ⊗ repr y
x / y = x * 1/ y
0−_ : ℤq → ℤq
0− x = mod-q (negate (repr x))
sum prod : List ℤq → ℤq
sum = foldr _+_ 0#
prod = foldr _*_ 1#
instance
ℤ[_]-Eq? : Eq? ℤq
ℤ[_]-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤq → ℤq → 𝟚
x ==' y = equals (repr x) (repr y)
postulate
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
+-mon-ops : Monoid-Ops ℤq
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops ℤq
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops ℤq
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops ℤq
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops ℤq
fld-ops = +-grp-ops , *-grp-ops
postulate
fld-struct : Field-Struct fld-ops
fld : Field ℤq
fld = fld-ops , fld-struct
module fld = Field fld
open fld using (+-grp) public
-- -}
-- -}
-- -}
-- -}
|
rename as ℤ[_], and improve imports from BigI
|
FiniteField: rename as ℤ[_], and improve imports from BigI
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
5d3aa98ffc0c22e45257913557e911cc654c4777
|
Popl14/Change/Evaluation.agda
|
Popl14/Change/Evaluation.agda
|
module Popl14.Change.Evaluation where
open import Popl14.Syntax.Type
open import Popl14.Syntax.Term
open import Popl14.Change.Type
open import Popl14.Change.Term
open import Popl14.Change.Value
open import Popl14.Denotation.Value
open import Popl14.Denotation.Evaluation
open import Relation.Binary.PropositionalEquality
open import Base.Denotation.Notation
open import Postulate.Extensionality
-- unique names with unambiguous types
-- to help type inference figure things out
private
module Disambiguation where
infixr 9 _⋆_
_⋆_ : Type → Context → Context
_⋆_ = _•_
-- Relating `diff` with `diff-term`, `apply` with `apply-term`
meaning-⊕ : ∀ {τ Γ}
{t : Term Γ τ} {Δt : Term Γ (ΔType τ)} {ρ : ⟦ Γ ⟧} →
let
_+_ = _⟦⊕⟧_ {τ}
in
⟦ t ⟧ ρ + ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕ Δt ⟧ ρ
meaning-⊝ : ∀ {τ Γ}
{s : Term Γ τ} {t : Term Γ τ} {ρ : ⟦ Γ ⟧} →
let
_-_ = _⟦⊝⟧_ {τ}
in
⟦ s ⟧ ρ - ⟦ t ⟧ ρ ≡ ⟦ s ⊝ t ⟧ ρ
meaning-⊕ {base base-int} = refl
meaning-⊕ {base base-bag} = refl
meaning-⊕ {σ ⇒ τ} {Γ} {t} {Δt} {ρ} = ext (λ v →
let
Γ′ = σ ⋆ (σ ⇒ τ) ⋆ ΔType (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧
ρ′ = v • (⟦ t ⟧ ρ) • (⟦ Δt ⟧ ρ) • ρ
_-₀_ = _⟦⊝⟧_ {σ}
_+₁_ = _⟦⊕⟧_ {τ}
x : Term Γ′ σ
x = var this
f : Term Γ′ (σ ⇒ τ)
f = var (that this)
Δf : Term Γ′ (ΔType (σ ⇒ τ))
Δf = var (that (that this))
y = app f x
Δy = app (app Δf x) (x ⊝ x)
in
begin
⟦ t ⟧ ρ v +₁ ⟦ Δt ⟧ ρ v (v -₀ v)
≡⟨ cong (λ hole → ⟦ t ⟧ ρ v +₁ ⟦ Δt ⟧ ρ v hole)
(meaning-⊝ {s = x} {x} {ρ′}) ⟩
⟦ t ⟧ ρ v +₁ ⟦ Δt ⟧ ρ v (⟦ x ⊝ x ⟧ ρ′)
≡⟨ meaning-⊕ {t = y} {Δt = Δy} {ρ′} ⟩
⟦ y ⊕ Δy ⟧ ρ′
∎)
where
open ≡-Reasoning
open Disambiguation
meaning-⊝ {int} = refl
meaning-⊝ {bag} = refl
meaning-⊝ {σ ⇒ τ} {Γ} {s} {t} {ρ} =
ext (λ v → ext (λ Δv →
let
Γ′ = ΔType σ ⋆ σ ⋆ (σ ⇒ τ) ⋆ (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧Context
ρ′ = Δv • v • ⟦ t ⟧Term ρ • ⟦ s ⟧Term ρ • ρ
Δx : Term Γ′ (ΔType σ)
Δx = var this
x : Term Γ′ σ
x = var (that this)
f : Term Γ′ (σ ⇒ τ)
f = var (that (that this))
g : Term Γ′ (σ ⇒ τ)
g = var (that (that (that this)))
y = app f x
y′ = app g (x ⊕ Δx)
_+₀_ = _⟦⊕⟧_ {σ}
_-₁_ = _⟦⊝⟧_ {τ}
in
begin
⟦ s ⟧ ρ (v +₀ Δv) -₁ ⟦ t ⟧ ρ v
≡⟨ cong (λ hole → ⟦ s ⟧ ρ hole -₁ ⟦ t ⟧ ρ v)
(meaning-⊕ {t = x} {Δt = Δx} {ρ′}) ⟩
⟦ s ⟧ ρ (⟦ x ⊕ Δx ⟧ ρ′) -₁ ⟦ t ⟧ ρ v
≡⟨ meaning-⊝ {s = y′} {y} {ρ′} ⟩
⟦ y′ ⊝ y ⟧ ρ′
∎))
where
open ≡-Reasoning
open Disambiguation
|
module Popl14.Change.Evaluation where
open import Popl14.Syntax.Type
open import Popl14.Syntax.Term
open import Popl14.Change.Type
open import Popl14.Change.Term
open import Popl14.Change.Value
open import Popl14.Denotation.Value
open import Popl14.Denotation.Evaluation
open import Relation.Binary.PropositionalEquality
open import Base.Denotation.Notation
open import Postulate.Extensionality
-- unique names with unambiguous types
-- to help type inference figure things out
private
module Disambiguation where
infixr 9 _⋆_
_⋆_ : Type → Context → Context
_⋆_ = _•_
-- Relating `diff` with `diff-term`, `apply` with `apply-term`
meaning-⊕ : ∀ {τ Γ}
{t : Term Γ τ} {Δt : Term Γ (ΔType τ)} {ρ : ⟦ Γ ⟧} →
⟦ t ⟧ ρ ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕ Δt ⟧ ρ
meaning-⊝ : ∀ {τ Γ}
{s : Term Γ τ} {t : Term Γ τ} {ρ : ⟦ Γ ⟧} →
⟦ s ⟧ ρ ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝ t ⟧ ρ
meaning-⊕ {base base-int} = refl
meaning-⊕ {base base-bag} = refl
meaning-⊕ {σ ⇒ τ} {Γ} {t} {Δt} {ρ} = ext (λ v →
let
Γ′ = σ ⋆ (σ ⇒ τ) ⋆ ΔType (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧
ρ′ = v • (⟦ t ⟧ ρ) • (⟦ Δt ⟧ ρ) • ρ
x : Term Γ′ σ
x = var this
f : Term Γ′ (σ ⇒ τ)
f = var (that this)
Δf : Term Γ′ (ΔType (σ ⇒ τ))
Δf = var (that (that this))
y = app f x
Δy = app (app Δf x) (x ⊝ x)
in
begin
⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (v ⟦⊝₍ σ ₎⟧ v)
≡⟨ cong (λ hole → ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v hole)
(meaning-⊝ {s = x} {x} {ρ′}) ⟩
⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (⟦ x ⊝ x ⟧ ρ′)
≡⟨ meaning-⊕ {t = y} {Δt = Δy} {ρ′} ⟩
⟦ y ⊕ Δy ⟧ ρ′
∎)
where
open ≡-Reasoning
open Disambiguation
meaning-⊝ {int} = refl
meaning-⊝ {bag} = refl
meaning-⊝ {σ ⇒ τ} {Γ} {s} {t} {ρ} =
ext (λ v → ext (λ Δv →
let
Γ′ = ΔType σ ⋆ σ ⋆ (σ ⇒ τ) ⋆ (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧Context
ρ′ = Δv • v • ⟦ t ⟧Term ρ • ⟦ s ⟧Term ρ • ρ
Δx : Term Γ′ (ΔType σ)
Δx = var this
x : Term Γ′ σ
x = var (that this)
f : Term Γ′ (σ ⇒ τ)
f = var (that (that this))
g : Term Γ′ (σ ⇒ τ)
g = var (that (that (that this)))
y = app f x
y′ = app g (x ⊕ Δx)
in
begin
⟦ s ⟧ ρ (v ⟦⊕₍ σ ₎⟧ Δv) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v
≡⟨ cong (λ hole → ⟦ s ⟧ ρ hole ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v)
(meaning-⊕ {t = x} {Δt = Δx} {ρ′}) ⟩
⟦ s ⟧ ρ (⟦ x ⊕ Δx ⟧ ρ′) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v
≡⟨ meaning-⊝ {s = y′} {y} {ρ′} ⟩
⟦ y′ ⊝ y ⟧ ρ′
∎))
where
open ≡-Reasoning
open Disambiguation
|
Use ⟦⊕₍_₎⟧ and ⟦⊝₍_₎⟧ in Popl14.Change.Evaluation.
|
Use ⟦⊕₍_₎⟧ and ⟦⊝₍_₎⟧ in Popl14.Change.Evaluation.
Old-commit-hash: a05b08ff42508e49817f899523f4849e9334e057
|
Agda
|
mit
|
inc-lc/ilc-agda
|
69fe0b1567d5b5439ff7565a79deffd679e3a82b
|
Parametric/Change/Type.agda
|
Parametric/Change/Type.agda
|
module Parametric.Change.Type
(Base : Set)
where
import Parametric.Syntax.Type as Type
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
|
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
|
Make P.C.Type conform to plugin pattern.
|
Make P.C.Type conform to plugin pattern.
Old-commit-hash: 8dc76b168a0b8d9f859a69834d52d91e2f34b235
|
Agda
|
mit
|
inc-lc/ilc-agda
|
799ecf7906eb3e8eafef355dc3f96dfd5a71014e
|
one-time-semantic-security.agda
|
one-time-semantic-security.agda
|
module one-time-semantic-security where
open import Function
open import Data.Nat
open import Data.Product using (∃; module Σ; _×_; _,_; proj₁; proj₂)
open import Data.Vec using (splitAt; take; drop)
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≗_)
open import Data.Bits
open import flat-funs
open import program-distance
open import flipbased-implem
-- Capture the various size parameters required
-- for a cipher.
--
-- M is the message space (|M| is the size of messages)
-- C is the cipher text space
-- Rᵉ is the randomness used by the cipher.
record EncParams : Set where
constructor mk
field
|M| |C| |R|ᵉ : ℕ
M = Bits |M|
C = Bits |C|
Rᵉ = Bits |R|ᵉ
-- The type of the encryption function
Enc : Set
Enc = M → ↺ |R|ᵉ C
module EncParams² (ep₀ ep₁ : EncParams) where
open EncParams ep₀ public using ()
renaming (|M| to |M|₀; |C| to |C|₀; |R|ᵉ to |R|ᵉ₀; M to M₀;
C to C₀; Rᵉ to Rᵉ₀; Enc to Enc₀)
open EncParams ep₁ public using ()
renaming (|M| to |M|₁; |C| to |C|₁; |R|ᵉ to |R|ᵉ₁; M to M₁;
C to C₁; Rᵉ to Rᵉ₁; Enc to Enc₁)
Tr = Enc₀ → Enc₁
Tr⁻¹ = Enc₁ → Enc₀
module EncParams²-same-|R|ᵉ (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where
ep₁ = EncParams.mk |M|₁ |C|₁ (EncParams.|R|ᵉ ep₀)
open EncParams² ep₀ ep₁ public
module ♭EncParams {t} {T : Set t}
(♭Funs : FlatFuns T)
(ep : EncParams) where
open FlatFuns ♭Funs
open EncParams ep public
`M = `Bits |M|
`C = `Bits |C|
`Rᵉ = `Bits |R|ᵉ
module ♭EncParams² {t} {T : Set t}
(♭Funs : FlatFuns T)
(ep₀ ep₁ : EncParams) where
open EncParams² ep₀ ep₁ public
open ♭EncParams ♭Funs ep₀ public using () renaming (`M to `M₀; `C to `C₀; `Rᵉ to `Rᵉ₀)
open ♭EncParams ♭Funs ep₁ public using () renaming (`M to `M₁; `C to `C₁; `Rᵉ to `Rᵉ₁)
module AbsSemSec {t} {T : Set t}
(♭Funs : FlatFuns T) where
open FlatFuns ♭Funs
record SemSecAdv (ep : EncParams) |R|ᵃ : Set where
constructor mk
open ♭EncParams ♭Funs ep
field
{|S|} : ℕ
`S = `Bits |S|
S = Bits |S|
-- Randomness adversary
`Rᵃ = `Bits |R|ᵃ
Rᵃ = Bits |R|ᵃ
field
step₀ : `Rᵃ `→ (`M `× `M) `× `S
step₁ : `C `× `S `→ `Bit
M² = Bit → M
`M² = `Bit `→ `M
module Ops (♭ops : FlatFunsOps ♭Funs) where
open FlatFunsOps ♭ops
observe : `C `× `Rᵃ `→ (`M `× `M) `× `Bit
observe = idO *** step₀ >>> ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ >>> idO *** step₁
where ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ = < snd >>> fst , < fst , snd >>> snd > >
open ♭EncParams ♭Funs ep public
SemSecReduction : ∀ ep₀ ep₁ (f : Coins → Coins) → Set
SemSecReduction ep₀ ep₁ f = ∀ {c} → SemSecAdv ep₀ c → SemSecAdv ep₁ (f c)
-- Here we use Agda functions for FlatFuns.
module FunSemSec (prgDist : PrgDist) where
open PrgDist prgDist
open AbsSemSec fun♭Funs
open FlatFunsOps fun♭Ops
module FunSemSecAdv {ep : EncParams} {|R|ᵃ} (A : SemSecAdv ep |R|ᵃ) where
open SemSecAdv A public
open Ops fun♭Ops public
step₀F : Rᵃ → (M² × S)
step₀F = step₀ >>> proj *** idO
step₀↺ : ↺ |R|ᵃ (M² × S)
step₀↺ = mk step₀F
step₁F : S → C → Bit
step₁F s c = step₁ (c , s)
module RunSemSec (ep : EncParams) where
open EncParams ep using (M; C; |R|ᵉ; Enc)
-- Returing 0 means Chal wins, Adv looses
-- 1 means Adv wins, Chal looses
runSemSec : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
runSemSec E A b
= A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) }
where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁)
_⇄_ : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
_⇄_ = runSemSec
_≗A_ : ∀ {p} (A₁ A₂ : SemSecAdv ep p) → Set
A₀ ≗A A₁ = observe A₀ ≗ observe A₁
where open FunSemSecAdv
change-adv : ∀ {ca E} {A₁ A₂ : SemSecAdv ep ca} → A₁ ≗A A₂ → (E ⇄ A₁) ≗⅁ (E ⇄ A₂)
change-adv {ca = ca} {A₁ = _} {_} pf b R with splitAt ca R
change-adv {E = E} {A₁} {A₂} pf b ._ | pre Σ., post , ≡.refl = ≡.trans (≡.cong proj₂ (helper₀ A₁)) helper₂
where open FunSemSecAdv
helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post , pre)
helper₂ = ≡.cong (λ m → step₁ A₂ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₂ pre)))
(helper₀ A₂)
_≗E_ : ∀ (E₀ E₁ : Enc) → Set
E₀ ≗E E₁ = ∀ m → E₀ m ≗↺ E₁ m
≗E→≗⅁ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c)
→ (E₀ ⇄ A) ≗⅁ (E₁ ⇄ A)
≗E→≗⅁ E₀≗E₁ {c} A b R
rewrite E₀≗E₁ (proj (proj₁ (SemSecAdv.step₀ A (take c R))) b) (drop c R) = ≡.refl
≗E→⇓ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c) → (E₀ ⇄ A) ⇓ (E₁ ⇄ A)
≗E→⇓ E₀≗E₁ A = extensional-reduction (≗E→≗⅁ E₀≗E₁ A)
module SemSecReductions (ep₀ ep₁ : EncParams) (f : Coins → Coins) where
open EncParams² ep₀ ep₁
open RunSemSec ep₀ public using () renaming (_⇄_ to _⇄₀_; _≗E_ to _≗E₀_; ≗E→⇓ to ≗E→⇓₀)
open RunSemSec ep₁ public using () renaming (_⇄_ to _⇄₁_; _≗E_ to _≗E₁_; ≗E→⇓ to ≗E→⇓₁)
Reduction : Set
Reduction = SemSecReduction ep₁ ep₀ f
SemSecTr : Tr → Set
SemSecTr tr =
∃ λ (red : Reduction) →
∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
SemSecTr⁻¹ : Tr⁻¹ → Set
SemSecTr⁻¹ tr⁻¹ =
∃ λ (red : Reduction) →
∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
SemSecTr→SemSecTr⁻¹ : ∀ tr tr⁻¹ (tr-right-inv : ∀ E → tr (tr⁻¹ E) ≗E₁ E)
→ SemSecTr tr → SemSecTr⁻¹ tr⁻¹
SemSecTr→SemSecTr⁻¹ _ tr⁻¹ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
helper E A A-breaks-E = pf (tr⁻¹ E) A A-breaks-tr-tr⁻¹-E
where A-breaks-tr-tr⁻¹-E = ≗E→⇓₁ (λ m R → ≡.sym (tr-inv E m R)) A A-breaks-E
SemSecTr⁻¹→SemSecTr : ∀ tr tr⁻¹ (tr-left-inv : ∀ E → tr⁻¹ (tr E) ≗E₀ E)
→ SemSecTr⁻¹ tr⁻¹ → SemSecTr tr
SemSecTr⁻¹→SemSecTr tr _ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
helper E {c} A A-breaks-E = ≗E→⇓₀ (tr-inv E) (red A) (pf (tr E) A A-breaks-E)
|
module one-time-semantic-security where
open import Function
open import Data.Nat
open import Data.Product using (∃; module Σ; _×_; _,_; proj₁; proj₂)
open import Data.Vec using (splitAt; take; drop)
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≗_)
open import Data.Bits
open import flat-funs
open import program-distance
open import flipbased-implem
-- Capture the various size parameters required
-- for a cipher.
--
-- M is the message space (|M| is the size of messages)
-- C is the cipher text space
-- Rᵉ is the randomness used by the cipher.
record EncParams : Set where
constructor mk
field
|M| |C| |R|ᵉ : ℕ
M = Bits |M|
C = Bits |C|
Rᵉ = Bits |R|ᵉ
-- The type of the encryption function
Enc : Set
Enc = M → ↺ |R|ᵉ C
module EncParams² (ep₀ ep₁ : EncParams) where
open EncParams ep₀ public using ()
renaming (|M| to |M|₀; |C| to |C|₀; |R|ᵉ to |R|ᵉ₀; M to M₀;
C to C₀; Rᵉ to Rᵉ₀; Enc to Enc₀)
open EncParams ep₁ public using ()
renaming (|M| to |M|₁; |C| to |C|₁; |R|ᵉ to |R|ᵉ₁; M to M₁;
C to C₁; Rᵉ to Rᵉ₁; Enc to Enc₁)
Tr = Enc₀ → Enc₁
Tr⁻¹ = Enc₁ → Enc₀
module EncParams²-same-|R|ᵉ (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where
ep₁ = EncParams.mk |M|₁ |C|₁ (EncParams.|R|ᵉ ep₀)
open EncParams² ep₀ ep₁ public
module ♭EncParams {t} {T : Set t}
(♭Funs : FlatFuns T)
(ep : EncParams) where
open FlatFuns ♭Funs
open EncParams ep public
`M = `Bits |M|
`C = `Bits |C|
`Rᵉ = `Bits |R|ᵉ
module ♭EncParams² {t} {T : Set t}
(♭Funs : FlatFuns T)
(ep₀ ep₁ : EncParams) where
open EncParams² ep₀ ep₁ public
open ♭EncParams ♭Funs ep₀ public using () renaming (`M to `M₀; `C to `C₀; `Rᵉ to `Rᵉ₀)
open ♭EncParams ♭Funs ep₁ public using () renaming (`M to `M₁; `C to `C₁; `Rᵉ to `Rᵉ₁)
module AbsSemSec {t} {T : Set t}
(♭Funs : FlatFuns T) where
open FlatFuns ♭Funs
record SemSecAdv (ep : EncParams) |R|ᵃ : Set where
constructor mk
open ♭EncParams ♭Funs ep
field
{|S|} : ℕ
`S = `Bits |S|
S = Bits |S|
-- Randomness adversary
`Rᵃ = `Bits |R|ᵃ
Rᵃ = Bits |R|ᵃ
field
step₀ : `Rᵃ `→ (`M `× `M) `× `S
step₁ : `C `× `S `→ `Bit
M² = Bit → M
`M² = `Bit `→ `M
module Ops (♭ops : FlatFunsOps ♭Funs) where
open FlatFunsOps ♭ops
observe : `C `× `Rᵃ `→ (`M `× `M) `× `Bit
observe = idO *** step₀ >>> ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ >>> idO *** step₁
where ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ = < snd >>> fst , < fst , snd >>> snd > >
open ♭EncParams ♭Funs ep public
SemSecReduction : ∀ ep₀ ep₁ (f : Coins → Coins) → Set
SemSecReduction ep₀ ep₁ f = ∀ {c} → SemSecAdv ep₀ c → SemSecAdv ep₁ (f c)
-- Here we use Agda functions for FlatFuns.
module FunSemSec (prgDist : PrgDist) where
open PrgDist prgDist
open AbsSemSec fun♭Funs
open FlatFunsOps fun♭Ops
module FunSemSecAdv {ep : EncParams} {|R|ᵃ} (A : SemSecAdv ep |R|ᵃ) where
open SemSecAdv A public
open Ops fun♭Ops public
step₀F : Rᵃ → (M² × S)
step₀F = step₀ >>> proj *** idO
step₀↺ : ↺ |R|ᵃ (M² × S)
step₀↺ = mk step₀F
step₁F : S → C → Bit
step₁F s c = step₁ (c , s)
module RunSemSec (ep : EncParams) where
open EncParams ep using (M; C; |R|ᵉ; Enc)
-- Returing 0 means Chal wins, Adv looses
-- 1 means Adv wins, Chal looses
runSemSec : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
runSemSec E A b
= A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) }
where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁)
_⇄_ : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
_⇄_ = runSemSec
_≗A_ : ∀ {p} (A₁ A₂ : SemSecAdv ep p) → Set
A₀ ≗A A₁ = observe A₀ ≗ observe A₁
where open FunSemSecAdv
change-adv : ∀ {ca} (A₀ A₁ : SemSecAdv ep ca) E → A₀ ≗A A₁ → (E ⇄ A₀) ≗⅁ (E ⇄ A₁)
change-adv {ca} _ _ _ pf b R with splitAt ca R
change-adv A₀ A₁ E pf b ._ | pre Σ., post , ≡.refl = ≡.trans (≡.cong proj₂ (helper₀ A₀)) helper₂
where open FunSemSecAdv
helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post , pre)
helper₂ = ≡.cong (λ m → step₁ A₁ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₁ pre)))
(helper₀ A₁)
_≗E_ : ∀ (E₀ E₁ : Enc) → Set
E₀ ≗E E₁ = ∀ m → E₀ m ≗↺ E₁ m
≗E→≗⅁ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c)
→ (E₀ ⇄ A) ≗⅁ (E₁ ⇄ A)
≗E→≗⅁ E₀≗E₁ {c} A b R
rewrite E₀≗E₁ (proj (proj₁ (SemSecAdv.step₀ A (take c R))) b) (drop c R) = ≡.refl
≗A→⇓ : ∀ {c} (A₀ A₁ : SemSecAdv ep c) → A₀ ≗A A₁ → ∀ E → (E ⇄ A₀) ⇓ (E ⇄ A₁)
≗A→⇓ A₀ A₁ A₀≗A₁ E = extensional-reduction (change-adv A₀ A₁ E A₀≗A₁)
≗E→⇓ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c) → (E₀ ⇄ A) ⇓ (E₁ ⇄ A)
≗E→⇓ E₀≗E₁ A = extensional-reduction (≗E→≗⅁ E₀≗E₁ A)
module SemSecReductions (ep₀ ep₁ : EncParams) (f : Coins → Coins) where
open EncParams² ep₀ ep₁
open RunSemSec ep₀ public using () renaming (_⇄_ to _⇄₀_; _≗E_ to _≗E₀_; ≗E→⇓ to ≗E→⇓₀)
open RunSemSec ep₁ public using () renaming (_⇄_ to _⇄₁_; _≗E_ to _≗E₁_; ≗E→⇓ to ≗E→⇓₁)
Reduction : Set
Reduction = SemSecReduction ep₁ ep₀ f
SemSecTr : Tr → Set
SemSecTr tr =
∃ λ (red : Reduction) →
∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
SemSecTr⁻¹ : Tr⁻¹ → Set
SemSecTr⁻¹ tr⁻¹ =
∃ λ (red : Reduction) →
∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
SemSecTr→SemSecTr⁻¹ : ∀ tr tr⁻¹ (tr-right-inv : ∀ E → tr (tr⁻¹ E) ≗E₁ E)
→ SemSecTr tr → SemSecTr⁻¹ tr⁻¹
SemSecTr→SemSecTr⁻¹ _ tr⁻¹ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
helper E A A-breaks-E = pf (tr⁻¹ E) A A-breaks-tr-tr⁻¹-E
where A-breaks-tr-tr⁻¹-E = ≗E→⇓₁ (λ m R → ≡.sym (tr-inv E m R)) A A-breaks-E
SemSecTr⁻¹→SemSecTr : ∀ tr tr⁻¹ (tr-left-inv : ∀ E → tr⁻¹ (tr E) ≗E₀ E)
→ SemSecTr⁻¹ tr⁻¹ → SemSecTr tr
SemSecTr⁻¹→SemSecTr tr _ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
helper E {c} A A-breaks-E = ≗E→⇓₀ (tr-inv E) (red A) (pf (tr E) A A-breaks-E)
|
Improve change-adv and add ≗A→⇓
|
one-time-semantic-security: Improve change-adv and add ≗A→⇓
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
ab1099f8573c544e98531d58aac709002e109268
|
lib/Type.agda
|
lib/Type.agda
|
module Type where
★₂ : Set₃
★₂ = Set₂
★₁ : ★₂
★₁ = Set₁
★ : ★₁
★ = Set
|
module Type where
open import Level
★₂ : Set₃
★₂ = Set₂
★₁ : ★₂
★₁ = Set₁
★₀ : ★₁
★₀ = Set
★ : ★₁
★ = ★₀
★_ : ∀ ℓ → Set (suc ℓ)
★_ ℓ = Set ℓ
|
add ★_ for the universal type of type
|
Type: add ★_ for the universal type of type
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
244c3b4285d455271121fa1cc4982d804ffe9437
|
Solver/Linear.agda
|
Solver/Linear.agda
|
-- NOTE with-K
module Solver.Linear where
open import FunUniverse.Types
open import FunUniverse.Rewiring.Linear
open import Data.Two hiding (_≟_)
open import Data.Nat as ℕ using (ℕ)
import Solver.Linear.Syntax as LinSyn
open import Relation.Nullary.Decidable
open import Relation.Binary
open import Relation.Binary.PropositionalEquality using (_≡_ ; refl; _≢_)
open import Relation.Nullary
import Data.String as String
module String≤ = StrictTotalOrder String.strictTotalOrder
open import Data.Fin using (Fin)
open import Data.Fin.Props using (strictTotalOrder) renaming (_≟_ to _≟ᶠ_)
open import Data.Vec using (Vec; lookup)
open import Data.Vec.N-ary using (N-ary ; _$ⁿ_)
open import Data.Vec using (allFin) renaming (map to vmap)
module Syntax
{Var : Set}
(_≤V₂_ : Var → Var → 𝟚)
(_≟V_ : Decidable (_≡_ {A = Var}))
{a} {A : Set a}
{funU : FunUniverse A}
(linRewiring : LinRewiring funU)
(Γ : Var → A)
where
open FunUniverse funU
open LinRewiring linRewiring
open LinSyn Var
eval : Syn → A
eval (var x) = Γ x
eval tt = `⊤
eval (s , s₁) = eval s `× eval s₁
EvalEq : Eq → Set
EvalEq e = eval (LHS e) `→ eval (RHS e)
data R : Syn → Syn → Set a where
_``∻_ : ∀ {A B C} → R A B → R B C → R A C
`<id,tt>⁻¹ : ∀ {A} → R (A , tt) A
`<tt,id>⁻¹ : ∀ {A} → R (tt , A) A
`<tt,id> : ∀ {A} → R A (tt , A)
`<id,tt> : ∀ {A} → R A (A , tt)
⟨_``×_⟩ : ∀ {A B C D} → R A C → R B D → R (A , B) (C , D)
`assoc : ∀ {A B C} → R (A , (B , C)) ((A , B) , C)
`assoc⁻¹ : ∀ {A B C} → R ((A , B) , C) (A , (B , C))
`id : ∀ {A} → R A A
`swap : ∀ {A B} → R (A , B) (B , A)
`⟨_×_⟩ : ∀ {A B C D} → R A C → R B D → R (A , B) (C , D)
`⟨ `id × `id ⟩ = `id
`⟨ r₁ × r₂ ⟩ = ⟨ r₁ ``× r₂ ⟩
_∻_ : ∀ {A B C} → R A B → R B C → R A C
`id ∻ r₂ = r₂
r₁ ∻ `id = r₁
`<tt,id>⁻¹ ∻ `<tt,id> = `id
`<id,tt>⁻¹ ∻ `<id,tt> = `id
`<tt,id> ∻ `<tt,id>⁻¹ = `id
`<id,tt> ∻ `<id,tt>⁻¹ = `id
`swap ∻ `<id,tt>⁻¹ = `<tt,id>⁻¹
`swap ∻ `<tt,id>⁻¹ = `<id,tt>⁻¹
`<id,tt> ∻ `swap = `<tt,id>
`<tt,id> ∻ `swap = `<id,tt>
`assoc ∻ `assoc⁻¹ = `id
`assoc ∻ (`assoc⁻¹ ``∻ r) = r
`assoc⁻¹ ∻ `assoc = `id
`assoc⁻¹ ∻ (`assoc ``∻ r) = r
`swap ∻ `swap = `id
`swap ∻ (`swap ``∻ r) = r
(r₁ ``∻ r₂) ∻ r₃ = r₁ ``∻ (r₂ ∻ r₃)
⟨ r₁ ``× r₂ ⟩ ∻ ⟨ r₃ ``× r₄ ⟩ = ⟨ r₁ ∻ r₃ ``× r₂ ∻ r₄ ⟩
⟨ r₁ ``× r₂ ⟩ ∻ ( ⟨ r₃ ``× r₄ ⟩ ``∻ r₅) with ⟨ r₁ ∻ r₃ ``× r₂ ∻ r₄ ⟩
... | `id = r₅
... | r₆ = r₆ ``∻ r₅
r₁ ∻ r₂ = r₁ ``∻ r₂
sym : ∀ {S S'} → R S S' → R S' S
sym (r ``∻ r₁) = sym r₁ ∻ sym r
sym `<id,tt> = `<id,tt>⁻¹
sym `<tt,id> = `<tt,id>⁻¹
sym `<id,tt>⁻¹ = `<id,tt>
sym `<tt,id>⁻¹ = `<tt,id>
sym ⟨ r ``× r₁ ⟩ = ⟨ sym r ``× sym r₁ ⟩
sym `assoc = `assoc⁻¹
sym `assoc⁻¹ = `assoc
sym `id = `id
sym `swap = `swap
proof₁ : ∀ {S S'} → R S S' → eval S `→ eval S'
proof₁ (r ``∻ r₁) = proof₁ r ⁏ proof₁ r₁
proof₁ `<id,tt> = <id,tt>
proof₁ `<tt,id> = <tt,id>
proof₁ `<id,tt>⁻¹ = fst<,tt>
proof₁ `<tt,id>⁻¹ = snd<tt,>
proof₁ ⟨ `id ``× r ⟩ = second (proof₁ r)
proof₁ ⟨ r ``× `id ⟩ = first (proof₁ r)
proof₁ ⟨ r ``× r₁ ⟩ = < proof₁ r × proof₁ r₁ >
proof₁ `assoc = assoc′
proof₁ `assoc⁻¹ = assoc
proof₁ `id = id
proof₁ `swap = swap
proof₂ : ∀ {S S'} → R S S' → eval S' `→ eval S
proof₂ r = proof₁ (sym r)
data NF : Syn → Set a where
tt : NF tt
var : ∀ x → NF (var x)
var_::_ : ∀ {S} i → NF S → NF (var i , S)
record NFP S : Set a where
constructor _⊢_
field
{S'} : Syn
term : NF S'
proof : R S' S
merge : ∀ {S S'} → NF S → NF S' → NFP (S , S')
merge tt n2 = n2 ⊢ `<tt,id>
merge (var i) n2 = (var i :: n2) ⊢ `id
merge (var i :: n1) n2 with merge n1 n2
... | t ⊢ p = (var i :: t) ⊢ (⟨ `id ``× p ⟩ ∻ `assoc)
norm : (x : Syn) → NFP x
norm (var x) = (var x) ⊢ `id
norm tt = tt ⊢ `id
norm (x , x₁) with norm x | norm x₁
... | t1 ⊢ p1 | t2 ⊢ p2 with merge t1 t2
... | t3 ⊢ p3 = t3 ⊢ (p3 ∻ ⟨ p1 ``× p2 ⟩)
insert : ∀ {S} x → NF S → NFP (var x , S)
insert y tt = (var y) ⊢ `<id,tt>
insert y (var i) with y ≤V₂ i
... | 1₂ = (var y :: var i) ⊢ `id
... | 0₂ = (var i :: var y) ⊢ `swap
insert y (var i :: n1) with y ≤V₂ i
... | 1₂ = (var y :: (var i :: n1)) ⊢ `id
... | 0₂ with insert y n1
... | t ⊢ p = (var i :: t) ⊢ (⟨ `id ``× p ⟩ ∻ (`assoc ∻ (⟨ `swap ``× `id ⟩ ∻ `assoc⁻¹)))
sort : ∀ {x : Syn} → NF x → NFP x
sort tt = tt ⊢ `id
sort (var i) = var i ⊢ `id
sort (var i :: n1) with sort n1
... | t1 ⊢ p1 with insert i t1
... | t2 ⊢ p2 = t2 ⊢ (p2 ∻ ⟨ `id ``× p1 ⟩)
normal : (x : Syn) → NFP x
normal x with norm x
... | t1 ⊢ p1 with sort t1
... | t2 ⊢ p2 = t2 ⊢ (p2 ∻ p1)
import Data.Unit
import Data.Empty
id≡ : ∀ {S S'} → S ≡ S' → R S S'
id≡ refl = `id
var-inj : ∀ {i j} → i ≢ j → Syn.var i ≢ var j
var-inj p refl = p refl
,-inj₁ : ∀ {x y a b} → x ≢ y → (x Syn., a) ≢ (y , b)
,-inj₁ p refl = p refl
,-inj₂ : ∀ {x y a b} → a ≢ b → (x Syn., a) ≢ (y , b)
,-inj₂ p refl = p refl
_≟_ : (x y : Syn) → Dec (x ≡ y)
var x ≟ var x₁ with x ≟V x₁
var .x₁ ≟ var x₁ | yes refl = yes refl
... | no p = no (var-inj p)
var x ≟ tt = no (λ ())
var x ≟ (y , y₁) = no (λ ())
tt ≟ var x = no (λ ())
tt ≟ tt = yes refl
tt ≟ (y , y₁) = no (λ ())
(x , x₁) ≟ var x₂ = no (λ ())
(x , x₁) ≟ tt = no (λ ())
(x , x₁) ≟ (y , y₁) with x ≟ y | x₁ ≟ y₁
(x , x₁) ≟ (.x , .x₁) | yes refl | yes refl = yes refl
(x , x₁) ≟ (y , y₁) | yes p | no ¬p = no (,-inj₂ ¬p)
(x , x₁) ≟ (y , y₁) | no ¬p | q = no (,-inj₁ ¬p)
data equation-not-ok : Set where
CHECK : Syn → Syn → Set
CHECK s1 s2 with s1 ≟ s2
... | yes p = Data.Unit.⊤
... | no p = equation-not-ok
EqOk? : Eq → Set
EqOk? e = CHECK (NFP.S' (normal (LHS e))) (NFP.S' (normal (RHS e)))
rewire : (e : Eq) {prf : EqOk? e} → EvalEq e
rewire (s₁ ↦ s₂) {eq} with NFP.S' (normal s₁) ≟ NFP.S' (normal s₂)
... | yes p = proof₁
((sym (NFP.proof (normal s₁)) ∻ id≡ p) ∻ NFP.proof (normal s₂))
rewire (_ ↦ _) {()} | no _
-- proof₂ (NFP.proof (normal s₁)) ∻' (eq ∻' proof₁ (NFP.proof (normal s₂)))
module Syntaxᶠ {a} {A : Set a} {funU : FunUniverse A} (linRewiring : LinRewiring funU) where
module _ {n} where
open LinSyn (Fin n) public
module _ {n} (Γ : Vec A n)
(f : N-ary n (LinSyn.Syn (Fin n)) (LinSyn.Eq (Fin n)))
where
open Syntax {Fin n} ((λ x y → ⌊ StrictTotalOrder._<?_ (strictTotalOrder n) x y ⌋))
_≟ᶠ_ {a} {A} {funU} linRewiring (λ i → lookup i Γ) public
private
e = f $ⁿ vmap Syn.var (allFin n)
rewireᶠ : {e-ok : EqOk? e} → EvalEq e
rewireᶠ {e-ok} = rewire e {e-ok}
module Syntaxˢ {a} {A} {funU} linRewiring Γ where
open Syntax (λ x y → ⌊ String≤._<?_ x y ⌋) String._≟_ {a} {A} {funU} linRewiring Γ public
open import Solver.Linear.Parser
rewireˢ : ∀ s {s-ok} →
let e = parseEqˢ s {s-ok} in
{e-ok : EqOk? e} → EvalEq e
rewireˢ s {s-ok} {e-ok} = rewire (parseEqˢ s {s-ok}) {e-ok}
-- -}
-- -}
-- -}
|
-- NOTO with-K
module Solver.Linear where
open import FunUniverse.Types
open import FunUniverse.Rewiring.Linear
open import Data.Two hiding (_≟_)
open import Data.Nat as ℕ using (ℕ)
import Solver.Linear.Syntax as LinSyn
open import Relation.Nullary.Decidable
open import Relation.Binary
open import Relation.Binary.PropositionalEquality using (_≡_ ; refl; _≢_)
open import Relation.Nullary
import Data.String as String
module String≤ = StrictTotalOrder String.strictTotalOrder
open import Data.Fin using (Fin)
open import Data.Fin.Props using (strictTotalOrder) renaming (_≟_ to _≟ᶠ_)
open import Data.Vec using (Vec; lookup)
open import Data.Vec.N-ary using (N-ary ; _$ⁿ_)
open import Data.Vec using (allFin) renaming (map to vmap)
module Syntax
{Var : Set}
(_≤V₂_ : Var → Var → 𝟚)
(_≟V_ : Decidable (_≡_ {A = Var}))
{a} {A : Set a}
{funU : FunUniverse A}
(linRewiring : LinRewiring funU)
(Γ : Var → A)
where
open FunUniverse funU
open LinRewiring linRewiring
open LinSyn Var
eval : Syn → A
eval (var x) = Γ x
eval tt = `⊤
eval (s , s₁) = eval s `× eval s₁
EvalEq : Eq → Set
EvalEq e = eval (LHS e) `→ eval (RHS e)
data R : Syn → Syn → Set a where
_``∻_ : ∀ {A B C} → R A B → R B C → R A C
`<id,tt>⁻¹ : ∀ {A} → R (A , tt) A
`<tt,id>⁻¹ : ∀ {A} → R (tt , A) A
`<tt,id> : ∀ {A} → R A (tt , A)
`<id,tt> : ∀ {A} → R A (A , tt)
⟨_``×_⟩ : ∀ {A B C D} → R A C → R B D → R (A , B) (C , D)
`assoc : ∀ {A B C} → R (A , (B , C)) ((A , B) , C)
`assoc⁻¹ : ∀ {A B C} → R ((A , B) , C) (A , (B , C))
`id : ∀ {A} → R A A
`swap : ∀ {A B} → R (A , B) (B , A)
`⟨_×_⟩ : ∀ {A B C D} → R A C → R B D → R (A , B) (C , D)
`⟨ `id × `id ⟩ = `id
`⟨ r₁ × r₂ ⟩ = ⟨ r₁ ``× r₂ ⟩
_∻_ : ∀ {A B C} → R A B → R B C → R A C
`id ∻ r₂ = r₂
r₁ ∻ `id = r₁
`<tt,id>⁻¹ ∻ `<tt,id> = `id
`<id,tt>⁻¹ ∻ `<id,tt> = `id
`<tt,id> ∻ `<tt,id>⁻¹ = `id
`<id,tt> ∻ `<id,tt>⁻¹ = `id
`swap ∻ `<id,tt>⁻¹ = `<tt,id>⁻¹
`swap ∻ `<tt,id>⁻¹ = `<id,tt>⁻¹
`<id,tt> ∻ `swap = `<tt,id>
`<tt,id> ∻ `swap = `<id,tt>
`assoc ∻ `assoc⁻¹ = `id
`assoc ∻ (`assoc⁻¹ ``∻ r) = r
`assoc⁻¹ ∻ `assoc = `id
`assoc⁻¹ ∻ (`assoc ``∻ r) = r
`swap ∻ `swap = `id
`swap ∻ (`swap ``∻ r) = r
(r₁ ``∻ r₂) ∻ r₃ = r₁ ``∻ (r₂ ∻ r₃)
⟨ r₁ ``× r₂ ⟩ ∻ ⟨ r₃ ``× r₄ ⟩ = ⟨ r₁ ∻ r₃ ``× r₂ ∻ r₄ ⟩
⟨ r₁ ``× r₂ ⟩ ∻ ( ⟨ r₃ ``× r₄ ⟩ ``∻ r₅) with ⟨ r₁ ∻ r₃ ``× r₂ ∻ r₄ ⟩
... | `id = r₅
... | r₆ = r₆ ``∻ r₅
r₁ ∻ r₂ = r₁ ``∻ r₂
sym : ∀ {S S'} → R S S' → R S' S
sym (r ``∻ r₁) = sym r₁ ∻ sym r
sym `<id,tt> = `<id,tt>⁻¹
sym `<tt,id> = `<tt,id>⁻¹
sym `<id,tt>⁻¹ = `<id,tt>
sym `<tt,id>⁻¹ = `<tt,id>
sym ⟨ r ``× r₁ ⟩ = ⟨ sym r ``× sym r₁ ⟩
sym `assoc = `assoc⁻¹
sym `assoc⁻¹ = `assoc
sym `id = `id
sym `swap = `swap
proof₁ : ∀ {S S'} → R S S' → eval S `→ eval S'
proof₁ (r ``∻ r₁) = proof₁ r ⁏ proof₁ r₁
proof₁ `<id,tt> = <id,tt>
proof₁ `<tt,id> = <tt,id>
proof₁ `<id,tt>⁻¹ = fst<,tt>
proof₁ `<tt,id>⁻¹ = snd<tt,>
proof₁ ⟨ `id ``× r ⟩ = second (proof₁ r)
proof₁ ⟨ r ``× `id ⟩ = first (proof₁ r)
proof₁ ⟨ r ``× r₁ ⟩ = < proof₁ r × proof₁ r₁ >
proof₁ `assoc = assoc′
proof₁ `assoc⁻¹ = assoc
proof₁ `id = id
proof₁ `swap = swap
proof₂ : ∀ {S S'} → R S S' → eval S' `→ eval S
proof₂ r = proof₁ (sym r)
data NF : Syn → Set a where
tt : NF tt
var : ∀ x → NF (var x)
var_::_ : ∀ {S} i → NF S → NF (var i , S)
record NFP S : Set a where
constructor _⊢_
field
{S'} : Syn
term : NF S'
proof : R S' S
merge : ∀ {S S'} → NF S → NF S' → NFP (S , S')
merge tt n2 = n2 ⊢ `<tt,id>
merge (var i) n2 = (var i :: n2) ⊢ `id
merge (var i :: n1) n2 with merge n1 n2
... | t ⊢ p = (var i :: t) ⊢ (⟨ `id ``× p ⟩ ∻ `assoc)
norm : (x : Syn) → NFP x
norm (var x) = (var x) ⊢ `id
norm tt = tt ⊢ `id
norm (x , x₁) with norm x | norm x₁
... | t1 ⊢ p1 | t2 ⊢ p2 with merge t1 t2
... | t3 ⊢ p3 = t3 ⊢ (p3 ∻ ⟨ p1 ``× p2 ⟩)
insert : ∀ {S} x → NF S → NFP (var x , S)
insert y tt = (var y) ⊢ `<id,tt>
insert y (var i) with y ≤V₂ i
... | 1₂ = (var y :: var i) ⊢ `id
... | 0₂ = (var i :: var y) ⊢ `swap
insert y (var i :: n1) with y ≤V₂ i
... | 1₂ = (var y :: (var i :: n1)) ⊢ `id
... | 0₂ with insert y n1
... | t ⊢ p = (var i :: t) ⊢ (⟨ `id ``× p ⟩ ∻ (`assoc ∻ (⟨ `swap ``× `id ⟩ ∻ `assoc⁻¹)))
sort : ∀ {x : Syn} → NF x → NFP x
sort tt = tt ⊢ `id
sort (var i) = var i ⊢ `id
sort (var i :: n1) with sort n1
... | t1 ⊢ p1 with insert i t1
... | t2 ⊢ p2 = t2 ⊢ (p2 ∻ ⟨ `id ``× p1 ⟩)
normal : (x : Syn) → NFP x
normal x with norm x
... | t1 ⊢ p1 with sort t1
... | t2 ⊢ p2 = t2 ⊢ (p2 ∻ p1)
import Data.Unit
import Data.Empty
id≡ : ∀ {S S'} → S ≡ S' → R S S'
id≡ refl = `id
var-inj : ∀ {i j} → i ≢ j → Syn.var i ≢ var j
var-inj p refl = p refl
,-inj₁ : ∀ {x y a b} → x ≢ y → (x Syn., a) ≢ (y , b)
,-inj₁ p refl = p refl
,-inj₂ : ∀ {x y a b} → a ≢ b → (x Syn., a) ≢ (y , b)
,-inj₂ p refl = p refl
_≟_ : (x y : Syn) → Dec (x ≡ y)
var x ≟ var x₁ with x ≟V x₁
var .x₁ ≟ var x₁ | yes refl = yes refl
... | no p = no (var-inj p)
var x ≟ tt = no (λ ())
var x ≟ (y , y₁) = no (λ ())
tt ≟ var x = no (λ ())
tt ≟ tt = yes refl
tt ≟ (y , y₁) = no (λ ())
(x , x₁) ≟ var x₂ = no (λ ())
(x , x₁) ≟ tt = no (λ ())
(x , x₁) ≟ (y , y₁) with x ≟ y | x₁ ≟ y₁
(x , x₁) ≟ (.x , .x₁) | yes refl | yes refl = yes refl
(x , x₁) ≟ (y , y₁) | yes p | no ¬p = no (,-inj₂ ¬p)
(x , x₁) ≟ (y , y₁) | no ¬p | q = no (,-inj₁ ¬p)
data equation-not-ok : Set where
CHECK : Syn → Syn → Set
CHECK s1 s2 with s1 ≟ s2
... | yes p = Data.Unit.⊤
... | no p = equation-not-ok
EqOk? : Eq → Set
EqOk? e = CHECK (NFP.S' (normal (LHS e))) (NFP.S' (normal (RHS e)))
rewire : (e : Eq) {prf : EqOk? e} → EvalEq e
rewire (s₁ ↦ s₂) {eq} with NFP.S' (normal s₁) ≟ NFP.S' (normal s₂)
... | yes p = proof₁
((sym (NFP.proof (normal s₁)) ∻ id≡ p) ∻ NFP.proof (normal s₂))
rewire (_ ↦ _) {()} | no _
-- proof₂ (NFP.proof (normal s₁)) ∻' (eq ∻' proof₁ (NFP.proof (normal s₂)))
module Syntaxᶠ {a} {A : Set a} {funU : FunUniverse A} (linRewiring : LinRewiring funU) where
module _ {n} where
open LinSyn (Fin n) public
module _ {n} (Γ : Vec A n)
(f : N-ary n (LinSyn.Syn (Fin n)) (LinSyn.Eq (Fin n)))
where
open Syntax {Fin n} ((λ x y → ⌊ StrictTotalOrder._<?_ (strictTotalOrder n) x y ⌋))
_≟ᶠ_ {a} {A} {funU} linRewiring (λ i → lookup i Γ) public
private
e = f $ⁿ vmap Syn.var (allFin n)
rewireᶠ : {e-ok : EqOk? e} → EvalEq e
rewireᶠ {e-ok} = rewire e {e-ok}
module Syntaxˢ {a} {A} {funU} linRewiring Γ where
open Syntax (λ x y → ⌊ String≤._<?_ x y ⌋) String._≟_ {a} {A} {funU} linRewiring Γ public
open import Solver.Linear.Parser
rewireˢ : ∀ s {s-ok} →
let e = parseEqˢ s {s-ok} in
{e-ok : EqOk? e} → EvalEq e
rewireˢ s {s-ok} {e-ok} = rewire (parseEqˢ s {s-ok}) {e-ok}
-- -}
-- -}
-- -}
|
Fix a note
|
Fix a note
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
23195232ac9f45758a208ab48715260ca5c37781
|
FunUniverse/Rewiring/Linear.agda
|
FunUniverse/Rewiring/Linear.agda
|
{-# OPTIONS --without-K #-}
open import Level.NP
open import Type
open import FunUniverse.Types
open import Data.One using (𝟙)
import Data.Vec as V
open V using (Vec; []; _∷_)
open import Data.Nat.NP using (ℕ; zero; suc; _+_; _*_; 2^_)
import FunUniverse.Category
import FunUniverse.Defaults.FirstPart as Defaults⟨first-part⟩
module FunUniverse.Rewiring.Linear where
record LinRewiring {t} {T : ★_ t} (funU : FunUniverse T) : ★_ t where
constructor mk
open FunUniverse funU
open FunUniverse.Category _`→_
field
cat : Category
open Category cat
field
-- Products (group 2 primitive functions or derived from group 1)
first : ∀ {A B C} → (A `→ C) → (A `× B) `→ (C `× B)
swap : ∀ {A B} → (A `× B) `→ (B `× A)
assoc : ∀ {A B C} → ((A `× B) `× C) `→ (A `× (B `× C))
<tt,id> : ∀ {A} → A `→ `𝟙 `× A
snd<tt,> : ∀ {A} → `𝟙 `× A `→ A
-- Products (derived from group 1 or 2)
<_×_> : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D)
second : ∀ {A B C} → (B `→ C) → (A `× B) `→ (A `× C)
-- Vectors
tt→[] : ∀ {A} → `𝟙 `→ `Vec A 0
[]→tt : ∀ {A} → `Vec A 0 `→ `𝟙
<∷> : ∀ {n A} → (A `× `Vec A n) `→ `Vec A (1 + n)
uncons : ∀ {n A} → `Vec A (1 + n) `→ (A `× `Vec A n)
open Defaults⟨first-part⟩ funU
open CompositionNotations _∘_ public
open DefaultAssoc′ _∘_ assoc swap first public
infixr 3 _***_
_***_ : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D)
f *** g = < f × g >
<id,tt> : ∀ {A} → A `→ A `× `𝟙
<id,tt> = <tt,id> ⁏ swap
<tt⁏_,_> : ∀ {A B C} → (`𝟙 `→ B) → (A `→ C) → A `→ B `× C
<tt⁏ f , g > = <tt,id> ⁏ < f × g >
<_,tt⁏_> : ∀ {A B C} → (A `→ B) → (`𝟙 `→ C) → A `→ B `× C
< f ,tt⁏ g > = <tt⁏ g , f > ⁏ swap
fst<,tt> : ∀ {A} → A `× `𝟙 `→ A
fst<,tt> = swap ⁏ snd<tt,>
fst<,[]> : ∀ {A B} → A `× `Vec B 0 `→ A
fst<,[]> = second []→tt ⁏ fst<,tt>
snd<[],> : ∀ {A B} → `Vec A 0 `× B `→ B
snd<[],> = first []→tt ⁏ snd<tt,>
-- Like first, but applies on a triple associated the other way
assoc-first : ∀ {A B C D E} → (A `× B `→ D `× E) → A `× B `× C `→ D `× E `× C
assoc-first f = assoc′ ⁏ first f ⁏ assoc
-- Like assoc-first but for second
assoc-second : ∀ {A B C D E} → (B `× C `→ E `× D) → (A `× B) `× C `→ (A `× E) `× D
assoc-second f = assoc ⁏ second f ⁏ assoc′
swap-top : ∀ {A B C} → A `× B `× C `→ B `× A `× C
swap-top = assoc-first swap
around : ∀ {A B C D X} → (A `× B `→ C `× D) → A `× X `× B `→ C `× X `× D
around f = swap-top ⁏ second f ⁏ swap-top
inner : ∀ {A B C D} → (A `→ B) → C `× A `× D `→ C `× B `× D
inner f = swap-top ⁏ first f ⁏ swap-top
{-
X ------------> X
+---+
A --->| |---> C
| f |
B --->| |---> D
+---+
Y ------------> Y
-}
inner2 : ∀ {A B C D X Y} → (A `× B `→ C `× D) → (X `× A) `× (B `× Y) `→ (X `× C) `× (D `× Y)
inner2 f = assoc-first (assoc-second f)
<_×₁_> : ∀ {A B C D E F} → (A `× B `→ D `× E) → (C `→ F) → A `× B `× C `→ D `× E `× F
< f ×₁ g > = assoc′ ⁏ < f × g > ⁏ assoc
<_×₂_> : ∀ {A B C D E F} → (A `→ D) → (B `× C `→ E `× F) → (A `× B) `× C `→ (D `× E) `× F
< f ×₂ g > = assoc ⁏ < f × g > ⁏ assoc′
<_`zip`_> : ∀ {A B C D E F} → ((A `× B) `→ C)
→ ((D `× E) `→ F)
→ ((A `× D) `× (B `× E)) `→ (C `× F)
< f `zip` g > = inner2 swap ⁏ < f × g >
<_∷′_> : ∀ {n A B C} → (A `→ C) → (B `→ `Vec C n)
→ A `× B `→ `Vec C (1 + n)
< f ∷′ g > = < f × g > ⁏ <∷>
<_∷_> : ∀ {m n A B} → (A `→ B) → (`Vec A m `→ `Vec B n)
→ `Vec A (1 + m) `→ `Vec B (1 + n)
< f ∷ g > = uncons ⁏ < f ∷′ g >
<tt⁏_∷′_> : ∀ {n A B} → (`𝟙 `→ B) → (A `→ `Vec B n)
→ A `→ `Vec B (1 + n)
<tt⁏ f ∷′ g > = <tt⁏ f , g > ⁏ <∷>
<_∷′tt⁏_> : ∀ {n A B} → (A `→ B) → (`𝟙 `→ `Vec B n)
→ A `→ `Vec B (1 + n)
< f ∷′tt⁏ g > = < f ,tt⁏ g > ⁏ <∷>
<_∷[]> : ∀ {A B} → (A `→ B) → A `→ `Vec B 1
< f ∷[]> = < f ∷′tt⁏ tt→[] >
<∷[]> : ∀ {A} → A `→ `Vec A 1
<∷[]> = < id ∷[]>
<[],_> : ∀ {A B C} → (A `→ B) → A `→ `Vec C 0 `× B
<[], f > = <tt⁏ tt→[] , f >
<_,[]> : ∀ {A B C} → (A `→ B) → A `→ B `× `Vec C 0
< f ,[]> = < f ,tt⁏ tt→[] >
head<∷> : ∀ {A} → `Vec A 1 `→ A
head<∷> = uncons ⁏ fst<,[]>
constVec⊤ : ∀ {n a} {A : ★_ a} {B} → (A → `𝟙 `→ B) → Vec A n → `𝟙 `→ `Vec B n
constVec⊤ f [] = tt→[]
constVec⊤ f (x ∷ xs) = <tt⁏ f x ∷′ constVec⊤ f xs >
[]→[] : ∀ {A B} → `Vec A 0 `→ `Vec B 0
[]→[] = []→tt ⁏ tt→[]
<[],[]>→[] : ∀ {A B C} → (`Vec A 0 `× `Vec B 0) `→ `Vec C 0
<[],[]>→[] = fst<,[]> ⁏ []→[]
<_⊛> : ∀ {n A B} → Vec (A `→ B) n → `Vec A n `→ `Vec B n
<_⊛> [] = []→[]
<_⊛> (f ∷ fs) = < f ∷ < fs ⊛> >
foldl : ∀ {n A B} → (B `× A `→ B) → (B `× `Vec A n) `→ B
foldl {zero} f = fst<,[]>
foldl {suc n} f = second uncons
⁏ assoc′
⁏ first f
⁏ foldl f
foldl₁ : ∀ {n A} → (A `× A `→ A) → `Vec A (1 + n) `→ A
foldl₁ f = uncons ⁏ foldl f
foldr : ∀ {n A B} → (A `× B `→ B) → (`Vec A n `× B) `→ B
foldr {zero} f = snd<[],>
foldr {suc n} f = first uncons
⁏ assoc
⁏ second (foldr f)
⁏ f
foldr₁ : ∀ {n A} → (A `× A `→ A) → `Vec A (1 + n) `→ A
foldr₁ f = uncons ⁏ swap ⁏ foldr f
map : ∀ {n A B} → (A `→ B) → (`Vec A n `→ `Vec B n)
map f = < V.replicate f ⊛>
zipWith : ∀ {n A B C} → ((A `× B) `→ C)
→ (`Vec A n `× `Vec B n) `→ `Vec C n
zipWith {zero} f = <[],[]>→[]
zipWith {suc n} f = < uncons × uncons >
⁏ < f `zip` (zipWith f) >
⁏ <∷>
zip : ∀ {n A B} → (`Vec A n `× `Vec B n) `→ `Vec (A `× B) n
zip = zipWith id
snoc : ∀ {n A} → (`Vec A n `× A) `→ `Vec A (1 + n)
snoc {zero} = < snd<[],> ∷[]>
snoc {suc n} = first uncons ⁏ assoc ⁏ second snoc ⁏ <∷>
reverse : ∀ {n A} → `Vec A n `→ `Vec A n
reverse {zero} = id
reverse {suc n} = uncons ⁏ swap ⁏ first reverse ⁏ snoc
append : ∀ {m n A} → (`Vec A m `× `Vec A n) `→ `Vec A (m + n)
append {zero} = snd<[],>
append {suc m} = first uncons
⁏ assoc
⁏ second append
⁏ <∷>
<_++_> : ∀ {m n A} → (`𝟙 `→ `Vec A m) → (`𝟙 `→ `Vec A n) →
`𝟙 `→ `Vec A (m + n)
< f ++ g > = <tt⁏ f , g > ⁏ append
splitAt : ∀ m {n A} → `Vec A (m + n) `→ (`Vec A m `× `Vec A n)
splitAt zero = <[], id >
splitAt (suc m) = uncons
⁏ second (splitAt m)
⁏ assoc′
⁏ first <∷>
rot-left : ∀ m {n A} → `Vec A (m + n) `→ `Vec A (n + m)
rot-left m = splitAt m ⁏ swap ⁏ append
rot-right : ∀ {m} n {A} → `Vec A (m + n) `→ `Vec A (n + m)
rot-right {m} _ = rot-left m
folda : ∀ n {A} → (A `× A `→ A) → `Vec A (2^ n) `→ A
folda zero f = head<∷>
folda (suc n) f = splitAt (2^ n) ⁏ < folda n f × folda n f > ⁏ f
concat : ∀ {m n A} → `Vec (`Vec A m) n `→ `Vec A (n * m)
concat {n = zero} = []→[]
concat {n = suc n} = uncons ⁏ second concat ⁏ append
group : ∀ {A} n k → `Vec A (n * k) `→ `Vec (`Vec A k) n
group zero k = []→[]
group (suc n) k = splitAt k ⁏ second (group n k) ⁏ <∷>
bind : ∀ {m n A B} → (A `→ `Vec B m) → `Vec A n `→ `Vec B (n * m)
bind f = map f ⁏ concat
replicate⊤ : ∀ n → `𝟙 `→ `Vec `𝟙 n
replicate⊤ _ = constVec⊤ (λ _ → id) (V.replicate {A = 𝟙} _)
open Category cat public
|
{-# OPTIONS --without-K #-}
open import Level.NP
open import Type
open import FunUniverse.Types
open import Data.One using (𝟙)
import Data.Vec as V
open V using (Vec; []; _∷_)
open import Data.Nat.NP using (ℕ; zero; suc; _+_; _*_; 2^_)
import FunUniverse.Category
import FunUniverse.Defaults.FirstPart as Defaults⟨first-part⟩
module FunUniverse.Rewiring.Linear where
record LinRewiring {t} {T : ★_ t} (funU : FunUniverse T) : ★_ t where
constructor mk
open FunUniverse funU
open FunUniverse.Category _`→_
field
cat : Category
open Category cat
field
-- Products (group 2 primitive functions or derived from group 1)
first : ∀ {A B C} → (A `→ C) → (A `× B) `→ (C `× B)
swap : ∀ {A B} → (A `× B) `→ (B `× A)
assoc : ∀ {A B C} → ((A `× B) `× C) `→ (A `× (B `× C))
<tt,id> : ∀ {A} → A `→ `𝟙 `× A
snd<tt,> : ∀ {A} → `𝟙 `× A `→ A
-- Products (derived from group 1 or 2)
<_×_> : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D)
second : ∀ {A B C} → (B `→ C) → (A `× B) `→ (A `× C)
-- Vectors
tt→[] : ∀ {A} → `𝟙 `→ `Vec A 0
[]→tt : ∀ {A} → `Vec A 0 `→ `𝟙
<∷> : ∀ {n A} → (A `× `Vec A n) `→ `Vec A (1 + n)
uncons : ∀ {n A} → `Vec A (1 + n) `→ (A `× `Vec A n)
open Defaults⟨first-part⟩ funU
open CompositionNotations _∘_ public
open DefaultAssoc′ _∘_ assoc swap first public
infixr 3 _***_
_***_ : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D)
f *** g = < f × g >
<id,tt> : ∀ {A} → A `→ A `× `𝟙
<id,tt> = <tt,id> ⁏ swap
<tt⁏_,_> : ∀ {A B C} → (`𝟙 `→ B) → (A `→ C) → A `→ B `× C
<tt⁏ f , g > = <tt,id> ⁏ < f × g >
<_,tt⁏_> : ∀ {A B C} → (A `→ B) → (`𝟙 `→ C) → A `→ B `× C
< f ,tt⁏ g > = <tt⁏ g , f > ⁏ swap
fst<,tt> : ∀ {A} → A `× `𝟙 `→ A
fst<,tt> = swap ⁏ snd<tt,>
fst<,[]> : ∀ {A B} → A `× `Vec B 0 `→ A
fst<,[]> = second []→tt ⁏ fst<,tt>
snd<[],> : ∀ {A B} → `Vec A 0 `× B `→ B
snd<[],> = first []→tt ⁏ snd<tt,>
-- Like first, but applies on a triple associated the other way
assoc-first : ∀ {A B C D E} → (A `× B `→ D `× E) → A `× B `× C `→ D `× E `× C
assoc-first f = assoc′ ⁏ first f ⁏ assoc
-- Like assoc-first but for second
assoc-second : ∀ {A B C D E} → (B `× C `→ E `× D) → (A `× B) `× C `→ (A `× E) `× D
assoc-second f = assoc ⁏ second f ⁏ assoc′
swap-top : ∀ {A B C} → A `× B `× C `→ B `× A `× C
swap-top = assoc-first swap
around : ∀ {A B C D X} → (A `× B `→ C `× D) → A `× X `× B `→ C `× X `× D
around f = swap-top ⁏ second f ⁏ swap-top
inner : ∀ {A B C D} → (A `→ B) → C `× A `× D `→ C `× B `× D
inner f = swap-top ⁏ first f ⁏ swap-top
{-
X ------------> X
+---+
A --->| |---> C
| f |
B --->| |---> D
+---+
Y ------------> Y
-}
inner2 : ∀ {A B C D X Y} → (A `× B `→ C `× D) → (X `× A) `× (B `× Y) `→ (X `× C) `× (D `× Y)
inner2 f = assoc-first (assoc-second f)
<_×₁_> : ∀ {A B C D E F} → (A `× B `→ D `× E) → (C `→ F) → A `× B `× C `→ D `× E `× F
< f ×₁ g > = assoc′ ⁏ < f × g > ⁏ assoc
<_×₂_> : ∀ {A B C D E F} → (A `→ D) → (B `× C `→ E `× F) → (A `× B) `× C `→ (D `× E) `× F
< f ×₂ g > = assoc ⁏ < f × g > ⁏ assoc′
<_`zip`_> : ∀ {A B C D E F} → ((A `× B) `→ C)
→ ((D `× E) `→ F)
→ ((A `× D) `× (B `× E)) `→ (C `× F)
< f `zip` g > = inner2 swap ⁏ < f × g >
<_∷′_> : ∀ {n A B C} → (A `→ C) → (B `→ `Vec C n)
→ A `× B `→ `Vec C (1 + n)
< f ∷′ g > = < f × g > ⁏ <∷>
<_∷_> : ∀ {m n A B} → (A `→ B) → (`Vec A m `→ `Vec B n)
→ `Vec A (1 + m) `→ `Vec B (1 + n)
< f ∷ g > = uncons ⁏ < f ∷′ g >
<tt⁏_∷′_> : ∀ {n A B} → (`𝟙 `→ B) → (A `→ `Vec B n)
→ A `→ `Vec B (1 + n)
<tt⁏ f ∷′ g > = <tt⁏ f , g > ⁏ <∷>
<_∷′tt⁏_> : ∀ {n A B} → (A `→ B) → (`𝟙 `→ `Vec B n)
→ A `→ `Vec B (1 + n)
< f ∷′tt⁏ g > = < f ,tt⁏ g > ⁏ <∷>
<_∷[]> : ∀ {A B} → (A `→ B) → A `→ `Vec B 1
< f ∷[]> = < f ∷′tt⁏ tt→[] >
<∷[]> : ∀ {A} → A `→ `Vec A 1
<∷[]> = < id ∷[]>
<[],_> : ∀ {A B C} → (A `→ B) → A `→ `Vec C 0 `× B
<[], f > = <tt⁏ tt→[] , f >
<_,[]> : ∀ {A B C} → (A `→ B) → A `→ B `× `Vec C 0
< f ,[]> = < f ,tt⁏ tt→[] >
head<∷> : ∀ {A} → `Vec A 1 `→ A
head<∷> = uncons ⁏ fst<,[]>
constVec⊤ : ∀ {n a} {A : ★_ a} {B} → (A → `𝟙 `→ B) → Vec A n → `𝟙 `→ `Vec B n
constVec⊤ f [] = tt→[]
constVec⊤ f (x ∷ xs) = <tt⁏ f x ∷′ constVec⊤ f xs >
[]→[] : ∀ {A B} → `Vec A 0 `→ `Vec B 0
[]→[] = []→tt ⁏ tt→[]
<[],[]>→[] : ∀ {A B C} → (`Vec A 0 `× `Vec B 0) `→ `Vec C 0
<[],[]>→[] = fst<,[]> ⁏ []→[]
<_⊛> : ∀ {n A B} → Vec (A `→ B) n → `Vec A n `→ `Vec B n
<_⊛> [] = []→[]
<_⊛> (f ∷ fs) = < f ∷ < fs ⊛> >
foldl : ∀ {n A B} → (B `× A `→ B) → (B `× `Vec A n) `→ B
foldl {zero} f = fst<,[]>
foldl {suc n} f = second uncons
⁏ assoc′
⁏ first f
⁏ foldl f
foldl₁ : ∀ {n A} → (A `× A `→ A) → `Vec A (1 + n) `→ A
foldl₁ f = uncons ⁏ foldl f
foldr : ∀ {n A B} → (A `× B `→ B) → (`Vec A n `× B) `→ B
foldr {zero} f = snd<[],>
foldr {suc n} f = first uncons
⁏ assoc
⁏ second (foldr f)
⁏ f
foldr₁ : ∀ {n A} → (A `× A `→ A) → `Vec A (1 + n) `→ A
foldr₁ f = uncons ⁏ swap ⁏ foldr f
map : ∀ {n A B} → (A `→ B) → (`Vec A n `→ `Vec B n)
map f = < V.replicate f ⊛>
mapAccum : ∀ {n A B S} → (S `× A `→ S `× B) → S `× `Vec A n `→ S `× `Vec B n
mapAccum {zero} F = second []→[]
mapAccum {suc n} F = second uncons
⁏ assoc-first F ⁏ around (mapAccum F)
⁏ second <∷>
zipWith : ∀ {n A B C} → ((A `× B) `→ C)
→ (`Vec A n `× `Vec B n) `→ `Vec C n
zipWith {zero} f = <[],[]>→[]
zipWith {suc n} f = < uncons × uncons >
⁏ < f `zip` (zipWith f) >
⁏ <∷>
zip : ∀ {n A B} → (`Vec A n `× `Vec B n) `→ `Vec (A `× B) n
zip = zipWith id
snoc : ∀ {n A} → (`Vec A n `× A) `→ `Vec A (1 + n)
snoc {zero} = < snd<[],> ∷[]>
snoc {suc n} = first uncons ⁏ assoc ⁏ second snoc ⁏ <∷>
reverse : ∀ {n A} → `Vec A n `→ `Vec A n
reverse {zero} = id
reverse {suc n} = uncons ⁏ swap ⁏ first reverse ⁏ snoc
append : ∀ {m n A} → (`Vec A m `× `Vec A n) `→ `Vec A (m + n)
append {zero} = snd<[],>
append {suc m} = first uncons
⁏ assoc
⁏ second append
⁏ <∷>
<_++_> : ∀ {m n A} → (`𝟙 `→ `Vec A m) → (`𝟙 `→ `Vec A n) →
`𝟙 `→ `Vec A (m + n)
< f ++ g > = <tt⁏ f , g > ⁏ append
splitAt : ∀ m {n A} → `Vec A (m + n) `→ (`Vec A m `× `Vec A n)
splitAt zero = <[], id >
splitAt (suc m) = uncons
⁏ second (splitAt m)
⁏ assoc′
⁏ first <∷>
rot-left : ∀ m {n A} → `Vec A (m + n) `→ `Vec A (n + m)
rot-left m = splitAt m ⁏ swap ⁏ append
rot-right : ∀ {m} n {A} → `Vec A (m + n) `→ `Vec A (n + m)
rot-right {m} _ = rot-left m
folda : ∀ n {A} → (A `× A `→ A) → `Vec A (2^ n) `→ A
folda zero f = head<∷>
folda (suc n) f = splitAt (2^ n) ⁏ < folda n f × folda n f > ⁏ f
concat : ∀ {m n A} → `Vec (`Vec A m) n `→ `Vec A (n * m)
concat {n = zero} = []→[]
concat {n = suc n} = uncons ⁏ second concat ⁏ append
group : ∀ {A} n k → `Vec A (n * k) `→ `Vec (`Vec A k) n
group zero k = []→[]
group (suc n) k = splitAt k ⁏ second (group n k) ⁏ <∷>
bind : ∀ {m n A B} → (A `→ `Vec B m) → `Vec A n `→ `Vec B (n * m)
bind f = map f ⁏ concat
replicate⊤ : ∀ n → `𝟙 `→ `Vec `𝟙 n
replicate⊤ _ = constVec⊤ (λ _ → id) (V.replicate {A = 𝟙} _)
open Category cat public
|
add mapAccum
|
FunUniverse/Rewiring/Linear: add mapAccum
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
2c661467da93de9a39c40cddc61abeeca5350793
|
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
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
validity (app s t) dρ =
let
v = ⟦ t ⟧ (ignore dρ)
Δv = ⟦ t ⟧Δ dρ
in
proj₁ (validity s dρ (cons v Δv (validity t dρ)))
validity {σ ⇒ τ} (abs t) dρ = λ v →
let
v′ = after {σ} v
Δv′ = v′ ⊟₍ σ ₎ v′
dρ₁ = v • dρ
dρ₂ = nil-valid-change σ v′ • dρ
in
validity t dρ₁
,
(begin
⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂
≡⟨ correctness {t = t} {dρ₂} ⟩
⟦ t ⟧ (update dρ₂)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (update dρ₁)
≡⟨ sym (correctness {t = t} {dρ₁}) ⟩
⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁
∎) where open ≡-Reasoning
correctVar : ∀ {τ Γ} {x : Var Γ τ} {dρ : ΔEnv Γ} →
⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ)
correctVar {x = this } {cons v dv R[v,dv] • dρ} = refl
correctVar {x = that y} {cons v dv R[v,dv] • dρ} = correctVar {x = y} {dρ}
correctness {t = intlit n} = right-id-int n
correctness {t = add s t} {dρ} = trans
(mn·pq=mp·nq
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _+_ (correctness {t = s}) (correctness {t = t}))
correctness {t = minus t} {dρ} = trans
(-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong -_ (correctness {t = t}))
correctness {t = empty} = right-id-bag emptyBag
correctness {t = insert s t} {dρ} =
let
n = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
n′ = ⟦ s ⟧ (update dρ)
b′ = ⟦ t ⟧ (update dρ)
Δn = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
begin
(singletonBag n ++ b) ++
(singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b)
≡⟨ a++[b\\a]=b ⟩
singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)
≡⟨ cong₂ _++_
(cong singletonBag (correctness {t = s}))
(correctness {t = t}) ⟩
singletonBag n′ ++ b′
∎ where open ≡-Reasoning
correctness {t = union s t} {dρ} = trans
(ab·cd=ac·bd
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _++_ (correctness {t = s}) (correctness {t = t}))
correctness {t = negate t} {dρ} = trans
(-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong negateBag (correctness {t = t}))
correctness {t = flatmap s t} {dρ} =
let
f = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δf = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in trans
(a++[b\\a]=b {flatmapBag f b}
{flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf)
(b ⊞₍ base base-bag ₎ Δb)})
(cong₂ flatmapBag (correctness {t = s}) (correctness {t = t}))
correctness {t = sum t} {dρ} =
let
b = ⟦ t ⟧ (ignore dρ)
Δb = ⟦ t ⟧Δ dρ
in
trans (sym homo-sum) (cong sumBag (correctness {t = t}))
correctness {τ} {t = var x} = correctVar {τ} {x = x}
correctness {t = app {σ} {τ} s t} {dρ} =
let
f = ⟦ s ⟧ (ignore dρ)
g = ⟦ s ⟧ (update dρ)
u = ⟦ t ⟧ (ignore dρ)
v = ⟦ t ⟧ (update dρ)
Δf = ⟦ s ⟧Δ dρ
Δu = ⟦ t ⟧Δ dρ
in
begin
f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ))
≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu)
≡⟨ correctness {σ ⇒ τ} {t = s} ⟨$⟩ correctness {σ} {t = t} ⟩
g v
∎ where open ≡-Reasoning
correctness {σ ⇒ τ} {Γ} {abs t} {dρ} = ext (λ v →
let
dρ′ : ΔEnv (σ • Γ)
dρ′ = nil-valid-change σ v • dρ
in
begin
⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′
≡⟨ correctness {τ} {t = t} {dρ′} ⟩
⟦ t ⟧ (update dρ′)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (v • update dρ)
∎
) where open ≡-Reasoning
-- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv
corollary : ∀ {σ τ Γ}
(s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} →
(⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ)
(⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ)
≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ))
⊞₍ τ ₎
(⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))
corollary {σ} {τ} s t {dρ} = proj₂
(validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ))
(⟦ t ⟧Δ dρ) (validity {σ} t dρ)))
corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)}
(v : ValidChange σ)
→ ⟦ t ⟧ ∅ (after {σ} v)
≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v
corollary-closed {σ} {τ} {t = t} v =
let
f = ⟦ t ⟧ ∅
Δf = ⟦ t ⟧Δ ∅
in
begin
f (after {σ} v)
≡⟨ cong (λ hole → hole (after {σ} v))
(sym (correctness {σ ⇒ τ} {t = t})) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v)
≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩
f (before {σ} v) ⊞₍ τ ₎ Δf v
∎ where open ≡-Reasoning
|
module Denotation.Specification.Canon-Popl14 where
-- Denotation-as-specification for Calculus Popl14
--
-- Contents
-- - ⟦_⟧Δ : binding-time-shifted derive
-- - Validity and correctness lemmas for ⟦_⟧Δ
-- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments
-- - `corollary-closed`: binding-time-shifted main theorem
open import Popl14.Syntax.Term
open import Popl14.Denotation.Value
open import Popl14.Change.Validity
open import Popl14.Denotation.Evaluation public
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
open import Theorem.Groups-Popl14
open import Theorem.CongApp
open import Postulate.Extensionality
⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (dρ : ΔEnv Γ) → Change τ
-- better name is: ⟦_⟧Δ reacts to future arguments
validity : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) →
valid {τ} (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ)
-- better name is: ⟦_⟧Δ reacts to free variables
correctness : ∀ {τ Γ} {t : Term Γ τ} {dρ : ΔEnv Γ}
→ ⟦ t ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ ≡ ⟦ t ⟧ (update dρ)
⟦_⟧ΔVar : ∀ {τ Γ} → Var Γ τ → ΔEnv Γ → Change τ
⟦ this ⟧ΔVar (cons v dv R[v,dv] • dρ) = dv
⟦ that x ⟧ΔVar (cons v dv R[v,dv] • dρ) = ⟦ x ⟧ΔVar dρ
⟦_⟧Δ (intlit n) dρ = + 0
⟦_⟧Δ (add s t) dρ = ⟦ s ⟧Δ dρ + ⟦ t ⟧Δ dρ
⟦_⟧Δ (minus t) dρ = - ⟦ t ⟧Δ dρ
⟦_⟧Δ empty dρ = emptyBag
⟦_⟧Δ (insert s t) dρ =
let
n = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δn = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
(singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b)
⟦_⟧Δ (union s t) dρ = ⟦ s ⟧Δ dρ ++ ⟦ t ⟧Δ dρ
⟦_⟧Δ (negate t) dρ = negateBag (⟦ t ⟧Δ dρ)
⟦_⟧Δ (flatmap s t) dρ =
let
f = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δf = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b
⟦_⟧Δ (sum t) dρ = sumBag (⟦ t ⟧Δ dρ)
⟦_⟧Δ (var x) dρ = ⟦ x ⟧ΔVar dρ
⟦_⟧Δ (app {σ} {τ} s t) dρ =
(⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ)
(validity {σ} t dρ))
⟦_⟧Δ (abs t) dρ = λ v →
⟦ t ⟧Δ (v • dρ)
validVar : ∀ {τ Γ} (x : Var Γ τ) →
(dρ : ΔEnv Γ) → valid {τ} (⟦ x ⟧ (ignore dρ)) (⟦ x ⟧ΔVar dρ)
validVar this (cons v Δv R[v,Δv] • _) = R[v,Δv]
validVar {τ} (that x) (cons _ _ _ • dρ) = validVar {τ} x dρ
validity (intlit n) dρ = tt
validity (add s t) dρ = tt
validity (minus t) dρ = tt
validity (empty) dρ = tt
validity (insert s t) dρ = tt
validity (union s t) dρ = tt
validity (negate t) dρ = tt
validity (flatmap s t) dρ = tt
validity (sum t) dρ = tt
validity {τ} (var x) dρ = validVar {τ} x dρ
validity (app s t) dρ =
let
v = ⟦ t ⟧ (ignore dρ)
Δv = ⟦ t ⟧Δ dρ
in
proj₁ (validity s dρ (cons v Δv (validity t dρ)))
validity {σ ⇒ τ} (abs t) dρ = λ v →
let
v′ = after {σ} v
Δv′ = v′ ⊟₍ σ ₎ v′
dρ₁ = v • dρ
dρ₂ = nil-valid-change σ v′ • dρ
in
validity t dρ₁
,
(begin
⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂
≡⟨ correctness {t = t} {dρ₂} ⟩
⟦ t ⟧ (update dρ₂)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (update dρ₁)
≡⟨ sym (correctness {t = t} {dρ₁}) ⟩
⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁
∎) where open ≡-Reasoning
correctVar : ∀ {τ Γ} {x : Var Γ τ} {dρ : ΔEnv Γ} →
⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ)
correctVar {x = this } {cons v dv R[v,dv] • dρ} = refl
correctVar {x = that y} {cons v dv R[v,dv] • dρ} = correctVar {x = y} {dρ}
correctness {t = intlit n} = right-id-int n
correctness {t = add s t} {dρ} = trans
(mn·pq=mp·nq
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _+_ (correctness {t = s}) (correctness {t = t}))
correctness {t = minus t} {dρ} = trans
(-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong -_ (correctness {t = t}))
correctness {t = empty} = right-id-bag emptyBag
correctness {t = insert s t} {dρ} =
let
n = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
n′ = ⟦ s ⟧ (update dρ)
b′ = ⟦ t ⟧ (update dρ)
Δn = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
begin
(singletonBag n ++ b) ++
(singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b)
≡⟨ a++[b\\a]=b ⟩
singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)
≡⟨ cong₂ _++_
(cong singletonBag (correctness {t = s}))
(correctness {t = t}) ⟩
singletonBag n′ ++ b′
∎ where open ≡-Reasoning
correctness {t = union s t} {dρ} = trans
(ab·cd=ac·bd
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _++_ (correctness {t = s}) (correctness {t = t}))
correctness {t = negate t} {dρ} = trans
(-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong negateBag (correctness {t = t}))
correctness {t = flatmap s t} {dρ} =
let
f = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δf = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in trans
(a++[b\\a]=b {flatmapBag f b}
{flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf)
(b ⊞₍ base base-bag ₎ Δb)})
(cong₂ flatmapBag (correctness {t = s}) (correctness {t = t}))
correctness {t = sum t} {dρ} =
let
b = ⟦ t ⟧ (ignore dρ)
Δb = ⟦ t ⟧Δ dρ
in
trans (sym homo-sum) (cong sumBag (correctness {t = t}))
correctness {τ} {t = var x} = correctVar {τ} {x = x}
correctness {t = app {σ} {τ} s t} {dρ} =
let
f = ⟦ s ⟧ (ignore dρ)
g = ⟦ s ⟧ (update dρ)
u = ⟦ t ⟧ (ignore dρ)
v = ⟦ t ⟧ (update dρ)
Δf = ⟦ s ⟧Δ dρ
Δu = ⟦ t ⟧Δ dρ
in
begin
f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ))
≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu)
≡⟨ correctness {σ ⇒ τ} {t = s} ⟨$⟩ correctness {σ} {t = t} ⟩
g v
∎ where open ≡-Reasoning
correctness {σ ⇒ τ} {Γ} {abs t} {dρ} = ext (λ v →
let
dρ′ : ΔEnv (σ • Γ)
dρ′ = nil-valid-change σ v • dρ
in
begin
⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′
≡⟨ correctness {τ} {t = t} {dρ′} ⟩
⟦ t ⟧ (update dρ′)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (v • update dρ)
∎
) where open ≡-Reasoning
-- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv
corollary : ∀ {σ τ Γ}
(s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} →
(⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ)
(⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ)
≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ))
⊞₍ τ ₎
(⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))
corollary {σ} {τ} s t {dρ} = proj₂
(validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ))
(⟦ t ⟧Δ dρ) (validity {σ} t dρ)))
corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)}
(v : ValidChange σ)
→ ⟦ t ⟧ ∅ (after {σ} v)
≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v
corollary-closed {σ} {τ} {t = t} v =
let
f = ⟦ t ⟧ ∅
Δf = ⟦ t ⟧Δ ∅
in
begin
f (after {σ} v)
≡⟨ cong (λ hole → hole (after {σ} v))
(sym (correctness {σ ⇒ τ} {t = t})) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v)
≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩
f (before {σ} v) ⊞₍ τ ₎ Δf v
∎ where open ≡-Reasoning
|
Make some arguments of validVar explicit.
|
Make some arguments of validVar explicit.
Old-commit-hash: dc6d178e02b8bdd4f772dddbba1bf337f3b1d74c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
3ee4b4fed60cc477d5e3672148d7f6f90db3368f
|
arrow.agda
|
arrow.agda
|
open import Data.Bool
open import Data.List
open import Data.Nat
----------------------------------------
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
_∈_ : ℕ → List ℕ → Bool
x ∈ [] = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q ≡ s
(p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) ∧ (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ [] = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure [] found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) ∨ (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs [])
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ []))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) ∧ (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ [] ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) ∨ (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
proofs : List Arrow
proofs =
(3 ⇒ (⇒ 4)) ∷
-- (5 ⇒ (⇒ 4)) ∷
(6 ⇒ (⇒ 4)) ∷
(3 ⇒ (⇒ 3)) ∷
(3 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(5 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 7)) ∷
(6 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 7)) ∷
(5 ⇒ (⇒ 10)) ∷
(10 ⇒ (⇒ 4)) ∷
(5 ⇒ (⇒ 11)) ∷
(6 ⇒ (⇒ 11)) ∷
(11 ⇒ (⇒ 4)) ∷
(10 ⇒ (⇒ 7)) ∷
(8 ⇒ (⇒ 4)) ∷
(9 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 8)) ∷
(3 ⇒ (⇒ 8)) ∷
(5 ⇒ (3 ⇒ (⇒ 9))) ∷
(6 ⇒ (7 ⇒ (⇒ 10))) ∷
(6 ⇒ (3 ⇒ (⇒ 3))) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (8 ⇒ (⇒ 10))) ∷
(3 ⇒ (10 ⇒ (⇒ 9))) ∷
(5 ⇒ (⇒ 1)) ∷
(3 ⇒ (1 ⇒ (⇒ 9))) ∷
(1 ⇒ (⇒ 2)) ∷
(10 ⇒ (⇒ 2)) ∷ []
cms : List Separation
cms =
(model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ []) (5 ∷ 3 ∷ 7 ∷ 7 ∷ [])) ∷
(model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ []) (5 ∷ [])) ∷
(model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ []) (6 ∷ 3 ∷ [])) ∷
(model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ []) (6 ∷ [])) ∷
(model (12 ∷ 4 ∷ 11 ∷ []) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ [])) ∷
(model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ []) (3 ∷ [])) ∷
(model (12 ∷ 4 ∷ 11 ∷ 7 ∷ []) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ [])) ∷
(model (10 ∷ 9 ∷ []) (1 ∷ [])) ∷
(model (3 ∷ 4 ∷ 11 ∷ []) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ [])) ∷
(model (12 ∷ 7 ∷ 1 ∷ []) (4 ∷ 11 ∷ 8 ∷ [])) ∷
(model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ []) (11 ∷ [])) ∷
(model (12 ∷ 4 ∷ 10 ∷ 1 ∷ []) (11 ∷ 3 ∷ [])) ∷
(model (3 ∷ 6 ∷ 5 ∷ []) ([])) ∷
(model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ []) (12 ∷ [])) ∷ []
testp : Arrow
testp = (5 ⇒ (⇒ 10))
testd : Arrow
testd = (5 ⇒ (⇒ 4))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
main = (closure proofs (5 ∷ []))
|
open import Agda.Builtin.Bool
_or_ : Bool → Bool → Bool
true or _ = true
_ or true = true
false or false = false
_and_ : Bool → Bool → Bool
false and _ = false
_ and false = false
true and true = true
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
infixr 5 _∷_
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
{-# BUILTIN LIST List #-}
{-# BUILTIN NIL ∘ #-}
{-# BUILTIN CONS _∷_ #-}
any : {A : Set} → (A → Bool) → List A → Bool
any _ ∘ = false
any f (x ∷ xs) = (f x) or (any f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q ≡ s
(p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ ∘ = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs ∘)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ ∘ ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
proofs : List Arrow
proofs =
(3 ⇒ (⇒ 4)) ∷
-- (5 ⇒ (⇒ 4)) ∷
(6 ⇒ (⇒ 4)) ∷
(3 ⇒ (⇒ 3)) ∷
(3 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(5 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 7)) ∷
(6 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 7)) ∷
(5 ⇒ (⇒ 10)) ∷
(10 ⇒ (⇒ 4)) ∷
(5 ⇒ (⇒ 11)) ∷
(6 ⇒ (⇒ 11)) ∷
(11 ⇒ (⇒ 4)) ∷
(10 ⇒ (⇒ 7)) ∷
(8 ⇒ (⇒ 4)) ∷
(9 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 8)) ∷
(3 ⇒ (⇒ 8)) ∷
(5 ⇒ (3 ⇒ (⇒ 9))) ∷
(6 ⇒ (7 ⇒ (⇒ 10))) ∷
(6 ⇒ (3 ⇒ (⇒ 3))) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (8 ⇒ (⇒ 10))) ∷
(3 ⇒ (10 ⇒ (⇒ 9))) ∷
(5 ⇒ (⇒ 1)) ∷
(3 ⇒ (1 ⇒ (⇒ 9))) ∷
(1 ⇒ (⇒ 2)) ∷
(10 ⇒ (⇒ 2)) ∷ ∘
cms : List Separation
cms =
(model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷
(model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷
(model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷
(model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷
(model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷
(model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷
(model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷
(model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘
testp : Arrow
testp = (5 ⇒ (⇒ 10))
testd : Arrow
testd = (5 ⇒ (⇒ 4))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
main = (closure proofs (5 ∷ ∘))
|
Revert "Convert to stdlib"
|
Revert "Convert to stdlib"
This reverts commit 68af677d56d4f0a4c1aaa95134ae06f0813500c1.
|
Agda
|
mit
|
louisswarren/hieretikz
|
ca3585f55fde86ac7d664ac346618396c7308e11
|
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 : ValidChange ⊆ Change
change (cons _ dv _) = dv
diff-valid-change : ∀ τ → (u v : ⟦ τ ⟧) → ValidChange τ
diff-valid-change τ u v = cons v (u ⊟₍ τ ₎ v) (R[v,u-v] {τ} {u} {v})
nil-valid-change : ∀ τ → ⟦ τ ⟧ → ValidChange τ
nil-valid-change τ v = diff-valid-change τ 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
-- applicative structure
call-valid-change : ∀ σ τ →
ValidChange (σ ⇒ τ) →
ValidChange σ →
ValidChange τ
call-valid-change σ τ (cons f Δf R[f,Δf]) Δv =
cons (f (before {σ} Δv)) (Δf Δv) (proj₁ (R[f,Δf] Δ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
change : ValidChange ⊆ Change
change (cons _ dv _) = dv
apply-valid-change : ∀ τ → (v : ⟦ τ ⟧) (dv : ValidChange τ) → ⟦ τ ⟧
apply-valid-change τ v dv = apply-change τ v (change dv)
diff-valid-change : ∀ τ → (u v : ⟦ τ ⟧) → ValidChange τ
diff-valid-change τ u v = cons v (u ⊟₍ τ ₎ v) (R[v,u-v] {τ} {u} {v})
nil-valid-change : ∀ τ → ⟦ τ ⟧ → ValidChange τ
nil-valid-change τ v = diff-valid-change τ 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
-- applicative structure
call-valid-change : ∀ σ τ →
ValidChange (σ ⇒ τ) →
ValidChange σ →
ValidChange τ
call-valid-change σ τ (cons f Δf R[f,Δf]) Δv =
cons (f (before {σ} Δv)) (Δf Δv) (proj₁ (R[f,Δf] Δ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 {τ})
|
Implement apply-valid-change.
|
Implement apply-valid-change.
Old-commit-hash: 64cc4483b462c94a945aa312124cbdf090ffd619
|
Agda
|
mit
|
inc-lc/ilc-agda
|
fbf5d17dabb1058927efc56a1b0c092c4e68e355
|
incremental.agda
|
incremental.agda
|
module incremental where
open import Relation.Binary.PropositionalEquality
open import meaning
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
-- `Δ τ` is the type of changes to `τ`
Δ : (τ : Type) → Type
infixr 5 _⇒_
-- Denotational Semantics
⟦_⟧Type : Type -> Set
⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type
⟦ Δ τ ⟧Type = ⟦ τ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning Set ⟦_⟧Type
-- TYPING CONTEXTS, VARIABLES and WEAKENING
open import binding Type ⟦_⟧Type
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- `Δ x t` maps changes in `x` to changes in `t`
Δ : ∀ {Γ τ₁ τ₂} → (x : Var Γ τ₁) → (t : 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 ⟧ ρ
⟦ Δ x t ⟧Term ρ = λ Δx _ → ⟦ t ⟧Term (update x Δx ρ)
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm {Γ} {τ} = meaning (⟦ Γ ⟧ → ⟦ τ ⟧) ⟦_⟧Term
-- NATURAL SEMANTICS
-- (without support for Δ for now)
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv {Γ} = meaning ⟦ Γ ⟧ ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal {τ} = meaning ⟦ τ ⟧ ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = refl
↓-sound (app ↓₁ ↓₂ ↓′) = trans (cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂)) (↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
-- 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 {Γ₁} {Γ₂} (Δ x t) = Δ (lift {Γ₁} {Γ₂} x) (weaken {Γ₁} {Γ₂} t)
|
module incremental where
open import Relation.Binary.PropositionalEquality
open import meaning
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
-- `Δ τ` is the type of changes to `τ`
Δ : (τ : Type) → Type
infixr 5 _⇒_
-- Denotational Semantics
⟦_⟧Type : Type -> Set
⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type
⟦ Δ τ ⟧Type = ⟦ τ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning Set ⟦_⟧Type
-- TYPING CONTEXTS, VARIABLES and WEAKENING
open import binding Type ⟦_⟧Type
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- `Δ x dx t` maps changes `dx` in `x` to changes in `t`
Δ : ∀ {Γ τ₁ τ₂} → (x : Var Γ τ₁) (dx : Var Γ (Δ τ₁)) → (t : 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 ⟧ ρ
⟦ Δ x dx t ⟧Term ρ = λ _ → ⟦ t ⟧Term (update x (⟦ dx ⟧ ρ) ρ)
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm {Γ} {τ} = meaning (⟦ Γ ⟧ → ⟦ τ ⟧) ⟦_⟧Term
-- NATURAL SEMANTICS
-- (without support for Δ for now)
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv {Γ} = meaning ⟦ Γ ⟧ ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal {τ} = meaning ⟦ τ ⟧ ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = refl
↓-sound (app ↓₁ ↓₂ ↓′) = trans (cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂)) (↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
-- 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 {Γ₁} {Γ₂} (Δ x dx t) = Δ (lift {Γ₁} {Γ₂} x) (lift {Γ₁} {Γ₂} dx) (weaken {Γ₁} {Γ₂} t)
|
Change Δ to expect the change in a free variable.
|
Change Δ to expect the change in a free variable.
Proposed by Paolo in a whiteboard discussion today.
Old-commit-hash: bce0357c249c6ae66ad8fe002147ad07623d842f
|
Agda
|
mit
|
inc-lc/ilc-agda
|
1d32ad0cbc8582d30660167abe83ddda4a4cf6d1
|
lib/Relation/Nullary/NP.agda
|
lib/Relation/Nullary/NP.agda
|
module Relation.Nullary.NP where
open import Type
open import Data.Sum
open import Relation.Nullary public
module _ {a b} {A : ★_ a} {B : ★_ b} where
Dec-⊎ : Dec A → Dec B → Dec (A ⊎ B)
Dec-⊎ (yes p) _ = yes (inj₁ p)
Dec-⊎ (no ¬p) (yes p) = yes (inj₂ p)
Dec-⊎ (no ¬p) (no ¬q) = no [ ¬p , ¬q ]
module _ (to : A → B)(from : B → A) where
map-Dec : Dec A → Dec B
map-Dec (yes p) = yes (to p)
map-Dec (no ¬p) = no (λ z → ¬p (from z))
|
module Relation.Nullary.NP where
open import Type
open import Function
open import Data.Zero
open import Data.One
open import Data.Sum
open import Data.Product renaming (proj₁ to fst; proj₂ to snd)
open import Relation.Nullary public
Dec-𝟘 : Dec 𝟘
Dec-𝟘 = no id
Dec-𝟙 : Dec 𝟙
Dec-𝟙 = yes _
module _ {a b} {A : ★_ a} {B : ★_ b} where
Dec-⊎ : Dec A → Dec B → Dec (A ⊎ B)
Dec-⊎ (yes p) _ = yes (inj₁ p)
Dec-⊎ (no ¬p) (yes q) = yes (inj₂ q)
Dec-⊎ (no ¬p) (no ¬q) = no [ ¬p , ¬q ]
Dec-× : Dec A → Dec B → Dec (A × B)
Dec-× (no ¬p) _ = no (¬p ∘ fst)
Dec-× (yes _) (no ¬q) = no (¬q ∘ snd)
Dec-× (yes p) (yes q) = yes (p , q)
Dec-→ : Dec A → Dec B → Dec (A → B)
Dec-→ _ (yes q) = yes (λ _ → q)
Dec-→ (no ¬p) _ = yes (𝟘-elim ∘ ¬p)
Dec-→ (yes p) (no ¬q) = no (λ f → ¬q (f p))
module _ (to : A → B)(from : B → A) where
map-Dec : Dec A → Dec B
map-Dec (yes p) = yes (to p)
map-Dec (no ¬p) = no (¬p ∘ from)
|
add Dec-{𝟘,𝟙,×,→}
|
Dec: add Dec-{𝟘,𝟙,×,→}
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
0ea795d1835bb4e398a04244310fe6e1eed4388f
|
one-time-semantic-security.agda
|
one-time-semantic-security.agda
|
module one-time-semantic-security where
open import Function
open import Data.Nat
open import Data.Product
open import Data.Vec using (splitAt; take; drop)
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≗_)
open import Data.Bits
open import flat-funs
open import program-distance
open import flipbased-implem
-- Capture the various size parameters required
-- for a cipher.
--
-- M is the message space (|M| is the size of messages)
-- C is the cipher text space
-- Rᵉ is the randomness used by the cipher.
record EncParams : Set where
constructor mk
field
|M| |C| |R|ᵉ : ℕ
M = Bits |M|
C = Bits |C|
Rᵉ = Bits |R|ᵉ
-- The type of the encryption function
Enc : Set
Enc = M → ↺ |R|ᵉ C
module EncParams² (ep₀ ep₁ : EncParams) where
open EncParams ep₀ public using ()
renaming (|M| to |M|₀; |C| to |C|₀; |R|ᵉ to |R|ᵉ₀; M to M₀;
C to C₀; Rᵉ to Rᵉ₀; Enc to Enc₀)
open EncParams ep₁ public using ()
renaming (|M| to |M|₁; |C| to |C|₁; |R|ᵉ to |R|ᵉ₁; M to M₁;
C to C₁; Rᵉ to Rᵉ₁; Enc to Enc₁)
Tr = Enc₀ → Enc₁
Tr⁻¹ = Enc₁ → Enc₀
module EncParams²-same-|R|ᵉ (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where
ep₁ = EncParams.mk |M|₁ |C|₁ (EncParams.|R|ᵉ ep₀)
open EncParams² ep₀ ep₁ public
module ♭EncParams {t} {T : Set t}
(♭Funs : FlatFuns T)
(ep : EncParams) where
open FlatFuns ♭Funs
open EncParams ep public
`M = `Bits |M|
`C = `Bits |C|
`Rᵉ = `Bits |R|ᵉ
module AbsSemSec {t} {T : Set t}
(♭Funs : FlatFuns T) where
open FlatFuns ♭Funs
record SemSecAdv (ep : EncParams) |R|ᵃ : Set where
constructor mk
open ♭EncParams ♭Funs ep
field
{|S|} : ℕ
`S = `Bits |S|
S = Bits |S|
-- Randomness adversary
`Rᵃ = `Bits |R|ᵃ
Rᵃ = Bits |R|ᵃ
field
step₀ : `Rᵃ `→ (`M `× `M) `× `S
step₁ : `C `× `S `→ `Bit
M² = Bit → M
`M² = `Bit `→ `M
module Ops (♭ops : FlatFunsOps ♭Funs) where
open FlatFunsOps ♭ops
observe : `C `× `Rᵃ `→ (`M `× `M) `× `Bit
observe = idO *** step₀ >>> ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ >>> idO *** step₁
where ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ = < snd >>> fst , < fst , snd >>> snd > >
open ♭EncParams ♭Funs ep public
SemSecReduction : ∀ ep₀ ep₁ (f : Coins → Coins) → Set
SemSecReduction ep₀ ep₁ f = ∀ {c} → SemSecAdv ep₀ c → SemSecAdv ep₁ (f c)
-- Here we use Agda functions for FlatFuns.
module FunSemSec (prgDist : PrgDist) where
open PrgDist prgDist
open AbsSemSec fun♭Funs
open FlatFunsOps fun♭Ops
module FunSemSecAdv {ep : EncParams} {|R|ᵃ} (A : SemSecAdv ep |R|ᵃ) where
open SemSecAdv A public
open Ops fun♭Ops public
step₀F : Rᵃ → (M² × S)
step₀F = step₀ >>> proj *** idO
step₀↺ : ↺ |R|ᵃ (M² × S)
step₀↺ = mk step₀F
step₁F : S → C → Bit
step₁F s c = step₁ (c , s)
module RunSemSec (ep : EncParams) where
open EncParams ep using (M; C; |R|ᵉ; Enc)
-- Returing 0 means Chal wins, Adv looses
-- 1 means Adv wins, Chal looses
runSemSec : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
runSemSec E A b
= A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) }
where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁)
_⇄_ : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
_⇄_ = runSemSec
_≗A_ : ∀ {p} (A₁ A₂ : SemSecAdv ep p) → Set
A₀ ≗A A₁ = observe A₀ ≗ observe A₁
where open FunSemSecAdv
change-adv : ∀ {ca E} {A₁ A₂ : SemSecAdv ep ca} → A₁ ≗A A₂ → (E ⇄ A₁) ≗⅁ (E ⇄ A₂)
change-adv {ca = ca} {A₁ = _} {_} pf b R with splitAt ca R
change-adv {E = E} {A₁} {A₂} pf b ._ | pre Σ., post , ≡.refl = ≡.trans (≡.cong proj₂ (helper₀ A₁)) helper₂
where open FunSemSecAdv
helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post , pre)
helper₂ = ≡.cong (λ m → step₁ A₂ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₂ pre)))
(helper₀ A₂)
_≗E_ : ∀ (E₀ E₁ : Enc) → Set
E₀ ≗E E₁ = ∀ m → E₀ m ≗↺ E₁ m
≗E→≗⅁ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c)
→ (E₀ ⇄ A) ≗⅁ (E₁ ⇄ A)
≗E→≗⅁ E₀≗E₁ {c} A b R
rewrite E₀≗E₁ (proj (proj₁ (SemSecAdv.step₀ A (take c R))) b) (drop c R) = ≡.refl
≗E→⇓ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c) → (E₀ ⇄ A) ⇓ (E₁ ⇄ A)
≗E→⇓ E₀≗E₁ A = extensional-reduction (≗E→≗⅁ E₀≗E₁ A)
module SemSecReductions (ep₀ ep₁ : EncParams) (f : Coins → Coins) where
open EncParams² ep₀ ep₁
open RunSemSec ep₀ public using () renaming (_⇄_ to _⇄₀_; _≗E_ to _≗E₀_; ≗E→⇓ to ≗E→⇓₀)
open RunSemSec ep₁ public using () renaming (_⇄_ to _⇄₁_; _≗E_ to _≗E₁_; ≗E→⇓ to ≗E→⇓₁)
Reduction : Set
Reduction = SemSecReduction ep₁ ep₀ f
SemSecTr : Tr → Set
SemSecTr tr =
∃ λ (red : Reduction) →
∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
SemSecTr⁻¹ : Tr⁻¹ → Set
SemSecTr⁻¹ tr⁻¹ =
∃ λ (red : Reduction) →
∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
SemSecTr→SemSecTr⁻¹ : ∀ tr tr⁻¹ (tr-right-inv : ∀ E → tr (tr⁻¹ E) ≗E₁ E)
→ SemSecTr tr → SemSecTr⁻¹ tr⁻¹
SemSecTr→SemSecTr⁻¹ _ tr⁻¹ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
helper E A A-breaks-E = pf (tr⁻¹ E) A A-breaks-tr-tr⁻¹-E
where A-breaks-tr-tr⁻¹-E = ≗E→⇓₁ (λ m R → ≡.sym (tr-inv E m R)) A A-breaks-E
SemSecTr⁻¹→SemSecTr : ∀ tr tr⁻¹ (tr-left-inv : ∀ E → tr⁻¹ (tr E) ≗E₀ E)
→ SemSecTr⁻¹ tr⁻¹ → SemSecTr tr
SemSecTr⁻¹→SemSecTr tr _ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
helper E {c} A A-breaks-E = ≗E→⇓₀ (tr-inv E) (red A) (pf (tr E) A A-breaks-E)
|
module one-time-semantic-security where
open import Function
open import Data.Nat
open import Data.Product using (∃; module Σ; _×_; _,_; proj₁; proj₂)
open import Data.Vec using (splitAt; take; drop)
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≗_)
open import Data.Bits
open import flat-funs
open import program-distance
open import flipbased-implem
-- Capture the various size parameters required
-- for a cipher.
--
-- M is the message space (|M| is the size of messages)
-- C is the cipher text space
-- Rᵉ is the randomness used by the cipher.
record EncParams : Set where
constructor mk
field
|M| |C| |R|ᵉ : ℕ
M = Bits |M|
C = Bits |C|
Rᵉ = Bits |R|ᵉ
-- The type of the encryption function
Enc : Set
Enc = M → ↺ |R|ᵉ C
module EncParams² (ep₀ ep₁ : EncParams) where
open EncParams ep₀ public using ()
renaming (|M| to |M|₀; |C| to |C|₀; |R|ᵉ to |R|ᵉ₀; M to M₀;
C to C₀; Rᵉ to Rᵉ₀; Enc to Enc₀)
open EncParams ep₁ public using ()
renaming (|M| to |M|₁; |C| to |C|₁; |R|ᵉ to |R|ᵉ₁; M to M₁;
C to C₁; Rᵉ to Rᵉ₁; Enc to Enc₁)
Tr = Enc₀ → Enc₁
Tr⁻¹ = Enc₁ → Enc₀
module EncParams²-same-|R|ᵉ (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where
ep₁ = EncParams.mk |M|₁ |C|₁ (EncParams.|R|ᵉ ep₀)
open EncParams² ep₀ ep₁ public
module ♭EncParams {t} {T : Set t}
(♭Funs : FlatFuns T)
(ep : EncParams) where
open FlatFuns ♭Funs
open EncParams ep public
`M = `Bits |M|
`C = `Bits |C|
`Rᵉ = `Bits |R|ᵉ
module ♭EncParams² {t} {T : Set t}
(♭Funs : FlatFuns T)
(ep₀ ep₁ : EncParams) where
open EncParams² ep₀ ep₁ public
open ♭EncParams ♭Funs ep₀ public using () renaming (`M to `M₀; `C to `C₀; `Rᵉ to `Rᵉ₀)
open ♭EncParams ♭Funs ep₁ public using () renaming (`M to `M₁; `C to `C₁; `Rᵉ to `Rᵉ₁)
module AbsSemSec {t} {T : Set t}
(♭Funs : FlatFuns T) where
open FlatFuns ♭Funs
record SemSecAdv (ep : EncParams) |R|ᵃ : Set where
constructor mk
open ♭EncParams ♭Funs ep
field
{|S|} : ℕ
`S = `Bits |S|
S = Bits |S|
-- Randomness adversary
`Rᵃ = `Bits |R|ᵃ
Rᵃ = Bits |R|ᵃ
field
step₀ : `Rᵃ `→ (`M `× `M) `× `S
step₁ : `C `× `S `→ `Bit
M² = Bit → M
`M² = `Bit `→ `M
module Ops (♭ops : FlatFunsOps ♭Funs) where
open FlatFunsOps ♭ops
observe : `C `× `Rᵃ `→ (`M `× `M) `× `Bit
observe = idO *** step₀ >>> ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ >>> idO *** step₁
where ⟨C,⟨M,S⟩⟩→⟨M¸⟨C,S⟩⟩ = < snd >>> fst , < fst , snd >>> snd > >
open ♭EncParams ♭Funs ep public
SemSecReduction : ∀ ep₀ ep₁ (f : Coins → Coins) → Set
SemSecReduction ep₀ ep₁ f = ∀ {c} → SemSecAdv ep₀ c → SemSecAdv ep₁ (f c)
-- Here we use Agda functions for FlatFuns.
module FunSemSec (prgDist : PrgDist) where
open PrgDist prgDist
open AbsSemSec fun♭Funs
open FlatFunsOps fun♭Ops
module FunSemSecAdv {ep : EncParams} {|R|ᵃ} (A : SemSecAdv ep |R|ᵃ) where
open SemSecAdv A public
open Ops fun♭Ops public
step₀F : Rᵃ → (M² × S)
step₀F = step₀ >>> proj *** idO
step₀↺ : ↺ |R|ᵃ (M² × S)
step₀↺ = mk step₀F
step₁F : S → C → Bit
step₁F s c = step₁ (c , s)
module RunSemSec (ep : EncParams) where
open EncParams ep using (M; C; |R|ᵉ; Enc)
-- Returing 0 means Chal wins, Adv looses
-- 1 means Adv wins, Chal looses
runSemSec : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
runSemSec E A b
= A-step₀ >>= λ { (m , s) → map↺ (A-step₁ s) (E (m b)) }
where open FunSemSecAdv A renaming (step₀↺ to A-step₀; step₁F to A-step₁)
_⇄_ : ∀ {|R|ᵃ} (E : Enc) (A : SemSecAdv ep |R|ᵃ) b → ↺ (|R|ᵃ + |R|ᵉ) Bit
_⇄_ = runSemSec
_≗A_ : ∀ {p} (A₁ A₂ : SemSecAdv ep p) → Set
A₀ ≗A A₁ = observe A₀ ≗ observe A₁
where open FunSemSecAdv
change-adv : ∀ {ca E} {A₁ A₂ : SemSecAdv ep ca} → A₁ ≗A A₂ → (E ⇄ A₁) ≗⅁ (E ⇄ A₂)
change-adv {ca = ca} {A₁ = _} {_} pf b R with splitAt ca R
change-adv {E = E} {A₁} {A₂} pf b ._ | pre Σ., post , ≡.refl = ≡.trans (≡.cong proj₂ (helper₀ A₁)) helper₂
where open FunSemSecAdv
helper₀ = λ A → pf (run↺ (E (proj (proj₁ (step₀ A pre)) b)) post , pre)
helper₂ = ≡.cong (λ m → step₁ A₂ (run↺ (E (proj (proj₁ m) b)) post , proj₂ (step₀ A₂ pre)))
(helper₀ A₂)
_≗E_ : ∀ (E₀ E₁ : Enc) → Set
E₀ ≗E E₁ = ∀ m → E₀ m ≗↺ E₁ m
≗E→≗⅁ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c)
→ (E₀ ⇄ A) ≗⅁ (E₁ ⇄ A)
≗E→≗⅁ E₀≗E₁ {c} A b R
rewrite E₀≗E₁ (proj (proj₁ (SemSecAdv.step₀ A (take c R))) b) (drop c R) = ≡.refl
≗E→⇓ : ∀ {E₀ E₁} → E₀ ≗E E₁ → ∀ {c} (A : SemSecAdv ep c) → (E₀ ⇄ A) ⇓ (E₁ ⇄ A)
≗E→⇓ E₀≗E₁ A = extensional-reduction (≗E→≗⅁ E₀≗E₁ A)
module SemSecReductions (ep₀ ep₁ : EncParams) (f : Coins → Coins) where
open EncParams² ep₀ ep₁
open RunSemSec ep₀ public using () renaming (_⇄_ to _⇄₀_; _≗E_ to _≗E₀_; ≗E→⇓ to ≗E→⇓₀)
open RunSemSec ep₁ public using () renaming (_⇄_ to _⇄₁_; _≗E_ to _≗E₁_; ≗E→⇓ to ≗E→⇓₁)
Reduction : Set
Reduction = SemSecReduction ep₁ ep₀ f
SemSecTr : Tr → Set
SemSecTr tr =
∃ λ (red : Reduction) →
∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
SemSecTr⁻¹ : Tr⁻¹ → Set
SemSecTr⁻¹ tr⁻¹ =
∃ λ (red : Reduction) →
∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
SemSecTr→SemSecTr⁻¹ : ∀ tr tr⁻¹ (tr-right-inv : ∀ E → tr (tr⁻¹ E) ≗E₁ E)
→ SemSecTr tr → SemSecTr⁻¹ tr⁻¹
SemSecTr→SemSecTr⁻¹ _ tr⁻¹ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (E ⇄₁ A) ⇓ (tr⁻¹ E ⇄₀ red {c} A)
helper E A A-breaks-E = pf (tr⁻¹ E) A A-breaks-tr-tr⁻¹-E
where A-breaks-tr-tr⁻¹-E = ≗E→⇓₁ (λ m R → ≡.sym (tr-inv E m R)) A A-breaks-E
SemSecTr⁻¹→SemSecTr : ∀ tr tr⁻¹ (tr-left-inv : ∀ E → tr⁻¹ (tr E) ≗E₀ E)
→ SemSecTr⁻¹ tr⁻¹ → SemSecTr tr
SemSecTr⁻¹→SemSecTr tr _ tr-inv (red , pf) = red , helper
where helper : ∀ E {c} A → (tr E ⇄₁ A) ⇓ (E ⇄₀ red {c} A)
helper E {c} A A-breaks-E = ≗E→⇓₀ (tr-inv E) (red A) (pf (tr E) A A-breaks-E)
|
add ♭EncParams²
|
one-time-semantic-security: add ♭EncParams²
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
00a7767b9e6a38ec98ff6336651129e2786a6a98
|
lib/Data/Fin/Logical.agda
|
lib/Data/Fin/Logical.agda
|
-- NOTE with-K
module Data.Fin.Logical where
open import Data.Nat.Param.Binary
open import Data.Fin.Param.Binary
open import Function
open import Data.Fin
open import Relation.Binary
open import Relation.Binary.Logical
private
module ⟦ℕ⟧s = Setoid ⟦ℕ⟧-setoid
{-
data ⟦Fin⟧ : (⟦ℕ⟧ ⟦→⟧ ⟦★₀⟧) Fin Fin where
⟦zero⟧ : ∀ {n₁ n₂} {nᵣ : ⟦ℕ⟧ n₁ n₂} → ⟦Fin⟧ (suc nᵣ) zero zero
⟦suc⟧ : ∀ {n₁ n₂} {nᵣ : ⟦ℕ⟧ n₁ n₂} {x₁ x₂} (xᵣ : ⟦Fin⟧ nᵣ x₁ x₂) → ⟦Fin⟧ (suc nᵣ) (suc x₁) (suc x₂)
-}
private
refl : ∀ {n} → Reflexive (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
refl {x = zero} = ⟦zero⟧
refl {x = suc _} = ⟦suc⟧ refl
sym : ∀ {n} → Symmetric (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
sym ⟦zero⟧ = ⟦zero⟧
sym (⟦suc⟧ xᵣ) = ⟦suc⟧ (sym xᵣ)
trans : ∀ {n} → Transitive (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
trans ⟦zero⟧ xᵣ = xᵣ
trans (⟦suc⟧ xᵣ) (⟦suc⟧ yᵣ) = ⟦suc⟧ (trans xᵣ yᵣ)
subst : ∀ {ℓ n} → Substitutive (⟦Fin⟧ {n} ⟦ℕ⟧s.refl) ℓ
subst _ ⟦zero⟧ = id
subst P (⟦suc⟧ xᵣ) = subst (P ∘ suc) xᵣ
isEquivalence : ∀ {n} → IsEquivalence (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
isEquivalence = record { refl = refl; sym = sym; trans = trans }
|
{-# OPTIONS --with-K #-}
module Data.Fin.Logical where
open import Data.Nat.Logical
open import Data.Nat.Param.Binary
-- open import Data.Fin.Param.Binary
open import Function
open import Data.Fin
open import Relation.Binary
open import Relation.Binary.Logical
private
module ⟦ℕ⟧s = Setoid ⟦ℕ⟧-setoid
data ⟦Fin⟧ : (⟦ℕ⟧ ⟦→⟧ ⟦★₀⟧) Fin Fin where
⟦zero⟧ : ∀ {n₁ n₂} {nᵣ : ⟦ℕ⟧ n₁ n₂} → ⟦Fin⟧ (⟦suc⟧ nᵣ) zero zero
⟦suc⟧ : ∀ {n₁ n₂} {nᵣ : ⟦ℕ⟧ n₁ n₂} {x₁ x₂} (xᵣ : ⟦Fin⟧ nᵣ x₁ x₂) → ⟦Fin⟧ (⟦suc⟧ nᵣ) (suc x₁) (suc x₂)
private
refl : ∀ {n} → Reflexive (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
refl {x = zero} = ⟦zero⟧
refl {x = suc _} = ⟦suc⟧ refl
sym : ∀ {n} → Symmetric (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
sym ⟦zero⟧ = ⟦zero⟧
sym (⟦suc⟧ xᵣ) = ⟦suc⟧ (sym xᵣ)
trans : ∀ {n} → Transitive (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
trans ⟦zero⟧ xᵣ = xᵣ
trans (⟦suc⟧ xᵣ) (⟦suc⟧ yᵣ) = ⟦suc⟧ (trans xᵣ yᵣ)
subst : ∀ {ℓ n} → Substitutive (⟦Fin⟧ {n} ⟦ℕ⟧s.refl) ℓ
subst _ ⟦zero⟧ = id
subst P (⟦suc⟧ xᵣ) = subst (P ∘ suc) xᵣ
isEquivalence : ∀ {n} → IsEquivalence (⟦Fin⟧ {n} ⟦ℕ⟧s.refl)
isEquivalence = record { refl = refl; sym = sym; trans = trans }
|
Fix Data.Fin.Logical
|
Fix Data.Fin.Logical
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
8403c80b80206ebcbe4f1d3acfac2cfce30552ca
|
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 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
-- 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
--open import Base.Data.DependentList public -- No
--
-- Workaround https://github.com/agda/agda/issues/1985 by making this import
-- private; many clients will need to start importing this now.
open import Base.Data.DependentList
-- (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; [_]; reverse)
open import Data.Nat
module AbsNHelpers where
open import Function
hoasArgType : ∀ {n} → Context → Type → Vec Type n → Set
hoasArgType Γ τres = foldr _ (λ a b → a → b) (Term Γ τres) ∘ Data.Vec.map (Term Γ)
-- That is,
--hoasArgType Γ τres [] = Term Γ τres
--hoasArgType Γ τres (τ₀ ∷ τs) = Term Γ τ₀ → hoasArgType Γ τres τs
hoasResType : ∀ {n} → Type → Vec Type n → Type
hoasResType τres = foldr _ _⇒_ τres
absNType : {n : ℕ} → Vec _ n → Set
absNType τs = ∀ {Γ τres} →
(f : ∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → hoasArgType Γ′ τres τs) →
Term Γ (hoasResType τres τs)
drop-≼-l : ∀ {Γ Γ′ τ} → (τ • Γ ≼ Γ′) → Γ ≼ Γ′
drop-≼-l Γ′≼Γ′₁ = ≼-trans (drop _ • ≼-refl) Γ′≼Γ′₁
absN : {n : ℕ} → (τs : Vec _ n) → absNType τs
absN [] f = f {Γ≼Γ′ = ≼-refl}
absN (τ₁ ∷ τs) f =
abs (absN τs
(λ {_} {Γ′≼Γ′₁} →
f
{Γ≼Γ′ = drop-≼-l Γ′≼Γ′₁}
(weaken Γ′≼Γ′₁ (var this))))
-- Using a similar trick, we can declare absV (where V stands for variadic),
-- which takes the N implicit type arguments individually, collects them and
-- passes them on to absN. This is inspired by the example in the Agda 2.4.0
-- release notes about support for varying arity. To collect them, we need
-- to use an accumulator argument.
absVType : ∀ n {m} (τs : Vec Type m) → Set
absVType zero τs = absNType (reverse τs)
absVType (suc n) τs = {τ₁ : Type} → absVType n (τ₁ ∷ τs)
absVAux : ∀ {m} → (τs : Vec Type m) → ∀ n → absVType n τs
absVAux τs zero = absN (reverse τs)
-- (Support for varying arity does not work here, so {τ₁} must be bound in
-- the right-hand side).
absVAux τs (suc n) {τ₁} = absVAux (τ₁ ∷ τs) n --λ {τ₁} → absVAux (τ₁ ∷ τs) n
-- "Initialize" accumulator to the empty list.
absV = absVAux []
-- We could maybe define absV directly, without going through absN, by
-- making hoasArgType and hoasResType also variadic, but I don't see a clear
-- advantage.
open AbsNHelpers using (absV) public
{-
Example types:
absV 1:
{τ₁ : Type} {Γ : Context} {τres : Type} →
({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τres) →
Term Γ (τ₁ ⇒ τres)
absV 2:
{τ₁ : Type} {τ₁ = τ₂ : Type} {Γ : Context} {τres : Type} →
({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} →
Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τres) →
Term Γ (τ₁ ⇒ τ₂ ⇒ τres)
-}
|
------------------------------------------------------------------------
-- 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
-- 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
--open import Base.Data.DependentList public -- No
--
-- Workaround https://github.com/agda/agda/issues/1985 by making this import
-- private; many clients will need to start importing this now.
open import Base.Data.DependentList
-- (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)
-- 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; [_]; reverse)
open import Data.Nat
module AbsNHelpers where
open import Function
hoasArgType : ∀ {n} → Context → Type → Vec Type n → Set
hoasArgType Γ τres = foldr _ (λ a b → a → b) (Term Γ τres) ∘ Data.Vec.map (Term Γ)
-- That is,
--hoasArgType Γ τres [] = Term Γ τres
--hoasArgType Γ τres (τ₀ ∷ τs) = Term Γ τ₀ → hoasArgType Γ τres τs
hoasResType : ∀ {n} → Type → Vec Type n → Type
hoasResType τres = foldr _ _⇒_ τres
absNType : {n : ℕ} → Vec _ n → Set
absNType τs = ∀ {Γ τres} →
(f : ∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → hoasArgType Γ′ τres τs) →
Term Γ (hoasResType τres τs)
drop-≼-l : ∀ {Γ Γ′ τ} → (τ • Γ ≼ Γ′) → Γ ≼ Γ′
drop-≼-l Γ′≼Γ′₁ = ≼-trans (drop _ • ≼-refl) Γ′≼Γ′₁
absN : {n : ℕ} → (τs : Vec _ n) → absNType τs
absN [] f = f {Γ≼Γ′ = ≼-refl}
absN (τ₁ ∷ τs) f =
abs (absN τs
(λ {_} {Γ′≼Γ′₁} →
f
{Γ≼Γ′ = drop-≼-l Γ′≼Γ′₁}
(weaken Γ′≼Γ′₁ (var this))))
-- Using a similar trick, we can declare absV (where V stands for variadic),
-- which takes the N implicit type arguments individually, collects them and
-- passes them on to absN. This is inspired by the example in the Agda 2.4.0
-- release notes about support for varying arity. To collect them, we need
-- to use an accumulator argument.
absVType : ∀ n {m} (τs : Vec Type m) → Set
absVType zero τs = absNType (reverse τs)
absVType (suc n) τs = {τ₁ : Type} → absVType n (τ₁ ∷ τs)
absVAux : ∀ {m} → (τs : Vec Type m) → ∀ n → absVType n τs
absVAux τs zero = absN (reverse τs)
-- (Support for varying arity does not work here, so {τ₁} must be bound in
-- the right-hand side).
absVAux τs (suc n) {τ₁} = absVAux (τ₁ ∷ τs) n --λ {τ₁} → absVAux (τ₁ ∷ τs) n
-- "Initialize" accumulator to the empty list.
absV = absVAux []
-- We could maybe define absV directly, without going through absN, by
-- making hoasArgType and hoasResType also variadic, but I don't see a clear
-- advantage.
open AbsNHelpers using (absV) public
{-
Example types:
absV 1:
{τ₁ : Type} {Γ : Context} {τres : Type} →
({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τres) →
Term Γ (τ₁ ⇒ τres)
absV 2:
{τ₁ : Type} {τ₁ = τ₂ : Type} {Γ : Context} {τres : Type} →
({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} →
Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τres) →
Term Γ (τ₁ ⇒ τ₂ ⇒ τres)
-}
|
Drop unused defs
|
Drop unused defs
|
Agda
|
mit
|
inc-lc/ilc-agda
|
c7ad461731109d522f831464e795886500cbf40d
|
README.agda
|
README.agda
|
module README where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * multiple calculi
import Popl14.Syntax.Term
import Base.Syntax.Context
import Base.Change.Context
import Popl14.Change.Derive
{- Language definition of Calc. Atlas -}
import Atlas.Syntax.Term
{- Terms of a calculus described in Plotkin style
- types are parametric in base types
- terms are parametric in constants
This style of language description is employed in:
G. D. Plotkin. "LCF considered as a programming language."
Theoretical Computer Science 5(3) pp. 223--255, 1997.
http://dx.doi.org/10.1016/0304-3975(77)90044-5 -}
import Parametric.Syntax.Term
import Popl14.Change.Term
import Atlas.Syntax.Type
import Parametric.Syntax.Type
import Popl14.Syntax.Type
import Base.Syntax.Vars
import Popl14.Change.Validity
{- Correctness theorem for canonical derivation of Calc. Popl14 -}
import Popl14.Change.Correctness
import Theorem.Irrelevance-Popl14
import Base.Denotation.Environment
import Popl14.Denotation.Evaluation
{- The idea of implementing a denotational specification for Calc. Popl14 -}
import Popl14.Change.Implementation
import Base.Denotation.Notation
{- Denotation-as-specification for canonical derivation of Calc. Popl14 -}
import Popl14.Change.Specification
import Popl14.Denotation.Value
import experimental.DecidableEq
import experimental.FoldableBag
import experimental.FoldableBagParametric
import experimental.NormalizationByEvaluation
import experimental.OrdBag
import experimental.Sorting
import Postulate.Bag-Popl14
import Postulate.Extensionality
import Property.Uniqueness
import Structure.Bag.Popl14
import Structure.Tuples
import Theorem.CongApp
import Theorem.EqualityUnique
import Theorem.Groups-Popl14
import Theorem.IrrelevanceUnique-Popl14
import Theorem.ProductUnique
import UNDEFINED
import Everything
|
module README where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * multiple calculi
--
-- To typecheck this formalization, you need to install the appropriate version
-- of Agda, the Agda standard library (version 0.7), generate Everything.agda
-- with the attached Haskell helper, and finally run Agda on this file.
--
-- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh
-- script and following instructions given on output will help with the
-- "generate Everything.agda" part.
--
-- We use Agda HEAD from September 2013; Agda 2.3.2.1 might happen to work, but
-- has some bugs with serialization of code using some recent syntactic sugar
-- which we use (https://code.google.com/p/agda/issues/detail?id=756), so it
-- might work or not. When it does not, removing Agda caches (.agdai files)
-- appears to often help.
import Popl14.Syntax.Term
import Base.Syntax.Context
import Base.Change.Context
import Popl14.Change.Derive
{- Language definition of Calc. Atlas -}
import Atlas.Syntax.Term
{- Terms of a calculus described in Plotkin style
- types are parametric in base types
- terms are parametric in constants
This style of language description is employed in:
G. D. Plotkin. "LCF considered as a programming language."
Theoretical Computer Science 5(3) pp. 223--255, 1997.
http://dx.doi.org/10.1016/0304-3975(77)90044-5 -}
import Parametric.Syntax.Term
import Popl14.Change.Term
import Atlas.Syntax.Type
import Parametric.Syntax.Type
import Popl14.Syntax.Type
import Base.Syntax.Vars
import Popl14.Change.Validity
{- Correctness theorem for canonical derivation of Calc. Popl14 -}
import Popl14.Change.Correctness
import Theorem.Irrelevance-Popl14
import Base.Denotation.Environment
import Popl14.Denotation.Evaluation
{- The idea of implementing a denotational specification for Calc. Popl14 -}
import Popl14.Change.Implementation
import Base.Denotation.Notation
{- Denotation-as-specification for canonical derivation of Calc. Popl14 -}
import Popl14.Change.Specification
import Popl14.Denotation.Value
import experimental.DecidableEq
import experimental.FoldableBag
import experimental.FoldableBagParametric
import experimental.NormalizationByEvaluation
import experimental.OrdBag
import experimental.Sorting
import Postulate.Bag-Popl14
import Postulate.Extensionality
import Property.Uniqueness
import Structure.Bag.Popl14
import Structure.Tuples
import Theorem.CongApp
import Theorem.EqualityUnique
import Theorem.Groups-Popl14
import Theorem.IrrelevanceUnique-Popl14
import Theorem.ProductUnique
import UNDEFINED
import Everything
|
Add instructions for users to README.agda
|
Add instructions for users to README.agda
Old-commit-hash: c0018596eda8908801ba4aa7eeec5aa0ef9452b5
|
Agda
|
mit
|
inc-lc/ilc-agda
|
97ac1ebb0cbaecc1af38b1d9916fb946e06656a7
|
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 (derive-correct-closed)
-- Theorem 3.15 (Correctness of differentiation)
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)
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)
|
remove redundant comment
|
remove redundant comment
Old-commit-hash: 72138e09941c4514d0acef9dc39a91cd83e8077c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
ceb7acd1c67116365b51503065e55f947cda63a5
|
total.agda
|
total.agda
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import meaning
open import IlcModel
open import Changes
open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
-- LIFTING terms into Δ-Contexts
lift-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) τ
lift-var this = that this
lift-var (that x) = that (that (lift-var x))
lift-var′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Var Γ τ → Var (Δ-Context′ Γ Γ′) τ
lift-var′ ∅ x = lift-var x
lift-var′ (τ • Γ′) this = this
lift-var′ (τ • Γ′) (that x) = that (lift-var′ Γ′ x)
lift-term′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Term Γ τ → Term (Δ-Context′ Γ Γ′) τ
lift-term′ Γ′ (abs t) = abs (lift-term′ (_ • Γ′) t)
lift-term′ Γ′ (app t₁ t₂) = app (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂)
lift-term′ Γ′ (var x) = var (lift-var′ Γ′ x)
lift-term′ Γ′ true = true
lift-term′ Γ′ false = false
lift-term′ Γ′ (if t₁ t₂ t₃) = if (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂) (lift-term′ Γ′ t₃)
lift-term′ {.(Δ-Context Γ)} {.(Δ-Type τ)} Γ′ (Δ {Γ} {τ} t) = weakenMore Γ′ t
where
open import Relation.Binary.PropositionalEquality using (sym)
doWeakenMore : ∀ Γprefix Γrest {τ} →
Term (Γprefix ⋎ Γrest) (Δ-Type τ) →
Term (Γprefix ⋎ Δ-Context Γrest) (Δ-Type τ)
doWeakenMore Γprefix ∅ t₁ = t₁
doWeakenMore ∅ (τ₂ • Γrest) t₁ =
weakenOne ∅ (Δ-Type τ₂) (doWeakenMore (τ₂ • ∅) Γrest t₁)
doWeakenMore Γprefix (τ₂ • Γrest) {τ} t₁ =
weakenOne Γprefix (Δ-Type τ₂)
(substTerm (sym (move-prefix Γprefix τ₂ (Δ-Context Γrest)))
(doWeakenMore (Γprefix ⋎ (τ₂ • ∅)) Γrest
(substTerm (move-prefix Γprefix τ₂ Γrest) t₁)))
prefix : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
prefix Γ Γ′ = take (Δ-Context Γ) Γ′
rest : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
rest Γ Γ′ = drop (Δ-Context Γ) Γ′
weakenMore2 : ∀ Γ Γ′ {τ} →
Term Γ τ →
Term (prefix Γ Γ′ ⋎ Δ-Context (rest Γ Γ′)) (Δ-Type τ)
weakenMore2 Γ Γ′ t =
doWeakenMore (prefix Γ Γ′) (rest Γ Γ′) (
substTerm (sym (take-drop (Δ-Context Γ) Γ′)) (Δ t))
weakenMore : ∀ {Γ τ} Γ′ →
Term Γ τ → Term (Δ-Context′ (Δ-Context Γ) Γ′) (Δ-Type τ)
weakenMore {Γ} Γ′ t =
substTerm
(sym (take-⋎-Δ-Context-drop-Δ-Context′ (Δ-Context Γ) Γ′))
(weakenMore2 Γ Γ′ t)
lift-term′ {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) τ
lift-term = lift-term′ ∅
-- PROPERTIES of lift-term
lift-var-ignore : ∀ {Γ τ} (ρ : ⟦ Δ-Context Γ ⟧) (x : Var Γ τ) →
⟦ lift-var x ⟧ ρ ≡ ⟦ x ⟧ (ignore ρ)
lift-var-ignore (v • dv • ρ) this = ≡-refl
lift-var-ignore (v • dv • ρ) (that x) = lift-var-ignore ρ x
lift-var-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) (ρ : ⟦ Δ-Context′ Γ Γ′ ⟧) (x : Var Γ τ) →
⟦ lift-var′ Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (ignore′ Γ′ ρ)
lift-var-ignore′ ∅ ρ x = lift-var-ignore ρ x
lift-var-ignore′ (τ • Γ′) (v • ρ) this = ≡-refl
lift-var-ignore′ (τ • Γ′) (v • ρ) (that x) = lift-var-ignore′ Γ′ ρ x
lift-term-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) {ρ : ⟦ Δ-Context′ Γ Γ′ ⟧} (t : Term Γ τ) →
⟦ lift-term′ Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (ignore′ Γ′ ρ)
lift-term-ignore′ Γ′ (abs t) =
ext (λ v → lift-term-ignore′ (_ • Γ′) t)
lift-term-ignore′ Γ′ (app t₁ t₂) =
≡-app (lift-term-ignore′ Γ′ t₁) (lift-term-ignore′ Γ′ t₂)
lift-term-ignore′ Γ′ (var x) = lift-var-ignore′ Γ′ _ x
lift-term-ignore′ Γ′ true = ≡-refl
lift-term-ignore′ Γ′ false = ≡-refl
lift-term-ignore′ Γ′ {ρ} (if t₁ t₂ t₃)
with ⟦ lift-term′ Γ′ t₁ ⟧ ρ
| ⟦ t₁ ⟧ (ignore′ Γ′ ρ)
| lift-term-ignore′ Γ′ {ρ} t₁
... | true | true | bool = lift-term-ignore′ Γ′ t₂
... | false | false | bool = lift-term-ignore′ Γ′ t₃
lift-term-ignore′ Γ′ (Δ t) = {!!}
lift-term-ignore′ _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term-ignore : ∀ {Γ τ} {ρ : ⟦ Δ-Context Γ ⟧} (t : Term Γ τ) →
⟦ lift-term t ⟧ ρ ≡ ⟦ t ⟧ (ignore ρ)
lift-term-ignore = lift-term-ignore′ ∅
-- PROPERTIES of Δ
Δ-abs : ∀ {Γ τ₁ τ₂} (t : Term (τ₁ • Γ) τ₂) →
Δ (abs t) ≈ abs (abs (Δ t))
Δ-abs t = ext (λ ρ → ≡-refl)
Δ-app : ∀ {Γ τ₁ τ₂} (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) →
Δ (app t₁ t₂) ≈ app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
Δ-app t₁ t₂ = ≈-sym (ext (λ ρ →
begin
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ lift-term t₂ ⟧ ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term t₂ ⟧ ρ))
≡⟨ ≡-cong
(λ x →
diff
(⟦ t₁ ⟧ (update ρ)
(apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x))
(⟦ t₁ ⟧ (ignore ρ) x))
(lift-term-ignore {ρ = ρ} t₂) ⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ t₂ ⟧ (ignore ρ))))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨ ≡-cong
(λ x →
diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))))
(apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩
diff
(⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var this = this
derive-var (that x) = that (that (derive-var x))
diff-term : ∀ {Γ τ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term = {!!}
apply-term : ∀ {Γ τ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term = {!!}
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = {!!}
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = {!!}
derive-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) (Δ-Type τ)
derive-term (abs t) = abs (abs (derive-term t))
derive-term (app t₁ t₂) = app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
derive-term (var x) = var (derive-var x)
derive-term true = false
derive-term false = false
derive-term (if c t e) =
if ((derive-term c) and (lift-term c))
(diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))
(if ((derive-term c) and (lift-term (! c)))
(diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))
(if (lift-term c)
(derive-term t)
(derive-term e)))
derive-term (Δ t) = Δ (derive-term t)
derive-term (weakenOne Γ₁ τ₂ {Γ₃} t) =
substTerm (Δ-Context-⋎-expanded Γ₁ τ₂ Γ₃)
(weakenOne (Δ-Context Γ₁) (Δ-Type τ₂)
(weakenOne (Δ-Context Γ₁) τ₂
(substTerm (Δ-Context-⋎ Γ₁ Γ₃)
(derive-term t))))
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ τ} → (t : Term Γ τ) →
Δ t ≈ derive-term t
derive-term-correct {Γ} (abs t) =
begin
Δ (abs t)
≈⟨ Δ-abs t ⟩
abs (abs (Δ t))
≈⟨ ≈-abs (≈-abs (derive-term-correct t)) ⟩
abs (abs (derive-term t))
≈⟨ ≈-refl ⟩
derive-term (abs t)
∎ where open ≈-Reasoning
derive-term-correct (app t₁ t₂) =
begin
Δ (app t₁ t₂)
≈⟨ Δ-app t₁ t₂ ⟩
app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
≈⟨ ≈-app (≈-app (derive-term-correct t₁) ≈-refl) (derive-term-correct t₂) ⟩
app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
≈⟨ ≈-refl ⟩
derive-term (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct (var x) = ext (λ ρ → derive-var-correct ρ x)
derive-term-correct true = ext (λ ρ → ≡-refl)
derive-term-correct false = ext (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
derive-term-correct (weakenOne _ _ t) = {!!}
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import meaning
open import IlcModel
open import Changes
open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
-- LIFTING terms into Δ-Contexts
lift-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) τ
lift-var this = that this
lift-var (that x) = that (that (lift-var x))
lift-var′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Var Γ τ → Var (Δ-Context′ Γ Γ′) τ
lift-var′ ∅ x = lift-var x
lift-var′ (τ • Γ′) this = this
lift-var′ (τ • Γ′) (that x) = that (lift-var′ Γ′ x)
lift-term′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Term Γ τ → Term (Δ-Context′ Γ Γ′) τ
lift-term′ Γ′ (abs t) = abs (lift-term′ (_ • Γ′) t)
lift-term′ Γ′ (app t₁ t₂) = app (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂)
lift-term′ Γ′ (var x) = var (lift-var′ Γ′ x)
lift-term′ Γ′ true = true
lift-term′ Γ′ false = false
lift-term′ Γ′ (if t₁ t₂ t₃) = if (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂) (lift-term′ Γ′ t₃)
lift-term′ {.(Δ-Context Γ)} {.(Δ-Type τ)} Γ′ (Δ {Γ} {τ} t) = weakenMore Γ′ t
where
open import Relation.Binary.PropositionalEquality using (sym)
doWeakenMore : ∀ Γprefix Γrest {τ} →
Term (Γprefix ⋎ Γrest) (Δ-Type τ) →
Term (Γprefix ⋎ Δ-Context Γrest) (Δ-Type τ)
doWeakenMore Γprefix ∅ t₁ = t₁
doWeakenMore Γprefix (τ₂ • Γrest) {τ} t₁ =
weakenOne Γprefix (Δ-Type τ₂)
(substTerm (sym (move-prefix Γprefix τ₂ (Δ-Context Γrest)))
(doWeakenMore (Γprefix ⋎ (τ₂ • ∅)) Γrest
(substTerm (move-prefix Γprefix τ₂ Γrest) t₁)))
weakenMore : ∀ {Γ τ} Γ′ →
Term Γ τ → Term (Δ-Context′ (Δ-Context Γ) Γ′) (Δ-Type τ)
weakenMore {Γ} Γ′ t =
substTerm
(sym (take-⋎-Δ-Context-drop-Δ-Context′ (Δ-Context Γ) Γ′))
(doWeakenMore (take (Δ-Context Γ) Γ′) (drop (Δ-Context Γ) Γ′) (
substTerm (sym (take-drop (Δ-Context Γ) Γ′)) (Δ t)))
lift-term′ {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) τ
lift-term = lift-term′ ∅
-- PROPERTIES of lift-term
lift-var-ignore : ∀ {Γ τ} (ρ : ⟦ Δ-Context Γ ⟧) (x : Var Γ τ) →
⟦ lift-var x ⟧ ρ ≡ ⟦ x ⟧ (ignore ρ)
lift-var-ignore (v • dv • ρ) this = ≡-refl
lift-var-ignore (v • dv • ρ) (that x) = lift-var-ignore ρ x
lift-var-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) (ρ : ⟦ Δ-Context′ Γ Γ′ ⟧) (x : Var Γ τ) →
⟦ lift-var′ Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (ignore′ Γ′ ρ)
lift-var-ignore′ ∅ ρ x = lift-var-ignore ρ x
lift-var-ignore′ (τ • Γ′) (v • ρ) this = ≡-refl
lift-var-ignore′ (τ • Γ′) (v • ρ) (that x) = lift-var-ignore′ Γ′ ρ x
lift-term-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) {ρ : ⟦ Δ-Context′ Γ Γ′ ⟧} (t : Term Γ τ) →
⟦ lift-term′ Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (ignore′ Γ′ ρ)
lift-term-ignore′ Γ′ (abs t) =
ext (λ v → lift-term-ignore′ (_ • Γ′) t)
lift-term-ignore′ Γ′ (app t₁ t₂) =
≡-app (lift-term-ignore′ Γ′ t₁) (lift-term-ignore′ Γ′ t₂)
lift-term-ignore′ Γ′ (var x) = lift-var-ignore′ Γ′ _ x
lift-term-ignore′ Γ′ true = ≡-refl
lift-term-ignore′ Γ′ false = ≡-refl
lift-term-ignore′ Γ′ {ρ} (if t₁ t₂ t₃)
with ⟦ lift-term′ Γ′ t₁ ⟧ ρ
| ⟦ t₁ ⟧ (ignore′ Γ′ ρ)
| lift-term-ignore′ Γ′ {ρ} t₁
... | true | true | bool = lift-term-ignore′ Γ′ t₂
... | false | false | bool = lift-term-ignore′ Γ′ t₃
lift-term-ignore′ Γ′ (Δ t) = {!!}
lift-term-ignore′ _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term-ignore : ∀ {Γ τ} {ρ : ⟦ Δ-Context Γ ⟧} (t : Term Γ τ) →
⟦ lift-term t ⟧ ρ ≡ ⟦ t ⟧ (ignore ρ)
lift-term-ignore = lift-term-ignore′ ∅
-- PROPERTIES of Δ
Δ-abs : ∀ {Γ τ₁ τ₂} (t : Term (τ₁ • Γ) τ₂) →
Δ (abs t) ≈ abs (abs (Δ t))
Δ-abs t = ext (λ ρ → ≡-refl)
Δ-app : ∀ {Γ τ₁ τ₂} (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) →
Δ (app t₁ t₂) ≈ app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
Δ-app t₁ t₂ = ≈-sym (ext (λ ρ →
begin
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ lift-term t₂ ⟧ ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term t₂ ⟧ ρ))
≡⟨ ≡-cong
(λ x →
diff
(⟦ t₁ ⟧ (update ρ)
(apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x))
(⟦ t₁ ⟧ (ignore ρ) x))
(lift-term-ignore {ρ = ρ} t₂) ⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ t₂ ⟧ (ignore ρ))))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨ ≡-cong
(λ x →
diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))))
(apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩
diff
(⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var this = this
derive-var (that x) = that (that (derive-var x))
diff-term : ∀ {Γ τ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term = {!!}
apply-term : ∀ {Γ τ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term = {!!}
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = {!!}
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = {!!}
derive-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) (Δ-Type τ)
derive-term (abs t) = abs (abs (derive-term t))
derive-term (app t₁ t₂) = app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
derive-term (var x) = var (derive-var x)
derive-term true = false
derive-term false = false
derive-term (if c t e) =
if ((derive-term c) and (lift-term c))
(diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))
(if ((derive-term c) and (lift-term (! c)))
(diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))
(if (lift-term c)
(derive-term t)
(derive-term e)))
derive-term (Δ t) = Δ (derive-term t)
derive-term (weakenOne Γ₁ τ₂ {Γ₃} t) =
substTerm (Δ-Context-⋎-expanded Γ₁ τ₂ Γ₃)
(weakenOne (Δ-Context Γ₁) (Δ-Type τ₂)
(weakenOne (Δ-Context Γ₁) τ₂
(substTerm (Δ-Context-⋎ Γ₁ Γ₃)
(derive-term t))))
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ τ} → (t : Term Γ τ) →
Δ t ≈ derive-term t
derive-term-correct {Γ} (abs t) =
begin
Δ (abs t)
≈⟨ Δ-abs t ⟩
abs (abs (Δ t))
≈⟨ ≈-abs (≈-abs (derive-term-correct t)) ⟩
abs (abs (derive-term t))
≈⟨ ≈-refl ⟩
derive-term (abs t)
∎ where open ≈-Reasoning
derive-term-correct (app t₁ t₂) =
begin
Δ (app t₁ t₂)
≈⟨ Δ-app t₁ t₂ ⟩
app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
≈⟨ ≈-app (≈-app (derive-term-correct t₁) ≈-refl) (derive-term-correct t₂) ⟩
app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
≈⟨ ≈-refl ⟩
derive-term (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct (var x) = ext (λ ρ → derive-var-correct ρ x)
derive-term-correct true = ext (λ ρ → ≡-refl)
derive-term-correct false = ext (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
derive-term-correct (weakenOne _ _ t) = {!!}
|
simplify weakenMore
|
agda/...: simplify weakenMore
* Inline some definitions where appropriate.
* Remove useless pattern matching on context.
Old-commit-hash: a4c4aee377597f7c17b5fa7d8b1c8a02492041c0
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a737167afb8fb406b75d8e0d52fdfb3fa6c50035
|
Parametric/Change/Specification.agda
|
Parametric/Change/Specification.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Change evaluation (Def 3.6 and Fig. 4h).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Validity as Validity
module Parametric.Change.Specification
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
{{validity-structure : Validity.Structure ⟦_⟧Base}}
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open Validity.Structure ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
module Structure where
---------------
-- Interface --
---------------
-- We provide: Derivatives of terms.
⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ)
-- And we provide correctness proofs about the derivatives.
correctness : ∀ {τ Γ} (t : Term Γ τ) →
IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ
--------------------
-- Implementation --
--------------------
⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ)
⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv
⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ
⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ ⟦ c ⟧Const
⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ
⟦_⟧Δ (app {σ} {τ} s t) ρ dρ =
call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ)
⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record
{ apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ)
; correct = λ v dv →
begin
⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎
⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)
≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩
⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ))
≡⟨⟩
⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩
⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ)
≡⟨⟩
⟦ t ⟧ (after-env (dv • dρ))
≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩
⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ)
∎
} where open ≡-Reasoning
correctVar : ∀ {τ Γ} (x : Var Γ τ) →
IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar
correctVar (this) (v • ρ) (dv • dρ) = refl
correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ
correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const
correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ
correctness (app {σ} {τ} s t) ρ dρ =
let
f = ⟦ s ⟧ ρ
g = ⟦ s ⟧ (after-env dρ)
u = ⟦ t ⟧ ρ
v = ⟦ t ⟧ (after-env dρ)
Δf = ⟦ s ⟧Δ ρ dρ
Δu = ⟦ t ⟧Δ ρ dρ
in
begin
f u ⊞₍ τ ₎ call-change {σ} {τ} Δf u Δu
≡⟨ sym (is-valid {σ} {τ} Δf u Δu) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu)
≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩
g v
∎ where open ≡-Reasoning
correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v →
let
-- dρ′ : ΔEnv (σ • Γ) (v • ρ)
dρ′ = nil₍ σ ₎ v • dρ
in
begin
⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′
≡⟨ correctness {τ} t _ dρ′ ⟩
⟦ t ⟧ (after-env dρ′)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩
⟦ t ⟧ (v • after-env dρ)
∎
) where open ≡-Reasoning
-- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv
corollary : ∀ {σ τ Γ}
(s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) →
(⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ)
(⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ)
≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ)
⊞₍ τ ₎
(call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ)
corollary {σ} {τ} s t ρ dρ =
is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ)
corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ))
(v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v)
→ ⟦ t ⟧ ∅ (after₍ σ ₎ dv)
≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv
corollary-closed {σ} {τ} t v dv =
let
f = ⟦ t ⟧ ∅
Δf = ⟦ t ⟧Δ ∅ ∅
in
begin
f (after₍ σ ₎ dv)
≡⟨ cong (λ hole → hole (after₍ σ ₎ dv))
(sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv)
≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩
f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv
∎ where open ≡-Reasoning
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Change evaluation (Def 3.6 and Fig. 4h).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Validity as Validity
module Parametric.Change.Specification
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
{{validity-structure : Validity.Structure ⟦_⟧Base}}
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open Validity.Structure ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
module Structure where
---------------
-- Interface --
---------------
-- We provide: Derivatives of terms.
⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) → Δ₍ τ ₎ (⟦ t ⟧ ρ)
-- And we provide correctness proofs about the derivatives.
correctness : ∀ {τ Γ} (t : Term Γ τ) →
IsDerivative₍ Γ , τ ₎ ⟦ t ⟧ ⟦ t ⟧Δ
--------------------
-- Implementation --
--------------------
⟦_⟧ΔVar : ∀ {τ Γ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧) → Δ₍ Γ ₎ ρ → Δ₍ τ ₎ (⟦ x ⟧Var ρ)
⟦ this ⟧ΔVar (v • ρ) (dv • dρ) = dv
⟦ that x ⟧ΔVar (v • ρ) (dv • dρ) = ⟦ x ⟧ΔVar ρ dρ
⟦_⟧Δ (const {τ} c) ρ dρ = nil₍ τ ₎ ⟦ c ⟧Const
⟦_⟧Δ (var x) ρ dρ = ⟦ x ⟧ΔVar ρ dρ
⟦_⟧Δ (app {σ} {τ} s t) ρ dρ =
call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ)
⟦_⟧Δ (abs {σ} {τ} t) ρ dρ = record
{ apply = λ v dv → ⟦ t ⟧Δ (v • ρ) (dv • dρ)
; correct = λ v dv →
begin
⟦ t ⟧ (v ⊞₍ σ ₎ dv • ρ) ⊞₍ τ ₎
⟦ t ⟧Δ (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ)
≡⟨ correctness t (v ⊞₍ σ ₎ dv • ρ) (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ) ⟩
⟦ t ⟧ (after-env (nil₍ σ ₎ (v ⊞₍ σ ₎ dv) • dρ))
≡⟨⟩
⟦ t ⟧ (((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) • after-env dρ)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ (v ⊞₍ σ ₎ dv)) ⟩
⟦ t ⟧ (v ⊞₍ σ ₎ dv • after-env dρ)
≡⟨⟩
⟦ t ⟧ (after-env (dv • dρ))
≡⟨ sym (correctness t (v • ρ) (dv • dρ)) ⟩
⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ (v • ρ) (dv • dρ)
∎
} where open ≡-Reasoning
correctVar : ∀ {τ Γ} (x : Var Γ τ) →
IsDerivative₍ Γ , τ ₎ ⟦ x ⟧ ⟦ x ⟧ΔVar
correctVar (this) (v • ρ) (dv • dρ) = refl
correctVar (that y) (v • ρ) (dv • dρ) = correctVar y ρ dρ
correctness (const {τ} c) ρ dρ = update-nil₍ τ ₎ ⟦ c ⟧Const
correctness {τ} (var x) ρ dρ = correctVar {τ} x ρ dρ
correctness (app {σ} {τ} s t) ρ dρ =
let
f₁ = ⟦ s ⟧ ρ
f₂ = ⟦ s ⟧ (after-env dρ)
Δf = ⟦ s ⟧Δ ρ dρ
u₁ = ⟦ t ⟧ ρ
u₂ = ⟦ t ⟧ (after-env dρ)
Δu = ⟦ t ⟧Δ ρ dρ
in
begin
f₁ u₁ ⊞₍ τ ₎ call-change {σ} {τ} Δf u₁ Δu
≡⟨ sym (is-valid {σ} {τ} Δf u₁ Δu) ⟩
(f₁ ⊞₍ σ ⇒ τ ₎ Δf) (u₁ ⊞₍ σ ₎ Δu)
≡⟨ correctness {σ ⇒ τ} s ρ dρ ⟨$⟩ correctness {σ} t ρ dρ ⟩
f₂ u₂
∎ where open ≡-Reasoning
correctness {σ ⇒ τ} {Γ} (abs t) ρ dρ = ext (λ v →
let
-- dρ′ : ΔEnv (σ • Γ) (v • ρ)
dρ′ = nil₍ σ ₎ v • dρ
in
begin
⟦ t ⟧ (v • ρ) ⊞₍ τ ₎ ⟦ t ⟧Δ _ dρ′
≡⟨ correctness {τ} t _ dρ′ ⟩
⟦ t ⟧ (after-env dρ′)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • after-env dρ)) (update-nil₍ σ ₎ v) ⟩
⟦ t ⟧ (v • after-env dρ)
∎
) where open ≡-Reasoning
-- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv
corollary : ∀ {σ τ Γ}
(s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) (ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) →
(⟦ s ⟧ ρ ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ ρ dρ)
(⟦ t ⟧ ρ ⊞₍ σ ₎ ⟦ t ⟧Δ ρ dρ)
≡ (⟦ s ⟧ ρ) (⟦ t ⟧ ρ)
⊞₍ τ ₎
(call-change {σ} {τ} (⟦ s ⟧Δ ρ dρ)) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ)
corollary {σ} {τ} s t ρ dρ =
is-valid {σ} {τ} (⟦ s ⟧Δ ρ dρ) (⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ)
corollary-closed : ∀ {σ τ} (t : Term ∅ (σ ⇒ τ))
(v : ⟦ σ ⟧) (dv : Δ₍ σ ₎ v)
→ ⟦ t ⟧ ∅ (after₍ σ ₎ dv)
≡ ⟦ t ⟧ ∅ v ⊞₍ τ ₎ call-change {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv
corollary-closed {σ} {τ} t v dv =
let
f = ⟦ t ⟧ ∅
Δf = ⟦ t ⟧Δ ∅ ∅
in
begin
f (after₍ σ ₎ dv)
≡⟨ cong (λ hole → hole (after₍ σ ₎ dv))
(sym (correctness {σ ⇒ τ} t ∅ ∅)) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (after₍ σ ₎ dv)
≡⟨ is-valid {σ} {τ} (⟦ t ⟧Δ ∅ ∅) v dv ⟩
f (before₍ σ ₎ dv) ⊞₍ τ ₎ call-change {σ} {τ} Δf v dv
∎ where open ≡-Reasoning
|
Rename locals in proof for clarity
|
Rename locals in proof for clarity
Was re-reading this proof for the thesis.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a06adfefda06e416bc4f111d7085cd3b5ad42568
|
Parametric/Change/Evaluation.agda
|
Parametric/Change/Evaluation.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Type as ChangeType
import Parametric.Change.Term as ChangeTerm
import Parametric.Change.Value as ChangeValue
module Parametric.Change.Evaluation
{Base : Type.Structure}
{Const : Term.Structure Base}
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ΔBase : ChangeType.Structure Base)
(apply-base : ChangeTerm.ApplyStructure Const ΔBase)
(diff-base : ChangeTerm.DiffStructure Const ΔBase)
(⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase)
(⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open ChangeType.Structure Base ΔBase
open ChangeTerm.Structure Const ΔBase diff-base apply-base
open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧
open import Relation.Binary.PropositionalEquality
open import Base.Denotation.Notation
open import Postulate.Extensionality
-- Relating `diff` with `diff-term`, `apply` with `apply-term`
ApplyStructure : Set
ApplyStructure = ∀ ι {Γ} →
{t : Term Γ (base ι)} {Δt : Term Γ (ΔType (base ι))} {ρ : ⟦ Γ ⟧} →
⟦ t ⟧ ρ ⟦⊕₍ base ι ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ base ι ₎ Δt ⟧ ρ
DiffStructure : Set
DiffStructure = ∀ ι {Γ} →
{s : Term Γ (base ι)} {t : Term Γ (base ι)} {ρ : ⟦ Γ ⟧} →
⟦ s ⟧ ρ ⟦⊝₍ base ι ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ base ι ₎ t ⟧ ρ
module Structure
(meaning-⊕-base : ApplyStructure)
(meaning-⊝-base : DiffStructure)
where
-- unique names with unambiguous types
-- to help type inference figure things out
private
module Disambiguation where
infixr 9 _⋆_
_⋆_ : Type → Context → Context
_⋆_ = _•_
meaning-⊕ : ∀ {τ Γ}
{t : Term Γ τ} {Δt : Term Γ (ΔType τ)} {ρ : ⟦ Γ ⟧} →
⟦ t ⟧ ρ ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ τ ₎ Δt ⟧ ρ
meaning-⊝ : ∀ {τ Γ}
{s : Term Γ τ} {t : Term Γ τ} {ρ : ⟦ Γ ⟧} →
⟦ s ⟧ ρ ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ τ ₎ t ⟧ ρ
meaning-⊕ {base ι} {Γ} {τ} {Δt} {ρ} = meaning-⊕-base ι {Γ} {τ} {Δt} {ρ}
meaning-⊕ {σ ⇒ τ} {Γ} {t} {Δt} {ρ} = ext (λ v →
let
Γ′ = σ ⋆ (σ ⇒ τ) ⋆ ΔType (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧
ρ′ = v • (⟦ t ⟧ ρ) • (⟦ Δt ⟧ ρ) • ρ
x : Term Γ′ σ
x = var this
f : Term Γ′ (σ ⇒ τ)
f = var (that this)
Δf : Term Γ′ (ΔType (σ ⇒ τ))
Δf = var (that (that this))
y = app f x
Δy = app (app Δf x) (x ⊝ x)
in
begin
⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (v ⟦⊝₍ σ ₎⟧ v)
≡⟨ cong (λ hole → ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v hole)
(meaning-⊝ {s = x} {x} {ρ′}) ⟩
⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (⟦ x ⊝ x ⟧ ρ′)
≡⟨ meaning-⊕ {t = y} {Δt = Δy} {ρ′} ⟩
⟦ y ⊕₍ τ ₎ Δy ⟧ ρ′
∎)
where
open ≡-Reasoning
open Disambiguation
meaning-⊝ {base ι} {Γ} {s} {t} {ρ} = meaning-⊝-base ι {Γ} {s} {t} {ρ}
meaning-⊝ {σ ⇒ τ} {Γ} {s} {t} {ρ} =
ext (λ v → ext (λ Δv →
let
Γ′ = ΔType σ ⋆ σ ⋆ (σ ⇒ τ) ⋆ (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧Context
ρ′ = Δv • v • ⟦ t ⟧Term ρ • ⟦ s ⟧Term ρ • ρ
Δx : Term Γ′ (ΔType σ)
Δx = var this
x : Term Γ′ σ
x = var (that this)
f : Term Γ′ (σ ⇒ τ)
f = var (that (that this))
g : Term Γ′ (σ ⇒ τ)
g = var (that (that (that this)))
y = app f x
y′ = app g (x ⊕₍ σ ₎ Δx)
in
begin
⟦ s ⟧ ρ (v ⟦⊕₍ σ ₎⟧ Δv) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v
≡⟨ cong (λ hole → ⟦ s ⟧ ρ hole ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v)
(meaning-⊕ {t = x} {Δt = Δx} {ρ′}) ⟩
⟦ s ⟧ ρ (⟦ x ⊕₍ σ ₎ Δx ⟧ ρ′) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v
≡⟨ meaning-⊝ {s = y′} {y} {ρ′} ⟩
⟦ y′ ⊝ y ⟧ ρ′
∎))
where
open ≡-Reasoning
open Disambiguation
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Connecting Parametric.Change.Term and Parametric.Change.Value.
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Type as ChangeType
import Parametric.Change.Term as ChangeTerm
import Parametric.Change.Value as ChangeValue
module Parametric.Change.Evaluation
{Base : Type.Structure}
{Const : Term.Structure Base}
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ΔBase : ChangeType.Structure Base)
(apply-base : ChangeTerm.ApplyStructure Const ΔBase)
(diff-base : ChangeTerm.DiffStructure Const ΔBase)
(⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase)
(⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open ChangeType.Structure Base ΔBase
open ChangeTerm.Structure Const ΔBase diff-base apply-base
open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧
open import Relation.Binary.PropositionalEquality
open import Base.Denotation.Notation
open import Postulate.Extensionality
-- Extension point 1: Relating ⊝ and its value on base types
ApplyStructure : Set
ApplyStructure = ∀ ι {Γ} →
{t : Term Γ (base ι)} {Δt : Term Γ (ΔType (base ι))} {ρ : ⟦ Γ ⟧} →
⟦ t ⟧ ρ ⟦⊕₍ base ι ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ base ι ₎ Δt ⟧ ρ
-- Extension point 2: Relating ⊕ and its value on base types
DiffStructure : Set
DiffStructure = ∀ ι {Γ} →
{s : Term Γ (base ι)} {t : Term Γ (base ι)} {ρ : ⟦ Γ ⟧} →
⟦ s ⟧ ρ ⟦⊝₍ base ι ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ base ι ₎ t ⟧ ρ
module Structure
(meaning-⊕-base : ApplyStructure)
(meaning-⊝-base : DiffStructure)
where
-- unique names with unambiguous types
-- to help type inference figure things out
private
module Disambiguation where
infixr 9 _⋆_
_⋆_ : Type → Context → Context
_⋆_ = _•_
-- We provide: Relating ⊕ and ⊝ and their values on arbitrary types.
meaning-⊕ : ∀ {τ Γ}
{t : Term Γ τ} {Δt : Term Γ (ΔType τ)} {ρ : ⟦ Γ ⟧} →
⟦ t ⟧ ρ ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ ≡ ⟦ t ⊕₍ τ ₎ Δt ⟧ ρ
meaning-⊝ : ∀ {τ Γ}
{s : Term Γ τ} {t : Term Γ τ} {ρ : ⟦ Γ ⟧} →
⟦ s ⟧ ρ ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ ≡ ⟦ s ⊝₍ τ ₎ t ⟧ ρ
meaning-⊕ {base ι} {Γ} {τ} {Δt} {ρ} = meaning-⊕-base ι {Γ} {τ} {Δt} {ρ}
meaning-⊕ {σ ⇒ τ} {Γ} {t} {Δt} {ρ} = ext (λ v →
let
Γ′ = σ ⋆ (σ ⇒ τ) ⋆ ΔType (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧
ρ′ = v • (⟦ t ⟧ ρ) • (⟦ Δt ⟧ ρ) • ρ
x : Term Γ′ σ
x = var this
f : Term Γ′ (σ ⇒ τ)
f = var (that this)
Δf : Term Γ′ (ΔType (σ ⇒ τ))
Δf = var (that (that this))
y = app f x
Δy = app (app Δf x) (x ⊝ x)
in
begin
⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (v ⟦⊝₍ σ ₎⟧ v)
≡⟨ cong (λ hole → ⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v hole)
(meaning-⊝ {s = x} {x} {ρ′}) ⟩
⟦ t ⟧ ρ v ⟦⊕₍ τ ₎⟧ ⟦ Δt ⟧ ρ v (⟦ x ⊝ x ⟧ ρ′)
≡⟨ meaning-⊕ {t = y} {Δt = Δy} {ρ′} ⟩
⟦ y ⊕₍ τ ₎ Δy ⟧ ρ′
∎)
where
open ≡-Reasoning
open Disambiguation
meaning-⊝ {base ι} {Γ} {s} {t} {ρ} = meaning-⊝-base ι {Γ} {s} {t} {ρ}
meaning-⊝ {σ ⇒ τ} {Γ} {s} {t} {ρ} =
ext (λ v → ext (λ Δv →
let
Γ′ = ΔType σ ⋆ σ ⋆ (σ ⇒ τ) ⋆ (σ ⇒ τ) ⋆ Γ
ρ′ : ⟦ Γ′ ⟧Context
ρ′ = Δv • v • ⟦ t ⟧Term ρ • ⟦ s ⟧Term ρ • ρ
Δx : Term Γ′ (ΔType σ)
Δx = var this
x : Term Γ′ σ
x = var (that this)
f : Term Γ′ (σ ⇒ τ)
f = var (that (that this))
g : Term Γ′ (σ ⇒ τ)
g = var (that (that (that this)))
y = app f x
y′ = app g (x ⊕₍ σ ₎ Δx)
in
begin
⟦ s ⟧ ρ (v ⟦⊕₍ σ ₎⟧ Δv) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v
≡⟨ cong (λ hole → ⟦ s ⟧ ρ hole ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v)
(meaning-⊕ {t = x} {Δt = Δx} {ρ′}) ⟩
⟦ s ⟧ ρ (⟦ x ⊕₍ σ ₎ Δx ⟧ ρ′) ⟦⊝₍ τ ₎⟧ ⟦ t ⟧ ρ v
≡⟨ meaning-⊝ {s = y′} {y} {ρ′} ⟩
⟦ y′ ⊝ y ⟧ ρ′
∎))
where
open ≡-Reasoning
open Disambiguation
|
Document P.C.Evaluation.
|
Document P.C.Evaluation.
Old-commit-hash: d56cb423e28dc729478353d4e2d2c4fb422d2f8f
|
Agda
|
mit
|
inc-lc/ilc-agda
|
61de78a178ea4a23092e726b277f92787528c970
|
Denotation/Specification/Canon-Popl14.agda
|
Denotation/Specification/Canon-Popl14.agda
|
module Denotation.Specification.Canon-Popl14 where
-- Denotation-as-specification for Calculus Popl14
--
-- Contents
-- - ⟦_⟧Δ : binding-time-shifted derive
-- - Validity and correctness lemmas for ⟦_⟧Δ
-- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments
-- - `corollary-closed`: binding-time-shifted main theorem
open import Popl14.Syntax.Term
open import Popl14.Denotation.Value
open import Popl14.Change.Validity
open import Popl14.Denotation.Evaluation public
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
open import Theorem.Groups-Popl14
open import Theorem.CongApp
open import Postulate.Extensionality
⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (dρ : ΔEnv Γ) → Change τ
-- better name is: ⟦_⟧Δ reacts to future arguments
validity : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) →
valid {τ} (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ)
-- better name is: ⟦_⟧Δ reacts to free variables
correctness : ∀ {τ Γ} {t : Term Γ τ} {dρ : ΔEnv Γ}
→ ⟦ t ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ ≡ ⟦ t ⟧ (update dρ)
⟦_⟧ΔVar : ∀ {τ Γ} → Var Γ τ → ΔEnv Γ → Change τ
⟦ this ⟧ΔVar (cons v dv R[v,dv] • dρ) = dv
⟦ that x ⟧ΔVar (cons v dv R[v,dv] • dρ) = ⟦ x ⟧ΔVar dρ
⟦_⟧Δ (intlit n) dρ = + 0
⟦_⟧Δ (add s t) dρ = ⟦ s ⟧Δ dρ + ⟦ t ⟧Δ dρ
⟦_⟧Δ (minus t) dρ = - ⟦ t ⟧Δ dρ
⟦_⟧Δ empty dρ = emptyBag
⟦_⟧Δ (insert s t) dρ =
let
n = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δn = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
(singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b)
⟦_⟧Δ (union s t) dρ = ⟦ s ⟧Δ dρ ++ ⟦ t ⟧Δ dρ
⟦_⟧Δ (negate t) dρ = negateBag (⟦ t ⟧Δ dρ)
⟦_⟧Δ (flatmap s t) dρ =
let
f = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δf = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b
⟦_⟧Δ (sum t) dρ = sumBag (⟦ t ⟧Δ dρ)
⟦_⟧Δ (var x) dρ = ⟦ x ⟧ΔVar dρ
⟦_⟧Δ (app {σ} {τ} s t) dρ =
(⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ)
(validity {σ} t dρ))
⟦_⟧Δ (abs t) dρ = λ v →
⟦ t ⟧Δ (v • dρ)
validVar : ∀ {τ Γ} (x : Var Γ τ) →
(dρ : ΔEnv Γ) → valid {τ} (⟦ x ⟧ (ignore dρ)) (⟦ x ⟧ΔVar dρ)
validVar this (cons v Δv R[v,Δv] • _) = R[v,Δv]
validVar {τ} (that x) (cons _ _ _ • dρ) = validVar {τ} x dρ
validity (intlit n) dρ = tt
validity (add s t) dρ = tt
validity (minus t) dρ = tt
validity (empty) dρ = tt
validity (insert s t) dρ = tt
validity (union s t) dρ = tt
validity (negate t) dρ = tt
validity (flatmap s t) dρ = tt
validity (sum t) dρ = tt
validity {τ} (var x) dρ = validVar {τ} x dρ
validity (app s t) dρ =
let
v = ⟦ t ⟧ (ignore dρ)
Δv = ⟦ t ⟧Δ dρ
in
proj₁ (validity s dρ (cons v Δv (validity t dρ)))
validity {σ ⇒ τ} (abs t) dρ = λ v →
let
v′ = after {σ} v
Δv′ = v′ ⊟₍ σ ₎ v′
dρ₁ = v • dρ
dρ₂ = nil-valid-change σ v′ • dρ
in
validity t dρ₁
,
(begin
⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂
≡⟨ correctness {t = t} {dρ₂} ⟩
⟦ t ⟧ (update dρ₂)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (update dρ₁)
≡⟨ sym (correctness {t = t} {dρ₁}) ⟩
⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁
∎) where open ≡-Reasoning
correctVar : ∀ {τ Γ} {x : Var Γ τ} {dρ : ΔEnv Γ} →
⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ)
correctVar {x = this } {cons v dv R[v,dv] • dρ} = refl
correctVar {x = that y} {cons v dv R[v,dv] • dρ} = correctVar {x = y} {dρ}
correctness {t = intlit n} = right-id-int n
correctness {t = add s t} {dρ} = trans
(mn·pq=mp·nq
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _+_ (correctness {t = s}) (correctness {t = t}))
correctness {t = minus t} {dρ} = trans
(-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong -_ (correctness {t = t}))
correctness {t = empty} = right-id-bag emptyBag
correctness {t = insert s t} {dρ} =
let
n = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
n′ = ⟦ s ⟧ (update dρ)
b′ = ⟦ t ⟧ (update dρ)
Δn = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
begin
(singletonBag n ++ b) ++
(singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b)
≡⟨ a++[b\\a]=b ⟩
singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)
≡⟨ cong₂ _++_
(cong singletonBag (correctness {t = s}))
(correctness {t = t}) ⟩
singletonBag n′ ++ b′
∎ where open ≡-Reasoning
correctness {t = union s t} {dρ} = trans
(ab·cd=ac·bd
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _++_ (correctness {t = s}) (correctness {t = t}))
correctness {t = negate t} {dρ} = trans
(-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong negateBag (correctness {t = t}))
correctness {t = flatmap s t} {dρ} =
let
f = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δf = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in trans
(a++[b\\a]=b {flatmapBag f b}
{flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf)
(b ⊞₍ base base-bag ₎ Δb)})
(cong₂ flatmapBag (correctness {t = s}) (correctness {t = t}))
correctness {t = sum t} {dρ} =
let
b = ⟦ t ⟧ (ignore dρ)
Δb = ⟦ t ⟧Δ dρ
in
trans (sym homo-sum) (cong sumBag (correctness {t = t}))
correctness {τ} {t = var x} = correctVar {τ} {x = x}
correctness {t = app {σ} {τ} s t} {dρ} =
let
f = ⟦ s ⟧ (ignore dρ)
g = ⟦ s ⟧ (update dρ)
u = ⟦ t ⟧ (ignore dρ)
v = ⟦ t ⟧ (update dρ)
Δf = ⟦ s ⟧Δ dρ
Δu = ⟦ t ⟧Δ dρ
in
begin
f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ))
≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu)
≡⟨ correctness {σ ⇒ τ} {t = s} ⟨$⟩ correctness {σ} {t = t} ⟩
g v
∎ where open ≡-Reasoning
correctness {σ ⇒ τ} {Γ} {abs t} {dρ} = ext (λ v →
let
dρ′ : ΔEnv (σ • Γ)
dρ′ = nil-valid-change σ v • dρ
in
begin
⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′
≡⟨ correctness {τ} {t = t} {dρ′} ⟩
⟦ t ⟧ (update dρ′)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (v • update dρ)
∎
) where open ≡-Reasoning
-- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv
corollary : ∀ {σ τ Γ}
(s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} →
(⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ)
(⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ)
≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ))
⊞₍ τ ₎
(⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))
corollary {σ} {τ} s t {dρ} = proj₂
(validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ))
(⟦ t ⟧Δ dρ) (validity {σ} t dρ)))
corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)}
(v : ValidChange σ)
→ ⟦ t ⟧ ∅ (after {σ} v)
≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v
corollary-closed {σ} {τ} {t = t} v =
let
f = ⟦ t ⟧ ∅
Δf = ⟦ t ⟧Δ ∅
in
begin
f (after {σ} v)
≡⟨ cong (λ hole → hole (after {σ} v))
(sym (correctness {σ ⇒ τ} {t = t})) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v)
≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩
f (before {σ} v) ⊞₍ τ ₎ Δf v
∎ where open ≡-Reasoning
|
module Denotation.Specification.Canon-Popl14 where
-- Denotation-as-specification for Calculus Popl14
--
-- Contents
-- - ⟦_⟧Δ : binding-time-shifted derive
-- - Validity and correctness lemmas for ⟦_⟧Δ
-- - `corollary`: ⟦_⟧Δ reacts to both environment and arguments
-- - `corollary-closed`: binding-time-shifted main theorem
open import Popl14.Syntax.Term
open import Popl14.Denotation.Value
open import Popl14.Change.Validity
open import Popl14.Denotation.Evaluation public
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
open import Theorem.Groups-Popl14
open import Theorem.CongApp
open import Postulate.Extensionality
⟦_⟧Δ : ∀ {τ Γ} → (t : Term Γ τ) (dρ : ΔEnv Γ) → Change τ
-- better name is: ⟦_⟧Δ reacts to future arguments
validity : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ) →
valid {τ} (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ)
-- better name is: ⟦_⟧Δ reacts to free variables
correctness : ∀ {τ Γ} (t : Term Γ τ) (dρ : ΔEnv Γ)
→ ⟦ t ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ ≡ ⟦ t ⟧ (update dρ)
⟦_⟧ΔVar : ∀ {τ Γ} → Var Γ τ → ΔEnv Γ → Change τ
⟦ this ⟧ΔVar (cons v dv R[v,dv] • dρ) = dv
⟦ that x ⟧ΔVar (cons v dv R[v,dv] • dρ) = ⟦ x ⟧ΔVar dρ
⟦_⟧Δ (intlit n) dρ = + 0
⟦_⟧Δ (add s t) dρ = ⟦ s ⟧Δ dρ + ⟦ t ⟧Δ dρ
⟦_⟧Δ (minus t) dρ = - ⟦ t ⟧Δ dρ
⟦_⟧Δ empty dρ = emptyBag
⟦_⟧Δ (insert s t) dρ =
let
n = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δn = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
(singletonBag (n ⊞₍ int ₎ Δn) ++ (b ⊞₍ bag ₎ Δb)) ⊟₍ bag ₎ (singletonBag n ++ b)
⟦_⟧Δ (union s t) dρ = ⟦ s ⟧Δ dρ ++ ⟦ t ⟧Δ dρ
⟦_⟧Δ (negate t) dρ = negateBag (⟦ t ⟧Δ dρ)
⟦_⟧Δ (flatmap s t) dρ =
let
f = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δf = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
flatmapBag (f ⊞₍ int ⇒ bag ₎ Δf) (b ⊞₍ bag ₎ Δb) ⊟₍ bag ₎ flatmapBag f b
⟦_⟧Δ (sum t) dρ = sumBag (⟦ t ⟧Δ dρ)
⟦_⟧Δ (var x) dρ = ⟦ x ⟧ΔVar dρ
⟦_⟧Δ (app {σ} {τ} s t) dρ =
(⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ)
(validity {σ} t dρ))
⟦_⟧Δ (abs t) dρ = λ v →
⟦ t ⟧Δ (v • dρ)
validVar : ∀ {τ Γ} (x : Var Γ τ) →
(dρ : ΔEnv Γ) → valid {τ} (⟦ x ⟧ (ignore dρ)) (⟦ x ⟧ΔVar dρ)
validVar this (cons v Δv R[v,Δv] • _) = R[v,Δv]
validVar {τ} (that x) (cons _ _ _ • dρ) = validVar {τ} x dρ
validity (intlit n) dρ = tt
validity (add s t) dρ = tt
validity (minus t) dρ = tt
validity (empty) dρ = tt
validity (insert s t) dρ = tt
validity (union s t) dρ = tt
validity (negate t) dρ = tt
validity (flatmap s t) dρ = tt
validity (sum t) dρ = tt
validity {τ} (var x) dρ = validVar {τ} x dρ
validity (app s t) dρ =
let
v = ⟦ t ⟧ (ignore dρ)
Δv = ⟦ t ⟧Δ dρ
in
proj₁ (validity s dρ (cons v Δv (validity t dρ)))
validity {σ ⇒ τ} (abs t) dρ = λ v →
let
v′ = after {σ} v
Δv′ = v′ ⊟₍ σ ₎ v′
dρ₁ = v • dρ
dρ₂ = nil-valid-change σ v′ • dρ
in
validity t dρ₁
,
(begin
⟦ t ⟧ (ignore dρ₂) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₂
≡⟨ correctness t dρ₂ ⟩
⟦ t ⟧ (update dρ₂)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (update dρ₁)
≡⟨ sym (correctness t dρ₁) ⟩
⟦ t ⟧ (ignore dρ₁) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ₁
∎) where open ≡-Reasoning
correctVar : ∀ {τ Γ} {x : Var Γ τ} {dρ : ΔEnv Γ} →
⟦ x ⟧ (ignore dρ) ⊞₍ τ ₎ ⟦ x ⟧ΔVar dρ ≡ ⟦ x ⟧ (update dρ)
correctVar {x = this } {cons v dv R[v,dv] • dρ} = refl
correctVar {x = that y} {cons v dv R[v,dv] • dρ} = correctVar {x = y} {dρ}
correctness (intlit n) dρ = right-id-int n
correctness (add s t) dρ = trans
(mn·pq=mp·nq
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _+_ (correctness s dρ) (correctness t dρ))
correctness (minus t) dρ = trans
(-m·-n=-mn {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong -_ (correctness t dρ))
correctness empty dρ = right-id-bag emptyBag
correctness (insert s t) dρ =
let
n = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
n′ = ⟦ s ⟧ (update dρ)
b′ = ⟦ t ⟧ (update dρ)
Δn = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in
begin
(singletonBag n ++ b) ++
(singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)) \\ (singletonBag n ++ b)
≡⟨ a++[b\\a]=b ⟩
singletonBag (n ⊞₍ base base-int ₎ Δn) ++
(b ⊞₍ base base-bag ₎ Δb)
≡⟨ cong₂ _++_
(cong singletonBag (correctness s dρ))
(correctness t dρ) ⟩
singletonBag n′ ++ b′
∎ where open ≡-Reasoning
correctness (union s t) dρ = trans
(ab·cd=ac·bd
{⟦ s ⟧ (ignore dρ)} {⟦ t ⟧ (ignore dρ)} {⟦ s ⟧Δ dρ} {⟦ t ⟧Δ dρ})
(cong₂ _++_ (correctness s dρ) (correctness t dρ))
correctness (negate t) dρ = trans
(-a·-b=-ab {⟦ t ⟧ (ignore dρ)} {⟦ t ⟧Δ dρ})
(cong negateBag (correctness t dρ))
correctness (flatmap s t) dρ =
let
f = ⟦ s ⟧ (ignore dρ)
b = ⟦ t ⟧ (ignore dρ)
Δf = ⟦ s ⟧Δ dρ
Δb = ⟦ t ⟧Δ dρ
in trans
(a++[b\\a]=b {flatmapBag f b}
{flatmapBag (f ⊞₍ base base-int ⇒ base base-bag ₎ Δf)
(b ⊞₍ base base-bag ₎ Δb)})
(cong₂ flatmapBag (correctness s dρ) (correctness t dρ))
correctness (sum t) dρ =
let
b = ⟦ t ⟧ (ignore dρ)
Δb = ⟦ t ⟧Δ dρ
in
trans (sym homo-sum) (cong sumBag (correctness t dρ))
correctness {τ} (var x) dρ = correctVar {τ} {x = x}
correctness (app {σ} {τ} s t) dρ =
let
f = ⟦ s ⟧ (ignore dρ)
g = ⟦ s ⟧ (update dρ)
u = ⟦ t ⟧ (ignore dρ)
v = ⟦ t ⟧ (update dρ)
Δf = ⟦ s ⟧Δ dρ
Δu = ⟦ t ⟧Δ dρ
in
begin
f u ⊞₍ τ ₎ Δf (cons u Δu (validity {σ} t dρ))
≡⟨ sym (proj₂ (validity {σ ⇒ τ} s dρ (cons u Δu (validity {σ} t dρ)))) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (u ⊞₍ σ ₎ Δu)
≡⟨ correctness {σ ⇒ τ} s dρ ⟨$⟩ correctness {σ} t dρ ⟩
g v
∎ where open ≡-Reasoning
correctness {σ ⇒ τ} {Γ} (abs t) dρ = ext (λ v →
let
dρ′ : ΔEnv (σ • Γ)
dρ′ = nil-valid-change σ v • dρ
in
begin
⟦ t ⟧ (ignore dρ′) ⊞₍ τ ₎ ⟦ t ⟧Δ dρ′
≡⟨ correctness {τ} t dρ′ ⟩
⟦ t ⟧ (update dρ′)
≡⟨ cong (λ hole → ⟦ t ⟧ (hole • update dρ)) (v+[u-v]=u {σ}) ⟩
⟦ t ⟧ (v • update dρ)
∎
) where open ≡-Reasoning
-- Corollary: (f ⊞ df) (v ⊞ dv) = f v ⊞ df v dv
corollary : ∀ {σ τ Γ}
(s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) {dρ : ΔEnv Γ} →
(⟦ s ⟧ (ignore dρ) ⊞₍ σ ⇒ τ ₎ ⟦ s ⟧Δ dρ)
(⟦ t ⟧ (ignore dρ) ⊞₍ σ ₎ ⟦ t ⟧Δ dρ)
≡ (⟦ s ⟧ (ignore dρ)) (⟦ t ⟧ (ignore dρ))
⊞₍ τ ₎
(⟦ s ⟧Δ dρ) (cons (⟦ t ⟧ (ignore dρ)) (⟦ t ⟧Δ dρ) (validity {σ} t dρ))
corollary {σ} {τ} s t {dρ} = proj₂
(validity {σ ⇒ τ} s dρ (cons (⟦ t ⟧ (ignore dρ))
(⟦ t ⟧Δ dρ) (validity {σ} t dρ)))
corollary-closed : ∀ {σ τ} {t : Term ∅ (σ ⇒ τ)}
(v : ValidChange σ)
→ ⟦ t ⟧ ∅ (after {σ} v)
≡ ⟦ t ⟧ ∅ (before {σ} v) ⊞₍ τ ₎ ⟦ t ⟧Δ ∅ v
corollary-closed {σ} {τ} {t = t} v =
let
f = ⟦ t ⟧ ∅
Δf = ⟦ t ⟧Δ ∅
in
begin
f (after {σ} v)
≡⟨ cong (λ hole → hole (after {σ} v))
(sym (correctness {σ ⇒ τ} t ∅)) ⟩
(f ⊞₍ σ ⇒ τ ₎ Δf) (after {σ} v)
≡⟨ proj₂ (validity {σ ⇒ τ} t ∅ v) ⟩
f (before {σ} v) ⊞₍ τ ₎ Δf v
∎ where open ≡-Reasoning
|
Make some arguments of correctness explicit.
|
Make some arguments of correctness explicit.
Old-commit-hash: b8f4ff3d6b2f0ea72c79a94c10ae3ec1b545c2ca
|
Agda
|
mit
|
inc-lc/ilc-agda
|
80463c85413df224feab7c464101dbfd5ff7ec77
|
Syntax/Language/Atlas.agda
|
Syntax/Language/Atlas.agda
|
module Syntax.Language.Atlas where
-- Base types of the calculus Atlas
-- to be used with Plotkin-style language description
--
-- Atlas supports maps with neutral elements. The change type to
-- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the
-- ground type ι. Such a change to maps can support insertions
-- and deletions as well: Inserting `k -> v` means mapping `k` to
-- the change from the neutral element to `v`, and deleting
-- `k -> v` means mapping `k` to the change from `v` to the
-- neutral element.
open import Syntax.Language.Calculus
data Atlas-type : Set where
Bool : Atlas-type
Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type
data Atlas-const : Set where
true : Atlas-const
false : Atlas-const
xor : Atlas-const
empty : ∀ {κ ι : Atlas-type} → Atlas-const
update : ∀ {κ ι : Atlas-type} → Atlas-const
zip : ∀ {κ a b c : Atlas-type} → Atlas-const
Atlas-lookup : Atlas-const → Type Atlas-type
Atlas-lookup true = base Bool
Atlas-lookup false = base Bool
Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool
Atlas-lookup (empty {κ} {ι}) = base (Map κ ι)
-- `update key val my-map` would
-- - insert if `key` is not present in `my-map`
-- - delete if `val` is the neutral element
-- - make an update otherwise
Atlas-lookup (update {κ} {ι}) =
base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι)
-- Model of zip = Haskell Data.List.zipWith
-- zipWith :: (a → b → c) → [a] → [b] → [c]
Atlas-lookup (zip {κ} {a} {b} {c}) =
(base κ ⇒ base a ⇒ base b ⇒ base c) ⇒
base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c)
Atlas-Δbase : Atlas-type → Atlas-type
-- change to a boolean is a xor-rand
Atlas-Δbase Bool = Bool
-- change to a map is change to its values
Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val))
Atlas-Δtype : Type Atlas-type → Type Atlas-type
Atlas-Δtype = lift-Δtype₀ Atlas-Δbase
Atlas-context : Set
Atlas-context = Context {Type Atlas-type}
Atlas-term : Atlas-context → Type Atlas-type → Set
Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}
-- Every base type has a known nil-change.
-- The nil-change of ι is also the neutral element of Map κ Δι.
neutral : ∀ {ι : Atlas-type} → Atlas-const
neutral {Bool} = false
neutral {Map κ ι} = empty {κ} {ι}
neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι)
neutral-term {Bool} = const (neutral {Bool})
neutral-term {Map κ ι} = const (neutral {Map κ ι})
nil-const : ∀ {ι : Atlas-type} → Atlas-const
nil-const {ι} = neutral {Atlas-Δbase ι}
nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι))
nil-term {Bool} = const (nil-const {Bool})
nil-term {Map κ ι} = const (nil-const {Map κ ι})
-- Shorthands of constants
--
-- There's probably a uniform way to lift constants
-- into term constructors.
--
-- TODO: write this and call it Syntax.Term.Plotkin.lift-term
zip! : ∀ {κ a b c Γ} →
Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) →
Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) →
Atlas-term Γ (base (Map κ c))
zip! f m₁ m₂ = app (app (app (const zip) f) m₁) m₂
lookup! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base ι)
lookup! = {!!} -- TODO: add constant `lookup`
-- diff-term and apply-term
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι))
Atlas-diff {Bool} = const xor
Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = const xor
Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Atlas-term Γ τ → Atlas-term Γ τ →
Atlas-term Γ (Atlas-Δtype τ)
diff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t
apply : ∀ {τ Γ} →
Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ →
Atlas-term Γ τ
apply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
update! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base (Map κ ι))
update! k v m = app (app (app (const update) k) v) m
insert : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
-- last argument is the change accumulator
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
-- The binary operator such that
-- union t t ≡ t
-- To implement union, we need for each non-map base-type
-- an operator such that `op v v = v` on all values `v`.
-- for Booleans, conjunction is good enough.
--
-- TODO (later): support conjunction, probably by Boolean
-- elimination form if-then-else
postulate
and! : ∀ {Γ} →
Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) →
Atlas-term Γ (base Bool)
union : ∀ {ι Γ} →
Atlas-term Γ (base ι) → Atlas-term Γ (base ι) →
Atlas-term Γ (base ι)
union {Bool} s t = and! s t
union {Map κ ι} s t =
let
union-term = abs (abs (union (var (that this)) (var this)))
in
zip! (abs union-term) s t
-- Shorthand for 4-way zip
zip4! : ∀ {κ a b c d e Γ} →
let
t:_ = λ ι → Atlas-term Γ (base ι)
in
Atlas-term Γ
(base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) →
t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e
zip4! f m₁ m₂ m₃ m₄ =
let
v₁ = var (that this)
v₂ = var this
v₃ = var (that this)
v₄ = var this
k₁₂ = var (that (that this))
k₃₄ = var (that (that this))
f₁₂ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₁₂) v₁) v₂)
(lookup! k₁₂ (weaken₃ m₃))) (lookup! k₁₂ (weaken₃ m₄)))))
f₃₄ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₃₄)
(lookup! k₃₄ (weaken₃ m₁)))
(lookup! k₃₄ (weaken₃ m₂))) v₃) v₄)))
in
-- A correct but inefficient implementation.
-- May want to speed it up after constants are finalized.
union (zip! f₁₂ m₁ m₂) (zip! f₃₄ m₃ m₄)
-- Type signature of Atlas-Δconst is boilerplate.
Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) →
Atlas-term Γ (Atlas-Δtype (Atlas-lookup c))
Atlas-Δconst true = const false
Atlas-Δconst false = const false
-- Δxor = λ x Δx y Δy → Δx xor Δy
Atlas-Δconst xor =
let
Δx = var (that (that this))
Δy = var this
in abs (abs (abs (abs (app (app (const xor) Δx) Δy))))
Atlas-Δconst empty = const empty
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
Atlas-Δconst update =
let
k = var (that (that (that (that (that this)))))
Δk = var (that (that (that (that this))))
v = var (that (that (that this)))
Δv = var (that (that this))
-- m = var (that this) -- unused parameter
Δm = var this
in
abs (abs (abs (abs (abs (abs
(insert (apply Δk k) (apply Δv v)
(delete k v Δm)))))))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
Atlas-Δconst zip =
let
Δf = var (that (that (that (that this))))
m₁ = var (that (that (that this)))
Δm₁ = var (that (that this))
m₂ = var (that this)
Δm₂ = var this
g = abs (app (app (weaken₁ Δf) (var this)) nil-term)
in
abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂))))))
Atlas = calculus-with
Atlas-type
Atlas-const
Atlas-lookup
Atlas-Δtype
Atlas-Δconst
|
module Syntax.Language.Atlas where
-- Base types of the calculus Atlas
-- to be used with Plotkin-style language description
--
-- Atlas supports maps with neutral elements. The change type to
-- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the
-- ground type ι. Such a change to maps can support insertions
-- and deletions as well: Inserting `k -> v` means mapping `k` to
-- the change from the neutral element to `v`, and deleting
-- `k -> v` means mapping `k` to the change from `v` to the
-- neutral element.
open import Syntax.Language.Calculus
data Atlas-type : Set where
Bool : Atlas-type
Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type
data Atlas-const : Set where
true : Atlas-const
false : Atlas-const
xor : Atlas-const
empty : ∀ {κ ι : Atlas-type} → Atlas-const
update : ∀ {κ ι : Atlas-type} → Atlas-const
zip : ∀ {κ a b c : Atlas-type} → Atlas-const
Atlas-lookup : Atlas-const → Type Atlas-type
Atlas-lookup true = base Bool
Atlas-lookup false = base Bool
Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool
Atlas-lookup (empty {κ} {ι}) = base (Map κ ι)
-- `update key val my-map` would
-- - insert if `key` is not present in `my-map`
-- - delete if `val` is the neutral element
-- - make an update otherwise
Atlas-lookup (update {κ} {ι}) =
base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι)
-- Model of zip = Haskell Data.List.zipWith
-- zipWith :: (a → b → c) → [a] → [b] → [c]
Atlas-lookup (zip {κ} {a} {b} {c}) =
(base κ ⇒ base a ⇒ base b ⇒ base c) ⇒
base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c)
Atlas-Δbase : Atlas-type → Atlas-type
-- change to a boolean is a xor-rand
Atlas-Δbase Bool = Bool
-- change to a map is change to its values
Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val))
Atlas-Δtype : Type Atlas-type → Type Atlas-type
Atlas-Δtype = lift-Δtype₀ Atlas-Δbase
Atlas-context : Set
Atlas-context = Context {Type Atlas-type}
Atlas-term : Atlas-context → Type Atlas-type → Set
Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}
-- Every base type has a known nil-change.
-- The nil-change of ι is also the neutral element of Map κ Δι.
neutral : ∀ {ι : Atlas-type} → Atlas-const
neutral {Bool} = false
neutral {Map κ ι} = empty {κ} {ι}
neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι)
neutral-term {Bool} = const (neutral {Bool})
neutral-term {Map κ ι} = const (neutral {Map κ ι})
nil-const : ∀ {ι : Atlas-type} → Atlas-const
nil-const {ι} = neutral {Atlas-Δbase ι}
nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι))
nil-term {Bool} = const (nil-const {Bool})
nil-term {Map κ ι} = const (nil-const {Map κ ι})
-- Shorthands of constants
--
-- There's probably a uniform way to lift constants
-- into term constructors.
--
-- TODO: write this and call it Syntax.Term.Plotkin.lift-term
zip! : ∀ {κ a b c Γ} →
Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) →
Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) →
Atlas-term Γ (base (Map κ c))
zip! f m₁ m₂ = app (app (app (const zip) f) m₁) m₂
lookup! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base ι)
lookup! = {!!} -- TODO: add constant `lookup`
-- diff-term and apply-term
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι))
Atlas-diff {Bool} = const xor
Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = const xor
Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Atlas-term Γ τ → Atlas-term Γ τ →
Atlas-term Γ (Atlas-Δtype τ)
diff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t
apply : ∀ {τ Γ} →
Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ →
Atlas-term Γ τ
apply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
update! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base (Map κ ι))
update! k v m = app (app (app (const update) k) v) m
insert : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
-- last argument is the change accumulator
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
-- union s t should be:
-- 1. equal to t if s is neutral
-- 2. equal to s if t is neutral
-- 3. equal to s if s == t
-- 4. whatever it wants to be otherwise
--
-- On Booleans, only conjunction can be union.
--
-- TODO (later): support conjunction, probably by Boolean
-- elimination form if-then-else
postulate
and! : ∀ {Γ} →
Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) →
Atlas-term Γ (base Bool)
union : ∀ {ι Γ} →
Atlas-term Γ (base ι) → Atlas-term Γ (base ι) →
Atlas-term Γ (base ι)
union {Bool} s t = and! s t
union {Map κ ι} s t =
let
union-term = abs (abs (union (var (that this)) (var this)))
in
zip! (abs union-term) s t
-- Shorthand for 4-way zip
zip4! : ∀ {κ a b c d e Γ} →
let
t:_ = λ ι → Atlas-term Γ (base ι)
in
Atlas-term Γ
(base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) →
t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e
zip4! f m₁ m₂ m₃ m₄ =
let
v₁ = var (that this)
v₂ = var this
v₃ = var (that this)
v₄ = var this
k₁₂ = var (that (that this))
k₃₄ = var (that (that this))
f₁₂ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₁₂) v₁) v₂)
(lookup! k₁₂ (weaken₃ m₃))) (lookup! k₁₂ (weaken₃ m₄)))))
f₃₄ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₃₄)
(lookup! k₃₄ (weaken₃ m₁)))
(lookup! k₃₄ (weaken₃ m₂))) v₃) v₄)))
in
-- A correct but inefficient implementation.
-- May want to speed it up after constants are finalized.
union (zip! f₁₂ m₁ m₂) (zip! f₃₄ m₃ m₄)
-- Type signature of Atlas-Δconst is boilerplate.
Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) →
Atlas-term Γ (Atlas-Δtype (Atlas-lookup c))
Atlas-Δconst true = const false
Atlas-Δconst false = const false
-- Δxor = λ x Δx y Δy → Δx xor Δy
Atlas-Δconst xor =
let
Δx = var (that (that this))
Δy = var this
in abs (abs (abs (abs (app (app (const xor) Δx) Δy))))
Atlas-Δconst empty = const empty
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
Atlas-Δconst update =
let
k = var (that (that (that (that (that this)))))
Δk = var (that (that (that (that this))))
v = var (that (that (that this)))
Δv = var (that (that this))
-- m = var (that this) -- unused parameter
Δm = var this
in
abs (abs (abs (abs (abs (abs
(insert (apply Δk k) (apply Δv v)
(delete k v Δm)))))))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
Atlas-Δconst zip =
let
Δf = var (that (that (that (that this))))
m₁ = var (that (that (that this)))
Δm₁ = var (that (that this))
m₂ = var (that this)
Δm₂ = var this
g = abs (app (app (weaken₁ Δf) (var this)) nil-term)
in
abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂))))))
Atlas = calculus-with
Atlas-type
Atlas-const
Atlas-lookup
Atlas-Δtype
Atlas-Δconst
|
improve specification of union
|
Atlas: improve specification of union
Old-commit-hash: cda40f70859c26a48a9077ad39b93d3b9a0af045
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2c54443576bbae481639179d8b3bc48566274aad
|
Syntax/FreeVars/Popl14.agda
|
Syntax/FreeVars/Popl14.agda
|
module Syntax.FreeVars.Popl14 where
-- Free variables of Calculus Popl14
--
-- Contents
-- FV: get the free variables of a term
-- closed?: test if a term is closed, producing a witness if yes.
open import Popl14.Syntax.Type
open import Popl14.Change.Term
open import Base.Syntax.Vars Type public
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Sum
FV : ∀ {τ Γ} → Term Γ τ → Vars Γ
FV {Γ = Γ} (intlit n) = none
FV (add s t) = FV s ∪ FV t
FV (minus t) = FV t
FV {Γ = Γ} empty = none
FV (insert s t) = FV s ∪ FV t
FV (union s t) = FV s ∪ FV t
FV (negate t) = FV t
FV (flatmap s t) = FV s ∪ FV t
FV (sum t) = FV t
FV (var x) = singleton x
FV (abs t) = tail (FV t)
FV (app s t) = FV s ∪ FV t
closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤
closed? t = empty? (FV t)
|
module Syntax.FreeVars.Popl14 where
-- Free variables of Calculus Popl14
--
-- Contents
-- FV: get the free variables of a term
-- closed?: test if a term is closed, producing a witness if yes.
open import Popl14.Syntax.Type
open import Popl14.Change.Term
open import Base.Syntax.Vars Type public
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Sum
FV : ∀ {τ Γ} → Term Γ τ → Vars Γ
FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ
FV (const ι ts) = FV-terms ts
FV (var x) = singleton x
FV (abs t) = tail (FV t)
FV (app s t) = FV s ∪ FV t
FV-terms ∅ = none
FV-terms (t • ts) = FV t ∪ FV-terms ts
closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤
closed? t = empty? (FV t)
|
Implement FV more parametrically.
|
Implement FV more parametrically.
Old-commit-hash: 470f2fd7a3e73bb1ac01fce483f8aa80e4ec51e3
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e71e428c1e63d766e7896b94aa406645ef65a52e
|
ModalLogic.agda
|
ModalLogic.agda
|
module ModalLogic where
-- Implement Frank Pfenning's lambda calculus based on modal logic S4, as
-- described in "A Modal Analysis of Staged Computation".
open import Denotational.Notation
data BaseType : Set where
Base : BaseType
Next : BaseType → BaseType
data Type : Set where
base : BaseType → Type
_⇒_ : Type → Type → Type
□_ : Type → Type
-- Reuse contexts, variables and weakening from Tillmann's library.
open import Syntactic.Contexts Type
-- No semantics for these types.
data Term : Context → Context → Type → Set where
abs : ∀ {τ₁ τ₂ Γ Δ} → (t : Term (τ₁ • Γ) Δ τ₂) → Term Γ Δ (τ₁ ⇒ τ₂)
app : ∀ {τ₁ τ₂ Γ Δ} → (t₁ : Term Γ Δ (τ₁ ⇒ τ₂)) (t₂ : Term Γ Δ τ₁) → Term Γ Δ τ₂
ovar : ∀ {Γ Δ τ} → (x : Var Γ τ) → Term Γ Δ τ
mvar : ∀ {Γ Δ τ} → (u : Var Δ τ) → Term Γ Δ τ
-- Note the builtin weakening
box : ∀ {Γ Δ τ} → Term ∅ Δ τ → Term Γ Δ (□ τ)
let-box_in-_ : ∀ {τ₁ τ₂ Γ Δ} → (e₁ : Term Γ Δ (□ τ₁)) → (e₂ : Term Γ (τ₁ • Δ) τ₂) → Term Γ Δ τ₂
-- Lemma 1 includes weakening.
-- Most of the proof can be generated by C-c C-a, but syntax errors must then be fixed.
weaken : ∀ {Γ₁ Γ₂ Δ₁ Δ₂ τ} → (Γ′ : Γ₁ ≼ Γ₂) → (Δ′ : Δ₁ ≼ Δ₂) → Term Γ₁ Δ₁ τ → Term Γ₂ Δ₂ τ
weaken Γ′ Δ′ (abs {τ₁} t) = abs (weaken (keep τ₁ • Γ′) Δ′ t)
weaken Γ′ Δ′ (app t t₁) = app (weaken Γ′ Δ′ t) (weaken Γ′ Δ′ t₁)
weaken Γ′ Δ′ (ovar x) = ovar (lift Γ′ x)
weaken Γ′ Δ′ (mvar u) = mvar (lift Δ′ u)
weaken Γ′ Δ′ (box t) = box (weaken ∅ Δ′ t)
weaken Γ′ Δ′ (let-box_in-_ {τ₁} t t₁) =
let-box weaken Γ′ Δ′ t in-
weaken Γ′ (keep τ₁ • Δ′) t₁
|
module ModalLogic where
-- Implement Frank Pfenning's lambda calculus based on modal logic S4, as
-- described in "A Modal Analysis of Staged Computation".
open import Denotational.Notation
data BaseType : Set where
Base : BaseType
Next : BaseType → BaseType
data Type : Set where
base : BaseType → Type
_⇒_ : Type → Type → Type
□_ : Type → Type
-- Reuse contexts, variables and weakening from Tillmann's library.
open import Syntactic.Contexts Type
-- No semantics for these types.
data Term : Context → Context → Type → Set where
abs : ∀ {τ₁ τ₂ Γ Δ} → (t : Term (τ₁ • Γ) Δ τ₂) → Term Γ Δ (τ₁ ⇒ τ₂)
app : ∀ {τ₁ τ₂ Γ Δ} → (t₁ : Term Γ Δ (τ₁ ⇒ τ₂)) (t₂ : Term Γ Δ τ₁) → Term Γ Δ τ₂
ovar : ∀ {Γ Δ τ} → (x : Var Γ τ) → Term Γ Δ τ
mvar : ∀ {Γ Δ τ} → (u : Var Δ τ) → Term Γ Δ τ
-- Note the builtin weakening
box : ∀ {Γ Δ τ} → Term ∅ Δ τ → Term Γ Δ (□ τ)
let-box_in-_ : ∀ {τ₁ τ₂ Γ Δ} → (e₁ : Term Γ Δ (□ τ₁)) → (e₂ : Term Γ (τ₁ • Δ) τ₂) → Term Γ Δ τ₂
-- Lemma 1 includes weakening.
-- Attempt 1 at weakening, subcontext-based. This works, but it is not what we need later.
--
-- Most of the proof can be generated by C-c C-a, but syntax errors must then be
-- fixed and lift introduced by hand.
weaken : ∀ {Γ₁ Γ₂ Δ₁ Δ₂ τ} → (Γ′ : Γ₁ ≼ Γ₂) → (Δ′ : Δ₁ ≼ Δ₂) → Term Γ₁ Δ₁ τ → Term Γ₂ Δ₂ τ
weaken Γ′ Δ′ (abs {τ₁} t) = abs (weaken (keep τ₁ • Γ′) Δ′ t)
weaken Γ′ Δ′ (app t t₁) = app (weaken Γ′ Δ′ t) (weaken Γ′ Δ′ t₁)
weaken Γ′ Δ′ (ovar x) = ovar (lift Γ′ x)
weaken Γ′ Δ′ (mvar u) = mvar (lift Δ′ u)
weaken Γ′ Δ′ (box t) = box (weaken ∅ Δ′ t)
weaken Γ′ Δ′ (let-box_in-_ {τ₁} t t₁) =
let-box weaken Γ′ Δ′ t in-
weaken Γ′ (keep τ₁ • Δ′) t₁
open import Relation.Binary.PropositionalEquality
weaken-≼ : ∀ {Γ₁ Γ₂ Γ₃ Δ τ} → Term (Γ₁ ⋎ Γ₃) Δ τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) Δ τ
weaken-≼ {Γ₁} {Γ₂} {Γ₃} (abs {τ₁} t) = abs (weaken-≼ {τ₁ • Γ₁} {Γ₂} t)
weaken-≼ {Γ₁} {Γ₂} (app t t₁) = app (weaken-≼ {Γ₁} {Γ₂} t) (weaken-≼ {Γ₁} {Γ₂} t₁)
weaken-≼ {Γ₁} {Γ₂} (ovar x) = ovar (Prefixes.lift {Γ₁} {Γ₂} x)
weaken-≼ (mvar u) = mvar u
weaken-≼ (box t) = box t
weaken-≼ {Γ₁} {Γ₂} (let-box t in- t₁) = let-box weaken-≼ {Γ₁} {Γ₂} t in- weaken-≼ {Γ₁} {Γ₂} t₁
substVar : ∀ {Γ Γ′ Δ τ₁ τ₂} → (t : Term Γ′ Δ τ₁) → (v : Var (Γ ⋎ (τ₁ • Γ′)) τ₂) → Term (Γ ⋎ Γ′) Δ τ₂
substVar {∅} t this = t
substVar {∅} t (that v) = ovar v
substVar {τ • Γ} t this = ovar this
substVar {τ • Γ} t (that v) = weaken-≼ {∅} {τ • ∅} (substVar {Γ} t v)
substTerm : ∀ {Γ Γ′ Δ τ₁ τ₂} → (t₁ : Term Γ′ Δ τ₁) → (t₂ : Term (Γ ⋎ (τ₁ • Γ′)) Δ τ₂) → Term (Γ ⋎ Γ′) Δ τ₂
substTerm {Γ} {Γ′} t₁ (abs {τ₂} t₂) = abs ( substTerm {τ₂ • Γ} {Γ′} t₁ t₂ )
substTerm {Γ} {Γ′} t₁ (app t₂ t₃) = app (substTerm {Γ} {Γ′} t₁ t₂) (substTerm {Γ} {Γ′} t₁ t₃)
substTerm {Γ} {Γ′} t₁ (ovar v) = substVar {Γ} {Γ′} t₁ v
substTerm t₁ (mvar u) = mvar u
substTerm t₁ (box t₂) = box t₂
substTerm {Γ} {Γ′} t₁ (let-box_in-_ {τ₃} t₂ t₃) =
let-box substTerm {Γ} {Γ′} t₁ t₂ in-
substTerm {Γ} {Γ′}
(weaken ≼-refl (drop τ₃ • ≼-refl) t₁)
t₃
|
implement substitution for modal logic
|
Agda: implement substitution for modal logic
Old-commit-hash: e8580d4ac39e998fdf42c16064112cd2c0326763
|
Agda
|
mit
|
inc-lc/ilc-agda
|
d02c0c3ef60a8f1e0b8ace3fdea7c8bb38c61281
|
Syntax/Context.agda
|
Syntax/Context.agda
|
module Syntax.Context
{Type : Set}
where
-- CONTEXTS
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Specialized congruence rules
⟨∅⟩ : ∅ ≡ ∅
⟨∅⟩ = refl
_⟨•⟩_ : ∀ {τ₁ τ₂ Γ₁ Γ₂} → τ₁ ≡ τ₂ → Γ₁ ≡ Γ₂ → τ₁ • Γ₁ ≡ τ₂ • Γ₂
_⟨•⟩_ = cong₂ _•_
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- WEAKENING
-- CONTEXT PREFIXES
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂)
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- SUBCONTEXTS
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
module Subcontexts where
infix 8 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
lift (keep τ • ≼₁) this = this
lift (keep τ • ≼₁) (that x) = that (lift ≼₁ x)
lift (drop τ • ≼₁) x = that (lift ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
module Syntax.Context
{Type : Set}
where
-- CONTEXTS
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Specialized congruence rules
⟨∅⟩ : ∅ ≡ ∅
⟨∅⟩ = refl
_⟨•⟩_ : ∀ {τ₁ τ₂ Γ₁ Γ₂} → τ₁ ≡ τ₂ → Γ₁ ≡ Γ₂ → τ₁ • Γ₁ ≡ τ₂ • Γ₂
_⟨•⟩_ = cong₂ _•_
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ
-- WEAKENING
-- CONTEXT PREFIXES
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂)
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- SUBCONTEXTS
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
module Subcontexts where
infix 8 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
lift (keep τ • ≼₁) this = this
lift (keep τ • ≼₁) (that x) = that (lift ≼₁ x)
lift (drop τ • ≼₁) x = that (lift ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
rename type parameter of `that`
|
Agda: rename type parameter of `that`
Old-commit-hash: 0b30e5858214cb3ecab7a1661b7a2a79ac6bc871
|
Agda
|
mit
|
inc-lc/ilc-agda
|
cd132a2ada356a76c361341940070bcfccc2c571
|
lib/Explore/Zero.agda
|
lib/Explore/Zero.agda
|
open import Type
open import Type.Identities
open import Level.NP
open import Explore.Core
open import Explore.Properties
open import Explore.Explorable
open import Data.Zero
open import Function.NP
open import Function.Extensionality
open import Data.Product
open import Relation.Binary.PropositionalEquality.NP using (_≡_; refl; _∙_; !_)
open import HoTT
open Equivalences
import Explore.Monad
module Explore.Zero where
module _ {ℓ} where
𝟘ᵉ : Explore ℓ 𝟘
𝟘ᵉ = empty-explore
{- or
𝟘ᵉ ε _ _ = ε
-}
𝟘ⁱ : ∀ {p} → ExploreInd p 𝟘ᵉ
𝟘ⁱ = empty-explore-ind
{- or
𝟘ⁱ _ Pε _ _ = Pε
-}
module _ {ℓ₁ ℓ₂ ℓᵣ} {R : 𝟘 → 𝟘 → ★₀} where
⟦𝟘ᵉ⟧ : ⟦Explore⟧ᵤ ℓ₁ ℓ₂ ℓᵣ R 𝟘ᵉ 𝟘ᵉ
⟦𝟘ᵉ⟧ _ εᵣ _ _ = εᵣ
open Explorable₀ 𝟘ⁱ public using () renaming (sum to 𝟘ˢ; product to 𝟘ᵖ)
module _ {ℓ} where
open Explorableₛ {ℓ} 𝟘ⁱ public using () renaming (reify to 𝟘ʳ)
open Explorableₛₛ {ℓ} 𝟘ⁱ public using () renaming (unfocus to 𝟘ᵘ)
𝟘ˡ : Lookup {ℓ} 𝟘ᵉ
𝟘ˡ _ ()
𝟘ᶠ : Focus {ℓ} 𝟘ᵉ
𝟘ᶠ ((), _)
module _ {{_ : UA}} where
Σᵉ𝟘-ok : Adequate-Σᵉ {ℓ} 𝟘ᵉ
Σᵉ𝟘-ok _ = ! Σ𝟘-lift∘fst
module _ {{_ : UA}}{{_ : FunExt}} where
Πᵉ𝟘-ok : Adequate-Πᵉ {ℓ} 𝟘ᵉ
Πᵉ𝟘-ok _ = ! Π𝟘-uniq _
module _ {{_ : UA}} where
𝟘ˢ-ok : Adequate-sum 𝟘ˢ
𝟘ˢ-ok _ = Fin0≡𝟘 ∙ ! Σ𝟘-fst
module _ {{_ : UA}}{{_ : FunExt}} where
𝟘ᵖ-ok : Adequate-product 𝟘ᵖ
𝟘ᵖ-ok _ = Fin1≡𝟙 ∙ ! (Π𝟘-uniq₀ _)
explore𝟘 = 𝟘ᵉ
explore𝟘-ind = 𝟘ⁱ
lookup𝟘 = 𝟘ˡ
reify𝟘 = 𝟘ʳ
focus𝟘 = 𝟘ᶠ
unfocus𝟘 = 𝟘ᵘ
sum𝟘 = 𝟘ˢ
adequate-sum𝟘 = 𝟘ˢ-ok
product𝟘 = 𝟘ᵖ
adequate-product𝟘 = 𝟘ᵖ-ok
-- DEPRECATED
module _ {{_ : UA}} where
open ExplorableRecord
μ𝟘 : Explorable 𝟘
μ𝟘 = mk _ 𝟘ⁱ 𝟘ˢ-ok
-- -}
|
open import Type
open import Type.Identities
open import Level.NP
open import Explore.Core
open import Explore.Properties
open import Explore.Explorable
open import Data.Zero
open import Function.NP
open import Function.Extensionality
open import Data.Product
open import Relation.Binary.PropositionalEquality.NP using (_≡_; refl; _∙_; !_)
open import HoTT
open Equivalences
import Explore.Monad
open import Explore.Isomorphism
module Explore.Zero where
module _ {ℓ} where
𝟘ᵉ : Explore ℓ 𝟘
𝟘ᵉ = empty-explore
{- or
𝟘ᵉ ε _ _ = ε
-}
𝟘ⁱ : ∀ {p} → ExploreInd p 𝟘ᵉ
𝟘ⁱ = empty-explore-ind
{- or
𝟘ⁱ _ Pε _ _ = Pε
-}
module _ {ℓ₁ ℓ₂ ℓᵣ} {R : 𝟘 → 𝟘 → ★₀} where
⟦𝟘ᵉ⟧ : ⟦Explore⟧ᵤ ℓ₁ ℓ₂ ℓᵣ R 𝟘ᵉ 𝟘ᵉ
⟦𝟘ᵉ⟧ _ εᵣ _ _ = εᵣ
open Explorable₀ 𝟘ⁱ public using () renaming (sum to 𝟘ˢ; product to 𝟘ᵖ)
module _ {ℓ} where
open Explorableₛ {ℓ} 𝟘ⁱ public using () renaming (reify to 𝟘ʳ)
open Explorableₛₛ {ℓ} 𝟘ⁱ public using () renaming (unfocus to 𝟘ᵘ)
𝟘ˡ : Lookup {ℓ} 𝟘ᵉ
𝟘ˡ _ ()
𝟘ᶠ : Focus {ℓ} 𝟘ᵉ
𝟘ᶠ ((), _)
module _ {{_ : UA}} where
Σᵉ𝟘-ok : Adequate-Σᵉ {ℓ} 𝟘ᵉ
Σᵉ𝟘-ok _ = ! Σ𝟘-lift∘fst
module _ {{_ : UA}}{{_ : FunExt}} where
Πᵉ𝟘-ok : Adequate-Πᵉ {ℓ} 𝟘ᵉ
Πᵉ𝟘-ok _ = ! Π𝟘-uniq _
module _ {{_ : UA}} where
𝟘ˢ-ok : Adequate-sum 𝟘ˢ
𝟘ˢ-ok _ = Fin0≡𝟘 ∙ ! Σ𝟘-fst
module _ {{_ : UA}}{{_ : FunExt}} where
𝟘ᵖ-ok : Adequate-product 𝟘ᵖ
𝟘ᵖ-ok _ = Fin1≡𝟙 ∙ ! (Π𝟘-uniq₀ _)
explore𝟘 = 𝟘ᵉ
explore𝟘-ind = 𝟘ⁱ
lookup𝟘 = 𝟘ˡ
reify𝟘 = 𝟘ʳ
focus𝟘 = 𝟘ᶠ
unfocus𝟘 = 𝟘ᵘ
sum𝟘 = 𝟘ˢ
adequate-sum𝟘 = 𝟘ˢ-ok
product𝟘 = 𝟘ᵖ
adequate-product𝟘 = 𝟘ᵖ-ok
Lift𝟘ᵉ : ∀ {m} → Explore m (Lift 𝟘)
Lift𝟘ᵉ = explore-iso (≃-sym Lift≃id) 𝟘ᵉ
ΣᵉLift𝟘-ok : ∀ {{_ : UA}}{{_ : FunExt}}{m} → Adequate-Σᵉ {m} Lift𝟘ᵉ
ΣᵉLift𝟘-ok = Σ-iso-ok (≃-sym Lift≃id) {Aᵉ = 𝟘ᵉ} Σᵉ𝟘-ok
-- DEPRECATED
module _ {{_ : UA}} where
open ExplorableRecord
μ𝟘 : Explorable 𝟘
μ𝟘 = mk _ 𝟘ⁱ 𝟘ˢ-ok
-- -}
|
add explore function for Lift 𝟘
|
add explore function for Lift 𝟘
|
Agda
|
bsd-3-clause
|
crypto-agda/explore
|
8aefca99408c27e4a214ce4b0721a4e4794eeb87
|
Atlas/Change/Derivation.agda
|
Atlas/Change/Derivation.agda
|
module Atlas.Change.Derivation where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
open import Atlas.Change.Term
open import Base.Syntax.Context Type
open import Base.Change.Context ΔType
Atlas-Δconst : ∀ {Γ Σ τ} → (c : Const Σ τ) →
Term Γ (internalizeContext (ΔContext′ Σ) (ΔType τ))
Atlas-Δconst true = false!
Atlas-Δconst false = false!
Atlas-Δconst xor = abs₄ (λ x Δx y Δy → xor! Δx Δy)
Atlas-Δconst empty = empty!
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
Atlas-Δconst update = abs₆ (λ k Δk v Δv m Δm →
insert (apply Δk k) (apply Δv v) (delete k v Δm))
-- Δlookup k Δk m Δm | true? (k ⊕ Δk ≡ k)
-- ... | true = lookup k Δm
-- ... | false =
-- (lookup (k ⊕ Δk) m ⊕ lookup (k ⊕ Δk) Δm)
-- ⊝ lookup k m
--
-- Only the false-branch is implemented.
Atlas-Δconst lookup = abs₄ (λ k Δk m Δm →
let
k′ = apply Δk k
in
(diff (apply {base _} (lookup! k′ Δm) (lookup! k′ m))
(lookup! k m)))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
Atlas-Δconst zip = abs₆ (λ f Δf m₁ Δm₁ m₂ Δm₂ →
let
g = abs (app₂ (weaken₁ Δf) (var this) nil-term)
in
zip4! g m₁ Δm₁ m₂ Δm₂)
-- Δfold f Δf z Δz m Δm = proj₂
-- (fold (λ k [a,Δa] [b,Δb] →
-- uncurry (λ a Δa → uncurry (λ b Δb →
-- pair (f k a b) (Δf k nil a Δa b Δb))
-- [b,Δb]) [a,Δa])
-- (pair z Δz)
-- (zip-pair m Δm))
--
-- Δfold is efficient only if evaluation is lazy and Δf is
-- self-maintainable: it doesn't look at the argument
-- (b = fold f k a b₀) at all.
Atlas-Δconst (fold {κ} {α} {β}) =
let -- TODO (tedius): write weaken₇
f = weaken₃ (weaken₃ (weaken₁
(var (that (that (that (that (that this))))))))
Δf = weaken₃ (weaken₃ (weaken₁
(var (that (that (that (that this)))))))
z = var (that (that (that this)))
Δz = var (that (that this))
m = var (that this)
Δm = var this
k = weaken₃ (weaken₁ (var (that (that this))))
[a,Δa] = var (that this)
[b,Δb] = var this
a = var (that (that (that this)))
Δa = var (that (that this))
b = var (that this)
Δb = var this
g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs
(pair (app₃ f k a b)
(app₆ Δf k nil-term a Δa b Δb))))
(weaken₂ [b,Δb])))) [a,Δa])))
proj₂ = uncurry (abs (abs (var this)))
in
abs (abs (abs (abs (abs (abs
(proj₂ (fold! g (pair z Δz) (zip-pair m Δm))))))))
|
module Atlas.Change.Derivation where
open import Atlas.Syntax.Type
open import Atlas.Syntax.Term
open import Atlas.Change.Type
open import Atlas.Change.Term
open import Base.Syntax.Context Type
open import Base.Change.Context ΔType
ΔConst : ∀ {Γ Σ τ} → (c : Const Σ τ) →
Term Γ (internalizeContext (ΔContext′ Σ) (ΔType τ))
ΔConst true = false!
ΔConst false = false!
ΔConst xor = abs₄ (λ x Δx y Δy → xor! Δx Δy)
ΔConst empty = empty!
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
ΔConst update = abs₆ (λ k Δk v Δv m Δm →
insert (apply Δk k) (apply Δv v) (delete k v Δm))
-- Δlookup k Δk m Δm | true? (k ⊕ Δk ≡ k)
-- ... | true = lookup k Δm
-- ... | false =
-- (lookup (k ⊕ Δk) m ⊕ lookup (k ⊕ Δk) Δm)
-- ⊝ lookup k m
--
-- Only the false-branch is implemented.
ΔConst lookup = abs₄ (λ k Δk m Δm →
let
k′ = apply Δk k
in
(diff (apply {base _} (lookup! k′ Δm) (lookup! k′ m))
(lookup! k m)))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
ΔConst zip = abs₆ (λ f Δf m₁ Δm₁ m₂ Δm₂ →
let
g = abs (app₂ (weaken₁ Δf) (var this) nil-term)
in
zip4! g m₁ Δm₁ m₂ Δm₂)
-- Δfold f Δf z Δz m Δm = proj₂
-- (fold (λ k [a,Δa] [b,Δb] →
-- uncurry (λ a Δa → uncurry (λ b Δb →
-- pair (f k a b) (Δf k nil a Δa b Δb))
-- [b,Δb]) [a,Δa])
-- (pair z Δz)
-- (zip-pair m Δm))
--
-- Δfold is efficient only if evaluation is lazy and Δf is
-- self-maintainable: it doesn't look at the argument
-- (b = fold f k a b₀) at all.
ΔConst (fold {κ} {α} {β}) =
let -- TODO (tedius): write weaken₇
f = weaken₃ (weaken₃ (weaken₁
(var (that (that (that (that (that this))))))))
Δf = weaken₃ (weaken₃ (weaken₁
(var (that (that (that (that this)))))))
z = var (that (that (that this)))
Δz = var (that (that this))
m = var (that this)
Δm = var this
k = weaken₃ (weaken₁ (var (that (that this))))
[a,Δa] = var (that this)
[b,Δb] = var this
a = var (that (that (that this)))
Δa = var (that (that this))
b = var (that this)
Δb = var this
g = abs (abs (abs (uncurry (abs (abs (uncurry (abs (abs
(pair (app₃ f k a b)
(app₆ Δf k nil-term a Δa b Δb))))
(weaken₂ [b,Δb])))) [a,Δa])))
proj₂ = uncurry (abs (abs (var this)))
in
abs (abs (abs (abs (abs (abs
(proj₂ (fold! g (pair z Δz) (zip-pair m Δm))))))))
|
Rename Atlas-Δconst to ΔConst.
|
Rename Atlas-Δconst to ΔConst.
Old-commit-hash: e7ed19ec0ca363e486fe72ac858facfcd192e057
|
Agda
|
mit
|
inc-lc/ilc-agda
|
af1d5371ab683fac0cfee48a20cabc40e4da7d41
|
program-distance.agda
|
program-distance.agda
|
module program-distance where
open import flipbased-implem
open import Data.Bits
open import Data.Bool
open import Data.Vec.NP using (Vec; count; countᶠ)
open import Data.Nat.NP
open import Data.Nat.Properties
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
import Data.Fin as Fin
record PrgDist : Set₁ where
constructor mk
field
_]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set
]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f
]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h
]-[-cong-right-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ]-[ g → g ≈⅁ h → f ]-[ h
]-[-cong-right-≈↺ pf pf' = ]-[-sym (]-[-cong-left-≈↺ (sym pf') (]-[-sym pf))
]-[-cong-≗↺ : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g'
]-[-cong-≗↺ {c} {c'} {f} {g} {f'} {g'} f≗g f'≗g' pf
= ]-[-cong-left-≈↺ {f = g} {g = f} {h = g'}
((≗⇒≈⅁ (λ x → sym (f≗g x)))) (]-[-cong-right-≈↺ pf (≗⇒≈⅁ f'≗g'))
breaks : ∀ {c} (EXP : Bit → ⅁ c) → Set
breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b
-- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁
_⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ⅁ c₀) (⅁₁ : Bit → ⅁ c₁) → Set
⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁
extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ⅁ c}
→ ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁
extensional-reduction same-games = ]-[-cong-≗↺ (same-games 0b) (same-games 1b)
module HomImplem k where
-- | Pr[ f ≡ 1 ] - Pr[ g ≡ 1 ] | ≥ ε [ on reals ]
-- dist Pr[ f ≡ 1 ] Pr[ g ≡ 1 ] ≥ ε [ on reals ]
-- dist (#1 f / 2^ c) (#1 g / 2^ c) ≥ ε [ on reals ]
-- dist (#1 f) (#1 g) ≥ ε * 2^ c where ε = 2^ -k [ on rationals ]
-- dist (#1 f) (#1 g) ≥ 2^(-k) * 2^ c [ on rationals ]
-- dist (#1 f) (#1 g) ≥ 2^(c - k) [ on rationals ]
-- dist (#1 f) (#1 g) ≥ 2^(c ∸ k) [ on natural ]
_]-[_ : ∀ {n} (f g : ⅁ n) → Set
_]-[_ {n} f g = dist (count↺ f) (count↺ g) ≥ 2^(n ∸ k)
]-[-sym : ∀ {n} {f g : ⅁ n} → f ]-[ g → g ]-[ f
]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count↺ f) (count↺ g) = f]-[g
]-[-cong-left-≈↺ : ∀ {n} {f g h : ⅁ n} → f ≈⅁ g → g ]-[ h → f ]-[ h
]-[-cong-left-≈↺ {n} {f} {g} f≈g g]-[h rewrite ≈⅁⇒≈⅁′ {n} {f} {g} f≈g = g]-[h
-- dist #g #h ≥ 2^(n ∸ k)
-- dist #f #h ≥ 2^(n ∸ k)
module Implem k where
_]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set
_]-[_ {m} {n} f g = dist (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) ≥ 2^((m + n) ∸ k)
]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f
]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) | ℕ°.+-comm m n = f]-[g
postulate
helper : ∀ x y z t → x + ((y + z) ∸ t) ≡ y + ((x + z) ∸ t)
]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h
]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} f≈g g]-[h
with 2^*-mono m g]-[h
... | q rewrite sym (dist-2^* m (⟨2^ o ⟩* (count↺ g)) (⟨2^ n ⟩* (count↺ h)))
| 2^-comm m o (count↺ g)
| sym f≈g
| 2^-comm o n (count↺ f)
| 2^-comm m n (count↺ h)
| dist-2^* n (⟨2^ o ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ h))
| 2^-+ m (n + o ∸ k) 1
| helper m n o k
| sym (2^-+ n (m + o ∸ k) 1)
= 2^*-mono′ n q
prgDist : PrgDist
prgDist = mk _]-[_ (λ {m n f g} x → ]-[-sym {m} {n} {f} {g} x) (λ {m n o f g h} x y → ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} x y)
|
module program-distance where
open import flipbased-implem
open import Data.Bits
open import Data.Bool
open import Data.Vec.NP using (Vec; count; countᶠ)
open import Data.Nat.NP
open import Data.Nat.Properties
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
import Data.Fin as Fin
record PrgDist : Set₁ where
constructor mk
field
_]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set
]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f
]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h
]-[-cong-right-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ]-[ g → g ≈⅁ h → f ]-[ h
]-[-cong-right-≈↺ pf pf' = ]-[-sym (]-[-cong-left-≈↺ (sym pf') (]-[-sym pf))
]-[-cong-≗↺ : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ]-[ f' → g ]-[ g'
]-[-cong-≗↺ {c} {c'} {f} {g} {f'} {g'} f≗g f'≗g' pf
= ]-[-cong-left-≈↺ {f = g} {g = f} {h = g'}
((≗⇒≈⅁ (λ x → sym (f≗g x)))) (]-[-cong-right-≈↺ pf (≗⇒≈⅁ f'≗g'))
breaks : ∀ {c} (EXP : Bit → ⅁ c) → Set
breaks ⅁ = ⅁ 0b ]-[ ⅁ 1b
-- An wining adversary for game ⅁₀ reduces to a wining adversary for game ⅁₁
_⇓_ : ∀ {c₀ c₁} (⅁₀ : Bit → ⅁ c₀) (⅁₁ : Bit → ⅁ c₁) → Set
⅁₀ ⇓ ⅁₁ = breaks ⅁₀ → breaks ⅁₁
extensional-reduction : ∀ {c} {⅁₀ ⅁₁ : Bit → ⅁ c}
→ ⅁₀ ≗⅁ ⅁₁ → ⅁₀ ⇓ ⅁₁
extensional-reduction same-games = ]-[-cong-≗↺ (same-games 0b) (same-games 1b)
module HomImplem k where
-- | Pr[ f ≡ 1 ] - Pr[ g ≡ 1 ] | ≥ ε [ on reals ]
-- dist Pr[ f ≡ 1 ] Pr[ g ≡ 1 ] ≥ ε [ on reals ]
-- dist (#1 f / 2^ c) (#1 g / 2^ c) ≥ ε [ on reals ]
-- dist (#1 f) (#1 g) ≥ ε * 2^ c where ε = 2^ -k [ on rationals ]
-- dist (#1 f) (#1 g) ≥ 2^(-k) * 2^ c [ on rationals ]
-- dist (#1 f) (#1 g) ≥ 2^(c - k) [ on rationals ]
-- dist (#1 f) (#1 g) ≥ 2^(c ∸ k) [ on natural ]
_]-[_ : ∀ {n} (f g : ⅁ n) → Set
_]-[_ {n} f g = dist (count↺ f) (count↺ g) ≥ 2^(n ∸ k)
]-[-sym : ∀ {n} {f g : ⅁ n} → f ]-[ g → g ]-[ f
]-[-sym {n} {f} {g} f]-[g rewrite dist-sym (count↺ f) (count↺ g) = f]-[g
]-[-cong-left-≈↺ : ∀ {n} {f g h : ⅁ n} → f ≈⅁ g → g ]-[ h → f ]-[ h
]-[-cong-left-≈↺ {n} {f} {g} f≈g g]-[h rewrite ≈⅁⇒≈⅁′ {n} {f} {g} f≈g = g]-[h
-- dist #g #h ≥ 2^(n ∸ k)
-- dist #f #h ≥ 2^(n ∸ k)
module Implem k where
_]-[_ : ∀ {m n} → ⅁ m → ⅁ n → Set
_]-[_ {m} {n} f g = dist (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) ≥ 2^((m + n) ∸ k)
]-[-sym : ∀ {m n} {f : ⅁ m} {g : ⅁ n} → f ]-[ g → g ]-[ f
]-[-sym {m} {n} {f} {g} f]-[g rewrite dist-sym (⟨2^ n ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ g)) | ℕ°.+-comm m n = f]-[g
postulate
helper : ∀ x y z t → x + ((y + z) ∸ t) ≡ y + ((x + z) ∸ t)
]-[-cong-left-≈↺ : ∀ {m n o} {f : ⅁ m} {g : ⅁ n} {h : ⅁ o} → f ≈⅁ g → g ]-[ h → f ]-[ h
]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} f≈g g]-[h
with 2^*-mono m g]-[h
-- 2ᵐ(dist 2ᵒ#g 2ⁿ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ
... | q rewrite sym (dist-2^* m (⟨2^ o ⟩* (count↺ g)) (⟨2^ n ⟩* (count↺ h)))
-- dist 2ᵐ2ᵒ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ
| 2^-comm m o (count↺ g)
-- dist 2ᵒ2ᵐ#g 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ
| sym f≈g
-- dist 2ᵒ2ⁿ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ
| 2^-comm o n (count↺ f)
-- dist 2ⁿ2ᵒ#f 2ᵐ2ⁿ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ
| 2^-comm m n (count↺ h)
-- dist 2ⁿ2ᵒ#f 2ⁿ2ᵐ#h ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ
| dist-2^* n (⟨2^ o ⟩* (count↺ f)) (⟨2^ m ⟩* (count↺ h))
-- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ2ⁿ⁺ᵒ⁻ᵏ
| 2^-+ m (n + o ∸ k) 1
-- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ᵐ⁺ⁿ⁺ᵒ⁻ᵏ
| helper m n o k
-- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ⁺ᵐ⁺ᵒ⁻ᵏ
| sym (2^-+ n (m + o ∸ k) 1)
-- 2ⁿ(dist 2ᵒ#f 2ᵐ#h) ≤ 2ⁿ2ᵐ⁺ᵒ⁻ᵏ
= 2^*-mono′ n q
-- dist 2ᵒ#f 2ᵐ#h ≤ 2ᵐ⁺ᵒ⁻ᵏ
prgDist : PrgDist
prgDist = mk _]-[_ (λ {m n f g} x → ]-[-sym {m} {n} {f} {g} x) (λ {m n o f g h} x y → ]-[-cong-left-≈↺ {m} {n} {o} {f} {g} {h} x y)
|
document a broken (so far) proof
|
document a broken (so far) proof
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
d0df7845258df353bcf850c309c4a494f9869248
|
Parametric/Denotation/CachingEvaluation.agda
|
Parametric/Denotation/CachingEvaluation.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Caching evaluation
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
module Structure where
{-
-- Prototype here the type-correctness of a simple non-standard semantics.
-- This describes a simplified version of the transformation by Liu and
-- Tanenbaum, PEPM 1995 - but for now, instead of producing object language
-- terms, we produce host language terms to take advantage of the richer type
-- system of the host language (in particular, here we need the unit type,
-- product types and *existentials*).
--
-- As usual, we'll later switch to a real term transformation.
-}
open import Data.Product hiding (map)
open import Data.Sum hiding (map)
open import Data.Unit
open import Level
-- Type semantics for this scenario.
⟦_⟧TypeHidCache : (τ : Type) → Set₁
⟦ base ι ⟧TypeHidCache = Lift ⟦ base ι ⟧
⟦ σ ⇒ τ ⟧TypeHidCache = ⟦ σ ⟧TypeHidCache → (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧TypeHidCache × τ₁ )
open import Parametric.Syntax.MType as MType
open MType.Structure Base
open import Parametric.Syntax.MTerm as MTerm
open MTerm.Structure Const
open import Function hiding (const)
⟦_⟧ValType : (τ : ValType) → Set
⟦_⟧CompType : (τ : CompType) → Set
⟦ U c ⟧ValType = ⟦ c ⟧CompType
⟦ B ι ⟧ValType = ⟦ base ι ⟧
⟦ vUnit ⟧ValType = ⊤
⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType
⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType
⟦ F τ ⟧CompType = ⟦ τ ⟧ValType
⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType
-- XXX: below we need to override just a few cases. Inheritance would handle
-- this precisely; without inheritance, we might want to use one of the
-- standard encodings of related features (delegation?).
⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁
⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁
-- This line is the only change, up to now, for the caching semantics starting from CBPV.
⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ )
-- Delegation upward.
⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType
⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType
-- The above does not override what happens in recursive occurrences.
{-# NO_TERMINATION_CHECK #-}
⟦_⟧ValTypeHidCache : (τ : ValType) → Set
⟦_⟧CompTypeHidCache : (τ : CompType) → Set
⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache
⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧
⟦ vUnit ⟧ValTypeHidCache = ⊤
⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache
⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache
-- This line is the only change, up to now, for the caching semantics.
--
-- XXX The termination checker isn't happy with it, and it may be right ─ if
-- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big.
-- I think we don't do that unless we are caching a non-terminating
-- computation to begin with, but I'm not entirely sure.
--
-- However, the termination checker can't prove that the function is
-- terminating because it's not structurally recursive, but one call of the
-- function will produce another call of the function stuck on a neutral term:
-- So the computation will have terminated, just in an unusual way!
--
-- Anyway, I need not mechanize this part of the proof for my goals.
⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache )
⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧CtxHidCache : (Γ : Context) → Set₁
⟦_⟧CtxHidCache = DependentList ⟦_⟧TypeHidCache
⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set
⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache
{-
⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁
⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache
-}
-- It's questionable that this is not polymorphic enough.
⟦_⟧VarHidCache : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache
⟦ this ⟧VarHidCache (v • ρ) = v
⟦ that x ⟧VarHidCache (v • ρ) = ⟦ x ⟧VarHidCache ρ
-- Now, let us define a caching semantics for terms.
-- This proves to be hard, because we need to insert and remove caches where
-- we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
--
-- Inserting and removing caches --
--
-- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time.
-- Indeed, both functions seem structurally recursive on τ.
dropCache : ∀ {τ} → ⟦ τ ⟧TypeHidCache → ⟦ τ ⟧
extend : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧TypeHidCache
dropCache {base ι} v = lower v
dropCache {σ ⇒ τ} v x = dropCache (proj₁ (proj₂ (v (extend x))))
extend {base ι} v = lift v
extend {σ ⇒ τ} v = λ x → , (extend (v (dropCache x)) , tt)
-- OK, this version is syntax-directed, luckily enough, except on primitives
-- (as expected). This reveals a bug of ours on higher-order primitives.
--
-- Moreover, we can somewhat safely assume that each call to extend and to
-- dropCache is bad: then we see that the handling of constants is bad. That's
-- correct, because constants will not return intermediate results in this
-- schema :-(.
⟦_⟧TermCache : ∀ {τ Γ} → Term Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache
⟦_⟧TermCache (const c args) ρ = extend (⟦ const c args ⟧ (map dropCache ρ))
⟦_⟧TermCache (var x) ρ = ⟦ x ⟧VarHidCache ρ
-- It seems odd (a probable bug?) that the result of t needn't be stripped of
-- its cache.
⟦_⟧TermCache (app s t) ρ = proj₁ (proj₂ ((⟦ s ⟧TermCache ρ) (⟦ t ⟧TermCache ρ)))
-- Provide an empty cache!
⟦_⟧TermCache (abs t) ρ x = , (⟦ t ⟧TermCache (x • ρ) , tt)
-- The solution is to distinguish among different kinds of constants. Some are
-- value constructors (and thus do not return caches), while others are
-- computation constructors (and thus should return caches). For products, I
-- believe we will only use the products which are values, not computations
-- (XXX check CBPV paper for the name).
⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVar)
-- This says that the environment does not contain caches... sounds wrong!
-- Either we add extra variables for the caches, or we store computations in
-- the environment (but that does not make sense), or we store caches in
-- values, by acting not on F but on something else (U?).
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- The real deal, finally.
open import UNDEFINED
-- XXX constants are still a slight mess because I'm abusing CBPV...
-- (Actually, I just forgot the difference, and believe I had too little clue
-- when I wrote these constructors... but some of them did make sense).
⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED
-- Also, where are introduction forms for pairs and sums among values? With
-- them, we should see that we can interpret them without adding a cache.
-- Thunks keep seeming noops.
⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ
-- Here, in an actual implementation, we would return the actual cache with
-- all variables.
--
-- The effect of F is a writer monad of cached values, where the monoid is
-- (isomorphic to) the free monoid over (∃ τ . τ), but we push the
-- existentials up when pairing things!
-- That's what we're interpreting computations in. XXX maybe consider using
-- monads directly. But that doesn't deal with arity.
⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt)
-- For this to be enough, a lambda needs to return a produce, not to forward
-- the underlying one (unless there are no intermediate results). The correct
-- requirements can maybe be enforced through a linear typing discipline.
{-
-- Here we'd have a problem with the original into from CBPV, because it does
-- not require converting expressions to the "CBPV A-normal form".
⟦_⟧CompTermCache (v₁ into v₂) ρ =
-- Sequence commands and combine their caches.
{-
let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in , (r₂ , (c₁ ,′ c₂))
-}
-- The code above does not work, because we only guarantee that v₂ is
-- a computation, not that it's an F-computation - v₂ could also be function
-- type or a computation product.
-- We need a restricted CBPV, where these two possibilities are forbidden. If
-- you want to save something between different lambdas, you need to add an F
-- U to reflect that. (Double-check the papers which show how to encode arity
-- using CBPV, it seems that they should find the same problem --- XXX they
-- don't).
-- Instead, just drop the first cache (XXX WRONG).
⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ)
-}
-- But if we alter _into_ as described above, composing the caches works!
-- However, we should not forget we also need to save the new intermediate
-- result, that is the one produced by the first part of the let.
⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ =
let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂))
-- Note the compositionality and luck: we don't need to do anything at the
-- cReturn time, we just need the nested into to do their job, because as I
-- said intermediate results are a writer monad.
--
-- Q: But then, do we still need to do all the other stuff? IOW, do we still
-- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <-
-- f args; z <- g args'; z)?
--
-- A: One thing we still need is using the monadic version of into for the
-- same reasons - which makes sense, since it has the type of monadic bind.
--
-- Maybe not: if we use a monad, which respects left and right identity, the
-- two above forms are equivalent. But what about associativity? We don't have
-- associativity with nested tuples in the middle. That's why the monad uses
-- lists! We can also use nested tuple, as long as in the into case we don't
-- do (a, b) but append a b (ahem, where?), which decomposes the first list
-- and prepends it to the second. To this end, we need to know the type of the
-- first element, or to ensure it's always a pair. Maybe we just want to reuse
-- an HList.
-- In abstractions, we should start collecting all variables...
-- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache,
-- that's moved into the handling of cReturn. This makes *the* difference for
-- nested lambdas, where we don't need to create caches multiple times!
⟦_⟧CompTermCache (cAbs v) ρ x = ⟦ v ⟧CompTermCache (x • ρ)
-- Here we see that we are in a sort of A-normal form, because the argument is
-- a value (not quite ANF though, since values can be thunks - that is,
-- computations which haven't been run yet, I guess. Do we have an use for
-- that? That allows passing lambdas as arguments directly - which is fine,
-- because producing a closure indeed does not have intermediate results!).
⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Caching evaluation
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
module Structure where
{-
-- Prototype here the type-correctness of a simple non-standard semantics.
-- This describes a simplified version of the transformation by Liu and
-- Tanenbaum, PEPM 1995 - but for now, instead of producing object language
-- terms, we produce host language terms to take advantage of the richer type
-- system of the host language (in particular, here we need the unit type,
-- product types and *existentials*).
--
-- As usual, we'll later switch to a real term transformation.
-}
open import Data.Product hiding (map)
open import Data.Sum hiding (map)
open import Data.Unit
open import Level
-- Type semantics for this scenario.
⟦_⟧TypeHidCache : (τ : Type) → Set₁
⟦ base ι ⟧TypeHidCache = Lift ⟦ base ι ⟧
⟦ σ ⇒ τ ⟧TypeHidCache = ⟦ σ ⟧TypeHidCache → (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧TypeHidCache × τ₁ )
-- Now, let us define a caching semantics for terms.
-- This proves to be hard, because we need to insert and remove caches where
-- we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
--
-- Inserting and removing caches --
--
-- Implementation note: The mutual recursion looks like a fold on exponentials, where you need to define the function and the inverse at the same time.
-- Indeed, both functions seem structurally recursive on τ.
dropCache : ∀ {τ} → ⟦ τ ⟧TypeHidCache → ⟦ τ ⟧
extend : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧TypeHidCache
dropCache {base ι} v = lower v
dropCache {σ ⇒ τ} v x = dropCache (proj₁ (proj₂ (v (extend x))))
extend {base ι} v = lift v
extend {σ ⇒ τ} v = λ x → , (extend (v (dropCache x)) , tt)
⟦_⟧CtxHidCache : (Γ : Context) → Set₁
⟦_⟧CtxHidCache = DependentList ⟦_⟧TypeHidCache
-- It's questionable that this is not polymorphic enough.
⟦_⟧VarHidCache : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache
⟦ this ⟧VarHidCache (v • ρ) = v
⟦ that x ⟧VarHidCache (v • ρ) = ⟦ x ⟧VarHidCache ρ
-- OK, this version is syntax-directed, luckily enough, except on primitives
-- (as expected). This reveals a bug of ours on higher-order primitives.
--
-- Moreover, we can somewhat safely assume that each call to extend and to
-- dropCache is bad: then we see that the handling of constants is bad. That's
-- correct, because constants will not return intermediate results in this
-- schema :-(.
⟦_⟧TermCache : ∀ {τ Γ} → Term Γ τ → ⟦ Γ ⟧CtxHidCache → ⟦ τ ⟧TypeHidCache
⟦_⟧TermCache (const c args) ρ = extend (⟦ const c args ⟧ (map dropCache ρ))
⟦_⟧TermCache (var x) ρ = ⟦ x ⟧VarHidCache ρ
-- It seems odd (a probable bug?) that the result of t needn't be stripped of
-- its cache.
⟦_⟧TermCache (app s t) ρ = proj₁ (proj₂ ((⟦ s ⟧TermCache ρ) (⟦ t ⟧TermCache ρ)))
-- Provide an empty cache!
⟦_⟧TermCache (abs t) ρ x = , (⟦ t ⟧TermCache (x • ρ) , tt)
open import Parametric.Syntax.MType as MType
open MType.Structure Base
open import Parametric.Syntax.MTerm as MTerm
open MTerm.Structure Const
open import Function hiding (const)
⟦_⟧ValType : (τ : ValType) → Set
⟦_⟧CompType : (τ : CompType) → Set
⟦ U c ⟧ValType = ⟦ c ⟧CompType
⟦ B ι ⟧ValType = ⟦ base ι ⟧
⟦ vUnit ⟧ValType = ⊤
⟦ τ₁ v× τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType × ⟦ τ₂ ⟧ValType
⟦ τ₁ v+ τ₂ ⟧ValType = ⟦ τ₁ ⟧ValType ⊎ ⟦ τ₂ ⟧ValType
⟦ F τ ⟧CompType = ⟦ τ ⟧ValType
⟦ σ ⇛ τ ⟧CompType = ⟦ σ ⟧ValType → ⟦ τ ⟧CompType
-- XXX: below we need to override just a few cases. Inheritance would handle
-- this precisely; without inheritance, we might want to use one of the
-- standard encodings of related features (delegation?).
⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁
⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁
-- This line is the only change, up to now, for the caching semantics starting from CBPV.
⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ Set ] ⟦ τ ⟧ValTypeHidCacheWrong × τ₁ )
-- Delegation upward.
⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType
⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType
-- The above does not override what happens in recursive occurrences.
{-# NO_TERMINATION_CHECK #-}
⟦_⟧ValTypeHidCache : (τ : ValType) → Set
⟦_⟧CompTypeHidCache : (τ : CompType) → Set
⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache
⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧
⟦ vUnit ⟧ValTypeHidCache = ⊤
⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache
⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache
-- This line is the only change, up to now, for the caching semantics.
--
-- XXX The termination checker isn't happy with it, and it may be right ─ if
-- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big.
-- I think we don't do that unless we are caching a non-terminating
-- computation to begin with, but I'm not entirely sure.
--
-- However, the termination checker can't prove that the function is
-- terminating because it's not structurally recursive, but one call of the
-- function will produce another call of the function stuck on a neutral term:
-- So the computation will have terminated, just in an unusual way!
--
-- Anyway, I need not mechanize this part of the proof for my goals.
⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache )
⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set
⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache
{-
⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁
⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache
-}
-- The solution is to distinguish among different kinds of constants. Some are
-- value constructors (and thus do not return caches), while others are
-- computation constructors (and thus should return caches). For products, I
-- believe we will only use the products which are values, not computations
-- (XXX check CBPV paper for the name).
⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVar)
-- This says that the environment does not contain caches... sounds wrong!
-- Either we add extra variables for the caches, or we store computations in
-- the environment (but that does not make sense), or we store caches in
-- values, by acting not on F but on something else (U?).
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVar ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- The real deal, finally.
open import UNDEFINED
-- XXX constants are still a slight mess because I'm abusing CBPV...
-- (Actually, I just forgot the difference, and believe I had too little clue
-- when I wrote these constructors... but some of them did make sense).
⟦_⟧CompTermCache (cConst c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstV c args) ρ = reveal UNDEFINED
⟦_⟧CompTermCache (cConstV2 c args) ρ = reveal UNDEFINED
-- Also, where are introduction forms for pairs and sums among values? With
-- them, we should see that we can interpret them without adding a cache.
-- Thunks keep seeming noops.
⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ
-- Here, in an actual implementation, we would return the actual cache with
-- all variables.
--
-- The effect of F is a writer monad of cached values, where the monoid is
-- (isomorphic to) the free monoid over (∃ τ . τ), but we push the
-- existentials up when pairing things!
-- That's what we're interpreting computations in. XXX maybe consider using
-- monads directly. But that doesn't deal with arity.
⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt)
-- For this to be enough, a lambda needs to return a produce, not to forward
-- the underlying one (unless there are no intermediate results). The correct
-- requirements can maybe be enforced through a linear typing discipline.
{-
-- Here we'd have a problem with the original into from CBPV, because it does
-- not require converting expressions to the "CBPV A-normal form".
⟦_⟧CompTermCache (v₁ into v₂) ρ =
-- Sequence commands and combine their caches.
{-
let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in , (r₂ , (c₁ ,′ c₂))
-}
-- The code above does not work, because we only guarantee that v₂ is
-- a computation, not that it's an F-computation - v₂ could also be function
-- type or a computation product.
-- We need a restricted CBPV, where these two possibilities are forbidden. If
-- you want to save something between different lambdas, you need to add an F
-- U to reflect that. (Double-check the papers which show how to encode arity
-- using CBPV, it seems that they should find the same problem --- XXX they
-- don't).
-- Instead, just drop the first cache (XXX WRONG).
⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ)
-}
-- But if we alter _into_ as described above, composing the caches works!
-- However, we should not forget we also need to save the new intermediate
-- result, that is the one produced by the first part of the let.
⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ =
let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂))
-- Note the compositionality and luck: we don't need to do anything at the
-- cReturn time, we just need the nested into to do their job, because as I
-- said intermediate results are a writer monad.
--
-- Q: But then, do we still need to do all the other stuff? IOW, do we still
-- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <-
-- f args; z <- g args'; z)?
--
-- A: One thing we still need is using the monadic version of into for the
-- same reasons - which makes sense, since it has the type of monadic bind.
--
-- Maybe not: if we use a monad, which respects left and right identity, the
-- two above forms are equivalent. But what about associativity? We don't have
-- associativity with nested tuples in the middle. That's why the monad uses
-- lists! We can also use nested tuple, as long as in the into case we don't
-- do (a, b) but append a b (ahem, where?), which decomposes the first list
-- and prepends it to the second. To this end, we need to know the type of the
-- first element, or to ensure it's always a pair. Maybe we just want to reuse
-- an HList.
-- In abstractions, we should start collecting all variables...
-- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache,
-- that's moved into the handling of cReturn. This makes *the* difference for
-- nested lambdas, where we don't need to create caches multiple times!
⟦_⟧CompTermCache (cAbs v) ρ x = ⟦ v ⟧CompTermCache (x • ρ)
-- Here we see that we are in a sort of A-normal form, because the argument is
-- a value (not quite ANF though, since values can be thunks - that is,
-- computations which haven't been run yet, I guess. Do we have an use for
-- that? That allows passing lambdas as arguments directly - which is fine,
-- because producing a closure indeed does not have intermediate results!).
⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
|
Reorder code
|
Reorder code
Separate non-CBPV and CBPV version of the code
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9654b48257da5d67093ffa9653ed62f10df0bfa4
|
composition-sem-sec-reduction.agda
|
composition-sem-sec-reduction.agda
|
module otp-sem-sec where
import Level as L
open import Function
open import Data.Nat.NP
open import Data.Bits
open import Data.Bool
open import Data.Bool.Properties
open import Data.Vec hiding (_>>=_)
open import Data.Product.NP hiding (_⟦×⟧_)
open import Relation.Nullary
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
open import flipbased-implem
open ≡-Reasoning
open import Data.Unit using (⊤)
open import composable
open import vcomp
open import forkable
open import flat-funs
open import program-distance
open import one-time-semantic-security
import bit-guessing-game
module BitsExtra where
splitAt′ : ∀ k {n} → Bits (k + n) → Bits k × Bits n
splitAt′ k xs = case splitAt k xs of λ { (ys , zs , _) → (ys , zs) }
vnot∘vnot : ∀ {n} → vnot {n} ∘ vnot ≗ id
vnot∘vnot [] = refl
vnot∘vnot (x ∷ xs) rewrite not-involutive x = cong (_∷_ x) (vnot∘vnot xs)
open BitsExtra
module CompRed {t} {T : Set t}
{♭Funs : FlatFuns T}
(♭ops : FlatFunsOps ♭Funs)
(ep ep' : EncParams) where
open FlatFunsOps ♭ops
open AbsSemSec ♭Funs
open EncParams ep using (|M|; |C|)
open EncParams ep' using () renaming (|M| to |M|'; |C| to |C|')
M = `Bits |M|
C = `Bits |C|
M' = `Bits |M|'
C' = `Bits |C|'
comp-red : (pre-E : M' `→ M)
(post-E : C `→ C') → SemSecReduction ep' ep _
comp-red pre-E post-E (mk A₀ A₁) = mk (A₀ >>> (pre-E *** pre-E) *** idO) (post-E *** idO >>> A₁)
module Comp (ep : EncParams) (|M|' |C|' : ℕ) where
open EncParams ep
ep' : EncParams
ep' = EncParams.mk |M|' |C|' |R|e
open EncParams ep' using () renaming (Enc to Enc'; M to M'; C to C')
Tr = Enc → Enc'
open FlatFunsOps fun♭Ops
comp : (pre-E : M' → M) (post-E : C → C') → Tr
comp pre-E post-E E = pre-E >>> E >>> {-weaken≤ |R|e≤|R|e' ∘-} map↺ post-E
module CompSec (prgDist : PrgDist) (ep : EncParams) (|M|' |C|' : ℕ) where
open PrgDist prgDist
open FlatFunsOps fun♭Ops
open FunSemSec prgDist
open AbsSemSec fun♭Funs
open EncParams ep
ep' : EncParams
ep' = EncParams.mk |M|' |C|' |R|e
open EncParams ep' using () renaming (Enc to Enc'; M to M'; C to C')
module CompSec' (pre-E : M' → M) (post-E : C → C') where
open SemSecReductions ep ep'
open CompRed fun♭Ops ep ep' hiding (M; C)
open Comp ep |M|' |C|'
comp-pres-sem-sec : SemSecTr id (comp pre-E post-E)
comp-pres-sem-sec = comp-red pre-E post-E , λ E A → id
open SemSecReductions ep ep'
open CompRed fun♭Ops ep ep' hiding (M; C)
open CompSec' public
module Comp' {x y z} = Comp x y z
open Comp' hiding (Tr)
comp-pres-sem-sec⁻¹ : ∀ pre-E pre-E⁻¹
(pre-E-right-inv : pre-E ∘ pre-E⁻¹ ≗ id)
post-E post-E⁻¹
(post-E-left-inv : post-E⁻¹ ∘ post-E ≗ id)
→ SemSecTr⁻¹ id (comp pre-E post-E)
comp-pres-sem-sec⁻¹ pre-E pre-E⁻¹ pre-E-inv post-E post-E⁻¹ post-E-inv =
SemSecTr→SemSecTr⁻¹
(comp pre-E⁻¹ post-E⁻¹)
(comp pre-E post-E)
(λ E m R → trans (post-E-inv _) (cong (λ x → run↺ (E x) R) (pre-E-inv _)))
(comp-pres-sem-sec pre-E⁻¹ post-E⁻¹)
module PostNegSec (prgDist : PrgDist) ep where
open EncParams ep
open Comp ep |M| |C| hiding (Tr)
open CompSec prgDist ep |M| |C|
open FunSemSec prgDist
open SemSecReductions ep ep
post-neg : Tr
post-neg = comp id vnot
post-neg-pres-sem-sec : SemSecTr id post-neg
post-neg-pres-sem-sec = comp-pres-sem-sec id vnot
post-neg-pres-sem-sec⁻¹ : SemSecTr⁻¹ id post-neg
post-neg-pres-sem-sec⁻¹ = comp-pres-sem-sec⁻¹ id id (λ _ → refl) vnot vnot vnot∘vnot
module Concrete k where
open program-distance.Implem k
module Guess' = bit-guessing-game prgDist
module FunSemSec' = FunSemSec prgDist
module CompSec' = CompSec prgDist
|
module composition-sem-sec-reduction where
import Level as L
open import Function
open import Data.Nat.NP
open import Data.Bits
open import Data.Bool
open import Data.Vec hiding (_>>=_)
open import Data.Product.NP using (_,_)
open import Relation.Nullary
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
open import flipbased-implem
open ≡-Reasoning
open import Data.Unit using (⊤)
open import composable
open import vcomp
open import forkable
open import flat-funs
open import program-distance
open import one-time-semantic-security
import bit-guessing-game
-- One focuses on a particular kind of transformation
-- over a cipher, namely pre and post composing functions
-- to a given cipher E.
-- This transformation can change the size of input/output
-- of the given cipher.
module Comp (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where
open EncParams²-same-|R|ᵉ ep₀ |M|₁ |C|₁
open FlatFunsOps fun♭Ops
comp : (pre-E : M₁ → M₀) (post-E : C₀ → C₁) → Tr
comp pre-E post-E E = pre-E >>> E >>> map↺ post-E
-- This module shows how to adapt an adversary that is supposed to break
-- one time semantic security of some cipher E enhanced by pre and post
-- composition to an adversary breaking one time semantic security of the
-- original cipher E.
--
-- The adversary transformation works for any notion of function space
-- (FlatFuns) and the corresponding operations (FlatFunsOps).
-- Because of this abstraction the following construction can be safely
-- instantiated for at least three uses:
-- * When provided with a concrete notion of function like circuits
-- or at least functions over bit-vectors, one get a concrete circuit
-- transformation process.
-- * When provided with a high-level function space with real products
-- then proving properties about the code is made easy.
-- * When provided with a notion of cost such as time or space then
-- one get a the cost of the resulting adversary given the cost of
-- the inputs (adversary...).
module CompRed {t} {T : Set t}
{♭Funs : FlatFuns T}
(♭ops : FlatFunsOps ♭Funs)
(ep₀ ep₁ : EncParams) where
open FlatFuns ♭Funs
open FlatFunsOps ♭ops
open AbsSemSec ♭Funs
open ♭EncParams² ♭Funs ep₀ ep₁ using (`M₀; `C₀; `M₁; `C₁)
comp-red : (pre-E : `M₁ `→ `M₀)
(post-E : `C₀ `→ `C₁)
→ SemSecReduction ep₁ ep₀ _
comp-red pre-E post-E (mk A₀ A₁) =
mk (A₀ >>> (pre-E *** pre-E) *** idO) (post-E *** idO >>> A₁)
module CompSec (prgDist : PrgDist) (ep₀ : EncParams) (|M|₁ |C|₁ : ℕ) where
open PrgDist prgDist
open FlatFunsOps fun♭Ops
open FunSemSec prgDist
open AbsSemSec fun♭Funs
open EncParams²-same-|R|ᵉ ep₀ |M|₁ |C|₁
open SemSecReductions ep₀ ep₁ id
open CompRed fun♭Ops ep₀ ep₁
private
module Comp-implicit {x y z} = Comp x y z
open Comp-implicit public
comp-pres-sem-sec : ∀ pre-E post-E → SemSecTr (comp pre-E post-E)
comp-pres-sem-sec pre-E post-E = comp-red pre-E post-E , λ E A → id
comp-pres-sem-sec⁻¹ : ∀ pre-E pre-E⁻¹
(pre-E-right-inv : pre-E ∘ pre-E⁻¹ ≗ id)
post-E post-E⁻¹
(post-E-left-inv : post-E⁻¹ ∘ post-E ≗ id)
→ SemSecTr⁻¹ (comp pre-E post-E)
comp-pres-sem-sec⁻¹ pre-E pre-E⁻¹ pre-E-inv post-E post-E⁻¹ post-E-inv =
SemSecTr→SemSecTr⁻¹
(comp pre-E⁻¹ post-E⁻¹)
(comp pre-E post-E)
(λ E m R → trans (post-E-inv _) (cong (λ x → run↺ (E x) R) (pre-E-inv _)))
(comp-pres-sem-sec pre-E⁻¹ post-E⁻¹)
module PostNegSec (prgDist : PrgDist) ep where
open EncParams ep
open EncParams² ep ep using (Tr)
open CompSec prgDist ep |M| |C|
open FunSemSec prgDist
open SemSecReductions ep ep id
post-neg : Tr
post-neg = comp id vnot
post-neg-pres-sem-sec : SemSecTr post-neg
post-neg-pres-sem-sec = comp-pres-sem-sec id vnot
post-neg-pres-sem-sec⁻¹ : SemSecTr⁻¹ post-neg
post-neg-pres-sem-sec⁻¹ = comp-pres-sem-sec⁻¹ id id (λ _ → refl) vnot vnot vnot∘vnot≗id
module Concrete k where
open program-distance.Implem k
module Guess-prgDist = bit-guessing-game prgDist
module FunSemSec-prgDist = FunSemSec prgDist
module CompSec-prgDist = CompSec prgDist
|
Rework composition-sem-sec-reduction.agda
|
Rework composition-sem-sec-reduction.agda
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
be0274dd27e35d690791eb80a7b4b66635414047
|
Thesis/ANormalDTerm.agda
|
Thesis/ANormalDTerm.agda
|
module Thesis.ANormalDTerm where
open import Thesis.ANormal
{-
Try defining an alternative syntax for differential terms. We'll need this for
cache terms. Maybe?
Options:
1. use standard untyped lambda calculus as a target language. This requires much
more weakening in the syntax, which is a nuisance in proofs. Maybe use de Bruijn
levels instead?
2. use a specialized syntax as a target language.
3. use separate namespaces, environments, and so on?
Also, caching uses quite a bit of sugar on top of lambda calculus, with pairs
and what not.
-}
{-
-- To define an alternative syntax for the derivation output, let's remember that:
derive (var x) = dvar dx
derive (let y = f x in t) = let y = f x ; dy = df x dx in derive t
-}
-- Attempt 1 leads to function calls in datatype indexes. That's very very bad!
module Try1 where
-- data DTerm : (Γ : Context) (τ : Type) → Set where
-- dvar : ∀ {Γ τ} (x : Var Γ τ) →
-- DTerm (ΔΓ Γ) (Δt τ)
-- dlett : ∀ {Γ σ τ₁ τ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) → DTerm (Δt τ₁ • τ₁ • ΔΓ Γ) (Δt τ) → DTerm (ΔΓ Γ) (Δt τ)
-- derive-dterm : ∀ {Γ τ} → Term Γ τ → DTerm (ΔΓ Γ) (Δt τ)
-- derive-dterm (var x) = dvar x
-- derive-dterm (lett f x t) = dlett f x (derive-dterm t)
-- Case split on dt FAILS!
-- ⟦_⟧DTerm : ∀ {Γ τ} → DTerm (ΔΓ Γ) (Δt τ) → ⟦ ΔΓ Γ ⟧Context → ⟦ Δt τ ⟧Type
-- ⟦ dt ⟧DTerm ρ = {!dt!}
ΔΔ : Context → Context
ΔΔ ∅ = ∅
ΔΔ (τ • Γ) = Δt τ • ΔΔ Γ
Δτ = Δt
ChΔ : ∀ (Δ : Context) → Set
ChΔ Δ = ⟦ ΔΔ Δ ⟧Context
-- changeStructureΔ : ∀ Δ → ChangeStructure ⟦ Δ ⟧Context
-- changeStructureΔ = {!!}
-- ch_from_to_ {{changeStructureΔ Δ}}
-- [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set
-- [ ∅ ]Δ ∅ from ∅ to ∅ = ⊤
-- [ τ • Δ ]Δ dv • dρ from v1 • ρ1 to (v2 • ρ2) = [ τ ]τ dv from v1 to v2 × [ Δ ]Δ dρ from ρ1 to ρ2
-- Δ is bound to the change type! Not good!
data [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set where
v∅ : [ ∅ ]Δ ∅ from ∅ to ∅
_v•_ : ∀ {τ Δ dv v1 v2 dρ ρ1 ρ2} →
(dvv : [ τ ]τ dv from v1 to v2) →
(dρρ : [ Δ ]Δ dρ from ρ1 to ρ2) →
[ τ • Δ ]Δ (dv • dρ) from (v1 • ρ1) to (v2 • ρ2)
module Try3 where
derive-dvar : ∀ {Δ σ} → (x : Var Δ σ) → Var (ΔΔ Δ) (Δτ σ)
derive-dvar this = this
derive-dvar (that x) = that (derive-dvar x)
fromtoDeriveDVar : ∀ {Δ τ} → (x : Var Δ τ) →
∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 →
[ τ ]τ (⟦ derive-dvar x ⟧Var dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2)
fromtoDeriveDVar this (dvv v• dρρ) = dvv
fromtoDeriveDVar (that x) (dvv v• dρρ) = fromtoDeriveDVar x dρρ
-- A DTerm evaluates in normal context Δ, change context (ΔΔ Δ), and produces
-- a result of type (Δt τ).
data DTerm : (Δ : Context) (τ : Type) → Set where
dvar : ∀ {Δ τ} (x : Var (ΔΔ Δ) (Δt τ)) →
DTerm Δ τ
dlett : ∀ {Δ τ σ τ₁} →
(f : Var Δ (σ ⇒ τ₁)) →
(x : Var Δ σ) →
(t : Term (τ₁ • Δ) τ) →
(df : Var (ΔΔ Δ) (Δτ (σ ⇒ τ₁))) →
(dx : Var (ΔΔ Δ) (Δτ σ)) →
(dt : DTerm (τ₁ • Δ) τ) →
DTerm Δ τ
data DFun (Δ : Context) : (τ : Type) → Set where
dterm : ∀ {τ} → DTerm Δ τ → DFun Δ τ
dabs : ∀ {σ τ} → DFun (σ • Δ) τ → DFun Δ (σ ⇒ τ)
derive-dterm : ∀ {Δ σ} → (t : Term Δ σ) → DTerm Δ σ
derive-dterm (var x) = dvar (derive-dvar x)
derive-dterm (lett f x t) =
dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t)
⟦_⟧DTerm : ∀ {Δ τ} → DTerm Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type
⟦ dvar x ⟧DTerm ρ dρ = ⟦ x ⟧Var dρ
⟦ dlett f x t df dx dt ⟧DTerm ρ dρ =
let v = (⟦ x ⟧Var ρ)
in
⟦ dt ⟧DTerm
(⟦ f ⟧Var ρ v • ρ)
(⟦ df ⟧Var dρ v (⟦ dx ⟧Var dρ) • dρ)
fromtoDeriveDTerm : ∀ {Δ τ} → (t : Term Δ τ) →
∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 →
[ τ ]τ (⟦ derive-dterm t ⟧DTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2)
fromtoDeriveDTerm (var x) dρρ = fromtoDeriveDVar x dρρ
fromtoDeriveDTerm (lett f x t) dρρ =
let fromToF = fromtoDeriveDVar f dρρ
fromToX = fromtoDeriveDVar x dρρ
fromToFX = fromToF _ _ _ fromToX
in fromtoDeriveDTerm t (fromToFX v• dρρ)
derive-dfun : ∀ {Δ σ} → (t : Fun Δ σ) → DFun Δ σ
derive-dfun (term t) = dterm (derive-dterm t)
derive-dfun (abs f) = dabs (derive-dfun f)
⟦_⟧DFun : ∀ {Δ τ} → DFun Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type
⟦ dterm t ⟧DFun = ⟦ t ⟧DTerm
⟦ dabs df ⟧DFun ρ dρ = λ v dv → ⟦ df ⟧DFun (v • ρ) (dv • dρ)
fromtoDeriveDFun : ∀ {Δ τ} → (f : Fun Δ τ) →
∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 →
[ τ ]τ (⟦ derive-dfun f ⟧DFun ρ1 dρ) from (⟦ f ⟧Fun ρ1) to (⟦ f ⟧Fun ρ2)
fromtoDeriveDFun (term t) = fromtoDeriveDTerm t
fromtoDeriveDFun (abs f) dρρ = λ dv v1 v2 dvv → fromtoDeriveDFun f (dvv v• dρρ)
module Try2 where
-- This is literally isomorphic to the source type :-| derivation is part of erasure...
data DTerm : (Γ : Context) (τ : Type) → Set where
dvar : ∀ {Γ τ} (x : Var Γ τ) →
DTerm Γ τ
-- dlett : ∀ {Γ σ τ₁ τ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) → DTerm (Δt τ₁ • τ₁ • ΔΓ Γ) (Δt τ) → DTerm (ΔΓ Γ) (Δt τ)
dlett : ∀ {Γ τ σ τ₁} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) →
DTerm (τ₁ • Γ) τ →
DTerm Γ τ
derive-dterm : ∀ {Γ τ} → Term Γ τ → DTerm Γ τ
derive-dterm (var x) = dvar x
derive-dterm (lett f x t) = dlett f x (derive-dterm t)
-- Incremental semantics over terms. Same results as base semantics over change
-- terms. But less weakening.
⟦_⟧DTerm : ∀ {Γ τ} → DTerm Γ τ → ⟦ ΔΓ Γ ⟧Context → ⟦ Δt τ ⟧Type
⟦ dvar x ⟧DTerm dρ = ⟦ derive-var x ⟧Var dρ
⟦ dlett f x dt ⟧DTerm dρ =
let ρ = ⟦ Γ≼ΔΓ ⟧≼ dρ
in ⟦ dt ⟧DTerm
( ⟦ derive-var f ⟧Var dρ (⟦ x ⟧Var ρ) (⟦ derive-var x ⟧Var dρ)
• ⟦ f ⟧Var ρ (⟦ x ⟧Var ρ)
• dρ)
|
module Thesis.ANormalDTerm where
open import Thesis.ANormal
ΔΔ : Context → Context
ΔΔ ∅ = ∅
ΔΔ (τ • Γ) = Δt τ • ΔΔ Γ
Δτ = Δt
ChΔ : ∀ (Δ : Context) → Set
ChΔ Δ = ⟦ ΔΔ Δ ⟧Context
-- [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set
-- [ ∅ ]Δ ∅ from ∅ to ∅ = ⊤
-- [ τ • Δ ]Δ dv • dρ from v1 • ρ1 to (v2 • ρ2) = [ τ ]τ dv from v1 to v2 × [ Δ ]Δ dρ from ρ1 to ρ2
data [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set where
v∅ : [ ∅ ]Δ ∅ from ∅ to ∅
_v•_ : ∀ {τ Δ dv v1 v2 dρ ρ1 ρ2} →
(dvv : [ τ ]τ dv from v1 to v2) →
(dρρ : [ Δ ]Δ dρ from ρ1 to ρ2) →
[ τ • Δ ]Δ (dv • dρ) from (v1 • ρ1) to (v2 • ρ2)
derive-dvar : ∀ {Δ σ} → (x : Var Δ σ) → Var (ΔΔ Δ) (Δτ σ)
derive-dvar this = this
derive-dvar (that x) = that (derive-dvar x)
fromtoDeriveDVar : ∀ {Δ τ} → (x : Var Δ τ) →
∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 →
[ τ ]τ (⟦ derive-dvar x ⟧Var dρ) from (⟦ x ⟧Var ρ1) to (⟦ x ⟧Var ρ2)
fromtoDeriveDVar this (dvv v• dρρ) = dvv
fromtoDeriveDVar (that x) (dvv v• dρρ) = fromtoDeriveDVar x dρρ
-- A DTerm evaluates in normal context Δ, change context (ΔΔ Δ), and produces
-- a result of type (Δt τ).
data DTerm : (Δ : Context) (τ : Type) → Set where
dvar : ∀ {Δ τ} (x : Var (ΔΔ Δ) (Δt τ)) →
DTerm Δ τ
dlett : ∀ {Δ τ σ τ₁} →
(f : Var Δ (σ ⇒ τ₁)) →
(x : Var Δ σ) →
(t : Term (τ₁ • Δ) τ) →
(df : Var (ΔΔ Δ) (Δτ (σ ⇒ τ₁))) →
(dx : Var (ΔΔ Δ) (Δτ σ)) →
(dt : DTerm (τ₁ • Δ) τ) →
DTerm Δ τ
data DFun (Δ : Context) : (τ : Type) → Set where
dterm : ∀ {τ} → DTerm Δ τ → DFun Δ τ
dabs : ∀ {σ τ} → DFun (σ • Δ) τ → DFun Δ (σ ⇒ τ)
derive-dterm : ∀ {Δ σ} → (t : Term Δ σ) → DTerm Δ σ
derive-dterm (var x) = dvar (derive-dvar x)
derive-dterm (lett f x t) =
dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t)
⟦_⟧DTerm : ∀ {Δ τ} → DTerm Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type
⟦ dvar x ⟧DTerm ρ dρ = ⟦ x ⟧Var dρ
⟦ dlett f x t df dx dt ⟧DTerm ρ dρ =
let v = (⟦ x ⟧Var ρ)
in
⟦ dt ⟧DTerm
(⟦ f ⟧Var ρ v • ρ)
(⟦ df ⟧Var dρ v (⟦ dx ⟧Var dρ) • dρ)
fromtoDeriveDTerm : ∀ {Δ τ} → (t : Term Δ τ) →
∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 →
[ τ ]τ (⟦ derive-dterm t ⟧DTerm ρ1 dρ) from (⟦ t ⟧Term ρ1) to (⟦ t ⟧Term ρ2)
fromtoDeriveDTerm (var x) dρρ = fromtoDeriveDVar x dρρ
fromtoDeriveDTerm (lett f x t) dρρ =
let fromToF = fromtoDeriveDVar f dρρ
fromToX = fromtoDeriveDVar x dρρ
fromToFX = fromToF _ _ _ fromToX
in fromtoDeriveDTerm t (fromToFX v• dρρ)
derive-dfun : ∀ {Δ σ} → (t : Fun Δ σ) → DFun Δ σ
derive-dfun (term t) = dterm (derive-dterm t)
derive-dfun (abs f) = dabs (derive-dfun f)
⟦_⟧DFun : ∀ {Δ τ} → DFun Δ τ → ⟦ Δ ⟧Context → ⟦ ΔΔ Δ ⟧Context → ⟦ Δt τ ⟧Type
⟦ dterm t ⟧DFun = ⟦ t ⟧DTerm
⟦ dabs df ⟧DFun ρ dρ = λ v dv → ⟦ df ⟧DFun (v • ρ) (dv • dρ)
fromtoDeriveDFun : ∀ {Δ τ} → (f : Fun Δ τ) →
∀ {dρ ρ1 ρ2} → [ Δ ]Δ dρ from ρ1 to ρ2 →
[ τ ]τ (⟦ derive-dfun f ⟧DFun ρ1 dρ) from (⟦ f ⟧Fun ρ1) to (⟦ f ⟧Fun ρ2)
fromtoDeriveDFun (term t) = fromtoDeriveDTerm t
fromtoDeriveDFun (abs f) dρρ = λ dv v1 v2 dvv → fromtoDeriveDFun f (dvv v• dρρ)
|
remove bad experiments
|
ANormalDTerm: remove bad experiments
|
Agda
|
mit
|
inc-lc/ilc-agda
|
3ef9b9505385dfad155ad9a494a161385271f576
|
ZK/GroupHom/ElGamal.agda
|
ZK/GroupHom/ElGamal.agda
|
{-# OPTIONS --without-K #-}
open import Type using (Type)
open import Function using (flip; _∘_)
open import Data.Product renaming (proj₁ to fst; proj₂ to snd)
open import Data.Two
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP using (_≡_; ap; ap₂; !_; _∙_; module ≡-Reasoning)
open import Algebra.Group
open import Algebra.Group.Constructions
open import Algebra.Group.Homomorphism
import ZK.SigmaProtocol.KnownStatement
import ZK.GroupHom
open import ZK.GroupHom.Types
module ZK.GroupHom.ElGamal
(G+ G* : Type)
(𝔾+ : Group G+)
(𝔾* : Group G*)
(_==_ : G* → G* → 𝟚)
(✓-== : ∀ {x y} → x ≡ y → ✓ (x == y))
(==-✓ : ∀ {x y} → ✓ (x == y) → x ≡ y)
(_^_ : G* → G+ → G*)
(^-hom : ∀ b → GroupHomomorphism 𝔾+ 𝔾* (_^_ b))
(g : G*)
where
open Additive-Group 𝔾+
open Multiplicative-Group 𝔾* hiding (_^_)
-- TODO: Re-use another module
module ElGamal-encryption where
record CipherText : Type where
constructor _,_
field
α β : G*
PubKey = G*
PrivKey = G+
EncRnd = G+ {- randomness used for encryption of ct -}
Message = G* {- plain text message -}
enc : PubKey → EncRnd → Message → CipherText
enc y r M = α , β where
α = g ^ r
β = (y ^ r) * M
dec : PrivKey → CipherText → Message
dec x ct = (α ^ x)⁻¹ * β where
open CipherText ct
open ElGamal-encryption
-- CP : (g₀ g₁ u₀ u₁ : G) (w : ℤq) → Type
-- CP g₀ g₁ u₀ u₁ w = (g₀ ^ w ≡ u₀) × (g₁ ^ w ≡ u₁)
-- CP : (g₀ g₁ u₀ u₁ : G*) (w : G+) → Type
-- CP g₀ g₁ u₀ u₁ w = ✓ (((g₀ ^ w) == u₀) ∧ ((g₁ ^ w) == u₁))
module _ (y : PubKey)
(M : Message)
(ct : CipherText)
where
module CT = CipherText ct
KnownEncRnd : EncRnd → Type
KnownEncRnd r = enc y r M ≡ ct
known-enc-rnd : GrpHom _ _ KnownEncRnd
known-enc-rnd = record
{ 𝔾+ = 𝔾+
; 𝔾* = Product.×-grp 𝔾* 𝔾*
; _==_ = λ x y → fst x == fst y ∧ snd x == snd y
; ✓-== = λ e → ✓∧ (✓-== (ap fst e)) (✓-== (ap snd e))
; ==-✓ = λ e → let p = ✓∧× e in
ap₂ _,_ (==-✓ (fst p))
(==-✓ (snd p))
; φ = _
; φ-hom = Pair.pair-hom _ _ _ (_^_ g) (^-hom g)
(_^_ y) (^-hom y)
; y = CT.α , CT.β / M
; φ⇒P = λ _ e → ap₂ (λ p q → fst p , q) e
(ap (flip _*_ M ∘ snd) e ∙ ! /-*)
; P⇒φ = λ _ e → ap₂ _,_ (ap CipherText.α e)
(*-/ ∙ ap (flip _/_ M) (ap CipherText.β e))
}
KnownDec : PrivKey → Type
KnownDec x = (g ^ x ≡ y) × (dec x ct ≡ M)
open ≡-Reasoning
/⁻¹-* : ∀ {x y} → (x / y)⁻¹ * x ≡ y
/⁻¹-* {x} {y} =
(x / y)⁻¹ * x ≡⟨ ap (λ z → z * x) ⁻¹-hom′ ⟩
y ⁻¹ ⁻¹ * x ⁻¹ * x ≡⟨ *-assoc ⟩
y ⁻¹ ⁻¹ * (x ⁻¹ * x) ≡⟨ ap₂ _*_ ⁻¹-involutive (fst ⁻¹-inverse) ⟩
y * 1# ≡⟨ *1-identity ⟩
y ∎
/-⁻¹* : ∀ {x y} → x / (y ⁻¹ * x) ≡ y
/-⁻¹* {x} {y} =
x * (y ⁻¹ * x) ⁻¹ ≡⟨ ap (_*_ x) ⁻¹-hom′ ⟩
x * (x ⁻¹ * y ⁻¹ ⁻¹) ≡⟨ ! *-assoc ⟩
(x * x ⁻¹) * y ⁻¹ ⁻¹ ≡⟨ *= (snd ⁻¹-inverse) ⁻¹-involutive ⟩
1# * y ≡⟨ 1*-identity ⟩
y ∎
known-dec : GrpHom _ _ KnownDec
known-dec = record
{ 𝔾+ = 𝔾+
; 𝔾* = Product.×-grp 𝔾* 𝔾*
; _==_ = λ x y → fst x == fst y ∧ snd x == snd y
; ✓-== = λ e → ✓∧ (✓-== (ap fst e)) (✓-== (ap snd e))
; ==-✓ = λ e → let p = ✓∧× e in
ap₂ _,_ (==-✓ (fst p))
(==-✓ (snd p))
; φ = _
; φ-hom = Pair.pair-hom _ _ _ (_^_ g) (^-hom g)
(_^_ CT.α) (^-hom CT.α)
; y = y , CT.β / M
; φ⇒P = λ x e → ap fst e , ap (λ z → z ⁻¹ * CT.β) (ap snd e) ∙ /⁻¹-*
; P⇒φ = λ x e → ap₂ _,_ (fst e) (! /-⁻¹* ∙ ap (_/_ CT.β) (snd e))
}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type using (Type)
open import Type.Eq
open import Function.NP using (flip; _∘_; it)
open import Data.Product.NP
open import Data.Two hiding (_²)
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP using (idp; _≡_; ap; ap₂; !_; _∙_; module ≡-Reasoning)
open import Algebra.Group
open import Algebra.Group.Constructions
open import Algebra.Group.Homomorphism
import ZK.GroupHom
open import ZK.GroupHom.Types
module ZK.GroupHom.ElGamal
(G+ G* : Type)
(𝔾+ : Group G+)
(𝔾* : Group G*)
(G*-eq? : Eq? G*)
(_^_ : G* → G+ → G*)
(^-hom : ∀ b → GroupHomomorphism 𝔾+ 𝔾* (_^_ b))
(g : G*)
where
module 𝔾* = Group 𝔾*
open Additive-Group 𝔾+
open module MG = Multiplicative-Group 𝔾* hiding (_^_; _²)
module ^ b = GroupHomomorphism (^-hom b)
_² : Type → Type
A ² = A × A
-- TODO: Re-use another module
module ElGamal-encryption where
PubKey = G*
PrivKey = G+
EncRnd = G+ {- randomness used for encryption of ct -}
Message = G* {- plain text message -}
CipherText = G* × G*
module CipherText (ct : CipherText) where
α β : G*
α = fst ct
β = snd ct
pub-of : PrivKey → PubKey
pub-of sk = g ^ sk
enc : PubKey → Message → EncRnd → CipherText
enc y M r = α , β
module enc where
α = g ^ r
β = (y ^ r) * M
dec : PrivKey → CipherText → Message
dec sk (α , β) = (α ^ sk)⁻¹ * β
open ElGamal-encryption
open Product
_²-grp : {A : Type} → Group A → Group (A ²)
𝔸 ²-grp = ×-grp 𝔸 𝔸
𝕄 : Group Message
𝕄 = 𝔾*
ℂ𝕋 : Group CipherText
ℂ𝕋 = ×-grp 𝔾* 𝔾*
-- CP : (g₀ g₁ u₀ u₁ : G) (w : ℤq) → Type
-- CP g₀ g₁ u₀ u₁ w = (g₀ ^ w ≡ u₀) × (g₁ ^ w ≡ u₁)
-- CP : (g₀ g₁ u₀ u₁ : G*) (w : G+) → Type
-- CP g₀ g₁ u₀ u₁ w = ✓ (((g₀ ^ w) == u₀) ∧ ((g₁ ^ w) == u₁))
module Known-enc-rnd
(y : PubKey)
(M : Message)
(ct : CipherText)
where
module ct = CipherText ct
Valid-witness : EncRnd → Type
Valid-witness r = enc y M r ≡ ct
zk-hom : ZK-hom _ _ Valid-witness
zk-hom = record
{ φ-hom = < ^-hom g , ^-hom y >-hom
; y = ct.α , ct.β / M
; φ⇒P = λ _ e → ap₂ (λ p q → fst p , q) e
(ap (flip _*_ M ∘ snd) e ∙ ! /-*)
; P⇒φ = λ _ e → ap₂ _,_ (ap fst e)
(*-/ ∙ ap (flip _/_ M) (ap snd e))
}
open ≡-Reasoning
/⁻¹-* : ∀ {x y} → (x / y)⁻¹ * x ≡ y
/⁻¹-* {x} {y} =
(x / y)⁻¹ * x ≡⟨ ap (λ z → z * x) ⁻¹-hom′ ⟩
y ⁻¹ ⁻¹ * x ⁻¹ * x ≡⟨ *-assoc ⟩
y ⁻¹ ⁻¹ * (x ⁻¹ * x) ≡⟨ ap₂ _*_ ⁻¹-involutive (fst ⁻¹-inverse) ⟩
y * 1# ≡⟨ *1-identity ⟩
y ∎
/-⁻¹* : ∀ {x y} → x / (y ⁻¹ * x) ≡ y
/-⁻¹* {x} {y} =
x * (y ⁻¹ * x) ⁻¹ ≡⟨ ap (_*_ x) ⁻¹-hom′ ⟩
x * (x ⁻¹ * y ⁻¹ ⁻¹) ≡⟨ ! *-assoc ⟩
(x * x ⁻¹) * y ⁻¹ ⁻¹ ≡⟨ *= (snd ⁻¹-inverse) ⁻¹-involutive ⟩
1# * y ≡⟨ 1*-identity ⟩
y ∎
x⁻¹y≡1→x≡y : ∀ {x y} → x ⁻¹ * y ≡ 1# → x ≡ y
x⁻¹y≡1→x≡y e = cancels-*-left (fst ⁻¹-inverse ∙ ! e)
module Known-dec
(y : PubKey)
(M : Message)
(ct : CipherText)
where
module ct = CipherText ct
Valid-witness : PrivKey → Type
Valid-witness sk = (g ^ sk ≡ y) × (dec sk ct ≡ M)
zk-hom : ZK-hom _ _ Valid-witness
zk-hom = record
{ φ-hom = < ^-hom g , ^-hom ct.α >-hom
; y = y , ct.β / M
; φ⇒P = λ x e → ap fst e , ap (λ z → z ⁻¹ * ct.β) (ap snd e) ∙ /⁻¹-*
; P⇒φ = λ x e → ap₂ _,_ (fst e) (! /-⁻¹* ∙ ap (_/_ ct.β) (snd e))
}
-- Inverse of ciphertexts
_⁻¹CT : CipherText → CipherText
(α , β)⁻¹CT = α ⁻¹ , β ⁻¹
-- Division of ciphertexts
_/CT_ : CipherText → CipherText → CipherText
(α₀ , β₀) /CT (α₁ , β₁) = (α₀ / α₁) , (β₀ / β₁)
-- TODO: Move this elsewhere (Cipher.ElGamal.Homomorphic)
import Algebra.FunctionProperties.Eq as FP
open FP.Implicits
open import Algebra.Group.Abelian
module From-*-comm
(*-comm : Commutative _*_)
where
private
module 𝔾*-comm = Abelian-Group-Struct (𝔾*.grp-struct , *-comm)
hom-enc : (y : PubKey) → GroupHomomorphism (×-grp 𝕄 𝔾+) ℂ𝕋 (uncurry (enc y))
hom-enc y = mk λ { {M₀ , r₀} {M₁ , r₁} →
ap₂ _,_ (^.hom _)
(enc.β y (M₀ * M₁) (r₀ + r₁) ≡⟨by-definition⟩
y ^ (r₀ + r₁) * (M₀ * M₁) ≡⟨ *= (^.hom y) idp ⟩
(y ^ r₀ * y ^ r₁) * (M₀ * M₁) ≡⟨ 𝔾*-comm.interchange ⟩
enc.β y M₀ r₀ * enc.β y M₁ r₁ ∎) }
module hom-enc y = GroupHomomorphism (hom-enc y)
-- The encryption of the inverse is the inverse of the encryption
-- (notice that the randomnesses seems to be negated only because we give an additive notation to our G+ group)
enc-⁻¹ : ∀ {y M r} → enc y (M ⁻¹) (0− r) ≡ enc y M r ⁻¹CT
enc-⁻¹ = hom-enc.pres-inv _
-- The encryption of the division is the division of the encryptions
-- (notice that the randomnesses seems to be subtracted only because we give an additive notation to our G+ group)
enc-/ : ∀ {y M₀ r₀ M₁ r₁} → enc y (M₀ / M₁) (r₀ − r₁) ≡ enc y M₀ r₀ /CT enc y M₁ r₁
enc-/ = hom-enc.−-/ _
-- Alice wants to prove to the public that she encrypted the
-- same message for two (potentially different) recepients
-- without revealing the content of the message or the randomness
-- used for the encryptions.
module Message-equality-enc
(y₀ y₁ : PubKey)
(ct₀ ct₁ : CipherText)
where
module ct₀ = CipherText ct₀
module ct₁ = CipherText ct₁
Witness = Message × EncRnd ²
Statement = CipherText ²
Valid-witness : Witness → Type
Valid-witness (M , r₀ , r₁) = enc y₀ M r₀ ≡ ct₀ × enc y₁ M r₁ ≡ ct₁
private
θ : Message × EncRnd ² → (Message × EncRnd)²
θ (M , r₀ , r₁) = ((M , r₀) , (M , r₁))
θ-hom : GroupHomomorphism (×-grp 𝕄 (𝔾+ ²-grp)) ((×-grp 𝕄 𝔾+)²-grp) θ
θ-hom = mk idp
φ-hom : GroupHomomorphism _ _ _
φ-hom = < hom-enc y₀ × hom-enc y₁ >-hom ∘-hom θ-hom
zk-hom : ZK-hom _ _ Valid-witness
zk-hom = record
{ φ-hom = φ-hom
; y = ct₀ , ct₁
; φ⇒P = λ _ e → ap fst e , ap snd e
; P⇒φ = λ x e → ap₂ _,_ (fst e) (snd e)
}
module From-flip-^-hom
(flip-^-hom : ∀ x → GroupHomomorphism 𝔾* 𝔾* (flip _^_ x))
where
module flip-^ x = GroupHomomorphism (flip-^-hom x)
hom-dec : (x : PrivKey) → GroupHomomorphism ℂ𝕋 𝕄 (dec x)
hom-dec x = mk λ { {α₀ , β₀} {α₁ , β₁} →
dec x (α₀ * α₁ , β₀ * β₁) ≡⟨by-definition⟩
((α₀ * α₁) ^ x)⁻¹ * (β₀ * β₁) ≡⟨ ap (λ z → _*_ (_⁻¹ z) (_*_ β₀ β₁)) (flip-^.hom x) ⟩
(α₀ ^ x * α₁ ^ x)⁻¹ * (β₀ * β₁) ≡⟨ *= 𝔾*-comm.⁻¹-hom idp ⟩
(α₀ ^ x)⁻¹ * (α₁ ^ x)⁻¹ * (β₀ * β₁) ≡⟨ 𝔾*-comm.interchange ⟩
dec x (α₀ , β₀) * dec x (α₁ , β₁) ∎ }
module hom-dec x = GroupHomomorphism (hom-dec x)
-- Bob wants to prove to the public that he decrypted two
-- ciphertexts which decrypt to the same message,
-- without revealing the content of the message or his
-- secret key.
-- The two ciphertexts are encrypted using the same public key.
module Message-equality-dec
(y : PubKey)
(ct₀ ct₁ : CipherText)
where
private
module ct₀ = CipherText ct₀
module ct₁ = CipherText ct₁
α/ = ct₀.α / ct₁.α
β/ = ct₀.β / ct₁.β
Valid-witness : PrivKey → Type
Valid-witness sk = pub-of sk ≡ y × dec sk ct₀ ≡ dec sk ct₁
zk-hom : ZK-hom _ _ Valid-witness
zk-hom = record
{ φ-hom = < ^-hom g , ^-hom (ct₀.α / ct₁.α) >-hom
; y = y , ct₀.β / ct₁.β
; φ⇒P = λ x e →
ap fst e ,
x/y≡1→x≡y
(dec x ct₀ / dec x ct₁ ≡⟨ ! hom-dec.−-/ x ⟩
dec x (ct₀ /CT ct₁) ≡⟨by-definition⟩
dec x (α/ , β/) ≡⟨ ! ap (λ z → dec x (α/ , snd z)) e ⟩
dec x (α/ , (α/)^ x) ≡⟨ fst ⁻¹-inverse ⟩
1# ∎)
; P⇒φ = λ x e → ap₂ _,_ (fst e)
(x⁻¹y≡1→x≡y
(dec x (α/ , β/) ≡⟨ hom-dec.−-/ x ⟩
dec x ct₀ / dec x ct₁ ≡⟨ /= (snd e) idp ⟩
dec x ct₁ / dec x ct₁ ≡⟨ snd ⁻¹-inverse ⟩
1# ∎))
}
-- -}
-- -}
-- -}
|
add proofs of equality of message (two styles)
|
ZK.GroupHom.ElGamal: add proofs of equality of message (two styles)
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
ccb724c55f5701acf132f0a3045f0a1c2d116f96
|
Denotational/ValidChanges.agda
|
Denotational/ValidChanges.agda
|
module Denotational.ValidChanges where
open import Data.Product
open import Data.Unit
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Equivalence
open import Changes
-- DEFINITION of valid changes via a logical relation
{-
What I wanted to write:
data ValidΔ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where
base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv
fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) →
(∀ (s : ⟦ S ⟧) ds (valid : ValidΔ s ds) → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) →
ValidΔ f df
-}
-- What I had to write:
-- Note: now I could go back to using a datatype, since the datatype is now strictly positive.
Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set
Valid-Δ {bool} v dv = ⊤
Valid-Δ {S ⇒ T} f df =
∀ (s : ⟦ S ⟧) ds {- (valid-w : Valid-Δ s ds) -} →
Valid-Δ (f s) (df s ds) ×
(apply df f) (apply ds s) ≡ apply (df s ds) (f s)
diff-is-valid : ∀ {τ} (v′ v : ⟦ τ ⟧) → Valid-Δ {τ} v (diff v′ v)
diff-is-valid {bool} v′ v = tt
diff-is-valid {τ ⇒ τ₁} v′ v =
λ s ds →
diff-is-valid (v′ (apply ds s)) (v s) , (
begin
apply (diff v′ v) v (apply ds s)
≡⟨ refl ⟩
apply
(diff (v′ (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s)))
(v (apply ds s))
≡⟨ ≡-cong (λ x → apply (diff (v′ x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) ⟩
apply (diff (v′ (apply ds s)) (v (apply ds s))) (v (apply ds s))
≡⟨ apply-diff (v (apply ds s)) (v′ (apply ds s)) ⟩
v′ (apply ds s)
≡⟨ sym (apply-diff (v s) (v′ (apply ds s))) ⟩
apply ((diff v′ v) s ds) (v s)
∎) where open ≡-Reasoning
derive-is-valid : ∀ {τ} (v : ⟦ τ ⟧) → Valid-Δ {τ} v (derive v)
derive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v
-- This is a postulate elsewhere, but here I provide a proper proof.
diff-apply-proof : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Valid-Δ v dv) → diff (apply dv v) v ≡ dv
diff-apply-proof {τ₁ ⇒ τ₂} df f df-valid = ext (λ v → ext (λ dv →
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv)) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv)) ⟩
df v dv
∎)) where open ≡-Reasoning
diff-apply-proof {bool} db b _ = xor-cancellative b db
|
module Denotational.ValidChanges where
open import Data.Product
open import Data.Unit
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Equivalence
open import Changes
-- DEFINITION of valid changes via a logical relation
{-
What I wanted to write:
data Valid-Δ : {T : Type} → (v : ⟦ T ⟧) → (dv : ⟦ Δ-Type T ⟧) → Set where
base : (v : ⟦ bool ⟧) → (dv : ⟦ Δ-Type bool ⟧) → ValidΔ v dv
fun : ∀ {S T} → (f : ⟦ S ⇒ T ⟧) → (df : ⟦ Δ-Type (S ⇒ T) ⟧) →
(∀ (s : ⟦ S ⟧) ds → (ValidΔ (f s) (df s ds)) × ((apply df f) (apply ds s) ≡ apply (df s ds) (f s))) →
ValidΔ f df
-}
-- What I had to write:
-- Note: now I could go back to using a datatype, since the datatype is now strictly positive.
Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set
Valid-Δ {bool} v dv = ⊤
Valid-Δ {S ⇒ T} f df =
∀ (s : ⟦ S ⟧) ds {- (valid-w : Valid-Δ s ds) -} →
Valid-Δ (f s) (df s ds) ×
(apply df f) (apply ds s) ≡ apply (df s ds) (f s)
diff-is-valid : ∀ {τ} (v′ v : ⟦ τ ⟧) → Valid-Δ {τ} v (diff v′ v)
diff-is-valid {bool} v′ v = tt
diff-is-valid {τ ⇒ τ₁} v′ v =
λ s ds →
diff-is-valid (v′ (apply ds s)) (v s) , (
begin
apply (diff v′ v) v (apply ds s)
≡⟨ refl ⟩
apply
(diff (v′ (apply (derive (apply ds s)) (apply ds s))) (v (apply ds s)))
(v (apply ds s))
≡⟨ ≡-cong (λ x → apply (diff (v′ x) (v (apply ds s))) (v (apply ds s))) (apply-derive (apply ds s)) ⟩
apply (diff (v′ (apply ds s)) (v (apply ds s))) (v (apply ds s))
≡⟨ apply-diff (v (apply ds s)) (v′ (apply ds s)) ⟩
v′ (apply ds s)
≡⟨ sym (apply-diff (v s) (v′ (apply ds s))) ⟩
apply ((diff v′ v) s ds) (v s)
∎) where open ≡-Reasoning
derive-is-valid : ∀ {τ} (v : ⟦ τ ⟧) → Valid-Δ {τ} v (derive v)
derive-is-valid v rewrite sym (diff-derive v) = diff-is-valid v v
-- This is a postulate elsewhere, but here I provide a proper proof.
diff-apply-proof : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Valid-Δ v dv) → diff (apply dv v) v ≡ dv
diff-apply-proof {τ₁ ⇒ τ₂} df f df-valid = ext (λ v → ext (λ dv →
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv)) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv)) ⟩
df v dv
∎)) where open ≡-Reasoning
diff-apply-proof {bool} db b _ = xor-cancellative b db
|
Update alternative definition of Valid-Δ
|
agda: Update alternative definition of Valid-Δ
Rationale for maintaining this alternative definition: If all proofs
go through with the current definition of Valid-Δ, which can be
expressed as a datatype, we'll switch to defining it with a datatype.
Old-commit-hash: a21df87af1c12f4d94d3710d37e6e4eedfcc83b0
|
Agda
|
mit
|
inc-lc/ilc-agda
|
884a5f0cedea9599476b8420c36f53c0676e8551
|
arrow.agda
|
arrow.agda
|
data Bool : Set where
true : Bool
false : Bool
--------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
--------------------
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
--------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
modusponens : ℕ → Arrow → Arrow
modusponens n (⇒ q) = (⇒ q)
modusponens n (p ⇒ q) with (n ≡ p)
... | true = modusponens n q
... | false = p ⇒ (modusponens n q)
reduce : ℕ → List Arrow → List Arrow
reduce n ∘ = ∘
reduce n (arr ∷ rst) = (modusponens n arr) ∷ (reduce n rst)
search : List Arrow → List ℕ
search ∘ = ∘
search ((⇒ n) ∷ rst) = n ∷ (search (reduce n rst))
search ((n ⇒ q) ∷ rst) with (n ∈ (search rst))
... | true = search (q ∷ rst)
... | false = search rst
|
data Bool : Set where
true : Bool
false : Bool
--------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
--------------------
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
--------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
modusponens : ℕ → Arrow → Arrow
modusponens n (⇒ q) = (⇒ q)
modusponens n (p ⇒ q) with (n ≡ p)
... | true = modusponens n q
... | false = p ⇒ (modusponens n q)
reduce : ℕ → List Arrow → List Arrow
reduce n ∘ = ∘
reduce n (arr ∷ rst) = (modusponens n arr) ∷ (reduce n rst)
search : List Arrow → List ℕ
search ∘ = ∘
search ((⇒ n) ∷ rst) = n ∷ (search (reduce n rst))
search ((n ⇒ q) ∷ rst) with (n ∈ (search rst))
... | true = search (q ∷ rst)
... | false = search rst
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (search cs)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
|
Add turnstile
|
Add turnstile
|
Agda
|
mit
|
louisswarren/hieretikz
|
b5aa26aa273178564c9e3df4846793f1e96e2e81
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
open import Base.Change.Algebra as CA
using (ChangeAlgebraFamily)
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Level
-- Extension Point: Change algebras for base types
Structure : Set₁
Structure = ChangeAlgebraFamily ⟦_⟧Base
module Structure {{change-algebra-base : Structure}} where
-- change algebras
open CA public renaming
-- Constructors for All′
( [] to ∅
; _∷_ to _•_
)
-- We provide: change algebra for every type
instance
change-algebra : ∀ τ → ChangeAlgebra ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = changeAlgebraFun {{change-algebra τ₁}} {{change-algebra τ₂}}
change-algebra-family : ChangeAlgebraFamily ⟦_⟧Type
change-algebra-family = family change-algebra
-- function changes
module _ {τ₁ τ₂ : Type} where
open FunctionChange {{change-algebra τ₁}} {{change-algebra τ₂}} public
renaming
( correct to is-valid
; apply to call-change
)
-- We also provide: change environments (aka. environment changes).
open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebraListChanges to environment-changes
)
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍_₎ Γ
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
open import Base.Change.Algebra as CA
using (ChangeAlgebraFamily)
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Level
-- Extension Point: Change algebras for base types
Structure : Set₁
Structure = ChangeAlgebraFamily ⟦_⟧Base
module Structure {{change-algebra-base : Structure}} where
-- change algebras
open CA public renaming
-- Constructors for All′
( [] to ∅
; _∷_ to _•_
)
change-algebra : ∀ τ → ChangeAlgebra ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = changeAlgebraFun {{change-algebra τ₁}} {{change-algebra τ₂}}
-- We provide: change algebra for every type
instance
change-algebra-family : ChangeAlgebraFamily ⟦_⟧Type
change-algebra-family = family change-algebra
-- function changes
module _ {τ₁ τ₂ : Type} where
open FunctionChange {{change-algebra τ₁}} {{change-algebra τ₂}} public
renaming
( correct to is-valid
; apply to call-change
)
-- We also provide: change environments (aka. environment changes).
open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebraListChanges to environment-changes
)
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍_₎ Γ
|
Disable unused instance
|
Disable unused instance
Agda will not infer explicit arguments of instance arguments. On the
other hand, trying to make this into a proper instance (with an implicit
argument) makes typechecking too much slower or not terminating (more
precisely, on some modules typechecking took longer than I wanted to
wait and I didn't investigate further).
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e41ee7bc6a42348d7050206b8746f460ef632c69
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set
apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base
diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
Change (base ι) = Change-base ι
Change (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : Change σ) → valid v dv → Change τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
n ⊞₍ base ι ₎ Δn = apply-change-base ι n Δn
f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
m ⊟₍ base ι ₎ n = diff-change-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : Change σ) (R[v,Δv] : valid v Δv)
→ valid {τ} (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v}
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞₍ σ ₎ Δw
in
R[v,u-v] {τ}
,
(begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ValidChange : Type → Set
ValidChange τ = Triple
⟦ τ ⟧
(λ _ → Change τ)
(λ v dv → valid {τ} v dv)
ΔEnv : Context → Set
ΔEnv = DependentList ValidChange
ignore-valid-change : ValidChange ⊆ ⟦_⟧
ignore-valid-change (cons v _ _) = v
update-valid-change : ValidChange ⊆ ⟦_⟧
update-valid-change {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv
ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
ignore = map (λ {τ} → ignore-valid-change {τ})
update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
update = map (λ {τ} → update-valid-change {τ})
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → Change-base ι → Set
apply-change-base : ∀ ι → ⟦ ι ⟧Base → Change-base ι → ⟦ ι ⟧Base
diff-change-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → Change-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-change-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
apply-change : ∀ τ → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
diff-change : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
ValidChange : Type → Set
ValidChange τ = Triple
⟦ τ ⟧
(λ _ → Change τ)
(λ v dv → valid {τ} v dv)
Change (base ι) = Change-base ι
Change (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : Change σ) → valid v dv → Change τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
n ⊞₍ base ι ₎ Δn = apply-change-base ι n Δn
f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
m ⊟₍ base ι ₎ n = diff-change-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → Change τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : Change σ) (R[v,Δv] : valid v Δv)
→ valid {τ} (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v}
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞₍ σ ₎ Δw
in
R[v,u-v] {τ}
,
(begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ΔEnv : Context → Set
ΔEnv = DependentList ValidChange
ignore-valid-change : ValidChange ⊆ ⟦_⟧
ignore-valid-change (cons v _ _) = v
update-valid-change : ValidChange ⊆ ⟦_⟧
update-valid-change {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv
ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
ignore = map (λ {τ} → ignore-valid-change {τ})
update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
update = map (λ {τ} → update-valid-change {τ})
|
Move ValidChange next to Change.
|
Move ValidChange next to Change.
This commit prepares for using ValidChange in the definition of Change.
Old-commit-hash: a90d85385de56e35cc3aba5821ddc4a2d3448247
|
Agda
|
mit
|
inc-lc/ilc-agda
|
6156984d33b98f39610ee7244b167632999b9b41
|
Parametric/Change/Correctness.agda
|
Parametric/Change/Correctness.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Validity as Validity
import Parametric.Change.Specification as Specification
import Parametric.Change.Type as ChangeType
import Parametric.Change.Term as ChangeTerm
import Parametric.Change.Value as ChangeValue
import Parametric.Change.Evaluation as ChangeEvaluation
import Parametric.Change.Derive as Derive
import Parametric.Change.Implementation as Implementation
module Parametric.Change.Correctness
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ΔBase : ChangeType.Structure Base)
(diff-base : ChangeTerm.DiffStructure Const ΔBase)
(apply-base : ChangeTerm.ApplyStructure Const ΔBase)
(⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase)
(⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase)
(meaning-⊕-base : ChangeEvaluation.ApplyStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(meaning-⊝-base : ChangeEvaluation.DiffStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(validity-structure : Validity.Structure ⟦_⟧Base)
(specification-structure : Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure)
(derive-const : Derive.Structure Const ΔBase)
(implementation-structure : Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase
validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open Validity.Structure ⟦_⟧Base validity-structure
open Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure
open ChangeType.Structure Base ΔBase
open ChangeTerm.Structure Const ΔBase diff-base apply-base
open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧
open ChangeEvaluation.Structure
⟦_⟧Base ⟦_⟧Const ΔBase
apply-base diff-base
⟦apply-base⟧ ⟦diff-base⟧
meaning-⊕-base meaning-⊝-base
open Derive.Structure Const ΔBase derive-const
open Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure
-- The denotational properties of the `derive` transformation.
-- In particular, the main theorem about it producing the correct
-- incremental behavior.
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
Structure : Set
Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
(ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) →
⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′)
module Structure (derive-const-correct : Structure) where
deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′)
deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′
deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′
-- That `derive t` implements ⟦ t ⟧Δ
derive-correct : ∀ {τ Γ} (t : Term Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′)
derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))
derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅
derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ =
derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′
derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ =
derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′
(derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′)
derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ =
deriveVar-correct x ρ dρ ρ′ dρ≈ρ′
derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′
= subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′)
(derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′
(⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′))
derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ =
λ w dw w′ dw≈w′ →
derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′)
derive-correct-closed : ∀ {τ} (t : Term ∅ τ) →
⟦ t ⟧Δ ∅ ∅ ≈₍ τ ₎ ⟦ derive t ⟧ ∅
derive-correct-closed t = derive-correct t ∅ ∅ ∅ ∅
main-theorem : ∀ {σ τ}
{f : Term ∅ (σ ⇒ τ)} {s : Term ∅ σ} {ds : Term ∅ (ΔType σ)} →
{dv : Δ₍ σ ₎ (⟦ s ⟧ ∅)} {erasure : dv ≈₍ σ ₎ (⟦ ds ⟧ ∅)} →
⟦ app f (s ⊕₍ σ ₎ ds) ⟧ ≡ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧
main-theorem {σ} {τ} {f} {s} {ds} {dv} {erasure} =
let
g = ⟦ f ⟧ ∅
Δg = ⟦ f ⟧Δ ∅ ∅
Δg′ = ⟦ derive f ⟧ ∅
v = ⟦ s ⟧ ∅
dv′ = ⟦ ds ⟧ ∅
u = ⟦ s ⊕₍ σ ₎ ds ⟧ ∅
-- Δoutput-term = app (app (derive f) x) (y ⊝ x)
in
ext {A = ⟦ ∅ ⟧Context} (λ { ∅ →
begin
g u
≡⟨ cong g (sym (meaning-⊕ {t = s} {Δt = ds})) ⟩
g (v ⟦⊕₍ σ ₎⟧ dv′)
≡⟨ cong g (sym (carry-over {σ} dv erasure)) ⟩
g (v ⊞₍ σ ₎ dv)
≡⟨ corollary-closed {σ} {τ} f v dv ⟩
g v ⊞₍ τ ₎ call-change {σ} {τ} Δg v dv
≡⟨ carry-over {τ} (call-change {σ} {τ} Δg v dv)
(derive-correct f ∅ ∅ ∅ ∅ v dv dv′ erasure) ⟩
g v ⟦⊕₍ τ ₎⟧ Δg′ v dv′
≡⟨ meaning-⊕ {t = app f s} {Δt = app (app (derive f) s) ds} ⟩
⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ ∅
∎}) where open ≡-Reasoning
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Validity as Validity
import Parametric.Change.Specification as Specification
import Parametric.Change.Type as ChangeType
import Parametric.Change.Term as ChangeTerm
import Parametric.Change.Value as ChangeValue
import Parametric.Change.Evaluation as ChangeEvaluation
import Parametric.Change.Derive as Derive
import Parametric.Change.Implementation as Implementation
module Parametric.Change.Correctness
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ΔBase : ChangeType.Structure Base)
(diff-base : ChangeTerm.DiffStructure Const ΔBase)
(apply-base : ChangeTerm.ApplyStructure Const ΔBase)
(⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase)
(⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase)
(meaning-⊕-base : ChangeEvaluation.ApplyStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(meaning-⊝-base : ChangeEvaluation.DiffStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(validity-structure : Validity.Structure ⟦_⟧Base)
(specification-structure : Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure)
(derive-const : Derive.Structure Const ΔBase)
(implementation-structure : Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase
validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open Validity.Structure ⟦_⟧Base validity-structure
open Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure
open ChangeType.Structure Base ΔBase
open ChangeTerm.Structure Const ΔBase diff-base apply-base
open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧
open ChangeEvaluation.Structure
⟦_⟧Base ⟦_⟧Const ΔBase
apply-base diff-base
⟦apply-base⟧ ⟦diff-base⟧
meaning-⊕-base meaning-⊝-base
open Derive.Structure Const ΔBase derive-const
open Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure
-- The denotational properties of the `derive` transformation.
-- In particular, the main theorem about it producing the correct
-- incremental behavior.
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
Structure : Set
Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
(ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) →
⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′)
module Structure (derive-const-correct : Structure) where
deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′)
deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′
deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′
-- That `derive t` implements ⟦ t ⟧Δ
derive-correct : ∀ {τ Γ} (t : Term Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′)
derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))
derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅
derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ =
derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′
derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ =
derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′
(derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′)
derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ =
deriveVar-correct x ρ dρ ρ′ dρ≈ρ′
derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′
= subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′)
(derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′
(⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′))
derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ =
λ w dw w′ dw≈w′ →
derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′)
derive-correct-closed : ∀ {τ} (t : Term ∅ τ) →
⟦ t ⟧Δ ∅ ∅ ≈₍ τ ₎ ⟦ derive t ⟧ ∅
derive-correct-closed t = derive-correct t ∅ ∅ ∅ ∅
main-theorem : ∀ {σ τ}
{f : Term ∅ (σ ⇒ τ)} {s : Term ∅ σ} {ds : Term ∅ (ΔType σ)} →
{dv : Δ₍ σ ₎ (⟦ s ⟧ ∅)} {erasure : dv ≈₍ σ ₎ (⟦ ds ⟧ ∅)} →
⟦ app f (s ⊕₍ σ ₎ ds) ⟧ ≡ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧
main-theorem {σ} {τ} {f} {s} {ds} {dv} {erasure} =
let
g = ⟦ f ⟧ ∅
Δg = ⟦ f ⟧Δ ∅ ∅
Δg′ = ⟦ derive f ⟧ ∅
v = ⟦ s ⟧ ∅
dv′ = ⟦ ds ⟧ ∅
u = ⟦ s ⊕₍ σ ₎ ds ⟧ ∅
-- Δoutput-term = app (app (derive f) x) (y ⊝ x)
in
ext {A = ⟦ ∅ ⟧Context} (λ { ∅ →
begin
g u
≡⟨ cong g (sym (meaning-⊕ {t = s} {Δt = ds})) ⟩
g (v ⟦⊕₍ σ ₎⟧ dv′)
≡⟨ cong g (sym (carry-over {σ} dv erasure)) ⟩
g (v ⊞₍ σ ₎ dv)
≡⟨ corollary-closed {σ} {τ} f v dv ⟩
g v ⊞₍ τ ₎ call-change {σ} {τ} Δg v dv
≡⟨ carry-over {τ} (call-change {σ} {τ} Δg v dv)
(derive-correct-closed f v dv dv′ erasure) ⟩
g v ⟦⊕₍ τ ₎⟧ Δg′ v dv′
≡⟨ meaning-⊕ {t = app f s} {Δt = app (app (derive f) s) ds} ⟩
⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ ∅
∎}) where open ≡-Reasoning
|
call derive-correct-closed instead of derive-correct in thm:main
|
call derive-correct-closed instead of derive-correct in thm:main
Old-commit-hash: 9fa1103b0ac110498ae8ab0d0c0f3320ff4b264f
|
Agda
|
mit
|
inc-lc/ilc-agda
|
88a46de4b9496c0c6c4f861d964d90e3ae3cfd27
|
Syntax/Derive/Plotkin.agda
|
Syntax/Derive/Plotkin.agda
|
import Parametric.Syntax.Type as Type
import Base.Syntax.Context as Context
import Syntax.Term.Plotkin as Term
import Syntax.DeltaContext as DeltaContext
import Parametric.Change.Type as DeltaType
module Syntax.Derive.Plotkin
{Base : Set {- of base types -}}
{Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}}
(ΔBase : Base → Base)
(deriveConst :
∀ {Γ Σ τ} → Constant Σ τ →
Term.Terms
Constant
(DeltaContext.ΔContext (DeltaType.ΔType ΔBase) Γ)
(DeltaContext.ΔContext (DeltaType.ΔType ΔBase) Σ) →
Term.Term Constant (DeltaContext.ΔContext (DeltaType.ΔType ΔBase) Γ) (DeltaType.ΔType ΔBase τ))
where
-- Terms of languages described in Plotkin style
open Type Base
open Context Type
open import Syntax.Context.Plotkin Base
open Term Constant
open DeltaType ΔBase
open DeltaContext ΔType
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (ΔContext Σ)
derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ)
derive-terms {∅} ∅ = ∅
derive-terms {τ • Σ} (t • ts) = derive t • fit t • derive-terms ts
derive (var x) = var (deriveVar x)
derive (app s t) = app (app (derive s) (fit t)) (derive t)
derive (abs t) = abs (abs (derive t))
derive (const c ts) = deriveConst c (derive-terms ts)
|
import Parametric.Syntax.Type as Type
import Base.Syntax.Context as Context
import Syntax.Term.Plotkin as Term
import Syntax.DeltaContext as DeltaContext
import Parametric.Change.Type as ChangeType
module Syntax.Derive.Plotkin
{Base : Set {- of base types -}}
{Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -}}
(ΔBase : Base → Base)
(deriveConst :
∀ {Γ Σ τ} → Constant Σ τ →
Term.Terms
Constant
(DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Γ)
(DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Σ) →
Term.Term Constant (DeltaContext.ΔContext (ChangeType.ΔType ΔBase) Γ) (ChangeType.ΔType ΔBase τ))
where
-- Terms of languages described in Plotkin style
open Type Base
open Context Type
open import Syntax.Context.Plotkin Base
open Term Constant
open ChangeType ΔBase
open DeltaContext ΔType
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (ΔContext Σ)
derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ)
derive-terms {∅} ∅ = ∅
derive-terms {τ • Σ} (t • ts) = derive t • fit t • derive-terms ts
derive (var x) = var (deriveVar x)
derive (app s t) = app (app (derive s) (fit t)) (derive t)
derive (abs t) = abs (abs (derive t))
derive (const c ts) = deriveConst c (derive-terms ts)
|
Update qualified import for new module name.
|
Update qualified import for new module name.
Old-commit-hash: 2cb47a8d412768ed582e8af1a042b51c3ca781be
|
Agda
|
mit
|
inc-lc/ilc-agda
|
92f785054b5dd55703ecbd7d5aec15af5e0d04d2
|
README.agda
|
README.agda
|
module README where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Note that Δ is *not* the same as the ∂ operator in
-- definition/intro.tex. See discussion at:
--
-- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
-- The formalization is split across the following files.
-- TODO: document them more.
-- Note that we postulate function extensionality (in
-- `Denotational.ExtensionalityPostulate`), known to be consistent
-- with intensional type theory.
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.ChangeTypes.ChangesAreDerivatives
open import Syntactic.ChangeContexts
open import Syntactic.Terms.Total
open import Syntactic.Changes
open import Denotational.Notation
open import Denotational.Values
open import Denotational.ExtensionalityPostulate
open import Denotational.EqualityLemmas
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Changes
open import Denotational.Equivalence
open import Denotational.ValidChanges
open import Denotational.WeakValidChanges
open import ChangeContexts
open import ChangeContextLifting
open import PropsDelta
open import SymbolicDerivation
-- This finishes the correctness proof of the above work.
open import total
-- EXAMPLES
open import Examples.Examples1
-- NATURAL semantics
open import Syntactic.Closures
open import Denotational.Closures
open import Natural.Lookup
open import Natural.Evaluation
open import Natural.Soundness
-- Other calculi
open import partial
open import lambda
|
module README where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
--
-- XXX: fill in again other details.
-- 41e0f95d7fda7244a258b67f8a4255e4128a42c3 declares that the main theorems are
-- in these files:
open import Denotation.Derive.Canon-Popl14
open import Denotation.Derive.Optimized-Popl14
-- TODO: this file is intended to load all maintained Agda code; otherwise, a
-- better solution to issue #41 should be found.
--
-- Moreover, arguably it would be good if this file would list all relevant
-- imports, to highlight the structure of the development, as it did previously.
-- But I'm not sure what's the best structure.
|
Update a bit README.agda (#41)
|
Update a bit README.agda (#41)
README.agda was not up-to-date. Remaining issues are described inline.
Old-commit-hash: 93469332bbc849a2a13486a1bfae7d21dc9eb399
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9cdc324caee9ca0888f5f85d83d0727e04d6e654
|
incremental.agda
|
incremental.agda
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Semantics
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
Env⟦_⟧ : Context → Set
Env⟦ ∅ ⟧ = Empty
Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- Semantics
lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
lookup⟦ this ⟧ (v • ρ) = v
lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- Semantics
eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ)
eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ)
eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
nil : ∀ {τ Γ} → Term Γ (Δ-Type τ)
nil {τ₁ ⇒ τ₂} =
abs nil
-- λx. nil
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ
-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = {!!}
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Semantics
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
Env⟦_⟧ : Context → Set
Env⟦ ∅ ⟧ = Empty
Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- Semantics
lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
lookup⟦ this ⟧ (v • ρ) = v
lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- Semantics
eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ)
eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ)
eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
nil : ∀ {τ Γ} → Term Γ (Δ-Type τ)
nil {τ₁ ⇒ τ₂} =
abs nil
-- λx. nil
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Δ-Context Γ
-- CHANGE VARIABLES
-- changes 'x' to 'dx'
rename : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
rename this = that this
rename (that x) = that (that (rename x))
-- changes 'x' to 'nil' (if internally bound) or 'dx' (if externally bound)
Δ-var : ∀ {Γ₁ Γ₂ τ} → Var (Γ₁ ⋎ Γ₂) τ → Term (Δ-Context Γ₂) (Δ-Type τ)
Δ-var {∅} x = var (rename x)
Δ-var {τ • Γ} this = nil
Δ-var {τ • Γ} (that x) = Δ-var {Γ} x
-- CHANGE TERMS
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = weaken {∅} {Γ} (Δ-var {Γ} x)
|
Implement 'Δ-term' for variables.
|
Implement 'Δ-term' for variables.
Old-commit-hash: dd0c2916cc4b7472353b0073fa6538f60e7325f5
|
Agda
|
mit
|
inc-lc/ilc-agda
|
f9aeb58a90443e04e8fd3afd4951c3d768d88d29
|
Syntax/Context/Plotkin.agda
|
Syntax/Context/Plotkin.agda
|
module Syntax.Context.Plotkin where
-- Context for Plotkin-stype language descriptions
--
-- Duplicates Syntax.Context to a large extent.
-- Consider having Syntax.Context import this module.
infixr 9 _•_
data Context {Type : Set} : Set where
∅ : Context {Type}
_•_ : (τ : Type) (Γ : Context {Type}) → Context {Type}
data Var {Type : Set} : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
|
module Syntax.Context.Plotkin where
-- Context for Plotkin-stype language descriptions
--
-- This module will tend to duplicate Syntax.Context to a large
-- extent. Consider having Syntax.Context specialize future
-- content of this module to maintain its interface.
infixr 9 _•_
data Context {Type : Set} : Set where
∅ : Context {Type}
_•_ : (τ : Type) (Γ : Context {Type}) → Context {Type}
data Var {Type : Set} : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
|
update description of Plotkin's context
|
Agda: update description of Plotkin's context
Reason: the previous description contains a modularization task
that is already partially carried out.
Reference:
https://github.com/ps-mr/ilc/commit/249e99d807e2382a37fb35aee78c235b98e427aa#commitcomment-3659727
Old-commit-hash: 3c52ba4a07600646db8de764b1460fa6b69c3014
|
Agda
|
mit
|
inc-lc/ilc-agda
|
63b0b983e562c4195da605874a1e5b1e379fa13c
|
UNDEFINED.agda
|
UNDEFINED.agda
|
-- Allow holes in modules to import, by introducing a single general postulate.
module UNDEFINED where
open import Data.Unit.NonEta using (Hidden)
open import Data.Unit.NonEta public using (reveal)
postulate
-- If this postulate would produce a T, it could be used as instance argument, triggering many ambiguities.
-- This postulate is named in capitals because it introduces an inconsistency.
-- Hence, this must be used through `reveal UNDEFINED`.
UNDEFINED : ∀ {ℓ} → {T : Set ℓ} → Hidden T
|
-- Allow holes in modules to import, by introducing a single general postulate.
module UNDEFINED where
postulate
UNDEFINED : ∀ {ℓ} → {T : Set ℓ} → T
|
Refactor UNDEFINED to simplify it (XXX untested)
|
Refactor UNDEFINED to simplify it (XXX untested)
Instance arguments changed behavior, so not every definition is
available anymore. Hence we can avoid the wrapping.
It typechecks, but not really tested on clients.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9c628f8fc4514a3379c8bb109b925c9a196d5571
|
Parametric/Syntax/MTerm.agda
|
Parametric/Syntax/MTerm.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Syntax.MType as MType
module Parametric.Syntax.MTerm
{Base : Type.Structure}
(Const : Term.Structure Base)
where
open Type.Structure Base
open MType.Structure Base
open Term.Structure Base Const
-- Our extension points are sets of primitives, indexed by the
-- types of their arguments and their return type.
-- We want different types of constants; some produce values, some produce
-- computations. In all cases, arguments are assumed to be values (though
-- individual primitives might require thunks).
ValConstStructure : Set₁
ValConstStructure = ValContext → ValType → Set
CompConstStructure : Set₁
CompConstStructure = ValContext → CompType → Set
module Structure (ValConst : ValConstStructure) (CompConst : CompConstStructure) where
mutual
-- Analogues of Terms
Vals : ValContext → ValContext → Set
Vals Γ = DependentList (Val Γ)
Comps : ValContext → List CompType → Set
Comps Γ = DependentList (Comp Γ)
data Val Γ : (τ : ValType) → Set where
vVar : ∀ {τ} (x : ValVar Γ τ) → Val Γ τ
-- XXX Do we need thunks? The draft in the paper doesn't have them.
-- However, they will start being useful if we deal with CBN source
-- languages.
vThunk : ∀ {τ} → Comp Γ τ → Val Γ (U τ)
vConst : ∀ {Σ τ} →
(c : ValConst Σ τ) →
(args : Vals Γ Σ) →
Val Γ τ
data Comp Γ : (τ : CompType) → Set where
cConst : ∀ {Σ τ} →
(c : CompConst Σ τ) →
(args : Vals Γ Σ) →
Comp Γ τ
cForce : ∀ {τ} → Val Γ (U τ) → Comp Γ τ
cReturn : ∀ {τ} (v : Val Γ τ) → Comp Γ (F τ)
{-
-- Originally, M to x in N. But here we have no names!
_into_ : ∀ {σ τ} →
(e₁ : Comp Γ (F σ)) →
(e₂ : Comp (σ •• Γ) τ) →
Comp Γ τ
-}
-- The following constructor is the main difference between CBPV and this
-- monadic calculus. This is better for the caching transformation.
_into_ : ∀ {σ τ} →
(e₁ : Comp Γ (F σ)) →
(e₂ : Comp (σ •• Γ) (F τ)) →
Comp Γ (F τ)
cAbs : ∀ {σ τ} →
(t : Comp (σ •• Γ) τ) →
Comp Γ (σ ⇛ τ)
cApp : ∀ {σ τ} →
(s : Comp Γ (σ ⇛ τ)) →
(t : Val Γ σ) →
Comp Γ τ
weaken-val : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) →
Val Γ₁ τ →
Val Γ₂ τ
weaken-comp : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) →
Comp Γ₁ τ →
Comp Γ₂ τ
weaken-vals : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) →
Vals Γ₁ Σ →
Vals Γ₂ Σ
weaken-val Γ₁≼Γ₂ (vVar x) = vVar (weaken-val-var Γ₁≼Γ₂ x)
weaken-val Γ₁≼Γ₂ (vThunk x) = vThunk (weaken-comp Γ₁≼Γ₂ x)
weaken-val Γ₁≼Γ₂ (vConst c args) = vConst c (weaken-vals Γ₁≼Γ₂ args)
weaken-comp Γ₁≼Γ₂ (cConst c args) = cConst c (weaken-vals Γ₁≼Γ₂ args)
weaken-comp Γ₁≼Γ₂ (cForce x) = cForce (weaken-val Γ₁≼Γ₂ x)
weaken-comp Γ₁≼Γ₂ (cReturn v) = cReturn (weaken-val Γ₁≼Γ₂ v)
weaken-comp Γ₁≼Γ₂ (_into_ {σ} c c₁) = (weaken-comp Γ₁≼Γ₂ c) into (weaken-comp (keep σ •• Γ₁≼Γ₂) c₁)
weaken-comp Γ₁≼Γ₂ (cAbs {σ} c) = cAbs (weaken-comp (keep σ •• Γ₁≼Γ₂) c)
weaken-comp Γ₁≼Γ₂ (cApp s t) = cApp (weaken-comp Γ₁≼Γ₂ s) (weaken-val Γ₁≼Γ₂ t)
weaken-vals Γ₁≼Γ₂ ∅ = ∅
weaken-vals Γ₁≼Γ₂ (px • ts) = (weaken-val Γ₁≼Γ₂ px) • (weaken-vals Γ₁≼Γ₂ ts)
fromCBN : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBNCtx Γ) (cbnToCompType τ)
fromCBNTerms : ∀ {Γ Σ} → Terms Γ Σ → Vals (fromCBNCtx Γ) (fromCBNCtx Σ)
fromCBNTerms ∅ = ∅
fromCBNTerms (px • ts) = vThunk (fromCBN px) • fromCBNTerms ts
open import UNDEFINED
-- This is really supposed to be part of the plugin interface.
cbnToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBNCtx Σ) (cbnToCompType τ)
cbnToCompConst = reveal UNDEFINED
fromCBN (const c args) = cConst (cbnToCompConst c) (fromCBNTerms args)
fromCBN (var x) = cForce (vVar (fromVar cbnToValType x))
fromCBN (app s t) = cApp (fromCBN s) (vThunk (fromCBN t))
fromCBN (abs t) = cAbs (fromCBN t)
-- To satisfy termination checking, we'd need to inline fromCBV and weaken: fromCBV needs to produce a term in a bigger context.
-- But let's ignore that.
{-# NO_TERMINATION_CHECK #-}
fromCBV : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBVCtx Γ) (cbvToCompType τ)
-- This is really supposed to be part of the plugin interface.
cbvToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBVCtx Σ) (cbvToCompType τ)
cbvToCompConst = reveal UNDEFINED
cbvTermsToComps : ∀ {Γ Σ} → Terms Γ Σ → Comps (fromCBVCtx Γ) (fromCBVToCompList Σ)
cbvTermsToComps ∅ = ∅
cbvTermsToComps (px • ts) = fromCBV px • cbvTermsToComps ts
module _ where
dequeValContexts : ValContext → ValContext → ValContext
dequeValContexts ∅ Γ = Γ
dequeValContexts (x • Σ) Γ = dequeValContexts Σ (x • Γ)
dequeValContexts≼≼ : ∀ Σ Γ → Γ ≼≼ dequeValContexts Σ Γ
dequeValContexts≼≼ ∅ Γ = ≼≼-refl
dequeValContexts≼≼ (x • Σ) Γ = ≼≼-trans (drop_••_ x ≼≼-refl) (dequeValContexts≼≼ Σ (x • Γ)) --
dequeContexts : Context → Context → ValContext
dequeContexts Σ Γ = dequeValContexts (fromCBVCtx Σ) (fromCBVCtx Γ)
fromCBVArg : ∀ {σ Γ τ} → Term Γ τ → (Val (cbvToValType τ • fromCBVCtx Γ) (cbvToValType τ) → Comp (cbvToValType τ • fromCBVCtx Γ) (cbvToCompType σ)) → Comp (fromCBVCtx Γ) (cbvToCompType σ)
fromCBVArg t k = (fromCBV t) into k (vVar vThis)
fromCBVArgs : ∀ {Σ Γ τ} → Terms Γ Σ → (Vals (dequeContexts Σ Γ) (fromCBVCtx Σ) → Comp (dequeContexts Σ Γ) (cbvToCompType τ)) → Comp (fromCBVCtx Γ) (cbvToCompType τ)
fromCBVArgs ∅ k = k ∅
fromCBVArgs {σ • Σ} {Γ} (t • ts) k = fromCBVArg t (λ v → fromCBVArgs (weaken-terms (drop_•_ _ ≼-refl) ts) (λ vs → k (weaken-val (dequeValContexts≼≼ (fromCBVCtx Σ) _) v • vs)))
-- Transform a constant application into
--
-- arg1 to x1 in (arg2 to x2 in ... (argn to xn in (cConst (cbvToCompConst c) (x1 :: x2 :: ... xn)))).
fromCBVConstCPSRoot : ∀ {Σ Γ τ} → Const Σ τ → Terms Γ Σ → Comp (fromCBVCtx Γ) (cbvToCompType τ)
-- pass that as a closure to compose with (_•_ x) for each new variable.
fromCBVConstCPSRoot c ts = fromCBVArgs ts (λ vs → cConst (cbvToCompConst c) vs) -- fromCBVConstCPSDo ts {!cConst (cbvToCompConst c)!}
-- In the beginning, we should get a function that expects a whole set of
-- arguments (Vals Γ Σ) for c, in the initial context Γ.
-- Later, each call should match:
-- - Σ = τΣ • Σ′, then we recurse with Γ′ = τΣ • Γ and Σ′. Terms ought to be
-- weakened. The result of f (or the arguments) also ought to be weakened! So f should weaken
-- all arguments.
-- - Σ = ∅.
fromCBV (const c args) = fromCBVConstCPSRoot c args
fromCBV (app s t) =
(fromCBV s) into
((fromCBV (weaken (drop _ • ≼-refl) t)) into
cApp (cForce (vVar (vThat vThis))) (vVar vThis))
-- Values
fromCBV (var x) = cReturn (vVar (fromVar cbvToValType x))
fromCBV (abs t) = cReturn (vThunk (cAbs (fromCBV t)))
-- This reflects the CBV conversion of values in TLCA '99, but we can't write
-- this because it's partial on the Term type. Instead, we duplicate thunking
-- at the call site.
{-
fromCBVToVal : ∀ {Γ τ} (t : Term Γ τ) → Val (fromCBVCtx Γ) (cbvToValType τ)
fromCBVToVal (var x) = vVar (fromVar cbvToValType x)
fromCBVToVal (abs t) = vThunk (cAbs (fromCBV t))
fromCBVToVal (const c args) = {!!}
fromCBVToVal (app t t₁) = {!!} -- Not a value
-}
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Syntax.MType as MType
module Parametric.Syntax.MTerm
{Base : Type.Structure}
(Const : Term.Structure Base)
where
open Type.Structure Base
open MType.Structure Base
open Term.Structure Base Const
-- Our extension points are sets of primitives, indexed by the
-- types of their arguments and their return type.
-- We want different types of constants; some produce values, some produce
-- computations. In all cases, arguments are assumed to be values (though
-- individual primitives might require thunks).
ValConstStructure : Set₁
ValConstStructure = ValContext → ValType → Set
CompConstStructure : Set₁
CompConstStructure = ValContext → CompType → Set
module Structure (ValConst : ValConstStructure) (CompConst : CompConstStructure) where
mutual
-- Analogues of Terms
Vals : ValContext → ValContext → Set
Vals Γ = DependentList (Val Γ)
Comps : ValContext → List CompType → Set
Comps Γ = DependentList (Comp Γ)
data Val Γ : (τ : ValType) → Set where
vVar : ∀ {τ} (x : ValVar Γ τ) → Val Γ τ
-- XXX Do we need thunks? The draft in the paper doesn't have them.
-- However, they will start being useful if we deal with CBN source
-- languages.
vThunk : ∀ {τ} → Comp Γ τ → Val Γ (U τ)
vConst : ∀ {Σ τ} →
(c : ValConst Σ τ) →
(args : Vals Γ Σ) →
Val Γ τ
data Comp Γ : (τ : CompType) → Set where
cConst : ∀ {Σ τ} →
(c : CompConst Σ τ) →
(args : Vals Γ Σ) →
Comp Γ τ
cForce : ∀ {τ} → Val Γ (U τ) → Comp Γ τ
cReturn : ∀ {τ} (v : Val Γ τ) → Comp Γ (F τ)
{-
-- Originally, M to x in N. But here we have no names!
_into_ : ∀ {σ τ} →
(e₁ : Comp Γ (F σ)) →
(e₂ : Comp (σ •• Γ) τ) →
Comp Γ τ
-}
-- The following constructor is the main difference between CBPV and this
-- monadic calculus. This is better for the caching transformation.
_into_ : ∀ {σ τ} →
(e₁ : Comp Γ (F σ)) →
(e₂ : Comp (σ •• Γ) (F τ)) →
Comp Γ (F τ)
cAbs : ∀ {σ τ} →
(t : Comp (σ •• Γ) τ) →
Comp Γ (σ ⇛ τ)
cApp : ∀ {σ τ} →
(s : Comp Γ (σ ⇛ τ)) →
(t : Val Γ σ) →
Comp Γ τ
weaken-val : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) →
Val Γ₁ τ →
Val Γ₂ τ
weaken-comp : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) →
Comp Γ₁ τ →
Comp Γ₂ τ
weaken-vals : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼≼ Γ₂) →
Vals Γ₁ Σ →
Vals Γ₂ Σ
weaken-val Γ₁≼Γ₂ (vVar x) = vVar (weaken-val-var Γ₁≼Γ₂ x)
weaken-val Γ₁≼Γ₂ (vThunk x) = vThunk (weaken-comp Γ₁≼Γ₂ x)
weaken-val Γ₁≼Γ₂ (vConst c args) = vConst c (weaken-vals Γ₁≼Γ₂ args)
weaken-comp Γ₁≼Γ₂ (cConst c args) = cConst c (weaken-vals Γ₁≼Γ₂ args)
weaken-comp Γ₁≼Γ₂ (cForce x) = cForce (weaken-val Γ₁≼Γ₂ x)
weaken-comp Γ₁≼Γ₂ (cReturn v) = cReturn (weaken-val Γ₁≼Γ₂ v)
weaken-comp Γ₁≼Γ₂ (_into_ {σ} c c₁) = (weaken-comp Γ₁≼Γ₂ c) into (weaken-comp (keep σ •• Γ₁≼Γ₂) c₁)
weaken-comp Γ₁≼Γ₂ (cAbs {σ} c) = cAbs (weaken-comp (keep σ •• Γ₁≼Γ₂) c)
weaken-comp Γ₁≼Γ₂ (cApp s t) = cApp (weaken-comp Γ₁≼Γ₂ s) (weaken-val Γ₁≼Γ₂ t)
weaken-vals Γ₁≼Γ₂ ∅ = ∅
weaken-vals Γ₁≼Γ₂ (px • ts) = (weaken-val Γ₁≼Γ₂ px) • (weaken-vals Γ₁≼Γ₂ ts)
fromCBN : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBNCtx Γ) (cbnToCompType τ)
fromCBNTerms : ∀ {Γ Σ} → Terms Γ Σ → Vals (fromCBNCtx Γ) (fromCBNCtx Σ)
fromCBNTerms ∅ = ∅
fromCBNTerms (px • ts) = vThunk (fromCBN px) • fromCBNTerms ts
open import UNDEFINED
-- This is really supposed to be part of the plugin interface.
cbnToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBNCtx Σ) (cbnToCompType τ)
cbnToCompConst = reveal UNDEFINED
fromCBN (const c args) = cConst (cbnToCompConst c) (fromCBNTerms args)
fromCBN (var x) = cForce (vVar (fromVar cbnToValType x))
fromCBN (app s t) = cApp (fromCBN s) (vThunk (fromCBN t))
fromCBN (abs t) = cAbs (fromCBN t)
-- To satisfy termination checking, we'd need to inline fromCBV and weaken: fromCBV needs to produce a term in a bigger context.
-- But let's ignore that.
{-# NO_TERMINATION_CHECK #-}
fromCBV : ∀ {Γ τ} (t : Term Γ τ) → Comp (fromCBVCtx Γ) (cbvToCompType τ)
-- This is really supposed to be part of the plugin interface.
cbvToCompConst : ∀ {Σ τ} → Const Σ τ → CompConst (fromCBVCtx Σ) (cbvToCompType τ)
cbvToCompConst = reveal UNDEFINED
cbvTermsToComps : ∀ {Γ Σ} → Terms Γ Σ → Comps (fromCBVCtx Γ) (fromCBVToCompList Σ)
cbvTermsToComps ∅ = ∅
cbvTermsToComps (px • ts) = fromCBV px • cbvTermsToComps ts
module _ where
dequeValContexts : ValContext → ValContext → ValContext
dequeValContexts ∅ Γ = Γ
dequeValContexts (x • Σ) Γ = dequeValContexts Σ (x • Γ)
dequeValContexts≼≼ : ∀ Σ Γ → Γ ≼≼ dequeValContexts Σ Γ
dequeValContexts≼≼ ∅ Γ = ≼≼-refl
dequeValContexts≼≼ (x • Σ) Γ = ≼≼-trans (drop_••_ x ≼≼-refl) (dequeValContexts≼≼ Σ (x • Γ)) --
dequeContexts : Context → Context → ValContext
dequeContexts Σ Γ = dequeValContexts (fromCBVCtx Σ) (fromCBVCtx Γ)
fromCBVArg : ∀ {σ Γ τ} → Term Γ τ → (Val (cbvToValType τ • fromCBVCtx Γ) (cbvToValType τ) → Comp (cbvToValType τ • fromCBVCtx Γ) (cbvToCompType σ)) → Comp (fromCBVCtx Γ) (cbvToCompType σ)
fromCBVArg t k = (fromCBV t) into k (vVar vThis)
fromCBVArgs : ∀ {Σ Γ τ} → Terms Γ Σ → (Vals (dequeContexts Σ Γ) (fromCBVCtx Σ) → Comp (dequeContexts Σ Γ) (cbvToCompType τ)) → Comp (fromCBVCtx Γ) (cbvToCompType τ)
fromCBVArgs ∅ k = k ∅
fromCBVArgs {σ • Σ} {Γ} (t • ts) k = fromCBVArg t (λ v → fromCBVArgs (weaken-terms (drop_•_ _ ≼-refl) ts) (λ vs → k (weaken-val (dequeValContexts≼≼ (fromCBVCtx Σ) _) v • vs)))
-- Transform a constant application into
--
-- arg1 to x1 in (arg2 to x2 in ... (argn to xn in (cConst (cbvToCompConst c) (x1 :: x2 :: ... xn)))).
fromCBVConstCPSRoot : ∀ {Σ Γ τ} → Const Σ τ → Terms Γ Σ → Comp (fromCBVCtx Γ) (cbvToCompType τ)
-- pass that as a closure to compose with (_•_ x) for each new variable.
fromCBVConstCPSRoot c ts = fromCBVArgs ts (λ vs → cConst (cbvToCompConst c) vs) -- fromCBVConstCPSDo ts {!cConst (cbvToCompConst c)!}
-- In the beginning, we should get a function that expects a whole set of
-- arguments (Vals Γ Σ) for c, in the initial context Γ.
-- Later, each call should match:
-- - Σ = τΣ • Σ′, then we recurse with Γ′ = τΣ • Γ and Σ′. Terms ought to be
-- weakened. The result of f (or the arguments) also ought to be weakened! So f should weaken
-- all arguments.
-- - Σ = ∅.
fromCBV (const c args) = fromCBVConstCPSRoot c args
fromCBV (app s t) =
(fromCBV s) into
(fromCBV (weaken (drop _ • ≼-refl) t) into
cApp (cForce (vVar (vThat vThis))) (vVar vThis))
-- Values
fromCBV (var x) = cReturn (vVar (fromVar cbvToValType x))
fromCBV (abs t) = cReturn (vThunk (cAbs (fromCBV t)))
-- This reflects the CBV conversion of values in TLCA '99, but we can't write
-- this because it's partial on the Term type. Instead, we duplicate thunking
-- at the call site.
{-
fromCBVToVal : ∀ {Γ τ} (t : Term Γ τ) → Val (fromCBVCtx Γ) (cbvToValType τ)
fromCBVToVal (var x) = vVar (fromVar cbvToValType x)
fromCBVToVal (abs t) = vThunk (cAbs (fromCBV t))
fromCBVToVal (const c args) = {!!}
fromCBVToVal (app t t₁) = {!!} -- Not a value
-}
|
Drop extra parens
|
Drop extra parens
|
Agda
|
mit
|
inc-lc/ilc-agda
|
bf4c2374eee5aebf6d658c3c557a4233b3ba3172
|
flipbased.agda
|
flipbased.agda
|
module flipbased where
open import Algebra
import Level as L
open L using () renaming (_⊔_ to _L⊔_)
open import Function hiding (_⟨_⟩_)
open import Data.Nat.NP
open import Data.Nat.Properties
open import Data.Product using (proj₁; proj₂; _,_; swap; _×_)
open import Data.Bits hiding (replicateM)
open import Data.Bool
open import Data.Vec using (Vec; []; _∷_; take; drop; head; tail)
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_)
record M {a} n (A : Set a) : Set a where
constructor mk
field
run : Bits n → A
toss′ : M 1 Bit
toss′ = mk head
return′ : ∀ {a} {A : Set a} → A → M 0 A
return′ = mk ∘ const
pure′ : ∀ {a} {A : Set a} → A → M 0 A
pure′ = return′
comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → M m A → M n A
comap f (mk g) = mk (g ∘ f)
weaken : ∀ {m n a} {A : Set a} → M n A → M (m + n) A
weaken {m} = comap (drop m)
weaken′ : ∀ {m n a} {A : Set a} → M n A → M (n + m) A
weaken′ = comap (take _)
private
take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n
take≤ z≤n _ = []
take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs
weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → M m A → M n A
weaken≤ p = comap (take≤ p)
coerce : ∀ {m n a} {A : Set a} → m ≡ n → M m A → M n A
coerce ≡.refl = id
toss : ∀ {n} → M (1 + n) Bit
toss = weaken′ toss′
return : ∀ {n a} {A : Set a} → A → M n A
return = weaken′ ∘ return′
pure : ∀ {n a} {A : Set a} → A → M n A
pure = return
_>>=_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₁ + n₂) B
_>>=_ {n₁} x f = mk (λ bs → M.run (f (M.run x (take _ bs))) (drop n₁ bs))
_>>=′_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₂ + n₁) B
_>>=′_ {n₁} {n₂} rewrite ℕ°.+-comm n₂ n₁ = _>>=_
_>>_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → M n₂ B → M (n₁ + n₂) B
_>>_ {n₁} x y = x >>= const y
map : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → M n A → M n B
map f x = mk (f ∘ M.run x)
-- map f x ≗ x >>=′ (return {0} ∘ f)
join : ∀ {n₁ n₂ a} {A : Set a} →
M n₁ (M n₂ A) → M (n₁ + n₂) A
join x = x >>= id
infixl 4 _⊛_
_⊛_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ (A → B) → M n₂ A → M (n₁ + n₂) B
_⊛_ {n₁} mf mx = mf >>= λ f → map (_$_ f) mx
_⟨_⟩_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
M m A → (A → B → C) → M n B → M (m + n) C
x ⟨ f ⟩ y = map f x ⊛ y
⟪_⟫ : ∀ {n} {a} {A : Set a} → A → M n A
⟪_⟫ = pure
⟪_⟫′ : ∀ {a} {A : Set a} → A → M 0 A
⟪_⟫′ = pure′
⟪_·_⟫ : ∀ {a b} {A : Set a} {B : Set b} {n} → (A → B) → M n A → M n B
⟪ f · x ⟫ = map f x
⟪_·_·_⟫ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
(A → B → C) → M m A → M n B → M (m + n) C
⟪ f · x · y ⟫ = map f x ⊛ y
⟪_·_·_·_⟫ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} →
(A → B → C → D) → M m A → M n B → M o C → M (m + n + o) D
⟪ f · x · y · z ⟫ = map f x ⊛ y ⊛ z
_⟨,⟩_ : ∀ {a b} {A : Set a} {B : Set b} {m n} → M m A → M n B → M (m + n) (A × B)
x ⟨,⟩ y = ⟪ _,_ · x · y ⟫
_⟨xor⟩_ : ∀ {n₁ n₂} → M n₁ Bit → M n₂ Bit → M (n₁ + n₂) Bit
x ⟨xor⟩ y = ⟪ _xor_ · x · y ⟫
_⟨⊕⟩_ : ∀ {n₁ n₂ m} → M n₁ (Bits m) → M n₂ (Bits m) → M (n₁ + n₂) (Bits m)
x ⟨⊕⟩ y = ⟪ _⊕_ · x · y ⟫
replicateM : ∀ {n m} {a} {A : Set a} → M m A → M (n * m) (Vec A n)
replicateM {zero} _ = ⟪ [] ⟫
replicateM {suc _} x = ⟪ _∷_ · x · replicateM x ⟫
random : ∀ {n} → M n (Bits n)
-- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce
random {zero} = ⟪ [] ⟫
random {suc _} = ⟪ _∷_ · toss′ · random ⟫
randomTbl : ∀ m n → M (2 ^ m * n) (Vec (Bits n) (2 ^ m))
randomTbl m n = replicateM random
randomFun : ∀ m n → M (2 ^ m * n) (Bits m → Bits n)
randomFun m n = ⟪ funFromTbl · randomTbl m n ⟫
randomFunExt : ∀ {n k a} {A : Set a} → M k (Bits n → A) → M (k + k) (Bits (suc n) → A)
randomFunExt f = ⟪ comb · f · f ⟫ where comb = λ g₁ g₂ xs → (if head xs then g₁ else g₂) (tail xs)
2*_ : ℕ → ℕ
2* x = x + x
2^_ : ℕ → ℕ
2^ 0 = 1
2^ (suc n) = 2* (2^ n)
costRndFun : ℕ → ℕ → ℕ
costRndFun zero n = n
costRndFun (suc m) n = 2* (costRndFun m n)
lem : ∀ m n → costRndFun m n ≡ 2 ^ m * n
lem zero n rewrite ℕ°.+-comm n 0 = ≡.refl
lem (suc m) n rewrite lem m n | ℕ°.*-assoc 2 (2 ^ m) n | ℕ°.+-comm (2 ^ m * n) 0 = ≡.refl
randomFun′ : ∀ {m n} → M (costRndFun m n) (Bits m → Bits n)
randomFun′ {zero} = ⟪ const · random ⟫
randomFun′ {suc m} = randomFunExt (randomFun′ {m})
record ProgEquiv a ℓ : Set (L.suc ℓ L⊔ L.suc a) where
infix 2 _≈_ _≋_
field
_≈_ : ∀ {n} {A : Set a} → Rel (M n A) ℓ
refl : ∀ {n A} → Reflexive {A = M n A} _≈_
sym : ∀ {n A} → Symmetric {A = M n A} _≈_
-- not strictly transitive
reflexive : ∀ {n A} → _≡_ ⇒ _≈_ {n} {A}
reflexive ≡.refl = refl
_≋_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_≋_ {n₁} {n₂} p₁ p₂ = _≈_ {n = n₁ ⊔ n₂} (weaken≤ (m≤m⊔n _ _) p₁) (weaken≤ (m≤n⊔m _ n₁) p₂)
where m≤n⊔m : ∀ m n → m ≤ n ⊔ m
m≤n⊔m m n rewrite ⊔°.+-comm n m = m≤m⊔n m n
-- Another name for _≋_
_looks_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_looks_ = _≋_
module WithEquiv (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
SecPRG : ∀ {k n} (prg : (key : Bits k) → Bits n) → Set
SecPRG prg = this looks random where this = ⟪ prg · random ⟫
record PRG k n : Set where
constructor _,_
field
prg : Bits k → Bits n
sec : SecPRG prg
OneTimeSecPRF : ∀ {k m n} (prf : (key : Bits k) (msg : Bits m) → Bits n) → Set
OneTimeSecPRF prf = ∀ {xs} → let this = ⟪ prf · random · ⟪ xs ⟫′ ⟫ in
this looks random
record PRF k m n : Set where
constructor _,_
field
prf : Bits k → Bits m → Bits n
sec : OneTimeSecPRF prf
OTP : ∀ {n} → Bits n → Bits n → Bits n
OTP key msg = key ⊕ msg
init : ∀ {k a} {A : Set a} → (Bits k → A) → M k A
init f = ⟪ f · random ⟫
module Examples (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
open WithEquiv progEq
left-unit-law = ∀ {A B : Set} {n} {x : A} {f : A → M n B} → return′ x >>= f ≈ f x
right-unit-law = ∀ {A : Set} {n} {x : M n A} → x >>=′ return′ ≈ x
assoc-law = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≋ x >>= (λ x → f x >>= g)
assoc-law′ = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≈ coerce (≡.sym (ℕ°.+-assoc n₁ n₂ n₃)) (x >>= (λ x → f x >>= g))
ex₁ = ∀ {x} → toss′ ⟨xor⟩ ⟪ x ⟫′ ≈ ⟪ x ⟫
ex₂ = p ≈ map swap p where p = toss′ ⟨,⟩ toss′
ex₃ = ∀ {n} → OneTimeSecPRF {n} OTP
ex₄ = ∀ {k n} (prg : PRG k n) → OneTimeSecPRF (λ key xs → xs ⊕ PRG.prg prg key)
ex₅ = ∀ {k n} → PRG k n → PRF k n n
|
module flipbased where
open import Algebra
import Level as L
open L using () renaming (_⊔_ to _L⊔_)
open import Function hiding (_⟨_⟩_)
open import Data.Nat.NP
open import Data.Bool
open import Data.Nat.Properties
open import Data.Product using (proj₁; proj₂; _,_; swap; _×_)
open import Data.Bits hiding (replicateM)
open import Data.Bool
open import Data.Vec using (Vec; []; _∷_; take; drop; head; tail)
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_)
record M {a} n (A : Set a) : Set a where
constructor mk
field
run : Bits n → A
toss′ : M 1 Bit
toss′ = mk head
return′ : ∀ {a} {A : Set a} → A → M 0 A
return′ = mk ∘ const
pure′ : ∀ {a} {A : Set a} → A → M 0 A
pure′ = return′
comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → M m A → M n A
comap f (mk g) = mk (g ∘ f)
weaken : ∀ {m n a} {A : Set a} → M n A → M (m + n) A
weaken {m} = comap (drop m)
weaken′ : ∀ {m n a} {A : Set a} → M n A → M (n + m) A
weaken′ = comap (take _)
private
take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n
take≤ z≤n _ = []
take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs
weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → M m A → M n A
weaken≤ p = comap (take≤ p)
coerce : ∀ {m n a} {A : Set a} → m ≡ n → M m A → M n A
coerce ≡.refl = id
toss : ∀ {n} → M (1 + n) Bit
toss = weaken′ toss′
return : ∀ {n a} {A : Set a} → A → M n A
return = weaken′ ∘ return′
pure : ∀ {n a} {A : Set a} → A → M n A
pure = return
_>>=_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₁ + n₂) B
_>>=_ {n₁} x f = mk (λ bs → M.run (f (M.run x (take _ bs))) (drop n₁ bs))
_>>=′_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → (A → M n₂ B) → M (n₂ + n₁) B
_>>=′_ {n₁} {n₂} rewrite ℕ°.+-comm n₂ n₁ = _>>=_
_>>_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ A → M n₂ B → M (n₁ + n₂) B
_>>_ {n₁} x y = x >>= const y
map : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → M n A → M n B
map f x = mk (f ∘ M.run x)
-- map f x ≗ x >>=′ (return {0} ∘ f)
join : ∀ {n₁ n₂ a} {A : Set a} →
M n₁ (M n₂ A) → M (n₁ + n₂) A
join x = x >>= id
infixl 4 _⊛_
_⊛_ : ∀ {n₁ n₂ a b} {A : Set a} {B : Set b} →
M n₁ (A → B) → M n₂ A → M (n₁ + n₂) B
_⊛_ {n₁} mf mx = mf >>= λ f → map (_$_ f) mx
_⟨_⟩_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
M m A → (A → B → C) → M n B → M (m + n) C
x ⟨ f ⟩ y = map f x ⊛ y
⟪_⟫ : ∀ {n} {a} {A : Set a} → A → M n A
⟪_⟫ = pure
⟪_⟫′ : ∀ {a} {A : Set a} → A → M 0 A
⟪_⟫′ = pure′
⟪_·_⟫ : ∀ {a b} {A : Set a} {B : Set b} {n} → (A → B) → M n A → M n B
⟪ f · x ⟫ = map f x
⟪_·_·_⟫ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} {m n} →
(A → B → C) → M m A → M n B → M (m + n) C
⟪ f · x · y ⟫ = map f x ⊛ y
⟪_·_·_·_⟫ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d} {m n o} →
(A → B → C → D) → M m A → M n B → M o C → M (m + n + o) D
⟪ f · x · y · z ⟫ = map f x ⊛ y ⊛ z
choose : ∀ {n a} {A : Set a} → M n A → M n A → M (suc n) A
choose x y = ⟪ if_then_else_ · toss · x · y ⟫
_⟨,⟩_ : ∀ {a b} {A : Set a} {B : Set b} {m n} → M m A → M n B → M (m + n) (A × B)
x ⟨,⟩ y = ⟪ _,_ · x · y ⟫
_⟨xor⟩_ : ∀ {n₁ n₂} → M n₁ Bit → M n₂ Bit → M (n₁ + n₂) Bit
x ⟨xor⟩ y = ⟪ _xor_ · x · y ⟫
_⟨⊕⟩_ : ∀ {n₁ n₂ m} → M n₁ (Bits m) → M n₂ (Bits m) → M (n₁ + n₂) (Bits m)
x ⟨⊕⟩ y = ⟪ _⊕_ · x · y ⟫
replicateM : ∀ {n m} {a} {A : Set a} → M m A → M (n * m) (Vec A n)
replicateM {zero} _ = ⟪ [] ⟫
replicateM {suc _} x = ⟪ _∷_ · x · replicateM x ⟫
random : ∀ {n} → M n (Bits n)
-- random = coerce ? (replicateM toss) -- specialized version for now to avoid coerce
random {zero} = ⟪ [] ⟫
random {suc _} = ⟪ _∷_ · toss′ · random ⟫
randomTbl : ∀ m n → M (2 ^ m * n) (Vec (Bits n) (2 ^ m))
randomTbl m n = replicateM random
randomFun : ∀ m n → M (2 ^ m * n) (Bits m → Bits n)
randomFun m n = ⟪ funFromTbl · randomTbl m n ⟫
randomFunExt : ∀ {n k a} {A : Set a} → M k (Bits n → A) → M (k + k) (Bits (suc n) → A)
randomFunExt f = ⟪ comb · f · f ⟫ where comb = λ g₁ g₂ xs → (if head xs then g₁ else g₂) (tail xs)
2*_ : ℕ → ℕ
2* x = x + x
2^_ : ℕ → ℕ
2^ 0 = 1
2^ (suc n) = 2* (2^ n)
costRndFun : ℕ → ℕ → ℕ
costRndFun zero n = n
costRndFun (suc m) n = 2* (costRndFun m n)
lem : ∀ m n → costRndFun m n ≡ 2 ^ m * n
lem zero n rewrite ℕ°.+-comm n 0 = ≡.refl
lem (suc m) n rewrite lem m n | ℕ°.*-assoc 2 (2 ^ m) n | ℕ°.+-comm (2 ^ m * n) 0 = ≡.refl
randomFun′ : ∀ {m n} → M (costRndFun m n) (Bits m → Bits n)
randomFun′ {zero} = ⟪ const · random ⟫
randomFun′ {suc m} = randomFunExt (randomFun′ {m})
record ProgEquiv a ℓ : Set (L.suc ℓ L⊔ L.suc a) where
infix 2 _≈_ _≋_
field
_≈_ : ∀ {n} {A : Set a} → Rel (M n A) ℓ
refl : ∀ {n A} → Reflexive {A = M n A} _≈_
sym : ∀ {n A} → Symmetric {A = M n A} _≈_
-- not strictly transitive
reflexive : ∀ {n A} → _≡_ ⇒ _≈_ {n} {A}
reflexive ≡.refl = refl
_≋_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_≋_ {n₁} {n₂} p₁ p₂ = _≈_ {n = n₁ ⊔ n₂} (weaken≤ (m≤m⊔n _ _) p₁) (weaken≤ (m≤n⊔m _ n₁) p₂)
where m≤n⊔m : ∀ m n → m ≤ n ⊔ m
m≤n⊔m m n rewrite ⊔°.+-comm n m = m≤m⊔n m n
-- Another name for _≋_
_looks_ : ∀ {n₁ n₂} {A : Set a} → M n₁ A → M n₂ A → Set ℓ
_looks_ = _≋_
module WithEquiv (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
SecPRG : ∀ {k n} (prg : (key : Bits k) → Bits n) → Set
SecPRG prg = this looks random where this = ⟪ prg · random ⟫
record PRG k n : Set where
constructor _,_
field
prg : Bits k → Bits n
sec : SecPRG prg
OneTimeSecPRF : ∀ {k m n} (prf : (key : Bits k) (msg : Bits m) → Bits n) → Set
OneTimeSecPRF prf = ∀ {xs} → let this = ⟪ prf · random · ⟪ xs ⟫′ ⟫ in
this looks random
record PRF k m n : Set where
constructor _,_
field
prf : Bits k → Bits m → Bits n
sec : OneTimeSecPRF prf
OTP : ∀ {n} → Bits n → Bits n → Bits n
OTP key msg = key ⊕ msg
init : ∀ {k a} {A : Set a} → (Bits k → A) → M k A
init f = ⟪ f · random ⟫
module Examples (progEq : ProgEquiv L.zero L.zero) where
open ProgEquiv progEq
open WithEquiv progEq
left-unit-law = ∀ {A B : Set} {n} {x : A} {f : A → M n B} → return′ x >>= f ≈ f x
right-unit-law = ∀ {A : Set} {n} {x : M n A} → x >>=′ return′ ≈ x
assoc-law = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≋ x >>= (λ x → f x >>= g)
assoc-law′ = ∀ {A B C : Set} {n₁ n₂ n₃} {x : M n₁ A} {f : A → M n₂ B} {g : B → M n₃ C}
→ (x >>= f) >>= g ≈ coerce (≡.sym (ℕ°.+-assoc n₁ n₂ n₃)) (x >>= (λ x → f x >>= g))
ex₁ = ∀ {x} → toss′ ⟨xor⟩ ⟪ x ⟫′ ≈ ⟪ x ⟫
ex₂ = p ≈ map swap p where p = toss′ ⟨,⟩ toss′
ex₃ = ∀ {n} → OneTimeSecPRF {n} OTP
ex₄ = ∀ {k n} (prg : PRG k n) → OneTimeSecPRF (λ key xs → xs ⊕ PRG.prg prg key)
ex₅ = ∀ {k n} → PRG k n → PRF k n n
|
add choose
|
flipbased: add choose
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
b2e5536ab47da9ade851c78e2d0e284fdaed9d6e
|
SymbolicDerivation.agda
|
SymbolicDerivation.agda
|
module SymbolicDerivation where
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Syntactic.Changes
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Denotational.ValidChanges
open import Natural.Evaluation
open import Changes
open import ChangeContexts
open import ChangeContextLifting
open import PropsDelta
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var {τ • Γ} this = this
derive-var {τ • Γ} (that x) = that (that (derive-var x))
derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ)
derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t))
where Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x))
derive-term {{Γ′}} true = false
derive-term {{Γ′}} false = false
derive-term {{Γ′}} (if c t e) =
if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c))
(diff-term (apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e)) (lift-term {{Γ′}} t))
(if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c)))
(diff-term (apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t)) (lift-term {{Γ′}} e))
(if (lift-term {{Γ′}} c)
(derive-term {{Γ′}} t)
(derive-term {{Γ′}} e)))
derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t)
|
module SymbolicDerivation where
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Syntactic.Changes
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Denotational.ValidChanges
open import Natural.Evaluation
open import Changes
open import ChangeContexts
open import ChangeContextLifting
open import PropsDelta
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var {τ • Γ} this = this
derive-var {τ • Γ} (that x) = that (that (derive-var x))
derive-term : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → Term Γ₁ τ → Term Γ₂ (Δ-Type τ)
derive-term {Γ₁} {{Γ′}} (abs {τ} t) = abs (abs (derive-term {τ • Γ₁} {{Γ″}} t))
where Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term {{Γ′}} (app t₁ t₂) = app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
derive-term {{Γ′}} (var x) = var (lift Γ′ (derive-var x))
derive-term {{Γ′}} true = false
derive-term {{Γ′}} false = false
derive-term {{Γ′}} (if c t e) =
if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} c))
(diff-term
(apply-term (derive-term {{Γ′}} e) (lift-term {{Γ′}} e))
(lift-term {{Γ′}} t))
(if ((derive-term {{Γ′}} c) and (lift-term {{Γ′}} (! c)))
(diff-term
(apply-term (derive-term {{Γ′}} t) (lift-term {{Γ′}} t))
(lift-term {{Γ′}} e))
(if (lift-term {{Γ′}} c)
(derive-term {{Γ′}} t)
(derive-term {{Γ′}} e)))
derive-term {{Γ′}} (Δ {{Γ″}} t) = Δ {{Γ′}} (derive-term {{Γ″}} t)
|
Break some lines.
|
Break some lines.
Old-commit-hash: f541af3f72f8831d3daba892d4230bda0bdb2cdf
|
Agda
|
mit
|
inc-lc/ilc-agda
|
789ee03b932a743e1b7f9832f09bd7efc37f4fa8
|
Popl14/Denotation/Value.agda
|
Popl14/Denotation/Value.agda
|
module Popl14.Denotation.Value where
open import Popl14.Syntax.Type public
open import Popl14.Change.Type public
open import Base.Denotation.Notation public
open import Structure.Bag.Popl14
open import Data.Integer
-- Values of Calculus Popl14
--
-- Contents
-- - Domains associated with types
-- - `diff` and `apply` in semantic domains
⟦_⟧Type : Type -> Set
⟦ int ⟧Type = ℤ
⟦ bag ⟧Type = Bag
⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
|
module Popl14.Denotation.Value where
open import Popl14.Syntax.Type public
open import Popl14.Change.Type public
open import Base.Denotation.Notation public
open import Structure.Bag.Popl14
open import Data.Integer
-- Values of Calculus Popl14
--
-- Contents
-- - Domains associated with types
-- - `diff` and `apply` in semantic domains
⟦_⟧Base : Base → Set
⟦ base-int ⟧Base = ℤ
⟦ base-bag ⟧Base = Bag
⟦_⟧Type : Type -> Set
⟦ base ι ⟧Type = ⟦ ι ⟧Base
⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
|
Split ⟦_⟧Base from ⟦_⟧Type (WIP).
|
Split ⟦_⟧Base from ⟦_⟧Type (WIP).
Needs fixes to type inference failures!
Old-commit-hash: 4f7d5e46e2d2a202dc2c672a08ae4deeebe589c2
|
Agda
|
mit
|
inc-lc/ilc-agda
|
3ef3b1f40e4b0b9e7308a67388ab552654f5feeb
|
lib/Data/Bits.agda
|
lib/Data/Bits.agda
|
{-# OPTIONS --without-K #-}
module Data.Bits where
open import Algebra
open import Level.NP
open import Type hiding (★)
open import Data.Nat.NP hiding (_==_) renaming (_<=_ to _ℕ<=_)
open import Data.Bit using (Bit)
open import Data.Two renaming (_==_ to _==ᵇ_)
open import Data.Fin.NP using (Fin; zero; suc; inject₁; inject+; raise; Fin▹ℕ)
open import Data.Vec.NP
open import Function.NP
import Data.List.NP as L
-- Re-export some vector functions, maybe they should be given
-- less generic types.
open Data.Vec.NP public using ([]; _∷_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; onᵢ)
Bits : ℕ → ★₀
Bits = Vec Bit
_→ᵇ_ : ℕ → ℕ → ★₀
i →ᵇ o = Bits i → Bits o
0ⁿ : ∀ {n} → Bits n
0ⁿ = replicate 0₂
-- Notice that all empty vectors are the same, hence 0ⁿ {0} ≡ 1ⁿ {0}
1ⁿ : ∀ {n} → Bits n
1ⁿ = replicate 1₂
0∷_ : ∀ {n} → Bits n → Bits (suc n)
0∷ xs = 0₂ ∷ xs
-- can't we make these pattern aliases?
1∷_ : ∀ {n} → Bits n → Bits (suc n)
1∷ xs = 1₂ ∷ xs
_!_ : ∀ {a n} {A : ★ a} → Vec A n → Fin n → A
_!_ = flip lookup
-- see Data.Bits.Properties
_==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bit
[] == [] = 1₂
(b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁
_<=_ : ∀ {n} (xs ys : Bits n) → Bit
[] <= [] = 1₂
(1₂ ∷ xs) <= (0₂ ∷ ys) = 0₂
(0₂ ∷ xs) <= (1₂ ∷ ys) = 1₂
(_ ∷ xs) <= (_ ∷ ys) = xs <= ys
infixr 5 _⊕_
_⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n
_⊕_ = zipWith _xor_
⊕-group : ℕ → Group ₀ ₀
⊕-group = LiftGroup.group Xor°.+-group
module ⊕-Group (n : ℕ) = Group (⊕-group n)
module ⊕-Monoid (n : ℕ) = Monoid (⊕-Group.monoid n)
-- Negate all bits, i.e. "xor"ing them by one.
vnot : ∀ {n} → Endo (Bits n)
vnot = _⊕_ 1ⁿ
-- Negate the i-th bit.
notᵢ : ∀ {n} (i : Fin n) → Bits n → Bits n
notᵢ = onᵢ not
msb : ∀ k {n} → Bits (k + n) → Bits k
msb = take
lsb : ∀ k {n} → Bits (n + k) → Bits k
lsb _ {n} = drop n
msb₂ : ∀ {n} → Bits (2 + n) → Bits 2
msb₂ = msb 2
lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2
lsb₂ = reverse ∘ msb 2 ∘ reverse
#1 : ∀ {n} → Bits n → Fin (suc n)
#1 [] = zero
#1 (0₂ ∷ bs) = inject₁ (#1 bs)
#1 (1₂ ∷ bs) = suc (#1 bs)
#0 : ∀ {n} → Bits n → Fin (suc n)
#0 = #1 ∘ map not
allBitsL : ∀ n → L.List (Bits n)
allBitsL _ = replicateM (toList (0₂ ∷ 1₂ ∷ []))
where open L.Monad
allBits : ∀ n → Vec (Bits n) (2^ n)
allBits zero = [] ∷ []
allBits (suc n) = map 0∷_ bs ++ map 1∷_ bs
where bs = allBits n
always : ∀ n → Bits n → Bit
always _ _ = 1₂
never : ∀ n → Bits n → Bit
never _ _ = 0₂
_∨°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit
_∨°_ f g x = f x ∨ g x
_∧°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit
_∧°_ f g x = f x ∧ g x
not° : ∀ {n} (f : Bits n → Bit) → Bits n → Bit
not° f = not ∘ f
view∷ : ∀ {n a b} {A : ★ a} {B : ★ b} → (A → Vec A n → B) → Vec A (suc n) → B
view∷ f (x ∷ xs) = f x xs
sucBCarry : ∀ {n} → Bits n → Bits (1 + n)
sucBCarry [] = 0₂ ∷ []
sucBCarry (0₂ ∷ xs) = 0₂ ∷ sucBCarry xs
sucBCarry (1₂ ∷ xs) = view∷ (λ x xs → x ∷ not x ∷ xs) (sucBCarry xs)
sucB : ∀ {n} → Bits n → Bits n
sucB = tail ∘ sucBCarry
--_[mod_] : ℕ → ℕ → ★₀
--a [mod b ] = DivMod' a b
module ReversedBits where
sucRB : ∀ {n} → Bits n → Bits n
sucRB [] = []
sucRB (0₂ ∷ xs) = 1₂ ∷ xs
sucRB (1₂ ∷ xs) = 0₂ ∷ sucRB xs
toFin : ∀ {n} → Bits n → Fin (2^ n)
toFin [] = zero
toFin (0₂ ∷ xs) = inject+ _ (toFin xs)
toFin {suc n} (1₂ ∷ xs) = raise (2^ n) (toFin xs)
Bits▹ℕ : ∀ {n} → Bits n → ℕ
Bits▹ℕ [] = zero
Bits▹ℕ (0₂ ∷ xs) = Bits▹ℕ xs
Bits▹ℕ {suc n} (1₂ ∷ xs) = 2^ n + Bits▹ℕ xs
ℕ▹Bits : ∀ {n} → ℕ → Bits n
ℕ▹Bits {zero} _ = []
ℕ▹Bits {suc n} x = [0: 0∷ ℕ▹Bits x
1: 1∷ ℕ▹Bits (x ∸ 2^ n)
]′ (2^ n ℕ<= x)
ℕ▹Bits′ : ∀ {n} → ℕ → Bits n
ℕ▹Bits′ = fold 0ⁿ sucB
fromFin : ∀ {n} → Fin (2^ n) → Bits n
fromFin = ℕ▹Bits ∘ Fin▹ℕ
lookupTbl : ∀ {n a} {A : ★ a} → Bits n → Vec A (2^ n) → A
lookupTbl [] (x ∷ []) = x
lookupTbl (0₂ ∷ key) tbl = lookupTbl key (take _ tbl)
lookupTbl {suc n} (1₂ ∷ key) tbl = lookupTbl key (drop (2^ n) tbl)
funFromTbl : ∀ {n a} {A : ★ a} → Vec A (2^ n) → (Bits n → A)
funFromTbl = flip lookupTbl
tblFromFun : ∀ {n a} {A : ★ a} → (Bits n → A) → Vec A (2^ n)
-- tblFromFun f = tabulate (f ∘ fromFin)
tblFromFun {zero} f = f [] ∷ []
tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_) ++ tblFromFun {n} (f ∘ 1∷_)
|
{-# OPTIONS --without-K #-}
module Data.Bits where
open import Algebra
open import Level.NP
open import Type hiding (★)
open import Data.Nat.NP hiding (_==_) renaming (_<=_ to _ℕ<=_)
open import Data.Bit using (Bit)
open import Data.Two renaming (_==_ to _==ᵇ_)
open import Data.Fin.NP using (Fin; zero; suc; inject₁; inject+; raise; Fin▹ℕ)
open import Data.Vec.NP
open import Function.NP
import Data.List.NP as L
-- Re-export some vector functions, maybe they should be given
-- less generic types.
open Data.Vec.NP public using ([]; _∷_; _++_; head; tail; map; replicate; RewireTbl; rewire; rewireTbl; onᵢ)
Bits : ℕ → ★₀
Bits = Vec Bit
_→ᵇ_ : ℕ → ℕ → ★₀
i →ᵇ o = Bits i → Bits o
0ⁿ : ∀ {n} → Bits n
0ⁿ = replicate 0₂
-- Notice that all empty vectors are the same, hence 0ⁿ {0} ≡ 1ⁿ {0}
1ⁿ : ∀ {n} → Bits n
1ⁿ = replicate 1₂
pattern 0∷_ xs = 0₂ ∷ xs
pattern 1∷_ xs = 1₂ ∷ xs
{-
0∷_ : ∀ {n} → Bits n → Bits (suc n)
0∷ xs = 0₂ ∷ xs
-- can't we make these pattern aliases?
1∷_ : ∀ {n} → Bits n → Bits (suc n)
1∷ xs = 1₂ ∷ xs
-}
_!_ : ∀ {a n} {A : ★ a} → Vec A n → Fin n → A
_!_ = flip lookup
-- see Data.Bits.Properties
_==_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bit
[] == [] = 1₂
(b₀ ∷ bs₀) == (b₁ ∷ bs₁) = (b₀ ==ᵇ b₁) ∧ bs₀ == bs₁
_<=_ : ∀ {n} (xs ys : Bits n) → Bit
[] <= [] = 1₂
(1₂ ∷ xs) <= (0₂ ∷ ys) = 0₂
(0₂ ∷ xs) <= (1₂ ∷ ys) = 1₂
(_ ∷ xs) <= (_ ∷ ys) = xs <= ys
infixr 5 _⊕_
_⊕_ : ∀ {n} (bs₀ bs₁ : Bits n) → Bits n
_⊕_ = zipWith _xor_
⊕-group : ℕ → Group ₀ ₀
⊕-group = LiftGroup.group Xor°.+-group
module ⊕-Group (n : ℕ) = Group (⊕-group n)
module ⊕-Monoid (n : ℕ) = Monoid (⊕-Group.monoid n)
-- Negate all bits, i.e. "xor"ing them by one.
vnot : ∀ {n} → Endo (Bits n)
vnot = _⊕_ 1ⁿ
-- Negate the i-th bit.
notᵢ : ∀ {n} (i : Fin n) → Bits n → Bits n
notᵢ = onᵢ not
msb : ∀ k {n} → Bits (k + n) → Bits k
msb = take
lsb : ∀ k {n} → Bits (n + k) → Bits k
lsb _ {n} = drop n
msb₂ : ∀ {n} → Bits (2 + n) → Bits 2
msb₂ = msb 2
lsb₂ : ∀ {n} → Bits (2 + n) → Bits 2
lsb₂ = reverse ∘ msb 2 ∘ reverse
#1 : ∀ {n} → Bits n → Fin (suc n)
#1 [] = zero
#1 (0∷ bs) = inject₁ (#1 bs)
#1 (1∷ bs) = suc (#1 bs)
#0 : ∀ {n} → Bits n → Fin (suc n)
#0 = #1 ∘ map not
allBitsL : ∀ n → L.List (Bits n)
allBitsL _ = replicateM (toList (0∷ 1∷ []))
where open L.Monad
allBits : ∀ n → Vec (Bits n) (2^ n)
allBits zero = [] ∷ []
allBits (suc n) = map 0∷_ bs ++ map 1∷_ bs
where bs = allBits n
always : ∀ n → Bits n → Bit
always _ _ = 1₂
never : ∀ n → Bits n → Bit
never _ _ = 0₂
_∨°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit
_∨°_ f g x = f x ∨ g x
_∧°_ : ∀ {n} → (f g : Bits n → Bit) → Bits n → Bit
_∧°_ f g x = f x ∧ g x
not° : ∀ {n} (f : Bits n → Bit) → Bits n → Bit
not° f = not ∘ f
view∷ : ∀ {n a b} {A : ★ a} {B : ★ b} → (A → Vec A n → B) → Vec A (suc n) → B
view∷ f (x ∷ xs) = f x xs
sucBCarry : ∀ {n} → Bits n → Bits (1 + n)
sucBCarry [] = 0∷ []
sucBCarry (0∷ xs) = 0∷ sucBCarry xs
sucBCarry (1∷ xs) = view∷ (λ x xs → x ∷ not x ∷ xs) (sucBCarry xs)
sucB : ∀ {n} → Bits n → Bits n
sucB = tail ∘ sucBCarry
--_[mod_] : ℕ → ℕ → ★₀
--a [mod b ] = DivMod' a b
module ReversedBits where
sucRB : ∀ {n} → Bits n → Bits n
sucRB [] = []
sucRB (0∷ xs) = 1∷ xs
sucRB (1∷ xs) = 0∷ sucRB xs
toFin : ∀ {n} → Bits n → Fin (2^ n)
toFin [] = zero
toFin (0∷ xs) = inject+ _ (toFin xs)
toFin {suc n} (1∷ xs) = raise (2^ n) (toFin xs)
Bits▹ℕ : ∀ {n} → Bits n → ℕ
Bits▹ℕ [] = zero
Bits▹ℕ (0∷ xs) = Bits▹ℕ xs
Bits▹ℕ {suc n} (1∷ xs) = 2^ n + Bits▹ℕ xs
ℕ▹Bits : ∀ {n} → ℕ → Bits n
ℕ▹Bits {zero} _ = []
ℕ▹Bits {suc n} x = [0: 0∷ ℕ▹Bits x
1: 1∷ ℕ▹Bits (x ∸ 2^ n)
]′ (2^ n ℕ<= x)
ℕ▹Bits′ : ∀ {n} → ℕ → Bits n
ℕ▹Bits′ = fold 0ⁿ sucB
fromFin : ∀ {n} → Fin (2^ n) → Bits n
fromFin = ℕ▹Bits ∘ Fin▹ℕ
lookupTbl : ∀ {n a} {A : ★ a} → Bits n → Vec A (2^ n) → A
lookupTbl [] = head
lookupTbl (0∷ key) = lookupTbl key ∘ take _
lookupTbl {suc n} (1∷ key) = lookupTbl key ∘ drop (2^ n)
funFromTbl : ∀ {n a} {A : ★ a} → Vec A (2^ n) → (Bits n → A)
funFromTbl = flip lookupTbl
tblFromFun : ∀ {n a} {A : ★ a} → (Bits n → A) → Vec A (2^ n)
-- tblFromFun f = tabulate (f ∘ fromFin)
tblFromFun {zero} f = f [] ∷ []
tblFromFun {suc n} f = tblFromFun {n} (f ∘ 0∷_)
++ tblFromFun {n} (f ∘ 1∷_)
|
use patterns 0∷ and 1∷
|
Bits: use patterns 0∷ and 1∷
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
4cbed45974f3385d5c182dddefd1bb738b95c793
|
ChangeContexts.agda
|
ChangeContexts.agda
|
module ChangeContexts where
open import IlcModel
open import Changes
open import meaning
-- TYPING CONTEXTS, VARIABLES and WEAKENING
open import binding Type ⟦_⟧Type
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = Δ-Type τ • τ • Δ-Context Γ
update : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧
update {∅} ∅ = ∅
update {τ • Γ} (dv • v • ρ) = apply dv v • update ρ
ignore : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧
ignore {∅} ∅ = ∅
ignore {τ • Γ} (dv • v • ρ) = v • ignore ρ
Δ-Context′ : (Γ : Context) → Prefix Γ → Context
Δ-Context′ Γ ∅ = Δ-Context Γ
Δ-Context′ (.τ • Γ) (τ • Γ′) = τ • Δ-Context′ Γ Γ′
update′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧
update′ ∅ ρ = update ρ
update′ (τ • Γ′) (v • ρ) = v • update′ Γ′ ρ
ignore′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧
ignore′ ∅ ρ = ignore ρ
ignore′ (τ • Γ′) (v • ρ) = v • ignore′ Γ′ ρ
|
module ChangeContexts where
open import IlcModel
open import Changes
open import meaning
-- TYPING CONTEXTS, VARIABLES and WEAKENING
open import binding Type ⟦_⟧Type
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = Δ-Type τ • τ • Δ-Context Γ
update : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧
update {∅} ∅ = ∅
update {τ • Γ} (dv • v • ρ) = apply dv v • update ρ
ignore : ∀ {Γ} → ⟦ Δ-Context Γ ⟧ → ⟦ Γ ⟧
ignore {∅} ∅ = ∅
ignore {τ • Γ} (dv • v • ρ) = v • ignore ρ
-- Δ-Context′: behaves like Δ-Context, but has an extra argument Γ′, a
-- prefix of its first argument which should not be touched.
Δ-Context′ : (Γ : Context) → Prefix Γ → Context
Δ-Context′ Γ ∅ = Δ-Context Γ
Δ-Context′ (.τ • Γ) (τ • Γ′) = τ • Δ-Context′ Γ Γ′
update′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧
update′ ∅ ρ = update ρ
update′ (τ • Γ′) (v • ρ) = v • update′ Γ′ ρ
ignore′ : ∀ {Γ} → (Γ′ : Prefix Γ) → ⟦ Δ-Context′ Γ Γ′ ⟧ → ⟦ Γ ⟧
ignore′ ∅ ρ = ignore ρ
ignore′ (τ • Γ′) (v • ρ) = v • ignore′ Γ′ ρ
|
document a function
|
agda: document a function
Old-commit-hash: c0550a6f2e84b20bc63a5c0f91a168988125b117
|
Agda
|
mit
|
inc-lc/ilc-agda
|
70907b2ea28e2d6559f103705f74a8fefbca6f63
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : (ι : Base) → ⟦ ι ⟧Base → Set
apply-change-base : ∀ ι → (v : ⟦ ι ⟧Base) → Change-base ι v → ⟦ ι ⟧Base
diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : (τ : Type) → ⟦ τ ⟧ → Set
nil-change : ∀ τ v → Change τ v
apply-change : ∀ τ → (v : ⟦ τ ⟧) (dv : Change τ v) → ⟦ τ ⟧
diff-change : ∀ τ → (u v : ⟦ τ ⟧) → Change τ v
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
Change (base ι) v = Change-base ι v
Change (σ ⇒ τ) f = Pair
(∀ v → Change σ v → Change τ (f v))
(λ Δf → ∀ v dv →
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ Δf (v ⊞₍ σ ₎ dv) (nil-change σ (v ⊞₍ σ ₎ dv)) ≡ f v ⊞₍ τ ₎ Δf v dv)
before : ∀ {τ v} → Change τ v → ⟦ τ ⟧
before {τ} {v} _ = v
after : ∀ {τ v} → Change τ v → ⟦ τ ⟧
after {τ} {v} dv = v ⊞₍ τ ₎ dv
open Pair public using () renaming
( cdr to is-valid
; car to call-change
)
nil-change τ v = diff-change τ v v
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
apply-change (base ι) n Δn = apply-change-base ι n Δn
apply-change (σ ⇒ τ) f Δf = λ v → f v ⊞₍ τ ₎ call-change Δf v (nil-change σ v)
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
diff-change (base ι) m n = diff-change-base ι m n
diff-change (σ ⇒ τ) g f = cons (λ v dv → g (after {σ} dv) ⊟₍ τ ₎ f v)
(λ v dv →
begin
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g ((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ cong (λ □ → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g □ ⊟₍ τ ₎ (f (v ⊞₍ σ ₎ dv))))
(v+[u-v]=u {σ} {v ⊞₍ σ ₎ dv} {v ⊞₍ σ ₎ dv}) ⟩
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f (v ⊞₍ σ ₎ dv)} ⟩
g (v ⊞₍ σ ₎ dv)
≡⟨ sym (v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f v} ) ⟩
f v ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v)
∎) where open ≡-Reasoning
-- call this lemma "replace"?
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(apply-change (σ ⇒ τ) v (diff-change (σ ⇒ τ) u v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → (v : ⟦ τ ⟧) → Change τ v → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → (u v : ⟦ τ ⟧) → Change τ v
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
data ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set where
∅ : ΔEnv ∅ ∅
_•_ : ∀ {τ Γ v ρ} →
(dv : Change τ v) →
(dρ : ΔEnv Γ ρ) →
ΔEnv (τ • Γ) (v • ρ)
ignore : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
ignore {Γ} {ρ} _ = ρ
update : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
update ∅ = ∅
update {τ • Γ} (dv • dρ) = after {τ} dv • update dρ
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
Change-base : (ι : Base) → ⟦ ι ⟧Base → Set
apply-change-base : ∀ ι → (v : ⟦ ι ⟧Base) → Change-base ι v → ⟦ ι ⟧Base
diff-change-base : ∀ ι → (u v : ⟦ ι ⟧Base) → Change-base ι v
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-change-base ι v (diff-change-base ι u v) ≡ u
---------------
-- Interface --
---------------
Change : (τ : Type) → ⟦ τ ⟧ → Set
nil-change : ∀ τ v → Change τ v
apply-change : ∀ τ → (v : ⟦ τ ⟧) (dv : Change τ v) → ⟦ τ ⟧
diff-change : ∀ τ → (u v : ⟦ τ ⟧) → Change τ v
infixl 6 apply-change diff-change -- as with + - in GHC.Num
syntax apply-change τ v dv = v ⊞₍ τ ₎ dv
syntax diff-change τ u v = u ⊟₍ τ ₎ v
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (Change τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (Change (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- Change τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- Change : Type → Set
Change (base ι) v = Change-base ι v
Change (σ ⇒ τ) f = Pair
(∀ v → Change σ v → Change τ (f v))
(λ Δf → ∀ v dv →
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ Δf (v ⊞₍ σ ₎ dv) (nil-change σ (v ⊞₍ σ ₎ dv)) ≡ f v ⊞₍ τ ₎ Δf v dv)
open Pair public using () renaming
( cdr to is-valid
; car to call-change
)
nil-change τ v = diff-change τ v v
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → Change τ → ⟦ τ ⟧
apply-change (base ι) n Δn = apply-change-base ι n Δn
apply-change (σ ⇒ τ) f Δf = λ v → f v ⊞₍ τ ₎ call-change Δf v (nil-change σ v)
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → Change τ
diff-change (base ι) m n = diff-change-base ι m n
diff-change (σ ⇒ τ) g f = cons (λ v dv → g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v)
(λ v dv →
begin
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g ((v ⊞₍ σ ₎ dv) ⊞₍ σ ₎ ((v ⊞₍ σ ₎ dv) ⊟₍ σ ₎ (v ⊞₍ σ ₎ dv))) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ cong (λ □ → f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g □ ⊟₍ τ ₎ (f (v ⊞₍ σ ₎ dv))))
(v+[u-v]=u {σ} {v ⊞₍ σ ₎ dv} {v ⊞₍ σ ₎ dv}) ⟩
f (v ⊞₍ σ ₎ dv) ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f (v ⊞₍ σ ₎ dv))
≡⟨ v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f (v ⊞₍ σ ₎ dv)} ⟩
g (v ⊞₍ σ ₎ dv)
≡⟨ sym (v+[u-v]=u {τ} {g (v ⊞₍ σ ₎ dv)} {f v} ) ⟩
f v ⊞₍ τ ₎ (g (v ⊞₍ σ ₎ dv) ⊟₍ τ ₎ f v)
∎) where open ≡-Reasoning
-- call this lemma "replace"?
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(apply-change (σ ⇒ τ) v (diff-change (σ ⇒ τ) u v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → (v : ⟦ τ ⟧) → Change τ v → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → (u v : ⟦ τ ⟧) → Change τ v
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
-- abbrevitations
before : ∀ {τ v} → Change τ v → ⟦ τ ⟧
before {τ} {v} _ = v
after : ∀ {τ v} → Change τ v → ⟦ τ ⟧
after {τ} {v} dv = v ⊞₍ τ ₎ dv
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
data ΔEnv : ∀ (Γ : Context) → ⟦ Γ ⟧ → Set where
∅ : ΔEnv ∅ ∅
_•_ : ∀ {τ Γ v ρ} →
(dv : Change τ v) →
(dρ : ΔEnv Γ ρ) →
ΔEnv (τ • Γ) (v • ρ)
ignore : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
ignore {Γ} {ρ} _ = ρ
update : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : ΔEnv Γ ρ) → ⟦ Γ ⟧
update ∅ = ∅
update {τ • Γ} (dv • dρ) = after {τ} dv • update dρ
|
Move before and after out of the mutually dependent block.
|
Move before and after out of the mutually dependent block.
Old-commit-hash: 1d7a74aa7d62457de4936eae9faaeb605d739bf3
|
Agda
|
mit
|
inc-lc/ilc-agda
|
6f7fde98554f01770bfdbd43711ab8e45eab8b9b
|
Control/Protocol.agda
|
Control/Protocol.agda
|
{-# OPTIONS --without-K #-}
open import Function.NP
open import Type
open import Level.NP
open import Data.Product.NP using (Σ; _×_; _,_) renaming (proj₁ to fst)
open import Data.One using (𝟙)
open import Relation.Binary.PropositionalEquality.NP using (_≡_; !_; _∙_; refl; ap; coe; coe!)
open import Function.Extensionality
open import HoTT
open import Data.ShapePolymorphism
open Equivalences
module Control.Protocol where
data InOut : ★ where
In Out : InOut
dualᴵᴼ : InOut → InOut
dualᴵᴼ In = Out
dualᴵᴼ Out = In
dualᴵᴼ-involutive : ∀ io → dualᴵᴼ (dualᴵᴼ io) ≡ io
dualᴵᴼ-involutive In = refl
dualᴵᴼ-involutive Out = refl
dualᴵᴼ-equiv : Is-equiv dualᴵᴼ
dualᴵᴼ-equiv = self-inv-is-equiv dualᴵᴼ-involutive
dualᴵᴼ-inj : ∀ {x y} → dualᴵᴼ x ≡ dualᴵᴼ y → x ≡ y
dualᴵᴼ-inj = Is-equiv.injective dualᴵᴼ-equiv
{-
module UniversalProtocols ℓ {U : ★_(ₛ ℓ)}(U⟦_⟧ : U → ★_ ℓ) where
-}
module _ ℓ where
U = ★_ ℓ
U⟦_⟧ = id
data Proto_ : ★_(ₛ ℓ) where
end : Proto_
com : (io : InOut){M : U}(P : U⟦ M ⟧ → Proto_) → Proto_
{-
module U★ ℓ = UniversalProtocols ℓ {★_ ℓ} id
open U★
-}
Proto : ★₁
Proto = Proto_ ₀
Proto₀ = Proto
Proto₁ = Proto_ ₁
pattern recv P = com In P
pattern send P = com Out P
module _ {{_ : FunExt}} where
com= : ∀ {io₀ io₁}(io= : io₀ ≡ io₁)
{M₀ M₁}(M= : M₀ ≡ M₁)
{P₀ : M₀ → Proto}{P₁ : M₁ → Proto}(P= : ∀ m₀ → P₀ m₀ ≡ P₁ (coe M= m₀))
→ com io₀ P₀ ≡ com io₁ P₁
com= refl refl P= = ap (com _) (λ= P=)
module _ {io₀ io₁}(io= : io₀ ≡ io₁)
{M₀ M₁}(M≃ : M₀ ≃ M₁)
{P₀ : M₀ → Proto}{P₁ : M₁ → Proto}
(P= : ∀ m₀ → P₀ m₀ ≡ P₁ (–> M≃ m₀))
{{_ : UA}} where
com≃ : com io₀ P₀ ≡ com io₁ P₁
com≃ = com= io= (ua M≃) λ m → P= m ∙ ap P₁ (! coe-β M≃ m)
module _ io {M N}(P : M × N → Proto)
where
com₂ : Proto
com₂ = com io λ m → com io λ n → P (m , n)
{- Proving this would be awesome...
module _ io
{M₀ M₁ N₀ N₁ : ★}
(M×N≃ : (M₀ × N₀) ≃ (M₁ × N₁))
{P₀ : M₀ × N₀ → Proto}{P₁ : M₁ × N₁ → Proto}
(P= : ∀ m,n₀ → P₀ m,n₀ ≡ P₁ (–> M×N≃ m,n₀))
{{_ : UA}} where
com₂≃ : com₂ io P₀ ≡ com₂ io P₁
com₂≃ = {!com=!}
-}
send= = com= {Out} refl
send≃ = com≃ {Out} refl
recv= = com= {In} refl
recv≃ = com≃ {In} refl
com=′ : ∀ io {M}{P₀ P₁ : M → Proto}(P= : ∀ m → P₀ m ≡ P₁ m) → com io P₀ ≡ com io P₁
com=′ io = com= refl refl
send=′ : ∀ {M}{P₀ P₁ : M → Proto}(P= : ∀ m → P₀ m ≡ P₁ m) → send P₀ ≡ send P₁
send=′ = send= refl
recv=′ : ∀ {M}{P₀ P₁ : M → Proto}(P= : ∀ m → P₀ m ≡ P₁ m) → recv P₀ ≡ recv P₁
recv=′ = recv= refl
pattern recvE M P = com In {M} P
pattern sendE M P = com Out {M} P
recv☐ : {M : ★}(P : ..(_ : M) → Proto) → Proto
recv☐ P = recv (λ { [ m ] → P m })
send☐ : {M : ★}(P : ..(_ : M) → Proto) → Proto
send☐ P = send (λ { [ m ] → P m })
{-
dual : Proto → Proto
dual end = end
dual (send P) = recv λ m → dual (P m)
dual (recv P) = send λ m → dual (P m)
-}
dual : Proto → Proto
dual end = end
dual (com io P) = com (dualᴵᴼ io) λ m → dual (P m)
source-of : Proto → Proto
source-of end = end
source-of (com _ P) = send λ m → source-of (P m)
sink-of : Proto → Proto
sink-of = dual ∘ source-of
data IsSource : Proto → ★₁ where
end : IsSource end
send' : ∀ {M P} (PT : (m : M) → IsSource (P m)) → IsSource (send P)
data IsSink : Proto → ★₁ where
end : IsSink end
recv' : ∀ {M P} (PT : (m : M) → IsSink (P m)) → IsSink (recv P)
data Proto☐ : Proto → ★₁ where
end : Proto☐ end
com : ∀ io {M P} (P☐ : ∀ (m : ☐ M) → Proto☐ (P m)) → Proto☐ (com io P)
record End_ ℓ : ★_ ℓ where
constructor end
End : ∀ {ℓ} → ★_ ℓ
End = End_ _
End-equiv : End ≃ 𝟙
End-equiv = equiv {₀} _ _ (λ _ → refl) (λ _ → refl)
End-uniq : {{_ : UA}} → End ≡ 𝟙
End-uniq = ua End-equiv
⟦_⟧ᴵᴼ : InOut → ∀{ℓ}(M : ★_ ℓ)(P : M → ★_ ℓ) → ★_ ℓ
⟦_⟧ᴵᴼ In = Π
⟦_⟧ᴵᴼ Out = Σ
⟦_⟧ : ∀ {ℓ} → Proto_ ℓ → ★_ ℓ
⟦ end ⟧ = End
⟦ com io P ⟧ = ⟦ io ⟧ᴵᴼ _ λ m → ⟦ P m ⟧
⟦_⊥⟧ : Proto → ★
⟦ P ⊥⟧ = ⟦ dual P ⟧
Log : Proto → ★
Log P = ⟦ source-of P ⟧
Sink : Proto → ★
Sink P = ⟦ sink-of P ⟧
sink : ∀ P → Sink P
sink end = _
sink (com _ P) x = sink (P x)
telecom : ∀ P → ⟦ P ⟧ → ⟦ P ⊥⟧ → Log P
telecom end _ _ = _
telecom (recv P) p (m , q) = m , telecom (P m) (p m) q
telecom (send P) (m , p) q = m , telecom (P m) p (q m)
send′ : ★ → Proto → Proto
send′ M P = send λ (_ : M) → P
recv′ : ★ → Proto → Proto
recv′ M P = recv λ (_ : M) → P
module _ {{_ : FunExt}} where
dual-involutive : ∀ P → dual (dual P) ≡ P
dual-involutive end = refl
dual-involutive (com io P) = com= (dualᴵᴼ-involutive io) refl λ m → dual-involutive (P m)
dual-equiv : Is-equiv dual
dual-equiv = self-inv-is-equiv dual-involutive
dual-inj : ∀ {P Q} → dual P ≡ dual Q → P ≡ Q
dual-inj = Is-equiv.injective dual-equiv
source-of-idempotent : ∀ P → source-of (source-of P) ≡ source-of P
source-of-idempotent end = refl
source-of-idempotent (com _ P) = com= refl refl λ m → source-of-idempotent (P m)
source-of-dual-oblivious : ∀ P → source-of (dual P) ≡ source-of P
source-of-dual-oblivious end = refl
source-of-dual-oblivious (com _ P) = com= refl refl λ m → source-of-dual-oblivious (P m)
module _ {{_ : FunExt}} where
sink-contr : ∀ P s → sink P ≡ s
sink-contr end s = refl
sink-contr (com _ P) s = λ= λ m → sink-contr (P m) (s m)
Sink-is-contr : ∀ P → Is-contr (Sink P)
Sink-is-contr P = sink P , sink-contr P
𝟙≃Sink : ∀ P → 𝟙 ≃ Sink P
𝟙≃Sink P = Is-contr-to-Is-equiv.𝟙≃ (Sink-is-contr P)
dual-Log : ∀ P → Log (dual P) ≡ Log P
dual-Log = ap ⟦_⟧ ∘ source-of-dual-oblivious
|
module Control.Protocol where
open import Control.Protocol.InOut public
open import Control.Protocol.End public
open import Control.Protocol.Core public
|
Split Control.Protocol
|
Split Control.Protocol
|
Agda
|
bsd-3-clause
|
crypto-agda/protocols
|
81eecc922c3dc4fc227770d378988bd4c8881616
|
ZK/GroupHom.agda
|
ZK/GroupHom.agda
|
{-# OPTIONS --without-K #-}
open import Type using (Type)
open import Type.Eq using (Eq?; _==_; ≡⇒==; ==⇒≡)
open import Function using (case_of_)
open import Data.Sum renaming (inj₁ to inl; inj₂ to inr)
open import Data.Product renaming (proj₁ to fst; proj₂ to snd)
open import Relation.Binary.PropositionalEquality.Base using (_≡_; _≢_; idp; ap; ap₂; !_; _∙_; module ≡-Reasoning)
open import Algebra.Group
open import Algebra.Group.Homomorphism
import ZK.SigmaProtocol.KnownStatement
module ZK.GroupHom
{G+ G* : Type} (grp-G+ : Group G+) (grp-G* : Group G*)
{{eq?-G* : Eq? G*}}
(open Additive-Group grp-G+ hiding (_⊗_))
(open Multiplicative-Group grp-G* hiding (_^_))
{C : Type} -- e.g. C ⊆ ℕ
(_>_ : C → C → Type)
(swap? : {c₀ c₁ : C} → c₀ ≢ c₁ → (c₀ > c₁) ⊎ (c₁ > c₀))
(_⊗_ : G+ → C → G+)
(_^_ : G* → C → G*)
(_−ᶜ_ : C → C → C)
(1/ : C → C)
(φ : G+ → G*) -- For instance φ(x) = g^x
(φ-hom : GroupHomomorphism grp-G+ grp-G* φ)
(φ-hom-iterated : ∀ {x c} → φ (x ⊗ c) ≡ φ x ^ c)
(Y : G*)
(^-−ᶜ : ∀ {c₀ c₁}(c> : c₀ > c₁) → Y ^(c₀ −ᶜ c₁) ≡ (Y ^ c₀) / (Y ^ c₁))
(^-^-1/ : ∀ {c₀ c₁}(c> : c₀ > c₁) → (Y ^(c₀ −ᶜ c₁))^(1/(c₀ −ᶜ c₁)) ≡ Y)
where
Commitment = G*
Challenge = C
Response = G+
Witness = G+
Randomness = G+
_∈y : Witness → Type
x ∈y = φ x ≡ Y
open ZK.SigmaProtocol.KnownStatement Commitment Challenge Response Randomness Witness _∈y public
prover : Prover
prover a x = φ a , response
where response : Challenge → Response
response c = (x ⊗ c) + a
verifier : Verifier
verifier (mk A c s) = φ s == ((Y ^ c) * A)
-- We still call it Schnorr since essentially not much changed.
-- The scheme abstracts away g^ as an homomorphism called φ
-- and one get to distinguish between the types C vs G+ and
-- their operations _⊗_ vs _*_.
Schnorr : Σ-Protocol
Schnorr = (prover , verifier)
-- The simulator shows why it is so important for the
-- challenge to be picked once the commitment is known.
-- To fake a transcript, the challenge and response can
-- be arbitrarily chosen. However to be indistinguishable
-- from a valid proof they need to be picked at random.
simulator : Simulator
simulator c s = A
where
-- Compute A, such that the verifier accepts!
A = (Y ^ c)⁻¹ * φ s
swap-t²? : Transcript²[ _≢_ ] verifier → Transcript²[ _>_ ] verifier
swap-t²? t² = t²'
module Swap? where
open Transcript² t² public
using (verify₀; verify₁; c₀≢c₁)
renaming ( get-A to A
; get-f₀ to r₀; get-f₁ to r₁
; get-c₀ to c₀; get-c₁ to c₁
)
t²' = case swap? c₀≢c₁ of λ
{ (inl c₀>c₁) → mk A c₀ c₁ c₀>c₁ r₀ r₁ verify₀ verify₁
; (inr c₀<c₁) → mk A c₁ c₀ c₀<c₁ r₁ r₀ verify₁ verify₀
}
-- The extractor shows the importance of never reusing a
-- commitment. If the prover answers to two different challenges
-- comming from the same commitment then the knowledge of the
-- prover (the witness) can be extracted.
extractor : Extractor verifier
extractor t² = x'
module Witness-extractor where
open Transcript² (swap-t²? t²) public
using (verify₀; verify₁)
renaming ( get-A to A
; get-f₀ to r₀; get-f₁ to r₁
; get-c₀ to c₀; get-c₁ to c₁
; c₀≢c₁ to c₀>c₁
)
rd = r₀ − r₁
cd = c₀ −ᶜ c₁
1/cd = 1/ cd
x' = rd ⊗ 1/cd
open ≡-Reasoning
open GroupHomomorphism φ-hom
correct : Correct Schnorr
correct x a c w
= ≡⇒== (φ((x ⊗ c) + a) ≡⟨ hom ⟩
φ(x ⊗ c) * A ≡⟨ *= φ-hom-iterated idp ⟩
(φ x ^ c) * A ≡⟨ ap (λ z → (z ^ c) * A) w ⟩
(Y ^ c) * A ∎)
where
A = φ a
shvzk : Special-Honest-Verifier-Zero-Knowledge Schnorr
shvzk = record { simulator = simulator
; correct-simulator =
λ _ _ → ≡⇒== (! elim-*-!assoc= (snd ⁻¹-inverse)) }
module _ (t² : Transcript² verifier) where
open Witness-extractor t²
φrd≡ycd : _
φrd≡ycd
= φ rd ≡⟨by-definition⟩
φ (r₀ − r₁) ≡⟨ −-/ ⟩
φ r₀ / φ r₁ ≡⟨ /= (==⇒≡ verify₀) (==⇒≡ verify₁) ⟩
(Y ^ c₀ * A) / (Y ^ c₁ * A) ≡⟨ elim-*-right-/ ⟩
Y ^ c₀ / Y ^ c₁ ≡⟨ ! ^-−ᶜ c₀>c₁ ⟩
Y ^(c₀ −ᶜ c₁) ≡⟨by-definition⟩
Y ^ cd ∎
-- The extracted x is correct
extractor-ok : φ x' ≡ Y
extractor-ok
= φ(rd ⊗ 1/cd) ≡⟨ φ-hom-iterated ⟩
φ rd ^ 1/cd ≡⟨ ap₂ _^_ φrd≡ycd idp ⟩
(Y ^ cd)^ 1/cd ≡⟨ ^-^-1/ c₀>c₁ ⟩
Y ∎
special-soundness : Special-Soundness Schnorr
special-soundness = record { extractor = extractor
; extract-valid-witness = extractor-ok }
special-Σ-protocol : Special-Σ-Protocol
special-Σ-protocol = record { Σ-protocol = Schnorr ; correct = correct ; shvzk = shvzk ; ssound = special-soundness }
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type using (Type)
open import Type.Eq using (Eq?; _==_; ≡⇒==; ==⇒≡)
open import Function using (case_of_)
open import Data.Sum renaming (inj₁ to inl; inj₂ to inr)
open import Data.Product renaming (proj₁ to fst; proj₂ to snd)
open import Relation.Binary.PropositionalEquality.Base using (_≡_; _≢_; idp; ap; ap₂; !_; _∙_; module ≡-Reasoning)
open import Algebra.Group
open import Algebra.Group.Homomorphism
import ZK.SigmaProtocol.KnownStatement
module ZK.GroupHom
{G+ G* : Type} (grp-G+ : Group G+) (grp-G* : Group G*)
{{eq?-G* : Eq? G*}}
(open Additive-Group grp-G+ hiding (_⊗_))
(open Multiplicative-Group grp-G* hiding (_^_))
{C : Type} -- e.g. C ⊆ ℕ
(_>_ : C → C → Type)
(swap? : {c₀ c₁ : C} → c₀ ≢ c₁ → (c₀ > c₁) ⊎ (c₁ > c₀))
(_⊗_ : G+ → C → G+)
(_^_ : G* → C → G*)
(_−ᶜ_ : C → C → C)
(1/ : C → C)
(φ : G+ → G*) -- For instance φ(x) = g^x
(φ-hom : GroupHomomorphism grp-G+ grp-G* φ)
(φ-hom-iterated : ∀ {x c} → φ (x ⊗ c) ≡ φ x ^ c)
(Y : G*)
(^-−ᶜ : ∀ {c₀ c₁}(c> : c₀ > c₁) → Y ^(c₀ −ᶜ c₁) ≡ (Y ^ c₀) / (Y ^ c₁))
(^-^-1/ : ∀ {c₀ c₁}(c> : c₀ > c₁) → (Y ^(c₀ −ᶜ c₁))^(1/(c₀ −ᶜ c₁)) ≡ Y)
where
Commitment = G*
Challenge = C
Response = G+
Witness = G+
Randomness = G+
_∈y : Witness → Type
x ∈y = φ x ≡ Y
open ZK.SigmaProtocol.KnownStatement Commitment Challenge Response Randomness Witness _∈y public
prover : Prover
prover a x = φ a , response
where response : Challenge → Response
response c = (x ⊗ c) + a
verifier' : (A : Commitment)(c : Challenge)(s : Response) → _
verifier' A c s = φ s == ((Y ^ c) * A)
verifier : Verifier
verifier (mk A c s) = verifier' A c s
-- We still call it Schnorr since essentially not much changed.
-- The scheme abstracts away g^ as an homomorphism called φ
-- and one get to distinguish between the types C vs G+ and
-- their operations _⊗_ vs _*_.
Schnorr : Σ-Protocol
Schnorr = (prover , verifier)
-- The simulator shows why it is so important for the
-- challenge to be picked once the commitment is known.
-- To fake a transcript, the challenge and response can
-- be arbitrarily chosen. However to be indistinguishable
-- from a valid proof they need to be picked at random.
simulator : Simulator
simulator c s = A
where
-- Compute A, such that the verifier accepts!
A = (Y ^ c)⁻¹ * φ s
swap-t²? : Transcript²[ _≢_ ] verifier → Transcript²[ _>_ ] verifier
swap-t²? t² = t²'
module Swap? where
open Transcript² t² public
using (verify₀; verify₁; c₀≢c₁)
renaming ( get-A to A
; get-f₀ to r₀; get-f₁ to r₁
; get-c₀ to c₀; get-c₁ to c₁
)
t²' = case swap? c₀≢c₁ of λ
{ (inl c₀>c₁) → mk A c₀ c₁ c₀>c₁ r₀ r₁ verify₀ verify₁
; (inr c₀<c₁) → mk A c₁ c₀ c₀<c₁ r₁ r₀ verify₁ verify₀
}
-- The extractor shows the importance of never reusing a
-- commitment. If the prover answers to two different challenges
-- comming from the same commitment then the knowledge of the
-- prover (the witness) can be extracted.
extractor : Extractor verifier
extractor t² = x'
module Witness-extractor where
open Transcript² (swap-t²? t²) public
using (verify₀; verify₁)
renaming ( get-A to A
; get-f₀ to r₀; get-f₁ to r₁
; get-c₀ to c₀; get-c₁ to c₁
; c₀≢c₁ to c₀>c₁
)
rd = r₀ − r₁
cd = c₀ −ᶜ c₁
1/cd = 1/ cd
x' = rd ⊗ 1/cd
open ≡-Reasoning
open GroupHomomorphism φ-hom
correct : Correct Schnorr
correct x a c w
= ≡⇒== (φ((x ⊗ c) + a) ≡⟨ hom ⟩
φ(x ⊗ c) * A ≡⟨ *= φ-hom-iterated idp ⟩
(φ x ^ c) * A ≡⟨ ap (λ z → (z ^ c) * A) w ⟩
(Y ^ c) * A ∎)
where
A = φ a
shvzk : Special-Honest-Verifier-Zero-Knowledge Schnorr
shvzk = record { simulator = simulator
; correct-simulator =
λ _ _ → ≡⇒== (! elim-*-!assoc= (snd ⁻¹-inverse)) }
module _ (t² : Transcript² verifier) where
open Witness-extractor t²
φrd≡ycd : _
φrd≡ycd
= φ rd ≡⟨by-definition⟩
φ (r₀ − r₁) ≡⟨ −-/ ⟩
φ r₀ / φ r₁ ≡⟨ /= (==⇒≡ verify₀) (==⇒≡ verify₁) ⟩
(Y ^ c₀ * A) / (Y ^ c₁ * A) ≡⟨ elim-*-right-/ ⟩
Y ^ c₀ / Y ^ c₁ ≡⟨ ! ^-−ᶜ c₀>c₁ ⟩
Y ^(c₀ −ᶜ c₁) ≡⟨by-definition⟩
Y ^ cd ∎
-- The extracted x is correct
extractor-ok : φ x' ≡ Y
extractor-ok
= φ(rd ⊗ 1/cd) ≡⟨ φ-hom-iterated ⟩
φ rd ^ 1/cd ≡⟨ ap₂ _^_ φrd≡ycd idp ⟩
(Y ^ cd)^ 1/cd ≡⟨ ^-^-1/ c₀>c₁ ⟩
Y ∎
special-soundness : Special-Soundness Schnorr
special-soundness = record { extractor = extractor
; extract-valid-witness = extractor-ok }
special-Σ-protocol : Special-Σ-Protocol
special-Σ-protocol = record { Σ-protocol = Schnorr ; correct = correct ; shvzk = shvzk ; ssound = special-soundness }
-- -}
-- -}
-- -}
-- -}
|
split the verifier function
|
GroupHom: split the verifier function
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.