@@ -425,16 +425,16 @@ We can write simple functions that pattern match on natural numbers just as we
425
425
did above -- for example , the predecessor function:
426
426
427
427
> pred : (n : Nat) -> Nat
428
- > pred Z = Z
429
- > pred (S n' ) = n'
428
+ > pred Z = Z
429
+ > pred (S k ) = k
430
430
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 `."
433
433
434
434
> 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
438
438
439
439
Because natural numbers are such a pervasive form of data , Idris provides a tiny
440
440
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.
472
472
> ||| Determine whether a number is even .
473
473
> ||| @n a number
474
474
> 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
478
478
479
479
We can define `oddb` by a similar recursive declaration, but here is a simpler
480
480
definition that is a bit easier to work with:
@@ -493,8 +493,8 @@ Naturally we can also define multi-argument functions by recursion.
493
493
494
494
> namespace Playground2
495
495
> 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)
498
498
499
499
Adding three to two now gives us five , as we'd expect.
500
500
@@ -525,8 +525,8 @@ the same as if we had written `(n : Nat) -> (m : Nat)`.
525
525
526
526
```idris
527
527
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)
530
530
```
531
531
532
532
> testMult1 : (mult 3 3) = 9
@@ -535,10 +535,10 @@ mult (S n') = plus m (mult n' m)
535
535
You can match two expression at once:
536
536
537
537
```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
542
542
```
543
543
544
544
\color{red}
@@ -605,19 +605,19 @@ The `beq_nat` function tests `Nat`ural numbers for `eq`uality, yielding a
605
605
606
606
> ||| Test natural numbers for equality.
607
607
> 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
612
612
613
613
The `leb` function tests whether its first argument is less than or equal to its
614
614
second argument , yielding a boolean.
615
615
616
616
> ||| Test whether a number is less than or equal to another.
617
617
> leb : (n, m : Nat) -> Bool
618
618
> 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
621
621
622
622
> testLeb1 : leb 2 2 = True
623
623
> testLeb1 = Refl
@@ -809,28 +809,28 @@ expression `n + 1`; neither can be simplified.
809
809
810
810
To make progress, we need to consider the possible forms of `n` separately. If
811
811
`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,
813
813
although we don't know exactly what number `n + 1` yields, we can calculate
814
814
that, at least , it will begin with one `S`, and this is enough to calculate
815
815
that, again, `beq_nat (n + 1) 0` will yield `False`.
816
816
817
817
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`.
819
819
820
820
\color{red}
821
821
-- TODO: mention case splitting interactively in Emacs , Atom, etc.
822
822
\color{black}
823
823
824
824
> 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
827
827
828
828
Case splitting on `n` generates _two_ holes, which we must then prove,
829
829
separately, in order to get Idris to accept the theorem.
830
830
831
831
In this example, each of the holes is easily filled by a single use of `Refl`,
832
832
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 +
834
834
1)`, then unfolding `beq_nat`, simplifying its pattern matching .
835
835
836
836
There are no hard and fast rules for how proofs should be formatted in Idris.
0 commit comments