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!
== 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]].
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)