-
Notifications
You must be signed in to change notification settings - Fork 21
chore: Split out the finishing tactic aspect of simp_mem into mem_omega [2/?] #231
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/- | ||
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Author(s): Siddharth Bhat | ||
|
||
In this file, we define proof automation for separation conditions of memory. | ||
|
||
References: | ||
- https://github.com/leanprover/lean4/blob/240ebff549a2cf557f9abe9568f5de885f13e50d/src/Lean/Elab/Tactic/Omega/OmegaM.lean | ||
- https://github.com/leanprover/lean4/blob/240ebff549a2cf557f9abe9568f5de885f13e50d/src/Lean/Elab/Tactic/Omega/Frontend.lean | ||
-/ | ||
import Arm | ||
import Arm.Memory.MemoryProofs | ||
import Arm.BitVec | ||
import Arm.Memory.Attr | ||
import Arm.Memory.AddressNormalization | ||
import Lean | ||
import Lean.Meta.Tactic.Rewrite | ||
import Lean.Meta.Tactic.Rewrites | ||
import Lean.Elab.Tactic.Conv | ||
import Lean.Elab.Tactic.Conv.Basic | ||
import Tactics.Simp | ||
import Tactics.BvOmegaBench | ||
import Arm.Memory.Common | ||
|
||
open Lean Meta Elab Tactic Memory | ||
|
||
namespace MemOmega | ||
|
||
structure Config where | ||
/-- | ||
If true, then MemOmega will explode uses of pairwiseSeparate [mem₁, ... memₙ] | ||
into O(n^2) separation conditions. | ||
-/ | ||
explodePairwiseSeparate : Bool := false | ||
|
||
/-- Edit the config for mem_omega! -/ | ||
def Config.mkBang (c : Config) : Config := | ||
{ c with explodePairwiseSeparate := true } | ||
|
||
/-- Context for the `SimpMemM` monad, containing the user configurable options. -/ | ||
structure Context where | ||
/-- User configurable options for `simp_mem`. -/ | ||
cfg : Config | ||
/-- Cache of `bv_toNat` simp context. -/ | ||
bvToNatSimpCtx : Simp.Context | ||
/-- Cache of `bv_toNat` simprocs. -/ | ||
bvToNatSimprocs : Array Simp.Simprocs | ||
|
||
|
||
namespace Context | ||
|
||
def init (cfg : Config) : MetaM Context := do | ||
let (bvToNatSimpCtx, bvToNatSimprocs) ← | ||
LNSymSimpContext | ||
(config := {failIfUnchanged := false}) | ||
-- Also use `mem_{legal', subset', separate'}.iff_omega to unfold definitions that | ||
-- occur inside compound expressions, such as (mem_subset' .. ∨ mem_subset' ..) | ||
-- (thms := #[``mem_legal'.iff_omega, ``mem_subset'.iff_omega, ``mem_separate'.iff_omega]) | ||
(simp_attrs := #[`bv_toNat]) | ||
(useDefaultSimprocs := false) | ||
return {cfg, bvToNatSimpCtx, bvToNatSimprocs} | ||
end Context | ||
|
||
abbrev MemOmegaM := (ReaderT Context TacticM) | ||
|
||
namespace MemOmegaM | ||
|
||
def run (ctx : Context) (x : MemOmegaM α) : TacticM α := ReaderT.run x ctx | ||
|
||
end MemOmegaM | ||
|
||
def memOmegaTac : MemOmegaM Unit := do | ||
let g ← getMainGoal | ||
g.withContext do | ||
/- We need to explode all pairwise separate hyps -/ | ||
let rawHyps ← getLocalHyps | ||
let mut hyps := #[] | ||
-- extract out structed values for all hyps. | ||
for h in rawHyps do | ||
hyps ← hypothesisOfExpr h hyps | ||
|
||
-- only enable pairwise constraints if it is enabled. | ||
let isPairwiseEnabled := (← readThe Context).cfg.explodePairwiseSeparate | ||
hyps := hyps.filter (!·.isPairwiseSeparate || isPairwiseEnabled) | ||
|
||
-- used specialized procedure that doesn't unfold everything for the easy case. | ||
if ← closeMemSideCondition (← getMainGoal) (← readThe Context).bvToNatSimpCtx (← readThe Context).bvToNatSimprocs hyps then | ||
return () | ||
else | ||
-- in the bad case, just rip through everything. | ||
-- let _ ← Hypothesis.addOmegaFactsOfHyps (hyps.toList.filter (fun h => h.isPairwiseSeparate)) #[] | ||
let _ ← Hypothesis.addOmegaFactsOfHyps hyps.toList #[] | ||
|
||
TacticM.withTraceNode' m!"Reducion to omega" do | ||
try | ||
TacticM.traceLargeMsg m!"goal (Note: can be large)" m!"{← getMainGoal}" | ||
omega (← readThe Context).bvToNatSimpCtx (← readThe Context).bvToNatSimprocs | ||
trace[simp_mem.info] "{checkEmoji} `omega` succeeded." | ||
catch e => | ||
trace[simp_mem.info] "{crossEmoji} `omega` failed with error:\n{e.toMessageData}" | ||
|
||
|
||
/-- | ||
Allow elaboration of `MemOmegaConfig` arguments to tactics. | ||
-/ | ||
declare_config_elab elabMemOmegaConfig MemOmega.Config | ||
|
||
/-- | ||
The `mem_omega` tactic is a finishing tactic which is used to dispatch memory side conditions. | ||
Broadly, the algorithm works as follows: | ||
- It scans the set of hypotheses for `mem_separate`, `mem_subset`, and `mem_legal` hypotheses, and turns them into `omega` based information. | ||
- It calls `omega` as a finishing tactic to close the current goal state. | ||
- Cruicially, it **does not unfold** `pairwiseSeparate` constraints. We expect the user to do so. If they want `pairwiseSeparate` unfolded, then please use `mem_omega!`. | ||
-/ | ||
syntax (name := mem_omega) "mem_omega" (Lean.Parser.Tactic.config)? : tactic | ||
|
||
/-- | ||
The `mem_omega!` tactic is a finishing tactic, that is a more aggressive variant of `mem_omega`. | ||
-/ | ||
syntax (name := mem_omega_bang) "mem_omega!" (Lean.Parser.Tactic.config)? : tactic | ||
|
||
@[tactic mem_omega] | ||
def evalMemOmega : Tactic := fun | ||
| `(tactic| mem_omega $[$cfg]?) => do | ||
let cfg ← elabMemOmegaConfig (mkOptionalNode cfg) | ||
memOmegaTac.run (← Context.init cfg) | ||
| _ => throwUnsupportedSyntax | ||
|
||
@[tactic mem_omega_bang] | ||
def evalMemOmegaBang : Tactic := fun | ||
| `(tactic| mem_omega! $[$cfg]?) => do | ||
let cfg ← elabMemOmegaConfig (mkOptionalNode cfg) | ||
memOmegaTac.run (← Context.init cfg.mkBang) | ||
| _ => throwUnsupportedSyntax | ||
|
||
end MemOmega |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.