Skip to content
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

Ltac2 local2ptree #712

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ COQLIB=$(shell $(COQC) -where | tr -d '\r' | tr '\\' '/')

# Check Coq version

COQVERSION= 8.16.0 or-else 8.16.1 or-else 8.17+rc1 or-else 8.17 or-else 8.17.0
COQVERSION= 8.16.0 or-else 8.16.1 or-else 8.17+rc1 or-else 8.17 or-else 8.17.0 or-else 8.17.1

COQV=$(shell $(COQC) -v)
ifneq ($(IGNORECOQVERSION),true)
Expand Down
99 changes: 98 additions & 1 deletion floyd/local2ptree_denote.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ Require Import VST.floyd.base2.
Require Import VST.floyd.client_lemmas.
Import compcert.lib.Maps.

Require Import Ltac2.Ltac2.
Require Import Ltac2.Printf.
(* We want to use Ltac1 in proofs *)
Set Default Proof Mode "Classic".

Import LiftNotation.
Local Open Scope logic.

Expand Down Expand Up @@ -62,7 +67,10 @@ Ltac grab_gvars L :=
| nil => let x := constr:(@nil globals) in x
end.

Ltac prove_local2ptree :=
(* The Ltac1 version of prove_local2ptree works by expanding each local symbol individually, doing a cbv delta [<ptree_functions>] and undoing the local symbol expansions *)
(* Below is a Ltac2 version, which achieves the same much faster by using a computed delta list including the local symbols and the ptree symbols *)

Ltac prove_local2ptree_old :=
clear;
match goal with |- local2ptree ?A = _ => hnf_localdef_list A end;
etransitivity;
Expand All @@ -76,6 +84,95 @@ Ltac prove_local2ptree :=
match goal with |- ?L = _ => let x := grab_gvars L in instantiate(1:=x); reflexivity end
].

(** Ltac2 allows to work with computed delta symbol lists, so instead of individually expanding all variables IDs to numbers, we can compile a list of local symbol IDs *)

Ltac2 throw_invalid_argument fmt :=
Message.Format.kfprintf (fun x => Control.throw (Invalid_argument (Some x))) fmt.
Ltac2 Notation "throw_invalid_argument" fmt(format) := throw_invalid_argument fmt.

(* If one works with computed symbol lists, one can't use the tactic notation for evaluation functions (which construct such records in a more concise way) *)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you do something like Ltac2 adjust_redflags_delta (syms : Std.reference list) (redflags : Std.red_flags) : Std.red_flags := { redflags with Std.rDelta := false; Std.rConst := syms } and then make a notation so you can write adjust_redflags_delta [id] syms or adjust_redflags_delta (beta iota) syms?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or { redflags with Std.rConst := List.app redflags.rConst syms } and then you could do reduction_strategy:([<list of things>]) below and pass that around instead of starting with a list of references. Might be slightly more concise

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to think about it. I tried a few things with notations, but none of this did work well in case one has mixed references, say local and global, manually specified and computed lists. In the end it was more effort and harder to understand than doing it low level. But meanwhile I am a bit smarter and can reconsider it.


Ltac2 redflags_all_delta_only (syms : Std.reference list) : Std.red_flags :=
{
(* beta: expand the application of an unfolded functions by substitution *)
Std.rBeta := true;
(* delta: true = expand all but rConst; false = expand only rConst *)
Std.rDelta := false;
(* Note: iota in tactics like cbv is a shorthand for match, fix and cofix *)
(* iota-match: simplify matches by choosing a pattern *)
Std.rMatch := true;
(* iota-fix: simplify fixpoint expressions by expanding one level *)
Std.rFix := true;
(* iota-cofix: simplify cofixpoint expressions by expanding one level *)
Std.rCofix := true;
(* zeta: expand let expressions by substitution *)
Std.rZeta := true;
(* Symbols to expand *)
Std.rConst := syms
}.

Ltac2 reference_of_constr(ref : constr) : Std.reference :=
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems worth upstreaming into the Coq standard library, I've needed something like this a couple of times too

match Constr.Unsafe.kind ref with
| Constr.Unsafe.Var ident
=> Std.VarRef ident
| Constr.Unsafe.Constant constant instance
=> Std.ConstRef constant
| Constr.Unsafe.Ind inductive instance
=> Std.IndRef inductive
| Constr.Unsafe.Constructor constructor instance
=> Std.ConstructRef constructor
| _ => throw_invalid_argument "reference_of_constr: not a reference: %t" ref
end.

Ltac2 add_local_symbol (term : constr) (symbols : Std.reference list) : Std.reference list :=
match Constr.Unsafe.kind term with
| Constr.Unsafe.Var ident => Std.VarRef ident :: symbols
| Constr.Unsafe.Constant constant instance => Std.ConstRef constant :: symbols
| _ => symbols
end.

(* Add the symbols in "locals" to "symbols" *)

Ltac2 rec get_local_symbols (locals : constr) (symbols : Std.reference list) : Std.reference list :=
lazy_match! locals with
| ?head :: ?tail =>
lazy_match! head with
| temp ?i ?v => get_local_symbols tail (add_local_symbol i symbols)
| lvar ?i ?t ?v => get_local_symbols tail (add_local_symbol i symbols)
| gvars ?gv => get_local_symbols tail (add_local_symbol gv symbols)
| ?sym =>
(* This handles "T" in the test below *)
let symref := reference_of_constr sym in
let locals_ex := Std.eval_lazy (redflags_all_delta_only [symref]) locals in
get_local_symbols locals_ex (symref :: symbols)
end
| nil => symbols
| ?sym => (add_local_symbol sym symbols)
end.

Ltac2 prove_local2ptree () : unit :=
lazy_match! goal with
| [ |- local2ptree ?locals = ?rhs ] =>
let syms_ptree := [
reference:(local2ptree);
reference:(local2ptree1);
reference:(local2ptree_aux);
reference:(PTree.set);
reference:(PTree.empty);
reference:(PTree.set0);
reference:(PTree.set');
reference:(PTree.get);
reference:(PTree.get')
] in
let syms_all := get_local_symbols locals syms_ptree in
Std.lazy (redflags_all_delta_only syms_all) { Std.on_hyps := None; Std.on_concl := Std.AllOccurrences };
reflexivity
| [ |- ?g ] => throw_invalid_argument "prove_local2ptree: invalid goal %t" g
end.

(* For speed comparison, this can be defined to prove_local2ptree_old *)
Ltac prove_local2ptree := ltac2:(prove_local2ptree ()).

Goal exists x, local2ptree (
temp 1%positive Vundef
:: temp 3%positive (Vint (Int.repr (3+4)))
Expand Down
Loading