source: src/joint/linearise.ma @ 2570

Last change on this file since 2570 was 2570, checked in by piccolo, 7 years ago

ERTLtoERTLptr in place

File size: 33.2 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    [1,3: [#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    | #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 @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 #H #_ @(H I) ]
330  normalize nodelta inversion (nxt ∈ visited') #EQ normalize nodelta
331  #H1 #H2 [1,3: @(H2 … (refl …)) >EQ % |*: @H1 % * ]
332|3: elim H #pre ** #H1 #H2 #_
333  #i >mem_set_union
334  #H elim (orb_Prop_true … H) -H
335  [ @add_req_gen_prf [ #_ >mem_set_empty * ]
336    #next #_ #next_vis #H >(mem_set_singl_to_eq … H) %2 assumption
337  | >mem_set_union
338    #H elim (orb_Prop_true … H) -H
339    [ #i_expl %1 @Exists_append_l
340      lapply i_expl whd in match translated_statement;
341      cases statement [ * [ #f #args #dest | #a #ltrue | #s ] #nxt | #s | *] normalize nodelta #H
342      lapply (mem_list_as_set … H) -H #H
343      [1,3,4: @Exists_append_r assumption ]
344      cases (nxt ∈ visited') in H; normalize nodelta * [2,4: * [2: * ]]
345      #EQ destruct(EQ) [ %1 % |*: %2 %1 % ]
346    | (* i was already required *)
347      #i_req
348      elim (required_prf1 … i_req)
349      [ >H2 #H elim (Exists_append … H) -H
350        [ (* i in preamble → i∈visited *)
351          #i_pre %2 >mem_set_add @orb_Prop_r
352          @(All_In … H1 i_pre)
353        | *
354          [ (* i is vis_hd *)
355            #eq_i >eq_i %2 @mem_set_add_id
356          | (* i in vis_tl → i∈visiting' *)
357            #i_post % @Exists_append_r assumption
358          ]
359        ]
360      | (* i in visited *)
361        #i_vis %2 >mem_set_add @orb_Prop_r assumption
362      ]
363    ]
364  ]
365|4,5,6: change with reverse in match rev;
366  >reverse_append whd in match (reverse ??); >rev_append_def
367  >associative_append
368  [ #pt #s
369    @(leb_elim (S pt) (|generated|)) #cmp
370    whd in match (stmt_at ????);
371    [ >nth_opt_append_l [2: >length_reverse assumption ]
372      change with (stmt_at ???? = ? → ?)
373      #EQ lapply(required_prf2 … EQ) @All_mp
374      #l #l_req >mem_set_union @orb_Prop_r
375      >mem_set_union @orb_Prop_r @l_req
376    | >nth_opt_append_r [2: >length_reverse @not_lt_to_le assumption ]
377      cases (pt - ?)
378      [ whd in match (nth_opt ???); whd in ⊢ (??%?→?);
379        #EQ lapply (sym_eq ??? EQ) -EQ #EQ destruct(EQ) whd
380        whd in match required';
381        cut (∀p : lin_params.∀globals.∀s.
382          stmt_implicit_label p globals s = None ?)
383        [ #p #globals * normalize // ]
384        #lin_implicit_label change with (? @ ?) in match (stmt_labels ???);
385        >lin_implicit_label
386        change with (All ?? (stmt_explicit_labels ???))
387        generalize in match (stmt_explicit_labels … translated_statement);
388        #l @list_as_set_All
389        #i >mem_set_union >mem_set_union
390        #i_l @orb_Prop_r @orb_Prop_l assumption
391      | @add_req_gen_prf
392        [ #_ | #next #_ #next_vis *
393          [ whd in ⊢ (??%?→?);
394            #EQ' destruct(EQ') whd %{I} >mem_set_union
395            @orb_Prop_l @mem_set_add_id ]]
396        #n whd in ⊢ (??%?→?); #ABS destruct(ABS)
397      ]
398    ]
399  | elim H #pre ** #H1 #H2 #H3
400    #i whd in match visited';
401    @(eq_identifier_elim … i vis_hd)
402    [ #EQi >EQi -i #pos
403      >lookup_add_hit in ⊢ (%→?); #EQpos (* too slow: destruct(EQpos) *)
404      cut (gen_length = pos)
405      [ -visited destruct(EQpos) %]
406      -EQpos #EQpos <EQpos -pos
407      %
408      [ >lin_code_has_label
409        @add_req_gen_prf
410        [ #_
411        | #next #_ #next_vis
412          change with (? @ ([?] @ [?])) in match (? @ [? ; ?]);
413          <associative_append >occurs_exactly_once_None
414        ]
415        >occurs_exactly_once_Some_eq >eq_identifier_refl
416        normalize nodelta
417        @generated_prf2
418        lapply (in_map_domain … visited vis_hd)
419        >H3 normalize nodelta //
420      | %{statement}
421        %
422        [ @lookup_eq_safe
423        | normalize nodelta
424          change with statement in match (lookup_safe ?????);
425          cases statement;
426          [ * [ #f #args #dest | #a #ltrue | #s ] #nxt | #s | * ] normalize nodelta
427          [1,2,3: inversion (nxt ∈ visited') normalize nodelta #nxt_vis ]
428          [1,2,5,6: % | %2 | %1 % ]
429          [1,3,5,7,9,10,12:
430            >nth_opt_append_r >rev_length <gen_length_prf try %
431            <minus_n_n try % whd in match graph_to_lin_statement; normalize nodelta
432            >nxt_vis %
433          |*:
434            lapply (in_map_domain … visited' nxt) >nxt_vis normalize nodelta
435            [1,3: * #n' ] #EQlookup >EQlookup normalize nodelta
436            [1,2: %2 >nth_opt_append_r >rev_length <gen_length_prf [2,4: %2 %1 ]
437              <minus_Sn_n %
438            |*: %%
439            ]
440          ]
441        ]
442      ]
443    | #NEQ #n_i >(lookup_add_miss … visited … NEQ)
444      #Hlookup elim (generated_prf1 … Hlookup)
445      #G1 * #s * #G2 #G3
446      %
447      [ >lin_code_has_label <associative_append
448        >occurs_exactly_once_append
449        @orb_Prop_l @andb_Prop
450        [ >occurs_exactly_once_Some_eq
451          >eq_identifier_false [2: % #ABS >ABS in NEQ; * #ABS' @ABS' % ]
452          normalize nodelta >lin_code_has_label in G1; #K @K
453        | @add_req_gen_prf
454          [ #_ % |  #next #_ #_  % ]
455        ]
456      | %{s}
457        %{G2}
458        cases s in G3;
459        [ * [ #f #args #dest | #a #ltrue | #s ] #nxt | #s | * ]
460        [1,3: * #EQnth_opt #next_spec % | * [*] #EQnth_opt [#next_spec %1 % | %2] | #EQnth_opt ]
461        [1,3,5,7: @nth_opt_append_hit_l assumption
462        |2,4,6: @(eq_identifier_elim … nxt vis_hd)
463          [1,3: #EQ destruct(EQ) >lookup_add_hit whd [ %1 ]
464            lapply (in_map_domain … visited vis_hd)
465            >H3 #EQ >EQ in next_spec; * #_ #OK >OK %
466          |*: #NEQ' >(lookup_add_miss … visited … NEQ')
467            lapply (in_map_domain … visited nxt)
468            inversion (nxt ∈ visited) #nxt_vis [1,3: * #n'' ] #EQlookup'
469            >EQlookup' in next_spec; whd in ⊢ (%→%);
470            [ * [ #H %1{H} ] #H %2 @nth_opt_append_hit_l assumption
471            | #H @H
472            |*: * >H2
473              cases pre in H1;
474              [1,3: #_
475              |*: #frst #pre_tl * #ABS #_
476              ] whd in ⊢ (??%?→?); #EQ destruct(EQ)
477              [1,2: cases NEQ' #ABS cases (ABS ?) %
478              |*: >nxt_vis in ABS; *
479              ]
480            ]
481          ]
482        ]
483      ]
484    ]
485  | #i whd in match visited';
486    @(eq_identifier_elim … i vis_hd) #Heq
487    [ >Heq >lookup_add_hit #ABS destruct(ABS) ]
488    >(lookup_add_miss ?????? Heq)
489    #i_n_vis
490    >does_not_occur_append @andb_Prop
491    [ @generated_prf2 assumption
492    | change with (bool_to_Prop (¬eq_identifier ??? ∧ ?))
493      >eq_identifier_false [2: % #ABS <ABS in Heq; * #ABS' @ABS' % ]
494      @add_req_gen_prf [ #_ | #s #next #_ #_ ] %
495    ]
496  ]
497| @add_req_gen_prf
498  [ #K | #s #next #K #next_vis %1 %1 %{(GOTO … next)} % ]
499  whd in match generated'; whd in match translated_statement;
500  normalize nodelta
501  change with statement in match (lookup_safe ?????);
502  cases statement in K;
503  [ * [ #s | #a #ltrue ] #nxt | #s #_ %1 %1 %{s} % | * ] normalize nodelta
504  [2: #_ cases (true_or_false_Prop (nxt ∈ visited')) #nxt_vis
505    [ >nxt_vis normalize nodelta %1 %2 %{a} %{ltrue} %{nxt} % ]
506  | #nxt_vis ]
507  %2 % * >nxt_vis *
508| whd in match generated';
509  @add_req_gen_prf [ #_ | #s #next #_ #_ ] normalize >gen_length_prf %
510|
511]
512qed.
513
514(* CSC: The branch compression (aka tunneling) optimization is not implemented
515   in Matita *)
516definition branch_compress
517  ≝ λp: graph_params.λglobals.λg:codeT p globals.
518    λentry : Σl.bool_to_Prop (code_has_label … g l).g.
519 
520lemma branch_compress_closed : ∀p,globals,g,l.code_closed ?? g →
521  code_closed … (branch_compress p globals g l).
522#p#globals#g#l#H @H qed.
523
524lemma branch_compress_has_entry : ∀p,globals,g,l.
525  code_has_label … (branch_compress p globals g l) l.
526#p#globals#g*#l#l_prf @l_prf qed.
527
528definition filter_labels ≝ λtag,A.λtest : identifier tag → bool.λc : list (labelled_obj tag A).
529  map ??
530    (λs. let 〈l_opt,x〉 ≝ s in
531      〈! l ← l_opt ; if test l then return l else None ?, x〉) c.
532     
533lemma does_not_occur_filter_labels :
534  ∀tag,A,test,id,c.
535    does_not_occur ?? id (filter_labels tag A test c) =
536      (does_not_occur ?? id c ∨ ¬ test id).
537#tag #A #test #id #c elim c
538[ //
539| ** [2: #lbl] #s #tl #IH
540  whd in match (filter_labels ????); normalize nodelta
541  whd in match (does_not_occur ????) in ⊢ (??%%);
542  [2: @IH]
543  normalize in match (! l ← ? ; ?); >IH
544  @(eq_identifier_elim ?? lbl id) #Heq [<Heq]
545  elim (test lbl) normalize nodelta
546  change with (eq_identifier ???) in match (instruction_matches_identifier ????);
547  [1,2: >eq_identifier_refl [2: >commutative_orb] normalize %
548  |*: >(eq_identifier_false ??? Heq) normalize nodelta %
549  ]
550]
551qed.
552
553lemma occurs_exactly_once_filter_labels :
554  ∀tag,A,test,id,c.
555    occurs_exactly_once ?? id (filter_labels tag A test c) =
556      (occurs_exactly_once ?? id c ∧ test id).
557#tag #A #test #id #c elim c
558[ //
559| ** [2: #lbl] #s #tl #IH
560  whd in match (filter_labels ????); normalize nodelta
561  whd in match (occurs_exactly_once ????) in ⊢ (??%%);
562  [2: @IH]
563  normalize in match (! l ← ? ; ?); >IH
564  >does_not_occur_filter_labels
565  @(eq_identifier_elim ?? lbl id) #Heq [<Heq]
566  elim (test lbl) normalize nodelta
567  change with (eq_identifier ???) in match (instruction_matches_identifier ????);
568  [1,2: >eq_identifier_refl >commutative_andb [ >(commutative_andb ? true) >commutative_orb | >(commutative_andb ? false)] normalize %
569  |*: >(eq_identifier_false ??? Heq) normalize nodelta %
570  ]
571]
572qed.
573
574lemma nth_opt_filter_labels : ∀tag,A,test,instrs,n.
575  nth_opt ? n (filter_labels tag A test instrs) =
576  ! 〈lopt, s〉 ← nth_opt ? n instrs ;
577  return 〈 ! lbl ← lopt; if test lbl then return lbl else None ?, s〉.
578#tag #A #test #instrs elim instrs
579[ * [2: #n'] %
580| * #lopt #s #tl #IH * [2: #n']
581  whd in match (filter_labels ????); normalize nodelta
582  whd in match (nth_opt ???) in ⊢ (??%%); [>IH] %
583]
584qed.
585
586lemma stmt_at_filter_labels : ∀p : lin_params.∀globals,test.∀c : codeT p globals.
587∀i.stmt_at p globals (filter_labels ?? test c) i = stmt_at p globals c i.
588#p#globals#test #c#i
589whd in ⊢ (??%%); >nth_opt_filter_labels
590elim (nth_opt ???); //
591qed.
592
593(* spostato in ExtraMonads.ma
594lemma option_bind_inverse : ∀A,B.∀m : option A.∀f : A → option B.∀r.
595  ! x ← m ; f x = return r →
596  ∃x.m = return x ∧ f x = return r.
597#A #B * normalize [2:#x] #f #r #EQ destruct
598%{x} %{EQ} %
599qed.
600*)
601
602lemma nth_opt_reverse_hit :
603  ∀A,l,n.n < |l| → nth_opt A n (reverse ? l) = nth_opt A (|l| - (S n)) l.
604#A #l elim l
605[ #n #ABS normalize in ABS; @⊥ -A /2 by absurd/
606| #hd #tl #IH #n #lim whd in match (reverse ??); >rev_append_def
607  @(leb_elim (S n) (|tl|)) #H
608  [ >nth_opt_append_l [2: >length_reverse @H ]
609    >(IH … H) >(minus_Sn_m … H) %
610  | >(le_to_le_to_eq … (le_S_S_to_le … lim) (not_lt_to_le … H))
611    >nth_opt_append_r >length_reverse [2: % ]
612    <minus_n_n <minus_n_n %
613  ]
614]
615qed.
616
617lemma nth_opt_reverse_hit_inv :
618  ∀A,l,n.n < |l| → nth_opt A (|l| - (S n)) (reverse ? l) = nth_opt A n l.
619#A #l #n #H <(reverse_reverse ? l) in ⊢ (???%); @sym_eq
620<length_reverse @nth_opt_reverse_hit >length_reverse @H
621qed.
622
623definition good_local_sigma :
624  ∀p:unserialized_params.
625  ∀globals.
626  ∀g:codeT (mk_graph_params p) globals.
627  (Σl.bool_to_Prop (code_has_label … g l)) →
628  codeT (mk_lin_params p) globals →
629  (label → option ℕ) → Prop ≝
630  λp,globals,g,entry,c,sigma.
631    sigma entry = Some ? 0 ∧
632    (∀l,n.point_of_label … c l = Some ? n → sigma l = Some ? n) ∧
633    ∀l,n.sigma l = Some ? n →
634      ∃s. stmt_at … g l = Some ? s ∧
635          All ? (λl.sigma l ≠ None ?) (stmt_labels … s) ∧
636          (match s with
637           [ sequential s' nxt ⇒
638             match s' with
639             [ step_seq _ ⇒
640               (stmt_at … c n = Some ? (sequential … s' it)) ∧
641                  ((sigma nxt = Some ? (S n)) ∨
642                   (stmt_at … c (S n) = Some ? (GOTO … nxt)))
643             | COND a ltrue ⇒
644               (stmt_at … c n = Some ? (sequential … s' it) ∧ sigma nxt = Some ? (S n)) ∨
645               (stmt_at … c n = Some ? (FCOND … I a ltrue nxt))
646             ]
647           | final s' ⇒
648             stmt_at … c n = Some ? (final … s')
649           | FCOND abs _ _ _ ⇒ Ⓧabs
650           ]).
651
652definition linearise_code:
653 ∀p : unserialized_params.∀globals.
654  ∀g : codeT (mk_graph_params p) globals.code_closed … g →
655  ∀entry : (Σl.bool_to_Prop (code_has_label … g l))
656  .Σ〈c, sigma〉.
657    good_local_sigma … g entry c sigma ∧
658    code_closed … c
659       (* ∧
660      ∃ sigma : identifier_map LabelTag ℕ.
661      lookup … sigma entry = Some ? 0 ∧
662      ∀l,n.lookup … sigma l = Some ? n →
663        ∃s. lookup … g l = Some ? s ∧
664          opt_Exists ?
665            (λls.let 〈lopt, ts〉 ≝ ls in
666              opt_All ? (eq ? l) lopt ∧
667              ts = graph_to_lin_statement … s ∧
668              opt_All ?
669                (λnext.Or (lookup … sigma next = Some ? (S n))
670                (nth_opt … (S n) c = Some ? 〈None ?, GOTO … next〉))
671                (stmt_implicit_label … s)) (nth_opt … n c)*)
672
673 λp,globals,g,g_prf,entry_sig.
674    let g ≝ branch_compress (mk_graph_params p) ? g entry_sig in
675    let g_prf ≝ branch_compress_closed … g entry_sig g_prf in
676    match graph_visit p globals g ∅ (empty_map …) [ ] [entry_sig] 0 (|g|)
677      (entry_sig) g_prf ?????????
678    with
679    [ mk_Sig triple H ⇒
680      let sigma ≝ \fst (\fst triple) in
681      let required ≝ \snd (\fst triple) in
682      let crev ≝ \snd triple in
683      let lbld_code ≝ rev ? crev in
684      〈filter_labels … (λl.l∈required) lbld_code, lookup … sigma〉 ].
685[ cases (graph_visit ????????????????????)
686 (* done later *)
687| #i >mem_set_empty *
688|3,4: #a #b whd in ⊢ (??%?→?); #ABS destruct(ABS)
689| #l #_ %
690| %2 % * >mem_set_empty *
691| % [2: %] @(branch_compress_has_entry … g entry_sig)
692| %
693| % % [% %] cases (pi1 … entry_sig) normalize #_ % //
694| >commutative_plus change with (? ≤ |g|) %
695]
696cases daemon qed. (*
697**
698#visited #required #generated normalize nodelta ****
699#entry_O #req_vis #last_fin #labels_in_req #sigma_prop
700%
701[ % [ % [assumption]]
702  #lbl #n
703  [ change with (If ? then with prf do ? else ? = ? → ?)
704    @If_elim [2: #_ #ABS destruct(ABS) ]
705    #H lapply H
706    >occurs_exactly_once_filter_labels
707    elim (true_or_false_Prop … (occurs_exactly_once ?? lbl ?))
708    [1,2: #H1 >H1 |*:] [2: * ]
709    elim (true_or_false_Prop … (lbl ∈ required)) #H2 >H2 *
710    lapply (in_map_domain … visited lbl) >(req_vis … H2)
711    * #n_lbl #EQsigma
712    elim (sigma_prop … EQsigma) #_ * #stmt * #_
713    cases stmt [ * [ #s | #a #ltrue ] #nxt | #s | * ] normalize nodelta
714    [ * #EQnth_opt #_ | * [ * ] #EQnth_opt [ #_ ] | #EQnth_opt ]
715    >(nth_opt_index_of_label ???? n_lbl ? H)
716    [1,4,7,10: normalize in ⊢ (% → ?); #EQ destruct(EQ) assumption
717    |3: @(sequential … s it) |6: @(sequential … (COND … a ltrue) it)
718    |9: @(FCOND … I a ltrue nxt) |12: @(final … s)
719    |2,5,8,11: >nth_opt_filter_labels >EQnth_opt >m_return_bind >m_return_bind
720      >H2 %
721    ]
722  | #eq_lookup elim (sigma_prop ?? eq_lookup)
723    #lbl_in_gen * #stmt * #stmt_in_g #stmt_spec
724    % [2: % [ % [ assumption ]] |]
725    [ @All_append
726      [ cases stmt in stmt_spec;
727        [ * [ #s | #a #ltrue ] #nxt | #s #_ % | * ]
728        normalize nodelta * [ #stmt_at_EQ * #nxt_spec | * #stmt_at_EQ #nxt_spec | #stmt_at_EQ ]
729        %{I}
730        [1,3: >nxt_spec % #ABS destruct(ABS)
731        |*: lapply (in_map_domain … visited nxt)
732          >req_vis
733          [1,3: * #x #EQ >EQ % #ABS destruct(ABS)
734          |2: @(proj1 … (labels_in_req (S n) (GOTO … nxt) …))
735            whd in ⊢ (??%?); >nxt_spec %
736          |4: @(proj1 … (proj2 … (labels_in_req n (FCOND … I a ltrue nxt) …)))
737            whd in ⊢ (??%?); >stmt_at_EQ %
738          ]
739        ]
740      | cases stmt in stmt_spec;
741        [ * [ #s | #a #ltrue ] #nxt | #s | * ] normalize nodelta
742        [ * #EQ #_ | * [ * #EQ #_ | #EQ ] | #EQ ]
743        whd in match stmt_explicit_labels; normalize nodelta
744        [ @(All_mp … (labels_in_req n (sequential … s it) ?))
745        | @(All_mp … (labels_in_req n (sequential … (COND … a ltrue) it) ?))
746        | cases (labels_in_req n (FCOND … I a ltrue nxt) ?)
747          [ #l_req #_ %{I} lapply (in_map_domain … visited ltrue)
748            >(req_vis … l_req) * #n #EQ' >EQ' % #ABS destruct(ABS) ]
749        | @(All_mp … (labels_in_req n (final … s) ?))
750        ]
751        [1,3,6: #l #l_req >(lookup_eq_safe … (req_vis … l_req)) % #ABS destruct(ABS)
752        |*: whd in ⊢ (??%?); >EQ %
753        ]
754      ]
755    | cases stmt in stmt_spec;
756      [ * [ #s | #a #ltrue ] #nxt | #s | * ] normalize nodelta
757      [ * #EQ #H | * [ * #EQ #H | #EQ ] | #EQ ]
758      >stmt_at_filter_labels
759      whd in match (stmt_at (mk_lin_params p) ?? n); >EQ normalize nodelta
760      [ % [%] cases H -H [#H %1{H} | #EQ' %2 >stmt_at_filter_labels whd in ⊢ (??%?); >EQ' % ]
761      | %1 %{H} %
762      | %2 %
763      | %
764      ]
765    ]
766  ]
767| #i #s
768  >stmt_at_filter_labels #EQ
769  %
770  [ @stmt_forall_labels_explicit
771    @(All_mp … (labels_in_req … EQ))
772    #l #l_req >lin_code_has_label
773    >occurs_exactly_once_filter_labels >l_req
774    >commutative_andb whd in ⊢ (?%);
775    elim (sigma_prop ?? (lookup_eq_safe … (req_vis … l_req)))
776    >lin_code_has_label #H #_ @H
777  | lapply EQ cases s [ #s' * | #s' #_ % | * #a #ltrue #lfalse #_ % ]
778    -EQ #EQ change with (bool_to_Prop (code_has_point ????))
779    whd in match (point_of_succ ???);
780    >lin_code_has_point @leb_elim [ #_ % ] >length_map >length_reverse
781    #INEQ
782    cut (|generated| = S i)
783    [ @(le_to_le_to_eq … (not_lt_to_le … INEQ) )
784      elim (option_bind_inverse ????? EQ) #x * #EQ1 #EQ2
785      <length_reverse
786      @(nth_opt_hit_length … EQ1)
787    ] #EQ_length
788    elim last_fin * [ #fin | #a * #ltrue * #lfalse ] #EQ'
789    lapply EQ whd in match (stmt_at ????);
790    >nth_opt_reverse_hit >EQ_length [2,4: % ] <minus_n_n
791    change with (stmt_at ???? = ? → ?)
792    >EQ' #ABS destruct(ABS)
793  ]
794]
795qed.
796*)
797
798definition linearise_int_fun :
799  ∀p : unserialized_params.
800  ∀globals.
801    ∀fn_in : joint_closed_internal_function (mk_graph_params p) globals
802     .Σ〈fn_out : joint_closed_internal_function (mk_lin_params p) globals,
803        sigma : ?〉.
804        good_local_sigma … (joint_if_code … fn_in) (joint_if_entry … fn_in)
805          (joint_if_code … fn_out) sigma
806     (* ∃sigma : identifier_map LabelTag ℕ.
807        let g ≝ joint_if_code ?? (pi1 … fin) in
808        let c ≝ joint_if_code ?? (pi1 … fout) in
809        let entry ≝ joint_if_entry ?? (pi1 … fin) in
810         lookup … sigma entry = Some ? 0 ∧
811          ∀l,n.lookup … sigma l = Some ? n →
812            ∃s. lookup … g l = Some ? s ∧
813              opt_Exists ?
814                (λls.let 〈lopt, ts〉 ≝ ls in
815                  opt_All ? (eq ? l) lopt ∧
816                  ts = graph_to_lin_statement … s ∧
817                  opt_All ?
818                    (λnext.Or (lookup … sigma next = Some ? (S n))
819                    (nth_opt … (S n) c = Some ? 〈None ?, GOTO … next〉))
820                    (stmt_implicit_label … s)) (nth_opt … n c)*) ≝
821  λp,globals,f_sig.
822  let code_sigma ≝ linearise_code …
823    (joint_if_code … f_sig)
824    (pi2 … f_sig)
825    (joint_if_entry … f_sig) in
826  let code ≝ \fst code_sigma in
827  let sigma ≝ \snd code_sigma in
828  let entry : Σpt.bool_to_Prop (code_has_point … code pt) ≝ «0, hide_prf ??» in
829  〈«mk_joint_internal_function (mk_lin_params p) globals
830   (joint_if_luniverse ?? f_sig) (joint_if_runiverse ?? f_sig)
831   (joint_if_result ?? f_sig) (joint_if_params ?? f_sig) (joint_if_locals ?? f_sig)
832   (joint_if_stacksize ?? f_sig) code entry entry (* exit is dummy! *), ?»,
833   sigma〉.
834normalize nodelta
835cases (linearise_code ?????) * #code #sigma normalize nodelta * #H1 #H2
836[ @H2
837| @H1
838| cases H1 * #H3 #H4 #H5 elim (H5 … H3)
839  #s ** #_ #_ >lin_code_has_point cases code
840  [ cases s [ * [ #s' | #a #ltrue ] #nxt | #s' | * ]
841    [ * #ABS #_ | * [ * #ABS #_ | #ABS ] | #ABS ] normalize in ABS; destruct(ABS)
842  | #hd #tl #_ %
843  ]
844]
845qed.
846
847definition linearise : ∀p : unserialized_params.
848  program (joint_function (mk_graph_params p)) ℕ →
849  program (joint_function (mk_lin_params p)) ℕ
850   ≝
851  λp,pr.transform_program ??? pr
852    (λglobals.transf_fundef ?? (λf_in.\fst (linearise_int_fun p globals f_in))).
853
854(*
855definition good_sigma :
856  ∀p:unserialized_params.
857  ∀prog_in : joint_program (mk_graph_params p).
858  ((Σi.is_internal_function_of_program … prog_in i) → label → option ℕ) → Prop ≝
859  λp,prog_in,sigma.
860  let prog_out ≝ linearise … prog_in in
861  ∀i : Σi.is_internal_function_of_program … prog_in i.∀prf.
862  let fn_in ≝ if_of_function … i in
863  let i' : Σi.is_internal_function_of_program … prog_out i ≝ «pi1 ?? i, prf» in
864  let fn_out ≝ if_of_function … i' in
865  let sigma_local ≝ sigma i in
866  good_local_sigma ?? (joint_if_code ?? fn_in) (joint_if_entry … fn_in)
867          (joint_if_code ?? fn_out) sigma_local.
868
869lemma linearise_spec : ∀p,prog.∃sigma.good_sigma p prog sigma.
870#p #prog
871letin sigma ≝
872  (λi : Σi.is_internal_function_of_program … prog i.    let fn_in ≝ if_of_function … i in
873    \snd (linearise_int_fun … fn_in))
874%{sigma}
875#i #prf >(prog_if_of_function_transform … i) [2: % ]
876normalize nodelta
877cases (linearise_int_fun ???) * #fn_out #sigma_loc
878normalize nodelta #prf @prf
879qed.
880*)
Note: See TracBrowser for help on using the repository browser.