Skip to content

Commit 8495331

Browse files
author
Alex Gryzlov
committed
beq_nat -> (==), fix some todos
1 parent a9665aa commit 8495331

File tree

7 files changed

+146
-151
lines changed

7 files changed

+146
-151
lines changed

src/Basics.lidr

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -627,16 +627,19 @@ simply instructions to the Idris parser to accept \idr{x + y} in place of
627627
\idr{plus x y} and, conversely, to the Idris pretty-printer to display \idr{plus
628628
x y} as \idr{x + y}.
629629

630-
The \idr{beq_nat} function tests \idr{Nat}ural numbers for \idr{eq}uality,
631-
yielding a \idr{b}oolean.
630+
\todo[inline]{Mention interfaces here? Say this is infix}
632631

633-
> ||| Test natural numbers for equality.
634-
> beq_nat : (n, m : Nat) -> Bool
635-
> beq_nat Z Z = True
636-
> beq_nat Z (S j) = False
637-
> beq_nat (S k) Z = False
638-
> beq_nat (S k) (S j) = beq_nat k j
639-
>
632+
The \idr{(==)} function tests \idr{Nat}ural numbers for equality, yielding a
633+
\idr{Bool}ean.
634+
635+
```idris
636+
||| Test natural numbers for equality.
637+
(==) : (n, m : Nat) -> Bool
638+
(==) Z Z = True
639+
(==) Z (S j) = False
640+
(==) (S k) Z = False
641+
(==) (S k) (S j) = (==) k j
642+
```
640643

641644
The \idr{lte} function tests whether its first argument is less than or equal to
642645
its second argument, yielding a boolean.
@@ -839,28 +842,28 @@ can block simplification. For example, if we try to prove the following fact
839842
using the \idr{Refl} tactic as above, we get stuck.
840843

841844
```idris
842-
plus_1_neq_0_firsttry : (n : Nat) -> beq_nat (n + 1) 0 = False
845+
plus_1_neq_0_firsttry : (n : Nat) -> (n + 1) == 0 = False
843846
plus_1_neq_0_firsttry n = Refl -- does nothing!
844847
```
845848

846-
The reason for this is that the definitions of both \idr{beq_nat} and \idr{+}
849+
The reason for this is that the definitions of both \idr{(==)} and \idr{+}
847850
begin by performing a \idr{match} on their first argument. But here, the first
848851
argument to \idr{+} is the unknown number \idr{n} and the argument to
849-
\idr{beq_nat} is the compound expression \idr{n + 1}; neither can be simplified.
852+
\idr{(==)} is the compound expression \idr{n + 1}; neither can be simplified.
850853

851854
To make progress, we need to consider the possible forms of \idr{n} separately.
852-
If \idr{n} is \idr{Z}, then we can calculate the final result of \idr{beq_nat (n
853-
+ 1) 0} and check that it is, indeed, \idr{False}. And if \idr{n = S k} for some
855+
If \idr{n} is \idr{Z}, then we can calculate the final result of \idr{(n + 1) ==
856+
0} and check that it is, indeed, \idr{False}. And if \idr{n = S k} for some
854857
\idr{k}, then, although we don't know exactly what number \idr{n + 1} yields, we
855858
can calculate that, at least, it will begin with one \idr{S}, and this is enough
856-
to calculate that, again, \idr{beq_nat (n + 1) 0} will yield \idr{False}.
859+
to calculate that, again, \idr{(n + 1) == 0} will yield \idr{False}.
857860

858861
To tell Idris to consider, separately, the cases where \idr{n = Z} and where
859862
\idr{n = S k}, simply case split on \idr{n}.
860863

861864
\todo[inline]{Mention case splitting interactively in Emacs, Atom, etc.}
862865

863-
> plus_1_neq_0 : (n : Nat) -> beq_nat (n + 1) 0 = False
866+
> plus_1_neq_0 : (n : Nat) -> (n + 1) == 0 = False
864867
> plus_1_neq_0 Z = Refl
865868
> plus_1_neq_0 (S k) = Refl
866869
>
@@ -870,8 +873,8 @@ separately, in order to get Idris to accept the theorem.
870873

871874
In this example, each of the holes is easily filled by a single use of
872875
\idr{Refl}, which itself performs some simplification -- e.g., the first one
873-
simplifies \idr{beq_nat (S k + 1) 0} to \idr{False} by first rewriting \idr{(S k
874-
+ 1)} to \idr{S (k + 1)}, then unfolding \idr{beq_nat}, simplifying its pattern
876+
simplifies \idr{(S k + 1) == 0} to \idr{False} by first rewriting \idr{(S k +
877+
1)} to \idr{S (k + 1)}, then unfolding \idr{(==)}, simplifying its pattern
875878
matching.
876879

877880
There are no hard and fast rules for how proofs should be formatted in Idris.
@@ -939,7 +942,7 @@ $\square$
939942

940943
==== Exercise: 1 star (zero_nbeq_plus_1)
941944

942-
> zero_nbeq_plus_1 : (n : Nat) -> beq_nat 0 (n + 1) = False
945+
> zero_nbeq_plus_1 : (n : Nat) -> 0 == (n + 1) = False
943946
> zero_nbeq_plus_1 n = ?zero_nbeq_plus_1_rhs
944947
>
945948

