Skip to content

Commit a9665aa

Browse files
author
Alex Gryzlov
committed
remove negb, use && and ||
1 parent ce2dbbc commit a9665aa

File tree

4 files changed

+54
-47
lines changed

4 files changed

+54
-47
lines changed

src/Basics.lidr

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,12 @@ standard library.
249249

250250
Functions over booleans can be defined in the same way as above:
251251

252-
> negb : (b : Bool) -> Bool
253-
> negb True = False
254-
> negb False = True
255-
>
252+
```idris
253+
not : (b : Bool) -> Bool
254+
not True = False
255+
not False = True
256+
```
257+
256258
> andb : (b1 : Bool) -> (b2 : Bool) -> Bool
257259
> andb True b2 = b2
258260
> andb False b2 = False
@@ -286,15 +288,17 @@ We can also introduce some familiar syntax for the boolean operations we have
286288
just defined. The \idr{syntax} command defines a new symbolic notation for an
287289
existing definition, and \idr{infixl} specifies left-associative fixity.
288290

289-
> infixl 4 /\, \/
290-
>
291-
> (/\) : Bool -> Bool -> Bool
292-
> (/\) = andb
293-
>
294-
> (\/) : Bool -> Bool -> Bool
295-
> (\/) = orb
296-
>
297-
> testOrb5 : False \/ False \/ True = True
291+
```idris
292+
infixl 4 &&, ||
293+
294+
(&&) : Bool -> Bool -> Bool
295+
(&&) = andb
296+
297+
(||) : Bool -> Bool -> Bool
298+
(||) = orb
299+
```
300+
301+
> testOrb5 : False || False || True = True
298302
> testOrb5 = Refl
299303
>
300304

@@ -355,27 +359,27 @@ Every expression in Idris has a type, describing what sort of thing it computes.
355359
The \idr{:type} (or \idr{:t}) REPL command asks Idris to print the type of an
356360
expression.
357361

358-
For example, the type of \idr{negb True} is \idr{Bool}.
362+
For example, the type of \idr{not True} is \idr{Bool}.
359363

360364
```idris
361365
λΠ> :type True
362366
True : Bool
363-
λΠ> :t negb True
364-
negb True : Bool
367+
λΠ> :t not True
368+
not True : Bool
365369
```
366370

367371
\todo[inline]{Confirm the "function types" wording.}
368372

369-
Functions like \idr{negb} itself are also data values, just like \idr{True} and
373+
Functions like \idr{not} itself are also data values, just like \idr{True} and
370374
\idr{False}. Their types are called \glspl{function type}, and they are written
371375
with arrows.
372376

373377
```idris
374-
λΠ> :t negb
375-
negb : Bool -> Bool
378+
λΠ> :t not
379+
not : Bool -> Bool
376380
```
377381

378-
The type of \idr{negb}, written \idr{Bool -> Bool} and pronounced "\idr{Bool}
382+
The type of \idr{not}, written \idr{Bool -> Bool} and pronounced "\idr{Bool}
379383
arrow \idr{Bool}," can be read, "Given an input of type \idr{Bool}, this
380384
function produces an output of type \idr{Bool}." Similarly, the type of
381385
\idr{andb}, written \idr{Bool -> Bool -> Bool}, can be read, "Given two inputs,
@@ -499,7 +503,7 @@ simpler definition that is a bit easier to work with:
499503
> ||| Determine whether a number is odd.
500504
> ||| @n a number
501505
> oddb : (n : Nat) -> Bool
502-
> oddb n = negb (evenb n)
506+
> oddb n = not (evenb n)
503507
>
504508
> testOddb1 : oddb 1 = True
505509
> testOddb1 = Refl
@@ -886,9 +890,9 @@ For example, we use it next to prove that boolean negation is involutive --
886890
i.e., that negation is its own inverse.
887891

888892
> ||| A proof that boolean negation is involutive.
889-
> negb_involutive : (b : Bool) -> negb (negb b) = b
890-
> negb_involutive True = Refl
891-
> negb_involutive False = Refl
893+
> not_involutive : (b : Bool) -> not (not b) = b
894+
> not_involutive True = Refl
895+
> not_involutive False = Refl
892896
>
893897

