{"id":26168788,"url":"https://github.com/elben/sat","last_synced_at":"2025-09-07T08:35:30.709Z","repository":{"id":136497995,"uuid":"59932097","full_name":"elben/sat","owner":"elben","description":"SAT solver assistant. Converts propositions (p ^ q) ==\u003e CNF ==\u003e DIMACS CNF files.","archived":false,"fork":false,"pushed_at":"2018-12-29T19:38:45.000Z","size":2569,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-11T18:52:54.514Z","etag":null,"topics":["haskell","sat-solver"],"latest_commit_sha":null,"homepage":"","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elben.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-29T07:43:13.000Z","updated_at":"2024-08-29T13:23:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"d574fb3f-a7bd-402e-8646-44cf9b647f50","html_url":"https://github.com/elben/sat","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/elben/sat","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elben%2Fsat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elben%2Fsat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elben%2Fsat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elben%2Fsat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elben","download_url":"https://codeload.github.com/elben/sat/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elben%2Fsat/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274013059,"owners_count":25207376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-07T02:00:09.463Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["haskell","sat-solver"],"created_at":"2025-03-11T18:35:21.357Z","updated_at":"2025-09-07T08:35:30.700Z","avatar_url":"https://github.com/elben.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Satisfiability (SAT) Solver\n\nAs children we learn that the game of tic-tac-toe can end in ties. We know this\nfrom experience. But can we ask a machine to prove that ties exist in\ntic-tac-toe? Or any other game?\n\nIf we can represent this question in terms of a boolean formula, we can ask\nwhether or not this formula is satisfiable. That is, whether we can set the\nvarious varibles in such a way that the formula returns `True`.\n\nThis is known as the [Boolean\nsatisfiability](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem)\nproblem, or SAT for short.\n\nFor example, consider this simple predicate:\n\n```\nA and B\n```\n\nWhere `A` and `B` are variables that can either be set to `True` or `False`.\nThe statement above can be satified by setting both variables to\n`True`.\n\nAn example statement that cannot be satisfied is `A and (not A)`.\n\nIt turns out we can state a wide range of questions in this fashion. But when\nthe posed question contains a lot of variables and is very large, we need the\nhelp of a *solver*. These are specialized programs that can take in large\nquestions and spit out whether or not the posed question is satisfiable.\n\nThis library is not such a solver. Instead, this library helps you write\nquestions in the format that solvers take in. Namely, the [DIMACS CNF\nformat](http://www.satcompetition.org/2009/format-benchmarks2009.html). Examples\nof solvers include [clasp](http://www.cs.uni-potsdam.de/clasp/) and\n[MiniSat](http://minisat.se/).\n\n`sat` also allows you to write boolean formulas in Haskell, which can then be\nconverted to conjunctive normal form, which is in intermediary format that DMACS\nCNF accepts. Example (note that `^` means AND, and `v`\nmeans OR):\n\n```haskell\n-- Convert the formula (b ^ c) v a v d ==\u003e ((b v a v d) ^ (c v a v d))\n\u003e\u003e display $ cnf (Or [And [Var \"b\", Var \"c\"], Var \"a\", Var \"d\"])\n\"((b v a v d) ^ (c v a v d))\"\n\n-- Convert the formula, then emit the DIMACS format.\nputStrLn $ emitDimacs $ cnf (Or [And [Var \"b\", Var \"c\"], Var \"a\", Var \"d\"])\n-- p cnf 4 2\n-- 1 2 3 0\n-- 4 2 3 0\n```\n\nThe format that `emitDimacs` spat out looks like random stuff, but that is the\nexact text that you would feed the SAT solvers. Continue reading this README to\nsee real-world examples.\n\n# Installing\n\n`sat` needs an external SAT solver to work. On OS X, [clasp](http://www.cs.uni-potsdam.de/clasp/) is the easiest to install:\n\n```bash\nbrew install clasp\n```\n\nOn Linux and Windows, try [MiniSat](http://minisat.se/).\n\nSAT uses Nix to build and run the REPL. If you don't have Nix installed:\n\n```\ncurl https://nixos.org/nix/install | sh\n```\n\n# Using\n\nTo run a REPL, first open a Nix shell. All `cabal` commands should be executed\ninside of a Nix shell:\n\n```bash\nnix-shell --attr env release.nix\n```\n\nThen, we can run the sample program (which is the TicTacToe program described\nbelow), or open a REPL like so:\n\n```\ncabal new-run sat-exe\n\ncabal new-repl\n```\n\n## Tic Tac Toe\n\nI have the `TicTacToe` example that proves that tic-tac-toe can end in a tie.\nEven though we all learned this at the age of five, it's a small problem that\nwe can all understand.\n\nThe `TicTacToe.hs` file contains detailed description on how we specify this\nproblem.\n\n```bash\ncabal new-repl\n:l app/TicTacToe\nputStrLn $ emitDimacs TicTacToe.canEndInTie\n```\n\nTo prove that TicTacToe can indeed end in a tie:\n\n```\ncabal run sat-exe \u003e tictactoe.txt\n\n# In bash:\nclasp 3 tictactoe.txt\n\n# c clasp version 3.1.3\n# c Reading from tictactoe.txt\n# c Solving...\n# c Answer: 1\n# v -1 2 -3 4 -5 -6 7 -8 9 10 -11 12 -13 14 15 -16 17 -18 0\n# c Answer: 2\n# v -1 2 -3 -4 -5 6 7 -8 9 10 -11 12 13 14 -15 -16 17 -18 0\n# c Answer: 3\n# v -1 2 -3 4 -5 6 7 -8 9 10 -11 12 -13 14 -15 -16 17 -18 0\n# s SATISFIABLE\n# c\n# c Models         : 3+\n# c Calls          : 1\n# c Time           : 0.001s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)\n# c CPU Time       : 0.000s\n```\n\nNote that the answer is `SATISFIABLE`, which means that TicTacToe can indeed end\nin a tie.\n\n## Clique\n\nThe `Clique` module allows you to build Clique propositions.\n\nA [clique](https://en.wikipedia.org/wiki/Clique_(graph_theory)) is a sub-graph\nof an undirected graph where every node is connected to every other node.\n\nFor example, the graph below has a clique of size 3. Namely, the nodes\n`{2,3,5}` form the clique because each node is connected to every other node.\n\n```\n       3----4\n      / \\   |\n     2---5--6\n    /\n   1\n```\n\nWe can form a boolean proposition that asks, for a given graph, whether or not\nthere is a clique of size `k` in a graph with `n` nodes.\n\nTo use:\n\n```\n# Inside of a REPL\n:l app/Clique.hs\n\n# Note that n = 6, k = 3.\n\u003e\u003e\u003e putStrLn $ emitDimacs $ buildGraph 6 3 [(1,2), (2,3), (2,5), (3,4), (3,5), (4,6), (5,6)]\n\n# Copy-paste output into clique.txt.\n\nclasp 1 clique.txt\n\n# c Reading from clique.txt\n# c Solving...\n# c Answer: 1\n# v -1 2 -3 -4 -5 -6 -7 -8 -9 -10 11 -12 -13 -14 15 -16 -17 -18 0\n# s SATISFIABLE\n```\n\n## Others\n\nOther problem to solve via SAT:\n\n- [SuDoku](http://www.cs.qub.ac.uk/~I.Spence/SuDoku/SuDoku.html)\n- 3-Coloring\n- Hamiltonian cycle\n\n# Development\n\nInstall nix, if you haven't already:\n\n```\ncurl https://nixos.org/nix/install | sh\n```\n\nBuilding a new `sat.nix`:\n\n```\n# If cabal2nix is not installed:\nnix-env --install cabal2nix\n\nrm -f sat.nix \u0026\u0026 cabal2nix . \u003e sat.nix\n```\n\nBuild using Nix:\n\n```\nnix-build release.nix\n```\n\nGetting a Nix shell, and using Cabal to build:\n\n```\nnix-shell --attr env release.nix\n\ncabal new-configure\ncabal new-build\n```\n\nRun doc tests:\n\n```\ndoctest -isrc -Wall -fno-warn-type-defaults src/Lib.hs\n```\n\nRun QuickCheck:\n\n```\n# Inside of a REPL\n:l test/Spec.hs\n\u003e\u003e main\n+++ OK, passed 100 tests.\n+++ OK, passed 100 tests.\n```\n\n# References\n\nInspired by Felienne Hermans' [Quarto](https://github.com/Felienne/Quarto\n) talk at LambdaConf 2016.\n\n[clasp](http://www.cs.uni-potsdam.de/clasp/)\n\nhttp://www.cs.duke.edu/courses/summer13/compsci230/restricted/lectures/L03.pdf\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felben%2Fsat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felben%2Fsat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felben%2Fsat/lists"}