├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .hlint.yaml ├── .travis.yml ├── .vscode └── settings.json ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── MODULES.md ├── README.md ├── bin └── retag ├── cabal.project ├── cbits ├── Makefile ├── dlx.cpp ├── dlx.hpp ├── dlx_capi.cpp ├── dlx_capi.h ├── dlx_queens.cpp ├── dxz.cpp ├── dxz.hpp ├── dxz_capi.cpp ├── dxz_capi.h └── dxz_queens.cpp ├── guanxi.cabal ├── src ├── Aligned │ ├── Base.hs │ ├── Free.hs │ ├── Freer.hs │ └── Internal.hs ├── Cover │ ├── DLX.hs │ └── DXZ.hs ├── Disjoint.hs ├── Domain │ ├── Internal.hs │ ├── Interval.hs │ └── Relational.hs ├── Equality.hs ├── FD │ ├── Monad.hs │ └── Var.hs ├── Key.hs ├── Key │ └── Coercible.hs ├── Log.hs ├── Logic │ ├── Class.hs │ ├── Cont.hs │ ├── Naive.hs │ └── Reflection.hs ├── Par │ ├── Class.hs │ ├── Cont.hs │ ├── Future.hs │ └── Promise.hs ├── Prompt │ ├── Class.hs │ ├── Iterator.hs │ └── Reflection.hs ├── Ref.hs ├── Relative │ ├── Base.hs │ └── Internal.hs ├── SAT.hs ├── Sharing.hs ├── Signal.hs ├── Sink.hs ├── Tactic.hs ├── Unaligned │ ├── Base.hs │ └── Internal.hs ├── Unification │ └── Class.hs ├── Unique.hs └── Vec.hs ├── test ├── Spec │ ├── Cover │ │ └── DLX.hs │ ├── Domain │ │ └── Interval.hs │ ├── FD │ │ └── Monad.hs │ ├── Logic │ │ └── Reflection.hs │ ├── Prompt │ │ └── Iterator.hs │ └── Unaligned │ │ └── Base.hs ├── doctest-main.hs ├── doctest.json ├── hedgehog-main.hs ├── queens.hs └── spec.hs └── wip ├── ProbabilisticTree.hs └── RationalArithmetic.hs /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: haskell ci 2 | on: 3 | push: 4 | pull_request: 5 | workflow_dispatch: 6 | jobs: 7 | generate-matrix: 8 | name: "Generate matrix from cabal" 9 | outputs: 10 | matrix: ${{ steps.set-matrix.outputs.matrix }} 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Extract the tested GHC versions 14 | id: set-matrix 15 | uses: kleidukos/get-tested@v0.1.7.0 16 | with: 17 | cabal-file: guanxi.cabal 18 | ubuntu-version: latest 19 | macos-version: latest 20 | windows-version: latest 21 | version: 0.1.7.0 22 | tests: 23 | name: ${{ matrix.ghc }} on ${{ matrix.os }} 24 | needs: generate-matrix 25 | runs-on: ${{ matrix.os }} 26 | strategy: 27 | fail-fast: false 28 | matrix: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }} 29 | steps: 30 | - uses: actions/checkout@v4 31 | - uses: haskell-actions/setup@v2 32 | id: setup-haskell 33 | with: 34 | ghc-version: ${{ matrix.ghc }} 35 | - run: cabal freeze --enable-tests 36 | - uses: actions/cache@v2 37 | with: 38 | path: ${{ steps.setup-haskell.outputs.cabal-store }} 39 | key: ${{ runner.os }}-${{ matrix.ghc }}-${{ hashFiles('cabal.project.freeze') }} 40 | restore-keys: ${{ runner.os }}-${{ matrix.ghc }}- 41 | - run: cabal build all 42 | - run: cabal test --test-option=--color --test-show-details=always test:queens 43 | if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' 44 | - run: cabal test --test-option=--color --test-show-details=always test:spec 45 | if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' 46 | - run: cabal test --test-option=--color --test-show-details=always test:hedgehog 47 | if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *# 2 | *.aux 3 | *.chi 4 | *.chs.h 5 | *.dyn_hi 6 | *.dyn_o 7 | *.eventlog 8 | *.hi 9 | *.hp 10 | *.o 11 | *.prof 12 | *~ 13 | .*.swo 14 | .*.swp 15 | .DS_Store 16 | .HTF/ 17 | .cabal-sandbox/ 18 | .depend 19 | .ghc.environment.* 20 | .ghcid* 21 | .hpc 22 | .hsenv 23 | .stack-work/ 24 | TAGS 25 | cabal-dev 26 | cabal.project.local 27 | cabal.project.local~ 28 | cabal.sandbox.config 29 | cbits/.depend 30 | cbits/dlx_queens 31 | cbits/dxz_queens 32 | dist 33 | dist-newstyle 34 | dist-doctest 35 | docs 36 | ghcid.txt 37 | old 38 | tags 39 | wiki 40 | wip 41 | -------------------------------------------------------------------------------- /.hlint.yaml: -------------------------------------------------------------------------------- 1 | - arguments: [--color, --cpp-define=HLINT] 2 | - ignore: {name: "Use const" } 3 | - ignore: {name: "Use camelCase" } 4 | - ignore: {name: "Use <$>", within: [ Sharing ]} 5 | - ignore: {name: "Unused LANGUAGE pragma", within: [ FD.Monad ]} 6 | - ignore: {name: "Parse error", within: [ Ref.Log ]} 7 | - ignore: {name: "Eta reduce", within: [ Cover.DXZ, Ref.Env, Ref.Key, Ref.Log ]} 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: haskell 2 | dist: trusty 3 | 4 | cache: 5 | directories: 6 | - $HOME/.cabal/store 7 | 8 | cabal: "2.4" 9 | 10 | matrix: 11 | include: 12 | - ghc: "8.6.4" 13 | 14 | install: 15 | - cabal --version 16 | - ghc --version 17 | 18 | script: 19 | - cabal v2-update 20 | - cabal v2-build 21 | - cabal v2-test --enable-test 22 | 23 | notifications: 24 | irc: 25 | channels: 26 | - "irc.freenode.org##haskell-lens" 27 | skip_join: true 28 | template: 29 | - "\x0313guanxi\x0f/\x0306%{branch}\x0f \x0314%{commit}\x0f %{message} \x0302\x1f%{build_url}\x0f" 30 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.exclude": { 3 | ".ghc.environment*": true, 4 | "**/.depend": true, 5 | "**/*.o": true, 6 | "cbits/*_queens": true, 7 | "dist": true, 8 | "dist-doctest": true, 9 | "dist-newstyle": true 10 | } 11 | 12 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0 2 | 3 | * Repository initialized. 4 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at ekmett@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Patches welcome! 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # License 2 | 3 | Licensed under either of 4 | * Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) 5 | * BSD 2-Clause license (https://opensource.org/licenses/BSD-2-Clause) 6 | at your option. 7 | 8 | ## BSD 2-Clause License 9 | 10 | - Copyright 2017-2018 Edward Kmett 11 | - Copyright 2012-2014 Edward Kmett and Dan Doel 12 | 13 | All rights reserved. 14 | 15 | Redistribution and use in source and binary forms, with or without 16 | modification, are permitted provided that the following conditions 17 | are met: 18 | 19 | 1. Redistributions of source code must retain the above copyright 20 | notice, this list of conditions and the following disclaimer. 21 | 22 | 2. Redistributions in binary form must reproduce the above copyright 23 | notice, this list of conditions and the following disclaimer in the 24 | documentation and/or other materials provided with the distribution. 25 | 26 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 27 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 29 | DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR 30 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 34 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 35 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 | POSSIBILITY OF SUCH DAMAGE. 37 | 38 | ## Apache License 39 | 40 | _Version 2.0, January 2004_ 41 | _<>_ 42 | 43 | ### Terms and Conditions for use, reproduction, and distribution 44 | 45 | #### 1. Definitions 46 | 47 | “License” shall mean the terms and conditions for use, reproduction, and 48 | distribution as defined by Sections 1 through 9 of this document. 49 | 50 | “Licensor” shall mean the copyright owner or entity authorized by the copyright 51 | owner that is granting the License. 52 | 53 | “Legal Entity” shall mean the union of the acting entity and all other entities 54 | that control, are controlled by, or are under common control with that entity. 55 | For the purposes of this definition, “control” means **(i)** the power, direct or 56 | indirect, to cause the direction or management of such entity, whether by 57 | contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the 58 | outstanding shares, or **(iii)** beneficial ownership of such entity. 59 | 60 | “You” (or “Your”) shall mean an individual or Legal Entity exercising 61 | permissions granted by this License. 62 | 63 | “Source” form shall mean the preferred form for making modifications, including 64 | but not limited to software source code, documentation source, and configuration 65 | files. 66 | 67 | “Object” form shall mean any form resulting from mechanical transformation or 68 | translation of a Source form, including but not limited to compiled object code, 69 | generated documentation, and conversions to other media types. 70 | 71 | “Work” shall mean the work of authorship, whether in Source or Object form, made 72 | available under the License, as indicated by a copyright notice that is included 73 | in or attached to the work (an example is provided in the Appendix below). 74 | 75 | “Derivative Works” shall mean any work, whether in Source or Object form, that 76 | is based on (or derived from) the Work and for which the editorial revisions, 77 | annotations, elaborations, or other modifications represent, as a whole, an 78 | original work of authorship. For the purposes of this License, Derivative Works 79 | shall not include works that remain separable from, or merely link (or bind by 80 | name) to the interfaces of, the Work and Derivative Works thereof. 81 | 82 | “Contribution” shall mean any work of authorship, including the original version 83 | of the Work and any modifications or additions to that Work or Derivative Works 84 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 85 | by the copyright owner or by an individual or Legal Entity authorized to submit 86 | on behalf of the copyright owner. For the purposes of this definition, 87 | “submitted” means any form of electronic, verbal, or written communication sent 88 | to the Licensor or its representatives, including but not limited to 89 | communication on electronic mailing lists, source code control systems, and 90 | issue tracking systems that are managed by, or on behalf of, the Licensor for 91 | the purpose of discussing and improving the Work, but excluding communication 92 | that is conspicuously marked or otherwise designated in writing by the copyright 93 | owner as “Not a Contribution.” 94 | 95 | “Contributor” shall mean Licensor and any individual or Legal Entity on behalf 96 | of whom a Contribution has been received by Licensor and subsequently 97 | incorporated within the Work. 98 | 99 | #### 2. Grant of Copyright License 100 | 101 | Subject to the terms and conditions of this License, each Contributor hereby 102 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 103 | irrevocable copyright license to reproduce, prepare Derivative Works of, 104 | publicly display, publicly perform, sublicense, and distribute the Work and such 105 | Derivative Works in Source or Object form. 106 | 107 | #### 3. Grant of Patent License 108 | 109 | Subject to the terms and conditions of this License, each Contributor hereby 110 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 111 | irrevocable (except as stated in this section) patent license to make, have 112 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 113 | such license applies only to those patent claims licensable by such Contributor 114 | that are necessarily infringed by their Contribution(s) alone or by combination 115 | of their Contribution(s) with the Work to which such Contribution(s) was 116 | submitted. If You institute patent litigation against any entity (including a 117 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 118 | Contribution incorporated within the Work constitutes direct or contributory 119 | patent infringement, then any patent licenses granted to You under this License 120 | for that Work shall terminate as of the date such litigation is filed. 121 | 122 | #### 4. Redistribution 123 | 124 | You may reproduce and distribute copies of the Work or Derivative Works thereof 125 | in any medium, with or without modifications, and in Source or Object form, 126 | provided that You meet the following conditions: 127 | 128 | * **(a)** You must give any other recipients of the Work or Derivative Works a copy of 129 | this License; and 130 | * **(b)** You must cause any modified files to carry prominent notices stating that You 131 | changed the files; and 132 | * **(c)** You must retain, in the Source form of any Derivative Works that You distribute, 133 | all copyright, patent, trademark, and attribution notices from the Source form 134 | of the Work, excluding those notices that do not pertain to any part of the 135 | Derivative Works; and 136 | * **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any 137 | Derivative Works that You distribute must include a readable copy of the 138 | attribution notices contained within such NOTICE file, excluding those notices 139 | that do not pertain to any part of the Derivative Works, in at least one of the 140 | following places: within a NOTICE text file distributed as part of the 141 | Derivative Works; within the Source form or documentation, if provided along 142 | with the Derivative Works; or, within a display generated by the Derivative 143 | Works, if and wherever such third-party notices normally appear. The contents of 144 | the NOTICE file are for informational purposes only and do not modify the 145 | License. You may add Your own attribution notices within Derivative Works that 146 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 147 | provided that such additional attribution notices cannot be construed as 148 | modifying the License. 149 | 150 | You may add Your own copyright statement to Your modifications and may provide 151 | additional or different license terms and conditions for use, reproduction, or 152 | distribution of Your modifications, or for any such Derivative Works as a whole, 153 | provided Your use, reproduction, and distribution of the Work otherwise complies 154 | with the conditions stated in this License. 155 | 156 | #### 5. Submission of Contributions 157 | 158 | Unless You explicitly state otherwise, any Contribution intentionally submitted 159 | for inclusion in the Work by You to the Licensor shall be under the terms and 160 | conditions of this License, without any additional terms or conditions. 161 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 162 | any separate license agreement you may have executed with Licensor regarding 163 | such Contributions. 164 | 165 | #### 6. Trademarks 166 | 167 | This License does not grant permission to use the trade names, trademarks, 168 | service marks, or product names of the Licensor, except as required for 169 | reasonable and customary use in describing the origin of the Work and 170 | reproducing the content of the NOTICE file. 171 | 172 | #### 7. Disclaimer of Warranty 173 | 174 | Unless required by applicable law or agreed to in writing, Licensor provides the 175 | Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, 176 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 177 | including, without limitation, any warranties or conditions of TITLE, 178 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 179 | solely responsible for determining the appropriateness of using or 180 | redistributing the Work and assume any risks associated with Your exercise of 181 | permissions under this License. 182 | 183 | #### 8. Limitation of Liability 184 | 185 | In no event and under no legal theory, whether in tort (including negligence), 186 | contract, or otherwise, unless required by applicable law (such as deliberate 187 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 188 | liable to You for damages, including any direct, indirect, special, incidental, 189 | or consequential damages of any character arising as a result of this License or 190 | out of the use or inability to use the Work (including but not limited to 191 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 192 | any and all other commercial damages or losses), even if such Contributor has 193 | been advised of the possibility of such damages. 194 | 195 | #### 9. Accepting Warranty or Additional Liability 196 | 197 | While redistributing the Work or Derivative Works thereof, You may choose to 198 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 199 | other liability obligations and/or rights consistent with this License. However, 200 | in accepting such obligations, You may act only on Your own behalf and on Your 201 | sole responsibility, not on behalf of any other Contributor, and only if You 202 | agree to indemnify, defend, and hold each Contributor harmless for any liability 203 | incurred by, or claims asserted against, such Contributor by reason of your 204 | accepting any such warranty or additional liability. 205 | 206 | _END OF TERMS AND CONDITIONS_ 207 | 208 | ### APPENDIX: How to apply the Apache License to your work 209 | 210 | To apply the Apache License to your work, attach the following boilerplate 211 | notice, with the fields enclosed by brackets `[]` replaced with your own 212 | identifying information. (Don't include the brackets!) The text should be 213 | enclosed in the appropriate comment syntax for the file format. We also 214 | recommend that a file or class name and description of purpose be included on 215 | the same “printed page” as the copyright notice for easier identification within 216 | third-party archives. 217 | 218 | Copyright [yyyy] [name of copyright owner] 219 | 220 | Licensed under the Apache License, Version 2.0 (the "License"); 221 | you may not use this file except in compliance with the License. 222 | You may obtain a copy of the License at 223 | 224 | http://www.apache.org/licenses/LICENSE-2.0 225 | 226 | Unless required by applicable law or agreed to in writing, software 227 | distributed under the License is distributed on an "AS IS" BASIS, 228 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 229 | See the License for the specific language governing permissions and 230 | limitations under the License. 231 | 232 | -------------------------------------------------------------------------------- /MODULES.md: -------------------------------------------------------------------------------- 1 | Modules 2 | ======= 3 | 4 | This is a brief and incomplete description of the rough module layout of guanxi 5 | 6 | * Aligned 7 | * Base - type-aligned sequences 8 | * Free - Reflection Without Remorse (RWR) free monad 9 | * Freer - RWR freer monad 10 | * Cover 11 | * DLX - Knuth's dancling links 12 | * DXZ - DLX with ZDDs (zero-suppressed binary decision diagrams) https://aaai.org/ocs/index.php/AAAI/AAAI17/paper/view/14907 13 | * Domain 14 | * Interval - interval arithmetic built with propagators 15 | * FD (Finite Domain) 16 | * Monad - This monad drives everything else in guanxi for now 17 | * Var - finite domain variables encoded as sets of values 18 | * Unaligned 19 | * Base - Okasaki-style catenable sequences and queues 20 | * Logic 21 | * Class - LogicT type class 22 | * Cont - continuation-based implementation of LogicT. If you don't need reflection, this implementation is the fastest. 23 | * Naive - naive LogicT implementation 24 | * Reflection - RWR-style LogicT, using Unaligned.Base 25 | * Prompt 26 | * Class - delimited continuations based on RWR 27 | * Signal - partial propagator implementation 28 | * Tactics - A toy tactic language 29 | * Unique - Fast unique symbols 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | guanxi 2 | ====== 3 | 4 | [![haskell ci](https://github.com/ekmett/guanxi/actions/workflows/ci.yml/badge.svg)](https://github.com/ekmett/guanxi/actions/workflows/ci.yml) 5 | 6 | An exploration of relational programming in Haskell. 7 | 8 | License 9 | ------- 10 | 11 | [Licensed](LICENSE.md) under either of 12 | 13 | * [Apache License, Version 2.0][license-apache] 14 | 15 | * [BSD 2-Clause license][license-bsd] 16 | 17 | at your option. 18 | 19 | Contribution 20 | ------------ 21 | 22 | Unless you explicitly state otherwise, any contribution intentionally submitted 23 | for inclusion in the work by you shall be dual-licensed as above, without any 24 | additional terms or conditions. 25 | 26 | Contact Information 27 | ------------------- 28 | 29 | Contributions and bug reports are welcome! 30 | 31 | Please feel free to contact me through github or on the `#haskell` IRC channel on `irc.freenode.net`. 32 | 33 | -Edward Kmett 34 | 35 | [license-apache]: http://www.apache.org/licenses/LICENSE-2.0 36 | [license-bsd]: https://opensource.org/licenses/BSD-2-Clause 37 | -------------------------------------------------------------------------------- /bin/retag: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | fast-tags -R src test 3 | ctags -a -R cbits 4 | -------------------------------------------------------------------------------- /cabal.project: -------------------------------------------------------------------------------- 1 | packages: . 2 | -------------------------------------------------------------------------------- /cbits/Makefile: -------------------------------------------------------------------------------- 1 | CXX := g++ 2 | CXXFLAGS=-std=c++11 -Wall -g 3 | 4 | SRCS := $(shell find . -name "*.cpp") 5 | OBJS := $(patsubst %.cpp, %.o, $(SRCS)) 6 | 7 | all: dxz_queens dlx_queens dlx_capi.o 8 | 9 | clean: 10 | rm -f dlx_queens dxz_queens $(OBJS) 11 | 12 | dist-clean: clean 13 | rm -f .depend 14 | 15 | .PHONY: all clean dist-clean depend 16 | 17 | depend: .depend 18 | 19 | .depend: $(SRCS) 20 | rm -f ./.depend 21 | $(CXX) $(CXXFLAGS) -MM $^>>./.depend; 22 | 23 | include .depend 24 | 25 | dlx_queens: dlx_queens.o dlx.o 26 | $(CXX) $(CXXFLAGS) $(LDFLAGS) -o dlx_queens $^ $(LDLIBS) 27 | 28 | dxz_queens: dxz_queens.o dxz.o 29 | $(CXX) $(CXXFLAGS) $(LDFLAGS) -o dxz_queens $^ $(LDLIBS) 30 | 31 | %.o: %.cpp 32 | $(CXX) $(CXXFLAGS) -c $< 33 | -------------------------------------------------------------------------------- /cbits/dlx.cpp: -------------------------------------------------------------------------------- 1 | #include "dlx.hpp" 2 | 3 | // compute exact covers using dancing links 4 | 5 | using namespace std; 6 | 7 | dlx::dlx(uint32_t n, uint32_t k) noexcept 8 | : cells(0) 9 | , items(0) 10 | , result(0) 11 | , current_state(state::guessing) { 12 | uint32_t N = n+k; 13 | cells.reserve(N?N+1:2); 14 | items.reserve(N+1); 15 | 16 | for (uint32_t i=0;i values) noexcept { 103 | return add_option(values.begin(),values.end()); 104 | } 105 | 106 | option dlx::pick(link c) noexcept { 107 | return for_option_containing(c, [&](link i) noexcept { 108 | auto & x = cells[i]; 109 | auto & item = items[x.item]; 110 | auto header = item.cell; 111 | items[item.n].p = item.p; 112 | items[item.p].n = item.n; 113 | for (auto j = x.u; j != header; j = cells[j].u) 114 | for_option_containing_exclusive(j, [&](link k) noexcept { 115 | auto & y = cells[k]; 116 | cells[y.u].d = y.d; 117 | cells[y.d].u = y.u; 118 | }); 119 | for (auto j = x.d; j != header; j = cells[j].d) 120 | for_option_containing_exclusive(j, [&](link k) noexcept { 121 | auto & y = cells[k]; 122 | cells[y.u].d = y.d; 123 | cells[y.d].u = y.u; 124 | }); 125 | }); 126 | } 127 | 128 | void dlx::unpick(link c) noexcept { 129 | for_option_containing(c, [&](link i) noexcept { 130 | auto & x = cells[i]; 131 | auto & item = items[x.item]; 132 | auto header = item.cell; 133 | items[item.n].p = x.item; 134 | items[item.p].n = x.item; 135 | for (auto j = x.u; j != header; j = cells[j].u) 136 | for_option_containing_exclusive(j, [&](link k) noexcept { 137 | auto & y = cells[k]; 138 | cells[y.u].d = k; 139 | cells[y.d].u = k; 140 | }); 141 | for (auto j = x.d; j != header; j = cells[j].d) 142 | for_option_containing_exclusive(j, [&](link k) noexcept { 143 | auto & y = cells[k]; 144 | cells[y.u].d = k; 145 | cells[y.d].u = k; 146 | }); 147 | }); 148 | } 149 | 150 | item dlx::best_item() const noexcept { 151 | item best = root(); 152 | uint32_t best_count = INT32_MAX; 153 | for (item i = items[root()].n; i != root(); i = items[i].n) { 154 | uint32_t count = items[i].count; 155 | if (count < best_count) { 156 | best_count = count; 157 | best = i; 158 | } 159 | } 160 | return best; 161 | } 162 | 163 | void dlx::reset() noexcept { 164 | for (auto i=stack.size(); i-- > 0;) unpick(stack[i]); 165 | result.clear(); 166 | stack.clear(); 167 | current_state = state::guessing; 168 | } 169 | 170 | bool dlx::next(item * & results, int & nresults) noexcept { 171 | for (;;) 172 | switch (current_state) { 173 | //case state::done: 174 | // current_state = state::guessing; 175 | // return false; 176 | 177 | case state::guessing: 178 | { 179 | item best = best_item(); 180 | if (best == root()) { 181 | current_state = state::backtracking; 182 | results = result.data(); 183 | nresults = result.size(); 184 | return true; 185 | } 186 | auto header = items[best].cell; 187 | auto candidate = cells[header].d; 188 | if (candidate == header) { 189 | current_state = state::backtracking; 190 | } else { 191 | stack.emplace_back(candidate); 192 | result.emplace_back(pick(candidate)); 193 | } 194 | break; 195 | } 196 | case state::backtracking: 197 | if (stack.size() == 0) { 198 | current_state = state::guessing; 199 | return false; 200 | } else { 201 | auto bad_choice = stack[stack.size()-1]; 202 | unpick(bad_choice); 203 | auto & bad = cells[bad_choice]; 204 | stack.pop_back(); 205 | result.pop_back(); 206 | auto header = items[bad.item].cell; 207 | if (bad.d != header) { 208 | stack.emplace_back(bad.d); 209 | result.emplace_back(pick(bad.d)); 210 | current_state = state::guessing; 211 | } 212 | break; 213 | } 214 | } 215 | } 216 | 217 | int dlx::count() noexcept { 218 | auto item = best_item(); 219 | if (item == root()) return 1; 220 | auto header = items[item].cell; 221 | auto candidate = cells[header].d; 222 | int n = 0; 223 | while (candidate != header) { 224 | auto row = pick(candidate); 225 | n += count(); 226 | unpick(row); 227 | candidate = cells[candidate].d; 228 | } 229 | return n; 230 | } 231 | -------------------------------------------------------------------------------- /cbits/dlx.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDED_DLX_HPP 2 | #define INCLUDED_DLX_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // compute exact covers using dancing links 12 | 13 | using namespace std; 14 | 15 | typedef std::uint32_t link; 16 | typedef std::uint32_t item; 17 | typedef std::uint32_t option; 18 | 19 | struct cell { 20 | std::uint32_t parity:1, item:31, u, d; 21 | cell(uint32_t parity=0, std::uint32_t item=0, link u=0, link d=0) 22 | : parity(parity), item(item), u(u), d(d) {} 23 | }; 24 | 25 | struct item_info { 26 | std::uint32_t p, n, cell, count; 27 | item_info(item p=0, item n=0, link cell=0, std::uint32_t count=0) 28 | : p(p), n(n), cell(cell), count(count) {} 29 | }; 30 | 31 | enum class state { 32 | guessing, backtracking 33 | }; 34 | 35 | struct dlx { 36 | std::vector cells; 37 | std::vector items; 38 | std::vector