Zulip Chat Archive

Stream: mathlib4

Topic: Type class inference failed even though instance exists


Andrey Yao (Oct 30 2024 at 16:31):

import Mathlib.Order.Defs
import Mathlib.Order.BooleanAlgebra
import Mathlib.Order.Lattice

instance : BoundedOrder Bool := by infer_instance

lemma list_fold_lattice_bot {α β : Type} [DistribLattice α] [BoundedOrder α] :
   (f : β -> α) (l : List β), List.foldl (fun acc e => acc  f e)  l =  := by
  intros f l
  induction l with
  | nil => rfl
  | cons hd tl IH => simp; exact IH

lemma list_fold_true_impl_forall_true {β : Type} :
   (f : β -> Bool) (l : List β), List.foldl (fun acc e => acc  f e)  l =  
   e  l, f e =  := by
  intros f l eq; induction l with
  | nil => simp
  | cons hd tl IHtl => simp; cases val : (f hd) with
    | true => aesop
    | false =>
      simp at eq; apply And.intro
      . rw [val] at eq; rw [<- list_fold_lattice_bot f tl] at eq
      . rw [val, list_fold_lattice_bot] at eq; apply IHtl eq

Hello! I am trying to prove the lemma list_fold_true_impl_forall_true, but at the second to last line, Lean4 complains that tactic 'rewrite' failed, did not find instance of the pattern in the target expression ⊥, even though there is one. How would one go about fixing/debuggin these typeclass inference-related errors? Thanks

Damiano Testa (Oct 30 2024 at 16:51):

I think that rw is just telling you that it cannot find in your expression. If you get it to be there, then your next rw seems to work: try adding

rw [ bot_eq_false] at eq

to make Lean represent false as .

Ruben Van de Velde (Oct 30 2024 at 16:52):

Why are you using lattice operations on booleans anyway?

Andrey Yao (Oct 30 2024 at 17:01):

Damiano Testa said:

I think that rw is just telling you that it cannot find in your expression. If you get it to be there, then your next rw seems to work: try adding

rw [ bot_eq_false] at eq

to make Lean represent false as .

Thanks! It worked by adding some rewrites

Andrey Yao (Oct 30 2024 at 17:02):

Ruben Van de Velde said:

Why are you using lattice operations on booleans anyway?

I'm trying to prove some more general results using bounded distributive lattices, and the boolean one is just one of the examples


Last updated: May 02 2025 at 03:31 UTC