# FormalSystems 0.1.3

Play with formal systems from "Gödel Escher Bach", Douglas Hofstadter

This is a Python implementation of Douglas Hofstadter formal systems, from his book Gödel, Escher, Bach: An Eternal Golden Braid (commonly GEB).

In fact, you may define your own formal systems using a quite simple syntax, close to free text. Examples for MIU, pg, fg and NDP formal systems from the book are implemented in directory definitions.

A main Python script gives you possibilities to play with the formal system, including:

• axiom schema support (iteration, decision procedure)
• theorem step by step generation (using different algorithms)
• theorem derivation

## Formal system definition

### Examples

The MIU system may be define with:

```axioms:
- MI

rules:
- x is .*, xI => xIU
- x is .*, Mx => Mxx
- x is .*, y is .*, xIIIy => xUy
- x y  .* , xUUy => xy
```

The underlying syntax is YAML (see raw format). You can define one or several axioms, or even an infinite number of axioms using a schema, as in the pg formal system:

```axioms:
- x is -+, xp-gx-

rules:
- x y z are -+, xpygz => xpy-gz-
```

### Syntax

Axiom definitions should be formatted like this ([] means this is optional):

```[def_1, [def_2, ...]] expr
```

Where:

• def_i is an optional definition of wildcard, using a regular expression, for example:
• “.*” may be anything including the empty string
• “-+” is a string composed of “-“

The definitions are written using char [is] regexp or char1 char2 [are] regexp if different wildcards have the same definition. Note that you should use only one character for wildcard definition.

• expr is the axiom expression

Rules for theorem production should be formatted like this:

```[def_1, [def_2, ...]] cond_1 [and cond_2 [and ...]] => th_1 [and th_2 [and ...]]
```

Where:

• def_i is the same as before
• cond_i is a required theorem, in order to produce new theorems (separated by and if several conditions)
• th_i is a produced theorems with the rule

## Installation

Install with:

```\$ python setup.py install --user
```

A script should be put in ~/.local/bin, make sure this path is in your \$PATH:

```\$ export PATH=\$PATH:~/.local/bin
```

## Tests

If installation is successful, run the tests with:

```\$ cd tests
\$ python test_formalsystems.py -v
```

## Main script

After installation, you should have the main script FormalSystemsMain.py deployed somewhere where you \$PATH points to, under the name FormalSystems. If it is not the case, you can always execute the script directly, assuming the dependencies are properly installed (just pyyaml and LEPL).

Usage of the main script is fully documented in --help argument.

You may generate theorems step by step if the number of axioms is finite:

```\$ FormalSystems definitions/MIU.yaml --iteration 3
> Finite number of axioms, using step algorithm

STEP 1: MI

P  (1) x is .*, xI => xIU                    for  MI                         gives  MIU
P  (2) x is .*, Mx => Mxx                    for  MI                         gives  MII
.  (3) x is .*, y is .*, xIIIy => xUy        for  MI
.  (4) x y  .* , xUUy => xy                  for  MI

STEP 2: MIU/MII

P  (1) x is .*, xI => xIU                    for  MII                        gives  MIIU
.  (1) x is .*, xI => xIU                    for  MIU
P  (2) x is .*, Mx => Mxx                    for  MII                        gives  MIIII
P  (2) x is .*, Mx => Mxx                    for  MIU                        gives  MIUIU
.  (3) x is .*, y is .*, xIIIy => xUy        for  MII
.  (3) x is .*, y is .*, xIIIy => xUy        for  MIU
.  (4) x y  .* , xUUy => xy                  for  MII
.  (4) x y  .* , xUUy => xy                  for  MIU

STEP 3: MIIU/MIIII/MIUIU
```

Or using a bucket where axioms are thrown and theorems computed iteratively if the number of axioms is infinite:

```\$ FormalSystems definitions/pg.yaml --iteration 4
> Infinite number of axioms, using bucket algorithm

[Adding -p-g-- to bucket]

=== BUCKET 1: -p-g--

P  (1) x y z are -+, xpygz => xpy-gz-        for  -p-g--                     gives  -p--g---
[Adding --p-g--- to bucket]

=== BUCKET 2: -p--g---/--p-g---

P  (1) x y z are -+, xpygz => xpy-gz-        for  -p--g---                   gives  -p---g----
P  (1) x y z are -+, xpygz => xpy-gz-        for  --p-g---                   gives  --p--g----
[Adding ---p-g---- to bucket]

=== BUCKET 3: -p---g----/--p--g----/---p-g----

P  (1) x y z are -+, xpygz => xpy-gz-        for  -p---g----                 gives  -p----g-----
P  (1) x y z are -+, xpygz => xpy-gz-        for  ---p-g----                 gives  ---p--g-----
P  (1) x y z are -+, xpygz => xpy-gz-        for  --p--g----                 gives  --p---g-----
[Adding ----p-g----- to bucket]

=== BUCKET 4: -p----g-----/---p--g-----/--p---g-----/----p-g-----
```

Options are available to display theorem derivation as well:

