swipl [treewalk]. true. [trace] ?- treewalk(1,[1]). Call: (7) treewalk(1, [1]) ? creep Call: (8) integer(1) ? creep Exit: (8) integer(1) ? creep Exit: (7) treewalk(1, [1]) ? creep true. //tested first rule [trace] ?- treewalk(add(1,2),X). //test of add rule Call: (7) treewalk(add(1, 2), _G1789) ? creep Call: (8) integer(add(1, 2)) ? creep Fail: (8) integer(add(1, 2)) ? creep Redo: (7) treewalk(add(1, 2), _G1789) ? creep Call: (8) treewalk(1, _G1868) ? creep Call: (9) integer(1) ? creep Exit: (9) integer(1) ? creep Exit: (8) treewalk(1, [1]) ? creep //builds left arg part of list Call: (8) treewalk(2, _G1871) ? creep Call: (9) integer(2) ? creep Exit: (9) integer(2) ? creep Exit: (8) treewalk(2, [2]) ? creep //right arg part of list Call: (8) append([1], [+], _G1878) ? creep Call: (9) append([], [+], _G1870) ? creep Exit: (9) append([], [+], [+]) ? creep Exit: (8) append([1], [+], [1, +]) ? creep Call: (8) append([1, +], [2], _G1789) ? creep //appends left arg to right arg w + inbetween Call: (9) append([+], [2], _G1873) ? creep Call: (10) append([], [2], _G1876) ? creep Exit: (10) append([], [2], [2]) ? creep Exit: (9) append([+], [2], [+, 2]) ? creep Exit: (8) append([1, +], [2], [1, +, 2]) ? creep Exit: (7) treewalk(add(1, 2), [1, +, 2]) ? creep X = [1, +, 2]. //correct answer -- notice no precedence here [trace] ?- treewalk(add(times(2,3),4),X). Call: (7) treewalk(add(times(2, 3), 4), _G1792) ? creep Call: (8) integer(add(times(2, 3), 4)) ? creep Fail: (8) integer(add(times(2, 3), 4)) ? creep Redo: (7) treewalk(add(times(2, 3), 4), _G1792) ? creep Call: (8) treewalk(times(2, 3), _G1874) ? creep Call: (9) integer(times(2, 3)) ? creep Fail: (9) integer(times(2, 3)) ? creep Redo: (8) treewalk(times(2, 3), _G1874) ? creep Call: (9) treewalk(2, _G1874) ? creep Call: (10) integer(2) ? creep Exit: (10) integer(2) ? creep Exit: (9) treewalk(2, [2]) ? creep Call: (9) treewalk(3, _G1877) ? creep Call: (10) integer(3) ? creep Exit: (10) integer(3) ? creep Exit: (9) treewalk(3, [3]) ? creep Call: (9) append([2], [*], _G1884) ? creep Call: (10) append([], [*], _G1876) ? creep Exit: (10) append([], [*], [*]) ? creep Exit: (9) append([2], [*], [2, *]) ? creep Call: (9) append([2, *], [3], _G1887) ? creep Call: (10) append([*], [3], _G1879) ? creep Call: (11) append([], [3], _G1882) ? creep Exit: (11) append([], [3], [3]) ? creep Exit: (10) append([*], [3], [*, 3]) ? creep Exit: (9) append([2, *], [3], [2, *, 3]) ? creep Exit: (8) treewalk(times(2, 3), [2, *, 3]) ? creep Call: (8) treewalk(4, _G1892) ? creep Call: (9) integer(4) ? creep Exit: (9) integer(4) ? creep Exit: (8) treewalk(4, [4]) ? creep Call: (8) append([2, *, 3], [+], _G1899) ? creep Call: (9) append([*, 3], [+], _G1891) ? creep Call: (10) append([3], [+], _G1894) ? creep Call: (11) append([], [+], _G1897) ? creep Exit: (11) append([], [+], [+]) ? creep Exit: (10) append([3], [+], [3, +]) ? creep Exit: (9) append([*, 3], [+], [*, 3, +]) ? creep Exit: (8) append([2, *, 3], [+], [2, *, 3, +]) ? creep Call: (8) append([2, *, 3, +], [4], _G1792) ? creep Call: (9) append([*, 3, +], [4], _G1900) ? creep Call: (10) append([3, +], [4], _G1903) ? creep Call: (11) append([+], [4], _G1906) ? creep Call: (12) append([], [4], _G1909) ? creep Exit: (12) append([], [4], [4]) ? creep Exit: (11) append([+], [4], [+, 4]) ? creep Exit: (10) append([3, +], [4], [3, +, 4]) ? creep Exit: (9) append([*, 3, +], [4], [*, 3, +, 4]) ? creep Exit: (8) append([2, *, 3, +], [4], [2, *, 3, +, 4]) ? creep Exit: (7) treewalk(add(times(2, 3), 4), [2, *, 3, +, 4]) ? creep X = [2, *, 3, +, 4]. [trace] ?-