Compare commits

..

4 Commits

Author SHA1 Message Date
9ab43b34ef Use mathlib definition of inverses for Isomorphism.lean
Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
2026-06-24 14:32:50 -05:00
97a9150bf3 Simplify the strict-step extraction in LTSeries.exists_unzip
Derive c.head < c 1 from the series' StrictMono instance and Fin.one_pos'
instead of unfolding c.step with manual Fin.succ index arithmetic.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 14:23:13 -05:00
93f913a699 Clean up namespaces in the analysis framework
- Wrap the forward-analysis framework in a Spa.Forward namespace so its
  generic names (analyze, result, joinAll, variablesAt, ...) no longer
  sit flat in Spa, matching the ConstAnalysis/SignAnalysis convention.
- Merge the split Graph namespace in Graphs.lean by relocating buildCfg.
- Use nested namespace Spa / Fixedpoint instead of Spa.Fixedpoint.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 13:56:16 -05:00
7fb9d9aa19 Clean up Lattice.lean's namespaces
Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
2026-06-24 13:56:16 -05:00
12 changed files with 44 additions and 38 deletions

View File

@@ -5,6 +5,8 @@ import Spa.Showable
namespace Spa namespace Spa
open Forward
abbrev ConstLattice : Type := AboveBelow abbrev ConstLattice : Type := AboveBelow
namespace ConstAnalysis namespace ConstAnalysis
@@ -157,7 +159,7 @@ instance eval_valid : ValidExprEvaluator ConstLattice prog := by
theorem analyze_correct {ρ : Env} (hrun : EvalStmt [] prog.rootStmt ρ) : theorem analyze_correct {ρ : Env} (hrun : EvalStmt [] prog.rootStmt ρ) :
variablesAt prog.finalState (result ConstLattice prog) ρ := variablesAt prog.finalState (result ConstLattice prog) ρ :=
Spa.analyze_correct ConstLattice prog hrun Forward.analyze_correct ConstLattice prog hrun
end ConstAnalysis end ConstAnalysis

View File

@@ -5,6 +5,8 @@ import Spa.Fixedpoint
namespace Spa namespace Spa
namespace Forward
variable {L : Type} [Lattice L] {prog : Program} [E : StmtEvaluator L prog] variable {L : Type} [Lattice L] {prog : Program} [E : StmtEvaluator L prog]
def updateVariablesForState (s : prog.State) (sv : StateVariables L prog) : def updateVariablesForState (s : prog.State) (sv : StateVariables L prog) :
@@ -116,4 +118,6 @@ theorem analyze_correct {ρ : Env} (hrun : EvalStmt [] prog.rootStmt ρ) :
variablesAt prog.finalState (result L prog) ρ := variablesAt prog.finalState (result L prog) ρ :=
walkTrace interp_joinForKey_initialState (prog.trace hrun) walkTrace interp_joinForKey_initialState (prog.trace hrun)
end Forward
end Spa end Spa

View File

@@ -2,6 +2,8 @@ import Spa.Analysis.Forward.Evaluation
namespace Spa namespace Spa
namespace Forward
variable {L : Type} [Lattice L] {prog : Program} [E : ExprEvaluator L prog] variable {L : Type} [Lattice L] {prog : Program} [E : ExprEvaluator L prog]
def updateVariablesFromExpression (k : String) (e : Expr) def updateVariablesFromExpression (k : String) (e : Expr)
@@ -51,4 +53,6 @@ instance ExprEvaluator.toStmtEvaluator_valid [LatticeInterpretation L]
(fun hmem => hne (List.mem_singleton.mp hmem)) hk'l₀ (fun hmem => hne (List.mem_singleton.mp hmem)) hk'l₀
exact hvs _ _ hk'l' _ hmem' exact hvs _ _ hk'l' _ hmem'
end Forward
end Spa end Spa

View File

@@ -2,6 +2,8 @@ import Spa.Analysis.Forward.Lattices
namespace Spa namespace Spa
namespace Forward
variable (L : Type) [Lattice L] (prog : Program) variable (L : Type) [Lattice L] (prog : Program)
class StmtEvaluator where class StmtEvaluator where
@@ -23,4 +25,6 @@ class ValidStmtEvaluator [E : StmtEvaluator L prog] [LatticeInterpretation L] :
{bs : BasicStmt}, {bs : BasicStmt},
EvalBasicStmt ρ₁ bs ρ₂ vs ρ₁ E.eval s bs vs ρ₂ EvalBasicStmt ρ₁ bs ρ₂ vs ρ₁ E.eval s bs vs ρ₂
end Forward
end Spa end Spa

