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).
17 lines
400 B
Text
17 lines
400 B
Text
|
|
-- Check that indent is required
|
|
|
|
def nullary1 (x : Nat) : Option Unit := nullary1 (x + 1)
|
|
partial_fixpoint monotonicity
|
|
fun _ _ a x => a (x + 1)
|
|
|
|
def nullary2 (x : Nat) : Option Unit := nullary2 (x + 1)
|
|
partial_fixpoint
|
|
monotonicity fun _ _ a x => a (x + 1)
|
|
|
|
-- This is allowed:
|
|
|
|
def nullary3 (x : Nat) : Option Unit := nullary3 (x + 1)
|
|
partial_fixpoint
|
|
monotonicity
|
|
fun _ _ a x => a (x + 1)
|