This PR adds the ability to define possibly non-terminating functions
and still be able to reason about them equationally, as long as they are
tail-recursive or monadic.
Typical uses of this feature are
```lean4
def ack : (n m : Nat) → Option Nat
| 0, y => some (y+1)
| x+1, 0 => ack x 1
| x+1, y+1 => do ack x (← ack (x+1) y)
partial_fixpiont
def whileSome (f : α → Option α) (x : α) : α :=
match f x with
| none => x
| some x' => whileSome f x'
partial_fixpiont
def computeLfp {α : Type u} [DecidableEq α] (f : α → α) (x : α) : α :=
let next := f x
if x ≠ next then
computeLfp f next
else
x
partial_fixpiont
noncomputable def geom : Distr Nat := do
let head ← coin
if head then
return 0
else
let n ← geom
return (n + 1)
partial_fixpiont
```
This PR contains
* The necessary fragment of domain theory, up to (a variant of)
Knaster–Tarski theorem (merged as
https://github.com/leanprover/lean4/pull/6477)
* A tactic to solve monotonicity goals compositionally (a bit like
mathlib’s `fun_prop`) (merged as
https://github.com/leanprover/lean4/pull/6506)
* An attribute to extend that tactic (merged as
https://github.com/leanprover/lean4/pull/6506)
* A “derecursifier” that uses that machinery to define recursive
function, including support for dependent functions and mutual
recursion.
* Fixed-point induction principles (technical, tedious to use)
* For `Option`-valued functions: Partial correctness induction theorems
that hide all the domain theory
This is heavily inspired by [Isabelle’s `partial_function`
command](https://isabelle.in.tum.de/doc/codegen.pdf).
37 lines
799 B
Text
37 lines
799 B
Text
-- Tests the `monotonicity` tactic
|
|
|
|
/-
|
|
Should test that the tactic syntax is scoped, but cannot use #guard_msgs to catch “tactic not known”
|
|
errors, it seems:
|
|
|
|
/--
|
|
error: unsolved goals
|
|
⊢ True
|
|
-/
|
|
#guard_msgs in
|
|
example : True := by monotonicity
|
|
|
|
-/
|
|
|
|
open Lean.Order
|
|
|
|
example : monotone (fun (f : Nat → Option Unit) => do {do f 1; f 2; f 3}) := by
|
|
repeat monotonicity
|
|
|
|
example : monotone (fun (f : Option Unit) => do {do f; f; f}) := by
|
|
repeat monotonicity
|
|
|
|
example : monotone
|
|
(fun (f : Nat → Option Unit) => do
|
|
for x in [1,2,3] do f x) := by
|
|
repeat' monotonicity
|
|
|
|
example : monotone
|
|
(fun (f : Nat → Option Nat) => do
|
|
let mut acc := 0
|
|
for x in [1,2,3] do
|
|
acc := acc + (← f x)
|
|
if acc > 10 then
|
|
return 5
|
|
pure acc) := by
|
|
repeat' monotonicity
|