```\$ FormalSystems definitions/NDP.yaml --quiet --derivation P-----
> Infinite number of axioms, using bucket algorithm
> Rule with several parents, using recursivity

=== BUCKET 1: --NDP-
=== BUCKET 2: --NDP---/-SD--/P--
=== BUCKET 3: --NDP-----/---SD--/---NDP--
=== BUCKET 4: --NDP-------/---NDP-----/-----SD--/P---/---NDP-
=== BUCKET 5: --NDP---------/---NDP--------/---NDP----/-------SD--/-----SD---/-SD---/----NDP---
=== BUCKET 6: ---NDP-----------/----NDP-------/---NDP-------/--NDP-----------/---------SD--/----NDP-
=== BUCKET 7: ----NDP-----------/----NDP-----/---NDP----------/---NDP--------------/--NDP-------------/-----------SD--/-------SD---/-SD----/----NDP--
=== BUCKET 8: ----NDP---------/----NDP---------------/---NDP-------------/---NDP-----------------/--NDP---------------/----NDP------/-------------SD--/-------SD----/-----SD----/-----------SD---/-----NDP-
=== BUCKET 9: --NDP-----------------/-----NDP------/----NDP-------------/---NDP--------------------/---NDP----------------/----NDP----------/----NDP-------------------/---------------SD--/-SD-----/-------------SD---/-----------SD----/P-----/-----NDP--

=== Theorem P----- found, derivation:
[1 ]  Axiom                                                                     gives  --NDP-
[2 ]  (1) x y are -+, xNDPy => xNDPxy           for  --NDP-                     gives  --NDP---
[3 ]  Axiom                                                                     gives  ---NDP--
[3 ]  (1) x y are -+, xNDPy => xNDPxy           for  --NDP---                   gives  --NDP-----
[4 ]  Axiom                                                                     gives  ----NDP-
[4 ]  (1) x y are -+, xNDPy => xNDPxy           for  ---NDP--                   gives  ---NDP-----
[4 ]  (2) z is -+, --NDPz => zSD--              for  --NDP-----                 gives  -----SD--
[5 ]  (1) x y are -+, xNDPy => xNDPxy           for  ----NDP-                   gives  ----NDP-----
[5 ]  (3) x z are -+, zSDx and x-NDPz => zSDx-  for  -----SD-- and ---NDP-----  gives  -----SD---
[6 ]  (3) x z are -+, zSDx and x-NDPz => zSDx-  for  -----SD--- and ----NDP-----  gives  -----SD----
[7 ]  (4) z is -+, z-SDz => Pz-                 for  -----SD----                gives  P-----
```

## Python API

Some tests using doctests:

```>>> from formalsystems.formalsystems import FormalSystem, Theorem
```

MIU formal system:

```>>> fs = FormalSystem()
>>> r = fs.apply_rules_step(fs.iterate_over_schema(), step=4, verbose=False)
STEP 1: MI
STEP 2: MIU/MII
STEP 3: MIIU/MIIII/MIUIU
STEP 4: MIIIIU/MIIIIIIII/MIIUIIU/MIUIUIUIU/MIU/MUI
>>> print [str(a) for a in fs.iterate_over_schema()]
['MI']
```

pg formal system:

```>>> fs = FormalSystem()
>>> r = fs.apply_rules_bucket_till(fs.iterate_over_schema(), max_turns=4, verbose=False)
=== BUCKET 1: -p-g--
=== BUCKET 2: -p--g---/--p-g---
=== BUCKET 3: -p---g----/--p--g----/---p-g----
=== BUCKET 4: -p----g-----/---p--g-----/--p---g-----/----p-g-----
>>> r = fs.apply_rules_bucket_till(fs.iterate_over_schema(), min_len=9, verbose=False)
=== BUCKET 1: -p-g--
=== BUCKET 2: -p--g---/--p-g---
=== BUCKET 3: -p---g----/--p--g----/---p-g----
```

NDP formal system:

```>>> fs = FormalSystem()
>>> r = fs.apply_rules_bucket_till(fs.iterate_over_schema(), max_turns=2, full=True, verbose=False)
=== BUCKET 1: --NDP-
=== BUCKET 2: --NDP---/-SD--/P--
```

Successful derivation:

```>>> fs = FormalSystem()
>>> r = fs.derivation_asc(fs.iterate_over_schema(), Theorem('P-----'), full=True, max_turns=10)
<BLANKLINE>
...
=== Theorem P----- found, derivation:
...
```

Failed derivation:

```>>> fs = FormalSystem()
>>> r = fs.derivation_step(fs.iterate_over_schema(), Theorem('MIUIU'), step=5)
<BLANKLINE>
...
=== Theorem MIUIU found, derivation:
...
>>> r = fs.derivation_step(fs.iterate_over_schema(), Theorem('MU'), step=5)
<BLANKLINE>
...
```

File Type Py Version Uploaded on Size
Source 2016-07-05 14KB
• Author: Alex Prengère
```Apache License
Version 2.0, January 2004

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.

"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.

"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.

"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.

"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.

"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).

"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.

"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."

"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.

2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.

3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.

4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:

(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.

may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.

5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.

6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.

7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.

8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.

9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!)  The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.