source: src/joint/linearise.ma @ 2757

Last change on this file since 2757 was 2712, checked in by tranquil, 7 years ago

changed some fields of joint_internal_function's invariant
fixed linearise

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