For an assignment I have to generate a prolog procedure body dynamically. How can I do this?
See here for the swi prolog manual description of relevant predicates.
As an example consider the following
goal :- Z =.. [foo, 1],
Y =.. [bar,2],
X =.. [',', Z, Y],
R =.. [':-', r, X],
assert(Z), assert(Y), assert(X), assert(R).
PS: Another possibility is lower class, but sometimes can be a better choice: just print out what you need to construct to a file.