Re: determinism

michel levy <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
Daniel Diaz wrote:
> michel levy a écrit :
> 
>>Could you help to write this program :
>>det(T,G) succeeds if and only if G succeeds once and only once and give
>>the T answer.
>>1) I don't want the solution below by findall, because I want to try at
>>most two back tracks on G.
>>det(T,G) :- findall(T,G,L),length(L,1)
>>
>>2) I know already call_det but it's not the solution because G can have
>>choice points, but only one answer.
>>
> 
> 
> You can do it using a global variable to count solutions. Stop when the
> second is reached.
> 
> 
> det(Goal) :-
> 	det1(Goal).
> 
> det(_) :-
> 	g_read(det_count, 1).
> 	
> 
> det1(Goal) :-
> 	g_assign(det_count, 0),
> 	call(Goal),
> 	g_inc(det_count, Count), % increment and return the counter
> 	Count = 2,	% cut if 2nd solution is reached (else fail)
> 	!,
> 	fail.
> 
> Hope this helps
> 
> 
Thank you really much for your help.
Your solution is very simple to understand.

I have modify it slightly to have the answer (like in findall).

det(Answer,Goal):-
	det1(Answer,Goal).

det(Answer,_) :-
	g_read(det_count, 1),retract(sol(Answer)).	

det1(Answer,Goal) :-
	g_assign(det_count, 0),
	call(Goal),assertz(sol(Answer)),
	g_inc(det_count, Count), % increment and return the counter
	Count = 2,	% cut if 2nd solution is reached (else fail)
	!,
	retract(sol(_)),
	fail.
I have modify it slightly to have the answer (like in findall).

My goal was to write a program :
sudoko(L) succeds if L is a 81 elements list of a sudoku
and fails if the answer is not unique.

Therefore now I write
L = [  _,_,_,1,3,_,_,_,7,
           5,7,_,6,_,_,_,_,_,
	  9,_,1,_,2,_,3,_,_,
	  _,4,_,_,_,_,2,_,_,
	  1,_,_,_,5,_,_,_,9,
	  2,_,_,_,4,6,_,_,1,
	  _,_,_,9,_,_,_,_,8,
	  _,6,_,_,7,_,_,_,_,
	  8,_,_,4,1,_,5,_,3],
det(L,sudoku(L)).
L = [6,2,4,1,3,5,9,8,7,
5,7,3,6,8,9,4,1,2,
9,8,1,7,2,4,3,5,6,
7,4,6,8,9,1,2,3,5,
1,3,8,2,5,7,6,4,9,
2,5,9,3,4,6,8,7,1,
4,1,5,9,6,3,7,2,8,
3,6,2,5,7,8,1,9,4,
8,9,7,4,1,2,5,6,3]
yes

L = [  _,_,_,_,3,_,_,_,7,
           5,7,_,6,_,_,_,_,_,
	  9,_,1,_,2,_,3,_,_,
	  _,4,_,_,_,_,2,_,_,
	  1,_,_,_,5,_,_,_,9,
	  2,_,_,_,4,6,_,_,1,
	  _,_,_,9,_,_,_,_,8,
	  _,6,_,_,7,_,_,_,_,
	  8,_,_,4,1,_,5,_,_],
det(L,sudoku(L)).
no
This sudoku problem has more than a solution.

My sudoku progam has a side effect, write the sudoku in a visible shape 
and why I get really in the last case is :

det(L,sudoku(L)).
[4,2,6][5,3,9][8,1,7]
[5,7,3][6,8,1][9,2,4]
[9,8,1][7,2,4][3,6,5]
---------------------
[6,4,7][1,9,8][2,5,3]
[1,3,8][2,5,7][6,4,9]
[2,5,9][3,4,6][7,8,1]
---------------------
[7,1,5][9,6,2][4,3,8]
[3,6,4][8,7,5][1,9,2]
[8,9,2][4,1,3][5,7,6]



[4,2,6][5,3,9][8,1,7]
[5,7,3][6,8,1][9,2,4]
[9,8,1][7,2,4][3,6,5]
---------------------
[6,4,8][1,9,7][2,5,3]
[1,3,7][2,5,8][6,4,9]
[2,5,9][3,4,6][7,8,1]
---------------------
[7,1,5][9,6,2][4,3,8]
[3,6,4][8,7,5][1,9,2]
[8,9,2][4,1,3][5,7,6]

(1 ms) no

So you see the first two solutions (there are plenty of solutions).

	Sincerely yours


-- 
Michel Levy
36 rue George Sand
38400 Saint Martin d'Heres
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.