Start exercises 6

amethyst
Shad Amethyst 11 months ago
parent 3b3c8c67e7
commit 28c21e9e93

@ -19,18 +19,20 @@ Implicit Types
You may use the results from this exercise, in particular the fixpoint combinator and its typing, in other exercises, however (which is why it comes first in this Coq file). You may use the results from this exercise, in particular the fixpoint combinator and its typing, in other exercises, however (which is why it comes first in this Coq file).
*) *)
Section recursion_combinator. Section recursion_combinator.
Variable (f x: string). (* the template of the recursive function *) Variable (f F x: string). (* the template of the recursive function *)
Hypothesis (Hfx: f x). Hypothesis (Hfx: f x).
Hypothesis (HFx: F x).
Hypothesis (HfF: f F).
(** Recursion Combinator *) (** Recursion Combinator *)
(* First, setup a definition [Rec] satisfying the reduction lemmas below. *) (* First, setup a definition [Rec] satisfying the reduction lemmas below. *)
(** You may find an auxiliary definition [rec_body] helpful *) (** You may find an auxiliary definition [rec_body] helpful *)
Definition rec_body (t: expr) : expr := Definition rec_body (t : expr): expr :=
roll (λ: f x, #0). (* TODO *) roll (λ: f, t (λ: x, (unroll f) f x)).
Definition Rec (t: expr): val := Definition Rec (t: expr): val :=
λ: x, rec_body t. (* TODO *) λ: x, (unroll (rec_body t)) (rec_body t) x.
Lemma closed_rec_body t : Lemma closed_rec_body t :
is_closed [] t is_closed [] (rec_body t). is_closed [] t is_closed [] (rec_body t).
@ -42,8 +44,30 @@ Section recursion_combinator.
is_val (Rec t). is_val (Rec t).
Proof. done. Qed. Proof. done. Qed.
Lemma bnamed_eq {A : Type} {a b : string} {g h : A} :
(if decide (BNamed a = BNamed b) then g else h) = if decide (a = b) then g else h.
Proof.
induction (decide (a = b)) as [H | H].
rewrite H.
rewrite decide_True; reflexivity.
rewrite decide_False; first reflexivity.
intro H2.
injection H2.
tauto.
Qed.
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.
(*
(Rec t) e = (λx. (unroll (rec' t)) (rec' t) x) e
~> (unroll (rec' t)) (rec' t) e
~> (λ f, t (λ x, (unroll f) f x)) (rec' t) e
~> (t (λ x, (unroll (rec' t)) (rec' t) x)) e
= (t (Rec t) e)
Do not attempt to understand what is going on in the proof, this clearly isn't meant to be solved by humans.
*)
Lemma Rec_red (t e: expr): Lemma Rec_red (t e: expr):
is_val e is_val e
is_val t is_val t
@ -51,10 +75,55 @@ Section recursion_combinator.
is_closed [] t is_closed [] t
rtc contextual_step ((Rec t) e) (t (Rec t) e). rtc contextual_step ((Rec t) e) (t (Rec t) e).
Proof. Proof.
(* TODO: exercise *) have Hxf : x f.
Admitted. symmetry; assumption.
intros.
eapply rtc_l.
eapply base_contextual_step.
econstructor; [assumption | reflexivity].
simpl.
repeat rewrite bnamed_eq.
repeat (rewrite (decide_False (P := x = f)); last auto).
repeat (rewrite (decide_True (P := x = x)); last reflexivity).
rewrite subst_is_closed_nil; last assumption.
is_val_to_val v H; subst.
eapply rtc_l.
eapply (Ectx_step _ _ [
AppLCtx (RollV (λ: (BNamed f),
App t (λ: (BNamed x),
App (App (unroll Var f) (Var f)) (Var x)
))
);
AppLCtx v
]).
reflexivity.
reflexivity.
econstructor.
eauto.
simpl.
eapply rtc_l.
eapply (Ectx_step _ _ [
AppLCtx v
]).
reflexivity.
reflexivity.
econstructor.
eauto.
reflexivity.
simpl.
repeat rewrite bnamed_eq.
repeat (rewrite (decide_False (P := f = x)); last auto).
repeat (rewrite (decide_True (P := f = f)); last reflexivity).
rewrite subst_is_closed_nil; last assumption.
reflexivity.
Qed.
Search (?A <[?f := ?x]> ?A).
Lemma rec_body_typing n Γ (A B: type) t : Lemma rec_body_typing n Γ (A B: type) t :
Γ !! x = None Γ !! x = None
Γ !! f = None Γ !! f = None
@ -63,8 +132,20 @@ Section recursion_combinator.
TY n; Γ t : ((A B) (A B)) TY n; Γ t : ((A B) (A B))
TY n; Γ rec_body t : (μ: #0 rename (+1) A rename (+1) B). TY n; Γ rec_body t : (μ: #0 rename (+1) A rename (+1) B).
Proof. Proof.
(* TODO: exercise *) intros x_notin_Γ f_notin_Γ A_wf B_wf Hty_t.
Admitted. unfold rec_body.
solve_typing.
simpl.
asimpl.
eapply typed_weakening; [exact Hty_t | simplify_list_subseteq | reflexivity ].
(* Love the consistency in the naming here: *)
apply insert_subseteq; assumption.
asimpl; apply lookup_insert.
apply type_wf_rename; [ assumption | intros; simpl; lia ].
apply type_wf_rename; [ assumption | intros; simpl; lia ].
Qed.
Lemma Rec_typing n Γ A B t: Lemma Rec_typing n Γ A B t:
@ -75,6 +156,8 @@ Section recursion_combinator.
TY n; Γ t : ((A B) (A B)) TY n; Γ t : ((A B) (A B))
TY n; Γ (Rec t) : (A B). TY n; Γ (Rec t) : (A B).
Proof. Proof.
intros.
(* TODO: exercise *) (* TODO: exercise *)
Admitted. Admitted.
@ -128,24 +211,34 @@ Admitted.
(** ** Exercise 1: Encode arithmetic expressions *) (** ** Exercise 1: Encode arithmetic expressions *)
Definition aexpr : type := #0 (* TODO *). (*
int, addition, multiplication:
(int + (aexpr × aexpr) + (aexpr × aexpr))
Definition num_val (v : val) : val := #0 (* TODO *). (int + ((μα. α × α) + (μα. α × α)))
Definition num_expr (e : expr) : expr := #0 (* TODO *). *)
Definition aexpr : type := (μ:
(Int + (
(#0 × #0)
+ (#0 × #0)
)))
.
Definition plus_val (v1 v2 : val) : val := #0 (* TODO *). Definition num_val (v : val) : val := RollV (InjLV v).
Definition plus_expr (e1 e2 : expr) : expr := #0 (* TODO *). Definition num_expr (e : expr) : expr := Roll (InjL e).
Definition mul_val (v1 v2 : val) : val := #0 (* TODO *). Definition plus_val (v1 v2 : val) : val := RollV (InjRV (InjLV (PairV v1 v2))).
Definition mul_expr (e1 e2 : expr) : expr := #0 (* TODO *). Definition plus_expr (e1 e2 : expr) : expr := Roll (InjR (InjL (Pair e1 e2))).
Definition mul_val (v1 v2 : val) : val := RollV (InjRV (InjRV (PairV v1 v2))).
Definition mul_expr (e1 e2 : expr) : expr := Roll (InjR (InjR (Pair e1 e2))).
Lemma num_expr_typed n Γ e : Lemma num_expr_typed n Γ e :
TY n; Γ e : Int TY n; Γ e : Int
TY n; Γ num_expr e : aexpr. TY n; Γ num_expr e : aexpr.
Proof. Proof.
intros. solve_typing. intros. solve_typing.
(* TODO: exercise *) Qed.
Admitted.
Lemma plus_expr_typed n Γ e1 e2 : Lemma plus_expr_typed n Γ e1 e2 :
@ -153,9 +246,8 @@ Lemma plus_expr_typed n Γ e1 e2 :
TY n; Γ e2 : aexpr TY n; Γ e2 : aexpr
TY n; Γ plus_expr e1 e2 : aexpr. TY n; Γ plus_expr e1 e2 : aexpr.
Proof. Proof.
(*intros; solve_typing.*) intros; solve_typing.
(* TODO: exercise *) Qed.
Admitted.
Lemma mul_expr_typed n Γ e1 e2 : Lemma mul_expr_typed n Γ e1 e2 :
@ -163,19 +255,27 @@ Lemma mul_expr_typed n Γ e1 e2 :
TY n; Γ e2 : aexpr TY n; Γ e2 : aexpr
TY n; Γ mul_expr e1 e2 : aexpr. TY n; Γ mul_expr e1 e2 : aexpr.
Proof. Proof.
(*intros; solve_typing.*) intros; solve_typing.
(* TODO: exercise *) Qed.
Admitted.
Definition eval_aexpr : val := Definition eval_aexpr : val := (fix: "eval" "x" := (
#0. (* TODO *) match: (Unroll "x") with
InjL "int" => "int"
| InjR "bin" => (match: "bin" with
InjL "add" => "eval" (Fst "add") + "eval" (Snd "add")
| InjR "mul" => "eval" (Fst "mul") * "eval" (Snd "mul")
end
)
end
)).
Lemma eval_aexpr_typed Γ n : Lemma eval_aexpr_typed Γ n :
TY n; Γ eval_aexpr : (aexpr Int). TY n; Γ eval_aexpr : (aexpr Int).
Proof. Proof.
(* TODO: exercise *) apply fix_typing; [solve_typing | solve_typing | auto | ].
Admitted. solve_typing.
Qed.
@ -187,9 +287,18 @@ Definition list_t (A : type) : type :=
× (: #1 #0 (A.[ren (+2)] #1 #0) #0)) (* mylistcase *) × (: #1 #0 (A.[ren (+2)] #1 #0) #0)) (* mylistcase *)
. .
Definition mylist_impl : val := Definition mylist_impl : val := PackV (
#0 (* TODO *) (#0, (LitV LitUnit)),
. (λ: "a" "l", (#1 + (Fst "l"), ("a", (Snd "")))),
(Λ, λ: "l" "zero" "cb",
(* (fix "rec" "l" := *)
if: (Fst "l") = #0 then
"zero"
else
"cb" (Fst (Snd "l")) (Snd (Snd "l"))
(* ) "l1" *)
)
).
@ -199,4 +308,3 @@ Lemma mylist_impl_sem_typed A :
Proof. Proof.
(* TODO: exercise *) (* TODO: exercise *)
Admitted. Admitted.

@ -983,20 +983,72 @@ Proof.
eapply sem_val_expr_rel. eapply val_rel_mono; last done. lia. eapply sem_val_expr_rel. eapply val_rel_mono; last done. lia.
Qed. Qed.
(*
*)
Lemma compat_injl Δ Γ e A B : Lemma compat_injl Δ Γ e A B :
TY Δ; Γ e : A TY Δ; Γ e : A
TY Δ; Γ InjL e : A + B. TY Δ; Γ InjL e : A + B.
Proof. Proof.
(* TODO: exercise *) intro Hty.
Admitted. destruct Hty as (Hcl & Hty).
econstructor.
simpl; assumption.
intros θ δ k Hctx.
specialize (Hty θ δ k Hctx).
simpl.
eapply (bind [InjLCtx]).
exact Hty.
intros j v j_le_k Hv.
simpl.
have h : of_val (InjLV v) = InjL (of_val v).
{
reflexivity.
}
rewrite <-h.
eapply sem_val_expr_rel.
simp type_interp.
left.
eexists; split; first reflexivity.
exact Hv.
Qed.
Lemma compat_injr Δ Γ e A B : Lemma compat_injr Δ Γ e A B :
TY Δ; Γ e : B TY Δ; Γ e : B
TY Δ; Γ InjR e : A + B. TY Δ; Γ InjR e : A + B.
Proof. Proof.
(* TODO: exercise *) intro Hty.
Admitted. destruct Hty as (Hcl & Hty).
econstructor.
simpl; assumption.
intros θ δ k Hctx.
specialize (Hty θ δ k Hctx).
simpl.
eapply (bind [InjRCtx]).
exact Hty.
intros j v j_le_k Hv.
simpl.
have h : of_val (InjRV v) = InjR (of_val v).
{
reflexivity.
}
rewrite <-h.
eapply sem_val_expr_rel.
simp type_interp.
right.
eexists; split; first reflexivity.
exact Hv.
Qed.
Lemma compat_case Δ Γ e e1 e2 A B C : Lemma compat_case Δ Γ e e1 e2 A B C :
@ -1005,8 +1057,71 @@ Lemma compat_case Δ Γ e e1 e2 A B C :
TY Δ; Γ e2 : (C A) TY Δ; Γ e2 : (C A)
TY Δ; Γ Case e e1 e2 : A. TY Δ; Γ Case e e1 e2 : A.
Proof. Proof.
(* TODO: exercise *) intros Hty Hty Hty.
Admitted. destruct Hty as (Hcl & Hty).
remember Hty as Hty'; clear HeqHty'.
destruct Hty as (Hcl & Hty).
destruct Hty as (Hcl & Hty).
econstructor.
simpl.
apply andb_True; split; [ apply andb_True; split | ]; assumption.
intros θ δ k Hctx.
specialize (Hty _ _ _ Hctx).
simpl.
eapply (bind [CaseCtx _ _]).
exact Hty.
simpl.
intros j v j_le_k Hty'.
have Hctx' : 𝒢 δ (j-1) Γ θ.
eapply sem_context_rel_mono; last exact Hctx; lia.
simp type_interp in Hty'.
induction Hty' as [(v' & -> & Hty') | (v' & -> & Hty')].
- eapply expr_det_step_closure.
{
eapply det_step_casel.
eauto.
}
fold of_val.
specialize (Hty θ δ _ Hctx').
eapply (bind [AppLCtx _]); first exact Hty.
intros i v i_lt_j Hty.
simpl.
simp type_interp in Hty; destruct Hty as (x & e & -> & Hcl & Hty).
eapply expr_det_step_closure.
{
eapply det_step_beta; apply is_val_of_val.
}
apply Hty.
lia.
eapply val_rel_mono; last exact Hty'; lia.
- eapply expr_det_step_closure.
{
eapply det_step_caser.
eauto.
}
fold of_val.
specialize (Hty θ δ _ Hctx').
eapply (bind [AppLCtx _]); first exact Hty.
intros i v i_lt_j Hty.
simpl.
simp type_interp in Hty; destruct Hty as (x & e & -> & Hcl & Hty).
eapply expr_det_step_closure.
{
eapply det_step_beta; apply is_val_of_val.
}
apply Hty.
lia.
eapply val_rel_mono; last exact Hty'; lia.
Qed.
Lemma compat_roll n Γ e A : Lemma compat_roll n Γ e A :

Loading…
Cancel
Save