2024: Domino Containers - The Next Step. News from the Domino Container commu...
Prolog as-meta-lang-pres
1. Prolog as a Meta-Language
Guy Wiener
Sayeret Lambda
July 14, 2010
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 1 / 86
2. Disclaimer
Prolog is:
Well-unknown
Domain-specific
Inefficient
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 2 / 86
3. Outline
1 Introduction to Prolog
2 Functional Prolog
3 Prolog as an Interpreter
4 Prolog for Software Modeling
5 Prolog Libs and Tricks
6 Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 3 / 86
4. Intro
Outline
1 Introduction to Prolog
2 Functional Prolog
3 Prolog as an Interpreter
4 Prolog for Software Modeling
5 Prolog Libs and Tricks
6 Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 4 / 86
5. Intro Terms
Outline
1 Introduction to Prolog
Terms
Unification
Queries
Rules
Cutting
Term Expansion
Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 5 / 86
6. Intro Terms
Grounded Atomic Terms
Numbers 1, 2, 3.14. . .
Atoms foo, bar, ’Baz’
Characters $a, $b, $c
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 6 / 86
7. Intro Terms
Grounded Composite Terms
Functors func(a, b, 5)
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 7 / 86
8. Intro Terms
Grounded Composite Terms
Functors func(a, b, 5)
Lists [a, b, 5], [1, 2 | ...]
Strings "some text"
Operators 3 + 4, foo @> goo
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 7 / 86
9. Intro Terms
Ungrounded Terms
Variables X, Y, Foo, GOO, _, _A
Composites func(X, Y, 5), [1, 2 | Rest]
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 8 / 86
10. Intro Unification
Outline
1 Introduction to Prolog
Terms
Unification
Queries
Rules
Cutting
Term Expansion
Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 9 / 86
11. Intro Unification
Unifying Grounded Terms
1 = 1, foo = foo
1 = 2, foo = goo
func(a, b, 5) = func(a, b, 5)
func(a, b, 5) = func(a, c, 5)
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 10 / 86
12. Intro Unification
Unifying Ungrounded Terms
X = 1, Y = foo(bar(5))
foo(X, goo(4)) = foo(a, Y) ⇒
X = a, Y = goo(4)
[1, 2, 3] = [X | Rest] ⇒
X = 1, Rest = [2, 3]
foo(1, 2) = foo(X, X)
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 11 / 86
13. Intro Unification
The Reflection Operator
func(a, b, c) =.. [func, a, b, c]
func(a, b, c) =.. [F | Args] ⇒
F = func, Args = [a, b, c]
F =.. [foo, bar, X] ⇒ F = foo(bar, X)
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 12 / 86
14. Intro Unification
Arithmetic
X is 2 + 3 ⇒ X = 5
5 is X + 3 ⇒ false
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 13 / 86
15. Intro Queries
Outline
1 Introduction to Prolog
Terms
Unification
Queries
Rules
Cutting
Term Expansion
Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 14 / 86
16. Intro Queries
Programs and Queries
Program
extends(man, person).
extends(woman, person).
consists(couple, man).
consists(couple, woman).
Queries
?- extends(man, X). ?- extends(X, person).
X = person X = man ;
X = woman
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 15 / 86
17. Intro Queries
And-Queries
Program
extends(man, person).
extends(woman, person).
consists(couple, man).
consists(couple, woman).
Queries
?- consists(couple, X) , extends(X, Y).
X = man, Y = person ;
X = woman, Y = person
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 16 / 86
18. Intro Queries
And-Queries
Program
extends(man, person).
extends(woman, person).
consists(couple, man).
consists(couple, woman).
Queries
?- consists(couple, X), consists(couple, Y),
X = Y.
X = man, Y = woman
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 16 / 86
19. Intro Queries
Or-Queries
Program
extends(man, person).
extends(woman, person).
consists(couple, man).
consists(couple, woman).
Queries
?- extends(man, X) ; consists(X, man).
X = person ;
X = couple
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 17 / 86
20. Intro Queries
Queries
Program
extends(man, person).
extends(woman, person).
consists(couple, man).
consists(couple, woman).
Queries
?- (extends(man, X) ; consists(X, man)) ,
extends(woman, X).
X = person
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 18 / 86
21. Intro Queries
Operators are Functors Too
Program
:- op(xfx, 300, |>).
man |> person.
woman |> person.
Queries
?- X |> Y.
X = man, Y = person ;
X = woman, Y = person
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 19 / 86
22. Intro Rules
Outline
1 Introduction to Prolog
Terms
Unification
Queries
Rules
Cutting
Term Expansion
Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 20 / 86
23. Intro Rules
Rules
Program
% ...as before
extends(boy, man).
subtype(X, Y) :- extends(X, Y).
subtype(X, Y) :- extends(X, Z), subtype(Z, Y).
Queries
?- subtype(X, person).
X = man ; X = woman ;
X = boy
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 21 / 86
24. Intro Rules
Rules
Program Query
extends(item, element). ?- composite(X, Y).
extends(bucket, element). X = bucket,
consists(bucket, element). Y = element.
composite(Comp, Base) :-
subtype(Comp, Base),
consists(Comp, Base).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 22 / 86
25. Intro Cutting
Outline
1 Introduction to Prolog
Terms
Unification
Queries
Rules
Cutting
Term Expansion
Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 23 / 86
26. Intro Cutting
The Notorious Cut
The ‘!’ operator
Do not retry previous goals
Do not try subsequent clauses
Example
type(X, v) :- var(X). ?- type(a, T).
type(X, a) :- atom(X). T = a ;
type(_, f). T = f
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 24 / 86
27. Intro Cutting
The Notorious Cut
The ‘!’ operator
Do not retry previous goals
Do not try subsequent clauses
Example
type(X, v) :- var(X) !. ?- type(a, T).
type(X, a) :- atom(X) !. T = a ;
type(_, f).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 24 / 86
28. Intro Term Expansion
Outline
1 Introduction to Prolog
Terms
Unification
Queries
Rules
Cutting
Term Expansion
Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 25 / 86
29. Intro Term Expansion
Term Expansion
Macro definition
term_expansion(recursive(R, P),
[(Head :- CallP1),
(Head :- CallP2, CallR)]) :-
Head =.. [R, X, Y],
CallP1 =.. [P, X, Y],
CallP2 =.. [P, X, Z],
CallR =.. [R, Z, Y].
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 26 / 86
30. Intro Term Expansion
Term Expansion
Source Expanded
recursive(subtype, subtype(X, Y) :-
extends). extends(X, Y).
subtype(X, Y) :-
extends(X, Z),
subtype(Z, Y).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 27 / 86
31. Intro Summary
Outline
1 Introduction to Prolog
Terms
Unification
Queries
Rules
Cutting
Term Expansion
Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 28 / 86
32. Intro Summary
Summary of Prolog Terminology
foo(goo, X, bar(Elem))
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 29 / 86
33. Intro Summary
Summary of Prolog Terminology
Atoms
foo(goo, X, bar(Elem))
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 29 / 86
34. Intro Summary
Summary of Prolog Terminology
foo(goo, X, bar(Elem))
Variables
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 29 / 86
35. Intro Summary
Summary of Prolog Terminology
functor
foo(goo, X, bar(Elem))
functor
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 30 / 86
36. Intro Summary
Summary of Prolog Terminology
Head subtype(X, Y) :- Operator
Body extends(X, Z), Goals
subtype(Z, Y).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 31 / 86
37. Intro Summary
Summary of Prolog Terminology
extends(man, person).
Facts extends(woman, person).
extends(boy, man). Clauses
subtype(X, Y) :-
Rules extends(X, Y).
subtype(X, Y) :-
extends(X, Z),
subtype(Z, Y).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 32 / 86
38. Intro Summary
Summary of Prolog Terminology
extends(man, person).
extends/2 extends(woman, person).
extends(boy, man). Predicates
subtype(X, Y) :-
extends(X, Y).
subtype/2 subtype(X, Y) :-
extends(X, Z),
subtype(Z, Y).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 32 / 86
39. Functional Prolog
Outline
1 Introduction to Prolog
2 Functional Prolog
3 Prolog as an Interpreter
4 Prolog for Software Modeling
5 Prolog Libs and Tricks
6 Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 33 / 86
40. Functional Prolog Variables as Goals
Outline
2 Functional Prolog
Variables as Goals
Finding All Solutions
Failure as Negation
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 34 / 86
41. Functional Prolog Variables as Goals
How Two Things are Related?
Program
related(R, X, Y) :-
P =.. [R, X, Y], % P = R(X, Y)
P. % Call P
Query
?- related(extends, X, Y).
X = man, Y = person ;
X = woman, Y = person ;
...
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 35 / 86
42. Functional Prolog Variables as Goals
How Two Things are Related?
Program
related(R, X, Y) :-
call(R, X, Y).
Query
?- related(extends, X, Y).
X = man, Y = person ;
X = woman, Y = person ;
...
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 35 / 86
43. Functional Prolog Variables as Goals
How Two Things are Related?
Program
relation(extends). related(R, X, Y) :-
relation(consists). relation(R),
relation(subtype) call(R, X, Y).
Query
?- related(R, boy, person).
R = subtype
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 36 / 86
44. Functional Prolog Variables as Goals
How Two Things are Related?
Program
relation(extends). related(R, X, Y) :-
relation(consists). relation(R),
relation(subtype) object(X),
object(person). object(Y),
object(couple). % etc. call(R, X, Y).
Query
?- related(R, X, Y).
R = consists, X = couple, Y = man ;
...
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 37 / 86
45. Functional Prolog Variables as Goals
Curried Call
Query
?- call(consists(couple), X).
X = man ;
X = woman
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 38 / 86
46. Functional Prolog Variables as Goals
Curried Call
call appends the arguments to
the functor
Query
?- call(consists(couple), X).
X = man ;
X = woman
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 38 / 86
47. Functional Prolog Finding All Solutions
Outline
2 Functional Prolog
Variables as Goals
Finding All Solutions
Failure as Negation
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 39 / 86
48. Functional Prolog Finding All Solutions
Finding All Solutions
Meta-Predicates
findall(Template, Goal, Bag)
bagof(Template, Goal, Bag)
setof(Template, Goal, Bag)
findall example
?- findall(X, extends(X, person), Xs).
Xs = [man, woman].
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 40 / 86
49. Functional Prolog Finding All Solutions
List Processing
List meta-predicates
maplist(Pred, List1, List2) (in lists module)
include, exclude, partition (in apply module)
include example
subtype_of(Y, X) :- subtype(X, Y).
?- include(subtype_of(person),
[item, man, boy, element], L).
L = [man, boy].
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 41 / 86
50. Functional Prolog Failure as Negation
Outline
2 Functional Prolog
Variables as Goals
Finding All Solutions
Failure as Negation
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 42 / 86
51. Functional Prolog Failure as Negation
Failure as Negation
The ‘fail’ operator
+ R Succeed only if R fails
Program
unassignable(X, Y) :- + subtype_of(X, Y),
Query
?- unassignable(boy, bucket).
true.
?- unassignable(boy, person).
false.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 43 / 86
52. Prolog as an Interpreter
Outline
1 Introduction to Prolog
2 Functional Prolog
3 Prolog as an Interpreter
4 Prolog for Software Modeling
5 Prolog Libs and Tricks
6 Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 44 / 86
53. Prolog as an Interpreter Programs as Inputs
Outline
3 Prolog as an Interpreter
Programs as Inputs
The Prolog Meta-Interpreter
Forward Chaining
OOP
Other Interpreters
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 45 / 86
54. Prolog as an Interpreter Programs as Inputs
Programs as Inputs
Interpreter Input
subtype(X, Y) :- extends(man, person).
extends(X, Y). extends(woman, person).
subtype(X, Y) :- extends(boy, man).
extends(X, Z), consists(couple, man).
subtype(Z, Y). consists(couple, woman).
composite(C, B) :-
subtype(C, B), Queries
consists(C, B).
?- subtype(X, Y).
X = man, Y = person
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 46 / 86
55. Prolog as an Interpreter Programs as Inputs
Programs as Inputs
Interpreter Input
subtype(X, Y) :- extends(item, elem).
extends(X, Y). extends(bucket, elem).
subtype(X, Y) :- consists(bucket, elem).
extends(X, Z),
subtype(Z, Y).
composite(C, B) :-
subtype(C, B), Queries
consists(C, B).
?- subtype(X, Y).
X = item, Y = elem
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 46 / 86
56. Prolog as an Interpreter Programs as Inputs
Programs as Inputs
Interpreter Input
:- op(xfx, 300, |>). item |> elem.
:- op(xfx, 300, <>). bucket |> elem.
bucket <> elem.
subtype(X, Y) :-
X |> Y.
subtype(X, Y) :- Queries
X |> Z, Z <> Y.
?- subtype(X, Y).
composite(C, B) :- X = item, Y = elem
subtype(C, B),
C <> B.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 46 / 86
57. Prolog as an Interpreter The Prolog Meta-Interpreter
Outline
3 Prolog as an Interpreter
Programs as Inputs
The Prolog Meta-Interpreter
Forward Chaining
OOP
Other Interpreters
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 47 / 86
58. Prolog as an Interpreter The Prolog Meta-Interpreter
The Prolog Meta-Interpreter
Meta-Interpreter
meta((A, B)) :- !, meta(A), meta(B).
meta(A) :- system(A), !, A.
meta(A) :- (A :- B), meta(B).
system(true).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 48 / 86
59. Prolog as an Interpreter The Prolog Meta-Interpreter
The Prolog Meta-Interpreter
Meta-Interpreter
meta((A, B)) :- !, meta(A), meta(B).
meta(A) :- system(A), !, A.
meta(A) :- clause(A, B), meta(B).
system(true).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 48 / 86
60. Prolog as an Interpreter The Prolog Meta-Interpreter
Modifying the Meta-Interpreter
Asking your opinion
meta((A, B)) :- !, meta(A), meta(B).
meta(A) :- system(A), !, A.
meta(A) :- clause(A, B), meta(B).
meta(A) :-
+ clause(A,_), ground(A),
print(A), print(’?’),
read(yes).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 49 / 86
61. Prolog as an Interpreter Forward Chaining
Outline
3 Prolog as an Interpreter
Programs as Inputs
The Prolog Meta-Interpreter
Forward Chaining
OOP
Other Interpreters
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 50 / 86
62. Prolog as an Interpreter Forward Chaining
Forward Chaining
Example program using a ’-:’ operator
p -: q.
r, s -: p.
q -: t.
Example run
?- fire(r), fire(s).
true
?- t.
true.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 51 / 86
63. Prolog as an Interpreter Forward Chaining
Forward Chaining Interpreter
Fire
:- op(1150, xfx, -:).
fire(X) :- clause(X, true), !.
fire(X) :-
assert(X),
(If -: Then),
amember(X, If), % member in a commas list
+ (amember(Y, If), + clause(Y, true)),
fire(Then),
fail.
fire(_).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 52 / 86
64. Prolog as an Interpreter OOP
Outline
3 Prolog as an Interpreter
Programs as Inputs
The Prolog Meta-Interpreter
Forward Chaining
OOP
Other Interpreters
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 53 / 86
65. Prolog as an Interpreter OOP
Prologer’s OOP
Objects
object(rect(W, H),
[(area(A) :- A is W*H),
(show :- print(W * H))]).
object(square(X), []).
isa(square(X), rect(X, X)).
Sending messages
?- Rec1 = rect(4,5), send(Rec1, area(A)).
A = 20
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 54 / 86
66. Prolog as an Interpreter OOP
Prolog OOP Interpeter
send(Obj, Msg) :-
get_methods(Obj, Methods),
process(MSg, Methods).
get_methods(Obj, Methods) :-
object(Obj, Methods).
get_methods(Obj, Methods) :- % Inheritance
isa(Obj, Super),
get_methods(Super, Methods).
process(Msg, [Msg | _]). % Fact
process(Msg, [(Msg :- Body) | _]) :- % Rule
call(Body).
process(Msg, [_ | Rest]) :-
process(Msg, Rest).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 55 / 86
67. Prolog as an Interpreter Other Interpreters
Outline
3 Prolog as an Interpreter
Programs as Inputs
The Prolog Meta-Interpreter
Forward Chaining
OOP
Other Interpreters
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 56 / 86
68. Prolog as an Interpreter Other Interpreters
Other Interpreters
Breadth-first
Best-first
Uncertainty
Constraint solving
...
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 57 / 86
69. Prolog for Software Modeling
Outline
1 Introduction to Prolog
2 Functional Prolog
3 Prolog as an Interpreter
4 Prolog for Software Modeling
5 Prolog Libs and Tricks
6 Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 58 / 86
70. Prolog for Software Modeling OPM
Outline
4 Prolog for Software Modeling
OPM
Erlang AST
Synthesizing Code
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 59 / 86
71. Prolog for Software Modeling OPM
The Object-Process Methodology (OPM)
Dov Dori, 2002
Paragraph (OPL) Diagram (OPD)
registry
object person
states single, married. person
rabbi
object man, woman, couple.
married
object registry, rabbi, ring.
process marrying.
man, woman extends person. marrying
couple consists man, woman. single
marrying changes person 1..2
ring
from single to married. man
marrying consumes ring(1,2).
marrying yields couple. couple
woman
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 60 / 86
72. Prolog for Software Modeling OPM
Representing OPM
OPM database Querying
object(person). ?- object(X).
object(man). X = person ;
object(woman).
process(marrying).
X = man ;
state(person::single). X = woman;
state(person::married). ...
extends(man, person).
extends(woman, person). ?- extends(X, person).
consists(couple, man). X = man ;
consists(couple, woman).
consumes(marrying, ring).
X = woman
produces(marrying, couple).
...
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 61 / 86
73. Prolog for Software Modeling OPM
More Queries
Use cases Conversion functions
use_case(Proc) :- convert(Proc, From, To) :-
process(Proc), process(Proc),
object(User), consumes(Proc, From),
handles(User, Proc), yields(Proc, To),
+ (process(Top), + requires(Proc, _),
consists(Top, Proc)). + handles(_, Proc),
+ (consumes(Proc, X),
X = From),
+ (yields(Proc, Y),
Y = To).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 62 / 86
74. Prolog for Software Modeling Erlang AST
Outline
4 Prolog for Software Modeling
OPM
Erlang AST
Synthesizing Code
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 63 / 86
75. Prolog for Software Modeling Erlang AST
Erlang ASTs
Erlang
-module(pq).
p(X) -> X + 1.
q(X) -> X + 2.
r(A) -> A + 3.
AST tuples
{function,4,p,1,
[{clause,4,[{var,4,’X’}],[],
[{op,5,’+’,{var,5,’X’},{integer,5,1}}]}]}
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 64 / 86
76. Prolog for Software Modeling Erlang AST
Erlang ASTs
Erlang
-module(pq).
p(X) -> X + 1.
q(X) -> X + 2.
r(A) -> A + 3.
AST functors
function(4,’p’,1,
[clause(4,[var(4,’X’)],[],
[op(5,’+’,var(5,’X’),integer(5,1))])])
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 64 / 86
77. Prolog for Software Modeling Synthesizing Code
Outline
4 Prolog for Software Modeling
OPM
Erlang AST
Synthesizing Code
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 65 / 86
78. Prolog for Software Modeling Synthesizing Code
A Data Flow Model
Paragraph Diagram
process p, q, r, pq, s. a x
object x, y, z, a, b.
s
pq consists p, q.
s consists pq, r. pq
p consumes x.
q consumes x. r q p
p yields y.
q yields z.
r consumes a. b z y
r yields b.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 66 / 86
79. Prolog for Software Modeling Synthesizing Code
Synthesized Sequential Code
Model Sequential Code
a x
s
-module(pq1).
pq -record(pq_ret,{y,z}).
r q p
-record(s_ret,{b,y,z}).
% p, q, and r
b z y pq(X) ->
Y = p(X),
Z = q(X),
Source Code #pq_ret{y=Y, z=Z}.
s(A, X) ->
-module(pq). #pq_ret{y=Y, z=Z} = pq(X),
p(X) -> X + 1. B = r(A),
q(X) -> X + 2. #s_ret{b=B, y=Y, z=Z}.
r(A) -> A + 3.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 67 / 86
80. Prolog for Software Modeling Synthesizing Code
Synthesized Parallel Code
-module(pq1).
-record(pq_ret,{y,z}).
-record(s_ret,{b,y,z}).
p(X) -> X + 1.
p1(X) ->
Y = p(X),
pq ! {y,Y}.
q(X) -> X + 2.
q1(X) ->
Z = q(X),
pq ! {z,Z}.
r(A) -> A + 3.
r1(A) ->
B = r(A),
s ! {b,B}.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 68 / 86
81. Prolog for Software Modeling Synthesizing Code
Synthesized Parallel Code
pq(X) ->
register(q, spawn(fun() -> q1(X) end)),
register(p, spawn(fun() -> p1(X) end)),
receive {y,Y} -> ok end,
receive {z,Z} -> ok end,
#pq_ret{y = Y,z = Z}.
pq1(X) ->
#pq_ret{y = Y,z = Z} = pq(X),
s ! {y,Y}, s ! {z,Z}.
s(A, X) ->
register(s, self()),
register(r, spawn(fun() -> r1(A) end)),
register(pq, spawn(fun() -> pq1(X) end)),
receive {y,Y} -> ok end,
receive {z,Z} -> ok end,
receive {b,B} -> ok end,
#s_ret{y = Y,z = Z,b = B}.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 68 / 86
82. Prolog for Software Modeling Synthesizing Code
Example Rules
yields_snd0(P, X,
op(’!’, atom(P),
tuple([atom(X), V]))) :-
var_of(X, V).
yields_snd(From, To, X, S) :-
consists(P, From),
consists(P, To),
yields_snd0(To, X, S).
yields_snd(From, To, X, S) :-
consists(P, From),
+ consists(P, To),
yields_snd0(P, X, S).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 69 / 86
83. Prolog for Software Modeling Synthesizing Code
Workflow
Sequential Extract AST AST as
module from code Prolog functors
Synthesize
Model as
OPM Term
Prolog facts AST of parallel
model expansion
module
Erlang
pretty-printer
Parallel
module
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 70 / 86
84. Prolog Libs and Tricks
Outline
1 Introduction to Prolog
2 Functional Prolog
3 Prolog as an Interpreter
4 Prolog for Software Modeling
5 Prolog Libs and Tricks
6 Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 71 / 86
85. Prolog Libs and Tricks DCG
Outline
5 Prolog Libs and Tricks
DCG
Constraints
Meta-Prolog
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 72 / 86
86. Prolog Libs and Tricks DCG
Definitive Clause Grammar (DCG)
Grammar Rules
integer(I) -->
digit(D0),
digits(D),
{ number_chars(I, [D0|D]) }.
digits([D|T]) -->
digit(D), !,
digits(T).
digits([]) --> [].
digit(D) -->
[ D ],
{ code_type(D, digit) }.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 73 / 86
87. Prolog Libs and Tricks DCG
Definitive Clause Grammar (DCG)
Grammar Rules
integer(I) -->
digit(D0),
digits(D),
{ number_chars(I, [D0|D]) }. Call grammar rules
digits([D|T]) -->
digit(D), !,
digits(T).
digits([]) --> []. Regular Prolog
digit(D) -->
[ D ], Match in list
{ code_type(D, digit) }.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 73 / 86
88. Prolog Libs and Tricks DCG
Definitive Clause Grammar (DCG)
Phrasing
?- phrase(integer(X), "42 times", Rest).
X = 42
Rest = " times"
?- phrase(integer(42), S).
S = "42"
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 74 / 86
89. Prolog Libs and Tricks Constraints
Outline
5 Prolog Libs and Tricks
DCG
Constraints
Meta-Prolog
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 75 / 86
90. Prolog Libs and Tricks Constraints
Constraints Solving over Finite Domains
Problem Solver
S E N D :- use_module(library(clpfd)).
+ M O R E
puzzle([S,E,N,D] + [M,O,R,E] =
---------
[M,O,N,E,Y]) :-
M O N E Y Vars = [S,E,N,D,M,O,R,Y],
Vars ins 0..9,
all_different(Vars),
S*1000 + E*100 + N*10 + D +
M*1000 + O*100 + R*10 + E #=
M*10000 + O*1000 + N*100 + E*10 + Y,
M #= 0, S #= 0.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 76 / 86
91. Prolog Libs and Tricks Constraints
Variables Labeling
Solving the puzzle
?- puzzle(As+Bs=Cs), label(As).
As = [9, 5, 6, 7],
Bs = [1, 0, 8, 5],
Cs = [1, 0, 6, 5, 2]
Labeling options
Minimum / maximum of an expression
By order
Other strategies
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 77 / 86
92. Prolog Libs and Tricks Meta-Prolog
Outline
5 Prolog Libs and Tricks
DCG
Constraints
Meta-Prolog
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 78 / 86
93. Prolog Libs and Tricks Meta-Prolog
Reflective Predicates
Caller/Callee relations
uses(Caller, Callee) :-
current_predicate(Caller, Head),
+ predicate_property(Head, built_in),
clause(Head, Body),
amember(Goal, Body),
+ var(Goal),
functor(Goal, Callee, _).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 79 / 86
94. Prolog Libs and Tricks Meta-Prolog
Reflective Predicates
Sample Program
bar(X) :- foo(X).
bar(X) :- goo(1), bar(X).
Usage
?- uses(bar, X). ?- uses(X, foo).
X = foo ; X = bar
X = goo ;
X = bar ?- recursive(X).
X = bar
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 80 / 86
95. Prolog Libs and Tricks Meta-Prolog
Manipulative Predicates
Poor man’s AOP
wrap(Head, Before, After) :-
clause(Head, Body),
retract((Head :- Body)),
Head =.. [Name | Args],
atom_concat(Name, 1, Name1),
Head1 =.. [Name1 | Args],
assert((Head1 :- Body)),
assert((Head :- Before, Head1, After)),
fail.
wrap(_, _, _).
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 81 / 86
96. Prolog Libs and Tricks Meta-Prolog
Manipulative Predicates
Predicate
:- dynamic test/1.
test(A) :- atom(A), !, print(A).
test(X) :- N is X + 1, print(N).
Wrapping
?- test(5).
6
?- wrap(test(X), print(X), print(a)).
?- test(5).
56a
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 82 / 86
97. Summary
Outline
1 Introduction to Prolog
2 Functional Prolog
3 Prolog as an Interpreter
4 Prolog for Software Modeling
5 Prolog Libs and Tricks
6 Summary
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 83 / 86
98. Summary
Claimer
Prolog can:
Help you write DSLs and interpreters
Add AI capabilities to your domain
Be a hacker’s delight!
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 84 / 86
99. Summary
Bibliography
Leon Sterling, Ehud Shapiro
The Art of Prolog
Richard O’Keefe
The Craft of Prolog
Yoav Shoham
Artificial Intelligence Techniques in Prolog
Ivan Bratko
Prolog Programming for Artificial Intelligence
Dov Dori
Object-Process Methodology
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 85 / 86
100. Summary
thank :- you, !.
Guy Wiener (Sayeret Lambda) Prolog as a Meta-Language July 14, 2010 86 / 86