Skip to content

Commit f4ba079

Browse files
committed
Type progress and preservation
1 parent f0b3f35 commit f4ba079

File tree

1 file changed

+93
-91
lines changed

1 file changed

+93
-91
lines changed

src/Types.lidr

Lines changed: 93 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -298,28 +298,28 @@ is always empty.
298298
\newline
299299
\]
300300

301-
> syntax "|-" [p] "::" [q] = Has_type p q
301+
> syntax "|-" [p] "." [q] = Has_type p q
302302

303303
> data Has_type : Tm -> Ty -> Type where
304-
> T_True : |- Ttrue :: TBool
305-
> T_False : |- Tfalse :: TBool
304+
> T_True : |- Ttrue . TBool
305+
> T_False : |- Tfalse . TBool
306306
> T_If : {t1, t2, t3: Tm} -> {T: Ty} ->
307307
> Has_type t1 TBool ->
308308
> Has_type t2 T ->
309309
> Has_type t3 T ->
310-
> |- (Tif t1 t2 t3) :: T
311-
> T_Zero : |- Tzero :: TNat
310+
> |- (Tif t1 t2 t3) . T
311+
> T_Zero : |- Tzero . TNat
312312
> T_Succ : {t1 : Tm} ->
313313
> Has_type t1 TNat ->
314-
> |- (Tsucc t1) :: TNat
314+
> |- (Tsucc t1) . TNat
315315
> T_Pred : {t1 : Tm} ->
316316
> Has_type t1 TNat ->
317-
> |- (Tpred t1) :: TNat
317+
> |- (Tpred t1) . TNat
318318
> T_Iszero : {t1 : Tm} ->
319319
> Has_type t1 TNat ->
320-
> |- (Tiszero t1) :: TBool
320+
> |- (Tiszero t1) . TBool
321321

322-
> has_type_1 : |- (Tif Tfalse Tzero (Tsucc Tzero)) :: TNat
322+
> has_type_1 : |- (Tif Tfalse Tzero (Tsucc Tzero)) . TNat
323323
> has_type_1 = T_If (T_False) (T_Zero) (T_Succ T_Zero)
324324

325325
It's important to realize that the typing relation is a
@@ -333,7 +333,7 @@ not calculate the type of its normal form.
333333
==== Exercise: 1 star, optional (succ_hastype_nat__hastype_nat)
334334

335335
> succ_hastype_nat__hastype_nat : {t : Tm} ->
336-
> Has_type (Tsucc t) TNat -> |- t :: TNat
336+
> Has_type (Tsucc t) TNat -> |- t . TNat
337337
> succ_hastype_nat__hastype_nat = ?succ_hastype_nat__hastype_nat_rhs
338338

339339
=== Canonical forms
@@ -419,113 +419,115 @@ that we saw in the `Smallstep` chapter, where _all_ normal forms
419419
were values. Here a term can be stuck, but only if it is ill
420420
typed.
421421

422-
<!--
423422

