Advertisement
Nolrai

insert_sort

May 26th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import data.list
  2. import data.list.sorted
  3.  
  4. open list
  5. open list.sorted
  6. open has_le
  7.  
  8. namespace list
  9.  
  10. inductive In (x : A) : list A -> Prop :=
  11. | base {} : forall {l}, In x (x :: l)
  12. | step {} : forall {l} {y}, In x l -> In x (y :: l)
  13.  
  14. definition subset l1 l2 := forall x, In x l1 -> In x l2
  15. definition perm l1 l2 := and (subset l1 l2) (subset l2 l1)
  16. local notation `~=`:60 := perm
  17.  
  18. lemma perm_sym [symm] : forall l l', l ~= l' -> l' ~= l :=
  19. begin
  20. now
  21. end
  22.  
  23. lemma hd_rel_perm : forall R y l1 l2
  24. , l1 ~= l2
  25. -> hd_rel R y l1
  26. -> hd_rel R y l2 :=
  27. begin
  28. now
  29. end
  30.  
  31. lemma sorted_to_hd
  32. : forall x y l
  33. , sorted l
  34. -> not (x <= y)
  35. -> hd_rel le y (x l) :=
  36. begin
  37. now
  38. end
  39.  
  40. end list
  41.  
  42.  
  43. section insert_sort
  44.  
  45. parameter {A : Type}
  46. infix `<=` := has_le.le
  47.  
  48. definition insert
  49. [has_le A]
  50. [forall x y: A, decidable (x <= y)]
  51. : A -> list A -> list A
  52. | x [] := [x]
  53. | x (y :: ys) :=
  54. if x <= y
  55. then x :: (y :: ys)
  56. else y :: insert x ys
  57.  
  58. definition insert_sort
  59. [has_le A]
  60. [forall x y: A, decidable (x <= y)]
  61. : list A -> list A
  62. | [] := []
  63. | (x :: xs) := insert x (insert_sort xs)
  64.  
  65. print decidable
  66.  
  67. lemma ite_rec
  68. {T} (P : T -> Prop) (t f : T) c [decidable c]
  69. : (c -> P t) -> (not c -> P f) -> P (ite c t f) :=
  70. begin
  71. intros Pt Pf
  72. , cases _inst_1
  73. , all_goals unfold ite
  74. , {apply Pt, assumption}
  75. , {apply Pf, assumption}
  76. , now
  77. end
  78.  
  79. print list.hd_rel
  80.  
  81. lemma insert_step_correct
  82. [has_le A]
  83. [forall x y: A, decidable (x <= y)]
  84. (x : A)
  85. (l : list A)
  86. : sorted le l -> sorted le (insert x l) :=
  87. begin
  88. induction l with y l IH
  89. , all_goals intros S
  90. , all_goals unfold insert
  91. , {repeat constructor}
  92. , apply ite_rec
  93. , all_goals intros H
  94. , { repeat1 constructor
  95. , all_goals assumption
  96. }
  97. , { constructor
  98. , now
  99. }
  100. , now
  101. end
  102.  
  103. end insert_sort
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement