1 | % minimize ProB AST integer expressions for shrinking stage | |
2 | ||
3 | minimize_int_expr(b(integer(Value),integer,[]),b(integer(Value),integer,[])). | |
4 | ||
5 | % add, minus, multiplication, div, power_of, modulo | |
6 | % shrink expression to its value when both arguments are integer values | |
7 | minimize_int_expr(b(Expression,integer,Info),Shrunken) :- | |
8 | Expression =.. [Type,Arg1,Arg2] , | |
9 | member(Type,[add,minus,multiplication,div,power_of,modulo]) , | |
10 | prob_is_ground(Arg1,true) , | |
11 | prob_is_ground(Arg2,true) , | |
12 | int(b(Expression,integer,Info),Value) , | |
13 | Shrunken = b(integer(Value),integer,Info). | |
14 | minimize_int_expr(b(Expression,integer,Info),Shrunken) :- | |
15 | Expression =.. [Type,Arg1,Arg2] , | |
16 | member(Type,[add,minus,multiplication,div,power_of,modulo]) , | |
17 | % hold integer value and minimize expression | |
18 | (prob_is_ground(Arg1,true) , prob_is_ground(Arg2,false) | |
19 | -> minimize_int_expr(Arg2,NewArg2) , | |
20 | NewExpr =.. [Type,Arg1,NewArg2] , | |
21 | Shrunken = b(NewExpr,integer,Info) | |
22 | ; prob_is_ground(Arg1,false) , prob_is_ground(Arg2,true) | |
23 | -> minimize_int_expr(Arg1,NewArg1) , | |
24 | NewExpr =.. [Type,NewArg1,Arg2] , | |
25 | Shrunken = b(NewExpr,integer,Info) | |
26 | ; % minimize both expressions | |
27 | minimize_int_expr(Arg1,NewArg1) , | |
28 | minimize_int_expr(Arg2,NewArg2) , | |
29 | NewExpr =.. [Type,NewArg1,NewArg2] , | |
30 | Shrunken = b(NewExpr,integer,Info)). | |
31 | ||
32 | % unary_minus | |
33 | minimize_int_expr(b(unary_minus(Expression),integer,Info),Shrunken) :- | |
34 | (Expression = b(integer(Value),integer,_) | |
35 | -> Temp is -(Value) , | |
36 | Shrunken = b(integer(Temp),integer,Info) | |
37 | ; minimize_int_expr(Expression,Temp) , | |
38 | Shrunken = b(unary_minus(Temp),integer,Info)). | |
39 | ||
40 | % card | |
41 | /*minimize_int_expr(b(card(Set),integer,Info),Shrunken) :- | |
42 | (prob_is_ground(Set,true) -> | |
43 | int(b(card(Set),integer,Info),Value) , | |
44 | Shrunken = b(integer(Value),integer,Info) | |
45 | ; | |
46 | minimize_set_expr(Set,ShrunkenSet) , | |
47 | Shrunken = b(card(ShrunkenSet),integer,Info)).*/ | |
48 | ||
49 | % max_int, min_int | |
50 | minimize_int_expr(b(Expression,integer,Info),Shrunken) :- | |
51 | member(Expression,[max_int,min_int]) , | |
52 | int(b(Expression,integer,Info),Value) , | |
53 | Shrunken = b(integer(Value),integer,Info). | |
54 | ||
55 | % max, min | |
56 | minimize_int_expr(b(Expression,integer,Info),Shrunken) :- | |
57 | Expression =.. [Type,Set] , | |
58 | member(Type,[max,min]) , | |
59 | (prob_is_ground(Set,true) | |
60 | -> int(b(Expression,integer,Info),Val) , | |
61 | Shrunken = b(integer(Val),integer,[]) | |
62 | ; minimize_set_expr(Set,Temp) , | |
63 | % no empty set in max, min for well-definedness | |
64 | ((Temp = [] ; Temp = avl_set(empty)) | |
65 | -> ShrunkenSet = Set | |
66 | ; ShrunkenSet = Temp) , | |
67 | NewExpr =.. [Type,ShrunkenSet] , | |
68 | Shrunken = b(NewExpr,integer,Info)). | |
69 | ||
70 | % skip identifier | |
71 | minimize_int_expr(b(identifier(Name),Type,Info),b(identifier(Name),Type,Info)). | |
72 | ||
73 | % get the deepest value or expression of an prob ast expression | |
74 | get_inner_expr(b(Expression,Type,Info),Shrunken) :- | |
75 | Expression =.. [_,Arg1,Arg2] , | |
76 | prob_is_ground(Arg1,Res1) , prob_is_ground(Arg2,Res2) , | |
77 | ( Res1 = true , Res2 = true | |
78 | -> % done | |
79 | Shrunken = b(Expression,Type,Info) | |
80 | ; % get inner expression of second argument | |
81 | Res1 = true , Res2 = false | |
82 | -> get_inner_expr(Arg2,Shrunken) | |
83 | ; % both arguments are expressions | |
84 | Res1 = false , Res2 = false | |
85 | -> % random choice heuristic | |
86 | random_member(Argument,[Arg1,Arg2]) , | |
87 | get_inner_expr(Argument,Shrunken) | |
88 | ; % get inner expression of first argument | |
89 | get_inner_expr(Arg1,Shrunken)). | |
90 | ||
91 | get_inner_expr(Expr,Expr). |