prologstack-overflowgnu-prolog# I wrote a program to handle unit conversions in Prolog, but I always get stack-overflows: Do you know why?

The problem continues regardless of refactoring, but it changes, if I switch line 6 and 7 I get different results? How could that even be possible?

If relevent this is gnuprolog on a MacOS Monterey (version 12.6.2) device.

Here is the relevent code:

```
conversion(meters, 82/25, feet).
conversion(feet, 12/1, in ).
conversion(hr, 60/1, min ).
conversion(min, 60/1, sec ).
conversion(Unit1, X/Y, Unit2) :- conversion(Unit2, Y/X, Unit1), !.
conversion(Unit1, Num/Den, Unit3) :- conversion(Unit1, X/Y, Unit2), conversion(Unit2, A/B, Unit3), Num is X * A, Den is Y*B.
```

Whever I enter a question that requires more then a single *inversion* (as in the second to last rule) I get an error. So,

```
conversion(meters,X,in).
conversion(hr,X,sec).
conversion(in, X, meters).
```

Cause,

```
Fatal Error: local stack overflow (size: 16384 Kb, reached: 16384 Kb, environment variable used: LOCALSZ)
```

But

```
conversion(min,X,hr).
conversion(feet,X,meters).
```

Produce the expected results. Any wisdom would be greatly appreciated!

Solution

The following appears to work, thanks @brebs!

```
conversion(meters, 82/25,feet ).
conversion(feet, 12/1, inches ).
conversion(hours, 60/1, minutes).
conversion(minutes,60/1, seconds).
walk(Unit1, X/Y, Unit3, Vs) :-
conversion(Unit1, X/Y, Unit3);
conversion(Unit3, Y/X, Unit1);
\+ member(Unit2, Vs),
walk(Unit1, A/B, Unit2, [Unit2|Vs]),
walk(Unit2, C/D , Unit3, [Unit2|Vs]),
N is A * C, M is B * D,
gcd(N,M,Z),
X is N // Z,
Y is M // Z.
ratio(Unit1, X/Y, Unit2) :- walk(Unit1, X/Y, Unit2, []).
gcd(X, 0, X):- !.
gcd(0, X, X):- !.
gcd(X, Y, D):- X =< Y, !, Z is Y - X, gcd(X, Z, D).
gcd(X, Y, D):- gcd(Y, X, D).
```

- Find the divisors of a number using Prolog by using recursion
- Prolog type definition in swi-prolog
- In SWI-Prolog, how can I find a solution that maximises or minimises some atom?
- Making prolog predicates deterministic
- Up-to-date Prolog implementation benchmarks?
- Creating a list of interleaved elements: Prolog
- What are the (perceived) shortcomings of the ISO standard for Prolog modules (ISO/IEC 13211-2)?
- Are HiLog terms still useful in modern Prolog?
- Prolog - replacing list elements
- Trouble obtaining a specific answer to a query in Prolog
- Prolog lists: combination of comma and |
- Solving a logical puzzle with Prolog gives a wrong answer
- How to identify valid bodies and convertible terms
- Find all subsequences of a list that cover the whole list in prolog
- How to flatten nested Prolog statements into a plain Horn clause
- Solving Caliban problems with prolog
- Why is there an error for the query: "query(member(one, [one,two,three]))." in Problog?
- PROLOG Make a list of words from big text file
- CLP(FD): solution number count speedup
- Why do i get : uncaught exception: error(existence_error(procedure,foggy/0),top_level/0)?
- Prolog falls into an infinite loop
- Problem with insert function into an organized list in Prolog
- Prolog Predicate for positive and negative integers
- Understanding recursive rules in Prolog
- How to collect maximal non-overlapping ascending/descending prefixes of a random sequence of numbers
- Prolog - first list is sublist of second list?
- Prolog: X is the grandfather of Y
- Why does this Prolog program not terminate?
- Blocks-World Problem in Prolog keeps oscillating between the same two states
- Moving elements in a list-of-lists in prolog