Skip to content

Commit 776f2b4

Browse files
author
Alex Gryzlov
committed
fixes for latest Idris
1 parent c0eaca7 commit 776f2b4

File tree

8 files changed

+68
-132
lines changed

8 files changed

+68
-132
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Others may work, but here are the versions I'm using.
1919
| Dependency | Version |
2020
|------------------|----------------------------------------|
2121
| [(run)ghc][GHC] | 8.0.2 |
22-
| [Idris][] | 1.0 |
22+
| [Idris][] | 1.1.1 |
2323
| [latexmk][] | 4.52c |
2424
| [Make][] | 4.2.1 |
2525
| [minted][] | 2.4.1 |

software_foundations.ipkg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ modules = Basics
77
, Tactics
88
, Logic
99
, IndProp
10+
, Imp
1011

1112
brief = "Software Foundations in Idris"
1213
version = 0.0.1.0

src/Imp.lidr

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
> module Imp
44

5+
> import Logic
6+
57
In this chapter, we begin a new direction that will continue for the rest of the
68
course. Up to now most of our attention has been focused on various aspects of
79
Idris itself, while from now on we'll mostly be using Idris to formalize other
@@ -1085,9 +1087,9 @@ Here's an attempt at defining an evaluation function for commands, omitting the
10851087
> let st' = ceval_fun_no_while st c1
10861088
> in ceval_fun_no_while st' c2
10871089
> ceval_fun_no_while st (CIf b c1 c2) =
1088-
> ceval_fun_no_while st $ if beval st b
1089-
> then c1
1090-
> else c2
1090+
> if beval st b
1091+
> then ceval_fun_no_while st c1
1092+
> else ceval_fun_no_while st c2
10911093
> ceval_fun_no_while st (CWhile b c) = st -- bogus
10921094
10931095
In a traditional functional programming language like OCaml or Haskell we could
@@ -1232,8 +1234,8 @@ rather than just letting Idris's computation mechanism do it for us.
12321234
> THEN (Y ::= ANum 3)
12331235
> ELSE (Z ::= ANum 4)
12341236
> FI)
1235-
> ) / empty_state
1236-
> |/ (t_update Z 4 $ t_update X 2 empty_state)
1237+
> ) / Imp.empty_state
1238+
> |/ (t_update Z 4 $ t_update X 2 Imp.empty_state)
12371239
> ceval_example1 =
12381240
> E_Seq
12391241
> (E_Ass Refl)
@@ -1245,8 +1247,8 @@ rather than just letting Idris's computation mechanism do it for us.
12451247
12461248
> ceval_example2 :
12471249
> ((X ::= ANum 0);; (Y ::= ANum 1);; (Z ::= ANum 2))
1248-
> / empty_state
1249-
> |/ (t_update Z 2 $ t_update Y 1 $ t_update X 0 empty_state)
1250+
> / Imp.empty_state
1251+
> |/ (t_update Z 2 $ t_update Y 1 $ t_update X 0 Imp.empty_state)
12501252
> ceval_example2 = ?ceval_example2_rhs
12511253
12521254
$\square$
@@ -1262,13 +1264,13 @@ as intended for \idr{X = 2} (this is trickier than you might expect).
12621264
> pup_to_n = ?pup_to_n_rhs
12631265
12641266
> pup_to_2_ceval :
1265-
> pup_to_n / (t_update X 2 empty_state) |/
1267+
> Imp.pup_to_n / (t_update X 2 Imp.empty_state) |/
12661268
> (t_update X 0 $
12671269
> t_update Y 3 $
12681270
> t_update X 1 $
12691271
> t_update Y 2 $
12701272
> t_update Y 0 $
1271-
> t_update X 2 empty_state)
1273+
> t_update X 2 Imp.empty_state)
12721274
> pup_to_2_ceval = ?pup_to_2_ceval_rhs
12731275
12741276
$\square$
@@ -1476,11 +1478,11 @@ will never emit such a malformed program.
14761478
> List Nat
14771479
> s_execute st stack prog = ?s_execute_rhs
14781480
1479-
> s_execute1 : s_execute empty_state [] [SPush 5, SPush 3, SPush 1, SMinus]
1481+
> s_execute1 : s_execute Imp.empty_state [] [SPush 5, SPush 3, SPush 1, SMinus]
14801482
> = [2,5]
14811483
> s_execute1 = ?s_execute1_rhs
14821484
1483-
> s_execute2 : s_execute (t_update X 3 empty_state) [3,4]
1485+
> s_execute2 : s_execute (t_update X 3 Imp.empty_state) [3,4]
14841486
> [SPush 4, SLoad X, SMult, SPlus]
14851487
> = [15,4]
14861488
> s_execute2 = ?s_execute2_rhs

