|
| 1 | +/- |
| 2 | +Copyright (c) 2025 Lean FRO, LLC. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Henrik Böving |
| 5 | +-/ |
| 6 | +prelude |
| 7 | +import Lean.Elab.Tactic.BVDecide.Frontend.Normalize.Basic |
| 8 | +import Lean.Meta.Tactic.Cases |
| 9 | + |
| 10 | +/-! |
| 11 | +This module contains the implementation of the pre processing pass for automatically splitting up |
| 12 | +structures containing information about supported types into individual parts recursively. |
| 13 | +
|
| 14 | +The implementation runs cases recursively on all "interesting" types where a type is interesting if |
| 15 | +it is a non recursive structure and at least one of the following conditions hold: |
| 16 | +- it contains something of type `BitVec`/`UIntX`/`Bool` |
| 17 | +- it is parametrized by an interesting type |
| 18 | +- it contains another interesting type |
| 19 | +-/ |
| 20 | + |
| 21 | +namespace Lean.Elab.Tactic.BVDecide |
| 22 | +namespace Frontend.Normalize |
| 23 | + |
| 24 | +open Lean.Meta |
| 25 | + |
| 26 | +/-- |
| 27 | +Contains a cache for interesting and uninteresting types such that we don't duplicate work in the |
| 28 | +structures pass. |
| 29 | +-/ |
| 30 | +structure InterestingStructures where |
| 31 | + interesting : Std.HashMap Name Bool := {} |
| 32 | + |
| 33 | +private abbrev M := StateRefT InterestingStructures MetaM |
| 34 | + |
| 35 | +namespace M |
| 36 | + |
| 37 | +@[inline] |
| 38 | +def lookup (n : Name) : M (Option Bool) := do |
| 39 | + let s ← get |
| 40 | + return s.interesting.get? n |
| 41 | + |
| 42 | +@[inline] |
| 43 | +def markInteresting (n : Name) : M Unit := do |
| 44 | + modify (fun s => {s with interesting := s.interesting.insert n true}) |
| 45 | + |
| 46 | +@[inline] |
| 47 | +def markUninteresting (n : Name) : M Unit := do |
| 48 | + modify (fun s => {s with interesting := s.interesting.insert n false}) |
| 49 | + |
| 50 | +end M |
| 51 | + |
| 52 | +partial def structuresPass : Pass where |
| 53 | + name := `structures |
| 54 | + run' goal := do |
| 55 | + let (_, { interesting, .. }) ← checkContext goal |>.run {} |
| 56 | + |
| 57 | + let goals ← goal.casesRec fun decl => do |
| 58 | + if decl.isLet || decl.isImplementationDetail then |
| 59 | + return false |
| 60 | + else |
| 61 | + let some const := decl.type.getAppFn.constName? | return false |
| 62 | + return interesting.getD const false |
| 63 | + match goals with |
| 64 | + | [goal] => return goal |
| 65 | + | _ => throwError "structures preprocessor generated more than 1 goal" |
| 66 | +where |
| 67 | + checkContext (goal : MVarId) : M Unit := do |
| 68 | + goal.withContext do |
| 69 | + for decl in ← getLCtx do |
| 70 | + if !decl.isLet && !decl.isImplementationDetail then |
| 71 | + discard <| typeInteresting decl.type |
| 72 | + |
| 73 | + constInterestingCached (n : Name) : M Bool := do |
| 74 | + if let some cached ← M.lookup n then |
| 75 | + return cached |
| 76 | + |
| 77 | + let interesting ← constInteresting n |
| 78 | + if interesting then |
| 79 | + M.markInteresting n |
| 80 | + return true |
| 81 | + else |
| 82 | + M.markUninteresting n |
| 83 | + return false |
| 84 | + |
| 85 | + constInteresting (n : Name) : M Bool := do |
| 86 | + let env ← getEnv |
| 87 | + if !isStructure env n then |
| 88 | + return false |
| 89 | + let constInfo := (← getConstInfoInduct n) |
| 90 | + if constInfo.isRec then |
| 91 | + return false |
| 92 | + |
| 93 | + let ctorTyp := (← getConstInfoCtor constInfo.ctors.head!).type |
| 94 | + let analyzer state arg := do |
| 95 | + return state || (← typeInteresting (← arg.fvarId!.getType)) |
| 96 | + forallTelescope ctorTyp fun args _ => args.foldlM (init := false) analyzer |
| 97 | + |
| 98 | + typeInteresting (expr : Expr) : M Bool := do |
| 99 | + match_expr expr with |
| 100 | + | BitVec n => return (← getNatValue? n).isSome |
| 101 | + | UInt8 => return true |
| 102 | + | UInt16 => return true |
| 103 | + | UInt32 => return true |
| 104 | + | UInt64 => return true |
| 105 | + | USize => return true |
| 106 | + | Bool => return true |
| 107 | + | _ => |
| 108 | + let some const := expr.getAppFn.constName? | return false |
| 109 | + constInterestingCached const |
| 110 | + |
| 111 | + |
| 112 | +end Frontend.Normalize |
| 113 | +end Lean.Elab.Tactic.BVDecide |
0 commit comments