title: Prolog query with constraint returns false, but there are answers
body: <p>I am trying to write a prolog program that works out which course I can choose in the university course catalog. Each course has a unique code like cs180 and each course may have some prerequisite courses, which means the required courses must be finished before registering for this course.</p>
  <p>Assuming that there's no circular dependency of courses, all courses in the university form a directed graph with no loops.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>req(ma262, [ma261,ma182,ma174,ma271,ma27101,ma172,ma263]).
  req(ma351, [ma174,ma172,ma182,ma261,ma271,ma27101,ma263]).
  req(ma366, [ma351,ma265]).
  req(ma341, [ma174,ma172,ma182,ma261]).
  req(ma440, [ma35301]).
  req(ma35301, [ma265,ma351]).
  req(cs314, [ma262,ma265,ma350,ma351]).
  req(cs314, [cs180,cs158,cs177,cs159]).
  ok(ma172).
  ok(cs180).
  </code></pre>
  <p>In this program, <code>req(A,B)</code> indicates that: to register for class A, I must complete at least one course from the list of classes B. If there's multiple statements of requirement, then all of the requirements must be met. For example, to register for <code>cs314</code>, I need to complete one course from both lists. <code>ok/1</code> indicates that I've already completed the course.</p>
  <p>Then I define a <code>valid scheme</code> as a list of courses such that each class in the list is either</p>
  <ol>
  <li>completed, or</li>
  <li>some other classes in the list fulfills its requirement.</li>
  </ol>
  <p>For example, <code>[ma172]</code> is a valid scheme as it's completed. <code>[ma172,ma262]</code> is a valid scheme since the requirement of <code>ma262</code> can be met with <code>ma172</code>.</p>
  <p>Here's my code.</p>
  <pre class="lang-cpp prettyprint-override"><code>valid(Cs):-
      is_set(Cs), /*Cs is distinct*/
      maplist({Cs}/[C]&gt;&gt;( /*for each class in Cs*/
                        ok(C); /*either completed, or meets requirements*/
                        (setof(P,req(C,P),Ps), /*get all requirements*/
                         maplist({Cs}/[L]&gt;&gt;( /*check all requirements*/
                                           member(X,L),
                                           (member(X,Cs);ok(X))
                                           
                                           ),Ps)
                        )),Cs).
  </code></pre>
  <p>When I query <code>?- valid(N)</code> , prolog simply returns <code>[]</code> and then <code>false</code>. However, these examples hold true:</p>
  <pre class="lang-cpp prettyprint-override"><code>?- valid([ma366,ma351])
     true
  ?- valid([ma366,ma351,ma172])
     true
  </code></pre>
  <p>I don't know why the solver doesn't work correctly. If my code breaks the monotonicity, would you like to point out where? And how should I change my code to get it to work?</p>