-
Notifications
You must be signed in to change notification settings - Fork 93
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
MSoegtropIMC
wants to merge
2
commits into
PrincetonUniversity:master
Choose a base branch
from
MSoegtropIMC:ltac2_local2ptree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ltac2 local2ptree #712
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
|
@@ -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; | ||
|
@@ -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) *) | ||
|
||
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 := | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 writeadjust_redflags_delta [id] syms
oradjust_redflags_delta (beta iota) syms
?There was a problem hiding this comment.
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 doreduction_strategy:([<list of things>])
below and pass that around instead of starting with a list of references. Might be slightly more conciseThere was a problem hiding this comment.
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.