This PR extends the notion of “fixed parameter” of a recursive function
also to parameters that come after varying function. The main benefit is
that we get nicer induction principles.
Before the definition
```lean
def app (as : List α) (bs : List α) : List α :=
match as with
| [] => bs
| a::as => a :: app as bs
```
produced
```lean
app.induct.{u_1} {α : Type u_1} (motive : List α → List α → Prop) (case1 : ∀ (bs : List α), motive [] bs)
(case2 : ∀ (bs : List α) (a : α) (as : List α), motive as bs → motive (a :: as) bs) (as bs : List α) : motive as bs
```
and now you get
```lean
app.induct.{u_1} {α : Type u_1} (motive : List α → Prop) (case1 : motive [])
(case2 : ∀ (a : α) (as : List α), motive as → motive (a :: as)) (as : List α) : motive as
```
because `bs` is fixed throughout the recursion (and can completely be
dropped from the principle).
This is a breaking change when such an induction principle is used
explicitly. Using `fun_induction` makes proof tactics robust against
this change.
The rules for when a parameter is fixed are now:
1. A parameter is fixed if it is reducibly defq to the the corresponding
argument in each recursive call, so we have to look at each such call.
2. With mutual recursion, it is not clear a-priori which arguments of
another function correspond to the parameter. This requires an analysis
with some graph algorithms to determine.
3. A parameter can only be fixed if all parameters occurring in its type
are fixed as well.
This dependency graph on parameters can be different for the different
functions in a recursive group, even leading to cycles.
4. For structural recursion, we kinda want to know the fixed parameters
before investigating which argument to actually recurs on. But once we
have that we may find that we fixed an index of the recursive
parameter’s type, and these cannot be fixed. So we have to un-fix them
5. … and all other fixed parameters that have dependencies on them.
Lean tries to identify the largest set of parameters that satisfies
these criteria.
Note that in a definition like
```lean
def app : List α → List α → List α
| [], bs => bs
| a::as, bs => a :: app as bs
```
the `bs` is not considered fixes, as it goes through the matcher
machinery.
Fixes #7027
Fixes #2113
101 lines
2.3 KiB
Text
101 lines
2.3 KiB
Text
private axiom test_sorry : ∀ {α}, α
|
||
|
||
set_option trace.Elab.definition.fixedParams true
|
||
|
||
namespace Ex1
|
||
|
||
/--
|
||
error: well-founded recursion cannot be used, 'Ex1.foo' does not take any (non-fixed) arguments
|
||
---
|
||
info: [Elab.definition.fixedParams] getFixedParams:
|
||
• ⏎
|
||
•
|
||
-/
|
||
#guard_msgs in
|
||
mutual
|
||
def foo : Nat := bar
|
||
def bar : Nat := foo
|
||
decreasing_by exact test_sorry
|
||
end
|
||
|
||
end Ex1
|
||
|
||
|
||
namespace Ex2
|
||
/--
|
||
error: well-founded recursion cannot be used, 'Ex2.foo' does not take any (non-fixed) arguments
|
||
---
|
||
info: [Elab.definition.fixedParams] getFixedParams:
|
||
• [#1 #1]
|
||
• [#1 #1]
|
||
-/
|
||
#guard_msgs in
|
||
mutual
|
||
def foo (n : Nat) : Nat := bar n
|
||
def bar (n : Nat) : Nat := foo n
|
||
decreasing_by exact test_sorry
|
||
end
|
||
end Ex2
|
||
|
||
namespace Ex3
|
||
/--
|
||
error: well-founded recursion cannot be used, 'Ex3.foo' does not take any (non-fixed) arguments
|
||
---
|
||
info: [Elab.definition.fixedParams] getFixedParams:
|
||
• [#1 #2] [#2 #1]
|
||
• [#2 #1] [#1 #2]
|
||
-/
|
||
#guard_msgs in
|
||
mutual
|
||
def foo (n : Nat) (m : Nat) : Nat := bar m n
|
||
decreasing_by all_goals exact test_sorry
|
||
def bar (n : Nat) (m : Nat) : Nat := foo m n
|
||
decreasing_by all_goals exact test_sorry
|
||
end
|
||
end Ex3
|
||
|
||
namespace Ex4
|
||
/--
|
||
info: [Elab.definition.fixedParams] getFixedParams: notFixed 0 3:
|
||
In foo c n b m
|
||
m not matched
|
||
[Elab.definition.fixedParams] getFixedParams: • [#1 #3] ❌ [#3 #1] ❌ • [#3 #1] ❌ [#1 #3] ❌
|
||
-/
|
||
#guard_msgs in
|
||
mutual
|
||
def foo (a : Nat) (n : Nat) (d : Nat) (m : Nat) : Nat := bar d m a n
|
||
decreasing_by exact test_sorry
|
||
def bar (b : Nat) (n : Nat) (c : Nat) (m : Nat) : Nat := foo c n b m
|
||
decreasing_by exact test_sorry
|
||
end
|
||
end Ex4
|
||
|
||
namespace Append1
|
||
|
||
/--
|
||
info: [Elab.definition.fixedParams] getFixedParams: notFixed 0 1:
|
||
In app as bs
|
||
x✝¹ =/= as
|
||
[Elab.definition.fixedParams] getFixedParams: notFixed 0 2:
|
||
In app as bs
|
||
x✝ =/= bs
|
||
[Elab.definition.fixedParams] getFixedParams: • [#1] ❌ ❌
|
||
-/
|
||
#guard_msgs(info) in
|
||
def app : List α → List α → List α
|
||
| [], bs => bs
|
||
| a::as, bs => a :: app as bs
|
||
|
||
/--
|
||
info: [Elab.definition.fixedParams] getFixedParams: notFixed 0 1:
|
||
In app' as bs
|
||
as✝ =/= as
|
||
[Elab.definition.fixedParams] getFixedParams: • [#1] ❌ [#3]
|
||
-/
|
||
#guard_msgs(info) in
|
||
def app' (as : List α) (bs : List α) : List α :=
|
||
match as with
|
||
| [] => bs
|
||
| a::as => a :: app' as bs
|
||
|
||
end Append1
|