Implement is_polynomial_as_list_valid_in_predicate and refactored is_term_valid_in_predicate to is_polynomial_valid_in_predicate

This commit is contained in:
Diogo Cordeiro 2018-11-24 20:34:55 +00:00
parent 7a22fe136b
commit d298364d0f
1 changed files with 32 additions and 14 deletions

View File

@ -39,7 +39,7 @@
argument) and vice-versa.
*/
poly2list(P, L) :-
is_term_valid_in_predicate(P, "poly2list"),
is_polynomial_valid_in_predicate(P, "poly2list"),
polynomial_to_list(P, L),
!.
@ -48,7 +48,7 @@ poly2list(P, L) :-
another polynomial as a list.
*/
simpoly_list(L, S) :-
is_polynomial_list_valid_in_predicate(L, "simpoly_list"), %TODO IMPLEMENT
is_polynomial_as_list_valid_in_predicate(L, "simpoly_list"),
simplify_polynomial_as_list(L, S),
!.
@ -57,7 +57,7 @@ simpoly_list(L, S) :-
as another polynomial as an expression.
*/
simpoly(P, S) :-
is_term_valid_in_predicate(P, "simpoly"),
is_polynomial_valid_in_predicate(P, "simpoly"),
simplify_polynomial(P, S),
!.
@ -67,8 +67,8 @@ simpoly(P, S) :-
be ground. The polynomial resulting from the sum is in simplified form.
*/
scalepoly(P1, P2, S) :-
is_term_valid_in_predicate(P1, "scalepoly"),
is_term_valid_in_predicate(P2, "scalepoly"),
is_polynomial_valid_in_predicate(P1, "scalepoly"),
is_polynomial_valid_in_predicate(P2, "scalepoly"),
scale_polynomial(P1, P2, S),
!.
@ -78,8 +78,8 @@ scalepoly(P1, P2, S) :-
The polynomial resulting from the sum is in simplified form.
*/
addpoly(P1, P2, S) :-
is_term_valid_in_predicate(P1, "addpoly"),
is_term_valid_in_predicate(P2, "addpoly"),
is_polynomial_valid_in_predicate(P1, "addpoly"),
is_polynomial_valid_in_predicate(P2, "addpoly"),
add_polynomial(P1, P2, S),
!.
@ -207,17 +207,17 @@ term(L * R) :-
%@ _1188 in inf..sup .
%% Doesn't give all possible terms, because number(N) is not reversible
%% is_term_valid_in_predicate(+T, +F) is det
%% is_polynomial_valid_in_predicate(+T, +F) is det
%
% Returns true if valid Term, fails with UI message otherwise.
% The fail message reports which Term is invalid and in which
% Returns true if valid polynomial, fails with UI message otherwise.
% The failure message reports which polynomial is invalid and in which
% predicate the problem ocurred.
%
is_term_valid_in_predicate(P, _) :-
is_polynomial_valid_in_predicate(P, _) :-
%% If P is a valid polynomial, return true
polynomial(P),
!.
is_term_valid_in_predicate(P, F) :-
is_polynomial_valid_in_predicate(P, F) :-
%% Writes the polynomial and fails otherwise
write("Invalid polynomial in "),
write(F),
@ -225,12 +225,30 @@ is_term_valid_in_predicate(P, F) :-
write(P),
fail.
%% Tests:
%% ?- is_term_valid_in_predicate(1, "Test").
%% ?- is_polynomial_valid_in_predicate(1, "Test").
%@ true.
%% ?- is_term_valid_in_predicate(a*4-0*x, "Test").
%% ?- is_polynomial_valid_in_predicate(a*4-0*x, "Test").
%@ Invalid polynomial in Test: a*4-0*x
%@ false.
%% is_polynomial_as_list_valid_in_predicate(+L, +F) is det
%
% Returns true if the polynomial represented as list is valid,
% fails with UI message otherwise.
% The failure message reports which polynomial is invalid and
% in which predicate the problem ocurred.
%
is_polynomial_as_list_valid_in_predicate(L, F) :-
%% If L is a valid polynomial, return true
list_to_polynomial(L, P),
is_polynomial_valid_in_predicate(P, F).
%% Tests:
%% ?- is_polynomial_as_list_valid_in_predicate([1], "Test").
%@ true.
%% ?- is_polynomial_as_list_valid_in_predicate([0*x, a*4], "Test").
%@Invalid polynomial in Test: a*4+0*x
%@false.
%% polynomial(+M:atom) is semidet
%
% Returns true if polynomial, false otherwise.