Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. Require Import List.
  2. Import ListNotations.
  3. Require Import Nat.
  4. Require Import Bool.
  5.  
  6. Fixpoint insertAt {X: Type} (x: X) (n: nat) (l: list X): list X :=
  7. match l with
  8. | nil => if n =? 0 then [x] else nil
  9. | h :: t => if n =? 0 then x :: h :: t else h :: (insertAt x (n-1) t)
  10. end.
  11.  
  12. Fixpoint range (n: nat): list nat :=
  13. match n with
  14. | O => [0]
  15. | S n => (range n) ++ [S n]
  16. end.
  17.  
  18. Fixpoint permutations {X: Type} (l: list X) : list (list X) :=
  19. match l with
  20. | nil => [nil]
  21. | h :: t => flat_map (fun l =>
  22. map (fun n => insertAt h n l) (range (length l))
  23. ) (permutations t)
  24. end.
  25.  
  26. Class Ord A := {
  27. lt : A -> A -> bool
  28. }.
  29. Instance natOrd : Ord nat := { lt := Nat.ltb }.
  30. Fixpoint listLt {X} {H: Ord X} (l1 l2: list X): bool :=
  31. match l1, l2 with
  32. | nil, _ => true
  33. | _, nil => false
  34. | (h1 :: t1), (h2 :: t2) => if @lt X H h1 h2
  35. then true
  36. else if @lt X H h2 h1
  37. then false
  38. else @listLt X H H t1 t2
  39. end.
  40.  
  41. Instance listOrd {X} `{H: Ord X}: Ord (list X) := {
  42. lt := @listLt X H H
  43. }.
  44.  
  45. Fixpoint insertSorted {X: Type} `{Ord X} (x: X) (l: list X): list X :=
  46. match l with
  47. | nil => [x]
  48. | h :: t => if lt x h then x :: h :: t else h :: (insertSorted x t)
  49. end.
  50.  
  51. Fixpoint badSort {X} `{_: Ord X} (k: nat) (l: list X): list X :=
  52. match k with
  53. | O => fold_right insertSorted [] l
  54. | S n => hd nil (@badSort (list X) listOrd n (permutations l))
  55. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement