Skip to content

Commit c2ae0a7

Browse files
author
Alex Gryzlov
committed
fix unnecessary copies, some todos
1 parent 199758e commit c2ae0a7

File tree

7 files changed

+67
-156
lines changed

7 files changed

+67
-156
lines changed

src/Basics.lidr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,8 @@ arrow symbol is pronounced "implies."
769769
As before, we need to be able to reason by assuming the existence of some
770770
numbers \idr{n} and \idr{m}. We also need to assume the hypothesis \idr{n = m}.
771771

772-
\todo[inline]{Edit.}
772+
\todo[inline]{Edit, mention the "generate initial pattern match" editor command}
773+
773774
The \idr{intros} tactic will serve to move all three of these from the goal into
774775
assumptions in the current context.
775776

src/IndProp.lidr

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
= IndProp: Inductively Defined Propositions
1+
= IndProp : Inductively Defined Propositions
22

33
> module IndProp
44
>
55
> import Basics
66
> import Induction
7+
> import Tactics
8+
> import Logic
79
>
810
> %hide Basics.Numbers.pred
9-
> %hide Prelude.Stream.(::)
1011
>
12+
> %access public export
13+
> %default total
14+
1115

1216
== Inductively Defined Propositions
1317

@@ -106,8 +110,8 @@ When checking argument n to IndType.Wrong_ev:
106110
Nat (Expected type)
107111
```
108112

109-
\todo[inline]{Edit the explanation, it works fine if you remove the first \idr{n ->}
110-
in \idr{Wrong_ev_SS}}
113+
\todo[inline]{Edit the explanation, it works fine if you remove the first \idr{n
114+
->} in \idr{Wrong_ev_SS}}
111115

112116
("Parameter" here is Idris jargon for an argument on the left of the colon in an
113117
Inductive definition; "index" is used to refer to arguments on the right of the
@@ -338,13 +342,6 @@ induction hypothesis talks about n', as opposed to n or some other number.
338342
The equivalence between the second and third definitions of evenness now
339343
follows.
340344

341-
\todo[inline]{Copypasted `<->` for now}
342-
343-
> iff : {p,q : Type} -> Type
344-
> iff {p} {q} = (p -> q, q -> p)
345-
>
346-
> syntax [p] "<->" [q] = iff {p} {q}
347-
>
348345
> ev_even_iff : (Ev n) <-> (k ** n = double k)
349346
> ev_even_iff = (ev_even, fro)
350347
> where
@@ -894,16 +891,13 @@ chapter: If \idr{ss : List (List t)} represents a sequence of strings \idr{s1,
894891
..., sn}, then \idr{fold (++) ss []} is the result of concatenating them all
895892
together.
896893

897-
\todo[inline]{Copied these here for now, add hyperlink}
894+
\todo[inline]{Copied from \idr{Poly}, cannot import it due to tuple sugar
895+
issues}
898896

899897
> fold : (f : x -> y -> y) -> (l : List x) -> (b : y) -> y
900898
> fold f [] b = b
901899
> fold f (h::t) b = f h (fold f t b)
902900
>
903-
> In : (x : a) -> (l : List a) -> Type
904-
> In x [] = Void
905-
> In x (x' :: xs) = (x' = x) `Either` In x xs
906-
>
907901
> MStar' : ((s : List t) -> (In s ss) -> (s =~ re)) ->
908902
> (fold (++) ss []) =~ Star re
909903
> MStar' f = ?MStar__rhs
@@ -944,28 +938,6 @@ regular expression:
944938

945939
We can then phrase our theorem as follows:
946940

947-
\todo[inline]{Copied here for now}
948-
949-
> in_app_iff : (In a (l++l')) <-> (In a l `Either` In a l')
950-
> in_app_iff {l} {l'} = (to l l', fro l l')
951-
> where
952-
> to : (l, l' : List x) -> In a (l ++ l') -> (In a l) `Either` (In a l')
953-
> to [] [] prf = absurd prf
954-
> to [] _ prf = Right prf
955-
> to (_ :: _) _ (Left Refl) = Left $ Left Refl
956-
> to (_ :: xs) l' (Right prf) =
957-
> case to xs l' prf of
958-
> Left ixs => Left $ Right ixs
959-
> Right il' => Right il'
960-
> fro : (l, l' : List x) -> (In a l) `Either` (In a l') -> In a (l ++ l')
961-
> fro [] _ (Left prf) = absurd prf
962-
> fro (_ :: _) _ (Left (Left Refl)) = Left Refl
963-
> fro (_ :: xs) l' (Left (Right prf)) = Right $ fro xs l' (Left prf)
964-
> fro _ [] (Right prf) = absurd prf
965-
> fro [] _ (Right prf) = prf
966-
> fro (_ :: ys) l' prf@(Right _) = Right $ fro ys l' prf
967-
>
968-
969941
\todo[inline]{Some unfortunate implicit plumbing}
970942

971943
> destruct : In x (s1 ++ s2) -> (In x s1) `Either` (In x s2)
@@ -1243,15 +1215,6 @@ computations to statements in \idr{Type}. But performing this conversion in the
12431215
way we did it there can result in tedious proof scripts. Consider the proof of
12441216
the following theorem:
12451217

1246-
\todo[inline]{Copy here for now}
1247-
1248-
> beq_nat_true : {n, m : Nat} -> n == m = True -> n = m
1249-
> beq_nat_true {n=Z} {m=Z} _ = Refl
1250-
> beq_nat_true {n=(S _)} {m=Z} Refl impossible
1251-
> beq_nat_true {n=Z} {m=(S _)} Refl impossible
1252-
> beq_nat_true {n=(S n')} {m=(S m')} eq =
1253-
> rewrite beq_nat_true {n=n'} {m=m'} eq in Refl
1254-
>
12551218
> filter_not_empty_In : {n : Nat} -> Not (filter ((==) n) l = []) -> In n l
12561219
> filter_not_empty_In {l=[]} contra = contra Refl
12571220
> filter_not_empty_In {l=(x::_)} {n} contra with (n == x) proof h
@@ -1325,19 +1288,6 @@ perform case analysis on \idr{b} while at the same time generating appropriate
13251288
hypothesis in the two branches (\idr{p} in the first subgoal and \idr{Not p} in
13261289
the second).
13271290

1328-
\todo[inline]{Copy here for now}
1329-
1330-
> beq_nat_true_iff : (n1, n2 : Nat) -> (n1 == n2 = True) <-> (n1 = n2)
1331-
> beq_nat_true_iff n1 n2 = (to, fro n1 n2)
1332-
> where
1333-
> to : (n1 == n2 = True) -> (n1 = n2)
1334-
> to = beq_nat_true {n=n1} {m=n2}
1335-
> fro : (n1, n2 : Nat) -> (n1 = n2) -> (n1 == n2 = True)
1336-
> fro n1 n1 Refl = sym $ beq_nat_refl n1
1337-
>
1338-
> iff_sym : (p <-> q) -> (q <-> p)
1339-
> iff_sym (pq, qp) = (qp, pq)
1340-
>
13411291
> beq_natP : {n, m : Nat} -> Reflect (n = m) (n == m)
13421292
> beq_natP {n} {m} = iff_reflect $ iff_sym $ beq_nat_true_iff n m
13431293
>

src/Lists.lidr

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
> %hide Prelude.Basics.snd
99
> %hide Prelude.Nat.pred
1010

11+
> %access public export
1112
> %default total
1213

1314

@@ -137,8 +138,12 @@ operator and square brackets as an "outfix" notation for constructing lists.
137138
> syntax [x] "::" [l] = Cons x l
138139
> syntax "[ ]" = Nil
139140
140-
\todo[inline]{Figure out \idr{syntax} command for this and edit the section}
141+
\todo[inline]{Seems it's impossible to make an Idris \idr{syntax} to overload
142+
the list notation. Edit the section.}
143+
144+
```coq
141145
Notation "[ x ; .. ; y ]" := ( cons x .. ( cons y nil ) ..).
146+
```
142147
143148
It is not necessary to understand the details of these declarations, but in case
144149
you are interested, here is roughly what's going on. The right associativity
@@ -158,19 +163,21 @@ The at level 60 part tells Coq how to parenthesize expressions that involve both
158163
:: and some other infix operator. For example, since we defined + as infix
159164
notation for the plus function at level 50,
160165
166+
```coq
161167
Notation "x + y" := ( plus x y )
162168
( at level 50, left associativity ).
169+
```
163170
164-
the + operator will bind tighter than :: , so 1 + 2 :: [3] will be parsed, as
165-
we'd expect, as (1 + 2) :: [3] rather than 1 + (2 :: [3]) .
171+
the `+` operator will bind tighter than `::` , so `1 + 2 :: [3]` will be parsed,
172+
as we'd expect, as `(1 + 2) :: [3]` rather than `1 + (2 :: [3])`.
166173
167-
(Expressions like "1 + 2 :: [3]" can be a little confusing when you read them
168-
in a .v file. The inner brackets, around 3, indicate a list, but the outer
174+
(Expressions like "`1 + 2 :: [3]`" can be a little confusing when you read them
175+
in a `.v` file. The inner brackets, around `3`, indicate a list, but the outer
169176
brackets, which are invisible in the HTML rendering, are there to instruct the
170177
"coqdoc" tool that the bracketed part should be displayed as Coq code rather
171178
than running text.)
172179
173-
The second and third Notation declarations above introduce the standard
180+
The second and third `Notation` declarations above introduce the standard
174181
square-bracket notation for lists; the right-hand side of the third one
175182
illustrates Coq's syntax for declaring n-ary notations and translating them to
176183
nested sequences of binary constructors.
@@ -337,6 +344,7 @@ multisets a little bit differently, which is why we don't use that name for this
337344
operation.)
338345
339346
\todo[inline]{How to forbid recursion here? Edit}
347+
340348
For \idr{sum} we're giving you a header that does not give explicit names to the
341349
arguments. Moreover, it uses the keyword Definition instead of Fixpoint, so
342350
even if you had names for the arguments, you wouldn't be able to process them
@@ -506,6 +514,7 @@ lists \idr{l}. Here's a concrete example:
506514
> rewrite inductiveHypothesis in Refl
507515
508516
\todo[inline]{Edit}
517+
509518
Notice that, as when doing induction on natural numbers, the as ... clause
510519
provided to the induction tactic gives a name to the induction hypothesis
511520
corresponding to the smaller list l1' in the cons case. Once again, this Coq
@@ -689,22 +698,24 @@ default for our present purposes.
689698
\ \todo[inline]{Edit, mention \idr{:s} and \idr{:apropos}?}
690699
691700
We've seen that proofs can make use of other theorems we've already proved,
692-
e.g., using rewrite . But in order to refer to a theorem, we need to know its
693-
name! Indeed, it is often hard even to remember what theorems have been proven,
694-
much less what they are called.
701+
e.g., using \idr{rewrite}. But in order to refer to a theorem, we need to know
702+
its name! Indeed, it is often hard even to remember what theorems have been
703+
proven, much less what they are called.
695704
696-
Coq's Search command is quite helpful with this. Typing Search foo will cause
697-
Coq to display a list of all theorems involving foo . For example, try
705+
Coq's `Search` command is quite helpful with this. Typing `Search foo` will
706+
cause Coq to display a list of all theorems involving `foo`. For example, try
698707
uncommenting the following line to see a list of theorems that we have proved
699-
about rev :
708+
about `rev`:
700709
710+
```coq
701711
(* Search rev. *)
712+
```
702713
703-
Keep Search in mind as you do the following exercises and throughout the rest of
704-
the book; it can save you a lot of time!
714+
Keep `Search` in mind as you do the following exercises and throughout the rest
715+
of the book; it can save you a lot of time!
705716
706-
If you are using ProofGeneral, you can run Search with C-c C-a C-a. Pasting its
707-
response into your buffer can be accomplished with C-c C-;.
717+
If you are using ProofGeneral, you can run `Search` with `C-c C-a C-a`. Pasting
718+
its response into your buffer can be accomplished with `C-c C-;`.
708719
709720
710721
=== List Exercises, Part 1
@@ -862,7 +873,8 @@ programming language: conditional expressions...
862873
> then Some a
863874
> else nth_error' l' (pred n)
864875
865-
\todo[inline]{Edit this paragraph}
876+
\todo[inline]{Edit or remove this paragraph, doesn't seem to hold in Idris}
877+
866878
Coq's conditionals are exactly like those found in any other language, with one
867879
small generalization. Since the boolean type is not built in, Coq actually
868880
supports conditional expressions over any inductively defined type with exactly

src/Logic.lidr

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
> module Logic
44

55
> import Basics
6+
> import Induction
7+
> import Tactics
68

79
> %hide Basics.Numbers.pred
810
> %hide Basics.Playground2.plus
911

12+
> %access public export
13+
> %default total
14+
1015
In previous chapters, we have seen many examples of factual claims
1116
(_propositions_) and ways of presenting evidence of their truth (_proofs_). In
1217
particular, we have worked extensively with equality propositions of the form
@@ -783,7 +788,7 @@ We will see many more examples of the idioms from this section in later chapters
783788

784789
\todo[inline]{Edit, Idris's core is likely some variant of MLTT}
785790

786-
Idris's logical core, the Calculus of Inductive Constructions, differs in some
791+
Coq's logical core, the Calculus of Inductive Constructions, differs in some
787792
important ways from other formal systems that are used by mathematicians for
788793
writing down precise and rigorous proofs. For example, in the most popular
789794
foundation for mainstream paper-and-pencil mathematics, Zermelo-Fraenkel Set
@@ -885,10 +890,6 @@ have is that it performs a call to \idr{++} on each step; running \idr{++} takes
885890
time asymptotically linear in the size of the list, which means that \idr{rev}
886891
has quadratic running time.
887892

888-
> rev : (l : List x) -> List x
889-
> rev [] = []
890-
> rev (h::t) = (rev t) ++ [h]
891-
892893
We can improve this with the following definition:
893894

894895
> rev_append : (l1, l2 : List x) -> List x
@@ -928,10 +929,6 @@ For instance, to claim that a number \idr{n} is even, we can say either
928929
We often say that the boolean \idr{evenb n} _reflects_ the proposition \idr{(k
929930
** n = double k)}.
930931

931-
> double : (n : Nat) -> Nat
932-
> double Z = Z
933-
> double (S k) = S (S (double k))
934-
935932
> evenb_double : evenb (double k) = True
936933
> evenb_double {k = Z} = Refl
937934
> evenb_double {k = (S k')} = evenb_double {k=k'}
@@ -951,33 +948,16 @@ $\square$
951948
> even_bool_prop = (to, fro)
952949
> where
953950
> to : evenb n = True -> (k ** n = double k)
954-
> to {n} prf = let
955-
> (k ** p) = evenb_double_conv {n}
956-
> in
957-
958-
\todo[inline]{Is there a shorter way?}
959-
960-
> (k ** replace prf p {P = \x => n = if x then double k else S (double k)})
951+
> to {n} prf =
952+
> let (k ** p) = evenb_double_conv {n}
953+
> in (k ** rewrite p in rewrite prf in Refl)
961954
> fro : (k ** n = double k) -> evenb n = True
962955
> fro {n} (k**prf) = rewrite prf in evenb_double {k}
963956

964957
Similarly, to state that two numbers \idr{n} and \idr{m} are equal, we can say
965958
either (1) that \idr{n == m} returns \idr{True} or (2) that \idr{n = m}. These
966959
two notions are equivalent.
967960

968-
\todo[inline]{Copy these 2 here for now}
969-
970-
> beq_nat_true : {n, m : Nat} -> n == m = True -> n = m
971-
> beq_nat_true {n=Z} {m=Z} _ = Refl
972-
> beq_nat_true {n=(S _)} {m=Z} Refl impossible
973-
> beq_nat_true {n=Z} {m=(S _)} Refl impossible
974-
> beq_nat_true {n=(S n')} {m=(S m')} eq =
975-
> rewrite beq_nat_true {n=n'} {m=m'} eq in Refl
976-
977-
> beq_nat_refl : (n : Nat) -> True = n == n
978-
> beq_nat_refl Z = Refl
979-
> beq_nat_refl (S k) = beq_nat_refl k
980-
981961
> beq_nat_true_iff : (n1, n2 : Nat) -> (n1 == n2 = True) <-> (n1 = n2)
982962
> beq_nat_true_iff n1 n2 = (to, fro n1 n2)
983963
> where
@@ -1158,12 +1138,6 @@ However, if we happen to know that \idr{p} is reflected in some boolean term
11581138
\idr{b}, then knowing whether it holds or not is trivial: we just have to check
11591139
the value of \idr{b}.
11601140

1161-
\todo[inline]{Remove when a release with
1162-
https://github.com/idris-lang/Idris-dev/pull/3925 happens}
1163-
1164-
> Uninhabited (False = True) where
1165-
> uninhabited Refl impossible
1166-
11671141
> restricted_excluded_middle : (p <-> b = True) -> p `Either` Not p
11681142
> restricted_excluded_middle {b = True} (_, bp) = Left $ bp Refl
11691143
> restricted_excluded_middle {b = False} (pb, _) = Right $ uninhabited . pb

0 commit comments

Comments
 (0)