For Programmers: Free Programming Magazines  


Home > Archive > Prolog > May 2004 > Forced expansion with underlying test.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Forced expansion with underlying test.
Dirk Mittler

2004-05-12, 9:20 pm

In my previous posting, I listed a function which performed a test for
attribute-consistency (via existing hooks) on an object, for use with CLP.
And a fact about my test, was that it already wasted a lot of CPU time on
large structures, by duplicating the entire term. But I would still want to
be sure that my test, and the attribute hooks, don't change the value of my
argument.

Since the work is performed anyway, it occurred to me, that given a term,
rather than a variable always, I might just as well perform the test for
each atom in the term. I have therefore defined a second version of this,
which performs an attribute test, which is an example of forced expansion.

Here is the source-code:

lousy_attrib(X) :-
freeze(X, fail).

consistent(X) :- nonvar(X),
duplicate_term(X, Temp),
copy_term(X, Temp).

consistent(X) :- var(X),
duplicate_term(X, Temp),
copy_term(0, Temp).

consistent_l(L) :- is_list(L),
length(L,N),
consistent_l(L,N).

consistent_l(L) :- \+ is_list(L),
consistent(L).

consistent_l(_L,0).

consistent_l(L, N) :-
nth1(N, L, E),
consistent_l(E),
N2 is N - 1,
consistent_l(L, N2).

consistent_e(E) :- nonvar(E),
E =.. L,
consistent_l(L).

consistent_e(E) :- var(E),
consistent(E).


And this is the result:

1 ?- lousy_attrib(A), E = [[A, B], C], consistent(E).

A = _G475{freeze = user:fail}
E = [[_G475{freeze = user:fail}, _G480], _G486]
B = _G480
C = _G486

Yes
2 ?- lousy_attrib(A), E = [[A, B], C], consistent_e(E).

No
3 ?- E = [[A, B], C], consistent_e(E).

E = [[_G378, _G381], _G387]
A = _G378
B = _G381
C = _G387

Yes
4 ?-


Dirk



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com