28621

Predicate must be true of all elements in a list

Question:

I have a set of facts:

likes(john,mary). likes(mary,robert). likes(robert,kate). likes(alan,george). likes(alan,mary). likes(george,mary). likes(harry,mary). likes(john,alan).

Now I want to write a relation which will check for all element X of an input list if likes(X,A) is true. my relation should return true once if likes(X,A) is true for all element X in my list L. If I try this this:

relat(X) :- member(A,[john,alan,george,harry]), likes(A,X).

but the output is

?- relat(mary). true ; true ; true ; true.

I want to write it such that it returns one true once it found that likes(john,mary),likes(alan,mary),likes(george,mary),likes(harry,mary) all are true. How to approach this problem?

Answer1:

In SWI-Prolog, you can use forall/2:

?- forall(member(A, [john, alan, george, harry]), likes(A, mary)). true. ?- forall(member(A, [john,alan,george,harry,marys_ex]), likes(A, mary)). false.

Answer2:

With standard list processing you can do the following:

helper(X, []). % No one left to check helper(X, [H|L]) :- likes(H, X), helper(X, L). % Check head, then rest relat(X) :- helper(X, [john,alan,george,harry]).

Demo:

| ?- relat(harry). no | ?- relat(mary). true ? ; no | ?-

Answer3:

Using library(lambda):

liked_byall(X, Ps) :- maplist(X+\P^likes(P,X), Ps).

Equally without lambdas:

liked_byall(X, Ps) :- maplist(liked(X), Ps). liked(X, P) :- likes(P, X).

Equally:

liked_byall(_X, []). liked_byall(X, [P|Ps]) :- likes(P, X), liked_byall(X, Ps).

With above definitions you can ask even more general questions like "Who is liked by certain persons?"

?- liked_byall(N,[john, alan, george, harry]). N = mary ; false.

With the following definition these general questions are no longer possible.

liked_byall(X, Ps) :- \+ ( member(P, Ps), \+ likes(P, X) ).

This second definition only makes sense if X is ground and Ps is a ground list. We can ensure this as follows:

liked_byall(X, Ps) :- ( ground(X+Ps) -> true ; throw(error(instantiation_error,_)) ), length(Ps,_), \+ ( member(P, Ps), \+ likes(P, X) ).

These extra checks ensure that absurd cases as the following do not succeed:

?- liked_byall(mary, nonlist).

And that otherwise legitimate cases do not produce an incorrect answer:

?- liked_byall(N,[john, alan, george, harry]), N = the_grinch. N = the_grinch.

Recommend

  • Prolog: Access read from parent rule
  • Help With Prolog Lists
  • How to change incorrect spelling for the correct word?
  • prolog I have to make a program that calculates the magic matrix permutate
  • Puzzle taken from Gardner
  • extracting evidence of equality from match
  • Understanding how to construct GHC.Generics Rep's and convert back to values
  • Einstein's riddle
  • Combinatios in List of LIsts Prolog
  • how to read a file in prolog?
  • conditions for accessors in Coldfusion ORM
  • Enumerating Controls on a Form
  • Basic many-to-many left join query
  • Why isn't obj.style.left = “200px”; working in this code?
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • Floated image with variable width and heading with background image
  • SetWindowsHookEx does not react on media keys
  • Remove final comma from string in vb.net
  • Create DicomImage from scratch using Dcmtk
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • Blackberry - Custom EditField Cursor
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • How to do unit test for HttpContext.Current.Server.MapPath
  • output of program is not same as passed argument
  • How to check if every primary key value is being referenced as foreign key in another table
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • ORA-29908: missing primary invocation for ancillary operator
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • python regex in pyparsing
  • Trying to get generic when generic is not available
  • How does Linux kernel interrupt the application?
  • python draw pie shapes with colour filled
  • Reading document lines to the user (python)
  • How to Embed XSL into XML
  • Converting MP3 duration time