src/IndProp.lidr

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,42 +1245,42 @@ the following theorem:
12451245

12461246
\todo[inline]{Copy here for now}
12471247

1248-
> beq_nat_true : beq_nat n m = True -> n = m
1248+
> beq_nat_true : {n, m : Nat} -> n == m = True -> n = m
12491249
> beq_nat_true {n=Z} {m=Z} _ = Refl
12501250
> beq_nat_true {n=(S _)} {m=Z} Refl impossible
12511251
> beq_nat_true {n=Z} {m=(S _)} Refl impossible
12521252
> beq_nat_true {n=(S n')} {m=(S m')} eq =
12531253
> rewrite beq_nat_true {n=n'} {m=m'} eq in Refl
12541254
>
1255-
> filter_not_empty_In : Not (filter (beq_nat n) l = []) -> In n l
1255+
> filter_not_empty_In : {n : Nat} -> Not (filter ((==) n) l = []) -> In n l
12561256
> filter_not_empty_In {l=[]} contra = contra Refl
1257-
> filter_not_empty_In {l=(x::_)} {n} contra with (beq_nat n x) proof h
1257+
> filter_not_empty_In {l=(x::_)} {n} contra with (n == x) proof h
12581258
> filter_not_empty_In contra | True =
12591259
> Left $ sym $ beq_nat_true $ sym h
12601260
> filter_not_empty_In contra | False =
12611261
> Right $ filter_not_empty_In contra
12621262
>
12631263

12641264
In the second case we explicitly apply the \idr{beq_nat_true} lemma to the
1265-
equation generated by doing a dependent match on \idr{beq_nat n x}, to convert
1266-
the assumption \idr{beq_nat n x = True} into the assumption \idr{n = m}.
1265+
equation generated by doing a dependent match on \idr{n == x}, to convert the
1266+
assumption \idr{n == x = True} into the assumption \idr{n = m}.
12671267

12681268
We can streamline this by defining an inductive proposition that yields a better
1269-
case-analysis principle for \idr{beq_nat n m}. Instead of generating an equation
1270-
such as \idr{beq_nat n m = True}, which is generally not directly useful, this
1271-
principle gives us right away the assumption we really need: \idr{n = m}.
1269+
case-analysis principle for \idr{n == m}. Instead of generating an equation such
1270+
as \idr{n == m = True}, which is generally not directly useful, this principle
1271+
gives us right away the assumption we really need: \idr{n = m}.
12721272

12731273
We'll actually define something a bit more general, which can be used with
12741274
arbitrary properties (and not just equalities):
12751275

1276+
\todo[inline]{Update the text: seems that additional \idr{(b=...)} constructor
1277+
parameter is needed for this to work in Idris.}
1278+
12761279
```idris
12771280
data Reflect : Type -> Bool -> Type where
1278-
ReflectT : (p : Type) -> Reflect p True
1279-
ReflectF : (p : Type) -> (Not p) -> Reflect p False
1280-
```
1281-
1282-
\todo[inline]{Seems that additional `(b=True/False)` constructor parameter is
1283-
needed for this to work in Idris. Update the text.`}
1281+
ReflectT : (p : Type) -> (b=True) -> Reflect p b
1282+
ReflectF : (p : Type) -> (Not p) -> (b=False) -> Reflect p b
1283+
```
12841284

12851285
Before explaining this, let's rearrange it a little: Since the types of both
12861286
\idr{ReflectT} and \idr{ReflectF} begin with \idr{(p : Type)}, we can make the
@@ -1327,18 +1327,18 @@ the second).
13271327

13281328
\todo[inline]{Copy here for now}
13291329

1330-
> beq_nat_true_iff : (n1, n2 : Nat) -> (beq_nat n1 n2 = True) <-> (n1 = n2)
1330+
> beq_nat_true_iff : (n1, n2 : Nat) -> (n1 == n2 = True) <-> (n1 = n2)
13311331
> beq_nat_true_iff n1 n2 = (to, fro n1 n2)
13321332
> where
1333-
> to : (beq_nat n1 n2 = True) -> (n1 = n2)
1333+
> to : (n1 == n2 = True) -> (n1 = n2)
13341334
> to = beq_nat_true {n=n1} {m=n2}
1335-
> fro : (n1, n2 : Nat) -> (n1 = n2) -> (beq_nat n1 n2 = True)
1335+
> fro : (n1, n2 : Nat) -> (n1 = n2) -> (n1 == n2 = True)
13361336
> fro n1 n1 Refl = sym $ beq_nat_refl n1
13371337
>
13381338
> iff_sym : (p <-> q) -> (q <-> p)
13391339
> iff_sym (pq, qp) = (qp, pq)
13401340
>
1341-
> beq_natP : Reflect (n = m) (beq_nat n m)
1341+
> beq_natP : {n, m : Nat} -> Reflect (n = m) (n == m)
13421342
> beq_natP {n} {m} = iff_reflect $ iff_sym $ beq_nat_true_iff n m
13431343
>
13441344

