Deep Prolog Search (Vanilla Meta Translator)

I need to change the vanilla meta-interpreter to perform a search with limited depth. I use the following code to test my resolution:

value(wire1,1). connected(wire2, wire1). connected(wire3, wire2). connected(wire4, wire3). connected(wire5, wire4). connected(wire6, wire5). connected(wire7, wire6). connected(wire8, wire7). connected(wire9, wire8). value(W,X):-connected(W,V), value(V,X). 

And the goal is something like:

 solve(value(w9,X), 3). /*depth =3, it should return false*/ solve(value(w9,X), 20). /*depth=20 is enought for returning X=1*/ 

By the way my code

 solve(true,_):-!. solve((A,B),D) :-!, solve(A,D), solve(B,D). solve(A,D) :- clause(A, B),solve(B,D2),D=D2+1,D>0). 

But that does not work. Could you help me? Thank you very much in advance

+6
source share
3 answers

An interesting page on metaprogramming came from a good developer: Marcus Trisk. Here (a couple of meta translators in Prolog) you will find both theory and practice. For instance:

... Another group of extensions is aimed at improving the strategy for calculating partial defaults. We start with MI, which limits the depth of the search tree:

  mi_limit(Goal, Max) :- mi_limit(Goal, Max, _). mi_limit(true, N, N). mi_limit((A,B), N0, N) :- mi_limit(A, N0, N1), mi_limit(B, N1, N). mi_limit(g(G), N0, N) :- N0 > 0, mi_clause(G, Body), N1 is N0 - 1, mi_limit(Body, N1, N). 
+4
source
You were almost there. Only the last sentence needs a little redistribution:
 solve(A, D) :- clause(A, B), D1 is D - 1, D1 > 0, solve(B, D1). ?- solve(value(wire9, X), 9). ===> false. ?- solve(value(wire9, X), 10). ===> X = 1. 
+3
source
 dls(X,X,[X],L):- L >0 goal(X). dls(X,Y,[A|p],L):- L > 0 ,goal(Y) , move(X,Y), L1 is L - 1 , dls(Z,Y ,P,L1). 
-1
source

Source: https://habr.com/ru/post/899922/


All Articles