This commit is contained in:
Michael Zhang 2021-12-09 06:00:09 -06:00
parent a4d892287d
commit 6aa4e905a8
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 54 additions and 51 deletions

View file

@ -13,14 +13,14 @@ open Aexp
open Value
open State
astep : {Γ A} Aexp Γ A Env Γ Value A
astep : { Γ A} Aexp Γ A Env Γ Value A
astep (value v) _ = v
astep zero _ = zero
astep (suc c) e = suc $ astep c e
astep (` id) e = lookup e id
astep (ƛ body) e = clo body e
inject : {A : Type} Exp A State A
inject : {A : Type} Exp A A State A
inject {A} C = mkState A C halt
step : { : Type} State StepResult
@ -36,15 +36,15 @@ step (mkState Tc Γ (x₁ ∘ x₂) E K) with astep x₁ E
-- abort (x₁ ∘ x₂) : A
-- apply-kont (letk (abort `zero)) 2
-- get back to doing (3 + )
... | cont {} {A} k =
... | cont k =
let val = astep x₂ E in
let K = kont $ letk Γ (abort (` zero)) E k in
apply-kont {! K !} val
let K = kont $ letk Γ (atomic $ ` zero) E k in
apply-kont K val
step {} (mkState Tc Γ (call/cc {A} aexp) E K) with astep aexp E
... | clo {Γc} body env =
let Γ′ = Γc , K[ A ] in
let E = env [ K[ A ] cont { = } K ] in
part $ mkState Tc Γ′ body E K
let E = env [ K[ A ] cont K ] in
part $ mkState Tω Γ′ body E halt
step (mkState Tc Γ (abort V⊥) E K) with astep V⊥ E
... | ()
step (mkState Tc Γ (`let {A} C₁ C₂) E K) =
@ -61,11 +61,11 @@ eval (suc n) s with step s
... | part x = eval n x
... | done x = complete $ done x
eval : {A} Exp A EvalResult
eval : {A} Exp A A EvalResult
eval n e = eval n (inject e)
expRes : EvalResult
expRes = eval 100 exp
-- expRes+ : expRes ≡ (complete $ done $ (suc (suc (suc zero))))
-- expRes+ = refl
expRes+ : expRes (complete $ done $ (suc (suc (suc zero))))
expRes+ = refl

View file

@ -21,80 +21,83 @@ data Context : Set where
: Context
_,_ : Context Type Context
data Value : Type Set
data Value ( : Type) : Type Set
data Env : Context Set where
: Env
_[__] : {Γ} Env Γ (A : Type) Value A Env (Γ , A)
data Env ( : Type) : Context Set where
: Env
_[__] : {Γ} Env Γ (A : Type) Value A Env (Γ , A)
data _∋_ : Context Type Set where
zero : {Γ A} Γ , A A
suc : {Γ A B} Γ A Γ , B A
lookup : {Γ A} Env Γ Γ A Value A
lookup : { Γ A} Env Γ Γ A Value A
lookup ()
lookup (env [ A x ]) zero = x
lookup (env [ A x ]) (suc id) = lookup env id
data Aexp Context : Type Set
data Exp Context : Type Set
data Aexp ( : Type) Context : Type Set
data Exp ( : Type) Context : Type Set
data Aexp Γ where
value : {A} Value A Aexp Γ A
data Aexp Γ where
value : {A} Value A Aexp Γ A
-- Natural numbers
zero : Aexp Γ `
suc : Aexp Γ ` Aexp Γ `
zero : Aexp Γ `
suc : Aexp Γ ` Aexp Γ `
-- Functions
`_ : {A} Γ A Aexp Γ A
ƛ : {B} {A : Type} Exp (Γ , A) B Aexp Γ (A B)
`_ : {A} Γ A Aexp Γ A
ƛ : {B} {A : Type} Exp (Γ , A) B Aexp Γ (A B)
data Exp Γ where
abort : {A} Aexp Γ Exp Γ A
data Exp Γ where
abort : {A} Aexp Γ Exp Γ A
-- Atomic expressions
atomic : {A} Aexp Γ A Exp Γ A
atomic : {A} Aexp Γ A Exp Γ A
-- Natural numbers
case : {A} Aexp Γ ` Exp Γ A Aexp Γ (` A) Exp Γ A
case : {A} Aexp Γ ` Exp Γ A Aexp Γ (` A) Exp Γ A
-- Functions
_·_ : {A B} Aexp Γ (A B) Aexp Γ A Exp Γ B
_∘_ : {A B} Aexp Γ K[ A B ] Aexp Γ A Exp Γ B
_·_ : {A B} Aexp Γ (A B) Aexp Γ A Exp Γ B
_∘_ : {A} Aexp Γ K[ A ] Aexp Γ A Exp Γ
-- Call/cc
call/cc : {A } Aexp Γ (K[ A ] ) Exp Γ
call/cc : {A} Aexp Γ (K[ A ] ) Exp Γ A
-- Let
`let : {A B : Type} Exp Γ A Exp (Γ , A) B Exp Γ B
`let : {A B : Type} Exp Γ A Exp (Γ , A) B Exp Γ B
-- exp = let (call/cc ƛ . let (abort `0) (`0 · 2)) ((\ . suc `0) · `0)
-- exp = let (s = call/cc ƛk . let (k' = abort k) (k' · 2)) (suc s)
-- exp = let (call/cc ƛ . let (`0 · 2) (abort `0)) ((\. suc `0) · `0)
-- exp = 3
exp : Exp `
exp =
`let
(call/cc (ƛ (` zero suc (suc zero))))
(`let
(abort (` zero))
((ƛ (atomic (suc (` suc zero)))) · ` zero))
exp : Exp ` `
exp =
`let (call/cc (ƛ (`let (` zero suc (suc zero)) (abort (` zero)))))
((ƛ $ atomic $ suc $ ` zero) · ` zero)
-- `let
-- (call/cc (ƛ (` zero ∘ suc (suc zero))))
-- (`let
-- (abort (` zero))
-- ((ƛ (atomic (suc (` suc zero)))) · ` zero))
data Kont ( : Type) : Type Set
record Letk (Tv : Type) : Set
data Value where
data Value where
-- Natural numbers
zero : Value `
suc : Value ` Value `
zero : Value `
suc : Value ` Value `
-- Functions
clo : {Γ} {A B : Type} Exp (Γ , A) B Env Γ Value (A B)
clo : {Γ} {A B : Type} Exp (Γ , A) B Env Γ Value (A B)
-- Call/CC
-- cont : ∀ {Tω A} → Kont Tω A → Value (A ⇒ ⊥)
cont : { A B} Kont A Value K[ B ]
cont : {A} Kont A Value K[ A ]
record Letk Tv where
inductive
@ -102,8 +105,8 @@ record Letk Tω Tv where
field
{Tc} : Type
Γ : Context
C : Exp (Γ , Tv) Tc
E : Env Γ
C : Exp (Γ , Tv) Tc
E : Env Γ
K : Kont Tc
data Kont where
@ -117,6 +120,6 @@ record State (Tω : Type) : Set where
field
Tc : Type
Γ : Context
C : Exp Γ Tc
E : Env Γ
C : Exp Γ Tc
E : Env Γ
K : Kont Tc

View file

@ -7,14 +7,14 @@ open import Project.Util
data StepResult (A : Type) : Set where
part : State A StepResult A
done : Value A StepResult A
done : Value A A StepResult A
apply-kont : {Tv } Kont Tv Value Tv StepResult
apply-kont : {Tv } Kont Tv Value Tv StepResult
apply-kont {Tv} (kont (letk {Tc} Γ C E K)) v =
part $ mkState Tc (Γ , Tv) C (E [ Tv v ]) K
apply-kont halt v = done v
apply-proc-clo : {Γ A B } Exp (Γ , A) B Env Γ Value A Kont B StepResult
apply-proc-clo : {Γ A B } Exp (Γ , A) B Env Γ Value A Kont B StepResult
apply-proc-clo {Γ} {A} {B} body env arg k =
let Γ′ = Γ , A in
let E = env [ A arg ] in