Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ test-oracle: build-test-oracle
reseed: build-examples
$(PYTHON) test/diff/reseed_goldens.py

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

test-all: test-quick test-slow test-diff test-oracle

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
.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



Expand Down
17 changes: 14 additions & 3 deletions Python.thy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ begin

ML_file \<open>code_python.ML\<close>

(* Keywords and builtins of the target Python, which generated names must not hide;
make reserved-words writes the file from the Python in .python-version *)
external_file \<open>etc/python_reserved_words\<close>

setup \<open>fn thy =>
File.read_lines (Resources.master_directory thy + \<^path>\<open>etc/python_reserved_words\<close>)
|> map trim_line
|> filter_out (fn line => line = "" orelse String.isPrefix "#" line)
|> (fn names => fold (Code_Target.add_reserved Code_Python.target) names thy)
\<close>

declare [[default_code_width = 2000]]

code_identifier
Expand Down Expand Up @@ -36,11 +47,11 @@ code_printing
| constant "(+) :: String.literal \<Rightarrow> String.literal \<Rightarrow> String.literal" \<rightharpoonup>
(Python) infixl 65 "+"
| constant "HOL.equal :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup>
(Python) infixl 40 "=="
(Python) "(_ == _)"
| constant "(\<le>) :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup>
(Python) infixl 35 "<="
(Python) "(_ <= _)"
| constant "(<) :: String.literal \<Rightarrow> String.literal \<Rightarrow> bool" \<rightharpoonup>
(Python) infixl 35 "<"
(Python) "(_ < _)"

setup \<open>
Literal.add_code "Python"
Expand Down
13 changes: 7 additions & 6 deletions Python_Setup.thy
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ code_printing

