Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
69255d0
Native characters, reserved builtins and undefined as stub
rolyp-claude-bot Oct 24, 2026
1f83418
Trailing underscore for a name clashing with a keyword or builtin
rolyp-claude-bot Oct 24, 2026
90b6d25
Example of names clashing with builtins deleted, since other examples…
rolyp-claude-bot Oct 24, 2026
33d02c7
Example of characters deleted, since the quick test checks the same d…
rolyp-claude-bot Oct 24, 2026
8f50d4d
Header naming the source theories of each generated file
rolyp-claude-bot Oct 24, 2026
47dd03d
Characters section of quick test in the style of the others
rolyp-claude-bot Oct 24, 2026
e16b72c
README paragraph on the setup theory as one sentence
rolyp-claude-bot Oct 24, 2026
6c5aaec
Single blank lines between character tests, as elsewhere in the file
rolyp-claude-bot Oct 24, 2026
98ad16b
Reserved builtins read from the builtins module of the Python on the …
rolyp-claude-bot Oct 24, 2026
4cd0c09
Target Python version from the spec repository, used throughout
rolyp-claude-bot Oct 24, 2026
f80a8cb
Shorter comment on reserved builtins
rolyp-claude-bot Oct 24, 2026
2da0531
Comment on reserved builtins states their purpose
rolyp-claude-bot Oct 24, 2026
74143a6
Keywords read from the target Python, and header names defined once
rolyp-claude-bot Oct 24, 2026
6c7061a
Header names as one list again
rolyp-claude-bot Oct 24, 2026
93470f4
Error for a definition with the name of a module imported by its module
rolyp-claude-bot Oct 24, 2026
87d4a15
Merge remote-tracking branch 'origin/main' into 3-names-and-characters
rolyp-claude-bot Oct 24, 2026
2b1bf36
Abort helper for constants without code, with the constant's full name
rolyp-claude-bot Oct 24, 2026
a4ce2a6
No warning for a constant without code equations
rolyp-claude-bot Oct 24, 2026
75b68c6
Trailing underscore for a definition with the name of a module
rolyp-claude-bot Oct 24, 2026
f7636c9
Prelude names, in place of header names
rolyp-claude-bot Oct 24, 2026
5f5d588
Character tests deleted, pending the oracle of #4
rolyp-claude-bot Oct 24, 2026
8121eed
Test of a definition named after a module deleted
rolyp-claude-bot Oct 24, 2026
7da3102
Comment on datatypes as before
rolyp-claude-bot Oct 24, 2026
afe2479
Typos in comment on function printer
rolyp-claude-bot Oct 24, 2026
bcab221
Shorter comment on renaming
rolyp-claude-bot Oct 24, 2026
6162de2
Trailing underscores for every clash, including duplicate names
rolyp-claude-bot Oct 24, 2026
59ddfab
Comment on first naming pass as an operation
rolyp-claude-bot Oct 24, 2026
0616e60
No fallback for an unknown theory name; table of theory names built once
rolyp-claude-bot Oct 24, 2026
0b01168
Explicit tests in place of caught exceptions for printed constants
rolyp-claude-bot Oct 24, 2026
6de7f3f
Shorter comment on constant arity
rolyp-claude-bot Oct 24, 2026
e8bdc12
Shorter comment on constructor test
rolyp-claude-bot Oct 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install python3/pip and pytest
run: |
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
pip3 install --break-system-packages pytest 2>/dev/null || pip3 install pytest
- name: Install uv
uses: astral-sh/setup-uv@v6

- name: extract Python code
run: /home/isabelle/Isabelle/bin/isabelle build -v -d . -e Python_Test_Quick

- name: Run quick test suite
run: pytest test/quick/run_test.py
run: uv run pytest test/quick/run_test.py

diff:
runs-on: ubuntu-latest
Expand All @@ -36,17 +33,14 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install python3/pip and pytest
run: |
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
pip3 install --break-system-packages pytest 2>/dev/null || pip3 install pytest
- name: Install uv
uses: astral-sh/setup-uv@v6

- name: extract Python code
run: /home/isabelle/Isabelle/bin/isabelle build -v -d . -e Python_Examples

- name: Run diff test suite
run: pytest test/diff/run_test.py
run: uv run pytest test/diff/run_test.py

slow:
runs-on: ubuntu-latest
Expand All @@ -56,14 +50,14 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install python3
run: apt-get update && apt-get install -y --no-install-recommends python3
- name: Install uv
uses: astral-sh/setup-uv@v6

- name: extract Python code
run: ISABELLE_PYTHON=1 /home/isabelle/Isabelle/bin/isabelle build -v -d . -e Python_Test_Slow