424-
(* ================================================================= *)
425-
(** ** Type Preservation *)
423+
=== Type Preservation
424+
425+
The second critical property of typing is that, when a well-typed
426+
term takes a step, the result is also a well-typed term.
427+
428+
> preservation : {t, t': Tm} -> {T: Ty} ->
429+
> Has_type t T -> t ->> t' -> |- t' . T
426430

427-
(** The second critical property of typing is that, when a well-typed
428-
term takes a step, the result is also a well-typed term. *)
431+
==== Exercise: 2 stars (finish_preservation)
429432

430-
(** **** Exercise: 2 stars (finish_preservation) *)
431-
Theorem preservation : forall t t' T,
432-
|- t \in T ->
433-
t ==> t' ->
434-
|- t' \in T.
433+
> preservation {t=Tif t1 t2 t3} {t'} (T_If h1 h2 h3) he =
434+
> case he of
435+
> ST_IfTrue => h2
436+
> ST_IfFalse => h3
437+
> ST_If hyp =>
438+
> let indHyp = preservation h1 hyp
439+
> in T_If indHyp h2 h3
440+
> ST_Succ hyp impossible
441+
> ST_PredZero impossible
442+
> ST_PredSucc hyp impossible
443+
> ST_Pred hyp impossible
444+
> ST_IszeroZero impossible
445+
> ST_IszeroSucc hyp impossible
446+
> ST_Iszero hyp impossible
447+
> preservation t he = ?preservation_rhs
435448

436-
(** Complete the formal proof of the `preservation` property. (Again,
449+
Complete the formal proof of the `preservation` property. (Again,
437450
make sure you understand the informal proof fragment in the
438-
following exercise first.) *)
439-
440-
Proof with auto.
441-
intros t t' T HT HE.
442-
generalize dependent t'.
443-
induction HT;
444-
(* every case needs to introduce a couple of things *)
445-
intros t' HE;
446-
(* and we can deal with several impossible
447-
cases all at once *)
448-
try solve_by_invert.
449-
- (* T_If *) inversion HE; subst; clear HE.
450-
+ (* ST_IFTrue *) assumption.
451-
+ (* ST_IfFalse *) assumption.
452-
+ (* ST_If *) apply T_If; try assumption.
453-
apply IHHT1; assumption.
454-
(* FILL IN HERE *) Admitted.
455-
(** `` *)
451+
following exercise first.)
456452

457-
(** **** Exercise: 3 stars, advanced (finish_preservation_informal) *)
458-
(** Complete the following informal proof: *)
453+
==== Exercise: 3 stars, advanced (finish_preservation_informal)
459454

460-
(** _Theorem_: If `|- t \in T` and `t ==> t'`, then `|- t' \in T`. *)
455+
Complete the following informal proof:
461456

462-
(** _Proof_: By induction on a derivation of `|- t \in T`.
457+
_Theorem_: If `|- t \in T` and `t ==> t'`, then `|- t' \in T`.
463458

464-
- If the last rule in the derivation is `T_If`, then `t = if t1
465-
then t2 else t3`, with `|- t1 \in Bool`, `|- t2 \in T` and `|- t3
466-
\in T`.
459+
_Proof_: By induction on a derivation of `|- t \in T`.
467460

468-
Inspecting the rules for the small-step reduction relation and
469-
remembering that `t` has the form `if ...`, we see that the
470-
only ones that could have been used to prove `t ==> t'` are
471-
`ST_IfTrue`, `ST_IfFalse`, or `ST_If`.
461+
- If the last rule in the derivation is `T_If`, then `t = if t1
462+
then t2 else t3`, with `|- t1 \in Bool`, `|- t2 \in T` and `|- t3
463+
\in T`.
472464

473-
- If the last rule was `ST_IfTrue`, then `t' = t2`. But we
474-
know that `|- t2 \in T`, so we are done.
465+
Inspecting the rules for the small-step reduction relation and
466+
remembering that `t` has the form `if ...`, we see that the
467+
only ones that could have been used to prove `t ==> t'` are
468+
`ST_IfTrue`, `ST_IfFalse`, or `ST_If`.
475469

476-
- If the last rule was `ST_IfFalse`, then `t' = t3`. But we
477-
know that `|- t3 \in T`, so we are done.
470+
- If the last rule was `ST_IfTrue`, then `t' = t2`. But we
471+
know that `|- t2 \in T`, so we are done.
478472

479-
- If the last rule was `ST_If`, then `t' = if t1' then t2
480-
else t3`, where `t1 ==> t1'`. We know `|- t1 \in Bool` so,
481-
by the IH, `|- t1' \in Bool`. The `T_If` rule then gives us
482-
`|- if t1' then t2 else t3 \in T`, as required.
473+
- If the last rule was `ST_IfFalse`, then `t' = t3`. But we
474+
know that `|- t3 \in T`, so we are done.
483475

484-
- (* FILL IN HERE *)
485-
*)
486-
(** `` *)
476+
- If the last rule was `ST_If`, then `t' = if t1' then t2
477+
else t3`, where `t1 ==> t1'`. We know `|- t1 \in Bool` so,
478+
by the IH, `|- t1' \in Bool`. The `T_If` rule then gives us
479+
`|- if t1' then t2 else t3 \in T`, as required.
487480

488-
(** **** Exercise: 3 stars (preservation_alternate_proof) *)
489-
(** Now prove the same property again by induction on the
490-
_evaluation_ derivation instead of on the typing derivation.
491-
Begin by carefully reading and thinking about the first few
492-
lines of the above proofs to make sure you understand what
493-
each one is doing. The set-up for this proof is similar, but
494-
not exactly the same. *)
495-
496-
Theorem preservation' : forall t t' T,
497-
|- t \in T ->
498-
t ==> t' ->
499-
|- t' \in T.
500-
Proof with eauto.
501-
(* FILL IN HERE *) Admitted.
502-
(** `` *)
481+
- (* FILL IN HERE *)
503482

504-
(** The preservation theorem is often called _subject reduction_,
505-
because it tells us what happens when the "subject" of the typing
506-
relation is reduced. This terminology comes from thinking of
507-
typing statements as sentences, where the term is the subject and
508-
the type is the predicate. *)
483+
==== Exercise: 3 stars (preservation_alternate_proof)
509484

510-
(* ================================================================= *)
511-
(** ** Type Soundness *)
485+
Now prove the same property again by induction on the
486+
_evaluation_ derivation instead of on the typing derivation.
487+
Begin by carefully reading and thinking about the first few
488+
lines of the above proofs to make sure you understand what
489+
each one is doing. The set-up for this proof is similar, but
490+
not exactly the same.
491+
492+
> preservation' : {t, t': Tm} -> {T: Ty} ->
493+
> Has_type t T -> t ->> t' -> |- t' . T
494+
> preservation' h1 h2 = ?preservation'_rhs
512495

513-
(** Putting progress and preservation together, we see that a
514-
well-typed term can never reach a stuck state. *)
496+
The preservation theorem is often called _subject reduction_,
497+
because it tells us what happens when the "subject" of the typing
498+
relation is reduced. This terminology comes from thinking of
499+
typing statements as sentences, where the term is the subject and
500+
the type is the predicate.
515501

516-
Definition multistep := (multi step).
517-
Notation "t1 '==>*' t2" := (multistep t1 t2) (at level 40).
518502

519-
Corollary soundness : forall t t' T,
520-
|- t \in T ->
521-
t ==>* t' ->
522-
~(stuck t').
503+
=== Type Soundness
504+
505+
Putting progress and preservation together, we see that a
506+
well-typed term can never reach a stuck state.
507+
508+
> mutual
509+
> infixl 6 ->>*
510+
> (->>*) : Tm -> Tm -> Type
511+
> (->>*) = multistep
512+
513+
> multistep : Tm -> Tm -> Type
514+
> multistep = Multi Step
515+
516+
517+
> soundness : {t, t': Tm} -> {T: Ty} ->
518+
> Has_type t T ->
519+
> t ->>* t' ->
520+
> Not (stuck t')
521+
> soundness {t} {t'} {T=ty} ht p = ?hole
522+
523523
Proof.
524524
intros t t' T HT P. induction P; intros `R S`.
525525
destruct (progress x T HT); auto.
526526
apply IHP. apply (preservation x y T HT H).
527527
unfold stuck. split; auto. Qed.
528528

529+
<!--
530+
529531
(* ################################################################# *)
530532
(** * Aside: the `normalize` Tactic *)
531533

0 commit comments

Comments
 (0)