| 1 | % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
| 2 | % Heinrich Heine Universitaet Duesseldorf | |
| 3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
| 4 | ||
| 5 | :- module(partition_detection, [detect_partitions/2, detect_all_partitions_in_predicate/2, is_disjoint_pred/4]). | |
| 6 | ||
| 7 | :- use_module(module_information,[module_info/2]). | |
| 8 | :- module_info(group,typechecker). | |
| 9 | :- module_info(description,'This module detects set partition predicates.'). | |
| 10 | ||
| 11 | :- use_module(library(lists)). | |
| 12 | :- use_module(bsyntaxtree). | |
| 13 | :- use_module(bmachine_structure,[select_section/5]). | |
| 14 | ||
| 15 | detect_partitions(In,Out) :- | |
| 16 | select_section(properties,Old,New,In,Out), | |
| 17 | detect_all_partitions_in_predicate(Old,New). | |
| 18 | ||
| 19 | % detect all set partition predicates at "top-level", not inside inner predicates (bool(.), comprehension_set,...) | |
| 20 | % this is assumed to be checked by b_ast_cleanup during bottom-up construction | |
| 21 | detect_all_partitions_in_predicate(Predicate,NewPredicate) :- | |
| 22 | conjunction_to_list(Predicate,PList), | |
| 23 | detect_partitions_in_predicates(PList,P1), % fails if there is no partition: Predicate (and its labels) is kept unchanged | |
| 24 | !, | |
| 25 | detect_all_remaining_partitions(P1,PNew), | |
| 26 | conjunct_predicates(PNew,NewPredicate). | |
| 27 | detect_all_partitions_in_predicate(Predicate,Predicate). % relevant for e.g., test 1840 to avoid removing labels,... | |
| 28 | ||
| 29 | detect_all_remaining_partitions(Predicates,NewPredicates) :- | |
| 30 | (detect_partitions_in_predicates(Predicates,P1) | |
| 31 | -> detect_all_remaining_partitions(P1,NewPredicates) | |
| 32 | ; NewPredicates=Predicates). | |
| 33 | ||
| 34 | :- use_module(probsrc(error_manager), [add_message/4]). | |
| 35 | :- use_module(probsrc(debug), [debug_mode/1]). | |
| 36 | :- use_module(probsrc(preferences), [get_preference/2]). | |
| 37 | ||
| 38 | % detect partitions of set of the type: | |
| 39 | % x /\ y = {} & x \/ y = z | |
| 40 | detect_partitions_in_predicates(Predicates,NewPredicates) :- Predicates \= [], | |
| 41 | ? | find_union(Superset,Partitions,EqualityPred,Predicates,P11,P12), |
| 42 | create_pairs(Partitions,Pairs), % we need to find inequalities for every pair of elements of Partitions | |
| 43 | get_texpr_type(Superset,Type), | |
| 44 | find_all_intersections(Pairs,Type,RestPairs1,P11,P21,Disj1), | |
| 45 | find_all_intersections(RestPairs1,Type,RestPairs2,P12,P22,Disj2), | |
| 46 | RestPairs2=[], % all pairs are disjoint --> we have found a partition of Superset | |
| 47 | !, | |
| 48 | safe_create_texpr(partition(Superset,Partitions),pred,Part), | |
| 49 | append([Disj1,[EqualityPred],Disj2],RemovedPreds), % in original order | |
| 50 | copy_infos_of_removed_preds(RemovedPreds,EqualityPred,Part,PartWithInfo), | |
| 51 | % now insert partition predicate at position of union | |
| 52 | (debug_mode(off),get_preference(performance_monitoring_on,false) -> true | |
| 53 | ; add_message(partition_detection,'Detected partition predicate for:',Superset,EqualityPred)), | |
| 54 | append(P21,[PartWithInfo|P22],NewPredicates). | |
| 55 | ||
| 56 | % should we also find A = {c1,c2,...cn} with all_different(c1,...,cn) ?? | |
| 57 | find_union(Superset,Partitions,Equal,In,Out1,Out2) :- | |
| 58 | create_texpr(equal(A,B),pred,_,Equal), | |
| 59 | get_texpr_set_type(A,Type), | |
| 60 | get_texpr_set_type(B,Type), | |
| 61 | append(Out1,[Equal|Out2],In), % select Equal in In Predicates | |
| 62 | \+ predicate_has_ignore_pragma(Equal), % ignored predicates must not become part of a partition | |
| 63 | ( is_union(A,Partitions) -> Superset=B | |
| 64 | ; is_union(B,Partitions) -> Superset=A). | |
| 65 | ||
| 66 | is_union(Expr,Partitions) :- | |
| 67 | get_texpr_expr(Expr,union(_,_)),!, | |
| 68 | collect_union(Expr,Partitions,[]). | |
| 69 | is_union(Expr,Partitions) :- | |
| 70 | % Note: this pattern is rewritten in ast_cleanup, unless optimize_ast is false | |
| 71 | get_texpr_expr(Expr,general_union(b(set_extension(Partitions),_,_))). | |
| 72 | ||
| 73 | collect_union(Expr) --> | |
| 74 | {get_texpr_expr(Expr,union(A,B)),!}, | |
| 75 | collect_union(A), | |
| 76 | collect_union(B). | |
| 77 | collect_union(Expr) --> [Expr]. | |
| 78 | ||
| 79 | create_pairs(Parts,Pairs) :- | |
| 80 | create_pairs2(Parts,Pairs,[]). | |
| 81 | create_pairs2([]) --> []. | |
| 82 | create_pairs2([A|Rest]) --> | |
| 83 | create_pairs3(Rest,A), | |
| 84 | create_pairs2(Rest). | |
| 85 | create_pairs3([],_) --> []. | |
| 86 | create_pairs3([B|Rest],A) --> | |
| 87 | [distinct(A,B)], | |
| 88 | create_pairs3(Rest,A). | |
| 89 | ||
| 90 | % Out: the predicates of In which are not used; Removed: the disjointness predicates used | |
| 91 | find_all_intersections(Pairs,Type,RestPairs,In,Out,Removed) :- | |
| 92 | find_all_intersections2(In,Pairs,Type,RestPairs,Out,Removed). | |
| 93 | find_all_intersections2([],Pairs,_,Pairs,[],[]). | |
| 94 | find_all_intersections2([Expr|Erest],Pairs,Type,RestPairs,Out,[Expr|Removed]) :- | |
| 95 | is_disjoint_pred(Expr,Type,A,B), | |
| 96 | \+ predicate_has_ignore_pragma(Expr), | |
| 97 | remove_pair(A,B,Pairs,InterPairs),!, | |
| 98 | find_all_intersections2(Erest,InterPairs,Type,RestPairs,Out,Removed). | |
| 99 | find_all_intersections2([Expr|Erest],Pairs,Type,RestPairs,[Expr|Out],Removed) :- | |
| 100 | find_all_intersections2(Erest,Pairs,Type,RestPairs,Out,Removed). | |
| 101 | ||
| 102 | % the partition predicate replaces RemovedPreds: give it the position of the union equality | |
| 103 | % (a single position cannot cover conjuncts spread over the PROPERTIES) and the labels of all RemovedPreds | |
| 104 | copy_infos_of_removed_preds(RemovedPreds,EqualityPred,Part,Res) :- | |
| 105 | copy_pos_infos(EqualityPred,Part,Part1), % may be a Rodin position which also serves as label | |
| 106 | findall(L,(member(P,RemovedPreds),get_texpr_labels(P,Ls),member(L,Ls)),AllLabels), | |
| 107 | findall(L,(get_texpr_labels(Part1,Ls),member(L,Ls)),ExistingLabels), | |
| 108 | new_labels(AllLabels,ExistingLabels,NewLabels), | |
| 109 | add_labels_to_texpr(Part1,NewLabels,Res). | |
| 110 | ||
| 111 | % remove duplicates and labels already present, keeping the order | |
| 112 | new_labels([],_,[]). | |
| 113 | new_labels([L|T],Seen,Res) :- | |
| 114 | (memberchk(L,Seen) -> Res=Res1 ; Res=[L|Res1]), | |
| 115 | new_labels(T,[L|Seen],Res1). | |
| 116 | ||
| 117 | is_disjoint_pred(b(Expr,_,_),Type,A,B) :- | |
| 118 | is_disjoint_pred2(Expr,Type,A,B). | |
| 119 | is_disjoint_pred2(equal(X,Y),Type,A,B) :- % detect A /\ B = {} or {} = A /\ B | |
| 120 | ( get_texpr_expr(X,empty_set) -> Inter=Y | |
| 121 | ; get_texpr_expr(Y,empty_set) -> Inter=X), | |
| 122 | get_texpr_expr(Inter,intersection(A,B)), get_texpr_type(Inter,Type). | |
| 123 | is_disjoint_pred2(not_equal(X,Y),set(Type),A,B) :- get_texpr_type(X,Type), | |
| 124 | A = b(set_extension([X]),set(Type),_), | |
| 125 | B = b(set_extension([Y]),set(Type),_). | |
| 126 | is_disjoint_pred2(negation(NE),Type,A,B) :- | |
| 127 | is_disjoint_neg_pred(NE,Type,A,B). | |
| 128 | ||
| 129 | is_disjoint_neg_pred(b(Expr,_,_),Type,A,B) :- | |
| 130 | is_disjoint_neg_pred2(Expr,Type,A,B). | |
| 131 | is_disjoint_neg_pred2(not_equal(X,Y),Type,A,B) :- % detect A /\ B /= {} or {} /= A /\ B | |
| 132 | ( get_texpr_expr(X,empty_set) -> Inter=Y | |
| 133 | ; get_texpr_expr(Y,empty_set) -> Inter=X), | |
| 134 | get_texpr_expr(Inter,intersection(A,B)), get_texpr_type(Inter,Type). | |
| 135 | is_disjoint_neg_pred2(equal(X,Y),set(Type),A,B) :- get_texpr_type(X,Type), | |
| 136 | A = b(set_extension([X]),set(Type),_), | |
| 137 | B = b(set_extension([Y]),set(Type),_). | |
| 138 | is_disjoint_neg_pred2(negation(NE),Type,A,B) :- | |
| 139 | is_disjoint_pred(NE,Type,A,B). | |
| 140 | ||
| 141 | remove_pair(A,B,In,Out) :- | |
| 142 | remove_all_infos(A,FA), | |
| 143 | ? | remove_all_infos(B,FB), |
| 144 | ? | ( select(distinct(FA,FB),In,Out) |
| 145 | ? | ; select(distinct(FB,FA),In,Out)),!. |