View File

@@ -4,6 +4,8 @@ import Spa.Interp
namespace Spa namespace Spa
namespace Forward
variable (L : Type) [Lattice L] (prog : Program) variable (L : Type) [Lattice L] (prog : Program)
abbrev VariableValues : Type := FiniteMap String L prog.vars abbrev VariableValues : Type := FiniteMap String L prog.vars
@@ -96,4 +98,6 @@ theorem interp_foldr {vs : VariableValues L prog}
· exact interp_sup (Or.inl hvs) · exact interp_sup (Or.inl hvs)
· exact interp_sup (Or.inr (ih hmem')) · exact interp_sup (Or.inr (ih hmem'))
end Forward
end Spa end Spa

View File

@@ -5,6 +5,8 @@ import Spa.Showable
namespace Spa namespace Spa
open Forward
inductive Sign where inductive Sign where
| plus | plus
| minus | minus
@@ -215,7 +217,7 @@ instance eval_valid : ValidExprEvaluator SignLattice prog := by
theorem analyze_correct {ρ : Env} (hrun : EvalStmt [] prog.rootStmt ρ) : theorem analyze_correct {ρ : Env} (hrun : EvalStmt [] prog.rootStmt ρ) :
variablesAt prog.finalState (result SignLattice prog) ρ := variablesAt prog.finalState (result SignLattice prog) ρ :=
Spa.analyze_correct SignLattice prog hrun Forward.analyze_correct SignLattice prog hrun
end SignAnalysis end SignAnalysis

View File

@@ -1,6 +1,8 @@
import Spa.Lattice import Spa.Lattice
namespace Spa.Fixedpoint namespace Spa
namespace Fixedpoint
open FiniteHeightLattice (height) open FiniteHeightLattice (height)
@@ -49,4 +51,6 @@ theorem aFix_le (f : αα) (hf : Monotone f)
{a : α} (ha : a = f a) : aFix f hf a := {a : α} (ha : a = f a) : aFix f hf a :=
doStep_le f hf ha _ _ _ _ (by simpa using FiniteHeightLattice.bot_le α a) doStep_le f hf ha _ _ _ _ (by simpa using FiniteHeightLattice.bot_le α a)
end Spa.Fixedpoint end Fixedpoint
end Spa

View File

@@ -5,20 +5,18 @@ namespace Spa
def FiniteHeightLattice.transport {α β : Type*} [Lattice α] [Lattice β] def FiniteHeightLattice.transport {α β : Type*} [Lattice α] [Lattice β]
[I : FiniteHeightLattice α] (f : α β) (g : β α) [I : FiniteHeightLattice α] (f : α β) (g : β α)
(hf : Monotone f) (hg : Monotone g) (hf : Monotone f) (hg : Monotone g)
(hgf : a, g (f a) = a) (hfg : b, f (g b) = b) : (hgf : Function.LeftInverse g f) (hfg : Function.LeftInverse f g) :
FiniteHeightLattice β where FiniteHeightLattice β where
bot := f bot := f
top := f top := f
height := I.height height := I.height
longestChain := longestChain :=
{ series := { series :=
I.longestChain.series.map f I.longestChain.series.map f (hf.strictMono_of_injective hgf.injective)
(hf.strictMono_of_injective (Function.LeftInverse.injective hgf))
head_series := congrArg f I.longestChain.head_series head_series := congrArg f I.longestChain.head_series
last_series := congrArg f I.longestChain.last_series last_series := congrArg f I.longestChain.last_series
length_series := I.longestChain.length_series } length_series := I.longestChain.length_series }
chains_bounded := fun c => chains_bounded := fun c =>
I.chains_bounded I.chains_bounded (c.map g (hg.strictMono_of_injective hfg.injective))
(c.map g (hg.strictMono_of_injective (Function.LeftInverse.injective hfg)))
end Spa end Spa

View File

@@ -89,17 +89,6 @@ def singleton (bss : List BasicStmt) : Graph where
def wrap (g : Graph) : Graph := def wrap (g : Graph) : Graph :=
singleton [] g singleton [] singleton [] g singleton []
end Graph
open Graph in
def buildCfg : Stmt Graph
| .basic bs => Graph.singleton [bs]
| .andThen s₁ s₂ => buildCfg s₁ buildCfg s₂
| .ifElse _ s₁ s₂ => buildCfg s₁ buildCfg s₂
| .whileLoop _ s => Graph.loop (buildCfg s)
namespace Graph
variable (g : Graph) variable (g : Graph)
def indices : List g.Index := List.finRange g.size def indices : List g.Index := List.finRange g.size
@@ -123,4 +112,11 @@ theorem edge_of_mem_predecessors {idx₁ idx₂ : g.Index}
end Graph end Graph
open Graph in
def buildCfg : Stmt Graph
| .basic bs => Graph.singleton [bs]
| .andThen s₁ s₂ => buildCfg s₁ buildCfg s₂
| .ifElse _ s₁ s₂ => buildCfg s₁ buildCfg s₂
| .whileLoop _ s => Graph.loop (buildCfg s)
end Spa end Spa

