accessing program listing in prolog

122 Views Asked by At

I'm having some strange (or not so strange) problems defining variables in SWI-Prolog. Example:

I'd like to do something like below:

:- initialization(main).

main :- 
X = listing(main),
write(X).

but it's simply printing "listing(main)"

2

There are 2 best solutions below

0
On BEST ANSWER

maybe, using another casual predicate instead of main/0...

?- with_output_to(atom(X), listing(pattern)), write(X).
gram:pattern(A, B, C) :-
    dig(A, B, C).
gram:pattern(A+C, B, E) :-
    ten(A, B, D),
    dig(C, D, E).
...
0
On

What you are doing with X = listing(...) is creating a term, which later you are printing with write.

It seems you want to access the code of main. The thing you are looking for is clause/2:

clause(:Head, ?Body)

True if Head can be unified with a clause head and Body with the corresponding clause body. Gives alternative clauses on backtracking. For facts, Body is unified with the atom true.

Example:

main :- clause(main, X), write(X).

?- main.
clause(main,_G2381),write(_G2381)
true.