For Lean 3, I'd start out with something like:
def fib: ℕ → ℕ
| 0 := 0
| 1 := 1
| (n + 2) := fib n + fib (n+1)
or for your second example:
def foo : ℕ → ℕ
| 0 := 1
| (n + 1) := (foo n) + (foo n)
(I modified your second example to be easier to express recursively.
You should be able to prove that foo 0 + ... + foo n = f (n+1)
, although I admit I don't know the lean notation off hand.)
If this becomes difficult to work with for your use case, then maybe ask another question with the example you are working on as there are other ways to define this which might have better properties for your particular use case.
In particular, we are treating the sequence 0, 1, 1, 2, 3,... as a function fib : ℕ → ℕ
. This is a standard way to work with sequences, and is easy to prove things about.
If you also need your sequence to depend on a parameter, like maybe we are starting fib
with two numbers a
and b
, then the notation is maybe a bit confusing at first:
def fib_general (a : ℕ) (b : ℕ) : ℕ → ℕ
| 0 := a
| 1 := b
| (n + 2) := fib_general n + fib_general (n+1)
In particular notice that we don't pass a
and b
to the recursive call since they are in a sense fixed.
Here is a resource for more information: https://leanprover.github.io/logic_and_proof/the_natural_numbers_and_induction_in_lean.html
f
or just do math with it? That probably makes a difference. fib is defined with a stream in mathlib for performance. I'm not sure if that is recommended though. $\endgroup$