title: Prolog arg built in personal implementation, don't work
body: <p>I have some trouble with an exercise that ask me to implement the classic <strong>arg</strong> predicate in Prolog.</p>
  
  <pre><code>arg(?Arg, +Term, ?Value)
  </code></pre>
  
  <p>Where Arg it is the index of the argument in the arguments list of a Term. Value it is the value of this argument.</p>
  
  <p>For example:</p>
  
  <pre><code>arg(1, t(f(X),Y,a), Value)
  Value = f(X).
  </code></pre>
  
  <p>Because f(x) it is the first argument in the arguments list of the t main functor.</p>
  
  <p>So I am tryng to resolve the exercise using the univ =.. predicates in this way:</p>
  
  <pre><code>argom(ArgIndex,Term,ValueArg):- Term =.. [_|ArgsList],
                              element_n(ArgIndex,ArgsList,ValueArg).
  
  element_n(1,[ValueArg,_],ValueArg).
  
  element_n(N,[_,Tail],ValueArg):- N1 is N-1,
                                   element_n(N1,Tail,ValueArg).
  </code></pre>
  
  <p>Where <strong>argom</strong> relation it is my personal implementation of the <strong>arg</strong> built in predicate...</p>
  
  <p>The problem is that solution don't work...the idea is that I have an <strong>argom</strong> predicate that take:</p>
  
  <ol>
  <li><strong>ArgIndex</strong>: the index of the argument that I would obtain.</li>
  <li><strong>Term</strong> that is an expression like: <strong>f(a,b,c)</strong></li>
  <li><strong>ValueArg</strong>: that rappresent the value of the argument having <strong>ArgIndex</strong> as index</li>
  </ol>
  
  <p>So, if I have <strong>f(a,b,c)</strong> as <strong>Term</strong> and <strong>ArgIndex</strong> is 1, then <strong>ArgValue</strong> have to be <strong>a</strong>.</p>
  
  <p>I use the <strong>unive</strong> built in predicate that, starting from my <strong>Term</strong> create a list where in the head there is the main functor of my <strong>Term</strong> and in the tail (named <strong>ArgsList</strong>) there is the arguments list of the main functor.</p>
  
  <p>I would the nth argument so I would the  nth element in the ArgsList and to obtain it I define the <strong>element_n</strong> relation that take: ArgIndex, ArgsList, ValueArg.</p>
  
  <p>If I would the <strong>first</strong> element in the list the <strong>ArgIndex</strong> value is 1 and the base case it is matched:</p>
  
  <pre><code> element_n(1,[ValueArg,_],ValueArg).
  </code></pre>
  
  <p>So the ValueArg is in the head of my ArgsList argument list and unify it.</p>
  
  <p>Otherwise call the rule to obtain the nth element in the ArgsList</p>
  
  <p>The problem is that when I consult it don't work and the answer is always: <strong>fail</strong> also if I try to obtain the first element (that is the simplest case in wich I use the base case rule)</p>
  
  <p>Looking at the trace I have the same situation:</p>
  
  <pre><code>[trace] 9 ?- argom(1,f(a,b,c),a).
     Call: (6) argom(1, f(a, b, c), a) ? creep
     Call: (7) f(a, b, c)=..[_G3148|_G3149] ? creep
     Exit: (7) f(a, b, c)=..[f, a, b, c] ? creep
     Call: (7) element_n(1, [a, b, c], a) ? creep
     Fail: (7) element_n(1, [a, b, c], a) ? creep
     Fail: (6) argom(1, f(a, b, c), a) ? creep
  false.
  </code></pre>
  
  <p>So, it call the argom rule saying to it: <strong>"Is it TRUE that the first argument of the main functor is a?"</strong></p>
  
  <p>It use in the right way the <strong>univ =..</strong> built in predicate generating the list <strong>[f, a, b, c]</strong> from my Term <strong>f(a, b, c)</strong></p>
  
  <p>Now it call the <strong>element_n</strong> predicate and this is the base case so have to unify the <strong>ValueArg</strong> with the <strong>head of the list</strong> but then it <strong>fail</strong></p>
  
  <p>I have also try to ask: <strong>What is the value of ValueArg if ArgIndex value is 1 and the Term is f(a,b,c)?</strong></p>
  
  <pre><code>[trace] 10 ?- argom(1,f(a,b,c),ValueArg).
     Call: (6) argom(1, f(a, b, c), _G3483) ? creep
     Call: (7) f(a, b, c)=..[_G3553|_G3554] ? creep
     Exit: (7) f(a, b, c)=..[f, a, b, c] ? creep
     Call: (7) element_n(1, [a, b, c], _G3483) ? creep
     Fail: (7) element_n(1, [a, b, c], _G3483) ? creep
     Fail: (6) argom(1, f(a, b, c), _G3483) ? creep
  false.
  </code></pre>
  
  <p>So it appear clear that the problem is that seems to have some problem to unify the head of the list with the ValueArg in this rule:</p>
  
  <pre><code>element_n(1,[ValueArg,_],ValueArg).
  </code></pre>
  
  <p>Why?</p>