@@ -21,6 +21,7 @@ work to deal with these.
21
21
22
22
> % access public export
23
23
> % default total
24
+
24
25
> % hide Types . Tm
25
26
> % hide Types . Ty
26
27
> % hide Types . (->> )
@@ -169,18 +170,19 @@ We next formalize the syntax of the STLC.
169
170
> infixr 7 #
170
171
171
172
> data Tm : Type where
172
- > Tvar : String -> Tm
173
+ > Tvar : Id -> Tm
173
174
> (#) : Tm -> Tm -> Tm
174
- > Tabs : String -> Ty -> Tm -> Tm
175
+ > Tabs : Id -> Ty -> Tm -> Tm
175
176
> Ttrue : Tm
176
177
> Tfalse : Tm
177
178
> Tif : Tm -> Tm -> Tm -> Tm
178
179
179
- > syntax " (" " \\ " [p] " :" [q] " ." [r] " )" = Tabs " p" q r
180
+ > syntax " (" " \\ " [p] " :" [q] " ." [r] " )" = Tabs ( MkId " p" ) q r
180
181
181
182
> syntax " lif" [c] " then" [p] " else" [n] = Tif c p n
182
183
183
- > syntax " &" [p] = Tvar " p"
184
+ > var : String -> Tm
185
+ > var s = Tvar (MkId s)
184
186
185
187
Note that an abstraction `\ x : T.t` (formally, `tabs x T t`) is
186
188
always annotated with the type `T` of its : parameter, in contrast
@@ -193,27 +195,27 @@ Some examples...
193
195
`idB = \ x : Bool . x`
194
196
195
197
> idB : Tm
196
- > idB = (\ x : TBool . &x )
198
+ > idB = (\ x : TBool . var "x" )
197
199
198
200
`idBB = \ x : Bool -> Bool . x`
199
201
200
202
> idBB : Tm
201
- > idBB = (\ x : (TBool :=> TBool) . &x )
203
+ > idBB = (\ x : (TBool :=> TBool) . var "x" )
202
204
203
205
`idBBBB = \ x : (Bool -> Bool ) -> (Bool -> Bool ). x`
204
206
205
207
> idBBBB : Tm
206
- > idBBBB = (\ x : ((TBool :=> TBool) :=> (TBool :=> TBool)). &x )
208
+ > idBBBB = (\ x : ((TBool :=> TBool) :=> (TBool :=> TBool)). var "x" )
207
209
208
210
`k = \ x : Bool . \y:Bool . x`
209
211
210
212
> k : Tm
211
- > k = (\ x : TBool . (\y : TBool . &x ))
213
+ > k = (\ x : TBool . (\y : TBool . var "x" ))
212
214
213
215
`notB = \ x : Bool . if x then false else true`
214
216
215
217
> notB : Tm
216
- > notB = (\ x : TBool . (lif &x then Tfalse else Ttrue))
218
+ > notB = (\ x : TBool . (lif (var "x") then Tfalse else Ttrue))
217
219
218
220
=== Operational Semantics
219
221
@@ -259,7 +261,7 @@ function is actually applied to an argument. We also make the
259
261
second choice here.
260
262
261
263
> data Value : Tm -> Type where
262
- > V_abs : {x : String } -> {T : Ty} -> {t : Tm} -> Value (Tabs x T t)
264
+ > V_abs : {x : Id } -> {T : Ty} -> {t : Tm} -> Value (Tabs x T t)
263
265
> V_true : Value Ttrue
264
266
> V_false : Value Tfalse
265
267
@@ -344,15 +346,15 @@ Here is the definition, informally...
344
346
345
347
... and formally :
346
348
347
- > subst : String -> Tm -> Tm -> Tm
349
+ > subst : Id -> Tm -> Tm -> Tm
348
350
> subst x s (Tvar x') =
349
351
> case decEq x x' of
350
- > Yes _ => s
351
- > No _ => ( Tvar x')
352
+ > Yes => s
353
+ > No _ => Tvar x'
352
354
> subst x s (Tabs x' ty t1) =
353
- > Tabs x' ty ( case decEq x x' of
354
- > Yes p => t1
355
- > No p => subst x s t1)
355
+ > case decEq x x' of
356
+ > Yes => t1
357
+ > No _ => subst x s t1
356
358
> subst x s (t1 # t2) = subst x s t1 # subst x s t2
357
359
> subst x s Ttrue = Ttrue
358
360
> subst x s Tfalse = Tfalse
@@ -396,7 +398,7 @@ one of the constructors; your job is to fill in the rest of the
396
398
constructors and prove that the relation you've defined coincides
397
399
with the function given above.
398
400
399
- > data Substi : (s :Tm) -> (x :String ) -> Tm -> Tm -> Type where
401
+ > data Substi : (s :Tm) -> (x :Id ) -> Tm -> Tm -> Type where
400
402
> S_True : Substi s x Ttrue Ttrue
401
403
> S_False : Substi s x Tfalse Tfalse
402
404
> S_App : {l', r':Tm} -> Substi s x l l' -> Substi s x r r' -> Substi s x (l # r) (l' # r')
@@ -408,7 +410,7 @@ with the function given above.
408
410
> S_Abs2 : Substi s x (Tabs y ty t) (Tabs y ty t)
409
411
410
412
411
- > substi_correct : (s :Tm) -> (x : String ) -> (t : Tm) -> (t' : Tm) ->
413
+ > substi_correct : (s :Tm) -> (x : Id ) -> (t : Tm) -> (t' : Tm) ->
412
414
> (([ x := s ] t) = t') <-> Substi s x t t'
413
415
> substi_correct s x t t' = ? substi_correct_rhs1
414
416
@@ -481,9 +483,9 @@ Formally:
481
483
> (->> ) = Step
482
484
>
483
485
> data Step : Tm -> Tm -> Type where
484
- > ST_AppAbs : {x : String } -> {ty : Ty} -> {t12 : Tm} -> {v2 : Tm} ->
486
+ > ST_AppAbs : {x : Id } -> {ty : Ty} -> {t12 : Tm} -> {v2 : Tm} ->
485
487
> Value v2 ->
486
- > (Tabs x ty t12) # v2 -> > [ x := v2] t12
488
+ > (Tabs x ty t12) # v2 -> > subst x v2 t12
487
489
> ST_App1 : {t1, t1', t2: Tm} ->
488
490
> t1 -> > t1' ->
489
491
> t1 # t2 -> > t1' # t2
@@ -497,7 +499,7 @@ Formally:
497
499
> Tif Tfalse t1 t2 -> > t2
498
500
> ST_If : {t1, t1', t2, t3: Tm} ->
499
501
> t1 -> > t1' ->
500
- > Tif t1 t2 t3 -> > Tif t1' t2 t3
502
+ > Tif t1 t2 t3 -> > Tif t1' t2 t3
501
503
502
504
> infixl 6 ->>*
503
505
> (->>* ) : Tm -> Tm -> Type
@@ -514,8 +516,7 @@ Example:
514
516
idBB idB ->>* idB
515
517
516
518
> step_example1 : Stlc.idBB # Stlc.idB -> >* Stlc.idB
517
- > step_example1 =
518
- > Multi_step (ST_AppAbs V_abs ) Multi_refl
519
+ > step_example1 = Multi_step (ST_AppAbs V_abs ) Multi_refl -- (ST_AppAbs V_abs) Multi_refl
519
520
520
521
Example :
521
522
@@ -674,11 +675,11 @@ We can read the three-place relation `Gamma |- t ::T` as:
674
675
> syntax [context] " |- " [t] " :: " [T] " . " = Has_type context t T
675
676
676
677
> data Has_type : Context -> Tm -> Ty -> Type where
677
- > T_Var : {Gamma: Context} -> {x: String } -> {T: Ty} ->
678
- > Gamma (MkId x) = Just T ->
678
+ > T_Var : {Gamma: Context} -> {x: Id } -> {T: Ty} ->
679
+ > Gamma x = Just T ->
679
680
> Gamma |- (Tvar x) :: T .
680
- > T_Abs : {Gamma: Context} -> {x: String } -> {T11, T12: Ty} -> {t12 : Tm} ->
681
- > (Gamma & {{ (MkId x) ==> T11 }}) |- t12 :: T12 . ->
681
+ > T_Abs : {Gamma: Context} -> {x: Id } -> {T11, T12: Ty} -> {t12 : Tm} ->
682
+ > (Gamma & {{ x ==> T11 }}) |- t12 :: T12 . ->
682
683
> Gamma |- (Tabs x T11 t12) :: (T11 :=> T12) .
683
684
> T_App : {Gamma: Context} -> {T11, T12: Ty} -> {t1, t2 : Tm} ->
684
685
> Gamma |- t1 :: (T11 :=> T12). ->
@@ -696,7 +697,7 @@ We can read the three-place relation `Gamma |- t ::T` as:
696
697
697
698
==== Examples
698
699
699
- > typing_example_1 : empty |- (Tabs " x" TBool (Tvar " x" )) :: (TBool :=> TBool) .
700
+ > typing_example_1 : empty |- (Tabs (MkId " x" ) TBool (var " x" )) :: (TBool :=> TBool) .
700
701
> typing_example_1 = T_Abs (T_Var Refl)
701
702
702
703
@@ -708,9 +709,9 @@ Another example:
708
709
```
709
710
710
711
> typing_example_2 : empty |-
711
- > (Tabs " x" TBool
712
- > (Tabs " y" (TBool :=> TBool)
713
- > (Tvar " y" # Tvar " y" # Tvar " x" ))) ::
712
+ > (Tabs (MkId " x" ) TBool
713
+ > (Tabs (MkId " y" ) (TBool :=> TBool)
714
+ > (var " y" # var " y" # var " x" ))) ::
714
715
> (TBool :=> (TBool :=> TBool) :=> TBool) .
715
716
> typing_example_2 =
716
717
> T_Abs (T_Abs (T_App (T_Var Refl) (T_App (T_Var Refl) (T_Var Refl))))
@@ -728,10 +729,10 @@ Formally prove the following typing derivation holds:
728
729
729
730
> typing_example_3 :
730
731
> (T : Ty ** empty |-
731
- > (Tabs " x" (TBool :=> TBool)
732
- > (Tabs " y" (TBool :=> TBool)
733
- > (Tabs " z" TBool
734
- > (Tvar " y" # (Tvar " x" # Tvar " z" ))))) :: T . )
732
+ > (Tabs (MkId " x" ) (TBool :=> TBool)
733
+ > (Tabs (MkId " y" ) (TBool :=> TBool)
734
+ > (Tabs (MkId " z" ) TBool
735
+ > (Tvar (MkId " y" ) # (Tvar (MkId " x" ) # Tvar (MkId " z" ) ))))) :: T . )
735
736
> typing_example_3 = ?typing_example_3_rhs
736
737
737
738
$\square$
@@ -747,14 +748,14 @@ to the term `\x:Bool. \y:Bool, x y` -- i.e.,
747
748
748
749
> forallToExistence : {X : Type} -> {P: X -> Type} ->
749
750
> ((a : X) -> Not (P a)) -> Not (a : X ** P a)
750
- > forallToExistence hyp (b ** p2) = hyp b p2
751
+ > forallToExistence hyp (a ** p2) = hyp a p2
751
752
752
753
> typing_nonexample_1 :
753
754
> Not (T : Ty **
754
755
> empty |-
755
- > (Tabs " x" TBool
756
- > (Tabs " y" TBool
757
- > (Tvar " x" # Tvar y ))) :: T . )
756
+ > (Tabs (MkId " x" ) TBool
757
+ > (Tabs (MkId " y" ) TBool
758
+ > (Tvar (MkId " x" ) # Tvar (MkId y) ))) :: T . )
758
759
> typing_nonexample_1 = forallToExistence
759
760
> (\ a , (T_Abs (T_Abs (T_App (T_Var Refl)(T_Var Refl)))) impossible)
760
761
@@ -769,8 +770,8 @@ Another nonexample:
769
770
> typing_nonexample_3 :
770
771
> Not (s : Ty ** t : Ty **
771
772
> empty |-
772
- > (Tabs " x" s
773
- > (Tvar " x" # Tvar " x" )) :: t . )
773
+ > (Tabs (MkId " x" ) s
774
+ > (Tvar (MkId " x" ) # Tvar (MkId " x" ) )) :: t . )
774
775
> typing_nonexample_3 = ?typing_nonexample_3_rhs
775
776
776
777
$\square$
0 commit comments