Load generated code

I’m using: SWI-Prolog version 8.0.1

I’ve written a prolog program that generates some prolog code. To use the generated code later, it is written to a file.

To test the generated code I do something like this:

test_code :-
    Code = [(add(A, B, C) :- C is A + B)],
    open('generated.pl', write, OutStream),
    maplist(portray_clause(OutStream), Code),
    close(OutStream),
    consult('generated.pl'),
    add(1, 2, 3).

Is it possible to load the code without writing it to a file? If so how can the code be loaded?

Thank you

1 Like

A little easier is to use assertz/1: assertz(( add(A, B, C) :- C is A + B)). If there is one thing Prolog is good at it it is that code = data and you can easily manipulate code.

2 Likes