From 3651dc76186ba77d18201294d0a3ba67324905eb Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Fri, 7 Jun 2019 16:26:25 -0700 Subject: [PATCH] feat(library/init/lean): add `evalConst` The implementation is good enough for implementing extensible parsers, elaborators and tactics, but there are a few TODOs 1- We should have a better story for standalone applications. Most of them don't need `evalConst`, and the global table is just initialization overhead. 2- The global table introduces a dependency on the `Lean.Name` implementation. So, all standalone applications will depend on it. 3- We are not storing arity 0 constants in the table. This one should be easy to fix in the future. --- library/init/lean/default.lean | 1 + library/init/lean/evalconst.lean | 40 ++ library/init/lean/name.lean | 2 +- src/init/init.cpp | 3 + src/runtime/object.cpp | 17 +- src/runtime/object.h | 2 - src/stage0/CMakeLists.txt | 2 +- src/stage0/init/lean/default.cpp | 5 +- src/stage0/init/lean/evalconst.cpp | 638 +++++++++++++++++++++++++++++ tests/playground/eval2.lean | 18 + 10 files changed, 716 insertions(+), 12 deletions(-) create mode 100644 library/init/lean/evalconst.lean create mode 100644 src/stage0/init/lean/evalconst.cpp create mode 100644 tests/playground/eval2.lean diff --git a/library/init/lean/default.lean b/library/init/lean/default.lean index 473f5acd1a..71787ad64d 100644 --- a/library/init/lean/default.lean +++ b/library/init/lean/default.lean @@ -10,3 +10,4 @@ import init.lean.environment import init.lean.modifiers import init.lean.runtime import init.lean.attributes +import init.lean.evalconst diff --git a/library/init/lean/evalconst.lean b/library/init/lean/evalconst.lean new file mode 100644 index 0000000000..3b6aac2f84 --- /dev/null +++ b/library/init/lean/evalconst.lean @@ -0,0 +1,40 @@ +/- +Copyright (c) 2019 Microsoft Corporation. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Leonardo de Moura +-/ +prelude +import init.io +import init.data.array +import init.lean.name + +namespace Lean + +/- We mark this primitive as `unsafe` because it assumes the table is + being accessed by a single thread. -/ +@[extern 2 "lean_modify_constant_table"] +unsafe constant modifyConstTable (f : Array (Name × NonScalar) → Array (Name × NonScalar)) : IO Unit := default _ + +/- We mark this primitive as `unsafe` because it assumes the table is + being accessed by a single thread. -/ +@[extern 1 "lean_get_constant_table"] +unsafe constant getConstTable : IO (Array (Name × NonScalar)) := default _ + +/- We mark this primitive as `unsafe` because it assumes the table is + being accessed by a single thread. We meet this requirement by invoking + it after we have initialized all modules. + See `src/init/init.cpp` -/ +@[export lean.sort_const_table_core] +unsafe def sortConstTable : IO Unit := +modifyConstTable (λ cs, cs.qsort (λ e₁ e₂, Name.quickLt e₁.1 e₂.1)) + +/- We make this primitive as `unsafe` because it uses `unsafeCast`, and + the program may crash if the type provided by the user is incorrect. + It also assumes there are no threads trying to update the table concurrently. -/ +unsafe def evalConst (α : Type) [Inhabited α] (c : Name) : IO α := +do cs ← getConstTable, + match cs.binSearch (c, default _) (λ e₁ e₂, Name.quickLt e₁.1 e₂.1) with + | some (_, v) := pure (unsafeCast v) + | none := throw (IO.userError ("unknow constant '" ++ toString c ++ "'")) + +end Lean diff --git a/library/init/lean/name.lean b/library/init/lean/name.lean index 479945938b..cf11710d69 100644 --- a/library/init/lean/name.lean +++ b/library/init/lean/name.lean @@ -9,7 +9,7 @@ import init.data.hashable init.data.rbmap init.data.rbtree namespace Lean inductive Name -| anonymous : Name +| anonymous : Name | mkString : Name → String → Name | mkNumeral : Name → Nat → Name diff --git a/src/init/init.cpp b/src/init/init.cpp index 4f1a29c2d7..064503c122 100644 --- a/src/init/init.cpp +++ b/src/init/init.cpp @@ -22,11 +22,14 @@ lean::object* initialize_init_default(lean::object* w); lean::object* initialize_init_lean_default(lean::object* w); namespace lean { +object* sort_const_table_core(object * w); + void initialize() { save_stack_info(); initialize_util_module(); object * w = initialize_init_default(io_mk_world()); w = initialize_init_lean_default(w); + // w = sort_const_table_core(w); if (io_result_is_error(w)) { io_result_show_error(w); dec(w); diff --git a/src/runtime/object.cpp b/src/runtime/object.cpp index e4086cb969..c33ed27543 100644 --- a/src/runtime/object.cpp +++ b/src/runtime/object.cpp @@ -1799,14 +1799,17 @@ runtime_stats g_runtime_stats; static object * g_decls = nullptr; -/* Low level function for creating a Lean.Name in the runtime. */ +/* Remark: this is ugly, it forces the Lean runtime to depend on the implementation of `Lean.Name`. + This may be an issue for standalone applications. */ +extern "C" object* lean_name_mk_string(obj_arg p, obj_arg s); + obj_res mk_const_name(obj_arg p, char const * s) { - obj_res r = alloc_cnstr(1, 2, 0); - cnstr_set(r, 0, p); - cnstr_set(r, 1, mk_string(s)); - return r; + return lean_name_mk_string(p, mk_string(s)); } +/* Remark: we should improve this too. A standalone application implemented in Lean will seldom + need a table with all constants. This table is only used to implement `Lean.evalConst` + unsafe primitive. */ void register_constant(obj_arg fn, obj_arg obj) { object * p = alloc_cnstr(0, 2, 0); cnstr_set(p, 0, fn); @@ -1816,12 +1819,12 @@ void register_constant(obj_arg fn, obj_arg obj) { obj_res set_io_result(obj_arg r, obj_arg a); -obj_res modify_constant_table(obj_arg f, obj_arg w) { +extern "C" obj_res lean_modify_constant_table(obj_arg f, obj_arg w) { g_decls = apply_1(f, g_decls); return w; } -obj_res get_constant_table(obj_arg w) { +extern "C" obj_res lean_get_constant_table(obj_arg w) { inc(g_decls); return set_io_result(w, g_decls); } diff --git a/src/runtime/object.h b/src/runtime/object.h index b070294ae3..d780c4bb93 100644 --- a/src/runtime/object.h +++ b/src/runtime/object.h @@ -1504,8 +1504,6 @@ obj_res mk_const_name(obj_arg p, char const * s); inline obj_res mk_const_name(char const * s) { return mk_const_name(box(0), s); } void register_constant(obj_arg n, obj_arg obj); #define REGISTER_LEAN_FUNCTION(fn, arity, cls) ::lean::register_constant(fn, lean::alloc_closure(reinterpret_cast(cls), arity, 0)) -obj_res modify_constant_table(obj_arg f, obj_arg w); -obj_res get_constant_table(obj_arg w); // ======================================= // Module initialization/finalization diff --git a/src/stage0/CMakeLists.txt b/src/stage0/CMakeLists.txt index 29fecc054e..fcbc80ccfc 100644 --- a/src/stage0/CMakeLists.txt +++ b/src/stage0/CMakeLists.txt @@ -1 +1 @@ -add_library (stage0 OBJECT ./init/coe.cpp ./init/control/alternative.cpp ./init/control/applicative.cpp ./init/control/combinators.cpp ./init/control/conditional.cpp ./init/control/default.cpp ./init/control/estate.cpp ./init/control/except.cpp ./init/control/functor.cpp ./init/control/id.cpp ./init/control/lift.cpp ./init/control/monad.cpp ./init/control/monadfail.cpp ./init/control/option.cpp ./init/control/reader.cpp ./init/control/state.cpp ./init/core.cpp ./init/data/array/basic.cpp ./init/data/array/binsearch.cpp ./init/data/array/default.cpp ./init/data/array/qsort.cpp ./init/data/assoclist.cpp ./init/data/basic.cpp ./init/data/bytearray/basic.cpp ./init/data/bytearray/default.cpp ./init/data/char/basic.cpp ./init/data/char/default.cpp ./init/data/default.cpp ./init/data/dlist.cpp ./init/data/fin/basic.cpp ./init/data/fin/default.cpp ./init/data/hashable.cpp ./init/data/hashmap/basic.cpp ./init/data/hashmap/default.cpp ./init/data/int/basic.cpp ./init/data/int/default.cpp ./init/data/list/basic.cpp ./init/data/list/default.cpp ./init/data/list/instances.cpp ./init/data/nat/basic.cpp ./init/data/nat/bitwise.cpp ./init/data/nat/default.cpp ./init/data/nat/div.cpp ./init/data/option/basic.cpp ./init/data/option/instances.cpp ./init/data/ordering/basic.cpp ./init/data/ordering/default.cpp ./init/data/persistentarray/basic.cpp ./init/data/persistentarray/default.cpp ./init/data/random.cpp ./init/data/rbmap/basic.cpp ./init/data/rbmap/default.cpp ./init/data/rbtree/basic.cpp ./init/data/rbtree/default.cpp ./init/data/repr.cpp ./init/data/string/basic.cpp ./init/data/string/default.cpp ./init/data/tostring.cpp ./init/data/uint.cpp ./init/default.cpp ./init/env_ext.cpp ./init/fix.cpp ./init/io.cpp ./init/lean/attributes.cpp ./init/lean/compiler/closedtermcache.cpp ./init/lean/compiler/constfolding.cpp ./init/lean/compiler/default.cpp ./init/lean/compiler/export.cpp ./init/lean/compiler/initattr.cpp ./init/lean/compiler/inline.cpp ./init/lean/compiler/ir/basic.cpp ./init/lean/compiler/ir/borrow.cpp ./init/lean/compiler/ir/boxing.cpp ./init/lean/compiler/ir/checker.cpp ./init/lean/compiler/ir/compilerm.cpp ./init/lean/compiler/ir/default.cpp ./init/lean/compiler/ir/elimdead.cpp ./init/lean/compiler/ir/emitcpp.cpp ./init/lean/compiler/ir/emitutil.cpp ./init/lean/compiler/ir/expandresetreuse.cpp ./init/lean/compiler/ir/format.cpp ./init/lean/compiler/ir/freevars.cpp ./init/lean/compiler/ir/livevars.cpp ./init/lean/compiler/ir/normids.cpp ./init/lean/compiler/ir/pushproj.cpp ./init/lean/compiler/ir/rc.cpp ./init/lean/compiler/ir/resetreuse.cpp ./init/lean/compiler/ir/simpcase.cpp ./init/lean/compiler/util.cpp ./init/lean/declaration.cpp ./init/lean/default.cpp ./init/lean/disjoint_set.cpp ./init/lean/environment.cpp ./init/lean/expr.cpp ./init/lean/extern.cpp ./init/lean/format.cpp ./init/lean/kvmap.cpp ./init/lean/level.cpp ./init/lean/message.cpp ./init/lean/modifiers.cpp ./init/lean/name.cpp ./init/lean/name_mangling.cpp ./init/lean/options.cpp ./init/lean/parser/identifier.cpp ./init/lean/parser/trie.cpp ./init/lean/position.cpp ./init/lean/runtime.cpp ./init/lean/smap.cpp ./init/lean/syntax.cpp ./init/lean/trace.cpp ./init/lean/util.cpp ./init/platform.cpp ./init/util.cpp ./init/wf.cpp) +add_library (stage0 OBJECT ./init/coe.cpp ./init/control/alternative.cpp ./init/control/applicative.cpp ./init/control/combinators.cpp ./init/control/conditional.cpp ./init/control/default.cpp ./init/control/estate.cpp ./init/control/except.cpp ./init/control/functor.cpp ./init/control/id.cpp ./init/control/lift.cpp ./init/control/monad.cpp ./init/control/monadfail.cpp ./init/control/option.cpp ./init/control/reader.cpp ./init/control/state.cpp ./init/core.cpp ./init/data/array/basic.cpp ./init/data/array/binsearch.cpp ./init/data/array/default.cpp ./init/data/array/qsort.cpp ./init/data/assoclist.cpp ./init/data/basic.cpp ./init/data/bytearray/basic.cpp ./init/data/bytearray/default.cpp ./init/data/char/basic.cpp ./init/data/char/default.cpp ./init/data/default.cpp ./init/data/dlist.cpp ./init/data/fin/basic.cpp ./init/data/fin/default.cpp ./init/data/hashable.cpp ./init/data/hashmap/basic.cpp ./init/data/hashmap/default.cpp ./init/data/int/basic.cpp ./init/data/int/default.cpp ./init/data/list/basic.cpp ./init/data/list/default.cpp ./init/data/list/instances.cpp ./init/data/nat/basic.cpp ./init/data/nat/bitwise.cpp ./init/data/nat/default.cpp ./init/data/nat/div.cpp ./init/data/option/basic.cpp ./init/data/option/instances.cpp ./init/data/ordering/basic.cpp ./init/data/ordering/default.cpp ./init/data/persistentarray/basic.cpp ./init/data/persistentarray/default.cpp ./init/data/random.cpp ./init/data/rbmap/basic.cpp ./init/data/rbmap/default.cpp ./init/data/rbtree/basic.cpp ./init/data/rbtree/default.cpp ./init/data/repr.cpp ./init/data/string/basic.cpp ./init/data/string/default.cpp ./init/data/tostring.cpp ./init/data/uint.cpp ./init/default.cpp ./init/env_ext.cpp ./init/fix.cpp ./init/io.cpp ./init/lean/attributes.cpp ./init/lean/compiler/closedtermcache.cpp ./init/lean/compiler/constfolding.cpp ./init/lean/compiler/default.cpp ./init/lean/compiler/export.cpp ./init/lean/compiler/initattr.cpp ./init/lean/compiler/inline.cpp ./init/lean/compiler/ir/basic.cpp ./init/lean/compiler/ir/borrow.cpp ./init/lean/compiler/ir/boxing.cpp ./init/lean/compiler/ir/checker.cpp ./init/lean/compiler/ir/compilerm.cpp ./init/lean/compiler/ir/default.cpp ./init/lean/compiler/ir/elimdead.cpp ./init/lean/compiler/ir/emitcpp.cpp ./init/lean/compiler/ir/emitutil.cpp ./init/lean/compiler/ir/expandresetreuse.cpp ./init/lean/compiler/ir/format.cpp ./init/lean/compiler/ir/freevars.cpp ./init/lean/compiler/ir/livevars.cpp ./init/lean/compiler/ir/normids.cpp ./init/lean/compiler/ir/pushproj.cpp ./init/lean/compiler/ir/rc.cpp ./init/lean/compiler/ir/resetreuse.cpp ./init/lean/compiler/ir/simpcase.cpp ./init/lean/compiler/util.cpp ./init/lean/declaration.cpp ./init/lean/default.cpp ./init/lean/disjoint_set.cpp ./init/lean/environment.cpp ./init/lean/evalconst.cpp ./init/lean/expr.cpp ./init/lean/extern.cpp ./init/lean/format.cpp ./init/lean/kvmap.cpp ./init/lean/level.cpp ./init/lean/message.cpp ./init/lean/modifiers.cpp ./init/lean/name.cpp ./init/lean/name_mangling.cpp ./init/lean/options.cpp ./init/lean/parser/identifier.cpp ./init/lean/parser/trie.cpp ./init/lean/position.cpp ./init/lean/runtime.cpp ./init/lean/smap.cpp ./init/lean/syntax.cpp ./init/lean/trace.cpp ./init/lean/util.cpp ./init/platform.cpp ./init/util.cpp ./init/wf.cpp) diff --git a/src/stage0/init/lean/default.cpp b/src/stage0/init/lean/default.cpp index 19f69ec5ef..96ae61dd11 100644 --- a/src/stage0/init/lean/default.cpp +++ b/src/stage0/init/lean/default.cpp @@ -1,6 +1,6 @@ // Lean compiler output // Module: init.lean.default -// Imports: init.lean.compiler.default init.lean.extern init.lean.environment init.lean.modifiers init.lean.runtime init.lean.attributes +// Imports: init.lean.compiler.default init.lean.extern init.lean.environment init.lean.modifiers init.lean.runtime init.lean.attributes init.lean.evalconst #include "runtime/object.h" #include "runtime/apply.h" typedef lean::object obj; typedef lean::usize usize; @@ -20,6 +20,7 @@ obj* initialize_init_lean_environment(obj*); obj* initialize_init_lean_modifiers(obj*); obj* initialize_init_lean_runtime(obj*); obj* initialize_init_lean_attributes(obj*); +obj* initialize_init_lean_evalconst(obj*); static bool _G_initialized = false; obj* initialize_init_lean_default(obj* w) { if (_G_initialized) return w; @@ -37,5 +38,7 @@ w = initialize_init_lean_runtime(w); if (io_result_is_error(w)) return w; w = initialize_init_lean_attributes(w); if (io_result_is_error(w)) return w; +w = initialize_init_lean_evalconst(w); +if (io_result_is_error(w)) return w; return w; } diff --git a/src/stage0/init/lean/evalconst.cpp b/src/stage0/init/lean/evalconst.cpp new file mode 100644 index 0000000000..5862d0c8ab --- /dev/null +++ b/src/stage0/init/lean/evalconst.cpp @@ -0,0 +1,638 @@ +// Lean compiler output +// Module: init.lean.evalconst +// Imports: init.io init.data.array.default init.lean.name +#include "runtime/object.h" +#include "runtime/apply.h" +typedef lean::object obj; typedef lean::usize usize; +typedef lean::uint8 uint8; typedef lean::uint16 uint16; +typedef lean::uint32 uint32; typedef lean::uint64 uint64; +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wunused-parameter" +#pragma clang diagnostic ignored "-Wunused-label" +#elif defined(__GNUC__) && !defined(__CLANG__) +#pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wunused-label" +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" +#endif +obj* l_unsafeCast(obj*, obj*, obj*, obj*); +obj* l_Array_binSearchAux___main___at_Lean_evalConst___spec__1(obj*, obj*, obj*, obj*); +namespace lean { +obj* nat_sub(obj*, obj*); +} +obj* l_Lean_modifyConstTable___boxed(obj*, obj*); +obj* l_Lean_evalConst(obj*); +obj* l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(obj*, obj*, obj*); +obj* l_Lean_getConstTable___boxed(obj*); +extern "C" obj* lean_modify_constant_table(obj*, obj*); +obj* l_Lean_Name_toStringWithSep___main(obj*, obj*); +extern obj* l_Lean_Inhabited; +obj* l_Lean_evalConst___rarg___closed__1; +obj* l_Array_swap(obj*, obj*, obj*, obj*); +namespace lean { +obj* string_append(obj*, obj*); +} +obj* l_Array_binSearchAux___main___at_Lean_evalConst___spec__1___boxed(obj*, obj*, obj*, obj*); +namespace lean { +uint8 nat_dec_lt(obj*, obj*); +} +extern "C" obj* lean_get_constant_table(obj*); +obj* l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2(obj*, obj*, obj*, obj*, obj*); +extern obj* l_Char_HasRepr___closed__1; +namespace lean { +obj* nat_add(obj*, obj*); +} +namespace lean { +uint8 nat_dec_eq(obj*, obj*); +} +obj* l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1___boxed(obj*, obj*, obj*); +extern obj* l_NonScalar_Inhabited; +obj* l_Lean_sortConstTable___closed__1; +uint8 l_Lean_Name_quickLt(obj*, obj*); +obj* l_Array_size(obj*, obj*); +obj* l_Array_get(obj*, obj*, obj*, obj*); +extern obj* l_Lean_Name_toString___closed__1; +namespace lean { +uint8 nat_dec_le(obj*, obj*); +} +namespace lean { +obj* nat_div(obj*, obj*); +} +obj* l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1; +namespace lean { +obj* sort_const_table_core(obj*); +} +obj* l_Lean_evalConst___rarg(obj*, obj*, obj*); +obj* l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___boxed(obj*, obj*, obj*, obj*, obj*); +obj* l_Lean_sortConstTable___lambda__1(obj*); +obj* l_Lean_modifyConstTable___boxed(obj* x_1, obj* x_2) { +_start: +{ +obj* x_3; +x_3 = lean_modify_constant_table(x_1, x_2); +return x_3; +} +} +obj* l_Lean_getConstTable___boxed(obj* x_1) { +_start: +{ +obj* x_2; +x_2 = lean_get_constant_table(x_1); +return x_2; +} +} +obj* _init_l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1() { +_start: +{ +obj* x_1; obj* x_2; obj* x_3; +x_1 = l_Lean_Inhabited; +x_2 = l_NonScalar_Inhabited; +x_3 = lean::alloc_cnstr(0, 2, 0); +lean::cnstr_set(x_3, 0, x_1); +lean::cnstr_set(x_3, 1, x_2); +return x_3; +} +} +obj* l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2(obj* x_1, obj* x_2, obj* x_3, obj* x_4, obj* x_5) { +_start: +{ +uint8 x_6; +x_6 = lean::nat_dec_lt(x_5, x_1); +if (x_6 == 0) +{ +obj* x_7; obj* x_8; +lean::dec(x_5); +x_7 = lean::array_swap(x_3, x_4, x_1); +x_8 = lean::alloc_cnstr(0, 2, 0); +lean::cnstr_set(x_8, 0, x_4); +lean::cnstr_set(x_8, 1, x_7); +return x_8; +} +else +{ +obj* x_9; obj* x_10; obj* x_11; obj* x_12; uint8 x_13; +x_9 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1; +x_10 = lean::array_get(x_9, x_3, x_5); +x_11 = lean::cnstr_get(x_10, 0); +lean::inc(x_11); +lean::dec(x_10); +x_12 = lean::cnstr_get(x_2, 0); +x_13 = l_Lean_Name_quickLt(x_11, x_12); +lean::dec(x_11); +if (x_13 == 0) +{ +obj* x_14; obj* x_15; +x_14 = lean::mk_nat_obj(1u); +x_15 = lean::nat_add(x_5, x_14); +lean::dec(x_5); +x_5 = x_15; +goto _start; +} +else +{ +obj* x_17; obj* x_18; obj* x_19; obj* x_20; +x_17 = lean::array_swap(x_3, x_4, x_5); +x_18 = lean::mk_nat_obj(1u); +x_19 = lean::nat_add(x_4, x_18); +lean::dec(x_4); +x_20 = lean::nat_add(x_5, x_18); +lean::dec(x_5); +x_3 = x_17; +x_4 = x_19; +x_5 = x_20; +goto _start; +} +} +} +} +obj* l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(obj* x_1, obj* x_2, obj* x_3) { +_start: +{ +uint8 x_4; +x_4 = lean::nat_dec_lt(x_2, x_3); +if (x_4 == 0) +{ +lean::dec(x_2); +return x_1; +} +else +{ +obj* x_5; obj* x_6; obj* x_7; obj* x_8; obj* x_57; obj* x_58; obj* x_59; obj* x_60; obj* x_61; uint8 x_62; +x_5 = lean::nat_add(x_2, x_3); +x_6 = lean::mk_nat_obj(2u); +x_7 = lean::nat_div(x_5, x_6); +lean::dec(x_5); +x_57 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1; +x_58 = lean::array_get(x_57, x_1, x_7); +x_59 = lean::array_get(x_57, x_1, x_2); +x_60 = lean::cnstr_get(x_58, 0); +lean::inc(x_60); +lean::dec(x_58); +x_61 = lean::cnstr_get(x_59, 0); +lean::inc(x_61); +lean::dec(x_59); +x_62 = l_Lean_Name_quickLt(x_60, x_61); +lean::dec(x_61); +lean::dec(x_60); +if (x_62 == 0) +{ +x_8 = x_1; +goto block_56; +} +else +{ +obj* x_63; +x_63 = lean::array_swap(x_1, x_2, x_7); +x_8 = x_63; +goto block_56; +} +block_56: +{ +obj* x_9; obj* x_10; obj* x_11; obj* x_12; obj* x_13; uint8 x_14; +x_9 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1; +x_10 = lean::array_get(x_9, x_8, x_3); +x_11 = lean::array_get(x_9, x_8, x_2); +x_12 = lean::cnstr_get(x_10, 0); +lean::inc(x_12); +x_13 = lean::cnstr_get(x_11, 0); +lean::inc(x_13); +lean::dec(x_11); +x_14 = l_Lean_Name_quickLt(x_12, x_13); +lean::dec(x_13); +if (x_14 == 0) +{ +obj* x_15; obj* x_16; uint8 x_17; +x_15 = lean::array_get(x_9, x_8, x_7); +x_16 = lean::cnstr_get(x_15, 0); +lean::inc(x_16); +lean::dec(x_15); +x_17 = l_Lean_Name_quickLt(x_16, x_12); +lean::dec(x_12); +lean::dec(x_16); +if (x_17 == 0) +{ +obj* x_18; obj* x_19; obj* x_20; obj* x_21; obj* x_22; obj* x_23; +lean::dec(x_7); +lean::inc(x_2, 2); +x_18 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2(x_3, x_10, x_8, x_2, x_2); +lean::dec(x_10); +x_19 = lean::cnstr_get(x_18, 0); +lean::inc(x_19); +x_20 = lean::cnstr_get(x_18, 1); +lean::inc(x_20); +lean::dec(x_18); +x_21 = l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(x_20, x_2, x_19); +x_22 = lean::mk_nat_obj(1u); +x_23 = lean::nat_add(x_19, x_22); +lean::dec(x_19); +x_1 = x_21; +x_2 = x_23; +goto _start; +} +else +{ +obj* x_25; obj* x_26; obj* x_27; obj* x_28; obj* x_29; obj* x_30; obj* x_31; obj* x_32; +lean::dec(x_10); +x_25 = lean::array_swap(x_8, x_7, x_3); +lean::dec(x_7); +x_26 = lean::array_get(x_9, x_25, x_3); +lean::inc(x_2, 2); +x_27 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2(x_3, x_26, x_25, x_2, x_2); +lean::dec(x_26); +x_28 = lean::cnstr_get(x_27, 0); +lean::inc(x_28); +x_29 = lean::cnstr_get(x_27, 1); +lean::inc(x_29); +lean::dec(x_27); +x_30 = l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(x_29, x_2, x_28); +x_31 = lean::mk_nat_obj(1u); +x_32 = lean::nat_add(x_28, x_31); +lean::dec(x_28); +x_1 = x_30; +x_2 = x_32; +goto _start; +} +} +else +{ +obj* x_34; obj* x_35; obj* x_36; obj* x_37; obj* x_38; uint8 x_39; +lean::dec(x_12); +lean::dec(x_10); +x_34 = lean::array_swap(x_8, x_2, x_3); +x_35 = lean::array_get(x_9, x_34, x_7); +x_36 = lean::array_get(x_9, x_34, x_3); +x_37 = lean::cnstr_get(x_35, 0); +lean::inc(x_37); +lean::dec(x_35); +x_38 = lean::cnstr_get(x_36, 0); +lean::inc(x_38); +x_39 = l_Lean_Name_quickLt(x_37, x_38); +lean::dec(x_38); +lean::dec(x_37); +if (x_39 == 0) +{ +obj* x_40; obj* x_41; obj* x_42; obj* x_43; obj* x_44; obj* x_45; +lean::dec(x_7); +lean::inc(x_2, 2); +x_40 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2(x_3, x_36, x_34, x_2, x_2); +lean::dec(x_36); +x_41 = lean::cnstr_get(x_40, 0); +lean::inc(x_41); +x_42 = lean::cnstr_get(x_40, 1); +lean::inc(x_42); +lean::dec(x_40); +x_43 = l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(x_42, x_2, x_41); +x_44 = lean::mk_nat_obj(1u); +x_45 = lean::nat_add(x_41, x_44); +lean::dec(x_41); +x_1 = x_43; +x_2 = x_45; +goto _start; +} +else +{ +obj* x_47; obj* x_48; obj* x_49; obj* x_50; obj* x_51; obj* x_52; obj* x_53; obj* x_54; +lean::dec(x_36); +x_47 = lean::array_swap(x_34, x_7, x_3); +lean::dec(x_7); +x_48 = lean::array_get(x_9, x_47, x_3); +lean::inc(x_2, 2); +x_49 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2(x_3, x_48, x_47, x_2, x_2); +lean::dec(x_48); +x_50 = lean::cnstr_get(x_49, 0); +lean::inc(x_50); +x_51 = lean::cnstr_get(x_49, 1); +lean::inc(x_51); +lean::dec(x_49); +x_52 = l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(x_51, x_2, x_50); +x_53 = lean::mk_nat_obj(1u); +x_54 = lean::nat_add(x_50, x_53); +lean::dec(x_50); +x_1 = x_52; +x_2 = x_54; +goto _start; +} +} +} +} +} +} +obj* l_Lean_sortConstTable___lambda__1(obj* x_1) { +_start: +{ +obj* x_2; obj* x_3; obj* x_4; obj* x_5; obj* x_6; +x_2 = lean::array_get_size(x_1); +x_3 = lean::mk_nat_obj(1u); +x_4 = lean::nat_sub(x_2, x_3); +lean::dec(x_2); +x_5 = lean::mk_nat_obj(0u); +x_6 = l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(x_1, x_5, x_4); +lean::dec(x_4); +return x_6; +} +} +obj* _init_l_Lean_sortConstTable___closed__1() { +_start: +{ +obj* x_1; +x_1 = lean::alloc_closure(reinterpret_cast(l_Lean_sortConstTable___lambda__1), 1, 0); +return x_1; +} +} +namespace lean { +obj* sort_const_table_core(obj* x_1) { +_start: +{ +obj* x_2; obj* x_3; +x_2 = l_Lean_sortConstTable___closed__1; +x_3 = lean_modify_constant_table(x_2, x_1); +return x_3; +} +} +} +obj* l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___boxed(obj* x_1, obj* x_2, obj* x_3, obj* x_4, obj* x_5) { +_start: +{ +obj* x_6; +x_6 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2(x_1, x_2, x_3, x_4, x_5); +lean::dec(x_2); +lean::dec(x_1); +return x_6; +} +} +obj* l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1___boxed(obj* x_1, obj* x_2, obj* x_3) { +_start: +{ +obj* x_4; +x_4 = l_Array_qsortAux___main___at_Lean_sortConstTable___spec__1(x_1, x_2, x_3); +lean::dec(x_3); +return x_4; +} +} +obj* l_Array_binSearchAux___main___at_Lean_evalConst___spec__1(obj* x_1, obj* x_2, obj* x_3, obj* x_4) { +_start: +{ +uint8 x_5; +x_5 = lean::nat_dec_le(x_3, x_4); +if (x_5 == 0) +{ +obj* x_6; +lean::dec(x_4); +lean::dec(x_3); +x_6 = lean::box(0); +return x_6; +} +else +{ +obj* x_7; obj* x_8; obj* x_9; obj* x_10; obj* x_11; obj* x_12; obj* x_13; uint8 x_14; +x_7 = lean::nat_add(x_3, x_4); +x_8 = lean::mk_nat_obj(2u); +x_9 = lean::nat_div(x_7, x_8); +lean::dec(x_7); +x_10 = l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1; +x_11 = lean::array_get(x_10, x_1, x_9); +x_12 = lean::cnstr_get(x_11, 0); +lean::inc(x_12); +x_13 = lean::cnstr_get(x_2, 0); +x_14 = l_Lean_Name_quickLt(x_12, x_13); +if (x_14 == 0) +{ +uint8 x_15; +lean::dec(x_4); +x_15 = l_Lean_Name_quickLt(x_13, x_12); +lean::dec(x_12); +if (x_15 == 0) +{ +obj* x_16; +lean::dec(x_9); +lean::dec(x_3); +x_16 = lean::alloc_cnstr(1, 1, 0); +lean::cnstr_set(x_16, 0, x_11); +return x_16; +} +else +{ +obj* x_17; uint8 x_18; +lean::dec(x_11); +x_17 = lean::mk_nat_obj(0u); +x_18 = lean::nat_dec_eq(x_9, x_17); +if (x_18 == 0) +{ +obj* x_19; obj* x_20; +x_19 = lean::mk_nat_obj(1u); +x_20 = lean::nat_sub(x_9, x_19); +lean::dec(x_9); +x_4 = x_20; +goto _start; +} +else +{ +obj* x_22; +lean::dec(x_9); +lean::dec(x_3); +x_22 = lean::box(0); +return x_22; +} +} +} +else +{ +obj* x_23; obj* x_24; +lean::dec(x_12); +lean::dec(x_11); +lean::dec(x_3); +x_23 = lean::mk_nat_obj(1u); +x_24 = lean::nat_add(x_9, x_23); +lean::dec(x_9); +x_3 = x_24; +goto _start; +} +} +} +} +obj* _init_l_Lean_evalConst___rarg___closed__1() { +_start: +{ +obj* x_1; +x_1 = lean::mk_string("unknow constant '"); +return x_1; +} +} +obj* l_Lean_evalConst___rarg(obj* x_1, obj* x_2, obj* x_3) { +_start: +{ +obj* x_4; +x_4 = lean_get_constant_table(x_3); +if (lean::obj_tag(x_4) == 0) +{ +uint8 x_5; +x_5 = !lean::is_exclusive(x_4); +if (x_5 == 0) +{ +obj* x_6; obj* x_7; obj* x_8; obj* x_9; obj* x_10; obj* x_11; obj* x_12; +x_6 = lean::cnstr_get(x_4, 0); +x_7 = lean::mk_nat_obj(0u); +lean::inc(x_2); +x_8 = lean::alloc_cnstr(0, 2, 0); +lean::cnstr_set(x_8, 0, x_2); +lean::cnstr_set(x_8, 1, x_7); +x_9 = lean::array_get_size(x_6); +x_10 = lean::mk_nat_obj(1u); +x_11 = lean::nat_sub(x_9, x_10); +lean::dec(x_9); +x_12 = l_Array_binSearchAux___main___at_Lean_evalConst___spec__1(x_6, x_8, x_7, x_11); +lean::dec(x_8); +lean::dec(x_6); +if (lean::obj_tag(x_12) == 0) +{ +obj* x_13; obj* x_14; obj* x_15; obj* x_16; obj* x_17; obj* x_18; +lean::dec(x_1); +x_13 = l_Lean_Name_toString___closed__1; +x_14 = l_Lean_Name_toStringWithSep___main(x_13, x_2); +x_15 = l_Lean_evalConst___rarg___closed__1; +x_16 = lean::string_append(x_15, x_14); +lean::dec(x_14); +x_17 = l_Char_HasRepr___closed__1; +x_18 = lean::string_append(x_16, x_17); +lean::cnstr_set_tag(x_4, 1); +lean::cnstr_set(x_4, 0, x_18); +return x_4; +} +else +{ +obj* x_19; obj* x_20; obj* x_21; +lean::dec(x_2); +x_19 = lean::cnstr_get(x_12, 0); +lean::inc(x_19); +lean::dec(x_12); +x_20 = lean::cnstr_get(x_19, 1); +lean::inc(x_20); +lean::dec(x_19); +x_21 = x_20; +lean::cnstr_set(x_4, 0, x_21); +return x_4; +} +} +else +{ +obj* x_22; obj* x_23; obj* x_24; obj* x_25; obj* x_26; obj* x_27; obj* x_28; obj* x_29; +x_22 = lean::cnstr_get(x_4, 0); +x_23 = lean::cnstr_get(x_4, 1); +lean::inc(x_23); +lean::inc(x_22); +lean::dec(x_4); +x_24 = lean::mk_nat_obj(0u); +lean::inc(x_2); +x_25 = lean::alloc_cnstr(0, 2, 0); +lean::cnstr_set(x_25, 0, x_2); +lean::cnstr_set(x_25, 1, x_24); +x_26 = lean::array_get_size(x_22); +x_27 = lean::mk_nat_obj(1u); +x_28 = lean::nat_sub(x_26, x_27); +lean::dec(x_26); +x_29 = l_Array_binSearchAux___main___at_Lean_evalConst___spec__1(x_22, x_25, x_24, x_28); +lean::dec(x_25); +lean::dec(x_22); +if (lean::obj_tag(x_29) == 0) +{ +obj* x_30; obj* x_31; obj* x_32; obj* x_33; obj* x_34; obj* x_35; obj* x_36; +lean::dec(x_1); +x_30 = l_Lean_Name_toString___closed__1; +x_31 = l_Lean_Name_toStringWithSep___main(x_30, x_2); +x_32 = l_Lean_evalConst___rarg___closed__1; +x_33 = lean::string_append(x_32, x_31); +lean::dec(x_31); +x_34 = l_Char_HasRepr___closed__1; +x_35 = lean::string_append(x_33, x_34); +x_36 = lean::alloc_cnstr(1, 2, 0); +lean::cnstr_set(x_36, 0, x_35); +lean::cnstr_set(x_36, 1, x_23); +return x_36; +} +else +{ +obj* x_37; obj* x_38; obj* x_39; obj* x_40; +lean::dec(x_2); +x_37 = lean::cnstr_get(x_29, 0); +lean::inc(x_37); +lean::dec(x_29); +x_38 = lean::cnstr_get(x_37, 1); +lean::inc(x_38); +lean::dec(x_37); +x_39 = x_38; +x_40 = lean::alloc_cnstr(0, 2, 0); +lean::cnstr_set(x_40, 0, x_39); +lean::cnstr_set(x_40, 1, x_23); +return x_40; +} +} +} +else +{ +uint8 x_41; +lean::dec(x_2); +lean::dec(x_1); +x_41 = !lean::is_exclusive(x_4); +if (x_41 == 0) +{ +return x_4; +} +else +{ +obj* x_42; obj* x_43; obj* x_44; +x_42 = lean::cnstr_get(x_4, 0); +x_43 = lean::cnstr_get(x_4, 1); +lean::inc(x_43); +lean::inc(x_42); +lean::dec(x_4); +x_44 = lean::alloc_cnstr(1, 2, 0); +lean::cnstr_set(x_44, 0, x_42); +lean::cnstr_set(x_44, 1, x_43); +return x_44; +} +} +} +} +obj* l_Lean_evalConst(obj* x_1) { +_start: +{ +obj* x_2; +x_2 = lean::alloc_closure(reinterpret_cast(l_Lean_evalConst___rarg), 3, 0); +return x_2; +} +} +obj* l_Array_binSearchAux___main___at_Lean_evalConst___spec__1___boxed(obj* x_1, obj* x_2, obj* x_3, obj* x_4) { +_start: +{ +obj* x_5; +x_5 = l_Array_binSearchAux___main___at_Lean_evalConst___spec__1(x_1, x_2, x_3, x_4); +lean::dec(x_2); +lean::dec(x_1); +return x_5; +} +} +obj* initialize_init_io(obj*); +obj* initialize_init_data_array_default(obj*); +obj* initialize_init_lean_name(obj*); +static bool _G_initialized = false; +obj* initialize_init_lean_evalconst(obj* w) { +if (_G_initialized) return w; +_G_initialized = true; +if (io_result_is_error(w)) return w; +w = initialize_init_io(w); +if (io_result_is_error(w)) return w; +w = initialize_init_data_array_default(w); +if (io_result_is_error(w)) return w; +w = initialize_init_lean_name(w); +if (io_result_is_error(w)) return w; +REGISTER_LEAN_FUNCTION(lean::mk_const_name(lean::mk_const_name("Lean"), "modifyConstTable"), 2, l_Lean_modifyConstTable___boxed); +REGISTER_LEAN_FUNCTION(lean::mk_const_name(lean::mk_const_name("Lean"), "getConstTable"), 1, l_Lean_getConstTable___boxed); +l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1 = _init_l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1(); +lean::mark_persistent(l___private_init_data_array_qsort_1__partitionAux___main___at_Lean_sortConstTable___spec__2___closed__1); +l_Lean_sortConstTable___closed__1 = _init_l_Lean_sortConstTable___closed__1(); +lean::mark_persistent(l_Lean_sortConstTable___closed__1); +REGISTER_LEAN_FUNCTION(lean::mk_const_name(lean::mk_const_name("Lean"), "sortConstTable"), 1, lean::sort_const_table_core); +l_Lean_evalConst___rarg___closed__1 = _init_l_Lean_evalConst___rarg___closed__1(); +lean::mark_persistent(l_Lean_evalConst___rarg___closed__1); +REGISTER_LEAN_FUNCTION(lean::mk_const_name(lean::mk_const_name("Lean"), "evalConst"), 1, l_Lean_evalConst); +return w; +} diff --git a/tests/playground/eval2.lean b/tests/playground/eval2.lean new file mode 100644 index 0000000000..154a5d75f9 --- /dev/null +++ b/tests/playground/eval2.lean @@ -0,0 +1,18 @@ +import init.lean.evalconst +open Lean + +def add10 (n : Nat) := n+10 +def mul10 (n : Nat) := n*10 +def inc (n : Nat) := n+1 + +unsafe def eval (fName : Name) (n : Nat) : IO Unit := +do f ← evalConst (Nat → Nat) fName, + IO.println (f n) + +unsafe def main (xs : List String) : IO Unit := +do let x := xs.head.toNat, + sortConstTable, -- we don't sort the constant table by default in standalone applications + eval `add10 x, + eval `mul10 x, + eval `inc x, + pure ()