# Term\_expansion/2 vs goal\_expansion/2

**URL:** <https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056>\
**Category:** Predicate\
**Created:** [October 16, 2020, 7:52pm UTC](https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056 "2020-10-16T19:52:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [October 16, 2020, 7:52pm UTC](https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056/1 "2020-10-16T19:52:35Z")

</div>

I have read the relevant documentation, but I still don’t understand the difference between these two predicates. Could anybody explain or point me to a source?  
Cheers/JC

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [October 16, 2020, 8:11pm UTC](https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056/2 "2020-10-16T20:11:49Z")

</div>

term\_expansion/2 is the original … it takes an entire fact or clause and expands it.

goal\_expansion/2 came later, to handle a common case when you want to expand individual goals (in the body of a clause) without having to deal with all control stuff ((,)/2, (;)/2, (-\>)/2, (+)/1, etc.).

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 17, 2020, 3:56pm UTC](https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056/3 "2020-10-17T15:56:34Z")

</div>

This is related information to goal\_expansion/2.

Once I tried to use goal\_expansion/2 to expand query goals which contain private macros, but it didn’t work. After asking, I was replied in SWI-Prolog mailing list from Jan W. several years ago, there was some limitation of goal\_expansion/2 at least for my purpose. Unfortunately I forgot detail. Anyway in stead of goal\_expansion/2, I had to use expand\_goal/4 and term\_expansion/2 to expand queries contains the private macros. Although the codes still works well, it would be good for us if goal\_expansion/2 works also for query.

Kuniaki Mukai

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [October 17, 2020, 4:26pm UTC](https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056/4 "2020-10-17T16:26:50Z")

</div>

There have been some improvements to term\_expansion/2, see [How to get to a fixed point with goal expansion?](https://swi-prolog.discourse.group/t/how-to-get-to-a-fixed-point-with-goal-expansion/334)

For this particular situation, wrap\_predicate/4 turned out to be the easiest (and best?) solution.

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 17, 2020, 4:59pm UTC](https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056/5 "2020-10-17T16:59:56Z")

</div>

It is good to hear on some improvements on term\_expansion/2, on which my pack pac (closure) library is heavily based. But I vaguely remember that my trouble was not related to such elegant “fixpoint” problem but merely to that my goal\_expansion/2 clauses were not activated. It was strange for me but also it seemed it was beyond my efforts. I will go back the trouble when I have time in the future.

Thaks for reply.

Kuniaki Mukai

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [October 17, 2020, 5:15pm UTC](https://swi-prolog.discourse.group/t/term-expansion-2-vs-goal-expansion-2/3056/6 "2020-10-17T17:15:11Z")

</div>

Thank you for all the useful suggestions!
