Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. Add LoadPath "/Users/erutuf/Documents/mit/fiat/src" as Fiat.
  2. Require Import Fiat.Common Fiat.Computation
  3. Fiat.ADTRefinement Fiat.ADTNotation Fiat.ADTRefinement.BuildADTRefinements.
  4. Require Import List Omega.
  5.  
  6. Set Implicit Arguments.
  7. Set Regular Subst Tactic.
  8. Import ListNotations.
  9.  
  10. Inductive removed A : nat -> list A -> list A -> Prop :=
  11. | removed_O : forall a l, removed O (a :: l) l
  12. | removed_Sn : forall n a l l', removed n l l' -> removed (S n) (a :: l) (a :: l').
  13.  
  14. Lemma removed_len : forall A n (l l' : list A), removed n l l' ->
  15. length l = S (length l').
  16. Proof with simpl in *.
  17. induction n; intros...
  18. - inversion H. subst. auto.
  19. - inversion H. subst... f_equal. auto.
  20. Qed.
  21.  
  22. Definition remove_spec A (n : nat)(l : list A) : Comp (list A) :=
  23. { l' | removed n l l' }%comp.
  24.  
  25. Ltac return_impl := repeat
  26. match goal with
  27. | H : forall v, (ret ?x) ↝ v -> _ |- _ => specialize (H _ (@ReturnComputes _ _))
  28. end.
  29. Ltac refines := unfold refine in *; intros; return_impl;
  30. repeat computes_to_econstructor; repeat computes_to_inv; subst.
  31. Ltac monad_simpl := autosetoid_rewrite with refine_monad;
  32. try simplify_with_applied_monad_laws; simpl.
  33.  
  34. Lemma remove_ex A n (l : list A) (H : n < length l) :
  35. {l' | refine (remove_spec n l) (ret l')}.
  36. Proof with simpl in *.
  37. unfold remove_spec. revert l H.
  38. induction n; intros; destruct l; simpl in *; [omega|..].
  39. - exists l. refines. constructor.
  40. - omega.
  41. - assert (n < length l) as H0 by omega.
  42. apply IHn in H0. destruct H0.
  43. exists (a :: x). refines. constructor. auto.
  44. Defined.
  45.  
  46. Definition remove A n (l : list A) (H : n < length l) :=
  47. ` (@remove_ex A n l H).
  48.  
  49. Inductive removed_lst A : nat -> nat -> list (list A) -> list (list A) -> Prop :=
  50. | removed_lst_O : forall m l l' lst, removed m l l' -> removed_lst O m (l :: lst) (l' :: lst)
  51. | removed_lst_Sn : forall n m l lst lst',
  52. removed_lst n m lst lst' -> removed_lst (S n) m (l :: lst) (l :: lst').
  53.  
  54. Inductive lst_len A : nat -> list (list A) -> list (list A) -> Prop :=
  55. | lst_len_O : forall l l' lst lst',
  56. length l = S (length l') ->
  57. Forall2 (fun l1 l2 => length l1 = length l2) lst lst' ->
  58. lst_len O (l :: lst) (l' :: lst')
  59. | lst_len_Sn : forall n l l' lst lst',
  60. lst_len n lst lst' ->
  61. length l = length l' ->
  62. lst_len (S n) (l :: lst) (l' :: lst').
  63.  
  64. Lemma removed_lst_len : forall A n m (lst lst' : list (list A)),
  65. removed_lst n m lst lst' -> lst_len n lst lst'.
  66. Proof with simpl in *.
  67. induction n; intros...
  68. - inversion H; subst; constructor.
  69. + eauto using removed_len.
  70. + clear H H0. induction lst0; auto.
  71. - inversion H; subst. constructor; eauto.
  72. Qed.
  73.  
  74. Definition remove_lst_spec A n m (lst : list (list A)) : Comp (list (list A)) :=
  75. { lst' | removed_lst n m lst lst' }%comp.
  76.  
  77. Inductive cond A : nat -> nat -> list (list A) -> Type :=
  78. | cond_O : forall m l lst, m < length l -> cond O m (l :: lst)
  79. | cond_Sn : forall n m l lst, cond n m lst -> cond (S n) m (l :: lst).
  80.  
  81. Definition remove_lst_O A m l (lst : list (list A)) :=
  82. l' <- remove_spec m l;
  83. ret (l' :: lst).
  84.  
  85. Lemma remove_lst_O_ex : forall A m l (lst : list (list A)),
  86. refine (remove_lst_spec O m (l :: lst)) (remove_lst_O m l lst).
  87. Proof with simpl in *.
  88. intros. unfold remove_lst_O. refines.
  89. inversion H; subst; repeat constructor; auto.
  90. Qed.
  91.  
  92. Definition remove_lst_ex A n m (lst : list (list A)) (H : cond n m lst) :
  93. { lst' | refine (remove_lst_spec n m lst) (ret lst') }.
  94. Proof with simpl in *.
  95. revert m lst H. induction n; intros; inversion H; subst...
  96. - destruct (@remove_ex A m l H0).
  97. eexists. rewrite remove_lst_O_ex. unfold remove_lst_O.
  98. rewrite r. monad_simpl.
  99. finish honing.
  100. - apply IHn in X. destruct X.
  101. exists (l :: x). refines. constructor. auto.
  102. Defined.
  103.  
  104. Definition remove_lst A n m (lst : list (list A)) (H : cond n m lst) :=
  105. ` (@remove_lst_ex A n m lst H).
  106.  
  107. Definition hoge := [[1; 2; 3]; [2; 3]].
  108. Lemma hoge_cond : cond 0 1 hoge.
  109. Proof.
  110. unfold hoge. repeat constructor.
  111. Defined.
  112.  
  113. Eval compute in (@remove_lst nat 0 1 hoge hoge_cond).
  114.  
  115. Definition hoge_cond2 : cond 1 0 hoge.
  116. Proof.
  117. unfold hoge. repeat constructor.
  118. Defined.
  119.  
  120. Eval compute in (@remove_lst nat 1 0 hoge hoge_cond2).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement