|
91 | 91 | | saturday |
92 | 92 | | sunday. |
93 | 93 | """ |
94 | | -#from kdrag.all import * |
| 94 | + |
| 95 | +# from kdrag.all import * |
95 | 96 | import kdrag as kd |
96 | 97 | from kdrag import smt |
97 | 98 |
|
|
118 | 119 | end. |
119 | 120 | """ |
120 | 121 | d = smt.Const("d", day) |
121 | | -next_weekday = kd.define("next_weekday", [d], kd.cond( |
122 | | - (d.is_monday, day.tuesday), |
123 | | - (d.is_tuesday, day.wednesday), |
124 | | - (d.is_wednesday, day.thursday), |
125 | | - (d.is_thursday, day.friday), |
126 | | - (d.is_friday, day.monday), |
127 | | - (d.is_saturday, day.monday), |
128 | | - (d.is_sunday, day.monday), |
129 | | -)) |
| 122 | +next_weekday = kd.define( |
| 123 | + "next_weekday", |
| 124 | + [d], |
| 125 | + kd.cond( |
| 126 | + (d.is_monday, day.tuesday), |
| 127 | + (d.is_tuesday, day.wednesday), |
| 128 | + (d.is_wednesday, day.thursday), |
| 129 | + (d.is_thursday, day.friday), |
| 130 | + (d.is_friday, day.monday), |
| 131 | + (d.is_saturday, day.monday), |
| 132 | + (d.is_sunday, day.monday), |
| 133 | + ), |
| 134 | +) |
130 | 135 |
|
131 | 136 |
|
132 | 137 | # %% [markdown] |
|
155 | 160 | # interpreter under your favorite IDE (see the [Preface] for |
156 | 161 | # installation instructions) and try it for yourself. Load this |
157 | 162 | # file, [Basics.v], from the book's Coq sources, find the above |
158 | | -# example, submit it to Coq, and observe the result.) |
| 163 | +# example, submit it to Coq, and observe the result.) |
159 | 164 | # |
160 | 165 | # Second, we can record what we _expect_ the result to be in the |
161 | 166 | # form of a Coq example: |
|
175 | 180 | this: |
176 | 181 | """ |
177 | 182 | """Proof. simpl. reflexivity. Qed.""" |
178 | | -#l.unfold(next_weekday, next_weekday) |
| 183 | +# l.unfold(next_weekday, next_weekday) |
179 | 184 | l.auto(by=next_weekday.defn) |
180 | 185 | l.qed() |
181 | 186 |
|
|
201 | 206 | # We'll come back to this topic in later chapters. |
202 | 207 |
|
203 | 208 | # %% [markdown] |
204 | | -# ### Homework Submission Guidelines |
| 209 | +# ### Homework Submission Guidelines |
205 | 210 | # |
206 | 211 | # (** If you are using _Software Foundations_ in a course, your |
207 | 212 | # instructor may use automatic scripts to help grade your homework |
|
257 | 262 | # that the grading scripts can use it for internal purposes. *) |
258 | 263 |
|
259 | 264 | # %% |
260 | | -#From Coq Require Export String. |
| 265 | +# From Coq Require Export String. |
261 | 266 |
|
262 | 267 | # %% [markdown] |
263 | 268 | # ### Booleans |
264 | 269 | # Following the pattern of the days of the week above, we can |
265 | 270 | # define the standard type [bool] of booleans, with members [true] |
266 | | -# and [false]. |
| 271 | +# and [false]. |
267 | 272 |
|
268 | 273 | # %% |
269 | 274 | """Inductive bool : Type := |
|
274 | 279 |
|
275 | 280 | # %% [markdown] |
276 | 281 | # Functions over booleans can be defined in the same way as |
277 | | -# above: |
| 282 | +# above: |
278 | 283 |
|
279 | 284 | # %% |
280 | 285 | """ |
|
298 | 303 | """ |
299 | 304 |
|
300 | 305 |
|
301 | | - |
302 | 306 | b, b1, b2 = smt.Consts("b b1 b2", bool) |
303 | 307 |
|
304 | 308 |
|
305 | | -negb = kd.define("negb", [b], kd.cond( |
306 | | - (b.is_true, bool.false), |
307 | | - (b.is_false, bool.true), |
308 | | -)) |
309 | | -andb = kd.define("andb", [b1, b2], kd.cond( |
310 | | - (b1.is_true, b2), |
311 | | - (b1.is_false, bool.false), |
312 | | -)) |
313 | | -orb = kd.define("orb", [b1, b2], kd.cond( |
314 | | - (b1.is_true, bool.true), |
315 | | - (b1.is_false, b2), |
316 | | -)) |
317 | | - |
318 | | -negb1 = kd.define("negb1", [b], |
319 | | -b.match_( |
320 | | - (bool.true, bool.false), |
321 | | - (bool.false, bool.true), |
322 | | -)) |
| 309 | +negb = kd.define( |
| 310 | + "negb", |
| 311 | + [b], |
| 312 | + kd.cond( |
| 313 | + (b.is_true, bool.false), |
| 314 | + (b.is_false, bool.true), |
| 315 | + ), |
| 316 | +) |
| 317 | +andb = kd.define( |
| 318 | + "andb", |
| 319 | + [b1, b2], |
| 320 | + kd.cond( |
| 321 | + (b1.is_true, b2), |
| 322 | + (b1.is_false, bool.false), |
| 323 | + ), |
| 324 | +) |
| 325 | +orb = kd.define( |
| 326 | + "orb", |
| 327 | + [b1, b2], |
| 328 | + kd.cond( |
| 329 | + (b1.is_true, bool.true), |
| 330 | + (b1.is_false, b2), |
| 331 | + ), |
| 332 | +) |
| 333 | + |
| 334 | +negb1 = kd.define( |
| 335 | + "negb1", |
| 336 | + [b], |
| 337 | + b.match_( |
| 338 | + (bool.true, bool.false), |
| 339 | + (bool.false, bool.true), |
| 340 | + ), |
| 341 | +) |
323 | 342 |
|
324 | 343 | kd.prove(smt.ForAll([b], negb(b) == negb1(b)), by=[negb1.defn, negb.defn]) |
325 | 344 |
|
|
357 | 376 | # %% [markdown] |
358 | 377 | # We can also introduce some familiar infix syntax for the |
359 | 378 | # boolean operations we have just defined. The [Notation] command |
360 | | -# defines a new symbolic notation for an existing definition. |
| 379 | +# defines a new symbolic notation for an existing definition. |
361 | 380 | # |
362 | 381 |
|
363 | 382 | # %% |
|
402 | 421 | if b1 then true |
403 | 422 | else b2. |
404 | 423 | """ |
405 | | -negb1 = kd.define("negb1", [b], smt.If(b1.is_true, bool.false, bool.true)) |
406 | | -andb1 = kd.define("andb1", [b1, b2], smt.If(b1.is_true, b2, bool.false)) |
407 | | -orb1 = kd.define("orb1", [b1, b2], smt.If(b1.is_true, bool.true, b2)) |
| 424 | +negb2 = kd.define("negb2", [b], smt.If(b1.is_true, bool.false, bool.true)) |
| 425 | +andb2 = kd.define("andb2", [b1, b2], smt.If(b1.is_true, b2, bool.false)) |
| 426 | +orb2 = kd.define("orb2", [b1, b2], smt.If(b1.is_true, bool.true, b2)) |
408 | 427 |
|
409 | 428 |
|
410 | 429 | # %% [markdown] |
|
630 | 649 | # %% |
631 | 650 | c = smt.Const("c", color) |
632 | 651 | p = smt.Const("p", rgb) |
633 | | -monochome = kd.define("monochrome", [c], |
634 | | - c.match_( |
635 | | - (color.black, bool.true), |
636 | | - (color.white, bool.true), |
637 | | - (color.primary(p), bool.false) |
638 | | - ) |
639 | | - ) |
640 | | - |
641 | | -isred = kd.define("isred", [c], |
642 | | - c.match_( |
643 | | - (color.primary(rgb.red), bool.true), |
644 | | - default=bool.false) |
645 | | - ) |
| 652 | +monochome = kd.define( |
| 653 | + "monochrome", |
| 654 | + [c], |
| 655 | + c.match_( |
| 656 | + (color.black, bool.true), |
| 657 | + (color.white, bool.true), |
| 658 | + (color.primary(p), bool.false), |
| 659 | + ), |
| 660 | +) |
| 661 | + |
| 662 | +isred = kd.define( |
| 663 | + "isred", [c], c.match_((color.primary(rgb.red), bool.true), default=bool.false) |
| 664 | +) |
646 | 665 | isred.defn |
647 | 666 |
|
648 | 667 | # %% [markdown] |
|
719 | 738 | bit = kd.Enum("bit", "B1 B0") |
720 | 739 | nybble = kd.Struct("nybble", ("b0", bit), ("b1", bit), ("b2", bit), ("b3", bit)) |
721 | 740 |
|
722 | | -nybble( |
723 | | - b0=bit.B0, |
724 | | - b1=bit.B1, |
725 | | - b2=bit.B0, |
726 | | - b3=bit.B1 |
727 | | -) |
| 741 | +nybble(b0=bit.B0, b1=bit.B1, b2=bit.B0, b3=bit.B1) |
728 | 742 |
|
729 | 743 | # %% [markdown] |
730 | 744 | # |
731 | 745 | # |
732 | 746 | # (* ================================================================= *) |
733 | | -# ## Numbers |
| 747 | +# ## Numbers |
734 | 748 | # |
735 | 749 | # (** We put this section in a module so that our own definition of |
736 | 750 | # natural numbers does not interfere with the one from the |
|
852 | 866 |
|
853 | 867 | # %% |
854 | 868 | n = smt.Const("n", Nat) |
855 | | -pred = kd.define("pred", [n], n.match_( |
856 | | - (Nat.O, Nat.O), |
857 | | - (Nat.S(n), n) |
858 | | -)) |
| 869 | +pred = kd.define("pred", [n], n.match_((Nat.O, Nat.O), (Nat.S(n), n))) |
859 | 870 | pred.defn |
860 | 871 |
|
861 | 872 | # %% [markdown] |
|
0 commit comments