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!
== Programming in Prolog == In Prolog, loading code is referred to as ''consulting''. Prolog can be used interactively by entering queries at the Prolog prompt <code>?-</code>. If there is no solution, Prolog writes <code>no</code>. If a solution exists then it is printed. If there are multiple solutions to the query, then these can be requested by entering a semi-colon <code>;</code>. There are guidelines on good programming practice to improve code efficiency, readability and maintainability.<ref>{{Cite journal |last1=Covington |first1=Michael A. |last2=Bagnara |first2=Roberto |last3=O'Keefe |first3=Richard A. |author-link3=Richard O'Keefe |last4=Wielemaker |first4=Jan |last5=Price |first5=Simon |title=Coding guidelines for Prolog |doi=10.1017/S1471068411000391 |journal=[[Theory and Practice of Logic Programming]] |volume=12 |issue=6 |pages=889β927 |year=2011 |arxiv=0911.2899|s2cid=438363}}</ref> Here follow some example programs written in Prolog. === Hello World === Example of a basic query in a couple of popular Prolog dialects: {| |- ! [[SWI-Prolog]] ! [[GNU Prolog]] |- |<syntaxhighlight lang="prolog"> ?- write('Hello World!'), nl. Hello World! true. ?- </syntaxhighlight> |<syntaxhighlight lang="prolog"> | ?- write('Hello World!'), nl. Hello World! yes | ?- </syntaxhighlight> |} This comparison shows the prompt ("?-" vs "| ?-") and resolution status ("true". vs "yes", "false". vs "no") can differ from one Prolog implementation to another. === Compiler optimization === Any computation can be expressed declaratively as a sequence of state transitions. As an example, an [[optimizing compiler]] with three optimization passes could be implemented as a relation between an initial program and its optimized form: <syntaxhighlight lang="prolog"> program_optimized(Prog0, Prog) :- optimization_pass_1(Prog0, Prog1), optimization_pass_2(Prog1, Prog2), optimization_pass_3(Prog2, Prog). </syntaxhighlight> or equivalently using [[definite clause grammar|DCG]] notation: <syntaxhighlight lang="prolog"> program_optimized --> optimization_pass_1, optimization_pass_2, optimization_pass_3. </syntaxhighlight> === Quicksort === The [[quicksort]] sorting algorithm, relating a list to its sorted version: <syntaxhighlight lang="prolog"> partition([], _, [], []). partition([X|Xs], Pivot, Smalls, Bigs) :- ( X @< Pivot -> Smalls = [X|Rest], partition(Xs, Pivot, Rest, Bigs) ; Bigs = [X|Rest], partition(Xs, Pivot, Smalls, Rest) ). quicksort([]) --> []. quicksort([X|Xs]) --> { partition(Xs, X, Smaller, Bigger) }, quicksort(Smaller), [X], quicksort(Bigger). </syntaxhighlight>
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)