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!
== Higher-order programming == {{Main|Higher-order logic|Higher-order programming}} A higher-order predicate is a predicate that takes one or more other predicates as arguments. Although support for higher-order programming takes Prolog outside the domain of first-order logic, which does not allow quantification over predicates,<ref>{{cite web|title=With regard to Prolog variables, variables only in the head are implicitly universally quantified, and those only in the body are implicitly existentially quantified|url=http://okmij.org/ftp/Prolog/quantification.txt|access-date=2013-05-04}}</ref> ISO Prolog now has some built-in higher-order predicates such as <code>call/1</code>, <code>call/2</code>, <code>call/3</code>, <code>findall/3</code>, <code>setof/3</code>, and <code>bagof/3</code>.<ref name="ISO 13211"/> Furthermore, since arbitrary Prolog goals can be constructed and evaluated at run-time, it is easy to write higher-order predicates like <code>maplist/2</code>, which applies an arbitrary predicate to each member of a given list, and <code>sublist/3</code>, which filters elements that satisfy a given predicate, also allowing for [[currying]].<ref name="Naish1996"/> To convert solutions from temporal representation (answer substitutions on backtracking) to spatial representation (terms), Prolog has various all-solutions predicates that collect all answer substitutions of a given query in a list. This can be used for [[list comprehension]]. For example, [[perfect numbers]] equal the sum of their proper divisors: <syntaxhighlight lang="prolog"> perfect(N) :- between(1, inf, N), U is N // 2, findall(D, (between(1,U,D), N mod D =:= 0), Ds), sumlist(Ds, N). </syntaxhighlight> This can be used to enumerate perfect numbers, and to check if a number is perfect. As another example, the predicate <code>maplist</code> applies a predicate <code>P</code> to all corresponding positions in a pair of lists: <syntaxhighlight lang="prolog"> maplist(_, [], []). maplist(P, [X|Xs], [Y|Ys]) :- call(P, X, Y), maplist(P, Xs, Ys). </syntaxhighlight> When <code>P</code> is a predicate that for all <code>X</code>, <code>P(X,Y)</code> unifies <code>Y</code> with a single unique value, <code>maplist(P, Xs, Ys)</code> is equivalent to applying the [[Map (higher-order function)|map]] function in [[functional programming]] as <code>Ys = map(Function, Xs)</code>. Higher-order programming style in Prolog was pioneered in [[HiLog]] and [[λProlog]].
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