integer division operator in Prolog

10.7k views Asked by At

This code is a part of the program that I am writing to solve a cryptarithmatic puzzle in prolog. I am solving this using CLPFD (SICStus Prolog). I am getting an error while using the inbuilt integer division operator "//" (See Below for details). I was not able to solve this So I wrote my own integer division function.

What I want to know is why "//" fails. There is no such problem with the other arithmetic operators like +, - , /

BEFORE:

solve1([],[],[H],H).
solve([H1],[H2],[H3,H4],C) :- H1 #\= 0, H2 #\= 0,
                              H3 #= (C+H1+H2)mod 10,
                              H4 #=(C+H1+H2)//10.

solve([H1|T1] , [H2|T2] , [H3|T3],C) :- H3 #= (C+H1+H2)mod 10,
                                        C1 #=(C+H1+H2)//10 ,
                                        solve(T1,T2,T3,C1).

This is the error I get

ERROR: Domain error: clpfd_expression' expected, found(0+_G1592+_G1586)//10' Exception: (12) throw(error(domain_error(clpfd_expression, (0+_G3994{0..9}+_G3991{0..9})//10), _G3976)) ?

AFTER:

integer_div(N,M) :- M #= N/10 , integer(M).
integer_div(N,M) :- N1 #= N mod 10 , N2 #= N-N1 , M #= N2/10.


solve1([],[],[H],H).
solve([H1],[H2],[H3,H4],C) :- H1 #\= 0, H2 #\= 0,
                              H3 #= (C+H1+H2)mod 10,
                              integer_div((C+H1+H2),H4).

solve([H1|T1] , [H2|T2] , [H3|T3],C) :- H3 #= (C+H1+H2)mod 10,
                                        integer_div((C+H1+H2),C1) ,
                                        solve(T1,T2,T3,C1).

This works perfectly All I did was replace "//" by my own version of integer division

1

There are 1 answers

3
starblue On

According to the documentation, CLP-FD uses / for integer division.