Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Prolog
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Meta-interpreters and reflection == Prolog is a [[homoiconic]] language and provides many facilities for [[reflective programming]] (reflection). Its implicit execution strategy makes it possible to write a concise [[meta-circular evaluator]] (also called ''meta-interpreter'') for pure Prolog code: <syntaxhighlight lang="prolog"> solve(true). solve((Subgoal1,Subgoal2)) :- solve(Subgoal1), solve(Subgoal2). solve(Head) :- clause(Head, Body), solve(Body). </syntaxhighlight> where <code>true</code> represents an empty conjunction, and <code>clause(Head, Body)</code> unifies with clauses in the database of the form <code>Head :- Body</code>. Since Prolog programs are themselves sequences of Prolog terms (<code>:-/2</code> is an infix [[Operator (programming)|operator]]) that are easily read and inspected using built-in mechanisms (like <code>read/1</code>), it is possible to write customized interpreters that augment Prolog with domain-specific features. For example, Sterling and Shapiro present a meta-interpreter that performs reasoning with uncertainty, reproduced here with slight modifications:<ref name=AOP94>{{cite book |author1=Shapiro, Ehud Y. |author2=Sterling, Leon |title=The Art of Prolog: Advanced Programming Techniques |publisher=MIT Press |location=Cambridge, Massachusetts |year=1994 |isbn=978-0-262-19338-2}}</ref>{{rp|330}} <syntaxhighlight lang="prolog"> solve(true, 1) :- !. solve((Subgoal1,Subgoal2), Certainty) :- !, solve(Subgoal1, Certainty1), solve(Subgoal2, Certainty2), Certainty is min(Certainty1, Certainty2). solve(Goal, 1) :- builtin(Goal), !, Goal. solve(Head, Certainty) :- clause_cf(Head, Body, Certainty1), solve(Body, Certainty2), Certainty is Certainty1 * Certainty2. </syntaxhighlight> This interpreter uses a table of built-in Prolog predicates of the form<ref name="AOP94"/>{{rp|327}} <syntaxhighlight lang="prolog"> builtin(A is B). builtin(read(X)). % etc. </syntaxhighlight> and clauses represented as <code>clause_cf(Head, Body, Certainty)</code>. Given those, it can be called as <code>solve(Goal, Certainty)</code> to execute <code>Goal</code> and obtain a measure of certainty about the result.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)