# Example of prolog\_trace\_interception/4

**URL:** <https://swi-prolog.discourse.group/t/example-of-prolog-trace-interception-4/4458>\
**Category:** Nice to know\
**Created:** [September 17, 2021, 4:08pm UTC](https://swi-prolog.discourse.group/t/example-of-prolog-trace-interception-4/4458 "2021-09-17T16:08:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 17, 2021, 4:08pm UTC](https://swi-prolog.discourse.group/t/example-of-prolog-trace-interception-4/4458/1 "2021-09-17T16:08:29Z")

</div>

File: `prolog_trace_interception_examples.pl`

```prolog
user:prolog_trace_interception(Port, Frame, _Choice, continue) :-
    prolog_frame_attribute(Frame,goal,Goal),
    (
        trace_filter(Filter),
        Goal = Filter
    ->
        format('Port: ~w, Goal: ~w~n',[Port,Goal])
    ;
        true
    ),
    true.

:- dynamic trace_filter/1.
:- visible(+all).

fruit(apple).
fruit(banana).
fruit(cherry).
fruit(date).

vegetable(arugula).

female(pam).
female(liz).
female(pat).
female(ann).

male(tom).
male(bob).
male(jim).

parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
parent(bob,peter).
parent(peter,jim).

mother(X,Y) :-
    parent(X,Y),
    female(X).

grandmother(X,Z) :-
    mother(X,Y),
    parent(Y,Z).

example(1) :-
    asserta( trace_filter(prolog_trace_interception_examples:fruit(_)), Ref_1 ),
    trace,
    (
        fruit(_),
        fail,
        !
    ;
        notrace,
        nodebug
    ),
    erase(Ref_1).

example(2) :-
    asserta( trace_filter(prolog_trace_interception_examples:vegetable(_)), Ref_1 ),
    trace,
    (
        vegetable(_),
        fail,
        !
    ;
        notrace,
        nodebug
    ),
    erase(Ref_1).

example(3) :-
    asserta( trace_filter(prolog_trace_interception_examples:parent(_,_)), Ref_1 ),
    asserta( trace_filter(prolog_trace_interception_examples:female(_)), Ref_2 ),
    asserta( trace_filter(prolog_trace_interception_examples:mother(_,_)), Ref_3 ),
    trace,
    (
        mother(_,_),
        fail,
        !
    ;
        notrace,
        nodebug
    ),
    erase(Ref_1),
    erase(Ref_2),
    erase(Ref_3).

example(4) :-
    asserta( trace_filter(prolog_trace_interception_examples:parent(_,_)), Ref_1 ),
    asserta( trace_filter(prolog_trace_interception_examples:female(_)), Ref_2 ),
    asserta( trace_filter(prolog_trace_interception_examples:mother(_,_)), Ref_3 ),
    asserta( trace_filter(prolog_trace_interception_examples:grandmother(_,_)), Ref_4 ),
    trace,
    (
        grandmother(_,_),
        fail,
        !
    ;
        notrace,
        nodebug
    ),
    erase(Ref_1),
    erase(Ref_2),
    erase(Ref_3),
    erase(Ref_4).

```

Example run:

```prolog
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.28-20)
...

?- working_directory(_,'C:/Users/Groot').
true.

?- [prolog_trace_interception_examples].
true.

?- example(1).
Port: call, Goal: prolog_trace_interception_examples:fruit(_3532)
Port: unify, Goal: prolog_trace_interception_examples:fruit(apple)
Port: exit, Goal: prolog_trace_interception_examples:fruit(apple)
Port: redo(0), Goal: prolog_trace_interception_examples:fruit(_3538)
Port: unify, Goal: prolog_trace_interception_examples:fruit(banana)
Port: exit, Goal: prolog_trace_interception_examples:fruit(banana)
Port: redo(0), Goal: prolog_trace_interception_examples:fruit(_3538)
Port: unify, Goal: prolog_trace_interception_examples:fruit(cherry)
Port: exit, Goal: prolog_trace_interception_examples:fruit(cherry)
Port: redo(0), Goal: prolog_trace_interception_examples:fruit(_3538)
Port: unify, Goal: prolog_trace_interception_examples:fruit(date)
Port: exit, Goal: prolog_trace_interception_examples:fruit(date)
true.

?- example(2).
Port: call, Goal: prolog_trace_interception_examples:vegetable(_7130)
Port: unify, Goal: prolog_trace_interception_examples:vegetable(arugula)
Port: exit, Goal: prolog_trace_interception_examples:vegetable(arugula)
true.

?- example(3).
Port: call, Goal: prolog_trace_interception_examples:mother(_9362,_9364)
Port: unify, Goal: prolog_trace_interception_examples:mother(_9362,_9364)
Port: call, Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(pam,bob)
Port: exit, Goal: prolog_trace_interception_examples:parent(pam,bob)
Port: call, Goal: prolog_trace_interception_examples:female(pam)
Port: unify, Goal: prolog_trace_interception_examples:female(pam)
Port: exit, Goal: prolog_trace_interception_examples:female(pam)
Port: exit, Goal: prolog_trace_interception_examples:mother(pam,bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(tom,bob)
Port: exit, Goal: prolog_trace_interception_examples:parent(tom,bob)
Port: call, Goal: prolog_trace_interception_examples:female(tom)
Port: fail, Goal: prolog_trace_interception_examples:female(tom)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(tom,liz)
Port: exit, Goal: prolog_trace_interception_examples:parent(tom,liz)
Port: call, Goal: prolog_trace_interception_examples:female(tom)
Port: fail, Goal: prolog_trace_interception_examples:female(tom)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,ann)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,ann)
Port: call, Goal: prolog_trace_interception_examples:female(bob)
Port: fail, Goal: prolog_trace_interception_examples:female(bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,pat)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,pat)
Port: call, Goal: prolog_trace_interception_examples:female(bob)
Port: fail, Goal: prolog_trace_interception_examples:female(bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(pat,jim)
Port: exit, Goal: prolog_trace_interception_examples:parent(pat,jim)
Port: call, Goal: prolog_trace_interception_examples:female(pat)
Port: unify, Goal: prolog_trace_interception_examples:female(pat)
Port: exit, Goal: prolog_trace_interception_examples:female(pat)
Port: exit, Goal: prolog_trace_interception_examples:mother(pat,jim)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: call, Goal: prolog_trace_interception_examples:female(bob)
Port: fail, Goal: prolog_trace_interception_examples:female(bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_9356,_9358)
Port: unify, Goal: prolog_trace_interception_examples:parent(peter,jim)
Port: exit, Goal: prolog_trace_interception_examples:parent(peter,jim)
Port: call, Goal: prolog_trace_interception_examples:female(peter)
Port: fail, Goal: prolog_trace_interception_examples:female(peter)
Port: fail, Goal: prolog_trace_interception_examples:mother(_9362,_9364)
true.

?- example(4).
Port: call, Goal: prolog_trace_interception_examples:grandmother(_11612,_11614)
Port: unify, Goal: prolog_trace_interception_examples:grandmother(_11612,_11614)
Port: call, Goal: prolog_trace_interception_examples:mother(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:mother(_11606,_11608)
Port: call, Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(pam,bob)
Port: exit, Goal: prolog_trace_interception_examples:parent(pam,bob)
Port: call, Goal: prolog_trace_interception_examples:female(pam)
Port: unify, Goal: prolog_trace_interception_examples:female(pam)
Port: exit, Goal: prolog_trace_interception_examples:female(pam)
Port: exit, Goal: prolog_trace_interception_examples:mother(pam,bob)
Port: call, Goal: prolog_trace_interception_examples:parent(bob,_11610)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,ann)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,ann)
Port: exit, Goal: prolog_trace_interception_examples:grandmother(pam,ann)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(bob,_11610)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,pat)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,pat)
Port: exit, Goal: prolog_trace_interception_examples:grandmother(pam,pat)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(bob,_11610)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: exit, Goal: prolog_trace_interception_examples:grandmother(pam,peter)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(tom,bob)
Port: exit, Goal: prolog_trace_interception_examples:parent(tom,bob)
Port: call, Goal: prolog_trace_interception_examples:female(tom)
Port: fail, Goal: prolog_trace_interception_examples:female(tom)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(tom,liz)
Port: exit, Goal: prolog_trace_interception_examples:parent(tom,liz)
Port: call, Goal: prolog_trace_interception_examples:female(tom)
Port: fail, Goal: prolog_trace_interception_examples:female(tom)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,ann)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,ann)
Port: call, Goal: prolog_trace_interception_examples:female(bob)
Port: fail, Goal: prolog_trace_interception_examples:female(bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,pat)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,pat)
Port: call, Goal: prolog_trace_interception_examples:female(bob)
Port: fail, Goal: prolog_trace_interception_examples:female(bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(pat,jim)
Port: exit, Goal: prolog_trace_interception_examples:parent(pat,jim)
Port: call, Goal: prolog_trace_interception_examples:female(pat)
Port: unify, Goal: prolog_trace_interception_examples:female(pat)
Port: exit, Goal: prolog_trace_interception_examples:female(pat)
Port: exit, Goal: prolog_trace_interception_examples:mother(pat,jim)
Port: call, Goal: prolog_trace_interception_examples:parent(jim,_11610)
Port: fail, Goal: prolog_trace_interception_examples:parent(jim,_11610)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: call, Goal: prolog_trace_interception_examples:female(bob)
Port: fail, Goal: prolog_trace_interception_examples:female(bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(pat,jim)
Port: exit, Goal: prolog_trace_interception_examples:parent(pat,jim)
Port: call, Goal: prolog_trace_interception_examples:female(pat)
Port: unify, Goal: prolog_trace_interception_examples:female(pat)
Port: exit, Goal: prolog_trace_interception_examples:female(pat)
Port: exit, Goal: prolog_trace_interception_examples:mother(pat,jim)
Port: call, Goal: prolog_trace_interception_examples:parent(jim,_11610)
Port: fail, Goal: prolog_trace_interception_examples:parent(jim,_11610)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: exit, Goal: prolog_trace_interception_examples:parent(bob,peter)
Port: call, Goal: prolog_trace_interception_examples:female(bob)
Port: fail, Goal: prolog_trace_interception_examples:female(bob)
Port: redo(0), Goal: prolog_trace_interception_examples:parent(_11606,_11608)
Port: unify, Goal: prolog_trace_interception_examples:parent(peter,jim)
Port: exit, Goal: prolog_trace_interception_examples:parent(peter,jim)
Port: call, Goal: prolog_trace_interception_examples:female(peter)
Port: fail, Goal: prolog_trace_interception_examples:female(peter)
Port: fail, Goal: prolog_trace_interception_examples:mother(_11606,_11608)
Port: fail, Goal: prolog_trace_interception_examples:grandmother(_11612,_11614)
true.

```
