source: LTS/Language.ma @ 3562

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