Prolog SAT Solver

前端 未结 4 1720
独厮守ぢ
独厮守ぢ 2021-01-01 19:51

I\'m trying to build a simple Prolog SAT solver. My idea is that the user should enter the boolean formula to be solved in CNF (Conjuctive Normal Form) using Prolog lists, f

4条回答
  •  孤城傲影
    2021-01-01 20:30

    There is a wonderful paper by Howe and King about SAT Solving in (SICStus) Prolog (see http://www.soi.city.ac.uk/~jacob/solver/index.html).

    sat(Clauses, Vars) :- 
        problem_setup(Clauses), elim_var(Vars). 
    
    elim_var([]). 
    elim_var([Var | Vars]) :- 
        elim_var(Vars), (Var = true; Var = false). 
    
    problem_setup([]). 
    problem_setup([Clause | Clauses]) :- 
        clause_setup(Clause), 
        problem_setup(Clauses). 
    
    clause_setup([Pol-Var | Pairs]) :- set_watch(Pairs, Var, Pol). 
    
    set_watch([], Var, Pol) :- Var = Pol. 
    set_watch([Pol2-Var2 | Pairs], Var1, Pol1):- 
        watch(Var1, Pol1, Var2, Pol2, Pairs). 
    
    :- block watch(-, ?, -, ?, ?). 
    watch(Var1, Pol1, Var2, Pol2, Pairs) :- 
        nonvar(Var1) -> 
            update_watch(Var1, Pol1, Var2, Pol2, Pairs); 
            update_watch(Var2, Pol2, Var1, Pol1, Pairs). 
    
    update_watch(Var1, Pol1, Var2, Pol2, Pairs) :- 
        Var1 == Pol1 -> true; set_watch(Pairs, Var2, Pol2).
    

    The clauses are given in CNF like this:

    | ?- sat([[true-X,false-Y],[false-X,false-Y],[true-X,true-Z]],[X,Y,Z]).
     X = true,
     Y = false,
     Z = true ? ;
     X = false,
     Y = false,
     Z = true ? ;
     X = true,
     Y = false,
     Z = false ? ;
    no
    

提交回复
热议问题