{"id":245,"date":"2016-05-26T06:10:12","date_gmt":"2016-05-26T06:10:12","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=245"},"modified":"2016-05-26T07:51:22","modified_gmt":"2016-05-26T07:51:22","slug":"constraint-propagation-in-picat","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/26\/constraint-propagation-in-picat\/","title":{"rendered":"Constraint Programming in Picat"},"content":{"rendered":"<p>In my last <a href=\"http:\/\/www.rangakrish.com\/index.php\/2016\/05\/17\/constraint-programming-using-screamer\/\">post<\/a>\u00a0I 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 <a href=\"http:\/\/picat-lang.org\" target=\"_blank\">Picat<\/a>, a Prolog-derived language. Although I am familiar with Prolog and have been a user of <a href=\"https:\/\/sicstus.sics.se\" target=\"_blank\">Sicstus Prolog<\/a> for many years, I have not looked at Picat until now. I downloaded a few tutorials on Picat (there is also a Youtube <a href=\"https:\/\/www.youtube.com\/watch?v=XLLxwnKtx78\" target=\"_blank\">presentation<\/a> 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: <a href=\"http:\/\/www.amazon.com\/Constraint-Solving-Planning-SpringerBriefs-Intelligent\/dp\/3319258818\/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1464240952&amp;sr=1-1&amp;keywords=picat\" target=\"_blank\">Constraint Solving and Planning with Picat (Springer, 2015)<\/a><\/p>\n<p>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 <a href=\"https:\/\/opusmodus.com\" target=\"_blank\">Opusmodus<\/a>\u00a0to 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.<\/p>\n<p>Here is the Picat program I wrote:<\/p>\n<figure id=\"attachment_246\" aria-describedby=\"caption-attachment-246\" style=\"width: 523px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Program.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"246\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/26\/constraint-propagation-in-picat\/picat-program\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Program.png\" data-orig-size=\"523,852\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Picat Program\" data-image-description=\"&lt;p&gt;Picat Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Picat Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Program.png\" class=\"size-full wp-image-246\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Program.png?resize=523%2C852\" alt=\"Picat Program\" width=\"523\" height=\"852\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Program.png?w=523&amp;ssl=1 523w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Program.png?resize=184%2C300&amp;ssl=1 184w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/a><figcaption id=\"caption-attachment-246\" class=\"wp-caption-text\">Picat Program<\/figcaption><\/figure>\n<p>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.<\/p>\n<figure id=\"attachment_247\" aria-describedby=\"caption-attachment-247\" style=\"width: 769px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"247\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2016\/05\/26\/constraint-propagation-in-picat\/picat-opusmodus-interaction\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction.png\" data-orig-size=\"809,1077\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Picat &amp;#8211; Opusmodus Interaction\" data-image-description=\"&lt;p&gt;Picat &amp;#8211; Opusmodus Interaction&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Picat &amp;#8211; Opusmodus Interaction&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction-769x1024.png\" class=\"size-large wp-image-247\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction-769x1024.png?resize=769%2C1024\" alt=\"Picat - Opusmodus Interaction\" width=\"769\" height=\"1024\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction.png?resize=769%2C1024&amp;ssl=1 769w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction.png?resize=225%2C300&amp;ssl=1 225w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction.png?resize=768%2C1022&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2016\/05\/Picat-Opusmodus-Interaction.png?w=809&amp;ssl=1 809w\" sizes=\"(max-width: 769px) 100vw, 769px\" \/><\/a><figcaption id=\"caption-attachment-247\" class=\"wp-caption-text\">Picat &#8211; Opusmodus Interaction<\/figcaption><\/figure>\n<p>So now we have two ways to do constraint programming in Opusmodus: Using\u00a0<strong>Screamer<\/strong>\u00a0(in Lisp) and <strong>Picat<\/strong>. The more, the merrier!<\/p>\n<p>Download the Picat source code <a href=\"http:\/\/www.rangakrish.com\/downloads\/chord_tones.pi\" target=\"_blank\">here<\/a>. Opusmodus source is <a href=\"http:\/\/www.rangakrish.com\/downloads\/Picat Interaction.opmo\" target=\"_blank\">here<\/a>.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post\u00a0I 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,5,17],"tags":[59,60],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-lisp","category-music","category-programming","tag-constraint-programming","tag-picat"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-3X","jetpack-related-posts":[{"id":2922,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/06\/why-learn-prolog\/","url_meta":{"origin":245,"position":0},"title":"Why Learn Prolog?","author":"admin","date":"October 6, 2022","format":false,"excerpt":"There are several programming languages in use today and a simple google search will throw up interesting recommendations of a subset of these languages to learn, usually based on popularity ranking. As is expected, the popularity of a programming language varies over time and hence a language that was in\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3589,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/01\/04\/word-transformation-puzzle-part-2-implementation-in-common-lisp\/","url_meta":{"origin":245,"position":1},"title":"Word Transformation Puzzle &#8211; Part 2: Implementation in Common Lisp","author":"admin","date":"January 4, 2025","format":false,"excerpt":"In the last article\u00a0I discussed an interesting word puzzle and showed how to solve it using Prolog. Here is the problem statement: \u201cYou are given two words of the same length. You have to transform the first word into the second word, by changing only one letter at a time.\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Getting Word Neighbors","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/01\/code1-300x127.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/01\/code1-300x127.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/01\/code1-300x127.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":245,"position":2},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":541,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/06\/04\/definite-clause-grammars-in-lisp-part-2\/","url_meta":{"origin":245,"position":3},"title":"Definite Clause Grammars in Lisp &#8211; Part 2","author":"admin","date":"June 4, 2017","format":false,"excerpt":"In the last post, I showed how we can implement DCGs in LispWorks using the KnowledgeWorks package. The grammar discussed in that post did not take into account subject\/predicate number agreement. This is one of the basic constraints in English grammar. Today I will show how easy it is to\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Prolog Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Prolog-Grammar.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3573,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/12\/17\/using-prolog-to-solve-the-word-transformation-puzzle\/","url_meta":{"origin":245,"position":4},"title":"Using Prolog to Solve the Word Transformation Puzzle","author":"admin","date":"December 17, 2024","format":false,"excerpt":"In today\u2019s article, I want to share an interesting word puzzle, and then show how to solve it in Prolog. Here is the puzzle: You are given two words of the same length. You have to transform the first word into the second word, by changing only one letter at\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Our Dictionary","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/12\/prolog1-300x280.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":245,"position":5},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}