Różnice
Różnice między wybraną wersją a wersją aktualną.
|
|
pl:prolog:pllib:occurs [2019/06/27 15:50] |
pl:prolog:pllib:occurs [2019/06/27 15:50] (aktualna) |
| ====== Occurs ====== |
| {{tag>terms}} |
| ===== Description ===== |
| Occurs in |
| |
| |
| **Source**: The Art of Prolog |
| ===== Download ===== |
| Program source code: {{occurs.pl}} |
| |
| ===== Listing ===== |
| <code prolog> |
| /* |
| |
| occurs_in(Sub,Term) :- |
| |
| Sub is a subterm of the (possibly non-ground term) Term. |
| |
| */ |
| |
| % a: Using == |
| |
| |
| |
| occurs_in(X,Term) :- subterm(Sub,Term), X == Sub. |
| |
| |
| |
| % b: Using freeze $$$$ freeze is not in Standard Prolog |
| |
| |
| |
| occurs_in(X,_) :- freeze(X,Xf), freeze(_,Termf), subterm(Xf,Termf). |
| |
| |
| |
| /* |
| |
| subterm(Sub,Term) :- Sub is a subterm of the ground term Term. |
| |
| */ |
| |
| subterm(Term,Term). |
| |
| subterm(Sub,Term) :- |
| |
| compound(Term), functor(Term,_,N), subterm(N,Sub,Term). |
| |
| |
| |
| subterm(N,Sub,Term) :- |
| |
| arg(N,Term,Arg), subterm(Sub,Arg). |
| |
| subterm(N,Sub,Term) :- |
| |
| N > 1, N1 is N-1, subterm(N1,Sub,Term). |
| |
| |
| |
| % Program |
| |
| 0.7 Occurs in |
| |
| </code> |
| ===== Comments ===== |
| |