Re: backtracking in prolog



On Nov 8, 12:17 pm, equinox <aditya15...@xxxxxxxxx> wrote:
Chip,

Thank you for the suggestion, and sorry for not being so clear on my
question. I am actually building a scanner using
prolog. The thing that I am trying to satisfy here is to say true when
there are ONE OR MORE VOWELS in the list. I have
tried your come and it gives me mostly of what I want. Perhaps that I
should mention this as well. An example:

foo([vowel, con], [a,i,u,e,k]).
true;
no.

notice that it only returns one true in the execution instead of 5
true. Con is just another bunch of rules I have in the database
that matches exactly one consonant. So is there anyway you could help
me out in this case?

Regards,
equinox

On Nov 8, 7:45 am, Chip Eastham <hardm...@xxxxxxxxx> wrote:

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



Let's set aside for the moment the particular predicate
or combination of predicates (vowel, con, etc.) that you
are trying to apply to a list. Let's clear up what you
expect to happen for various lists that have more than
one element.

The title of your thread says 'backtracking in prolog',
and I see the semicolon in your output, so I'm guessing
that indeed you want a non-deterministic goal, one that
potentially can succeed in more than one way.

So let's assume a new goal, bar(List), and consider some
inputs that we might give:

bar([yes,yes,no,no,yes]).

bar([no,yes,yes,yes,yes]).

The idea here is to dispense for the time being with
your internal database and just suppose as a placeholder
that the code would succeed for yes items and fail for
no items.

I think you will clear up what it is about the
backtracking behavior that you expect/need. Then we
can go back into combining more than one predicate,
if you wish.


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: Getting a list of list permissions for WSS
    ... this on a backup copy of the database so you're not hitting the live DB. ... Now when you run the script and refresh the SPS Event List which this ... my departmental Event Lists roll-up flawlessly to a single Portal list. ...
    (microsoft.public.sharepoint.portalserver)