source: LTS/Language.ma @ 3447

Last change on this file since 3447 was 3447, checked in by piccolo, 6 years ago

correctness proof in place

File size: 108.4 KB
Line 
1(**************************************************************************)
2(*       ___                                                              *)
3(*      ||M||                                                             *)
4(*      ||A||       A project by Andrea Asperti                           *)
5(*      ||T||                                                             *)
6(*      ||I||       Developers:                                           *)
7(*      ||T||         The HELM team.                                      *)
8(*      ||A||         http://helm.cs.unibo.it                             *)
9(*      \   /                                                             *)
10(*       \ /        This file is distributed under the terms of the       *)
11(*        v         GNU General Public License Version 2                  *)
12(*                                                                        *)
13(**************************************************************************)
14
15include "basics/types.ma".
16include "Traces.ma".
17include "basics/lists/list.ma".
18include "../src/utilities/option.ma".
19include "basics/jmeq.ma".
20
21discriminator option.
22
23record instr_params : Type[1] ≝
24{ seq_instr : DeqSet
25; io_instr : DeqSet
26; cond_instr : DeqSet
27; loop_instr : DeqSet
28; act_params_type : DeqSet
29; return_type : DeqSet
30}.
31
32
33inductive Instructions (p : instr_params) : Type[0] ≝
34 | EMPTY : Instructions p
35 | RETURN : return_type p → Instructions p
36 | SEQ : (seq_instr p) → option NonFunctionalLabel → Instructions p → Instructions p
37 | COND : (cond_instr p) → NonFunctionalLabel → Instructions p →
38                 NonFunctionalLabel → Instructions p → Instructions p →
39                       Instructions p
40 | LOOP : (loop_instr p) → NonFunctionalLabel → Instructions p →
41                  NonFunctionalLabel → Instructions p → Instructions p
42 | CALL : FunctionName → (act_params_type p) → option ReturnPostCostLabel →
43            Instructions p → Instructions p
44 | IO : NonFunctionalLabel → (io_instr p) → NonFunctionalLabel → Instructions p →
45             Instructions p.
46
47let rec eq_instructions (p : instr_params) (i : Instructions p)
48 on i : (Instructions p) → bool ≝
49match i with
50[ EMPTY ⇒ λi'.match i' with [ EMPTY ⇒ true | _ ⇒ false ]
51| RETURN x ⇒ λi'.match i' with [ RETURN y ⇒ x == y | _ ⇒ false ]
52| SEQ x lab instr ⇒ λi'.match i' with
53                      [ SEQ y lab' instr' ⇒ x == y ∧ eq_instructions … instr instr' ∧
54                              match lab with [ None ⇒ match lab' with [ None ⇒ true | _ ⇒ false ]
55                                             | Some l1 ⇒ match lab' with [Some l2 ⇒ eq_nf_label l1 l2 | _ ⇒ false]
56                                             ]
57                      | _ ⇒ false
58                      ]
59| COND x ltrue i1 lfalse i2 i3 ⇒ λi'.match i' with
60                          [ COND y ltrue' i1' lfalse' i2' i3' ⇒
61                             x == y ∧ eq_nf_label ltrue ltrue' ∧
62                             eq_instructions … i1 i1' ∧ eq_nf_label lfalse lfalse' ∧
63                             eq_instructions … i2 i2' ∧ eq_instructions … i3 i3'
64                         | _ ⇒ false
65                         ]
66| LOOP x ltrue i1 lfalse i2 ⇒ λi'.match i' with
67              [ LOOP y ltrue' i1' lfalse' i2' ⇒ x == y ∧
68                      eq_instructions … i1 i1' ∧ eq_nf_label ltrue ltrue' ∧
69                      eq_instructions … i2 i2'
70              | _ ⇒ false
71              ]
72| CALL f act_p r_lb i1 ⇒ λi'.match i' with
73             [ CALL f' act_p' r_lb' i1' ⇒ eq_function_name f f' ∧
74                       act_p == act_p' ∧ eq_instructions … i1 i1' ∧
75                       match r_lb with [ None ⇒ match r_lb' with [None ⇒ true | _ ⇒ false]
76                                       | Some z ⇒ match r_lb' with [Some w ⇒  eq_return_cost_lab z w | _ ⇒ false ]
77                                       ]
78            | _ ⇒ false
79            ]
80| IO lin io lout i1 ⇒ λi'.match i' with
81             [ IO lin' io' lout' i1' ⇒ eq_nf_label lin lin' ∧ io == io' ∧
82                                       eq_nf_label lout lout' ∧ eq_instructions … i1 i1'
83             | _ ⇒ false                                       
84             ]
85].
86
87(*
88lemma eq_instructions_elim : ∀ P : bool → Prop.∀p,i1,i2.(i1 = i2 → P true) →
89(i1 ≠ i2 → P false) → P (eq_instructions p i1 i2).
90#P #p #i1 elim i1
91[* normalize [/2/ ] #x [|*: #y #z [2,3: #w1 #w2 [#w3] |4,5: #w1]] #_ #H2 @H2 % #EQ
92  lapply (eq_to_jmeq ??? EQ) #EQ' destruct(EQ')
93| #rt * normalize [2: #rt' cases (dec_eq … rt rt') #H [>(\b H) | >(\bf H) ] /2/
94  #_ #K @K % #abs lapply (eq_to_jmeq ??? abs) #abs' destruct(abs') @(absurd ?? H) //]
95  [|*: #x #y #z [2,3: #w1 #w2 [#w3] |4,5: #w1]] #_ #H2 @H2 % #EQ
96  lapply (eq_to_jmeq ??? EQ) #EQ' destruct(EQ')
97| cases daemon (*TODO*)
98qed.
99*)
100
101record env_params : Type[1] ≝
102{ form_params_type : Type[0]
103}.
104
105record signature (p : env_params) (p' : instr_params) : Type[0] ≝
106{ f_name : FunctionName
107; f_pars : form_params_type p
108; f_ret : return_type p'
109}.
110
111record env_item (p : env_params) (p' : instr_params) : Type[0] ≝
112{ f_sig :> signature p p'
113; f_lab : CallCostLabel
114; f_body : Instructions p'
115}.
116
117record state_params : Type[1] ≝
118{ i_pars :> instr_params
119; e_pars :> env_params
120; store_type : DeqSet
121}.
122
123record state (p : state_params) : Type[0] ≝
124{ code : Instructions p
125; cont : list (ActionLabel × (Instructions p))
126; store : store_type p
127; io_info : bool
128}.
129
130definition is_io : ∀p.state p → Prop ≝ λp,st.io_info … st = true.
131
132record sem_state_params (p : state_params) : Type[0] ≝
133{ eval_seq : seq_instr p → (store_type p) → option (store_type p)
134; eval_io : io_instr p → store_type p → option (store_type p)
135; eval_cond_cond : cond_instr p → store_type p → option (bool × (store_type p))
136; eval_loop_cond : loop_instr p → store_type p → option (bool × (store_type p))
137; eval_call : signature p p → act_params_type p → store_type p → option (store_type p)
138; eval_after_return : return_type p → store_type p → option (store_type p)
139; init_store : store_type p
140}.
141
142
143let rec lookup (p : env_params) (p' : instr_params) (l : list (env_item p p'))
144 on l : FunctionName → option (env_item p p') ≝
145match l with
146[ nil ⇒ λ_.None ?
147| cons x xs ⇒  λf.if (eq_function_name f (f_name … x))
148                  then Some ? x
149                  else lookup … xs f
150].
151
152definition is_ret_act : ActionLabel → Prop ≝
153λa.match a with [ret_act _ ⇒ True | _ ⇒ False ].
154
155inductive execute_l (p : state_params) (p' : sem_state_params p) (env : list (env_item p p)) :
156                                         ActionLabel → relation (state p) ≝
157| empty : ∀st,st',hd,tl.(code ? st) = (EMPTY p)→ (cont ? st) = hd :: tl →
158           (code ? st') = \snd hd → (cont … st') = tl → (store … st) = (store … st') →
159           (io_info … st = true → is_non_silent_cost_act (\fst hd)) →  (io_info … st') = false → ¬ is_ret_act (\fst hd) →  execute_l … (\fst hd) st st'
160| seq_sil : ∀st,st',i,cd,s,opt_l.(code ? st) = SEQ … i opt_l cd →
161             eval_seq … p' i (store … st) = return s → (code ? st') = cd →
162             (cont … st) = (cont … st') → (store … st') = s →
163             io_info … st = false →  io_info ? st' = false → execute_l … (cost_act opt_l) st st'
164| cond_true : ∀st,st',exp,ltrue,i_true,lfalse,i_false,cd,new_m.
165   (code ? st) = COND … exp ltrue i_true lfalse i_false cd → eval_cond_cond … p' exp (store … st) = return 〈true,new_m〉 →
166   cont ? st' = 〈cost_act (None ?),cd〉 ::(cont … st) → code … st' = i_true → store … st' = new_m →
167   io_info … st = false →  io_info … st' = false → execute_l … (cost_act (Some ? ltrue)) st st'
168| cond_false : ∀st,st',exp,ltrue,i_true,lfalse,i_false,cd,new_m.
169   (code ? st) = COND … exp ltrue i_true lfalse i_false cd → eval_cond_cond … p' exp (store … st) = return 〈false,new_m〉 →
170   cont ? st' = 〈cost_act (None ?),cd〉 ::(cont … st) → code … st' = i_false → store … st' = new_m →
171   io_info … st = false →  io_info … st' = false → execute_l … (cost_act (Some ? lfalse)) st st'
172| loop_true : ∀st,st',exp,ltrue,i_true,lfalse,i_false,new_m.
173   code ? st = LOOP … exp ltrue i_true lfalse i_false → eval_loop_cond … p' exp (store … st) = return 〈true,new_m〉 →
174   cont ? st' = 〈cost_act (None ?),LOOP … exp ltrue i_true lfalse i_false〉 :: (cont … st) →
175   code … st' = i_true → store … st' = new_m → io_info … st = false →  io_info … st' = false →
176   execute_l … (cost_act (Some ? ltrue)) st st'
177| loop_false : ∀st,st',exp,ltrue,i_true,lfalse,i_false,new_m.
178   code ? st = LOOP … exp ltrue i_true lfalse i_false → eval_loop_cond … p' exp (store … st) = return 〈false,new_m〉 →
179   cont ? st' = cont … st → code … st' = i_false → store … st' = new_m →
180   io_info … st = false →  io_info … st' = false → execute_l … (cost_act (Some ? lfalse)) st st'
181| io_in : ∀st,st',lin,io,lout,cd,mem.(code ? st) = IO … lin io lout cd →
182    eval_io … p' io (store … st) = return mem → code ? st' = EMPTY p →
183    cont … st' = 〈cost_act (Some ? lout),cd〉 :: (cont … st) → store … st' = mem →
184    io_info … st' = true → execute_l … (cost_act (Some ? lin)) st st'
185| call : ∀st,st',f,act_p,r_lb,cd,mem,env_it.(code ? st) = CALL … f act_p r_lb cd →
186    lookup … env f = return env_it →
187    eval_call ? p' env_it act_p (store … st) = return mem →
188    store ? st' = mem → code … st' = f_body … env_it →
189     cont … st' =
190       〈(ret_act r_lb),cd〉 :: (cont … st) → 
191    io_info … st = false →  (io_info … st') = false →
192    execute_l … (call_act f (f_lab ?? env_it)) st st'
193| ret_instr : ∀st,st',r_t,mem,tl',rb,cd.code ? st = RETURN … r_t →
194   cont … st = 〈ret_act rb,cd〉 :: tl' → cont ? st' = tl' →
195   io_info … st = false →  io_info ? st' = false →
196   eval_after_return … p' r_t (store … st) = return mem → code … st' = cd →
197   store … st' = mem → execute_l … (ret_act rb) st st'.
198   
199let rec get_labels_of_code (p : instr_params) (i : Instructions p) on i : list CostLabel ≝
200match i with
201[ EMPTY ⇒ [ ]
202| RETURN x ⇒ [ ]
203| SEQ x lab instr ⇒ let ih ≝ get_labels_of_code … instr in
204  match lab with [ None ⇒ ih | Some lbl ⇒ a_non_functional_label lbl :: ih ]
205| COND x ltrue i1 lfalse i2 i3 ⇒
206   let ih3 ≝ get_labels_of_code … i3 in
207   let ih2 ≝ get_labels_of_code … i2 in
208   let ih1 ≝ get_labels_of_code … i1 in
209   ltrue :: lfalse :: (ih1 @ ih2 @ih3)
210| LOOP x ltrue i1 lfalse i2 ⇒
211   let ih2 ≝ get_labels_of_code … i2 in
212   let ih1 ≝ get_labels_of_code … i1 in
213   a_non_functional_label ltrue :: a_non_functional_label lfalse :: (ih1 @ ih2)
214| CALL f act_p r_lb i1 ⇒
215   let ih1 ≝ get_labels_of_code … i1 in
216   match r_lb with [ None ⇒ ih1 | Some lbl ⇒ a_return_post lbl :: ih1]
217| IO lin io lout i1 ⇒
218   let ih1 ≝ get_labels_of_code … i1 in
219   a_non_functional_label lin :: a_non_functional_label lout :: ih1
220].
221
222include "basics/lists/listb.ma".
223include "../src/utilities/hide.ma".
224
225let rec no_duplicates (A : DeqSet) (l : list A) on l : Prop ≝
226match l with
227[ nil ⇒ True
228| cons x xs ⇒ ¬ (bool_to_Prop (x ∈ xs)) ∧ no_duplicates … xs
229].
230
231lemma no_duplicates_append_r : ∀A : DeqSet.∀l1,l2 : list A.no_duplicates … (l1 @ l2) →
232no_duplicates … l2.
233#A #l1 elim l1 // #x #xs normalize #IH #l2 * /2/
234qed.
235
236lemma no_duplicates_append_l : ∀A : DeqSet.∀l1,l2 : list A.no_duplicates … (l1 @ l2) →
237no_duplicates … l1.
238#A #l1 elim l1 // #x #xs normalize #IH #l2 * #H1 #H2 % [2: /2/ ]
239inversion(x ∈ xs @l2) in H1; normalize [ #_ * #H @⊥ @H %] #H1 #_
240% inversion(x ∈ xs) normalize [2: //] #H3 #_ >(memb_append_l1 … H3) in H1;
241#EQ destruct(EQ)
242qed.
243   
244record Program (p : env_params) (p' : instr_params) : Type[0] ≝
245{ env : list (env_item p p')
246; main : Instructions p'
247}.
248
249
250definition no_duplicates_labels : ∀p,p'.Program p p' → Prop ≝
251λp,p',prog.
252   no_duplicates …
253    (foldr … (λitem,acc.((a_call (f_lab … item)) :: get_labels_of_code … (f_body … item)) @ acc) (get_labels_of_code … (main … prog)) (env … prog)).
254
255lemma no_duplicates_domain_of_fun:
256 ∀p,p',prog.no_duplicates_labels … prog →
257 ∀f,env_it.lookup p p' (env … prog) f = return env_it →
258 no_duplicates … (get_labels_of_code … (f_body … env_it)).
259#p #p' * #env elim env [ #main normalize #_ #f #env_it #EQ destruct(EQ)]
260#x #xs #IH #main whd in ⊢ (% → ?); whd in match (foldr ?????); #H #f #env_it
261whd in ⊢ (??%? → ?); @eq_function_name_elim normalize nodelta
262[ whd in ⊢ (? → ???% → ?); #EQ1 #EQ2 destruct(EQ1 EQ2) cases H #_ /2/ ]
263#H1 #EQenv_it @IH cases H /2/
264qed.
265
266
267definition is_synt_succ : ∀p.relation (state p) ≝ λp,s1,s2.cont … s1 = cont … s2 ∧
268 match (code … s1) with
269 [ CALL f act_p r_lb i1 ⇒ code … s2 = i1
270 | _ ⇒ False
271 ].
272
273definition operational_semantics : ∀p : state_params.∀p'.Program p p → abstract_status ≝
274λp,p',prog.mk_abstract_status
275                (state p)
276                (execute_l ? p' (env … prog))
277                (is_synt_succ …)
278                (λs.match (code … s) with
279                    [ COND _ _ _ _ _ _ ⇒ cl_jump
280                    | LOOP _ _ _ _ _ ⇒ cl_jump
281                    | EMPTY ⇒ if io_info … s then cl_io else cl_other
282                    | _ ⇒ cl_other
283                    ])
284                (λs.match (code … s) with
285                    [CALL _ _ m _ ⇒ match m with [ Some _ ⇒ true | None ⇒ false ]
286                    | _ ⇒ false
287                    ])
288                (λs.eq_instructions … (code … s) (main … prog) ∧ isnilb … (cont … s) ∧ store … s == init_store … p' ∧ io_info … s)
289                (λs.match (cont … s) with
290                    [ nil ⇒ match (code … s) with
291                            [ EMPTY ⇒ true
292                            | RETURN _ ⇒ true
293                            | _ ⇒ false
294                            ]
295                    | _ ⇒ false
296                    ])
297                ???.
298@hide_prf
299[ #s1 #s2 #l #H #H1 inversion H1 #st #st'
300 [ #hd #tl
301 | #i #cd #s #opt_l
302 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
303 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
304 | #lin #io #lout #cd #mem
305 | #f #act_p #r_lb #cd #mem #env_it
306 | #r_t #mem #tl #rb #cd
307 ]
308 #EQcode
309 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQ5 #H2 #EQ' #EQ6 #EQ7
310 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
311 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
312 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
313 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
314 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
315 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8
316 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
317 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
318 ]
319 #_ destruct >EQcode in H; normalize nodelta /2 by ex_intro/
320 [ cases(io_info ??) normalize nodelta] #EQ destruct
321| #s1 #s2 #l #H #H1 inversion H1 #st #st'
322 [ #hd #tl
323 | #i #cd #s #opt_l
324 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
325 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
326 | #lin #io #lout #cd #mem
327 | #f #act_p #r_lb #cd #mem #env_it
328 | #r_t #mem #tl #rb #cd
329 ]
330 #EQcode
331 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQiost' #H2 #EQ' #EQ6 #EQ7
332 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
333 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
334 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
335 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
336 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
337 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost' #EQ6 #EQ7 #EQ8
338 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQiost' #EQ8 #EQ9 #EQ10
339 | #EQ1 #EQ2 #EQ3 #EQiost' #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
340 ]
341 #_ destruct
342 cases(code ? st') in H; normalize nodelta >EQiost' normalize nodelta
343 #eq destruct try (#eq1 destruct) try (#eq2 destruct) try (#eq3 destruct)
344 try (#eq4 destruct) try (#eq5 destruct) try (#eq6 destruct) %{lin} %
345| #s1 #s2 #l #H #H1 inversion H1 #st #st'
346 [ #hd #tl
347 | #i #cd #s #opt_l
348 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
349 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
350 | #lin #io #lout #cd #mem
351 | #f #act_p #r_lb #cd #mem #env_it
352 | #r_t #mem #tl #rb #cd
353 ]
354 #EQcode
355 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQiost' #H2 #EQ' #EQ6 #EQ7
356 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
357 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
358 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
359 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
360 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
361 | #EQ1 #EQ2 #EQ3 #EQiost #EQiost' #EQ6 #EQ7 #EQ8
362 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost #EQiost' #EQ8 #EQ9 #EQ10
363 | #EQ1 #EQ2 #EQiost #EQiost' #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
364 ]
365 #_ destruct >EQcode in H; normalize nodelta [|*: #EQ destruct]
366 cases(io_info … st) in x; normalize nodelta [2: #_ #EQ destruct]
367 #H3 #_ @H3 %
368]
369qed.
370
371let rec eqb_list (D : DeqSet) (l1 : list D) on l1 : list D → bool ≝
372match l1 with
373[ nil ⇒ λl2.match l2 with [nil ⇒ true | _ ⇒ false ]
374| cons x xs ⇒ λl2.match l2 with [ cons y ys ⇒ x == y ∧ eqb_list … xs ys | _ ⇒ false ]
375].
376
377definition DeqSet_List : DeqSet → DeqSet ≝
378λX.mk_DeqSet (list X) (eqb_list …) ?.
379#x elim x [ * /2 by refl, conj/ #y #ys normalize % #EQ destruct] -x
380#x #xs #IH * [ normalize % #EQ destruct] #y #ys normalize % inversion(x == y)
381#EQ normalize nodelta
382[ #H >(proj1 … (IH ys) H) @eq_f2 [2: %] @(proj1 … (eqb_true …)) assumption
383| #EQ destruct
384| #EQ1 destruct @(proj2 … (IH …)) %
385| #EQ1 destruct <EQ @(proj2 … (eqb_true …)) %
386]
387qed.
388
389unification hint  0 ≔ C;
390    X ≟ DeqSet_List C
391(* ---------------------------------------- *) ⊢
392    list C ≡ carr X.
393
394
395unification hint  0 ≔ D,p1,p2;
396    X ≟ DeqSet_List D
397(* ---------------------------------------- *) ⊢
398    eqb_list D p1 p2 ≡ eqb X p1 p2.
399
400definition associative_list : DeqSet → Type[0] → Type[0] ≝
401λA,B.list (A × (list B)).
402
403let rec update_list (A : DeqSet) (B : Type[0]) (l : associative_list A B)
404   on l : A → list B → associative_list A B ≝
405λa,b.match l with
406    [ nil ⇒ [〈a,b〉]
407    | cons x xs ⇒ if (a == (\fst x)) then 〈a,b〉 :: xs
408                  else x :: (update_list … xs a b) 
409    ].
410
411let rec get_element (A :DeqSet) (B : Type[0]) (l: associative_list A B) on l : A → list B ≝
412λa.match l with [ nil ⇒ nil ?
413                | cons x xs ⇒ if (a == \fst x) then \snd x else get_element … xs a
414                ].
415
416let rec domain_of_associative_list (A :DeqSet) (B : Type[0]) (l: associative_list A B) on l : list A ≝
417 match l with
418  [ nil ⇒ []
419  | cons x xs ⇒ \fst x :: domain_of_associative_list … xs
420  ].
421
422lemma get_element_append_l:
423 ∀A,B. ∀l1,l2: associative_list A B. ∀x.
424  x ∈ domain_of_associative_list … l1 →
425   get_element … (l1@l2) x = get_element … l1 x.
426#A #B #l1 elim l1 normalize [ #l2 #x * ] #hd #tl #IH #l2 #x cases (dec_eq … x (\fst hd))
427#H [ >(\b H) | >(\bf H) ] normalize /2/
428qed.
429
430lemma get_element_append_r:
431 ∀A,B. ∀l1,l2: associative_list A B. ∀x.
432  ¬ (bool_to_Prop (x ∈ domain_of_associative_list … l1)) →
433   get_element ?? (l1@l2) x = get_element … l2 x.
434#A #B #l1 elim l1 normalize [ #l2 #x // ] #hd #tl #IH #l2 #x cases (dec_eq … x (\fst hd))
435#H [ >(\b H) | >(\bf H) ] normalize /2 by/ * #K cases (K I)
436qed.
437
438lemma get_element_append_l1 :
439 ∀A,B. ∀l1,l2: associative_list A B. ∀x.
440  ¬ (bool_to_Prop (x ∈ domain_of_associative_list … l2)) →
441   get_element ?? (l1@l2) x = get_element … l1 x.
442#A #B #l1 elim l1 normalize [2: #x #xs #IH #l2 #a #H >IH // ]
443#l2 elim l2 // #y #ys #IH #a normalize cases(a == \fst y) normalize
444[ * #H @⊥ @H % ] #H @IH assumption
445qed.
446
447lemma get_element_append_r1 :
448 ∀A,B. ∀l1,l2: associative_list A B. ∀x.
449  ¬ (bool_to_Prop (x ∈ domain_of_associative_list … l1)) →
450   get_element ?? (l1@l2) x = get_element … l2 x.
451#A #B #l1 elim l1 normalize // #x #xs #IH #l2 #a cases (?==?)
452normalize [* #H cases H //] #H >IH normalize //
453qed.
454
455definition fresh_nf_label : ℕ → NonFunctionalLabel × ℕ ≝
456λx.〈a_non_functional_label x,S x〉.
457
458definition fresh_cc_labe : ℕ → CallCostLabel × ℕ ≝
459λx.〈a_call_label x,S x〉.
460
461definition fresh_rc_label : ℕ → ReturnPostCostLabel × ℕ ≝
462λx.〈a_return_cost_label (S x),S x〉.
463
464record call_post_info (p : instr_params) : Type[0] ≝
465{ gen_labels : list CostLabel
466; t_code : Instructions p
467; fresh : ℕ
468; lab_map : associative_list DEQCostLabel CostLabel
469; lab_to_keep : list ReturnPostCostLabel
470}.
471
472let rec call_post_trans (p : instr_params) (i : Instructions p) (n : ℕ) on i :
473list CostLabel → call_post_info p ≝
474λabs.
475match i with
476[ EMPTY ⇒ mk_call_post_info ? abs (EMPTY …) n (nil ?) (nil ?)
477| RETURN x ⇒ mk_call_post_info ? abs (RETURN … x) n (nil ?) (nil ?)
478| SEQ x lab instr ⇒
479   let ih ≝ call_post_trans … instr n abs in
480   match lab with
481   [ None ⇒ mk_call_post_info ? (gen_labels … ih) (SEQ … x (None ?) (t_code … ih))
482             (fresh … ih) (lab_map … ih) (lab_to_keep … ih)
483   | Some lbl ⇒
484      mk_call_post_info ? (nil ?) (SEQ … x (Some ? lbl) (t_code …  ih)) (fresh … ih)
485      (〈a_non_functional_label lbl,(a_non_functional_label lbl :: (gen_labels … ih))〉 :: (lab_map … ih))
486      (lab_to_keep … ih)
487   ]
488| COND x ltrue i1 lfalse i2 i3 ⇒
489   let ih3 ≝ call_post_trans … i3 n abs in
490   let ih2 ≝ call_post_trans … i2 (fresh … ih3) (gen_labels … ih3) in
491   let ih1 ≝ call_post_trans … i1 (fresh … ih2) (gen_labels … ih3) in
492   mk_call_post_info ? (nil ?) (COND … x ltrue (t_code … ih1) lfalse (t_code … ih2) (t_code … ih3))
493    (fresh … ih1) 
494    (〈a_non_functional_label ltrue,(a_non_functional_label ltrue :: (gen_labels … ih1))〉::
495      〈a_non_functional_label lfalse,(a_non_functional_label lfalse :: (gen_labels … ih2))〉::
496       ((lab_map … ih1) @ (lab_map …  ih2) @ (lab_map … ih3)))
497    ((lab_to_keep … ih1) @ (lab_to_keep … ih2) @ (lab_to_keep … ih3))
498| LOOP x ltrue i1 lfalse i2 ⇒
499   let ih2 ≝ call_post_trans … i2 n abs in
500   let ih1 ≝ call_post_trans … i1 (fresh … ih2) (nil ?) in
501   mk_call_post_info ? (nil ?) (LOOP … x ltrue (t_code … ih1) lfalse (t_code … ih2)) (fresh … ih1)
502    (〈a_non_functional_label lfalse,(a_non_functional_label lfalse :: (gen_labels … ih2))〉 ::
503     〈a_non_functional_label ltrue,(a_non_functional_label ltrue :: (gen_labels … ih1))〉 ::
504      ((lab_map … ih1) @ (lab_map … ih2)))
505    ((lab_to_keep … ih1) @ (lab_to_keep … ih2))
506| CALL f act_p r_lb i1 ⇒
507   let ih ≝ call_post_trans … i1 n abs in
508   match r_lb with
509   [ None ⇒ let 〈l',f''〉 ≝ fresh_rc_label (fresh … ih) in
510       mk_call_post_info ? ((a_return_post l')::(gen_labels … ih))
511         (CALL … f act_p (Some ? l') (t_code … ih))  f'' (lab_map … ih) (lab_to_keep … ih)
512   | Some lbl ⇒
513      mk_call_post_info ? (nil ?) (CALL ? f act_p (Some ? lbl) (t_code … ih)) (fresh … ih)
514       (〈a_return_post lbl,(a_return_post lbl :: (gen_labels … ih))〉 :: (lab_map … ih))
515       (lbl :: lab_to_keep … ih)
516   ]
517| IO lin io lout i1 ⇒
518    let ih ≝ call_post_trans … i1 n abs in
519    mk_call_post_info ? (nil ?) (IO ? lin io lout (t_code … ih)) (fresh … ih)
520     (〈a_non_functional_label lout,(a_non_functional_label lout :: (gen_labels … ih))〉 ::
521      〈a_non_functional_label lin,[a_non_functional_label lin]〉 :: (lab_map … ih)) (lab_to_keep … ih)
522].
523
524
525let rec call_post_clean (p : instr_params) (i : Instructions p) on i :
526associative_list DEQCostLabel CostLabel → list ReturnPostCostLabel → list CostLabel →
527option ((list CostLabel) × (Instructions p)) ≝
528λm,keep,abs.
529 match i with
530[ EMPTY ⇒ Some ? 〈abs,EMPTY …〉
531| RETURN x ⇒ Some ? 〈abs,RETURN … x〉
532| SEQ x lab instr ⇒
533   ! 〈l,i1〉 ← call_post_clean … instr m keep abs;
534   match lab with
535   [ None ⇒ return 〈l,SEQ … x (None ?) i1〉
536   | Some lbl ⇒ if ((get_element … m lbl) == lbl :: l)
537                then return 〈nil ?,SEQ … x (Some ? lbl) i1〉
538                else None ?
539   ]
540| COND x ltrue i1 lfalse i2 i3 ⇒
541    ! 〈l3,instr3〉 ← call_post_clean … i3 m keep abs;
542    ! 〈l2,instr2〉 ← call_post_clean … i2 m keep l3;
543    ! 〈l1,instr1〉 ← call_post_clean … i1 m keep l3;
544    if ((get_element … m ltrue) == ltrue :: l1) ∧
545       ((get_element … m lfalse) == lfalse :: l2)
546    then return 〈nil ?,COND … x ltrue instr1 lfalse instr2 instr3〉
547    else None ?
548| LOOP x ltrue i1 lfalse i2 ⇒
549   ! 〈l2,instr2〉 ← call_post_clean … i2 m keep abs;
550   ! 〈l1,instr1〉 ← call_post_clean … i1 m keep (nil ?);
551   if ((get_element … m ltrue) == ltrue :: l1) ∧
552      ((get_element … m lfalse) == lfalse :: l2)
553   then return 〈nil ?,LOOP … x ltrue instr1 lfalse instr2〉
554   else None ?
555| CALL f act_p r_lb i1 ⇒
556  ! 〈l1,instr1〉 ← call_post_clean … i1 m keep abs;
557  match r_lb with
558  [ None ⇒ None ?
559  | Some lbl ⇒ if (lbl ∈ keep)
560               then if ((get_element … m lbl) == lbl :: l1)
561                    then return 〈nil ?,CALL … f act_p (Some ? lbl) instr1〉
562                    else None ?
563               else return 〈(a_return_post lbl) :: l1,CALL … f act_p (None ?) instr1〉
564  ]
565| IO lin io lout i1 ⇒
566   ! 〈l1,instr1〉 ← call_post_clean … i1 m keep abs;
567   if ((get_element … m lout) == lout :: l1) ∧ ((get_element … m lin) == [lin])
568   then return 〈nil ?,IO … lin io lout instr1〉
569   else None ?   
570].
571
572let rec foldr2 (A : Type[0]) (B : Type[0]) (C : Type[0]) (a : A) (l1 : list B)
573(l2 : list C) (f : A → B → C → A) on l1 : option A≝
574match l1 with
575[ nil ⇒ match l2 with [ nil ⇒ return a | cons y ys ⇒ None ? ]
576| cons x xs ⇒
577        match l2 with
578        [ nil ⇒ None ?
579        | cons y ys ⇒ ! ih ← (foldr2 … a xs ys f);
580                      return f ih x y
581        ]
582].
583
584definition is_silent_cost_act_b : ActionLabel → bool ≝
585λact. match act with
586 [ cost_act x ⇒ match x with [None ⇒ true | _ ⇒ false ] | _ ⇒ false].
587
588definition eq_ActionLabel : ActionLabel → ActionLabel → bool ≝
589λc1,c2.
590match c1 with
591[ call_act f l ⇒ match c2 with [ call_act f' l' ⇒ f == f' ∧ l == l' | _ ⇒ false]
592| ret_act x ⇒ match c2 with [ret_act y ⇒ match x with [ None ⇒ match y with [ None ⇒ true | _ ⇒ false]
593                                                      | Some l ⇒ match y with [ Some l' ⇒ l == l' | _ ⇒ false]
594                                                      ]
595                            | _ ⇒ false
596                            ]
597| cost_act x ⇒ match c2 with [cost_act y ⇒ match x with [ None ⇒ match y with [ None ⇒ true | _ ⇒ false]
598                                                        | Some l ⇒ match y with [ Some l' ⇒ l == l' | _ ⇒ false]
599                                                        ]
600                             | _ ⇒ false
601                             ]
602| init_act ⇒ match c2 with [init_act ⇒ true | _ ⇒ false]
603].
604
605definition ret_costed_abs : list ReturnPostCostLabel → option ReturnPostCostLabel → option CostLabel ≝
606λkeep,x.
607 match x with
608              [ Some lbl ⇒ if lbl ∈ keep then return (a_return_post lbl)
609                           else None ?
610              | None ⇒ None ?
611              ].
612
613
614definition check_continuations : ∀p : instr_params.
615∀l1,l2 : list (ActionLabel × (Instructions p)).
616associative_list DEQCostLabel CostLabel →
617list ReturnPostCostLabel →  option (Prop × (list CostLabel) × (list CostLabel)) ≝
618λp,cont1,cont2,m,keep.
619foldr2 ??? 〈True,nil ?,nil ?〉 cont1 cont2
620 (λx,y,z.
621   let 〈cond,abs_top',abs_tail'〉 ≝ x in
622   match call_post_clean p (\snd z) m keep abs_top' with
623   [ None ⇒ 〈False,nil ?,nil ?〉
624   | Some w ⇒
625      match \fst z with
626       [ ret_act opt_x ⇒
627           match ret_costed_abs keep opt_x with
628           [ Some lbl ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
629                               get_element … m lbl = lbl :: (\fst w),(nil ?),abs_tail'〉
630           | None ⇒
631              〈\fst y = ret_act (None ?) ∧ cond ∧ \snd w = \snd y,(nil ?),(\fst w) @ abs_tail'〉
632           ]
633       | cost_act opt_x ⇒
634           match opt_x with
635           [ None ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y,\fst w,abs_tail'〉
636           | Some xx ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
637                               get_element … m xx = xx :: (\fst w),(nil ?),abs_tail'〉]
638       | _ ⇒ (* dummy *) 〈False,nil ?,nil ?〉]]).
639
640(*
641definition check_continuations : ∀p : instr_params.
642∀l1,l2 : list (ActionLabel × (Instructions p)).
643associative_list DEQCostLabel CostLabel → list CostLabel → list CostLabel →
644list (list CostLabel) →  option (Prop × (list CostLabel) × (list (list CostLabel))) ≝
645λp,cont1,cont2,m,keep,abs_top,abs_tail.
646foldr2 ??? 〈True,abs_top,abs_tail〉 cont1 cont2
647 (λx,y,z.
648   let 〈cond,abs_top',abs_tail'〉 ≝ x in
649   match call_post_clean p (\snd z) m keep abs_top' with
650   [ None ⇒ 〈False,nil ?,nil ?〉
651   | Some w ⇒
652      match \fst z with
653       [ ret_act opt_x ⇒
654           match ret_costed_abs keep opt_x with
655           [ Some lbl ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
656                               get_element … m lbl = lbl :: (\fst w),(nil ?),(nil ?) :: abs_tail'〉
657           | None ⇒
658              〈\fst y = ret_act (None ?) ∧ cond ∧ \snd w = \snd y,(nil ?),(\fst w) :: abs_tail'〉
659           ]
660       | cost_act opt_x ⇒
661           match opt_x with
662           [ None ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y,\fst w,abs_tail'〉
663           | Some xx ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
664                               get_element … m xx = xx :: (\fst w),(nil ?),abs_tail'〉]
665       | _ ⇒ (* dummy *) 〈False,nil ?,nil ?〉]]). *)
666(* in input :
667     abs_top is the list of labels to be propageted to the deepest level of the call stack
668     abs_tail are the lists of labels to be propagated to the levels "below" the deepest level
669   in output :
670     abs_top is the list of labels to be propageted from the current level of the call stack
671     abs_tail are the lists of labels to be propagated from the levels "below" the current level
672  *)       
673       
674
675definition state_rel : ∀p.
676associative_list DEQCostLabel CostLabel → list ReturnPostCostLabel → list CostLabel →(list CostLabel) →
677relation (state p) ≝ λp,m,keep,abs_top,abs_tail,st1,st2.
678match check_continuations ? (cont ? st1) (cont … st2) m keep with
679[ Some x ⇒ let 〈prf1,abs_top',abs_tail'〉 ≝ x in
680           prf1 ∧ call_post_clean … (code … st2) m keep abs_top' = return 〈abs_top,(code … st1)〉
681           ∧ store … st1 = store … st2 ∧ io_info … st1 = io_info … st2 ∧ abs_tail = abs_tail'
682| None ⇒ False
683].
684
685include "Simulation.ma".
686
687let rec len (s : abstract_status) (st1 : s) (st2 : s) (t : raw_trace s st1 st2)
688on t : ℕ ≝
689match t with
690[ t_base s ⇒ O
691| t_ind s1 s2 s3 l prf lt ⇒ S (len … lt)
692].
693(*
694lemma associative_max : ∀n1,n2,n3.max (max n1 n2) n3 = max n1 (max n2 n3).
695#n1 #n2 #n3 normalize @leb_elim normalize
696[ @leb_elim normalize
697  [ #H1 #H2 @leb_elim normalize
698    [ @leb_elim normalize // * #H @⊥ @H assumption 
699    | @leb_elim normalize
700      [ #H3 * #H @⊥ @H @(transitive_le … H1) assumption
701      | * #H @⊥ @H assumption
702      ]
703    ]
704  | #H1 #H2 @leb_elim normalize
705    [ @leb_elim normalize // #_ #H cases H1 #H1 @⊥ @H1 assumption
706    | @leb_elim normalize
707      [ #_ * #H @⊥ @H assumption
708      | * #H @⊥ @H @(transitive_le … H2)
709*)
710let rec compute_max_n (p : instr_params) (i : Instructions p) on i : ℕ ≝
711match i with
712[ EMPTY ⇒ O
713| RETURN x ⇒ O
714| SEQ x lab instr ⇒ let n ≝ compute_max_n … instr in
715                    match lab with
716                    [ None ⇒ n
717                    | Some l ⇒
718                        match l with
719                        [ a_non_functional_label n' ⇒ S (max n n') ]
720                    ]
721| COND x ltrue i1 lfalse i2 i3 ⇒
722  let n1 ≝ compute_max_n … i1 in
723  let n2 ≝ compute_max_n … i2 in
724  let n3 ≝ compute_max_n … i3 in
725  let mx ≝ max (max n1 n2) n3 in
726  match ltrue with
727  [ a_non_functional_label lt ⇒
728    match lfalse with
729    [a_non_functional_label lf ⇒ S (max (max mx lt) lf) ] ]
730| LOOP x ltrue i1 lfalse i2 ⇒
731   let n1 ≝ compute_max_n … i1 in
732   let n2 ≝ compute_max_n … i2 in
733   let mx ≝ max n1 n2 in
734   match ltrue with
735  [ a_non_functional_label lt ⇒
736    match lfalse with
737    [a_non_functional_label lf ⇒ S (max (max mx lt) lf) ] ]
738| CALL f act_p r_lb i1 ⇒
739   let n ≝ compute_max_n … i1 in
740   match r_lb with
741   [ None ⇒ n
742   | Some lbl ⇒ match lbl with [a_return_cost_label l ⇒ S(max l n) ]
743   ]
744| IO lin io lout i1 ⇒
745  let n ≝ compute_max_n … i1 in
746  match lin with
747  [a_non_functional_label l1 ⇒
748    match lout with
749    [a_non_functional_label l2 ⇒ S(max (max n l1) l2) ] ]
750].
751
752
753definition same_fresh_map_on : list CostLabel →
754relation (associative_list DEQCostLabel CostLabel) ≝
755λdom,m1,m2.∀x.bool_to_Prop (x ∈ dom) → get_element … m1 x = get_element … m2 x.
756
757definition same_to_keep_on : list CostLabel → relation (list ReturnPostCostLabel) ≝
758λdom,keep1,keep2.∀x. bool_to_Prop (a_return_post x ∈ dom) → (x ∈ keep1) = (x ∈ keep2).
759
760lemma memb_not_append : ∀D : DeqSet.∀l1,l2 : list D.∀x : D.
761x ∈ l1 = false → x ∈ l2 = false → x ∈ (l1 @ l2) = false.
762#D #l1 elim l1
763[ #l2 #x #_ #H @H ]
764#x #xs #IH #l2 #x1 whd in match (memb ???); inversion (x1 == x) normalize nodelta
765[ #_ #EQ destruct] #EQx1 #EQxs #EQl2 whd in match (memb ???); >EQx1
766normalize nodelta @IH //
767qed.
768
769lemma memb_no_duplicates_append : ∀A : DeqSet.∀x.∀l1,l2 : list A .
770no_duplicates … (l1 @ l2) → x ∈  l1 → x ∈ l2 → False.
771#A #x #l1 elim l1 // #x1 #xs #IH #l2 * #H1 #H2 whd in match (memb ???);
772inversion (x == x1) normalize nodelta
773[ #H3 #_ #H4 >memb_append_l2 in H1; [2: <(\P H3) @H4 ] * #H @H %
774| #_ @IH //
775]
776qed.
777
778
779lemma same_to_keep_on_append : ∀dom1,dom2,dom3 : list CostLabel.
780∀l1,l2,l3,l : list ReturnPostCostLabel.
781no_duplicates … (dom1@dom2@dom3) → (∀x.x ∈ l1 → a_return_post x ∈ dom1) →
782(∀x.x ∈ l3 → a_return_post x ∈ dom3) →
783same_to_keep_on (dom1@dom2@dom3) (l1@l2@l3) l →
784same_to_keep_on dom2 l2 l.
785#dom1 #dom2 #dom3 #l1 #l2 #l3 #l #no_dup #sub_set1 #sub_set3 #H2
786#x #Hx inversion (x ∈ l2)
787 [ #EQkeep <H2 [> memb_append_l2 // >memb_append_l1 // ]
788   >memb_append_l2 // >memb_append_l1 // >Hx //
789 | #EQno_keep <H2
790   [2: >memb_append_l2 // >memb_append_l1 // >Hx //
791   | @sym_eq @memb_not_append [2: @memb_not_append //]
792   [ <associative_append in no_dup; #no_dup ]
793   lapply(memb_no_duplicates_append … (a_return_post x) … no_dup) #H
794   inversion(memb ???) // #H1 cases H
795   [1,4: [>memb_append_l2 | >memb_append_l1] // >Hx //
796   | @sub_set3 >H1 //
797   | @sub_set1 >H1 //
798   ]
799   ]
800 ]
801qed.
802
803lemma same_fresh_map_on_append : ∀dom1,dom2,dom3,l1,l2,l3,l.
804no_duplicates … (dom1 @dom2 @ dom3) → (∀x.x ∈ domain_of_associative_list … l1 → x ∈ dom1) →
805(∀x.x ∈ domain_of_associative_list … l3 → x ∈ dom3) →
806same_fresh_map_on … (dom1 @dom2 @dom3) (l1 @l2 @ l3) l →
807same_fresh_map_on … dom2 l2 l.
808#dom1 #dom2 #dom3 #l1 #l2 #l3 #l #no_dup #subset1 #subset3 whd in ⊢ (% → ?); #H1
809whd #x #Hx <H1
810[2: >memb_append_l2 // >memb_append_l1 // >Hx //]
811>get_element_append_r [ >get_element_append_l1 // ] % #K
812[ lapply(subset3 … K) | lapply(subset1 … K) ] #ABS
813[ <associative_append in no_dup; #no_dup] @(memb_no_duplicates_append … x … no_dup)
814// [>memb_append_l2 | >memb_append_l1 ] // >Hx //
815qed.
816
817
818lemma lab_to_keep_in_domain : ∀p.∀i : Instructions p.
819∀x,n,l.
820x ∈ lab_to_keep … (call_post_trans … i n l) → a_return_post x ∈ get_labels_of_code …  i.
821#p #i elim i //
822[ #seq #opt_l #instr #IH #x #n #l whd in match (call_post_trans ????);
823  cases opt_l -opt_l normalize nodelta [|#lbl]
824  whd in match (get_labels_of_code ??); #H [2: @orb_Prop_r] /2/
825| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #x #n #l
826  whd in match (call_post_trans ????); whd in match (get_labels_of_code ??);
827  #H cases(memb_append … H) -H #H @orb_Prop_r @orb_Prop_r
828  [ >memb_append_l1 // @IH1 [3: >H // |*: ]
829  | >memb_append_l2 // cases(memb_append … H) -H #H
830     [>memb_append_l1 // @IH2 [3: >H // |*: ]
831     | >memb_append_l2 // @IH3 [3: >H // |*: ]
832     ]
833  ]
834| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #x #n #l
835  whd in match (call_post_trans ????); whd in match (get_labels_of_code ??);
836  #H cases(memb_append … H) -H #H @orb_Prop_r @orb_Prop_r
837  [ >memb_append_l1 | >memb_append_l2 ] // [ @IH1 | @IH2 ] [3,6: >H |*: ] //
838| #f #act_p * [|#lbl] #i1 #IH #x #n #l whd in match (call_post_trans ????);
839  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???);
840  inversion(x == lbl) #Hlbl normalize nodelta
841  [*  >(\P Hlbl) @orb_Prop_l @eq_costlabel_elim // * #H @H %
842  | #H @orb_Prop_r @IH //
843  ]
844| #lin #io #lout #i1 #IH #x #n #l whd in match (call_post_trans ????);
845  whd in match (get_labels_of_code ??); #H @orb_Prop_r @orb_Prop_r @IH //
846]
847qed.
848
849lemma domain_of_associative_list_append : ∀A,B.∀l1,l2 : associative_list A B.
850domain_of_associative_list ?? (l1 @ l2) =
851 (domain_of_associative_list ?? l1) @ (domain_of_associative_list ?? l2).
852#A #B #l1 elim l1 // #x #xs #IH #l2 normalize //
853qed.
854
855lemma lab_map_in_domain: ∀p.∀i: Instructions p.
856 ∀x,n,l.
857  x ∈ domain_of_associative_list … (lab_map p (call_post_trans … i n l)) →
858   x ∈ get_labels_of_code … i.
859#p #i elim i //
860[ #seq * [|#lbl] #i1 #IH #x #n #l whd in match(call_post_trans ????);
861  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???);
862  inversion(x==lbl) #Hlbl normalize nodelta [#_ whd in match (memb ???); >Hlbl % ]
863  #H >memb_cons // @IH //
864| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #x #n #l
865  whd in match (call_post_trans ????); whd in match (memb ???);
866  whd in match (get_labels_of_code ??); inversion(x == ltrue) #Hlbl normalize nodelta
867  [ #_ whd in match (memb ???); >Hlbl % ] whd in match (memb ???);
868  inversion(x == lfalse) #Hlbl1 normalize nodelta
869  [ #_ whd in match (memb ???); >Hlbl normalize nodelta whd in match (memb ???);
870    >Hlbl1 % ] #H >memb_cons // >memb_cons // >domain_of_associative_list_append in H;
871    #H cases(memb_append … H) [ #H1 >memb_append_l1 // @IH1 [3: >H1 // |*:] ]
872    >domain_of_associative_list_append #H1 cases(memb_append … H1)
873    #H2 >memb_append_l2 // [ >memb_append_l1 | >memb_append_l2 ] //
874    [ @IH2 | @IH3] /2 by eq_true_to_b/
875| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #x #n #l whd in match (call_post_trans ????);
876  whd in match (get_labels_of_code ??); whd in match (memb ???); inversion(x == lfalse)
877  #Hlfalse normalize nodelta [ #_ >memb_cons // whd in match (memb ???); >Hlfalse // ]
878  whd in match (memb ???); inversion(x==ltrue) normalize nodelta #Hltrue
879  [ #_ whd in match (memb ???); >Hltrue %] >domain_of_associative_list_append #H
880  cases(memb_append … H) #H1 >memb_cons // >memb_cons // [ >memb_append_l1 | >memb_append_l2 ]
881  // [ @IH1 | @IH2] /2/
882| #f #act_p * [|#lbl] #i1 #IH #x #n #l whd in match (call_post_trans ????);
883  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???); inversion (x == lbl)
884  #Hlbl normalize nodelta [ #_ whd in match (memb ???); >Hlbl % ] #H >memb_cons // @IH /2/
885| #lin #io #lout #i1 #IH #x #n #l whd in match (memb ???); inversion(x == lout) #Hlout
886  normalize nodelta [ #_ >memb_cons // whd in match (memb ???); >Hlout % ]
887  whd in match (memb ???); inversion(x==lin) #Hlin normalize nodelta
888  [ #_ whd in match (memb ???); >Hlin % ] #H >memb_cons // >memb_cons // @IH /2/
889]
890qed.
891
892lemma eq_a_non_functional_label :
893∀c1,c2.c1 == c2 → a_non_functional_label c1 == a_non_functional_label c2.
894#c1 #c2 #EQ cases(eqb_true DEQNonFunctionalLabel c1 c2) #H1 #_ lapply(H1 (eq_true_to_b … EQ)) -EQ
895#EQ destruct >(\b (refl …)) @I
896qed.
897
898let rec is_fresh_for_return (keep : list CostLabel) (n : ℕ) on keep : Prop ≝
899match keep with
900[ nil ⇒ True
901| cons x xs ⇒ let ih ≝ is_fresh_for_return xs n in
902              match x with
903              [ a_return_post y ⇒ match y with [a_return_cost_label m ⇒ m ≤ n ∧ ih ]
904              | _ ⇒ ih
905              ]
906].
907
908lemma fresh_ok_call_post_trans : ∀p : instr_params.∀i1 : Instructions p.∀n : ℕ.∀l.
909n ≤ fresh … (call_post_trans p i1 n l).
910#p #i1 elim i1 normalize /2 by transitive_le, le_n/
911[ #seq * [|#lbl] #i2 #IH #n #l normalize /2 by /
912| #cond #ltrue #i_true #lfalse #i_false #i2 #IH1 #IH2 #IH3 #n #l
913  @(transitive_le … (IH3 …)) [2: @(transitive_le … (IH2 …)) [2: @(transitive_le … (IH1 …)) ]] //
914| #f #act_p * [|#lbl] normalize #i2 #IH /2 by le_S/
915]
916qed.
917
918lemma fresh_keep_n_ok : ∀n,m,l.
919is_fresh_for_return l n → n ≤ m → is_fresh_for_return l m.
920#n #m #l lapply n -n lapply m -m elim l // ** #x #xs #IH #n #m
921normalize [2: * #H1] #H2 #H3 [ %] /2 by transitive_le/
922qed.
923
924definition cast_return_to_cost_labels ≝ map … (a_return_post …).
925coercion cast_return_to_cost_labels.
926
927lemma fresh_false : ∀n.∀l: list ReturnPostCostLabel.is_fresh_for_return l n →
928a_return_cost_label (S n) ∈ l = false.
929#n #l lapply n -n elim l // * #x #xs #IH #n whd in ⊢ (% → ??%?); * #H1
930#H2 @eq_return_cost_lab_elim
931[ #EQ destruct @⊥ /2 by absurd/
932| #_ >IH //
933]
934qed.
935
936lemma inverse_call_post_trans : ∀p : instr_params.∀i1 : Instructions p.∀n : ℕ.
937let dom ≝ get_labels_of_code … i1 in
938no_duplicates … dom →
939∀m,keep.
940∀info,l.call_post_trans p i1 n l = info →
941same_fresh_map_on dom (lab_map … info) m →
942same_to_keep_on dom (lab_to_keep … info) keep →
943is_fresh_for_return keep n →
944call_post_clean ? (t_code ? info) m keep l
945 = return 〈gen_labels … info,i1〉.
946#p #i1 elim i1
947[ #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
948  #EQ destruct(EQ) //
949| #x #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
950  #EQ destruct(EQ) //
951| #seq * [|#lbl] #instr #IH #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep
952  #l whd in ⊢ (??%? → ?); #EQ destruct(EQ) #H1 #H2 #H3 whd in ⊢ (??%%); normalize nodelta
953  >IH //
954  [1,4: whd whd in H2; #x #Hx @H2 whd in match (get_labels_of_code ??); //
955  |2,5: whd whd in H1; #x #Hx [ @H1 // ] cases no_dup #H3 #_ <H1
956       [2: whd in match (get_labels_of_code ??); @orb_Prop_r // ]
957       whd in ⊢ (???%); cases(eqb_true … x lbl) inversion(x == lbl) normalize nodelta
958       [2: //] #_ #H4 >H4 in Hx; // #H5 >H5 in H3; * #ABS @⊥ @ABS %
959  |6: cases no_dup //
960  ]
961  normalize nodelta <(H1 lbl)
962  [2: whd in match (get_labels_of_code ??); @orb_Prop_l cases(eqb_true … (a_non_functional_label lbl) lbl)
963      #H3 #H4 >H4 % ]
964  whd in match (get_element ????); >(\b (refl …)) normalize nodelta
965  >(\b (refl …)) %
966| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #n normalize nodelta
967  #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
968  #EQ destruct(EQ) #H1 #H2 #H3 whd in ⊢ (??%?); >IH3 //
969  [2: whd  in match (get_labels_of_code ??) in H2;
970      change with ([?;?]@?) in match (?::?) in H2;
971      <associative_append in H2; <associative_append
972      <(append_nil … (?@?)) <associative_append in ⊢ (??%? → ?);
973      <(append_nil … (?@?)) in ⊢ (??%? → ?); >associative_append
974      >associative_append in ⊢ (??%? → ?); #H2
975      @(same_to_keep_on_append … H2) // [ >append_nil
976      whd in ⊢ (??%); whd in no_dup:(??%); >associative_append // ]
977      #x #Hx cases (memb_append … Hx) -Hx #Hx @orb_Prop_r @orb_Prop_r
978      [ >memb_append_l1 | >memb_append_l2 ] //
979      @(lab_to_keep_in_domain … (eq_true_to_b … Hx))
980  |3: -H2 whd in match (get_labels_of_code ??) in H1;
981      change with ([?;?]@?) in match (?::?) in H1;
982      <associative_append in H1; <associative_append     
983      <(append_nil … (?@?)) >associative_append
984      change with ([?;?]@?) in match (?::?::?) in ⊢ (??%? → ?);
985      <associative_append in ⊢ (??%? → ?);
986      <associative_append in ⊢ (??%? → ?);
987      <(append_nil … (?@?)) in ⊢ (??%? → ?);
988      >associative_append in ⊢ (??%? → ?); #H1
989      @(same_fresh_map_on_append … H1) //
990      [ >append_nil >associative_append // ]
991      #x whd in match (memb ???); inversion(x == ltrue)
992      [ #Hltrue normalize nodelta #_ whd in match (memb ???); >Hltrue %
993      | #Hltrue normalize nodelta whd in match (memb ???); inversion(x == lfalse)
994         [ #Hlfalse #_ @orb_Prop_r @orb_Prop_l >Hlfalse %
995         | #Hlfalse normalize nodelta #Hx @orb_Prop_r @orb_Prop_r
996           >domain_of_associative_list_append in Hx; #H
997           cases(memb_append … H) #H2 [ >memb_append_l1 | >memb_append_l2 ]
998           // @(lab_map_in_domain … (eq_true_to_b … H2))
999         ]
1000      ]
1001  |4: cases no_dup #_ * #_ #H @no_duplicates_append_r [2: @(no_duplicates_append_r … H) |]
1002  ]
1003  normalize nodelta >IH2
1004  [5: %
1005  |2: /2 by fresh_keep_n_ok/
1006  |3: whd  in match (get_labels_of_code ??) in H2;
1007   change with ([?;?]@?) in match (?::?) in H2;
1008   <associative_append in H2; #H2
1009   @(same_to_keep_on_append … H2) // #x #Hx [ @orb_Prop_r @orb_Prop_r ]
1010   @(lab_to_keep_in_domain … (eq_true_to_b … Hx))
1011  |4: whd  in match (get_labels_of_code ??) in H1;
1012   change with ([?;?]@?) in match (?::?) in H1;
1013   change with ([?;?]@?) in match (?::?::?) in H1 : (??%?);
1014   <associative_append in H1; <associative_append in ⊢ (??%? → ?); #H1
1015   @(same_fresh_map_on_append … H1) // [2: /2 by lab_map_in_domain/ ]
1016   #x >domain_of_associative_list_append #H cases(memb_append … H)
1017   [ whd in ⊢ (??%? → ?%); cases(x == ltrue) // normalize nodelta
1018     whd in ⊢ (??%? → ?%); cases(x == lfalse) // normalize nodelta
1019     normalize #EQ destruct
1020   | #H1 @orb_Prop_r @orb_Prop_r
1021     @(lab_map_in_domain … (eq_true_to_b … H1))
1022   ]
1023  |6: cases no_dup #_ * #_ #K lapply (no_duplicates_append_r … K) @no_duplicates_append_l
1024  |*:
1025  ]
1026  >m_return_bind >IH1
1027  [5: %
1028  |2: /3 by fresh_keep_n_ok/
1029  |3:  whd  in match (get_labels_of_code ??) in H2;
1030   change with ([?;?]@?) in match (?::?) in H2;
1031   change with ([ ] @ ?) in match (lab_to_keep ??) in H2;
1032   >associative_append in H2 : (??%?); #H2
1033   @(same_to_keep_on_append … H2) //  #x #Hx cases(memb_append … Hx)
1034   -Hx #Hx [ >memb_append_l1 | >memb_append_l2] //
1035   @(lab_to_keep_in_domain … (eq_true_to_b … Hx))
1036  |4:  whd  in match (get_labels_of_code ??) in H1;
1037   change with ([?;?]@?) in match (?::?) in H1;
1038   change with ([?;?]@?) in match (?::?::?) in H1 : (??%?);
1039    @(same_fresh_map_on_append … H1) // #x >domain_of_associative_list_append
1040    #Hx cases(memb_append … Hx) -Hx #Hx [ >memb_append_l1 | >memb_append_l2 ]
1041    // @(lab_map_in_domain … (eq_true_to_b … Hx))
1042  |6: cases no_dup #_ * #_ @no_duplicates_append_l
1043  |*:
1044  ]
1045  >m_return_bind normalize nodelta whd in H1; <H1
1046  [2: whd in match (get_labels_of_code ??); whd in match (memb ???);
1047      >(\b (refl …)) % ] whd in match (get_element ????); >(\b (refl …))
1048      normalize nodelta >(\b (refl …)) <H1
1049      [2: whd in match (get_labels_of_code ??); >memb_cons //
1050      whd in match (memb ???); >(\b (refl …)) % ]
1051      whd in match (get_element ????); @eq_costlabel_elim normalize nodelta
1052      [ #ABS @⊥ cases no_dup >ABS * #H #_ @H @orb_Prop_l 
1053      >(\b (refl ? (a_non_functional_label ltrue))) % ] #_
1054      whd in match (get_element ????); >(\b (refl …)) normalize nodelta
1055      >(\b (refl …)) %
1056| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #n #no_dup #m #keep #info #l whd in ⊢ (??%? → ?);
1057  #EQ destruct(EQ) whd in match (get_labels_of_code ??); #fresh_map #keep_on #f_k
1058  whd in ⊢ (??%?); >(IH2 … (refl …))
1059  [ normalize nodelta >(IH1 … (refl …))
1060    [ >m_return_bind <fresh_map [2: @orb_Prop_l >(\b (refl …)) % ]
1061      whd in match (get_element ????);
1062      inversion(a_non_functional_label ltrue == a_non_functional_label lfalse)
1063      #Hltrue normalize nodelta
1064      [ cases no_dup whd in match (memb ???);
1065        cases(eqb_true … (a_non_functional_label ltrue) (a_non_functional_label lfalse))
1066        #H1 #_ lapply(H1 Hltrue) #EQ destruct(EQ) >(\b (refl …)) * #ABS @⊥ @ABS % ]
1067      whd in match (get_element ????); >(\b (refl …)) normalize nodelta >(\b (refl …))
1068      <fresh_map [2: @orb_Prop_r @orb_Prop_l >(\b (refl …)) % ]
1069      whd in match (get_element ????); >(\b (refl …)) normalize nodelta
1070      >(\b (refl …)) %
1071    | /2 by fresh_keep_n_ok/
1072    | change with ([?;?]@?@?) in keep_on : (?%??); change with ((nil ?) @ ? @ ?) in keep_on : (??%?);
1073      @(same_to_keep_on_append … keep_on) // #x /2 by lab_to_keep_in_domain/
1074    | change with ([?;?]@?@?) in fresh_map : (?%%?); @(same_fresh_map_on_append … fresh_map)
1075      /2 by lab_map_in_domain/ #x whd in match (memb ???); inversion(x==lfalse) #Hlfalse
1076      normalize nodelta
1077      [ #_ @orb_Prop_r whd in match (memb ???); >Hlfalse %
1078      | whd in match (memb ???); inversion(x==ltrue) #Hltrue normalize nodelta [2: *] #_
1079        @orb_Prop_l >Hltrue %
1080      ]
1081    | cases no_dup #_ * #_ /2/
1082    ]
1083  | //
1084  | change with ([?;?]@?@?) in keep_on : (?%??); <associative_append in keep_on;
1085    <(append_nil … (?@?)) <(append_nil … (?@?)) in ⊢ (??%? → ?);
1086    >associative_append in ⊢ (?%%? → ?); >associative_append in ⊢ (??%? → ?);
1087    #keep_on @(same_to_keep_on_append … keep_on) //
1088    [ >associative_append >append_nil //
1089    | #x #Hx @orb_Prop_r @orb_Prop_r /2 by lab_to_keep_in_domain/
1090    ]
1091  | change with ([?;?]@?@?) in fresh_map : (?%??); <associative_append in fresh_map;
1092    <(append_nil … (?@?)) change with ([?;?]@?@?) in ⊢ (??%? → ?);
1093    <associative_append in ⊢ (??%? → ?); <(append_nil … (?@?)) in ⊢ (??%? → ?);
1094    >associative_append in ⊢ (?%%? → ?); >associative_append in ⊢ (??%? → ?);
1095    #fresh_map @(same_fresh_map_on_append … fresh_map) //
1096    [ >append_nil //
1097    | #x >domain_of_associative_list_append #Hx cases(memb_append … Hx)
1098      [2: #Hx1 @orb_Prop_r @orb_Prop_r @(lab_map_in_domain … (eq_true_to_b … Hx1)) ]
1099      whd in match (memb ???); inversion(x == lfalse) normalize nodelta #Hlfalse
1100      [ #_ @orb_Prop_r @orb_Prop_l >Hlfalse %
1101      | whd in match (memb ???); inversion (x==ltrue) normalize nodelta #Hltrue
1102        [ #_ @orb_Prop_l >Hltrue %
1103        | whd in match (memb ???); #EQ destruct
1104        ]
1105      ]
1106    ]
1107  | cases no_dup #_ * #_ /2 by no_duplicates_append_r/
1108  ]
1109| #f #act_p * [|#r_lb] #i #IH #n #no_dup #m #keep #info #l whd in ⊢ (??%? → ?);
1110  #EQ destruct(EQ) #fresh_map #same_keep #f_k whd in ⊢ (??%?);
1111  >(IH … (refl …))
1112  [1,6: normalize nodelta
1113     [ >fresh_false [2: /2 by fresh_keep_n_ok/] %
1114     | <same_keep
1115       [ whd in match (memb ???); >(\b (refl …)) normalize nodelta
1116         <fresh_map
1117         [ whd in match (get_element ????); >(\b (refl …)) normalize nodelta
1118           >(\b (refl …)) %
1119         | whd in match (memb ???); >(\b (refl …)) %
1120         ]
1121       | whd in match (memb ???); >(\b (refl …)) %
1122       ]
1123    ]
1124  |2,7: //
1125  |3,8: whd in match (get_labels_of_code ??) in same_keep; // #x #Hx <same_keep
1126        [2: >memb_cons // >Hx // ] cases no_dup * #ABS #_ whd in ⊢ (???%);
1127        inversion(x==?) [2: #_ //] #ABS1 @⊥ @ABS <(\P ABS1) >Hx //
1128  |4,9: whd in match (get_labels_of_code ??) in fresh_map; // #x #Hx <fresh_map
1129        [2: >memb_cons // >Hx //] cases no_dup * #ABS #_ whd in ⊢ (???%);
1130        inversion(x==?) [2: #_ //] #ABS1 @⊥ @ABS <(\P ABS1) //
1131  |5,10: [ @no_dup | cases no_dup // ]
1132  ]
1133| #lin #io #lout #i #IH #n whd in match (get_labels_of_code ??); #no_dup
1134  #m #keep #info #l whd in ⊢ (??%? → ?); #EQ destruct(EQ) #fresh_map #same_keep
1135  #f_k whd in ⊢ (??%?); >(IH … (refl …))
1136  [ normalize nodelta <fresh_map [2: >memb_cons // >memb_hd // ]
1137    whd in match (get_element ????); >(\b (refl …)) normalize nodelta
1138    >(\b (refl …)) <fresh_map [2: >memb_hd //] whd in match (get_element ????);
1139    inversion(lin==lout)
1140    [ #ABS @⊥ cases no_dup * #ABS1 #_ @ABS1 whd in match (memb ???); >(\P ABS)
1141      >(\b (refl …)) //
1142    | #H inversion (a_non_functional_label lin== ? lout)
1143      [ #ABS lapply(\P ABS) #EQ destruct >(\b (refl …)) in H; #EQ destruct
1144      | #_ normalize nodelta whd in match (get_element ????); >(\b (refl …))
1145        normalize nodelta >(\b (refl …)) %
1146      ]
1147    ]
1148  | //
1149  | #x #Hx >same_keep [2: >memb_cons // >memb_cons // >Hx % ] %
1150  | #x #Hx <fresh_map [2: >memb_cons // >memb_cons // >Hx %]
1151    cases no_dup * #ABS1 ** #ABS2 #_ whd in ⊢ (???%); inversion(x == lout)
1152    normalize nodelta
1153    [2: #_ whd in ⊢ (???%); inversion(x==lin) normalize nodelta //
1154        #H @⊥ @ABS1 >memb_cons // <(\P H) >Hx //
1155    | #H @⊥ @ABS2 <(\P H) >Hx //
1156    ]
1157  | cases no_dup #_ * #_ //
1158  ]
1159]
1160qed.
1161
1162definition fresh_for_prog_aux : ∀p,p'.Program p p' → ℕ → ℕ ≝
1163λp,p',prog,n.foldl … (λn,i.max n (compute_max_n … (f_body … i))) n (env … prog).
1164
1165include alias "arithmetics/nat.ma".
1166
1167lemma max_1 : ∀n,m,k.k ≤ m → k ≤ max m n.
1168#m #n #k #H normalize @leb_elim // normalize nodelta #H1
1169/2 by transitive_le/
1170qed.
1171
1172lemma max_2 : ∀n,m,k.k≤ n → k ≤ max m n.
1173#m #n #k #H normalize @leb_elim // #H1 normalize nodelta
1174@(transitive_le … H) @(transitive_le … (not_le_to_lt … H1)) //
1175qed.
1176
1177lemma fresh_aux_ok : ∀p,p'.∀prog : Program p p'.∀n,m.n ≤ m →
1178fresh_for_prog_aux … prog n ≤ fresh_for_prog_aux … prog m.
1179#p #p' * #env #main elim env // #hd #tl #IH #n #m #H whd in ⊢ (?%%);
1180@IH whd in ⊢ (?%?); @leb_elim normalize nodelta
1181[ #H1 @max_2 // | #_ @max_1 // ]
1182qed.
1183
1184definition fresh_for_prog : ∀p,p'.Program p p' → ℕ ≝
1185λp,p',prog.fresh_for_prog_aux … prog
1186(compute_max_n … (main … prog)).
1187
1188definition translate_env ≝
1189λp,p'.λenv : list (env_item p p').λmax_all.(foldr ??
1190           (λi,x.let 〈t_env,n,m,keep〉 ≝ x in
1191           let info ≝ call_post_trans … (f_body … i) n (nil ?) in
1192                   〈(mk_env_item ??
1193                       (mk_signature ??(f_name ?? i) (f_pars … i) (f_ret … i))
1194                       (f_lab … i) (t_code … info)) :: t_env,
1195                     fresh … info, 〈a_call (f_lab … i),(a_call (f_lab … i)) :: (gen_labels ? info)〉 ::
1196                                     ((lab_map … info) @ m),(lab_to_keep … info) @ keep〉)
1197          (〈nil ?,max_all,nil ?,nil ?〉) env).
1198
1199definition trans_prog : ∀p,p'.Program p p' →
1200((Program p p') × (associative_list DEQCostLabel CostLabel)) × ((list ReturnPostCostLabel))≝
1201λp,p',prog.
1202let max_all ≝ fresh_for_prog … prog in
1203let info_main ≝ (call_post_trans … (main … prog) max_all (nil ?)) in
1204let 〈t_env,n,m,keep〉 ≝ translate_env … (env … prog) (fresh … info_main) in
1205〈mk_Program ?? t_env (t_code … info_main),m @ (lab_map … info_main),keep @ (lab_to_keep … info_main)〉.
1206
1207definition map_labels_on_trace :
1208(associative_list DEQCostLabel CostLabel) → list CostLabel → list CostLabel ≝
1209λm,l.foldr … (λlab,t.(get_element … m lab) @ t) (nil ?) l.
1210
1211lemma map_labels_on_trace_append:
1212 ∀m,l1,l2. map_labels_on_trace m (l1@l2) =
1213  map_labels_on_trace m l1 @ map_labels_on_trace m l2.
1214 #m #l1 elim l1 // #hd #tl #IH #l2 >associative_append <IH //
1215qed.
1216
1217include "../src/common/Errors.ma".
1218
1219axiom is_permutation: ∀A.list A → list A → Prop.
1220axiom is_permutation_eq : ∀A.∀l : list A.is_permutation … l l.
1221axiom is_permutation_cons : ∀A.∀l1,l2,x.is_permutation A l1 l2 →
1222                                       is_permutation A (x :: l1) (x :: l2).
1223
1224(*
1225inductive is_permutation (A : Type[0]) : list A → list A → Prop ≝
1226| p_empty : is_permutation A (nil ?) (nil ?)
1227| p_append : ∀x,x1,x2,y,y1,y2.
1228               x = y → is_permutation A (x1 @ x2) (y1 @ y2) →
1229                 is_permutation A (x1 @ [x] @ x2) (y1 @ [y] @ y2).
1230
1231lemma is_permutation_eq : ∀A.∀l : list A.is_permutation … l l.
1232#A #l elim l // #x #xs #IH
1233change with ((nil ?) @ (x :: xs)) in ⊢ (??%%);
1234>append_cons >associative_append
1235@(p_append ? x (nil ?) xs x (nil ?) xs (refl …)) @IH
1236qed.
1237
1238lemma is_permutation_append : ∀A.∀l1,l2,l3,l4 : list A.
1239is_permutation A l1 l3 → is_permutation A l2 l4 →
1240is_permutation A (l1 @ l2) (l3 @ l4).
1241#A #l1 inversion (|l1|)  [2: #n lapply l1 elim n
1242[ #l2 #l3 #l4 #H inversion H // #x #x1 #x2 #y #y1 #y2 #EQ #H1 #_
1243 #ABS cases(nil_to_nil … (sym_eq ??? ABS)) -ABS #_ #ABS
1244 cases(nil_to_nil … ABS) #EQ1 destruct(EQ1) ]
1245#x #xs #IH #l2 #l3 #l4 #H inversion H
1246[#EQ lapply(jmeq_to_eq ??? EQ) -EQ #EQ destruct(EQ) ]
1247#y #y1 #y2 #z #z1 #z2 #EQ destruct(EQ) #H1 #_ #EQx_xs #EQ destruct(EQ) #_
1248*)
1249
1250lemma memb_append_l22 : ∀A : DeqSet.∀x : A.∀l1,l2 : list A.
1251¬ (x ∈ l1) → x∈ l1 @ l2 = (x ∈ l2).
1252#A #x #l1 elim l1 normalize // #y #ys #IH #l2 cases(x==y)
1253normalize [*] @IH
1254qed.
1255
1256lemma memb_append_l12 : ∀A : DeqSet.∀x : A.∀l1,l2 : list A.
1257¬ (x ∈ l2) → x∈ l1 @ l2 = (x ∈ l1).
1258#A #x #l1 elim l1
1259[ #l2 #H whd in match (append ???); @not_b_to_eq_false @Prop_notb >H % ]
1260#y #ys #IH #l2 #H1 whd in match (memb ???); >IH //
1261qed.
1262
1263
1264lemma lookup_ok_append : ∀p,p',l,f,env_it.
1265lookup p p' l f = return env_it → ∃l1,l2. l = l1 @ [env_it] @ l2 ∧
1266f_name … env_it = f.
1267#p #p' #l elim l [ #f #env_it normalize #EQ destruct]
1268#x #xs #IH #f #env_it whd in ⊢ (??%? → ?); @eq_function_name_elim
1269[ #EQ destruct(EQ) normalize nodelta whd in ⊢ (???% → ?); #EQ destruct
1270  %{(nil ?)} %{xs} /2/
1271| #Hno_f normalize nodelta #EQ_env_it cases(IH … EQ_env_it)
1272  #l1 * #l2 * #EQ1 #EQ2 %{(x :: l1)} %{l2} >EQ1 /2/
1273]
1274qed.
1275(*
1276lemma foldr_append :
1277  ∀A,B:Type[0]. ∀l1, l2 : list A. ∀f:A → B → B. ∀seed. foldr ?? f seed (l1 @ l2) = foldr ?? f (foldr ?? f seed l2) l1.
1278#A #B #l1 elim l1 //
1279#hd #tl #Hind #l2 #f #seed normalize >Hind @refl
1280qed.
1281*)
1282
1283lemma foldr_map_append :
1284  ∀A,B:Type[0]. ∀l1, l2 : list A.
1285   ∀f:A → list B. ∀seed.
1286    foldr ?? (λx,acc. (f x) @ acc) seed (l1 @ l2) =
1287     append ? (foldr ?? (λx,acc. (f x) @ acc) (nil ?) l1)
1288       (foldr  ?? (λx,acc. (f x) @ acc) seed l2).
1289#A #B #l1 elim l1 normalize // /3 by eq_f, trans_eq/
1290qed.
1291
1292lemma cons_append : ∀A.∀x : A.∀l.x::l = ([x]@l).
1293//
1294qed.
1295
1296lemma eq_a_call_label :
1297∀c1,c2.c1 == c2 → a_call c1 == a_call c2.
1298#c1 #c2 #EQ cases(eqb_true ? c1 c2) #H1 #_ lapply(H1 (eq_true_to_b … EQ)) -EQ
1299#EQ destruct >(\b (refl …)) @I
1300qed.
1301
1302
1303lemma no_duplicates_append_commute : ∀ A : DeqSet.∀l1,l2 : list A.
1304no_duplicates … (l1 @ l2) →
1305no_duplicates … (l2 @ l1).
1306#A #l1 elim l1
1307[ #l2 >append_nil //]
1308#x #xs #IH #l2 * #H1 #H2 lapply(IH … H2) lapply H1 -H1 -IH -H2
1309elim l2 -l1
1310[ >append_nil #H1 #H2 % // ]
1311#y #ys #IH * #H1 * #H2 #H3 %
1312[2: @IH
1313   [ % #H4 @H1 cases(memb_append … H4)
1314     [ #H5 >memb_append_l1 //
1315     | #H5 >memb_append_l2 // @orb_Prop_r >H5 //
1316     ]
1317   | //
1318   ]
1319| % #H4 cases(memb_append … H4)
1320  [ #H5 @(absurd ?? H2) >memb_append_l1 //
1321  | whd in match (memb ???); inversion(y==x)
1322    [ #H5 #_ <(\P H5) in H1; #H1 @H1 >memb_append_l2 //
1323    | #_ normalize nodelta #H5 @(absurd ?? H2) >memb_append_l2 //
1324    ]
1325  ]
1326]
1327qed.
1328
1329(* aggiungere fresh_to_keep al lemma seguente??*)
1330
1331let rec subset (A : Type[0]) (l1,l2 : list A) on l1 : Prop ≝
1332match l1 with
1333[ nil ⇒ True
1334| cons x xs ⇒ mem … x l2 ∧ subset … xs l2
1335].
1336
1337interpretation "subset" 'subseteq a b = (subset ? a b).
1338
1339lemma subset_append_l2 : ∀A,l2,l1.subset A l2 (l1 @ l2).
1340#A #l2 elim l2 // normalize #x #xs #IH #l1 % // @mem_append_l2 whd /2/
1341qed.
1342
1343lemma refl_subset : ∀A.reflexive … (subset A).
1344#A #l1 elim l1 // #x #xs #IH normalize % /2/
1345qed.
1346
1347lemma fresh_for_subset : ∀l1,l2,n.l1 ⊆ l2 → is_fresh_for_return … l2 n →
1348is_fresh_for_return … l1 n.
1349#l1 elim l1 // ** #x #xs #IH #l2 #n * #H1 #H2 #H3 whd
1350try(@IH) // % [2: @IH //] elim l2 in H1 H3; normalize [*]
1351** #y #ys #IH normalize
1352[2,3: * [2,4: #H3 [2: @IH //] * #H4 #H5 @IH //
1353|*: #EQ destruct * //
1354]]
1355*
1356[ #EQ destruct ] #H3 #H4 @IH //
1357qed.
1358
1359lemma fresh_append : ∀n,l1,l2.is_fresh_for_return l1 n → is_fresh_for_return l2 n →
1360is_fresh_for_return (l1@l2) n.
1361#n #l1 lapply n -n elim l1 // ** #x #xs #IH #n #l2 [2: * #H1 ] #H2 #H3
1362[ % // @IH //] @IH //
1363qed.
1364
1365definition labels_of_prog : ∀p,p'.Program p p' → ? ≝
1366λp,p',prog.foldr … (λx,l.l @ (get_labels_of_code … (f_body … x)))
1367 (get_labels_of_code … (main … prog)) (env … prog).
1368
1369lemma cast_return_append : ∀l1,l2.cast_return_to_cost_labels … (l1 @ l2) =
1370(cast_return_to_cost_labels … l1) @ (cast_return_to_cost_labels … l2).
1371#l1 #l2 @(sym_eq … (map_append …)) qed.
1372
1373include alias "arithmetics/nat.ma".
1374
1375
1376lemma is_fresh_code : ∀p.∀i : Instructions p.
1377is_fresh_for_return (get_labels_of_code … i) (compute_max_n … i).
1378#p #main  elim main //
1379[ #seq * [| * #lbl] #i #IH normalize // @(fresh_keep_n_ok … IH)
1380  inversion(leb ? lbl) // @leb_elim #Hlbl #EQ destruct normalize nodelta
1381  /2 by le_S/
1382| #cond * #ltrue #i1 * #lfalse #i2 #i3 #IH1 #IH2 #IH3 whd in ⊢ (?%%);
1383  @fresh_append
1384  [ @(fresh_keep_n_ok … IH1) @le_S @max_1 @max_1 @max_1 @max_1 //
1385  | @fresh_append
1386    [ @(fresh_keep_n_ok … IH2) @le_S @max_1 @max_1 @max_1 @max_2 //
1387    | @(fresh_keep_n_ok … IH3) @le_S @max_1 @max_1 @max_2 //
1388    ]
1389  ]
1390| #cond * #ltrue #i1 * #lfalse #i2 #IH1 #IH2 whd in ⊢ (?%%); @fresh_append
1391  [ @(fresh_keep_n_ok … IH1) @le_S @max_1 @max_1 @max_1 //
1392  | @(fresh_keep_n_ok … IH2) @le_S @max_1 @max_1 @max_2 //
1393  ]
1394| #f #act_p * [| * #lbl] #i #IH whd in ⊢ (?%%); //
1395  change with ([?]@?) in ⊢ (?%?); @fresh_append
1396  [ whd % // @le_S @max_1 //
1397  | @(fresh_keep_n_ok … IH) @le_S @max_2 //
1398  ]
1399| * #lin #io * #lout #i #IH whd in ⊢ (?%%); @(fresh_keep_n_ok … IH)
1400  @le_S @max_1 @max_1 //
1401]
1402qed.
1403
1404lemma is_fresh_fresh_for_prog : ∀p,p'.∀prog : Program p p'.
1405is_fresh_for_return (labels_of_prog … prog) (fresh_for_prog … prog).
1406#p #p' * #env #main whd in match fresh_for_prog; normalize nodelta whd in ⊢ (?%?);
1407elim env // * #sig #cost #i #tail #IH  whd in ⊢ (?%?); @fresh_append
1408[ @(fresh_keep_n_ok … IH) @fresh_aux_ok @max_1 //
1409| @(fresh_keep_n_ok … (is_fresh_code … i)) whd in match fresh_for_prog_aux; normalize nodelta
1410  whd in ⊢ (??%); elim tail [ @max_2 // ] #hd1 #tl1 #IH1 @(transitive_le …  IH1)
1411  whd in ⊢ (??%); change with (fresh_for_prog_aux ?? (mk_Program ?? tl1 main) ?) in ⊢ (?%%);
1412  @fresh_aux_ok @max_1 //
1413]
1414qed.
1415
1416lemma subset_def : ∀A : DeqSet.∀l1,l2 : list A.(∀x.x ∈l1 → x ∈ l2) → l1 ⊆ l2.
1417#A #l1 elim l1 // #x #xs #IH #l2 #H %
1418[ @memb_to_mem >H // >memb_hd //
1419| @IH #y #H1 @H >memb_cons // >H1 //
1420]
1421qed.
1422
1423lemma memb_cast_return : ∀keep,x.x ∈ cast_return_to_cost_labels keep →
1424∃ y.x = a_return_post y ∧ bool_to_Prop (y ∈ keep).
1425#keep elim keep
1426[ #x *] #x #xs #IH #y whd in match cast_return_to_cost_labels;
1427whd in match (map ????); whd in match (memb ???); inversion(y==x)
1428[ #Hx #_ %{x} >(\P Hx) %{(refl …)} >memb_hd //
1429| #Hx normalize nodelta #H cases(IH … H) #z * #H1 #H2 %{z} %{H1} >memb_cons // >H2 //
1430]
1431qed.
1432
1433lemma subset_append : ∀A.∀l1,l2,l3 : list A.l1 ⊆ l3 → l2 ⊆ l3 → (l1 @ l2) ⊆ l3.
1434#A #l1 elim l1 // #x #xs #IH #l2 #l3 * #H1 #H2 #H3 % /2/
1435qed.
1436
1437lemma subset_def_inv : ∀A.∀l1,l2 : list A. l1 ⊆ l2 → ∀x.mem … x l1 → mem … x l2.
1438#A #l1 elim l1 [ #l2 * #x * ] #x #xs #IH #l2 * #H1 #H2 #y *
1439[ #EQ destruct // | #H3 @IH // ]
1440qed.
1441
1442lemma transitive_subset : ∀A.transitive … (subset A).
1443#A #l1 elim l1 // #x #xs #IH #l2 #l3 * #H1 #H2 #H3 %
1444[ @(subset_def_inv … H3) // | @IH // ]
1445qed.
1446
1447lemma subset_append_h1 : ∀A.∀l1,l2,l3 : list A.l1 ⊆ l3 → l1 ⊆ (l3 @ l2).
1448#A #l1 elim l1 // #x #x2 #IH #l2 #l3 * #H1 #H2 % [ @mem_append_l1 // | @IH // ]
1449qed.
1450
1451lemma subset_append_h2 : ∀A.∀l1,l2,l3 : list A.l2 ⊆ l3 → l2 ⊆ (l1 @ l3).
1452#A #l1 elim l1 // #x #xs #IH #l2 elim l2 // #y #ys #IH #l3 * #H1 #H2 %
1453[ @mem_append_l2 // | @IH // ]
1454qed.
1455
1456lemma lab_to_keep_in_prog : ∀p,p'.∀prog : Program p p'.
1457∀t_prog,m,keep.trans_prog … prog = 〈t_prog,m,keep〉 →
1458(cast_return_to_cost_labels keep) ⊆ (labels_of_prog p p' prog).
1459#p #p' * #env #main #t_prog #m #keep whd in match trans_prog; normalize nodelta
1460@pair_elim * #env1 #fresh * #m1 #keep1 #EQenv1 normalize nodelta #EQ destruct
1461lapply EQenv1 -EQenv1 lapply keep1 -keep1 lapply m1 -m1 lapply fresh -fresh
1462lapply env1 -env1 generalize in match (fresh_for_prog ???); elim env
1463[ #n #t_env #n1 #m #keep whd in ⊢ (??%? → ?); #EQ destruct whd in match (append ???);
1464  @subset_def #x #H whd in match (labels_of_prog); normalize nodelta
1465  whd in match (foldr ?????); cases(memb_cast_return … H) -H #x1 * #EQ1 #H destruct
1466  @(lab_to_keep_in_domain … H)
1467| #x #xs #IH #n #t_env #n1 #m #keep whd in ⊢ (??%? → ?); @pair_elim
1468  * #t_env_tail #fresh_tail * #t_m_tail #t_keep_tail
1469  change with (translate_env ????) in match (foldr ?????); #EQt_env_tail
1470  normalize nodelta #EQ1 destruct >cast_return_append @subset_append
1471  [ >cast_return_append @subset_append
1472    [ whd in match labels_of_prog; normalize nodelta whd in match (foldr ?????);
1473      @subset_def #y #H cases(memb_cast_return … H) -H #y1 * #EQ destruct #H
1474      >memb_append_l2 // @(lab_to_keep_in_domain … H)
1475    | whd in match labels_of_prog; normalize nodelta whd in match (foldr ?????);
1476      change with (labels_of_prog ?? (mk_Program ?? xs ?)) in match (foldr ?????);
1477      @subset_append_h1 @(transitive_subset … (IH … EQt_env_tail))
1478      >cast_return_append @subset_append_h1 //
1479    ]
1480  | whd in match labels_of_prog; normalize nodelta whd in match (foldr ?????);
1481    change with (labels_of_prog ?? (mk_Program ?? xs ?)) in match (foldr ?????);
1482    @subset_append_h1 @(transitive_subset … (IH … EQt_env_tail))
1483     >cast_return_append @subset_append_h2 //
1484  ]
1485]
1486qed.
1487
1488lemma fresh_call_post_trans_ok : ∀p.∀i : Instructions p.∀n,l.
1489n ≤ fresh … (call_post_trans … i n l).
1490#p #i elim i //
1491qed.
1492
1493lemma fresh_translate_env_ok : ∀p,p'.∀env,t_env : list (env_item p p').∀n,n1,m,keep.
1494translate_env … env n = 〈t_env,n1,m,keep〉 → n ≤ n1.
1495#p #p' #env elim env
1496[ #t_env #n #n1 #m #keep whd in ⊢ (??%? → ?); #EQ destruct // ]
1497#x #xs #IH #t_env #n #n1 #m #keep whd in ⊢ (??%? → ?);
1498change with (translate_env ????) in match (foldr ?????); @pair_elim
1499* #t_env_tail #fresh_tail * #t_m_tail #t_keep_tail #EQt_env_tail normalize nodelta
1500#EQ destruct @(transitive_le … (IH … EQt_env_tail)) @fresh_call_post_trans_ok
1501qed.
1502 
1503
1504lemma trans_env_ok : ∀p : state_params.∀ prog.
1505no_duplicates_labels … prog →
1506let 〈t_prog,m,keep〉 ≝ trans_prog … prog in
1507∀f,env_it.lookup p p (env … prog) f = return env_it →
1508let dom ≝ get_labels_of_code … (f_body … env_it) in
1509∃env_it',n.is_fresh_for_return keep n ∧lookup p p (env … t_prog) f = return env_it' ∧
1510let info ≝ call_post_trans … (f_body … env_it) n (nil ?) in
1511t_code … info = f_body … env_it' ∧
1512get_element … m (a_call (f_lab … env_it')) = (a_call (f_lab … env_it')) :: gen_labels … info ∧
1513f_sig … env_it = f_sig … env_it' ∧ f_lab … env_it = f_lab … env_it' ∧
1514same_fresh_map_on dom m (lab_map … info) ∧ same_to_keep_on dom keep (lab_to_keep … info).
1515#p #prog inversion(trans_prog … prog) * #t_prog0 #m0 #keep0 #EQt_prog
1516lapply EQt_prog normalize nodelta
1517generalize in match keep0 in ⊢ (% → ? → ? → ? → ? → ??(λ_.??(λ_.?%?)));
1518#keep1 #EQkeep1 inversion prog in EQt_prog; #env #main #EQprog
1519whd in match trans_prog; normalize nodelta
1520@pair_elim
1521cut(fresh_for_prog ?? prog ≤ fresh_for_prog ?? (mk_Program … env main)) [ >EQprog //]
1522generalize in match (fresh_for_prog ???) in ⊢ (??% → %);
1523lapply t_prog0 lapply m0 lapply keep0
1524elim env in ⊢ (?→ ? → ? → ? → ? → %);
1525[ #keep #m #t_prog #n #_ * #env' #fresh * #x #y #_ #_ #_ #f #env_it normalize in ⊢ (% → ?);  #ABS destruct]
1526* #hd_sig #hd_lab #hd_code #tail #IH #keep #m #t_prog #fresh1 #Hfresh1 * #env' #fresh * #m' #keep'
1527normalize in ⊢ (% → ?); normalize nodelta @pair_elim * #env_tail #fresh_tail
1528* #m_tail #keep_tail change with (translate_env ????) in ⊢ (??%? → ?); #EQtail normalize nodelta #EQ1 destruct(EQ1) #EQ2 destruct(EQ2)
1529whd in ⊢ (% → ?); whd in match (foldr ?????); * #Hhd_lab #H lapply(no_duplicates_append_r … H)
1530change with (no_duplicates_labels p p (mk_Program p p tail main)) in match
1531(no_duplicates_labels p p (mk_Program p p tail main)); #no_dup_tail
1532lapply(no_duplicates_append_l … H) #no_dup_head normalize nodelta
1533#f #env_it whd in ⊢ (??%? → ?); @eq_function_name_elim normalize nodelta
1534[ #EQ destruct(EQ) whd in ⊢ (???% → ?); #EQ destruct(EQ)
1535  inversion (call_post_trans … hd_code fresh_tail [])
1536  #gen_labs #t_hd_code #t_fresh #t_lab_map #t_lab_to_keep #EQ_trans_code
1537  %{(mk_env_item … hd_sig hd_lab t_hd_code)} %{fresh_tail} %
1538  [ %
1539    [ @(fresh_keep_n_ok … fresh1)
1540      [ @(fresh_keep_n_ok … Hfresh1)
1541        @(fresh_for_subset … (labels_of_prog … prog))
1542        [ @(lab_to_keep_in_prog … EQkeep1) | @is_fresh_fresh_for_prog ]
1543       | @(transitive_le … (fresh_translate_env_ok … EQtail)) //
1544      ]
1545    | whd in ⊢ (??%?); @eq_function_name_elim [2: * #H @⊥ @H %] #_ normalize nodelta
1546      @eq_f cases hd_sig // ]] >EQ_trans_code % [% [ % [ % [% // whd in ⊢ (??%?); >(\b (refl …)) %] % ] % | whd
1547    #x #Hx whd in ⊢ (??%?); >(? : (x == hd_lab) = false)
1548    [2: inversion(x==hd_lab) // #EQx_hdlab cases Hhd_lab -Hhd_lab #Hhd_lab cases Hhd_lab
1549        >memb_append_l1 // <(\P EQx_hdlab) >Hx // ]
1550    normalize nodelta >get_element_append_l1
1551    [2: cases daemon (*noduplicates*)] @get_element_append_l1
1552    % #H1 
1553    (* subproof with no nice statement *)
1554    lapply H1 -H1 lapply H -H lapply Hhd_lab -Hhd_lab lapply EQtail -EQtail
1555    generalize in match fresh1; lapply env_tail -env_tail lapply fresh_tail
1556    -fresh_tail lapply m_tail -m_tail lapply keep_tail -keep_tail elim tail
1557    normalize nodelta
1558    [ #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1559      #EQ destruct(EQ) whd in match (foldr ?????);
1560      #H1 #H2 * ]
1561    #head #tail #IH #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1562    whd in match (foldr ?????);
1563    @pair_elim * #env_tail_res #fresh_tail_res * #lab_map_res #keep_res #EQres
1564    normalize nodelta #EQ destruct(EQ) whd in match (foldr ?????);
1565    #H1 #H2 whd in match (memb ???); inversion(x == ?)
1566    [ #H3 #_ <(\P H3) in H2; change with ([?]@?) in match (?::?); #H2
1567      lapply(no_duplicates_append_commute … H2) -H2 ** #ABS #_ @ABS
1568      >memb_append_l2 // >Hx %
1569    | #H3 normalize nodelta #H4 @(IH … EQres)
1570      [3: >domain_of_associative_list_append in H4; #H4 cases(memb_append … H4) [2: #EQ >EQ %]
1571          #ABS @⊥ @(memb_no_duplicates_append … x … H2) // @orb_Prop_r >memb_append_l1 //
1572          @(lab_map_in_domain … (eq_true_to_b … ABS))
1573      | % #ABS elim H1 -H1 #H1 @H1 cases(memb_append … ABS)
1574        [ #H5 >memb_append_l1 //
1575        | #H5 >memb_append_l2 // @orb_Prop_r >memb_append_l2 //
1576        ]
1577      | lapply(no_duplicates_append_commute … H2) * #_ >associative_append
1578        #h @no_duplicates_append_commute @(no_duplicates_append_r … h)
1579      ]
1580    ]
1581    ]
1582  | whd #x #Hx >memb_append_l12 [2: cases daemon (*TODO*) ]
1583    >memb_append_l12 // inversion(memb ???) // #ABS @(memb_no_duplicates_append … (a_return_post x) … H)
1584    // @⊥
1585    (* subproof with no nice statement *)
1586    lapply ABS -ABS lapply H -H lapply Hhd_lab -Hhd_lab lapply EQtail -EQtail
1587    generalize in match fresh1; lapply env_tail -env_tail lapply fresh_tail
1588    -fresh_tail lapply m_tail -m_tail lapply keep_tail -keep_tail elim tail
1589    normalize nodelta
1590    [ #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1591      #EQ destruct(EQ) whd in match (foldr ?????);
1592      #H1 #H2 whd in ⊢ (??%? → ?); #EQ destruct ]
1593    #head #tail #IH #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1594    whd in match (foldr ?????);
1595    @pair_elim * #env_tail_res #fresh_tail_res * #lab_map_res #keep_res #EQres
1596    normalize nodelta #EQ destruct(EQ) whd in match (foldr ?????);
1597    #H1 #H2 #H3 cases(memb_append … H3) -H3
1598    [ #H3 change with ([?]@?) in match (?::?) in H2;
1599      lapply(no_duplicates_append_commute … H2) -H2 * #_ #H4 @(memb_no_duplicates_append … (a_return_post x) … H4)
1600      [ whd in match (append ???); >memb_append_l1 // >(lab_to_keep_in_domain … (eq_true_to_b … H3)) %
1601      | //
1602      ]
1603    | #H3 normalize nodelta @(IH … EQres)
1604      [3: //
1605      |  % #ABS elim H1 -H1 #H1 @H1 cases(memb_append … ABS)
1606        [ #H5 >memb_append_l1 //
1607        | #H5 >memb_append_l2 // @orb_Prop_r >memb_append_l2 //
1608        ]
1609      | lapply(no_duplicates_append_commute … H2) * #_ >associative_append
1610        #h @no_duplicates_append_commute @(no_duplicates_append_r … h)
1611      ]
1612    ]
1613  ]
1614| #Hf #Henv_it cases(IH … no_dup_tail … Henv_it)
1615  [9: >EQtail in ⊢ (??%?); %
1616  |13: %
1617  |6: assumption
1618  |10: %
1619  |*:
1620  ]
1621  #new_env_it * #new_fresh ** #is_fresh_newfresh #EQlook_new_env_it ***** #EQt_code #EQ_get_el
1622  #EQsign_env_it #EQ_f_lab #same_fresh_map #same_to_keep %{new_env_it} %{new_fresh}
1623  %
1624  [ %
1625     [ assumption
1626     | whd in ⊢ (??%?); @eq_function_name_elim [ #ABS >ABS in Hf; * #H @⊥ @H %]
1627        #_ normalize nodelta assumption ]]
1628   % [2: #x #Hx <same_to_keep // >associative_append @memb_append_l22
1629        inversion(memb ???) // #ABS lapply(lab_to_keep_in_domain … (eq_true_to_b … ABS))
1630        #ABS1 @(memb_no_duplicates_append … (a_return_post x) … H) //
1631        cases(lookup_ok_append … Henv_it) #l1 * #l2 * #EQ1 #EQ2 destruct(EQ1 EQ2)
1632        >foldr_map_append >memb_append_l2 // >foldr_map_append >memb_append_l1 //
1633        whd in match (foldr ?????); @orb_Prop_r >memb_append_l1 // >Hx % ]
1634   % [2: #x #Hx <same_fresh_map // >cons_append <associative_append
1635         <associative_append in ⊢ (??(???(??%?)?)?); >associative_append
1636         @(get_element_append_r1)
1637         % >domain_of_associative_list_append #ABS cases(memb_append … ABS)
1638         [ whd in match (memb ???); inversion(x==hd_lab) normalize nodelta
1639           [2: #_ whd in match (memb ???); #EQ destruct ] #EQx_hdlab #_
1640               <(\P EQx_hdlab) in Hhd_lab; cases(lookup_ok_append … Henv_it)
1641               #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1642               * #ABS1 @ABS1 >memb_append_l2 // >memb_append_l2 //
1643               >memb_append_l1 // whd in ⊢ (??%?); cases(x==?) //
1644               normalize nodelta >memb_append_l1 // >Hx %
1645         | #ABS1 @(memb_no_duplicates_append … x … H)
1646           [ @(lab_map_in_domain … (eq_true_to_b … ABS1))
1647           | cases(lookup_ok_append … Henv_it)
1648             #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1649             >memb_append_l2 // >memb_append_l1 //
1650             whd in ⊢ (??%?); cases(x==?) //
1651             normalize nodelta >memb_append_l1 // >Hx %
1652           ]
1653         ]
1654     ] 
1655   % // % // % // <EQ_get_el >cons_append <associative_append  <associative_append in ⊢ (??(???(??%?)?)?);
1656   >associative_append
1657   @get_element_append_r1 % >domain_of_associative_list_append #ABS cases(memb_append … ABS)
1658         [ whd in match (memb ???); inversion(a_call (f_lab … new_env_it)== a_call hd_lab)
1659           #EQ_hdlab normalize nodelta
1660           [2: whd in ⊢ (??%? → ?); #EQ destruct ] 
1661           #_ <(\P EQ_hdlab) in Hhd_lab; cases(lookup_ok_append … Henv_it)
1662           #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1663           * #ABS1 @ABS1 >memb_append_l2 // >memb_append_l2 //
1664           >memb_append_l1 // whd in ⊢ (??%?); >EQ_f_lab >(\b (refl …)) //
1665         | #ABS1 @(memb_no_duplicates_append … (a_call (f_lab … new_env_it)) … H)
1666           [ @(lab_map_in_domain … (eq_true_to_b … ABS1))
1667           | cases(lookup_ok_append … Henv_it)
1668             #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1669             >memb_append_l2 // >memb_append_l1 //
1670             whd in ⊢ (??%?); >EQ_f_lab >(\b (refl …)) //
1671           ]
1672         ]
1673]
1674qed.
1675
1676lemma len_append : ∀S : abstract_status.∀st1,st2,st3 : S.
1677∀t1 : raw_trace … st1 st2.∀t2 : raw_trace … st2 st3.
1678len … (t1 @ t2) = len … t1 + len …  t2.
1679#S #st1 #st2 #st3 #t1 elim t1 normalize //
1680qed.
1681
1682axiom permute_ok : ∀A.∀l1,l2,l3,l4,l5,l6,l7,l8,l9,x,y.
1683  (is_permutation A ((l5 @l1) @l6@l7) ((l4 @[]) @l6@l7)
1684 →is_permutation A ((l6 @l2) @l7) ((l3 @l8) @l9)
1685 →is_permutation A
1686   (y ::((l6 @((x ::l5) @(l1 @l2))) @l7))
1687   (((x ::l4 @y ::l3) @l8) @l9)).
1688   
1689
1690lemma correctness : ∀p,p',prog.
1691no_duplicates_labels … prog →
1692let 〈t_prog,m,keep〉 ≝ trans_prog … prog in
1693∀s1,s2,s1' : state p.∀abs_top,abs_tail.
1694∀t : raw_trace (operational_semantics … p' prog) s1 s2.
1695pre_measurable_trace … t →
1696state_rel … m keep abs_top abs_tail s1 s1' →
1697∃abs_top',abs_tail'.∃s2'.∃t' : raw_trace (operational_semantics … p' t_prog) s1' s2'.
1698state_rel  … m keep abs_top' abs_tail' s2 s2' ∧
1699is_permutation ? ((abs_top @ (map_labels_on_trace m (get_costlabels_of_trace … t))) @ abs_tail) 
1700                 (((get_costlabels_of_trace … t') @ abs_top' ) @ abs_tail') ∧
1701 len … t = len … t' ∧
1702 ∀k.∀i.option_hd … (cont … s2) = Some ? 〈ret_act (None ?),i〉 →
1703 cont … s1 = k @ cont … s2 →
1704All … (λx.let 〈lab,i〉 ≝ x in ¬ is_unlabelled_ret_act lab) k →
1705∃k'.cont … s1' = k' @ cont … s2' ∧ |k| = |k'|.
1706#p #p' #prog #no_dup @pair_elim * #t_prog #m #keep #EQtrans
1707#s1 #s2 #s1' #abs_top #abs_tail #t #Hpre lapply abs_top -abs_top lapply abs_tail
1708-abs_tail lapply s1' -s1' elim Hpre
1709[ #st #_ #s1' #abs_tail #abs_top #H %{abs_top} %{abs_tail} %{s1'} %{(t_base …)}
1710  %
1711  [ % [2: %] %{H} whd in match (get_costlabels_of_trace ????); >append_nil
1712    @is_permutation_eq
1713  | #k #i #_ #EQcont_st #wf %{[ ]} % // @sym_eq @eq_f @append_l1_injective_r //
1714  ]
1715| #st1 #st2 #st3 #lab whd in ⊢ (% → ?); #H inversion H in ⊢ ?; #st11 #st12
1716  [ * #lab1 #new_code #new_cont #EQcode11 #EQcont11 #EQcode12 #EQcont12 #EQstore
1717    #Hio11 #EQio12 #Hl1 #EQ1 #EQ2 #EQ3 #EQ4 destruct #tl #_ * #opt_l #EQ destruct(EQ)
1718    #pre_tl #IH #s1' #abs_tail #abs_top whd in match state_rel in ⊢ (% → ?); normalize nodelta >EQcont11 in ⊢ (% → ?);
1719    whd in match check_continuations; normalize nodelta whd in match (foldr2 ???????);
1720    inversion (cont ? s1') [ #_ *] * #l1' #new_code' #new_cont' #_ #EQconts1'
1721    normalize nodelta change with (check_continuations ?????) in match (foldr2 ???????);
1722    inversion(check_continuations ?????) [ #_ *] ** #H1 #l2 #ll2 #EQHl2
1723    >m_return_bind normalize nodelta inversion (call_post_clean ?????) [ #_ ***** ]
1724    * #l3 #code_c_st12 #EQcode_c_st12 normalize nodelta
1725    cases l1' in EQconts1'; (*in Hio11 Hl1 EQcont11 H;*) normalize nodelta
1726    [1,4: [ #x #y ] #_ (*#_ #_ #_ #H*) *****
1727    | #x #_ cases (ret_costed_abs ??) normalize nodelta [|#c] ******[|*] #EQ @⊥ >EQ in Hl1;
1728      normalize * /2/ ] *
1729    [ #EQconts1' normalize nodelta ****** #EQ destruct(EQ)
1730      #HH1 #EQ destruct(EQ) >EQcode11 in ⊢ (% → ?); inversion(code … s1')
1731      [
1732      | #x
1733      | #seq #lbl #i #_
1734      | #cond #ltrue #i1 #lfalse #i2 #i3 #_ #_ #_
1735      | #cond #ltrue #i1 #lfalse #i2 #_ #_
1736      | #f #act_p #ret_post #i #_
1737      | #l_in #io #l_out #i #_
1738      ]
1739      [|*: #_ whd in ⊢ (??%% → ?); [ #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ)]
1740           cases(call_post_clean ?????) normalize nodelta
1741           [1,3,5,7,9: #EQ destruct(EQ)] cases daemon (* da fare assurdi!!!*) ]
1742      #EQcodes1' whd in ⊢ (??%% → ?); #EQ destruct(EQ) #EQstore11 #EQio11 #EQ destruct(EQ)
1743      cases(IH (mk_state ? new_code' new_cont' (store … s1') false) ll2 abs_top …)
1744      [2: whd whd in match check_continuations; normalize nodelta
1745         change with (check_continuations ?????) in match (foldr2 ???????); >EQHl2
1746         normalize nodelta % // % // % // % // @EQcode_c_st12 ]
1747      #abs_top' * #abs_tail' * #st3' * #t' *** #Hst3st3' #EQcosts #EQlen #stack_safety
1748      %{abs_top'} %{abs_tail'} %{st3'} %{(t_ind … (cost_act (None ?)) …  t')}
1749      [ @hide_prf whd @(empty ????? 〈(cost_act (None ?)),?〉)
1750        [3: assumption |4: assumption |*:] /3 by nmk/ ]
1751      % [%   [2: whd in ⊢ (??%%); >EQlen % ] %{Hst3st3'} @EQcosts]
1752      * [| #hd #tl] #i #EQcont_st3 >EQcont11 #EQ whd in EQ : (???%);
1753      [ <EQ in EQcont_st3; whd in ⊢ (??%% → ?); #EQ1 destruct ]
1754      destruct * #_ #H cases(stack_safety … EQcont_st3 … e0 H) #k1 *
1755      #EQk1 #EQlength %{(〈cost_act (None ?),new_code'〉::k1)}
1756      % [ >EQk1 % | whd in ⊢ (??%%); @eq_f // ]
1757    | #lbl #EQconts1' normalize nodelta ******* #EQ destruct(EQ)
1758      #HH1 #EQ destruct(EQ) #EQget_el >EQcode11 in ⊢ (% → ?);
1759      inversion(code … s1')
1760      [
1761      | #x
1762      | #seq #lbl #i #_
1763      | #cond #ltrue #i1 #lfalse #i2 #i3 #_ #_ #_
1764      | #cond #ltrue #i1 #lfalse #i2 #_ #_
1765      | #f #act_p #ret_post #i #_
1766      | #l_in #io #l_out #i #_
1767      ]
1768      [|*: #_ whd in ⊢ (??%% → ?); #EQ cases daemon (* da fare assurdi !!!*) ]
1769      #EQcodes1' whd in ⊢ (??%% → ?); #EQ destruct(EQ) #EQstore #EQio #EQ destruct(EQ)
1770      cases(IH (mk_state ? new_code' new_cont' (store … s1') false) ll2 l3 …)
1771      [2: whd whd in match check_continuations; normalize nodelta
1772         change with (check_continuations ?????) in match (foldr2 ???????);
1773         >EQHl2 in ⊢ (match % with [ _⇒ ? | _ ⇒ ?]);
1774         normalize nodelta % // % // % // % // @EQcode_c_st12 ]
1775      #abs_top' * #abs_tail' * #st3' * #t' *** #Hst3st3' #EQcost #EQlen #stack_safety
1776      %{abs_top'} %{abs_tail'} %{st3'}
1777      %{(t_ind … (cost_act (Some ? lbl)) … t')}
1778      [ @hide_prf whd @(empty ????? 〈(cost_act (Some ? lbl)),?〉)
1779        [3: assumption |4: assumption |*:] /3 by nmk/ ]
1780      % [% [2: whd in ⊢ (??%%); >EQlen % ] %{Hst3st3'} >map_labels_on_trace_append
1781          whd in match (map_labels_on_trace ? [lbl]); >append_nil >EQget_el
1782          @is_permutation_cons assumption
1783        | * [| #hd #tl] #i #EQcont_st3 >EQcont11 #EQ whd in EQ : (???%);
1784          [ <EQ in EQcont_st3; whd in ⊢ (??%% → ?); #EQ1 destruct ]
1785          destruct * #_ #H cases(stack_safety … EQcont_st3 … e0 H) #k1 *
1786          #EQk1 #EQlength %{(〈cost_act (Some ? lbl),new_code'〉::k1)}
1787          % [ >EQk1 % | whd in ⊢ (??%%); @eq_f // ]
1788        ]
1789    ]
1790  | #seq #i #mem * [|#lbl] #EQcode_st11 #EQeval_seq #EQ destruct(EQ) #EQcont
1791    #EQ destruct(EQ) #EQio11 #EQio12 #EQ1 #EQ2 #EQ3 #EQ4 destruct #t #Hst11
1792    #_ #Hpre_t #IH #s1' #abs_tail #abs_top whd in ⊢ (% → ?);
1793    inversion(check_continuations ?????) normalize nodelta [1,3: #_ *]
1794    ** #H1 #abs_top_cont #abs_tail_cont #EQcheck normalize nodelta ****
1795    #HH1 [ >EQcode_st11 in ⊢ (% → ?); | >EQcode_st11 in ⊢ (% → ?); ]
1796    inversion(code ??) [1,2,4,5,6,7,8,9,11,12,13,14: cases daemon (*assurdi*)]
1797    #seq1 #opt_l #i' #_ #EQcode_s1' change with (m_bind ?????) in ⊢ (??%? → ?);
1798    inversion(call_post_clean ????) [1,3: #_ whd in ⊢ (??%% → ?); #EQ destruct]
1799    * #gen_labs #i'' #EQclean >m_return_bind inversion(opt_l) normalize nodelta
1800    [2,4: #lab1 ] #EQ destruct(EQ)
1801    [1,2: inversion(?==?) normalize nodelta #EQget_el ]
1802    whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ)
1803    #EQstore #EQinfo #EQ destruct(EQ)
1804    cases(IH … (mk_state ? i' (cont … s1') (store … st12) (io_info … s1')) abs_tail_cont ?)
1805    [3,6: whd
1806       [ <EQcont in ⊢ (match % with [_ ⇒ ? | _⇒ ?]); >EQcheck in ⊢ (match % with [_ ⇒ ? | _⇒ ?]);
1807       | <EQcont in ⊢ (match % with [_ ⇒ ? | _⇒ ?]); >EQcheck in ⊢ (match % with [_ ⇒ ? | _⇒ ?]);
1808       ]
1809       normalize nodelta % // % // % // % // assumption
1810    |2,5:
1811    ]
1812    #abs_top1 * #abs_tail1 * #s2' * #t' *** #Hst3_s2' #EQcosts #EQlen #stack_safety
1813    %{abs_top1}
1814    %{abs_tail1} %{s2'} %{(t_ind … t')}
1815    [1,4: @hide_prf @(seq_sil … EQcode_s1') //
1816    |2,5:
1817    ]
1818    % [1,3: % [2,4: whd in ⊢ (??%%); @eq_f // ] %{Hst3_s2'} [2: assumption]
1819        whd in ⊢ (??%%); whd in match (get_costlabels_of_trace ????);
1820        whd in match (map_labels_on_trace ??); >(\P EQget_el) @is_permutation_cons
1821        assumption
1822      |*: #k #i #EQcont_st3 >EQcont #EQ #H
1823          cases(stack_safety … EQcont_st3 … EQ H) #k1 * #EQk1 #EQlength %{k1} % //
1824      ]
1825  | #cond #ltrue #i_true #lfalse #i_false #i #mem #EQcode_st11 #EQcond #EQcont_st12
1826    #EQ1 #EQ2 destruct(EQ1 EQ2) #EQio_st11 #EQio_st12 #EQ1 #EQ2 #EQ3 #EQ4 destruct #t
1827    #Hclass #_ #Hpre #IH #s1' #abs_tail #abs_top whd in ⊢ (% → ?); inversion(check_continuations ?????)
1828    [ #_ *] ** #H1 #abs_top_cont #abs_tail_cont #EQcheck normalize nodelta **** #HH1
1829    >EQcode_st11 in ⊢ (% → ?); inversion(code ??)
1830    [1,2,3,5,6,7: cases daemon (*ASSURDI*)] #cond1 #ltrue1 #i_true1 #lfalse1 #ifalse1 #i' #_ #_ #_
1831    #EQcode_s1' whd in ⊢ (??%% → ?); inversion(call_post_clean ?????) normalize nodelta
1832    [ #_ #EQ destruct ] * #gen_lab_i' #i'' #EQi'' inversion(call_post_clean ????)
1833    [ #_ whd in ⊢ (??%% → ?); #EQ destruct] * #gen_lab_ifalse1 #i_false' #EQi_false'
1834    >m_return_bind inversion(call_post_clean ????) [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
1835    * #gen_lab_i_true' #i_true' #EQi_true' >m_return_bind inversion(?==?) normalize nodelta
1836    [2: #_ #EQ destruct] #EQget_ltrue1 inversion(?==?) normalize nodelta #EQget_lfalse1
1837    whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ) #EQstore11 #EQio11
1838    #EQ destruct(EQ)
1839    cases(IH …
1840     (mk_state ? i_true1 (〈cost_act (None ?),i'〉 :: cont … s1') (store … st12) (io_info … st12))
1841     abs_tail_cont gen_lab_i_true')
1842    [2: whd whd in match (check_continuations ?????);
1843        >EQcont_st12 in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?]); whd in match (foldr2 ???????);
1844        change with (check_continuations ?????) in match (foldr2 ???????);
1845        >EQcheck in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?]); normalize nodelta
1846        >EQi'' normalize nodelta % // % // % // % [2: assumption ] % // % //
1847    ] #abs_top1 * #abs_tail1 * #s2' * #t' *** #Hst3_s2' #EQcost #EQlen
1848      #stack_safety %{abs_top1} %{abs_tail1}
1849    %{s2'} %{(t_ind … t')}
1850    [ @hide_prf @(cond_true … EQcode_s1') //
1851    |
1852    ]
1853    %
1854    [ % [2: whd in ⊢ (??%%); @eq_f // ] %{Hst3_s2'}
1855      whd in match (get_costlabels_of_trace ????); whd in match(map_labels_on_trace ??);
1856      >(\P EQget_ltrue1) whd in match (get_costlabels_of_trace ????) in ⊢ (???%); @is_permutation_cons
1857      assumption
1858    |  #k #i1 #EQcont_st3 #EQcont_st11 #H
1859       cases(stack_safety … (〈cost_act (None NonFunctionalLabel),i〉::k) … EQcont_st3 …)
1860       [2: >EQcont_st12 >EQcont_st11 % |3: % // % whd in ⊢ (% → ?); #EQ destruct]
1861       * [|#hd1 #tl1] * #EQk1 whd in ⊢ (??%% → ?); #EQlength destruct
1862       %{tl1} % // whd in EQk1 : (??%%); destruct //
1863    ]
1864  | #cond #ltrue #i_true #lfalse #i_false #i #mem #EQcode_st11 #EQcond #EQcont_st12
1865    #EQ1 #EQ2 destruct(EQ1 EQ2) #EQio_st11 #EQio_st12 #EQ1 #EQ2 #EQ3 #EQ4 destruct #t
1866    #Hclass #_ #Hpre #IH #s1' #abs_tail #abs_top whd in ⊢ (% → ?); inversion(check_continuations ?????)
1867    [ #_ *] ** #H1 #abs_top_cont #abs_tail_cont #EQcheck normalize nodelta **** #HH1
1868    >EQcode_st11 in ⊢ (% → ?); inversion(code ??)
1869    [1,2,3,5,6,7: cases daemon (*ASSURDI*)] #cond1 #ltrue1 #i_true1 #lfalse1 #ifalse1 #i' #_ #_ #_
1870    #EQcode_s1' whd in ⊢ (??%% → ?); inversion(call_post_clean ?????) normalize nodelta
1871    [ #_ #EQ destruct ] * #gen_lab_i' #i'' #EQi'' inversion(call_post_clean ????)
1872    [ #_ whd in ⊢ (??%% → ?); #EQ destruct] * #gen_lab_ifalse1 #i_false' #EQi_false'
1873    >m_return_bind inversion(call_post_clean ????) [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
1874    * #gen_lab_i_true' #i_true' #EQi_true' >m_return_bind inversion(?==?) normalize nodelta
1875    [2: #_ #EQ destruct] #EQget_ltrue1 inversion(?==?) normalize nodelta #EQget_lfalse1
1876    whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ) #EQstore11 #EQio11
1877    #EQ destruct(EQ)
1878    cases(IH …
1879     (mk_state ? ifalse1 (〈cost_act (None ?),i'〉 :: cont … s1') (store … st12) (io_info … st12))
1880     abs_tail_cont gen_lab_ifalse1)
1881    [2: whd whd in match (check_continuations ?????);
1882        >EQcont_st12 in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?]); whd in match (foldr2 ???????);
1883        change with (check_continuations ?????) in match (foldr2 ???????);
1884        >EQcheck in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?]); normalize nodelta
1885        >EQi'' normalize nodelta % // % // % // % [2: assumption ] % // % //
1886    ] #abs_top1 * #abs_tail1 * #s2' * #t' *** #Hst3_s2' #EQcost #EQlen #stack_safety
1887    %{abs_top1} %{abs_tail1} %{s2'} %{(t_ind … t')}
1888    [ @hide_prf @(cond_false … EQcode_s1') //
1889    |
1890    ]
1891    %
1892    [ % [2: whd in ⊢ (??%%); @eq_f // ] %{Hst3_s2'}
1893      whd in match (get_costlabels_of_trace ????); whd in match(map_labels_on_trace ??);
1894      >(\P EQget_lfalse1) whd in match (get_costlabels_of_trace ????) in ⊢ (???%); @is_permutation_cons
1895      assumption
1896    | #k #i1 #EQcont_st3 #EQcont_st11 #H
1897      cases(stack_safety … (〈cost_act (None NonFunctionalLabel),i〉::k) … EQcont_st3 …)
1898      [2: >EQcont_st12 >EQcont_st11 % |3: % // % whd in ⊢ (% → ?); #EQ destruct]
1899      * [|#hd1 #tl1] * #EQk1 whd in ⊢ (??%% → ?); #EQlength destruct
1900      %{tl1} % // whd in EQk1 : (??%%); destruct //
1901    ]
1902 | #cond #ltrue #i_true #lfalse #i_false #store #EQcode_st11 #EQev_loop #EQcont_st12 #EQ1 #EQ2 destruct
1903   #EQio_11 #EQio_12 #EQ1 #EQ2 #EQ3 #EQ4 destruct #t #Hclass11 #_ #Hpre #IH #s1' #abs_top #abs_tail
1904   whd in ⊢ (% → ?); inversion(check_continuations ?????) [ #_ * ] ** #H1 #abs_top_cont #abs_tail_cont
1905   #EQcheck normalize nodelta **** #HH1 >EQcode_st11 in ⊢ (% → ?); inversion(code ??)
1906   [1,2,3,4,6,7: cases daemon (*assurdi*) ] #cond1 #ltrue1 #i_true1 #lfalse1 #i_false1 #_ #_
1907   #EQcode_s1' whd in ⊢ (??%% → ?); inversion(call_post_clean ?????) normalize nodelta
1908   [ #_ #EQ destruct] * #gen_ifalse1 #i_false2 #EQi_false2 inversion(call_post_clean ?????)
1909   [ #_ whd in ⊢ (??%% → ?); #EQ destruct ] * #gen_itrue1 #i_true2 #EQi_true2 >m_return_bind
1910   inversion (?==?) normalize nodelta [2: #_ #EQ destruct] #EQget_ltrue1
1911   inversion(?==?) #EQget_lfalse1 whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ)
1912   -EQ #EQ destruct(EQ) #EQstore_11 #EQ_info11 #EQ destruct
1913   cases(IH …
1914    (mk_state ? i_true1
1915     (〈cost_act (None ?),LOOP … cond ltrue i_true1 lfalse i_false1〉 ::
1916        cont … s1') (store … st12) (io_info … st12)) abs_tail_cont gen_itrue1)
1917   [2: whd >EQcont_st12 whd in match (check_continuations ?????);
1918       whd in match (foldr2 ???????);
1919       change with (check_continuations ?????) in match (foldr2 ???????);
1920       >EQcheck normalize nodelta whd in match (call_post_clean ?????);
1921       >EQi_false2 normalize nodelta >EQi_true2 >m_return_bind >EQget_ltrue1
1922       >EQget_lfalse1 normalize nodelta % // % // % // % [2: assumption] % //
1923       % //
1924   ]
1925   #abs_cont1 * #abs_tail1 * #s2' * #t' *** #Hst3_s2' #EQcosts #EQlen #stack_safety
1926   %{abs_cont1} %{abs_tail1} %{s2'} %{(t_ind … t')}
1927   [ @hide_prf @(loop_true … EQcode_s1') // |]
1928   %
1929   [ % [2: whd in ⊢ (??%%); @eq_f //] %{Hst3_s2'}
1930     whd in match (get_costlabels_of_trace ????); whd in match(map_labels_on_trace ??);
1931      >(\P EQget_ltrue1) whd in match (get_costlabels_of_trace ????) in ⊢ (???%);
1932     @is_permutation_cons assumption
1933   | #k #i1 #EQcont_st3 #EQcont_st11 #H
1934      cases(stack_safety … (〈cost_act (None NonFunctionalLabel), LOOP p cond ltrue (code p st12) lfalse i_false〉::k) … EQcont_st3 …)
1935      [2: >EQcont_st12 >EQcont_st11 % |3: % // % whd in ⊢ (% → ?); #EQ destruct]
1936      * [|#hd1 #tl1] * #EQk1 whd in ⊢ (??%% → ?); #EQlength destruct
1937      %{tl1} % // whd in EQk1 : (??%%); destruct //
1938   ]
1939 | #cond #ltrue #i_true #lfalse #i_false #mem #EQcode_st11 #EQev_loop #EQcont_st12 #EQ1 #EQstore11 destruct
1940   #EQio_11 #EQio_12 #EQ1 #EQ2 #EQ3 #EQ4 destruct #t #Hclass11 #_ #Hpre #IH #s1' #abs_top #abs_tail
1941   whd in ⊢ (% → ?); inversion(check_continuations ?????) [ #_ * ] ** #H1 #abs_top_cont #abs_tail_cont
1942   #EQcheck normalize nodelta **** #HH1 >EQcode_st11 in ⊢ (% → ?); inversion(code ??)
1943   [1,2,3,4,6,7: cases daemon (*assurdi*) ] #cond1 #ltrue1 #i_true1 #lfalse1 #i_false1 #_ #_
1944   #EQcode_s1' whd in ⊢ (??%% → ?); inversion(call_post_clean ?????) normalize nodelta
1945   [ #_ #EQ destruct] * #gen_ifalse1 #i_false2 #EQi_false2 inversion(call_post_clean ?????)
1946   [ #_ whd in ⊢ (??%% → ?); #EQ destruct ] * #gen_itrue1 #i_true2 #EQi_true2 >m_return_bind
1947   inversion (?==?) normalize nodelta [2: #_ #EQ destruct] #EQget_ltrue1
1948   inversion(?==?) #EQget_lfalse1 whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ)
1949   -EQ #EQ destruct(EQ) #EQstore_11 #EQ_info11 #EQ destruct
1950   cases(IH …
1951    (mk_state ? i_false1
1952     (cont … s1') (store … st12) (io_info … st12)) abs_tail_cont gen_ifalse1)
1953   [2: whd >EQcont_st12 whd in match (check_continuations ?????);
1954       whd in match (foldr2 ???????);
1955       change with (check_continuations ?????) in match (foldr2 ???????);
1956       >EQcheck normalize nodelta whd in match (call_post_clean ?????);
1957       >EQi_false2 normalize nodelta >EQi_true2
1958        % // % // % // % [2: %] //
1959   ]
1960   #abs_cont1 * #abs_tail1 * #s2' * #t' *** #Hst3_s2' #EQcosts #EQlen #stack_safety
1961   %{abs_cont1} %{abs_tail1} %{s2'} %{(t_ind … t')}
1962   [ @hide_prf @(loop_false … EQcode_s1') // |]
1963   %
1964   [ % [2: whd in ⊢ (??%%); @eq_f //] %{Hst3_s2'}
1965     whd in match (get_costlabels_of_trace ????); whd in match(map_labels_on_trace ??);
1966     >(\P EQget_lfalse1) whd in match (get_costlabels_of_trace ????) in ⊢ (???%);
1967     @is_permutation_cons assumption
1968   | #k #i #EQcont_st3 <EQcont_st12 #EQ #H
1969     cases(stack_safety … EQcont_st3 … EQ H) #k1 * #EQk1 #EQlength %{k1} % //
1970   ]
1971 | #lin #io #lout #i #mem #EQcode_st11 #EQev_io #EQcost_st12 #EQcont_st12
1972   #EQ destruct #EQio_12 #EQ1 #EQ2 #EQ3 #EQ4 destruct #t #Hclass_11 #_ #Hpre
1973   #IH #s1' #abs_top #abs_tail whd in ⊢ (% → ?); inversion(check_continuations ?????)
1974   [#_ *] ** #H1 #abs_top_cont #abs_tail_cont #EQcheck normalize nodelta ****
1975   #HH1 >EQcode_st11 in ⊢ (% → ?); inversion(code ??)
1976   [1,2,3,4,5,6: cases daemon (*ASSURDI*)] #lin1 #io1 #lout1 #i1 #_ #EQcode_s1'
1977   whd in ⊢ (??%% → ?); inversion(call_post_clean ?????) normalize nodelta
1978   [ #_ #EQ destruct] * #gen_lab #i2 #EQ_i2 inversion(?==?) normalize nodelta
1979   [2: #_ #EQ destruct] #EQget_lout1 inversion(?==?) #EQget_lin1 normalize nodelta
1980   whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ)
1981   #EQstore11 #EQinfo11 #EQ destruct
1982   cases(IH …
1983    (mk_state ? (EMPTY p) (〈cost_act (Some ? lout),i1〉::cont … s1') (store … st12) true)
1984    abs_tail_cont [ ])
1985   [2: whd >EQcont_st12 whd in match (check_continuations ?????);
1986       whd in match (foldr2 ???????);
1987       change with (check_continuations ?????) in match (foldr2 ???????);
1988       >EQcheck normalize nodelta >EQ_i2 normalize nodelta % // % // % // %
1989       [2: >EQcost_st12 % ] % [ % // % //] >(\P EQget_lout1) %
1990   ]
1991   #abs_cont1 * #abs_tail1 * #s2' * #t' *** #Hst3_s2' #EQcosts #EQlen #stack_safety
1992   %{abs_cont1} %{abs_tail1} %{s2'} %{(t_ind … t')}
1993   [ @hide_prf @(io_in … EQcode_s1') // |]
1994   %
1995   [ % [2: whd in ⊢ (??%%); @eq_f //] %{Hst3_s2'}
1996     whd in match (get_costlabels_of_trace ????); whd in match(map_labels_on_trace ??);
1997     >(\P EQget_lin1) whd in match (get_costlabels_of_trace ????) in ⊢ (???%);
1998     @is_permutation_cons assumption
1999   | #k #i1 #EQcont_st3 #EQcont_st11 #H
2000      cases(stack_safety … (〈cost_act (Some ? lout),i〉::k) … EQcont_st3 …)
2001      [2: >EQcont_st12 >EQcont_st11 % |3: % // % whd in ⊢ (% → ?); #EQ destruct]
2002      * [|#hd1 #tl1] * #EQk1 whd in ⊢ (??%% → ?); #EQlength destruct
2003      %{tl1} % // whd in EQk1 : (??%%); destruct //
2004   ]
2005  | #f #act_p #r_lb #cd #mem #env_it #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
2006    #EQ11 #EQ12 destruct #tl #_ * #x #EQ destruct(EQ)
2007  | #r_t #mem #con #rb #i #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10 #EQ11 #EQ12
2008    destruct #t #_ * #nf #EQ destruct
2009  ]
2010| #st1 #st2 #st3 #lab #st1_noio #H inversion H in ⊢ ?; [1,2,3,4,5,6,7,8: cases daemon (*assurdi*)]
2011  #st11 #st12 #r_t #mem #cont * [|#x] #i #EQcode_st11 #EQcont_st11 #EQ destruct(EQ)
2012  #EQio_11 #EQio_12 #EQev_ret #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 destruct(EQ1 EQ2 EQ3 EQ4 EQ5 EQ6)
2013  #t * #lbl #EQ destruct(EQ) #pre_t #IH #s1' #abs_tail #abs_top whd in ⊢ (% → ?);
2014  inversion(check_continuations …) [#_ *] ** #H1 #abs_top_cont #abs_tail_cont
2015  >EQcont_st11 in ⊢ (% → ?); inversion(cont … s1') [ #_ whd in ⊢ (??%% → ?); #EQ destruct(EQ)]
2016  * #act_lab #i #cont_s1'' #_ #EQcont_s1' whd in ⊢ (??%% → ?);
2017  change with (check_continuations ?????) in match (foldr2 ???????);
2018  inversion(check_continuations ?????) normalize nodelta [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
2019  ** #H2 #abs_top_cont' #abs_tail_cont' #EQcheck normalize nodelta
2020  inversion(call_post_clean ?????) normalize nodelta [ #_ whd in ⊢ (??%% → ?); #EQ destruct *****]
2021  * #gen_labs #i' #EQi' inversion act_lab normalize nodelta
2022  [ #f #lab | * [| #lbl1 ]| * [| #nf_l] |] #EQ destruct(EQ)
2023  [1,6: whd in ⊢ (??%% → ?); #EQ destruct *****
2024  |4,5: normalize nodelta whd in ⊢ (??%% → ?); #EQ destruct(EQ) ****** [2: *] #EQ destruct
2025  ]
2026  whd in match ret_costed_abs; normalize nodelta
2027  [ whd in ⊢ (??%% → ?); #EQ destruct(EQ) ****** #EQ destruct(EQ) ]
2028  inversion (memb ???) normalize nodelta #Hlbl1 whd in ⊢ (??%% → ?); #EQ destruct(EQ)
2029  ****** [ * ] #EQ destruct(EQ) #HH2 #EQ destruct #EQget_el >EQcode_st11 in ⊢ (% → ?);
2030  inversion(code … s1') [1,3,4,5,6,7: cases daemon (*TODO*) ] #r_t' #EQcode_s1'
2031  whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ)
2032  #EQstore11 #EQio_11 #EQ destruct
2033  cases(IH … (mk_state ? i cont_s1'' (store … st12) (io_info … st12)) abs_tail_cont gen_labs)
2034  [2: whd >EQcheck normalize nodelta % // % // % // % // @EQi' ]
2035  #abs_top1 * #abs_tail1 * #s2' * #t' *** #Hst3_s2' #EQcosts #EQlen #stack_safety
2036  %{abs_top1} %{abs_tail1} %{s2'} %{(t_ind … t')}
2037  [ @hide_prf @(ret_instr … EQcode_s1' … EQcont_s1') //
2038  | ]
2039  %
2040  [ % [2: whd in ⊢ (??%%); @eq_f // ] %{Hst3_s2'}
2041    whd in match (get_costlabels_of_trace ????);
2042    whd in match (get_costlabels_of_trace ????) in ⊢ (???%);
2043    whd in match (map_labels_on_trace ??); >EQget_el @is_permutation_cons
2044    assumption
2045  | * [| #hd #tl] #i1 #EQcont_st3 >EQcont_st11 #EQ whd in EQ : (???%);
2046    [ <EQ in EQcont_st3; whd in ⊢ (??%% → ?); #EQ1 destruct ]
2047    destruct * #_ #H cases(stack_safety … EQcont_st3 … e0 H) #k1 *
2048    #EQk1 #EQlength %{(〈ret_act (Some ? lbl1),i〉::k1)}
2049    % [ >EQk1 % | whd in ⊢ (??%%); @eq_f // ]
2050  ]
2051| #st1 #st2 #st3 #lab #H inversion H in ⊢ ?; #st11 #st12
2052  [1,2,3,4,5,6,7,9: cases daemon (*absurd!*)
2053  | #f #act_p #opt_l #i #mem #env_it #EQcode11 #EQenv_it #EQmem
2054    #EQ destruct(EQ) #EQcode12 #EQcont12 #EQio11 #EQio12 #EQ1 #EQ2 #EQ3
2055    destruct(EQ1 EQ2 EQ3) #EQ destruct(EQ) #tl #_ #_ whd in ⊢ (?% → ?); >EQcode11 in ⊢ (% → ?);
2056    normalize nodelta cases opt_l in EQcode11 EQcont12; normalize nodelta [ #x #y *]
2057    #lbl #EQcode11 #EQcont12 * #pre_tl #IH #st1' #abs_top #abs_tail
2058    whd in ⊢ (% → ?); inversion(check_continuations ????) [ #_ *] ** #H1
2059    #abs_top_cont #abs_tail_cont #EQcheck
2060    normalize nodelta **** #HH1 >EQcode11 in ⊢ (% → ?); inversion(code … st1')
2061    [1,2,3,4,5,7: (*assurdi da fare*) cases daemon] #f' #act_p' #opt_l' #i' #_
2062    #EQcode_st1' #EQclean #EQstore #EQio #EQ destruct(EQ)
2063    lapply(trans_env_ok … no_dup) >EQtrans normalize nodelta #H
2064    cases(H … EQenv_it) -H #env_it' * #fresh' ** #is_fresh_fresh'
2065    #EQenv_it' ***** #EQtrans
2066    #EQgen_labels #EQsignature #EQlab_env_it #same_map #same_keep
2067    change with (m_bind ?????) in EQclean : (??%?); inversion(call_post_clean ?????) in EQclean;
2068    [ #_ whd in ⊢ (??%% → ?); #EQ destruct] * #abs_top'' #i'' #EQi' >m_return_bind
2069    inversion opt_l' in EQcode_st1'; [| #lbl'] #EQopt_l' #EQcode_st1' normalize nodelta
2070    [2: inversion(memb ???) normalize nodelta #Hlbl_keep
2071        [  inversion (get_element ????) normalize nodelta [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
2072           #lbl'' #l3' #_ #EQget_el whd in match (eqb ???); inversion (eqb ???) normalize nodelta
2073          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true ? lbl'' lbl') #H1 #_
2074          lapply(H1 H) -H -H1 #EQ destruct(EQ) inversion(eqb_list ???) normalize nodelta
2075          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true (DeqSet_List ?) l3' abs_top'')
2076          #H1 #_ lapply(H1 H) -H -H1 #EQ destruct(EQ) whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ
2077          #EQ destruct(EQ)
2078          cases(IH (mk_state ? (f_body … env_it') (〈ret_act opt_l',i'〉 :: (cont … st1')) (store ? st12) false) (abs_tail_cont) (gen_labels … (call_post_trans … (f_body … env_it) fresh' (nil ?))))
2079          [2: whd >EQcont12
2080            change with (m_bind ??? (check_continuations ?????) ?) in match (check_continuations ?????);
2081            >EQcheck >m_return_bind normalize nodelta >EQi' normalize nodelta >EQopt_l'
2082            whd in match ret_costed_abs; normalize nodelta >Hlbl_keep normalize nodelta
2083            % // % // % // % [/5 by conj/] >EQgen_labels >EQcode12 <EQtrans
2084            @(inverse_call_post_trans … is_fresh_fresh')
2085            [2: % |*: [2,3: /2 by / ]
2086                cases(lookup_ok_append … EQenv_it) #env1 * #env2 * #EQ1 #EQ2
2087                whd in no_dup; destruct >EQ1 in no_dup; >foldr_map_append >foldr_map_append
2088                #no_dup lapply(no_duplicates_append_r … no_dup) #H1
2089                lapply(no_duplicates_append_l … H1) whd in match (foldr ?????); -H1
2090                change with ([?]@?) in match (?::?); #H1
2091                lapply(no_duplicates_append_r … H1) >append_nil //
2092            ]
2093          ]
2094          #abs_top''' * #abs_tail''' * #st3' * #t' *** #Hst3st3' #EQcosts #EQlen #stack_safety
2095          %{abs_top'''}
2096          %{abs_tail'''} %{st3'} %{(t_ind … (call_act f (f_lab … env_it')) …  t')}
2097          [ @hide_prf @call /2 width=10 by jmeq_to_eq/ ]
2098          %
2099          [ % [2: whd in ⊢ (??%%); >EQlen %]
2100            %{Hst3st3'} >map_labels_on_trace_append whd in match (get_costlabels_of_trace ????) in ⊢ (???%);
2101            >EQlab_env_it >associative_append whd in match (append ???); >associative_append
2102            >associative_append in ⊢ (???%); >(associative_append … [?]) in ⊢ (???%);
2103            whd in match (map_labels_on_trace ??); >EQgen_labels @is_permutation_cons
2104            >append_nil whd in match (append ???) in ⊢ (???%); //
2105          | #k #i1 #EQcont_st3 #EQcont_st11 #H
2106            cases(stack_safety … (〈ret_act (Some ? lbl),i〉::k) … EQcont_st3 …)
2107            [2: >EQcont12 >EQcont_st11 % |3: % // % whd in ⊢ (% → ?); #EQ destruct]
2108            * [|#hd1 #tl1] * #EQk1 whd in ⊢ (??%% → ?); #EQlength destruct
2109            %{tl1} % // whd in EQk1 : (??%%); destruct //
2110          ]
2111        | whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ)
2112        ]
2113    | whd in ⊢ (??%% → ?); #EQ destruct(EQ)
2114    ]
2115  ]
2116| #st1 #st2 #st3 #st4 #st5 #l1 #l2 #H inversion H in ⊢ ?;
2117  [1,2,3,4,5,6,7,9: cases daemon (*assurdi*) ]
2118  #st11 #st12 #f #act_p #opt_l #i #mem #env_it #EQcode11 #EQenv_it #EQmem
2119  #EQ destruct(EQ) #EQcode12 #EQcont12 #EQio11 #EQio12 #EQ1 #EQ2 #EQ3
2120  destruct(EQ1 EQ2 EQ3) #EQ destruct(EQ) #t1 #t2 #H inversion H in ⊢ ?;
2121  [1,2,3,4,5,6,7,8: cases daemon (*assurdi*) ]
2122   #st41 #st42 #r_t #mem1 #new_cont #opt_l1 #cd #EQcode41 #EQcont41 #EQ destruct(EQ)
2123   #EQio41 #EQio42 #EQmem1 #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 destruct #st11_noio
2124   #st41_noio #_ whd in ⊢ (?(?%) → ?); >EQcode11 in ⊢ (% → ?); normalize nodelta
2125   inversion(opt_l) in ⊢(% → ?); normalize nodelta [2: #lbl #_ * ] #EQopt_l destruct(EQopt_l)
2126   #_ #Hpre_t1 #Hpre_t2 whd in ⊢ (% → ?); * #EQcont11_42 >EQcode11 in ⊢ (% → ?);
2127   normalize nodelta #EQ destruct(EQ) whd in ⊢ (% → ?);
2128   #EQ destruct #IH1 #IH2 #st1' #abs_top #abs_tail
2129    whd in ⊢ (% → ?); inversion(check_continuations ????) [ #_ *] ** #H1
2130    #abs_top_cont #abs_tail_cont #EQcheck
2131    normalize nodelta **** #HH1 >EQcode11 in ⊢ (% → ?); inversion(code … st1')
2132    [1,2,3,4,5,7: (*assurdi da fare*) cases daemon] #f' #act_p' #opt_l' #i' #_
2133    #EQcode_st1' #EQclean #EQstore #EQio #EQ destruct(EQ)
2134    lapply(trans_env_ok … no_dup) >EQtrans normalize nodelta #H
2135    cases(H … EQenv_it) -H #env_it' * #fresh' ** #is_fresh_fresh' #EQenv_it' ***** #EQtrans
2136    #EQgen_labels #EQsignature #EQlab_env_it #same_map #same_keep
2137    change with (m_bind ?????) in EQclean : (??%?); inversion(call_post_clean ?????) in EQclean;
2138    [ #_ whd in ⊢ (??%% → ?); #EQ destruct] * #abs_top'' #i'' #EQi' >m_return_bind
2139    inversion opt_l' in EQcode_st1'; [| #lbl'] #EQopt_l' #EQcode_st1' normalize nodelta
2140    [ whd in ⊢ (??%% → ?); #EQ destruct(EQ) ] destruct(EQopt_l')
2141    inversion(memb ???) normalize nodelta #Hlbl_keep'
2142    [ cases(?==?) normalize nodelta ]
2143     whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct
2144     cases(IH1
2145            (mk_state ?
2146              (f_body … env_it')
2147              (〈ret_act (Some ? lbl'),i'〉 :: (cont … st1'))
2148              (store ? st12) false)
2149            (abs_top''@abs_tail_cont)
2150            (gen_labels … (call_post_trans … (f_body … env_it) fresh' (nil ?))))
2151     [2: whd >EQcont12
2152         change with (m_bind ??? (check_continuations ?????) ?) in match (check_continuations ?????);
2153          >EQcheck >m_return_bind normalize nodelta >EQi' normalize nodelta
2154          whd in match ret_costed_abs; normalize nodelta >Hlbl_keep' normalize nodelta
2155          % // % // % // % [/5 by conj/] >EQcode12 <EQtrans
2156          @(inverse_call_post_trans … fresh')
2157          [2: % |*: [2,3: /2 by / ]
2158              cases(lookup_ok_append … EQenv_it) #env1 * #env2 * #EQ1 #EQ2
2159              whd in no_dup; destruct >EQ1 in no_dup; >foldr_map_append >foldr_map_append
2160              #no_dup lapply(no_duplicates_append_r … no_dup) #H1
2161              lapply(no_duplicates_append_l … H1) whd in match (foldr ?????); -H1
2162              change with ([?]@?) in match (?::?); #H1
2163              lapply(no_duplicates_append_r … H1) >append_nil //
2164          ]
2165     ]
2166     #abs_top''' * #abs_tail''' * #st41' * #t1' *** #Hst41st41' #EQcosts #EQlen #stack_safety1
2167     whd in Hst41st41'; inversion(check_continuations …) in Hst41st41'; [ #_ * ]
2168     ** #H2 #abs_top_cont' #abs_tail_cont' >EQcont41 in ⊢ (% → ?);
2169     whd in ⊢ (??%? → ?); inversion(cont … st41') normalize nodelta
2170     [ #_ #EQ destruct(EQ) ] * #act_lbl #i'' #cont_st42' #_ #EQcont_st41'
2171     change with (check_continuations ?????) in match (foldr2 ???????);
2172     inversion(check_continuations ?????) [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
2173     ** #H3 #abs_top_cont'' #abs_tail_cont'' #EQ_contst42 >m_return_bind
2174     normalize nodelta inversion(call_post_clean ?????) normalize nodelta
2175     [ #_ whd in ⊢ (??%% → ?); #EQ destruct ***** ] * #abs_top'''' #i'''
2176     #EQ_clean_i'' inversion(act_lbl) normalize nodelta [1,3,4: cases daemon (*assurdi*)]
2177     cut(act_lbl = ret_act (Some ? lbl') ∧ cont_st42' = cont … st1' ∧ i'' = i')
2178     [ cases(stack_safety1 [ ] …)
2179       [3: >EQcont41 in ⊢ (??%?); % |4: normalize // |5: % |2:] * [|#x #xs] *
2180       whd in ⊢ (??%% → ??%% → ?); #EQ1 #EQ2 destruct > EQcont_st41' in EQ1;
2181       #EQ destruct(EQ) /3 by conj/
2182     ]
2183     ** #EQ1 #EQ2 #EQ3 destruct #x #EQ destruct whd in match ret_costed_abs; normalize nodelta
2184     >Hlbl_keep' normalize nodelta
2185     whd in ⊢ (??%% → ?); #EQ destruct ****** #_ #HH3 #EQ destruct(EQ)
2186     >EQcode41 in ⊢ (% → ?); inversion(code … st41')
2187     [1,3,4,5,6,7: cases daemon (*ASSURDI*) ] #r_t' #EQcode_st41' whd in ⊢ (??%% → ?);
2188     #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ) #EQstore_st41' #EQinfo_st41'
2189     #EQ destruct(EQ) >EQcont11_42 in EQcheck; >EQ_contst42 in ⊢ (% → ?);
2190     #EQ destruct(EQ) >EQi' in EQ_clean_i''; #EQ destruct(EQ)
2191     cases(IH2
2192             (mk_state ? i' (cont … st1') (store … st42) (io_info … st42))
2193             abs_tail_cont abs_top'''')
2194     [2: whd >EQ_contst42 normalize nodelta % // % // % // % // @EQi' ]
2195     #abs_top_1 * #abs_tail_1 * #st5' * #t2' *** #Hst5_st5' #EQcosts'
2196     #EQlen'  #stack_safety2 %{abs_top_1} %{abs_tail_1} %{st5'} %{(t_ind … (t1' @ (t_ind … t2')))}
2197     [3: @hide_prf @(call …  EQcode_st1' …  EQenv_it') //
2198     |1: @hide_prf @(ret_instr … EQcode_st41' … EQcont_st41') //
2199     |2,4: skip
2200     ]
2201     %
2202     [ % [2: whd in ⊢ (??%%); @eq_f >len_append >len_append @eq_f2 //
2203           whd in ⊢ (??%%); @eq_f // ]
2204       %{Hst5_st5'} whd in match (map_labels_on_trace ??);
2205       change with (map_labels_on_trace ??) in match (foldr ?????);
2206       >map_labels_on_trace_append >get_cost_label_append
2207       whd in match (get_costlabels_of_trace ??? (t_ind …));
2208       whd in match (get_costlabels_of_trace ??? (t_ind …));
2209       >get_cost_label_append
2210       whd in match (get_costlabels_of_trace ??? (t_ind …));
2211       >map_labels_on_trace_append >EQlab_env_it >EQgen_labels
2212       whd in match (map_labels_on_trace ? [ ]);
2213       whd in match (append ? (nil ?) ?);
2214       cut (∀A.∀l: list A. [ ] @ l = l) [//] #nil_append
2215       >nil_append >nil_append >nil_append @(permute_ok … EQcosts EQcosts')
2216     | #k #i #EQcont_st3 #EQ #H
2217       cases(stack_safety2 … EQcont_st3 … EQ H) #k1 * #EQk1 #EQlength %{k1} % //
2218     ]
2219]
2220qed.
Note: See TracBrowser for help on using the repository browser.