894898
Note that the case splitting here doesn't introduce any variables because none
@@ -898,7 +902,7 @@ any names.
898902
It is sometimes useful to case split on more than one parameter, generating yet
899903
more proof obligations. For example:
900904

901-
> andb_commutative : (b, c : Bool) -> andb b c = andb c b
905+
> andb_commutative : (b, c : Bool) -> b && c = c && b
902906
> andb_commutative True True = Refl
903907
> andb_commutative True False = Refl
904908
> andb_commutative False True = Refl
@@ -908,15 +912,15 @@ more proof obligations. For example:
908912
In more complex proofs, it is often better to lift subgoals to lemmas:
909913

910914

911-
> andb_commutative'_rhs_1 : (c : Bool) -> c = andb c True
915+
> andb_commutative'_rhs_1 : (c : Bool) -> c = c && True
912916
> andb_commutative'_rhs_1 True = Refl
913917
> andb_commutative'_rhs_1 False = Refl
914918
>
915-
> andb_commutative'_rhs_2 : (c : Bool) -> False = andb c False
919+
> andb_commutative'_rhs_2 : (c : Bool) -> False = c && False
916920
> andb_commutative'_rhs_2 True = Refl
917921
> andb_commutative'_rhs_2 False = Refl
918922
>
919-
> andb_commutative' : (b, c : Bool) -> andb b c = andb c b
923+
> andb_commutative' : (b, c : Bool) -> b && c = c && b
920924
> andb_commutative' True = andb_commutative'_rhs_1
921925
> andb_commutative' False = andb_commutative'_rhs_2
922926
>
@@ -926,7 +930,7 @@ In more complex proofs, it is often better to lift subgoals to lemmas:
926930

927931
Prove the following claim, lift cases (and subcases) to lemmas when case split.
928932

929-
> andb_true_elim_2 : (b, c : Bool) -> (andb b c = True) -> c = True
933+
> andb_true_elim_2 : (b, c : Bool) -> (b && c = True) -> c = True
930934
> andb_true_elim_2 b c prf = ?andb_true_elim_2_rhs
931935
>
932936

@@ -988,7 +992,7 @@ boolean functions.
988992

989993
Now state and prove a theorem \idr{negation_fn_applied_twice} similar to the
990994
previous one but where the second hypothesis says that the function \idr{f} has
991-
the property that \idr{f x = negb x}.
995+
the property that \idr{f x = not x}.
992996

993997
>
994998
> -- FILL IN HERE
@@ -1003,7 +1007,7 @@ Prove the following theorem. (You may want to first prove a subsidiary lemma or
10031007
two. Alternatively, remember that you do not have to introduce all hypotheses at
10041008
the same time.)
10051009

1006-
> andb_eq_orb : (b, c : Bool) -> (andb b c = orb b c) -> b = c
1010+
> andb_eq_orb : (b, c : Bool) -> (b && c = b || c) -> b = c
10071011
> andb_eq_orb b c prf = ?andb_eq_orb_rhs
10081012

10091013
$\square$

src/Induction.lidr

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ n} harder when done by induction on \idr{n}, since we may need an induction
129129
hypothesis about \idr{n - 2}. The following lemma gives a better
130130
characterization of \idr{evenb (S n)}:
131131

132-
> evenb_S : (n : Nat) -> evenb (S n) = negb (evenb n)
132+
> evenb_S : (n : Nat) -> evenb (S n) = not (evenb n)
133133
> evenb_S n = ?evenb_S_rhs
134134

135135
$\square$
@@ -236,7 +236,7 @@ your piece of paper; this is just to encourage you to reflect before you hack!)
236236
> zero_nbeq_S : (n : Nat) -> beq_nat 0 (S n) = False
237237
> zero_nbeq_S n = ?zero_nbeq_S_rhs
238238

239-
> andb_false_r : (b : Bool) -> andb b False = False
239+
> andb_false_r : (b : Bool) -> b && False = False
240240
> andb_false_r b = ?andb_false_r_rhs
241241

