source: LTS/Language.ma @ 3576

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