add backtracking function
move apply-rule to rules