Skip to content

Commit e41aa35

Browse files
committed
[ week 5 ] today's lecture
1 parent 37f867b commit e41aa35

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

lectures/Week5.agda

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,90 @@ aTGoodSum = add (nat 42) (nat 10)
144144
-- We can now compute the type of the value of a given typed
145145
-- expression.
146146

147-
-- TVal; teval
147+
TVal : Ty Set
148+
TVal bool = Bool
149+
TVal nat =
150+
151+
152+
{-
153+
data TVal : Ty → Set where
154+
nat : ℕ -> TVal nat
155+
bool : Bool -> TVal bool
156+
-}
157+
158+
teval : forall {T} -> TExpr T -> TVal T
159+
teval (nat n) = n
160+
teval (bool b) = b
161+
teval (add en em) = teval en + teval em
162+
teval (ifte eb et ee) = if teval eb then teval et else teval ee
163+
164+
148165

149166
--------------------------------------------------------------------------
150167
-- Relating typed and untyped expressions
151168
---------------------------------------------------------------------------
152169

153170
-- It is easy to forget the type of a typed expression.
154171

155-
-- ∣_∣ : ∀ {t} → TExpr t -> Expr
172+
∣_∣ : {t} TExpr t -> Expr
173+
∣ nat n ∣ = nat n
174+
∣ bool b ∣ = bool b
175+
∣ add en em ∣ = add ∣ en ∣ ∣ em ∣
176+
∣ ifte eb et ee ∣ = ifte (∣ eb ∣) (∣ et ∣) (∣ ee ∣)
156177

157178

179+
embed : {t} TVal t Val
180+
embed {bool} v = bool v
181+
embed {nat} v = nat v
182+
183+
∣∣-correct : {t} (e : TExpr t) eval ∣ e ∣ ≡ just (embed (teval e))
184+
∣∣-correct = {!!}
185+
158186

159187
-- Conversely, we can record when a given untyped expression is
160188
-- welltyped. (As we have seen, this is not always the case.)
161189

162190
record Welltyped (e : Expr) : Set where
191+
constructor mkWellTyped
192+
field
193+
aType : Ty
194+
aTerm : TExpr aType
195+
proof : ∣ aTerm ∣ ≡ e
196+
open Welltyped
197+
-- And we can infer the type of an expression
163198

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

165-
-- And we can infer the type of an expression
166204

167-
-- infer : (e : Expr) -> Welltyped e
205+
206+
infer : (e : Expr) -> Maybe (Welltyped e)
207+
infer (nat n) = just (mkWellTyped nat (nat n) refl)
208+
infer (bool b) = just (mkWellTyped bool (bool b) refl)
209+
infer (en +E em) = do
210+
mkWellTyped ty₁ tm refl <- infer en
211+
refl eqTy? ty₁ nat
212+
mkWellTyped ty₂ tm' refl <- infer em
213+
refl eqTy? ty₂ nat
214+
just (mkWellTyped nat (add tm tm') refl)
215+
infer (ifE eb then et else ee) = do
216+
mkWellTyped ty₀ tm refl <- infer eb
217+
refl eqTy? ty₀ bool
218+
mkWellTyped ty₁ tm₁ refl <- infer et
219+
mkWellTyped ty₂ tm₂ refl <- infer ee
220+
refl <- eqTy? ty₁ ty₂
221+
just (mkWellTyped ty₁ (ifte tm tm₁ tm₂) refl)
222+
223+
typingUnique : (e : Expr) (p q : Welltyped e) aType p ≡ aType q
224+
typingUnique = {!!}
225+
226+
anIFTE' : Expr
227+
anIFTE' = ifE aBool then aNat else bool true
168228

169229

230+
foo = infer anIFTE'
170231

171232

172233
---------------------------------------------------------------------------
@@ -177,7 +238,8 @@ record Welltyped (e : Expr) : Set where
177238
-- false`. Using typed expressions, we can already record in the type
178239
-- of this function that this optimisation is type-preserving.
179240

180-
-- reduce-if : ∀ {T} → TExpr T -> TExpr T
241+
reduce-if : {T} TExpr T -> TExpr T
242+
reduce-if e = {!!}
181243

182244

183245

0 commit comments

Comments
 (0)