Re: backtracking in prolog



On Nov 7, 7:21 pm, herlamba <aditya15...@xxxxxxxxx> wrote:
I have a lot of database called: istrue(X), X here is just a char.

so something like:

istrue('a')
istrue('d')
istrue('h')
.......
and the list goes on

I want to create a predicate, which takes two arguments, each which is
a list. The predicate is called foo for example (so foo takes two
list).

what foo does here is to find if there's more than 1 match of istrue
in the database, an example:

foo([istrue], [a,i,e]) will return

true;
true;
true

because foo can match one or more things found in the istrue()
database.

another example

foo([istrue], [d,k,o]) will return

no

because the first character 'd' is not in the istrue database, in
other words there's no istrue('d').

Question is, how can I create such function? So far I've tried and
been able to get one true only

Hi, herlamba:

For future reference, it would be expeditious if
you describe what you tried that didn't work, at
least in part. If nothing else, it helps to
gauge the level of explanation that will be most
helpful to you.

I think I understand the main point you ask about,
though there are a couple of minor inconsistencies
we can skip over for now.

You seem to want the backtracking to continue only
as long as the predicate can be satisfied, ie. to
stop backtracking at the first item in the list
that does not satisfy the given predicate. I say
this because in your second example there was no
backtracking, just stopping after trying istrue('d')
(although istrue('d') was included at the top of
you post in your example code).

So I'd do something like this:

foo( MyPred, [H|_] ) :- not( MyPred(H) ), !, fail.
foo( MyPred, [H|_] ) :- MyPred(H).
foo( MyPred, [_|T] ) :- !, foo( MyPred, T ).

The first clause introduces a cut to force a stop
if an item is encountered that doesn't satisfy the
predicate. Otherwise the attempt to satisfy foo/2
continues backtracking through the list.

When you wrote that "what foo does here is to find if
there's more than 1 match of istrue in the database",
it actually suggested to me a more complicated goal
than what your examples suggest, so if I've perhaps
missed your meaning somewhat.

regards, chip
.



Relevant Pages

  • Re: backtracking in prolog
    ... The predicate is called foo for example (so foo takes two ... that does not satisfy the given predicate. ... backtracking, ... expect to happen for various lists that have more than ...
    (comp.lang.prolog)
  • Re: backtracking in prolog
    ... The predicate is called foo for example (so foo takes two ... that does not satisfy the given predicate. ... backtracking, ... expect to happen for various lists that have more than ...
    (comp.lang.prolog)
  • Re: backtracking in prolog
    ... The predicate is called foo for example (so foo takes two ... that does not satisfy the given predicate. ... backtracking, ... expect to happen for various lists that have more than ...
    (comp.lang.prolog)
  • Re: backtracking in prolog
    ... Con is just another bunch of rules I have in the database ... The predicate is called foo for example (so foo takes two ... You seem to want the backtracking to continue only ... that does not satisfy the given predicate. ...
    (comp.lang.prolog)
  • Re: "is a" & "has a" relations re: 2nd-Order Logic
    ... But the relation of a FOO to a second-order ... predicate seems to be a "has a" relation. ... x is NOT a "first-order object" in this context. ... FOP: sweet ...
    (sci.logic)