Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. Require Import ssreflect.
  2. From mathcomp Require Import all_ssreflect.
  3. Require Import Extraction.
  4.  
  5.  
  6. (* 練習問題5.1 *)
  7.  
  8. Section sort.
  9. Variable A : eqType.
  10. Variable le : A -> A -> bool.
  11. Variable le_trans: forall x y z, le x y -> le y z -> le x z.
  12. Variable le_total: forall x y, ~~ le x y -> le y x.
  13. Fixpoint insert a l := match l with
  14. | nil => (a :: nil)
  15. | b :: l' => if le a b
  16. then a :: l
  17. else b :: insert a l'
  18. end.
  19. Fixpoint isort l := if l is a :: l' then insert a (isort l') else nil.
  20.  
  21. Fixpoint sorted l := (* allを使ってbool上の述語を定義する *) if l is a :: l'
  22. then all (le a) l' && sorted l'
  23. else true.
  24.  
  25. Lemma le_seq_insert a b l : le a b -> all (le a) l -> all (le a) (insert b l).
  26. Proof. elim: l => /= [-> // | c l IH].
  27. move=> leab. move => /andP [leac leal].
  28. case: ifPn => lebc /=.
  29. - rewrite leab leac. done.
  30. - by rewrite leac IH.
  31. Qed.
  32. Lemma le_seq_trans a b l : le a b -> all (le b) l -> all (le a) l.
  33. Proof.
  34. move=> leab /allP lebl. apply/allP => x Hx. by apply/le_trans/lebl.
  35. Qed.
  36. Theorem insert_ok a l : sorted l -> sorted (insert a l).
  37. Proof.
  38. elim: l.
  39. + done.
  40. + move => b l'.
  41. move => IH.
  42. simpl.
  43. case: ifPn.
  44. move => leab.
  45. simpl.
  46. rewrite leab andTb.
  47. move => /andP [allbl' sortedl'].
  48. rewrite allbl' sortedl' !andbT.
  49. apply (le_seq_trans a b l').
  50. - assumption.
  51. - assumption.
  52. + move => leba /andP [lebl' sortedl'].
  53. rewrite /=.
  54. rewrite le_seq_insert.
  55. rewrite andTb.
  56. by apply IH.
  57. by apply le_total.
  58. assumption.
  59. Qed.
  60. Theorem isort_ok l : sorted (isort l).
  61. Proof.
  62. elim: l.
  63. + done.
  64. + move => a l' IH.
  65. simpl.
  66. apply insert_ok.
  67. assumption.
  68. Qed.
  69.  
  70.  
  71. (* perm_eq が seq で定義されているが補題だけを使う *)
  72. Theorem insert_perm l a : perm_eq (a :: l) (insert a l).
  73. Proof.
  74. elim: l => //= b l pal.
  75. case: ifPn => //= leab. by rewrite (perm_catCA [:: a] [:: b]) perm_cons.
  76. Qed.
  77. Check perm_catCA.
  78.  
  79. (*
  80. perm_catCA
  81. : forall (T : eqType) (s1 s2 s3 : seq T), perm_eql (s1 ++ s2 ++ s3) (s2 ++ s1 ++ s3)
  82. *)
  83. (* perm_eq_trans : forall (T : eqType), transitive (seq T) perm_eq *)
  84. Search "perm_eq".
  85. Check perm_eq_sym.
  86. Check perm_cons.
  87. Theorem isort_perm l : perm_eq l (isort l).
  88. Proof.
  89. elim: l.
  90. + done.
  91. + move => a l' IH.
  92. simpl.
  93. rewrite perm_eq_sym.
  94. apply: perm_eq_trans.
  95. rewrite perm_eq_sym.
  96. apply insert_perm.
  97. rewrite perm_cons.
  98. by rewrite perm_eq_sym.
  99.  
  100. Qed.
  101.  
  102. End sort.
  103.  
  104. Check isort.
  105.  
  106. Definition isortn : seq nat -> seq nat := isort _ leq.
  107. Definition sortedn := sorted _ leq.
  108. Lemma leq_total a b : ~~ (a <= b) -> b <= a.
  109. rewrite leqNgt.
  110. Search (reflect _ (~~ _)).
  111. move => /negPn a_nleq_b .
  112. rewrite <- ltnS.
  113. apply: leq_trans.
  114. apply a_nleq_b.
  115. rewrite <- addn1.
  116. apply leq_addr.
  117. Qed.
  118. Theorem isortn_ok l : sortedn (isortn l) && perm_eq l (isortn l).
  119. Proof.
  120. rewrite isort_perm andbT.
  121. rewrite /sortedn.
  122. rewrite isort_ok.
  123. reflexivity.
  124. move => x y z.
  125. apply: leq_trans.
  126. move => x y.
  127. apply leq_total.
  128. Qed.
  129. Require Import Extraction. Extraction "isort.ml" isortn.
  130. Extraction leq.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement