Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

prolog - λProlog hypothetical reasoning Tic Tac Toe

λProlog features hypothetical reasoning. By using the operator (=>)/2 we can temporarily assert some clause. Can this be used to realize adversarial search in λProlog?

Was thinking about Tic-Tac-Toe game. Instead of starting with cell/3 facts that are populated to blank, we could also simply start with no cell/3 facts at all.

And then each player makes a move by adding cell/3 facts. Any corresponding implementations around? I guess they would be adaptations of this code here.

After two moves the database could look like this:

cell(2, 2, x).
cell(1, 1, o).
question from:https://stackoverflow.com/questions/65865359/%ce%bbprolog-hypothetical-reasoning-tic-tac-toe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It turns out, that for the game search, we only need a special case of hypothetical reasoning which can be implemented in pure Prolog. Lets use the operator (-:)/2 for hypothetical reasoning, then we have this inference rule:

   Γ, F |- G
 -------------
  Γ |- F -: G

So verbally to solve the subgoal F -: G, the fact F is added the the database Γ and then the subgoal G is tried. What is challenging that for every success of G the fact F must disappear again from the database, because what is proved is without F in the database.

The special case we consider is that G is deterministic. That is G either succeeds once or fails. We currently do not assume exceptions or F non-ground as well. Under these assumption hypothetical reasoning can be implemented as follows:

:- op(1200, xfx, -:).

(F -: G) :-
   assertz(F),
   G, !,
   retract(F).
(F -: _) :-
   retract(F), fail.

Its now possible to rewrite the move/3 predicate from here into a predicate move/2 that offers a new fact F. The main adversial search routine then reads as follows, where we have also changed the win/2 and tie/2 predicate accordingly into win/1 and tie/1 to check the dynamic facts cell/3:

best(P, F) :-
   move(P, F),
   (F -: 
     (  win(P) -> true
     ;  other(P, Q),
        + tie(Q),
        + best(Q, _))).

Here are some results. The first mover has no winning strategy:

?- best(x, F).
false.

If the board is [[x, -, o], [x, -, -], [o, -, -]] the player x has winning strategy:

?- (cell(1,1,x) -: (cell(3,1,o) -: (cell(1,2,x) -: (cell(1,3,o) -: best(x, F))))).
F = cell(2, 2, x).

How performant is assertz/retract compared to a Prolog term game state? ca. 3 times slower:

?- time(best(x, F)).
% 478,598 inferences, 0.094 CPU in 0.094 seconds (100% CPU, 5105045 Lips)
false.

Open source:

Special Case of Hypothetical Reasoning
https://gist.github.com/jburse/279b6280ab4311de456e458a7386c1da#file-hypo-pl

Prolog code for the tic-tac-toe game
Min-max search via negation
Game board via Prolog facts
https://gist.github.com/jburse/279b6280ab4311de456e458a7386c1da#file-tictac2-pl


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...