Search code examples
prologpolynomialsinstantiation-error

Arguments aren't sufficient instantiated


I read my code for an hour but I'm not able to understand where the problem is. I read that this error means that I use an argument I didn't instantiate before but I can't see where it is. Can you help me?

as_monomial(X, m(X, 0, [])) :- number(X), !.
as_monomial(^(Y, Z), m(1, Z, [v(Z, Y)])) :- !.
as_monomial(*(X, ^(Y, Z)), m(G, K, Q)) :- as_monomial(X, m(G, TD, Vars)), K is (TD + Z), ordina_m([v(Z, Y)| Vars], Q), !.
as_monomial(*(X, Y), m(G, K, Q)) :- as_monomial(X, m(G, TD, Vars)), K is (TD + 1), ordina_m([v(1, Y)| Vars], Q), !.
as_monomial(-(X), m(-A, Y, L)) :- as_monomial(X, m(A, Y, L)).
as_monomial(X, m(1, 1, [v(1, X)])).

ordina_m(List, Sorted) :- sort(2, @=<, List,  Sorted).

ordina_poly1(List, Sorted) :- sort(2, @>=, List,  Sorted).
ordina_poly2(List, Sorted) :- sort(3, @=<, List,  Sorted).

is_monomial(m(_C, TD, VPs)) :- integer(TD), TD >= 0, is_list(VPs).
is_polynomial(poly(M)) :- is_list(M), foreach(member(Monomio, M), is_monomial(Monomio)).

as_polynomial(+(X, Y), poly(C)) :- as_monomial(Y, G), as_polynomial(X, poly(Gs)), inverti(G, H), inverti2(Gs, Hs), ordina_poly2([H| Hs], D), inverti2(D, F), ordina_poly1(F, C), !.
as_polynomial(-(X, Y), poly(C)) :- as_monomial(-Y, G), as_polynomial(X, poly(Gs)), inverti(G, H), inverti2(Gs, Hs), ordina_poly2([H| Hs], D), inverti2(D, F), ordina_poly1(F, C), !.
as_polynomial(X, poly([X])) :- is_monomial(X), !.
as_polynomial(X, poly([Q])) :- as_monomial(X, Q), !.

/* grado massimo */
maxdegree(Poly1, Result) :- is_polynomial(Poly1), max_degree(Poly1, Result), !.
maxdegree(Poly1, Result) :- as_polynomial(Poly1, Result1), max_degree(Result1, Result), !.

max_degree(poly([]), 0) :- !.
max_degree(poly([m(_, X, _)|Xs]), X) :- max_degree(poly(Xs), Ys), X > Ys, !.
max_degree(poly([m(_, X, _)|Xs]), Ys) :- max_degree(poly(Xs), Ys), X =< Ys, !.

/* grado minimo */
mindegree(Poly1, Result) :- is_polynomial(Poly1), min_degree(Poly1, Result), !.
mindegree(Poly1, Result) :- as_polynomial(Poly1, Result1), min_degree(Result1, Result), !.

min_degree(poly([m(_, X, _)]), X) :- !.
min_degree(poly([m(_, X, _)|Xs]), X) :- min_degree(poly(Xs), Ys), X < Ys, !.
min_degree(poly([m(_, X, _)|Xs]), Ys) :- min_degree(poly(Xs), Ys), X >= Ys, !.

inverti(m(_, _, []), m(_, _, [])) :- !.
inverti(m(X, Y, [v(W, Z)| Xs]), m(X, Y, [v(Z, W)| Ys])) :- inverti(m(X, Y, Xs), m(X, Y, Ys)), !.

inverti2([], []) :- !.
inverti2([m(X, Y, [])| Zs], [m(X, Y, [])| Ss]) :- inverti2(Zs, Ss), !.
inverti2([m(X, Y, [v(W, Z)| Xs])| Zs], [m(X, Y, [v(Z, W)| Ys])| Ss]) :- inverti2([m(X, Y, Xs)| Zs], [m(X, Y, Ys)| Ss]), !.

I will not include code for as/is_polynomial cuz I already use it before and I don't have problem with that piece of code. Can you help me please? An example for it, I tried, is maxdegree(x^5+y^500+4, R). (same thing for mindegree)


Solution

  • I solve my problem adding a clausure in both maxdegree and mindegree as base case:

    max_degree(poly([m(_, _X, [])]), 0) :- !. max_degree(poly([m(_, X, _)]), X).

    min_degree(poly([m(_, _X, [])]), 0) :- !. min_degree(poly([m(_, X, _)]), X) :- !.