Skip to content

Experiment: using existential variables for Let and Call #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions CoqOfRust/core/convert/links/num.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ Module Impl_TryFrom_u64_for_usize.
}
{ constructor.
run_symbolic.
instantiate (1 := Result.Ok _).
with_strategy transparent [φ] reflexivity.
all: admit.
}
Defined.
Admitted.

Instance run : TryFrom.Run Self U64.t TryFromIntError.t := {
TryFrom.try_from := run_try_from;
Expand Down
4 changes: 2 additions & 2 deletions CoqOfRust/core/links/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Proof.
Qed.
Smpl Add eapply of_value_with_4 : of_value.

Definition of_value_4
Definition of_value_4
(value1' : Value.t)
(H_value1' : OfValue.t value1')
(value2' : Value.t) (value2 : OfValue.get_Set H_value1')
Expand All @@ -76,7 +76,7 @@ Proof.
eapply OfValue.Make with (A := t A {| Integer.value := 4 |}).
apply of_value_with_4; eassumption.
Defined.
Smpl Add eapply of_value_4 : of_value.
Smpl Add unshelve eapply of_value_4; try smpl of_value : of_value.

(** This lemma is useful when the [repeat_nat] construct (used to build array with repetition)
appears and to switch it with the [φ] on its parameters. *)
Expand Down
1 change: 0 additions & 1 deletion CoqOfRust/core/links/hint.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ Instance run_unreachable_unchecked :
Run.Trait hint.unreachable_unchecked [] [] [] Empty_set.
Proof.
constructor.
run_symbolic.
Admitted.
1 change: 1 addition & 0 deletions CoqOfRust/core/links/result.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Require Import CoqOfRust.CoqOfRust.
Require Import links.M.
Require Import core.result.

Module Result.
Inductive t {T E : Set} : Set :=
| Ok : T -> t
Expand Down
103 changes: 79 additions & 24 deletions CoqOfRust/links/M.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ Arguments Φ _ {_}.

Global Opaque φ.

Module WrappedLink.
Record t : Type := {
A : Set;
H : Link A;
}.
End WrappedLink.

Smpl Create of_ty.

Module OfTy.
Expand Down Expand Up @@ -877,6 +884,17 @@ Module Run.
{{ k (SuccessOrPanic.to_value value_inter) 🔽 R, Output }}
) ->
{{ LowM.CallClosure ty closure args k 🔽 R, Output }}
| CallClosureWrapped
(ty : Ty.t) (f : list Value.t -> M) (args : list Value.t) (k : Value.t + Exception.t -> M)
(wrapped_link : WrappedLink.t) :
let Output' : Set := wrapped_link.(WrappedLink.A) in
let _ := wrapped_link.(WrappedLink.H) in
let closure := Value.Closure (existS (_, _) f) in
{{ f args 🔽 Output', Output' }} ->
(forall (value_inter : SuccessOrPanic.t Output'),
{{ k (SuccessOrPanic.to_value value_inter) 🔽 R, Output }}
) ->
{{ LowM.CallClosure ty closure args k 🔽 R, Output }}
| CallLogicalOp
(op : LogicalOp.t) (lhs : bool) (rhs : M) (k : Value.t + Exception.t -> M) :
{{ rhs 🔽 R, bool }} ->
Expand All @@ -893,6 +911,16 @@ Module Run.
{{ k (Output.to_value value_inter) 🔽 R, Output }}
) ->
{{ LowM.Let ty e k 🔽 R, Output }}
| LetWrapped
(ty : Ty.t) (e : M) (k : Value.t + Exception.t -> M)
(wrapped_link : WrappedLink.t) :
let Output' : Set := wrapped_link.(WrappedLink.A) in
let _ := wrapped_link.(WrappedLink.H) in
{{ e 🔽 R, Output' }} ->
(forall (value_inter : Output.t R Output'),
{{ k (Output.to_value value_inter) 🔽 R, Output }}
) ->
{{ LowM.Let ty e k 🔽 R, Output }}
| Loop
(ty : Ty.t) (body : M) (k : Value.t + Exception.t -> M)
(of_ty : OfTy.t ty) :
Expand Down Expand Up @@ -1068,6 +1096,15 @@ Proof.
| H : forall _ : SuccessOrPanic.t Output', _ |- _ => apply (H output')
end.
}
{ (* CallClosureWrapped *)
eapply LowM.Call. {
exact (evaluate _ _ _ _ _ run).
}
intros output'; eapply evaluate.
match goal with
| H : forall _ : SuccessOrPanic.t Output', _ |- _ => apply (H output')
end.
}
{ (* CallLogicalOp *)
match goal with
| H : forall _ : Output.t _ bool, _ |- _ => rename H into H_k
Expand Down Expand Up @@ -1111,6 +1148,15 @@ Proof.
| H : forall _ : Output.t _ Output', _ |- _ => apply (H output')
end.
}
{ (* LetWrapped *)
eapply LowM.Let. {
exact (evaluate _ _ _ _ _ run).
}
intros output'; eapply evaluate.
match goal with
| H : forall _ : Output.t _ Output', _ |- _ => apply (H output')
end.
}
{ (* Loop *)
eapply LowM.Loop. {
exact (evaluate _ _ _ _ _ run).
Expand Down Expand Up @@ -1244,8 +1290,8 @@ Ltac run_symbolic_closure :=
].

Ltac run_symbolic_closure_auto :=
unshelve eapply Run.CallClosure; [
now repeat smpl of_ty |
unshelve eapply Run.CallClosureWrapped; [
econstructor; shelve |
try prepare_call;
(
(
Expand Down Expand Up @@ -1354,16 +1400,15 @@ Ltac rewrite_cast_integer :=
|]
end.

Ltac run_symbolic_let :=
unshelve eapply Run.Let; [repeat smpl of_ty | | cbn; intros []].

Ltac run_symbolic_loop :=
unshelve eapply Run.Loop; [
smpl of_ty |
|
cbn; intros []
].

Smpl Create run_symbolic.

Ltac run_symbolic_one_step_immediate :=
match goal with
| |- {{ _ 🔽 _, _ }} =>
Expand All @@ -1380,31 +1425,41 @@ Ltac run_symbolic_one_step_immediate :=
run_symbolic_get_trait_method ||
run_symbolic_closure_auto ||
run_symbolic_logical_op ||
run_symbolic_let ||
run_sub_pointer ||
run_symbolic_loop ||
fold @LowM.let_
fold @LowM.let_ ||
smpl run_symbolic
end.

Smpl Create run_symbolic.
Ltac run_symbolic_inner :=
(run_symbolic_one_step_immediate; run_symbolic_inner) ||
(unshelve eapply Run.LetWrapped; [
econstructor; shelve |
run_symbolic_inner |
cbn; intros [];
run_symbolic_inner
]) ||
match goal with
| |- context[match Output.Exception.to_exception ?exception with _ => _ end] =>
destruct exception; run_symbolic_inner
end ||
match goal with
| |- {{ ?expression 🔽 _, _ }} =>
match expression with
| context [match ?expression with _ => _ end] =>
destruct expression; run_symbolic_inner
end
end ||
(* We do our best to make progress, but can stop at any point to debug. *)
idtac.

(** We should use this tactic instead of the ones above, as this one calls all the others. *)
Ltac run_symbolic :=
progress (repeat (
run_symbolic_one_step_immediate ||
smpl run_symbolic ||
match goal with
| |- context[match Output.Exception.to_exception ?exception with _ => _ end] =>
destruct exception; run_symbolic
end ||
match goal with
| |- {{ ?expression 🔽 _, _ }} =>
match expression with
| context [match ?expression with _ => _ end] =>
destruct expression; run_symbolic
end
end
)).
(unshelve (progress run_symbolic_inner));
(* There might be remaining types for branches that are never reached. *)
try (
exact Empty_set ||
typeclasses eauto
).

Axiom is_discriminant_tuple_eq :
forall
Expand Down
22 changes: 0 additions & 22 deletions CoqOfRust/revm/revm_interpreter/instructions/links/i256.v
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,6 @@ Instance run_MAX_POSITIVE_VALUE :
Proof.
constructor.
run_symbolic.
with_strategy transparent [φ] apply (
run_from_limbs
{| Integer.value := 256 |}
{| Integer.value := 4 |}
{| array.value := [
{| Integer.value := _ |};
{| Integer.value := _ |};
{| Integer.value := _ |};
{| Integer.value := _ |}
] |}
).
Defined.

(* pub const MIN_NEGATIVE_VALUE: U256 *)
Expand All @@ -116,17 +105,6 @@ Instance run_MIN_NEGATIVE_VALUE :
Proof.
constructor.
run_symbolic.
with_strategy transparent [φ] apply (
run_from_limbs
{| Integer.value := 256 |}
{| Integer.value := 4 |}
{| array.value := [
{| Integer.value := _ |};
{| Integer.value := _ |};
{| Integer.value := _ |};
{| Integer.value := _ |}
] |}
).
Defined.

(* const FLIPH_BITMASK_U64: u64 *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Proof.
destruct run_MemoryTrait_for_Memory.
destruct (Impl_Into_for_From_T.run Impl_From_FixedBytes_32_for_U256.run).
destruct (Impl_AsRef_for_Slice.run U8.t).
(* "synthetic" type appearing *)
run_symbolic.
Admitted.

Expand Down
2 changes: 2 additions & 0 deletions CoqOfRust/revm/revm_interpreter/interpreter/links/stack.v
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ Module Impl_Stack.
Proof.
constructor.
run_symbolic.
Unshelve.
all: exact Empty_set || typeclasses eauto.
Defined.

(* pub fn peek(&self, no_from_top: usize) -> Result<U256, InstructionResult> *)
Expand Down
2 changes: 1 addition & 1 deletion CoqOfRust/revm/revm_interpreter/links/instruction_result.v
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ Module Impl_InstructionResult.
Proof.
(* This is the only one that is fast in this file, for some reasons! *)
constructor.
destruct self; run_symbolic.
run_symbolic.
Defined.

(* pub const fn is_revert(self) -> bool *)
Expand Down
Loading