Home > Archive > Prolog > April 2007 > checking to see if a variable is infinite
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
checking to see if a variable is infinite
|
|
| Reuben Grinberg 2007-04-17, 10:03 pm |
| I have somehow managed to unify a variable so that it has become infinite:
T = f(f(f(f(f(f(f(f(f(f(...),_A),_A),_A),_A),_A),_A),_A),_A),_A)
When I try to walk this hierarchy, prolog goes into an infinite loop. Is
there a way to see if a variable is instantiated to something recursive
/ infinite?
Thanks,
Reuben
| |
| Benjamin Johnston 2007-04-17, 10:03 pm |
| > I have somehow managed to unify a variable so that it has become infinite:
>
> T = f(f(f(f(f(f(f(f(f(f(...),_A),_A),_A),_A),_A),_A),_A),_A),_A)
You can end up with something like that, by doing this:
T = f(T,_A)
> When I try to walk this hierarchy, prolog goes into an infinite loop. Is
> there a way to see if a variable is instantiated to something recursive
> / infinite?
Your prolog may have cyclic_term/1 to check for this:
i.e., cyclic_term(T) fails if T is infinite.
Also, you can explicitly prevent infinite terms from being created by
using unify_with_occurs_check/2.
A = f(B,C)
and
A = f(A,C)
normally both succeed
But if you replace the unification (=) with unify_with_occurs_check,
you'll get:
unify_with_occurs_check(A,f(B,C))
succeeds with the result that A = f(B,C)
but
unify_with_occurs_check(A,f(A,C))
fails
-Benjamin Johnston
| |
| Reuben Grinberg 2007-04-18, 4:03 am |
| Thanks -- that works perfectly.
Benjamin Johnston wrote:
>
> You can end up with something like that, by doing this:
>
> T = f(T,_A)
>
>
> Your prolog may have cyclic_term/1 to check for this:
>
> i.e., cyclic_term(T) fails if T is infinite.
>
> Also, you can explicitly prevent infinite terms from being created by
> using unify_with_occurs_check/2.
>
> A = f(B,C)
> and
> A = f(A,C)
> normally both succeed
>
> But if you replace the unification (=) with unify_with_occurs_check,
> you'll get:
>
> unify_with_occurs_check(A,f(B,C))
> succeeds with the result that A = f(B,C)
>
> but
> unify_with_occurs_check(A,f(A,C))
> fails
>
> -Benjamin Johnston
>
| |
|
|
|
|
|