source: src/joint/linearise.ma @ 2702

Last change on this file since 2702 was 2702, checked in by sacerdot, 8 years ago
  1. proof closed in ASM/UtilBranch
  2. more passes integrated in the compiler.ma
  3. some work on commenting out broken proofs in linearise.ma
File size: 34.0 KB
Line 
1(* include "joint/TranslateUtils.ma". *)
2include "joint/Joint.ma".
3include "utilities/hide.ma".
4
5definition graph_to_lin_statement :
6  ∀p : unserialized_params.∀globals.
7  ∀A.identifier_map LabelTag A →
8  joint_statement (mk_graph_params p) globals → joint_statement (mk_lin_params p) globals ≝
9  λp,globals,A,visited,stmt.
10  match stmt return λ_.joint_statement (mk_lin_params ?) ? with
11  [ sequential c nxt ⇒
12    match c with
13    [ COND a ltrue ⇒
14      if nxt ∈ visited then FCOND … I a ltrue nxt else
15      sequential (mk_lin_params p) … c it
16    | _ ⇒ sequential (mk_lin_params p) … c it
17    ]
18  | final c ⇒ final … c
19  | FCOND abs _ _ _ ⇒ Ⓧabs
20  ].
21
22(*
23lemma graph_to_lin_labels :
24  ∀p : unserialized_params.∀globals,s.
25  stmt_labels … (graph_to_lin_statement p globals s) =
26  stmt_explicit_labels … s.
27#p#globals * [//] * //
28qed.
29*)
30
31(* discard all elements passing test, return first element failing it *)
32(* and the rest of the list, if any. *)
33let rec chop A (test : A → bool) (l : list A) on l : option (A × (list A)) ≝
34  match l with
35  [ nil ⇒ None ?
36  | cons x l' ⇒ if test x then chop A test l' else return 〈x, l'〉
37  ].
38
39lemma chop_ok : ∀A,f,l.
40  match chop A f l with
41  [ Some pr ⇒
42    let x ≝ \fst pr in
43    let post ≝ \snd pr in
44    ∃pre.All ? (λx.bool_to_Prop (f x)) pre ∧
45    l = pre @ x :: post ∧ ¬bool_to_Prop (f x)
46  | None ⇒ All A (λx.bool_to_Prop (f x)) l
47  ].
48#A #f #l elim l
49[ %
50| #hd #tl #IH whd in match (chop ???);
51  elim (true_or_false_Prop (f hd)) #H >H normalize nodelta
52  [ lapply IH elim (chop ???) normalize nodelta
53    [ #G %{H G}
54    | #pr * #pre ** #H1 #H2 #H3 %{(hd :: pre)} %{H3} %
55      [ %{H H1}
56      | >H2 %
57      ]
58    ]
59  | %{[ ]} %{H} %%
60  ]
61]
62qed.
63 
64unification hint 0 ≔ p, globals;
65lp ≟ lin_params_to_params p,
66sp ≟ stmt_pars lp,
67js ≟ joint_statement sp globals,
68lo ≟ labelled_obj LabelTag js
69(*----------------------------*)⊢
70list lo ≡ codeT lp globals.
71
72definition graph_visit_ret_type ≝ λp,globals.λg : codeT (mk_graph_params p) globals.
73  λentry : label.
74  (Σ〈visited'   : identifier_map LabelTag ℕ,
75   required'  : identifier_set LabelTag,
76   generated' : codeT (mk_lin_params p) globals〉.'hide (
77      And (And (And (And
78        (lookup ?? visited' entry = Some ? 0)
79        (required' ⊆ visited'))
80        ((∃last.stmt_at … generated' 0 = Some ? (final … last)) ∨
81         (∃a,ltrue,lfalse.stmt_at … generated' 0 = Some ? (FCOND … I a ltrue lfalse))))
82        (code_forall_labels … (λl.bool_to_Prop (l∈required')) (rev … generated')))
83        (∀l,n.lookup ?? visited' l = Some ? n →
84          And (bool_to_Prop (code_has_label … (rev ? generated') l))
85            (∃s.And
86              (lookup … g l = Some ? s)
87              (match s with
88               [ sequential s' nxt ⇒
89                 match s' with
90                 [ COND a ltrue ⇒
91                   Or
92                    (And (nth_opt … n (rev … generated') =
93                            Some ? 〈Some ? l, sequential … (COND … a ltrue) it〉)
94                         (lookup … visited' nxt = Some ? (S n)))
95                    (nth_opt … n (rev … generated') =
96                            Some ? 〈Some ? l, FCOND … I a ltrue nxt〉)
97                 |  _ ⇒
98                   And
99                     (nth_opt … n (rev … generated') =
100                        Some ? 〈Some ? l, sequential … s' it〉)
101                     (Or (lookup … visited' nxt = Some ? (S n))
102                      (nth_opt … (S n) (rev … generated') =
103                       Some ? 〈None ?, GOTO … nxt〉))
104               
105                 ]
106               | final s' ⇒
107                  nth_opt … n (rev … generated') =
108                            Some ? 〈Some ? l, final … s'〉
109               | FCOND abs _ _ _ ⇒ Ⓧabs
110               ]))))).
111               
112unification hint 0 ≔ tag ⊢ identifier_set tag ≡ identifier_map tag unit.
113
114include alias "common/Identifiers.ma".
115
116lemma lookup_safe_elim : ∀tag,A.∀P : A → Prop.∀m,i,prf.
117  (∀x.lookup tag A m i = Some ? x → P x) → P (lookup_safe tag A m i prf) ≝
118λtag,A,P,m,i,prf,H.(H … (lookup_eq_safe …)).
119
120let rec graph_visit
121  (p : unserialized_params)
122  (globals: list ident)
123  (g : codeT (mk_graph_params p) globals)
124  (* = graph (joint_statement (mk_graph_params p) globals *)
125  (required: identifier_set LabelTag)
126  (visited: identifier_map LabelTag ℕ) (* the reversed index of labels in the new code *)
127  (generated: codeT (mk_lin_params p) globals)
128  (* ≝ list ((option label)×(joint_statement (mk_lin_params p) globals)) *)
129  (visiting: list label)
130  (gen_length : ℕ)
131  (n: nat)
132  (entry : label)
133  (g_prf : code_closed … g)
134  (required_prf1 : ∀i.i∈required → Or (In ? visiting i) (bool_to_Prop (i∈visited)))
135  (required_prf2 : code_forall_labels … (λl.bool_to_Prop (l ∈ required)) (rev … generated))
136  (generated_prf1 : ∀l,n.lookup … visited l = Some ? n → hide_Prop (
137    And (bool_to_Prop (code_has_label … (rev ? generated) l))
138    (∃s.And
139      (lookup … g l = Some ? s)
140      (match s with
141       [ sequential s' nxt ⇒
142         match s' with
143         [ COND a ltrue ⇒
144           Or
145            (And (nth_opt … n (rev … generated) =
146                    Some ? 〈Some ? l, sequential … (COND … a ltrue) it〉)
147                 (match lookup … visited nxt with
148                  [ Some n' ⇒ n' = S n
149                  | None ⇒ And (nth_opt ? 0 visiting = Some ? nxt) (S n = gen_length)
150                  ]))
151            (nth_opt … n (rev … generated) =
152                    Some ? 〈Some ? l, FCOND … I a ltrue nxt〉)
153         | _ ⇒
154           And
155             (nth_opt … n (rev … generated) =
156                Some ? 〈Some ? l, sequential … s' it〉)
157             (match lookup … visited nxt with
158              [ Some n' ⇒ Or (n' = S n) (nth_opt … (S n) (rev … generated) = Some ? 〈None ?, GOTO … nxt〉)
159              | None ⇒ And (nth_opt ? 0 visiting = Some ? nxt) (S n = gen_length)
160              ])
161         ]
162       | final s' ⇒
163          nth_opt … n (rev … generated) =
164                    Some ? 〈Some ? l, final … s'〉
165       | FCOND abs _ _ _ ⇒ Ⓧabs
166       ]))))
167  (generated_prf2 : ∀l.lookup … visited l = None ? → does_not_occur … l (rev ? generated))
168  (generated_prf3 : (∃last.stmt_at … generated 0 = Some ? (final … last)) ∨
169     (∃a,ltrue,lfalse.stmt_at … generated 0 = Some ? (FCOND … a ltrue lfalse)) ∨
170     (¬All ? (λx.bool_to_Prop (x∈visited)) visiting))
171  (visiting_prf : All … (λl.bool_to_Prop (l∈g)) visiting)
172  (gen_length_prf : gen_length = length ? generated)
173  (entry_prf : Or (And (And (visiting = [entry]) (gen_length = 0)) (Not (bool_to_Prop (entry∈visited))))
174                  (lookup … visited entry = Some ? 0))
175  (n_prf : le (id_map_size … g) (plus n (id_map_size … visited)))
176  on n
177  : graph_visit_ret_type … g entry ≝
178  match chop ? (λx.x∈visited) visiting
179  return λx.? → graph_visit_ret_type … g entry with
180  [ None ⇒ λH.
181    «〈visited, required, generated〉, ?»
182  | Some pr ⇒ λH.
183    let vis_hd ≝ \fst pr in
184    let vis_tl ≝ \snd pr in
185    match n return λx.? → graph_visit_ret_type … g entry with
186    [ O ⇒ λn_prf'.⊥
187    | S n' ⇒ λn_prf'.
188      (* add the label to the visited ones *)
189      let visited' ≝ add … visited vis_hd gen_length in
190      (* take l's statement *)
191      let hd_vis_in_g ≝ (hide_prf ? ?) in
192      let statement ≝ lookup_safe ?? g vis_hd hd_vis_in_g in 
193      (* translate it to its linear version *)
194      let translated_statement ≝ graph_to_lin_statement … visited' statement in
195      (* add the translated statement to the code (which will be reversed later) *)
196      let generated' ≝ 〈Some … vis_hd, translated_statement〉 :: generated in
197      let required' ≝ union_set ???
198        (set_from_list … (stmt_explicit_labels … translated_statement))
199        required in
200      (* put successors in the visiting worklist *)
201      let visiting' ≝ stmt_labels … statement @ vis_tl in
202      (* finally, check the implicit successor *)
203      (* if it has been already visited, we need to add a GOTO *)
204      let add_req_gen ≝
205        match statement with
206        [ sequential s nxt ⇒
207          match s with
208          [ COND _ _ ⇒ 〈0, ∅, [ ]〉
209          | _ ⇒
210            if nxt ∈ visited' then
211              〈1, {(nxt)}, [〈None label, (GOTO … nxt : joint_statement ??)〉]〉
212            else 〈0, ∅, [ ]〉
213          ]
214        | _ ⇒ 〈0, ∅, [ ]〉
215        ] in
216      (* prepare a common utility to deal with add_req_gen *)
217      let add_req_gen_prf :
218        ∀P : (ℕ × (identifier_set LabelTag) × (codeT (mk_lin_params p) globals)) → Prop.
219        (match statement with
220         [ sequential s nxt ⇒
221           match s with [ COND _ _ ⇒ True | _ ⇒ ¬bool_to_Prop (nxt ∈ visited') ]
222         | _ ⇒ True] → P 〈0,∅,[ ]〉) →
223        (∀nxt.match statement with
224         [ sequential s nxt' ⇒
225          match s with [ COND _ _ ⇒ False | _ ⇒
226            nxt = nxt']
227         | _ ⇒ False]
228         → nxt ∈ visited' → P 〈1, {(nxt)}, [〈None ?, GOTO (mk_lin_params p) nxt〉]〉) →
229        P add_req_gen ≝ hide_prf ?? in
230      graph_visit ???
231        (union_set ??? (\snd (\fst add_req_gen)) required')
232        visited'
233        (\snd add_req_gen @ generated')
234        visiting'
235        (plus (\fst (\fst add_req_gen)) (S gen_length))
236        n' entry g_prf ?????????
237    ] n_prf
238  ] (chop_ok ? (λx.x∈visited) visiting).
239whd
240[ (* base case, visiting is all visited *)
241  %[%[%[%]]]
242  [ elim entry_prf
243    [ ** #eq_visiting #gen_length_O #entry_vis >eq_visiting in H; * >entry_vis *
244    | //
245    ]
246  | #l #l_req
247    elim (required_prf1 … l_req) #G
248    [ @(All_In … H G)
249    | assumption
250    ]
251  | cases generated_prf3 [/2 by /]
252    * #ABS @⊥ @ABS assumption
253  | assumption
254  | #l #n #H elim (generated_prf1 … H) -H
255    #H1 * #s * #H2 #H3 %{H1} %{s} %{H2}
256    cases s in H3; [3: *]
257    [ * ] normalize nodelta
258    [2,4: [#f #args #dest |#s'] #nxt * #EQnth_opt #H %{EQnth_opt}
259      inversion (lookup … visited nxt) in H; [2: #n'] #EQlookup
260      normalize nodelta *
261      [ #EQn' %1 >EQn' %
262      | #H %2{H}
263      | #H' lapply (All_nth … H … H')
264        whd in ⊢ (?%→?); >EQlookup *
265      ]
266    |3: #a #ltrue #lfalse * [2: #H %2{H} ] * #H1 #H2 %1 %{H1}
267      inversion (lookup … visited lfalse) in H2;
268      [ #ABS * #H' lapply (All_nth … H … H')
269        whd in ⊢ (?%→?); >ABS *
270      | #n' #_ normalize nodelta #EQ >EQ %
271      ]
272    | #s #H (*CSC XXXXXXXXXXXXXXXXXXXX @H *)
273    ]
274  ]
275(* first, close goals where add_gen_req plays no role *)
276|13: (* vis_hd is in g *)
277  elim H #pre ** #_ #H2 #_
278  @(All_split … visiting_prf … H2)
279|2: (* n = 0 absrud *)
280  elim H #pre ** #_ #H2 #H3
281  @(absurd … n_prf')
282  @lt_to_not_le
283  lapply (add_size … visited vis_hd 0 (* dummy value *))
284  >H3 normalize nodelta
285  whd in match (lt ? ?);
286  whd in match (1 + ?);
287  #EQ <EQ @subset_card @add_subset
288  [ @(All_split ? (λx.bool_to_Prop (x∈g)) ????? H2) @(All_mp … visiting_prf)
289    #a elim g #gm whd in ⊢ (?→?%); #H >(lookup_eq_safe … H) %
290  | #l #H
291    elim (generated_prf1 … (lookup_eq_safe … H)) #_ * #s * #s_in_g #_
292    whd in ⊢ (?%); >s_in_g %
293  ]
294|8:
295  elim H #pre ** #_ #H2 #_
296  @All_append
297  [ elim(g_prf … vis_hd statement ?) [2:@lookup_eq_safe] #G1 #G2
298    @(All_append … G1)
299    cases statement in G2; [2: // |3: * ]
300    #s #nxt #G' %{G'} %
301  | >H2 in visiting_prf;
302    #H' lapply (All_append_r … H') -H' * #_ //
303  ]
304|10:
305  elim H #pre ** #_ #H2 #H3 -add_req_gen_prf
306  %2 elim entry_prf
307  [ ** >H2 cases pre
308    [2: #x' #pre' #ABS normalize in ABS; destruct(ABS)
309      cases pre' in e0; [2: #x'' #pre''] #ABS' normalize in ABS'; destruct(ABS')
310    |1: #EQ normalize in EQ; destruct(EQ)
311      #eq_gen_length #_
312      >lookup_add_hit >eq_gen_length %
313    ]
314  | #lookup_entry cut (entry ≠ vis_hd)
315    [ % whd in match vis_hd; #entry_vis_hd <entry_vis_hd in H3;
316      whd in ⊢ (?(?%)→?); >lookup_entry * #ABS @ABS % ]
317    #entry_not_vis_hd >(lookup_add_miss ?????? entry_not_vis_hd) assumption
318  ]
319|11:
320  elim H #pre ** #_ #_ #H3
321  >commutative_plus
322  >add_size >H3 normalize nodelta
323  whd in match (1 + ?);
324  >commutative_plus
325  assumption
326|12: (* add_req_gen utility *)
327  #P whd in match add_req_gen;
328  cases statement [ * [#f #args #dest|  #a #ltrue | #s ] #nxt | #s | * ]
329 [2,4: normalize nodelta #l #H1 #H2 lapply (H2 … (refl …)) -H2
330      cases (l ∈ add …) in H1; normalize nodelta #H1 #H2 try @H2 try @H1 try %
331      //
332 |3,5: normalize nodelta [#_] #H #_ @H %
333 | normalize nodelta in dest nxt ⊢ %; inversion (args ∈ add …) #EQ
334   normalize nodelta [ @nxt | @dest ] >EQ try % ]
335|14: skip
336] cases daemon (*CSC: XXXXXXXXX
337|3: elim H #pre ** #H1 #H2 #_
338  #i >mem_set_union
339  #H elim (orb_Prop_true … H) -H
340  [ @add_req_gen_prf [ #_ >mem_set_empty * ]
341    #next #_ #next_vis #H >(mem_set_singl_to_eq … H) %2 assumption
342  | >mem_set_union
343    #H elim (orb_Prop_true … H) -H (*CSC XXXXXXXXXX
344    [ #i_expl %1 @Exists_append_l
345      lapply i_expl whd in match translated_statement;
346      cases statement [ * [ #f #args #dest | #a #ltrue | #s ] #nxt | #s | *] normalize nodelta #H
347      lapply (mem_list_as_set … H) -H #H
348      [1,3,4: @Exists_append_r assumption ]
349      cases (nxt ∈ visited') in H; normalize nodelta * [2,4: * [2: * ]]
350      #EQ destruct(EQ) [ %1 % |*: %2 %1 % ]
351    | (* i was already required *)
352      #i_req
353      elim (required_prf1 … i_req)
354      [ >H2 #H elim (Exists_append … H) -H
355        [ (* i in preamble → i∈visited *)
356          #i_pre %2 >mem_set_add @orb_Prop_r
357          @(All_In … H1 i_pre)
358        | *
359          [ (* i is vis_hd *)
360            #eq_i >eq_i %2 @mem_set_add_id
361          | (* i in vis_tl → i∈visiting' *)
362            #i_post % @Exists_append_r assumption
363          ]
364        ]
365      | (* i in visited *)
366        #i_vis %2 >mem_set_add @orb_Prop_r assumption
367      ]
368    ]*) cases daemon
369  ]
370|4,5,6: change with reverse in match rev;
371  >reverse_append whd in match (reverse ??); >rev_append_def
372  >associative_append
373  [ #pt #s
374    @(leb_elim (S pt) (|generated|)) #cmp
375    whd in match (stmt_at ????);
376    [ >nth_opt_append_l [2: >length_reverse assumption ]
377      change with (stmt_at ???? = ? → ?)
378      #EQ lapply(required_prf2 … EQ) @All_mp
379      #l #l_req >mem_set_union @orb_Prop_r
380      >mem_set_union @orb_Prop_r @l_req
381    | >nth_opt_append_r [2: >length_reverse @not_lt_to_le assumption ]
382      cases (pt - ?)
383      [ whd in match (nth_opt ???); whd in ⊢ (??%?→?);
384        #EQ lapply (sym_eq ??? EQ) -EQ #EQ destruct(EQ) whd
385        whd in match required';
386        cut (∀p : lin_params.∀globals.∀s.
387          stmt_implicit_label p globals s = None ?)
388        [ #p #globals * normalize // ]
389        #lin_implicit_label change with (? @ ?) in match (stmt_labels ???);
390        >lin_implicit_label
391        change with (All ?? (stmt_explicit_labels ???))
392        generalize in match (stmt_explicit_labels … translated_statement);
393        #l @list_as_set_All
394        #i >mem_set_union >mem_set_union
395        #i_l @orb_Prop_r @orb_Prop_l assumption
396      | @add_req_gen_prf
397        [ #_ | #next #_ #next_vis *
398          [ whd in ⊢ (??%?→?);
399            #EQ' destruct(EQ') whd %{I} >mem_set_union
400            @orb_Prop_l @mem_set_add_id ]]
401        #n whd in ⊢ (??%?→?); #ABS destruct(ABS)
402      ]
403    ]
404  | elim H #pre ** #H1 #H2 #H3
405    #i whd in match visited';
406    @(eq_identifier_elim … i vis_hd)
407    [ #EQi >EQi -i #pos
408      >lookup_add_hit in ⊢ (%→?); #EQpos (* too slow: destruct(EQpos) *)
409      cut (gen_length = pos)
410      [ -visited destruct(EQpos) %]
411      -EQpos #EQpos <EQpos -pos
412      %
413      [ >lin_code_has_label
414        @add_req_gen_prf
415        [ #_
416        | #next #_ #next_vis
417          change with (? @ ([?] @ [?])) in match (? @ [? ; ?]);
418          <associative_append >occurs_exactly_once_None
419        ]
420        >occurs_exactly_once_Some_eq >eq_identifier_refl
421        normalize nodelta
422        @generated_prf2
423        lapply (in_map_domain … visited vis_hd)
424        >H3 normalize nodelta //
425      | %{statement}  (*CSC XXXXXXX
426        %
427        [ @lookup_eq_safe
428        | normalize nodelta
429          change with statement in match (lookup_safe ?????);
430          cases statement;
431          [ * [ #f #args #dest | #a #ltrue | #s ] #nxt | #s | * ] normalize nodelta
432          [1,2,3: inversion (nxt ∈ visited') normalize nodelta #nxt_vis ]
433          [1,2,5,6: % | %2 | %1 % ]
434          [1,3,5,7,9,10,12:
435            >nth_opt_append_r >rev_length <gen_length_prf try %
436            <minus_n_n try % whd in match graph_to_lin_statement; normalize nodelta
437            >nxt_vis %
438          |*:
439            lapply (in_map_domain … visited' nxt) >nxt_vis normalize nodelta
440            [1,3: * #n' ] #EQlookup >EQlookup normalize nodelta
441            [1,2: %2 >nth_opt_append_r >rev_length <gen_length_prf [2,4: %2 %1 ]
442              <minus_Sn_n %
443            |*: %%
444            ]
445          ]*) cases daemon
446        ]
447      ]
448    | #NEQ #n_i >(lookup_add_miss … visited … NEQ)
449      cases daemon (*CSC XXXX
450      #Hlookup elim (generated_prf1 … Hlookup)
451      #G1 * #s * #G2 #G3
452      %
453      [ >lin_code_has_label <associative_append
454        >occurs_exactly_once_append
455        @orb_Prop_l @andb_Prop
456        [ >occurs_exactly_once_Some_eq
457          >eq_identifier_false [2: % #ABS >ABS in NEQ; * #ABS' @ABS' % ]
458          normalize nodelta >lin_code_has_label in G1; #K @K
459        | @add_req_gen_prf
460          [ #_ % |  #next #_ #_  % ]
461        ]
462      | %{s}
463        %{G2}
464        cases s in G3;
465        [ * [ #f #args #dest | #a #ltrue | #s ] #nxt | #s | * ]
466        [1,3: * #EQnth_opt #next_spec % | * [*] #EQnth_opt [#next_spec %1 % | %2] | #EQnth_opt ]
467        [1,3,5,7: @nth_opt_append_hit_l assumption
468        |2,4,6: @(eq_identifier_elim … nxt vis_hd)
469          [1,3: #EQ destruct(EQ) >lookup_add_hit whd [ %1 ]
470            lapply (in_map_domain … visited vis_hd)
471            >H3 #EQ >EQ in next_spec; * #_ #OK >OK %
472          |*: #NEQ' >(lookup_add_miss … visited … NEQ')
473            lapply (in_map_domain … visited nxt)
474            inversion (nxt ∈ visited) #nxt_vis [1,3: * #n'' ] #EQlookup'
475            >EQlookup' in next_spec; whd in ⊢ (%→%);
476            [ * [ #H %1{H} ] #H %2 @nth_opt_append_hit_l assumption
477            | #H @H
478            |*: * >H2
479              cases pre in H1;
480              [1,3: #_
481              |*: #frst #pre_tl * #ABS #_
482              ] whd in ⊢ (??%?→?); #EQ destruct(EQ)
483              [1,2: cases NEQ' #ABS cases (ABS ?) %
484              |*: >nxt_vis in ABS; *
485              ]
486            ]
487          ]
488        ]
489      ] *)
490    ]
491  | #i whd in match visited';
492    @(eq_identifier_elim … i vis_hd) #Heq
493    [ >Heq >lookup_add_hit #ABS destruct(ABS) ]
494    >(lookup_add_miss ?????? Heq)
495    #i_n_vis
496    >does_not_occur_append @andb_Prop
497    [ @generated_prf2 assumption
498    | change with (bool_to_Prop (¬eq_identifier ??? ∧ ?))
499      >eq_identifier_false [2: % #ABS <ABS in Heq; * #ABS' @ABS' % ]
500      @add_req_gen_prf [ #_ | #s #next #_ #_ ] %
501    ]
502  ]
503| @add_req_gen_prf
504  [ #K | #s #next #K #next_vis %1 %1 %{(GOTO … next)} % ]
505  whd in match generated'; whd in match translated_statement;
506  normalize nodelta
507  change with statement in match (lookup_safe ?????);
508  cases statement in K;
509  [ * [ #s | #a #ltrue ] #nxt | #s #_ %1 %1 %{s} % | * ] normalize nodelta
510  [2: #_ cases (true_or_false_Prop (nxt ∈ visited')) #nxt_vis
511    [ >nxt_vis normalize nodelta %1 %2 %{a} %{ltrue} %{nxt} % ]
512  | #nxt_vis ]
513  %2 % * >nxt_vis *
514| whd in match generated';
515  @add_req_gen_prf [ #_ | #s #next #_ #_ ] normalize >gen_length_prf %
516|
517]*)
518qed.
519
520(* CSC: The branch compression (aka tunneling) optimization is not implemented
521   in Matita *)
522definition branch_compress
523  ≝ λp: graph_params.λglobals.λg:codeT p globals.
524    λentry : Σl.bool_to_Prop (code_has_label … g l).g.
525 
526lemma branch_compress_closed : ∀p,globals,g,l.code_closed ?? g →
527  code_closed … (branch_compress p globals g l).
528#p#globals#g#l#H @H qed.
529
530lemma branch_compress_has_entry : ∀p,globals,g,l.
531  code_has_label … (branch_compress p globals g l) l.
532#p#globals#g*#l#l_prf @l_prf qed.
533
534definition filter_labels ≝ λtag,A.λtest : identifier tag → bool.λc : list (labelled_obj tag A).
535  map ??
536    (λs. let 〈l_opt,x〉 ≝ s in
537      〈! l ← l_opt ; if test l then return l else None ?, x〉) c.
538     
539lemma does_not_occur_filter_labels :
540  ∀tag,A,test,id,c.
541    does_not_occur ?? id (filter_labels tag A test c) =
542      (does_not_occur ?? id c ∨ ¬ test id).
543#tag #A #test #id #c elim c
544[ //
545| ** [2: #lbl] #s #tl #IH
546  whd in match (filter_labels ????); normalize nodelta
547  whd in match (does_not_occur ????) in ⊢ (??%%);
548  [2: @IH]
549  normalize in match (! l ← ? ; ?); >IH
550  @(eq_identifier_elim ?? lbl id) #Heq [<Heq]
551  elim (test lbl) normalize nodelta
552  change with (eq_identifier ???) in match (instruction_matches_identifier ????);
553  [1,2: >eq_identifier_refl [2: >commutative_orb] normalize %
554  |*: >(eq_identifier_false ??? Heq) normalize nodelta %
555  ]
556]
557qed.
558
559lemma occurs_exactly_once_filter_labels :
560  ∀tag,A,test,id,c.
561    occurs_exactly_once ?? id (filter_labels tag A test c) =
562      (occurs_exactly_once ?? id c ∧ test id).
563#tag #A #test #id #c elim c
564[ //
565| ** [2: #lbl] #s #tl #IH
566  whd in match (filter_labels ????); normalize nodelta
567  whd in match (occurs_exactly_once ????) in ⊢ (??%%);
568  [2: @IH]
569  normalize in match (! l ← ? ; ?); >IH
570  >does_not_occur_filter_labels
571  @(eq_identifier_elim ?? lbl id) #Heq [<Heq]
572  elim (test lbl) normalize nodelta
573  change with (eq_identifier ???) in match (instruction_matches_identifier ????);
574  [1,2: >eq_identifier_refl >commutative_andb [ >(commutative_andb ? true) >commutative_orb | >(commutative_andb ? false)] normalize %
575  |*: >(eq_identifier_false ??? Heq) normalize nodelta %
576  ]
577]
578qed.
579
580lemma nth_opt_filter_labels : ∀tag,A,test,instrs,n.
581  nth_opt ? n (filter_labels tag A test instrs) =
582  ! 〈lopt, s〉 ← nth_opt ? n instrs ;
583  return 〈 ! lbl ← lopt; if test lbl then return lbl else None ?, s〉.
584#tag #A #test #instrs elim instrs
585[ * [2: #n'] %
586| * #lopt #s #tl #IH * [2: #n']
587  whd in match (filter_labels ????); normalize nodelta
588  whd in match (nth_opt ???) in ⊢ (??%%); [>IH] %
589]
590qed.
591
592lemma stmt_at_filter_labels : ∀p : lin_params.∀globals,test.∀c : codeT p globals.
593∀i.stmt_at p globals (filter_labels ?? test c) i = stmt_at p globals c i.
594#p#globals#test #c#i
595whd in ⊢ (??%%); >nth_opt_filter_labels
596elim (nth_opt ???); //
597qed.
598
599(* spostato in ExtraMonads.ma
600lemma option_bind_inverse : ∀A,B.∀m : option A.∀f : A → option B.∀r.
601  ! x ← m ; f x = return r →
602  ∃x.m = return x ∧ f x = return r.
603#A #B * normalize [2:#x] #f #r #EQ destruct
604%{x} %{EQ} %
605qed.
606*)
607
608lemma nth_opt_reverse_hit :
609  ∀A,l,n.n < |l| → nth_opt A n (reverse ? l) = nth_opt A (|l| - (S n)) l.
610#A #l elim l
611[ #n #ABS normalize in ABS; @⊥ -A /2 by absurd/
612| #hd #tl #IH #n #lim whd in match (reverse ??); >rev_append_def
613  @(leb_elim (S n) (|tl|)) #H
614  [ >nth_opt_append_l [2: >length_reverse @H ]
615    >(IH … H) >(minus_Sn_m … H) %
616  | >(le_to_le_to_eq … (le_S_S_to_le … lim) (not_lt_to_le … H))
617    >nth_opt_append_r >length_reverse [2: % ]
618    <minus_n_n <minus_n_n %
619  ]
620]
621qed.
622
623lemma nth_opt_reverse_hit_inv :
624  ∀A,l,n.n < |l| → nth_opt A (|l| - (S n)) (reverse ? l) = nth_opt A n l.
625#A #l #n #H <(reverse_reverse ? l) in ⊢ (???%); @sym_eq
626<length_reverse @nth_opt_reverse_hit >length_reverse @H
627qed.
628
629definition good_local_sigma :
630  ∀p:unserialized_params.
631  ∀globals.
632  ∀g:codeT (mk_graph_params p) globals.
633  (Σl.bool_to_Prop (code_has_label … g l)) →
634  codeT (mk_lin_params p) globals →
635  (label → option ℕ) → Prop ≝
636  λp,globals,g,entry,c,sigma.
637    sigma entry = Some ? 0 ∧
638    (∀l,n.point_of_label … c l = Some ? n → sigma l = Some ? n) ∧
639    ∀l,n.sigma l = Some ? n →
640      ∃s. stmt_at … g l = Some ? s ∧
641          All ? (λl.sigma l ≠ None ?) (stmt_labels … s) ∧
642          (match s with
643           [ sequential s' nxt ⇒
644             match s' with
645             [ step_seq _ ⇒
646               (stmt_at … c n = Some ? (sequential … s' it)) ∧
647                  ((sigma nxt = Some ? (S n)) ∨
648                   (stmt_at … c (S n) = Some ? (GOTO … nxt)))
649             | COND a ltrue ⇒
650               (stmt_at … c n = Some ? (sequential … s' it) ∧ sigma nxt = Some ? (S n)) ∨
651               (stmt_at … c n = Some ? (FCOND … I a ltrue nxt))
652             | CALL _ _ _ ⇒
653               (stmt_at … c n = Some ? (sequential … s' it)) ∧
654                  ((sigma nxt = Some ? (S n)) ∨
655                   (stmt_at … c (S n) = Some ? (GOTO … nxt)))               
656             | COST_LABEL _ ⇒
657               (stmt_at … c n = Some ? (sequential … s' it)) ∧
658                  ((sigma nxt = Some ? (S n)) ∨
659                   (stmt_at … c (S n) = Some ? (GOTO … nxt)))               
660             ]
661           | final s' ⇒
662             stmt_at … c n = Some ? (final … s')
663           | FCOND abs _ _ _ ⇒ Ⓧabs
664           ]).
665
666definition linearise_code:
667 ∀p : unserialized_params.∀globals.
668  ∀g : codeT (mk_graph_params p) globals.code_closed … g →
669  ∀entry : (Σl.bool_to_Prop (code_has_label … g l))
670  .Σ〈c, sigma〉.
671    good_local_sigma … g entry c sigma ∧
672    code_closed … c
673       (* ∧
674      ∃ sigma : identifier_map LabelTag ℕ.
675      lookup … sigma entry = Some ? 0 ∧
676      ∀l,n.lookup … sigma l = Some ? n →
677        ∃s. lookup … g l = Some ? s ∧
678          opt_Exists ?
679            (λls.let 〈lopt, ts〉 ≝ ls in
680              opt_All ? (eq ? l) lopt ∧
681              ts = graph_to_lin_statement … s ∧
682              opt_All ?
683                (λnext.Or (lookup … sigma next = Some ? (S n))
684                (nth_opt … (S n) c = Some ? 〈None ?, GOTO … next〉))
685                (stmt_implicit_label … s)) (nth_opt … n c)*)
686
687 λp,globals,g,g_prf,entry_sig.
688    let g ≝ branch_compress (mk_graph_params p) ? g entry_sig in
689    let g_prf ≝ branch_compress_closed … g entry_sig g_prf in
690    match graph_visit p globals g ∅ (empty_map …) [ ] [entry_sig] 0 (|g|)
691      (entry_sig) g_prf ?????????
692    with
693    [ mk_Sig triple H ⇒
694      let sigma ≝ \fst (\fst triple) in
695      let required ≝ \snd (\fst triple) in
696      let crev ≝ \snd triple in
697      let lbld_code ≝ rev ? crev in
698      〈filter_labels … (λl.l∈required) lbld_code, lookup … sigma〉 ].
699[ cases (graph_visit ????????????????????)
700 (* done later *)
701| #i >mem_set_empty *
702|3,4: #a #b whd in ⊢ (??%?→?); #ABS destruct(ABS)
703| #l #_ %
704| %2 % * >mem_set_empty *
705| % [2: %] @(branch_compress_has_entry … g entry_sig)
706| %
707| % % [% %] cases (pi1 … entry_sig) normalize #_ % //
708| >commutative_plus change with (? ≤ |g|) %
709]
710cases daemon qed. (*
711**
712#visited #required #generated normalize nodelta ****
713#entry_O #req_vis #last_fin #labels_in_req #sigma_prop
714%
715[ % [ % [assumption]]
716  #lbl #n
717  [ change with (If ? then with prf do ? else ? = ? → ?)
718    @If_elim [2: #_ #ABS destruct(ABS) ]
719    #H lapply H
720    >occurs_exactly_once_filter_labels
721    elim (true_or_false_Prop … (occurs_exactly_once ?? lbl ?))
722    [1,2: #H1 >H1 |*:] [2: * ]
723    elim (true_or_false_Prop … (lbl ∈ required)) #H2 >H2 *
724    lapply (in_map_domain … visited lbl) >(req_vis … H2)
725    * #n_lbl #EQsigma
726    elim (sigma_prop … EQsigma) #_ * #stmt * #_
727    cases stmt [ * [ #s | #a #ltrue ] #nxt | #s | * ] normalize nodelta
728    [ * #EQnth_opt #_ | * [ * ] #EQnth_opt [ #_ ] | #EQnth_opt ]
729    >(nth_opt_index_of_label ???? n_lbl ? H)
730    [1,4,7,10: normalize in ⊢ (% → ?); #EQ destruct(EQ) assumption
731    |3: @(sequential … s it) |6: @(sequential … (COND … a ltrue) it)
732    |9: @(FCOND … I a ltrue nxt) |12: @(final … s)
733    |2,5,8,11: >nth_opt_filter_labels >EQnth_opt >m_return_bind >m_return_bind
734      >H2 %
735    ]
736  | #eq_lookup elim (sigma_prop ?? eq_lookup)
737    #lbl_in_gen * #stmt * #stmt_in_g #stmt_spec
738    % [2: % [ % [ assumption ]] |]
739    [ @All_append
740      [ cases stmt in stmt_spec;
741        [ * [ #s | #a #ltrue ] #nxt | #s #_ % | * ]
742        normalize nodelta * [ #stmt_at_EQ * #nxt_spec | * #stmt_at_EQ #nxt_spec | #stmt_at_EQ ]
743        %{I}
744        [1,3: >nxt_spec % #ABS destruct(ABS)
745        |*: lapply (in_map_domain … visited nxt)
746          >req_vis
747          [1,3: * #x #EQ >EQ % #ABS destruct(ABS)
748          |2: @(proj1 … (labels_in_req (S n) (GOTO … nxt) …))
749            whd in ⊢ (??%?); >nxt_spec %
750          |4: @(proj1 … (proj2 … (labels_in_req n (FCOND … I a ltrue nxt) …)))
751            whd in ⊢ (??%?); >stmt_at_EQ %
752          ]
753        ]
754      | cases stmt in stmt_spec;
755        [ * [ #s | #a #ltrue ] #nxt | #s | * ] normalize nodelta
756        [ * #EQ #_ | * [ * #EQ #_ | #EQ ] | #EQ ]
757        whd in match stmt_explicit_labels; normalize nodelta
758        [ @(All_mp … (labels_in_req n (sequential … s it) ?))
759        | @(All_mp … (labels_in_req n (sequential … (COND … a ltrue) it) ?))
760        | cases (labels_in_req n (FCOND … I a ltrue nxt) ?)
761          [ #l_req #_ %{I} lapply (in_map_domain … visited ltrue)
762            >(req_vis … l_req) * #n #EQ' >EQ' % #ABS destruct(ABS) ]
763        | @(All_mp … (labels_in_req n (final … s) ?))
764        ]
765        [1,3,6: #l #l_req >(lookup_eq_safe … (req_vis … l_req)) % #ABS destruct(ABS)
766        |*: whd in ⊢ (??%?); >EQ %
767        ]
768      ]
769    | cases stmt in stmt_spec;
770      [ * [ #s | #a #ltrue ] #nxt | #s | * ] normalize nodelta
771      [ * #EQ #H | * [ * #EQ #H | #EQ ] | #EQ ]
772      >stmt_at_filter_labels
773      whd in match (stmt_at (mk_lin_params p) ?? n); >EQ normalize nodelta
774      [ % [%] cases H -H [#H %1{H} | #EQ' %2 >stmt_at_filter_labels whd in ⊢ (??%?); >EQ' % ]
775      | %1 %{H} %
776      | %2 %
777      | %
778      ]
779    ]
780  ]
781| #i #s
782  >stmt_at_filter_labels #EQ
783  %
784  [ @stmt_forall_labels_explicit
785    @(All_mp … (labels_in_req … EQ))
786    #l #l_req >lin_code_has_label
787    >occurs_exactly_once_filter_labels >l_req
788    >commutative_andb whd in ⊢ (?%);
789    elim (sigma_prop ?? (lookup_eq_safe … (req_vis … l_req)))
790    >lin_code_has_label #H #_ @H
791  | lapply EQ cases s [ #s' * | #s' #_ % | * #a #ltrue #lfalse #_ % ]
792    -EQ #EQ change with (bool_to_Prop (code_has_point ????))
793    whd in match (point_of_succ ???);
794    >lin_code_has_point @leb_elim [ #_ % ] >length_map >length_reverse
795    #INEQ
796    cut (|generated| = S i)
797    [ @(le_to_le_to_eq … (not_lt_to_le … INEQ) )
798      elim (option_bind_inverse ????? EQ) #x * #EQ1 #EQ2
799      <length_reverse
800      @(nth_opt_hit_length … EQ1)
801    ] #EQ_length
802    elim last_fin * [ #fin | #a * #ltrue * #lfalse ] #EQ'
803    lapply EQ whd in match (stmt_at ????);
804    >nth_opt_reverse_hit >EQ_length [2,4: % ] <minus_n_n
805    change with (stmt_at ???? = ? → ?)
806    >EQ' #ABS destruct(ABS)
807  ]
808]
809qed.
810*)
811
812definition linearise_int_fun :
813  ∀p : unserialized_params.
814  ∀globals.
815    ∀fn_in : joint_closed_internal_function (mk_graph_params p) globals
816     .Σ〈fn_out : joint_closed_internal_function (mk_lin_params p) globals,
817        sigma : ?〉.
818        good_local_sigma … (joint_if_code … fn_in) (joint_if_entry … fn_in)
819          (joint_if_code … fn_out) sigma
820     (* ∃sigma : identifier_map LabelTag ℕ.
821        let g ≝ joint_if_code ?? (pi1 … fin) in
822        let c ≝ joint_if_code ?? (pi1 … fout) in
823        let entry ≝ joint_if_entry ?? (pi1 … fin) in
824         lookup … sigma entry = Some ? 0 ∧
825          ∀l,n.lookup … sigma l = Some ? n →
826            ∃s. lookup … g l = Some ? s ∧
827              opt_Exists ?
828                (λls.let 〈lopt, ts〉 ≝ ls in
829                  opt_All ? (eq ? l) lopt ∧
830                  ts = graph_to_lin_statement … s ∧
831                  opt_All ?
832                    (λnext.Or (lookup … sigma next = Some ? (S n))
833                    (nth_opt … (S n) c = Some ? 〈None ?, GOTO … next〉))
834                    (stmt_implicit_label … s)) (nth_opt … n c)*) ≝
835  λp,globals,f_sig.
836  let code_sigma ≝ linearise_code …
837    (joint_if_code … f_sig)
838    (pi2 … f_sig)
839    (joint_if_entry … f_sig) in
840  let code ≝ \fst code_sigma in
841  let sigma ≝ \snd code_sigma in
842  let entry : Σpt.bool_to_Prop (code_has_point … code pt) ≝ «0, hide_prf ??» in
843  〈«mk_joint_internal_function (mk_lin_params p) globals
844   (joint_if_luniverse ?? f_sig) (joint_if_runiverse ?? f_sig)
845   (joint_if_result ?? f_sig) (joint_if_params ?? f_sig)
846   (joint_if_stacksize ?? f_sig) code entry, ?»,
847   sigma〉.
848normalize nodelta
849cases (linearise_code ?????) * #code #sigma normalize nodelta * #H1 #H2
850[ @H2
851| @H1
852| cases H1 * #H3 #H4 #H5 elim (H5 … H3)
853  #s ** #_ #_ >lin_code_has_point cases code
854  [ cases s [ * [ #s' | #a #ltrue ] #nxt | #s' | * ]
855    [ * #ABS #_ | * [ * #ABS #_ | #ABS ] | #ABS ] normalize in ABS; destruct(ABS)
856  | #hd #tl #_ %
857  ]
858]
859qed.
860
861definition linearise : ∀p : unserialized_params.
862  program (joint_function (mk_graph_params p)) ℕ →
863  program (joint_function (mk_lin_params p)) ℕ
864   ≝
865  λp,pr.transform_program ??? pr
866    (λglobals.transf_fundef ?? (λf_in.\fst (linearise_int_fun p globals f_in))).
867
868(*
869definition good_sigma :
870  ∀p:unserialized_params.
871  ∀prog_in : joint_program (mk_graph_params p).
872  ((Σi.is_internal_function_of_program … prog_in i) → label → option ℕ) → Prop ≝
873  λp,prog_in,sigma.
874  let prog_out ≝ linearise … prog_in in
875  ∀i : Σi.is_internal_function_of_program … prog_in i.∀prf.
876  let fn_in ≝ if_of_function … i in
877  let i' : Σi.is_internal_function_of_program … prog_out i ≝ «pi1 ?? i, prf» in
878  let fn_out ≝ if_of_function … i' in
879  let sigma_local ≝ sigma i in
880  good_local_sigma ?? (joint_if_code ?? fn_in) (joint_if_entry … fn_in)
881          (joint_if_code ?? fn_out) sigma_local.
882
883lemma linearise_spec : ∀p,prog.∃sigma.good_sigma p prog sigma.
884#p #prog
885letin sigma ≝
886  (λi : Σi.is_internal_function_of_program … prog i.    let fn_in ≝ if_of_function … i in
887    \snd (linearise_int_fun … fn_in))
888%{sigma}
889#i #prf >(prog_if_of_function_transform … i) [2: % ]
890normalize nodelta
891cases (linearise_int_fun ???) * #fn_out #sigma_loc
892normalize nodelta #prf @prf
893qed.
894*)
Note: See TracBrowser for help on using the repository browser.