Skip to content
Draft
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
17 changes: 13 additions & 4 deletions Veil/Backend/SMT/Model.lean
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ private def convertEnumToSmt? (enumExpr enumType smtType : Expr)
converted ← mkIte adapt.smtType cond idxExpr converted
return some converted

/-- SMT functions are commonly returned as tables of nested ifs. Check their integer
outputs before converting to Nat, including when a symbolic input prevents reduction. -/
private partial def checkNatOutput (value : Expr) (name : Name) : MetaM Unit := do
let value := value.headBeta
if ← Meta.isDefEq (← Meta.mkAppM ``Int.ofNat #[← Meta.mkAppM ``Int.toNat #[value]]) value then return
let_expr ite _ _ _ yes no := value | throwError "invalid Nat assignment {name}"
checkNatOutput yes name
checkNatOutput no name

/-- Convert an SMT model function to match the expected Lean type.
SMT models may convert types during embedding:
Expand All @@ -126,7 +135,7 @@ private def convertEnumToSmt? (enumExpr enumType smtType : Expr)
TODO: upstream this to `lean-smt` -/
def adaptSmtExprType (smtExpr : Expr) (expectedType : Expr)
(enumAdapts : Array EnumSortAdaptation := #[]) : MetaM Expr := do
(enumAdapts : Array EnumSortAdaptation := #[]) (name : Name := .anonymous) : MetaM Expr := do
-- Reduce types for accurate comparison
let expectedType ← Meta.reduceAll expectedType
let smtType ← Meta.reduceAll (← Meta.inferType smtExpr)
Expand Down Expand Up @@ -164,9 +173,9 @@ def adaptSmtExprType (smtExpr : Expr) (expectedType : Expr)
-- Prop → Bool: use `decide`
if expectedRetTy.isBool && smtRetTy.isProp then
Meta.mkAppOptM ``decide #[some app, none]
-- Int → Nat: use `Int.toNat` (returns 0 for negative values, but SMT model
-- should always return valid non-negative values for Nat-typed results)
else if expectedRetTy.isNat && smtRetTy.isInt then
-- Int → Nat requires a nonnegative result.
else if expectedRetTy.isNat && smtRetTy.isInt then do
checkNatOutput app name
Meta.mkAppM ``Int.toNat #[app]
-- SMT sort (typically `Fin n`) → enum concrete type
else if let some converted ← convertSmtToEnum? app smtRetTy expectedRetTy enumAdapts then
Expand Down
151 changes: 89 additions & 62 deletions Veil/Core/Tools/ModelChecker/Symbolic/TraceLang.lean
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public meta import Veil.Util.Meta
public meta import Veil.Frontend.DSL.Module.Util.Assertions
public meta import Veil.Frontend.DSL.Module.Names
public meta import Veil.Frontend.DSL.Util
public meta import Veil.Core.Tools.ModelChecker.TransitionSystem
public meta import Veil.Core.Tools.ModelChecker.Trace
public meta import Veil.Core.Tools.Verifier.Server
public meta import Veil.Core.Tools.Verifier.Results
public meta import Veil.Frontend.DSL.Module.VCGen
Expand Down Expand Up @@ -102,24 +102,36 @@ private def mkTransitionExpr (rtsExpr theoryId currState nextState : TSyntax `te
let trExpr ← `(term| (($rtsExpr).$(mkIdent `tr) $theoryId $currState $labelExpr $nextState))
`(term| ($tagId = $actionTagMember ∧ $trExpr))

/-- Collect module-level binders (sorts, user parameters, sort assumptions, user-defined typeclasses) -/
def traceModuleParams (mod : Module) : Array Parameter := Id.run do
let typeclassParams := mod.parameters.filter fun p =>
match p.kind with
| .moduleTypeclass .sortAssumption | .moduleTypeclass .userDefined => true
| _ => false
return mod.uninterpretedParameters ++ typeclassParams

/-- Build a disjunction representing any action transition with ActionTag constraint -/
private def mkAnyActionDisjunction (mod : Module) (rtsExpr theoryId currState nextState : TSyntax `term)
(tagId : Ident) (trIdx : Nat) : TermElabM (Term × Array (TSyntax ``Lean.Parser.Term.bracketedBinder)) := do
let moduleBinders ← (traceModuleParams mod).mapM (·.binder)
let sorts ← mod.uninterpretedParamIdents
Term.elabBinders moduleBinders fun _ => do
let results ← mod.actions.mapIdxM fun idx act => do
let (_, specificParams) ← mod.declarationAllParams act.name act.declarationKind
let (argIdents, binders) ← mkTransitionParamBinders trIdx idx specificParams.size
return (← mkTransitionExpr rtsExpr theoryId currState nextState tagId act.name argIdents, binders)
let edge ← mkTransitionExpr rtsExpr theoryId currState nextState tagId act.name argIdents
unless binders.isEmpty do
-- Hoisting across a disjunction needs inhabitants for the unselected branches.
let ctor ← elabTerm (← `(@$(mkIdent (labelTypeName ++ act.name)) $sorts*)) none
let mut ty ← inferType ctor
for _ in argIdents do
let .forallE _ domain body _ ← whnf ty | throwError "missing parameter in {ctor}"
let some inhabited ← synthInstance? (← mkAppM ``Nonempty #[domain])
| return (← `(∃ $[$argIdents:ident]*, $edge), #[])
ty := body.instantiate1 (← mkAppM ``Classical.choice #[inhabited])
return (edge, binders)
return (← repeatedOr (results.map (·.1)), results.flatMap (·.2))

/-- Collect module-level binders (sorts, user parameters, sort assumptions, user-defined typeclasses) -/
private def collectModuleBinders (mod : Module) : TermElabM (Array (TSyntax ``Lean.Parser.Term.bracketedBinder)) := do
let sortBinders ← mod.uninterpretedParamBinders
let typeclassParams := mod.parameters.filter fun p =>
match p.kind with
| .moduleTypeclass .sortAssumption | .moduleTypeclass .userDefined => true
| _ => false
return sortBinders ++ (← typeclassParams.mapM (·.binder))

/-- State threaded through trace spec elaboration -/
structure TraceElabState where
/-- Remaining state identifiers (head is current state) -/
Expand All @@ -128,6 +140,8 @@ structure TraceElabState where
trIdx : Nat := 1
/-- Accumulated assertions -/
assertions : Array (TSyntax `term) := #[]
/-- Query-specific constraints on the decoded states and labels. -/
replayLines : Array Term := #[]
/-- Accumulated binders -/
binders : BinderAccum := {}

Expand Down Expand Up @@ -165,40 +179,46 @@ private def processTransition (ctx : TraceElabCtx) (curr next : Ident) (trIdx :
return (disjunction, { params := paramBinders, tags := #[tagBinder] })
| _ => unreachable!

scoped syntax "veil_trace_assertion% " term:max : term

elab_rules : term <= expectedType
| `(veil_trace_assertion% $assertion) => do
let mod ← getCurrentModule
let firstTree := (← getInfoTrees).size
let value ← withSaveInfoContext <| elabTerm assertion expectedType
mod.warnTermShadows assertion firstTree
pure value

/-- Process a user assertion (no state transition) -/
private def processAssertion (ctx : TraceElabCtx) (currState : Ident) (t : Term) : TermElabM (TSyntax `term) := do
/-- Elaborate a checkpoint in its own module context. Both uses unfold the same predicate. -/
private def processAssertion (ctx : TraceElabCtx) (t : Term) : TermElabM Term := do
let fieldRepInstance ← `(term| $instAbstractFieldRepresentation $(ctx.sorts)*)
let stateSortTerm ← `(term| $fieldAbstractDispatcher $(ctx.sorts)*)
let wrappedTerm ← withTheoryAndStateFn ctx.mod (← `(uqc% (($t:term):Prop))) (some $ ← `(term|Prop)) ctx.theoryT ctx.stateT fieldRepInstance stateSortTerm
let assertion ← `(term|($wrappedTerm $(ctx.theoryId) $currState))
let assertion := Syntax.inheritSourceSpanFrom assertion t
`(term|veil_trace_assertion% $assertion)
let params := traceModuleParams ctx.mod
let binders ← params.mapM fun p => p.binder >>= bracketedBinderToFunBinder
let predicate ← mkFunSyntax binders wrappedTerm
let firstTree := (← getInfoTrees).size
let value ← withSaveInfoContext <| withoutErrToSorry <| elabTermAndSynthesize predicate none
ctx.mod.warnTermShadows t firstTree
let name ← addVeilDefinition (← mkFreshUserName `traceCheckpoint) value (compile := false)
let args ← params.mapM (·.arg)
`(delta% @$(mkIdent name) $args*)

/-- Expand anyNActions into individual anyAction entries -/
private def expandTraceLine : TraceSpecLine → List TraceSpecLine
| .anyNActions k => List.replicate k .anyAction
| other => [other]

open Lean.Parser.Term in
/-- Process a single trace spec line, returning updated state -/
private def processLine (ctx : TraceElabCtx) (s : TraceElabState) : TraceSpecLine → TermElabM TraceElabState
| .assertion t => do
let curr := s.states.head!
let assertion ← processAssertion ctx curr t
return { s with assertions := s.assertions.push assertion }
| line => do -- .action or .anyAction
let predicate ← processAssertion ctx t
let assertion ← `($predicate $(ctx.theoryId) $(s.states.head!))
let line ← `(ModelChecker.Trace.QueryLine.assertion $predicate)
return { s with assertions := s.assertions.push assertion, replayLines := s.replayLines.push line }
| line => do
let (curr, next, s') ← s.advance.getDM (throwError "insufficient states for transition")
let (assertion, newBinders) ← processTransition ctx curr next s.trIdx line
return { s' with assertions := s'.assertions.push assertion, binders := s'.binders ++ newBinders }
let matchesLabel ← match line with
| .action name =>
if ctx.mod.actions.size == 1 then `(fun _ => True)
else `(fun label => (label matches $(mkIdent (labelTypeName ++ name)):ident ..) = true)
| _ => `(fun _ => True)
let line ← `(ModelChecker.Trace.QueryLine.action $matchesLabel)
return { s' with
assertions := s'.assertions.push assertion, binders := s'.binders ++ newBinders
replayLines := s'.replayLines.push line }

/-- Extract structured JSON trace from a discharger result (if SAT). -/
private def extractTraceJson? (result : Option (DischargerResult SmtResult)) : Option Json :=
Expand Down Expand Up @@ -312,6 +332,40 @@ private def logTraceResults (stx : Syntax) (isExpectedSat : Bool) (vcName : Name
if shouldHaveTrace isExpectedSat vcResult.status && traceJson?.isNone then
logViolation "Could not extract trace JSON"; logDischargerErrors vcResult.timing.dischargers

/-- Elaborate the SMT query and the positive predicate replayed on decoded labels and states. -/
def mkTraceQuery (mod : Module) (spec : TSyntax `traceSpec) : TermElabM (Term × TraceReplay) := do
let expandedSpec := (← parseTraceSpec spec).flatMap expandTraceLine
let numTransitions := expandedSpec.filter (!· matches .assertion _) |>.length
let stateIds := (List.range (numTransitions + 1)).map fun i =>
mkIdent (modelBinderName (traceStateName i))

let sorts ← mod.uninterpretedParamIdents
let theoryId := mkIdent (modelBinderName `th)
let theoryT ← mod.theoryStx
let stateT ← `(term| $(mkIdent stateName) ($fieldAbstractDispatcher $sorts*))
let rtsExpr ← `(term| $assembledRTS $sorts*)
let tagType ← Lean.mkIdent <$> mkFreshUserName actionTagTypeName
let ctx : TraceElabCtx := { mod, tagType, rtsExpr, theoryId, theoryT, stateT, sorts }

let initAssertion ← `(term|
($rtsExpr).$(mkIdent `assumptions) $theoryId ∧
(($rtsExpr).$(mkIdent `init) $theoryId $(stateIds.head!)))
let initState : TraceElabState := {
states := stateIds, assertions := #[initAssertion] }
let finalState ← expandedSpec.foldlM (processLine ctx) initState

let conjunction ← repeatedAnd finalState.assertions
let actionTagBinders ← if mod.actions.isEmpty then pure #[] else mkActionTagBinders tagType
let params := traceModuleParams mod
let moduleBinders ← params.mapM (·.binder)
let allBinders := moduleBinders ++ actionTagBinders ++ finalState.binders.all
let stateNames := stateIds.toArray
let assertion ← `(∀ $allBinders* ($theoryId:ident : $theoryT) ($stateNames* : $stateT), ¬ $conjunction)

let query ← `(∀ $moduleBinders* (path : ModelChecker.Trace $theoryT $stateT ($labelType $sorts*)),
ModelChecker.Trace.isValidFor path $rtsExpr [$(finalState.replayLines),*])
return (assertion, { params, statement := query, numTransitions })

/-- Elaborate the trace command at the current ref. Its widget shows the result while the solver
runs, through `withStreamingInfo`, so the elaborator that calls this is `@[incremental]`. -/
def elabTraceSpec (r : TSyntax `expected_smt_result) (name : Option (TSyntax `ident))
Expand All @@ -324,35 +378,8 @@ def elabTraceSpec (r : TSyntax `expected_smt_result) (name : Option (TSyntax `id
-- Determine if this is a sat or unsat trace query
let isExpectedSat := r.raw.isOfKind ``expected_sat

-- Build the trace specification
let (assertion, numTransitions, vcName) ← Command.runTermElabM fun _ => do
let expandedSpec := (← parseTraceSpec spec).flatMap expandTraceLine
let numTransitions := expandedSpec.filter (!· matches .assertion _) |>.length
let stateIds := (List.range (numTransitions + 1)).map fun i =>
mkIdent (modelBinderName (traceStateName i))

let sorts ← mod.uninterpretedParamIdents
let theoryId := mkIdent (modelBinderName `th)
let theoryT ← mod.theoryStx
let stateT ← `(term| $(mkIdent stateName) ($fieldAbstractDispatcher $sorts*))
let rtsExpr ← `(term| @$assembledRTS $sorts*)
let tagType ← Lean.mkIdent <$> mkFreshUserName actionTagTypeName
let ctx : TraceElabCtx := { mod, tagType, rtsExpr, theoryId, theoryT, stateT, sorts }

let initAssertion ← `(term|
($rtsExpr).$(mkIdent `assumptions) $theoryId ∧
(($rtsExpr).$(mkIdent `init) $theoryId $(stateIds.head!)))
let initState : TraceElabState := { states := stateIds, assertions := #[initAssertion] }
let finalState ← expandedSpec.foldlM (processLine ctx) initState

let vcName ← match name with | some n => pure n.getId | none => mkFreshUserName `trace
let conjunction ← repeatedAnd finalState.assertions
let actionTagBinders ← if mod.actions.isEmpty then pure #[] else mkActionTagBinders tagType
let allBinders := (← collectModuleBinders mod) ++ actionTagBinders ++ finalState.binders.all
let stateNames := stateIds.toArray
let assertion ← `(∀ $allBinders* ($theoryId:ident : $theoryT) ($stateNames* : $stateT), ¬ $conjunction)

return (assertion, numTransitions, vcName)
let vcName ← match name with | some n => pure n.getId | none => liftCoreM (mkFreshUserName `trace)
let (assertion, replay) ← Command.runTermElabM fun _ => mkTraceQuery mod spec

match pf with
| some proofTerm =>
Expand All @@ -362,7 +389,7 @@ def elabTraceSpec (r : TSyntax `expected_smt_result) (name : Option (TSyntax `id
| none =>
-- Use VCManager with automatic discharger
let vcStatement ← mkTraceVCStatement mod vcName assertion
let metadata := mkTraceVCMetadata isExpectedSat numTransitions (some vcName) (some assertion)
let metadata := mkTraceVCMetadata isExpectedSat replay.numTransitions (some vcName) (some assertion)
-- Filter for matching trace VCs
let vcFilter := (· == metadata)
-- Check if a VC with this name already exists (avoid duplicate work)
Expand All @@ -372,7 +399,7 @@ def elabTraceSpec (r : TSyntax `expected_smt_result) (name : Option (TSyntax `id
return existingId
-- Add VC and discharger atomically
let (mgr', vcId) := mgr.addVC ⟨vcStatement, metadata⟩ {} #[]
let mgr'' ← mgr'.mkAddDischarger vcId (TraceDischarger.fromAssertion numTransitions isExpectedSat)
let mgr'' ← mgr'.mkAddDischarger vcId (TraceDischarger.fromAssertion replay isExpectedSat)
ref.set mgr''
return vcId

Expand Down
27 changes: 26 additions & 1 deletion Veil/Core/Tools/ModelChecker/Trace.lean
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ deriving Repr, Inhabited
abbrev Steps (σ : Type) (l : Type) := Array (Step σ l)
abbrev StepList (σ : Type) (l : Type) := List (Step σ l)

@[grind =]
@[simp, grind =]
def StepList.validFrom (sys : RelationalTransitionSystem ρ σ l) (th : ρ) (st : σ) (steps : StepList σ l) : Prop :=
match steps with
| [] => True
Expand Down Expand Up @@ -147,6 +147,31 @@ structure Trace.isValid (trace : Trace ρ σ l) (sys : RelationalTransitionSyste
initialStateSatisfiesInit : sys.init (trace.theory) (trace.initialState)
stepsValid : Steps.validFrom trace.steps sys trace.theory trace.initialState

@[simp] theorem Trace.isValid_iff (trace : Trace ρ σ l) (sys : RelationalTransitionSystem ρ σ l) :
trace.isValid sys ↔ sys.assumptions trace.theory ∧ sys.init trace.theory trace.initialState ∧
StepList.validFrom sys trace.theory trace.initialState trace.steps.toList :=
⟨fun h => ⟨h.theorySatisfiesAssumptions, h.initialStateSatisfiesInit, h.stepsValid⟩,
fun ⟨a, i, s⟩ => ⟨a, i, s⟩⟩

/-- A trace query either tests the current state or consumes a step with a matching label. -/
inductive Trace.QueryLine (ρ σ l : Type) where
| assertion (holds : ρ → σ → Prop)
| action (matchesLabel : l → Prop)

@[simp] def Trace.QueryLine.matchesFrom (th : ρ) :
List (QueryLine ρ σ l) → σ → List (Step σ l) → Prop
| [], _, steps => steps = []
| .assertion holds :: rest, st, steps => holds th st ∧ matchesFrom th rest st steps
| .action _ :: _, _, [] => False
| .action matchesLabel :: rest, _, step :: steps =>
matchesLabel step.transitionLabel ∧ matchesFrom th rest step.nextState steps

/-- A complete execution matching the query, with every assertion at its specified state. -/
@[simp] def Trace.isValidFor (trace : Trace ρ σ l) (sys : RelationalTransitionSystem ρ σ l)
(query : List (Trace.QueryLine ρ σ l)) : Prop :=
trace.isValid sys ∧ trace.failingStep = none ∧
QueryLine.matchesFrom trace.theory query trace.initialState trace.steps.toList

@[grind .]
theorem Trace.push_isValid (trace : Trace ρ σ l) (step : Step σ l) (sys : RelationalTransitionSystem ρ σ l) :
trace.isValid sys →
Expand Down
4 changes: 1 addition & 3 deletions Veil/Core/Tools/Synthesizer/Certificate.lean
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module

public meta import Veil.Core.Tools.Synthesizer.Progress
public meta import Veil.Core.Tools.Synthesizer.Tactic

public meta import Veil.DSL
public meta import Veil.Core.Tools.Synthesizer.Solver
Expand Down Expand Up @@ -42,9 +43,6 @@ public meta section
namespace Veil.Synthesizer
open Lean Elab Command Term Meta Veil Lean.Parser.Term

/-- The axioms a fully checked development rests on. -/
def standardAxioms : List Name := [``propext, ``Classical.choice, ``Quot.sound]

/-- The theorem a certificate added. -/
structure CertificateResult where
/-- `ic3_safe` or `ic3_unsafe`. -/
Expand Down
7 changes: 1 addition & 6 deletions Veil/Core/Tools/Synthesizer/Counterexample.lean
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,12 @@ def fieldValue (className field : Name) (rows : Array (Array Nat)) : FieldSpec
| .function argSorts s => do
let rows ← rows.mapM fun row ↦ return (row.pop, ← c.elementTerm s (row.back?.getD 0))
c.functionTerm argSorts rows (← `(default))
| .axiom _ => `(by first | decide | (intros; simp_all; done) | (intros; grind))
| .axiom _ => modelLaw
| .other => throwError "IC3: field {field} of {className} is neither a relation, a constant, a \
function between sorts nor an axiom; the counterexample certificate cannot build the instance"

end CexCtx

/-- `{ f₁ := v₁, … }`. -/
def structInstance (fields : Array (Name × Term)) : CommandElabM Term := do
let items ← fields.mapM fun (f, v) ↦ `(structInstField| $(mkIdent f):ident := $v)
`({ $items:structInstField,* })

/-- Build and check the counterexample certificate. `property` are the conjuncts of `P`. -/
def certifyUnsafe (mod : Veil.Module) (property : Array Term) (path : DecodedTrace)
(progress? : Option Progress.Ref := none) :
Expand Down
2 changes: 1 addition & 1 deletion Veil/Core/Tools/Synthesizer/Solver.lean
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def runStatementOnce (mod : Veil.Module) (stmt : Term) (steps coreLits : Nat) (w
let ty? ← elabStatement stmt
notePhase (fun p dt ↦ { p with elabMs := p.elabMs + dt }) tElab
let ty ← match ty? with
| .ok ty => pure (nameModelBinders ty (moduleModelNames mod.uninterpretedParameters).toList)
| .ok ty => pure (nameModelBinders ty (moduleModelNames (traceModuleParams mod)).toList)
| .error e => return (.error s!"statement failed to elaborate: {e.take 300}", none)
if fault == some ("throw", steps) then throwError (injectedQueryFailure "throw" steps)
let mv ← mkFreshExprMVar ty
Expand Down
Loading
Loading