Skip to content

Commit 26ad099

Browse files
committed
Prefer k and j to n’ and m’
Related: #3
1 parent 068af22 commit 26ad099

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

docs/pdf/sf-idris-2016.pdf

-15 Bytes
Binary file not shown.

src/Basics.lidr

+29-29
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,16 @@ We can write simple functions that pattern match on natural numbers just as we
425425
did above -- for example, the predecessor function:
426426

427427
> pred : (n : Nat) -> Nat
428-
> pred Z = Z
429-
> pred (S n') = n'
428+
> pred Z = Z
429+
> pred (S k) = k
430430

431-
The second branch can be read: "if `n` has the form `S n'` for some `n'`, then
432-
return `n'`."
431+
The second branch can be read: "if `n` has the form `S k` for some `k`, then
432+
return `k`."
433433

434434
> minusTwo : (n : Nat) -> Nat
435-
> minusTwo Z = Z
436-
> minusTwo (S Z) = Z
437-
> minusTwo (S (S n')) = n'
435+
> minusTwo Z = Z
436+
> minusTwo (S Z) = Z
437+
> minusTwo (S (S k)) = k
438438

439439
Because natural numbers are such a pervasive form of data, Idris provides a tiny
440440
bit of built-in magic for parsing and printing them: ordinary arabic numerals
@@ -472,9 +472,9 @@ need to recursively check whether `n-2` is even.
472472
> ||| Determine whether a number is even.
473473
> ||| @n a number
474474
> evenb : (n : Nat) -> Bool
475-
> evenb Z = True
476-
> evenb (S Z) = False
477-
> evenb (S (S n')) = evenb n'
475+
> evenb Z = True
476+
> evenb (S Z) = False
477+
> evenb (S (S k)) = evenb k
478478

479479
We can define `oddb` by a similar recursive declaration, but here is a simpler
480480
definition that is a bit easier to work with:
@@ -493,8 +493,8 @@ Naturally we can also define multi-argument functions by recursion.
493493

494494
> namespace Playground2
495495
> plus : (n : Nat) -> (m : Nat) -> Nat
496-
> plus Z m = m
497-
> plus (S n') m = S (Playground2.plus n' m)
496+
> plus Z m = m
497+
> plus (S k) m = S (Playground2.plus k m)
498498

499499
Adding three to two now gives us five, as we'd expect.
500500

@@ -525,8 +525,8 @@ the same as if we had written `(n : Nat) -> (m : Nat)`.
525525

526526
```idris
527527
mult : (n, m : Nat) -> Nat
528-
mult Z = Z
529-
mult (S n') = plus m (mult n' m)
528+
mult Z = Z
529+
mult (S k) = plus m (mult k m)
530530
```
531531

532532
> testMult1 : (mult 3 3) = 9
@@ -535,10 +535,10 @@ mult (S n') = plus m (mult n' m)
535535
You can match two expression at once:
536536

537537
```idris
538-
minus (n, m : Nat) -> Nat
539-
minus Z _ = Z
540-
minus n Z = n
541-
minus (S n') (S m') = minus n' m'
538+
minus : (n, m : Nat) -> Nat
539+
minus Z _ = Z
540+
minus n Z = n
541+
minus (S k) (S j) = minus k j
542542
```
543543

544544
\color{red}
@@ -605,19 +605,19 @@ The `beq_nat` function tests `Nat`ural numbers for `eq`uality, yielding a
605605

606606
> ||| Test natural numbers for equality.
607607
> beq_nat : (n, m : Nat) -> Bool
608-
> beq_nat Z Z = True
609-
> beq_nat Z (S m') = False
610-
> beq_nat (S n') Z = False
611-
> beq_nat (S n') (S m') = beq_nat n' m'
608+
> beq_nat Z Z = True
609+
> beq_nat Z (S j) = False
610+
> beq_nat (S k) Z = False
611+
> beq_nat (S k) (S j) = beq_nat k j
612612

613613
The `leb` function tests whether its first argument is less than or equal to its
614614
second argument, yielding a boolean.
615615

616616
> ||| Test whether a number is less than or equal to another.
617617
> leb : (n, m : Nat) -> Bool
618618
> leb Z m = True
619-
> leb (S n') Z = False
620-
> leb (S n') (S m') = leb n' m'
619+
> leb (S k) Z = False
620+
> leb (S k) (S j) = leb k j
621621

622622
> testLeb1 : leb 2 2 = True
623623
> testLeb1 = Refl
@@ -809,28 +809,28 @@ expression `n + 1`; neither can be simplified.
809809

810810
To make progress, we need to consider the possible forms of `n` separately. If
811811
`n` is `Z`, then we can calculate the final result of `beq_nat (n + 1) 0` and
812-
check that it is, indeed, `False`. And if `n = S n'` for some `n'`, then,
812+
check that it is, indeed, `False`. And if `n = S k` for some `k`, then,
813813
although we don't know exactly what number `n + 1` yields, we can calculate
814814
that, at least, it will begin with one `S`, and this is enough to calculate
815815
that, again, `beq_nat (n + 1) 0` will yield `False`.
816816

817817
To tell Idris to consider, separately, the cases where `n = Z` and
818-
where `n = S n'`, simply case split on `n`.
818+
where `n = S k`, simply case split on `n`.
819819

820820
\color{red}
821821
-- TODO: mention case splitting interactively in Emacs, Atom, etc.
822822
\color{black}
823823

824824
> plus_1_neq_0 : (n : Nat) -> beq_nat (n + 1) 0 = False
825-
> plus_1_neq_0 Z = Refl
826-
> plus_1_neq_0 (S n') = Refl
825+
> plus_1_neq_0 Z = Refl
826+
> plus_1_neq_0 (S k) = Refl
827827

828828
Case splitting on `n` generates _two_ holes, which we must then prove,
829829
separately, in order to get Idris to accept the theorem.
830830

831831
In this example, each of the holes is easily filled by a single use of `Refl`,
832832
which itself performs some simplification -- e.g., the first one simplifies
833-
`beq_nat (S n' + 1) 0` to `False` by first rewriting `(S n' + 1)` to `S (n' +
833+
`beq_nat (S k + 1) 0` to `False` by first rewriting `(S k + 1)` to `S (k +
834834
1)`, then unfolding `beq_nat`, simplifying its pattern matching.
835835

836836
There are no hard and fast rules for how proofs should be formatted in Idris.

src/Induction.lidr

+9-9
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,26 @@ number `n` and we want to show that `p` holds for _all_ numbers `n`, we can
6161
reason like this:
6262

6363
- show that `p Z` holds;
64-
- show that, for any `n'`, if `p in'` holds, then so does `p (S n')`;
64+
- show that, for any `k`, if `p k` holds, then so does `p (S k)`;
6565
- conclude that `p n` holds for all `n`.
6666

6767
In Idris, the steps are the same and can often be written as function clauses by
6868
case splitting. Here's how this works for the theorem at hand.
6969

7070
> plus_n_Z : (n : Nat) -> n = n + 0
71-
> plus_n_Z Z = Refl
72-
> plus_n_Z (S n') =
73-
> let inductiveHypothesis = plus_n_Z n' in
71+
> plus_n_Z Z = Refl
72+
> plus_n_Z (S k) =
73+
> let inductiveHypothesis = plus_n_Z k in
7474
> rewrite inductiveHypothesis in Refl
7575

7676
In the first clause, `n` is replaced by `Z` and the goal becomes `0 = 0`, which
77-
follows by `Refl`exivity. In the second, `n` is replaced by `S n'` and the goal
78-
becomes `S n' = S (plus n' 0)`. Then we define the inductive hypothesis, `n' =
79-
n' + 0`, which can be written as `plus_n_Z n'`, and the goal follows from it.
77+
follows by `Refl`exivity. In the second, `n` is replaced by `S k` and the goal
78+
becomes `S k = S (plus k 0)`. Then we define the inductive hypothesis, `k =
79+
k + 0`, which can be written as `plus_n_Z k`, and the goal follows from it.
8080

8181
> minus_diag : (n : Nat) -> minus n n = 0
82-
> minus_diag Z = Refl
83-
> minus_diag (S n') = minus_diag n'
82+
> minus_diag Z = Refl
83+
> minus_diag (S k) = minus_diag k
8484

8585

8686
=== Exercise: 2 stars, recommended (basic_induction)

0 commit comments

Comments
 (0)