diff --git a/polymani.pl b/polymani.pl index a48ded6..db44eca 100644 --- a/polymani.pl +++ b/polymani.pl @@ -117,12 +117,71 @@ process_input(command(CL, void)) :- %% Process only command left do_process_input(CL). + %% do_process_input(+tree) is det % % Process a single command from the input % Takes, as the only argument, the tree representing % the work to be done % +do_process_input(help_menu) :- + writeln("Please allow me to introduce myself"), + writeln("I'm a man[ual] of wealth and taste"), + writeln("I've been around for a long, long semester"), + writeln("Saved many a man's soul and faith"), + writeln("Pleased to meet you"), + writeln("Hope you guess my name"), + writeln("But what's puzzling you"), + writeln("Is the nature of my game"), + nl, + writeln("I'm a Polynomial Manipulator and the following commands are available:"), + writeln("=> Show - Allows to print a polynomial mathematically"), + writeln("=> Multiply - Allows to make multiplications"), + writeln("=> Simplify - Allows to simplify a given polynomial"), + writeln("=> Add - Allows to make sums"), + writeln("=> bye - Hello darkness, my old friend"), + writeln("Use 'tell me about {command}' to learn more about a specific command"), + nl, + writeln("Furthermore, I'm capable of memorizing polynomials during runtime. To learn more on that, type: tell me about storage"). +do_process_input(help(show)) :- + writeln("It's almost an echo of what you said, but a mathy one."), + writeln("Some example queries:"), + writeln(">"). +do_process_input(help(multiply)) :- + writeln("Multiplies a polynomial represented as an expression by a scalar resulting in a second polynomial. The two first arguments are assumed to be ground. The polynomial resulting from the sum is in simplified form."), + writeln("Some example queries:"), + writeln(">"). +do_process_input(help(simplify)) :- + writeln("Simplifies a polynomial represented as an expression as another polynomial as an expression."), + writeln("Some example queries:"), + writeln(">"). +do_process_input(help(add)) :- + writeln("Adds two polynomials as expressions resulting in a third one. The two first arguments are assumed to be ground. The polynomial resulting from the sum is in simplified form."), + writeln("Some example queries:"), + writeln(">"). +do_process_input(help(storage)) :- + writeln("Polynomials, pressed between the entries of my storage"), + writeln("Polynomials, simplified through the ages just like predicates"), + writeln("Quiet goal come floating down"), + writeln("And settle softly to the ground"), + writeln("Like golden atom leaves around my root"), + writeln("I touched them and they burst apart with sweet polynomials"), + writeln("Sweet polynomials"), + nl, + writeln("Storage manipulation is better illustrated with examples. Some example queries:"), + writeln("Asking me to memorize something:"), + writeln(">"), + nl, + writeln("Asking me to forget something:"), + writeln(">"), + nl, + writeln("Some examples of memory resources usage:"), + writeln(">"). +do_process_input(help(bye)) :- + writeln("There must be some kind of way outta here"), + writeln("Said the joker to the thief"), + writeln("There's too much confusion"), + writeln("I can't get no relief"). do_process_input(show_stored_polynomials) :- %% If the command is 'show_stored_polynomials' %% Store in D the list of all results of @@ -167,6 +226,14 @@ do_process_input(show(P, T)) :- %% Convert the input tree to a flat polynomial, to print polynomial_tree_to_polynomial(T, Pl), writeln(Pl). +do_process_input(store_simplified(V,PTNS)) :- + polynomial_tree_to_polynomial(PTNS, PNS), + simpoly(PNS,P), + assertz(polynomial_store(V, P)), + write(V), + write(" = "), + write(P), + nl. do_process_input(store(P, T)) :- ( %% If there is a polynomial stored with the same name @@ -190,14 +257,35 @@ do_process_input(simplify(PT)) :- %% gives nice error messages simpoly(P, SP), writeln(SP). +do_process_input(store_multiplication(TN, PT, V)) :- + polynomial_tree_to_polynomial(TN, N), + polynomial_tree_to_polynomial(PT, P), + ( + not(number(N)), + scalepoly(N, P, P2) + ; + number(N), + scalepoly(P, N, P2) + ), + simpoly(P2, SP), + assertz(polynomial_store(V, SP)), + write(V), + write(" = "), + write(SP), + nl. do_process_input(multiply(TN, PT)) :- %% To multiply, assume the left is a number %% Flatten both polynomial_tree_to_polynomial(TN, N), polynomial_tree_to_polynomial(PT, P), %% Use function from UI layer - scalepoly(N, P, P2), - %% Simplify + ( + not(number(N)), + scalepoly(N, P, P2) + ; + number(N), + scalepoly(P, N, P2) + ), simpoly(P2, SP), writeln(SP). @@ -311,13 +399,11 @@ parse_number_explicit(void, void, T, [WN | In], NC) :- %% and tree as arguments. The same in the other clauses parse_number_explicit(P, N, T, In, NC). parse_number_explicit(fy, NL, T, [WN | In], NC) :- - %% If we read an fy before and now an f special_word_number(WN, N, f), !, %% Add them on the left tree and recurse parse_number_explicit(f, op(+, NL, N), T, In, NC). parse_number_explicit(xfy, TL, T, [WN | In], NC) :- - %% Parsed an xfy before, now parse any number on the right TL \= void, special_word_number(WN, N, P), member(P, [f, g, fy]), @@ -452,10 +538,12 @@ parse_power(op(^, TB, TN)) --> parse_power(TB) --> parse_polynomial_variable(TB). + %% parse_operation(+Op, +stream, -not_consumed) is det % % Associate an operator with a word % +parse_operation(-) --> [minus]. parse_operation(+) --> [plus]. parse_operation(*) --> [times]. @@ -591,11 +679,12 @@ parse_polynomial_explicit(TLP-TL, T) --> %% Parse an operand on the left and place it in the tree %% on the left, through the difference structure, parse_polynomial_operand(TL), - %% if we find a plus - parse_operation(+), + %% parse either a minus or a plus + member(COp, [-, +]), + parse_operation(COp), !, - %% Recurse on the right; position the sub tree on the right - parse_polynomial_explicit(op(+, TLP, TRP)-TRP, T). + %% Recurse on the right with add; position the sub tree on the right + parse_polynomial_explicit(op(COp, TLP, TRP)-TRP, T). parse_polynomial_explicit(TLP-T, TLP) --> %% Parse an operand on the left, placing the result of the %% recusion on the TLP, through the difference structure and return that @@ -628,6 +717,10 @@ parse_polynomial_explicit(void-_, T) --> % % Parse each individual command % +parse_command(help_menu) --> + [help]. +parse_command(help(C)) --> + [tell, me, about, C]. parse_command(show_stored_polynomials) --> [show, stored, polynomials]. parse_command(forget(P)) --> @@ -655,14 +748,32 @@ parse_command(store(P, T)) --> parse_polynomial(T), [as], parse_stored_variable(P). +parse_command(store_simplified(V, P)) --> + [simplify], + parse_polynomial(P), + [as], + [V]. parse_command(simplify(T)) --> [simplify], parse_polynomial(T). +parse_command(store_multiplication(TN, TP, V)) --> + [multiply], + parse_polynomial(TN), + [by], + parse_polynomial(TP), + [as], + [V]. parse_command(multiply(TN, TP)) --> [multiply], - parse_number(TN), + parse_polynomial(TN), [by], parse_polynomial(TP). +parse_command(op(-, TN, TP)) --> + [sub], + parse_polynomial(TN), + [X], + { member(X, [to, with]) }, + parse_polynomial(TP). parse_command(op(+, TN, TP)) --> [add], parse_polynomial(TN),