Zulip Chat Archive

Stream: new members

Topic: Infinite recursing macro?


Joseph O (Mar 16 2022 at 13:13):

Im using lean 4, and i have this macro. I am wondering why it is infinitely recursing:

inductive Nat' where
  | zero
  | succ : Nat'  Nat'
  deriving Repr

open Nat'

syntax (name := to_nat') "nat!" term : term
macro_rules (kind := to_nat')
  | `(nat! 0) => `(Nat'.zero)
  | `(nat! $n) => `(Nat'.succ $ nat!($n-1))

#eval let seven := nat! 7; seven
/- maximum recursion depth has been reached (use `set_option maxRecDepth <num>` to increase limit) -/

Yakov Pechersky (Mar 16 2022 at 13:26):

Won't nat! 0 match both rules? And then nat!($n-1) gives nat!0 because 0 - 1 = 0?

Joseph O (Mar 16 2022 at 13:32):

Yes, but shouldn't it stop on the first rule?

Mauricio Collares (Mar 16 2022 at 13:45):

1-1 probably does not match 0 for macro expansion purposes

Joseph O (Mar 16 2022 at 13:45):

Then do you have an alternative solution?


Last updated: Dec 20 2023 at 11:08 UTC