├── .gitignore ├── CHANGELOG.org ├── LICENSE ├── Makefile ├── README.org ├── reazon-perf.el ├── reazon-sudoku.el ├── reazon.el └── test ├── reazon-test-interface.el ├── reazon-test-prolog.el ├── reazon-test-puzzle.el ├── reazon-test-relation.el ├── reazon-test-sudoku.el ├── reazon-test-trs.el ├── reazon-test-unit.el └── reazon-test-utils.el /.gitignore: -------------------------------------------------------------------------------- 1 | *.elc 2 | -------------------------------------------------------------------------------- /CHANGELOG.org: -------------------------------------------------------------------------------- 1 | * 0.5 (next) 2 | 3 | * 0.4.1 4 | ** Fixed 5 | - reazon--take was taking too many (@ethan-leba) 6 | 7 | * 0.4 8 | ** Added 9 | - Docstring support for defrel (@ethan-leba) 10 | - Add reazon-timeout option for limiting queries (@ethan-leba) 11 | - Add flag for whether or not to run the occurs-check 12 | - Raise error on circular query 13 | 14 | * 0.3 15 | ** Added 16 | - The impure operators conda and condu 17 | - Relations: assqo 18 | 19 | ** Fixed 20 | - `precedeso` was really doing `immediately-precedeso`. The latter was added as a separate function, and the former was fixed to be appropriately general. 21 | - The byte compiler would choke on expressions with too many fresh variables, as for instance in the 9x9 Sudoku solver. 22 | 23 | ** Improved 24 | - Made `membero` and `subseto` more efficient. 25 | - Significantly improved speed of Sudoku solvers. 26 | 27 | * 0.2 28 | ** Added 29 | - Relations: listo, membero, subseto, set-equalo, adjeacento, and precedeso 30 | - Sudoku solvers 31 | - Profiler and benchmarker 32 | 33 | ** Changed 34 | - Renamed relations to end with =o= rather than =-o= 35 | 36 | ** Fixed 37 | - Rewrote internal control functions to avoid recusion, fixing an OOM bug 38 | - Empty conj/disj clause bug 39 | 40 | * 0.1 41 | First release 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PKGDIR = . 2 | LISPDIR = $(PKGDIR)/ 3 | TESTDIR = $(PKGDIR)/test 4 | 5 | LISP = $(wildcard $(LISPDIR)/*.el) 6 | TEST = $(wildcard $(TESTDIR)/*.el) 7 | OBJS = $(LISP:.el=.elc) $(TEST:.el=.elc) 8 | 9 | # Emacs CLI flags 10 | DIRECTORY = --directory 11 | FUNCALL = --funcall 12 | LOAD = --load 13 | 14 | # Emacs batch functions 15 | BATCH = emacs -Q --batch $(DIRECTORY) $(LISPDIR) 16 | 17 | COMPILE = $(FUNCALL) batch-byte-compile 18 | RUN_ERT = $(FUNCALL) ert-run-tests-batch-and-exit 19 | 20 | # Test commands 21 | RUN_TEST = $(BATCH) $(DIRECTORY) $(TESTDIR) 22 | 23 | .PHONY: all clean compile test $(TEST) 24 | 25 | all: compile test 26 | 27 | clean: 28 | $(RM) $(OBJS) 29 | 30 | # This is here to make sure the byte compiler doesn't complain about 31 | # anything, but I don't think it loads any compiled code. 32 | compile: clean 33 | $(BATCH) $(COMPILE) $(LISP) 34 | $(RUN_TEST) $(COMPILE) $(TEST) 35 | 36 | test: $(TEST) 37 | 38 | # This also runs the test utils file, which contains no tests. This 39 | # doesn't have any ill effects, I don't think, except for the useless 40 | # message "Ran 0 tests, 0 results as expected". Is there a simple way 41 | # to exclude that file? 42 | $(TEST): 43 | $(RUN_TEST) $(LOAD) $@ $(RUN_ERT) 44 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | * Reazon -- miniKanren for Emacs 2 | 3 | *Reazon* is an Emacs implementation of *miniKanren*, a small domain-specific logic programming language. Whereas languages like Elisp deal with /functions/ that take inputs and yield outputs, miniKanren deals with sets of values that satisfy /relations/. Every function is a relation, but not vice versa, since a relation might include the output of a function but not its inputs. In such a case, miniKanren would attempt to find inputs yielding the output, effectively running the function backwards. 4 | 5 | /THAT'S VAGUE. HOW ABOUT AN EXAMPLE?/ 6 | 7 | Great idea. Consider this recursive definition for the function =append= [fn:1]: 8 | 9 | #+BEGIN_SRC emacs-lisp :exports both :results code 10 | ;; Use `_append' rather than `append' to avoid clobbering the builtin. 11 | (defun _append (head tail) 12 | (let ((out 13 | (if (null head) 14 | tail 15 | (let* ((a (car head)) 16 | (d (cdr head)) 17 | (rec (_append d tail))) 18 | (cons a rec))))) 19 | out)) 20 | 21 | (list 22 | (_append '(1 2 3) '()) 23 | (_append '() '(4 5 6)) 24 | (_append '(1 2 3) '(4 5 6))) 25 | #+END_SRC 26 | 27 | #+RESULTS: 28 | #+BEGIN_SRC emacs-lisp 29 | ((1 2 3) 30 | (4 5 6) 31 | (1 2 3 4 5 6)) 32 | #+END_SRC 33 | 34 | In that definition, we set up a variable =out= and then assign it one of two values: =tail= if =head= is null, or else the =cons= of the =car= of =head= and the recursive appending of the =cdr= of =head= onto =tail=. 35 | 36 | A miniKanren relation can be defined in the same way: 37 | 38 | #+BEGIN_SRC emacs-lisp :results silent 39 | (reazon-defrel appendo (head tail out) 40 | (reazon-conde 41 | ((reazon-== head '()) (reazon-== out tail)) 42 | ((reazon-fresh (a d rec) 43 | (reazon-== head (cons a d)) 44 | (reazon-== out (cons a rec)) 45 | (appendo d tail rec))))) 46 | #+END_SRC 47 | 48 | Again, we have variables =head=, =tail=, and =out=, and =out= is made to be =tail= when =head= is null and the =cons= of the =car= of =head= and the recursive appending of the =cdr= of =head= onto =tail= when it isn't. 49 | 50 | /THEY LOOK LIKE THE SAME DEFINITION./ 51 | 52 | Indeed, =appendo= can be used just like =append=: 53 | 54 | #+BEGIN_SRC emacs-lisp :exports both :results code 55 | (let ((r1 56 | (reazon-run* out 57 | (appendo '(1 2 3) '() out))) 58 | (r2 59 | (reazon-run* out 60 | (appendo '() '(4 5 6) out))) 61 | (r3 62 | (reazon-run* out 63 | (appendo '(1 2 3) '(4 5 6) out)))) 64 | (mapcar #'car (list r1 r2 r3))) 65 | #+END_SRC 66 | 67 | #+RESULTS: 68 | #+BEGIN_SRC emacs-lisp 69 | ((1 2 3) 70 | (4 5 6) 71 | (1 2 3 4 5 6)) 72 | #+END_SRC 73 | 74 | /SO WHAT'S THE DIFFERENCE? AND WHY IS =out= AN INPUT?/ 75 | 76 | A value can be supplied for =out=, in which case appropriate values will be found for =head= and =tail=: 77 | 78 | #+BEGIN_SRC emacs-lisp :exports both :results code 79 | (reazon-run* (head tail) 80 | (appendo head tail '(1 2 3 4 5 6))) 81 | #+END_SRC 82 | 83 | #+RESULTS: 84 | #+BEGIN_SRC emacs-lisp 85 | ((nil 86 | (1 2 3 4 5 6)) 87 | ((1) 88 | (2 3 4 5 6)) 89 | ((1 2) 90 | (3 4 5 6)) 91 | ((1 2 3) 92 | (4 5 6)) 93 | ((1 2 3 4) 94 | (5 6)) 95 | ((1 2 3 4 5) 96 | (6)) 97 | ((1 2 3 4 5 6) 98 | nil)) 99 | #+END_SRC 100 | 101 | /SO THE INPUTS ARE OUTPUTS?/ 102 | 103 | They can be. They can also be supplied along with the "output", and they can even be supplied only partially: 104 | 105 | #+BEGIN_SRC emacs-lisp :exports both :results code 106 | (reazon-run* (head tail out) 107 | (reazon-fresh (a c d) 108 | (reazon-== head `(,a 2 ,c ,d))) 109 | (reazon-fresh (a b e) 110 | (reazon-== out `(,a ,b 3 4 ,e 6))) 111 | (appendo head tail out)) 112 | #+END_SRC 113 | 114 | #+RESULTS: 115 | #+BEGIN_SRC emacs-lisp 116 | (((_0 2 3 4) 117 | (_1 6) 118 | (_0 2 3 4 _1 6))) 119 | #+END_SRC 120 | 121 | /NOT BAD! BUT APPENDING LISTS TOGETHER ISN'T A VERY EXCITING APPLICATION./ 122 | 123 | Consider the function =eval=, commonly found in languages like Python and Javascript. It takes as input an expression and returns as output the value to which that expression evaluates. The relational equivalent of =eval= would be the relation =evalo=, which would associate expressions with values. Supposing we had such a relation, what be the result of the following query? 124 | 125 | #+BEGIN_SRC emacs-lisp 126 | (reazon-run 1 q 127 | (evalo q q)) 128 | #+END_SRC 129 | 130 | /UHH.../ 131 | 132 | Right, we would get a /quine/, that is, an expression that evaluates to itself! And supposing we had a disequality operator =reazon-!== to stipulate that two expressions are distinct, what would we get from running this? 133 | 134 | #+BEGIN_SRC emacs-lisp 135 | (reazon-run 1 (p q) 136 | (reazon-!= p q) 137 | (evalo p q) 138 | (evalo q p)) 139 | #+END_SRC 140 | 141 | /TWO DISTINCT EXPRESSIONS THAT EVALUATE TO EACH OTHER?/ 142 | 143 | Yes, two /twines/! 144 | 145 | /WOW, REAZON CAN DO ALL THAT?/ 146 | 147 | No. Currently Reazon lacks the ability to constrain values, or really to handle negation at all. I mean, the language is Turing-complete, so it's certainly possible in some sense, but it can't be done in a straightforward way. 148 | 149 | * Installation 150 | For now, the best way to run Reazon is to open the file [[https://github.com/nickdrozd/reazon/blob/master/reazon.el][reazon.el]] and run the command =eval-buffer=. After that, all the Reazon functions, macros, and relations will be loaded up and you can start writing some queries! 151 | 152 | *IMPORTANT NOTE: Reazon relations must be defined in a context with lexical binding.* To enable lexical binding, run =(setq lexical-binding t)= or put =;; -*- lexical-binding: t; -*-= at the top of a source file. *I'm serious, Reazon will not work without lexical binding.* 153 | 154 | ** Emacs Version 155 | Because it relies on lexical binding, Reazon absolutely requires Emacs version 24+. It also uses the function =gensym= in its macros. This was added as a builtin function in Emacs 26, so running Reazon as-is requires at least that. However, =gensym= existed in other forms prior to 26, so Reazon can also be run in versions 24 and 25 by adding one of the following expressions: 156 | 157 | #+BEGIN_SRC emacs-lisp 158 | (progn 159 | (require 'cl-lib) 160 | (defalias 'gensym 'cl-gensym)) 161 | #+END_SRC 162 | 163 | or 164 | 165 | #+BEGIN_SRC emacs-lisp 166 | (require 'cl) 167 | #+END_SRC 168 | 169 | * FAQ 170 | ** Did you come up with miniKanren all by yourself? 171 | No. miniKanren is described in detail and implemented in the book [[https://books.google.com/books?id=HulPDwAAQBAJ&printsec=frontcover&dq=reasoned+schemer#v=onepage&q&f=false][The Reasoned Schemer]] (a sequel to /The Little Schemer/) by Byrd, Friedman, Kiselyov, and Hemann. Reazon is a straightforward adaptation to Elisp of the code from the second edition. 172 | 173 | ** So you just copied some code out of a book? 174 | To some extent, yes. The big differences between the code here and the code there are that 1) the macros are written with =defmacro= instead of =define-syntax=, and 2) certain control functions are written with explicit iteration instead of recursion, since Elisp lacks tail-call elimination. 175 | 176 | ** How does this compare to Clojure's [[https://github.com/clojure/core.logic][core.logic]]? 177 | 178 | =core.logic= is a Clojure implementation of miniKanren. It's significantly more developed than Reazon (I mean, like way more developed), but they are the same in spirit. If you need to use miniKanren for something serious, use that, not this. 179 | 180 | ** Why does "Reazon" have a "z" in it? That's stupid. 181 | 182 | Maybe. Initially it was just called "Reason" (with an "s") after *The Reasoned Schemer*, but then I discovered to my dismay that there was already a package called "Reason" on Melpa (a major mode for some language). Changing the "s" to "z" seemed like a fine way around that roadblock, with the added benefit of making the name "pop". 183 | 184 | ** Why are all the relations named with =o= at the end (=conso=, =listo=, etc)? That's ugly and weird. 185 | 186 | I don't know. That convention was established long before I came around miniKanren. In the context of Emacs, however, it actually fits in with another ugly convention, namely that of ending the names of test functions with =p= (=consp=, =listp=, etc). This suggests that, for example, =conso= is somehow similar to =consp=, which is accurate. 187 | 188 | ** I ran a query, but I got =*** Eval error *** Lisp nesting exceeds ‘max-lisp-eval-depth’=. 189 | 190 | The interface operator =reazon-run*= searches for as many solutions as it can find. If your query has infinitely many solutions, it will keep searching until it blows the stack. For instance, there are infinitely many triples =x, y, z= such that =x= and =y= append to form =z=, so the following query will error: 191 | 192 | #+BEGIN_SRC emacs-lisp 193 | (reazon-run* (x y z) 194 | (appendo x y z)) 195 | #+END_SRC 196 | 197 | Try using =reazon-run= (no asterisk) with a count to limit the search: 198 | 199 | #+BEGIN_SRC emacs-lisp :results code :exports both 200 | (reazon-run 3 (x y z) 201 | (appendo x y z)) 202 | #+END_SRC 203 | 204 | #+RESULTS: 205 | #+BEGIN_SRC emacs-lisp 206 | ((nil _0 _0) 207 | ((_0) 208 | _1 209 | (_0 . _1)) 210 | ((_0 _1) 211 | _2 212 | (_0 _1 . _2))) 213 | #+END_SRC 214 | 215 | ** I defined a relation and ran a query, but I got =*** Eval error *** Symbol’s value as variable is void: x=. I double-checked and I'm sure I wrote the definition correctly. 216 | 217 | Reazon relations need to be defined in lexical environments. This can be set in an interpreter like =ielm= by running =(setq lexical-binding t)= or by adding =;; -*- lexical-binding: t; -*-= to the top of a source file. 218 | 219 | I learned this the hard way. 220 | 221 | In =ielm=: 222 | 223 | #+BEGIN_SRC emacs-lisp 224 | > (reazon-defrel _five (x) (reazon-== x 5)) 225 | _five 226 | > (reazon-run* q (_five q)) 227 | ,*** Eval error *** Symbol’s value as variable is void: x 228 | > (setq lexical-binding t) 229 | t 230 | > (reazon-run* q (_five q)) 231 | ,*** Eval error *** Symbol’s value as variable is void: x 232 | > (reazon-defrel _five (x) (reazon-== x 5)) 233 | _five 234 | > (reazon-run* q (_five q)) 235 | (5) 236 | #+END_SRC 237 | 238 | ** Is the equality operator ~(reazon-)==~ an assertion that two things are the same, or an assignment that makes two things the same? 239 | 240 | That is a deep question. 241 | 242 | ** It's a pain in the ass to use the =reazon-= namespace prefix all the time, especially for an operator as common as ~reazon-==~. 243 | 244 | I know. I'm working on it. 245 | 246 | ** Can Reazon be used to solve dusty old logic puzzles? 247 | 248 | Yes. Consider the following dusty old logic puzzle[fn:2]: 249 | 250 | #+BEGIN_QUOTE 251 | Five schoolgirls sat for an examination. Their parents -- so they thought -- showed an undue degree of interest in the result. They therefore agreed that, in writing home about the examination, each girl should make one true statement and one untrue one. The following are the relevant passages from their letters: 252 | 253 | > Betty: "Kitty was second in the examination. I was only third." 254 | 255 | > Ethel: "You'll be glad to hear that I was on top. Joan was second." 256 | 257 | > Joan: "I was third, and poor old Ethel was bottom." 258 | 259 | > Kitty: "I came out second. Mary was only fourth." 260 | 261 | > Mary: "I was fourth. Top place was taken by Betty." 262 | 263 | What in fact was the order in which the five girls were placed? 264 | #+END_QUOTE 265 | 266 | Representing the exam results as a five element list ordered from first to last, this puzzle can be transformed into the following query: 267 | 268 | #+BEGIN_SRC emacs-lisp :exports both :results code 269 | (reazon-run* q 270 | (reazon-fresh (a b c d e) 271 | ;; Betty: "Kitty was second in the examination. I was only third." 272 | (reazon-disj 273 | (reazon-== q `(,a kitty ,c ,d ,e)) 274 | (reazon-== q `(,a ,b betty ,d ,e))) 275 | ;; Ethel: "You'll be glad to hear that I was on top. Joan was second." 276 | (reazon-disj 277 | (reazon-== q `(ethel ,b ,c ,d ,e)) 278 | (reazon-== q `(,a joan ,c ,d ,e))) 279 | ;; Joan: "I was third, and poor old Ethel was bottom." 280 | (reazon-disj 281 | (reazon-== q `(,a ,b joan ,d ,e)) 282 | (reazon-== q `(,a ,b ,c ,d ethel))) 283 | ;; Kitty: "I came out second. Mary was only fourth." 284 | (reazon-disj 285 | (reazon-== q `(,a kitty ,c ,d ,e)) 286 | (reazon-== q `(,a ,b ,c mary ,e))) 287 | ;; Mary: "I was fourth. Top place was taken by Betty." 288 | (reazon-disj 289 | (reazon-== q `(,a ,b ,c mary ,e)) 290 | (reazon-== q `(betty ,b ,c ,d ,e)))) 291 | (reazon-subseto '(betty ethel joan kitty mary) q)) 292 | #+END_SRC 293 | 294 | #+RESULTS: 295 | #+BEGIN_SRC emacs-lisp 296 | ((ethel kitty joan mary betty) 297 | (ethel kitty joan mary betty) 298 | (kitty joan betty mary ethel)) 299 | #+END_SRC 300 | 301 | That query yielded multiple results (two, in fact, with one duplicate) because Reazon lacks a disequality operator, making it difficult to encode negation. The puzzle says that each girl made one true statement and one false statement. Our use of the disjunction operator requires that at least one of the two statements be true, but doesn't preclude them from both being true. Thus the query fails to pin down the correct answer. 302 | 303 | With a little bit of boring manual labor, it's possible to get something that works: 304 | 305 | #+BEGIN_SRC emacs-lisp :exports both :results code 306 | (reazon-run* q 307 | (reazon-fresh (a b c d e) 308 | ;; Betty: "Kitty was second in the examination. I was only third." 309 | (reazon-disj 310 | (reazon-== q `(,a kitty ,c ,d ,e)) 311 | (reazon-== q `(,a ,b betty ,d ,e))) 312 | ;; Ethel: "You'll be glad to hear that I was on top. Joan was second." 313 | (reazon-disj 314 | (reazon-== q `(ethel ,b ,c ,d ,e)) 315 | (reazon-== q `(,a joan ,c ,d ,e))) 316 | ;; Joan: "I was third, and poor old Ethel was bottom." 317 | (reazon-disj 318 | (reazon-== q `(,a ,b joan ,d ,e)) 319 | (reazon-== q `(,a ,b ,c ,d ethel))) 320 | ;; Kitty: "I came out second. Mary was only fourth." 321 | (reazon-disj 322 | ;; Explicity enumerate possibilities to simulate negation. 323 | (reazon-disj 324 | (reazon-== q `(mary kitty ,c ,d ,e)) 325 | (reazon-== q `(,a kitty mary ,d ,e)) 326 | (reazon-== q `(,a kitty ,c ,d mary))) 327 | (reazon-disj 328 | (reazon-== q `(kitty ,b ,c mary ,e)) 329 | (reazon-== q `(,a ,b kitty mary ,e)) 330 | (reazon-== q `(,a ,b ,c mary kitty)))) 331 | ;; Mary: "I was fourth. Top place was taken by Betty." 332 | (reazon-disj 333 | (reazon-== q `(,a ,b ,c mary ,e)) 334 | (reazon-== q `(betty ,b ,c ,d ,e)))) 335 | (reazon-subseto '(betty ethel joan kitty mary) q)) 336 | #+END_SRC 337 | 338 | #+RESULTS: 339 | #+BEGIN_SRC emacs-lisp 340 | ((kitty joan betty mary ethel)) 341 | #+END_SRC 342 | 343 | See [[https://nickdrozd.github.io/2018/08/07/reazon-logic.html][Solving Logic Puzzles in Emacs with Reazon]]. 344 | 345 | ** Can Reazon be used as a Sudoku solver? 346 | 347 | Yes. See the included file [[https://github.com/nickdrozd/reazon/blob/master/reazon-sudoku.el][reazon-sudoku.el]]. 348 | 349 | ** Can Reazon be used as a theorem prover? 350 | 351 | Yes. See: 352 | - [[https://nickdrozd.github.io/2018/08/14/modal-sentences.html][Generating Sentences of Modal Logic]] 353 | - [[https://nickdrozd.github.io/2018/08/15/prop-proofs.html][Generating Propositional Logic Proofs]] 354 | 355 | ** Can Reazon be used as part of a static analyzer / linter? 356 | 357 | Sure. Suppose you wanted to write a rule that says any expression of the form =(if condition (progn body-1 body-2 ...))= should be rewritten as =(when condition body-1 body-2 ...)= [fn:3]. This could be encoded as something like the following: 358 | 359 | #+BEGIN_SRC emacs-lisp 360 | (reazon-defrel if-without-else-becomes-when (exp out) 361 | (reazon-fresh (condition body) 362 | (reazon-== exp `(if ,condition (progn ,@body))) 363 | (reazon-== out `(when ,condition ,@body)))) 364 | #+END_SRC 365 | 366 | Transforming an expression is merely a matter of running the relation rule against it: 367 | 368 | #+BEGIN_SRC emacs-lisp :exports both :results code 369 | (let ((exp '(if condition (progn body-1 body-2)))) 370 | (reazon-run* q 371 | (if-without-else-becomes-when exp q))) 372 | #+END_SRC 373 | 374 | #+RESULTS: 375 | #+BEGIN_SRC emacs-lisp 376 | ((when condition body-1 body-2)) 377 | #+END_SRC 378 | 379 | ** That looks like simple pattern matching. Wouldn't it be easier just to use pcase? 380 | 381 | =pcase= only goes one way. What if you wanted to go the other direction? 382 | 383 | #+BEGIN_SRC emacs-lisp :exports both :results code 384 | (let ((exp '(when condition body-1 body-2))) 385 | (reazon-run* q 386 | (if-without-else-becomes-when q exp))) 387 | #+END_SRC 388 | 389 | #+RESULTS: 390 | #+BEGIN_SRC emacs-lisp 391 | ((if condition 392 | (progn body-1 body-2))) 393 | #+END_SRC 394 | 395 | That's right: with Reazon, you can *run your linter backwards* to *systematically generate worse code*! If that isn't a killer app, I don't know what is. 396 | 397 | ** Can Reazon be used as part of a scheduler, say, in conjuction with Org Mode? 398 | 399 | That sounds like a neat idea! 400 | 401 | ** Does Reazon support Prolog-style database queries? 402 | 403 | Not directly, but it's possible to simulate them. In Prolog, a /goal/ is a /predicate/ applied to some arguments, and a predicate is defined by a set of one or more /clauses/, where a clause is list of one or more goals. A /fact/ is a clause consisting of a single goal and a /rule/ is a clause consisting of two or more goals. The first goal of a rule is called the /head/ and the rest are collectively called the /body/. (Actually, we can simply define a fact as a rule with an empty body). The head of a rule holds when each goal in its body does, and a predicate holds when any of its clauses do. 404 | 405 | Now, suppose we want to define the predicate =likes= [fn:4]. There are some primitive facts about who likes whom: 406 | 407 | #+BEGIN_SRC 408 | likes(kim, robin) 409 | likes(sandy, lee) 410 | likes(sandy, kim) 411 | likes(robin, cats) 412 | likes(?x, ?x) 413 | #+END_SRC 414 | 415 | And there are some rules extending the =likes= predicate: 416 | 417 | #+BEGIN_SRC 418 | likes(sandy, ?x) :- likes(?x, cats) 419 | likes(kim, ?x) :- likes(?x, lee), likes(?x, kim) 420 | #+END_SRC 421 | 422 | A predicate holds when any of its clauses do, and a clause holds when each goal in its body does. Thus the truth of a predicate is defined by a disjunction of conjunctions. Well, that's exactly what =conde= does! 423 | 424 | #+BEGIN_SRC emacs-lisp 425 | (reazon-defrel likes (a b) 426 | (reazon-conde 427 | ((reazon-== a 'kim) (reazon-== b 'robin)) 428 | ((reazon-== a 'sandy) (reazon-== b 'lee)) 429 | ((reazon-== a 'sandy) (reazon-== b 'kim)) 430 | ((reazon-== a 'robin) (reazon-== b 'cats)) 431 | ((reazon-fresh (x) 432 | (reazon-== a 'sandy) 433 | (reazon-== b x) 434 | (likes x 'cats))) 435 | ((reazon-fresh (x) 436 | (reazon-== a 'kim) 437 | (reazon-== b x) 438 | (likes x 'lee) 439 | (likes x 'kim))) 440 | ((reazon-fresh (x) 441 | (reazon-== a x) 442 | (reazon-== b x))))) 443 | #+END_SRC 444 | 445 | Defining a predicate in this way makes it inconvenient to update it with new rules and facts, but it is nonetheless logically sufficient. 446 | 447 | See the test file [[https://github.com/nickdrozd/reazon/blob/master/test/reazon-test-prolog.el][test/reazon-test-prolog.el]] for an extended example involving a personnel records database. 448 | 449 | ** I tried adding some numbers in a query and I got a type error. 450 | 451 | Working with numbers in Reazon is not straightforward. Here's something you might think of doing: 452 | 453 | #+BEGIN_SRC emacs-lisp 454 | (reazon-run* (x y) 455 | (reazon-== x 5) 456 | (reazon-== y (+ x 1))) 457 | #+END_SRC 458 | 459 | A type error will be raised when =(+ x 1)= is evaluated. This is because =x= is not 5, but rather a logic variable bound to 5. =+= can't operate on logic variables, so a type error is raised. 460 | 461 | One way around this is to encode numbers as lists, but that requires a lot of machinery and is overkill for a lot of simple math problems. Another way is to use =project= to get at the value bound to a variable. This 462 | 463 | #+BEGIN_SRC emacs-lisp 464 | (reazon-run* (x y) 465 | (reazon-== x 5) 466 | (reazon-project (x) 467 | (reazon-== y (* x x)))) 468 | #+END_SRC 469 | 470 | will yield ='((5 25))=. But =project= is an *impure* operator, in that the value of a =project= expression depends on the ordering of clauses. Switching the clauses in the previous example will cause a type error to be raised. 471 | 472 | #+BEGIN_SRC emacs-lisp 473 | (reazon-run* (x y) 474 | (reazon-project (x) 475 | (reazon-== y (* x x))) 476 | (reazon-== x 5)) 477 | #+END_SRC 478 | 479 | Impure operators are generally frowned upon in logic programming for basically the same reasons that mutable state is frowned upon in functional programming, and so =project= should generally be avoided unless absolutely necessary. 480 | 481 | * Footnotes 482 | 483 | [fn:1] This example is used by every introduction to logic programming I've ever seen, including those for Prolog. 484 | 485 | [fn:2] This is [[https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-28.html#%25_thm_4.42][SICP exercise 4.42]]. 486 | 487 | [fn:3] See the [[https://github.com/bbatsov/emacs-lisp-style-guide#syntax][Emacs Lisp Style Guide]]. 488 | 489 | [fn:4] This example comes from the [[https://github.com/norvig/paip-lisp/blob/master/docs/chapter11.md][logic programming chapter of PAIP]]. 490 | -------------------------------------------------------------------------------- /reazon-perf.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-perf.el --- Performance utilities for Reazon -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; 24 | 25 | ;;; Code: 26 | 27 | (require 'benchmark) 28 | (require 'cl-lib) 29 | (require 'profiler) 30 | (require 'reazon) 31 | (require 'reazon-sudoku) 32 | 33 | (defun reazon-profile-memory () 34 | "Profile Reazon's memory usage. 35 | Keep an eye out for recursive functions!" 36 | (interactive) 37 | (profiler-start 'mem) 38 | (dotimes (_ 2) 39 | ;; dummy value to silence compiler warnings 40 | (let ((dummy 41 | (reazon-sudoku-solve-4x4))) 42 | (null dummy))) 43 | (profiler-report) 44 | (profiler-stop)) 45 | 46 | (defun reazon-benchmark () 47 | "Benchmark Reazon performance." 48 | (interactive) 49 | (benchmark 50 | 1 51 | '(let ((board 52 | (reazon-sudoku-solve-9x9 53 | (a2 3) (a3 5) (a4 2) (a5 6) (a6 9) (a7 7) 54 | (b1 6) (b3 2) (b4 5) (b6 1) (b7 4) (b9 3) 55 | (c1 1) (c2 9) (c4 8) (c5 3) (c6 4) (c8 6) (c9 2) 56 | (d1 8) (d2 2) (d3 6) (d5 9) (d7 3) (d9 7) 57 | (e2 7) (e3 4) (e4 6) (e6 2) (e7 9) (e8 1) (e9 5) 58 | (f1 9) (f2 5) (f3 1) (f5 4) (f7 6) (f8 2) 59 | (g2 1) (g4 3) (g5 2) (g6 6) (g8 7) 60 | (h1 2) (h3 8) (h4 9) (h5 5) (h6 7) (h7 1) 61 | (i2 6) (i3 3)))) 62 | (cl-assert 63 | (equal 64 | board 65 | '((4 3 5 2 6 9 7 8 1 66 | 6 8 2 5 7 1 4 9 3 67 | 1 9 7 8 3 4 5 6 2 68 | 8 2 6 1 9 5 3 4 7 69 | 3 7 4 6 8 2 9 1 5 70 | 9 5 1 7 4 3 6 2 8 71 | 5 1 9 3 2 6 8 7 4 72 | 2 4 8 9 5 7 1 3 6 73 | 7 6 3 4 1 8 2 5 9))))))) 74 | 75 | 76 | (provide 'reazon-perf) 77 | ;;; reazon-perf.el ends here 78 | -------------------------------------------------------------------------------- /reazon-sudoku.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-sudoku.el --- Sudoku solvers written in Reazon -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Some Sudoku solvers. These are considerably slower than specialized 24 | ;; Sudoku programs, but they have the advantage of being incredibly 25 | ;; simple to write and use. 26 | 27 | ;;; Code: 28 | 29 | (require 'reazon) 30 | 31 | (defmacro reazon--one-of-the-following (s perms) 32 | "Assert that S is one of PERMS." 33 | `(reazon-disj 34 | ,@(mapcar 35 | (lambda (perm) 36 | `(reazon-== ,s ',perm)) 37 | perms))) 38 | 39 | (defmacro reazon-sudoku-solve-4x4 (&rest coordinate-value-pairs) 40 | "Solve 4x4 sudoku puzzles, given constraints COORDINATE-VALUE-PAIRS. 41 | 42 | Rows are named with letters, and columns are named with numbers. For example, b4 43 | is the square in the fourth column from the left of the second row from the top. 44 | The items in COORDINATE-VALUE-PAIRS must be lists of the form (COORDINATE 45 | VALUE). For example, (a3 1) puts the value 1 in the third column of the first 46 | row. Answers are in the form of lists of coordinates ordered by row and then by 47 | column (so that, for example, c4 comes immediately after c3 but immediately 48 | before d1). 49 | 50 | Example call: 51 | (reazon-sudoku-solve-4x4 (a2 3) (b1 4) (b4 2) (c4 1) (d3 2)) 52 | => ((2 3 1 4 4 1 3 2 3 2 4 1 1 4 2 3)) 53 | 54 | If there are multiple solutions satisfying the given constraints, all of them 55 | will be generated. In particular, if no constraints are specified, all 4x4 56 | solved instances will be generated." 57 | 58 | (let ((perms (reazon-run* q 59 | (reazon-fresh (a b c d) 60 | (reazon-== q `(,a ,b ,c ,d))) 61 | (reazon-set-equalo q '(1 2 3 4))))) 62 | `(reazon-run* (a1 a2 a3 a4 b1 b2 b3 b4 c1 c2 c3 c4 d1 d2 d3 d4) 63 | 64 | ,@(mapcar 65 | (lambda (cvp) `(reazon-== ,@cvp)) 66 | coordinate-value-pairs) 67 | 68 | (reazon--one-of-the-following `(,a1 ,a2 ,a3 ,a4) ,perms) 69 | (reazon--one-of-the-following `(,a1 ,b1 ,c1 ,d1) ,perms) 70 | (reazon--one-of-the-following `(,a1 ,a2 ,b1 ,b2) ,perms) 71 | (reazon--one-of-the-following `(,b1 ,b2 ,b3 ,b4) ,perms) 72 | (reazon--one-of-the-following `(,a2 ,b2 ,c2 ,d2) ,perms) 73 | (reazon--one-of-the-following `(,a3 ,b3 ,c3 ,d3) ,perms) 74 | (reazon--one-of-the-following `(,a3 ,a4 ,b3 ,b4) ,perms) 75 | (reazon--one-of-the-following `(,a4 ,b4 ,c4 ,d4) ,perms) 76 | (reazon--one-of-the-following `(,c1 ,c2 ,c3 ,c4) ,perms) 77 | (reazon--one-of-the-following `(,c1 ,c2 ,d1 ,d2) ,perms) 78 | (reazon--one-of-the-following `(,d1 ,d2 ,d3 ,d4) ,perms) 79 | (reazon--one-of-the-following `(,c3 ,c4 ,d3 ,d4) ,perms)))) 80 | 81 | 82 | (reazon-defrel reazon--in-range-9 (s) 83 | "Assert that numbers 1-9 are in S." 84 | (reazon-membero 1 s) 85 | (reazon-membero 2 s) 86 | (reazon-membero 3 s) 87 | (reazon-membero 4 s) 88 | (reazon-membero 5 s) 89 | (reazon-membero 6 s) 90 | (reazon-membero 7 s) 91 | (reazon-membero 8 s) 92 | (reazon-membero 9 s)) 93 | 94 | (defmacro reazon-sudoku-solve-9x9 (&rest coordinate-value-pairs) 95 | "Solve 9x9 sudoku puzzles, given constraints COORDINATE-VALUE-PAIRS. 96 | 97 | NOTE: This is currently untenably slow. 98 | 99 | Rows are named with letters, and columns are named with numbers. For example, b4 100 | is the square in the fourth column from the left of the second row from the top. 101 | The items in COORDINATE-VALUE-PAIRS must be lists of the form (COORDINATE 102 | VALUE). For example, (a3 1) puts the value 1 in the third column of the first 103 | row. Answers are in the form of lists of coordinates ordered by row and then by 104 | column (so that, for example, c9 comes immediately after c8 but immediately 105 | before d1). 106 | 107 | If there are multiple solutions satisfying the given constraints, all of them 108 | will be generated. In particular, if no constraints are specified, all 9x9 109 | solved instances will be generated (but don't rely on it; the universe will 110 | expire before the computation completes)." 111 | 112 | `(let (reazon-occurs-check) 113 | (reazon-run* (a1 a2 a3 a4 a5 a6 a7 a8 a9 114 | b1 b2 b3 b4 b5 b6 b7 b8 b9 115 | c1 c2 c3 c4 c5 c6 c7 c8 c9 116 | d1 d2 d3 d4 d5 d6 d7 d8 d9 117 | e1 e2 e3 e4 e5 e6 e7 e8 e9 118 | f1 f2 f3 f4 f5 f6 f7 f8 f9 119 | g1 g2 g3 g4 g5 g6 g7 g8 g9 120 | h1 h2 h3 h4 h5 h6 h7 h8 h9 121 | i1 i2 i3 i4 i5 i6 i7 i8 i9) 122 | 123 | ,@(mapcar 124 | (lambda (cvp) `(reazon-== ,@cvp)) 125 | coordinate-value-pairs) 126 | 127 | (reazon--in-range-9 (list a1 a2 a3 a4 a5 a6 a7 a8 a9)) 128 | (reazon--in-range-9 (list a1 b1 c1 d1 e1 f1 g1 h1 i1)) 129 | (reazon--in-range-9 (list a1 a2 a3 b1 b2 b3 c1 c2 c3)) 130 | (reazon--in-range-9 (list a2 b2 c2 d2 e2 f2 g2 h2 i2)) 131 | (reazon--in-range-9 (list b1 b2 b3 b4 b5 b6 b7 b8 b9)) 132 | (reazon--in-range-9 (list a3 b3 c3 d3 e3 f3 g3 h3 i3)) 133 | (reazon--in-range-9 (list c1 c2 c3 c4 c5 c6 c7 c8 c9)) 134 | (reazon--in-range-9 (list a4 a5 a6 b4 b5 b6 c4 c5 c6)) 135 | (reazon--in-range-9 (list a4 b4 c4 d4 e4 f4 g4 h4 i4)) 136 | (reazon--in-range-9 (list a5 b5 c5 d5 e5 f5 g5 h5 i5)) 137 | (reazon--in-range-9 (list d1 d2 d3 d4 d5 d6 d7 d8 d9)) 138 | (reazon--in-range-9 (list d4 d5 d6 e4 e5 e6 f4 f5 f6)) 139 | (reazon--in-range-9 (list d1 d2 d3 e1 e2 e3 f1 f2 f3)) 140 | (reazon--in-range-9 (list e1 e2 e3 e4 e5 e6 e7 e8 e9)) 141 | (reazon--in-range-9 (list f1 f2 f3 f4 f5 f6 f7 f8 f9)) 142 | (reazon--in-range-9 (list a6 b6 c6 d6 e6 f6 g6 h6 i6)) 143 | (reazon--in-range-9 (list g4 g5 g6 h4 h5 h6 i4 i5 i6)) 144 | (reazon--in-range-9 (list i1 i2 i3 i4 i5 i6 i7 i8 i9)) 145 | (reazon--in-range-9 (list g1 g2 g3 g4 g5 g6 g7 g8 g9)) 146 | (reazon--in-range-9 (list h1 h2 h3 h4 h5 h6 h7 h8 h9)) 147 | (reazon--in-range-9 (list a7 b7 c7 d7 e7 f7 g7 h7 i7)) 148 | (reazon--in-range-9 (list a8 b8 c8 d8 e8 f8 g8 h8 i8)) 149 | (reazon--in-range-9 (list a9 b9 c9 d9 e9 f9 g9 h9 i9)) 150 | (reazon--in-range-9 (list a7 a8 a9 b7 b8 b9 c7 c8 c9)) 151 | (reazon--in-range-9 (list d7 d8 d9 e7 e8 e9 f7 f8 f9)) 152 | (reazon--in-range-9 (list g1 g2 g3 h1 h2 h3 i1 i2 i3)) 153 | (reazon--in-range-9 (list g7 g8 g9 h7 h8 h9 i7 i8 i9))))) 154 | 155 | 156 | (provide 'reazon-sudoku) 157 | ;;; reazon-sudoku.el ends here 158 | -------------------------------------------------------------------------------- /reazon.el: -------------------------------------------------------------------------------- 1 | ;;; reazon.el --- miniKanren for Emacs -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; URL: https://github.com/nickdrozd/reazon 7 | ;; Version: 0.4.1 8 | ;; Package-Requires: ((emacs "26")) 9 | ;; Keywords: languages, extensions, lisp 10 | 11 | ;; This program is free software; you can redistribute it and/or modify 12 | ;; it under the terms of the GNU General Public License as published by 13 | ;; the Free Software Foundation, either version 3 of the License, or 14 | ;; (at your option) any later version. 15 | 16 | ;; This program is distributed in the hope that it will be useful, 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | ;; GNU General Public License for more details. 20 | 21 | ;; You should have received a copy of the GNU General Public License 22 | ;; along with this program. If not, see . 23 | 24 | ;;; Commentary: 25 | 26 | ;; Reazon is an implmentation of the miniKanren language for Emacs. It 27 | ;; provides an interface for writing and running relational programs. 28 | ;; That interface consists of the following macros: 29 | 30 | ;; * reazon-defrel 31 | ;; * reazon-run* 32 | ;; * reazon-run 33 | ;; * reazon-fresh 34 | ;; * reazon-conde 35 | ;; * reazon-conj 36 | ;; * reazon-disj 37 | ;; * reazon-project 38 | 39 | ;; Besides these, there is a single primitive goal, reazon-==. 40 | 41 | ;;; Code: 42 | 43 | ;; -- Configuration -- 44 | 45 | (defvar reazon-timeout nil 46 | "The maximum amount of time in seconds that a query can take. 47 | 48 | If a query is interrupted, it will return any results it has 49 | collected so far. 50 | 51 | Consider let-binding this around your call to `reazon-run' 52 | instead of setqing it.") 53 | 54 | (defvar reazon-occurs-check t 55 | "Whether to run the `occurs' check during unification. 56 | Circular queries are bad. It is possible to detect them, but it isn't 57 | cheap. Setting the flag to nil grants the programmer faster queries in 58 | exchange for assuming the responsibility of ensuring correctness on 59 | their own.") 60 | 61 | ;; -- Variables -- 62 | 63 | ;; Reazon variables need to be distinct from Lisp symbols. They are 64 | ;; implemented here as vectors. 65 | 66 | (defun reazon--make-variable (name) 67 | "Create a unique variable from NAME." 68 | (vector name)) 69 | 70 | (defun reazon--variable-p (var) 71 | "Check whether VAR is a variable." 72 | (vector-or-char-table-p var)) 73 | 74 | ;; -- Substitutions -- 75 | 76 | ;; An ASSOCIATION is a cons pair whose car is a Reazon variable. 77 | 78 | ;; A SUBSTITUTION is a list of associations such that no variable is 79 | ;; associated more than once and no variable is associated to itself, 80 | ;; either directly or through a CYCLE. 81 | 82 | ;; A variable unassociated in a substitution is FRESH. 83 | 84 | (defun reazon--walk (var sub) 85 | "Return the value associated with VAR in SUB if there is one, else VAR." 86 | (let* ((next var) 87 | (val (and (reazon--variable-p next) (assoc next sub)))) 88 | (while (consp val) 89 | (setq next (cdr val)) 90 | (setq val (and (reazon--variable-p next) (assoc next sub)))) 91 | next)) 92 | 93 | (defun reazon--walk* (var sub) 94 | "Return SUB with VAR replaced by its recursively walked value." 95 | (let ((walked (reazon--walk var sub))) 96 | (if (not (consp walked)) 97 | walked 98 | (cons 99 | (reazon--walk* (car walked) sub) 100 | (reazon--walk* (cdr walked) sub))))) 101 | 102 | (defun reazon--occurs-p (var val sub) 103 | "Return whether VAL is chain-associated with VAR in SUB." 104 | (let ((walked (reazon--walk val sub))) 105 | (cond 106 | ((reazon--variable-p walked) 107 | (equal walked var)) 108 | ((consp walked) 109 | (or (reazon--occurs-p var (car walked) sub) 110 | (reazon--occurs-p var (cdr walked) sub))) 111 | (t nil)))) 112 | 113 | (define-error 'reazon-circular-query 114 | "Circular query detected") 115 | 116 | (defconst reazon--false (gensym) 117 | "A symbol to indicate substitution failure. 118 | In The Reasoned Schemer, several substitution-handling functions 119 | return #f (false) in certain circumstances instead of a substitution 120 | to indicate that the operation failed. In Emacs, false is nil, which 121 | is also the empty list, which happens to be a substitution. To avoid 122 | confusing these functions, we pick an arbitrary dummy symbol to 123 | indicate substitution failure.") 124 | 125 | (defun reazon--extend (var val sub) 126 | "Associate VAR and VAL in SUB." 127 | (if (and reazon-occurs-check (reazon--occurs-p var val sub)) 128 | (signal 'reazon-circular-query `(,var ,val ,sub)) 129 | (cons `(,var . ,val) sub))) 130 | 131 | (defun reazon--unify (u v sub) 132 | "Attempt to extend SUB with recursive associations between U and V." 133 | (let ((u-walked (reazon--walk u sub)) 134 | (v-walked (reazon--walk v sub))) 135 | (cond 136 | ((equal u-walked v-walked) 137 | ;; The vars are already associated, so do nothing. 138 | sub) 139 | ((reazon--variable-p u-walked) 140 | ;; u-walked is fresh, so associate it with v-walked. 141 | (reazon--extend u-walked v-walked sub)) 142 | ((reazon--variable-p v-walked) 143 | ;; v-walked is fresh and u-walked is not, so associate v-walked 144 | ;; with u-walked. 145 | (reazon--extend v-walked u-walked sub)) 146 | ((and (consp u-walked) (consp v-walked)) 147 | ;; Destructure the vars and attempts to recursively unify them. 148 | (let ((sub (reazon--unify (car u-walked) (car v-walked) sub))) 149 | (if (equal sub reazon--false) 150 | reazon--false 151 | (reazon--unify (cdr u-walked) (cdr v-walked) sub)))) 152 | (t 153 | ;; Unification failed. 154 | reazon--false)))) 155 | 156 | ;; -- Streams -- 157 | 158 | ;; A STREAM is 159 | ;; * the empty list, 160 | ;; * a cons pair whose cdr is a stream, or 161 | ;; * a function of no arguments whose body is a stream. 162 | ;; 163 | ;; The last of these is called a SUSPENSION. 164 | 165 | (defun reazon--append (stream-1 stream-2) 166 | "Join STREAM-1 and STREAM-2 into a new stream. 167 | If STREAM-1 is a suspsension, force it and append the result to 168 | STREAM-2, else append them as usual." 169 | (let (result 170 | (rest stream-1)) 171 | (while (and rest (not (functionp rest))) 172 | (setq result (cons (car rest) result)) 173 | (setq rest (cdr rest))) 174 | ;; rest is nil or a suspension 175 | (append 176 | (nreverse result) 177 | (if (null rest) stream-2 178 | ;; In the recursive call, STREAM-1 is forced and swapped with 179 | ;; STREAM-2. This swap is critical; without it, the search 180 | ;; would be depth-first and incomplete, whereas with it the 181 | ;; search is complete. See "microKanren: A Lucid Little Logic 182 | ;; Language with a Simple Complete Search". 183 | (lambda () 184 | (reazon--append 185 | stream-2 186 | (funcall rest))))))) 187 | 188 | (defvar reazon--stop-time nil 189 | "The time from epoch in seconds that a query should halt computation. 190 | Should not be set manually; its value is derived from `reazon-timeout'.") 191 | 192 | (defun reazon--pull (stream) 193 | "Force STREAM until it isn't a suspension or `reazon--stop-time' is reached." 194 | (let ((result stream)) 195 | (while (and (functionp result) 196 | (or (not reazon--stop-time) 197 | (> reazon--stop-time (float-time)))) 198 | (setq result (funcall result))) 199 | result)) 200 | 201 | (defun reazon--take (n stream) 202 | "Pull N values from STREAM if N is positive else pull it without stopping. 203 | 204 | If `stream' is a function, then `reazon--pull' ended due to a 205 | timeout, and the values collected so far will be returned." 206 | (declare (indent 1)) 207 | (cond 208 | ((or (functionp stream) (null stream)) nil) 209 | ((equal 1 n) (list (car stream))) 210 | (t (let ((count (if n (1- n) -1)) 211 | (result (list (car stream))) 212 | (rest (reazon--pull (cdr stream)))) 213 | (while (and rest (not (zerop count)) (not (functionp rest))) 214 | (setq count (1- count)) 215 | (setq result (cons (car rest) result)) 216 | (setq rest (reazon--pull (cdr rest)))) 217 | (nreverse result))))) 218 | 219 | ;; -- Goals -- 220 | 221 | ;; A GOAL is a function that takes a substitution and returns a stream 222 | ;; of substitutions. 223 | 224 | (defun reazon-== (u v) 225 | "Attempt to unify U and V in the provided substitution. 226 | If unification succeeds, return a stream containing the 227 | resulting substitution, else return the empty stream. 228 | 229 | This primitive goal succeeds if U and V can be unified." 230 | (lambda (sub) 231 | (let ((unified (reazon--unify u v sub))) 232 | (if (equal unified reazon--false) 233 | '() 234 | `(,unified))))) 235 | 236 | (defun reazon-!S (sub) 237 | "Return a stream containing SUB. 238 | This primitive goal always succeeds." 239 | `(,sub)) 240 | 241 | (defun reazon-!U (_sub) 242 | "Return the empty stream. 243 | This primitive goal always fails." 244 | '()) 245 | 246 | (defun reazon--disj-2 (goal-1 goal-2) 247 | "Join GOAL-1 and GOAL-2 into a new goal containing them both. 248 | This primitive goal succeeds if either of them do." 249 | (lambda (stream) 250 | (reazon--append 251 | (funcall goal-1 stream) 252 | (funcall goal-2 stream)))) 253 | 254 | (defun reazon--append-map (goal stream) 255 | "Run GOAL with every element of STREAM." 256 | (cond 257 | ((null stream) nil) 258 | ((functionp stream) (lambda () (reazon--append-map goal (funcall stream)))) 259 | (t (reazon--append 260 | (funcall goal (car stream)) 261 | (reazon--append-map goal (cdr stream)))))) 262 | 263 | (defun reazon--conj-2 (goal-1 goal-2) 264 | "Run GOAL-2 with the result of running GOAL-1 with the provided stream. 265 | This primitive goal succeeds if they both do." 266 | (lambda (stream) 267 | (reazon--append-map 268 | goal-2 269 | (funcall goal-1 stream)))) 270 | 271 | (defun reazon--ifte (test consq alt) 272 | "Run CONSQ if TEST succeeds, else ALT." 273 | (declare (indent 1)) 274 | (lambda (s) 275 | (reazon--ifte-help (funcall test s) consq alt))) 276 | 277 | (defun reazon--ifte-help (stream consq alt) 278 | "Run ALT with STREAM if it's nil, else CONSQ." 279 | (cond 280 | ((null stream) (funcall alt stream)) 281 | ((functionp stream) 282 | (lambda () (reazon--ifte-help (funcall stream) consq alt))) 283 | (t (reazon--append-map consq stream)))) 284 | 285 | (defun reazon--once (goal) 286 | "Run GOAL for just one value (if there is one)." 287 | (lambda (s) 288 | (reazon--once-help (funcall goal s)))) 289 | 290 | (defun reazon--once-help (stream) 291 | "Pull at most one value out of STREAM." 292 | (cond 293 | ((null stream) '()) 294 | ((functionp stream) 295 | (lambda () (reazon--once-help (funcall stream)))) 296 | (t `(,(car stream))))) 297 | 298 | (defun reazon--run-goal (goal) 299 | "Pull GOAL with the empty stream." 300 | (reazon--pull (funcall goal nil))) 301 | 302 | ;; -- Reification -- 303 | 304 | ;; A REIFIED NAME is a concrete identifier assigned to a fresh 305 | ;; variable when values are presented. 306 | 307 | (defun reazon--reify-name (number) 308 | "Return the symbol '_$NUMBER." 309 | ;; Should this use `make-symbol' instead of `intern'? 310 | (intern (concat "_" (number-to-string number)))) 311 | 312 | (defun reazon--reify-sub (var sub) 313 | "Replace VAR in SUB with its reified name." 314 | (let ((walked (reazon--walk var sub))) 315 | (cond 316 | ((reazon--variable-p walked) 317 | (let ((name (reazon--reify-name (length sub)))) 318 | (reazon--extend walked name sub))) 319 | ((consp walked) 320 | (let ((sub (reazon--reify-sub (car walked) sub))) 321 | (reazon--reify-sub (cdr walked) sub))) 322 | (t 323 | sub)))) 324 | 325 | (defun reazon--reify (var) 326 | "Return a function that takes a substitution and reifies VAR therein. 327 | This is the reification entrypoint." 328 | (lambda (sub) 329 | (let* ((walked-var (reazon--walk* var sub)) 330 | (reified-sub (reazon--reify-sub walked-var '()))) 331 | (reazon--walk* walked-var reified-sub)))) 332 | 333 | (defun reazon--call-with-fresh (names function) 334 | "Call FUNCTION with a variable created from NAMES. 335 | names: list of symbols 336 | function: variables -> goal, e.g. (lambda (fruit) (reazon-== 'plum fruit))" 337 | (declare (indent 1)) 338 | (apply function (mapcar #'reazon--make-variable names))) 339 | 340 | ;; -- Macros -- 341 | 342 | ;; Reazon is implemented in such a way that everything could in 343 | ;; principle be run without using any macros. No "work" is being done 344 | ;; by the macros; they are strictly wrappers around the functions 345 | ;; defined above. 346 | 347 | (defmacro reazon-disj (&rest goals) 348 | "Chain together GOALS with `reazon--disj-2' if there are any, else fail." 349 | (pcase goals 350 | ('() '#'reazon-!U) 351 | (`(,goal) goal) 352 | (`(,goal . ,rest) 353 | `(reazon--disj-2 ,goal (reazon-disj ,@rest))))) 354 | 355 | (defmacro reazon-conj (&rest goals) 356 | "Chain together GOALS with `reazon--conj-2' if there are any, else succeed." 357 | (pcase goals 358 | ('() '#'reazon-!S) 359 | (`(,goal) goal) 360 | (`(,goal . ,rest) 361 | `(reazon--conj-2 ,goal (reazon-conj ,@rest))))) 362 | 363 | (defmacro reazon-fresh (vars &rest goals) 364 | "Bind each of VARS as a fresh variable and run the conjunction of GOALS." 365 | (declare (indent 1)) 366 | (if (null vars) 367 | `(reazon-conj ,@goals) 368 | `(reazon--call-with-fresh (mapcar #'gensym ',vars) 369 | (lambda (,@vars) 370 | (reazon-conj ,@goals))))) 371 | 372 | (defmacro reazon-project (vars &rest goals) 373 | "Run GOALS with the values associated with VARS lexically bound. 374 | This is an impure, non-relational operator, and its correct use 375 | depends on the ordering of clauses. For example, 376 | 377 | (reazon-run* q 378 | (reazon-fresh (x) 379 | (reazon-== x 5) 380 | (reazon-project (x) 381 | (reazon-== q (* x x))))) 382 | 383 | succeeds with a value of '(25), while 384 | 385 | (reazon-run* q 386 | (reazon-fresh (x) 387 | (reazon-project (x) 388 | (reazon-== q (* x x))) 389 | (reazon-== x 5))) 390 | 391 | raises an error. This is because in the second instance, the variable 392 | `x' is still fresh in the substitution, so the multiplication fails 393 | when applied to it." 394 | (declare (indent 1)) 395 | (if (null vars) 396 | `(reazon-conj ,@goals) 397 | (let* ((stream (gensym)) 398 | (walked-vars 399 | (mapcar (lambda (var) `(,var (reazon--walk* ,var ,stream))) vars))) 400 | `(lambda (,stream) 401 | (let ,walked-vars 402 | (funcall (reazon-conj ,@goals) ,stream)))))) 403 | 404 | (defmacro reazon-run (n var/list &rest goals) 405 | "Run GOALS against VAR/LIST for at most N values. 406 | If N is nil, run for as many values as possible. VAR/LIST can be 407 | either a symbol or a list." 408 | (declare (indent 2)) 409 | (if (listp var/list) 410 | (let ((vars var/list) 411 | (q (gensym))) 412 | `(reazon-run ,n ,q 413 | (reazon-fresh ,vars 414 | (reazon-== (list ,@vars) ,q) 415 | ,@goals))) 416 | (let ((var var/list)) 417 | `(let ((,var (reazon--make-variable ',var)) 418 | (reazon--stop-time (and reazon-timeout (+ reazon-timeout (float-time))))) 419 | (mapcar 420 | (reazon--reify ,var) 421 | (reazon--take ,n 422 | (reazon--run-goal (reazon-conj ,@goals)))))))) 423 | 424 | (defmacro reazon-run* (query-var &rest goals) 425 | "Run GOALS against QUERY-VAR for as many values as possible. 426 | This will raise an error if the query has infinitely many solutions." 427 | (declare (indent 1)) 428 | `(reazon-run nil ,query-var 429 | ,@goals)) 430 | 431 | (defmacro reazon-conde (&rest clauses) 432 | "Chain together CLAUSES as a disjunction of conjunctions." 433 | `(reazon-disj ,@(mapcar (lambda (arm) `(reazon-conj ,@arm)) clauses))) 434 | 435 | (defmacro reazon-conda (&rest clauses) 436 | "Run only the first clause in CLAUSES whose head succeeds. 437 | Also known as committed choice. This operator is impure." 438 | (pcase clauses 439 | ('() '#'reazon-!U) 440 | (`(,clause) `(reazon-conj ,@clause)) 441 | (`((,head . ,body) . ,rest) 442 | `(reazon--ifte ,head 443 | (reazon-conj ,@body) 444 | (reazon-conda ,@rest))))) 445 | 446 | (defmacro reazon-condu (&rest clauses) 447 | "Run for just one value the first clause in CLAUSES whose head succeeds. 448 | Also known as committed choice. This operator is impure." 449 | (pcase clauses 450 | ('() '#'reazon-!U) 451 | (`((,head . ,body) . ,rest) 452 | `(reazon-conda 453 | ((reazon--once ,head) ,@body) 454 | ,@rest)))) 455 | 456 | (defmacro reazon-defrel (name varlist &optional docstring &rest goals) 457 | "Define relation NAME with args VARLIST and body GOALS, and a DOCSTRING." 458 | (declare (indent 2) (doc-string 3)) 459 | 460 | ;; keep this nasty docstring logic 461 | ;; away from the relation definition 462 | (cond 463 | ((stringp docstring) (setq docstring `(,docstring))) 464 | (docstring (setq goals `(,docstring . ,goals) 465 | docstring nil))) 466 | 467 | (let ((stream (gensym))) 468 | `(defun ,name ,varlist 469 | ,@docstring 470 | (lambda (,stream) 471 | (lambda () 472 | (funcall (reazon-conj ,@goals) ,stream)))))) 473 | 474 | (reazon-defrel reazon-caro (p a) 475 | "A is the car of P." 476 | (reazon-fresh (d) 477 | (reazon-conso a d p))) 478 | 479 | (reazon-defrel reazon-cdro (p d) 480 | "D is the cdr of P." 481 | (reazon-fresh (a) 482 | (reazon-conso a d p))) 483 | 484 | (reazon-defrel reazon-conso (a d p) 485 | "P is a cons of A and D." 486 | (reazon-== p (cons a d))) 487 | 488 | (reazon-defrel reazon-nullo (x) 489 | "X is null." 490 | (reazon-== x '())) 491 | 492 | (reazon-defrel reazon-pairo (p) 493 | "P is a pair." 494 | (reazon-fresh (a d) 495 | (reazon-conso a d p))) 496 | 497 | (reazon-defrel reazon-listo (s) 498 | "S is a proper list." 499 | (reazon-conde 500 | ((reazon-nullo s)) 501 | ((reazon-pairo s) 502 | (reazon-fresh (d) 503 | (reazon-cdro s d) 504 | (reazon-listo d))))) 505 | 506 | (reazon-defrel reazon-appendo (l p out) 507 | "L appended to P produces OUT." 508 | (reazon-conde 509 | ((reazon-nullo l) (reazon-== p out)) 510 | ((reazon-fresh (a d res) 511 | (reazon-conso a d l) 512 | (reazon-conso a res out) 513 | (reazon-appendo d p res))))) 514 | 515 | (reazon-defrel reazon-assqo (x s out) 516 | "Retrieving X from the alist S produces OUT." 517 | (reazon-conde 518 | ((reazon-nullo s) (reazon-nullo out)) 519 | ((reazon-fresh (key val rest) 520 | (reazon-== s `((,key . ,val) ,@rest)) 521 | (reazon-conde 522 | ((reazon-== x key) (reazon-== out `(,key . ,val))) 523 | ((reazon-assqo x rest out))))))) 524 | 525 | (reazon-defrel reazon-membero (x s) 526 | "X exists in the list S." 527 | (reazon-fresh (a d) 528 | (reazon-== s `(,a . ,d)) 529 | (reazon-disj 530 | (reazon-== a x) 531 | (reazon-membero x d)))) 532 | 533 | (reazon-defrel reazon-precedeso (x y s) 534 | "X is the list of elements preceding Y in the list S." 535 | (reazon-fresh (a d) 536 | (reazon-conso a d s) 537 | (reazon-conde 538 | ((reazon-== x a) (reazon-membero y d)) 539 | ((reazon-precedeso x y d))))) 540 | 541 | (reazon-defrel reazon-immediately-precedeso (x y s) 542 | "X is the element immediately preceding Y in the list S." 543 | (reazon-fresh (a d) 544 | (reazon-conso a d s) 545 | (reazon-conde 546 | ((reazon-== x a) (reazon-caro d y)) 547 | ((reazon-immediately-precedeso x y d))))) 548 | 549 | (reazon-defrel reazon-adjacento (x y s) 550 | "X is an adjacent element to Y in the list S." 551 | (reazon-disj 552 | (reazon-immediately-precedeso x y s) 553 | (reazon-immediately-precedeso y x s))) 554 | 555 | (reazon-defrel reazon-subseto (subset set) 556 | "SUBSET is a subset of SET." 557 | (reazon-disj 558 | (reazon-== subset '()) 559 | (reazon-fresh (a d) 560 | (reazon-== subset `(,a . ,d)) 561 | (reazon-membero a set) 562 | (reazon-subseto d set)))) 563 | 564 | (reazon-defrel reazon-set-equalo (s1 s2) 565 | "S1 contains the same set of elements as S2." 566 | (reazon-subseto s1 s2) 567 | (reazon-subseto s2 s1)) 568 | 569 | 570 | (provide 'reazon) 571 | ;;; reazon.el ends here 572 | -------------------------------------------------------------------------------- /test/reazon-test-interface.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-interface.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Tests for the Reazon query interface and various means of query 24 | ;; combination. Most of these come from TRS. 25 | 26 | ;;; Code: 27 | 28 | (require 'reazon-test-utils) 29 | 30 | (reazon-defrel reazon-test--alwayso () 31 | "Infinite successful goals." 32 | (reazon-disj 33 | #'reazon-!S 34 | (reazon-test--alwayso))) 35 | 36 | (reazon-defrel reazon-test--nevero () 37 | "Infinite unsuccessful goals." 38 | (reazon-disj 39 | #'reazon-!U 40 | (reazon-test--nevero))) 41 | 42 | (ert-deftest reazon-test-interface-run () 43 | (reazon--should-equal '() 44 | (reazon-run* q 45 | #'reazon-!U)) 46 | (reazon--should-equal '(t) 47 | (reazon-run* q 48 | (reazon-== t q))) 49 | (reazon--should-equal '() 50 | (reazon-run* q 51 | #'reazon-!U 52 | (reazon-== t q))) 53 | (reazon--should-equal '(t) 54 | (reazon-run* q 55 | #'reazon-!S 56 | (reazon-== t q))) 57 | (reazon--should-equal '(corn) 58 | (reazon-run* r 59 | #'reazon-!S 60 | (reazon-== 'corn r))) 61 | (reazon--should-equal '(olive oil) 62 | (reazon-run* x 63 | (reazon-disj 64 | (reazon-== 'olive x) 65 | (reazon-== 'oil x)))) 66 | (reazon--should-equal '(oil olive) 67 | (reazon-run* x 68 | (reazon-disj 69 | (reazon-== 'oil x) 70 | (reazon-== 'olive x)))) 71 | (reazon--should-equal '(oil) 72 | (reazon-run* x 73 | (reazon-disj 74 | (reazon--conj-2 75 | (reazon-== 'olive x) 76 | #'reazon-!U) 77 | (reazon-== 'oil x)))) 78 | (reazon--should-equal '(olive _0 oil) 79 | (reazon-run* x 80 | (reazon-disj 81 | (reazon-conj 82 | (reazon-== 'virgin x) 83 | #'reazon-!U) 84 | (reazon-disj 85 | (reazon-== 'olive x) 86 | (reazon-disj 87 | #'reazon-!S 88 | (reazon-== 'oil x)))))) 89 | (reazon--should-equal '(done) 90 | (reazon-run 1 q 91 | (reazon-disj 92 | (reazon-== q 'done) 93 | (reazon-test--nevero))))) 94 | 95 | (ert-deftest reazon-test-interface-timeout () 96 | (reazon--should-equal '() 97 | (let ((reazon-timeout 0.01)) 98 | (reazon-run* q (reazon-test--nevero)))) 99 | ;; This test might fail if your computer is REALLY slow. 100 | (should 101 | (> 102 | (length 103 | (let ((reazon-timeout 0.01)) 104 | (reazon-run* q 105 | (reazon-test--alwayso)))) 106 | 3))) 107 | 108 | (ert-deftest reazon-test-interface-circular-query () 109 | (reazon--should-error 'reazon-circular-query 110 | (reazon-run* q 111 | (reazon-== q `(,q))) 112 | (reazon-run 10 q 113 | (reazon-== q `(,q))))) 114 | 115 | (ert-deftest reazon-test-interface-fresh () 116 | (reazon--should-equal '(t) 117 | (reazon-run* q 118 | (reazon-fresh (x) 119 | (reazon-== t x) 120 | (reazon-== t q)))) 121 | (reazon--should-equal '((_0 _1)) 122 | (reazon-run* s 123 | (reazon-fresh (x) 124 | (reazon-fresh (y) 125 | (reazon-== `(,x ,y) s))))) 126 | (reazon--should-equal '((_0 _1 _0)) 127 | (reazon-run* s 128 | (reazon-fresh (x y) 129 | (reazon-== `(,x ,y ,x) s)))) 130 | (reazon--should-equal '((split pea)) 131 | (reazon-run* r 132 | (reazon-fresh (x) 133 | (reazon-fresh (y) 134 | (reazon-== 'split x) 135 | (reazon-== 'pea y) 136 | (reazon-== `(,x ,y) r)))) 137 | (reazon-run* r 138 | (reazon-fresh (x) 139 | (reazon-fresh (y) 140 | (reazon-== 'split x) 141 | (reazon-== 'pea y) 142 | (reazon-== `(,x ,y) r)))) 143 | (reazon-run* r 144 | (reazon-fresh (x y) 145 | (reazon-== 'split x) 146 | (reazon-== 'pea y) 147 | (reazon-== `(,x ,y) r))) 148 | (reazon-run* (x y) 149 | (reazon-== 'split x) 150 | (reazon-== 'pea y))) 151 | (reazon--should-equal '(((split pea) split pea)) 152 | (reazon-run* (r x y) 153 | (reazon-== 'split x) 154 | (reazon-== 'pea y) 155 | (reazon-== `(,x ,y) r))) 156 | (reazon--should-equal '((_0 _1) (_0 _1)) 157 | (reazon-run* (x y) 158 | (reazon-fresh (z) 159 | (reazon-conde 160 | ((reazon-== x z) (reazon-fresh (z) (reazon-== y z))) 161 | ((reazon-fresh (z) (reazon-== x z)) (reazon-== y z)))))) 162 | (reazon--should-equal '((nil _0) (_0 nil)) 163 | (reazon-run* (x y) 164 | (reazon-fresh (z) 165 | (reazon-conde 166 | ((reazon-== x z) (reazon-fresh (z) (reazon-== y z))) 167 | ((reazon-fresh (z) (reazon-== x z)) (reazon-== y z))) 168 | (reazon-== nil z))))) 169 | 170 | (ert-deftest reazon-test-interface-project () 171 | (reazon--should-equal '(25) 172 | (reazon-run* q 173 | (reazon-fresh (x) 174 | (reazon-== x 5) 175 | (reazon-project (x) 176 | (reazon-== q (* x x)))))) 177 | (should-error 178 | (reazon-run* q 179 | (reazon-fresh (x) 180 | (reazon-project (x) 181 | (reazon-== q (* x x))) 182 | (reazon-== x 5))))) 183 | 184 | (reazon-defrel reazon--test-teacupo (x) 185 | "X is tea or cup." 186 | (reazon-disj (reazon-== x 'tea) (reazon-== x 'cup))) 187 | 188 | (ert-deftest reazon-test-interface-defrel () 189 | (reazon--should-equal '(tea cup) 190 | (reazon-run* x (reazon--test-teacupo x))) 191 | (reazon--should-equal "X is tea or cup." 192 | (documentation #'reazon--test-teacupo)) 193 | (reazon--should-equal '((nil t) (tea t) (cup t)) 194 | (reazon-run* (x y) 195 | (reazon-conde 196 | ((reazon--test-teacupo x) (reazon-== y t)) 197 | ((reazon-== x nil) (reazon-== y t))))) 198 | (reazon--should-equal '((tea tea) (tea cup) (cup tea) (cup cup)) 199 | (reazon-run* (x y) 200 | (reazon--test-teacupo x) 201 | (reazon--test-teacupo y))) 202 | (reazon--should-equal '(tea cup) 203 | (reazon-run* x 204 | (reazon--test-teacupo x) 205 | (reazon--test-teacupo x))) 206 | (reazon--should-equal '((nil tea) (nil cup) (tea _0) (cup _0)) 207 | (reazon-run* (x y) 208 | (reazon--disj-2 209 | (reazon--conj-2 210 | (reazon--test-teacupo x) 211 | (reazon--test-teacupo x)) 212 | (reazon--conj-2 213 | (reazon-== nil x) 214 | (reazon--test-teacupo y))))) 215 | (reazon--should-equal '((t tea) (t cup) (tea _0) (cup _0)) 216 | (reazon-run* (x y) 217 | (reazon-conde 218 | ((reazon--test-teacupo x) (reazon--test-teacupo x)) 219 | ((reazon-== x t) (reazon--test-teacupo y)))))) 220 | 221 | (reazon-defrel reazon--test-empty-relo (_x)) 222 | (reazon-defrel reazon--test-empty-relo-with-doc (_x) "docstring") 223 | 224 | (ert-deftest reazon-test-interface-empty-relation () 225 | (reazon--should-equal '(_0) 226 | (reazon-run* x (reazon--test-empty-relo-with-doc x))) 227 | (reazon--should-equal '(_0) 228 | (reazon-run* x (reazon--test-empty-relo x)))) 229 | 230 | (ert-deftest reazon--test-conde () 231 | (reazon--should-equal '() 232 | (reazon-run* q (reazon-conde))) 233 | (reazon--should-equal '((split pea) (navy bean) (red lentil)) 234 | (reazon-run* (x y) 235 | (reazon-conde 236 | ((reazon-== x 'split) (reazon-== y 'pea)) 237 | ((reazon-== x 'navy) (reazon-== y 'bean)) 238 | ((reazon-== x 'red) (reazon-== y 'lentil))))) 239 | (reazon--should-equal '(oil) 240 | (reazon-run* x 241 | (reazon-conde 242 | ((reazon-== x 'olive) #'reazon-!U) 243 | ((reazon-== x 'oil)))))) 244 | 245 | (ert-deftest reazon-test-interface-empty-conj-disj () 246 | (reazon--should-equal '(_0) 247 | (reazon-run* q) 248 | (reazon-run* q (reazon-conj))) 249 | (reazon--should-equal '() 250 | (reazon-run* q (reazon-disj)))) 251 | 252 | (ert-deftest reazon-test-interface-conda () 253 | (reazon--should-equal '() 254 | (reazon-run* q 255 | (reazon-conda 256 | (#'reazon-!U #'reazon-!S) 257 | (#'reazon-!U))) 258 | (reazon-run* q 259 | (reazon-conda 260 | (#'reazon-!S #'reazon-!U) 261 | (#'reazon-!S))) 262 | (reazon-run* x 263 | (reazon-conda 264 | ((reazon-== 'virgin x) #'reazon-!U) 265 | ((reazon-== 'olive x) #'reazon-!S) 266 | ((reazon-== 'oil x)))) 267 | (reazon-run* q 268 | (reazon-fresh (x y) 269 | (reazon-== 'split x) 270 | (reazon-== 'pea y) 271 | (reazon-conda 272 | ((reazon-== 'split x) (reazon-== x y)) 273 | (#'reazon-!S))))) 274 | (reazon--should-equal '(_0) 275 | (reazon-run* q 276 | (reazon-conda 277 | (#'reazon-!U #'reazon-!S) 278 | (#'reazon-!S))) 279 | (reazon-run* q 280 | (reazon-conda 281 | (#'reazon-!S #'reazon-!S) 282 | (#'reazon-!U))) 283 | (reazon-run* q 284 | (reazon-fresh (x y) 285 | (reazon-== 'split x) 286 | (reazon-== 'pea y) 287 | (reazon-conda 288 | ((reazon-== x y) (reazon-== 'split x)) 289 | (#'reazon-!S))))) 290 | (reazon--should-equal '(olive) 291 | (reazon-run* x 292 | (reazon-conda 293 | ((reazon-== 'olive x) #'reazon-!S) 294 | ((reazon-== 'oil x)))))) 295 | 296 | (reazon-defrel reazon--not-pasta (x) 297 | "X is not pasta." 298 | (reazon-conda 299 | ((reazon-== 'pasta x) #'reazon-!U) 300 | (#'reazon-!S))) 301 | 302 | (ert-deftest reazon-test-interface-not-pasta () 303 | (reazon--should-equal '(spaghetti) 304 | (reazon-run* x 305 | (reazon-conda 306 | ((reazon--not-pasta x) #'reazon-!U) 307 | ((reazon-== 'spaghetti x) #'reazon-!S) 308 | (#'reazon-!U)))) 309 | (reazon--should-equal '() 310 | (reazon-run* x 311 | (reazon-== 'spaghetti x) 312 | (reazon-conda 313 | ((reazon--not-pasta x) #'reazon-!U) 314 | ((reazon-== 'spaghetti x) #'reazon-!S) 315 | (#'reazon-!U))))) 316 | 317 | (reazon-defrel reazon--test-onceo (goal) 318 | "???" 319 | (reazon-condu 320 | (goal #'reazon-!S) 321 | (#'reazon-!U))) 322 | 323 | (ert-deftest reazon-test-interface-condu () 324 | (reazon--should-equal '(tea) 325 | (reazon-run* x 326 | (reazon--test-onceo 327 | (reazon--test-teacupo x))))) 328 | 329 | (ert-deftest reazon-test-interface-condeau () 330 | (reazon--should-equal '(5 tea cup) 331 | (reazon-run* q 332 | (reazon-conde 333 | ((reazon--test-teacupo q) (reazon-== 0 0)) 334 | ((reazon-== q 5) (reazon-== 0 0))))) 335 | (reazon--should-equal '(tea cup) 336 | (reazon-run* q 337 | (reazon-conda 338 | ((reazon--test-teacupo q) (reazon-== 0 0)) 339 | ((reazon-== q 5))))) 340 | (reazon--should-equal '(tea) 341 | (reazon-run* q 342 | (reazon-condu 343 | ((reazon--test-teacupo q) (reazon-== 0 0)) 344 | ((reazon-== q 5))))) 345 | (reazon--should-equal '() 346 | (reazon-run* q (reazon-conde)) 347 | (reazon-run* q (reazon-conda)) 348 | (reazon-run* q (reazon-condu)))) 349 | 350 | (ert-deftest reazon-test-interface-conda-bug () 351 | :expected-result :failed 352 | (reazon--should-equal '(a1) 353 | (reazon-run* x 354 | (reazon-conde 355 | ((reazon-== x 'a1)) 356 | ((reazon-== x 'a2))) 357 | (reazon-conda 358 | ((reazon-== x 'a2) #'reazon-!U) 359 | (#'reazon-!S))) 360 | (reazon-run* (x) 361 | (reazon-conde 362 | ((reazon-== x 'a1)) 363 | ((reazon-== x 'a2))) 364 | (reazon-conda 365 | ((reazon-== x 'a2) (reazon-== x 'c)) 366 | ((reazon-== x x)))))) 367 | 368 | (provide 'reazon-test-interface) 369 | ;;; reazon-test-interface.el ends here 370 | -------------------------------------------------------------------------------- /test/reazon-test-prolog.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-prolog.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Tests for Prolog-style database queries. 24 | 25 | ;;; Code: 26 | 27 | (require 'reazon-test-utils) 28 | 29 | (reazon-defrel reazon--test-likes (a b) 30 | "A likes B." 31 | (reazon-conde 32 | ((reazon-== a 'kim) (reazon-== b 'robin)) 33 | ((reazon-== a 'sandy) (reazon-== b 'lee)) 34 | ((reazon-== a 'sandy) (reazon-== b 'kim)) 35 | ((reazon-== a 'robin) (reazon-== b 'cats)) 36 | ;; ((reazon-== a 'sandy) (reazon--test-likes b 'cats)) 37 | ((reazon-fresh (x) 38 | (reazon-== a 'sandy) 39 | (reazon-== b x) 40 | (reazon--test-likes x 'cats))) 41 | ;; ((reazon-== a 'kim) (reazon--test-likes b 'lee) (reazon--test-likes b 'kim)) 42 | ((reazon-fresh (x) 43 | (reazon-== a 'kim) 44 | (reazon-== b x) 45 | (reazon--test-likes x 'lee) 46 | (reazon--test-likes x 'kim))) 47 | ;; ((reazon-== a b)) 48 | ((reazon-fresh (x) 49 | (reazon-== a x) 50 | (reazon-== b x))))) 51 | 52 | (ert-deftest reazon-test-prolog-likes () 53 | "This is an example from the chapter on Prolog in Norvig's PAIP." 54 | (reazon--should-equal '(lee kim sandy robin sandy cats) 55 | (reazon-run* who 56 | (reazon--test-likes 'sandy who))) 57 | (reazon--should-equal '(sandy sandy kim) 58 | (reazon-run* who 59 | (reazon--test-likes who 'sandy))) 60 | (reazon--should-equal '() 61 | (reazon-run* q 62 | (reazon--test-likes 'robin 'lee))) 63 | (reazon--should-equal 64 | '((sandy kim) 65 | (_0 _0) 66 | (sandy sandy) 67 | (sandy sandy) 68 | (sandy sandy) 69 | (kim sandy)) 70 | (reazon-run* (x y) 71 | (reazon--test-likes x y) 72 | (reazon--test-likes y x)))) 73 | 74 | ;; From the SICP section on Prolog (4.4), the personnel records 75 | ;; database for "Microshaft, a thriving high-technology company in the 76 | ;; Boston area". 77 | 78 | (reazon-defrel reazon--microshaft-address (name address) 79 | "Addresses for Microshaft personnel." 80 | (reazon-membero 81 | `(,name ,address) 82 | '(((Bitdiddle Ben) (Slumerville (Ridge Road) 10)) 83 | ((Hacker Alyssa P) (Cambridge (Mass Ave) 78)) 84 | ((Fect Cy D) (Cambridge (Ames Street) 3)) 85 | ((Tweakit Lem E) (Boston (Bay State Road) 22)) 86 | ((Reasoner Louis) (Slumerville (Pine Tree Road) 80)) 87 | ((Warbucks Oliver) (Swellesley (Top Heap Road))) 88 | ((Scrooge Eben) (Weston (Shady Lane) 10)) 89 | ((Cratchet Robert) (Allston (N Harvard Street) 16)) 90 | ((Aull DeWitt) (Slumerville (Onion Square) 5))))) 91 | 92 | (reazon-defrel reazon--microshaft-job (name job) 93 | "Job titles for Microshaft personnel." 94 | (reazon-membero 95 | `(,name ,job) 96 | '(((Bitdiddle Ben) (computer wizard)) 97 | ((Hacker Alyssa P) (computer programmer)) 98 | ((Fect Cy D) (computer programmer)) 99 | ((Tweakit Lem E) (computer technician)) 100 | ((Reasoner Louis) (computer programmer trainee)) 101 | ((Warbucks Oliver) (administration big wheel)) 102 | ((Scrooge Eben) (accounting chief accountant)) 103 | ((Cratchet Robert) (accounting scrivener)) 104 | ((Aull DeWitt) (administration secretary))))) 105 | 106 | (reazon-defrel reazon--microshaft-salary (name salary) 107 | "Salary for Microshaft personnel. 108 | At least I think that's what it is. This data 109 | comes from a thumbdrive I found in the employee 110 | parking lot labeled \"SALARY INFO -- DO NOT 111 | SHARE\"." 112 | (reazon-membero 113 | `(,name ,salary) 114 | '(((Bitdiddle Ben) 60000) 115 | ((Hacker Alyssa P) 40000) 116 | ((Fect Cy D) 35000) 117 | ((Tweakit Lem E) 25000) 118 | ((Reasoner Louis) 30000) 119 | ((Warbucks Oliver) 150000) 120 | ((Scrooge Eben) 75000) 121 | ((Cratchet Robert) 18000) 122 | ((Aull DeWitt) 25000)))) 123 | 124 | (reazon-defrel reazon--microshaft-supervisor (name supervisor) 125 | "The Microshaft org chart." 126 | (reazon-membero 127 | `(,name ,supervisor) 128 | '(((Bitdiddle Ben) (Warbucks Oliver)) 129 | ((Hacker Alyssa P) (Bitdiddle Ben)) 130 | ((Fect Cy D) (Bitdiddle Ben)) 131 | ((Tweakit Lem E) (Bitdiddle Ben)) 132 | ((Reasoner Louis) (Hacker Alyssa P)) 133 | ((Scrooge Eben) (Warbucks Oliver)) 134 | ((Cratchet Robert) (Scrooge Eben)) 135 | ((Aull DeWitt) (Warbucks Oliver))))) 136 | 137 | (reazon-defrel reazon--microshaft-lives-near (a b) 138 | "A lives near B" 139 | (reazon-fresh (town rest-a rest-b) 140 | (reazon--microshaft-address a `(,town . ,rest-a)) 141 | (reazon--microshaft-address b `(,town . ,rest-b)) 142 | (reazon-project (a b) 143 | (reazon-== nil (equal a b))))) 144 | 145 | (reazon-defrel reazon--microshaft-wheel (wheel) 146 | "Big wheels keep on turning." 147 | (reazon-fresh (middle-manager underling) 148 | (reazon--microshaft-supervisor middle-manager wheel) 149 | (reazon--microshaft-supervisor underling middle-manager))) 150 | 151 | (reazon-defrel reazon--microshaft-meeting (division time) 152 | "Gotta meet all day til the meetin's done." 153 | (reazon-membero 154 | `(,division ,time) 155 | '((accounting (Monday 9am)) 156 | (administration (Monday 10am)) 157 | (computer (Wednesday 3pm)) 158 | (administration (Friday 1pm)) 159 | (whole-company (Wednesday 4pm))))) 160 | 161 | (reazon-defrel reazon--microshaft-meeting-time (name day-and-time) 162 | "It's always meeting time somewhere." 163 | (reazon-fresh (division job) 164 | (reazon-disj 165 | (reazon-== division 'whole-company) 166 | (reazon--microshaft-job name `(,division . ,job))) 167 | (reazon--microshaft-meeting division day-and-time))) 168 | 169 | (ert-deftest reazon-test-prolog-microshaft () 170 | ;; The addresses of all the computer programmers 171 | (reazon--should-equal '(((Hacker Alyssa P) (Cambridge (Mass Ave) 78)) 172 | ((Fect Cy D) (Cambridge (Ames Street) 3))) 173 | (reazon-run* (name address) 174 | (reazon--microshaft-job name '(computer programmer)) 175 | (reazon--microshaft-address name address))) 176 | ;; Everyone supervised by Ben Bitdiddle or Alyssa P Hacker 177 | (reazon--should-equal '(((Hacker Alyssa P) (Bitdiddle Ben)) 178 | ((Fect Cy D) (Bitdiddle Ben)) 179 | ((Tweakit Lem E) (Bitdiddle Ben)) 180 | ((Reasoner Louis) (Hacker Alyssa P))) 181 | (reazon-run* (name supervisor) 182 | (reazon-disj 183 | (reazon-== supervisor '(Bitdiddle Ben)) 184 | (reazon-== supervisor '(Hacker Alyssa P))) 185 | (reazon--microshaft-supervisor name supervisor))) 186 | ;; Everyone supervised by Ben Bitdiddle who is not a computer programmer 187 | (reazon--should-equal '(((Tweakit Lem E) (computer technician))) 188 | (reazon-run* (name job) 189 | (reazon--microshaft-supervisor name '(Bitdiddle Ben)) 190 | (reazon--microshaft-job name job) 191 | (reazon-project (job) 192 | (reazon-== nil (equal job '(computer programmer)))))) 193 | ;; Everyone who makes more than 30000 194 | (reazon--should-equal '(((Bitdiddle Ben) 60000) 195 | ((Hacker Alyssa P) 40000) 196 | ((Fect Cy D) 35000) 197 | ((Warbucks Oliver) 150000) 198 | ((Scrooge Eben) 75000)) 199 | (reazon-run* (name salary) 200 | (reazon--microshaft-salary name salary) 201 | (reazon-project (salary) 202 | (reazon-== t (> salary 30000))))) 203 | ;; Everyone who lives near Ben Bitdiddle 204 | (reazon--should-equal '((Reasoner Louis) (Aull DeWitt)) 205 | (reazon-run* name 206 | (reazon--microshaft-lives-near name '(Bitdiddle Ben)))) 207 | ;; All the wheels (see SICP exercise 4.65) 208 | (reazon--should-equal '((Warbucks Oliver) 209 | (Warbucks Oliver) 210 | (Warbucks Oliver) 211 | (Bitdiddle Ben) 212 | (Warbucks Oliver)) 213 | (reazon-run* name 214 | (reazon--microshaft-wheel name))) 215 | ;; All the Friday meetings (SICP ex 4.59a) 216 | (reazon--should-equal '((administration (Friday 1pm))) 217 | (reazon-run* (division time) 218 | (reazon-caro time 'Friday) 219 | (reazon--microshaft-meeting division time))) 220 | ;; The times of Alyssa's Wednesday meetings (SICP ex 4.59c) 221 | (reazon--should-equal '((4pm) (3pm)) 222 | (reazon-run* (time) 223 | (reazon--microshaft-meeting-time '(Hacker Alyssa P) `(Wednesday ,time))))) 224 | 225 | 226 | (provide 'reazon-test-prolog) 227 | ;;; reazon-test-prolog.el ends here 228 | -------------------------------------------------------------------------------- /test/reazon-test-puzzle.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-puzzle.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; 24 | 25 | ;;; Code: 26 | 27 | (require 'reazon-test-utils) 28 | 29 | (ert-deftest reazon-test-puzzle-yachts () 30 | ;; Mary Ann's father has a yacht and so has each of his four 31 | ;; friends. They are: Mr. Moore, Colonel Downing, Mr. Hall, Sir 32 | ;; Barnacle Hood, and Dr. Parker. Each of the five also has one 33 | ;; daughter and each has named his yacht after a daughter of one of 34 | ;; the others. Sir Barnacle's yacht is the Gabrielle, Mr. Moore owns 35 | ;; the Lorna; Mr. Hall the Rosalind. The Melissa, owned by Colonel 36 | ;; Downing, is named after Sir Barnacle's daughter. Gabrielle's 37 | ;; father owns the yacht that is named after Dr. Parker's daughter. 38 | ;; Who is Lorna's father? 39 | (let ((names '(mary-anne gabrielle lorna rosalind melissa))) 40 | (reazon-run* q 41 | (reazon-fresh (a b c d) 42 | (reazon-== q `((barnacle-hood melissa gabrielle) ,a ,b ,c ,d)) 43 | (reazon-fresh (daughter) 44 | (reazon-== a `(moore ,daughter lorna)) 45 | (reazon-membero daughter names)) 46 | (reazon-fresh (daughter) 47 | (reazon-== b `(downing ,daughter melissa)) 48 | (reazon-membero daughter names)) 49 | (reazon-fresh (daughter) 50 | (reazon-== c `(hall ,daughter rosalind)) 51 | (reazon-membero daughter names)) 52 | (reazon-fresh (common-name yacht other other-father) 53 | (reazon-== d `(parker ,common-name ,yacht)) 54 | (reazon-== other `(,other-father gabrielle ,common-name)) 55 | (reazon-membero common-name names) 56 | (reazon-membero other q)))))) 57 | 58 | (ert-deftest reazon-test-puzzle-zebra () 59 | (reazon--should-equal '(((yel nrw wat koo fox) 60 | (blu ukr tea chs hrs) 61 | (red eng mlk olg snl) 62 | (ivr spn ojj lks dog) 63 | (grn jap cof prl zeb))) 64 | (reazon-run* q 65 | ;; Represent houses as quintuples: (color nationality drink smoke pet) 66 | 67 | ;; 1 There are five houses. 68 | (reazon-fresh (a b c d e) 69 | (reazon-== q `(,a ,b ,c ,d ,e)) 70 | 71 | ;; Ordering-related clues 72 | 73 | ;; 10 The Norwegian lives in the first house. 74 | (reazon-fresh (col drn smk pet) 75 | (reazon-== a `(,col nrw ,drn ,smk ,pet))) 76 | 77 | ;; 15 The Norwegian lives next to the blue house. 78 | (reazon-fresh (nat drn smk pet) 79 | (reazon-== b `(blu ,nat ,drn ,smk ,pet))) 80 | 81 | ;; 9 Milk is drunk in the middle house. 82 | (reazon-fresh (col nat smk pet) 83 | (reazon-== c `(,col ,nat mlk ,smk ,pet))) 84 | 85 | ;; 6 The green house is immediately to the right of the ivory house. 86 | ;; 4 Coffee is drunk in the green house. 87 | (reazon-fresh (ho1 ho2 nt1 nt2 dr1 sm1 sm2 pt1 pt2) 88 | (reazon-== ho1 `(ivr ,nt1 ,dr1 ,sm1 ,pt1)) 89 | (reazon-== ho2 `(grn ,nt2 cof ,sm2 ,pt2)) 90 | (reazon-immediately-precedeso ho1 ho2 q)) 91 | 92 | ;; 11 The man who smokes Chesterfields lives in the house next to the man with the fox. 93 | (reazon-fresh (ho1 ho2 co1 co2 nt1 nt2 dr1 dr2 sm2 pt1) 94 | (reazon-== ho1 `(,co1 ,nt1 ,dr1 chs ,pt1)) 95 | (reazon-== ho2 `(,co2 ,nt2 ,dr2 ,sm2 fox)) 96 | (reazon-adjacento ho1 ho2 q)) 97 | 98 | ;; 12 Kools are smoked in the house next to the house where the horse is kept. 99 | ;; 8 Kools are smoked in the yellow house. 100 | (reazon-fresh (ho1 ho2 co2 nt1 nt2 dr1 dr2 sm2 pt1) 101 | (reazon-== ho1 `(yel ,nt1 ,dr1 koo ,pt1)) 102 | (reazon-== ho2 `(,co2 ,nt2 ,dr2 ,sm2 hrs)) 103 | (reazon-adjacento ho1 ho2 q))) 104 | 105 | ;; General clues 106 | 107 | ;; 2 The Englishman lives in the red house. 108 | (reazon-fresh (hou drn smk pet) 109 | (reazon-== hou `(red eng ,drn ,smk ,pet)) 110 | (reazon-membero hou q)) 111 | 112 | ;; 3 The Spaniard owns the dog. 113 | (reazon-fresh (hou col drn smk) 114 | (reazon-== hou `(,col spn ,drn ,smk dog)) 115 | (reazon-membero hou q)) 116 | 117 | ;; 5 The Ukrainian drinks tea. 118 | (reazon-fresh (hou col smk pet) 119 | (reazon-== hou `(,col ukr tea ,smk ,pet)) 120 | (reazon-membero hou q)) 121 | 122 | ;; 7 The Old Gold smoker owns snails. 123 | (reazon-fresh (hou col nat drn) 124 | (reazon-== hou `(,col ,nat ,drn olg snl)) 125 | (reazon-membero hou q)) 126 | 127 | ;; 13 The Lucky Strike smoker drinks orange juice. 128 | (reazon-fresh (hou col nat pet) 129 | (reazon-== hou `(,col ,nat ojj lks ,pet)) 130 | (reazon-membero hou q)) 131 | 132 | ;; 14 The Japanese smokes Parliaments. 133 | (reazon-fresh (hou col drn pet) 134 | (reazon-== hou `(,col jap ,drn prl ,pet)) 135 | (reazon-membero hou q)) 136 | 137 | ;; Now, who drinks water? 138 | (reazon-fresh (hou col nat smk pet) 139 | (reazon-== hou `(,col ,nat wat ,smk ,pet)) 140 | (reazon-membero hou q)) 141 | 142 | ;; Who owns the zebra? 143 | (reazon-fresh (hou col nat drn smk) 144 | (reazon-== hou `(,col ,nat ,drn ,smk zeb)) 145 | (reazon-membero hou q))))) 146 | 147 | (ert-deftest reazon-test-puzzle-interrogation () 148 | ;; Three men were once arrested for a crime which beyond a shadow of 149 | ;; a doubt had been committed by one of them. Preliminary 150 | ;; questioning disclosed the curious fact that one of the suspects 151 | ;; was a highly respected judge, one just an average citizen, and 152 | ;; one a notorious crook. In what follows they will be referred to 153 | ;; as Brown, Jones, and Smith, though not necessarily respectively. 154 | ;; Each man made two statements to the police, which were in effect 155 | ;; 156 | ;; Brown: 157 | ;; I didn't do it. 158 | ;; Jones didn't do it. 159 | ;; 160 | ;; Jones: 161 | ;; Brown didn't do it. 162 | ;; Smith did it. 163 | ;; 164 | ;; Smith: 165 | ;; I didn't do it. 166 | ;; Brown did it. 167 | ;; 168 | ;; Further investigation showed, as might have been expected, that 169 | ;; both statements made by the judge were true, both statements made 170 | ;; by the criminal were false, and of the two statements made by the 171 | ;; average man one was true and one was false. 172 | ;; 173 | ;; Which of the three men was the judge, the average citizen, the 174 | ;; crook? And who committed the crime? 175 | 176 | (reazon--should-equal '(((brown a y) (jones c n) (smith j n))) 177 | (reazon-run* q 178 | ;; s(tatus): j(udge), a(verage citizen), c(rook) 179 | ;; v(erdict): y(es), n(o) 180 | (reazon-fresh (bs bv js jv ss sv) 181 | 182 | (reazon-== q `((brown ,bs ,bv) (jones ,js ,jv) (smith ,ss ,sv))) 183 | 184 | (reazon-subseto '(j a c) `(,bs ,js ,ss)) 185 | 186 | ;; This feels inelegant. 187 | (reazon-membero `(,bv ,jv ,sv) '((y n n) (n y n) (n n y))) 188 | 189 | (reazon-conde 190 | ;; Brown is the judge. 191 | ((reazon-== `(,bs ,bv jv) '(j n n))) 192 | ;; Brown is the crook. 193 | ((reazon-== `(,bs ,bv ,jv) '(c y y))) 194 | ;; Brown is the average citizen. 195 | ((reazon-== bs 'a) 196 | (reazon-disj 197 | (reazon-== `(,bv ,jv) '(y n)) 198 | (reazon-== `(,bv ,jv) '(n y))))) 199 | 200 | (reazon-conde 201 | ;; Jones is the judge. 202 | ((reazon-== `(,js ,bv ,sv) '(j n y))) 203 | ;; Jones is the crook. 204 | ((reazon-== `(,js ,bv ,sv) '(c y n))) 205 | ;; Jones is the average citizen. 206 | ((reazon-== js 'a) 207 | (reazon-disj 208 | (reazon-== `(,bv ,sv) '(n n)) 209 | (reazon-== `(,bv ,sv) '(y y))))) 210 | 211 | (reazon-conde 212 | ;; Smith is the judge. 213 | ((reazon-== `(,ss ,sv ,bv) '(j n y))) 214 | ;; Smith is the crook. 215 | ((reazon-== `(,ss ,sv ,bv) '(c y n))) 216 | ;; Smith is the average citizen. 217 | ((reazon-== ss 'a) 218 | (reazon-disj 219 | (reazon-== `(,sv ,bv) '(n n)) 220 | (reazon-== `(,sv ,bv) '(y n))))))))) 221 | 222 | (ert-deftest reazon-test-puzzle-exam () 223 | ;; Five schoolgirls sat for an examination. Their parents -- so they 224 | ;; thought -- showed an undue degree of interest in the result. They 225 | ;; therefore agreed that, in writing home about the examination, 226 | ;; each girl should make one true statement and one untrue one. The 227 | ;; following are the relevant passages from their letters: 228 | 229 | ;; > Betty: "Kitty was second in the examination. I was only third." 230 | ;; > Ethel: "You'll be glad to hear that I was on top. Joan was second." 231 | ;; > Joan: "I was third, and poor old Ethel was bottom." 232 | ;; > Kitty: "I came out second. Mary was only fourth." 233 | ;; > Mary: "I was fourth. Top place was taken by Betty." 234 | 235 | ;; What in fact was the order in which the five girls were placed? 236 | (reazon--should-equal '((kitty joan betty mary ethel)) 237 | (reazon-run* q 238 | (reazon-fresh (a b c d e) 239 | ;; Betty: "Kitty was second in the examination. I was only third." 240 | (reazon-disj 241 | (reazon-== q `(,a kitty ,c ,d ,e)) 242 | (reazon-== q `(,a ,b betty ,d ,e))) 243 | ;; Ethel: "You'll be glad to hear that I was on top. Joan was second." 244 | (reazon-disj 245 | (reazon-== q `(ethel ,b ,c ,d ,e)) 246 | (reazon-== q `(,a joan ,c ,d ,e))) 247 | ;; Joan: "I was third, and poor old Ethel was bottom." 248 | (reazon-disj 249 | (reazon-== q `(,a ,b joan ,d ,e)) 250 | (reazon-== q `(,a ,b ,c ,d ethel))) 251 | ;; Kitty: "I came out second. Mary was only fourth." 252 | (reazon-disj 253 | ;; Explicity enumerate possibilities to simulate negation. 254 | (reazon-disj 255 | (reazon-== q `(mary kitty ,c ,d ,e)) 256 | (reazon-== q `(,a kitty mary ,d ,e)) 257 | (reazon-== q `(,a kitty ,c ,d mary))) 258 | (reazon-disj 259 | (reazon-== q `(kitty ,b ,c mary ,e)) 260 | (reazon-== q `(,a ,b kitty mary ,e)) 261 | (reazon-== q `(,a ,b ,c mary kitty)))) 262 | ;; Mary: "I was fourth. Top place was taken by Betty." 263 | (reazon-disj 264 | (reazon-== q `(,a ,b ,c mary ,e)) 265 | (reazon-== q `(betty ,b ,c ,d ,e)))) 266 | (reazon-subseto '(betty ethel joan kitty mary) q)))) 267 | 268 | (ert-deftest reazon-test-puzzle-ages () 269 | ;; Tom and Betty have the same birthday and are both in their 270 | ;; twenties. He is four times as old as she was when he was three 271 | ;; times as old as she was when he was twice as old as she was. How 272 | ;; old are they? 273 | (reazon--should-equal '((24 21)) 274 | (reazon-run* (tom betty) 275 | (reazon-subseto `(,tom ,betty) (number-sequence 20 29)) 276 | (reazon-project (tom betty) 277 | (reazon-fresh (diff) 278 | (reazon-== diff (- tom betty)) 279 | (reazon-project (diff) 280 | (reazon-fresh (b1) 281 | (reazon-== t (= 0 (mod tom 4))) 282 | (reazon-== b1 (/ tom 4)) 283 | (reazon-project (b1) 284 | (reazon-fresh (t1) 285 | (reazon-== t1 (+ b1 diff)) 286 | (reazon-project (t1) 287 | (reazon-fresh (b2) 288 | (reazon-== t (= 0 (mod t1 3))) 289 | (reazon-== b2 (/ t1 3)) 290 | (reazon-project (b2) 291 | (reazon-fresh (t2) 292 | (reazon-== t2 (+ b2 diff)) 293 | (reazon-== t2 (* b2 2))))))))))))) 294 | ;; Same as the last one, but with the fresh variables hoisted. 295 | (reazon-run* (tom betty) 296 | (reazon-subseto `(,tom ,betty) (number-sequence 20 29)) 297 | (reazon-fresh (diff b1 t1 b2 t2) 298 | (reazon-project (tom betty) 299 | (reazon-== diff (- tom betty)) 300 | (reazon-project (diff) 301 | (reazon-== t (= 0 (mod tom 4))) 302 | (reazon-== b1 (/ tom 4)) 303 | (reazon-project (b1) 304 | (reazon-== t1 (+ b1 diff)) 305 | (reazon-project (t1) 306 | (reazon-== t (= 0 (mod t1 3))) 307 | (reazon-== b2 (/ t1 3)) 308 | (reazon-project (b2) 309 | (reazon-== t2 (+ b2 diff)) 310 | (reazon-== t2 (* b2 2))))))))))) 311 | 312 | 313 | (provide 'reazon-test-puzzle) 314 | ;;; reazon-test-puzzle.el ends here 315 | -------------------------------------------------------------------------------- /test/reazon-test-relation.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-relations.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Tests for builtin Reazon relations. Many of them come from TRS, and 24 | ;; it's probably obvious which ones. 25 | 26 | ;;; Code: 27 | 28 | (require 'reazon-test-utils) 29 | 30 | (ert-deftest reazon-test-relation-caro () 31 | (reazon--should-equal '(a) 32 | (reazon-run* p 33 | (reazon-caro '(a c o r n) p))) 34 | (reazon--should-equal '(t) 35 | (reazon-run* q 36 | (reazon-caro '(a c o r n) 'a) 37 | (reazon-== q t))) 38 | (reazon--should-equal '(pear) 39 | (reazon-run* r 40 | (reazon-fresh (x y) 41 | (reazon-caro `(,r ,y) x) 42 | (reazon-== x 'pear)))) 43 | (reazon--should-equal '((grape a)) 44 | (reazon-run* r 45 | (reazon-fresh (x y) 46 | (reazon-fresh (d) (reazon-== (cons x d) '(grape raisin pear))) 47 | (reazon-fresh (d) (reazon-== (cons y d) '((a) (b) (c)))) 48 | (reazon-== r (cons x y)))) 49 | (reazon-run* r 50 | (reazon-fresh (x y) 51 | (reazon-caro '(grape raisin pear) x) 52 | (reazon-caro '((a) (b) (c)) y) 53 | (reazon-== r (cons x y)))))) 54 | 55 | (ert-deftest reazon-test-relation-cdro () 56 | (reazon--should-equal '(c) 57 | (reazon-run* r 58 | (reazon-fresh (v) 59 | (reazon-cdro '(a c o r n) v) 60 | (reazon-caro v r)))) 61 | (reazon--should-equal '(((raisin pear) a)) 62 | (reazon-run* r 63 | (reazon-fresh (x y) 64 | (reazon-cdro '(grape raisin pear) x) 65 | (reazon-caro '((a) (b) (c)) y) 66 | (reazon-== r (cons x y))))) 67 | (reazon--should-equal '(t) 68 | (reazon-run* q 69 | (reazon-cdro '(a c o r n) '(c o r n)) 70 | (reazon-== q t))) 71 | (reazon--should-equal '(o) 72 | (reazon-run* x 73 | (reazon-cdro '(c o r n) `(,x r n)))) 74 | (reazon--should-equal '((a c o r n)) 75 | (reazon-run* l 76 | (reazon-fresh (x) 77 | (reazon-cdro l '(c o r n)) 78 | (reazon-caro l x) 79 | (reazon-== x 'a))))) 80 | 81 | (ert-deftest reazon-test-relation-conso () 82 | (reazon--should-equal '(((a b c) d e f)) 83 | (reazon-run* l 84 | (reazon-conso '(a b c) '(d e f) l))) 85 | (reazon--should-equal '(d) 86 | (reazon-run* x 87 | (reazon-conso x '(a b c) '(d a b c)))) 88 | (reazon--should-equal '((e a d c )) 89 | (reazon-run* r 90 | (reazon-fresh (x y z) 91 | (reazon-== r `(e a d ,x)) 92 | (reazon-conso y `(a ,z c) r)))) 93 | (reazon--should-equal '((d a d c)) 94 | (reazon-run* l 95 | (reazon-fresh (x) 96 | (reazon-== l `(d a ,x c)) 97 | (reazon-conso x `(a ,x c) l))) 98 | (reazon-run* l 99 | (reazon-fresh (x) 100 | (reazon-conso x `(a ,x c) l) 101 | (reazon-== l `(d a ,x c))))) 102 | (reazon--should-equal '((b o n u s)) 103 | (reazon-run* l 104 | (reazon-fresh (d p x y w) 105 | (reazon-conso w '(n u s) p) 106 | (reazon-cdro l p) 107 | (reazon-caro l x) 108 | (reazon-== x 'b) 109 | (reazon-cdro l d) 110 | (reazon-caro d y) 111 | (reazon-== y 'o))))) 112 | 113 | (ert-deftest reazon-test-relation-nullo () 114 | (reazon--should-equal '() 115 | (reazon-run* q 116 | (reazon-nullo '(grape raisin pear)) 117 | (reazon-== q t))) 118 | (reazon--should-equal '(t) 119 | (reazon-run* q 120 | (reazon-nullo '()) 121 | (reazon-== q t))) 122 | (reazon--should-equal '(()) 123 | (reazon-run* x 124 | (reazon-nullo x)))) 125 | 126 | (ert-deftest reazon-test-relation-pairo () 127 | (reazon--should-equal '(t) 128 | (reazon-run* q 129 | (reazon-pairo (cons q q)) 130 | (reazon-== q t))) 131 | (reazon--should-equal '() 132 | (reazon-run* q 133 | (reazon-pairo '()) 134 | (reazon-== q t)) 135 | (reazon-run* q 136 | (reazon-pairo 'pair) 137 | (reazon-== q t))) 138 | (reazon--should-equal '((_0 . _1)) 139 | (reazon-run* x 140 | (reazon-pairo x)))) 141 | 142 | (ert-deftest reazon-test-relation-appendo () 143 | (reazon--should-equal '(() (_0) (_0 _1) (_0 _1 _2) (_0 _1 _2 _3)) 144 | (reazon-run 5 x 145 | (reazon-fresh (y z) 146 | (reazon-appendo x y z)))) 147 | (reazon--should-equal '(_0 _0 _0 _0 _0) 148 | (reazon-run 5 y 149 | (reazon-fresh (x z) 150 | (reazon-appendo x y z)))) 151 | (reazon--should-equal '(_0 (_0 . _1) (_0 _1 . _2) (_0 _1 _2 . _3) (_0 _1 _2 _3 . _4)) 152 | (reazon-run 5 z 153 | (reazon-fresh (x y) 154 | (reazon-appendo x y z)))) 155 | (reazon--should-equal '((cake tastes yummy)) 156 | (reazon-run* x 157 | (reazon-appendo 158 | '(cake) 159 | '(tastes yummy) 160 | x))) 161 | (reazon--should-equal '((cake with ice _0 tastes yummy)) 162 | (reazon-run* x 163 | (reazon-fresh (y) 164 | (reazon-appendo 165 | `(cake with ice ,y) 166 | '(tastes yummy) 167 | x)))) 168 | (reazon--should-equal '((cake with ice cream . _0)) 169 | (reazon-run* x 170 | (reazon-fresh (y) 171 | (reazon-appendo 172 | '(cake with ice cream) 173 | y 174 | x)))) 175 | (reazon--should-equal '((cake with ice d t) 176 | (cake with ice _0 d t) 177 | (cake with ice _0 _1 d t) 178 | (cake with ice _0 _1 _2 d t) 179 | (cake with ice _0 _1 _2 _3 d t)) 180 | (reazon-run 5 x 181 | (reazon-fresh (y) 182 | (reazon-appendo `(cake with ice . ,y) '(d t) x)))) 183 | (reazon--should-equal '(() (_0) (_0 _1) (_0 _1 _2) (_0 _1 _2 _3)) 184 | (reazon-run 5 y 185 | (reazon-fresh (x) 186 | (reazon-appendo `(cake with ice . ,y) '(d t) x)))) 187 | (reazon--should-equal '((() (cake with ice d t)) 188 | ((cake) (with ice d t)) 189 | ((cake with) (ice d t)) 190 | ((cake with ice) (d t)) 191 | ((cake with ice d) (t)) 192 | ((cake with ice d t) ())) 193 | (reazon-run 6 (x y) 194 | (reazon-appendo x y '(cake with ice d t))) 195 | (reazon-run* (x y) 196 | (reazon-appendo x y '(cake with ice d t))))) 197 | 198 | (ert-deftest reazon-test-relations-assqo () 199 | ;; Verify that assqo matches assq 200 | (let ((results 201 | (reazon-run 100 (x s out) 202 | (reazon-assqo x s out)))) 203 | (dolist (res results) 204 | (pcase res 205 | (`(,x ,s ,out) 206 | (reazon--should-equal out 207 | (assq x s))))))) 208 | 209 | (ert-deftest reazon-test-relation-listo () 210 | (reazon--should-equal '(() (_0) (_0 _1) (_0 _1 _2) (_0 _1 _2 _3)) 211 | (reazon-run 5 q 212 | (reazon-listo q)) 213 | (reazon-run 5 q 214 | (reazon-listo `(a b c . ,q))))) 215 | 216 | (ert-deftest reazon-test-relation-membero () 217 | (reazon--should-equal '(e) 218 | (reazon-run* x 219 | (reazon-membero 'e `(pasta ,x fagioli)))) 220 | (reazon--should-equal '((e _0) (_0 e)) 221 | (reazon-run* (x y) 222 | (reazon-membero 'e `(pasta ,x fagioli ,y)))) 223 | (reazon--should-equal '((pasta e fagioli _0) (pasta _0 fagioli e)) 224 | (reazon-run* q 225 | (reazon-fresh (x y) 226 | (reazon-== q `(pasta ,x fagioli ,y)) 227 | (reazon-membero 'e q)))) 228 | (reazon--should-equal '((tofu . _0) (_0 tofu . _1) (_0 _1 tofu . _2)) 229 | (reazon-run 3 s 230 | (reazon-membero 'tofu s)))) 231 | 232 | (ert-deftest reazon-test-relation-precedeso () 233 | "Tests for precedeso, adjacento, and immediately-precedeso." 234 | (let ((s '(1 2 3 4 5))) 235 | (reazon--should-equal '(_0) 236 | (reazon-run* q 237 | (reazon-immediately-precedeso 1 2 s) 238 | (reazon-immediately-precedeso 2 3 s) 239 | (reazon-immediately-precedeso 3 4 s) 240 | (reazon-immediately-precedeso 4 5 s) 241 | (reazon-precedeso 1 2 s) 242 | (reazon-precedeso 1 3 s) 243 | (reazon-precedeso 1 4 s) 244 | (reazon-precedeso 1 5 s) 245 | (reazon-precedeso 2 3 s) 246 | (reazon-precedeso 2 4 s) 247 | (reazon-precedeso 2 5 s) 248 | (reazon-adjacento 1 2 s) 249 | (reazon-adjacento 2 1 s) 250 | (reazon-adjacento 2 3 s) 251 | (reazon-adjacento 3 2 s) 252 | (reazon-adjacento 3 4 s) 253 | (reazon-adjacento 4 3 s) 254 | (reazon-adjacento 4 5 s) 255 | (reazon-adjacento 5 4 s))) 256 | (reazon--should-equal '() 257 | (reazon-run* q (reazon-immediately-precedeso 1 3 s)) 258 | (reazon-run* q (reazon-immediately-precedeso 1 4 s)) 259 | (reazon-run* q (reazon-immediately-precedeso 1 5 s)) 260 | (reazon-run* q (reazon-immediately-precedeso 3 2 s)) 261 | (reazon-run* q (reazon-precedeso 3 2 s)) 262 | (reazon-run* q (reazon-precedeso 4 2 s)) 263 | (reazon-run* q (reazon-precedeso 5 2 s)) 264 | (reazon-run* q (reazon-adjacento 1 4 s)) 265 | (reazon-run* q (reazon-adjacento 5 3 s))))) 266 | 267 | 268 | (provide 'reazon-test-relations) 269 | ;;; reazon-test-relations.el ends here 270 | -------------------------------------------------------------------------------- /test/reazon-test-sudoku.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-sudoku.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Tests for Sudoku solvers. There aren't as many of these as there 24 | ;; should be, since Sudoku queries can take a long time to finish. 25 | 26 | ;;; Code: 27 | 28 | (require 'reazon-test-utils) 29 | (require 'reazon-sudoku) 30 | 31 | (ert-deftest reazon-test-sudoku-solve-4x4 () 32 | (reazon--should-equal '((2 3 1 4 4 1 3 2 3 2 4 1 1 4 2 3)) 33 | (reazon-sudoku-solve-4x4 (a2 3) (b1 4) (b4 2) (c4 1) (d3 2))) 34 | (reazon--should-equal '() 35 | (reazon-sudoku-solve-4x4 (a1 1) (b2 1)) 36 | (reazon-sudoku-solve-4x4 (a3 2) (a4 2)) 37 | (reazon-sudoku-solve-4x4 (a1 3) (b1 3)) 38 | (reazon-sudoku-solve-4x4 (d3 1) (d4 1))) 39 | (reazon--should-equal 288 40 | (length 41 | (reazon-sudoku-solve-4x4)))) 42 | 43 | (ert-deftest reazon-test-sudoku-solve-9x9 () 44 | (reazon--should-equal '() 45 | ;; Reject invalid boards 46 | (reazon-sudoku-solve-9x9 47 | (a1 1) (a2 1) (a3 1) (a4 1) (a5 1))) 48 | (reazon--should-equal '((4 3 5 2 6 9 7 8 1 49 | 6 8 2 5 7 1 4 9 3 50 | 1 9 7 8 3 4 5 6 2 51 | 8 2 6 1 9 5 3 4 7 52 | 3 7 4 6 8 2 9 1 5 53 | 9 5 1 7 4 3 6 2 8 54 | 5 1 9 3 2 6 8 7 4 55 | 2 4 8 9 5 7 1 3 6 56 | 7 6 3 4 1 8 2 5 9)) 57 | ;; Verify a solved board 58 | (reazon-sudoku-solve-9x9 59 | (a1 4) (a2 3) (a3 5) (a4 2) (a5 6) (a6 9) (a7 7) (a8 8) (a9 1) 60 | (b1 6) (b2 8) (b3 2) (b4 5) (b5 7) (b6 1) (b7 4) (b8 9) (b9 3) 61 | (c1 1) (c2 9) (c3 7) (c4 8) (c5 3) (c6 4) (c7 5) (c8 6) (c9 2) 62 | (d1 8) (d2 2) (d3 6) (d4 1) (d5 9) (d6 5) (d7 3) (d8 4) (d9 7) 63 | (e1 3) (e2 7) (e3 4) (e4 6) (e5 8) (e6 2) (e7 9) (e8 1) (e9 5) 64 | (f1 9) (f2 5) (f3 1) (f4 7) (f5 4) (f6 3) (f7 6) (f8 2) (f9 8) 65 | (g1 5) (g2 1) (g3 9) (g4 3) (g5 2) (g6 6) (g7 8) (g8 7) (g9 4) 66 | (h1 2) (h2 4) (h3 8) (h4 9) (h5 5) (h6 7) (h7 1) (h8 3) (h9 6) 67 | (i1 7) (i2 6) (i3 3) (i4 4) (i5 1) (i6 8) (i7 2) (i8 5) (i9 9)) 68 | ;; Same as the previous one, but missing the last row and column 69 | (reazon-sudoku-solve-9x9 70 | (a1 4) (a2 3) (a3 5) (a4 2) (a5 6) (a6 9) (a7 7) (a8 8) 71 | (b1 6) (b2 8) (b3 2) (b4 5) (b5 7) (b6 1) (b7 4) (b8 9) 72 | (c1 1) (c2 9) (c3 7) (c4 8) (c5 3) (c6 4) (c7 5) (c8 6) 73 | (d1 8) (d2 2) (d3 6) (d4 1) (d5 9) (d6 5) (d7 3) (d8 4) 74 | (e1 3) (e2 7) (e3 4) (e4 6) (e5 8) (e6 2) (e7 9) (e8 1) 75 | (f1 9) (f2 5) (f3 1) (f4 7) (f5 4) (f6 3) (f7 6) (f8 2) 76 | (g1 5) (g2 1) (g3 9) (g4 3) (g5 2) (g6 6) (g7 8) (g8 7) 77 | (h1 2) (h2 4) (h3 8) (h4 9) (h5 5) (h6 7) (h7 1) (h8 3)) 78 | ;; Missing all diagonals 79 | (reazon-sudoku-solve-9x9 80 | (a2 3) (a3 5) (a4 2) (a5 6) (a6 9) (a7 7) (a8 8) 81 | (b1 6) (b3 2) (b4 5) (b5 7) (b6 1) (b7 4) (b9 3) 82 | (c1 1) (c2 9) (c4 8) (c5 3) (c6 4) (c8 6) (c9 2) 83 | (d1 8) (d2 2) (d3 6) (d5 9) (d7 3) (d8 4) (d9 7) 84 | (e1 3) (e2 7) (e3 4) (e4 6) (e6 2) (e7 9) (e8 1) (e9 5) 85 | (f1 9) (f2 5) (f3 1) (f5 4) (f7 6) (f8 2) (f9 8) 86 | (g1 5) (g2 1) (g4 3) (g5 2) (g6 6) (g8 7) (g9 4) 87 | (h1 2) (h3 8) (h4 9) (h5 5) (h6 7) (h7 1) (h9 6) 88 | (i2 6) (i3 3) (i4 4) (i5 1) (i6 8) (i7 2) (i8 5)) 89 | ;; Missing the middle of every box and the corners (and others) 90 | (reazon-sudoku-solve-9x9 91 | (a2 3) (a3 5) (a4 2) (a6 9) (a7 7) (a8 8) 92 | (b1 6) (b3 2) (b4 5) (b6 1) (b7 4) (b9 3) 93 | (c1 1) (c2 9) (c3 7) (c4 8) (c5 3) (c6 4) (c7 5) (c8 6) (c9 2) 94 | (d1 8) (d2 2) (d3 6) (d4 1) (d5 9) (d6 5) (d7 3) (d8 4) (d9 7) 95 | (e1 3) (e3 4) (e4 6) (e6 2) (e7 9) (e9 5) 96 | (f1 9) (f2 5) (f3 1) (f4 7) (f5 4) (f6 3) (f7 6) (f8 2) (f9 8) 97 | (g1 5) (g2 1) (g3 9) (g4 3) (g5 2) (g6 6) (g7 8) (g8 7) (g9 4) 98 | (h1 2) (h3 8) (h4 9) (h6 7) (h7 1) (h9 6) 99 | (i2 6) (i3 3) (i4 4) (i6 8) (i7 2) (i8 5)))) 100 | 101 | 102 | (provide 'reazon-test-sudoku) 103 | ;;; reazon-test-sudoku.el ends here 104 | -------------------------------------------------------------------------------- /test/reazon-test-trs.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-trs.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Tests for some of the relations from The Reasoned Schemer (TRS). 24 | ;; These aren't generally useful enough to be included as builtin 25 | ;; Reazon relations. 26 | 27 | ;;; Code: 28 | 29 | (require 'reazon-test-utils) 30 | 31 | (reazon-defrel reazon--test-lolo (s) 32 | (reazon-conde 33 | ((reazon-nullo s)) 34 | ((reazon-fresh (a) 35 | (reazon-caro s a) 36 | (reazon-listo a)) 37 | (reazon-fresh (d) 38 | (reazon-cdro s d) 39 | (reazon--test-lolo d))))) 40 | 41 | (ert-deftest reazon-test-trs-lolo () 42 | (reazon--should-equal '(nil (nil) ((_0)) (nil nil) ((_0 _1))) 43 | (reazon-run 5 q 44 | (reazon--test-lolo q)) 45 | (reazon-run 5 q 46 | (reazon--test-lolo `((a b) (c d) . ,q))))) 47 | 48 | (reazon-defrel reazon--test-twino (s) 49 | (reazon-fresh (x) 50 | (reazon-== s `(,x ,x)))) 51 | 52 | (ert-deftest reazon-test-trs-twino () 53 | (reazon--should-equal '(tofu) 54 | (reazon-run* q (reazon--test-twino `(,q tofu))))) 55 | 56 | (reazon-defrel reazon--test-loto (s) 57 | (reazon-conde 58 | ((reazon-nullo s)) 59 | ((reazon-fresh (a) 60 | (reazon-caro s a) 61 | (reazon--test-twino a)) 62 | (reazon-fresh (d) 63 | (reazon-cdro s d) 64 | (reazon--test-loto d))))) 65 | 66 | (ert-deftest reazon-test-trs-loto () 67 | (reazon--should-equal '(nil ((_0 _0)) ((_0 _0) (_1 _1))) 68 | (reazon-run 3 q 69 | (reazon--test-loto q)) 70 | (reazon-run 3 q 71 | (reazon--test-loto `((g g) . ,q))))) 72 | 73 | (reazon-defrel reazon--test-proper-membero (x s) 74 | (reazon-conde 75 | ((reazon-caro s x) 76 | (reazon-fresh (d) 77 | (reazon-cdro s d) 78 | (reazon-listo d))) 79 | ((reazon-fresh (d) 80 | (reazon-cdro s d) 81 | (reazon--test-proper-membero x d))))) 82 | 83 | (ert-deftest reazon-test-trs-proper-membero () 84 | ;; The ordering here is different from what's in the book. 85 | (reazon--should-equal '((tofu) (tofu _0) (_0 tofu) (tofu _0 _1) 86 | (tofu _0 _1 _2) (_0 tofu _1) 87 | (tofu _0 _1 _2 _3) 88 | (tofu _0 _1 _2 _3 _4) 89 | (_0 _1 tofu) (_0 tofu _1 _2) 90 | (tofu _0 _1 _2 _3 _4 _5) 91 | (tofu _0 _1 _2 _3 _4 _5 _6)) 92 | (reazon-run 12 s 93 | (reazon--test-proper-membero 'tofu s)))) 94 | 95 | (reazon-defrel reazon--test-memo (x s out) 96 | (reazon-conde 97 | ((reazon-caro s x) (reazon-== out s)) 98 | ((reazon-fresh (d) 99 | (reazon-cdro s d) 100 | (reazon--test-memo x d out))))) 101 | 102 | (ert-deftest reazon-test-trs-memo () 103 | (reazon--should-equal '((tofu d tofu e) (tofu e)) 104 | (reazon-run* out 105 | (reazon-fresh (x) 106 | (reazon--test-memo 'tofu `(a b ,x d tofu e) out)))) 107 | (reazon--should-equal '(((tofu d tofu e . _0) _0) 108 | ((tofu e . _0) _0) 109 | ((tofu . _0) (tofu . _0)) 110 | ((tofu . _0) (_1 tofu . _0)) 111 | ((tofu . _0) (_1 _2 tofu . _0)) 112 | ((tofu . _0) (_1 _2 _3 tofu . _0)) 113 | ((tofu . _0) (_1 _2 _3 _4 tofu . _0)) 114 | ((tofu . _0) (_1 _2 _3 _4 _5 tofu . _0)) 115 | ((tofu . _0) (_1 _2 _3 _4 _5 _6 tofu . _0))) 116 | (reazon-run 9 (x y) 117 | (reazon--test-memo 'tofu `(a b tofu d tofu e . ,y) x)))) 118 | 119 | (reazon-defrel reazon--test-rembero (x s out) 120 | (reazon-conde 121 | ((reazon-nullo s) (reazon-nullo out)) 122 | ((reazon-caro s x) (reazon-cdro s out)) 123 | ((reazon-fresh (a d rec) 124 | (reazon-conso a d s) 125 | (reazon-conso a rec out) 126 | (reazon--test-rembero x d rec))))) 127 | 128 | (ert-deftest reazon-test-trs-rembero () 129 | ;; These tests confirm some behavior that seems pathological. If all 130 | ;; the other tests pass and these ones don't, that might be good. 131 | (reazon--should-equal '((a b d peas e)) 132 | (reazon-run 1 out 133 | (reazon-fresh (y) 134 | (reazon--test-rembero 'peas `(a b ,y d peas e) out)))) 135 | (reazon--should-equal '((b a d _0 e) (a b d _0 e) (a b d _0 e) 136 | (a b d _0 e) (a b _0 d e) 137 | (a b e d _0) (a b _0 d _1 e)) 138 | (reazon-run* out 139 | (reazon-fresh (y z) 140 | (reazon--test-rembero y `(a b ,y d ,z e) out)))) 141 | (reazon--should-equal '(_0 _0 _0 _0 _0 142 | nil (_0 . _1) (_0) 143 | (_0 _1 . _2) (_0 _1) 144 | (_0 _1 _2 . _3)) 145 | (reazon-run 11 w 146 | (reazon-fresh (y z out) 147 | (reazon--test-rembero y `(a b ,y d ,z . ,w) out)))) 148 | (reazon--should-equal '((peas a peas c) (a peas peas c) (a peas peas c) 149 | (a peas c) (a peas c peas)) 150 | (reazon-run* q 151 | (reazon--test-rembero 'peas q `(a peas c)))) 152 | (reazon--should-equal '(b) 153 | (reazon-run* r 154 | (reazon--test-rembero r '(a b c) '(a b c)) 155 | (reazon-== r 'b)) 156 | (reazon-run* r 157 | (reazon-== r 'b) 158 | (reazon--test-rembero r '(a b c) '(a b c))))) 159 | 160 | 161 | (provide 'reazon-test-trs) 162 | ;;; reazon-test-trs.el ends here 163 | -------------------------------------------------------------------------------- /test/reazon-test-unit.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-unit.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Tests for Reazon's internal components. These tests do not use the 24 | ;; Reazon query interface, and can therefore be changed relatively 25 | ;; freely. They are mainly here as a guide for modifying the language 26 | ;; implementation. 27 | 28 | ;;; Code: 29 | 30 | (require 'reazon-test-utils) 31 | 32 | (ert-deftest reazon-test-unit-variable () 33 | (should (reazon--variable-p (reazon--make-variable 'x))) 34 | (should-not (reazon--variable-p 'x)) 35 | (reazon--with-variables (x y z) 36 | (should (reazon--variable-p x)) 37 | (should (reazon--variable-p y)) 38 | (should (reazon--variable-p z)))) 39 | 40 | (ert-deftest reazon-test-unit-walk () 41 | (reazon--with-variables (u w x y z) 42 | (let ((sub-1 `((,z . a) (,x . ,w) (,y . ,z))) 43 | (sub-2 `((,x . b) (,z . ,y) (,w . (,x e ,z)) (,u . ,w)))) 44 | (reazon--should-equal 'a 45 | (reazon--walk z sub-1) 46 | (reazon--walk y sub-1)) 47 | (reazon--should-equal w 48 | (reazon--walk x sub-1) 49 | (reazon--walk w sub-1)) 50 | (reazon--should-equal 'b (reazon--walk x sub-2)) 51 | (reazon--should-equal `(,x e ,z) (reazon--walk u sub-2)) 52 | (reazon--should-equal `(b e ,y) (reazon--walk* u sub-2))))) 53 | 54 | (ert-deftest reazon-test-unit-occurs () 55 | (reazon--with-variables (x y) 56 | (should (reazon--occurs-p x x '())) 57 | (should (reazon--occurs-p x `(,y) `((,y . ,x)))))) 58 | 59 | (ert-deftest reazon-test-unit-extend () 60 | (reazon--with-variables (x y z) 61 | (reazon--should-equal 'e 62 | (reazon--walk y (reazon--extend x 'e `((,z . ,x) (,y . ,z))))) 63 | (reazon--should-error 'reazon-circular-query 64 | (reazon--extend x x '()) 65 | (reazon--extend x `(,x) '()) 66 | (reazon--extend x `(,y) `((,y . ,x)))))) 67 | 68 | (ert-deftest reazon-test-unit-unify () 69 | (reazon--with-variables (x y z) 70 | (reazon--should-equal `((,y . 1) (,x . 2)) 71 | (reazon--unify `(,x + 1) `(2 + ,y) '())) 72 | (reazon--should-equal `((,x . ,y)) 73 | (reazon--unify x y '())) 74 | (reazon--should-equal `((,x . ,y)) 75 | (reazon--unify `(,x ,x) `(,y ,y) '()) 76 | (reazon--unify `(,x ,x ,x) `(,y ,y ,y) '()) 77 | (reazon--unify `(,x ,y) `(,y ,x) '())) 78 | (reazon--should-equal `((,y . a) (,x . ,y)) 79 | (reazon--unify `(,x ,y a) `(,y ,x ,x) '())) 80 | (reazon--should-error 'reazon-circular-query 81 | (reazon--unify x `(f ,x) '()) 82 | (reazon--unify `(,x ,y) `((f ,y) (f ,x)) '()) 83 | (reazon--unify `(,x ,y ,z) `((,y ,z) (,x ,z) (,x ,y)) '())) 84 | 85 | ;; PAIP ex 11.5 86 | 87 | ;; At least six books (Abelson and Sussman 1985, ...) present 88 | ;; unification algorithms with a common error. They all have 89 | ;; problems unifying `(?x ?y a)' with `(?y ?x ?x)'. Some of these 90 | ;; texts assume that `unify' will be called in a context where no 91 | ;; variables are shared between the two arguments. However, they 92 | ;; are still suspect to the bug, as the following example points 93 | ;; out: 94 | 95 | (reazon--should-equal `((,x . a) (,y . ,x) (,z ,x ,y a)) 96 | (reazon--unify `(f (,x ,y a) (,y ,x ,x)) `(f ,z ,z) '())) 97 | 98 | ;; Despite this subtle bug, I highly recommend each of the books 99 | ;; to the reader. It is interesting to compare different 100 | ;; implementations of the same algorithm. It turns out that there 101 | ;; are more similarities than differences. This indicates two 102 | ;; things: (1) there is a generally agreed-upon style for writing 103 | ;; these functions, and (2) good programmers sometimes take 104 | ;; advantage of opportunities to look at others' code. 105 | 106 | ;; The question is: Can you give an informal proof of the 107 | ;; correctness of the algorithm presented in this chapter? Start 108 | ;; by making a clear statement of the specification. Apply that to 109 | ;; the other algorithms, and show where they go wrong. Then see if 110 | ;; you can prove that the unify function in this chapter is 111 | ;; correct. Failing a complete proof, can you at least prove that 112 | ;; the algorithm will always terminate? 113 | )) 114 | 115 | (ert-deftest reazon-test-unit-primitives () 116 | (reazon--should-equal '(()) 117 | (reazon-!S '()) 118 | (funcall (reazon-== 4 4) '())) 119 | (reazon--should-equal '() 120 | (reazon-!U '()) 121 | (funcall (reazon-== 4 5) '()))) 122 | 123 | (ert-deftest reazon-test-unit-reify () 124 | (reazon--with-variables (u v w x y z) 125 | (let ((a1 `(,x . (,u ,w ,y ,z ((ice) ,z)))) 126 | (a2 `(,y . corn)) 127 | (a3 `(,w .(,v ,u)))) 128 | (reazon--should-equal `(_0 (_1 _0) corn _2 ((ice) _2)) 129 | (funcall (reazon--reify x) `(,a1 ,a2 ,a3)))))) 130 | 131 | (ert-deftest reazon-test-unit-stream () 132 | (let ((s1 '(a b c d)) 133 | (s2 `(e f . (lambda () '(g h)))) 134 | (s3 (lambda () '(i j k l)))) 135 | (reazon--should-equal '(a b c d i j k l) 136 | (reazon--pull (reazon--append s3 s1))) 137 | (reazon--should-equal '(e f g h) 138 | (reazon--take nil s2)))) 139 | 140 | (ert-deftest reazon-test-unit-goal () 141 | (reazon--with-variables (x) 142 | (let* ((g (reazon--disj-2 (reazon-== 'olive x) (reazon-== 'oil x))) 143 | (s (reazon--run-goal g)) 144 | (l (reazon--take 5 s)) 145 | (k (length l))) 146 | (reazon--should-equal k 2) 147 | (reazon--should-equal '(1 1) 148 | (mapcar #'length l)) 149 | (reazon--should-equal '(olive oil) 150 | (mapcar (reazon--reify x) s)) 151 | (reazon--should-equal l 152 | (reazon--take nil s))))) 153 | 154 | (ert-deftest reazon-test-unit-impure () 155 | (reazon--with-variables (x y) 156 | (reazon--should-equal `(((,y . 6))) 157 | (funcall (reazon--ifte #'reazon-!U 158 | (reazon-== 5 y) 159 | (reazon-== 6 y)) 160 | '())) 161 | (reazon--should-equal `(((,y . 5) (,x . 6))) 162 | (funcall (reazon--ifte (reazon-== 6 x) 163 | (reazon-== 5 y) 164 | (reazon-== 6 y)) 165 | '())) 166 | (reazon--should-equal `(((,y . 5) (,x . 6)) ((,y . 5) (,x . 5))) 167 | (funcall (reazon--ifte (reazon--disj-2 168 | (reazon-== x 6) 169 | (reazon-== x 5)) 170 | (reazon-== 5 y) 171 | (reazon-== 6 y)) 172 | '())) 173 | (reazon--should-equal `(((,y . 5) (,x . 6))) 174 | (funcall (reazon--ifte (reazon--once 175 | (reazon--disj-2 176 | (reazon-== x 6) 177 | (reazon-== x 5))) 178 | (reazon-== 5 y) 179 | (reazon-== 6 y)) 180 | '())))) 181 | 182 | (defun reazon--test-unproductive () 183 | "Produce nothing...forever." 184 | (lambda (s) 185 | (lambda () 186 | (funcall (reazon--test-unproductive) s)))) 187 | 188 | (defun reazon--test-productive () 189 | "Produce something...forever." 190 | (lambda (s) 191 | (lambda () 192 | (funcall (reazon--disj-2 #'reazon-!S (reazon--test-productive)) s)))) 193 | 194 | (ert-deftest reazon-test-unit-productivity () 195 | (reazon--with-variables (x) 196 | (let ((s (funcall (reazon--disj-2 197 | (reazon-== 'olive x) 198 | (reazon--test-unproductive)) 199 | nil))) 200 | (reazon--should-equal `((,x . olive)) 201 | (car s))) 202 | (let ((s (funcall (reazon--disj-2 203 | (reazon--test-unproductive) 204 | (reazon-== 'olive x)) 205 | nil))) 206 | (reazon--should-equal `((,x . olive)) 207 | (car (funcall s))) 208 | (reazon--should-equal `((,x . olive)) 209 | (car (reazon--pull s)))) 210 | (reazon--should-equal '(() () ()) 211 | (reazon--take 3 (reazon--run-goal (reazon--test-productive)))))) 212 | 213 | 214 | (provide 'reazon-test-unit) 215 | ;;; reazon-test-unit.el ends here 216 | -------------------------------------------------------------------------------- /test/reazon-test-utils.el: -------------------------------------------------------------------------------- 1 | ;;; reazon-test-utils.el --- -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018 Nick Drozd 4 | 5 | ;; Author: Nick Drozd 6 | ;; Keywords: 7 | 8 | ;; This program is free software; you can redistribute it and/or modify 9 | ;; it under the terms of the GNU General Public License as published by 10 | ;; the Free Software Foundation, either version 3 of the License, or 11 | ;; (at your option) any later version. 12 | 13 | ;; This program is distributed in the hope that it will be useful, 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | ;; GNU General Public License for more details. 17 | 18 | ;; You should have received a copy of the GNU General Public License 19 | ;; along with this program. If not, see . 20 | 21 | ;;; Commentary: 22 | 23 | ;; Utilities for Reazon tests. `reazon--with-variables' could 24 | ;; conceivably be moved to and used in the main Reazon file, while 25 | ;; something like `reazon--should-equal' would already be included in 26 | ;; ERT and I wouldn't have to implement it myself. 27 | 28 | ;;; Code: 29 | 30 | (require 'ert) 31 | (require 'reazon) 32 | 33 | (defmacro reazon--with-variables (variables &rest body) 34 | "Evaluate BODY with VARIABLES as reazon--variables." 35 | (declare (indent 1)) 36 | (let ((reazon--vars 37 | (mapcar (lambda (var) 38 | `(,var (reazon--make-variable ',var))) 39 | variables))) 40 | `(let (,@reazon--vars) 41 | ,@body))) 42 | 43 | (defun reazon--should-equal (expected &rest forms) 44 | "Assert that each form in FORMS equals EXPECTED." 45 | (declare (indent 1)) 46 | (dolist (form forms) 47 | (should (equal form expected)))) 48 | 49 | (defmacro reazon--should-error (type &rest forms) 50 | "Assert that each form in FORMS errors with TYPE." 51 | (declare (indent 1)) 52 | `(progn 53 | ,@(mapcar 54 | (lambda (form) 55 | `(should-error ,form :type ,type)) 56 | forms))) 57 | 58 | (provide 'reazon-test-utils) 59 | ;;; reazon-test-utils.el ends here 60 | --------------------------------------------------------------------------------