cek-call-cc/src/Project/Do.agda

46 lines
1.8 KiB
Agda
Raw Normal View History

2021-12-09 02:57:30 +00:00
-- {-# OPTIONS --allow-unsolved-metas #-}
2021-12-08 18:35:31 +00:00
2021-12-08 06:33:28 +00:00
module Project.Do where
open import Project.Definitions using (Letk; Kont; Exp; Value; State; Type; kont; halt; letk; clo; zero; suc; mkState; `_; `; _·_; _⇒_; _,_; _[__])
open import Project.Util using (_$_)
data StepResult (A : Type) : Set where
part : State A StepResult A
done : Value A StepResult A
-- Apply the continuation to the value, resulting in a state.
do-kont : {Tv } (L : Letk Tv ) Value Tv State
do-kont {Tv} {} (letk {Tc} Γ C E k) v =
let Γ′ = Γ , Tv in
let E = E [ Tv v ] in
mkState Tc Γ′ C E k
2021-12-09 02:57:30 +00:00
applykont : {Tv } Letk Tv Value Tv StepResult
applykont {Tv} (letk {Tc} Γ C E K) v =
part $ mkState Tc (Γ , Tv) C (E [ Tv v ]) K
chain-kont : {A B C} Letk A B Letk B C Letk A C
chain-kont (letk Γ C E halt) b = letk Γ C E $ kont b
chain-kont (letk Γ C E (kont k)) b = letk Γ C E $ kont $ chain-kont k b
2021-12-08 18:35:31 +00:00
do-apply-kont : {A B } Value (A B) Value A Letk B StepResult
do-apply-kont (clo {Γ} {A} {B} body e) v k =
let Γ′ = Γ , A in
let E = e [ A v ] in
part $ mkState B Γ′ body E (kont k)
2021-12-09 02:57:30 +00:00
do-apply-kont (kont halt) a k = part $ do-kont k a
do-apply-kont {A} {B} {} (kont (kont x)) a k =
let k = chain-kont x k in
part $ do-kont k a
-- do-apply-kont (kont halt) b (letk Γ C E K) = {! done b !}
-- do-apply-kont (kont (kont x)) b (letk Γ C E K) = {! !}
2021-12-08 18:35:31 +00:00
-- This needs to be a separate function in order to unify B with Tω
do-apply-halt : {A } Value (A ) Value A StepResult
do-apply-halt {A} {} (clo {Γ} body e) v =
2021-12-08 06:33:28 +00:00
let Γ′ = Γ , A in
let E = e [ A v ] in
2021-12-08 18:35:31 +00:00
part $ mkState Γ′ body E halt
2021-12-09 02:57:30 +00:00
do-apply-halt (kont halt) a = done a
do-apply-halt (kont (kont k)) a = part $ do-kont k a