Anderson Green
Anderson Green

Reputation: 31840

Expanding Prolog clauses without parameters

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?

Upvotes: 2

Views: 272

Answers (1)

false
false

Reputation: 10142

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.

Upvotes: 1

Related Questions