src/IndProp.lidr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
> import Basics
66
> import Induction
77
>
8+
> %access public export
9+
>
810
> %hide Basics.Numbers.pred
911
> %hide Prelude.Stream.(::)
1012
>

src/Logic.lidr

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
> import Basics
66

7-
> %hide Basics.Numbers.pred
7+
> %access public export
8+
9+
> %hide Basics.Numbers.pred
810
> %hide Basics.Playground2.plus
911

1012
In previous chapters, we have seen many examples of factual claims
1113
(_propositions_) and ways of presenting evidence of their truth (_proofs_). In
1214
particular, we have worked extensively with equality propositions of the form
1315
\idr{e1 = e2}, with implications (\idr{p -> q}), and with quantified
1416
propositions (\idr{x -> P(x)}). In this chapter, we will see how Idris can be
15-
used to carry out other familiar forms of logical reasoning.
17+
used to carry out other familiar forms of logical reasoning.
1618

1719
Before diving into details, let's talk a bit about the status of mathematical
1820
statements in Idris. Recall that Idris is a _typed_ language, which means that
@@ -57,7 +59,7 @@ type signatures.
5759
But propositions can be used in many other ways. For example, we can give a name
5860
to a proposition as a value on its own, just as we have given names to
5961
expressions of other sorts (you'll soon see why we start the name with a capital
60-
letter).
62+
letter).
6163

6264
> Plus_fact : Type
6365
> Plus_fact = 2+2=4
@@ -82,7 +84,7 @@ arguments of some type and return a proposition. For instance, the following
8284
function takes a number and returns a proposition asserting that this number is
8385
equal to three:
8486

85-
> is_three : Nat -> Type
87+
> is_three : Nat -> Type
8688
> is_three n = n=3
8789

