Stream: Beginner Questions

Topic: Defining a recursive function within a proof


view this post on Zulip John Hughes (Nov 15 2026 at 14:01):

I can define a function inside a proof like this (the lemma statement is just there to let me open up a proof context):

lemma example:
  fixes k::nat
  shows "k < k+1"
proof -
  define n::nat where "n = 4"

  define g::"nat ⇒ nat" where
  "g  = (λ k .  k + n)"

and it's OK for g to refer to the 'local variable' n without it being another argument to the function. But is there a way to write a recursive function --- the kind of thing I'd define using fun outside the context of a theorem? And more important, one that uses a local variable in a similar way. For instance, suppose I wanted to define g(0) = 5 and g(Suc k) = k + n (where n here is the constant in the definition above). Is there a way to do this?

To give a little more context, my actual lemma looks like this:

lemma
  fixes X:: "'a set"
  fixes Y:: "'b set"
  fixes f:: "'a ⇒ 'b"
  fixes g:: "'b ⇒ 'a"
  assumes injf: "inj_on f X"
  assumes injg: "inj_on g Y"
  assumes ...
  shows ...
proof -
  define R where Rdef: "R = X - g`Y"
  define h where hdef: "h = g ∘ f"

  fun Xset :: "nat ⇒  'a set" where
  "Xset 0 = X - g`Y" |
  "Xset (Suc n)  = h`(Xset n)"

except that the fun definition generates an error "Bad context for command "fun" -- using reset state", with a note that variables X, g, and Y occur only on the right hand side in the first branch of the function definition.

view this post on Zulip Mathias Fleury (Nov 15 2026 at 14:30):

sort of. You can use rec_nat and have to derive the simplification lemmas yourself

view this post on Zulip Mathias Fleury (Nov 15 2026 at 14:32):

  define Xset where ‹Xset = rec_nat (X - g`Y) (λ_ a. h ` a)›
  have [simp]: ‹Xset 0 = X - g`Y› and ‹Xset (Suc n)  = h`(Xset n)› for n
    unfolding Xset_def by auto

view this post on Zulip Mathias Fleury (Nov 15 2026 at 14:38):

I have done this a few times, but I don't remember a case where the translation was not obvious.

view this post on Zulip Mathias Fleury (Nov 15 2026 at 14:39):

primrec is actually using the recursor, so you can get the definition directly:

primrec Xset :: "nat ⇒  'a set" where
  "Xset 0 = X - g`Y" |
  "Xset (Suc n)  = h`(Xset n)"

  thm Xset_def
(*local.Xset ≡ rec_nat (X - g ` Y) (λn. (`) h)*)

view this post on Zulip Mathias Fleury (Nov 15 2026 at 14:42):

fun is using fixpoints

declare [[function_internals]]
context

  fixes X:: "'a set"
  fixes Y:: "'b set"
  fixes f:: "'a ⇒ 'b"
  fixes g:: "'b ⇒ 'a"
  fixes h:: "'a ⇒ 'a"
begin
  fun Xset :: "nat ⇒  'a set" where
  "Xset 0 = X - g`Y" |
  "Xset (Suc n)  = h`(Xset n)"

  thm  nitpick_unfold(161)
(*
local.Xset_graph ≡ lfp (λp x1 x2. (∃Xset_sum. x1 = 0 ∧ x2 = X - g ` Y) ∨ (∃Xset_sum n. x1 = Suc n ∧ x2 = h ` Xset_sum n ∧ p n (Xset_sum n)))
*

so if you need full fun power, it is more (or even a lot) of work to translate the function back to the recursor

view this post on Zulip Mathias Fleury (Nov 15 2026 at 14:42):

That being said, I don't ever remember needing that (a function that is not definable using primrec).

view this post on Zulip John Hughes (Nov 15 2026 at 16:09):

I don't follow a lot of this ... I'll have to look at the definition of rec_nat for it to make sense, but it clearly does the job.

I'm not sure what you mean by "primrec is actually using the recursor ...", because when I type that primrec definition before my lemma, it says that Y is an extra variable on the RHS, and if I type it inside the lemma I get a "bad context for primrec". Any chance you can explain about this?

The final paragraph feels a bit like what I'd like, but I'm not sure how it fits into the bigger picture. Fortunately, I don't think I need that full power, and the "a lot of work to translate back" says that I should probably avoid this for now.

view this post on Zulip Mathias Fleury (Nov 15 2026 at 16:32):

primrec internally defines the constant Xset using the recursor (Xset_def) and proves the simplification lemmas Xset.simps.

view this post on Zulip Mathias Fleury (Nov 15 2026 at 16:33):

In proofs, primrec does not work and you have to do the same construct yourself, but it is easy.

view this post on Zulip Mathias Fleury (Nov 15 2026 at 16:33):

But you can see the definition produced by primrec (once you have fixed the variables you want the function to depend end). So you don't have to guess it.

view this post on Zulip Mathias Fleury (Nov 15 2026 at 16:36):

The final paragraph feels a bit like what I'd like, but I'm not sure how it fits into the bigger picture. Fortunately, I don't think I need that full power, and the "a lot of work to translate back" says that I should probably avoid this for now.

There are functions that you cannot express easily with primrec:

fun f :: ‹nat ⇒ nat› where
 ‹f 0 = 1› |
 ‹f n = Suc (f (n div 2))›

fun manages to define f using the least-fix-point, proves termination automatically, and derives the simplification lemmas.

It must be possible, but I don't know how to easily define such recursive pattern with the recursor.

view this post on Zulip Mathias Fleury (Nov 15 2026 at 16:40):

Here is the complete example:

lemma
  fixes X:: "'a set"
  fixes Y:: "'b set"
  fixes f:: "'a ⇒ 'b"
  fixes g:: "'b ⇒ 'a"
  assumes injf: "inj_on f X"
  assumes injg: "inj_on g Y"
  shows False
proof -
  define R where Rdef: "R = X - g`Y"
  define h where hdef: "h = g ∘ f"
  define Xset where ‹Xset = rec_nat (X - g`Y) (λ_ a. h ` a)›
  have [simp]: ‹Xset 0 = X - g`Y› and ‹Xset (Suc n)  = h`(Xset n)› for n
    unfolding Xset_def by auto

view this post on Zulip John Hughes (Nov 15 2026 at 16:44):

Aha....now I see what you meant in that earlier explanation. Thanks very much for taking part of your Sunday to explain this to me. I'll go on with the example code, since my particular case is a relatively simple one.


Last updated: Dec 02 2026 at 13:38 UTC