picat tutorial
TRANSCRIPT
![Page 1: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/1.jpg)
Picat: A Scalable Logic-Based
Language
Neng-Fa Zhou & Jonathan Fruhman
111/10/2013
![Page 2: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/2.jpg)
Wh A th L ?Why Another Language?
Complaints about Prolog Prolog is old Implicit unification and non-determinism are difficult Cuts and dynamic predicates are non-logical Lack of constructs for programming everyday things Lack of constructs for programming everyday things Lack of standard libraries Lack of types Overemphasizing meta-programming Prolog is slow …
211/10/2013
![Page 3: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/3.jpg)
Wh A th L ?Why Another Language?
Previous efforts Mercury
Too many declarations
Erlang Non-determinism is abandoned in favor of concurrency
Oz Strange syntax and implicit laziness
Curry Curry Too close to Haskell
Many extensions of Prolog Arrays and loops in ECLiPSe and B-Prolog, dynamic indexing in
Yap, support of multi-paradigms in Ciao,…311/10/2013
![Page 4: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/4.jpg)
F t f PICATFeatures of PICAT
Pattern-matching Predicates and functions are defined with pattern-matching rules
Imperative Imperative Assignments, loops, list comprehensions
Constraints CP, SAT and LP/MIP
Actors Action rules, event-driven programming, actor-based concurrency Action rules, event driven programming, actor based concurrency
Tabling Memoization, dynamic programming, planning, model-checking
411/10/2013
![Page 5: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/5.jpg)
Ai d A li ti f Pi tAimed Applications of Picat
Scripting and Modeling Constraint solving and optimization NLP Knowledge engineering Complex data processing Complex data processing Web services …
511/10/2013
![Page 6: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/6.jpg)
D t TData Types Variables plain and attributed Variables – plain and attributed
X1 _ _ab
Primitive values Primitive values Integer and float Atomx1 ‘_’ ‘_ab’ ‘$%’ ‘你好’
Compound values List List[17,3,1,6,40]
Structure$triangle(0.0,13.5,19.2)
6
$ g ( , , )
11/10/2013
![Page 7: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/7.jpg)
Th T Hi hThe Type Hierarchy
711/10/2013
![Page 8: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/8.jpg)
C ti St t d Li tCreating Structures and Lists Generic StructureGeneric Structure
Picat> P = new_struct(point, 3)P = point(_3b0, _3b4, _3b8)Picat> S = $student(marry,cs,3.8)
List Comprehension List ComprehensionPicat> L = [E : E in 1..10, E mod 2 != 0]L = [1,3,5,7,9]
RangePicat> L = 1..2..10L = [1,3,5,7,9]
StringPicat> write(“hello ”++“world”)[h,e,l,l,o,’ ’,w,o,r,l,d][ , , , , , , , , , , ]
ArrayPicat> A = new_array(2,3)A = {{_3d0, _3d4, _3d8}, {_3e0, _3e4, _3e8}}
Map
8
MapPicat> M = new_map([alpha= 1, beta=2])M = (map)[alpha = 1,beta = 2]
11/10/2013
![Page 9: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/9.jpg)
S i l St tSpecial Structures These structures do not need to be preceded by These structures do not need to be preceded by
a $ symbol Patterns
p(X+Y) => p(X), p(Y).
Goals(a, b) (a; b) not a X=Y
Constraints and Constraint ExpressionsX+Y #= 100 X #!= 0 X #/\ Y
Arraysy{2, 3, 5, 7, 11, 13, 17, 19}
911/10/2013
![Page 10: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/10.jpg)
Built insBuilt-insPicat> integer(2)yes
Picat> integer(2.0)no
Picat> real(3.0)yesPicat> not real(3.0)noPicat> var(X)yesyesPicat> X = 5, var(X)no
Picat> trueyesyesPicat> failno
1011/10/2013
![Page 11: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/11.jpg)
B ilt i (C t )Built-ins (Cont.)Picat> X = to_binary_string(5), Y = to_binary_string(13)X = [‘1’, ‘0’, ‘1’]Y = [‘1’, ‘1’, ‘0’, ‘1’]
% X is an attributed variable% X is an attributed variablePicat> put(X, age, 35), put(X, weight, 205), A = get(X, age)A = 35
% X is a mapPicat> X = new_map([age=35, weight=205]), put(X, gender, male)X = map([age=35, weight=205, gender=male])
Picat> S = $point(1.0, 2.0)$, Name = name(S), Arity = length(S)Name = pointName pointArity = 2
Picat> I = read_int(stdin) % Read an integer from standard input123
11
I = 123
11/10/2013
![Page 12: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/12.jpg)
I d N t tiIndex NotationX[I1,…,In] : X references a compound valueX[I1,…,In] : X references a compound value
Picat> L = [a,b,c,d], X = L[2]X = b
Picat> S = $student(marry,cs,3.8), GPA=S[3]GPA = 3.8
Picat> A = {{1, 2, 3}, {4, 5, 6}}, B = A[2, 3]B = 6
1211/10/2013
![Page 13: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/13.jpg)
Li t C h iList Comprehension[T E D C d E D C d ][T : E1 in D1, Condn , . . ., En in Dn, Condn]
Picat> L = [X : X in 1..5].L = [1,2,3,4,5]
Picat> L = [(A,I): A in [a,b], I in 1..2].L = [(a,1),(a,2),(b,1),(b,2)]
Picat> L = [X : I in 1..5] % X is local L [ b 8 b f0 b f8 bf00 bf08]L = [_bee8,_bef0,_bef8,_bf00,_bf08]
Picat> X=X, L = [X : I in 1..5] % X is non-localL = [X X X X X]L = [X,X,X,X,X]
11/10/201313
![Page 14: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/14.jpg)
OOP N t tiOOP Notation Picat> Y = 13 to binary string() O.f(t1,…,tn)Picat> Y = 13.to_binary_string()Y = [‘1’, ‘1’, ‘0’, ‘1’]
Picat> Y = 13.to_binary_string().reverse()Y = [‘1’, ‘0’, ‘1’, ‘1’]
( )-- means module qualified call if O is atom-- means f(O,t1,…,tn) otherwise.
% X becomes an attributed variablePicat> X.put(age, 35), X.put(weight, 205), A = X.ageA = 35
%X is a mapPicat> X = new_map([age=35, weight=205]), X.put(gender, male)X = (map)([age=35, weight=205, gender=male])
Pi t> S $ i t(1 0 2 0)$ N S A it S l thPicat> S = $point(1.0, 2.0)$, Name = S.name, Arity = S.lengthName = pointArity = 2
Picat> I = math.pi % module qualifier
14
I = 3.14159
11/10/2013
![Page 15: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/15.jpg)
E li it U ifi ti t tExplicit Unification t1=t2Picat> X=1X=1Picat> $f(a,b) = $f(a,b)yes
bind
testyesPicat> [H|T] = [a,b,c]H=aT=[b,c]Pi t> $f(X Y) $f( b)
matching
Picat> $f(X,Y) = $f(a,b)X=aY=bPicat> $f(X,b) = $f(a,Y)
matching
full unificationX=aY=bPicat> X = $f(X)X=f(f(
full unification
without occur checkingX=f(f(......
1511/10/2013
![Page 16: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/16.jpg)
PredicatesPredicates Relation with pattern-matching rulesp g
fib(0,F) => F=1.fib(1,F) => F=1.fib(N,F),N>1 => fib(N-1,F1),fib(N-2,F2),F=F1+F2.fib(N,F) => throw $error(wrong argument,fib,N). _
Backtracking (explicit non-determinism)member(X,[Y|_]) ?=> X=Y.
b (X [ |L]) > b (X L)member(X,[_|L]) => member(X,L).
Picat> member(X,[1,2,3])X = 1;X 2X = 2;X = 3;no
Control backtracking
16
Picat> once(member(X,[1,2,3]))
11/10/2013
![Page 17: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/17.jpg)
P di t F tPredicate Facts
index(+,-) (-,+)edge(a,b).edge(a,c).
edge(a,Y) ?=> Y=b.edge(a,Y) => Y=c.edge(b,Y) => Y=c.edge(a,c).
edge(b,c).edge(c,b).
edge(c,Y) => Y=b.edge(X,b) ?=> X=a.edge(X,c) ?=> X=a.edge(X,c) => X=b.edge(X,b) => X=c.
Facts must be ground A call with insufficiently instantiated arguments fails
Picat> edge(X,Y)no
1711/10/2013
![Page 18: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/18.jpg)
F tiFunctions Always succeed with a return valuey Non-backtrackable
fib(0)=F => F=1.ifib(1)=F => F=1.
fib(N)=F,N>1 => F=fib(N-1)+fib(N-2).
Function facts Function facts
fib(0)=1.fib(1)=1.fib(N)=F N>1 => F=fib(N-1)+fib(N-2)fib(N)=F,N>1 => F=fib(N-1)+fib(N-2).
Return expressions
18
fib(N)=fib(N-1)+fib(N-2),N>1 => true.
11/10/2013
![Page 19: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/19.jpg)
M F tiMore on Functions
Ranges are always functionswrite($f(L..U)) is the same as Lst=L..U, write($f(Lst))
Index notations are always functionsX[1]+X[2] #= 100 is the same as X1=X[1], X2=X[2], X1+X2 #= 100write($f(X[I])) is the same as Xi=X[I] write($f(Xi))write($f(X[I])) is the same as Xi=X[I], write($f(Xi))
List comprehensions are always functionssum([A[I,J] : I in 1..N, J in 1..N]) #= N*N
is the same asL = [A[I,J] : I in 1..N, J in 1..N], sum(L) #= N*N
1911/10/2013
![Page 20: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/20.jpg)
P tt i H dPatterns in Heads
Index notations, ranges, dot notations, and list comprehensions cannot occur in head patterns
As-patterns
merge([],Ys) = Ys.merge(Xs,[]) = Xs.merge([X|Xs] Ys@[Y| ])=[X|Zs] X<Y => Zs=merge(Xs Ys)merge([X|Xs],Ys@[Y|_]) [X|Zs],X<Y > Zs merge(Xs,Ys).merge(Xs,[Y|Ys])=[Y|Zs] => Zs=merge(Xs,Ys).
2011/10/2013
![Page 21: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/21.jpg)
Conditional StatementsConditional Statements If-then-else
fib(N)=F => if (N=0; N=1) then
F=1F 1 elseif N>1 then
F=fib(N-1)+fib(N-2)else
throw $error(wrong argument,fib,N)$throw $error(wrong_argument,fib,N)$end.
Prolog-style if-then-else
(C -> A; B)
Conditional Expressions
21
fib(N) = cond((N==0;N==1), 1, fib(N-1)+fib(N-2))
11/10/2013
![Page 22: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/22.jpg)
AssignmentsAssignments X[I1,…,In] := Exp
Destructively update the component to Exp. Undo the update upon backtracking.
Var := Exp The compiler changes it to Var’ = Exp and replace all subsequent occurrences of Var inreplace all subsequent occurrences of Var in the body of the rule by Var’.
test => X = 0, X := X + 1, X := X + 2, write(X).
test => X = 0 X1 = X + 1 X2 = X1 + 2 write(X2)
22
test > X 0, X1 X + 1, X2 X1 + 2, write(X2).
11/10/2013
![Page 23: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/23.jpg)
LLoops Types Types
foreach(E1 in D1, …, En in Dn) Goal end while (Cond) Goal end do Goal while (Cond)
Loops provide another way to write recurrences A loop forms a name scope: variables that do
not occur before in the outer scope are local. Loops are compiled into tail-recursive predicates
2311/10/2013
![Page 24: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/24.jpg)
S f V i blScopes of Variables
Variables that occur within a loop but not before in its outer scope are local to each iteration
p(A) => p(A):-p( )foreach(I in 1 .. A.length)
A[I] = $node(X)end.
p( )noop(X),foreach(I in 1 .. A.length)
A[I] = $node(X)d
q(L) =>L = [X : I in 1 .. 5].
end.
q(L) =>noop(X),
24
L = [X : I in 1 .. 5].
11/10/2013
![Page 25: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/25.jpg)
L ( 1)Loops (ex-1)sum list(L)=Sum =>sum_list(L) Sum
S=0,foreach (X in L)
S:=S+Xend,S S
RecurrencesSum=S.
S=0
Q
S 0S1=L[1]+SS2=L[2]+S1…
Sn=L[n]+Sn-1
Sum = Sn Query Sum Sn
Picat> S=sum_list([1,2,3])S=6
2511/10/2013
![Page 26: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/26.jpg)
L ( 2)Loops (ex-2)read_list=List =>
L=[],E=read_int(),while (E != 0)
L := [E|L],E := read int()
Recurrences
E : read_int()end,List=L.
L=[]
Q
L1=[e1|L]L2=[e2|L1]…Ln=[en|Ln-1]List=L
Query List=Ln
Picat> L=read_list()1 2 3
26
L=[3,2,1]
11/10/2013
![Page 27: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/27.jpg)
L ( 3)Loops (ex-3)read list=List =>_
List=L,E=read_int(),while (E != 0)
L = [E|T],
R
L := T,E := read_int()
end,L=[].
RecurrencesL=[e1|L1]L1=[e2|L2]…
QueryLn-1=[en|Ln]Ln=[]
Picat> L=read_list()1 2 3
27
1 2 3L=[1,2,3]
11/10/2013
![Page 28: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/28.jpg)
Li t C h i t LList Comprehensions to LoopsLi t [(A X) A i [ b] X i 1 2]List = [(A,X) : A in [a,b], X in 1..2]
List = L,foreach(A in [a,b], X in 1..2)
L = [(A,X)|T],L [(A,X)|T],L := T
end,L = [].
2811/10/2013
![Page 29: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/29.jpg)
T bliTablingPredicates define relations here a set of facts Predicates define relations where a set of facts is implicitly generated by the rules
The process of fact generation might never end The process of fact generation might never end, and can contain a lot of redundancy
Tabling memorizes calls and their answers in Tabling memorizes calls and their answers in order to prevent infinite loops and to limit redundancyy
2911/10/2013
![Page 30: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/30.jpg)
T bli ( l )Tabling (example)t bltablefib(0)=1.fib(1) 1fib(1)=1.fib(N)=fib(N-1)+fib(N-2).
Without tabling, fib(N) takes exponential time in N
With tabling, fib(N) takes linear time
3011/10/2013
![Page 31: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/31.jpg)
S l ti T bliSelective TablingA table mode declaration instr cts the A table mode declaration instructs the system on what answers to table table(M1 M2 Mn) where Mi is: table(M1,M2,…,Mn) where Mi is:
+: input -: output min: output, corresponding variable should be
minimized max: output, corresponding variable should be
maximized nt: not-tabled (only the last argument can be nt)
Selective tabling is useful for dynamic
31
Selective tabling is useful for dynamic programming problems
11/10/2013
![Page 32: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/32.jpg)
Dynamic Programming (examples)Dynamic Programming (examples) Shortest Path
table(+,+,-,min)shortest_path(X,Y,Path,W) =>
Path = [(X,Y)],edge(X,Y,W),
shortest_path(X,Y,Path,W) =>
Knapsack Problem
Path = [(X,Z)|PathR],edge(X,Z,W1),shortest_path(Z,Y,PathR,W2),W = W1+W2.
Knapsack Problemtable(+, +,-,max)knapsack(_,0,Bag,V) =>
Bag = [], V = 0V = 0.
knapsack([_|L],K,Bag,V), K>0 ?=>knapsack(L,K,Bag,V).
knapsack([F|L],K,Bag,V), K>=F =>Bag = [F|Bag1],knapsack(L K F Bag1 V1)
32
knapsack(L,K-F,Bag1,V1),V = V1+1.
11/10/2013
![Page 33: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/33.jpg)
ModulesModulesmodule M.import M1,M2,…,Mn.
The declared module name and the file name must be the same
import M1,M2,…,Mn.
Files that do not begin with a module declaration are in the global module
Atoms and structure names are globalg Picat has a global symbol table for atoms, a global
symbol table for structure names, and a global symbol table for modulestable for modules
Each module has its own symbol table for the public predicates and functions
3311/10/2013
![Page 34: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/34.jpg)
M d l (C t )Modules (Cont.)
Binding of normal calls to their definitions occurs at compile time The compiler searches modules in the order that they were The compiler searches modules in the order that they were
imported Binding of higher-order calls to their definitions
occurs at runtimeoccurs at runtime. The runtime system searches modules in the order that they
were loaded The environment variable PICATPATH tells where The environment variable PICATPATH tells where
the compiler or runtime system searches for modules
3411/10/2013
![Page 35: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/35.jpg)
M d l (C t )Modules (Cont.)
No module variables are allowed
Recall that M.f(…) stands for f(M,…) if M is a variable
No module-qualified higher-order calls
3511/10/2013
![Page 36: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/36.jpg)
Modules (example)Modules (example)% In file qsort.pimodule qsort.sort([]) = [].sort([H|T]) = sort([E : E in T, E <= H) ++ [H] ++ sort([E : E in T, E > H).
% In file isort.pimodule isort.sort([]) = [].sort([]) [].sort([H|T]) = insert(H, sort(T)).
privateinsert(X,[]) = [X].i t(X Y @[Y| ]) Z X <Y > Z [X|Y ]insert(X,Ys@[Y|_]) = Zs, X=<Y => Zs=[X|Ys].insert(X,[Y|Ys]) = [Y|insert(X,Ys)].
% another file test_sort.piimport qsort,isort.p q
sort1(L)=S =>S=sort(L).
sort2(L)=S =>S=qsort sort(L)
36
S=qsort.sort(L).sort3(L)=S =>
S=isort.sort(L).
11/10/2013
![Page 37: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/37.jpg)
Th l M d lThe planner Module
Useful for solving planning problems plan(State,Limit,Plan,PlanCost) b t l (State Limit Plan PlanCost) best_plan(State,Limit,Plan,PlanCost) …
Users only need to define final/1 and action/4 final(State) is true if State is a final state action(State,NextState,Action,ActionCost) encodes the state
transition diagramtransition diagram
Uses the early termination and resource-bounded search techniques to speedup search
3711/10/2013
![Page 38: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/38.jpg)
E Th F ’ P blEx: The Farmer’s Problemimport planner.import planner.
go =>S0=[s,s,s,s],best_plan(S0,Plan),it l (Pl )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)not unsafe(S1).
…
3811/10/2013
![Page 39: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/39.jpg)
C t i tConstraints Picat can be used for constraint satisfaction and
optimization problems Constraint Problems
Generate variables Generate constraints over the variables
S l th bl fi di i t f l t th Solve the problem, finding an assignment of values to the variables that matches all the constraints
Picat can be used as a modeling language forPicat can be used as a modeling language for CP, SAT, LP/MIP Loops are helpful for modeling
3911/10/2013
![Page 40: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/40.jpg)
C t i t ( l )Constraints (example) SEND + MORE = MONEY
import cp.
go =>V [S E N D M O R Y] % t i blVars=[S,E,N,D,M,O,R,Y], % generate variablesVars :: 0..9, % define the domainsall_different(Vars), % generate constraintsS #!= 0S #!= 0,M #!= 0,1000*S+100*E+10*N+D+1000*M+100*O+10*R+E
#= 10000*M+1000*O+100*N+10*E+Y,,solve(Vars), % searchwriteln(Vars).
4011/10/2013
![Page 41: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/41.jpg)
N-Queens ProblemN-Queens Problemimport cp.
queens(N) =>Qs=new_array(N),Qs :: 1..N,foreach (I in 1..N-1, J in I+1..N)
Qs[I] #!= Qs[J],b (Q [I] Q [J]) #! J Iabs(Qs[I]-Qs[J]) #!= J-I
end,solve(Qs),writeln(Qs)writeln(Qs).
4111/10/2013
![Page 42: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/42.jpg)
A ti R lAction Rules Syntax
Head, Head, Condition, {Condition, {EventSetEventSet} => Action} => Action Syntax
Agentp(X1,…,Xn)
Condition Inline tests (e.g., var(X),nonvar(X),X==Y,X>Y)
E tS t EventSet event(X,O) -- a general form event ins(X) -- X is instantiateddom(X E) An inner element E of X’s domain is excluded dom(X,E) – An inner element E of X’s domain is excluded
dom_any(X,E) -- An arbitrary element E is excluded Action
Same as a rule body
42
Same as a rule body
11/10/2013
![Page 43: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/43.jpg)
A li ti f ARApplications of AR
Co-routining and concurrency freeze(X,Call) is compiled to AR
Constraint propagation Constraints in the cp module are compiled to AR U bl ifi t f Users can program problem-specific propagators for
global constraints Compiling CHRCo p g C Interactive graphical user interfaces
4311/10/2013
![Page 44: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/44.jpg)
I l ti f (X G l)Implementing freeze(X,Goal)
freeze(X,q(X,Y)))
freeze_q(X,Y),var(X),{ins(X)} => true.freeze_q(X,Y) => q(X,Y).
4411/10/2013
![Page 45: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/45.jpg)
E t H dliEvent-Handlingecho(X),{event(X,O)}=> writeln(O).
Picat> echo(X), X.post_event(hello).hello
Pi t h (X) t X t t(h ll ) l f ilPicat> echo(X), repeat, X.post_event(hello), nl, fail.hello
hello
hello…
11/10/201345
![Page 46: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/46.jpg)
Programming ConstraintProgramming Constraint Propagators Maintaining arc consistency for aX=bY+c
'aX in bY+c_arc'(A,X,B,Y,C),var(X),var(Y),{dom(Y,Ey)}=> T = B*Ey+C, E T//AEx = T//A, (A*Ex==T -> fd_set_false(X,Ex);true).
'aX in bY+c_arc'(A,X,B,Y,C) => true.
Whenever an element Ey is excluded from Y’s domain,exclude Ey’s counterpart, Ex, from X’s domain.
11/10/201346
![Page 47: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/47.jpg)
Higher-Order CallsHigher-Order Calls Functions and predicates that take calls as arguments call(S,A1,…,An)
Calls the named predicate with the specified arguments apply(S,A1,…,An)
Similar to call, except apply returns a value fi d ll(T l t C ll) findall(Template, Call)
Returns a list of all possible solutions of Call in the form Template. findall forms a name scope like a loop.
Picat> C = $member(X), call(C, [1,2,3])X = 1;X = 2;X = 3;X 3;no
Picat> L = findall(X, member(X, [1, 2, 3]))L = [1,2,3]
4711/10/2013
![Page 48: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/48.jpg)
Hi h O d F tiHigher-Order Functionsmap(_F,[]) = [].map(F,[X|Xs])=[apply(F,X)|map(F,Xs)].
map2(_F,[],[]) = [].map2(F,[X|Xs],[Y|Ys])=[apply(F,X,Y)|map2(F,Xs,Ys)].
f ld( F A []) Afold(_F,Acc,[]) = Acc.fold(F,Acc,[H|T])=fold(F, apply(F,H,Acc),T).
4811/10/2013
![Page 49: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/49.jpg)
Using Higher-Order Calls isUsing Higher Order Calls is Discouraged
List comprehensions are significantly faster than higher-order calls
X map(-,L) O [ X : X in L] O [-X : X in L]
X map2(+,L1,L2) O [X+Y : {X,Y} in zip(L1,L2)]
4911/10/2013
![Page 50: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/50.jpg)
Gl b l H M d Gl b l MGlobal Heap Maps and Global Maps Each thread has a global heap mapg p p
Created on the heap after the thread is created get_heap_map() returns the current thread’s map Changes are undone when backtracking Changes are undone when backtracking
All threads share a global map Created in the global area when Picat is startedg get_global_map() returns the global map Changes are not undone when backtracking
P d f l b l Pros and cons of global maps Allows data to be accessed everywhere without being
passed as arguments
50
Affects locality of data, and readability of programs
11/10/2013
![Page 51: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/51.jpg)
Global Heap Maps and Global Maps (example)go ?=>
get heap map() put(one 1)get_heap_map().put(one,1),get_global_map().put(one,1),fail.
go =>if (get heap map() has key(one)) thenif (get_heap_map().has_key(one)) then
writef("heap map has key%n") else
writef("heap map has no key%n")endend,if (get_global_map().has_key(one)) then
writef("global map has key%n") else
writef("global map has no key%n")writef( global map has no key%n )end.
5111/10/2013
![Page 52: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/52.jpg)
Pi t V P lPicat Vs. Prolog
Picat is arguably more expressive
qsort([])=[].qsort([H|T])=qsort([E : E in T, E=<H])++[H]++qsort([E : E in T, E>H]).
power_set([]) = [[]].2power_set([H|T]) = P1++P2 =>
P1 = power_set(T),P2 = [[H|S] : S in P1].
matrix multi(A,B) = C =>_ ( , )C = new_array(A.length,B[1].length),foreach(I in 1..A.length, J in 1..B[1].length)
C[I,J] = sum([A[I,K]*B[K,J] : K in 1..A[1].length])end.
5211/10/2013
![Page 53: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/53.jpg)
Pi t V P lPicat Vs. Prolog
Picat is more scalable because pattern-matching facilitates indexing rules
L ::= (“abcd”| “abc” | “ab” | “a”)*
p([a,b,c,d|T]) => p(T).p([a,b,c|T]) => p(T).p([a,b|T]) => p(T).p([a|T]) => p(T).p([ | ]) p( )p([]) => true.
5311/10/2013
![Page 54: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/54.jpg)
Pi t V P lPicat Vs. Prolog
Picat is arguably more reliable than Prolog Explicit unification and nondeterminism Functions don’t fail (at least built-in functions) No cuts or dynamic predicates No operator overloading No operator overloading A simple static module system
5411/10/2013
![Page 55: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/55.jpg)
SSummary
Picat is a hybrid of LP, FP and scripting Picat or Copycat?py
Prolog (in particular B-Prolog), Haskell, Scala, Mercury, Erlang, Python, Ruby, C-family (C++, Java, C#), OCaml,…
Th fi t i i il bl t i t l The first version is available at picat-lang.org Reuses a lot of B-Prolog codes
Supported modules Supported modules basic, io, sys, math, os, cp, sat, and util
More modules will be added More modules will be added
5511/10/2013
![Page 56: Picat Tutorial](https://reader031.vdocuments.net/reader031/viewer/2022020219/55cf9ac8550346d033a35c13/html5/thumbnails/56.jpg)
RResources
Users’ Guide http://picat-lang.org/download/picat_guide.pdf
H k Kj ll t d’ Pi t P Hakan Kjellerstrand’s Picat Page http://www.hakank.org/picat/
ExamplesExamples http://picat-lang.org/download/exs.pi
Modules http://picat-lang.org/download/basic.pi math.pi, io.pi, os.pi, sys.pi, cp.pi, sat.pi, util.pi
5611/10/2013