|
|
|
@ -47,7 +47,27 @@ Inductive syn_typed : typing_context → expr → type → Prop :=
|
|
|
|
|
Γ ⊢ e1 : Int →
|
|
|
|
|
Γ ⊢ e2 : Int →
|
|
|
|
|
Γ ⊢ e1 + e2 : Int
|
|
|
|
|
(* TODO: provide the new typing rules *)
|
|
|
|
|
| typed_pair Γ e_lhs e_rhs A B:
|
|
|
|
|
Γ ⊢ e_lhs : A →
|
|
|
|
|
Γ ⊢ e_rhs : B →
|
|
|
|
|
Γ ⊢ (Pair e_lhs e_rhs) : A × B
|
|
|
|
|
| typed_fst Γ e_pair A B:
|
|
|
|
|
Γ ⊢ e_pair: A × B →
|
|
|
|
|
Γ ⊢ (Fst e_pair): A
|
|
|
|
|
| typed_snd Γ e_pair A B:
|
|
|
|
|
Γ ⊢ e_pair: A × B →
|
|
|
|
|
Γ ⊢ (Snd e_pair): B
|
|
|
|
|
| typed_injl Γ e_inj A B:
|
|
|
|
|
Γ ⊢ e_inj : A →
|
|
|
|
|
Γ ⊢ (InjL e_inj) : A + B
|
|
|
|
|
| typed_injr Γ e_inj A B:
|
|
|
|
|
Γ ⊢ e_inj : B →
|
|
|
|
|
Γ ⊢ (InjR e_inj) : A + B
|
|
|
|
|
| typed_case Γ e_inj e_lhs e_rhs A B C:
|
|
|
|
|
Γ ⊢ e_inj: A + B →
|
|
|
|
|
Γ ⊢ e_lhs: (A → C) →
|
|
|
|
|
Γ ⊢ e_rhs: (B → C) →
|
|
|
|
|
Γ ⊢ (Case e_inj e_lhs e_rhs): C
|
|
|
|
|
where "Γ ⊢ e : A" := (syn_typed Γ e%E A%ty).
|
|
|
|
|
#[export] Hint Constructors syn_typed : core.
|
|
|
|
|
|
|
|
|
@ -60,8 +80,7 @@ Lemma syn_typed_closed Γ e A X :
|
|
|
|
|
(∀ x, x ∈ dom Γ → x ∈ X) →
|
|
|
|
|
is_closed X e.
|
|
|
|
|
Proof.
|
|
|
|
|
(* TODO: you will need to add the new cases, i.e. "|"'s to the intro pattern. The proof then should go through *)
|
|
|
|
|
induction 1 as [ | ?????? IH | | | | ] in X |-*; simpl; intros Hx; try done.
|
|
|
|
|
induction 1 as [ | ?????? IH | | | | | | | | | | ] in X |-*; simpl; intros Hx; try done.
|
|
|
|
|
{ (* var *) apply bool_decide_pack, Hx. apply elem_of_dom; eauto. }
|
|
|
|
|
{ (* lam *) apply IH.
|
|
|
|
|
intros y. rewrite elem_of_dom lookup_insert_is_Some.
|
|
|
|
@ -81,8 +100,7 @@ Lemma typed_weakening Γ Δ e A:
|
|
|
|
|
Γ ⊆ Δ →
|
|
|
|
|
Δ ⊢ e : A.
|
|
|
|
|
Proof.
|
|
|
|
|
(* TODO: here you will need to add the new cases to the intro pattern as well. The proof then should go through *)
|
|
|
|
|
induction 1 as [| Γ x e A B Htyp IH | | | | ] in Δ |-*; intros Hsub; eauto.
|
|
|
|
|
induction 1 as [| Γ x e A B Htyp IH | | | | | | | | | | ] in Δ |-*; intros Hsub; eauto.
|
|
|
|
|
- (* var *) econstructor. by eapply lookup_weaken.
|
|
|
|
|
- (* lam *) econstructor. eapply IH; eauto. by eapply insert_mono.
|
|
|
|
|
Qed.
|
|
|
|
@ -115,13 +133,54 @@ Proof. inversion 1; subst; eauto. Qed.
|
|
|
|
|
They will be very useful for the proofs below!
|
|
|
|
|
*)
|
|
|
|
|
|
|
|
|
|
Lemma pair_inversion Γ e_lhs e_rhs C:
|
|
|
|
|
Γ ⊢ (Pair e_lhs e_rhs) : C →
|
|
|
|
|
∃ A B, C = (A × B)%ty ∧ Γ ⊢ e_lhs : A ∧ Γ ⊢ e_rhs : B.
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; subst; eauto.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma fst_inversion Γ e_pair A:
|
|
|
|
|
Γ ⊢ (Fst e_pair) : A →
|
|
|
|
|
∃ B, Γ ⊢ e_pair : A × B.
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; subst; eauto.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma snd_inversion Γ e_pair B:
|
|
|
|
|
Γ ⊢ (Snd e_pair) : B →
|
|
|
|
|
∃ A, Γ ⊢ e_pair : A × B.
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; subst; eauto.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma injl_inversion Γ e_inj C:
|
|
|
|
|
Γ ⊢ (InjL e_inj): C →
|
|
|
|
|
∃ A B, C = (A + B)%ty ∧ Γ ⊢ e_inj: A.
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; subst; eauto.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma injr_inversion Γ e_inj C:
|
|
|
|
|
Γ ⊢ (InjR e_inj): C →
|
|
|
|
|
∃ A B, C = (A + B)%ty ∧ Γ ⊢ e_inj: B.
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; subst; eauto.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma case_inversion Γ e_inj e_lhs e_rhs C:
|
|
|
|
|
Γ ⊢ (Case e_inj e_lhs e_rhs): C →
|
|
|
|
|
∃ A B, Γ ⊢ e_inj: (A+B)%ty ∧ Γ ⊢ e_lhs: (A → C)%ty ∧ Γ ⊢ e_rhs: (B → C)%ty.
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; subst; eauto.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma typed_substitutivity e e' Γ (x: string) A B :
|
|
|
|
|
∅ ⊢ e' : A →
|
|
|
|
|
(<[x := A]> Γ) ⊢ e : B →
|
|
|
|
|
Γ ⊢ lang.subst x e' e : B.
|
|
|
|
|
Proof.
|
|
|
|
|
intros He'. revert B Γ; induction e as [y | y | | | | | | | | | ]; intros B Γ; simpl.
|
|
|
|
|
intros He'. revert B Γ; induction e as [y | y | | | | | | | | | ]; intros B Γ; simpl.
|
|
|
|
|
- intros Hp % var_inversion.
|
|
|
|
|
destruct (decide (x = y)).
|
|
|
|
|
+ subst. rewrite lookup_insert in Hp. injection Hp as ->.
|
|
|
|
@ -140,13 +199,23 @@ Proof.
|
|
|
|
|
- intros (C & Hty1 & Hty2) % app_inversion. eauto.
|
|
|
|
|
- inversion 1; subst; auto.
|
|
|
|
|
- intros (-> & Hty1 & Hty2)%plus_inversion; eauto.
|
|
|
|
|
- (* TODO *) admit.
|
|
|
|
|
- (* TODO *) admit.
|
|
|
|
|
- (* TODO *) admit.
|
|
|
|
|
- (* TODO *) admit.
|
|
|
|
|
- (* TODO *) admit.
|
|
|
|
|
- (* TODO *) admit.
|
|
|
|
|
Admitted.
|
|
|
|
|
- intros Hty%pair_inversion.
|
|
|
|
|
destruct Hty as (A' & B' & -> & Hty1 & Hty2).
|
|
|
|
|
eauto.
|
|
|
|
|
- intros (A' & Hty)%fst_inversion.
|
|
|
|
|
econstructor.
|
|
|
|
|
exact (IHe _ _ Hty).
|
|
|
|
|
- intros (B' & Hty)%snd_inversion; eauto.
|
|
|
|
|
- intros (A' & B' & -> & Hty)%injl_inversion.
|
|
|
|
|
econstructor.
|
|
|
|
|
exact (IHe _ _ Hty).
|
|
|
|
|
- intros (A' & B' & -> & Hty)%injr_inversion; eauto.
|
|
|
|
|
- intros (A' & B' & Hty_inj & Hty_lhs & Hty_rhs)%case_inversion.
|
|
|
|
|
econstructor.
|
|
|
|
|
apply IHe1; exact Hty_inj.
|
|
|
|
|
apply IHe2; exact Hty_lhs.
|
|
|
|
|
apply IHe3; exact Hty_rhs.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
(** Canonical values *)
|
|
|
|
|
Lemma canonical_values_arr Γ e A B:
|
|
|
|
@ -165,15 +234,37 @@ Proof.
|
|
|
|
|
inversion 1; simpl; naive_solver.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
(* TODO: add canonical forms lemmas for the new types *)
|
|
|
|
|
Ltac is_val_to_val v H :=
|
|
|
|
|
rewrite (is_val_spec _) in H; destruct H as [v H]; apply of_to_val in H; symmetry in H.
|
|
|
|
|
|
|
|
|
|
Lemma canonical_values_prod Γ e A B :
|
|
|
|
|
Γ ⊢ e : A × B →
|
|
|
|
|
is_val e →
|
|
|
|
|
∃ e1 e2, e = (e1, e2)%E ∧ is_val e1 ∧ is_val e2.
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; simpl; naive_solver.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma canonical_values_sum Γ e A B :
|
|
|
|
|
Γ ⊢ e : A + B →
|
|
|
|
|
is_val e →
|
|
|
|
|
(∃ e', e = InjL e' ∧ is_val e') ∨ (∃ e', e = InjR e' ∧ is_val e').
|
|
|
|
|
Proof.
|
|
|
|
|
inversion 1; simpl; naive_solver.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
(** Progress *)
|
|
|
|
|
Lemma typed_progress e A:
|
|
|
|
|
∅ ⊢ e : A → is_val e ∨ reducible e.
|
|
|
|
|
Proof.
|
|
|
|
|
remember ∅ as Γ.
|
|
|
|
|
(* TODO: you will need to extend the intro pattern *)
|
|
|
|
|
induction 1 as [| | | | Γ e1 e2 A B Hty IH1 _ IH2 | Γ e1 e2 Hty1 IH1 Hty2 IH2].
|
|
|
|
|
induction 1 as [| | | | Γ e1 e2 A B Hty IH1 _ IH2 | Γ e1 e2 Hty1 IH1 Hty2 IH2 | Γ ? ? ? ? Hty_lhs IH_lhs Hty_rhs IH_rhs |
|
|
|
|
|
Γ e A B Hty IH |
|
|
|
|
|
Γ e A B Hty IH |
|
|
|
|
|
Γ e A B Hty IH |
|
|
|
|
|
Γ e A B Hty IH |
|
|
|
|
|
Γ e e1 e2 A B C Htye IHe Htye1 IHe1 Htye2 IHe2
|
|
|
|
|
].
|
|
|
|
|
- subst. naive_solver.
|
|
|
|
|
- left. done.
|
|
|
|
|
- left. done.
|
|
|
|
@ -194,13 +285,47 @@ Proof.
|
|
|
|
|
subst. eexists; eapply base_contextual_step. eauto.
|
|
|
|
|
+ right. destruct H1 as [e1' Hstep]. eexists. eauto.
|
|
|
|
|
+ right. destruct H2 as [e2' H2]. eexists. eauto.
|
|
|
|
|
|
|
|
|
|
(* FIXME: prove the new cases *)
|
|
|
|
|
Admitted.
|
|
|
|
|
- destruct (IH_rhs HeqΓ) as [Hval_rhs | Hval_rhs];
|
|
|
|
|
first destruct (IH_lhs HeqΓ) as [Hval_lhs | Hval_lhs].
|
|
|
|
|
+ left. constructor; assumption.
|
|
|
|
|
+ right.
|
|
|
|
|
destruct Hval_lhs as [e_lhs' Hstep].
|
|
|
|
|
eexists; eapply contextual_step_pair_l.
|
|
|
|
|
assumption.
|
|
|
|
|
exact Hstep.
|
|
|
|
|
+ right.
|
|
|
|
|
destruct Hval_rhs as [e_rhs' Hstep].
|
|
|
|
|
eexists; exact (contextual_step_pair_r _ _ _ Hstep).
|
|
|
|
|
- (* fst *)
|
|
|
|
|
destruct (IH HeqΓ) as [H | H].
|
|
|
|
|
+ eapply canonical_values_prod in Hty as (e1 & e2 & -> & ? & ?); last done.
|
|
|
|
|
right. eexists. eapply base_contextual_step. eapply FstS; assumption.
|
|
|
|
|
+ right. destruct H as [e' H]. eexists. eauto.
|
|
|
|
|
- (* snd *)
|
|
|
|
|
destruct (IH HeqΓ) as [H | H].
|
|
|
|
|
+ eapply canonical_values_prod in Hty as (e1 & e2 & -> & ? & ?); last done.
|
|
|
|
|
right. eexists. eapply base_contextual_step. eapply SndS; assumption.
|
|
|
|
|
+ right. destruct H as [e' H]. eexists. eauto.
|
|
|
|
|
- (* injl *)
|
|
|
|
|
destruct (IH HeqΓ) as [H | H].
|
|
|
|
|
+ left. done.
|
|
|
|
|
+ right. destruct H as [e' H]. eexists. eauto.
|
|
|
|
|
- (* injr *)
|
|
|
|
|
destruct (IH HeqΓ) as [H | H].
|
|
|
|
|
+ left. done.
|
|
|
|
|
+ right. destruct H as [e' H]. eexists. eauto.
|
|
|
|
|
- (* case *)
|
|
|
|
|
right. destruct (IHe HeqΓ) as [H1|H1].
|
|
|
|
|
+ eapply canonical_values_sum in Htye as [(e' & -> & ?) | (e' & -> & ?)]; last done.
|
|
|
|
|
* eexists. eapply base_contextual_step. econstructor. done.
|
|
|
|
|
* eexists. eapply base_contextual_step. econstructor. done.
|
|
|
|
|
+ destruct H1 as [e' H1]. eexists. eauto.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Definition ectx_typing (K: ectx) (A B: type) :=
|
|
|
|
|
∀ e, ∅ ⊢ e : A → ∅ ⊢ (fill K e) : B.
|
|
|
|
|
|
|
|
|
|
(* This is actually a pretty strong lemma: if ⊢ K[e]: A, then ∃B, ⊢e:B and ∀e',⊢e':B→K[e']: A *)
|
|
|
|
|
Lemma fill_typing_decompose K e A:
|
|
|
|
|
∅ ⊢ fill K e : A →
|
|
|
|
|
∃ B, ∅ ⊢ e : B ∧ ectx_typing K B A.
|
|
|
|
@ -231,9 +356,27 @@ Proof.
|
|
|
|
|
injection Heq as -> ->.
|
|
|
|
|
eapply typed_substitutivity; eauto.
|
|
|
|
|
- eapply plus_inversion in Hty as (-> & Hty1 & Hty2). constructor.
|
|
|
|
|
|
|
|
|
|
(* TODO: extend this for the new cases *)
|
|
|
|
|
Admitted.
|
|
|
|
|
- eapply fst_inversion in Hty as (B & Hty).
|
|
|
|
|
eapply pair_inversion in Hty as (A' & B' & H_inj & H1 & H2).
|
|
|
|
|
injection H_inj; intros HA HB; subst.
|
|
|
|
|
assumption.
|
|
|
|
|
- eapply snd_inversion in Hty as (B & Hty).
|
|
|
|
|
eapply pair_inversion in Hty as (A' & B' & H_inj & H1 & H2).
|
|
|
|
|
injection H_inj; intros HA HB; subst.
|
|
|
|
|
assumption.
|
|
|
|
|
- eapply case_inversion in Hty as (A' & B' & Hty_inj & Hty_lhs & Hty_rhs).
|
|
|
|
|
eapply injl_inversion in Hty_inj as (A'' & B'' & Heq & Hty_inj).
|
|
|
|
|
injection Heq as -> ->.
|
|
|
|
|
eapply typed_app.
|
|
|
|
|
exact Hty_lhs.
|
|
|
|
|
exact Hty_inj.
|
|
|
|
|
- eapply case_inversion in Hty as (A' & B' & Hty_inj & Hty_lhs & Hty_rhs).
|
|
|
|
|
eapply injr_inversion in Hty_inj as (A'' & B'' & Heq & Hty_inj).
|
|
|
|
|
injection Heq as -> ->.
|
|
|
|
|
eapply typed_app.
|
|
|
|
|
exact Hty_rhs.
|
|
|
|
|
exact Hty_inj.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
Lemma typed_preservation e e' A:
|
|
|
|
|
∅ ⊢ e : A →
|
|
|
|
|