Skip to content

Commit 4a17cf1

Browse files
committed
fix: don't pollute other namespaces to avoid conflicts
1 parent ddfd395 commit 4a17cf1

8 files changed

Lines changed: 47 additions & 45 deletions

File tree

Veil/DSL/Action/Lang.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ partial def expandDoElemVeil (stx : doSeqItem) : VeilM (Array doSeqItem) := do
204204
modify (·.push ⟨fr, typeStx.getD (<- `(_))⟩)
205205
expandDoElemVeil $ <- `(Term.doSeqItem|$id:ident := $fr)
206206
| `(Term.doSeqItem| $idts:term := *) =>
207-
let some (id, ts) := idts.isApp? | throwErrorAt stx "wrong syntax for non-deterministic assignment {stx}"
207+
let some (id, ts) := TSyntax.isApp? idts | throwErrorAt stx "wrong syntax for non-deterministic assignment {stx}"
208208
let typeStx ← (<- localSpecCtx.get) |>.spec.getStateComponentTypeStx (id.getId)
209209
let fr := mkIdent <| <- mkFreshUserName `fresh
210210
modify (·.push ⟨fr, typeStx.getD (<- `(_))⟩)
@@ -232,7 +232,7 @@ partial def expandDoElemVeil (stx : doSeqItem) : VeilM (Array doSeqItem) := do
232232
expandDoElemVeil $ <- withRef stx `(Term.doSeqItem| $base:ident := { $base with $suff:ident := $t })
233233
| `(Term.doSeqItem| $idts:term := $t:term) =>
234234
trace[veil.debug] "[expand assignment with args] {stx}"
235-
let some (id, ts) := idts.isApp? | return #[stx]
235+
let some (id, ts) := TSyntax.isApp? idts | return #[stx]
236236
let stx' <- withRef t `(term| $id[ $[$ts],* ↦ $t:term ])
237237
let stx <- withRef stx `(Term.doSeqItem| $id:ident := $stx')
238238
expandDoElemVeil stx

Veil/DSL/Check/InvariantManipulation.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def theoremSuggestionsForIndicators' (actIndicators invIndicators : List (Name
6464
-- FIXME: enhance this function, and all related functions, by handling error better, avoiding repeating code, etc.
6565
def recoverInvariantsInTrStyle : CommandElabM Unit := do
6666
let (_, invChecks) ← getAllChecks
67-
let actIndicators := (invChecks.map (fun (_, (act_name, ind_name)) => (act_name, ind_name))).toList.removeDuplicates
68-
let invIndicators := (invChecks.map (fun ((inv_name, ind_name), _) => (inv_name, ind_name))).toList.removeDuplicates
67+
let actIndicators := (invChecks.map (fun (_, (act_name, ind_name)) => (act_name, ind_name))).toList |> List.removeDuplicates
68+
let invIndicators := (invChecks.map (fun ((inv_name, ind_name), _) => (inv_name, ind_name))).toList |> List.removeDuplicates
6969
let allTheorems ← theoremSuggestionsForIndicators' actIndicators invIndicators
7070
for (thmId, cmd) in allTheorems do
7171
-- if the `tr` theorem hasn't been proven yet, we can recover it from `wp`

Veil/DSL/Check/Main.lean

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import Veil.DSL.Specification.SpecDef
1010

1111
open Lean Elab Command Term Meta Lean.Parser Tactic.TryThis Lean.Core
1212

13-
def Lean.MessageLog.containsSubStr (msgs : MessageLog) (substr : String) : CommandElabM Bool := do
14-
msgs.toList.anyM (fun msg => return String.isSubStrOf substr (← msg.data.toString))
15-
1613
namespace Veil
1714

1815
inductive CheckStyle
@@ -228,10 +225,17 @@ def parseStrAsJson [Monad m] [MonadError m] (str : String) : m Json := do
228225
| .ok json => return json
229226
| .error err => throwError s!"could not parse {str} as Json: {err}"
230227

228+
def MessageLog.containsSubStr (msgs : MessageLog) (substr : String) : CommandElabM Bool := do
229+
msgs.toList.anyM (fun msg => return String.isSubStrOf substr (← msg.data.toString))
230+
231+
def List.removeDuplicates [BEq α] (xs : List α) : List α :=
232+
xs.foldl (init := []) fun acc x =>
233+
if acc.contains x then acc else x :: acc
234+
231235
def checkTheorems (stx : Syntax) (initChecks: Array (Name × Expr)) (invChecks: Array ((Name × Expr) × (Name × Expr))) (behaviour : CheckInvariantsBehaviour) :
232236
CommandElabM Unit := do
233-
let actIndicators := (invChecks.map (fun (_, (act_name, ind_name)) => (act_name, ind_name))).toList.removeDuplicates
234-
let invIndicators := (invChecks.map (fun ((inv_name, ind_name), _) => (inv_name, ind_name))).toList.removeDuplicates
237+
let actIndicators := (invChecks.map (fun (_, (act_name, ind_name)) => (act_name, ind_name))).toList |> List.removeDuplicates
238+
let invIndicators := (invChecks.map (fun ((inv_name, ind_name), _) => (inv_name, ind_name))).toList |> List.removeDuplicates
235239
let (vcStyle, _checkStyle, suggestionStyle) := behaviour
236240
let allTheorems ← theoremSuggestionsForIndicators (!initChecks.isEmpty) actIndicators invIndicators vcStyle
237241
match behaviour with
@@ -252,7 +256,7 @@ def checkTheorems (stx : Syntax) (initChecks: Array (Name × Expr)) (invChecks:
252256
-- The theorem is not proven; we need to figure out why:
253257
-- either solver returned `sat`, `unknown`, or there was an error
254258
let msgsTxt := String.intercalate "\n" (← msgs.toList.filterMapM (fun msg => if msg.severity == .error then msg.toString else pure none))
255-
let (hasSat, hasUnknown, hasFailure) := (← msgs.containsSubStr Veil.SMT.satGoalStr, ← msgs.containsSubStr Veil.SMT.unknownGoalStr, ← msgs.containsSubStr Veil.SMT.failureGoalStr)
259+
let (hasSat, hasUnknown, hasFailure) := (← MessageLog.containsSubStr msgs Veil.SMT.satGoalStr, ← MessageLog.containsSubStr msgs Veil.SMT.unknownGoalStr, ← MessageLog.containsSubStr msgs Veil.SMT.failureGoalStr)
256260
modelStr := if hasSat then .some $ (s!"{theoremId.theoremName}" ++ (getModelStr msgsTxt) ++ "\n") else .none
257261
pure $ match hasSat, hasUnknown, hasFailure with
258262
| true, false, false => SmtResult.Sat .none

Veil/DSL/Specification/ActionDef.lean

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ where
123123
| Mode.internal => do `(fun $funBinders* => do' .internal in $l)
124124
| Mode.external => do `(fun $funBinders* => do' .external in $l)
125125
let genExp <- withDeclName genName do elabTermAndSynthesize genl none
126-
let ⟨genExp, _, _⟩ <- genExp.simpWp
126+
let ⟨genExp, _, _⟩ <- simpWp genExp
127127
let genExp <- instantiateMVars <| <- mkLambdaFVarsImplicit vs genExp
128128
simpleAddDefn genName genExp (attr := #[{name := `generatorSimp}, {name := `actSimp}, {name := `reducible}])
129129
return (genName, genExp)
@@ -143,8 +143,8 @@ where
143143
| Mode.internal => baseName
144144
| Mode.external => toExtName baseName
145145
let genExp := Lean.mkConst genName
146-
let act ← genExp |>.runUnfold (genName :: wpUnfold)
147-
let ⟨act, _actPf, _⟩ <- act.simpAction
146+
let act ← runUnfold genExp (genName :: wpUnfold)
147+
let ⟨act, _actPf, _⟩ <- simpAction act
148148
let mut attr : Array Attribute := #[{name := `initSimp}, {name := `actSimp}]
149149
simpleAddDefn actName act (attr := attr) («type» := ← inferType genExp)
150150
return actName
@@ -183,9 +183,9 @@ where
183183
-- Here, to account for the case when the action is generated from a transition,
184184
-- we also unfold `Function.toWp` and the original definition of the transition.
185185
let origName := toOriginalName baseName
186-
let act ← genExp |>.runUnfold (genName :: wpUnfold)
187-
let act ← act |>.runUnfold [``Function.toWp, origName]
188-
let ⟨act, actPf, _⟩ <- act.simpAction
186+
let act ← runUnfold genExp (genName :: wpUnfold)
187+
let act ← runUnfold act [``Function.toWp, origName]
188+
let ⟨act, actPf, _⟩ <- simpAction act
189189
let mut attr : Array Attribute := #[{name := `actSimp}]
190190
simpleAddDefn actName act (attr := attr) («type» := ← inferType genExp)
191191
return (actName, actPf)
@@ -204,9 +204,9 @@ where
204204
-- transition that existentially quantifies over the state of the
205205
-- dependency, which is bad. Instead, we apply the `lift_transition`
206206
-- theorem, giving us a nicer lifted transition.
207-
let ⟨actTr, _, _⟩ <- actTr.runSimp `(tactic| simp only [$(mkIdent `generatorSimp):ident, setIn, getFrom, lift_transition])
207+
let ⟨actTr, _, _⟩ <- runSimp actTr `(tactic| simp only [$(mkIdent `generatorSimp):ident, setIn, getFrom, lift_transition])
208208
-- After (potentially) `lift_transition` theorem, we simplify as usual
209-
let ⟨actTr, _, _⟩ <- actTr.simpAction
209+
let ⟨actTr, _, _⟩ <- simpAction actTr
210210
let actTr <- mkLambdaFVarsImplicit vs actTr
211211
let actTr <- instantiateMVars actTr
212212
simpleAddDefn actTrName actTr (attr := #[{name := `actSimp}])
@@ -224,7 +224,7 @@ where
224224
let actTrStx <- `(fun st st' => exists? $br ?, (@$(mkIdent actEName) $sectionArgs* $args*).toTwoState st st')
225225
let trActThmStatement ← `(forall? $[$vd]* , ($actTrStx) = (@$(mkIdent actTrName) $sectionArgs*))
226226
let trActThm ← elabTermAndSynthesize trActThmStatement mkProp
227-
let ⟨afterSimp, thmPf, _⟩ <- trActThm.simpAction
227+
let ⟨afterSimp, thmPf, _⟩ <- simpAction trActThm
228228
if !afterSimp.isTrue then
229229
throwError "[genSoundness] {trActThmStatement} could not be proven by `simp`"
230230
let proof ← match thmPf with
@@ -268,9 +268,9 @@ where
268268
genProcedure (baseName : Name) (genIName : Name) := do
269269
let procName := baseName
270270
let genExp := Lean.mkConst genIName
271-
let act ← genExp |>.runUnfold (genIName :: wpUnfold)
272-
let act ← act |>.runUnfold [``Function.toWp]
273-
let ⟨act, actPf, _⟩ <- act.simpAction
271+
let act ← runUnfold genExp (genIName :: wpUnfold)
272+
let act ← runUnfold act [``Function.toWp]
273+
let ⟨act, actPf, _⟩ <- simpAction act
274274
let mut attr : Array Attribute := #[{name := `actSimp}]
275275
simpleAddDefn procName act (attr := attr) («type» := ← inferType genExp)
276276
return (procName, actPf)
@@ -345,7 +345,7 @@ where
345345
let liftedGenName := toGenName liftedActName mode
346346
trace[veil.info] "{liftedGenName} := {liftedGenStx}"
347347
let genExp <- withDeclName liftedGenName do elabTermAndSynthesize liftedGenStx .none
348-
let ⟨genExp, _, _⟩ <- genExp.simpWp
348+
let ⟨genExp, _, _⟩ <- simpWp genExp
349349
let genExp <- instantiateMVars <| <- mkLambdaFVarsImplicit actVs genExp
350350
let initAttr := if isInitialAction then #[{name := `initSimp}] else #[]
351351
simpleAddDefn liftedGenName genExp (attr := #[{name := `generatorSimp}, {name := `actSimp}, {name := `reducible}] ++ initAttr)

Veil/Util/DSL.lean

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ import Veil.Model.State
33
import Veil.DSL.Internals.StateExtensions
44
import Veil.DSL.Action.Theory
55

6-
def Lean.TSyntax.isApp? (stx : Term) : Option (Ident × Array Term) := do
6+
namespace Veil
7+
8+
open Lean Elab Command Term Meta Lean.Parser
9+
10+
def TSyntax.isApp? (stx : Term) : Option (Ident × Array Term) := do
711
let #[f, args] := stx.raw.getArgs | failure
812
let `(term| $f:ident) := f | failure
913
return (⟨f⟩, args.getArgs.map (⟨·⟩))
1014

11-
def List.removeDuplicates [BEq α] (xs : List α) : List α :=
12-
xs.foldl (init := []) fun acc x =>
13-
if acc.contains x then acc else x :: acc
14-
15-
namespace Veil
16-
17-
open Lean Elab Command Term Meta Lean.Parser
1815

1916
def isTypeClassBinder : TSyntax `Lean.Parser.Term.bracketedBinder → Bool
2017
| `(bracketedBinder| $_:instBinder) => true

Veil/Util/Display.lean

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,4 @@ def getModelStr (msg : String) : String :=
8787
| [model, _] => model
8888
| _ => resWithErr
8989

90-
91-
def Lean.MessageLog.getErrorMessages (log : MessageLog) : MessageLog :=
92-
{ unreported := log.unreported.filter fun m => match m.severity with | MessageSeverity.error => true | _ => false }
93-
9490
end Veil

Veil/Util/Quantifiers.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private partial def isClassQuick? : Expr → MetaM (LOption Name)
7070
| _ => return .undef
7171
| _ => return .none
7272

73-
def _root_.Lean.Expr.isExists (e : Expr) : Bool :=
73+
def isExists (e : Expr) : Bool :=
7474
match_expr e with
7575
| Exists _ _ => true
7676
| _ => false
@@ -146,7 +146,7 @@ mutual
146146
withNewLocalInstancesImp fvars j do
147147
if reducing then
148148
let newType ← whnf type
149-
if newType.isExists then
149+
if isExists newType then
150150
process lctx fvars fvars.size newType
151151
else
152152
k fvars type
@@ -156,7 +156,7 @@ mutual
156156

157157
private partial def existsTelescopeReducingAux (type : Expr) (k : Array Expr → Expr → MetaM α) (cleanupAnnotations : Bool) : MetaM α := do
158158
let newType ← whnf type
159-
if newType.isExists then
159+
if isExists newType then
160160
existsTelescopeReducingAuxAux true newType k cleanupAnnotations
161161
else
162162
k #[] type

Veil/Util/TermSimp.lean

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import Lean
22

3+
4+
namespace Veil
5+
36
/-
47
This file provides facilities for running tactics on terms. In
58
particular, it provides a version of `Tactic.run` that returns a
@@ -39,8 +42,8 @@ end Tactic
3942

4043
def veilSimpMaxSteps := 10 * Lean.Meta.Simp.defaultMaxSteps
4144

42-
open Lean.Elab Tactic Lean.Meta
43-
def Lean.Expr.runSimp (e : Expr) (stx : TermElabM (TSyntax `tactic)) : TermElabM Simp.Result := do
45+
open Lean Lean.Elab Tactic Lean.Meta
46+
def runSimp (e : Expr) (stx : TermElabM (TSyntax `tactic)) : TermElabM Simp.Result := do
4447
let stx ← stx
4548
let mv := (← mkFreshExprMVar e)
4649
let (res, [_l]) ← Tactic.run mv.mvarId! (do
@@ -52,15 +55,17 @@ def Lean.Expr.runSimp (e : Expr) (stx : TermElabM (TSyntax `tactic)) : TermElabM
5255
) | throwError "[runSimp] expected exactly one goal after simplification"
5356
return res.1
5457

55-
def Lean.Expr.simpWp (e : Expr) : TermElabM Simp.Result := do
58+
def simpWp (e : Expr) : TermElabM Simp.Result := do
5659
let stx := `(tactic| simp only [wpSimp])
57-
e.runSimp stx
60+
runSimp e stx
5861

59-
def Lean.Expr.simpAction (e : Expr) : TermElabM Simp.Result := do
62+
def simpAction (e : Expr) : TermElabM Simp.Result := do
6063
let stx := `(tactic| simp only [actSimp, smtSimp, quantifierSimp])
61-
e.runSimp stx
64+
runSimp e stx
6265

63-
def Lean.Expr.runUnfold (e : Expr) (defs : List Name) : TermElabM Expr := do
66+
def runUnfold (e : Expr) (defs : List Name) : TermElabM Expr := do
6467
let mut eu := e
6568
for name in defs do eu := (<- unfold eu name).expr
6669
return eu
70+
71+
end Veil

0 commit comments

Comments
 (0)