-3

Can someone give me definitions of some keywords/predicates in Prolog please?

  • Parse-time
  • bagof
  • findall
  • the ^ predicate
  • the @ predicate

for example, bagof(X, Y^place(X, Y , japan), Xs), A<@B, A>@B.

can someone please give me the meaning of these predicates and any other that might be quite useful, I have a good interest in Prolog but due to my job I can't afford to give Prolog too much of my time. I have quite good knowledge in Prolog but it just I can't seem to find out the meaning of some words .

false
  • 10,264
  • 13
  • 101
  • 209
KennD
  • 7
  • 6
  • See for `(^)/2`: http://stackoverflow.com/questions/19931801/what-is-the-prolog-operator/19931970#19931970 – false Jan 07 '14 at 18:01

2 Answers2

2

All that you need seems to be well documented, for example for GNU-Prolog:

And parse_time in SWI-Prolog documentation

Jakub Kotowski
  • 7,411
  • 29
  • 38
2

See the links that @jkbkot gave for the complete descriptions. By way of very brief and very narrow examples:


parse_time (note spelling - SWI Prolog predicate)
?- parse_time('Fri, 08 Dec 2006 15:29:44 GMT', Format, Time).
Format = rfc_1123,
Time = 1165591784.0.

?- parse_time('2013-10-12', Format, Time).
Format = iso_8601,
Time = 1381536000.0.

Using this, you can get a numerical representation of date/time and manage them numerically. The date string is a required input parameter (must be instantiated). So, unfortunately, parse_time isn't useful for converting formats back and forth.

?- parse_time(D, iso_8601, 1381536000.0).
ERROR: atom_codes/2: Arguments are not sufficiently instantiated


bagof and ^
likes(a,b).
likes(a,c).
likes(a,d).
likes(b,c).
likes(b,e).

?- bagof(X, likes(X,Y), L).
Y = b,
L = [a] ;
Y = c,
L = [a, b] ;
Y = d,
L = [a] ;
Y = e,
L = [b].

?- bagof(X, Y^likes(X,Y), L).
L = [a, a, a, b, b]

?- bagof(X-Y, likes(X,Y), L).
L = [a-b, a-c, a-d, b-c, b-e].

?- setof(X, Y^likes(X,Y), L).   % provides the unique, sorted results
L = [a, b]

?- setof(X-Y, likes(X,Y), L).
L = [a-b, a-c, a-d, b-c, b-e].


findall
likes(a,b).
likes(a,c).
likes(a,d).
likes(b,c).
likes(b,e).

?- findall(X, likes(X,Y), L).
L = [a, a, a, b, b].

?- findall(X-Y, likes(X,Y), L).
L = [a-b, a-c, a-d, b-c, b-e].


@

The most common use of this symbol is in comparison predicates @</2, @>/2, etc. Example expressions of A<@B and A>@B are Prolog syntax errors.

?- 1 < 2.
true.

?- X=1, Y=2, X+Y<5.
true.

?- a < b.
ERROR: </2: Arithmetic: `a/0' is not a function

?- a @< b.
true.

?- a @> b.
false.

?- a < 1.
false.

?- [2,3,4] @< [2,3,5].
true.

?- [2,3,4] @< [2,3,3].
false.

?- foo(a,X) @< foo(b,Y).
true.

?- foo(b,X) @< foo(a,Y).
false.

Using @< you don't necessarily have to use parse_time if you want to compare date/time strings as long as their chronological order follows ASCII lexicographical order:

?- '2013-11-09' @< '2013-12-01'.
true.

?- '2013-01-12' @< '2012-12-12'.
false.

The @ comparison predicates can be quite handy. :)

lurker
  • 56,987
  • 9
  • 69
  • 103
  • 1
    In fact, `@`/2 is a predicate (or if you like control construct) in SWI and IF to manipulate the module context. – false Jan 07 '14 at 18:21
  • Thanks @false I totally missed that. I spend too much time in GNU Prolog (although I strove to give examples above in SWI). :) – lurker Jan 07 '14 at 18:24
  • Hi @mbratch, how did you, for example print this in a list bagof(X, Y^likes(X,Y), L). L = [a, a, a, b, b]? – KennD Jan 07 '14 at 21:44
  • @KennD that's how it works. Did you try it? – lurker Jan 07 '14 at 21:46
  • @ mbratch, sorry, what I meant to ask was, how can you create a rule for a list? without using built in functions such as bagof or findall. – KennD Jan 07 '14 at 21:56
  • @KennD that's a totally different question that you could submit in SO. If you look through SO [Prolog] section, there have been several responses to that type of question already. It's not a simple comment answer. – lurker Jan 07 '14 at 22:07
  • Could you please give me a link to the SO [Prolog] section please. – KennD Jan 07 '14 at 22:13
  • @KennD there are several if you just search for them. Most of them indicate use of `findall`. Here's a combination: http://stackoverflow.com/questions/6825218/how-to-create-a-list-of-numbers-that-add-up-to-a-specific-number – lurker Jan 07 '14 at 22:15
  • Thanks for the link, but I am looking for quite something different. The link you provided, although says how to use a list but I want to learn how to create a rule that prints given item in a database in a list e.g. names, numbers or characters – KennD Jan 07 '14 at 22:22
  • @KennD, here's another interesting post, which works for certain scenarios: http://stackoverflow.com/questions/20925428/filling-a-list-recursively/20927294#20927294 – lurker Jan 08 '14 at 15:18
  • @KennD and another nice, simple case: http://stackoverflow.com/questions/21041164/using-cuts-in-prolog-to-select-facts-from-database – lurker Jan 10 '14 at 15:18