r/prolog • u/alexcommacommacomma • Oct 19 '26
Can Prolog code be synthesized from traces?
This has been sitting in my head for over 6 months since the second lecture I had on Logic Programming where I was introduced to SWI-Prolog's `trace` predicate (I'm not sure if it exists in other implementations).
This is what the output of `?- sibling(sally, erica).` looks like with tracing on:
5 ?- trace.
true.
[trace] 5 ?- sibling(sally, erica).
Call: (12) sibling(sally, erica) ? creep
Call: (13) parent_child(_23350, sally) ? creep
Call: (14) father_child(_23350, sally) ? creep
Exit: (14) father_child(tom, sally) ? creep
Exit: (13) parent_child(tom, sally) ? creep
Call: (13) parent_child(tom, erica) ? creep
Call: (14) father_child(tom, erica) ? creep
Exit: (14) father_child(tom, erica) ? creep
Exit: (13) parent_child(tom, erica) ? creep
^ Call: (13) not(sally=erica) ? creep
^ Exit: (13) not(user:(sally=erica)) ? creep
Exit: (12) sibling(sally, erica) ? creep
My question: Can there be a program that, given *nothing more* than this trace, can figure out (synthesize, I suppose) a set of rules and facts which would yield this trace for the same query?
Further question: Since the first program would accept a single trace and output a single set of facts and rules, can there be a program which takes several of these outputs and generalizes them? So if we fed to this pipeline a representative enough sample of queries and traces of our program, we would be able to synthesize the program exactly?
Is this possible? Does it exist? Is this something that has been done before? Is there any research on it? (I'm sure someone much smarter than me has thought about it before, I'm surely not the only one who's thought of this)
2
u/steve_anunknown Oct 21 '26
This resemble passive automata learning. You may want to look into it.
1
u/tvmaly Oct 22 '26
You would miss the possible paths it didn’t take, so you could only synthesize what showed up in the trace
1
u/ka-splam 15d ago
ChatGPT can do it, so yes there's an existence proof.
Prompt: "Hi, given this SWI Prolog trace for the query ?- sibling(sally, erica). <paste> can you write a Prolog program / predicates / rules which shows the same trace for the same query? (I understand you cannot infer code which is not in the trace)"
Reply: 1. Reconstructed Prolog program
<three screens of explanation and commentary>
which I tried in SWISH and the trace looked the same to me.
1
u/brebs-prolog Oct 20 '26
I supppose it would have to run several times, by someone who already understands which different input data to feed it, to tell the difference between these:
... which is using soft cut.