Prolog SAT Solver

前端 未结 4 1689
独厮守ぢ
独厮守ぢ 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:26

    One can use CLP(FD) to solve SAT. Just start with a CNF and then observe that a clause:

    x1 v .. v xn 
    

    Can be represented as a constraint:

    x1 + .. + xn #> 0
    

    Further for a negative literal:

    ~x
    

    Simply use:

    1-x
    

    You need to restrict the variables to the domain 0..1 and invoke labeling. As soon as labeling returns some value for the variables, you know that your original formula is satisfiable.

    Here is an example run, running the test of Joe Lehmann:

    Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.5.2)
    Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
    
    ?- use_module(library(clpfd)).
    
    ?- L = [X,Y,Z], L ins 0..1, X+1-Y #> 0, 1-X+1-Y #> 0, X+Z #> 0, label(L).
    X = Y, Y = 0,
    Z = 1 ;
    X = 1,
    Y = Z, Z = 0 ;
    X = Z, Z = 1,
    Y = 0.
    

    Bye

    Constraint Logic Programming over Finite Domains
    http://www.swi-prolog.org/man/clpfd.html

提交回复
热议问题