Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Prolog
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Prolog
(section)
Add topic