TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Solving a Dungeons and Dragons riddle using Prolog

127 pointsby _xgwover 2 years ago

12 comments

IncRndover 2 years ago
While it&#x27;s not quite the same, we can create a short makefile.<p><pre><code> vixen: rudolph prancer dasher cupid blitzen echo vixen dancer : vixen donder blitzen rudolph cupid dasher echo dancer comet: vixen cupid prancer rudolph echo comet donder: comet vixen dasher prancer cupid blitzen rudolph echo donder cupid: prancer echo cupid blitzen: cupid dasher echo blitzen rudolph: cupid prancer echo rudolph dasher: rudolph prancer echo dasher prancer: ; echo prancer order: vixen dancer comet donder cupid blitzen rudolph dasher prancer echo Get in Line </code></pre> Then: make -n order<p>outputs:<p><pre><code> echo prancer echo cupid echo rudolph echo dasher echo blitzen echo vixen echo comet echo donder echo dancer echo Get in Line </code></pre> Which, for me, is:<p><pre><code> real 0m0.015s user 0m0.003s sys 0m0.006s</code></pre>
评论 #34285352 未加载
triskaover 2 years ago
Very nice!<p>This solution uses the library predicate list_to_set&#x2F;2, relating a (known) list Ls0 of elements to a list Ls <i>without duplicates</i>, where the elements occur in the same order in which they first appear in Ls0. I think it is interesting to consider how such a relation can be described in Prolog, and also <i>how efficient</i> it can be.<p>An immediate solution suggests itself, considering the elements of Ls0 in the order they appear, and keeping track of the elements that have already been &quot;seen&quot;. If an element is encountered that has already been seen, ignore it, otherwise it is part of the list Ls we want to describe. We can use a list to keep track of elements that have already been encountered:<p><pre><code> list_to_set(Ls0, Ls) :- phrase(firsts(Ls0, []), Ls). firsts([], _) --&gt; []. firsts([L|Ls], Seen) --&gt; ( { member(L, Seen) } -&gt; [] ; [L] ), firsts(Ls, [L|Seen]). </code></pre> This works correctly if the list is ground:<p><pre><code> ?- list_to_set(&quot;Corvus corax&quot;, Ls). Ls = &quot;Corvus cax&quot;. </code></pre> Yet, this solution has a very severe drawback: It is worst-case <i>quadratic</i> in the number of elements, and thus not usable for long lists:<p><pre><code> ?- length(_, E), E #&gt; 10, N #= 2^E, numlist(1, N, Ls0), time(list_to_set(Ls0, Ls)). </code></pre> yielding:<p><pre><code> % CPU time: 0.222s E = 11, N = 2048, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; % CPU time: 0.880s E = 12, N = 4096, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; % CPU time: 3.518s E = 13, N = 8192, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; ... . </code></pre> So, how to improve it? Well, it may be tempting to use for example a <i>hash</i> or an AVL tree to keep track of the &quot;seen&quot; elements, so that it can be more efficiently decided whether an element has already been encountered. And indeed, that is easy to do, and reduces the runtime considerably.<p>For example, using the commonly available library(assoc) for AVL trees, providing <i>O</i>(log(<i>N</i>)) lookup:<p><pre><code> list_to_set(Ls0, Ls) :- empty_assoc(A0), phrase(firsts(Ls0, A0), Ls). firsts([], _) --&gt; []. firsts([L|Ls], A0) --&gt; ( { get_assoc(L, A0, _) } -&gt; [] ; [L] ), { put_assoc(L, A0, t, A) }, firsts(Ls, A). </code></pre> With this simple change, we get for the query above:<p><pre><code> % CPU time: 0.034s E = 11, N = 2048, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; % CPU time: 0.070s E = 12, N = 4096, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; % CPU time: 0.155s E = 13, N = 8192, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; ... . </code></pre> The most interesting part is that we can do significantly better, by leveraging Prolog&#x27;s <i>logic variables</i> to propagate the information whether elements have already been encountered, yielding a very efficient solution where <i>sorting</i> the list Ls0 (or rather: the list of pairs LVs0, where we associate with each element of Ls0 a logic variable that can be used to propagate information by <i>unifying</i> it with other variables and more concrete terms) dominates the asymptotic complexity:<p><pre><code> list_to_set(Ls0, Ls) :- maplist(with_var, Ls0, LVs0), keysort(LVs0, LVs), same_elements(LVs), pick_firsts(LVs0, Ls). pick_firsts([], []). pick_firsts([E-V|EVs], Fs0) :- ( V == visited -&gt; Fs0 = Fs ; V = visited, Fs0 = [E|Fs] ), pick_firsts(EVs, Fs). with_var(E, E-_). same_elements([]). same_elements([EV|EVs]) :- foldl(unify_same, EVs, EV, _). unify_same(E-V, Prev-Var, E-V) :- ( Prev == E -&gt; Var = V ; true ). </code></pre> We now get significantly improved performance:<p><pre><code> % CPU time: 0.003s E = 11, N = 2048, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; % CPU time: 0.006s E = 12, N = 4096, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; % CPU time: 0.013s E = 13, N = 8192, Ls0 = [1,2,3,4,5,...], Ls = [1,2,3,4,5,...] ; ... . </code></pre> And this is indeed how list_to_set&#x2F;2 is implemented for example in Scryer Prolog&#x27;s library(lists):<p><a href="https:&#x2F;&#x2F;github.com&#x2F;mthom&#x2F;scryer-prolog&#x2F;blob&#x2F;fd19128530f68c4623e9df5d0eab6042f62ac8c6&#x2F;src&#x2F;lib&#x2F;lists.pl#L324">https:&#x2F;&#x2F;github.com&#x2F;mthom&#x2F;scryer-prolog&#x2F;blob&#x2F;fd19128530f68c46...</a>
评论 #34225403 未加载
soveranover 2 years ago
For an alternative solution, describe the directed graph by listing the nodes and using whitespace to represent the arcs:<p><pre><code> $ cat riddle Vixen Rudolph Vixen Prancer Vixen Dasher Dancer Vixen Comet Vixen ... Vixen Dasher </code></pre> Then use tsort:<p><pre><code> $ tsort riddle Dancer Donder Comet Vixen Blitzen Dasher Rudolph Cupid Prancer</code></pre>
评论 #34230925 未加载
YeGoblynQueenneover 2 years ago
The raindeer in the riddle are in a total ordering, with each following one other, like a linked list. That means we can just... sort them.<p>We could do that by hand-rolling a sorting algorithm with a custom comparison. Or, if we want to leave time for breakfast, there&#x27;s SWI-Prolog&#x27;s predsort&#x2F;3 that takes as an argument a custom ordering predicate, and then sorts a list of arbitrary Prolog terms according to that ordering.<p>For example, I define raindeer_order&#x2F;2 as an ordering predicate, reusing follows&#x2F;2 from the article above, like this:<p><pre><code> raindeer_order(&gt;,R1,R2):- once(follows(R1,R2)). raindeer_order(&lt;,R1,R2):- once(follows(R2,R1)). raindeer_order(=,R,R). </code></pre> If you squint a bit you&#x27;ll notice the polarity of &quot;&lt;&quot; and &quot;&gt;&quot; is inverted. That&#x27;s because follows&#x2F;2 is an inverse order.<p>Now we can find all the raindeer and sort them:<p><pre><code> ordered_raindeer(Rs_):- setof(R1 ,R2^( is_behind(R1,R2) ; is_behind(R2,R1) ) ,Rs) ,predsort(raindeer_order,Rs,Rs_). </code></pre> And, at the command line:<p><pre><code> ?- ordered_raindeer(Rs). Rs = [prancer, cupid, rudolph, dasher, blitzen, vixen, comet, donder, dancer] ; false. </code></pre> Runs in O(log(n)) :P<p>(Edit: I think it&#x27;s n log n actually: follows&#x2F;2 might have to run the length of the list to compare two reindeer.)
评论 #34238743 未加载
mLubyover 2 years ago
Very nice. I just solved it with GraphViz: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;mLuby&#x2F;d184c08c507fa03292c72acb38a146ff" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;mLuby&#x2F;d184c08c507fa03292c72acb38a146...</a>
评论 #34226122 未加载
评论 #34226184 未加载
colandermanover 2 years ago
You can do this without the intermediate step of generating all permutations simply like so:<p><pre><code> order([]). order([_]). order([X,Y|L]) :- follows(Y, X), order([Y|L]). ?- length(L, 9), order(L). L = [prancer, cupid, rudolph, dasher, blitzen, vixen, comet, donder, dancer] . </code></pre> This is likely more efficient as we&#x27;re cutting short the generation of most permutations.<p>Or you can use CPL(FD) as suggested by @Avshalom below, though more heavyweight this is likely more efficient still.<p>The most efficient though is simply to use a topological sort algorithm, which will run in linear time, unlike any of these solutions (some of which are exponential). SWI Prolog has this built-in:<p><pre><code> ?- findall(X-Y, is_behind(Y, X), Edges), vertices_edges_to_ugraph([], Edges, UG), top_sort(UG, L). L = [prancer, cupid, rudolph, dasher, blitzen, vixen, comet, donder, dancer].</code></pre>
评论 #34226869 未加载
评论 #34226478 未加载
评论 #34229877 未加载
SloopJonover 2 years ago
I got the solution with a straightforward set of assertions in Z3. It was a lot of typing, and I wonder if there&#x27;s a more succinct way to do this than the following:<p><pre><code> (declare-const blitzen Int) (declare-const comet Int) ... (assert (and ; lower bound for readability (1 is front) (&gt; blitzen 0) (&gt; comet 0) ... ; upper bound for readability (9 is rear) (&lt; blitzen 10) (&lt; comet 10) ... ; clues from the puzzle (&gt; vixen rudolph) ... (&lt; dasher vixen)))</code></pre>
shagieover 2 years ago
I&#x27;m curious how it would deal with the self-referential aptitude test (original text: <a href="https:&#x2F;&#x2F;faculty.uml.edu&#x2F;&#x2F;jpropp&#x2F;srat.html" rel="nofollow">https:&#x2F;&#x2F;faculty.uml.edu&#x2F;&#x2F;jpropp&#x2F;srat.html</a> -- web version: <a href="http:&#x2F;&#x2F;www.drunkmenworkhere.org&#x2F;170" rel="nofollow">http:&#x2F;&#x2F;www.drunkmenworkhere.org&#x2F;170</a> )<p>&gt; 1. The first question whose answer is B is question ...
Labo333over 2 years ago
From a more algorithmic point of view, this is exactly the task of topological sorting [1].<p>And it runs linearly in the number of edges!<p>I expect Prolog to be slower for large and hard inputs. But Makefiles solve exactly that!<p>[1]: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Topological_sorting" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Topological_sorting</a>
Avshalomover 2 years ago
Now I immediately turned to library(clpfd). Something along the lines of:<p><pre><code> Names = [rudolph, dancer... Vars = [Rudolph, Dancer... Vars ins 1..9, all_different(Vars), Rudolph #&gt; Dancer, ... ... pairs_keys_values(P,Vars,Names), keysort(P,S), write(S).</code></pre>
评论 #34234286 未加载
tobinfrickeover 2 years ago
<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Topological_sorting" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Topological_sorting</a>
kwon-youngover 2 years ago
Prolog was made to parse text, so shouldn&#x27;t we derive the constraints from the text itself with a DCG ?<p><pre><code> :- set_prolog_flag(double_quotes, codes). text(&quot;Vixen should be behind Rudolph, Prancer and Dasher, whilst Vixen should be in front of Dancer and Comet. Dancer should be behind Donder, Blitzen and Rudolph. Comet should be behind Cupid, Prancer and Rudolph. Donder should be behind Comet, Vixen, Dasher, Prancer and Cupid. Cupid should be in front of Comet, Blitzen, Vixen, Dancer and Rudolph. Prancer should be in front of Blitzen, Donder and Cupid. Blitzen should be behind Cupid but in front of Dancer, Vixen and Donder. Rudolph should be behind Prancer but in front of Dasher, Dancer and Donder. Finally, Dasher should be behind Prancer but in front of Blitzen, Dancer and Vixen.&quot;). space --&gt; &quot; &quot;. reindeer(&#x27;Blitzen&#x27;) --&gt; &quot;Blitzen&quot;. reindeer(&#x27;Comet&#x27;) --&gt; &quot;Comet&quot;. reindeer(&#x27;Cupid&#x27;) --&gt; &quot;Cupid&quot;. reindeer(&#x27;Dancer&#x27;) --&gt; &quot;Dancer&quot;. reindeer(&#x27;Dasher&#x27;) --&gt; &quot;Dasher&quot;. reindeer(&#x27;Donder&#x27;) --&gt; &quot;Donder&quot;. reindeer(&#x27;Prancer&#x27;) --&gt; &quot;Prancer&quot;. reindeer(&#x27;Rudolph&#x27;) --&gt; &quot;Rudolph&quot;. reindeer(&#x27;Vixen&#x27;) --&gt; &quot;Vixen&quot;. complement(S, P, [[S, P, Reindeer] | R], R) --&gt; reindeer(Reindeer). sep --&gt; &quot;, &quot;. sep --&gt; &quot; and &quot;. list(Pred, Sep, S1, S3) --&gt; call(Pred, S1, S2), list_next(Pred, Sep, S2, S3). list_next(Pred, Sep, S1, S3) --&gt; Sep, call(Pred, S1, S2), list_next(Pred, Sep, S2, S3). list_next(_, _, S, S) --&gt; []. position(&gt;) --&gt; &quot;behind&quot;. position(&lt;) --&gt; &quot;in front of&quot;. text(S) --&gt; list(proposition, space, S, S2), space, last_sentence(S2, []). last_sentence(S1, S2) --&gt; &quot;Finally, &quot;, proposition(S1, S2). proposition(S1, S3) --&gt; proposition(R, S1, S2), inverse_proposition(R, S2, S3), &quot;.&quot;. proposition(R, S1, S2) --&gt; reindeer(R), &quot; should be &quot;, position_list(R, S1, S2). position_list(R, S1, S2) --&gt; position(P), space, list(complement(R, P), sep, S1, S2). inverse_proposition(R, S1, S2) --&gt; &quot; but &quot;, position_list(R, S1, S2). inverse_proposition(R, S1, S2) --&gt; &quot;, whilst &quot;, proposition(R, S1, S2). inverse_proposition(_, S, S) --&gt; []. :- table(follows&#x2F;3). follows(R1, R2, Pairs) :- member([R1, &gt;, R2], Pairs). follows(R1, R2, Pairs) :- member([R2, &lt;, R1], Pairs). follows(R1, R3, Pairs) :- follows(R1, R2, Pairs), follows(R2, R3, Pairs). order([X | L], Pairs) :- order(L, X, Pairs). order([], _, _). order([Y | L], X, Pairs) :- follows(Y, X, Pairs), order(L, Y, Pairs). </code></pre> And we can solve the riddle with:<p><pre><code> ?- text(T), phrase(text(Pairs), T), length(L, 9), order(L, Pairs). T = [86, 105, 120, 101, 110, 32, 115, 104, 111|...], Pairs = [[&#x27;Vixen&#x27;, &gt;, &#x27;Rudolph&#x27;], [&#x27;Vixen&#x27;, &gt;, &#x27;Prancer&#x27;], [&#x27;Vixen&#x27;, &gt;, &#x27;Dasher&#x27;], [&#x27;Vixen&#x27;, &lt; , &#x27;Dancer&#x27;], [&#x27;Vixen&#x27;, &lt;, &#x27;Comet&#x27;], [&#x27;Dancer&#x27;, &gt;, &#x27;Donder&#x27;], [&#x27;Dancer&#x27;, &gt;|...], [&#x27;Dancer&#x27;|...] , [...|...]|...], L = [&#x27;Prancer&#x27;, &#x27;Cupid&#x27;, &#x27;Rudolph&#x27;, &#x27;Dasher&#x27;, &#x27;Blitzen&#x27;, &#x27;Vixen&#x27;, &#x27;Comet&#x27;, &#x27;Donder&#x27;, &#x27;Dancer&#x27;] </code></pre> One nice thing we can do with this grammar is that we can also generate the text from a list of constraints:<p><pre><code> ?- Pairs = [[&#x27;Prancer&#x27;, &lt;, &#x27;Cupid&#x27;], [&#x27;Cupid&#x27;, &lt;, &#x27;Rudolph&#x27;]], phrase(text(Pairs), T), string_codes(S, T). Pairs = [[&#x27;Prancer&#x27;, &lt;, &#x27;Cupid&#x27;], [&#x27;Cupid&#x27;, &lt;, &#x27;Rudolph&#x27;]], T = [80, 114, 97, 110, 99, 101, 114, 32, 115|...], S = &quot;Prancer should be in front of Cupid. Finally, Cupid should be in front of Rudolph.&quot; .</code></pre>