I enjoy programming in Picat because it suits my mindset very well. -- Hakan Kjellerstrand
Thank you for your beautiful project! Using Picat, I felt "at home" almost right away. -- Stefan Kral
The Picat language is really cool; it's a very usable mix of logic, functional, constraint, and imperative programming. Scripts can be made quite short but also easily readable. And the built-in tabling is really cool for speeding up recursive programs. I think Picat is like a perfect Swiss army knife that you can do anything with. -- Lorenz Schiffmann
In some cases the use of Picat simplifies the implementation compared to conventional imperative programming languages, while in others it allows to directly convert the problem statement into an efficiently solvable declarative problem specification without inventing an imperative algorithm. -- Sergii Dymchenko
Picat is a simple, and yet powerful, logic-based multi-paradigm programming language aimed for general-purpose applications. Picat is a rule-based language, in which predicates, functions, and actors are defined with pattern-matching rules. Picat incorporates many declarative language features for better productivity of software development, including explicit non-determinism, explicit unification, functions, list comprehensions, constraints, and tabling. Picat also provides imperative language constructs, such as assignments and loops, for programming everyday things. The Picat implementation, which is based on a well-designed virtual machine and incorporates a memory manager that garbage-collects and expands the stacks and data areas when needed, is efficient and scalable. Picat can be used for not only symbolic computations, which is a traditional application domain of declarative languages, but also for scripting and modeling tasks.
Example 1: The following predicate,
input_data(Tri), reads rows of integers from the text file
triangle.txt into an array. This is the first part of a Picat solution for the Euler Project, problem #67.
Lines = read_file_lines("triangle.txt"),
Tri = new_array(Lines.length),
I = 1,
foreach(Line in Lines)
Tri[I] = Line.split().map(to_integer).to_array(),
I := I+1
read_file_lines/1, which is imported by default from the
io module, reads all of the lines from a file as a list of strings. For each
foreach loop splits
Line into tokens (using the function
split/1, which is imported from the
util module), maps the tokens to integers (
map(to_integer)), and converts the list to an array (
to_array). As illustrated in this example, Picat, as a scripting language, is as powerful as Python and Ruby.
Example 2: The following function,
perms(L), returns a list of all of the permutations of
perms() = [].
perms(Lst) = [[E|P] : E in Lst, P in perms(Lst.delete(E))].
Picat provides functional programming features, such as pattern-matching, recursion, and list comprehension, for composing functions.
Example 3: Given a triangle stored in an array, the following tabled predicate finds the maximum total sum from top to bottom. This is the second part of the Picat solution for the Euler Project, problem #67.
path(Row,Col,Sum,Tri), Row == Tri.length => Sum = Tri[Row,Col].
Sum = Sum1+Tri[Row,Col].
Sum = Sum1+Tri[Row,Col].
The first line is a table mode declaration that instructs the system about how to table the calls and answers:
+ means that the argument is tabled,
max means that the argument should be maximized, and
nt means that the argument is not tabled. This predicate searches for a path with the maximum total sum. If the current row is at the bottom of the triangle, then the leaf value is returned. Otherwise, it makes a non-deterministic choice between two branches, one going straight down and the other going down to the adjacent number. This program is not only compact, but also runs fast. For the 100-row triangle that is provided by the Euler project, this program finds the answer in only 0.01 second.
planner module is based on tabling. For a planning problem, users only need to specify the conditions on the final states and the set of actions, and call the planner on an initial state to find a plan or a best plan.
Example 4: The following gives part of a program for the Farmer's problem.
S0 = [s,s,s,s],
final([n,n,n,n]) => true.
Action = farmer_wolf,
ActionCost = 1,
S1 = [F1,F1,G,C],
Tabling, when enhanced with term-sharing, early termination, and resource-bounded search techniques, has been shown to be a better alternative to planning than ASP and SAT-based PDDL planners.
Picat currently provides four modules, named
smt, for solving constraint satisfaction and optimization problems. These modules follow the same interface. This makes it possible to seamlessly switch from one solver to another. The language features, such as arrays, maps, loops and list comprehensions, make Picat a powerful modeling language for constraint problems.
Example 5: The following example models the N-queens problem by using three
queens(N, Q) =>
Q = new_list(N),
Q :: 1..N,
all_different([$Q[I]-I : I in 1..N]),
all_different([$Q[I]+I : I in 1..N]),
As demonstrated by the above examples, Picat offers many advantages over other languages. Compared with functional and scripting languages, the support of explicit unification, explicit non-determinism, tabling, and constraints makes Picat more suitable for symbolic computations. Compared with Prolog, Picat is arguably more expressive and scalable: it is not rare to find problems for which Picat requires an order of magnitude fewer lines of code to describe than Prolog and Picat can be significantly faster than Prolog because pattern-matching facilitates indexing of rules.
Picat can be used for any fair purpose, including commercial applications. The project is open and you are welcome to join, as a developer, a sponsor, a user, or a reviewer. Please contact firstname.lastname@example.org and join the news group email@example.com.