1structure vars_as_resourceSyntax :> vars_as_resourceSyntax =
2struct
3
4(*
5quietdec := true;
6loadPath := (Globals.HOLDIR ^ "/examples/separationLogic/src") ::
7            !loadPath;
8
9map load ["finite_mapTheory", "vars_as_resourceTheory"];
10show_assums := true;
11*)
12
13open HolKernel Parse boolLib finite_mapTheory
14open vars_as_resourceTheory;
15open separationLogicSyntax separationLogicLib separationLogicTheory
16
17(*
18quietdec := false;
19*)
20
21fun var_res_mk_const n =
22   prim_mk_const {Name = n, Thy = "vars_as_resource"}
23
24val var_res_prop_stack_true_term =  var_res_mk_const "var_res_prop_stack_true";
25val dest_var_res_prop_stack_true = strip_comb_1 var_res_prop_stack_true_term
26val is_var_res_prop_stack_true = can dest_var_res_prop_stack_true;
27
28
29val var_res_bool_proposition_term =
30   var_res_mk_const "var_res_bool_proposition";
31val dest_var_res_bool_proposition = strip_comb_2 var_res_bool_proposition_term
32val is_var_res_bool_proposition = can dest_var_res_bool_proposition;
33
34val var_res_prop_equal_term  =
35   var_res_mk_const "var_res_prop_equal";
36val dest_var_res_prop_equal = strip_comb_3 var_res_prop_equal_term
37val is_var_res_prop_equal = can dest_var_res_prop_equal;
38fun is_var_res_prop_equal_sym e1 e2 tt =
39let
40   val (_, e1', e2') = dest_var_res_prop_equal tt;
41in
42   ((aconv e1 e1') andalso (aconv e2 e2')) orelse
43   ((aconv e1 e2') andalso (aconv e2 e1'))
44end handle HOL_ERR _ => false
45
46
47val var_res_prop_unequal_term  =
48   var_res_mk_const "var_res_prop_unequal";
49val dest_var_res_prop_unequal = strip_comb_3 var_res_prop_unequal_term
50val is_var_res_prop_unequal = can dest_var_res_prop_unequal;
51fun is_var_res_prop_unequal_sym e1 e2 tt =
52let
53   val (_, e1', e2') = dest_var_res_prop_unequal tt;
54in
55   ((aconv e1 e1') andalso (aconv e2 e2')) orelse
56   ((aconv e1 e2') andalso (aconv e2 e1'))
57end handle HOL_ERR _ => false
58
59val var_res_prop_expression_term = var_res_mk_const "var_res_prop_expression";
60val var_res_prop_binexpression_term = var_res_mk_const "var_res_prop_binexpression";
61val dest_var_res_prop_binexpression = strip_comb_5 var_res_prop_binexpression_term;
62val is_var_res_prop_binexpression = can dest_var_res_prop_binexpression
63
64val var_res_prop_binexpression_cond_term = var_res_mk_const "var_res_prop_binexpression_cond";
65val dest_var_res_prop_binexpression_cond = strip_comb_6 var_res_prop_binexpression_cond_term;
66val is_var_res_prop_binexpression_cond = can dest_var_res_prop_binexpression_cond
67
68val var_res_prog_procedure_call_term          = var_res_mk_const "var_res_prog_procedure_call"
69val var_res_prog_parallel_procedure_call_term = var_res_mk_const "var_res_prog_parallel_procedure_call"
70val var_res_prog_local_var_term               = var_res_mk_const "var_res_prog_local_var"
71val var_res_prog_call_by_value_arg_term       = var_res_mk_const "var_res_prog_call_by_value_arg"
72
73
74fun var_res_mk_local_var (var,term) =
75   mk_icomb (var_res_prog_local_var_term, mk_abs (var, term));
76
77fun dest_var_res_prog_local_var t = dest_abs (strip_comb_1 var_res_prog_local_var_term t);
78val is_var_res_prog_local_var = can dest_var_res_prog_local_var
79
80
81fun var_res_mk_call_by_value_arg ((arg,expr),term) =
82   list_mk_icomb (var_res_prog_call_by_value_arg_term, [mk_abs (arg, term), expr]);
83
84fun dest_var_res_prog_call_by_value_arg t =
85let
86   val (tt, c) = strip_comb_2 var_res_prog_call_by_value_arg_term t;
87   val (v, body) = dest_abs tt;
88in
89   (c,v,body)
90end;
91val is_var_res_prog_call_by_value_arg = can dest_var_res_prog_call_by_value_arg
92
93
94(*
95val t =
96   ``var_res_prog_call_by_value_arg
97    (\(a :holfoot_var).
98       var_res_prog_call_by_value_arg
99         (\(z :holfoot_var).
100            var_res_prog_local_var
101              (\(y :holfoot_var).
102                 var_res_prog_local_var
103                   (\(x :holfoot_var).
104                      holfoot_prog_dispose (var_res_exp_var x)))) (5 :
105         num)) (3 :num)``
106*)
107
108fun var_res_strip_local_vars t =
109    let
110       val (op_term, args) = strip_comb t;
111    in
112       if (same_const op_term var_res_prog_call_by_value_arg_term) then (
113         let
114             val (arg1, arg2) = (el 1 args, el 2 args);
115             val (v, t') = dest_abs arg1;
116             val (l, t'') = var_res_strip_local_vars t';
117         in
118             ((v,SOME arg2)::l, t'')
119         end
120       ) else if (same_const op_term var_res_prog_local_var_term) then (
121         let
122             val arg1 = el 1 args;
123             val (v, t') = dest_abs arg1;
124             val (l, t'') = var_res_strip_local_vars t';
125         in
126             ((v,NONE)::l, t'')
127         end
128       ) else ([],t)
129    end handle HOL_ERR _ => ([], t);
130
131
132fun var_res_mk_local_vars [] t = t
133  | var_res_mk_local_vars ((v, NONE)::L) t =
134       var_res_mk_local_var (v, var_res_mk_local_vars L t)
135  | var_res_mk_local_vars ((v, SOME e)::L) t =
136       var_res_mk_call_by_value_arg ((v,e), var_res_mk_local_vars L t);
137
138
139val VAR_RES_HOARE_TRIPLE_term = var_res_mk_const "VAR_RES_HOARE_TRIPLE";
140val dest_VAR_RES_HOARE_TRIPLE = strip_comb_5 VAR_RES_HOARE_TRIPLE_term;
141val is_VAR_RES_HOARE_TRIPLE = (can dest_VAR_RES_HOARE_TRIPLE);
142
143val VAR_RES_COND_HOARE_TRIPLE_term = var_res_mk_const "VAR_RES_COND_HOARE_TRIPLE";
144val dest_VAR_RES_COND_HOARE_TRIPLE = strip_comb_4 VAR_RES_COND_HOARE_TRIPLE_term;
145val is_VAR_RES_COND_HOARE_TRIPLE = (can dest_VAR_RES_COND_HOARE_TRIPLE);
146
147
148fun VAR_RES_COND_HOARE_TRIPLE___location_inc_CONV tt =
149let
150   val (f,pre,prog,post) = dest_VAR_RES_COND_HOARE_TRIPLE tt;
151   val (p1, pL) = listSyntax.dest_cons (dest_asl_prog_block prog)
152   val (c, p1') = dest_asl_comment_location p1;
153   val p1_thm = ISPECL [c, p1'] separationLogicTheory.asl_comment_location_def
154
155   val c' = separationLogicLib.asl_comment_modify_INC c;
156   val pL_thm = ((if listSyntax.is_cons pL then
157                   (RATOR_CONV o RAND_CONV) else I)
158                (asl_comment_location_INTRO_CONV c')) pL
159
160   val thm = ((RATOR_CONV o RAND_CONV o RAND_CONV)
161              ((RAND_CONV (K pL_thm)) THENC
162               ((RATOR_CONV o RAND_CONV) (K p1_thm)))) tt
163in
164   thm
165end;
166
167fun dest_VAR_RES_COND_HOARE_TRIPLE___FIRST_COMMAND tt =
168let
169   val (f,pre,prog,post) = dest_VAR_RES_COND_HOARE_TRIPLE tt;
170   val (p1, pL) = listSyntax.dest_cons (dest_asl_prog_block prog)
171in
172   (p1, pL, f, pre, post)
173end;
174
175fun dest_VAR_RES_COND_HOARE_TRIPLE___FIRST_COMMAND_location tt =
176let
177   val (f,pre,prog,post) = dest_VAR_RES_COND_HOARE_TRIPLE tt;
178   val (p1, pL) = listSyntax.dest_cons (dest_asl_prog_block prog)
179
180   val (p1', c_opt, thm_fun) = if not (is_asl_comment_location p1) then
181       (p1, NONE, fn () => REFL tt) else
182       let
183          val (c, p1') = dest_asl_comment_location p1;
184       in
185          (p1', SOME c, fn () => VAR_RES_COND_HOARE_TRIPLE___location_inc_CONV tt)
186       end
187in
188   (p1', c_opt, f, pre, post, thm_fun)
189end;
190
191fun mk_VAR_RES_COND_HOARE_TRIPLE (f, P, prog, Q) =
192list_mk_icomb (VAR_RES_COND_HOARE_TRIPLE_term, [f, P, prog, Q])
193
194
195
196val var_res_prog_assign_term = var_res_mk_const "var_res_prog_assign";
197val dest_var_res_prog_assign = strip_comb_2 var_res_prog_assign_term;
198val is_var_res_prog_assign = (can dest_var_res_prog_assign);
199
200
201
202val var_res_prog_aquire_lock_internal_term  = var_res_mk_const "var_res_prog_aquire_lock_internal";
203val var_res_prog_release_lock_internal_term = var_res_mk_const "var_res_prog_release_lock_internal";
204val var_res_prog_aquire_lock_term           = var_res_mk_const "var_res_prog_aquire_lock";
205val var_res_prog_release_lock_term          = var_res_mk_const "var_res_prog_release_lock";
206val var_res_prog_aquire_lock_input_term     = var_res_mk_const "var_res_prog_aquire_lock_input";
207val var_res_prog_release_lock_input_term    = var_res_mk_const "var_res_prog_release_lock_input";
208
209val dest_var_res_prog_release_lock_input = strip_comb_3 var_res_prog_release_lock_input_term;
210val is_var_res_prog_release_lock_input = (can dest_var_res_prog_release_lock_input)
211
212val dest_var_res_prog_release_lock_internal = strip_comb_3 var_res_prog_release_lock_internal_term;
213val is_var_res_prog_release_lock_internal = (can dest_var_res_prog_release_lock_internal)
214
215val dest_var_res_prog_release_lock = strip_comb_3 var_res_prog_release_lock_term;
216val is_var_res_prog_release_lock = (can dest_var_res_prog_release_lock)
217
218val dest_var_res_prog_aquire_lock_input = strip_comb_4 var_res_prog_aquire_lock_input_term;
219val is_var_res_prog_aquire_lock_input = (can dest_var_res_prog_aquire_lock_input)
220
221val dest_var_res_prog_aquire_lock_internal = strip_comb_4 var_res_prog_aquire_lock_internal_term;
222val is_var_res_prog_aquire_lock_internal = (can dest_var_res_prog_aquire_lock_internal)
223
224val dest_var_res_prog_aquire_lock = strip_comb_4 var_res_prog_aquire_lock_term;
225val is_var_res_prog_aquire_lock = (can dest_var_res_prog_aquire_lock)
226
227
228
229val var_res_prog_eval_expressions_term = var_res_mk_const "var_res_prog_eval_expressions";
230val dest_var_res_prog_eval_expressions = strip_comb_2 var_res_prog_eval_expressions_term;
231val is_var_res_prog_eval_expressions = can dest_var_res_prog_eval_expressions;
232
233
234
235
236val var_res_cond_best_local_action_term =  var_res_mk_const "var_res_cond_best_local_action";
237val dest_var_res_cond_best_local_action = strip_comb_3 var_res_cond_best_local_action_term;
238val is_var_res_cond_best_local_action = (can dest_var_res_cond_best_local_action);
239
240val var_res_best_local_action_term =  var_res_mk_const "var_res_best_local_action";
241val dest_var_res_best_local_action = strip_comb_3 var_res_best_local_action_term;
242val is_var_res_best_local_action = (can dest_var_res_best_local_action);
243
244
245val var_res_prog_best_local_action_term = var_res_mk_const "var_res_prog_best_local_action";
246val dest_var_res_prog_best_local_action = strip_comb_2 var_res_prog_best_local_action_term;
247val is_var_res_prog_best_local_action = (can dest_var_res_prog_best_local_action);
248
249val var_res_prog_quant_best_local_action_term = var_res_mk_const "var_res_prog_quant_best_local_action";
250val dest_var_res_prog_quant_best_local_action = strip_comb_2 var_res_prog_quant_best_local_action_term;
251val is_var_res_prog_quant_best_local_action = (can dest_var_res_prog_quant_best_local_action);
252
253
254val var_res_prog_cond_best_local_action_term = var_res_mk_const "var_res_prog_cond_best_local_action";
255val dest_var_res_prog_cond_best_local_action = strip_comb_2 var_res_prog_cond_best_local_action_term;
256val is_var_res_prog_cond_best_local_action = (can dest_var_res_prog_cond_best_local_action);
257
258val var_res_prog_cond_quant_best_local_action_term = var_res_mk_const "var_res_prog_cond_quant_best_local_action";
259val dest_var_res_prog_cond_quant_best_local_action = strip_comb_2 var_res_prog_cond_quant_best_local_action_term;
260val is_var_res_prog_cond_quant_best_local_action = (can dest_var_res_prog_cond_quant_best_local_action);
261
262
263val dest_var_res_prog_procedure_call = strip_comb_2 var_res_prog_procedure_call_term;
264val is_var_res_prog_procedure_call = (can dest_var_res_prog_procedure_call);
265
266val dest_var_res_prog_parallel_procedure_call = strip_comb_4 var_res_prog_parallel_procedure_call_term;
267val is_var_res_prog_parallel_procedure_call = (can dest_var_res_prog_parallel_procedure_call);
268
269
270val var_res_map_term = var_res_mk_const "var_res_map";
271
272val var_res_prop_internal_term = var_res_mk_const "var_res_prop_internal";
273fun dest_var_res_prop_internal tt =
274  let
275     val (f,wpbrpb,wprp,d,sfb,P) = strip_comb_6 var_res_prop_internal_term tt;
276     val (wpb, rpb) = safe_dest_pair wpbrpb;
277     val (wp, rp) = safe_dest_pair wprp;
278  in
279    (f,wpb,rpb,wp,rp,d,sfb,P)
280  end;
281val is_var_res_prop_internal = (can dest_var_res_prop_internal);
282
283
284val var_res_prop_term = var_res_mk_const "var_res_prop";
285fun dest_var_res_prop tt =
286  let
287     val (f, wprp, sfb) = strip_comb_3 var_res_prop_term tt;
288     val (wp, rp) = safe_dest_pair wprp;
289  in
290    (f, wp,rp, sfb)
291  end
292val is_var_res_prop = (can dest_var_res_prop);
293
294fun dest_var_res_prop___propL tt =
295let
296   val (f, wp, rp, sfb) = dest_var_res_prop tt;
297   val sfs = fst (bagSyntax.dest_bag sfb);
298in
299   (f, wp, rp, sfb, sfs)
300end;
301
302
303val var_res_implies_unequal_term = var_res_mk_const "var_res_implies_unequal"
304val dest_var_res_implies_unequal = strip_comb_4 var_res_implies_unequal_term
305val is_var_res_implies_unequal = can dest_var_res_implies_unequal
306
307fun is_var_res_implies_unequal_sym e1 e2 tt =
308let
309   val (_, _, e1', e2') = dest_var_res_implies_unequal tt;
310in
311   ((aconv e1 e1') andalso (aconv e2 e2')) orelse
312   ((aconv e1 e2') andalso (aconv e2 e1'))
313end handle HOL_ERR _ => false
314
315
316val var_res_prop_implies_term = var_res_mk_const "var_res_prop_implies"
317fun dest_var_res_prop_implies tt =
318  let
319     val (f, wprp, sfb, sfb') = strip_comb_4 var_res_prop_implies_term tt;
320     val (wp, rp) = safe_dest_pair wprp;
321  in
322    (f, wp,rp, sfb,sfb')
323  end
324val is_var_res_prop_implies = (can dest_var_res_prop_implies);
325
326val var_res_prop_implies_eq_term = var_res_mk_const "var_res_prop_implies_eq"
327fun dest_var_res_prop_implies_eq tt =
328  let
329     val (f, wprp, sfb, sfb1, sfb1') = strip_comb_5 var_res_prop_implies_eq_term tt;
330     val (wp, rp) = safe_dest_pair wprp;
331  in
332    (f, wp,rp, sfb,sfb1,sfb1')
333  end
334val is_var_res_prop_implies_eq = (can dest_var_res_prop_implies_eq);
335
336
337val var_res_prop_input_ap_distinct_term = var_res_mk_const "var_res_prop_input_ap_distinct";
338val dest_var_res_prop_input_ap_distinct = strip_comb_4 var_res_prop_input_ap_distinct_term;
339val is_var_res_prop_input_ap_distinct = (can dest_var_res_prop_input_ap_distinct);
340val var_res_prop_input_ap_term = var_res_mk_const "var_res_prop_input_ap";
341
342
343val var_res_prop_input_distinct_term = var_res_mk_const "var_res_prop_input_distinct";
344val dest_var_res_prop_input_distinct = strip_comb_4 var_res_prop_input_distinct_term;
345val is_var_res_prop_input_distinct = (can dest_var_res_prop_input_distinct);
346
347
348
349val VAR_RES_FRAME_SPLIT_term = var_res_mk_const "VAR_RES_FRAME_SPLIT";
350val dest_VAR_RES_FRAME_SPLIT = strip_comb_8 VAR_RES_FRAME_SPLIT_term;
351val is_VAR_RES_FRAME_SPLIT = (can dest_VAR_RES_FRAME_SPLIT);
352
353
354val VAR_RES_PROP_IS_EQUIV_FALSE_term = var_res_mk_const "VAR_RES_PROP_IS_EQUIV_FALSE";
355val dest_VAR_RES_PROP_IS_EQUIV_FALSE = strip_comb_4 VAR_RES_PROP_IS_EQUIV_FALSE_term;
356val is_VAR_RES_PROP_IS_EQUIV_FALSE = (can dest_VAR_RES_PROP_IS_EQUIV_FALSE);
357
358
359val VAR_RES_IS_STACK_IMPRECISE___USED_VARS_term = var_res_mk_const "VAR_RES_IS_STACK_IMPRECISE___USED_VARS";
360val dest_VAR_RES_IS_STACK_IMPRECISE___USED_VARS = strip_comb_2 VAR_RES_IS_STACK_IMPRECISE___USED_VARS_term;
361val is_VAR_RES_IS_STACK_IMPRECISE___USED_VARS = can dest_VAR_RES_IS_STACK_IMPRECISE___USED_VARS;
362fun mk_VAR_RES_IS_STACK_IMPRECISE___USED_VARS vs p =
363   list_mk_icomb (VAR_RES_IS_STACK_IMPRECISE___USED_VARS_term, [vs,p])
364
365
366val VAR_RES_IS_STACK_IMPRECISE_term = var_res_mk_const "VAR_RES_IS_STACK_IMPRECISE";
367val dest_VAR_RES_IS_STACK_IMPRECISE = strip_comb_1 VAR_RES_IS_STACK_IMPRECISE_term;
368val is_VAR_RES_IS_STACK_IMPRECISE = can dest_VAR_RES_IS_STACK_IMPRECISE;
369
370
371fun inst_type_to_type ty t =
372   inst (match_type (type_of t) ty) t
373
374val var_res_exp_var_term  = var_res_mk_const "var_res_exp_var";
375val dest_var_res_exp_var = strip_comb_1 var_res_exp_var_term;
376val is_var_res_exp_var = can dest_var_res_exp_var;
377
378fun mk_var_res_exp_var v ty =
379   inst_type_to_type ty (mk_icomb(var_res_exp_var_term, v));
380
381val var_res_exp_const_term = var_res_mk_const "var_res_exp_const";
382val dest_var_res_exp_const = strip_comb_1 var_res_exp_const_term;
383val is_var_res_exp_const = can dest_var_res_exp_const;
384fun mk_var_res_exp_const c ty = inst_type_to_type ty
385   (mk_icomb(var_res_exp_const_term, c));
386
387
388val var_res_exp_op_term = var_res_mk_const "var_res_exp_op";
389
390val var_res_exp_binop_term = var_res_mk_const "var_res_exp_binop";
391val dest_var_res_exp_binop = strip_comb_2 var_res_exp_binop_term;
392val is_var_res_exp_binop = can dest_var_res_exp_binop;
393
394
395val VAR_RES_IS_PURE_PROPOSITION_term = var_res_mk_const "VAR_RES_IS_PURE_PROPOSITION";
396val dest_VAR_RES_IS_PURE_PROPOSITION = strip_comb_2 VAR_RES_IS_PURE_PROPOSITION_term;
397val is_VAR_RES_IS_PURE_PROPOSITION = can dest_VAR_RES_IS_PURE_PROPOSITION;
398fun mk_VAR_RES_IS_PURE_PROPOSITION f p =
399   list_mk_icomb (VAR_RES_IS_PURE_PROPOSITION_term, [f, p])
400
401
402val var_res_pred_term = var_res_mk_const "var_res_pred";
403val var_res_pred_bin_term = var_res_mk_const "var_res_pred_bin";
404
405fun string2num_var s = mk_var(s, numLib.num);
406
407
408val var_res_prop___var_eq_const_BAG_term =
409   var_res_mk_const "var_res_prop___var_eq_const_BAG"
410
411val var_res_prop_varlist_update_term =
412   var_res_mk_const "var_res_prop_varlist_update"
413val dest_var_res_prop_varlist_update = strip_comb_2 var_res_prop_varlist_update_term
414val is_var_res_prop_varlist_update = can dest_var_res_prop_varlist_update
415
416val var_res_exp_varlist_update_term =
417   var_res_mk_const "var_res_exp_varlist_update"
418val dest_var_res_exp_varlist_update = strip_comb_2 var_res_exp_varlist_update_term
419val is_var_res_exp_varlist_update = can dest_var_res_exp_varlist_update
420
421
422fun dest_var_res_state_type ty =
423   let
424      val (var_ty, x) = finite_mapSyntax.dest_fmap_ty ty
425      val (data_ty, _) = pairSyntax.dest_prod x
426   in
427      (var_ty, data_ty)
428   end;
429
430fun dest_var_res_expr_type ty =
431   dest_var_res_state_type (hd (fst (strip_fun ty)))
432
433local
434   val org_ty = Type `:('a, 'b, 'a) var_res_expression`;
435in
436fun mk_var_res_expr_type state_ty =
437let
438   val (var_ty, data_ty) = dest_var_res_state_type state_ty
439in
440   type_subst [beta |-> var_ty, alpha |-> data_ty] org_ty
441end;
442end
443
444fun dest_var_res_ext_state_type ty =
445   pairSyntax.dest_prod ty
446
447fun dest_var_res_proposition ty =
448   (hd (fst (strip_fun ty)))
449
450fun dest_var_res_cond_proposition ty =
451   dest_var_res_proposition (snd (pairSyntax.dest_prod ty))
452
453
454
455
456
457
458
459end
460