2

I am writing a program that transforms other programs by expanding predicates. I usually do this using clause/2, but it doesn't always expand a predicate if it has no parameters:

:- set_prolog_flag('double_quotes','chars').
:- initialization(main).

main :- clause(thing,C),writeln(C).
% this prints "true" instead of "A = 1"

thing :- A = 1.

Is it possible to expand predicates that have no parameters?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328

1 Answers1

1

Some general remark: Note that this code is highly specific to SWI. In other systems which are ISO conforming you can only access definitions via clause/2, if that predicate happens to be dynamic.

For SWI, say listing. to see what is happening.

?- assert(( thing :- A = 1 )).
true.

?- listing(thing).
:- dynamic thing/0.

thing.

true.

?- assert(( thing :- p(A) = p(1) )).
true.

?- assert(( thing(X) :- Y = 2 )).
true.

?- listing(thing).
:- dynamic thing/0.

thing.
thing :-
    p(_)=p(1).

:- dynamic thing/1.

thing(_).

true.

It all looks like some tiny source level optimization.

false
  • 10,264
  • 13
  • 101
  • 209
  • `clause/2` sometimes expands clauses that have no parameters, but not always. If I change the definition to `thing :- A == 1`, then `clause(thing,A)` expands the body of the clause instead of unifying `A` with `true`. I'm still looking for an alternative to `clause/2` that works more consistently. – Anderson Green Feb 02 '20 at 23:28
  • *expands clauses that have no parameters*, and also clauses that do have parameters. See `thing/1` above. Not sure what you want to do that interferes with this little transformation. – false Feb 02 '20 at 23:31