2010-01-11 21 views
5

Estoy tratando de hacer coincidir un subconjunto de los hechos que estoy creando, ¡y mi testcase funcionó de maravilla!Argumentos Prolog no están lo suficientemente instanciados

 
x([1,2,3,4],'bleah'). 
x([1,2,4],'bleah2'). 
x([1,2],'bleah8'). 
x([1,3,4],'bleah3'). 
x([5,6,7,8],'bleah5'). 
x([6,7,8,9],'bleah6'). 

fuzzy(X,R) :- x(Z, R), subset(X,Z) . 
remaining(X,Y,D,M) :- x(Z,D) , select(X,Z,N), select(Y,N,M). 
pair(X,Y,R) :- x([X,Y],R) ; x([Y,X],R). 

Output: 
?- x([1,2|REST],D). 
REST = [3, 4], 
D = bleah ; 
REST = [4], 
D = bleah2 ; 
REST = [], 
D = bleah8 ; 
false. 

?- pair(2,1,D). 
D = bleah8 ; 
false. 

?- fuzzy([2,1],R). 
R = bleah ; 
R = bleah2 ; 
R = bleah8 ; 
false. 

?- remaining(2,1,D,M). 
D = bleah, 
M = [3, 4] ; 
D = bleah2, 
M = [4] ; 
D = bleah8, 
M = [] ; 
false. 

Luego agregué un hecho para representar mi próximo caso potencial, y ahora está bastante roto. Soy nuevo en Prolog, no estoy seguro de por qué es esto o cómo solucionarlo.

 
x([6,X,8,9],'woot') :- (X+0) > 7. 

Output: 
?- x([1,2|REST],D). 
REST = [3, 4], 
D = bleah ; 
REST = [4], 
D = bleah2 ; 
REST = [], 
D = bleah8 ; 
false. 

?- pair(2,1,D). 
D = bleah8 ; 
false. 

?- fuzzy([2,1],R). 
R = bleah ; 
R = bleah2 ; 
R = bleah8 ; 
ERROR: >/2: Arguments are not sufficiently instantiated 
^ Exception: (9) _G260+0>7 ? abort 
% Execution Aborted 

?- remaining(2,1,D,M). 
D = bleah, 
M = [3, 4] ; 
D = bleah2, 
M = [4] ; 
D = bleah8, 
M = [] ; 
ERROR: >/2: Arguments are not sufficiently instantiated 
^ Exception: (10) _G270+0>7 ? abort 
% Execution Aborted 

?- x([_,15,_,_],D). 
D = woot. 

Sugerencias de bienvenida.

Respuesta

1

Ok, cambiando a un tipo de datos finita ayudado!

 

% Basic comparisons 
same(X,Y) :- X == Y. 
greaterThan(X,Y) :- lessThan(Y,X). 

lessThan(X,Y) :- is_lessThan(X,Y). 
lessThan(X,Y) :- is_lessThan(X,Z) , lessThan(Z,Y). 

% Enumerate a list 
is_lessThan('a', 'b'). 
is_lessThan('b', 'c'). 
is_lessThan('c', 'd'). 
is_lessThan('d', 'e'). 
is_lessThan('e', 'f'). 
is_lessThan('f', 'g'). 
is_lessThan('g', 'h'). 
is_lessThan('h', 'i'). 

% "Static" facts of variable length 
x(['a','b','c','d'],'abcd'). 
x(['a','b','d'],'abd'). 
x(['a','b'],'ab'). 
x(['a','c','d'],'acd'). 
x(['e','f','g','h'],'efgh'). 
x(['f','g','h','i'],'fghi'). 

% "Dynamic" facts of variable length and constraint 
x(['f',X,'h','i'],'fXhi') :- greaterThan('g',X). 
x(['f',X,Y],'fXY') :- greaterThan('g',X), lessThan(Y,'i'). 

% specify the two list items separately in X & Y 
fuzzyMatch(X,Y,R) :- x([X,Y],R) ; x([Y,X],R) . 

% specify the list X 
fuzzyMatch(X,R) :- x(Z, R), subset(X,Z) . 

% specify two list items separately, returning the remaining terms that didn't match 
fuzzyMatch(X,Y,D,M) :- x(Z,D) , select(X,Z,N), select(Y,N,M). 

Output: 

?- fuzzyMatch('b','a',D). 
D = ab ; 
false. 

?- fuzzyMatch(['b','a'],D). 
D = abcd ; 
D = abd ; 
D = ab ; 
D = fXY ; 
D = fXY ; 
false. 

?- fuzzyMatch('b','a',R,D). 
R = abcd, 
D = [c, d] ; 
R = abd, 
D = [d] ; 
R = ab, 
D = [] ; 
R = fXY, 
D = [f] ; 
R = fXY, 
D = [f] ; 
false. 
2

En fuzzy/2 y remaining/4, que están llamando x/2 con un instanciado Z. Esto significa que se ha desinstalado el lado izquierdo de + (y por lo tanto >).

+0

Lamentablemente, creo que esto puede invalidar este método de búsqueda de mis datos. ¿Sugeriría alguna solución alternativa, o tendré que volver a evaluar mi modelo? – Demosthenex

3

¿Puede X ser solo un número natural? Si es así, entonces se puede cambiar la regla

x([6,X,8,9], 'woot') :- (X+0) > 7. 

a

x([6, X, 8, 9], 'woot') :- between(8, inf, X). 

Esto funciona al menos en SWI-Prolog:

?- x(A, B). 
A = [6, 8, 8, 9], 
B = woot ; 
A = [6, 9, 8, 9], 
B = woot ; 
A = [6, 10, 8, 9], 
B = woot ; 
... 
+0

Eventualmente será una prueba en contra de una serie de hechos que he enumerado (es decir: menos (a, b), menos (b, c) -> menos (a, c)). Estaba usando números como marcadores de posición mientras probé el concepto. Lo intentaré con otro tipo de datos porque mi enumeración debería limitar la lista de opciones. – Demosthenex

Cuestiones relacionadas