You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day8.v 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. Require Import Coq.ZArith.Int.
  2. Require Import Coq.Lists.ListSet.
  3. Require Import Coq.Vectors.VectorDef.
  4. Require Import Coq.Vectors.Fin.
  5. Require Import Coq.Program.Equality.
  6. Require Import Coq.Logic.Eqdep_dec.
  7. Require Import Coq.Arith.Peano_dec.
  8. Require Import Coq.Program.Wf.
  9. Require Import Lia.
  10. Module DayEight (Import M:Int).
  11. (* We need to coerce natural numbers into integers to add them. *)
  12. Parameter nat_to_t : nat -> t.
  13. (* We need a way to convert integers back into finite sets. *)
  14. Parameter clamp : forall {n}, t -> option (Fin.t n).
  15. Definition fin := Fin.t.
  16. (* The opcode of our instructions. *)
  17. Inductive opcode : Type :=
  18. | add
  19. | nop
  20. | jmp.
  21. (* The result of running a program is either the accumulator
  22. or an infinite loop error. In the latter case, we return the
  23. set of instructions that we tried. *)
  24. Inductive run_result {n : nat} : Type :=
  25. | Ok : t -> run_result
  26. | Fail : set (fin n) -> run_result.
  27. (* A single program state .*)
  28. Definition state n : Type := (fin (S n) * set (fin n) * t).
  29. (* An instruction is a pair of an opcode and an argument. *)
  30. Definition inst : Type := (opcode * t).
  31. (* An input is a bounded list of instructions. *)
  32. Definition input (n : nat) := VectorDef.t inst n.
  33. (* 'indices' represents the list of instruction
  34. addresses, which are used for calculating jumps. *)
  35. Definition indices (n : nat) := VectorDef.t (fin n) n.
  36. (* Change a jump to a nop, or a nop to a jump. *)
  37. Definition swap (i : inst) : inst :=
  38. match i with
  39. | (add, t) => (add, t)
  40. | (nop, t) => (jmp, t)
  41. | (jmp, t) => (nop, t)
  42. end.
  43. Inductive swappable : inst -> Prop :=
  44. | swap_nop : forall t, swappable (nop, t)
  45. | swap_jmp : forall t, swappable (jmp, t).
  46. (* Compute the destination jump index, an integer. *)
  47. Definition jump_t {n} (pc : fin n) (off : t) : t :=
  48. M.add (nat_to_t (proj1_sig (to_nat pc))) off.
  49. (* Compute a destination index that's valid.
  50. Not all inputs are valid, so this may fail. *)
  51. Definition valid_jump_t {n} (pc : fin n) (off : t) : option (fin (S n)) := @clamp (S n) (jump_t pc off).
  52. (* Cast a fin n to a fin (S n). *)
  53. Fixpoint weaken_one {n} (f : fin n) : fin (S n) :=
  54. match f with
  55. | F1 => F1
  56. | FS f' => FS (weaken_one f')
  57. end.
  58. (* Convert a nat to fin. *)
  59. Fixpoint nat_to_fin (n : nat) : fin (S n) :=
  60. match n with
  61. | O => F1
  62. | S n' => FS (nat_to_fin n')
  63. end.
  64. (* A finite natural is either its maximum value (aka nat_to_fin n),
  65. or it's not thatbig, which means it can be cast down to
  66. a fin (pred n). *)
  67. Lemma fin_big_or_small : forall {n} (f : fin (S n)),
  68. (f = nat_to_fin n) \/ (exists (f' : fin n), f = weaken_one f').
  69. Proof.
  70. (* Hey, looks like the creator of Fin provided
  71. us with nice inductive principles. Using Coq's
  72. default `induction` breaks here.
  73. Merci, Pierre! *)
  74. apply Fin.rectS.
  75. - intros n. destruct n.
  76. + left. reflexivity.
  77. + right. exists F1. auto.
  78. - intros n p IH.
  79. destruct IH.
  80. + left. rewrite H. reflexivity.
  81. + right. destruct H as [f' Heq].
  82. exists (FS f'). simpl. rewrite Heq.
  83. reflexivity.
  84. Qed.
  85. (* One modification: we really want to use 'allowed' addresses,
  86. a set that shrinks as the program continues, rather than 'visited'
  87. addresses, a set that increases as the program continues. *)
  88. Inductive step_noswap {n} : inst -> (fin n * t) -> (fin (S n) * t) -> Prop :=
  89. | step_noswap_add : forall pc acc t,
  90. step_noswap (add, t) (pc, acc) (FS pc, M.add acc t)
  91. | step_noswap_nop : forall pc acc t,
  92. step_noswap (nop, t) (pc, acc) (FS pc, acc)
  93. | step_noswap_jmp : forall pc pc' acc t,
  94. valid_jump_t pc t = Some pc' ->
  95. step_noswap (jmp, t) (pc, acc) (pc', acc).
  96. Inductive done {n} : input n -> state n -> Prop :=
  97. | done_prog : forall inp v acc, done inp (nat_to_fin n, v, acc).
  98. Inductive stuck {n} : input n -> state n -> Prop :=
  99. | stuck_prog : forall inp pc' v acc,
  100. ~ set_In pc' v -> stuck inp (weaken_one pc', v, acc).
  101. Inductive run_noswap {n} : input n -> state n -> state n -> Prop :=
  102. | run_noswap_ok : forall inp st, done inp st -> run_noswap inp st st
  103. | run_noswap_fail : forall inp st, stuck inp st -> run_noswap inp st st
  104. | run_noswap_trans : forall inp pc pc' v acc acc' st',
  105. set_In pc v ->
  106. step_noswap (nth inp pc) (pc, acc) (pc', acc') ->
  107. run_noswap inp (pc', set_remove Fin.eq_dec pc v, acc') st' ->
  108. run_noswap inp (weaken_one pc, v, acc) st'.
  109. Inductive run_swap {n} : input n -> state n -> state n -> Prop :=
  110. | run_swap_normal : forall inp pc pc' v acc acc' st',
  111. set_In pc v ->
  112. ~ swappable (nth inp pc) ->
  113. step_noswap (nth inp pc) (pc, acc) (pc', acc') ->
  114. run_swap inp (pc', set_remove Fin.eq_dec pc v, acc') st' ->
  115. run_swap inp (weaken_one pc, v, acc) st'
  116. | run_swap_swapped_ok : forall inp pc pc' v acc acc' st',
  117. set_In pc v ->
  118. swappable (nth inp pc) ->
  119. step_noswap (swap (nth inp pc)) (pc, acc) (pc', acc') ->
  120. run_noswap inp (pc', set_remove Fin.eq_dec pc v, acc') st' ->
  121. done inp st' ->
  122. run_swap inp (weaken_one pc, v, acc) st'
  123. | run_swap_swapped_next : forall inp pc pc'w pc'n v acc acc'w acc'n st'w st'n,
  124. set_In pc v ->
  125. swappable (nth inp pc) ->
  126. step_noswap (swap (nth inp pc)) (pc, acc) (pc'w, acc'w) ->
  127. run_noswap inp (pc'w, set_remove Fin.eq_dec pc v, acc'w) st'w ->
  128. stuck inp st'w ->
  129. step_noswap (nth inp pc) (pc, acc) (pc'n, acc'n) ->
  130. run_swap inp (pc'n, set_remove Fin.eq_dec pc v, acc'n) st'n ->
  131. run_swap inp (weaken_one pc, v, acc) st'n.
  132. Inductive valid_inst {n} : inst -> fin n -> Prop :=
  133. | valid_inst_add : forall t f, valid_inst (add, t) f
  134. | valid_inst_nop : forall t f f',
  135. valid_jump_t f t = Some f' -> valid_inst (nop, t) f
  136. | valid_inst_jmp : forall t f f',
  137. valid_jump_t f t = Some f' -> valid_inst (jmp, t) f.
  138. (* An input is valid if all its instructions are valid. *)
  139. Definition valid_input {n} (inp : input n) : Prop := forall (pc : fin n),
  140. valid_inst (nth inp pc) pc.
  141. Section ValidInput.
  142. Variable n : nat.
  143. Variable inp : input n.
  144. Hypothesis Hv : valid_input inp.
  145. Theorem valid_input_can_step : forall pc acc, exists pc' acc',
  146. step_noswap (nth inp pc) (pc, acc) (pc', acc').
  147. Proof.
  148. intros pc acc.
  149. destruct (nth inp pc) eqn:Hop.
  150. destruct o.
  151. - exists (FS pc). exists (M.add acc t0). apply step_noswap_add.
  152. - exists (FS pc). exists acc. eapply step_noswap_nop.
  153. - specialize (Hv pc). rewrite Hop in Hv. inversion Hv; subst.
  154. exists f'. exists acc. eapply step_noswap_jmp. apply H0.
  155. Qed.
  156. (* A program is either done, stuck (at an invalid/visited address), or can step. *)
  157. Theorem valid_input_progress : forall pc v acc,
  158. (pc = nat_to_fin n /\ done inp (pc, v, acc)) \/
  159. (exists pcs, pc = weaken_one pcs /\
  160. ((~ set_In pcs v /\ stuck inp (pc, v, acc)) \/
  161. (exists pc' acc', set_In pcs v /\
  162. step_noswap (nth inp pcs) (pcs, acc) (pc', acc')))).
  163. Proof.
  164. intros pc v acc.
  165. (* Have we reached the end? *)
  166. destruct (fin_big_or_small pc).
  167. (* We're at the end, so we're done. *)
  168. left. rewrite H. split. reflexivity. apply done_prog.
  169. (* We're not at the end. *)
  170. right. destruct H as [pcs H]. exists pcs. rewrite H. split. reflexivity.
  171. (* We're not at the end. Is the PC valid? *)
  172. destruct (set_In_dec Fin.eq_dec pcs v).
  173. - (* It is. *)
  174. right.
  175. destruct (valid_input_can_step pcs acc) as [pc' [acc' Hstep]].
  176. exists pc'; exists acc'; auto.
  177. - (* It is not. *)
  178. left. split; auto. apply stuck_prog; auto.
  179. Qed.
  180. Theorem list_length_induction {X : Type} (P : list X -> Prop) :
  181. (forall l, (forall l', length l' < length l -> P l') -> P l) ->
  182. forall l, P l.
  183. Proof.
  184. intros Hrec.
  185. assert (forall (l l' : list X), length l' <= length l -> P l').
  186. { induction l; intros l' Hlen; apply Hrec; intros l'0 Hlen0.
  187. - simpl in Hlen. lia.
  188. - apply IHl. simpl in Hlen. lia. }
  189. intros l. apply H with l. lia.
  190. Qed.
  191. Theorem set_remove_length : forall (f : fin n) (s : set (fin n)),
  192. set_In f s ->
  193. length (set_remove Fin.eq_dec f s) < length s.
  194. Proof.
  195. intros f s Hin.
  196. induction s.
  197. - inversion Hin.
  198. - simpl. destruct (Fin.eq_dec f a) eqn:Heq.
  199. + unfold lt. apply le_n. (* Why couldn't lia get this one? *)
  200. + inversion Hin; subst. exfalso. apply n0. auto.
  201. apply IHs in H. simpl. lia.
  202. Qed.
  203. Theorem valid_input_terminates : forall (pc : fin (S n)) (v : set (fin n)) (acc : t),
  204. (exists pc', run_noswap inp (pc, v, acc) pc').
  205. Proof.
  206. intros pc v. generalize dependent pc.
  207. induction v using list_length_induction.
  208. intros pc acc.
  209. destruct (valid_input_progress pc l acc) as [[_ Hd]|[pc' [Hw [[_ Hst]|[pc'' [acc'' [Hin Hst]]]]]]].
  210. - (* We're done. *)
  211. eexists. apply run_noswap_ok. assumption.
  212. - (* We're stuck. *)
  213. eexists. apply run_noswap_fail. assumption.
  214. - (* We can make a step. This will remove our current PC from the valid list, *)
  215. edestruct (H (set_remove Fin.eq_dec pc' l)).
  216. (* Since the PC must be in the list, removing it makes the list smaller. *)
  217. apply (set_remove_length _ _ Hin).
  218. (* Without the current PC, our valid set shrinks.
  219. Since this is the inductive step, we have assumed
  220. that programs with smaller sets of valid PCs always
  221. terminate. Thus, after we make the step, we're done. *)
  222. exists x. subst. eapply run_noswap_trans.
  223. + auto.
  224. + apply Hst.
  225. + apply H0.
  226. Qed.
  227. End ValidInput.
  228. End DayEight.