kibi-swipl-linux-x64-gnu 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (550) hide show
  1. package/README.md +29 -0
  2. package/bin/swipl +0 -0
  3. package/bin/swipl-win +0 -0
  4. package/build-manifest.json +70 -0
  5. package/lib/cmake/swipl/SWIPLConfig.cmake +21 -0
  6. package/lib/cmake/swipl/SWIPLConfigVersion.cmake +65 -0
  7. package/lib/cmake/swipl/SWIPLTargets-release.cmake +28 -0
  8. package/lib/cmake/swipl/SWIPLTargets.cmake +109 -0
  9. package/lib/swipl/ABI +1 -0
  10. package/lib/swipl/LICENSE +41 -0
  11. package/lib/swipl/README.md +116 -0
  12. package/lib/swipl/app/README.md +25 -0
  13. package/lib/swipl/app/app.pl +87 -0
  14. package/lib/swipl/app/pack.pl +350 -0
  15. package/lib/swipl/app/qlf.pl +521 -0
  16. package/lib/swipl/app/splfr.pl +177 -0
  17. package/lib/swipl/app/sys.pl +310 -0
  18. package/lib/swipl/bin/swipl.home +1 -0
  19. package/lib/swipl/boot/apply.pl +73 -0
  20. package/lib/swipl/boot/attvar.pl +344 -0
  21. package/lib/swipl/boot/autoload.pl +1226 -0
  22. package/lib/swipl/boot/bags.pl +283 -0
  23. package/lib/swipl/boot/dcg.pl +388 -0
  24. package/lib/swipl/boot/dicts.pl +193 -0
  25. package/lib/swipl/boot/dwim.pl +485 -0
  26. package/lib/swipl/boot/engines.pl +152 -0
  27. package/lib/swipl/boot/expand.pl +1628 -0
  28. package/lib/swipl/boot/gc.pl +65 -0
  29. package/lib/swipl/boot/history.pl +304 -0
  30. package/lib/swipl/boot/init.pl +4682 -0
  31. package/lib/swipl/boot/iri.pl +64 -0
  32. package/lib/swipl/boot/license.pl +390 -0
  33. package/lib/swipl/boot/load.pl +81 -0
  34. package/lib/swipl/boot/messages.pl +2521 -0
  35. package/lib/swipl/boot/packs.pl +306 -0
  36. package/lib/swipl/boot/predopts.pl +154 -0
  37. package/lib/swipl/boot/qlf.pl +141 -0
  38. package/lib/swipl/boot/rc.pl +263 -0
  39. package/lib/swipl/boot/syspred.pl +1634 -0
  40. package/lib/swipl/boot/tabling.pl +2321 -0
  41. package/lib/swipl/boot/threads.pl +42 -0
  42. package/lib/swipl/boot/toplevel.pl +2248 -0
  43. package/lib/swipl/boot/topvars.pl +261 -0
  44. package/lib/swipl/boot.prc +0 -0
  45. package/lib/swipl/cmake/swipl.cmake +131 -0
  46. package/lib/swipl/customize/README.md +23 -0
  47. package/lib/swipl/customize/edit +101 -0
  48. package/lib/swipl/customize/init.pl +164 -0
  49. package/lib/swipl/demo/README.md +33 -0
  50. package/lib/swipl/demo/likes.pl +44 -0
  51. package/lib/swipl/doc/packages/examples/chr/bool.chr +281 -0
  52. package/lib/swipl/doc/packages/examples/chr/chrdif.chr +87 -0
  53. package/lib/swipl/doc/packages/examples/chr/chrfreeze.chr +6 -0
  54. package/lib/swipl/doc/packages/examples/chr/family.chr +116 -0
  55. package/lib/swipl/doc/packages/examples/chr/fib.chr +24 -0
  56. package/lib/swipl/doc/packages/examples/chr/fibonacci.chr +31 -0
  57. package/lib/swipl/doc/packages/examples/chr/gcd.chr +28 -0
  58. package/lib/swipl/doc/packages/examples/chr/leq.chr +34 -0
  59. package/lib/swipl/doc/packages/examples/chr/listdom.chr +138 -0
  60. package/lib/swipl/doc/packages/examples/chr/primes.chr +29 -0
  61. package/lib/swipl/doc/packages/examples/http/README.md +98 -0
  62. package/lib/swipl/doc/packages/examples/http/calc.pl +92 -0
  63. package/lib/swipl/doc/packages/examples/http/demo_body.pl +232 -0
  64. package/lib/swipl/doc/packages/examples/http/demo_client.pl +165 -0
  65. package/lib/swipl/doc/packages/examples/http/demo_daemon.pl +38 -0
  66. package/lib/swipl/doc/packages/examples/http/demo_files.pl +50 -0
  67. package/lib/swipl/doc/packages/examples/http/demo_hello.pl +15 -0
  68. package/lib/swipl/doc/packages/examples/http/demo_login.pl +280 -0
  69. package/lib/swipl/doc/packages/examples/http/demo_openid.pl +177 -0
  70. package/lib/swipl/doc/packages/examples/http/demo_pwp.pl +20 -0
  71. package/lib/swipl/doc/packages/examples/http/demo_rest.pl +56 -0
  72. package/lib/swipl/doc/packages/examples/http/demo_threads.pl +52 -0
  73. package/lib/swipl/doc/packages/examples/http/linux-init-script +126 -0
  74. package/lib/swipl/doc/packages/examples/http/pwp/context.pwp +23 -0
  75. package/lib/swipl/doc/packages/examples/http/pwp/index.pwp +25 -0
  76. package/lib/swipl/doc/packages/examples/http/pwp/pwp1.pwp +12 -0
  77. package/lib/swipl/doc/packages/examples/http/pwp/pwp2.pwp +10 -0
  78. package/lib/swipl/doc/packages/examples/http/pwp/pwp3.pwp +25 -0
  79. package/lib/swipl/doc/packages/examples/http/pwp/pwp4.pwp +23 -0
  80. package/lib/swipl/doc/packages/examples/http/pwp/pwp5.pwp +11 -0
  81. package/lib/swipl/doc/packages/examples/http/pwp/pwp6.pwp +3 -0
  82. package/lib/swipl/doc/packages/examples/http/pwp/pwp7.pwp +3 -0
  83. package/lib/swipl/doc/packages/examples/http/pwp/pwp8.pwp +3 -0
  84. package/lib/swipl/doc/packages/examples/http/pwp/pwpdb.pl +12 -0
  85. package/lib/swipl/doc/packages/examples/http/systemd-script.service +18 -0
  86. package/lib/swipl/doc/packages/examples/http/upstart-script.conf +20 -0
  87. package/lib/swipl/doc/packages/examples/plunit/read.pl +17 -0
  88. package/lib/swipl/doc/packages/examples/plunit/simple.pl +33 -0
  89. package/lib/swipl/doc/packages/examples/ssl/client.pl +83 -0
  90. package/lib/swipl/doc/packages/examples/ssl/etc/README +52 -0
  91. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-cert.p12 +0 -0
  92. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-cert.pem +85 -0
  93. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-key.pem +30 -0
  94. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/cacert.pem +82 -0
  95. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/careq.pem +18 -0
  96. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/crlnumber +1 -0
  97. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/index.txt +3 -0
  98. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/index.txt.attr +1 -0
  99. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/newcerts/9DC08797A0F63F7C.pem +85 -0
  100. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/newcerts/9DC08797A0F63F7D.pem +85 -0
  101. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/private/cakey.pem +30 -0
  102. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/serial +1 -0
  103. package/lib/swipl/doc/packages/examples/ssl/etc/server/server-cert.pem +85 -0
  104. package/lib/swipl/doc/packages/examples/ssl/etc/server/server-key.pem +30 -0
  105. package/lib/swipl/doc/packages/examples/ssl/https.pl +267 -0
  106. package/lib/swipl/doc/packages/examples/ssl/server.pl +106 -0
  107. package/lib/swipl/include/SWI-Prolog.h +1544 -0
  108. package/lib/swipl/include/SWI-Stream.h +537 -0
  109. package/lib/swipl/include/Yap/YapInterface.h +283 -0
  110. package/lib/swipl/include/sicstus/sicstus.h +452 -0
  111. package/lib/swipl/lib/x86_64-linux/cgi.so +0 -0
  112. package/lib/swipl/lib/x86_64-linux/crypt.so +0 -0
  113. package/lib/swipl/lib/x86_64-linux/crypto4pl.so +0 -0
  114. package/lib/swipl/lib/x86_64-linux/double_metaphone.so +0 -0
  115. package/lib/swipl/lib/x86_64-linux/files.so +0 -0
  116. package/lib/swipl/lib/x86_64-linux/hashstream.so +0 -0
  117. package/lib/swipl/lib/x86_64-linux/http_stream.so +0 -0
  118. package/lib/swipl/lib/x86_64-linux/isub.so +0 -0
  119. package/lib/swipl/lib/x86_64-linux/json.so +0 -0
  120. package/lib/swipl/lib/x86_64-linux/libswipl.so.10 +0 -0
  121. package/lib/swipl/lib/x86_64-linux/mallocinfo.so +0 -0
  122. package/lib/swipl/lib/x86_64-linux/md54pl.so +0 -0
  123. package/lib/swipl/lib/x86_64-linux/memfile.so +0 -0
  124. package/lib/swipl/lib/x86_64-linux/ntriples.so +0 -0
  125. package/lib/swipl/lib/x86_64-linux/pcre4pl.so +0 -0
  126. package/lib/swipl/lib/x86_64-linux/porter_stem.so +0 -0
  127. package/lib/swipl/lib/x86_64-linux/process.so +0 -0
  128. package/lib/swipl/lib/x86_64-linux/prolog_stream.so +0 -0
  129. package/lib/swipl/lib/x86_64-linux/rdf_db.so +0 -0
  130. package/lib/swipl/lib/x86_64-linux/readutil.so +0 -0
  131. package/lib/swipl/lib/x86_64-linux/rlimit.so +0 -0
  132. package/lib/swipl/lib/x86_64-linux/sched.so +0 -0
  133. package/lib/swipl/lib/x86_64-linux/sgml2pl.so +0 -0
  134. package/lib/swipl/lib/x86_64-linux/sha4pl.so +0 -0
  135. package/lib/swipl/lib/x86_64-linux/snowball.so +0 -0
  136. package/lib/swipl/lib/x86_64-linux/socket.so +0 -0
  137. package/lib/swipl/lib/x86_64-linux/ssl4pl.so +0 -0
  138. package/lib/swipl/lib/x86_64-linux/streaminfo.so +0 -0
  139. package/lib/swipl/lib/x86_64-linux/syslog.so +0 -0
  140. package/lib/swipl/lib/x86_64-linux/time.so +0 -0
  141. package/lib/swipl/lib/x86_64-linux/turtle.so +0 -0
  142. package/lib/swipl/lib/x86_64-linux/uid.so +0 -0
  143. package/lib/swipl/lib/x86_64-linux/unix.so +0 -0
  144. package/lib/swipl/lib/x86_64-linux/uri.so +0 -0
  145. package/lib/swipl/lib/x86_64-linux/websocket.so +0 -0
  146. package/lib/swipl/lib/x86_64-linux/zlib4pl.so +0 -0
  147. package/lib/swipl/library/INDEX.pl +1020 -0
  148. package/lib/swipl/library/aggregate.pl +755 -0
  149. package/lib/swipl/library/ansi_term.pl +653 -0
  150. package/lib/swipl/library/apply.pl +426 -0
  151. package/lib/swipl/library/apply_macros.pl +497 -0
  152. package/lib/swipl/library/arithmetic.pl +320 -0
  153. package/lib/swipl/library/assoc.pl +522 -0
  154. package/lib/swipl/library/atom.pl +169 -0
  155. package/lib/swipl/library/backcomp.pl +688 -0
  156. package/lib/swipl/library/base32.pl +291 -0
  157. package/lib/swipl/library/base64.pl +441 -0
  158. package/lib/swipl/library/broadcast.pl +161 -0
  159. package/lib/swipl/library/build/cmake.pl +137 -0
  160. package/lib/swipl/library/build/conan.pl +232 -0
  161. package/lib/swipl/library/build/make.pl +168 -0
  162. package/lib/swipl/library/build/tools.pl +825 -0
  163. package/lib/swipl/library/charsio.pl +195 -0
  164. package/lib/swipl/library/check.pl +1097 -0
  165. package/lib/swipl/library/check_installation.pl +912 -0
  166. package/lib/swipl/library/clp/INDEX.pl +157 -0
  167. package/lib/swipl/library/clp/bounds.pl +2036 -0
  168. package/lib/swipl/library/clp/clp_distinct.pl +263 -0
  169. package/lib/swipl/library/clp/clp_events.pl +92 -0
  170. package/lib/swipl/library/clp/clpb.pl +1896 -0
  171. package/lib/swipl/library/clp/clpfd.pl +7906 -0
  172. package/lib/swipl/library/clp/simplex.pl +1580 -0
  173. package/lib/swipl/library/codesio.pl +182 -0
  174. package/lib/swipl/library/coinduction.pl +183 -0
  175. package/lib/swipl/library/console_input.pl +289 -0
  176. package/lib/swipl/library/csv.pl +611 -0
  177. package/lib/swipl/library/ctypes.pl +161 -0
  178. package/lib/swipl/library/date.pl +370 -0
  179. package/lib/swipl/library/dcg/INDEX.pl +35 -0
  180. package/lib/swipl/library/dcg/basics.pl +514 -0
  181. package/lib/swipl/library/dcg/high_order.pl +227 -0
  182. package/lib/swipl/library/debug.pl +499 -0
  183. package/lib/swipl/library/dialect/bim.pl +186 -0
  184. package/lib/swipl/library/dialect/commons.pl +77 -0
  185. package/lib/swipl/library/dialect/eclipse/test_util_iso.pl +355 -0
  186. package/lib/swipl/library/dialect/hprolog/format.pl +44 -0
  187. package/lib/swipl/library/dialect/hprolog.pl +314 -0
  188. package/lib/swipl/library/dialect/ifprolog.pl +1317 -0
  189. package/lib/swipl/library/dialect/iso/iso_predicates.pl +277 -0
  190. package/lib/swipl/library/dialect/sicstus/README.TXT +1 -0
  191. package/lib/swipl/library/dialect/sicstus/arrays.pl +121 -0
  192. package/lib/swipl/library/dialect/sicstus/block.pl +278 -0
  193. package/lib/swipl/library/dialect/sicstus/lists.pl +167 -0
  194. package/lib/swipl/library/dialect/sicstus/ordsets.pl +52 -0
  195. package/lib/swipl/library/dialect/sicstus/sockets.pl +174 -0
  196. package/lib/swipl/library/dialect/sicstus/system.pl +262 -0
  197. package/lib/swipl/library/dialect/sicstus/terms.pl +57 -0
  198. package/lib/swipl/library/dialect/sicstus/timeout.pl +92 -0
  199. package/lib/swipl/library/dialect/sicstus.pl +582 -0
  200. package/lib/swipl/library/dialect/sicstus4/aggregate.pl +60 -0
  201. package/lib/swipl/library/dialect/sicstus4/between.pl +62 -0
  202. package/lib/swipl/library/dialect/sicstus4/clpfd.pl +75 -0
  203. package/lib/swipl/library/dialect/sicstus4/file_systems.pl +413 -0
  204. package/lib/swipl/library/dialect/sicstus4/lists.pl +274 -0
  205. package/lib/swipl/library/dialect/sicstus4/ordsets.pl +54 -0
  206. package/lib/swipl/library/dialect/sicstus4/samsort.pl +86 -0
  207. package/lib/swipl/library/dialect/sicstus4/sets.pl +124 -0
  208. package/lib/swipl/library/dialect/sicstus4/sockets.pl +164 -0
  209. package/lib/swipl/library/dialect/sicstus4/system.pl +80 -0
  210. package/lib/swipl/library/dialect/sicstus4/terms.pl +89 -0
  211. package/lib/swipl/library/dialect/sicstus4/timeout.pl +43 -0
  212. package/lib/swipl/library/dialect/sicstus4/types.pl +103 -0
  213. package/lib/swipl/library/dialect/sicstus4.pl +200 -0
  214. package/lib/swipl/library/dialect/swi/syspred_options.pl +250 -0
  215. package/lib/swipl/library/dialect/xsb/README.md +20 -0
  216. package/lib/swipl/library/dialect/xsb/basics.pl +318 -0
  217. package/lib/swipl/library/dialect/xsb/consult.pl +87 -0
  218. package/lib/swipl/library/dialect/xsb/curr_sym.pl +57 -0
  219. package/lib/swipl/library/dialect/xsb/error_handler.pl +111 -0
  220. package/lib/swipl/library/dialect/xsb/gensym.pl +53 -0
  221. package/lib/swipl/library/dialect/xsb/gpp.pl +138 -0
  222. package/lib/swipl/library/dialect/xsb/intern.pl +48 -0
  223. package/lib/swipl/library/dialect/xsb/lists.pl +47 -0
  224. package/lib/swipl/library/dialect/xsb/machine.pl +295 -0
  225. package/lib/swipl/library/dialect/xsb/ordsets.pl +53 -0
  226. package/lib/swipl/library/dialect/xsb/setof.pl +54 -0
  227. package/lib/swipl/library/dialect/xsb/source.pl +308 -0
  228. package/lib/swipl/library/dialect/xsb/standard.pl +169 -0
  229. package/lib/swipl/library/dialect/xsb/storage.pl +70 -0
  230. package/lib/swipl/library/dialect/xsb/string.pl +76 -0
  231. package/lib/swipl/library/dialect/xsb/thread.pl +62 -0
  232. package/lib/swipl/library/dialect/xsb/timed_call.pl +158 -0
  233. package/lib/swipl/library/dialect/xsb.pl +688 -0
  234. package/lib/swipl/library/dialect/yap/README.TXT +8 -0
  235. package/lib/swipl/library/dialect/yap.pl +283 -0
  236. package/lib/swipl/library/dialect.pl +123 -0
  237. package/lib/swipl/library/dicts.pl +399 -0
  238. package/lib/swipl/library/dif.pl +426 -0
  239. package/lib/swipl/library/edinburgh.pl +138 -0
  240. package/lib/swipl/library/edit.pl +783 -0
  241. package/lib/swipl/library/error.pl +495 -0
  242. package/lib/swipl/library/exceptions.pl +231 -0
  243. package/lib/swipl/library/explain.pl +543 -0
  244. package/lib/swipl/library/ext/RDF/INDEX.pl +25 -0
  245. package/lib/swipl/library/ext/RDF/rdf.pl +450 -0
  246. package/lib/swipl/library/ext/RDF/rdf_parser.pl +649 -0
  247. package/lib/swipl/library/ext/RDF/rdf_triple.pl +448 -0
  248. package/lib/swipl/library/ext/RDF/rdf_write.pl +701 -0
  249. package/lib/swipl/library/ext/RDF/rewrite_term.pl +177 -0
  250. package/lib/swipl/library/ext/chr/chr/a_star.pl +80 -0
  251. package/lib/swipl/library/ext/chr/chr/binomialheap.pl +142 -0
  252. package/lib/swipl/library/ext/chr/chr/builtins.pl +629 -0
  253. package/lib/swipl/library/ext/chr/chr/chr_compiler_errors.pl +179 -0
  254. package/lib/swipl/library/ext/chr/chr/chr_compiler_options.pl +382 -0
  255. package/lib/swipl/library/ext/chr/chr/chr_compiler_utility.pl +338 -0
  256. package/lib/swipl/library/ext/chr/chr/chr_debug.pl +59 -0
  257. package/lib/swipl/library/ext/chr/chr/chr_hashtable_store.pl +425 -0
  258. package/lib/swipl/library/ext/chr/chr/chr_integertable_store.pl +139 -0
  259. package/lib/swipl/library/ext/chr/chr/chr_messages.pl +176 -0
  260. package/lib/swipl/library/ext/chr/chr/chr_op.pl +53 -0
  261. package/lib/swipl/library/ext/chr/chr/chr_runtime.pl +1016 -0
  262. package/lib/swipl/library/ext/chr/chr/chr_translate.pl +25424 -0
  263. package/lib/swipl/library/ext/chr/chr/clean_code.pl +252 -0
  264. package/lib/swipl/library/ext/chr/chr/find.pl +78 -0
  265. package/lib/swipl/library/ext/chr/chr/guard_entailment.pl +19432 -0
  266. package/lib/swipl/library/ext/chr/chr/listmap.pl +108 -0
  267. package/lib/swipl/library/ext/chr/chr/pairlist.pl +78 -0
  268. package/lib/swipl/library/ext/chr/chr.pl +686 -0
  269. package/lib/swipl/library/ext/clib/INDEX.pl +158 -0
  270. package/lib/swipl/library/ext/clib/cgi.pl +86 -0
  271. package/lib/swipl/library/ext/clib/crypt.pl +44 -0
  272. package/lib/swipl/library/ext/clib/filesex.pl +644 -0
  273. package/lib/swipl/library/ext/clib/hash_stream.pl +119 -0
  274. package/lib/swipl/library/ext/clib/mallocinfo.pl +138 -0
  275. package/lib/swipl/library/ext/clib/md5.pl +68 -0
  276. package/lib/swipl/library/ext/clib/memfile.pl +61 -0
  277. package/lib/swipl/library/ext/clib/process.pl +573 -0
  278. package/lib/swipl/library/ext/clib/prolog_server.pl +157 -0
  279. package/lib/swipl/library/ext/clib/prolog_stream.pl +80 -0
  280. package/lib/swipl/library/ext/clib/rlimit.pl +54 -0
  281. package/lib/swipl/library/ext/clib/sched.pl +85 -0
  282. package/lib/swipl/library/ext/clib/sha.pl +162 -0
  283. package/lib/swipl/library/ext/clib/socket.pl +1102 -0
  284. package/lib/swipl/library/ext/clib/streaminfo.pl +103 -0
  285. package/lib/swipl/library/ext/clib/streampool.pl +136 -0
  286. package/lib/swipl/library/ext/clib/syslog.pl +180 -0
  287. package/lib/swipl/library/ext/clib/time.pl +194 -0
  288. package/lib/swipl/library/ext/clib/udp_broadcast.pl +975 -0
  289. package/lib/swipl/library/ext/clib/uid.pl +220 -0
  290. package/lib/swipl/library/ext/clib/unix.pl +317 -0
  291. package/lib/swipl/library/ext/clib/uri.pl +530 -0
  292. package/lib/swipl/library/ext/clib/uuid.pl +207 -0
  293. package/lib/swipl/library/ext/http/http/INDEX.pl +266 -0
  294. package/lib/swipl/library/ext/http/http/README.md +34 -0
  295. package/lib/swipl/library/ext/http/http/ax.pl +189 -0
  296. package/lib/swipl/library/ext/http/http/dcg_basics.pl +49 -0
  297. package/lib/swipl/library/ext/http/http/graphql.pl +1367 -0
  298. package/lib/swipl/library/ext/http/http/html_decl.pl +416 -0
  299. package/lib/swipl/library/ext/http/http/html_head.pl +654 -0
  300. package/lib/swipl/library/ext/http/http/html_quasiquotations.pl +120 -0
  301. package/lib/swipl/library/ext/http/http/html_write.pl +1425 -0
  302. package/lib/swipl/library/ext/http/http/htmx.pl +139 -0
  303. package/lib/swipl/library/ext/http/http/http_authenticate.pl +364 -0
  304. package/lib/swipl/library/ext/http/http/http_client.pl +412 -0
  305. package/lib/swipl/library/ext/http/http/http_cookie.pl +245 -0
  306. package/lib/swipl/library/ext/http/http/http_cors.pl +176 -0
  307. package/lib/swipl/library/ext/http/http/http_digest.pl +820 -0
  308. package/lib/swipl/library/ext/http/http/http_dirindex.pl +296 -0
  309. package/lib/swipl/library/ext/http/http/http_dispatch.pl +1552 -0
  310. package/lib/swipl/library/ext/http/http/http_dyn_workers.pl +214 -0
  311. package/lib/swipl/library/ext/http/http/http_error.pl +126 -0
  312. package/lib/swipl/library/ext/http/http/http_exception.pl +225 -0
  313. package/lib/swipl/library/ext/http/http/http_files.pl +163 -0
  314. package/lib/swipl/library/ext/http/http/http_header.pl +2820 -0
  315. package/lib/swipl/library/ext/http/http/http_hook.pl +106 -0
  316. package/lib/swipl/library/ext/http/http/http_host.pl +204 -0
  317. package/lib/swipl/library/ext/http/http/http_load.pl +113 -0
  318. package/lib/swipl/library/ext/http/http/http_log.pl +738 -0
  319. package/lib/swipl/library/ext/http/http/http_multipart_plugin.pl +232 -0
  320. package/lib/swipl/library/ext/http/http/http_open.pl +1821 -0
  321. package/lib/swipl/library/ext/http/http/http_openid.pl +1561 -0
  322. package/lib/swipl/library/ext/http/http/http_parameters.pl +504 -0
  323. package/lib/swipl/library/ext/http/http/http_path.pl +380 -0
  324. package/lib/swipl/library/ext/http/http/http_proxy.pl +80 -0
  325. package/lib/swipl/library/ext/http/http/http_pwp.pl +351 -0
  326. package/lib/swipl/library/ext/http/http/http_redis_plugin.pl +332 -0
  327. package/lib/swipl/library/ext/http/http/http_server.pl +105 -0
  328. package/lib/swipl/library/ext/http/http/http_server_files.pl +126 -0
  329. package/lib/swipl/library/ext/http/http/http_server_health.pl +186 -0
  330. package/lib/swipl/library/ext/http/http/http_session.pl +1040 -0
  331. package/lib/swipl/library/ext/http/http/http_sgml_plugin.pl +137 -0
  332. package/lib/swipl/library/ext/http/http/http_stream.pl +376 -0
  333. package/lib/swipl/library/ext/http/http/http_unix_daemon.pl +989 -0
  334. package/lib/swipl/library/ext/http/http/http_wrapper.pl +559 -0
  335. package/lib/swipl/library/ext/http/http/hub.pl +600 -0
  336. package/lib/swipl/library/ext/http/http/js_write.pl +461 -0
  337. package/lib/swipl/library/ext/http/http/mimepack.pl +224 -0
  338. package/lib/swipl/library/ext/http/http/mimetype.pl +276 -0
  339. package/lib/swipl/library/ext/http/http/term_html.pl +797 -0
  340. package/lib/swipl/library/ext/http/http/thread_httpd.pl +1181 -0
  341. package/lib/swipl/library/ext/http/http/web/css/dirindex.css +24 -0
  342. package/lib/swipl/library/ext/http/http/web/css/openid.css +57 -0
  343. package/lib/swipl/library/ext/http/http/web/icons/back.png +0 -0
  344. package/lib/swipl/library/ext/http/http/web/icons/c.png +0 -0
  345. package/lib/swipl/library/ext/http/http/web/icons/compressed.png +0 -0
  346. package/lib/swipl/library/ext/http/http/web/icons/folder.png +0 -0
  347. package/lib/swipl/library/ext/http/http/web/icons/generic.png +0 -0
  348. package/lib/swipl/library/ext/http/http/web/icons/layout.png +0 -0
  349. package/lib/swipl/library/ext/http/http/web/icons/openid-logo-square.png +0 -0
  350. package/lib/swipl/library/ext/http/http/web/icons/openid-logo-tiny.png +0 -0
  351. package/lib/swipl/library/ext/http/http/websocket.pl +729 -0
  352. package/lib/swipl/library/ext/http/http/yadis.pl +205 -0
  353. package/lib/swipl/library/ext/json/INDEX.pl +36 -0
  354. package/lib/swipl/library/ext/json/http/INDEX.pl +16 -0
  355. package/lib/swipl/library/ext/json/http/http_json.pl +490 -0
  356. package/lib/swipl/library/ext/json/http/js_grammar.pl +53 -0
  357. package/lib/swipl/library/ext/json/http/json.pl +48 -0
  358. package/lib/swipl/library/ext/json/http/json_convert.pl +48 -0
  359. package/lib/swipl/library/ext/json/json.pl +1215 -0
  360. package/lib/swipl/library/ext/json/json_convert.pl +681 -0
  361. package/lib/swipl/library/ext/json/json_grammar.pl +349 -0
  362. package/lib/swipl/library/ext/json/json_rpc_client.pl +423 -0
  363. package/lib/swipl/library/ext/json/json_rpc_common.pl +104 -0
  364. package/lib/swipl/library/ext/json/json_rpc_server.pl +489 -0
  365. package/lib/swipl/library/ext/json/json_schema.pl +2111 -0
  366. package/lib/swipl/library/ext/nlp/INDEX.pl +15 -0
  367. package/lib/swipl/library/ext/nlp/double_metaphone.pl +86 -0
  368. package/lib/swipl/library/ext/nlp/isub.pl +162 -0
  369. package/lib/swipl/library/ext/nlp/porter_stem.pl +50 -0
  370. package/lib/swipl/library/ext/nlp/snowball.pl +97 -0
  371. package/lib/swipl/library/ext/pcre/INDEX.pl +17 -0
  372. package/lib/swipl/library/ext/pcre/pcre.pl +974 -0
  373. package/lib/swipl/library/ext/plunit/INDEX.pl +20 -0
  374. package/lib/swipl/library/ext/plunit/plunit.pl +2631 -0
  375. package/lib/swipl/library/ext/plunit/test_wizard.pl +249 -0
  376. package/lib/swipl/library/ext/semweb/semweb/INDEX.pl +288 -0
  377. package/lib/swipl/library/ext/semweb/semweb/rdf11.pl +2106 -0
  378. package/lib/swipl/library/ext/semweb/semweb/rdf11_containers.pl +299 -0
  379. package/lib/swipl/library/ext/semweb/semweb/rdf_cache.pl +278 -0
  380. package/lib/swipl/library/ext/semweb/semweb/rdf_compare.pl +132 -0
  381. package/lib/swipl/library/ext/semweb/semweb/rdf_db.pl +3468 -0
  382. package/lib/swipl/library/ext/semweb/semweb/rdf_edit.pl +1145 -0
  383. package/lib/swipl/library/ext/semweb/semweb/rdf_http_plugin.pl +274 -0
  384. package/lib/swipl/library/ext/semweb/semweb/rdf_library.pl +1021 -0
  385. package/lib/swipl/library/ext/semweb/semweb/rdf_litindex.pl +1035 -0
  386. package/lib/swipl/library/ext/semweb/semweb/rdf_ntriples.pl +422 -0
  387. package/lib/swipl/library/ext/semweb/semweb/rdf_persistency.pl +1576 -0
  388. package/lib/swipl/library/ext/semweb/semweb/rdf_portray.pl +182 -0
  389. package/lib/swipl/library/ext/semweb/semweb/rdf_prefixes.pl +681 -0
  390. package/lib/swipl/library/ext/semweb/semweb/rdf_sandbox.pl +153 -0
  391. package/lib/swipl/library/ext/semweb/semweb/rdf_turtle.pl +54 -0
  392. package/lib/swipl/library/ext/semweb/semweb/rdf_turtle_write.pl +1587 -0
  393. package/lib/swipl/library/ext/semweb/semweb/rdf_zlib_plugin.pl +52 -0
  394. package/lib/swipl/library/ext/semweb/semweb/rdfa.pl +1510 -0
  395. package/lib/swipl/library/ext/semweb/semweb/rdfs.pl +475 -0
  396. package/lib/swipl/library/ext/semweb/semweb/sparql_client.pl +643 -0
  397. package/lib/swipl/library/ext/semweb/semweb/turtle.pl +455 -0
  398. package/lib/swipl/library/ext/sgml/DTD/HTML4.dcl +88 -0
  399. package/lib/swipl/library/ext/sgml/DTD/HTML4.dtd +1092 -0
  400. package/lib/swipl/library/ext/sgml/DTD/HTML4.soc +6 -0
  401. package/lib/swipl/library/ext/sgml/DTD/HTML5.dtd +906 -0
  402. package/lib/swipl/library/ext/sgml/DTD/HTMLlat1.ent +195 -0
  403. package/lib/swipl/library/ext/sgml/DTD/HTMLspec.ent +77 -0
  404. package/lib/swipl/library/ext/sgml/DTD/HTMLsym.ent +241 -0
  405. package/lib/swipl/library/ext/sgml/INDEX.pl +64 -0
  406. package/lib/swipl/library/ext/sgml/c14n2.pl +203 -0
  407. package/lib/swipl/library/ext/sgml/iso_639.pl +631 -0
  408. package/lib/swipl/library/ext/sgml/pwp.pl +705 -0
  409. package/lib/swipl/library/ext/sgml/sgml.pl +815 -0
  410. package/lib/swipl/library/ext/sgml/sgml_write.pl +1003 -0
  411. package/lib/swipl/library/ext/sgml/xpath.pl +620 -0
  412. package/lib/swipl/library/ext/sgml/xsdp_types.pl +232 -0
  413. package/lib/swipl/library/ext/ssl/INDEX.pl +60 -0
  414. package/lib/swipl/library/ext/ssl/crypto.pl +1015 -0
  415. package/lib/swipl/library/ext/ssl/http/INDEX.pl +5 -0
  416. package/lib/swipl/library/ext/ssl/http/http_ssl_plugin.pl +296 -0
  417. package/lib/swipl/library/ext/ssl/saml.pl +609 -0
  418. package/lib/swipl/library/ext/ssl/ssl.pl +647 -0
  419. package/lib/swipl/library/ext/ssl/xmldsig.pl +391 -0
  420. package/lib/swipl/library/ext/ssl/xmlenc.pl +354 -0
  421. package/lib/swipl/library/ext/zlib/INDEX.pl +8 -0
  422. package/lib/swipl/library/ext/zlib/zlib.pl +112 -0
  423. package/lib/swipl/library/fastrw.pl +105 -0
  424. package/lib/swipl/library/files.pl +80 -0
  425. package/lib/swipl/library/gensym.pl +111 -0
  426. package/lib/swipl/library/git.pl +1027 -0
  427. package/lib/swipl/library/hashtable.pl +365 -0
  428. package/lib/swipl/library/heaps.pl +252 -0
  429. package/lib/swipl/library/hotfix.pl +239 -0
  430. package/lib/swipl/library/http/jquery.pl +103 -0
  431. package/lib/swipl/library/http/web/js/jquery-3.6.0.min.js +2 -0
  432. package/lib/swipl/library/increval.pl +217 -0
  433. package/lib/swipl/library/intercept.pl +263 -0
  434. package/lib/swipl/library/iostream.pl +237 -0
  435. package/lib/swipl/library/iri_scheme/file.pl +70 -0
  436. package/lib/swipl/library/lazy_lists.pl +481 -0
  437. package/lib/swipl/library/listing.pl +1362 -0
  438. package/lib/swipl/library/lists.pl +860 -0
  439. package/lib/swipl/library/lynx/INDEX.pl +14 -0
  440. package/lib/swipl/library/lynx/format.pl +380 -0
  441. package/lib/swipl/library/lynx/html_style.pl +129 -0
  442. package/lib/swipl/library/lynx/html_text.pl +865 -0
  443. package/lib/swipl/library/lynx/pldoc_style.pl +87 -0
  444. package/lib/swipl/library/macros.pl +451 -0
  445. package/lib/swipl/library/main.pl +1200 -0
  446. package/lib/swipl/library/make.pl +197 -0
  447. package/lib/swipl/library/modules.pl +129 -0
  448. package/lib/swipl/library/nb_rbtrees.pl +267 -0
  449. package/lib/swipl/library/nb_set.pl +203 -0
  450. package/lib/swipl/library/obfuscate.pl +115 -0
  451. package/lib/swipl/library/occurs.pl +241 -0
  452. package/lib/swipl/library/operators.pl +182 -0
  453. package/lib/swipl/library/option.pl +388 -0
  454. package/lib/swipl/library/optparse.pl +966 -0
  455. package/lib/swipl/library/ordsets.pl +518 -0
  456. package/lib/swipl/library/oset.pl +160 -0
  457. package/lib/swipl/library/pairs.pl +187 -0
  458. package/lib/swipl/library/persistency.pl +713 -0
  459. package/lib/swipl/library/pio.pl +47 -0
  460. package/lib/swipl/library/portray_text.pl +277 -0
  461. package/lib/swipl/library/pprint.pl +940 -0
  462. package/lib/swipl/library/predicate_options.pl +975 -0
  463. package/lib/swipl/library/prolog_autoload.pl +233 -0
  464. package/lib/swipl/library/prolog_breakpoints.pl +396 -0
  465. package/lib/swipl/library/prolog_clause.pl +1104 -0
  466. package/lib/swipl/library/prolog_code.pl +416 -0
  467. package/lib/swipl/library/prolog_codewalk.pl +1248 -0
  468. package/lib/swipl/library/prolog_colour.pl +3301 -0
  469. package/lib/swipl/library/prolog_colour.qlf +0 -0
  470. package/lib/swipl/library/prolog_config.pl +154 -0
  471. package/lib/swipl/library/prolog_coverage.pl +1333 -0
  472. package/lib/swipl/library/prolog_debug.pl +404 -0
  473. package/lib/swipl/library/prolog_deps.pl +675 -0
  474. package/lib/swipl/library/prolog_evaluable.pl +234 -0
  475. package/lib/swipl/library/prolog_format.pl +219 -0
  476. package/lib/swipl/library/prolog_history.pl +161 -0
  477. package/lib/swipl/library/prolog_jiti.pl +333 -0
  478. package/lib/swipl/library/prolog_locale.pl +61 -0
  479. package/lib/swipl/library/prolog_metainference.pl +307 -0
  480. package/lib/swipl/library/prolog_pack.pl +3906 -0
  481. package/lib/swipl/library/prolog_profile.pl +346 -0
  482. package/lib/swipl/library/prolog_qlfmake.pl +533 -0
  483. package/lib/swipl/library/prolog_source.pl +1188 -0
  484. package/lib/swipl/library/prolog_stack.pl +794 -0
  485. package/lib/swipl/library/prolog_trace.pl +365 -0
  486. package/lib/swipl/library/prolog_versions.pl +347 -0
  487. package/lib/swipl/library/prolog_wrap.pl +164 -0
  488. package/lib/swipl/library/prolog_xref.pl +3090 -0
  489. package/lib/swipl/library/prolog_xref.qlf +0 -0
  490. package/lib/swipl/library/pure_input.pl +266 -0
  491. package/lib/swipl/library/qpforeign.pl +695 -0
  492. package/lib/swipl/library/qsave.pl +1516 -0
  493. package/lib/swipl/library/quasi_quotations.pl +300 -0
  494. package/lib/swipl/library/quintus.pl +437 -0
  495. package/lib/swipl/library/random.pl +459 -0
  496. package/lib/swipl/library/rbtrees.pl +1153 -0
  497. package/lib/swipl/library/readln.pl +256 -0
  498. package/lib/swipl/library/readutil.pl +304 -0
  499. package/lib/swipl/library/record.pl +544 -0
  500. package/lib/swipl/library/rwlocks.pl +172 -0
  501. package/lib/swipl/library/sandbox.pl +1384 -0
  502. package/lib/swipl/library/semweb/dc.rdfs +198 -0
  503. package/lib/swipl/library/semweb/eor.rdfs +95 -0
  504. package/lib/swipl/library/semweb/owl.owl +287 -0
  505. package/lib/swipl/library/semweb/rdf_library.ttl +90 -0
  506. package/lib/swipl/library/semweb/rdfs.rdfs +251 -0
  507. package/lib/swipl/library/settings.pl +755 -0
  508. package/lib/swipl/library/shell.pl +462 -0
  509. package/lib/swipl/library/shlib.pl +670 -0
  510. package/lib/swipl/library/solution_sequences.pl +439 -0
  511. package/lib/swipl/library/sort.pl +119 -0
  512. package/lib/swipl/library/statistics.pl +541 -0
  513. package/lib/swipl/library/streams.pl +118 -0
  514. package/lib/swipl/library/strings.pl +469 -0
  515. package/lib/swipl/library/system.pl +95 -0
  516. package/lib/swipl/library/tables.pl +381 -0
  517. package/lib/swipl/library/tableutil.pl +771 -0
  518. package/lib/swipl/library/tabling.pl +42 -0
  519. package/lib/swipl/library/terms.pl +457 -0
  520. package/lib/swipl/library/theme/auto.pl +61 -0
  521. package/lib/swipl/library/theme/dark.pl +455 -0
  522. package/lib/swipl/library/theme/light.pl +45 -0
  523. package/lib/swipl/library/thread.pl +810 -0
  524. package/lib/swipl/library/thread_pool.pl +508 -0
  525. package/lib/swipl/library/threadutil.pl +574 -0
  526. package/lib/swipl/library/tty.pl +316 -0
  527. package/lib/swipl/library/ugraphs.pl +644 -0
  528. package/lib/swipl/library/unicode/INDEX.pl +6 -0
  529. package/lib/swipl/library/unicode/blocks.pl +414 -0
  530. package/lib/swipl/library/url.pl +1129 -0
  531. package/lib/swipl/library/utf8.pl +145 -0
  532. package/lib/swipl/library/varnumbers.pl +221 -0
  533. package/lib/swipl/library/vm.pl +251 -0
  534. package/lib/swipl/library/wfs.pl +205 -0
  535. package/lib/swipl/library/when.pl +263 -0
  536. package/lib/swipl/library/writef.pl +278 -0
  537. package/lib/swipl/library/www_browser.pl +251 -0
  538. package/lib/swipl/library/yall.pl +574 -0
  539. package/lib/swipl/library/zip.pl +211 -0
  540. package/lib/swipl/swipl.home +1 -0
  541. package/lib/vendor/libcrypto.so.3 +0 -0
  542. package/lib/vendor/libpcre2-8.so.0 +0 -0
  543. package/lib/vendor/libssl.so.3 +0 -0
  544. package/lib/vendor/libz.so.1 +0 -0
  545. package/licenses/OpenSSL-LICENSE.txt +177 -0
  546. package/licenses/PCRE2-COPYING +5 -0
  547. package/licenses/README.md +12 -0
  548. package/licenses/SWI-Prolog-LICENSE +41 -0
  549. package/licenses/zlib-LICENSE +22 -0
  550. package/package.json +27 -0
