Sujet : Re: Can we do this with ancestral cuts or something?
De : julio (at) *nospam* diegidio.name (Julio Di Egidio)
Groupes : comp.lang.prologDate : 09. Dec 2024, 16:47:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vj73f8$clr4$2@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 09/12/2024 15:14, Mild Shock wrote:
Julio Di Egidio schrieb:
Given a list of goals representing a conjunction, I would like it to fail as soon as any goal fails, but I would like *not* to cut on the search space otherwise.
>
Here is my actual test case, only considering ground goals (otherwise too many ways to play with it that seem immaterial to the problem):
>
First create a conjunction:
A1,...,An
Wrap each goal Aj into a soft cut:
(Aj *-> true; !).
The cut will abort all previous goals.
Indeed soft-cuts do it, thank you very much!
Though with this scheme: `(Aj *-> true; !, fail)`.
I have also managed to abstract the recursive scheme away to a meta-predicate, by combining soft with ancestral cuts. But I am not sure how good it is, especially since term rewriting also can do the trick and quite more simply so...
```
red(1).
red(1).
and(Xs) :- % with Xs ground!
scut(and__do(Xs)).
and__do([], _) :- !.
and__do([H:B|Xs], ChF) :-
red(B), writeln(H:B),
scut(and__do(Xs), ChF).
:- meta_predicate
scut(1),
scut(1, +).
scut(G) :-
prolog_current_choice(ChF),
call(G, ChF).
scut(G, ChF) :-
( call(G, ChF)
*-> true
; prolog_cut_to(ChF),
fail
).
/*
?- and([x:1,y:1]), fail. % ok: full search space
x:1
y:1
y:1
x:1
y:1
y:1
false.
?- and([x:1,y:0,z:1]), fail. % ok: fails early
x:1
false.
*/
```
-Julio