Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
56 changes: 56 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,42 @@ jobs:
- name: Install uv
uses: astral-sh/setup-uv@v6

# -o python_type_annotations needs the option declared in etc/options, which Isabelle
# only reads for a registered component; the Python session must be built after this,
# or it is compiled without the option and -o has no effect
- name: register component
run: /home/isabelle/Isabelle/bin/isabelle components -u "$PWD"

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

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

- name: extract Python code with type annotations
run: |
/home/isabelle/Isabelle/bin/isabelle build -v -d . -c -o python_type_annotations -e Python_Examples
grep -q "from __future__ import annotations" examples/python_out/Python_Examples.Example/code/Example.py

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

- name: type-check generated code
# each example has its own copy of the library modules, so check them one by one
run: |
status=0
for d in examples/python_out/*/code; do uv run mypy "$d" || status=1; done
exit $status

- name: type-check generated code with ty
# ty finds the casts that are there for mypy redundant
run: |
status=0
for d in examples/python_out/*/code; do
uv run ty check --python-version 3.12 --no-progress --ignore redundant-cast --extra-search-path "$d" "$d" || status=1
done
exit $status

oracle:
runs-on: ubuntu-latest
container:
Expand Down Expand Up @@ -73,11 +103,37 @@ jobs:
- name: Install uv
uses: astral-sh/setup-uv@v6

# -o python_type_annotations needs the option declared in etc/options, which Isabelle
# only reads for a registered component; the Python session must be built after this,
# or it is compiled without the option and -o has no effect
- name: register component
run: /home/isabelle/Isabelle/bin/isabelle components -u "$PWD"

