@@ -147,7 +147,7 @@ definition tail4 : Π (A : Type), list A → list A
147147#+END_SRC
148148
149149
150- ** Structural Recursion/ Induction
150+ ** Structural Recursion and Induction
151151
152152The function definition package supports structural recursion:
153153recursive applications where one of the arguments is a subterm of the
@@ -194,9 +194,43 @@ end hide
194194The "definition" of =zero_add= makes it clear that proof by induction
195195is really a form of induction in Lean.
196196
197- A more example is the Fibonacci function =fib=, together with a
198- theorem, =fib_pos=, which combines pattern matching, recursive
199- equations, and calculational proofs.
197+ As with definition by pattern matching, parameters to a structural
198+ recursion or induction may appear before the colon. Such parameters
199+ are simply added to the local context before the definition is
200+ processed. For example, the definition of addition may be written as
201+ follows:
202+ #+BEGIN_SRC lean
203+ namespace hide
204+
205+ inductive nat : Type :=
206+ | zero : nat
207+ | succ : nat → nat
208+
209+ namespace nat
210+
211+ notation 0 := zero
212+
213+ -- BEGIN
214+ definition add (m : nat) : nat → nat
215+ | add 0 := m
216+ | add (succ n) := succ (add n)
217+ -- END
218+
219+ end nat
220+ end hide
221+ #+END_SRC
222+ This may seem a little odd, but you should read the definition as
223+ follows: "Fix =m=, and define the function which adds something to =m=
224+ recursively, as follows. To add zero, return =m=. To add the successor
225+ of =n=, first add =n=, and then take the successor." The mechanism
226+ for adding parameters to the local context is what makes it possible
227+ to process match expressions within terms, as described below.
228+
229+ A more interesting example of strutural recursion is given by the
230+ Fibonacci function =fib=. The subsequent theorem, =fib_pos=, which
231+ combines pattern matching, recursive equations, and calculational
232+ proof.
233+
200234#+BEGIN_SRC lean
201235import data.nat
202236open nat
0 commit comments