source: LTS/Language.ma @ 3402

Last change on this file since 3402 was 3402, checked in by piccolo, 6 years ago
File size: 61.1 KB
Line 
1(**************************************************************************)
2(*       ___                                                              *)
3(*      ||M||                                                             *)
4(*      ||A||       A project by Andrea Asperti                           *)
5(*      ||T||                                                             *)
6(*      ||I||       Developers:                                           *)
7(*      ||T||         The HELM team.                                      *)
8(*      ||A||         http://helm.cs.unibo.it                             *)
9(*      \   /                                                             *)
10(*       \ /        This file is distributed under the terms of the       *)
11(*        v         GNU General Public License Version 2                  *)
12(*                                                                        *)
13(**************************************************************************)
14
15include "basics/types.ma".
16include "Traces.ma".
17include "basics/lists/list.ma".
18include "../src/utilities/option.ma".
19include "basics/jmeq.ma".
20
21discriminator option.
22
23record instr_params : Type[1] ≝
24{ seq_instr : DeqSet
25; io_instr : DeqSet
26; cond_instr : DeqSet
27; loop_instr : DeqSet
28; act_params_type : DeqSet
29; return_type : DeqSet
30}.
31
32
33inductive Instructions (p : instr_params) : Type[0] ≝
34 | EMPTY : Instructions p
35 | RETURN : return_type p → Instructions p
36 | SEQ : (seq_instr p) → option NonFunctionalLabel → Instructions p → Instructions p
37 | COND : (cond_instr p) → NonFunctionalLabel → Instructions p →
38                 NonFunctionalLabel → Instructions p → Instructions p →
39                       Instructions p
40 | LOOP : (loop_instr p) → NonFunctionalLabel → Instructions p →
41                  NonFunctionalLabel → Instructions p → Instructions p
42 | CALL : FunctionName → (act_params_type p) → option ReturnPostCostLabel →
43            Instructions p → Instructions p
44 | IO : NonFunctionalLabel → (io_instr p) → NonFunctionalLabel → Instructions p →
45             Instructions p.
46
47let rec eq_instructions (p : instr_params) (i : Instructions p)
48 on i : (Instructions p) → bool ≝
49match i with
50[ EMPTY ⇒ λi'.match i' with [ EMPTY ⇒ true | _ ⇒ false ]
51| RETURN x ⇒ λi'.match i' with [ RETURN y ⇒ x == y | _ ⇒ false ]
52| SEQ x lab instr ⇒ λi'.match i' with
53                      [ SEQ y lab' instr' ⇒ x == y ∧ eq_instructions … instr instr' ∧
54                              match lab with [ None ⇒ match lab' with [ None ⇒ true | _ ⇒ false ]
55                                             | Some l1 ⇒ match lab' with [Some l2 ⇒ eq_nf_label l1 l2 | _ ⇒ false]
56                                             ]
57                      | _ ⇒ false
58                      ]
59| COND x ltrue i1 lfalse i2 i3 ⇒ λi'.match i' with
60                          [ COND y ltrue' i1' lfalse' i2' i3' ⇒
61                             x == y ∧ eq_nf_label ltrue ltrue' ∧
62                             eq_instructions … i1 i1' ∧ eq_nf_label lfalse lfalse' ∧
63                             eq_instructions … i2 i2' ∧ eq_instructions … i3 i3'
64                         | _ ⇒ false
65                         ]
66| LOOP x ltrue i1 lfalse i2 ⇒ λi'.match i' with
67              [ LOOP y ltrue' i1' lfalse' i2' ⇒ x == y ∧
68                      eq_instructions … i1 i1' ∧ eq_nf_label ltrue ltrue' ∧
69                      eq_instructions … i2 i2'
70              | _ ⇒ false
71              ]
72| CALL f act_p r_lb i1 ⇒ λi'.match i' with
73             [ CALL f' act_p' r_lb' i1' ⇒ eq_function_name f f' ∧
74                       act_p == act_p' ∧ eq_instructions … i1 i1' ∧
75                       match r_lb with [ None ⇒ match r_lb' with [None ⇒ true | _ ⇒ false]
76                                       | Some z ⇒ match r_lb' with [Some w ⇒  eq_return_cost_lab z w | _ ⇒ false ]
77                                       ]
78            | _ ⇒ false
79            ]
80| IO lin io lout i1 ⇒ λi'.match i' with
81             [ IO lin' io' lout' i1' ⇒ eq_nf_label lin lin' ∧ io == io' ∧
82                                       eq_nf_label lout lout' ∧ eq_instructions … i1 i1'
83             | _ ⇒ false                                       
84             ]
85].
86
87(*
88lemma eq_instructions_elim : ∀ P : bool → Prop.∀p,i1,i2.(i1 = i2 → P true) →
89(i1 ≠ i2 → P false) → P (eq_instructions p i1 i2).
90#P #p #i1 elim i1
91[* normalize [/2/ ] #x [|*: #y #z [2,3: #w1 #w2 [#w3] |4,5: #w1]] #_ #H2 @H2 % #EQ
92  lapply (eq_to_jmeq ??? EQ) #EQ' destruct(EQ')
93| #rt * normalize [2: #rt' cases (dec_eq … rt rt') #H [>(\b H) | >(\bf H) ] /2/
94  #_ #K @K % #abs lapply (eq_to_jmeq ??? abs) #abs' destruct(abs') @(absurd ?? H) //]
95  [|*: #x #y #z [2,3: #w1 #w2 [#w3] |4,5: #w1]] #_ #H2 @H2 % #EQ
96  lapply (eq_to_jmeq ??? EQ) #EQ' destruct(EQ')
97| cases daemon (*TODO*)
98qed.
99*)
100
101record env_params : Type[1] ≝
102{ form_params_type : Type[0]
103}.
104
105record signature (p : env_params) (p' : instr_params) : Type[0] ≝
106{ f_name : FunctionName
107; f_pars : form_params_type p
108; f_ret : return_type p'
109}.
110
111record env_item (p : env_params) (p' : instr_params) : Type[0] ≝
112{ f_sig :> signature p p'
113; f_lab : CallCostLabel
114; f_body : Instructions p'
115}.
116
117record state_params : Type[1] ≝
118{ i_pars :> instr_params
119; e_pars :> env_params
120; store_type : DeqSet
121}.
122
123record state (p : state_params) : Type[0] ≝
124{ code : Instructions p
125; cont : list (ActionLabel × (Instructions p))
126; store : store_type p
127; io_info : bool
128}.
129
130definition is_io : ∀p.state p → Prop ≝ λp,st.io_info … st = true.
131
132record sem_state_params (p : state_params) : Type[0] ≝
133{ eval_seq : seq_instr p → state p → option (store_type p)
134; eval_io : io_instr p → state p → option (store_type p)
135; eval_cond_cond : cond_instr p → state p → option (bool × (store_type p))
136; eval_loop_cond : loop_instr p → state p → option (bool × (store_type p))
137; eval_call : signature p p → act_params_type p → store_type p → option (store_type p)
138; eval_after_return : return_type p → store_type p → option (store_type p)
139; init_store : store_type p
140}.
141
142
143let rec lookup (p : env_params) (p' : instr_params) (l : list (env_item p p'))
144 on l : FunctionName → option (env_item p p') ≝
145match l with
146[ nil ⇒ λ_.None ?
147| cons x xs ⇒  λf.if (eq_function_name f (f_name … x))
148                  then Some ? x
149                  else lookup … xs f
150].
151
152definition is_ret_act : ActionLabel → Prop ≝
153λa.match a with [ret_act _ ⇒ True | _ ⇒ False ].
154
155inductive execute_l (p : state_params) (p' : sem_state_params p) (env : list (env_item p p)) :
156                                         ActionLabel → relation (state p) ≝
157| empty : ∀st,st',hd,tl.(code ? st) = (EMPTY p)→ (cont ? st) = hd :: tl →
158           (code ? st') = \snd hd → (cont … st') = tl → (store … st) = (store … st') →
159           (io_info … st = true → is_non_silent_cost_act (\fst hd)) →  (io_info … st') = false → ¬ is_ret_act (\fst hd) →  execute_l … (\fst hd) st st'
160| seq_sil : ∀st,st',i,cd,s,opt_l.(code ? st) = SEQ … i opt_l cd →
161             eval_seq … p' i st = return s → (code ? st') = cd →
162             (cont … st) = (cont … st') → (store … st') = s →
163             io_info … st = false →  io_info ? st' = false → execute_l … (cost_act opt_l) st st'
164| cond_true : ∀st,st',exp,ltrue,i_true,lfalse,i_false,cd,new_m.
165   (code ? st) = COND … exp ltrue i_true lfalse i_false cd → eval_cond_cond … p' exp st = return 〈true,new_m〉 →
166   cont ? st' = 〈cost_act (None ?),cd〉 ::(cont … st) → code … st' = i_true → store … st' = new_m →
167   io_info … st = false →  io_info … st' = false → execute_l … (cost_act (Some ? ltrue)) st st'
168| cond_false : ∀st,st',exp,ltrue,i_true,lfalse,i_false,cd,new_m.
169   (code ? st) = COND … exp ltrue i_true lfalse i_false cd → eval_cond_cond … p' exp st = return 〈false,new_m〉 →
170   cont ? st' = 〈cost_act (None ?),cd〉 ::(cont … st) → code … st' = i_false → store … st' = new_m →
171   io_info … st = false →  io_info … st' = false → execute_l … (cost_act (Some ? lfalse)) st st'
172| loop_true : ∀st,st',exp,ltrue,i_true,lfalse,i_false,new_m.
173   code ? st = LOOP … exp ltrue i_true lfalse i_false → eval_loop_cond … p' exp st = return 〈true,new_m〉 →
174   cont ? st' = 〈cost_act (None ?),LOOP … exp ltrue i_true lfalse i_false〉 :: (cont … st) →
175   code … st' = i_true → store … st' = new_m → io_info … st = false →  io_info … st' = false →
176   execute_l … (cost_act (Some ? ltrue)) st st'
177| loop_false : ∀st,st',exp,ltrue,i_true,lfalse,i_false,new_m.
178   code ? st = LOOP … exp ltrue i_true lfalse i_false → eval_loop_cond … p' exp st = return 〈false,new_m〉 →
179   cont ? st' = cont … st → code … st' = i_false → store … st = store … st' →
180   io_info … st = false →  io_info … st' = false → execute_l … (cost_act (Some ? lfalse)) st st'
181| io_in : ∀st,st',lin,io,lout,cd,mem.(code ? st) = IO … lin io lout cd →
182    eval_io … p' io st = return mem → code ? st' = EMPTY p →
183    cont … st' = 〈cost_act (Some ? lout),cd〉 :: (cont … st) → store … st' = mem →
184    io_info … st' = true → execute_l … (cost_act (Some ? lin)) st st'
185| call : ∀st,st',f,act_p,r_lb,cd,mem,env_it.(code ? st) = CALL … f act_p r_lb cd →
186    lookup … env f = return env_it →
187    eval_call ? p' env_it act_p (store … st) = return mem →
188    store ? st' = mem → code … st' = f_body … env_it →
189     cont … st' =
190       〈(ret_act r_lb),cd〉 :: (cont … st) → 
191    io_info … st = false →  (io_info … st') = false →
192    execute_l … (call_act f (f_lab ?? env_it)) st st'
193| ret_instr : ∀st,st',r_t,mem,tl',rb,cd.code ? st = RETURN … r_t →
194   cont … st = 〈ret_act rb,cd〉 :: tl' → cont ? st' = tl' →
195   io_info … st = false →  io_info ? st' = false →
196   eval_after_return … p' r_t (store … st) = return mem → code … st' = cd →
197   store … st' = mem → execute_l … (ret_act rb) st st'.
198   
199let rec get_labels_of_code (p : instr_params) (i : Instructions p) on i : list CostLabel ≝
200match i with
201[ EMPTY ⇒ [ ]
202| RETURN x ⇒ [ ]
203| SEQ x lab instr ⇒ let ih ≝ get_labels_of_code … instr in
204  match lab with [ None ⇒ ih | Some lbl ⇒ a_non_functional_label lbl :: ih ]
205| COND x ltrue i1 lfalse i2 i3 ⇒
206   let ih3 ≝ get_labels_of_code … i3 in
207   let ih2 ≝ get_labels_of_code … i2 in
208   let ih1 ≝ get_labels_of_code … i1 in
209   ltrue :: lfalse :: (ih1 @ ih2 @ih3)
210| LOOP x ltrue i1 lfalse i2 ⇒
211   let ih2 ≝ get_labels_of_code … i2 in
212   let ih1 ≝ get_labels_of_code … i1 in
213   a_non_functional_label ltrue :: a_non_functional_label lfalse :: (ih1 @ ih2)
214| CALL f act_p r_lb i1 ⇒
215   let ih1 ≝ get_labels_of_code … i1 in
216   match r_lb with [ None ⇒ ih1 | Some lbl ⇒ a_return_post lbl :: ih1]
217| IO lin io lout i1 ⇒
218   let ih1 ≝ get_labels_of_code … i1 in
219   a_non_functional_label lin :: a_non_functional_label lout :: ih1
220].
221
222include "basics/lists/listb.ma".
223include "../src/utilities/hide.ma".
224
225let rec no_duplicates (A : DeqSet) (l : list A) on l : Prop ≝
226match l with
227[ nil ⇒ True
228| cons x xs ⇒ ¬ (bool_to_Prop (x ∈ xs)) ∧ no_duplicates … xs
229].
230
231lemma no_duplicates_append_r : ∀A : DeqSet.∀l1,l2 : list A.no_duplicates … (l1 @ l2) →
232no_duplicates … l2.
233#A #l1 elim l1 // #x #xs normalize #IH #l2 * /2/
234qed.
235
236lemma no_duplicates_append_l : ∀A : DeqSet.∀l1,l2 : list A.no_duplicates … (l1 @ l2) →
237no_duplicates … l1.
238#A #l1 elim l1 // #x #xs normalize #IH #l2 * #H1 #H2 % [2: /2/ ]
239inversion(x ∈ xs @l2) in H1; normalize [ #_ * #H @⊥ @H %] #H1 #_
240% inversion(x ∈ xs) normalize [2: //] #H3 #_ >(memb_append_l1 … H3) in H1;
241#EQ destruct(EQ)
242qed.
243   
244record Program (p : env_params) (p' : instr_params) : Type[0] ≝
245{ env : list (env_item p p')
246; main : Instructions p'
247}.
248
249
250definition no_duplicates_labels : ∀p,p'.Program p p' → Prop ≝
251λp,p',prog.
252   no_duplicates …
253    (foldr … (λitem,acc.((a_call (f_lab … item)) :: get_labels_of_code … (f_body … item)) @ acc) (get_labels_of_code … (main … prog)) (env … prog)).
254
255lemma no_duplicates_domain_of_fun:
256 ∀p,p',prog.no_duplicates_labels … prog →
257 ∀f,env_it.lookup p p' (env … prog) f = return env_it →
258 no_duplicates … (get_labels_of_code … (f_body … env_it)).
259#p #p' * #env elim env [ #main normalize #_ #f #env_it #EQ destruct(EQ)]
260#x #xs #IH #main whd in ⊢ (% → ?); whd in match (foldr ?????); #H #f #env_it
261whd in ⊢ (??%? → ?); @eq_function_name_elim normalize nodelta
262[ whd in ⊢ (? → ???% → ?); #EQ1 #EQ2 destruct(EQ1 EQ2) cases H #_ /2/ ]
263#H1 #EQenv_it @IH cases H /2/
264qed.
265
266
267definition is_synt_succ : ∀p.relation (state p) ≝ λp,s1,s2.True.
268(* bisognerebbe confrontare la coda dell'istruzione corrente e la parte di stack fino alla prima label.
269 match (code … s1) with
270 [ CALL f act_p r_lb i1 ⇒ code … s2 = i1
271 | _ ⇒ False
272 ].
273*)
274
275definition operational_semantics : ∀p : state_params.∀p'.Program p p → abstract_status ≝
276λp,p',prog.mk_abstract_status
277                (state p)
278                (execute_l ? p' (env … prog))
279                (is_synt_succ …)
280                (λs.match (code … s) with
281                    [ COND _ _ _ _ _ _ ⇒ cl_jump
282                    | LOOP _ _ _ _ _ ⇒ cl_jump
283                    | EMPTY ⇒ if io_info … s then cl_io else cl_other
284                    | _ ⇒ cl_other
285                    ])
286                (λs.match (code … s) with
287                    [CALL _ _ m _ ⇒ match m with [ Some _ ⇒ true | None ⇒ false ]
288                    | _ ⇒ false
289                    ])
290                (λs.eq_instructions … (code … s) (main … prog) ∧ isnilb … (cont … s) ∧ store … s == init_store … p' ∧ io_info … s)
291                (λs.match (cont … s) with
292                    [ nil ⇒ match (code … s) with
293                            [ EMPTY ⇒ true
294                            | RETURN _ ⇒ true
295                            | _ ⇒ false
296                            ]
297                    | _ ⇒ false
298                    ])
299                ???.
300@hide_prf
301[ #s1 #s2 #l #H #H1 inversion H1 #st #st'
302 [ #hd #tl
303 | #i #cd #s #opt_l
304 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
305 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
306 | #lin #io #lout #cd #mem
307 | #f #act_p #r_lb #cd #mem #env_it
308 | #r_t #mem #tl #rb #cd
309 ]
310 #EQcode
311 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQ5 #H2 #EQ' #EQ6 #EQ7
312 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
313 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
314 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
315 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
316 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9
317 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8
318 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
319 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
320 ]
321 #_ destruct >EQcode in H; normalize nodelta /2 by ex_intro/
322 [ cases(io_info ??) normalize nodelta] #EQ destruct
323| #s1 #s2 #l #H #H1 inversion H1 #st #st'
324 [ #hd #tl
325 | #i #cd #s #opt_l
326 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
327 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
328 | #lin #io #lout #cd #mem
329 | #f #act_p #r_lb #cd #mem #env_it
330 | #r_t #mem #tl #rb #cd
331 ]
332 #EQcode
333 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQiost' #H2 #EQ' #EQ6 #EQ7
334 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
335 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
336 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
337 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
338 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost' #EQ7 #EQ8 #EQ9
339 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost' #EQ6 #EQ7 #EQ8
340 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQ6 #EQiost' #EQ8 #EQ9 #EQ10
341 | #EQ1 #EQ2 #EQ3 #EQiost' #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
342 ]
343 #_ destruct
344 cases(code ? st') in H; normalize nodelta >EQiost' normalize nodelta
345 #eq destruct try (#eq1 destruct) try (#eq2 destruct) try (#eq3 destruct)
346 try (#eq4 destruct) try (#eq5 destruct) try (#eq6 destruct) %{lin} %
347| #s1 #s2 #l #H #H1 inversion H1 #st #st'
348 [ #hd #tl
349 | #i #cd #s #opt_l
350 |3,4: #exp #ltrue #i_true #lfalse #i_false #cd #new_m
351 |5,6: #exp #ltrue #i_true #lfalse #ifalse #new_m
352 | #lin #io #lout #cd #mem
353 | #f #act_p #r_lb #cd #mem #env_it
354 | #r_t #mem #tl #rb #cd
355 ]
356 #EQcode
357 [ #EQ1 #EQ2 #EQ3 #EQ4 #x #EQiost' #H2 #EQ' #EQ6 #EQ7
358 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
359 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
360 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
361 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
362 | #EQ1 #EQ2 #EQ3 #EQ4 #EQiost #EQiost' #EQ7 #EQ8 #EQ9
363 | #EQ1 #EQ2 #EQ3 #EQiost #EQiost' #EQ6 #EQ7 #EQ8
364 | #EQ1 #EQ2 #EQ3 #EQ4 #EQ5 #EQiost #EQiost' #EQ8 #EQ9 #EQ10
365 | #EQ1 #EQ2 #EQiost #EQiost' #EQ5 #EQ6 #EQ7 #EQ8 #EQ9 #EQ10
366 ]
367 #_ destruct >EQcode in H; normalize nodelta [|*: #EQ destruct]
368 cases(io_info … st) in x; normalize nodelta [2: #_ #EQ destruct]
369 #H3 #_ @H3 %
370]
371qed.
372
373let rec eqb_list (D : DeqSet) (l1 : list D) on l1 : list D → bool ≝
374match l1 with
375[ nil ⇒ λl2.match l2 with [nil ⇒ true | _ ⇒ false ]
376| cons x xs ⇒ λl2.match l2 with [ cons y ys ⇒ x == y ∧ eqb_list … xs ys | _ ⇒ false ]
377].
378
379definition DeqSet_List : DeqSet → DeqSet ≝
380λX.mk_DeqSet (list X) (eqb_list …) ?.
381#x elim x [ * /2 by refl, conj/ #y #ys normalize % #EQ destruct] -x
382#x #xs #IH * [ normalize % #EQ destruct] #y #ys normalize % inversion(x == y)
383#EQ normalize nodelta
384[ #H >(proj1 … (IH ys) H) @eq_f2 [2: %] @(proj1 … (eqb_true …)) assumption
385| #EQ destruct
386| #EQ1 destruct @(proj2 … (IH …)) %
387| #EQ1 destruct <EQ @(proj2 … (eqb_true …)) %
388]
389qed.
390
391unification hint  0 ≔ C;
392    X ≟ DeqSet_List C
393(* ---------------------------------------- *) ⊢
394    list C ≡ carr X.
395
396
397unification hint  0 ≔ D,p1,p2;
398    X ≟ DeqSet_List D
399(* ---------------------------------------- *) ⊢
400    eqb_list D p1 p2 ≡ eqb X p1 p2.
401
402definition associative_list : DeqSet → Type[0] → Type[0] ≝
403λA,B.list (A × (list B)).
404
405let rec update_list (A : DeqSet) (B : Type[0]) (l : associative_list A B)
406   on l : A → list B → associative_list A B ≝
407λa,b.match l with
408    [ nil ⇒ [〈a,b〉]
409    | cons x xs ⇒ if (a == (\fst x)) then 〈a,b〉 :: xs
410                  else x :: (update_list … xs a b) 
411    ].
412
413let rec get_element (A :DeqSet) (B : Type[0]) (l: associative_list A B) on l : A → list B ≝
414λa.match l with [ nil ⇒ nil ?
415                | cons x xs ⇒ if (a == \fst x) then \snd x else get_element … xs a
416                ].
417
418let rec domain_of_associative_list (A :DeqSet) (B : Type[0]) (l: associative_list A B) on l : list A ≝
419 match l with
420  [ nil ⇒ []
421  | cons x xs ⇒ \fst x :: domain_of_associative_list … xs
422  ].
423
424lemma get_element_append_l:
425 ∀A,B. ∀l1,l2: associative_list A B. ∀x.
426  x ∈ domain_of_associative_list … l1 →
427   get_element … (l1@l2) x = get_element … l1 x.
428#A #B #l1 elim l1 normalize [ #l2 #x * ] #hd #tl #IH #l2 #x cases (dec_eq … x (\fst hd))
429#H [ >(\b H) | >(\bf H) ] normalize /2/
430qed.
431
432lemma get_element_append_r:
433 ∀A,B. ∀l1,l2: associative_list A B. ∀x.
434  ¬ (bool_to_Prop (x ∈ domain_of_associative_list … l1)) →
435   get_element ?? (l1@l2) x = get_element … l2 x.
436#A #B #l1 elim l1 normalize [ #l2 #x // ] #hd #tl #IH #l2 #x cases (dec_eq … x (\fst hd))
437#H [ >(\b H) | >(\bf H) ] normalize /2 by/ * #K cases (K I)
438qed.
439
440definition fresh_nf_label : ℕ → NonFunctionalLabel × ℕ ≝
441λx.〈a_non_functional_label x,S x〉.
442
443definition fresh_cc_labe : ℕ → CallCostLabel × ℕ ≝
444λx.〈a_call_label x,S x〉.
445
446definition fresh_rc_label : ℕ → ReturnPostCostLabel × ℕ ≝
447λx.〈a_return_cost_label x,S x〉.
448
449record call_post_info (p : instr_params) : Type[0] ≝
450{ gen_labels : list CostLabel
451; t_code : Instructions p
452; fresh : ℕ
453; lab_map : associative_list DEQCostLabel CostLabel
454; lab_to_keep : list CostLabel
455}.
456
457let rec call_post_trans (p : instr_params) (i : Instructions p) (n : ℕ) on i :
458list CostLabel → call_post_info p ≝
459λabs.
460match i with
461[ EMPTY ⇒ mk_call_post_info ? abs (EMPTY …) n (nil ?) (nil ?)
462| RETURN x ⇒ mk_call_post_info ? abs (RETURN … x) n (nil ?) (nil ?)
463| SEQ x lab instr ⇒
464   let ih ≝ call_post_trans … instr n abs in
465   match lab with
466   [ None ⇒ mk_call_post_info ? (gen_labels … ih) (SEQ … x (None ?) (t_code … ih))
467             (fresh … ih) (lab_map … ih) (lab_to_keep … ih)
468   | Some lbl ⇒
469      mk_call_post_info ? (nil ?) (SEQ … x (Some ? lbl) (t_code …  ih)) (fresh … ih)
470      (〈a_non_functional_label lbl,(a_non_functional_label lbl :: (gen_labels … ih))〉 :: (lab_map … ih))
471      (lab_to_keep … ih)
472   ]
473| COND x ltrue i1 lfalse i2 i3 ⇒
474   let ih3 ≝ call_post_trans … i3 n abs in
475   let ih2 ≝ call_post_trans … i2 (fresh … ih3) (gen_labels … ih3) in
476   let ih1 ≝ call_post_trans … i1 (fresh … ih2) (gen_labels … ih3) in
477   mk_call_post_info ? (nil ?) (COND … x ltrue (t_code … ih1) lfalse (t_code … ih2) (t_code … ih3))
478    (fresh … ih1) 
479    (〈a_non_functional_label ltrue,(a_non_functional_label ltrue :: (gen_labels … ih1))〉::
480      〈a_non_functional_label lfalse,(a_non_functional_label lfalse :: (gen_labels … ih2))〉::
481       ((lab_map … ih1) @ (lab_map …  ih2) @ (lab_map … ih3)))
482    ((lab_to_keep … ih1) @ (lab_to_keep … ih2) @ (lab_to_keep … ih3))
483| LOOP x ltrue i1 lfalse i2 ⇒
484   let ih2 ≝ call_post_trans … i2 n abs in
485   let ih1 ≝ call_post_trans … i1 (fresh … ih2) (nil ?) in
486   mk_call_post_info ? (nil ?) (LOOP … x ltrue (t_code … ih1) lfalse (t_code … ih2)) (fresh … ih1)
487    (〈a_non_functional_label lfalse,(a_non_functional_label lfalse :: (gen_labels … ih2))〉 ::
488     〈a_non_functional_label ltrue,(a_non_functional_label ltrue :: (gen_labels … ih1))〉 ::
489      ((lab_map … ih1) @ (lab_map … ih2)))
490    ((lab_to_keep … ih1) @ (lab_to_keep … ih2))
491| CALL f act_p r_lb i1 ⇒
492   let ih ≝ call_post_trans … i1 n abs in
493   match r_lb with
494   [ None ⇒ let 〈l',f''〉 ≝ fresh_rc_label (fresh … ih) in
495       mk_call_post_info ? ((a_return_post l')::(gen_labels … ih))
496         (CALL … f act_p (Some ? l') (t_code … ih))  f'' (lab_map … ih) (lab_to_keep … ih)
497   | Some lbl ⇒
498      mk_call_post_info ? (nil ?) (CALL ? f act_p (Some ? lbl) (t_code … ih)) (fresh … ih)
499       (〈a_return_post lbl,(a_return_post lbl :: (gen_labels … ih))〉 :: (lab_map … ih))
500       (a_return_post lbl :: lab_to_keep … ih)
501   ]
502| IO lin io lout i1 ⇒
503    let ih ≝ call_post_trans … i1 n abs in
504    mk_call_post_info ? (nil ?) (IO ? lin io lout (t_code … ih)) (fresh … ih)
505     (〈a_non_functional_label lout,(a_non_functional_label lout :: (gen_labels … ih))〉 ::
506      〈a_non_functional_label lin,[a_non_functional_label lin]〉 :: (lab_map … ih)) (lab_to_keep … ih)
507].
508
509
510let rec call_post_clean (p : instr_params) (i : Instructions p) on i :
511associative_list DEQCostLabel CostLabel → list CostLabel → list CostLabel →
512option ((list CostLabel) × (Instructions p)) ≝
513λm,keep,abs.
514 match i with
515[ EMPTY ⇒ Some ? 〈abs,EMPTY …〉
516| RETURN x ⇒ Some ? 〈abs,RETURN … x〉
517| SEQ x lab instr ⇒
518   ! 〈l,i1〉 ← call_post_clean … instr m keep abs;
519   match lab with
520   [ None ⇒ return 〈l,SEQ … x (None ?) i1〉
521   | Some lbl ⇒ if ((get_element … m lbl) == lbl :: l)
522                then return 〈nil ?,SEQ … x (Some ? lbl) i1〉
523                else None ?
524   ]
525| COND x ltrue i1 lfalse i2 i3 ⇒
526    ! 〈l3,instr3〉 ← call_post_clean … i3 m keep abs;
527    ! 〈l2,instr2〉 ← call_post_clean … i2 m keep l3;
528    ! 〈l1,instr1〉 ← call_post_clean … i1 m keep l3;
529    if ((get_element … m ltrue) == ltrue :: l1) ∧
530       ((get_element … m lfalse) == lfalse :: l2)
531    then return 〈nil ?,COND … x ltrue instr1 lfalse instr2 instr3〉
532    else None ?
533| LOOP x ltrue i1 lfalse i2 ⇒
534   ! 〈l2,instr2〉 ← call_post_clean … i2 m keep abs;
535   ! 〈l1,instr1〉 ← call_post_clean … i1 m keep (nil ?);
536   if ((get_element … m ltrue) == ltrue :: l1) ∧
537      ((get_element … m lfalse) == lfalse :: l2)
538   then return 〈nil ?,LOOP … x ltrue instr1 lfalse instr2〉
539   else None ?
540| CALL f act_p r_lb i1 ⇒
541  ! 〈l1,instr1〉 ← call_post_clean … i1 m keep abs;
542  match r_lb with
543  [ None ⇒ return 〈l1,CALL … f act_p (None ?) instr1〉
544  | Some lbl ⇒ if ((a_return_post lbl ∈ keep))
545               then if ((get_element … m lbl) == lbl :: l1)
546                    then return 〈nil ?,CALL … f act_p (Some ? lbl) instr1〉
547                    else None ?
548               else return 〈(a_return_post lbl) :: l1,CALL … f act_p (None ?) instr1〉
549  ]
550| IO lin io lout i1 ⇒
551   ! 〈l1,instr1〉 ← call_post_clean … i1 m keep abs;
552   if ((get_element … m lout) == lout :: l1) ∧ ((get_element … m lin) == [lin])
553   then return 〈nil ?,IO … lin io lout instr1〉
554   else None ?   
555].
556
557let rec foldr2 (A : Type[0]) (B : Type[0]) (C : Type[0]) (a : A) (l1 : list B)
558(l2 : list C) (f : A → B → C → A) on l1 : option A≝
559match l1 with
560[ nil ⇒ match l2 with [ nil ⇒ return a | cons y ys ⇒ None ? ]
561| cons x xs ⇒
562        match l2 with
563        [ nil ⇒ None ?
564        | cons y ys ⇒ ! ih ← (foldr2 … a xs ys f);
565                      return f ih x y
566        ]
567].
568
569definition is_silent_cost_act_b : ActionLabel → bool ≝
570λact. match act with
571 [ cost_act x ⇒ match x with [None ⇒ true | _ ⇒ false ] | _ ⇒ false].
572
573definition eq_ActionLabel : ActionLabel → ActionLabel → bool ≝
574λc1,c2.
575match c1 with
576[ call_act f l ⇒ match c2 with [ call_act f' l' ⇒ f == f' ∧ l == l' | _ ⇒ false]
577| ret_act x ⇒ match c2 with [ret_act y ⇒ match x with [ None ⇒ match y with [ None ⇒ true | _ ⇒ false]
578                                                      | Some l ⇒ match y with [ Some l' ⇒ l == l' | _ ⇒ false]
579                                                      ]
580                            | _ ⇒ false
581                            ]
582| cost_act x ⇒ match c2 with [cost_act y ⇒ match x with [ None ⇒ match y with [ None ⇒ true | _ ⇒ false]
583                                                        | Some l ⇒ match y with [ Some l' ⇒ l == l' | _ ⇒ false]
584                                                        ]
585                             | _ ⇒ false
586                             ]
587| init_act ⇒ match c2 with [init_act ⇒ true | _ ⇒ false]
588].
589
590definition ret_costed_abs : list CostLabel → option ReturnPostCostLabel → option CostLabel ≝
591λkeep,x.
592 match x with
593              [ Some lbl ⇒ if (a_return_post lbl) ∈ keep then return (a_return_post lbl)
594                           else None ?
595              | None ⇒ None ?
596              ].
597
598
599definition check_continuations : ∀p : instr_params.
600∀l1,l2 : list (ActionLabel × (Instructions p)).
601associative_list DEQCostLabel CostLabel →
602list CostLabel →  option (Prop × (list CostLabel) × (list CostLabel)) ≝
603λp,cont1,cont2,m,keep.
604foldr2 ??? 〈True,nil ?,nil ?〉 cont1 cont2
605 (λx,y,z.
606   let 〈cond,abs_top',abs_tail'〉 ≝ x in
607   match call_post_clean p (\snd z) m keep abs_top' with
608   [ None ⇒ 〈False,nil ?,nil ?〉
609   | Some w ⇒
610      match \fst z with
611       [ ret_act opt_x ⇒
612           match ret_costed_abs keep opt_x with
613           [ Some lbl ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
614                               get_element … m lbl = lbl :: (\fst w),(nil ?),abs_tail'〉
615           | None ⇒
616              〈\fst y = ret_act (None ?) ∧ cond ∧ \snd w = \snd y,(nil ?),(\fst w) @ abs_tail'〉
617           ]
618       | cost_act opt_x ⇒
619           match opt_x with
620           [ None ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y,\fst w,abs_tail'〉
621           | Some xx ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
622                               get_element … m xx = xx :: (\fst w),(nil ?),abs_tail'〉]
623       | _ ⇒ (* dummy *) 〈False,nil ?,nil ?〉]]).
624
625(*
626definition check_continuations : ∀p : instr_params.
627∀l1,l2 : list (ActionLabel × (Instructions p)).
628associative_list DEQCostLabel CostLabel → list CostLabel → list CostLabel →
629list (list CostLabel) →  option (Prop × (list CostLabel) × (list (list CostLabel))) ≝
630λp,cont1,cont2,m,keep,abs_top,abs_tail.
631foldr2 ??? 〈True,abs_top,abs_tail〉 cont1 cont2
632 (λx,y,z.
633   let 〈cond,abs_top',abs_tail'〉 ≝ x in
634   match call_post_clean p (\snd z) m keep abs_top' with
635   [ None ⇒ 〈False,nil ?,nil ?〉
636   | Some w ⇒
637      match \fst z with
638       [ ret_act opt_x ⇒
639           match ret_costed_abs keep opt_x with
640           [ Some lbl ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
641                               get_element … m lbl = lbl :: (\fst w),(nil ?),(nil ?) :: abs_tail'〉
642           | None ⇒
643              〈\fst y = ret_act (None ?) ∧ cond ∧ \snd w = \snd y,(nil ?),(\fst w) :: abs_tail'〉
644           ]
645       | cost_act opt_x ⇒
646           match opt_x with
647           [ None ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y,\fst w,abs_tail'〉
648           | Some xx ⇒ 〈\fst y = \fst z ∧ cond ∧ \snd w = \snd y ∧
649                               get_element … m xx = xx :: (\fst w),(nil ?),abs_tail'〉]
650       | _ ⇒ (* dummy *) 〈False,nil ?,nil ?〉]]). *)
651(* in input :
652     abs_top is the list of labels to be propageted to the deepest level of the call stack
653     abs_tail are the lists of labels to be propagated to the levels "below" the deepest level
654   in output :
655     abs_top is the list of labels to be propageted from the current level of the call stack
656     abs_tail are the lists of labels to be propagated from the levels "below" the current level
657  *)       
658       
659
660definition state_rel : ∀p.
661associative_list DEQCostLabel CostLabel → list CostLabel → list CostLabel →(list CostLabel) →
662relation (state p) ≝ λp,m,keep,abs_top,abs_tail,st1,st2.
663match check_continuations ? (cont ? st1) (cont … st2) m keep with
664[ Some x ⇒ let 〈prf1,abs_top',abs_tail'〉 ≝ x in
665           prf1 ∧ call_post_clean … (code … st2) m keep abs_top' = return 〈abs_top,(code … st1)〉
666           ∧ store … st1 = store … st2 ∧ io_info … st1 = io_info … st2 ∧ abs_tail = abs_tail'
667| None ⇒ False
668].
669
670include "Simulation.ma".
671
672let rec len (S : abstract_status) (st1 : S) (st2 : S) (t : raw_trace S st1 st2)
673on t : ℕ ≝
674match t with
675[ t_base s ⇒ O
676| t_ind s1 s2 s3 l prf lt ⇒ (len … lt) + 1
677].
678(*
679lemma associative_max : ∀n1,n2,n3.max (max n1 n2) n3 = max n1 (max n2 n3).
680#n1 #n2 #n3 normalize @leb_elim normalize
681[ @leb_elim normalize
682  [ #H1 #H2 @leb_elim normalize
683    [ @leb_elim normalize // * #H @⊥ @H assumption 
684    | @leb_elim normalize
685      [ #H3 * #H @⊥ @H @(transitive_le … H1) assumption
686      | * #H @⊥ @H assumption
687      ]
688    ]
689  | #H1 #H2 @leb_elim normalize
690    [ @leb_elim normalize // #_ #H cases H1 #H1 @⊥ @H1 assumption
691    | @leb_elim normalize
692      [ #_ * #H @⊥ @H assumption
693      | * #H @⊥ @H @(transitive_le … H2)
694*)
695let rec compute_max_n (p : instr_params) (i : Instructions p) on i : ℕ ≝
696match i with
697[ EMPTY ⇒ O
698| RETURN x ⇒ O
699| SEQ x lab instr ⇒ let n ≝ compute_max_n … instr in
700                    match lab with
701                    [ None ⇒ n
702                    | Some l ⇒
703                        match l with
704                        [ a_non_functional_label n' ⇒ S (max n n') ]
705                    ]
706| COND x ltrue i1 lfalse i2 i3 ⇒
707  let n1 ≝ compute_max_n … i1 in
708  let n2 ≝ compute_max_n … i2 in
709  let n3 ≝ compute_max_n … i3 in
710  let mx ≝ max (max n1 n2) n3 in
711  match ltrue with
712  [ a_non_functional_label lt ⇒
713    match lfalse with
714    [a_non_functional_label lf ⇒ S (max (max mx lt) lf) ] ]
715| LOOP x ltrue i1 lfalse i2 ⇒
716   let n1 ≝ compute_max_n … i1 in
717   let n2 ≝ compute_max_n … i2 in
718   let mx ≝ max n1 n2 in
719   match ltrue with
720  [ a_non_functional_label lt ⇒
721    match lfalse with
722    [a_non_functional_label lf ⇒ S (max (max mx lt) lf) ] ]
723| CALL f act_p r_lb i1 ⇒
724   let n ≝ compute_max_n … i1 in
725   match r_lb with
726   [ None ⇒ n
727   | Some lbl ⇒ match lbl with [a_return_cost_label l ⇒ S(max l n) ]
728   ]
729| IO lin io lout i1 ⇒
730  let n ≝ compute_max_n … i1 in
731  match lin with
732  [a_non_functional_label l1 ⇒
733    match lout with
734    [a_non_functional_label l2 ⇒ S(max (max n l1) l2) ] ]
735].
736
737
738definition same_fresh_map_on : list CostLabel →
739relation (associative_list DEQCostLabel CostLabel) ≝
740λdom,m1,m2.∀x.bool_to_Prop (x ∈ dom) → get_element … m1 x = get_element … m2 x.
741
742definition same_to_keep_on : list CostLabel → relation (list CostLabel) ≝
743λdom,keep1,keep2.∀x. bool_to_Prop (x ∈ dom) → (x ∈ keep1) = (x ∈ keep2).
744
745lemma memb_not_append : ∀D : DeqSet.∀l1,l2 : list D.∀x : D.
746x ∈ l1 = false → x ∈ l2 = false → x ∈ (l1 @ l2) = false.
747#D #l1 elim l1
748[ #l2 #x #_ #H @H ]
749#x #xs #IH #l2 #x1 whd in match (memb ???); inversion (x1 == x) normalize nodelta
750[ #_ #EQ destruct] #EQx1 #EQxs #EQl2 whd in match (memb ???); >EQx1
751normalize nodelta @IH //
752qed.
753
754lemma lab_to_keep_in_domain : ∀p.∀i : Instructions p.
755∀x,n,l.
756x ∈ lab_to_keep … (call_post_trans … i n l) → x ∈ get_labels_of_code …  i.
757#p #i elim i //
758[ #seq #opt_l #instr #IH #x #n #l whd in match (call_post_trans ????);
759  cases opt_l -opt_l normalize nodelta [|#lbl]
760  whd in match (get_labels_of_code ??); #H [2: @orb_Prop_r] /2/
761| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #x #n #l
762  whd in match (call_post_trans ????); whd in match (get_labels_of_code ??);
763  #H cases(memb_append … H) -H #H @orb_Prop_r @orb_Prop_r
764  [ >memb_append_l1 // @IH1 [3: >H // |*: ]
765  | >memb_append_l2 // cases(memb_append … H) -H #H
766     [>memb_append_l1 // @IH2 [3: >H // |*: ]
767     | >memb_append_l2 // @IH3 [3: >H // |*: ]
768     ]
769  ]
770| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #x #n #l
771  whd in match (call_post_trans ????); whd in match (get_labels_of_code ??);
772  #H cases(memb_append … H) -H #H @orb_Prop_r @orb_Prop_r
773  [ >memb_append_l1 | >memb_append_l2 ] // [ @IH1 | @IH2 ] [3,6: >H |*: ] //
774| #f #act_p * [|#lbl] #i1 #IH #x #n #l whd in match (call_post_trans ????);
775  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???);
776  inversion(x == lbl) #Hlbl normalize nodelta
777  [*  >(\P Hlbl) @orb_Prop_l @eq_costlabel_elim // * #H @H %
778  | #H @orb_Prop_r @IH //
779  ]
780| #lin #io #lout #i1 #IH #x #n #l whd in match (call_post_trans ????);
781  whd in match (get_labels_of_code ??); #H @orb_Prop_r @orb_Prop_r @IH //
782]
783qed.
784
785lemma domain_of_associative_list_append : ∀A,B.∀l1,l2 : associative_list A B.
786domain_of_associative_list ?? (l1 @ l2) =
787 (domain_of_associative_list ?? l1) @ (domain_of_associative_list ?? l2).
788#A #B #l1 elim l1 // #x #xs #IH #l2 normalize //
789qed.
790
791lemma lab_map_in_domain: ∀p.∀i: Instructions p.
792 ∀x,n,l.
793  x ∈ domain_of_associative_list … (lab_map p (call_post_trans … i n l)) →
794   x ∈ get_labels_of_code … i.
795#p #i elim i //
796[ #seq * [|#lbl] #i1 #IH #x #n #l whd in match(call_post_trans ????);
797  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???);
798  inversion(x==lbl) #Hlbl normalize nodelta [#_ whd in match (memb ???); >Hlbl % ]
799  #H >memb_cons // @IH //
800| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #x #n #l
801  whd in match (call_post_trans ????); whd in match (memb ???);
802  whd in match (get_labels_of_code ??); inversion(x == ltrue) #Hlbl normalize nodelta
803  [ #_ whd in match (memb ???); >Hlbl % ] whd in match (memb ???);
804  inversion(x == lfalse) #Hlbl1 normalize nodelta
805  [ #_ whd in match (memb ???); >Hlbl normalize nodelta whd in match (memb ???);
806    >Hlbl1 % ] #H >memb_cons // >memb_cons // >domain_of_associative_list_append in H;
807    #H cases(memb_append … H) [ #H1 >memb_append_l1 // @IH1 [3: >H1 // |*:] ]
808    >domain_of_associative_list_append #H1 cases(memb_append … H1)
809    #H2 >memb_append_l2 // [ >memb_append_l1 | >memb_append_l2 ] //
810    [ @IH2 | @IH3] /2 by eq_true_to_b/
811| #loop #ltrue #i1 #lfalse #i2 #IH1 #IH2 #x #n #l whd in match (call_post_trans ????);
812  whd in match (get_labels_of_code ??); whd in match (memb ???); inversion(x == lfalse)
813  #Hlfalse normalize nodelta [ #_ >memb_cons // whd in match (memb ???); >Hlfalse // ]
814  whd in match (memb ???); inversion(x==ltrue) normalize nodelta #Hltrue
815  [ #_ whd in match (memb ???); >Hltrue %] >domain_of_associative_list_append #H
816  cases(memb_append … H) #H1 >memb_cons // >memb_cons // [ >memb_append_l1 | >memb_append_l2 ]
817  // [ @IH1 | @IH2] /2/
818| #f #act_p * [|#lbl] #i1 #IH #x #n #l whd in match (call_post_trans ????);
819  whd in match (get_labels_of_code ??); /2/ whd in match (memb ???); inversion (x == lbl)
820  #Hlbl normalize nodelta [ #_ whd in match (memb ???); >Hlbl % ] #H >memb_cons // @IH /2/
821| #lin #io #lout #i1 #IH #x #n #l whd in match (memb ???); inversion(x == lout) #Hlout
822  normalize nodelta [ #_ >memb_cons // whd in match (memb ???); >Hlout % ]
823  whd in match (memb ???); inversion(x==lin) #Hlin normalize nodelta
824  [ #_ whd in match (memb ???); >Hlin % ] #H >memb_cons // >memb_cons // @IH /2/
825]
826qed.
827
828lemma memb_no_duplicates_append : ∀A : DeqSet.∀x.∀l1,l2 : list A .
829no_duplicates … (l1 @ l2) → x ∈  l1 → x ∈ l2 → False.
830#A #x #l1 elim l1 // #x1 #xs #IH #l2 * #H1 #H2 whd in match (memb ???);
831inversion (x == x1) normalize nodelta
832[ #H3 #_ #H4 >memb_append_l2 in H1; [2: <(\P H3) @H4 ] * #H @H %
833| #_ @IH //
834]
835qed.
836 
837lemma inverse_call_post_trans : ∀p : instr_params.∀i1 : Instructions p.∀n : ℕ.
838let dom ≝ get_labels_of_code … i1 in
839no_duplicates … dom →
840∀m,keep.
841∀info,l.call_post_trans p i1 n l = info →
842same_fresh_map_on dom (lab_map … info) m →
843same_to_keep_on dom (lab_to_keep … info) keep →
844call_post_clean … (t_code … info) m keep l
845 = return 〈gen_labels … info,i1〉.
846#p #i1 elim i1
847[ #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
848  #EQ destruct(EQ) //
849| #x #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
850  #EQ destruct(EQ) //
851| #seq * [|#lbl] #instr #IH #n #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep
852  #l whd in ⊢ (??%? → ?); #EQ destruct(EQ) #H1 #H2 whd in ⊢ (??%%); normalize nodelta
853  >IH //
854  [1,4: whd whd in H2; #x #Hx @H2 whd in match (get_labels_of_code ??); //
855        @orb_Prop_r //
856  |2,5: whd whd in H1; #x #Hx [ @H1 // ] cases no_dup #H3 #_ <H1
857       [2: whd in match (get_labels_of_code ??); @orb_Prop_r // ]
858       whd in ⊢ (???%); cases(eqb_true … x lbl) inversion(x == lbl) normalize nodelta
859       [2: //] #_ #H4 >H4 in Hx; // #H5 >H5 in H3; * #ABS @⊥ @ABS %
860  |6: cases no_dup //
861  ]
862  normalize nodelta <(H1 lbl)
863  [2: whd in match (get_labels_of_code ??); @orb_Prop_l cases(eqb_true … (a_non_functional_label lbl) lbl)
864      #H3 #H4 >H4 % ]
865  whd in match (get_element ????); >(\b (refl …)) normalize nodelta
866  >(\b (refl …)) %
867| #cond #ltrue #i1 #lfalse #i2 #i3 #IH1 #IH2 #IH3 #n normalize nodelta
868  #no_dup #m #keep * #gen_lab #t_code #fresh #lab_map #lab_keep #l whd in ⊢ (??%? → ?);
869  #EQ destruct(EQ) #H1 #H2 whd in ⊢ (??%?); >IH3 //
870  [2: #x #Hx whd in H2; inversion(x ∈ lab_to_keep … (call_post_trans …))
871    [ #EQkeep <H2 [ >memb_append_l2 // >memb_append_l2 // ]
872      whd in match (get_labels_of_code ??); @orb_Prop_r @orb_Prop_r >memb_append_l2 //
873      >memb_append_l2 // >Hx %
874    | #EQno_keep <H2
875      [2: whd in match (get_labels_of_code ??); @orb_Prop_r @orb_Prop_r
876          >memb_append_l2 // >memb_append_l2 // >Hx % ] @sym_eq @memb_not_append
877         [| @memb_not_append //] inversion(x ∈ lab_to_keep ??) // #Hlab @⊥
878         lapply(lab_to_keep_in_domain ????? (eq_true_to_b … Hlab)) #ABS     
879         cases no_dup #_ * #_ #ABS1
880         [ @(memb_no_duplicates_append … x … ABS1) // >memb_append_l2 //
881         | <associative_append in ABS1; #ABS1       
882           @(memb_no_duplicates_append … x … ABS1) // >memb_append_l2 // >ABS %
883         ]
884         >Hx %
885    ]
886  |3: whd in H1; #x #Hx <H1
887    [2: >memb_cons // >memb_cons // >memb_append_l2 // >memb_append_l2 // >Hx // ]
888    whd in ⊢ (???%); >(\bf ?) [ whd in ⊢ (???%); >(\bf ?) [ whd in ⊢ (???%);
889    >get_element_append_r [ >get_element_append_r // % #K lapply (lab_map_in_domain … K)
890    #K2 cases no_dup; #_ * #_ #no_dup3
891    @(memb_no_duplicates_append … x … (no_duplicates_append_r … no_dup3)) // ]
892    % #K lapply (lab_map_in_domain … K) #K2 cases no_dup; #_ * #_ #dup3
893    @(memb_no_duplicates_append … x … dup3) // >memb_append_l2 // >Hx // ]
894    cases no_dup #_ * #A1 #_ % #A2 <A2 in A1; * #K @K >memb_append_l2 //
895    >memb_append_l2 // @Hx ] cases no_dup #A1 #_ % #A2 <A2 in A1; * #K @K
896    >memb_cons // >memb_append_l2 // >memb_append_l2 // @Hx
897  |4: cases no_dup #_ * #_ #H @no_duplicates_append_r [2: @(no_duplicates_append_r … H) |]
898  ]
899  normalize nodelta >IH2 //
900  [2: #x #Hx <H2
901    [2: >memb_cons // >memb_cons // >memb_append_l2 // >memb_append_l1 // >Hx // ]
902    whd in ⊢ (???%); >memb_append_l2 [2: >memb_append_l1] //
903    cases daemon (*CSC: strange goal here; do we need a lemma? but is the goal true?*)
904  |3: (*no duplicates ok *) cases daemon
905  |4: cases no_dup #_ * #_ #K lapply (no_duplicates_append_r … K) @no_duplicates_append_l
906  ]
907  >m_return_bind >IH1 //
908  [2: #x #Hx <H2
909    [2: >memb_cons // >memb_cons // >memb_append_l1 // @Hx ]
910    >memb_append_l1
911    cases daemon (*CSC: strange goal here; do we need a lemma? but is the goal true?*) 
912  |3: cases daemon (*come sopra*)
913  |4: cases no_dup #_ * #_ @no_duplicates_append_l
914  ]
915  >m_return_bind normalize nodelta whd in H1; <H1
916  [2: whd in match (get_labels_of_code ??); whd in match (memb ???);
917      >(\b (refl …)) % ] whd in match (get_element ????); >(\b (refl …))
918      normalize nodelta >(\b (refl …)) <H1
919      [2: whd in match (get_labels_of_code ??); >memb_cons //
920      whd in match (memb ???); >(\b (refl …)) % ]
921      whd in match (get_element ????); @eq_costlabel_elim normalize nodelta
922      [ #ABS cases daemon (*CSC: FALSO!! *) ] #_ whd in match (get_element ????);
923      >(\b (refl …)) normalize nodelta >(\b (refl …)) %
924|*: cases daemon (*TODO*)
925]
926qed.
927
928
929definition trans_prog : ∀p,p'.Program p p' →
930(Program p p') × (associative_list DEQCostLabel CostLabel) × (list CostLabel)≝
931λp,p',prog.
932let max_all ≝ foldl … (λn,i.max n (compute_max_n … (f_body … i))) O (env … prog) in
933let 〈t_env,n,m,keep〉 ≝ (foldr ??
934           (λi,x.let 〈t_env,n,m,keep〉 ≝ x in
935           let info ≝ call_post_trans … (f_body … i) n (nil ?) in
936                   〈(mk_env_item ??
937                       (mk_signature ??(f_name ?? i) (f_pars … i) (f_ret … i))
938                       (f_lab … i) (t_code … info)) :: t_env,
939                     fresh … info, 〈a_call (f_lab … i),(a_call (f_lab … i)) :: (gen_labels ? info)〉 ::
940                                     ((lab_map … info) @ m),(lab_to_keep … info) @ keep〉)
941          (〈nil ?,max_all,nil ?,nil ?〉) (env … prog)) in
942〈mk_Program ?? t_env (t_code … (call_post_trans … (main … prog) n (nil ?))),m,keep〉 .
943
944definition map_labels_on_trace :
945(associative_list DEQCostLabel CostLabel) → list CostLabel → list CostLabel ≝
946λm,l.foldr … (λlab,t.(get_element … m lab) @ t) (nil ?) l.
947
948lemma map_labels_on_trace_append:
949 ∀m,l1,l2. map_labels_on_trace m (l1@l2) =
950  map_labels_on_trace m l1 @ map_labels_on_trace m l2.
951 #m #l1 elim l1 // #hd #tl #IH #l2 >associative_append <IH //
952qed.
953
954include "../src/common/Errors.ma".
955
956axiom is_permutation: ∀A.list A → list A → Prop.
957axiom is_permutation_eq : ∀A.∀l : list A.is_permutation … l l.
958axiom is_permutation_cons : ∀A.∀l1,l2,x.is_permutation A l1 l2 →
959                                       is_permutation A (x :: l1) (x :: l2).
960
961(*
962inductive is_permutation (A : Type[0]) : list A → list A → Prop ≝
963| p_empty : is_permutation A (nil ?) (nil ?)
964| p_append : ∀x,x1,x2,y,y1,y2.
965               x = y → is_permutation A (x1 @ x2) (y1 @ y2) →
966                 is_permutation A (x1 @ [x] @ x2) (y1 @ [y] @ y2).
967
968lemma is_permutation_eq : ∀A.∀l : list A.is_permutation … l l.
969#A #l elim l // #x #xs #IH
970change with ((nil ?) @ (x :: xs)) in ⊢ (??%%);
971>append_cons >associative_append
972@(p_append ? x (nil ?) xs x (nil ?) xs (refl …)) @IH
973qed.
974
975lemma is_permutation_append : ∀A.∀l1,l2,l3,l4 : list A.
976is_permutation A l1 l3 → is_permutation A l2 l4 →
977is_permutation A (l1 @ l2) (l3 @ l4).
978#A #l1 inversion (|l1|)  [2: #n lapply l1 elim n
979[ #l2 #l3 #l4 #H inversion H // #x #x1 #x2 #y #y1 #y2 #EQ #H1 #_
980 #ABS cases(nil_to_nil … (sym_eq ??? ABS)) -ABS #_ #ABS
981 cases(nil_to_nil … ABS) #EQ1 destruct(EQ1) ]
982#x #xs #IH #l2 #l3 #l4 #H inversion H
983[#EQ lapply(jmeq_to_eq ??? EQ) -EQ #EQ destruct(EQ) ]
984#y #y1 #y2 #z #z1 #z2 #EQ destruct(EQ) #H1 #_ #EQx_xs #EQ destruct(EQ) #_
985*)
986
987lemma trans_env_ok : ∀p : state_params.∀ prog.
988no_duplicates_labels … prog →
989let 〈t_prog,m,keep〉 ≝ trans_prog … prog in
990∀f,env_it.lookup p p (env … prog) f = return env_it →
991let dom ≝ get_labels_of_code … (f_body … env_it) in
992∃env_it',n.lookup p p (env … t_prog) f = return env_it' ∧
993let info ≝ call_post_trans … (f_body … env_it) n (nil ?) in
994t_code … info = f_body … env_it' ∧
995get_element … m (a_call (f_lab … env_it')) = (a_call (f_lab … env_it')) :: gen_labels … info ∧
996f_sig … env_it = f_sig … env_it' ∧ f_lab … env_it = f_lab … env_it' ∧
997same_fresh_map_on dom m (lab_map … info) ∧ same_to_keep_on dom keep (lab_to_keep … info).
998#p * #env #main @pair_elim * #t_prog #m #keep whd in match trans_prog;
999normalize nodelta @pair_elim generalize in match O; xxxx (*probably needs invariant *) elim env
1000[ * #env' #fresh #x #_ #_ #_ #f #env_it normalize in ⊢ (% → ?);  #ABS destruct]
1001* #hd_sig #hd_lab #hd_code #tail #IH * #env' #fresh * #m' #keep'
1002normalize in ⊢ (% → ?); normalize nodelta @pair_elim * #env_tail #fresh_tail
1003* #m_tail #keep_tail #EQtail normalize nodelta #EQ1 #EQ2 destruct(EQ1 EQ2)
1004whd in ⊢ (% → ?); whd in match (foldr ?????); * #Hhd_lab #H lapply(no_duplicates_append_r … H)
1005change with (no_duplicates_labels p p (mk_Program p p tail main)) in match
1006(no_duplicates_labels p p (mk_Program p p tail main)); #no_dup_tail
1007lapply(no_duplicates_append_l … H) #no_dup_head normalize nodelta
1008#f #env_it whd in ⊢ (??%? → ?); @eq_function_name_elim normalize nodelta
1009[ #EQ destruct(EQ) whd in ⊢ (???% → ?); #EQ destruct(EQ)
1010  inversion (call_post_trans … hd_code fresh_tail [])
1011  #gen_labs #t_hd_code #t_fresh #t_lab_map #t_lab_to_keep #EQ_trans_code
1012  %{(mk_env_item … hd_sig hd_lab t_hd_code)} %{fresh_tail} %
1013  [ whd in ⊢ (??%?); @eq_function_name_elim [2: * #H @⊥ @H %] #_ normalize nodelta
1014    @eq_f cases hd_sig // ] >EQ_trans_code % [% [ % [ % [% // whd in ⊢ (??%?); >(\b (refl …)) %] % ] % | whd
1015    #x #Hx whd in ⊢ (??%?); >(? : (x == hd_lab) = false) [2: cases daemon (*per Hhd_lab*)]
1016    normalize nodelta cases daemon (* needs lemma on maps *)]
1017  | whd cases daemon (*using lab_to_keep_in_domain and H *)
1018  ]
1019| #Hf #Henv_it cases(IH … no_dup_tail … Henv_it)
1020  [4: >EQtail in ⊢ (??%?);
1021   
1022   
1023    [ >EQ_trans_code % | >EQ_trans_code
1024    [ >EQ_trans_code % | >EQ_trans_code whd in ⊢ (??%?);
1025      cases(eqb_true … (a_call hd_lab) hd_lab) #_ #H1 >(H1 (refl …)) // ]]
1026
1027  #EQt_prog
1028
1029
1030
1031lemma correctness : ∀p,p',prog.
1032let 〈t_prog,m,keep〉 ≝ trans_prog … prog in
1033∀s1,s2,s1' : state p.∀abs_top,abs_tail.
1034∀t : raw_trace (operational_semantics … p' prog) s1 s2.
1035state_rel … m keep abs_top abs_tail s1 s1' →
1036∃abs_top',abs_tail'.∃s2'.∃t' : raw_trace (operational_semantics … p' t_prog) s1' s2'.
1037state_rel  … m keep abs_top' abs_tail' s2 s2' ∧
1038is_permutation ? ((abs_top @ (map_labels_on_trace m (get_costlabels_of_trace … t))) @ abs_tail) 
1039                 (((get_costlabels_of_trace … t') @ abs_top' ) @ abs_tail') ∧
1040 len … t = len … t'.
1041#p #p' #prog @pair_elim * #t_prog #m #keep #EQtrans
1042#s1 #s2 #s1' #abs_top #abs_tail #t lapply abs_top -abs_top lapply abs_tail
1043-abs_tail lapply s1' -s1' elim t
1044[ #st #s1' #abs_tail #abs_top #H %{abs_top} %{abs_tail} %{s1'} %{(t_base …)}
1045  % [2: %] %{H} whd in match (get_costlabels_of_trace ????); >append_nil
1046  @is_permutation_eq
1047]
1048#st1 #st2 #st3 #lab whd in ⊢ (% → ?); #H inversion H in ⊢ ?; #st11 #st12
1049[ * #lab1 #new_code #new_cont #EQcode11 #EQcont11 #EQcode12 #EQcont12 #EQstore
1050  #Hio11 #EQio12 #Hl1 #EQ1 #EQ2 #EQ3 #EQ4 destruct #tl #IH #s1' #abs_tail #abs_top
1051  whd in match state_rel in ⊢ (% → ?); normalize nodelta >EQcont11 in ⊢ (% → ?);
1052  whd in match check_continuations; normalize nodelta whd in match (foldr2 ???????);
1053  inversion (cont ? s1') [ #_ *] * #l1' #new_code' #new_cont' #_ #EQconts1'
1054  normalize nodelta change with (check_continuations ?????) in match (foldr2 ???????);
1055  inversion(check_continuations ?????) [ #_ *] ** #H1 #l2 #ll2 #EQHl2
1056  >m_return_bind normalize nodelta inversion (call_post_clean ?????) [ #_ ***** ]
1057  * #l3 #code_c_st12 #EQcode_c_st12 normalize nodelta
1058  cases l1' in EQconts1'; (*in Hio11 Hl1 EQcont11 H;*) normalize nodelta
1059  [1,4: [ #x #y ] #_ (*#_ #_ #_ #H*) *****
1060  | #x #_ cases (ret_costed_abs ??) normalize nodelta [|#c] ******[|*] #EQ @⊥ >EQ in Hl1;
1061    normalize * /2/ ] *
1062  [ #EQconts1' normalize nodelta ****** #EQ destruct(EQ)
1063    #HH1 #EQ destruct(EQ) >EQcode11 in ⊢ (% → ?); inversion(code … s1')
1064    [
1065    | #x
1066    | #seq #lbl #i #_
1067    | #cond #ltrue #i1 #lfalse #i2 #i3 #_ #_ #_
1068    | #cond #ltrue #i1 #lfalse #i2 #_ #_
1069    | #f #act_p #ret_post #i #_
1070    | #l_in #io #l_out #i #_
1071    ]
1072    [|*: #_ whd in ⊢ (??%% → ?); [ #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ)]
1073         cases(call_post_clean ?????) normalize nodelta
1074         [1,3,5,7,9: #EQ destruct(EQ)] cases daemon (* da fare assurdi!!!*) ]
1075    #EQcodes1' whd in ⊢ (??%% → ?); #EQ destruct(EQ) #EQstore11 #EQio11 #EQ destruct(EQ)
1076    cases(IH (mk_state ? new_code' new_cont' (store … s1') false) ll2 abs_top …)
1077    [2: whd whd in match check_continuations; normalize nodelta
1078       change with (check_continuations ?????) in match (foldr2 ???????); >EQHl2
1079        normalize nodelta % // % // % // % // @EQcode_c_st12 ]
1080    #abs_top' * #abs_tail' * #st3' * #t' ** #Hst3st3' #EQcosts #EQlen
1081    %{abs_top'} %{abs_tail'} %{st3'} %{(t_ind … (cost_act (None ?)) …  t')}
1082    [ @hide_prf whd @(empty ????? 〈(cost_act (None ?)),?〉)
1083      [3: assumption |4: assumption |*:] /3 by nmk/ ]
1084    % [2: whd in ⊢ (??%%); >EQlen % ] %{Hst3st3'} @EQcosts
1085  | #lbl #EQconts1' normalize nodelta ******* #EQ destruct(EQ)
1086    #HH1 #EQ destruct(EQ) #EQget_el >EQcode11 in ⊢ (% → ?);
1087    inversion(code … s1')
1088    [
1089    | #x
1090    | #seq #lbl #i #_
1091    | #cond #ltrue #i1 #lfalse #i2 #i3 #_ #_ #_
1092    | #cond #ltrue #i1 #lfalse #i2 #_ #_
1093    | #f #act_p #ret_post #i #_
1094    | #l_in #io #l_out #i #_
1095    ]
1096    [|*: #_ whd in ⊢ (??%% → ?); #EQ cases daemon (* da fare assurdi !!!*) ]
1097    #EQcodes1' whd in ⊢ (??%% → ?); #EQ destruct(EQ) #EQstore #EQio #EQ destruct(EQ)
1098    cases(IH (mk_state ? new_code' new_cont' (store … s1') false) ll2 l3 …)
1099    [2: whd whd in match check_continuations; normalize nodelta
1100       change with (check_continuations ?????) in match (foldr2 ???????);
1101       >EQHl2 in ⊢ (match % with [ _⇒ ? | _ ⇒ ?]);
1102        normalize nodelta % // % // % // % // @EQcode_c_st12 ]
1103    #abs_top' * #abs_tail' * #st3' * #t' ** #Hst3st3' #EQcost #EQlen
1104    %{abs_top'} %{abs_tail'} %{st3'}
1105    %{(t_ind … (cost_act (Some ? lbl)) … t')}
1106    [ @hide_prf whd @(empty ????? 〈(cost_act (Some ? lbl)),?〉)
1107      [3: assumption |4: assumption |*:] /3 by nmk/ ]
1108    % [2: whd in ⊢ (??%%); >EQlen % ] %{Hst3st3'} >map_labels_on_trace_append
1109    whd in match (map_labels_on_trace ? [lbl]); >append_nil >EQget_el
1110    @is_permutation_cons assumption
1111  ]
1112(*     
1113| #seq #i #store * [| #lbl] #EQcode11 #EQstore #EQ destruct(EQ) #EQcont
1114  #EQ destruct(EQ) #Hio_st11 #Hio_st12 #EQ destruct(EQ) #EQ1 #EQ2 destruct(EQ1 EQ2)
1115  #EQ destruct(EQ) #tl #IH #st3 #l1 whd in ⊢ (% → ?);
1116  inversion(check_continuations ?????) [1,3: #_ *] * #H1 #l2
1117  [ >EQcont in ⊢ (% → ?); | >EQcont in ⊢ (% → ?); ] #EQcheck normalize nodelta
1118  *** #HH1 [ >EQcode11 in ⊢ (% → ?); | >EQcode11 in ⊢ (% → ?); ]
1119  inversion(code … st3)
1120  [1,2,4,5,6,7,8,9,11,12,13,14: (*assurdi da fare *) cases daemon ]
1121  #seq1 #opt_l #i1 #_ #EQcode_st3 change with (m_bind ?????) in ⊢ (??%? → ?);
1122  cases daemon *)
1123|8: #f #act_p #opt_l #i #mem #env_it #EQcode11 #EQenv_it #EQmem
1124    #EQ destruct(EQ) #EQcode12 #EQcont12 #EQio11 #EQio12 #EQ1 #EQ2 #EQ3
1125    destruct(EQ1 EQ2 EQ3) #EQ destruct(EQ) #tl #IH #st1' #abs_top #abs_tail
1126    whd in ⊢ (% → ?); inversion(check_continuations ????) [ #_ *] ** #H1
1127    #abs_top_cont #abs_tail_cont #EQcheck
1128    normalize nodelta **** #HH1 >EQcode11 in ⊢ (% → ?); inversion(code … st1')
1129    [1,2,3,4,5,7: (*assurdi da fare*) cases daemon] #f' #act_p' #opt_l' #i' #_
1130    #EQcode_st1' #EQclean #EQstore #EQio #EQ destruct(EQ)
1131    cut(∃env_it',n.lookup p p (env … t_prog) f = return env_it' ∧
1132          t_code … (call_post_trans … (f_body … env_it) n (nil ?)) = f_body … env_it' ∧
1133          get_element … m (a_call (f_lab … env_it')) = (a_call (f_lab … env_it')) :: gen_labels … (call_post_trans … (f_body … env_it) n (nil ?)) ∧
1134          f_sig … env_it = f_sig … env_it' ∧ f_lab … env_it = f_lab … env_it')
1135    [ cases daemon (*TODO*) ] * #env_it' * #fresh' **** #EQenv_it' #EQtrans #EQgen_labels #EQsignature #EQlab_env_it
1136    change with (m_bind ?????) in EQclean : (??%?); inversion(call_post_clean ?????) in EQclean;
1137    [ #_ whd in ⊢ (??%% → ?); #EQ destruct] * #abs_top'' #i'' #EQi' >m_return_bind
1138    inversion opt_l' in EQcode_st1'; [| #lbl'] #EQopt_l' #EQcode_st1' normalize nodelta
1139    [2: inversion(memb ???) normalize nodelta #Hlbl_keep
1140        [  inversion (get_element ????) normalize nodelta [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
1141           #lbl'' #l3' #_ #EQget_el whd in match (eqb ???); inversion (eqb ???) normalize nodelta
1142          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true ? lbl'' lbl') #H1 #_
1143          lapply(H1 H) -H -H1 #EQ destruct(EQ) inversion(eqb_list ???) normalize nodelta
1144          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true (DeqSet_List ?) l3' abs_top'')
1145          #H1 #_ lapply(H1 H) -H -H1 #EQ destruct(EQ) whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ
1146          #EQ destruct(EQ)
1147          cases(IH (mk_state ? (f_body … env_it') (〈ret_act opt_l',i'〉 :: (cont … st1')) (store ? st12) false) (abs_tail_cont) (gen_labels … (call_post_trans … (f_body … env_it) fresh' (nil ?))))
1148          [2: whd >EQcont12
1149            change with (m_bind ??? (check_continuations ?????) ?) in match (check_continuations ?????);
1150            >EQcheck >m_return_bind normalize nodelta >EQi' normalize nodelta >EQopt_l'
1151            whd in match ret_costed_abs; normalize nodelta >Hlbl_keep normalize nodelta
1152            % // % // % // % [/5 by conj/] >EQgen_labels >EQcode12 <EQtrans
1153            @(inverse_call_post_trans … fresh') [2: % |*: cases daemon (*TODO*) ]
1154          ]
1155          #abs_top''' * #abs_tail''' * #st3' * #t' ** #Hst3st3' #EQcosts #EQlen %{abs_top'''}
1156          %{abs_tail'''} %{st3'} %{(t_ind … (call_act f (f_lab … env_it')) …  t')}
1157          [ @hide_prf @call /2 width=10 by jmeq_to_eq/ ] % [2: whd in ⊢ (??%%); >EQlen %]
1158          %{Hst3st3'} >map_labels_on_trace_append whd in match (get_costlabels_of_trace ????) in ⊢ (???%);
1159          >EQlab_env_it >associative_append whd in match (append ???); >associative_append
1160          >associative_append in ⊢ (???%); >(associative_append … [?]) in ⊢ (???%);
1161          whd in match (map_labels_on_trace ??); >EQgen_labels @is_permutation_cons
1162          >append_nil whd in match (append ???) in ⊢ (???%); //
1163        | 
1164          xxxxxxxxxx
1165           
1166           
1167           
1168                   
1169            inversion(memb ???) normalize nodelta #Hlbl_keep
1170            [ inversion (get_element ????) normalize nodelta [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
1171          #lbl'' #l3' #_ #EQget_el whd in match (eqb ???); inversion (eqb ???) normalize nodelta
1172          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true ? lbl'' lbl') #H1 #_
1173          lapply(H1 H) -H -H1 #EQ destruct(EQ) inversion(eqb_list ???) normalize nodelta
1174          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true (DeqSet_List ?) l3' abs_top'')
1175          #H1 #_ lapply(H1 H) -H -H1 #EQ destruct(EQ) whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ
1176          #EQ destruct(EQ) whd in match ret_costed_abs; normalize nodelta >Hlbl_keep
1177          normalize nodelta >EQi' normalize nodelta % // % // % [ % // % // % //]
1178   
1179   
1180   
1181   
1182    cases(IH (mk_state ? (f_body … env_it') (〈ret_act opt_l',i'〉 :: (cont … st1')) (store ? st12) false) (abs_top'' @ abs_tail_cont) (get_element … m (a_call (f_lab … env_it'))))
1183    [2: whd >EQcont12
1184     change with (m_bind ??? (check_continuations ?????) ?) in match (check_continuations ?????);
1185     >EQcheck >m_return_bind normalize nodelta >EQi' normalize nodelta
1186     cases opt_l' in EQcode_st1' EQclean; [| #lbl'] #EQcode_st1' normalize nodelta
1187    [2: inversion(memb ???) normalize nodelta #Hlbl_keep
1188        [ inversion (get_element ????) normalize nodelta [ #_ whd in ⊢ (??%% → ?); #EQ destruct]
1189          #lbl'' #l3' #_ #EQget_el whd in match (eqb ???); inversion (eqb ???) normalize nodelta
1190          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true ? lbl'' lbl') #H1 #_
1191          lapply(H1 H) -H -H1 #EQ destruct(EQ) inversion(eqb_list ???) normalize nodelta
1192          [2: #_ whd in ⊢ (??%% → ?); #EQ destruct] #H cases(eqb_true (DeqSet_List ?) l3' abs_top'')
1193          #H1 #_ lapply(H1 H) -H -H1 #EQ destruct(EQ) whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ
1194          #EQ destruct(EQ) whd in match ret_costed_abs; normalize nodelta >Hlbl_keep
1195          normalize nodelta >EQi' normalize nodelta % // % // % [ % // % // % //] >EQcode12 <EQtrans
1196          cases daemon (*TODO*)
1197        | whd in ⊢ (??%% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ destruct(EQ)
1198          whd in match ret_costed_abs; normalize nodelta >Hlbl_keep normalize nodelta
1199          % // % // % [ % // % //]  >EQcode12 <EQtrans
1200           * [ #_ #l3'
1201    cases(bind_inversion ????? H) in ⊢ ?;
1202
1203   
1204    >EQcost normalize
1205   
1206    %{l1}
1207  xxxxxxxx
1208   whd in match is_silent_cost_act_b; normalize nodelta
1209    cases lab1 in H Hio11; normalize nodelta
1210    [1,2,4: [1,2: #x [ #y]] #H #Hio11 #EQ destruct]
1211    * normalize nodelta [2: #x #H #Hio11 #EQ destruct] #H #Hio11 #_
1212    inversion(call_post_clean ?????)
1213    [ #_ *** ] * #l3 #code_c_st12 #EQcode_c_st12 normalize nodelta *****
1214    #EQ destruct(EQ) #HH1 #EQ destruct(EQ) >EQcode11
1215    whd in match (call_post_clean ?????) in ⊢ (% → ?); whd in ⊢ (??%% → ?);
1216    inversion(code … s1')
1217    [| #x #EQ1 normalize in ⊢ (% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ
1218       destruct(EQ) |*: cases daemon (*ASSURDI!!*)] #EQcode_s1'
1219    normalize in ⊢ (% → ?); #EQ destruct(EQ) #EQstore #EQio
1220    cases(IH (mk_state ? new_code' new_cont' (store … s1') false)  l1 ?)
1221    [2: whd whd in match check_continuations; normalize nodelta >EQHl2
1222        normalize nodelta % // % // >EQcode_c_st12 % // ] #l3 * #st3' * #t' **
1223    #Hst3st12' #EQcost #EQlen %{l3} %{st3'} %{(t_ind … (cost_act (None ?)) … t')}
1224    [ whd @(empty ????? 〈(cost_act (None ?)),?〉)
1225      [3: @hide_prf assumption |4: @hide_prf @EQconts1' |*: ] @hide_prf //
1226      [ <EQio #H2 @⊥ lapply(Hio11 H2) * #F #eq destruct | % *]  ] %
1227    [ %{Hst3st12'} whd in match (get_costlabels_of_trace ????); 
1228      whd in match (get_costlabels_of_trace ????) in ⊢ (???%); //
1229    | whd in match (len ????); whd in match (len ????) in ⊢ (???%);
1230      >EQlen %
1231    ]
1232  | #non_sil_lab1 normalize nodelta inversion(call_post_clean ?????) [ #_ ****]
1233    * #l3 #code' #EQcall' normalize nodelta inversion(ret_costed_abs ??)
1234    [2: #x #H2 @⊥ cases lab1 in Hl1 H2; normalize
1235        [ #f #c #_
1236        | #x * #ABS @⊥ @ABS %
1237        | #x #_
1238        | #_
1239        ]
1240        #EQ destruct(EQ) ]
1241    #Hlab1 normalize nodelta ***** #EQ destruct(EQ) #HH1 #EQ destruct(EQ)
1242    >EQcode11  inversion(code … s1')
1243    [| #x #EQ1 normalize in ⊢ (% → ?); #EQ lapply(eq_to_jmeq ??? EQ) -EQ #EQ
1244       destruct(EQ) |*: cases daemon (*ASSURDI!!*)] #EQcode_s1'
1245    normalize in ⊢ (% → ?); #EQ destruct(EQ) #EQstore #EQio
1246    cases(IH (mk_state ? new_code' new_cont' (store … s1') false) l1 ?)
1247    [2: whd whd in match check_continuations; normalize nodelta >EQHl2
1248        normalize nodelta % // % // % // >EQcall' % ] #l4 * #st3' * #t' **
1249        #Hst3st3' #EQcost #EQlen %{l4} %{st3'} %{(t_ind … lab1 … t')}
1250        [ whd @(empty ????? 〈lab1,?〉)
1251        [3: @hide_prf assumption |4: assumption |*:] @hide_prf // #H3 @Hio11 >EQio @H3]
1252        % [2: whd in match (len ????); whd in match (len ????) in ⊢ (???%);
1253              >EQlen % ]
1254        %{Hst3st3'} >associative_append <EQcost <associative_append
1255        >map_labels_on_trace_append <associative_append @eq_f2 //
1256        cases new_code' in EQcall';
1257        [| #y | #seq #opt_l #i1 | #cond #ltrue #i_true #lfalse #i_false #i1
1258         | #cond #ltrue #i_true #lfalse #ifalse | #f #act_p #ret_opt #i | #l_in #io #l_out #i ]
1259        whd in match (call_post_clean ?????);
1260        [1,2: #EQ destruct(EQ) //
1261       
1262       
1263         whd in match (get_costlabels_of_trace ??? ?) in ⊢ (??%%);
1264       
1265         @eq_f2  whd in ⊢ (??%?);
1266         
1267 
1268 
1269cases daemon (*TODO*)
1270qed.
Note: See TracBrowser for help on using the repository browser.