Merged other
This commit is contained in:
commit
506cd53192
97
polymani.pl
97
polymani.pl
@ -72,7 +72,7 @@ simpoly(P, S) :-
|
|||||||
*/
|
*/
|
||||||
scalepoly(P1, C, S) :-
|
scalepoly(P1, C, S) :-
|
||||||
is_polynomial_valid_in_predicate(P1, "scalepoly"),
|
is_polynomial_valid_in_predicate(P1, "scalepoly"),
|
||||||
is_number_in_predicate(C, "scalepoly"),
|
is_number_valid_in_predicate(C, "scalepoly"),
|
||||||
scale_polynomial(P1, C, S),
|
scale_polynomial(P1, C, S),
|
||||||
!.
|
!.
|
||||||
%% Tests:
|
%% Tests:
|
||||||
@ -137,14 +137,14 @@ is_polynomial_as_list_valid_in_predicate(L, F) :-
|
|||||||
%@ Invalid polynomial in Test: a*4+0*x
|
%@ Invalid polynomial in Test: a*4+0*x
|
||||||
%@ false.
|
%@ false.
|
||||||
|
|
||||||
%% is_number_in_predicate(+C:number, +F:string) is det
|
%% is_number_valid_in_predicate(+C:number, +F:string) is det
|
||||||
%
|
%
|
||||||
% Validates that C is a number or prints F and it then it
|
% Validates that C is a number or prints F and it then it
|
||||||
%
|
%
|
||||||
is_number_in_predicate(C, _) :-
|
is_number_valid_in_predicate(C, _) :-
|
||||||
number(C),
|
number(C),
|
||||||
!.
|
!.
|
||||||
is_number_in_predicate(C, F) :-
|
is_number_valid_in_predicate(C, F) :-
|
||||||
%% Writes the argument and fails
|
%% Writes the argument and fails
|
||||||
write("Invalid number in "),
|
write("Invalid number in "),
|
||||||
write(F),
|
write(F),
|
||||||
@ -152,58 +152,42 @@ is_number_in_predicate(C, F) :-
|
|||||||
write(C),
|
write(C),
|
||||||
fail.
|
fail.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************
|
/*******************************
|
||||||
* NLP *
|
* NLP *
|
||||||
*******************************/
|
*******************************/
|
||||||
|
|
||||||
/* DCG */
|
%% polyplay() is det
|
||||||
separator --> ["and"].
|
%
|
||||||
separator --> ["by"].
|
% Interactive prompt for the NLP Interface
|
||||||
|
%
|
||||||
command --> ["show"].
|
|
||||||
command --> ["multiply"].
|
|
||||||
command --> ["simplify"].
|
|
||||||
command --> ["add"].
|
|
||||||
command --> ["forget"].
|
|
||||||
expression(A,B,C):-writeln("oi"),writeln(A),writeln(B),writeln(C),writeln("bye").
|
|
||||||
instruction(Left_expr, Right_expr) --> command, expression(Left_expr), separator, expression(Right_expr).
|
|
||||||
|
|
||||||
polyplay :-
|
polyplay :-
|
||||||
write("> "),
|
prompt(Old, '> '),
|
||||||
read_string(user_input, "\n", "\r\t ", _, Stdin),
|
read_string(user_input, "\n", "\r\t ", _, Stdin),
|
||||||
split_string(Stdin, " ", "", R),
|
prompt(_, Old),
|
||||||
|
string_lower(Stdin, Stdin_lower),
|
||||||
|
split_string(Stdin_lower, " ", "", LS),
|
||||||
|
maplist(string_to_atom, LS, R),
|
||||||
(
|
(
|
||||||
R == ["bye"],
|
R == [bye],
|
||||||
write("See ya"),
|
write("See ya"),
|
||||||
!
|
!
|
||||||
;
|
;
|
||||||
(
|
(
|
||||||
nlp_understand(R, P, I),
|
nlp_handler(R, Z),
|
||||||
writeln("That's trivial:"),
|
writeln(Z)
|
||||||
nlp_compute(P, I)
|
|
||||||
;
|
;
|
||||||
writeln("I didn't understand what you want.")
|
writeln("I didn't understand what you want.")
|
||||||
),
|
),
|
||||||
polyplay
|
polyplay
|
||||||
),
|
),
|
||||||
!.
|
!.
|
||||||
|
|
||||||
nlp_understand(R, P, I) :-
|
%% nlp_number(?W:Atom, ?D:Int) is det
|
||||||
(
|
%
|
||||||
R == ["simplify", "x", "squared"],
|
% Definition of a Alphabetical and Numerical relation
|
||||||
P = simplify,
|
%
|
||||||
I = x^2
|
|
||||||
;
|
|
||||||
fail
|
|
||||||
).
|
|
||||||
|
|
||||||
nlp_compute(simplify, P) :-
|
|
||||||
!,
|
|
||||||
simplify_polynomial(P, O),
|
|
||||||
writeln(O).
|
|
||||||
nlp_compute(_,_) :-
|
|
||||||
fail.
|
|
||||||
|
|
||||||
special_word_number(zero, 0, f).
|
special_word_number(zero, 0, f).
|
||||||
special_word_number(a, 1, f).
|
special_word_number(a, 1, f).
|
||||||
special_word_number(one, 1, f).
|
special_word_number(one, 1, f).
|
||||||
@ -237,6 +221,7 @@ 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).
|
||||||
|
|
||||||
|
%% Entry point
|
||||||
parse_number_explicit(void, void, T, [WN | R], NC) :-
|
parse_number_explicit(void, void, T, [WN | R], NC) :-
|
||||||
special_word_number(WN, N, P),
|
special_word_number(WN, N, P),
|
||||||
member(P, [f, g, fy]),
|
member(P, [f, g, fy]),
|
||||||
@ -245,14 +230,14 @@ parse_number_explicit(void, void, T, [WN | R], NC) :-
|
|||||||
parse_number_explicit(fy, NL, T, [WN | R], NC) :-
|
parse_number_explicit(fy, NL, T, [WN | R], NC) :-
|
||||||
special_word_number(WN, N, f),
|
special_word_number(WN, N, f),
|
||||||
!,
|
!,
|
||||||
parse_number_explicit(P, op(+, NL, N), T, R, NC).
|
parse_number_explicit(f, op(+, NL, N), T, R, NC).
|
||||||
parse_number_explicit(xfy, TL, T, [WN | R], NC) :-
|
parse_number_explicit(xfy, TL, T, [WN | R], NC) :-
|
||||||
TL \= void,
|
TL \= void,
|
||||||
special_word_number(WN, N, P),
|
special_word_number(WN, N, P),
|
||||||
member(P, [f, g, fy]),
|
member(P, [f, g, fy]),
|
||||||
!,
|
!,
|
||||||
parse_number_explicit(P, op(+, TL, N), T, R, NC).
|
parse_number_explicit(P, op(+, TL, N), T, R, NC).
|
||||||
parse_number_explicit(P, TL, T, [WN | R], NC) :-
|
parse_number_explicit(_, TL, T, [WN | R], NC) :-
|
||||||
special_word_number(WN, N, xfy),
|
special_word_number(WN, N, xfy),
|
||||||
TL \= void,
|
TL \= void,
|
||||||
!,
|
!,
|
||||||
@ -309,18 +294,18 @@ parse_number(T, SL, NC) :-
|
|||||||
%% ?- parse_number(T, [foo, five, million], NC).
|
%% ?- parse_number(T, [foo, five, million], NC).
|
||||||
%@ false.
|
%@ false.
|
||||||
|
|
||||||
operations(times, *).
|
parse_operation(+) --> [plus].
|
||||||
operations(plus, +).
|
parse_operation(*) --> [times].
|
||||||
|
|
||||||
parse_operation(Op) --> [WOp], { operations(WOp, Op) }.
|
|
||||||
|
|
||||||
parse_polynomial_operand(T) --> parse_number(T).
|
parse_polynomial_operand(T) --> parse_number(T).
|
||||||
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).
|
||||||
|
|
||||||
parse_stored_variable(op(load, P, void)) --> %% NOTE Not sure if it's better to load now or later
|
:- dynamic polynomial_store/2.
|
||||||
|
|
||||||
|
parse_stored_variable(T) --> %% NOTE Not sure if it's better to load now or later
|
||||||
[P],
|
[P],
|
||||||
polynomial_store(P, T).
|
{ polynomial_store(P, T) }.
|
||||||
|
|
||||||
parse_polynomial_variable(B) -->
|
parse_polynomial_variable(B) -->
|
||||||
[B],
|
[B],
|
||||||
@ -361,7 +346,9 @@ parse_polynomial(TLP-TL, TLP) -->
|
|||||||
!,
|
!,
|
||||||
{ TL \= void }.
|
{ TL \= void }.
|
||||||
parse_polynomial(void-_, T) -->
|
parse_polynomial(void-_, T) -->
|
||||||
parse_polynomial_operand(T), { T \= void }.
|
parse_polynomial_operand(T),
|
||||||
|
!,
|
||||||
|
{ T \= void }.
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- parse_polynomial(_-_, T, [], _).
|
%% ?- parse_polynomial(_-_, T, [], _).
|
||||||
%@ false.
|
%@ false.
|
||||||
@ -386,20 +373,20 @@ parse_polynomial(void-_, T) -->
|
|||||||
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
|
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
|
||||||
|
|
||||||
|
|
||||||
parse_command(op(show, T, void)) --> %% NOTE Probably easier if the tree is always binary
|
parse_command(show(T)) --> %% NOTE Probably easier if the tree is always binary
|
||||||
[show],
|
[show],
|
||||||
parse_polynomial(T).
|
parse_polynomial(T).
|
||||||
parse_command(op(show, op(store, P, T))) -->
|
parse_command(show(store(P, T))) -->
|
||||||
[show],
|
[show],
|
||||||
parse_polynomial(T),
|
parse_polynomial(T),
|
||||||
[as],
|
[as],
|
||||||
[P].
|
[P].
|
||||||
parse_command(op(store, P, T)) -->
|
parse_command(store(P, T)) -->
|
||||||
[let],
|
[let],
|
||||||
[P],
|
[P],
|
||||||
[be],
|
[be],
|
||||||
parse_polynomial(T).
|
parse_polynomial(T).
|
||||||
parse_command(T) -->
|
parse_command(simplify(T)) -->
|
||||||
[simplify],
|
[simplify],
|
||||||
parse_polynomial(T).
|
parse_polynomial(T).
|
||||||
parse_command(op(*, TN, TP)) -->
|
parse_command(op(*, TN, TP)) -->
|
||||||
@ -824,8 +811,8 @@ simplify_polynomial_as_list(L, L13) :-
|
|||||||
%% Sort list converting back gives the result in the correct order
|
%% Sort list converting back gives the result in the correct order
|
||||||
sort(0, @=<, L11, L12),
|
sort(0, @=<, L11, L12),
|
||||||
(
|
(
|
||||||
%% If the list is empty, the result is a list with 0
|
%% If the list is empty, the result is a list with 0
|
||||||
L12 = [], L13 = [0]
|
L12 = [], L13 = [0]
|
||||||
;
|
;
|
||||||
%% Otherwise, this is the result
|
%% Otherwise, this is the result
|
||||||
L13 = L12
|
L13 = L12
|
||||||
|
Reference in New Issue
Block a user