|
| 1 | +From mathcomp Require Import ssreflect ssrfun ssrnat ssrbool eqtype seq. |
| 2 | +From compcert Require lib.Floats. |
| 3 | +From Wasm Require Export common typing operations. |
| 4 | +From Coq Require Import BinNat. |
| 5 | + |
| 6 | +Set Implicit Arguments. |
| 7 | +Unset Strict Implicit. |
| 8 | +Unset Printing Implicit Defensive. |
| 9 | + |
| 10 | +Section Context_inference. |
| 11 | + |
| 12 | +Variable host_function : eqType. |
| 13 | + |
| 14 | +Let function_closure := function_closure host_function. |
| 15 | +Let store_record := store_record host_function. |
| 16 | + |
| 17 | +Definition func_typing_inf (fs: list function_closure) (n: nat) : option function_type := |
| 18 | + option_map cl_type (List.nth_error fs n). |
| 19 | + |
| 20 | +Definition funcs_typing_inf (s: store_record) (inst: instance) : option (list function_type) := |
| 21 | + those (map (fun i => func_typing_inf s.(s_funcs) i) inst.(inst_funcs)). |
| 22 | + |
| 23 | +(* Choosing the most lenient bound *) |
| 24 | +Definition tab_typing_inf (ts: list tableinst) (n: nat) : option table_type := |
| 25 | + match List.nth_error ts n with |
| 26 | + | Some _ => Some (Build_table_type (Build_limits 0%N None) ELT_funcref) |
| 27 | + | _ => None |
| 28 | + end. |
| 29 | + |
| 30 | +Definition tabs_typing_inf (s: store_record) (inst: instance) : option (list table_type) := |
| 31 | + those (map (fun i => tab_typing_inf s.(s_tables) i) inst.(inst_tab)). |
| 32 | + |
| 33 | +(* Choosing the most lenient bound *) |
| 34 | +Definition mem_typing_inf (ms: list memory) (n: nat) : option memory_type := |
| 35 | + match List.nth_error ms n with |
| 36 | + | Some _ => Some (Build_limits 0%N None) |
| 37 | + | _ => None |
| 38 | + end. |
| 39 | + |
| 40 | +Definition mems_typing_inf (s: store_record) (inst: instance) : option (list memory_type) := |
| 41 | + those (map (fun i => mem_typing_inf s.(s_mems) i) inst.(inst_memory)). |
| 42 | + |
| 43 | +Definition global_typing_inf (gs: list global) (n: nat) : option global_type := |
| 44 | + match List.nth_error gs n with |
| 45 | + | Some g => Some (Build_global_type g.(g_mut) (typeof g.(g_val))) |
| 46 | + | _ => None |
| 47 | + end. |
| 48 | + |
| 49 | +Definition globals_typing_inf (s: store_record) (inst: instance) : option (list global_type) := |
| 50 | + those (map (fun i => global_typing_inf s.(s_globals) i) inst.(inst_globs)). |
| 51 | + |
| 52 | +Definition inst_typing_inf (s: store_record) (inst: instance) : option t_context := |
| 53 | + match funcs_typing_inf s inst with |
| 54 | + | Some fts => |
| 55 | + match tabs_typing_inf s inst with |
| 56 | + | Some tts => |
| 57 | + match mems_typing_inf s inst with |
| 58 | + | Some mts => |
| 59 | + match globals_typing_inf s inst with |
| 60 | + | Some gts => |
| 61 | + Some (Build_t_context inst.(inst_types) fts gts tts mts nil nil None) |
| 62 | + | _ => None |
| 63 | + end |
| 64 | + | _ => None |
| 65 | + end |
| 66 | + | _ => None |
| 67 | + end |
| 68 | + | _ => None |
| 69 | + end. |
| 70 | + |
| 71 | +Definition frame_typing_inf (s: store_record) (f: frame) : option t_context := |
| 72 | + match inst_typing_inf s f.(f_inst) with |
| 73 | + | Some C => Some (upd_local C ((map typeof f.(f_locs)) ++ tc_local C)) |
| 74 | + | None => None |
| 75 | + end. |
| 76 | + |
| 77 | +Lemma func_typing_inf_agree: forall xs n x, |
| 78 | + (func_typing_inf xs n == Some x) = |
| 79 | + functions_agree xs n x. |
| 80 | +Proof. |
| 81 | + move => xs n x. |
| 82 | + unfold func_typing_inf, functions_agree. |
| 83 | + destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth; rewrite Hnth => /=. |
| 84 | + - assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth. |
| 85 | + move/ltP in Hlen. |
| 86 | + by rewrite Hlen. |
| 87 | + - cbn. |
| 88 | + by lias. |
| 89 | +Qed. |
| 90 | + |
| 91 | +Lemma tab_typing_inf_agree: forall xs n x, |
| 92 | + tab_typing_inf xs n = Some x -> |
| 93 | + tabi_agree xs n x. |
| 94 | +Proof. |
| 95 | + move => xs n x. |
| 96 | + unfold tab_typing_inf, tabi_agree, tab_typing, limit_match. |
| 97 | + destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth => //=. |
| 98 | + assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth. |
| 99 | + move => Hinf. |
| 100 | + move/ltP in Hlen. |
| 101 | + rewrite Hlen => /=. |
| 102 | + injection Hinf as <-. |
| 103 | + cbn. |
| 104 | + rewrite Bool.andb_true_r. |
| 105 | + apply/N.leb_spec0. |
| 106 | + by lias. |
| 107 | +Qed. |
| 108 | + |
| 109 | +Lemma mem_typing_inf_agree: forall xs n x, |
| 110 | + mem_typing_inf xs n = Some x -> |
| 111 | + memi_agree xs n x. |
| 112 | +Proof. |
| 113 | + move => xs n x. |
| 114 | + unfold mem_typing_inf, memi_agree, mem_typing, limit_match. |
| 115 | + destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth => //=. |
| 116 | + assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth. |
| 117 | + move => Hinf. |
| 118 | + move/ltP in Hlen. |
| 119 | + rewrite Hlen => /=. |
| 120 | + injection Hinf as <-. |
| 121 | + cbn. |
| 122 | + rewrite Bool.andb_true_r. |
| 123 | + apply/N.leb_spec0. |
| 124 | + by lias. |
| 125 | +Qed. |
| 126 | + |
| 127 | +Lemma global_typing_inf_agree: forall xs n x, |
| 128 | + (global_typing_inf xs n = Some x) -> |
| 129 | + globals_agree xs n x. |
| 130 | +Proof. |
| 131 | + move => xs n x. |
| 132 | + unfold global_typing_inf, globals_agree, global_agree. |
| 133 | + destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth => //=. |
| 134 | + assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth. |
| 135 | + move/ltP in Hlen. |
| 136 | + rewrite Hlen => /=. |
| 137 | + move => [<-] => /=. |
| 138 | + apply/eqP; f_equal. |
| 139 | + by lias. |
| 140 | +Qed. |
| 141 | + |
| 142 | +Lemma those_all2_impl {X Y Z: Type}: forall (xs: list X) (ns: list Y) (ts: list Z) (f: list X -> Y -> option Z) (g: list X -> Y -> Z -> bool), |
| 143 | + (forall xs n x, f xs n = Some x -> g xs n x) -> |
| 144 | + (those (map (f xs) ns) = Some ts) -> |
| 145 | + all2 (g xs) ns ts. |
| 146 | +Proof. |
| 147 | + setoid_rewrite <- those_those0. |
| 148 | + move => xs. |
| 149 | + elim => //=; first by intros; destruct ts. |
| 150 | + move => y ys IH ts f g Heq Himpl. |
| 151 | + remove_bools_options. |
| 152 | + apply Heq in Hoption. |
| 153 | + eapply IH in Hoption0; last by apply Heq. |
| 154 | + by rewrite Hoption Hoption0. |
| 155 | +Qed. |
| 156 | + |
| 157 | +Lemma funcs_typing_inf_agree: forall s inst xts, |
| 158 | + (funcs_typing_inf s inst = Some xts) -> |
| 159 | + all2 (functions_agree s.(s_funcs)) inst.(inst_funcs) xts. |
| 160 | +Proof. |
| 161 | + move => f inst xts Hinf. |
| 162 | + unfold funcs_typing_inf in Hinf. |
| 163 | + eapply those_all2_impl; eauto. |
| 164 | + intros. |
| 165 | + rewrite <- func_typing_inf_agree. |
| 166 | + by apply/eqP. |
| 167 | +Qed. |
| 168 | + |
| 169 | +Lemma tabs_typing_inf_agree: forall s inst xts, |
| 170 | + (tabs_typing_inf s inst = Some xts) -> |
| 171 | + all2 (tabi_agree s.(s_tables)) inst.(inst_tab) xts. |
| 172 | +Proof. |
| 173 | + move => f inst fts Hinf. |
| 174 | + eapply those_all2_impl; eauto. |
| 175 | + intros. |
| 176 | + by apply tab_typing_inf_agree. |
| 177 | +Qed. |
| 178 | + |
| 179 | +Lemma mems_typing_inf_agree: forall s inst xts, |
| 180 | + (mems_typing_inf s inst = Some xts) -> |
| 181 | + all2 (memi_agree s.(s_mems)) inst.(inst_memory) xts. |
| 182 | +Proof. |
| 183 | + move => f inst fts Hinf. |
| 184 | + eapply those_all2_impl; eauto. |
| 185 | + intros. |
| 186 | + by apply mem_typing_inf_agree. |
| 187 | +Qed. |
| 188 | + |
| 189 | +Lemma globals_typing_inf_agree: forall s inst xts, |
| 190 | + (globals_typing_inf s inst = Some xts) -> |
| 191 | + all2 (globals_agree s.(s_globals)) inst.(inst_globs) xts. |
| 192 | +Proof. |
| 193 | + move => f inst fts Hinf. |
| 194 | + eapply those_all2_impl; eauto. |
| 195 | + intros. |
| 196 | + by apply global_typing_inf_agree. |
| 197 | +Qed. |
| 198 | + |
| 199 | +Lemma inst_typing_inf_impl: forall s inst C, |
| 200 | + inst_typing_inf s inst = Some C -> |
| 201 | + inst_typing s inst C. |
| 202 | +Proof. |
| 203 | + move => s inst C Hinf. |
| 204 | + unfold inst_typing_inf in Hinf. |
| 205 | + destruct inst. |
| 206 | + remove_bools_options => /=. |
| 207 | + apply/andP; split; last by apply mems_typing_inf_agree in Hoption1. |
| 208 | + apply/andP; split; last by apply tabs_typing_inf_agree in Hoption0. |
| 209 | + apply/andP; split; last by apply globals_typing_inf_agree in Hoption2. |
| 210 | + apply/andP; split; last by apply funcs_typing_inf_agree in Hoption. |
| 211 | + by apply/eqP. |
| 212 | +Defined. |
| 213 | + |
| 214 | +End Context_inference. |
0 commit comments