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
|
---|---|---|---|---|---|---|---|---|---|
07045b7aa32a14de23ec233029b6fc6134b24685
|
experiment/liftVec.agda
|
experiment/liftVec.agda
|
open import Function
open import Data.Nat using (ℕ ; suc ; zero)
open import Data.Fin using (Fin)
open import Data.Vec
open import Data.Vec.N-ary.NP
open import Relation.Binary.PropositionalEquality
module liftVec where
module single (A : Set)(_+_ : A → A → A)where
data Tm : Set where
var : Tm
cst : A → Tm
fun : Tm → Tm → Tm
eval : Tm → A → A
eval var x = x
eval (cst c) x = c
eval (fun tm tm') x = eval tm x + eval tm' x
veval : {m : ℕ} → Tm → Vec A m → Vec A m
veval var xs = xs
veval (cst x) xs = replicate x
veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs)
record Fm : Set where
constructor _=='_
field
lhs : Tm
rhs : Tm
s[_] : Fm → Set
s[ lhs ==' rhs ] = (x : A) → eval lhs x ≡ eval rhs x
v[_] : Fm → Set
v[ lhs ==' rhs ] = {m : ℕ}(xs : Vec A m) → veval lhs xs ≡ veval rhs xs
lemma1 : (t : Tm) → veval t [] ≡ []
lemma1 var = refl
lemma1 (cst x) = refl
lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl
lemma2 : {m : ℕ}(x : A)(xs : Vec A m)(t : Tm) → veval t (x ∷ xs) ≡ eval t x ∷ veval t xs
lemma2 _ _ var = refl
lemma2 _ _ (cst c) = refl
lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl
prf : (t : Fm) → s[ t ] → v[ t ]
prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl
prf (l ==' r) pr (x ∷ xs) rewrite lemma2 x xs l | lemma2 x xs r = cong₂ (_∷_) (pr x) (prf (l ==' r) pr xs)
module MultiDataType {A : Set} {nrVar : ℕ} where
data Tm : Set where
var : Fin nrVar → Tm
cst : A → Tm
fun : Tm → Tm → Tm
infixl 6 _+'_
_+'_ : Tm → Tm → Tm
_+'_ = fun
infix 4 _≡'_
record TmEq : Set where
constructor _≡'_
field
lhs rhs : Tm
module multi (A : Set)(_+_ : A → A → A)(nrVar : ℕ) where
open MultiDataType {A} {nrVar} public
sEnv : Set
sEnv = Vec A nrVar
vEnv : ℕ → Set
vEnv m = Vec (Vec A m) nrVar
Var : Set
Var = Fin nrVar
_!_ : {X : Set} → Vec X nrVar → Var → X
E ! v = lookup v E
eval : Tm → sEnv → A
eval (var x) G = G ! x
eval (cst c) G = c
eval (fun t t') G = eval t G + eval t' G
veval : {m : ℕ} → Tm → vEnv m → Vec A m
veval (var x) G = G ! x
veval (cst c) G = replicate c
veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G)
lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) → veval t xs ≡ []
lemma1 {xs} (var x) with xs ! x
... | [] = refl
lemma1 (cst x) = refl
lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl
lemVar : {m n : ℕ}(G : Vec (Vec A (suc m)) n)(i : Fin n) → lookup i G ≡ lookup i (map head G) ∷ lookup i (map tail G)
lemVar [] ()
lemVar ((x ∷ G) ∷ G₁) Data.Fin.zero = refl
lemVar (G ∷ G') (Data.Fin.suc i) = lemVar G' i
lemma2 : {n : ℕ}(xs : vEnv (suc n))(t : Tm) → veval t xs ≡ eval t (map head xs) ∷ veval t (map tail xs)
lemma2 G (var x) = lemVar G x
lemma2 _ (cst x) = refl
lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl
s[_==_] : Tm → Tm → Set
s[ l == r ] = (G : Vec A nrVar) → eval l G ≡ eval r G
v[_==_] : Tm → Tm → Set
v[ l == r ] = {m : ℕ}(G : Vec (Vec A m) nrVar) → veval l G ≡ veval r G
prf : (l r : Tm) → s[ l == r ] → v[ l == r ]
prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl
prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r
= cong₂ _∷_ (pr (replicate head ⊛ G)) (prf l r pr (map tail G))
module Full (A : Set)(_+_ : A → A → A) (m : ℕ) n where
open multi A _+_ n public
solve : ∀ (l r : Tm) → ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G))
→ ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G))
solve l r x = curryⁿ {n} {A = Vec A m} {B = curryⁿ′ f}
(λ xs → subst id (sym (curry-$ⁿ′ f xs))
(prf l r (λ G → subst id (curry-$ⁿ′ g G) (x $ⁿ G)) xs))
where f = λ G → veval {m} l G ≡ veval r G
g = λ G → eval l G ≡ eval r G
mkTm : N-ary n Tm TmEq → TmEq
mkTm = go (tabulate id) where
go : {m : ℕ} -> Vec (Fin n) m → N-ary m Tm TmEq → TmEq
go [] f = f
go (x ∷ args) f = go args (f (var x))
prove : ∀ (t : N-ary n Tm TmEq) →
let
l = TmEq.lhs (mkTm t)
r = TmEq.rhs (mkTm t)
in ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G))
→ ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G))
prove t x = solve (TmEq.lhs (mkTm t)) (TmEq.rhs (mkTm t)) x
open import Data.Bool
module example where
open import Data.Fin
open import Data.Bool.NP
open import Data.Product using (Σ ; proj₁)
module VecBoolXorProps {m} = Full Bool _xor_ m
open MultiDataType
coolTheorem : {m : ℕ} → (xs ys : Vec Bool m) → zipWith _xor_ xs ys ≡ zipWith _xor_ ys xs
coolTheorem = VecBoolXorProps.prove 2 (λ x y → x +' y ≡' y +' x) Xor°.+-comm
coolTheorem2 : {m : ℕ} → (xs : Vec Bool m) → _
coolTheorem2 = VecBoolXorProps.prove 1 (λ x → (x +' x) ≡' (cst false)) (proj₁ Xor°.-‿inverse)
test = example.coolTheorem (true ∷ false ∷ []) (false ∷ false ∷ [])
|
open import Function
open import Data.Nat using (ℕ ; suc ; zero)
open import Data.Fin using (Fin)
open import Data.Vec
open import Data.Vec.N-ary.NP
open import Relation.Binary.PropositionalEquality
module liftVec where
module single (A : Set)(_+_ : A → A → A)where
data Tm : Set where
var : Tm
cst : A → Tm
fun : Tm → Tm → Tm
eval : Tm → A → A
eval var x = x
eval (cst c) x = c
eval (fun tm tm') x = eval tm x + eval tm' x
veval : {m : ℕ} → Tm → Vec A m → Vec A m
veval var xs = xs
veval (cst x) xs = replicate x
veval (fun tm tm') xs = zipWith _+_ (veval tm xs) (veval tm' xs)
record Fm : Set where
constructor _=='_
field
lhs : Tm
rhs : Tm
s[_] : Fm → Set
s[ lhs ==' rhs ] = (x : A) → eval lhs x ≡ eval rhs x
v[_] : Fm → Set
v[ lhs ==' rhs ] = {m : ℕ}(xs : Vec A m) → veval lhs xs ≡ veval rhs xs
lemma1 : (t : Tm) → veval t [] ≡ []
lemma1 var = refl
lemma1 (cst x) = refl
lemma1 (fun t t') rewrite lemma1 t | lemma1 t' = refl
lemma2 : {m : ℕ}(x : A)(xs : Vec A m)(t : Tm) → veval t (x ∷ xs) ≡ eval t x ∷ veval t xs
lemma2 _ _ var = refl
lemma2 _ _ (cst c) = refl
lemma2 x xs (fun t t') rewrite lemma2 x xs t | lemma2 x xs t' = refl
prf : (t : Fm) → s[ t ] → v[ t ]
prf (lhs ==' rhs) pr [] rewrite lemma1 lhs | lemma1 rhs = refl
prf (l ==' r) pr (x ∷ xs) rewrite lemma2 x xs l | lemma2 x xs r = cong₂ (_∷_) (pr x) (prf (l ==' r) pr xs)
module MultiDataType {A : Set} {nrVar : ℕ} where
data Tm : Set where
var : Fin nrVar → Tm
cst : A → Tm
fun : Tm → Tm → Tm
infixl 6 _+'_
_+'_ : Tm → Tm → Tm
_+'_ = fun
infix 4 _≡'_
record TmEq : Set where
constructor _≡'_
field
lhs rhs : Tm
module multi (A : Set)(_+_ : A → A → A)(nrVar : ℕ) where
open MultiDataType {A} {nrVar} public
sEnv : Set
sEnv = Vec A nrVar
vEnv : ℕ → Set
vEnv m = Vec (Vec A m) nrVar
Var : Set
Var = Fin nrVar
_!_ : {X : Set} → Vec X nrVar → Var → X
E ! v = lookup v E
eval : Tm → sEnv → A
eval (var x) G = G ! x
eval (cst c) G = c
eval (fun t t') G = eval t G + eval t' G
veval : {m : ℕ} → Tm → vEnv m → Vec A m
veval (var x) G = G ! x
veval (cst c) G = replicate c
veval (fun t t') G = zipWith _+_ (veval t G) (veval t' G)
lemma1 : {xs : Vec (Vec A 0) nrVar} (t : Tm) → veval t xs ≡ []
lemma1 {xs} (var x) with xs ! x
... | [] = refl
lemma1 (cst x) = refl
lemma1 {xs} (fun t t') rewrite lemma1 {xs} t | lemma1 {xs} t' = refl
lemVar : {m n : ℕ}(G : Vec (Vec A (suc m)) n)(i : Fin n) → lookup i G ≡ lookup i (map head G) ∷ lookup i (map tail G)
lemVar [] ()
lemVar ((x ∷ G) ∷ G₁) Data.Fin.zero = refl
lemVar (G ∷ G') (Data.Fin.suc i) = lemVar G' i
lemma2 : {n : ℕ}(xs : vEnv (suc n))(t : Tm) → veval t xs ≡ eval t (map head xs) ∷ veval t (map tail xs)
lemma2 G (var x) = lemVar G x
lemma2 _ (cst x) = refl
lemma2 G (fun t t') rewrite lemma2 G t | lemma2 G t' = refl
s[_==_] : Tm → Tm → Set
s[ l == r ] = (G : Vec A nrVar) → eval l G ≡ eval r G
v[_==_] : Tm → Tm → Set
v[ l == r ] = {m : ℕ}(G : Vec (Vec A m) nrVar) → veval l G ≡ veval r G
prf : (l r : Tm) → s[ l == r ] → v[ l == r ]
prf l r pr {zero} G rewrite lemma1 {G} l | lemma1 {G} r = refl
prf l r pr {suc m} G rewrite lemma2 G l | lemma2 G r
= cong₂ _∷_ (pr (replicate head ⊛ G)) (prf l r pr (map tail G))
module Full (A : Set)(_+_ : A → A → A) (m : ℕ) n where
open multi A _+_ n public
solve : ∀ (l r : Tm) → ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G))
→ ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G))
solve l r x = curryⁿ {n} {A = Vec A m} {B = curryⁿ′ f}
(λ xs → subst id (sym (curry-$ⁿ′ f xs))
(prf l r (λ G → subst id (curry-$ⁿ′ g G) (x $ⁿ G)) xs))
where f = λ G → veval {m} l G ≡ veval r G
g = λ G → eval l G ≡ eval r G
mkTm : N-ary n Tm TmEq → TmEq
mkTm = go (tabulate id) where
go : {m : ℕ} -> Vec (Fin n) m → N-ary m Tm TmEq → TmEq
go [] f = f
go (x ∷ args) f = go args (f (var x))
prove : ∀ (t : N-ary n Tm TmEq) →
let
l = TmEq.lhs (mkTm t)
r = TmEq.rhs (mkTm t)
in ∀ⁿ n (curryⁿ′ (λ G → eval l G ≡ eval r G))
→ ∀ⁿ n (curryⁿ′ (λ G → veval {m} l G ≡ veval r G))
prove t x = solve (TmEq.lhs (mkTm t)) (TmEq.rhs (mkTm t)) x
open import Data.Two
module example where
open import Data.Fin
open import Data.Product using (Σ ; proj₁)
module VecBoolXorProps {m} = Full 𝟚 _xor_ m
open MultiDataType
coolTheorem : {m : ℕ} → (xs ys : Vec 𝟚 m) → zipWith _xor_ xs ys ≡ zipWith _xor_ ys xs
coolTheorem = VecBoolXorProps.prove 2 (λ x y → x +' y ≡' y +' x) Xor°.+-comm
coolTheorem2 : {m : ℕ} → (xs : Vec 𝟚 m) → _
coolTheorem2 = VecBoolXorProps.prove 1 (λ x → (x +' x) ≡' (cst 0₂)) (proj₁ Xor°.-‿inverse)
test = example.coolTheorem (1₂ ∷ 0₂ ∷ []) (0₂ ∷ 0₂ ∷ [])
|
Upgrade to Data.Two
|
Upgrade to Data.Two
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
a79e9939c6512db4827fc831f2689871d99cfc26
|
Base/Change/Context.agda
|
Base/Change/Context.agda
|
module Base.Change.Context
{Type : Set}
(ΔType : Type → Type) where
-- Transform a context of values into a context of values and
-- changes.
open import Base.Syntax.Context Type
ΔContext : Context → Context
ΔContext ∅ = ∅
ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ
-- like ΔContext, but ΔType τ and τ are swapped
ΔContext′ : Context → Context
ΔContext′ ∅ = ∅
ΔContext′ (τ • Γ) = τ • ΔType τ • ΔContext′ Γ
Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ
Γ≼ΔΓ {∅} = ∅
Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ
|
module Base.Change.Context
{Type : Set}
(ΔType : Type → Type) where
open import Base.Syntax.Context Type
-- Transform a context of values into a context of values and
-- changes.
ΔContext : Context → Context
ΔContext ∅ = ∅
ΔContext (τ • Γ) = ΔType τ • τ • ΔContext Γ
-- like ΔContext, but ΔType τ and τ are swapped
ΔContext′ : Context → Context
ΔContext′ ∅ = ∅
ΔContext′ (τ • Γ) = τ • ΔType τ • ΔContext′ Γ
Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔContext Γ
Γ≼ΔΓ {∅} = ∅
Γ≼ΔΓ {τ • Γ} = drop ΔType τ • keep τ • Γ≼ΔΓ
|
Move function-specific comment right before function
|
Move function-specific comment right before function
Currently, this seems a comment about the whole module.
Old-commit-hash: c25e918e712496981adfe4c74f94a8d80e8e5052
|
Agda
|
mit
|
inc-lc/ilc-agda
|
06d988ce4bc3692e4e60e39bdb8f2e1daddb660e
|
Denotation/Change/Popl14.agda
|
Denotation/Change/Popl14.agda
|
module Denotation.Change.Popl14 where
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Popl14.Denotation.Value
open import Theorem.Groups-Popl14
open import Postulate.Extensionality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
-- apply Δv v ::= v ⊞ Δv
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
-- diff u v ::= u ⊟ v
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
--------------------
-- Implementation --
--------------------
-- ΔVal τ is intended to be the semantic domain for changes of values
-- of type τ.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
--
-- ΔVal : Type → Set
ΔVal int = ℤ
ΔVal bag = Bag
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {int} n Δn = n + Δn
_⊞_ {bag} b Δb = b ++ Δb
_⊞_ {σ ⇒ τ} f Δf = λ v → f v ⊞ Δf v (v ⊟ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {int} m n = m - n
_⊟_ {bag} d b = d \\ b
_⊟_ {σ ⇒ τ} g f = λ v Δv R[v,Δv] → g (v ⊞ Δv) ⊟ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {int} n Δn = ⊤
valid {bag} b Δb = ⊤
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (R[v,Δv] : valid v Δv)
→ valid (f v) (Δf v Δv R[v,Δv])
× (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {int} {u} {v} = n+[m-n]=m {v} {u}
v+[u-v]=u {bag} {u} {v} = a++[b\\a]=b {v} {u}
v+[u-v]=u {σ ⇒ τ} {u} {v} = ext (λ w →
begin
(v ⊞ (u ⊟ v)) w
≡⟨ refl ⟩
v w ⊞ (u (w ⊞ (w ⊟ w)) ⊟ v w)
≡⟨ cong (λ hole → v w ⊞ (u hole ⊟ v w)) v+[u-v]=u ⟩
v w ⊞ (u w ⊟ v w)
≡⟨ v+[u-v]=u ⟩
u w
∎) where open ≡-Reasoning
R[v,u-v] {int} {u} {v} = tt
R[v,u-v] {bag} {u} {v} = tt
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞ Δw
in
R[v,u-v] {τ}
,
(begin
(v ⊞ (u ⊟ v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {u = u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {u = u w′} {v w}) ⟩
v w ⊞ (u ⊟ v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- `diff` and `apply`, without validity proofs
infixl 6 _⟦⊕⟧_ _⟦⊝⟧_
_⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_⟦⊕⟧_ {int} n Δn = n + Δn
_⟦⊕⟧_ {bag} b Δb = b ++ Δb
_⟦⊕⟧_ {σ ⇒ τ} f Δf = λ v → f v ⟦⊕⟧ Δf v (v ⟦⊝⟧ v)
_⟦⊝⟧_ {int} m n = m - n
_⟦⊝⟧_ {bag} a b = a \\ b
_⟦⊝⟧_ {σ ⇒ τ} g f = λ v Δv → g (v ⟦⊕⟧ Δv) ⟦⊝⟧ f v
|
module Denotation.Change.Popl14 where
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Popl14.Denotation.Value
open import Theorem.Groups-Popl14
open import Postulate.Extensionality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
-- apply Δv v ::= v ⊞ Δv
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
-- diff u v ::= u ⊟ v
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
let
_+_ = _⊞_ {τ}
_-_ = _⊟_ {τ}
in
v + (u - v) ≡ u
--------------------
-- Implementation --
--------------------
-- ΔVal τ is intended to be the semantic domain for changes of values
-- of type τ.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
--
-- ΔVal : Type → Set
ΔVal (base base-int) = ℤ
ΔVal (base base-bag) = Bag
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {base base-int} n Δn = n + Δn
_⊞_ {base base-bag} b Δb = b ++ Δb
_⊞_ {σ ⇒ τ} f Δf = λ v → f v ⊞ Δf v (v ⊟ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {base base-int} m n = m - n
_⊟_ {base base-bag} d b = d \\ b
_⊟_ {σ ⇒ τ} g f = λ v Δv R[v,Δv] → g (v ⊞ Δv) ⊟ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base base-int} n Δn = ⊤
valid {base base-bag} b Δb = ⊤
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (R[v,Δv] : valid v Δv)
→ valid (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (_⊞_ {σ ⇒ τ} f Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base base-int} {u} {v} = n+[m-n]=m {v} {u}
v+[u-v]=u {base base-bag} {u} {v} = a++[b\\a]=b {v} {u}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
let
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₀_ = _⊞_ {σ}
_-₀_ = _⊟_ {σ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v + (u - v)) w
≡⟨ refl ⟩
v w +₁ (u (w +₀ (w -₀ w)) -₁ v w)
≡⟨ cong (λ hole → v w +₁ (u hole -₁ v w)) (v+[u-v]=u {σ}) ⟩
v w +₁ (u w -₁ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base base-int} {u} {v} = tt
R[v,u-v] {base base-bag} {u} {v} = tt
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞ Δw
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
R[v,u-v] {τ}
,
(begin
(v + (u - v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w +₁ (u - v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- `diff` and `apply`, without validity proofs
infixl 6 _⟦⊕⟧_ _⟦⊝⟧_
_⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_⟦⊕⟧_ {base base-int} n Δn = n + Δn
_⟦⊕⟧_ {base base-bag} b Δb = b ++ Δb
_⟦⊕⟧_ {σ ⇒ τ} f Δf = λ v →
let
_-₀_ = _⟦⊝⟧_ {σ}
_+₁_ = _⟦⊕⟧_ {τ}
in
f v +₁ Δf v (v -₀ v)
_⟦⊝⟧_ {base base-int} m n = m - n
_⟦⊝⟧_ {base base-bag} a b = a \\ b
_⟦⊝⟧_ {σ ⇒ τ} g f = λ v Δv → _⟦⊝⟧_ {τ} (g (_⟦⊕⟧_ {σ} v Δv)) (f v)
|
add type annotations to Denotation.Change.Popl14
|
[WIP] add type annotations to Denotation.Change.Popl14
Old-commit-hash: 90b06722aecf9d0b5c6c69547217cb2c522912be
|
Agda
|
mit
|
inc-lc/ilc-agda
|
d9a3d68c58bd5c8ae49738e7a8f68fd5e89b4bce
|
lambda.agda
|
lambda.agda
|
module lambda where
open import Relation.Binary.PropositionalEquality
open import Denotational.Notation
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
bool : Type
infixr 5 _⇒_
-- Denotational Semantics
data Bool : Set where
true false : Bool
⟦_⟧Type : Type -> Set
⟦ τ₁ ⇒ τ₂ ⟧Type = ⟦ τ₁ ⟧Type → ⟦ τ₂ ⟧Type
⟦ bool ⟧Type = Bool
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- TYPING CONTEXTS, VARIABLES and WEAKENING
open import Syntactic.Contexts Type public
open import Denotational.Environments Type ⟦_⟧Type public
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
true false : ∀ {Γ} → Term Γ bool
cond : ∀ {Γ τ} → (e₁ : Term Γ bool) (e₂ e₃ : Term Γ τ) → Term Γ τ
-- Denotational Semantics
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ app t₁ t₂ ⟧Term ρ = (⟦ t₁ ⟧Term ρ) (⟦ t₂ ⟧Term ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ true ⟧Term ρ = true
⟦ false ⟧Term ρ = false
⟦ cond t₁ t₂ t₃ ⟧Term ρ with ⟦ t₁ ⟧Term ρ
... | true = ⟦ t₂ ⟧Term ρ
... | false = ⟦ t₃ ⟧Term ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
-- NATURAL SEMANTICS
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
true : Val bool
false : Val bool
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
cond-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} →
ρ ⊢ t₁ ↓ true →
ρ ⊢ t₂ ↓ v₂ →
ρ ⊢ cond t₁ t₂ t₃ ↓ v₂
cond-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} →
ρ ⊢ t₁ ↓ false →
ρ ⊢ t₃ ↓ v₃ →
ρ ⊢ cond t₁ t₂ t₃ ↓ v₃
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
⟦ true ⟧Val = true
⟦ false ⟧Val = false
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv = meaning ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal = meaning ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = refl
↓-sound (app ↓₁ ↓₂ ↓′) = trans (cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂)) (↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
↓-sound (cond-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂
↓-sound (cond-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃
-- WEAKENING
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
weaken {Γ₁} {Γ₂} true = true
weaken {Γ₁} {Γ₂} false = false
weaken {Γ₁} {Γ₂} (cond e₁ e₂ e₃) = cond (weaken {Γ₁} {Γ₂} e₁) (weaken {Γ₁} {Γ₂} e₂) (weaken {Γ₁} {Γ₂} e₃)
|
module lambda where
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types public
open import Syntactic.Contexts Type public
open import Denotational.Notation
open import Denotational.Values public
open import Denotational.Environments Type ⟦_⟧Type public
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
true false : ∀ {Γ} → Term Γ bool
cond : ∀ {Γ τ} → (e₁ : Term Γ bool) (e₂ e₃ : Term Γ τ) → Term Γ τ
-- Denotational Semantics
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ app t₁ t₂ ⟧Term ρ = (⟦ t₁ ⟧Term ρ) (⟦ t₂ ⟧Term ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ true ⟧Term ρ = true
⟦ false ⟧Term ρ = false
⟦ cond t₁ t₂ t₃ ⟧Term ρ with ⟦ t₁ ⟧Term ρ
... | true = ⟦ t₂ ⟧Term ρ
... | false = ⟦ t₃ ⟧Term ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
-- NATURAL SEMANTICS
-- Syntax
data Env : Context → Set
data Val : Type → Set
data Val where
⟨abs_,_⟩ : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) (ρ : Env Γ) → Val (τ₁ ⇒ τ₂)
true : Val bool
false : Val bool
data Env where
∅ : Env ∅
_•_ : ∀ {Γ τ} → Val τ → Env Γ → Env (τ • Γ)
-- Lookup
infixr 8 _⊢_↓_ _⊢_↦_
data _⊢_↦_ : ∀ {Γ τ} → Env Γ → Var Γ τ → Val τ → Set where
this : ∀ {Γ τ} {ρ : Env Γ} {v : Val τ} →
v • ρ ⊢ this ↦ v
that : ∀ {Γ τ₁ τ₂ x} {ρ : Env Γ} {v₁ : Val τ₁} {v₂ : Val τ₂} →
ρ ⊢ x ↦ v₂ →
v₁ • ρ ⊢ that x ↦ v₂
-- Reduction
data _⊢_↓_ : ∀ {Γ τ} → Env Γ → Term Γ τ → Val τ → Set where
abs : ∀ {Γ τ₁ τ₂ ρ} {t : Term (τ₁ • Γ) τ₂} →
ρ ⊢ abs t ↓ ⟨abs t , ρ ⟩
app : ∀ {Γ Γ′ τ₁ τ₂ ρ ρ′ v₂ v′} {t₁ : Term Γ (τ₁ ⇒ τ₂)} {t₂ : Term Γ τ₁} {t′ : Term (τ₁ • Γ′) τ₂} →
ρ ⊢ t₁ ↓ ⟨abs t′ , ρ′ ⟩ →
ρ ⊢ t₂ ↓ v₂ →
v₂ • ρ′ ⊢ t′ ↓ v′ →
ρ ⊢ app t₁ t₂ ↓ v′
var : ∀ {Γ τ x} {ρ : Env Γ} {v : Val τ}→
ρ ⊢ x ↦ v →
ρ ⊢ var x ↓ v
cond-true : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₂ : Val τ} →
ρ ⊢ t₁ ↓ true →
ρ ⊢ t₂ ↓ v₂ →
ρ ⊢ cond t₁ t₂ t₃ ↓ v₂
cond-false : ∀ {Γ τ} {ρ : Env Γ} {t₁ t₂ t₃} {v₃ : Val τ} →
ρ ⊢ t₁ ↓ false →
ρ ⊢ t₃ ↓ v₃ →
ρ ⊢ cond t₁ t₂ t₃ ↓ v₃
-- SOUNDNESS of natural semantics
⟦_⟧Env : ∀ {Γ} → Env Γ → ⟦ Γ ⟧
⟦_⟧Val : ∀ {τ} → Val τ → ⟦ τ ⟧
⟦ ∅ ⟧Env = ∅
⟦ v • ρ ⟧Env = ⟦ v ⟧Val • ⟦ ρ ⟧Env
⟦ ⟨abs t , ρ ⟩ ⟧Val = λ v → ⟦ t ⟧ (v • ⟦ ρ ⟧Env)
⟦ true ⟧Val = true
⟦ false ⟧Val = false
meaningOfEnv : ∀ {Γ} → Meaning (Env Γ)
meaningOfEnv = meaning ⟦_⟧Env
meaningOfVal : ∀ {τ} → Meaning (Val τ)
meaningOfVal = meaning ⟦_⟧Val
↦-sound : ∀ {Γ τ ρ v} {x : Var Γ τ} →
ρ ⊢ x ↦ v →
⟦ x ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↦-sound this = refl
↦-sound (that ↦) = ↦-sound ↦
↓-sound : ∀ {Γ τ ρ v} {t : Term Γ τ} →
ρ ⊢ t ↓ v →
⟦ t ⟧ ⟦ ρ ⟧ ≡ ⟦ v ⟧
↓-sound abs = refl
↓-sound (app ↓₁ ↓₂ ↓′) = trans (cong₂ (λ x y → x y) (↓-sound ↓₁) (↓-sound ↓₂)) (↓-sound ↓′)
↓-sound (var ↦) = ↦-sound ↦
↓-sound (cond-true ↓₁ ↓₂) rewrite ↓-sound ↓₁ = ↓-sound ↓₂
↓-sound (cond-false ↓₁ ↓₃) rewrite ↓-sound ↓₁ = ↓-sound ↓₃
-- WEAKENING
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
weaken {Γ₁} {Γ₂} true = true
weaken {Γ₁} {Γ₂} false = false
weaken {Γ₁} {Γ₂} (cond e₁ e₂ e₃) = cond (weaken {Γ₁} {Γ₂} e₁) (weaken {Γ₁} {Γ₂} e₂) (weaken {Γ₁} {Γ₂} e₃)
|
Improve reuse (see #28).
|
Improve reuse (see #28).
Old-commit-hash: 7da11aabf971e192ab71542164b98c80cbe26325
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a883bca1e4dcf18cbd172d5af888116a00518e15
|
Parametric/Change/Term.agda
|
Parametric/Change/Term.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Constant : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Constant
open ChangeType.Structure Base ΔBase
open import Data.Product
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
∀ {τ Γ} →
Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-diff-apply {base ι} = diff-base , apply-base
lift-diff-apply {σ ⇒ τ} =
let
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ})
_⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t
_⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t
_⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t
in
abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x)
,
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y))
lift-diff :
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
lift-diff = λ {τ Γ} →
proj₁ (lift-diff-apply {τ} {Γ})
lift-apply :
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply = λ {τ Γ} →
proj₂ (lift-diff-apply {τ} {Γ})
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ lift-diff
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ lift-apply
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Constant : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Constant
open ChangeType.Structure Base ΔBase
open import Data.Product
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
∀ {τ Γ} →
Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-diff-apply {base ι} = diff-base , apply-base
lift-diff-apply {σ ⇒ τ} =
let
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ})
_⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t
_⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t
_⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t
in
abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x)
,
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y))
diff-term :
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
diff-term = λ {τ Γ} →
proj₁ (lift-diff-apply {τ} {Γ})
lift-apply :
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply = λ {τ Γ} →
proj₂ (lift-diff-apply {τ} {Γ})
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ diff-term
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ lift-apply
|
Rename lift-diff to diff-term.
|
Rename lift-diff to diff-term.
Old-commit-hash: 64cb73fbed07060d460e4d9ee85fcec48281d13a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b01d8abafe916c9d2d8af12b12d2adb5f2ae8340
|
Syntax/Term/Plotkin.agda
|
Syntax/Term/Plotkin.agda
|
module Syntax.Term.Plotkin where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open import Syntax.Type.Plotkin
open import Syntax.Context
data Term
{B : Set {- of base types -}}
{C : Set {- of constants -}}
{type-of : C → Type B}
(Γ : Context {Type B}) :
(τ : Type B) → Set
where
const : (c : C) → Term Γ (type-of c)
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ}
(s : Term {B} {C} {type-of} Γ (σ ⇒ τ))
(t : Term {B} {C} {type-of} Γ σ) → Term Γ τ
abs : ∀ {σ τ}
(t : Term {B} {C} {type-of} (σ • Γ) τ) → Term Γ (σ ⇒ τ)
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {B C type-of} {Δbase : B → Type B} →
let
Δtype = lift-Δtype Δbase
term = Term {B} {C} {type-of}
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {B C type-of} {Δbase : B → Type B} →
let
Δtype = lift-Δtype Δbase
term = Term {B} {C} {type-of}
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {B C type-of} {Δbase : B → Type B} →
let
Δtype = lift-Δtype Δbase
term = Term {B} {C} {type-of}
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {B C ⊢ Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term {B} {C} {⊢} Γ₁ τ →
Term {B} {C} {⊢} Γ₂ τ
weaken Γ₁≼Γ₂ (const c) = const c
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
-- Specialized weakening
weaken₁ : ∀ {B C ⊢ Γ σ τ} →
Term {B} {C} {⊢} Γ τ → Term {B} {C} {⊢} (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {B C ⊢ Γ α β τ} →
Term {B} {C} {⊢} Γ τ → Term {B} {C} {⊢} (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {B C ⊢ Γ α β γ τ} →
Term {B} {C} {⊢} Γ τ → Term {B} {C} {⊢} (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {B C ⊢ Γ α β γ} →
Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {B C ⊢ Γ α β γ δ} →
Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {B C ⊢ Γ α β γ δ ε} →
Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {B C ⊢ Γ α β γ δ ε ζ} →
Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {B C ⊢ Γ α β γ δ ε ζ η} →
Term {B} {C} {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
|
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open import Syntax.Type.Plotkin
open import Syntax.Context
data Term
{type-of : C → Type B}
(Γ : Context {Type B}) :
(τ : Type B) → Set
where
const : (c : C) → Term Γ (type-of c)
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ}
(s : Term {type-of} Γ (σ ⇒ τ))
(t : Term {type-of} Γ σ) → Term Γ τ
abs : ∀ {σ τ}
(t : Term {type-of} (σ • Γ) τ) → Term Γ (σ ⇒ τ)
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {type-of} {Δbase : B → Type B} →
let
Δtype = lift-Δtype Δbase
term = Term {type-of}
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {type-of} {Δbase : B → Type B} →
let
Δtype = lift-Δtype Δbase
term = Term {type-of}
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {type-of} {Δbase : B → Type B} →
let
Δtype = lift-Δtype Δbase
term = Term {type-of}
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {⊢ Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term {⊢} Γ₁ τ →
Term {⊢} Γ₂ τ
weaken Γ₁≼Γ₂ (const c) = const c
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
-- Specialized weakening
weaken₁ : ∀ {⊢ Γ σ τ} →
Term {⊢} Γ τ → Term {⊢} (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {⊢ Γ α β τ} →
Term {⊢} Γ τ → Term {⊢} (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {⊢ Γ α β γ τ} →
Term {⊢} Γ τ → Term {⊢} (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {⊢ Γ α β γ} →
Term {⊢} Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {⊢ Γ α β γ δ} →
Term {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {⊢ Γ α β γ δ ε} →
Term {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {⊢ Γ α β γ δ ε ζ} →
Term {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {⊢ Γ α β γ δ ε ζ η} →
Term {⊢} Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
|
Move parameters to module level.
|
Move parameters to module level.
All definitions in this module depend on the parameters {B} and
{C} which never change inside this module. So it is easier to
parameterize the whole module instead of each separate
definition.
Old-commit-hash: 1303320f48d486d11152713c604275c2c7bcdc18
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9755ab9f86f1e00b28c36e99421e4e9be389a95f
|
Base/Change/Products.agda
|
Base/Change/Products.agda
|
module Base.Change.Products where
open import Relation.Binary.PropositionalEquality
open import Level
open import Base.Change.Algebra
open import Data.Product
-- Also try defining sectioned change structures on the positives halves of
-- groups? Or on arbitrary subsets?
-- Restriction: we pair sets on the same level (because right now everything
-- else would risk getting in the way).
module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where
open ≡-Reasoning
-- The simplest possible definition of changes for products.
-- The following is probably bullshit:
-- Does not handle products of functions - more accurately, writing the
-- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p
PChange : A × B → Set ℓ
PChange (a , b) = Δ a × Δ b
_⊕_ : (v : A × B) → PChange v → A × B
_⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db
_⊝_ : A × B → (v : A × B) → PChange v
_⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b
p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u
p-update-diff (ua , ub) (va , vb) =
let u = (ua , ub)
v = (va , vb)
in
begin
v ⊕ (u ⊝ v)
≡⟨⟩
(va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb))
--v ⊕ ((ua ⊟ va , ub ⊟ vb))
≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩
(ua , ub)
≡⟨⟩
u
∎
changeAlgebra : ChangeAlgebra ℓ (A × B)
changeAlgebra = record
{ Change = PChange
; update = _⊕_
; diff = _⊝_
; isChangeAlgebra = record
{ update-diff = p-update-diff
}
}
proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v)
proj₁′ (a , b) (da , db) = da
proj₁′Derivative : Derivative proj₁ proj₁′
-- Implementation note: we do not need to pattern match on v and dv because
-- they are records, hence Agda knows that pattern matching on records cannot
-- fail. Technically, the required feature is the eta-rule on records.
proj₁′Derivative v dv = refl
-- An extended explanation.
proj₁′Derivative₁ : Derivative proj₁ proj₁′
proj₁′Derivative₁ (a , b) (da , db) =
let v = (a , b)
dv = (da , db)
in
begin
proj₁ v ⊞ proj₁′ v dv
≡⟨⟩
a ⊞ da
≡⟨⟩
proj₁ (v ⊞ dv)
∎
-- Same for the second extractor.
proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v)
proj₂′ (a , b) (da , db) = db
proj₂′Derivative : Derivative proj₂ proj₂′
proj₂′Derivative v dv = refl
-- We should do the same for uncurry instead.
-- What one could wrongly expect to be the derivative of the constructor:
_,_′ : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b)
_,_′ a da b db = da , db
-- That has the correct behavior, in a sense, and it would be in the
-- subset-based formalization in the paper.
--
-- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof
-- As a consequence, proving that's a derivative seems too insanely hard. We
-- might want to provide a proof schema for curried functions at once,
-- starting from the right correctness equation.
B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B)
A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}}
module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}}
module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}}
_,_′-real : Δ _,_
_,_′-real = nil _,_
_,_′-real-Derivative : Derivative {{CA}} {{B→A×B}} _,_ (ΔA→B→A×B.apply _,_′-real)
_,_′-real-Derivative =
FunctionChanges.nil-is-derivative A (B → A × B) {{CA}} {{B→A×B}} _,_
_,_′′ : (a : A) → Δ a →
Δ {{B→A×B}} (λ b → (a , b))
_,_′′ a da = record
{ apply = _,_′ a da
; correct = λ b db →
begin
(a , b ⊞ db) ⊞ (_,_′ a da) (b ⊞ db) (nil (b ⊞ db))
≡⟨⟩
a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db))
≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩
a ⊞ da , b ⊞ db
≡⟨⟩
(a , b) ⊞ (_,_′ a da) b db
∎
}
_,_′′′ : Δ {{A→B→A×B}} _,_
_,_′′′ = record
{ apply = _,_′′
; correct = λ a da →
begin
update
(_,_ (a ⊞ da))
(_,_′′ (a ⊞ da) (nil (a ⊞ da)))
≡⟨ {!!} ⟩
update (_,_ a) (_,_′′ a da)
∎
}
where
-- This is needed to use update above.
-- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly.
open ChangeAlgebra B→A×B hiding (nil)
{-
{!
begin
(_,_ (a ⊞ da)) ⊞ _,_′′ (a ⊞ da) (nil (a ⊞ da))
{- ≡⟨⟩
a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) -}
≡⟨ ? ⟩
{-a ⊞ da , b ⊞ db
≡⟨⟩-}
(_,_ a) ⊞ (_,_′′ a da)
∎!}
}
-}
open import Postulate.Extensionality
_,_′Derivative :
Derivative {{CA}} {{B→A×B}} _,_ _,_′′
_,_′Derivative a da =
begin
_⊞_ {{B→A×B}} (_,_ a) (_,_′′ a da)
≡⟨⟩
(λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b))
--ext (λ b → cong (λ □ → (a , b) ⊞ □) (update-nil {{?}} b))
≡⟨ {!!} ⟩
(λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b))
≡⟨ sym {!ΔA→B→A×B.incrementalization _,_ _,_′′′ a da!} ⟩
--FunctionChanges.incrementalization A (B → A × B) {{CA}} {{{!B→A×B!}}} _,_ {!!} {!!} {!!}
_,_ (a ⊞ da)
∎
|
module Base.Change.Products where
open import Relation.Binary.PropositionalEquality
open import Level
open import Base.Change.Algebra
open import Data.Product
-- Also try defining sectioned change structures on the positives halves of
-- groups? Or on arbitrary subsets?
-- Restriction: we pair sets on the same level (because right now everything
-- else would risk getting in the way).
module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where
open ≡-Reasoning
-- The simplest possible definition of changes for products.
-- The following is probably bullshit:
-- Does not handle products of functions - more accurately, writing the
-- derivative of fst and snd for products of functions is hard: fst' p dp must return the change of fst p
PChange : A × B → Set ℓ
PChange (a , b) = Δ a × Δ b
_⊕_ : (v : A × B) → PChange v → A × B
_⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db
_⊝_ : A × B → (v : A × B) → PChange v
_⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b
p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u
p-update-diff (ua , ub) (va , vb) =
let u = (ua , ub)
v = (va , vb)
in
begin
v ⊕ (u ⊝ v)
≡⟨⟩
(va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb))
--v ⊕ ((ua ⊟ va , ub ⊟ vb))
≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩
(ua , ub)
≡⟨⟩
u
∎
changeAlgebra : ChangeAlgebra ℓ (A × B)
changeAlgebra = record
{ Change = PChange
; update = _⊕_
; diff = _⊝_
; isChangeAlgebra = record
{ update-diff = p-update-diff
}
}
proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v)
proj₁′ (a , b) (da , db) = da
proj₁′Derivative : Derivative proj₁ proj₁′
-- Implementation note: we do not need to pattern match on v and dv because
-- they are records, hence Agda knows that pattern matching on records cannot
-- fail. Technically, the required feature is the eta-rule on records.
proj₁′Derivative v dv = refl
-- An extended explanation.
proj₁′Derivative₁ : Derivative proj₁ proj₁′
proj₁′Derivative₁ (a , b) (da , db) =
let v = (a , b)
dv = (da , db)
in
begin
proj₁ v ⊞ proj₁′ v dv
≡⟨⟩
a ⊞ da
≡⟨⟩
proj₁ (v ⊞ dv)
∎
-- Same for the second extractor.
proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v)
proj₂′ (a , b) (da , db) = db
proj₂′Derivative : Derivative proj₂ proj₂′
proj₂′Derivative v dv = refl
-- We should do the same for uncurry instead.
-- What one could wrongly expect to be the derivative of the constructor:
_,_′ : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b)
_,_′ a da b db = da , db
-- That has the correct behavior, in a sense, and it would be in the
-- subset-based formalization in the paper.
--
-- But the above is not even a change, because it does not contain a proof of its own validity, and because after application it does not contain a proof
-- As a consequence, proving that's a derivative seems too insanely hard. We
-- might want to provide a proof schema for curried functions at once,
-- starting from the right correctness equation.
B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B)
A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}}
module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}}
module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}}
_,_′-real : Δ _,_
_,_′-real = nil _,_
_,_′-real-Derivative : Derivative {{CA}} {{B→A×B}} _,_ (ΔA→B→A×B.apply _,_′-real)
_,_′-real-Derivative =
FunctionChanges.nil-is-derivative A (B → A × B) {{CA}} {{B→A×B}} _,_
_,_′′ : (a : A) → Δ a →
Δ {{B→A×B}} (λ b → (a , b))
_,_′′ a da = record
{ apply = _,_′ a da
; correct = λ b db →
begin
(a , b ⊞ db) ⊞ (_,_′ a da) (b ⊞ db) (nil (b ⊞ db))
≡⟨⟩
a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db))
≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩
a ⊞ da , b ⊞ db
≡⟨⟩
(a , b) ⊞ (_,_′ a da) b db
∎
}
{-
_,_′′′ : Δ {{A→B→A×B}} _,_
_,_′′′ = record
{ apply = _,_′′
; correct = λ a da →
begin
update
(_,_ (a ⊞ da))
(_,_′′ (a ⊞ da) (nil (a ⊞ da)))
≡⟨ {!!} ⟩
update (_,_ a) (_,_′′ a da)
∎
}
where
-- This is needed to use update above.
-- Passing the change structure seems hard with the given operators; maybe I'm just using them wrongly.
open ChangeAlgebra B→A×B hiding (nil)
{-
{!
begin
(_,_ (a ⊞ da)) ⊞ _,_′′ (a ⊞ da) (nil (a ⊞ da))
{- ≡⟨⟩
a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db)) -}
≡⟨ ? ⟩
{-a ⊞ da , b ⊞ db
≡⟨⟩-}
(_,_ a) ⊞ (_,_′′ a da)
∎!}
}
-}
open import Postulate.Extensionality
_,_′Derivative :
Derivative {{CA}} {{B→A×B}} _,_ _,_′′
_,_′Derivative a da =
begin
_⊞_ {{B→A×B}} (_,_ a) (_,_′′ a da)
≡⟨⟩
(λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b))
--ext (λ b → cong (λ □ → (a , b) ⊞ □) (update-nil {{?}} b))
≡⟨ {!!} ⟩
(λ b → (a , b) ⊞ ΔBA×B.apply (_,_′′ a da) b (nil b))
≡⟨ sym {!ΔA→B→A×B.incrementalization _,_ _,_′′′ a da!} ⟩
--FunctionChanges.incrementalization A (B → A × B) {{CA}} {{{!B→A×B!}}} _,_ {!!} {!!} {!!}
_,_ (a ⊞ da)
∎
-}
|
Comment out holes to ensure Everything compiles again
|
Comment out holes to ensure Everything compiles again
Old-commit-hash: dfc28a00f9301015066e19d4be2c4179f8e0849b
|
Agda
|
mit
|
inc-lc/ilc-agda
|
07b3b64ff9795f6941b64231e7b2a0dd630e0de4
|
lib/Explore/Examples.agda
|
lib/Explore/Examples.agda
|
{-# OPTIONS --without-K #-}
module Explore.Examples where
open import Type
open import Level.NP
open import Data.Maybe.NP
open import Data.List
open import Data.Two
open import Data.Product
open import Data.Sum.NP
open import Data.One
open import HoTT using (UA)
open import Function.Extensionality using (FunExt)
open import Relation.Binary.PropositionalEquality hiding ([_])
open import Explore.Core
open import Explore.Explorable
open import Explore.Universe.Base
open import Explore.Monad {₀} ₀ public renaming (map to map-explore)
open import Explore.Two
open import Explore.Product
open Explore.Product.Operators
module E where
open FromExplore public
module M {Msg Digest : ★}
(_==_ : Digest → Digest → 𝟚)
(H : Msg → Digest)
(exploreMsg : ∀ {ℓ} → Explore ℓ Msg)
(d : Digest) where
module V1 where
list-H⁻¹ : List Msg
list-H⁻¹ = exploreMsg [] _++_ (λ m → [0: [] 1: [ m ] ] (H m == d))
module ExploreMsg = FromExplore {A = Msg} exploreMsg
module V2 where
first-H⁻¹ : Maybe Msg
first-H⁻¹ = ExploreMsg.findKey (λ m → H m == d)
module V3 where
explore-H⁻¹ : Explore ₀ Msg
explore-H⁻¹ ε _⊕_ f = exploreMsg ε _⊕_ (λ m → [0: ε 1: f m ] (H m == d))
module V4 where
explore-H⁻¹ : Explore ₀ Msg
explore-H⁻¹ = exploreMsg >>= λ m → [0: empty-explore 1: point-explore m ] (H m == d)
module V5 where
explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg
explore-H⁻¹ = filter-explore (λ m → H m == d) exploreMsg
list-H⁻¹ : List Msg
list-H⁻¹ = E.list explore-H⁻¹
first-H⁻¹ : Maybe Msg
first-H⁻¹ = E.first explore-H⁻¹
module V6 where
explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg
explore-H⁻¹ = explore-endo (filter-explore (λ m → H m == d) exploreMsg)
list-H⁻¹ : List Msg
list-H⁻¹ = E.list explore-H⁻¹
first-H⁻¹ : Maybe Msg
first-H⁻¹ = E.first explore-H⁻¹
last-H⁻¹ : Maybe Msg
last-H⁻¹ = E.last explore-H⁻¹
Msg = 𝟚 × 𝟚
Digest = 𝟚
-- _==_ : Digest → Digest → 𝟚
H : Msg → Digest
H (x , y) = x xor y
Msgᵉ : ∀ {ℓ} → Explore ℓ Msg
Msgᵉ = 𝟚ᵉ ×ᵉ 𝟚ᵉ
module N5 = M.V5 _==_ H Msgᵉ
module N6 = M.V6 _==_ H Msgᵉ
test5 = N5.list-H⁻¹
test6-list : N6.list-H⁻¹ 0₂ ≡ (0₂ , 0₂) ∷ (1₂ , 1₂) ∷ []
test6-list = refl
test6-rev-list : E.list (E.backward (N6.explore-H⁻¹ 0₂)) ≡ (1₂ , 1₂) ∷ (0₂ , 0₂) ∷ []
test6-rev-list = refl
test6-first : N6.first-H⁻¹ 0₂ ≡ just (0₂ , 0₂)
test6-first = refl
test6-last : N6.last-H⁻¹ 0₂ ≡ just (1₂ , 1₂)
test6-last = refl
-- -}
{-
𝟛ᵁ : U
𝟛ᵁ = 𝟙ᵁ ⊎ᵁ 𝟚ᵁ
list22 = list (𝟚ᵁ →ᵁ 𝟚ᵁ)
list33 = list (𝟛ᵁ →ᵁ 𝟛ᵁ)
-}
{-
module _ {{_ : UA}}{{_ : FunExt}} where
check22 : ∀ (f : 𝟚 → 𝟚) x → ✓ (f x == f (f (f x)))
check22 f x = {!check! ((𝟚ᵁ →ᵁ 𝟚ᵁ) ×ᵁ 𝟚ᵁ) (λ { (f , x) → let f' = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0₂ , f 1₂) , x)!}
-}
|
{-# OPTIONS --without-K #-}
module Explore.Examples where
open import Type
open import Level.NP
open import Data.Maybe.NP
open import Data.List
open import Data.Zero
open import Data.One
open import Data.Two
open import Data.Product
open import Data.Sum.NP
open import HoTT using (UA)
open import Function.NP
open import Function.Extensionality using (FunExt)
open import Relation.Binary.PropositionalEquality hiding ([_])
open import Explore.Core
open import Explore.Explorable
open import Explore.Universe.Type {𝟘}
open import Explore.Universe.Base
open import Explore.Monad {₀} ₀ public renaming (map to map-explore)
open import Explore.Two
open import Explore.Product
open Explore.Product.Operators
module E where
open FromExplore public
module M {Msg Digest : ★}
(_==_ : Digest → Digest → 𝟚)
(H : Msg → Digest)
(exploreMsg : ∀ {ℓ} → Explore ℓ Msg)
(d : Digest) where
module V1 where
list-H⁻¹ : List Msg
list-H⁻¹ = exploreMsg [] _++_ (λ m → [0: [] 1: [ m ] ] (H m == d))
module ExploreMsg = FromExplore {A = Msg} exploreMsg
module V2 where
first-H⁻¹ : Maybe Msg
first-H⁻¹ = ExploreMsg.findKey (λ m → H m == d)
module V3 where
explore-H⁻¹ : Explore ₀ Msg
explore-H⁻¹ ε _⊕_ f = exploreMsg ε _⊕_ (λ m → [0: ε 1: f m ] (H m == d))
module V4 where
explore-H⁻¹ : Explore ₀ Msg
explore-H⁻¹ = exploreMsg >>= λ m → [0: empty-explore 1: point-explore m ] (H m == d)
module V5 where
explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg
explore-H⁻¹ = filter-explore (λ m → H m == d) exploreMsg
list-H⁻¹ : List Msg
list-H⁻¹ = E.list explore-H⁻¹
first-H⁻¹ : Maybe Msg
first-H⁻¹ = E.first explore-H⁻¹
module V6 where
explore-H⁻¹ : ∀ {ℓ} → Explore ℓ Msg
explore-H⁻¹ = explore-endo (filter-explore (λ m → H m == d) exploreMsg)
list-H⁻¹ : List Msg
list-H⁻¹ = E.list explore-H⁻¹
first-H⁻¹ : Maybe Msg
first-H⁻¹ = E.first explore-H⁻¹
last-H⁻¹ : Maybe Msg
last-H⁻¹ = E.last explore-H⁻¹
Msg = 𝟚 × 𝟚
Digest = 𝟚
-- _==_ : Digest → Digest → 𝟚
H : Msg → Digest
H (x , y) = x xor y
Msgᵉ : ∀ {ℓ} → Explore ℓ Msg
Msgᵉ = 𝟚ᵉ ×ᵉ 𝟚ᵉ
module N5 = M.V5 _==_ H Msgᵉ
module N6 = M.V6 _==_ H Msgᵉ
test5 = N5.list-H⁻¹
test6-list : N6.list-H⁻¹ 0₂ ≡ (0₂ , 0₂) ∷ (1₂ , 1₂) ∷ []
test6-list = refl
test6-rev-list : E.list (E.backward (N6.explore-H⁻¹ 0₂)) ≡ (1₂ , 1₂) ∷ (0₂ , 0₂) ∷ []
test6-rev-list = refl
test6-first : N6.first-H⁻¹ 0₂ ≡ just (0₂ , 0₂)
test6-first = refl
test6-last : N6.last-H⁻¹ 0₂ ≡ just (1₂ , 1₂)
test6-last = refl
-- -}
𝟛ᵁ : U
𝟛ᵁ = 𝟙ᵁ ⊎ᵁ 𝟚ᵁ
prop-∧-comm : 𝟚 × 𝟚 → 𝟚
prop-∧-comm (x , y) = x ∧ y == y ∧ x
module _ {{_ : UA}}{{_ : FunExt}} where
check-∧-comm : ∀ x y → ✓ (x ∧ y == y ∧ x)
check-∧-comm x y = check! (𝟚ᵁ ×ᵁ 𝟚ᵁ) prop-∧-comm (x , y)
prop-∧-∨-distr : 𝟚 × 𝟚 × 𝟚 → 𝟚
prop-∧-∨-distr (x , y , z) = x ∧ (y ∨ z) == x ∧ y ∨ x ∧ z
module _ {{_ : UA}}{{_ : FunExt}} where
check-∧-∨-distr : ∀ x y z → ✓ (x ∧ (y ∨ z) == x ∧ y ∨ x ∧ z)
check-∧-∨-distr x y z =
check! (𝟚ᵁ ×ᵁ 𝟚ᵁ ×ᵁ 𝟚ᵁ) prop-∧-∨-distr (x , y , z)
list22 = list (𝟚ᵁ →ᵁ 𝟚ᵁ)
list33 = list (𝟛ᵁ →ᵁ 𝟛ᵁ)
{-
module _ {{_ : UA}}{{_ : FunExt}} where
module _ (fᵁ : El (𝟚ᵁ →ᵁ 𝟚ᵁ)) x where
f = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ fᵁ
check22 : ✓ (f x == f (f (f x)))
check22 = check! ((𝟚ᵁ →ᵁ 𝟚ᵁ) ×ᵁ 𝟚ᵁ) (λ { (f , x) → let f' = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0₂ , f 1₂) , x)
{-
check22 : ∀ (f : 𝟚 → 𝟚) x → ✓ (f x == f (f (f x)))
check22 f x = let k = check! ((𝟚ᵁ →ᵁ 𝟚ᵁ) ×ᵁ 𝟚ᵁ) (λ { (f , x) → let f' = →ᵁ→→ 𝟚ᵁ 𝟚ᵁ f in f' x == f' (f' (f' x)) }) {{!!}} ((f 0₂ , f 1₂) , x) in {!k!}
-- -}
-- -}
-- -}
-- -}
|
Update the Examples
|
Update the Examples
|
Agda
|
bsd-3-clause
|
crypto-agda/explore
|
b4c39231ae725a5b759926a08d76642ed2df680d
|
Base/Change/Products.agda
|
Base/Change/Products.agda
|
module Base.Change.Products where
open import Relation.Binary.PropositionalEquality
open import Level
open import Base.Change.Algebra
open import Data.Product
-- Also try defining sectioned change structures on the positives halves of
-- groups? Or on arbitrary subsets?
-- Restriction: we pair sets on the same level (because right now everything
-- else would risk getting in the way).
module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where
open ≡-Reasoning
-- The simplest possible definition of changes for products.
PChange : A × B → Set ℓ
PChange (a , b) = Δ a × Δ b
-- An interesting alternative definition allows omitting the nil change of a
-- component when that nil change can be computed from the type. For instance, the nil change for integers is always the same.
-- However, the nil change for function isn't always the same (unless we
-- defunctionalize them first), so nil changes for functions can't be omitted.
_⊕_ : (v : A × B) → PChange v → A × B
_⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db
_⊝_ : A × B → (v : A × B) → PChange v
_⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b
p-nil : (v : A × B) → PChange v
p-nil (a , b) = (nil a , nil b)
p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u
p-update-diff (ua , ub) (va , vb) =
let u = (ua , ub)
v = (va , vb)
in
begin
v ⊕ (u ⊝ v)
≡⟨⟩
(va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb))
--v ⊕ ((ua ⊟ va , ub ⊟ vb))
≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩
(ua , ub)
≡⟨⟩
u
∎
p-update-nil : (v : A × B) → v ⊕ (p-nil v) ≡ v
p-update-nil (a , b) =
let v = (a , b)
in
begin
v ⊕ (p-nil v)
≡⟨⟩
(a ⊞ nil a , b ⊞ nil b)
≡⟨ cong₂ _,_ (update-nil a) (update-nil b)⟩
(a , b)
≡⟨⟩
v
∎
changeAlgebra : ChangeAlgebra ℓ (A × B)
changeAlgebra = record
{ Change = PChange
; update = _⊕_
; diff = _⊝_
; nil = p-nil
; isChangeAlgebra = record
{ update-diff = p-update-diff
; update-nil = p-update-nil
}
}
proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v)
proj₁′ (a , b) (da , db) = da
proj₁′Derivative : Derivative proj₁ proj₁′
-- Implementation note: we do not need to pattern match on v and dv because
-- they are records, hence Agda knows that pattern matching on records cannot
-- fail. Technically, the required feature is the eta-rule on records.
proj₁′Derivative v dv = refl
-- An extended explanation.
proj₁′Derivative₁ : Derivative proj₁ proj₁′
proj₁′Derivative₁ (a , b) (da , db) =
let v = (a , b)
dv = (da , db)
in
begin
proj₁ v ⊞ proj₁′ v dv
≡⟨⟩
a ⊞ da
≡⟨⟩
proj₁ (v ⊞ dv)
∎
-- Same for the second extractor.
proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v)
proj₂′ (a , b) (da , db) = db
proj₂′Derivative : Derivative proj₂ proj₂′
proj₂′Derivative v dv = refl
B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} B (A × B)
A→B→A×B = FunctionChanges.changeAlgebra {c = ℓ} {d = ℓ} A (B → A × B) {{CA}} {{B→A×B}}
module ΔBA×B = FunctionChanges B (A × B) {{CB}} {{changeAlgebra}}
module ΔA→B→A×B = FunctionChanges A (B → A × B) {{CA}} {{B→A×B}}
-- Morally, the following is a change:
-- What one could wrongly expect to be the derivative of the constructor:
_,_′-realizer : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b)
_,_′-realizer a da b db = da , db
-- That has the correct behavior, in a sense, and it would be in the
-- subset-based formalization in the paper.
--
-- But the above is not even a change, because it does not contain a proof of
-- its own validity, and because after application it does not contain a
-- proof.
--
-- However, the above is (morally) a "realizer" of the actual change, since it
-- only represents its computational behavior, not its proof manipulation.
-- Hence, we need to do some additional work.
_,_′-realizer-correct _,_′-realizer-correct-detailed :
(a : A) → (da : Δ a) → (b : B) → (db : Δ b) →
(a , b ⊞ db) ⊞ (_,_′-realizer a da (b ⊞ db) (nil (b ⊞ db))) ≡ (a , b) ⊞ (_,_′-realizer a da b db)
_,_′-realizer-correct a da b db rewrite update-nil (b ⊞ db) = refl
_,_′-realizer-correct-detailed a da b db =
begin
(a , b ⊞ db) ⊞ (_,_′-realizer a da) (b ⊞ db) (nil (b ⊞ db))
≡⟨⟩
a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db))
≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩
a ⊞ da , b ⊞ db
≡⟨⟩
(a , b) ⊞ (_,_′-realizer a da) b db
∎
_,_′ : (a : A) → (da : Δ a) → Δ (_,_ a)
_,_′ a da = record { apply = _,_′-realizer a da ; correct = λ b db → _,_′-realizer-correct a da b db }
_,_′-Derivative : Derivative _,_ _,_′
_,_′-Derivative a da = ext (λ b → cong (_,_ (a ⊞ da)) (update-nil b))
where
open import Postulate.Extensionality
-- Define specialized variant of uncurry, and derive it.
uncurry₀ : ∀ {C : Set ℓ} → (A → B → C) → A × B → C
uncurry₀ f (a , b) = f a b
module _ {C : Set ℓ} {{CC : ChangeAlgebra ℓ C}} where
B→C : ChangeAlgebra ℓ (B → C)
B→C = FunctionChanges.changeAlgebra B C
A→B→C : ChangeAlgebra ℓ (A → B → C)
A→B→C = FunctionChanges.changeAlgebra A (B → C)
A×B→C : ChangeAlgebra ℓ (A × B → C)
A×B→C = FunctionChanges.changeAlgebra (A × B) C
module ΔB→C = FunctionChanges B C {{CB}} {{CC}}
module ΔA→B→C = FunctionChanges A (B → C) {{CA}} {{B→C}}
module ΔA×B→C = FunctionChanges (A × B) C {{changeAlgebra}} {{CC}}
uncurry₀′-realizer : (f : A → B → C) → Δ f → (p : A × B) → Δ p → Δ (uncurry₀ f p)
uncurry₀′-realizer f df (a , b) (da , db) = ΔB→C.apply (ΔA→B→C.apply df a da) b db
uncurry₀′-realizer-correct uncurry₀′-realizer-correct-detailed :
∀ (f : A → B → C) (df : Δ f) (p : A × B) (dp : Δ p) →
uncurry₀ f (p ⊕ dp) ⊞ uncurry₀′-realizer f df (p ⊕ dp) (nil (p ⊞ dp)) ≡ uncurry₀ f p ⊞ uncurry₀′-realizer f df p dp
-- Hard to read
uncurry₀′-realizer-correct f df (a , b) (da , db)
rewrite sym (ΔB→C.incrementalization (f (a ⊞ da)) (ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db)))
| update-nil (b ⊞ db)
| {- cong (λ □ → □ (b ⊞ db)) -} (sym (ΔA→B→C.incrementalization f df (a ⊞ da) (nil (a ⊞ da))))
| update-nil (a ⊞ da)
| cong (λ □ → □ (b ⊞ db)) (ΔA→B→C.incrementalization f df a da)
| ΔB→C.incrementalization (f a) (ΔA→B→C.apply df a da) b db
= refl
-- Verbose, but it shows all the intermediate steps.
uncurry₀′-realizer-correct-detailed f df (a , b) (da , db) =
begin
uncurry₀ f (a ⊞ da , b ⊞ db) ⊞ uncurry₀′-realizer f df (a ⊞ da , b ⊞ db) (nil (a ⊞ da , b ⊞ db))
≡⟨⟩
f (a ⊞ da) (b ⊞ db) ⊞ ΔB→C.apply (ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))
≡⟨ sym (ΔB→C.incrementalization (f (a ⊞ da)) (ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))) ⟩
(f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db)))
≡⟨ cong-lem₀ ⟩
(f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db)
≡⟨ sym cong-lem₂ ⟩
((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db)
≡⟨ cong-lem₁ ⟩
(f ⊞ df) (a ⊞ da) (b ⊞ db)
≡⟨ cong (λ □ → □ (b ⊞ db)) (ΔA→B→C.incrementalization f df a da) ⟩
(f a ⊞ ΔA→B→C.apply df a da) (b ⊞ db)
≡⟨ ΔB→C.incrementalization (f a) (ΔA→B→C.apply df a da) b db ⟩
f a b ⊞ ΔB→C.apply (ΔA→B→C.apply df a da) b db
≡⟨⟩
uncurry₀ f (a , b) ⊞ uncurry₀′-realizer f df (a , b) (da , db)
∎
where
cong-lem₀ :
(f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db)))
≡
(f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db)
cong-lem₀ rewrite update-nil (b ⊞ db) = refl
cong-lem₁ :
((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db)
≡
(f ⊞ df) (a ⊞ da) (b ⊞ db)
cong-lem₁ rewrite update-nil (a ⊞ da) = refl
cong-lem₂ :
((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db)
≡
(f (a ⊞ da) ⊞ ΔA→B→C.apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db)
cong-lem₂ = cong (λ □ → □ (b ⊞ db)) (ΔA→B→C.incrementalization f df (a ⊞ da) (nil (a ⊞ da)))
uncurry₀′ : (f : A → B → C) → Δ f → Δ (uncurry f)
uncurry₀′ f df = record
{ apply = uncurry₀′-realizer f df
; correct = uncurry₀′-realizer-correct f df }
-- Now proving that uncurry₀′ is a derivative is trivial!
uncurry₀′Derivative₀ : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′
uncurry₀′Derivative₀ f df = refl
-- If you wonder what's going on, here's the step-by-step proof, going purely by definitional equality.
uncurry₀′Derivative : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′
uncurry₀′Derivative f df =
begin
uncurry₀ f ⊞ uncurry₀′ f df
≡⟨⟩
(λ {(a , b) → uncurry₀ f (a , b) ⊞ ΔA×B→C.apply (uncurry₀′ f df) (a , b) (nil (a , b))})
≡⟨⟩
(λ {(a , b) → f a b ⊞ ΔB→C.apply (ΔA→B→C.apply df a (nil a)) b (nil b)})
≡⟨⟩
(λ {(a , b) → (f a ⊞ ΔA→B→C.apply df a (nil a)) b})
≡⟨⟩
(λ {(a , b) → (f ⊞ df) a b})
≡⟨⟩
(λ {(a , b) → uncurry₀ (f ⊞ df) (a , b)})
≡⟨⟩
uncurry₀ (f ⊞ df)
∎
|
module Base.Change.Products where
open import Relation.Binary.PropositionalEquality
open import Level
open import Base.Change.Algebra
open import Data.Product
-- Also try defining sectioned change structures on the positives halves of
-- groups? Or on arbitrary subsets?
-- Restriction: we pair sets on the same level (because right now everything
-- else would risk getting in the way).
module ProductChanges ℓ (A B : Set ℓ) {{CA : ChangeAlgebra ℓ A}} {{CB : ChangeAlgebra ℓ B}} where
open ≡-Reasoning
-- The simplest possible definition of changes for products.
PChange : A × B → Set ℓ
PChange (a , b) = Δ a × Δ b
-- An interesting alternative definition allows omitting the nil change of a
-- component when that nil change can be computed from the type. For instance, the nil change for integers is always the same.
-- However, the nil change for function isn't always the same (unless we
-- defunctionalize them first), so nil changes for functions can't be omitted.
_⊕_ : (v : A × B) → PChange v → A × B
_⊕_ (a , b) (da , db) = a ⊞ da , b ⊞ db
_⊝_ : A × B → (v : A × B) → PChange v
_⊝_ (aNew , bNew) (a , b) = aNew ⊟ a , bNew ⊟ b
p-nil : (v : A × B) → PChange v
p-nil (a , b) = (nil a , nil b)
p-update-diff : (u v : A × B) → v ⊕ (u ⊝ v) ≡ u
p-update-diff (ua , ub) (va , vb) =
let u = (ua , ub)
v = (va , vb)
in
begin
v ⊕ (u ⊝ v)
≡⟨⟩
(va ⊞ (ua ⊟ va) , vb ⊞ (ub ⊟ vb))
--v ⊕ ((ua ⊟ va , ub ⊟ vb))
≡⟨ cong₂ _,_ (update-diff ua va) (update-diff ub vb)⟩
(ua , ub)
≡⟨⟩
u
∎
p-update-nil : (v : A × B) → v ⊕ (p-nil v) ≡ v
p-update-nil (a , b) =
let v = (a , b)
in
begin
v ⊕ (p-nil v)
≡⟨⟩
(a ⊞ nil a , b ⊞ nil b)
≡⟨ cong₂ _,_ (update-nil a) (update-nil b)⟩
(a , b)
≡⟨⟩
v
∎
changeAlgebra : ChangeAlgebra ℓ (A × B)
changeAlgebra = record
{ Change = PChange
; update = _⊕_
; diff = _⊝_
; nil = p-nil
; isChangeAlgebra = record
{ update-diff = p-update-diff
; update-nil = p-update-nil
}
}
proj₁′ : (v : A × B) → Δ v → Δ (proj₁ v)
proj₁′ (a , b) (da , db) = da
proj₁′Derivative : Derivative proj₁ proj₁′
-- Implementation note: we do not need to pattern match on v and dv because
-- they are records, hence Agda knows that pattern matching on records cannot
-- fail. Technically, the required feature is the eta-rule on records.
proj₁′Derivative v dv = refl
-- An extended explanation.
proj₁′Derivative₁ : Derivative proj₁ proj₁′
proj₁′Derivative₁ (a , b) (da , db) =
let v = (a , b)
dv = (da , db)
in
begin
proj₁ v ⊞ proj₁′ v dv
≡⟨⟩
a ⊞ da
≡⟨⟩
proj₁ (v ⊞ dv)
∎
-- Same for the second extractor.
proj₂′ : (v : A × B) → Δ v → Δ (proj₂ v)
proj₂′ (a , b) (da , db) = db
proj₂′Derivative : Derivative proj₂ proj₂′
proj₂′Derivative v dv = refl
B→A×B = FunctionChanges.changeAlgebra B (A × B)
A→B→A×B = FunctionChanges.changeAlgebra A (B → A × B) {{CA}} {{B→A×B}}
-- Morally, the following is a change:
-- What one could wrongly expect to be the derivative of the constructor:
_,_′-realizer : (a : A) → (da : Δ a) → (b : B) → (db : Δ b) → Δ (a , b)
_,_′-realizer a da b db = da , db
-- That has the correct behavior, in a sense, and it would be in the
-- subset-based formalization in the paper.
--
-- But the above is not even a change, because it does not contain a proof of
-- its own validity, and because after application it does not contain a
-- proof.
--
-- However, the above is (morally) a "realizer" of the actual change, since it
-- only represents its computational behavior, not its proof manipulation.
-- Hence, we need to do some additional work.
_,_′-realizer-correct _,_′-realizer-correct-detailed :
(a : A) → (da : Δ a) → (b : B) → (db : Δ b) →
(a , b ⊞ db) ⊞ (_,_′-realizer a da (b ⊞ db) (nil (b ⊞ db))) ≡ (a , b) ⊞ (_,_′-realizer a da b db)
_,_′-realizer-correct a da b db rewrite update-nil (b ⊞ db) = refl
_,_′-realizer-correct-detailed a da b db =
begin
(a , b ⊞ db) ⊞ (_,_′-realizer a da) (b ⊞ db) (nil (b ⊞ db))
≡⟨⟩
a ⊞ da , b ⊞ db ⊞ (nil (b ⊞ db))
≡⟨ cong (λ □ → a ⊞ da , □) (update-nil (b ⊞ db)) ⟩
a ⊞ da , b ⊞ db
≡⟨⟩
(a , b) ⊞ (_,_′-realizer a da) b db
∎
_,_′ : (a : A) → (da : Δ a) → Δ (_,_ a)
_,_′ a da = record { apply = _,_′-realizer a da ; correct = λ b db → _,_′-realizer-correct a da b db }
_,_′-Derivative : Derivative _,_ _,_′
_,_′-Derivative a da = ext (λ b → cong (_,_ (a ⊞ da)) (update-nil b))
where
open import Postulate.Extensionality
-- Define specialized variant of uncurry, and derive it.
uncurry₀ : ∀ {C : Set ℓ} → (A → B → C) → A × B → C
uncurry₀ f (a , b) = f a b
module _ {C : Set ℓ} {{CC : ChangeAlgebra ℓ C}} where
B→C : ChangeAlgebra ℓ (B → C)
B→C = FunctionChanges.changeAlgebra B C
A→B→C : ChangeAlgebra ℓ (A → B → C)
A→B→C = FunctionChanges.changeAlgebra A (B → C)
A×B→C : ChangeAlgebra ℓ (A × B → C)
A×B→C = FunctionChanges.changeAlgebra (A × B) C
open FunctionChanges using (apply; correct)
uncurry₀′-realizer : (f : A → B → C) → Δ f → (p : A × B) → Δ p → Δ (uncurry₀ f p)
uncurry₀′-realizer f df (a , b) (da , db) = apply (apply df a da) b db
uncurry₀′-realizer-correct uncurry₀′-realizer-correct-detailed :
∀ (f : A → B → C) (df : Δ f) (p : A × B) (dp : Δ p) →
uncurry₀ f (p ⊕ dp) ⊞ uncurry₀′-realizer f df (p ⊕ dp) (nil (p ⊞ dp)) ≡ uncurry₀ f p ⊞ uncurry₀′-realizer f df p dp
-- Hard to read
uncurry₀′-realizer-correct f df (a , b) (da , db)
rewrite sym (correct (apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db)))
| update-nil (b ⊞ db)
| {- cong (λ □ → □ (b ⊞ db)) -} (sym (correct df (a ⊞ da) (nil (a ⊞ da))))
| update-nil (a ⊞ da)
| cong (λ □ → □ (b ⊞ db)) (correct df a da)
| correct (apply df a da) b db
= refl
-- Verbose, but it shows all the intermediate steps.
uncurry₀′-realizer-correct-detailed f df (a , b) (da , db) =
begin
uncurry₀ f (a ⊞ da , b ⊞ db) ⊞ uncurry₀′-realizer f df (a ⊞ da , b ⊞ db) (nil (a ⊞ da , b ⊞ db))
≡⟨⟩
f (a ⊞ da) (b ⊞ db) ⊞ apply (apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))
≡⟨ sym (correct (apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db) (nil (b ⊞ db))) ⟩
(f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db)))
≡⟨ cong-lem₀ ⟩
(f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db)
≡⟨ sym cong-lem₂ ⟩
((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db)
≡⟨ cong-lem₁ ⟩
(f ⊞ df) (a ⊞ da) (b ⊞ db)
≡⟨ cong (λ □ → □ (b ⊞ db)) (correct df a da) ⟩
(f a ⊞ apply df a da) (b ⊞ db)
≡⟨ correct (apply df a da) b db ⟩
f a b ⊞ apply (apply df a da) b db
≡⟨⟩
uncurry₀ f (a , b) ⊞ uncurry₀′-realizer f df (a , b) (da , db)
∎
where
cong-lem₀ :
(f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) ((b ⊞ db) ⊞ (nil (b ⊞ db)))
≡
(f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db)
cong-lem₀ rewrite update-nil (b ⊞ db) = refl
cong-lem₁ :
((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db)
≡
(f ⊞ df) (a ⊞ da) (b ⊞ db)
cong-lem₁ rewrite update-nil (a ⊞ da) = refl
cong-lem₂ :
((f ⊞ df) ((a ⊞ da) ⊞ (nil (a ⊞ da)))) (b ⊞ db)
≡
(f (a ⊞ da) ⊞ apply df (a ⊞ da) (nil (a ⊞ da))) (b ⊞ db)
cong-lem₂ = cong (λ □ → □ (b ⊞ db)) (correct df (a ⊞ da) (nil (a ⊞ da)))
uncurry₀′ : (f : A → B → C) → Δ f → Δ (uncurry f)
uncurry₀′ f df = record
{ apply = uncurry₀′-realizer f df
; correct = uncurry₀′-realizer-correct f df }
-- Now proving that uncurry₀′ is a derivative is trivial!
uncurry₀′Derivative₀ : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′
uncurry₀′Derivative₀ f df = refl
-- If you wonder what's going on, here's the step-by-step proof, going purely by definitional equality.
uncurry₀′Derivative : Derivative {{CB = A×B→C}} uncurry₀ uncurry₀′
uncurry₀′Derivative f df =
begin
uncurry₀ f ⊞ uncurry₀′ f df
≡⟨⟩
(λ {(a , b) → uncurry₀ f (a , b) ⊞ apply (uncurry₀′ f df) (a , b) (nil (a , b))})
≡⟨⟩
(λ {(a , b) → f a b ⊞ apply (apply df a (nil a)) b (nil b)})
≡⟨⟩
(λ {(a , b) → (f a ⊞ apply df a (nil a)) b})
≡⟨⟩
(λ {(a , b) → (f ⊞ df) a b})
≡⟨⟩
(λ {(a , b) → uncurry₀ (f ⊞ df) (a , b)})
≡⟨⟩
uncurry₀ (f ⊞ df)
∎
|
Simplify code
|
Base.Change.Products: Simplify code
These simplifications are inspired by Base.Change.Sums. But they're
still modest.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
5957a0a5f21ebc9ad88ea001c4afa6b381372c04
|
Parametric/Change/Type.agda
|
Parametric/Change/Type.agda
|
import Parametric.Syntax.Type as Type
module Parametric.Change.Type
(Base : Type.Structure)
where
open Type.Structure Base
Structure : Set
Structure = Base → Base
module Structure (ΔBase : Structure) where
ΔType : Type → Type
ΔType (base ι) = base (ΔBase ι)
ΔType (σ ⇒ τ) = σ ⇒ ΔType σ ⇒ ΔType τ
open import Base.Change.Context ΔType public
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Simply-typed changes (Fig. 3 and Fig. 4d)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
module Parametric.Change.Type
(Base : Type.Structure)
where
open Type.Structure Base
-- Extension point: Simply-typed changes of base types.
Structure : Set
Structure = Base → Base
module Structure (ΔBase : Structure) where
-- We provide: Simply-typed changes on simple types.
ΔType : Type → Type
ΔType (base ι) = base (ΔBase ι)
ΔType (σ ⇒ τ) = σ ⇒ ΔType σ ⇒ ΔType τ
-- And we also provide context merging.
open import Base.Change.Context ΔType public
|
Document P.C.Type.
|
Document P.C.Type.
Old-commit-hash: 11e38d018843f21411d014020ce42181e1f77d32
|
Agda
|
mit
|
inc-lc/ilc-agda
|
27037592477c7d9112554d56337e06c5a2c6f0b9
|
Parametric/Syntax/Term.agda
|
Parametric/Syntax/Term.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- The syntax of terms (Fig. 1a and 1b).
------------------------------------------------------------------------
-- The syntax of terms depends on the syntax of simple types
-- (because terms are indexed by types in order to rule out
-- ill-typed terms). But we are in the Parametric.* hierarchy, so
-- we don't know the full syntax of types, only how to lift the
-- syntax of base types into the syntax of simple types. This
-- means that we have to be parametric in the syntax of base
-- types, too.
--
-- In such parametric modules that depend on other parametric
-- modules, we first import our dependencies under a more
-- convenient name.
import Parametric.Syntax.Type as Type
-- Then we start the module proper, with parameters for all
-- extension points of our dependencies. Note that here, the
-- "Structure" naming convenion makes some sense, because we can
-- say that we need some "Type.Structure" in order to define the
-- "Term.Structure".
module Parametric.Syntax.Term
(Base : Type.Structure)
where
-- Now inside the module, we can open our dependencies with the
-- parameters for their extension points. Again, here the name
-- "Structure" makes some sense, because we can say that we want
-- to access the "Type.Structure" that is induced by Base.
open Type.Structure Base
-- At this point, we have dealt with the extension points of our
-- dependencies, and we have all the definitions about simple
-- types, contexts, variables, and variable sets in scope that we
-- provided in Parametric.Syntax.Type. Now we can proceed to
-- define our own extension point, following the pattern
-- explained in Parametric.Syntax.Type.
open import Relation.Binary.PropositionalEquality
open import Function using (_∘_)
open import Data.Unit
open import Data.Sum
-- Our extension point is a set of primitives, indexed by the
-- types of their arguments and their return type. In general, if
-- you're confused about what an extension point means, you might
-- want to open the corresponding module in the Nehemiah
-- hierarchy to see how it is implemented in the example
-- plugin. In this case, that would be the Nehemiah.Syntax.Term
-- module.
Structure : Set₁
Structure = Context → Type → Set
module Structure (Const : Structure) where
import Base.Data.DependentList as DependentList
open DependentList public using (∅ ; _•_)
open DependentList
-- Declarations of Term and Terms to enable mutual recursion.
--
-- Note that terms are indexed by contexts and types. In the
-- paper, we define the abstract syntax of terms in Fig 1a and
-- then define a type system in Fig 1b. All lemmas and theorems
-- then explicitly specify that they only hold for well-typed
-- terms. Here, we use the indices to define a type that can
-- only hold well-typed terms in the first place.
data Term
(Γ : Context) :
(τ : Type) → Set
-- (Terms Γ Σ) represents a list of terms with types from Σ
-- with free variables bound in Γ.
Terms : Context → Context → Set
Terms Γ = DependentList (Term Γ)
-- (Term Γ τ) represents a term of type τ
-- with free variables bound in Γ.
data Term Γ where
-- constants aka. primitives can only occur fully applied.
const : ∀ {Σ τ} →
(c : Const Σ τ) →
(args : Terms Γ Σ) →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
-- we use de Bruijn indicies, so we don't need binding occurrences.
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- Free variables
FV : ∀ {τ Γ} → Term Γ τ → Vars Γ
FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ
FV (const ι ts) = FV-terms ts
FV (var x) = singleton x
FV (abs t) = tail (FV t)
FV (app s t) = FV s ∪ FV t
FV-terms ∅ = none
FV-terms (t • ts) = FV t ∪ FV-terms ts
closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤
closed? t = empty? (FV t)
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weaken-terms : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weaken-terms Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weaken-terms Γ₁≼Γ₂ ∅ = ∅
weaken-terms Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weaken-terms Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ
uncurriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ
uncurriedConst constant = const constant
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ
curryTermConstructor {∅} k = k ∅
curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts))
curriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
curriedConst constant = curryTermConstructor (uncurriedConst constant)
-- HOAS-like smart constructors for lambdas, for different arities.
-- We could also write this:
module NamespaceForBadAbs₁ where
abs₁′ :
∀ {Γ τ₁ τ} →
(Term (τ₁ • Γ) τ₁ → Term (τ₁ • Γ) τ) →
(Term Γ (τ₁ ⇒ τ))
abs₁′ {Γ} {τ₁} = λ f → abs (f (var this))
-- However, this is less general, and it is harder to reuse. In particular,
-- this cannot be used inside abs₂, ..., abs₆.
-- Now, let's write other variants with a loop!
open import Data.Vec using (_∷_; []; Vec; foldr)
open import Data.Nat
module AbsNHelpers where
open import Function
hoasArgType : ∀ {n} → Context → Type → Vec Type n → Set
hoasArgType Γ τ = foldr _ (λ a b → a → b) (Term Γ τ) ∘ Data.Vec.map (Term Γ)
-- That is,
--hoasArgType Γ τ [] = Term Γ τ
--hoasArgType Γ τ (τ₀ ∷ τs) = Term Γ τ₀ → hoasArgType Γ τ τs
hoasResType : ∀ {n} → Type → Vec Type n → Type
hoasResType τ = foldr _ _⇒_ τ
absNType : {n : ℕ} → Vec _ n → Set
absNType τs = ∀ {Γ τ} →
(f : ∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → hoasArgType Γ′ τ τs) →
Term Γ (hoasResType τ τs)
-- A better type for absN but a mess to use due to the proofs (which aren't synthesized, even though maybe they should be?)
-- absN : (n : ℕ) → {_ : n > 0} → absNType n
-- XXX See "how to keep your neighbours in order" for tricks.
-- Please the termination checker by keeping this case separate.
absNBase : ∀ {τ₁} → absNType (τ₁ ∷ [])
absNBase {τ₁} f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this))
-- Otherwise, the recursive step of absN would invoke absN twice, and the
-- termination checker does not figure out that the calls are in fact
-- terminating.
open AbsNHelpers using (absNType; absNBase)
-- XXX: could we be using the same trick as above to take the N implicit
-- arguments individually, rather than in a vector? I can't figure out how,
-- at least not without trying it. But it seems that's what's shown in Agda 2.4.0 release notes!
absN : {n : ℕ} → (τs : Vec _ (suc n)) → absNType τs
absN {zero} (τ₁ ∷ []) = absNBase
absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f =
absNBase (λ {_} {Γ≼Γ′} x₁ →
absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
-- What I'd like to write, avoiding the need for absNBase, but can't because of the termination checker.
{-
absN {zero} (τ₁ ∷ []) f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this))
absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f =
absN (τ₁ ∷ []) (λ {_} {Γ≼Γ′} x₁ →
absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
-}
-- Declare abs₁ .. abs₆ wrappers for more convenient use, allowing implicit
-- type arguments to be synthesized. Somehow, Agda does not manage to
-- synthesize τs by unification.
-- Implicit arguments are reversed when assembling the list, but that's no real problem.
module _ {τ₁ : Type} where
τs₁ = τ₁ ∷ []
abs₁ : absNType τs₁
abs₁ = absN τs₁
module _ {τ₂ : Type} where
τs₂ = τ₂ ∷ τs₁
abs₂ : absNType τs₂
abs₂ = absN τs₂
module _ {τ₃ : Type} where
τs₃ = τ₃ ∷ τs₂
abs₃ : absNType τs₃
abs₃ = absN τs₃
module _ {τ₄ : Type} where
τs₄ = τ₄ ∷ τs₃
abs₄ : absNType τs₄
abs₄ = absN τs₄
module _ {τ₅ : Type} where
τs₅ = τ₅ ∷ τs₄
abs₅ : absNType τs₅
abs₅ = absN τs₅
module _ {τ₆ : Type} where
τs₆ = τ₆ ∷ τs₅
abs₆ : absNType τs₆
abs₆ = absN τs₆
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- The syntax of terms (Fig. 1a and 1b).
------------------------------------------------------------------------
-- The syntax of terms depends on the syntax of simple types
-- (because terms are indexed by types in order to rule out
-- ill-typed terms). But we are in the Parametric.* hierarchy, so
-- we don't know the full syntax of types, only how to lift the
-- syntax of base types into the syntax of simple types. This
-- means that we have to be parametric in the syntax of base
-- types, too.
--
-- In such parametric modules that depend on other parametric
-- modules, we first import our dependencies under a more
-- convenient name.
import Parametric.Syntax.Type as Type
-- Then we start the module proper, with parameters for all
-- extension points of our dependencies. Note that here, the
-- "Structure" naming convenion makes some sense, because we can
-- say that we need some "Type.Structure" in order to define the
-- "Term.Structure".
module Parametric.Syntax.Term
(Base : Type.Structure)
where
-- Now inside the module, we can open our dependencies with the
-- parameters for their extension points. Again, here the name
-- "Structure" makes some sense, because we can say that we want
-- to access the "Type.Structure" that is induced by Base.
open Type.Structure Base
-- At this point, we have dealt with the extension points of our
-- dependencies, and we have all the definitions about simple
-- types, contexts, variables, and variable sets in scope that we
-- provided in Parametric.Syntax.Type. Now we can proceed to
-- define our own extension point, following the pattern
-- explained in Parametric.Syntax.Type.
open import Relation.Binary.PropositionalEquality hiding ([_])
open import Function using (_∘_)
open import Data.Unit
open import Data.Sum
-- Our extension point is a set of primitives, indexed by the
-- types of their arguments and their return type. In general, if
-- you're confused about what an extension point means, you might
-- want to open the corresponding module in the Nehemiah
-- hierarchy to see how it is implemented in the example
-- plugin. In this case, that would be the Nehemiah.Syntax.Term
-- module.
Structure : Set₁
Structure = Context → Type → Set
module Structure (Const : Structure) where
import Base.Data.DependentList as DependentList
open DependentList public using (∅ ; _•_)
open DependentList
-- Declarations of Term and Terms to enable mutual recursion.
--
-- Note that terms are indexed by contexts and types. In the
-- paper, we define the abstract syntax of terms in Fig 1a and
-- then define a type system in Fig 1b. All lemmas and theorems
-- then explicitly specify that they only hold for well-typed
-- terms. Here, we use the indices to define a type that can
-- only hold well-typed terms in the first place.
data Term
(Γ : Context) :
(τ : Type) → Set
-- (Terms Γ Σ) represents a list of terms with types from Σ
-- with free variables bound in Γ.
Terms : Context → Context → Set
Terms Γ = DependentList (Term Γ)
-- (Term Γ τ) represents a term of type τ
-- with free variables bound in Γ.
data Term Γ where
-- constants aka. primitives can only occur fully applied.
const : ∀ {Σ τ} →
(c : Const Σ τ) →
(args : Terms Γ Σ) →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
-- we use de Bruijn indicies, so we don't need binding occurrences.
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- Free variables
FV : ∀ {τ Γ} → Term Γ τ → Vars Γ
FV-terms : ∀ {Σ Γ} → Terms Γ Σ → Vars Γ
FV (const ι ts) = FV-terms ts
FV (var x) = singleton x
FV (abs t) = tail (FV t)
FV (app s t) = FV s ∪ FV t
FV-terms ∅ = none
FV-terms (t • ts) = FV t ∪ FV-terms ts
closed? : ∀ {τ Γ} → (t : Term Γ τ) → (FV t ≡ none) ⊎ ⊤
closed? t = empty? (FV t)
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weaken-terms : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weaken-terms Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weaken-terms Γ₁≼Γ₂ ∅ = ∅
weaken-terms Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weaken-terms Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ
uncurriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ
uncurriedConst constant = const constant
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ
curryTermConstructor {∅} k = k ∅
curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts))
curriedConst : ∀ {Σ τ} → Const Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
curriedConst constant = curryTermConstructor (uncurriedConst constant)
-- HOAS-like smart constructors for lambdas, for different arities.
-- We could also write this:
module NamespaceForBadAbs₁ where
abs₁′ :
∀ {Γ τ₁ τ} →
(Term (τ₁ • Γ) τ₁ → Term (τ₁ • Γ) τ) →
(Term Γ (τ₁ ⇒ τ))
abs₁′ {Γ} {τ₁} = λ f → abs (f (var this))
-- However, this is less general, and it is harder to reuse. In particular,
-- this cannot be used inside abs₂, ..., abs₆.
-- Now, let's write other variants with a loop!
open import Data.Vec using (_∷_; []; Vec; foldr; [_])
open import Data.Nat
module AbsNHelpers where
open import Function
hoasArgType : ∀ {n} → Context → Type → Vec Type n → Set
hoasArgType Γ τ = foldr _ (λ a b → a → b) (Term Γ τ) ∘ Data.Vec.map (Term Γ)
-- That is,
--hoasArgType Γ τ [] = Term Γ τ
--hoasArgType Γ τ (τ₀ ∷ τs) = Term Γ τ₀ → hoasArgType Γ τ τs
hoasResType : ∀ {n} → Type → Vec Type n → Type
hoasResType τ = foldr _ _⇒_ τ
absNType : {n : ℕ} → Vec _ n → Set
absNType τs = ∀ {Γ τ} →
(f : ∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → hoasArgType Γ′ τ τs) →
Term Γ (hoasResType τ τs)
-- A better type for absN but a mess to use due to the proofs (which aren't synthesized, even though maybe they should be?)
-- absN : (n : ℕ) → {_ : n > 0} → absNType n
-- XXX See "how to keep your neighbours in order" for tricks.
-- Please the termination checker by keeping this case separate.
absNBase : ∀ {τ₁} → absNType [ τ₁ ]
absNBase {τ₁} f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this))
-- Otherwise, the recursive step of absN would invoke absN twice, and the
-- termination checker does not figure out that the calls are in fact
-- terminating.
-- What I'd like to write, avoiding the need for absNBase, but can't because of the termination checker.
{-
absN {zero} (τ₁ ∷ []) f = abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this))
absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f =
absN (τ₁ ∷ []) (λ {_} {Γ≼Γ′} x₁ →
absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
-}
--What I have to write instead:
absN : {n : ℕ} → (τs : Vec _ (suc n)) → absNType τs
absN {zero} (τ₁ ∷ []) = absNBase
absN {suc n} (τ₁ ∷ τ₂ ∷ τs) f =
absNBase (λ {_} {Γ≼Γ′} x₁ →
absN {n} (τ₂ ∷ τs) (λ {Γ′₁} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
-- Using a similar trick, we can declare absV which takes the N implicit
-- type arguments individually, collects them and passes them on to absN.
-- This is inspired by what's shown in the Agda 2.4.0 release notes, and
-- relies critically on support for varying arity. To collect them, we need
-- to use an accumulator argument.
absVType : ∀ n {m} (τs : Vec Type m) → Set
absVType 0 τs = absNType τs
absVType (suc n) τs = {τᵢ : Type} → absVType n (τᵢ ∷ τs)
-- XXX
absVAux : ∀ {m} → (τs : Vec Type m) → ∀ n → absVType (suc n) τs
absVAux τs zero {τᵢ} = absN (τᵢ ∷ τs)
absVAux τs (suc n) {τᵢ} = absVAux (τᵢ ∷ τs) n
absV = absVAux []
open AbsNHelpers using (absV) public
-- Declare abs₁ .. abs₆ wrappers for more convenient use, allowing implicit
-- type arguments to be synthesized. Somehow, Agda does not manage to
-- synthesize τs by unification.
-- Implicit arguments are reversed when assembling the list, but that's no real problem.
abs₁ = absV 0
abs₂ = absV 1
abs₃ = absV 2
abs₄ = absV 3
abs₅ = absV 4
abs₆ = absV 5
{-
abs₁
Have: {τ₁ : Type} {Γ : Context} {τ : Type} →
({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ) →
Term Γ (τ₁ Type.Structure.⇒ τ)
abs₂
Have: {τ₁ : Type} {τ₁ = τ₂ : Type} {Γ : Context} {τ : Type} →
({Γ′ : Context} {Γ≼Γ′ : Γ ≼ Γ′} →
Term Γ′ τ₂ → Term Γ′ τ₁ → Term Γ′ τ) →
Term Γ (τ₂ Type.Structure.⇒ τ₁ Type.Structure.⇒ τ)
-}
|
Remove remaining duplication between abs₁ ... abs₆
|
Remove remaining duplication between abs₁ ... abs₆
This relies on Agda 2.4.0's varying arity in a more essential way.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
05536450804504640dcab4d2a372088bbc7ef7b3
|
src/fot/FOTC/Program/McCarthy91/PropertiesATP.agda
|
src/fot/FOTC/Program/McCarthy91/PropertiesATP.agda
|
------------------------------------------------------------------------------
-- Main properties of the McCarthy 91 function
------------------------------------------------------------------------------
{-# OPTIONS --no-universe-polymorphism #-}
{-# OPTIONS --without-K #-}
-- The main properties proved of the McCarthy 91 function (called
-- f₉₁) are
-- 1. The function always terminates.
-- 2. For all n, n < f₉₁ n + 11.
-- 3. For all n > 100, then f₉₁ n = n - 10.
-- 4. For all n <= 100, then f₉₁ n = 91.
-- N.B This module does not contain combined proofs, but it imports
-- modules which contain combined proofs.
module FOTC.Program.McCarthy91.PropertiesATP where
open import FOTC.Base
open import FOTC.Data.Nat
open import FOTC.Data.Nat.Inequalities
open import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP
using ( x>y→x≤y→⊥ )
open import FOTC.Data.Nat.Inequalities.PropertiesATP
using ( x>y∨x≯y
; x≯y→x≤y
; x≯Sy→x≯y∨x≡Sy
; x+k<y+k→x<y
; <-trans
)
open import FOTC.Data.Nat.PropertiesATP
using ( ∸-N
; +-N
)
open import FOTC.Data.Nat.UnaryNumbers
open import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP
using ( x<x+11 )
open import FOTC.Data.Nat.UnaryNumbers.TotalityATP
using ( 10-N
; 11-N
; 89-N
; 90-N
; 91-N
; 92-N
; 93-N
; 94-N
; 95-N
; 96-N
; 97-N
; 98-N
; 99-N
; 100-N
)
open import FOTC.Program.McCarthy91.ArithmeticATP
open import FOTC.Program.McCarthy91.AuxiliaryPropertiesATP
open import FOTC.Program.McCarthy91.McCarthy91
open import FOTC.Program.McCarthy91.WF-Relation
open import FOTC.Program.McCarthy91.WF-Relation.LT2WF-RelationATP
open import FOTC.Program.McCarthy91.WF-Relation.Induction.Acc.WF-ATP
------------------------------------------------------------------------------
-- For all n > 100, then f₉₁ n = n - 10.
--
-- N.B. (21 November 2013). The hypothesis N n is not necessary.
postulate f₉₁-x>100 : ∀ n → N n → n > 100' → f₉₁ n ≡ n ∸ 10'
{-# ATP prove f₉₁-x>100 #-}
-- For all n <= 100, then f₉₁ n = 91.
f₉₁-x≯100 : ∀ {n} → N n → n ≯ 100' → f₉₁ n ≡ 91'
f₉₁-x≯100 = ◁-wfind A h
where
A : D → Set
A d = d ≯ 100' → f₉₁ d ≡ 91'
h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m
h {m} Nm f with x>y∨x≯y Nm 100-N
... | inj₁ m>100 = λ m≯100 →
⊥-elim (x>y→x≤y→⊥ Nm 100-N m>100 (x≯y→x≤y Nm 100-N m≯100))
... | inj₂ m≯100 with x≯Sy→x≯y∨x≡Sy Nm 99-N m≯100
... | inj₂ m≡100 = λ _ → f₉₁-x≡y f₉₁-100 m≡100
... | inj₁ m≯99 with x≯Sy→x≯y∨x≡Sy Nm 98-N m≯99
... | inj₂ m≡99 = λ _ → f₉₁-x≡y f₉₁-99 m≡99
... | inj₁ m≯98 with x≯Sy→x≯y∨x≡Sy Nm 97-N m≯98
... | inj₂ m≡98 = λ _ → f₉₁-x≡y f₉₁-98 m≡98
... | inj₁ m≯97 with x≯Sy→x≯y∨x≡Sy Nm 96-N m≯97
... | inj₂ m≡97 = λ _ → f₉₁-x≡y f₉₁-97 m≡97
... | inj₁ m≯96 with x≯Sy→x≯y∨x≡Sy Nm 95-N m≯96
... | inj₂ m≡96 = λ _ → f₉₁-x≡y f₉₁-96 m≡96
... | inj₁ m≯95 with x≯Sy→x≯y∨x≡Sy Nm 94-N m≯95
... | inj₂ m≡95 = λ _ → f₉₁-x≡y f₉₁-95 m≡95
... | inj₁ m≯94 with x≯Sy→x≯y∨x≡Sy Nm 93-N m≯94
... | inj₂ m≡94 = λ _ → f₉₁-x≡y f₉₁-94 m≡94
... | inj₁ m≯93 with x≯Sy→x≯y∨x≡Sy Nm 92-N m≯93
... | inj₂ m≡93 = λ _ → f₉₁-x≡y f₉₁-93 m≡93
... | inj₁ m≯92 with x≯Sy→x≯y∨x≡Sy Nm 91-N m≯92
... | inj₂ m≡92 = λ _ → f₉₁-x≡y f₉₁-92 m≡92
... | inj₁ m≯91 with x≯Sy→x≯y∨x≡Sy Nm 90-N m≯91
... | inj₂ m≡91 = λ _ → f₉₁-x≡y f₉₁-91 m≡91
... | inj₁ m≯90 with x≯Sy→x≯y∨x≡Sy Nm 89-N m≯90
... | inj₂ m≡90 = λ _ → f₉₁-x≡y f₉₁-90 m≡90
... | inj₁ m≯89 = λ _ → f₉₁-m≯89
where
m≤89 : m ≤ 89'
m≤89 = x≯y→x≤y Nm 89-N m≯89
f₉₁-m+11 : m + 11' ≯ 100' → f₉₁ (m + 11') ≡ 91'
f₉₁-m+11 = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm))
f₉₁-m≯89 : f₉₁ m ≡ 91'
f₉₁-m≯89 = f₉₁-x≯100-helper m 91' m≯100
(f₉₁-m+11 (x≤89→x+11≯100 Nm m≤89))
f₉₁-91
-- The function always terminates.
f₉₁-N : ∀ {n} → N n → N (f₉₁ n)
f₉₁-N {n} Nn with x>y∨x≯y Nn 100-N
... | inj₁ n>100 = subst N (sym (f₉₁-x>100 n Nn n>100)) (∸-N Nn 10-N)
... | inj₂ n≯100 = subst N (sym (f₉₁-x≯100 Nn n≯100)) 91-N
-- For all n, n < f₉₁ n + 11.
f₉₁-ineq : ∀ {n} → N n → n < f₉₁ n + 11'
f₉₁-ineq = ◁-wfind A h
where
A : D → Set
A d = d < f₉₁ d + 11'
h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m
h {m} Nm f with x>y∨x≯y Nm 100-N
... | inj₁ m>100 = x>100→x<f₉₁-x+11 Nm m>100
... | inj₂ m≯100 =
let f₉₁-m+11-N : N (f₉₁ (m + 11'))
f₉₁-m+11-N = f₉₁-N (+-N Nm 11-N)
h₁ : A (m + 11')
h₁ = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm))
m<f₉₁m+11 : m < f₉₁ (m + 11')
m<f₉₁m+11 = x+k<y+k→x<y Nm f₉₁-m+11-N 11-N h₁
h₂ : A (f₉₁ (m + 11'))
h₂ = f f₉₁-m+11-N (<→◁ f₉₁-m+11-N Nm m≯100 m<f₉₁m+11)
in (<-trans Nm f₉₁-m+11-N (x+11-N (f₉₁-N Nm))
m<f₉₁m+11
(f₉₁x+11<f₉₁x+11 m m≯100 h₂))
|
------------------------------------------------------------------------------
-- Main properties of the McCarthy 91 function
------------------------------------------------------------------------------
{-# OPTIONS --no-universe-polymorphism #-}
{-# OPTIONS --without-K #-}
-- The main properties proved of the McCarthy 91 function (called
-- f₉₁) are
-- 1. The function always terminates.
-- 2. For all n, n < f₉₁ n + 11.
-- 3. For all n > 100, then f₉₁ n = n - 10.
-- 4. For all n <= 100, then f₉₁ n = 91.
-- N.B This module does not contain combined proofs, but it imports
-- modules which contain combined proofs.
module FOTC.Program.McCarthy91.PropertiesATP where
open import FOTC.Base
open import FOTC.Data.Nat
open import FOTC.Data.Nat.Inequalities
open import FOTC.Data.Nat.Inequalities.EliminationPropertiesATP
using ( x>y→x≤y→⊥ )
open import FOTC.Data.Nat.Inequalities.PropertiesATP
using ( x>y∨x≯y
; x≯y→x≤y
; x≯Sy→x≯y∨x≡Sy
; x+k<y+k→x<y
; <-trans
)
open import FOTC.Data.Nat.PropertiesATP
using ( ∸-N
; +-N
)
open import FOTC.Data.Nat.UnaryNumbers
open import FOTC.Data.Nat.UnaryNumbers.Inequalities.PropertiesATP
using ( x<x+11 )
open import FOTC.Data.Nat.UnaryNumbers.TotalityATP
using ( 10-N
; 11-N
; 89-N
; 90-N
; 91-N
; 92-N
; 93-N
; 94-N
; 95-N
; 96-N
; 97-N
; 98-N
; 99-N
; 100-N
)
open import FOTC.Program.McCarthy91.ArithmeticATP
open import FOTC.Program.McCarthy91.AuxiliaryPropertiesATP
open import FOTC.Program.McCarthy91.McCarthy91
open import FOTC.Program.McCarthy91.WF-Relation
open import FOTC.Program.McCarthy91.WF-Relation.LT2WF-RelationATP
open import FOTC.Program.McCarthy91.WF-Relation.Induction.Acc.WF-ATP
------------------------------------------------------------------------------
-- For all n > 100, then f₉₁ n = n - 10.
postulate f₉₁-x>100 : ∀ n → n > 100' → f₉₁ n ≡ n ∸ 10'
{-# ATP prove f₉₁-x>100 #-}
-- For all n <= 100, then f₉₁ n = 91.
f₉₁-x≯100 : ∀ {n} → N n → n ≯ 100' → f₉₁ n ≡ 91'
f₉₁-x≯100 = ◁-wfind A h
where
A : D → Set
A d = d ≯ 100' → f₉₁ d ≡ 91'
h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m
h {m} Nm f with x>y∨x≯y Nm 100-N
... | inj₁ m>100 = λ m≯100 →
⊥-elim (x>y→x≤y→⊥ Nm 100-N m>100 (x≯y→x≤y Nm 100-N m≯100))
... | inj₂ m≯100 with x≯Sy→x≯y∨x≡Sy Nm 99-N m≯100
... | inj₂ m≡100 = λ _ → f₉₁-x≡y f₉₁-100 m≡100
... | inj₁ m≯99 with x≯Sy→x≯y∨x≡Sy Nm 98-N m≯99
... | inj₂ m≡99 = λ _ → f₉₁-x≡y f₉₁-99 m≡99
... | inj₁ m≯98 with x≯Sy→x≯y∨x≡Sy Nm 97-N m≯98
... | inj₂ m≡98 = λ _ → f₉₁-x≡y f₉₁-98 m≡98
... | inj₁ m≯97 with x≯Sy→x≯y∨x≡Sy Nm 96-N m≯97
... | inj₂ m≡97 = λ _ → f₉₁-x≡y f₉₁-97 m≡97
... | inj₁ m≯96 with x≯Sy→x≯y∨x≡Sy Nm 95-N m≯96
... | inj₂ m≡96 = λ _ → f₉₁-x≡y f₉₁-96 m≡96
... | inj₁ m≯95 with x≯Sy→x≯y∨x≡Sy Nm 94-N m≯95
... | inj₂ m≡95 = λ _ → f₉₁-x≡y f₉₁-95 m≡95
... | inj₁ m≯94 with x≯Sy→x≯y∨x≡Sy Nm 93-N m≯94
... | inj₂ m≡94 = λ _ → f₉₁-x≡y f₉₁-94 m≡94
... | inj₁ m≯93 with x≯Sy→x≯y∨x≡Sy Nm 92-N m≯93
... | inj₂ m≡93 = λ _ → f₉₁-x≡y f₉₁-93 m≡93
... | inj₁ m≯92 with x≯Sy→x≯y∨x≡Sy Nm 91-N m≯92
... | inj₂ m≡92 = λ _ → f₉₁-x≡y f₉₁-92 m≡92
... | inj₁ m≯91 with x≯Sy→x≯y∨x≡Sy Nm 90-N m≯91
... | inj₂ m≡91 = λ _ → f₉₁-x≡y f₉₁-91 m≡91
... | inj₁ m≯90 with x≯Sy→x≯y∨x≡Sy Nm 89-N m≯90
... | inj₂ m≡90 = λ _ → f₉₁-x≡y f₉₁-90 m≡90
... | inj₁ m≯89 = λ _ → f₉₁-m≯89
where
m≤89 : m ≤ 89'
m≤89 = x≯y→x≤y Nm 89-N m≯89
f₉₁-m+11 : m + 11' ≯ 100' → f₉₁ (m + 11') ≡ 91'
f₉₁-m+11 = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm))
f₉₁-m≯89 : f₉₁ m ≡ 91'
f₉₁-m≯89 = f₉₁-x≯100-helper m 91' m≯100
(f₉₁-m+11 (x≤89→x+11≯100 Nm m≤89))
f₉₁-91
-- The function always terminates.
f₉₁-N : ∀ {n} → N n → N (f₉₁ n)
f₉₁-N {n} Nn with x>y∨x≯y Nn 100-N
... | inj₁ n>100 = subst N (sym (f₉₁-x>100 n n>100)) (∸-N Nn 10-N)
... | inj₂ n≯100 = subst N (sym (f₉₁-x≯100 Nn n≯100)) 91-N
-- For all n, n < f₉₁ n + 11.
f₉₁-ineq : ∀ {n} → N n → n < f₉₁ n + 11'
f₉₁-ineq = ◁-wfind A h
where
A : D → Set
A d = d < f₉₁ d + 11'
h : ∀ {m} → N m → (∀ {k} → N k → k ◁ m → A k) → A m
h {m} Nm f with x>y∨x≯y Nm 100-N
... | inj₁ m>100 = x>100→x<f₉₁-x+11 Nm m>100
... | inj₂ m≯100 =
let f₉₁-m+11-N : N (f₉₁ (m + 11'))
f₉₁-m+11-N = f₉₁-N (+-N Nm 11-N)
h₁ : A (m + 11')
h₁ = f (x+11-N Nm) (<→◁ (x+11-N Nm) Nm m≯100 (x<x+11 Nm))
m<f₉₁m+11 : m < f₉₁ (m + 11')
m<f₉₁m+11 = x+k<y+k→x<y Nm f₉₁-m+11-N 11-N h₁
h₂ : A (f₉₁ (m + 11'))
h₂ = f f₉₁-m+11-N (<→◁ f₉₁-m+11-N Nm m≯100 m<f₉₁m+11)
in (<-trans Nm f₉₁-m+11-N (x+11-N (f₉₁-N Nm))
m<f₉₁m+11
(f₉₁x+11<f₉₁x+11 m m≯100 h₂))
|
Revert "Added a non-required totality hypothesis."
|
Revert "Added a non-required totality hypothesis."
This reverts commit 57708b770c00d1ba6276cda0f02752e147f2f533.
|
Agda
|
mit
|
asr/fotc,asr/fotc
|
f04e665aed5f41deea97dd4b7160ad5ee957e41a
|
Nehemiah/Change/Validity.agda
|
Nehemiah/Change/Validity.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes with the Nehemiah plugin.
------------------------------------------------------------------------
module Nehemiah.Change.Validity where
open import Nehemiah.Syntax.Type
open import Nehemiah.Denotation.Value
import Parametric.Change.Validity ⟦_⟧Base as Validity
open import Nehemiah.Change.Type
open import Nehemiah.Change.Value
open import Data.Integer
open import Structure.Bag.Nehemiah
open import Base.Change.Algebra
open import Level
change-algebra-base : ∀ ι → ChangeAlgebra zero ⟦ ι ⟧Base
change-algebra-base base-int = GroupChanges.changeAlgebra ℤ
change-algebra-base base-bag = GroupChanges.changeAlgebra Bag
change-algebra-base-family : ChangeAlgebraFamily zero ⟦_⟧Base
change-algebra-base-family = family change-algebra-base
open Validity.Structure change-algebra-base-family public
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes with the Nehemiah plugin.
------------------------------------------------------------------------
module Nehemiah.Change.Validity where
open import Nehemiah.Syntax.Type
open import Nehemiah.Denotation.Value
import Parametric.Change.Validity ⟦_⟧Base as Validity
open import Nehemiah.Change.Type
open import Nehemiah.Change.Value
open import Data.Integer
open import Structure.Bag.Nehemiah
open import Base.Change.Algebra
open import Level
change-algebra-base : ∀ ι → ChangeAlgebra zero ⟦ ι ⟧Base
change-algebra-base base-int = GroupChanges.changeAlgebra ℤ {{abelian-int}}
change-algebra-base base-bag = GroupChanges.changeAlgebra Bag {{abelian-bag}}
instance
change-algebra-base-family : ChangeAlgebraFamily zero ⟦_⟧Base
change-algebra-base-family = family change-algebra-base
--open Validity.Structure change-algebra-base-family public --XXX agda/agda#1985.
open Validity.Structure public
|
Update Nehemiah.Change.Validity
|
Update Nehemiah.Change.Validity
* Adapt to weaker instance resolution.
* Omit module application that runs into agda/agda#1985.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
94c667f3d542af3d0d5f3f603fe4d1aeccaf6e95
|
New/Changes.agda
|
New/Changes.agda
|
module New.Changes where
open import Relation.Binary.PropositionalEquality
open import Level
open import Data.Unit
open import Data.Product
record IsChAlg {ℓ : Level} (A : Set ℓ) (Ch : Set ℓ) : Set (suc ℓ) where
field
_⊕_ : A → Ch → A
_⊝_ : A → A → Ch
valid : A → Ch → Set ℓ
⊝-valid : ∀ (a b : A) → valid a (b ⊝ a)
⊕-⊝ : (b a : A) → a ⊕ (b ⊝ a) ≡ b
infixl 6 _⊕_ _⊝_
Δ : A → Set ℓ
Δ a = Σ[ da ∈ Ch ] (valid a da)
update-diff = ⊕-⊝
nil : A → Ch
nil a = a ⊝ a
nil-valid : (a : A) → valid a (nil a)
nil-valid a = ⊝-valid a a
update-nil : (a : A) → a ⊕ nil a ≡ a
update-nil a = update-diff a a
record ChAlg {ℓ : Level} (A : Set ℓ) : Set (suc ℓ) where
field
Ch : Set ℓ
isChAlg : IsChAlg A Ch
open IsChAlg isChAlg public
open ChAlg {{...}} public hiding (Ch)
Ch : ∀ {ℓ} (A : Set ℓ) → {{CA : ChAlg A}} → Set ℓ
Ch A {{CA}} = ChAlg.Ch CA
-- Huge hack, but it does gives the output you want to see in all cases I've seen.
{-# DISPLAY IsChAlg.valid x = valid #-}
{-# DISPLAY ChAlg.valid x = valid #-}
{-# DISPLAY IsChAlg._⊕_ x = _⊕_ #-}
{-# DISPLAY ChAlg._⊕_ x = _⊕_ #-}
{-# DISPLAY IsChAlg.nil x = nil #-}
{-# DISPLAY ChAlg.nil x = nil #-}
{-# DISPLAY IsChAlg._⊝_ x = _⊝_ #-}
{-# DISPLAY ChAlg._⊝_ x = _⊝_ #-}
module _ {ℓ₁} {ℓ₂}
{A : Set ℓ₁} {B : Set ℓ₂} {{CA : ChAlg A}} {{CB : ChAlg B}} where
private
fCh = A → Ch A → Ch B
_f⊕_ : (A → B) → fCh → A → B
_f⊕_ = λ f df a → f a ⊕ df a (nil a)
_f⊝_ : (g f : A → B) → fCh
_f⊝_ = λ g f a da → g (a ⊕ da) ⊝ f a
open ≡-Reasoning
open import Postulate.Extensionality
IsDerivative : ∀ (f : A → B) → (df : fCh) → Set (ℓ₁ ⊔ ℓ₂)
IsDerivative f df = ∀ a da (v : valid a da) → f (a ⊕ da) ≡ f a ⊕ df a da
instance
funCA : ChAlg (A → B)
private
funUpdateDiff : ∀ g f a → (f f⊕ (g f⊝ f)) a ≡ g a
funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a)
funCA = record
{ Ch = A → Ch A → Ch B
; isChAlg = record
{ _⊕_ = _f⊕_
; _⊝_ = _f⊝_
; valid = λ f df → ∀ a da (v : valid a da) →
valid (f a) (df a da) ×
(f f⊕ df) (a ⊕ da) ≡ f a ⊕ df a da
; ⊝-valid = λ f g a da (v : valid a da) →
⊝-valid (f a) (g (a ⊕ da))
, ( begin
f (a ⊕ da) ⊕ (g (a ⊕ da ⊕ nil (a ⊕ da)) ⊝ f (a ⊕ da))
≡⟨ cong (λ □ → f (a ⊕ da) ⊕ (g □ ⊝ f (a ⊕ da)))
(update-nil (a ⊕ da)) ⟩
f (a ⊕ da) ⊕ (g (a ⊕ da) ⊝ f (a ⊕ da))
≡⟨ update-diff (g (a ⊕ da)) (f (a ⊕ da)) ⟩
g (a ⊕ da)
≡⟨ sym (update-diff (g (a ⊕ da)) (f a)) ⟩
f a ⊕ (g (a ⊕ da) ⊝ f a)
∎)
; ⊕-⊝ = λ g f → ext (funUpdateDiff g f)
} }
nil-is-derivative : ∀ (f : A → B) → IsDerivative f (nil f)
nil-is-derivative f a da v =
begin
f (a ⊕ da)
≡⟨ sym (cong (λ □ → □ (_⊕_ a da)) (update-nil f)) ⟩
(f ⊕ nil f) (a ⊕ da)
≡⟨ proj₂ (nil-valid f a da v) ⟩
f a ⊕ (nil f a da)
∎
private
_p⊕_ : A × B → Ch A × Ch B → A × B
_p⊕_ (a , b) (da , db) = a ⊕ da , b ⊕ db
_p⊝_ : A × B → A × B → Ch A × Ch B
_p⊝_ (a2 , b2) (a1 , b1) = a2 ⊝ a1 , b2 ⊝ b1
pvalid : A × B → Ch A × Ch B → Set (ℓ₂ ⊔ ℓ₁)
pvalid (a , b) (da , db) = valid a da × valid b db
p⊝-valid : (p1 p2 : A × B) → pvalid p1 (p2 p⊝ p1)
p⊝-valid (a1 , b1) (a2 , b2) = ⊝-valid a1 a2 , ⊝-valid b1 b2
p⊕-⊝ : (p2 p1 : A × B) → p1 p⊕ (p2 p⊝ p1) ≡ p2
p⊕-⊝ (a2 , b2) (a1 , b1) = cong₂ _,_ (⊕-⊝ a2 a1) (⊕-⊝ b2 b1)
instance
pairCA : ChAlg (A × B)
pairCA = record
{ Ch = Ch A × Ch B
; isChAlg = record
{ _⊕_ = _p⊕_
; _⊝_ = _p⊝_
; valid = pvalid
; ⊝-valid = p⊝-valid
; ⊕-⊝ = p⊕-⊝
} }
open import Data.Integer
open import Theorem.Groups-Nehemiah
instance
intCA : ChAlg ℤ
intCA = record
{ Ch = ℤ
; isChAlg = record
{ _⊕_ = _+_
; _⊝_ = _-_
; valid = λ a b → ⊤
; ⊝-valid = λ a b → tt
; ⊕-⊝ = λ b a → n+[m-n]=m {a} {b} } }
|
module New.Changes where
open import Relation.Binary.PropositionalEquality
open import Level
open import Data.Unit
open import Data.Product
open import Data.Sum
record IsChAlg {ℓ : Level} (A : Set ℓ) (Ch : Set ℓ) : Set (suc ℓ) where
field
_⊕_ : A → Ch → A
_⊝_ : A → A → Ch
valid : A → Ch → Set ℓ
⊝-valid : ∀ (a b : A) → valid a (b ⊝ a)
⊕-⊝ : (b a : A) → a ⊕ (b ⊝ a) ≡ b
infixl 6 _⊕_ _⊝_
Δ : A → Set ℓ
Δ a = Σ[ da ∈ Ch ] (valid a da)
update-diff = ⊕-⊝
nil : A → Ch
nil a = a ⊝ a
nil-valid : (a : A) → valid a (nil a)
nil-valid a = ⊝-valid a a
update-nil : (a : A) → a ⊕ nil a ≡ a
update-nil a = update-diff a a
record ChAlg {ℓ : Level} (A : Set ℓ) : Set (suc ℓ) where
field
Ch : Set ℓ
isChAlg : IsChAlg A Ch
open IsChAlg isChAlg public
open ChAlg {{...}} public hiding (Ch)
Ch : ∀ {ℓ} (A : Set ℓ) → {{CA : ChAlg A}} → Set ℓ
Ch A {{CA}} = ChAlg.Ch CA
-- Huge hack, but it does gives the output you want to see in all cases I've seen.
{-# DISPLAY IsChAlg.valid x = valid #-}
{-# DISPLAY ChAlg.valid x = valid #-}
{-# DISPLAY IsChAlg._⊕_ x = _⊕_ #-}
{-# DISPLAY ChAlg._⊕_ x = _⊕_ #-}
{-# DISPLAY IsChAlg.nil x = nil #-}
{-# DISPLAY ChAlg.nil x = nil #-}
{-# DISPLAY IsChAlg._⊝_ x = _⊝_ #-}
{-# DISPLAY ChAlg._⊝_ x = _⊝_ #-}
module _ {ℓ₁} {ℓ₂}
{A : Set ℓ₁} {B : Set ℓ₂} {{CA : ChAlg A}} {{CB : ChAlg B}} where
private
fCh = A → Ch A → Ch B
_f⊕_ : (A → B) → fCh → A → B
_f⊕_ = λ f df a → f a ⊕ df a (nil a)
_f⊝_ : (g f : A → B) → fCh
_f⊝_ = λ g f a da → g (a ⊕ da) ⊝ f a
open ≡-Reasoning
open import Postulate.Extensionality
IsDerivative : ∀ (f : A → B) → (df : fCh) → Set (ℓ₁ ⊔ ℓ₂)
IsDerivative f df = ∀ a da (v : valid a da) → f (a ⊕ da) ≡ f a ⊕ df a da
instance
funCA : ChAlg (A → B)
private
funUpdateDiff : ∀ g f a → (f f⊕ (g f⊝ f)) a ≡ g a
funUpdateDiff g f a rewrite update-nil a = update-diff (g a) (f a)
funCA = record
{ Ch = A → Ch A → Ch B
; isChAlg = record
{ _⊕_ = _f⊕_
; _⊝_ = _f⊝_
; valid = λ f df → ∀ a da (v : valid a da) →
valid (f a) (df a da) ×
(f f⊕ df) (a ⊕ da) ≡ f a ⊕ df a da
; ⊝-valid = λ f g a da (v : valid a da) →
⊝-valid (f a) (g (a ⊕ da))
, ( begin
f (a ⊕ da) ⊕ (g (a ⊕ da ⊕ nil (a ⊕ da)) ⊝ f (a ⊕ da))
≡⟨ cong (λ □ → f (a ⊕ da) ⊕ (g □ ⊝ f (a ⊕ da)))
(update-nil (a ⊕ da)) ⟩
f (a ⊕ da) ⊕ (g (a ⊕ da) ⊝ f (a ⊕ da))
≡⟨ update-diff (g (a ⊕ da)) (f (a ⊕ da)) ⟩
g (a ⊕ da)
≡⟨ sym (update-diff (g (a ⊕ da)) (f a)) ⟩
f a ⊕ (g (a ⊕ da) ⊝ f a)
∎)
; ⊕-⊝ = λ g f → ext (funUpdateDiff g f)
} }
nil-is-derivative : ∀ (f : A → B) → IsDerivative f (nil f)
nil-is-derivative f a da v =
begin
f (a ⊕ da)
≡⟨ sym (cong (λ □ → □ (_⊕_ a da)) (update-nil f)) ⟩
(f ⊕ nil f) (a ⊕ da)
≡⟨ proj₂ (nil-valid f a da v) ⟩
f a ⊕ (nil f a da)
∎
private
_p⊕_ : A × B → Ch A × Ch B → A × B
_p⊕_ (a , b) (da , db) = a ⊕ da , b ⊕ db
_p⊝_ : A × B → A × B → Ch A × Ch B
_p⊝_ (a2 , b2) (a1 , b1) = a2 ⊝ a1 , b2 ⊝ b1
pvalid : A × B → Ch A × Ch B → Set (ℓ₂ ⊔ ℓ₁)
pvalid (a , b) (da , db) = valid a da × valid b db
p⊝-valid : (p1 p2 : A × B) → pvalid p1 (p2 p⊝ p1)
p⊝-valid (a1 , b1) (a2 , b2) = ⊝-valid a1 a2 , ⊝-valid b1 b2
p⊕-⊝ : (p2 p1 : A × B) → p1 p⊕ (p2 p⊝ p1) ≡ p2
p⊕-⊝ (a2 , b2) (a1 , b1) = cong₂ _,_ (⊕-⊝ a2 a1) (⊕-⊝ b2 b1)
instance
pairCA : ChAlg (A × B)
pairCA = record
{ Ch = Ch A × Ch B
; isChAlg = record
{ _⊕_ = _p⊕_
; _⊝_ = _p⊝_
; valid = pvalid
; ⊝-valid = p⊝-valid
; ⊕-⊝ = p⊕-⊝
} }
private
SumChange = (Ch A ⊎ Ch B) ⊎ (A ⊎ B)
data SumChange2 : Set (ℓ₁ ⊔ ℓ₂) where
ch₁ : (da : Ch A) → SumChange2
ch₂ : (db : Ch B) → SumChange2
rp : (s : A ⊎ B) → SumChange2
convert : SumChange → SumChange2
convert (inj₁ (inj₁ da)) = ch₁ da
convert (inj₁ (inj₂ db)) = ch₂ db
convert (inj₂ s) = rp s
convert₁ : SumChange2 → SumChange
convert₁ (ch₁ da) = inj₁ (inj₁ da)
convert₁ (ch₂ db) = inj₁ (inj₂ db)
convert₁ (rp s) = inj₂ s
data SValid : A ⊎ B → SumChange → Set (ℓ₁ ⊔ ℓ₂) where
sv₁ : ∀ (a : A) (da : Ch A) (ada : valid a da) → SValid (inj₁ a) (convert₁ (ch₁ da))
sv₂ : ∀ (b : B) (db : Ch B) (bdb : valid b db) → SValid (inj₂ b) (convert₁ (ch₂ db))
svrp : ∀ (s₁ s₂ : A ⊎ B) → SValid s₁ (convert₁ (rp s₂))
inv1 : ∀ ds → convert₁ (convert ds) ≡ ds
inv1 (inj₁ (inj₁ da)) = refl
inv1 (inj₁ (inj₂ db)) = refl
inv1 (inj₂ s) = refl
inv2 : ∀ ds → convert (convert₁ ds) ≡ ds
inv2 (ch₁ da) = refl
inv2 (ch₂ db) = refl
inv2 (rp s) = refl
private
s⊕2 : A ⊎ B → SumChange2 → A ⊎ B
s⊕2 (inj₁ a) (ch₁ da) = inj₁ (a ⊕ da)
s⊕2 (inj₂ b) (ch₂ db) = inj₂ (b ⊕ db)
s⊕2 (inj₂ b) (ch₁ da) = inj₂ b -- invalid
s⊕2 (inj₁ a) (ch₂ db) = inj₁ a -- invalid
s⊕2 s (rp s₁) = s₁
s⊕ : A ⊎ B → SumChange → A ⊎ B
s⊕ s ds = s⊕2 s (convert ds)
s⊝2 : A ⊎ B → A ⊎ B → SumChange2
s⊝2 (inj₁ x2) (inj₁ x1) = ch₁ (x2 ⊝ x1)
s⊝2 (inj₂ y2) (inj₂ y1) = ch₂ (y2 ⊝ y1)
s⊝2 s2 s1 = rp s2
s⊝ : A ⊎ B → A ⊎ B → SumChange
s⊝ s2 s1 = convert₁ (s⊝2 s2 s1)
s⊝-valid : (a b : A ⊎ B) → SValid a (s⊝ b a)
s⊝-valid (inj₁ x1) (inj₁ x2) = sv₁ x1 (x2 ⊝ x1) (⊝-valid x1 x2)
s⊝-valid (inj₂ y1) (inj₂ y2) = sv₂ y1 (y2 ⊝ y1) (⊝-valid y1 y2)
s⊝-valid s1@(inj₁ x) s2@(inj₂ y) = svrp s1 s2
s⊝-valid s1@(inj₂ y) s2@(inj₁ x) = svrp s1 s2
s⊕-⊝ : (b a : A ⊎ B) → s⊕ a (s⊝ b a) ≡ b
s⊕-⊝ (inj₁ x2) (inj₁ x1) rewrite ⊕-⊝ x2 x1 = refl
s⊕-⊝ (inj₁ x2) (inj₂ y1) = refl
s⊕-⊝ (inj₂ y2) (inj₁ x1) = refl
s⊕-⊝ (inj₂ y2) (inj₂ y1) rewrite ⊕-⊝ y2 y1 = refl
sumCA = record
{ Ch = SumChange
; isChAlg = record
{ _⊕_ = s⊕
; _⊝_ = s⊝
; valid = SValid
; ⊝-valid = s⊝-valid
; ⊕-⊝ = s⊕-⊝
} }
open import Data.Integer
open import Theorem.Groups-Nehemiah
instance
intCA : ChAlg ℤ
intCA = record
{ Ch = ℤ
; isChAlg = record
{ _⊕_ = _+_
; _⊝_ = _-_
; valid = λ a b → ⊤
; ⊝-valid = λ a b → tt
; ⊕-⊝ = λ b a → n+[m-n]=m {a} {b} } }
|
Add change structure for changes
|
Add change structure for changes
|
Agda
|
mit
|
inc-lc/ilc-agda
|
f1c2df98ed89a3de0f4b42407cb950eded1c5ed4
|
total.agda
|
total.agda
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Note that Δ is *not* the same as the ∂ operator in
-- definition/intro.tex. See discussion at:
--
-- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Syntactic.Changes
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Denotational.ValidChanges
open import Changes
open import ChangeContexts
open import ChangeContextLifting
open import PropsDelta
open import SymbolicDerivation
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) →
Δ {{Γ′}} t ≈ derive-term {{Γ′}} t
derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) =
begin
Δ {{Γ′}} (abs t)
≈⟨ Δ-abs {{Γ′}} t ⟩
abs (abs (Δ {τ • Γ₁} {{Γ″}} t))
≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} {{Γ″}} t)) ⟩
abs (abs (derive-term {τ • Γ₁} {{Γ″}} t))
≡⟨⟩
derive-term {{Γ′}} (abs t)
∎ where
open ≈-Reasoning
Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) =
begin
Δ {{Γ′}} (app t₁ t₂)
≈⟨ Δ-app {{Γ′}} t₁ t₂ ⟩
app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩
app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
≡⟨⟩
derive-term {{Γ′}} (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ →
begin
⟦ Δ {{Γ′}} (var x) ⟧ ρ
≡⟨⟩
diff
(⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ)))
(⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ)))
≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩
⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ)
≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩
⟦ lift Γ′ (derive-var x) ⟧Var ρ
∎) where open ≡-Reasoning
derive-term-correct true = ext-t (λ ρ → ≡-refl)
derive-term-correct false = ext-t (λ ρ → ≡-refl)
derive-term-correct {{Γ′}} (if t₁ t₂ t₃) =
begin
Δ (if t₁ t₂ t₃)
≈⟨ Δ-if {{Γ′}} t₁ t₂ t₃ ⟩
if (Δ t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (Δ {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (Δ {{Γ′}} t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(Δ {{Γ′}} t₂)
(Δ {{Γ′}} t₃))
≈⟨ ≈-if (derive-term-correct {{Γ′}} t₁)
(≈-if (≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₃) ≈-refl) ≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₂) ≈-refl) ≈-refl))
(≈-if (≈-refl)
(derive-term-correct {{Γ′}} t₂)
(derive-term-correct {{Γ′}} t₃)) ⟩
if (derive-term {{Γ′}} t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (derive-term {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (derive-term {{Γ′}} t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(derive-term {{Γ′}} t₂)
(derive-term {{Γ′}} t₃))
≡⟨⟩
derive-term {{Γ′}} (if t₁ t₂ t₃)
∎ where open ≈-Reasoning
derive-term-correct {{Γ′}} (Δ {{Γ″}} t) = ≈-Δ {{Γ′}} (derive-term-correct {{Γ″}} t)
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Note that Δ is *not* the same as the ∂ operator in
-- definition/intro.tex. See discussion at:
--
-- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Syntactic.Changes
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Denotational.ValidChanges
open import Changes
open import ChangeContexts
open import ChangeContextLifting
open import PropsDelta
open import SymbolicDerivation
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) →
Δ {{Γ′}} t ≈ derive-term {{Γ′}} t
derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) =
begin
Δ {{Γ′}} (abs t)
≈⟨ Δ-abs {{Γ′}} t ⟩
abs (abs (Δ {τ • Γ₁} {{Γ″}} t))
≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} {{Γ″}} t)) ⟩
abs (abs (derive-term {τ • Γ₁} {{Γ″}} t))
≡⟨⟩
derive-term {{Γ′}} (abs t)
∎ where
open ≈-Reasoning
Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) =
begin
Δ {{Γ′}} (app t₁ t₂)
≈⟨ Δ-app {{Γ′}} t₁ t₂ ⟩
app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩
app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
≡⟨⟩
derive-term {{Γ′}} (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ →
begin
⟦ Δ {{Γ′}} (var x) ⟧ ρ
≡⟨⟩
diff
(⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ)))
(⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ)))
≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩
⟦ derive-var x ⟧ (⟦ Γ′ ⟧ ρ)
≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩
⟦ lift Γ′ (derive-var x) ⟧ ρ
∎) where open ≡-Reasoning
derive-term-correct true = ext-t (λ ρ → ≡-refl)
derive-term-correct false = ext-t (λ ρ → ≡-refl)
derive-term-correct {{Γ′}} (if t₁ t₂ t₃) =
begin
Δ (if t₁ t₂ t₃)
≈⟨ Δ-if {{Γ′}} t₁ t₂ t₃ ⟩
if (Δ t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (Δ {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (Δ {{Γ′}} t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(Δ {{Γ′}} t₂)
(Δ {{Γ′}} t₃))
≈⟨ ≈-if (derive-term-correct {{Γ′}} t₁)
(≈-if (≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₃) ≈-refl) ≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₂) ≈-refl) ≈-refl))
(≈-if (≈-refl)
(derive-term-correct {{Γ′}} t₂)
(derive-term-correct {{Γ′}} t₃)) ⟩
if (derive-term {{Γ′}} t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (derive-term {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (derive-term {{Γ′}} t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(derive-term {{Γ′}} t₂)
(derive-term {{Γ′}} t₃))
≡⟨⟩
derive-term {{Γ′}} (if t₁ t₂ t₃)
∎ where open ≈-Reasoning
derive-term-correct {{Γ′}} (Δ {{Γ″}} t) = ≈-Δ {{Γ′}} (derive-term-correct {{Γ″}} t)
|
Increase overloading and confusion.
|
Increase overloading and confusion.
Old-commit-hash: df682306832e5b833f837c92dec32b3c428a7ab9
|
Agda
|
mit
|
inc-lc/ilc-agda
|
757995f811b789374410d560566e8717a68f1e24
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR ≙-setoid public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
trans
(proof (let open ≙-Reasoning in
begin
dx
≙⟨ dx≙nil-x ⟩
nil x
∎))
(let open ≡-Reasoning in
begin
x ⊞ (nil x)
≡⟨ update-nil x ⟩
x
∎)
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- We know that Derivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> Derivative f (apply df).
-- Now, we try to prove that if Derivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
Derivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil x))
≡⟨ ext (λ x → cong f (update-nil x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
Derivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- Unused, but just to test that inference works.
lemma : nil f ≙ dg
lemma = ≙-sym (derivative-is-nil dg fdg)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR ≙-setoid public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil x)
≡⟨ update-nil x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- We know that Derivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> Derivative f (apply df).
-- Now, we try to prove that if Derivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
Derivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil x))
≡⟨ ext (λ x → cong f (update-nil x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
Derivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- Unused, but just to test that inference works.
lemma : nil f ≙ dg
lemma = ≙-sym (derivative-is-nil dg fdg)
|
Revert "Show how badly reasoning for ≙ and ≡ combine"
|
Revert "Show how badly reasoning for ≙ and ≡ combine"
This reverts commit 24faa2c5edd91d1db64977d5124ef40e01de433f. The commit
showed how badly something worked, so let's leave in the old version for
now.
Old-commit-hash: 9f5f49cf0319ba809356655a204ec83725dfceb4
|
Agda
|
mit
|
inc-lc/ilc-agda
|
c1d53d1c793d91e002e6c8b4ffbbcc8dca4c6eda
|
README.agda
|
README.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Machine-checked formalization of the theoretical results presented
-- in the paper:
--
-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.
-- A Theory of Changes for Higher-Order Languages:
-- Incrementalizing λ-Calculi by Static Differentiation.
-- To appear at PLDI, ACM 2014.
--
-- We claim that this formalization
--
-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,
-- (2) formally specifies the interface between our incrementalization
-- framework and potential plugins, and
-- (3) shows that the plugin interface can be instantiated.
--
-- The first claim is the main reason for a machine-checked
-- proof: We want to be sure that we got the proofs right.
--
-- The second claim is about reusability and applicability: Only
-- a clearly defined interface allows other researchers to
-- provide plugins for our framework.
--
-- The third claim is to show that the plugin interface is
-- consistent: An inconsistent plugin interface would allow to
-- prove arbitrary results in the framework.
------------------------------------------------------------------------
module README where
-- We know two good ways to read this code base. You can either
-- use Emacs with agda2-mode to interact with the source files,
-- or you can use a web browser to view the pretty-printed and
-- hyperlinked source files. For Agda power users, we also
-- include basic setup information for your own machine.
-- IF YOU WANT TO USE A BROWSER
-- ============================
--
-- Start with *HTML* version of this readme. On the AEC
-- submission website (online or inside the VM), follow the link
-- "view the Agda code in their browser" to the file
-- agda/README.html.
--
-- The source code is syntax highlighted and hyperlinked. You can
-- click on module names to open the corresponding files, or you
-- can click on identifiers to jump to their definition. In
-- general, a Agda file with name `Foo/Bar/Baz.agda` contains a
-- module `Foo.Bar.Baz` and is shown in an HTML file
-- `Foo.Bar.Baz.html`.
--
-- Note that we also include the HTML files generated for our
-- transitive dependencies from the Agda standard library. This
-- allows you to follow hyperlinks to the Agda standard
-- library. It is the default behavior of `agda --html` which we
-- used to generate the HTML.
--
-- To get started, continue below on "Where to start reading?".
-- IF YOU WANT TO USE EMACS
-- ========================
--
-- Open this file in Emacs with agda2-mode installed. See below
-- for which Agda version you need to install. On the VM image,
-- everything is setup for you and you can just open this file in
-- Emacs.
--
-- C-c C-l Load code. Type checks and syntax highlights. Use
-- this if the code is not syntax highlighted, or
-- after you changed something that you want to type
-- check.
--
-- M-. Jump to definition of identifier at the point.
-- M-* Jump back.
--
-- Note that README.agda imports Everything.agda which imports
-- every Agda file in our formalization. So if README.agda type
-- checks successfully, everything does. If you want to type
-- check everything from scratch, delete the *.agdai files to
-- disable separate compilation. You can use "find . -name
-- '*.agdai' | xargs rm" to do that.
--
-- More information on the Agda mode is available on the Agda wiki:
--
-- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode
-- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations
--
-- To get started, continue below on "Where to start reading?".
-- IF YOU WANT TO USE YOUR OWN SETUP
-- =================================
--
-- To typecheck this formalization, you need to install the appropriate version
-- of Agda (2.4.0), the Agda standard library (version 0.8), generate
-- Everything.agda with the attached Haskell helper, and finally run Agda on
-- this file.
--
-- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh
-- script and following instructions given on output will eventually generate
-- Everything.agda and proceed to type check everything on command line.
--
-- If you're not an Agda power user, it is probably easier to use
-- the VM image or look at the pretty-printed and hyperlinked
-- HTML files, see above.
-- WHERE TO START READING?
-- =======================
--
-- modules.pdf
-- The graph of dependencies between Agda modules.
-- Good if you want to get a broad overview.
--
-- README.agda
-- This file. A coarse-grained introduction to the Agda
-- formalization. Good if you want to begin at the beginning
-- and understand the structure of our code.
--
-- PLDI14-List-of-Theorems.agda
-- Pointers to the Agda formalizations of all theorems, lemmas
-- or definitions in the PLDI paper. Good if you want to read
-- the paper and the Agda code side by side.
--
-- Here is an import of this file, so you can jump to it
-- directly (use M-. in Emacs or click the module name in the
-- Browser):
import PLDI14-List-of-Theorems
-- Everything.agda
-- Imports every Agda module in our formalization. Good if you
-- want to make sure you don't miss anything.
--
-- Again, here's is an import of this file so you can navigate
-- there:
import Everything
-- (This import is also important to ensure that if we typecheck
-- README.agda, we also typecheck every other file of our
-- formalization).
-- THE AGDA CODE
-- =============
--
-- The formalization has four parts:
--
-- 1. A formalization of change structures. This lives in
-- Base.Change.Algebra. (What we call "change structure" in the
-- paper, we call "change algebra" in the Agda code. We changed
-- the name when writing the paper, and never got around to
-- updating the name in the Agda code).
--
-- 2. Incrementalization framework for first-class functions,
-- with extension points for plugging in data types and their
-- incrementalization. This lives in the Parametric.*
-- hierarchy.
--
-- 3. An example plugin that provides integers and bags
-- with negative multiplicity. This lives in the Nehemiah.*
-- hierarchy. (For some reason, we choose to call this
-- particular incarnation of the plugin Nehemiah).
--
-- 4. Other material that is unrelated to the framework/plugin
-- distinction. This is all other files.
-- FORMALIZATION OF CHANGE STRUCTURES
-- ==================================
--
-- Section 2 of the paper, and Base.Change.Algebra in Agda.
import Base.Change.Algebra
-- INCREMENTALIZATION FRAMEWORK
-- ============================
--
-- Section 3 of the paper, and Parametric.* hierarchy in Agda.
--
-- The extension points are implemented as module parameters. See
-- detailed explanation in Parametric.Syntax.Type and
-- Parametric.Syntax.Term. Some extension points are for types,
-- some for values, and some for proof fragments. In Agda, these
-- three kinds of entities are unified anyway, so we can encode
-- all of them as module parameters.
--
-- Every module in the Parametric.* hierarchy adds at least one
-- extension point, so the module hierarchy of a plugin will
-- typically mirror the Parametric.* hierarchy, defining the
-- values for these extension points.
--
-- Firstly, we have the syntax of types and terms, the erased
-- change structure for function types and incrementalization as
-- a term-to-term transformation. The contents of these modules
-- formalizes Sec. 3.1 and 3.2 of the paper, except for the
-- second and third line of Figure 3, which is formalized further
-- below.
import Parametric.Syntax.Type -- syntax of types
import Parametric.Syntax.Term -- syntax of terms
import Parametric.Change.Type -- simply-typed changes
import Parametric.Change.Derive -- incrementalization
-- Secondly, we define the usual denotational semantics of the
-- simply-typed lambda calculus in terms of total functions, and
-- the change semantics in terms of a change structure on total
-- functions. The contents of these modules formalize Sec. 3.3,
-- 3.4 and 3.5 of the paper.
import Parametric.Denotation.Value -- standard values
import Parametric.Denotation.Evaluation -- standard evaluation
import Parametric.Change.Validity -- dependently-typed changes
import Parametric.Change.Specification -- change evaluation
-- Thirdly, we define terms that operate on simply-typed changes,
-- and connect them to their values. The concents of these
-- modules formalize the second and third line of Figure 3, as
-- well as the semantics of these lines.
import Parametric.Change.Term -- terms that operate on simply-typed changes
import Parametric.Change.Value -- the values of these terms
import Parametric.Change.Evaluation -- connecting the terms and their values
-- Finally, we prove correctness by connecting the (syntactic)
-- incrementalization to the (semantic) change evaluation by a
-- logical relation, and a proof that the values of terms
-- produced by the incrementalization transformation are related
-- to the change values of the original terms. The contents of
-- these modules formalize Sec. 3.6.
import Parametric.Change.Implementation -- logical relation
import Parametric.Change.Correctness -- main correctness proof
-- EXAMPLE PLUGIN
-- ==============
--
-- Sec. 3.7 in the paper, and the Nehemiah.* hierarchy in Agda.
--
-- The module structure of the plugin follows the structure of
-- the Parametric.* hierarchy. For example, the extension point
-- defined in Parametric.Syntax.Term is instantiated in
-- Nehemiah.Syntax.Term.
--
-- As discussed in Sec. 3.7 of the paper, the point of this
-- plugin is not to speed up any real programs, but to show "that
-- the interface for proof plugins can be implemented". As a
-- first step towards proving correctness of the more complicated
-- plugin with integers, bags and finite maps we implement in
-- Scala, we choose to define plugin with integers and bags in
-- Agda. Instead of implementing bags (with negative
-- multiplicities, like in the paper) in Agda, though, we
-- postulate that a group of such bags exist. Note that integer
-- bags with integer multiplicities are actually the free group
-- given a singleton operation `Integer -> Bag`, so this should
-- be easy to formalize in principle.
-- Before we start with the plugin, we postulate an abstract data
-- type for integer bags.
import Postulate.Bag-Nehemiah
-- Firstly, we extend the syntax of types and terms, the erased
-- change structure for function types, and incrementalization as
-- a term-to-term transformation to account for the data types of
-- the Nehemiah language. The contents of these modules
-- instantiate the extension points in Sec. 3.1 and 3.2 of the
-- paper, except for the second and third line of Figure 3, which
-- is instantiated further below.
import Nehemiah.Syntax.Type
import Nehemiah.Syntax.Term
import Nehemiah.Change.Type
import Nehemiah.Change.Derive
-- Secondly, we extend the usual denotational semantics and the
-- change semantics to account for the data types of the Nehemiah
-- language. The contents of these modules instantiate the
-- extension points in Sec. 3.3, 3.4 and 3.5 of the paper.
import Nehemiah.Denotation.Value
import Nehemiah.Denotation.Evaluation
import Nehemiah.Change.Validity
import Nehemiah.Change.Specification
-- Thirdly, we extend the terms that operate on simply-typed
-- changes, and the connection to their values to account for the
-- data types of the Nehemiah language. The concents of these
-- modules instantiate the extension points in the second and
-- third line of Figure 3.
import Nehemiah.Change.Term
import Nehemiah.Change.Value
import Nehemiah.Change.Evaluation
-- Finally, we extend the logical relation and the main
-- correctness proof to account for the data types in the
-- Nehemiah language. The contents of these modules instantiate
-- the extension points defined in Sec. 3.6.
import Nehemiah.Change.Implementation
import Nehemiah.Change.Correctness
-- OTHER MATERIAL
-- ==============
--
-- We postulate extensionality of Agda functions. This postulate
-- is well known to be compatible with Agda's type theory.
import Postulate.Extensionality
-- For historical reasons, we reexport Data.List.All from the
-- standard library under the name DependentList.
import Base.Data.DependentList
-- This module supports overloading the ⟦_⟧ notation based on
-- Agda's instance arguments.
import Base.Denotation.Notation
-- These modules implement contexts including typed de Bruijn
-- indices to represent bound variables, sets of bound variables,
-- and environments. These modules are parametric in the set of
-- types (that are stored in contexts) and the set of values
-- (that are stored in environments). So these modules are even
-- more parametric than the Parametric.* hierarchy.
import Base.Syntax.Context
import Base.Syntax.Vars
import Base.Denotation.Environment
-- This module contains some helper definitions to merge a
-- context of values and a context of changes.
import Base.Change.Context
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Machine-checked formalization of the theoretical results presented
-- in the paper:
--
-- Yufei Cai, Paolo G. Giarrusso, Tillmann Rendel, Klaus Ostermann.
-- A Theory of Changes for Higher-Order Languages:
-- Incrementalizing λ-Calculi by Static Differentiation.
-- To appear at PLDI, ACM 2014.
--
-- We claim that this formalization
--
-- (1) proves every lemma and theorem in Sec. 2 and 3 of the paper,
-- (2) formally specifies the interface between our incrementalization
-- framework and potential plugins, and
-- (3) shows that the plugin interface can be instantiated.
--
-- The first claim is the main reason for a machine-checked
-- proof: We want to be sure that we got the proofs right.
--
-- The second claim is about reusability and applicability: Only
-- a clearly defined interface allows other researchers to
-- provide plugins for our framework.
--
-- The third claim is to show that the plugin interface is
-- consistent: An inconsistent plugin interface would allow to
-- prove arbitrary results in the framework.
------------------------------------------------------------------------
module README where
-- We know two good ways to read this code base. You can either
-- use Emacs with agda2-mode to interact with the source files,
-- or you can use a web browser to view the pretty-printed and
-- hyperlinked source files. For Agda power users, we also
-- include basic setup information for your own machine.
-- IF YOU WANT TO USE A BROWSER
-- ============================
--
-- Start with *HTML* version of this readme. On the AEC
-- submission website (online or inside the VM), follow the link
-- "view the Agda code in their browser" to the file
-- agda/README.html.
--
-- The source code is syntax highlighted and hyperlinked. You can
-- click on module names to open the corresponding files, or you
-- can click on identifiers to jump to their definition. In
-- general, a Agda file with name `Foo/Bar/Baz.agda` contains a
-- module `Foo.Bar.Baz` and is shown in an HTML file
-- `Foo.Bar.Baz.html`.
--
-- Note that we also include the HTML files generated for our
-- transitive dependencies from the Agda standard library. This
-- allows you to follow hyperlinks to the Agda standard
-- library. It is the default behavior of `agda --html` which we
-- used to generate the HTML.
--
-- To get started, continue below on "Where to start reading?".
-- IF YOU WANT TO USE EMACS
-- ========================
--
-- Open this file in Emacs with agda2-mode installed. See below
-- for which Agda version you need to install. On the VM image,
-- everything is setup for you and you can just open this file in
-- Emacs.
--
-- C-c C-l Load code. Type checks and syntax highlights. Use
-- this if the code is not syntax highlighted, or
-- after you changed something that you want to type
-- check.
--
-- M-. Jump to definition of identifier at the point.
-- M-* Jump back.
--
-- Note that README.agda imports Everything.agda which imports
-- every Agda file in our formalization. So if README.agda type
-- checks successfully, everything does. If you want to type
-- check everything from scratch, delete the *.agdai files to
-- disable separate compilation. You can use "find . -name
-- '*.agdai' | xargs rm" to do that.
--
-- More information on the Agda mode is available on the Agda wiki:
--
-- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.QuickGuideToEditingTypeCheckingAndCompilingAgdaCode
-- http://wiki.portal.chalmers.se/agda/pmwiki.php?n=Docs.EmacsModeKeyCombinations
--
-- To get started, continue below on "Where to start reading?".
-- IF YOU WANT TO USE YOUR OWN SETUP
-- =================================
--
-- To typecheck this formalization, you need to install the appropriate version
-- of Agda (2.4.0.*), the Agda standard library (version 0.8), generate
-- Everything.agda with the attached Haskell helper, and finally run Agda on
-- this file.
--
-- Given a Unix-like environment (including Cygwin), running the ./agdaCheck.sh
-- script and following instructions given on output will eventually generate
-- Everything.agda and proceed to type check everything on command line.
--
-- If you're not an Agda power user, it is probably easier to use
-- the VM image or look at the pretty-printed and hyperlinked
-- HTML files, see above.
-- WHERE TO START READING?
-- =======================
--
-- modules.pdf
-- The graph of dependencies between Agda modules.
-- Good if you want to get a broad overview.
--
-- README.agda
-- This file. A coarse-grained introduction to the Agda
-- formalization. Good if you want to begin at the beginning
-- and understand the structure of our code.
--
-- PLDI14-List-of-Theorems.agda
-- Pointers to the Agda formalizations of all theorems, lemmas
-- or definitions in the PLDI paper. Good if you want to read
-- the paper and the Agda code side by side.
--
-- Here is an import of this file, so you can jump to it
-- directly (use M-. in Emacs or click the module name in the
-- Browser):
import PLDI14-List-of-Theorems
-- Everything.agda
-- Imports every Agda module in our formalization. Good if you
-- want to make sure you don't miss anything.
--
-- Again, here's is an import of this file so you can navigate
-- there:
import Everything
-- (This import is also important to ensure that if we typecheck
-- README.agda, we also typecheck every other file of our
-- formalization).
-- THE AGDA CODE
-- =============
--
-- The formalization has four parts:
--
-- 1. A formalization of change structures. This lives in
-- Base.Change.Algebra. (What we call "change structure" in the
-- paper, we call "change algebra" in the Agda code. We changed
-- the name when writing the paper, and never got around to
-- updating the name in the Agda code).
--
-- 2. Incrementalization framework for first-class functions,
-- with extension points for plugging in data types and their
-- incrementalization. This lives in the Parametric.*
-- hierarchy.
--
-- 3. An example plugin that provides integers and bags
-- with negative multiplicity. This lives in the Nehemiah.*
-- hierarchy. (For some reason, we choose to call this
-- particular incarnation of the plugin Nehemiah).
--
-- 4. Other material that is unrelated to the framework/plugin
-- distinction. This is all other files.
-- FORMALIZATION OF CHANGE STRUCTURES
-- ==================================
--
-- Section 2 of the paper, and Base.Change.Algebra in Agda.
import Base.Change.Algebra
-- INCREMENTALIZATION FRAMEWORK
-- ============================
--
-- Section 3 of the paper, and Parametric.* hierarchy in Agda.
--
-- The extension points are implemented as module parameters. See
-- detailed explanation in Parametric.Syntax.Type and
-- Parametric.Syntax.Term. Some extension points are for types,
-- some for values, and some for proof fragments. In Agda, these
-- three kinds of entities are unified anyway, so we can encode
-- all of them as module parameters.
--
-- Every module in the Parametric.* hierarchy adds at least one
-- extension point, so the module hierarchy of a plugin will
-- typically mirror the Parametric.* hierarchy, defining the
-- values for these extension points.
--
-- Firstly, we have the syntax of types and terms, the erased
-- change structure for function types and incrementalization as
-- a term-to-term transformation. The contents of these modules
-- formalizes Sec. 3.1 and 3.2 of the paper, except for the
-- second and third line of Figure 3, which is formalized further
-- below.
import Parametric.Syntax.Type -- syntax of types
import Parametric.Syntax.Term -- syntax of terms
import Parametric.Change.Type -- simply-typed changes
import Parametric.Change.Derive -- incrementalization
-- Secondly, we define the usual denotational semantics of the
-- simply-typed lambda calculus in terms of total functions, and
-- the change semantics in terms of a change structure on total
-- functions. The contents of these modules formalize Sec. 3.3,
-- 3.4 and 3.5 of the paper.
import Parametric.Denotation.Value -- standard values
import Parametric.Denotation.Evaluation -- standard evaluation
import Parametric.Change.Validity -- dependently-typed changes
import Parametric.Change.Specification -- change evaluation
-- Thirdly, we define terms that operate on simply-typed changes,
-- and connect them to their values. The concents of these
-- modules formalize the second and third line of Figure 3, as
-- well as the semantics of these lines.
import Parametric.Change.Term -- terms that operate on simply-typed changes
import Parametric.Change.Value -- the values of these terms
import Parametric.Change.Evaluation -- connecting the terms and their values
-- Finally, we prove correctness by connecting the (syntactic)
-- incrementalization to the (semantic) change evaluation by a
-- logical relation, and a proof that the values of terms
-- produced by the incrementalization transformation are related
-- to the change values of the original terms. The contents of
-- these modules formalize Sec. 3.6.
import Parametric.Change.Implementation -- logical relation
import Parametric.Change.Correctness -- main correctness proof
-- EXAMPLE PLUGIN
-- ==============
--
-- Sec. 3.7 in the paper, and the Nehemiah.* hierarchy in Agda.
--
-- The module structure of the plugin follows the structure of
-- the Parametric.* hierarchy. For example, the extension point
-- defined in Parametric.Syntax.Term is instantiated in
-- Nehemiah.Syntax.Term.
--
-- As discussed in Sec. 3.7 of the paper, the point of this
-- plugin is not to speed up any real programs, but to show "that
-- the interface for proof plugins can be implemented". As a
-- first step towards proving correctness of the more complicated
-- plugin with integers, bags and finite maps we implement in
-- Scala, we choose to define plugin with integers and bags in
-- Agda. Instead of implementing bags (with negative
-- multiplicities, like in the paper) in Agda, though, we
-- postulate that a group of such bags exist. Note that integer
-- bags with integer multiplicities are actually the free group
-- given a singleton operation `Integer -> Bag`, so this should
-- be easy to formalize in principle.
-- Before we start with the plugin, we postulate an abstract data
-- type for integer bags.
import Postulate.Bag-Nehemiah
-- Firstly, we extend the syntax of types and terms, the erased
-- change structure for function types, and incrementalization as
-- a term-to-term transformation to account for the data types of
-- the Nehemiah language. The contents of these modules
-- instantiate the extension points in Sec. 3.1 and 3.2 of the
-- paper, except for the second and third line of Figure 3, which
-- is instantiated further below.
import Nehemiah.Syntax.Type
import Nehemiah.Syntax.Term
import Nehemiah.Change.Type
import Nehemiah.Change.Derive
-- Secondly, we extend the usual denotational semantics and the
-- change semantics to account for the data types of the Nehemiah
-- language. The contents of these modules instantiate the
-- extension points in Sec. 3.3, 3.4 and 3.5 of the paper.
import Nehemiah.Denotation.Value
import Nehemiah.Denotation.Evaluation
import Nehemiah.Change.Validity
import Nehemiah.Change.Specification
-- Thirdly, we extend the terms that operate on simply-typed
-- changes, and the connection to their values to account for the
-- data types of the Nehemiah language. The concents of these
-- modules instantiate the extension points in the second and
-- third line of Figure 3.
import Nehemiah.Change.Term
import Nehemiah.Change.Value
import Nehemiah.Change.Evaluation
-- Finally, we extend the logical relation and the main
-- correctness proof to account for the data types in the
-- Nehemiah language. The contents of these modules instantiate
-- the extension points defined in Sec. 3.6.
import Nehemiah.Change.Implementation
import Nehemiah.Change.Correctness
-- OTHER MATERIAL
-- ==============
--
-- We postulate extensionality of Agda functions. This postulate
-- is well known to be compatible with Agda's type theory.
import Postulate.Extensionality
-- For historical reasons, we reexport Data.List.All from the
-- standard library under the name DependentList.
import Base.Data.DependentList
-- This module supports overloading the ⟦_⟧ notation based on
-- Agda's instance arguments.
import Base.Denotation.Notation
-- These modules implement contexts including typed de Bruijn
-- indices to represent bound variables, sets of bound variables,
-- and environments. These modules are parametric in the set of
-- types (that are stored in contexts) and the set of values
-- (that are stored in environments). So these modules are even
-- more parametric than the Parametric.* hierarchy.
import Base.Syntax.Context
import Base.Syntax.Vars
import Base.Denotation.Environment
-- This module contains some helper definitions to merge a
-- context of values and a context of changes.
import Base.Change.Context
|
Update recommended version of Agda
|
README.agda: Update recommended version of Agda
Rationale: 2.4.0 doesn't install any more.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
464a985aa70ecceb7a7917d62fc7cb3e05dd59c2
|
models/Desc.agda
|
models/Desc.agda
|
{-# OPTIONS --type-in-type
--no-termination-check
--no-positivity-check #-}
module Desc where
--********************************************
-- Prelude
--********************************************
-- Some preliminary stuffs, to avoid relying on the stdlib
--****************
-- Sigma and friends
--****************
data Sigma (A : Set) (B : A -> Set) : Set where
_,_ : (x : A) (y : B x) -> Sigma A B
_*_ : (A B : Set) -> Set
A * B = Sigma A \_ -> B
fst : {A : Set}{B : A -> Set} -> Sigma A B -> A
fst (a , _) = a
snd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)
snd (a , b) = b
data Zero : Set where
record One : Set where
--****************
-- Sum and friends
--****************
data _+_ (A B : Set) : Set where
l : A -> A + B
r : B -> A + B
--********************************************
-- Desc code
--********************************************
-- Inductive types are implemented as a Universe. Hence, in this
-- section, we implement their code.
-- We can read this code as follow (see Conor's "Ornamental
-- algebras"): a description in |Desc| is a program to read one node
-- of the described tree.
data Desc : Set where
Arg : (X : Set) -> (X -> Desc) -> Desc
-- Read a field in |X|; continue, given its value
-- Often, |X| is an |EnumT|, hence allowing to choose a constructor
-- among a finite set of constructors
Ind : (H : Set) -> Desc -> Desc
-- Read a field in H; read a recursive subnode given the field,
-- continue regarless of the subnode
-- Often, |H| is |1|, hence |Ind| simplifies to |Inf : Desc -> Desc|,
-- meaning: read a recursive subnode and continue regardless
Done : Desc
-- Stop reading
--********************************************
-- Desc decoder
--********************************************
-- Provided the type of the recursive subnodes |R|, we decode a
-- description as a record describing the node.
[|_|]_ : Desc -> Set -> Set
[| Arg A D |] R = Sigma A (\ a -> [| D a |] R)
[| Ind H D |] R = (H -> R) * [| D |] R
[| Done |] R = One
--********************************************
-- Functions on codes
--********************************************
-- Saying that a "predicate" |p| holds everywhere in |v| amounts to
-- write something of the following type:
Everywhere : (d : Desc) (D : Set) (bp : D -> Set) (V : [| d |] D) -> Set
Everywhere (Arg A f) d p v = Everywhere (f (fst v)) d p (snd v)
-- It must hold for this constructor
Everywhere (Ind H x) d p v = ((y : H) -> p (fst v y)) * Everywhere x d p (snd v)
-- It must hold for the subtrees
Everywhere Done _ _ _ = _
-- It trivially holds at endpoints
-- Then, we can build terms that inhabits this type. That is, a
-- function that takes a "predicate" |bp| and makes it hold everywhere
-- in the data-structure. It is the equivalent of a "map", but in a
-- dependently-typed setting.
everywhere : (d : Desc) (D : Set) (bp : D -> Set) ->
((y : D) -> bp y) -> (v : [| d |] D) ->
Everywhere d D bp v
everywhere (Arg a f) d bp p v = everywhere (f (fst v)) d bp p (snd v)
-- It holds everywhere on this constructor
everywhere (Ind H x) d bp p v = (\y -> p (fst v y)) , everywhere x d bp p (snd v)
-- It holds here, and down in the recursive subtrees
everywhere Done _ _ _ _ = One
-- Nothing needs to be done on endpoints
-- Looking at the decoder, a natural thing to do is to define its
-- fixpoint |Mu D|, hence instantiating |R| with |Mu D| itself.
data Mu (D : Desc) : Set where
Con : [| D |] (Mu D) -> Mu D
-- Using the "map" defined by |everywhere|, we can implement a "fold"
-- over the |Mu| fixpoint:
foldDesc : (D : Desc) (bp : Mu D -> Set) ->
((x : [| D |] (Mu D)) -> Everywhere D (Mu D) bp x -> bp (Con x)) ->
(v : Mu D) ->
bp v
foldDesc D bp p (Con v) = p v (everywhere D (Mu D) bp (\x -> foldDesc D bp p x) v)
--********************************************
-- Nat
--********************************************
data NatConst : Set where
ZE : NatConst
SU : NatConst
natc : NatConst -> Desc
natc ZE = Done
natc SU = Ind One Done
natd : Desc
natd = Arg NatConst natc
nat : Set
nat = Mu natd
zero : nat
zero = Con ( ZE , _ )
suc : nat -> nat
suc n = Con ( SU , ( (\_ -> n) , _ ) )
two : nat
two = suc (suc zero)
four : nat
four = suc (suc (suc (suc zero)))
sum : nat ->
((x : Sigma NatConst (\ a -> [| natc a |] Mu (Arg NatConst natc))) ->
Everywhere (natc (fst x)) (Mu (Arg NatConst natc)) (\ _ -> Mu (Arg NatConst natc)) (snd x) ->
Mu (Arg NatConst natc))
sum n2 (ZE , _) p = n2
sum n2 (SU , f) p = suc ( fst p _)
plus : nat -> nat -> nat
plus n1 n2 = foldDesc natd (\_ -> nat) (sum n2) n1
x : nat
x = plus two two
|
{-# OPTIONS --type-in-type #-}
module Desc where
--********************************************
-- Prelude
--********************************************
-- Some preliminary stuffs, to avoid relying on the stdlib
--****************
-- Sigma and friends
--****************
data Sigma (A : Set) (B : A -> Set) : Set where
_,_ : (x : A) (y : B x) -> Sigma A B
_*_ : (A : Set)(B : Set) -> Set
A * B = Sigma A \_ -> B
fst : {A : Set}{B : A -> Set} -> Sigma A B -> A
fst (a , _) = a
snd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)
snd (a , b) = b
data Zero : Set where
data Unit : Set where
Void : Unit
--****************
-- Sum and friends
--****************
data _+_ (A : Set)(B : Set) : Set where
l : A -> A + B
r : B -> A + B
--****************
-- Equality
--****************
data _==_ {A : Set}(x : A) : A -> Set where
refl : x == x
cong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y
cong f refl = refl
cong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} ->
x == y -> z == t -> f x z == f y t
cong2 f refl refl = refl
postulate
reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g
--********************************************
-- Desc code
--********************************************
data Desc : Set where
id : Desc
const : Set -> Desc
prod : Desc -> Desc -> Desc
sigma : (S : Set) -> (S -> Desc) -> Desc
pi : (S : Set) -> (S -> Desc) -> Desc
--********************************************
-- Desc interpretation
--********************************************
[|_|]_ : Desc -> Set -> Set
[| id |] Z = Z
[| const X |] Z = X
[| prod D D' |] Z = [| D |] Z * [| D' |] Z
[| sigma S T |] Z = Sigma S (\s -> [| T s |] Z)
[| pi S T |] Z = (s : S) -> [| T s |] Z
--********************************************
-- Fixpoint construction
--********************************************
data Mu (D : Desc) : Set where
con : [| D |] (Mu D) -> Mu D
--********************************************
-- Predicate: All
--********************************************
All : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set
All id X P x = P x
All (const Z) X P x = Z
All (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')
All (sigma S T) X P (a , b) = All (T a) X P b
All (pi S T) X P f = (s : S) -> All (T s) X P (f s)
all : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x
all id X P R x = R x
all (const Z) X P R z = z
all (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'
all (sigma S T) X P R (a , b) = all (T a) X P R b
all (pi S T) X P R f = \ s -> all (T s) X P R (f s)
--********************************************
-- Elimination principle: induction
--********************************************
{-
induction : (D : Desc)
(P : Mu D -> Set) ->
( (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x)) ->
(v : Mu D) ->
P v
induction D P ms (con xs) = ms xs (all D (Mu D) P (\x -> induction D P ms x) xs)
-}
module Elim (D : Desc)
(P : Mu D -> Set)
(ms : (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x))
where
mutual
induction : (x : Mu D) -> P x
induction (con xs) = ms xs (hyps D xs)
hyps : (D' : Desc)
(xs : [| D' |] (Mu D)) ->
All D' (Mu D) P xs
hyps id x = induction x
hyps (const Z) z = z
hyps (prod D D') (d , d') = hyps D d , hyps D' d'
hyps (sigma S T) (a , b) = hyps (T a) b
hyps (pi S T) f = \s -> hyps (T s) (f s)
induction : (D : Desc)
(P : Mu D -> Set) ->
( (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x)) ->
(v : Mu D) ->
P v
induction D P ms x = Elim.induction D P ms x
--********************************************
-- Examples
--********************************************
--****************
-- Nat
--****************
data NatConst : Set where
Ze : NatConst
Suc : NatConst
natCases : NatConst -> Desc
natCases Ze = const Unit
natCases Suc = id
NatD : Desc
NatD = sigma NatConst natCases
Nat : Set
Nat = Mu NatD
ze : Nat
ze = con (Ze , Void)
suc : Nat -> Nat
suc n = con (Suc , n)
--****************
-- List
--****************
data ListConst : Set where
Nil : ListConst
Cons : ListConst
listCases : Set -> ListConst -> Desc
listCases X Nil = const Unit
listCases X Cons = sigma X (\_ -> id)
ListD : Set -> Desc
ListD X = sigma ListConst (listCases X)
List : Set -> Set
List X = Mu (ListD X)
nil : {X : Set} -> List X
nil = con ( Nil , Void )
cons : {X : Set} -> X -> List X -> List X
cons x t = con ( Cons , ( x , t ))
--****************
-- Tree
--****************
data TreeConst : Set where
Leaf : TreeConst
Node : TreeConst
treeCases : Set -> TreeConst -> Desc
treeCases X Leaf = const Unit
treeCases X Node = sigma X (\_ -> prod id id)
TreeD : Set -> Desc
TreeD X = sigma TreeConst (treeCases X)
Tree : Set -> Set
Tree X = Mu (TreeD X)
leaf : {X : Set} -> Tree X
leaf = con (Leaf , Void)
node : {X : Set} -> X -> Tree X -> Tree X -> Tree X
node x le ri = con (Node , (x , (le , ri)))
--********************************************
-- Finite sets
--********************************************
EnumU : Set
EnumU = Nat
nilE : EnumU
nilE = ze
consE : EnumU -> EnumU
consE e = suc e
{-
data EnumU : Set where
nilE : EnumU
consE : EnumU -> EnumU
-}
data EnumT : (e : EnumU) -> Set where
EZe : {e : EnumU} -> EnumT (consE e)
ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)
casesSpi : (xs : [| NatD |] Nat) ->
All NatD Nat (\e -> (P' : EnumT e -> Set) -> Set) xs ->
(P' : EnumT (con xs) -> Set) -> Set
casesSpi (Ze , Void) hs P' = Unit
casesSpi (Suc , n) hs P' = P' EZe * hs (\e -> P' (ESu e))
spi : (e : EnumU)(P : EnumT e -> Set) -> Set
spi e P = induction NatD (\e -> (P : EnumT e -> Set) -> Set) casesSpi e P
{-
spi : (e : EnumU)(P : EnumT e -> Set) -> Set
spi nilE P = Unit
spi (consE e) P = P EZe * spi e (\e -> P (ESu e))
-}
casesSwitch : (xs : [| NatD |] Nat) ->
All NatD Nat (\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->
(P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'
casesSwitch (Ze , Void) hs P' b' ()
casesSwitch (Suc , n) hs P' b' EZe = fst b'
casesSwitch (Suc , n) hs P' b' (ESu e') = hs (\e -> P' (ESu e)) (snd b') e'
switch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x
switch e P b x = induction NatD (\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x
{-
switch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x
switch nilE P b ()
switch (consE e) P b EZe = fst b
switch (consE e) P b (ESu n) = switch e (\e -> P (ESu e)) (snd b) n
-}
--********************************************
-- Tagged description
--********************************************
TagDesc : Set
TagDesc = Sigma EnumU (\e -> spi e (\_ -> Desc))
toDesc : TagDesc -> Desc
toDesc (B , F) = sigma (EnumT B) (\e -> switch B (\_ -> Desc) F e)
--********************************************
-- Catamorphism
--********************************************
cata : (D : Desc)
(T : Set) ->
([| D |] T -> T) ->
(Mu D) -> T
cata D T phi x = induction D (\_ -> T) (\x ms -> phi (replace D T x ms )) x
where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\_ -> T) xs) -> [| D' |] T
replace id T x y = y
replace (const Z) T z z' = z'
replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'
replace (sigma A B) T (a , b) t = a , replace (B a) T b t
replace (pi A B) T f t = \s -> replace (B s) T (f s) (t s)
--********************************************
-- Free monad construction
--********************************************
_**_ : TagDesc -> (X : Set) -> TagDesc
(e , D) ** X = consE e , (const X , D)
--********************************************
-- Substitution
--********************************************
apply : (D : TagDesc)(X Y : Set) ->
(X -> Mu (toDesc (D ** Y))) ->
[| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->
Mu (toDesc (D ** Y))
apply (E , B) X Y sig (EZe , x) = sig x
apply (E , B) X Y sig (ESu n , t) = con (ESu n , t)
subst : (D : TagDesc)(X Y : Set) ->
Mu (toDesc (D ** X)) ->
(X -> Mu (toDesc (D ** Y))) ->
Mu (toDesc (D ** Y))
subst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x
|
Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.
|
Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.
|
Agda
|
mit
|
kwangkim/pigment,kwangkim/pigment,brixen/Epigram,kwangkim/pigment
|
7f1a4e72fc0081b46fe06904e4627bb80b3240ac
|
proposal/examples/TacticsMatchingFunctionCalls.agda
|
proposal/examples/TacticsMatchingFunctionCalls.agda
|
{-
This file demonstrates a technique that simulates the ability to
pattern match against function calls rather than just variables
or constructors.
This technique is necessary to write certain kinds of tactics
as generic functions.
-}
module TacticsMatchingFunctionCalls where
open import Data.Bool hiding ( _≟_ )
open import Data.Nat
open import Data.Fin hiding ( _+_ )
open import Data.Product hiding ( map )
open import Data.List
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
open import Function
----------------------------------------------------------------------
{-
Zero is the right identity of addition on the natural numbers.
-}
plusrident : (n : ℕ) → n ≡ n + 0
plusrident zero = refl
plusrident (suc n) = cong suc (plusrident n)
----------------------------------------------------------------------
{-
Arith is a universe of codes representing the natural numbers
along with a limited set of functions over them. We would like to write
tactics that pattern match against types indexed by applications of
these functions.
-}
data Arith : Set where
`zero : Arith
`suc : (a : Arith) → Arith
_`+_ _`*_ : (a₁ a₂ : Arith) → Arith
eval : Arith → ℕ
eval `zero = zero
eval (`suc a) = suc (eval a)
eval (a₁ `+ a₂) = eval a₁ + eval a₂
eval (a₁ `* a₂) = eval a₁ * eval a₂
----------------------------------------------------------------------
{-
Φ (standing for *function*-indexed types)
represents an indexed type, but we *can* pattern
match on function calls in its index position.
A - The type of codes, including constructors and functions.
i.e. Arith
A′ - The model underlying the codes.
i.e. ℕ
el - The evaluation function from code values to model values.
i.e. eval
B - The genuine indexed datatype that Φ represents.
i.e. Fin
a - The index value as a code, which may be a function call and hence supports
pattern matching.
i.e. (a `+ `zero)
-}
data Φ (A A′ : Set) (el : A → A′) (B : A′ → Set) (a : A) : Set where
φ : (b : B (el a)) → Φ A A′ el B a
----------------------------------------------------------------------
{-
Alternatively, we can specialize Φ to our Arith universe. This may be
a good use of our ability to cheaply define new datatypes via Desc.
-}
data ΦArith (B : ℕ → Set) (a : Arith) : Set where
φ : (b : B (eval a)) → ΦArith B a
----------------------------------------------------------------------
{-
Going further, we could even specialize B in ΦArith to Fin.
-}
data Fin₂ (a : Arith) : Set where
fin : (i : Fin (eval a)) → Fin₂ a
----------------------------------------------------------------------
{-
A small universe of dependent types that is sufficient for the
examples given below.
-}
data Type : Set
⟦_⟧ : Type → Set
data Type where
`Bool `ℕ `Arith : Type
`Fin : (n : ℕ) → Type
`Φ : (A A′ : Type)
(el : ⟦ A ⟧ → ⟦ A′ ⟧)
(B : ⟦ A′ ⟧ → Type)
(a : ⟦ A ⟧)
→ Type
`ΦArith : (B : ℕ → Type) (a : Arith) → Type
`Fin₂ : (a : Arith) → Type
`Π `Σ : (A : Type) (B : ⟦ A ⟧ → Type) → Type
`Id : (A : Type) (x y : ⟦ A ⟧) → Type
⟦ `Bool ⟧ = Bool
⟦ `ℕ ⟧ = ℕ
⟦ `Arith ⟧ = Arith
⟦ `Fin n ⟧ = Fin n
⟦ `Φ A A′ el B a ⟧ = Φ ⟦ A ⟧ ⟦ A′ ⟧ el (λ a → ⟦ B a ⟧) a
⟦ `ΦArith B a ⟧ = ΦArith (λ x → ⟦ B x ⟧) a
⟦ `Fin₂ a ⟧ = Fin₂ a
⟦ `Π A B ⟧ = (a : ⟦ A ⟧) → ⟦ B a ⟧
⟦ `Σ A B ⟧ = Σ ⟦ A ⟧ (λ a → ⟦ B a ⟧)
⟦ `Id A x y ⟧ = x ≡ y
_`→_ : (A B : Type) → Type
A `→ B = `Π A (const B)
_`×_ : (A B : Type) → Type
A `× B = `Σ A (const B)
----------------------------------------------------------------------
{-
Tactics are represented as generic functions taking a Dynamic value (of
any type), and returning a Dynamic value (at a possibly different type).
The convention is to behave like the identity function if the tactic
doesn't match the current value or fails.
In practice, the context will be a List of Dynamic values, and a tactic
will map a Context to a Context. For simplicity, the tactics I give below
only map a Dynamic to a Dynamic.
-}
Dynamic : Set
Dynamic = Σ Type ⟦_⟧
Tactic : Set
Tactic = Dynamic → Dynamic
----------------------------------------------------------------------
{-
Here is an example of a tactic that only needs to pattern match on a
variable or constructor, but not a function. We don't encounter
any problems when writing this kind of a tactic.
The tactic below changes a `Fin n` value into a `Fin (n + 0)` value.
-}
add-plus0-Fin : Tactic
add-plus0-Fin (`Fin n , i) = `Fin (n + 0) , subst Fin (plusrident n) i
add-plus0-Fin x = x
----------------------------------------------------------------------
{-
In contrast, it is not straightforward to write tactics that need
to pattern match on function calls.
For example, below is ideally what we want to write to change
a value of `Fin (n + 0)` to a value of `Fin n`.
-}
-- rm-plus0-Fin : Tactic
-- rm-plus0-Fin (`Fin (n + 0) , i) = `Fin n , subst Fin (sym (plusrident n)) i
-- rm-plus0-Fin x = x
----------------------------------------------------------------------
{-
Instead of matching directly on `Fin, we can match on a `Φ that
represents `Fin. This allows us to match on the the `+ function call
in the index position.
Because we cannot match against the function `el` (we would like to match
it against `eval`), we add to our return type that proves the
extentional equality between `el` and `eval`.
Also note that the tactic below can be used for any type indexed by
ℕ, not just Fin.
-}
rm-plus0 : Tactic
rm-plus0 (`Φ `Arith `ℕ el B (a `+ `zero) , φ b) =
`Π `Arith (λ x → `Id `ℕ (el x) (eval x))
`→
`Φ `Arith `ℕ el B a
,
λ f → φ
(subst (λ x → ⟦ B x ⟧)
(sym (f a))
(subst (λ x → ⟦ B x ⟧)
(sym (plusrident (eval a)))
(subst (λ x → ⟦ B x ⟧)
(f (a `+ `zero))
b)))
rm-plus0 x = x
----------------------------------------------------------------------
{-
Now it is possible to apply the generic tactic to a specific
value and have the desired behavior occur. Notice that the generated
extentional equality premise is trivially provable because
eval is always equal to eval.
-}
eg-rm-plus0 : (a : Arith)
→ ⟦ `Φ `Arith `ℕ eval `Fin (a `+ `zero) ⟧
→ ⟦ `Φ `Arith `ℕ eval `Fin a ⟧
eg-rm-plus0 a (φ i) = proj₂
(rm-plus0 (`Φ `Arith `ℕ eval `Fin (a `+ `zero) , φ i))
(λ _ → refl)
----------------------------------------------------------------------
{-
Here is the same tactic but using the specialized ΦArith type.
-}
rm-plus0-Arith : Tactic
rm-plus0-Arith (`ΦArith B (a `+ `zero) , φ i) =
`ΦArith B a
,
φ (subst (λ x → ⟦ B x ⟧) (sym (plusrident (eval a))) i)
rm-plus0-Arith x = x
----------------------------------------------------------------------
{-
And the same example usage, this time without needing to prove a premise.
-}
eg-rm-plus0-Arith : (a : Arith)
→ ⟦ `ΦArith `Fin (a `+ `zero) ⟧
→ ⟦ `ΦArith `Fin a ⟧
eg-rm-plus0-Arith a (φ i) = proj₂
(rm-plus0-Arith (`ΦArith `Fin (a `+ `zero) , φ i))
----------------------------------------------------------------------
{-
Finally, here is the same tactic but using the even more
specialized Fin₂ type.
-}
rm-plus0-Fin : Tactic
rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i) =
`Fin₂ a
,
fin (subst (λ x → ⟦ `Fin x ⟧) (sym (plusrident (eval a))) i)
rm-plus0-Fin x = x
----------------------------------------------------------------------
eg-rm-plus0-Fin : (a : Arith)
→ ⟦ `Fin₂ (a `+ `zero) ⟧
→ ⟦ `Fin₂ a ⟧
eg-rm-plus0-Fin a (fin i) = proj₂
(rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i))
----------------------------------------------------------------------
|
{-
This file demonstrates a technique that simulates the ability to
pattern match against function calls rather than just variables
or constructors.
This technique is necessary to write certain kinds of tactics
as generic functions.
-}
module TacticsMatchingFunctionCalls where
open import Data.Bool hiding ( _≟_ )
open import Data.Nat
open import Data.Fin hiding ( _+_ )
open import Data.Product hiding ( map )
open import Data.List
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
open import Function
----------------------------------------------------------------------
{-
Zero is the right identity of addition on the natural numbers.
-}
plusrident : (n : ℕ) → n ≡ n + 0
plusrident zero = refl
plusrident (suc n) = cong suc (plusrident n)
----------------------------------------------------------------------
{-
Arith is a universe of codes representing the natural numbers
along with a limited set of functions over them. We would like to write
tactics that pattern match against types indexed by applications of
these functions.
-}
data Arith : Set where
`zero : Arith
`suc : (a : Arith) → Arith
_`+_ _`*_ : (a₁ a₂ : Arith) → Arith
eval : Arith → ℕ
eval `zero = zero
eval (`suc a) = suc (eval a)
eval (a₁ `+ a₂) = eval a₁ + eval a₂
eval (a₁ `* a₂) = eval a₁ * eval a₂
----------------------------------------------------------------------
{-
Φ (standing for *function*-indexed types)
represents an indexed type, but we *can* pattern
match on function calls in its index position.
A - The type of codes, including constructors and functions.
i.e. Arith
A′ - The model underlying the codes.
i.e. ℕ
el - The evaluation function from code values to model values.
i.e. eval
B - The genuine indexed datatype that Φ represents.
i.e. Fin
a - The index value as a code, which may be a function call and hence supports
pattern matching.
i.e. (a `+ `zero)
-}
data Φ (A A′ : Set) (el : A → A′) (B : A′ → Set) (a : A) : Set where
φ : (b : B (el a)) → Φ A A′ el B a
----------------------------------------------------------------------
{-
Alternatively, we can specialize Φ to our Arith universe. This may be
a good use of our ability to cheaply define new datatypes via Desc.
-}
data ΦArith (B : ℕ → Set) (a : Arith) : Set where
φ : (b : B (eval a)) → ΦArith B a
----------------------------------------------------------------------
{-
Going further, we could even specialize B in ΦArith to Fin.
-}
data Fin₂ (a : Arith) : Set where
fin : (i : Fin (eval a)) → Fin₂ a
----------------------------------------------------------------------
{-
A small universe of dependent types that is sufficient for the
examples given below.
-}
data Type : Set
⟦_⟧ : Type → Set
data Type where
`Bool `ℕ `Arith : Type
`Fin : (n : ℕ) → Type
`Φ : (A A′ : Type)
(el : ⟦ A ⟧ → ⟦ A′ ⟧)
(B : ⟦ A′ ⟧ → Type)
(a : ⟦ A ⟧)
→ Type
`ΦArith : (B : ℕ → Type) (a : Arith) → Type
`Fin₂ : (a : Arith) → Type
`Π `Σ : (A : Type) (B : ⟦ A ⟧ → Type) → Type
`Id : (A : Type) (x y : ⟦ A ⟧) → Type
⟦ `Bool ⟧ = Bool
⟦ `ℕ ⟧ = ℕ
⟦ `Arith ⟧ = Arith
⟦ `Fin n ⟧ = Fin n
⟦ `Φ A A′ el B a ⟧ = Φ ⟦ A ⟧ ⟦ A′ ⟧ el (λ a → ⟦ B a ⟧) a
⟦ `ΦArith B a ⟧ = ΦArith (λ x → ⟦ B x ⟧) a
⟦ `Fin₂ a ⟧ = Fin₂ a
⟦ `Π A B ⟧ = (a : ⟦ A ⟧) → ⟦ B a ⟧
⟦ `Σ A B ⟧ = Σ ⟦ A ⟧ (λ a → ⟦ B a ⟧)
⟦ `Id A x y ⟧ = x ≡ y
_`→_ : (A B : Type) → Type
A `→ B = `Π A (const B)
_`×_ : (A B : Type) → Type
A `× B = `Σ A (const B)
----------------------------------------------------------------------
{-
Tactics are represented as generic functions taking a Dynamic value (of
any type), and returning a Dynamic value (at a possibly different type).
The convention is to behave like the identity function if the tactic
doesn't match the current value or fails.
In practice, the context will be a List of Dynamic values, and a tactic
will map a Context to a Context. For simplicity, the tactics I give below
only map a Dynamic to a Dynamic.
-}
Dynamic : Set
Dynamic = Σ Type ⟦_⟧
Tactic : Set
Tactic = Dynamic → Dynamic
----------------------------------------------------------------------
{-
Here is an example of a tactic that only needs to pattern match on a
variable or constructor, but not a function. We don't encounter
any problems when writing this kind of a tactic.
The tactic below changes a `Fin n` value into a `Fin (n + 0)` value.
-}
add-plus0-Fin : Tactic
add-plus0-Fin (`Fin n , i) = `Fin (n + 0) , subst Fin (plusrident n) i
add-plus0-Fin x = x
----------------------------------------------------------------------
{-
In contrast, it is not straightforward to write tactics that need
to pattern match on function calls.
For example, below is ideally what we want to write to change
a value of `Fin (n + 0)` to a value of `Fin n`.
-}
-- rm-plus0-Fin : Tactic
-- rm-plus0-Fin (`Fin (n + 0) , i) = `Fin n , subst Fin (sym (plusrident n)) i
-- rm-plus0-Fin x = x
----------------------------------------------------------------------
{-
Instead of matching directly on `Fin, we can match on a `Φ that
represents `Fin. This allows us to match on the the `+ function call
in the index position.
Because we cannot match against the function `el` (we would like to match
it against `eval`), we add to our return type that proves the
extentional equality between `el` and `eval`.
Also note that the tactic below can be used for any type indexed by
ℕ, not just Fin.
-}
rm-plus0 : Tactic
rm-plus0 (`Φ `Arith `ℕ el B (a `+ `zero) , φ b) =
`Π `Arith (λ x → `Id `ℕ (el x) (eval x))
`→
`Φ `Arith `ℕ el B a
,
λ f → φ
(subst (λ x → ⟦ B x ⟧)
(sym (f a))
(subst (λ x → ⟦ B x ⟧)
(sym (plusrident (eval a)))
(subst (λ x → ⟦ B x ⟧)
(f (a `+ `zero))
b)))
rm-plus0 x = x
----------------------------------------------------------------------
{-
Now it is possible to apply the generic tactic to a specific
value and have the desired behavior occur. Notice that the generated
extentional equality premise is trivially provable because
eval is always equal to eval.
-}
eg-rm-plus0 : (a : Arith)
→ ⟦ `Φ `Arith `ℕ eval `Fin (a `+ `zero) ⟧
→ ⟦ `Φ `Arith `ℕ eval `Fin a ⟧
eg-rm-plus0 a (φ i) = proj₂
(rm-plus0 (`Φ `Arith `ℕ eval `Fin (a `+ `zero) , φ i))
(λ _ → refl)
----------------------------------------------------------------------
{-
Here is the same tactic but using the specialized ΦArith type.
-}
rm-plus0-Arith : Tactic
rm-plus0-Arith (`ΦArith B (a `+ `zero) , φ i) =
`ΦArith B a
,
φ (subst (λ x → ⟦ B x ⟧) (sym (plusrident (eval a))) i)
rm-plus0-Arith x = x
----------------------------------------------------------------------
{-
And the same example usage, this time without needing to prove a premise.
-}
eg-rm-plus0-Arith : (a : Arith)
→ ⟦ `ΦArith `Fin (a `+ `zero) ⟧
→ ⟦ `ΦArith `Fin a ⟧
eg-rm-plus0-Arith a (φ i) = proj₂
(rm-plus0-Arith (`ΦArith `Fin (a `+ `zero) , φ i))
----------------------------------------------------------------------
{-
Finally, here is the same tactic but using the even more
specialized Fin₂ type.
-}
rm-plus0-Fin : Tactic
rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i) =
`Fin₂ a
,
fin (subst (λ x → ⟦ `Fin x ⟧) (sym (plusrident (eval a))) i)
rm-plus0-Fin x = x
----------------------------------------------------------------------
eg-rm-plus0-Fin : (a : Arith)
→ ⟦ `Fin₂ (a `+ `zero) ⟧
→ ⟦ `Fin₂ a ⟧
eg-rm-plus0-Fin a (fin i) = proj₂
(rm-plus0-Fin (`Fin₂ (a `+ `zero) , fin i))
----------------------------------------------------------------------
{-
A tactic to simplify by one step of `+.
-}
simp-step-plus0-Fin : Tactic
simp-step-plus0-Fin (`Fin₂ (`zero `+ a) , fin i) =
`Fin₂ a , fin i
simp-step-plus0-Fin (`Fin₂ (`suc a₁ `+ a₂) , fin i) =
`Fin₂ (`suc (a₁ `+ a₂)) , fin i
simp-step-plus0-Fin x = x
----------------------------------------------------------------------
{-
A semantics-preserving definitional evaluator, and a tactic
to simplify as far as possible using definitional equality.
This is like the "simp" tactic in Coq.
-}
eval₂ : (a₁ : Arith) → Σ Arith (λ a₂ → eval a₁ ≡ eval a₂)
eval₂ `zero = `zero , refl
eval₂ (`suc a)
with eval₂ a
... | a′ , p
rewrite p = `suc a′ , refl
eval₂ (`zero `+ a₂) = eval₂ a₂
eval₂ (`suc a₁ `+ a₂)
with eval₂ a₁ | eval₂ a₂
... | a₁′ , p | a₂′ , q
rewrite p | q = `suc (a₁′ `+ a₂′) , refl
eval₂ (a₁ `+ a₂)
with eval₂ a₁ | eval₂ a₂
... | a₁′ , p | a₂′ , q
rewrite p | q = (a₁′ `+ a₂′) , refl
eval₂ (`zero `* a₂) = `zero , refl
eval₂ (`suc a₁ `* a₂)
with eval₂ a₁ | eval₂ a₂
... | a₁′ , p | a₂′ , q
rewrite p | q = a₂′ `+ (a₁′ `* a₂′) , refl
eval₂ (a₁ `* a₂)
with eval₂ a₁ | eval₂ a₂
... | a₁′ , p | a₂′ , q
rewrite p | q = (a₁′ `* a₂′) , refl
simp-Fin : Tactic
simp-Fin (`Fin₂ a , fin i)
with eval₂ a
... | a′ , p rewrite p =
`Fin₂ a′ , fin i
simp-Fin x = x
----------------------------------------------------------------------
|
Add Coq's "simp" tactic.
|
Add Coq's "simp" tactic.
|
Agda
|
bsd-3-clause
|
spire/spire
|
71b3a14edcb512975548e8df67858a30b1f62d68
|
Syntax/Term/Plotkin.agda
|
Syntax/Term/Plotkin.agda
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Denotation.Environment Type
open import Syntax.Context.Plotkin B
-- Declarations of Term and Terms to enable mutual recursion
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
-- (Term Γ τ) represents a term of type τ
-- with free variables bound in Γ.
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- (Terms Γ Σ) represents a list of terms with types from Σ
-- with free variables bound in Γ.
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
TermConstructor : (Γ Σ : Context) (τ : Type) → Set
TermConstructor Γ ∅ τ′ = Term Γ τ′
TermConstructor Γ (τ • Σ) τ′ = Term Γ τ → TermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → TermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → TermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const constant)
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Denotation.Environment Type
open import Syntax.Context.Plotkin B
-- Declarations of Term and Terms to enable mutual recursion
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
-- (Term Γ τ) represents a term of type τ
-- with free variables bound in Γ.
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- (Terms Γ Σ) represents a list of terms with types from Σ
-- with free variables bound in Γ.
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → CurriedTermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const constant)
|
Rename TermConstructor to CurriedTermConstructor.
|
Rename TermConstructor to CurriedTermConstructor.
This rename prepares for introducing UncurriedTermConstructor which
will allow to clarify the role of lift-η-const-rec.
Old-commit-hash: b9de3c413f5a2a2015d3114bfb8483ab3078cc06
|
Agda
|
mit
|
inc-lc/ilc-agda
|
6538c1e3a868c0dc095139fc2243a8683b77c995
|
Data/NatBag/Properties.agda
|
Data/NatBag/Properties.agda
|
module Data.NatBag.Properties where
import Data.Nat as ℕ
open import Relation.Binary.PropositionalEquality
open import Data.NatBag
open import Data.Integer
open import Data.Sum hiding (map)
open import Data.Product hiding (map)
-- This import is too slow.
-- It causes Agda 2.3.2 to use so much memory that cai's
-- computer with 4GB RAM begins to thresh.
--
-- open import Data.Integer.Properties using (n⊖n≡0)
n⊖n≡0 : ∀ n → n ⊖ n ≡ + 0
n⊖n≡0 ℕ.zero = refl
n⊖n≡0 (ℕ.suc n) = n⊖n≡0 n
----------------
-- Statements --
----------------
b\\b=∅ : ∀ {b : Bag} → b \\ b ≡ empty
∅++b=b : ∀ {b : Bag} → empty ++ b ≡ b
b\\∅=b : ∀ {b : Bag} → b \\ empty ≡ b
∅\\b=-b : ∀ {b : Bag} → empty \\ b ≡ map₂ -_ b
-- ++d=\\-d : ∀ {b d : Bag} → b ++ d ≡ b \\ map₂ -_ d
b++[d\\b]=d : ∀ {b d : Bag} → b ++ (d \\ b) ≡ d
------------
-- Proofs --
------------
i-i=0 : ∀ {i : ℤ} → (i - i) ≡ (+ 0)
i-i=0 {+ ℕ.zero} = refl
i-i=0 {+ ℕ.suc n} = n⊖n≡0 n
i-i=0 { -[1+ n ]} = n⊖n≡0 n
-- Debug tool
-- Lets you try out inhabitance of any type anywhere
absurd! : ∀ {B C : Set} → 0 ≡ 1 → B → {x : B} → C
absurd! ()
-- Specialized absurdity needed to type check.
-- λ () hasn't enough information sometimes.
absurd : Nonzero (+ 0) → ∀ {A : Set} → A
absurd ()
-- Here to please the termination checker.
neb\\neb=∅ : ∀ {neb : NonemptyBag} → zipNonempty _-_ neb neb ≡ empty
neb\\neb=∅ {singleton i i≠0} with nonzero? (i - i)
... | inj₁ _ = refl
... | inj₂ 0≠0 rewrite i-i=0 {i} = absurd 0≠0
neb\\neb=∅ {i ∷ neb} with nonzero? (i - i)
... | inj₁ _ rewrite neb\\neb=∅ {neb} = refl
... | inj₂ 0≠0 rewrite neb\\neb=∅ {neb} | i-i=0 {i} = absurd 0≠0
{-
++d=\\-d {inj₁ ∅} {inj₁ ∅} = refl
++d=\\-d {inj₁ ∅} {inj₂ (i ∷ y)} = {!!}
++d=\\-d {inj₂ y} {d} = {!!}
++d=\\-d {inj₁ ∅} {inj₂ (singleton i i≠0)}
rewrite ∅++b=b {inj₂ (singleton i i≠0)}
with nonzero? i | nonzero? (+ 0 - i)
... | inj₂ _ | inj₂ 0-i≠0 =
begin
{!!}
≡⟨ {!!} ⟩
{!inj₂ (singleton (- i) ?)!}
∎ where open ≡-Reasoning
... | inj₁ i=0 | _ rewrite i=0 = absurd i≠0
++d=\\-d {inj₁ ∅} {inj₂ (singleton (+ 0) i≠0)} | _ | _ = absurd i≠0
++d=\\-d {inj₁ ∅} {inj₂ (singleton (+ (ℕ.suc n)) i≠0)}
| inj₂ (positive .n) | inj₁ ()
++d=\\-d {inj₁ ∅} {inj₂ (singleton -[1+ n ] i≠0)}
| inj₂ (negative .n) | inj₁ ()
-}
b\\b=∅ {inj₁ ∅} = refl
b\\b=∅ {inj₂ neb} = neb\\neb=∅ {neb}
∅++b=b {b} = {!!}
b\\∅=b {b} = {!!}
∅\\b=-b {b} = {!!}
negate : ∀ {i} → Nonzero i → Nonzero (- i)
negate (negative n) = positive n
negate (positive n) = negative n
negate′ : ∀ {i} → (i≠0 : Nonzero i) → Nonzero (+ 0 - i)
negate′ { -[1+ n ]} (negative .n) = positive n
negate′ {+ .(ℕ.suc n)} (positive n) = negative n
0-i=-i : ∀ {i} → + 0 - i ≡ - i
0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to
0-i=-i {+ ℕ.zero} = refl -- refl are different.
0-i=-i {+ ℕ.suc n} = refl
rewrite-singleton :
∀ (i : ℤ) (0-i≠0 : Nonzero (+ 0 - i)) ( -i≠0 : Nonzero (- i)) →
singleton (+ 0 - i) 0-i≠0 ≡ singleton (- i) -i≠0
rewrite-singleton (+ ℕ.zero) () ()
rewrite-singleton (+ ℕ.suc n) (negative .n) (negative .n) = refl
rewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl
negateSingleton : ∀ {i i≠0} →
mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0)
≡
inj₂ (singleton (- i) (negate i≠0))
-- Fun fact:
-- Pattern-match on implicit parameters in the first two
-- cases results in rejection by Agda.
negateSingleton {i} {i≠0} with nonzero? i | nonzero? (+ 0 - i)
negateSingleton | inj₂ (negative n) | inj₁ ()
negateSingleton | inj₂ (positive n) | inj₁ ()
negateSingleton {_} {i≠0} | inj₁ i=0 | _ rewrite i=0 = absurd i≠0
negateSingleton {i} {i≠0} | inj₂ _ | inj₂ 0-i≠0 =
begin -- Reasoning done in 1 step. Included for clarity only.
inj₂ (singleton (+ 0 + - i) 0-i≠0)
≡⟨ cong inj₂ (rewrite-singleton i 0-i≠0 (negate i≠0)) ⟩
inj₂ (singleton (- i) (negate i≠0))
∎ where open ≡-Reasoning
absurd[i-i≠0] : ∀ {i} → Nonzero (i - i) → ∀ {A : Set} → A
absurd[i-i≠0] {+ ℕ.zero} = absurd
absurd[i-i≠0] {+ ℕ.suc n} = absurd[i-i≠0] { -[1+ n ]}
absurd[i-i≠0] { -[1+ ℕ.zero ]} = absurd
absurd[i-i≠0] { -[1+ ℕ.suc n ]} = absurd[i-i≠0] { -[1+ n ]}
annihilate : ∀ {i i≠0} →
inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0)) ≡ inj₁ ∅
annihilate {i} with nonzero? (i - i)
... | inj₁ i-i=0 = λ {i≠0} → refl
... | inj₂ i-i≠0 = absurd[i-i≠0] {i} i-i≠0
{-
left-is-not-right : ∀ {A B : Set} {a : A} {b : B} →
inj₁ a ≡ inj₂ b → ∀ {X : Set} → X
left-is-not-right = λ {A} {B} {a} {b} → λ ()
never-both : ∀ {A B : Set} {sum : A ⊎ B} {a b} →
sum ≡ inj₁ a → sum ≡ inj₂ b → ∀ {X : Set} → X
never-both s=a s=b = left-is-not-right (trans (sym s=a) s=b)
empty-bag? : ∀ (b : Bag) → (b ≡ inj₁ ∅) ⊎ Σ NonemptyBag (λ neb → b ≡ inj₂ neb)
empty-bag? (inj₁ ∅) = inj₁ refl
empty-bag? (inj₂ neb) = inj₂ (neb , refl)
-}
b++[∅\\b]=∅ : ∀ {b} → b ++ (empty \\ b) ≡ empty
b++[∅\\b]=∅ {inj₁ ∅} = refl
b++[∅\\b]=∅ {inj₂ (singleton i i≠0)} =
begin
inj₂ (singleton i i≠0) ++
mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0)
≡⟨ cong₂ _++_ {x = inj₂ (singleton i i≠0)} refl
(negateSingleton {i} {i≠0}) ⟩
inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0))
≡⟨ annihilate {i} {i≠0} ⟩
inj₁ ∅
∎ where open ≡-Reasoning
b++[∅\\b]=∅ {inj₂ (i ∷ y)} = {!!}
b++[d\\b]=d {inj₁ ∅} {d} rewrite b\\∅=b {d} | ∅++b=b {d} = refl
b++[d\\b]=d {b} {inj₁ ∅} = b++[∅\\b]=∅ {b}
b++[d\\b]=d {inj₂ b} {inj₂ d} = {!!}
|
module Data.NatBag.Properties where
import Data.Nat as ℕ
open import Relation.Binary.PropositionalEquality
open import Data.NatBag
open import Data.Integer
open import Data.Sum hiding (map)
open import Data.Product hiding (map)
-- This import is too slow.
-- It causes Agda 2.3.2 to use so much memory that cai's
-- computer with 4GB RAM begins to thresh.
--
-- open import Data.Integer.Properties using (n⊖n≡0)
n⊖n≡0 : ∀ n → n ⊖ n ≡ + 0
n⊖n≡0 ℕ.zero = refl
n⊖n≡0 (ℕ.suc n) = n⊖n≡0 n
----------------
-- Statements --
----------------
b\\b=∅ : ∀ {b : Bag} → b \\ b ≡ empty
∅++b=b : ∀ {b : Bag} → empty ++ b ≡ b
b\\∅=b : ∀ {b : Bag} → b \\ empty ≡ b
∅\\b=-b : ∀ {b : Bag} → empty \\ b ≡ map₂ -_ b
b++[d\\b]=d : ∀ {b d : Bag} → b ++ (d \\ b) ≡ d
------------
-- Proofs --
------------
i-i=0 : ∀ {i : ℤ} → (i - i) ≡ (+ 0)
i-i=0 {+ ℕ.zero} = refl
i-i=0 {+ ℕ.suc n} = n⊖n≡0 n
i-i=0 { -[1+ n ]} = n⊖n≡0 n
-- Debug tool
-- Lets you try out inhabitance of any type anywhere
absurd! : ∀ {B C : Set} → 0 ≡ 1 → B → {x : B} → C
absurd! ()
-- Specialized absurdity needed to type check.
-- λ () hasn't enough information sometimes.
absurd : Nonzero (+ 0) → ∀ {A : Set} → A
absurd ()
-- Here to please the termination checker.
neb\\neb=∅ : ∀ {neb : NonemptyBag} → zipNonempty _-_ neb neb ≡ empty
neb\\neb=∅ {singleton i i≠0} with nonzero? (i - i)
... | inj₁ _ = refl
... | inj₂ 0≠0 rewrite i-i=0 {i} = absurd 0≠0
neb\\neb=∅ {i ∷ neb} with nonzero? (i - i)
... | inj₁ _ rewrite neb\\neb=∅ {neb} = refl
... | inj₂ 0≠0 rewrite neb\\neb=∅ {neb} | i-i=0 {i} = absurd 0≠0
b\\b=∅ {inj₁ ∅} = refl
b\\b=∅ {inj₂ neb} = neb\\neb=∅ {neb}
∅++b=b {b} = {!!}
b\\∅=b {b} = {!!}
∅\\b=-b {b} = {!!}
negate : ∀ {i} → Nonzero i → Nonzero (- i)
negate (negative n) = positive n
negate (positive n) = negative n
negate′ : ∀ {i} → (i≠0 : Nonzero i) → Nonzero (+ 0 - i)
negate′ { -[1+ n ]} (negative .n) = positive n
negate′ {+ .(ℕ.suc n)} (positive n) = negative n
0-i=-i : ∀ {i} → + 0 - i ≡ - i
0-i=-i { -[1+ n ]} = refl -- cases are split, for arguments to
0-i=-i {+ ℕ.zero} = refl -- refl are different.
0-i=-i {+ ℕ.suc n} = refl
rewrite-singleton :
∀ (i : ℤ) (0-i≠0 : Nonzero (+ 0 - i)) ( -i≠0 : Nonzero (- i)) →
singleton (+ 0 - i) 0-i≠0 ≡ singleton (- i) -i≠0
rewrite-singleton (+ ℕ.zero) () ()
rewrite-singleton (+ ℕ.suc n) (negative .n) (negative .n) = refl
rewrite-singleton ( -[1+ n ]) (positive .n) (positive .n) = refl
negateSingleton : ∀ {i i≠0} →
mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0)
≡
inj₂ (singleton (- i) (negate i≠0))
-- Fun fact:
-- Pattern-match on implicit parameters in the first two
-- cases results in rejection by Agda.
negateSingleton {i} {i≠0} with nonzero? i | nonzero? (+ 0 - i)
negateSingleton | inj₂ (negative n) | inj₁ ()
negateSingleton | inj₂ (positive n) | inj₁ ()
negateSingleton {_} {i≠0} | inj₁ i=0 | _ rewrite i=0 = absurd i≠0
negateSingleton {i} {i≠0} | inj₂ _ | inj₂ 0-i≠0 =
begin -- Reasoning done in 1 step. Included for clarity only.
inj₂ (singleton (+ 0 + - i) 0-i≠0)
≡⟨ cong inj₂ (rewrite-singleton i 0-i≠0 (negate i≠0)) ⟩
inj₂ (singleton (- i) (negate i≠0))
∎ where open ≡-Reasoning
absurd[i-i≠0] : ∀ {i} → Nonzero (i - i) → ∀ {A : Set} → A
absurd[i-i≠0] {+ ℕ.zero} = absurd
absurd[i-i≠0] {+ ℕ.suc n} = absurd[i-i≠0] { -[1+ n ]}
absurd[i-i≠0] { -[1+ ℕ.zero ]} = absurd
absurd[i-i≠0] { -[1+ ℕ.suc n ]} = absurd[i-i≠0] { -[1+ n ]}
annihilate : ∀ {i i≠0} →
inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0)) ≡ inj₁ ∅
annihilate {i} with nonzero? (i - i)
... | inj₁ i-i=0 = λ {i≠0} → refl
... | inj₂ i-i≠0 = absurd[i-i≠0] {i} i-i≠0
b++[∅\\b]=∅ : ∀ {b} → b ++ (empty \\ b) ≡ empty
b++[∅\\b]=∅ {inj₁ ∅} = refl
b++[∅\\b]=∅ {inj₂ (singleton i i≠0)} =
begin
inj₂ (singleton i i≠0) ++
mapNonempty₂ (λ j → + 0 - j) (singleton i i≠0)
≡⟨ cong₂ _++_ {x = inj₂ (singleton i i≠0)} refl
(negateSingleton {i} {i≠0}) ⟩
inj₂ (singleton i i≠0) ++ inj₂ (singleton (- i) (negate i≠0))
≡⟨ annihilate {i} {i≠0} ⟩
inj₁ ∅
∎ where open ≡-Reasoning
b++[∅\\b]=∅ {inj₂ (i ∷ y)} = {!!}
b++[d\\b]=d {inj₁ ∅} {d} rewrite b\\∅=b {d} | ∅++b=b {d} = refl
b++[d\\b]=d {b} {inj₁ ∅} = b++[∅\\b]=∅ {b}
b++[d\\b]=d {inj₂ b} {inj₂ d} = {!!}
|
Remove dead code of Data.NatBag.Properties Checkpoint: Recast property proofs using set theoretic methods (#55)
|
Remove dead code of Data.NatBag.Properties
Checkpoint: Recast property proofs using set theoretic methods (#55)
Old-commit-hash: 04b32e5d92152cdab089a25c0d8f9f4c39279fb0
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2971c097d16d13188929ef857808539310db0b8e
|
meaning.agda
|
meaning.agda
|
module meaning where
open import Level
record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where
constructor
meaning
field
{Semantics} : Set ℓ
⟦_⟧ : Syntax → Semantics
open Meaning {{...}} public
|
module meaning where
open import Level
record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where
constructor
meaning
field
{Semantics} : Set ℓ
⟨_⟩⟦_⟧ : Syntax → Semantics
open Meaning {{...}} public
renaming (⟨_⟩⟦_⟧ to ⟦_⟧)
open Meaning public
using (⟨_⟩⟦_⟧)
|
Improve printing of resolved overloading.
|
Improve printing of resolved overloading.
After this change, the semantic brackets will contain the syntactic
thing even if Agda displays explicitly resolved overloaded notation.
Old-commit-hash: c8cbc43ed8e715342e0cc3bccc98e0be2dd23560
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b5190556d5bc58a768c39b9fa871dea8b2d6f0f1
|
Parametric/Change/Term.agda
|
Parametric/Change/Term.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Constant : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Constant
open ChangeType.Structure Base ΔBase
open import Data.Product
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ diff-term
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ apply-term
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Constant : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Constant
open ChangeType.Structure Base ΔBase
open import Data.Product
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
abs₄ (λ g f x Δx → app g (x ⊕σ Δx) ⊝τ app f x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff = app₂ diff-term
apply : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply = app₂ apply-term
|
Fix bug in diff-term.
|
Fix bug in diff-term.
Old-commit-hash: 01ada999b676d2a8a2ab289b3a0c79f1524620e5
|
Agda
|
mit
|
inc-lc/ilc-agda
|
011116b87218eb84d5010b97f93c495b4f090fae
|
incremental.agda
|
incremental.agda
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Semantics
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
Env⟦_⟧ : Context → Set
Env⟦ ∅ ⟧ = Empty
Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- Semantics
lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
lookup⟦ this ⟧ (v • ρ) = v
lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- Semantics
eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ)
eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ)
eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Γ
-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = {!!}
|
module incremental where
-- SIMPLE TYPES
-- Syntax
data Type : Set where
_⇒_ : (τ₁ τ₂ : Type) → Type
infixr 5 _⇒_
-- Semantics
Dom⟦_⟧ : Type -> Set
Dom⟦ τ₁ ⇒ τ₂ ⟧ = Dom⟦ τ₁ ⟧ → Dom⟦ τ₂ ⟧
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Semantics
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
Env⟦_⟧ : Context → Set
Env⟦ ∅ ⟧ = Empty
Env⟦ τ • Γ ⟧ = Bind Dom⟦ τ ⟧ Env⟦ Γ ⟧
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- Semantics
lookup⟦_⟧ : ∀ {Γ τ} → Var Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
lookup⟦ this ⟧ (v • ρ) = v
lookup⟦ that x ⟧ (v • ρ) = lookup⟦ x ⟧ ρ
-- TERMS
-- Syntax
data Term : Context → Type → Set where
abs : ∀ {Γ τ₁ τ₂} → (t : Term (τ₁ • Γ) τ₂) → Term Γ (τ₁ ⇒ τ₂)
app : ∀ {Γ τ₁ τ₂} → (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) → Term Γ τ₂
var : ∀ {Γ τ} → (x : Var Γ τ) → Term Γ τ
-- Semantics
eval⟦_⟧ : ∀ {Γ τ} → Term Γ τ → Env⟦ Γ ⟧ → Dom⟦ τ ⟧
eval⟦ abs t ⟧ ρ = λ v → eval⟦ t ⟧ (v • ρ)
eval⟦ app t₁ t₂ ⟧ ρ = (eval⟦ t₁ ⟧ ρ) (eval⟦ t₂ ⟧ ρ)
eval⟦ var x ⟧ ρ = lookup⟦ x ⟧ ρ
-- WEAKENING
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₁ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • Γ₁ ⋎ Γ₂
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Weaken a term to a super context
weaken : ∀ {Γ₁ Γ₂ Γ₃ τ} → Term (Γ₁ ⋎ Γ₃) τ → Term (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
weaken {Γ₁} {Γ₂} (abs {τ₁ = τ} t) = abs (weaken {τ • Γ₁} {Γ₂} t)
weaken {Γ₁} {Γ₂} (app t₁ t₂) = app (weaken {Γ₁} {Γ₂} t₁) (weaken {Γ₁} {Γ₂} t₂)
weaken {Γ₁} {Γ₂} (var x) = var (lift {Γ₁} {Γ₂} x)
-- CHANGE TYPES
Δ-Type : Type → Type
Δ-Type (τ₁ ⇒ τ₂) = τ₁ ⇒ Δ-Type τ₂
apply : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ τ ⇒ τ)
apply {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app apply (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λf. λx. apply ( df x ) ( f x )
compose : ∀ {τ Γ} → Term Γ (Δ-Type τ ⇒ Δ-Type τ ⇒ Δ-Type τ)
compose {τ₁ ⇒ τ₂} =
abs (abs (abs (app (app compose (app (var (that (that this))) (var this))) (app (var (that this)) (var this)))))
-- λdf. λdg. λx. compose ( df x ) ( dg x )
nil : ∀ {τ Γ} → Term Γ (Δ-Type τ)
nil {τ₁ ⇒ τ₂} =
abs nil
-- λx. nil
-- Hey, apply is α-equivalent to compose, what's going on?
-- Oh, and `Δ-Type` is the identity function?
-- CHANGE CONTEXTS
Δ-Context : Context → Context
Δ-Context ∅ = ∅
Δ-Context (τ • Γ) = τ • Δ-Type τ • Γ
-- CHANGING TERMS WHEN THE ENVIRONMENT CHANGES
Δ-term : ∀ {Γ₁ Γ₂ τ} → Term (Γ₁ ⋎ Γ₂) τ → Term (Γ₁ ⋎ Δ-Context Γ₂) (Δ-Type τ)
Δ-term {Γ} (abs {τ₁ = τ} t) = abs (Δ-term {τ • Γ} t)
Δ-term {Γ} (app t₁ t₂) = {!!}
Δ-term {Γ} (var x) = {!!}
|
Implement nil changes.
|
Implement nil changes.
Old-commit-hash: da546d60ec48fe5f6f8b261aac1fdbb71b18fa69
|
Agda
|
mit
|
inc-lc/ilc-agda
|
f2fde70dc52c3050e537b787efd3c7efa9778118
|
Syntax/Term/Plotkin.agda
|
Syntax/Term/Plotkin.agda
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Syntax.Context.Plotkin B
-- Declarations of Term and Terms to enable mutual recursion
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
-- (Term Γ τ) represents a term of type τ
-- with free variables bound in Γ.
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- (Terms Γ Σ) represents a list of terms with types from Σ
-- with free variables bound in Γ.
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ
uncurriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ
uncurriedConst constant = const constant
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ
curryTermConstructor {∅} k = k ∅
curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts))
curriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
curriedConst constant = curryTermConstructor (uncurriedConst constant)
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Syntax.Context.Plotkin B
-- Declarations of Term and Terms to enable mutual recursion
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
-- (Term Γ τ) represents a term of type τ
-- with free variables bound in Γ.
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- (Terms Γ Σ) represents a list of terms with types from Σ
-- with free variables bound in Γ.
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
UncurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
UncurriedTermConstructor Γ Σ τ = Terms Γ Σ → Term Γ τ
uncurriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → UncurriedTermConstructor Γ Σ τ
uncurriedConst constant = const constant
CurriedTermConstructor : (Γ Σ : Context) (τ : Type) → Set
CurriedTermConstructor Γ ∅ τ′ = Term Γ τ′
CurriedTermConstructor Γ (τ • Σ) τ′ = Term Γ τ → CurriedTermConstructor Γ Σ τ′
curryTermConstructor : ∀ {Σ Γ τ} → UncurriedTermConstructor Γ Σ τ → CurriedTermConstructor Γ Σ τ
curryTermConstructor {∅} k = k ∅
curryTermConstructor {τ • Σ} k = λ t → curryTermConstructor (λ ts → k (t • ts))
curriedConst : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → CurriedTermConstructor Γ Σ τ
curriedConst constant = curryTermConstructor (uncurriedConst constant)
-- HOAS-like smart constructors for lambdas, for different arities.
abs₁ :
∀ {Γ τ₁ τ} →
(∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → (x : Term Γ′ τ₁) → Term Γ′ τ) →
(Term Γ (τ₁ ⇒ τ))
abs₁ {Γ} {τ₁} = λ f → abs (f {Γ≼Γ′ = drop τ₁ • ≼-refl} (var this))
abs₂ :
∀ {Γ τ₁ τ₂ τ} →
(∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ) →
(Term Γ (τ₁ ⇒ τ₂ ⇒ τ))
abs₂ f =
abs₁ (λ {_} {Γ≼Γ′} x₁ →
abs₁ (λ {_} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
abs₃ :
∀ {Γ τ₁ τ₂ τ₃ τ} →
(∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ) →
(Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ))
abs₃ f =
abs₁ (λ {_} {Γ≼Γ′} x₁ →
abs₂ (λ {_} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
abs₄ :
∀ {Γ τ₁ τ₂ τ₃ τ₄ τ} →
(∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ) →
(Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ))
abs₄ f =
abs₁ (λ {_} {Γ≼Γ′} x₁ →
abs₃ (λ {_} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
abs₅ :
∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ} →
(∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ) →
(Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ))
abs₅ f =
abs₁ (λ {_} {Γ≼Γ′} x₁ →
abs₄ (λ {_} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
abs₆ :
∀ {Γ τ₁ τ₂ τ₃ τ₄ τ₅ τ₆ τ} →
(∀ {Γ′} → {Γ≼Γ′ : Γ ≼ Γ′} → Term Γ′ τ₁ → Term Γ′ τ₂ → Term Γ′ τ₃ → Term Γ′ τ₄ → Term Γ′ τ₅ → Term Γ′ τ₆ → Term Γ′ τ) →
(Term Γ (τ₁ ⇒ τ₂ ⇒ τ₃ ⇒ τ₄ ⇒ τ₅ ⇒ τ₆ ⇒ τ))
abs₆ f =
abs₁ (λ {_} {Γ≼Γ′} x₁ →
abs₅ (λ {_} {Γ′≼Γ′₁} →
f {Γ≼Γ′ = ≼-trans Γ≼Γ′ Γ′≼Γ′₁} (weaken Γ′≼Γ′₁ x₁)))
|
Introduce HOAS-like smart constructors for lambdas
|
Introduce HOAS-like smart constructors for lambdas
Old-commit-hash: 6bf4939f8ec84c611d98f6f2395332943573dfe5
|
Agda
|
mit
|
inc-lc/ilc-agda
|
8514aefef6973b813691fb922e6d44e072cfc75d
|
generic-zero-knowledge-interactive.agda
|
generic-zero-knowledge-interactive.agda
|
open import Type
open import Data.Bool.NP as Bool hiding (check)
open import Data.Nat
open import Data.Maybe
open import Data.Product.NP
open import Data.Bits
open import Function.NP
open import Relation.Binary.PropositionalEquality.NP
open import sum
module generic-zero-knowledge-interactive where
-- A random argument, this is only a formal notation to
-- indicate that the argument is supposed to be picked
-- at random uniformly. (do not confuse with our randomness
-- monad).
record ↺ (A : ★) : ★ where
constructor rand
field get : A
module M (Permutation : ★)
(_⁻¹ : Endo Permutation)
(sumπ : Sum Permutation)
(μπ : SumProp sumπ)
(Rₚ-xtra : ★) -- extra prover/adversary randomness
(sumRₚ-xtra : Sum Rₚ-xtra)
(μRₚ-xtra : SumProp sumRₚ-xtra)
(Problem : ★)
(_==_ : Problem → Problem → Bit)
(==-refl : ∀ {pb} → (pb == pb) ≡ true)
(_∙P_ : Permutation → Endo Problem)
(⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x)
(Solution : ★)
(_∙S_ : Permutation → Endo Solution)
(⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x)
(check : Problem → Solution → Bit)
(check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s)
(easy-pb : Permutation → Problem)
(easy-sol : Permutation → Solution)
(check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true)
where
-- prover/adversary randomness
Rₚ : ★
Rₚ = Permutation × Rₚ-xtra
sumRₚ : Sum Rₚ
sumRₚ = sumπ ×Sum sumRₚ-xtra
μRₚ : SumProp sumRₚ
μRₚ = μπ ×μ μRₚ-xtra
R = Bit × Rₚ
sumR : Sum R
sumR = sumBit ×Sum sumRₚ
μR : SumProp sumR
μR = μBit ×μ μRₚ
check-π : Problem → Solution → Rₚ → Bit
check-π p s (π , _) = check (π ∙P p) (π ∙S s)
otp-∙-check : let #_ = count μRₚ
in
∀ p₀ s₀ p₁ s₁ →
check p₀ s₀ ≡ check p₁ s₁ →
#(check-π p₀ s₀) ≡ #(check-π p₁ s₁)
otp-∙-check p₀ s₀ p₁ s₁ check-pf =
count-ext μRₚ {f = check-π p₀ s₀} {check-π p₁ s₁} (λ π,r →
check-π p₀ s₀ π,r ≡⟨ check-∙ p₀ s₀ (proj₁ π,r) ⟩
check p₀ s₀ ≡⟨ check-pf ⟩
check p₁ s₁ ≡⟨ sym (check-∙ p₁ s₁ (proj₁ π,r)) ⟩
check-π p₁ s₁ π,r ∎)
where open ≡-Reasoning
#_ : (↺ (Bit × Permutation × Rₚ-xtra) → Bit) → ℕ
# f = count μR (f ∘ rand)
_≡#_ : (f g : ↺ (Bit × Rₚ) → Bit) → ★
f ≡# g = # f ≡ # g
{-
otp-∙ : let otp = λ O pb s → count μRₚ (λ { (π , _) → O (π ∙P pb) (π ∙S s) }) in
∀ pb₀ s₀ pb₁ s₁ →
check pb₀ s₀ ≡ check pb₁ s₁ →
(O : _ → _ → Bit) → otp O pb₀ s₀ ≡ otp O pb₁ s₁
otp-∙ pb₀ s₀ pb₁ s₁ check-pf O = {!(μπ ×Sum-proj₂ μRₚ-xtra ?!}
-}
Answer : Bit → ★
Answer false{-0b-} = Permutation
Answer true {-1b-} = Solution
answer : Permutation → Solution → ∀ b → Answer b
answer π _ false = π
answer _ s true = s
-- The prover is the advesary in the generic terminology,
-- and the verifier is the challenger.
DepProver : ★
DepProver = Problem → ↺ Rₚ → (b : Bit) → Problem × Answer b
Prover₀ : ★
Prover₀ = Problem → ↺ Rₚ → Problem × Permutation
Prover₁ : ★
Prover₁ = Problem → ↺ Rₚ → Problem × Solution
Prover : ★
Prover = Prover₀ × Prover₁
prover : DepProver → Prover
prover dpr = (λ pb r → dpr pb r 0b) , (λ pb r → dpr pb r 1b)
depProver : Prover → DepProver
depProver (pr₀ , pr₁) pb r false = pr₀ pb r
depProver (pr₀ , pr₁) pb r true = pr₁ pb r
-- Here we show that the explicit commitment step seems useless given
-- the formalization. The verifier can "trust" the prover on the fact
-- that any choice is going to be govern only by the problem and the
-- randomness.
module WithCommitment (Commitment : ★)
(AnswerWC : Bit → ★)
(reveal : ∀ b → Commitment → AnswerWC b → Problem × Answer b) where
ProverWC = (Problem → Rₚ → Commitment)
× (Problem → Rₚ → (b : Bit) → AnswerWC b)
depProver' : ProverWC → DepProver
depProver' (pr₀ , pr₁) pb (rand rₚ) b = reveal b (pr₀ pb rₚ) (pr₁ pb rₚ b)
Verif : Problem → ∀ b → Problem × Answer b → Bit
Verif pb false{-0b-} (π∙pb , π) = (π ∙P pb) == π∙pb
Verif pb true {-1b-} (π∙pb , π∙s) = check π∙pb π∙s
_⇄′_ : Problem → DepProver → Bit → ↺ Rₚ → Bit
(pb ⇄′ pr) b (rand rₚ) = Verif pb b (pr pb (rand rₚ) b)
_⇄_ : Problem → DepProver → ↺ (Bit × Rₚ) → Bit
(pb ⇄ pr) (rand (b , rₚ)) = (pb ⇄′ pr) b (rand rₚ)
_⇄''_ : Problem → Prover → ↺ (Bit × Rₚ) → Bit
pb ⇄'' pr = pb ⇄ depProver pr
honest : (Problem → Maybe Solution) → DepProver
honest solve pb (rand (π , rₚ)) b = (π ∙P pb , answer π sol b)
module Honest where
sol : Solution
sol with solve pb
... | just sol = π ∙S sol
... | nothing = π ∙S easy-sol π
module WithCorrectSolver (pb : Problem)
(s : Solution)
(check-s : check pb s ≡ true)
where
-- When the honest prover has a solution, he gets accepted
-- unconditionally by the verifier.
honest-accepted : ∀ r → (pb ⇄ honest (const (just s))) r ≡ 1b
honest-accepted (rand (true , π , rₚ)) rewrite check-∙ pb s π = check-s
honest-accepted (rand (false , π , rₚ)) = ==-refl
honest-⅁ = λ pb s → (pb ⇄ honest (const (just s)))
module HonestLeakZeroKnowledge (pb₀ pb₁ : Problem)
(s₀ s₁ : Solution)
(check-pf : check pb₀ s₀ ≡ check pb₁ s₁) where
helper : ∀ rₚ → Bool.toℕ ((pb₀ ⇄′ honest (const (just s₀))) 0b (rand rₚ))
≡ Bool.toℕ ((pb₁ ⇄′ honest (const (just s₁))) 0b (rand rₚ))
helper (π , rₚ) rewrite ==-refl {π ∙P pb₀} | ==-refl {π ∙P pb₁} = refl
honest-leak : honest-⅁ pb₀ s₀ ≡# honest-⅁ pb₁ s₁
honest-leak rewrite otp-∙-check pb₀ s₀ pb₁ s₁ check-pf | sum-ext μRₚ helper = refl
module HonestLeakZeroKnowledge' (pb : Problem)
(s₀ s₁ : Solution)
(check-pf : check pb s₀ ≡ check pb s₁) where
honest-leak : honest-⅁ pb s₀ ≡# honest-⅁ pb s₁
honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s₀ s₁ check-pf
-- Predicts b=b′
cheater : ∀ b′ → DepProver
cheater b′ pb (rand (π , _)) b = π ∙P (case b′ 0→ pb 1→ easy-pb π)
, answer π (π ∙S easy-sol π) b
-- If cheater predicts correctly, verifer accepts him
cheater-accepted : ∀ b pb rₚ → (pb ⇄′ cheater b) b rₚ ≡ 1b
cheater-accepted true pb (rand (π , rₚ)) = check-easy π
cheater-accepted false pb (rand (π , rₚ)) = ==-refl
-- If cheater predicts incorrecty, verifier rejects him
module CheaterRejected (pb : Problem)
(not-easy-sol : ∀ π → check (π ∙P pb) (π ∙S easy-sol π) ≡ false)
(not-easy-pb : ∀ π → ((π ∙P pb) == (π ∙P easy-pb π)) ≡ false) where
cheater-rejected : ∀ b rₚ → (pb ⇄′ cheater (not b)) b rₚ ≡ 0b
cheater-rejected true (rand (π , rₚ)) = not-easy-sol π
cheater-rejected false (rand (π , rₚ)) = not-easy-pb π
module DLog (ℤq : ★)
(_⊞_ : ℤq → ℤq → ℤq)
(⊟_ : ℤq → ℤq)
(G : ★)
(g : G)
(_^_ : G → ℤq → G)
(_∙_ : G → G → G)
(⊟-⊞ : ∀ π x → (⊟ π) ⊞ (π ⊞ x) ≡ x)
(^⊟-∙ : ∀ α β x → ((α ^ (⊟ x)) ∙ ((α ^ x) ∙ β)) ≡ β)
-- (∙-assoc : ∀ α β γ → α ∙ (β ∙ γ) ≡ (α ∙ β) ∙ γ)
(dist-^-⊞ : ∀ α x y → α ^ (x ⊞ y) ≡ (α ^ x) ∙ (α ^ y))
(_==_ : G → G → Bool)
(==-refl : ∀ {α} → (α == α) ≡ true)
(==-cong-∙ : ∀ {α β b} γ → α == β ≡ b → (γ ∙ α) == (γ ∙ β) ≡ b)
(==-true : ∀ {α β} → α == β ≡ true → α ≡ β)
(sumℤq : Sum ℤq)
(μℤq : SumProp sumℤq)
(Rₚ-xtra : ★) -- extra prover/adversary randomness
(sumRₚ-xtra : Sum Rₚ-xtra)
(μRₚ-xtra : SumProp sumRₚ-xtra)
(some-ℤq : ℤq)
where
Permutation = ℤq
Problem = G
Solution = ℤq
_⁻¹ : Endo Permutation
π ⁻¹ = ⊟ π
g^_ : ℤq → G
g^ x = g ^ x
_∙P_ : Permutation → Endo Problem
π ∙P p = g^ π ∙ p
⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x
⁻¹-inverseP π x rewrite ^⊟-∙ g x π = refl
_∙S_ : Permutation → Endo Solution
π ∙S s = π ⊞ s
⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x
⁻¹-inverseS = ⊟-⊞
check : Problem → Solution → Bit
check p s = p == g^ s
check-∙' : ∀ p s π b → check p s ≡ b → check (π ∙P p) (π ∙S s) ≡ b
check-∙' p s π true check-p-s rewrite dist-^-⊞ g π s | ==-true check-p-s = ==-refl
check-∙' p s π false check-p-s rewrite dist-^-⊞ g π s = ==-cong-∙ (g^ π) check-p-s
check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s
check-∙ p s π = check-∙' p s π (check p s) refl
easy-sol : Permutation → Solution
easy-sol π = some-ℤq
easy-pb : Permutation → Problem
easy-pb π = g^(easy-sol π)
check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true
check-easy π rewrite dist-^-⊞ g π (easy-sol π) = ==-refl
open M Permutation _⁻¹ sumℤq μℤq Rₚ-xtra sumRₚ-xtra μRₚ-xtra
Problem _==_ ==-refl _∙P_ ⁻¹-inverseP Solution _∙S_ ⁻¹-inverseS check check-∙ easy-pb easy-sol check-easy
-- -}
-- -}
-- -}
-- -}
-- -}
|
open import Type
open import Data.Bool.NP as Bool hiding (check)
open import Data.Nat
open import Data.Maybe
open import Data.Product.NP
open import Data.Bits
open import Function.NP
open import Relation.Binary.PropositionalEquality.NP
open import sum
module generic-zero-knowledge-interactive where
-- A random argument, this is only a formal notation to
-- indicate that the argument is supposed to be picked
-- at random uniformly. (do not confuse with our randomness
-- monad).
record ↺ (A : ★) : ★ where
constructor rand
field get : A
module M (Permutation : ★)
(_⁻¹ : Endo Permutation)
(μπ : SumProp Permutation)
(Rₚ-xtra : ★) -- extra prover/adversary randomness
(μRₚ-xtra : SumProp Rₚ-xtra)
(Problem : ★)
(_==_ : Problem → Problem → Bit)
(==-refl : ∀ {pb} → (pb == pb) ≡ true)
(_∙P_ : Permutation → Endo Problem)
(⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x)
(Solution : ★)
(_∙S_ : Permutation → Endo Solution)
(⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x)
(check : Problem → Solution → Bit)
(check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s)
(easy-pb : Permutation → Problem)
(easy-sol : Permutation → Solution)
(check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true)
where
-- prover/adversary randomness
Rₚ : ★
Rₚ = Permutation × Rₚ-xtra
μRₚ : SumProp Rₚ
μRₚ = μπ ×μ μRₚ-xtra
R = Bit × Rₚ
μR : SumProp R
μR = μBit ×μ μRₚ
check-π : Problem → Solution → Rₚ → Bit
check-π p s (π , _) = check (π ∙P p) (π ∙S s)
otp-∙-check : let #_ = count μRₚ
in
∀ p₀ s₀ p₁ s₁ →
check p₀ s₀ ≡ check p₁ s₁ →
#(check-π p₀ s₀) ≡ #(check-π p₁ s₁)
otp-∙-check p₀ s₀ p₁ s₁ check-pf =
count-ext μRₚ {f = check-π p₀ s₀} {check-π p₁ s₁} (λ π,r →
check-π p₀ s₀ π,r ≡⟨ check-∙ p₀ s₀ (proj₁ π,r) ⟩
check p₀ s₀ ≡⟨ check-pf ⟩
check p₁ s₁ ≡⟨ sym (check-∙ p₁ s₁ (proj₁ π,r)) ⟩
check-π p₁ s₁ π,r ∎)
where open ≡-Reasoning
#_ : (↺ (Bit × Permutation × Rₚ-xtra) → Bit) → ℕ
# f = count μR (f ∘ rand)
_≡#_ : (f g : ↺ (Bit × Rₚ) → Bit) → ★
f ≡# g = # f ≡ # g
{-
otp-∙ : let otp = λ O pb s → count μRₚ (λ { (π , _) → O (π ∙P pb) (π ∙S s) }) in
∀ pb₀ s₀ pb₁ s₁ →
check pb₀ s₀ ≡ check pb₁ s₁ →
(O : _ → _ → Bit) → otp O pb₀ s₀ ≡ otp O pb₁ s₁
otp-∙ pb₀ s₀ pb₁ s₁ check-pf O = {!(μπ ×Sum-proj₂ μRₚ-xtra ?!}
-}
Answer : Bit → ★
Answer false{-0b-} = Permutation
Answer true {-1b-} = Solution
answer : Permutation → Solution → ∀ b → Answer b
answer π _ false = π
answer _ s true = s
-- The prover is the advesary in the generic terminology,
-- and the verifier is the challenger.
DepProver : ★
DepProver = Problem → ↺ Rₚ → (b : Bit) → Problem × Answer b
Prover₀ : ★
Prover₀ = Problem → ↺ Rₚ → Problem × Permutation
Prover₁ : ★
Prover₁ = Problem → ↺ Rₚ → Problem × Solution
Prover : ★
Prover = Prover₀ × Prover₁
prover : DepProver → Prover
prover dpr = (λ pb r → dpr pb r 0b) , (λ pb r → dpr pb r 1b)
depProver : Prover → DepProver
depProver (pr₀ , pr₁) pb r false = pr₀ pb r
depProver (pr₀ , pr₁) pb r true = pr₁ pb r
-- Here we show that the explicit commitment step seems useless given
-- the formalization. The verifier can "trust" the prover on the fact
-- that any choice is going to be govern only by the problem and the
-- randomness.
module WithCommitment (Commitment : ★)
(AnswerWC : Bit → ★)
(reveal : ∀ b → Commitment → AnswerWC b → Problem × Answer b) where
ProverWC = (Problem → Rₚ → Commitment)
× (Problem → Rₚ → (b : Bit) → AnswerWC b)
depProver' : ProverWC → DepProver
depProver' (pr₀ , pr₁) pb (rand rₚ) b = reveal b (pr₀ pb rₚ) (pr₁ pb rₚ b)
Verif : Problem → ∀ b → Problem × Answer b → Bit
Verif pb false{-0b-} (π∙pb , π) = (π ∙P pb) == π∙pb
Verif pb true {-1b-} (π∙pb , π∙s) = check π∙pb π∙s
_⇄′_ : Problem → DepProver → Bit → ↺ Rₚ → Bit
(pb ⇄′ pr) b (rand rₚ) = Verif pb b (pr pb (rand rₚ) b)
_⇄_ : Problem → DepProver → ↺ (Bit × Rₚ) → Bit
(pb ⇄ pr) (rand (b , rₚ)) = (pb ⇄′ pr) b (rand rₚ)
_⇄''_ : Problem → Prover → ↺ (Bit × Rₚ) → Bit
pb ⇄'' pr = pb ⇄ depProver pr
honest : (Problem → Maybe Solution) → DepProver
honest solve pb (rand (π , rₚ)) b = (π ∙P pb , answer π sol b)
module Honest where
sol : Solution
sol with solve pb
... | just sol = π ∙S sol
... | nothing = π ∙S easy-sol π
module WithCorrectSolver (pb : Problem)
(s : Solution)
(check-s : check pb s ≡ true)
where
-- When the honest prover has a solution, he gets accepted
-- unconditionally by the verifier.
honest-accepted : ∀ r → (pb ⇄ honest (const (just s))) r ≡ 1b
honest-accepted (rand (true , π , rₚ)) rewrite check-∙ pb s π = check-s
honest-accepted (rand (false , π , rₚ)) = ==-refl
honest-⅁ = λ pb s → (pb ⇄ honest (const (just s)))
module HonestLeakZeroKnowledge (pb₀ pb₁ : Problem)
(s₀ s₁ : Solution)
(check-pf : check pb₀ s₀ ≡ check pb₁ s₁) where
helper : ∀ rₚ → Bool.toℕ ((pb₀ ⇄′ honest (const (just s₀))) 0b (rand rₚ))
≡ Bool.toℕ ((pb₁ ⇄′ honest (const (just s₁))) 0b (rand rₚ))
helper (π , rₚ) rewrite ==-refl {π ∙P pb₀} | ==-refl {π ∙P pb₁} = refl
honest-leak : honest-⅁ pb₀ s₀ ≡# honest-⅁ pb₁ s₁
honest-leak rewrite otp-∙-check pb₀ s₀ pb₁ s₁ check-pf | sum-ext μRₚ helper = refl
module HonestLeakZeroKnowledge' (pb : Problem)
(s₀ s₁ : Solution)
(check-pf : check pb s₀ ≡ check pb s₁) where
honest-leak : honest-⅁ pb s₀ ≡# honest-⅁ pb s₁
honest-leak = HonestLeakZeroKnowledge.honest-leak pb pb s₀ s₁ check-pf
-- Predicts b=b′
cheater : ∀ b′ → DepProver
cheater b′ pb (rand (π , _)) b = π ∙P (case b′ 0→ pb 1→ easy-pb π)
, answer π (π ∙S easy-sol π) b
-- If cheater predicts correctly, verifer accepts him
cheater-accepted : ∀ b pb rₚ → (pb ⇄′ cheater b) b rₚ ≡ 1b
cheater-accepted true pb (rand (π , rₚ)) = check-easy π
cheater-accepted false pb (rand (π , rₚ)) = ==-refl
-- If cheater predicts incorrecty, verifier rejects him
module CheaterRejected (pb : Problem)
(not-easy-sol : ∀ π → check (π ∙P pb) (π ∙S easy-sol π) ≡ false)
(not-easy-pb : ∀ π → ((π ∙P pb) == (π ∙P easy-pb π)) ≡ false) where
cheater-rejected : ∀ b rₚ → (pb ⇄′ cheater (not b)) b rₚ ≡ 0b
cheater-rejected true (rand (π , rₚ)) = not-easy-sol π
cheater-rejected false (rand (π , rₚ)) = not-easy-pb π
module DLog (ℤq : ★)
(_⊞_ : ℤq → ℤq → ℤq)
(⊟_ : ℤq → ℤq)
(G : ★)
(g : G)
(_^_ : G → ℤq → G)
(_∙_ : G → G → G)
(⊟-⊞ : ∀ π x → (⊟ π) ⊞ (π ⊞ x) ≡ x)
(^⊟-∙ : ∀ α β x → ((α ^ (⊟ x)) ∙ ((α ^ x) ∙ β)) ≡ β)
-- (∙-assoc : ∀ α β γ → α ∙ (β ∙ γ) ≡ (α ∙ β) ∙ γ)
(dist-^-⊞ : ∀ α x y → α ^ (x ⊞ y) ≡ (α ^ x) ∙ (α ^ y))
(_==_ : G → G → Bool)
(==-refl : ∀ {α} → (α == α) ≡ true)
(==-cong-∙ : ∀ {α β b} γ → α == β ≡ b → (γ ∙ α) == (γ ∙ β) ≡ b)
(==-true : ∀ {α β} → α == β ≡ true → α ≡ β)
(μℤq : SumProp ℤq)
(Rₚ-xtra : ★) -- extra prover/adversary randomness
(μRₚ-xtra : SumProp Rₚ-xtra)
(some-ℤq : ℤq)
where
Permutation = ℤq
Problem = G
Solution = ℤq
_⁻¹ : Endo Permutation
π ⁻¹ = ⊟ π
g^_ : ℤq → G
g^ x = g ^ x
_∙P_ : Permutation → Endo Problem
π ∙P p = g^ π ∙ p
⁻¹-inverseP : ∀ π x → π ⁻¹ ∙P (π ∙P x) ≡ x
⁻¹-inverseP π x rewrite ^⊟-∙ g x π = refl
_∙S_ : Permutation → Endo Solution
π ∙S s = π ⊞ s
⁻¹-inverseS : ∀ π x → π ⁻¹ ∙S (π ∙S x) ≡ x
⁻¹-inverseS = ⊟-⊞
check : Problem → Solution → Bit
check p s = p == g^ s
check-∙' : ∀ p s π b → check p s ≡ b → check (π ∙P p) (π ∙S s) ≡ b
check-∙' p s π true check-p-s rewrite dist-^-⊞ g π s | ==-true check-p-s = ==-refl
check-∙' p s π false check-p-s rewrite dist-^-⊞ g π s = ==-cong-∙ (g^ π) check-p-s
check-∙ : ∀ p s π → check (π ∙P p) (π ∙S s) ≡ check p s
check-∙ p s π = check-∙' p s π (check p s) refl
easy-sol : Permutation → Solution
easy-sol π = some-ℤq
easy-pb : Permutation → Problem
easy-pb π = g^(easy-sol π)
check-easy : ∀ π → check (π ∙P easy-pb π) (π ∙S easy-sol π) ≡ true
check-easy π rewrite dist-^-⊞ g π (easy-sol π) = ==-refl
open M Permutation _⁻¹ μℤq Rₚ-xtra μRₚ-xtra
Problem _==_ ==-refl _∙P_ ⁻¹-inverseP Solution _∙S_ ⁻¹-inverseS check check-∙ easy-pb easy-sol check-easy
-- -}
-- -}
-- -}
-- -}
-- -}
|
update generic ZK
|
update generic ZK
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
569e98b5b6d8a84329f54249ad3f83477357f717
|
flipbased-implem.agda
|
flipbased-implem.agda
|
module flipbased-implem where
open import Function
open import Data.Bits
open import Data.Nat.NP
open import Data.Vec
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
import Data.Fin as Fin
open ≡ using (_≗_; _≡_)
open Fin using (Fin; suc)
import flipbased
-- “↺ n A” reads like: “toss n coins and then return a value of type A”
record ↺ {a} n (A : Set a) : Set a where
constructor mk
field
run↺ : Bits n → A
open ↺ public
private
-- If you are not allowed to toss any coin, then you are deterministic.
Det : ∀ {a} → Set a → Set a
Det = ↺ 0
runDet : ∀ {a} {A : Set a} → Det A → A
runDet f = run↺ f []
toss : ↺ 1 Bit
toss = mk head
return↺ : ∀ {n a} {A : Set a} → A → ↺ n A
return↺ = mk ∘ const
map↺ : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → ↺ n A → ↺ n B
map↺ f x = mk (f ∘ run↺ x)
-- map↺ f x ≗ x >>=′ (return {0} ∘ f)
join↺ : ∀ {n₁ n₂ a} {A : Set a} → ↺ n₁ (↺ n₂ A) → ↺ (n₁ + n₂) A
join↺ {n₁} x = mk (λ bs → run↺ (run↺ x (take _ bs)) (drop n₁ bs))
-- join↺ x = x >>= id
comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → ↺ m A → ↺ n A
comap f (mk g) = mk (g ∘ f)
private
take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n
take≤ z≤n _ = []
take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs
weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → ↺ m A → ↺ n A
weaken≤ p = comap (take≤ p)
open flipbased ↺ toss weaken≤ return↺ map↺ join↺ public
_≗↺_ : ∀ {c a} {A : Set a} (f g : ↺ c A) → Set a
f ≗↺ g = run↺ f ≗ run↺ g
⅁ : ℕ → Set
⅁ n = ↺ n Bit
_≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ⅁ c) → Set
⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b
≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c})
≗⅁-trans p q b R = ≡.trans (p b R) (q b R)
count↺ᶠ : ∀ {c} → ⅁ c → Fin (suc (2^ c))
count↺ᶠ f = #⟨ run↺ f ⟩ᶠ
count↺ : ∀ {c} → ⅁ c → ℕ
count↺ = Fin.toℕ ∘ count↺ᶠ
_∼[_]⅁_ : ∀ {m n} → ⅁ m → (ℕ → ℕ → Set) → ⅁ n → Set
_∼[_]⅁_ {m} {n} f _∼_ g = ⟨2^ n * count↺ f ⟩ ∼ ⟨2^ m * count↺ g ⟩
_∼[_]⅁′_ : ∀ {n} → ⅁ n → (ℕ → ℕ → Set) → ⅁ n → Set
_∼[_]⅁′_ {n} f _∼_ g = count↺ f ∼ count↺ g
_≈⅁_ : ∀ {m n} → ⅁ m → ⅁ n → Set
f ≈⅁ g = f ∼[ _≡_ ]⅁ g
_≈⅁′_ : ∀ {n} (f g : ⅁ n) → Set
f ≈⅁′ g = f ∼[ _≡_ ]⅁′ g
≈⅁-refl : ∀ {n} {f : ⅁ n} → f ≈⅁ f
≈⅁-refl = ≡.refl
≈⅁-sym : ∀ {n} → Symmetric {A = ⅁ n} _≈⅁_
≈⅁-sym = ≡.sym
≈⅁-trans : ∀ {n} → Transitive {A = ⅁ n} _≈⅁_
≈⅁-trans = ≡.trans
≗⇒≈⅁ : ∀ {c} {f g : ⅁ c} → f ≗↺ g → f ≈⅁ g
≗⇒≈⅁ pf rewrite ext-# pf = ≡.refl
≈⅁′⇒≈⅁ : ∀ {n} {f g : ⅁ n} → f ≈⅁′ g → f ≈⅁ g
≈⅁′⇒≈⅁ eq rewrite eq = ≡.refl
≈⅁⇒≈⅁′ : ∀ {n} {f g : ⅁ n} → f ≈⅁ g → f ≈⅁′ g
≈⅁⇒≈⅁′ {n} = 2^-inj n
≈⅁-cong : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ≈⅁ f' → g ≈⅁ g'
≈⅁-cong f≗g f'≗g' f≈f' rewrite ext-# f≗g | ext-# f'≗g' = f≈f'
≈⅁′-cong : ∀ {c} {f g f' g' : ⅁ c} → f ≗↺ g → f' ≗↺ g' → f ≈⅁′ f' → g ≈⅁′ g'
≈⅁′-cong f≗g f'≗g' f≈f' rewrite ext-# f≗g | ext-# f'≗g' = f≈f'
|
module flipbased-implem where
open import Function
open import Data.Bits
open import Data.Nat.NP
open import Data.Vec
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
import Data.Fin as Fin
open ≡ using (_≗_; _≡_)
open Fin using (Fin; suc)
import flipbased
-- “↺ n A” reads like: “toss n coins and then return a value of type A”
record ↺ {a} n (A : Set a) : Set a where
constructor mk
field
run↺ : Bits n → A
open ↺ public
private
-- If you are not allowed to toss any coin, then you are deterministic.
Det : ∀ {a} → Set a → Set a
Det = ↺ 0
runDet : ∀ {a} {A : Set a} → Det A → A
runDet f = run↺ f []
toss : ↺ 1 Bit
toss = mk head
return↺ : ∀ {n a} {A : Set a} → A → ↺ n A
return↺ = mk ∘ const
map↺ : ∀ {n a b} {A : Set a} {B : Set b} → (A → B) → ↺ n A → ↺ n B
map↺ f x = mk (f ∘ run↺ x)
-- map↺ f x ≗ x >>=′ (return {0} ∘ f)
join↺ : ∀ {n₁ n₂ a} {A : Set a} → ↺ n₁ (↺ n₂ A) → ↺ (n₁ + n₂) A
join↺ {n₁} x = mk (λ bs → run↺ (run↺ x (take _ bs)) (drop n₁ bs))
-- join↺ x = x >>= id
comap : ∀ {m n a} {A : Set a} → (Bits n → Bits m) → ↺ m A → ↺ n A
comap f (mk g) = mk (g ∘ f)
private
take≤ : ∀ {a} {A : Set a} {m n} → n ≤ m → Vec A m → Vec A n
take≤ z≤n _ = []
take≤ (s≤s p) (x ∷ xs) = x ∷ take≤ p xs
weaken≤ : ∀ {m n a} {A : Set a} → m ≤ n → ↺ m A → ↺ n A
weaken≤ p = comap (take≤ p)
open flipbased ↺ toss weaken≤ return↺ map↺ join↺ public
_≗↺_ : ∀ {c a} {A : Set a} (f g : ↺ c A) → Set a
f ≗↺ g = run↺ f ≗ run↺ g
⅁ : ℕ → Set
⅁ n = ↺ n Bit
_≗⅁_ : ∀ {c} (⅁₀ ⅁₁ : Bit → ⅁ c) → Set
⅁₀ ≗⅁ ⅁₁ = ∀ b → ⅁₀ b ≗↺ ⅁₁ b
≗⅁-trans : ∀ {c} → Transitive (_≗⅁_ {c})
≗⅁-trans p q b R = ≡.trans (p b R) (q b R)
count↺ᶠ : ∀ {c} → ⅁ c → Fin (suc (2^ c))
count↺ᶠ f = #⟨ run↺ f ⟩ᶠ
count↺ : ∀ {c} → ⅁ c → ℕ
count↺ f = #⟨ run↺ f ⟩
≗↺-cong-# : ∀ {n} {f g : ⅁ n} → f ≗↺ g → count↺ f ≡ count↺ g
≗↺-cong-# {f = f} {g} = ≗-cong-# (run↺ f) (run↺ g)
_∼[_]⅁_ : ∀ {m n} → ⅁ m → (ℕ → ℕ → Set) → ⅁ n → Set
_∼[_]⅁_ {m} {n} f _∼_ g = ⟨2^ n * count↺ f ⟩ ∼ ⟨2^ m * count↺ g ⟩
_∼[_]⅁′_ : ∀ {n} → ⅁ n → (ℕ → ℕ → Set) → ⅁ n → Set
_∼[_]⅁′_ {n} f _∼_ g = count↺ f ∼ count↺ g
_≈⅁_ : ∀ {m n} → ⅁ m → ⅁ n → Set
f ≈⅁ g = f ∼[ _≡_ ]⅁ g
_≈⅁′_ : ∀ {n} (f g : ⅁ n) → Set
f ≈⅁′ g = f ∼[ _≡_ ]⅁′ g
≈⅁-refl : ∀ {n} {f : ⅁ n} → f ≈⅁ f
≈⅁-refl = ≡.refl
≈⅁-sym : ∀ {n} → Symmetric {A = ⅁ n} _≈⅁_
≈⅁-sym = ≡.sym
≈⅁-trans : ∀ {n} → Transitive {A = ⅁ n} _≈⅁_
≈⅁-trans = ≡.trans
≗⇒≈⅁ : ∀ {c} {f g : ⅁ c} → f ≗↺ g → f ≈⅁ g
≗⇒≈⅁ pf rewrite ≗↺-cong-# pf = ≡.refl
≈⅁′⇒≈⅁ : ∀ {n} {f g : ⅁ n} → f ≈⅁′ g → f ≈⅁ g
≈⅁′⇒≈⅁ eq rewrite eq = ≡.refl
≈⅁⇒≈⅁′ : ∀ {n} {f g : ⅁ n} → f ≈⅁ g → f ≈⅁′ g
≈⅁⇒≈⅁′ {n} = 2^-inj n
≈⅁-cong : ∀ {c c'} {f g : ⅁ c} {f' g' : ⅁ c'} → f ≗↺ g → f' ≗↺ g' → f ≈⅁ f' → g ≈⅁ g'
≈⅁-cong f≗g f'≗g' f≈f' rewrite ≗↺-cong-# f≗g | ≗↺-cong-# f'≗g' = f≈f'
≈⅁′-cong : ∀ {c} {f g f' g' : ⅁ c} → f ≗↺ g → f' ≗↺ g' → f ≈⅁′ f' → g ≈⅁′ g'
≈⅁′-cong f≗g f'≗g' f≈f' rewrite ≗↺-cong-# f≗g | ≗↺-cong-# f'≗g' = f≈f'
data Rat : Set where _/_ : (num denom : ℕ) → Rat
Pr[_≡1] : ∀ {n} (f : ⅁ n) → Rat
Pr[_≡1] {n} f = count↺ f / 2^ n
|
Switch count↺ to be based on search
|
Switch count↺ to be based on search
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
b590f8a3ed001a65516e5f2211013e1a46683692
|
ZK/PartialHeliosVerifier.agda
|
ZK/PartialHeliosVerifier.agda
|
{-# OPTIONS --without-K #-}
module ZK.PartialHeliosVerifier where
open import Type
open import Function hiding (case_of_)
open import Data.Bool.Base
open import Data.Product
open import Data.List.Base using (List; []; _∷_; and; foldr)
open import Data.String.Base using (String)
open import FFI.JS as JS hiding (_+_; _/_; _*_; join)
open import FFI.JS.BigI as BigI
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
open import FFI.JS.SHA1
open import FFI.JS.Proc
open import Control.Process.Type
join : String → List String → String
join sep [] = ""
join sep (x ∷ xs) = x ++ foldr (λ y z → sep ++ y ++ z) "" xs
{-
instance
showBigI = mk λ _ → showString ∘ BigI.toString
-}
G = BigI
ℤq = BigI
BigI▹G : BigI → G
BigI▹G = id
BigI▹ℤq : BigI → ℤq
BigI▹ℤq = id
non0I : BigI → BigI
non0I x with equals x 0I
... | true = throw "Should be non zero!" 0I
... | false = x
module BigICG (p q : BigI) where
_^_ : G → ℤq → G
_·_ : G → G → G
_/_ : G → G → G
_+_ : ℤq → ℤq → ℤq
_*_ : ℤq → ℤq → ℤq
_==_ : (x y : G) → Bool
_^_ = λ x y → modPow x y p
_·_ = λ x y → mod (multiply x y) p
_/_ = λ x y → mod (multiply x (modInv (non0I y) p)) p
_+_ = λ x y → mod (add x y) q
_*_ = λ x y → mod (multiply x y) q
_==_ = equals
sumI : List BigI → BigI
sumI = foldr _+_ 0I
bignum : Number → BigI
bignum n = bigI (Number▹String n) "10"
-- TODO check with undefined
bigdec : JSValue → BigI
bigdec v = bigI (castString v) "10"
{-
print : {A : Set}{{_ : Show A}} → A → Callback0
print = Console.log ∘ show
-}
PubKey = G
EncRnd = ℤq {- randomness used for encryption of ct -}
Message = G {- plain text message -}
Challenge = ℤq
Response = ℤq
CommitmentPart = G
CipherTextPart = G
module HeliosVerifyV3 (g : G) p q (y : PubKey) where
open BigICG p q
verify-chaum-pedersen : (α β : CipherTextPart)(M : Message)(A B : G)(c : Challenge)(s : Response) → Bool
verify-chaum-pedersen α β M A B c s
= trace "α=" α λ _ →
trace "β=" β λ _ →
trace "M=" M λ _ →
trace "A=" A λ _ →
trace "B=" B λ _ →
trace "c=" c λ _ →
trace "s=" s λ _ →
(g ^ s) == (A · (α ^ c))
∧ (y ^ s) == (B · ((β / M) ^ c))
verify-individual-proof : (α β : CipherTextPart)(ix : Number)(π : JSValue) → Bool
verify-individual-proof α β ix π
= trace "m=" m λ _ →
trace "M=" M λ _ →
trace "commitmentA=" A λ _ →
trace "commitmentB=" B λ _ →
trace "challenge=" c λ _ →
trace "response=" s λ _ →
res
where
m = bignum ix
M = g ^ m
A = bigdec (π ·« "commitment" » ·« "A" »)
B = bigdec (π ·« "commitment" » ·« "B" »)
c = bigdec (π ·« "challenge" »)
s = bigdec (π ·« "response" »)
res = verify-chaum-pedersen α β M A B c s
-- Conform to HeliosV3 but it is too weak.
-- One should follow a "Strong Fiat Shamir" transformation
-- from interactive ZK proof to non-interactive ZK proofs.
-- Namely one should hash the statement as well.
--
-- SHA1(A0 + "," + B0 + "," + A1 + "," + B1 + ... + "Amax" + "," + Bmax)
hash-commitments : JSArray JSValue → BigI
hash-commitments πs =
fromHex $
trace-call "SHA1(commitments)=" SHA1 $
trace-call "commitments=" (join ",") $
decodeJSArray πs λ _ π →
(castString (π ·« "commitment" » ·« "A" »))
++ "," ++
(castString (π ·« "commitment" » ·« "B" »))
sum-challenges : JSArray JSValue → BigI
sum-challenges πs =
sumI $ decodeJSArray πs λ _ π → bigdec (π ·« "challenge" »)
verify-challenges : JSArray JSValue → Bool
verify-challenges πs =
trace "hash(commitments)=" h λ _ →
trace "sum(challenge)=" c λ _ →
h == c
where
h = hash-commitments πs
c = sum-challenges πs
verify-choice : (v : JSValue)(πs : JSArray JSValue) → Bool
verify-choice v πs =
trace "α=" α λ _ →
trace "β=" β λ _ →
verify-challenges πs
∧
and (decodeJSArray πs $ verify-individual-proof α β)
where
α = bigdec (v ·« "alpha" »)
β = bigdec (v ·« "beta" »)
verify-choices : (choices πs : JSArray JSValue) → Bool
verify-choices choices πs =
trace "TODO: check the array size of choices πs together election data" "" λ _ →
and $ decodeJSArray choices λ ix choice →
trace "verify choice " ix λ _ →
verify-choice choice (castJSArray (πs Array[ ix ]))
verify-answer : (answer : JSValue) → Bool
verify-answer answer =
trace "TODO: CHECK the overall_proof" "" λ _ →
verify-choices choices individual-proofs
where
choices = answer ·« "choices" »A
individual-proofs = answer ·« "individual_proofs" »A
overall-proof = answer ·« "overall_proof" »A
verify-answers : (answers : JSArray JSValue) → Bool
verify-answers answers =
and $ decodeJSArray answers λ ix a →
trace "verify answer " ix λ _ →
verify-answer a
{-
verify-election-hash =
""
-- notice the whitespaces and the alphabetical order of keys
-- {"email": ["[email protected]", "[email protected]"], "first_name": "Ben", "last_name": "Adida"}
computed_hash = base64.b64encode(hash.new(election.toJSON()).digest())[:-1]
computed_hash == vote.election_hash:
""
-}
-- module _ {-(election : JSValue)-} where
verify-ballot : (ballot : JSValue) → Bool
verify-ballot ballot =
trace "TODO: CHECK the election_hash: " election_hash λ _ →
trace "TODO: CHECK the election_uuid: " election_uuid λ _ →
verify-answers answers
where
answers = ballot ·« "answers" »A
election_hash = ballot ·« "election_hash" »
election_uuid = ballot ·« "election_uuid" »
verify-ballots : (ballots : JSArray JSValue) → Bool
verify-ballots ballots =
and $ decodeJSArray ballots λ ix ballot →
trace "verify ballot " ix λ _ →
verify-ballot ballot
-- Many checks are still missing!
verify-helios-election : (arg : JSValue) → Bool
verify-helios-election arg = trace "res=" res id
where
ed = arg ·« "election_data" »
bs = arg ·« "ballots" »A
pk = ed ·« "public_key" »
g = bigdec (pk ·« "g" »)
p = bigdec (pk ·« "p" »)
q = bigdec (pk ·« "q" »)
y = bigdec (pk ·« "y" »)
res =
trace "g=" g λ _ →
trace "p=" p λ _ →
trace "q=" q λ _ →
trace "y=" y λ _ →
HeliosVerifyV3.verify-ballots g p q y bs
srv : URI → JSProc
srv d =
recv d λ q →
send d (fromBool (verify-helios-election q))
end
-- Working around Agda.Primitive.lsuc being undefined
case_of_ : {A : Set} {B : Set} → A → (A → B) → B
case x of f = f x
main : JS!
main =
Process.argv !₁ λ args →
case JSArray▹ListString args of λ {
(_node ∷ _run ∷ _test ∷ args') →
case args' of λ {
[] →
server "127.0.0.1" "1337" srv !₁ λ uri →
Console.log (showURI uri)
; (arg ∷ args'') →
case args'' of λ {
[] →
let opts =
-- fromJSObject (fromObject (("encoding" , fromString "utf8") ∷ []))
-- nullJS
JSON-parse "{\"encoding\":\"utf8\"}"
in
Console.log ("readFile=" ++ arg) >>
FS.readFile arg opts !₂ λ err dat →
Console.log ("readFile: err=" ++ JS.toString err) >>
Console.log (Bool▹String (verify-helios-election (JSON-parse (castString dat))))
; _ →
Console.log "usage"
}
}
; _ →
Console.log "usage"
}
-- -}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
module ZK.PartialHeliosVerifier where
open import Type
open import Function hiding (case_of_)
open import Data.Bool.Base
open import Data.Product
open import Data.List.Base using (List; []; _∷_; and; foldr)
open import Data.String.Base using (String)
open import FFI.JS as JS hiding (_+_; _/_; _*_; join)
open import FFI.JS.BigI as BigI
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
open import FFI.JS.SHA1
open import FFI.JS.Proc
open import Control.Process.Type
join : String → List String → String
join sep [] = ""
join sep (x ∷ xs) = x ++ foldr (λ y z → sep ++ y ++ z) "" xs
{-
instance
showBigI = mk λ _ → showString ∘ BigI.toString
-}
G = BigI
ℤq = BigI
BigI▹G : BigI → G
BigI▹G = id
BigI▹ℤq : BigI → ℤq
BigI▹ℤq = id
non0I : BigI → BigI
non0I x with equals x 0I
... | true = throw "Should be non zero!" 0I
... | false = x
module BigICG (p q : BigI) where
_^_ : G → ℤq → G
_·_ : G → G → G
_/_ : G → G → G
_+_ : ℤq → ℤq → ℤq
_*_ : ℤq → ℤq → ℤq
_==_ : (x y : G) → Bool
_^_ = λ x y → modPow x y p
_·_ = λ x y → mod (multiply x y) p
_/_ = λ x y → mod (multiply x (modInv (non0I y) p)) p
_+_ = λ x y → mod (add x y) q
_*_ = λ x y → mod (multiply x y) q
_==_ = equals
sumI : List BigI → BigI
sumI = foldr _+_ 0I
bignum : Number → BigI
bignum n = bigI (Number▹String n) "10"
-- TODO check with undefined
bigdec : JSValue → BigI
bigdec v = bigI (castString v) "10"
{-
print : {A : Set}{{_ : Show A}} → A → Callback0
print = Console.log ∘ show
-}
PubKey = G
EncRnd = ℤq {- randomness used for encryption of ct -}
Message = G {- plain text message -}
Challenge = ℤq
Response = ℤq
CommitmentPart = G
CipherTextPart = G
module HeliosVerifyV3 (g : G) p q (y : PubKey) where
open BigICG p q
verify-chaum-pedersen : (α β : CipherTextPart)(M : Message)(A B : G)(c : Challenge)(s : Response) → Bool
verify-chaum-pedersen α β M A B c s
= trace "α=" α λ _ →
trace "β=" β λ _ →
trace "M=" M λ _ →
trace "A=" A λ _ →
trace "B=" B λ _ →
trace "c=" c λ _ →
trace "s=" s λ _ →
(g ^ s) == (A · (α ^ c))
∧ (y ^ s) == (B · ((β / M) ^ c))
verify-individual-proof : (α β : CipherTextPart)(ix : Number)(π : JSValue) → Bool
verify-individual-proof α β ix π
= trace "m=" m λ _ →
trace "M=" M λ _ →
trace "commitmentA=" A λ _ →
trace "commitmentB=" B λ _ →
trace "challenge=" c λ _ →
trace "response=" s λ _ →
res
where
m = bignum ix
M = g ^ m
A = bigdec (π ·« "commitment" » ·« "A" »)
B = bigdec (π ·« "commitment" » ·« "B" »)
c = bigdec (π ·« "challenge" »)
s = bigdec (π ·« "response" »)
res = verify-chaum-pedersen α β M A B c s
-- Conform to HeliosV3 but it is too weak.
-- One should follow a "Strong Fiat Shamir" transformation
-- from interactive ZK proof to non-interactive ZK proofs.
-- Namely one should hash the statement as well.
--
-- SHA1(A0 + "," + B0 + "," + A1 + "," + B1 + ... + "Amax" + "," + Bmax)
hash-commitments : JSArray JSValue → BigI
hash-commitments πs =
fromHex $
trace-call "SHA1(commitments)=" SHA1 $
trace-call "commitments=" (join ",") $
decodeJSArray πs λ _ π →
(castString (π ·« "commitment" » ·« "A" »))
++ "," ++
(castString (π ·« "commitment" » ·« "B" »))
sum-challenges : JSArray JSValue → BigI
sum-challenges πs =
sumI $ decodeJSArray πs λ _ π → bigdec (π ·« "challenge" »)
verify-challenges : JSArray JSValue → Bool
verify-challenges πs =
trace "hash(commitments)=" h λ _ →
trace "sum(challenge)=" c λ _ →
h == c
where
h = hash-commitments πs
c = sum-challenges πs
verify-choice : (v : JSValue)(πs : JSArray JSValue) → Bool
verify-choice v πs =
trace "α=" α λ _ →
trace "β=" β λ _ →
verify-challenges πs
∧
and (decodeJSArray πs $ verify-individual-proof α β)
where
α = bigdec (v ·« "alpha" »)
β = bigdec (v ·« "beta" »)
verify-choices : (choices πs : JSArray JSValue) → Bool
verify-choices choices πs =
trace "TODO: check the array size of choices πs together election data" "" λ _ →
and $ decodeJSArray choices λ ix choice →
trace "verify choice " ix λ _ →
verify-choice choice (castJSArray (πs Array[ ix ]))
verify-answer : (answer : JSValue) → Bool
verify-answer answer =
trace "TODO: CHECK the overall_proof" "" λ _ →
verify-choices choices individual-proofs
where
choices = answer ·« "choices" »A
individual-proofs = answer ·« "individual_proofs" »A
overall-proof = answer ·« "overall_proof" »A
verify-answers : (answers : JSArray JSValue) → Bool
verify-answers answers =
and $ decodeJSArray answers λ ix a →
trace "verify answer " ix λ _ →
verify-answer a
{-
verify-election-hash =
""
-- notice the whitespaces and the alphabetical order of keys
-- {"email": ["[email protected]", "[email protected]"], "first_name": "Ben", "last_name": "Adida"}
computed_hash = base64.b64encode(hash.new(election.toJSON()).digest())[:-1]
computed_hash == vote.election_hash:
""
-}
-- module _ {-(election : JSValue)-} where
verify-ballot : (ballot : JSValue) → Bool
verify-ballot ballot =
trace "TODO: CHECK the election_hash: " election_hash λ _ →
trace "TODO: CHECK the election_uuid: " election_uuid λ _ →
verify-answers answers
where
answers = ballot ·« "answers" »A
election_hash = ballot ·« "election_hash" »
election_uuid = ballot ·« "election_uuid" »
verify-ballots : (ballots : JSArray JSValue) → Bool
verify-ballots ballots =
and $ decodeJSArray ballots λ ix ballot →
trace "verify ballot " ix λ _ →
verify-ballot ballot
-- Many checks are still missing!
verify-helios-election : (arg : JSValue) → Bool
verify-helios-election arg = trace "res=" res id
where
ed = arg ·« "election_data" »
bs = arg ·« "ballots" »A
pk = ed ·« "public_key" »
g = bigdec (pk ·« "g" »)
p = bigdec (pk ·« "p" »)
q = bigdec (pk ·« "q" »)
y = bigdec (pk ·« "y" »)
res =
trace "g=" g λ _ →
trace "p=" p λ _ →
trace "q=" q λ _ →
trace "y=" y λ _ →
HeliosVerifyV3.verify-ballots g p q y bs
srv : URI → JSProc
srv d =
recv d λ q →
send d (fromBool (verify-helios-election q))
end
-- Working around Agda.Primitive.lsuc being undefined
case_of_ : {A : Set} {B : Set} → A → (A → B) → B
case x of f = f x
main : JS!
main =
Process.argv >>= λ args →
case JSArray▹ListString args of λ {
(_node ∷ _run ∷ _test ∷ args') →
case args' of λ {
[] →
server "127.0.0.1" "1337" srv >>= λ uri →
Console.log (showURI uri)
; (arg ∷ args'') →
case args'' of λ {
[] →
let opts =
-- fromJSObject (fromObject (("encoding" , fromString "utf8") ∷ []))
-- nullJS
JSON-parse "{\"encoding\":\"utf8\"}"
in
Console.log ("readFile=" ++ arg) >>
FS.readFile arg opts >>== λ err dat →
Console.log ("readFile: err=" ++ JS.toString err) >>
Console.log (Bool▹String (verify-helios-election (JSON-parse (castString dat))))
; _ →
Console.log "usage"
}
}
; _ →
Console.log "usage"
}
-- -}
-- -}
-- -}
-- -}
-- -}
|
Update binds
|
Update binds
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
24e0a0189105b8f3f2766a604a1ad5423b51b77e
|
Parametric/Change/Derive.agda
|
Parametric/Change/Derive.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Derive
{Base : Type.Structure}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open ChangeType.Structure Base ΔBase
Structure : Set
Structure = ∀ {Γ Σ τ} →
Const Σ τ →
Terms (ΔContext Γ) Σ →
Terms (ΔContext Γ) (mapContext ΔType Σ) →
Term (ΔContext Γ) (ΔType τ)
module Structure (deriveConst : Structure) where
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
fit-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) Σ
fit-terms = weaken-terms Γ≼ΔΓ
deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (mapContext ΔType Σ)
derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ)
derive-terms {∅} ∅ = ∅
derive-terms {τ • Σ} (t • ts) = derive t • derive-terms ts
derive (var x) = var (deriveVar x)
derive (app s t) = app (app (derive s) (fit t)) (derive t)
derive (abs t) = abs (abs (derive t))
derive (const c ts) = deriveConst c (fit-terms ts) (derive-terms ts)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Incrementalization as term-to-term transformation (Fig. 4g).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Derive
{Base : Type.Structure}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open ChangeType.Structure Base ΔBase
-- Extension point: Incrementalization of fully applied primitives.
Structure : Set
Structure = ∀ {Γ Σ τ} →
Const Σ τ →
Terms (ΔContext Γ) Σ →
Terms (ΔContext Γ) (mapContext ΔType Σ) →
Term (ΔContext Γ) (ΔType τ)
module Structure (deriveConst : Structure) where
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
fit-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) Σ
fit-terms = weaken-terms Γ≼ΔΓ
-- In the paper, we transform "x" to "dx". Here, we work with
-- de Bruijn indices, so we have to manipulate the indices to
-- account for a bigger context after transformation.
deriveVar : ∀ {τ Γ} → Var Γ τ → Var (ΔContext Γ) (ΔType τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
derive-terms : ∀ {Σ Γ} → Terms Γ Σ → Terms (ΔContext Γ) (mapContext ΔType Σ)
derive : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) (ΔType τ)
derive-terms {∅} ∅ = ∅
derive-terms {τ • Σ} (t • ts) = derive t • derive-terms ts
-- We provide: Incrementalization of arbitrary terms.
derive (var x) = var (deriveVar x)
derive (app s t) = app (app (derive s) (fit t)) (derive t)
derive (abs t) = abs (abs (derive t))
derive (const c ts) = deriveConst c (fit-terms ts) (derive-terms ts)
|
Document P.C.Derive.
|
Document P.C.Derive.
Old-commit-hash: 962425cf9bb190e1b8fb2c400bd9765180081dd0
|
Agda
|
mit
|
inc-lc/ilc-agda
|
8d4599155dc64db203782bdd39087df1a8014331
|
Popl14/Change/Term.agda
|
Popl14/Change/Term.agda
|
module Popl14.Change.Term where
-- Terms Calculus Popl14
--
-- Contents
-- - Term constructors
-- - Weakening on terms
-- - `fit`: weaken a term to its ΔContext
-- - diff-term, apply-term and their syntactic sugars
open import Data.Integer
open import Popl14.Syntax.Type public
open import Popl14.Syntax.Term public
open import Popl14.Change.Type public
import Parametric.Change.Term Const ΔBase as ChangeTerm
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
-- Sugars for diff-term and apply-term
infixl 6 _⊕_ _⊝_
_⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ
_⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ)
t ⊕ Δt = app (app apply-term Δt) t
s ⊝ t = app (app diff-term s) t
apply-base : ChangeTerm.ApplyStructure
apply-base {base-int} =
abs₂ (λ Δx x → add x Δx)
apply-base {base-bag} =
abs₂ (λ Δx x → union x Δx)
apply-term {base ι} = apply-base {ι}
apply-term {σ ⇒ τ} =
let
Δf = var (that (that this))
f = var (that this)
x = var this
in
-- Δf f x
abs (abs (abs
(app f x ⊕ app (app Δf x) (x ⊝ x))))
diff-base : ChangeTerm.DiffStructure
diff-base {base-int} =
abs₂ (λ x y → add x (minus y))
diff-base {base-bag} =
abs₂ (λ x y → union x (negate y))
diff-term {base ι} = diff-base {ι}
diff-term {σ ⇒ τ} =
let
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
in
-- g f x Δx
abs (abs (abs (abs
(app g (x ⊕ Δx) ⊝ app f x))))
|
module Popl14.Change.Term where
-- Terms Calculus Popl14
--
-- Contents
-- - Term constructors
-- - Weakening on terms
-- - `fit`: weaken a term to its ΔContext
-- - diff-term, apply-term and their syntactic sugars
open import Data.Integer
open import Popl14.Syntax.Type public
open import Popl14.Syntax.Term public
open import Popl14.Change.Type public
import Parametric.Change.Term Const ΔBase as ChangeTerm
diff-base : ChangeTerm.DiffStructure
diff-base {base-int} = abs₂ (λ x y → add x (minus y))
diff-base {base-bag} = abs₂ (λ x y → union x (negate y))
apply-base : ChangeTerm.ApplyStructure
apply-base {base-int} = abs₂ (λ Δx x → add x Δx)
apply-base {base-bag} = abs₂ (λ Δx x → union x Δx)
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
-- Sugars for diff-term and apply-term
infixl 6 _⊕_ _⊝_
_⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ
_⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ)
t ⊕ Δt = app (app apply-term Δt) t
s ⊝ t = app (app diff-term s) t
apply-term {base ι} = apply-base {ι}
apply-term {σ ⇒ τ} =
let
Δf = var (that (that this))
f = var (that this)
x = var this
in
-- Δf f x
abs (abs (abs
(app f x ⊕ app (app Δf x) (x ⊝ x))))
diff-term {base ι} = diff-base {ι}
diff-term {σ ⇒ τ} =
let
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
in
-- g f x Δx
abs (abs (abs (abs
(app g (x ⊕ Δx) ⊝ app f x))))
|
Move (apply|diff)-base to the top of the module.
|
Move (apply|diff)-base to the top of the module.
This commit separates the POPL14-specific code from the
language-independent code in this module. The following commits will
remove the language-independent code.
Old-commit-hash: 245a7b618b2ac811d28d702089482e9aed8b8425
|
Agda
|
mit
|
inc-lc/ilc-agda
|
c68a9b8870e9eaca8c1a746f2ec67e2c84d97fd5
|
Crypto/JS/BigI/CyclicGroup.agda
|
Crypto/JS/BigI/CyclicGroup.agda
|
{-# OPTIONS --without-K #-}
open import Type.Eq
open import FFI.JS using (JS[_]; return; Bool; _++_; _>>_)
open import FFI.JS.Check using (check!)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Data.Two hiding (_==_)
open import Relation.Binary.PropositionalEquality
open import Algebra.Raw
open import Algebra.Group
-- TODO carry on a primality proof of p
module Crypto.JS.BigI.CyclicGroup (p : BigI) where
abstract
ℤ[_]★ : Set
ℤ[_]★ = BigI
private
ℤp★ : Set
ℤp★ = BigI
mod-p : BigI → ℤp★
mod-p x = mod x p
-- There are two ways to go from BigI to ℤp★: check and mod-p
-- Use check for untrusted input data and mod-p for internal
-- computation.
BigI▹ℤ[_]★ : BigI → JS[ ℤp★ ]
BigI▹ℤ[_]★ x =
-- Console.log "BigI▹ℤ[_]★" >>
check! "below modulus" (x <I p)
(λ _ → "Not below the modulus: p:" ++
toString p ++
" is less than x:" ++
toString x) >>
check! "strictcly positive" (x >I 0I)
(λ _ → "Should be strictly positive: " ++
toString x ++
" <= 0") >>
return x
repr : ℤp★ → BigI
repr x = x
1# : ℤp★
1# = 1I
1/_ : ℤp★ → ℤp★
1/ x = modInv x p
_^_ : ℤp★ → BigI → ℤp★
x ^ y = modPow x y p
_*_ _/_ : ℤp★ → ℤp★ → ℤp★
x * y = mod-p (multiply (repr x) (repr y))
x / y = x * 1/ y
instance
ℤ[_]★-Eq? : Eq? ℤp★
ℤ[_]★-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤp★ → ℤp★ → 𝟚
x ==' y = equals (repr x) (repr y)
postulate
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
prod : List ℤp★ → ℤp★
prod = foldr _*_ 1#
mon-ops : Monoid-Ops ℤp★
mon-ops = _*_ , 1#
grp-ops : Group-Ops ℤp★
grp-ops = mon-ops , 1/_
postulate
grp-struct : Group-Struct grp-ops
grp : Group ℤp★
grp = grp-ops , grp-struct
module grp = Group grp
-- -}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type.Eq
open import FFI.JS using (JS[_]; return; _++_; _>>_)
open import FFI.JS.Check using (check!)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Data.Two hiding (_==_)
open import Relation.Binary.PropositionalEquality
open import Algebra.Raw
open import Algebra.Group
-- TODO carry on a primality proof of p
module Crypto.JS.BigI.CyclicGroup (p : BigI) where
abstract
ℤ[_]★ : Set
ℤ[_]★ = BigI
private
ℤp★ : Set
ℤp★ = BigI
mod-p : BigI → ℤp★
mod-p x = mod x p
-- There are two ways to go from BigI to ℤp★: check and mod-p
-- Use check for untrusted input data and mod-p for internal
-- computation.
BigI▹ℤ[_]★ : BigI → JS[ ℤp★ ]
BigI▹ℤ[_]★ x =
-- Console.log "BigI▹ℤ[_]★" >>
check! "below modulus" (x <I p)
(λ _ → "Not below the modulus: p:" ++
toString p ++
" is less than x:" ++
toString x) >>
check! "strictcly positive" (x >I 0I)
(λ _ → "Should be strictly positive: " ++
toString x ++
" <= 0") >>
return x
repr : ℤp★ → BigI
repr x = x
1# : ℤp★
1# = 1I
1/_ : ℤp★ → ℤp★
1/ x = modInv x p
_^_ : ℤp★ → BigI → ℤp★
x ^ y = modPow x y p
_*_ _/_ : ℤp★ → ℤp★ → ℤp★
x * y = mod-p (multiply (repr x) (repr y))
x / y = x * 1/ y
instance
ℤ[_]★-Eq? : Eq? ℤp★
ℤ[_]★-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤp★ → ℤp★ → 𝟚
x ==' y = equals (repr x) (repr y)
postulate
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
prod : List ℤp★ → ℤp★
prod = foldr _*_ 1#
mon-ops : Monoid-Ops ℤp★
mon-ops = _*_ , 1#
grp-ops : Group-Ops ℤp★
grp-ops = mon-ops , 1/_
postulate
grp-struct : Group-Struct grp-ops
grp : Group ℤp★
grp = grp-ops , grp-struct
module grp = Group grp
-- -}
-- -}
-- -}
-- -}
-- -}
|
remove useless import
|
CyclicGroup: remove useless import
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
3a1143b9db99b9ec7e1431f08aab91f27cf9bd7a
|
Postulate/Extensionality.agda
|
Postulate/Extensionality.agda
|
module Postulate.Extensionality where
-- POSTULATE EXTENSIONALITY
--
-- Justification on Agda mailing list:
-- http://permalink.gmane.org/gmane.comp.lang.agda/2343
open import Relation.Binary.PropositionalEquality
postulate ext : ∀ {a b} → Extensionality a b
-- Convenience of using extensionality 3 times in a row
-- (using it twice in a row is moderately tolerable)
ext³ : ∀
{A : Set}
{B : A → Set}
{C : (a : A) → B a → Set }
{D : (a : A) → (b : B a) → C a b → Set}
{f g : (a : A) → (b : B a) → (c : C a b) → D a b c} →
((a : A) (b : B a) (c : C a b) → f a b c ≡ g a b c) → f ≡ g
ext³ fabc=gabc = ext (λ a → ext (λ b → ext (λ c → fabc=gabc a b c)))
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Postulate extensionality of functions.
--
-- Justification on Agda mailing list:
-- http://permalink.gmane.org/gmane.comp.lang.agda/2343
------------------------------------------------------------------------
module Postulate.Extensionality where
open import Relation.Binary.PropositionalEquality
postulate ext : ∀ {a b} → Extensionality a b
-- Convenience of using extensionality 3 times in a row
-- (using it twice in a row is moderately tolerable)
ext³ : ∀
{A : Set}
{B : A → Set}
{C : (a : A) → B a → Set }
{D : (a : A) → (b : B a) → C a b → Set}
{f g : (a : A) → (b : B a) → (c : C a b) → D a b c} →
((a : A) (b : B a) (c : C a b) → f a b c ≡ g a b c) → f ≡ g
ext³ fabc=gabc = ext (λ a → ext (λ b → ext (λ c → fabc=gabc a b c)))
|
Document Postulate.Extensionality regularly.
|
Document Postulate.Extensionality regularly.
Old-commit-hash: 35222e92088dbd2313ec7252e50cd68e85b340bf
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e7d0ffc6976dc3270e58e80326cf78dc7e9dcb2e
|
flat-funs.agda
|
flat-funs.agda
|
module flat-funs where
open import Data.Nat
import Level as L
open import composable
open import vcomp
record FlatFuns {t} (T : Set t) : Set (L.suc t) where
constructor mk
field
`⊤ : T
`Bit : T
_`×_ : T → T → T
_`^_ : T → ℕ → T
_`→_ : T → T → Set
`Vec : T → ℕ → T
`Vec A n = A `^ n
`Bits : ℕ → T
`Bits n = `Bit `^ n
infixr 2 _`×_
infixl 2 _`^_
infix 0 _`→_
record FlatFunsOps {t} {T : Set t} (♭Funs : FlatFuns T) : Set t where
constructor mk
open FlatFuns ♭Funs
field
idO : ∀ {A} → A `→ A
isComposable : Composable _`→_
isVComposable : VComposable _`×_ _`→_
-- Fanout
_&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C
fst : ∀ {A B} → A `× B `→ A
snd : ∀ {A B} → A `× B `→ B
open Composable isComposable
open VComposable isVComposable
<_,_> : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C
< f , g > = f &&& g
<_×_> : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D)
< f × g > = f *** g
open Composable isComposable public
open VComposable isVComposable public
open import Data.Unit using (⊤)
open import Data.Vec
open import Data.Bits
open import Data.Product
open import Function
fun♭Funs : FlatFuns Set
fun♭Funs = mk ⊤ Bit _×_ Vec (λ A B → A → B)
bitsFun♭Funs : FlatFuns ℕ
bitsFun♭Funs = mk 0 1 _+_ _*_ (λ i o → Bits i → Bits o)
fun♭Ops : FlatFunsOps fun♭Funs
fun♭Ops = mk id funComp funVComp _&&&_ proj₁ proj₂
where
_&&&_ : ∀ {A B C : Set} → (A → B) → (A → C) → A → B × C
(f &&& g) x = (f x , g x)
bitsFun♭Ops : FlatFunsOps bitsFun♭Funs
bitsFun♭Ops = mk id bitsFunComp bitsFunVComp _&&&_ (λ {A} → take A) (λ {A} → drop A)
where
open FlatFuns bitsFun♭Funs
_&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C
(f &&& g) x = (f x ++ g x)
×-♭Funs : ∀ {s t} {S : Set s} {T : Set t} → FlatFuns S → FlatFuns T → FlatFuns (S × T)
×-♭Funs funs-S funs-T = ?
where module S = FlatFuns funs-S
module T = FlatFuns funs-T
×-♭Ops : ∀ {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}
→ FlatFunsOps funs-S → FlatFunsOps funs-T
→ FlatFunsOps (×-♭Funs funs-S funs-T)
×-♭Ops ops-S ops-T = ?
where module S = FlatFunsOps ops-S
module T = FlatFunsOps ops-T
|
module flat-funs where
open import Data.Nat
open import Data.Bits using (Bits)
import Level as L
import Function as F
open import composable
open import vcomp
open import universe
record FlatFuns {t} (T : Set t) : Set (L.suc t) where
constructor mk
field
universe : Universe T
_`→_ : T → T → Set
infix 0 _`→_
open Universe universe public
record FlatFunsOps {t} {T : Set t} (♭Funs : FlatFuns T) : Set t where
constructor mk
open FlatFuns ♭Funs
field
idO : ∀ {A} → A `→ A
_>>>_ : ∀ {A B C} → (A `→ B) → (B `→ C) → (A `→ C)
_***_ : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D)
-- Fanout
_&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C
fst : ∀ {A B} → A `× B `→ A
snd : ∀ {A B} → A `× B `→ B
constBits : ∀ {n} → Bits n → `⊤ `→ `Bits n
<_,_> : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C
< f , g > = f &&& g
<_×_> : ∀ {A B C D} → (A `→ C) → (B `→ D) → (A `× B) `→ (C `× D)
< f × g > = f *** g
open import Data.Unit using (⊤)
open import Data.Vec
open import Data.Bits
open import Data.Fin using (Fin) renaming (_+_ to _+ᶠ_)
open import Data.Product renaming (zip to ×-zip; map to ×-map)
open import Function
-→- : Set → Set → Set
-→- A B = A → B
_→ᶠ_ : ℕ → ℕ → Set
_→ᶠ_ i o = Fin i → Fin o
mapArr : ∀ {s t} {S : Set s} {T : Set t} (F G : T → S)
→ (S → S → Set) → (T → T → Set)
mapArr F G _`→_ A B = F A `→ G B
fun♭Funs : FlatFuns Set
fun♭Funs = mk Set-U -→-
bitsFun♭Funs : FlatFuns ℕ
bitsFun♭Funs = mk Bits-U _→ᵇ_
finFun♭Funs : FlatFuns ℕ
finFun♭Funs = mk Fin-U _→ᶠ_
fun♭Ops : FlatFunsOps fun♭Funs
fun♭Ops = mk id (λ f g x → g (f x)) (λ f g → ×-map f g) _&&&_ proj₁ proj₂ const
where
_&&&_ : ∀ {A B C : Set} → (A → B) → (A → C) → A → B × C
(f &&& g) x = (f x , g x)
bitsFun♭Ops : FlatFunsOps bitsFun♭Funs
bitsFun♭Ops = mk id (λ f g → g ∘ f) (VComposable._***_ bitsFunVComp) _&&&_ (λ {A} → take A)
(λ {A} → drop A) (λ xs _ → xs ++ [] {- ugly? -})
where
open FlatFuns bitsFun♭Funs
_&&&_ : ∀ {A B C} → (A `→ B) → (A `→ C) → A `→ B `× C
(f &&& g) x = (f x ++ g x)
×-♭Funs : ∀ {s t} {S : Set s} {T : Set t} → FlatFuns S → FlatFuns T → FlatFuns (S × T)
×-♭Funs funs-S funs-T = mk (×-U S.universe T.universe)
(λ { (A₀ , A₁) (B₀ , B₁) → (A₀ S.`→ B₀) × (A₁ T.`→ B₁) })
where module S = FlatFuns funs-S
module T = FlatFuns funs-T
×⊤-♭Funs : ∀ {s} {S : Set s} → FlatFuns S → FlatFuns ⊤ → FlatFuns S
×⊤-♭Funs funs-S funs-T = mk S.universe
(λ A B → (A S.`→ B) × (_ T.`→ _))
where module S = FlatFuns funs-S
module T = FlatFuns funs-T
×-♭Ops : ∀ {s t} {S : Set s} {T : Set t} {funs-S : FlatFuns S} {funs-T : FlatFuns T}
→ FlatFunsOps funs-S → FlatFunsOps funs-T
→ FlatFunsOps (×-♭Funs funs-S funs-T)
×-♭Ops ops-S ops-T = mk (S.idO , T.idO) (×-zip S._>>>_ T._>>>_) (×-zip S._***_ T._***_)
(×-zip S._&&&_ T._&&&_) (S.fst , T.fst) (S.snd , T.snd)
(S.constBits &&& T.constBits)
where module S = FlatFunsOps ops-S
module T = FlatFunsOps ops-T
open FlatFunsOps fun♭Ops
×⊤-♭Ops : ∀ {s} {S : Set s} {funs-S : FlatFuns S} {funs-⊤ : FlatFuns ⊤}
→ FlatFunsOps funs-S → FlatFunsOps funs-⊤
→ FlatFunsOps (×⊤-♭Funs funs-S funs-⊤)
×⊤-♭Ops ops-S ops-⊤ = mk (S.idO , T.idO) (×-zip S._>>>_ T._>>>_) (×-zip S._***_ T._***_)
(×-zip S._&&&_ T._&&&_) (S.fst , T.fst) (S.snd , T.snd)
(S.constBits &&& T.constBits)
where module S = FlatFunsOps ops-S
module T = FlatFunsOps ops-⊤
open FlatFunsOps fun♭Ops
constFuns : Set → FlatFuns ⊤
constFuns A = mk ⊤-U (λ _ _ → A)
timeOps : FlatFunsOps (constFuns ℕ)
timeOps = mk 0 _+_ _⊔_ _⊔_ 0 0 (const 0)
spaceOps : FlatFunsOps (constFuns ℕ)
spaceOps = mk 0 _+_ _+_ _+_ 0 0 (λ {n} _ → n)
time×spaceOps : FlatFunsOps (constFuns (ℕ × ℕ))
time×spaceOps = ×⊤-♭Ops timeOps spaceOps
|
put the type interface in the universe module
|
flat-funs: put the type interface in the universe module
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
785dddce82b3dc294f8d52a3a8a96b64366b1ac4
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
_≙_ : ∀ dx dy → Set a
dx ≙ dy = x ⊞ dx ≡ x ⊞ dy
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = refl
≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-symm ≙ = sym ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma
where
open ≡-Reasoning
open import Postulate.Extensionality
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
module Structure where
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
_≙_ : ∀ dx dy → Set a
dx ≙ dy = x ⊞ dx ≡ x ⊞ dy
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = refl
≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-symm ≙ = sym ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
|
Remove more
|
Remove more
No Structure in Base.
Old-commit-hash: 81cee315c1ade9b842f0400fbc2bd215a189e8dc
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2aa331ecc397f402c621e7b4b53886eb5ca53e91
|
Parametric/Change/Term.agda
|
Parametric/Change/Term.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Constant : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Constant
open ChangeType.Structure Base ΔBase
open import Data.Product
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
∀ {τ Γ} →
Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-diff-apply {base ι} = diff-base , apply-base
lift-diff-apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ})
_⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t
_⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t
_⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff :
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
lift-diff = λ {τ Γ} →
proj₁ (lift-diff-apply {τ} {Γ})
lift-apply :
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply = λ {τ Γ} →
proj₂ (lift-diff-apply {τ} {Γ})
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Constant : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Constant
open ChangeType.Structure Base ΔBase
open import Data.Product
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
∀ {τ Γ} →
Term Γ (τ ⇒ τ ⇒ ΔType τ) × Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-diff-apply {base ι} = diff-base , apply-base
lift-diff-apply {σ ⇒ τ} =
let
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply {τ} {Γ})
_⊝σ_ = λ {Γ} s t → app₂ (diffσ {Γ}) s t
_⊝τ_ = λ {Γ} s t → app₂ (diffτ {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (applyσ {Γ}) Δt t
_⊕τ_ = λ {Γ} t Δt → app₂ (applyτ {Γ}) Δt t
in
abs₄ (λ g f x Δx → app f (x ⊕σ Δx) ⊝τ app g x)
,
abs₃ (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y))
lift-diff :
∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
lift-diff = λ {τ Γ} →
proj₁ (lift-diff-apply {τ} {Γ})
lift-apply :
∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
lift-apply = λ {τ Γ} →
proj₂ (lift-diff-apply {τ} {Γ})
|
Use HOAS-enabled helpers for nested abstractions.
|
Use HOAS-enabled helpers for nested abstractions.
Old-commit-hash: f5006b448dcc30a8ff103cf5de501e945e14faa1
|
Agda
|
mit
|
inc-lc/ilc-agda
|
24ce7b62d29582982f0f0c2694ebd10a473113aa
|
Game/IND-CCA2.agda
|
Game/IND-CCA2.agda
|
{-# OPTIONS --without-K #-}
open import Type
open import Data.Bit
open import Data.Maybe
open import Data.Product
open import Data.Unit
open import Data.Nat.NP
open import Rat
open import Explore.Type
open import Explore.Explorable
open import Explore.Product
open Operators
open import Relation.Binary.PropositionalEquality
module Game.IND-CCA2
(PubKey : ★)
(SecKey : ★)
(Message : ★)
(CipherText : ★)
-- randomness supply for, encryption, key-generation, adversary, adversary state
(Rₑ Rₖ Rₐ : ★)
(KeyGen : Rₖ → PubKey × SecKey)
(Enc : PubKey → Message → Rₑ → CipherText)
(Dec : SecKey → CipherText → Message)
where
open import Game.CCA-Common Message CipherText
Adv : ★
Adv = Rₐ → PubKey → Strategy ((Message × Message) × (CipherText → Strategy Bit))
{-
Valid-Adv : Adv → Set
Valid-Adv adv = ∀ {rₐ rₓ pk c} → Valid (λ x → x ≢ c) {!!}
-}
R : ★
R = Rₐ × Rₖ × Rₑ
Game : ★
Game = Adv → R → Bit
⅁ : Bit → Game
⅁ b m (rₐ , rₖ , rₑ) with KeyGen rₖ
... | pk , sk = b′ where
open Eval Dec sk
ev = eval (m rₐ pk)
mb = proj (proj₁ ev) b
d = proj₂ ev
c = Enc pk mb rₑ
b′ = eval (d c)
module Advantage
(μₑ : Explore₀ Rₑ)
(μₖ : Explore₀ Rₖ)
(μₐ : Explore₀ Rₐ)
where
μR : Explore₀ R
μR = μₐ ×ᵉ μₖ ×ᵉ μₑ
module μR = FromExplore₀ μR
run : Bit → Adv → ℕ
run b adv = μR.count (⅁ b adv)
Advantage : Adv → ℕ
Advantage adv = dist (run 0b adv) (run 1b adv)
Advantageℚ : Adv → ℚ
Advantageℚ adv = Advantage adv / μR.Card
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type
open import Data.Bit
open import Data.Maybe
open import Data.Product
open import Data.Unit
open import Data.Nat.NP
--open import Rat
open import Explore.Type
open import Explore.Explorable
open import Explore.Product
open Operators
open import Relation.Binary.PropositionalEquality
module Game.IND-CCA2
(PubKey : ★)
(SecKey : ★)
(Message : ★)
(CipherText : ★)
-- randomness supply for, encryption, key-generation, adversary, adversary state
(Rₑ Rₖ Rₐ : ★)
(KeyGen : Rₖ → PubKey × SecKey)
(Enc : PubKey → Message → Rₑ → CipherText)
(Dec : SecKey → CipherText → Message)
where
open import Game.CCA-Common Message CipherText
Adv : ★
Adv = Rₐ → PubKey → Strategy ((Message × Message) × (CipherText → Strategy Bit))
{-
Valid-Adv : Adv → Set
Valid-Adv adv = ∀ {rₐ rₓ pk c} → Valid (λ x → x ≢ c) {!!}
-}
R : ★
R = Rₐ × Rₖ × Rₑ
Game : ★
Game = Adv → R → Bit
⅁ : Bit → Game
⅁ b m (rₐ , rₖ , rₑ) with KeyGen rₖ
... | pk , sk = b′ where
open Eval Dec sk
ev = eval (m rₐ pk)
mb = proj (proj₁ ev) b
d = proj₂ ev
c = Enc pk mb rₑ
b′ = eval (d c)
module Advantage
(μₑ : Explore₀ Rₑ)
(μₖ : Explore₀ Rₖ)
(μₐ : Explore₀ Rₐ)
where
μR : Explore₀ R
μR = μₐ ×ᵉ μₖ ×ᵉ μₑ
module μR = FromExplore₀ μR
run : Bit → Adv → ℕ
run b adv = μR.count (⅁ b adv)
Advantage : Adv → ℕ
Advantage adv = dist (run 0b adv) (run 1b adv)
--Advantageℚ : Adv → ℚ
--Advantageℚ adv = Advantage adv / μR.Card
-- -}
-- -}
-- -}
-- -}
|
Hide the Rat code
|
IND-CCA2: Hide the Rat code
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
16f8a95227efdacdb93599ca7b8ddfd847c2e47a
|
Parametric/Denotation/Value.agda
|
Parametric/Denotation/Value.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Value domains for languages described in Plotkin style
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
module Parametric.Denotation.Value
(Base : Type.Structure)
where
open import Base.Denotation.Notation
open Type.Structure Base
Structure : Set₁
Structure = Base → Set
module Structure (⟦_⟧Base : Structure) where
⟦_⟧Type : Type -> Set
⟦ base ι ⟧Type = ⟦ ι ⟧Base
⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
open import Base.Denotation.Environment Type ⟦_⟧Type public
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Values for standard evaluation (Def. 3.1 and 3.2, Fig. 4c and 4f).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
module Parametric.Denotation.Value
(Base : Type.Structure)
where
open import Base.Denotation.Notation
open Type.Structure Base
-- Extension point: Values for base types.
Structure : Set₁
Structure = Base → Set
module Structure (⟦_⟧Base : Structure) where
-- We provide: Values for arbitrary types.
⟦_⟧Type : Type -> Set
⟦ base ι ⟧Type = ⟦ ι ⟧Base
⟦ σ ⇒ τ ⟧Type = ⟦ σ ⟧Type → ⟦ τ ⟧Type
-- This means: Overload ⟦_⟧ to mean ⟦_⟧Type.
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- We also provide: Environments of such values.
open import Base.Denotation.Environment Type ⟦_⟧Type public
|
Document P.D.Value.
|
Document P.D.Value.
Old-commit-hash: bfaab88fbd6365ff0b9046dd999ccd1362c526d6
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a2f7958b6a6e7245442d1e150092aacdc7c8707d
|
examples/Propositional.agda
|
examples/Propositional.agda
|
module Propositional where
open import Data.Nat
open import Relation.Binary.PropositionalEquality
open import Holes.Term using (⌞_⌟)
open import Holes.Cong.Propositional
open ≡-Reasoning
--------------------------------------------------------------------------------
-- Some easy lemmas
--------------------------------------------------------------------------------
+-zero : ∀ a → a + zero ≡ a
+-zero zero = refl
+-zero (suc a) rewrite +-zero a = refl
+-suc : ∀ a b → a + suc b ≡ suc (a + b)
+-suc zero b = refl
+-suc (suc a) b rewrite +-suc a b = refl
+-assoc : ∀ a b c → a + b + c ≡ a + (b + c)
+-assoc zero b c = refl
+-assoc (suc a) b c rewrite +-assoc a b c = refl
--------------------------------------------------------------------------------
-- Proofs by equational reasoning
--------------------------------------------------------------------------------
-- commutativity of addition proved traditionally
+-comm₁ : ∀ a b → a + b ≡ b + a
+-comm₁ zero b = sym (+-zero b)
+-comm₁ (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong suc (+-comm₁ a b) ⟩
suc (b + a) ≡⟨ sym (+-suc b a) ⟩
b + suc a ∎
-- commutativity of addition proved with holes
+-comm : ∀ a b → a + b ≡ b + a
+-comm zero b = sym (+-zero b)
+-comm (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong! (+-comm a b) ⟩
suc (b + a) ≡⟨ cong! (+-suc b a) ⟩
b + suc a ∎
-- distributivity of multiplication over addition proved traditionally
*-distrib-+₁ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+₁ zero b c = refl
*-distrib-+₁ (suc a) b c =
b + c + a * (b + c) ≡⟨ cong (λ h → b + c + h) (*-distrib-+₁ a b c) ⟩
(b + c) + (a * b + a * c) ≡⟨ sym (+-assoc (b + c) (a * b) (a * c)) ⟩
((b + c) + a * b) + a * c ≡⟨ cong (λ h → h + a * c) (+-assoc b c (a * b)) ⟩
(b + (c + a * b)) + a * c ≡⟨ cong (λ h → (b + h) + a * c) (+-comm c (a * b)) ⟩
(b + (a * b + c)) + a * c ≡⟨ cong (λ h → h + a * c) (sym (+-assoc b (a * b) c)) ⟩
((b + a * b) + c) + a * c ≡⟨ +-assoc (b + a * b) c (a * c) ⟩
(b + a * b) + (c + a * c)
∎
-- distributivity of multiplication over addition proved with holes
*-distrib-+ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+ zero b c = refl
*-distrib-+ (suc a) b c =
b + c + ⌞ a * (b + c) ⌟ ≡⟨ cong! (*-distrib-+ a b c) ⟩
(b + c) + (a * b + a * c) ≡⟨ cong! (+-assoc (b + c) (a * b) (a * c)) ⟩
⌞ (b + c) + a * b ⌟ + a * c ≡⟨ cong! (+-assoc b c (a * b)) ⟩
(b + ⌞ c + a * b ⌟) + a * c ≡⟨ cong! (+-comm c (a * b)) ⟩
⌞ b + (a * b + c) ⌟ + a * c ≡⟨ cong! (+-assoc b (a * b) c) ⟩
⌞ ((b + a * b) + c) + a * c ⌟ ≡⟨ cong! (+-assoc (b + a * b) c (a * c)) ⟩
(b + a * b) + (c + a * c)
∎
|
module Propositional where
open import Data.Nat
open import Relation.Binary.PropositionalEquality
open import Holes.Term using (⌞_⌟)
open import Holes.Cong.Propositional
open ≡-Reasoning
--------------------------------------------------------------------------------
-- Some easy lemmas
--------------------------------------------------------------------------------
+-zero : ∀ a → a + zero ≡ a
+-zero zero = refl
+-zero (suc a) rewrite +-zero a = refl
+-suc : ∀ a b → a + suc b ≡ suc (a + b)
+-suc zero b = refl
+-suc (suc a) b rewrite +-suc a b = refl
+-assoc : ∀ a b c → a + b + c ≡ a + (b + c)
+-assoc zero b c = refl
+-assoc (suc a) b c rewrite +-assoc a b c = refl
--------------------------------------------------------------------------------
-- Proofs by equational reasoning
--------------------------------------------------------------------------------
-- commutativity of addition proved traditionally
+-comm₁ : ∀ a b → a + b ≡ b + a
+-comm₁ zero b = sym (+-zero b)
+-comm₁ (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong suc (+-comm₁ a b) ⟩
suc (b + a) ≡⟨ sym (+-suc b a) ⟩
b + suc a ∎
-- commutativity of addition proved with holes
+-comm : ∀ a b → a + b ≡ b + a
+-comm zero b = sym (+-zero b)
+-comm (suc a) b =
suc ⌞ a + b ⌟ ≡⟨ cong! (+-comm a b) ⟩
suc (b + a) ≡⟨ cong! (+-suc b a) ⟩
b + suc a ∎
-- distributivity of multiplication over addition proved traditionally
*-distrib-+₁ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+₁ zero b c = refl
*-distrib-+₁ (suc a) b c =
b + c + a * (b + c) ≡⟨ cong (λ h → b + c + h) (*-distrib-+₁ a b c) ⟩
(b + c) + (a * b + a * c) ≡⟨ sym (+-assoc (b + c) (a * b) (a * c)) ⟩
((b + c) + a * b) + a * c ≡⟨ cong (λ h → h + a * c) (+-assoc b c (a * b)) ⟩
(b + (c + a * b)) + a * c ≡⟨ cong (λ h → (b + h) + a * c) (+-comm c (a * b)) ⟩
(b + (a * b + c)) + a * c ≡⟨ cong (λ h → h + a * c) (sym (+-assoc b (a * b) c)) ⟩
((b + a * b) + c) + a * c ≡⟨ +-assoc (b + a * b) c (a * c) ⟩
(b + a * b) + (c + a * c)
∎
-- distributivity of multiplication over addition proved with holes
*-distrib-+ : ∀ a b c → a * (b + c) ≡ a * b + a * c
*-distrib-+ zero b c = refl
*-distrib-+ (suc a) b c =
b + c + ⌞ a * (b + c) ⌟ ≡⟨ cong! (*-distrib-+ a b c) ⟩
⌞ (b + c) + (a * b + a * c) ⌟ ≡⟨ cong! (+-assoc (b + c) (a * b) (a * c)) ⟩
⌞ (b + c) + a * b ⌟ + a * c ≡⟨ cong! (+-assoc b c (a * b)) ⟩
(b + ⌞ c + a * b ⌟) + a * c ≡⟨ cong! (+-comm c (a * b)) ⟩
⌞ b + (a * b + c) ⌟ + a * c ≡⟨ cong! (+-assoc b (a * b) c) ⟩
⌞ ((b + a * b) + c) + a * c ⌟ ≡⟨ cong! (+-assoc (b + a * b) c (a * c)) ⟩
(b + a * b) + (c + a * c)
∎
|
Add a hole in the examples that's strictly unnecessary
|
Add a hole in the examples that's strictly unnecessary
... but potentially confusing if it isn't there
|
Agda
|
mit
|
bch29/agda-holes
|
48d1be2bb4bb61634b15fbd0cff064c02be1f4f9
|
Base/Syntax/Context.agda
|
Base/Syntax/Context.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Variables and contexts
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
--
------------------------------------------------------------------------
module Base.Syntax.Context
(Type : Set)
where
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- TYPING CONTEXTS
-- Syntax
import Data.List as List
open List public
using ()
renaming
( [] to ∅ ; _∷_ to _•_
; map to mapContext
)
Context : Set
Context = List.List Type
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ
-- WEAKENING
-- CONTEXT PREFIXES
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂)
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- SUBCONTEXTS
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
module Subcontexts where
infix 4 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
weaken-var (keep τ • ≼₁) this = this
weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x)
weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Variables and contexts
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
--
------------------------------------------------------------------------
module Base.Syntax.Context
(Type : Set)
where
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- TYPING CONTEXTS
-- Syntax
import Data.List as List
open List public
using ()
renaming
( [] to ∅ ; _∷_ to _•_
; map to mapContext
)
Context : Set
Context = List.List Type
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ
-- WEAKENING
-- CONTEXT PREFIXES
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂)
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- SUBCONTEXTS
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
module Subcontexts where
infix 4 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
weaken-var (keep τ • ≼₁) this = this
weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x)
weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
Comment on handling of contexts
|
Comment on handling of contexts
Old-commit-hash: 0bfc0dfdc024921016f8df0d8252aa20f5f0f8d6
|
Agda
|
mit
|
inc-lc/ilc-agda
|
7ec8bed818d248b395b4b0b2c2d2f21361f940bc
|
Thesis/LangOps.agda
|
Thesis/LangOps.agda
|
module Thesis.LangOps where
open import Thesis.Lang
open import Thesis.Changes
open import Thesis.LangChanges
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
oplusτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ ⇒ τ)
ominusτo : ∀ {Γ} τ → Term Γ (τ ⇒ τ ⇒ Δt τ)
onilτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ)
onilτo τ = abs (app₂ (ominusτo τ) (var this) (var this))
-- Do NOT try to read this, such terms are write-only. But the behavior is
-- specified to be oplusτ-equiv and ominusτ-equiv.
oplusτo (σ ⇒ τ) = abs (abs (abs
(app₂ (oplusτo τ)
(app (var (that (that this))) (var this))
(app₂ (var (that this)) (var this) (app (onilτo σ) (var this))))))
oplusτo unit = abs (abs (const unit))
oplusτo int = const plus
oplusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (oplusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (oplusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
oplusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const linj) (app₂ (oplusτo σ) (var (that (that this))) (var this))))
(abs (app (const linj) (var (that (that this)))))))
(abs (var this))))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const rinj) (var (that (that this)))))
(abs (app (const rinj) (app₂ (oplusτo τ) (var (that (that this))) (var this))))))
(abs (var this))))))
ominusτo (σ ⇒ τ) = abs (abs (abs (abs (app₂ (ominusτo τ)
(app (var (that (that (that this)))) (app₂ (oplusτo σ) (var (that this)) (var this)))
(app (var (that (that this))) (var (that this)))))))
ominusτo unit = abs (abs (const unit))
ominusτo int = const minus
ominusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (ominusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (ominusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
ominusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app (const linj) (app (const linj) (app₂ (ominusτo σ) (var (that this)) (var this)))))
(abs (app (const rinj) (var (that (that (that this))))))))
(abs (app₃ (const match) (var (that this))
(abs (app (const rinj) (var (that (that (that this))))))
(abs (app (const linj) (app (const rinj) (app₂ (ominusτo τ) (var (that this)) (var this)))))))))
oplusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ a da → ⟦ oplusτo τ ⟧Term ρ a da ≡ a ⊕ da
ominusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ b a → ⟦ ominusτo τ ⟧Term ρ b a ≡ b ⊝ a
oplusτ-equiv-ext : ∀ τ Γ → ⟦ oplusτo {Γ} τ ⟧Term ≡ λ ρ → _⊕_
oplusτ-equiv-ext τ _ = ext³ (λ ρ a da → oplusτ-equiv _ ρ τ a da)
ominusτ-equiv-ext : ∀ τ Γ → ⟦ ominusτo {Γ} τ ⟧Term ≡ λ ρ → _⊝_
ominusτ-equiv-ext τ _ = ext³ (λ ρ a da → ominusτ-equiv _ ρ τ a da)
oplusτ-equiv Γ ρ (σ ⇒ τ) f df = ext (λ a → lemma a)
where
module _ (a : ⟦ σ ⟧Type) where
ρ′ = a • df • f • ρ
ρ′′ = a • ρ′
lemma : ⟦ oplusτo τ ⟧Term ρ′ (f a)
(df a (⟦ ominusτo σ ⟧Term ρ′′ a a))
≡ f a ⊕ df a (nil a)
lemma
rewrite ominusτ-equiv _ ρ′′ σ a a
| oplusτ-equiv _ ρ′ τ (f a) (df a (nil a))
= refl
oplusτ-equiv Γ ρ unit tt tt = refl
oplusτ-equiv Γ ρ int a da = refl
oplusτ-equiv Γ ρ (pair σ τ) (a , b) (da , db)
rewrite oplusτ-equiv _ ((da , db) • (a , b) • ρ) σ a da
| oplusτ-equiv _ ((da , db) • (a , b) • ρ) τ b db
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₁ dx))
rewrite oplusτ-equiv-ext σ (Δt σ • sum (Δt σ) (Δt τ) • σ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₂ dy)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₁ dx)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₂ dy))
rewrite oplusτ-equiv-ext τ (Δt τ • sum (Δt σ) (Δt τ) • τ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁) = refl
ominusτ-equiv Γ ρ (σ ⇒ τ) g f = ext (λ a → ext (lemma a))
where
module _ (a : ⟦ σ ⟧Type) (da : Chτ σ) where
ρ′ = da • a • f • g • ρ
lemma : ⟦ ominusτo τ ⟧Term (da • a • f • g • ρ)
(g (⟦ oplusτo σ ⟧Term (da • a • f • g • ρ) a da)) (f a)
≡ g (a ⊕ da) ⊝ f a
lemma
rewrite oplusτ-equiv _ ρ′ σ a da
| ominusτ-equiv _ ρ′ τ (g (a ⊕ da)) (f a) = refl
ominusτ-equiv Γ ρ unit tt tt = refl
ominusτ-equiv Γ ρ int b a = refl
ominusτ-equiv Γ ρ (pair σ τ) (a2 , b2) (a1 , b1)
rewrite ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) σ a2 a1
| ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) τ b2 b1
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ x₁)
rewrite ominusτ-equiv-ext σ (σ • σ • sum σ τ • sum σ τ • Γ)
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ x) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁)
rewrite ominusτ-equiv-ext τ (τ • τ • sum σ τ • sum σ τ • Γ)
= refl
|
module Thesis.LangOps where
open import Thesis.Lang
open import Thesis.Changes
open import Thesis.LangChanges
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
oplusτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ ⇒ τ)
ominusτo : ∀ {Γ} τ → Term Γ (τ ⇒ τ ⇒ Δt τ)
onilτo : ∀ {Γ} τ → Term Γ (τ ⇒ Δt τ)
onilτo τ = abs (app₂ (ominusτo τ) (var this) (var this))
-- Do NOT try to read this, such terms are write-only. But the behavior is
-- specified to be oplusτ-equiv and ominusτ-equiv.
oplusτo (σ ⇒ τ) = abs (abs (abs
(app₂ (oplusτo τ)
(app (var (that (that this))) (var this))
(app₂ (var (that this)) (var this) (app (onilτo σ) (var this))))))
oplusτo unit = abs (abs (const unit))
oplusτo int = const plus
oplusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (oplusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (oplusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
oplusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const linj) (app₂ (oplusτo σ) (var (that (that this))) (var this))))
(abs (app (const linj) (var (that (that this)))))))
(abs (var this))))
(abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var this)
(abs (app (const rinj) (var (that (that this)))))
(abs (app (const rinj) (app₂ (oplusτo τ) (var (that (that this))) (var this))))))
(abs (var this))))))
ominusτo (σ ⇒ τ) = abs (abs (abs (abs (app₂ (ominusτo τ)
(app (var (that (that (that this)))) (app₂ (oplusτo σ) (var (that this)) (var this)))
(app (var (that (that this))) (var (that this)))))))
ominusτo unit = abs (abs (const unit))
ominusτo int = const minus
ominusτo (pair σ τ) = abs (abs (app₂ (const cons)
(app₂ (ominusτo σ) (app (const fst) (var (that this))) (app (const fst) (var this)))
(app₂ (ominusτo τ) (app (const snd) (var (that this))) (app (const snd) (var this)))))
ominusτo (sum σ τ) = abs (abs (app₃ (const match) (var (that this))
(abs (app₃ (const match) (var (that this))
(abs (app (const linj) (app (const linj) (app₂ (ominusτo σ) (var (that this)) (var this)))))
(abs (app (const rinj) (var (that (that (that this))))))))
(abs (app₃ (const match) (var (that this))
(abs (app (const rinj) (var (that (that (that this))))))
(abs (app (const linj) (app (const rinj) (app₂ (ominusτo τ) (var (that this)) (var this)))))))))
oplusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ a da → ⟦ oplusτo τ ⟧Term ρ a da ≡ a ⊕ da
ominusτ-equiv : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ b a → ⟦ ominusτo τ ⟧Term ρ b a ≡ b ⊝ a
oplusτ-equiv-ext : ∀ τ Γ → ⟦ oplusτo {Γ} τ ⟧Term ≡ λ ρ → _⊕_
oplusτ-equiv-ext τ _ = ext³ (λ ρ a da → oplusτ-equiv _ ρ τ a da)
ominusτ-equiv-ext : ∀ τ Γ → ⟦ ominusτo {Γ} τ ⟧Term ≡ λ ρ → _⊝_
ominusτ-equiv-ext τ _ = ext³ (λ ρ a da → ominusτ-equiv _ ρ τ a da)
oplusτ-equiv Γ ρ (σ ⇒ τ) f df = ext (λ a → lemma a)
where
module _ (a : ⟦ σ ⟧Type) where
ρ′ = a • df • f • ρ
ρ′′ = a • ρ′
lemma : ⟦ oplusτo τ ⟧Term ρ′ (f a)
(df a (⟦ ominusτo σ ⟧Term ρ′′ a a))
≡ f a ⊕ df a (nil a)
lemma
rewrite ominusτ-equiv _ ρ′′ σ a a
| oplusτ-equiv _ ρ′ τ (f a) (df a (nil a))
= refl
oplusτ-equiv Γ ρ unit tt tt = refl
oplusτ-equiv Γ ρ int a da = refl
oplusτ-equiv Γ ρ (pair σ τ) (a , b) (da , db)
rewrite oplusτ-equiv _ ((da , db) • (a , b) • ρ) σ a da
| oplusτ-equiv _ ((da , db) • (a , b) • ρ) τ b db
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₁ dx))
rewrite oplusτ-equiv-ext σ (Δt σ • sum (Δt σ) (Δt τ) • σ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ (inj₂ dy)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₁ dx)) = refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ (inj₂ dy))
rewrite oplusτ-equiv-ext τ (Δt τ • sum (Δt σ) (Δt τ) • τ • Δt (sum σ τ) • sum σ τ • Γ)
= refl
oplusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁) = refl
ominusτ-equiv Γ ρ (σ ⇒ τ) g f = ext (λ a → ext (lemma a))
where
module _ (a : ⟦ σ ⟧Type) (da : Chτ σ) where
ρ′ = da • a • f • g • ρ
lemma : ⟦ ominusτo τ ⟧Term (da • a • f • g • ρ)
(g (⟦ oplusτo σ ⟧Term (da • a • f • g • ρ) a da)) (f a)
≡ g (a ⊕ da) ⊝ f a
lemma
rewrite oplusτ-equiv _ ρ′ σ a da
| ominusτ-equiv _ ρ′ τ (g (a ⊕ da)) (f a) = refl
ominusτ-equiv Γ ρ unit tt tt = refl
ominusτ-equiv Γ ρ int b a = refl
ominusτ-equiv Γ ρ (pair σ τ) (a2 , b2) (a1 , b1)
rewrite ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) σ a2 a1
| ominusτ-equiv _ ((a1 , b1) • (a2 , b2) • ρ) τ b2 b1
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₁ x₁)
rewrite ominusτ-equiv-ext σ (σ • σ • sum σ τ • sum σ τ • Γ)
= refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₁ x) (inj₂ y) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₁ x) = refl
ominusτ-equiv Γ ρ (sum σ τ) (inj₂ y) (inj₂ y₁)
rewrite ominusτ-equiv-ext τ (τ • τ • sum σ τ • sum σ τ • Γ)
= refl
-- Proved these lemmas to show them in thesis.
oplusτ-equiv-term : ∀ dΓ (dρ : ⟦ dΓ ⟧Context) τ t dt →
⟦ app₂ (oplusτo τ) t dt ⟧Term dρ ≡ ⟦ t ⟧Term dρ ⊕ ⟦ dt ⟧Term dρ
oplusτ-equiv-term dΓ dρ τ t dt = oplusτ-equiv dΓ dρ τ (⟦ t ⟧Term dρ) (⟦ dt ⟧Term dρ)
ominusτ-equiv-term : ∀ Γ (ρ : ⟦ Γ ⟧Context) τ t2 t1 →
⟦ app₂ (ominusτo τ) t2 t1 ⟧Term ρ ≡ ⟦ t2 ⟧Term ρ ⊝ ⟦ t1 ⟧Term ρ
ominusτ-equiv-term Γ ρ τ t2 t1 = ominusτ-equiv Γ ρ τ (⟦ t2 ⟧Term ρ) (⟦ t1 ⟧Term ρ)
|
Add lemmas I present in thesis
|
Add lemmas I present in thesis
|
Agda
|
mit
|
inc-lc/ilc-agda
|
965133f11d3863769cb8e636eff326a49bc7321b
|
Crypto/JS/BigI/CyclicGroup.agda
|
Crypto/JS/BigI/CyclicGroup.agda
|
{-# OPTIONS --without-K #-}
open import Type.Eq
open import FFI.JS using (Bool; trace-call; _++_)
open import FFI.JS.Check
renaming (check to check?)
--renaming (warn-check to check?)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Data.Two hiding (_==_)
open import Relation.Binary.PropositionalEquality
open import Algebra.Raw
open import Algebra.Group
-- TODO carry on a primality proof of p
module Crypto.JS.BigI.CyclicGroup (p : BigI) where
abstract
-- ℤp*
𝔾 : Set
𝔾 = BigI
private
mod-p : BigI → 𝔾
mod-p x = mod x p
-- There are two ways to go from BigI to 𝔾: check and mod-p
-- Use check for untrusted input data and mod-p for internal
-- computation.
BigI▹𝔾 : BigI → 𝔾
BigI▹𝔾 = -- trace-call "BigI▹𝔾 "
λ x →
(check? (x <I p)
(λ _ → "Not below the modulus: p:" ++ toString p ++ " is less than x:" ++ toString x)
(check? (x >I 0I)
(λ _ → "Should be strictly positive: " ++ toString x ++ " <= 0") x))
check-non-zero : 𝔾 → BigI
check-non-zero = -- trace-call "check-non-zero "
λ x → check? (x >I 0I) (λ _ → "Should be non zero") x
repr : 𝔾 → BigI
repr x = x
1# : 𝔾
1# = 1I
1/_ : 𝔾 → 𝔾
1/ x = modInv (check-non-zero x) p
_^_ : 𝔾 → BigI → 𝔾
x ^ y = modPow x y p
_*_ _/_ : 𝔾 → 𝔾 → 𝔾
x * y = mod-p (multiply (repr x) (repr y))
x / y = x * 1/ y
instance
𝔾-Eq? : Eq? 𝔾
𝔾-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : 𝔾 → 𝔾 → 𝟚
x ==' y = equals (repr x) (repr y)
postulate
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
prod : List 𝔾 → 𝔾
prod = foldr _*_ 1#
mon-ops : Monoid-Ops 𝔾
mon-ops = _*_ , 1#
grp-ops : Group-Ops 𝔾
grp-ops = mon-ops , 1/_
postulate
grp-struct : Group-Struct grp-ops
grp : Group 𝔾
grp = grp-ops , grp-struct
module grp = Group grp
-- -}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type.Eq
open import FFI.JS using (Bool; trace-call; _++_)
open import FFI.JS.Check
renaming (check to check?)
--renaming (warn-check to check?)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Data.Two hiding (_==_)
open import Relation.Binary.PropositionalEquality
open import Algebra.Raw
open import Algebra.Group
-- TODO carry on a primality proof of p
module Crypto.JS.BigI.CyclicGroup (p : BigI) where
abstract
ℤ[_]★ : Set
ℤ[_]★ = BigI
private
ℤp★ : Set
ℤp★ = BigI
mod-p : BigI → ℤp★
mod-p x = mod x p
-- There are two ways to go from BigI to ℤp★: check and mod-p
-- Use check for untrusted input data and mod-p for internal
-- computation.
BigI▹ℤ[_]★ : BigI → ℤp★
BigI▹ℤ[_]★ = -- trace-call "BigI▹ℤ[_]★ "
λ x →
(check? (x <I p)
(λ _ → "Not below the modulus: p:" ++ toString p ++ " is less than x:" ++ toString x)
(check? (x >I 0I)
(λ _ → "Should be strictly positive: " ++ toString x ++ " <= 0") x))
check-non-zero : ℤp★ → BigI
check-non-zero = -- trace-call "check-non-zero "
λ x → check? (x >I 0I) (λ _ → "Should be non zero") x
repr : ℤp★ → BigI
repr x = x
1# : ℤp★
1# = 1I
1/_ : ℤp★ → ℤp★
1/ x = modInv (check-non-zero x) p
_^_ : ℤp★ → BigI → ℤp★
x ^ y = modPow x y p
_*_ _/_ : ℤp★ → ℤp★ → ℤp★
x * y = mod-p (multiply (repr x) (repr y))
x / y = x * 1/ y
instance
ℤ[_]★-Eq? : Eq? ℤp★
ℤ[_]★-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤp★ → ℤp★ → 𝟚
x ==' y = equals (repr x) (repr y)
postulate
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
prod : List ℤp★ → ℤp★
prod = foldr _*_ 1#
mon-ops : Monoid-Ops ℤp★
mon-ops = _*_ , 1#
grp-ops : Group-Ops ℤp★
grp-ops = mon-ops , 1/_
postulate
grp-struct : Group-Struct grp-ops
grp : Group ℤp★
grp = grp-ops , grp-struct
module grp = Group grp
-- -}
-- -}
-- -}
-- -}
-- -}
|
rename 𝔾 as ℤ[ p ]★
|
CyclicGroup: rename 𝔾 as ℤ[ p ]★
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
da7ed128a0220380c3109913af5f58760ad241d3
|
Base/Denotation/Notation.agda
|
Base/Denotation/Notation.agda
|
module Base.Denotation.Notation where
-- OVERLOADING ⟦_⟧ NOTATION
--
-- This module defines a general mechanism for overloading the
-- ⟦_⟧ notation, using Agda’s instance arguments.
open import Level
record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where
constructor
meaning
field
{Semantics} : Set ℓ
⟨_⟩⟦_⟧ : Syntax → Semantics
open Meaning {{...}} public
renaming (⟨_⟩⟦_⟧ to ⟦_⟧)
open Meaning public
using (⟨_⟩⟦_⟧)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Overloading ⟦_⟧ notation
--
-- This module defines a general mechanism for overloading the
-- ⟦_⟧ notation, using Agda’s instance arguments.
------------------------------------------------------------------------
module Base.Denotation.Notation where
open import Level
record Meaning (Syntax : Set) {ℓ : Level} : Set (suc ℓ) where
constructor
meaning
field
{Semantics} : Set ℓ
⟨_⟩⟦_⟧ : Syntax → Semantics
open Meaning {{...}} public
renaming (⟨_⟩⟦_⟧ to ⟦_⟧)
open Meaning public
using (⟨_⟩⟦_⟧)
|
Improve commentary in Base.Denotation.Notation.
|
Improve commentary in Base.Denotation.Notation.
Old-commit-hash: f51d5958c73259a9120fc8a2ad20854bb12f8870
|
Agda
|
mit
|
inc-lc/ilc-agda
|
66a643a5678be6f131477803e19ed0cb52159556
|
agda/Equality.agda
|
agda/Equality.agda
|
-- This is an implementation of the equality type for Sets. Agda's
-- standard equality is more powerful. The main idea here is to
-- illustrate the equality type.
module Equality where
open import Level
-- The equality of two elements of type A. The type a ≡ b is a family
-- of types which captures the statement of equality in A. Not all of
-- the types are inhabited as in general not all elements are equal
-- here. The only type that are inhabited are a ≡ a by the element
-- that we call definition. This is called refl (for reflection in
-- agda).
data _≡_ {ℓ} {A : Set ℓ} : (a b : A) → Set ℓ where
definition : {x : A} → x ≡ x
-- ≡ is a symmetric relation.
sym : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → b ≡ a
sym definition = definition
-- ≡ is a transitive relation.
trans : ∀ {ℓ} {A : Set ℓ} {a b c : A} → a ≡ b → b ≡ c → a ≡ c
trans pAB definition = pAB
-- trans definition pBC = pBC -- alternate proof of transitivity.
-- Congruence. If we apply f to equals the result are also equal.
cong : ∀{ℓ₀ ℓ₁} {A : Set ℓ₀} {B : Set ℓ₁}
{a₀ a₁ : A} → (f : A → B) → a₀ ≡ a₁ → f a₀ ≡ f a₁
cong f definition = definition
-- Pretty way of doing equational reasoning. If we want to prove a₀ ≡
-- b through an intermediate set of equations use this. The general
-- form will look like.
--
-- begin a ≈ a₀ by p₀
-- ≈ a₁ by p₁
-- ...
-- ≈ b by p
-- ∎
begin : ∀{ℓ} {A : Set ℓ} (a : A) → a ≡ a
_≈_by_ : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → (c : A) → b ≡ c → a ≡ c
_∎ : ∀{ℓ} {A : Set ℓ} (a : A) → A
begin a = definition
aEb ≈ c by bEc = trans aEb bEc
x ∎ = x
infixl 1 _≈_by_
infixl 1 _≡_
|
-- This is an implementation of the equality type for Sets. Agda's
-- standard equality is more powerful. The main idea here is to
-- illustrate the equality type.
module Equality where
open import Level
-- The equality of two elements of type A. The type a ≡ b is a family
-- of types which captures the statement of equality in A. Not all of
-- the types are inhabited as in general not all elements are equal
-- here. The only type that are inhabited are a ≡ a by the element
-- that we call definition. This is called refl (for reflection in
-- agda).
data _≡_ {ℓ} {A : Set ℓ} : (a b : A) → Set ℓ where
refl : {x : A} → x ≡ x
-- ≡ is a symmetric relation.
sym : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → b ≡ a
sym refl = refl
-- ≡ is a transitive relation.
trans : ∀ {ℓ} {A : Set ℓ} {a b c : A} → a ≡ b → b ≡ c → a ≡ c
trans pAB refl = pAB
-- trans refl pBC = pBC -- alternate proof of transitivity.
-- Congruence. If we apply f to equals the result are also equal.
cong : ∀{ℓ₀ ℓ₁} {A : Set ℓ₀} {B : Set ℓ₁}
{a₀ a₁ : A} → (f : A → B) → a₀ ≡ a₁ → f a₀ ≡ f a₁
cong f refl = refl
-- Pretty way of doing equational reasoning. If we want to prove a₀ ≡
-- b through an intermediate set of equations use this. The general
-- form will look like.
--
-- begin a ≈ a₀ by p₀
-- ≈ a₁ by p₁
-- ...
-- ≈ b by p
-- ∎
begin : ∀{ℓ} {A : Set ℓ} (a : A) → a ≡ a
_≈_by_ : ∀{ℓ} {A : Set ℓ} {a b : A} → a ≡ b → (c : A) → b ≡ c → a ≡ c
_∎ : ∀{ℓ} {A : Set ℓ} (a : A) → A
begin a = refl
aEb ≈ c by bEc = trans aEb bEc
x ∎ = x
infixl 1 _≈_by_
infixl 1 _≡_
|
use the standard terminology refl.
|
use the standard terminology refl.
|
Agda
|
unlicense
|
piyush-kurur/sample-code
|
5303d7901af55944be2ec4695ee17bbd01624291
|
Theorem/CongApp.agda
|
Theorem/CongApp.agda
|
module Theorem.CongApp where
-- Theorem CongApp.
-- If f ≡ g and x ≡ y, then (f x) ≡ (g y).
open import Relation.Binary.PropositionalEquality public
infixl 0 _⟨$⟩_
_⟨$⟩_ : ∀ {a b} {A : Set a} {B : Set b}
{f g : A → B} {x y : A} →
f ≡ g → x ≡ y → f x ≡ g y
_⟨$⟩_ = cong₂ (λ x y → x y)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Congruence of application.
--
-- If f ≡ g and x ≡ y, then (f x) ≡ (g y).
------------------------------------------------------------------------
module Theorem.CongApp where
open import Relation.Binary.PropositionalEquality public
infixl 0 _⟨$⟩_
_⟨$⟩_ : ∀ {a b} {A : Set a} {B : Set b}
{f g : A → B} {x y : A} →
f ≡ g → x ≡ y → f x ≡ g y
_⟨$⟩_ = cong₂ (λ x y → x y)
|
Document Theorem.CongApp regularly.
|
Document Theorem.CongApp regularly.
Old-commit-hash: f16f3e3d0b8e59230bd86a0c223af263a8db75e8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b87eb75ad51669de831d389ef1a78b415b31a18d
|
lib/Data/Nat/Distance.agda
|
lib/Data/Nat/Distance.agda
|
open import Data.Nat.NP
open import Data.Nat.Properties as Props
open import Data.Product
open import Relation.Binary.PropositionalEquality.NP
module Data.Nat.Distance where
dist : ℕ → ℕ → ℕ
dist zero y = y
dist x zero = x
dist (suc x) (suc y) = dist x y
dist-refl : ∀ x → dist x x ≡ 0
dist-refl zero = idp
dist-refl (suc x) rewrite dist-refl x = idp
dist-0≡id : ∀ x → dist 0 x ≡ x
dist-0≡id _ = idp
dist-x-x+y≡y : ∀ x y → dist x (x + y) ≡ y
dist-x-x+y≡y zero y = idp
dist-x-x+y≡y (suc x) y = dist-x-x+y≡y x y
dist-comm : ∀ x y → dist x y ≡ dist y x
dist-comm zero zero = idp
dist-comm zero (suc y) = idp
dist-comm (suc x) zero = idp
dist-comm (suc x) (suc y) = dist-comm x y
dist-x+ : ∀ x y z → dist (x + y) (x + z) ≡ dist y z
dist-x+ zero y z = idp
dist-x+ (suc x) y z = dist-x+ x y z
dist-2* : ∀ x y → dist (2* x) (2* y) ≡ 2* dist x y
dist-2* zero y = idp
dist-2* (suc x) zero = idp
dist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x | +-assoc-comm y 1 y = dist-2* x y
dist-asym-def : ∀ {x y} → x ≤ y → x + dist x y ≡ y
dist-asym-def z≤n = idp
dist-asym-def (s≤s pf) = ap suc (dist-asym-def pf)
dist-sym-wlog : ∀ (f : ℕ → ℕ) → (∀ x k → dist (f x) (f (x + k)) ≡ f k) → ∀ x y → dist (f x) (f y) ≡ f (dist x y)
dist-sym-wlog f pf x y with compare x y
dist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)
... | q rewrite +-assoc-comm x 1 k | q | ! +-assoc-comm x 1 k | dist-x-x+y≡y x (suc k) = idp
dist-sym-wlog f pf .y y | equal .y with pf y 0
... | q rewrite ℕ°.+-comm y 0 | dist-refl y = q
dist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)
... | q rewrite +-assoc-comm 1 y k | dist-comm (y + suc k) y | dist-x-x+y≡y y (suc k) | dist-comm (f (y + suc k)) (f y) = q
dist-x* : ∀ x y z → dist (x * y) (x * z) ≡ x * dist y z
dist-x* x = dist-sym-wlog (_*_ x) pf
where pf : ∀ a k → dist (x * a) (x * (a + k)) ≡ x * k
pf a k rewrite proj₁ ℕ°.distrib x a k = dist-x-x+y≡y (x * a) _
dist-2^* : ∀ x y z → dist ⟨2^ x * y ⟩ ⟨2^ x * z ⟩ ≡ ⟨2^ x * dist y z ⟩
dist-2^* x = dist-sym-wlog (2^⟨ x ⟩*) pf
where pf : ∀ a k → dist ⟨2^ x * a ⟩ ⟨2^ x * (a + k) ⟩ ≡ ⟨2^ x * k ⟩
pf a k rewrite 2^*-distrib x a k = dist-x-x+y≡y ⟨2^ x * a ⟩ ⟨2^ x * k ⟩
dist-bounded : ∀ {x y f} → x ≤ f → y ≤ f → dist x y ≤ f
dist-bounded z≤n y≤f = y≤f
dist-bounded (s≤s x≤f) z≤n = s≤s x≤f
dist-bounded (s≤s x≤f) (s≤s y≤f) = ≤-step (dist-bounded x≤f y≤f)
-- Triangular inequality
dist-sum : ∀ x y z → dist x z ≤ dist x y + dist y z
dist-sum zero zero z = ℕ≤.refl
dist-sum zero (suc y) zero = z≤n
dist-sum zero (suc y) (suc z) = s≤s (dist-sum zero y z)
dist-sum (suc x) zero zero = s≤s (ℕ≤.reflexive (ℕ°.+-comm 0 x))
dist-sum (suc x) (suc y) zero
rewrite ℕ°.+-comm (dist x y) (suc y)
| dist-comm x y = s≤s (dist-sum zero y x)
dist-sum (suc x) zero (suc z) = dist-sum x zero z
∙≤ ℕ≤.reflexive (ap₂ _+_ (dist-comm x 0) idp)
∙≤ ≤-step (ℕ≤.refl {x} +-mono ≤-step ℕ≤.refl)
dist-sum (suc x) (suc y) (suc z) = dist-sum x y z
dist≡0 : ∀ x y → dist x y ≡ 0 → x ≡ y
dist≡0 zero zero d≡0 = refl
dist≡0 zero (suc y) ()
dist≡0 (suc x) zero ()
dist≡0 (suc x) (suc y) d≡0 = ap suc (dist≡0 x y d≡0)
∸-dist : ∀ x y → y ≤ x → x ∸ y ≡ dist x y
∸-dist x .0 z≤n = ! dist-comm x 0
∸-dist ._ ._ (s≤s y≤x) = ∸-dist _ _ y≤x
{-
post--ulate
dist-≤ : ∀ x y → dist x y ≤ x
dist-mono₁ : ∀ x y z t → x ≤ y → dist z t ≤ dist (x + z) (y + t)
-}
-- Haskell
-- let dist x y = abs (x - y)
-- quickCheck (forAll (replicateM 3 (choose (0,100))) (\ [x,y,z] -> dist (x * y) (x * z) == x * dist y z))
|
open import Function.NP
open import Data.Nat.NP
open import Data.Nat.Properties as Props
open import Data.Nat.Properties.Simple
open import Data.Product.NP
open import Relation.Binary.PropositionalEquality.NP
open import Relation.Binary
open import Relation.Nullary
module Data.Nat.Distance where
{-
dist : ℕ → ℕ → ℕ
dist zero y = y
dist x zero = x
dist (suc x) (suc y) = dist x y
-}
-- `symIter` from Conor McBride
-- Since symIter is essentially `dist` with callbacks, I
-- include it here.
module Generic {a}{A : Set a}(z : ℕ → A)(s : A → A) where
dist : ℕ → ℕ → A
dist zero y = z y
dist x zero = z x
dist (suc x) (suc y) = s (dist x y)
dist-comm : ∀ x y → dist x y ≡ dist y x
dist-comm zero zero = idp
dist-comm zero (suc y) = idp
dist-comm (suc x) zero = idp
dist-comm (suc x) (suc y) = ap s (dist-comm x y)
dist-0≡id : ∀ x → dist 0 x ≡ z x
dist-0≡id _ = idp
dist-0≡id′ : ∀ x → dist x 0 ≡ z x
dist-0≡id′ zero = idp
dist-0≡id′ (suc _) = idp
open Generic id id public
module Add where
open Generic id (suc ∘ suc)
public
renaming ( dist to _+'_
; dist-0≡id to +'0-identity
; dist-0≡id′ to 0+'-identity
; dist-comm to +'-comm
)
+-spec : ∀ x y → x +' y ≡ x + y
+-spec zero _ = idp
+-spec (suc x) zero = ap suc (ℕ°.+-comm 0 x)
+-spec (suc x) (suc y) = ap suc (ap suc (+-spec x y) ∙ ! +-suc x y)
module AddMult where
open Generic (λ x → x , 0)
(λ { (s , p) → (2 + s , 1 + s + p) })
public
renaming ( dist to _+*_
; dist-0≡id to +*0-identity
; dist-0≡id′ to 0+*-identity
; dist-comm to +*-comm
)
_+'_ : ℕ → ℕ → ℕ
x +' y = fst (x +* y)
_*'_ : ℕ → ℕ → ℕ
x *' y = snd (x +* y)
+*-spec : ∀ x y → (x +' y ≡ x + y) × (x *' y ≡ x * y)
+*-spec zero _ = idp , idp
+*-spec (suc x) zero = ap suc (ℕ°.+-comm 0 x) , ℕ°.*-comm 0 x
+*-spec (suc x) (suc y) = ap suc p+ , ap suc p*
where p = +*-spec x y
p+ = ap suc (fst p) ∙ ! +-suc x y
p* = ap₂ _+_ (fst p) (snd p)
∙ +-assoc x y (x * y)
∙ ap (λ z → x + (y + z)) (ℕ°.*-comm x y)
∙ ! +-assoc-comm y x (y * x)
∙ ap (_+_ y) (ℕ°.*-comm (suc y) x)
module Min where
open Generic (const zero) suc
public
renaming ( dist to _⊓'_
; dist-0≡id to ⊓'0-zero
; dist-0≡id′ to 0⊓'-zero
; dist-comm to ⊓'-comm
)
⊓'-spec : ∀ x y → x ⊓' y ≡ x ⊓ y
⊓'-spec zero zero = idp
⊓'-spec zero (suc y) = idp
⊓'-spec (suc x) zero = idp
⊓'-spec (suc x) (suc y) = ap suc (⊓'-spec x y)
module Max where
open Generic id suc
public
renaming ( dist to _⊔'_
; dist-0≡id to ⊔'0-identity
; dist-0≡id′ to 0⊔'-identity
; dist-comm to ⊔'-comm
)
⊔'-spec : ∀ x y → x ⊔' y ≡ x ⊔ y
⊔'-spec zero zero = idp
⊔'-spec zero (suc y) = idp
⊔'-spec (suc x) zero = idp
⊔'-spec (suc x) (suc y) = ap suc (⊔'-spec x y)
dist-refl : ∀ x → dist x x ≡ 0
dist-refl zero = idp
dist-refl (suc x) rewrite dist-refl x = idp
dist-x-x+y≡y : ∀ x y → dist x (x + y) ≡ y
dist-x-x+y≡y zero y = idp
dist-x-x+y≡y (suc x) y = dist-x-x+y≡y x y
dist-x+ : ∀ x y z → dist (x + y) (x + z) ≡ dist y z
dist-x+ zero y z = idp
dist-x+ (suc x) y z = dist-x+ x y z
dist-2* : ∀ x y → dist (2* x) (2* y) ≡ 2* dist x y
dist-2* zero y = idp
dist-2* (suc x) zero = idp
dist-2* (suc x) (suc y) rewrite +-assoc-comm x 1 x | +-assoc-comm y 1 y = dist-2* x y
dist-asym-def : ∀ {x y} → x ≤ y → x + dist x y ≡ y
dist-asym-def z≤n = idp
dist-asym-def (s≤s pf) = ap suc (dist-asym-def pf)
dist-sym-wlog : ∀ (f : ℕ → ℕ) → (∀ x k → dist (f x) (f (x + k)) ≡ f k) → ∀ x y → dist (f x) (f y) ≡ f (dist x y)
dist-sym-wlog f pf x y with compare x y
dist-sym-wlog f pf x .(suc (x + k)) | less .x k with pf x (suc k)
... | q rewrite +-assoc-comm x 1 k | q | ! +-assoc-comm x 1 k | dist-x-x+y≡y x (suc k) = idp
dist-sym-wlog f pf .y y | equal .y with pf y 0
... | q rewrite ℕ°.+-comm y 0 | dist-refl y = q
dist-sym-wlog f pf .(suc (y + k)) y | greater .y k with pf y (suc k)
... | q rewrite +-assoc-comm 1 y k | dist-comm (y + suc k) y | dist-x-x+y≡y y (suc k) | dist-comm (f (y + suc k)) (f y) = q
dist-x* : ∀ x y z → dist (x * y) (x * z) ≡ x * dist y z
dist-x* x = dist-sym-wlog (_*_ x) pf
where pf : ∀ a k → dist (x * a) (x * (a + k)) ≡ x * k
pf a k rewrite fst ℕ°.distrib x a k = dist-x-x+y≡y (x * a) _
dist-2^* : ∀ x y z → dist ⟨2^ x * y ⟩ ⟨2^ x * z ⟩ ≡ ⟨2^ x * dist y z ⟩
dist-2^* x = dist-sym-wlog (2^⟨ x ⟩*) pf
where pf : ∀ a k → dist ⟨2^ x * a ⟩ ⟨2^ x * (a + k) ⟩ ≡ ⟨2^ x * k ⟩
pf a k rewrite 2^*-distrib x a k = dist-x-x+y≡y ⟨2^ x * a ⟩ ⟨2^ x * k ⟩
dist-bounded : ∀ {x y f} → x ≤ f → y ≤ f → dist x y ≤ f
dist-bounded z≤n y≤f = y≤f
dist-bounded (s≤s x≤f) z≤n = s≤s x≤f
dist-bounded (s≤s x≤f) (s≤s y≤f) = ≤-step (dist-bounded x≤f y≤f)
-- Triangular inequality
dist-sum : ∀ x y z → dist x z ≤ dist x y + dist y z
dist-sum zero zero z = ℕ≤.refl
dist-sum zero (suc y) zero = z≤n
dist-sum zero (suc y) (suc z) = s≤s (dist-sum zero y z)
dist-sum (suc x) zero zero = s≤s (ℕ≤.reflexive (ℕ°.+-comm 0 x))
dist-sum (suc x) (suc y) zero
rewrite ℕ°.+-comm (dist x y) (suc y)
| dist-comm x y = s≤s (dist-sum zero y x)
dist-sum (suc x) zero (suc z) = dist-sum x zero z
∙≤ ℕ≤.reflexive (ap₂ _+_ (dist-comm x 0) idp)
∙≤ ≤-step (ℕ≤.refl {x} +-mono ≤-step ℕ≤.refl)
dist-sum (suc x) (suc y) (suc z) = dist-sum x y z
dist≡0 : ∀ x y → dist x y ≡ 0 → x ≡ y
dist≡0 zero zero d≡0 = idp
dist≡0 zero (suc y) ()
dist≡0 (suc x) zero ()
dist≡0 (suc x) (suc y) d≡0 = ap suc (dist≡0 x y d≡0)
∸-≤-dist : ∀ x y → x ∸ y ≤ dist x y
∸-≤-dist zero zero = z≤n
∸-≤-dist zero (suc y) = z≤n
∸-≤-dist (suc x) zero = ℕ≤.refl
∸-≤-dist (suc x) (suc y) = ∸-≤-dist x y
∸-dist : ∀ x y → y ≤ x → x ∸ y ≡ dist x y
∸-dist x .0 z≤n = ! dist-comm x 0
∸-dist ._ ._ (s≤s y≤x) = ∸-dist _ _ y≤x
dist-min-max : ∀ x y → dist x y ≡ (x ⊔ y) ∸ (x ⊓ y)
dist-min-max zero zero = idp
dist-min-max zero (suc y) = idp
dist-min-max (suc x) zero = idp
dist-min-max (suc x) (suc y) = dist-min-max x y
-- -}
-- -}
-- -}
-- -}
-- -}
|
add the `symIter` from Conor and some lemmas
|
Dist: add the `symIter` from Conor and some lemmas
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
1dd4fe7a8a920498ea83e46ad2707edca7f6f4bf
|
Syntax/Type/Plotkin.agda
|
Syntax/Type/Plotkin.agda
|
module Syntax.Type.Plotkin where
-- Types for language description à la Plotkin (LCF as PL)
--
-- Given base types, we build function types.
infixr 5 _⇒_
data Type (B : Set {- of base types -}) : Set where
base : (ι : B) → Type B
_⇒_ : (σ : Type B) → (τ : Type B) → Type B
-- Lift (Δ : B → Type B) to (Δtype : Type B → Type B)
-- according to Δ (σ ⇒ τ) = σ ⇒ Δ σ ⇒ Δ τ
lift-Δtype : ∀ {B} → (B → Type B) → (Type B → Type B)
lift-Δtype f (base ι) = f ι
lift-Δtype f (σ ⇒ τ) = let Δ = lift-Δtype f in σ ⇒ Δ σ ⇒ Δ τ
-- Note: the above is *not* a monadic bind.
--
-- Proof. base` is the most straightforward `return` of the
-- functor `Type`.
--
-- return : B → Type B
-- return = base
--
-- Let
--
-- m : Type B
-- m = base κ ⇒ base ι
--
-- then
--
-- m >>= return = lift-Δtype return m
-- = base κ ⇒ base κ ⇒ base ι
--
-- violating the second monadic law, m >>= return ≡ m. ∎
open import Function
-- Variant of lift-Δtype for (Δ : B → B).
lift-Δtype₀ : ∀ {B} → (B → B) → (Type B → Type B)
lift-Δtype₀ f = lift-Δtype $ base ∘ f
-- This has a similar type to the type of `fmap`,
-- and `base` has a similar type to the type of `return`.
--
-- Similarly, for collections map can be defined from flatMap,
-- like lift-Δtype₀ can be defined in terms of lift-Δtype.
|
module Syntax.Type.Plotkin where
-- Types for language description à la Plotkin (LCF as PL)
--
-- Given base types, we build function types.
open import Function
infixr 5 _⇒_
data Type (B : Set {- of base types -}) : Set where
base : (ι : B) → Type B
_⇒_ : (σ : Type B) → (τ : Type B) → Type B
-- Lift (Δ : B → Type B) to (Δtype : Type B → Type B)
-- according to Δ (σ ⇒ τ) = σ ⇒ Δ σ ⇒ Δ τ
lift-Δtype : ∀ {B} → (B → Type B) → (Type B → Type B)
lift-Δtype f (base ι) = f ι
lift-Δtype f (σ ⇒ τ) = let Δ = lift-Δtype f in σ ⇒ Δ σ ⇒ Δ τ
-- Note: the above is *not* a monadic bind.
--
-- Proof. base` is the most straightforward `return` of the
-- functor `Type`.
--
-- return : B → Type B
-- return = base
--
-- Let
--
-- m : Type B
-- m = base κ ⇒ base ι
--
-- then
--
-- m >>= return = lift-Δtype return m
-- = base κ ⇒ base κ ⇒ base ι
--
-- violating the second monadic law, m >>= return ≡ m. ∎
-- Variant of lift-Δtype for (Δ : B → B).
lift-Δtype₀ : ∀ {B} → (B → B) → (Type B → Type B)
lift-Δtype₀ f = lift-Δtype $ base ∘ f
-- This has a similar type to the type of `fmap`,
-- and `base` has a similar type to the type of `return`.
--
-- Similarly, for collections map can be defined from flatMap,
-- like lift-Δtype₀ can be defined in terms of lift-Δtype.
|
move import to top of file in Syntax.Type.Plotkin
|
Agda: move import to top of file in Syntax.Type.Plotkin
Old-commit-hash: 0d789d2318a5215ac4ba2f68286e7db5826593ec
|
Agda
|
mit
|
inc-lc/ilc-agda
|
fa1e5e6ba14f3f7038a0a46052c8f81d12ed2e9d
|
Base/Change/Equivalence/Base.agda
|
Base/Change/Equivalence/Base.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence - base definitions
------------------------------------------------------------------------
module Base.Change.Equivalence.Base where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence - base definitions
------------------------------------------------------------------------
module Base.Change.Equivalence.Base where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record (a Haskell "newtype")
-- instead of a "type synonym".
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
|
Clarify Agda comment
|
Clarify Agda comment
Old-commit-hash: e00f9f0bb80cb86372669d674900a9abd43b7ef4
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e2d49c81f464ab9449818f964e0078660f4315da
|
Syntactic/Contexts.agda
|
Syntactic/Contexts.agda
|
module Syntactic.Contexts
(Type : Set)
where
-- CONTEXTS
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Specialized congruence rules
⟨∅⟩ : ∅ ≡ ∅
⟨∅⟩ = refl
_⟨•⟩_ : ∀ {τ₁ τ₂ Γ₁ Γ₂} → τ₁ ≡ τ₂ → Γ₁ ≡ Γ₂ → τ₁ • Γ₁ ≡ τ₂ • Γ₂
_⟨•⟩_ = cong₂ _•_
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- WEAKENING
-- CONTEXT PREFIXES
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂)
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- SUBCONTEXTS
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
module Subcontexts where
infix 8 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
lift (keep τ • ≼₁) this = this
lift (keep τ • ≼₁) (that x) = that (lift ≼₁ x)
lift (drop τ • ≼₁) x = that (lift ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
module Syntactic.Contexts
(Type : Set)
where
-- CONTEXTS
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- TYPING CONTEXTS
-- Syntax
data Context : Set where
∅ : Context
_•_ : (τ : Type) (Γ : Context) → Context
infixr 9 _•_
-- Specialized congruence rules
⟨∅⟩ : ∅ ≡ ∅
⟨∅⟩ = refl
_⟨•⟩_ : ∀ {τ₁ τ₂ Γ₁ Γ₂} → τ₁ ≡ τ₂ → Γ₁ ≡ Γ₂ → τ₁ • Γ₁ ≡ τ₂ • Γ₂
_⟨•⟩_ = cong₂ _•_
-- VARIABLES
-- Syntax
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ τ τ′} → (x : Var Γ τ) → Var (τ′ • Γ) τ
-- WEAKENING
-- CONTEXT PREFIXES
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
∅ ⋎ Γ₂ = Γ₂
(τ • Γ₁) ⋎ Γ₂ = τ • (Γ₁ ⋎ Γ₂)
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- SUBCONTEXTS
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
module Subcontexts where
infix 8 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
lift (keep τ • ≼₁) this = this
lift (keep τ • ≼₁) (that x) = that (lift ≼₁ x)
lift (drop τ • ≼₁) x = that (lift ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
add lemma about syntactic contexts
|
Agda: add lemma about syntactic contexts
Old-commit-hash: 3613bba9a32778eaaa8f9d22419c23a8505be346
|
Agda
|
mit
|
inc-lc/ilc-agda
|
0ecf1536814a880e8cdbe60b6eef1ff1106504bf
|
Parametric/Change/Correctness.agda
|
Parametric/Change/Correctness.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Validity as Validity
import Parametric.Change.Specification as Specification
import Parametric.Change.Type as ChangeType
import Parametric.Change.Term as ChangeTerm
import Parametric.Change.Value as ChangeValue
import Parametric.Change.Evaluation as ChangeEvaluation
import Parametric.Change.Derive as Derive
import Parametric.Change.Implementation as Implementation
module Parametric.Change.Correctness
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ΔBase : ChangeType.Structure Base)
(diff-base : ChangeTerm.DiffStructure Const ΔBase)
(apply-base : ChangeTerm.ApplyStructure Const ΔBase)
(⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase)
(⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase)
(meaning-⊕-base : ChangeEvaluation.ApplyStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(meaning-⊝-base : ChangeEvaluation.DiffStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(validity-structure : Validity.Structure ⟦_⟧Base)
(specification-structure : Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure)
(derive-const : Derive.Structure Const ΔBase)
(implementation-structure : Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase
validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open Validity.Structure ⟦_⟧Base validity-structure
open Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure
open ChangeType.Structure Base ΔBase
open ChangeTerm.Structure Const ΔBase diff-base apply-base
open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧
open ChangeEvaluation.Structure
⟦_⟧Base ⟦_⟧Const ΔBase
apply-base diff-base
⟦apply-base⟧ ⟦diff-base⟧
meaning-⊕-base meaning-⊝-base
open Derive.Structure Const ΔBase derive-const
open Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure
-- The denotational properties of the `derive` transformation.
-- In particular, the main theorem about it producing the correct
-- incremental behavior.
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
Structure : Set
Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
(ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) →
⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′)
module Structure (derive-const-correct : Structure) where
deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′)
deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′
deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′
-- That `derive t` implements ⟦ t ⟧Δ
derive-correct : ∀ {τ Γ} (t : Term Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′)
derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))
derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅
derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ =
derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′
derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ =
derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′
(derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′)
derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ =
deriveVar-correct x ρ dρ ρ′ dρ≈ρ′
derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′
= subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′)
(derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′
(⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′))
derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ =
λ w dw w′ dw≈w′ →
derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′)
-- Our main theorem, as we used to state it in the paper.
main-theorem : ∀ {σ τ}
{f : Term ∅ (σ ⇒ τ)} {x : Term ∅ σ} {y : Term ∅ σ}
→ ⟦ app f y ⟧
≡ ⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) (y ⊝ x) ⟧
main-theorem {σ} {τ} {f} {x} {y} =
let
h = ⟦ f ⟧ ∅
Δh = ⟦ f ⟧Δ ∅ ∅
Δh′ = ⟦ derive f ⟧ ∅
v = ⟦ x ⟧ ∅
u = ⟦ y ⟧ ∅
Δoutput-term = app (app (derive f) x) (y ⊝ x)
in
ext {A = ⟦ ∅ ⟧Context} (λ { ∅ →
begin
h u
≡⟨ cong h (sym (update-diff₍ σ ₎ u v)) ⟩
h (v ⊞₍ σ ₎ (u ⊟₍ σ ₎ v))
≡⟨ corollary-closed {σ} {τ} f v (u ⊟₍ σ ₎ v) ⟩
h v ⊞₍ τ ₎ call-change {σ} {τ} Δh v (u ⊟₍ σ ₎ v)
≡⟨ carry-over {τ}
(call-change {σ} {τ} Δh v (u ⊟₍ σ ₎ v))
(derive-correct f
∅ ∅ ∅ ∅ v (u ⊟₍ σ ₎ v) (u ⟦⊝₍ σ ₎⟧ v) (u⊟v≈u⊝v {σ} {u} {v})) ⟩
h v ⟦⊕₍ τ ₎⟧ Δh′ v (u ⟦⊝₍ σ ₎⟧ v)
≡⟨ trans
(cong (λ hole → h v ⟦⊕₍ τ ₎⟧ Δh′ v hole) (meaning-⊝ {σ} {s = y} {x}))
(meaning-⊕ {t = app f x} {Δt = Δoutput-term}) ⟩
⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) (y ⊝ x) ⟧ ∅
∎}) where open ≡-Reasoning
-- A corollary, closer to what we state in the paper.
main-theorem-coroll : ∀ {σ τ}
{f : Term ∅ (σ ⇒ τ)} {x : Term ∅ σ} {dx : Term ∅ (ΔType σ)}
→ ⟦ app f (x ⊕₍ σ ₎ dx) ⟧
≡ ⟦ app f x ⊕₍ τ ₎ app (app (derive f) x) ((x ⊕₍ σ ₎ dx) ⊝ x) ⟧
main-theorem-coroll {σ} {τ} {f} {x} {dx} = main-theorem {σ} {τ} {f} {x} {x ⊕₍ σ ₎ dx}
-- For the statement in the paper, we'd need to talk about valid changes in
-- the lambda calculus. In fact we can, thanks to the `implements` relation;
-- but I guess the required proof must be done directly from derive-correct,
-- not from main-theorem.
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Change.Validity as Validity
import Parametric.Change.Specification as Specification
import Parametric.Change.Type as ChangeType
import Parametric.Change.Term as ChangeTerm
import Parametric.Change.Value as ChangeValue
import Parametric.Change.Evaluation as ChangeEvaluation
import Parametric.Change.Derive as Derive
import Parametric.Change.Implementation as Implementation
module Parametric.Change.Correctness
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ΔBase : ChangeType.Structure Base)
(diff-base : ChangeTerm.DiffStructure Const ΔBase)
(apply-base : ChangeTerm.ApplyStructure Const ΔBase)
(⟦apply-base⟧ : ChangeValue.ApplyStructure Const ⟦_⟧Base ΔBase)
(⟦diff-base⟧ : ChangeValue.DiffStructure Const ⟦_⟧Base ΔBase)
(meaning-⊕-base : ChangeEvaluation.ApplyStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(meaning-⊝-base : ChangeEvaluation.DiffStructure
⟦_⟧Base ⟦_⟧Const ΔBase apply-base diff-base ⟦apply-base⟧ ⟦diff-base⟧)
(validity-structure : Validity.Structure ⟦_⟧Base)
(specification-structure : Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure)
(derive-const : Derive.Structure Const ΔBase)
(implementation-structure : Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase
validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open Validity.Structure ⟦_⟧Base validity-structure
open Specification.Structure
Const ⟦_⟧Base ⟦_⟧Const validity-structure specification-structure
open ChangeType.Structure Base ΔBase
open ChangeTerm.Structure Const ΔBase diff-base apply-base
open ChangeValue.Structure Const ⟦_⟧Base ΔBase ⟦apply-base⟧ ⟦diff-base⟧
open ChangeEvaluation.Structure
⟦_⟧Base ⟦_⟧Const ΔBase
apply-base diff-base
⟦apply-base⟧ ⟦diff-base⟧
meaning-⊕-base meaning-⊝-base
open Derive.Structure Const ΔBase derive-const
open Implementation.Structure
Const ⟦_⟧Base ⟦_⟧Const ΔBase validity-structure specification-structure
⟦apply-base⟧ ⟦diff-base⟧ derive-const implementation-structure
-- The denotational properties of the `derive` transformation.
-- In particular, the main theorem about it producing the correct
-- incremental behavior.
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
Structure : Set
Structure = ∀ {Σ Γ τ} (c : Const Σ τ) (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
(ts-correct : implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))) →
⟦ c ⟧ΔConst (⟦ ts ⟧Terms ρ) (⟦ ts ⟧ΔTerms ρ dρ) ≈₍ τ ₎ ⟦ derive-const c (fit-terms ts) (derive-terms ts) ⟧ (alternate ρ ρ′)
module Structure (derive-const-correct : Structure) where
deriveVar-correct : ∀ {τ Γ} (x : Var Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ x ⟧ΔVar ρ dρ ≈₍ τ ₎ ⟦ deriveVar x ⟧ (alternate ρ ρ′)
deriveVar-correct this (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = dv≈dv′
deriveVar-correct (that x) (v • ρ) (dv • dρ) (dv′ • dρ′) (dv≈dv′ • dρ≈dρ′) = deriveVar-correct x ρ dρ dρ′ dρ≈dρ′
-- That `derive t` implements ⟦ t ⟧Δ
derive-correct : ∀ {τ Γ} (t : Term Γ τ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
⟦ t ⟧Δ ρ dρ ≈₍ τ ₎ ⟦ derive t ⟧ (alternate ρ ρ′)
derive-terms-correct : ∀ {Σ Γ} (ts : Terms Γ Σ)
(ρ : ⟦ Γ ⟧) (dρ : Δ₍ Γ ₎ ρ) (ρ′ : ⟦ mapContext ΔType Γ ⟧) (dρ≈ρ′ : implements-env Γ dρ ρ′) →
implements-env Σ (⟦ ts ⟧ΔTerms ρ dρ) (⟦ derive-terms ts ⟧Terms (alternate ρ ρ′))
derive-terms-correct ∅ ρ dρ ρ′ dρ≈ρ′ = ∅
derive-terms-correct (t • ts) ρ dρ ρ′ dρ≈ρ′ =
derive-correct t ρ dρ ρ′ dρ≈ρ′ • derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′
derive-correct (const c ts) ρ dρ ρ′ dρ≈ρ′ =
derive-const-correct c ts ρ dρ ρ′ dρ≈ρ′
(derive-terms-correct ts ρ dρ ρ′ dρ≈ρ′)
derive-correct (var x) ρ dρ ρ′ dρ≈ρ′ =
deriveVar-correct x ρ dρ ρ′ dρ≈ρ′
derive-correct (app {σ} {τ} s t) ρ dρ ρ′ dρ≈ρ′
= subst (λ ⟦t⟧ → ⟦ app s t ⟧Δ ρ dρ ≈₍ τ ₎ (⟦ derive s ⟧Term (alternate ρ ρ′)) ⟦t⟧ (⟦ derive t ⟧Term (alternate ρ ρ′))) (⟦fit⟧ t ρ ρ′)
(derive-correct {σ ⇒ τ} s ρ dρ ρ′ dρ≈ρ′
(⟦ t ⟧ ρ) (⟦ t ⟧Δ ρ dρ) (⟦ derive t ⟧ (alternate ρ ρ′)) (derive-correct {σ} t ρ dρ ρ′ dρ≈ρ′))
derive-correct (abs {σ} {τ} t) ρ dρ ρ′ dρ≈ρ′ =
λ w dw w′ dw≈w′ →
derive-correct t (w • ρ) (dw • dρ) (w′ • ρ′) (dw≈w′ • dρ≈ρ′)
derive-correct-closed : ∀ {τ} (t : Term ∅ τ) →
⟦ t ⟧Δ ∅ ∅ ≈₍ τ ₎ ⟦ derive t ⟧ ∅
derive-correct-closed t = derive-correct t ∅ ∅ ∅ ∅
main-theorem : ∀ {σ τ}
{f : Term ∅ (σ ⇒ τ)} {s : Term ∅ σ} {ds : Term ∅ (ΔType σ)} →
{dv : Δ₍ σ ₎ (⟦ s ⟧ ∅)} {erasure : dv ≈₍ σ ₎ (⟦ ds ⟧ ∅)} →
⟦ app f (s ⊕₍ σ ₎ ds) ⟧ ≡ ⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧
main-theorem {σ} {τ} {f} {s} {ds} {dv} {erasure} =
let
g = ⟦ f ⟧ ∅
Δg = ⟦ f ⟧Δ ∅ ∅
Δg′ = ⟦ derive f ⟧ ∅
v = ⟦ s ⟧ ∅
dv′ = ⟦ ds ⟧ ∅
u = ⟦ s ⊕₍ σ ₎ ds ⟧ ∅
-- Δoutput-term = app (app (derive f) x) (y ⊝ x)
in
ext {A = ⟦ ∅ ⟧Context} (λ { ∅ →
begin
g u
≡⟨ cong g (sym (meaning-⊕ {t = s} {Δt = ds})) ⟩
g (v ⟦⊕₍ σ ₎⟧ dv′)
≡⟨ cong g (sym (carry-over {σ} dv erasure)) ⟩
g (v ⊞₍ σ ₎ dv)
≡⟨ corollary-closed {σ} {τ} f v dv ⟩
g v ⊞₍ τ ₎ call-change {σ} {τ} Δg v dv
≡⟨ carry-over {τ} (call-change {σ} {τ} Δg v dv)
(derive-correct f ∅ ∅ ∅ ∅ v dv dv′ erasure) ⟩
g v ⟦⊕₍ τ ₎⟧ Δg′ v dv′
≡⟨ meaning-⊕ {t = app f s} {Δt = app (app (derive f) s) ds} ⟩
⟦ app f s ⊕₍ τ ₎ app (app (derive f) s) ds ⟧ ∅
∎}) where open ≡-Reasoning
|
make main theorem correspond to the one stated in the paper
|
make main theorem correspond to the one stated in the paper
Old-commit-hash: 84979b2067c54a685110f1bad744ba106a962a0f
|
Agda
|
mit
|
inc-lc/ilc-agda
|
272abae5605c8fd8c5385376ffe4f4d4c26d2a9d
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR ≙-setoid public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil x)
≡⟨ update-nil x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ cong (f ⊞ df₁) $ proof dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ cong (λ f → f (x ⊞ dx₂)) $ proof df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- We know that Derivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> Derivative f (apply df).
-- Now, we try to prove that if Derivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
Derivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil x))
≡⟨ ext (λ x → cong f (update-nil x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
Derivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- Unused, but just to test that inference works.
lemma : nil f ≙ dg
lemma = ≙-sym (derivative-is-nil dg fdg)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR ≙-setoid public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil x)
≡⟨ update-nil x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- We know that Derivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> Derivative f (apply df).
-- Now, we try to prove that if Derivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
Derivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil x))
≡⟨ ext (λ x → cong f (update-nil x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
Derivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- Unused, but just to test that inference works.
lemma : nil f ≙ dg
lemma = ≙-sym (derivative-is-nil dg fdg)
|
Add and use a congruence lemma
|
Add and use a congruence lemma
To reduce explicit wrapping/unwrapping of proofs of ≙.
Old-commit-hash: 2bc64b5b9f3b074a70918e11f66512232fb6796b
|
Agda
|
mit
|
inc-lc/ilc-agda
|
57dd579828aa8a7c7ff8b0546dd51520584eb18e
|
Denotational/Evaluation/Total.agda
|
Denotational/Evaluation/Total.agda
|
module Denotational.Evaluation.Total where
-- EVALUATION with a primitive for TOTAL DERIVATIVES
--
-- This module defines the semantics of terms that support a
-- primitive (Δ e) for computing the total derivative according
-- to all free variables in e and all future arguments of e if e
-- is a function.
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Changes
open import ChangeContexts
-- TERMS
-- Denotational Semantics
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ app t₁ t₂ ⟧Term ρ = (⟦ t₁ ⟧Term ρ) (⟦ t₂ ⟧Term ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ true ⟧Term ρ = true
⟦ false ⟧Term ρ = false
⟦ if t₁ t₂ t₃ ⟧Term ρ = if ⟦ t₁ ⟧Term ρ then ⟦ t₂ ⟧Term ρ else ⟦ t₃ ⟧Term ρ
⟦ Δ {{Γ′}} t ⟧Term ρ = diff (⟦ t ⟧Term (update (⟦ Γ′ ⟧ ρ))) (⟦ t ⟧Term (ignore (⟦ Γ′ ⟧ ρ)))
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
-- PROPERTIES of WEAKENING
weaken-sound : ∀ {Γ₁ Γ₂ τ} (t : Term Γ₁ τ) {Γ′ : Γ₁ ≼ Γ₂} →
∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ′ ⟧ ρ)
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound (app t₁ t₂) ρ = ≡-app (weaken-sound t₁ ρ) (weaken-sound t₂ ρ)
weaken-sound (var x) ρ = lift-sound _ x ρ
weaken-sound true ρ = refl
weaken-sound false ρ = refl
weaken-sound (if t₁ t₂ t₃) {Γ′} ρ with weaken-sound t₁ {Γ′} ρ
... | H with ⟦ weaken Γ′ t₁ ⟧ ρ | ⟦ t₁ ⟧ (⟦ Γ′ ⟧ ρ)
weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | true | true = weaken-sound t₂ {Γ′} ρ
weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | false | false = weaken-sound t₃ {Γ′} ρ
weaken-sound (Δ {{Γ′}} t) {Γ″} ρ =
cong (λ x → diff (⟦ t ⟧ (update x)) (⟦ t ⟧ (ignore x))) (⟦⟧-≼-trans Γ′ Γ″ ρ)
|
module Denotational.Evaluation.Total where
-- EVALUATION with a primitive for TOTAL DERIVATIVES
--
-- This module defines the semantics of terms that support a
-- primitive (Δ e) for computing the total derivative according
-- to all free variables in e and all future arguments of e if e
-- is a function.
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Changes
open import ChangeContexts
-- TERMS
-- Denotational Semantics
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ app t₁ t₂ ⟧Term ρ = (⟦ t₁ ⟧Term ρ) (⟦ t₂ ⟧Term ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ true ⟧Term ρ = true
⟦ false ⟧Term ρ = false
⟦ if t₁ t₂ t₃ ⟧Term ρ = if ⟦ t₁ ⟧Term ρ then ⟦ t₂ ⟧Term ρ else ⟦ t₃ ⟧Term ρ
⟦ Δ {{Γ′}} t ⟧Term ρ = diff (⟦ t ⟧Term (update (⟦ Γ′ ⟧ ρ))) (⟦ t ⟧Term (ignore (⟦ Γ′ ⟧ ρ)))
{-
Here is an example to understand the semantics of Δ. I will use a
named variable representation for the task.
Consider the typing judgment:
x: T |- x: T
Thus, we have that:
dx : Δ T, x: T |- Δ x : Δ T
Thanks to weakening, we also have:
y : S, dx : Δ T, x: T |- Δ x : Δ T
In the formalization, we need a proof Γ′ that the context Γ₁ = dx : Δ
T, x: T is a subcontext of Γ₂ = y : S, dx : Δ T, x: T. Thus, Γ′ has
type Γ₁ ≼ Γ₂.
Now take the environment:
ρ = y ↦ w, dx ↦ dv, x ↦ v
Since the semantics of Γ′ : Γ₁ ≼ Γ₂ is a function from environments
for Γ₂ to environments for Γ₁, we have that:
⟦ Γ′ ⟧ ρ = dx ↦ dv, x ↦ v
From the definitions of update and ignore, it follows that:
update (⟦ Γ′ ⟧ ρ) = x ↦ dv ⊕ v
ignore (⟦ Γ′ ⟧ ρ) = x ↦ v
Hence, finally, we have that:
diff (⟦ t ⟧Term (update (⟦ Γ′ ⟧ ρ))) (⟦ t ⟧Term (ignore (⟦ Γ′ ⟧ ρ)))
is simply diff (dv ⊕ v) v (or (dv ⊕ v) ⊝ v). If dv is a valid change,
that's just dv, that is ⟦ dx ⟧ ρ. In other words
-}
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
-- PROPERTIES of WEAKENING
weaken-sound : ∀ {Γ₁ Γ₂ τ} (t : Term Γ₁ τ) {Γ′ : Γ₁ ≼ Γ₂} →
∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ′ ⟧ ρ)
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound (app t₁ t₂) ρ = ≡-app (weaken-sound t₁ ρ) (weaken-sound t₂ ρ)
weaken-sound (var x) ρ = lift-sound _ x ρ
weaken-sound true ρ = refl
weaken-sound false ρ = refl
weaken-sound (if t₁ t₂ t₃) {Γ′} ρ with weaken-sound t₁ {Γ′} ρ
... | H with ⟦ weaken Γ′ t₁ ⟧ ρ | ⟦ t₁ ⟧ (⟦ Γ′ ⟧ ρ)
weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | true | true = weaken-sound t₂ {Γ′} ρ
weaken-sound (if t₁ t₂ t₃) {Γ′} ρ | refl | false | false = weaken-sound t₃ {Γ′} ρ
weaken-sound (Δ {{Γ′}} t) {Γ″} ρ =
cong (λ x → diff (⟦ t ⟧ (update x)) (⟦ t ⟧ (ignore x))) (⟦⟧-≼-trans Γ′ Γ″ ρ)
|
add extended comment on semantics of Δ
|
agda: add extended comment on semantics of Δ
During my walk-throughs on the code, explaining this line was a bit
hard and required a small example. After writing it here during the
walkthrough, I decided to extend it and save it.
Old-commit-hash: 279b6047512b8c934cae49bda5977fa104077e12
|
Agda
|
mit
|
inc-lc/ilc-agda
|
beadc4c6bf5a0654b270878f150131cb18ada078
|
lib/Data/Bool/NP.agda
|
lib/Data/Bool/NP.agda
|
{-# OPTIONS --universe-polymorphism #-}
module Data.Bool.NP where
open import Data.Bool using (Bool; true; false; T; if_then_else_; not)
open import Data.Unit using (⊤)
open import Data.Sum
open import Function
open import Relation.Binary.NP
open import Relation.Binary.Logical
open import Relation.Nullary
open import Relation.Nullary.Decidable
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_)
check : ∀ b → {pf : T b} → ⊤
check = _
If_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → (T b → A) → (T (not b) → B) → if b then A else B
If_then_else_ true x _ = x _
If_then_else_ false _ x = x _
If′_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → A → B → if b then A else B
If′_then_else_ true x _ = x
If′_then_else_ false _ x = x
If-map : ∀ {A B C D : Set} b (f : T b → A → C) (g : T (not b) → B → D) →
if b then A else B → if b then C else D
If-map true f _ = f _
If-map false _ f = f _
If-elim : ∀ {A B : Set} {P : Bool → Set}
b (f : T b → A → P true) (g : T (not b) → B → P false) → if b then A else B → P b
If-elim true f _ = f _
If-elim false _ f = f _
If-true : ∀ {A B : Set} {b} → T b → (if b then A else B) ≡ A
If-true {b = true} _ = ≡.refl
If-true {b = false} ()
If-false : ∀ {A B : Set} {b} → T (not b) → (if b then A else B) ≡ B
If-false {b = true} ()
If-false {b = false} _ = ≡.refl
cong-if : ∀ {A B : Set} b {t₀ t₁} (f : A → B) → (if b then f t₀ else f t₁) ≡ f (if b then t₀ else t₁)
cong-if true _ = ≡.refl
cong-if false _ = ≡.refl
data ⟦Bool⟧ : (b₁ b₂ : Bool) → Set where
⟦true⟧ : ⟦Bool⟧ true true
⟦false⟧ : ⟦Bool⟧ false false
private
module ⟦Bool⟧-Internals where
refl : Reflexive ⟦Bool⟧
refl {true} = ⟦true⟧
refl {false} = ⟦false⟧
sym : Symmetric ⟦Bool⟧
sym ⟦true⟧ = ⟦true⟧
sym ⟦false⟧ = ⟦false⟧
trans : Transitive ⟦Bool⟧
trans ⟦true⟧ = id
trans ⟦false⟧ = id
subst : ∀ {ℓ} → Substitutive ⟦Bool⟧ ℓ
subst _ ⟦true⟧ = id
subst _ ⟦false⟧ = id
_≟_ : Decidable ⟦Bool⟧
true ≟ true = yes ⟦true⟧
false ≟ false = yes ⟦false⟧
true ≟ false = no (λ())
false ≟ true = no (λ())
isEquivalence : IsEquivalence ⟦Bool⟧
isEquivalence = record { refl = refl; sym = sym; trans = trans }
isDecEquivalence : IsDecEquivalence ⟦Bool⟧
isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ }
setoid : Setoid _ _
setoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isEquivalence = isEquivalence }
decSetoid : DecSetoid _ _
decSetoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isDecEquivalence = isDecEquivalence }
equality : Equality ⟦Bool⟧
equality = record { isEquivalence = isEquivalence; subst = subst }
module ⟦Bool⟧-Props where
open ⟦Bool⟧-Internals public using (subst; decSetoid; equality)
open DecSetoid decSetoid public
open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)
⟦if⟨_⟩_then_else_⟧ : ∀ {a₁ a₂ aᵣ} → (∀⟨ Aᵣ ∶ ⟦Set⟧ {a₁} {a₂} aᵣ ⟩⟦→⟧ ⟦Bool⟧ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ) if_then_else_ if_then_else_
⟦if⟨_⟩_then_else_⟧ _ ⟦true⟧ xᵣ _ = xᵣ
⟦if⟨_⟩_then_else_⟧ _ ⟦false⟧ _ xᵣ = xᵣ
⟦If′⟨_,_⟩_then_else_⟧ : ∀ {ℓ₁ ℓ₂ ℓᵣ} →
(∀⟨ Aᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧ ∀⟨ Bᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧
⟨ bᵣ ∶ ⟦Bool⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Bᵣ ⟦→⟧ ⟦if⟨ ⟦Set⟧ _ ⟩ bᵣ then Aᵣ else Bᵣ ⟧)
If′_then_else_ If′_then_else_
⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦true⟧ xᵣ _ = xᵣ
⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦false⟧ _ xᵣ = xᵣ
_==_ : (x y : Bool) → Bool
true == true = true
true == false = false
false == true = false
false == false = true
module == where
_≈_ : (x y : Bool) → Set
x ≈ y = T (x == y)
refl : Reflexive _≈_
refl {true} = _
refl {false} = _
subst : ∀ {ℓ} → Substitutive _≈_ ℓ
subst _ {true} {true} _ = id
subst _ {false} {false} _ = id
subst _ {true} {false} ()
subst _ {false} {true} ()
sym : Symmetric _≈_
sym {x} {y} eq = subst (λ y → y ≈ x) {x} {y} eq (refl {x})
trans : Transitive _≈_
trans {x} {y} {z} x≈y y≈z = subst (_≈_ x) {y} {z} y≈z x≈y
_≟_ : Decidable _≈_
true ≟ true = yes _
false ≟ false = yes _
true ≟ false = no (λ())
false ≟ true = no (λ())
isEquivalence : IsEquivalence _≈_
isEquivalence = record { refl = λ {x} → refl {x}
; sym = λ {x} {y} → sym {x} {y}
; trans = λ {x} {y} {z} → trans {x} {y} {z} }
isDecEquivalence : IsDecEquivalence _≈_
isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ }
setoid : Setoid _ _
setoid = record { Carrier = Bool; _≈_ = _≈_ ; isEquivalence = isEquivalence }
decSetoid : DecSetoid _ _
decSetoid = record { Carrier = Bool; _≈_ = _≈_; isDecEquivalence = isDecEquivalence }
module ⟦Bool⟧-Reasoning = Setoid-Reasoning ⟦Bool⟧-Props.setoid
open Data.Bool public
⟦true⟧′ : ∀ {b} → T b → ⟦Bool⟧ true b
⟦true⟧′ {true} _ = ⟦true⟧
⟦true⟧′ {false} ()
⟦false⟧′ : ∀ {b} → T (not b) → ⟦Bool⟧ false b
⟦false⟧′ {true} ()
⟦false⟧′ {false} _ = ⟦false⟧
T∧ : ∀ {b₁ b₂} → T b₁ → T b₂ → T (b₁ ∧ b₂)
T∧ {true} {true} _ _ = _
T∧ {false} {_} () _
T∧ {true} {false} _ ()
T∧₁ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₁
T∧₁ {true} {true} _ = _
T∧₁ {false} {_} ()
T∧₁ {true} {false} ()
T∧₂ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₂
T∧₂ {true} {true} _ = _
T∧₂ {false} {_} ()
T∧₂ {true} {false} ()
T∨'⊎ : ∀ {b₁ b₂} → T (b₁ ∨ b₂) → T b₁ ⊎ T b₂
T∨'⊎ {true} _ = inj₁ _
T∨'⊎ {false} {true} _ = inj₂ _
T∨'⊎ {false} {false} ()
T∨₁ : ∀ {b₁ b₂} → T b₁ → T (b₁ ∨ b₂)
T∨₁ {true} _ = _
T∨₁ {false} {true} _ = _
T∨₁ {false} {false} ()
T∨₂ : ∀ {b₁ b₂} → T b₂ → T (b₁ ∨ b₂)
T∨₂ {true} _ = _
T∨₂ {false} {true} _ = _
T∨₂ {false} {false} ()
T'not'¬ : ∀ {b} → T (not b) → ¬ (T b)
T'not'¬ {false} _ = λ()
T'not'¬ {true} ()
T'¬'not : ∀ {b} → ¬ (T b) → T (not b)
T'¬'not {true} f = f _
T'¬'not {false} _ = _
Tdec : ∀ b → Dec (T b)
Tdec true = yes _
Tdec false = no λ()
|
{-# OPTIONS --universe-polymorphism #-}
module Data.Bool.NP where
open import Data.Bool using (Bool; true; false; T; if_then_else_; not)
import Algebra
import Data.Bool.Properties
open import Data.Unit using (⊤)
open import Data.Sum
open import Function
open import Relation.Binary.NP
open import Relation.Binary.Logical
open import Relation.Nullary
open import Relation.Nullary.Decidable
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_)
module Xor° = Algebra.CommutativeRing Data.Bool.Properties.commutativeRing-xor-∧
module Bool° = Algebra.CommutativeSemiring Data.Bool.Properties.commutativeSemiring-∧-∨
check : ∀ b → {pf : T b} → ⊤
check = _
If_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → (T b → A) → (T (not b) → B) → if b then A else B
If_then_else_ true x _ = x _
If_then_else_ false _ x = x _
If′_then_else_ : ∀ {ℓ} {A B : Set ℓ} b → A → B → if b then A else B
If′_then_else_ true x _ = x
If′_then_else_ false _ x = x
If-map : ∀ {A B C D : Set} b (f : T b → A → C) (g : T (not b) → B → D) →
if b then A else B → if b then C else D
If-map true f _ = f _
If-map false _ f = f _
If-elim : ∀ {A B : Set} {P : Bool → Set}
b (f : T b → A → P true) (g : T (not b) → B → P false) → if b then A else B → P b
If-elim true f _ = f _
If-elim false _ f = f _
If-true : ∀ {A B : Set} {b} → T b → (if b then A else B) ≡ A
If-true {b = true} _ = ≡.refl
If-true {b = false} ()
If-false : ∀ {A B : Set} {b} → T (not b) → (if b then A else B) ≡ B
If-false {b = true} ()
If-false {b = false} _ = ≡.refl
cong-if : ∀ {A B : Set} b {t₀ t₁} (f : A → B) → (if b then f t₀ else f t₁) ≡ f (if b then t₀ else t₁)
cong-if true _ = ≡.refl
cong-if false _ = ≡.refl
data ⟦Bool⟧ : (b₁ b₂ : Bool) → Set where
⟦true⟧ : ⟦Bool⟧ true true
⟦false⟧ : ⟦Bool⟧ false false
private
module ⟦Bool⟧-Internals where
refl : Reflexive ⟦Bool⟧
refl {true} = ⟦true⟧
refl {false} = ⟦false⟧
sym : Symmetric ⟦Bool⟧
sym ⟦true⟧ = ⟦true⟧
sym ⟦false⟧ = ⟦false⟧
trans : Transitive ⟦Bool⟧
trans ⟦true⟧ = id
trans ⟦false⟧ = id
subst : ∀ {ℓ} → Substitutive ⟦Bool⟧ ℓ
subst _ ⟦true⟧ = id
subst _ ⟦false⟧ = id
_≟_ : Decidable ⟦Bool⟧
true ≟ true = yes ⟦true⟧
false ≟ false = yes ⟦false⟧
true ≟ false = no (λ())
false ≟ true = no (λ())
isEquivalence : IsEquivalence ⟦Bool⟧
isEquivalence = record { refl = refl; sym = sym; trans = trans }
isDecEquivalence : IsDecEquivalence ⟦Bool⟧
isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ }
setoid : Setoid _ _
setoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isEquivalence = isEquivalence }
decSetoid : DecSetoid _ _
decSetoid = record { Carrier = Bool; _≈_ = ⟦Bool⟧; isDecEquivalence = isDecEquivalence }
equality : Equality ⟦Bool⟧
equality = record { isEquivalence = isEquivalence; subst = subst }
module ⟦Bool⟧-Props where
open ⟦Bool⟧-Internals public using (subst; decSetoid; equality)
open DecSetoid decSetoid public
open Equality equality public hiding (subst; isEquivalence; refl; reflexive; sym; trans)
⟦if⟨_⟩_then_else_⟧ : ∀ {a₁ a₂ aᵣ} → (∀⟨ Aᵣ ∶ ⟦Set⟧ {a₁} {a₂} aᵣ ⟩⟦→⟧ ⟦Bool⟧ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ Aᵣ) if_then_else_ if_then_else_
⟦if⟨_⟩_then_else_⟧ _ ⟦true⟧ xᵣ _ = xᵣ
⟦if⟨_⟩_then_else_⟧ _ ⟦false⟧ _ xᵣ = xᵣ
⟦If′⟨_,_⟩_then_else_⟧ : ∀ {ℓ₁ ℓ₂ ℓᵣ} →
(∀⟨ Aᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧ ∀⟨ Bᵣ ∶ ⟦Set⟧ {ℓ₁} {ℓ₂} ℓᵣ ⟩⟦→⟧
⟨ bᵣ ∶ ⟦Bool⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Bᵣ ⟦→⟧ ⟦if⟨ ⟦Set⟧ _ ⟩ bᵣ then Aᵣ else Bᵣ ⟧)
If′_then_else_ If′_then_else_
⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦true⟧ xᵣ _ = xᵣ
⟦If′⟨_,_⟩_then_else_⟧ _ _ ⟦false⟧ _ xᵣ = xᵣ
_==_ : (x y : Bool) → Bool
true == true = true
true == false = false
false == true = false
false == false = true
module == where
_≈_ : (x y : Bool) → Set
x ≈ y = T (x == y)
refl : Reflexive _≈_
refl {true} = _
refl {false} = _
subst : ∀ {ℓ} → Substitutive _≈_ ℓ
subst _ {true} {true} _ = id
subst _ {false} {false} _ = id
subst _ {true} {false} ()
subst _ {false} {true} ()
sym : Symmetric _≈_
sym {x} {y} eq = subst (λ y → y ≈ x) {x} {y} eq (refl {x})
trans : Transitive _≈_
trans {x} {y} {z} x≈y y≈z = subst (_≈_ x) {y} {z} y≈z x≈y
_≟_ : Decidable _≈_
true ≟ true = yes _
false ≟ false = yes _
true ≟ false = no (λ())
false ≟ true = no (λ())
isEquivalence : IsEquivalence _≈_
isEquivalence = record { refl = λ {x} → refl {x}
; sym = λ {x} {y} → sym {x} {y}
; trans = λ {x} {y} {z} → trans {x} {y} {z} }
isDecEquivalence : IsDecEquivalence _≈_
isDecEquivalence = record { isEquivalence = isEquivalence; _≟_ = _≟_ }
setoid : Setoid _ _
setoid = record { Carrier = Bool; _≈_ = _≈_ ; isEquivalence = isEquivalence }
decSetoid : DecSetoid _ _
decSetoid = record { Carrier = Bool; _≈_ = _≈_; isDecEquivalence = isDecEquivalence }
module ⟦Bool⟧-Reasoning = Setoid-Reasoning ⟦Bool⟧-Props.setoid
open Data.Bool public
⟦true⟧′ : ∀ {b} → T b → ⟦Bool⟧ true b
⟦true⟧′ {true} _ = ⟦true⟧
⟦true⟧′ {false} ()
⟦false⟧′ : ∀ {b} → T (not b) → ⟦Bool⟧ false b
⟦false⟧′ {true} ()
⟦false⟧′ {false} _ = ⟦false⟧
T∧ : ∀ {b₁ b₂} → T b₁ → T b₂ → T (b₁ ∧ b₂)
T∧ {true} {true} _ _ = _
T∧ {false} {_} () _
T∧ {true} {false} _ ()
T∧₁ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₁
T∧₁ {true} {true} _ = _
T∧₁ {false} {_} ()
T∧₁ {true} {false} ()
T∧₂ : ∀ {b₁ b₂} → T (b₁ ∧ b₂) → T b₂
T∧₂ {true} {true} _ = _
T∧₂ {false} {_} ()
T∧₂ {true} {false} ()
T∨'⊎ : ∀ {b₁ b₂} → T (b₁ ∨ b₂) → T b₁ ⊎ T b₂
T∨'⊎ {true} _ = inj₁ _
T∨'⊎ {false} {true} _ = inj₂ _
T∨'⊎ {false} {false} ()
T∨₁ : ∀ {b₁ b₂} → T b₁ → T (b₁ ∨ b₂)
T∨₁ {true} _ = _
T∨₁ {false} {true} _ = _
T∨₁ {false} {false} ()
T∨₂ : ∀ {b₁ b₂} → T b₂ → T (b₁ ∨ b₂)
T∨₂ {true} _ = _
T∨₂ {false} {true} _ = _
T∨₂ {false} {false} ()
T'not'¬ : ∀ {b} → T (not b) → ¬ (T b)
T'not'¬ {false} _ = λ()
T'not'¬ {true} ()
T'¬'not : ∀ {b} → ¬ (T b) → T (not b)
T'¬'not {true} f = f _
T'¬'not {false} _ = _
Tdec : ∀ b → Dec (T b)
Tdec true = yes _
Tdec false = no λ()
|
add ring modules Xor°, Bool°
|
Data.Bool.NP: add ring modules Xor°, Bool°
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
09aa7263bcfccc0537afdaba6875c6d31a8fa0e1
|
lib/Algebra/Group.agda
|
lib/Algebra/Group.agda
|
{-# OPTIONS --without-K #-}
-- TODO
-- If you are looking for a proof of:
-- f (Σ(xᵢ∈A) g(x₁)) ≡ Π(xᵢ∈A) (f(g(xᵢ)))
-- Have a look to:
-- https://github.com/crypto-agda/explore/blob/master/lib/Explore/GroupHomomorphism.agda
open import Type using (Type_)
open import Data.Product.NP using (_,_)
import Algebra.FunctionProperties.Eq
open Algebra.FunctionProperties.Eq.Implicits
open import Algebra.Monoid
module Algebra.Group where
record Group-Ops {ℓ} (G : Type ℓ) : Type ℓ where
constructor _,_
field
mon-ops : Monoid-Ops G
_⁻¹ : G → G
open Monoid-Ops mon-ops public
open From-Group-Ops ε _∙_ _⁻¹ public
record Group-Struct {ℓ} {G : Type ℓ} (grp-ops : Group-Ops G) : Type ℓ where
constructor _,_
open Group-Ops grp-ops
-- laws
field
mon-struct : Monoid-Struct mon-ops
inverse : Inverse ε _⁻¹ _∙_
mon : Monoid G
mon = mon-ops , mon-struct
open Monoid-Struct mon-struct public
open From-Assoc-Identities-Inverse assoc identity inverse public
-- TODO Monoid+LeftInverse → Group
record Group {ℓ}(G : Type ℓ) : Type ℓ where
constructor _,_
field
grp-ops : Group-Ops G
grp-struct : Group-Struct grp-ops
open Group-Ops grp-ops public
open Group-Struct grp-struct public
-- A renaming of Group-Ops with additive notation
module Additive-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) where
private
module M = Group-Ops grp
using ()
renaming ( _⁻¹ to 0−_
; _/_ to _−_
; _^⁻_ to _⊗⁻_
; _^_ to _⊗_
; mon-ops to +-mon-ops
; /= to −=)
open M public using (0−_; +-mon-ops; −=)
open Additive-Monoid-Ops +-mon-ops public
infixl 6 _−_
infixl 7 _⊗⁻_ _⊗_
_−_ = M._−_
_⊗⁻_ = M._⊗⁻_
_⊗_ = M._⊗_
-- A renaming of Group-Struct with additive notation
module Additive-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ()
renaming ( mon-struct to +-mon-struct
; mon to +-mon
; assoc to +-assoc
; identity to +-identity
; ε∙-identity to 0+-identity
; ∙ε-identity to +0-identity
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; inverse to 0−-inverse
; ∙-/ to +-−; /-∙ to −-+
; unique-ε-left to unique-0-left
; unique-ε-right to unique-0-right
; x/y≡ε→x≡y to x−y≡0→x≡y
; x/y≢ε to x−y≢0
; is-ε-left to is-0-left
; is-ε-right to is-0-right
; unique-⁻¹ to unique-0−
; cancels-∙-left to cancels-+-left
; cancels-∙-right to cancels-+-right
; elim-∙-right-/ to elim-+-right-−
; elim-assoc= to elim-+-assoc=
; elim-!assoc= to elim-+-!assoc=
; elim-inner= to elim-+-inner=
; ⁻¹-hom′ to 0−-hom′
; ⁻¹-inj to 0−-inj
; ⁻¹-involutive to 0−-involutive
; ε⁻¹≡ε to 0−0≡0
)
-- A renaming of Group with additive notation
module Additive-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Additive-Group-Ops (Group.grp-ops mon) public
open Additive-Group-Struct (Group.grp-struct mon) public
-- A renaming of Group-Ops with multiplicative notation
module Multiplicative-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) = Group-Ops grp
using ( _⁻¹; _/_; /=; _^⁺_ ; _^⁻_; _^_; _²; _³; _⁴ )
renaming ( _∙_ to _*_; ε to 1#; mon-ops to *-mon-ops; ∙= to *= )
-- A renaming of Group-Struct with multiplicative notation
module Multiplicative-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ( unique-⁻¹
; ⁻¹-hom′
; ⁻¹-inj
; ⁻¹-involutive
)
renaming ( assoc to *-assoc
; identity to *-identity
; ε∙-identity to 1*-identity
; ∙ε-identity to *1-identity
; inverse to ⁻¹-inverse
; ∙-/ to *-/; /-∙ to /-*
; mon-struct to *-mon-struct
; mon to *-mon
; unique-ε-left to unique-1-left
; unique-ε-right to unique-1-right
; x/y≡ε→x≡y to x/y≡1→x≡y
; x/y≢ε to x/y≢1
; is-ε-left to is-1-left
; is-ε-right to is-1-right
; cancels-∙-left to cancels-*-left
; cancels-∙-right to cancels-*-right
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; elim-∙-right-/ to elim-*-right-/
; elim-assoc= to elim-*-assoc=
; elim-!assoc= to elim-*-!assoc=
; elim-inner= to elim-*-inner=
; ε⁻¹≡ε to 1⁻¹≡1
)
-- A renaming of Group with multiplicative notation
module Multiplicative-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Multiplicative-Group-Ops (Group.grp-ops mon) public
open Multiplicative-Group-Struct (Group.grp-struct mon) public
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
-- TODO
-- If you are looking for a proof of:
-- f (Σ(xᵢ∈A) g(x₁)) ≡ Π(xᵢ∈A) (f(g(xᵢ)))
-- Have a look to:
-- https://github.com/crypto-agda/explore/blob/master/lib/Explore/GroupHomomorphism.agda
open import Type using (Type_)
open import Data.Product.NP using (_,_;fst;snd)
import Algebra.FunctionProperties.Eq
open Algebra.FunctionProperties.Eq.Implicits
open import Algebra.Monoid
module Algebra.Group where
record Group-Ops {ℓ} (G : Type ℓ) : Type ℓ where
constructor _,_
field
mon-ops : Monoid-Ops G
_⁻¹ : G → G
open Monoid-Ops mon-ops public
open From-Group-Ops ε _∙_ _⁻¹ public
record Group-Struct {ℓ} {G : Type ℓ} (grp-ops : Group-Ops G) : Type ℓ where
constructor _,_
open Group-Ops grp-ops
-- laws
field
mon-struct : Monoid-Struct mon-ops
inverse : Inverse ε _⁻¹ _∙_
mon : Monoid G
mon = mon-ops , mon-struct
⁻¹∙-inverse : LeftInverse ε _⁻¹ _∙_
⁻¹∙-inverse = fst inverse
∙⁻¹-inverse : RightInverse ε _⁻¹ _∙_
∙⁻¹-inverse = snd inverse
open Monoid-Struct mon-struct public
open From-Assoc-Identities-Inverse assoc identity inverse public
-- TODO Monoid+LeftInverse → Group
record Group {ℓ}(G : Type ℓ) : Type ℓ where
constructor _,_
field
grp-ops : Group-Ops G
grp-struct : Group-Struct grp-ops
open Group-Ops grp-ops public
open Group-Struct grp-struct public
-- A renaming of Group-Ops with additive notation
module Additive-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) where
private
module M = Group-Ops grp
using ()
renaming ( _⁻¹ to 0−_
; _/_ to _−_
; _^⁻_ to _⊗⁻_
; _^_ to _⊗_
; mon-ops to +-mon-ops
; /= to −=)
open M public using (0−_; +-mon-ops; −=)
open Additive-Monoid-Ops +-mon-ops public
infixl 6 _−_
infixl 7 _⊗⁻_ _⊗_
_−_ = M._−_
_⊗⁻_ = M._⊗⁻_
_⊗_ = M._⊗_
-- A renaming of Group-Struct with additive notation
module Additive-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ()
renaming ( mon-struct to +-mon-struct
; mon to +-mon
; assoc to +-assoc
; identity to +-identity
; ε∙-identity to 0+-identity
; ∙ε-identity to +0-identity
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; inverse to 0−-inverse
; ∙-/ to +-−; /-∙ to −-+
; unique-ε-left to unique-0-left
; unique-ε-right to unique-0-right
; x/y≡ε→x≡y to x−y≡0→x≡y
; x/y≢ε to x−y≢0
; is-ε-left to is-0-left
; is-ε-right to is-0-right
; unique-⁻¹ to unique-0−
; cancels-∙-left to cancels-+-left
; cancels-∙-right to cancels-+-right
; elim-∙-right-/ to elim-+-right-−
; elim-assoc= to elim-+-assoc=
; elim-!assoc= to elim-+-!assoc=
; elim-inner= to elim-+-inner=
; ⁻¹-hom′ to 0−-hom′
; ⁻¹-inj to 0−-inj
; ⁻¹-involutive to 0−-involutive
; ε⁻¹≡ε to 0−0≡0
)
-- A renaming of Group with additive notation
module Additive-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Additive-Group-Ops (Group.grp-ops mon) public
open Additive-Group-Struct (Group.grp-struct mon) public
-- A renaming of Group-Ops with multiplicative notation
module Multiplicative-Group-Ops {ℓ}{G : Type ℓ} (grp : Group-Ops G) = Group-Ops grp
using ( _⁻¹; _/_; /=; _^⁺_ ; _^⁻_; _^_; _²; _³; _⁴ )
renaming ( _∙_ to _*_; ε to 1#; mon-ops to *-mon-ops; ∙= to *= )
-- A renaming of Group-Struct with multiplicative notation
module Multiplicative-Group-Struct {ℓ}{G : Type ℓ}{grp-ops : Group-Ops G}
(grp-struct : Group-Struct grp-ops)
= Group-Struct grp-struct
using ( unique-⁻¹
; ⁻¹-hom′
; ⁻¹-inj
; ⁻¹-involutive
)
renaming ( assoc to *-assoc
; identity to *-identity
; ε∙-identity to 1*-identity
; ∙ε-identity to *1-identity
; inverse to ⁻¹-inverse
; ∙-/ to *-/; /-∙ to /-*
; mon-struct to *-mon-struct
; mon to *-mon
; unique-ε-left to unique-1-left
; unique-ε-right to unique-1-right
; x/y≡ε→x≡y to x/y≡1→x≡y
; x/y≢ε to x/y≢1
; is-ε-left to is-1-left
; is-ε-right to is-1-right
; cancels-∙-left to cancels-*-left
; cancels-∙-right to cancels-*-right
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; elim-∙-right-/ to elim-*-right-/
; elim-assoc= to elim-*-assoc=
; elim-!assoc= to elim-*-!assoc=
; elim-inner= to elim-*-inner=
; ε⁻¹≡ε to 1⁻¹≡1
)
-- A renaming of Group with multiplicative notation
module Multiplicative-Group {ℓ}{G : Type ℓ}(mon : Group G) where
open Multiplicative-Group-Ops (Group.grp-ops mon) public
open Multiplicative-Group-Struct (Group.grp-struct mon) public
-- -}
-- -}
-- -}
-- -}
|
update Group to include left and right inverse
|
update Group to include left and right inverse
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
bd4721cbfeecd02bd82f2eb2bb19570e836dac3a
|
Denotation/Implementation/Popl14.agda
|
Denotation/Implementation/Popl14.agda
|
module Denotation.Implementation.Popl14 where
-- Notions of programs being implementations of specifications
-- for Calculus Popl14
open import Denotation.Specification.Canon-Popl14 public
open import Popl14.Syntax.Type
open import Popl14.Syntax.Term
open import Popl14.Denotation.Value
open import Popl14.Change.Derive
open import Popl14.Change.Value
open import Popl14.Change.Validity
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Tuples
open import Structure.Bag.Popl14
open import Postulate.Extensionality
infix 4 implements
syntax implements τ u v = u ≈₍ τ ₎ v
implements : ∀ (τ : Type) → Change τ → ⟦ ΔType τ ⟧ → Set
u ≈₍ int ₎ v = u ≡ v
u ≈₍ bag ₎ v = u ≡ v
u ≈₍ σ ⇒ τ ₎ v =
(w : ⟦ σ ⟧) (Δw : Change σ) (R[w,Δw] : valid {σ} w Δw)
(Δw′ : ⟦ ΔType σ ⟧) (Δw≈Δw′ : implements σ Δw Δw′) →
implements τ (u (cons w Δw R[w,Δw])) (v w Δw′)
infix 4 _≈_
_≈_ : ∀ {τ} → Change τ → ⟦ ΔType τ ⟧ → Set
_≈_ {τ} = implements τ
module Disambiguation (τ : Type) where
_✚_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚_ = _⟦⊕⟧_ {τ}
_−_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−_ = _⟦⊝⟧_ {τ}
infixl 6 _✚_ _−_
_≃_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃_ = _≈_ {τ}
infix 4 _≃_
module FunctionDisambiguation (σ : Type) (τ : Type) where
open Disambiguation (σ ⇒ τ) public
_✚₁_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚₁_ = _⟦⊕⟧_ {τ}
_−₁_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−₁_ = _⟦⊝⟧_ {τ}
infixl 6 _✚₁_ _−₁_
_≃₁_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃₁_ = _≈_ {τ}
infix 4 _≃₁_
_✚₀_ : ⟦ σ ⟧ → ⟦ ΔType σ ⟧ → ⟦ σ ⟧
_✚₀_ = _⟦⊕⟧_ {σ}
_−₀_ : ⟦ σ ⟧ → ⟦ σ ⟧ → ⟦ ΔType σ ⟧
_−₀_ = _⟦⊝⟧_ {σ}
infixl 6 _✚₀_ _−₀_
_≃₀_ : Change σ → ⟦ ΔType σ ⟧ → Set
_≃₀_ = _≈_ {σ}
infix 4 _≃₀_
compatible : ∀ {Γ} → ΔEnv Γ → ⟦ ΔContext Γ ⟧ → Set
compatible {∅} ∅ ∅ = ⊤
compatible {τ • Γ} (cons v Δv _ • ρ) (Δv′ • v′ • ρ′) =
Triple (v ≡ v′) (λ _ → Δv ≈₍ τ ₎ Δv′) (λ _ _ → compatible ρ ρ′)
-- If a program implements a specification, then certain things
-- proven about the specification carry over to the programs.
carry-over : ∀ {τ}
{v : ⟦ τ ⟧} {Δv : Change τ} {Δv′ : ⟦ ΔType τ ⟧}
(R[v,Δv] : valid {τ} v Δv) (Δv≈Δv′ : Δv ≈₍ τ ₎ Δv′) →
let open Disambiguation τ in
v ⊞₍ τ ₎ Δv ≡ v ✚ Δv′
u⊟v≈u⊝v : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
let open Disambiguation τ in
u ⊟₍ τ ₎ v ≃ u − v
u⊟v≈u⊝v {base base-int} = refl
u⊟v≈u⊝v {base base-bag} = refl
u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where
open FunctionDisambiguation σ τ
result : (w : ⟦ σ ⟧) (Δw : Change σ) → valid {σ} w Δw →
(Δw′ : ⟦ ΔType σ ⟧) → Δw ≈₍ σ ₎ Δw′ →
g (w ⊞₍ σ ₎ Δw) ⊟₍ τ ₎ f w ≃₁ g (w ✚₀ Δw′) −₁ f w
result w Δw R[w,Δw] Δw′ Δw≈Δw′
rewrite carry-over {σ} {w} R[w,Δw] Δw≈Δw′ =
u⊟v≈u⊝v {τ} {g (w ✚₀ Δw′)} {f w}
carry-over {base base-int} {v} _ Δv≈Δv′ = cong (_+_ v) Δv≈Δv′
carry-over {base base-bag} {v} _ Δv≈Δv′ = cong (_++_ v) Δv≈Δv′
carry-over {σ ⇒ τ} {f} {Δf} {Δf′} R[f,Δf] Δf≈Δf′ =
ext (λ v →
let
open FunctionDisambiguation σ τ
V = R[v,u-v] {σ} {v} {v}
S = u⊟v≈u⊝v {σ} {v} {v}
in
carry-over {τ} {f v}
{Δf (nil-valid-change σ v)} {Δf′ v (v −₀ v)}
(proj₁ (R[f,Δf] (nil-valid-change σ v)))
(Δf≈Δf′ v (v ⊟₍ σ ₎ v) V (v −₀ v) S))
-- A property relating `ignore` and the subcontext relation Γ≼ΔΓ
⟦Γ≼ΔΓ⟧ : ∀ {Γ} {ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧}
(C : compatible ρ ρ′) → ignore ρ ≡ ⟦ Γ≼ΔΓ ⟧ ρ′
⟦Γ≼ΔΓ⟧ {∅} {∅} {∅} _ = refl
⟦Γ≼ΔΓ⟧ {τ • Γ} {cons v dv _ • ρ} {dv′ • v′ • ρ′}
(cons v≡v′ _ C) = cong₂ _•_ v≡v′ (⟦Γ≼ΔΓ⟧ C)
-- A specialization of the soundness of weakening
⟦fit⟧ : ∀ {τ Γ} (t : Term Γ τ)
{ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧} (C : compatible ρ ρ′) →
⟦ t ⟧ (ignore ρ) ≡ ⟦ fit t ⟧ ρ′
⟦fit⟧ t {ρ} {ρ′} C =
trans (cong ⟦ t ⟧ (⟦Γ≼ΔΓ⟧ C)) (sym (weaken-sound t ρ′))
|
module Denotation.Implementation.Popl14 where
-- Notions of programs being implementations of specifications
-- for Calculus Popl14
open import Denotation.Specification.Canon-Popl14 public
open import Popl14.Syntax.Type
open import Popl14.Syntax.Term
open import Popl14.Denotation.Value
open import Popl14.Change.Derive
open import Popl14.Change.Value
open import Popl14.Change.Validity
open import Relation.Binary.PropositionalEquality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Tuples
open import Structure.Bag.Popl14
open import Postulate.Extensionality
infix 4 implements
syntax implements τ u v = u ≈₍ τ ₎ v
implements : ∀ (τ : Type) → Change τ → ⟦ ΔType τ ⟧ → Set
u ≈₍ int ₎ v = u ≡ v
u ≈₍ bag ₎ v = u ≡ v
u ≈₍ σ ⇒ τ ₎ v =
(w : ⟦ σ ⟧) (Δw : Change σ) (R[w,Δw] : valid {σ} w Δw)
(Δw′ : ⟦ ΔType σ ⟧) (Δw≈Δw′ : implements σ Δw Δw′) →
implements τ (u (cons w Δw R[w,Δw])) (v w Δw′)
infix 4 _≈_
_≈_ : ∀ {τ} → Change τ → ⟦ ΔType τ ⟧ → Set
_≈_ {τ} = implements τ
module Disambiguation (τ : Type) where
_✚_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚_ = _⟦⊕⟧_ {τ}
_−_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−_ = _⟦⊝⟧_ {τ}
infixl 6 _✚_ _−_
_≃_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃_ = _≈_ {τ}
infix 4 _≃_
module FunctionDisambiguation (σ : Type) (τ : Type) where
open Disambiguation (σ ⇒ τ) public
_✚₁_ : ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_✚₁_ = _⟦⊕⟧_ {τ}
_−₁_ : ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_−₁_ = _⟦⊝⟧_ {τ}
infixl 6 _✚₁_ _−₁_
_≃₁_ : Change τ → ⟦ ΔType τ ⟧ → Set
_≃₁_ = _≈_ {τ}
infix 4 _≃₁_
_✚₀_ : ⟦ σ ⟧ → ⟦ ΔType σ ⟧ → ⟦ σ ⟧
_✚₀_ = _⟦⊕⟧_ {σ}
_−₀_ : ⟦ σ ⟧ → ⟦ σ ⟧ → ⟦ ΔType σ ⟧
_−₀_ = _⟦⊝⟧_ {σ}
infixl 6 _✚₀_ _−₀_
_≃₀_ : Change σ → ⟦ ΔType σ ⟧ → Set
_≃₀_ = _≈_ {σ}
infix 4 _≃₀_
compatible : ∀ {Γ} → ΔEnv Γ → ⟦ ΔContext Γ ⟧ → Set
compatible {∅} ∅ ∅ = ⊤
compatible {τ • Γ} (cons v Δv _ • ρ) (Δv′ • v′ • ρ′) =
Triple (v ≡ v′) (λ _ → Δv ≈₍ τ ₎ Δv′) (λ _ _ → compatible ρ ρ′)
-- If a program implements a specification, then certain things
-- proven about the specification carry over to the programs.
carry-over : ∀ {τ}
{v : ⟦ τ ⟧} {Δv : Change τ} (R[v,Δv] : valid {τ} v Δv)
{Δv′ : ⟦ ΔType τ ⟧} (Δv≈Δv′ : Δv ≈₍ τ ₎ Δv′) →
let open Disambiguation τ in
v ⊞₍ τ ₎ Δv ≡ v ✚ Δv′
u⊟v≈u⊝v : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
let open Disambiguation τ in
u ⊟₍ τ ₎ v ≃ u − v
u⊟v≈u⊝v {base base-int} = refl
u⊟v≈u⊝v {base base-bag} = refl
u⊟v≈u⊝v {σ ⇒ τ} {g} {f} = result where
open FunctionDisambiguation σ τ
result : (w : ⟦ σ ⟧) (Δw : Change σ) → valid {σ} w Δw →
(Δw′ : ⟦ ΔType σ ⟧) → Δw ≈₍ σ ₎ Δw′ →
g (w ⊞₍ σ ₎ Δw) ⊟₍ τ ₎ f w ≃₁ g (w ✚₀ Δw′) −₁ f w
result w Δw R[w,Δw] Δw′ Δw≈Δw′
rewrite carry-over {σ} {w} R[w,Δw] Δw≈Δw′ =
u⊟v≈u⊝v {τ} {g (w ✚₀ Δw′)} {f w}
carry-over {base base-int} {v} _ Δv≈Δv′ = cong (_+_ v) Δv≈Δv′
carry-over {base base-bag} {v} _ Δv≈Δv′ = cong (_++_ v) Δv≈Δv′
carry-over {σ ⇒ τ} {f} {Δf} R[f,Δf] {Δf′} Δf≈Δf′ =
ext (λ v →
let
open FunctionDisambiguation σ τ
V = R[v,u-v] {σ} {v} {v}
S = u⊟v≈u⊝v {σ} {v} {v}
in
carry-over {τ} {f v}
{Δf (nil-valid-change σ v)} (proj₁ (R[f,Δf] (nil-valid-change σ v)))
{Δf′ v (v −₀ v)}
(Δf≈Δf′ v (v ⊟₍ σ ₎ v) V (v −₀ v) S))
-- A property relating `ignore` and the subcontext relation Γ≼ΔΓ
⟦Γ≼ΔΓ⟧ : ∀ {Γ} {ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧}
(C : compatible ρ ρ′) → ignore ρ ≡ ⟦ Γ≼ΔΓ ⟧ ρ′
⟦Γ≼ΔΓ⟧ {∅} {∅} {∅} _ = refl
⟦Γ≼ΔΓ⟧ {τ • Γ} {cons v dv _ • ρ} {dv′ • v′ • ρ′}
(cons v≡v′ _ C) = cong₂ _•_ v≡v′ (⟦Γ≼ΔΓ⟧ C)
-- A specialization of the soundness of weakening
⟦fit⟧ : ∀ {τ Γ} (t : Term Γ τ)
{ρ : ΔEnv Γ} {ρ′ : ⟦ ΔContext Γ ⟧} (C : compatible ρ ρ′) →
⟦ t ⟧ (ignore ρ) ≡ ⟦ fit t ⟧ ρ′
⟦fit⟧ t {ρ} {ρ′} C =
trans (cong ⟦ t ⟧ (⟦Γ≼ΔΓ⟧ C)) (sym (weaken-sound t ρ′))
|
Change argument order of carry-over.
|
Change argument order of carry-over.
This commit prepares for uncurrying cary-over.
Old-commit-hash: e1a21bdcbe7ee2b217917bd6ac9dfc576487102a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
9028d1ebd71caa01d4b8a57cbb884b32e787ebe1
|
total.agda
|
total.agda
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import meaning
open import IlcModel
open import Changes
open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
-- LIFTING terms into Δ-Contexts
lift-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) τ
lift-var this = that this
lift-var (that x) = that (that (lift-var x))
lift-var′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Var Γ τ → Var (Δ-Context′ Γ Γ′) τ
lift-var′ ∅ x = lift-var x
lift-var′ (τ • Γ′) this = this
lift-var′ (τ • Γ′) (that x) = that (lift-var′ Γ′ x)
lift-var-3 : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context (Δ-Context Γ)) τ
lift-var-3 this = that (that (that this))
lift-var-3 (that x) = that (that (that (that (lift-var-3 x))))
lift-term′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Term Γ τ → Term (Δ-Context′ Γ Γ′) τ
lift-term′ Γ′ (abs t) = abs (lift-term′ (_ • Γ′) t)
lift-term′ Γ′ (app t₁ t₂) = app (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂)
lift-term′ Γ′ (var x) = var (lift-var′ Γ′ x)
lift-term′ Γ′ true = true
lift-term′ Γ′ false = false
lift-term′ Γ′ (if t₁ t₂ t₃) = if (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂) (lift-term′ Γ′ t₃)
lift-term′ {.(Δ-Context Γ)} {.(Δ-Type τ)} Γ′ (Δ {Γ} {τ} t) = weakenMore t
where
open import Relation.Binary.PropositionalEquality using (sym)
doWeakenMore : ∀ Γprefix Γrest {τ} →
Term (Γprefix ⋎ Γrest) (Δ-Type τ) →
Term (Γprefix ⋎ Δ-Context Γrest) (Δ-Type τ)
doWeakenMore Γprefix ∅ t₁ = t₁
doWeakenMore ∅ (τ₂ • Γrest) t₁ =
weakenOne ∅ (Δ-Type τ₂) (doWeakenMore (τ₂ • ∅) Γrest t₁)
doWeakenMore Γprefix (τ₂ • Γrest) {τ} t₁ =
weakenOne Γprefix (Δ-Type τ₂)
(substTerm (sym (move-prefix Γprefix τ₂ (Δ-Context Γrest)))
(doWeakenMore (Γprefix ⋎ (τ₂ • ∅)) Γrest
(substTerm (move-prefix Γprefix τ₂ Γrest) t₁)))
prefix : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
prefix Γ Γ′ = take (Δ-Context Γ) Γ′
rest : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
rest Γ Γ′ = drop (Δ-Context Γ) Γ′
weakenMore2 : ∀ Γ Γ′ {τ} →
Term Γ τ →
Term (prefix Γ Γ′ ⋎ Δ-Context (rest Γ Γ′)) (Δ-Type τ)
weakenMore2 Γ Γ′ t =
doWeakenMore (prefix Γ Γ′) (rest Γ Γ′) (
substTerm (sym (take-drop (Δ-Context Γ) Γ′)) (Δ t))
weakenMore : --∀ {Γ τ} Γ′ →
Term Γ τ → Term (Δ-Context′ (Δ-Context Γ) Γ′) (Δ-Type τ)
weakenMore t =
substTerm
(sym (take-⋎-Δ-Context-drop-Δ-Context′ (Δ-Context Γ) Γ′))
(weakenMore2 Γ Γ′ t)
{-
weakenMore2 : ∀ Γ Γ′ {τ} →
Term Γ τ →
Term (prefix Γ Γ′ ⋎ Δ-Context (rest Γ Γ′)) (Δ-Type τ)
weakenMore2 ∅ ∅ t₁ = Δ t₁
weakenMore2 (τ₁ • Γ₁) ∅ t₁ =
weakenOne ∅ (Δ-Type (Δ-Type τ₁))
(weakenOne (Δ-Type τ₁ • ∅) (Δ-Type τ₁) {!
weakenMore2
(Δ {τ₁ • Γ₁} {τ} t₁)!})
--(weakenMore2 {τ₁ • Γ₁} (Δ-Type τ₁ • τ₁ • ∅) t₁ ))
-- (Δ {τ₁ • Γ₁} {τ} t₁)))
weakenMore2 (τ₁ • Γ₁) (.(Δ-Type τ₁) • Γ′₁) t₁ = {!!}
-}
lift-term′ {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) τ
lift-term = lift-term′ ∅
-- PROPERTIES of lift-term
lift-var-ignore : ∀ {Γ τ} (ρ : ⟦ Δ-Context Γ ⟧) (x : Var Γ τ) →
⟦ lift-var x ⟧ ρ ≡ ⟦ x ⟧ (ignore ρ)
lift-var-ignore (v • dv • ρ) this = ≡-refl
lift-var-ignore (v • dv • ρ) (that x) = lift-var-ignore ρ x
lift-var-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) (ρ : ⟦ Δ-Context′ Γ Γ′ ⟧) (x : Var Γ τ) →
⟦ lift-var′ Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (ignore′ Γ′ ρ)
lift-var-ignore′ ∅ ρ x = lift-var-ignore ρ x
lift-var-ignore′ (τ • Γ′) (v • ρ) this = ≡-refl
lift-var-ignore′ (τ • Γ′) (v • ρ) (that x) = lift-var-ignore′ Γ′ ρ x
lift-term-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) {ρ : ⟦ Δ-Context′ Γ Γ′ ⟧} (t : Term Γ τ) →
⟦ lift-term′ Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (ignore′ Γ′ ρ)
lift-term-ignore′ Γ′ (abs t) =
ext (λ v → lift-term-ignore′ (_ • Γ′) t)
lift-term-ignore′ Γ′ (app t₁ t₂) =
≡-app (lift-term-ignore′ Γ′ t₁) (lift-term-ignore′ Γ′ t₂)
lift-term-ignore′ Γ′ (var x) = lift-var-ignore′ Γ′ _ x
lift-term-ignore′ Γ′ true = ≡-refl
lift-term-ignore′ Γ′ false = ≡-refl
lift-term-ignore′ Γ′ {ρ} (if t₁ t₂ t₃)
with ⟦ lift-term′ Γ′ t₁ ⟧ ρ
| ⟦ t₁ ⟧ (ignore′ Γ′ ρ)
| lift-term-ignore′ Γ′ {ρ} t₁
... | true | true | bool = lift-term-ignore′ Γ′ t₂
... | false | false | bool = lift-term-ignore′ Γ′ t₃
lift-term-ignore′ Γ′ (Δ t) = {!!}
lift-term-ignore′ _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term-ignore : ∀ {Γ τ} {ρ : ⟦ Δ-Context Γ ⟧} (t : Term Γ τ) →
⟦ lift-term t ⟧ ρ ≡ ⟦ t ⟧ (ignore ρ)
lift-term-ignore = lift-term-ignore′ ∅
-- PROPERTIES of Δ
Δ-abs : ∀ {Γ τ₁ τ₂} (t : Term (τ₁ • Γ) τ₂) →
Δ (abs t) ≈ abs (abs (Δ t))
Δ-abs t = ext (λ ρ → ≡-refl)
Δ-app : ∀ {Γ τ₁ τ₂} (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) →
Δ (app t₁ t₂) ≈ app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
Δ-app t₁ t₂ = ≈-sym (ext (λ ρ →
begin
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ lift-term t₂ ⟧ ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term t₂ ⟧ ρ))
≡⟨ ≡-cong
(λ x →
diff
(⟦ t₁ ⟧ (update ρ)
(apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x))
(⟦ t₁ ⟧ (ignore ρ) x))
(lift-term-ignore {ρ = ρ} t₂) ⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ t₂ ⟧ (ignore ρ))))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨ ≡-cong
(λ x →
diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))))
(apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩
diff
(⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var this = this
derive-var (that x) = that (that (derive-var x))
diff-term : ∀ {Γ τ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term = {!!}
apply-term : ∀ {Γ τ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term = {!!}
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = {!!}
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = {!!}
derive-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) (Δ-Type τ)
derive-term (abs t) = abs (abs (derive-term t))
derive-term (app t₁ t₂) = app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
derive-term (var x) = var (derive-var x)
derive-term true = false
derive-term false = false
derive-term (if c t e) =
if ((derive-term c) and (lift-term c))
(diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))
(if ((derive-term c) and (lift-term (! c)))
(diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))
(if (lift-term c)
(derive-term t)
(derive-term e)))
derive-term (Δ t) = Δ (derive-term t)
derive-term (weakenOne Γ₁ τ₂ {Γ₃} t) =
substTerm (Δ-Context-⋎-expanded Γ₁ τ₂ Γ₃)
(weakenOne (Δ-Context Γ₁) (Δ-Type τ₂)
(weakenOne (Δ-Context Γ₁) τ₂
(substTerm (Δ-Context-⋎ Γ₁ Γ₃)
(derive-term t))))
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ τ} → (t : Term Γ τ) →
Δ t ≈ derive-term t
derive-term-correct {Γ} (abs t) =
begin
Δ (abs t)
≈⟨ Δ-abs t ⟩
abs (abs (Δ t))
≈⟨ ≈-abs (≈-abs (derive-term-correct t)) ⟩
abs (abs (derive-term t))
≈⟨ ≈-refl ⟩
derive-term (abs t)
∎ where open ≈-Reasoning
derive-term-correct (app t₁ t₂) =
begin
Δ (app t₁ t₂)
≈⟨ Δ-app t₁ t₂ ⟩
app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
≈⟨ ≈-app (≈-app (derive-term-correct t₁) ≈-refl) (derive-term-correct t₂) ⟩
app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
≈⟨ ≈-refl ⟩
derive-term (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct (var x) = ext (λ ρ → derive-var-correct ρ x)
derive-term-correct true = ext (λ ρ → ≡-refl)
derive-term-correct false = ext (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
derive-term-correct (weakenOne _ _ t) = {!!}
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
import Relation.Binary as B
open import Relation.Binary using
(IsEquivalence; Setoid; Reflexive; Symmetric; Transitive)
import Relation.Binary.EqReasoning as EqR
open import Relation.Nullary using (¬_)
open import meaning
open import IlcModel
open import Changes
open import ChangeContexts
open import binding Type ⟦_⟧Type
open import TotalTerms
-- LIFTING terms into Δ-Contexts
lift-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) τ
lift-var this = that this
lift-var (that x) = that (that (lift-var x))
lift-var′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Var Γ τ → Var (Δ-Context′ Γ Γ′) τ
lift-var′ ∅ x = lift-var x
lift-var′ (τ • Γ′) this = this
lift-var′ (τ • Γ′) (that x) = that (lift-var′ Γ′ x)
lift-term′ : ∀ {Γ τ} → (Γ′ : Prefix Γ) → Term Γ τ → Term (Δ-Context′ Γ Γ′) τ
lift-term′ Γ′ (abs t) = abs (lift-term′ (_ • Γ′) t)
lift-term′ Γ′ (app t₁ t₂) = app (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂)
lift-term′ Γ′ (var x) = var (lift-var′ Γ′ x)
lift-term′ Γ′ true = true
lift-term′ Γ′ false = false
lift-term′ Γ′ (if t₁ t₂ t₃) = if (lift-term′ Γ′ t₁) (lift-term′ Γ′ t₂) (lift-term′ Γ′ t₃)
lift-term′ {.(Δ-Context Γ)} {.(Δ-Type τ)} Γ′ (Δ {Γ} {τ} t) = weakenMore t
where
open import Relation.Binary.PropositionalEquality using (sym)
doWeakenMore : ∀ Γprefix Γrest {τ} →
Term (Γprefix ⋎ Γrest) (Δ-Type τ) →
Term (Γprefix ⋎ Δ-Context Γrest) (Δ-Type τ)
doWeakenMore Γprefix ∅ t₁ = t₁
doWeakenMore ∅ (τ₂ • Γrest) t₁ =
weakenOne ∅ (Δ-Type τ₂) (doWeakenMore (τ₂ • ∅) Γrest t₁)
doWeakenMore Γprefix (τ₂ • Γrest) {τ} t₁ =
weakenOne Γprefix (Δ-Type τ₂)
(substTerm (sym (move-prefix Γprefix τ₂ (Δ-Context Γrest)))
(doWeakenMore (Γprefix ⋎ (τ₂ • ∅)) Γrest
(substTerm (move-prefix Γprefix τ₂ Γrest) t₁)))
prefix : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
prefix Γ Γ′ = take (Δ-Context Γ) Γ′
rest : ∀ Γ → (Γ′ : Prefix (Δ-Context Γ)) → Context
rest Γ Γ′ = drop (Δ-Context Γ) Γ′
weakenMore2 : ∀ Γ Γ′ {τ} →
Term Γ τ →
Term (prefix Γ Γ′ ⋎ Δ-Context (rest Γ Γ′)) (Δ-Type τ)
weakenMore2 Γ Γ′ t =
doWeakenMore (prefix Γ Γ′) (rest Γ Γ′) (
substTerm (sym (take-drop (Δ-Context Γ) Γ′)) (Δ t))
weakenMore : --∀ {Γ τ} Γ′ →
Term Γ τ → Term (Δ-Context′ (Δ-Context Γ) Γ′) (Δ-Type τ)
weakenMore t =
substTerm
(sym (take-⋎-Δ-Context-drop-Δ-Context′ (Δ-Context Γ) Γ′))
(weakenMore2 Γ Γ′ t)
lift-term′ {._} {_} _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) τ
lift-term = lift-term′ ∅
-- PROPERTIES of lift-term
lift-var-ignore : ∀ {Γ τ} (ρ : ⟦ Δ-Context Γ ⟧) (x : Var Γ τ) →
⟦ lift-var x ⟧ ρ ≡ ⟦ x ⟧ (ignore ρ)
lift-var-ignore (v • dv • ρ) this = ≡-refl
lift-var-ignore (v • dv • ρ) (that x) = lift-var-ignore ρ x
lift-var-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) (ρ : ⟦ Δ-Context′ Γ Γ′ ⟧) (x : Var Γ τ) →
⟦ lift-var′ Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (ignore′ Γ′ ρ)
lift-var-ignore′ ∅ ρ x = lift-var-ignore ρ x
lift-var-ignore′ (τ • Γ′) (v • ρ) this = ≡-refl
lift-var-ignore′ (τ • Γ′) (v • ρ) (that x) = lift-var-ignore′ Γ′ ρ x
lift-term-ignore′ : ∀ {Γ τ} →
(Γ′ : Prefix Γ) {ρ : ⟦ Δ-Context′ Γ Γ′ ⟧} (t : Term Γ τ) →
⟦ lift-term′ Γ′ t ⟧ ρ ≡ ⟦ t ⟧ (ignore′ Γ′ ρ)
lift-term-ignore′ Γ′ (abs t) =
ext (λ v → lift-term-ignore′ (_ • Γ′) t)
lift-term-ignore′ Γ′ (app t₁ t₂) =
≡-app (lift-term-ignore′ Γ′ t₁) (lift-term-ignore′ Γ′ t₂)
lift-term-ignore′ Γ′ (var x) = lift-var-ignore′ Γ′ _ x
lift-term-ignore′ Γ′ true = ≡-refl
lift-term-ignore′ Γ′ false = ≡-refl
lift-term-ignore′ Γ′ {ρ} (if t₁ t₂ t₃)
with ⟦ lift-term′ Γ′ t₁ ⟧ ρ
| ⟦ t₁ ⟧ (ignore′ Γ′ ρ)
| lift-term-ignore′ Γ′ {ρ} t₁
... | true | true | bool = lift-term-ignore′ Γ′ t₂
... | false | false | bool = lift-term-ignore′ Γ′ t₃
lift-term-ignore′ Γ′ (Δ t) = {!!}
lift-term-ignore′ _ (weakenOne _ _ {_} {._} _) = {!!}
lift-term-ignore : ∀ {Γ τ} {ρ : ⟦ Δ-Context Γ ⟧} (t : Term Γ τ) →
⟦ lift-term t ⟧ ρ ≡ ⟦ t ⟧ (ignore ρ)
lift-term-ignore = lift-term-ignore′ ∅
-- PROPERTIES of Δ
Δ-abs : ∀ {Γ τ₁ τ₂} (t : Term (τ₁ • Γ) τ₂) →
Δ (abs t) ≈ abs (abs (Δ t))
Δ-abs t = ext (λ ρ → ≡-refl)
Δ-app : ∀ {Γ τ₁ τ₂} (t₁ : Term Γ (τ₁ ⇒ τ₂)) (t₂ : Term Γ τ₁) →
Δ (app t₁ t₂) ≈ app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
Δ-app t₁ t₂ = ≈-sym (ext (λ ρ →
begin
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ lift-term t₂ ⟧ ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ lift-term t₂ ⟧ ρ))
≡⟨ ≡-cong
(λ x →
diff
(⟦ t₁ ⟧ (update ρ)
(apply (diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ))) x))
(⟦ t₁ ⟧ (ignore ρ) x))
(lift-term-ignore {ρ = ρ} t₂) ⟩
diff
(⟦ t₁ ⟧ (update ρ)
(apply
(diff (⟦ t₂ ⟧ (update ρ)) (⟦ t₂ ⟧ (ignore ρ)))
(⟦ t₂ ⟧ (ignore ρ))))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
≡⟨ ≡-cong
(λ x →
diff (⟦ t₁ ⟧ (update ρ) x) (⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ))))
(apply-diff (⟦ t₂ ⟧ (ignore ρ)) (⟦ t₂ ⟧ (update ρ))) ⟩
diff
(⟦ t₁ ⟧ (update ρ) (⟦ t₂ ⟧ (update ρ)))
(⟦ t₁ ⟧ (ignore ρ) (⟦ t₂ ⟧ (ignore ρ)))
∎)) where open ≡-Reasoning
-- SYMBOLIC DERIVATION
derive-var : ∀ {Γ τ} → Var Γ τ → Var (Δ-Context Γ) (Δ-Type τ)
derive-var this = this
derive-var (that x) = that (that (derive-var x))
diff-term : ∀ {Γ τ} → Term Γ τ → Term Γ τ → Term Γ (Δ-Type τ)
diff-term = {!!}
apply-term : ∀ {Γ τ} → Term Γ (Δ-Type τ) → Term Γ τ → Term Γ τ
apply-term = {!!}
_and_ : ∀ {Γ} → Term Γ bool → Term Γ bool → Term Γ bool
a and b = {!!}
!_ : ∀ {Γ} → Term Γ bool → Term Γ bool
! x = {!!}
derive-term : ∀ {Γ τ} → Term Γ τ → Term (Δ-Context Γ) (Δ-Type τ)
derive-term (abs t) = abs (abs (derive-term t))
derive-term (app t₁ t₂) = app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
derive-term (var x) = var (derive-var x)
derive-term true = false
derive-term false = false
derive-term (if c t e) =
if ((derive-term c) and (lift-term c))
(diff-term (apply-term (derive-term e) (lift-term e)) (lift-term t))
(if ((derive-term c) and (lift-term (! c)))
(diff-term (apply-term (derive-term t) (lift-term t)) (lift-term e))
(if (lift-term c)
(derive-term t)
(derive-term e)))
derive-term (Δ t) = Δ (derive-term t)
derive-term (weakenOne Γ₁ τ₂ {Γ₃} t) =
substTerm (Δ-Context-⋎-expanded Γ₁ τ₂ Γ₃)
(weakenOne (Δ-Context Γ₁) (Δ-Type τ₂)
(weakenOne (Δ-Context Γ₁) τ₂
(substTerm (Δ-Context-⋎ Γ₁ Γ₃)
(derive-term t))))
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ τ} → (t : Term Γ τ) →
Δ t ≈ derive-term t
derive-term-correct {Γ} (abs t) =
begin
Δ (abs t)
≈⟨ Δ-abs t ⟩
abs (abs (Δ t))
≈⟨ ≈-abs (≈-abs (derive-term-correct t)) ⟩
abs (abs (derive-term t))
≈⟨ ≈-refl ⟩
derive-term (abs t)
∎ where open ≈-Reasoning
derive-term-correct (app t₁ t₂) =
begin
Δ (app t₁ t₂)
≈⟨ Δ-app t₁ t₂ ⟩
app (app (Δ t₁) (lift-term t₂)) (Δ t₂)
≈⟨ ≈-app (≈-app (derive-term-correct t₁) ≈-refl) (derive-term-correct t₂) ⟩
app (app (derive-term t₁) (lift-term t₂)) (derive-term t₂)
≈⟨ ≈-refl ⟩
derive-term (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct (var x) = ext (λ ρ → derive-var-correct ρ x)
derive-term-correct true = ext (λ ρ → ≡-refl)
derive-term-correct false = ext (λ ρ → ≡-refl)
derive-term-correct (if t₁ t₂ t₃) = {!!}
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
derive-term-correct (weakenOne _ _ t) = {!!}
|
Revert "agda/...: misc (to revert)"
|
Revert "agda/...: misc (to revert)"
This reverts commit ae3a9db6fb87ddc01573ae236f8850b322bec8db.
Old-commit-hash: fa2ae9742c4282275b8f242a197b35ef1acf5932
|
Agda
|
mit
|
inc-lc/ilc-agda
|
2e98871b50826699f6f16a2718f8f2361a559c4d
|
Thesis/ANormalUntyped.agda
|
Thesis/ANormalUntyped.agda
|
module Thesis.ANormalUntyped where
open import Data.Nat.Base
open import Data.Integer.Base
open import Relation.Binary.PropositionalEquality
{- Typed deBruijn indexes for untyped languages. -}
-- Using a record gives an eta rule saying that all types are equal.
record Type : Set where
constructor Uni
open import Base.Syntax.Context Type public
data Term (Γ : Context) : Set where
var : (x : Var Γ Uni) →
Term Γ
lett : (f : Var Γ Uni) → (x : Var Γ Uni) → Term (Uni • Γ) → Term Γ
{-
deriveC Δ (lett f x t) = dlett df x dx
-}
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- cvar : (x : Var Γ τ) Δ →
-- ΔCTerm Γ τ Δ
-- clett : ∀ {σ τ₁ κ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) →
-- ΔCTerm (τ₁ • Γ) τ (? • Δ) →
-- ΔCTerm Γ τ Δ
weaken-term : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ →
Term Γ₂
weaken-term Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x)
weaken-term Γ₁≼Γ₂ (lett f x t) = lett (weaken-var Γ₁≼Γ₂ f) (weaken-var Γ₁≼Γ₂ x) (weaken-term (keep _ • Γ₁≼Γ₂) t)
data Fun (Γ : Context) : Set where
term : Term Γ → Fun Γ
abs : ∀ {σ} → Fun (σ • Γ) → Fun Γ
weaken-fun : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Fun Γ₁ →
Fun Γ₂
weaken-fun Γ₁≼Γ₂ (term x) = term (weaken-term Γ₁≼Γ₂ x)
weaken-fun Γ₁≼Γ₂ (abs f) = abs (weaken-fun (keep _ • Γ₁≼Γ₂) f)
data Val : Type → Set
-- data Val (τ : Type) : Set
open import Base.Denotation.Environment Type Val public
open import Base.Data.DependentList public
-- data Val (τ : Type) where
data Val where
closure : ∀ {Γ} → (t : Fun (Uni • Γ)) → (ρ : ⟦ Γ ⟧Context) → Val Uni
intV : ∀ (n : ℤ) → Val Uni
-- ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧Context → ⟦ τ ⟧Type
-- ⟦ var x ⟧Term ρ = ⟦ x ⟧Var ρ
-- ⟦ lett f x t ⟧Term ρ = ⟦ t ⟧Term (⟦ f ⟧Var ρ (⟦ x ⟧Var ρ) • ρ)
data ErrVal : Set where
Done : (v : Val Uni) → ErrVal
Error : ErrVal
TimeOut : ErrVal
_>>=_ : ErrVal → (Val Uni → ErrVal) → ErrVal
Done v >>= f = f v
Error >>= f = Error
TimeOut >>= f = TimeOut
Res : Set
Res = ℕ → ErrVal
eval-fun : ∀ {Γ} → Fun Γ → ⟦ Γ ⟧Context → Res
eval-term : ∀ {Γ} → Term Γ → ⟦ Γ ⟧Context → Res
apply : Val Uni → Val Uni → Res
apply (closure f ρ) a n = eval-fun f (a • ρ) n
apply (intV _) a n = Error
eval-term t ρ zero = TimeOut
eval-term (var x) ρ (suc n) = Done (⟦ x ⟧Var ρ)
eval-term (lett f x t) ρ (suc n) = apply (⟦ f ⟧Var ρ) (⟦ x ⟧Var ρ) n >>= (λ v → eval-term t (v • ρ) n)
eval-fun (term t) ρ n = eval-term t ρ n
eval-fun (abs f) ρ n = Done (closure f ρ)
-- Erasure from typed to untyped values.
import Thesis.ANormalBigStep as T
erase-type : T.Type → Type
erase-type _ = Uni
erase-val : ∀ {τ} → T.Val τ → Val (erase-type τ)
erase-errVal : ∀ {τ} → T.ErrVal τ → ErrVal
erase-errVal (T.Done v) = Done (erase-val v)
erase-errVal T.Error = Error
erase-errVal T.TimeOut = TimeOut
erase-res : ∀ {τ} → T.Res τ → Res
erase-res r n = erase-errVal (r n)
erase-ctx : T.Context → Context
erase-ctx ∅ = ∅
erase-ctx (τ • Γ) = erase-type τ • (erase-ctx Γ)
erase-env : ∀ {Γ} → T.Op.⟦ Γ ⟧Context → ⟦ erase-ctx Γ ⟧Context
erase-env ∅ = ∅
erase-env (v • ρ) = erase-val v • erase-env ρ
erase-var : ∀ {Γ τ} → T.Var Γ τ → Var (erase-ctx Γ) (erase-type τ)
erase-var T.this = this
erase-var (T.that x) = that (erase-var x)
erase-term : ∀ {Γ τ} → T.Term Γ τ → Term (erase-ctx Γ)
erase-term (T.var x) = var (erase-var x)
erase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)
erase-fun : ∀ {Γ τ} → T.Fun Γ τ → Fun (erase-ctx Γ)
erase-fun (T.term x) = term (erase-term x)
erase-fun (T.abs f) = abs (erase-fun f)
erase-val (T.closure t ρ) = closure (erase-fun t) (erase-env ρ)
erase-val (T.intV n) = intV n
-- Different erasures commute.
erasure-commute-var : ∀ {Γ τ} (x : T.Var Γ τ) ρ →
erase-val (T.Op.⟦ x ⟧Var ρ) ≡ ⟦ erase-var x ⟧Var (erase-env ρ)
erasure-commute-var T.this (v • ρ) = refl
erasure-commute-var (T.that x) (v • ρ) = erasure-commute-var x ρ
erase-bind : ∀ {σ τ Γ} a (t : T.Term (σ • Γ) τ) ρ n → erase-errVal (a T.>>= (λ v → T.eval-term t (v • ρ) n)) ≡ erase-errVal a >>= (λ v → eval-term (erase-term t) (v • erase-env ρ) n)
erasure-commute-fun : ∀ {Γ τ} (t : T.Fun Γ τ) ρ n →
erase-errVal (T.eval-fun t ρ n) ≡ eval-fun (erase-fun t) (erase-env ρ) n
erasure-commute-apply : ∀ {σ τ} (f : T.Val (σ T.⇒ τ)) a n → erase-errVal (T.apply f a n) ≡ apply (erase-val f) (erase-val a) n
erasure-commute-apply {σ} (T.closure t ρ) a n = erasure-commute-fun t (a • ρ) n
erasure-commute-term : ∀ {Γ τ} (t : T.Term Γ τ) ρ n →
erase-errVal (T.eval-term t ρ n) ≡ eval-term (erase-term t) (erase-env ρ) n
erasure-commute-fun (T.term t) ρ n = erasure-commute-term t ρ n
erasure-commute-fun (T.abs t) ρ n = refl
erasure-commute-term t ρ zero = refl
erasure-commute-term (T.var x) ρ (ℕ.suc n) = cong Done (erasure-commute-var x ρ)
erasure-commute-term (T.lett f x t) ρ (ℕ.suc n) rewrite erase-bind (T.apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n) t ρ n | erasure-commute-apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n | erasure-commute-var f ρ | erasure-commute-var x ρ = refl
erase-bind (T.Done v) t ρ n = erasure-commute-term t (v • ρ) n
erase-bind T.Error t ρ n = refl
erase-bind T.TimeOut t ρ n = refl
|
module Thesis.ANormalUntyped where
open import Data.Empty
open import Data.Product
open import Data.Nat.Base
import Data.Integer.Base as I
open I using (ℤ)
open import Data.Integer.Base using (ℤ)
open import Relation.Binary.PropositionalEquality
{- Typed deBruijn indexes for untyped languages. -}
-- Using a record gives an eta rule saying that all types are equal.
record Type : Set where
constructor Uni
record DType : Set where
constructor DUni
open import Base.Syntax.Context Type public
import Base.Syntax.Context DType as DC
data Term (Γ : Context) : Set where
var : (x : Var Γ Uni) →
Term Γ
lett : (f : Var Γ Uni) → (x : Var Γ Uni) → Term (Uni • Γ) → Term Γ
ΔΔ : Context → DC.Context
ΔΔ ∅ = ∅
ΔΔ (τ • Γ) = DUni • ΔΔ Γ
derive-dvar : ∀ {Δ} → (x : Var Δ Uni) → DC.Var (ΔΔ Δ) DUni
derive-dvar this = DC.this
derive-dvar (that x) = DC.that (derive-dvar x)
data DTerm : (Δ : Context) → Set where
dvar : ∀ {Δ} (x : DC.Var (ΔΔ Δ) DUni) →
DTerm Δ
dlett : ∀ {Δ} →
(f : Var Δ Uni) →
(x : Var Δ Uni) →
(t : Term (Uni • Δ)) →
(df : DC.Var (ΔΔ Δ) DUni) →
(dx : DC.Var (ΔΔ Δ) DUni) →
(dt : DTerm (Uni • Δ)) →
DTerm Δ
derive-dterm : ∀ {Δ} → (t : Term Δ) → DTerm Δ
derive-dterm (var x) = dvar (derive-dvar x)
derive-dterm (lett f x t) =
dlett f x t (derive-dvar f) (derive-dvar x) (derive-dterm t)
{-
deriveC Δ (lett f x t) = dlett df x dx
-}
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- data ΔCTerm (Γ : Context) (τ : Type) (Δ : Context) : Set where
-- cvar : (x : Var Γ τ) Δ →
-- ΔCTerm Γ τ Δ
-- clett : ∀ {σ τ₁ κ} → (f : Var Γ (σ ⇒ τ₁)) → (x : Var Γ σ) →
-- ΔCTerm (τ₁ • Γ) τ (? • Δ) →
-- ΔCTerm Γ τ Δ
weaken-term : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ →
Term Γ₂
weaken-term Γ₁≼Γ₂ (var x) = var (weaken-var Γ₁≼Γ₂ x)
weaken-term Γ₁≼Γ₂ (lett f x t) = lett (weaken-var Γ₁≼Γ₂ f) (weaken-var Γ₁≼Γ₂ x) (weaken-term (keep _ • Γ₁≼Γ₂) t)
-- I don't necessarily recommend having a separate syntactic category for
-- functions, but we should prove a fundamental lemma for them too, somehow.
-- I'll probably end up with some ANF allowing lambdas to do the semantics.
data Fun (Γ : Context) : Set where
term : Term Γ → Fun Γ
abs : ∀ {σ} → Fun (σ • Γ) → Fun Γ
data DFun (Δ : Context) : Set where
dterm : DTerm Δ → DFun Δ
dabs : DFun (Uni • Δ) → DFun Δ
derive-dfun : ∀ {Δ} → (t : Fun Δ) → DFun Δ
derive-dfun (term t) = dterm (derive-dterm t)
derive-dfun (abs f) = dabs (derive-dfun f)
weaken-fun : ∀ {Γ₁ Γ₂} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Fun Γ₁ →
Fun Γ₂
weaken-fun Γ₁≼Γ₂ (term x) = term (weaken-term Γ₁≼Γ₂ x)
weaken-fun Γ₁≼Γ₂ (abs f) = abs (weaken-fun (keep _ • Γ₁≼Γ₂) f)
data Val : Type → Set
data DVal : DType → Set
-- data Val (τ : Type) : Set
open import Base.Denotation.Environment Type Val public
open import Base.Data.DependentList public
import Base.Denotation.Environment DType DVal as D
-- data Val (τ : Type) where
data Val where
closure : ∀ {Γ} → (t : Fun (Uni • Γ)) → (ρ : ⟦ Γ ⟧Context) → Val Uni
intV : ∀ (n : ℕ) → Val Uni
data DVal where
dclosure : ∀ {Γ} → (dt : DFun (Uni • Γ)) → (ρ : ⟦ Γ ⟧Context) → (dρ : D.⟦ ΔΔ Γ ⟧Context) → DVal DUni
dintV : ∀ (n : ℤ) → DVal DUni
ChΔ : ∀ (Δ : Context) → Set
ChΔ Δ = D.⟦ ΔΔ Δ ⟧Context
-- ⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧Context → ⟦ τ ⟧Type
-- ⟦ var x ⟧Term ρ = ⟦ x ⟧Var ρ
-- ⟦ lett f x t ⟧Term ρ = ⟦ t ⟧Term (⟦ f ⟧Var ρ (⟦ x ⟧Var ρ) • ρ)
-- XXX separate syntax is a bit dangerous. Also, do I want to be so accurate relative to the original model?
data _F⊢_↓[_]_ {Γ} (ρ : ⟦ Γ ⟧Context) : Fun Γ → ℕ → Val Uni → Set where
abs : ∀ {t : Fun (Uni • Γ)} →
ρ F⊢ abs t ↓[ 0 ] closure t ρ
data _⊢_↓[_]_ {Γ} (ρ : ⟦ Γ ⟧Context) : Term Γ → ℕ → Val Uni → Set where
var : ∀ (x : Var Γ Uni) →
ρ ⊢ var x ↓[ 0 ] (⟦ x ⟧Var ρ)
lett : ∀ n1 n2 {Γ' ρ′ v1 v2 v3} {f x t t'} →
ρ ⊢ var f ↓[ 0 ] closure {Γ'} t' ρ′ →
ρ ⊢ var x ↓[ 0 ] v1 →
(v1 • ρ′) F⊢ t' ↓[ n1 ] v2 →
(v2 • ρ) ⊢ t ↓[ n2 ] v3 →
ρ ⊢ lett f x t ↓[ suc (suc (n1 + n2)) ] v3
-- lit : ∀ n →
-- ρ ⊢ const (lit n) ↓[ 0 ] intV n
-- data _D_⊢_↓[_]_ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : D.⟦ ΔΔ Γ ⟧Context) : DTerm Γ → ℕ → DVal DUni → Set where
-- dvar : ∀ (x : DC.Var (ΔΔ Γ) DUni) →
-- ρ D dρ ⊢ dvar x ↓[ 0 ] (D.⟦ x ⟧Var dρ)
-- dlett : ∀ n1 n2 n3 n4 {Γ' ρ′ ρ'' dρ' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} →
-- ρ ⊢ var f ↓[ 0 ] closure {Γ'} t' ρ′ →
-- ρ ⊢ var x ↓[ 0 ] v1 →
-- (v1 • ρ′) F⊢ t' ↓[ n1 ] v2 →
-- (v2 • ρ) ⊢ t ↓[ n2 ] v3 →
-- -- With a valid input ρ' and ρ'' coincide? Varies among plausible validity
-- -- definitions.
-- ρ D dρ ⊢ dvar df ↓[ 0 ] dclosure {Γ'} dt' ρ'' dρ' →
-- ρ D dρ ⊢ dvar dx ↓[ 0 ] dv1 →
-- (v1 • ρ'') D (dv1 • dρ') F⊢ dt' ↓[ n3 ] dv2 →
-- (v2 • ρ) D (dv2 • dρ) ⊢ dt ↓[ n4 ] dv3 →
-- ρ D dρ ⊢ dlett f x t df dx dt ↓[ suc (suc (n1 + n2)) ] dv3
-- Do I need to damn count steps here? No.
data _D_F⊢_↓_ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) : DFun Γ → DVal DUni → Set where
dabs : ∀ {t : DFun (Uni • Γ)} →
ρ D dρ F⊢ dabs t ↓ dclosure t ρ dρ
data _D_⊢_↓_ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) : DTerm Γ → DVal DUni → Set where
dvar : ∀ (x : DC.Var (ΔΔ Γ) DUni) →
ρ D dρ ⊢ dvar x ↓ (D.⟦ x ⟧Var dρ)
dlett : ∀ n1 n2 {Γ' ρ′ ρ'' dρ' v1 v2 v3 dv1 dv2 dv3} {f x t df dx dt t' dt'} →
ρ ⊢ var f ↓[ 0 ] closure {Γ'} t' ρ′ →
ρ ⊢ var x ↓[ 0 ] v1 →
(v1 • ρ′) F⊢ t' ↓[ n1 ] v2 →
(v2 • ρ) ⊢ t ↓[ n2 ] v3 →
-- With a valid input ρ' and ρ'' coincide? Varies among plausible validity
-- definitions.
ρ D dρ ⊢ dvar df ↓ dclosure {Γ'} dt' ρ'' dρ' →
ρ D dρ ⊢ dvar dx ↓ dv1 →
(v1 • ρ'') D (dv1 • dρ') F⊢ dt' ↓ dv2 →
(v2 • ρ) D (dv2 • dρ) ⊢ dt ↓ dv3 →
ρ D dρ ⊢ dlett f x t df dx dt ↓ dv3
{-# TERMINATING #-} -- Dear lord. Why on Earth.
mutual
-- Single context? Yeah, good, that's what we want in the end...
-- Though we might want more flexibility till when we have replacement
-- changes.
rrelT3 : ∀ {Γ} (t1 : Fun Γ) (dt : DFun Γ) (t2 : Fun Γ) (ρ1 : ⟦ Γ ⟧Context) (dρ : ChΔ Γ) (ρ2 : ⟦ Γ ⟧Context) → ℕ → Set
rrelT3 t1 dt t2 ρ1 dρ ρ2 k =
(v1 v2 : Val Uni) →
∀ j n2 (j<k : j < k) →
(ρ1⊢t1↓[j]v1 : ρ1 F⊢ t1 ↓[ j ] v1) →
(ρ2⊢t2↓[n2]v2 : ρ2 F⊢ t2 ↓[ n2 ] v2) →
Σ[ dv ∈ DVal DUni ] Σ[ dn ∈ ℕ ]
ρ1 D dρ F⊢ dt ↓ dv ×
rrelV3 v1 dv v2 (k ∸ j)
rrelV3 : ∀ (v1 : Val Uni) (dv : DVal DUni) (v2 : Val Uni) → ℕ → Set
rrelV3 (intV v1) (dintV dv) (intV v2) n = dv I.+ (I.+ v1) ≡ (I.+ v2)
rrelV3 (closure {Γ1} t1 ρ1) (dclosure {ΔΓ} dt ρ' dρ) (closure {Γ2} t2 ρ2) n =
-- Require a proof that the two contexts match:
Σ ((Γ1 ≡ Γ2) × (Γ1 ≡ ΔΓ)) λ { (refl , refl) →
∀ (k : ℕ) (k<n : k < n) v1 dv v2 →
rrelV3 v1 dv v2 k →
rrelT3
t1
dt
t2
(v1 • ρ1)
(dv • dρ)
(v2 • ρ2)
k
}
rrelV3 _ _ _ n = ⊥
-- -- [_]τ from v1 to v2) :
-- -- [ τ ]τ dv from v1 to v2) =
-- -- data [_]Δ_from_to_ : ∀ Δ → ChΔ Δ → (ρ1 ρ2 : ⟦ Δ ⟧Context) → Set where
-- -- v∅ : [ ∅ ]Δ ∅ from ∅ to ∅
-- -- _v•_ : ∀ {τ Δ dv v1 v2 dρ ρ1 ρ2} →
-- -- (dvv : [ τ ]τ dv from v1 to v2) →
-- -- (dρρ : [ Δ ]Δ dρ from ρ1 to ρ2) →
-- -- [ τ • Δ ]Δ (dv • dρ) from (v1 • ρ1) to (v2 • ρ2)
-- data ErrVal : Set where
-- Done : (v : Val Uni) → ErrVal
-- Error : ErrVal
-- TimeOut : ErrVal
-- _>>=_ : ErrVal → (Val Uni → ErrVal) → ErrVal
-- Done v >>= f = f v
-- Error >>= f = Error
-- TimeOut >>= f = TimeOut
-- Res : Set
-- Res = ℕ → ErrVal
-- -- eval-fun : ∀ {Γ} → Fun Γ → ⟦ Γ ⟧Context → Res
-- -- eval-term : ∀ {Γ} → Term Γ → ⟦ Γ ⟧Context → Res
-- -- apply : Val Uni → Val Uni → Res
-- -- apply (closure f ρ) a n = eval-fun f (a • ρ) n
-- -- apply (intV _) a n = Error
-- -- eval-term t ρ zero = TimeOut
-- -- eval-term (var x) ρ (suc n) = Done (⟦ x ⟧Var ρ)
-- -- eval-term (lett f x t) ρ (suc n) = apply (⟦ f ⟧Var ρ) (⟦ x ⟧Var ρ) n >>= (λ v → eval-term t (v • ρ) n)
-- -- eval-fun (term t) ρ n = eval-term t ρ n
-- -- eval-fun (abs f) ρ n = Done (closure f ρ)
-- -- -- Erasure from typed to untyped values.
-- -- import Thesis.ANormalBigStep as T
-- -- erase-type : T.Type → Type
-- -- erase-type _ = Uni
-- -- erase-val : ∀ {τ} → T.Val τ → Val (erase-type τ)
-- -- erase-errVal : ∀ {τ} → T.ErrVal τ → ErrVal
-- -- erase-errVal (T.Done v) = Done (erase-val v)
-- -- erase-errVal T.Error = Error
-- -- erase-errVal T.TimeOut = TimeOut
-- -- erase-res : ∀ {τ} → T.Res τ → Res
-- -- erase-res r n = erase-errVal (r n)
-- -- erase-ctx : T.Context → Context
-- -- erase-ctx ∅ = ∅
-- -- erase-ctx (τ • Γ) = erase-type τ • (erase-ctx Γ)
-- -- erase-env : ∀ {Γ} → T.Op.⟦ Γ ⟧Context → ⟦ erase-ctx Γ ⟧Context
-- -- erase-env ∅ = ∅
-- -- erase-env (v • ρ) = erase-val v • erase-env ρ
-- -- erase-var : ∀ {Γ τ} → T.Var Γ τ → Var (erase-ctx Γ) (erase-type τ)
-- -- erase-var T.this = this
-- -- erase-var (T.that x) = that (erase-var x)
-- -- erase-term : ∀ {Γ τ} → T.Term Γ τ → Term (erase-ctx Γ)
-- -- erase-term (T.var x) = var (erase-var x)
-- -- erase-term (T.lett f x t) = lett (erase-var f) (erase-var x) (erase-term t)
-- -- erase-fun : ∀ {Γ τ} → T.Fun Γ τ → Fun (erase-ctx Γ)
-- -- erase-fun (T.term x) = term (erase-term x)
-- -- erase-fun (T.abs f) = abs (erase-fun f)
-- -- erase-val (T.closure t ρ) = closure (erase-fun t) (erase-env ρ)
-- -- erase-val (T.intV n) = intV n
-- -- -- Different erasures commute.
-- -- erasure-commute-var : ∀ {Γ τ} (x : T.Var Γ τ) ρ →
-- -- erase-val (T.Op.⟦ x ⟧Var ρ) ≡ ⟦ erase-var x ⟧Var (erase-env ρ)
-- -- erasure-commute-var T.this (v • ρ) = refl
-- -- erasure-commute-var (T.that x) (v • ρ) = erasure-commute-var x ρ
-- -- erase-bind : ∀ {σ τ Γ} a (t : T.Term (σ • Γ) τ) ρ n → erase-errVal (a T.>>= (λ v → T.eval-term t (v • ρ) n)) ≡ erase-errVal a >>= (λ v → eval-term (erase-term t) (v • erase-env ρ) n)
-- -- erasure-commute-fun : ∀ {Γ τ} (t : T.Fun Γ τ) ρ n →
-- -- erase-errVal (T.eval-fun t ρ n) ≡ eval-fun (erase-fun t) (erase-env ρ) n
-- -- erasure-commute-apply : ∀ {σ τ} (f : T.Val (σ T.⇒ τ)) a n → erase-errVal (T.apply f a n) ≡ apply (erase-val f) (erase-val a) n
-- -- erasure-commute-apply {σ} (T.closure t ρ) a n = erasure-commute-fun t (a • ρ) n
-- -- erasure-commute-term : ∀ {Γ τ} (t : T.Term Γ τ) ρ n →
-- -- erase-errVal (T.eval-term t ρ n) ≡ eval-term (erase-term t) (erase-env ρ) n
-- -- erasure-commute-fun (T.term t) ρ n = erasure-commute-term t ρ n
-- -- erasure-commute-fun (T.abs t) ρ n = refl
-- -- erasure-commute-term t ρ zero = refl
-- -- erasure-commute-term (T.var x) ρ (ℕ.suc n) = cong Done (erasure-commute-var x ρ)
-- -- erasure-commute-term (T.lett f x t) ρ (ℕ.suc n) rewrite erase-bind (T.apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n) t ρ n | erasure-commute-apply (T.Op.⟦ f ⟧Var ρ) (T.Op.⟦ x ⟧Var ρ) n | erasure-commute-var f ρ | erasure-commute-var x ρ = refl
-- -- erase-bind (T.Done v) t ρ n = erasure-commute-term t (v • ρ) n
-- -- erase-bind T.Error t ρ n = refl
-- -- erase-bind T.TimeOut t ρ n = refl
|
Write down real step-indexed logical relation for untyped terms
|
Write down real step-indexed logical relation for untyped terms
Sadly, Agda does not recognize this is terminating, because recursing on k with
k < n is not a structurally recursive call on a subterm. SAD!
|
Agda
|
mit
|
inc-lc/ilc-agda
|
7849a93dd727b32c2606d958173b08212260367d
|
Game/Transformation/ReceiptFreeness-CCA2d/Valid.agda
|
Game/Transformation/ReceiptFreeness-CCA2d/Valid.agda
|
{-# OPTIONS --copatterns #-}
open import Type
open import Function
open import Data.One
open import Data.Two
open import Data.Maybe
open import Data.Product
open import Data.Nat
open import Data.Vec hiding (_>>=_ ; _∈_)
open import Data.List as L
open import Data.Fin as Fin using (Fin)
open import Relation.Binary.PropositionalEquality.NP as ≡
open import Control.Strategy renaming (map to mapS)
open import Game.Challenge
import Game.ReceiptFreeness
import Game.IND-CCA2-dagger
import Game.IND-CPA-utils
import Data.List.Any
open Data.List.Any using (here; there)
open Data.List.Any.Membership-≡ using (_∈_ ; _∉_)
module Game.Transformation.ReceiptFreeness-CCA2d.Valid
(PubKey : ★)
(SecKey : ★)
-- Message = 𝟚
(CipherText : ★)
(SerialNumber : ★)
-- randomness supply for, encryption, key-generation, adversary, adversary state
(Rₑ Rₖ Rₐ : ★)
(#q : ℕ) (max#q : Fin #q)
(KeyGen : Rₖ → PubKey × SecKey)
(Enc : let Message = 𝟚 in
PubKey → Message → Rₑ → CipherText)
(Dec : let Message = 𝟚 in
SecKey → CipherText → Message)
(Check : let open Game.ReceiptFreeness PubKey SecKey CipherText SerialNumber Rₑ Rₖ Rₐ #q max#q KeyGen Enc Dec
in BB → Receipt → 𝟚)
(CheckMem : ∀ bb r → ✓ (Check bb r) → proj₁ (proj₂ r) ∉ L.map (proj₁ ∘ proj₂) bb)
-- (CheckEnc : ∀ pk m rₑ → Check (Enc pk m rₑ) ≡ 1₂)
where
_²' : ★ → ★
X ²' = X × X
r-sn : Receipt → SerialNumber
r-sn (_ , sn , _) = sn
module Simulator-Valid (RFA : RF.Adversary)(RFA-Valid : RF.Valid-Adversary RFA)
(WRONG-HYP : ∀ r r' → r-sn r ≡ r-sn r' → enc-co r ≡ enc-co r')
where
valid : CCA2†.Valid-Adversary (Simulator.A† RFA)
valid (rₐ , rgb) pk = Phase1 _ (RFA-Valid rₐ pk) where
open CCA2†.Valid-Adversary (rₐ , rgb) pk
module RFA = RF.Valid-Adversary rₐ pk
open Simulator RFA
open AdversaryParts rgb pk rₐ
-- could refine r more
-- {-
Phase2 : ∀ RF {bb i taA taB r} → r-sn (r 0₂) ∈ L.map r-sn bb → r-sn (r 1₂) ∈ L.map r-sn bb → RFA.Phase2-Valid r RF
→ Phase2-Valid (proj₂ ∘ proj₂ ∘ r) (mapS proj₁ (mitm-to-client-trans (MITM-phase 1₂ i bb (taA , taB)) RF))
Phase2 (ask REB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RBB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RTally cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask (RCO x) cont) r0 r1 ((r₀ , r₁) , RF-valid) = r₀ , r₁ , (λ r → Phase2 (cont _) r0 r1 (RF-valid _))
Phase2 (ask (Vote x) cont) {bb} r0 r1 RF-valid with Check bb x | CheckMem bb x
... | 0₂ | _ = Phase2 (cont _) r0 r1 (RF-valid _)
... | 1₂ | not-in-bb = (λ eq → not-in-bb _ {!subst (λ x → x ∈ L.map r-sn bb) eq!})
, {!!},
, λ r → Phase2 (cont _) (there r0) (there r1) (RF-valid _)
Phase2 (done x) r0 r1 RF-valid = RF-valid
Phase1 : ∀ RF {sn i bb taA taB} → RFA.Phase1-Valid sn RF
→ Phase1-Valid (mapS A†2 (mitm-to-client-trans (MITM-phase 0₂ i bb (taA , taB)) RF))
Phase1 (ask REB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RBB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RTally cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask (RCO x) cont) RF-valid r = Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) {bb = bb} RF-valid with Check bb x
Phase1 (ask (Vote x) cont) RF-valid | 1₂ = λ r → Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) RF-valid | 0₂ = Phase1 _ (RF-valid _)
Phase1 (done x) (sn₀∉sn , sn₁∉sn , RF-valid) cs = Phase2 (put-resp x (proj₂ (put-resp (hack-challenge x) cs) ))
(here refl) (there (here refl)) (RF-valid _)
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --copatterns #-}
open import Type
open import Function
open import Data.One
open import Data.Two
open import Data.Maybe
open import Data.Product
open import Data.Nat
open import Data.Vec hiding (_>>=_ ; _∈_)
open import Data.List as L
open import Data.Fin as Fin using (Fin)
open import Relation.Binary.PropositionalEquality.NP as ≡
open import Control.Strategy renaming (map to mapS)
open import Control.Strategy.Utils
open import Game.Challenge
import Game.ReceiptFreeness
import Game.IND-CCA2-dagger
import Game.IND-CCA2-dagger.Valid
import Game.IND-CPA-utils
import Data.List.Any
open Data.List.Any using (here; there)
open Data.List.Any.Membership-≡ using (_∈_ ; _∉_)
import Game.ReceiptFreeness.Adversary
import Game.ReceiptFreeness.Valid
import Game.Transformation.ReceiptFreeness-CCA2d.Simulator
module Game.Transformation.ReceiptFreeness-CCA2d.Valid
(PubKey : ★)
(CipherText : ★)
(SerialNumber : ★)
(Receipt : ★)
(MarkedReceipt? : ★)
(Ballot : ★)
(Tally : ★)
-- (BB : ★)
-- ([] : BB)
-- (_∷_ : Receipt → BB → BB)
(Rgb : ★)
(genBallot : PubKey → Rgb → Ballot)
(tallyMarkedReceipt? : let CO = 𝟚 in CO → MarkedReceipt? → Tally)
(0,0 : Tally)
(1,1 : Tally)
(_+,+_ : Tally → Tally → Tally)
(receipts : SerialNumber ² → CipherText ² → Receipt ²)
(enc-co : Receipt → CipherText)
(r-sn : Receipt → SerialNumber)
(m? : Receipt → MarkedReceipt?)
(b-sn : Ballot → SerialNumber)
-- randomness supply for, encryption, key-generation, adversary, adversary state
(Rₐ : ★)
(#q : ℕ) (max#q : Fin #q)
(Check : let BB = List Receipt in BB → Receipt → 𝟚)
where
_²' : ★ → ★
X ²' = X × X
CO = 𝟚
BB = List Receipt
all-sn : BB → List SerialNumber
all-sn = L.map r-sn
module RF = Game.ReceiptFreeness.Adversary PubKey (SerialNumber ²) Rₐ Receipt Ballot Tally CO BB
module RFV = Game.ReceiptFreeness.Valid PubKey SerialNumber Rₐ Receipt Ballot Tally CO BB
CipherText enc-co r-sn b-sn
open Game.Transformation.ReceiptFreeness-CCA2d.Simulator
PubKey CipherText (SerialNumber ²) Receipt MarkedReceipt? Ballot Tally BB [] _∷_ Rgb genBallot
tallyMarkedReceipt? 0,0 1,1 _+,+_ receipts enc-co m? Rₐ #q max#q Check
module CCA2†V = Game.IND-CCA2-dagger.Valid PubKey Message CipherText Rₐ†
module Simulator-Valid (RFA : RF.Adversary)(RFA-Valid : RFV.Valid-Adversary RFA)
(WRONG-HYP : ∀ r r' → enc-co r ≡ enc-co r' → r-sn r ≡ r-sn r')
(CheckMem : ∀ bb r → ✓ (Check bb r) → r-sn r ∉ all-sn bb)
where
valid : CCA2†V.Valid-Adversary (Simulator.A† RFA)
valid (rₐ , rgb) pk = Phase1 _ (RFA-Valid rₐ pk) where
open CCA2†V.Valid-Adversary (rₐ , rgb) pk
module RFVA = RFV.Valid-Adversary rₐ pk
open RF
open Simulator RFA
open AdversaryParts rgb pk rₐ
-- could refine r more
-- {-
Phase2 : ∀ RF {bb i ta r} → r-sn (r 0₂) ∈ all-sn bb → r-sn (r 1₂) ∈ all-sn bb → RFVA.Phase2-Valid r RF
→ Phase2-Valid (enc-co ∘ r) (mapS proj₁ (mitm-to-client-trans (MITM-phase 1₂ i bb ta) RF))
Phase2 (ask REB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RBB cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask RTally cont) r0 r1 RF-valid = Phase2 (cont _) r0 r1 (RF-valid _)
Phase2 (ask (RCO x) cont) r0 r1 ((r₀ , r₁) , RF-valid) = r₀ , r₁ , (λ r → Phase2 (cont _) r0 r1 (RF-valid _))
Phase2 (ask (Vote x) cont) {bb} r0 r1 RF-valid with Check bb x | CheckMem bb x
... | 0₂ | _ = Phase2 (cont _) r0 r1 (RF-valid _)
... | 1₂ | not-in-bb = (λ eq → not-in-bb _ (subst (λ x₁ → x₁ ∈ all-sn bb) (WRONG-HYP _ _ eq) r0))
, (λ eq → not-in-bb _ (subst (λ x₁ → x₁ ∈ all-sn bb) (WRONG-HYP _ _ eq) r1))
, λ r → Phase2 (cont _) (there r0) (there r1) (RF-valid _)
--Phase2 (cont _) (there r0) (there r1) (RF-valid _)
Phase2 (done x) r0 r1 RF-valid = RF-valid
Phase1 : ∀ RF {sn i bb ta} → RFVA.Phase1-Valid sn RF
→ Phase1-Valid (mapS A†2 (mitm-to-client-trans (MITM-phase 0₂ i bb ta) RF))
Phase1 (ask REB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RBB cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask RTally cont) RF-valid = Phase1 _ (RF-valid _)
Phase1 (ask (RCO x) cont) RF-valid r = Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) {bb = bb} RF-valid with Check bb x
Phase1 (ask (Vote x) cont) RF-valid | 1₂ = λ r → Phase1 _ (RF-valid _)
Phase1 (ask (Vote x) cont) RF-valid | 0₂ = Phase1 _ (RF-valid _)
Phase1 (done x) {bb = bb'}{ta} (sn₀∉sn , sn₁∉sn , RF-valid) cs
= {!Phase2 (put-resp x (proj₂ (put-resp (hack-challenge x) cs))) (here refl) (there (here refl)) (RF-valid _)!}
-- Phase2 (put-resp x (proj₂ (put-resp (hack-challenge x) cs) ))
-- ? ? (RF-valid _)
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
|
update the validity for the simulator
|
update the validity for the simulator
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
41898751fdcc62758217e161ad60e93a20db6f51
|
lib/Algebra/Monoid.agda
|
lib/Algebra/Monoid.agda
|
open import Function.NP
open import Data.Product.NP
open import Data.Nat
using (ℕ; fold; zero)
renaming (_+_ to _+ℕ_; _*_ to _*ℕ_; suc to 1+_)
open import Data.Integer
using (ℤ; +_; -[1+_]; _⊖_)
renaming (_+_ to _+ℤ_; _*_ to _*ℤ_)
open import Relation.Binary.PropositionalEquality.NP renaming (_∙_ to _♦_)
open import Algebra.FunctionProperties.Eq
open ≡-Reasoning
module Algebra.Monoid where
record Monoid-Ops {ℓ} (M : Set ℓ) : Set ℓ where
constructor mk
infixl 7 _∙_
field
_∙_ : M → M → M
ε : M
_^⁺_ : M → ℕ → M
x ^⁺ n = fold ε (_∙_ x) n
module FromInverseOp
(_⁻¹ : Op₁ M)
where
infixl 7 _/_
_/_ : M → M → M
x / y = x ∙ y ⁻¹
open FromOp₂ _/_ public renaming (op= to /=)
_^⁻_ _^⁻′_ : M → ℕ → M
x ^⁻ n = (x ^⁺ n)⁻¹
x ^⁻′ n = (x ⁻¹)^⁺ n
_^_ : M → ℤ → M
x ^ -[1+ n ] = x ^⁻(1+ n)
x ^ (+ n) = x ^⁺ n
record Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
-- laws
field
assoc : Associative _∙_
identity : Identity ε _∙_
open FromOp₂ _∙_ public renaming (op= to ∙=)
open FromAssoc _∙_ assoc public
module _ {b} where
^⁺0-ε : b ^⁺ 0 ≡ ε
^⁺0-ε = idp
^⁺1-id : b ^⁺ 1 ≡ b
^⁺1-id = snd identity
^⁺2-∙ : b ^⁺ 2 ≡ b ∙ b
^⁺2-∙ = ap (_∙_ _) ^⁺1-id
^⁺-+ : ∀ m {n} → b ^⁺ (m +ℕ n) ≡ b ^⁺ m ∙ b ^⁺ n
^⁺-+ 0 = ! fst identity
^⁺-+ (1+ m) = ap (_∙_ b) (^⁺-+ m) ♦ ! assoc
^⁺-* : ∀ m {n} → b ^⁺(m *ℕ n) ≡ (b ^⁺ n)^⁺ m
^⁺-* 0 = idp
^⁺-* (1+ m) {n}
= b ^⁺ (n +ℕ m *ℕ n) ≡⟨ ^⁺-+ n ⟩
b ^⁺ n ∙ b ^⁺(m *ℕ n) ≡⟨ ap (_∙_ (b ^⁺ n)) (^⁺-* m) ⟩
b ^⁺ n ∙ (b ^⁺ n)^⁺ m ∎
comm-ε : ∀ {x} → x ∙ ε ≡ ε ∙ x
comm-ε = snd identity ♦ ! fst identity
module _ {c x y} (e : (x ∙ y) ≡ ε) where
elim-assoc= : (c ∙ x) ∙ y ≡ c
elim-assoc= = assoc ♦ ∙= idp e ♦ snd identity
elim-!assoc= : x ∙ (y ∙ c) ≡ c
elim-!assoc= = ! assoc ♦ ∙= e idp ♦ fst identity
module _ {c d x y} (e : (x ∙ y) ≡ ε) where
elim-inner= : (c ∙ x) ∙ (y ∙ d) ≡ c ∙ d
elim-inner= = assoc ♦ ap (_∙_ c) (elim-!assoc= e)
module FromLeftInverse
(_⁻¹ : Op₁ M)
(inv-l : LeftInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-left : LeftCancel _∙_
cancels-∙-left {c} {x} {y} e
= x ≡⟨ ! fst identity ⟩
ε ∙ x ≡⟨ ∙= (! inv-l) idp ⟩
c ⁻¹ ∙ c ∙ x ≡⟨ !assoc= e ⟩
c ⁻¹ ∙ c ∙ y ≡⟨ ∙= inv-l idp ⟩
ε ∙ y ≡⟨ fst identity ⟩
y ∎
inv-r : RightInverse ε _⁻¹ _∙_
inv-r = cancels-∙-left (! assoc ♦ ∙= inv-l idp ♦ ! comm-ε)
/-∙ : ∀ {x y} → x ≡ (x / y) ∙ y
/-∙ {x} {y}
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-l) ⟩
x ∙ (y ⁻¹ ∙ y) ≡⟨ ! assoc ⟩
(x / y) ∙ y ∎
module FromRightInverse
(_⁻¹ : Op₁ M)
(inv-r : RightInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-right : RightCancel _∙_
cancels-∙-right {c} {x} {y} e
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ∙= idp (! inv-r) ⟩
x ∙ (c ∙ c ⁻¹) ≡⟨ assoc= e ⟩
y ∙ (c ∙ c ⁻¹) ≡⟨ ∙= idp inv-r ⟩
y ∙ ε ≡⟨ snd identity ⟩
y ∎
inv-l : LeftInverse ε _⁻¹ _∙_
inv-l = cancels-∙-right (assoc ♦ ∙= idp inv-r ♦ comm-ε)
module _ {x y} where
is-ε-left : x ≡ ε → x ∙ y ≡ y
is-ε-left e = ap (λ z → z ∙ _) e ♦ fst identity
is-ε-right : y ≡ ε → x ∙ y ≡ x
is-ε-right e = ap (λ z → _ ∙ z) e ♦ snd identity
∙-/ : x ≡ (x ∙ y) / y
∙-/
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-r) ⟩
x ∙ (y / y) ≡⟨ ! assoc ⟩
(x ∙ y) / y ∎
module _ {x y} where
unique-ε-left : x ∙ y ≡ y → x ≡ ε
unique-ε-left eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
y / y ≡⟨ inv-r ⟩
ε ∎
unique-ε-right : x ∙ y ≡ x → y ≡ ε
unique-ε-right eq
= y ≡⟨ ! is-ε-left inv-l ⟩
x ⁻¹ ∙ x ∙ y ≡⟨ assoc ⟩
x ⁻¹ ∙ (x ∙ y) ≡⟨ ∙= idp eq ⟩
x ⁻¹ ∙ x ≡⟨ inv-l ⟩
ε ∎
unique-⁻¹ : x ∙ y ≡ ε → x ≡ y ⁻¹
unique-⁻¹ eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
ε / y ≡⟨ fst identity ⟩
y ⁻¹ ∎
open FromLeftInverse _⁻¹ inv-l hiding (inv-r)
ε⁻¹-ε : ε ⁻¹ ≡ ε
ε⁻¹-ε = unique-ε-left inv-l
involutive : Involutive _⁻¹
involutive {x}
= cancels-∙-right
(x ⁻¹ ⁻¹ ∙ x ⁻¹ ≡⟨ inv-l ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ∎)
⁻¹-hom′ : ∀ {x y} → (x ∙ y)⁻¹ ≡ y ⁻¹ ∙ x ⁻¹
⁻¹-hom′ {x} {y} = cancels-∙-left {x ∙ y}
((x ∙ y) ∙ (x ∙ y)⁻¹ ≡⟨ inv-r ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ≡⟨ ap (_∙_ x) (! fst identity) ⟩
x ∙ (ε ∙ x ⁻¹) ≡⟨ ∙= idp (∙= (! inv-r) idp) ⟩
x ∙ ((y ∙ y ⁻¹) ∙ x ⁻¹) ≡⟨ ap (_∙_ x) assoc ⟩
x ∙ (y ∙ (y ⁻¹ ∙ x ⁻¹)) ≡⟨ ! assoc ⟩
(x ∙ y) ∙ (y ⁻¹ ∙ x ⁻¹) ∎)
elim-∙-left-⁻¹∙ : ∀ {c x y} → (c ∙ x)⁻¹ ∙ (c ∙ y) ≡ x ⁻¹ ∙ y
elim-∙-left-⁻¹∙ {c} {x} {y}
= (c ∙ x)⁻¹ ∙ (c ∙ y) ≡⟨ ∙= ⁻¹-hom′ idp ⟩
x ⁻¹ ∙ c ⁻¹ ∙ (c ∙ y) ≡⟨ elim-inner= inv-l ⟩
x ⁻¹ ∙ y ∎
elim-∙-right-/ : ∀ {c x y} → (x ∙ c) / (y ∙ c) ≡ x / y
elim-∙-right-/ {c} {x} {y}
= x ∙ c ∙ (y ∙ c)⁻¹ ≡⟨ ap (_∙_ _) ⁻¹-hom′ ⟩
x ∙ c ∙ (c ⁻¹ / y) ≡⟨ elim-inner= inv-r ⟩
x / y ∎
module _ {b} where
^⁺suc : ∀ n → b ^⁺(1+ n) ≡ b ^⁺ n ∙ b
^⁺suc 0 = comm-ε
^⁺suc (1+ n) = ap (_∙_ b) (^⁺suc n) ♦ ! assoc
^⁺-comm : ∀ n → b ∙ b ^⁺ n ≡ b ^⁺ n ∙ b
^⁺-comm = ^⁺suc
^⁻suc : ∀ n → b ^⁻(1+ n) ≡ b ⁻¹ ∙ b ^⁻ n
^⁻suc n = ap _⁻¹ (^⁺suc n) ♦ ⁻¹-hom′
^⁻′-spec : ∀ n → b ^⁻′ n ≡ b ^⁻ n
^⁻′-spec 0 = ! ε⁻¹-ε
^⁻′-spec (1+ n) = ap (_∙_ (b ⁻¹)) (^⁻′-spec n)
♦ ! ⁻¹-hom′
♦ ap _⁻¹ (! ^⁺suc n)
^⁻′1-id : b ^⁻′ 1 ≡ b ⁻¹
^⁻′1-id = snd identity
^⁻1-id : b ^⁻ 1 ≡ b ⁻¹
^⁻1-id = ! ^⁻′-spec 1 ♦ ^⁻′1-id
^⁻′2-∙ : b ^⁻′ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻′2-∙ = ap (_∙_ _) ^⁻′1-id
^⁻2-∙ : b ^⁻ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻2-∙ = ! ^⁻′-spec 2 ♦ ^⁻′2-∙
record Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-struct : Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Monoid-Struct mon-struct public
record Commutative-Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
field
mon-struct : Monoid-Struct mon-ops
comm : Commutative _∙_
open Monoid-Struct mon-struct public
open FromAssocComm _∙_ assoc comm public
hiding (!assoc=; assoc=; inner=)
record Commutative-Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-comm : Commutative-Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Commutative-Monoid-Struct mon-comm public
mon : Monoid M
mon = record { mon-struct = mon-struct }
-- A renaming of Monoid with additive notation
module Additive-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
)
-- A renaming of Monoid with multiplicative notation
module Multiplicative-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
)
module Additive-Commutative-Monoid {M} (mon-comm : Commutative-Monoid M)
= Commutative-Monoid mon-comm
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; assoc-comm to +-assoc-comm
; interchange to +-interchange
; outer= to +-outer=
)
module Multiplicative-Commutative-Monoid {M} (mon : Commutative-Monoid M) = Commutative-Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; assoc-comm to *-assoc-comm
; interchange to *-interchange
; outer= to *-outer=
)
record MonoidHomomorphism {A B : Set}
(monA0+ : Monoid A)
(monB1* : Monoid B)
(f : A → B) : Set where
open Additive-Monoid monA0+
open Multiplicative-Monoid monB1*
field
0-hom-1 : f 0ᵐ ≡ 1ᵐ
+-hom-* : ∀ {x y} → f (x + y) ≡ f x * f y
-- -}
-- -}
-- -}
-- -}
|
open import Function.NP
open import Data.Product.NP
open import Data.Nat
using (ℕ; zero)
renaming (_+_ to _+ℕ_; _*_ to _*ℕ_; suc to 1+_)
open import Data.Integer
using (ℤ; +_; -[1+_]; _⊖_)
renaming (_+_ to _+ℤ_; _*_ to _*ℤ_)
open import Relation.Binary.PropositionalEquality.NP renaming (_∙_ to _♦_)
open import Algebra.FunctionProperties.Eq
open ≡-Reasoning
module Algebra.Monoid where
record Monoid-Ops {ℓ} (M : Set ℓ) : Set ℓ where
constructor mk
infixl 7 _∙_
field
_∙_ : M → M → M
ε : M
_^⁺_ : M → ℕ → M
x ^⁺ n = nest n (_∙_ x) ε
module FromInverseOp
(_⁻¹ : Op₁ M)
where
infixl 7 _/_
_/_ : M → M → M
x / y = x ∙ y ⁻¹
open FromOp₂ _/_ public renaming (op= to /=)
_^⁻_ _^⁻′_ : M → ℕ → M
x ^⁻ n = (x ^⁺ n)⁻¹
x ^⁻′ n = (x ⁻¹)^⁺ n
_^_ : M → ℤ → M
x ^ -[1+ n ] = x ^⁻(1+ n)
x ^ (+ n) = x ^⁺ n
record Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
-- laws
field
assoc : Associative _∙_
identity : Identity ε _∙_
open FromOp₂ _∙_ public renaming (op= to ∙=)
open FromAssoc _∙_ assoc public
module _ {b} where
^⁺0-ε : b ^⁺ 0 ≡ ε
^⁺0-ε = idp
^⁺1-id : b ^⁺ 1 ≡ b
^⁺1-id = snd identity
^⁺2-∙ : b ^⁺ 2 ≡ b ∙ b
^⁺2-∙ = ap (_∙_ _) ^⁺1-id
^⁺-+ : ∀ m {n} → b ^⁺ (m +ℕ n) ≡ b ^⁺ m ∙ b ^⁺ n
^⁺-+ 0 = ! fst identity
^⁺-+ (1+ m) = ap (_∙_ b) (^⁺-+ m) ♦ ! assoc
^⁺-* : ∀ m {n} → b ^⁺(m *ℕ n) ≡ (b ^⁺ n)^⁺ m
^⁺-* 0 = idp
^⁺-* (1+ m) {n}
= b ^⁺ (n +ℕ m *ℕ n) ≡⟨ ^⁺-+ n ⟩
b ^⁺ n ∙ b ^⁺(m *ℕ n) ≡⟨ ap (_∙_ (b ^⁺ n)) (^⁺-* m) ⟩
b ^⁺ n ∙ (b ^⁺ n)^⁺ m ∎
comm-ε : ∀ {x} → x ∙ ε ≡ ε ∙ x
comm-ε = snd identity ♦ ! fst identity
module _ {c x y} (e : (x ∙ y) ≡ ε) where
elim-assoc= : (c ∙ x) ∙ y ≡ c
elim-assoc= = assoc ♦ ∙= idp e ♦ snd identity
elim-!assoc= : x ∙ (y ∙ c) ≡ c
elim-!assoc= = ! assoc ♦ ∙= e idp ♦ fst identity
module _ {c d x y} (e : (x ∙ y) ≡ ε) where
elim-inner= : (c ∙ x) ∙ (y ∙ d) ≡ c ∙ d
elim-inner= = assoc ♦ ap (_∙_ c) (elim-!assoc= e)
module FromLeftInverse
(_⁻¹ : Op₁ M)
(inv-l : LeftInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-left : LeftCancel _∙_
cancels-∙-left {c} {x} {y} e
= x ≡⟨ ! fst identity ⟩
ε ∙ x ≡⟨ ∙= (! inv-l) idp ⟩
c ⁻¹ ∙ c ∙ x ≡⟨ !assoc= e ⟩
c ⁻¹ ∙ c ∙ y ≡⟨ ∙= inv-l idp ⟩
ε ∙ y ≡⟨ fst identity ⟩
y ∎
inv-r : RightInverse ε _⁻¹ _∙_
inv-r = cancels-∙-left (! assoc ♦ ∙= inv-l idp ♦ ! comm-ε)
/-∙ : ∀ {x y} → x ≡ (x / y) ∙ y
/-∙ {x} {y}
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-l) ⟩
x ∙ (y ⁻¹ ∙ y) ≡⟨ ! assoc ⟩
(x / y) ∙ y ∎
module FromRightInverse
(_⁻¹ : Op₁ M)
(inv-r : RightInverse ε _⁻¹ _∙_)
where
open FromInverseOp _⁻¹
cancels-∙-right : RightCancel _∙_
cancels-∙-right {c} {x} {y} e
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ∙= idp (! inv-r) ⟩
x ∙ (c ∙ c ⁻¹) ≡⟨ assoc= e ⟩
y ∙ (c ∙ c ⁻¹) ≡⟨ ∙= idp inv-r ⟩
y ∙ ε ≡⟨ snd identity ⟩
y ∎
inv-l : LeftInverse ε _⁻¹ _∙_
inv-l = cancels-∙-right (assoc ♦ ∙= idp inv-r ♦ comm-ε)
module _ {x y} where
is-ε-left : x ≡ ε → x ∙ y ≡ y
is-ε-left e = ap (λ z → z ∙ _) e ♦ fst identity
is-ε-right : y ≡ ε → x ∙ y ≡ x
is-ε-right e = ap (λ z → _ ∙ z) e ♦ snd identity
∙-/ : x ≡ (x ∙ y) / y
∙-/
= x ≡⟨ ! snd identity ⟩
x ∙ ε ≡⟨ ap (_∙_ x) (! inv-r) ⟩
x ∙ (y / y) ≡⟨ ! assoc ⟩
(x ∙ y) / y ∎
module _ {x y} where
unique-ε-left : x ∙ y ≡ y → x ≡ ε
unique-ε-left eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
y / y ≡⟨ inv-r ⟩
ε ∎
unique-ε-right : x ∙ y ≡ x → y ≡ ε
unique-ε-right eq
= y ≡⟨ ! is-ε-left inv-l ⟩
x ⁻¹ ∙ x ∙ y ≡⟨ assoc ⟩
x ⁻¹ ∙ (x ∙ y) ≡⟨ ∙= idp eq ⟩
x ⁻¹ ∙ x ≡⟨ inv-l ⟩
ε ∎
unique-⁻¹ : x ∙ y ≡ ε → x ≡ y ⁻¹
unique-⁻¹ eq
= x ≡⟨ ∙-/ ⟩
(x ∙ y) / y ≡⟨ /= eq idp ⟩
ε / y ≡⟨ fst identity ⟩
y ⁻¹ ∎
open FromLeftInverse _⁻¹ inv-l hiding (inv-r)
ε⁻¹-ε : ε ⁻¹ ≡ ε
ε⁻¹-ε = unique-ε-left inv-l
involutive : Involutive _⁻¹
involutive {x}
= cancels-∙-right
(x ⁻¹ ⁻¹ ∙ x ⁻¹ ≡⟨ inv-l ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ∎)
⁻¹-hom′ : ∀ {x y} → (x ∙ y)⁻¹ ≡ y ⁻¹ ∙ x ⁻¹
⁻¹-hom′ {x} {y} = cancels-∙-left {x ∙ y}
((x ∙ y) ∙ (x ∙ y)⁻¹ ≡⟨ inv-r ⟩
ε ≡⟨ ! inv-r ⟩
x ∙ x ⁻¹ ≡⟨ ap (_∙_ x) (! fst identity) ⟩
x ∙ (ε ∙ x ⁻¹) ≡⟨ ∙= idp (∙= (! inv-r) idp) ⟩
x ∙ ((y ∙ y ⁻¹) ∙ x ⁻¹) ≡⟨ ap (_∙_ x) assoc ⟩
x ∙ (y ∙ (y ⁻¹ ∙ x ⁻¹)) ≡⟨ ! assoc ⟩
(x ∙ y) ∙ (y ⁻¹ ∙ x ⁻¹) ∎)
elim-∙-left-⁻¹∙ : ∀ {c x y} → (c ∙ x)⁻¹ ∙ (c ∙ y) ≡ x ⁻¹ ∙ y
elim-∙-left-⁻¹∙ {c} {x} {y}
= (c ∙ x)⁻¹ ∙ (c ∙ y) ≡⟨ ∙= ⁻¹-hom′ idp ⟩
x ⁻¹ ∙ c ⁻¹ ∙ (c ∙ y) ≡⟨ elim-inner= inv-l ⟩
x ⁻¹ ∙ y ∎
elim-∙-right-/ : ∀ {c x y} → (x ∙ c) / (y ∙ c) ≡ x / y
elim-∙-right-/ {c} {x} {y}
= x ∙ c ∙ (y ∙ c)⁻¹ ≡⟨ ap (_∙_ _) ⁻¹-hom′ ⟩
x ∙ c ∙ (c ⁻¹ / y) ≡⟨ elim-inner= inv-r ⟩
x / y ∎
module _ {b} where
^⁺suc : ∀ n → b ^⁺(1+ n) ≡ b ^⁺ n ∙ b
^⁺suc 0 = comm-ε
^⁺suc (1+ n) = ap (_∙_ b) (^⁺suc n) ♦ ! assoc
^⁺-comm : ∀ n → b ∙ b ^⁺ n ≡ b ^⁺ n ∙ b
^⁺-comm = ^⁺suc
^⁻suc : ∀ n → b ^⁻(1+ n) ≡ b ⁻¹ ∙ b ^⁻ n
^⁻suc n = ap _⁻¹ (^⁺suc n) ♦ ⁻¹-hom′
^⁻′-spec : ∀ n → b ^⁻′ n ≡ b ^⁻ n
^⁻′-spec 0 = ! ε⁻¹-ε
^⁻′-spec (1+ n) = ap (_∙_ (b ⁻¹)) (^⁻′-spec n)
♦ ! ⁻¹-hom′
♦ ap _⁻¹ (! ^⁺suc n)
^⁻′1-id : b ^⁻′ 1 ≡ b ⁻¹
^⁻′1-id = snd identity
^⁻1-id : b ^⁻ 1 ≡ b ⁻¹
^⁻1-id = ! ^⁻′-spec 1 ♦ ^⁻′1-id
^⁻′2-∙ : b ^⁻′ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻′2-∙ = ap (_∙_ _) ^⁻′1-id
^⁻2-∙ : b ^⁻ 2 ≡ b ⁻¹ ∙ b ⁻¹
^⁻2-∙ = ! ^⁻′-spec 2 ♦ ^⁻′2-∙
record Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-struct : Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Monoid-Struct mon-struct public
record Commutative-Monoid-Struct {ℓ} {M : Set ℓ} (mon-ops : Monoid-Ops M) : Set ℓ where
open Monoid-Ops mon-ops
field
mon-struct : Monoid-Struct mon-ops
comm : Commutative _∙_
open Monoid-Struct mon-struct public
open FromAssocComm _∙_ assoc comm public
hiding (!assoc=; assoc=; inner=)
record Commutative-Monoid (M : Set) : Set where
field
mon-ops : Monoid-Ops M
mon-comm : Commutative-Monoid-Struct mon-ops
open Monoid-Ops mon-ops public
open Commutative-Monoid-Struct mon-comm public
mon : Monoid M
mon = record { mon-struct = mon-struct }
-- A renaming of Monoid with additive notation
module Additive-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
)
-- A renaming of Monoid with multiplicative notation
module Multiplicative-Monoid {M} (mon : Monoid M) = Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
)
module Additive-Commutative-Monoid {M} (mon-comm : Commutative-Monoid M)
= Commutative-Monoid mon-comm
renaming ( _∙_ to _+_; ε to 0ᵐ
; assoc to +-assoc; identity to +-identity
; ∙= to +=
; assoc= to +-assoc=
; !assoc= to +-!assoc=
; inner= to +-inner=
; assoc-comm to +-assoc-comm
; interchange to +-interchange
; outer= to +-outer=
)
module Multiplicative-Commutative-Monoid {M} (mon : Commutative-Monoid M) = Commutative-Monoid mon
renaming ( _∙_ to _*_; ε to 1ᵐ
; assoc to *-assoc; identity to *-identity
; ∙= to *=
; assoc= to *-assoc=
; !assoc= to *-!assoc=
; inner= to *-inner=
; assoc-comm to *-assoc-comm
; interchange to *-interchange
; outer= to *-outer=
)
record MonoidHomomorphism {A B : Set}
(monA0+ : Monoid A)
(monB1* : Monoid B)
(f : A → B) : Set where
open Additive-Monoid monA0+
open Multiplicative-Monoid monB1*
field
0-hom-1 : f 0ᵐ ≡ 1ᵐ
+-hom-* : ∀ {x y} → f (x + y) ≡ f x * f y
-- -}
-- -}
-- -}
-- -}
|
use nest and not fold
|
Monoid: use nest and not fold
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
7826abee6b67c7b55aa70968ca7fd6b637d30857
|
Base/Syntax/Context.agda
|
Base/Syntax/Context.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Variables and contexts
--
-- This module defines the syntax of contexts, prefixes of
-- contexts and variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
------------------------------------------------------------------------
module Base.Syntax.Context
(Type : Set)
where
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- Typing Contexts
-- ===============
import Data.List as List
open import Base.Data.ContextList public
Context : Set
Context = List.List Type
-- Variables
-- =========
--
-- Here it is clear that we are using de Bruijn indices,
-- encoded as natural numbers, more or less.
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ
-- Weakening
-- =========
--
-- We provide two approaches for weakening: context prefixes and
-- and subcontext relationship. In this formalization, we mostly
-- (maybe exclusively?) use the latter.
-- Context Prefixes
-- ----------------
--
-- Useful for making lemmas strong enough to prove by induction.
--
-- Consider using the Subcontexts module instead.
module Prefixes where
-- Prefix of a context
data Prefix : Context → Set where
∅ : ∀ {Γ} → Prefix Γ
_•_ : ∀ {Γ} → (τ : Type) → Prefix Γ → Prefix (τ • Γ)
-- take only the prefix of a context
take : (Γ : Context) → Prefix Γ → Context
take Γ ∅ = ∅
take (τ • Γ) (.τ • Γ′) = τ • take Γ Γ′
-- drop the prefix of a context
drop : (Γ : Context) → Prefix Γ → Context
drop Γ ∅ = Γ
drop (τ • Γ) (.τ • Γ′) = drop Γ Γ′
-- Extend a context to a super context
infixr 10 _⋎_
_⋎_ : (Γ₁ Γ₂ : Context) → Context
_⋎_ = List._++_
take-drop : ∀ Γ Γ′ → take Γ Γ′ ⋎ drop Γ Γ′ ≡ Γ
take-drop ∅ ∅ = refl
take-drop (τ • Γ) ∅ = refl
take-drop (τ • Γ) (.τ • Γ′) rewrite take-drop Γ Γ′ = refl
or-unit : ∀ Γ → Γ ⋎ ∅ ≡ Γ
or-unit ∅ = refl
or-unit (τ • Γ) rewrite or-unit Γ = refl
move-prefix : ∀ Γ τ Γ′ →
Γ ⋎ (τ • Γ′) ≡ (Γ ⋎ (τ • ∅)) ⋎ Γ′
move-prefix ∅ τ Γ′ = refl
move-prefix (τ • Γ) τ₁ ∅ = sym (or-unit (τ • Γ ⋎ (τ₁ • ∅)))
move-prefix (τ • Γ) τ₁ (τ₂ • Γ′) rewrite move-prefix Γ τ₁ (τ₂ • Γ′) = refl
-- Lift a variable to a super context
lift : ∀ {Γ₁ Γ₂ Γ₃ τ} → Var (Γ₁ ⋎ Γ₃) τ → Var (Γ₁ ⋎ Γ₂ ⋎ Γ₃) τ
lift {∅} {∅} x = x
lift {∅} {τ • Γ₂} x = that (lift {∅} {Γ₂} x)
lift {τ • Γ₁} {Γ₂} this = this
lift {τ • Γ₁} {Γ₂} (that x) = that (lift {Γ₁} {Γ₂} x)
-- Subcontexts
-- -----------
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
module Subcontexts where
infix 4 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
weaken-var (keep τ • ≼₁) this = this
weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x)
weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x)
-- Currently, we export the subcontext relation as well as the
-- definition of _⋎_.
open Subcontexts public
open Prefixes public using (_⋎_)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Variables and contexts
--
-- This module defines the syntax of contexts and subcontexts,
-- together with variables and properties of these notions.
--
-- This module is parametric in the syntax of types, so it
-- can be reused for different calculi.
------------------------------------------------------------------------
module Base.Syntax.Context
(Type : Set)
where
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
-- Typing Contexts
-- ===============
import Data.List as List
open import Base.Data.ContextList public
Context : Set
Context = List.List Type
-- Variables
-- =========
--
-- Here it is clear that we are using de Bruijn indices,
-- encoded as natural numbers, more or less.
data Var : Context → Type → Set where
this : ∀ {Γ τ} → Var (τ • Γ) τ
that : ∀ {Γ σ τ} → (x : Var Γ τ) → Var (σ • Γ) τ
-- Weakening
-- =========
--
-- We define weakening based on subcontext relationship.
-- Subcontexts
-- -----------
--
-- Useful as a reified weakening operation,
-- and for making theorems strong enough to prove by induction.
--
-- The contents of this module are currently exported at the end
-- of this file.
-- This handling of contexts is recommended by [this
-- email](https://lists.chalmers.se/pipermail/agda/2011/003423.html) and
-- attributed to Conor McBride.
--
-- The associated thread discusses a few alternatives solutions, including one
-- where beta-reduction can handle associativity of ++.
module Subcontexts where
infix 4 _≼_
data _≼_ : (Γ₁ Γ₂ : Context) → Set where
∅ : ∅ ≼ ∅
keep_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
τ • Γ₁ ≼ τ • Γ₂
drop_•_ : ∀ {Γ₁ Γ₂} →
(τ : Type) →
Γ₁ ≼ Γ₂ →
Γ₁ ≼ τ • Γ₂
-- Properties
∅≼Γ : ∀ {Γ} → ∅ ≼ Γ
∅≼Γ {∅} = ∅
∅≼Γ {τ • Γ} = drop τ • ∅≼Γ
≼-refl : Reflexive _≼_
≼-refl {∅} = ∅
≼-refl {τ • Γ} = keep τ • ≼-refl
≼-reflexive : ∀ {Γ₁ Γ₂} → Γ₁ ≡ Γ₂ → Γ₁ ≼ Γ₂
≼-reflexive refl = ≼-refl
≼-trans : Transitive _≼_
≼-trans ≼₁ ∅ = ≼₁
≼-trans (keep .τ • ≼₁) (keep τ • ≼₂) = keep τ • ≼-trans ≼₁ ≼₂
≼-trans (drop .τ • ≼₁) (keep τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-trans ≼₁ (drop τ • ≼₂) = drop τ • ≼-trans ≼₁ ≼₂
≼-isPreorder : IsPreorder _≡_ _≼_
≼-isPreorder = record
{ isEquivalence = isEquivalence
; reflexive = ≼-reflexive
; trans = ≼-trans
}
≼-preorder : Preorder _ _ _
≼-preorder = record
{ Carrier = Context
; _≈_ = _≡_
; _∼_ = _≼_
; isPreorder = ≼-isPreorder
}
module ≼-Reasoning where
open import Relation.Binary.PreorderReasoning ≼-preorder public
renaming
( _≈⟨_⟩_ to _≡⟨_⟩_
; _∼⟨_⟩_ to _≼⟨_⟩_
; _≈⟨⟩_ to _≡⟨⟩_
)
-- Lift a variable to a super context
weaken-var : ∀ {Γ₁ Γ₂ τ} → Γ₁ ≼ Γ₂ → Var Γ₁ τ → Var Γ₂ τ
weaken-var (keep τ • ≼₁) this = this
weaken-var (keep τ • ≼₁) (that x) = that (weaken-var ≼₁ x)
weaken-var (drop τ • ≼₁) x = that (weaken-var ≼₁ x)
-- Currently, we export the subcontext relation.
open Subcontexts public
|
Drop Prefixes
|
Drop Prefixes
|
Agda
|
mit
|
inc-lc/ilc-agda
|
d4d74883fdfb5330e27bb48434b046f8291b5f3d
|
Denotation/Change/Popl14.agda
|
Denotation/Change/Popl14.agda
|
module Denotation.Change.Popl14 where
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Popl14.Denotation.Value
open import Theorem.Groups-Popl14
open import Postulate.Extensionality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
-- apply Δv v ::= v ⊞ Δv
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
-- diff u v ::= u ⊟ v
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
let
_+_ = _⊞_ {τ}
_-_ = _⊟_ {τ}
in
v + (u - v) ≡ u
--------------------
-- Implementation --
--------------------
-- ΔVal τ is intended to be the semantic domain for changes of values
-- of type τ.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
--
-- ΔVal : Type → Set
ΔVal (base base-int) = ℤ
ΔVal (base base-bag) = Bag
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {base base-int} n Δn = n + Δn
_⊞_ {base base-bag} b Δb = b ++ Δb
_⊞_ {σ ⇒ τ} f Δf = λ v → f v ⊞ Δf v (v ⊟ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {base base-int} m n = m - n
_⊟_ {base base-bag} d b = d \\ b
_⊟_ {σ ⇒ τ} g f = λ v Δv R[v,Δv] → g (v ⊞ Δv) ⊟ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base base-int} n Δn = ⊤
valid {base base-bag} b Δb = ⊤
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (R[v,Δv] : valid v Δv)
→ valid (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (_⊞_ {σ ⇒ τ} f Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base base-int} {u} {v} = n+[m-n]=m {v} {u}
v+[u-v]=u {base base-bag} {u} {v} = a++[b\\a]=b {v} {u}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
let
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₀_ = _⊞_ {σ}
_-₀_ = _⊟_ {σ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v + (u - v)) w
≡⟨ refl ⟩
v w +₁ (u (w +₀ (w -₀ w)) -₁ v w)
≡⟨ cong (λ hole → v w +₁ (u hole -₁ v w)) (v+[u-v]=u {σ}) ⟩
v w +₁ (u w -₁ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base base-int} {u} {v} = tt
R[v,u-v] {base base-bag} {u} {v} = tt
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞ Δw
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
R[v,u-v] {τ}
,
(begin
(v + (u - v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w +₁ (u - v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- `diff` and `apply`, without validity proofs
infixl 6 _⟦⊕⟧_ _⟦⊝⟧_
_⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_⟦⊕⟧_ {base base-int} n Δn = n + Δn
_⟦⊕⟧_ {base base-bag} b Δb = b ++ Δb
_⟦⊕⟧_ {σ ⇒ τ} f Δf = λ v →
let
_-₀_ = _⟦⊝⟧_ {σ}
_+₁_ = _⟦⊕⟧_ {τ}
in
f v +₁ Δf v (v -₀ v)
_⟦⊝⟧_ {base base-int} m n = m - n
_⟦⊝⟧_ {base base-bag} a b = a \\ b
_⟦⊝⟧_ {σ ⇒ τ} g f = λ v Δv → _⟦⊝⟧_ {τ} (g (_⟦⊕⟧_ {σ} v Δv)) (f v)
|
module Denotation.Change.Popl14 where
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Popl14.Denotation.Value
open import Theorem.Groups-Popl14
open import Postulate.Extensionality
open import Data.Unit
open import Data.Product
open import Data.Integer
open import Structure.Bag.Popl14
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
-- apply Δv v ::= v ⊞ Δv
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
-- diff u v ::= u ⊟ v
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
let
_+_ = _⊞_ {τ}
_-_ = _⊟_ {τ}
in
v + (u - v) ≡ u
--------------------
-- Implementation --
--------------------
-- (ΔVal τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (ΔVal (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
--
-- ΔVal : Type → Set
ΔVal (base base-int) = ℤ
ΔVal (base base-bag) = Bag
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {base base-int} n Δn = n + Δn
_⊞_ {base base-bag} b Δb = b ++ Δb
_⊞_ {σ ⇒ τ} f Δf = λ v → f v ⊞ Δf v (v ⊟ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {base base-int} m n = m - n
_⊟_ {base base-bag} d b = d \\ b
_⊟_ {σ ⇒ τ} g f = λ v Δv R[v,Δv] → g (v ⊞ Δv) ⊟ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base base-int} n Δn = ⊤
valid {base base-bag} b Δb = ⊤
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (R[v,Δv] : valid v Δv)
→ valid (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (_⊞_ {σ ⇒ τ} f Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base base-int} {u} {v} = n+[m-n]=m {v} {u}
v+[u-v]=u {base base-bag} {u} {v} = a++[b\\a]=b {v} {u}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
let
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₀_ = _⊞_ {σ}
_-₀_ = _⊟_ {σ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v + (u - v)) w
≡⟨ refl ⟩
v w +₁ (u (w +₀ (w -₀ w)) -₁ v w)
≡⟨ cong (λ hole → v w +₁ (u hole -₁ v w)) (v+[u-v]=u {σ}) ⟩
v w +₁ (u w -₁ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base base-int} {u} {v} = tt
R[v,u-v] {base base-bag} {u} {v} = tt
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞ Δw
_+_ = _⊞_ {σ ⇒ τ}
_-_ = _⊟_ {σ ⇒ τ}
_+₁_ = _⊞_ {τ}
_-₁_ = _⊟_ {τ}
in
R[v,u-v] {τ}
,
(begin
(v + (u - v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w +₁ (u - v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- `diff` and `apply`, without validity proofs
infixl 6 _⟦⊕⟧_ _⟦⊝⟧_
_⟦⊕⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ ΔType τ ⟧ → ⟦ τ ⟧
_⟦⊝⟧_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ⟦ ΔType τ ⟧
_⟦⊕⟧_ {base base-int} n Δn = n + Δn
_⟦⊕⟧_ {base base-bag} b Δb = b ++ Δb
_⟦⊕⟧_ {σ ⇒ τ} f Δf = λ v →
let
_-₀_ = _⟦⊝⟧_ {σ}
_+₁_ = _⟦⊕⟧_ {τ}
in
f v +₁ Δf v (v -₀ v)
_⟦⊝⟧_ {base base-int} m n = m - n
_⟦⊝⟧_ {base base-bag} a b = a \\ b
_⟦⊝⟧_ {σ ⇒ τ} g f = λ v Δv → _⟦⊝⟧_ {τ} (g (_⟦⊕⟧_ {σ} v Δv)) (f v)
|
Improve documentation of ΔVal.
|
Improve documentation of ΔVal.
Old-commit-hash: fb71a0ea6af97c62b01a9d8b7d3089e5cd3139ed
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a0c0e331a5839a6f7cd2921fe50b2acfdf977244
|
total.agda
|
total.agda
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Note that Δ is *not* the same as the ∂ operator in
-- definition/intro.tex. See discussion at:
--
-- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Syntactic.Changes
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Denotational.ValidChanges
open import Changes
open import ChangeContexts
open import ChangeContextLifting
open import PropsDelta
open import SymbolicDerivation
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) →
Δ {{Γ′}} t ≈ derive-term {{Γ′}} t
derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) =
begin
Δ (abs t)
≈⟨ Δ-abs t ⟩
abs (abs (Δ {τ • Γ₁} t))
≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} t)) ⟩
abs (abs (derive-term {τ • Γ₁} t))
≡⟨⟩
derive-term (abs t)
∎ where
open ≈-Reasoning
Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) =
begin
Δ (app t₁ t₂)
≈⟨ Δ-app t₁ t₂ ⟩
app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩
app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
≡⟨⟩
derive-term {{Γ′}} (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ →
begin
⟦ Δ {{Γ′}} (var x) ⟧ ρ
≡⟨⟩
diff
(⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ)))
(⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ)))
≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩
⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ)
≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩
⟦ lift Γ′ (derive-var x) ⟧Var ρ
∎) where open ≡-Reasoning
derive-term-correct true = ext-t (λ ρ → ≡-refl)
derive-term-correct false = ext-t (λ ρ → ≡-refl)
derive-term-correct {{Γ′}} (if t₁ t₂ t₃) =
begin
Δ (if t₁ t₂ t₃)
≈⟨ Δ-if {{Γ′}} t₁ t₂ t₃ ⟩
if (Δ t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (Δ t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (Δ t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(Δ t₂)
(Δ t₃))
≈⟨ ≈-if (derive-term-correct t₁)
(≈-if (≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct t₃) ≈-refl) ≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct t₂) ≈-refl) ≈-refl))
(≈-if (≈-refl)
(derive-term-correct t₂)
(derive-term-correct t₃)) ⟩
if (derive-term t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (derive-term t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (derive-term t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(derive-term t₂)
(derive-term t₃))
≡⟨⟩
derive-term (if t₁ t₂ t₃)
∎ where open ≈-Reasoning
derive-term-correct (Δ t) = ≈-Δ (derive-term-correct t)
|
module total where
-- INCREMENTAL λ-CALCULUS
-- with total derivatives
--
-- Features:
-- * changes and derivatives are unified (following Cai)
-- * Δ e describes how e changes when its free variables or its arguments change
-- * denotational semantics including semantics of changes
--
-- Note that Δ is *not* the same as the ∂ operator in
-- definition/intro.tex. See discussion at:
--
-- https://github.com/ps-mr/ilc/pull/34#discussion_r4290325
--
-- Work in Progress:
-- * lemmas about behavior of changes
-- * lemmas about behavior of Δ
-- * correctness proof for symbolic derivation
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.Contexts Type
open import Syntactic.Terms.Total
open import Syntactic.Changes
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Environments Type ⟦_⟧Type
open import Denotational.Evaluation.Total
open import Denotational.Equivalence
open import Denotational.ValidChanges
open import Changes
open import ChangeContexts
open import ChangeContextLifting
open import PropsDelta
open import SymbolicDerivation
-- CORRECTNESS of derivation
derive-var-correct : ∀ {Γ τ} → (ρ : ⟦ Δ-Context Γ ⟧) → (x : Var Γ τ) →
diff (⟦ x ⟧ (update ρ)) (⟦ x ⟧ (ignore ρ)) ≡
⟦ derive-var x ⟧ ρ
derive-var-correct (dv • v • ρ) this = diff-apply dv v
derive-var-correct (dv • v • ρ) (that x) = derive-var-correct ρ x
derive-term-correct : ∀ {Γ₁ Γ₂ τ} → {{Γ′ : Δ-Context Γ₁ ≼ Γ₂}} → (t : Term Γ₁ τ) →
Δ {{Γ′}} t ≈ derive-term {{Γ′}} t
derive-term-correct {Γ₁} {{Γ′}} (abs {τ} t) =
begin
Δ {{Γ′}} (abs t)
≈⟨ Δ-abs {{Γ′}} t ⟩
abs (abs (Δ {τ • Γ₁} {{Γ″}} t))
≈⟨ ≈-abs (≈-abs (derive-term-correct {τ • Γ₁} {{Γ″}} t)) ⟩
abs (abs (derive-term {τ • Γ₁} {{Γ″}} t))
≡⟨⟩
derive-term {{Γ′}} (abs t)
∎ where
open ≈-Reasoning
Γ″ = keep Δ-Type τ • keep τ • Γ′
derive-term-correct {Γ₁} {{Γ′}} (app t₁ t₂) =
begin
Δ {{Γ′}} (app t₁ t₂)
≈⟨ Δ-app {{Γ′}} t₁ t₂ ⟩
app (app (Δ {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (Δ {{Γ′}} t₂)
≈⟨ ≈-app (≈-app (derive-term-correct {{Γ′}} t₁) ≈-refl) (derive-term-correct {{Γ′}} t₂) ⟩
app (app (derive-term {{Γ′}} t₁) (lift-term {{Γ′}} t₂)) (derive-term {{Γ′}} t₂)
≡⟨⟩
derive-term {{Γ′}} (app t₁ t₂)
∎ where open ≈-Reasoning
derive-term-correct {Γ₁} {{Γ′}} (var x) = ext-t (λ ρ →
begin
⟦ Δ {{Γ′}} (var x) ⟧ ρ
≡⟨⟩
diff
(⟦ x ⟧ (update (⟦ Γ′ ⟧ ρ)))
(⟦ x ⟧ (ignore (⟦ Γ′ ⟧ ρ)))
≡⟨ derive-var-correct {Γ₁} (⟦ Γ′ ⟧ ρ) x ⟩
⟦ derive-var x ⟧Var (⟦ Γ′ ⟧ ρ)
≡⟨ sym (lift-sound Γ′ (derive-var x) ρ) ⟩
⟦ lift Γ′ (derive-var x) ⟧Var ρ
∎) where open ≡-Reasoning
derive-term-correct true = ext-t (λ ρ → ≡-refl)
derive-term-correct false = ext-t (λ ρ → ≡-refl)
derive-term-correct {{Γ′}} (if t₁ t₂ t₃) =
begin
Δ (if t₁ t₂ t₃)
≈⟨ Δ-if {{Γ′}} t₁ t₂ t₃ ⟩
if (Δ t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (Δ {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (Δ {{Γ′}} t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(Δ {{Γ′}} t₂)
(Δ {{Γ′}} t₃))
≈⟨ ≈-if (derive-term-correct {{Γ′}} t₁)
(≈-if (≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₃) ≈-refl) ≈-refl)
(≈-diff-term (≈-apply-term (derive-term-correct {{Γ′}} t₂) ≈-refl) ≈-refl))
(≈-if (≈-refl)
(derive-term-correct {{Γ′}} t₂)
(derive-term-correct {{Γ′}} t₃)) ⟩
if (derive-term {{Γ′}} t₁)
(if (lift-term {{Γ′}} t₁)
(diff-term (apply-term (derive-term {{Γ′}} t₃) (lift-term {{Γ′}} t₃)) (lift-term {{Γ′}} t₂))
(diff-term (apply-term (derive-term {{Γ′}} t₂) (lift-term {{Γ′}} t₂)) (lift-term {{Γ′}} t₃)))
(if (lift-term {{Γ′}} t₁)
(derive-term {{Γ′}} t₂)
(derive-term {{Γ′}} t₃))
≡⟨⟩
derive-term {{Γ′}} (if t₁ t₂ t₃)
∎ where open ≈-Reasoning
derive-term-correct {{Γ′}} (Δ {{Γ″}} t) = ≈-Δ {{Γ′}} (derive-term-correct {{Γ″}} t)
|
Resolve implicit arguments.
|
Resolve implicit arguments.
When I filled the last hole in total.agda in the previous commit,
Agda told me to 'resolve implicit argument', so I added explicit
implicit arguments until Agda was satisfied. I don't know whether
or why it matters to do this.
Old-commit-hash: ed138bd06659ab2caac47a670b7d3d60c4d97f3a
|
Agda
|
mit
|
inc-lc/ilc-agda
|
db5307718bfbf262aa80ac0a0dd4b8da3799363b
|
Denotational/WeakValidChanges.agda
|
Denotational/WeakValidChanges.agda
|
module Denotational.WeakValidChanges where
open import Data.Product
open import Data.Unit
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.ChangeTypes.ChangesAreDerivatives
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Changes
open import Denotational.EqualityLemmas
-- DEFINITION of weakly valid changes via a logical relation
-- Strong validity:
open import Denotational.ValidChanges
-- Weak validity, defined through a function producing a type.
--
-- Since this is a logical relation, this couldn't be defined as a
-- datatype, since it is not strictly positive.
Weak-Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set
Weak-Valid-Δ {bool} v dv = ⊤
Weak-Valid-Δ {S ⇒ T} f df =
∀ (s : ⟦ S ⟧) ds (valid-w : Weak-Valid-Δ s ds) →
Weak-Valid-Δ (f s) (df s ds) ×
df is-correct-for f on s and ds
strong-to-weak-validity : ∀ {τ : Type} {v : ⟦ τ ⟧} {dv} → Valid-Δ v dv → Weak-Valid-Δ v dv
strong-to-weak-validity {bool} _ = tt
strong-to-weak-validity {τ ⇒ τ₁} {v} {dv} s-valid-v-dv s ds _ = strong-to-weak-validity dv-s-ds-valid-on-v-s , dv-is-correct-for-v-on-s-and-ds
where
proofs = s-valid-v-dv s ds
dv-s-ds-valid-on-v-s = proj₁ proofs
dv-is-correct-for-v-on-s-and-ds = proj₂ proofs
{-
-- This proof doesn't go through: the desired equivalence is too
-- strong, and we can't fill the holes because dv is arbitrary.
diff-apply-proof-weak : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≡ dv
diff-apply-proof-weak {τ₁ ⇒ τ₂} df f df-valid = ext (λ v → ext (λ dv →
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv {!!})) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv {!!})) ⟩
df v dv
∎)) where open ≡-Reasoning
diff-apply-proof-weak {bool} db b _ = xor-cancellative b db
-- That'd be the core of the proof of diff-apply-weak, if we relax the
-- equivalence in the goal to something more correct. We need a proof
-- of validity only for the first argument, somehow: maybe because
-- diff itself produces strongly valid changes?
--
-- Ahah! Not really: below we use the unprovable
-- diff-apply-proof-weak. To be able to use induction, we need to use
-- this weaker lemma again. Hence, we'll only be able to prove the
-- same equivalence, which will needs another proof of validity to
-- unfold further until the base case.
diff-apply-proof-weak-f : ∀ {τ₁ τ₂} (df : ⟦ Δ-Type (τ₁ ⇒ τ₂) ⟧) (f : ⟦ τ₁ ⇒ τ₂ ⟧) →
(Weak-Valid-Δ f df) → ∀ dv v → (Weak-Valid-Δ v dv) → (diff (apply df f) f) v dv ≡ df v dv
diff-apply-proof-weak-f {τ₁} {τ₂} df f df-valid dv v dv-valid =
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid)) ⟩
df v dv
∎ where open ≡-Reasoning
-}
-- A stronger delta equivalence. Note this isn't a congruence; it
-- should be possible to define some congruence rules, but those will
-- be more complex.
data _≈_ : {τ : Type} → ⟦ Δ-Type τ ⟧ → ⟦ Δ-Type τ ⟧ → Set where
--base : ∀ (v : ⟦ bool ⟧) → v ≈ v
base : ∀ (v1 v2 : ⟦ bool ⟧) → (≡ : v1 ≡ v2) → v1 ≈ v2
fun : ∀ {σ τ} (f1 f2 : ⟦ Δ-Type (σ ⇒ τ) ⟧) →
(t≈ : ∀ s ds (valid : Weak-Valid-Δ s ds) → f1 s ds ≈ f2 s ds) →
f1 ≈ f2
≡→≈ : ∀ {τ} {v1 v2 : ⟦ Δ-Type τ ⟧} → v1 ≡ v2 → v1 ≈ v2
≡→≈ {bool} {v1} {.v1} refl = base v1 v1 refl
≡→≈ {τ₁ ⇒ τ₂} {v1} {.v1} refl = fun v1 v1 (λ s ds _ → ≡→≈ refl)
open import Relation.Binary hiding (_⇒_)
≈-refl : ∀ {τ} → Reflexive (_≈_ {τ})
≈-refl = ≡→≈ refl
≈-sym : ∀ {τ} → Symmetric (_≈_ {τ})
≈-sym (base v1 v2 ≡) = base _ _ (sym ≡)
≈-sym {(σ ⇒ τ)} (fun f1 f2 t≈) = fun _ _ (λ s ds valid → ≈-sym (t≈ s ds valid))
≈-trans : ∀ {τ} → Transitive (_≈_ {τ})
≈-trans {.bool} {.k} {.k} {k} (base .k .k refl) (base .k .k refl) = base k k refl
≈-trans {σ ⇒ τ} {.f1} {.f2} {k} (fun f1 .f2 ≈₁) (fun f2 .k ≈₂) = fun f1 k (λ s ds valid → ≈-trans (≈₁ _ _ valid) (≈₂ _ _ valid))
≈-isEquivalence : ∀ {τ} → IsEquivalence (_≈_ {τ})
≈-isEquivalence = record
{ refl = ≈-refl
; sym = ≈-sym
; trans = ≈-trans
}
≈-setoid : Type → Setoid _ _
≈-setoid τ = record
{ Carrier = ⟦ Δ-Type τ ⟧
; _≈_ = _≈_
; isEquivalence = ≈-isEquivalence
}
module ≈-Reasoning where
module _ {τ : Type} where
open import Relation.Binary.EqReasoning (≈-setoid τ) public
hiding (_≡⟨_⟩_)
-- Correct proof, to refactor using ≈-Reasoning
diff-apply-proof-weak-real : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≈ dv
diff-apply-proof-weak-real {τ₁ ⇒ τ₂} df f df-valid = fun _ _ (λ v dv dv-valid → ≈-trans (≡→≈ (
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
∎)) (diff-apply-proof-weak-real {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid))))
--≈⟨ fill in the above proof ⟩
--df v dv
where open ≡-Reasoning
diff-apply-proof-weak-real {bool} db b _ = ≡→≈ (xor-cancellative b db)
|
module Denotational.WeakValidChanges where
open import Data.Product
open import Data.Unit
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
open import Syntactic.Types
open import Syntactic.ChangeTypes.ChangesAreDerivatives
open import Denotational.Notation
open import Denotational.Values
open import Denotational.Changes
open import Denotational.EqualityLemmas
-- DEFINITION of weakly valid changes via a logical relation
-- Strong validity:
open import Denotational.ValidChanges
-- Weak validity, defined through a function producing a type.
--
-- Since this is a logical relation, this couldn't be defined as a
-- datatype, since it is not strictly positive.
Weak-Valid-Δ : {τ : Type} → ⟦ τ ⟧ → ⟦ Δ-Type τ ⟧ → Set
Weak-Valid-Δ {bool} v dv = ⊤
Weak-Valid-Δ {S ⇒ T} f df =
∀ (s : ⟦ S ⟧) ds (valid-w : Weak-Valid-Δ s ds) →
Weak-Valid-Δ (f s) (df s ds) ×
df is-correct-for f on s and ds
strong-to-weak-validity : ∀ {τ : Type} {v : ⟦ τ ⟧} {dv} → Valid-Δ v dv → Weak-Valid-Δ v dv
strong-to-weak-validity {bool} _ = tt
strong-to-weak-validity {τ ⇒ τ₁} {v} {dv} s-valid-v-dv = λ s ds _ →
let
proofs = s-valid-v-dv s ds
dv-s-ds-valid-on-v-s = proj₁ proofs
dv-is-correct-for-v-on-s-and-ds = proj₂ proofs
in
strong-to-weak-validity dv-s-ds-valid-on-v-s , dv-is-correct-for-v-on-s-and-ds
{-
-- This proof doesn't go through: the desired equivalence is too
-- strong, and we can't fill the holes because dv is arbitrary.
diff-apply-proof-weak : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≡ dv
diff-apply-proof-weak {τ₁ ⇒ τ₂} df f df-valid = ext (λ v → ext (λ dv →
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv {!!})) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv {!!})) ⟩
df v dv
∎)) where open ≡-Reasoning
diff-apply-proof-weak {bool} db b _ = xor-cancellative b db
-- That'd be the core of the proof of diff-apply-weak, if we relax the
-- equivalence in the goal to something more correct. We need a proof
-- of validity only for the first argument, somehow: maybe because
-- diff itself produces strongly valid changes?
--
-- Ahah! Not really: below we use the unprovable
-- diff-apply-proof-weak. To be able to use induction, we need to use
-- this weaker lemma again. Hence, we'll only be able to prove the
-- same equivalence, which will needs another proof of validity to
-- unfold further until the base case.
diff-apply-proof-weak-f : ∀ {τ₁ τ₂} (df : ⟦ Δ-Type (τ₁ ⇒ τ₂) ⟧) (f : ⟦ τ₁ ⇒ τ₂ ⟧) →
(Weak-Valid-Δ f df) → ∀ dv v → (Weak-Valid-Δ v dv) → (diff (apply df f) f) v dv ≡ df v dv
diff-apply-proof-weak-f {τ₁} {τ₂} df f df-valid dv v dv-valid =
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
≡⟨ diff-apply-proof-weak {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid)) ⟩
df v dv
∎ where open ≡-Reasoning
-}
-- A stronger delta equivalence. Note this isn't a congruence; it
-- should be possible to define some congruence rules, but those will
-- be more complex.
data _≈_ : {τ : Type} → ⟦ Δ-Type τ ⟧ → ⟦ Δ-Type τ ⟧ → Set where
--base : ∀ (v : ⟦ bool ⟧) → v ≈ v
base : ∀ (v1 v2 : ⟦ bool ⟧) → (≡ : v1 ≡ v2) → v1 ≈ v2
fun : ∀ {σ τ} (f1 f2 : ⟦ Δ-Type (σ ⇒ τ) ⟧) →
(t≈ : ∀ s ds (valid : Weak-Valid-Δ s ds) → f1 s ds ≈ f2 s ds) →
f1 ≈ f2
≡→≈ : ∀ {τ} {v1 v2 : ⟦ Δ-Type τ ⟧} → v1 ≡ v2 → v1 ≈ v2
≡→≈ {bool} {v1} {.v1} refl = base v1 v1 refl
≡→≈ {τ₁ ⇒ τ₂} {v1} {.v1} refl = fun v1 v1 (λ s ds _ → ≡→≈ refl)
open import Relation.Binary hiding (_⇒_)
≈-refl : ∀ {τ} → Reflexive (_≈_ {τ})
≈-refl = ≡→≈ refl
≈-sym : ∀ {τ} → Symmetric (_≈_ {τ})
≈-sym (base v1 v2 ≡) = base _ _ (sym ≡)
≈-sym {(σ ⇒ τ)} (fun f1 f2 t≈) = fun _ _ (λ s ds valid → ≈-sym (t≈ s ds valid))
≈-trans : ∀ {τ} → Transitive (_≈_ {τ})
≈-trans {.bool} {.k} {.k} {k} (base .k .k refl) (base .k .k refl) = base k k refl
≈-trans {σ ⇒ τ} {.f1} {.f2} {k} (fun f1 .f2 ≈₁) (fun f2 .k ≈₂) = fun f1 k (λ s ds valid → ≈-trans (≈₁ _ _ valid) (≈₂ _ _ valid))
≈-isEquivalence : ∀ {τ} → IsEquivalence (_≈_ {τ})
≈-isEquivalence = record
{ refl = ≈-refl
; sym = ≈-sym
; trans = ≈-trans
}
≈-setoid : Type → Setoid _ _
≈-setoid τ = record
{ Carrier = ⟦ Δ-Type τ ⟧
; _≈_ = _≈_
; isEquivalence = ≈-isEquivalence
}
module ≈-Reasoning where
module _ {τ : Type} where
open import Relation.Binary.EqReasoning (≈-setoid τ) public
hiding (_≡⟨_⟩_)
-- Correct proof, to refactor using ≈-Reasoning
diff-apply-proof-weak-real : ∀ {τ} (dv : ⟦ Δ-Type τ ⟧) (v : ⟦ τ ⟧) →
(Weak-Valid-Δ v dv) → diff (apply dv v) v ≈ dv
diff-apply-proof-weak-real {τ₁ ⇒ τ₂} df f df-valid = fun _ _ (λ v dv dv-valid → ≈-trans (≡→≈ (
begin
diff (apply (df (apply dv v) (derive (apply dv v))) (f (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(sym (proj₂ (df-valid (apply dv v) (derive (apply dv v)) (strong-to-weak-validity (derive-is-valid (apply dv v)))))) ⟩
diff ((apply df f) (apply (derive (apply dv v)) (apply dv v))) (f v)
≡⟨ ≡-cong
(λ x → diff (apply df f x) (f v))
(apply-derive (apply dv v)) ⟩
diff ((apply df f) (apply dv v)) (f v)
≡⟨ ≡-cong
(λ x → diff x (f v))
(proj₂ (df-valid v dv dv-valid)) ⟩
diff (apply (df v dv) (f v)) (f v)
∎)) (diff-apply-proof-weak-real {τ₂} (df v dv) (f v) (proj₁ (df-valid v dv dv-valid))))
--≈⟨ fill in the above proof ⟩
--df v dv
where open ≡-Reasoning
diff-apply-proof-weak-real {bool} db b _ = ≡→≈ (xor-cancellative b db)
|
Change where-clause to let-expr so that agda 2.3.2 typechecks README
|
Change where-clause to let-expr so that agda 2.3.2 typechecks README
Old-commit-hash: 6ffa74c100a7ee9d51e15d960db14c801f06e27c
|
Agda
|
mit
|
inc-lc/ilc-agda
|
cad090cf1face3e9ee7c8e4575440a427c74e66a
|
lib/Explore/BinTree.agda
|
lib/Explore/BinTree.agda
|
{-# OPTIONS --without-K #-}
module Explore.BinTree where
open import Data.Tree.Binary
open import Explore.Core
open import Explore.Properties
fromBinTree : ∀ {m} {A} → BinTree A → Explore m A
fromBinTree empty = empty-explore
fromBinTree (leaf x) = point-explore x
fromBinTree (fork ℓ r) = merge-explore (fromBinTree ℓ) (fromBinTree r)
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind empty = empty-explore-ind
fromBinTree-ind (leaf x) = point-explore-ind x
fromBinTree-ind (fork ℓ r) = merge-explore-ind (fromBinTree-ind ℓ)
(fromBinTree-ind r)
{-
fromBinTree : ∀ {m A} → BinTree A → Explore m A
fromBinTree (leaf x) _ _ f = f x
fromBinTree (fork ℓ r) ε _∙_ f = fromBinTree ℓ ε _∙_ f ∙ fromBinTree r ε _∙_ f
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind (leaf x) P _ P∙ Pf = Pf x
fromBinTree-ind (fork ℓ r) P Pε P∙ Pf = P∙ (fromBinTree-ind ℓ P Pε P∙ Pf)
(fromBinTree-ind r P Pε P∙ Pf)
-}
|
{-# OPTIONS --without-K #-}
module Explore.BinTree where
open import Level.NP
open import Type hiding (★)
open import Data.Tree.Binary
open import Data.Zero
open import Data.Product
open import Relation.Binary.PropositionalEquality.NP
open import HoTT
open Equivalences
open import Type.Identities
open import Function.Extensionality
open import Explore.Core
open import Explore.Properties
open import Explore.Zero
open import Explore.Sum
open import Explore.Isomorphism
fromBinTree : ∀ {m} {A} → BinTree A → Explore m A
fromBinTree empty = empty-explore
fromBinTree (leaf x) = point-explore x
fromBinTree (fork ℓ r) = merge-explore (fromBinTree ℓ) (fromBinTree r)
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind empty = empty-explore-ind
fromBinTree-ind (leaf x) = point-explore-ind x
fromBinTree-ind (fork ℓ r) = merge-explore-ind (fromBinTree-ind ℓ)
(fromBinTree-ind r)
{-
fromBinTree : ∀ {m A} → BinTree A → Explore m A
fromBinTree (leaf x) _ _ f = f x
fromBinTree (fork ℓ r) ε _∙_ f = fromBinTree ℓ ε _∙_ f ∙ fromBinTree r ε _∙_ f
fromBinTree-ind : ∀ {m p A} (t : BinTree A) → ExploreInd p (fromBinTree {m} t)
fromBinTree-ind (leaf x) P _ P∙ Pf = Pf x
fromBinTree-ind (fork ℓ r) P Pε P∙ Pf = P∙ (fromBinTree-ind ℓ P Pε P∙ Pf)
(fromBinTree-ind r P Pε P∙ Pf)
-}
AnyP≡ΣfromBinTree : ∀ {p}{A : ★ _}{P : A → ★ p}(xs : BinTree A) → Any P xs ≡ Σᵉ (fromBinTree xs) P
AnyP≡ΣfromBinTree empty = idp
AnyP≡ΣfromBinTree (leaf x) = idp
AnyP≡ΣfromBinTree (fork xs xs₁) = ⊎= (AnyP≡ΣfromBinTree xs) (AnyP≡ΣfromBinTree xs₁)
module _ {{_ : UA}}{{_ : FunExt}}{A : ★ ₀} where
exploreΣ∈ : ∀ {m} xs → Explore m (Σ A λ x → Any (_≡_ x) xs)
exploreΣ∈ empty = explore-iso (coe-equiv (Lift≡id ∙ ! ×𝟘-snd ∙ ×= idp (! Lift≡id))) Lift𝟘ᵉ
exploreΣ∈ (leaf x) = point-explore (x , idp)
exploreΣ∈ (fork xs xs₁) = explore-iso (coe-equiv (! Σ⊎-split)) (exploreΣ∈ xs ⊎ᵉ exploreΣ∈ xs₁)
Σᵉ-adq-exploreΣ∈ : ∀ {m} xs → Adequate-Σᵉ {m} (exploreΣ∈ xs)
Σᵉ-adq-exploreΣ∈ empty = Σ-iso-ok (coe-equiv (Lift≡id ∙ ! ×𝟘-snd ∙ ×= idp (! Lift≡id)))
{Aᵉ = Lift𝟘ᵉ} ΣᵉLift𝟘-ok
Σᵉ-adq-exploreΣ∈ (leaf x₁) F = ! Σ𝟙-snd ∙ Σ-fst≃ (≃-sym (Σx≡≃𝟙 x₁)) F
Σᵉ-adq-exploreΣ∈ (fork xs xs₁) = Σ-iso-ok (coe-equiv (! Σ⊎-split)) {Aᵉ = exploreΣ∈ xs ⊎ᵉ exploreΣ∈ xs₁}
(Σᵉ⊎-ok {eᴬ = exploreΣ∈ xs}{eᴮ = exploreΣ∈ xs₁} (Σᵉ-adq-exploreΣ∈ xs) (Σᵉ-adq-exploreΣ∈ xs₁))
module _ {{_ : UA}}{{_ : FunExt}}{A : ★ ₀}{P : A → ★ _}(explore-P : ∀ {m} x → Explore m (P x)) where
open import Explore.Zero
open import Explore.Sum
open import Explore.Isomorphism
exploreAny : ∀ {m} xs → Explore m (Any P xs)
exploreAny empty = Lift𝟘ᵉ
exploreAny (leaf x) = explore-P x
exploreAny (fork xs xs₁) = exploreAny xs ⊎ᵉ exploreAny xs₁
module _ (Σᵉ-adq-explore-P : ∀ {m} x → Adequate-Σᵉ {m} (explore-P x)) where
Σᵉ-adq-exploreAny : ∀ {m} xs → Adequate-Σᵉ {m} (exploreAny xs)
Σᵉ-adq-exploreAny empty F = ! Σ𝟘-lift∘fst ∙ Σ-fst= (! Lift≡id) _
Σᵉ-adq-exploreAny (leaf x₁) F = Σᵉ-adq-explore-P x₁ F
Σᵉ-adq-exploreAny (fork xs xs₁) F = ⊎= (Σᵉ-adq-exploreAny xs _) (Σᵉ-adq-exploreAny xs₁ _) ∙ ! dist-⊎-Σ
exploreΣᵉ : ∀ {m} xs → Explore m (Σᵉ (fromBinTree xs) P)
exploreΣᵉ {m} xs = fromBinTree-ind xs (λ e → Explore m (Σᵉ e P)) Lift𝟘ᵉ _⊎ᵉ_ explore-P
module _ (Σᵉ-adq-explore-P : ∀ {m} x → Adequate-Σᵉ {m} (explore-P x)) where
Σᵉ-adq-exploreΣᵉ : ∀ {m} xs → Adequate-Σᵉ {m} (exploreΣᵉ xs)
Σᵉ-adq-exploreΣᵉ empty F = ! Σ𝟘-lift∘fst ∙ Σ-fst= (! Lift≡id) _
Σᵉ-adq-exploreΣᵉ (leaf x₁) F = Σᵉ-adq-explore-P x₁ F
Σᵉ-adq-exploreΣᵉ (fork xs xs₁) F = ⊎= (Σᵉ-adq-exploreΣᵉ xs _) (Σᵉ-adq-exploreΣᵉ xs₁ _) ∙ ! dist-⊎-Σ
-- -}
-- -}
-- -}
-- -}
|
Add more exploration functions about binary trees
|
Add more exploration functions about binary trees
|
Agda
|
bsd-3-clause
|
crypto-agda/explore
|
15cd0eafd613a991286818c276496c93db0ada5d
|
Parametric/Denotation/CachingMValue.agda
|
Parametric/Denotation/CachingMValue.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Values for caching evaluation of MTerm
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.MType as MType
import Parametric.Denotation.Value as Value
import Parametric.Denotation.MValue as MValue
module Parametric.Denotation.CachingMValue
(Base : Type.Structure)
(⟦_⟧Base : Value.Structure Base)
where
open import Base.Denotation.Notation
open Type.Structure Base
open MType.Structure Base
open Value.Structure Base ⟦_⟧Base
open MValue.Structure Base ⟦_⟧Base
open import Data.Product hiding (map)
open import Data.Sum hiding (map)
open import Data.Unit
open import Level
open import Function hiding (const)
module Structure where
-- XXX: below we need to override just a few cases. Inheritance would handle
-- this precisely; without inheritance, we might want to use one of the
-- standard encodings of related features (delegation?).
⟦_⟧ValTypeHidCacheWrong : (τ : ValType) → Set₁
⟦_⟧CompTypeHidCacheWrong : (τ : CompType) → Set₁
-- This line is the only change, up to now, for the caching semantics starting from CBPV.
⟦ F τ ⟧CompTypeHidCacheWrong = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCacheWrong × ⟦ τ₁ ⟧ValTypeHidCacheWrong )
-- Delegation upward.
⟦ τ ⟧CompTypeHidCacheWrong = Lift ⟦ τ ⟧CompType
⟦_⟧ValTypeHidCacheWrong = Lift ∘ ⟦_⟧ValType
-- The above does not override what happens in recursive occurrences.
{-# NO_TERMINATION_CHECK #-}
⟦_⟧ValTypeHidCache : (τ : ValType) → Set
⟦_⟧CompTypeHidCache : (τ : CompType) → Set
⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache
⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧
⟦ vUnit ⟧ValTypeHidCache = ⊤
⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache
⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache
-- This line is the only change, up to now, for the caching semantics.
--
-- XXX The termination checker isn't happy with it, and it may be right ─ if
-- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big.
-- I think we don't do that unless we are caching a non-terminating
-- computation to begin with, but I'm not entirely sure.
--
-- However, the termination checker can't prove that the function is
-- terminating because it's not structurally recursive, but one call of the
-- function will produce another call of the function stuck on a neutral term:
-- So the computation will have terminated, just in an unusual way!
--
-- Anyway, I need not mechanize this part of the proof for my goals.
⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache )
⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set
⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache
{-
⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁
⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache
-}
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Values for caching evaluation of MTerm
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.MType as MType
import Parametric.Denotation.Value as Value
import Parametric.Denotation.MValue as MValue
module Parametric.Denotation.CachingMValue
(Base : Type.Structure)
(⟦_⟧Base : Value.Structure Base)
where
open import Base.Denotation.Notation
open Type.Structure Base
open MType.Structure Base
open Value.Structure Base ⟦_⟧Base
open MValue.Structure Base ⟦_⟧Base
open import Data.Product hiding (map)
open import Data.Sum hiding (map)
open import Data.Unit
open import Level
open import Function hiding (const)
module Structure where
{-# NO_TERMINATION_CHECK #-}
⟦_⟧ValTypeHidCache : (τ : ValType) → Set
⟦_⟧CompTypeHidCache : (τ : CompType) → Set
⟦ U c ⟧ValTypeHidCache = ⟦ c ⟧CompTypeHidCache
⟦ B ι ⟧ValTypeHidCache = ⟦ base ι ⟧
⟦ vUnit ⟧ValTypeHidCache = ⊤
⟦ τ₁ v× τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache × ⟦ τ₂ ⟧ValTypeHidCache
⟦ τ₁ v+ τ₂ ⟧ValTypeHidCache = ⟦ τ₁ ⟧ValTypeHidCache ⊎ ⟦ τ₂ ⟧ValTypeHidCache
--
-- XXX The termination checker isn't happy with it, and it may be right ─ if
-- you keep substituting τ₁ = U (F τ), you can make the cache arbitrarily big.
-- I think we don't do that unless we are caching a non-terminating
-- computation to begin with, but I'm not entirely sure.
--
-- However, the termination checker can't prove that the function is
-- terminating because it's not structurally recursive, but one call of the
-- function will produce another call of the function stuck on a neutral term:
-- So the computation will have terminated, just in an unusual way!
--
-- Anyway, I need not mechanize this part of the proof for my goals.
--
-- XXX: This line is the only change, up to now, for the caching semantics,
-- the rest is copied. Inheritance would handle this precisely; without
-- inheritance, we might want to use one of the standard encodings of related
-- features (delegation?).
⟦ F τ ⟧CompTypeHidCache = (Σ[ τ₁ ∈ ValType ] ⟦ τ ⟧ValTypeHidCache × ⟦ τ₁ ⟧ValTypeHidCache )
⟦ σ ⇛ τ ⟧CompTypeHidCache = ⟦ σ ⟧ValTypeHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValCtxHidCache : (Γ : ValContext) → Set
⟦_⟧ValCtxHidCache = DependentList ⟦_⟧ValTypeHidCache
{-
⟦_⟧CompCtxHidCache : (Γ : CompContext) → Set₁
⟦_⟧CompCtxHidCache = DependentList ⟦_⟧CompTypeHidCache
-}
|
Drop ⟦_⟧*TypeHidCacheWrong
|
Drop ⟦_⟧*TypeHidCacheWrong
This wrong definition should not be counted among the failures before
CBPV, since it was created after and it is due to a separate limitation.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
e7b75259e34cf9de11a2696f75d1384a4dc62f8d
|
agda/Nat.agda
|
agda/Nat.agda
|
module Nat where
data ℕ : Set where
zero : ℕ
succ : ℕ → ℕ
_+_ : ℕ → ℕ → ℕ
zero + b = b
succ a + b = succ (a + b)
_×_ : ℕ → ℕ → ℕ
zero × b = zero
succ a × b = (a × b) + b
open import Relation.Binary.PropositionalEquality
0-is-right-identity-of-+ : ∀ (n : ℕ) → n + zero ≡ n
0-is-right-identity-of-+ zero = refl
0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)
+-is-associative : ∀ (a b c : ℕ) → a + (b + c) ≡ (a + b) + c
+-is-associative zero b c = refl
+-is-associative (succ a) b c = cong succ (+-is-associative a b c)
lemma : ∀ (a b : ℕ) → a + succ b ≡ succ (a + b)
lemma zero b = refl
lemma (succ a) b = cong succ (lemma a b)
import Relation.Binary.EqReasoning as EqR
open module EqNat = EqR (setoid ℕ)
+-is-commutative : ∀ (a b : ℕ) → a + b ≡ b + a
+-is-commutative a zero = 0-is-right-identity-of-+ a
+-is-commutative a (succ b) =
begin
a + succ b
≈⟨ lemma a b ⟩
succ (a + b)
≈⟨ cong succ (+-is-commutative a b) ⟩
succ (b + a)
≈⟨ refl ⟩
succ b + a
∎
|
module Nat where
data ℕ : Set where
zero : ℕ
succ : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
{-# BUILTIN ZERO zero #-}
{-# BUILTIN SUC succ #-}
_+_ : ℕ → ℕ → ℕ
zero + b = b
succ a + b = succ (a + b)
_×_ : ℕ → ℕ → ℕ
zero × b = zero
succ a × b = (a × b) + b
open import Relation.Binary.PropositionalEquality
0-is-right-identity-of-+ : ∀ (n : ℕ) → n + zero ≡ n
0-is-right-identity-of-+ zero = refl
0-is-right-identity-of-+ (succ n) = cong succ (0-is-right-identity-of-+ n)
+-is-associative : ∀ (a b c : ℕ) → a + (b + c) ≡ (a + b) + c
+-is-associative zero b c = refl
+-is-associative (succ a) b c = cong succ (+-is-associative a b c)
lemma : ∀ (a b : ℕ) → a + succ b ≡ succ (a + b)
lemma zero b = refl
lemma (succ a) b = cong succ (lemma a b)
import Relation.Binary.EqReasoning as EqR
open module EqNat = EqR (setoid ℕ)
+-is-commutative : ∀ (a b : ℕ) → a + b ≡ b + a
+-is-commutative a zero = 0-is-right-identity-of-+ a
+-is-commutative a (succ b) =
begin
a + succ b
≈⟨ lemma a b ⟩
succ (a + b)
≈⟨ cong succ (+-is-commutative a b) ⟩
succ (b + a)
≈⟨ refl ⟩
succ b + a
∎
|
allow the use of more natural notation in Nat
|
allow the use of more natural notation in Nat
|
Agda
|
unlicense
|
piyush-kurur/sample-code
|
8e1932027422fbfd5254b377ea26f80df138163c
|
Parametric/Change/Term.agda
|
Parametric/Change/Term.agda
|
import Parametric.Syntax.Type as Type
import Base.Syntax.Context as Context
module Parametric.Change.Term
{Base : Set}
(Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -})
where
-- Terms that operate on changes
open Type Base
open Context Type
open import Parametric.Change.Type Δbase
open import Parametric.Syntax.Term Constant
open import Data.Product
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : Base → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : Base → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : Base → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
|
import Parametric.Syntax.Type as Type
import Base.Syntax.Context as Context
module Parametric.Change.Term
{Base : Set}
(Constant : Context.Context (Type.Type Base) → Type.Type Base → Set {- of constants -})
{Δbase : Base → Type.Type Base}
where
-- Terms that operate on changes
open Type Base
open Context Type
open import Parametric.Change.Type Δbase
open import Parametric.Syntax.Term Constant
open import Data.Product
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply :
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff :
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply :
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
|
Move parameter to module level.
|
Move parameter to module level.
All definitions in this module take the same parameter, so moving that
parameter to the module level makes it easier to instantiate all of the
definitions at once.
Old-commit-hash: c9a73b24cd02aed1ee75b46416c1893c8c37b130
|
Agda
|
mit
|
inc-lc/ilc-agda
|
bdfd1c903bebb00f241b98797e6544d8970bea49
|
Crypto/JS/BigI/FiniteField.agda
|
Crypto/JS/BigI/FiniteField.agda
|
{-# OPTIONS --without-K #-}
open import Type.Eq using (_==_; Eq?; ≡⇒==; ==⇒≡)
open import Data.Two.Base using (𝟚; ✓)
open import Relation.Binary.PropositionalEquality.Base using (_≡_; refl; ap)
open import FFI.JS using (JS[_]; _++_; return; _>>_)
open import FFI.JS.Check
using (check!)
renaming (check to check?)
--renaming (warn-check to check?)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Algebra.Raw
open import Algebra.Group
open import Algebra.Group.Homomorphism
open import Algebra.Field
-- TODO carry on a primality proof of q
module Crypto.JS.BigI.FiniteField (q : BigI) where
-- The constructor mk is not exported.
private
module Internals where
data ℤ[_] : Set where
mk : BigI → ℤ[_]
mk-inj : ∀ {x y : BigI} → ℤ[_].mk x ≡ mk y → x ≡ y
mk-inj refl = refl
open Internals public using (ℤ[_])
open Internals
ℤq : Set
ℤq = ℤ[_]
private
mod-q : BigI → ℤq
mod-q x = mk (mod x q)
-- There are two ways to go from BigI to ℤq: BigI▹ℤ[ q ] and mod-q
-- Use BigI▹ℤ[ q ] for untrusted input data and mod-q for internal
-- computation.
BigI▹ℤ[_] : BigI → JS[ ℤq ]
BigI▹ℤ[_] x =
-- Console.log "BigI▹ℤ[_]"
check! "below the modulus" (x <I q)
(λ _ → "Not below the modulus: q:" ++ toString q ++
" is less than x:" ++ toString x) >>
check! "positivity" (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") >>
return (mk x)
check-non-zero : ℤq → BigI
check-non-zero (mk x) = -- trace-call "check-non-zero "
check? (x >I 0I) (λ _ → "Should be non zero") x
ℤ[_]▹BigI : ℤq → BigI
ℤ[_]▹BigI (mk x) = x
0# 1# : ℤq
0# = mk 0I
1# = mk 1I
-- One could choose here to return a dummy value when 0 is given.
-- Instead we throw an exception which in some circumstances could
-- be bad if the runtime semantics is too eager.
1/_ : ℤq → ℤq
1/ x = mk (modInv (check-non-zero x) q)
_^I_ : ℤq → BigI → ℤq
mk x ^I y = mk (modPow x y q)
ℤq▹BigI = ℤ[_]▹BigI
BigI▹ℤq = BigI▹ℤ[_]
_⊗I_ : ℤq → BigI → ℤq
x ⊗I y = mod-q (multiply (ℤq▹BigI x) y)
_+_ _−_ _*_ _/_ : ℤq → ℤq → ℤq
x + y = mod-q (add (ℤq▹BigI x) (ℤq▹BigI y))
x − y = mod-q (subtract (ℤq▹BigI x) (ℤq▹BigI y))
x * y = x ⊗I ℤq▹BigI y
x / y = x * 1/ y
*-def : _*_ ≡ (λ x y → x ⊗I ℤq▹BigI y)
*-def = refl
0−_ : ℤq → ℤq
0− x = mod-q (negate (ℤq▹BigI x))
sum prod : List ℤq → ℤq
sum = foldr _+_ 0#
prod = foldr _*_ 1#
instance
ℤ[_]-Eq? : Eq? ℤq
ℤ[_]-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤq → ℤq → 𝟚
mk x ==' mk y = x == y
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
≡⇒==' {mk x} {mk y} p = ≡⇒== (mk-inj p)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
==⇒≡' {mk x} {mk y} p = ap mk (==⇒≡ p)
ℤq-Eq? = ℤ[_]-Eq?
+-mon-ops : Monoid-Ops ℤq
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops ℤq
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops ℤq
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops ℤq
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops ℤq
fld-ops = +-grp-ops , *-grp-ops
postulate fld-struct : Field-Struct fld-ops
fld : Field ℤq
fld = fld-ops , fld-struct
module fld = Field fld
-- open fld using (+-grp) public
ℤ[_]+-grp : Group ℤq
ℤ[_]+-grp = fld.+-grp
ℤq+-grp : Group ℤq
ℤq+-grp = fld.+-grp
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
open import Type.Eq using (_==_; Eq?; ≡⇒==; ==⇒≡)
open import Data.Two.Base using (𝟚; ✓)
open import Relation.Binary.PropositionalEquality.Base using (_≡_; refl; ap)
open import FFI.JS using (JS[_]; _++_; return; _>>_)
open import FFI.JS.Check
using (check!)
--renaming (check to check?)
renaming (warn-check to check?)
open import FFI.JS.BigI
open import Data.List.Base using (List; foldr)
open import Algebra.Raw
open import Algebra.Group
open import Algebra.Group.Homomorphism
open import Algebra.Field
-- TODO carry on a primality proof of q
module Crypto.JS.BigI.FiniteField (q : BigI) where
-- The constructor mk is not exported.
private
module Internals where
data ℤ[_] : Set where
mk : BigI → ℤ[_]
mk-inj : ∀ {x y : BigI} → ℤ[_].mk x ≡ mk y → x ≡ y
mk-inj refl = refl
open Internals public using (ℤ[_])
open Internals
ℤq : Set
ℤq = ℤ[_]
private
mod-q : BigI → ℤq
mod-q x = mk (mod x q)
-- There are two ways to go from BigI to ℤq: BigI▹ℤ[ q ] and mod-q
-- Use BigI▹ℤ[ q ] for untrusted input data and mod-q for internal
-- computation.
BigI▹ℤ[_] : BigI → JS[ ℤq ]
BigI▹ℤ[_] x =
-- Console.log "BigI▹ℤ[_]"
check! "below the modulus" (x <I q)
(λ _ → "Not below the modulus: q:" ++ toString q ++
" is less than x:" ++ toString x) >>
check! "positivity" (x ≥I 0I)
(λ _ → "Should be positive: " ++ toString x ++ " < 0") >>
return (mk x)
check-non-zero : ℤq → BigI
check-non-zero (mk x) = -- trace-call "check-non-zero "
check? (x >I 0I) (λ _ → "Should be non zero") x
ℤ[_]▹BigI : ℤq → BigI
ℤ[_]▹BigI (mk x) = x
0# 1# : ℤq
0# = mk 0I
1# = mk 1I
-- One could choose here to return a dummy value when 0 is given.
-- Instead we throw an exception which in some circumstances could
-- be bad if the runtime semantics is too eager.
1/_ : ℤq → ℤq
1/ x = mk (modInv (check-non-zero x) q)
_^I_ : ℤq → BigI → ℤq
mk x ^I y = mk (modPow x y q)
ℤq▹BigI = ℤ[_]▹BigI
BigI▹ℤq = BigI▹ℤ[_]
_⊗I_ : ℤq → BigI → ℤq
x ⊗I y = mod-q (multiply (ℤq▹BigI x) y)
_+_ _−_ _*_ _/_ : ℤq → ℤq → ℤq
x + y = mod-q (add (ℤq▹BigI x) (ℤq▹BigI y))
x − y = mod-q (subtract (ℤq▹BigI x) (ℤq▹BigI y))
x * y = x ⊗I ℤq▹BigI y
x / y = x * 1/ y
*-def : _*_ ≡ (λ x y → x ⊗I ℤq▹BigI y)
*-def = refl
0−_ : ℤq → ℤq
0− x = mod-q (negate (ℤq▹BigI x))
sum prod : List ℤq → ℤq
sum = foldr _+_ 0#
prod = foldr _*_ 1#
instance
ℤ[_]-Eq? : Eq? ℤq
ℤ[_]-Eq? = record
{ _==_ = _=='_
; ≡⇒== = ≡⇒=='
; ==⇒≡ = ==⇒≡' }
where
_=='_ : ℤq → ℤq → 𝟚
mk x ==' mk y = x == y
≡⇒==' : ∀ {x y} → x ≡ y → ✓ (x ==' y)
≡⇒==' {mk x} {mk y} p = ≡⇒== (mk-inj p)
==⇒≡' : ∀ {x y} → ✓ (x ==' y) → x ≡ y
==⇒≡' {mk x} {mk y} p = ap mk (==⇒≡ p)
ℤq-Eq? = ℤ[_]-Eq?
+-mon-ops : Monoid-Ops ℤq
+-mon-ops = _+_ , 0#
+-grp-ops : Group-Ops ℤq
+-grp-ops = +-mon-ops , 0−_
*-mon-ops : Monoid-Ops ℤq
*-mon-ops = _*_ , 1#
*-grp-ops : Group-Ops ℤq
*-grp-ops = *-mon-ops , 1/_
fld-ops : Field-Ops ℤq
fld-ops = +-grp-ops , *-grp-ops
postulate fld-struct : Field-Struct fld-ops
fld : Field ℤq
fld = fld-ops , fld-struct
module fld = Field fld
-- open fld using (+-grp) public
ℤ[_]+-grp : Group ℤq
ℤ[_]+-grp = fld.+-grp
ℤq+-grp : Group ℤq
ℤq+-grp = fld.+-grp
-- -}
-- -}
-- -}
-- -}
|
Use warn-check since check is monadic now...
|
ℤq: Use warn-check since check is monadic now...
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
3be2477097d1bfe6056a5d1fbcef0fb70636e4dc
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
{-# OPTIONS --universe-polymorphism #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≡≡_ : ∀ {a} {A : Set a} {i j : A}
(i≡j₁ : i ≡ j) (i≡j₂ : i ≡ j) → i≡j₁ ≡ i≡j₂
_≡≡_ refl refl = refl
_≟≡_ : ∀ {a} {A : Set a} {i j : A} → Decidable {A = i ≡ j} _≡_
_≟≡_ refl refl = yes refl
injective : ∀ {a} {A : Set a} → InjectiveRel A _≡_
injective refl refl = refl
surjective : ∀ {a} {A : Set a} → SurjectiveRel A _≡_
surjective refl refl = refl
bijective : ∀ {a} {A : Set a} → BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
module ≡-Reasoning {a} {A : Set a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : Set a} {B : Set b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦Set⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦Set⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → Set
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦Set₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
{-# OPTIONS --universe-polymorphism #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≡≡_ : ∀ {a} {A : Set a} {i j : A}
(i≡j₁ : i ≡ j) (i≡j₂ : i ≡ j) → i≡j₁ ≡ i≡j₂
_≡≡_ refl refl = refl
_≟≡_ : ∀ {a} {A : Set a} {i j : A} → Decidable {A = i ≡ j} _≡_
_≟≡_ refl refl = yes refl
_≗₂_ : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c} (f g : A → B → C) → Set _
f ≗₂ g = ∀ x y → f x y ≡ g x y
injective : ∀ {a} {A : Set a} → InjectiveRel A _≡_
injective refl refl = refl
surjective : ∀ {a} {A : Set a} → SurjectiveRel A _≡_
surjective refl refl = refl
bijective : ∀ {a} {A : Set a} → BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
module ≡-Reasoning {a} {A : Set a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : Set a} {B : Set b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦Set⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦Set⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → Set
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦Set₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦Set₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
add ≗₂
|
add ≗₂
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
89da077b4d098d6e0939d860c48061f66c5d3369
|
formalization/agda/Spire/Operational.agda
|
formalization/agda/Spire/Operational.agda
|
module Spire.Operational where
----------------------------------------------------------------------
data Level : Set where
zero : Level
suc : Level → Level
----------------------------------------------------------------------
data Context : Set
data Type (Γ : Context) : Set
data Value (Γ : Context) : Type Γ → Set
data Neutral (Γ : Context) : Type Γ → Set
----------------------------------------------------------------------
data Context where
∅ : Context
_,_ : (Γ : Context) → Type Γ → Context
data Type Γ where
`⊥ `⊤ `Bool : Type Γ
`Type : (ℓ : Level) → Type Γ
`Π `Σ : (A : Type Γ) (B : Type (Γ , A)) → Type Γ
`⟦_⟧ : ∀{ℓ} → Neutral Γ (`Type ℓ) → Type Γ
----------------------------------------------------------------------
⟦_⟧ : ∀{Γ ℓ} → Value Γ (`Type ℓ) → Type Γ
postulate
wknT : ∀{Γ A} → Type Γ → Type (Γ , A)
subT : ∀{Γ A} → Type (Γ , A) → Value Γ A → Type Γ
subV : ∀{Γ A B} → Value (Γ , A) B → (x : Value Γ A) → Value Γ (subT B x)
data Var : (Γ : Context) (A : Type Γ) → Set where
here : ∀{Γ A} → Var (Γ , A) (wknT A)
there : ∀{Γ A B} → Var Γ A → Var (Γ , B) (wknT A)
----------------------------------------------------------------------
data Value Γ where
{- Type introduction -}
`⊥ `⊤ `Bool `Type : ∀{ℓ} → Value Γ (`Type ℓ)
`Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ)
`⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (suc ℓ))
{- Value introduction -}
`tt : Value Γ `⊤
`true `false : Value Γ `Bool
_`,_ : ∀{A B} (a : Value Γ A) (b : Value Γ (subT B a)) → Value Γ (`Σ A B)
`λ : ∀{A B} → Value (Γ , A) B → Value Γ (`Π A B)
`neut : ∀{A} → Neutral Γ A → Value Γ A
----------------------------------------------------------------------
data Neutral Γ where
{- Value elimination -}
`var : ∀{A} → Var Γ A → Neutral Γ A
`if_`then_`else_ : ∀{C} (b : Neutral Γ `Bool) (c₁ c₂ : Value Γ C) → Neutral Γ C
`proj₁ : ∀{A B} → Neutral Γ (`Σ A B) → Neutral Γ A
`proj₂ : ∀{A B} (ab : Neutral Γ (`Σ A B)) → Neutral Γ (subT B (`neut (`proj₁ ab)))
_`$_ : ∀{A B} (f : Neutral Γ (`Π A B)) (a : Value Γ A) → Neutral Γ (subT B a)
----------------------------------------------------------------------
⟦ `Π A B ⟧ = `Π ⟦ A ⟧ ⟦ B ⟧
⟦ `Σ A B ⟧ = `Σ ⟦ A ⟧ ⟦ B ⟧
⟦ `⊥ ⟧ = `⊥
⟦ `⊤ ⟧ = `⊤
⟦ `Bool ⟧ = `Bool
⟦ `Type {zero} ⟧ = `⊥
⟦ `Type {suc ℓ} ⟧ = `Type ℓ
⟦ `⟦ A ⟧ ⟧ = ⟦ A ⟧
⟦ `neut A ⟧ = `⟦ A ⟧
----------------------------------------------------------------------
if_then_else_ : ∀{Γ C} (b : Value Γ `Bool) (c₁ c₂ : Value Γ C) → Value Γ C
if `true then c₁ else c₂ = c₁
if `false then c₁ else c₂ = c₂
if `neut b then c₁ else c₂ = `neut (`if b `then c₁ `else c₂)
----------------------------------------------------------------------
proj₁ : ∀{Γ A B} → Value Γ (`Σ A B) → Value Γ A
proj₁ (a `, b) = a
proj₁ (`neut ab) = `neut (`proj₁ ab)
proj₂ : ∀{Γ A B} (ab : Value Γ (`Σ A B)) → Value Γ (subT B (proj₁ ab))
proj₂ (a `, b) = b
proj₂ (`neut ab) = `neut (`proj₂ ab)
----------------------------------------------------------------------
_$_ : ∀{Γ A B} → Value Γ (`Π A B) → (a : Value Γ A) → Value Γ (subT B a)
`λ b $ a = subV b a
`neut f $ a = `neut (f `$ a)
----------------------------------------------------------------------
data Term (Γ : Context) : Type Γ → Set
eval : ∀{Γ A} → Term Γ A → Value Γ A
data Term Γ where
{- Type introduction -}
`⊥ `⊤ `Bool `Type : ∀{ℓ} → Term Γ (`Type ℓ)
`Π `Σ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (B : Term (Γ , ⟦ eval A ⟧) (`Type ℓ)) → Term Γ (`Type ℓ)
`⟦_⟧ : ∀{ℓ} → Term Γ (`Type ℓ) → Term Γ (`Type (suc ℓ))
{- Value introduction -}
`tt : Term Γ `⊤
`true `false : Term Γ `Bool
_`,_ : ∀{A B}
(a : Term Γ A) (b : Term Γ (subT B (eval a)))
→ Term Γ (`Σ A B)
{- Value elimination -}
`var : ∀{A} → Var Γ A → Term Γ A
`if_`then_`else_ : ∀{C}
(b : Term Γ `Bool)
(c₁ c₂ : Term Γ C)
→ Term Γ C
_`$_ : ∀{A B} (f : Term Γ (`Π A B)) (a : Term Γ A) → Term Γ (subT B (eval a))
`proj₁ : ∀{A B} → Term Γ (`Σ A B) → Term Γ A
`proj₂ : ∀{A B} (ab : Term Γ (`Σ A B)) → Term Γ (subT B (proj₁ (eval ab)))
----------------------------------------------------------------------
{- Type introduction -}
eval `⊥ = `⊥
eval `⊤ = `⊤
eval `Bool = `Bool
eval `Type = `Type
eval (`Π A B) = `Π (eval A) (eval B)
eval (`Σ A B) = `Σ (eval A) (eval B)
eval `⟦ A ⟧ = `⟦ eval A ⟧
{- Value introduction -}
eval `tt = `tt
eval `true = `true
eval `false = `false
eval (a `, b) = eval a `, eval b
{- Value elimination -}
eval (`var i) = `neut (`var i)
eval (`if b `then c₁ `else c₂) = if eval b then eval c₁ else eval c₂
eval (f `$ a) = eval f $ eval a
eval (`proj₁ ab) = proj₁ (eval ab)
eval (`proj₂ ab) = proj₂ (eval ab)
----------------------------------------------------------------------
|
module Spire.Operational where
----------------------------------------------------------------------
data Level : Set where
zero : Level
suc : Level → Level
----------------------------------------------------------------------
data Context : Set
data Type (Γ : Context) : Set
data Value (Γ : Context) : Type Γ → Set
data Neutral (Γ : Context) : Type Γ → Set
----------------------------------------------------------------------
data Context where
∅ : Context
_,_ : (Γ : Context) → Type Γ → Context
data Type Γ where
`⊥ `⊤ `Bool : Type Γ
`Type : (ℓ : Level) → Type Γ
`Π `Σ : (A : Type Γ) (B : Type (Γ , A)) → Type Γ
`⟦_⟧ : ∀{ℓ} → Neutral Γ (`Type ℓ) → Type Γ
----------------------------------------------------------------------
⟦_⟧ : ∀{Γ ℓ} → Value Γ (`Type ℓ) → Type Γ
postulate
wknT : ∀{Γ A} → Type Γ → Type (Γ , A)
subT : ∀{Γ A} → Type (Γ , A) → Value Γ A → Type Γ
subV : ∀{Γ A B} → Value (Γ , A) B → (x : Value Γ A) → Value Γ (subT B x)
data Var : (Γ : Context) (A : Type Γ) → Set where
here : ∀{Γ A} → Var (Γ , A) (wknT A)
there : ∀{Γ A B} → Var Γ A → Var (Γ , B) (wknT A)
----------------------------------------------------------------------
data Value Γ where
{- Type introduction -}
`⊥ `⊤ `Bool `Type : ∀{ℓ} → Value Γ (`Type ℓ)
`Π `Σ : ∀{ℓ} (A : Value Γ (`Type ℓ)) (B : Value (Γ , ⟦ A ⟧) (`Type ℓ)) → Value Γ (`Type ℓ)
`⟦_⟧ : ∀{ℓ} → Value Γ (`Type ℓ) → Value Γ (`Type (suc ℓ))
{- Value introduction -}
`tt : Value Γ `⊤
`true `false : Value Γ `Bool
_`,_ : ∀{A B} (a : Value Γ A) (b : Value Γ (subT B a)) → Value Γ (`Σ A B)
`λ : ∀{A B} → Value (Γ , A) B → Value Γ (`Π A B)
`neut : ∀{A} → Neutral Γ A → Value Γ A
----------------------------------------------------------------------
data Neutral Γ where
{- Value elimination -}
`var : ∀{A} → Var Γ A → Neutral Γ A
`if_`then_`else_ : ∀{C} (b : Neutral Γ `Bool) (c₁ c₂ : Value Γ C) → Neutral Γ C
`elimBool : ∀{ℓ} (P : Value (Γ , `Bool) (`Type ℓ))
(pt : Value Γ (subT ⟦ P ⟧ `true))
(pf : Value Γ (subT ⟦ P ⟧ `false))
(b : Neutral Γ `Bool) → Neutral Γ (subT ⟦ P ⟧ (`neut b))
`proj₁ : ∀{A B} → Neutral Γ (`Σ A B) → Neutral Γ A
`proj₂ : ∀{A B} (ab : Neutral Γ (`Σ A B)) → Neutral Γ (subT B (`neut (`proj₁ ab)))
_`$_ : ∀{A B} (f : Neutral Γ (`Π A B)) (a : Value Γ A) → Neutral Γ (subT B a)
----------------------------------------------------------------------
⟦ `Π A B ⟧ = `Π ⟦ A ⟧ ⟦ B ⟧
⟦ `Σ A B ⟧ = `Σ ⟦ A ⟧ ⟦ B ⟧
⟦ `⊥ ⟧ = `⊥
⟦ `⊤ ⟧ = `⊤
⟦ `Bool ⟧ = `Bool
⟦ `Type {zero} ⟧ = `⊥
⟦ `Type {suc ℓ} ⟧ = `Type ℓ
⟦ `⟦ A ⟧ ⟧ = ⟦ A ⟧
⟦ `neut A ⟧ = `⟦ A ⟧
----------------------------------------------------------------------
if_then_else_ : ∀{Γ C} (b : Value Γ `Bool) (c₁ c₂ : Value Γ C) → Value Γ C
if `true then c₁ else c₂ = c₁
if `false then c₁ else c₂ = c₂
if `neut b then c₁ else c₂ = `neut (`if b `then c₁ `else c₂)
elimBool : ∀{Γ ℓ} (P : Value (Γ , `Bool) (`Type ℓ))
(pt : Value Γ (subT ⟦ P ⟧ `true))
(pf : Value Γ (subT ⟦ P ⟧ `false))
(b : Value Γ `Bool)
→ Value Γ (subT ⟦ P ⟧ b)
elimBool P pt pf `true = pt
elimBool P pt pf `false = pf
elimBool P pt pf (`neut b) = `neut (`elimBool P pt pf b)
----------------------------------------------------------------------
proj₁ : ∀{Γ A B} → Value Γ (`Σ A B) → Value Γ A
proj₁ (a `, b) = a
proj₁ (`neut ab) = `neut (`proj₁ ab)
proj₂ : ∀{Γ A B} (ab : Value Γ (`Σ A B)) → Value Γ (subT B (proj₁ ab))
proj₂ (a `, b) = b
proj₂ (`neut ab) = `neut (`proj₂ ab)
----------------------------------------------------------------------
_$_ : ∀{Γ A B} → Value Γ (`Π A B) → (a : Value Γ A) → Value Γ (subT B a)
`λ b $ a = subV b a
`neut f $ a = `neut (f `$ a)
----------------------------------------------------------------------
data Term (Γ : Context) : Type Γ → Set
eval : ∀{Γ A} → Term Γ A → Value Γ A
data Term Γ where
{- Type introduction -}
`⊥ `⊤ `Bool `Type : ∀{ℓ} → Term Γ (`Type ℓ)
`Π `Σ : ∀{ℓ} (A : Term Γ (`Type ℓ)) (B : Term (Γ , ⟦ eval A ⟧) (`Type ℓ)) → Term Γ (`Type ℓ)
`⟦_⟧ : ∀{ℓ} → Term Γ (`Type ℓ) → Term Γ (`Type (suc ℓ))
{- Value introduction -}
`tt : Term Γ `⊤
`true `false : Term Γ `Bool
_`,_ : ∀{A B}
(a : Term Γ A) (b : Term Γ (subT B (eval a)))
→ Term Γ (`Σ A B)
{- Value elimination -}
`var : ∀{A} → Var Γ A → Term Γ A
`if_`then_`else_ : ∀{C}
(b : Term Γ `Bool)
(c₁ c₂ : Term Γ C)
→ Term Γ C
_`$_ : ∀{A B} (f : Term Γ (`Π A B)) (a : Term Γ A) → Term Γ (subT B (eval a))
`proj₁ : ∀{A B} → Term Γ (`Σ A B) → Term Γ A
`proj₂ : ∀{A B} (ab : Term Γ (`Σ A B)) → Term Γ (subT B (proj₁ (eval ab)))
`elimBool : ∀{ℓ} (P : Term (Γ , `Bool) (`Type ℓ))
(pt : Term Γ (subT ⟦ eval P ⟧ `true))
(pf : Term Γ (subT ⟦ eval P ⟧ `false))
(b : Term Γ `Bool)
→ Term Γ (subT ⟦ eval P ⟧ (eval b))
----------------------------------------------------------------------
{- Type introduction -}
eval `⊥ = `⊥
eval `⊤ = `⊤
eval `Bool = `Bool
eval `Type = `Type
eval (`Π A B) = `Π (eval A) (eval B)
eval (`Σ A B) = `Σ (eval A) (eval B)
eval `⟦ A ⟧ = `⟦ eval A ⟧
{- Value introduction -}
eval `tt = `tt
eval `true = `true
eval `false = `false
eval (a `, b) = eval a `, eval b
{- Value elimination -}
eval (`var i) = `neut (`var i)
eval (`if b `then c₁ `else c₂) = if eval b then eval c₁ else eval c₂
eval (f `$ a) = eval f $ eval a
eval (`proj₁ ab) = proj₁ (eval ab)
eval (`proj₂ ab) = proj₂ (eval ab)
eval (`elimBool P pt pf b) = elimBool (eval P) (eval pt) (eval pf) (eval b)
----------------------------------------------------------------------
|
Add elimBool to operational semantics.
|
Add elimBool to operational semantics.
|
Agda
|
bsd-3-clause
|
spire/spire
|
d28029e6a6b72c675115285102a2d423b1c0b3f9
|
prg.agda
|
prg.agda
|
module prg where
open import fun-universe
open import Function
open import Data.Nat.NP hiding (_==_)
open import Data.Bool.NP hiding (_==_)
open import Data.Nat.Properties
open import Data.Bool.Properties
open import Data.Bits
open import Data.Fin using (Fin; zero; suc)
open import Data.Product
open import Data.Empty
open import Data.Vec
open import Relation.Binary.PropositionalEquality.NP
PRGFun : (k n : ℕ) → Set
PRGFun k n = Bits k → Bits n
module PRG⅁₁ {k n} (PRG : PRGFun k n) where
-- TODO: give the adv some rand
Adv : Set
Adv = Bits n → Bit
chal : Bit → Bits (n + k) → Bits n
chal b R = if b then take n R else PRG (drop n R)
prg⅁ : Adv → Bit → Bits (n + k) → Bit
prg⅁ adv b R = adv (chal b R)
-- ``Looks pseudo-random if the message is the output of the PRG some key''
brute′ : Adv
brute′ m = search {k} _∨_ ((_==_ m) ∘ PRG)
-- ``Looks random if the message is not a possible output of the PRG''
brute : Adv
brute m = search {k} _∧_ (not ∘ (_==_ m) ∘ PRG)
private
brute-PRG-K≡0b-aux : ∀ {k n} (PRG : Bits k → Bits n) K
→ PRG⅁₁.brute PRG (PRG K) ≡ 0b
brute-PRG-K≡0b-aux {zero} {n} PRG [] = cong not (==-refl (PRG []))
brute-PRG-K≡0b-aux {suc k} {n} PRG (true ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 1∷_) K
= Bool°.+-comm (search _∧_ (not ∘ _==_ (PRG (1∷ K)) ∘ PRG ∘ 0∷_)) 0b
brute-PRG-K≡0b-aux {suc k} {n} PRG (false ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 0∷_) K = refl
brute′-bound-aux : ∀ {k n} (PRG : Bits k → Bits n)
→ let open PRG⅁₁ PRG in
#⟨ brute′ ⟩ ≤ 2^ k
brute′-bound-aux {zero} PRG = ℕ≤.reflexive #⟨ PRG [] ==⟩
brute′-bound-aux {suc k} PRG = #∨ {f = λ x → search _∨_ (_==_ x ∘ PRG ∘ 0∷_)}
(brute′-bound-aux (PRG ∘ 0∷_))
(brute′-bound-aux (PRG ∘ 1∷_))
module PRG⅁ {k n} (PRG : PRGFun k n) where
open PRG⅁₁ PRG public
brute-PRG-K≡0b : ∀ K → brute (PRG K) ≡ 0b
brute-PRG-K≡0b = brute-PRG-K≡0b-aux PRG
prg⅁-brute-0 : prg⅁ brute 0b ≗ (const 0b)
prg⅁-brute-0 R = brute-PRG-K≡0b (drop n R)
brute′-bound : #⟨ brute′ ⟩ ≤ 2^ k
brute′-bound = brute′-bound-aux PRG
not∘brute′≗brute : not ∘ brute′ ≗ brute
not∘brute′≗brute x = search-comm _∨_ _∧_ not (_==_ x ∘ PRG) de-morgan
brute≗not∘brute′ : brute′ ≗ not ∘ brute
brute≗not∘brute′ x = trans (sym (not-involutive _)) (cong not (not∘brute′≗brute x))
|
module prg where
open import fun-universe
open import Function
open import Data.Nat.NP hiding (_==_)
open import Data.Bool.NP hiding (_==_)
open import Data.Nat.Properties
open import Data.Bool.Properties
open import Data.Bits
open import Data.Fin using (Fin; zero; suc)
open import Data.Product
open import Data.Empty
open import Data.Vec
open import Relation.Binary.PropositionalEquality.NP
PRGFun : (k n : ℕ) → Set
PRGFun k n = Bits k → Bits n
module PRG⅁₁ {k n} (PRG : PRGFun k n) where
-- TODO: give the adv some rand
Adv : Set
Adv = Bits n → Bit
chal : Bit → Bits (n + k) → Bits n
chal b R = if b then take n R else PRG (drop n R)
prg⅁ : Adv → Bit → Bits (n + k) → Bit
prg⅁ adv b R = adv (chal b R)
-- ``Looks pseudo-random if the message is the output of the PRG some key''
brute′ : Adv
brute′ m = search _∨_ {k} ((_==_ m) ∘ PRG)
-- ``Looks random if the message is not a possible output of the PRG''
brute : Adv
brute m = search _∧_ {k} (not ∘ (_==_ m) ∘ PRG)
private
brute-PRG-K≡0b-aux : ∀ {k n} (PRG : Bits k → Bits n) K
→ PRG⅁₁.brute PRG (PRG K) ≡ 0b
brute-PRG-K≡0b-aux {zero} {n} PRG [] = cong not (==-refl (PRG []))
brute-PRG-K≡0b-aux {suc k} {n} PRG (true ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 1∷_) K
= Bool°.+-comm (search _∧_ (not ∘ _==_ (PRG (1∷ K)) ∘ PRG ∘ 0∷_)) 0b
brute-PRG-K≡0b-aux {suc k} {n} PRG (false ∷ K)
rewrite brute-PRG-K≡0b-aux (PRG ∘ 0∷_) K = refl
brute′-bound-aux : ∀ {k n} (PRG : Bits k → Bits n)
→ let open PRG⅁₁ PRG in
#⟨ brute′ ⟩ ≤ 2^ k
brute′-bound-aux {zero} PRG = ℕ≤.reflexive #⟨ PRG [] ==⟩
brute′-bound-aux {suc k} PRG = #∨ {f = λ x → search _∨_ (_==_ x ∘ PRG ∘ 0∷_)}
(brute′-bound-aux (PRG ∘ 0∷_))
(brute′-bound-aux (PRG ∘ 1∷_))
module PRG⅁ {k n} (PRG : PRGFun k n) where
open PRG⅁₁ PRG public
brute-PRG-K≡0b : ∀ K → brute (PRG K) ≡ 0b
brute-PRG-K≡0b = brute-PRG-K≡0b-aux PRG
prg⅁-brute-0 : prg⅁ brute 0b ≗ (const 0b)
prg⅁-brute-0 R = brute-PRG-K≡0b (drop n R)
brute′-bound : #⟨ brute′ ⟩ ≤ 2^ k
brute′-bound = brute′-bound-aux PRG
not∘brute′≗brute : not ∘ brute′ ≗ brute
not∘brute′≗brute x = search-hom _∨_ _∧_ not (_==_ x ∘ PRG) de-morgan
brute≗not∘brute′ : brute′ ≗ not ∘ brute
brute≗not∘brute′ x = trans (sym (not-involutive _)) (cong not (not∘brute′≗brute x))
|
fix argument order
|
prg: fix argument order
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
f274de0be6b9f871bb446ee6381969cac1a7ad8a
|
Parametric/Denotation/CachingEvaluation.agda
|
Parametric/Denotation/CachingEvaluation.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Caching evaluation
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
open import Base.Denotation.Notation
-- Extension Point: Evaluation of fully applied constants.
ValStructure : Set
ValStructure = ∀ {Σ τ} → ValConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
CompStructure : Set
CompStructure = ∀ {Σ τ} → CompConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
module Structure
(⟦_⟧ValBaseTermCache : ValStructure)
(⟦_⟧CompBaseTermCache : CompStructure)
where
{-
-- Prototype here the type-correctness of a simple non-standard semantics.
-- This describes a simplified version of the transformation by Liu and
-- Tanenbaum, PEPM 1995 - but for now, instead of producing object language
-- terms, we produce host language terms to take advantage of the richer type
-- system of the host language (in particular, here we need the unit type,
-- product types and *existentials*).
--
-- As usual, we'll later switch to a real term transformation.
-}
open import Data.Product hiding (map)
open import Data.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- The solution is to distinguish among different kinds of constants. Some are
-- value constructors (and thus do not return caches), while others are
-- computation constructors (and thus should return caches). For products, I
-- believe we will only use the products which are values, not computations
-- (XXX check CBPV paper for the name).
⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- This says that the environment does not contain caches... sounds wrong!
-- Either we add extra variables for the caches, or we store computations in
-- the environment (but that does not make sense), or we store caches in
-- values, by acting not on F but on something else (U?).
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- No caching, because the arguments are values, so evaluating them does not
-- produce intermediate results.
⟦ vConst c args ⟧ValTermCache ρ = ⟦ c ⟧ValBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- The only caching is done by the interpretation of the constant (because the
-- arguments are values so need no caching).
⟦_⟧CompTermCache (cConst c args) ρ = ⟦ c ⟧CompBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- Also, where are introduction forms for pairs and sums among values? With
-- them, we should see that we can interpret them without adding a cache.
-- Thunks keep seeming noops.
⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ
-- The effect of F is a writer monad of cached values, where the monoid is
-- (isomorphic to) the free monoid over (∃ τ . τ), but we push the
-- existentials up when pairing things!
-- That's what we're interpreting computations in. XXX maybe consider using
-- monads directly. But that doesn't deal with arity.
⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt)
-- For this to be enough, a lambda needs to return a produce, not to forward
-- the underlying one (unless there are no intermediate results). The correct
-- requirements can maybe be enforced through a linear typing discipline.
{-
-- Here we'd have a problem with the original into from CBPV, because it does
-- not require converting expressions to the "CBPV A-normal form".
⟦_⟧CompTermCache (v₁ into v₂) ρ =
-- Sequence commands and combine their caches.
{-
let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in , (r₂ , (c₁ ,′ c₂))
-}
-- The code above does not work, because we only guarantee that v₂ is
-- a computation, not that it's an F-computation - v₂ could also be function
-- type or a computation product.
-- We need a restricted CBPV, where these two possibilities are forbidden. If
-- you want to save something between different lambdas, you need to add an F
-- U to reflect that. (Double-check the papers which show how to encode arity
-- using CBPV, it seems that they should find the same problem --- XXX they
-- don't).
-- Instead, just drop the first cache (XXX WRONG).
⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ)
-}
-- But if we alter _into_ as described above, composing the caches works!
-- However, we should not forget we also need to save the new intermediate
-- result, that is the one produced by the first part of the let.
⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ =
let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in (σ v× τ₁ v× τ₂) , (r₂ ,′ ( r₁ ,′ c₁ ,′ c₂))
-- Note the compositionality and luck: we don't need to do anything at the
-- cReturn time, we just need the nested into to do their job, because as I
-- said intermediate results are a writer monad.
--
-- Q: But then, do we still need to do all the other stuff? IOW, do we still
-- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <-
-- f args; z <- g args'; z)?
--
-- A: One thing we still need is using the monadic version of into for the
-- same reasons - which makes sense, since it has the type of monadic bind.
--
-- Maybe not: if we use a monad, which respects left and right identity, the
-- two above forms are equivalent. But what about associativity? We don't have
-- associativity with nested tuples in the middle. That's why the monad uses
-- lists! We can also use nested tuple, as long as in the into case we don't
-- do (a, b) but append a b (ahem, where?), which decomposes the first list
-- and prepends it to the second. To this end, we need to know the type of the
-- first element, or to ensure it's always a pair. Maybe we just want to reuse
-- an HList.
-- In abstractions, we should start collecting all variables...
-- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache,
-- that's moved into the handling of cReturn. This makes *the* difference for
-- nested lambdas, where we don't need to create caches multiple times!
⟦_⟧CompTermCache (cAbs v) ρ = λ x → ⟦ v ⟧CompTermCache (x • ρ)
-- Here we see that we are in a sort of A-normal form, because the argument is
-- a value (not quite ANF though, since values can be thunks - that is,
-- computations which haven't been run yet, I guess. Do we have an use for
-- that? That allows passing lambdas as arguments directly - which is fine,
-- because producing a closure indeed does not have intermediate results!).
⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
⟦_⟧TermCacheCBV : ∀ {τ Γ} → Term Γ τ → ⟦ fromCBVCtx Γ ⟧ValCtxHidCache → ⟦ cbvToCompType τ ⟧CompTypeHidCache
⟦ t ⟧TermCacheCBV = ⟦ fromCBV t ⟧CompTermCache
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Caching evaluation
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Syntax.MType as MType
import Parametric.Syntax.MTerm as MTerm
import Parametric.Denotation.Value as Value
import Parametric.Denotation.Evaluation as Evaluation
import Parametric.Denotation.MValue as MValue
import Parametric.Denotation.CachingMValue as CachingMValue
import Parametric.Denotation.MEvaluation as MEvaluation
module Parametric.Denotation.CachingEvaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
(⟦_⟧Const : Evaluation.Structure Const ⟦_⟧Base)
(ValConst : MTerm.ValConstStructure Const)
(CompConst : MTerm.CompConstStructure Const)
-- I should really switch to records - can it get sillier than this?
(⟦_⟧ValBase : MEvaluation.ValStructure Const ⟦_⟧Base ValConst CompConst)
(⟦_⟧CompBase : MEvaluation.CompStructure Const ⟦_⟧Base ValConst CompConst)
where
open Type.Structure Base
open Term.Structure Base Const
open MType.Structure Base
open MTerm.Structure Const ValConst CompConst
open Value.Structure Base ⟦_⟧Base
open Evaluation.Structure Const ⟦_⟧Base ⟦_⟧Const
open MValue.Structure Base ⟦_⟧Base
open CachingMValue.Structure Base ⟦_⟧Base
open MEvaluation.Structure Const ⟦_⟧Base ValConst CompConst ⟦_⟧ValBase ⟦_⟧CompBase
open import Base.Denotation.Notation
-- Extension Point: Evaluation of fully applied constants.
ValStructure : Set
ValStructure = ∀ {Σ τ} → ValConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
CompStructure : Set
CompStructure = ∀ {Σ τ} → CompConst Σ τ → ⟦ Σ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
module Structure
(⟦_⟧ValBaseTermCache : ValStructure)
(⟦_⟧CompBaseTermCache : CompStructure)
where
{-
-- Prototype here the type-correctness of a simple non-standard semantics.
-- This describes a simplified version of the transformation by Liu and
-- Tanenbaum, PEPM 1995 - but for now, instead of producing object language
-- terms, we produce host language terms to take advantage of the richer type
-- system of the host language (in particular, here we need the unit type,
-- product types and *existentials*).
--
-- As usual, we'll later switch to a real term transformation.
-}
open import Data.Product hiding (map)
open import Data.Unit
-- Defining a caching semantics for Term proves to be hard, requiring to
-- insert and remove caches where we apply constants.
-- Indeed, our plugin interface is not satisfactory for adding caching. CBPV can help us.
-- The solution is to distinguish among different kinds of constants. Some are
-- value constructors (and thus do not return caches), while others are
-- computation constructors (and thus should return caches). For products, I
-- believe we will only use the products which are values, not computations
-- (XXX check CBPV paper for the name).
⟦_⟧CompTermCache : ∀ {τ Γ} → Comp Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧CompTypeHidCache
⟦_⟧ValTermCache : ∀ {τ Γ} → Val Γ τ → ⟦ Γ ⟧ValCtxHidCache → ⟦ τ ⟧ValTypeHidCache
⟦_⟧ValsTermCache : ∀ {Γ Σ} → Vals Γ Σ → ⟦ Γ ⟧ValCtxHidCache → ⟦ Σ ⟧ValCtxHidCache
open import Base.Denotation.Environment ValType ⟦_⟧ValTypeHidCache public
using ()
renaming (⟦_⟧Var to ⟦_⟧ValVarHidCache)
-- This says that the environment does not contain caches... sounds wrong!
-- Either we add extra variables for the caches, or we store computations in
-- the environment (but that does not make sense), or we store caches in
-- values, by acting not on F but on something else (U?).
-- Copy of ⟦_⟧Vals
⟦ ∅ ⟧ValsTermCache ρ = ∅
⟦ vt • valtms ⟧ValsTermCache ρ = ⟦ vt ⟧ValTermCache ρ • ⟦ valtms ⟧ValsTermCache ρ
-- I suspect the plan was to use extra variables; that's annoying to model in
-- Agda but easier in implementations.
⟦ vVar x ⟧ValTermCache ρ = ⟦ x ⟧ValVarHidCache ρ
⟦ vThunk x ⟧ValTermCache ρ = ⟦ x ⟧CompTermCache ρ
-- No caching, because the arguments are values, so evaluating them does not
-- produce intermediate results.
⟦ vConst c args ⟧ValTermCache ρ = ⟦ c ⟧ValBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- The only caching is done by the interpretation of the constant (because the
-- arguments are values so need no caching).
⟦_⟧CompTermCache (cConst c args) ρ = ⟦ c ⟧CompBaseTermCache (⟦ args ⟧ValsTermCache ρ)
-- Also, where are introduction forms for pairs and sums among values? With
-- them, we should see that we can interpret them without adding a cache.
-- Thunks keep seeming noops.
⟦_⟧CompTermCache (cForce x) ρ = ⟦ x ⟧ValTermCache ρ
-- The effect of F is a writer monad of cached values, where the monoid is
-- (isomorphic to) the free monoid over (∃ τ . τ), but we push the
-- existentials up when pairing things!
-- That's what we're interpreting computations in. XXX maybe consider using
-- monads directly. But that doesn't deal with arity.
⟦_⟧CompTermCache (cReturn v) ρ = vUnit , (⟦ v ⟧ValTermCache ρ , tt)
-- For this to be enough, a lambda needs to return a produce, not to forward
-- the underlying one (unless there are no intermediate results). The correct
-- requirements can maybe be enforced through a linear typing discipline.
{-
-- Here we'd have a problem with the original into from CBPV, because it does
-- not require converting expressions to the "CBPV A-normal form".
⟦_⟧CompTermCache (v₁ into v₂) ρ =
-- Sequence commands and combine their caches.
{-
let (_ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(_ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in , (r₂ , (c₁ ,′ c₂))
-}
-- The code above does not work, because we only guarantee that v₂ is
-- a computation, not that it's an F-computation - v₂ could also be function
-- type or a computation product.
-- We need a restricted CBPV, where these two possibilities are forbidden. If
-- you want to save something between different lambdas, you need to add an F
-- U to reflect that. (Double-check the papers which show how to encode arity
-- using CBPV, it seems that they should find the same problem --- XXX they
-- don't).
-- Instead, just drop the first cache (XXX WRONG).
⟦ v₂ ⟧CompTermCache (proj₁ (proj₂ (⟦ v₁ ⟧CompTermCache ρ)) • ρ)
-}
-- But if we alter _into_ as described above, composing the caches works!
-- However, we should not forget we also need to save the new intermediate
-- result, that is the one produced by the first part of the let.
⟦_⟧CompTermCache (_into_ {σ} {τ} v₁ v₂) ρ =
let (τ₁ , (r₁ , c₁)) = ⟦ v₁ ⟧CompTermCache ρ
(τ₂ , (r₂ , c₂)) = ⟦ v₂ ⟧CompTermCache (r₁ • ρ)
in (τ₁ v× τ₂ v× σ) , (r₂ ,′ (c₁ ,′ c₂ ,′ r₁))
-- Note the compositionality and luck: we don't need to do anything at the
-- cReturn time, we just need the nested into to do their job, because as I
-- said intermediate results are a writer monad.
--
-- Q: But then, do we still need to do all the other stuff? IOW, do we still
-- need to forbid (λ x . y <- f args; g args') and replace it with (λ x . y <-
-- f args; z <- g args'; z)?
--
-- A: One thing we still need is using the monadic version of into for the
-- same reasons - which makes sense, since it has the type of monadic bind.
--
-- Maybe not: if we use a monad, which respects left and right identity, the
-- two above forms are equivalent. But what about associativity? We don't have
-- associativity with nested tuples in the middle. That's why the monad uses
-- lists! We can also use nested tuple, as long as in the into case we don't
-- do (a, b) but append a b (ahem, where?), which decomposes the first list
-- and prepends it to the second. To this end, we need to know the type of the
-- first element, or to ensure it's always a pair. Maybe we just want to reuse
-- an HList.
-- In abstractions, we should start collecting all variables...
-- Here, unlike in ⟦_⟧TermCache, we don't need to invent an empty cache,
-- that's moved into the handling of cReturn. This makes *the* difference for
-- nested lambdas, where we don't need to create caches multiple times!
⟦_⟧CompTermCache (cAbs v) ρ = λ x → ⟦ v ⟧CompTermCache (x • ρ)
-- Here we see that we are in a sort of A-normal form, because the argument is
-- a value (not quite ANF though, since values can be thunks - that is,
-- computations which haven't been run yet, I guess. Do we have an use for
-- that? That allows passing lambdas as arguments directly - which is fine,
-- because producing a closure indeed does not have intermediate results!).
⟦_⟧CompTermCache (cApp t v) ρ = ⟦ t ⟧CompTermCache ρ (⟦ v ⟧ValTermCache ρ)
⟦_⟧TermCacheCBV : ∀ {τ Γ} → Term Γ τ → ⟦ fromCBVCtx Γ ⟧ValCtxHidCache → ⟦ cbvToCompType τ ⟧CompTypeHidCache
⟦ t ⟧TermCacheCBV = ⟦ fromCBV t ⟧CompTermCache
|
Fix order of returned caches
|
Fix order of returned caches
|
Agda
|
mit
|
inc-lc/ilc-agda
|
a127cbe0afe5735ec22de57b718586c54a38c33b
|
arrow.agda
|
arrow.agda
|
data Bool : Set where
true : Bool
false : Bool
_or_ : Bool → Bool → Bool
true or _ = true
_ or true = true
false or false = false
_and_ : Bool → Bool → Bool
false and _ = false
_ and false = false
true and true = true
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
infixr 5 _∷_
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
any : {A : Set} → (A → Bool) → List A → Bool
any _ ∘ = false
any f (x ∷ xs) = (f x) or (any f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q ≡ s
(p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ ∘ = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs ∘)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ ∘ ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
proofs : List Arrow
proofs =
(3 ⇒ (⇒ 4)) ∷
-- (5 ⇒ (⇒ 4)) ∷
(6 ⇒ (⇒ 4)) ∷
(3 ⇒ (⇒ 3)) ∷
(3 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(5 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 7)) ∷
(6 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 7)) ∷
(5 ⇒ (⇒ 10)) ∷
(10 ⇒ (⇒ 4)) ∷
(5 ⇒ (⇒ 11)) ∷
(6 ⇒ (⇒ 11)) ∷
(11 ⇒ (⇒ 4)) ∷
(10 ⇒ (⇒ 7)) ∷
(8 ⇒ (⇒ 4)) ∷
(9 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 8)) ∷
(3 ⇒ (⇒ 8)) ∷
(5 ⇒ (3 ⇒ (⇒ 9))) ∷
(6 ⇒ (7 ⇒ (⇒ 10))) ∷
(6 ⇒ (3 ⇒ (⇒ 3))) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (8 ⇒ (⇒ 10))) ∷
(3 ⇒ (10 ⇒ (⇒ 9))) ∷
(5 ⇒ (⇒ 1)) ∷
(3 ⇒ (1 ⇒ (⇒ 9))) ∷
(1 ⇒ (⇒ 2)) ∷
(10 ⇒ (⇒ 2)) ∷ ∘
cms : List Separation
cms =
(model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷
(model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷
(model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷
(model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷
(model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷
(model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷
(model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷
(model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘
testp : Arrow
testp = (5 ⇒ (⇒ 10))
testd : Arrow
testd = (5 ⇒ (⇒ 7))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
|
data Bool : Set where
true : Bool
false : Bool
_or_ : Bool → Bool → Bool
true or _ = true
_ or true = true
false or false = false
_and_ : Bool → Bool → Bool
false and _ = false
_ and false = false
true and true = true
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
infixr 5 _∷_
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
any : {A : Set} → (A → Bool) → List A → Bool
any _ ∘ = false
any f (x ∷ xs) = (f x) or (any f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q ≡ s
(p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ ∘ = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs ∘)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ ∘ ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
proofs : List Arrow
proofs =
(3 ⇒ (⇒ 4)) ∷
-- (5 ⇒ (⇒ 4)) ∷
(6 ⇒ (⇒ 4)) ∷
(3 ⇒ (⇒ 3)) ∷
(3 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(5 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 7)) ∷
(6 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 7)) ∷
(5 ⇒ (⇒ 10)) ∷
(10 ⇒ (⇒ 4)) ∷
(5 ⇒ (⇒ 11)) ∷
(6 ⇒ (⇒ 11)) ∷
(11 ⇒ (⇒ 4)) ∷
(10 ⇒ (⇒ 7)) ∷
(8 ⇒ (⇒ 4)) ∷
(9 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 8)) ∷
(3 ⇒ (⇒ 8)) ∷
(5 ⇒ (3 ⇒ (⇒ 9))) ∷
(6 ⇒ (7 ⇒ (⇒ 10))) ∷
(6 ⇒ (3 ⇒ (⇒ 3))) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (8 ⇒ (⇒ 10))) ∷
(3 ⇒ (10 ⇒ (⇒ 9))) ∷
(5 ⇒ (⇒ 1)) ∷
(3 ⇒ (1 ⇒ (⇒ 9))) ∷
(1 ⇒ (⇒ 2)) ∷
(10 ⇒ (⇒ 2)) ∷ ∘
cms : List Separation
cms =
(model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷
(model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷
(model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷
(model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷
(model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷
(model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷
(model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷
(model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘
testp : Arrow
testp = (5 ⇒ (⇒ 10))
testd : Arrow
testd = (5 ⇒ (⇒ 4))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
|
Fix testd
|
Fix testd
|
Agda
|
mit
|
louisswarren/hieretikz
|
3682d42d35e0734a6a4d6a4ee62cbad3965309b9
|
Parametric/Change/Term.agda
|
Parametric/Change/Term.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Terms that operate on changes (Fig. 3).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Const
open ChangeType.Structure Base ΔBase
open import Data.Product
-- Extension point 1: A term for ⊝ on base types.
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
-- Extension point 2: A term for ⊕ on base types.
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
-- We provide: terms for ⊕ and ⊝ on arbitrary types.
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
absV 4 (λ g f x Δx → app g (x ⊕σ Δx) ⊝τ app f x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
absV 3 (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ τ {Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff _ = app₂ diff-term
apply : ∀ τ {Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply _ = app₂ apply-term
infixl 6 apply diff
syntax apply τ x Δx = Δx ⊕₍ τ ₎ x
syntax diff τ x y = x ⊝₍ τ ₎ y
infixl 6 _⊕_ _⊝_
_⊝_ : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
_⊝_ {τ} = diff τ
_⊕_ : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
_⊕_ {τ} = app₂ apply-term
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Terms that operate on changes (Fig. 3).
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Change.Type as ChangeType
module Parametric.Change.Term
{Base : Set}
(Const : Term.Structure Base)
(ΔBase : ChangeType.Structure Base)
where
-- Terms that operate on changes
open Type.Structure Base
open Term.Structure Base Const
open ChangeType.Structure Base ΔBase
open import Data.Product
-- Extension point 1: A term for ⊝ on base types.
DiffStructure : Set
DiffStructure = ∀ {ι Γ} → Term Γ (base ι ⇒ base ι ⇒ base (ΔBase ι))
-- Extension point 2: A term for ⊕ on base types.
ApplyStructure : Set
ApplyStructure = ∀ {ι Γ} → Term Γ (ΔType (base ι) ⇒ base ι ⇒ base ι)
module Structure
(diff-base : DiffStructure)
(apply-base : ApplyStructure)
where
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
-- We provide: terms for ⊕ and ⊝ on arbitrary types.
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
diff-term {base ι} = diff-base
diff-term {σ ⇒ τ} =
(let
_⊝τ_ = λ {Γ} s t → app₂ (diff-term {τ} {Γ}) s t
_⊕σ_ = λ {Γ} t Δt → app₂ (apply-term {σ} {Γ}) Δt t
in
absV 4 (λ g f x Δx → app g (x ⊕σ Δx) ⊝τ app f x))
apply-term {base ι} = apply-base
apply-term {σ ⇒ τ} =
(let
_⊝σ_ = λ {Γ} s t → app₂ (diff-term {σ} {Γ}) s t
_⊕τ_ = λ {Γ} t Δt → app₂ (apply-term {τ} {Γ}) Δt t
in
absV 3 (λ Δh h y → app h y ⊕τ app (app Δh y) (y ⊝σ y)))
diff : ∀ τ {Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
diff _ = app₂ diff-term
apply : ∀ τ {Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
apply _ = app₂ apply-term
infixl 6 apply diff
syntax apply τ x Δx = Δx ⊕₍ τ ₎ x
syntax diff τ x y = x ⊝₍ τ ₎ y
infixl 6 _⊕_ _⊝_
_⊝_ : ∀ {τ Γ} →
Term Γ τ → Term Γ τ →
Term Γ (ΔType τ)
_⊝_ {τ} = diff τ
_⊕_ : ∀ {τ Γ} →
Term Γ (ΔType τ) → Term Γ τ →
Term Γ τ
_⊕_ {τ} = apply τ
|
Make _⊕_ more coherent with _⊝_
|
Make _⊕_ more coherent with _⊝_
|
Agda
|
mit
|
inc-lc/ilc-agda
|
5cd94161fed3259ba24dc7c2099fc439a5fdadb1
|
New/Derive.agda
|
New/Derive.agda
|
module New.Derive where
open import New.Lang
open import New.Changes
open import New.LangOps
ΔΓ : Context → Context
ΔΓ ∅ = ∅
ΔΓ (τ • Γ) = Δt τ • τ • ΔΓ Γ
Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔΓ Γ
Γ≼ΔΓ {∅} = ∅
Γ≼ΔΓ {τ • Γ} = drop Δt τ • keep τ • Γ≼ΔΓ
deriveConst : ∀ {τ} →
Const τ →
Term ∅ (Δt τ)
-- dplus = λ m dm n dn → (m + dm) + (n + dn) - (m + n) = dm + dn
deriveConst plus = abs (abs (abs (abs (app₂ (const plus) (var (that (that this))) (var this)))))
-- minus = λ m n → m - n
-- dminus = λ m dm n dn → (m + dm) - (n + dn) - (m - n) = dm - dn
deriveConst minus = abs (abs (abs (abs (app₂ (const minus) (var (that (that this))) (var this)))))
deriveConst cons = abs (abs (abs (abs (app (app (const cons) (var (that (that this)))) (var this)))))
deriveConst fst = abs (abs (app (const fst) (var this)))
deriveConst snd = abs (abs (app (const snd) (var this)))
-- deriveConst linj = abs (abs (app (const linj) (app (const linj) (var this))))
-- deriveConst rinj = abs (abs (app (const linj) (app (const rinj) (var this))))
-- deriveConst (match {t1} {t2} {t3}) =
-- -- λ s ds f df g dg →
-- abs (abs (abs (abs (abs (abs
-- -- match ds
-- (app₃ (const match) (var (that (that (that (that this)))))
-- -- λ ds₁ → match ds₁
-- (abs (app₃ (const match) (var this)
-- -- case inj₁ da → absV 1 (λ da → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- λ a → app₂ df a da
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (var (that this))))
-- -- absurd: λ b → dg b (nil b)
-- (abs (app₂ (var (that (that (that this)))) (var this) (app (onilτo t2) (var this))))))
-- -- case inj₂ db → absV 1 (λ db → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- absurd: λ a → df a (nil a)
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (app (onilτo t1) (var this))))
-- -- λ b → app₂ dg b db
-- (abs (app₂ (var (that (that (that this)))) (var this) (var (that this))))))))
-- -- recomputation branch:
-- -- λ s2 → ominus (match s2 (f ⊕ df) (g ⊕ dg)) (match s f g)
-- (abs (app₂ (ominusτo t3)
-- -- (match s2 (f ⊕ df) (g ⊕ dg))
-- (app₃ (const match)
-- (var this)
-- (app₂ (oplusτo (t1 ⇒ t3))
-- (var (that (that (that (that this)))))
-- (var (that (that (that this)))))
-- (app₂ (oplusτo (t2 ⇒ t3))
-- (var (that (that this)))
-- (var (that this))))
-- -- (match s f g)
-- (app₃ (const match)
-- (var (that (that (that (that (that (that this)))))))
-- (var (that (that (that (that this)))))
-- (var (that (that this))))))))))))
-- {-
-- derive (λ s f g → match s f g) =
-- λ s ds f df g dg →
-- case ds of
-- ch1 da →
-- case s of
-- inj1 a → df a da
-- inj2 b → {- absurd -} dg b (nil b)
-- ch2 db →
-- case s of
-- inj2 b → dg b db
-- inj1 a → {- absurd -} df a (nil a)
-- rp s2 →
-- match (f ⊕ df) (g ⊕ dg) s2 ⊝
-- match f g s
-- -}
deriveVar : ∀ {Γ τ} → Var Γ τ → Var (ΔΓ Γ) (Δt τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔΓ Γ) τ
fit = weaken Γ≼ΔΓ
derive : ∀ {Γ τ} → Term Γ τ → Term (ΔΓ Γ) (Δt τ)
derive (const c) = weaken (∅≼Γ {ΔΓ _}) (deriveConst c)
derive (var x) = var (deriveVar x)
derive (app s t) = app (app (derive s) (fit t)) (derive t)
derive (abs t) = abs (abs (derive t))
open import New.LangChanges
-- Lemmas needed to reason about derivation, for any correctness proof
alternate : ∀ {Γ} → ⟦ Γ ⟧Context → eCh Γ → ⟦ ΔΓ Γ ⟧Context
alternate {∅} ∅ ∅ = ∅
alternate {τ • Γ} (v • ρ) (dv • dρ) = dv • v • alternate ρ dρ
⟦Γ≼ΔΓ⟧ : ∀ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
ρ ≡ ⟦ Γ≼ΔΓ ⟧≼ (alternate ρ dρ)
⟦Γ≼ΔΓ⟧ ∅ ∅ = refl
⟦Γ≼ΔΓ⟧ (v • ρ) (dv • dρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ ρ dρ)
fit-sound : ∀ {Γ τ} → (t : Term Γ τ) →
(ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
⟦ t ⟧Term ρ ≡ ⟦ fit t ⟧Term (alternate ρ dρ)
fit-sound t ρ dρ = trans
(cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ ρ dρ))
(sym (weaken-sound t _))
-- The change semantics is just the semantics composed with derivation!
⟦_⟧ΔVar : ∀ {Γ τ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ x ⟧ΔVar ρ dρ = ⟦ deriveVar x ⟧Var (alternate ρ dρ)
⟦_⟧ΔTerm : ∀ {Γ τ} → (t : Term Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ t ⟧ΔTerm ρ dρ = ⟦ derive t ⟧Term (alternate ρ dρ)
⟦_⟧ΔConst : ∀ {τ} (c : Const τ) → Cht τ
⟦ c ⟧ΔConst = ⟦ deriveConst c ⟧Term ∅
⟦_⟧ΔConst-rewrite : ∀ {τ Γ} (c : Const τ) (ρ : ⟦ Γ ⟧Context) dρ → ⟦_⟧ΔTerm (const c) ρ dρ ≡ ⟦ c ⟧ΔConst
⟦ c ⟧ΔConst-rewrite ρ dρ rewrite weaken-sound {Γ₁≼Γ₂ = ∅≼Γ} (deriveConst c) (alternate ρ dρ) | ⟦∅≼Γ⟧-∅ (alternate ρ dρ) = refl
|
module New.Derive where
open import New.Lang
open import New.Changes
open import New.LangOps
deriveConst : ∀ {τ} →
Const τ →
Term ∅ (Δt τ)
-- dplus = λ m dm n dn → (m + dm) + (n + dn) - (m + n) = dm + dn
deriveConst plus = abs (abs (abs (abs (app₂ (const plus) (var (that (that this))) (var this)))))
-- minus = λ m n → m - n
-- dminus = λ m dm n dn → (m + dm) - (n + dn) - (m - n) = dm - dn
deriveConst minus = abs (abs (abs (abs (app₂ (const minus) (var (that (that this))) (var this)))))
deriveConst cons = abs (abs (abs (abs (app (app (const cons) (var (that (that this)))) (var this)))))
deriveConst fst = abs (abs (app (const fst) (var this)))
deriveConst snd = abs (abs (app (const snd) (var this)))
-- deriveConst linj = abs (abs (app (const linj) (app (const linj) (var this))))
-- deriveConst rinj = abs (abs (app (const linj) (app (const rinj) (var this))))
-- deriveConst (match {t1} {t2} {t3}) =
-- -- λ s ds f df g dg →
-- abs (abs (abs (abs (abs (abs
-- -- match ds
-- (app₃ (const match) (var (that (that (that (that this)))))
-- -- λ ds₁ → match ds₁
-- (abs (app₃ (const match) (var this)
-- -- case inj₁ da → absV 1 (λ da → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- λ a → app₂ df a da
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (var (that this))))
-- -- absurd: λ b → dg b (nil b)
-- (abs (app₂ (var (that (that (that this)))) (var this) (app (onilτo t2) (var this))))))
-- -- case inj₂ db → absV 1 (λ db → match s
-- (abs (app₃ (const match) (var (that (that (that (that (that (that (that this))))))))
-- -- absurd: λ a → df a (nil a)
-- (abs (app₂ (var (that (that (that (that (that this)))))) (var this) (app (onilτo t1) (var this))))
-- -- λ b → app₂ dg b db
-- (abs (app₂ (var (that (that (that this)))) (var this) (var (that this))))))))
-- -- recomputation branch:
-- -- λ s2 → ominus (match s2 (f ⊕ df) (g ⊕ dg)) (match s f g)
-- (abs (app₂ (ominusτo t3)
-- -- (match s2 (f ⊕ df) (g ⊕ dg))
-- (app₃ (const match)
-- (var this)
-- (app₂ (oplusτo (t1 ⇒ t3))
-- (var (that (that (that (that this)))))
-- (var (that (that (that this)))))
-- (app₂ (oplusτo (t2 ⇒ t3))
-- (var (that (that this)))
-- (var (that this))))
-- -- (match s f g)
-- (app₃ (const match)
-- (var (that (that (that (that (that (that this)))))))
-- (var (that (that (that (that this)))))
-- (var (that (that this))))))))))))
-- {-
-- derive (λ s f g → match s f g) =
-- λ s ds f df g dg →
-- case ds of
-- ch1 da →
-- case s of
-- inj1 a → df a da
-- inj2 b → {- absurd -} dg b (nil b)
-- ch2 db →
-- case s of
-- inj2 b → dg b db
-- inj1 a → {- absurd -} df a (nil a)
-- rp s2 →
-- match (f ⊕ df) (g ⊕ dg) s2 ⊝
-- match f g s
-- -}
ΔΓ : Context → Context
ΔΓ ∅ = ∅
ΔΓ (τ • Γ) = Δt τ • τ • ΔΓ Γ
Γ≼ΔΓ : ∀ {Γ} → Γ ≼ ΔΓ Γ
Γ≼ΔΓ {∅} = ∅
Γ≼ΔΓ {τ • Γ} = drop Δt τ • keep τ • Γ≼ΔΓ
deriveVar : ∀ {Γ τ} → Var Γ τ → Var (ΔΓ Γ) (Δt τ)
deriveVar this = this
deriveVar (that x) = that (that (deriveVar x))
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔΓ Γ) τ
fit = weaken Γ≼ΔΓ
derive : ∀ {Γ τ} → Term Γ τ → Term (ΔΓ Γ) (Δt τ)
derive (const c) = weaken (∅≼Γ {ΔΓ _}) (deriveConst c)
derive (var x) = var (deriveVar x)
derive (app s t) = app (app (derive s) (fit t)) (derive t)
derive (abs t) = abs (abs (derive t))
open import New.LangChanges
-- Lemmas needed to reason about derivation, for any correctness proof
alternate : ∀ {Γ} → ⟦ Γ ⟧Context → eCh Γ → ⟦ ΔΓ Γ ⟧Context
alternate {∅} ∅ ∅ = ∅
alternate {τ • Γ} (v • ρ) (dv • dρ) = dv • v • alternate ρ dρ
⟦Γ≼ΔΓ⟧ : ∀ {Γ} (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
ρ ≡ ⟦ Γ≼ΔΓ ⟧≼ (alternate ρ dρ)
⟦Γ≼ΔΓ⟧ ∅ ∅ = refl
⟦Γ≼ΔΓ⟧ (v • ρ) (dv • dρ) = cong₂ _•_ refl (⟦Γ≼ΔΓ⟧ ρ dρ)
fit-sound : ∀ {Γ τ} → (t : Term Γ τ) →
(ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) →
⟦ t ⟧Term ρ ≡ ⟦ fit t ⟧Term (alternate ρ dρ)
fit-sound t ρ dρ = trans
(cong ⟦ t ⟧Term (⟦Γ≼ΔΓ⟧ ρ dρ))
(sym (weaken-sound t _))
-- The change semantics is just the semantics composed with derivation!
⟦_⟧ΔVar : ∀ {Γ τ} → (x : Var Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ x ⟧ΔVar ρ dρ = ⟦ deriveVar x ⟧Var (alternate ρ dρ)
⟦_⟧ΔTerm : ∀ {Γ τ} → (t : Term Γ τ) → (ρ : ⟦ Γ ⟧Context) (dρ : eCh Γ) → Cht τ
⟦ t ⟧ΔTerm ρ dρ = ⟦ derive t ⟧Term (alternate ρ dρ)
⟦_⟧ΔConst : ∀ {τ} (c : Const τ) → Cht τ
⟦ c ⟧ΔConst = ⟦ deriveConst c ⟧Term ∅
⟦_⟧ΔConst-rewrite : ∀ {τ Γ} (c : Const τ) (ρ : ⟦ Γ ⟧Context) dρ → ⟦_⟧ΔTerm (const c) ρ dρ ≡ ⟦ c ⟧ΔConst
⟦ c ⟧ΔConst-rewrite ρ dρ rewrite weaken-sound {Γ₁≼Γ₂ = ∅≼Γ} (deriveConst c) (alternate ρ dρ) | ⟦∅≼Γ⟧-∅ (alternate ρ dρ) = refl
|
Reorder definitions
|
Reorder definitions
Move definitions about change contexts next to their uses.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
42316d32e1b90cea16ece1ad29627cb678610bc6
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
_≙_ : ∀ dx dy → Set a
dx ≙ dy = x ⊞ dx ≡ x ⊞ dy
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = refl
≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-symm ≙ = sym ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma
where
open ≡-Reasoning
open import Postulate.Extensionality
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
_≙_ : ∀ dx dy → Set a
dx ≙ dy = x ⊞ dx ≡ x ⊞ dy
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = refl
≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-symm ≙ = sym ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = lemma
where
open ≡-Reasoning
open import Postulate.Extensionality
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
|
Clarify that change algebra operations trivially preserve d.o.e.
|
Clarify that change algebra operations trivially preserve d.o.e.
Old-commit-hash: c11fb0c1abae938f0c7ec3dacff96f112f6bbee4
|
Agda
|
mit
|
inc-lc/ilc-agda
|
b3c29af8a0e31056b3efe8dd76723fe0d834058a
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
lib/Relation/Binary/PropositionalEquality/NP.agda
|
{-# OPTIONS --without-K #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Type hiding (★)
open import Data.One using (𝟙)
open import Data.Product using (Σ; _,_)
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
private
module Dummy {a} {A : ★ a} where
open IsEquivalence (isEquivalence {a} {A}) public hiding (refl; sym; trans)
open Dummy public
PathOver : ∀ {i j} {A : ★ i} (B : A → ★ j)
{x y : A} (p : x ≡ y) (u : B x) (v : B y) → ★ j
PathOver B refl u v = (u ≡ v)
syntax PathOver B p u v =
u ≡ v [ B ↓ p ]
-- Some definitions with the names from Agda-HoTT
-- this is only a temporary state of affairs...
module _ {a} {A : ★ a} where
idp : {x : A} → x ≡ x
idp = refl
infixr 8 _∙_ _∙'_
_∙_ _∙'_ : {x y z : A} → (x ≡ y → y ≡ z → x ≡ z)
refl ∙ q = q
q ∙' refl = q
! : {x y : A} → (x ≡ y → y ≡ x)
! refl = refl
ap↓ : ∀ {i j k} {A : ★ i} {B : A → ★ j} {C : A → ★ k}
(g : {a : A} → B a → C a) {x y : A} {p : x ≡ y}
{u : B x} {v : B y}
→ (u ≡ v [ B ↓ p ] → g u ≡ g v [ C ↓ p ])
ap↓ g {p = refl} refl = refl
ap : ∀ {i j} {A : ★ i} {B : ★ j} (f : A → B) {x y : A}
→ (x ≡ y → f x ≡ f y)
ap f p = ap↓ {A = 𝟙} f {p = refl} p
apd : ∀ {i j} {A : ★ i} {B : A → ★ j} (f : (a : A) → B a) {x y : A}
→ (p : x ≡ y) → f x ≡ f y [ B ↓ p ]
apd f refl = refl
coe : ∀ {i} {A B : ★ i} (p : A ≡ B) → A → B
coe refl x = x
coe! : ∀ {i} {A B : ★ i} (p : A ≡ B) → B → A
coe! refl x = x
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≗₂_ : ∀ {a b c} {A : ★ a} {B : ★ b} {C : ★ c} (f g : A → B → C) → ★ _
f ≗₂ g = ∀ x y → f x y ≡ g x y
module _ {a} {A : ★ a} where
injective : InjectiveRel A _≡_
injective p q = p ∙ ! q
surjective : SurjectiveRel A _≡_
surjective p q = ! p ∙ q
bijective : BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
Equalizer : ∀ {b} {B : A → ★ b} (f g : (x : A) → B x) → ★ _
Equalizer f g = Σ A (λ x → f x ≡ g x)
{- In a category theory context this type would the object 'E'
and 'proj₁' would be the morphism 'eq : E → A' such that
given any object O, and morphism 'm : O → A', there exists
a unique morphism 'u : O → E' such that 'eq ∘ u ≡ m'.
-}
module ≡-Reasoning {a} {A : ★ a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : ★ a} {B : ★ b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦★⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦★⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → ★
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦★₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
{-# OPTIONS --without-K #-}
-- move this to propeq
module Relation.Binary.PropositionalEquality.NP where
open import Type hiding (★)
open import Data.One using (𝟙)
open import Data.Product using (Σ; _,_)
open import Relation.Binary.PropositionalEquality public hiding (module ≡-Reasoning)
open import Relation.Binary.NP
open import Relation.Binary.Bijection
open import Relation.Binary.Logical
open import Relation.Nullary
private
module Dummy {a} {A : ★ a} where
open IsEquivalence (isEquivalence {a} {A}) public hiding (refl; sym; trans)
open Dummy public
PathOver : ∀ {i j} {A : ★ i} (B : A → ★ j)
{x y : A} (p : x ≡ y) (u : B x) (v : B y) → ★ j
PathOver B refl u v = (u ≡ v)
syntax PathOver B p u v =
u ≡ v [ B ↓ p ]
-- Some definitions with the names from Agda-HoTT
-- this is only a temporary state of affairs...
module _ {a} {A : ★ a} where
idp : {x : A} → x ≡ x
idp = refl
infixr 8 _∙_ _∙'_
_∙_ _∙'_ : {x y z : A} → (x ≡ y → y ≡ z → x ≡ z)
refl ∙ q = q
q ∙' refl = q
! : {x y : A} → (x ≡ y → y ≡ x)
! refl = refl
ap↓ : ∀ {i j k} {A : ★ i} {B : A → ★ j} {C : A → ★ k}
(g : {a : A} → B a → C a) {x y : A} {p : x ≡ y}
{u : B x} {v : B y}
→ (u ≡ v [ B ↓ p ] → g u ≡ g v [ C ↓ p ])
ap↓ g {p = refl} refl = refl
ap : ∀ {i j} {A : ★ i} {B : ★ j} (f : A → B) {x y : A}
→ (x ≡ y → f x ≡ f y)
ap f p = ap↓ {A = 𝟙} f {p = refl} p
apd : ∀ {i j} {A : ★ i} {B : A → ★ j} (f : (a : A) → B a) {x y : A}
→ (p : x ≡ y) → f x ≡ f y [ B ↓ p ]
apd f refl = refl
coe : ∀ {i} {A B : ★ i} (p : A ≡ B) → A → B
coe refl x = x
coe! : ∀ {i} {A B : ★ i} (p : A ≡ B) → B → A
coe! refl x = x
cong₃ : ∀ {a b c d} {A : Set a} {B : Set b} {C : Set c} {D : Set d}
(f : A → B → C → D) {a₁ a₂ b₁ b₂ c₁ c₂}
→ a₁ ≡ a₂ → b₁ ≡ b₂ → c₁ ≡ c₂ → f a₁ b₁ c₁ ≡ f a₂ b₂ c₂
cong₃ f refl refl refl = refl
_≗₂_ : ∀ {a b c} {A : ★ a} {B : ★ b} {C : ★ c} (f g : A → B → C) → ★ _
f ≗₂ g = ∀ x y → f x y ≡ g x y
module _ {a} {A : ★ a} where
injective : InjectiveRel A _≡_
injective p q = p ∙ ! q
surjective : SurjectiveRel A _≡_
surjective p q = ! p ∙ q
bijective : BijectiveRel A _≡_
bijective = record { injectiveREL = injective; surjectiveREL = surjective }
Equalizer : ∀ {b} {B : A → ★ b} (f g : (x : A) → B x) → ★ _
Equalizer f g = Σ A (λ x → f x ≡ g x)
{- In a category theory context this type would the object 'E'
and 'proj₁' would be the morphism 'eq : E → A' such that
given any object O, and morphism 'm : O → A', there exists
a unique morphism 'u : O → E' such that 'eq ∘ u ≡ m'.
-}
Pullback : ∀ {b c} {B : ★ b} {C : ★ c} (f : A → C) (g : B → C) → ★ _
Pullback {B = B} f g = Σ A (λ x → Σ B (λ y → f x ≡ g y))
module ≡-Reasoning {a} {A : ★ a} where
open Setoid-Reasoning (setoid A) public renaming (_≈⟨_⟩_ to _≡⟨_⟩_)
module ≗-Reasoning {a b} {A : ★ a} {B : ★ b} where
open Setoid-Reasoning (A →-setoid B) public renaming (_≈⟨_⟩_ to _≗⟨_⟩_)
data ⟦≡⟧ {a₁ a₂ aᵣ}
{A₁ A₂} (Aᵣ : ⟦★⟧ {a₁} {a₂} aᵣ A₁ A₂)
{x₁ x₂} (xᵣ : Aᵣ x₁ x₂)
: (Aᵣ ⟦→⟧ ⟦★⟧ aᵣ) (_≡_ x₁) (_≡_ x₂) where
-- : ∀ {y₁ y₂} (yᵣ : Aᵣ y₁ y₂) → x₁ ≡ y₁ → x₂ ≡ y₂ → ★
⟦refl⟧ : ⟦≡⟧ Aᵣ xᵣ xᵣ refl refl
-- Double checking level 0 with a direct ⟦_⟧ encoding
private
⟦≡⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ Aᵣ ⟦→⟧ Aᵣ ⟦→⟧ ⟦★₀⟧) _≡_ _≡_
⟦≡⟧′ = ⟦≡⟧
⟦refl⟧′ : (∀⟨ Aᵣ ∶ ⟦★₀⟧ ⟩⟦→⟧ ∀⟨ xᵣ ∶ Aᵣ ⟩⟦→⟧ ⟦≡⟧ Aᵣ xᵣ xᵣ) refl refl
⟦refl⟧′ _ _ = ⟦refl⟧
|
Add Pullback
|
Add Pullback
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
e58fdcf2bd3f99745f653c3ee049cb1244bcb092
|
lib/Relation/Binary/NP.agda
|
lib/Relation/Binary/NP.agda
|
{-# OPTIONS --without-K #-}
{-# OPTIONS --universe-polymorphism #-}
module Relation.Binary.NP where
open import Level
open import Relation.Binary public
import Relation.Binary.PropositionalEquality as PropEq
-- could this be moved in place of Trans is Relation.Binary.Core
Trans' : ∀ {a b c ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : Set b} {C : Set c} →
REL A B ℓ₁ → REL B C ℓ₂ → REL A C ℓ₃ → Set _
Trans' P Q R = ∀ {i j k} (x : P i j) (xs : Q j k) → R i k
substitutive-to-reflexive : ∀ {a ℓ ℓ'} {A : Set a} {≈ : Rel A ℓ} {≡ : Rel A ℓ'}
→ Substitutive ≡ ℓ → Reflexive ≈ → ≡ ⇒ ≈
substitutive-to-reflexive {≈ = ≈} ≡-subst ≈-refl xᵣ = ≡-subst (≈ _) xᵣ ≈-refl
substitutive⇒≡ : ∀ {a ℓ} {A : Set a} {≡ : Rel A ℓ} → Substitutive ≡ a → ≡ ⇒ PropEq._≡_
substitutive⇒≡ subst = substitutive-to-reflexive {≈ = PropEq._≡_} subst PropEq.refl
record Equality {a ℓ} {A : Set a} (_≡_ : Rel A ℓ) : Set (suc a ⊔ ℓ) where
field
isEquivalence : IsEquivalence _≡_
subst : Substitutive _≡_ a
open IsEquivalence isEquivalence public
to-reflexive : ∀ {≈} → Reflexive ≈ → _≡_ ⇒ ≈
to-reflexive = substitutive-to-reflexive subst
to-propositional : _≡_ ⇒ PropEq._≡_
to-propositional = substitutive⇒≡ subst
-- Equational reasoning combinators.
module Trans-Reasoning {a ℓ} {A : Set a} (_≈_ : Rel A ℓ) (trans : Transitive _≈_) where
infix 2 finally
infixr 2 _≈⟨_⟩_
_≈⟨_⟩_ : ∀ x {y z : A} → x ≈ y → y ≈ z → x ≈ z
_ ≈⟨ x≈y ⟩ y≈z = trans x≈y y≈z
-- When there is no reflexivty available this
-- combinator can be used to end the reasoning.
finally : ∀ (x y : A) → x ≈ y → x ≈ y
finally _ _ x≈y = x≈y
syntax finally x y x≈y = x ≈⟨ x≈y ⟩∎ y ∎
module Refl-Trans-Reasoning
{a ℓ} {A : Set a} (_≈_ : Rel A ℓ)
(refl : Reflexive _≈_)
(trans : Transitive _≈_) where
open Trans-Reasoning _≈_ trans public hiding (finally)
infix 2 _∎
_∎ : ∀ x → x ≈ x
_ ∎ = refl
infixr 2 _≈⟨-by-definition-⟩_
_≈⟨-by-definition-⟩_ : ∀ x {y : A} → x ≈ y → x ≈ y
_ ≈⟨-by-definition-⟩ x≈y = x≈y
module Equivalence-Reasoning
{a ℓ} {A : Set a} {_≈_ : Rel A ℓ}
(E : IsEquivalence _≈_) where
open IsEquivalence E
open Refl-Trans-Reasoning _≈_ refl trans public
module Preorder-Reasoning
{p₁ p₂ p₃} (P : Preorder p₁ p₂ p₃) where
open Preorder P
open Refl-Trans-Reasoning _∼_ refl trans public
renaming (_≈⟨_⟩_ to _∼⟨_⟩_; _≈⟨-by-definition-⟩_ to _∼⟨-by-definition-⟩_)
open Equivalence-Reasoning isEquivalence public renaming (_∎ to _☐)
module Setoid-Reasoning {a ℓ} (s : Setoid a ℓ) where
open Equivalence-Reasoning (Setoid.isEquivalence s) public
|
{-# OPTIONS --without-K #-}
{-# OPTIONS --universe-polymorphism #-}
module Relation.Binary.NP where
open import Level
open import Relation.Binary public
import Relation.Binary.PropositionalEquality as PropEq
-- could this be moved in place of Trans is Relation.Binary.Core
Trans' : ∀ {a b c ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : Set b} {C : Set c} →
REL A B ℓ₁ → REL B C ℓ₂ → REL A C ℓ₃ → Set _
Trans' P Q R = ∀ {i j k} (x : P i j) (xs : Q j k) → R i k
substitutive-to-reflexive : ∀ {a ℓ ℓ'} {A : Set a} {≈ : Rel A ℓ} {≡ : Rel A ℓ'}
→ Substitutive ≡ ℓ → Reflexive ≈ → ≡ ⇒ ≈
substitutive-to-reflexive {≈ = ≈} ≡-subst ≈-refl xᵣ = ≡-subst (≈ _) xᵣ ≈-refl
substitutive⇒≡ : ∀ {a ℓ} {A : Set a} {≡ : Rel A ℓ} → Substitutive ≡ a → ≡ ⇒ PropEq._≡_
substitutive⇒≡ subst = substitutive-to-reflexive {≈ = PropEq._≡_} subst PropEq.refl
record Equality {a ℓ} {A : Set a} (_≡_ : Rel A ℓ) : Set (suc a ⊔ ℓ) where
field
isEquivalence : IsEquivalence _≡_
subst : Substitutive _≡_ a
open IsEquivalence isEquivalence public
to-reflexive : ∀ {≈} → Reflexive ≈ → _≡_ ⇒ ≈
to-reflexive = substitutive-to-reflexive subst
to-propositional : _≡_ ⇒ PropEq._≡_
to-propositional = substitutive⇒≡ subst
-- Equational reasoning combinators.
module Trans-Reasoning {a ℓ} {A : Set a} (_≈_ : Rel A ℓ) (trans : Transitive _≈_) where
infix 2 finally
infixr 2 _≈⟨_⟩_
_≈⟨_⟩_ : ∀ x {y z : A} → x ≈ y → y ≈ z → x ≈ z
_ ≈⟨ x≈y ⟩ y≈z = trans x≈y y≈z
-- When there is no reflexivty available this
-- combinator can be used to end the reasoning.
finally : ∀ (x y : A) → x ≈ y → x ≈ y
finally _ _ x≈y = x≈y
syntax finally x y x≈y = x ≈⟨ x≈y ⟩∎ y ∎
module Refl-Trans-Reasoning
{a ℓ} {A : Set a} (_≈_ : Rel A ℓ)
(refl : Reflexive _≈_)
(trans : Transitive _≈_) where
open Trans-Reasoning _≈_ trans public hiding (finally)
infix 2 _∎
_∎ : ∀ x → x ≈ x
_ ∎ = refl
infixr 2 _≈⟨by-definition⟩_
_≈⟨by-definition⟩_ : ∀ x {y : A} → x ≈ y → x ≈ y
_ ≈⟨by-definition⟩ x≈y = x≈y
module Equivalence-Reasoning
{a ℓ} {A : Set a} {_≈_ : Rel A ℓ}
(E : IsEquivalence _≈_) where
open IsEquivalence E
open Refl-Trans-Reasoning _≈_ refl trans public
module Preorder-Reasoning
{p₁ p₂ p₃} (P : Preorder p₁ p₂ p₃) where
open Preorder P
open Refl-Trans-Reasoning _∼_ refl trans public
renaming (_≈⟨_⟩_ to _∼⟨_⟩_; _≈⟨by-definition⟩_ to _∼⟨by-definition⟩_)
open Equivalence-Reasoning isEquivalence public renaming (_∎ to _☐)
module Setoid-Reasoning {a ℓ} (s : Setoid a ℓ) where
open Equivalence-Reasoning (Setoid.isEquivalence s) public
|
Rename by-definition combinator for eq-reasoning
|
Rename by-definition combinator for eq-reasoning
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
a0cb2129d6925e76e7c69250fbe510b3a711b245
|
Parametric/Denotation/Evaluation.agda
|
Parametric/Denotation/Evaluation.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Standard evaluation (Def. 3.3 and Fig. 4i)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
-- Extension Point: Evaluation of fully applied constants.
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
-- We provide: Evaluation of arbitrary terms.
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
-- this is what we'd like to write.
-- unfortunately termination checker complains.
--
-- ⟦ terms ⟧Terms ρ = map-IVT (λ t → ⟦ t ⟧Term ρ) terms
--
-- so we do explicit pattern matching instead.
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ₁≼Γ₂ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound : ∀ {Γ₁ Γ₂ Σ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(terms : Terms Γ₁ Σ) (ρ : ⟦ Γ₂ ⟧) →
⟦ weaken-terms Γ₁≼Γ₂ terms ⟧Terms ρ ≡ ⟦ terms ⟧Terms (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound ∅ ρ = refl
weaken-terms-sound (t • terms) ρ =
cong₂ _•_ (weaken-sound t ρ) (weaken-terms-sound terms ρ)
weaken-sound {Γ₁≼Γ₂ = Γ₁≼Γ₂} (var x) ρ = weaken-var-sound Γ₁≼Γ₂ x ρ
weaken-sound (app s t) ρ = weaken-sound s ρ ⟨$⟩ weaken-sound t ρ
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound {Γ₁} {Γ₂} {Γ₁≼Γ₂ = Γ₁≼Γ₂} (const {Σ} {τ} c args) ρ =
cong ⟦ c ⟧Const (weaken-terms-sound args ρ)
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Standard evaluation (Def. 3.3 and Fig. 4i)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation
open import Relation.Binary.PropositionalEquality
open import Theorem.CongApp
open import Postulate.Extensionality
-- Extension Point: Evaluation of fully applied constants.
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
-- We provide: Evaluation of arbitrary terms.
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
-- this is what we'd like to write.
-- unfortunately termination checker complains.
--
-- ⟦ terms ⟧Terms ρ = map (λ t → ⟦ t ⟧Term ρ) terms
--
-- so we do explicit pattern matching instead.
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
weaken-sound : ∀ {Γ₁ Γ₂ τ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(t : Term Γ₁ τ) (ρ : ⟦ Γ₂ ⟧) → ⟦ weaken Γ₁≼Γ₂ t ⟧ ρ ≡ ⟦ t ⟧ (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound : ∀ {Γ₁ Γ₂ Σ} {Γ₁≼Γ₂ : Γ₁ ≼ Γ₂}
(terms : Terms Γ₁ Σ) (ρ : ⟦ Γ₂ ⟧) →
⟦ weaken-terms Γ₁≼Γ₂ terms ⟧Terms ρ ≡ ⟦ terms ⟧Terms (⟦ Γ₁≼Γ₂ ⟧ ρ)
weaken-terms-sound ∅ ρ = refl
weaken-terms-sound (t • terms) ρ =
cong₂ _•_ (weaken-sound t ρ) (weaken-terms-sound terms ρ)
weaken-sound {Γ₁≼Γ₂ = Γ₁≼Γ₂} (var x) ρ = weaken-var-sound Γ₁≼Γ₂ x ρ
weaken-sound (app s t) ρ = weaken-sound s ρ ⟨$⟩ weaken-sound t ρ
weaken-sound (abs t) ρ = ext (λ v → weaken-sound t (v • ρ))
weaken-sound {Γ₁} {Γ₂} {Γ₁≼Γ₂ = Γ₁≼Γ₂} (const {Σ} {τ} c args) ρ =
cong ⟦ c ⟧Const (weaken-terms-sound args ρ)
|
Correct code in comment
|
Correct code in comment
map-IVT does not exist currently (not sure it ever did, I did not
investigate); what we want and does not work is simply map. Hence, fix
the comment to clarify it.
|
Agda
|
mit
|
inc-lc/ilc-agda
|
caab0f0a9ebc53432d697d8dfef61fb6cae09b67
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR ≙-setoid public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil x)
≡⟨ update-nil x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- We know that Derivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> Derivative f (apply df).
-- Now, we try to prove that if Derivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
Derivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil x))
≡⟨ ext (λ x → cong f (update-nil x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
Derivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
-- To avoid unification problems, use a one-field record.
record _≙_ dx dy : Set a where
-- doe = Delta-Observational Equivalence.
constructor doe
field
proof : x ⊞ dx ≡ x ⊞ dy
open _≙_ public
-- Same priority as ≡
infix 4 _≙_
open import Relation.Binary
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = doe refl
≙-sym : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-sym ≙ = doe $ sym $ proof ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = doe $ trans (proof ≙₁) (proof ≙₂)
-- That's standard congruence applied to ≙
≙-cong : ∀ {b} {B : Set b}
(f : A → B) {dx dy} → dx ≙ dy → f (x ⊞ dx) ≡ f (x ⊞ dy)
≙-cong f da≙db = cong f $ proof da≙db
≙-isEquivalence : IsEquivalence (_≙_)
≙-isEquivalence = record
{ refl = ≙-refl
; sym = ≙-sym
; trans = ≙-trans
}
≙-setoid : Setoid ℓ a
≙-setoid = record
{ Carrier = Δ x
; _≈_ = _≙_
; isEquivalence = ≙-isEquivalence
}
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
------------------------------------------------------------------------
-- Convenient syntax for equational reasoning
import Relation.Binary.EqReasoning as EqR
module ≙-Reasoning where
open EqR (≙-setoid {x = x}) public
renaming (_≈⟨_⟩_ to _≙⟨_⟩_)
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ dx → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil x)
≡⟨ update-nil x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ dx → x ⊞ dx ≡ x → dx ≙ nil x
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil x) ⟩
x ⊞ nil x
∎
where
open ≡-Reasoning
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by fun-change-respects.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : x ⊞ (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization)
open FC.FunctionChange
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
df₁ ≙ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
fun-change-respects {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- We know that Derivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> Derivative f (apply df).
-- Now, we try to prove that if Derivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
Derivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil x))
≡⟨ ext (λ x → cong f (update-nil x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
Derivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → Derivative f (apply df) → Derivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
|
Prepare for splitting
|
Equivalence: Prepare for splitting
The file has become too big.
Splitting the anonymous modules requires more work out of type inference
and introduces one type inference failure, which is fixed in this commmit.
Old-commit-hash: 32f42f80b60cd3dd45e879d7a367b4fe933bb9d8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
26feb31d8f07b57839d5ad020a366816d0ae21fb
|
Parametric/Denotation/Evaluation.agda
|
Parametric/Denotation/Evaluation.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Syntax.Context Type
open import Base.Denotation.Environment Type ⟦_⟧Type
open import Base.Denotation.Notation
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure(⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
|
import Parametric.Syntax.Type as Type
import Parametric.Syntax.Term as Term
import Parametric.Denotation.Value as Value
module Parametric.Denotation.Evaluation
{Base : Type.Structure}
(Const : Term.Structure Base)
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Term.Structure Base Const
open Value.Structure Base ⟦_⟧Base
open import Base.Syntax.Context Type
open import Base.Denotation.Environment Type ⟦_⟧Type
open import Base.Denotation.Notation
Structure : Set
Structure = ∀ {Σ τ} → Const Σ τ → ⟦ Σ ⟧ → ⟦ τ ⟧
module Structure (⟦_⟧Const : Structure) where
⟦_⟧Term : ∀ {Γ τ} → Term Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦_⟧Terms : ∀ {Γ Σ} → Terms Γ Σ → ⟦ Γ ⟧ → ⟦ Σ ⟧
⟦ const c args ⟧Term ρ = ⟦ c ⟧Const (⟦ args ⟧Terms ρ)
⟦ var x ⟧Term ρ = ⟦ x ⟧ ρ
⟦ app s t ⟧Term ρ = (⟦ s ⟧Term ρ) (⟦ t ⟧Term ρ)
⟦ abs t ⟧Term ρ = λ v → ⟦ t ⟧Term (v • ρ)
⟦ ∅ ⟧Terms ρ = ∅
⟦ s • terms ⟧Terms ρ = ⟦ s ⟧Term ρ • ⟦ terms ⟧Terms ρ
meaningOfTerm : ∀ {Γ τ} → Meaning (Term Γ τ)
meaningOfTerm = meaning ⟦_⟧Term
|
insert missing space (cosmetic)
|
insert missing space (cosmetic)
Old-commit-hash: 4d559497f68fabe2674c6d523508f715c78256c8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
321249014d14436b6f3f84e01fdeb73f0d5faf7b
|
crypto-agda.agda
|
crypto-agda.agda
|
module crypto-agda where
import Cipher.ElGamal.Generic
import Composition.Forkable
import Composition.Horizontal
import Composition.Vertical
import Crypto.Schemes
--import FiniteField.FinImplem
import FunUniverse.Agda
import FunUniverse.BinTree
import FunUniverse.Bits
import FunUniverse.Category
import FunUniverse.Category.Op
import FunUniverse.Circuit
import FunUniverse.Const
import FunUniverse.Core
import FunUniverse.Cost
import FunUniverse.Data
import FunUniverse.Defaults.FirstPart
--import FunUniverse.ExnArrow
import FunUniverse.Fin
import FunUniverse.Fin.Op
import FunUniverse.Fin.Op.Abstract
--import FunUniverse.FlatFunsProd
--import FunUniverse.Interface.Bits
import FunUniverse.Interface.Two
import FunUniverse.Interface.Vec
import FunUniverse.Inverse
--import FunUniverse.Loop
import FunUniverse.Nand
import FunUniverse.Nand.Function
import FunUniverse.Nand.Properties
import FunUniverse.README
import FunUniverse.Rewiring.Linear
--import FunUniverse.State
import FunUniverse.Syntax
import FunUniverse.Types
import Game.DDH
import Game.EntropySmoothing
import Game.EntropySmoothing.WithKey
import Game.IND-CPA
import Game.Transformation.InternalExternal
import Language.Simple.Abstract
import Language.Simple.Interface
import Language.Simple.Two.Mux
import Language.Simple.Two.Mux.Normalise
import Language.Simple.Two.Nand
import README
import Solver.AddMax
import Solver.Linear
--import Solver.Linear.Examples
import Solver.Linear.Parser
import Solver.Linear.Syntax
import adder
import alea.cpo
import bijection-syntax.Bijection-Fin
import bijection-syntax.Bijection
import bijection-syntax.README
--import circuits.bytecode
import circuits.circuit
--import elgamal
--import sha1
|
module crypto-agda where
import Attack.Compression
import Attack.Reencryption
import Attack.Reencryption.OneBitMessage
import Cipher.ElGamal.Generic
import Cipher.ElGamal.Homomorphic
import Composition.Forkable
import Composition.Horizontal
import Composition.Vertical
import Crypto.Schemes
import FiniteField.FinImplem
import FunUniverse.Agda
import FunUniverse.BinTree
import FunUniverse.Bits
import FunUniverse.Category
import FunUniverse.Category.Op
import FunUniverse.Circuit
import FunUniverse.Const
import FunUniverse.Core
import FunUniverse.Cost
import FunUniverse.Data
import FunUniverse.Defaults.FirstPart
import FunUniverse.ExnArrow
import FunUniverse.Fin
import FunUniverse.Fin.Op
import FunUniverse.Fin.Op.Abstract
import FunUniverse.FlatFunsProd
import FunUniverse.Interface.Bits
import FunUniverse.Interface.Two
import FunUniverse.Interface.Vec
import FunUniverse.Inverse
import FunUniverse.Loop
import FunUniverse.Nand
import FunUniverse.Nand.Function
import FunUniverse.Nand.Properties
import FunUniverse.README
import FunUniverse.Rewiring.Linear
import FunUniverse.State
import FunUniverse.Syntax
import FunUniverse.Types
import Game.DDH
import Game.EntropySmoothing
import Game.EntropySmoothing.WithKey
import Game.IND-CCA
import Game.IND-CCA2-dagger
import Game.IND-CCA2
import Game.IND-CPA
import Game.Transformation.CCA-CPA
import Game.Transformation.CCA2-CCA
import Game.Transformation.CCA2-CCA2d
import Game.Transformation.CCA2d-CCA2
import Game.Transformation.InternalExternal
import Language.Simple.Abstract
import Language.Simple.Interface
import Language.Simple.Two.Mux
import Language.Simple.Two.Mux.Normalise
import Language.Simple.Two.Nand
import README
import Solver.AddMax
import Solver.Linear
import Solver.Linear.Examples
import Solver.Linear.Parser
import Solver.Linear.Syntax
import adder
import alea.cpo
import bijection-syntax.Bijection-Fin
import bijection-syntax.Bijection
import bijection-syntax.README
import circuits.bytecode
import circuits.circuit
import elgamal
import sha1
|
update crypto-agda.agda
|
update crypto-agda.agda
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
bc0f697e345522fad65844eed07cba984158e3b7
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
open import Base.Change.Algebra as CA
using (ChangeAlgebraFamily)
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Level
-- Extension Point: Change algebras for base types
Structure : Set₁
Structure = ChangeAlgebraFamily zero ⟦_⟧Base
module Structure (change-algebra-base : Structure) where
-- change algebras
open CA public renaming
( [] to ∅
; _∷_ to _•_
)
-- We provide: change algebra for every type
change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}}
change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type
change-algebra-family = family change-algebra
-- function changes
open FunctionChanges public using (cons)
module _ {τ₁ τ₂ : Type} where
open FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public
renaming
( correct to is-valid
; apply to call-change
)
-- We also provide: change environments (aka. environment changes).
open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebra to environment-changes
)
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍ Γ ₎
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Dependently typed changes (Def 3.4 and 3.5, Fig. 4b and 4e)
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
open import Base.Change.Algebra as CA
using (ChangeAlgebraFamily)
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Level
-- Extension Point: Change algebras for base types
Structure : Set₁
Structure = ChangeAlgebraFamily zero ⟦_⟧Base
module Structure (change-algebra-base : Structure) where
-- change algebras
open CA public renaming
( [] to ∅
; _∷_ to _•_
)
-- We provide: change algebra for every type
change-algebra : ∀ τ → ChangeAlgebra zero ⟦ τ ⟧Type
change-algebra (base ι) = change-algebra₍ ι ₎
change-algebra (τ₁ ⇒ τ₂) = CA.FunctionChanges.changeAlgebra _ _ {{change-algebra τ₁}} {{change-algebra τ₂}}
change-algebra-family : ChangeAlgebraFamily zero ⟦_⟧Type
change-algebra-family = family change-algebra
-- function changes
open FunctionChanges public using (cons)
module _ {τ₁ τ₂ : Type} where
open FunctionChanges.FunctionChange _ _ {{change-algebra τ₁}} {{change-algebra τ₂}} public
renaming
( correct to is-valid
; apply to call-change
)
-- We also provide: change environments (aka. environment changes).
{-
open ListChanges ⟦_⟧Type {{change-algebra-family}} public using () renaming
( changeAlgebra to environment-changes
)
after-env : ∀ {Γ : Context} → {ρ : ⟦ Γ ⟧} (dρ : Δ₍ Γ ₎ ρ) → ⟦ Γ ⟧
after-env {Γ} = after₍ Γ ₎
-}
|
Comment out bug-triggering code
|
Parametric.Change.Validity: Comment out bug-triggering code
This code is exported and used elsewhere :-(
|
Agda
|
mit
|
inc-lc/ilc-agda
|
ac25a046e34905a5fd630275f7f19cf585a2f9ab
|
ZK/JSChecker.agda
|
ZK/JSChecker.agda
|
{-# OPTIONS --without-K #-}
module ZK.JSChecker where
open import Function using (id; _∘′_; case_of_)
open import Data.Bool.Base using (Bool; true; false; _∧_)
open import Data.List.Base using (List; []; _∷_; and; foldr)
open import Data.String.Base using (String)
open import FFI.JS
open import FFI.JS.Check
-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)
-- open import Control.Process.Type
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
import FFI.JS.BigI as BigI
open BigI using (BigI; bigI)
import Crypto.JS.BigI.ZqZp as ZqZp
-- TODO dynamise me
primality-test-probability-bound : Number
primality-test-probability-bound = readNumber "10"
-- TODO: check if this is large enough
min-bits-q : Number
min-bits-q = 256N
min-bits-p : Number
min-bits-p = 2048N
-- TODO check with undefined
bigdec : JSValue → BigI
bigdec v = bigI (castString v) "10"
-- TODO bug (undefined)!
record ZK-chaum-pedersen-pok-elgamal-rnd {--(ℤq ℤp★ : Set)--} : Set where
field
m c s : BigI {--ℤq--}
g p q y α β A B : BigI --ℤp★
zk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} → JS!
zk-check-chaum-pedersen-pok-elgamal-rnd! pf
= trace "g=" g λ _ →
trace "p=" I.p λ _ →
trace "q=" I.q λ _ →
trace "y=" y λ _ →
trace "α=" α λ _ →
trace "β=" β λ _ →
trace "m=" m λ _ →
trace "M=" M λ _ →
trace "A=" A λ _ →
trace "B=" B λ _ →
trace "c=" c λ _ →
trace "s=" s λ _ →
checks!
>> check! "g^s==A·α^c" ((g ^ s) == (A · (α ^ c))) (λ _ → "")
>> check! "y^s==B·(β/M)^c" ((y ^ s) == (B · ((β ·/ M) ^ c))) (λ _ → "")
module ZK-check-chaum-pedersen-pok-elgamal-rnd where
module I = ZK-chaum-pedersen-pok-elgamal-rnd pf
params = record
{ primality-test-probability-bound = primality-test-probability-bound
; min-bits-q = min-bits-q
; min-bits-p = min-bits-p
; qI = I.q
; pI = I.p
; gI = I.g
}
open module [ℤq]ℤp★ = ZqZp params
A = BigI▹ℤp★ I.A
B = BigI▹ℤp★ I.B
α = BigI▹ℤp★ I.α
β = BigI▹ℤp★ I.β
y = BigI▹ℤp★ I.y
s = BigI▹ℤq I.s
c = BigI▹ℤq I.c
m = BigI▹ℤq I.m
M = g ^ m
zk-check! : JSValue → JS!
zk-check! arg =
check! "type of statement" (typ === fromString cpt)
(λ _ → "Expected type of statement: " ++ cpt ++ " not " ++ toString typ)
>> zk-check-chaum-pedersen-pok-elgamal-rnd! pok
module Zk-check where
cpt = "chaum-pedersen-pok-elgamal-rnd"
stm = arg ·« "statement" »
typ = stm ·« "type" »
dat = stm ·« "data" »
g = bigdec (dat ·« "g" »)
p = bigdec (dat ·« "p" »)
q = bigdec (dat ·« "q" »)
y = bigdec (dat ·« "y" »)
m = bigdec (dat ·« "plain" »)
enc = dat ·« "enc" »
α = bigdec (enc ·« "alpha" »)
β = bigdec (enc ·« "beta" »)
prf = arg ·« "proof" »
com = prf ·« "commitment" »
A = bigdec (com ·« "A" »)
B = bigdec (com ·« "B" »)
c = bigdec (prf ·« "challenge" »)
s = bigdec (prf ·« "response" »)
pok = record { g = g; p = p; q = q; y = y; α = α; β = β; A = A; B = B; c = c; s = s; m = m }
{-
srv : URI → JSProc
srv d =
recv d λ q →
send d (fromBool (zk-check q))
end
-}
-- Working around Agda.Primitive.lsuc being undefined
-- case_of_ : {A : Set} {B : Set} → A → (A → B) → B
-- case x of f = f x
main : JS!
main =
Process.argv !₁ λ args →
case JSArray▹ListString args of λ {
(_node ∷ _run ∷ _test ∷ args') →
case args' of λ {
[] →
Console.log "usage: No arguments"
{- server "127.0.0.1" "1337" srv !₁ λ uri →
Console.log (showURI uri)
-}
; (arg ∷ args'') →
case args'' of λ {
[] →
Console.log ("Reading input file: " ++ arg) >>
FS.readFile arg nullJS !₂ λ err dat →
check! "reading input file" (is-null err)
(λ _ → "readFile error: " ++ toString err) >>
zk-check! (JSON-parse (toString dat))
; _ →
Console.log "usage: Too many arguments"
}
}
; _ →
Console.log "usage"
}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
module ZK.JSChecker where
open import Function using (id; _∘′_; case_of_)
open import Data.Bool.Base using (Bool; true; false; _∧_)
open import Data.List.Base using (List; []; _∷_; and; foldr)
open import Data.String.Base using (String)
open import FFI.JS
open import FFI.JS.Check
-- open import FFI.JS.Proc using (URI; JSProc; showURI; server)
-- open import Control.Process.Type
import FFI.JS.Console as Console
import FFI.JS.Process as Process
import FFI.JS.FS as FS
import FFI.JS.BigI as BigI
open BigI using (BigI; bigI)
import Crypto.JS.BigI.ZqZp as ZqZp
-- TODO dynamise me
primality-test-probability-bound : Number
primality-test-probability-bound = readNumber "10"
-- TODO: check if this is large enough
min-bits-q : Number
min-bits-q = 256N
min-bits-p : Number
min-bits-p = 2048N
bigdec : JSValue → BigI
bigdec v = bigI (castString v) "10"
-- TODO bug (undefined)!
record ZK-chaum-pedersen-pok-elgamal-rnd {--(ℤq ℤp★ : Set)--} : Set where
field
m c s : BigI {--ℤq--}
g p q y α β A B : BigI --ℤp★
zk-check-chaum-pedersen-pok-elgamal-rnd! : ZK-chaum-pedersen-pok-elgamal-rnd {-BigI BigI-} → JS!
zk-check-chaum-pedersen-pok-elgamal-rnd! pf
= trace "g=" g λ _ →
trace "p=" I.p λ _ →
trace "q=" I.q λ _ →
trace "y=" y λ _ →
trace "α=" α λ _ →
trace "β=" β λ _ →
trace "m=" m λ _ →
trace "M=" M λ _ →
trace "A=" A λ _ →
trace "B=" B λ _ →
trace "c=" c λ _ →
trace "s=" s λ _ →
checks!
>> check! "g^s==A·α^c" ((g ^ s) == (A · (α ^ c))) (λ _ → "")
>> check! "y^s==B·(β/M)^c" ((y ^ s) == (B · ((β ·/ M) ^ c))) (λ _ → "")
module ZK-check-chaum-pedersen-pok-elgamal-rnd where
module I = ZK-chaum-pedersen-pok-elgamal-rnd pf
params = record
{ primality-test-probability-bound = primality-test-probability-bound
; min-bits-q = min-bits-q
; min-bits-p = min-bits-p
; qI = I.q
; pI = I.p
; gI = I.g
}
open module [ℤq]ℤp★ = ZqZp params
A = BigI▹ℤp★ I.A
B = BigI▹ℤp★ I.B
α = BigI▹ℤp★ I.α
β = BigI▹ℤp★ I.β
y = BigI▹ℤp★ I.y
s = BigI▹ℤq I.s
c = BigI▹ℤq I.c
m = BigI▹ℤq I.m
M = g ^ m
zk-check! : JSValue → JS!
zk-check! arg =
check! "type of statement" (typ === fromString cpt)
(λ _ → "Expected type of statement: " ++ cpt ++ " not " ++ toString typ)
>> zk-check-chaum-pedersen-pok-elgamal-rnd! pok
module Zk-check where
cpt = "chaum-pedersen-pok-elgamal-rnd"
stm = arg ·« "statement" »
typ = stm ·« "type" »
dat = stm ·« "data" »
g = bigdec (dat ·« "g" »)
p = bigdec (dat ·« "p" »)
q = bigdec (dat ·« "q" »)
y = bigdec (dat ·« "y" »)
m = bigdec (dat ·« "plain" »)
enc = dat ·« "enc" »
α = bigdec (enc ·« "alpha" »)
β = bigdec (enc ·« "beta" »)
prf = arg ·« "proof" »
com = prf ·« "commitment" »
A = bigdec (com ·« "A" »)
B = bigdec (com ·« "B" »)
c = bigdec (prf ·« "challenge" »)
s = bigdec (prf ·« "response" »)
pok = record { g = g; p = p; q = q; y = y; α = α; β = β; A = A; B = B; c = c; s = s; m = m }
{-
srv : URI → JSProc
srv d =
recv d λ q →
send d (fromBool (zk-check q))
end
-}
-- Working around Agda.Primitive.lsuc being undefined
-- case_of_ : {A : Set} {B : Set} → A → (A → B) → B
-- case x of f = f x
main : JS!
main =
Process.argv !₁ λ args →
case JSArray▹ListString args of λ {
(_node ∷ _run ∷ _test ∷ args') →
case args' of λ {
[] →
Console.log "usage: No arguments"
{- server "127.0.0.1" "1337" srv !₁ λ uri →
Console.log (showURI uri)
-}
; (arg ∷ args'') →
case args'' of λ {
[] →
Console.log ("Reading input file: " ++ arg) >>
FS.readFile arg nullJS !₂ λ err dat →
check! "reading input file" (is-null err)
(λ _ → "readFile error: " ++ toString err) >>
zk-check! (JSON-parse (toString dat))
; _ →
Console.log "usage: Too many arguments"
}
}
; _ →
Console.log "usage"
}
-- -}
-- -}
-- -}
-- -}
|
Remove no longer relevant TODO
|
Remove no longer relevant TODO
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
a6dfe1f553ba79964ad12503ce1a9095fe79ffe2
|
arrow.agda
|
arrow.agda
|
data Bool : Set where
true : Bool
false : Bool
_or_ : Bool → Bool → Bool
true or _ = true
_ or true = true
false or false = false
_and_ : Bool → Bool → Bool
false and _ = false
_ and false = false
true and true = true
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
any : {A : Set} → (A → Bool) → List A → Bool
any _ ∘ = false
any f (x ∷ xs) = (f x) or (any f xs)
apply : {A B : Set} → (A → B) → List A → List B
apply _ ∘ = ∘
apply f (x ∷ xs) = (f x) ∷ (apply f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q ≡ s
(p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ ∘ = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs ∘)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ ∘ ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
|
data Bool : Set where
true : Bool
false : Bool
_or_ : Bool → Bool → Bool
true or _ = true
_ or true = true
false or false = false
_and_ : Bool → Bool → Bool
false and _ = false
_ and false = false
true and true = true
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
infixr 5 _∷_
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
any : {A : Set} → (A → Bool) → List A → Bool
any _ ∘ = false
any f (x ∷ xs) = (f x) or (any f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q ≡ s
(p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ ∘ = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs ∘)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ ∘ ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
proofs : List Arrow
proofs =
(3 ⇒ (⇒ 4)) ∷
-- (5 ⇒ (⇒ 4)) ∷
(6 ⇒ (⇒ 4)) ∷
(3 ⇒ (⇒ 3)) ∷
(3 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(5 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 7)) ∷
(6 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 7)) ∷
(5 ⇒ (⇒ 10)) ∷
(10 ⇒ (⇒ 4)) ∷
(5 ⇒ (⇒ 11)) ∷
(6 ⇒ (⇒ 11)) ∷
(11 ⇒ (⇒ 4)) ∷
(10 ⇒ (⇒ 7)) ∷
(8 ⇒ (⇒ 4)) ∷
(9 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 8)) ∷
(3 ⇒ (⇒ 8)) ∷
(5 ⇒ (3 ⇒ (⇒ 9))) ∷
(6 ⇒ (7 ⇒ (⇒ 10))) ∷
(6 ⇒ (3 ⇒ (⇒ 3))) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (8 ⇒ (⇒ 10))) ∷
(3 ⇒ (10 ⇒ (⇒ 9))) ∷
(5 ⇒ (⇒ 1)) ∷
(3 ⇒ (1 ⇒ (⇒ 9))) ∷
(1 ⇒ (⇒ 2)) ∷
(10 ⇒ (⇒ 2)) ∷ ∘
cms : List Separation
cms =
(model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷
(model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷
(model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷
(model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷
(model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷
(model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷
(model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷
(model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘
testp : Arrow
testp = (5 ⇒ (⇒ 10))
testd : Arrow
testd = (5 ⇒ (⇒ 7))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
|
Add test
|
Add test
|
Agda
|
mit
|
louisswarren/hieretikz
|
51432b971511d6e3b28904b1d33fc55d32888906
|
lib/Function/Extensionality.agda
|
lib/Function/Extensionality.agda
|
{-# OPTIONS --without-K #-}
module Function.Extensionality where
open import Function.NP
open import Relation.Binary.PropositionalEquality.NP -- renaming (subst to tr)
happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}
→ f ≡ g → (x : A) → f x ≡ g x
happly p x = ap (λ f → f x) p
postulate
FunExt : Set
λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f₀ f₁ : (x : A) → B x}{{fe : FunExt}}
(f= : ∀ x → f₀ x ≡ f₁ x) → f₀ ≡ f₁
happly-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → happly (λ= fg) ≡ fg
λ=-happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → λ= (happly α) ≡ α
-- This should be derivable if I had a proper proof of λ=
tr-λ= : ∀ {a b p}{A : Set a}{B : A → Set b}{x}(P : B x → Set p)
{f g : (x : A) → B x}{{fe : FunExt}}(fg : (x : A) → f x ≡ g x)
→ tr (λ f → P (f x)) (λ= fg) ≡ tr P (fg x)
!-α-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → ! α ≡ λ= (!_ ∘ happly α)
!-α-λ= refl = ! λ=-happly refl
!-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → ! (λ= fg) ≡ λ= (!_ ∘ fg)
!-λ= fg = !-α-λ= (λ= fg) ∙ ap λ= (λ= (λ x → ap !_ (happly (happly-λ= fg) x)))
|
{-# OPTIONS --without-K #-}
module Function.Extensionality where
open import Function.NP
open import Relation.Binary.PropositionalEquality.NP -- renaming (subst to tr)
happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}
→ f ≡ g → (x : A) → f x ≡ g x
happly p x = ap (λ f → f x) p
postulate
FunExt : Set
λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f₀ f₁ : (x : A) → B x}{{fe : FunExt}}
(f= : ∀ x → f₀ x ≡ f₁ x) → f₀ ≡ f₁
happly-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → happly (λ= fg) ≡ fg
λ=-happly : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → λ= (happly α) ≡ α
-- This should be derivable if I had a proper proof of λ=
tr-λ= : ∀ {a b p}{A : Set a}{B : A → Set b}{x}(P : B x → Set p)
{f g : (x : A) → B x}{{fe : FunExt}}(fg : (x : A) → f x ≡ g x)
→ tr (λ f → P (f x)) (λ= fg) ≡ tr P (fg x)
!-α-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(α : f ≡ g) → ! α ≡ λ= (!_ ∘ happly α)
!-α-λ= refl = ! λ=-happly refl
!-λ= : ∀ {a b}{A : Set a}{B : A → Set b}{f g : (x : A) → B x}{{fe : FunExt}}
(fg : ∀ x → f x ≡ g x) → ! (λ= fg) ≡ λ= (!_ ∘ fg)
!-λ= fg = !-α-λ= (λ= fg) ∙ ap λ= (λ= (λ x → ap !_ (happly (happly-λ= fg) x)))
module _ {a b}{A : Set a}{B : A → Set b}{f₀ f₁ : (x : A) → B x}{{fe : FunExt}} where
λ=ⁱ : (f= : ∀ {x} → f₀ x ≡ f₁ x) → f₀ ≡ f₁
λ=ⁱ f= = λ= λ x → f= {x}
|
add a version with implicit arguments
|
FunExt: add a version with implicit arguments
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
702fcd37b6983eaf128ea29fca185751c2444c69
|
lib/Relation/Binary/Permutation.agda
|
lib/Relation/Binary/Permutation.agda
|
--TODO {-# OPTIONS --without-K #-}
module Relation.Binary.Permutation where
open import Level
open import Data.Product.NP
open import Data.Zero
open import Data.One
open import Data.Sum
open import Data.List
open import Relation.Nullary
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_;_≢_)
private
_⇔_ : ∀ {a b ℓ₁ ℓ₂} {A : Set a} {B : Set b} (R₁ : REL A B ℓ₁) (R₂ : REL A B ℓ₂) → Set _
R₁ ⇔ R₂ = R₁ ⇒ R₂ × R₂ ⇒ R₁
infix 2 _⇔_
⟨_,_⟩∈_ : ∀ {ℓ a b} {A : Set a} {B : Set b} (x : A) (y : B) → REL A B ℓ → Set ℓ
⟨_,_⟩∈_ x y R = R x y
data _[_↔_] {a} {A : Set a} (R : Rel A a) (x y : A) : Rel A a where
here₁ : ∀ {j}
(yRj : ⟨ y , j ⟩∈ R )
→ ----------------------
⟨ x , j ⟩∈ R [ x ↔ y ]
here₂ : ∀ {j}
(xRj : ⟨ x , j ⟩∈ R )
→ ----------------------
⟨ y , j ⟩∈ R [ x ↔ y ]
there : ∀ {i j}
(x≢i : x ≢ i )
(y≢i : y ≢ i )
(iRj : ⟨ i , j ⟩∈ R )
→ -----------------------
⟨ i , j ⟩∈ R [ x ↔ y ]
module PermComm {a} {A : Set a} {R : Rel A a} where
⟹ : ∀ {x y} → R [ x ↔ y ] ⇒ R [ y ↔ x ]
⟹ (here₁ yRj) = here₂ yRj
⟹ (here₂ xRj) = here₁ xRj
⟹ (there x≢i x≢j iRj) = there x≢j x≢i iRj
lem : ∀ {x y} → R [ x ↔ y ] ⇔ R [ y ↔ x ]
lem = (λ {_} → ⟹) , (λ {_} → ⟹)
module PermIdem {a} {A : Set a} (_≟_ : Decidable {A = A} _≡_) {x y : A} {R : Rel A a} where
⇐ : ∀ {x y} → R [ x ↔ y ] [ x ↔ y ] ⇒ R
⇐ (here₁ (here₁ yRj)) = yRj
⇐ (here₁ (here₂ xRj)) = xRj
⇐ (here₁ (there _ y≢y _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (here₂ (here₁ yRj)) = yRj
⇐ (here₂ (here₂ xRj)) = xRj
⇐ (here₂ (there x≢x _ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there x≢x _ (here₁ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there _ y≢y (here₂ _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (there _ _ (there _ _ iRj)) = iRj
⟹ : R ⇒ R [ x ↔ y ] [ x ↔ y ]
⟹ {i} {j} R
with x ≟ i | y ≟ i
... | yes x≡i | _ rewrite x≡i = here₁ (here₂ R)
... | _ | yes y≡i rewrite y≡i = here₂ (here₁ R)
... | no x≢i | no y≢i = there x≢i y≢i (there x≢i y≢i R)
lem : R ⇔ R [ x ↔ y ] [ x ↔ y ]
lem = (λ {_} → ⟹) , λ {_} → ⇐
Permutation : ∀ {a} → Set a → Set a
Permutation A = List (A × A)
permRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a → Rel A a
permRel π R = foldr (λ p r → r [ fst p ↔ snd p ]) R π
toRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a
toRel π = permRel π (λ _ _ → Lift 𝟙)
{-
_⟨$⟩₁_ : ∀ {a} {A : Set a} → Permutation A → A → Maybe A
[] ⟨$⟩₁ y = nothing
(x ∷ xs) ⟨$⟩₁ y = if ⌊ x ≟A y ⌋ then ? else
-}
|
--TODO {-# OPTIONS --without-K #-}
module Relation.Binary.Permutation where
open import Level
open import Data.Product.NP
open import Data.Zero
open import Data.One
open import Data.Sum
open import Data.List
open import Relation.Nullary
open import Relation.Binary
import Relation.Binary.PropositionalEquality as ≡
open ≡ using (_≡_;_≢_)
private
_⇔_ : ∀ {a b ℓ₁ ℓ₂} {A : Set a} {B : Set b} (R₁ : REL A B ℓ₁) (R₂ : REL A B ℓ₂) → Set _
R₁ ⇔ R₂ = R₁ ⇒ R₂ × R₂ ⇒ R₁
infix 2 _⇔_
⟨_,_⟩∈_ : ∀ {ℓ a b} {A : Set a} {B : Set b} (x : A) (y : B) → REL A B ℓ → Set ℓ
⟨_,_⟩∈_ x y R = R x y
data _[_↔_] {a} {A : Set a} (R : Rel A a) (x y : A) : Rel A a where
here₁ : ∀ {j}
(yRj : ⟨ y , j ⟩∈ R )
→ ----------------------
⟨ x , j ⟩∈ R [ x ↔ y ]
here₂ : ∀ {j}
(xRj : ⟨ x , j ⟩∈ R )
→ ----------------------
⟨ y , j ⟩∈ R [ x ↔ y ]
there : ∀ {i j}
(x≢i : x ≢ i )
(y≢i : y ≢ i )
(iRj : ⟨ i , j ⟩∈ R )
→ -----------------------
⟨ i , j ⟩∈ R [ x ↔ y ]
module PermComm {a} {A : Set a} {R : Rel A a} where
⟹ : ∀ {x y} → R [ x ↔ y ] ⇒ R [ y ↔ x ]
⟹ (here₁ yRj) = here₂ yRj
⟹ (here₂ xRj) = here₁ xRj
⟹ (there x≢i x≢j iRj) = there x≢j x≢i iRj
lem : ∀ {x y} → R [ x ↔ y ] ⇔ R [ y ↔ x ]
lem = (λ {_} → ⟹) , (λ {_} → ⟹)
module PermIdem {a} {A : Set a} (_≟_ : Decidable {A = A} _≡_) {R : Rel A a} where
⇐ : ∀ {x y} → R [ x ↔ y ] [ x ↔ y ] ⇒ R
⇐ (here₁ (here₁ yRj)) = yRj
⇐ (here₁ (here₂ xRj)) = xRj
⇐ (here₁ (there _ y≢y _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (here₂ (here₁ yRj)) = yRj
⇐ (here₂ (here₂ xRj)) = xRj
⇐ (here₂ (there x≢x _ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there x≢x _ (here₁ _)) = 𝟘-elim (x≢x ≡.refl)
⇐ (there _ y≢y (here₂ _)) = 𝟘-elim (y≢y ≡.refl)
⇐ (there _ _ (there _ _ iRj)) = iRj
⟹ : ∀ {x y} → R ⇒ R [ x ↔ y ] [ x ↔ y ]
⟹ {x} {y} {i} {j} R
with x ≟ i | y ≟ i
... | yes x≡i | _ rewrite x≡i = here₁ (here₂ R)
... | _ | yes y≡i rewrite y≡i = here₂ (here₁ R)
... | no x≢i | no y≢i = there x≢i y≢i (there x≢i y≢i R)
lem : ∀ {x y} → R ⇔ R [ x ↔ y ] [ x ↔ y ]
lem = (λ {_} → ⟹) , λ {_} → ⇐
Permutation : ∀ {a} → Set a → Set a
Permutation A = List (A × A)
permRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a → Rel A a
permRel π R = foldr (λ p r → r [ fst p ↔ snd p ]) R π
toRel : ∀ {a} {A : Set a} → (π : Permutation A) → Rel A a
toRel π = permRel π (λ _ _ → Lift 𝟙)
{-
_⟨$⟩₁_ : ∀ {a} {A : Set a} → Permutation A → A → Maybe A
[] ⟨$⟩₁ y = nothing
(x ∷ xs) ⟨$⟩₁ y = if ⌊ x ≟A y ⌋ then ? else
-}
|
Fix in Relation.Binary.Permutation
|
Fix in Relation.Binary.Permutation
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
3822d110c060a75d914e9d0e4fc105f00e99980a
|
Syntax/Language/Atlas.agda
|
Syntax/Language/Atlas.agda
|
module Syntax.Language.Atlas where
-- Base types of the calculus Atlas
-- to be used with Plotkin-style language description
--
-- Atlas supports maps with neutral elements. The change type to
-- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the
-- ground type ι. Such a change to maps can support insertions
-- and deletions as well: Inserting `k -> v` means mapping `k` to
-- the change from the neutral element to `v`, and deleting
-- `k -> v` means mapping `k` to the change from `v` to the
-- neutral element.
open import Syntax.Language.Calculus
data Atlas-type : Set where
Bool : Atlas-type
Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type
data Atlas-const : Set where
true : Atlas-const
false : Atlas-const
xor : Atlas-const
empty : ∀ {κ ι : Atlas-type} → Atlas-const
update : ∀ {κ ι : Atlas-type} → Atlas-const
zip : ∀ {κ a b c : Atlas-type} → Atlas-const
Atlas-lookup : Atlas-const → Type Atlas-type
Atlas-lookup true = base Bool
Atlas-lookup false = base Bool
Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool
Atlas-lookup (empty {κ} {ι}) = base (Map κ ι)
-- `update key val my-map` would
-- - insert if `key` is not present in `my-map`
-- - delete if `val` is the neutral element
-- - make an update otherwise
Atlas-lookup (update {κ} {ι}) =
base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι)
-- Model of zip = Haskell Data.List.zipWith
-- zipWith :: (a → b → c) → [a] → [b] → [c]
Atlas-lookup (zip {κ} {a} {b} {c}) =
(base κ ⇒ base a ⇒ base b ⇒ base c) ⇒
base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c)
Atlas-Δbase : Atlas-type → Atlas-type
-- change to a boolean is a xor-rand
Atlas-Δbase Bool = Bool
-- change to a map is change to its values
Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val))
Atlas-Δtype : Type Atlas-type → Type Atlas-type
Atlas-Δtype = lift-Δtype₀ Atlas-Δbase
Atlas-context : Set
Atlas-context = Context {Type Atlas-type}
Atlas-term : Atlas-context → Type Atlas-type → Set
Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}
-- Every base type has a known nil-change.
-- The nil-change of ι is also the neutral element of Map κ Δι.
neutral : ∀ {ι : Atlas-type} → Atlas-const
neutral {Bool} = false
neutral {Map κ ι} = empty {κ} {ι}
neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι)
neutral-term {Bool} = const (neutral {Bool})
neutral-term {Map κ ι} = const (neutral {Map κ ι})
nil-const : ∀ {ι : Atlas-type} → Atlas-const
nil-const {ι} = neutral {Atlas-Δbase ι}
nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι))
nil-term {Bool} = const (nil-const {Bool})
nil-term {Map κ ι} = const (nil-const {Map κ ι})
-- Shorthands of constants
--
-- There's probably a uniform way to lift constants
-- into term constructors.
--
-- TODO: write this and call it Syntax.Term.Plotkin.lift-term
zip! : ∀ {κ a b c Γ} →
Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) →
Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) →
Atlas-term Γ (base (Map κ c))
zip! f m₁ m₂ = app (app (app (const zip) f) m₁) m₂
lookup! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base ι)
lookup! = {!!} -- TODO: add constant `lookup`
-- diff-term and apply-term
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι))
Atlas-diff {Bool} = const xor
Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = const xor
Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Atlas-term Γ τ → Atlas-term Γ τ →
Atlas-term Γ (Atlas-Δtype τ)
diff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t
apply : ∀ {τ Γ} →
Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ →
Atlas-term Γ τ
apply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
update! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base (Map κ ι))
update! k v m = app (app (app (const update) k) v) m
insert : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
-- last argument is the change accumulator
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
-- The binary operator with which all base-type values
-- form a group
union : ∀ {ι Γ} →
Atlas-term Γ (base ι) → Atlas-term Γ (base ι) →
Atlas-term Γ (base ι)
union {Bool} s t = app (app (const xor) s) t
union {Map κ ι} s t =
let
union-term = abs (abs (union (var (that this)) (var this)))
in
zip! (abs union-term) s t
-- Shorthand for 4-way zip
zip4! : ∀ {κ a b c d e Γ} →
let
t:_ = λ ι → Atlas-term Γ (base ι)
in
Atlas-term Γ
(base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) →
t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e
zip4! f m₁ m₂ m₃ m₄ =
let
v₁ = var (that this)
v₂ = var this
v₃ = var (that this)
v₄ = var this
k₁₂ = var (that (that this))
k₃₄ = var (that (that this))
f₁₂ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₁₂) v₁) v₂)
(lookup! k₁₂ (weaken₃ m₃))) (lookup! k₁₂ (weaken₃ m₄)))))
f₃₄ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₃₄)
(lookup! k₃₄ (weaken₃ m₁)))
(lookup! k₃₄ (weaken₃ m₂))) v₃) v₄)))
in
-- A correct but inefficient implementation.
-- May want to speed it up after constants are finalized.
union (zip! f₁₂ m₁ m₂) (zip! f₃₄ m₃ m₄)
-- Type signature of Atlas-Δconst is boilerplate.
Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) →
Atlas-term Γ (Atlas-Δtype (Atlas-lookup c))
Atlas-Δconst true = const false
Atlas-Δconst false = const false
-- Δxor = λ x Δx y Δy → Δx xor Δy
Atlas-Δconst xor =
let
Δx = var (that (that this))
Δy = var this
in abs (abs (abs (abs (app (app (const xor) Δx) Δy))))
Atlas-Δconst empty = const empty
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
Atlas-Δconst update =
let
k = var (that (that (that (that (that this)))))
Δk = var (that (that (that (that this))))
v = var (that (that (that this)))
Δv = var (that (that this))
-- m = var (that this) -- unused parameter
Δm = var this
in
abs (abs (abs (abs (abs (abs
(insert (apply Δk k) (apply Δv v)
(delete k v Δm)))))))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
Atlas-Δconst zip =
let
Δf = var (that (that (that (that this))))
m₁ = var (that (that (that this)))
Δm₁ = var (that (that this))
m₂ = var (that this)
Δm₂ = var this
g = abs (app (app (weaken₁ Δf) (var this)) nil-term)
in
abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂))))))
Atlas = calculus-with
Atlas-type
Atlas-const
Atlas-lookup
Atlas-Δtype
Atlas-Δconst
|
module Syntax.Language.Atlas where
-- Base types of the calculus Atlas
-- to be used with Plotkin-style language description
--
-- Atlas supports maps with neutral elements. The change type to
-- `Map κ ι` is `Map κ Δι`, where Δι is the change type of the
-- ground type ι. Such a change to maps can support insertions
-- and deletions as well: Inserting `k -> v` means mapping `k` to
-- the change from the neutral element to `v`, and deleting
-- `k -> v` means mapping `k` to the change from `v` to the
-- neutral element.
open import Syntax.Language.Calculus
data Atlas-type : Set where
Bool : Atlas-type
Map : (κ : Atlas-type) (ι : Atlas-type) → Atlas-type
data Atlas-const : Set where
true : Atlas-const
false : Atlas-const
xor : Atlas-const
empty : ∀ {κ ι : Atlas-type} → Atlas-const
update : ∀ {κ ι : Atlas-type} → Atlas-const
zip : ∀ {κ a b c : Atlas-type} → Atlas-const
Atlas-lookup : Atlas-const → Type Atlas-type
Atlas-lookup true = base Bool
Atlas-lookup false = base Bool
Atlas-lookup xor = base Bool ⇒ base Bool ⇒ base Bool
Atlas-lookup (empty {κ} {ι}) = base (Map κ ι)
-- `update key val my-map` would
-- - insert if `key` is not present in `my-map`
-- - delete if `val` is the neutral element
-- - make an update otherwise
Atlas-lookup (update {κ} {ι}) =
base κ ⇒ base ι ⇒ base (Map κ ι) ⇒ base (Map κ ι)
-- Model of zip = Haskell Data.List.zipWith
-- zipWith :: (a → b → c) → [a] → [b] → [c]
Atlas-lookup (zip {κ} {a} {b} {c}) =
(base κ ⇒ base a ⇒ base b ⇒ base c) ⇒
base (Map κ a) ⇒ base (Map κ b) ⇒ base (Map κ c)
Atlas-Δbase : Atlas-type → Atlas-type
-- change to a boolean is a xor-rand
Atlas-Δbase Bool = Bool
-- change to a map is change to its values
Atlas-Δbase (Map key val) = (Map key (Atlas-Δbase val))
Atlas-Δtype : Type Atlas-type → Type Atlas-type
Atlas-Δtype = lift-Δtype₀ Atlas-Δbase
Atlas-context : Set
Atlas-context = Context {Type Atlas-type}
Atlas-term : Atlas-context → Type Atlas-type → Set
Atlas-term = Term {Atlas-type} {Atlas-const} {Atlas-lookup}
-- Every base type has a known nil-change.
-- The nil-change of ι is also the neutral element of Map κ Δι.
neutral : ∀ {ι : Atlas-type} → Atlas-const
neutral {Bool} = false
neutral {Map κ ι} = empty {κ} {ι}
neutral-term : ∀ {ι Γ} → Atlas-term Γ (base ι)
neutral-term {Bool} = const (neutral {Bool})
neutral-term {Map κ ι} = const (neutral {Map κ ι})
nil-const : ∀ {ι : Atlas-type} → Atlas-const
nil-const {ι} = neutral {Atlas-Δbase ι}
nil-term : ∀ {ι Γ} → Atlas-term Γ (base (Atlas-Δbase ι))
nil-term {Bool} = const (nil-const {Bool})
nil-term {Map κ ι} = const (nil-const {Map κ ι})
-- Shorthands of constants
--
-- There's probably a uniform way to lift constants
-- into term constructors.
--
-- TODO: write this and call it Syntax.Term.Plotkin.lift-term
zip! : ∀ {κ a b c Γ} →
Atlas-term Γ (base κ ⇒ base a ⇒ base b ⇒ base c) →
Atlas-term Γ (base (Map κ a)) → Atlas-term Γ (base (Map κ b)) →
Atlas-term Γ (base (Map κ c))
zip! f m₁ m₂ = app (app (app (const zip) f) m₁) m₂
lookup! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base ι)
lookup! = {!!} -- TODO: add constant `lookup`
-- diff-term and apply-term
-- b₀ ⊝ b₁ = b₀ xor b₁
-- m₀ ⊝ m₁ = zip _⊝_ m₀ m₁
Atlas-diff : ∀ {ι Γ} →
Atlas-term Γ (base ι ⇒ base ι ⇒ Atlas-Δtype (base ι))
Atlas-diff {Bool} = const xor
Atlas-diff {Map κ ι} = app (const zip) (abs Atlas-diff)
-- b ⊕ Δb = b xor Δb
-- m ⊕ Δm = zip _⊕_ m Δm
Atlas-apply : ∀ {ι Γ} →
Atlas-term Γ (Atlas-Δtype (base ι) ⇒ base ι ⇒ base ι)
Atlas-apply {Bool} = const xor
Atlas-apply {Map κ ι} = app (const zip) (abs Atlas-apply)
-- Shorthands for working with diff-term and apply-term
diff : ∀ {τ Γ} →
Atlas-term Γ τ → Atlas-term Γ τ →
Atlas-term Γ (Atlas-Δtype τ)
diff s t = app (app (lift-diff Atlas-diff Atlas-apply) s) t
apply : ∀ {τ Γ} →
Atlas-term Γ (Atlas-Δtype τ) → Atlas-term Γ τ →
Atlas-term Γ τ
apply s t = app (app (lift-apply Atlas-diff Atlas-apply) s) t
-- Shorthands for creating changes corresponding to
-- insertion/deletion.
update! : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (base (Map κ ι)) →
Atlas-term Γ (base (Map κ ι))
update! k v m = app (app (app (const update) k) v) m
insert : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
-- last argument is the change accumulator
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
delete : ∀ {κ ι Γ} →
Atlas-term Γ (base κ) → Atlas-term Γ (base ι) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι))) →
Atlas-term Γ (Atlas-Δtype (base (Map κ ι)))
insert k v acc = update! k (diff v neutral-term) acc
delete k v acc = update! k (diff neutral-term v) acc
-- The binary operator such that
-- union t t ≡ t
-- To implement union, we need for each non-map base-type
-- an operator such that `op v v = v` on all values `v`.
-- for Booleans, conjunction is good enough.
--
-- TODO (later): support conjunction, probably by Boolean
-- elimination form if-then-else
postulate
and! : ∀ {Γ} →
Atlas-term Γ (base Bool) → Atlas-term Γ (base Bool) →
Atlas-term Γ (base Bool)
union : ∀ {ι Γ} →
Atlas-term Γ (base ι) → Atlas-term Γ (base ι) →
Atlas-term Γ (base ι)
union {Bool} s t = and! s t
union {Map κ ι} s t =
let
union-term = abs (abs (union (var (that this)) (var this)))
in
zip! (abs union-term) s t
-- Shorthand for 4-way zip
zip4! : ∀ {κ a b c d e Γ} →
let
t:_ = λ ι → Atlas-term Γ (base ι)
in
Atlas-term Γ
(base κ ⇒ base a ⇒ base b ⇒ base c ⇒ base d ⇒ base e) →
t: Map κ a → t: Map κ b → t: Map κ c → t: Map κ d → t: Map κ e
zip4! f m₁ m₂ m₃ m₄ =
let
v₁ = var (that this)
v₂ = var this
v₃ = var (that this)
v₄ = var this
k₁₂ = var (that (that this))
k₃₄ = var (that (that this))
f₁₂ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₁₂) v₁) v₂)
(lookup! k₁₂ (weaken₃ m₃))) (lookup! k₁₂ (weaken₃ m₄)))))
f₃₄ = abs (abs (abs (app (app (app (app (app
(weaken₃ f) k₃₄)
(lookup! k₃₄ (weaken₃ m₁)))
(lookup! k₃₄ (weaken₃ m₂))) v₃) v₄)))
in
-- A correct but inefficient implementation.
-- May want to speed it up after constants are finalized.
union (zip! f₁₂ m₁ m₂) (zip! f₃₄ m₃ m₄)
-- Type signature of Atlas-Δconst is boilerplate.
Atlas-Δconst : ∀ {Γ} → (c : Atlas-const) →
Atlas-term Γ (Atlas-Δtype (Atlas-lookup c))
Atlas-Δconst true = const false
Atlas-Δconst false = const false
-- Δxor = λ x Δx y Δy → Δx xor Δy
Atlas-Δconst xor =
let
Δx = var (that (that this))
Δy = var this
in abs (abs (abs (abs (app (app (const xor) Δx) Δy))))
Atlas-Δconst empty = const empty
-- If k ⊕ Δk ≡ k, then
-- Δupdate k Δk v Δv m Δm = update k Δv Δm
-- else it is a deletion followed by insertion:
-- Δupdate k Δk v Δv m Δm =
-- insert (k ⊕ Δk) (v ⊕ Δv) (delete k v Δm)
--
-- We implement the else-branch only for the moment.
Atlas-Δconst update =
let
k = var (that (that (that (that (that this)))))
Δk = var (that (that (that (that this))))
v = var (that (that (that this)))
Δv = var (that (that this))
-- m = var (that this) -- unused parameter
Δm = var this
in
abs (abs (abs (abs (abs (abs
(insert (apply Δk k) (apply Δv v)
(delete k v Δm)))))))
-- Δzip f Δf m₁ Δm₁ m₂ Δm₂ | true? (f ⊕ Δf ≡ f)
--
-- ... | true =
-- zip (λ k Δv₁ Δv₂ → Δf (lookup k m₁) Δv₁ (lookup k m₂) Δv₂)
-- Δm₁ Δm₂
--
-- ... | false = zip₄ Δf m₁ Δm₁ m₂ Δm₂
--
-- we implement the false-branch for the moment.
Atlas-Δconst zip =
let
Δf = var (that (that (that (that this))))
m₁ = var (that (that (that this)))
Δm₁ = var (that (that this))
m₂ = var (that this)
Δm₂ = var this
g = abs (app (app (weaken₁ Δf) (var this)) nil-term)
in
abs (abs (abs (abs (abs (abs (zip4! g m₁ Δm₁ m₂ Δm₂))))))
Atlas = calculus-with
Atlas-type
Atlas-const
Atlas-lookup
Atlas-Δtype
Atlas-Δconst
|
fix bug in `union` by postulating conjunction
|
Atlas: fix bug in `union` by postulating conjunction
Old-commit-hash: 69ed9a83b2b162709e5dfcf541f9d0bf18c7e970
|
Agda
|
mit
|
inc-lc/ilc-agda
|
080a08a480530a0bfbe7ee42fb486407caea4a5e
|
Parametric/Change/Equivalence.agda
|
Parametric/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Equivalence
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
module _ {ℓ} {A} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
_≙_ : ∀ dx dy → Set
dx ≙ dy = x ⊞ dx ≡ x ⊞ dy
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = refl
≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-symm ≙ = sym ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely. That should be true for functions
-- using changes parametrically, for derivatives and function changes, and
-- for functions using only the interface to changes (including the fact
-- that function changes are functions). Stating the general result, though,
-- seems hard, we should rather have lemmas proving that certain classes of
-- functions respect this equivalence.
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Parametric.Change.Equivalence where
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
-- Extension Point: None (currently). Do we need to allow plugins to customize
-- this concept?
Structure : Set
Structure = Unit
module Structure (unused : Structure) where
module _ {ℓ} {A} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- Delta-observational equivalence: these asserts that two changes
-- give the same result when applied to a base value.
_≙_ : ∀ dx dy → Set
dx ≙ dy = x ⊞ dx ≡ x ⊞ dy
-- _≙_ is indeed an equivalence relation:
≙-refl : ∀ {dx} → dx ≙ dx
≙-refl = refl
≙-symm : ∀ {dx dy} → dx ≙ dy → dy ≙ dx
≙-symm ≙ = sym ≙
≙-trans : ∀ {dx dy dz} → dx ≙ dy → dy ≙ dz → dx ≙ dz
≙-trans ≙₁ ≙₂ = trans ≙₁ ≙₂
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely. That should be true for functions
-- using changes parametrically, for derivatives and function changes, and
-- for functions using only the interface to changes (including the fact
-- that function changes are functions). Stating the general result, though,
-- seems hard, we should rather have lemmas proving that certain classes of
-- functions respect this equivalence.
|
Remove extra imports
|
Remove extra imports
Old-commit-hash: 3a9775f6afd97585f6b425390b5431b43807b03e
|
Agda
|
mit
|
inc-lc/ilc-agda
|
3409935502fb668aa913b86adee0b90b81efb211
|
arrow.agda
|
arrow.agda
|
data Bool : Set where
true : Bool
false : Bool
_or_ : Bool → Bool → Bool
true or _ = true
_ or true = true
false or false = false
_and_ : Bool → Bool → Bool
false and _ = false
_ and false = false
true and true = true
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_≡_ : ℕ → ℕ → Bool
zero ≡ zero = true
suc n ≡ suc m = n ≡ m
_ ≡ _ = false
----------------------------------------
infixr 5 _∷_
data List (A : Set) : Set where
∘ : List A
_∷_ : A → List A → List A
any : {A : Set} → (A → Bool) → List A → Bool
any _ ∘ = false
any f (x ∷ xs) = (f x) or (any f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ ∘ = false
x ∈ (y ∷ ys) with x ≡ y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q ≡ s
(p ⇒ q) ≡≡ (r ⇒ s) = (p ≡ r) and (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ ∘ = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure ∘ found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) or (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs ∘)
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs (n ∷ ∘))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) and (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ ∘ ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) or (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
proofs : List Arrow
proofs =
(3 ⇒ (⇒ 4)) ∷
-- (5 ⇒ (⇒ 4)) ∷
(6 ⇒ (⇒ 4)) ∷
(3 ⇒ (⇒ 3)) ∷
(3 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(5 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 7)) ∷
(6 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 7)) ∷
(5 ⇒ (⇒ 10)) ∷
(10 ⇒ (⇒ 4)) ∷
(5 ⇒ (⇒ 11)) ∷
(6 ⇒ (⇒ 11)) ∷
(11 ⇒ (⇒ 4)) ∷
(10 ⇒ (⇒ 7)) ∷
(8 ⇒ (⇒ 4)) ∷
(9 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 8)) ∷
(3 ⇒ (⇒ 8)) ∷
(5 ⇒ (3 ⇒ (⇒ 9))) ∷
(6 ⇒ (7 ⇒ (⇒ 10))) ∷
(6 ⇒ (3 ⇒ (⇒ 3))) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (8 ⇒ (⇒ 10))) ∷
(3 ⇒ (10 ⇒ (⇒ 9))) ∷
(5 ⇒ (⇒ 1)) ∷
(3 ⇒ (1 ⇒ (⇒ 9))) ∷
(1 ⇒ (⇒ 2)) ∷
(10 ⇒ (⇒ 2)) ∷ ∘
cms : List Separation
cms =
(model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (5 ∷ 3 ∷ 7 ∷ 7 ∷ ∘)) ∷
(model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (5 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ ∘) (6 ∷ 3 ∷ ∘)) ∷
(model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ ∘) (6 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ ∘) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ ∘) (3 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 11 ∷ 7 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ ∘)) ∷
(model (10 ∷ 9 ∷ ∘) (1 ∷ ∘)) ∷
(model (3 ∷ 4 ∷ 11 ∷ ∘) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ ∘)) ∷
(model (12 ∷ 7 ∷ 1 ∷ ∘) (4 ∷ 11 ∷ 8 ∷ ∘)) ∷
(model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ ∘) (11 ∷ ∘)) ∷
(model (12 ∷ 4 ∷ 10 ∷ 1 ∷ ∘) (11 ∷ 3 ∷ ∘)) ∷
(model (3 ∷ 6 ∷ 5 ∷ ∘) (∘)) ∷
(model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ ∘) (12 ∷ ∘)) ∷ ∘
testp : Arrow
testp = (5 ⇒ (⇒ 10))
testd : Arrow
testd = (5 ⇒ (⇒ 4))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
|
data Bool : Set where
true : Bool
false : Bool
_∨_ : Bool → Bool → Bool
true ∨ _ = true
false ∨ b = b
_∧_ : Bool → Bool → Bool
false ∧ _ = false
true ∧ b = b
----------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
_==_ : ℕ → ℕ → Bool
zero == zero = true
suc n == suc m = n == m
_ == _ = false
----------------------------------------
data List (A : Set) : Set where
[] : List A
_∷_ : A → List A → List A
infixr 5 _∷_
[_] : {A : Set} → A → List A
[ x ] = x ∷ []
any : {A : Set} → (A → Bool) → List A → Bool
any _ [] = false
any f (x ∷ xs) = (f x) ∨ (any f xs)
_∈_ : ℕ → List ℕ → Bool
x ∈ [] = false
x ∈ (y ∷ ys) with x == y
... | true = true
... | false = x ∈ ys
_∋_ : List ℕ → ℕ → Bool
xs ∋ y = y ∈ xs
----------------------------------------
data Arrow : Set where
⇒_ : ℕ → Arrow
_⇒_ : ℕ → Arrow → Arrow
_≡≡_ : Arrow → Arrow → Bool
(⇒ q) ≡≡ (⇒ s) = q == s
(p ⇒ q) ≡≡ (r ⇒ s) = (p == r) ∧ (q ≡≡ s)
_ ≡≡ _ = false
_∈∈_ : Arrow → List Arrow → Bool
x ∈∈ [] = false
x ∈∈ (y ∷ ys) with x ≡≡ y
... | true = true
... | false = x ∈∈ ys
closure : List Arrow → List ℕ → List ℕ
closure [] found = found
closure ((⇒ n) ∷ rest) found = n ∷ (closure rest (n ∷ found))
closure ((n ⇒ q) ∷ rest) found with (n ∈ found) ∨ (n ∈ (closure rest found))
... | true = closure (q ∷ rest) found
... | false = closure rest found
_,_⊢_ : List Arrow → List ℕ → ℕ → Bool
cs , ps ⊢ q = q ∈ (closure cs ps)
_⊢_ : List Arrow → Arrow → Bool
cs ⊢ (⇒ q) = q ∈ (closure cs [])
cs ⊢ (p ⇒ q) = ((⇒ p) ∷ cs) ⊢ q
----------------------------------------
data Separation : Set where
model : List ℕ → List ℕ → Separation
modelsupports : Separation → List Arrow → ℕ → Bool
modelsupports (model holds _) cs n = cs , holds ⊢ n
modeldenies : Separation → List Arrow → ℕ → Bool
modeldenies (model _ fails) cs n = any (_∋_ (closure cs ([ n ]))) fails
_⟪!_⟫_ : List Arrow → Separation → Arrow → Bool
cs ⟪! m ⟫ (⇒ q) = modeldenies m cs q
cs ⟪! m ⟫ (p ⇒ q) = (modelsupports m cs p) ∧ (cs ⟪! m ⟫ q)
_⟪_⟫_ : List Arrow → List Separation → Arrow → Bool
cs ⟪ [] ⟫ arr = false
cs ⟪ m ∷ ms ⟫ arr = (cs ⟪! m ⟫ arr) ∨ (cs ⟪ ms ⟫ arr)
----------------------------------------
data Relation : Set where
Proved : Relation
Derivable : Relation
Separated : Relation
Unknown : Relation
consider : List Arrow → List Separation → Arrow → Relation
consider cs ms arr with (arr ∈∈ cs)
... | true = Proved
... | false with (cs ⊢ arr)
... | true = Derivable
... | false with (cs ⟪ ms ⟫ arr)
... | true = Separated
... | false = Unknown
proofs : List Arrow
proofs =
(3 ⇒ (⇒ 4)) ∷
-- (5 ⇒ (⇒ 4)) ∷
(6 ⇒ (⇒ 4)) ∷
(3 ⇒ (⇒ 3)) ∷
(3 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(9 ⇒ (⇒ 3)) ∷
(5 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 7)) ∷
(6 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 8)) ∷
(10 ⇒ (⇒ 7)) ∷
(5 ⇒ (⇒ 10)) ∷
(10 ⇒ (⇒ 4)) ∷
(5 ⇒ (⇒ 11)) ∷
(6 ⇒ (⇒ 11)) ∷
(11 ⇒ (⇒ 4)) ∷
(10 ⇒ (⇒ 7)) ∷
(8 ⇒ (⇒ 4)) ∷
(9 ⇒ (⇒ 7)) ∷
(9 ⇒ (⇒ 8)) ∷
(3 ⇒ (⇒ 8)) ∷
(5 ⇒ (3 ⇒ (⇒ 9))) ∷
(6 ⇒ (7 ⇒ (⇒ 10))) ∷
(6 ⇒ (3 ⇒ (⇒ 3))) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (⇒ 7)) ∷
(7 ⇒ (8 ⇒ (⇒ 10))) ∷
(3 ⇒ (10 ⇒ (⇒ 9))) ∷
(5 ⇒ (⇒ 1)) ∷
(3 ⇒ (1 ⇒ (⇒ 9))) ∷
(1 ⇒ (⇒ 2)) ∷
(10 ⇒ (⇒ 2)) ∷ []
cms : List Separation
cms =
(model (12 ∷ 6 ∷ 11 ∷ 4 ∷ 1 ∷ []) (5 ∷ 3 ∷ 7 ∷ 7 ∷ [])) ∷
(model (6 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ []) (5 ∷ [])) ∷
(model (12 ∷ 5 ∷ 11 ∷ 4 ∷ 1 ∷ []) (6 ∷ 3 ∷ [])) ∷
(model (5 ∷ 3 ∷ 11 ∷ 4 ∷ 7 ∷ 8 ∷ 3 ∷ 9 ∷ 10 ∷ 1 ∷ []) (6 ∷ [])) ∷
(model (12 ∷ 4 ∷ 11 ∷ []) (5 ∷ 6 ∷ 3 ∷ 8 ∷ 9 ∷ 1 ∷ [])) ∷
(model (12 ∷ 5 ∷ 6 ∷ 4 ∷ 11 ∷ 1 ∷ []) (3 ∷ [])) ∷
(model (12 ∷ 4 ∷ 11 ∷ 7 ∷ []) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 8 ∷ 1 ∷ [])) ∷
(model (10 ∷ 9 ∷ []) (1 ∷ [])) ∷
(model (3 ∷ 4 ∷ 11 ∷ []) (9 ∷ 5 ∷ 6 ∷ 10 ∷ 7 ∷ 1 ∷ [])) ∷
(model (12 ∷ 7 ∷ 1 ∷ []) (4 ∷ 11 ∷ 8 ∷ [])) ∷
(model (9 ∷ 3 ∷ 10 ∷ 8 ∷ 1 ∷ []) (11 ∷ [])) ∷
(model (12 ∷ 4 ∷ 10 ∷ 1 ∷ []) (11 ∷ 3 ∷ [])) ∷
(model (3 ∷ 6 ∷ 5 ∷ []) ([])) ∷
(model (1 ∷ 2 ∷ 3 ∷ 4 ∷ 5 ∷ 6 ∷ 7 ∷ 8 ∷ 9 ∷ 10 ∷ 11 ∷ []) (12 ∷ [])) ∷ []
testp : Arrow
testp = (5 ⇒ (⇒ 10))
testd : Arrow
testd = (5 ⇒ (⇒ 4))
tests : Arrow
tests = (5 ⇒ (⇒ 3))
testu : Arrow
testu = (6 ⇒ (⇒ 1))
|
Use standard syntax
|
Use standard syntax
|
Agda
|
mit
|
louisswarren/hieretikz
|
20a1abbafdf38b144be117be78c688be2786fbee
|
Neglible.agda
|
Neglible.agda
|
{-# OPTIONS --copatterns #-}
open import Algebra
open import Function
open import Data.Nat.NP
open import Data.Nat.Distance
open import Data.Nat.Properties
open import Data.Two
open import Data.Zero
open import Data.Product
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
open import HoTT
open Equivalences
open import Explore.Core
open import Explore.Universe.Type {𝟘}
open import Explore.Universe.Base
module Neglible where
module prop = CommutativeSemiring commutativeSemiring
module OR = Poset (DecTotalOrder.poset decTotalOrder)
≤-*-cancel : ∀ {x m n} → 1 ≤ x → x * m ≤ x * n → m ≤ n
≤-*-cancel {suc x} {m} {n} (s≤s le) mn
rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-≤ _ _ _ mn
record ℕ→ℚ : Set where
constructor _/_[_]
field
εN : (n : ℕ) → ℕ
εD : (n : ℕ) → ℕ
εD-pos : ∀ n → εD n > 0
record Is-Neg (ε : ℕ→ℚ) : Set where
constructor mk
open ℕ→ℚ ε
field
cₙ : (c : ℕ) → ℕ
prf : ∀(c n : ℕ) → n > cₙ n → n ^ c * εN n ≤ εD n
open Is-Neg
0ℕℚ : ℕ→ℚ
ℕ→ℚ.εN 0ℕℚ _ = 0
ℕ→ℚ.εD 0ℕℚ _ = 1
ℕ→ℚ.εD-pos 0ℕℚ _ = s≤s z≤n
0ℕℚ-neg : Is-Neg 0ℕℚ
cₙ 0ℕℚ-neg _ = 0
prf 0ℕℚ-neg c n x = OR.trans (OR.reflexive (proj₂ prop.zero (n ^ c))) z≤n
_+ℕℚ_ : ℕ→ℚ → ℕ→ℚ → ℕ→ℚ
ℕ→ℚ.εN ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εN n * μD n + μN n * εD n
ℕ→ℚ.εD ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εD n * μD n
ℕ→ℚ.εD-pos ((εN / εD [ εD+ ]) +ℕℚ (μN / μD [ μD+ ])) n = εD+ n *-mono μD+ n
+ℕℚ-neg : {ε μ : ℕ→ℚ} → Is-Neg ε → Is-Neg μ → Is-Neg (ε +ℕℚ μ)
cₙ (+ℕℚ-neg ε μ) n = 1 + cₙ ε n + cₙ μ n
prf (+ℕℚ-neg {εM} {μM} ε μ) c n n>nc = ≤-*-cancel {x = n} (OR.trans (s≤s z≤n) n>nc) lemma
where
open ≤-Reasoning
open ℕ→ℚ εM
open ℕ→ℚ μM renaming (εN to μN; εD to μD; εD-pos to μD-pos)
lemma = n * (n ^ c * (εN n * μD n + μN n * εD n))
≡⟨ ! prop.*-assoc n (n ^ c) _
∙ proj₁ prop.distrib (n ^ (1 + c)) (εN n * μD n) (μN n * εD n)
∙ ap₂ _+_ (! prop.*-assoc (n ^ (1 + c)) (εN n) (μD n))
(! (prop.*-assoc (n ^ (1 + c)) (μN n) (εD n))) ⟩
n ^ (1 + c) * εN n * μD n + n ^ (1 + c) * μN n * εD n
≤⟨ (prf ε (1 + c) n (OR.trans (s≤s (≤-step (m≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (μD n ∎))
+-mono (prf μ (1 + c) n (OR.trans (s≤s (≤-step (n≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (εD n ∎)) ⟩
εD n * μD n + μD n * εD n
≡⟨ ap₂ _+_ (refl {x = εD n * μD n}) (prop.*-comm (μD n) (εD n) ∙ ! proj₂ prop.+-identity (εD n * μD n)) ⟩
2 * (εD n * μD n)
≤⟨ OR.trans (s≤s (s≤s z≤n)) n>nc *-mono (εD n * μD n ∎) ⟩
n * (εD n * μD n)
∎
module _ (Rᵁ : ℕ → U)(let R = λ n → El (Rᵁ n)) where
# : ∀ {n} → Count (R n)
# {n} = count (Rᵁ n)
record _~_ (f g : (x : ℕ) → R x → 𝟚) : Set where
constructor mk
field
ε : ℕ→ℚ
open ℕ→ℚ ε
field
ε-neg : Is-Neg ε
bounded : ∀ k → εD k * dist (# (f k)) (# (g k)) ≤ Card (Rᵁ k) * εN k
~-trans : Transitive _~_
_~_.ε (~-trans x x₁) = _
_~_.ε-neg (~-trans x x₁) = +ℕℚ-neg (_~_.ε-neg x) (_~_.ε-neg x₁)
_~_.bounded (~-trans {f}{g}{h}(mk ε₀ ε₀-neg fg) (mk ε₁ ε₁-neg gh)) k
= (b * d) * dist #f #h
≤⟨ (b * d ∎) *-mono dist-sum #f #g #h ⟩
(b * d) * (dist #f #g + dist #g #h)
≡⟨ proj₁ prop.distrib (b * d) (dist #f #g) (dist #g #h)
∙ ap₂ _+_ (ap₂ _*_ (prop.*-comm b d) refl
∙ prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))
⟩
d * (b * dist #f #g) + b * (d * dist #g #h)
≤⟨ ((d ∎) *-mono fg k) +-mono ((b ∎) *-mono gh k) ⟩
d * (|R| * a) + b * (|R| * c)
≡⟨ ap₂ _+_ (rot d |R| a) (rot b |R| c) ∙ ! proj₁ prop.distrib |R| (a * d) (c * b) ⟩
|R| * ℕ→ℚ.εN (ε₀ +ℕℚ ε₁) k
∎
where
open ≤-Reasoning
rot : ∀ x y z → x * (y * z) ≡ y * (z * x)
rot x y z = prop.*-comm x (y * z) ∙ prop.*-assoc y z x
|R| = Card (Rᵁ k)
#f = # (f k)
#g = # (g k)
#h = # (h k)
a = ℕ→ℚ.εN ε₀ k
b = ℕ→ℚ.εD ε₀ k
c = ℕ→ℚ.εN ε₁ k
d = ℕ→ℚ.εD ε₁ k
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --copatterns #-}
open import Algebra
open import Function
open import Data.Nat.NP
open import Data.Nat.Distance
open import Data.Nat.Properties
open import Data.Two
open import Data.Zero
open import Data.Product
open import Relation.Binary
open import Relation.Binary.PropositionalEquality.NP
open import HoTT
open Equivalences
open import Explore.Core
open import Explore.Universe.Type {𝟘}
open import Explore.Universe.Base
module Neglible where
module prop = CommutativeSemiring commutativeSemiring
module OR = Poset (DecTotalOrder.poset decTotalOrder)
≤-*-cancel : ∀ {x m n} → 1 ≤ x → x * m ≤ x * n → m ≤ n
≤-*-cancel {suc x} {m} {n} (s≤s le) mn
rewrite prop.*-comm (suc x) m | prop.*-comm (suc x) n = cancel-*-right-≤ _ _ _ mn
record ℕ→ℚ : Set where
constructor _/_[_]
field
εN : (n : ℕ) → ℕ
εD : (n : ℕ) → ℕ
εD-pos : ∀ n → εD n > 0
record Is-Neg (ε : ℕ→ℚ) : Set where
constructor mk
open ℕ→ℚ ε
field
cₙ : (c : ℕ) → ℕ
prf : ∀(c n : ℕ) → n > cₙ n → n ^ c * εN n ≤ εD n
open Is-Neg
0ℕℚ : ℕ→ℚ
ℕ→ℚ.εN 0ℕℚ _ = 0
ℕ→ℚ.εD 0ℕℚ _ = 1
ℕ→ℚ.εD-pos 0ℕℚ _ = s≤s z≤n
0ℕℚ-neg : Is-Neg 0ℕℚ
cₙ 0ℕℚ-neg _ = 0
prf 0ℕℚ-neg c n x = OR.trans (OR.reflexive (proj₂ prop.zero (n ^ c))) z≤n
_+ℕℚ_ : ℕ→ℚ → ℕ→ℚ → ℕ→ℚ
ℕ→ℚ.εN ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εN n * μD n + μN n * εD n
ℕ→ℚ.εD ((εN / εD [ _ ]) +ℕℚ (μN / μD [ _ ])) n = εD n * μD n
ℕ→ℚ.εD-pos ((εN / εD [ εD+ ]) +ℕℚ (μN / μD [ μD+ ])) n = εD+ n *-mono μD+ n
+ℕℚ-neg : {ε μ : ℕ→ℚ} → Is-Neg ε → Is-Neg μ → Is-Neg (ε +ℕℚ μ)
cₙ (+ℕℚ-neg ε μ) n = 1 + cₙ ε n + cₙ μ n
prf (+ℕℚ-neg {εM} {μM} ε μ) c n n>nc = ≤-*-cancel {x = n} (OR.trans (s≤s z≤n) n>nc) lemma
where
open ≤-Reasoning
open ℕ→ℚ εM
open ℕ→ℚ μM renaming (εN to μN; εD to μD; εD-pos to μD-pos)
lemma = n * (n ^ c * (εN n * μD n + μN n * εD n))
≡⟨ ! prop.*-assoc n (n ^ c) _
∙ proj₁ prop.distrib (n ^ (1 + c)) (εN n * μD n) (μN n * εD n)
∙ ap₂ _+_ (! prop.*-assoc (n ^ (1 + c)) (εN n) (μD n))
(! (prop.*-assoc (n ^ (1 + c)) (μN n) (εD n))) ⟩
n ^ (1 + c) * εN n * μD n + n ^ (1 + c) * μN n * εD n
≤⟨ (prf ε (1 + c) n (OR.trans (s≤s (≤-step (m≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (μD n ∎))
+-mono (prf μ (1 + c) n (OR.trans (s≤s (≤-step (n≤m+n (cₙ ε n) (cₙ μ n)))) n>nc) *-mono (εD n ∎)) ⟩
εD n * μD n + μD n * εD n
≡⟨ ap₂ _+_ (refl {x = εD n * μD n}) (prop.*-comm (μD n) (εD n) ∙ ! proj₂ prop.+-identity (εD n * μD n)) ⟩
2 * (εD n * μD n)
≤⟨ OR.trans (s≤s (s≤s z≤n)) n>nc *-mono (εD n * μD n ∎) ⟩
n * (εD n * μD n)
∎
infix 4 _≤→_
record _≤→_ (f g : ℕ→ℚ) : Set where
constructor mk
open ℕ→ℚ f renaming (εN to fN; εD to fD)
open ℕ→ℚ g renaming (εN to gN; εD to gD)
field
-- fN k / fD k ≤ gN k / gD k
≤→ : ∀ k → fN k * gD k ≤ gN k * fD k
≤→-trans : ∀ {f g h} → f ≤→ g → g ≤→ h → f ≤→ h
_≤→_.≤→ (≤→-trans {fN / fD [ fD-pos ]} {gN / gD [ gD-pos ]} {hN / hD [ hD-pos ]} (mk fg) (mk gh)) k
= ≤-*-cancel (gD-pos k) lemma
where
open ≤-Reasoning
lemma : gD k * (fN k * hD k) ≤ gD k * (hN k * fD k)
lemma = gD k * (fN k * hD k)
≡⟨ ! prop.*-assoc (gD k) (fN k) (hD k)
∙ ap (flip _*_ (hD k)) (prop.*-comm (gD k) (fN k))
⟩
(fN k * gD k) * hD k
≤⟨ fg k *-mono OR.refl ⟩
(gN k * fD k) * hD k
≡⟨ prop.*-assoc (gN k) (fD k) (hD k)
∙ ap (_*_ (gN k)) (prop.*-comm (fD k) (hD k))
∙ ! prop.*-assoc (gN k) (hD k) (fD k)
⟩
(gN k * hD k) * fD k
≤⟨ gh k *-mono OR.refl ⟩
(hN k * gD k) * fD k
≡⟨ ap (flip _*_ (fD k)) (prop.*-comm (hN k) (gD k))
∙ prop.*-assoc (gD k) (hN k) (fD k)
⟩
gD k * (hN k * fD k)
∎
+ℕℚ-mono : ∀ {f f' g g'} → f ≤→ f' → g ≤→ g' → f +ℕℚ g ≤→ f' +ℕℚ g'
_≤→_.≤→ (+ℕℚ-mono {fN / fD [ _ ]} {f'N / f'D [ _ ]} {gN / gD [ _ ]} {g'N / g'D [ _ ]} (mk ff) (mk gg)) k
= (fN k * gD k + gN k * fD k) * (f'D k * g'D k)
≡⟨ proj₂ prop.distrib (f'D k * g'D k) (fN k * gD k) (gN k * fD k) ⟩
fN k * gD k * (f'D k * g'D k) + gN k * fD k * (f'D k * g'D k)
≡⟨ ap₂ _+_ (*-interchange (fN k) (gD k) (f'D k) (g'D k) ∙ ap (_*_ (fN k * f'D k)) (prop.*-comm (gD k) (g'D k)))
(ap (_*_ (gN k * fD k)) (prop.*-comm (f'D k) (g'D k)) ∙ *-interchange (gN k) (fD k) (g'D k) (f'D k))
⟩
fN k * f'D k * (g'D k * gD k) + gN k * g'D k * (fD k * f'D k)
≤⟨ (ff k *-mono OR.refl) +-mono (gg k *-mono OR.refl) ⟩
f'N k * fD k * (g'D k * gD k) + g'N k * gD k * (fD k * f'D k)
≡⟨ ap₂ _+_ (*-interchange (f'N k) (fD k) (g'D k) (gD k))
(ap (_*_ (g'N k * gD k)) (prop.*-comm (fD k) (f'D k))
∙ *-interchange (g'N k) (gD k) (f'D k) (fD k)
∙ ap (_*_ (g'N k * f'D k)) (prop.*-comm (gD k) (fD k)))
⟩
f'N k * g'D k * (fD k * gD k) + g'N k * f'D k * (fD k * gD k)
≡⟨ ! proj₂ prop.distrib (fD k * gD k) (f'N k * g'D k) (g'N k * f'D k) ⟩
(f'N k * g'D k + g'N k * f'D k) * (fD k * gD k)
∎
where
open ≤-Reasoning
record NegBounded (f : ℕ→ℚ) : Set where
constructor mk
field
ε : ℕ→ℚ
ε-neg : Is-Neg ε
bounded : f ≤→ ε
module _ where
open NegBounded
≤-NB : {f g : ℕ→ℚ} → f ≤→ g → NegBounded g → NegBounded f
ε (≤-NB le nb) = ε nb
ε-neg (≤-NB le nb) = ε-neg nb
bounded (≤-NB le nb) = ≤→-trans le (bounded nb)
_+NB_ : {f g : ℕ→ℚ} → NegBounded f → NegBounded g → NegBounded (f +ℕℚ g)
ε (fNB +NB gNB) = ε fNB +ℕℚ ε gNB
ε-neg (fNB +NB gNB) = +ℕℚ-neg (ε-neg fNB) (ε-neg gNB)
bounded (fNB +NB gNB) = +ℕℚ-mono (bounded fNB) (bounded gNB)
module ~-NegBounded (Rᵁ : ℕ → U)(let R = λ n → El (Rᵁ n))(inh : ∀ x → 0 < Card (Rᵁ x)) where
# : ∀ {n} → Count (R n)
# {n} = count (Rᵁ n)
~dist : (f g : (x : ℕ) → R x → 𝟚) → ℕ→ℚ
ℕ→ℚ.εN (~dist f g) n = dist (# (f n)) (# (g n))
ℕ→ℚ.εD (~dist f g) n = Card (Rᵁ n)
ℕ→ℚ.εD-pos (~dist f g) n = inh n
~dist-sum : ∀ f g h → ~dist f h ≤→ ~dist f g +ℕℚ ~dist g h
_≤→_.≤→ (~dist-sum f g h) k
= #fh * (|R| * |R|)
≤⟨ dist-sum #f #g #h *-mono OR.refl ⟩
(#fg + #gh) * (|R| * |R|)
≡⟨ ! prop.*-assoc (#fg + #gh) |R| |R| ∙ ap (flip _*_ |R|) (proj₂ prop.distrib |R| #fg #gh) ⟩
(#fg * |R| + #gh * |R|) * |R|
∎
where
open ≤-Reasoning
|R| = Card (Rᵁ k)
#f = # (f k)
#g = # (g k)
#h = # (h k)
#fh = dist #f #h
#fg = dist #f #g
#gh = dist #g #h
record _~_ (f g : (x : ℕ) → R x → 𝟚) : Set where
constructor mk
field
~ : NegBounded (~dist f g)
~-trans : Transitive _~_
_~_.~ (~-trans {f}{g}{h} (mk fg) (mk gh)) = ≤-NB (~dist-sum f g h) (fg +NB gh)
module ~-Inlined (Rᵁ : ℕ → U)(let R = λ n → El (Rᵁ n)) where
# : ∀ {n} → Count (R n)
# {n} = count (Rᵁ n)
record _~_ (f g : (x : ℕ) → R x → 𝟚) : Set where
constructor mk
field
ε : ℕ→ℚ
open ℕ→ℚ ε
field
ε-neg : Is-Neg ε
bounded : ∀ k → εD k * dist (# (f k)) (# (g k)) ≤ Card (Rᵁ k) * εN k
~-trans : Transitive _~_
_~_.ε (~-trans x x₁) = _
_~_.ε-neg (~-trans x x₁) = +ℕℚ-neg (_~_.ε-neg x) (_~_.ε-neg x₁)
_~_.bounded (~-trans {f}{g}{h}(mk ε₀ ε₀-neg fg) (mk ε₁ ε₁-neg gh)) k
= (b * d) * dist #f #h
≤⟨ (b * d ∎) *-mono dist-sum #f #g #h ⟩
(b * d) * (dist #f #g + dist #g #h)
≡⟨ proj₁ prop.distrib (b * d) (dist #f #g) (dist #g #h)
∙ ap₂ _+_ (ap₂ _*_ (prop.*-comm b d) refl
∙ prop.*-assoc d b (dist #f #g)) (prop.*-assoc b d (dist #g #h))
⟩
d * (b * dist #f #g) + b * (d * dist #g #h)
≤⟨ ((d ∎) *-mono fg k) +-mono ((b ∎) *-mono gh k) ⟩
d * (|R| * a) + b * (|R| * c)
≡⟨ ap₂ _+_ (rot d |R| a) (rot b |R| c) ∙ ! proj₁ prop.distrib |R| (a * d) (c * b) ⟩
|R| * ℕ→ℚ.εN (ε₀ +ℕℚ ε₁) k
∎
where
open ≤-Reasoning
rot : ∀ x y z → x * (y * z) ≡ y * (z * x)
rot x y z = prop.*-comm x (y * z) ∙ prop.*-assoc y z x
|R| = Card (Rᵁ k)
#f = # (f k)
#g = # (g k)
#h = # (h k)
a = ℕ→ℚ.εN ε₀ k
b = ℕ→ℚ.εD ε₀ k
c = ℕ→ℚ.εN ε₁ k
d = ℕ→ℚ.εD ε₁ k
-- -}
-- -}
-- -}
-- -}
-- -}
-- -}
|
Update to Neglible
|
Update to Neglible
|
Agda
|
bsd-3-clause
|
crypto-agda/crypto-agda
|
f7a0824e23aa553c512f0fbff1879941ee29f6d0
|
lib/FFI/JS.agda
|
lib/FFI/JS.agda
|
module FFI.JS where
open import Data.Empty public renaming (⊥ to 𝟘)
open import Data.Unit.Base public renaming (⊤ to 𝟙)
open import Data.Char.Base public using (Char)
open import Data.String.Base public using (String)
open import Data.Bool.Base public using (Bool; true; false)
open import Data.List.Base using (List; []; _∷_)
open import Data.Product using (_×_) renaming (proj₁ to fst; proj₂ to snd)
open import Function using (id; _∘_)
open import Control.Process.Type
{-# COMPILED_JS Bool function (x,v) { return ((x)? v["true"]() : v["false"]()); } #-}
{-# COMPILED_JS true true #-}
{-# COMPILED_JS false false #-}
postulate
Number : Set
JSArray : Set → Set
JSObject : Set
JSValue : Set
postulate readNumber : String → Number
{-# COMPILED_JS readNumber Number #-}
postulate 0N : Number
{-# COMPILED_JS 0N 0 #-}
postulate 1N : Number
{-# COMPILED_JS 1N 1 #-}
postulate 2N : Number
{-# COMPILED_JS 2N 2 #-}
postulate 4N : Number
{-# COMPILED_JS 4N 4 #-}
postulate 8N : Number
{-# COMPILED_JS 8N 8 #-}
postulate 16N : Number
{-# COMPILED_JS 16N 16 #-}
postulate 32N : Number
{-# COMPILED_JS 32N 32 #-}
postulate 64N : Number
{-# COMPILED_JS 64N 64 #-}
postulate 128N : Number
{-# COMPILED_JS 128N 128 #-}
postulate 256N : Number
{-# COMPILED_JS 256N 256 #-}
postulate 512N : Number
{-# COMPILED_JS 512N 512 #-}
postulate 1024N : Number
{-# COMPILED_JS 1024N 1024 #-}
postulate 2048N : Number
{-# COMPILED_JS 2048N 2048 #-}
postulate 4096N : Number
{-# COMPILED_JS 4096N 4096 #-}
postulate _+_ : Number → Number → Number
{-# COMPILED_JS _+_ function(x) { return function(y) { return x + y; }; } #-}
postulate _−_ : Number → Number → Number
{-# COMPILED_JS _−_ function(x) { return function(y) { return x - y; }; } #-}
postulate _*_ : Number → Number → Number
{-# COMPILED_JS _*_ function(x) { return function(y) { return x * y; }; } #-}
postulate _/_ : Number → Number → Number
{-# COMPILED_JS _/_ function(x) { return function(y) { return x / y; }; } #-}
infixr 5 _++_
postulate _++_ : String → String → String
{-# COMPILED_JS _++_ function(x) { return function(y) { return x + y; }; } #-}
postulate _+JS_ : JSValue → JSValue → JSValue
{-# COMPILED_JS _+JS_ function(x) { return function(y) { return x + y; }; } #-}
postulate _≤JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≤JS_ function(x) { return function(y) { return x <= y; }; } #-}
postulate _<JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _<JS_ function(x) { return function(y) { return x < y; }; } #-}
postulate _>JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _>JS_ function(x) { return function(y) { return x > y; }; } #-}
postulate _≥JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≥JS_ function(x) { return function(y) { return x >= y; }; } #-}
postulate _===_ : JSValue → JSValue → Bool
{-# COMPILED_JS _===_ function(x) { return function(y) { return x === y; }; } #-}
postulate reverse : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS reverse function(ty) { return function(x) { return x.reverse(); }; } #-}
postulate sort : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS sort function(ty) { return function(x) { return x.sort(); }; } #-}
postulate split : (sep target : String) → JSArray String
{-# COMPILED_JS split function(sep) { return function(target) { return target.split(sep); }; } #-}
postulate join : (sep : String)(target : JSArray String) → String
{-# COMPILED_JS join function(sep) { return function(target) { return target.join(sep); }; } #-}
postulate fromList : {A B : Set}(xs : List A)(fromElt : A → B) → JSArray B
{-# COMPILED_JS fromList require("libagda").fromList #-}
postulate length : String → Number
{-# COMPILED_JS length function(s) { return s.length; } #-}
postulate JSON-stringify : JSValue → String
{-# COMPILED_JS JSON-stringify JSON.stringify #-}
postulate JSON-parse : String → JSValue
{-# COMPILED_JS JSON-parse JSON.parse #-}
postulate toString : JSValue → String
{-# COMPILED_JS toString function(x) { return x.toString(); } #-}
postulate fromBool : Bool → JSValue
{-# COMPILED_JS fromBool function(x) { return x; } #-}
postulate fromString : String → JSValue
{-# COMPILED_JS fromString function(x) { return x; } #-}
postulate fromChar : Char → JSValue
{-# COMPILED_JS fromChar String #-}
postulate Char▹String : Char → String
{-# COMPILED_JS Char▹String String #-}
postulate fromNumber : Number → JSValue
{-# COMPILED_JS fromNumber function(x) { return x; } #-}
postulate fromJSArray : {A : Set} → JSArray A → JSValue
{-# COMPILED_JS fromJSArray function(ty) { return function(x) { return x; }; } #-}
postulate fromJSObject : JSObject → JSValue
{-# COMPILED_JS fromJSObject function(x) { return x; } #-}
postulate objectFromList : {A : Set}(xs : List A)(fromKey : A → String)(fromVal : A → JSValue) → JSObject
{-# COMPILED_JS objectFromList require("libagda").objectFromList #-}
postulate decodeJSArray : {A B : Set}(arr : JSArray A)(fromElt : Number → A → B) → List B
{-# COMPILED_JS decodeJSArray require("libagda").decodeJSArray #-}
postulate castNumber : JSValue → Number
{-# COMPILED_JS castNumber Number #-}
postulate castString : JSValue → String
{-# COMPILED_JS castString String #-}
-- TODO dyn check of length 1?
postulate castChar : JSValue → Char
{-# COMPILED_JS castChar String #-}
-- TODO dyn check of length 1?
postulate String▹Char : String → Char
{-# COMPILED_JS String▹Char String #-}
-- TODO dyn check?
postulate castJSArray : JSValue → JSArray JSValue
{-# COMPILED_JS castJSArray function(x) { return x; } #-}
-- TODO dyn check?
postulate castJSObject : JSValue → JSObject
{-# COMPILED_JS castJSObject function(x) { return x; } #-}
postulate nullJS : JSValue
{-# COMPILED_JS nullJS null #-}
postulate _·[_] : JSValue → JSValue → JSValue
{-# COMPILED_JS _·[_] require("libagda").readProp #-}
postulate _Array[_] : {A : Set} → JSArray A → Number → A
{-# COMPILED_JS _Array[_] function(ty) { return require("libagda").readProp; } #-}
postulate onJSArray : {A : Set} (f : JSArray JSValue → A) → JSValue → A
{-# COMPILED_JS onJSArray require("libagda").onJSArray #-}
postulate onString : {A : Set} (f : String → A) → JSValue → A
{-# COMPILED_JS onString require("libagda").onString #-}
-- Writes 'msg' and 'inp' to the console and then returns `f inp`
postulate trace : {A B : Set}(msg : String)(inp : A)(f : A → B) → B
{-# COMPILED_JS trace require("libagda").trace #-}
postulate throw : {A : Set} → String → A → A
{-# COMPILED_JS throw require("libagda").throw #-}
data Value : Set₀ where
array : List Value → Value
object : List (String × Value) → Value
string : String → Value
number : Number → Value
bool : Bool → Value
null : Value
Object = List (String × JSValue)
postulate fromValue : Value → JSValue
{-# COMPILED_JS fromValue require("libagda").fromValue #-}
-- TODO we could make it a COMPILED type and remove the encoding by using JSValue as the internal repr.
data ValueView : Set₀ where
array : JSArray JSValue → ValueView
object : JSObject → ValueView
string : String → ValueView
number : Number → ValueView
bool : Bool → ValueView
null : ValueView
-- TODO not yet tested
postulate viewJSValue : JSValue → ValueView
{-# COMPILED_JS viewJSValue require("libagda").viewJSValue #-}
Bool▹String : Bool → String
Bool▹String true = "true"
Bool▹String false = "false"
List▹String : List Char → String
List▹String xs = join "" (fromList xs Char▹String)
String▹List : String → List Char
String▹List s = decodeJSArray (split "" s) (λ _ → String▹Char)
Number▹String : Number → String
Number▹String = castString ∘ fromNumber
JSArray▹ListString : {A : Set} → JSArray A → List A
JSArray▹ListString a = decodeJSArray a (λ _ → id)
fromObject : Object → JSObject
fromObject o = objectFromList o fst snd
_≤Char_ : Char → Char → Bool
x ≤Char y = fromChar x ≤JS fromChar y
_<Char_ : Char → Char → Bool
x <Char y = fromChar x <JS fromChar y
_>Char_ : Char → Char → Bool
x >Char y = fromChar x >JS fromChar y
_≥Char_ : Char → Char → Bool
x ≥Char y = fromChar x ≥JS fromChar y
_≤String_ : String → String → Bool
x ≤String y = fromString x ≤JS fromString y
_<String_ : String → String → Bool
x <String y = fromString x <JS fromString y
_>String_ : String → String → Bool
x >String y = fromString x >JS fromString y
_≥String_ : String → String → Bool
x ≥String y = fromString x ≥JS fromString y
_≤Number_ : Number → Number → Bool
x ≤Number y = fromNumber x ≤JS fromNumber y
_<Number_ : Number → Number → Bool
x <Number y = fromNumber x <JS fromNumber y
_>Number_ : Number → Number → Bool
x >Number y = fromNumber x >JS fromNumber y
_≥Number_ : Number → Number → Bool
x ≥Number y = fromNumber x ≥JS fromNumber y
_·«_» : JSValue → String → JSValue
v ·« s » = v ·[ fromString s ]
_·«_»A : JSValue → String → JSArray JSValue
v ·« s »A = castJSArray (v ·« s »)
trace-call : {A B : Set} → String → (A → B) → A → B
trace-call s f x = trace s (f x) id
postulate JSCmd : Set → Set
Callback1 : Set → Set
Callback1 A = JSCmd ((A → 𝟘) → 𝟘)
Callback0 : Set
Callback0 = Callback1 𝟙
Callback2 : Set → Set → Set
Callback2 A B = JSCmd ((A → B → 𝟘) → 𝟘)
postulate assert : Bool → Callback0
{-# COMPILED_JS assert require("libagda").assert #-}
check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
check true errmsg x = x
check false errmsg x = throw (errmsg _) x
warn-check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
warn-check true errmsg x = x
warn-check false errmsg x = trace ("Warning: " ++ errmsg _) x id
infixr 0 _>>_ _!₁_ _!₂_
data JS! : Set₁ where
end : JS!
_!₁_ : {A : Set}(cmd : Callback1 A)(cb : A → JS!) → JS!
_!₂_ : {A B : Set}(cmd : JSCmd ((A → B → 𝟘) → 𝟘))(cb : A → B → JS!) → JS!
_>>_ : Callback0 → JS! → JS!
cmd >> cont = cmd !₁ λ _ → cont
-- -}
-- -}
-- -}
-- -}
-- -}
|
module FFI.JS where
open import Data.Empty public renaming (⊥ to 𝟘)
open import Data.Unit.Base public renaming (⊤ to 𝟙)
open import Data.Char.Base public using (Char)
open import Data.String.Base public using (String)
open import Data.Bool.Base public using (Bool; true; false)
open import Data.List.Base using (List; []; _∷_)
open import Data.Product using (_×_) renaming (proj₁ to fst; proj₂ to snd)
open import Function using (id; _∘_)
open import Control.Process.Type
{-# COMPILED_JS Bool function (x,v) { return ((x)? v["true"]() : v["false"]()); } #-}
{-# COMPILED_JS true true #-}
{-# COMPILED_JS false false #-}
postulate
Number : Set
JSArray : Set → Set
JSObject : Set
JSValue : Set
postulate readNumber : String → Number
{-# COMPILED_JS readNumber Number #-}
postulate 0N : Number
{-# COMPILED_JS 0N 0 #-}
postulate 1N : Number
{-# COMPILED_JS 1N 1 #-}
postulate 2N : Number
{-# COMPILED_JS 2N 2 #-}
postulate 4N : Number
{-# COMPILED_JS 4N 4 #-}
postulate 8N : Number
{-# COMPILED_JS 8N 8 #-}
postulate 16N : Number
{-# COMPILED_JS 16N 16 #-}
postulate 32N : Number
{-# COMPILED_JS 32N 32 #-}
postulate 64N : Number
{-# COMPILED_JS 64N 64 #-}
postulate 128N : Number
{-# COMPILED_JS 128N 128 #-}
postulate 256N : Number
{-# COMPILED_JS 256N 256 #-}
postulate 512N : Number
{-# COMPILED_JS 512N 512 #-}
postulate 1024N : Number
{-# COMPILED_JS 1024N 1024 #-}
postulate 2048N : Number
{-# COMPILED_JS 2048N 2048 #-}
postulate 4096N : Number
{-# COMPILED_JS 4096N 4096 #-}
postulate _+_ : Number → Number → Number
{-# COMPILED_JS _+_ function(x) { return function(y) { return x + y; }; } #-}
postulate _−_ : Number → Number → Number
{-# COMPILED_JS _−_ function(x) { return function(y) { return x - y; }; } #-}
postulate _*_ : Number → Number → Number
{-# COMPILED_JS _*_ function(x) { return function(y) { return x * y; }; } #-}
postulate _/_ : Number → Number → Number
{-# COMPILED_JS _/_ function(x) { return function(y) { return x / y; }; } #-}
infixr 5 _++_
postulate _++_ : String → String → String
{-# COMPILED_JS _++_ function(x) { return function(y) { return x + y; }; } #-}
postulate _+JS_ : JSValue → JSValue → JSValue
{-# COMPILED_JS _+JS_ function(x) { return function(y) { return x + y; }; } #-}
postulate _≤JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≤JS_ function(x) { return function(y) { return x <= y; }; } #-}
postulate _<JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _<JS_ function(x) { return function(y) { return x < y; }; } #-}
postulate _>JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _>JS_ function(x) { return function(y) { return x > y; }; } #-}
postulate _≥JS_ : JSValue → JSValue → Bool
{-# COMPILED_JS _≥JS_ function(x) { return function(y) { return x >= y; }; } #-}
postulate _===_ : JSValue → JSValue → Bool
{-# COMPILED_JS _===_ function(x) { return function(y) { return x === y; }; } #-}
postulate reverse : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS reverse function(ty) { return function(x) { return x.reverse(); }; } #-}
postulate sort : {A : Set} → JSArray A → JSArray A
{-# COMPILED_JS sort function(ty) { return function(x) { return x.sort(); }; } #-}
postulate split : (sep target : String) → JSArray String
{-# COMPILED_JS split function(sep) { return function(target) { return target.split(sep); }; } #-}
postulate join : (sep : String)(target : JSArray String) → String
{-# COMPILED_JS join function(sep) { return function(target) { return target.join(sep); }; } #-}
postulate fromList : {A B : Set}(xs : List A)(fromElt : A → B) → JSArray B
{-# COMPILED_JS fromList require("libagda").fromList #-}
postulate length : String → Number
{-# COMPILED_JS length function(s) { return s.length; } #-}
postulate JSON-stringify : JSValue → String
{-# COMPILED_JS JSON-stringify JSON.stringify #-}
postulate JSON-parse : String → JSValue
{-# COMPILED_JS JSON-parse JSON.parse #-}
postulate toString : JSValue → String
{-# COMPILED_JS toString function(x) { return x.toString(); } #-}
postulate fromBool : Bool → JSValue
{-# COMPILED_JS fromBool function(x) { return x; } #-}
postulate fromString : String → JSValue
{-# COMPILED_JS fromString function(x) { return x; } #-}
postulate fromChar : Char → JSValue
{-# COMPILED_JS fromChar String #-}
postulate Char▹String : Char → String
{-# COMPILED_JS Char▹String String #-}
postulate fromNumber : Number → JSValue
{-# COMPILED_JS fromNumber function(x) { return x; } #-}
postulate fromJSArray : {A : Set} → JSArray A → JSValue
{-# COMPILED_JS fromJSArray function(ty) { return function(x) { return x; }; } #-}
postulate fromJSObject : JSObject → JSValue
{-# COMPILED_JS fromJSObject function(x) { return x; } #-}
postulate objectFromList : {A : Set}(xs : List A)(fromKey : A → String)(fromVal : A → JSValue) → JSObject
{-# COMPILED_JS objectFromList require("libagda").objectFromList #-}
postulate decodeJSArray : {A B : Set}(arr : JSArray A)(fromElt : Number → A → B) → List B
{-# COMPILED_JS decodeJSArray require("libagda").decodeJSArray #-}
postulate castNumber : JSValue → Number
{-# COMPILED_JS castNumber Number #-}
postulate castString : JSValue → String
{-# COMPILED_JS castString String #-}
-- TODO dyn check of length 1?
postulate castChar : JSValue → Char
{-# COMPILED_JS castChar String #-}
-- TODO dyn check of length 1?
postulate String▹Char : String → Char
{-# COMPILED_JS String▹Char String #-}
-- TODO dyn check?
postulate castJSArray : JSValue → JSArray JSValue
{-# COMPILED_JS castJSArray function(x) { return x; } #-}
-- TODO dyn check?
postulate castJSObject : JSValue → JSObject
{-# COMPILED_JS castJSObject function(x) { return x; } #-}
postulate nullJS : JSValue
{-# COMPILED_JS nullJS null #-}
postulate _·[_] : JSValue → JSValue → JSValue
{-# COMPILED_JS _·[_] require("libagda").readProp #-}
postulate _Array[_] : {A : Set} → JSArray A → Number → A
{-# COMPILED_JS _Array[_] function(ty) { return require("libagda").readProp; } #-}
postulate onJSArray : {A : Set} (f : JSArray JSValue → A) → JSValue → A
{-# COMPILED_JS onJSArray require("libagda").onJSArray #-}
postulate onString : {A : Set} (f : String → A) → JSValue → A
{-# COMPILED_JS onString require("libagda").onString #-}
-- Writes 'msg' and 'inp' to the console and then returns `f inp`
postulate trace : {A B : Set}(msg : String)(inp : A)(f : A → B) → B
{-# COMPILED_JS trace require("libagda").trace #-}
postulate throw : {A : Set} → String → A → A
{-# COMPILED_JS throw require("libagda").throw #-}
postulate is-null : JSValue → Bool
{-# COMPILED_JS is-null function(x) { return (x == null); } #-}
data Value : Set₀ where
array : List Value → Value
object : List (String × Value) → Value
string : String → Value
number : Number → Value
bool : Bool → Value
null : Value
Object = List (String × JSValue)
postulate fromValue : Value → JSValue
{-# COMPILED_JS fromValue require("libagda").fromValue #-}
-- TODO we could make it a COMPILED type and remove the encoding by using JSValue as the internal repr.
data ValueView : Set₀ where
array : JSArray JSValue → ValueView
object : JSObject → ValueView
string : String → ValueView
number : Number → ValueView
bool : Bool → ValueView
null : ValueView
-- TODO not yet tested
postulate viewJSValue : JSValue → ValueView
{-# COMPILED_JS viewJSValue require("libagda").viewJSValue #-}
Bool▹String : Bool → String
Bool▹String true = "true"
Bool▹String false = "false"
List▹String : List Char → String
List▹String xs = join "" (fromList xs Char▹String)
String▹List : String → List Char
String▹List s = decodeJSArray (split "" s) (λ _ → String▹Char)
Number▹String : Number → String
Number▹String = castString ∘ fromNumber
JSArray▹ListString : {A : Set} → JSArray A → List A
JSArray▹ListString a = decodeJSArray a (λ _ → id)
fromObject : Object → JSObject
fromObject o = objectFromList o fst snd
_≤Char_ : Char → Char → Bool
x ≤Char y = fromChar x ≤JS fromChar y
_<Char_ : Char → Char → Bool
x <Char y = fromChar x <JS fromChar y
_>Char_ : Char → Char → Bool
x >Char y = fromChar x >JS fromChar y
_≥Char_ : Char → Char → Bool
x ≥Char y = fromChar x ≥JS fromChar y
_≤String_ : String → String → Bool
x ≤String y = fromString x ≤JS fromString y
_<String_ : String → String → Bool
x <String y = fromString x <JS fromString y
_>String_ : String → String → Bool
x >String y = fromString x >JS fromString y
_≥String_ : String → String → Bool
x ≥String y = fromString x ≥JS fromString y
_≤Number_ : Number → Number → Bool
x ≤Number y = fromNumber x ≤JS fromNumber y
_<Number_ : Number → Number → Bool
x <Number y = fromNumber x <JS fromNumber y
_>Number_ : Number → Number → Bool
x >Number y = fromNumber x >JS fromNumber y
_≥Number_ : Number → Number → Bool
x ≥Number y = fromNumber x ≥JS fromNumber y
_·«_» : JSValue → String → JSValue
v ·« s » = v ·[ fromString s ]
_·«_»A : JSValue → String → JSArray JSValue
v ·« s »A = castJSArray (v ·« s »)
trace-call : {A B : Set} → String → (A → B) → A → B
trace-call s f x = trace s (f x) id
postulate JSCmd : Set → Set
Callback1 : Set → Set
Callback1 A = JSCmd ((A → 𝟘) → 𝟘)
Callback0 : Set
Callback0 = Callback1 𝟙
Callback2 : Set → Set → Set
Callback2 A B = JSCmd ((A → B → 𝟘) → 𝟘)
postulate assert : Bool → Callback0
{-# COMPILED_JS assert require("libagda").assert #-}
check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
check true errmsg x = x
check false errmsg x = throw (errmsg _) x
warn-check : {A : Set}(pred : Bool)(errmsg : 𝟙 → String)(input : A) → A
warn-check true errmsg x = x
warn-check false errmsg x = trace ("Warning: " ++ errmsg _) x id
infixr 0 _>>_ _!₁_ _!₂_
data JS! : Set₁ where
end : JS!
_!₁_ : {A : Set}(cmd : Callback1 A)(cb : A → JS!) → JS!
_!₂_ : {A B : Set}(cmd : JSCmd ((A → B → 𝟘) → 𝟘))(cb : A → B → JS!) → JS!
_>>_ : Callback0 → JS! → JS!
cmd >> cont = cmd !₁ λ _ → cont
-- -}
-- -}
-- -}
-- -}
-- -}
|
add is-null
|
JS: add is-null
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-libjs
|
7b2c31fcd7415cecc448653df3d386001cbebf1b
|
Syntax/Term/Plotkin.agda
|
Syntax/Term/Plotkin.agda
|
import Syntax.Type.Plotkin as Type
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open import Syntax.Context {Type}
data Term
(Γ : Context) :
(τ : Type) → Set
where
const : ∀ {τ} → (c : C τ) → Term Γ τ
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ))
(t : Term Γ σ) → Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ)
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weaken Γ₁≼Γ₂ (const c) = const c
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
|
import Syntax.Type.Plotkin as Type
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open import Syntax.Context {Type}
data Term
(Γ : Context) :
(τ : Type) → Set
where
const : ∀ {τ} →
(c : C τ) →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weaken Γ₁≼Γ₂ (const c) = const c
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
|
Put constructor arguments on different lines.
|
Put constructor arguments on different lines.
Old-commit-hash: 02f9c59c59ddef7050c2c035457c98ecb00464dc
|
Agda
|
mit
|
inc-lc/ilc-agda
|
fbbd955ba2c82259764f220c075ee5ab09f0a3c1
|
models/Desc.agda
|
models/Desc.agda
|
{-# OPTIONS --type-in-type
--no-termination-check
--no-positivity-check #-}
module Desc where
--********************************************
-- Prelude
--********************************************
-- Some preliminary stuffs, to avoid relying on the stdlib
--****************
-- Sigma and friends
--****************
data Sigma (A : Set) (B : A -> Set) : Set where
_,_ : (x : A) (y : B x) -> Sigma A B
_*_ : (A B : Set) -> Set
A * B = Sigma A \_ -> B
fst : {A : Set}{B : A -> Set} -> Sigma A B -> A
fst (a , _) = a
snd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)
snd (a , b) = b
data Zero : Set where
record One : Set where
--****************
-- Sum and friends
--****************
data _+_ (A B : Set) : Set where
l : A -> A + B
r : B -> A + B
--********************************************
-- Desc code
--********************************************
-- Inductive types are implemented as a Universe. Hence, in this
-- section, we implement their code.
-- We can read this code as follow (see Conor's "Ornamental
-- algebras"): a description in |Desc| is a program to read one node
-- of the described tree.
data Desc : Set where
Arg : (X : Set) -> (X -> Desc) -> Desc
-- Read a field in |X|; continue, given its value
-- Often, |X| is an |EnumT|, hence allowing to choose a constructor
-- among a finite set of constructors
Ind : (H : Set) -> Desc -> Desc
-- Read a field in H; read a recursive subnode given the field,
-- continue regarless of the subnode
-- Often, |H| is |1|, hence |Ind| simplifies to |Inf : Desc -> Desc|,
-- meaning: read a recursive subnode and continue regardless
Done : Desc
-- Stop reading
--********************************************
-- Desc decoder
--********************************************
-- Provided the type of the recursive subnodes |R|, we decode a
-- description as a record describing the node.
[|_|]_ : Desc -> Set -> Set
[| Arg A D |] R = Sigma A (\ a -> [| D a |] R)
[| Ind H D |] R = (H -> R) * [| D |] R
[| Done |] R = One
--********************************************
-- Functions on codes
--********************************************
-- Saying that a "predicate" |p| holds everywhere in |v| amounts to
-- write something of the following type:
Everywhere : (d : Desc) (D : Set) (bp : D -> Set) (V : [| d |] D) -> Set
Everywhere (Arg A f) d p v = Everywhere (f (fst v)) d p (snd v)
-- It must hold for this constructor
Everywhere (Ind H x) d p v = ((y : H) -> p (fst v y)) * Everywhere x d p (snd v)
-- It must hold for the subtrees
Everywhere Done _ _ _ = _
-- It trivially holds at endpoints
-- Then, we can build terms that inhabits this type. That is, a
-- function that takes a "predicate" |bp| and makes it hold everywhere
-- in the data-structure. It is the equivalent of a "map", but in a
-- dependently-typed setting.
everywhere : (d : Desc) (D : Set) (bp : D -> Set) ->
((y : D) -> bp y) -> (v : [| d |] D) ->
Everywhere d D bp v
everywhere (Arg a f) d bp p v = everywhere (f (fst v)) d bp p (snd v)
-- It holds everywhere on this constructor
everywhere (Ind H x) d bp p v = (\y -> p (fst v y)) , everywhere x d bp p (snd v)
-- It holds here, and down in the recursive subtrees
everywhere Done _ _ _ _ = One
-- Nothing needs to be done on endpoints
-- Looking at the decoder, a natural thing to do is to define its
-- fixpoint |Mu D|, hence instantiating |R| with |Mu D| itself.
data Mu (D : Desc) : Set where
Con : [| D |] (Mu D) -> Mu D
-- Using the "map" defined by |everywhere|, we can implement a "fold"
-- over the |Mu| fixpoint:
foldDesc : (D : Desc) (bp : Mu D -> Set) ->
((x : [| D |] (Mu D)) -> Everywhere D (Mu D) bp x -> bp (Con x)) ->
(v : Mu D) ->
bp v
foldDesc D bp p (Con v) = p v (everywhere D (Mu D) bp (\x -> foldDesc D bp p x) v)
--********************************************
-- Nat
--********************************************
data NatConst : Set where
ZE : NatConst
SU : NatConst
natc : NatConst -> Desc
natc ZE = Done
natc SU = Ind One Done
natd : Desc
natd = Arg NatConst natc
nat : Set
nat = Mu natd
zero : nat
zero = Con ( ZE , _ )
suc : nat -> nat
suc n = Con ( SU , ( (\_ -> n) , _ ) )
two : nat
two = suc (suc zero)
four : nat
four = suc (suc (suc (suc zero)))
sum : nat ->
((x : Sigma NatConst (\ a -> [| natc a |] Mu (Arg NatConst natc))) ->
Everywhere (natc (fst x)) (Mu (Arg NatConst natc)) (\ _ -> Mu (Arg NatConst natc)) (snd x) ->
Mu (Arg NatConst natc))
sum n2 (ZE , _) p = n2
sum n2 (SU , f) p = suc ( fst p _)
plus : nat -> nat -> nat
plus n1 n2 = foldDesc natd (\_ -> nat) (sum n2) n1
x : nat
x = plus two two
|
{-# OPTIONS --type-in-type #-}
module Desc where
--********************************************
-- Prelude
--********************************************
-- Some preliminary stuffs, to avoid relying on the stdlib
--****************
-- Sigma and friends
--****************
data Sigma (A : Set) (B : A -> Set) : Set where
_,_ : (x : A) (y : B x) -> Sigma A B
_*_ : (A : Set)(B : Set) -> Set
A * B = Sigma A \_ -> B
fst : {A : Set}{B : A -> Set} -> Sigma A B -> A
fst (a , _) = a
snd : {A : Set}{B : A -> Set} (p : Sigma A B) -> B (fst p)
snd (a , b) = b
data Zero : Set where
data Unit : Set where
Void : Unit
--****************
-- Sum and friends
--****************
data _+_ (A : Set)(B : Set) : Set where
l : A -> A + B
r : B -> A + B
--****************
-- Equality
--****************
data _==_ {A : Set}(x : A) : A -> Set where
refl : x == x
cong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y
cong f refl = refl
cong2 : {A B C : Set}(f : A -> B -> C){x y : A}{z t : B} ->
x == y -> z == t -> f x z == f y t
cong2 f refl refl = refl
postulate
reflFun : {A B : Set}(f : A -> B)(g : A -> B)-> ((a : A) -> f a == g a) -> f == g
--********************************************
-- Desc code
--********************************************
data Desc : Set where
id : Desc
const : Set -> Desc
prod : Desc -> Desc -> Desc
sigma : (S : Set) -> (S -> Desc) -> Desc
pi : (S : Set) -> (S -> Desc) -> Desc
--********************************************
-- Desc interpretation
--********************************************
[|_|]_ : Desc -> Set -> Set
[| id |] Z = Z
[| const X |] Z = X
[| prod D D' |] Z = [| D |] Z * [| D' |] Z
[| sigma S T |] Z = Sigma S (\s -> [| T s |] Z)
[| pi S T |] Z = (s : S) -> [| T s |] Z
--********************************************
-- Fixpoint construction
--********************************************
data Mu (D : Desc) : Set where
con : [| D |] (Mu D) -> Mu D
--********************************************
-- Predicate: All
--********************************************
All : (D : Desc)(X : Set)(P : X -> Set) -> [| D |] X -> Set
All id X P x = P x
All (const Z) X P x = Z
All (prod D D') X P (d , d') = (All D X P d) * (All D' X P d')
All (sigma S T) X P (a , b) = All (T a) X P b
All (pi S T) X P f = (s : S) -> All (T s) X P (f s)
all : (D : Desc)(X : Set)(P : X -> Set)(R : (x : X) -> P x)(x : [| D |] X) -> All D X P x
all id X P R x = R x
all (const Z) X P R z = z
all (prod D D') X P R (d , d') = all D X P R d , all D' X P R d'
all (sigma S T) X P R (a , b) = all (T a) X P R b
all (pi S T) X P R f = \ s -> all (T s) X P R (f s)
--********************************************
-- Elimination principle: induction
--********************************************
{-
induction : (D : Desc)
(P : Mu D -> Set) ->
( (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x)) ->
(v : Mu D) ->
P v
induction D P ms (con xs) = ms xs (all D (Mu D) P (\x -> induction D P ms x) xs)
-}
module Elim (D : Desc)
(P : Mu D -> Set)
(ms : (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x))
where
mutual
induction : (x : Mu D) -> P x
induction (con xs) = ms xs (hyps D xs)
hyps : (D' : Desc)
(xs : [| D' |] (Mu D)) ->
All D' (Mu D) P xs
hyps id x = induction x
hyps (const Z) z = z
hyps (prod D D') (d , d') = hyps D d , hyps D' d'
hyps (sigma S T) (a , b) = hyps (T a) b
hyps (pi S T) f = \s -> hyps (T s) (f s)
induction : (D : Desc)
(P : Mu D -> Set) ->
( (x : [| D |] (Mu D)) ->
All D (Mu D) P x -> P (con x)) ->
(v : Mu D) ->
P v
induction D P ms x = Elim.induction D P ms x
--********************************************
-- Examples
--********************************************
--****************
-- Nat
--****************
data NatConst : Set where
Ze : NatConst
Suc : NatConst
natCases : NatConst -> Desc
natCases Ze = const Unit
natCases Suc = id
NatD : Desc
NatD = sigma NatConst natCases
Nat : Set
Nat = Mu NatD
ze : Nat
ze = con (Ze , Void)
suc : Nat -> Nat
suc n = con (Suc , n)
--****************
-- List
--****************
data ListConst : Set where
Nil : ListConst
Cons : ListConst
listCases : Set -> ListConst -> Desc
listCases X Nil = const Unit
listCases X Cons = sigma X (\_ -> id)
ListD : Set -> Desc
ListD X = sigma ListConst (listCases X)
List : Set -> Set
List X = Mu (ListD X)
nil : {X : Set} -> List X
nil = con ( Nil , Void )
cons : {X : Set} -> X -> List X -> List X
cons x t = con ( Cons , ( x , t ))
--****************
-- Tree
--****************
data TreeConst : Set where
Leaf : TreeConst
Node : TreeConst
treeCases : Set -> TreeConst -> Desc
treeCases X Leaf = const Unit
treeCases X Node = sigma X (\_ -> prod id id)
TreeD : Set -> Desc
TreeD X = sigma TreeConst (treeCases X)
Tree : Set -> Set
Tree X = Mu (TreeD X)
leaf : {X : Set} -> Tree X
leaf = con (Leaf , Void)
node : {X : Set} -> X -> Tree X -> Tree X -> Tree X
node x le ri = con (Node , (x , (le , ri)))
--********************************************
-- Finite sets
--********************************************
EnumU : Set
EnumU = Nat
nilE : EnumU
nilE = ze
consE : EnumU -> EnumU
consE e = suc e
{-
data EnumU : Set where
nilE : EnumU
consE : EnumU -> EnumU
-}
data EnumT : (e : EnumU) -> Set where
EZe : {e : EnumU} -> EnumT (consE e)
ESu : {e : EnumU} -> EnumT e -> EnumT (consE e)
casesSpi : (xs : [| NatD |] Nat) ->
All NatD Nat (\e -> (P' : EnumT e -> Set) -> Set) xs ->
(P' : EnumT (con xs) -> Set) -> Set
casesSpi (Ze , Void) hs P' = Unit
casesSpi (Suc , n) hs P' = P' EZe * hs (\e -> P' (ESu e))
spi : (e : EnumU)(P : EnumT e -> Set) -> Set
spi e P = induction NatD (\e -> (P : EnumT e -> Set) -> Set) casesSpi e P
{-
spi : (e : EnumU)(P : EnumT e -> Set) -> Set
spi nilE P = Unit
spi (consE e) P = P EZe * spi e (\e -> P (ESu e))
-}
casesSwitch : (xs : [| NatD |] Nat) ->
All NatD Nat (\e -> (P' : EnumT e -> Set)(b' : spi e P')(x' : EnumT e) -> P' x') xs ->
(P' : EnumT (con xs) -> Set)(b' : spi (con xs) P')(x' : EnumT (con xs)) -> P' x'
casesSwitch (Ze , Void) hs P' b' ()
casesSwitch (Suc , n) hs P' b' EZe = fst b'
casesSwitch (Suc , n) hs P' b' (ESu e') = hs (\e -> P' (ESu e)) (snd b') e'
switch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x
switch e P b x = induction NatD (\e -> (P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x) casesSwitch e P b x
{-
switch : (e : EnumU)(P : EnumT e -> Set)(b : spi e P)(x : EnumT e) -> P x
switch nilE P b ()
switch (consE e) P b EZe = fst b
switch (consE e) P b (ESu n) = switch e (\e -> P (ESu e)) (snd b) n
-}
--********************************************
-- Tagged description
--********************************************
TagDesc : Set
TagDesc = Sigma EnumU (\e -> spi e (\_ -> Desc))
toDesc : TagDesc -> Desc
toDesc (B , F) = sigma (EnumT B) (\e -> switch B (\_ -> Desc) F e)
--********************************************
-- Catamorphism
--********************************************
cata : (D : Desc)
(T : Set) ->
([| D |] T -> T) ->
(Mu D) -> T
cata D T phi x = induction D (\_ -> T) (\x ms -> phi (replace D T x ms )) x
where replace : (D' : Desc)(T : Set)(xs : [| D' |] (Mu D))(ms : All D' (Mu D) (\_ -> T) xs) -> [| D' |] T
replace id T x y = y
replace (const Z) T z z' = z'
replace (prod D D') T (x , x') (y , y') = replace D T x y , replace D' T x' y'
replace (sigma A B) T (a , b) t = a , replace (B a) T b t
replace (pi A B) T f t = \s -> replace (B s) T (f s) (t s)
--********************************************
-- Free monad construction
--********************************************
_**_ : TagDesc -> (X : Set) -> TagDesc
(e , D) ** X = consE e , (const X , D)
--********************************************
-- Substitution
--********************************************
apply : (D : TagDesc)(X Y : Set) ->
(X -> Mu (toDesc (D ** Y))) ->
[| toDesc (D ** X) |] (Mu (toDesc (D ** Y))) ->
Mu (toDesc (D ** Y))
apply (E , B) X Y sig (EZe , x) = sig x
apply (E , B) X Y sig (ESu n , t) = con (ESu n , t)
subst : (D : TagDesc)(X Y : Set) ->
Mu (toDesc (D ** X)) ->
(X -> Mu (toDesc (D ** Y))) ->
Mu (toDesc (D ** Y))
subst D X Y x sig = cata (toDesc (D ** X)) (Mu (toDesc (D ** Y))) (apply D X Y sig) x
|
Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.
|
Update Desc model. Follow ICFP paper. With type-in-type, but termination-checker-friendly.
|
Agda
|
mit
|
mietek/epigram2,larrytheliquid/pigit,mietek/epigram2
|
bb08669e1bdc2f7d2df9bb9289bda1b42f3dece8
|
lib/Algebra/FunctionProperties/Eq.agda
|
lib/Algebra/FunctionProperties/Eq.agda
|
{-# OPTIONS --without-K #-}
-- Like Algebra.FunctionProperties but specialized to _≡_ and using implict arguments
-- Moreover there is some extensions such as:
-- * interchange
-- * non-zero inversions
-- * cancelation and non-zero cancelation
-- These properties can (for instance) be used to define algebraic
-- structures.
open import Level
open import Function using (flip)
open import Data.Product
open import Relation.Nullary
open import Relation.Binary.NP
open import Relation.Binary.PropositionalEquality.NP
-- The properties are specified using the following relation as
-- "equality".
module Algebra.FunctionProperties.Eq {a} {A : Set a} where
------------------------------------------------------------------------
-- Unary and binary operations
open import Algebra.FunctionProperties.Core public
------------------------------------------------------------------------
-- Properties of operations
Associative : Op₂ A → Set _
Associative _·_ = ∀ {x y z} → ((x · y) · z) ≡ (x · (y · z))
Commutative : Op₂ A → Set _
Commutative _·_ = ∀ {x y} → (x · y) ≡ (y · x)
LeftIdentity : A → Op₂ A → Set _
LeftIdentity e _·_ = ∀ {x} → (e · x) ≡ x
RightIdentity : A → Op₂ A → Set _
RightIdentity e _·_ = ∀ {x} → (x · e) ≡ x
Identity : A → Op₂ A → Set _
Identity e · = LeftIdentity e · × RightIdentity e ·
LeftZero : A → Op₂ A → Set _
LeftZero z _·_ = ∀ {x} → (z · x) ≡ z
RightZero : A → Op₂ A → Set _
RightZero z _·_ = ∀ {x} → (x · z) ≡ z
Zero : A → Op₂ A → Set _
Zero z · = LeftZero z · × RightZero z ·
LeftInverse : A → Op₁ A → Op₂ A → Set _
LeftInverse e _⁻¹ _·_ = ∀ {x} → (x ⁻¹ · x) ≡ e
LeftInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
LeftInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x ⁻¹ · x) ≡ e
RightInverse : A → Op₁ A → Op₂ A → Set _
RightInverse e _⁻¹ _·_ = ∀ {x} → (x · (x ⁻¹)) ≡ e
RightInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
RightInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x · (x ⁻¹)) ≡ e
Inverse : A → Op₁ A → Op₂ A → Set _
Inverse e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
InverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
InverseNonZero zero e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
_DistributesOverˡ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverˡ _+_ =
∀ {x y z} → (x * (y + z)) ≡ ((x * y) + (x * z))
_DistributesOverʳ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverʳ _+_ =
∀ {x y z} → ((y + z) * x) ≡ ((y * x) + (z * x))
_DistributesOver_ : Op₂ A → Op₂ A → Set _
* DistributesOver + = (* DistributesOverˡ +) × (* DistributesOverʳ +)
_IdempotentOn_ : Op₂ A → A → Set _
_·_ IdempotentOn x = (x · x) ≡ x
Idempotent : Op₂ A → Set _
Idempotent · = ∀ {x} → · IdempotentOn x
IdempotentFun : Op₁ A → Set _
IdempotentFun f = ∀ {x} → f (f x) ≡ f x
_Absorbs_ : Op₂ A → Op₂ A → Set _
_·_ Absorbs _∘_ = ∀ {x y} → (x · (x ∘ y)) ≡ x
Absorptive : Op₂ A → Op₂ A → Set _
Absorptive · ∘ = (· Absorbs ∘) × (∘ Absorbs ·)
Involutive : Op₁ A → Set _
Involutive f = ∀ {x} → f (f x) ≡ x
Interchange : Op₂ A → Op₂ A → Set _
Interchange _·_ _∘_ = ∀ {x y z t} → ((x · y) ∘ (z · t)) ≡ ((x ∘ z) · (y ∘ t))
LeftCancel : Op₂ A → Set _
LeftCancel _·_ = ∀ {c x y} → c · x ≡ c · y → x ≡ y
RightCancel : Op₂ A → Set _
RightCancel _·_ = ∀ {c x y} → x · c ≡ y · c → x ≡ y
LeftCancelNonZero : A → Op₂ A → Set _
LeftCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → c · x ≡ c · y → x ≡ y
RightCancelNonZero : A → Op₂ A → Set _
RightCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → x · c ≡ y · c → x ≡ y
module InterchangeFromAssocComm
(_·_ : Op₂ A)
(·-assoc : Associative _·_)
(·-comm : Commutative _·_)
where
open ≡-Reasoning
·= : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → (x · y) ≡ (x' · y')
·= refl refl = refl
·-interchange : Interchange _·_ _·_
·-interchange {x} {y} {z} {t}
= (x · y) · (z · t)
≡⟨ ·-assoc ⟩
x · (y · (z · t))
≡⟨ ·= refl (! ·-assoc) ⟩
x · ((y · z) · t)
≡⟨ ·= refl (·= ·-comm refl) ⟩
x · ((z · y) · t)
≡⟨ ·= refl ·-assoc ⟩
x · (z · (y · t))
≡⟨ ! ·-assoc ⟩
(x · z) · (y · t)
∎
module _ {b} {B : Set b} (f : A → B) where
Injective : Set (b ⊔ a)
Injective = ∀ {x y} → f x ≡ f y → x ≡ y
Conflict : Set (b ⊔ a)
Conflict = ∃ λ x → ∃ λ y → (x ≢ y) × f x ≡ f y
module _ {b} {B : Set b} {f : A → B} where
Injective-¬Conflict : Injective f → ¬ (Conflict f)
Injective-¬Conflict inj (x , y , x≢y , fx≡fy) = x≢y (inj fx≡fy)
Conflict-¬Injective : Conflict f → ¬ (Injective f)
Conflict-¬Injective = flip Injective-¬Conflict
-- -}
-- -}
-- -}
-- -}
|
{-# OPTIONS --without-K #-}
-- Like Algebra.FunctionProperties but specialized to _≡_ and using implict arguments
-- Moreover there is some extensions such as:
-- * interchange
-- * non-zero inversions
-- * cancelation and non-zero cancelation
-- These properties can (for instance) be used to define algebraic
-- structures.
open import Level
open import Function.NP using (_$⟨_⟩_; flip)
open import Data.Nat.Base using (ℕ)
open import Data.Product
open import Relation.Nullary
open import Relation.Binary.NP
open import Relation.Binary.PropositionalEquality.NP
-- The properties are specified using the following relation as
-- "equality".
module Algebra.FunctionProperties.Eq {a} {A : Set a} where
------------------------------------------------------------------------
-- Unary and binary operations
open import Algebra.FunctionProperties.Core public
------------------------------------------------------------------------
-- Properties of operations
Associative : Op₂ A → Set _
Associative _·_ = ∀ {x y z} → ((x · y) · z) ≡ (x · (y · z))
Commutative : Op₂ A → Set _
Commutative _·_ = ∀ {x y} → (x · y) ≡ (y · x)
LeftIdentity : A → Op₂ A → Set _
LeftIdentity e _·_ = ∀ {x} → (e · x) ≡ x
RightIdentity : A → Op₂ A → Set _
RightIdentity e _·_ = ∀ {x} → (x · e) ≡ x
Identity : A → Op₂ A → Set _
Identity e · = LeftIdentity e · × RightIdentity e ·
LeftZero : A → Op₂ A → Set _
LeftZero z _·_ = ∀ {x} → (z · x) ≡ z
RightZero : A → Op₂ A → Set _
RightZero z _·_ = ∀ {x} → (x · z) ≡ z
Zero : A → Op₂ A → Set _
Zero z · = LeftZero z · × RightZero z ·
LeftInverse : A → Op₁ A → Op₂ A → Set _
LeftInverse e _⁻¹ _·_ = ∀ {x} → (x ⁻¹ · x) ≡ e
LeftInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
LeftInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x ⁻¹ · x) ≡ e
RightInverse : A → Op₁ A → Op₂ A → Set _
RightInverse e _⁻¹ _·_ = ∀ {x} → (x · (x ⁻¹)) ≡ e
RightInverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
RightInverseNonZero zero e _⁻¹ _·_ = ∀ {x} → x ≢ zero → (x · (x ⁻¹)) ≡ e
Inverse : A → Op₁ A → Op₂ A → Set _
Inverse e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
InverseNonZero : (zero e : A) → Op₁ A → Op₂ A → Set _
InverseNonZero zero e ⁻¹ · = LeftInverse e ⁻¹ · × RightInverse e ⁻¹ ·
_DistributesOverˡ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverˡ _+_ =
∀ {x y z} → (x * (y + z)) ≡ ((x * y) + (x * z))
_DistributesOverʳ_ : Op₂ A → Op₂ A → Set _
_*_ DistributesOverʳ _+_ =
∀ {x y z} → ((y + z) * x) ≡ ((y * x) + (z * x))
_DistributesOver_ : Op₂ A → Op₂ A → Set _
* DistributesOver + = (* DistributesOverˡ +) × (* DistributesOverʳ +)
_IdempotentOn_ : Op₂ A → A → Set _
_·_ IdempotentOn x = (x · x) ≡ x
Idempotent : Op₂ A → Set _
Idempotent · = ∀ {x} → · IdempotentOn x
IdempotentFun : Op₁ A → Set _
IdempotentFun f = ∀ {x} → f (f x) ≡ f x
_Absorbs_ : Op₂ A → Op₂ A → Set _
_·_ Absorbs _∘_ = ∀ {x y} → (x · (x ∘ y)) ≡ x
Absorptive : Op₂ A → Op₂ A → Set _
Absorptive · ∘ = (· Absorbs ∘) × (∘ Absorbs ·)
Involutive : Op₁ A → Set _
Involutive f = ∀ {x} → f (f x) ≡ x
Interchange : Op₂ A → Op₂ A → Set _
Interchange _·_ _∘_ = ∀ {x y z t} → ((x · y) ∘ (z · t)) ≡ ((x ∘ z) · (y ∘ t))
LeftCancel : Op₂ A → Set _
LeftCancel _·_ = ∀ {c x y} → c · x ≡ c · y → x ≡ y
RightCancel : Op₂ A → Set _
RightCancel _·_ = ∀ {c x y} → x · c ≡ y · c → x ≡ y
LeftCancelNonZero : A → Op₂ A → Set _
LeftCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → c · x ≡ c · y → x ≡ y
RightCancelNonZero : A → Op₂ A → Set _
RightCancelNonZero zero _·_ = ∀ {c x y} → c ≢ zero → x · c ≡ y · c → x ≡ y
module InterchangeFromAssocComm
(_·_ : Op₂ A)
(·-assoc : Associative _·_)
(·-comm : Commutative _·_)
where
open ≡-Reasoning
·= : ∀ {x x' y y'} → x ≡ x' → y ≡ y' → (x · y) ≡ (x' · y')
·= refl refl = refl
·-interchange : Interchange _·_ _·_
·-interchange {x} {y} {z} {t}
= (x · y) · (z · t)
≡⟨ ·-assoc ⟩
x · (y · (z · t))
≡⟨ ·= refl (! ·-assoc) ⟩
x · ((y · z) · t)
≡⟨ ·= refl (·= ·-comm refl) ⟩
x · ((z · y) · t)
≡⟨ ·= refl ·-assoc ⟩
x · (z · (y · t))
≡⟨ ! ·-assoc ⟩
(x · z) · (y · t)
∎
module _ {b} {B : Set b} (f : A → B) where
Injective : Set (b ⊔ a)
Injective = ∀ {x y} → f x ≡ f y → x ≡ y
Conflict : Set (b ⊔ a)
Conflict = ∃ λ x → ∃ λ y → (x ≢ y) × f x ≡ f y
module _ {b} {B : Set b} {f : A → B} where
Injective-¬Conflict : Injective f → ¬ (Conflict f)
Injective-¬Conflict inj (x , y , x≢y , fx≡fy) = x≢y (inj fx≡fy)
Conflict-¬Injective : Conflict f → ¬ (Injective f)
Conflict-¬Injective = flip Injective-¬Conflict
module Endo {f : A → A} where
Cycle^ : ℕ → Set _
Cycle^ n = ∃ λ x → f $⟨ n ⟩ x ≡ x
Cycle = ∃ Cycle^
-- -}
-- -}
-- -}
-- -}
|
add Endo.Cycle
|
Alegbra.FunctionProperties.Eq: add Endo.Cycle
|
Agda
|
bsd-3-clause
|
crypto-agda/agda-nplib
|
675459709ff3563cde300754c35f229c6b963f10
|
Parametric/Change/Validity.agda
|
Parametric/Change/Validity.agda
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
ΔVal-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → ΔVal-base ι → Set
apply-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ΔVal-base ι → ⟦ ι ⟧Base
diff-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → ΔVal-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-ΔVal-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-ΔVal-base ι v (diff-ΔVal-base ι u v) ≡ u
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
apply-ΔVal : ∀ τ → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
diff-ΔVal : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
infixl 6 apply-ΔVal diff-ΔVal -- as with + - in GHC.Num
syntax apply-ΔVal τ v dv = v ⊞₍ τ ₎ dv
syntax diff-ΔVal τ u v = u ⊟₍ τ ₎ v
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (ΔVal τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (ΔVal (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- ΔVal : Type → Set
ΔVal (base ι) = ΔVal-base ι
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
n ⊞₍ base ι ₎ Δn = apply-ΔVal-base ι n Δn
f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
m ⊟₍ base ι ₎ n = diff-ΔVal-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (R[v,Δv] : valid v Δv)
→ valid {τ} (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v}
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞₍ σ ₎ Δw
in
R[v,u-v] {τ}
,
(begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ΔEnv-Entry : Type → Set
ΔEnv-Entry τ = Triple
⟦ τ ⟧
(λ _ → ΔVal τ)
(λ v dv → valid {τ} v dv)
ΔEnv : Context → Set
ΔEnv = DependentList ΔEnv-Entry
ignore-entry : ΔEnv-Entry ⊆ ⟦_⟧
ignore-entry (cons v _ _) = v
update-entry : ΔEnv-Entry ⊆ ⟦_⟧
update-entry {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv
ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
ignore = map (λ {τ} → ignore-entry {τ})
update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
update = map (λ {τ} → update-entry {τ})
|
import Parametric.Syntax.Type as Type
import Parametric.Denotation.Value as Value
module Parametric.Change.Validity
{Base : Type.Structure}
(⟦_⟧Base : Value.Structure Base)
where
open Type.Structure Base
open Value.Structure Base ⟦_⟧Base
-- Changes for Calculus Popl14
--
-- Contents
-- - Mutually recursive concepts: ΔVal, validity.
-- Under module Syntax, the corresponding concepts of
-- ΔType and ΔContext reside in separate files.
-- Now they have to be together due to mutual recursiveness.
-- - `diff` and `apply` on semantic values of changes:
-- they have to be here as well because they are mutually
-- recursive with validity.
-- - The lemma diff-is-valid: it has to be here because it is
-- mutually recursive with `apply`
-- - The lemma apply-diff: it is mutually recursive with `apply`
-- and `diff`
open import Base.Denotation.Notation public
open import Relation.Binary.PropositionalEquality
open import Postulate.Extensionality
open import Data.Product hiding (map)
import Structure.Tuples as Tuples
open Tuples
import Base.Data.DependentList as DependentList
open DependentList
open import Relation.Unary using (_⊆_)
record Structure : Set₁ where
----------------
-- Parameters --
----------------
field
ΔVal-base : Base → Set
valid-base : ∀ {ι} → ⟦ ι ⟧Base → ΔVal-base ι → Set
apply-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ΔVal-base ι → ⟦ ι ⟧Base
diff-ΔVal-base : ∀ ι → ⟦ ι ⟧Base → ⟦ ι ⟧Base → ΔVal-base ι
R[v,u-v]-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → valid-base {ι} v (diff-ΔVal-base ι u v)
v+[u-v]=u-base : ∀ {ι} {u v : ⟦ ι ⟧Base} → apply-ΔVal-base ι v (diff-ΔVal-base ι u v) ≡ u
---------------
-- Interface --
---------------
ΔVal : Type → Set
valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
apply-ΔVal : ∀ τ → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
diff-ΔVal : ∀ τ → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
infixl 6 apply-ΔVal diff-ΔVal -- as with + - in GHC.Num
syntax apply-ΔVal τ v dv = v ⊞₍ τ ₎ dv
syntax diff-ΔVal τ u v = u ⊟₍ τ ₎ v
-- Lemma diff-is-valid
R[v,u-v] : ∀ {τ : Type} {u v : ⟦ τ ⟧} → valid {τ} v (u ⊟₍ τ ₎ v)
-- Lemma apply-diff
v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} →
v ⊞₍ τ ₎ (u ⊟₍ τ ₎ v) ≡ u
--------------------
-- Implementation --
--------------------
-- (ΔVal τ) is the set of changes of type τ. This set is
-- strictly smaller than ⟦ ΔType τ⟧ if τ is a function type. In
-- particular, (ΔVal (σ ⇒ τ)) is a function that accepts only
-- valid changes, while ⟦ ΔType (σ ⇒ τ) ⟧ accepts also invalid
-- changes.
--
-- ΔVal τ is the target of the denotational specification ⟦_⟧Δ.
-- Detailed motivation:
--
-- https://github.com/ps-mr/ilc/blob/184a6291ac6eef80871c32d2483e3e62578baf06/POPL14/paper/sec-formal.tex
-- ΔVal : Type → Set
ΔVal (base ι) = ΔVal-base ι
ΔVal (σ ⇒ τ) = (v : ⟦ σ ⟧) → (dv : ΔVal σ) → valid v dv → ΔVal τ
-- _⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
n ⊞₍ base ι ₎ Δn = apply-ΔVal-base ι n Δn
f ⊞₍ σ ⇒ τ ₎ Δf = λ v → f v ⊞₍ τ ₎ Δf v (v ⊟₍ σ ₎ v) (R[v,u-v] {σ})
-- _⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
m ⊟₍ base ι ₎ n = diff-ΔVal-base ι m n
g ⊟₍ σ ⇒ τ ₎ f = λ v Δv R[v,Δv] → g (v ⊞₍ σ ₎ Δv) ⊟₍ τ ₎ f v
-- valid : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → Set
valid {base ι} n Δn = valid-base {ι} n Δn
valid {σ ⇒ τ} f Δf =
∀ (v : ⟦ σ ⟧) (Δv : ΔVal σ) (R[v,Δv] : valid v Δv)
→ valid {τ} (f v) (Δf v Δv R[v,Δv])
-- × (f ⊞ Δf) (v ⊞ Δv) ≡ f v ⊞ Δf v Δv R[v,Δv]
× (f ⊞₍ σ ⇒ τ ₎ Δf) (v ⊞₍ σ ₎ Δv) ≡ f v ⊞₍ τ ₎ Δf v Δv R[v,Δv]
-- v+[u-v]=u : ∀ {τ : Type} {u v : ⟦ τ ⟧} → v ⊞ (u ⊟ v) ≡ u
v+[u-v]=u {base ι} {u} {v} = v+[u-v]=u-base {ι} {u} {v}
v+[u-v]=u {σ ⇒ τ} {u} {v} =
ext {-⟦ σ ⟧} {λ _ → ⟦ τ ⟧-} (λ w →
begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w
≡⟨ refl ⟩
v w ⊞₍ τ ₎ (u (w ⊞₍ σ ₎ (w ⊟₍ σ ₎ w)) ⊟₍ τ ₎ v w)
≡⟨ cong (λ hole → v w ⊞₍ τ ₎ (u hole ⊟₍ τ ₎ v w)) (v+[u-v]=u {σ}) ⟩
v w ⊞₍ τ ₎ (u w ⊟₍ τ ₎ v w)
≡⟨ v+[u-v]=u {τ} ⟩
u w
∎) where
open ≡-Reasoning
R[v,u-v] {base ι} {u} {v} = R[v,u-v]-base {ι} {u} {v}
R[v,u-v] {σ ⇒ τ} {u} {v} = λ w Δw R[w,Δw] →
let
w′ = w ⊞₍ σ ₎ Δw
in
R[v,u-v] {τ}
,
(begin
(v ⊞₍ σ ⇒ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v)) w′
≡⟨ cong (λ hole → hole w′) (v+[u-v]=u {σ ⇒ τ} {u} {v}) ⟩
u w′
≡⟨ sym (v+[u-v]=u {τ} {u w′} {v w}) ⟩
v w ⊞₍ τ ₎ (u ⊟₍ σ ⇒ τ ₎ v) w Δw R[w,Δw]
∎) where open ≡-Reasoning
-- syntactic sugar for implicit indices
infixl 6 _⊞_ _⊟_ -- as with + - in GHC.Num
_⊞_ : ∀ {τ} → ⟦ τ ⟧ → ΔVal τ → ⟦ τ ⟧
_⊞_ {τ} v dv = v ⊞₍ τ ₎ dv
_⊟_ : ∀ {τ} → ⟦ τ ⟧ → ⟦ τ ⟧ → ΔVal τ
_⊟_ {τ} u v = u ⊟₍ τ ₎ v
------------------
-- Environments --
------------------
open DependentList public using (∅; _•_)
open Tuples public using (cons)
ValidChange : Type → Set
ValidChange τ = Triple
⟦ τ ⟧
(λ _ → ΔVal τ)
(λ v dv → valid {τ} v dv)
ΔEnv : Context → Set
ΔEnv = DependentList ValidChange
ignore-valid-change : ValidChange ⊆ ⟦_⟧
ignore-valid-change (cons v _ _) = v
update-valid-change : ValidChange ⊆ ⟦_⟧
update-valid-change {τ} (cons v dv R[v,dv]) = v ⊞₍ τ ₎ dv
ignore : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
ignore = map (λ {τ} → ignore-valid-change {τ})
update : ∀ {Γ : Context} → (ρ : ΔEnv Γ) → ⟦ Γ ⟧
update = map (λ {τ} → update-valid-change {τ})
|
Rename ΔEnv-Entry to ValidChange.
|
Rename ΔEnv-Entry to ValidChange.
Also rename operations related to ΔEnv-Entry.
Old-commit-hash: 35ab981b89bab6eced9a7b20753bb140f6caf7a8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
0e5b51a126b354cb6a2c34c080deb1fdcc75c7cb
|
Denotational/Environments.agda
|
Denotational/Environments.agda
|
module Denotational.Environments
(Type : Set)
(⟦_⟧Type : Type → Set)
where
-- ENVIRONMENTS
--
-- This module defines the meaning of contexts, that is,
-- the type of environments that fit a context, together
-- with operations and properties of these operations.
--
-- This module is parametric in the syntax and semantics
-- of types, so it can be reused for different calculi
-- and models.
open import Relation.Binary.PropositionalEquality
open import Syntactic.Contexts Type
open import Denotational.Notation
private
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- TYPING CONTEXTS
-- Denotational Semantics : Contexts Represent Environments
data Empty : Set where
∅ : Empty
data Bind A B : Set where
_•_ : (v : A) (ρ : B) → Bind A B
⟦_⟧Context : Context → Set
⟦ ∅ ⟧Context = Empty
⟦ τ • Γ ⟧Context = Bind ⟦ τ ⟧ ⟦ Γ ⟧Context
meaningOfContext : Meaning Context
meaningOfContext = meaning ⟦_⟧Context
-- VARIABLES
-- Denotational Semantics
⟦_⟧Var : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ this ⟧Var (v • ρ) = v
⟦ that x ⟧Var (v • ρ) = ⟦ x ⟧Var ρ
meaningOfVar : ∀ {Γ τ} → Meaning (Var Γ τ)
meaningOfVar = meaning ⟦_⟧Var
-- WEAKENING
-- Remove a variable from an environment
⟦_⟧≼ : ∀ {Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) → ⟦ Γ₂ ⟧ → ⟦ Γ₁ ⟧
⟦ ∅ ⟧≼ ∅ = ∅
⟦ keep τ • Γ′ ⟧≼ (v • ρ) = v • ⟦ Γ′ ⟧≼ ρ
⟦ drop τ • Γ′ ⟧≼ (v • ρ) = ⟦ Γ′ ⟧≼ ρ
meaningOf≼ : ∀ {Γ₁ Γ₂} → Meaning (Γ₁ ≼ Γ₂)
meaningOf≼ = meaning ⟦_⟧≼
-- Properties
⟦⟧-≼-trans : ∀ {Γ₃ Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) (Γ″ : Γ₂ ≼ Γ₃) →
∀ (ρ : ⟦ Γ₃ ⟧) → ⟦ ≼-trans Γ′ Γ″ ⟧ ρ ≡ ⟦ Γ′ ⟧ (⟦ Γ″ ⟧ ρ)
⟦⟧-≼-trans Γ′ ∅ ∅ = refl
⟦⟧-≼-trans (keep τ • Γ′) (keep .τ • Γ″) (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-trans Γ′ Γ″ ρ)
⟦⟧-≼-trans (drop τ • Γ′) (keep .τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-trans Γ′ (drop τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-refl : ∀ {Γ : Context} →
∀ (ρ : ⟦ Γ ⟧) → ⟦ ≼-refl ⟧ ρ ≡ ρ
⟦⟧-≼-refl {∅} ∅ = refl
⟦⟧-≼-refl {τ • Γ} (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-refl ρ)
-- SOUNDNESS of variable lifting
lift-sound : ∀ {Γ₁ Γ₂ τ} (Γ′ : Γ₁ ≼ Γ₂) (x : Var Γ₁ τ) →
∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ lift Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (⟦ Γ′ ⟧ ρ)
lift-sound ∅ () ρ
lift-sound (keep τ • Γ′) this (v • ρ) = refl
lift-sound (keep τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ x ρ
lift-sound (drop τ • Γ′) this (v • ρ) = lift-sound Γ′ this ρ
lift-sound (drop τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ (that x) ρ
|
module Denotational.Environments
(Type : Set)
{ℓ}
(⟦_⟧Type : Type → Set ℓ)
where
-- ENVIRONMENTS
--
-- This module defines the meaning of contexts, that is,
-- the type of environments that fit a context, together
-- with operations and properties of these operations.
--
-- This module is parametric in the syntax and semantics
-- of types, so it can be reused for different calculi
-- and models.
open import Relation.Binary.PropositionalEquality
open import Syntactic.Contexts Type
open import Denotational.Notation
private
meaningOfType : Meaning Type
meaningOfType = meaning ⟦_⟧Type
-- TYPING CONTEXTS
-- Denotational Semantics : Contexts Represent Environments
data Empty : Set ℓ where
∅ : Empty
data Bind A B : Set ℓ where
_•_ : (v : A) (ρ : B) → Bind A B
⟦_⟧Context : Context → Set ℓ
⟦ ∅ ⟧Context = Empty
⟦ τ • Γ ⟧Context = Bind ⟦ τ ⟧ ⟦ Γ ⟧Context
meaningOfContext : Meaning Context
meaningOfContext = meaning ⟦_⟧Context
-- VARIABLES
-- Denotational Semantics
⟦_⟧Var : ∀ {Γ τ} → Var Γ τ → ⟦ Γ ⟧ → ⟦ τ ⟧
⟦ this ⟧Var (v • ρ) = v
⟦ that x ⟧Var (v • ρ) = ⟦ x ⟧Var ρ
meaningOfVar : ∀ {Γ τ} → Meaning (Var Γ τ)
meaningOfVar = meaning ⟦_⟧Var
-- WEAKENING
-- Remove a variable from an environment
⟦_⟧≼ : ∀ {Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) → ⟦ Γ₂ ⟧ → ⟦ Γ₁ ⟧
⟦ ∅ ⟧≼ ∅ = ∅
⟦ keep τ • Γ′ ⟧≼ (v • ρ) = v • ⟦ Γ′ ⟧≼ ρ
⟦ drop τ • Γ′ ⟧≼ (v • ρ) = ⟦ Γ′ ⟧≼ ρ
meaningOf≼ : ∀ {Γ₁ Γ₂} → Meaning (Γ₁ ≼ Γ₂)
meaningOf≼ = meaning ⟦_⟧≼
-- Properties
⟦⟧-≼-trans : ∀ {Γ₃ Γ₁ Γ₂} → (Γ′ : Γ₁ ≼ Γ₂) (Γ″ : Γ₂ ≼ Γ₃) →
∀ (ρ : ⟦ Γ₃ ⟧) → ⟦ ≼-trans Γ′ Γ″ ⟧ ρ ≡ ⟦ Γ′ ⟧ (⟦ Γ″ ⟧ ρ)
⟦⟧-≼-trans Γ′ ∅ ∅ = refl
⟦⟧-≼-trans (keep τ • Γ′) (keep .τ • Γ″) (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-trans Γ′ Γ″ ρ)
⟦⟧-≼-trans (drop τ • Γ′) (keep .τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-trans Γ′ (drop τ • Γ″) (v • ρ) = ⟦⟧-≼-trans Γ′ Γ″ ρ
⟦⟧-≼-refl : ∀ {Γ : Context} →
∀ (ρ : ⟦ Γ ⟧) → ⟦ ≼-refl ⟧ ρ ≡ ρ
⟦⟧-≼-refl {∅} ∅ = refl
⟦⟧-≼-refl {τ • Γ} (v • ρ) = cong₂ _•_ refl (⟦⟧-≼-refl ρ)
-- SOUNDNESS of variable lifting
lift-sound : ∀ {Γ₁ Γ₂ τ} (Γ′ : Γ₁ ≼ Γ₂) (x : Var Γ₁ τ) →
∀ (ρ : ⟦ Γ₂ ⟧) → ⟦ lift Γ′ x ⟧ ρ ≡ ⟦ x ⟧ (⟦ Γ′ ⟧ ρ)
lift-sound ∅ () ρ
lift-sound (keep τ • Γ′) this (v • ρ) = refl
lift-sound (keep τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ x ρ
lift-sound (drop τ • Γ′) this (v • ρ) = lift-sound Γ′ this ρ
lift-sound (drop τ • Γ′) (that x) (v • ρ) = lift-sound Γ′ (that x) ρ
|
Generalize environments to values in higher universes.
|
Generalize environments to values in higher universes.
Old-commit-hash: 4ff4102742306da9be77151d785aa608f8558977
|
Agda
|
mit
|
inc-lc/ilc-agda
|
0ab0b0012d2db6e26ec2716c2d95f3743111df4d
|
Parametric/Syntax/MType.agda
|
Parametric/Syntax/MType.agda
|
import Parametric.Syntax.Type as Type
module Parametric.Syntax.MType where
module Structure (Base : Type.Structure) where
open Type.Structure Base
mutual
-- Derived from CBPV
data ValType : Set where
U : (c : CompType) → ValType
B : (ι : Base) → ValType
vUnit : ValType
_v×_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
_v+_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
data CompType : Set where
F : ValType → CompType
_⇛_ : ValType → CompType → CompType
-- We did not use this in CBPV, so dropped.
-- _Π_ : CompType → CompType → CompType
cbnToCompType : Type → CompType
cbnToCompType (base ι) = F (B ι)
cbnToCompType (σ ⇒ τ) = U (cbnToCompType σ) ⇛ cbnToCompType τ
cbvToValType : Type → ValType
cbvToValType (base ι) = B ι
cbvToValType (σ ⇒ τ) = U (cbvToValType σ ⇛ F (cbvToValType τ))
open import Base.Syntax.Context ValType public
using ()
renaming
( ∅ to ∅∅
; _•_ to _••_
; mapContext to mapValCtx
; Var to ValVar
; Context to ValContext
; this to vThis; that to vThat)
cbnToValType : Type → ValType
cbnToValType τ = U (cbnToCompType τ)
cbvToCompType : Type → CompType
cbvToCompType τ = F (cbvToValType τ)
fromCBNCtx : Context → ValContext
fromCBNCtx Γ = mapValCtx cbnToValType Γ
fromCBVCtx : Context → ValContext
fromCBVCtx Γ = mapValCtx cbvToValType Γ
open import Data.List
open Data.List using (List) public
fromCBVToCompList : Context → List CompType
fromCBVToCompList Γ = mapValCtx cbvToCompType Γ
fromVar : ∀ {Γ τ} → (f : Type → ValType) → Var Γ τ → ValVar (mapValCtx f Γ) (f τ)
fromVar {x • Γ} f this = vThis
fromVar {x • Γ} f (that v) = vThat (fromVar f v)
|
import Parametric.Syntax.Type as Type
module Parametric.Syntax.MType where
module Structure (Base : Type.Structure) where
open Type.Structure Base
mutual
-- Derived from CBPV
data ValType : Set where
U : (c : CompType) → ValType
B : (ι : Base) → ValType
vUnit : ValType
_v×_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
_v+_ : (τ₁ : ValType) → (τ₂ : ValType) → ValType
-- Same associativity as the standard _×_
infixr 2 _v×_
data CompType : Set where
F : ValType → CompType
_⇛_ : ValType → CompType → CompType
-- We did not use this in CBPV, so dropped.
-- _Π_ : CompType → CompType → CompType
cbnToCompType : Type → CompType
cbnToCompType (base ι) = F (B ι)
cbnToCompType (σ ⇒ τ) = U (cbnToCompType σ) ⇛ cbnToCompType τ
cbvToValType : Type → ValType
cbvToValType (base ι) = B ι
cbvToValType (σ ⇒ τ) = U (cbvToValType σ ⇛ F (cbvToValType τ))
open import Base.Syntax.Context ValType public
using ()
renaming
( ∅ to ∅∅
; _•_ to _••_
; mapContext to mapValCtx
; Var to ValVar
; Context to ValContext
; this to vThis; that to vThat)
cbnToValType : Type → ValType
cbnToValType τ = U (cbnToCompType τ)
cbvToCompType : Type → CompType
cbvToCompType τ = F (cbvToValType τ)
fromCBNCtx : Context → ValContext
fromCBNCtx Γ = mapValCtx cbnToValType Γ
fromCBVCtx : Context → ValContext
fromCBVCtx Γ = mapValCtx cbvToValType Γ
open import Data.List
open Data.List using (List) public
fromCBVToCompList : Context → List CompType
fromCBVToCompList Γ = mapValCtx cbvToCompType Γ
fromVar : ∀ {Γ τ} → (f : Type → ValType) → Var Γ τ → ValVar (mapValCtx f Γ) (f τ)
fromVar {x • Γ} f this = vThis
fromVar {x • Γ} f (that v) = vThat (fromVar f v)
|
Fix arity of _v×_
|
MType: Fix arity of _v×_
|
Agda
|
mit
|
inc-lc/ilc-agda
|
dd73b5a36235448b400cd9d02d7983b1155617bc
|
Syntax/Term/Plotkin.agda
|
Syntax/Term/Plotkin.agda
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Denotation.Environment Type
open import Syntax.Context.Plotkin B
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
TermConstructor : (Γ Σ : Context) (τ : Type) → Set
TermConstructor Γ ∅ τ′ = Term Γ τ′
TermConstructor Γ (τ • Σ) τ′ = Term Γ τ → TermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → TermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → TermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const constant)
|
import Syntax.Type.Plotkin as Type
import Syntax.Context as Context
module Syntax.Term.Plotkin
{B : Set {- of base types -}}
{C : Context.Context {Type.Type B} → Type.Type B → Set {- of constants -}}
where
-- Terms of languages described in Plotkin style
open import Function using (_∘_)
open import Data.Product
open Type B
open Context {Type}
open import Denotation.Environment Type
open import Syntax.Context.Plotkin B
data Term
(Γ : Context) :
(τ : Type) → Set
data Terms
(Γ : Context) :
(Σ : Context) → Set
data Term Γ where
const : ∀ {Σ τ} →
(c : C Σ τ) →
Terms Γ Σ →
Term Γ τ
var : ∀ {τ} →
(x : Var Γ τ) →
Term Γ τ
app : ∀ {σ τ}
(s : Term Γ (σ ⇒ τ)) →
(t : Term Γ σ) →
Term Γ τ
abs : ∀ {σ τ}
(t : Term (σ • Γ) τ) →
Term Γ (σ ⇒ τ)
data Terms Γ where
∅ : Terms Γ ∅
_•_ : ∀ {τ Σ} →
Term Γ τ →
Terms Γ Σ →
Terms Γ (τ • Σ)
infixr 9 _•_
-- g ⊝ f = λ x . λ Δx . g (x ⊕ Δx) ⊝ f x
-- f ⊕ Δf = λ x . f x ⊕ Δf x (x ⊝ x)
lift-diff-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} →
term Γ (τ ⇒ τ ⇒ Δtype τ) × term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-diff-apply diff apply {base ι} = diff , apply
lift-diff-apply diff apply {σ ⇒ τ} =
let
-- for diff
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
-- for apply
Δh = var (that (that this))
h = var (that this)
y = var this
-- syntactic sugars
diffσ = λ {Γ} → proj₁ (lift-diff-apply diff apply {σ} {Γ})
diffτ = λ {Γ} → proj₁ (lift-diff-apply diff apply {τ} {Γ})
applyσ = λ {Γ} → proj₂ (lift-diff-apply diff apply {σ} {Γ})
applyτ = λ {Γ} → proj₂ (lift-diff-apply diff apply {τ} {Γ})
_⊝σ_ = λ s t → app (app diffσ s) t
_⊝τ_ = λ s t → app (app diffτ s) t
_⊕σ_ = λ t Δt → app (app applyσ Δt) t
_⊕τ_ = λ t Δt → app (app applyτ Δt) t
in
abs (abs (abs (abs (app f (x ⊕σ Δx) ⊝τ app g x))))
,
abs (abs (abs (app h y ⊕τ app (app Δh y) (y ⊝σ y))))
lift-diff : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (τ ⇒ τ ⇒ Δtype τ)
lift-diff diff apply = λ {τ Γ} →
proj₁ (lift-diff-apply diff apply {τ} {Γ})
lift-apply : ∀ {Δbase : B → Type} →
let
Δtype = lift-Δtype Δbase
term = Term
in
(∀ {ι Γ} → term Γ (base ι ⇒ base ι ⇒ Δtype (base ι))) →
(∀ {ι Γ} → term Γ (Δtype (base ι) ⇒ base ι ⇒ base ι)) →
∀ {τ Γ} → term Γ (Δtype τ ⇒ τ ⇒ τ)
lift-apply diff apply = λ {τ Γ} →
proj₂ (lift-diff-apply diff apply {τ} {Γ})
-- Weakening
weaken : ∀ {Γ₁ Γ₂ τ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Term Γ₁ τ →
Term Γ₂ τ
weakenAll : ∀ {Γ₁ Γ₂ Σ} →
(Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) →
Terms Γ₁ Σ →
Terms Γ₂ Σ
weaken Γ₁≼Γ₂ (const c ts) = const c (weakenAll Γ₁≼Γ₂ ts)
weaken Γ₁≼Γ₂ (var x) = var (lift Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {σ} t) = abs (weaken (keep σ • Γ₁≼Γ₂) t)
weakenAll Γ₁≼Γ₂ ∅ = ∅
weakenAll Γ₁≼Γ₂ (t • ts) = weaken Γ₁≼Γ₂ t • weakenAll Γ₁≼Γ₂ ts
-- Specialized weakening
weaken₁ : ∀ {Γ σ τ} →
Term Γ τ → Term (σ • Γ) τ
weaken₁ t = weaken (drop _ • ≼-refl) t
weaken₂ : ∀ {Γ α β τ} →
Term Γ τ → Term (α • β • Γ) τ
weaken₂ t = weaken (drop _ • drop _ • ≼-refl) t
weaken₃ : ∀ {Γ α β γ τ} →
Term Γ τ → Term (α • β • γ • Γ) τ
weaken₃ t = weaken (drop _ • drop _ • drop _ • ≼-refl) t
-- Shorthands for nested applications
app₂ : ∀ {Γ α β γ} →
Term Γ (α ⇒ β ⇒ γ) →
Term Γ α → Term Γ β → Term Γ γ
app₂ f x = app (app f x)
app₃ : ∀ {Γ α β γ δ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ
app₃ f x = app₂ (app f x)
app₄ : ∀ {Γ α β γ δ ε} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε
app₄ f x = app₃ (app f x)
app₅ : ∀ {Γ α β γ δ ε ζ} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ
app₅ f x = app₄ (app f x)
app₆ : ∀ {Γ α β γ δ ε ζ η} →
Term Γ (α ⇒ β ⇒ γ ⇒ δ ⇒ ε ⇒ ζ ⇒ η) →
Term Γ α → Term Γ β → Term Γ γ → Term Γ δ →
Term Γ ε → Term Γ ζ → Term Γ η
app₆ f x = app₅ (app f x)
TermConstructor : (Γ Σ : Context) (τ : Type) → Set
TermConstructor Γ ∅ τ′ = Term Γ τ′
TermConstructor Γ (τ • Σ) τ′ = Term Γ τ → TermConstructor Γ Σ τ′
-- helper for lift-η-const, don't try to understand at home
lift-η-const-rec : ∀ {Σ Γ τ} → (Terms Γ Σ → Term Γ τ) → TermConstructor Γ Σ τ
lift-η-const-rec {∅} k = k ∅
lift-η-const-rec {τ • Σ} k = λ t → lift-η-const-rec (λ ts → k (t • ts))
lift-η-const : ∀ {Σ τ} → C Σ τ → ∀ {Γ} → TermConstructor Γ Σ τ
lift-η-const constant = lift-η-const-rec (const constant)
|
Reorder definitions of Term and Terms.
|
Reorder definitions of Term and Terms.
The order of definitions should follow the order of declaration,
and this code seems easier to understand when one looks at Term
first.
Old-commit-hash: 517547b8eebe1b8803d9f30fa3945f02685b6cc5
|
Agda
|
mit
|
inc-lc/ilc-agda
|
8d03eb08a3d8dc237fc96cbdf430c73571ead581
|
Base/Change/Equivalence.agda
|
Base/Change/Equivalence.agda
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
open import Base.Change.Equivalence.Base public
open import Base.Change.Equivalence.EqReasoning public
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ (dx : Δ {{ca}} x) → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil {{ca}} x)
≡⟨ update-nil {{ca}} x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ (dx : Δ {{ca}} x) → x ⊞ dx ≡ x → _≙_ {{ca}} dx (nil {{ca}} x)
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil {{ca}} x) ⟩
x ⊞ nil {{ca}} x
∎
where
open ≡-Reasoning
-- Let's show that nil x is d.o.e. to x ⊟ x
nil-x-is-x⊟x : nil x ≙ x ⊟ x
nil-x-is-x⊟x = ≙-sym (⊞-unit-is-nil (x ⊟ x) (update-diff {{ca}} x x))
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by
-- equiv-fun-changes-respect, and its corollaries fun-change-respects and
-- equiv-fun-changes-funs.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx : Δ {{ca}} x} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : _⊞_ {{ca}} x (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff {{ca}} (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization; DerivativeAsChange)
open FC.FunctionChange
equiv-fun-changes-respect : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
_≙_ {{changeAlgebra}} df₁ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
equiv-fun-changes-respect {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} (df : Δ f) →
dx₁ ≙ dx₂ → apply df x dx₁ ≙ apply df x dx₂
fun-change-respects df dx₁≙dx₂ = equiv-fun-changes-respect (≙-refl {dx = df}) dx₁≙dx₂
-- D.o.e. function changes behave like the same function (up to d.o.e.).
equiv-fun-changes-funs : ∀ {x : A} {dx : Δ x} {f : A → B} {df₁ df₂ : Δ f} →
_≙_ {{changeAlgebra}} df₁ df₂ → apply df₁ x dx ≙ apply df₂ x dx
equiv-fun-changes-funs {dx = dx} df₁≙df₂ = equiv-fun-changes-respect df₁≙df₂ (≙-refl {dx = dx})
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- You could think that the function should relate equivalent changes, but
-- that's a stronger hypothesis, which doesn't give you extra guarantees. But
-- here's the statement and proof, for completeness:
delta-ext₂ : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx₁ dx₂ → _≙_ {{CA}} dx₁ dx₂ → apply df x dx₁ ≙ apply dg x dx₂) → df ≙ dg
delta-ext₂ {f} {df} {dg} df-x-dx≙dg-x-dx = delta-ext (λ x dx → df-x-dx≙dg-x-dx x dx dx ≙-refl)
-- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> IsDerivative f (apply df).
-- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
IsDerivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil {{CA}} x))
≡⟨ ext (λ x → cong f (update-nil {{CA}} x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
IsDerivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
derivative-is-nil-alternative : ∀ {f : A → B} df →
(IsDerivative-f-df : IsDerivative f df) → DerivativeAsChange IsDerivative-f-df ≙ nil f
derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → IsDerivative f (apply df) → IsDerivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- This is Lemma 2.5 in the paper. Note that the statement in the paper uses
-- (incorrectly) normal equality instead of delta-observational equivalence.
deriv-zero :
∀ (f : A → B) df → IsDerivative f df →
∀ v → df v (nil {{CA}} v) ≙ nil {{CB}} (f v)
deriv-zero f df proof v = doe lemma
where
open ≡-Reasoning
lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil {{CB}} (f v)
lemma =
begin
f v ⊞ df v (nil {{CA}} v)
≡⟨ proof v (nil {{CA}} v) ⟩
f (v ⊞ (nil {{CA}} v))
≡⟨ cong f (update-nil {{CA}} v) ⟩
f v
≡⟨ sym (update-nil {{CB}} (f v)) ⟩
f v ⊞ nil {{CB}} (f v)
∎
|
------------------------------------------------------------------------
-- INCREMENTAL λ-CALCULUS
--
-- Delta-observational equivalence
------------------------------------------------------------------------
module Base.Change.Equivalence where
open import Relation.Binary.PropositionalEquality
open import Base.Change.Algebra
open import Level
open import Data.Unit
open import Function
open import Base.Change.Equivalence.Base public
open import Base.Change.Equivalence.EqReasoning public
module _ {a ℓ} {A : Set a} {{ca : ChangeAlgebra ℓ A}} {x : A} where
-- By update-nil, if dx = nil x, then x ⊞ dx ≡ x.
-- As a consequence, if dx ≙ nil x, then x ⊞ dx ≡ x
nil-is-⊞-unit : ∀ (dx : Δ {{ca}} x) → dx ≙ nil x → x ⊞ dx ≡ x
nil-is-⊞-unit dx dx≙nil-x =
begin
x ⊞ dx
≡⟨ proof dx≙nil-x ⟩
x ⊞ (nil {{ca}} x)
≡⟨ update-nil {{ca}} x ⟩
x
∎
where
open ≡-Reasoning
-- Here we prove the inverse:
⊞-unit-is-nil : ∀ (dx : Δ {{ca}} x) → x ⊞ dx ≡ x → _≙_ {{ca}} dx (nil {{ca}} x)
⊞-unit-is-nil dx x⊞dx≡x = doe $
begin
x ⊞ dx
≡⟨ x⊞dx≡x ⟩
x
≡⟨ sym (update-nil {{ca}} x) ⟩
x ⊞ nil {{ca}} x
∎
where
open ≡-Reasoning
-- Let's show that nil x is d.o.e. to x ⊟ x
nil-x-is-x⊟x : nil x ≙ x ⊟ x
nil-x-is-x⊟x = ≙-sym (⊞-unit-is-nil (x ⊟ x) (update-diff {{ca}} x x))
-- TODO: we want to show that all functions of interest respect
-- delta-observational equivalence, so that two d.o.e. changes can be
-- substituted for each other freely.
--
-- * That should be be true for
-- functions using changes parametrically.
--
-- * Moreover, d.o.e. should be respected by contexts [ ] x dx and df x [ ];
-- this is proved below on both contexts at once by
-- equiv-fun-changes-respect, and its corollaries fun-change-respects and
-- equiv-fun-changes-funs.
--
-- * Finally, change algebra operations should respect d.o.e. But ⊞ respects
-- it by definition, and ⊟ doesn't take change arguments - we will only
-- need a proof for compose, when we define it.
--
-- Stating the general result, though, seems hard, we should
-- rather have lemmas proving that certain classes of functions respect this
-- equivalence.
-- This results pairs with update-diff.
diff-update : ∀ {dx : Δ {{ca}} x} → (x ⊞ dx) ⊟ x ≙ dx
diff-update {dx} = doe lemma
where
lemma : _⊞_ {{ca}} x (x ⊞ dx ⊟ x) ≡ x ⊞ dx
lemma = update-diff {{ca}} (x ⊞ dx) x
module _ {a} {b} {c} {d} {A : Set a} {B : Set b}
{{CA : ChangeAlgebra c A}} {{CB : ChangeAlgebra d B}} where
module FC = FunctionChanges A B {{CA}} {{CB}}
open FC using (changeAlgebra; incrementalization; DerivativeAsChange)
open FC.FunctionChange
equiv-fun-changes-respect : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} {df₁ df₂} →
_≙_ {{changeAlgebra}} df₁ df₂ → dx₁ ≙ dx₂ → apply df₁ x dx₁ ≙ apply df₂ x dx₂
equiv-fun-changes-respect {x} {dx₁} {dx₂} {f} {df₁} {df₂} df₁≙df₂ dx₁≙dx₂ = doe lemma
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma : f x ⊞ apply df₁ x dx₁ ≡ f x ⊞ apply df₂ x dx₂
-- Informally: use incrementalization on both sides and then apply
-- congruence.
lemma =
begin
f x ⊞ apply df₁ x dx₁
≡⟨ sym (incrementalization f df₁ x dx₁) ⟩
(f ⊞ df₁) (x ⊞ dx₁)
≡⟨ ≙-cong (f ⊞ df₁) dx₁≙dx₂ ⟩
(f ⊞ df₁) (x ⊞ dx₂)
≡⟨ ≙-cong (λ f → f (x ⊞ dx₂)) df₁≙df₂ ⟩
(f ⊞ df₂) (x ⊞ dx₂)
≡⟨ incrementalization f df₂ x dx₂ ⟩
f x ⊞ apply df₂ x dx₂
∎
fun-change-respects : ∀ {x : A} {dx₁ dx₂ : Δ x} {f : A → B} (df : Δ f) →
dx₁ ≙ dx₂ → apply df x dx₁ ≙ apply df x dx₂
fun-change-respects df dx₁≙dx₂ = equiv-fun-changes-respect (≙-refl {dx = df}) dx₁≙dx₂
-- D.o.e. function changes behave like the same function (up to d.o.e.).
equiv-fun-changes-funs : ∀ {x : A} {dx : Δ x} {f : A → B} {df₁ df₂ : Δ f} →
_≙_ {{changeAlgebra}} df₁ df₂ → apply df₁ x dx ≙ apply df₂ x dx
equiv-fun-changes-funs {dx = dx} df₁≙df₂ = equiv-fun-changes-respect df₁≙df₂ (≙-refl {dx = dx})
derivative-doe-characterization : ∀ {a : A} {da : Δ a}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
_≙_ {{CB}} (df a da) (f (a ⊞ da) ⊟ f a)
derivative-doe-characterization {a} {da} {f} {df} is-derivative = doe lemma
where
open ≡-Reasoning
lemma : f a ⊞ df a da ≡ f a ⊞ (f (a ⊞ da) ⊟ f a)
lemma =
begin
(f a ⊞ df a da)
≡⟨ is-derivative a da ⟩
(f (a ⊞ da))
≡⟨ sym (update-diff (f (a ⊞ da)) (f a)) ⟩
(f a ⊞ (f (a ⊞ da) ⊟ f a))
∎
derivative-respects-doe : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
_≙_ {{CA}} dx₁ dx₂ → _≙_ {{CB}} (df x dx₁) (df x dx₂)
derivative-respects-doe {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
begin
df x dx₁
≙⟨ derivative-doe-characterization is-derivative ⟩
f (x ⊞ dx₁) ⊟ f x
≡⟨ cong (λ v → f v ⊟ f x) (proof dx₁≙dx₂) ⟩
f (x ⊞ dx₂) ⊟ f x
≙⟨ ≙-sym (derivative-doe-characterization is-derivative) ⟩
df x dx₂
∎
where
open ≙-Reasoning
-- This is also a corollary of fun-changes-respect
derivative-respects-doe-alt : ∀ {x : A} {dx₁ dx₂ : Δ x}
{f : A → B} {df : RawChange f} (is-derivative : IsDerivative f df) →
_≙_ {{CA}} dx₁ dx₂ → _≙_ {{CB}} (df x dx₁) (df x dx₂)
derivative-respects-doe-alt {x} {dx₁} {dx₂} {f} {df} is-derivative dx₁≙dx₂ =
fun-change-respects (DerivativeAsChange is-derivative) dx₁≙dx₂
open import Postulate.Extensionality
-- An extensionality principle for delta-observational equivalence: if
-- applying two function changes to the same base value and input change gives
-- a d.o.e. result, then the two function changes are d.o.e. themselves.
delta-ext : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx → apply df x dx ≙ apply dg x dx) → df ≙ dg
delta-ext {f} {df} {dg} df-x-dx≙dg-x-dx = doe lemma₂
where
open ≡-Reasoning
-- This type signature just expands the goal manually a bit.
lemma₁ : ∀ x dx → f x ⊞ apply df x dx ≡ f x ⊞ apply dg x dx
lemma₁ x dx = proof $ df-x-dx≙dg-x-dx x dx
lemma₂ : f ⊞ df ≡ f ⊞ dg
lemma₂ = ext (λ x → lemma₁ x (nil x))
-- You could think that the function should relate equivalent changes, but
-- that's a stronger hypothesis, which doesn't give you extra guarantees. But
-- here's the statement and proof, for completeness:
delta-ext₂ : ∀ {f : A → B} → ∀ {df dg : Δ f} → (∀ x dx₁ dx₂ → _≙_ {{CA}} dx₁ dx₂ → apply df x dx₁ ≙ apply dg x dx₂) → df ≙ dg
delta-ext₂ {f} {df} {dg} df-x-dx≙dg-x-dx = delta-ext (λ x dx → df-x-dx≙dg-x-dx x dx dx ≙-refl)
-- We know that IsDerivative f (apply (nil f)) (by nil-is-derivative).
-- That is, df = nil f -> IsDerivative f (apply df).
-- Now, we try to prove that if IsDerivative f (apply df) -> df = nil f.
-- But first, we prove that f ⊞ df = f.
derivative-is-⊞-unit : ∀ {f : A → B} df →
IsDerivative f (apply df) → f ⊞ df ≡ f
derivative-is-⊞-unit {f} df fdf =
begin
f ⊞ df
≡⟨⟩
(λ x → f x ⊞ apply df x (nil x))
≡⟨ ext (λ x → fdf x (nil x)) ⟩
(λ x → f (x ⊞ nil {{CA}} x))
≡⟨ ext (λ x → cong f (update-nil {{CA}} x)) ⟩
(λ x → f x)
≡⟨⟩
f
∎
where
open ≡-Reasoning
-- We can restate the above as "df is a nil change".
derivative-is-nil : ∀ {f : A → B} df →
IsDerivative f (apply df) → df ≙ nil f
derivative-is-nil df fdf = ⊞-unit-is-nil df (derivative-is-⊞-unit df fdf)
derivative-is-nil-alternative : ∀ {f : A → B} df →
(IsDerivative-f-df : IsDerivative f df) → DerivativeAsChange IsDerivative-f-df ≙ nil f
derivative-is-nil-alternative df IsDerivative-f-df = derivative-is-nil (DerivativeAsChange IsDerivative-f-df) IsDerivative-f-df
-- If we have two derivatives, they're both nil, hence they're equal.
derivative-unique : ∀ {f : A → B} {df dg : Δ f} → IsDerivative f (apply df) → IsDerivative f (apply dg) → df ≙ dg
derivative-unique {f} {df} {dg} fdf fdg =
begin
df
≙⟨ derivative-is-nil df fdf ⟩
nil f
≙⟨ ≙-sym (derivative-is-nil dg fdg) ⟩
dg
∎
where
open ≙-Reasoning
-- This is Lemma 2.5 in the paper. Note that the statement in the paper uses
-- (incorrectly) normal equality instead of delta-observational equivalence.
deriv-zero :
∀ (f : A → B) df → IsDerivative f df →
∀ v → df v (nil {{CA}} v) ≙ nil {{CB}} (f v)
deriv-zero f df proof v = doe lemma
where
open ≡-Reasoning
lemma : f v ⊞ df v (nil v) ≡ f v ⊞ nil {{CB}} (f v)
lemma =
begin
f v ⊞ df v (nil {{CA}} v)
≡⟨ proof v (nil {{CA}} v) ⟩
f (v ⊞ (nil {{CA}} v))
≡⟨ cong f (update-nil {{CA}} v) ⟩
f v
≡⟨ sym (update-nil {{CB}} (f v)) ⟩
f v ⊞ nil {{CB}} (f v)
∎
|
Add new lemmas on change equivalence
|
Add new lemmas on change equivalence
|
Agda
|
mit
|
inc-lc/ilc-agda
|
ca0490d3e647551767333fe1b8af760d98b01dfa
|
Syntax/Term/Popl14.agda
|
Syntax/Term/Popl14.agda
|
module Syntax.Term.Popl14 where
-- Terms Calculus Popl14
--
-- Contents
-- - Term constructors
-- - Weakening on terms
-- - `fit`: weaken a term to its ΔContext
-- - diff-term, apply-term and their syntactic sugars
open import Syntax.Context.Popl14 public
open import Data.Integer
data Term (Γ : Context) : Type -> Set where
int : (n : ℤ) → Term Γ int
add : (s : Term Γ int) (t : Term Γ int) → Term Γ int
minus : (t : Term Γ int) → Term Γ int
empty : Term Γ bag
insert : (s : Term Γ int) (t : Term Γ bag) → Term Γ bag
union : (s : Term Γ bag) → (t : Term Γ bag) → Term Γ bag
negate : (t : Term Γ bag) → Term Γ bag
flatmap : (s : Term Γ (int ⇒ bag)) (t : Term Γ bag) → Term Γ bag
sum : (t : Term Γ bag) → Term Γ int
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ} → (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) → Term Γ τ
abs : ∀ {σ τ} → (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ)
weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ
weaken Γ₁≼Γ₂ (int x) = int x
weaken Γ₁≼Γ₂ (add s t) = add (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (minus t) = minus (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ empty = empty
weaken Γ₁≼Γ₂ (insert s t) = insert (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (union s t) = union (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (negate t) = negate (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (flatmap s t) = flatmap (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (sum t) = sum (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (var x) = var (weakenVar Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {τ} t) = abs (weaken (keep τ • Γ₁≼Γ₂) t)
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
apply-term {int} =
let Δx = var (that this)
x = var this
in abs (abs (add x Δx))
apply-term {bag} =
let Δx = var (that this)
x = var this
in abs (abs (union x Δx))
apply-term {σ ⇒ τ} =
let
Δf = var (that (that this))
f = var (that this)
x = var this
in
-- Δf f x
abs (abs (abs
(app (app apply-term
(app (app Δf x) (app (app diff-term x) x)))
(app f x))))
diff-term {int} =
let x = var (that this)
y = var this
in abs (abs (add x (minus y)))
diff-term {bag} =
let x = var (that this)
y = var this
in abs (abs (union x (negate y)))
diff-term {σ ⇒ τ} =
let
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
in
-- g f x Δx
abs (abs (abs (abs
(app (app diff-term
(app g (app (app apply-term Δx) x)))
(app f x)))))
-- Sugars for diff-term and apply-term
infixl 6 _⊕_ _⊝_
_⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ
_⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ)
t ⊕ Δt = app (app apply-term Δt) t
s ⊝ t = app (app diff-term s) t
|
module Syntax.Term.Popl14 where
-- Terms Calculus Popl14
--
-- Contents
-- - Term constructors
-- - Weakening on terms
-- - `fit`: weaken a term to its ΔContext
-- - diff-term, apply-term and their syntactic sugars
open import Syntax.Context.Popl14 public
open import Data.Integer
data Term (Γ : Context) : Type -> Set where
int : (n : ℤ) → Term Γ int
add : (s : Term Γ int) (t : Term Γ int) → Term Γ int
minus : (t : Term Γ int) → Term Γ int
empty : Term Γ bag
insert : (s : Term Γ int) (t : Term Γ bag) → Term Γ bag
union : (s : Term Γ bag) → (t : Term Γ bag) → Term Γ bag
negate : (t : Term Γ bag) → Term Γ bag
flatmap : (s : Term Γ (int ⇒ bag)) (t : Term Γ bag) → Term Γ bag
sum : (t : Term Γ bag) → Term Γ int
var : ∀ {τ} → (x : Var Γ τ) → Term Γ τ
app : ∀ {σ τ} → (s : Term Γ (σ ⇒ τ)) (t : Term Γ σ) → Term Γ τ
abs : ∀ {σ τ} → (t : Term (σ • Γ) τ) → Term Γ (σ ⇒ τ)
weaken : ∀ {Γ₁ Γ₂ τ} → (Γ₁≼Γ₂ : Γ₁ ≼ Γ₂) → Term Γ₁ τ → Term Γ₂ τ
weaken Γ₁≼Γ₂ (int x) = int x
weaken Γ₁≼Γ₂ (add s t) = add (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (minus t) = minus (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ empty = empty
weaken Γ₁≼Γ₂ (insert s t) = insert (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (union s t) = union (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (negate t) = negate (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (flatmap s t) = flatmap (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (sum t) = sum (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (var x) = var (weakenVar Γ₁≼Γ₂ x)
weaken Γ₁≼Γ₂ (app s t) = app (weaken Γ₁≼Γ₂ s) (weaken Γ₁≼Γ₂ t)
weaken Γ₁≼Γ₂ (abs {τ} t) = abs (weaken (keep τ • Γ₁≼Γ₂) t)
fit : ∀ {τ Γ} → Term Γ τ → Term (ΔContext Γ) τ
fit = weaken Γ≼ΔΓ
diff-term : ∀ {τ Γ} → Term Γ (τ ⇒ τ ⇒ ΔType τ)
apply-term : ∀ {τ Γ} → Term Γ (ΔType τ ⇒ τ ⇒ τ)
-- Sugars for diff-term and apply-term
infixl 6 _⊕_ _⊝_
_⊕_ : ∀ {τ Γ} → Term Γ τ → Term Γ (ΔType τ) → Term Γ τ
_⊝_ : ∀ {τ Γ} → Term Γ τ → Term Γ τ → Term Γ (ΔType τ)
t ⊕ Δt = app (app apply-term Δt) t
s ⊝ t = app (app diff-term s) t
apply-term {int} =
let Δx = var (that this)
x = var this
in abs (abs (add x Δx))
apply-term {bag} =
let Δx = var (that this)
x = var this
in abs (abs (union x Δx))
apply-term {σ ⇒ τ} =
let
Δf = var (that (that this))
f = var (that this)
x = var this
in
-- Δf f x
abs (abs (abs
(app f x ⊕ app (app Δf x) (x ⊝ x))))
diff-term {int} =
let x = var (that this)
y = var this
in abs (abs (add x (minus y)))
diff-term {bag} =
let x = var (that this)
y = var this
in abs (abs (union x (negate y)))
diff-term {σ ⇒ τ} =
let
g = var (that (that (that this)))
f = var (that (that this))
x = var (that this)
Δx = var this
in
-- g f x Δx
abs (abs (abs (abs
(app g (x ⊕ Δx) ⊝ app f x))))
|
use ⊕ and ⊝ also when defining diff-term and apply-term
|
agda: use ⊕ and ⊝ also when defining diff-term and apply-term
Goal: make diff-term and apply-term themselves more readable.
Old-commit-hash: fc206b02d6057172e7f49c02eb521abaaafcb1e8
|
Agda
|
mit
|
inc-lc/ilc-agda
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.