8890
```idris
@@ -171,8 +173,8 @@ intermediate steps in proofs, especially in bigger developments. Here's a simple
171173
example:
172174

173175
> and_example3 : (n, m : Nat) -> n + m = 0 -> n * m = 0
174-
> and_example3 n m prf =
175-
> let (nz, _) = and_exercise n m prf in
176+
> and_example3 n m prf =
177+
> let (nz, _) = and_exercise n m prf in
176178
> rewrite nz in Refl
177179

178180
\todo[inline]{Remove lemma and exercise, use \idr{fst} and \idr{snd} directly?}
@@ -344,7 +346,7 @@ Write an informal proof of \idr{double_neg}:
344346

345347
_Theorem_: \idr{p} implies \idr{Not $ Not p}, for any proposition \idr{p}.
346348

347-
> -- FILL IN HERE
349+
> -- FILL IN HERE
348350

349351
$\square$
350352

@@ -369,7 +371,7 @@ $\square$
369371

370372
Write an informal proof (in English) of the proposition \idr{Not (p, Not p)}.
371373

372-
> -- FILL IN HERE
374+
> -- FILL IN HERE
373375

374376
$\square$
375377

@@ -484,16 +486,16 @@ We can now use these facts with \idr{rewrite} and \idr{Refl} to give smooth
484486
proofs of statements involving equivalences. Here is a ternary version of the
485487
previous \idr{mult_0} result:
486488

487-
> mult_0_3 : (n * m * p = Z) <->
489+
> mult_0_3 : (n * m * p = Z) <->
488490
> ((n = Z) `Either` ((m = Z) `Either` (p = Z)))
489491
> mult_0_3 = (to, fro)
490-
> where
492+
> where
491493
> to : (n * m * p = Z) -> ((n = Z) `Either` ((m = Z) `Either` (p = Z)))
492-
> to {n} {m} {p} prf = let
494+
> to {n} {m} {p} prf = let
493495
> (nm_p_to, _) = mult_0 {n=(n*m)} {m=p}
494496
> (n_m_to, _) = mult_0 {n} {m}
495497
> (_, or_a_fro) = or_assoc {p=(n=Z)} {q=(m=Z)} {r=(p=Z)}
496-
> in or_a_fro $ case nm_p_to prf of
498+
> in or_a_fro $ case nm_p_to prf of
497499
> Left prf => Left $ n_m_to prf
498500
> Right prf => Right prf
499501
> fro : ((n = Z) `Either` ((m = Z) `Either` (p = Z))) -> (n * m * p = Z)
@@ -504,7 +506,7 @@ previous \idr{mult_0} result:
504506
The apply tactic can also be used with \idr{<->}. When given an equivalence as
505507
its argument, apply tries to guess which side of the equivalence to use.
506508

507-
> apply_iff_example : (n, m : Nat) -> n * m = Z -> ((n = Z) `Either` (m = Z))
509+
> apply_iff_example : (n, m : Nat) -> n * m = Z -> ((n = Z) `Either` (m = Z))
508510
> apply_iff_example n m = fst $ mult_0 {n} {m}
509511

510512

@@ -549,7 +551,7 @@ $\square$
549551

550552
Prove that existential quantification distributes over disjunction.
551553

552-
> dist_exists_or : {p, q : a -> Type} -> (x ** (p x `Either` q x)) <->
554+
> dist_exists_or : {p, q : a -> Type} -> (x ** (p x `Either` q x)) <->
553555
> ((x ** p x) `Either` (x ** q x))
554556
> dist_exists_or = ?dist_exists_or_rhs
555557

@@ -593,7 +595,7 @@ We can also prove more generic, higher-level lemmas about \idr{In}.
593595
Note, in the next, how \idr{In} starts out applied to a variable and only gets
594596
expanded when we do case analysis on this variable:
595597

596-
> In_map : (f : a -> b) -> (l : List a) -> (x : a) -> In x l ->
598+
> In_map : (f : a -> b) -> (l : List a) -> (x : a) -> In x l ->
597599
> In (f x) (map f l)
598600
> In_map _ [] _ ixl = absurd ixl
599601
> In_map f (x' :: xs) x (Left prf) = rewrite prf in Left Refl
@@ -609,7 +611,7 @@ set of strengths and limitations.
609611

610612
==== Exercise: 2 stars (In_map_iff)
611613

612-
> In_map_iff : (f : a -> b) -> (l : List a) -> (y : b) ->
614+
> In_map_iff : (f : a -> b) -> (l : List a) -> (y : b) ->
613615
> (In y (map f l)) <-> (x ** (f x = y, In x l))
614616
> In_map_iff f l y = ?In_map_iff_rhs
615617

@@ -758,7 +760,7 @@ A more elegant alternative is to apply \idr{plusCommutative} directly to the
758760
arguments we want to instantiate it with, in much the same way as we apply a
759761
polymorphic function to a type argument.
760762

761-
> plus_comm3 n m p = rewrite plusCommutative n (m+p) in
763+
> plus_comm3 n m p = rewrite plusCommutative n (m+p) in
762764
> rewrite plusCommutative m p in Refl
763765

764766
You can "use theorems as functions" in this way with almost all tactics that
@@ -768,12 +770,12 @@ example, to supply wildcards as arguments to be inferred, or to declare some
768770
hypotheses to a theorem as implicit by default. These features are illustrated
769771
in the proof below.
770772

771-
> lemma_application_ex : (n : Nat) -> (ns : List Nat) ->
773+
> lemma_application_ex : (n : Nat) -> (ns : List Nat) ->
772774
> In n (map (\m => m * 0) ns) -> n = 0
773775
> lemma_application_ex _ [] prf = absurd prf
774-
> lemma_application_ex _ (y :: _) (Left prf) =
776+
> lemma_application_ex _ (y :: _) (Left prf) =
775777
> rewrite sym $ multZeroRightZero y in sym prf
776-
> lemma_application_ex n (_ :: xs) (Right prf) =
778+
> lemma_application_ex n (_ :: xs) (Right prf) =
777779
> lemma_application_ex n xs prf
778780

779781
We will see many more examples of the idioms from this section in later chapters.
@@ -850,7 +852,7 @@ that this isn't just something we're going to come back and fill in later!
850852
We can now invoke functional extensionality in proofs:
851853

852854
> function_equality_ex2 : (\x => plus x 1) = (\x => plus 1 x)
853-
> function_equality_ex2 = functional_extensionality $ \x => plusCommutative x 1
855+
> function_equality_ex2 = functional_extensionality $ \x => plusCommutative x 1
854856

855857
Naturally, we must be careful when adding new axioms into Idris's logic, as they
856858
may render it _inconsistent_ -- that is, they may make it possible to prove
@@ -883,7 +885,7 @@ Print Assumptions function_equality_ex2.
883885
One problem with the definition of the list-reversing function \idr{rev} that we
884886
have is that it performs a call to \idr{++} on each step; running \idr{++} takes
885887
time asymptotically linear in the size of the list, which means that \idr{rev}
886-
has quadratic running time.
888+
has quadratic running time.
887889

888890
> rev : (l : List x) -> List x
889891
> rev [] = []
@@ -928,8 +930,8 @@ For instance, to claim that a number \idr{n} is even, we can say either
928930
We often say that the boolean \idr{evenb n} _reflects_ the proposition \idr{(k
929931
** n = double k)}.
930932

931-
> double : (n : Nat) -> Nat
932-
> double Z = Z
933+
> double : (n : Nat) -> Nat
934+
> double Z = Z
933935
> double (S k) = S (S (double k))
934936

935937
> evenb_double : evenb (double k) = True
@@ -952,8 +954,8 @@ $\square$
952954
> where
953955
> to : evenb n = True -> (k ** n = double k)
954956
> to {n} prf = let
955-
> (k ** p) = evenb_double_conv {n}
956-
> in
957+
> (k ** p) = evenb_double_conv {n}
958+
> in
957959

958960
\todo[inline]{Is there a shorter way?}
959961

@@ -973,10 +975,10 @@ These two notions are equivalent.
973975
> beq_nat_true {n=Z} {m=(S _)} Refl impossible
974976
> beq_nat_true {n=(S n')} {m=(S m')} eq =
975977
> rewrite beq_nat_true {n=n'} {m=m'} eq in Refl
976-
978+
977979
> beq_nat_refl : (n : Nat) -> True = beq_nat n n
978980
> beq_nat_refl Z = Refl
979-
> beq_nat_refl (S k) = beq_nat_refl k
981+
> beq_nat_refl (S k) = beq_nat_refl k
980982

981983
> beq_nat_true_iff : (n1, n2 : Nat) -> (beq_nat n1 n2 = True) <-> (n1 = n2)
982984
> beq_nat_true_iff n1 n2 = (to, fro n1 n2)
@@ -1068,11 +1070,11 @@ showing the complementary strengths of booleans and general propositions.
10681070
The following lemmas relate the propositional connectives studied in this
10691071
chapter to the corresponding boolean operations.
10701072

1071-
> andb_true_iff : (b1, b2 : Bool) -> (b1 && b2 = True) <->
1073+
> andb_true_iff : (b1, b2 : Bool) -> (b1 && b2 = True) <->
10721074
> (b1 = True, b2 = True)
10731075
> andb_true_iff b1 b2 = ?andb_true_iff_rhs
10741076

1075-
> orb_true_iff : (b1, b2 : Bool) -> (b1 || b2 = True) <->
1077+
> orb_true_iff : (b1, b2 : Bool) -> (b1 || b2 = True) <->
10761078
> ((b1 = True) `Either` (b2 = True))
10771079
> orb_true_iff b1 b2 = ?orb_true_iff_rhs
10781080

@@ -1102,7 +1104,7 @@ function below. To make sure that your definition is correct, prove the lemma
11021104
> beq_list : (beq : a -> a -> Bool) -> (l1, l2 : List a) -> Bool
11031105
> beq_list beq l1 l2 = ?beq_list_rhs
11041106

1105-
> beq_list_true_iff : (beq : a -> a -> Bool) ->
1107+
> beq_list_true_iff : (beq : a -> a -> Bool) ->
11061108
> ((a1, a2 : a) -> (beq a1 a2 = True) <-> (a1 = a2)) ->
11071109
> ((l1, l2 : List a) -> (beq_list beq l1 l2 = True) <-> (l1 = l2))
11081110
> beq_list_true_iff beq f l1 l2 = ?beq_list_true_iff_rhs
@@ -1118,18 +1120,18 @@ Recall the function \idr{forallb}, from the exercise
11181120
> forallb : (test : x -> Bool) -> (l : List x) -> Bool
11191121
> forallb _ [] = True
11201122
> forallb test (x :: xs) = test x && forallb test xs
1121-
1123+
11221124
Prove the theorem below, which relates \idr{forallb} to the \idr{All} property
11231125
of the above exercise.
11241126

1125-
> forallb_true_iff : (l : List x) -> (forallb test l = True) <->
1127+
> forallb_true_iff : (l : List x) -> (forallb test l = True) <->
11261128
> (All (\x => test x = True) l)
11271129
> forallb_true_iff l = ?forallb_true_iff_rhs
11281130

11291131
Are there any important properties of the function \idr{forallb} which are not
11301132
captured by this specification?
11311133

1132-
> -- FILL IN HERE
1134+
> -- FILL IN HERE
11331135

11341136
$\square$
11351137

@@ -1158,12 +1160,6 @@ However, if we happen to know that \idr{p} is reflected in some boolean term
11581160
\idr{b}, then knowing whether it holds or not is trivial: we just have to check
11591161
the value of \idr{b}.
11601162

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-
11671163
> restricted_excluded_middle : (p <-> b = True) -> p `Either` Not p
11681164
> restricted_excluded_middle {b = True} (_, bp) = Left $ bp Refl
11691165
> restricted_excluded_middle {b = False} (pb, _) = Right $ uninhabited . pb
@@ -1174,9 +1170,9 @@ natural numbers \idr{n} and \idr{m}.
11741170
\todo[inline]{Is there a simpler way to write this? Maybe with setoids?}
11751171

11761172
> restricted_excluded_middle_eq : (n, m : Nat) -> (n = m) `Either` Not (n = m)
1177-
> restricted_excluded_middle_eq n m =
1173+
> restricted_excluded_middle_eq n m =
11781174
> restricted_excluded_middle (to n m, fro n m)
1179-
> where
1175+
> where
11801176
> to : (n, m : Nat) -> (n=m) -> (n==m)=True
11811177
> to Z Z prf = Refl
11821178
> to Z (S _) Refl impossible

src/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ LIDR_FILES := Preface.lidr \
1818
Poly.lidr \
1919
Tactics.lidr \
2020
Logic.lidr \
21-
IndProp.lidr
21+
IndProp.lidr \
22+
Imp.lidr
23+
2224
# TODO: Add more chapters, in order, here.
2325

2426

0 commit comments

Comments
 (0)