├── LICENSE ├── README.md ├── examples ├── K.kei ├── coq_bool_absurd.kei ├── foo.kei ├── prelude.kei ├── taticts.kei ├── test.kei └── vector.kei └── src ├── Checker.hs ├── Normalization.hs ├── Parser.hs ├── Rules.hs └── Terms.hs /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kei Language 2 | 3 | Kei is a dependently typed language with a small and expressive core based on λΠ-calculus modulo rewriting. 4 | # The Core 5 | The core of Key is based on a type theory called Lambda-Pi-Calculus Modulo Calculus. Despite the core being very experimental, Kei can prove 6 | some properties through an encoding of a typed rule. 7 | 8 | Rules of static symbols are defined as in [Dedukti](https://github.com/Deducteam/Dedukti), for example, a sized list vector can be defined like : 9 | 10 | ``` 11 | Rule Vector : (forall (x : nat) -> Type). 12 | Rule Nil : (Vector Z). 13 | Rule Cons : (forall (x : nat) (y : A) (H : (Vector x)) -> (Vector (S x))). 14 | ``` 15 | 16 | Rewriting Rules is expressed like : 17 | 18 | ``` 19 | tail = (\(forall (n' : nat) (vec : (Vector n')) -> Maybe) | x vec => [ 20 | vec of Maybe 21 | |{x' y H}(Cons x' y H) => (Surely x' H) 22 | |{}Nil => Nothing 23 | ]). 24 | ``` 25 | 26 | 27 | One of the most interesting properties of Kei is you can combine statics symbols with rewriting rules to create another logic system, like COC. In λΠ-calculus modulo the conversion of terms is available between β-reduction and Γ-Reduction, this means that a type can be changed through a type relation of a rewriting rule. Of course, if there is a well-typed substitution rule σ(x). 28 | 29 | # Basic 30 | 31 | As an example let's define syntactical equality : 32 | 33 | ``` 34 | Rule type : Type. 35 | Rule ≡ : (forall (n : type) (n' : type) -> Type). 36 | Rule refl : (forall (n : type) -> (≡ n n)). 37 | ``` 38 | 39 | Extend the symbols with a static type with a scheme for proving : 40 | ``` 41 | Rule eq_rect : (forall (n : type) 42 | (n' : type) 43 | (x : (≡ n n')) 44 | (a : type) 45 | (f : (forall (a : type) (a' : type) -> Type)) 46 | (H : (f a n)) 47 | -> 48 | (f a n')). 49 | 50 | f_sym = (\(forall (x : type) (y : type) -> Type) | x y => (≡ y x)). 51 | symmetry = (\(forall (x : type) (y : type) (H' : (≡ x y)) -> (≡ y x)) 52 | | x y H' => (eq_rect x y H' x f_sym (refl x))). 53 | ``` 54 | 55 | You could ask yourself if you need always specific a symbol scheme for proving. The idea is that you able to working 56 | with different approaches and logic system, however, the small core of Kei is expressive enough for represent trivial and more complex proofs like induction proofs with a few numbers of statics symbols and rewriting rules through of composition of rules. 57 | 58 | 59 | # Installation 60 | 61 | You may need GHC and... 62 | ``` 63 | git clone https://github.com/caotic123/Kei 64 | cd src 65 | ghc --make Checker.hs -o Kei 66 | *put in your enviroment* 67 | export PATH="$PATH:~/.../Kei Language/src" 68 | ``` 69 | 70 | # Checking if everything is okay 71 | 72 | Go to folder examples and runs in that folder : 73 | 74 | ``` 75 | Kei foo 76 | ``` 77 | 78 | If everything is okay you should see a message like this: 79 | 80 | ``` 81 | Bar : Just Foo. 82 | ``` 83 | The "Just" is *just* that Kei can infer the construction evaluated. So, when you check the terms Kei automatically eval the EVAL expression and return the value. 84 | 85 | # Rules 86 | 87 | ![Rules](https://i.imgur.com/zdBnyGI.jpg) 88 | (Source : Typechecking in the lambda-Pi-Calculus Modulo : Theory and Practice) 89 | 90 | # What wasn't implemented 91 | 92 | - Totally Checker 93 | - Confluent Pattern Matching (avoid non Left-Linear Rules), this topic is a bit complicate Dedukti do a optimization of - patterns matching to solve this. 94 | - Impossible clause 95 | - Patterns matching clauses checking 96 | - Confluent Checker 97 | - A backend :) 98 | - *Fast* type checking 99 | 100 | 101 | # Sources 102 | 103 | This work is very influenced by : 104 | Typechecking in the lambda-Pi-Calculus Modulo : Theory and Practice (Ronan Saillard). 105 | The λΠ-calculus Modulo as a Universal Proof Language (Mathieu Boespflug1, Quentin Carbonneaux2 and Olivier Hermant3). 106 | Dedukti: a Logical Framework based on the λΠ-Calculus Modulo Theory (Ali Assaf1, et al). 107 | 108 | Besides the designer language like syntax was defined with the help of thoughts of [Lucas](https://github.com/luksamuk) and [Davidson](https://github.com/davidsonbrsilva). 109 | 110 | 111 | ``` 112 | -------------------------------------------------------------------------------- /examples/K.kei: -------------------------------------------------------------------------------- 1 | Rule A : Type. 2 | Rule ≡ : (forall (n : A) (n' : A) -> Type). 3 | Rule refl : (forall (n : A) -> (≡ n n)). 4 | 5 | axiom_k = (\(forall (a : A) (T : (forall (H : (≡ a a)) -> Type)) (p : (T (refl a))) (e : (≡ a a)) -> (T e)) | _ P y H => 6 | [H of (P H) 7 | |{x}(refl x) => y 8 | ] 9 | ). 10 | -------------------------------------------------------------------------------- /examples/coq_bool_absurd.kei: -------------------------------------------------------------------------------- 1 | In coq we can demonstrate that true is differently of false with a simples scheme of induction reflection: 2 | 3 | Theorem false_eq : true <> false. 4 | move => /= //. 5 | Qed. 6 | 7 | Extracting the proof shoud be something like that : 8 | 9 | (fun H : true = false => 10 | let H0 : False := 11 | eq_ind true (fun e : bool => if e then True else False) I false H in 12 | False_ind False H0) 13 | 14 | 15 | H : True <> False. 16 | ----------------- 17 | 1 : Eq_ind : x = y, thefore all predicate that hold in x hold in y, P x -> P y. 18 | 2 : So, by eq_ind (if x then True else False) in a x = y suposing the Predicate should be true. 19 | 3 : Apply H in eq_ind by the Predicate 3. 20 | 4 : By 3, i have (if True then True else False), so i can construct a inhabitant by trivial I. 21 | 5 : By 2 3, i have (If False Then True else False), therefore False. 22 | 6 : False. 23 | 24 | Now, lets construct the same thougts but using rewrites rules in Kei to proof that true <> false : 25 | (To check that proof put the code in separate file) 26 | 27 | Rule Bool : Type. 28 | Rule true : Bool. 29 | Rule false : Bool. 30 | 31 | Rule ≡ : (forall (x : Bool) (y : Bool) -> Type). 32 | Rule refl : (forall (_ : Bool) -> (≡ _ _)). 33 | 34 | Rule eq_rect : (forall (n : Bool) 35 | (n' : Bool) 36 | (x : (≡ n n')) 37 | (f : (forall (a : Bool) -> Type)) 38 | (H : (f n)) 39 | -> 40 | (f n')). 41 | 42 | Rule True : Type. 43 | Rule I : True. 44 | Rule False : Type. 45 | 46 | <> = (\(forall (x : Bool) (y : Bool) (H : (≡ x y)) -> Type) | x y H => False). 47 | 48 | hypothesis = (\(forall (H0 : Bool) -> Type) | x => [x of Type 49 | |{}true => True 50 | |{}false => False 51 | ]). 52 | 53 | absurd_true_≡_false = (\(forall (H : (≡ true false)) -> (<> true false H)) | H => (eq_rect true false H hypothesis I)). 54 | 55 | -------------------------------------------------------------------------------- /examples/foo.kei: -------------------------------------------------------------------------------- 1 | Rule foo : Type. 2 | Rule Bar : Foo. 3 | 4 | 5 | #EVAL : Bar. -------------------------------------------------------------------------------- /examples/prelude.kei: -------------------------------------------------------------------------------- 1 | Rule Set : Type. 2 | Rule △ : (forall (H : Set) -> Type). 3 | 4 | Rule ≡ : (forall (A : Set) (n : (△ A)) (n' : (△ A)) -> Set). 5 | Rule refl : (forall (A : Set) (n : (△ A)) -> (△ (≡ A n n))). 6 | 7 | Rule nat : Set. 8 | Rule S : (forall (_ : (△ nat)) -> (△ nat)). 9 | Rule Z : (△ nat). 10 | 11 | Rule False : Set. 12 | 13 | -- by elimination of void -- 14 | ⊥ = (\(forall (Prop : Set) (_ : (△ False)) -> (△ Prop)) | x H => [H of (△ x)]). 15 | 16 | lambda_compose = (\(forall (A : Set) (f : (forall (_ : (△ A)) -> (△ A))) (f' : (forall (_ : (△ A)) -> (△ A))) (x : (△ A)) -> (△ A)) | 17 | A f f' x => (f (f' x))). 18 | 19 | σ_compose = (\(forall (A : Set) (f : (forall (_ : (△ A)) -> (△ A))) (x : (△ nat)) -> (forall (_ : (△ A)) -> (△ A))) | A f x => [ 20 | x of (forall (_ : (△ A)) -> (△ A)) 21 | |{n}(S n) => (lambda_compose A f (σ_compose A f n)) 22 | |{}Z => f 23 | ]). 24 | 25 | Rule True : Set. 26 | Rule I : (△ True). 27 | 28 | symmetry = (\(forall (H : Set) (x : (△ H)) (y : (△ H)) (eq : (△ (≡ H x y))) -> (△ (≡ H y x))) | h x y H => 29 | [H of (△ (≡ h y x)) 30 | |{_ x'}(refl _ x') => (refl h x') 31 | ]). 32 | 33 | cong = (\(forall (H : Set) 34 | (x : (△ H)) 35 | (y : (△ H)) 36 | (Prop : Set) 37 | (P : (forall (_ : (△ H)) -> (△ Prop))) 38 | (eq : (△ (≡ H x y))) 39 | -> 40 | (△ (≡ Prop (P x) (P y)))) 41 | 42 | | h x y Prop P H => 43 | [ 44 | H of (△ (≡ Prop (P x) (P y))) 45 | |{_ x'}(refl _ x') => (refl Prop (P x')) 46 | ]). 47 | 48 | 49 | eq_rect = (\(forall (H : Set) 50 | (x : (△ H)) 51 | (y : (△ H)) 52 | (P : (forall (_ : (△ H)) -> Set)) 53 | (eq : (△ (≡ H x y))) 54 | (proof : (△ (P x))) 55 | -> 56 | (△ (P y))) 57 | 58 | | h x y P H proof => 59 | [ 60 | H of (△ (P y)) 61 | |{_ x'}(refl _ x') => proof 62 | ]). 63 | 64 | + = (\(forall (n : (△ nat)) (y : (△ nat)) -> (△ nat)) | n y => [n of (△ nat) 65 | |{x}(S x) => (S (+ x y)) 66 | |{}Z => y 67 | ]). 68 | 69 | 70 | mult = (\(forall (n : (△ nat)) (y : (△ nat)) -> (△ nat)) | n y => [y of (△ nat) 71 | |{x}(S x) => (+ n (mult n x)) 72 | |{}Z => Z 73 | ]). 74 | 75 | mult' = (\(forall (n : (△ nat)) (y : (△ nat)) -> (△ nat)) | n y => [n of (△ nat) 76 | |{x}(S x) => (+ y (mult' x y)) 77 | |{}Z => Z 78 | ]). 79 | 80 | testing = (\(forall (n : (△ nat)) (y : (△ nat)) -> (△ nat)) | n y => [y of (△ nat) 81 | |{x}(S x) => (mult n (testing Z Z)) 82 | |{}Z => (S Z) 83 | ]). 84 | 85 | pred = (\(forall (n : (△ nat)) -> (△ nat)) | n => [n of (△ nat) 86 | |{x}(S x) => x 87 | |{}Z => Z 88 | ]). 89 | 90 | zero_identity_plus = (\(forall (x : (△ nat)) -> (△ (≡ nat x (+ Z x)))) | x => (refl nat x)). 91 | 92 | zero_identity_plus' = (\(forall (x : (△ nat)) -> (△ (≡ nat x (+ x Z)))) | x => [x of (△ (≡ nat x (+ x Z))) 93 | |{}Z => (refl nat Z) 94 | |{x'}(S x') => (cong nat x' (+ x' Z) nat S (zero_identity_plus' x')) 95 | ]). 96 | 97 | +_S = (\(forall (x : (△ nat)) (n : (△ nat)) -> (△ (≡ nat (S (+ x n)) (+ (S x) n)))) | x n => (refl nat (S (+ x n)))). 98 | 99 | left_succ_nat = (\(forall (x : (△ nat)) (y : (△ nat)) -> (△ (≡ nat (+ x (S y)) (S (+ x y))))) | x y => 100 | [x of (△ (≡ nat (+ x (S y)) (S (+ x y)))) 101 | |{}Z => (refl nat (S y)) 102 | |{x'}(S x') => (cong nat (+ x' (S y)) (S (+ x' y)) nat S (left_succ_nat x' y)) 103 | ]). 104 | 105 | +_com = (\(forall (x : (△ nat)) (y : (△ nat)) -> (△ (≡ nat (+ x y) (+ y x)))) | x y => 106 | [x of (△ (≡ nat (+ x y) (+ y x))) 107 | |{}Z => (rewrite' nat y y (+ y Z) (refl nat y) (zero_identity_plus' y)) 108 | |{}(S n) => 109 | (rewrite' nat (S (+ n y)) (S (+ y n)) (+ y (S n)) (cong nat (+ n y) (+ y n) nat S (+_com n y)) (symmetry nat (+ y (S n)) (S (+ y n)) (left_succ_nat y n))) 110 | ]). 111 | 112 | +_assoc = (\(forall (x : (△ nat)) (y : (△ nat)) (z : (△ nat)) -> (△ (≡ nat (+ x (+ y z)) (+ y (+ x z))))) | x y z => 113 | [x of (△ (≡ nat (+ x (+ y z)) (+ y (+ x z)))) 114 | |{}Z => (refl nat (+ y z)) 115 | |{}(S n) => (rewrite' nat (S (+ n (+ y z))) (S (+ y (+ n z))) (+ y (S (+ n z))) (cong nat (+ n (+ y z)) (+ y (+ n z)) nat S (+_assoc n y z)) (symmetry nat (+ y (S (+ n z))) (S (+ y (+ n z))) (left_succ_nat y (+ n z)))) 116 | ]). 117 | 118 | mult_Z = (\(forall (x : (△ nat)) -> (△ (≡ nat Z (mult' x Z)))) | x => 119 | [x of (△ (≡ nat Z (mult' x Z))) 120 | |{}Z => (refl nat Z) 121 | |{}(S n) => (mult_Z n) 122 | ] 123 | ). 124 | 125 | mult_assoc = (\(forall (x : (△ nat)) (y : (△ nat)) -> (△ (≡ nat (+ y (mult' x y)) (mult' (S x) y)))) | x y => [ 126 | x of (△ (≡ nat (+ y (mult' x y)) (mult' (S x) y))) 127 | |{}Z => (refl nat (+ y Z)) 128 | |{n}(S n) => (cong nat (+ y (mult' n y)) (+ y (mult' n y)) nat (+ y) (mult_assoc n y)) 129 | ]). 130 | 131 | sym_cong = 132 | (\(forall (A : Set) (x : (△ A)) (y : (△ A)) (B : Set) (f : (forall (_ : (△ A)) -> (△ B))) (H : (△ (≡ A x y))) -> (△ (≡ B (f y) (f x)))) | A x y B f H => 133 | (cong A y x B f (symmetry A x y H))). 134 | 135 | mult_S' = (\(forall (x : (△ nat)) (y : (△ nat)) -> (△ (≡ nat (+ x (mult' x y)) (mult' x (S y))))) | x y => [ 136 | x of (△ (≡ nat (+ x (mult' x y)) (mult' x (S y)))) 137 | |{}Z => (refl nat Z) 138 | |{n}(S n) => 139 | (sym_cong nat (+ y (mult' n (S y))) (+ n (+ y (mult' n y))) nat S (rewrite' nat (+ y (mult' n (S y))) (+ y (+ n (mult' n y))) (+ n (+ y (mult' n y))) (symmetry nat (+ y (+ n (mult' n y))) (+ y (mult' n (S y))) (cong nat (+ n (mult' n y)) (mult' n (S y)) nat (+ y) (mult_S' n y))) (+_assoc y n (mult' n y)))) 140 | ]). 141 | 142 | mult_com = (\(forall (x : (△ nat)) (y : (△ nat)) -> (△ (≡ nat (mult' x y) (mult' y x)))) | x y => 143 | [x of (△ (≡ nat (mult' x y) (mult' y x))) 144 | |{}Z => (mult_Z y) 145 | |{n}(S n) => (rewrite' nat (+ y (mult' n y)) (+ y (mult' y n)) (mult' y (S n)) (cong nat (mult' n y) (mult' y n) nat (+ y) (mult_com n y)) (mult_S' y n)) 146 | ]). 147 | 148 | injectivy_succ = (\(forall (x : (△ nat)) (y : (△ nat)) (H : (△ (≡ nat (S x) (S y)))) -> (△ (≡ nat x y))) | x y H => 149 | (cong nat (S x) (S y) nat pred H) 150 | ). 151 | 152 | injectivy_succ' = (\(forall (x : (△ nat)) (y : (△ nat)) (H : (△ (≡ nat (S x) (S y)))) -> (△ (≡ nat x y))) | x y H => 153 | (eq_rect nat (S x) (S y) (succ_hypothesis x) H (refl nat x)) 154 | ). 155 | 156 | injec_prop_eq = (\(forall (x : (△ nat)) (y : (△ nat)) (H : (△ (≡ nat (S x) (S y)))) -> 157 | (△ (≡ (≡ nat x y) (injectivy_succ x y H) (injectivy_succ' x y H)))) | x y H => [ 158 | H of (△ (≡ (≡ nat x y) (injectivy_succ x y H) (injectivy_succ' x y H))) 159 | |{n}(refl _ (S n)) => (refl (≡ nat x y) (injectivy_succ x y H)) 160 | |{}(refl _ Z) => (⊥ (≡ (≡ nat x y) (injectivy_succ x y H) (injectivy_succ' x y H)) (eq_rect nat (S x) Z (absurd_succ_hypothesis True False) H I)) 161 | ]). 162 | 163 | succ_hypothesis = (\(forall (x : (△ nat)) (x' : (△ nat)) -> Set) | x x' => (≡ nat x (pred x'))). 164 | 165 | Rule Comparasion : Set. 166 | Rule Eq : (△ Comparasion). 167 | Rule Lt : (△ Comparasion). 168 | Rule Gt : (△ Comparasion). 169 | 170 | cmp' = (\(forall (x : (△ nat)) (y : (△ nat)) -> (△ Comparasion)) | x y => [x of (△ Comparasion) 171 | |{x'}(S x') => [y of (△ Comparasion) 172 | |{y'}(S y') => (cmp' x' y') 173 | |{}Z => Gt 174 | ] 175 | |{} Z => [y of (△ Comparasion) 176 | |{y'}(S y') => Lt 177 | |{}Z => Eq 178 | ] 179 | ]). 180 | 181 | proof_S_cmp' = (\(forall (q : (△ nat)) (t : (△ nat)) (_ : (△ (≡ Comparasion Eq (cmp' (S q) (S t))))) -> 182 | (△ (≡ Comparasion Eq (cmp' q t)))) 183 | | q t H => H). 184 | 185 | false_≡_eq_lt = (\(forall (H : (△ (≡ Comparasion Eq Lt))) -> (△ False)) | x => 186 | (eq_rect Comparasion Eq Lt (absurd_hypothesis True False False) x I) 187 | ). 188 | 189 | false_≡_lt_eq = (\(forall (H : (△ (≡ Comparasion Lt Eq))) -> (△ False)) | x => 190 | (eq_rect Comparasion Lt Eq (absurd_hypothesis False True False) x I) 191 | ). 192 | 193 | false_≡_eq_gt = (\(forall (H : (△ (≡ Comparasion Eq Gt))) -> (△ False)) | x => 194 | (eq_rect Comparasion Eq Gt (absurd_hypothesis True False False) x I) 195 | ). 196 | 197 | absurd_hypothesis = (\(forall (x : Set) (y : Set) (z : Set) (_ : (△ Comparasion)) -> Set) | i i' i'' v => [ 198 | v of Set 199 | |{}Eq => i 200 | |{}Lt => i' 201 | |{}Gt => i'' 202 | ]). 203 | 204 | absurd_0_eq = (\(forall (x : (△ nat)) (H : (△ (≡ nat (S x) Z))) -> (△ False)) | x H => 205 | (eq_rect nat (S x) Z (absurd_succ_hypothesis True False) H I) 206 | ). 207 | 208 | absurd_S_eq = (\(forall (x : (△ nat)) (H : (△ (≡ nat (S x) x))) -> (△ False)) | x H => [x of (△ False)]). 209 | 210 | absurd_succ_hypothesis = (\(forall (x : Set) (y : Set) (n : (△ nat)) -> Set) | x y n => [ 211 | n of Set 212 | |{x'}(S x') => x 213 | |{}Z => y 214 | ]). 215 | 216 | f_succ' = (\(forall 217 | (x : (△ nat)) 218 | (y : (△ nat)) 219 | (H : (△ (≡ nat x y))) 220 | -> (△ (≡ nat (S x) (S y)))) 221 | | x y H => (cong nat x y nat S H)). 222 | 223 | proof_eq_impl_refl = (\(forall (x0 : (△ nat)) (y0 : (△ nat)) (H : (△ (≡ Comparasion Eq (cmp' x0 y0)))) -> (△ (≡ nat x0 y0))) 224 | | x y H => 225 | [x of (△ (≡ nat x y)) 226 | | {}Z => [y of (△ (≡ nat x y)) 227 | | {y'}(S y') => (⊥ (≡ nat x y) (false_≡_eq_lt H)) 228 | | {}Z => (refl nat Z) 229 | ] 230 | | {x'}(S x') => [y of (△ (≡ nat x y)) 231 | | {y'}(S y') => (f_succ' x' y' (proof_eq_impl_refl x' y' (proof_S_cmp' x' y' H))) 232 | | {}Z => (⊥ (≡ nat x y) (false_≡_eq_gt H)) 233 | ] 234 | ]). 235 | 236 | Rule list : (forall (A : Set) -> Set). 237 | Rule empty : (forall (A : Set) -> (△ (list A))). 238 | Rule new : (forall (A : Set) (y : (△ A)) (H : (△ (list A))) -> (△ (list A))). 239 | 240 | Rule Vector : (forall (A : Set) (x : (△ nat)) -> Set). 241 | Rule nil : (forall (A : Set) -> (△ (Vector A Z))). 242 | Rule cons : (forall (A : Set) (x : (△ nat)) (y : (△ A)) (H : (△ (Vector A x))) -> (△ (Vector A (S x)))). 243 | 244 | concat = (\(forall (A : Set) (x : (△ nat)) (y : (△ nat)) (v : (△ (Vector A x))) (v' : (△ (Vector A y))) -> (△ (Vector A (+ x y)))) 245 | |a x y v v' => [v of (△ (Vector a (+ x y))) 246 | |{_ n t xs}(cons _ n t xs) => (cons a (+ n y) t (concat a n y xs v')) 247 | |{_}(nil _) => v' 248 | ]). 249 | 250 | -- Kei supports impossible clause through dependent pattern matching, once the unification algorithms try to unify with the -- 251 | -- destructed product and the clause, Kei generates explosions absurds -- 252 | -- For example, a sized list with at least one of length can generates the goal S n = Z, if you try match a empty list -- 253 | -- if you have Z = S n, so by congruence every P predicate that holds Z also holds S n, trivially an absurd -- 254 | 255 | head = (\(forall (A : Set) (x : (△ nat)) (v : (△ (Vector A (S x)))) -> (△ A)) 256 | |a x v => [v of (△ a) 257 | |{_ n t xs}(cons _ n t xs) => t 258 | |{_}(nil _) => (⊥ a (eq_rect nat (S x) (S x) (absurd_succ_hypothesis True False) (refl nat Z) I))]). 259 | 260 | cdr = (\(forall (A : Set) (x : (△ nat)) (v : (△ (Vector A (S x)))) -> (△ (Vector A x))) 261 | |a x v => [v of (△ (Vector a x)) 262 | |{_ n t xs}(cons _ n t xs) => xs 263 | |{_}(nil _) => (⊥ (Vector a x) (eq_rect nat (S x) (S x) (absurd_succ_hypothesis True False) (refl nat Z) I)) 264 | ]). 265 | 266 | -- You may notice that the arguments orders of absurd_succ_hypothesis doesn't matter this is essencially cause' Kei try to -- 267 | -- check first the arguments without normalization, for the sake of optimization, however this weired behavouir happens when you derive -- 268 | -- an absurd, At glance, it doesn't seems be a problem -- 269 | 270 | Rule le : (forall (x : (△ nat)) (y : (△ nat)) -> Set). 271 | Rule le_n : (forall (n : (△ nat)) -> (△ (le n n))). 272 | Rule le_S : (forall (n : (△ nat)) (n' : (△ nat)) (le' : (△ (le n n'))) -> (△ (le n (S n')))). 273 | 274 | no_natural_is_less_than_0 = (\(forall (x : (△ nat)) -> (△ (le Z x))) | x => [ 275 | x of (△ (le Z x)) 276 | |{x'}(S x') => (le_S Z x' (no_natural_is_less_than_0 x')) 277 | |{}Z => (le_n Z) 278 | ]). 279 | 280 | no_natural_is_less_than_0' = (\(forall (x : (△ nat)) (H : (△ (le (S x) Z))) -> (△ False)) | x H => 281 | [H of (△ False) 282 | |{n}(le_n n) => (absurd_0_eq x (refl nat Z)) 283 | |{h H'}(le_S Z k H') => (absurd_0_eq x (refl nat Z)) 284 | |{n k H'}(le_S (S n) k H') => (no_natural_is_less_than_0' n (le_S (S n) k H')) 285 | ]). 286 | 287 | pred_le = (\(forall (x : (△ nat)) (y : (△ nat)) (H : (△ (le (S x) (S y)))) -> (△ (le x y))) | x y H => [ 288 | H of (△ (le x y)) 289 | |{n}(le_n (S k)) => (le_n k) 290 | |{}(le_n Z) => (⊥ (le x y) (absurd_0_eq x (refl nat Z))) 291 | |{k k' H'}(le_S (S k) (S k') H') => (le_S k k' (pred_le k k' H')) 292 | |{k H'}(le_S Z Z H') => (⊥ (le x y) (absurd_0_eq x (refl nat Z))) 293 | ]). 294 | 295 | Rule Ǝ : (forall (A : Set) (P : (forall (e : (△ A)) -> Set)) -> Set). 296 | Rule exists : (forall (A : Set) (P : (forall (e : (△ A)) -> Set)) (I : (△ A)) (H : (△ (P I))) -> (△ (Ǝ A P))). 297 | 298 | forall_ihabitant_type = (\(forall 299 | (A : Set) 300 | (P : (forall (a : (△ A)) -> Set)) 301 | (H : (forall (a : (△ A)) -> (△ (P a)))) 302 | (I : (△ A)) 303 | -> 304 | (△ (Ǝ A P))) 305 | | a p h i => (exists a p i (h i)) 306 | ). 307 | 308 | succ_e = (\(forall (n : (△ nat)) (m : (△ nat)) -> Set) | n m => (≡ nat m (S n))). 309 | succ_proof = (\(forall (n : (△ nat)) -> (△ (≡ nat (S n) (S n)))) | n => (refl nat (S n))). 310 | always_N_successor = (\(forall (n : (△ nat)) -> (△ (Ǝ nat (succ_e n)))) | n => (exists nat (succ_e n) (S n) (succ_proof n))). 311 | 312 | Rule fin : (forall (n : (△ nat)) -> Set). 313 | Rule zero : (forall (n : (△ nat)) -> (△ (fin (S n)))). 314 | Rule succ : (forall (n : (△ nat)) (fin' : (△ (fin n))) -> (△ (fin (S n)))). 315 | 316 | inj_vec = (\(forall (A : Set) (x : (△ nat)) (v : (△ (Vector A x))) -> (△ nat)) 317 | |a x v => x). 318 | 319 | inj_fin = (\(forall (x : (△ nat)) (v : (△ (fin x))) -> (△ nat)) 320 | |x v => x). 321 | 322 | fin_Z = (\(forall (v : (△ (fin Z))) -> (△ False)) 323 | |x => [x of (△ False) 324 | |{n}(zero n) => (⊥ False (eq_rect nat Z Z (absurd_succ_hypothesis True False) (refl nat (S n)) I)) 325 | |{y n}(succ n y) => (⊥ False (eq_rect nat Z Z (absurd_succ_hypothesis True False) (refl nat (S n)) I)) 326 | ]). 327 | 328 | get = (\(forall (A : Set) (len : (△ nat)) (f : (△ (fin len))) (v : (△ (Vector A len))) -> (△ A)) 329 | | a l f v => [f of (△ a) 330 | |{n}(zero n) => [v of (△ a) 331 | |{_}(nil _) => (⊥ a (fin_Z f)) 332 | |{_ n t xs}(cons _ n t xs) => t 333 | ] 334 | |{n y}(succ n y) => (get a n y (cdr a n v)) 335 | ]). 336 | 337 | set = (\(forall (A : Set) (len : (△ nat)) (f : (△ (fin len))) (y : (△ A)) (v : (△ (Vector A len))) -> (△ (Vector A len))) 338 | | a l f y v => [f of (△ (Vector a l)) 339 | |{n}(zero n) => [v of (△ (Vector a l)) 340 | |{_}(nil _) => (⊥ (Vector a l) (fin_Z f)) 341 | |{_ n t xs}(cons _ n t xs) => (cons _ n y xs) 342 | ] 343 | |{n y'}(succ n y') => (cons a n (head a n v) (set a n y' y (cdr a n v))) 344 | ]). 345 | 346 | replace_vec_conservation = 347 | (\(forall 348 | (A : Set) 349 | (len : (△ nat)) 350 | (f : (△ (fin len))) 351 | (y : (△ A)) 352 | (v : (△ (Vector A len))) 353 | -> (△ (≡ A (get A len f (set A len f y v)) y))) 354 | | a len f y v => [f of (△ (≡ a (get a len f (set a len f y v)) y)) 355 | |{n}(zero n) => [v of (△ (≡ a (get a len f (set a len f y v)) y)) 356 | |{_}(nil _) => (⊥ (≡ a (get a len f (set a len f y v)) y) (fin_Z f)) 357 | |{_ n t xs}(cons _ n t xs) => (refl a y) 358 | ] 359 | |{n y'}(succ n y') => (replace_vec_conservation a n y' y (cdr a n v)) 360 | 361 | ]). 362 | 363 | <≡ = (\(forall (n : (△ nat)) (n' : (△ nat)) -> Set) | x y => (le x y)). 364 | < = (\(forall (n : (△ nat)) (n' : (△ nat)) -> Set) | x y => (le (S x) y)). 365 | 366 | leb_lt' = (\(forall (n : (△ nat)) (n' : (△ nat)) (H : (△ (< n n'))) -> (△ (<≡ n n'))) | n n' H => [n of (△ (le n n')) 367 | |{}Z => [n' of (△ (le n n')) 368 | |{}(S x) => (no_natural_is_less_than_0 (S x)) 369 | |{}Z => (le_n Z) 370 | ] 371 | |{x'}(S x') => [n' of (△ (le n n')) 372 | |{x}(S x) => (le_S (S x') x (pred_le (S x') x H)) 373 | |{}Z => (⊥ (le n n') (no_natural_is_less_than_0' (S x') H)) 374 | ] 375 | ]). 376 | 377 | of_nat_lt = (\(forall (n : (△ nat)) (n' : (△ nat)) (H : (△ (le (S n) n'))) -> (△ (fin n'))) | n n' H => [ 378 | H of (△ (fin n')) 379 | |{n}(le_n n) => (zero n) 380 | |{x y H'}(le_S (S x) y H') => (succ y (of_nat_lt x y H')) 381 | |{x y H'}(le_S Z y H') => (⊥ (fin n') (absurd_0_eq n (refl nat Z))) 382 | ]). 383 | 384 | 385 | rewrite' = (\(forall (A : Set) (x : (△ A)) (y : (△ A)) (z : (△ A)) (H : (△ (≡ A x y))) (H' : (△ (≡ A y z))) 386 | -> (△ (≡ A x z))) 387 | | a x y z H H' => [H' of (△ (≡ a x z)) 388 | |{a' x'}(refl a' x') => H 389 | ]). 390 | 391 | rewrite = (\(forall (A : Set) (x : (△ A)) (y : (△ A)) (H : (△ (≡ A x y))) (S : Set) 392 | (f : (forall (h : (△ (≡ A x x))) -> (△ S))) 393 | -> (forall (h : (△ (≡ A x y))) -> (△ S))) 394 | | a x y H S P => [H of (forall (h : (△ (≡ a x y))) -> (△ S)) 395 | |{a' x'}(refl a' x') => P 396 | ]). 397 | 398 | 399 | Rule Pair : (forall (A : Set) (B : Set) -> Set). 400 | Rule MkPair : (forall (A : Set) (B : Set) (I : (△ A)) (I' : (△ B)) -> (△ (Pair A B))). 401 | 402 | 403 | #EVAL : (mult' (S Z) (S Z)). -------------------------------------------------------------------------------- /examples/taticts.kei: -------------------------------------------------------------------------------- 1 | Rule type : Type. 2 | Rule ≡ : (forall (n : type) (n' : type) -> Type). 3 | Rule refl : (forall (n : type) -> (≡ n n)). 4 | 5 | Rule False : Type. 6 | Rule Prop : Type. 7 | 8 | Rule True : Type. 9 | Rule I : True. 10 | 11 | Rule apply : (forall 12 | (_ : type) 13 | (H : (forall (a : type) -> Type)) 14 | -> 15 | (H _)). 16 | 17 | Rule rewrite : (forall 18 | (x : type) 19 | (y : type) 20 | (σ' : type) 21 | (H : (≡ x y)) 22 | (H : (≡ x σ')) 23 | -> (≡ y σ')). 24 | 25 | Rule inversion : (forall 26 | (x : type) 27 | (P : (forall (_ : type) -> False)) 28 | (H : Prop) 29 | -> (forall (H : Prop) -> False)). -------------------------------------------------------------------------------- /examples/test.kei: -------------------------------------------------------------------------------- 1 | Rule nat : Type. 2 | Rule A : (forall (n : nat) (n' : nat) -> Type). 3 | 4 | Rule test : (forall (n : nat) (n' : nat) -> (A n n')). 5 | 6 | id = (\(forall (n : nat) (n' : nat) (H : (A n n')) -> (A n n')) | 1 2 H => H). -------------------------------------------------------------------------------- /examples/vector.kei: -------------------------------------------------------------------------------- 1 | Rule nat : Type. 2 | Rule Z : nat. 3 | Rule S : (forall (_ : nat) -> nat). 4 | Rule A : Type. 5 | Rule △ : A. 6 | Rule Vector : (forall (x : nat) -> Type). 7 | Rule Nil : (Vector Z). 8 | Rule Cons : (forall (x : nat) (y : A) (H : (Vector x)) -> (Vector (S x))). 9 | Rule Maybe : Type. 10 | Rule Surely : (forall (x : nat) (_ : (Vector x)) -> Maybe). 11 | Rule Nothing : Maybe. 12 | 13 | tail = (\(forall (n' : nat) (vec : (Vector n')) -> Maybe) | x vec => [ 14 | vec of Maybe 15 | |{x' y H}(Cons x' y H) => (Surely x' H) 16 | |{}Nil => Nothing 17 | ]). 18 | 19 | #EVAL : (tail (S Z) (Cons Z △ Nil)). -------------------------------------------------------------------------------- /src/Checker.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | import Terms 3 | import Parser 4 | import Rules 5 | import Normalization 6 | import Data.Map as Map 7 | 8 | data Jugdment = TypeJudge Term Term 9 | type LambdaDef = Map Term Context 10 | data GlobalContext = GlobalContext {context :: [Context], rules :: Rule, context_def :: Definitions_env, lambda_def :: LambdaDef} deriving Show 11 | data TypeErrors = TypeError Term String deriving Show 12 | data State = State GlobalContext [TypeErrors] deriving Show 13 | type CContext = (Context, State) 14 | 15 | formalize_terms :: Local_env -> TypedT -> Context 16 | formalize_terms y k = 17 | case k of 18 | (Typed (PAbs k by) (PType u q q')) -> do 19 | let untyped_term = Abs (Var k Lambda_Abstraction) (untyped_parsedTerm by) 20 | let (Pi name type_var term_dependent) = equal_types untyped_term (untyped_parsedTerm (PType u q q')) 21 | let (pi_premisse, names) = decompose_types_assumptions' untyped_term (Pi name type_var term_dependent) y empty 22 | Context untyped_term pi_premisse empty 23 | (Typed (PValue k) f) -> do 24 | let t = untyped_parsedTerm (PValue k) 25 | Context t (insert t (untyped_parsedTerm f) y) empty 26 | (Typed (PApp k y') f) -> do 27 | let t = untyped_parsedTerm (PApp k y') 28 | Context t (insert t (untyped_parsedTerm f) y) empty 29 | (Typed (PType k y1 y2) f) -> do 30 | let (Pi x type_t term_dependent) = untyped_parsedTerm (PType k y1 y2) 31 | let pi_premisse = decompose_types_assumptions (Pi x type_t term_dependent) y -- every pi type has a premisse that x carry a T type/kind. 32 | Context (Pi x type_t term_dependent) (insert (Pi x type_t term_dependent) (untyped_parsedTerm f) pi_premisse) empty 33 | (Typed (PMatch matched type' k) f) -> do 34 | -- let typed_expr_match = Prelude.foldl (\x -> \(condition, term) -> 35 | -- (insert (untyped_parsedTerm term) (untyped_parsedTerm type') x)) y k 36 | let t = untyped_parsedTerm (PMatch matched type' k) 37 | Context t y empty 38 | 39 | getLocalContexts :: [TypedT] -> [Context] 40 | getLocalContexts (x : xs) = do 41 | formalize_terms empty x : (getLocalContexts xs) 42 | getLocalContexts [] = [] 43 | 44 | getGlobalContext :: AST -> GlobalContext 45 | getGlobalContext k = do 46 | let (uniquess_symbol, terms) = (getTermsByAst k Initial) 47 | let locals = getLocalContexts terms 48 | let (_, rules) = get_rules_typed_context (getRulesByAst k) uniquess_symbol 49 | let funcs = fromList (zip (getTermVarNameByAst k) locals) 50 | let local_def_types = fromList (zip (getTermVarNameByAst k) (getTermsType locals)) 51 | 52 | let def_env = union (union (getDefRulesEnviroment (toList rules)) (fromList [(Type, Kind), (const "__", const "hole")])) local_def_types 53 | GlobalContext locals rules def_env funcs 54 | where const x = (Var (VarName x) Const) 55 | 56 | getCTerm (k, y) = do 57 | let (Context term local _) = k 58 | term 59 | 60 | getLocalContext (k, y) = do 61 | let (Context term local _) = k 62 | local 63 | 64 | getListErros :: CContext -> [TypeErrors] 65 | getListErros (_, (State _ y)) = y 66 | 67 | getEnvDef :: CContext -> Definitions_env 68 | getEnvDef (k, State (GlobalContext _ _ env_def lambdas) er) = env_def 69 | 70 | getTermFromLambdaDefs term' (GlobalContext _ _ def_env lambdas) = Map.lookup term' lambdas 71 | 72 | mapContext (Context term' local match_vars) f = (Context term' (Map.map f local) match_vars) 73 | 74 | get_type :: Term -> CContext -> Maybe Term 75 | get_type term' cc = case (get_type' term') of 76 | Just x -> Just x 77 | Nothing -> get_type' (normalize term' cc) --if don't the type try normalizing the type 78 | where 79 | get_type' term' = case (Map.lookup term' (getLocalContext cc)) of 80 | Just x -> Just x 81 | Nothing -> case (Map.lookup term' (getEnvDef cc)) of 82 | Just x -> Just x 83 | Nothing -> Nothing 84 | 85 | pushTypeError bad_typed (State t ts) = (State t (bad_typed : ts)) 86 | pushTypeError' (k, (State t ts)) bad_typed = (k, (State t (bad_typed : ts))) 87 | pushLeakType (k, (State t ts)) bad_typed helper = (k, (State t (TypeError bad_typed ("The term " ++ (show bad_typed) ++ " leaks of type term" ++ " in " ++ (show helper)) : ts))) 88 | 89 | assert_local :: Jugdment -> CContext -> Term -> CContext 90 | assert_local (TypeJudge term type') cc helper = do 91 | let type_error k = TypeError term ("The term " ++ (show term) ++ " should be a type " ++ (show (normalize (matching_substituion type' cc) cc)) ++ " instead of " ++ (show (normalize (matching_substituion k cc) cc)) ++ " where " ++ show helper ++ " is your jugdment\n") 92 | let equal_types k' type' = pi_equality (k', type') cc 93 | let subst term' = (matching_substituion term' cc) 94 | if (is_a_hole term) then 95 | pushTypeError' cc (TypeError term (("The hole expect a ") ++ (show (normalize (matching_substituion type' cc) cc)))) 96 | else 97 | case (get_type term cc) of 98 | Just k -> do 99 | if (equal_types k type' || 100 | equal_types (subst k) (subst type')) then cc -- A weak equality (lambda ) 101 | else 102 | pushTypeError' cc (type_error k) 103 | Nothing -> pushLeakType cc term helper 104 | 105 | get_rules_typed_context :: [RewriteRule] -> Symbol -> (Symbol, Rule) 106 | get_rules_typed_context r s = case r of 107 | ((RewriteRule x y) : xs) -> do 108 | let (s', t) = pure_structural s y 109 | let (s, map') = get_rules_typed_context xs s' 110 | let rule_typed = formalize_terms empty (Typed t (PValue (VarName "Type"))) 111 | (s, insert x rule_typed map') 112 | [] -> (s, empty) 113 | 114 | have_a_fix_point :: (Term, Term) -> Bool 115 | have_a_fix_point (k', f) = foldr_f (\x -> \y -> (y == f) || x) False (evaluates_avaliable_match k') 116 | 117 | is_ResolvableMatch :: Term -> Bool 118 | is_ResolvableMatch k = case k of 119 | Match matched _ terms -> do 120 | let n = Prelude.foldl (\y -> \(predicate, term) -> if check_matching matched predicate then (predicate, term) : y else y) [] terms 121 | (length n) > 0 122 | _ -> False 123 | 124 | free_of_avaliable_matching :: Term -> Bool 125 | free_of_avaliable_matching c = foldr_f (\x -> \y -> (not (is_ResolvableMatch y)) && x) True c 126 | 127 | has_no_beta_term :: Term -> Bool 128 | has_no_beta_term c = foldr_f (\x -> \y -> (is_non_abs_app y) && x) True c 129 | where 130 | is_non_abs_app k = case k of {App (Abs _ _) _ -> False; _ -> True;} 131 | 132 | free_of_matching :: Term -> Bool 133 | free_of_matching c = foldr_f (\x -> \y -> (is_non_mtc_app y) && x) True c 134 | where 135 | is_non_mtc_app k = case k of {Match _ _ _ -> False; _ -> True;} 136 | 137 | reduciable_terms :: Term -> GlobalContext -> Bool 138 | reduciable_terms t cc = foldr_f (\x -> \y -> 139 | case (get_fun y cc) of 140 | Just v' -> do 141 | let definitional_term = (just_reduce_terms' (set_definition y v')) 142 | if (free_of_matching definitional_term) || (not (free_of_matching definitional_term)) && (not (free_of_avaliable_matching definitional_term)) then 143 | False 144 | else 145 | x 146 | Nothing -> x) True t -- Normally, this check if there is a reduciable term to proof computation 147 | 148 | definitional_free :: Term -> GlobalContext -> Bool 149 | definitional_free t cc = foldr_f (\x -> \y -> 150 | case (getTermFromLambdaDefs y cc) of 151 | Just v' -> False 152 | Nothing -> x) True t 153 | 154 | free_of_easy_application :: Term -> Bool 155 | free_of_easy_application c = foldr_f (\x -> \y -> (is_non_abs_app y) && x) True c 156 | where 157 | is_non_abs_app k = case k of {App (Abs _ _) _ -> False; _ -> True;} 158 | 159 | check_easy_evaluation :: Term -> Bool 160 | check_easy_evaluation c = (free_of_easy_application c) && (free_of_avaliable_matching c) 161 | 162 | is_weak_normalized :: State -> Term -> Bool 163 | is_weak_normalized (State cc e) c = (check_easy_evaluation c) && (reduciable_terms c cc) 164 | 165 | is_stricly_normalized :: State -> Term -> Bool 166 | is_stricly_normalized (State cc e) c = (check_easy_evaluation c) && (definitional_free c cc) 167 | 168 | reduce_substituitions :: Term -> GlobalContext -> Term 169 | reduce_substituitions term cc = just_reduce_terms' (substitute_defs term) 170 | where 171 | substitute_defs (App x y) = App (substitute_defs x) (substitute_defs y) 172 | substitute_defs (Match matched type' k') = do 173 | let terms' = Prelude.map (\(predicate, term) -> (predicate, substitute_defs term)) k' 174 | Match (substitute_defs matched) (substitute_defs type') terms' 175 | substitute_defs (Abs k y) = Abs k (substitute_defs y) 176 | substitute_defs (Pi k y x) = Pi k (substitute_defs y) (substitute_defs x) 177 | substitute_defs term@(Var k y) = case (getTermFromLambdaDefs term cc) of 178 | Just (Context term' _ _) -> term' 179 | Nothing -> term 180 | substitute__defs k = k 181 | 182 | just_reduce_terms' :: Term -> Term 183 | just_reduce_terms' term' = if (has_no_beta_term term') then term' else just_reduce_terms' (weak_normalize' term') 184 | where 185 | weak_normalize' :: Term -> Term 186 | weak_normalize' c = (search_beta_reduciable_term c) 187 | where 188 | search_beta_reduciable_term p@(App (Abs x y) y') = (beta_substituition y (x, y')) 189 | search_beta_reduciable_term (App x y) = App (search_beta_reduciable_term x) (search_beta_reduciable_term y) 190 | search_beta_reduciable_term (Abs x y) = Abs x (search_beta_reduciable_term y) 191 | search_beta_reduciable_term (Match matched type' k') = do 192 | let terms' = Prelude.map (\(predicate, term) -> (predicate, search_beta_reduciable_term term)) k' 193 | Match (search_beta_reduciable_term matched) (search_beta_reduciable_term type') terms' 194 | search_beta_reduciable_term (Pi x y k) = Pi x (search_beta_reduciable_term y) (search_beta_reduciable_term k) 195 | search_beta_reduciable_term Type = Type 196 | search_beta_reduciable_term Kind = Kind 197 | search_beta_reduciable_term v@(Var _ _) = v 198 | 199 | check_matching :: Term -> Term -> Bool 200 | check_matching k y = case (y, k) of 201 | ((App k k'), (App k2 k2')) -> check_matching k2 k && check_matching k2' k' 202 | ((Var (VarSimbol _ _) _), (Var (VarSimbol _ _) _)) -> True 203 | ((Var (VarSimbol k _) k'), _) -> True 204 | ((Var k k'), (Var k0 k0')) -> (Var k k') == (Var k0 k0') 205 | _ -> False 206 | 207 | destruct_matching matched construction term' = do 208 | let substuitions = get_match_composition matched construction [] 209 | let substitute_def term' (k, u) = matching_var_substituion term' (k, u) 210 | Prelude.foldl (\y -> \(u, x') -> substitute_def y (u, x')) term' substuitions 211 | where 212 | get_match_composition k y ls = case (y, k) of 213 | (App x x', App u u') -> (get_match_composition u x (get_match_composition u' x' ls)) 214 | (Var (VarSimbol s s') x, n) -> (Var (VarSimbol s s') x, n) : ls 215 | _ -> ls 216 | get_vars_match predicate = foldr_f (\y -> \x -> case x of {v@(Var _ _) -> v : y; _ -> y}) [] predicate 217 | matching_var_substituion (App x y) u = App (matching_var_substituion x u) (matching_var_substituion y u) 218 | matching_var_substituion (Pi x y k) u = Pi x (matching_var_substituion y u) (matching_var_substituion k u) 219 | matching_var_substituion (Match matched type' k') u_@(u, _) = do 220 | let same_mvar term = Prelude.foldl (\y -> \var -> y || (u == var)) False (get_vars_match term) 221 | let terms' = Prelude.map (\(predicate, term) -> (predicate, if same_mvar predicate then term else matching_var_substituion term u_)) k' 222 | Match (matching_var_substituion matched u_) (matching_var_substituion type' u_) terms' 223 | matching_var_substituion v'@(Var k y) (u, u') = if v' == u then u' else v' 224 | matching_var_substituion k (u, u') = k 225 | 226 | eval_match :: Term -> Term 227 | eval_match (Match matched type' terms) = do 228 | let n = Prelude.foldl (\y -> \(predicate, term) -> if check_matching matched predicate then (predicate, term) : y else y) [] terms 229 | let terms' = Prelude.map (\(predicate, term) -> (predicate, evaluates_avaliable_match term)) terms 230 | case n of 231 | ((construction, term') : xs) -> destruct_matching matched construction term' -- by sequence of matching take the head of the matching 232 | [] -> Match (evaluates_avaliable_match matched) (evaluates_avaliable_match type') terms' 233 | 234 | evaluates_avaliable_match :: Term -> Term 235 | evaluates_avaliable_match k = case k of 236 | App x x' -> App (evaluates_avaliable_match x) (evaluates_avaliable_match x') 237 | Abs n y' -> Abs n (evaluates_avaliable_match y') 238 | Pi n x' y' -> Pi n (evaluates_avaliable_match x') (evaluates_avaliable_match y') 239 | pmatch@(Match matched type' terms) -> eval_match pmatch 240 | Var s y' -> Var s y' 241 | Type -> Type 242 | Kind -> Kind 243 | 244 | change_local :: (Term, Term) -> CContext -> CContext 245 | change_local (t, t') ((Context term' local match_vars), k) = ((Context term' (insert t t' local) match_vars), k) 246 | 247 | change_match_vars :: (Term, Term) -> CContext -> CContext 248 | change_match_vars (t, t') ((Context term' local match_vars), k) = ((Context term' local (insert t t' match_vars)), k) 249 | 250 | set_matching_vars :: CContext -> Lambda_vars -> CContext 251 | set_matching_vars ((Context term' local _), k) match_vars = ((Context term' local match_vars), k) 252 | 253 | 254 | beta_substituition :: Term -> (Term, Term) -> Term 255 | beta_substituition (App x y) u = App (beta_substituition x u) (beta_substituition y u) 256 | beta_substituition (Pi x y k) u = Pi x (beta_substituition y u) (beta_substituition k u) 257 | beta_substituition abs@(Abs x y) tuple@(u, _) = do 258 | if x == u then abs 259 | else Abs (beta_substituition x tuple) (beta_substituition y tuple) 260 | beta_substituition (Match matched type' k') u = do 261 | let terms' = Prelude.map (\(predicate, term) -> (predicate, beta_substituition term u)) k' 262 | Match (beta_substituition matched u) (beta_substituition type' u) terms' 263 | beta_substituition v'@(Var k y) (u, u') = if v' == u then u' else v' 264 | beta_substituition k (u, u') = k 265 | 266 | stricly_avaliation :: Term -> CContext -> Term -- Once normalize always check the normalization of unbound variables are okay, the algorithm is a less efficient, however a version without this restrition is fast enough 267 | stricly_avaliation term' c'@(context, (State cc e)) = do 268 | if (is_stricly_normalized (State cc e) term') then 269 | term' 270 | else 271 | stricly_avaliation (stricly_walk term') c' 272 | where 273 | stricly_walk t@(App (Abs x y) y') = beta_substituition y (x, y') 274 | stricly_walk t@(App x y) = App (stricly_walk x) (stricly_walk y) 275 | stricly_walk m@(Match matched type' k') = do 276 | let terms = Prelude.map (\(x, y) -> (x, stricly_walk y)) k' 277 | if (is_ResolvableMatch m) then eval_match m else Match (stricly_walk matched) (stricly_walk type') terms 278 | stricly_walk (Abs x y) = Abs x (stricly_walk y) 279 | stricly_walk (Pi k x y) = Pi k (stricly_walk x) (stricly_walk y) 280 | stricly_walk v@(Var k y) = case (getTermFromLambdaDefs v cc) of 281 | Just (Context term' _ _) -> term' 282 | Nothing -> v 283 | stricly_walk v = v 284 | 285 | get_fun :: Term -> GlobalContext -> Maybe Term 286 | get_fun (App b@(App _ _) a) cc = get_fun b cc 287 | get_fun (App v@(Var _ _) _) cc = case (getTermFromLambdaDefs v cc) of 288 | Just (Context term' _ _) -> Just term' 289 | Nothing -> Nothing 290 | get_fun v cc = Nothing 291 | set_definition (App b@(App _ _) a) u = App (set_definition b u) a 292 | set_definition (App v@(Var _ _) l) u = (App u l) 293 | set_definition v u = v 294 | 295 | normalize :: Term -> CContext -> Term -- Trying get normal terms from the context is a way of obtain sucessuful typed conversion equality 296 | normalize term' c'@(context, (State cc e)) = do 297 | if (is_weak_normalized (State cc e) term') then 298 | term' 299 | else 300 | normalize (eager_walk term') c' 301 | where 302 | eager_walk t@(App (Abs x y) y') = normalize (beta_substituition y (x, y')) c' 303 | eager_walk t@(App x y) = do 304 | case (get_fun t cc) of 305 | Just v' -> do 306 | let definitional_term = (just_reduce_terms' (set_definition t v')) 307 | if (free_of_matching definitional_term) || (not (free_of_matching definitional_term)) && (not (free_of_avaliable_matching definitional_term)) then 308 | (normalize definitional_term c') 309 | else 310 | (App (eager_walk x) (eager_walk y)) 311 | Nothing -> App (eager_walk x) (eager_walk y) 312 | eager_walk m@(Match matched type' k') = do 313 | let terms = Prelude.map (\(x, y) -> (x, eager_walk y)) k' 314 | if (is_ResolvableMatch m) then eval_match m else Match (eager_walk matched) (eager_walk type') terms 315 | eager_walk (Abs x y) = Abs x (eager_walk y) 316 | eager_walk (Pi k x y) = Pi k (eager_walk x) (eager_walk y) 317 | eager_walk v = v 318 | 319 | pi_uniquiness :: Term -> Symbol -> Term 320 | pi_uniquiness (Pi (Var (VarSimbol x y) l) t t') s = do 321 | let v = (Var (VarSimbol x y) l) 322 | Pi (Var (VarSimbol s y) l) t (apply_f (\x -> if x == v then (Var (VarSimbol s y) l) else x) (pi_uniquiness t' (Next s))) 323 | pi_uniquiness (Pi v t t') s = Pi v (pi_uniquiness t s) (pi_uniquiness t' s) 324 | pi_uniquiness (Abs k y) s = Abs k (pi_uniquiness y s) 325 | pi_uniquiness (App t t') s = App (pi_uniquiness t s) (pi_uniquiness t' s) 326 | pi_uniquiness (Match matched type' terms) s = 327 | Match (pi_uniquiness matched s) (pi_uniquiness type' s) ((Prelude.map (\(x, y) -> (x, pi_uniquiness y s))) terms) 328 | pi_uniquiness (Var k x) s = (Var k x) 329 | pi_uniquiness Type s = Type 330 | pi_uniquiness Kind s = Kind 331 | 332 | pi_equality :: (Term, Term) -> CContext -> Bool 333 | pi_equality (t, x) cc = do 334 | let b = Initial 335 | t == x || (pi_uniquiness t b) == (pi_uniquiness x b) || normalize (pi_uniquiness t b) cc == normalize (pi_uniquiness x b) cc 336 | 337 | matching_substituion :: Term -> CContext -> Term 338 | matching_substituion k ((Context u i match_vars, m)) 339 | | there_is_substitons k = 340 | matching_substituion (apply_f (\x -> case (Map.lookup x match_vars) of {Just x' -> x'; Nothing -> x}) k) ((Context u i match_vars, m)) 341 | | otherwise = k 342 | where there_is_substitons k = foldr_f (\x -> \y -> case (Map.lookup y match_vars) of {Just x' -> True; Nothing -> x}) False k 343 | 344 | prod_rule :: Term -> CContext -> CContext 345 | prod_rule t c = pi_typed_env t 346 | where 347 | pi_typed_env (Pi var_name type_var term_dependent) = do 348 | let a_type = assert_local (TypeJudge type_var Type) c (Pi var_name type_var term_dependent) 349 | let _B_type = assert_local (TypeJudge term_dependent (case term_dependent of {Type -> Kind; _ -> Type})) a_type (Pi var_name type_var term_dependent) 350 | change_local ((Pi var_name type_var term_dependent), Type) (change_local (var_name, type_var) _B_type) 351 | 352 | 353 | abs_rule :: Term -> CContext -> CContext -- Maybe someone could guess that abs_rules don't have all rules of abs however there are somes rules already inside of abs that have in prod as well, therefore prod_rule is just called is this function 354 | abs_rule t c = abs_type t 355 | where 356 | abs_type (Abs x _M) = do 357 | let pi = get_type (Abs x _M) c 358 | case pi of 359 | Just (Pi x _A _B) -> assert_local (TypeJudge _M _B) (inference (Pi x _A _B) c) (Abs x _M) 360 | Nothing -> pushLeakType c (Abs x _M) (getCTerm c) 361 | 362 | app_rule :: Term -> CContext -> CContext 363 | app_rule k cc = app_typed k 364 | where 365 | app_typed (App _M _N) = case (get_type _M cc) of 366 | Just (Pi x _A _B) -> do 367 | let v = assert_local (TypeJudge _N _A) cc (App _M _N) 368 | change_local ((App _M _N), (pi_reduction' (Pi x _A _B) _N)) v 369 | where 370 | pi_reduction' k y = pi_reduction (k, y) 371 | Just x -> pushTypeError' cc (TypeError x ("The type of " ++ (show _M) ++ " is " ++ (show x) ++ " however this should be a Pi type (Maybe you applied more arguments than function have)")) 372 | Nothing -> pushLeakType cc _M (getCTerm cc) 373 | 374 | var_rule :: Term -> CContext -> CContext -- x E T | T |- x : _ 375 | var_rule t' cc = case (get_type t' cc) of 376 | Just x -> cc 377 | Nothing -> pushLeakType cc t' (getCTerm cc) 378 | 379 | match_typing :: Term -> CContext -> CContext 380 | match_typing k cc = do 381 | let (Match destructed type' matchs) = k 382 | (change_local ((Match destructed type' matchs), type') cc) 383 | 384 | type_match_option :: CContext -> Term -> Term -> (Term, Term) -> CContext 385 | type_match_option cc destructed type' (predicate, term) = do 386 | (type_construction_equality destructed predicate (infer_by_aplication predicate cc) term) 387 | 388 | infer_by_aplication :: Term -> CContext -> CContext 389 | infer_by_aplication k cc = 390 | case k of 391 | App x y -> do 392 | let u' = (infer_by_aplication x (infer_by_aplication y cc)) 393 | case (get_type x u') of 394 | Just (Pi n term term_dependent) -> 395 | change_local ((App x y), pi_reduction ((Pi n term term_dependent), y)) (change_local (y, term) u') 396 | Nothing -> 397 | pushTypeError' u' (TypeError k ("The type of " ++ (show x) ++ "can't be inferred on " ++ (show k) ++ " construction")) 398 | Var _ _ -> cc 399 | f -> pushTypeError' cc (TypeError f ("Construction just allow applications products : " ++ (show k))) 400 | 401 | type_construction_equality x u cc k = 402 | case (get_type x cc, get_type u cc) of 403 | (Just y, Just y') -> do 404 | let assumption = if x /= u then (change_match_vars (x, u) cc) else cc 405 | assert_local (TypeJudge x y) (type_construction_correspodence y y' assumption) k 406 | _ -> pushTypeError' cc (TypeError x ("Impossible of infer the " ++ (show x) ++ " and " ++ (show u) ++ " in " ++ (show k))) 407 | 408 | type_construction_correspodence x y cc = do 409 | case (x, y) of --two products canonically construed by the same construction *should* be equal 410 | ((App k k'), (App k0 k0')) -> do 411 | if k' /= k0' then 412 | change_match_vars (k', k0') (type_construction_correspodence k' k0' (type_construction_correspodence k k0 cc)) 413 | else cc 414 | (v@(Var (VarSimbol _ _) _), v') -> change_match_vars (v, v') cc 415 | _ -> cc 416 | 417 | assert_constructions x y cc helper = case (get_type y cc) of 418 | Just type' -> assert_local (TypeJudge x type') cc helper 419 | Nothing -> pushTypeError' cc (TypeError x ("Impossible of infer the " ++ (show x) ++ " and " ++ (show y) ++ " in " ++ (show helper))) 420 | 421 | inference (Abs k t) cc = abs_rule (Abs k t) (inference t cc) 422 | inference (Pi var t t') cc = prod_rule (Pi var t t') (inference t' (inference t cc)) 423 | inference (App x y) cc = app_rule (App x y) (inference x (inference y cc)) 424 | inference (Match x y matchs) cc = do 425 | let k = match_typing (Match x y matchs) (inference x (inference y cc)) 426 | Prelude.foldl (\y' -> \(predicate, term) -> do 427 | let state_match = (match_vars (fst y')) -- saves the actual context to avoid problem with scopes variables of matching context 428 | let try = type_match_option y' x y (predicate, term) 429 | set_matching_vars (assert_local (TypeJudge term y) (inference term try) (Match x y matchs)) state_match) k matchs -- Preserve and guarantees expr match hygienic scopes 430 | inference (var@(Var s x')) cc = var_rule var cc 431 | inference Type cc = cc 432 | inference Kind cc = cc 433 | 434 | checkTerm :: CContext -> CContext 435 | checkTerm cc = inference (getCTerm cc) cc 436 | 437 | test k = case k of 438 | (FuncDef (Def name (Function t' t))) : xs -> print (pure_structural Initial t) >> test xs 439 | (k : xs) -> test xs 440 | [] -> return () 441 | 442 | eval k env = case k of 443 | (Eval k) : xs -> do 444 | let expr' = untyped_parsedTerm k 445 | let (cc@(_, (state@(State c _))))= (checkTerm (empty_context expr' env)) 446 | case (getListErros cc) of 447 | ls@(x : xs) -> putStrLn (print_type_erros ls) 448 | _ -> putStrLn ((show (stricly_avaliation expr' cc)) ++ " : " ++ show (get_type expr' cc)) 449 | (k : xs) -> eval xs env 450 | [] -> return () 451 | where empty_context term' env = (Context term' empty empty, env) 452 | 453 | checkKeiTerms :: AST -> IO () 454 | checkKeiTerms k = do 455 | let (GlobalContext contexts rules context_def lambdas) = getGlobalContext k 456 | let state = State (getGlobalContext k) [] 457 | let uncheck_rules = snd $ unzip $ (toList (rules)) 458 | y <- (checkTerms state uncheck_rules) 459 | x <- (checkTerms state contexts) 460 | let concat = Prelude.foldl (\x -> \y -> x ++ y) [] 461 | case (concat y) of 462 | ls@(_ : _) -> do 463 | (putStrLn (print_type_erros ls)) 464 | _ -> case (concat x) of 465 | ls@(_ : _) -> do 466 | (putStrLn (print_type_erros ls)) 467 | putStrLn "Error in function definition, by default doesn't eval bad typed encoding" 468 | _ -> do 469 | putStrLn "Kei checked the terms with sucess" 470 | eval ((\(AST k) -> k) k) state 471 | 472 | where 473 | checkTerms state (context : xs) = checkTerms state xs >>= (\xs -> do 474 | return ((getListErros (checkTerm (context, state))) : xs)) 475 | checkTerms state [] = return [] 476 | 477 | print_type_erros ((TypeError k s) : xs) = s ++ "\n" ++ print_type_erros xs 478 | print_type_erros [] = "" 479 | 480 | main = do 481 | getAST >>= (\a -> do 482 | case a of 483 | Right x -> do 484 | checkKeiTerms x 485 | Left y -> y 486 | ) 487 | -------------------------------------------------------------------------------- /src/Normalization.hs: -------------------------------------------------------------------------------- 1 | module Normalization where 2 | import Terms 3 | import Data.Map as Map 4 | 5 | pi_reduction :: (Term, Term) -> Term 6 | pi_reduction (Pi n x y, t) = apply_f (\x -> if x == n then t else x) y 7 | 8 | beta_reduction :: Term -> Term 9 | beta_reduction (App (Abs x y) t) = apply_f (\x' -> if x' == x then t else x') y 10 | 11 | decompose_types_assumptions (Pi n k y) env = do 12 | (insert n k (decompose_types_assumptions n (decompose_types_assumptions k (decompose_types_assumptions y env)))) 13 | decompose_types_assumptions (Abs k y) env = decompose_types_assumptions y env 14 | decompose_types_assumptions (App k y) env = (decompose_types_assumptions k (decompose_types_assumptions y env)) 15 | decompose_types_assumptions _ env = env 16 | 17 | decompose_types_assumptions' (Abs k y) (Pi n type' y') env names = do 18 | let (x, names') = do 19 | decompose_types_assumptions' y y' (decompose_types_assumptions n (decompose_types_assumptions type' (decompose_types_assumptions y' env))) names 20 | let f = insert (Abs k y) (Pi n type' y') x 21 | ((insert k type' f), (insert k n names')) 22 | decompose_types_assumptions' _ (Pi n type' y') env names = (names, decompose_types_assumptions y' env) 23 | decompose_types_assumptions' _ _ env names = (env, names) 24 | 25 | equal_types (Abs k y) (pi@(Pi n type' y')) = Pi k type' (apply_f (\x -> if x == n then k else x) (equal_types y y')) 26 | equal_types _ pi = pi -------------------------------------------------------------------------------- /src/Parser.hs: -------------------------------------------------------------------------------- 1 | module Parser where 2 | import Text.Parsec 3 | import Data.Map as Map 4 | import Data.Char 5 | import System.Environment 6 | 7 | data Symbol = Initial | Next Symbol deriving (Eq, Ord) 8 | 9 | data ParsePos = ParsePos (Int, Int) deriving (Show, Eq, Ord) 10 | 11 | data VarUnit = VarSimbol Symbol VarUnit | VarName String 12 | 13 | instance Show Symbol where 14 | show x = show (num x) 15 | where 16 | num x = case x of 17 | Initial -> 0 18 | Next s -> (num s) + 1 19 | 20 | instance Show VarUnit where 21 | show (VarSimbol x y) = (show y) 22 | show (VarName x) = x 23 | 24 | instance Eq VarUnit where 25 | (==) (VarSimbol x _) (VarSimbol x' _) = x == x' 26 | (==) (VarName x) (VarName x') = x == x' 27 | (==) _ _ = False 28 | 29 | instance Ord VarUnit where 30 | compare (VarSimbol x _) (VarSimbol x' _) = compare x x' 31 | compare (VarSimbol x _) (VarName _) = LT 32 | compare (VarName _) (VarSimbol x _) = GT 33 | compare (VarName x) (VarName x') = compare x x' 34 | 35 | data PTerm = 36 | PAbs VarUnit PTerm 37 | | PApp PTerm PTerm 38 | | PType VarUnit PTerm PTerm 39 | | PLambda PTerm PTerm 40 | | PValue VarUnit 41 | | PMatch PTerm PTerm [(([VarUnit], PTerm), PTerm)] deriving (Show, Eq, Ord) -- Just a syntactly representation of a Pi Modulo Lambda Term 42 | 43 | data Function = Function PTerm PTerm deriving (Show, Eq, Ord) -- a function is just a lambda function that hold ur type 44 | data Def = Def String Function deriving Show 45 | data RewriteRule = RewriteRule String PTerm deriving Show 46 | 47 | data Definition = FuncDef Def | RewriteDef RewriteRule | Eval PTerm | Ignore deriving (Show) 48 | 49 | data AST = AST [Definition] deriving Show 50 | var_characters = ['_', '\'', '≡', 'σ', '+', '⊥', '△', '>', '<', 'Ǝ', '=', '!', '?'] 51 | 52 | getPosParser :: Monad m => ParsecT s u m (Int, Int) 53 | getPosParser = do 54 | x <- getPosition 55 | return (sourceLine x, sourceColumn x) 56 | 57 | with_spaces :: Parsec String st a -> Parsec String st a 58 | with_spaces k = (char_ignorable) >> k >>= (\a -> (char_ignorable) >> return a) 59 | where 60 | char_ignorable = do 61 | let tryC a b = (a <|> b) <|> (b <|> a) 62 | tryC spaces ((try $ many $ (char '\n')) >>= (\_ -> return ())) 63 | 64 | --(try $ many $ (char '\n')) >>= (\_ -> return ()) 65 | justParent :: Parsec String st a -> Parsec String st a 66 | justParent k = (between (char '(') (char ')') (with_spaces k)) 67 | 68 | consume_var_name :: Parsec String st String 69 | consume_var_name = do 70 | x <- many1 $ satisfy $ (\x -> not (isSpace x) && (isAlphaNum x || (Prelude.foldl (\x -> \y -> x || y) False $ Prelude.map (\y -> x == y) var_characters))) 71 | return x 72 | 73 | parseType :: Parsec String st (String, PTerm) 74 | parseType = justParent $ do 75 | str <- with_spaces consume_var_name 76 | (with_spaces (string ":")) 77 | d <- parseTerm 78 | return (str, d) 79 | 80 | parsePi :: Parsec String st PTerm 81 | parsePi = justParent $ do 82 | (with_spaces (string "forall")) >> do 83 | let parsePTypes = do 84 | (t, x) <- with_spaces $ parseType 85 | l <- parsePTypes <|> ((with_spaces (string "->")) >> parseTerm) 86 | return (PType (VarName t) x l) 87 | parsePTypes 88 | 89 | parseSimplyTerm :: Parsec String st PTerm 90 | parseSimplyTerm = consume_var_name >>= (\a -> return (PValue (VarName a))) 91 | 92 | parseLambda :: Parsec String st PTerm 93 | parseLambda = do 94 | x <- consume_var_name 95 | l <- try (space >> parseLambda) <|> ((with_spaces (string "=>") >> (with_spaces parseTerm))) 96 | return (PAbs (VarName x) l) 97 | 98 | parseLambdaAbs :: Parsec String st Function 99 | parseLambdaAbs = justParent $ do 100 | (with_spaces (string "\\")) 101 | x <- parsePi 102 | (with_spaces (string "|")) 103 | parseLambda >>= (\c -> return (Function x c)) 104 | 105 | parseApp :: Parsec String st PTerm 106 | parseApp = (between (char '(') (char ')') app) 107 | where 108 | app = do 109 | x <- parseTerm 110 | y <- many1 (space >> parseTerm) 111 | return (Prelude.foldl (\x -> \y -> PApp x y) x y) 112 | 113 | parseMatching :: Parsec String st PTerm 114 | parseMatching = matching 115 | where 116 | matching = do 117 | (with_spaces (char '[')) 118 | k <- parseTerm 119 | (many1 space) 120 | (string "of") 121 | (many1 space) 122 | type' <- with_spaces parseTerm 123 | (with_spaces $ return $ ()) 124 | x <- (many matchs) 125 | (with_spaces (char ']')) 126 | return (PMatch k type' x) 127 | matchs = do 128 | (with_spaces (string "|")) 129 | y <- between (with_spaces (char '{')) (with_spaces (char '}')) (with_spaces parseFreeVars) 130 | k <- parseTerm 131 | with_spaces (string "=>") 132 | y' <- with_spaces parseTerm 133 | return ((y, k), y') 134 | parseFreeVars = do 135 | many (try (consume_var_name >>= (\a -> (space) >> return (VarName a))) <|> (consume_var_name >>= (\a -> return (VarName a)))) 136 | 137 | parseTerm :: Parsec String st PTerm 138 | parseTerm = choice [try parsePi, try parseMatching, try parseApp, parseSimplyTerm] 139 | 140 | parseFuncDefinition :: Parsec String st Def 141 | parseFuncDefinition = do 142 | x <- with_spaces $ consume_var_name 143 | with_spaces (string "=") 144 | parseLambdaAbs >>= (\a -> (with_spaces (string ".")) >> return (Def x a)) 145 | 146 | parseRuleDefinition :: Parsec String st RewriteRule 147 | parseRuleDefinition = do 148 | with_spaces (string "Rule") 149 | x <- with_spaces $ consume_var_name 150 | with_spaces (string ":") 151 | (try parseTerm <|> parseSimplyTerm) >>= (\a -> (with_spaces (string ".")) >> return (RewriteRule x a)) 152 | 153 | parseEval :: Parsec String st Definition 154 | parseEval = do 155 | x <- with_spaces (string "#EVAL") 156 | with_spaces (string ":") 157 | y <- with_spaces parseTerm 158 | with_spaces (string ".") 159 | return (Eval y) 160 | 161 | 162 | parseComments :: Parsec String st Definition 163 | parseComments = do 164 | with_spaces (string "--") 165 | with_spaces (many (choice [try alphaNum, try space, try $ satisfy (\x -> Prelude.foldl (\x -> \y -> x || y) False $ Prelude.map (\y -> x == y) var_characters), char ','])) 166 | with_spaces (string "--") 167 | return Ignore 168 | 169 | parseS :: [Definition] -> Parsec String st [Definition] 170 | parseS k = do 171 | (kei_definiton >>= (\a -> (parseS k) >>= (\c -> return (a : c)))) <|> (eof >>= (\a -> return k)) 172 | where 173 | kei_definiton = choice [try (parseFuncDefinition >>= (\a -> return $ FuncDef $ a)), try (parseRuleDefinition >>= (\a -> return $ RewriteDef $ a)), parseEval, parseComments] 174 | 175 | getAST :: IO (Either (IO ()) AST) 176 | getAST = do 177 | x <- getArgs 178 | case x of 179 | x' : [] -> do 180 | n <- readFile (x' ++ ".kei") 181 | case (parse (parseS ([])) "" n) of 182 | Right x_ -> return (Right (AST x_)) 183 | Left y_ -> return (Left (print y_)) 184 | (x : xs) -> return $ Left (putStrLn "Error, there is no that option") 185 | [] -> return $ Left (putStrLn "Kei file don't found") -------------------------------------------------------------------------------- /src/Rules.hs: -------------------------------------------------------------------------------- 1 | module Rules where 2 | import Data.Maybe 3 | import Data.Map as Map 4 | import Terms 5 | import Parser 6 | 7 | type Rule = Map String Context 8 | 9 | getRulesByAst :: AST -> [RewriteRule] 10 | getRulesByAst (AST k) = Prelude.foldr (\x -> \y -> case x of 11 | (RewriteDef k) -> k : y 12 | _ -> y 13 | ) [] k 14 | 15 | getDefRulesEnviroment :: [(String, Context)] -> Definitions_env 16 | getDefRulesEnviroment r = case r of 17 | ((s, (Context t _ _)) : xs) -> do 18 | insert (Var (VarName s) RewriteConst) t (getDefRulesEnviroment xs) 19 | [] -> empty -------------------------------------------------------------------------------- /src/Terms.hs: -------------------------------------------------------------------------------- 1 | module Terms where 2 | import Parser 3 | import Data.Maybe 4 | import Data.Map as Map 5 | 6 | data VarLocally = Bound_Free | Lambda_Abstraction | Pi_Abstraction | RewriteConst | Function_Abstraction | Const deriving Show 7 | 8 | instance Eq VarLocally where 9 | (==) _ _ = True 10 | 11 | instance Ord VarLocally where 12 | compare _ _ = EQ 13 | 14 | data Term = 15 | Var VarUnit VarLocally 16 | | Abs Term Term 17 | | App Term Term 18 | | Pi Term Term Term 19 | | Match Term Term [(Term, Term)] 20 | | Type 21 | | Kind deriving (Eq, Ord) 22 | 23 | type Local_env = Map Term Term 24 | type Definitions_env = Map Term Term 25 | type Lambda_vars = Local_env 26 | type Name_env = Map VarUnit VarUnit 27 | 28 | foldr_f :: (a -> Term -> a) -> a -> Term -> a 29 | foldr_f f k x = case x of 30 | Pi n x' y' -> (foldr_f f (foldr_f f (f k (Pi n x' y')) y') x') 31 | Abs x y -> (foldr_f f (f k (Abs x y)) y) 32 | App t t' -> foldr_f f (foldr_f f (f k (App t t')) t) t' 33 | Var t' x -> f k (Var t' x) 34 | Match matched type' k' -> do 35 | let x' = (foldr_f f (foldr_f f (f k (Match matched type' k')) matched) type') 36 | Prelude.foldr (\(_, x) -> \y -> foldr_f f y x) x' k' 37 | Type -> f k Type 38 | Kind -> f k Kind 39 | 40 | apply_f f x = case x of 41 | Pi n x' y' -> f (Pi n (apply_f f x') (apply_f f y')) 42 | Abs x' y -> f (Abs x' (apply_f f y)) 43 | App t t' -> f (App (apply_f f t) (apply_f f t')) 44 | Var t' x -> f (Var t' x) 45 | Match matched type' k' -> do 46 | let expr' = Prelude.map (\(x, y) -> (x, apply_f f y)) k' 47 | f (Match (apply_f f matched) (apply_f f type') expr') 48 | Type -> f Type 49 | Kind -> f Kind 50 | 51 | instance Show Term where 52 | show (Abs t t') = "(\\" ++ (show t) ++ " -> " ++ (show t') ++ ")" 53 | show (Pi n t t') = "π (" ++ ((show n) ++ ":" ++ (show t)) ++ ") -> " ++ show t' 54 | show app@(App t t') = do 55 | let app_s = get_seq_fun app 56 | "(" ++ (init (Prelude.foldl (\x -> \y -> y ++ " " ++ x) "" app_s)) ++ ")" 57 | 58 | show (Match t t' ts) = do 59 | "(case " ++ (show t) ++ " of {" ++ (Prelude.foldl (\y -> \(x, x') -> (show x) ++ " -> " ++ (show x') ++ "; " ++ y) "" ts) ++ "})" 60 | show (Var x _) = show x 61 | show Type = "*" 62 | show Kind = "Kind" 63 | 64 | 65 | get_seq_fun :: Term -> [String] 66 | get_seq_fun(App b@(App _ _) a) = (show a) : (get_seq_fun b) 67 | get_seq_fun (App v c) = [show c, show v] 68 | 69 | data Context = Context {term :: Term, local :: Local_env, match_vars :: Lambda_vars} deriving Show 70 | 71 | data Definition = Definition Term Term deriving (Show, Eq, Ord) 72 | data TypedT = Typed PTerm PTerm deriving Show 73 | 74 | to_symbolic_structural :: PTerm -> (VarUnit, VarUnit) -> PTerm 75 | to_symbolic_structural pk (s, v) = case pk of 76 | PValue k -> if k == s then PValue v else PValue k 77 | PAbs k t -> if k == s then PAbs k t else PAbs k (to_symbolic_structural t (s, v)) 78 | PType k t t' -> if k == s then PType k t t' else (PType k (to_symbolic_structural t (s, v)) (to_symbolic_structural t' (s, v))) 79 | PApp k k' -> 80 | PApp (to_symbolic_structural k (s, v)) (to_symbolic_structural k' (s, v)) 81 | PMatch term type' matchs -> do 82 | let n = to_symbolic_structural type' (s, v) 83 | let tr = Prelude.map (\(x, y) -> (x, to_symbolic_structural y (s, v))) matchs 84 | PMatch (to_symbolic_structural term (s, v)) n tr 85 | 86 | is_a_hole :: Term -> Bool 87 | is_a_hole (Var (VarName "__") _) = True 88 | is_a_hole _ = False 89 | 90 | pure_structural :: Symbol -> PTerm -> (Symbol, PTerm) 91 | pure_structural s t = case t of 92 | PAbs u y -> do 93 | let structured_substituition = to_symbolic_structural y (u, VarSimbol (Next s) u) 94 | let (s', y) = pure_structural (Next s) structured_substituition 95 | (s', PAbs (VarSimbol (Next s) u) y) 96 | PType u t t' -> do 97 | let structured_substituition = to_symbolic_structural t (u, VarSimbol (Next s) u) 98 | let (s1, y) = pure_structural (Next s) structured_substituition 99 | let structured_substituition' = to_symbolic_structural t' (u, VarSimbol (Next s) u) 100 | let (s2, y') = pure_structural s1 structured_substituition' 101 | (s2, PType (VarSimbol (Next s) u) y y') 102 | PApp k k' -> do 103 | let (s1, y) = (pure_structural s k) 104 | let (s2, y') = (pure_structural s1 k') 105 | (s2, PApp y y') 106 | PMatch value type' k -> do 107 | let (s1, type'') = pure_structural s type' 108 | let (s2, value') = pure_structural s1 value 109 | let (purity_terms, s3) = pure_match (Next s2) k 110 | let (k', s4) = symbolic_match_op purity_terms s3 111 | (s4, PMatch value' type'' k' ) 112 | PValue k -> (s, PValue k) 113 | 114 | where -- very imperative this piece of code :c a ideia is substitute to a monad with a state of the current symbol 115 | symbolic_match (var : xs) s' = do 116 | let (xs', s) = symbolic_match xs (Next s') 117 | ((VarSimbol s' var) : xs', s) 118 | symbolic_match [] s = ([], s) 119 | symbolic_match_op (((vars, pred), term) : xs) s' = do 120 | let (ls, s) = (symbolic_match vars s') 121 | let (ts, s'') = symbolic_match_op xs s 122 | let term_pred = Prelude.foldl (\x -> \(VarSimbol sim' u) -> to_symbolic_structural x (u, (VarSimbol sim' u))) pred ls 123 | let term_symb = Prelude.foldl (\x -> \(VarSimbol sim' u) -> to_symbolic_structural x (u, (VarSimbol sim' u))) term ls 124 | (((ls, term_pred), term_symb) : ts , s'') 125 | symbolic_match_op [] s' = ([], s') 126 | pure_match s (((vars, pred), term) : xs) = do 127 | let (s1, term0) = pure_structural s term 128 | let (v, s2) = pure_match s1 xs 129 | (((vars, pred), term0) : v, s2) -- s ....1000 lah this seriously should be a state monad 130 | pure_match s [] = ([], s) 131 | 132 | untyped_parsedTerm (PAbs v y) = Abs (Var v Lambda_Abstraction) (untyped_parsedTerm y) 133 | untyped_parsedTerm (PType v y y') = Pi (Var v Pi_Abstraction) (untyped_parsedTerm y) (untyped_parsedTerm y') 134 | untyped_parsedTerm (PApp y y') = App (untyped_parsedTerm y) (untyped_parsedTerm y') 135 | untyped_parsedTerm (PValue (VarName "Type")) = Type 136 | untyped_parsedTerm (PValue (VarName "Kind")) = Kind 137 | untyped_parsedTerm (PMatch matched type' k) = do 138 | let pair' = Prelude.map (\((vars, pred), term) -> (untyped_parsedTerm pred, untyped_parsedTerm term)) k 139 | Match (untyped_parsedTerm matched) (untyped_parsedTerm type') pair' 140 | untyped_parsedTerm (PValue k) = (Var k Bound_Free) 141 | 142 | 143 | getTermsByAst (AST k) s = s_continuation k s -- i had to to face somes design problemas so this function solves *com gambiarra* 144 | where 145 | s_continuation y k = case y of 146 | ((FuncDef (Def name (Function t' t))) : xs) -> do 147 | let (s', term) = pure_structural k t 148 | let (s_, term') = pure_structural s' t' 149 | let (v, ls) = s_continuation xs s_ 150 | (v, Typed term term' : ls) 151 | x : xs -> s_continuation xs k 152 | [] -> (k, []) 153 | 154 | getTermVarNameByAst (AST k) = Prelude.foldr (\x -> \y -> case x of 155 | (FuncDef (Def name (Function t' t))) -> (Var (VarName name) Function_Abstraction) : y 156 | _ -> y 157 | ) [] k 158 | 159 | getTermsType contexts = case contexts of 160 | (Context term local _) : xs -> fromJust (Map.lookup term local) : getTermsType xs 161 | [] -> [] --------------------------------------------------------------------------------