#!/usr/local/bin/cadabra2 ex:=Q Q Q Q Q Q; substitute(ex, $Q->A+B, A->3, B->5$); ex:=Q Q Q Q Q Q; substitute(ex, $Q->A+B, A->3, B->5$, repeat=True); ex:=Q Q Q Q Q Q; converge(ex): substitute(_, $Q->A+B, A B->3$, repeat=True); distribute(_); ex:=Q Q Q Q Q Q; converge(ex): substitute(_, $Q->A+B, A B->3$, repeat=True) distribute(_) ;