source: LTS/Language.ma @ 3673

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