Skip to content

Commit b9749fb

Browse files
committed
Started Reduction.
1 parent 64fd826 commit b9749fb

File tree

1 file changed

+121
-108
lines changed

1 file changed

+121
-108
lines changed

src/Stlc.lidr

Lines changed: 121 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ work to deal with these.
2222
> %default total
2323
> %hide Types.Tm
2424
> %hide Types.Ty
25+
> %hide Types.(->>)
26+
27+
2528

2629

2730

@@ -162,7 +165,7 @@ We next formalize the syntax of the STLC.
162165

163166
==== Terms
164167

165-
> infixl 1 #
168+
> infixl 7 #
166169

167170
> data Tm : Type where
168171
> Tvar : String -> Tm
@@ -408,112 +411,122 @@ with the function given above.
408411
> substi_correct s x t t' = ?substi_correct_rhs1
409412

410413

411-
(* ================================================================= *)
412-
(** ** Reduction *)
413-
414-
(** The small-step reduction relation for STLC now follows the
415-
same pattern as the ones we have seen before. Intuitively, to
416-
reduce a function application, we first reduce its left-hand
417-
side (the function) until it becomes an abstraction; then we
418-
reduce its right-hand side (the argument) until it is also a
419-
value; and finally we substitute the argument for the bound
420-
variable in the body of the abstraction. This last rule, written
421-
informally as
422-
423-
(\x:T.t12) v2 ==> `x:=v2`t12
424-
425-
is traditionally called "beta-reduction". *)
426-
427-
(**
428-
value v2
429-
---------------------------- (ST_AppAbs)
430-
(\x:T.t12) v2 ==> `x:=v2`t12
431-
432-
t1 ==> t1'
433-
---------------- (ST_App1)
434-
t1 t2 ==> t1' t2
435-
436-
value v1
437-
t2 ==> t2'
438-
---------------- (ST_App2)
439-
v1 t2 ==> v1 t2'
440-
*)
441-
(** ... plus the usual rules for conditionals:
442-
443-
-------------------------------- (ST_IfTrue)
444-
(if true then t1 else t2) ==> t1
445-
446-
--------------------------------- (ST_IfFalse)
447-
(if false then t1 else t2) ==> t2
448-
449-
t1 ==> t1'
450-
---------------------------------------------------- (ST_If)
451-
(if t1 then t2 else t3) ==> (if t1' then t2 else t3)
452-
*)
453-
454-
(** Formally: *)
455-
456-
Reserved Notation "t1 '==>' t2" (at level 40).
457-
458-
Inductive step : Tm -> Tm -> Prop :=
459-
| ST_AppAbs : forall x T t12 v2,
460-
value v2 ->
461-
(tapp (tabs x T t12) v2) ==> `x:=v2`t12
462-
| ST_App1 : forall t1 t1' t2,
463-
t1 ==> t1' ->
464-
tapp t1 t2 ==> tapp t1' t2
465-
| ST_App2 : forall v1 t2 t2',
466-
value v1 ->
467-
t2 ==> t2' ->
468-
tapp v1 t2 ==> tapp v1 t2'
469-
| ST_IfTrue : forall t1 t2,
470-
(tif ttrue t1 t2) ==> t1
471-
| ST_IfFalse : forall t1 t2,
472-
(tif tfalse t1 t2) ==> t2
473-
| ST_If : forall t1 t1' t2 t3,
474-
t1 ==> t1' ->
475-
(tif t1 t2 t3) ==> (tif t1' t2 t3)
476-
477-
where "t1 '==>' t2" := (step t1 t2).
478-
479-
Hint Constructors step.
480-
481-
Notation multistep := (multi step).
482-
Notation "t1 '==>*' t2" := (multistep t1 t2) (at level 40).
483-
484-
(* ================================================================= *)
485-
(** ** Examples *)
486-
487-
(** Example:
488-
489-
(\x:Bool->Bool. x) (\x:Bool. x) ==>* \x:Bool. x
414+
== Reduction
415+
416+
The small-step reduction relation for STLC now follows the
417+
same pattern as the ones we have seen before. Intuitively, to
418+
reduce a function application, we first reduce its left-hand
419+
side (the function) until it becomes an abstraction; then we
420+
reduce its right-hand side (the argument) until it is also a
421+
value; and finally we substitute the argument for the bound
422+
variable in the body of the abstraction. This last rule, written
423+
informally as
424+
425+
(\x:T.t12) v2 ->> [x:=v2] t12
426+
427+
is traditionally called "beta-reduction".
428+
429+
430+
\[
431+
\begin{prooftree}
432+
\hypo{\idr{value v2}}
433+
\infer1[\idr{ST_AppAbs}]{\idr{(\x:T.t12) v2 ->> [x:=v2] t12}}
434+
\end{prooftree}
435+
\]
436+
437+
\[
438+
\begin{prooftree}
439+
\hypo{\idr{t1 ->> t1'}}
440+
\infer1[\idr{ST_App1}]{\idr{t1 t2 ->> t1' t2}
441+
\end{prooftree}
442+
\]
443+
444+
\[
445+
\begin{prooftree}
446+
\hypo{\idr{value v1}}
447+
\hypo{\idr{t2 ->> t2'}}
448+
\infer2[\idr{ST_App2}]{\idr{v1 t2 ->> v1 t2'}
449+
\end{prooftree}
450+
\]
451+
452+
... plus the usual rules for conditionals:
453+
454+
\[
455+
\begin{prooftree}
456+
\infer0[\idr{ST_IfTrue}]{\idr{(if true then t1 else t2) ->> t1}
457+
\end{prooftree}
458+
\]
459+
460+
\[
461+
\begin{prooftree}
462+
\infer0[\idr{ST_IfFalse}]{\idr{(if false then t1 else t2) ->> t2}
463+
\end{prooftree}
464+
\]
465+
466+
\[
467+
\begin{prooftree}
468+
\hypo{\idr{t1 ->> t1'}}
469+
\infer1[\idr{ST_If}]{\idr{(if t1 then t2 else t3) ->> (if t1' then t2 else t3)}
470+
\end{prooftree}
471+
\]
472+
473+
Formally:
474+
475+
> mutual
476+
> infixl 6 ->>
477+
> (->>) : Tm -> Tm -> Type
478+
> (->>) = Step
479+
>
480+
> data Step : Tm -> Tm -> Type where
481+
> ST_AppAbs : {x: String} -> {ty : Ty} -> {t12 : Tm} -> {v2 : Tm} ->
482+
> Value v2 ->
483+
> (Tabs x ty t12) # v2 ->> [ x := v2] t12
484+
> ST_App1 : {t1, t1', t2: Tm} ->
485+
> t1 ->> t1' ->
486+
> t1 # t2 ->> t1' # t2
487+
> ST_App2 : {v1, t2, t2' : Tm} ->
488+
> Value v1 ->
489+
> t2 ->> t2' ->
490+
> v1 # t2 ->> v1 # t2'
491+
> ST_IfTrue : {t1, t2: Tm} ->
492+
> Tif Ttrue t1 t2 ->> t1
493+
> ST_IfFalse : {t1, t2: Tm} ->
494+
> Tif Tfalse t1 t2 ->> t2
495+
> ST_If : {t1, t1', t2, t3: Tm} ->
496+
> t1 ->> t1' ->
497+
> Tif t1 t2 t3 ->> Tif t1' t2 t3
498+
499+
> infixl 6 ->>*
500+
> (->>*) : Tm -> Tm -> Type
501+
> (->>*) t t' = Multi Step t t'
502+
503+
=== Examples
504+
505+
Example:
506+
507+
(\x:Bool->Bool. x) (\x:Bool. x) ->>* \x:Bool. x
490508

491509
i.e.,
492510

493-
idBB idB ==>* idB
494-
*)
511+
idBB idB ->>* idB
495512

496-
Lemma step_example1 :
497-
(tapp idBB idB) ==>* idB.
498-
Proof.
499-
eapply multi_step.
500-
apply ST_AppAbs.
501-
apply v_abs.
502-
simpl.
503-
apply multi_refl. Qed.
513+
> step_example1 : Stlc.idBB # Stlc.idB ->>* Stlc.idB
514+
> step_example1 =
515+
> Multi_step (ST_AppAbs V_abs) Multi_refl
504516

505-
(** Example:
517+
Example:
506518

507519
(\x:Bool->Bool. x) ((\x:Bool->Bool. x) (\x:Bool. x))
508-
==>* \x:Bool. x
520+
->>* \x:Bool. x
509521

510522
i.e.,
511523

512-
(idBB (idBB idB)) ==>* idB.
513-
*)
524+
(idBB (idBB idB)) ->>* idB.
525+
526+
> step_example2 : Stlc.idBB # (Stlc.idBB # Stlc.idB) ->>* Stlc.idB
527+
> step_example2 =
528+
> Multi_step (ST_App2 V_abs ?hole1 ) (Multi_step (ST_AppAbs V_abs) ?hole3)
514529

515-
Lemma step_example2 :
516-
(tapp idBB (tapp idBB idB)) ==>* idB.
517530
Proof.
518531
eapply multi_step.
519532
apply ST_App2. auto.
@@ -527,15 +540,15 @@ Proof.
527540
(\x:Bool->Bool. x)
528541
(\x:Bool. if x then false else true)
529542
true
530-
==>* false
543+
->>* false
531544

532545
i.e.,
533546

534-
(idBB notB) ttrue ==>* tfalse.
547+
(idBB notB) ttrue ->>* tfalse.
535548
*)
536549

537550
Lemma step_example3 :
538-
tapp (tapp idBB notB) ttrue ==>* tfalse.
551+
tapp (tapp idBB notB) ttrue ->>* tfalse.
539552
Proof.
540553
eapply multi_step.
541554
apply ST_App1. apply ST_AppAbs. auto. simpl.
@@ -548,18 +561,18 @@ Proof.
548561

549562
(\x:Bool -> Bool. x)
550563
((\x:Bool. if x then false else true) true)
551-
==>* false
564+
->>* false
552565

553566
i.e.,
554567

555-
idBB (notB ttrue) ==>* tfalse.
568+
idBB (notB ttrue) ->>* tfalse.
556569

557570
(Note that this term doesn't actually typecheck; even so, we can
558571
ask how it reduces.)
559572
*)
560573

561574
Lemma step_example4 :
562-
tapp idBB (tapp notB ttrue) ==>* tfalse.
575+
tapp idBB (tapp notB ttrue) ->>* tfalse.
563576
Proof.
564577
eapply multi_step.
565578
apply ST_App2. auto.
@@ -575,33 +588,33 @@ Proof.
575588
to simplify these proofs. *)
576589

577590
Lemma step_example1' :
578-
(tapp idBB idB) ==>* idB.
591+
(tapp idBB idB) ->>* idB.
579592
Proof. normalize. Qed.
580593

581594
Lemma step_example2' :
582-
(tapp idBB (tapp idBB idB)) ==>* idB.
595+
(tapp idBB (tapp idBB idB)) ->>* idB.
583596
Proof. normalize. Qed.
584597

585598
Lemma step_example3' :
586-
tapp (tapp idBB notB) ttrue ==>* tfalse.
599+
tapp (tapp idBB notB) ttrue ->>* tfalse.
587600
Proof. normalize. Qed.
588601

589602
Lemma step_example4' :
590-
tapp idBB (tapp notB ttrue) ==>* tfalse.
603+
tapp idBB (tapp notB ttrue) ->>* tfalse.
591604
Proof. normalize. Qed.
592605

593606
(** **** Exercise: 2 stars (step_example5) *)
594607
(** Try to do this one both with and without `normalize`. *)
595608

596609
Lemma step_example5 :
597610
tapp (tapp idBBBB idBB) idB
598-
==>* idB.
611+
->>* idB.
599612
Proof.
600613
(* FILL IN HERE *) Admitted.
601614

602615
Lemma step_example5_with_normalize :
603616
tapp (tapp idBBBB idBB) idB
604-
==>* idB.
617+
->>* idB.
605618
Proof.
606619
(* FILL IN HERE *) Admitted.
607620
(** `` *)

0 commit comments

Comments
 (0)