Skip to content

Commit 5bbf08c

Browse files
committed
[ week5 ] solutions to the challenges + infer/check
1 parent e41aa35 commit 5bbf08c

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

lectures/Week5.agda

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,23 @@ embed {bool} v = bool v
181181
embed {nat} v = nat v
182182

183183
∣∣-correct : {t} (e : TExpr t) eval ∣ e ∣ ≡ just (embed (teval e))
184-
∣∣-correct = {!!}
184+
∣∣-correct (nat n) = refl
185+
∣∣-correct (bool n) = refl
186+
∣∣-correct (add em en)
187+
with just (nat m) eval ∣ em ∣
188+
| refl ∣∣-correct em
189+
with just (nat n) eval ∣ en ∣
190+
| refl ∣∣-correct en
191+
= refl
192+
∣∣-correct (ifte eb el er)
193+
with eval ∣ eb ∣ | {teval eb} | ∣∣-correct eb
194+
... | just (bool true) | refl = ∣∣-correct el
195+
... | just (bool false) | refl = ∣∣-correct er
196+
197+
eqTy? : (s : Ty) (t : Ty) Maybe (s ≡ t)
198+
eqTy? bool bool = just refl
199+
eqTy? nat nat = just refl
200+
eqTy? _ _ = nothing
185201

186202

187203
-- Conversely, we can record when a given untyped expression is
@@ -196,11 +212,28 @@ record Welltyped (e : Expr) : Set where
196212
open Welltyped
197213
-- And we can infer the type of an expression
198214

199-
eqTy? : (s : Ty) (t : Ty) Maybe (s ≡ t)
200-
eqTy? bool bool = just refl
201-
eqTy? nat nat = just refl
202-
eqTy? _ _ = nothing
203215

216+
ifT-eq : {a b c x y z}
217+
(ifE a then b else c) ≡ (ifE x then y else z)
218+
b ≡ y
219+
ifT-eq refl = refl
220+
221+
typingUnique : (e : Expr) (p q : Welltyped e) aType p ≡ aType q
222+
typingUnique e
223+
(mkWellTyped aType₁ (nat x) refl)
224+
(mkWellTyped aType₂ (nat x₁) proof₂) = refl
225+
typingUnique e
226+
(mkWellTyped aType₁ (bool x) refl)
227+
(mkWellTyped aType₂ (bool x₁) proof₂) = refl
228+
typingUnique e
229+
(mkWellTyped aType₁ (add aTerm₁ aTerm₂) refl)
230+
(mkWellTyped aType₂ (add aTerm₃ aTerm₄) proof₂) = refl
231+
typingUnique (ifte eb el er)
232+
(mkWellTyped aType₁ (ifte aTerm₁ aTerm₂ aTerm₃) refl)
233+
(mkWellTyped aType₂ (ifte aTerm₄ aTerm₅ aTerm₆) proof₂)
234+
= typingUnique el
235+
(mkWellTyped aType₁ aTerm₂ refl)
236+
(mkWellTyped aType₂ aTerm₅ (ifT-eq proof₂))
204237

205238

206239
infer : (e : Expr) -> Maybe (Welltyped e)
@@ -220,8 +253,39 @@ infer (ifE eb then et else ee) = do
220253
refl <- eqTy? ty₁ ty₂
221254
just (mkWellTyped ty₁ (ifte tm tm₁ tm₂) refl)
222255

223-
typingUnique : (e : Expr) (p q : Welltyped e) aType p ≡ aType q
224-
typingUnique = {!!}
256+
{-
257+
record Welltyped (ty : Ty) (e : Expr) : Set where
258+
constructor mkWellTyped
259+
field
260+
aTerm : TExpr ty
261+
proof : ∣ aTerm ∣ ≡ e
262+
open Welltyped
263+
-- And we can infer the type of an expression
264+
265+
open import Data.Product.Base using (∃; -,_; _,_)
266+
267+
infer : (e : Expr) -> Maybe (∃ λ ty → Welltyped ty e)
268+
check : (ty : Ty) (e : Expr) → Maybe (Welltyped ty e)
269+
270+
infer (nat n) = just (-, mkWellTyped (nat n) refl)
271+
infer (bool b) = just (-, mkWellTyped (bool b) refl)
272+
infer (en +E em) = do
273+
mkWellTyped tm refl <- check nat en
274+
mkWellTyped tm' refl <- check nat em
275+
just (-, mkWellTyped (add tm tm') refl)
276+
infer (ifE eb then et else ee) = do
277+
mkWellTyped tm refl <- check bool eb
278+
ty , mkWellTyped tm₁ refl <- infer et
279+
mkWellTyped tm₂ refl <- check ty ee
280+
just (-, mkWellTyped (ifte tm tm₁ tm₂) refl)
281+
282+
check ty e = do
283+
ty′ , e' ← infer e
284+
refl ← eqTy? ty ty′
285+
just e'
286+
-}
287+
288+
225289

226290
anIFTE' : Expr
227291
anIFTE' = ifE aBool then aNat else bool true

0 commit comments

Comments
 (0)