<p><strong>Preface</strong>: What you have implemented is, in an important way, <strong>a lot better</strong> than the built-in construct you mentioned. I will discuss this point in more detail below.</p>

<p>Regarding the <strong>literal</strong> question: I think you are <em>quite close</em> already, since  you can already <em>nest</em> this to some extent:</p>

<pre>
<b>?- ifthenelse(C1, ifthenelse(C2,X=1,X=2), X=3).</b>
C1 = C2, C2 = true,
X = 1 ;
C1 = true,
C2 = false,
X = 2 ;
C1 = false,
X = 3.
</pre>

<p>What now remains is to make it nestable <strong>in the condition</strong>. For this, you need a way to <em>reify</em> the outcome of conditions, that is, to turn the truth value into a Prolog&nbsp;<em>term</em> that you can reason about symbolically.</p>

<p>See <code>if_/3</code> for more information: <a href="https://arxiv.org/abs/1607.01590" rel="noreferrer"><strong>Indexing <code>dif/2</code></strong></a></p>

<p>A key property this construct preserves is called <a href="/questions/tagged/logical-purity" class="post-tag" title="show questions tagged &#39;logical-purity&#39;" rel="tag">logical-purity</a>. In particular, do not erroneously commit to one branch if both are logically possible!</p>

<hr>

<p><strong>Note on purity</strong>: From a declarative point of view, what you have implemented is very nice and has a clear logical interpretation. Taking for example the last two&nbsp;clauses, we can read <code>ifthenelse(C,G1,G2)</code> as: <em>If</em> <code>C</code> is <code>true</code>, then the predicate holds if <code>G1</code> holds. If <code>C</code> is <code>false</code>, then it holds if <code>G2</code> holds. Perfectly fine. A semantic "if...then" is not in any way problematic; in fact, every single Horn clause can be read in this way, as an implication from right to&nbsp;left.</p>

<p>In contrast, the built-in construct you mention <em>lacks</em> such a declarative reading in general. For example:</p>

<pre>
?- ( ( X = 1 ; X = 2 ) -> true ; true ).
<b>X = 1.</b>
</pre>

<p>But on the other hand:</p>

<pre>
?- <b>X = 2</b>, ( ( X = 1 ; X = 2 ) -> true ; true ).
<b>X = 2.</b>
</pre>

<p>So <em>adding a constraint</em> has led to a <em>new</em> solution. A classical logician's nightmare.</p>

<p>Your construct <em>avoids</em> such problems, because it does not prematurely <strong>commit</strong> to any particular branch. This yields a much more versatile predicate that can also be used in other directions. For example, see that <strong>all</strong> possible solutions are correctly generated:</p>

<pre>
<b>?- ifthenelse(C, true, true).</b>
true ;
C = true ;
C = false.
</pre>

<p>So, I highly encourage your way of formulating this: As you have made perfectly clear, it's <strong>your own</strong> 'if-then-else', and you are using only pure monotonic constructs to express it.</p>

<p>On a psychological note, I generally prefer to give more room to pure declarative constructs and I added this section only because the comments expressed genuine interest in these aspects.</p>
Prolog - Custom 'if-then-else'
<p>I'm experimenting with Prolog and tried to make my own "<strong><code>if-then-else</code></strong>" method, so that I don't use the <code>-&gt; ;</code> method, for experimenting's sake.
My goal is to make it so that there can be nested ifs and elses in my code if required.
Thus far I have got this:</p>

<pre><code>ifthenelse(_, G, G):- G.    %no matter the condition, the goals are the same
ifthenelse(true,G,_):- G.   %if
ifthenelse(false,_,G):- G.  %else
</code></pre>

<p>I think my way hardly seems correct. How can I make my own <code>ifthenelse/3</code> properly?</p>

<p>Thank you</p>