View File

@@ -61,12 +61,11 @@ class FiniteHeightLattice (α : Type*) [Lattice α] extends Bot α, Top α where
longestChain : PointedLTSeries α height longestChain : PointedLTSeries α height
chains_bounded : BoundedChains α height chains_bounded : BoundedChains α height
namespace FixedHeight namespace FiniteHeightLattice
variable {α : Type*} [Lattice α] {h : } variable (α : Type*) [Lattice α] [FiniteHeightLattice α]
theorem bot_le [FiniteHeightLattice α] : (a : α), a := by theorem bot_le (a : α) : ( : α) a := by
intro a
by_cases heq : a = by_cases heq : a =
· exact inf_eq_left.mp heq · exact inf_eq_left.mp heq
· exfalso · exfalso
@@ -78,14 +77,6 @@ theorem bot_le [FiniteHeightLattice α] : ∀ (a : α), ⊥ ≤ a := by
rw [RelSeries.cons_length, lc.length_series] at hbound rw [RelSeries.cons_length, lc.length_series] at hbound
omega omega
end FixedHeight
namespace FiniteHeightLattice
variable (α : Type*) [Lattice α] [FiniteHeightLattice α]
theorem bot_le (a : α) : ( : α) a := FixedHeight.bot_le a
end FiniteHeightLattice end FiniteHeightLattice
end Spa end Spa

View File

@@ -516,7 +516,7 @@ def fixedHeight [FiniteHeightLattice B] (ks : List A) :
FiniteHeightLattice (FiniteMap A B ks) := FiniteHeightLattice (FiniteMap A B ks) :=
FiniteHeightLattice.transport FiniteHeightLattice.transport
(ofIter ks) toIter (ofIter_monotone ks) toIter_monotone (ofIter ks) toIter (ofIter_monotone ks) toIter_monotone
(toIter_ofIter ks) (fun fm => ofIter_toIter fm) (toIter_ofIter ks) ofIter_toIter
instance [FiniteHeightLattice B] : FiniteHeightLattice (FiniteMap A B ks) := instance [FiniteHeightLattice B] : FiniteHeightLattice (FiniteMap A B ks) :=
fixedHeight ks fixedHeight ks

View File

@@ -27,16 +27,13 @@ theorem LTSeries.exists_unzip (c : LTSeries (α × β)) :
| succ n ih => | succ n ih =>
intro c hn intro c hn
have h0 : c.length 0 := by omega have h0 : c.length 0 := by omega
haveI : NeZero c.length := h0
obtain c₁, c₂, hh₁, hl₁, hh₂, hl₂, hlen := obtain c₁, c₂, hh₁, hl₁, hh₂, hl₂, hlen :=
ih (c.tail h0) (by simp [RelSeries.tail_length, hn]) ih (c.tail h0) (by simp [RelSeries.tail_length, hn])
rw [RelSeries.last_tail] at hl₁ hl₂ rw [RelSeries.last_tail] at hl₁ hl₂
rw [RelSeries.head_tail] at hh₁ hh₂ rw [RelSeries.head_tail] at hh₁ hh₂
rw [RelSeries.tail_length] at hlen rw [RelSeries.tail_length] at hlen
have hstep : c.head < c 1 := by have hstep : c.head < c 1 := c.strictMono Fin.one_pos'
have h := c.step 0, by omega
have h1 : (0, by omega : Fin c.length).succ = 1 := by
ext; simp [Fin.val_one, Nat.mod_eq_of_lt (by omega : 1 < c.length + 1)]
rwa [h1] at h
obtain hle1, hle2 := Prod.le_def.mp hstep.le obtain hle1, hle2 := Prod.le_def.mp hstep.le
rcases eq_or_lt_of_le hle1 with heq1 | hlt1 <;> rcases eq_or_lt_of_le hle1 with heq1 | hlt1 <;>
rcases eq_or_lt_of_le hle2 with heq2 | hlt2 rcases eq_or_lt_of_le hle2 with heq2 | hlt2