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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ def num_of_nat():
return (Fun.comp(Code_Numeral.num_of_integer, lambda a: a))
```

The option `python_cache` (on by default) controls the `@functools.cache`
decorator, on values and on instances without dictionary arguments. Turn it off
to measure what the caching gains; values are then still functions without
arguments, but are recomputed on every use:

```bash
isabelle build -o python_cache=false -d . -e Python_Examples
```

or `declare [[python_cache = false]]` in a theory, before `export_code`.

Code that uses the generated modules therefore calls a value too
(`Example_nat.nat_sum()`). An alias of a function (`f_int = f`) is printed as an
ordinary function instead.
Expand Down
1 change: 1 addition & 0 deletions ROOT
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ session Python_Test_Quick in "test/quick" = Python +
Quick_Test
Quick_Test_Tail_Calls
Quick_Test_Name_Clash
Quick_Test_No_Cache
export_files (in "python_out") "*:**"

session Python_Test_Slow in "test/slow" = "HOL-Library" +
Expand Down
18 changes: 15 additions & 3 deletions code_python.ML
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ sig
val enable_native_encoding: string -> theory -> theory
val print_python_string: string -> string
val tail_calls: bool Config.T
val cache: bool Config.T
end;

structure Code_Python : CODE_PYTHON =
Expand All @@ -27,6 +28,16 @@ val tail_calls =
then Attrib.setup_option_bool ("python_tail_calls", \<^here>)
else Attrib.setup_config_bool \<^binding>\<open>python_tail_calls\<close> (K false);

(* Memoise values and dictionary-free instances with @functools.cache (see serialize_python).
On by default; turn it off with `declare [[python_cache = false]]` or
`isabelle build -o python_cache=false`, e.g. to measure what the caching gains. Without it,
values are still printed as functions without arguments, but are recomputed on every use.
Falls back to a configuration option as python_tail_calls does. *)
val cache =
if can Options.default_typ "python_cache"
then Attrib.setup_option_bool ("python_cache", \<^here>)
else Attrib.setup_config_bool \<^binding>\<open>python_cache\<close> (K true);

structure Transparent_Wrapper_Data = Theory_Data
(
type T = unit Symtab.table
Expand Down Expand Up @@ -732,7 +743,8 @@ fun print_python_stmt undefineds const_syntax reserved args_num
Pretty.enum "," "(" ")" dict_args, Pretty.str ":"]
(* an instance without dictionary arguments always builds the same
(frozen) dictionary, so build it once *)
val decorator = if null dict_args then [Pretty.str "@functools.cache"] else []
val decorator = if null dict_args andalso Config.get ctxt cache
then [Pretty.str "@functools.cache"] else []
in Pretty.chunks (decorator @ [head, indent_block 4 body]) end;

fun print_func_head const vctxt dicts params =
Expand Down Expand Up @@ -892,7 +904,7 @@ fun print_python_stmt undefineds const_syntax reserved args_num
arguments that is evaluated once, when it is first used *)
let
val { rhs = t, thm = some_thm, ... } = hd eqs
val decorator = if is_cached_value (Constant const)
val decorator = if is_cached_value (Constant const) andalso Config.get ctxt cache
then [Pretty.str "@functools.cache"] else []
in Pretty.chunks (decorator @
[head, indent_block 4 (loop_body (print_rhs some_thm vars2 t))]) end
Expand Down Expand Up @@ -1128,7 +1140,7 @@ fun serialize_python undefineds _ ctxt { module_name, reserved_syms,
(* modules that print a cached (dictionary-free) instance or a value need functools *)
fun needs_functools mod_name =
let val (stmt_gr, _) = Graph.get_node python_program mod_name
in Code_Symbol.Graph.fold (fn (sym, ((_, stmt), _)) => fn b => b orelse
in Config.get ctxt cache andalso Code_Symbol.Graph.fold (fn (sym, ((_, stmt), _)) => fn b => b orelse
(case stmt of
SOME (_, Code_Thingol.Classinst { vs, ... }) => forall (null o snd) vs
| SOME _ => is_cached_value sym
Expand Down
3 changes: 3 additions & 0 deletions etc/options
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ section "Python Code Generation"

option python_tail_calls : bool = false for content
-- "print self tail calls of generated Python functions as loops (see code_python.ML)"

option python_cache : bool = true for content
-- "memoise values and dictionary-free instances of generated Python code with functools.cache (see code_python.ML)"
19 changes: 19 additions & 0 deletions test/quick/Quick_Test_No_Cache.thy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
theory Quick_Test_No_Cache
imports Main "Python.Python_Setup"
begin

text \<open>Checks for @{text "python_cache = false"}: values and dictionary-free instances are printed
without @{text "@functools.cache"}, so they are recomputed on every use, but still behave the same.\<close>

declare [[python_cache = false]]

definition nc_value :: nat where "nc_value = sum_list [1..<11]"

definition nc_less :: "int \<Rightarrow> int \<Rightarrow> bool" where "nc_less x y = (x < y)"

definition nc_sorted :: "int list \<Rightarrow> bool" where "nc_sorted = sorted"

export_code nc_value nc_less nc_sorted
in Python module_name Quick_Test_No_Cache file_prefix "."

end
16 changes: 16 additions & 0 deletions test/quick/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,22 @@ def test_tail_calls_with_closures_stay_recursive():
assert tc.tc_shift(3, lambda x: x)(0) == 3


# ---- python_cache = false (Quick_Test_No_Cache.thy, exported with the option off) ----

def test_no_cache_prints_no_functools_cache():
matches = list(PYTHON_OUT_ROOT.rglob("Quick_Test_No_Cache.py"))
if not matches:
pytest.skip("Quick_Test_No_Cache.py not generated")
source = matches[0].read_text()
assert "functools" not in source
sys.path.insert(0, str(matches[0].parent))
import Quick_Test_No_Cache as nc
assert nc.nc_value() == 55
assert nc.nc_value() == 55
assert nc.nc_sorted()([1, 2, 3])
assert not nc.nc_sorted()([2, 1])


# ---- name clashes in one module (Quick_Test_Name_Clash.thy, exported with module_name) ----

def _load_name_clash():
Expand Down
Loading