- 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: |
uv run python -m py_compile test/slow/generated_basic/*.py
uv run python -m py_compile test/slow/generated_full/*.py

- name: import check generated code
run: uv run pytest test/slow/run_test.py --tb=line

- name: extract Python code with type annotations
run: |
/home/isabelle/Isabelle/bin/isabelle build -v -d . -c -o python_type_annotations -e Python_Test_Slow
grep -q "from __future__ import annotations" test/slow/generated_full/Nat.py

- name: type-check generated code
run: |
uv run mypy test/slow/generated_basic
uv run mypy test/slow/generated_full

- name: type-check generated code with ty
# ty finds the casts that are there for mypy redundant
run: |
for d in test/slow/generated_basic test/slow/generated_full; do
uv run ty check --python-version 3.12 --no-progress --ignore redundant-cast --extra-search-path "$d" "$d"
done

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ python_out/
generated_basic/
generated_full/
__pycache__/
.mypy_cache/

# Editor backups
*.thy~
Expand Down
25 changes: 23 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ISABELLE = isabelle
PYTEST = uv run pytest
PYTHON = uv run python3
# ty finds the casts that are there for mypy redundant
TY = uv run ty check --python-version 3.12 --no-progress --ignore redundant-cast

default: build

Expand All @@ -10,15 +12,17 @@ build-test-slow:
test-slow: build-test-slow
$(PYTHON) -m py_compile test/slow/generated_basic/*.py
$(PYTHON) -m py_compile test/slow/generated_full/*.py
$(PYTEST) test/slow/run_test.py

build-test-quick:
$(ISABELLE) build -d . -e Python_Test_Quick

test-quick:
$(PYTEST) test/quick/run_test.py

# -c: a changed python_type_annotations does not rebuild the session by itself
build-examples:
$(ISABELLE) build -d . -e Python_Examples
$(ISABELLE) build -d . -c -e Python_Examples

test-diff: build-examples
$(PYTEST) test/diff/run_test.py
Expand All @@ -29,9 +33,26 @@ build-test-oracle:
test-oracle: build-test-oracle
$(PYTEST) test/oracle/run_test.py

typecheck-diff:
$(ISABELLE) build -d . -c -o python_type_annotations -e Python_Examples
$(PYTEST) test/diff/run_test.py
status=0; for d in examples/python_out/*/code; do uv run mypy $$d || status=1; done; exit $$status
status=0; for d in examples/python_out/*/code; do $(TY) --extra-search-path $$d $$d || status=1; done; exit $$status

typecheck-slow:
$(ISABELLE) build -d . -c -o python_type_annotations -e Python_Test_Slow
uv run mypy test/slow/generated_basic
uv run mypy test/slow/generated_full
$(TY) --extra-search-path test/slow/generated_basic test/slow/generated_basic
$(TY) --extra-search-path test/slow/generated_full test/slow/generated_full

reseed: build-examples
$(PYTHON) test/diff/reseed_goldens.py

reseed-annotated:
$(ISABELLE) build -d . -c -o python_type_annotations -e Python_Examples
$(PYTHON) test/diff/reseed_goldens.py

reserved-words:
$(PYTHON) etc/update_reserved_words.py

Expand All @@ -42,7 +63,7 @@ test: test-quick
build:
$(ISABELLE) build -d . -e Python

.PHONY: default build-test-slow test-slow build-test-quick test-quick build-examples test-diff reseed test-all test build-test-oracle test-oracle build reserved-words
.PHONY: default build-test-slow test-slow build-test-quick test-quick build-examples test-diff typecheck-diff typecheck-slow reseed reseed-annotated test-all test build-test-oracle test-oracle build reserved-words



Expand Down
18 changes: 10 additions & 8 deletions Python_IArray.thy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ text \<open>
\<close>

code_printing
type_constructor iarray \<rightharpoonup> (Python) "_ tuple"
type_constructor iarray \<rightharpoonup> (Python) "tuple[_, ...]"
| constant IArray \<rightharpoonup> (Python) "tuple'(_')"
| constant IArray.list_of \<rightharpoonup> (Python) "list'(_')"
| constant IArray.sub \<rightharpoonup> (Python) "_[_]"
Expand All @@ -24,7 +24,9 @@ code_printing
| constant IArray.exists \<rightharpoonup> (Python) "any'(map'(_, _')')"

text \<open>@{const IArray.sub'} and @{const IArray.tabulate} take a pair; a literal pair is
printed directly, any other pair expression is unpacked into a lambda.\<close>
printed directly, any other pair expression is passed to a lambda that takes it apart
by indexing (\<open>(lambda p: p[0][p[1]])(t)\<close>), so that it is evaluated once. Unpacking
it with \<open>*\<close> instead would lose the types of its components for a type checker.\<close>

setup \<open>
let
Expand All @@ -34,16 +36,16 @@ setup \<open>
`$ a `$ b) = SOME (a, b)
| dest_pair _ = NONE;

fun paired name params body = fn print_term => fn vars => fn t =>
fun paired body = fn print_term => fn vars => fn t =>
case dest_pair t of
SOME (a, b) => body (print_term vars BR a) (print_term vars NOBR b)
| NONE => Pretty.block [Pretty.str ("(lambda " ^ params ^ ": "),
body (Pretty.str (fst name)) (Pretty.str (snd name)),
Pretty.str ")(*", print_term vars NOBR t, Pretty.str ")"];
| NONE => Pretty.block [Pretty.str "(lambda p: ",
body (Pretty.str "p[0]") (Pretty.str "p[1]"),
Pretty.str ")(", print_term vars NOBR t, Pretty.str ")"];

val sub' = paired ("a", "i") "a, i" (fn a => fn i =>
val sub' = paired (fn a => fn i =>
Pretty.block [a, Pretty.str "[", i, Pretty.str "]"]);
val tabulate = paired ("n", "f") "n, f" (fn n => fn f =>
val tabulate = paired (fn n => fn f =>
Pretty.block [Pretty.str "tuple(map(", f, Pretty.str ", range(", n, Pretty.str ")))"]);

fun set target (c, pr) =
Expand Down
4 changes: 2 additions & 2 deletions Python_Setup.thy
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ setup \<open>

(* Lists *)
code_printing
type_constructor List.list \<rightharpoonup> (Python) "_ list"
type_constructor List.list \<rightharpoonup> (Python) "list[_]"
| constant List.list.Nil \<rightharpoonup> (Python) "[]"

code_printing
Expand Down Expand Up @@ -192,7 +192,7 @@ setup \<open> Code_Python.enable_native_encoding "list" \<close>

(* Pairs *)
code_printing
type_constructor Product_Type.prod \<rightharpoonup> (Python) "(_ * _)"
type_constructor Product_Type.prod \<rightharpoonup> (Python) "tuple[_, _]"
| constant Product_Type.Pair \<rightharpoonup> (Python) "(_, _)"
| constant fst \<rightharpoonup> (Python) "_[0]"
| constant snd \<rightharpoonup> (Python) "_[1]"
Expand Down
Loading
Loading