# Term rewriting / expansion...tutorials, other references?

**URL:** <https://swi-prolog.discourse.group/t/term-rewriting-expansion-tutorials-other-references/2201>\
**Category:** General\
**Created:** [April 20, 2020, 10:11am UTC](https://swi-prolog.discourse.group/t/term-rewriting-expansion-tutorials-other-references/2201 "2020-04-20T10:11:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 20, 2020, 10:11am UTC](https://swi-prolog.discourse.group/t/term-rewriting-expansion-tutorials-other-references/2201/1 "2020-04-20T10:11:54Z")

</div>

Hi, I am almost done with the basics of my AST, I have implemented the general recurisiveness of the source orm and three of the 84 reserved words!

I am wondering of term\_expansion/2 et al. can be of some assistance in cutting down the boiler plate, making things easier to read and reason about.

My basic n00b question is this: what is the advantage of using term\_expansion of just calling a subroutine predicate apart from the simple fact that term\_expansion is compile time and subroutines are run time.

Is that the only benefit really?

I have read all of the posts on this forum, created some simple expansions of my own with debug statements and mostly it works but sometimes I get odd errors:

```prolog

term_expansion(X, Out) :-
    ( functor(X, foo, 2)
    %
    % it's a foo, let's muck about with it
    %
    -> debug(eric, 'fiddle: ~p~n',[X]),
        Out = (
            foo(X,Y) :-
                Y is X+1,
                format('adding ~p and 1 is ~p',[X,Y])
        )

    %% Leave it alone
    ; Out = X
    ).

foo(_X,_Y).

foo(_Z).

% ?- foo(10,Out).
% ?- listing(foo).
% ?- debug(eric).

```

at the emacs prolog session I get this message:

```prolog
| % fiddle: foo(_6044,_6046)

ERROR: /tmp/prolcomp6531Hph.pl:28:
	Arithmetic: `foo(_6568,_6590)' is not a function
true.
?- 

```

I can see my debug statement but right now I am floundering due to a complete lack of knowledge! I made the vars anonymous because otherwise it was complaining about singleton variables.

Thanks,  
Sean.

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [April 20, 2020, 10:36am UTC](https://swi-prolog.discourse.group/t/term-rewriting-expansion-tutorials-other-references/2201/2 "2020-04-20T10:36:24Z")

</div>

You’re using the variable `X` for both the term to be expanded **and** the first argument of `foo/2`. Simply use different variables.

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 20, 2020, 10:53am UTC](https://swi-prolog.discourse.group/t/term-rewriting-expansion-tutorials-other-references/2201/3 "2020-04-20T10:53:57Z")

</div>

🤦‍♂️ …yes…painfully clear now! 😑
