New to Picat? Read the Getting Started guide.

I enjoy programming in Picat because it suits my mindset very well. — Håkan 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 general-purpose language that combines logic, functional, constraint, and imperative programming. Pattern-matching rules define predicates, functions, and actors. Picat supports explicit unification and nondeterminism, list comprehensions, tabling, assignments, and loops for scripting, symbolic computation, and modeling.

Example 1 — Scripting. Read a triangle of integers from a text file into an array for Project Euler problem 67.

import util.

input_data(Tri) => 
    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
    end.

Example 2 — Functions. Generate permutations with pattern matching, recursion, and a list comprehension.

perms([]) = [[]].
perms(Lst) = [[E|P] : E in Lst, P in perms(Lst.delete(E))].

Example 3 — Tabling. Find the maximum path sum through the triangle. The declaration table (+,+,max,nt) uses the row and column as table keys, keeps the largest sum, and excludes the triangle array from the table.

table (+,+,max,nt) 
path(Row,Col,Sum,Tri), Row == Tri.length => Sum = Tri[Row,Col].
path(Row,Col,Sum,Tri) ?=> 
    path(Row+1,Col,Sum1,Tri),
    Sum = Sum1+Tri[Row,Col].
path(Row,Col,Sum,Tri) => 
    path(Row+1,Col+1,Sum1,Tri),
    Sum = Sum1+Tri[Row,Col].    

Example 4 — Planning. Define the starting state, goal, and possible actions. The planner module uses tabling to find an optimal plan for the farmer’s river-crossing puzzle.

import planner.

go =>
    S0 = [s,s,s,s],
    best_plan(S0,Plan),
    writeln(Plan).

final([n,n,n,n]) => true.

action([F,F,G,C],S1,Action,ActionCost) ?=>
    Action = farmer_wolf,
    ActionCost = 1,        
    opposite(F,F1),
    S1 = [F1,F1,G,C],
    not unsafe(S1).

...

Picat’s cp, sat, mip, and smt modules share an interface for constraint solving and optimization.

Example 5 — Constraint solving. Model N-queens with three all_different constraints.

import cp.

queens(N, Q) =>
    Q = new_list(N),
    Q :: 1..N,
    all_different(Q),
    all_different([$Q[I]-I : I in 1..N]),
    all_different([$Q[I]+I : I in 1..N]),
    solve([ff],Q).

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 picat@picat-lang.org and join the news group https://groups.google.com/g/picat-lang.