1 | % minimize ProB AST set expressions for shrinking stage | |
2 | ||
3 | :- use_module(library(sets)). | |
4 | :- use_module(library(lists),[is_list/1]). | |
5 | :- use_module(library(avl),[avl_to_list/2,list_to_avl/2]). | |
6 | ||
7 | minimize_set_expr(b(set_extension(Set),set(Type),Info),b(set_extension(ShrunkenSet),set(Type),Info)) :- | |
8 | is_list(Set) , Set \= [b(_,set(_),_)] , | |
9 | shrink(list(_),Set,ShrunkenSet). | |
10 | ||
11 | % minimize set of set | |
12 | minimize_set_expr(b(set_extension([Set]),set(Type),Info),Shrunken) :- | |
13 | minimize_set_expr(Set,ShrunkenSet) , | |
14 | (ShrunkenSet = b(value([]),set(_),Info) | |
15 | -> Shrunken = ShrunkenSet | |
16 | ; Shrunken = b(set_extension([ShrunkenSet]),set(Type),Info)). | |
17 | ||
18 | minimize_set_expr(b(set_extension([]),set(Inner),Info),b(set_extension([]),set(Inner),Info)). | |
19 | ||
20 | minimize_set_expr(b(value(Set),set(Inner),Info),Shrunken) :- | |
21 | shrink(prob_value_set,Set,ShrunkenSet) , | |
22 | (ShrunkenSet = [] | |
23 | -> Shrunken = b(value([]),set(Inner),Info) | |
24 | ; Shrunken = b(value(ShrunkenSet),set(Inner),Info)). | |
25 | ||
26 | % shrink both sides of interval down to one integer | |
27 | minimize_set_expr(b(interval(b(integer(Value1),integer,Info1),b(integer(Value2),integer,Info2)),set(integer),Info),Shrunken) :- | |
28 | NewValue1 is Value1 + 1 , | |
29 | NewValue2 is Value2 - 1 , | |
30 | (NewValue1 >= NewValue2 | |
31 | -> Shrunken = b(set_extension([b(integer(NewValue1),integer,Info1)]),set(integer),Info) | |
32 | ; Shrunken = b(interval(b(integer(NewValue1),integer,Info1),b(integer(NewValue2),integer,Info2)),set(integer),Info)). | |
33 | ||
34 | % union, intersection, set_subtraction | |
35 | % shrink expression to its value when both arguments are set expressions | |
36 | minimize_set_expr(b(Expression,set(Inner),Info),Shrunken) :- | |
37 | Expression =.. [Type,Arg1,Arg2] , | |
38 | member(Type,[union,intersection,set_subtraction]) , | |
39 | prob_is_ground(Arg1,true) , | |
40 | prob_is_ground(Arg2,true) , | |
41 | sint_avl(b(Expression,set(Inner),Info),NewSet) , | |
42 | (NewSet = avl_set(empty) | |
43 | -> Shrunken = b(value([]),set(Inner),Info) | |
44 | ; Shrunken = b(value(NewSet),set(Inner),Info)). | |
45 | minimize_set_expr(b(Expression,set(Inner),Info),Shrunken) :- | |
46 | Expression =.. [Type,Arg1,Arg2] , | |
47 | member(Type,[union,intersection,set_subtraction]) , | |
48 | prob_is_ground(Arg1,Res1) , | |
49 | prob_is_ground(Arg2,Res2) , | |
50 | % hold set and minimize expression | |
51 | ( Res1 = true , Res2 = false | |
52 | -> minimize_set_expr(Arg2,NewArg2) , | |
53 | NewExpr =.. [Type,Arg1,NewArg2] , | |
54 | Shrunken = b(NewExpr,set(Inner),Info) | |
55 | ; Res1 = false , Res2 = true | |
56 | -> minimize_set_expr(Arg1,NewArg1) , | |
57 | NewExpr =.. [Type,NewArg1,Arg2] , | |
58 | Shrunken = b(NewExpr,set(Inner),Info) | |
59 | ; % minimize both expressions | |
60 | minimize_set_expr(Arg1,NewArg1) , | |
61 | minimize_set_expr(Arg2,NewArg2) , | |
62 | NewExpr =.. [Type,NewArg1,NewArg2] , | |
63 | Shrunken = b(NewExpr,set(Inner),Info)). | |
64 | ||
65 | % general_union, general_intersection | |
66 | minimize_set_expr(b(Expression,set(Inner),Info),Shrunken) :- | |
67 | Expression =.. [Type,Arg] , | |
68 | Arg = b(set_extension([Set]),_,_) , | |
69 | member(Type,[general_union,general_intersection]) , | |
70 | prob_is_ground(Set,Res) , | |
71 | (Res = true | |
72 | -> sint_avl(b(Expression,set(Inner),Info),NewSet) , | |
73 | % don't shrink to an empty set | |
74 | (NewSet = avl_set(empty) | |
75 | -> Shrunken = b(Expression,set(Inner),Info) | |
76 | ; Shrunken = b(value(NewSet),set(Inner),Info)) | |
77 | ; minimize_set_expr(Arg,NewArg) , | |
78 | NewExpr =.. [Type,NewArg] , | |
79 | Shrunken = b(NewExpr,set(Inner),Info)). | |
80 | ||
81 | % skip identifier | |
82 | minimize_set_expr(b(identifier(Name),Type,Info),b(identifier(Name),Type,Info)). |