Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. Require Import List.
  2. Require Import ZArith.
  3.  
  4.  
  5. Inductive E : Set -> Type :=
  6.  
  7. | EZ : Z -> E Z
  8. | Eadd : E Z -> E Z -> E Z
  9. | Emul : E Z -> E Z -> E Z
  10. | Esub : E Z -> E Z -> E Z
  11. | Ecmp : E Z -> E Z -> E bool
  12.  
  13. | Ebool : bool -> E bool
  14. | Enot : E bool -> E bool
  15. | Eand : E bool -> E bool -> E bool
  16. | Eor : E bool -> E bool -> E bool
  17. | Eif : forall {a : Set}, E bool -> E a -> E a -> E a
  18. .
  19.  
  20. SearchAbout Z.
  21.  
  22. Fixpoint Einterpret {t} (e : E t) : t :=
  23. match e with
  24. | EZ n => n
  25. | Eadd p q => (Einterpret p + Einterpret q)%Z
  26. | Emul p q => (Einterpret p * Einterpret q)%Z
  27. | Esub p q => (Einterpret p - Einterpret q)%Z
  28. | Ecmp p q => if Z_zerop (Einterpret p - Einterpret q) then true else false
  29. | Ebool b => b
  30. | Enot b => negb (Einterpret b)
  31. | Eand p q => andb (Einterpret p) (Einterpret q)
  32. | Eor p q => andb (Einterpret p) (Einterpret q)
  33. | Eif _ b m n => if (Einterpret b) then (Einterpret m) else (Einterpret n)
  34. end.
  35.  
  36.  
  37.  
  38.  
  39.  
  40. Inductive etype : Set :=
  41. | etype_bool
  42. | etype_z.
  43.  
  44. Definition ev_etype e :=
  45. match e with
  46. | etype_bool => bool
  47. | etype_z => Z
  48. end.
  49.  
  50.  
  51. Print eq_rect.
  52. Check eq_rect.
  53.  
  54. Definition equal_at_etype t (a : option (list Z)) : ev_etype t -> Prop :=
  55. match a with
  56. | Some (x :: _) => match t with
  57. | etype_bool => fun b => (if Z_zerop x then true else false) = b
  58. | etype_z => fun b => x = b
  59. end
  60. | _ => fun _ => False
  61. end.
  62.  
  63. Fixpoint etype_of {T} (e : E T) : etype :=
  64. match e with
  65. | EZ n => etype_z
  66. | Eadd p q => etype_z
  67. | Emul p q => etype_z
  68. | Esub p q => etype_z
  69. | Ecmp p q => etype_bool
  70. | Ebool b => etype_bool
  71. | Enot b => etype_bool
  72. | Eand p q => etype_bool
  73. | Eor p q => etype_bool
  74. | Eif _ b m n => etype_of m
  75. end.
  76.  
  77. Theorem ev_etype_of {T} (e : E T) : T = ev_etype (etype_of e).
  78. induction e; try reflexivity.
  79. simpl; assumption.
  80. Defined.
  81.  
  82. Definition Einterpret'' {T} (e : E T) : ev_etype (etype_of e).
  83. rewrite <- ev_etype_of.
  84. exact (Einterpret e).
  85. Defined.
  86.  
  87. Definition Einterpret' {T} (e : E T) : etype * Z.
  88. generalize (Einterpret'' e).
  89. destruct (etype_of e); simpl.
  90. refine (fun b => (etype_bool, if b then 1%Z else 0%Z)).
  91. refine (fun z => (etype_z, z)).
  92. Defined.
  93.  
  94.  
  95. Theorem etype_inversion (e : E Z) : etype_of e = etype_z.
  96. Proof.
  97.  
  98. Lemma etype_inversion' {T} (e : E T) : T = Z -> etype_of e = etype_z.
  99.  
  100.  
  101. Lemma bool_ne_Z : bool <> Z.
  102. Proof.
  103. intro.
  104. assert (exists a : Z, exists b, exists c,
  105. a <> b /\ a <> c /\ b <> c).
  106. exists (0%Z).
  107. exists (1%Z).
  108. exists (2%Z).
  109. omega.
  110. rewrite <- H in H0.
  111. repeat destruct H0.
  112. destruct H1.
  113. destruct x; destruct x0; destruct x1; intuition.
  114. Qed.
  115.  
  116.  
  117. induction e; simpl; intuition;
  118. elimtype False; apply bool_ne_Z; trivial.
  119.  
  120. Qed.
  121.  
  122. apply etype_inversion'.
  123. reflexivity.
  124.  
  125. Qed.
  126.  
  127. Inductive A : Set :=
  128. | Apush : Z -> A
  129. | Apop
  130. | Aadd
  131. | Asub
  132. | Amul
  133. | Askip : nat -> A
  134. | Askipz : nat -> A.
  135.  
  136. Require Import Recdef.
  137. Require Import Omega.
  138.  
  139. Fixpoint programLength (program : list A) : nat :=
  140. match program with
  141. | Askip n :: program => 1 + n + programLength program
  142. | Askipz n :: program => 1 + n + programLength program
  143. | _ :: program => 1 + programLength program
  144. | _ => 0
  145. end.
  146.  
  147. Function Ainterpret (stack : list (etype * Z)) (program : list A) {measure programLength program} : option (list (etype * Z)) :=
  148. match program with
  149. | nil => Some stack
  150. | instruction :: program =>
  151. match instruction with
  152. | Apush n => Ainterpret ((etype_z, n) :: stack) program
  153. | Apop => match stack with
  154. | nil => None
  155. | _ :: stack => Ainterpret stack program
  156. end
  157. | Aadd => match stack with
  158. | (_,x) :: (_,y) :: stack => Ainterpret ((etype_z, (x + y)%Z) :: stack) program
  159. | _ => None
  160. end
  161. | Asub => match stack with
  162. | (_,x) :: (_,y) :: stack => Ainterpret ((etype_z, (x - y)%Z) :: stack) program
  163. | _ => None
  164. end
  165. | Amul => match stack with
  166. | (_,x) :: (_,y) :: stack => Ainterpret ((etype_z, (x * y)%Z) :: stack) program
  167. | _ => None
  168. end
  169. | Askip n => match n with
  170. | 0 => Ainterpret stack program
  171. | S n => Ainterpret stack (Askip n :: program)
  172. end
  173. | Askipz n => match stack with
  174. | nil => None
  175. | (_,top) :: stack' =>
  176. if Z_zerop top
  177. then Ainterpret stack' program
  178. else match n with
  179. | 0 => Ainterpret stack' program
  180. | S n => Ainterpret stack' (Askipz n :: program)
  181. end
  182. end
  183. end
  184. end.
  185.  
  186. intros; simpl.
  187. omega.
  188.  
  189. intros; simpl.
  190. omega.
  191.  
  192. intros; simpl.
  193. omega.
  194.  
  195. intros; simpl.
  196. omega.
  197.  
  198. intros; simpl.
  199. omega.
  200.  
  201. intros; simpl.
  202. omega.
  203.  
  204. intros; simpl.
  205. omega.
  206.  
  207. intros; simpl.
  208. omega.
  209.  
  210. intros; simpl.
  211. omega.
  212.  
  213. intros; simpl.
  214. omega.
  215.  
  216. Defined.
  217.  
  218.  
  219. Print Ainterpret_equation.
  220.  
  221.  
  222. Fixpoint compile {t} (e : E t) (l : list A) : list A :=
  223. match e with
  224. | EZ z => Apush z :: l
  225. | Eadd p q => compile p (compile q (Aadd :: l))
  226. | Esub p q => compile p (compile q (Asub :: l))
  227. | Emul p q => compile p (compile q (Amul :: l))
  228. | Ecmp p q => compile p (compile q (Asub :: l))
  229. | Ebool b => match b with
  230. | true => Apush 1 :: l
  231. | false => Apush 0 :: l
  232. end
  233. | Enot b => Apush 1 :: (compile b (Asub :: l))
  234. | Eand p q => compile p (compile q (Amul :: l))
  235. | Eor p q => compile p (compile q (Aadd :: l))
  236. | Eif _ b p q =>
  237. let pl := length (compile p nil) in
  238. let ql := length (compile q nil) in
  239. compile b (Askipz (1 + pl) :: compile p (Askip ql :: compile q l))
  240. end.
  241.  
  242. Definition stackTop {t} (s : option (list t)) : option t :=
  243. match s with
  244. | None => None
  245. | Some nil => None
  246. | Some (x :: xs) => Some x
  247. end.
  248.  
  249.  
  250. Fixpoint etype_eq_list (a : (list (etype * Z))) (b : (list (etype * Z))) : Prop :=
  251. match a, b with
  252. | nil, nil => True
  253. | ((t1,v1) :: a), ((t2,v2) :: b) => match t1, t2 with
  254. | etype_bool, etype_bool => (if Z_zerop v1 then true else false) = (if Z_zerop v2 then true else false)
  255. | etype_z, etype_z => v1 = v2
  256. | _, _ => False
  257. end /\ etype_eq_list ( a) ( b)
  258. | _, _ => False
  259. end.
  260.  
  261. Theorem etype_eq_list_refl : forall a, etype_eq_list a a.
  262. Admitted.
  263. Theorem etype_eq_list_symm : forall a b, etype_eq_list a b -> etype_eq_list b a.
  264. Admitted.
  265. Theorem etype_eq_list_trans : forall a b c, etype_eq_list a b -> etype_eq_list b c -> etype_eq_list a c.
  266. Admitted.
  267.  
  268. Require Import Setoid.
  269.  
  270. Program Instance etype_eq_list_equivalence : Equivalence etype_eq_list.
  271. Obligation 1.
  272. unfold Reflexive.
  273. apply etype_eq_list_refl.
  274. Defined.
  275. Obligation 2.
  276. unfold Symmetric.
  277. apply etype_eq_list_symm.
  278. Defined.
  279. Obligation 3.
  280. unfold Transitive.
  281. apply etype_eq_list_trans.
  282. Defined.
  283.  
  284. Theorem correct {T} : forall (e : E T) s l,
  285. etype_eq_list
  286. (option_rec (fun _ => list (etype * Z)) (fun i => i) nil
  287. (Ainterpret s (compile e l)))
  288. (option_rec (fun _ => list (etype * Z)) (fun i => i) nil
  289. (Ainterpret (Einterpret' e :: s) l)).
  290. Proof.
  291. induction e; intros; simpl.
  292.  
  293. rewrite Ainterpret_equation.
  294. unfold Einterpret'.
  295. unfold Einterpret''.
  296. simpl.
  297. reflexivity.
  298.  
  299. unfold Einterpret'.
  300. unfold Einterpret''.
  301. simpl.
  302. rewrite IHe1.
  303. rewrite IHe2.
  304. rewrite Ainterpret_equation.
  305. unfold Einterpret'.
  306. unfold Einterpret''.
  307. clear IHe1.
  308. clear IHe2.
  309.  
  310. pose (etype_inversion e2) as e.
  311. rewrite e at 5.
  312.  
  313. rewrite <- (etype_inversion e2) in *.
  314.  
  315. rewrite (etype_inversion e1) in *.
  316. rewrite (etype_inversion e2).
  317.  
  318. assert (etype_of e2 = etype_z).
  319.  
  320.  
  321.  
  322. unfold Einterpret'; simpl.
  323. unfold equal_at_etype; simpl.
  324. simpl in IHe1.
  325.  
  326. Lemma
  327.  
  328.  
  329. simpl.
  330.  
  331. destruct t.
  332. ; simpl in prf; try (elimtype False; discriminate).
  333.  
  334. discriminate.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement