Looping in the Prolog metainterpreter

I am writing a trivial metainterpreter in Prolog for self-education. Basically, I want to bear the "probability" of this decision. For this, I simply declare that the probability that my article will be correct. I expect that if it works, I will expand it with a better framework, but for now, I'm just interested in solving the immediate problem, which is that my cycles are metainterpreter:

code:

fuzzy_prove(true, 1.0) :- !.
fuzzy_prove(probability(P), P) :- !.
fuzzy_prove((A,B), Prob) :-
    fuzzy_prove(A, P1),
    fuzzy_prove(B, P2),
    Prob is P1 * P2.
fuzzy_prove(A, P) :-
    clause(A, B), fuzzy_prove(B, P).

father(dave, jean).
father(dave, don) :- probability(0.5).
father(don, claudia).
father(don, jimmy) :- probability(0.5).
father(jean, davey).

grandfather(Grandpop, Babs) :-
    father(Grandpop, Dad),
    father(Dad, Babs).

My queries first work:

?- fuzzy_prove(grandfather(X, Z), Prob).
X = dave,
Z = davey,
Prob = 1.0 ;
X = dave,
Z = claudia,
Prob = 0.5 ;
X = dave,
Z = jimmy,
Prob = 0.25 ;

When I ask for the next solution, I get a crazy loop. If I interrupt it, it looks like this:

continue (trace mode)
   Call: (3,973,299) fuzzy_prove(call((father(_G3, _G28), father(_G28, _G4))), _G5) ? 
^  Call: (3,973,300) clause(call((father(_G3, _G28), father(_G28, _G4))), _G2044) ? 
^  Exit: (3,973,300) clause(call((father(_G3, _G28), father(_G28, _G4))), call((father(_G3, _G28), father(_G28, _G4)))) ? 
   Call: (3,973,300) fuzzy_prove(call((father(_G3, _G28), father(_G28, _G4))), _G5) ? 
^  Call: (3,973,301) clause(call((father(_G3, _G28), father(_G28, _G4))), _G2051) ? 
^  Exit: (3,973,301) clause(call((father(_G3, _G28), father(_G28, _G4))), call((father(_G3, _G28), father(_G28, _G4)))) ? 
   Call: (3,973,301) fuzzy_prove(call((father(_G3, _G28), father(_G28, _G4))), _G5) ? 
^  Call: (3,973,302) clause(call((father(_G3, _G28), father(_G28, _G4))), _G2058) ? 
^  Exit: (3,973,302) clause(call((father(_G3, _G28), father(_G28, _G4))), call((father(_G3, _G28), father(_G28, _G4)))) ? 
   Call: (3,973,302) fuzzy_prove(call((father(_G3, _G28), father(_G28, _G4))), _G5) ? 
^  Call: (3,973,303) clause(call((father(_G3, _G28), father(_G28, _G4))), _G2065) ? 
^  Exit: (3,973,303) clause(call((father(_G3, _G28), father(_G28, _G4))), call((father(_G3, _G28), father(_G28, _G4)))) ? 
   Call: (3,973,303) fuzzy_prove(call((father(_G3, _G28), father(_G28, _G4))), _G5) ? 
^  Call: (3,973,304) clause(call((father(_G3, _G28), father(_G28, _G4))), _G2072) ? 
^  Exit: (3,973,304) clause(call((father(_G3, _G28), father(_G28, _G4))), call((father(_G3, _G28), father(_G28, _G4)))) ? 

I'm sure I'm doing something obviously stupid, but I have problems with what it is.

+4
source share
2 answers

(,) : 3-, 4- fuzzy_prove, 4- , , .

, :

clause(grandfather(A,B), C0), clause(C0, C1), clause(C1, C2).

C1 = C2, .

, (,) . , , , , .

+3

, , SWI. SWI clause((A,B),R). ISO permission_error(access,private_procedure,Culprit) B, IF, IV, GNU, SICStus, XSB YAP . : private_procedure, , .. clause/2.

.

fuzzy_prove(true,0.0) , , . , .

- fuzzy_prove(G, P), , , G = true.

fuzzy_prove(probability(1.0),0.0) , , . , SWI probability/1.

+2
source

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


All Articles