@@ -1352,7 +1352,7 @@ calls to destruct and apply are combined into a single call to destruct.
13521352
Idris and observe the differences in proof state at the beginning of the first
13531353
case of the destruct.)
13541354

1355-
> filter_not_empty_In' : Not (filter (beq_nat n) l = []) -> In n l
1355+
> filter_not_empty_In' : {n : Nat} -> Not (filter ((==) n) l = []) -> In n l
13561356
> filter_not_empty_In' {l=[]} contra = contra Refl
13571357
> filter_not_empty_In' {n} {l=(x::xs)} contra with (beq_natP {n} {m=x})
13581358
> filter_not_empty_In' _ | (ReflectT eq _) = Left $ sym eq
@@ -1364,8 +1364,8 @@ case of the destruct.)
13641364
> contra' = replace notbeq contra
13651365
> {P = \a =>
13661366
> Not ((if a
1367-
> then x :: filter (beq_nat n) xs
1368-
> else filter (beq_nat n) xs) = [])}
1367+
> then x :: filter ((==) n) xs
1368+
> else filter ((==) n) xs) = [])}
13691369
> in
13701370
> Right $ filter_not_empty_In' contra'
13711371
>
@@ -1377,7 +1377,7 @@ Use \idr{beq_natP} as above to prove the following:
13771377

13781378
> count : (n : Nat) -> (l : List Nat) -> Nat
13791379
> count _ [] = 0
1380-
> count n (x :: xs) = (if beq_nat n x then 1 else 0) + count n xs
1380+
> count n (x :: xs) = (if n == x then 1 else 0) + count n xs
13811381
>
13821382
> beq_natP_practice : count n l = 0 -> Not (In n l)
13831383
> beq_natP_practice prf = ?beq_natP_practice_rhs

src/Induction.lidr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ your piece of paper; this is just to encourage you to reflect before you hack!)
233233
> lte_refl : (n : Nat) -> True = lte n n
234234
> lte_refl n = ?lte_refl_rhs
235235

236-
> zero_nbeq_S : (n : Nat) -> beq_nat 0 (S n) = False
236+
> zero_nbeq_S : (n : Nat) -> 0 == (S n) = False
237237
> zero_nbeq_S n = ?zero_nbeq_S_rhs
238238

239239
> andb_false_r : (b : Bool) -> b && False = False
@@ -243,7 +243,7 @@ your piece of paper; this is just to encourage you to reflect before you hack!)
243243
> lte n m = True -> lte (p + n) (p + m) = True
244244
> plus_ble_compat_l n m p prf = ?plus_ble_compat_l_rhs
245245

246-
> S_nbeq_0 : (n : Nat) -> beq_nat (S n) 0 = False
246+
> S_nbeq_0 : (n : Nat) -> (S n) == 0 = False
247247
> S_nbeq_0 n = ?S_nbeq_0_rhs
248248

249249
> mult_1_l : (n : Nat) -> 1 * n = n
@@ -272,7 +272,7 @@ standard library, so we follow suit. Rewriting works equally well in either
272272
direction, so we will have no problem using the theorem no matter which way we
273273
state it.)
274274

275-
> beq_nat_refl : (n : Nat) -> True = beq_nat n n
275+
> beq_nat_refl : (n : Nat) -> True = n == n
276276
> beq_nat_refl n = ?beq_nat_refl_rhs
277277

278278
$\square$

src/Lists.lidr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ some number to return when the list is too short...
820820
821821
> nth_bad : (l : NatList) -> (n : Nat) -> Nat
822822
> nth_bad Nil n = 42 -- arbitrary!
823-
> nth_bad (a :: l') n = case beq_nat n 0 of
823+
> nth_bad (a :: l') n = case n == 0 of
824824
> True => a
825825
> False => nth_bad l' (pred n)
826826
@@ -840,7 +840,7 @@ to indicate that it may result in an error.
840840
841841
> nth_error : (l : NatList) -> (n : Nat) -> NatOption
842842
> nth_error Nil n = None
843-
> nth_error (a :: l') n = case beq_nat n 0 of
843+
> nth_error (a :: l') n = case n == 0 of
844844
> True => Some a
845845
> False => nth_error l' (pred n)
846846
@@ -858,7 +858,7 @@ programming language: conditional expressions...
858858
859859
> nth_error' : (l : NatList) -> (n : Nat) -> NatOption
860860
> nth_error' Nil n = None
861-
> nth_error' (a :: l') n = if beq_nat n 0
861+
> nth_error' (a :: l') n = if n == 0
862862
> then Some a
863863
> else nth_error' l' (pred n)
864864
@@ -927,7 +927,7 @@ and gives us the flexibility to change representations later if we wish.
927927
We'll also need an equality test for \idr{Id}s:
928928
929929
> beq_id : (x1, x2 : Id) -> Bool
930-
> beq_id (MkId n1) (MkId n2) = beq_nat n1 n2
930+
> beq_id (MkId n1) (MkId n2) = n1 == n2
931931
932932
933933
==== Exercise: 1 star (beq_id_refl)

0 commit comments

Comments
 (0)