big refactoring to share the same term (and index) representation
for the distinct datalog algorithms; should also share AST repr;
prepare for more powerful term repr (records etc.)