37
38
39:- module(nf_q,
40 [
41 {}/1,
42 nf/2,
43 entailed/1,
44 split/3,
45 repair/2,
46 nf_constant/2,
47 wait_linear/3,
48 nf2term/2
49 ]). 50:- use_module(library(error), [type_error/2, instantiation_error/1]). 51:- use_module('../clpqr/geler',
52 [
53 geler/3
54 ]). 55:- use_module(bv_q,
56 [
57 log_deref/4,
58 solve/1,
59 'solve_<'/1,
60 'solve_=<'/1,
61 'solve_=\\='/1
62 ]). 63:- use_module(ineq_q,
64 [
65 ineq_one/4,
66 ineq_one_s_p_0/1,
67 ineq_one_s_n_0/1,
68 ineq_one_n_p_0/1,
69 ineq_one_n_n_0/1
70 ]). 71:- use_module(store_q,
72 [
73 add_linear_11/3,
74 normalize_scalar/2
75 ]). 76:- use_module('../clpqr/highlight', []). 77
78:- meta_predicate wait_linear(?, ?, 0). 79
81
89
90{Rel} :-
91 var(Rel),
92 !,
93 throw(instantiation_error({Rel},1)).
94{R,Rs} :-
95 !,
96 {R},{Rs}.
97{R;Rs} :-
98 !,
99 ({R};{Rs}). 100{L < R} :-
101 !,
102 nf(L-R,Nf),
103 submit_lt(Nf).
104{L > R} :-
105 !,
106 nf(R-L,Nf),
107 submit_lt(Nf).
108{L =< R} :-
109 !,
110 nf(L-R,Nf),
111 submit_le( Nf).
112{<=(L,R)} :-
113 !,
114 nf(L-R,Nf),
115 submit_le(Nf).
116{L >= R} :-
117 !,
118 nf(R-L,Nf),
119 submit_le(Nf).
120{L =\= R} :-
121 !,
122 nf(L-R,Nf),
123 submit_ne(Nf).
124{L =:= R} :-
125 !,
126 nf(L-R,Nf),
127 submit_eq(Nf).
128{L = R} :-
129 !,
130 nf(L-R,Nf),
131 submit_eq(Nf).
132{Rel} :-
133 type_error(clpq_constraint, Rel).
134
141
142entailed(C) :-
143 negate(C,Cn),
144 \+ {Cn}.
145
150
151negate(Rel,_) :-
152 var(Rel),
153 !,
154 instantiation_error(Rel).
155negate((A,B),(Na;Nb)) :-
156 !,
157 negate(A,Na),
158 negate(B,Nb).
159negate((A;B),(Na,Nb)) :-
160 !,
161 negate(A,Na),
162 negate(B,Nb).
163negate(A<B,A>=B) :- !.
164negate(A>B,A=<B) :- !.
165negate(A=<B,A>B) :- !.
166negate(A>=B,A<B) :- !.
167negate(A=:=B,A=\=B) :- !.
168negate(A=B,A=\=B) :- !.
169negate(A=\=B,A=:=B) :- !.
170negate(Rel,_) :-
171 type_error(clpq_constraint, Rel).
172
191
192submit_eq([]). 193submit_eq([T|Ts]) :-
194 submit_eq(Ts,T).
195submit_eq([],A) :- submit_eq_b(A). 196submit_eq([B|Bs],A) :- submit_eq_c(A,B,Bs). 197
201
203submit_eq_b(v(_,[])) :-
204 !,
205 fail.
207submit_eq_b(v(_,[X^P])) :-
208 var(X),
209 P > 0,
210 !,
211 X = 0.
213submit_eq_b(v(_,[NL^1])) :-
214 nonvar(NL),
215 nl_invertible(NL,X,0,Inv),
216 !,
217 nf(-Inv,S),
218 nf_add(X,S,New),
219 submit_eq(New).
221submit_eq_b(Term) :-
222 term_variables(Term,Vs),
223 geler(clpq,Vs,resubmit_eq([Term])).
224
228
230submit_eq_c(v(I,[]),B,Rest) :-
231 !,
232 submit_eq_c1(Rest,B,I).
234submit_eq_c(A,B,Rest) :- 235 A = v(_,[X^1]),
236 var(X),
237 B = v(_,[Y^1]),
238 var(Y),
239 linear(Rest),
240 !,
241 Hom = [A,B|Rest],
242 243 nf_length(Hom,0,Len),
244 log_deref(Len,Hom,[],HomD),
245 solve(HomD).
247submit_eq_c(A,B,Rest) :-
248 Norm = [A,B|Rest],
249 term_variables(Norm,Vs),
250 geler(clpq,Vs,resubmit_eq(Norm)).
251
255
257submit_eq_c1([],v(K,[X^P]),I) :-
258 var(X),
259 ( P = 1,
260 !,
261 X is -I rdiv K
262 ; P = -1,
263 !,
264 X is -K rdiv I
265 ).
268submit_eq_c1([],v(K,[NL^P]),I) :-
269 nonvar(NL),
270 ( P = 1,
271 Y is -I rdiv K
272 ; P = -1,
273 Y is -K rdiv I
274 ),
275 nl_invertible(NL,X,Y,Inv),
276 !,
277 nf(-Inv,S),
278 nf_add(X,S,New),
279 submit_eq(New).
281submit_eq_c1(Rest,B,I) :-
282 B = v(_,[Y^1]),
283 var(Y),
284 linear(Rest),
285 !,
286 287 Hom = [B|Rest],
288 nf_length(Hom,0,Len),
289 normalize_scalar(I,Nonvar),
290 log_deref(Len,Hom,[],HomD),
291 add_linear_11(Nonvar,HomD,LinD),
292 solve(LinD).
294submit_eq_c1(Rest,B,I) :-
295 Norm = [v(I,[]),B|Rest],
296 term_variables(Norm,Vs),
297 geler(clpq,Vs,resubmit_eq(Norm)).
298
300
304
306submit_lt([]) :- fail.
308submit_lt([A|As]) :- submit_lt(As,A).
309
313
315submit_lt([],v(K,P)) :- submit_lt_b(P,K).
317submit_lt([B|Bs],A) :- submit_lt_c(Bs,A,B).
318
322
324submit_lt_b([],I) :-
325 !,
326 I < 0.
328submit_lt_b([X^1],K) :-
329 var(X),
330 !,
331 ( K > 0
332 -> ineq_one_s_p_0(X) 333 ; ineq_one_s_n_0(X) 334 ).
336submit_lt_b(P,K) :-
337 term_variables(P,Vs),
338 geler(clpq,Vs,resubmit_lt([v(K,P)])).
339
343
345submit_lt_c([],A,B) :-
346 A = v(I,[]),
347 B = v(K,[Y^1]),
348 var(Y),
349 !,
350 ineq_one(strict,Y,K,I).
352submit_lt_c(Rest,A,B) :-
353 Norm = [A,B|Rest],
354 ( linear(Norm)
355 -> 'solve_<'(Norm)
356 ; term_variables(Norm,Vs),
357 geler(clpq,Vs,resubmit_lt(Norm))
358 ).
359
364
366submit_le([]).
368submit_le([A|As]) :- submit_le(As,A).
369
373
375submit_le([],v(K,P)) :- submit_le_b(P,K).
377submit_le([B|Bs],A) :- submit_le_c(Bs,A,B).
378
382
384submit_le_b([],I) :-
385 !,
386 I =< 0.
388submit_le_b([X^1],K) :-
389 var(X),
390 !,
391 ( K > 0
392 -> ineq_one_n_p_0(X) 393 ; ineq_one_n_n_0(X) 394 ).
396submit_le_b(P,K) :-
397 term_variables(P,Vs),
398 geler(clpq,Vs,resubmit_le([v(K,P)])).
399
403
405submit_le_c([],A,B) :-
406 A = v(I,[]),
407 B = v(K,[Y^1]),
408 var(Y),
409 !,
410 ineq_one(nonstrict,Y,K,I).
412submit_le_c(Rest,A,B) :-
413 Norm = [A,B|Rest],
414 ( linear(Norm)
415 -> 'solve_=<'(Norm)
416 ; term_variables(Norm,Vs),
417 geler(clpq,Vs,resubmit_le(Norm))
418 ).
419
424
425submit_ne(Norm1) :-
426 ( nf_constant(Norm1,K)
427 -> K =\= 0
428 ; linear(Norm1)
429 -> 'solve_=\\='(Norm1)
430 ; term_variables(Norm1,Vs),
431 geler(clpq,Vs,resubmit_ne(Norm1))
432 ).
433
437
438linear([]).
439linear(v(_,Ps)) :- linear_ps(Ps).
440linear([A|As]) :-
441 linear(A),
442 linear(As).
443
448
449linear_ps([]).
450linear_ps([V^1]) :- var(V). 451
456
457wait_linear(Term,Var,Goal) :-
458 nf(Term,Nf),
459 ( linear(Nf)
460 -> Var = Nf,
461 call(Goal)
462 ; term_variables(Nf,Vars),
463 geler(clpq,Vars,wait_linear_retry(Nf,Var,Goal))
464 ).
468resubmit_eq(N) :-
469 repair(N,Norm),
470 submit_eq(Norm).
471resubmit_lt(N) :-
472 repair(N,Norm),
473 submit_lt(Norm).
474resubmit_le(N) :-
475 repair(N,Norm),
476 submit_le(Norm).
477resubmit_ne(N) :-
478 repair(N,Norm),
479 submit_ne(Norm).
480wait_linear_retry(Nf0,Var,Goal) :-
481 repair(Nf0,Nf),
482 ( linear(Nf)
483 -> Var = Nf,
484 call(Goal)
485 ; term_variables(Nf,Vars),
486 geler(clpq,Vars,wait_linear_retry(Nf,Var,Goal))
487 ).
489
494
495nl_invertible(sin(X),X,Y,Res) :- Res is asin(Y).
496nl_invertible(cos(X),X,Y,Res) :- Res is acos(Y).
497nl_invertible(tan(X),X,Y,Res) :- Res is atan(Y).
498nl_invertible(exp(B,C),X,A,Res) :-
499 ( nf_constant(B,Kb)
500 -> A > 0,
501 Kb > 0,
502 Kb =\= 1,
503 X = C, 504 Res is rational(log(A)) rdiv rational(log(Kb))
505 ; nf_constant(C,Kc),
506 A =\= 0,
507 Kc > 0,
508 X = B, 509 Res is rational(A**(1 rdiv Kc))
510 ).
511
513
520
522nf(X,Norm) :-
523 var(X),
524 !,
525 Norm = [v(1,[X^1])].
526nf(X,Norm) :-
527 number(X),
528 !,
529 nf_number(X,Norm).
530nf(X,Norm) :-
531 rational(X),
532 !,
533 nf_number(X,Norm).
535nf(-A,Norm) :-
536 !,
537 nf(A,An),
538 nf_mul_factor(v(-1,[]),An,Norm).
539nf(+A,Norm) :-
540 !,
541 nf(A,Norm).
543nf(A+B,Norm) :-
544 !,
545 nf(A,An),
546 nf(B,Bn),
547 nf_add(An,Bn,Norm).
548nf(A-B,Norm) :-
549 !,
550 nf(A,An),
551 nf(-B,Bn),
552 nf_add(An,Bn,Norm).
554nf(A*B,Norm) :-
555 !,
556 nf(A,An),
557 nf(B,Bn),
558 nf_mul(An,Bn,Norm).
559nf(A/B,Norm) :-
560 !,
561 nf(A,An),
562 nf(B,Bn),
563 nf_div(Bn,An,Norm).
565nf(Term,Norm) :-
566 nonlin_1(Term,Arg,Skel,Sa1),
567 !,
568 nf(Arg,An),
569 nf_nonlin_1(Skel,An,Sa1,Norm).
571nf(Term,Norm) :-
572 nonlin_2(Term,A1,A2,Skel,Sa1,Sa2),
573 !,
574 nf(A1,A1n),
575 nf(A2,A2n),
576 nf_nonlin_2(Skel,A1n,A2n,Sa1,Sa2,Norm).
578nf(Term,_) :-
579 type_error(clpq_expression, Term).
580
584
585nf_number(N,Res) :-
586 Rat is rationalize(N),
587 ( Rat =:= 0
588 -> Res = []
589 ; Res = [v(Rat,[])]
590 ).
591
592nonlin_1(abs(X),X,abs(Y),Y).
593nonlin_1(sin(X),X,sin(Y),Y).
594nonlin_1(cos(X),X,cos(Y),Y).
595nonlin_1(tan(X),X,tan(Y),Y).
596nonlin_2(min(A,B),A,B,min(X,Y),X,Y).
597nonlin_2(max(A,B),A,B,max(X,Y),X,Y).
598nonlin_2(exp(A,B),A,B,exp(X,Y),X,Y).
599nonlin_2(pow(A,B),A,B,exp(X,Y),X,Y). 600nonlin_2(A^B,A,B,exp(X,Y),X,Y).
601
602nf_nonlin_1(Skel,An,S1,Norm) :-
603 ( nf_constant(An,S1)
604 -> nl_eval(Skel,Res),
605 nf_number(Res,Norm)
606 ; S1 = An,
607 Norm = [v(1,[Skel^1])]).
608nf_nonlin_2(Skel,A1n,A2n,S1,S2,Norm) :-
609 ( nf_constant(A1n,S1),
610 nf_constant(A2n,S2)
611 -> nl_eval(Skel,Res),
612 nf_number(Res,Norm)
613 ; Skel=exp(_,_),
614 nf_constant(A2n,Exp),
615 integer(Exp)
616 -> nf_power(Exp,A1n,Norm)
617 ; S1 = A1n,
618 S2 = A2n,
619 Norm = [v(1,[Skel^1])]
620 ).
621
623nl_eval(abs(X),R) :- R is abs(X).
624nl_eval(sin(X),R) :- R is sin(X).
625nl_eval(cos(X),R) :- R is cos(X).
626nl_eval(tan(X),R) :- R is tan(X).
629nl_eval(min(X,Y),R) :- R is min(X,Y).
630nl_eval(max(X,Y),R) :- R is max(X,Y).
631nl_eval(exp(X,Y),R) :- R is X**Y.
632
636
637nf_constant([],Z) :- Z = 0.
638nf_constant([v(K,[])],K).
639
647
648split([],[],0).
649split([First|T],H,I) :-
650 ( First = v(I,[])
651 -> H = T
652 ; I = 0,
653 H = [First|T]
654 ).
655
661
662nf_add([],Bs,Bs).
663nf_add([A|As],Bs,Cs) :- nf_add(Bs,A,As,Cs).
664
665nf_add([],A,As,Cs) :- Cs = [A|As].
666nf_add([B|Bs],A,As,Cs) :-
667 A = v(Ka,Pa),
668 B = v(Kb,Pb),
669 compare(Rel,Pa,Pb),
670 nf_add_case(Rel,A,As,Cs,B,Bs,Ka,Kb,Pa).
671
678nf_add_case(<,A,As,Cs,B,Bs,_,_,_) :-
679 Cs = [A|Rest],
680 nf_add(As,B,Bs,Rest).
681nf_add_case(>,A,As,Cs,B,Bs,_,_,_) :-
682 Cs = [B|Rest],
683 nf_add(Bs,A,As,Rest).
684nf_add_case(=,_,As,Cs,_,Bs,Ka,Kb,Pa) :-
685 Kc is Ka + Kb,
686 ( Kc =:= 0.0
687 -> nf_add(As,Bs,Cs)
688 ; Cs = [v(Kc,Pa)|Rest],
689 nf_add(As,Bs,Rest)
690 ).
691
692nf_mul(A,B,Res) :-
693 nf_length(A,0,LenA),
694 nf_length(B,0,LenB),
695 nf_mul_log(LenA,A,[],LenB,B,Res).
696
697nf_mul_log(0,As,As,_,_,[]) :- !.
698nf_mul_log(1,[A|As],As,Lb,B,R) :-
699 !,
700 nf_mul_factor_log(Lb,B,[],A,R).
701nf_mul_log(2,[A1,A2|As],As,Lb,B,R) :-
702 !,
703 nf_mul_factor_log(Lb,B,[],A1,A1b),
704 nf_mul_factor_log(Lb,B,[],A2,A2b),
705 nf_add(A1b,A2b,R).
706nf_mul_log(N,A0,A2,Lb,B,R) :-
707 P is N>>1,
708 Q is N-P,
709 nf_mul_log(P,A0,A1,Lb,B,Rp),
710 nf_mul_log(Q,A1,A2,Lb,B,Rq),
711 nf_add(Rp,Rq,R).
712
713
715nf_add_2(Af,Bf,Res) :- 716 Af = v(Ka,Pa),
717 Bf = v(Kb,Pb),
718 compare(Rel,Pa,Pb),
719 nf_add_2_case(Rel,Af,Bf,Res,Ka,Kb,Pa).
720
721nf_add_2_case(<,Af,Bf,[Af,Bf],_,_,_).
722nf_add_2_case(>,Af,Bf,[Bf,Af],_,_,_).
723nf_add_2_case(=,_, _,Res,Ka,Kb,Pa) :-
724 Kc is Ka + Kb,
725 ( Kc =:= 0
726 -> Res = []
727 ; Res = [v(Kc,Pa)]
728 ).
729
733nf_mul_k([],_,[]).
734nf_mul_k([v(I,P)|Vs],K,[v(Ki,P)|Vks]) :-
735 Ki is K*I,
736 nf_mul_k(Vs,K,Vks).
737
742nf_mul_factor(v(K,[]),Sum,Res) :-
743 !,
744 nf_mul_k(Sum,K,Res).
745nf_mul_factor(F,Sum,Res) :-
746 nf_length(Sum,0,Len),
747 nf_mul_factor_log(Len,Sum,[],F,Res).
748
754
755nf_mul_factor_log(0,As,As,_,[]) :- !.
756nf_mul_factor_log(1,[A|As],As,F,[R]) :-
757 !,
758 mult(A,F,R).
759nf_mul_factor_log(2,[A,B|As],As,F,Res) :-
760 !,
761 mult(A,F,Af),
762 mult(B,F,Bf),
763 nf_add_2(Af,Bf,Res).
764nf_mul_factor_log(N,A0,A2,F,R) :-
765 P is N>>1, 766 Q is N-P,
767 nf_mul_factor_log(P,A0,A1,F,Rp),
768 nf_mul_factor_log(Q,A1,A2,F,Rq),
769 nf_add(Rp,Rq,R).
770
774
775mult(v(Ka,La),v(Kb,Lb),v(Kc,Lc)) :-
776 Kc is Ka*Kb,
777 pmerge(La,Lb,Lc).
778
782
783pmerge([],Bs,Bs).
784pmerge([A|As],Bs,Cs) :- pmerge(Bs,A,As,Cs).
785
786pmerge([],A,As,Res) :- Res = [A|As].
787pmerge([B|Bs],A,As,Res) :-
788 A = Xa^Ka,
789 B = Xb^Kb,
790 compare(R,Xa,Xb),
791 pmerge_case(R,A,As,Res,B,Bs,Ka,Kb,Xa).
792
799
800pmerge_case(<,A,As,Res,B,Bs,_,_,_) :-
801 Res = [A|Tail],
802 pmerge(As,B,Bs,Tail).
803pmerge_case(>,A,As,Res,B,Bs,_,_,_) :-
804 Res = [B|Tail],
805 pmerge(Bs,A,As,Tail).
806pmerge_case(=,_,As,Res,_,Bs,Ka,Kb,Xa) :-
807 Kc is Ka + Kb,
808 ( Kc =:= 0
809 -> pmerge(As,Bs,Res)
810 ; Res = [Xa^Kc|Tail],
811 pmerge(As,Bs,Tail)
812 ).
813
817
819nf_div([],_,_) :-
820 !,
821 zero_division.
823nf_div([v(K,P)],Sum,Res) :-
824 !,
825 Ki is 1 rdiv K,
826 mult_exp(P,-1,Pi),
827 nf_mul_factor(v(Ki,Pi),Sum,Res).
828nf_div(D,A,[v(1,[(A/D)^1])]).
829
833zero_division :- fail. 834
839mult_exp([],_,[]).
840mult_exp([X^P|Xs],K,[X^I|Tail]) :-
841 I is K*P,
842 mult_exp(Xs,K,Tail).
849nf_power(N,Sum,Norm) :-
850 integer(N),
851 compare(Rel,N,0),
852 ( Rel = (<)
853 -> Pn is -N,
854 855 binom(Sum,Pn,Inorm),
856 nf_div(Inorm,[v(1,[])],Norm)
857 ; Rel = (>)
858 -> 859 binom(Sum,N,Norm)
860 ; Rel = (=)
861 -> 862 Norm = [v(1,[])]
863 ).
868nf_power_pos(1,Sum,Norm) :-
869 !,
870 Sum = Norm.
871nf_power_pos(N,Sum,Norm) :-
872 N1 is N-1,
873 nf_power_pos(N1,Sum,Pn1),
874 nf_mul(Sum,Pn1,Norm).
879binom(Sum,1,Power) :-
880 !,
881 Power = Sum.
882binom([],_,[]).
883binom([A|Bs],N,Power) :-
884 ( Bs = []
885 -> nf_power_factor(A,N,Ap),
886 Power = [Ap]
887 ; Bs = [_|_]
888 -> factor_powers(N,A,v(1,[]),Pas),
889 sum_powers(N,Bs,[v(1,[])],Pbs,[]),
890 combine_powers(Pas,Pbs,0,N,1,[],Power)
891 ).
892
893combine_powers([],[],_,_,_,Pi,Pi).
894combine_powers([A|As],[B|Bs],L,R,C,Pi,Po) :-
895 nf_mul(A,B,Ab),
896 nf_mul_k(Ab,C,Abc),
897 nf_add(Abc,Pi,Pii),
898 L1 is L+1,
899 R1 is R-1,
900 C1 is C*R//L1,
901 combine_powers(As,Bs,L1,R1,C1,Pii,Po).
902
903nf_power_factor(v(K,P),N,v(Kn,Pn)) :-
904 Kn is K**N,
905 mult_exp(P,N,Pn).
906
907factor_powers(0,_,Prev,[[Prev]]) :- !.
908factor_powers(N,F,Prev,[[Prev]|Ps]) :-
909 N1 is N-1,
910 mult(Prev,F,Next),
911 factor_powers(N1,F,Next,Ps).
912sum_powers(0,_,Prev,[Prev|Lt],Lt) :- !.
913sum_powers(N,S,Prev,L0,Lt) :-
914 N1 is N-1,
915 nf_mul(S,Prev,Next),
916 sum_powers(N1,S,Next,L0,[Prev|Lt]).
917
919repair(Sum,Norm) :-
920 nf_length(Sum,0,Len),
921 repair_log(Len,Sum,[],Norm).
922repair_log(0,As,As,[]) :- !.
923repair_log(1,[v(Ka,Pa)|As],As,R) :-
924 !,
925 repair_term(Ka,Pa,R).
926repair_log(2,[v(Ka,Pa),v(Kb,Pb)|As],As,R) :-
927 !,
928 repair_term(Ka,Pa,Ar),
929 repair_term(Kb,Pb,Br),
930 nf_add(Ar,Br,R).
931repair_log(N,A0,A2,R) :-
932 P is N>>1,
933 Q is N-P,
934 repair_log(P,A0,A1,Rp),
935 repair_log(Q,A1,A2,Rq),
936 nf_add(Rp,Rq,R).
937
938repair_term(K,P,Norm) :-
939 length(P,Len),
940 repair_p_log(Len,P,[],Pr,[v(1,[])],Sum),
941 nf_mul_factor(v(K,Pr),Sum,Norm).
942
943repair_p_log(0,Ps,Ps,[],L0,L0) :- !.
944repair_p_log(1,[X^P|Ps],Ps,R,L0,L1) :-
945 !,
946 repair_p(X,P,R,L0,L1).
947repair_p_log(2,[X^Px,Y^Py|Ps],Ps,R,L0,L2) :-
948 !,
949 repair_p(X,Px,Rx,L0,L1),
950 repair_p(Y,Py,Ry,L1,L2),
951 pmerge(Rx,Ry,R).
952repair_p_log(N,P0,P2,R,L0,L2) :-
953 P is N>>1,
954 Q is N-P,
955 repair_p_log(P,P0,P1,Rp,L0,L1),
956 repair_p_log(Q,P1,P2,Rq,L1,L2),
957 pmerge(Rp,Rq,R).
958
959repair_p(Term,P,[Term^P],L0,L0) :- var(Term).
960repair_p(Term,P,[],L0,L1) :-
961 nonvar(Term),
962 repair_p_one(Term,TermN),
963 nf_power(P,TermN,TermNP),
964 nf_mul(TermNP,L0,L1).
970repair_p_one(Term,TermN) :-
971 ( number(Term)
972 -> true
973 ; rational(Term)
974 ),
975 !,
976 nf_number(Term,TermN). 977repair_p_one(A1/A2,TermN) :-
978 repair(A1,A1n),
979 repair(A2,A2n),
980 !,
981 nf_div(A2n,A1n,TermN).
982repair_p_one(Term,TermN) :-
983 nonlin_1(Term,Arg,Skel,Sa),
984 repair(Arg,An),
985 !,
986 nf_nonlin_1(Skel,An,Sa,TermN).
987repair_p_one(Term,TermN) :-
988 nonlin_2(Term,A1,A2,Skel,Sa1,Sa2),
989 repair(A1,A1n),
990 repair(A2,A2n),
991 !,
992 nf_nonlin_2(Skel,A1n,A2n,Sa1,Sa2,TermN).
993repair_p_one(Term,TermN) :-
994 nf(Term,TermN).
995
996nf_length([],Li,Li).
997nf_length([_|R],Li,Lo) :-
998 Lii is Li+1,
999 nf_length(R,Lii,Lo).
1004
1006nf2term([],0).
1008nf2term([F|Fs],T) :-
1009 f02t(F,T0), 1010 yfx(Fs,T0,T). 1011
1012yfx([],T0,T0).
1013yfx([F|Fs],T0,TN) :-
1014 fn2t(F,Ft,Op),
1015 T1 =.. [Op,T0,Ft],
1016 yfx(Fs,T1,TN).
1017
1022f02t(v(K,P),T) :-
1023 ( 1024 P = []
1025 -> T = K
1026 ; K =:= 1
1027 -> p2term(P,T)
1028 ; K =:= -1
1029 -> T = -Pt,
1030 p2term(P,Pt)
1031 ; T = K*Pt,
1032 p2term(P,Pt)
1033 ).
1034
1039fn2t(v(K,P),Term,Op) :-
1040 ( K =:= 1
1041 -> Term = Pt,
1042 Op = +
1043 ; K =:= -1
1044 -> Term = Pt,
1045 Op = -
1046 ; K < 0
1047 -> Kf is -K,
1048 Term = Kf*Pt,
1049 Op = -
1050 ; Term = K*Pt,
1051 Op = +
1052 ),
1053 p2term(P,Pt).
1054
1056p2term([X^P|Xs],Term) :-
1057 ( Xs = []
1058 -> pe2term(X,Xt),
1059 exp2term(P,Xt,Term)
1060 ; Xs = [_|_]
1061 -> Term = Xst*Xtp,
1062 pe2term(X,Xt),
1063 exp2term(P,Xt,Xtp),
1064 p2term(Xs,Xst)
1065 ).
1066
1068exp2term(1,X,X) :- !.
1069exp2term(-1,X,1/X) :- !.
1070exp2term(P,X,Term) :-
1071 1072 Term = X^P.
1073
1074pe2term(X,Term) :-
1075 var(X),
1076 Term = X.
1077pe2term(X,Term) :-
1078 nonvar(X),
1079 X =.. [F|Args],
1080 pe2term_args(Args,Argst),
1081 Term =.. [F|Argst].
1082
1083pe2term_args([],[]).
1084pe2term_args([A|As],[T|Ts]) :-
1085 nf2term(A,T),
1086 pe2term_args(As,Ts).
1087
1094
1095transg(resubmit_eq(Nf)) -->
1096 {
1097 nf2term([],Z),
1098 nf2term(Nf,Term)
1099 },
1100 [clpq:{Term=Z}].
1101transg(resubmit_lt(Nf)) -->
1102 {
1103 nf2term([],Z),
1104 nf2term(Nf,Term)
1105 },
1106 [clpq:{Term<Z}].
1107transg(resubmit_le(Nf)) -->
1108 {
1109 nf2term([],Z),
1110 nf2term(Nf,Term)
1111 },
1112 [clpq:{Term=<Z}].
1113transg(resubmit_ne(Nf)) -->
1114 {
1115 nf2term([],Z),
1116 nf2term(Nf,Term)
1117 },
1118 [clpq:{Term=\=Z}].
1119transg(wait_linear_retry(Nf,Res,Goal)) -->
1120 {
1121 nf2term(Nf,Term)
1122 },
1123 [clpq:{Term=Res},Goal].
1124
1125 1128:- multifile
1129 sandbox:safe_primitive/1. 1130
1131sandbox:safe_primitive(nf_q:{_}).
1132sandbox:safe_primitive(nf_q:entailed(_))