/
heapstuffold.ml
767 lines (688 loc) · 23.6 KB
/
heapstuffold.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
(* The heap manipulation functions, plus abtraction rules and other goodies *)
open Global;;
open Gensym;;
open Parsetree;;
type pure_heap = Eq of exp * exp | Topp;;
(* the error is a really bad hack to show top *)
type spatial_heap = Pointsto of exp * exp | ListSeg of exp * exp | Junk |Emp | Tops;;
type symb_heap = pure_heap list * spatial_heap list ;;
let make_pure_heap exp1 exp2 = Eq(exp1,exp2);;
let rec has_tops sp = match sp with
| [] -> ([],[])
| Tops::tl -> ([Topp],[Tops])
| _::tl -> has_tops tl;;
let rec make_pure_heap_list = function
| [] -> []
| (e1,e2)::tl -> (Eq(e1,e2))::(make_pure_heap_list tl);;
let rec make_pure_heap_list2 = function
| [] -> []
| (e1,e2)::tl -> (Eq(Var(e1),Var(e2)))::(make_pure_heap_list2 tl);;
let isprimevar e = match e with
| VarPrime(x) -> true
| _ -> false;;
(*
let convert_exp (e:expl) =
match e with
Varl(v) -> Var(v);;
*)
(* List of primed variables in a sym heap *)
(*let rec varsprime symheap =
let (pure,spatial) = symheap in
let chooser e e' = if ((isprimevar e) & (isprimevar e')) then
[e;e']
else if (isprimevar e)
[e]
else if (isprimevar e')
[e']
else []
in
let rec _varsp_pure p = match p with
| [] -> []
| Eq(e,e')::tl -> (chooser e e')@(_varsp_pure tl)
in
let rec _varsp_spatial p = match p with
| [] -> []
| Pointsto(e,e')::tl -> (chooser e e')@(_varsp_spatial tl)
| ListSeg(e,e')::tl -> (chooser e e')@(_varsp_spatial tl)
in
(_varsp_pure pure )@(_varsp_spatial spatial);;
*)
(* Substitution for expressions *)
let subst_exp e1 e2 exp = match exp with
Var(e1) -> Var(e2)
| VarPrime(e1) -> VarPrime(e2)
| Null -> Null;;
let subst_helper oldvar newvar exp1 exp2 =
let a = (exp1 = oldvar) in
let b = (exp2 = oldvar) in
if (a & b) then (newvar,newvar)
else if a then (newvar,exp2)
else if b then (exp1,newvar)
else (exp1,exp2);;
(* Substitution in pure heap *)
let rec subst_pure_heap e1 e2 pure_heap =
match pure_heap with
[] -> []
| Eq(e,e')::tl -> let (newe,newe') = subst_helper e1 e2 e e' in
(Eq(newe,newe'))::subst_pure_heap e1 e2 tl;;
(* Substitution is spatial heap *)
let rec subst_spatial_heap e1 e2 spatial_heap =
match spatial_heap with
[] -> []
| Pointsto(e,e')::tl -> let (newe,newe') = subst_helper e1 e2 e e' in
(Pointsto(newe,newe'))::subst_spatial_heap e1 e2 tl
| ListSeg(e,e')::tl -> let (newe,newe') = subst_helper e1 e2 e e' in
(ListSeg(newe,newe'))::subst_spatial_heap e1 e2 tl
| Junk::tl -> (Junk)::(subst_spatial_heap e1 e2 tl);;
let rec find_directly_equiv pure_heap exp =
match pure_heap with
[] -> []
| Eq(e,e')::tl -> if (e = exp) then e'::(find_directly_equiv tl exp)
else if (e' = exp) then e::(find_directly_equiv tl exp)
else find_directly_equiv tl exp;;
(* very naive way of finding the equivelance class of an expression in the pure heap *)
let rec equivclass exp pure_heap =
let dir_eqv_lst = find_directly_equiv pure_heap exp in
let rec f lst seen =
match lst with
[] -> []
| g::tl -> if (not (List.mem g seen)) then
let t1 = find_directly_equiv pure_heap g in
let t3 = tl@t1 in
let t2 = f t3 (g::seen) in
g::(t2@t1)
else f tl seen
in
dir_eqv_lst@(f dir_eqv_lst []);;
let var_equivelant pureheap var1 var2 =
if (var1 = var2) then true else
let var1_equivclass = equivclass var1 pureheap in
List.mem var2 var1_equivclass;;
(* sorts a symbolic heap, placing pointso to preds first, then lsegs then junk *)
let sortsymheap sy =
let (p,s) = sy in
let rec _sep_parts lst pts lseg junk =
match lst with
| [] -> (pts,lseg,junk)
| Pointsto(e,e')::tl -> _sep_parts tl (Pointsto(e,e')::pts) lseg junk
| ListSeg(e,e')::tl -> _sep_parts tl pts (ListSeg(e,e')::lseg) junk
| Junk::tl -> _sep_parts tl pts lseg (Junk::junk)
in
let (s1,s2,s3) = _sep_parts s [] [] [] in
(p,(s1@s2@s3));;
(* alpha equivelance of two symbolic heaps *)
let alpha_eq t1 t2 =
let h = Hashtbl.create 100 in
let h' = Hashtbl.create 100 in
let map x y =
let y' = try Hashtbl.find h x
with Not_found -> begin Hashtbl.add h x y;y end
in
let x' = try Hashtbl.find h' y
with Not_found -> begin Hashtbl.add h' y x;x end
in
x = x' & y = y'
in
let forall2 f x y = try List.for_all2 f x y
with Invalid_argument _ -> false
in
let rec compare_spatial t1 t2 =
match (t1,t2) with
| (Pointsto(x,y),Pointsto(u,v)) -> (map x u) & (map y v)
| (ListSeg(x,y),ListSeg(u,v)) -> (map x u) & (map y v)
| _ -> false
in
let rec compare_pure t1 t2 =
match(t1,t2) with
| (Eq(x,y),Eq(u,v)) -> (map x u) & (map y v)
| _ -> false
in
let (p1,s1) = sortsymheap t1 in
let (p2,s2) = sortsymheap t2 in
(forall2 compare_pure p1 p2) & (forall2 compare_spatial s1 s2);;
let alpha_equiv t1 t2 =
if (alpha_eq t1 t2) then 0
else 1;;
(* Set for symheaps *)
module SymheapSet = Set.Make(struct
type t = symb_heap
let compare = alpha_equiv
end);;
(* removes duplicates symheaps *)
(* this is the ugliest hack I have ever done. if u dont feel sick after understanding this, then something is wrong with u *)
let remove_duplicates lstsymheap =
let rec _rem lst set = match lst with
| [] -> set
| e::tl -> _rem tl (SymheapSet.add e set)
in
SymheapSet.elements (_rem lstsymheap SymheapSet.empty);;
(* splits a spatial heap up into three parts, pts lseg and junk *)
let split_spatial s1 =
let rec _split s pts lseg junk =
match s with
| [] -> (pts,lseg,junk)
| Pointsto(e,e')::tl -> _split tl (Pointsto(e,e')::pts) lseg junk
| ListSeg(e,e')::tl -> _split tl pts (ListSeg(e,e')::lseg) junk
| Junk::tl -> _split tl pts lseg (Junk::junk)
in
_split s1 [] [] [];;
(* checks if sh is a member of the list using alpha_eq *)
let rec is_member sh lst =
match lst with
| [] -> false
| sh'::tl -> if (alpha_eq sh sh') then
true
else
is_member sh tl;;
let rec subset lst1 lst2 =
match lst1 with
| [] -> true
| sh::tl -> if (is_member sh lst2) then
subset tl lst2
else false;;
(* Abtraction rules *)
(* First some helper functions *)
let check_subst_st1 c =
match c with
| Eq(_,VarPrime(e)) -> true
| _ -> false;;
let check_subst_st2 c =
match c with
| Eq(VarPrime(e),_) -> true
| _ -> false;;
(* produce the transitive closure of the pure heap given *)
let rec transitive_close pure =
let rec makesymheap lst = match lst with
[] -> []
| e :: tl -> ([e],[Junk])::(makesymheap tl)
in
let pairup e e' = Eq(e',e) in
let _tran lst = match lst with
| [] -> []
| Eq(e,e')::tl -> let e_equiv_class = equivclass e pure in
let e'_equiv_class = equivclass e' pure in
[Eq(e,e')]@(List.map (pairup e') e_equiv_class)@(List.map (pairup e) e'_equiv_class)@(transitive_close tl)
| Topp::tl -> [Topp]@(transitive_close tl)
in
List.flatten (List.map fst (remove_duplicates (makesymheap (_tran pure))));;
(* TODO *)
(* ST1 and ST2 rules *)
let st1_st2 symheap =
let perform_subst eqexpr spheap =
match eqexpr with
(* maybe i should make sure e' is not a primed var?*)
| Eq(VarPrime(e),e') -> subst_spatial_heap (VarPrime(e)) e' spheap
| Eq(e',VarPrime(e)) -> subst_spatial_heap (VarPrime(e)) e' spheap
| _ -> spheap (* shouldnt happen! *)
in
let (pure,spatial) = symheap in
let spref = ref spatial in
let newpref = ref [] in
let loop_ptr = ref pure in
let c = ref (Eq(Var("x"),Var("y"))) in
while (not ((List.length !loop_ptr) = 0)) do
begin
c := List.hd !loop_ptr;
if (check_subst_st1 !c) then
begin
spref := (perform_subst !c !spref);
end
else if (check_subst_st2 !c) then
begin
spref := (perform_subst !c !spref);
end
else
begin
(* if no sub is to be made, add to new pure heap *)
newpref := (!c::(!newpref))
end;
loop_ptr := (List.tl !loop_ptr)
end
done;
(!newpref,!spref)
(* GB1 rule *)
(* we can exploit the fact that for this rule to work, the primed
variable needs to occur at least twice for it not to be junk. *)
let gb1 symheap =
let (pure,spatial) = symheap in
let filtervar_sp var spa_expr =
match spa_expr with
| Pointsto(e,e') -> (e = var) || (e' = var)
| ListSeg(e,e') -> (e = var) || (e' = var)
| Junk -> false
in
let filtervar_pure var pure_exp =
match pure_exp with
| Eq(e,e') -> (e = var) || (e' = var)
in
let is_garbage var =
let a = List.length (List.filter (filtervar_sp var) (spatial)) in
let b = List.length (List.filter (filtervar_pure var) (pure)) in
not ((a+b)>= 2)
in
let rec _gb1 spatial =
match spatial with
| [] -> []
| ListSeg(VarPrime(v),e)::tl -> if (is_garbage (VarPrime(v))) then
_gb1 tl
else
(ListSeg(VarPrime(v),e))::(_gb1 tl)
| Pointsto(VarPrime(v),e)::tl -> if (is_garbage (VarPrime(v))) then
_gb1 tl
else
(Pointsto(VarPrime(v),e))::(_gb1 tl)
| hd::tl -> hd::(_gb1 tl)
in
(pure,(_gb1 spatial));;
(* GB2 rule *)
let gb2 symheap =
let (pure,spatial) = symheap in
let filtervar_sp var spa_expr =
match spa_expr with
| Pointsto(e,e') -> (e = var) || (e' = var)
| ListSeg(e,e') -> (e = var) || (e' = var)
| Junk -> false
in
let filtervar_pure var pure_exp =
match pure_exp with
| Eq(e,e') -> (e = var) || (e' = var)
in
let is_garbage var =
let a = List.length (List.filter (filtervar_sp var) (spatial)) in
let b = List.length (List.filter (filtervar_pure var) (pure)) in
not ((a+b)> 2)
in
(* Takes an expression, and checks if the spatial list has the given expression.
If it does then it removes the given expression and returns the rest of the list
*)
let rec has_opposite e spatiallist =
match spatiallist with
| [] -> (false,[])
| e'::tl -> if (e = e') then
let (b,r) = has_opposite e tl in
(true,r)
else
let (b,r) = has_opposite e tl in
(b,(e'::r))
in
let rec _gb2 sp =
match sp with
| [] -> []
| Pointsto(VarPrime(v),VarPrime(v'))::tl ->
(* Makes the opposite spatial exp and removes it from the rest of the list *)
let opposite = (Pointsto(VarPrime(v'), VarPrime(v))) in
let (hasopposite,result) = has_opposite opposite tl in
(* This now checks if the current spatial expression is garbage *)
let isjunk1 = is_garbage (VarPrime(v)) in
let isjunk2 = is_garbage (VarPrime(v')) in
(* if the opposite expr was in the list it removes it *)
if (hasopposite & isjunk1 & isjunk2) then _gb2 result
else (Pointsto(VarPrime(v),VarPrime(v')))::(_gb2 result)
| ListSeg(VarPrime(v),VarPrime(v'))::tl ->
let opposite = (ListSeg(VarPrime(v'), VarPrime(v))) in
let (hasopposite,result) = has_opposite opposite tl in
let isjunk1 = is_garbage (VarPrime(v)) in
let isjunk2 = is_garbage (VarPrime(v')) in
if (hasopposite & isjunk1 & isjunk2) then _gb2 tl
else (Pointsto(VarPrime(v),VarPrime(v')))::(_gb2 tl)
| e::tl -> e::(_gb2 tl)
in
(pure,_gb2 spatial);;
let abs1 symheap =
let (pure,spatial) = symheap in
let filtervar_sp var spa_expr =
match spa_expr with
| Pointsto(e,e') -> (e = var) || (e' = var)
| ListSeg(e,e') -> (e = var) || (e' = var)
| Junk -> false
in
let filtervar_pure var pure_exp =
match pure_exp with
| Eq(e,e') -> (e = var) || (e' = var)
in
let is_garbage var =
let a = List.length (List.filter (filtervar_sp var) (spatial)) in
let b = List.length (List.filter (filtervar_pure var) (pure)) in
not ((a+b)> 2)
in
let rec findalloc var oldheap newheap =
match oldheap with
| [] -> (var,false,[])
| Pointsto(e,e')::tl -> let b1 = (e = var) in
let b3 = not (e' = var) in
let b2 = var_equivelant pure e' Null in
if (b1 & b2 & b3 & (is_garbage var)) then
(Null,true,newheap@tl)
else
findalloc var tl (Pointsto(e,e')::newheap)
| ListSeg(e,e')::tl -> let b1 = (e = var) in
let b3 = not (e' = var) in
let b2 = var_equivelant pure e' Null in
if (b1 & b2 & b3 & (is_garbage var)) then
(Null,true,newheap@tl)
else
findalloc var tl (ListSeg(e,e')::newheap)
| Junk::tl -> findalloc var tl (Junk::newheap)
in
let rec _abs1 oldspatial newspatial=
match oldspatial with
| [] -> newspatial
| ListSeg(e,VarPrime(v))::tl -> let b1 = not (e = VarPrime(v)) in
let (var,b2,newlst) = findalloc (VarPrime(v)) tl [] in
if (b2 & b1) then
ListSeg(e,var)::(newlst@newspatial)
else
let (var2,b3,newlst2) = findalloc (VarPrime(v)) newspatial [] in
if (b3 & b1) then
ListSeg(e,var2)::(newlst2@tl)
else
_abs1 tl ((ListSeg(e,VarPrime(v)))::newspatial)
| Pointsto(e,VarPrime(v))::tl -> let b1 = not (e = VarPrime(v)) in
let (var,b2,newlst) = findalloc (VarPrime(v)) tl [] in
if (b2 & b1) then
ListSeg(e,var)::(newlst@newspatial)
else
let (var2,b3,newlst2) = findalloc (VarPrime(v)) newspatial [] in
if (b3 & b1) then
ListSeg(e,var2)::(newlst2@tl)
else
(_abs1 tl ((Pointsto(e,VarPrime(v)))::newspatial))
| e::tl -> (_abs1 tl (e::newspatial))
in
(pure,(_abs1 spatial []));;
let abs2 symheap =
let (pure,spatial) = symheap in
let filtervar_sp var spa_expr =
match spa_expr with
| Pointsto(e,e') -> (e = var) || (e' = var)
| ListSeg(e,e') -> (e = var) || (e' = var)
| Junk -> false
in
let filtervar_pure var pure_exp =
match pure_exp with
| Eq(e,e') -> (e = var) || (e' = var)
in
let is_garbage var =
let a = List.length (List.filter (filtervar_sp var) (spatial)) in
let b = List.length (List.filter (filtervar_pure var) (pure)) in
not ((a+b)> 2)
in
let rec check_allocated var spheap =
let varequivclass = equivclass var pure in
match spheap with
| [] -> false
| Pointsto(e,e')::tl -> if ((e = var) || (List.mem e varequivclass)) then
true
else
check_allocated var tl
| ListSeg(e,e')::tl -> if ((e = var) ||( List.mem e varequivclass)) then
true
else
check_allocated var tl
| Junk::tl -> check_allocated var tl
in
let rec findalloc var oldheap newheap =
match oldheap with
| [] -> (var,false,[])
| Pointsto(e,e')::tl -> let b1 = (e = var) in
let b3 = not (e' = var) in
let b2 = check_allocated e' (newheap@tl) in
if (b1 & b2 & b3 & (is_garbage var)) then
(e',true,newheap@tl)
else
findalloc var tl (Pointsto(e,e')::newheap)
| ListSeg(e,e')::tl -> let b1 = (e = var) in
let b3 = not (e' = var) in
let b2 = check_allocated e' (newheap@tl) in
if (b1 & b2 & b3 & (is_garbage var)) then
(e',true,newheap@tl)
else
findalloc var tl (ListSeg(e,e')::newheap)
| Junk::tl -> findalloc var tl (Junk::newheap)
in
let rec _abs2 oldspatial newspatial=
match oldspatial with
| [] -> newspatial
| ListSeg(e,VarPrime(v))::tl -> let b1 = not (e = VarPrime(v)) in
let (var,b2,newlst) = findalloc (VarPrime(v)) tl [] in
(* check if v' is in the tl and we can remove it*)
if (b2 & b1) then
ListSeg(e,var)::(newlst@newspatial)
else
(* otherwise check the 'seen' elements of the heap *)
let (var2,b3,newlst2) = findalloc (VarPrime(v)) newspatial [] in
if (b3 & b1) then
ListSeg(e,var2)::(newlst2@tl)
else
(* otherwise we cant apply the rule *)
(_abs2 tl ((ListSeg(e,VarPrime(v)))::newspatial))
| Pointsto(e,VarPrime(v))::tl -> let b1 = not (e = VarPrime(v)) in
(* check if v' is in the tl and we can remove it*)
let (var,b2,newlst) = findalloc (VarPrime(v)) tl [] in
if (b2 & b1) then
ListSeg(e,var)::(newlst@newspatial)
else
(* otherwise check the 'seen' elements of the heap *)
let (var2,b3,newlst2) = findalloc (VarPrime(v)) newspatial [] in
if (b3 & b1) then
ListSeg(e,var2)::(newlst2@tl)
else
(* otherwise we cant apply the rule *)
(_abs2 tl ((Pointsto(e,VarPrime(v)))::newspatial))
| e::tl -> _abs2 tl (e::newspatial)
in
(pure,(_abs2 spatial []));;
let apply_abstraction symheap =
let continue = ref true in
let syhref = ref symheap in
let syhrefold = ref symheap in
while (!continue) do
syhref := gb2(abs2(abs1(gb1 (st1_st2 symheap))));
if (!syhref = !syhrefold) then
continue := false
else
syhrefold := !syhref
done;
!syhref;;
(* Querying Rules *)
let symheap_allocated var symheap =
let (pure,spatial) = symheap in
let rec _alloc var spatial =
match spatial with
| [] -> false
| ListSeg(var,_)::tl -> true
| Pointsto(var,_)::tl -> true
| _::tl -> _alloc var tl
in
_alloc var spatial;;
let symheap_circ_lseg symheap =
let (pure,spatial) = symheap in
let rec _chk_circ sp = match sp with
| [] -> false
| ListSeg(e,f)::tl -> if (var_equivelant pure e f) then
true
else
_chk_circ tl
| _::tl -> _chk_circ tl
in
_chk_circ spatial;;
(* move this function to misc or somwhere *)
let list_remove lst elem =
let f e = not (elem = e) in
List.filter f lst;;
let symheap_alloc_twice symheap =
let (pure,spatial) = symheap in
let rec _chk_alloc varlist sp =
match sp with
| [] -> false
| ListSeg(e,_)::tl -> if (List.mem e varlist) then true
else _chk_alloc varlist tl
| Pointsto(e,_)::tl -> if (List.mem e varlist) then true
else _chk_alloc varlist tl
| Junk::tl -> _chk_alloc varlist tl
in
let rec _chk_alloc_twice sp vars_seen =
match sp with
| [] -> false
| ListSeg(e,_)::tl -> if (not (List.mem e vars_seen)) then
let equiv_class = list_remove (equivclass e pure) e in
if (_chk_alloc equiv_class spatial) then
true
else
_chk_alloc_twice tl (e::vars_seen)
else _chk_alloc_twice tl vars_seen
| Pointsto(e,_)::tl -> if (not (List.mem e vars_seen)) then
let equiv_class = list_remove (equivclass e pure) e in
if (_chk_alloc equiv_class spatial) then
true
else
_chk_alloc_twice tl (e::vars_seen)
else _chk_alloc_twice tl vars_seen
| Junk::tl -> _chk_alloc_twice tl vars_seen
in
_chk_alloc_twice spatial [];;
let rec remove_empty_symheaps lstsymheap =
match lstsymheap with
| [] -> []
| ([],[])::tl -> remove_empty_symheaps tl
| s::tl -> s::(remove_empty_symheaps tl);;
let symheap_asserts_false symheap =
let (pure,spatial) = symheap in
let nil_eq_class = equivclass Null pure in
let rec _chk_nil_alloc sp = match sp with
| [] -> false
| ListSeg(e,_)::tl -> if (List.mem e nil_eq_class) then
if (symheap_allocated e symheap) then
true
else _chk_nil_alloc tl
else
_chk_nil_alloc tl
| Pointsto(e,_)::tl -> if (List.mem e nil_eq_class) then
if (symheap_allocated e symheap) then
true
else _chk_nil_alloc tl
else
_chk_nil_alloc tl
| Junk::tl -> _chk_nil_alloc tl
in
(_chk_nil_alloc spatial) || (symheap_alloc_twice symheap) || (symheap_circ_lseg symheap);;
(* Rearrangement Rules *)
let create_freshvar () =
let varstring = Gensym.next "x" in
VarPrime(varstring);;
let rec find_allocated var symheap =
match symheap with
| [] -> (false, Null)
| Pointsto(e,e')::tl -> if (e = var) then
(true,e')
else
find_allocated var tl
| ListSeg(e,e')::tl -> if (e = var) then
(true,e')
else
find_allocated var tl
| Junk::tl -> find_allocated var tl;;
let rec dispose var oldspatial newspatial =
match oldspatial with
| [] -> [Tops]
| Pointsto(e,e')::tl -> if (e = var) then
newspatial@tl
else
dispose var tl (Pointsto(e,e')::newspatial)
| ListSeg(e,e')::tl -> if (e = var) then
newspatial@tl
else
dispose var tl (ListSeg(e,e')::newspatial)
| Junk::tl -> dispose var tl (Junk::newspatial);;
let rec mutate var mutatedvar oldspatial newspatial =
match oldspatial with
| [] -> [Tops]
| Pointsto(e,e')::tl -> if (e = var) then
newspatial@(Pointsto(e,mutatedvar)::tl)
else
mutate var mutatedvar tl (Pointsto(e,e')::newspatial)
| ListSeg(e,e')::tl -> if (e = var) then
newspatial@(ListSeg(e,mutatedvar)::tl)
else
mutate var mutatedvar tl (ListSeg(e,e')::newspatial)
| Junk::tl -> mutate var mutatedvar tl (Junk::newspatial);;
let rearrange_name comm symheap =
let (pure,spatial) = symheap in
let rec _rearrange oldvar newvar oldspatial newspatial =
match oldspatial with
| [] -> (false,newspatial)
| Pointsto(e,e')::tl -> if e = oldvar then
(true,(newspatial@(Pointsto(newvar,e')::tl)))
else
_rearrange oldvar newvar tl (Pointsto(e,e')::newspatial)
| ListSeg(e,e')::tl -> if e = oldvar then
(true,(newspatial@(ListSeg(newvar,e')::tl)))
else
_rearrange oldvar newvar tl (ListSeg(e,e')::newspatial)
| Junk::tl -> _rearrange oldvar newvar tl (Junk::newspatial)
in
let rec _perform_subst var var_equiv =
match var_equiv with
| [] -> []
| v::tl -> let (stopb,newsp) = _rearrange v var spatial [] in
if stopb then
newsp
else
_perform_subst var tl
in
let (continue,var) = primitive_command comm in
if (not continue) then
[symheap] (* no need to rearrange *)
else
let (allocated,_) = find_allocated var spatial in
if allocated then [symheap]
else let var_equiv_class = equivclass var pure in
[(pure,(_perform_subst var var_equiv_class))];;
let rearrange_lseg comm symheap =
let rec _split_list_size2 var oldsymheap newsymheap =
match oldsymheap with
| [] -> newsymheap
| ListSeg(e,e')::tl -> if (e = var) then
let freshvar = create_freshvar() in
let sp1 = Pointsto(e,freshvar) in
let sp2 = ListSeg(freshvar,e') in
(newsymheap)@[sp1;sp2]@tl
else
_split_list_size2 var tl (ListSeg(e,e')::newsymheap)
| e::tl -> _split_list_size2 var tl (e::newsymheap)
in
let rec _split_list_size1 var oldsymheap newsymheap =
match oldsymheap with
| [] -> newsymheap
| ListSeg(e,e')::tl -> if (e = var) then
newsymheap@((Pointsto(e,e'))::tl)
else
_split_list_size1 var tl (ListSeg(e,e')::newsymheap)
| e::tl -> _split_list_size1 var tl (e::newsymheap)
in
let (pure,spatial) = symheap in
let (continue,var) = primitive_command comm in
if (continue) then
let (allocated,var1) = find_allocated var spatial in
if (allocated) then
(* dont need to rearrange *)
let symheap1 = _split_list_size1 var spatial [] in
let symheap2 = _split_list_size2 var spatial [] in
(*if (symheap2 = symheap1) then
[(pure,symheap1)]
else*)
[(pure,symheap1);(pure,symheap2)]
else
(* need to rename some variable using the equality from the pure part *)
(* shouldnt really need to go here? *)
[]
else
[symheap] (* Should really raise an exception, cos somthing must have gone really wrong *);;
let rearrange comm symheap =
(*let t3 = Printf.printf "BEFORE \n" in
let t = symheap_print symheap in
*) let r1 = rearrange_name comm symheap in
let r2 = rearrange_lseg comm (List.hd (r1)) in
(* let t4 = Printf.printf "After \n" in
let t2 = List.map symheap_print r2 in
*) r2;;