shithub: pprolog

Download patch

ref: 6c539b4da8be5dd00a6685d744544275774128d0
parent: 6b3d59a3256a292b582e4313325ca14154211671
author: Peter Mikkelsen <[email protected]>
date: Mon Jul 19 10:26:18 EDT 2021

Try to print A = B instead of A = _X, B = _X when two variables are equal

--- a/repl.pl
+++ b/repl.pl
@@ -35,8 +35,9 @@
 	!,
 	abolish(found_a_solution/0).
 
-eval_and_print(Goal, Vars, Choicecount) :-
+eval_and_print(Goal, Vars0, Choicecount) :-
 	user:call(Goal),
+	rewrite_equations(Vars0, Vars),
 	abolish(found_a_solution/0),
 	asserta(found_a_solution :- !),
 	'$choicestack_size'(ChoicecountNew),
@@ -58,6 +59,26 @@
 	write('false.'),
 	nl.
 
+rewrite_equations(Eqs0, Eqs) :-
+	apply_bindings(Eqs0),
+	remove_identities(Eqs0, Eqs).
+
+apply_bindings([]).
+apply_bindings([A = B|Rest]) :-
+	( var(B)
+	-> call(B = A)
+	; true
+	),
+	apply_bindings(Rest).
+
+remove_identities([], []).
+remove_identities([A = B|Rest0], Result) :-
+	remove_identities(Rest0, Rest),
+	( A == B
+	-> Result = Rest
+	; Result = [A = B|Rest]
+	).
+
 write_state(end) :- write('.'), nl.
 write_state(more).
 
@@ -68,7 +89,7 @@
 write_bindings([Var = Val|Bs]) :-
 	write(Var),
 	write(' = '),
-	writeq(Val),
+	write(Val),
 	( Bs = []
 	-> true
 	; put_char(','), nl