3 ms·
Wow! This is very elegant and general Prolog code, thank you for sharing it, and for your kind words! Regarding the handling of ">" and "<", one possible alter
by triska 8y ago
Wow! This is very elegant and general Prolog code, thank you for sharing it, and for your kind words!
Regarding the handling of ">" and "<", one possible alternative is to use constraint reification to reason about the truth values of arithmetic relations.
For instance, we can equivalently describe the semantics of ">" as:
func(>, [A,B|S], [T|S]) :- B #> A #<==> R, r_truth(R, T).
r_truth(0, false).
r_truth(1, true).
As in the context of meta-interpreters, reification also here means to "make it a thing" (from Latin res, rei f.: "thing", "matter") - in this case, to reflect the constraint's meta-level truth in a Boolean value on the object-level.
- carapace 8y ago> Wow! This is very elegant and general Prolog code, thank you for sharing it, and for your kind words! Thank you! You're very kind but the credit goes to Manfred von Thun, he came up with Joy. And your book has helped a lot. > Regarding the handling of ">" and "<", one possible alternative is to use constraint reification to reason about the truth values of arithmetic relations. This is exactly what I wanted! Thank you. I'm just starting out with Prolog so it's difficult to know what to reach for, I spent twenty minutes looking for docs on "true" and "false" before realizing they weren't built in! Heh. ?- thun([dup, 0, >, [+], [-], branch], Si, So). Si = [_9700, _9706|_9708], So = [_9724|_9708], _9700 in 1..sup, _9724+_9700#=_9706 ; Si = [_9800, _9806|_9808], So = [_9824|_9808], _9800 in inf..0, _9800+_9806#=_9824 ; false. Awesome. (Both solutions, ta-daaa!) Based on what you recommend about avoiding "defaulty" representations I've added a literal predicate: thun( [Lit|E], Si, So) :- literal(Lit), thun(E, [Lit|Si], So). literal([]). literal([_|_]). literal(I) :- number(I). literal(true). literal(false). Right now I'm reading up on compiling and code generation, and your "Thinking in States" chapter is a big help.