Re: backtracking in prolog
- From: equinox <aditya15417@xxxxxxxxx>
- Date: Sat, 8 Nov 2008 22:04:23 -0800 (PST)
On Nov 8, 10:33 pm, Chip Eastham <hardm...@xxxxxxxxx> wrote:
On Nov 9, 12:02 am, equinox <aditya15...@xxxxxxxxx> wrote:
On Nov 8, 8:13 pm, Chip Eastham <hardm...@xxxxxxxxx> wrote:
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
OK, so far I've come up with this code:
foo([vowel], [H|_] ) :- vowel(H).
foo([vowel|T], [H|T1] ) :- vowel(H),foo([vowel|T], T1).
foo([vowel|T], [H|T1] ) :- \+ vowel(H), !, foo(T, [H|T1]).
This code doesn't work for the input:
foo([vowel], [a,x,a]).
it returns 2 true instead it's supposed to return 1 only.. can you
help me what to add here, maybe I am missing something here
I'm not at all clear on your requirement, which is why
I asked my question above.
Let's drop the notion of backtracking and assume you
want a predicate that either succeeds once or fails.
(Such a predicate is called deterministic; it would
be "skipped over" in any backtracking.)
foo(MyPred, [H|_]) :- MyPred(H), !.
foo(MyPred, [_|T]) :- foo(MyPred, T).
See if this works for you. If not, you'll really
need to give some thought to defining what you
want to happen. We're just shooting in the dark
now.
regards, chip
chip have you checked your gmail email address?? I've addressed some
of the important points there.. see if you can see it, if not I can
always send it again
.
- Follow-Ups:
- Re: backtracking in prolog
- From: Chip Eastham
- Re: backtracking in prolog
- References:
- Re: backtracking in prolog
- From: Chip Eastham
- Re: backtracking in prolog
- From: equinox
- Re: backtracking in prolog
- From: Chip Eastham
- Re: backtracking in prolog
- From: equinox
- Re: backtracking in prolog
- From: Chip Eastham
- Re: backtracking in prolog
- Prev by Date: Re: backtracking in prolog
- Next by Date: Re: tracing question
- Previous by thread: Re: backtracking in prolog
- Next by thread: Re: backtracking in prolog
- Index(es):
Relevant Pages
|