title: Mercury: Determinism and pattern matching
body: <p>I have a semideterministic function. When I re-write it to use pattern matching instead of an if statement, Mercury says it becomes nondeterministic. I'd like to understand why.</p>
  
  <p>The original code:</p>
  
  <pre><code>:- pred nth(list(T), int, T).
  :- mode nth(in,      in,  out) is semidet.
  nth([Hd | Tl], N, X) :- (if N = 0 then X = Hd else nth(Tl, N - 1, X)).
  </code></pre>
  
  <p>The revised code:</p>
  
  <pre><code>:- pred nth(list(T), int, T).
  :- mode nth(in,      in,  out) is nondet.
  nth([Hd | _], 0, Hd).                             % Case A
  nth([_ | Tl], N, X) :- N \= 0, nth(Tl, N - 1, X). % Case B
  </code></pre>
  
  <p>I'm used to thinking about pattern matching in SML, where the 0 in  case A would ensure that in case B, N is not 0. Does Mercury work differently? Could case B get called, even if N is 0? (I added the <code>N \= 0</code> clause to case B in the hope of making the predicate semideterministic, but that didn't work.)</p>
  
  <p>Is there a way writing this predicate with pattern matching that is also semideterministic?</p>