You likely know about units conversion, as in "convert 4 feet into centimeters." This is a lot like the routing lesson, only instead of going from city A to B, we want to get from unit A to unit B.
To begin, the "data" we'll need for this will be conversion facts, like the fact that there's 12 inches in 1 foot, or 100 centimeters in 1 meter. Our plan is to simply give these facts to Prolog, then be able to ask it something like convert(foot,centimeter). With this, it should show us how to convert for example 4 ft into cm.
After the facts, the core conversion clause is called convert(To,From,Steps), which will work to show us how to convert a quantity in units of To into a quantity of From, putting the textual steps into Steps.
Now you try. Run the code as is, to convert inches into km. But defintely try your own conversions and add as many conversion facts as you need!
Type your code here:
See your results here:
The code:
We need the usual "symmetry" clause which we call direct. This ensures if we have a fact in that knows how to directly convert unit X to unit Y, it can also be used to convert Y into X.
The convert clause gets everthing going. It takes in the originating unit in From to works to convert it to the unit of To, while putting the plan to do so in How.
Note that How will always be a plan that is workable based on the facts given (i.e. your knowledge of units conversion). In other words, each
step given in How will be a direct unit-to-unit conversion based on a conversion in your fact facts statements.
There are two path clauses that look for a solution through the fact information. The first, path(X,X,Trail,[X|Trail]) one is the boundary condition. It says "if you end up going from unit X to X (the same unit), then you must either be 1) done or 2) you hit a snag, so finish. Add the units we're stalled on to the Trail information and stop." (See more below on this; it's not obvious why this happens.)
The second path clause is path(X,Y,Trail,How0). It says "To convert from unit X to Y, see what it's possible to convert X to via the direct(X,Z) call. If you find a Z that X can be converted to, make sure we haven't been to that unit Z before, then proceed and try to convert Z to Y, while also adding the the fact that we've just been to unit X into the trail." (Recall Y is always the desired final unit.)
It's not obvious why the boundary condition would ever get triggered. Why afterall, would Prolog try to find a path between a unit and itself in the path(X,X,...) clause? It works like this. If you look at the path(X,Y,...) clause, you'll note that Y, which is the eventual unit we seek, is always held in the Y position. It's a fixed constant throughout. Look for example at path(X,Y,Trail,How0) :- ... path(Z,Y,[X|Trail],How0). See how the Y is preserved?
What happens when the code runs, is that there will always be a final conversion from Z to Y (the last step in any successful conversion). As an example, if you convert inches to km, it'll proceed like this: 1) inch to cm 2) cm to meter 3) meter to km. It's #3, when Z becomes a km, that we'll get a path(km,km,[X|Trail],How0)., which will trigger the boundary condition.
Share your code
Show a friend, family member, or teacher what you've done!