Skip to content

Conversation

@dtumad
Copy link
Contributor

@dtumad dtumad commented Aug 5, 2025

Using e.g. 2^16 in place of 2 <<< 16 is what is causing most of the "(kernel) deep recursion detected" issues. This PR swaps to that form, and also tries to just unfold Word.toNat less in general. This happens in the kernel after Lean has unfolded the function calls, so just no opening up Word.toNat isn't enough, we need to actually change this.

import Mathlib

example (n : ℕ) :
    (match
      (match BitVec.ofNat 64 (n * 2^16) with
      | 0#64 => some false
      | _ => some true) with
    | none => ()
    | some _ => ()) =
    if 1 + 1 = 2 then () else () := by
  rw [one_add_one_eq_two] -- causes deep recursion error

example (n : ℕ) :
    (match
      (match BitVec.ofNat 64 (n <<< 16) with
      | 0#64 => some false
      | _ => some true) with
    | none => ()
    | some _ => ()) =
    if 1 + 1 = 2 then () else () := by
  rw [one_add_one_eq_two] -- no deep recursion error

@dtumad dtumad added the WIP Do Not Merge label Aug 5, 2025
@dtumad dtumad requested a review from PetarMax August 5, 2025 03:01
@dtumad dtumad changed the title Fix Deep Recursion by Using Bitshift Repr WIP: Fix Deep Recursion by Using Bitshift Repr Aug 5, 2025
@GZGavinZhao
Copy link
Collaborator

You can merge this if you want to. Don't worry about me.

simp_all

constructor <;> intro sgn <;> (repeat rw [this]) <;> clear this <;> simp_all
stop
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is going wrong here. split_ands hangs, and then when I override that with [ skip; constructor; constructor; constructor; constructor ] the following split_ifs` hangs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I was only searching for sorry to fix (no pun intended). Will look at this and why they are stalling

intro h_op1_isU64 h_op2_isU64
have ha' := Word.lt_cases_of_isU64 h_op1_isU64
have hb' := Word.lt_cases_of_isU64 h_op2_isU64
cases op <;> simp [execute_RTYPEW_pure] <;> congr
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This congr hangs as well, it didn't before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems maybe related to the fact that ofNat was causing deep recursion issues when it wasn't already unfolded (and it isn't @[reducible]). Hadn't looked very closely when just refactoring the proof.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly. I'm not sure how that would come up in this proof, given that the first change is here...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought was that one side contains e.g. a x * 2^16 and the other a x <<< 16, and Lean is both somehow seeing that, and trying to unify them in congruence which is difficult. I'll investigate more when I get home. This is definitely not the getElem change (given the location of the macro), and none of the bit-shifts should be in scope IIRC (will check when I'm back home)

@dtumad
Copy link
Contributor Author

dtumad commented Aug 19, 2025

This branch has grown too large and still isn't fully finished. Closing in favor of cherry picking individual commits to merge incrementally

@dtumad dtumad closed this Aug 19, 2025
@dtumad dtumad deleted the dtumad/deep-recursion-fixes branch October 2, 2025 18:33
@dtumad dtumad restored the dtumad/deep-recursion-fixes branch October 2, 2025 18:33
@dtumad dtumad deleted the dtumad/deep-recursion-fixes branch October 2, 2025 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WIP Do Not Merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants