Is this what you are trying to do?
The first line is the query ?- R = 42, N=R,write(N).
The next line 42
is the output of write(N)
The last line R = N, N = 42.
shows the MGU (Most General Unifier), (not sure if that is the correct terminology to use here)
The query is actually three predicates combined into a logic statement (goal), connected by the AND operator ,/2.
Note: In Prolog ;/2 is the OR operator.
?- =(R,42),=(N,R),write(N).
42
R = N, N = 42.
Taking that and using ,/2 as a prefix operator instead of an infix operator.
Note: To use ,/2 as a prefix operator need to quote the comma operator, i.e. ','
Note: This is only for demonstration purposes, it is not what most users would expect or have even seen.
?- ','(','(=(R,42),=(N,R)),write(N)).
42
R = N, N = 42.
For other ways to view the terms.
For the goals think abstract syntax tree (AST), for the terms think pretty print.
Note: The result of write_canonical/1 is not an MGU it is a linear representation of the expression with the operators in prefix form.
Note: The result of print_term/2 is not an MGU it is a pretty print of a term.
Ě€?- write_canonical((R = 42, N=R,write(N))).
','(=(A,42),','(=(B,A),write(B)))
true.
?- write_canonical(','(','(=(R,42),=(N,R)),write(N))).
','(','(=(A,42),=(B,A)),write(B))
true.
?- print_term((R = 42, N=R,write(N)),[]).
A=42,B = A,write(B)
true.
?- print_term(','(','(=(R,42),=(N,R)),write(N)),[]).
(A=42,B = A),write(B)
true.
Note: write_canonical/1 is nice for understanding the structure of goals and print_term/2 is nice for understanding and viewing complex terms.