UNB/ CS/ David Bremner/ teaching/ cs3383/ lectures/ 50.1-demos/ sat2.py
#!/usr/bin/env python3
from unit_prop import UnitProp

def backtrack(P0):
    S = [P0]
    while len(S) > 0:
        P = S.pop()
        P1 = UnitProp(P)
        if P1 != False:
            if P1 == True:
                return P1
            else:
                for R in expand(P1):
                    S.append(R)
    return False

def reduce(clauses,literal):
  out=[]
  for C in clauses:
    if not literal in C:
      new=[z for z in C if z != -1*literal]
      out.append(new)
  return out

def expand(clauses):
  j = clauses[0][0]
  return [reduce(clauses,j),
          reduce(clauses,-j)]

if __name__ == "__main__":
    print(backtrack([ [1], [-1,2] ,[-1,3,4] ]))
    print(backtrack([ [1], [-1,2] ,[-2] ]))