Picat - First steps


 Getting started with a new software system can always be a little daunting. Just a little bit of help to get over the curb can make that journey a lot easier. Picat is no exception, a quick review of the available resources, and then a review of these points hints will get you started in your journey of discovery. Picat is available on PC, Mac, Cygwin and Linux. Starting with the assumption that you know how to drive your environment and can create, open and edit files from the command prompt.

If you like to just explore a new environment, just follow along with these simple steps:

Resources page -> Lots of links and resources here

Installing Picat - > Go here and download a suitable version. Note the location of the main program. Usually called picat.exe or just picat on unix systems.

Creating a Picat program -> Use a text Editor to create a files called fib.pi and include just this text ...

main =>
S = 0,

    I = 1,
    F = fib(I),
    while (F <= 4000000)
        if (F mod 2 == 0) then
            S := S+F
        end,
        I := I+1,
        F := fib(I)
    end,
    printf("Sum of the even-valued terms is %w%n",S).
main([A1]) =>
    printf("fib(%s)=%w%n",A1,A1.to_integer().fib()).
table
fib(1) = 1.
fib(2) = 2.
fib(N) = fib(N-1)+fib(N-2).

Running Picat ->

From the command prompt/terminal either run inside the environment

% picat 

Picat 3.6, (C) picat-lang.org, 2013-2023.

Type 'help' for help.

Picat> cl(fib)

Compiling:: fib.pi

fib.pi compiled in 4 milliseconds

loading...


yes


Picat> main

Sum of the even-valued terms is 4613732


yes


Picat> X = fib(2500)

X = 2131097222364817258963242995170479743181820536370126875362868255835307759626590863125487300020126214968986354893376810558993752038316785973052343747096937468058048464282949473925420848907002689994007955245760613229271988138914634517152250189728338958657820391875946096623727383020868089975480618277687427319326526655563351096370488572490223863066558332769194471114405709169250300091519443513355074402828988912117721507439260724419664834993923257783450437301079831914800243767657600808784351534033062411236406608421704602501

yes


Picat> exit

OR as a stand alone command starting at the main entry point

% picat fib.pi

Sum of the even-valued terms is 4613732

OR as a stand alone command, with parameter starting at the main([A1]) entry point 

% picat fib.pi 2500

fib(2500)=2131097222364817258963242995170479743181820536370126875362868255835307759626590863125487300020126214968986354893376810558993752038316785973052343747096937468058048464282949473925420848907002689994007955245760613229271988138914634517152250189728338958657820391875946096623727383020868089975480618277687427319326526655563351096370488572490223863066558332769194471114405709169250300091519443513355074402828988912117721507439260724419664834993923257783450437301079831914800243767657600808784351534033062411236406608421704602501

