[eclipse-users] Fwd: 'forall' predicate

From: Malcolm Ryan <malcolmr_at_cse.unsw.edu.au>
Date: Wed, 28 Mar 2007 17:39:24 +1000
On 28/03/2007, at 4:57 PM, Malcolm Ryan wrote:
> I want to construct a predicate which applies a certain constraint to
> every variable in a list, kind of like the "map" function in
> functional programming. Its logical semantics should be "for all X in
> List : P(X)".

I worked it out. I wrote my own version of copy_term which only  
duplicated a particular variable. The code is below. If anyone can  
suggest a more efficient way of doing this, I'd like to hear it.

Malcolm

forall(X, Variables, Constraint) :-
	(foreach(V, Variables), param(X, Constraint)
	do
		copy_term(Constraint, Constraint1, X, Y),
		Y = V,
		call(Constraint1)
	).
	
copy_term(Term, Term, _X, _Y) :-
	ground(Term), !.
	
copy_term(Term, Copy, X, Y) :-
	var(Term), !,
	(Term == X ->
		Copy = Y
	;
		Copy = Term
	).
	
copy_term(Term, Copy, X, Y) :-
	Term =.. List,
	(foreach(T, List),
	 foreach(C, ListCopy),
	 param(X, Y)
	do
		copy_term(T, C, X, Y)
	),
	Copy =.. ListCopy.

>
> The following is close to what I want, but has a flaw:
>
> forall(X, Vars, P) :-
>     (foreach(V, Vars), param(X,P)
>     do
>         copy_term((X,P), (Y,Q)),
>         Y = V,
>         call(Q)
>     ).
>
> The problem is that the call to copy_term duplicates _every_ variable
> in P, not just X. So the following will not work.
>
> integers([N]), forall(X, [V1, V2, V3], X < N), N #= 3.
>
> The final binding of N does not effect V1, V2, V3 because they are
> each constrained with regard to a copy of N and not N itself.
>
> Malcolm
>
> --
> "The modern man in revolt has become
>            practically useless for all purposes of revolt.
> By rebelling against everything
>            he has lost his right to rebel against anything."
>                                                     - G.K.Chesterton,
> Orthodoxy
>
>
>
>
> _______________________________________________
> ECLiPSe-Users mailing list
> ECLiPSe-Users_at_crosscoreop.com
> http://www.crosscoreop.com/mailman/listinfo/eclipse-users

--
      "Progress should mean that we are always changing the world to fit
       the vision, instead we are always changing the vision."
                - G.K.Chesterton, Orthodoxy
Received on Wed Mar 28 2007 - 08:39:36 CEST

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:57 CET