Guest User

Untitled

a guest
May 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. Require Import Coq.Init.Nat.
  2. Require Import Coq.Strings.String.
  3. Require Import Coq.Vectors.VectorDef.
  4.  
  5. Open Scope string_scope.
  6. Open Scope nat_scope.
  7.  
  8.  
  9. (* Traversal definition and methods *)
  10.  
  11. Record result T A B := mkResult
  12. { n : nat
  13. ; apply : (t A n) * (t B n -> T)
  14. }.
  15. Arguments mkResult [T A B].
  16. Arguments n [T A B].
  17. Arguments apply [T A B].
  18.  
  19. Definition traversal S T A B := S -> result T A B.
  20.  
  21. Definition getAll {S T A B} (tr : traversal S T A B) (s : S) : t A (n (tr s)) :=
  22. fst (apply (tr s)).
  23.  
  24. Definition putAll {S T A B} (tr : traversal S T A B) (s : S) : (t B (n (tr s))) -> T :=
  25. snd (apply (tr s)).
  26.  
  27.  
  28. (* Examples *)
  29.  
  30. Inductive person : Type :=
  31. | mkPerson : string -> string -> nat -> person.
  32.  
  33. Definition nameTr : traversal person person string string := (fun s =>
  34. match s with
  35. | mkPerson first last age => mkResult _
  36. (cons _ first _ (cons _ last _ (nil _)),
  37. fun bs => mkPerson (hd bs) (hd (tl bs)) (age))
  38. end).
  39.  
  40. Example get_names :
  41. getAll nameTr (mkPerson "john" "doe" 40) =
  42. cons string "john" 1 (cons string "doe" 0 (nil string)).
  43. Proof. auto. Qed.
  44.  
  45. Example put_names :
  46. putAll nameTr (mkPerson "john" "doe" 40) (cons _ "jane" _ (cons _ "roe" _ (nil _))) =
  47. mkPerson "jane" "roe" 40.
  48. Proof. auto. Qed.
  49.  
  50. Definition vectorTr {A B n} : traversal (t A n) (t B n) A B :=
  51. fun s => mkResult _ (s, id).
  52.  
  53. Example get_as : forall B,
  54. @getAll _ _ _ B vectorTr (cons _ 1 _ (cons _ 2 _ (nil _))) =
  55. cons _ 1 _ (cons _ 2 _ (nil _)).
  56. Proof. auto. Qed.
  57.  
  58. Example set_bs :
  59. putAll vectorTr (cons _ 1 _ (cons _ 2 _ (nil _))) (cons _ "a" _ (cons _ "b" _ (nil _))) =
  60. (cons _ "a" _ (cons _ "b" _ (nil _))).
  61. Proof. auto. Qed.
  62.  
  63. Inductive tree (A : Type) : nat -> Type :=
  64. | leaf : tree A 0
  65. | node {m} : tree A m -> A -> tree A m -> tree A (S m).
  66.  
  67. Require Import ZArith.
  68.  
  69. Example xyz : forall n, pred (2 ^ n) + S (pred (2 ^ n)) = pred (2 ^ S n).
  70. Proof.
  71. intros.
  72. rewrite Nat.succ_pred.
  73. - admit.
  74. - assert (G : forall o p, (exists m n, (o = S m) \/ (p = S n)) -> o + p <> 0).
  75. { admit.
  76. }
  77. destruct n0; simpl; auto.
  78. rewrite <- plus_n_O.
  79. apply G.
  80. induction n0; simpl.
  81. + exists 0, 0. now left.
  82. + rewrite <- plus_n_O.
  83. destruct IHn0, H, H.
  84. * exists x, x0.
  85. Admitted.
  86.  
  87. Fixpoint inorder {A m} (tr : tree A m) : t A (pred (2 ^ m)) :=
  88. match tr with
  89. | leaf _ => nil _
  90. | node _ l a r => append (inorder l) (cons _ a _ (inorder r))
  91. end.
  92.  
  93. Example inorderTr {A B m} : traversal (tree A m) (tree B m) A B := fun s =>
  94. match s with
  95. | leaf _ => mkResult 0 (nil A, fun _ => leaf B)
  96. | node l a r => mkResult _ (inorder (node l a r), )
  97. end.
  98.  
  99. (* Doesn't compile! (which is fine) *)
  100.  
  101. (*
  102. Example put_names :
  103. putAll nameTr (mkPerson "john" "doe" 40) (cons _ "jane" _ (nil _)) =
  104. mkPerson "jane" "roe" 40.
  105.  
  106. Example get_names' :
  107. getAll nameTr (mkPerson "john" "doe" 40) =
  108. (cons string "john" 0 (nil string)).
  109. *)
Add Comment
Please, Sign In to add comment