source: src/joint/Joint.ma @ 2824

Last change on this file since 2824 was 2824, checked in by tranquil, 7 years ago
  • moved sum on lists notation to extranat
  • used sum on lists to define portion of stack occupied by globals
  • corrected probable bug in joint semantics, where initial state would not move stack pointer past globals before setting up main
File size: 23.4 KB
Line 
1include "common/BackEndOps.ma".
2include "common/CostLabel.ma".
3include "common/Registers.ma".
4include "ASM/I8051.ma".
5include "common/Graphs.ma".
6include "utilities/lists.ma".
7include "common/LabelledObjects.ma".
8include "ASM/Util.ma".
9include "basics/lists/listb.ma".
10include "joint/String.ma".
11
12(* Here is the structure of parameter records (downward edges are coercions,
13   the ↓ edges are the only explicitly defined coercions). lin_params and
14   graph_params are simple wrappers of unserialized_params, and the coercions
15   from them to params instantate the missing bits with values for linarized
16   programs and graph programs respectively.
17
18        lin_params      graph_params
19              |   \_____ /____   |
20              |         /     \  |
21              |        /      ↓  ↓
22              |       |      params
23              |       |        |
24              |       |   stmt_params
25              |       |    /   
26          unserialized_params             
27
28unserialized_params : things unrelated to being in graph or linear form
29stmt_params : adds successor type needed to define statements
30params : adds type of code and related properties *)
31
32(*
33inductive possible_flows : Type[0] ≝
34| Labels : list label → possible_flows
35| Call : possible_flows.
36*)
37
38inductive argument (T : Type[0]) : Type[0] ≝
39| Reg : T → argument T
40| Imm : Byte → argument T.
41
42definition psd_argument ≝ argument register.
43 
44definition psd_argument_from_reg : register → psd_argument ≝ Reg register.
45coercion reg_to_psd_argument : ∀r : register.psd_argument ≝ psd_argument_from_reg
46  on _r : register to psd_argument.
47(*
48definition psd_argument_from_beval : beval → psd_argument ≝ Imm register.
49coercion beval_to_psd_argument : ∀b : beval.psd_argument ≝ psd_argument_from_beval
50  on _b : beval to psd_argument.
51*)
52definition psd_argument_from_byte : Byte → psd_argument ≝ Imm ?.
53coercion byte_to_psd_argument : ∀b : Byte.psd_argument ≝ psd_argument_from_byte
54  on _b : Byte to psd_argument.
55
56definition hdw_argument ≝ argument Register.
57 
58definition hdw_argument_from_reg : Register → hdw_argument ≝ Reg Register.
59coercion reg_to_hdw_argument : ∀r : Register.hdw_argument ≝ hdw_argument_from_reg
60  on _r : Register to hdw_argument.
61(*
62definition hdw_argument_from_beval : beval → hdw_argument ≝ Imm Register.
63coercion beval_to_hdw_argument : ∀b : beval.hdw_argument ≝ hdw_argument_from_beval
64  on _b : beval to hdw_argument.
65*)
66definition hdw_argument_from_byte : Byte → hdw_argument ≝ Imm ?.
67coercion byte_to_hdw_argument : ∀b : Byte.psd_argument ≝ psd_argument_from_byte
68  on _b : Byte to hdw_argument.
69
70definition byte_of_nat : nat → Byte ≝ bitvector_of_nat 8.
71definition zero_byte : Byte ≝ bv_zero 8.
72
73record unserialized_params : Type[1] ≝
74 { acc_a_reg: Type[0] (* registers that will eventually need to be A *)
75 ; acc_b_reg: Type[0] (* registers that will eventually need to be B *)
76 ; acc_a_arg: Type[0] (* arguments that will eventually need to be A *)
77 ; acc_b_arg: Type[0] (* arguments that will eventually need to be B *)
78 ; dpl_reg: Type[0]   (* low address registers *)
79 ; dph_reg: Type[0]   (* high address registers *)
80 ; dpl_arg: Type[0]   (* low address registers *)
81 ; dph_arg: Type[0]   (* high address registers *)
82 ; snd_arg : Type[0]  (* second argument of binary op *)
83 ; pair_move: Type[0] (* argument of move instructions *)
84 ; call_args: Type[0] (* arguments of function calls *)
85 ; call_dest: Type[0] (* possible destination of function computation *)
86 (* other instructions not fitting in the general framework *)
87 ; ext_seq : Type[0]
88 ; ext_seq_labels : ext_seq → list label
89 ; has_tailcalls : bool
90 (* if needed: ; ext_fin_branch : Type[0] ; ext_fin_branch_labels : ext_fin_branch → list label *)
91 ; paramsT : Type[0]
92 }.
93 
94record get_pseudo_reg_functs (p : unserialized_params) : Type[0] ≝
95{ acc_a_regs : acc_a_reg p → list register
96; acc_b_regs : acc_b_reg p → list register
97; acc_a_args : acc_a_arg p → list register
98; acc_b_args : acc_b_arg p → list register
99; dpl_regs : dpl_reg p → list register
100; dph_regs : dph_reg p → list register
101; dpl_args : dpl_arg p → list register
102; dph_args : dph_arg p → list register
103; snd_args : snd_arg p → list register
104; pair_move_regs : pair_move p → list register
105; f_call_args : call_args p → list register
106; f_call_dest : call_dest p → list register
107; ext_seq_regs : ext_seq p → list register
108; params_regs : paramsT p → list register
109}.
110
111record uns_params : Type[1] ≝
112{ u_pars :> unserialized_params
113; functs : get_pseudo_reg_functs u_pars
114}.
115
116inductive joint_seq (p:unserialized_params) (globals: list ident): Type[0] ≝
117  | COMMENT: String → joint_seq p globals
118  | MOVE: pair_move p → joint_seq p globals
119  | POP: acc_a_reg p → joint_seq p globals
120  | PUSH: acc_a_arg p → joint_seq p globals
121  | ADDRESS: ∀i: ident. i ∈ globals → dpl_reg p → dph_reg p → joint_seq p globals
122  | OPACCS: OpAccs → acc_a_reg p → acc_b_reg p → acc_a_arg p → acc_b_arg p → joint_seq p globals
123  | OP1: Op1 → acc_a_reg p → acc_a_reg p → joint_seq p globals
124  | OP2: Op2 → acc_a_reg p → acc_a_arg p → snd_arg p → joint_seq p globals
125  (* int done with generic move *)
126(*| INT: generic_reg p → Byte → joint_seq p globals *)
127  | CLEAR_CARRY: joint_seq p globals
128  | SET_CARRY: joint_seq p globals
129  | LOAD: acc_a_reg p → dpl_arg p → dph_arg p → joint_seq p globals
130  | STORE: dpl_arg p → dph_arg p → acc_a_arg p → joint_seq p globals
131  | extension_seq : ext_seq p → joint_seq p globals.
132 
133definition get_used_registers_from_seq : ∀p : unserialized_params.∀globals.
134get_pseudo_reg_functs p → joint_seq p globals → list register ≝
135λp,globals,functs,seq.
136match seq with
137[ COMMENT _ ⇒ [ ]
138| MOVE pm ⇒ pair_move_regs … functs pm
139| POP r ⇒ acc_a_regs … functs r
140| PUSH r ⇒ acc_a_args … functs r
141| ADDRESS i prf r1 r2 ⇒ (dpl_regs … functs r1) @ (dph_regs … functs r2)
142| OPACCS o r1 r2 r3 r4 ⇒ (acc_a_regs … functs r1) @ (acc_b_regs … functs r2)
143       @ (acc_a_args … functs r3) @ (acc_b_args … functs r4)
144| OP1 o r1 r2 ⇒ (acc_a_regs … functs r1) @ (acc_a_regs … functs r2)
145| OP2 o r1 r2 r3 ⇒ (acc_a_regs … functs r1) @ (acc_a_args … functs r2) @
146         (snd_args … functs r3)
147| CLEAR_CARRY ⇒ [ ]
148| SET_CARRY ⇒ [ ]
149| LOAD r1 r2 r3 ⇒ (acc_a_regs … functs r1) @ (dpl_args … functs r2) @
150                  (dph_args … functs r3)
151| STORE r1 r2 r3 ⇒ (dpl_args … functs r1) @ (dph_args … functs r2) @
152                    (acc_a_args … functs r3)
153| extension_seq ext ⇒ ext_seq_regs … functs ext
154].
155
156definition NOOP ≝ λp,globals.COMMENT p globals EmptyString.
157
158notation "r ← a1 .op. a2" with precedence 60 for
159  @{'op2 $op $r $a1 $a2}.
160notation "r ← . op . a" with precedence 60 for
161  @{'op1 $op $r $a}.
162notation "r ← a" with precedence 60 for
163  @{'mov $r $a}. (* to be set in individual languages *)
164notation "❮r, s❯ ← a1 . op . a2" with precedence 55 for
165  @{'opaccs $op $r $s $a1 $a2}.
166
167interpretation "op2" 'op2 op r a1 a2 = (OP2 ? ? op r a1 a2).
168interpretation "op1" 'op1 op r a = (OP1 ? ? op r a).
169interpretation "opaccs" 'opaccs op r s a1 a2 = (OPACCS ? ? op r s a1 a2).
170
171coercion extension_seq_to_seq : ∀p,globals.∀s : ext_seq p.joint_seq p globals ≝
172  extension_seq on _s : ext_seq ? to joint_seq ??.
173
174(* inductive joint_branch (p : step_params) : Type[0] ≝
175  | COND: acc_a_reg p → label → joint_branch p
176  | extension_branch : ext_branch p → joint_branch p.*)
177
178(*coercion extension_to_branch : ∀p.∀s : ext_branch p.joint_branch p ≝
179  extension_branch on _s : ext_branch ? to joint_branch ?.*)
180
181inductive joint_step (p : unserialized_params) (globals : list ident) : Type[0] ≝
182  | COST_LABEL: costlabel → joint_step p globals
183  | CALL: (ident + (dpl_arg p × (dph_arg p))) → call_args p → call_dest p → joint_step p globals
184  | COND: acc_a_reg p → label → joint_step p globals
185  | step_seq : joint_seq p globals → joint_step p globals.
186 
187definition get_used_registers_from_step : ∀p : unserialized_params.∀globals.
188get_pseudo_reg_functs p → joint_step p globals → list register ≝
189λp,globals,functs,step.
190match step with
191[ COST_LABEL c ⇒ [ ]
192| CALL id args dest ⇒ (f_call_args … functs args) @ (f_call_dest … functs dest)
193| COND r lbl ⇒  acc_a_regs … functs r
194| step_seq s ⇒ get_used_registers_from_seq … functs s
195].
196
197coercion seq_to_step : ∀p,globals.∀s : joint_seq p globals.joint_step p globals ≝
198  step_seq on _s : joint_seq ?? to joint_step ??.
199
200definition step_labels ≝
201  λp, globals.λs : joint_step p globals.
202  match s with
203  [ step_seq s ⇒
204    match s with
205    [ extension_seq ext ⇒ ext_seq_labels … ext
206    | _ ⇒ [ ]
207    ]
208  | COND _ l ⇒ [l]
209  | _ ⇒ [ ]
210  ].
211
212definition step_forall_labels : ∀p.∀globals.
213    (label → Prop) → joint_step p globals → Prop ≝
214λp,g,P,inst. All … P (step_labels … inst).
215
216record stmt_params : Type[1] ≝
217  { uns_pars :> uns_params
218  ; succ : Type[0]
219  ; succ_label : succ → option label
220  ; has_fcond : bool
221  }.
222
223inductive joint_fin_step (p: unserialized_params): Type[0] ≝
224  | GOTO: label → joint_fin_step p
225  | RETURN: joint_fin_step p
226  | TAILCALL :
227    has_tailcalls p → (ident + (dpl_arg p × (dph_arg p))) →
228    call_args p → joint_fin_step p.
229
230definition fin_step_labels ≝ λp.λs : joint_fin_step p.
231  match s with
232  [ GOTO l ⇒ [l]
233  | _ ⇒ [ ]
234  ].
235
236inductive joint_statement (p: stmt_params) (globals: list ident): Type[0] ≝
237  | sequential: joint_step p globals → succ p → joint_statement p globals
238  | final: joint_fin_step p → joint_statement p globals
239  | FCOND: has_fcond p → acc_a_reg p → label → label → joint_statement p globals.
240
241coercion fin_step_to_stmt : ∀p : stmt_params.∀globals.
242  ∀s : joint_fin_step p.joint_statement p globals ≝
243  final on _s : joint_fin_step ? to joint_statement ??.
244
245record params : Type[1] ≝
246 { stmt_pars :> stmt_params
247 ; codeT: list ident → Type[0]
248 ; code_point : Type[0]
249 ; stmt_at : ∀globals.codeT globals → code_point → option (joint_statement stmt_pars globals)
250 ; point_of_label : ∀globals.codeT globals → label → option code_point
251 ; point_of_succ : code_point → succ stmt_pars → code_point
252 }.
253
254definition code_has_point ≝
255  λp,globals,c,pt.match stmt_at p globals c pt with [Some _ ⇒ true | None ⇒ false].
256
257(* interpretation "code membership" 'mem p c = (code_has_point ?? c p). *)
258(*
259definition point_in_code ≝ λp,globals,code.Σpt.bool_to_Prop (code_has_point p globals code pt).
260unification hint 0 ≔ p, globals, code ⊢ point_in_code p globals code ≡ Sig (code_point p) (λpt.bool_to_Prop (code_has_point p globals code pt)).
261
262definition stmt_at_safe ≝ λp,globals,code.λpt : point_in_code p globals code.
263  match pt with
264  [ mk_Sig pt' pt_prf ⇒
265    match stmt_at … code pt' return λx.stmt_at … code pt' = x → ? with
266    [ Some x ⇒ λ_.x
267    | None ⇒ λabs.⊥
268    ] (refl …)
269  ]. normalize in pt_prf;
270    >abs in pt_prf; // qed.
271*)
272
273definition forall_statements : ∀p : params.∀globals.pred_transformer (joint_statement p globals) (codeT p globals)  ≝
274  λp,globals,P,c. ∀pt,s.stmt_at ?? c pt = Some ? s → P s.
275
276definition forall_statements_i :
277  ∀p : params.∀globals.(code_point p → joint_statement p globals → Prop) →
278    codeT p globals → Prop  ≝
279  λp,globals,P,c. ∀pt,s.stmt_at ?? c pt = Some ? s → P pt s.
280
281lemma forall_statements_mp : ∀p,globals.modus_ponens ?? (forall_statements p globals).
282#p #globals #P #Q #H #y #G #pnt #s #EQ @H @(G … EQ) qed.
283
284lemma forall_statements_i_mp : ∀p,globals.∀P,Q.(∀pt,s.P pt s → Q pt s) →
285  ∀c.forall_statements_i p globals P c → forall_statements_i p globals Q c.
286#p #globals #P #Q #H #y #G #pnt #s #EQ @H @(G … EQ) qed.
287
288definition code_has_label ≝ λp,globals,c,l.
289  match point_of_label p globals c l with
290  [ Some pt ⇒ code_has_point … c pt
291  | None ⇒ false
292  ].
293
294definition stmt_explicit_labels :
295  ∀p,globals.
296  joint_statement p globals → list label ≝
297  λp,globals,stmt. match stmt with
298  [ sequential c _ ⇒ step_labels … c
299  | final c ⇒ fin_step_labels … c
300  | FCOND _ _ l1 l2 ⇒ [l1 ; l2]
301  ].
302
303definition stmt_implicit_label : ∀p,globals.joint_statement p globals →
304  option label ≝
305 λp,globals,s.match s with [ sequential _ s ⇒ succ_label … s | _ ⇒ None ?].
306 
307definition stmt_labels : ∀p : stmt_params.∀globals.
308    joint_statement p globals → list label ≝
309  λp,g,stmt.
310  (match stmt_implicit_label … stmt with
311     [ Some l ⇒ [l]
312     | None ⇒ [ ]
313     ]) @ stmt_explicit_labels … stmt.
314     
315definition stmt_registers : ∀p : stmt_params.∀globals.
316joint_statement p globals → list register ≝
317λp,globals,stmt.
318match stmt with
319[ sequential c _ ⇒ get_used_registers_from_step … (functs … p) c
320| final c ⇒
321   match c with [ TAILCALL _ _ r ⇒ f_call_args … (functs … p) r | _ ⇒ [ ] ]
322| FCOND _ r _ _ ⇒ acc_a_regs … (functs … p) r
323].
324
325definition stmt_forall_labels ≝
326  λp, globals.λ P : label → Prop.λs : joint_statement p globals.
327  All … P (stmt_labels … s).
328 
329
330lemma stmt_forall_labels_explicit : ∀p,globals,P.∀s : joint_statement p globals.
331  stmt_forall_labels … P s → All … P (stmt_explicit_labels … s).
332#p#globals#P #s
333whd in ⊢ (% → ?);
334whd in ⊢ (???% → ?);
335elim (stmt_implicit_label ???) [2: #next * #_] //
336qed.
337
338lemma stmt_forall_labels_implicit : ∀p,globals,P.∀s : joint_statement p globals.
339  stmt_forall_labels … P s →
340    opt_All … P (stmt_implicit_label … s).
341#p#globals#P#s
342whd in ⊢ (% → ?);
343whd in ⊢ (???% → ?);
344elim (stmt_implicit_label ???)
345[ //
346| #next * #Pnext #_ @Pnext
347]
348qed.
349
350definition code_forall_labels ≝
351  λp,globals,P,c.forall_statements p globals (stmt_forall_labels … P) c.
352
353lemma code_forall_labels_mp : ∀p,globals,P,Q.(∀l.P l → Q l) →
354  ∀c.code_forall_labels p globals P c → code_forall_labels … Q c ≝
355  λp,globals,P,Q,H.forall_statements_mp … (λs. All_mp … H ?).
356
357record lin_params : Type[1] ≝
358  { l_u_pars : uns_params  }.
359 
360lemma index_of_label_length : ∀tag,A,lbl,l.occurs_exactly_once ?? lbl l → lt (index_of_label tag A lbl l) (|l|).
361#tag #A #lbl #l elim l [*]
362** [2: #id] #a #tl #IH
363[ change with (if (eq_identifier ???) then ? else ?) in match (occurs_exactly_once ????);
364  change with (if (eq_identifier ???) then ? else ?) in match (index_of_label ????);
365  @eq_identifier_elim #Heq normalize nodelta
366  [ #_ normalize / by /]
367| whd in ⊢ (?%→?%?);
368]
369#H >(index_of_label_from_internal … H)
370@le_S_S @(IH H)
371qed.
372
373(* mv *)
374lemma nth_opt_hit_length : ∀A,l,n,x.nth_opt A n l = Some ? x → n < |l|.
375#A #l elim l normalize [ #n #x #ABS destruct(ABS)]
376#hd #tl #IH * [2:#n] #x normalize [#H @le_S_S @(IH … H)] /2 by /
377qed.
378
379lemma nth_opt_miss_length : ∀A,l,n.nth_opt A n l = None ? → n ≥ |l|.
380#A #l elim l [//] #hd #tl #IH * normalize [#ABS destruct(ABS)]
381#n' #H @le_S_S @(IH … H)
382qed.
383
384lemma nth_opt_safe : ∀A,l,n,prf.nth_opt A n l = Some ? (nth_safe A n l prf).
385#A #l elim l
386[ #n #ABS @⊥ /2 by absurd/
387| #hd #tl #IH * normalize //
388]
389qed.
390
391definition lin_params_to_params ≝
392  λlp : lin_params.
393     mk_params
394      (mk_stmt_params (l_u_pars lp) unit (λ_.None ?) true)
395    (* codeT ≝ *)(λglobals.list ((option label) × (joint_statement ? globals)))
396    (* code_point ≝ *)ℕ
397    (* stmt_at ≝ *)(λglobals,code,point.! ls ← nth_opt ? point code ; return \snd ls)
398    (* point_of_label ≝ *)(λglobals,c,lbl.
399      If occurs_exactly_once ?? lbl c then with prf do
400        return index_of_label ?? lbl c
401      else
402        None ?)
403    (* point_of_succ ≝ *)(λcurrent.λ_.S (current)).
404
405coercion lp_to_p : ∀lp : lin_params.params ≝ lin_params_to_params
406  on _lp : lin_params to params.
407 
408lemma lin_code_has_point : ∀lp : lin_params.∀globals.∀code:codeT lp globals.
409  ∀pt.code_has_point … code pt = leb (S pt) (|code|).
410#lp #globals #code elim code
411[ #pt %
412| #hd #tl #IH * [%]
413  #n @IH
414]qed.
415
416lemma lin_code_has_label : ∀lp : lin_params.∀globals.∀code:codeT lp globals.
417  ∀lbl.code_has_label … code lbl = occurs_exactly_once ?? lbl code.
418#lp #globals #code #lbl
419whd in match (code_has_label ????);
420whd in match (point_of_label ????);
421elim (true_or_false_Prop (occurs_exactly_once ?? lbl code))
422#Heq >Heq normalize nodelta
423[ >lin_code_has_point @(leb_elim (S ?)) [#_ |
424  #ABS elim(absurd ?? ABS) -ABS
425  @index_of_label_length assumption ]] %
426qed.
427
428record graph_params : Type[1] ≝
429  { g_u_pars : uns_params }.
430
431(* One common instantiation of params via Graphs of joint_statements
432   (all languages but LIN) *)
433definition graph_params_to_params ≝
434  λgp : graph_params.
435     mk_params
436      (mk_stmt_params (g_u_pars gp) label (Some ?) false)
437    (* codeT ≝ *)(λglobals.graph (joint_statement ? globals))
438    (* code_point ≝ *)label
439    (* stmt_at ≝ *)(λglobals,code.lookup LabelTag ? code)
440    (* point_of_label ≝ *)(λ_.λ_.λlbl.return lbl)
441    (* point_of_succ ≝ *)(λ_.λlbl.lbl).
442
443coercion gp_to_p : ∀gp:graph_params.params ≝ graph_params_to_params
444on _gp : graph_params to params.
445
446lemma graph_code_has_point : ∀gp : graph_params.∀globals.∀code:codeT gp globals.
447  ∀pt.code_has_point … code pt = (pt ∈ code). // qed.
448
449lemma graph_code_has_label : ∀gp : graph_params.∀globals.∀code:codeT gp globals.
450  ∀lbl.code_has_label … code lbl = (lbl ∈ code). // qed.
451
452definition stmt_forall_succ ≝ λp,globals.λP : succ p → Prop.
453  λs : joint_statement p globals.
454  match s with
455  [ sequential _ n ⇒ P n
456  | _ ⇒ True
457  ].
458
459definition statement_closed : ∀globals.∀p : params.
460  codeT p globals → code_point p → (joint_statement p globals) → Prop ≝
461λglobals,p,code,pt,s.
462  All ? (λl.bool_to_Prop (code_has_label ?? code l)) (stmt_explicit_labels … s) ∧
463  stmt_forall_succ … (λn.bool_to_Prop (code_has_point … code (point_of_succ ? pt n))) s.
464
465definition code_closed : ∀p : params.∀globals.
466  codeT p globals → Prop ≝ λp,globals,code.
467    forall_statements_i … (statement_closed … code) code.
468
469record joint_internal_function (p:params) (globals: list ident) : Type[0] ≝
470{ joint_if_luniverse: universe LabelTag;    (*CSC: used only for compilation*)
471  joint_if_runiverse: universe RegisterTag; (*CSC: used only for compilation*)
472  (* Paolo: if we want this machinery to work for RTLabs too, we will need the
473     following, right? *)
474(*  joint_if_sig: signature;  -- dropped in front end *)
475  joint_if_result   : call_dest p;
476  joint_if_params   : paramsT p;
477  joint_if_stacksize: nat;
478  joint_if_local_stacksize: nat; (* size of the stack devoted to "forced" stack positions
479    (that are already on stack in the front end) *)
480  joint_if_code     : codeT p globals ;
481  joint_if_entry : code_point p (* Paolo: condition entry ∈ code is ensured by good_if ;
482  joint_if_exit : Σpt.bool_to_Prop (code_has_point … joint_if_code pt) *)
483}.
484
485definition regs_in_universe : ∀p,globals.
486codeT p globals → universe RegisterTag → Prop ≝
487λp,globals,c,u.∀pt,stmt.stmt_at p globals c pt = return stmt →
488All ? (λreg.fresh_for_univ … reg u) (stmt_registers … stmt).
489
490definition code_in_universe : ∀p,globals.
491  codeT p globals → universe LabelTag → Prop ≝
492  λp,globals,c,u.∀l.code_has_label … c l → fresh_for_univ … l u.
493
494lemma memb_to_present : ∀tag,A.∀i : identifier tag.∀m.
495  i ∈ m → present tag A m i.
496#tag #A #i #m whd in ⊢ (?%→%); cases (lookup tag A m i)
497[ * | #x #_ % #ABS destruct ]
498qed.
499
500lemma present_to_memb : ∀tag,A.∀i : identifier tag.∀m.
501  present tag A m i → bool_to_Prop (i∈m).
502#tag #A #i #m whd in ⊢ (%→?%); cases (lookup tag A m i)
503[ * #ABS cases (ABS (refl …)) | #x #_ % ]
504qed.
505
506lemma graph_code_in_universe_if : ∀p : graph_params.∀globals.
507  ∀c : codeT p globals.∀u.fresh_map_for_univ … c u → code_in_universe … c u ≝
508λp,g,c,u,H,l,G.H ? (memb_to_present … G).
509
510lemma graph_code_in_universe_only_if : ∀p : graph_params.∀globals.
511  ∀c : codeT p globals.∀u.
512  code_in_universe … c u → fresh_map_for_univ … c u ≝
513λp,g,c,u,H,l,G.H ? (present_to_memb … G).
514
515include alias "basics/logic.ma".
516
517record good_if
518(p : params) (globals : list ident) (def : joint_internal_function p globals)
519: Prop ≝
520{ entry_costed :
521  ∃l,nxt.
522        stmt_at … (joint_if_code … def) (joint_if_entry … def) =
523          Some … (sequential … (COST_LABEL … l) nxt)
524; entry_unused :
525  let entry ≝ joint_if_entry … def in
526  let code ≝ joint_if_code … def in
527  forall_statements_i …
528    (λpt,stmt.stmt_forall_labels … (λlbl.point_of_label … code lbl ≠ Some ? entry) stmt ∧
529              stmt_forall_succ … (λnxt.point_of_succ … pt nxt ≠ entry) stmt) code
530; code_is_closed : code_closed … (joint_if_code … def)
531; code_is_in_universe :
532  code_in_universe … (joint_if_code … def) (joint_if_luniverse … def)
533; regs_are_in_univers :
534  regs_in_universe … (joint_if_code … def) (joint_if_runiverse … def)
535}.
536 
537definition joint_closed_internal_function ≝
538  λp,globals.
539    Σdef : joint_internal_function p globals.good_if … def.
540
541unification hint 0 ≔ p,g ⊢
542  joint_closed_internal_function p g ≡
543  Sig (joint_internal_function p g) (λdef.good_if … def).
544
545definition set_joint_code ≝
546  λglobals: list ident.
547  λpars: params.
548  λint_fun: joint_internal_function pars globals.
549  λgraph: codeT pars globals.
550  λentry.
551  (*λexit.*)
552    mk_joint_internal_function pars globals
553      (joint_if_luniverse … int_fun) (joint_if_runiverse … int_fun) (joint_if_result … int_fun)
554      (joint_if_params … int_fun) (joint_if_stacksize … int_fun)
555       (joint_if_local_stacksize … int_fun)
556      graph entry (*exit*).
557
558definition set_luniverse ≝
559  λglobals,pars.
560  λp : joint_internal_function globals pars.
561  λluniverse: universe LabelTag.
562   mk_joint_internal_function globals pars
563    luniverse (joint_if_runiverse … p) (joint_if_result … p)
564    (joint_if_params … p) (joint_if_stacksize … p) (joint_if_local_stacksize … p)
565    (joint_if_code … p) (joint_if_entry … p) (*(joint_if_exit … p)*).
566
567definition set_runiverse ≝
568  λglobals,pars.
569  λp : joint_internal_function globals pars.
570  λruniverse: universe RegisterTag.
571   mk_joint_internal_function globals pars
572    (joint_if_luniverse … p) runiverse (joint_if_result … p)
573    (joint_if_params … p) (joint_if_stacksize … p) (joint_if_local_stacksize … p)
574    (joint_if_code … p) (joint_if_entry … p) (*(joint_if_exit … p)*).
575   
576(* Specialized for graph_params *)
577definition add_graph ≝
578  λg_pars : graph_params.λglobals.λl:label.λstmt.
579    λp:joint_internal_function g_pars globals.
580   let code ≝ add … (joint_if_code … p) l stmt in
581    mk_joint_internal_function …
582     (joint_if_luniverse … p) (joint_if_runiverse … p) (joint_if_result … p)
583     (joint_if_params … p) (joint_if_stacksize … p) (joint_if_local_stacksize … p)
584     code
585     (joint_if_entry … p)
586     (*(pi1 … (joint_if_exit … p))*).
587
588definition joint_function ≝ λp,globals. fundef (joint_closed_internal_function p globals).
589
590definition joint_program ≝
591 λp:params. program (joint_function p) nat.
592
593(* The cost model for stack consumption *)
594definition stack_cost_model ≝ list (ident × nat).
595
596definition stack_cost : ∀p:params. joint_program p → stack_cost_model ≝
597 λp,pr.
598  foldr …
599   (λid_fun,acc. let 〈id,fun〉 ≝ id_fun in
600     match fun with
601     [ Internal jif ⇒ 〈id,joint_if_stacksize ?? (pi1 … jif)〉::acc
602     | External _ ⇒ acc ])
603   [ ] (prog_funct ?? pr).
604
605definition globals_stacksize : ∀p.joint_program p → nat ≝
606 λpars,p.
607 Σ_{entry ∈ prog_vars … p}(\snd entry).
608
Note: See TracBrowser for help on using the repository browser.