|
|
|
@ -69,19 +69,38 @@ Definition list_t (A : type) : type :=
|
|
|
|
|
|
|
|
|
|
Definition mystack : val :=
|
|
|
|
|
(* define your stack implementation, assuming "lc" is a list implementation *)
|
|
|
|
|
λ: "lc", #0. (* FIXME *)
|
|
|
|
|
|
|
|
|
|
(*
|
|
|
|
|
mystack = λ lc, {
|
|
|
|
|
new := λ_, new lc.nil,
|
|
|
|
|
push := λ stack, λ elem, stack <- lc.cons elem !stack; (),
|
|
|
|
|
pop := λ stack,
|
|
|
|
|
let res = lc.case !stack (Inj₁ ()) (λ head, λ rest, (Inj₂ head)) in
|
|
|
|
|
stack <- lc.case !stack lc.nil (λ head, λ rest, rest);
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
*)
|
|
|
|
|
λ: "lc", (
|
|
|
|
|
λ: BAnon, new (Fst (Fst "lc")),
|
|
|
|
|
λ: "stack" "elem", Snd ("stack" <- (Snd (Fst "lc")) "elem" !"stack", (Lit LitUnit)),
|
|
|
|
|
λ: "stack",
|
|
|
|
|
let: "res" := (Snd "lc")<> !"stack" (InjL (Lit LitUnit)) (λ: "head" "rest", (InjR "head")) in
|
|
|
|
|
Snd (
|
|
|
|
|
"stack" <- (Snd "lc")<> !"stack" (Fst (Fst "lc")) (λ: "head" "rest", "rest"),
|
|
|
|
|
"res"
|
|
|
|
|
)
|
|
|
|
|
).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Definition make_mystack : val :=
|
|
|
|
|
Λ, λ: "lc",
|
|
|
|
|
unpack "lc" as "lc" in
|
|
|
|
|
#0. (* FIXME *)
|
|
|
|
|
pack (mystack "lc").
|
|
|
|
|
|
|
|
|
|
Lemma make_mystack_typed Σ n Γ :
|
|
|
|
|
TY Σ; n; Γ ⊢ make_mystack : (∀: list_t #0 → stack_t #0).
|
|
|
|
|
Proof.
|
|
|
|
|
repeat solve_typing_fast.
|
|
|
|
|
Admitted.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(** Exercise 2 (LN Exercise 46): Obfuscated code *)
|
|
|
|
@ -104,24 +123,172 @@ Proof.
|
|
|
|
|
+ apply IH; done.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
(* Woah great lemma *)
|
|
|
|
|
Lemma rtc_contextual_step_fill' K e₁ e₂ e₃ e₄ h h' :
|
|
|
|
|
rtc contextual_step (e₂, h) (e₃, h') →
|
|
|
|
|
e₁ = fill K e₂ →
|
|
|
|
|
e₄ = fill K e₃ →
|
|
|
|
|
rtc contextual_step (e₁, h) (e₄, h').
|
|
|
|
|
Proof.
|
|
|
|
|
intros H -> ->.
|
|
|
|
|
exact (rtc_contextual_step_fill K _ _ _ _ H).
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
(* You may use the [new_fresh] and [init_heap_singleton] lemmas to allocate locations *)
|
|
|
|
|
|
|
|
|
|
(*
|
|
|
|
|
E := let x = new (λx : int. x + x) in
|
|
|
|
|
let f = (λg : int -> int. let f = !x in x <- g; f 11) in
|
|
|
|
|
f (λx : int. f (λy : int. x)) + f (λx : int. x + 9)
|
|
|
|
|
|
|
|
|
|
~> λx: int, x + x ∈ [xl]
|
|
|
|
|
let f = (λg : int -> int. let h = !xl in xl <- g; h 11) in
|
|
|
|
|
f (λz : int. f (λy : int. z)) + f (λz : int. z + 9)
|
|
|
|
|
|
|
|
|
|
~> λx : int, x + x ∈ [xl]
|
|
|
|
|
(λg : int -> int. let h = !xl in xl <- g; h 11)
|
|
|
|
|
(λz : int. (λg : int -> int. let h = !xl in xl <- g; h 11) (λy : int. z))
|
|
|
|
|
+ (λg : int -> int. let h = !xl in xl <- g; h 11) (λz : int. z + 9)
|
|
|
|
|
|
|
|
|
|
~>* λx : int, x + x ∈ [xl]
|
|
|
|
|
(λg : int -> int. let h = !xl in xl <- g; h 11)
|
|
|
|
|
(λz : int. (λg : int -> int. let h = !xl in xl <- g; h 11) (λy : int. z))
|
|
|
|
|
+ (xl <- (λz : int. z + 9); (λx, x + x) 11)
|
|
|
|
|
|
|
|
|
|
~>* (λz : int. z + 9) ∈ [xl]
|
|
|
|
|
(λg : int -> int. let h = !xl in xl <- g; h 11)
|
|
|
|
|
(λz : int. (λg : int -> int. let h = !xl in xl <- g; h 11) (λy : int. z))
|
|
|
|
|
+ 22
|
|
|
|
|
|
|
|
|
|
~> (λz : int. z + 9) ∈ [xl]
|
|
|
|
|
(xl <- (λz : int. (λg : int -> int. let h = !xl in xl <- g; h 11) (λy : int. z));
|
|
|
|
|
(λz : int. z + 9) 11)
|
|
|
|
|
+ 22
|
|
|
|
|
|
|
|
|
|
~>* (λz : int. (λg : int -> int. let h = !xl in xl <- g; h 11) (λy : int. z)) ∈ [xl]
|
|
|
|
|
20 + 22
|
|
|
|
|
|
|
|
|
|
~> 42 ?
|
|
|
|
|
*)
|
|
|
|
|
|
|
|
|
|
Ltac beta := (
|
|
|
|
|
eapply rtc_l; first (
|
|
|
|
|
eapply base_contextual_step;
|
|
|
|
|
eapply BetaS; [ auto | reflexivity ];
|
|
|
|
|
simpl
|
|
|
|
|
)
|
|
|
|
|
).
|
|
|
|
|
|
|
|
|
|
Lemma obf_expr_eval :
|
|
|
|
|
∃ h', rtc contextual_step (obf_expr, ∅) (of_val #0 (* FIXME: what is the result? *), h').
|
|
|
|
|
∃ h', rtc contextual_step (obf_expr, ∅) (of_val #42, h').
|
|
|
|
|
Proof.
|
|
|
|
|
eexists. unfold obf_expr.
|
|
|
|
|
(* TODO: exercise *)
|
|
|
|
|
Admitted.
|
|
|
|
|
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply (Ectx_step [AppRCtx _]); [reflexivity | reflexivity | ].
|
|
|
|
|
have h₁ : of_val (λ: "x", "x" + "x") = (λ: "x", "x" + "x")%E. reflexivity.
|
|
|
|
|
rewrite <-h₁.
|
|
|
|
|
apply new_fresh.
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
beta.
|
|
|
|
|
|
|
|
|
|
beta.
|
|
|
|
|
|
|
|
|
|
etransitivity.
|
|
|
|
|
eapply (rtc_contextual_step_fill' [BinOpRCtx PlusOp _]); [ | simpl; reflexivity | simpl; reflexivity ].
|
|
|
|
|
{
|
|
|
|
|
beta.
|
|
|
|
|
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply (Ectx_step [AppRCtx _]); [reflexivity | reflexivity | ].
|
|
|
|
|
eapply LoadS.
|
|
|
|
|
auto.
|
|
|
|
|
|
|
|
|
|
beta.
|
|
|
|
|
|
|
|
|
|
(* Store *)
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply (Ectx_step [AppRCtx _]); [reflexivity | reflexivity | ].
|
|
|
|
|
econstructor; [ eauto | eauto ].
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
beta.
|
|
|
|
|
|
|
|
|
|
beta.
|
|
|
|
|
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply base_contextual_step.
|
|
|
|
|
eapply BinOpS; [ auto | auto | auto ].
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
have h₂ : (Lit (Z.add 11 11)) = of_val (LitV 22).
|
|
|
|
|
reflexivity.
|
|
|
|
|
rewrite h₂.
|
|
|
|
|
reflexivity.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
etransitivity.
|
|
|
|
|
eapply (rtc_contextual_step_fill' [BinOpLCtx PlusOp (LitV 22)]); [ | reflexivity | reflexivity ].
|
|
|
|
|
{
|
|
|
|
|
beta.
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply (Ectx_step [AppRCtx _]); [reflexivity | reflexivity | ].
|
|
|
|
|
eapply LoadS.
|
|
|
|
|
auto.
|
|
|
|
|
|
|
|
|
|
beta.
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply (Ectx_step [AppRCtx _]); [reflexivity | reflexivity | ].
|
|
|
|
|
eapply StoreS; [ auto | auto ].
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
beta.
|
|
|
|
|
simpl.
|
|
|
|
|
beta.
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply base_contextual_step.
|
|
|
|
|
eapply BinOpS; [ auto | auto | auto ].
|
|
|
|
|
simpl.
|
|
|
|
|
|
|
|
|
|
reflexivity.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eapply rtc_l.
|
|
|
|
|
eapply base_contextual_step.
|
|
|
|
|
eapply BinOpS; [ auto | auto | auto ].
|
|
|
|
|
simpl.
|
|
|
|
|
reflexivity.
|
|
|
|
|
Qed.
|
|
|
|
|
|
|
|
|
|
(** Exercise 4 (LN Exercise 48): Fibonacci *)
|
|
|
|
|
|
|
|
|
|
(*
|
|
|
|
|
Idea: use the heap to get recursivity
|
|
|
|
|
|
|
|
|
|
fibonacci := λ n,
|
|
|
|
|
let rec := new (λ m, 0) in
|
|
|
|
|
rec <- λ m, if m <= 1 then m else (!rec (m-1)) + (!rec (m-2)) end;
|
|
|
|
|
!rec n
|
|
|
|
|
*)
|
|
|
|
|
|
|
|
|
|
Definition fibonacci : val := λ: "n",
|
|
|
|
|
let: "rec" := new (λ: "m", #0) in
|
|
|
|
|
Snd (Pair
|
|
|
|
|
("rec" <- (λ: "m", if: "m" ≤ #1 then "m" else (!"rec" ("m" - #1)) + (!"rec" ("m" - #2))))
|
|
|
|
|
(!"rec" "n")
|
|
|
|
|
)
|
|
|
|
|
.
|
|
|
|
|
|
|
|
|
|
Definition fibonacci : val := #0. (* FIXME *)
|
|
|
|
|
|
|
|
|
|
Lemma fibonacci_typed Σ n Γ :
|
|
|
|
|
TY Σ; n; Γ ⊢ fibonacci : (Int → Int).
|
|
|
|
|
Proof.
|
|
|
|
|
repeat solve_typing_fast.
|
|
|
|
|
Admitted.
|
|
|
|
|
Qed.
|
|
|
|
|