34
35:- module(move_assr, []). 36
37:- use_module(library(substitute)). 38:- use_module(library(assertions)). 39:- use_module(library(move_preds)). 40:- use_module(library(refactor)). 41:- init_expansors. 42
43move_preds:cond_move_pred_hook((:- Assertions1), CM, PredList, Into) :-
44 current_decomposed_assertion_1(Assertions1, _, CM, _, _, Body1, _, _, _, _, _, _),
45 !,
46 findall(M:F/A,
47 ( decompose_assertion_head_body(Body1, _, CM, M:H, true, _, _, _, _, _, _, _, _),
48 functor(H, F, A)
49 ), AllPredList),
50 ( subtract(AllPredList, PredList, [])
51 ->Into = []
52 ; substitute(assertions_exclude(AllPredList, PredList, CM), Body1, Body),
53 substitute_value(Body1, Body, Assertions1, Assertions),
54 Assertions \= Assertions1,
55 Into = (:- Assertions)
56 ).
57
58move_preds:move_preds_hook(PredList, MSource, _, MTarget, Target, Options) :-
59 cleanup_assertions(MSource, MTarget, PredList, [file(Target)|Options]).
60
61cleanup_assertion(MSource, MTarget, PredList, Assertions1, AssertionsDecl) :-
62 ( current_decomposed_assertion_1(Assertions1, _, MSource, _, _, Body1, _, _, _, _, _, _)
63 ->findall(M:F/A,
64 ( decompose_assertion_head_body(Body1, _, MSource, M:H, true, _, _, _, _, _, _, _, _),
65 predicate_property(M:H, defined),
66 predicate_property(M:H, implementation_module(MSource)),
67 functor(H, F, A)
68 ), AllPredList),
69 AllPredList \= [],
70 findall(M:F/A,
71 ( decompose_assertion_head_body(Body1, _, MTarget, M:H, true, _, _, _, _, _, _, _, _),
72 predicate_property(M:H, defined),
73 functor(H, F, A)
74 ), IncludePredList, PredList),
75 ( intersection(AllPredList, IncludePredList, [])
76 ->AssertionsDecl = []
77 ; substitute(assertions_include(AllPredList, IncludePredList, MSource), Body1, Body),
78 substitute_value(Body1, Body, Assertions1, Assertions),
79 Assertions \= Assertions1,
80 AssertionsDecl = (:- Assertions)
81 )
82 ).
83
84cleanup_assertions(MSource, MTarget, PredList, Options) :-
85 replace_sentence((:- Assertions1), AssertionsDecl,
86 cleanup_assertion(MSource, MTarget, PredList, Assertions1, AssertionsDecl),
87 Options).
88
89assertions_include(AllPreds, PredList, M, Assertions1, Replace) :-
90 assertions_include_exclude(AllPreds, PredList, M, Assertions1, Assertions1, '$RM', Replace).
91
92assertions_exclude(AllPreds, PredList, M, Assertions1, Replace) :-
93 assertions_include_exclude(AllPreds, PredList, M, Assertions1, '$RM', Assertions1, Replace).
94
95assertions_include_exclude(AllPreds, PredList, CM, Body1, ReplaceInclude, ReplaceExclude, Replace) :-
96 catch(
97 ( \+ decompose_assertion_head_body(Body1, _, CM, _, _, _, _, _, _, _, _, _, _)
98 ->fail
99 ; forall(( decompose_assertion_head_body(Body1, _, CM, M:H, _, _, _, _, _, _, _, _, _),
100 functor(H, F, A)
101 ),
102 memberchk(M:F/A, AllPreds)),
103 ( forall(( decompose_assertion_head_body(Body1, _, CM, M:H, _, _, _, _, _, _, _, _, _),
104 functor(H, F, A)
105 ),
106 \+ memberchk(M:F/A, PredList))
107 ->Replace = ReplaceExclude
108 ; forall(( decompose_assertion_head_body(Body1, _, CM, M:H, _, _, _, _, _, _, _, _, _),
109 functor(H, F, A)
110 ),
111 memberchk(M:F/A, PredList))
112 ->Replace = ReplaceInclude
113 114 )
115 ),
116 _,
117 fail)