Skip to content

Commit 5c68ed1

Browse files
committed
fix
1 parent 1a0333f commit 5c68ed1

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/examples/Arith.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ sig op-error = []
4141
open import ScopedTuple using (Tuple; _✖_; zip)
4242
open import Fold2 Op sig
4343

44-
open import AbstractBindingTree Op sig renaming (ABT to AST)
44+
open import AbstractBindingTree Op sig renaming (ABT to AST) public
4545
pattern $ n = op-num n ⦅ nil ⦆
4646
pattern # b = op-bool b ⦅ nil ⦆
4747
infixl 7 _⊗_

src/examples/ArithTypeSafety.agda

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
open import Data.Bool using (true; false; if_then_else_) renaming (Bool to 𝔹)
22
open import Data.List using (List; []; _∷_; length)
33
open import Data.Maybe using (Maybe; nothing; just)
4+
open import Data.Nat
5+
using (ℕ; zero; suc; _+_; _*_; _⊔_; _∸_; _≤_; _<_; z≤n; s≤s)
46
open import Data.Product
57
using (_×_; proj₁; proj₂; Σ-syntax) renaming (_,_ to ⟨_,_⟩ )
68
open import Data.Unit.Polymorphic using (⊤; tt)
79
open import Data.Vec using (Vec) renaming ([] to []̌; _∷_ to _∷̌_)
8-
open import examples.Arith
10+
open import examples.Arith hiding (eval-op; eval; evaluate)
11+
open import Fold Op sig
912
open import FoldPreserve Op sig
1013
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
1114
open import Sig
12-
open import Structures using (lower; lift-lower-id)
13-
open import Var
15+
open import Level using (Level; Lift; lift; lower)
16+
open import Var
17+
import Agda.Builtin.Unit
1418

1519
module examples.ArithTypeSafety where
1620

@@ -57,12 +61,44 @@ compress-⊢v : ∀{v A B Δ} → (B ∷ Δ) ⊢v v ⦂ A → Δ ⊢v v ⦂ A
5761
compress-⊢v {.nothing} ⊢v-none = ⊢v-none
5862
compress-⊢v {.(just _)} (⊢v-just x) = ⊢v-just x
5963

64+
open import Structures
65+
open WithOpSig Op sig
66+
open import ScopedTuple using (Tuple; _✖_; zip)
67+
68+
eval-op : (op : Op) Tuple (sig op) (Bind (Maybe Val) (Maybe Val))
69+
Maybe Val
70+
eval-op (op-num n) tt = just (v-num n)
71+
eval-op op-error tt = nothing
72+
eval-op op-mult ⟨ x , ⟨ y , tt ⟩ ⟩ = do
73+
v₁ x ; v₂ y
74+
num? v₁ (λ n num? v₂ (λ m just (v-num (n * m))))
75+
eval-op op-let ⟨ mv , ⟨ f , tt ⟩ ⟩ = f mv
76+
{- skipping check on mv, simpler -}
77+
eval-op (op-bool b) tt = just (v-bool b)
78+
eval-op op-if ⟨ cnd , ⟨ thn , ⟨ els , tt ⟩ ⟩ ⟩ = do
79+
vᶜ cnd
80+
bool? vᶜ (λ b if b then thn else els)
81+
82+
instance
83+
MVal-is-Foldable : Foldable (Maybe Val) (Maybe Val)
84+
MVal-is-Foldable = record { ret = λ x x ; fold-op = eval-op }
85+
86+
eval : (Var Maybe Val) AST Maybe Val
87+
eval = fold
88+
89+
evaluate : AST Maybe Val
90+
evaluate M = eval (λ x nothing) M
91+
6092
instance
6193
_ : FoldPreservable (Maybe Val) (Maybe Val) (Type)
6294
_ = record { 𝑉 = 𝑉 ; 𝑃 = 𝑃 ; 𝐴 = 𝐴 ; _⊢v_⦂_ = _⊢v_⦂_ ; _⊢c_⦂_ = _⊢c_⦂_
6395
; ret-pres = λ x x ; shift-⊢v = shift-⊢v
6496
; 𝑉-⊢v = λ { refl ⊢v⦂ ⊢v⦂ } ; prev-𝑉 = λ x x }
6597

98+
lift-lower-id : {ℓ ℓ′ : Level}{A : Set ℓ}{x : Lift ℓ′ A}
99+
lift (lower x) ≡ x
100+
lift-lower-id = refl
101+
66102
op-pres : {op}{Rs}{Δ}{A : Type}{As : Vec Type (length (sig op))}{Bs}
67103
sig op ∣ Δ ∣ Bs ⊢ᵣ₊ Rs ⦂ As
68104
𝑃 op As Bs A Δ ⊢c (eval-op op Rs) ⦂ A
@@ -78,8 +114,8 @@ op-pres {op-let} {A = Tᵣ}{As = T₁ ∷̌ T₂ ∷̌ []̆}
78114
(cons-r (bind-r{b}{Δ = Δ}{f = f} Pbody) nil-r))
79115
⟨ refl , refl ⟩ =
80116
compress-⊢v {B = T₁} (⊢ᵣ→⊢c G)
81-
where G : Sig.Sig.■ ∣ T₁ ∷ Δ ∣ tt ⊢ᵣ Structures.lift (lower (f c)) ⦂ Tᵣ
82-
G rewrite lift-lower-id (f c) = Pbody {c} (shift-⊢v Prhs) tt
117+
where G : ■ ∣ T₁ ∷ Δ ∣ lift Agda.Builtin.Unit.tt ⊢ᵣ f c ⦂ Tᵣ
118+
G = Pbody (shift-⊢v Prhs) tt
83119
op-pres {op-bool b} nil-r refl = ⊢v-just ⊢-bool
84120
op-pres {op-if} (cons-r (ast-r Pc) (cons-r (ast-r Pthn)
85121
(cons-r (ast-r Pels) nil-r)))

0 commit comments

Comments
 (0)