- name: syntax check generated code
run: |
python3 -m py_compile test/slow/generated_basic/*.py
python3 -m py_compile test/slow/generated_full/*.py
uv run python -m py_compile test/slow/generated_basic/*.py
uv run python -m py_compile test/slow/generated_full/*.py

2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.14
3.12
2 changes: 0 additions & 2 deletions Python_IArray.thy
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ text \<open>
depend on HOL-Library.IArray.
\<close>

code_reserved (Python) tuple range all any

code_printing
type_constructor iarray \<rightharpoonup> (Python) "_ tuple"
| constant IArray \<rightharpoonup> (Python) "tuple'(_')"
Expand Down
25 changes: 23 additions & 2 deletions Python_Setup.thy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
theory Python_Setup
imports Python "HOL-Library.Code_Target_Int" "HOL-Library.Code_Target_Nat"
"HOL-Library.Code_Abstract_Char"
begin

code_identifier
Expand Down Expand Up @@ -122,8 +123,28 @@ setup \<open>
\<close>


(* Python builtins used by the printings in this theory; generated names must not shadow them *)
code_reserved (Python) len list map max min abs divmod
(* Characters, as one-character strings *)
code_printing
type_constructor char \<rightharpoonup> (Python) "str"
| constant Code_Abstract_Char.Chr \<rightharpoonup> (Python) "chr'(_')"
| constant String.integer_of_char \<rightharpoonup> (Python) "ord'(_')"
| constant "HOL.equal :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup> (Python) infix 4 "=="
| constant "(\<le>) :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup> (Python) infixl 35 "<="
| constant "(<) :: char \<Rightarrow> char \<Rightarrow> bool" \<rightharpoonup> (Python) infixl 35 "<"
| constant String.explode \<rightharpoonup> (Python) "list'(_')"
| constant String.implode \<rightharpoonup> (Python) "\"\".join'(_')"

(* Each character constant of Code_Abstract_Char as a string literal *)
setup \<open>
let
fun literal n =
if 32 <= n andalso n < 127 then Code_Python.print_python_string (String.str (Char.chr n))
else quote ("\\x" ^ align_right "0" 2 (Int.fmt StringCvt.HEX n));
fun printing n = Code_Target.set_printings (Code_Symbol.Constant
("Code_Abstract_Char.Char_" ^ String_Syntax.hex n,
[("Python", SOME (Code_Printer.plain_const_syntax (literal n)))]));
in fold printing (0 upto 255) end
\<close>

(* Lists *)
code_printing
Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Python 3.

- A recent Isabelle/HOL release (with `isabelle` on your `PATH`, currently only
`Isabelle2025-2` has been tested)
- Python 3.10+ (generated pattern-matching code uses `match`/`case`)
- `pytest`, if you want to run the tests
- [uv](https://docs.astral.sh/uv/), which runs Python at the version in `.python-version`, the target version of
the generated code

## Installing as a component

Expand Down Expand Up @@ -38,10 +38,9 @@ In order to use this generator you have to import one of the following theories:
Strings), and relies on the automatically extracted HOL definitions for other
things.

`Python.Python_Setup` includes additional custom code printers to utilise
Python native integers for `int` and `nat`, as well as using Python's own
list and tuple data types. Most people will probably want to use this setup for
code generation.
`Python.Python_Setup` extends `Python.Python` by printing `int` and `nat` as
Python integers, lists as Python lists, pairs as tuples and `char` as
one-character strings.

### Tail calls as loops

Expand Down Expand Up @@ -79,7 +78,7 @@ and runs various tests defined in `/test/quick/run_test.py`.

```bash
isabelle build -d . -e Python_Test_Quick
pytest test/quick/run_test.py -v
uv run pytest test/quick/run_test.py -v
```

- **slow**
Expand All @@ -93,8 +92,8 @@ with `Python.Python_Setup`. Unfortunately, we can currently not use Isabelle's

```bash
isabelle build -d . -e Python_Test_Slow
python3 -m py_compile test/slow/generated_basic/*.py
python3 -m py_compile test/slow/generated_full/*.py
uv run python -m py_compile test/slow/generated_basic/*.py
uv run python -m py_compile test/slow/generated_full/*.py
```

- **diff**
Expand All @@ -106,15 +105,15 @@ suite holds golden reference files under `test/diff/golden/`.

```bash
isabelle build -d . -e Python_Examples
pytest test/diff/run_test.py
uv run pytest test/diff/run_test.py
```

If a new example is added, or the output has changed and is confirmed correct,
the golden files can be recreated by

```bash
isabelle build -d . -e Python_Examples
python3 test/diff/reseed_goldens.py
uv run python test/diff/reseed_goldens.py
```

## Known limitations
Expand Down
Loading
Loading