{-# OPTIONS --rewriting #-}
module Embed where
open import Data.Nat
open import Data.Product
open import Function
open import Relation.Binary.PropositionalEquality
open import DStermK
open import DSterm
embedT : typK → typ
embedT Nat = Nat
embedT (τ₂ ⇒ τ₁ cps[ τ₃ , τ₄ ]) =
embedT τ₂ ⇒ embedT τ₁ cps[ embedT τ₃ , embedT τ₄ ]
embedContT : conttypK → conttyp
embedContT (K τ₁ ▷ τ₂) = embedT τ₁ ▷ embedT τ₂
embedContT (• τ) = embedT τ ▷ embedT τ
mutual
embedV : {var : typ → Set} {τ : typK} →
valueK[ var ∘ embedT ] τ →
value[ var ] embedT τ
embedV (Var x) = Var x
embedV (Num n) = Num n
embedV (Fun e) = Fun (λ x → embed (e x))
embedV Shift = Shift
embed : {var : typ → Set} {τ : typK} {Δ : conttypK} →
termK[ var ∘ embedT , Δ ] τ →
term[ var , embedContT Δ ] embedT τ
embed (Ret k v) =
plug (embedC k) (Val (embedV v))
embed (App v w k) =
plug (embedC k)
(NonVal (App (Val (embedV v)) (Val (embedV w))))
embed (Shift2 e k) =
plug (embedC k)
(NonVal (Shift2 (λ k → embed (e k))))
embed (RetE {τ} k e) =
plug (embedC k) (NonVal (Reset (embed e)))
embedC : {var : typ → Set} {τ₃ τ₄ : typK} {Δ : conttypK} →
pcontextK[ var ∘ embedT , Δ , τ₃ ] τ₄ →
pcontext[ var , embedContT Δ , embedT τ₃ ] embedT τ₄
embedC KVar = Hole
embedC KId = Hole
embedC {Δ = K τ₁ ▷ τ₂} (KLet e) = Let Hole (λ x → embed (e x))
embedC {Δ = • τ} (KLet e) = Let Hole (λ x → embed (e x))
knormalT : typ → typK
knormalT Nat = Nat
knormalT (τ₂ ⇒ τ₁ cps[ τ₃ , τ₄ ]) =
knormalT τ₂ ⇒ knormalT τ₁ cps[ knormalT τ₃ , knormalT τ₄ ]
knormalContT : conttyp → conttypK
knormalContT (τ₁ ▷ τ₂) = K knormalT τ₁ ▷ knormalT τ₂
mutual
knormalV : {var : typK → Set} → {τ₁ : typ} →
value[ var ∘ knormalT ] τ₁ →
valueK[ var ] knormalT τ₁
knormalV (Var x) = Var x
knormalV (Num n) = Num n
knormalV (Fun e) = Fun (λ x → knormal (e x) KVar)
knormalV Shift = Shift
knormal : {var : typK → Set} →
{τ₁ τ₂ τ₃ : typ} → {Δ : conttypK} →
term[ var ∘ knormalT , τ₁ ▷ τ₂ ] τ₃ →
pcontextK[ var , Δ , knormalT τ₁ ] knormalT τ₂ →
termK[ var , Δ ] (knormalT τ₃)
knormal (Val v) k = Ret k (knormalV v)
knormal (NonVal (App (NonVal e₁) (NonVal e₂))) k =
knormal (NonVal e₁) (KLet (λ m →
knormal (NonVal e₂) (KLet (λ n →
App (Var m) (Var n) k))))
knormal (NonVal (App (NonVal e₁) (Val v₂))) k =
knormal (NonVal e₁) (KLet (λ m →
App (Var m) (knormalV v₂) k))
knormal (NonVal (App (Val v₁) (NonVal e₂))) k =
knormal (NonVal e₂) (KLet (λ n →
App (knormalV v₁) (Var n) k))
knormal (NonVal (App (Val v₁) (Val v₂))) k =
App (knormalV v₁) (knormalV v₂) k
knormal (NonVal (Shift2 e)) k =
Shift2 (λ k → knormal (e k) KVar) k
knormal (NonVal (Reset e)) k =
RetE k (knormal e KId)
knormal (NonVal (Let e₁ e₂)) k =
knormal e₁ (KLet (λ m →
knormal (e₂ m) k))
test1 : {var : typK → Set} → {τ₁ τ₂ : typ} →
valueK[ var ] (knormalT (τ₁ ⇒ τ₁ cps[ τ₂ , τ₂ ]))
test1 = knormalV DSterm.val1
test2 : {var : typK → Set} → {τ₁ τ₂ : typ} →
valueK[ var ] (knormalT (τ₁ ⇒ τ₁ cps[ τ₂ , τ₂ ]))
test2 = knormalV DSterm.val2
test3 : {var : typK → Set} → {τ₁ τ₂ τ₃ τ : typ} →
valueK[ var ] (knormalT (τ₁ ⇒ τ₂ cps[ τ₃ , τ₁ ]))
test3 {τ = τ} = knormalV (DSterm.val3 {τ = τ})
open import Agda.Builtin.Equality
open import Agda.Builtin.Equality.Rewrite
knormalT∘embedT≡id : (τ : typK) → knormalT (embedT τ) ≡ τ
knormalT∘embedT≡id Nat = refl
knormalT∘embedT≡id (τ₂ ⇒ τ₁ cps[ τ₃ , τ₄ ])
rewrite knormalT∘embedT≡id τ₁
| knormalT∘embedT≡id τ₂
| knormalT∘embedT≡id τ₃
| knormalT∘embedT≡id τ₄ = refl
{-# REWRITE knormalT∘embedT≡id #-}
embedT∘knormalT≡id : (τ : typ) → embedT (knormalT τ) ≡ τ
embedT∘knormalT≡id Nat = refl
embedT∘knormalT≡id (τ₂ ⇒ τ₁ cps[ τ₃ , τ₄ ])
rewrite embedT∘knormalT≡id τ₁
| embedT∘knormalT≡id τ₂
| embedT∘knormalT≡id τ₃
| embedT∘knormalT≡id τ₄ = refl
embedContT∘knormalContT≡id : (Δ : conttyp) → embedContT (knormalContT Δ) ≡ Δ
embedContT∘knormalContT≡id (τ₁ ▷ τ₂)
rewrite embedT∘knormalT≡id τ₁
| embedT∘knormalT≡id τ₂ = refl
{-# REWRITE embedT∘knormalT≡id #-}