diff --git a/README.md b/README.md index e88d9f0..a3a607e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/ROOT b/ROOT index 08c89aa..8c5a2fc 100644 --- a/ROOT +++ b/ROOT @@ -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" + diff --git a/code_python.ML b/code_python.ML index ccfce7d..fa00346 100644 --- a/code_python.ML +++ b/code_python.ML @@ -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 = @@ -27,6 +28,16 @@ val tail_calls = then Attrib.setup_option_bool ("python_tail_calls", \<^here>) else Attrib.setup_config_bool \<^binding>\python_tail_calls\ (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>\python_cache\ (K true); + structure Transparent_Wrapper_Data = Theory_Data ( type T = unit Symtab.table @@ -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 = @@ -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 @@ -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 diff --git a/etc/options b/etc/options index b9895b2..92e856c 100644 --- a/etc/options +++ b/etc/options @@ -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)" diff --git a/test/quick/Quick_Test_No_Cache.thy b/test/quick/Quick_Test_No_Cache.thy new file mode 100644 index 0000000..f470a43 --- /dev/null +++ b/test/quick/Quick_Test_No_Cache.thy @@ -0,0 +1,19 @@ +theory Quick_Test_No_Cache + imports Main "Python.Python_Setup" +begin + +text \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.\ + +declare [[python_cache = false]] + +definition nc_value :: nat where "nc_value = sum_list [1..<11]" + +definition nc_less :: "int \ int \ bool" where "nc_less x y = (x < y)" + +definition nc_sorted :: "int list \ bool" where "nc_sorted = sorted" + +export_code nc_value nc_less nc_sorted + in Python module_name Quick_Test_No_Cache file_prefix "." + +end diff --git a/test/quick/run_test.py b/test/quick/run_test.py index e248914..a3cae36 100644 --- a/test/quick/run_test.py +++ b/test/quick/run_test.py @@ -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():