Skip to content

Commit b92be28

Browse files
committed
Finished with Stlc.
1 parent 52b5187 commit b92be28

File tree

1 file changed

+60
-70
lines changed

1 file changed

+60
-70
lines changed

src/Stlc.lidr

Lines changed: 60 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ work to deal with these.
2929
> %hide Smallstep.(->>*)
3030
> %hide Types.Has_type
3131

32-
33-
34-
35-
36-
37-
3832
=== Overview
3933

4034
The STLC is built on some collection of _base types_:
@@ -199,7 +193,7 @@ Some examples...
199193
`idB = \x:Bool. x`
200194

201195
> idB : Tm
202-
> idB = (\ x : TBool . &x)
196+
> idB = (\x: TBool . &x)
203197

204198
`idBB = \x:Bool->Bool. x`
205199

@@ -418,6 +412,7 @@ with the function given above.
418412
> (([ x := s ] t) = t') <-> Substi s x t t'
419413
> substi_correct s x t t' = ?substi_correct_rhs1
420414

415+
$\square$
421416

422417
== Reduction
423418

@@ -595,12 +590,11 @@ Proof. normalize. Qed.
595590

596591
==== Exercise: 2 stars (step_example5)
597592

598-
Try to do this one both with and without `normalize`.
599-
600593
> step_example5 :
601594
> (Stlc.idBBBB # Stlc.idBB) # Stlc.idB ->>* Stlc.idB
602595
> step_example5 = ?step_example5_rhs
603596

597+
$\square$
604598

605599
=== Typing
606600

@@ -617,7 +611,7 @@ what assumptions we should make about the types of its free
617611
variables.
618612

619613
This leads us to a three-place _typing judgment_, informally
620-
written `Gamma |- t \in T`, where `Gamma` is a
614+
written `Gamma |- t ::T`, where `Gamma` is a
621615
"typing context" -- a mapping from variables to their types.
622616

623617
Following the usual notation for partial maps, we could write `Gamma
@@ -634,47 +628,47 @@ Following the usual notation for partial maps, we could write `Gamma
634628
\[
635629
\begin{prooftree}
636630
\hypo{\idr{Gamma x = T}}
637-
\infer1[\idr{T_Var}]{\idr{Gamma |- x \in T}}
631+
\infer1[\idr{T_Var}]{\idr{Gamma |- x ::T}}
638632
\end{prooftree}
639633
\]
640634
641635
\[
642636
\begin{prooftree}
643-
\hypo{\idr{Gamma & {{ x --> T11 }} |- t12 \in T12}}
644-
\infer1[\idr{T_Abs}]{\idr{Gamma |- \x:T11.t12 \in T11->T12}}
637+
\hypo{\idr{Gamma & {{ x --> T11 }} |- t12 :: T12}}
638+
\infer1[\idr{T_Abs}]{\idr{Gamma |- \x:T11.t12 ::T11->T12}}
645639
\end{prooftree}
646640
\]
647641
648642
\[
649643
\begin{prooftree}
650-
\hypo{\idr{Gamma |- t1 \in T11->T12}}
651-
\hypo{\idr{Gamma |- t2 \in T11}}
652-
\infer2[\idr{T_App}]{\idr{Gamma |- t1 t2 \in T12}}
644+
\hypo{\idr{Gamma |- t1 ::T11->T12}}
645+
\hypo{\idr{Gamma |- t2 ::T11}}
646+
\infer2[\idr{T_App}]{\idr{Gamma |- t1 t2 ::T12}}
653647
\end{prooftree}
654648
\]
655649
656650
\[
657651
\begin{prooftree}
658-
\infer0[\idr{T_True}]{\idr{Gamma |- true \in Bool}}
652+
\infer0[\idr{T_True}]{\idr{Gamma |- true ::Bool}}
659653
\end{prooftree}
660654
\]
661655
662656
\[
663657
\begin{prooftree}
664-
\infer0[\idr{T_False}]{\idr{Gamma |- false \in Bool}}
658+
\infer0[\idr{T_False}]{\idr{Gamma |- false ::Bool}}
665659
\end{prooftree}
666660
\]
667661
668662
\[
669663
\begin{prooftree}
670-
\hypo{\idr{Gamma |- t1 \in Bool}}
671-
\hypo{\idr{Gamma |- t2 \in T}}
672-
\hypo{\idr{Gamma |- t3 \in T}}
673-
\infer3[\idr{T_If}]{\idr{Gamma |- if t1 then t2 else t3 \in T}}
664+
\hypo{\idr{Gamma |- t1 ::Bool}}
665+
\hypo{\idr{Gamma |- t2 ::T}}
666+
\hypo{\idr{Gamma |- t3 ::T}}
667+
\infer3[\idr{T_If}]{\idr{Gamma |- if t1 then t2 else t3 ::T}}
674668
\end{prooftree}
675669
\]
676670
677-
We can read the three-place relation `Gamma |- t \in T` as:
671+
We can read the three-place relation `Gamma |- t ::T` as:
678672
"under the assumptions in Gamma, the term `t` has the type `T`." *)
679673
680674
> syntax [context] "|-" [t] "::" [T] "." = Has_type context t T
@@ -687,7 +681,7 @@ We can read the three-place relation `Gamma |- t \in T` as:
687681
> (Gamma & {{ (MkId x) ==> T11 }}) |- t12 :: T12 . ->
688682
> Gamma |- (Tabs x T11 t12) :: (T11 :=> T12) .
689683
> T_App : {Gamma: Context} -> {T11, T12: Ty} -> {t1, t2 : Tm} ->
690-
> Gamma |- t1 :: (T11 :=> T12) . ->
684+
> Gamma |- t1 :: (T11 :=> T12). ->
691685
> Gamma |- t2 :: T11 . ->
692686
> Gamma |- (t1 # t2) :: T12 .
693687
> T_True : {Gamma: Context} ->
@@ -710,15 +704,16 @@ Another example:
710704
711705
```
712706
empty |- \x:A. \y:A->A. y (y x)
713-
\in A -> (A->A) -> A.
707+
::A -> (A->A) -> A.
714708
```
715709
716710
> typing_example_2 : empty |-
717711
> (Tabs "x" TBool
718712
> (Tabs "y" (TBool :=> TBool)
719713
> (Tvar "y" # Tvar "y" # Tvar "x"))) ::
720714
> (TBool :=> (TBool :=> TBool) :=> TBool) .
721-
> typing_example_2 = T_Abs (T_Abs (T_App (T_Var Refl) (T_App (T_Var Refl) (T_Var Refl))))
715+
> typing_example_2 =
716+
> T_Abs (T_Abs (T_App (T_Var Refl) (T_App (T_Var Refl) (T_Var Refl))))
722717
723718
724719
==== Exercise: 2 stars (typing_example_3)
@@ -728,59 +723,54 @@ Formally prove the following typing derivation holds:
728723
```
729724
empty |- \x:Bool->B. \y:Bool->Bool. \z:Bool.
730725
y (x z)
731-
\in T.
726+
::T.
732727
```
733728
734-
> typing_example_3 : (T : Ty **
735-
> empty |-
736-
> (Tabs x (TBool :=> TBool)
737-
> (Tabs y (TBool :=> TBool)
738-
> (Tabs z TBool
739-
> (Tvar y # (Tvar x # Tvar z))))) :: T . )
729+
> typing_example_3 :
730+
> (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 . )
740735
> typing_example_3 = ?typing_example_3_rhs
741736
737+
$\square$
738+
742739
We can also show that terms are _not_ typable. For example, let's
743740
formally check that there is no typing derivation assigning a type
744741
to the term `\x:Bool. \y:Bool, x y` -- i.e.,
745742
746743
```
747744
~ exists T,
748-
empty |- \x:Bool. \y:Bool, x y \in T.
745+
empty |- \x:Bool. \y:Bool, x y ::T.
749746
```
750747
751-
Example typing_nonexample_1 :
752-
~ exists T,
753-
empty |-
754-
(tabs x TBool
755-
(tabs y TBool
756-
(tapp (tvar x) (tvar y)))) \in
757-
T.
758-
Proof.
759-
intros Hc. inversion Hc.
760-
(* The `clear` tactic is useful here for tidying away bits of
761-
the context that we're not going to need again. *)
762-
inversion H. subst. clear H.
763-
inversion H5. subst. clear H5.
764-
inversion H4. subst. clear H4.
765-
inversion H2. subst. clear H2.
766-
inversion H5. subst. clear H5.
767-
inversion H1. Qed.
768-
769-
(** **** Exercise: 3 stars, optional (typing_nonexample_3) *)
770-
(** Another nonexample:
771-
772-
~ (exists S, exists T,
773-
empty |- \x:S. x x \in T).
774-
*)
775-
776-
Example typing_nonexample_3 :
777-
~ (exists S, exists T,
778-
empty |-
779-
(tabs x S
780-
(tapp (tvar x) (tvar x))) \in
781-
T).
782-
Proof.
783-
(* FILL IN HERE *) Admitted.
784-
(** `` *)
785-
786-
End STLC.
748+
> forallToExistence : {X : Type} -> {P: X -> Type} ->
749+
> ((a : X) -> Not (P a)) -> Not (a : X ** P a)
750+
> forallToExistence hyp (b ** p2) = hyp b p2
751+
752+
> typing_nonexample_1 :
753+
> Not (T : Ty **
754+
> empty |-
755+
> (Tabs "x" TBool
756+
> (Tabs "y" TBool
757+
> (Tvar "x" # Tvar y))) :: T . )
758+
> typing_nonexample_1 = forallToExistence
759+
> (\ a , (T_Abs (T_Abs (T_App (T_Var Refl)(T_Var Refl)))) impossible)
760+
761+
==== Exercise: 3 stars, optional (typing_nonexample_3)
762+
763+
Another nonexample:
764+
765+
``` ~ (exists S, exists T,
766+
empty |- \x:S. x x ::T).
767+
```
768+
769+
> typing_nonexample_3 :
770+
> Not (s : Ty ** t : Ty **
771+
> empty |-
772+
> (Tabs "x" s
773+
> (Tvar "x" # Tvar "x")) :: t . )
774+
> typing_nonexample_3 = ?typing_nonexample_3_rhs
775+
776+
$\square$

0 commit comments

Comments
 (0)