Constraint Programming in Picat

Written by on May 26, 2016 in LISP, Music, Programming with 0 Comments

In my last post I briefly described how we can use the Screamer Lisp library for constraint programming in music. Another language I have been hearing a lot about, in the context of constraint programming, is Picat, a Prolog-derived language. Although I am familiar with Prolog and have been a user of Sicstus Prolog for many years, I have not looked at Picat until now. I downloaded a few tutorials on Picat (there is also a Youtube presentation by the creator Neng-Fa Zhou), installed the system on my iMac, and tried out some simple programs. The language does have good support for CP. If you are serious, there is also a recent book: Constraint Solving and Planning with Picat (Springer, 2015)

As in the Lisp version of last post, I wrote a Picat function to generate chord and non-chord tones for composing melody over a given chord progression. The next challenge was to figure out how to communicate from Opusmodus to Picat and back. To keep things manageable, I decided to spawn Picat from within Opusmodus, passing the relevant arguments via command line, and then reading the output emitted by Picat back into Opusmodus as a Lisp expression. Definitely an expensive and inefficient interaction protocol, but then we are not doing real-time programming, right? The good news is that this works, as you can see for yourself.

Here is the Picat program I wrote:

Picat Program

Picat Program

I had to write a couple of functions in Opusmodus to marshall the arguments back and forth between OM and Picat. But not very complicated.

Picat - Opusmodus Interaction

Picat – Opusmodus Interaction

So now we have two ways to do constraint programming in Opusmodus: Using Screamer (in Lisp) and Picat. The more, the merrier!

Download the Picat source code here. Opusmodus source is here.

Enjoy!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published.

Top