Script en prolog para trabajo final en el ITAM con el profesor San Miguel.
%Programación Lógica
%25 de Mayo de 2004
%Trabajo Final
%MarÌa Guadalupe Gonz·lez Llama
%48364
%Alberto Alcocer Medina-Mora
%54019
%------------analisis sintactico de las restricciones--------------
consulta(U, V, R) :-
sistema_de_restricciones(U, V, R).
sistema_de_restricciones(["{", "}" | V], V, []) :- !.
sistema_de_restricciones(["{" | Restriccion], V, [R | S]) :-
restriccion(Restriccion, SubExpresion, R),
resto_restricciones(SubExpresion, ["}"| V], S).
resto_restricciones(A, A, []).
resto_restricciones(["," | OtraRestriccion], V, [R | S]) :-
restriccion(OtraRestriccion, W, R),
resto_restricciones(W, V, S).
restriccion(Expresion, V, R) :-
termino(Expresion, W, S),
resto_de_restriccion(W, V, S, R).
resto_de_restriccion(["#" | SubExpresion], V, R, dif(R, S)) :-
termino(SubExpresion, V, S).
resto_de_restriccion(["=" | SubExpresion], V, R, igual(R, S)) :-
termino(SubExpresion, V, S).
resto_de_restriccion(U, V, R, gt(R, S)):-
restriccion_sup(U, V, S).
resto_de_restriccion(U, V, R, lt(R, S)):-
restriccion_inf(U, V, S).
restriccion_sup(U, V, R):-
sup(U,W),
termino(W, V, R).
restriccion_sup(U, V, gt(R, S)):-
sup(U,W),
termino(W, Y, R),
restriccion_sup(Y, V, S).
restriccion_inf(U, V, R):-
inf(U,W),
termino(W, V, R).
restriccion_inf(U, V, lt(R, S)):-
inf(U,W),
termino(W, Y, R),
restriccion_inf(Y, V, S).
sup([">" | U], U).
sup([">=" | U], U).
inf(["<" | U], U).
inf(["<=" | U], U).
%----------evaluacion de los diferentes terminos------------------
termino(Expresion,V,Representacion):-
term1(Expresion,V,Representacion).
term1(["+"|FaltaEvaluar],V,Representacion):-
term2(FaltaEvaluar,V,Representacion).
term1(["-"|FaltaEvaluar],V,negativo(Representacion)):-
term2(FaltaEvaluar,V).
term1(Expresion,V,suma(R,S)):-
term2(Expresion,["+"|SubExpresion],R),
term2(SubExpresion,V,S).
term1(Expresion,V,resta(R,S)):-
term2(Expresion,["-"|SubExpresion],R),
term2(SubExpresion,V,S).
term1(Expresion, V, Representacion):-
term2(Expresion,V,Representacion).
term2(Expresion, V, mult(R,S)):-
entero(Expresion,W,R),
variable(W,V,S).
term2(["("|Expresion],V,mult(R,S)):-
term1(Expresion,[")"|W],R),
variable(W,V,S).
term2(Expresion,V,mult(R,S)):-
term3(Expresion,["*"|SubExpresion],R),
term3(SubExpresion,V,S).
term2(Expresion,V,div(R,S)):-
term3(Expresion,["/"|SubExpresion],R),
term2(SubExpresion,V,S).
term2(Expresion,V,Representacion):-
term3(Expresion,V,Representacion).
term3(Expresion,V,Representacion):-
term4(Expresion,V,Representacion).
term4(Expresion,V,Representacion):-
term5(Expresion,V,Representacion).
term5(Expresion,V,Representacion):-
constante(Expresion,V,Representacion).
term5(Expresion,V,Representacion):-
variable(Expresion,V,Representacion).
term5(["("|Expresion],V,Representacion):-
term1(Expresion,[")"|V],Representacion).
constante(Numero,V,Representacion):-
real(Numero,V,Representacion).
constante(Numero,V,Representacion):-
entero(Numero,V,Representacion).
entero([Numero|V],V,Representacion):-
Prueba is scan(Numero),
Representacion is integer(Prueba).
real([Numero|V],V,Representacion):-
Prueba is scan(Numero),
Representacion is float(Prueba).
variable([Var|V],V,var(Variable)):-
Variable is scan(Var),
not(number(Variable)).
%----------------------------metodos para evaluar-------------------------
evalua(X,X):-
number(X).
evalua(suma(X,Y),R):-
evalua(X,R1),
evalua(Y,R2),
R is R1+R2.
evalua(resta(X,Y),R):-
evalua(X,R1),
evalua(Y,R2),
R is R1-R2.
evalua(mult(X,Y),R):-
evalua(X,R1),
evalua(Y,R2),
R is R1*R2.
evalua(div(X,Y),R):-
evalua(X,R1),
evalua(Y,R2),
R is R1/R2.
evalua(lt(X,Y),1):-
evalua(X,R1),
evalua(Y,R2),
R1
evalua(lte(X,Y),1):-
evalua(X,R1),
evalua(Y,R2),
R1
evalua(gte(X,Y),1):-
evalua(X,R1),
evalua(Y,R2),
evalua(igual(R1,R2),1).
evalua(igual(X,Y),1):-
evalua(X,R1),
evalua(Y,R2),
R1=:=R2.
evalua(dif(X,Y),1):-
evalua(X,R1),
evalua(Y,R2),
R1==R2.
%------------------negaciones de las evaluaciones---------------------------
evalua(lt(X,Y),0):-
evalua(X,R1),
evalua(Y,R2),
not(R1
evalua(lte(X,Y),0):-
evalua(X,R1),
evalua(Y,R2),
not(R1
evalua(gte(X,Y),0):-
evalua(X,R1),
evalua(Y,R2),
evalua(igual(R1,R2),0).
evalua(igual(X,Y),0):-
evalua(X,R1),
evalua(Y,R2),
not(R1=:=R2).
evalua(dif(X,Y),0):-
evalua(X,R1),
evalua(Y,R2),
not(R1==R2).
valor(1):-
write("verdadero").
valor(0):-
write("falso").
imprime_evaluaciones([X|Y]):-
write("la restriccion: "), write(X), nl,
evalua(X,M1),
write("da un valor "), valor(M1), nl, nl,
imprime_evaluaciones(Y).
imprime_evaluaciones([]).
?-
read(S,"",s),
write("------------------------------------------"), nl,
makeList(S, L, 0),
consulta(L,V,R),-
nl,nl,nl,nl,
write("representaciones de la cadena: "), nl,
write(S), nl,
write("representaciones validas: "), nl,
write(R), nl,
write("------------------------------------------"), nl,
imprime_evaluaciones(R),
write("-------------NOTAS------------------------"), nl,
write("marcara NO al intentar evaluar algo que contenga variables"), nl,
write("ademas el valor al evaluar las suceciones no es correcta,"), nl,
write("pero la representacion si lo es"), nl.
include("Grammar Rules").
%nota: el archivo Grammar Rules debe de estar en el mismo
%- directorio que este archivo
%pruebas:
% { ( 8 * 9 ) / 5 = 8 , 3 * 5 # 5 }
% { ( 5 - 2 ) * ( 8 / 10 ) # 10 , 5 / 2 = 8 , 5 > 4 > 3 > 1 , 1 < 2 < 3 < 4 , 1 # 1 }
% { ( 3 * 2 ) / 2 = 3 , 3 * 5 # 5 }
% { 3.5 - 2.5 >= 6 }