GithubHelp home page GithubHelp logo

sdiehl / haskell-picosat Goto Github PK

View Code? Open in Web Editor NEW
16.0 4.0 5.0 90 KB

Haskell bindings for PicoSAT solver

License: MIT License

Haskell 6.99% C 93.01%
picosat picosat-solver haskell haskell-library sat-solver logic-programming logic

haskell-picosat's Introduction

Haskell PicoSAT

Build Status Hackage

haskell-picosat are Haskell bindings to the PicoSAT solver, written in C. It reads in clauses in CNF ( Conjunctive-Normal Form ) and returns a solution which satisfies the clauses.

The most notable distinction of this binding is that the SAT solver library is included with the cabal package so you shouldn't need to install anything but this package to get going. It's also notably faster than a pure Haskell solution at solving very large constraint problems.

Installing

$ cabal install picosat

Usage

If we have a table of variables representing logical statements we can enumerate them with integers.

A  1
B  2
C  3
D  4
E  5
F  6

Then the clause can be written as sequences of positive integers (assertion) and negative integers (negation):

(A v ¬B v C)
1 -2 3 0
(B v D v E)
2 4 5 0
(D V F)
4 6 0

Solutions to a statement of the form:

(A v ¬B v C) ∧ (B v D v E) ∧ (D v F)

Can be written as zero-terminated lists of integers:

1 -2 3 0
2 4 5 0
4 6 0

To use the Haskell bindings simply pass a list of clauses to the solve function, this will return either the solution or Unsatisfiable or Unknown.

import Picosat

main :: IO [Int]
main = do
  solve [[1, -2, 3], [2,4,5], [4,6]]
  -- Solution [1,-2,3,4,5,6]

The solution given we can interpret as:

1   A 
-2 ¬B 
3   C
4   D
5   E
6   F

To generate all possible solutions we repeatedly feed the negated solution to the solver yielding which is implemented with the solveAll function which yields a sequence of solutions.

import Picosat

main :: IO [Int]
main = solveAll [[1,2]]
  -- [Solution [1,2],Solution [-1,2],Solution [1,-2]]

For a more complicated example a Sudoku solver is included as an example.

License

PicoSAT itself is included and is also licensed the MIT license. Copyright (c) 2006 - 2012, Armin Biere, Johannes Kepler University.

Released under the MIT License. Copyright (c) 2013-2020, Stephen Diehl

haskell-picosat's People

Contributors

franklinchen avatar gibiansky avatar malie avatar sdiehl avatar toxaris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

haskell-picosat's Issues

picosat fails with stack overflow

If you create a large problem, then picosat fails with stack overflow. It actually creates and solves the problem, but fails in getSolution while going through the results with forM. Here is a test program and a new version of forM which would work for arbitrary large problems (as long as you have heap space). It returns the solution list in the same order as before, but if you remove the "reverse" in the mapM', then it would still work but return the result in the opposite order (and would be slightly faster).

module Main where
import Control.Monad (forM)
import Picosat

vars :: Int
vars = 1000000
clauses :: [[Int]]
clauses = fmap (\x -> [x]) [1 .. vars]

main :: IO ()
main = do
    putStrLn "Running new forM':"
    sol1 <- forM' [1 .. vars] return
    print $ last sol1
--  putStrLn "Running old forM:"
--  sol2 <- forM [1 .. vars] return
--  print $ last sol2
    putStrLn $ "Running Picosat with " ++ show (length clauses) ++ " clauses:"
    sol <- Picosat.solve clauses
    putStrLn $ show sol

mapM' :: Monad m => (a -> m b) -> [a] -> [b] -> m [b]
mapM' _ [] ys = return (reverse ys)
mapM' f (x : xs) ys = f x >>= \y -> mapM' f xs (y : ys)

forM' :: Monad m => [a] -> (a -> m b) -> m [b]
forM' xs f = mapM' f xs []

test/Rand.hs:2:1: error: Failed to load interface for ‘RandomCNF’

In the Stackage Nightly build:

Building picosat-0.1.4...
Preprocessing library picosat-0.1.4...
[1 of 1] Compiling Picosat          ( src/Picosat.hs, dist/build/Picosat.o )
Preprocessing test suite 'Sudoku' for picosat-0.1.4...
[1 of 1] Compiling Main             ( test/Sudoku.hs, dist/build/Sudoku/Sudoku-tmp/Main.o )
Linking dist/build/Sudoku/Sudoku ...
Preprocessing test suite 'Scoped' for picosat-0.1.4...
[1 of 1] Compiling Main             ( test/Scoped.hs, dist/build/Scoped/Scoped-tmp/Main.o )
Linking dist/build/Scoped/Scoped ...
Preprocessing test suite 'rand-shared-improvement' for picosat-0.1.4...
[1 of 1] Compiling Main             ( test/Rand.hs, dist/build/rand-shared-improvement/rand-shared-improvement-tmp/Main.o )

test/Rand.hs:2:1: error:
    Failed to load interface for ‘RandomCNF’
    Use -v to see a list of the files searched for.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.