Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 2acb474

Browse files
avigadsoonhokong
authored andcommitted
feat(07_Induction_and_Recursion.org): explain handling of parameters in function definition package
1 parent c9e7f91 commit 2acb474

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

07_Induction_and_Recursion.org

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

152152
The function definition package supports structural recursion:
153153
recursive applications where one of the arguments is a subterm of the
@@ -194,9 +194,43 @@ end hide
194194
The "definition" of =zero_add= makes it clear that proof by induction
195195
is 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
201235
import data.nat
202236
open nat

08_Building_Theories_and_Proofs.org

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ are simultaneously satisifed. An assignment of terms to metavariables
315315
is known as a /substitution/, and the general task of finding a
316316
substitution that makes two expressions coincide is known as a
317317
/unification/ problem. (If only one of the expressions contains
318-
metavariables, it is a special case known as a /matching/ problem.)
318+
metavariables, the task is a special case known as a /matching/
319+
problem.)
319320

320321
Some constraints are straightforwardly handled. If =f= and =g= are
321322
distinct constants, it is clear that there is no way to unify the

0 commit comments

Comments
 (0)