Skip to content
Open
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
2 changes: 2 additions & 0 deletions Smt.lean
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public import Smt.Builtin
public meta import Smt.Builtin
public import Smt.Datatype
public meta import Smt.Datatype
public import Smt.Fin
public meta import Smt.Fin
public import Smt.Int
public meta import Smt.Int
public import Smt.Nat
Expand Down
11 changes: 11 additions & 0 deletions Smt/Fin.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/-
Copyright (c) 2021-2026 by the authors listed in the file AUTHORS and their
institutional affiliations. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: George Pîrlea
-/

module

public import Smt.Preprocess.Embedding.Fin
public meta import Smt.Preprocess.Embedding.Fin
9 changes: 7 additions & 2 deletions Smt/Preprocess/Embedding.lean
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def EmbeddingConfig.baseTypes (cfg : EmbeddingConfig) : MetaM (Array Name) := do
bts := bts.push ``Bool
return bts

private def isFinType (e : Expr) : Bool :=
match e.consumeMData with
| .app (.const ``Fin []) _ => true
| _ => false

def hasType (e : Expr) (p : Expr → Bool) : Bool :=
match e with
| .forallE _ t b _ => p t || hasType b p
Expand All @@ -63,7 +68,7 @@ def embeddingWithConfig (cfg : EmbeddingConfig) (mv : MVarId) (hs : Array Expr)
if bts.isEmpty then
return ⟨{}, hs, mv⟩
let fvts ← fvs.mapM FVarId.getType
if !(fvts ++ ts.push (← mv.getType)).any (·.contains ((bts.map (.const · [])).contains ·)) then
if !(fvts ++ ts.push (← mv.getType)).any (·.contains (fun e => (bts.map (.const · [])).contains e.consumeMData || (cfg.embedNat && isFinType e))) then
return ⟨{}, hs, mv⟩
-- Find the embedding simp theorems.
let some thmsExt ← Meta.getSimpExtension? `embedding | throwError "embedding simp extension not found"
Expand Down Expand Up @@ -93,7 +98,7 @@ def embeddingWithConfig (cfg : EmbeddingConfig) (mv : MVarId) (hs : Array Expr)
for fv in fvs do
fvs' := fvs'.push (some fv)
let t ← fv.getType
if hasReturnType t ((bts.map (.const · [])).contains ·) then
if hasReturnType t (fun e => (bts.map (.const · [])).contains e.consumeMData || (cfg.embedNat && isFinType e)) then
fvs' := fvs'.push none
-- Re-introduce all free vars (and their `nonneg` assumptions).
let (fvs'', mv) ← mv.introNP fvs'.size
Expand Down
291 changes: 291 additions & 0 deletions Smt/Preprocess/Embedding/Fin.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
/-
Copyright (c) 2021-2026 by the authors listed in the file AUTHORS and their
institutional affiliations. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: George Pîrlea
-/

module

public import Smt.Preprocess.Embedding.Attribute
public meta import Smt.Preprocess.Embedding.Attribute

public import Lean
public meta import Lean

public section

attribute [embedding ↓] Fin.ext_iff
attribute [embedding ↓] Fin.val_zero
attribute [embedding ↓] Fin.val_one
attribute [embedding ↓] Fin.val_ofNat
attribute [embedding ↓] Fin.mk.injEq

namespace Smt.Preprocess.Embedding

def ofIntFin (n : Nat) (x : Int) (h : 0 ≤ x ∧ x < n) : Fin n :=
⟨x.toNat, (Int.toNat_lt h.1).2 h.2⟩

@[embedding ↓]
theorem Int.natCast_ofIntFin {n : Nat} {x : Int} {h : 0 ≤ x ∧ x < n} :
(((ofIntFin n x h : Fin n) : Nat) : Int) = x :=
Int.toNat_of_nonneg h.1

theorem ofIntFin_of_fin {n : Nat} (x : Fin n)
(h : 0 ≤ ((x : Nat) : Int) ∧ ((x : Nat) : Int) < n) :
ofIntFin n ((x : Nat) : Int) h = x := by
apply Fin.ext
simp [ofIntFin]

def ofIntFinTotal (n : Nat) [NeZero n] (x : Int) : Fin n :=
Fin.ofNat n x.toNat

theorem ofIntFinTotal_of_fin {n : Nat} [NeZero n] (x : Fin n) :
ofIntFinTotal n ((x : Nat) : Int) = x := by
apply Fin.ext
simp [ofIntFinTotal]

@[embedding ↓]
theorem forall_fin_as_int {n : Nat} {p : Fin n → Prop} :
(∀ x : Fin n, p x) ↔
(∀ x : Int, (h : 0 ≤ x ∧ x < n) → p (ofIntFin n x h)) := by
constructor
· intro h x hx
exact h (ofIntFin n x hx)
· intro h x
have hx := h ((x : Nat) : Int) (by
constructor
· exact Int.natCast_nonneg x.val
· exact_mod_cast x.isLt)
simpa [ofIntFin_of_fin] using hx

@[embedding ↓]
theorem exists_fin_as_int {n : Nat} {p : Fin n → Prop} :
(∃ x : Fin n, p x) ↔
(∃ x : Int, ∃ h : 0 ≤ x ∧ x < n, p (ofIntFin n x h)) := by
constructor
· intro ⟨x, hx⟩
refine ⟨(x : Nat), ?_, ?_⟩
· constructor
· exact Int.natCast_nonneg x.val
· exact_mod_cast x.isLt
· simpa [ofIntFin_of_fin] using hx
· intro ⟨x, hx, hp⟩
exact ⟨ofIntFin n x hx, hp⟩

@[embedding ↓]
theorem forall_fin_out_as_int₁ {n : Nat} {p : (α₁ → Fin n) → Prop} :
(∀ f : α₁ → Fin n, p f) ↔
(∀ f : α₁ → Int, (hf : ∀ a₁, 0 ≤ f a₁ ∧ f a₁ < n) →
p (fun a₁ => ofIntFin n (f a₁) (hf a₁))) := by
constructor
· intro h f hf
exact h (fun a₁ => ofIntFin n (f a₁) (hf a₁))
· intro h f
have hf := h (fun a₁ => ((f a₁ : Nat) : Int)) (fun a₁ => by
constructor
· exact Int.natCast_nonneg (f a₁).val
· exact Int.ofNat_lt.mpr (f a₁).isLt)
simpa [ofIntFin_of_fin] using hf

@[embedding ↓]
theorem forall_fin_out_as_int₂ {n : Nat} {p : (α₁ → α₂ → Fin n) → Prop} :
(∀ f : α₁ → α₂ → Fin n, p f) ↔
(∀ f : α₁ → α₂ → Int, (hf : ∀ a₁ a₂, 0 ≤ f a₁ a₂ ∧ f a₁ a₂ < n) →
p (fun a₁ a₂ => ofIntFin n (f a₁ a₂) (hf a₁ a₂))) := by
constructor
· intro h f hf
exact h (fun a₁ a₂ => ofIntFin n (f a₁ a₂) (hf a₁ a₂))
· intro h f
have hf := h (fun a₁ a₂ => ((f a₁ a₂ : Nat) : Int)) (fun a₁ a₂ => by
constructor
· exact Int.natCast_nonneg (f a₁ a₂).val
· exact Int.ofNat_lt.mpr (f a₁ a₂).isLt)
simpa [ofIntFin_of_fin] using hf

@[embedding ↓]
theorem forall_fin_out_as_int₃ {n : Nat} {p : (α₁ → α₂ → α₃ → Fin n) → Prop} :
(∀ f : α₁ → α₂ → α₃ → Fin n, p f) ↔
(∀ f : α₁ → α₂ → α₃ → Int,
(hf : ∀ a₁ a₂ a₃, 0 ≤ f a₁ a₂ a₃ ∧ f a₁ a₂ a₃ < n) →
p (fun a₁ a₂ a₃ => ofIntFin n (f a₁ a₂ a₃) (hf a₁ a₂ a₃))) := by
constructor
· intro h f hf
exact h (fun a₁ a₂ a₃ => ofIntFin n (f a₁ a₂ a₃) (hf a₁ a₂ a₃))
· intro h f
have hf := h (fun a₁ a₂ a₃ => ((f a₁ a₂ a₃ : Nat) : Int)) (fun a₁ a₂ a₃ => by
constructor
· exact Int.natCast_nonneg (f a₁ a₂ a₃).val
· exact Int.ofNat_lt.mpr (f a₁ a₂ a₃).isLt)
simpa [ofIntFin_of_fin] using hf

@[embedding ↓]
theorem forall_fin_out_as_int₄ {n : Nat} {p : (α₁ → α₂ → α₃ → α₄ → Fin n) → Prop} :
(∀ f : α₁ → α₂ → α₃ → α₄ → Fin n, p f) ↔
(∀ f : α₁ → α₂ → α₃ → α₄ → Int,
(hf : ∀ a₁ a₂ a₃ a₄, 0 ≤ f a₁ a₂ a₃ a₄ ∧ f a₁ a₂ a₃ a₄ < n) →
p (fun a₁ a₂ a₃ a₄ => ofIntFin n (f a₁ a₂ a₃ a₄) (hf a₁ a₂ a₃ a₄))) := by
constructor
· intro h f hf
exact h (fun a₁ a₂ a₃ a₄ => ofIntFin n (f a₁ a₂ a₃ a₄) (hf a₁ a₂ a₃ a₄))
· intro h f
have hf := h (fun a₁ a₂ a₃ a₄ => ((f a₁ a₂ a₃ a₄ : Nat) : Int)) (fun a₁ a₂ a₃ a₄ => by
constructor
· exact Int.natCast_nonneg (f a₁ a₂ a₃ a₄).val
· exact Int.ofNat_lt.mpr (f a₁ a₂ a₃ a₄).isLt)
simpa [ofIntFin_of_fin] using hf

@[embedding ↓]
theorem forall_fin_out_as_int₅ {n : Nat} {p : (α₁ → α₂ → α₃ → α₄ → α₅ → Fin n) → Prop} :
(∀ f : α₁ → α₂ → α₃ → α₄ → α₅ → Fin n, p f) ↔
(∀ f : α₁ → α₂ → α₃ → α₄ → α₅ → Int,
(hf : ∀ a₁ a₂ a₃ a₄ a₅, 0 ≤ f a₁ a₂ a₃ a₄ a₅ ∧ f a₁ a₂ a₃ a₄ a₅ < n) →
p (fun a₁ a₂ a₃ a₄ a₅ => ofIntFin n (f a₁ a₂ a₃ a₄ a₅) (hf a₁ a₂ a₃ a₄ a₅))) := by
constructor
· intro h f hf
exact h (fun a₁ a₂ a₃ a₄ a₅ => ofIntFin n (f a₁ a₂ a₃ a₄ a₅) (hf a₁ a₂ a₃ a₄ a₅))
· intro h f
have hf := h (fun a₁ a₂ a₃ a₄ a₅ => ((f a₁ a₂ a₃ a₄ a₅ : Nat) : Int))
(fun a₁ a₂ a₃ a₄ a₅ => by
constructor
· exact Int.natCast_nonneg (f a₁ a₂ a₃ a₄ a₅).val
· exact Int.ofNat_lt.mpr (f a₁ a₂ a₃ a₄ a₅).isLt)
simpa [ofIntFin_of_fin] using hf

@[embedding ↓]
theorem forall_fin_in_as_int₁ {n : Nat} [NeZero n] {p : (Fin n → α₁) → Prop} :
(∀ f : Fin n → α₁, p f) ↔
(∀ f : Int → α₁, p (fun a => f ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a => f ((a : Nat) : Int))
· intro h f
have hf := h (fun b => f (ofIntFinTotal n b))
simpa [ofIntFinTotal_of_fin] using hf

@[embedding ↓]
theorem forall_fin_in_as_int₂ {n : Nat} [NeZero n] {p : (α₁ → Fin n → α₂) → Prop} :
(∀ f : α₁ → Fin n → α₂, p f) ↔
(∀ f : α₁ → Int → α₂, p (fun a₁ a => f a₁ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a => f a₁ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ b => f a₁ (ofIntFinTotal n b))
simpa [ofIntFinTotal_of_fin] using hf

@[embedding ↓]
theorem forall_fin_in_as_int₃ {n : Nat} [NeZero n] {p : (α₁ → α₂ → Fin n → α₃) → Prop} :
(∀ f : α₁ → α₂ → Fin n → α₃, p f) ↔
(∀ f : α₁ → α₂ → Int → α₃, p (fun a₁ a₂ a => f a₁ a₂ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a₂ a => f a₁ a₂ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ a₂ b => f a₁ a₂ (ofIntFinTotal n b))
simpa [ofIntFinTotal_of_fin] using hf

@[embedding ↓]
theorem forall_fin_in_as_int₄ {n : Nat} [NeZero n] {p : (α₁ → α₂ → α₃ → Fin n → α₄) → Prop} :
(∀ f : α₁ → α₂ → α₃ → Fin n → α₄, p f) ↔
(∀ f : α₁ → α₂ → α₃ → Int → α₄, p (fun a₁ a₂ a₃ a => f a₁ a₂ a₃ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a₂ a₃ a => f a₁ a₂ a₃ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ a₂ a₃ b => f a₁ a₂ a₃ (ofIntFinTotal n b))
simpa [ofIntFinTotal_of_fin] using hf

@[embedding ↓]
theorem forall_fin_in_as_int₅ {n : Nat} [NeZero n] {p : (α₁ → α₂ → α₃ → α₄ → Fin n → α₅) → Prop} :
(∀ f : α₁ → α₂ → α₃ → α₄ → Fin n → α₅, p f) ↔
(∀ f : α₁ → α₂ → α₃ → α₄ → Int → α₅,
p (fun a₁ a₂ a₃ a₄ a => f a₁ a₂ a₃ a₄ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a₂ a₃ a₄ a => f a₁ a₂ a₃ a₄ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ a₂ a₃ a₄ b => f a₁ a₂ a₃ a₄ (ofIntFinTotal n b))
simpa [ofIntFinTotal_of_fin] using hf

/-- Extends `f : Fin n → β` to all integers, with `default` outside `0 ≤ b < n`. -/
def extFin {n : Nat} {β : Type _} [Inhabited β] (f : Fin n → β) (b : Int) : β :=
if hb : 0 ≤ b ∧ b < n then f (ofIntFin n b hb) else default

theorem extFin_cast {n : Nat} {β : Type _} [Inhabited β] (f : Fin n → β) (a : Fin n) :
extFin f ((a : Nat) : Int) = f a := by
have hb : 0 ≤ ((a : Nat) : Int) ∧ ((a : Nat) : Int) < n :=
⟨Int.natCast_nonneg a.val, Int.ofNat_lt.mpr a.isLt⟩
unfold extFin
split
· exact congrArg f (ofIntFin_of_fin a _)
· rename_i hn
exact absurd hb hn

-- The rules below rewrite a function over `Fin n` for a symbolic `n` (where no `NeZero n` instance
-- exists) into a function over `Int`. They need the result type to be inhabited instead, because the
-- extension outside `0 ≤ b < n` needs a default value. Without them an `ofIntFin n x h` application
-- stays behind, and its proof argument `h` cannot be transported when `n` is itself rewritten under
-- the nonnegativity hypothesis of the `Nat` embedding.

@[embedding ↓]
theorem forall_fin_in_as_int_inh₁ {n : Nat} {α₁ : Type _} [Inhabited α₁] {p : (Fin n → α₁) → Prop} :
(∀ f : Fin n → α₁, p f) ↔
(∀ f : Int → α₁, p (fun a => f ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a => f ((a : Nat) : Int))
· intro h f
have hf := h (extFin f)
simpa [extFin_cast] using hf

@[embedding ↓]
theorem forall_fin_in_as_int_inh₂ {n : Nat} {α₁ α₂ : Type _} [Inhabited α₂]
{p : (α₁ → Fin n → α₂) → Prop} :
(∀ f : α₁ → Fin n → α₂, p f) ↔
(∀ f : α₁ → Int → α₂, p (fun a₁ a => f a₁ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a => f a₁ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ => extFin (f a₁))
simpa [extFin_cast] using hf

@[embedding ↓]
theorem forall_fin_in_as_int_inh₃ {n : Nat} {α₁ α₂ α₃ : Type _} [Inhabited α₃]
{p : (α₁ → α₂ → Fin n → α₃) → Prop} :
(∀ f : α₁ → α₂ → Fin n → α₃, p f) ↔
(∀ f : α₁ → α₂ → Int → α₃, p (fun a₁ a₂ a => f a₁ a₂ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a₂ a => f a₁ a₂ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ a₂ => extFin (f a₁ a₂))
simpa [extFin_cast] using hf

@[embedding ↓]
theorem forall_fin_in_as_int_inh₄ {n : Nat} {α₁ α₂ α₃ α₄ : Type _} [Inhabited α₄]
{p : (α₁ → α₂ → α₃ → Fin n → α₄) → Prop} :
(∀ f : α₁ → α₂ → α₃ → Fin n → α₄, p f) ↔
(∀ f : α₁ → α₂ → α₃ → Int → α₄, p (fun a₁ a₂ a₃ a => f a₁ a₂ a₃ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a₂ a₃ a => f a₁ a₂ a₃ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ a₂ a₃ => extFin (f a₁ a₂ a₃))
simpa [extFin_cast] using hf

@[embedding ↓]
theorem forall_fin_in_as_int_inh₅ {n : Nat} {α₁ α₂ α₃ α₄ α₅ : Type _} [Inhabited α₅]
{p : (α₁ → α₂ → α₃ → α₄ → Fin n → α₅) → Prop} :
(∀ f : α₁ → α₂ → α₃ → α₄ → Fin n → α₅, p f) ↔
(∀ f : α₁ → α₂ → α₃ → α₄ → Int → α₅,
p (fun a₁ a₂ a₃ a₄ a => f a₁ a₂ a₃ a₄ ((a : Nat) : Int))) := by
constructor
· intro h f
exact h (fun a₁ a₂ a₃ a₄ a => f a₁ a₂ a₃ a₄ ((a : Nat) : Int))
· intro h f
have hf := h (fun a₁ a₂ a₃ a₄ => extFin (f a₁ a₂ a₃ a₄))
simpa [extFin_cast] using hf

end Smt.Preprocess.Embedding
8 changes: 8 additions & 0 deletions Smt/Translate/Int.lean
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public meta section

namespace Smt.Translate.Int

open Lean Expr
open Translator Term

private def mkInt : Lean.Expr :=
Expand All @@ -28,6 +29,8 @@ private def mkInt : Lean.Expr :=
@[smt_translate] def translateInt : Translator := fun e => do
if let some n := e.natLitOf? mkInt then
return literalT (toString n)
else if let some n := natCastInt? e then
return ← applyTranslators! n
else if let some x := e.negOf? mkInt then
return appT (symbolT "-") (← applyTranslators! x)
else if let some (x, y) := e.hAddOf? mkInt mkInt then
Expand All @@ -42,6 +45,11 @@ private def mkInt : Lean.Expr :=
return mkApp2 (symbolT "mod") (← applyTranslators! x) (← applyTranslators! y)
else
return none
where
natCastInt? : Expr → Option Expr
| .app (.app (.app (.const ``Nat.cast _) α) _) n =>
if α.consumeMData == mkInt then some n else none
| _ => none

@[smt_translate] def translateProp : Translator := fun e => do
if let some (x, y) := e.ltOf? mkInt then
Expand Down
2 changes: 1 addition & 1 deletion Test/Bool/Assoc.expected
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Test/Bool/Assoc.lean:5:2: error: unable to prove goal, either it is false or you need to provide more facts. Here is a potential counter-example:

q = False
r = False
f = fun _arg_1 _arg_2 => if _arg_1 then ¬_arg_2 else ¬_arg_1 ∧ ¬_arg_2
r = False
p = True
4 changes: 2 additions & 2 deletions Test/Bool/Comm.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Test/Bool/Comm.lean:4:2: error: unable to prove goal, either it is false or you need to provide more facts. Here is a potential counter-example:

q = True
f = fun _arg_1 _arg_2 => _arg_1 ∧ ¬_arg_2
p = False
f = fun _arg_1 _arg_2 => _arg_1 ∧ ¬_arg_2
q = True
3 changes: 3 additions & 0 deletions Test/Fin.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test/Fin.lean:3:67: error: unable to prove goal, either it is false or you need to provide more facts. Try adding '+model' config option to display a potential counter-example (experimental).
Test/Fin.lean:5:71: error: unable to prove goal, either it is false or you need to provide more facts. Try adding '+model' config option to display a potential counter-example (experimental).
Test/Fin.lean:33:49: error: unable to prove goal, either it is false or you need to provide more facts. Try adding '+model' config option to display a potential counter-example (experimental).
Loading