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.




No comments:

Post a Comment

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...