@@ -0,0 +1,1896 @@
1
+ /* Part of SWI-Prolog
2
+
3
+ Author: Markus Triska
4
+ E-mail: triska@metalevel.at
5
+ WWW: http://www.swi-prolog.org
6
+ Copyright (C): 2014-2018 Markus Triska
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without
10
+ modification, are permitted provided that the following conditions
11
+ are met:
12
+
13
+ 1. Redistributions of source code must retain the above copyright
14
+ notice, this list of conditions and the following disclaimer.
15
+
16
+ 2. Redistributions in binary form must reproduce the above copyright
17
+ notice, this list of conditions and the following disclaimer in
18
+ the documentation and/or other materials provided with the
19
+ distribution.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ POSSIBILITY OF SUCH DAMAGE.
33
+ */
34
+
35
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36
+ CLP(B): Constraint Logic Programming over Boolean variables.
37
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
38
+
39
+ :- module(clpb, [
40
+ op(300, fy, ~),
41
+ op(500, yfx, #),
42
+ sat/1,
43
+ taut/2,
44
+ labeling/1,
45
+ sat_count/2,
46
+ weighted_maximum/3,
47
+ random_labeling/2
48
+ ]).
49
+
50
+ :- use_module(library(error)).
51
+ :- use_module(library(assoc)).
52
+ :- use_module(library(apply_macros)).
53
+
54
+ :- create_prolog_flag(clpb_monotonic, false, []).
55
+ :- create_prolog_flag(clpb_residuals, default, []).
56
+
57
+ /** <module> CLP(B): Constraint Logic Programming over Boolean Variables
58
+
59
+ ## Introduction {#clpb-intro}
60
+
61
+ This library provides CLP(B), Constraint Logic Programming over
62
+ Boolean variables. It can be used to model and solve combinatorial
63
+ problems such as verification, allocation and covering tasks.
64
+
65
+ CLP(B) is an instance of the general [CLP(_X_) scheme](<#clp>),
66
+ extending logic programming with reasoning over specialised domains.
67
+
68
+ The implementation is based on reduced and ordered Binary Decision
69
+ Diagrams (BDDs).
70
+
71
+ Benchmarks and usage examples of this library are available from:
72
+ [__https://www.metalevel.at/clpb/__](https://www.metalevel.at/clpb/)
73
+
74
+ We recommend the following references for citing this library in
75
+ scientific publications:
76
+
77
+ ==
78
+ @inproceedings{Triska2016,
79
+ author = "Markus Triska",
80
+ title = "The {Boolean} Constraint Solver of {SWI-Prolog}:
81
+ System Description",
82
+ booktitle = "FLOPS",
83
+ series = "LNCS",
84
+ volume = 9613,
85
+ year = 2016,
86
+ pages = "45--61"
87
+ }
88
+
89
+ @article{Triska2018,
90
+ title = "Boolean constraints in {SWI-Prolog}:
91
+ A comprehensive system description",
92
+ journal = "Science of Computer Programming",
93
+ volume = "164",
94
+ pages = "98 - 115",
95
+ year = "2018",
96
+ note = "Special issue of selected papers from FLOPS 2016",
97
+ issn = "0167-6423",
98
+ doi = "https://doi.org/10.1016/j.scico.2018.02.001",
99
+ url = "http://www.sciencedirect.com/science/article/pii/S0167642318300273",
100
+ author = "Markus Triska",
101
+ keywords = "CLP(B), Boolean unification, Decision diagrams, BDD"
102
+ }
103
+ ==
104
+
105
+ These papers are available from
106
+ [https://www.metalevel.at/swiclpb.pdf](https://www.metalevel.at/swiclpb.pdf)
107
+ and
108
+ [https://www.metalevel.at/boolean.pdf](https://www.metalevel.at/boolean.pdf)
109
+ respectively.
110
+
111
+ ## Boolean expressions {#clpb-exprs}
112
+
113
+ A _Boolean expression_ is one of:
114
+
115
+ | `0` | false |
116
+ | `1` | true |
117
+ | _variable_ | unknown truth value |
118
+ | _atom_ | universally quantified variable |
119
+ | ~ _Expr_ | logical NOT |
120
+ | _Expr_ + _Expr_ | logical OR |
121
+ | _Expr_ * _Expr_ | logical AND |
122
+ | _Expr_ # _Expr_ | exclusive OR |
123
+ | _Var_ ^ _Expr_ | existential quantification |
124
+ | _Expr_ =:= _Expr_ | equality |
125
+ | _Expr_ =\= _Expr_ | disequality (same as #) |
126
+ | _Expr_ =< _Expr_ | less or equal (implication) |
127
+ | _Expr_ >= _Expr_ | greater or equal |
128
+ | _Expr_ < _Expr_ | less than |
129
+ | _Expr_ > _Expr_ | greater than |
130
+ | card(Is,Exprs) | cardinality constraint (_see below_) |
131
+ | `+(Exprs)` | n-fold disjunction (_see below_) |
132
+ | `*(Exprs)` | n-fold conjunction (_see below_) |
133
+
134
+ where _Expr_ again denotes a Boolean expression.
135
+
136
+ The Boolean expression card(Is,Exprs) is true iff the number of true
137
+ expressions in the list `Exprs` is a member of the list `Is` of
138
+ integers and integer ranges of the form `From-To`. For example, to
139
+ state that precisely two of the three variables `X`, `Y` and `Z` are
140
+ `true`, you can use `sat(card([2],[X,Y,Z]))`.
141
+
142
+ `+(Exprs)` and `*(Exprs)` denote, respectively, the disjunction and
143
+ conjunction of all elements in the list `Exprs` of Boolean
144
+ expressions.
145
+
146
+ Atoms denote parametric values that are universally quantified. All
147
+ universal quantifiers appear implicitly in front of the entire
148
+ expression. In residual goals, universally quantified variables always
149
+ appear on the right-hand side of equations. Therefore, they can be
150
+ used to express functional dependencies on input variables.
151
+
152
+ ## Interface predicates {#clpb-interface}
153
+
154
+ The most frequently used CLP(B) predicates are:
155
+
156
+ * sat(+Expr)
157
+ True iff the Boolean expression Expr is satisfiable.
158
+
159
+ * taut(+Expr, -T)
160
+ If Expr is a tautology with respect to the posted constraints, succeeds
161
+ with *T = 1*. If Expr cannot be satisfied, succeeds with *T = 0*.
162
+ Otherwise, it fails.
163
+
164
+ * labeling(+Vs)
165
+ Assigns truth values to the variables Vs such that all constraints
166
+ are satisfied.
167
+
168
+ The unification of a CLP(B) variable _X_ with a term _T_ is equivalent
169
+ to posting the constraint sat(X=:=T).
170
+
171
+ ## Examples {#clpb-examples}
172
+
173
+ Here is an example session with a few queries and their answers:
174
+
175
+ ==
176
+ ?- use_module(library(clpb)).
177
+ true.
178
+
179
+ ?- sat(X*Y).
180
+ X = Y, Y = 1.
181
+
182
+ ?- sat(X * ~X).
183
+ false.
184
+
185
+ ?- taut(X * ~X, T).
186
+ T = 0,
187
+ sat(X=:=X).
188
+
189
+ ?- sat(X^Y^(X+Y)).
190
+ sat(X=:=X),
191
+ sat(Y=:=Y).
192
+
193
+ ?- sat(X*Y + X*Z), labeling([X,Y,Z]).
194
+ X = Z, Z = 1, Y = 0 ;
195
+ X = Y, Y = 1, Z = 0 ;
196
+ X = Y, Y = Z, Z = 1.
197
+
198
+ ?- sat(X =< Y), sat(Y =< Z), taut(X =< Z, T).
199
+ T = 1,
200
+ sat(X=:=X*Y),
201
+ sat(Y=:=Y*Z).
202
+
203
+ ?- sat(1#X#a#b).
204
+ sat(X=:=a#b).
205
+ ==
206
+
207
+ The pending residual goals constrain remaining variables to Boolean
208
+ expressions and are declaratively equivalent to the original query.
209
+ The last example illustrates that when applicable, remaining variables
210
+ are expressed as functions of universally quantified variables.
211
+
212
+ ## Obtaining BDDs {#clpb-residual-goals}
213
+
214
+ By default, CLP(B) residual goals appear in (approximately) algebraic
215
+ normal form (ANF). This projection is often computationally expensive.
216
+ You can set the Prolog flag `clpb_residuals` to the value `bdd` to see
217
+ the BDD representation of all constraints. This results in faster
218
+ projection to residual goals, and is also useful for learning more
219
+ about BDDs. For example:
220
+
221
+ ==
222
+ ?- set_prolog_flag(clpb_residuals, bdd).
223
+ true.
224
+
225
+ ?- sat(X#Y).
226
+ node(3)- (v(X, 0)->node(2);node(1)),
227
+ node(1)- (v(Y, 1)->true;false),
228
+ node(2)- (v(Y, 1)->false;true).
229
+ ==
230
+
231
+ Note that this representation cannot be pasted back on the toplevel,
232
+ and its details are subject to change. Use copy_term/3 to obtain
233
+ such answers as Prolog terms.
234
+
235
+ The variable order of the BDD is determined by the order in which the
236
+ variables first appear in constraints. To obtain different orders,
237
+ you can for example use:
238
+
239
+ ==
240
+ ?- sat(+[1,Y,X]), sat(X#Y).
241
+ node(3)- (v(Y, 0)->node(2);node(1)),
242
+ node(1)- (v(X, 1)->true;false),
243
+ node(2)- (v(X, 1)->false;true).
244
+ ==
245
+
246
+ ## Enabling monotonic CLP(B) {#clpb-monotonic}
247
+
248
+ In the default execution mode, CLP(B) constraints are _not_ monotonic.
249
+ This means that _adding_ constraints can yield new solutions. For
250
+ example:
251
+
252
+ ==
253
+ ?- sat(X=:=1), X = 1+0.
254
+ false.
255
+
256
+ ?- X = 1+0, sat(X=:=1), X = 1+0.
257
+ X = 1+0.
258
+ ==
259
+
260
+ This behaviour is highly problematic from a logical point of view, and
261
+ it may render [**declarative
262
+ debugging**](https://www.metalevel.at/prolog/debugging)
263
+ techniques inapplicable.
264
+
265
+ Set the flag `clpb_monotonic` to `true` to make CLP(B) *monotonic*. If
266
+ this mode is enabled, then you must wrap CLP(B) variables with the
267
+ functor `v/1`. For example:
268
+
269
+ ==
270
+ ?- set_prolog_flag(clpb_monotonic, true).
271
+ true.
272
+
273
+ ?- sat(v(X)=:=1#1).
274
+ X = 0.
275
+ ==
276
+
277
+ @author [Markus Triska](https://www.metalevel.at)
278
+ */
279
+
280
+
281
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
282
+ Each CLP(B) variable belongs to exactly one BDD. Each CLP(B)
283
+ variable gets an attribute (in module "clpb") of the form:
284
+
285
+ index_root(Index,Root)
286
+
287
+ where Index is the variable's unique integer index, and Root is the
288
+ root of the BDD that the variable belongs to.
289
+
290
+ Each CLP(B) variable also gets an attribute in module clpb_hash: an
291
+ association table node(LID,HID) -> Node, to keep the BDD reduced.
292
+ The association table of each variable must be rebuilt on occasion
293
+ to remove nodes that are no longer reachable. We rebuild the
294
+ association tables of involved variables after BDDs are merged to
295
+ build a new root. This only serves to reclaim memory: Keeping a
296
+ node in a local table even when it no longer occurs in any BDD does
297
+ not affect the solver's correctness. However, apply_shortcut/4
298
+ relies on the invariant that every node that occurs in the relevant
299
+ BDDs is also registered in the table of its branching variable.
300
+
301
+ A root is a logical variable with a single attribute ("clpb_bdd")
302
+ of the form:
303
+
304
+ Sat-BDD
305
+
306
+ where Sat is the SAT formula (in original form) that corresponds to
307
+ BDD. Sat is necessary to rebuild the BDD after variable aliasing,
308
+ and to project all remaining constraints to a list of sat/1 goals.
309
+
310
+ Finally, a BDD is either:
311
+
312
+ *) The integers 0 or 1, denoting false and true, respectively, or
313
+ *) A node of the form
314
+
315
+ node(ID, Var, Low, High, Aux)
316
+ Where ID is the node's unique integer ID, Var is the
317
+ node's branching variable, and Low and High are the
318
+ node's low (Var = 0) and high (Var = 1) children. Aux
319
+ is a free variable, one for each node, that can be used
320
+ to attach attributes and store intermediate results.
321
+
322
+ Variable aliasing is treated as a conjunction of corresponding SAT
323
+ formulae.
324
+
325
+ You should think of CLP(B) as a potentially vast collection of BDDs
326
+ that can range from small to gigantic in size, and which can merge.
327
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
328
+
329
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
330
+ Type checking.
331
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
332
+
333
+ is_sat(V) :- var(V), !, non_monotonic(V).
334
+ is_sat(v(V)) :- var(V), !.
335
+ is_sat(v(I)) :- integer(I), between(0, 1, I).
336
+ is_sat(I) :- integer(I), between(0, 1, I).
337
+ is_sat(A) :- atom(A).
338
+ is_sat(~A) :- is_sat(A).
339
+ is_sat(A*B) :- is_sat(A), is_sat(B).
340
+ is_sat(A+B) :- is_sat(A), is_sat(B).
341
+ is_sat(A#B) :- is_sat(A), is_sat(B).
342
+ is_sat(A=:=B) :- is_sat(A), is_sat(B).
343
+ is_sat(A=\=B) :- is_sat(A), is_sat(B).
344
+ is_sat(A=<B) :- is_sat(A), is_sat(B).
345
+ is_sat(A>=B) :- is_sat(A), is_sat(B).
346
+ is_sat(A<B) :- is_sat(A), is_sat(B).
347
+ is_sat(A>B) :- is_sat(A), is_sat(B).
348
+ is_sat(+(Ls)) :- must_be(list, Ls), maplist(is_sat, Ls).
349
+ is_sat(*(Ls)) :- must_be(list, Ls), maplist(is_sat, Ls).
350
+ is_sat(X^F) :- var(X), is_sat(F).
351
+ is_sat(card(Is,Fs)) :-
352
+ must_be(list(ground), Is),
353
+ must_be(list, Fs),
354
+ maplist(is_sat, Fs).
355
+
356
+ non_monotonic(X) :-
357
+ ( var_index(X, _) ->
358
+ % OK: already constrained to a CLP(B) variable
359
+ true
360
+ ; current_prolog_flag(clpb_monotonic, true) ->
361
+ instantiation_error(X)
362
+ ; true
363
+ ).
364
+
365
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
366
+ Rewriting to canonical expressions.
367
+ Atoms are converted to variables with a special attribute.
368
+ A global lookup table maintains the correspondence between atoms and
369
+ their variables throughout different sat/1 goals.
370
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
371
+
372
+ % elementary
373
+ sat_rewrite(V, V) :- var(V), !.
374
+ sat_rewrite(I, I) :- integer(I), !.
375
+ sat_rewrite(A, V) :- atom(A), !, clpb_atom_var(A, V).
376
+ sat_rewrite(v(V), V).
377
+ sat_rewrite(P0*Q0, P*Q) :- sat_rewrite(P0, P), sat_rewrite(Q0, Q).
378
+ sat_rewrite(P0+Q0, P+Q) :- sat_rewrite(P0, P), sat_rewrite(Q0, Q).
379
+ sat_rewrite(P0#Q0, P#Q) :- sat_rewrite(P0, P), sat_rewrite(Q0, Q).
380
+ sat_rewrite(X^F0, X^F) :- sat_rewrite(F0, F).
381
+ sat_rewrite(card(Is,Fs0), card(Is,Fs)) :-
382
+ maplist(sat_rewrite, Fs0, Fs).
383
+ % synonyms
384
+ sat_rewrite(~P, R) :- sat_rewrite(1 # P, R).
385
+ sat_rewrite(P =:= Q, R) :- sat_rewrite(~P # Q, R).
386
+ sat_rewrite(P =\= Q, R) :- sat_rewrite(P # Q, R).
387
+ sat_rewrite(P =< Q, R) :- sat_rewrite(~P + Q, R).
388
+ sat_rewrite(P >= Q, R) :- sat_rewrite(Q =< P, R).
389
+ sat_rewrite(P < Q, R) :- sat_rewrite(~P * Q, R).
390
+ sat_rewrite(P > Q, R) :- sat_rewrite(Q < P, R).
391
+ sat_rewrite(+(Ls), R) :- foldl(or, Ls, 0, F), sat_rewrite(F, R).
392
+ sat_rewrite(*(Ls), R) :- foldl(and, Ls, 1, F), sat_rewrite(F, R).
393
+
394
+ or(A, B, B + A).
395
+
396
+ and(A, B, B * A).
397
+
398
+ must_be_sat(Sat) :-
399
+ must_be(acyclic, Sat),
400
+ ( is_sat(Sat) -> true
401
+ ; no_truth_value(Sat)
402
+ ).
403
+
404
+ no_truth_value(Term) :- domain_error(clpb_expr, Term).
405
+
406
+ parse_sat(Sat0, Sat) :-
407
+ must_be_sat(Sat0),
408
+ sat_rewrite(Sat0, Sat),
409
+ term_variables(Sat, Vs),
410
+ maplist(enumerate_variable, Vs).
411
+
412
+ enumerate_variable(V) :-
413
+ ( var_index_root(V, _, _) -> true
414
+ ; clpb_next_id('$clpb_next_var', Index),
415
+ put_attr(V, clpb, index_root(Index,_)),
416
+ put_empty_hash(V)
417
+ ).
418
+
419
+ var_index(V, I) :- var_index_root(V, I, _).
420
+
421
+ var_index_root(V, I, Root) :- get_attr(V, clpb, index_root(I,Root)).
422
+
423
+ put_empty_hash(V) :-
424
+ empty_assoc(H0),
425
+ put_attr(V, clpb_hash, H0).
426
+
427
+ sat_roots(Sat, Roots) :-
428
+ term_variables(Sat, Vs),
429
+ maplist(var_index_root, Vs, _, Roots0),
430
+ term_variables(Roots0, Roots).
431
+
432
+ %% sat(+Expr) is semidet.
433
+ %
434
+ % True iff Expr is a satisfiable Boolean expression.
435
+
436
+ sat(Sat0) :-
437
+ ( phrase(sat_ands(Sat0), Ands), Ands = [_,_|_] ->
438
+ maplist(sat, Ands)
439
+ ; parse_sat(Sat0, Sat),
440
+ sat_bdd(Sat, BDD),
441
+ sat_roots(Sat, Roots),
442
+ roots_and(Roots, Sat0-BDD, And-BDD1),
443
+ maplist(del_bdd, Roots),
444
+ maplist(=(Root), Roots),
445
+ root_put_formula_bdd(Root, And, BDD1),
446
+ is_bdd(BDD1),
447
+ satisfiable_bdd(BDD1)
448
+ ).
449
+
450
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
451
+ Posting many small sat/1 constraints is better than posting a huge
452
+ conjunction (or negated disjunction), because unneeded nodes are
453
+ removed from node tables after BDDs are merged. This is not
454
+ possible in sat_bdd/2 because the nodes may occur in other BDDs. A
455
+ better version of sat_bdd/2 or a proper implementation of a unique
456
+ table including garbage collection would make this obsolete and
457
+ also improve taut/2 and sat_count/2 in such cases.
458
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
459
+
460
+ sat_ands(X) -->
461
+ ( { var(X) } -> [X]
462
+ ; { X = (A*B) } -> sat_ands(A), sat_ands(B)
463
+ ; { X = *(Ls) } -> sat_ands_(Ls)
464
+ ; { X = ~Y } -> not_ors(Y)
465
+ ; [X]
466
+ ).
467
+
468
+ sat_ands_([]) --> [].
469
+ sat_ands_([L|Ls]) --> [L], sat_ands_(Ls).
470
+
471
+ not_ors(X) -->
472
+ ( { var(X) } -> [~X]
473
+ ; { X = (A+B) } -> not_ors(A), not_ors(B)
474
+ ; { X = +(Ls) } -> not_ors_(Ls)
475
+ ; [~X]
476
+ ).
477
+
478
+ not_ors_([]) --> [].
479
+ not_ors_([L|Ls]) --> [~L], not_ors_(Ls).
480
+
481
+ del_bdd(Root) :- del_attr(Root, clpb_bdd).
482
+
483
+ root_get_formula_bdd(Root, F, BDD) :- get_attr(Root, clpb_bdd, F-BDD).
484
+
485
+ root_put_formula_bdd(Root, F, BDD) :- put_attr(Root, clpb_bdd, F-BDD).
486
+
487
+ roots_and(Roots, Sat0-BDD0, Sat-BDD) :-
488
+ foldl(root_and, Roots, Sat0-BDD0, Sat-BDD),
489
+ rebuild_hashes(BDD).
490
+
491
+ root_and(Root, Sat0-BDD0, Sat-BDD) :-
492
+ ( root_get_formula_bdd(Root, F, B) ->
493
+ Sat = F*Sat0,
494
+ bdd_and(B, BDD0, BDD)
495
+ ; Sat = Sat0,
496
+ BDD = BDD0
497
+ ).
498
+
499
+ bdd_and(NA, NB, And) :-
500
+ apply(*, NA, NB, And),
501
+ is_bdd(And).
502
+
503
+ %% taut(+Expr, -T) is semidet
504
+ %
505
+ % Tautology check. Succeeds with T = 0 if the Boolean expression Expr
506
+ % cannot be satisfied, and with T = 1 if Expr is always true with
507
+ % respect to the current constraints. Fails otherwise.
508
+
509
+ taut(Sat0, T) :-
510
+ parse_sat(Sat0, Sat),
511
+ ( T = 0, \+ sat(Sat) -> true
512
+ ; T = 1, tautology(Sat) -> true
513
+ ; false
514
+ ).
515
+
516
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
517
+ The algebraic equivalence: tautology(F) <=> \+ sat(~F) does NOT
518
+ hold in CLP(B) because the quantifiers of universally quantified
519
+ variables always implicitly appear in front of the *entire*
520
+ expression. Thus we have for example: X+a is not a tautology, but
521
+ ~(X+a), meaning forall(a, ~(X+a)), is unsatisfiable:
522
+
523
+ sat(~(X+a)) = sat(~X * ~a) = sat(~X), sat(~a) = X=0, false
524
+
525
+ The actual negation of X+a, namely ~forall(A,X+A), in terms of
526
+ CLP(B): ~ ~exists(A, ~(X+A)), is of course satisfiable:
527
+
528
+ ?- sat(~ ~A^ ~(X+A)).
529
+ %@ X = 0,
530
+ %@ sat(A=:=A).
531
+
532
+ Instead, of such rewriting, we test whether the BDD of the negated
533
+ formula is 0. Critically, this avoids constraint propagation.
534
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
535
+
536
+ tautology(Sat) :-
537
+ ( phrase(sat_ands(Sat), Ands), Ands = [_,_|_] ->
538
+ maplist(tautology, Ands)
539
+ ; catch((sat_roots(Sat, Roots),
540
+ roots_and(Roots, _-1, _-Ands),
541
+ sat_bdd(1#Sat, BDD),
542
+ bdd_and(BDD, Ands, B),
543
+ B == 0,
544
+ % reset all attributes
545
+ throw(tautology)),
546
+ tautology,
547
+ true)
548
+ ).
549
+
550
+ satisfiable_bdd(BDD) :-
551
+ ( BDD == 0 -> false
552
+ ; BDD == 1 -> true
553
+ ; ( bdd_nodes(var_unbound, BDD, Nodes) ->
554
+ bdd_variables_classification(BDD, Nodes, Classes),
555
+ partition(var_class, Classes, Eqs, Bs, Ds),
556
+ domain_consistency(Eqs, Goal),
557
+ aliasing_consistency(Bs, Ds, Goals),
558
+ maplist(unification, [Goal|Goals])
559
+ ; % if any variable is instantiated, we do not perform
560
+ % any propagation for now
561
+ true
562
+ )
563
+ ).
564
+
565
+ var_class(_=_, <).
566
+ var_class(further_branching(_,_), =).
567
+ var_class(negative_decisive(_), >).
568
+
569
+ unification(true).
570
+ unification(A=B) :- A = B. % safe_goal/1 detects safety of this call
571
+
572
+ var_unbound(Node) :-
573
+ node_var_low_high(Node, Var, _, _),
574
+ var(Var).
575
+
576
+ universal_var(Var) :- get_attr(Var, clpb_atom, _).
577
+
578
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
579
+ By aliasing consistency, we mean that all unifications X=Y, where
580
+ taut(X=:=Y, 1) holds, are posted.
581
+
582
+ To detect this, we distinguish two kinds of variables among those
583
+ variables that are not skipped in any branch: further-branching and
584
+ negative-decisive. X is negative-decisive iff every node where X
585
+ appears as a branching variable has 0 as one of its children. X is
586
+ further-branching iff 1 is not a direct child of any node where X
587
+ appears as a branching variable.
588
+
589
+ Any potential aliasing must involve one further-branching, and one
590
+ negative-decisive variable. X=Y must hold if, for each low branch
591
+ of nodes with X as branching variable, Y has high branch 0, and for
592
+ each high branch of nodes involving X, Y has low branch 0.
593
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
594
+
595
+ aliasing_consistency(Bs, Ds, Goals) :-
596
+ phrase(aliasings(Bs, Ds), Goals).
597
+
598
+ aliasings([], _) --> [].
599
+ aliasings([further_branching(B,Nodes)|Bs], Ds) -->
600
+ { var_index(B, BI) },
601
+ aliasings_(Ds, B, BI, Nodes),
602
+ aliasings(Bs, Ds).
603
+
604
+ aliasings_([], _, _, _) --> [].
605
+ aliasings_([negative_decisive(D)|Ds], B, BI, Nodes) -->
606
+ { var_index(D, DI) },
607
+ ( { DI > BI,
608
+ always_false(high, DI, Nodes),
609
+ always_false(low, DI, Nodes),
610
+ var_or_atom(D, DA), var_or_atom(B, BA) } ->
611
+ [DA=BA]
612
+ ; []
613
+ ),
614
+ aliasings_(Ds, B, BI, Nodes).
615
+
616
+ var_or_atom(Var, VA) :-
617
+ ( get_attr(Var, clpb_atom, VA) -> true
618
+ ; VA = Var
619
+ ).
620
+
621
+ always_false(Which, DI, Nodes) :-
622
+ phrase(nodes_always_false(Nodes, Which, DI), Opposites),
623
+ maplist(with_aux(unvisit), Opposites).
624
+
625
+ nodes_always_false([], _, _) --> [].
626
+ nodes_always_false([Node|Nodes], Which, DI) -->
627
+ { which_node_child(Which, Node, Child),
628
+ opposite(Which, Opposite) },
629
+ opposite_always_false(Opposite, DI, Child),
630
+ nodes_always_false(Nodes, Which, DI).
631
+
632
+ which_node_child(low, Node, Child) :-
633
+ node_var_low_high(Node, _, Child, _).
634
+ which_node_child(high, Node, Child) :-
635
+ node_var_low_high(Node, _, _, Child).
636
+
637
+ opposite(low, high).
638
+ opposite(high, low).
639
+
640
+ opposite_always_false(Opposite, DI, Node) -->
641
+ ( { node_visited(Node) } -> []
642
+ ; { node_var_low_high(Node, Var, Low, High),
643
+ with_aux(put_visited, Node),
644
+ var_index(Var, VI) },
645
+ [Node],
646
+ ( { VI =:= DI } ->
647
+ { which_node_child(Opposite, Node, Child),
648
+ Child == 0 }
649
+ ; opposite_always_false(Opposite, DI, Low),
650
+ opposite_always_false(Opposite, DI, High)
651
+ )
652
+ ).
653
+
654
+ further_branching(Node) :-
655
+ node_var_low_high(Node, _, Low, High),
656
+ Low \== 1,
657
+ High \== 1.
658
+
659
+ negative_decisive(Node) :-
660
+ node_var_low_high(Node, _, Low, High),
661
+ ( Low == 0 -> true
662
+ ; High == 0 -> true
663
+ ; false
664
+ ).
665
+
666
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
667
+ Instantiate all variables that only admit a single Boolean value.
668
+
669
+ This is the case if: The variable is not skipped in any branch
670
+ leading to 1 (its being skipped means that it may be assigned
671
+ either 0 or 1 and can thus not be fixed yet), and all nodes where
672
+ it occurs as a branching variable have either lower or upper child
673
+ fixed to 0 consistently.
674
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
675
+
676
+ domain_consistency(Eqs, Goal) :-
677
+ maplist(eq_a_b, Eqs, Vs, Values),
678
+ Goal = (Vs = Values). % propagate all assignments at once
679
+
680
+ eq_a_b(A=B, A, B).
681
+
682
+ consistently_false_(Which, Node) :-
683
+ which_node_child(Which, Node, Child),
684
+ Child == 0.
685
+
686
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
687
+ In essentially one sweep of the BDD, all variables can be classified:
688
+ Unification with 0 or 1, further branching and/or negative decisive.
689
+
690
+ Strategy: Breadth-first traversal of the BDD, failing (and thus
691
+ clearing all attributes) if the variable is skipped in some branch,
692
+ and moving the frontier along each time.
693
+
694
+ A formula is only satisfiable if it is a tautology after all (also
695
+ implicitly) existentially quantified variables are projected away.
696
+ However, we only need to check this explicitly if at least one
697
+ universally quantified variable appears. Otherwise, we know that
698
+ the formula is satisfiable at this point, because its BDD is not 0.
699
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
700
+
701
+ bdd_variables_classification(BDD, Nodes, Classes) :-
702
+ nodes_variables(Nodes, Vs0),
703
+ variables_in_index_order(Vs0, Vs),
704
+ ( partition(universal_var, Vs, [_|_], Es) ->
705
+ foldl(existential, Es, BDD, 1)
706
+ ; true
707
+ ),
708
+ phrase(variables_classification(Vs, [BDD]), Classes),
709
+ maplist(with_aux(unvisit), Nodes).
710
+
711
+ variables_classification([], _) --> [].
712
+ variables_classification([V|Vs], Nodes0) -->
713
+ { var_index(V, Index) },
714
+ ( { phrase(nodes_with_variable(Nodes0, Index), Nodes) } ->
715
+ ( { maplist(consistently_false_(low), Nodes) } -> [V=1]
716
+ ; { maplist(consistently_false_(high), Nodes) } -> [V=0]
717
+ ; []
718
+ ),
719
+ ( { maplist(further_branching, Nodes) } ->
720
+ [further_branching(V, Nodes)]
721
+ ; []
722
+ ),
723
+ ( { maplist(negative_decisive, Nodes) } ->
724
+ [negative_decisive(V)]
725
+ ; []
726
+ ),
727
+ { maplist(with_aux(unvisit), Nodes) },
728
+ variables_classification(Vs, Nodes)
729
+ ; variables_classification(Vs, Nodes0)
730
+ ).
731
+
732
+ nodes_with_variable([], _) --> [].
733
+ nodes_with_variable([Node|Nodes], VI) -->
734
+ { Node \== 1 },
735
+ ( { node_visited(Node) } -> nodes_with_variable(Nodes, VI)
736
+ ; { with_aux(put_visited, Node),
737
+ node_var_low_high(Node, OVar, Low, High),
738
+ var_index(OVar, OVI) },
739
+ { OVI =< VI },
740
+ ( { OVI =:= VI } -> [Node]
741
+ ; nodes_with_variable([Low,High], VI)
742
+ ),
743
+ nodes_with_variable(Nodes, VI)
744
+ ).
745
+
746
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
747
+ Node management. Always use an existing node, if there is one.
748
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
749
+
750
+ make_node(Var, Low, High, Node) :-
751
+ ( Low == High -> Node = Low
752
+ ; low_high_key(Low, High, Key),
753
+ ( lookup_node(Var, Key, Node) -> true
754
+ ; clpb_next_id('$clpb_next_node', ID),
755
+ Node = node(ID,Var,Low,High,_Aux),
756
+ register_node(Var, Key, Node)
757
+ )
758
+ ).
759
+
760
+ make_node(Var, Low, High, Node) -->
761
+ % make it conveniently usable within DCGs
762
+ { make_node(Var, Low, High, Node) }.
763
+
764
+
765
+ % The key of a node for hashing is determined by the IDs of its
766
+ % children.
767
+
768
+ low_high_key(Low, High, node(LID,HID)) :-
769
+ node_id(Low, LID),
770
+ node_id(High, HID).
771
+
772
+
773
+ rebuild_hashes(BDD) :-
774
+ bdd_nodes(nodevar_put_empty_hash, BDD, Nodes),
775
+ maplist(re_register_node, Nodes).
776
+
777
+ nodevar_put_empty_hash(Node) :-
778
+ node_var_low_high(Node, Var, _, _),
779
+ empty_assoc(H0),
780
+ put_attr(Var, clpb_hash, H0).
781
+
782
+ re_register_node(Node) :-
783
+ node_var_low_high(Node, Var, Low, High),
784
+ low_high_key(Low, High, Key),
785
+ register_node(Var, Key, Node).
786
+
787
+ register_node(Var, Key, Node) :-
788
+ get_attr(Var, clpb_hash, H0),
789
+ put_assoc(Key, H0, Node, H),
790
+ put_attr(Var, clpb_hash, H).
791
+
792
+ lookup_node(Var, Key, Node) :-
793
+ get_attr(Var, clpb_hash, H0),
794
+ get_assoc(Key, H0, Node).
795
+
796
+
797
+ node_id(0, false).
798
+ node_id(1, true).
799
+ node_id(node(ID,_,_,_,_), ID).
800
+
801
+ node_aux(Node, Aux) :- arg(5, Node, Aux).
802
+
803
+ node_var_low_high(Node, Var, Low, High) :-
804
+ Node = node(_,Var,Low,High,_).
805
+
806
+
807
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
808
+ sat_bdd/2 converts a SAT formula in canonical form to an ordered
809
+ and reduced BDD.
810
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
811
+
812
+ sat_bdd(V, Node) :- var(V), !, make_node(V, 0, 1, Node).
813
+ sat_bdd(I, I) :- integer(I), !.
814
+ sat_bdd(V^Sat, Node) :- !, sat_bdd(Sat, BDD), existential(V, BDD, Node).
815
+ sat_bdd(card(Is,Fs), Node) :- !, counter_network(Is, Fs, Node).
816
+ sat_bdd(Sat, Node) :- !,
817
+ Sat =.. [F,A,B],
818
+ sat_bdd(A, NA),
819
+ sat_bdd(B, NB),
820
+ apply(F, NA, NB, Node).
821
+
822
+ existential(V, BDD, Node) :-
823
+ var_index(V, Index),
824
+ bdd_restriction(BDD, Index, 0, NA),
825
+ bdd_restriction(BDD, Index, 1, NB),
826
+ apply(+, NA, NB, Node).
827
+
828
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
829
+ Counter network for card(Is,Fs).
830
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
831
+
832
+ counter_network(Cs, Fs, Node) :-
833
+ same_length([_|Fs], Indicators),
834
+ fill_indicators(Indicators, 0, Cs),
835
+ phrase(formulas_variables(Fs, Vars0), ExBDDs),
836
+ maplist(unvisit, Vars0),
837
+ % The counter network is built bottom-up, so variables with
838
+ % highest index must be processed first.
839
+ variables_in_index_order(Vars0, Vars1),
840
+ reverse(Vars1, Vars),
841
+ counter_network_(Vars, Indicators, Node0),
842
+ foldl(existential_and, ExBDDs, Node0, Node).
843
+
844
+ % Introduce fresh variables for expressions that are not variables.
845
+ % These variables are later existentially quantified to remove them.
846
+ % Also, new variables are introduced for variables that are used more
847
+ % than once, as in card([0,1],[X,X,Y]), to keep the BDD ordered.
848
+
849
+ formulas_variables([], []) --> [].
850
+ formulas_variables([F|Fs], [V|Vs]) -->
851
+ ( { var(F), \+ is_visited(F) } ->
852
+ { V = F,
853
+ put_visited(F) }
854
+ ; { enumerate_variable(V),
855
+ sat_rewrite(V =:= F, Sat),
856
+ sat_bdd(Sat, BDD) },
857
+ [V-BDD]
858
+ ),
859
+ formulas_variables(Fs, Vs).
860
+
861
+ counter_network_([], [Node], Node).
862
+ counter_network_([Var|Vars], [I|Is0], Node) :-
863
+ foldl(indicators_pairing(Var), Is0, Is, I, _),
864
+ counter_network_(Vars, Is, Node).
865
+
866
+ indicators_pairing(Var, I, Node, Prev, I) :- make_node(Var, Prev, I, Node).
867
+
868
+ fill_indicators([], _, _).
869
+ fill_indicators([I|Is], Index0, Cs) :-
870
+ ( memberchk(Index0, Cs) -> I = 1
871
+ ; member(A-B, Cs), between(A, B, Index0) -> I = 1
872
+ ; I = 0
873
+ ),
874
+ Index1 is Index0 + 1,
875
+ fill_indicators(Is, Index1, Cs).
876
+
877
+ existential_and(Ex-BDD, Node0, Node) :-
878
+ bdd_and(BDD, Node0, Node1),
879
+ existential(Ex, Node1, Node),
880
+ % remove attributes to avoid residual goals for variables that
881
+ % are only used as substitutes for formulas
882
+ del_attrs(Ex).
883
+
884
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
885
+ Compute F(NA, NB).
886
+
887
+ We use a DCG to thread through an implicit argument G0, an
888
+ association table F(IDA,IDB) -> Node, used for memoization.
889
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
890
+
891
+ apply(F, NA, NB, Node) :-
892
+ empty_assoc(G0),
893
+ phrase(apply(F, NA, NB, Node), [G0], _).
894
+
895
+ apply(F, NA, NB, Node) -->
896
+ ( { integer(NA), integer(NB) } -> { once(bool_op(F, NA, NB, Node)) }
897
+ ; { apply_shortcut(F, NA, NB, Node) } -> []
898
+ ; { node_id(NA, IDA), node_id(NB, IDB), Key =.. [F,IDA,IDB] },
899
+ ( state(G0), { get_assoc(Key, G0, Node) } -> []
900
+ ; apply_(F, NA, NB, Node),
901
+ state(G0, G),
902
+ { put_assoc(Key, G0, Node, G) }
903
+ )
904
+ ).
905
+
906
+ apply_shortcut(+, NA, NB, Node) :-
907
+ ( NA == 0 -> Node = NB
908
+ ; NA == 1 -> Node = 1
909
+ ; NB == 0 -> Node = NA
910
+ ; NB == 1 -> Node = 1
911
+ ; false
912
+ ).
913
+
914
+ apply_shortcut(*, NA, NB, Node) :-
915
+ ( NA == 0 -> Node = 0
916
+ ; NA == 1 -> Node = NB
917
+ ; NB == 0 -> Node = 0
918
+ ; NB == 1 -> Node = NA
919
+ ; false
920
+ ).
921
+
922
+
923
+ apply_(F, NA, NB, Node) -->
924
+ { var_less_than(NA, NB),
925
+ !,
926
+ node_var_low_high(NA, VA, LA, HA) },
927
+ apply(F, LA, NB, Low),
928
+ apply(F, HA, NB, High),
929
+ make_node(VA, Low, High, Node).
930
+ apply_(F, NA, NB, Node) -->
931
+ { node_var_low_high(NA, VA, LA, HA),
932
+ node_var_low_high(NB, VB, LB, HB),
933
+ VA == VB },
934
+ !,
935
+ apply(F, LA, LB, Low),
936
+ apply(F, HA, HB, High),
937
+ make_node(VA, Low, High, Node).
938
+ apply_(F, NA, NB, Node) --> % NB < NA
939
+ { node_var_low_high(NB, VB, LB, HB) },
940
+ apply(F, NA, LB, Low),
941
+ apply(F, NA, HB, High),
942
+ make_node(VB, Low, High, Node).
943
+
944
+
945
+ node_varindex(Node, VI) :-
946
+ node_var_low_high(Node, V, _, _),
947
+ var_index(V, VI).
948
+
949
+ var_less_than(NA, NB) :-
950
+ ( integer(NB) -> true
951
+ ; node_varindex(NA, VAI),
952
+ node_varindex(NB, VBI),
953
+ VAI < VBI
954
+ ).
955
+
956
+ bool_op(+, 0, 0, 0).
957
+ bool_op(+, 0, 1, 1).
958
+ bool_op(+, 1, 0, 1).
959
+ bool_op(+, 1, 1, 1).
960
+
961
+ bool_op(*, 0, 0, 0).
962
+ bool_op(*, 0, 1, 0).
963
+ bool_op(*, 1, 0, 0).
964
+ bool_op(*, 1, 1, 1).
965
+
966
+ bool_op(#, 0, 0, 0).
967
+ bool_op(#, 0, 1, 1).
968
+ bool_op(#, 1, 0, 1).
969
+ bool_op(#, 1, 1, 0).
970
+
971
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
972
+ Access implicit state in DCGs.
973
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
974
+
975
+ state(S) --> state(S, S).
976
+
977
+ state(S0, S), [S] --> [S0].
978
+
979
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
980
+ Unification. X = Expr is equivalent to sat(X =:= Expr).
981
+
982
+ Current limitation:
983
+ ===================
984
+
985
+ The current interface of attributed variables is not general enough
986
+ to express what we need. For example,
987
+
988
+ ?- sat(A + B), A = A + 1.
989
+
990
+ should be equivalent to
991
+
992
+ ?- sat(A + B), sat(A =:= A + 1).
993
+
994
+ However, attr_unify_hook/2 is only called *after* the unification
995
+ of A with A + 1 has already taken place and turned A into a cyclic
996
+ ground term, raised an error or failed (depending on the flag
997
+ occurs_check), making it impossible to reason about the variable A
998
+ in the unification hook. Therefore, a more general interface for
999
+ attributed variables should replace the current one. In particular,
1000
+ unification filters should be able to reason about terms before
1001
+ they are unified with anything.
1002
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1003
+
1004
+ attr_unify_hook(index_root(I,Root), Other) :-
1005
+ ( integer(Other) ->
1006
+ ( between(0, 1, Other) ->
1007
+ root_get_formula_bdd(Root, Sat, BDD0),
1008
+ bdd_restriction(BDD0, I, Other, BDD),
1009
+ root_put_formula_bdd(Root, Sat, BDD),
1010
+ satisfiable_bdd(BDD)
1011
+ ; no_truth_value(Other)
1012
+ )
1013
+ ; atom(Other) ->
1014
+ root_get_formula_bdd(Root, Sat0, _),
1015
+ parse_sat(Sat0, Sat),
1016
+ sat_bdd(Sat, BDD),
1017
+ root_put_formula_bdd(Root, Sat0, BDD),
1018
+ is_bdd(BDD),
1019
+ satisfiable_bdd(BDD)
1020
+ ; % due to variable aliasing, any BDDs may now be unordered,
1021
+ % so we need to rebuild the new BDD from the conjunction.
1022
+ root_get_formula_bdd(Root, Sat0, _),
1023
+ Sat = Sat0*OtherSat,
1024
+ ( var(Other), var_index_root(Other, _, OtherRoot),
1025
+ OtherRoot \== Root ->
1026
+ root_get_formula_bdd(OtherRoot, OtherSat, _),
1027
+ parse_sat(Sat, Sat1),
1028
+ sat_bdd(Sat1, BDD1),
1029
+ And = Sat,
1030
+ sat_roots(Sat, Roots)
1031
+ ; parse_sat(Other, OtherSat),
1032
+ sat_roots(Sat, Roots),
1033
+ maplist(root_rebuild_bdd, Roots),
1034
+ roots_and(Roots, 1-1, And-BDD1)
1035
+ ),
1036
+ maplist(del_bdd, Roots),
1037
+ maplist(=(NewRoot), Roots),
1038
+ root_put_formula_bdd(NewRoot, And, BDD1),
1039
+ is_bdd(BDD1),
1040
+ satisfiable_bdd(BDD1)
1041
+ ).
1042
+
1043
+ root_rebuild_bdd(Root) :-
1044
+ ( root_get_formula_bdd(Root, F0, _) ->
1045
+ parse_sat(F0, Sat),
1046
+ sat_bdd(Sat, BDD),
1047
+ root_put_formula_bdd(Root, F0, BDD)
1048
+ ; true
1049
+ ).
1050
+
1051
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1052
+ Support for project_attributes/2.
1053
+
1054
+ This is called by the toplevel as
1055
+
1056
+ project_attributes(+QueryVars, +AttrVars)
1057
+
1058
+ in order to project all remaining constraints onto QueryVars.
1059
+
1060
+ All CLP(B) variables that do not occur in QueryVars or AttrVars
1061
+ need to be existentially quantified, so that they do not occur in
1062
+ residual goals. This is very easy to do in the case of CLP(B).
1063
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1064
+
1065
+ project_attributes(QueryVars0, AttrVars) :-
1066
+ append(QueryVars0, AttrVars, QueryVars1),
1067
+ include(clpb_variable, QueryVars1, QueryVars),
1068
+ maplist(var_index_root, QueryVars, _, Roots0),
1069
+ sort(Roots0, Roots),
1070
+ maplist(remove_hidden_variables(QueryVars), Roots).
1071
+
1072
+ clpb_variable(Var) :- var_index(Var, _).
1073
+
1074
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1075
+ All CLP(B) variables occurring in BDDs but not in query variables
1076
+ become existentially quantified. This must also be reflected in the
1077
+ formula. In addition, an attribute is attached to these variables
1078
+ to suppress superfluous sat(V=:=V) goals.
1079
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1080
+
1081
+ remove_hidden_variables(QueryVars, Root) :-
1082
+ root_get_formula_bdd(Root, Formula, BDD0),
1083
+ maplist(put_visited, QueryVars),
1084
+ bdd_variables(BDD0, HiddenVars0),
1085
+ exclude(universal_var, HiddenVars0, HiddenVars),
1086
+ maplist(unvisit, QueryVars),
1087
+ foldl(existential, HiddenVars, BDD0, BDD),
1088
+ foldl(quantify_existantially, HiddenVars, Formula, ExFormula),
1089
+ root_put_formula_bdd(Root, ExFormula, BDD).
1090
+
1091
+ quantify_existantially(E, E0, E^E0) :- put_attr(E, clpb_omit_boolean, true).
1092
+
1093
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1094
+ BDD restriction.
1095
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1096
+
1097
+ bdd_restriction(Node, VI, Value, Res) :-
1098
+ empty_assoc(G0),
1099
+ phrase(bdd_restriction_(Node, VI, Value, Res), [G0], _),
1100
+ is_bdd(Res).
1101
+
1102
+ bdd_restriction_(Node, VI, Value, Res) -->
1103
+ ( { integer(Node) } -> { Res = Node }
1104
+ ; { node_var_low_high(Node, Var, Low, High) } ->
1105
+ ( { integer(Var) } ->
1106
+ ( { Var =:= 0 } -> bdd_restriction_(Low, VI, Value, Res)
1107
+ ; { Var =:= 1 } -> bdd_restriction_(High, VI, Value, Res)
1108
+ ; { no_truth_value(Var) }
1109
+ )
1110
+ ; { var_index(Var, I0),
1111
+ node_id(Node, ID) },
1112
+ ( { I0 =:= VI } ->
1113
+ ( { Value =:= 0 } -> { Res = Low }
1114
+ ; { Value =:= 1 } -> { Res = High }
1115
+ )
1116
+ ; { I0 > VI } -> { Res = Node }
1117
+ ; state(G0), { get_assoc(ID, G0, Res) } -> []
1118
+ ; bdd_restriction_(Low, VI, Value, LRes),
1119
+ bdd_restriction_(High, VI, Value, HRes),
1120
+ make_node(Var, LRes, HRes, Res),
1121
+ state(G0, G),
1122
+ { put_assoc(ID, G0, Res, G) }
1123
+ )
1124
+ )
1125
+ ; { domain_error(node, Node) }
1126
+ ).
1127
+
1128
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1129
+ Relating a BDD to its elements (nodes and variables).
1130
+
1131
+ Note that BDDs can become quite big (easily millions of nodes), and
1132
+ memory space is a major bottleneck for many problems. If possible,
1133
+ we therefore do not duplicate the entire BDD in memory (as in
1134
+ bdd_ites/2), but only extract its features as needed.
1135
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1136
+
1137
+ bdd_nodes(BDD, Ns) :- bdd_nodes(ignore_node, BDD, Ns).
1138
+
1139
+ ignore_node(_).
1140
+
1141
+ % VPred is a unary predicate that is called for each node that has a
1142
+ % branching variable (= each inner node).
1143
+
1144
+ bdd_nodes(VPred, BDD, Ns) :-
1145
+ phrase(bdd_nodes_(VPred, BDD), Ns),
1146
+ maplist(with_aux(unvisit), Ns).
1147
+
1148
+ bdd_nodes_(VPred, Node) -->
1149
+ ( { node_visited(Node) } -> []
1150
+ ; { call(VPred, Node),
1151
+ with_aux(put_visited, Node),
1152
+ node_var_low_high(Node, _, Low, High) },
1153
+ [Node],
1154
+ bdd_nodes_(VPred, Low),
1155
+ bdd_nodes_(VPred, High)
1156
+ ).
1157
+
1158
+ node_visited(Node) :- integer(Node).
1159
+ node_visited(Node) :- with_aux(is_visited, Node).
1160
+
1161
+ bdd_variables(BDD, Vs) :-
1162
+ bdd_nodes(BDD, Nodes),
1163
+ nodes_variables(Nodes, Vs).
1164
+
1165
+ nodes_variables(Nodes, Vs) :-
1166
+ phrase(nodes_variables_(Nodes), Vs),
1167
+ maplist(unvisit, Vs).
1168
+
1169
+ nodes_variables_([]) --> [].
1170
+ nodes_variables_([Node|Nodes]) -->
1171
+ { node_var_low_high(Node, Var, _, _) },
1172
+ ( { integer(Var) } -> []
1173
+ ; { is_visited(Var) } -> []
1174
+ ; { put_visited(Var) },
1175
+ [Var]
1176
+ ),
1177
+ nodes_variables_(Nodes).
1178
+
1179
+ unvisit(V) :- del_attr(V, clpb_visited).
1180
+
1181
+ is_visited(V) :- get_attr(V, clpb_visited, true).
1182
+
1183
+ put_visited(V) :- put_attr(V, clpb_visited, true).
1184
+
1185
+ with_aux(Pred, Node) :-
1186
+ node_aux(Node, Aux),
1187
+ call(Pred, Aux).
1188
+
1189
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1190
+ Internal consistency checks.
1191
+
1192
+ To enable these checks, set the flag clpb_validation to true.
1193
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1194
+
1195
+ is_bdd(BDD) :-
1196
+ ( current_prolog_flag(clpb_validation, true) ->
1197
+ bdd_ites(BDD, ITEs),
1198
+ pairs_values(ITEs, Ls0),
1199
+ sort(Ls0, Ls1),
1200
+ ( same_length(Ls0, Ls1) -> true
1201
+ ; domain_error(reduced_ites, (ITEs,Ls0,Ls1))
1202
+ ),
1203
+ ( member(ITE, ITEs), \+ registered_node(ITE) ->
1204
+ domain_error(registered_node, ITE)
1205
+ ; true
1206
+ ),
1207
+ ( member(I, ITEs), \+ ordered(I) ->
1208
+ domain_error(ordered_node, I)
1209
+ ; true
1210
+ )
1211
+ ; true
1212
+ ).
1213
+
1214
+ ordered(_-ite(Var,High,Low)) :-
1215
+ ( var_index(Var, VI) ->
1216
+ greater_varindex_than(High, VI),
1217
+ greater_varindex_than(Low, VI)
1218
+ ; true
1219
+ ).
1220
+
1221
+ greater_varindex_than(Node, VI) :-
1222
+ ( integer(Node) -> true
1223
+ ; node_var_low_high(Node, Var, _, _),
1224
+ ( var_index(Var, OI) ->
1225
+ OI > VI
1226
+ ; true
1227
+ )
1228
+ ).
1229
+
1230
+ registered_node(Node-ite(Var,High,Low)) :-
1231
+ ( var(Var) ->
1232
+ low_high_key(Low, High, Key),
1233
+ lookup_node(Var, Key, Node0),
1234
+ Node == Node0
1235
+ ; true
1236
+ ).
1237
+
1238
+ bdd_ites(BDD, ITEs) :-
1239
+ bdd_nodes(BDD, Nodes),
1240
+ maplist(node_ite, Nodes, ITEs).
1241
+
1242
+ node_ite(Node, Node-ite(Var,High,Low)) :-
1243
+ node_var_low_high(Node, Var, Low, High).
1244
+
1245
+ %% labeling(+Vs) is multi.
1246
+ %
1247
+ % Enumerate concrete solutions. Assigns truth values to the Boolean
1248
+ % variables Vs such that all stated constraints are satisfied.
1249
+
1250
+ labeling(Vs0) :-
1251
+ must_be(list, Vs0),
1252
+ maplist(labeling_var, Vs0),
1253
+ variables_in_index_order(Vs0, Vs),
1254
+ maplist(indomain, Vs).
1255
+
1256
+ labeling_var(V) :- var(V), !.
1257
+ labeling_var(V) :- V == 0, !.
1258
+ labeling_var(V) :- V == 1, !.
1259
+ labeling_var(V) :- domain_error(clpb_variable, V).
1260
+
1261
+ variables_in_index_order(Vs0, Vs) :-
1262
+ maplist(var_with_index, Vs0, IVs0),
1263
+ keysort(IVs0, IVs),
1264
+ pairs_values(IVs, Vs).
1265
+
1266
+ var_with_index(V, I-V) :-
1267
+ ( var_index_root(V, I, _) -> true
1268
+ ; I = 0
1269
+ ).
1270
+
1271
+ indomain(0).
1272
+ indomain(1).
1273
+
1274
+
1275
+ %% sat_count(+Expr, -Count) is det.
1276
+ %
1277
+ % Count the number of admissible assignments. Count is the number of
1278
+ % different assignments of truth values to the variables in the
1279
+ % Boolean expression Expr, such that Expr is true and all posted
1280
+ % constraints are satisfiable.
1281
+ %
1282
+ % A common form of invocation is `sat_count(+[1|Vs], Count)`: This
1283
+ % counts the number of admissible assignments to `Vs` without imposing
1284
+ % any further constraints.
1285
+ %
1286
+ % Examples:
1287
+ %
1288
+ % ==
1289
+ % ?- sat(A =< B), Vs = [A,B], sat_count(+[1|Vs], Count).
1290
+ % Vs = [A, B],
1291
+ % Count = 3,
1292
+ % sat(A=:=A*B).
1293
+ %
1294
+ % ?- length(Vs, 120),
1295
+ % sat_count(+Vs, CountOr),
1296
+ % sat_count(*(Vs), CountAnd).
1297
+ % Vs = [...],
1298
+ % CountOr = 1329227995784915872903807060280344575,
1299
+ % CountAnd = 1.
1300
+ % ==
1301
+
1302
+ sat_count(Sat0, N) :-
1303
+ catch((parse_sat(Sat0, Sat),
1304
+ sat_bdd(Sat, BDD),
1305
+ sat_roots(Sat, Roots),
1306
+ roots_and(Roots, _-BDD, _-BDD1),
1307
+ % we mark variables that occur in Sat0 as visited ...
1308
+ term_variables(Sat0, Vs),
1309
+ maplist(put_visited, Vs),
1310
+ % ... so that they do not appear in Vs1 ...
1311
+ bdd_variables(BDD1, Vs1),
1312
+ partition(universal_var, Vs1, Univs, Exis),
1313
+ % ... and then remove remaining variables:
1314
+ foldl(universal, Univs, BDD1, BDD2),
1315
+ foldl(existential, Exis, BDD2, BDD3),
1316
+ variables_in_index_order(Vs, IVs),
1317
+ foldl(renumber_variable, IVs, 1, VNum),
1318
+ bdd_count(BDD3, VNum, Count0),
1319
+ var_u(BDD3, VNum, P),
1320
+ % Do not unify N directly, because we are not prepared
1321
+ % for propagation here in case N is a CLP(B) variable.
1322
+ N0 is 2^(P - 1)*Count0,
1323
+ % reset all attributes and Aux variables
1324
+ throw(count(N0))),
1325
+ count(N0),
1326
+ N = N0).
1327
+
1328
+ universal(V, BDD, Node) :-
1329
+ var_index(V, Index),
1330
+ bdd_restriction(BDD, Index, 0, NA),
1331
+ bdd_restriction(BDD, Index, 1, NB),
1332
+ apply(*, NA, NB, Node).
1333
+
1334
+ renumber_variable(V, I0, I) :-
1335
+ put_attr(V, clpb, index_root(I0,_)),
1336
+ I is I0 + 1.
1337
+
1338
+ bdd_count(Node, VNum, Count) :-
1339
+ ( integer(Node) -> Count = Node
1340
+ ; node_aux(Node, Count),
1341
+ ( integer(Count) -> true
1342
+ ; node_var_low_high(Node, V, Low, High),
1343
+ bdd_count(Low, VNum, LCount),
1344
+ bdd_count(High, VNum, HCount),
1345
+ bdd_pow(Low, V, VNum, LPow),
1346
+ bdd_pow(High, V, VNum, HPow),
1347
+ Count is LPow*LCount + HPow*HCount
1348
+ )
1349
+ ).
1350
+
1351
+
1352
+ bdd_pow(Node, V, VNum, Pow) :-
1353
+ var_index(V, Index),
1354
+ var_u(Node, VNum, P),
1355
+ Pow is 2^(P - Index - 1).
1356
+
1357
+ var_u(Node, VNum, Index) :-
1358
+ ( integer(Node) -> Index = VNum
1359
+ ; node_varindex(Node, Index)
1360
+ ).
1361
+
1362
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1363
+ Pick a solution in such a way that each solution is equally likely.
1364
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1365
+
1366
+ %% random_labeling(+Seed, +Vs) is det.
1367
+ %
1368
+ % Select a single random solution. An admissible assignment of truth
1369
+ % values to the Boolean variables in Vs is chosen in such a way that
1370
+ % each admissible assignment is equally likely. Seed is an integer,
1371
+ % used as the initial seed for the random number generator.
1372
+
1373
+ single_bdd(Vars0) :-
1374
+ maplist(monotonic_variable, Vars0, Vars),
1375
+ % capture all variables with a single BDD
1376
+ sat(+[1|Vars]).
1377
+
1378
+ random_labeling(Seed, Vars) :-
1379
+ must_be(list, Vars),
1380
+ set_random(seed(Seed)),
1381
+ ( ground(Vars) -> true
1382
+ ; catch((single_bdd(Vars),
1383
+ once((member(Var, Vars),var(Var))),
1384
+ var_index_root(Var, _, Root),
1385
+ root_get_formula_bdd(Root, _, BDD),
1386
+ bdd_variables(BDD, Vs),
1387
+ variables_in_index_order(Vs, IVs),
1388
+ foldl(renumber_variable, IVs, 1, VNum),
1389
+ phrase(random_bindings(VNum, BDD), Bs),
1390
+ maplist(del_attrs, Vs),
1391
+ % reset all attribute modifications
1392
+ throw(randsol(Vars, Bs))),
1393
+ randsol(Vars, Bs),
1394
+ true),
1395
+ maplist(call, Bs),
1396
+ % set remaining variables to 0 or 1 with equal probability
1397
+ include(var, Vars, Remaining),
1398
+ maplist(maybe_zero, Remaining)
1399
+ ).
1400
+
1401
+ maybe_zero(Var) :-
1402
+ ( maybe -> Var = 0
1403
+ ; Var = 1
1404
+ ).
1405
+
1406
+ random_bindings(_, Node) --> { Node == 1 }, !.
1407
+ random_bindings(VNum, Node) -->
1408
+ { node_var_low_high(Node, Var, Low, High),
1409
+ bdd_count(Node, VNum, Total),
1410
+ bdd_count(Low, VNum, LCount) },
1411
+ ( { maybe(LCount, Total) } ->
1412
+ [Var=0], random_bindings(VNum, Low)
1413
+ ; [Var=1], random_bindings(VNum, High)
1414
+ ).
1415
+
1416
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1417
+ Find solutions with maximum weight.
1418
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1419
+
1420
+ %% weighted_maximum(+Weights, +Vs, -Maximum) is multi.
1421
+ %
1422
+ % Enumerate weighted optima over admissible assignments. Maximize a
1423
+ % linear objective function over Boolean variables Vs with integer
1424
+ % coefficients Weights. This predicate assigns 0 and 1 to the
1425
+ % variables in Vs such that all stated constraints are satisfied, and
1426
+ % Maximum is the maximum of sum(Weight_i*V_i) over all admissible
1427
+ % assignments. On backtracking, all admissible assignments that
1428
+ % attain the optimum are generated.
1429
+ %
1430
+ % This predicate can also be used to _minimize_ a linear Boolean
1431
+ % program, since negative integers can appear in Weights.
1432
+ %
1433
+ % Example:
1434
+ %
1435
+ % ==
1436
+ % ?- sat(A#B), weighted_maximum([1,2,1], [A,B,C], Maximum).
1437
+ % A = 0, B = 1, C = 1, Maximum = 3.
1438
+ % ==
1439
+
1440
+ weighted_maximum(Ws, Vars, Max) :-
1441
+ must_be(list(integer), Ws),
1442
+ must_be(list(var), Vars),
1443
+ single_bdd(Vars),
1444
+ Vars = [Var|_],
1445
+ var_index_root(Var, _, Root),
1446
+ root_get_formula_bdd(Root, _, BDD0),
1447
+ bdd_variables(BDD0, Vs),
1448
+ % existentially quantify variables that are not considered
1449
+ maplist(put_visited, Vars),
1450
+ exclude(is_visited, Vs, Unvisited),
1451
+ maplist(unvisit, Vars),
1452
+ foldl(existential, Unvisited, BDD0, BDD),
1453
+ maplist(var_with_index, Vars, IVs),
1454
+ pairs_keys_values(Pairs0, IVs, Ws),
1455
+ keysort(Pairs0, Pairs1),
1456
+ pairs_keys_values(Pairs1, IVs1, WeightsIndexOrder),
1457
+ pairs_values(IVs1, VarsIndexOrder),
1458
+ % Pairs is a list of Var-Weight terms, in index order of Vars
1459
+ pairs_keys_values(Pairs, VarsIndexOrder, WeightsIndexOrder),
1460
+ bdd_maximum(BDD, Pairs, Max),
1461
+ max_labeling(BDD, Pairs).
1462
+
1463
+ max_labeling(1, Pairs) :- max_upto(Pairs, _, _).
1464
+ max_labeling(node(_,Var,Low,High,Aux), Pairs0) :-
1465
+ max_upto(Pairs0, Var, Pairs),
1466
+ get_attr(Aux, clpb_max, max(_,Dir)),
1467
+ direction_labeling(Dir, Var, Low, High, Pairs).
1468
+
1469
+ max_upto([], _, _).
1470
+ max_upto([Var0-Weight|VWs0], Var, VWs) :-
1471
+ ( Var == Var0 -> VWs = VWs0
1472
+ ; Weight =:= 0 ->
1473
+ ( Var0 = 0 ; Var0 = 1 ),
1474
+ max_upto(VWs0, Var, VWs)
1475
+ ; Weight < 0 -> Var0 = 0, max_upto(VWs0, Var, VWs)
1476
+ ; Var0 = 1, max_upto(VWs0, Var, VWs)
1477
+ ).
1478
+
1479
+ direction_labeling(low, 0, Low, _, Pairs) :- max_labeling(Low, Pairs).
1480
+ direction_labeling(high, 1, _, High, Pairs) :- max_labeling(High, Pairs).
1481
+
1482
+ bdd_maximum(1, Pairs, Max) :-
1483
+ pairs_values(Pairs, Weights0),
1484
+ include(<(0), Weights0, Weights),
1485
+ sum_list(Weights, Max).
1486
+ bdd_maximum(node(_,Var,Low,High,Aux), Pairs0, Max) :-
1487
+ ( get_attr(Aux, clpb_max, max(Max,_)) -> true
1488
+ ; ( skip_to_var(Var, Weight, Pairs0, Pairs),
1489
+ ( Low == 0 ->
1490
+ bdd_maximum_(High, Pairs, MaxHigh, MaxToHigh),
1491
+ Max is MaxToHigh + MaxHigh + Weight,
1492
+ Dir = high
1493
+ ; High == 0 ->
1494
+ bdd_maximum_(Low, Pairs, MaxLow, MaxToLow),
1495
+ Max is MaxToLow + MaxLow,
1496
+ Dir = low
1497
+ ; bdd_maximum_(Low, Pairs, MaxLow, MaxToLow),
1498
+ bdd_maximum_(High, Pairs, MaxHigh, MaxToHigh),
1499
+ Max0 is MaxToLow + MaxLow,
1500
+ Max1 is MaxToHigh + MaxHigh + Weight,
1501
+ Max is max(Max0,Max1),
1502
+ ( Max0 =:= Max1 -> Dir = _Any
1503
+ ; Max0 < Max1 -> Dir = high
1504
+ ; Dir = low
1505
+ )
1506
+ ),
1507
+ store_maximum(Aux, Max, Dir)
1508
+ )
1509
+ ).
1510
+
1511
+ bdd_maximum_(Node, Pairs, Max, MaxTo) :-
1512
+ bdd_maximum(Node, Pairs, Max),
1513
+ between_weights(Node, Pairs, MaxTo).
1514
+
1515
+ store_maximum(Aux, Max, Dir) :- put_attr(Aux, clpb_max, max(Max,Dir)).
1516
+
1517
+ between_weights(Node, Pairs0, MaxTo) :-
1518
+ ( Node == 1 -> MaxTo = 0
1519
+ ; node_var_low_high(Node, Var, _, _),
1520
+ phrase(skip_to_var_(Var, _, Pairs0, _), Weights0),
1521
+ include(<(0), Weights0, Weights),
1522
+ sum_list(Weights, MaxTo)
1523
+ ).
1524
+
1525
+ skip_to_var(Var, Weight, Pairs0, Pairs) :-
1526
+ phrase(skip_to_var_(Var, Weight, Pairs0, Pairs), _).
1527
+
1528
+ skip_to_var_(Var, Weight, [Var0-Weight0|VWs0], VWs) -->
1529
+ ( { Var == Var0 } ->
1530
+ { Weight = Weight0, VWs0 = VWs }
1531
+ ; ( { Weight0 =< 0 } -> []
1532
+ ; [Weight0]
1533
+ ),
1534
+ skip_to_var_(Var, Weight, VWs0, VWs)
1535
+ ).
1536
+
1537
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1538
+ Projection to residual goals.
1539
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1540
+
1541
+ attribute_goals(Var) -->
1542
+ { var_index_root(Var, _, Root) },
1543
+ ( { root_get_formula_bdd(Root, Formula, BDD) } ->
1544
+ { del_bdd(Root) },
1545
+ ( { current_prolog_flag(clpb_residuals, bdd) } ->
1546
+ { bdd_nodes(BDD, Nodes),
1547
+ phrase(nodes(Nodes), Ns) },
1548
+ [clpb:'$clpb_bdd'(Ns)]
1549
+ ; { prepare_global_variables(BDD),
1550
+ phrase(sat_ands(Formula), Ands0),
1551
+ ands_fusion(Ands0, Ands),
1552
+ maplist(formula_anf, Ands, ANFs0),
1553
+ sort(ANFs0, ANFs1),
1554
+ exclude(eq_1, ANFs1, ANFs2),
1555
+ variables_separation(ANFs2, ANFs) },
1556
+ sats(ANFs)
1557
+ ),
1558
+ ( { get_attr(Var, clpb_atom, Atom) } ->
1559
+ [clpb:sat(Var=:=Atom)]
1560
+ ; []
1561
+ ),
1562
+ % formula variables not occurring in the BDD should be booleans
1563
+ { bdd_variables(BDD, Vs),
1564
+ maplist(del_clpb, Vs),
1565
+ term_variables(Formula, RestVs0),
1566
+ include(clpb_variable, RestVs0, RestVs) },
1567
+ booleans(RestVs)
1568
+ ; boolean(Var) % the variable may have occurred only in taut/2
1569
+ ).
1570
+
1571
+ del_clpb(Var) :- del_attr(Var, clpb).
1572
+
1573
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1574
+ To make residual projection work with recorded constraints, the
1575
+ global counters must be adjusted so that new variables and nodes
1576
+ also get new IDs. Also, clpb_next_id/2 is used to actually create
1577
+ these counters, because creating them with b_setval/2 would make
1578
+ them [] on backtracking, which is quite unfortunate in itself.
1579
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1580
+
1581
+ prepare_global_variables(BDD) :-
1582
+ clpb_next_id('$clpb_next_var', V0),
1583
+ clpb_next_id('$clpb_next_node', N0),
1584
+ bdd_nodes(BDD, Nodes),
1585
+ foldl(max_variable_node, Nodes, V0-N0, MaxV0-MaxN0),
1586
+ MaxV is MaxV0 + 1,
1587
+ MaxN is MaxN0 + 1,
1588
+ b_setval('$clpb_next_var', MaxV),
1589
+ b_setval('$clpb_next_node', MaxN).
1590
+
1591
+ max_variable_node(Node, V0-N0, V-N) :-
1592
+ node_id(Node, N1),
1593
+ node_varindex(Node, V1),
1594
+ N is max(N0,N1),
1595
+ V is max(V0,V1).
1596
+
1597
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1598
+ Fuse formulas that share the same variables into single conjunctions.
1599
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1600
+
1601
+ ands_fusion(Ands0, Ands) :-
1602
+ maplist(with_variables, Ands0, Pairs0),
1603
+ keysort(Pairs0, Pairs),
1604
+ group_pairs_by_key(Pairs, Groups),
1605
+ pairs_values(Groups, Andss),
1606
+ maplist(list_to_conjunction, Andss, Ands).
1607
+
1608
+ with_variables(F, Vs-F) :-
1609
+ term_variables(F, Vs0),
1610
+ variables_in_index_order(Vs0, Vs).
1611
+
1612
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1613
+ If possible, separate variables into different sat/1 goals.
1614
+ A formula F can be split in two if for two of its variables A and B,
1615
+ taut((A^F)*(B^F) =:= F, 1) holds. In the first conjunct, A does not
1616
+ occur, and in the second, B does not occur. We separate variables
1617
+ until that is no longer possible. There may be a better way to do this.
1618
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1619
+
1620
+ variables_separation(Fs0, Fs) :- separation_fixpoint(Fs0, [], Fs).
1621
+
1622
+ separation_fixpoint(Fs0, Ds0, Fs) :-
1623
+ phrase(variables_separation_(Fs0, Ds0, Rest), Fs1),
1624
+ partition(anf_done, Fs1, Ds1, Fs2),
1625
+ maplist(arg(1), Ds1, Ds2),
1626
+ maplist(arg(1), Fs2, Fs3),
1627
+ append(Ds0, Ds2, Ds3),
1628
+ append(Rest, Fs3, Fs4),
1629
+ sort(Fs4, Fs5),
1630
+ sort(Ds3, Ds4),
1631
+ ( Fs5 == [] -> Fs = Ds4
1632
+ ; separation_fixpoint(Fs5, Ds4, Fs)
1633
+ ).
1634
+
1635
+ anf_done(done(_)).
1636
+
1637
+ variables_separation_([], _, []) --> [].
1638
+ variables_separation_([F0|Fs0], Ds, Rest) -->
1639
+ ( { member(Done, Ds), F0 == Done } ->
1640
+ variables_separation_(Fs0, Ds, Rest)
1641
+ ; { sat_rewrite(F0, F),
1642
+ sat_bdd(F, BDD),
1643
+ bdd_variables(BDD, Vs0),
1644
+ exclude(universal_var, Vs0, Vs),
1645
+ maplist(existential_(BDD), Vs, Nodes),
1646
+ phrase(pairs(Nodes), Pairs),
1647
+ group_pairs_by_key(Pairs, Groups),
1648
+ phrase(groups_separation(Groups, BDD), ANFs) },
1649
+ ( { ANFs = [_|_] } ->
1650
+ list(ANFs),
1651
+ { Rest = Fs0 }
1652
+ ; [done(F0)],
1653
+ variables_separation_(Fs0, Ds, Rest)
1654
+ )
1655
+ ).
1656
+
1657
+
1658
+ existential_(BDD, V, Node) :- existential(V, BDD, Node).
1659
+
1660
+ groups_separation([], _) --> [].
1661
+ groups_separation([BDD1-BDDs|Groups], OrigBDD) -->
1662
+ { phrase(separate_pairs(BDDs, BDD1, OrigBDD), Nodes) },
1663
+ ( { Nodes = [_|_] } ->
1664
+ nodes_anfs([BDD1|Nodes])
1665
+ ; []
1666
+ ),
1667
+ groups_separation(Groups, OrigBDD).
1668
+
1669
+ separate_pairs([], _, _) --> [].
1670
+ separate_pairs([BDD2|Ps], BDD1, OrigBDD) -->
1671
+ ( { apply(*, BDD1, BDD2, And),
1672
+ And == OrigBDD } ->
1673
+ [BDD2]
1674
+ ; []
1675
+ ),
1676
+ separate_pairs(Ps, BDD1, OrigBDD).
1677
+
1678
+ nodes_anfs([]) --> [].
1679
+ nodes_anfs([N|Ns]) --> { node_anf(N, ANF) }, [anf(ANF)], nodes_anfs(Ns).
1680
+
1681
+ pairs([]) --> [].
1682
+ pairs([V|Vs]) --> pairs_(Vs, V), pairs(Vs).
1683
+
1684
+ pairs_([], _) --> [].
1685
+ pairs_([B|Bs], A) --> [A-B], pairs_(Bs, A).
1686
+
1687
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1688
+ Set the Prolog flag clpb_residuals to bdd to obtain the BDD nodes
1689
+ as residuals. Note that they cannot be used as regular goals.
1690
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1691
+
1692
+ nodes([]) --> [].
1693
+ nodes([Node|Nodes]) -->
1694
+ { node_var_low_high(Node, Var0, Low, High),
1695
+ var_or_atom(Var0, Var),
1696
+ maplist(node_projection, [Node,High,Low], [ID,HID,LID]),
1697
+ var_index(Var0, VI) },
1698
+ [ID-(v(Var,VI) -> HID ; LID)],
1699
+ nodes(Nodes).
1700
+
1701
+
1702
+ node_projection(Node, Projection) :-
1703
+ node_id(Node, ID),
1704
+ ( integer(ID) -> Projection = node(ID)
1705
+ ; Projection = ID
1706
+ ).
1707
+
1708
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1709
+ By default, residual goals are sat/1 calls of the remaining formulas,
1710
+ using (mostly) algebraic normal form.
1711
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1712
+
1713
+ sats([]) --> [].
1714
+ sats([A|As]) --> [clpb:sat(A)], sats(As).
1715
+
1716
+ booleans([]) --> [].
1717
+ booleans([B|Bs]) --> boolean(B), { del_clpb(B) }, booleans(Bs).
1718
+
1719
+ boolean(Var) -->
1720
+ ( { get_attr(Var, clpb_omit_boolean, true) } -> []
1721
+ ; [clpb:sat(Var =:= Var)]
1722
+ ).
1723
+
1724
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1725
+ Relate a formula to its algebraic normal form (ANF).
1726
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1727
+
1728
+ formula_anf(Formula0, ANF) :-
1729
+ parse_sat(Formula0, Formula),
1730
+ sat_bdd(Formula, Node),
1731
+ node_anf(Node, ANF).
1732
+
1733
+ node_anf(Node, ANF) :-
1734
+ node_xors(Node, Xors0),
1735
+ maplist(maplist(monotonic_variable), Xors0, Xors),
1736
+ maplist(list_to_conjunction, Xors, Conjs),
1737
+ ( Conjs = [Var,C|Rest], clpb_var(Var) ->
1738
+ foldl(xor, Rest, C, RANF),
1739
+ ANF = (Var =\= RANF)
1740
+ ; Conjs = [One,Var,C|Rest], One == 1, clpb_var(Var) ->
1741
+ foldl(xor, Rest, C, RANF),
1742
+ ANF = (Var =:= RANF)
1743
+ ; Conjs = [C|Cs],
1744
+ foldl(xor, Cs, C, ANF)
1745
+ ).
1746
+
1747
+ monotonic_variable(Var0, Var) :-
1748
+ ( var(Var0), current_prolog_flag(clpb_monotonic, true) ->
1749
+ Var = v(Var0)
1750
+ ; Var = Var0
1751
+ ).
1752
+
1753
+ clpb_var(Var) :- var(Var), !.
1754
+ clpb_var(v(_)).
1755
+
1756
+ list_to_conjunction([], 1).
1757
+ list_to_conjunction([L|Ls], Conj) :- foldl(and, Ls, L, Conj).
1758
+
1759
+ xor(A, B, B # A).
1760
+
1761
+ eq_1(V) :- V == 1.
1762
+
1763
+ node_xors(Node, Xors) :-
1764
+ phrase(xors(Node), Xors0),
1765
+ % we remove elements that occur an even number of times (A#A --> 0)
1766
+ maplist(sort, Xors0, Xors1),
1767
+ pairs_keys_values(Pairs0, Xors1, _),
1768
+ keysort(Pairs0, Pairs),
1769
+ group_pairs_by_key(Pairs, Groups),
1770
+ exclude(even_occurrences, Groups, Odds),
1771
+ pairs_keys(Odds, Xors2),
1772
+ maplist(exclude(eq_1), Xors2, Xors).
1773
+
1774
+ even_occurrences(_-Ls) :- length(Ls, L), L mod 2 =:= 0.
1775
+
1776
+ xors(Node) -->
1777
+ ( { Node == 0 } -> []
1778
+ ; { Node == 1 } -> [[1]]
1779
+ ; { node_var_low_high(Node, Var0, Low, High),
1780
+ var_or_atom(Var0, Var),
1781
+ node_xors(Low, Ls0),
1782
+ node_xors(High, Hs0),
1783
+ maplist(with_var(Var), Ls0, Ls),
1784
+ maplist(with_var(Var), Hs0, Hs) },
1785
+ list(Ls0),
1786
+ list(Ls),
1787
+ list(Hs)
1788
+ ).
1789
+
1790
+ list([]) --> [].
1791
+ list([L|Ls]) --> [L], list(Ls).
1792
+
1793
+ with_var(Var, Ls, [Var|Ls]).
1794
+
1795
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1796
+ Global variables for unique node and variable IDs and atoms.
1797
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1798
+
1799
+ make_clpb_var('$clpb_next_var') :- nb_setval('$clpb_next_var', 0).
1800
+
1801
+ make_clpb_var('$clpb_next_node') :- nb_setval('$clpb_next_node', 0).
1802
+
1803
+ make_clpb_var('$clpb_atoms') :-
1804
+ empty_assoc(E),
1805
+ nb_setval('$clpb_atoms', E).
1806
+
1807
+ :- multifile user:exception/3.
1808
+
1809
+ user:exception(undefined_global_variable, Name, retry) :-
1810
+ make_clpb_var(Name), !.
1811
+
1812
+ clpb_next_id(Var, ID) :-
1813
+ b_getval(Var, ID),
1814
+ Next is ID + 1,
1815
+ b_setval(Var, Next).
1816
+
1817
+ clpb_atom_var(Atom, Var) :-
1818
+ b_getval('$clpb_atoms', A0),
1819
+ ( get_assoc(Atom, A0, Var) -> true
1820
+ ; put_attr(Var, clpb_atom, Atom),
1821
+ put_attr(Var, clpb_omit_boolean, true),
1822
+ put_assoc(Atom, A0, Var, A),
1823
+ b_setval('$clpb_atoms', A)
1824
+ ).
1825
+
1826
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1827
+ The variable attributes below are not used as constraints by this
1828
+ library. Project remaining attributes to empty lists of residuals.
1829
+
1830
+ Because accessing these hooks is basically a cross-module call, we
1831
+ must declare them public.
1832
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1833
+
1834
+ :- public
1835
+ clpb_hash:attr_unify_hook/2,
1836
+ clpb_bdd:attribute_goals//1,
1837
+ clpb_hash:attribute_goals//1,
1838
+ clpb_omit_boolean:attr_unify_hook/2,
1839
+ clpb_omit_boolean:attribute_goals//1,
1840
+ clpb_atom:attr_unify_hook/2,
1841
+ clpb_atom:attribute_goals//1.
1842
+
1843
+ clpb_hash:attr_unify_hook(_,_). % this unification is always admissible
1844
+
1845
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1846
+ If a universally quantified variable is unified to a Boolean value,
1847
+ it indicates that the formula does not hold for the other value, so
1848
+ it is false.
1849
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1850
+
1851
+ clpb_atom:attr_unify_hook(_, _) :- false.
1852
+
1853
+ clpb_omit_boolean:attr_unify_hook(_,_).
1854
+
1855
+ clpb_bdd:attribute_goals(_) --> [].
1856
+ clpb_hash:attribute_goals(_) --> [].
1857
+ clpb_omit_boolean:attribute_goals(_) --> [].
1858
+ clpb_atom:attribute_goals(_) --> [].
1859
+
1860
+ % clpb_hash:attribute_goals(Var) -->
1861
+ % { get_attr(Var, clpb_hash, Assoc),
1862
+ % assoc_to_list(Assoc, List0),
1863
+ % maplist(node_portray, List0, List) }, [Var-List].
1864
+
1865
+ % node_portray(Key-Node, Key-Node-ite(Var,High,Low)) :-
1866
+ % node_var_low_high(Node, Var, Low, High).
1867
+
1868
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1869
+ Messages
1870
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1871
+
1872
+ :- multifile prolog:message//1.
1873
+
1874
+ prolog:message(clpb(bounded)) -->
1875
+ ['Using CLP(B) with bounded arithmetic may yield wrong results.'-[]].
1876
+
1877
+ warn_if_bounded_arithmetic :-
1878
+ ( current_prolog_flag(bounded, true) ->
1879
+ print_message(warning, clpb(bounded))
1880
+ ; true
1881
+ ).
1882
+
1883
+ :- initialization(warn_if_bounded_arithmetic).
1884
+
1885
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1886
+ Sanbox declarations
1887
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1888
+
1889
+ :- multifile
1890
+ sandbox:safe_global_variable/1,
1891
+ sandbox:safe_primitive/1.
1892
+
1893
+ sandbox:safe_global_variable('$clpb_next_var').
1894
+ sandbox:safe_global_variable('$clpb_next_node').
1895
+ sandbox:safe_global_variable('$clpb_atoms').
1896
+ sandbox:safe_primitive(set_prolog_flag(clpb_residuals, _)).