<p>Both your code and the code in <a href="https://stackoverflow.com/a/71699969/4609915">the answer of @TessellatingHacker</a> lose <a href="/questions/tagged/logical-purity" class="post-tag" title="show questions tagged &#39;logical-purity&#39;" rel="tag">logical-purity</a> when the arguments of <code>minset_one/3</code> are not sufficiently instantiated:</p>
<pre>
?- D1 = [X,Y,Z], D2 = [U,V], minset_one(D1,D2,T).
   D1 = [<b>1</b>,Y,Z], D2 = [<b>1</b>,V], T = D2
;  <b>false</b>.                           % no more solutions!
</pre>
<p>This is clearly incomplete. There <em>are</em> other solutions. We lost <a href="/questions/tagged/logical-purity" class="post-tag" title="show questions tagged &#39;logical-purity&#39;" rel="tag">logical-purity</a>.</p>
<p>So, what can we do about this?
Basically, we have two options:</p>
<ol>
<li>check <code>D1</code>, <code>D2</code> and <code>T</code> upfront and throw an <code>instantiation_error</code> when the instantiation is not sufficient.</li>
<li>use building blocks that are better suited for code that preserves <a href="/questions/tagged/logical-purity" class="post-tag" title="show questions tagged &#39;logical-purity&#39;" rel="tag">logical-purity</a>.</li>
</ol>
<hr />
<p>In this answer I want to show how to realise option number two.</p>
<p>The code is based on <a href="https://stackoverflow.com/a/27358600/4609915"><code>if_/3</code></a> which is the core of <a href="https://arxiv.org/pdf/1607.01590.pdf" rel="nofollow noreferrer"><code>library(reif)</code></a>.
In short, we reify the truth values of relations and use Prolog indexing on these values.</p>
<p>Using SWI-Prolog 8.4.2:</p>
<pre>
?- use_module(<a href="http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/swi/reif.pl" rel="nofollow noreferrer">library(reif)</a>).
</pre>
<p>First, <code>shorter_than_t(Xs,Ys,T)</code>
reifies <em>&quot;list <code>Xs</code> is shorter than <code>Ys</code>&quot;</em> into <code>T</code>:</p>
<pre>
shorter_than_t([],Ys,T) :-
   aux_nil_shorter_than(Ys,T).
shorter_than_t([_|Xs],Ys,T) :-
   aux_cons_shorter_than_t(Ys,Xs,T).

aux_nil_shorter_than_t([],false).
aux_nil_shorter_than_t([_|_],true).

aux_cons_shorter_than_t([],_,false).
aux_cons_shorter_than_t([_|Ys],Xs,T) :-
   shorter_than_t(Xs,Ys,T).
</pre>
<p>Based on <code>shorter_than_t/3</code> we define <code>minset_one/3</code>:</p>
<pre>
minset_one(D1,D2,T) :-
   if_(shorter_than_t(D1,D2),
       if_(memberd_t(1,D1), D1=T, (memberd_t(1,D2,true),D2=T)),
       if_(memberd_t(1,D2), D2=T, (memberd_t(1,D1,true),D1=T))).
</pre>
<p>Now let's run above query again:</p>
<pre>
?- D1 = [X,Y,Z], D2 = [U,V], minset_one(D1,D2,T).
   D1 = [X,Y,Z], D2 = [<b>1</b>,V], T = D2
;  D1 = [X,Y,Z], D2 = [U,<b>1</b>], T = D2, dif(U,1)
;  D1 = [<b>1</b>,Y,Z], D2 = [U,V], T = D1, dif(U,1), dif(V,1)
;  D1 = [X,<b>1</b>,Z], D2 = [U,V], T = D1, dif(U,1), dif(V,1), dif(X,1)
;  D1 = [X,Y,<b>1</b>], D2 = [U,V], T = D1, dif(U,1), dif(V,1), dif(X,1), dif(Y,1)
;  false.
</pre>
<p>At last, <code>minset_one/3</code> has become complete!</p>
Find smallest list under conditions
<p>In SWI-Prolog I want to establish the list L from two lists <strong>L1</strong> and <strong>L2</strong> with the smallest count of elements under the condition, that <code>1 ∈ L1</code> and <code>1 ∈ L2</code>.
If <code>1 ∉ L1</code> and <code>1 ∈ L2</code>, then <code>L = L1</code>. If <code>1 ∈ L1</code> and <code>1 ∉ L2</code>, then <code>L = L2</code>. If <code>1 ∉ L1</code> and <code>1 ∉ L2</code>, then the predicate returns false.</p>
<p>I could evaluate this in Prolog with the following conditions:</p>
<pre><code>minset_one(D1, D2, T) :- ((member(1, D1), not(member(1, D2))) -&gt; T=D1).
minset_one(D1, D2, T) :- ((not(member(1, D1)), member(1, D2)) -&gt; T=D2).
minset_one(D1, D2, T) :- (member(1, D1), member(1, D2), length(D1,L1), length(D2,L2), L1 &gt;= L2) -&gt; T=D2.
minset_one(D1, D2, T) :- (member(1, D1), member(1, D2), length(D1,L1), length(D2,L2), L2 &gt; L1) -&gt; T=D1.
</code></pre>
<p>My problem with that function is, the member function is called very often. Is their a way to reduce the complexity of that predicate in that way, the functions</p>
<ul>
<li><code>member(1, D1)</code></li>
<li><code>member(1, D2)</code></li>
<li><code>length(D1, L1)</code></li>
<li><code>length(D2, L2)</code></li>
</ul>
<p>are called only one time?</p>