Prolog: Saving the fact+rules to file

Sometimes you may wish you can save your current state to file. Here is how you do it

save(Heads,File) :- tell(File), listing(Heads), told.

you can use the same idea to save the result of other predicates to file.