Big progress\n\nCan now properly use memory, mostly, simplify, parse literal numbers\nFixed some bugs
This commit is contained in:
parent
8c2c66c6ba
commit
05fcfcbcb2
154
polymani.pl
154
polymani.pl
@ -37,19 +37,23 @@
|
|||||||
* NLP *
|
* NLP *
|
||||||
*******************************/
|
*******************************/
|
||||||
|
|
||||||
|
debug_print(false).
|
||||||
|
|
||||||
%% polyplay() is det
|
%% polyplay() is det
|
||||||
%
|
%
|
||||||
% Interactive prompt for the NLP Interface
|
% Interactive prompt for the NLP Interface
|
||||||
%
|
%
|
||||||
polyplay :-
|
polyplay :-
|
||||||
prompt(OldPrompt, '> '),
|
prompt(OldPrompt, '> '),
|
||||||
read_string(user_input, "\n", "\r\t ", _, In),
|
read_line_to_codes(user_input, InCodes),
|
||||||
|
%% read_string(user_input, "\n", "\r\t ", _, In),
|
||||||
prompt(_, OldPrompt),
|
prompt(_, OldPrompt),
|
||||||
split_string(In, " ", "", LS),
|
split_string(InCodes, " ", "\r\t", LS),
|
||||||
maplist(atom_string, LA, LS),
|
maplist(name, LA, LS),
|
||||||
(
|
(
|
||||||
LA == [bye],
|
LA == [bye],
|
||||||
write("See ya"),
|
write("See ya"),
|
||||||
|
nl,
|
||||||
!
|
!
|
||||||
;
|
;
|
||||||
(
|
(
|
||||||
@ -58,9 +62,17 @@ polyplay :-
|
|||||||
TIn == void,
|
TIn == void,
|
||||||
writeln("I didn't understand what you want."),
|
writeln("I didn't understand what you want."),
|
||||||
writeln(NC)
|
writeln(NC)
|
||||||
|
;
|
||||||
|
(
|
||||||
|
debug_print(true),
|
||||||
|
write(LA),
|
||||||
|
nl,
|
||||||
|
write(TIn),
|
||||||
|
nl
|
||||||
;
|
;
|
||||||
process_input(TIn)
|
process_input(TIn)
|
||||||
)
|
)
|
||||||
|
)
|
||||||
;
|
;
|
||||||
writeln("I didn't understand what you want.")
|
writeln("I didn't understand what you want.")
|
||||||
),
|
),
|
||||||
@ -68,60 +80,74 @@ polyplay :-
|
|||||||
),
|
),
|
||||||
!.
|
!.
|
||||||
|
|
||||||
%process_input(show(P, void)) :-
|
process_input(command(CL, void)) :-
|
||||||
% P \== void,
|
do_process_input(CL).
|
||||||
% polynomial_store(P, T),
|
process_input(command(CL, TCR)) :-
|
||||||
% write(P),
|
%% Process first command
|
||||||
% write(" = "),
|
do_process_input(CL),
|
||||||
% print_polynomial_tree(T),
|
%% If there's a tree on the right
|
||||||
% nl.
|
TCR \== void,
|
||||||
process_input(command(C1, C2)) :-
|
%% recurse
|
||||||
process_input(C1),
|
process_input(TCR).
|
||||||
process_input(C2).
|
|
||||||
process_input(show_stored_polynomials) :-
|
do_process_input(show_stored_polynomials) :-
|
||||||
findall(nm(X,Y), polynomial_store(X,Y), D),
|
findall(nm(X,Y), polynomial_store(X,Y), D),
|
||||||
nlp_print_memory(D).
|
nlp_print_memory(D).
|
||||||
process_input(show(P, T)) :-
|
do_process_input(show(load(P), void)) :-
|
||||||
|
P \== void,
|
||||||
|
(
|
||||||
|
polynomial_store(P, T),
|
||||||
|
write(P),
|
||||||
|
write(" = "),
|
||||||
|
print_polynomial_tree(T),
|
||||||
|
nl
|
||||||
|
;
|
||||||
|
write("Variable not stored"),
|
||||||
|
nl
|
||||||
|
).
|
||||||
|
do_process_input(show(P, T)) :-
|
||||||
P \== void,
|
P \== void,
|
||||||
T \== void,
|
T \== void,
|
||||||
write(P),
|
write(P),
|
||||||
write(" = "),
|
write(" = "),
|
||||||
print_polynomial_tree(T),
|
print_polynomial_tree(T),
|
||||||
nl.
|
nl.
|
||||||
process_input(show(void, T)) :-
|
do_process_input(show(void, T)) :-
|
||||||
T \== void,
|
T \== void,
|
||||||
print_polynomial_tree(T),
|
print_polynomial_tree(T),
|
||||||
nl.
|
nl.
|
||||||
process_input(store(P, T)) :-
|
do_process_input(store(P, T)) :-
|
||||||
assertz(polynomial_store(P, T)),
|
assertz(polynomial_store(P, T)),
|
||||||
write(P),
|
write(P),
|
||||||
write(" = "),
|
write(" = "),
|
||||||
print_polynomial_tree(T),
|
print_polynomial_tree(T),
|
||||||
nl.
|
nl.
|
||||||
process_input(forget(P)) :-
|
do_process_input(forget(P)) :-
|
||||||
retract(polynomial_store(P,_)).
|
retract(polynomial_store(P,_)).
|
||||||
process_input(simplify(P)) :-
|
do_process_input(simplify(P)) :-
|
||||||
polynomial_tree_to_polynomial(P, PP),
|
polynomial_tree_to_polynomial(P, PP),
|
||||||
simpoly(PP, SP),
|
simpoly(PP, SP),
|
||||||
write(SP),
|
write(SP),
|
||||||
nl.
|
nl.
|
||||||
process_input(op(Op, TL, TR)) :-
|
%% do_process_input(T) :-
|
||||||
process_input(simplify(op(Op, TL, TR))).
|
%% %% Empty command, assume simplfiy
|
||||||
|
%% do_process_input(simplify(T)).
|
||||||
|
|
||||||
print_polynomial_tree(op(Op, TL, TR)) :-
|
print_polynomial_tree(op(Op, TL, TR)) :-
|
||||||
|
!,
|
||||||
write(TL),
|
write(TL),
|
||||||
write(Op),
|
write(Op),
|
||||||
|
TR \== void,
|
||||||
print_polynomial_tree(TR).
|
print_polynomial_tree(TR).
|
||||||
print_polynomial_tree(T) :-
|
print_polynomial_tree(T) :-
|
||||||
write(T).
|
write(T).
|
||||||
|
|
||||||
polynomial_tree_to_polynomial(A,B) :-
|
polynomial_tree_to_polynomial(A,B) :-
|
||||||
polynomial_tree_to_atomic_polynomial(A,Pa),
|
polynomial_tree_to_atomic_polynomial(A,Pa),
|
||||||
term_to_atom(B, Pa).
|
name(B, Pa).
|
||||||
|
|
||||||
polynomial_tree_to_atomic_polynomial(load(L), P) :-
|
polynomial_tree_to_atomic_polynomial(load(L), P) :-
|
||||||
polynomial_store(L, P).
|
polynomial_store(L, P).
|
||||||
|
|
||||||
polynomial_tree_to_atomic_polynomial(op(Op, TL, TR), P) :-
|
polynomial_tree_to_atomic_polynomial(op(Op, TL, TR), P) :-
|
||||||
polynomial_tree_to_atomic_polynomial(TL,A),
|
polynomial_tree_to_atomic_polynomial(TL,A),
|
||||||
polynomial_tree_to_atomic_polynomial(TR,B),
|
polynomial_tree_to_atomic_polynomial(TR,B),
|
||||||
@ -166,7 +192,8 @@ special_word_number(ninety, 90, fy).
|
|||||||
special_word_number(hundred, 100, xfy).
|
special_word_number(hundred, 100, xfy).
|
||||||
special_word_number(thousand, 1000, xfy).
|
special_word_number(thousand, 1000, xfy).
|
||||||
special_word_number(million, 1000000, xfy).
|
special_word_number(million, 1000000, xfy).
|
||||||
special_word_number(IC, IC, _) :- number(IC).
|
%% NOTE This does not belong here. The time wasted on this... Don't put this here
|
||||||
|
%% special_word_number(IC, IC, _) :- number(IC).
|
||||||
|
|
||||||
%% nlp_number(?W:Atom, ?D:Int) is det
|
%% nlp_number(?W:Atom, ?D:Int) is det
|
||||||
%
|
%
|
||||||
@ -207,12 +234,15 @@ parse_number_explicit(_, T, T, [], []) :-
|
|||||||
!.
|
!.
|
||||||
|
|
||||||
parse_number(T, SL, NC) :-
|
parse_number(T, SL, NC) :-
|
||||||
parse_number_explicit(void, void, T1, SL, NC),
|
parse_number_explicit(void, void, T, SL, NC).
|
||||||
polynomial_tree_to_polynomial(T1, PP),
|
%% NOTE This is not supposed to be here.
|
||||||
simpoly(PP, T).
|
%% polynomial_tree_to_polynomial(T1, PP),
|
||||||
|
%% simpoly(PP, T2).
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- parse_number(T, [two], _).
|
%% ?- parse_number(T, [two], _).
|
||||||
%@ T = 2.
|
%@ T = 2.
|
||||||
|
%% ?- parse_number(T, [43], _).
|
||||||
|
%@ T = 43.
|
||||||
%% ?- parse_number(T, [nineteen, two], _).
|
%% ?- parse_number(T, [nineteen, two], _).
|
||||||
%@ false.
|
%@ false.
|
||||||
%% ?- parse_number(T, [twenty], _).
|
%% ?- parse_number(T, [twenty], _).
|
||||||
@ -271,14 +301,30 @@ parse_operation(+) --> [plus].
|
|||||||
parse_operation(*) --> [times].
|
parse_operation(*) --> [times].
|
||||||
|
|
||||||
parse_polynomial_operand(T) --> parse_number(T).
|
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_power(T).
|
||||||
parse_polynomial_operand(T) --> parse_stored_variable(T).
|
%% parse_polynomial_operand(T) --> parse_stored_variable(T).
|
||||||
|
%% Tests:
|
||||||
|
%% ?- parse_polynomial_operand(N, [3], _).
|
||||||
|
%@ N = 3.
|
||||||
|
|
||||||
:- dynamic polynomial_store/2.
|
:- dynamic polynomial_store/2.
|
||||||
|
|
||||||
parse_stored_variable(P) --> %% NOTE Not sure if it's better to load now or later
|
parse_stored_variable(P) -->
|
||||||
[load(P)],
|
[P],
|
||||||
{ polynomial_store(P, _) }.
|
{
|
||||||
|
atom_codes(P, L),
|
||||||
|
cons(F, R, L),
|
||||||
|
code_type(F, prolog_var_start),
|
||||||
|
maplist(code_type_swap(prolog_identifier_continue), R)
|
||||||
|
}.
|
||||||
|
%% Tests:
|
||||||
|
%% ?- parse_stored_variable(P, ['P1'], _).
|
||||||
|
%@ P = 'P1'.
|
||||||
|
%% ?- parse_stored_variable(P, ['x1'], _).
|
||||||
|
%@ false.
|
||||||
|
|
||||||
|
code_type_swap(X, Y) :- code_type(Y, X).
|
||||||
|
|
||||||
parse_polynomial_variable(B) -->
|
parse_polynomial_variable(B) -->
|
||||||
[B],
|
[B],
|
||||||
@ -289,6 +335,8 @@ parse_polynomial(T, NC, NC) :-
|
|||||||
!.
|
!.
|
||||||
parse_polynomial(T) -->
|
parse_polynomial(T) -->
|
||||||
parse_polynomial_explicit(_-_, T).
|
parse_polynomial_explicit(_-_, T).
|
||||||
|
parse_polynomial(T) -->
|
||||||
|
parse_stored_variable(T).
|
||||||
|
|
||||||
parse_polynomial_explicit(void-_, T) -->
|
parse_polynomial_explicit(void-_, T) -->
|
||||||
parse_polynomial_operand(TL),
|
parse_polynomial_operand(TL),
|
||||||
@ -337,33 +385,38 @@ parse_polynomial_explicit(void-_, T) -->
|
|||||||
%@ T = op(+, 2, op(+, op(*, x, 4), op(^, y, 5))).
|
%@ T = op(+, 2, op(+, op(*, x, 4), op(^, y, 5))).
|
||||||
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
|
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
|
||||||
%@ T = op(+, op(+, 2, 2), op(*, 1, 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) -->
|
parse_command(show_stored_polynomials) -->
|
||||||
[show, stored, polynomials].
|
[show, stored, polynomials].
|
||||||
parse_command(store(P, T)) -->
|
parse_command(command(show(P, T),
|
||||||
|
command(store(P, T),
|
||||||
|
void))) -->
|
||||||
[show],
|
[show],
|
||||||
parse_polynomial(T),
|
parse_polynomial(T),
|
||||||
[as],
|
[as],
|
||||||
[P].
|
parse_stored_variable(P).
|
||||||
parse_command(forget(P)) -->
|
parse_command(forget(P)) -->
|
||||||
[forget],
|
[forget],
|
||||||
[P].
|
parse_stored_variable(P).
|
||||||
|
parse_command(show(load(P), void)) -->
|
||||||
|
[show],
|
||||||
|
parse_stored_variable(P).
|
||||||
parse_command(show(void, T)) --> %% NOTE Probably easier if the tree is always binary
|
parse_command(show(void, T)) --> %% NOTE Probably easier if the tree is always binary
|
||||||
[show],
|
[show],
|
||||||
parse_polynomial(T).
|
parse_polynomial(T),
|
||||||
parse_command(show(P, void)) -->
|
{ nonvar(T) }.
|
||||||
[show],
|
|
||||||
[P].
|
|
||||||
parse_command(store(P, T)) -->
|
parse_command(store(P, T)) -->
|
||||||
[let],
|
[let],
|
||||||
[P],
|
parse_stored_variable(P),
|
||||||
[be],
|
[be],
|
||||||
parse_polynomial(T).
|
parse_polynomial(T).
|
||||||
parse_command(store(P, T)) -->
|
parse_command(store(P, T)) -->
|
||||||
[store],
|
[store],
|
||||||
parse_polynomial(T),
|
parse_polynomial(T),
|
||||||
[as],
|
[as],
|
||||||
[P].
|
parse_stored_variable(P).
|
||||||
parse_command(simplify(T)) -->
|
parse_command(simplify(T)) -->
|
||||||
[simplify],
|
[simplify],
|
||||||
parse_polynomial(T).
|
parse_polynomial(T).
|
||||||
@ -377,16 +430,20 @@ parse_command(op(+, TN, TP)) -->
|
|||||||
parse_polynomial(TN),
|
parse_polynomial(TN),
|
||||||
[with],
|
[with],
|
||||||
parse_polynomial(TP).
|
parse_polynomial(TP).
|
||||||
|
%% Tests:
|
||||||
|
%% ?- parse_command(T, [show, 3], NC).
|
||||||
|
%@ T = show(void, 3),
|
||||||
|
%@ NC = [].
|
||||||
|
|
||||||
parse_input(command(TCL, TCR)) -->
|
parse_input(command(TCL, TCR)) -->
|
||||||
parse_command(TCL),
|
parse_command(TCL),
|
||||||
[and],
|
[and],
|
||||||
!,
|
!,
|
||||||
parse_input(TCR).
|
parse_input(TCR).
|
||||||
parse_input(TC) -->
|
parse_input(command(TC, void)) -->
|
||||||
parse_command(TC).
|
parse_command(TC).
|
||||||
parse_input(void, [], _).
|
%% ?- parse_input(CT, [show, 3], _).
|
||||||
|
%@ CT = command(show(void, 3), void).
|
||||||
|
|
||||||
%% nlp_print_memory
|
%% nlp_print_memory
|
||||||
%
|
%
|
||||||
@ -431,6 +488,9 @@ simpoly(P, S) :-
|
|||||||
is_polynomial_valid_in_predicate(P, "simpoly"),
|
is_polynomial_valid_in_predicate(P, "simpoly"),
|
||||||
simplify_polynomial(P, S),
|
simplify_polynomial(P, S),
|
||||||
!.
|
!.
|
||||||
|
%% Tests:
|
||||||
|
%% ?- simpoly(2+2+1*y, S).
|
||||||
|
%@ S = y+4.
|
||||||
|
|
||||||
/*
|
/*
|
||||||
scalepoly/3 multiplies a polynomial represented as an expression by a scalar
|
scalepoly/3 multiplies a polynomial represented as an expression by a scalar
|
||||||
@ -910,7 +970,7 @@ simplify_polynomial(P, P2) :-
|
|||||||
%
|
%
|
||||||
% Simplifies a polynomial represented as a list.
|
% Simplifies a polynomial represented as a list.
|
||||||
%
|
%
|
||||||
simplify_polynomial_as_list(L, L13) :-
|
simplify_polynomial_as_list(L, L14) :-
|
||||||
%% Convert each term to a list
|
%% Convert each term to a list
|
||||||
maplist(term_to_list, L, L2),
|
maplist(term_to_list, L, L2),
|
||||||
%% Sort each sublist so that the next
|
%% Sort each sublist so that the next
|
||||||
@ -941,12 +1001,16 @@ simplify_polynomial_as_list(L, L13) :-
|
|||||||
;
|
;
|
||||||
%% Otherwise, this is the result
|
%% Otherwise, this is the result
|
||||||
L13 = L12
|
L13 = L12
|
||||||
).
|
),
|
||||||
|
%% Further make sure all terms are simplified
|
||||||
|
maplist(simplify_term, L13, L14).
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- simplify_polynomial_as_list([x, 1, x^2, x*y, 3*x^2, 4*x], L).
|
%% ?- simplify_polynomial_as_list([x, 1, x^2, x*y, 3*x^2, 4*x], L).
|
||||||
%@ L = [1, 4*x^2, 5*x, x*y] .
|
%@ L = [1, 4*x^2, 5*x, x*y] .
|
||||||
%% ?- simplify_polynomial_as_list([1, x^2, x*y, 3*x^2, -4, -1*x], L).
|
%% ?- simplify_polynomial_as_list([1, x^2, x*y, 3*x^2, -4, -1*x], L).
|
||||||
%@ L = [-3, -1*x, 4*x^2, x*y] .
|
%@ L = [-3, -1*x, 4*x^2, x*y] .
|
||||||
|
%% ?- simplify_polynomial_as_list([1, 1*x], L).
|
||||||
|
%@ L = [1, x] .
|
||||||
%% ?- simplify_polynomial_as_list([0*x, 0], L).
|
%% ?- simplify_polynomial_as_list([0*x, 0], L).
|
||||||
%@ L = [0] .
|
%@ L = [0] .
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user