source: LTS/Language.ma @ 3549

Last change on this file since 3549 was 3549, checked in by piccolo, 5 years ago

added paolo's trick

File size: 66.3 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".
20include "utils.ma".
21
22discriminator option.
23
24record instr_params : Type[1] ≝
25{ seq_instr : DeqSet
26; io_instr : DeqSet
27; cond_instr : DeqSet
28; loop_instr : DeqSet
29; act_params_type : DeqSet
30; return_type : DeqSet
31}.
32
33
34inductive Instructions (p : instr_params)
35  (l_p :label_params) : Type[0] ≝
36 | EMPTY : Instructions p l_p
37 | RETURN : return_type p → Instructions p l_p
38 | SEQ : (seq_instr p) → option (NonFunctionalLabel l_p) → Instructions p l_p → Instructions p l_p
39 | COND : (cond_instr p) → (NonFunctionalLabel l_p) → Instructions p l_p →
40                 (NonFunctionalLabel l_p) → Instructions p l_p → Instructions p l_p →
41                       Instructions p l_p
42 | LOOP : (loop_instr p) → NonFunctionalLabel l_p → Instructions p l_p →
43                  NonFunctionalLabel l_p → Instructions p l_p → Instructions p l_p
44 | CALL : FunctionName → (act_params_type p) → option (ReturnPostCostLabel l_p) →
45            Instructions p l_p → Instructions p l_p
46 | IO : NonFunctionalLabel l_p → (io_instr p) → NonFunctionalLabel l_p → Instructions p l_p →
47             Instructions p l_p.
48
49let rec eq_instructions (p : instr_params) (l_p : label_params) (i : Instructions p l_p)
50 on i : (Instructions p l_p) → bool ≝
51match i with
52[ EMPTY ⇒ λi'.match i' with [ EMPTY ⇒ true | _ ⇒ false ]
53| RETURN x ⇒ λi'.match i' with [ RETURN y ⇒ x == y | _ ⇒ false ]
54| SEQ x lab instr ⇒ λi'.match i' with
55                      [ SEQ y lab' instr' ⇒ x == y ∧ eq_instructions … instr instr' ∧
56                              match lab with [ None ⇒ match lab' with [ None ⇒ true | _ ⇒ false ]
57                                             | Some l1 ⇒ match lab' with [Some l2 ⇒ eq_nf_label … l1 l2 | _ ⇒ false]
58                                             ]
59                      | _ ⇒ false
60                      ]
61| COND x ltrue i1 lfalse i2 i3 ⇒ λi'.match i' with
62                          [ COND y ltrue' i1' lfalse' i2' i3' ⇒
63                             x == y ∧ eq_nf_label … ltrue ltrue' ∧
64                             eq_instructions … i1 i1' ∧ eq_nf_label … lfalse lfalse' ∧
65                             eq_instructions … i2 i2' ∧ eq_instructions … i3 i3'
66                         | _ ⇒ false
67                         ]
68| LOOP x ltrue i1 lfalse i2 ⇒ λi'.match i' with
69              [ LOOP y ltrue' i1' lfalse' i2' ⇒ x == y ∧
70                      eq_instructions … i1 i1' ∧ eq_nf_label … ltrue ltrue' ∧
71                      eq_instructions … i2 i2'
72              | _ ⇒ false
73              ]
74| CALL f act_p r_lb i1 ⇒ λi'.match i' with
75             [ CALL f' act_p' r_lb' i1' ⇒ eq_function_name f f' ∧
76                       act_p == act_p' ∧ eq_instructions … i1 i1' ∧
77                       match r_lb with [ None ⇒ match r_lb' with [None ⇒ true | _ ⇒ false]
78                                       | Some z ⇒ match r_lb' with [Some w ⇒  eq_return_cost_lab … z w | _ ⇒ false ]
79                                       ]
80            | _ ⇒ false
81            ]
82| IO lin io lout i1 ⇒ λi'.match i' with
83             [ IO lin' io' lout' i1' ⇒ eq_nf_label … lin lin' ∧ io == io' ∧
84                                       eq_nf_label … lout lout' ∧ eq_instructions … i1 i1'
85             | _ ⇒ false                                       
86             ]
87].
88
89
90(*
91lemma eq_instructions_elim : ∀ P : bool → Prop.∀p,i1,i2.(i1 = i2 → P true) →
92(i1 ≠ i2 → P false) → P (eq_instructions p i1 i2).
93#P #p #i1 elim i1
94[* normalize [/2/ ] #x [|*: #y #z [2,3: #w1 #w2 [#w3] |4,5: #w1]] #_ #H2 @H2 % #EQ
95  lapply (eq_to_jmeq ??? EQ) #EQ' destruct(EQ')
96| #rt * normalize [2: #rt' cases (dec_eq … rt rt') #H [>(\b H) | >(\bf H) ] /2/
97  #_ #K @K % #abs lapply (eq_to_jmeq ??? abs) #abs' destruct(abs') @(absurd ?? H) //]
98  [|*: #x #y #z [2,3: #w1 #w2 [#w3] |4,5: #w1]] #_ #H2 @H2 % #EQ
99  lapply (eq_to_jmeq ??? EQ) #EQ' destruct(EQ')
100| #seq * [| #lbl] #i #IH * normalize
101  [1,8: |2,9: #t_t |3,10: #seq1 #opt_l #i2 |4,11: #cond #ltrue #i_t #lfalse #i_f #i_c
102  |5,12: #cond #ltrue #i_t #lfalse #i_f |6,13: #f #act_p #ret #i3 |*: #lin #io #lout #i3]
103  #H1 #H2 try(@H2 % #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct)
104  inversion(?==?) normalize nodelta
105  [2,4: #ABS @H2 % #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct
106        >(\b (refl …)) in ABS; #EQ destruct]
107  #EQseq inversion(eq_instructions ???) normalize nodelta #Heq_i2
108  [2,4: @H2 % #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct
109*)
110
111record env_params : Type[1] ≝
112{ form_params_type : Type[0]
113}.
114
115record signature (p : env_params) (p' : instr_params) : Type[0] ≝
116{ f_name : FunctionName
117; f_pars : form_params_type p
118; f_ret : return_type p'
119}.
120
121record env_item (p : env_params) (p' : instr_params) (l_p : label_params) : Type[0] ≝
122{ f_sig :> signature p p'
123; f_lab : CallCostLabel l_p
124; f_body : Instructions p' l_p
125}.
126
127record state_params : Type[1] ≝
128{ i_pars :> instr_params
129; e_pars :> env_params
130; l_pars :> label_params
131; store_type : DeqSet
132}.
133
134record state (p : state_params) : Type[0] ≝
135{ code : Instructions p p
136; cont : list (ActionLabel p × (Instructions p p))
137; store : store_type p
138; io_info : bool
139}.
140
141definition is_io : ∀p.state p → Prop ≝ λp,st.io_info … st = true.
142
143record sem_state_params (p : state_params) : Type[0] ≝
144{ eval_seq : seq_instr p → (store_type p) → option (store_type p)
145; eval_io : io_instr p → store_type p → option (store_type p)
146; eval_cond_cond : cond_instr p → store_type p → option (bool × (store_type p))
147; eval_loop_cond : loop_instr p → store_type p → option (bool × (store_type p))
148; eval_call : signature p p → act_params_type p → store_type p → option (store_type p)
149; eval_after_return : return_type p → store_type p → option (store_type p)
150; init_store : store_type p
151}.
152
153
154let rec lookup (p : env_params) (p' : instr_params) (l_p : label_params) (l : list (env_item p p' l_p))
155 on l : FunctionName → option (env_item p p' l_p) ≝
156match l with
157[ nil ⇒ λ_.None ?
158| cons x xs ⇒  λf.if (eq_function_name f (f_name … x))
159                  then Some ? x
160                  else lookup … xs f
161].
162
163inductive execute_l (p : state_params) (p' : sem_state_params p) (env : list (env_item p p p)) :
164                                         ActionLabel p → relation (state p) ≝
165| empty : ∀st,st',hd,tl.(code ? st) = (EMPTY p p)→ (cont ? st) = hd :: tl →
166           (code ? st') = \snd hd → (cont … st') = tl → (store … st) = (store … st') →
167           (io_info … st = true → is_non_silent_cost_act … (\fst hd)) →  (io_info … st') = false → ¬ is_ret_call_act … (\fst hd) →  execute_l … (\fst hd) st st'
168| seq_sil : ∀st,st',i,cd,s,opt_l.(code ? st) = SEQ … i opt_l cd →
169             eval_seq … p' i (store … st) = return s → (code ? st') = cd →
170             (cont … st) = (cont … st') → (store … st') = s →
171             io_info … st = false →  io_info ? st' = false → execute_l … (cost_act … opt_l) st st'
172| cond_true : ∀st,st',exp,ltrue,i_true,lfalse,i_false,cd,new_m.
173   (code ? st) = COND … exp ltrue i_true lfalse i_false cd → eval_cond_cond … p' exp (store … st) = return 〈true,new_m〉 →
174   cont ? st' = 〈cost_act … (None ?),cd〉 ::(cont … st) → code … st' = i_true → store … st' = new_m →
175   io_info … st = false →  io_info … st' = false → execute_l … (cost_act … (Some ? ltrue)) st st'
176| cond_false : ∀st,st',exp,ltrue,i_true,lfalse,i_false,cd,new_m.
177   (code ? st) = COND … exp ltrue i_true lfalse i_false cd → eval_cond_cond … p' exp (store … st) = return 〈false,new_m〉 →
178   cont ? st' = 〈cost_act … (None ?),cd〉 ::(cont … st) → code … st' = i_false → store … st' = new_m →
179   io_info … st = false →  io_info … st' = false → execute_l … (cost_act … (Some ? lfalse)) st st'
180| loop_true : ∀st,st',exp,ltrue,i_true,lfalse,i_false,new_m.
181   code ? st = LOOP … exp ltrue i_true lfalse i_false → eval_loop_cond … p' exp (store … st) = return 〈true,new_m〉 →
182   cont ? st' = 〈cost_act … (None ?),LOOP … exp ltrue i_true lfalse i_false〉 :: (cont … st) →
183   code … st' = i_true → store … st' = new_m → io_info … st = false →  io_info … st' = false →
184   execute_l … (cost_act … (Some ? ltrue)) st st'
185| loop_false : ∀st,st',exp,ltrue,i_true,lfalse,i_false,new_m.
186   code ? st = LOOP … exp ltrue i_true lfalse i_false → eval_loop_cond … p' exp (store … st) = return 〈false,new_m〉 →
187   cont ? st' = cont … st → code … st' = i_false → store … st' = new_m →
188   io_info … st = false →  io_info … st' = false → execute_l … (cost_act … (Some ? lfalse)) st st'
189| io_in : ∀st,st',lin,io,lout,cd,mem.(code ? st) = IO … lin io lout cd →
190    eval_io … p' io (store … st) = return mem → code ? st' = EMPTY p p →
191    cont … st' = 〈cost_act … (Some ? lout),cd〉 :: (cont … st) → store … st' = mem →
192    io_info … st' = true → execute_l … (cost_act … (Some ? lin)) st st'
193| call : ∀st,st',f,act_p,r_lb,cd,mem,env_it.(code ? st) = CALL … f act_p r_lb cd →
194    lookup … env f = return env_it →
195    eval_call ? p' env_it act_p (store … st) = return mem →
196    store ? st' = mem → code … st' = f_body … env_it →
197     cont … st' =
198       〈(ret_act … r_lb),cd〉 :: (cont … st) → 
199    io_info … st = false →  (io_info … st') = false →
200    execute_l … (call_act … f (f_lab … env_it)) st st'
201| ret_instr : ∀st,st',r_t,mem,tl',rb,cd.code ? st = RETURN … r_t →
202   cont … st = 〈ret_act … rb,cd〉 :: tl' → cont ? st' = tl' →
203   io_info … st = false →  io_info ? st' = false →
204   eval_after_return … p' r_t (store … st) = return mem → code … st' = cd →
205   store … st' = mem → execute_l … (ret_act … rb) st st'.
206   
207let rec get_labels_of_code (p : instr_params) (l_p : label_params) (i : Instructions p l_p) on i : list (CostLabel l_p) ≝
208match i with
209[ EMPTY ⇒ [ ]
210| RETURN x ⇒ [ ]
211| SEQ x lab instr ⇒ let ih ≝ get_labels_of_code … instr in
212  match lab with [ None ⇒ ih | Some lbl ⇒ a_non_functional_label … lbl :: ih ]
213| COND x ltrue i1 lfalse i2 i3 ⇒
214   let ih3 ≝ get_labels_of_code … i3 in
215   let ih2 ≝ get_labels_of_code … i2 in
216   let ih1 ≝ get_labels_of_code … i1 in
217   ltrue :: lfalse :: (ih1 @ ih2 @ih3)
218| LOOP x ltrue i1 lfalse i2 ⇒
219   let ih2 ≝ get_labels_of_code … i2 in
220   let ih1 ≝ get_labels_of_code … i1 in
221   a_non_functional_label … ltrue :: a_non_functional_label … lfalse :: (ih1 @ ih2)
222| CALL f act_p r_lb i1 ⇒
223   let ih1 ≝ get_labels_of_code … i1 in
224   match r_lb with [ None ⇒ ih1 | Some lbl ⇒ a_return_post … lbl :: ih1]
225| IO lin io lout i1 ⇒
226   let ih1 ≝ get_labels_of_code … i1 in
227   a_non_functional_label … lin :: a_non_functional_label … lout :: ih1
228].
229   
230record Program (p : env_params) (p' : instr_params) (l_p : label_params) : Type[0] ≝
231{ env : list (env_item p p' l_p)
232; main : Instructions p' l_p
233}.
234
235
236
237definition no_duplicates_labels : ∀p,p',l_p.Program p p' l_p → Prop ≝
238λp,p',l_p,prog.
239   no_duplicates …
240    (foldr …
241      (λitem,acc.((a_call … (f_lab … item)) :: get_labels_of_code … (f_body … item)) @ acc)
242      (get_labels_of_code … (main … prog)) (env … prog)).
243
244lemma no_duplicates_domain_of_fun:
245 ∀p,p',l_p,prog.no_duplicates_labels … prog →
246 ∀f,env_it.lookup p p' l_p (env … prog) f = return env_it →
247 no_duplicates … (get_labels_of_code … (f_body … env_it)).
248#p #p' #l_p * #env elim env [ #main normalize #_ #f #env_it #EQ destruct(EQ)]
249#x #xs #IH #main whd in ⊢ (% → ?); whd in match (foldr ?????); #H #f #env_it
250whd in ⊢ (??%? → ?); @eq_function_name_elim normalize nodelta
251[ whd in ⊢ (? → ???% → ?); #EQ1 #EQ2 destruct(EQ1 EQ2) cases H #_ /2/ ]
252#H1 #EQenv_it @IH cases H /2/
253qed.
254
255
256definition is_synt_succ : ∀p.relation (state p) ≝ λp,s1,s2.cont … s1 = cont … s2 ∧
257 match (code … s1) with
258 [ CALL f act_p r_lb i1 ⇒ code … s2 = i1
259 | _ ⇒ False
260 ].
261
262definition operational_semantics : ∀p : state_params.∀p'.Program p p p → abstract_status p ≝
263λp,p',prog.mk_abstract_status …
264                (state p)
265                (execute_l ? p' (env … prog))
266                (is_synt_succ …)
267                (λs.match (code … s) with
268                    [ COND _ _ _ _ _ _ ⇒ cl_jump
269                    | LOOP _ _ _ _ _ ⇒ cl_jump
270                    | EMPTY ⇒ if io_info … s then cl_io else cl_other
271                    | _ ⇒ cl_other
272                    ])
273                (λs.match (code … s) with
274                    [CALL _ _ m _ ⇒ match m with [ Some _ ⇒ true | None ⇒ false ]
275                    | _ ⇒ false
276                    ])
277                (λs.eq_instructions … (code … s) (main … prog) ∧ isnilb … (cont … s) ∧ store … s == init_store … p' ∧ io_info … s)
278                (λs.match (cont … s) with
279                    [ nil ⇒ match (code … s) with
280                            [ EMPTY ⇒ true
281                            | RETURN _ ⇒ true
282                            | _ ⇒ false
283                            ]
284                    | _ ⇒ false
285                    ])
286                ???.
287@hide_prf
288[ #s1 #s2 #l #H #H1 inversion H1 #st #st'
289 [ #hd #tl
290 | #i #cd #s #opt_l
291 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
292 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
293 | #lin #io #lout #cd #mem
294 | #f #act_p #r_lb #cd #mem #env_it
295 | #r_t #mem #tl #rb #cd
296 ]
297 #EQcode
298 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQ5 #H2 #EQ' #EQ6 #EQ7
299 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
300 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
301 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
302 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
303 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
304 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8
305 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
306 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
307 ]
308 #_ destruct >EQcode in H; normalize nodelta /2 by ex_intro/
309 [ cases(io_info ??) normalize nodelta] #EQ destruct
310| #s1 #s2 #l #H #H1 inversion H1 #st #st'
311 [ #hd #tl
312 | #i #cd #s #opt_l
313 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
314 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
315 | #lin #io #lout #cd #mem
316 | #f #act_p #r_lb #cd #mem #env_it
317 | #r_t #mem #tl #rb #cd
318 ]
319 #EQcode
320 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQiost' #H2 #EQ' #EQ6 #EQ7
321 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
322 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
323 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
324 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
325 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
326 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost' #EQ6 #EQ7 #EQ8
327 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQiost' #EQ8 #EQ9 #EQ10
328 | #EQ1 #EQ2 #EQ3 #EQiost' #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
329 ]
330 #_ destruct
331 cases(code ? st') in H; normalize nodelta >EQiost' normalize nodelta
332 #eq destruct try (#eq1 destruct) try (#eq2 destruct) try (#eq3 destruct)
333 try (#eq4 destruct) try (#eq5 destruct) try (#eq6 destruct) %{lin} %
334| #s1 #s2 #l #H #H1 inversion H1 #st #st'
335 [ #hd #tl
336 | #i #cd #s #opt_l
337 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
338 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
339 | #lin #io #lout #cd #mem
340 | #f #act_p #r_lb #cd #mem #env_it
341 | #r_t #mem #tl #rb #cd
342 ]
343 #EQcode
344 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQiost' #H2 #EQ' #EQ6 #EQ7
345 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
346 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
347 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
348 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
349 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
350 | #EQ1 #EQ2 #EQ3 #EQiost #EQiost' #EQ6 #EQ7 #EQ8
351 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost #EQiost' #EQ8 #EQ9 #EQ10
352 | #EQ1 #EQ2 #EQiost #EQiost' #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
353 ]
354 #_ destruct >EQcode in H; normalize nodelta [|*: #EQ destruct]
355 cases(io_info … st) in x; normalize nodelta [2: #_ #EQ destruct]
356 #H3 #_ @H3 %
357]
358qed.
359
360record call_post_info (p : instr_params) (l_p : label_params) : Type[0] ≝
361{ gen_labels : list (CostLabel l_p)
362; t_code : Instructions p l_p
363; fresh : l_p
364; lab_map : associative_list (DEQCostLabel l_p) (CostLabel l_p)
365; lab_to_keep : list (ReturnPostCostLabel l_p)
366}.
367
368let rec call_post_trans (p : instr_params) (l_p : label_params) (i : Instructions p l_p) (n : l_p) on i :
369list (CostLabel l_p) → call_post_info p l_p ≝
370λabs.
371match i with
372[ EMPTY ⇒ mk_call_post_info … abs (EMPTY …) n (nil ?) (nil ?)
373| RETURN x ⇒ mk_call_post_info … abs (RETURN … x) n (nil ?) (nil ?)
374| SEQ x lab instr ⇒
375   let ih ≝ call_post_trans … instr n abs in
376   match lab with
377   [ None ⇒ mk_call_post_info … (gen_labels ?? ih) (SEQ … x (None ?) (t_code … ih))
378             (fresh … ih) (lab_map … ih) (lab_to_keep … ih)
379   | Some lbl ⇒
380      mk_call_post_info … (nil ?) (SEQ … x (Some ? lbl) (t_code …  ih)) (fresh … ih)
381      (〈a_non_functional_label … lbl,((a_non_functional_label … lbl) :: (gen_labels … ih))〉 :: (lab_map … ih))
382      (lab_to_keep … ih)
383   ]
384| COND x ltrue i1 lfalse i2 i3 ⇒
385   let ih3 ≝ call_post_trans … i3 n abs in
386   let ih2 ≝ call_post_trans … i2 (fresh … ih3) (gen_labels … ih3) in
387   let ih1 ≝ call_post_trans … i1 (fresh … ih2) (gen_labels … ih3) in
388   mk_call_post_info p l_p (nil ?) (COND … x ltrue (t_code … ih1) lfalse (t_code … ih2) (t_code … ih3))
389    (fresh … ih1) 
390    (〈a_non_functional_label … ltrue,(a_non_functional_label … ltrue :: (gen_labels … ih1))〉::
391      〈a_non_functional_label … lfalse,(a_non_functional_label … lfalse :: (gen_labels … ih2))〉::
392       ((lab_map … ih1) @ (lab_map …  ih2) @ (lab_map … ih3)))
393    ((lab_to_keep … ih1) @ (lab_to_keep … ih2) @ (lab_to_keep … ih3))
394| LOOP x ltrue i1 lfalse i2 ⇒
395   let ih2 ≝ call_post_trans … i2 n abs in
396   let ih1 ≝ call_post_trans … i1 (fresh … ih2) (nil ?) in
397   mk_call_post_info p l_p (nil ?) (LOOP … x ltrue (t_code … ih1) lfalse (t_code … ih2)) (fresh … ih1)
398    (〈a_non_functional_label … lfalse,(a_non_functional_label … lfalse :: (gen_labels … ih2))〉 ::
399     〈a_non_functional_label … ltrue,(a_non_functional_label … ltrue :: (gen_labels … ih1))〉 ::
400      ((lab_map … ih1) @ (lab_map … ih2)))
401    ((lab_to_keep … ih1) @ (lab_to_keep … ih2))
402| CALL f act_p r_lb i1 ⇒
403   let ih ≝ call_post_trans … i1 n abs in
404   match r_lb with
405   [ None ⇒ let 〈l',f''〉 ≝ fresh_rc_label l_p (fresh … ih) in
406       mk_call_post_info p l_p ((a_return_post … l')::(gen_labels … ih))
407         (CALL … f act_p (Some ? l') (t_code … ih))  f'' (lab_map … ih) (lab_to_keep … ih)
408   | Some lbl ⇒
409      mk_call_post_info p l_p (nil ?) (CALL … f act_p (Some ? lbl) (t_code … ih)) (fresh … ih)
410       (〈a_return_post … lbl,(a_return_post … lbl :: (gen_labels … ih))〉 :: (lab_map … ih))
411       (lbl :: lab_to_keep … ih)
412   ]
413| IO lin io lout i1 ⇒
414    let ih ≝ call_post_trans … i1 n abs in
415    mk_call_post_info p l_p (nil ?) (IO … lin io lout (t_code … ih)) (fresh … ih)
416     (〈a_non_functional_label … lout,(a_non_functional_label … lout :: (gen_labels … ih))〉 ::
417      〈a_non_functional_label … lin,[a_non_functional_label … lin]〉 :: (lab_map … ih)) (lab_to_keep … ih)
418].
419
420
421let rec call_post_clean (p : instr_params) (l_p : label_params) (i : Instructions p l_p) on i :
422associative_list (DEQCostLabel l_p) (CostLabel l_p) → list (ReturnPostCostLabel l_p) → list (CostLabel l_p) →
423option ((list (CostLabel l_p)) × (Instructions p l_p)) ≝
424λm,keep,abs.
425 match i with
426[ EMPTY ⇒ Some ? 〈abs,EMPTY …〉
427| RETURN x ⇒ Some ? 〈abs,RETURN … x〉
428| SEQ x lab instr ⇒
429   ! 〈l,i1〉 ← call_post_clean … instr m keep abs;
430   match lab with
431   [ None ⇒ return 〈l,SEQ … x (None ?) i1〉
432   | Some lbl ⇒ if ((get_element … m lbl) == lbl :: l)
433                then return 〈nil ?,SEQ … x (Some ? lbl) i1〉
434                else None ?
435   ]
436| COND x ltrue i1 lfalse i2 i3 ⇒
437    ! 〈l3,instr3〉 ← call_post_clean … i3 m keep abs;
438    ! 〈l2,instr2〉 ← call_post_clean … i2 m keep l3;
439    ! 〈l1,instr1〉 ← call_post_clean … i1 m keep l3;
440    if ((get_element … m ltrue) == ltrue :: l1) ∧
441       ((get_element … m lfalse) == lfalse :: l2)
442    then return 〈nil ?,COND … x ltrue instr1 lfalse instr2 instr3〉
443    else None ?
444| LOOP x ltrue i1 lfalse i2 ⇒
445   ! 〈l2,instr2〉 ← call_post_clean … i2 m keep abs;
446   ! 〈l1,instr1〉 ← call_post_clean … i1 m keep (nil ?);
447   if ((get_element … m ltrue) == ltrue :: l1) ∧
448      ((get_element … m lfalse) == lfalse :: l2)
449   then return 〈nil ?,LOOP … x ltrue instr1 lfalse instr2〉
450   else None ?
451| CALL f act_p r_lb i1 ⇒
452  ! 〈l1,instr1〉 ← call_post_clean … i1 m keep abs;
453  match r_lb with
454  [ None ⇒ None ?
455  | Some lbl ⇒ if (lbl ∈ keep)
456               then if ((get_element … m lbl) == lbl :: l1)
457                    then return 〈nil ?,CALL … f act_p (Some ? lbl) instr1〉
458                    else None ?
459               else return 〈(a_return_post l_p lbl) :: l1,CALL … f act_p (None ?) instr1〉
460  ]
461| IO lin io lout i1 ⇒
462   ! 〈l1,instr1〉 ← call_post_clean … i1 m keep abs;
463   if ((get_element … m lout) == lout :: l1) ∧ ((get_element … m lin) == [lin])
464   then return 〈nil ?,IO … lin io lout instr1〉
465   else None ?   
466].
467
468
469definition ret_costed_abs : ∀p.list (ReturnPostCostLabel p) → option (ReturnPostCostLabel p) →
470option (CostLabel p) ≝
471λp,keep,x.
472 match x with
473              [ Some lbl ⇒ if lbl ∈ keep then return (a_return_post … lbl)
474                           else None ?
475              | None ⇒ None ?
476              ].
477
478
479definition check_continuations : ∀p : instr_params.∀l_p : label_params.
480∀l1,l2 : list ((ActionLabel l_p) × (Instructions p l_p)).
481associative_list (DEQCostLabel l_p) (CostLabel l_p) →
482list (ReturnPostCostLabel l_p) →  option (Prop × (list (CostLabel l_p)) × (list (CostLabel l_p))) ≝
483λp,l_p,cont1,cont2,m,keep.
484foldr2 ??? 〈True,nil ?,nil ?〉 cont1 cont2
485 (λx,y,z.
486   let 〈cond,abs_top',abs_tail'〉 ≝ x in
487   match call_post_clean p l_p (\snd z) m keep abs_top' with
488   [ None ⇒ 〈False,nil ?,nil ?〉
489   | Some w ⇒
490      match \fst z with
491       [ ret_act opt_x ⇒
492           match ret_costed_abs … keep opt_x with
493           [ Some lbl ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
494                               get_element … m lbl = lbl :: (\fst w),(nil ?),abs_tail'〉
495           | None ⇒
496              〈\fst y = ret_act … (None ?) ∧ cond ∧ \snd w = \snd y,(nil ?),(\fst w) @ abs_tail'〉
497           ]
498       | cost_act opt_x ⇒
499           match opt_x with
500           [ None ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y,\fst w,abs_tail'〉
501           | Some xx ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
502                               get_element … m xx = xx :: (\fst w),(nil ?),abs_tail'〉]
503       | _ ⇒ (* dummy *) 〈False,nil ?,nil ?〉]]).
504
505(* in input :
506     abs_top is the list of labels to be propageted to the deepest level of the call stack
507             equivalently (?) is the list of labels I need to pay now
508
509     abs_tail are the lists of labels to be propagated to the levels "below" the deepest level
510              equivalently (?) is the list of labels I must have already payid in the
511              code already executed; generated by the continuations below me in the stack
512   in output :
513     abs_top is the list of labels to be propageted from the current level of the call stack
514             non empty only in the case of non-call stack frames (whiles, ifs, etc; but in
515             practice it is always nil!)
516     abs_tail are the lists of labels to be propagated from the levels "below" the current level
517             or equivalently (?) the list of labels I must have already paied in the code
518             already executed; generated by this level of the stack
519*)       
520       
521
522definition state_rel : ∀p : state_params.
523associative_list (DEQCostLabel p) (CostLabel p) → list (ReturnPostCostLabel p) →
524list (CostLabel p) → list (CostLabel p) →
525relation (state p) ≝ λp,m,keep,abs_top,abs_tail,st1,st2.
526match check_continuations … (cont ? st1) (cont … st2) m keep with
527[ Some x ⇒ let 〈prf1,abs_top',abs_tail'〉 ≝ x in
528           prf1 ∧ call_post_clean … (code … st2) m keep abs_top' = return 〈abs_top,(code … st1)〉
529           ∧ store … st1 = store … st2 ∧ io_info … st1 = io_info … st2 ∧ abs_tail = abs_tail'
530| None ⇒ False
531].
532
533let rec compute_max_n (p : instr_params) (l_p : label_params) (i : Instructions p l_p) on i : l_p ≝
534match i with
535[ EMPTY ⇒ e … l_p
536| RETURN x ⇒ e … l_p
537| SEQ x lab instr ⇒ let n ≝ compute_max_n … instr in
538                    match lab with
539                    [ None ⇒ n
540                    | Some l ⇒
541                        match l with
542                        [ a_non_functional_label n' ⇒ op … l_p n n' ]
543                    ]
544| COND x ltrue i1 lfalse i2 i3 ⇒
545  let n1 ≝ compute_max_n … i1 in
546  let n2 ≝ compute_max_n … i2 in
547  let n3 ≝ compute_max_n … i3 in
548  let mx ≝ op … l_p (op … l_p n1 n2) n3 in
549  match ltrue with
550  [ a_non_functional_label lt ⇒
551    match lfalse with
552    [a_non_functional_label lf ⇒  op … l_p (op … l_p mx lt) lf ] ]
553| LOOP x ltrue i1 lfalse i2 ⇒
554   let n1 ≝ compute_max_n … i1 in
555   let n2 ≝ compute_max_n … i2 in
556   let mx ≝ op … l_p n1 n2 in
557   match ltrue with
558  [ a_non_functional_label lt ⇒
559    match lfalse with
560    [a_non_functional_label lf ⇒ op … l_p (op … l_p mx lt) lf ] ]
561| CALL f act_p r_lb i1 ⇒
562   let n ≝ compute_max_n … i1 in
563   match r_lb with
564   [ None ⇒ n
565   | Some lbl ⇒ match lbl with [a_return_cost_label l ⇒ op … l_p l n ]
566   ]
567| IO lin io lout i1 ⇒
568  let n ≝ compute_max_n … i1 in
569  match lin with
570  [a_non_functional_label l1 ⇒
571    match lout with
572    [a_non_functional_label l2 ⇒ op … l_p (op … l_p n l1) l2 ] ]
573].
574
575
576definition same_fresh_map_on : ∀p.list (CostLabel p) →
577relation (associative_list (DEQCostLabel p) (CostLabel p)) ≝
578λp,dom,m1,m2.∀x.bool_to_Prop (x ∈ dom) → get_element … m1 x = get_element … m2 x.
579
580definition same_to_keep_on : ∀p. list (CostLabel p) → relation (list (ReturnPostCostLabel p)) ≝
581λp,dom,keep1,keep2.∀x. bool_to_Prop (a_return_post … x ∈ dom) → (x ∈ keep1) = (x ∈ keep2).
582
583
584lemma same_to_keep_on_append : ∀p.∀dom1,dom2,dom3 : list (CostLabel p).
585∀l1,l2,l3,l : list (ReturnPostCostLabel p).
586no_duplicates … (dom1@dom2@dom3) → (∀x.x ∈ l1 → a_return_post … x ∈ dom1) →
587(∀x.x ∈ l3 → a_return_post … x ∈ dom3) →
588same_to_keep_on … (dom1@dom2@dom3) (l1@l2@l3) l →
589same_to_keep_on … dom2 l2 l.
590#p #dom1 #dom2 #dom3 #l1 #l2 #l3 #l #no_dup #sub_set1 #sub_set3 #H2
591#x #Hx inversion (x ∈ l2)
592 [ #EQkeep <H2 [> memb_append_l2 // >memb_append_l1 // ]
593   >memb_append_l2 // >memb_append_l1 // >Hx //
594 | #EQno_keep <H2
595   [2: >memb_append_l2 // >memb_append_l1 // >Hx //
596   | @sym_eq @memb_not_append [2: @memb_not_append //]
597   [ <associative_append in no_dup; #no_dup ]
598   lapply(memb_no_duplicates_append … (a_return_post … x) … no_dup) #H
599   inversion(memb ???) // #H1 cases H
600   [1,4: [>memb_append_l2 | >memb_append_l1] // >Hx //
601   | @sub_set3 >H1 //
602   | @sub_set1 >H1 //
603   ]
604   ]
605 ]
606qed.
607
608lemma same_fresh_map_on_append : ∀p.∀dom1,dom2,dom3,l1,l2,l3,l.
609no_duplicates … (dom1 @dom2 @ dom3) → (∀x.x ∈ domain_of_associative_list … l1 → x ∈ dom1) →
610(∀x.x ∈ domain_of_associative_list … l3 → x ∈ dom3) →
611same_fresh_map_on p … (dom1 @dom2 @dom3) (l1 @l2 @ l3) l →
612same_fresh_map_on p … dom2 l2 l.
613#p #dom1 #dom2 #dom3 #l1 #l2 #l3 #l #no_dup #subset1 #subset3 whd in ⊢ (% → ?); #H1
614whd #x #Hx <H1
615[2: >memb_append_l2 // >memb_append_l1 // >Hx //]
616>get_element_append_r [ >get_element_append_l1 // ] % #K
617[ lapply(subset3 … K) | lapply(subset1 … K) ] #ABS
618[ <associative_append in no_dup; #no_dup] @(memb_no_duplicates_append … x … no_dup)
619// [>memb_append_l2 | >memb_append_l1 ] // >Hx //
620qed.
621
622
623lemma lab_to_keep_in_domain : ∀p,l_p.∀i : Instructions p l_p.
624∀x,n,l.
625x ∈ lab_to_keep … (call_post_trans … i n l) → a_return_post … x ∈ get_labels_of_code …  i.
626#p #l_p #i elim i //
627[ #seq #opt_l #instr #IH #x #n #l whd in match (call_post_trans ????);
628  cases opt_l -opt_l normalize nodelta [|#lbl]
629  whd in match (get_labels_of_code ??); #H [2: @orb_Prop_r] /2/
630| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #x #n #l
631  whd in match (call_post_trans ????); whd in match (get_labels_of_code ??);
632  #H cases(memb_append … H) -H #H @orb_Prop_r @orb_Prop_r
633  [ >memb_append_l1 // @IH1 [3: >H // |*: ]
634  | >memb_append_l2 // cases(memb_append … H) -H #H
635     [>memb_append_l1 // @IH2 [3: >H // |*: ]
636     | >memb_append_l2 // @IH3 [3: >H // |*: ]
637     ]
638  ]
639| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #x #n #l
640  whd in match (call_post_trans ????); whd in match (get_labels_of_code ??);
641  #H cases(memb_append … H) -H #H @orb_Prop_r @orb_Prop_r
642  [ >memb_append_l1 | >memb_append_l2 ] // [ @IH1 | @IH2 ] [3,6: >H |*: ] //
643| #f #act_p * [|#lbl] #i1 #IH #x #n #l whd in match (call_post_trans ????);
644  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???);
645  inversion(x == lbl) #Hlbl normalize nodelta
646  [*  >(\P Hlbl) @orb_Prop_l @eq_costlabel_elim // * #H @H %
647  | #H @orb_Prop_r @IH //
648  ]
649| #lin #io #lout #i1 #IH #x #n #l whd in match (call_post_trans ????);
650  whd in match (get_labels_of_code ??); #H @orb_Prop_r @orb_Prop_r @IH //
651]
652qed.
653
654lemma lab_map_in_domain: ∀p,l_p.∀i: Instructions p l_p.
655 ∀x,n,l.
656  x ∈ domain_of_associative_list … (lab_map … (call_post_trans … i n l)) →
657   x ∈ get_labels_of_code … i.
658#p #l_p #i elim i //
659[ #seq * [|#lbl] #i1 #IH #x #n #l whd in match(call_post_trans ????);
660  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???);
661  inversion(x==lbl) #Hlbl normalize nodelta [#_ whd in match (memb ???); >Hlbl % ]
662  #H >memb_cons // @IH //
663| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #x #n #l
664  whd in match (call_post_trans ????); whd in match (memb ???);
665  whd in match (get_labels_of_code ??); inversion(x == ltrue) #Hlbl normalize nodelta
666  [ #_ whd in match (memb ???); >Hlbl % ] whd in match (memb ???);
667  inversion(x == lfalse) #Hlbl1 normalize nodelta
668  [ #_ whd in match (memb ???); >Hlbl normalize nodelta whd in match (memb ???);
669    >Hlbl1 % ] #H >memb_cons // >memb_cons // >domain_of_associative_list_append in H;
670    #H cases(memb_append … H) [ #H1 >memb_append_l1 // @IH1 [3: >H1 // |*:] ]
671    >domain_of_associative_list_append #H1 cases(memb_append … H1)
672    #H2 >memb_append_l2 // [ >memb_append_l1 | >memb_append_l2 ] //
673    [ @IH2 | @IH3] /2 by eq_true_to_b/
674| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #x #n #l whd in match (call_post_trans ????);
675  whd in match (get_labels_of_code ??); whd in match (memb ???); inversion(x == lfalse)
676  #Hlfalse normalize nodelta [ #_ >memb_cons // whd in match (memb ???); >Hlfalse // ]
677  whd in match (memb ???); inversion(x==ltrue) normalize nodelta #Hltrue
678  [ #_ whd in match (memb ???); >Hltrue %] >domain_of_associative_list_append #H
679  cases(memb_append … H) #H1 >memb_cons // >memb_cons // [ >memb_append_l1 | >memb_append_l2 ]
680  // [ @IH1 | @IH2] /2/
681| #f #act_p * [|#lbl] #i1 #IH #x #n #l whd in match (call_post_trans ????);
682  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???); inversion (x == lbl)
683  #Hlbl normalize nodelta [ #_ whd in match (memb ???); >Hlbl % ] #H >memb_cons // @IH /2/
684| #lin #io #lout #i1 #IH #x #n #l whd in match (memb ???); inversion(x == lout) #Hlout
685  normalize nodelta [ #_ >memb_cons // whd in match (memb ???); >Hlout % ]
686  whd in match (memb ???); inversion(x==lin) #Hlin normalize nodelta
687  [ #_ whd in match (memb ???); >Hlin % ] #H >memb_cons // >memb_cons // @IH /2/
688]
689qed.
690
691let rec is_fresh_for_return (p : label_params) (keep : list (CostLabel p)) (n : p) on keep : Prop ≝
692match keep with
693[ nil ⇒ True
694| cons x xs ⇒ let ih ≝ is_fresh_for_return … xs n in
695              match x with
696              [ a_return_post y ⇒ match y with [a_return_cost_label m ⇒ m ⊑^{p} n ∧ ih ]
697              | _ ⇒ ih
698              ]
699].
700
701lemma fresh_ok_call_post_trans : ∀p : instr_params.∀l_p.∀i1 : Instructions p l_p.∀n : l_p.∀l.
702n ⊑^{l_p} fresh … (call_post_trans … i1 n l).
703#p #l_p #i1 elim i1 normalize /2 by trans_po_rel, refl_po_rel/
704[ #seq * [|#lbl] #i2 #IH #n #l normalize /2 by /
705| #cond #ltrue #i_true #lfalse #i_false #i2 #IH1 #IH2 #IH3 #n #l
706  @(trans_po_rel … (IH3 …)) [2: @(trans_po_rel … (IH2 …)) [2: @(trans_po_rel … (IH1 …)) ]] //
707| #f #act_p * [|#lbl] normalize #i2 #IH /2 by / #n #l @(trans_po_rel … (IH … l …)) @lab_po_rel_succ
708]
709qed.
710
711lemma fresh_keep_n_ok : ∀p : label_params.∀n,m.∀l.
712is_fresh_for_return p l n → n ⊑^{p} m → is_fresh_for_return p l m.
713#p #n #m #l lapply n -n lapply m -m elim l // *
714[1,2,3: * #x] #xs #IH #n #m
715normalize [2: * #H1] #H2 #H3 [ %] /2 by trans_po_rel/ @(IH … H2) assumption
716qed.
717
718definition cast_return_to_cost_labels ≝ λp.map … (a_return_post p …).
719coercion cast_return_to_cost_labels.
720
721lemma succ_label_leq_absurd : ∀p : label_params.∀x : p.succ_label … p … x ⊑^{p} x → False.
722#p #x #ABS @(absurd ?? (no_maps_in_id … p x)) @(antisym_po_rel … (po_label … p)) //
723qed.
724
725lemma fresh_false : ∀p.∀n.∀l: list (ReturnPostCostLabel p).is_fresh_for_return … l n →
726a_return_cost_label p (succ_label … n) ∈ l = false.
727#p #n #l lapply n -n elim l // * #x #xs #IH #n whd in ⊢ (% → ??%?); * #H1
728#H2 @eq_return_cost_lab_elim
729[ #EQ destruct @⊥ @succ_label_leq_absurd //
730| #_ >IH //
731]
732qed.
733
734lemma inverse_call_post_trans : ∀p : instr_params.∀l_p : label_params.∀i1 : Instructions p l_p.∀n : l_p.
735let dom ≝ get_labels_of_code … i1 in
736no_duplicates … dom →
737∀m,keep.
738∀info,l.call_post_trans … i1 n l = info →
739same_fresh_map_on … dom (lab_map … info) m →
740same_to_keep_on … dom (lab_to_keep … info) keep →
741is_fresh_for_return … keep n →
742call_post_clean … (t_code … info) m keep l
743 = return 〈gen_labels … info,i1〉.
744#p #l_p #i1 elim i1
745[ #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
746  #EQ destruct(EQ) //
747| #x #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
748  #EQ destruct(EQ) //
749| #seq * [|#lbl] #instr #IH #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep
750  #l whd in ⊢ (??%? → ?); #EQ destruct(EQ) #H1 #H2 #H3 whd in ⊢ (??%%); normalize nodelta
751  >IH //
752  [1,4: whd whd in H2; #x #Hx @H2 whd in match (get_labels_of_code ??); //
753  |2,5: whd whd in H1; #x #Hx [ @H1 // ] cases no_dup #H3 #_ <H1
754       [2: whd in match (get_labels_of_code ??); @orb_Prop_r // ]
755       whd in ⊢ (???%); cases(eqb_true … x lbl) inversion(x == lbl) normalize nodelta
756       [2: //] #_ #H4 >H4 in Hx; // #H5 >H5 in H3; * #ABS @⊥ @ABS %
757  |6: cases no_dup //
758  ]
759  normalize nodelta <(H1 lbl)
760  [2: whd in match (get_labels_of_code ??); @orb_Prop_l cases(eqb_true … (a_non_functional_label … lbl) lbl)
761      #H3 #H4 >H4 % ]
762  whd in match (get_element ????); >(\b (refl …)) normalize nodelta
763  >(\b (refl …)) %
764| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #n normalize nodelta
765  #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
766  #EQ destruct(EQ) #H1 #H2 #H3 whd in ⊢ (??%?); >IH3 //
767  [2: whd  in match (get_labels_of_code ??) in H2;
768      change with ([?;?]@?) in match (?::?) in H2;
769      <associative_append in H2; <associative_append
770      <(append_nil … (?@?)) <associative_append in ⊢ (???%? → ?);
771      <(append_nil … (?@?)) in ⊢ (???%? → ?); >associative_append
772      >associative_append in ⊢ (???%? → ?); #H2
773      @(same_to_keep_on_append … H2) // [ >append_nil
774      whd in ⊢ (??%); whd in no_dup:(??%); >associative_append // ]
775      #x #Hx cases (memb_append … Hx) -Hx #Hx @orb_Prop_r @orb_Prop_r
776      [ >memb_append_l1 | >memb_append_l2 ] //
777      @(lab_to_keep_in_domain … (eq_true_to_b … Hx))
778  |3: -H2 whd in match (get_labels_of_code ??) in H1;
779      change with ([?;?]@?) in match (?::?) in H1;
780      <associative_append in H1; <associative_append     
781      <(append_nil … (?@?)) >associative_append
782      change with ([?;?]@?) in match (?::?::?) in ⊢ (???%? → ?);
783      <associative_append in ⊢ (???%? → ?);
784      <associative_append in ⊢ (???%? → ?);
785      <(append_nil … (?@?)) in ⊢ (???%? → ?);
786      >associative_append in ⊢ (???%? → ?); #H1
787      @(same_fresh_map_on_append … H1) //
788      [ >append_nil >associative_append // ]
789      #x whd in match (memb ???); inversion(x == ltrue)
790      [ #Hltrue normalize nodelta #_ whd in match (memb ???); >Hltrue %
791      | #Hltrue normalize nodelta whd in match (memb ???); inversion(x == lfalse)
792         [ #Hlfalse #_ @orb_Prop_r @orb_Prop_l >Hlfalse %
793         | #Hlfalse normalize nodelta #Hx @orb_Prop_r @orb_Prop_r
794           >domain_of_associative_list_append in Hx; #H
795           cases(memb_append … H) #H2 [ >memb_append_l1 | >memb_append_l2 ]
796           // @(lab_map_in_domain … (eq_true_to_b … H2))
797         ]
798      ]
799  |4: cases no_dup #_ * #_ #H @no_duplicates_append_r [2: @(no_duplicates_append_r … H) |]
800  ]
801  normalize nodelta >IH2
802  [5: %
803  |2: /2 by fresh_keep_n_ok/
804  |3: whd  in match (get_labels_of_code ??) in H2;
805   change with ([?;?]@?) in match (?::?) in H2;
806   <associative_append in H2; #H2
807   @(same_to_keep_on_append … H2) // #x #Hx [ @orb_Prop_r @orb_Prop_r ]
808   @(lab_to_keep_in_domain … (eq_true_to_b … Hx))
809  |4: whd  in match (get_labels_of_code ??) in H1;
810   change with ([?;?]@?) in match (?::?) in H1;
811   change with ([?;?]@?) in match (?::?::?) in H1 : (???%?);
812   <associative_append in H1; <associative_append in ⊢ (???%? → ?); #H1
813   @(same_fresh_map_on_append … H1) // [2: /2 by lab_map_in_domain/ ]
814   #x >domain_of_associative_list_append #H cases(memb_append … H)
815   [ whd in ⊢ (??%? → ?%); cases(x == ltrue) // normalize nodelta
816     whd in ⊢ (??%? → ?%); cases(x == lfalse) // normalize nodelta
817     normalize #EQ destruct
818   | #H1 @orb_Prop_r @orb_Prop_r
819     @(lab_map_in_domain … (eq_true_to_b … H1))
820   ]
821  |6: cases no_dup #_ * #_ #K lapply (no_duplicates_append_r … K) @no_duplicates_append_l
822  |*:
823  ]
824  >m_return_bind >IH1
825  [5: %
826  |2: /3 by fresh_keep_n_ok/
827  |3:  whd  in match (get_labels_of_code ??) in H2;
828   change with ([?;?]@?) in match (?::?) in H2;
829   change with ([ ] @ ?) in match (lab_to_keep ???) in H2;
830   >associative_append in H2 : (???%?); #H2
831   @(same_to_keep_on_append … H2) //  #x #Hx cases(memb_append … Hx)
832   -Hx #Hx [ >memb_append_l1 | >memb_append_l2] //
833   @(lab_to_keep_in_domain … (eq_true_to_b … Hx))
834  |4:  whd  in match (get_labels_of_code ??) in H1;
835   change with ([?;?]@?) in match (?::?) in H1;
836   change with ([?;?]@?) in match (?::?::?) in H1 : (???%?);
837    @(same_fresh_map_on_append … H1) // #x >domain_of_associative_list_append
838    #Hx cases(memb_append … Hx) -Hx #Hx [ >memb_append_l1 | >memb_append_l2 ]
839    // @(lab_map_in_domain … (eq_true_to_b … Hx))
840  |6: cases no_dup #_ * #_ @no_duplicates_append_l
841  |*:
842  ]
843  >m_return_bind normalize nodelta whd in H1; <H1
844  [2: whd in match (get_labels_of_code ??); whd in match (memb ???);
845      >(\b (refl …)) % ] whd in match (get_element ????); >(\b (refl …))
846      normalize nodelta >(\b (refl …)) <H1
847      [2: whd in match (get_labels_of_code ??); >memb_cons //
848      whd in match (memb ???); >(\b (refl …)) % ]
849      whd in match (get_element ????); @eq_costlabel_elim normalize nodelta
850      [ #ABS @⊥ cases no_dup >ABS * #H #_ @H @orb_Prop_l 
851      >(\b (refl ? (a_non_functional_label … ltrue))) % ] #_
852      whd in match (get_element ????); >(\b (refl …)) normalize nodelta
853      >(\b (refl …)) %
854| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #n #no_dup #m #keep #info #l whd in ⊢ (??%? → ?);
855  #EQ destruct(EQ) whd in match (get_labels_of_code ??); #fresh_map #keep_on #f_k
856  whd in ⊢ (??%?); >(IH2 … (refl …))
857  [ normalize nodelta >(IH1 … (refl …))
858    [ >m_return_bind <fresh_map [2: @orb_Prop_l >(\b (refl …)) % ]
859      whd in match (get_element ????);
860      inversion(a_non_functional_label … ltrue == a_non_functional_label … lfalse)
861      #Hltrue normalize nodelta
862      [ cases no_dup whd in match (memb ???);
863        cases(eqb_true … (a_non_functional_label … ltrue) (a_non_functional_label … lfalse))
864        #H1 #_ lapply(H1 Hltrue) #EQ destruct(EQ) >(\b (refl …)) * #ABS @⊥ @ABS % ]
865      whd in match (get_element ????); >(\b (refl …)) normalize nodelta >(\b (refl …))
866      <fresh_map [2: @orb_Prop_r @orb_Prop_l >(\b (refl …)) % ]
867      whd in match (get_element ????); >(\b (refl …)) normalize nodelta
868      >(\b (refl …)) %
869    | /2 by fresh_keep_n_ok/
870    | change with ([?;?]@?@?) in keep_on : (??%??); change with ((nil ?) @ ? @ ?) in keep_on : (???%?);
871      @(same_to_keep_on_append … keep_on) // #x /2 by lab_to_keep_in_domain/
872    | change with ([?;?]@?@?) in fresh_map : (??%%?); @(same_fresh_map_on_append … fresh_map)
873      /2 by lab_map_in_domain/ #x whd in match (memb ???); inversion(x==lfalse) #Hlfalse
874      normalize nodelta
875      [ #_ @orb_Prop_r whd in match (memb ???); >Hlfalse %
876      | whd in match (memb ???); inversion(x==ltrue) #Hltrue normalize nodelta [2: *] #_
877        @orb_Prop_l >Hltrue %
878      ]
879    | cases no_dup #_ * #_ /2/
880    ]
881  | //
882  | change with ([?;?]@?@?) in keep_on : (??%??); <associative_append in keep_on;
883    <(append_nil … (?@?)) <(append_nil … (?@?)) in ⊢ (???%? → ?);
884    >associative_append in ⊢ (??%%? → ?); >associative_append in ⊢ (???%? → ?);
885    #keep_on @(same_to_keep_on_append … keep_on) //
886    [ >associative_append >append_nil //
887    | #x #Hx @orb_Prop_r @orb_Prop_r /2 by lab_to_keep_in_domain/
888    ]
889  | change with ([?;?]@?@?) in fresh_map : (??%??); <associative_append in fresh_map;
890    <(append_nil … (?@?)) change with ([?;?]@?@?) in ⊢ (???%? → ?);
891    <associative_append in ⊢ (???%? → ?); <(append_nil … (?@?)) in ⊢ (???%? → ?);
892    >associative_append in ⊢ (??%%? → ?); >associative_append in ⊢ (???%? → ?);
893    #fresh_map @(same_fresh_map_on_append … fresh_map) //
894    [ >append_nil //
895    | #x >domain_of_associative_list_append #Hx cases(memb_append … Hx)
896      [2: #Hx1 @orb_Prop_r @orb_Prop_r @(lab_map_in_domain … (eq_true_to_b … Hx1)) ]
897      whd in match (memb ???); inversion(x == lfalse) normalize nodelta #Hlfalse
898      [ #_ @orb_Prop_r @orb_Prop_l >Hlfalse %
899      | whd in match (memb ???); inversion (x==ltrue) normalize nodelta #Hltrue
900        [ #_ @orb_Prop_l >Hltrue %
901        | whd in match (memb ???); #EQ destruct
902        ]
903      ]
904    ]
905  | cases no_dup #_ * #_ /2 by no_duplicates_append_r/
906  ]
907| #f #act_p * [|#r_lb] #i #IH #n #no_dup #m #keep #info #l whd in ⊢ (??%? → ?);
908  #EQ destruct(EQ) #fresh_map #same_keep #f_k whd in ⊢ (??%?);
909  >(IH … (refl …))
910  [1,6: normalize nodelta
911     [ >fresh_false [2: /2 by fresh_keep_n_ok/] %
912     | <same_keep
913       [ whd in match (memb ???); >(\b (refl …)) normalize nodelta
914         <fresh_map
915         [ whd in match (get_element ????); >(\b (refl …)) normalize nodelta
916           >(\b (refl …)) %
917         | whd in match (memb ???); >(\b (refl …)) %
918         ]
919       | whd in match (memb ???); >(\b (refl …)) %
920       ]
921    ]
922  |2,7: //
923  |3,8: whd in match (get_labels_of_code ???) in same_keep; // #x #Hx <same_keep
924        [2: >memb_cons // >Hx // ] cases no_dup * #ABS #_ whd in ⊢ (???%);
925        inversion(x==?) [2: #_ //] #ABS1 @⊥ @ABS <(\P ABS1) >Hx //
926  |4,9: whd in match (get_labels_of_code ???) in fresh_map; // #x #Hx <fresh_map
927        [2: >memb_cons // >Hx //] cases no_dup * #ABS #_ whd in ⊢ (???%);
928        inversion(x==?) [2: #_ //] #ABS1 @⊥ @ABS <(\P ABS1) //
929  |5,10: [ @no_dup | cases no_dup // ]
930  ]
931| #lin #io #lout #i #IH #n whd in match (get_labels_of_code ???); #no_dup
932  #m #keep #info #l whd in ⊢ (??%? → ?); #EQ destruct(EQ) #fresh_map #same_keep
933  #f_k whd in ⊢ (??%?); >(IH … (refl …))
934  [ normalize nodelta <fresh_map [2: >memb_cons // >memb_hd // ]
935    whd in match (get_element ????); >(\b (refl …)) normalize nodelta
936    >(\b (refl …)) <fresh_map [2: >memb_hd //] whd in match (get_element ????);
937    inversion(lin==lout)
938    [ #ABS @⊥ cases no_dup * #ABS1 #_ @ABS1 whd in match (memb ???); >(\P ABS)
939      >(\b (refl …)) //
940    | #H inversion (a_non_functional_label … lin== ? lout)
941      [ #ABS lapply(\P ABS) #EQ destruct >(\b (refl …)) in H; #EQ destruct
942      | #_ normalize nodelta whd in match (get_element ????); >(\b (refl …))
943        normalize nodelta >(\b (refl …)) %
944      ]
945    ]
946  | //
947  | #x #Hx >same_keep [2: >memb_cons // >memb_cons // >Hx % ] %
948  | #x #Hx <fresh_map [2: >memb_cons // >memb_cons // >Hx %]
949    cases no_dup * #ABS1 ** #ABS2 #_ whd in ⊢ (???%); inversion(x == lout)
950    normalize nodelta
951    [2: #_ whd in ⊢ (???%); inversion(x==lin) normalize nodelta //
952        #H @⊥ @ABS1 >memb_cons // <(\P H) >Hx //
953    | #H @⊥ @ABS2 <(\P H) >Hx //
954    ]
955  | cases no_dup #_ * #_ //
956  ]
957]
958qed.
959
960definition fresh_for_prog_aux : ∀p,p',l_p.Program p p' l_p → l_p → l_p ≝
961λp,p',l_p,prog,n.foldl … (λn,i.op … l_p … n (compute_max_n … (f_body … i))) n (env … prog).
962
963
964lemma fresh_aux_ok : ∀p,p',l_p.∀prog : Program p p' l_p.∀n,m.n ⊑^{l_p} m →
965fresh_for_prog_aux … prog n ⊑^{l_p} fresh_for_prog_aux … prog m.
966#p #p' #l_p * #env #main elim env // #hd #tl #IH #n #m #H whd in ⊢ (???%%);
967@IH whd in ⊢ (???%?); @(monotonic_magg … l_p … H)
968qed.
969
970definition fresh_for_prog : ∀p,p',l_p.Program p p' l_p → l_p ≝
971λp,p',l_p,prog.fresh_for_prog_aux … prog
972(compute_max_n … (main … prog)).
973
974definition translate_env ≝
975λp,p',l_p.λenv : list (env_item p p' l_p).λmax_all.(foldr ??
976           (λi,x.let 〈t_env,n,m,keep〉 ≝ x in
977           let info ≝ call_post_trans … (f_body … i) n (nil ?) in
978                   〈(mk_env_item ???
979                       (mk_signature ??(f_name ?? i) (f_pars … i) (f_ret … i))
980                       (f_lab … i) (t_code … info)) :: t_env,
981                     fresh … info, 〈a_call … (f_lab … i),(a_call … (f_lab … i)) :: (gen_labels ?? info)〉 ::
982                                     ((lab_map … info) @ m),(lab_to_keep … info) @ keep〉)
983          (〈nil ?,max_all,nil ?,nil ?〉) env).
984
985definition trans_prog : ∀p,p',l_p.Program p p' l_p →
986((Program p p' l_p) × (associative_list (DEQCostLabel l_p) (CostLabel l_p)) × ((list (ReturnPostCostLabel l_p))))≝
987λp,p',l_p,prog.
988let max_all ≝ fresh_for_prog … prog in
989let info_main ≝ (call_post_trans … (main … prog) max_all (nil ?)) in
990let 〈t_env,n,m,keep〉 ≝ translate_env … (env … prog) (fresh … info_main) in
991〈mk_Program ??? t_env (t_code … info_main),m @ (lab_map … info_main),keep @ (lab_to_keep … info_main)〉.
992
993definition map_labels_on_trace : ∀p : label_params.
994(associative_list (DEQCostLabel p) (CostLabel p)) → list (CostLabel p) → list (CostLabel p) ≝
995λp,m,l.foldr … (λlab,t.(get_element … m lab) @ t) (nil ?) l.
996
997lemma map_labels_on_trace_append:
998 ∀p,m,l1,l2. map_labels_on_trace p m (l1@l2) =
999  map_labels_on_trace p m l1 @ map_labels_on_trace p m l2.
1000#p #m #l1 elim l1 // #hd #tl #IH #l2 >associative_append <IH //
1001qed.
1002
1003include "../src/common/Errors.ma".
1004include "Permutation.ma".
1005
1006(*
1007
1008axiom is_permutation: ∀A.list A → list A → Prop.
1009axiom is_permutation_eq : ∀A.∀l : list A.is_permutation … l l.
1010axiom is_permutation_cons : ∀A.∀l1,l2,x.is_permutation A l1 l2 →
1011                                       is_permutation A (x :: l1) (x :: l2).
1012*)
1013(*
1014inductive is_permutation (A : Type[0]) : list A → list A → Prop ≝
1015| p_empty : is_permutation A (nil ?) (nil ?)
1016| p_append : ∀x,x1,x2,y,y1,y2.
1017               x = y → is_permutation A (x1 @ x2) (y1 @ y2) →
1018                 is_permutation A (x1 @ [x] @ x2) (y1 @ [y] @ y2).
1019
1020lemma is_permutation_eq : ∀A.∀l : list A.is_permutation … l l.
1021#A #l elim l // #x #xs #IH
1022change with ((nil ?) @ (x :: xs)) in ⊢ (??%%);
1023>append_cons >associative_append
1024@(p_append ? x (nil ?) xs x (nil ?) xs (refl …)) @IH
1025qed.
1026
1027lemma is_permutation_append : ∀A.∀l1,l2,l3,l4 : list A.
1028is_permutation A l1 l3 → is_permutation A l2 l4 →
1029is_permutation A (l1 @ l2) (l3 @ l4).
1030#A #l1 inversion (|l1|)  [2: #n lapply l1 elim n
1031[ #l2 #l3 #l4 #H inversion H // #x #x1 #x2 #y #y1 #y2 #EQ #H1 #_
1032 #ABS cases(nil_to_nil … (sym_eq ??? ABS)) -ABS #_ #ABS
1033 cases(nil_to_nil … ABS) #EQ1 destruct(EQ1) ]
1034#x #xs #IH #l2 #l3 #l4 #H inversion H
1035[#EQ lapply(jmeq_to_eq ??? EQ) -EQ #EQ destruct(EQ) ]
1036#y #y1 #y2 #z #z1 #z2 #EQ destruct(EQ) #H1 #_ #EQx_xs #EQ destruct(EQ) #_
1037*)
1038
1039
1040
1041lemma lookup_ok_append : ∀p,p',l_p,l,f,env_it.
1042lookup p p' l_p l f = return env_it → ∃l1,l2. l = l1 @ [env_it] @ l2 ∧
1043f_name … env_it = f.
1044#p #p' #l_p #l elim l [ #f #env_it normalize #EQ destruct]
1045#x #xs #IH #f #env_it whd in ⊢ (??%? → ?); @eq_function_name_elim
1046[ #EQ destruct(EQ) normalize nodelta whd in ⊢ (???% → ?); #EQ destruct
1047  %{(nil ?)} %{xs} /2/
1048| #Hno_f normalize nodelta #EQ_env_it cases(IH … EQ_env_it)
1049  #l1 * #l2 * #EQ1 #EQ2 %{(x :: l1)} %{l2} >EQ1 /2/
1050]
1051qed.
1052(*
1053lemma foldr_append :
1054  ∀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.
1055#A #B #l1 elim l1 //
1056#hd #tl #Hind #l2 #f #seed normalize >Hind @refl
1057qed.
1058*)
1059
1060
1061
1062(* aggiungere fresh_to_keep al lemma seguente??*)
1063
1064lemma fresh_for_subset : ∀p : label_params.∀l1,l2,n.l1 ⊆ l2 → is_fresh_for_return p … l2 n →
1065is_fresh_for_return p … l1 n.
1066#p #l1 elim l1 // * [1,2,3: * #x] #xs #IH #l2 #n * #H1 #H2 #H3 whd
1067try(@IH) // % [2: @IH //] elim l2 in H1 H3; normalize [*]
1068* [1,2,3: * #y] #ys #IH normalize
1069[2,3: * [2,4: #H3 [2: @IH //] * #H4 #H5 @IH //
1070|*: #EQ destruct * //
1071]]
1072*
1073[1,3: #EQ destruct ] #H3 #H4 @IH //
1074qed.
1075
1076lemma fresh_append : ∀p.∀n,l1,l2.is_fresh_for_return p l1 n → is_fresh_for_return p l2 n →
1077is_fresh_for_return p (l1@l2) n.
1078#p #n #l1 lapply n -n elim l1 // * [1,2,3: * #x] #xs #IH #n #l2 [2: * #H1 ] #H2 #H3
1079[ % // @IH //] @IH //
1080qed.
1081
1082definition labels_of_prog : ∀p,p',l_p.Program p p' l_p → ? ≝
1083λp,p',l_p,prog.foldr … (λx,l.l @ (get_labels_of_code … (f_body … x)))
1084 (get_labels_of_code … (main … prog)) (env … prog).
1085
1086lemma cast_return_append : ∀p.∀l1,l2.cast_return_to_cost_labels p … (l1 @ l2) =
1087(cast_return_to_cost_labels p … l1) @ (cast_return_to_cost_labels p … l2).
1088#p #l1 #l2 @(sym_eq … (map_append …)) qed.
1089
1090include alias "arithmetics/nat.ma".
1091
1092
1093lemma is_fresh_code : ∀p,l_p.∀i : Instructions p l_p.
1094is_fresh_for_return l_p (get_labels_of_code … i) (compute_max_n … i).
1095#p #l_p #main  elim main //
1096[ #seq * [| * #lbl] #i #IH normalize // @(fresh_keep_n_ok … IH) //
1097| #cond * #ltrue #i1 * #lfalse #i2 #i3 #IH1 #IH2 #IH3 whd in ⊢ (??%%);
1098  @fresh_append
1099  [ @(fresh_keep_n_ok … IH1) @max_1 @max_1 @max_1 @max_1 //
1100  | @fresh_append
1101    [ @(fresh_keep_n_ok … IH2) @max_1 @max_1 @max_1 @max_2 //
1102    | @(fresh_keep_n_ok … IH3) @max_1 @max_1 @max_2 //
1103    ]
1104  ]
1105| #cond * #ltrue #i1 * #lfalse #i2 #IH1 #IH2 whd in ⊢ (??%%); @fresh_append
1106  [ @(fresh_keep_n_ok … IH1) @max_1 @max_1 @max_1 //
1107  | @(fresh_keep_n_ok … IH2) @max_1 @max_1 @max_2 //
1108  ]
1109| #f #act_p * [| * #lbl] #i #IH whd in ⊢ (??%%); //
1110  change with ([?]@?) in ⊢ (??%?); @fresh_append
1111  [ whd % //
1112  | @(fresh_keep_n_ok … IH) @max_2 //
1113  ]
1114| * #lin #io * #lout #i #IH whd in ⊢ (??%%); @(fresh_keep_n_ok … IH)
1115  @max_1 @max_1 //
1116]
1117qed.
1118
1119lemma is_fresh_fresh_for_prog : ∀p,p',l_p.∀prog : Program p p' l_p.
1120is_fresh_for_return … (labels_of_prog … prog) (fresh_for_prog … prog).
1121#p #p' #l_p * #env #main whd in match fresh_for_prog; normalize nodelta whd in ⊢ (??%?);
1122elim env // * #sig #cost #i #tail #IH  whd in ⊢ (??%?); @fresh_append
1123[ @(fresh_keep_n_ok … IH) @fresh_aux_ok @max_1 //
1124| @(fresh_keep_n_ok … (is_fresh_code … i)) whd in match fresh_for_prog_aux; normalize nodelta
1125  whd in ⊢ (????%); elim tail [ @max_2 // ] #hd1 #tl1 #IH1 @(trans_po_rel …  IH1)
1126  whd in ⊢ (????%); change with (fresh_for_prog_aux ??? (mk_Program ??? tl1 main) ?) in ⊢ (???%%);
1127  @fresh_aux_ok @max_1 //
1128]
1129qed.
1130
1131lemma memb_cast_return : ∀p.∀keep,x.x ∈ cast_return_to_cost_labels p keep →
1132∃ y.x = a_return_post … y ∧ bool_to_Prop (y ∈ keep).
1133#p #keep elim keep
1134[ #x *] #x #xs #IH #y whd in match cast_return_to_cost_labels;
1135whd in match (map ????); whd in match (memb ???); inversion(y==x)
1136[ #Hx #_ %{x} >(\P Hx) %{(refl …)} >memb_hd //
1137| #Hx normalize nodelta #H cases(IH … H) #z * #H1 #H2 %{z} %{H1} >memb_cons // >H2 //
1138]
1139qed.
1140
1141lemma lab_to_keep_in_prog : ∀p,p',l_p.∀prog : Program p p' l_p.
1142∀t_prog,m,keep.trans_prog … prog = 〈t_prog,m,keep〉 →
1143(cast_return_to_cost_labels l_p keep) ⊆ (labels_of_prog p p' l_p prog).
1144#p #p' #l_p * #env #main #t_prog #m #keep whd in match trans_prog; normalize nodelta
1145@pair_elim * #env1 #fresh * #m1 #keep1 #EQenv1 normalize nodelta #EQ destruct
1146lapply EQenv1 -EQenv1 lapply keep1 -keep1 lapply m1 -m1 lapply fresh -fresh
1147lapply env1 -env1 generalize in match (fresh_for_prog ????); elim env
1148[ #n #t_env #n1 #m #keep whd in ⊢ (??%? → ?); #EQ destruct whd in match (append ???);
1149  @subset_def #x #H whd in match (labels_of_prog); normalize nodelta
1150  whd in match (foldr ?????); cases(memb_cast_return … H) -H #x1 * #EQ1 #H destruct
1151  @(lab_to_keep_in_domain … H)
1152| #x #xs #IH #n #t_env #n1 #m #keep whd in ⊢ (??%? → ?); @pair_elim
1153  * #t_env_tail #fresh_tail * #t_m_tail #t_keep_tail
1154  change with (translate_env ?????) in match (foldr ?????); #EQt_env_tail
1155  normalize nodelta #EQ1 destruct >cast_return_append @subset_append
1156  [ >cast_return_append @subset_append
1157    [ whd in match labels_of_prog; normalize nodelta whd in match (foldr ?????);
1158      @subset_def #y #H cases(memb_cast_return … H) -H #y1 * #EQ destruct #H
1159      >memb_append_l2 // @(lab_to_keep_in_domain … H)
1160    | whd in match labels_of_prog; normalize nodelta whd in match (foldr ?????);
1161      change with (labels_of_prog ??? (mk_Program ??? xs ?)) in match (foldr ?????);
1162      @subset_append_h1 @(transitive_subset … (IH … EQt_env_tail))
1163      >cast_return_append @subset_append_h1 //
1164    ]
1165  | whd in match labels_of_prog; normalize nodelta whd in match (foldr ?????);
1166    change with (labels_of_prog ??? (mk_Program ??? xs ?)) in match (foldr ?????);
1167    @subset_append_h1 @(transitive_subset … (IH … EQt_env_tail))
1168     >cast_return_append @subset_append_h2 //
1169  ]
1170]
1171qed.
1172
1173lemma fresh_call_post_trans_ok : ∀p,l_p.∀i : Instructions p l_p.∀n,l.
1174n ⊑^{l_p} fresh … (call_post_trans … i n l).
1175#p #l_p #i elim i //
1176qed.
1177
1178lemma fresh_translate_env_ok : ∀p,p',l_p.∀env,t_env : list (env_item p p' l_p).∀n,n1,m,keep.
1179translate_env … env n = 〈t_env,n1,m,keep〉 → n ⊑^{l_p} n1.
1180#p #p' #l_p #env elim env
1181[ #t_env #n #n1 #m #keep whd in ⊢ (??%? → ?); #EQ destruct // ]
1182#x #xs #IH #t_env #n #n1 #m #keep whd in ⊢ (??%? → ?);
1183change with (translate_env ?????) in match (foldr ?????); @pair_elim
1184* #t_env_tail #fresh_tail * #t_m_tail #t_keep_tail #EQt_env_tail normalize nodelta
1185#EQ destruct @(trans_po_rel … (IH … EQt_env_tail)) @fresh_call_post_trans_ok
1186qed.
1187 
1188
1189lemma trans_env_ok : ∀p : state_params.∀ prog.
1190no_duplicates_labels … prog →
1191let 〈t_prog,m,keep〉 ≝ trans_prog … prog in
1192∀f,env_it.lookup p p p (env … prog) f = return env_it →
1193let dom ≝ get_labels_of_code … (f_body … env_it) in
1194∃env_it',n.is_fresh_for_return p keep n ∧lookup p p p (env … t_prog) f = return env_it' ∧
1195let info ≝ call_post_trans … (f_body … env_it) n (nil ?) in
1196t_code … info = f_body … env_it' ∧
1197get_element … m (a_call … (f_lab … env_it')) = (a_call … (f_lab … env_it')) :: gen_labels … info ∧
1198f_sig … env_it = f_sig … env_it' ∧ f_lab … env_it = f_lab … env_it' ∧
1199same_fresh_map_on … dom m (lab_map … info) ∧ same_to_keep_on … dom keep (lab_to_keep … info).
1200#p #prog inversion(trans_prog … prog) * #t_prog0 #m0 #keep0 #EQt_prog
1201lapply EQt_prog normalize nodelta
1202generalize in match keep0 in ⊢ (% → ? → ? → ? → ? → ??(λ_.??(λ_.?%?)));
1203#keep1 #EQkeep1 inversion prog in EQt_prog; #env #main #EQprog
1204whd in match trans_prog; normalize nodelta
1205@pair_elim
1206cut(fresh_for_prog ??? prog ⊑^{p} fresh_for_prog ??? (mk_Program … env main)) [ >EQprog //]
1207generalize in match (fresh_for_prog ????) in ⊢ (????% → %);
1208lapply t_prog0 lapply m0 lapply keep0
1209elim env in ⊢ (?→ ? → ? → ? → ? → %);
1210[ #keep #m #t_prog #n #_ * #env' #fresh * #x #y #_ #_ #_ #f #env_it normalize in ⊢ (% → ?);  #ABS destruct]
1211* #hd_sig #hd_lab #hd_code #tail #IH #keep #m #t_prog #fresh1 #Hfresh1 * #env' #fresh * #m' #keep'
1212normalize in ⊢ (% → ?); normalize nodelta @pair_elim * #env_tail #fresh_tail
1213* #m_tail #keep_tail change with (translate_env ?????) in ⊢ (??%? → ?); #EQtail normalize nodelta #EQ1 destruct(EQ1) #EQ2 destruct(EQ2)
1214whd in ⊢ (% → ?); whd in match (foldr ?????); * #Hhd_lab #H lapply(no_duplicates_append_r … H)
1215change with (no_duplicates_labels p p p (mk_Program p p p tail main)) in match
1216(no_duplicates_labels p p p (mk_Program p p p tail main)); #no_dup_tail
1217lapply(no_duplicates_append_l … H) #no_dup_head normalize nodelta
1218#f #env_it whd in ⊢ (??%? → ?); @eq_function_name_elim normalize nodelta
1219[ #EQ destruct(EQ) whd in ⊢ (???% → ?); #EQ destruct(EQ)
1220  inversion (call_post_trans … hd_code fresh_tail [])
1221  #gen_labs #t_hd_code #t_fresh #t_lab_map #t_lab_to_keep #EQ_trans_code
1222  %{(mk_env_item … hd_sig hd_lab t_hd_code)} %{fresh_tail} %
1223  [ %
1224    [ @(fresh_keep_n_ok … fresh1)
1225      [ @(fresh_keep_n_ok … Hfresh1)
1226        @(fresh_for_subset … (labels_of_prog … prog))
1227        [ @(lab_to_keep_in_prog … EQkeep1) | @is_fresh_fresh_for_prog ]
1228       | @(trans_po_rel … (fresh_translate_env_ok … EQtail)) //
1229      ]
1230    | whd in ⊢ (??%?); @eq_function_name_elim [2: * #H @⊥ @H %] #_ normalize nodelta
1231      @eq_f cases hd_sig // ]] >EQ_trans_code % [% [ % [ % [% // whd in ⊢ (??%?); >(\b (refl …)) %] % ] % | whd
1232    #x #Hx whd in ⊢ (??%?); >(? : (x == hd_lab) = false)
1233    [2: inversion(x==hd_lab) // #EQx_hdlab cases Hhd_lab -Hhd_lab #Hhd_lab cases Hhd_lab
1234        >memb_append_l1 // <(\P EQx_hdlab) >Hx // ]
1235    normalize nodelta >get_element_append_l1
1236    [2: % #ABS @(memb_no_duplicates_append … x … H) // elim tail
1237        [ whd in match (foldr ?????); @lab_map_in_domain // ]
1238        #x #xs #IH whd in match (foldr ?????); @orb_Prop_r
1239        >memb_append_l2 // >IH %
1240    ] @get_element_append_l1
1241    % #H1 
1242    (* subproof with no nice statement *)
1243    lapply H1 -H1 lapply H -H lapply Hhd_lab -Hhd_lab lapply EQtail -EQtail
1244    generalize in match fresh1; lapply env_tail -env_tail lapply fresh_tail
1245    -fresh_tail lapply m_tail -m_tail lapply keep_tail -keep_tail elim tail
1246    normalize nodelta
1247    [ #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1248      #EQ destruct(EQ) whd in match (foldr ?????);
1249      #H1 #H2 * ]
1250    #head #tail #IH #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1251    whd in match (foldr ?????);
1252    @pair_elim * #env_tail_res #fresh_tail_res * #lab_map_res #keep_res #EQres
1253    normalize nodelta #EQ destruct(EQ) whd in match (foldr ?????);
1254    #H1 #H2 whd in match (memb ???); inversion(x == ?)
1255    [ #H3 #_ <(\P H3) in H2; change with ([?]@?) in match (?::?); #H2
1256      lapply(no_duplicates_append_commute … H2) -H2 ** #ABS #_ @ABS
1257      >memb_append_l2 // >Hx %
1258    | #H3 normalize nodelta #H4 @(IH … EQres)
1259      [3: >domain_of_associative_list_append in H4; #H4 cases(memb_append … H4) [2: #EQ >EQ %]
1260          #ABS @⊥ @(memb_no_duplicates_append … x … H2) // @orb_Prop_r >memb_append_l1 //
1261          @(lab_map_in_domain … (eq_true_to_b … ABS))
1262      | % #ABS elim H1 -H1 #H1 @H1 cases(memb_append … ABS)
1263        [ #H5 >memb_append_l1 //
1264        | #H5 >memb_append_l2 // @orb_Prop_r >memb_append_l2 //
1265        ]
1266      | lapply(no_duplicates_append_commute … H2) * #_ >associative_append
1267        #h @no_duplicates_append_commute @(no_duplicates_append_r … h)
1268      ]
1269    ]
1270    ]
1271  | whd #x #Hx >memb_append_l12
1272    [2: @notb_Prop % #ABS @(memb_no_duplicates_append … H … Hx) elim tail
1273        [ whd in match (foldr ?????); @lab_to_keep_in_domain // ]
1274        #x #xs #IH whd in match (foldr ?????); @orb_Prop_r
1275        >memb_append_l2 // >IH %
1276    ]
1277    >memb_append_l12 // inversion(memb ???) // #ABS @(memb_no_duplicates_append … (a_return_post … x) … H)
1278    // @⊥
1279    (* subproof with no nice statement *)
1280    lapply ABS -ABS lapply H -H lapply Hhd_lab -Hhd_lab lapply EQtail -EQtail
1281    generalize in match fresh1; lapply env_tail -env_tail lapply fresh_tail
1282    -fresh_tail lapply m_tail -m_tail lapply keep_tail -keep_tail elim tail
1283    normalize nodelta
1284    [ #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1285      #EQ destruct(EQ) whd in match (foldr ?????);
1286      #H1 #H2 whd in ⊢ (??%? → ?); #EQ destruct ]
1287    #head #tail #IH #keep_tail #m_tail #fresh_tail #env_tail #n whd in ⊢ (??%? → ?);
1288    whd in match (foldr ?????);
1289    @pair_elim * #env_tail_res #fresh_tail_res * #lab_map_res #keep_res #EQres
1290    normalize nodelta #EQ destruct(EQ) whd in match (foldr ?????);
1291    #H1 #H2 #H3 cases(memb_append … H3) -H3
1292    [ #H3 change with ([?]@?) in match (?::?) in H2;
1293      lapply(no_duplicates_append_commute … H2) -H2 * #_ #H4 @(memb_no_duplicates_append … (a_return_post … x) … H4)
1294      [ whd in match (append ???); >memb_append_l1 // >(lab_to_keep_in_domain … (eq_true_to_b … H3)) %
1295      | //
1296      ]
1297    | #H3 normalize nodelta @(IH … EQres)
1298      [3: //
1299      |  % #ABS elim H1 -H1 #H1 @H1 cases(memb_append … ABS)
1300        [ #H5 >memb_append_l1 //
1301        | #H5 >memb_append_l2 // @orb_Prop_r >memb_append_l2 //
1302        ]
1303      | lapply(no_duplicates_append_commute … H2) * #_ >associative_append
1304        #h @no_duplicates_append_commute @(no_duplicates_append_r … h)
1305      ]
1306    ]
1307  ]
1308| #Hf #Henv_it cases(IH … no_dup_tail … Henv_it)
1309  [9: >EQtail in ⊢ (??%?); %
1310  |13: %
1311  |6: assumption
1312  |10: %
1313  |*:
1314  ]
1315  #new_env_it * #new_fresh ** #is_fresh_newfresh #EQlook_new_env_it ***** #EQt_code #EQ_get_el
1316  #EQsign_env_it #EQ_f_lab #same_fresh_map #same_to_keep %{new_env_it} %{new_fresh}
1317  %
1318  [ %
1319     [ assumption
1320     | whd in ⊢ (??%?); @eq_function_name_elim [ #ABS >ABS in Hf; * #H @⊥ @H %]
1321        #_ normalize nodelta assumption ]]
1322   % [2: #x #Hx <same_to_keep // >associative_append @memb_append_l22
1323        inversion(memb ???) // #ABS lapply(lab_to_keep_in_domain … (eq_true_to_b … ABS))
1324        #ABS1 @(memb_no_duplicates_append … (a_return_post … x) … H) //
1325        cases(lookup_ok_append … Henv_it) #l1 * #l2 * #EQ1 #EQ2 destruct(EQ1 EQ2)
1326        >foldr_map_append >memb_append_l2 // >foldr_map_append >memb_append_l1 //
1327        whd in match (foldr ?????); @orb_Prop_r >memb_append_l1 // >Hx % ]
1328   % [2: #x #Hx <same_fresh_map // >cons_append <associative_append
1329         <associative_append in ⊢ (??(???(??%?)?)?); >associative_append
1330         @(get_element_append_r1)
1331         % >domain_of_associative_list_append #ABS cases(memb_append … ABS)
1332         [ whd in match (memb ???); inversion(x==hd_lab) normalize nodelta
1333           [2: #_ whd in match (memb ???); #EQ destruct ] #EQx_hdlab #_
1334               <(\P EQx_hdlab) in Hhd_lab; cases(lookup_ok_append … Henv_it)
1335               #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1336               * #ABS1 @ABS1 >memb_append_l2 // >memb_append_l2 //
1337               >memb_append_l1 // whd in ⊢ (??%?); cases(x==?) //
1338               normalize nodelta >memb_append_l1 // >Hx %
1339         | #ABS1 @(memb_no_duplicates_append … x … H)
1340           [ @(lab_map_in_domain … (eq_true_to_b … ABS1))
1341           | cases(lookup_ok_append … Henv_it)
1342             #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1343             >memb_append_l2 // >memb_append_l1 //
1344             whd in ⊢ (??%?); cases(x==?) //
1345             normalize nodelta >memb_append_l1 // >Hx %
1346           ]
1347         ]
1348     ] 
1349   % // % // % // <EQ_get_el >cons_append <associative_append  <associative_append in ⊢ (??(???(??%?)?)?);
1350   >associative_append
1351   @get_element_append_r1 % >domain_of_associative_list_append #ABS cases(memb_append … ABS)
1352         [ whd in match (memb ???); inversion(a_call … (f_lab … new_env_it)== a_call … hd_lab)
1353           #EQ_hdlab normalize nodelta
1354           [2: whd in ⊢ (??%? → ?); #EQ destruct ] 
1355           #_ <(\P EQ_hdlab) in Hhd_lab; cases(lookup_ok_append … Henv_it)
1356           #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1357           * #ABS1 @ABS1 >memb_append_l2 // >memb_append_l2 //
1358           >memb_append_l1 // whd in ⊢ (??%?); >EQ_f_lab >(\b (refl …)) //
1359         | #ABS1 @(memb_no_duplicates_append … (a_call … (f_lab … new_env_it)) … H)
1360           [ @(lab_map_in_domain … (eq_true_to_b … ABS1))
1361           | cases(lookup_ok_append … Henv_it)
1362             #tail1 * #tail2 * #EQ1 #EQ2 destruct >foldr_map_append >foldr_map_append
1363             >memb_append_l2 // >memb_append_l1 //
1364             whd in ⊢ (??%?); >EQ_f_lab >(\b (refl …)) //
1365           ]
1366         ]
1367]
1368qed.
1369
1370(*
1371axiom permute_ok : ∀A.∀l1,l2,l3,l4,l5,l6,l7,l8,l9,x,y.
1372  (is_permutation A ((l5 @l1) @l6@l7) ((l4 @[]) @l6@l7)
1373 →is_permutation A ((l6 @l2) @l7) ((l3 @l8) @l9)
1374 →is_permutation A
1375   (y ::((l6 @((x ::l5) @(l1 @l2))) @l7))
1376   (((x ::l4 @y ::l3) @l8) @l9)).
1377*)   
1378
Note: See TracBrowser for help on using the repository browser.