Understanding Picat notes ->

  •     Upper case / lower case names for variables/functions are significant. Use Uppercase for variables and lower case for function names apparently.
  •     The structure of a program is , (comma) separated phrases, ending in .(dot) for end of sentence.
  •     There can be multiple entry points in to a program as show above.
  •     Running interactively. Picat does not seem to like cut&paste to add a program into a session unless preceded by go =>. Seems unfortunate that the interpreter format is slightly different from the in-file format. (still looking into this)
  •     To get the program generate all the possible answers (rather than just the first found answer) add in fail close to the end of the function. 
  • The definition of more variables than are explicitly used in the program can cause the solver to run long and generate values for the unset variables.
  • Picat has a rich syntax that uses some symbols in novel ways. Check out this post of comparisons with other languages.
  • The meanings of words such as "mode", "predicate", "function" and pattern matching require some study and should not be assumed as understood from study of other programming languages.
  • Looks like Picat has very long number arithmetic "bigint" built in. (see  arbitrary precision numbers

Notes and code extracted from this getting started guide.


Picat Stands for 

Picat is a general-purpose language that incorporates features from logic programming, functional programming, and scripting languages. The letters in the name summarize Picat’s features:

  • Pattern-matching:predicate defines a relation, and can have zero, one, or multiple answers. A function is a special kind of a predicate that always succeeds with one answer. Picat is a rule-based language. Predicates and functions are defined with pattern-matching rules.

  • Intuitive: Picat provides assignment and loop statements for programming everyday things. An assignable variable mimics multiple logic variables, each of which holds a value at a different stage of computation. Assignments are useful for computing aggregates and are used with the foreach loop for implementing list and array comprehensions.

  • Constraints: Picat supports constraint programming. Given a set of variables, each of which has a domain of possible values, and a set of constraints that limit the acceptable set of assignments of values to variables, the goal is to find an assignment of values to the variables that satisfies all of the constraints. Picat provides three solver modules: cpsat, and mip. These three modules follow the same interface, which allows for seamless switching from one solver to another.

  • Actors: Actors are event-driven calls. Picat provides action rules for describing event- driven behaviors of actors. Events are posted through channels. An actor can be attached to a channel in order to watch and to process its events.

  • Tabling: Tabling can be used to store the results of certain calculations in memory, allowing the program to do a quick table lookup instead of repeatedly calculating a value. As computer memory grows, tabling is becoming increasingly important for offering dynamic programming solutions for many problems. The planner module, which is implemented by the use of tabling, has been shown to be a more efficient tool than ASP and PDDL for solving many planning problems.




Picat - A new avenue for exploration

The field of constraint programming CP and Satisfaction Modular theory SMT appear to have a high barriers to entry. Many of the concepts that lie behind the solvers, rely on mathematical explanations and verified computational theories. However when it gets down to it, understanding a problem thoroughly, and being able to express it in precise logical terms is all that is needed for making progress in this area.

What has been very rewarding is seeing a computer generate a solution, after giving it just the description of the problem, rather than providing a method to solve the problem. This is very different from the current mode of artificial intelligence included in ChatGPT et cetera which provides solutions by looking for answers near places that textually look like the question being asked. 

The general method used up to this point, has been encoding a problem into a textual / numerical description, then transforming that description into the language required by either Z3, or MathSat5. This has been a successful approach, providing answers to all shapes and sizes of Soduku, Gogen, Hidato, and other similar numerical puzzles. Whilst this has proved a satisfactory avenue of exploration, it did feel like spoon feeding the solver with a rather low level and basic language. (SMT-2) being their language of choice.

I felt there must be a better way, and I think I found it. Picat is a problem text processor that encompasses in its logical language how to describe various shapes and sizes of problems and includes different solvers. I am just taking the very first steps in understanding how to use Picat, but I am encouraged by its compact syntax and flexibility. 

Peoples way of learning a new language or skill vary. Some like to tinker explore tryout, poke with a stick and others like to read the technical descriptions and before getting their hands dirty. Personally I like to tinker around with new things but have a goal in mind. Picat was very amenable to trying out things once a couple of fundamentals were understood. I'll document my first efforts in the next post.

A shortlist of resources is provided.

The introduction paper

My First Look At Picat as a Modeling Language for Constraint Solving and

Planning by HÃ¥kan Kjellerstrand1

http://picat-lang.org/download/a_first_look_at_picat.pdf

This is a great starting point for existing users of solver type programs.

The Website 

From which the "Getting started" and "User guides" are available along with install downloads for PC, Mac and Linux platforms.



The Book  

"Constraint Solving and Planning with Picat"

Whilst there is a .pdf available having the book makes access easier.


The jumbo boxes of Picat examples

There is a set of 99 converted problems here 
and 
A big set of other example including numerical puzzles here.

The Guy

I had a couple of email dialogs with Haken Kjellerstrand, the Sweden based co-author of the book and major contributor to Picat. He was just so helpful, engaged, and willing to put effort into looking at the questions I had that it gave me a warm, fuzzy feeling about Picat community.

The other Guys

Picat is created by Neng-Fa Zhou and Jonathan Fruhman.


SMT Solvers, introduction and links (Start here with the readme)

Picat - First steps

 Getting started with a new software system can always be a little daunting. Just a little bit of help to get over the curb can make that jo...