Bug fixes, support for floating point numbers

This commit is contained in:
Hugo Sales 2018-12-19 23:27:12 +00:00
parent c057493d86
commit e9a739b33e
1 changed files with 74 additions and 48 deletions

View File

@ -118,14 +118,14 @@ do_process_input(show(void, T)) :-
nl.
do_process_input(store(P, T)) :-
assertz(polynomial_store(P, T)).
do_process_input(show(store(P, T))) :-
assertz(polynomial_store(P, T)),
write(P),
write(" = "),
print_polynomial_tree(T),
nl.
%% do_process_input(show(store(P, T))) :-
%% assertz(polynomial_store(P, T)),
%% write(P),
%% write(" = "),
%% print_polynomial_tree(T),
%% nl.
do_process_input(forget(P)) :-
retract(polynomial_store(P,_)).
retract(polynomial_store(P, _)).
do_process_input(simplify(PT)) :-
polynomial_tree_to_polynomial(PT, P),
simpoly(P, SP),
@ -198,8 +198,10 @@ special_word_number(ninety, 90, fy).
special_word_number(hundred, 100, xfy).
special_word_number(thousand, 1000, xfy).
special_word_number(million, 1000000, xfy).
%% NOTE This does not belong here. The time wasted on this... Don't put this here
%% special_word_number(IC, IC, _) :- number(IC).
%% special_word_number(half, 0.5, xf).
%% special_word_number(third, 0.33333, xf).
%% special_word_number(quarter, 0.25, xf).
%% nlp_number(?W:Atom, ?D:Int) is det
%
@ -239,8 +241,16 @@ parse_number_explicit(_, T, T, [], []) :-
T \= void,
!.
parse_number(T, SL, NC) :-
parse_number_explicit(void, void, T, SL, NC).
parse_number(op('.', TL, TR)) -->
parse_number_explicit(void, void, TL),
[X],
{ member(X, [point, dot]), ! },
parse_number_explicit(void, void, TR).
parse_number(N) -->
[N],
{ number(N), ! }.
parse_number(T) -->
parse_number_explicit(void, void, T).
%% NOTE This is not supposed to be here.
%% polynomial_tree_to_polynomial(T1, PP),
%% simpoly(PP, T2).
@ -253,6 +263,9 @@ parse_number(T, SL, NC) :-
%@ false.
%% ?- parse_number(T, [twenty], _).
%@ T = 20.
%% ?- parse_number(T, [twenty, point, two], NC).
%@ T = op('.', 20, 2),
%@ NC = [].
%% ?- parse_number(T, [twenty, twenty], _).
%@ false.
%% ?- parse_number(T, [twenty, one], _).
@ -307,12 +320,7 @@ parse_operation(+) --> [plus].
parse_operation(*) --> [times].
parse_polynomial_operand(T) --> parse_number(T).
parse_polynomial_operand(N) --> [N], { number(N), ! }.
parse_polynomial_operand(T) --> parse_power(T).
%% parse_polynomial_operand(T) --> parse_stored_variable(T).
%% Tests:
%% ?- parse_polynomial_operand(N, [3], _).
%@ N = 3.
:- dynamic polynomial_store/2.
@ -336,13 +344,47 @@ parse_polynomial_variable(B) -->
[B],
{ polynomial_variable(B) }.
parse_polynomial(T) -->
[polynomial],
{ ! },
parse_polynomial_explicit(_-_, T).
parse_polynomial(T, NC, NC) :-
not(parse_polynomial_explicit(_-_, T, NC, _)),
!.
parse_polynomial(T) -->
parse_polynomial_explicit(_-_, T).
parse_polynomial_explicit(_-_, T),
!.
parse_polynomial(T) -->
parse_stored_variable(T).
parse_stored_variable(T),
!.
%% Tests:
%% ?- parse_polynomial(T, [], _).
%@ false.
%% ?- parse_polynomial(T, [two], _).
%@ T = 2.
%% ?- parse_polynomial(T, [two, times, three], _).
%@ T = op(*, 2, 3).
%% ?- parse_polynomial(T, [two, times, three, plus, four], _).
%@ T = op(+, op(*, 2, 3), 4).
%% ?- parse_polynomial(T, [two, plus, three, times, four], _).
%@ T = op(+, 2, op(*, 3, 4)).
%% ?- parse_polynomial(T, [two, plus, three, times, four, plus, six, times, five], _).
%@ T = op(+, 2, op(+, op(*, 3, 4), op(*, 6, 5))).
%% ?- parse_polynomial(T, [two, times, times, two], NC), write(T).
%@ _2986 %% NOTE Potential problem. It seems NC isn't unified with the list, if it fails
%@ NC = [two, times, times, two].
%% ?- parse_polynomial(T, [two, plus, x, times, four], _).
%@ T = op(+, 2, op(*, x, 4)).
%% ?- parse_polynomial(T, [two, plus, x, times, four, plus, y, raised, to, five], _).
%@ T = op(+, 2, op(+, op(*, x, 4), op(^, y, 5))).
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
%@ true.
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
%% ?- parse_polynomial(T, [polynomial, 2, plus, 3, plus, 4, y], NC).
%@ T = op(+, op(+, 2, 3), op(*, 4, y)),
%@ NC = [].
parse_polynomial_explicit(void-_, T) -->
parse_polynomial_operand(TL),
@ -359,6 +401,10 @@ parse_polynomial_explicit(TLP-T, TLP) -->
parse_operation(*),
!,
parse_polynomial_explicit(op(*, TL, TRP)-TRP, T).
parse_polynomial_explicit(TLP-T, TLP) -->
parse_polynomial_operand(TL),
parse_polynomial_explicit(op(*, TL, TRP)-TRP, T),
!.
parse_polynomial_explicit(TLP-TL, TLP) -->
{ TLP \= void },
parse_polynomial_operand(TL),
@ -368,37 +414,12 @@ parse_polynomial_explicit(void-_, T) -->
parse_polynomial_operand(T),
!,
{ T \= void }.
%% Tests:
%% ?- parse_polynomial(T, [], _).
%@ false.
%% ?- parse_polynomial(T, [two], _).
%@ T = 2.
%% ?- parse_polynomial(T, [two, times, three], _).
%@ T = op(*, 2, 3).
%% ?- parse_polynomial(T, [two, times, three, plus, four], _).
%@ T = op(+, op(*, 2, 3), 4).
%% ?- parse_polynomial(T, [two, plus, three, times, four], _).
%@ T = op(+, 2, op(*, 3, 4)).
%% ?- parse_polynomial(T, [two, plus, three, times, four, plus, six, times, five], _).
%@ T = op(+, 2, op(+, op(*, 3, 4), op(*, 6, 5))).
%% ?- parse_polynomial(T, [two, times, times, two], NC); write(NC).
%@ NC = [two, times, times, two] ;
%@ _2006
%@ true. %% NOTE Potential problem. It seems NC isn't unified with the list, if it fails
%% ?- parse_polynomial(T, [two, plus, x, times, four], _).
%@ T = op(+, 2, op(*, x, 4)).
%% ?- parse_polynomial(T, [two, plus, x, times, four, plus, y, raised, to, five], _).
%@ T = op(+, 2, op(+, op(*, x, 4), op(^, y, 5))).
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
%% ?- parse_polynomial(T, [2, plus, 3, plus, 1, times, y], _).
%@ T = op(+, op(+, 2, 3), op(*, 1, y)).
parse_command(show_stored_polynomials) -->
[show, stored, polynomials].
parse_command(command(show(P, T),
command(store(P, T),
void))) -->
parse_command(show(P, T),
command(store(P, T),
void)) -->
[show],
parse_polynomial(T),
[as],
@ -434,12 +455,17 @@ parse_command(multiply(TN, TP)) -->
parse_command(op(+, TN, TP)) -->
[add],
parse_polynomial(TN),
[with],
[X],
{ member(X, [to, with]) },
parse_polynomial(TP).
%% Tests:
%% ?- parse_command(T, [show, 3], NC).
%@ T = show(void, 3),
%@ NC = [].
%@ NC = [] .
%% ?- parse_command(T, [add, 3, plus, x, to, 4, plus, x], NC).
%@ T = op(+, op(+, 3, x), op(+, 4, x)),
%@ NC = [] ;
%@ false.
parse_input(command(TCL, TCR)) -->
parse_command(TCL),