Stable and DeBruijn

This commit is contained in:
wadler 2018-02-20 12:42:19 +01:00
parent 2a45baba90
commit f6f41167ba
2 changed files with 133 additions and 0 deletions

59
src/extra/DeBruijn.lagda Normal file
View file

@ -0,0 +1,59 @@
## DeBruijn encodings in Agda
\begin{code}
module DeBruijn where
\end{code}
## Imports
\begin{code}
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; sym; trans; cong)
open Eq.≡-Reasoning
open import Data.Nat using (; zero; suc)
open import Data.Product using (_×_; proj₁; proj₂) renaming (_,_ to ⟨_,_⟩)
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Relation.Nullary using (¬_)
open import Relation.Nullary.Negation using (contraposition)
open import Data.Unit using (; tt)
open import Data.Empty using (⊥; ⊥-elim)
open import Function using (_∘_)
\end{code}
## Representation
\begin{code}
data Type : Set where
` : Type
_`→_ : Type → Type → Type
data Env : Set where
ε : Env
_,_ : Env → Type → Env
data Var : Env → Type → Set where
zero : ∀ {Γ : Env} {A : Type} → Var (Γ , A) A
suc : ∀ {Γ : Env} {A B : Type} → Var Γ B → Var (Γ , A) B
data Exp : Env → Type → Set where
var : ∀ {Γ : Env} {A : Type} → Var Γ A → Exp Γ A
abs : ∀ {Γ : Env} {A B : Type} → Exp (Γ , A) B → Exp Γ (A `→ B)
app : ∀ {Γ : Env} {A B : Type} → Exp Γ (A `→ B) → Exp Γ A → Exp Γ B
type : Type → Set
type ` =
type (A `→ B) = type A → type B
env : Env → Set
env ε =
env (Γ , A) = env Γ × type A
lookup : ∀ {Γ : Env} {A : Type} → Var Γ A → env Γ → type A
lookup zero ⟨ ρ , v ⟩ = v
lookup (suc n) ⟨ ρ , v ⟩ = lookup n ρ
eval : ∀ {Γ : Env} {A : Type} → Exp Γ A → env Γ → type A
eval (var n) ρ = lookup n ρ
eval (abs N) ρ = λ{ v → eval N ⟨ ρ , v ⟩ }
eval (app L M) ρ = eval L ρ (eval M ρ)
\end{code}

74
src/extra/Stable.agda Normal file
View file

@ -0,0 +1,74 @@
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; sym; trans; cong)
open Eq.≡-Reasoning
open import Data.Nat using (; zero; suc)
open import Data.Product using (_×_; proj₁; proj₂) renaming (_,_ to ⟨_,_⟩)
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Relation.Nullary using (¬_)
open import Relation.Nullary.Negation using (contraposition)
open import Data.Unit using (; tt)
open import Data.Empty using (⊥; ⊥-elim)
open import Data.List using (List; []; _∷_; foldr; map)
open import Function using (_∘_)
double-negation : {A : Set} A ¬ ¬ A
double-negation x ¬x = ¬x x
triple-negation : {A : Set} ¬ ¬ ¬ A ¬ A
triple-negation ¬¬¬x x = ¬¬¬x (double-negation x)
Stable : Set Set
Stable A = ¬ ¬ A A
¬-stable : {A : Set} Stable (¬ A)
¬-stable = triple-negation
×-stable : {A B : Set} Stable A Stable B Stable (A × B)
×-stable ¬¬x→x ¬¬y→y ¬¬xy =
¬¬x→x (contraposition (contraposition proj₁) ¬¬xy)
, ¬¬y→y (contraposition (contraposition proj₂) ¬¬xy)
∀-stable : {A : Set} {B : A Set} ( (x : A) Stable (B x)) Stable ( (x : A) B x)
-stable x→¬¬y→y ¬¬∀x→y x =
x→¬¬y→y x (contraposition (contraposition λ{x→y x→y x}) ¬¬∀x→y)
-- Gödel-Gentzen translation
{--
data Var : Set where
zero : (n : ) Var (suc n)
suc : (n : ) Var n Var (suc n)
data Formula : Set where
_`≡_ : (n : ) Var n Var n Formula n
_`×_ : (n : ) Formula n Formula n Formula n
_`⊎_ : (n : ) Formula n Formula n Formula n
`¬_ : (n : ) Formula n Formula n
--}
data Formula : Set where
atomic : (A : Set) Formula
_`×_ : Formula Formula Formula
_`⊎_ : Formula Formula Formula
`¬_ : Formula Formula
interp : Formula Set
interp (atomic A) = A
interp (`A `× `B) = interp `A × interp `B
interp (`A `⊎ `B) = interp `A interp `B
interp ( `A) = ¬ interp `A
g : Formula Formula
g (atomic A) = (atomic A)
g (`A `× `B) = g `A `× g `B
g (`A `⊎ `B) = (( g `A) `× ( g `B))
g ( `A) = g `A
stable-g : (`A : Formula) Stable (interp (g `A))
stable-g (atomic A) = ¬-stable
stable-g (`A `× `B) = ×-stable (stable-g `A) (stable-g `B)
stable-g (`A `⊎ `B) = ¬-stable
stable-g ( `A) = ¬-stable