242242
> plus_ble_compat_l : (n, m, p : Nat) ->
@@ -250,10 +250,7 @@ your piece of paper; this is just to encourage you to reflect before you hack!)
250250
> mult_1_l n = ?mult_1_l_rhs
251251

252252
> all3_spec : (b, c : Bool) ->
253-
> orb (andb b c)
254-
> (orb (negb b)
255-
> (negb c))
256-
> = True
253+
> (b && c) || ((not b) || (not c)) = True
257254
> all3_spec b c = ?all3_spec_rhs
258255

259256
> mult_plus_distr_r : (n, m, p : Nat) -> (n + m) * p = (n * p) + (m * p)

src/Poly.lidr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,14 @@ body of \idr{doit3times} applies \idr{f} three times to some value \idr{n}.
546546
-- doit3times : (x -> x) -> x -> x
547547
```
548548

549+
\todo[inline]{Explain that the prefixes are needed to avoid the implicit scoping
550+
rule, seems that this fires up more often when passing functions as parameters
551+
to other functions}
552+
549553
> test_doit3times : doit3times Numbers.minusTwo 9 = 3
550554
> test_doit3times = Refl
551555

552-
> test_doit3times' : doit3times Booleans.negb True = False
556+
> test_doit3times' : doit3times Bool.not True = False
553557
> test_doit3times' = Refl
554558

555559

@@ -775,12 +779,16 @@ yields
775779

776780
Some more examples:
777781

782+
\todo[inline]{We go back to \idr{andb} here because \idr{(&&)}'s second
783+
parameter is lazy, with the left fold the return type is inferred to be lazy
784+
too, leading to type mismatch.}
785+
778786
```idris
779787
λΠ> :t fold andb
780788
fold andb : List Bool -> Bool -> Bool
781789
```
782790

783-
> fold_example1 : fold Nat.mult [1,2,3,4] 1 = 24
791+
> fold_example1 : fold (*) [1,2,3,4] 1 = 24
784792
> fold_example1 = Refl
785793

786794
> fold_example2 : fold Booleans.andb [True,True,False,True] True = False
@@ -849,11 +857,9 @@ if we like we can supply just the first. This is called _partial application_.
849857
> test_plus3 : plus3 4 = 7
850858
> test_plus3 = Refl
851859

852-
\todo[inline]{This is apparently a bug in Idris,
853-
https://github.com/idris-lang/Idris-dev/issues/3908}
854860

855-
> -- test_plus3' : doit3times plus3 0 = 9
856-
> -- test_plus3' = Refl
861+
> test_plus3' : doit3times Poly.plus3 0 = 9
862+
> test_plus3' = Refl
857863

858864
> test_plus3'' : doit3times (plus 3) 0 = 9
859865
> test_plus3'' = Refl

src/Tactics.lidr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
= Tactics: More Basic Tactics
1+
= Tactics : More Basic Tactics
22

33
This chapter introduces several additional proof strategies and tactics that
44
allow us to begin proving more interesting properties of functional programs. We
@@ -1193,7 +1193,7 @@ checks whether every element in a list satisfies a given predicate:
11931193
11941194
\idr{forallb oddb [1,3,5,7,9] = True}
11951195
1196-
\idr{forallb negb [False,False] = True}
1196+
\idr{forallb not [False,False] = True}
11971197
11981198
\idr{forallb evenb [0,2,4,5] = False}
11991199
@@ -1204,14 +1204,14 @@ given predicate:
12041204
12051205
\idr{existsb (beq_nat 5) [0,2,3,6] = False}
12061206
1207-
\idr{existsb (andb True) [True,True,False] = True}
1207+
\idr{existsb ((&&) True) [True,True,False] = True}
12081208
12091209
\idr{existsb oddb [1,0,0,0,0,3] = True}
12101210
12111211
\idr{existsb evenb [] = False}
12121212
12131213
Next, define a _nonrecursive_ version of \idr{existsb} -- call it \idr{existsb'}
1214-
-- using \idr{forallb} and \idr{negb}.
1214+
-- using \idr{forallb} and \idr{not}.
12151215
12161216
Finally, prove a theorem \idr{existsb_existsb'} stating that \idr{existsb'} and
12171217
\idr{existsb} have the same behavior.

0 commit comments

Comments
 (0)