Mercurial > cpdt > repo
comparison src/Subset.v @ 302:7b38729be069
Tweak mark-up to support coqdoc 8.3
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Mon, 17 Jan 2011 15:12:30 -0500 |
parents | b441010125d4 |
children | d5787b70cf48 |
comparison
equal
deleted
inserted
replaced
301:f4768d5a75eb | 302:7b38729be069 |
---|---|
148 (** %\vspace{-.15in}% [[ | 148 (** %\vspace{-.15in}% [[ |
149 Notation Scope | 149 Notation Scope |
150 "{ x : A | P }" := sig (fun x : A => P) | 150 "{ x : A | P }" := sig (fun x : A => P) |
151 : type_scope | 151 : type_scope |
152 (default interpretation) | 152 (default interpretation) |
153 ]] *) | 153 ]] |
154 *) | |
154 | 155 |
155 Definition pred_strong2 (s : {n : nat | n > 0}) : nat := | 156 Definition pred_strong2 (s : {n : nat | n > 0}) : nat := |
156 match s with | 157 match s with |
157 | exist O pf => match zgtz pf with end | 158 | exist O pf => match zgtz pf with end |
158 | exist (S n') _ => n' | 159 | exist (S n') _ => n' |
163 Eval compute in pred_strong2 (exist _ 2 two_gt0). | 164 Eval compute in pred_strong2 (exist _ 2 two_gt0). |
164 (** %\vspace{-.15in}% [[ | 165 (** %\vspace{-.15in}% [[ |
165 = 1 | 166 = 1 |
166 : nat | 167 : nat |
167 | 168 |
168 ]] *) | 169 ]] |
170 *) | |
169 | 171 |
170 Extraction pred_strong2. | 172 Extraction pred_strong2. |
171 | 173 |
172 (** %\begin{verbatim} | 174 (** %\begin{verbatim} |
173 (** val pred_strong2 : nat -> nat **) | 175 (** val pred_strong2 : nat -> nat **) |
198 Eval compute in pred_strong3 (exist _ 2 two_gt0). | 200 Eval compute in pred_strong3 (exist _ 2 two_gt0). |
199 (** %\vspace{-.15in}% [[ | 201 (** %\vspace{-.15in}% [[ |
200 = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) | 202 = exist (fun m : nat => 2 = S m) 1 (refl_equal 2) |
201 : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m} | 203 : {m : nat | proj1_sig (exist (lt 0) 2 two_gt0) = S m} |
202 | 204 |
203 ]] *) | 205 ]] |
206 *) | |
204 | 207 |
205 (** The function [proj1_sig] extracts the base value from a subset type. It turns out that we need to include an explicit [return] clause here, since Coq's heuristics are not smart enough to propagate the result type that we wrote earlier. | 208 (** The function [proj1_sig] extracts the base value from a subset type. It turns out that we need to include an explicit [return] clause here, since Coq's heuristics are not smart enough to propagate the result type that we wrote earlier. |
206 | 209 |
207 By now, the reader is probably ready to believe that the new [pred_strong] leads to the same OCaml code as we have seen several times so far, and Coq does not disappoint. *) | 210 By now, the reader is probably ready to believe that the new [pred_strong] leads to the same OCaml code as we have seen several times so far, and Coq does not disappoint. *) |
208 | 211 |
328 Eval compute in pred_strong6 two_gt0. | 331 Eval compute in pred_strong6 two_gt0. |
329 (** %\vspace{-.15in}% [[ | 332 (** %\vspace{-.15in}% [[ |
330 = [1] | 333 = [1] |
331 : {m : nat | 2 = S m} | 334 : {m : nat | 2 = S m} |
332 | 335 |
333 ]] *) | 336 ]] |
337 *) | |
334 | 338 |
335 | 339 |
336 (** * Decidable Proposition Types *) | 340 (** * Decidable Proposition Types *) |
337 | 341 |
338 (** There is another type in the standard library which captures the idea of program values that indicate which of two propositions is true. *) | 342 (** There is another type in the standard library which captures the idea of program values that indicate which of two propositions is true. *) |
368 Eval compute in eq_nat_dec 2 2. | 372 Eval compute in eq_nat_dec 2 2. |
369 (** %\vspace{-.15in}% [[ | 373 (** %\vspace{-.15in}% [[ |
370 = Yes | 374 = Yes |
371 : {2 = 2} + {2 <> 2} | 375 : {2 = 2} + {2 <> 2} |
372 | 376 |
373 ]] *) | 377 ]] |
378 *) | |
374 | 379 |
375 Eval compute in eq_nat_dec 2 3. | 380 Eval compute in eq_nat_dec 2 3. |
376 (** %\vspace{-.15in}% [[ | 381 (** %\vspace{-.15in}% [[ |
377 = No | 382 = No |
378 : {2 = 2} + {2 <> 2} | 383 : {2 = 2} + {2 <> 2} |
379 | 384 |
380 ]] *) | 385 ]] |
386 *) | |
381 | 387 |
382 (** Our definition extracts to reasonable OCaml code. *) | 388 (** Our definition extracts to reasonable OCaml code. *) |
383 | 389 |
384 Extraction eq_nat_dec. | 390 Extraction eq_nat_dec. |
385 | 391 |
473 Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil). | 479 Eval compute in In_dec eq_nat_dec 2 (1 :: 2 :: nil). |
474 (** %\vspace{-.15in}% [[ | 480 (** %\vspace{-.15in}% [[ |
475 = Yes | 481 = Yes |
476 : {In 2 (1 :: 2 :: nil)} + {~ In 2 (1 :: 2 :: nil)} | 482 : {In 2 (1 :: 2 :: nil)} + {~ In 2 (1 :: 2 :: nil)} |
477 | 483 |
478 ]] *) | 484 ]] |
485 *) | |
479 | 486 |
480 Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil). | 487 Eval compute in In_dec eq_nat_dec 3 (1 :: 2 :: nil). |
481 (** %\vspace{-.15in}% [[ | 488 (** %\vspace{-.15in}% [[ |
482 = No | 489 = No |
483 : {In 3 (1 :: 2 :: nil)} + {~ In 3 (1 :: 2 :: nil)} | 490 : {In 3 (1 :: 2 :: nil)} + {~ In 3 (1 :: 2 :: nil)} |
484 | 491 |
485 ]] *) | 492 ]] |
493 *) | |
486 | 494 |
487 (** [In_dec] has a reasonable extraction to OCaml. *) | 495 (** [In_dec] has a reasonable extraction to OCaml. *) |
488 | 496 |
489 Extraction In_dec. | 497 Extraction In_dec. |
490 (* end thide *) | 498 (* end thide *) |
539 Eval compute in pred_strong7 2. | 547 Eval compute in pred_strong7 2. |
540 (** %\vspace{-.15in}% [[ | 548 (** %\vspace{-.15in}% [[ |
541 = [[1]] | 549 = [[1]] |
542 : {{m | 2 = S m}} | 550 : {{m | 2 = S m}} |
543 | 551 |
544 ]] *) | 552 ]] |
553 *) | |
545 | 554 |
546 Eval compute in pred_strong7 0. | 555 Eval compute in pred_strong7 0. |
547 (** %\vspace{-.15in}% [[ | 556 (** %\vspace{-.15in}% [[ |
548 = ?? | 557 = ?? |
549 : {{m | 0 = S m}} | 558 : {{m | 0 = S m}} |
556 (** %\vspace{-.15in}% [[ | 565 (** %\vspace{-.15in}% [[ |
557 Inductive sumor (A : Type) (B : Prop) : Type := | 566 Inductive sumor (A : Type) (B : Prop) : Type := |
558 inleft : A -> A + {B} | inright : B -> A + {B} | 567 inleft : A -> A + {B} | inright : B -> A + {B} |
559 For inleft: Argument A is implicit | 568 For inleft: Argument A is implicit |
560 For inright: Argument B is implicit | 569 For inright: Argument B is implicit |
561 ]] *) | 570 ]] |
571 *) | |
562 | 572 |
563 (** We add notations for easy use of the [sumor] constructors. The second notation is specialized to [sumor]s whose [A] parameters are instantiated with regular subset types, since this is how we will use [sumor] below. *) | 573 (** We add notations for easy use of the [sumor] constructors. The second notation is specialized to [sumor]s whose [A] parameters are instantiated with regular subset types, since this is how we will use [sumor] below. *) |
564 | 574 |
565 Notation "!!" := (inright _ _). | 575 Notation "!!" := (inright _ _). |
566 Notation "[[[ x ]]]" := (inleft _ [x]). | 576 Notation "[[[ x ]]]" := (inleft _ [x]). |
578 Eval compute in pred_strong8 2. | 588 Eval compute in pred_strong8 2. |
579 (** %\vspace{-.15in}% [[ | 589 (** %\vspace{-.15in}% [[ |
580 = [[[1]]] | 590 = [[[1]]] |
581 : {m : nat | 2 = S m} + {2 = 0} | 591 : {m : nat | 2 = S m} + {2 = 0} |
582 | 592 |
583 ]] *) | 593 ]] |
594 *) | |
584 | 595 |
585 Eval compute in pred_strong8 0. | 596 Eval compute in pred_strong8 0. |
586 (** %\vspace{-.15in}% [[ | 597 (** %\vspace{-.15in}% [[ |
587 = !! | 598 = !! |
588 : {m : nat | 0 = S m} + {0 = 0} | 599 : {m : nat | 0 = S m} + {0 = 0} |
589 | 600 |
590 ]] *) | 601 ]] |
602 *) | |
591 | 603 |
592 | 604 |
593 (** * Monadic Notations *) | 605 (** * Monadic Notations *) |
594 | 606 |
595 (** We can treat [maybe] like a monad, in the same way that the Haskell [Maybe] type is interpreted as a failure monad. Our [maybe] has the wrong type to be a literal monad, but a %``%#"#bind#"#%''%-like notation will still be helpful. *) | 607 (** We can treat [maybe] like a monad, in the same way that the Haskell [Maybe] type is interpreted as a failure monad. Our [maybe] has the wrong type to be a literal monad, but a %``%#"#bind#"#%''%-like notation will still be helpful. *) |
704 | 716 |
705 Eval simpl in typeCheck (Nat 0). | 717 Eval simpl in typeCheck (Nat 0). |
706 (** %\vspace{-.15in}% [[ | 718 (** %\vspace{-.15in}% [[ |
707 = [[TNat]] | 719 = [[TNat]] |
708 : {{t | hasType (Nat 0) t}} | 720 : {{t | hasType (Nat 0) t}} |
709 ]] *) | 721 ]] |
722 *) | |
710 | 723 |
711 Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)). | 724 Eval simpl in typeCheck (Plus (Nat 1) (Nat 2)). |
712 (** %\vspace{-.15in}% [[ | 725 (** %\vspace{-.15in}% [[ |
713 = [[TNat]] | 726 = [[TNat]] |
714 : {{t | hasType (Plus (Nat 1) (Nat 2)) t}} | 727 : {{t | hasType (Plus (Nat 1) (Nat 2)) t}} |
715 ]] *) | 728 ]] |
729 *) | |
716 | 730 |
717 Eval simpl in typeCheck (Plus (Nat 1) (Bool false)). | 731 Eval simpl in typeCheck (Plus (Nat 1) (Bool false)). |
718 (** %\vspace{-.15in}% [[ | 732 (** %\vspace{-.15in}% [[ |
719 = ?? | 733 = ?? |
720 : {{t | hasType (Plus (Nat 1) (Bool false)) t}} | 734 : {{t | hasType (Plus (Nat 1) (Bool false)) t}} |
721 ]] *) | 735 ]] |
736 *) | |
722 | 737 |
723 (** The type-checker also extracts to some reasonable OCaml code. *) | 738 (** The type-checker also extracts to some reasonable OCaml code. *) |
724 | 739 |
725 Extraction typeCheck. | 740 Extraction typeCheck. |
726 | 741 |
854 Eval simpl in typeCheck' (Nat 0). | 869 Eval simpl in typeCheck' (Nat 0). |
855 (** %\vspace{-.15in}% [[ | 870 (** %\vspace{-.15in}% [[ |
856 = [[[TNat]]] | 871 = [[[TNat]]] |
857 : {t : type | hasType (Nat 0) t} + | 872 : {t : type | hasType (Nat 0) t} + |
858 {(forall t : type, ~ hasType (Nat 0) t)} | 873 {(forall t : type, ~ hasType (Nat 0) t)} |
859 ]] *) | 874 ]] |
875 *) | |
860 | 876 |
861 Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)). | 877 Eval simpl in typeCheck' (Plus (Nat 1) (Nat 2)). |
862 (** %\vspace{-.15in}% [[ | 878 (** %\vspace{-.15in}% [[ |
863 = [[[TNat]]] | 879 = [[[TNat]]] |
864 : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} + | 880 : {t : type | hasType (Plus (Nat 1) (Nat 2)) t} + |
865 {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)} | 881 {(forall t : type, ~ hasType (Plus (Nat 1) (Nat 2)) t)} |
866 ]] *) | 882 ]] |
883 *) | |
867 | 884 |
868 Eval simpl in typeCheck' (Plus (Nat 1) (Bool false)). | 885 Eval simpl in typeCheck' (Plus (Nat 1) (Bool false)). |
869 (** %\vspace{-.15in}% [[ | 886 (** %\vspace{-.15in}% [[ |
870 = !! | 887 = !! |
871 : {t : type | hasType (Plus (Nat 1) (Bool false)) t} + | 888 : {t : type | hasType (Plus (Nat 1) (Bool false)) t} + |
872 {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)} | 889 {(forall t : type, ~ hasType (Plus (Nat 1) (Bool false)) t)} |
873 ]] *) | 890 ]] |
891 *) | |
874 | 892 |
875 | 893 |
876 (** * Exercises *) | 894 (** * Exercises *) |
877 | 895 |
878 (** All of the notations defined in this chapter, plus some extras, are available for import from the module [MoreSpecif] of the book source. | 896 (** All of the notations defined in this chapter, plus some extras, are available for import from the module [MoreSpecif] of the book source. |