Constraint Solving with Picat

I’ve been playing around with a few multi-paradigm programming languages. The most recent one is called Picat. It’s a hybrid language that is a mix of logic, imperative, actor, and functional paradigms with built-in constraint solvers and planners. That’s a lot to take in so whenever I’m faced with a language with roots in the logic paradigm the first thing I try to do is solve “SEND + MORE = MONEY” cryptarithmetic puzzle. It’s a small and manageable problem and there are at least two ways to solve it so it allows one to exercise several features of the logic and constraint paradigms.


I mentioned there are at least two ways to solve the problem. If you search around you will find both approaches. My favored approach is the sum-carry method. I like it because it has a dataflow flavor and is the algorithm that most people learn in grade school so it also has an imperative aspect. The other approach is much more declarative but I think exercises fewer parts of the language. The sum-carry method forces calculation of carries which can sometimes be hard to express as a linear constraint and seeing how that works in the language is a good thing to know in general when working on other problems.

Here’s a high-level view of what the sum-carry method comes down to

So we just need to express the above as a set of constraints in Picat to solve the problem.


Downloading and installing Picat is left as an exercise for the reader so assuming you have Picat installed and working I’m going to present the solution.

First we need to import cp module so Picat knows we are going to use (c)onstraint (p)ropagation/solving

We next need a predicate for expressing sums and carries

There is a lot going on in those few lines. Picat is a logical language so it uses logical variables and pattern-matching/unification. It’s not unification like Prolog so there are some subtle differences. I’m going to show an example of how to use the above predicate in the Picat shell so you can get a feel for what is going on (you can start the Picat shell by just invoking the main Picat binary).

First we need to compile/load the file (I put it in a folder called test and called the file constraint.pi)

After the file is compiled/loaded we can use our definition

So far we have provided enough constraints for Picat to find a solution. So what happens when we don’t provide enough constraints?

Picat does the most sensible thing possible and provides us with the data structures that we can then pass to a solver to get a solution. The problem is unconstrained so there are many solutions

You can keep going to find “all” the solutions but it gets tedious. Notice how the variables are constrained to a specific range and how they’re actually beyond the range that our problem requires. Armed with this new knowledge we can improve our definition

Now our variables are constrained to a range that is sensible for the problem we are trying to solve. Range constraints are expressed with ::. If you compile and run the new version you will get the following output

That’s definitely more sensible for our problem.

I’m going to need one more predicate/function for printing the results and then we can solve the original problem.

Picat has format strings and I/O functions but I haven’t done too much with those yet.


All we need to do is declare our variables and use sum_carry to express the constraints

If you put the above in a file and invoke it you will get the final solution

Unfortunately the constraint/logic programming paradigm is underutilized. You can rectify the situation by learning a little bit of Prolog and Picat and looking for opportunities where they can be used. Some problems are custom made for logic/constraint programming and knowing when you can apply the paradigm is almost like having a superpower.