code_printing
constant "abs :: int \<Rightarrow> int" \<rightharpoonup> (Python) "abs'(_')"
| constant "min" \<rightharpoonup> (Python) "min(_, _)"
| constant "max" \<rightharpoonup> (Python) "max(_, _)"
(* min and max are derived automatically from Isabelle's own definition as `if a <= b then a else b` .
This is coherent with Python's semantics and scales up to data types like sets and lists *)

(* Bridge between int and integer *)
code_printing
Expand All @@ -50,7 +50,7 @@ code_printing
| constant "(<) :: nat \<Rightarrow> nat \<Rightarrow> bool" \<rightharpoonup> (Python) "(_ < _)"

code_printing
constant Code_Numeral.nat_of_integer \<rightharpoonup> (Python) "_"
constant Code_Numeral.nat_of_integer \<rightharpoonup> (Python) "max(0, _)"
| constant Code_Numeral.integer_of_nat \<rightharpoonup> (Python) "_"

setup \<open>
Expand Down Expand Up @@ -150,18 +150,19 @@ setup \<open>
code_printing
type_constructor List.list \<rightharpoonup> (Python) "_ list"
| constant List.list.Nil \<rightharpoonup> (Python) "[]"

code_printing
constant "length" \<rightharpoonup> (Python) "len'(_')"
| constant List.nth \<rightharpoonup> (Python) "_[_]"
| constant List.map \<rightharpoonup> (Python) "list(map((_), (_)))"
| constant List.append \<rightharpoonup> (Python) "(_ + _)"
| constant List.upt \<rightharpoonup> (Python) "list(range((_), (_)))"

setup \<open>
let
open Code_Thingol Code_Printer;

fun dest_list_literal (IConst { sym = Code_Symbol.Constant
fun dest_list_literal (IConst { sym = Code_Symbol.Constant
\<^const_name>\<open>List.list.Nil\<close>, ...}) = SOME []
| dest_list_literal (IConst { sym = Code_Symbol.Constant
\<^const_name>\<open>List.list.Cons\<close>, ...} `$ x `$ xs) =
Expand All @@ -178,7 +179,7 @@ setup \<open>
| NONE =>
Pretty.block [Pretty.str "[", print_term vars NOBR x, Pretty.str "] + ",
print_term vars NOBR xs];

in
(fn target =>
Code_Target.set_printings (Code_Symbol.Constant (\<^const_name>\<open>List.list.Cons\<close>,
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ things.
Python integers, lists as Python lists, pairs as tuples and `char` as
one-character strings.

### Reserved names

Generated code must not use Python's keywords as names, nor hide the builtins
it calls itself (`len`, `list`, `range`, ...). The names to avoid are listed one
per line in `etc/python_reserved_words`: the keywords, soft keywords and
builtins of the Python version in `.python-version`. Lines starting with `#`
are comments. `Python.thy` reserves every name in the file, together with the
names that the prelude of each generated module binds (`dataclass`, `Any`,
`Callable`, `functools`, `_abort`, `_unimplemented`; `prelude_names` in
`code_python.ML`).

A definition, class or datatype with a reserved name gets a trailing
underscore (`tuple` becomes `tuple_`), and a variable gets a fresh variant
(`len` becomes `lena`).

The list is generated rather than edited by hand. After changing
`.python-version`, regenerate it with

```bash
make reserved-words
```

which runs `etc/update_reserved_words.py` with the project's Python through
`uv run`. The script refuses to run under any other Python version than the
one in `.python-version`. Commit the updated list with the version change.
`Python.thy` declares the file with `external_file`, so the next
`isabelle build` rebuilds the `Python` session, and every session built on it,
whenever the list changes.

### Tail calls as loops

Python does not eliminate tail calls, so a function that recurses once per
Expand Down
18 changes: 4 additions & 14 deletions code_python.ML
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,9 @@ fun enable_native_encoding name = Native_Encoding_Data.map (Symtab.update (name,

fun use_native_encoding thy name = Symtab.defined (Native_Encoding_Data.get thy) name;

(* Names bound by the prelude of every generated module: its imports and the abort function *)
val prelude_names = ["dataclass", "Any", "Callable", "functools", "_abort"];

(* Keywords and builtins of the target Python, except the wildcard _ printed by the generator; generated names
must not hide these. *)
fun python_names thy =
let
val version = trim_line (File.read (Resources.master_directory thy + Path.basic ".python-version"));
val (out, rc) = Isabelle_System.bash_output ("uv run -q --no-project --python " ^ Bash.string version ^
" python -c 'import builtins, keyword; " ^
"print(*keyword.kwlist, *(k for k in keyword.softkwlist if k != \"_\"), *dir(builtins))'");
in if rc = 0 then space_explode " " (trim_line out) else error ("No names of Python " ^ version) end;
(* Names bound by the prelude of every generated module; the keywords and builtins of
Python are reserved by Python.thy, from etc/python_reserved_words *)
val prelude_names = ["dataclass", "Any", "Callable", "_unimplemented", "_abort", "functools"];

type eqtn = {
patterns: iterm list, (* left hand side pattern list *)
Expand Down Expand Up @@ -1161,7 +1152,6 @@ val _ = Theory.setup
run_command = Code_Target.run_command_bash (fn module_name =>
"python3 -m py_compile " ^ Bash.string module_name ^ "/*.py")},
evaluation_args = []})
#> (fn thy => fold (Code_Target.add_reserved target)
(distinct (op =) (python_names thy @ prelude_names)) thy));
#> fold (Code_Target.add_reserved target) prelude_names);

end; (* struct *)
192 changes: 192 additions & 0 deletions etc/python_reserved_words
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Keywords and builtins of Python 3.12, written by etc/update_reserved_words.py
False
None
True
and
as
assert
async
await
break
class
continue
def
del
elif
else
except
finally
for
from
global
if
import
in
is
lambda
nonlocal
not
or
pass
raise
return
try
while
with
yield
case
match
type
ArithmeticError
AssertionError
AttributeError
BaseException
BaseExceptionGroup
BlockingIOError
BrokenPipeError
BufferError
BytesWarning
ChildProcessError
ConnectionAbortedError
ConnectionError
ConnectionRefusedError
ConnectionResetError
DeprecationWarning
EOFError
Ellipsis
EncodingWarning
EnvironmentError
Exception
ExceptionGroup
FileExistsError
FileNotFoundError
FloatingPointError
FutureWarning
GeneratorExit
IOError
ImportError
ImportWarning
IndentationError
IndexError
InterruptedError
IsADirectoryError
KeyError
KeyboardInterrupt
LookupError
MemoryError
ModuleNotFoundError
NameError
NotADirectoryError
NotImplemented
NotImplementedError
OSError
OverflowError
PendingDeprecationWarning
PermissionError
ProcessLookupError
RecursionError
ReferenceError
ResourceWarning
RuntimeError
RuntimeWarning
StopAsyncIteration
StopIteration
SyntaxError
SyntaxWarning
SystemError
SystemExit
TabError
TimeoutError
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
UnicodeWarning
UserWarning
ValueError
Warning
ZeroDivisionError
__build_class__
__debug__
__doc__
__import__
__loader__
__name__
__package__
__spec__
abs
aiter
all
anext
any
ascii
bin
bool
breakpoint
bytearray
bytes
callable
chr
classmethod
compile
complex
copyright
credits
delattr
dict
dir
divmod
enumerate
eval
exec
exit
filter
float
format
frozenset
getattr
globals
hasattr
hash
help
hex
id
input
int
isinstance
issubclass
iter
len
license
list
locals
map
max
memoryview
min
next
object
oct
open
ord
pow
print
property
quit
range
repr
reversed
round
set
setattr
slice
sorted
staticmethod
str
sum
super
tuple
vars
zip
Loading
Loading